Commit graph

119 commits

Author SHA1 Message Date
Lubomir Rintel
a482fbe1b6 tests/general: allow error margin on comparing floats
Fixes test on Fedora/i686.
2017-03-28 13:52:27 +02:00
Lubomir Rintel
ed552c732c logging: log device and connection along with the message 2017-03-24 12:42:09 +01:00
Thomas Haller
0a34ae55a2 core/trivial: rename nm_utils_10pow() to nm_utils_exp10()
nm_utils_exp10() is a better name, because it reminds of the function
exp10() from <math.h> which has a similar purpose (but whose argument
is double, not gint16).
2017-03-24 11:28:57 +01:00
Thomas Haller
25654cfa31 core: add nm_utils_10pow() utils 2017-03-23 19:06:02 +01:00
Thomas Haller
b869d9cc0d device: add spec "driver:" to match devices
Changing the MAC address of devices is known to fail with
certain drivers. Add a device-spec to allow disabling it
for for such devices.

Related: https://bugzilla.gnome.org/show_bug.cgi?id=777523
2017-03-17 17:40:00 +01:00
Thomas Haller
bed2fa1bec core: track external activations types in the active-connection
We need a distinction between external activations and assuming
connections. The former shall have the meaning of devices that are
*not* managed by NetworkManager, the latter are configurations that
are gracefully taken over after restart (but fully managed).

Express that in the activation-type of the active connection.

Also, no longer use the settings NM_SETTINGS_CONNECTION_FLAGS_VOLATILE
flag to determine whether an assumed connection is "external". These
concepts are entirely orthogonal (although in pratice, external
activations are in-memory and flagged as volatile, but the inverse
is not necessarily true).

Also change match_connection_filter() to consider all connections.
Later, we only call nm_utils_match_connection() for the connection
we want to assume -- which will be a regular settings connection,
not a generated one.
2017-03-16 18:27:33 +01:00
Thomas Haller
8a31e66d2c core: add activation-type property to active-connection
It is still unused, but will be useful to mark a connection
whether it is a full activation or assumed.
2017-03-16 18:27:33 +01:00
Thomas Haller
b1eeb00937 all: use "unsigned long" instead of "long unsigned" 2017-03-14 11:23:46 +01:00
Beniamino Galvani
ef491acfde core: split nm_utils_format_con_diff_for_audit()
The function will be called to display in audit logs the changes done
to connections.
2017-03-06 10:29:37 +01:00
Thomas Haller
93f7ab2c54 core: consolidate sorting of connections by autoconnect/timestamp
NMPolicy's auto_activate_device() wants to sort by autoconnect-priority,
nm_utils_cmp_connection_by_autoconnect_priority() but fallback to the default
nm_settings_connection_cmp_default(), which includes the timestamp.

Extend nm_settings_connection_cmp_default() to consider the
autoconnect-priority as well. Thus change behavior so that
nm_settings_connection_cmp_default() is the sort order that
auto_activate_device() wants. That makes sense, as
nm_settings_connection_cmp_default() already considered the
ability to autoconnect as first. Hence, it should also honor
the autoconnect priority.

When doing that, rename nm_settings_connection_cmp_default()
to nm_settings_connection_cmp_autoconnect_priority().
2017-02-10 14:43:24 +01:00
Thomas Haller
a822132399 core: make nm_utils_cmp_connection_by_autoconnect_priority() more robust
Check for NULL and unexpected missing NMSettingConnection.
Be more forgiving and accept whatever is there when comparing
@a with @b.
2017-02-10 14:43:24 +01:00
Thomas Haller
eb5ceedbba core: add nm_utils_cmp_connection_by_autoconnect_priority_p_with_data() function
Have a proper cmp() function and a wrapper *_p_with_data() that can be
used for g_qsort_with_data().

Thus, establish a naming scheme (*_p_with_data()) for these compare
wrappers that we need all over the place. Note, we also have
nm_strcmp_p_with_data() for the same reason and later more such
functions will follow.
2017-02-10 14:43:24 +01:00
Beniamino Galvani
0a0893280b core: fix nm_utils_file_set_contents() handling of -1 length
nm_utils_file_set_contents() must compute the content size when
@length is -1.

Fixes: 21358edc54
2017-02-08 12:40:47 +01:00
Lubomir Rintel
6404c79e4d core: kill nm_spawn_process()
It's not used anymore. Which is a good thing, because if it was used
we'd have to get rid of the uses.

It did accept a whitespace separated string for an argument, which is
never useful for us; it indicated error either on g_spawn_sync()
failure or an error status code of the program spawned, but only set the
error in the former case which had let to errors.

The would would be a bit nicer place without it.
(But not much)
2017-02-03 18:53:40 +01:00
Thomas Haller
63d4764acf core: refactor parsing in match_device_s390_subchannels_parse()
Changes:

  - match_device_s390_subchannels_parse() should accept un-initialized
    arguments a,b,c, as they are striclty output arguments (without
    transfering ownership).
  - the output arguments should be set if (and only if) the function
    succeeds. That is, move assigning the output arguments to the end.
  - increase the BUFSIZE. It's unclear why choosing 10. Probably that
    was already sufficient as a subchannel looks like
    "0.0.f5f0,0.0.f5f1,0.0.f5f2". Still, increase it to be ample.
    If we want to restrict the parsing based on the lenght of the input,
    that should be done explicitly (but that seems not desirable).
  - use _nm_utils_ascii_str_to_int64() which checks that the range
    of the values fits in guint32.

It seems wrong that match_device_s390_subchannels_eval() only compares
the first of up to three subchannels. But leave it as is for now.

(cherry picked from commit 419151a19e)
2017-01-25 17:15:37 +01:00
Lubomir Rintel
aa9e908c6a core: add missing initializers to match_data_s390_subchannels_eval()
match_device_s390_subchannels_parse() asserts that arguments point to
zeroes.

  1299     static gboolean
  1300     match_data_s390_subchannels_eval (const char *spec_str,
  1301                                       MatchDeviceData *match_data)
  1302     {
  >>>     CID 160923:  Uninitialized variables  (UNINIT)
  >>>     Declaring variable "c" without initializer.
  1303            guint32 a, b, c;

Fixes: b0aaff86b6
(cherry picked from commit 20328eaddf)
2017-01-25 17:15:36 +01:00
Thomas Haller
383d81e813 core: drop duplicate code searching for match in nm_match_spec_device()
When searching for "*", we still need to check for higher priority
"except:" matches. But don't duplicate the search loop and just
proceed with the regular searched.

It already has the "if (!except && match == NM_MATCH_SPEC_MATCH)" which
short-cuts the search.

(cherry picked from commit 9fff9f501a)
2017-01-23 09:44:14 +01:00
Thomas Haller
05d35647ec core: refactor nm_match_spec_device() to use match-data structure
Instead of passing on invdividual arguments for the match, create
a MatchDeviceData structure and pass it on.

This reduces the number of arguments and extending it later should
be easier. Also, lazily parse the hardware address as needed.

(cherry picked from commit b0aaff86b6)
2017-01-23 09:44:14 +01:00
Thomas Haller
ba1cc6a288 core: refactor evaluation of device's match-spec
Previously, we would have different functions like
  - nm_match_spec_device_type()
  - nm_match_spec_hwaddr()
  - nm_match_spec_s390_subchannels()
  - nm_match_spec_interface_name()
which all would handle one type of match-spec.

So, to get the overall result whether the arguments
match or not, nm_device_spec_match_list() had to stich
them together and iterate the list multiple times.

Refactor the code to have one nm_match_spec_device()
function that gets all relevant paramters.

The upside is:

  - the logic how to evaluate the match-spec is all at one place
    (match_device_eval()) instead of spread over multiple
    functions.

  - It requires iterating the list at most twice. Twice, because
    we do a fast pre-search for "*".

One downside could be, that we have to pass all 4 arguments
for the evaluation, even if the might no be needed. That is,
because "nm-core-utils.c" shall be independend from NMDevice, it
cannot receive a device instance to get the parameters as needed.
As we would add new match-types, the argument list would grow.
However, all arguments are cached and fetching them from the
device's private data is very cheap.

(cherry picked from commit b957403efd)
2017-01-20 21:18:30 +01:00
Thomas Haller
f0d40525df device: support dynamic "connection.stable-id" in form of text-substitution
Usecase: when connecting to a public Wi-Fi with MAC address randomization
("wifi.cloned-mac-address=random") you get on every re-connect a new
IP address due to the changing MAC address.
"wifi.cloned-mac-address=stable" is the solution for that. But that
means, every time when reconnecting to this network, the same ID will
be reused. We want an ID that is stable for a while, but at a later
point a new ID should e generated when revisiting the Wi-Fi network.

Extend the stable-id to become dynamic and support templates/substitutions.
Currently supported is "${CONNECTION}", "${BOOT}" and "${RANDOM}".
Any unrecognized pattern is treated verbaim/untranslated.

"$$" is treated special to allow escaping the '$' character. This allows
the user to still embed verbatim '$' characters with the guarantee that
future versions of NetworkManager will still generate the same ID.
Of course, a user could just avoid '$' in the stable-id unless using
it for dynamic substitutions.

Later we might want to add more recognized substitutions. For example, it
could be useful to generate new IDs based on the current time. The ${} syntax
is extendable to support arguments like "${PERIODIC:weekly}".

Also allow "connection.stable-id" to be set as global default value.
Previously that made no sense because the stable-id was static
and is anyway strongly tied to the identity of the connection profile.
Now, with dynamic stable-ids it gets much more useful to specify
a global default.

Note that pre-existing stable-ids don't change and still generate
the same addresses -- unless they contain one of the new ${} patterns.
2017-01-09 14:50:33 +01:00
Thomas Haller
21ae09c1cc core: add assertions for network_id/stable_type
We require a network-id. Assert that it is set.

Also, we encode the stable-id as uint8. Thus, add
an assertion that we don't use more then 254 IDs.

If we ever make use of stable-type 255, we must extend
the encoding to allow for more values. The assertion
is there to catch that.
2017-01-09 14:50:33 +01:00
Thomas Haller
dea3c49ce2 utils: add nm_utils_get_boot_id() util to read "/proc/sys/kernel/random/boot_id" 2017-01-09 14:50:33 +01:00
Lubomir Rintel
260563a7d9 all: use nm_utils_is_valid_iface_name() 2017-01-06 15:11:56 +01:00
Thomas Haller
f779c51f87 shared: move nm_utils_strbuf_*() helper to shared/nm-utils 2017-01-05 11:13:17 +01:00
Thomas Haller
4bdee37771 all: use O_CLOEXEC for file descriptors 2016-12-13 11:26:59 +01:00
Thomas Haller
1d9bdad1df core: add nm_utils_file_get_contents() and nm_utils_fd_get_contents()
A reimplementation of g_file_get_contents() to overcome two limitations:

 - nm_utils_file_get_contents() accepts a @dirfd argument to open the
   file relative using openat().
 - nm_utils_fd_get_contents() allows to read the content from a file
   filedescriptor.
 - both support a max_length argument, to fail gracefully if we
   get tricked into loading a huge file.
2016-12-13 11:26:58 +01:00
Beniamino Galvani
025235e00f ppp: split ppp manager into a plugin
Moving the PPP manager to a separate plugin that is loaded when needed
has the advantage of slightly reducing memory footprint and makes it
possible to install the PPP support only where needed.

https://bugzilla.gnome.org/show_bug.cgi?id=773482
2016-12-06 11:08:11 +01:00
Beniamino Galvani
2e21d78494 core: factor out plugin validation
The new function will be used to validate other plugins we load.
2016-12-06 11:08:11 +01:00
Beniamino Galvani
7ae6e980e4 core: move plugin path reading function to nm-core-utils.c
Even if it's called only from one place, it can be considered an
utility function.
2016-12-06 11:08:11 +01:00
Lubomir Rintel
2dd384c817 utils: allow valid_lft=0 addresses
We use the lifetime of 0 to indicate permanent addresses while
DHCP uses that lifetime to indicate the addresses should be removed.
Use the presence of a timestamp to differentiate the two.

  dhclient[10867]: XMT: Rebind on wls1, interval 1030ms.
  dhclient[10867]: RCV: Reply message on wls1 from fe80::21e:8cff:feec:3ca2.
  NetworkManager[10481]: <info>  [1478020967.7634] dhcp6 (wls1):   valid_lft 0
  NetworkManager[10481]: <info>  [1478020967.7634] dhcp6 (wls1):   preferred_lft 0
  NetworkManager[10481]: <info>  [1478020967.7636] dhcp6 (wls1):   address fd25:d463:2f14::927
  NetworkManager[10481]: <info>  [1478020967.7636] dhcp6 (wls1):   nameserver 'fe80::21e:8cff:feec:3ca2'
  NetworkManager[10481]: <info>  [1478020967.7637] dhcp (wls1):   domain search 'venom.'
  NetworkManager[10481]: <info>  [1478020967.7637] dhcp6 (wls1): state changed unknown -> bound, event ID="fa💿2c:86|1478020967"
  NetworkManager[10481]: ((src/nm-core-utils.c:3521)): assertion '<dropped>' failed
2016-11-09 15:06:20 +01:00
Thomas Haller
e5fe5a4c03 libnm-core/utils: update hwaddr utilities
_nm_utils_hwaddr_length() did a validation of the string
and returned the length of the address. In all cases where
we were interested in that, we also either want to validate
the address, get the address in binary form, or canonicalize
the address.

We can avoid these duplicate checks, by using _nm_utils_hwaddr_aton()
which both does the parsing and returning the length.
2016-10-28 16:28:29 +02:00
Thomas Haller
7bf4a15504 build: merge "src/tests/Makefile.am" into toplevel Makefile 2016-10-21 17:04:06 +02:00
Beniamino Galvani
21358edc54 core: introduce and use nm_utils_file_set_contents()
In some places we use g_file_set_contents() after a umask() to limit
the permissions of the created file. Unfortunately if the containing
directory has a default ACL the umask will be ignored and the new file
will have a mode equal to the default ACL (since g_file_set_contents()
opens the file with mode 0666).

Calling a chmod() after the file gets created is insecure (see commit
60b7ed3bdc) and so the only solution seems to be to reimplement
g_file_set_contents() and accept a mode as parameter.

We already had similar functions in the tree, consolidate them into a
new generic utility function.

https://bugzilla.gnome.org/show_bug.cgi?id=769702
2016-10-12 14:54:57 +02:00
Thomas Haller
a83eb773ce all: modify line separator comments to be 80 chars wide
sed 's#^/\*\{5\}\*\+/$#/*****************************************************************************/#' $(git grep -l '\*\{5\}' | grep '\.[hc]$') -i
2016-10-03 12:01:15 +02:00
Aidan Delaney
05e66d5202 core: search in /usr/local before system paths for the dnsmasq binary
When looking for the dnsmasq (or any) binary, NetworkManager should
check /usr/local before it checks any system installed version. This
allows the user to replace the binary with a newer version should they
desire and is more consistent with the search behaviour commonly found
in $PATH.

https://github.com/NetworkManager/NetworkManager/pull/10
2016-10-01 13:52:04 +02:00
Thomas Haller
96cabbcbb8 all: make MAC address randomization algorithm configurable
For the per-connection settings "ethernet.cloned-mac-address"
and "wifi.cloned-mac-address", and for the per-device setting
"wifi.scan-rand-mac-address", we may generate MAC addresses using
either the "random" or "stable" algorithm.

Add new properties "generate-mac-address-mask" that allow to configure
which bits of the MAC address will be scrambled.

By default, the "random" and "stable" algorithms scamble all bits
of the MAC address, including the OUI part and generate a locally-
administered, unicast address.

By specifying a MAC address mask, we can now configure to perserve
parts of the current MAC address of the device. For example, setting
"FF:FF:FF:00:00:00" will preserve the first 3 octects of the current
MAC address.

One can also explicitly specify a MAC address to use instead of the
current MAC address. For example, "FF:FF:FF:00:00:00 68:F7:28:00:00:00"
sets the OUI part of the MAC address to "68:F7:28" while scrambling
the last 3 octects.
Similarly, "02:00:00:00:00:00 00:00:00:00:00:00" will scamble
all bits of the MAC address, except clearing the second-least
significant bit. Thus, creating a burned-in address, globally
administered.

One can also supply a list of MAC addresses like
"FF:FF:FF:00:00:00 68:F7:28:00:00:00 00:0C:29:00:00:00 ..." in which
case a MAC address is choosen randomly.

To fully scamble the MAC address one can configure
"02:00:00:00:00:00 00:00:00:00:00:00 02:00:00:00:00:00".
which also randomly creates either a locally or globally administered
address.

With this, the following macchanger options can be implemented:

  `macchanger --random`
   This is the default if no mask is configured.
   -> ""
   while is the same as:
   -> "00:00:00:00:00:00"
   -> "02:00:00:00:00:00 02:00:00:00:00:00"

  `macchanger --random --bia`
   -> "02:00:00:00:00:00 00:00:00:00:00:00"

  `macchanger --ending`
   This option cannot be fully implemented, because macchanger
   uses the current MAC address but also implies --bia.
   -> "FF:FF:FF:00:00:00"
      This would yields the same result only if the current MAC address
      is already a burned-in address too. Otherwise, it has not the same
      effect as --ending.
   -> "FF:FF:FF:00:00:00 <MAC_ADDR>"
      Alternatively, instead of using the current MAC address,
      spell the OUI part out. But again, that is not really the
      same as macchanger does because you explictly have to name
      the OUI part to use.

  `machanger --another`
  `machanger --another_any`
  -> "FF:FF:FF:00:00:00 <MAC_ADDR> <MAC_ADDR> ..."
     "$(printf "FF:FF:FF:00:00:00 %s\n" "$(sed -n 's/^\([0-9a-fA-F][0-9a-fA-F]\) \([0-9a-fA-F][0-9a-fA-F]\) \([0-9a-fA-F][0-9a-fA-F]\) .*/\1:\2:\3:00:00:00/p' /usr/share/macchanger/wireless.list | xargs)")"
2016-06-30 08:32:50 +02:00
Thomas Haller
8eed67122c device: extend MAC address handling including randomization for ethernet and wifi
Extend the "ethernet.cloned-mac-address" and "wifi.cloned-mac-address"
settings. Instead of specifying an explicit MAC address, the additional
special values "permanent", "preserve", "random", "random-bia", "stable" and
"stable-bia" are supported.

"permanent" means to use the permanent hardware address. Previously that
was the default if no explict cloned-mac-address was set. The default is
thus still "permanent", but it can be overwritten by global
configuration.

"preserve" means not to configure the MAC address when activating the
device. That was actually the default behavior before introducing MAC
address handling with commit 1b49f941a6.

"random" and "random-bia" use a randomized MAC address for each
connection. "stable" and "stable-bia" use a generated, stable
address based on some token. The "bia" suffix says to generate a
burned-in address. The stable method by default uses as token the
connection UUID, but the token can be explicitly choosen via
"stable:<TOKEN>" and "stable-bia:<TOKEN>".

On a D-Bus level, the "cloned-mac-address" is a bytestring and thus
cannot express the new forms. It is replaced by the new
"assigned-mac-address" field. For the GObject property, libnm's API,
nmcli, keyfile, etc. the old name "cloned-mac-address" is still used.
Deprecating the old field seems more complicated then just extending
the use of the existing "cloned-mac-address" field, although the name
doesn't match well with the extended meaning.

There is some overlap with the "wifi.mac-address-randomization" setting.

https://bugzilla.gnome.org/show_bug.cgi?id=705545
https://bugzilla.gnome.org/show_bug.cgi?id=708820
https://bugzilla.gnome.org/show_bug.cgi?id=758301
2016-06-30 08:29:56 +02:00
Thomas Haller
83d231776b core: use nm_utils_read_urandom() in nm_utils_secret_key_read()
nm_utils_read_urandom() repeats on EINTR and repeats for partial reads.
2016-06-30 08:29:54 +02:00
Thomas Haller
dcc8de16b2 core: add utils for file handling
Copied and adjusted from systemd code.
2016-06-30 08:29:54 +02:00
Thomas Haller
c7b7305b59 core/utils: convert MAC address in nm_match_spec_hwaddr() from string once 2016-06-30 08:29:54 +02:00
Thomas Haller
05ecb28436 core: make global variable _nm_utils_testing static 2016-06-30 08:29:54 +02:00
Thomas Haller
0a5af391e0 core: prefer connection.stable-id to generate IPv6 stable privacy addresses
The Network_ID for generating RFC 7217 stable privacy IPv6 addresses
is by default the UUID of the connection.

Alternatively, prefer "connection.stable-id" as Network_ID to generate
the stable addresses. This allows to configure a set of connections that
all use the same Network_ID for generating stable addresses.

Note that the stable-id and the UUID do no overlap, that is two
connections
    [connection]
    uuid=uuid1
    stable-id=
and
    [connection]
    uuid=uuid2
    stable-id=uuid1
generate distinct addresses.
2016-06-30 08:29:54 +02:00
Thomas Haller
0df5e9b736 rdisc/trivial: rename @uuid field to @network_id
Next we will optionally use a stable-id instead of the UUID. Rename it.
Also, RFC 7217 calls this argument Network_ID.
2016-06-30 08:29:54 +02:00
Beniamino Galvani
ae763642d8 dns: add reverse DNS IPv6 entries to dnsmasq
https://bugzilla.gnome.org/show_bug.cgi?id=767174
2016-06-07 11:20:09 +02:00
Beniamino Galvani
4d1e7dc23c dns: add CIDR reverse DNS IPv4 entries to dnsmasq
When a reverse DNS entry must be added to dnsmasq, instead of
considering IP addresses as classful use the prefix to compute one or
more "in-addr.arpa" according to CIDR rules.

https://bugzilla.gnome.org/show_bug.cgi?id=767174
2016-06-07 10:52:50 +02:00
Thomas Haller
d66b5e79c7 core: reuse common static string buffer in nm_utils_dnsmasq_status_to_string()
Instead of declaring our local string buffer @buffer,
reuse nm_utils_to_string_buffer_init().

https://github.com/NetworkManager/NetworkManager/pull/8
2016-06-03 09:24:45 +02:00
Lubomir Rintel
0c40bce314 core-utils: add conversions of ipv6 tokens
From/to strings and interface identifiers.
2016-05-30 16:19:20 +02:00
Lubomir Rintel
02f935c4cc all: fix the identfier typo 2016-05-30 16:14:04 +02:00
Thomas Haller
d3be90e3e7 utils: fix memleak in nm_utils_read_link_absolute()
Fixes: c4b88bf23f
2016-05-12 15:24:11 +02:00
Thomas Haller
ba90c9601c all: replace nm_unauto() by g_steal_pointer()
They do essentially the same.
2016-05-12 14:28:44 +02:00