Commit Graph

419 Commits

Author SHA1 Message Date
Thomas Haller
45a58c3811 trivial: fix whitespace 2017-07-17 12:49:58 +02:00
Thomas Haller
3f76b5b7eb core: use NM_HASH_COMBINE() function 2017-07-05 14:22:10 +02:00
Thomas Haller
a973eacb3b libnm: add enum for setting priorities
Using plain numbers make it cumbersome to grep for
setting types by priority.

The only downside is, that with the enum values it
is no longer obvious which value has higher or lower
priority.

Also, introduce NM_SETTING_PRIORITY_INVALID. This is what
_nm_setting_type_get_base_type_priority() returns. For the moment
it still has the same numerical value 0 as before. Later, that
shall be distinct from NM_SETTING_PRIORITY_CONNECTION.
2017-06-07 09:07:17 +02:00
Lubomir Rintel
c0419257e7 all: reject duplicate keys in JSON
Teamd is not happy about them and would fail anyway. Worse even, if we
json_loads() such a JSON, which is precisely what happens when we inject the
"hwaddr" key, we turn bad JSON into a good one in a lossy matter. Not good.

https://bugzilla.redhat.com/show_bug.cgi?id=1455130
2017-06-01 13:30:21 +02:00
Lubomir Rintel
2c1a178f5b core: negotiate the best base setting
When the two base settings are present, use one of higher priority.

This will pick the "bridge" setting when both "bridge" and "bluetooth" are
present for a Bluetooth NAP connection.
2017-05-31 20:15:24 +02:00
Thomas Haller
9800b1dccc libnm: move public nm_utils_enum_*() functions back to libnm-core
Commit a8730c51c8 moved the enum
utils from libnm-core to shared/nm-utils.

However, three of those functions are part of public API in libnm.
So, when statically linking against "shared/nm-utils/nm-enum-utils.c"
and dynamically linking against libnm.so, those symbols are present
twice and cause a linker failure.

Fix that by moving the public API back to libnm-core.

Fixes: a8730c51c8
2017-04-05 18:38:31 +02:00
Thomas Haller
d1c6d64e6a shared: move _nm_utils_strv_cleanup() to shared utils 2017-04-05 16:53:06 +02:00
Thomas Haller
a8730c51c8 libnm: move enum utils to new shared file shared/nm-utils/nm-enum-utils.h
libnm contains the public function nm_utils_enum_from_str() et al.
The function is not flexible enough for nmcli's usecase. So, I would
need another public function like nm_utils_enum_from_str_full() that
has an extended API.

That was already required previously for ifcfg-rh writer, but in that
case I could just add it as internal API as libnm-core is linked statically
with NetworkManager.

I don't want to commit to a public API for an utility function. So move
the code instead to the shared directory, so that nmcli may link
statically against it and use the internal API.
2017-03-30 13:09:58 +02:00
Thomas Haller
e26a81cf35 libnm: handler numeric values more gracefully in nm_utils_enum_from_str()
nm_utils_enum_to_str() may also output numeric values if there is no
corresponding "nick" for the enum/flag value.

For enums the value is in decimal and for flags the value is hexadecimal
(with a "0x" prefix).

The same was already supported by nm_utils_enum_from_str() when reading
the value. However, previously, reading a flag would only support hex
numbers and reading a enum would only support decimal numbers.

Extend that, to allow passing numbers in any base. For nm_utils_enum_to_str()
also make sure to never output nicks that may be misinterpreted as
numbers.
2017-03-30 13:09:55 +02:00
Thomas Haller
112f09cf4b libnm: return zero flags value from nm_utils_enum_to_str()
It is not uncommon that a flags type has also the value 0 mapped,
for example to "unknown" or "none".

In that case, we should not return an empty string, but instead
that zero value.

Also, flags actually have an unsigned type. That isn't a real
problem to cast it to a signed int. But be more careful about
it and use unsigned while handling unsigned values and only
cast to int once.
2017-03-30 13:09:54 +02:00
Thomas Haller
163e193286 libnm: fix type annotation for nm_utils_enum_from_str() 2017-03-25 17:12:27 +01:00
Beniamino Galvani
93b3a478bb libnm-core: add attribute parsing/format helpers
Various libnm objects (addresses, routes) carry an hash table of
attributes represented as GVariants indexed by name. Add common
routines to convert to and from a string representation.

To parse a string, a knowledge of the supported attributes (and their
types) is needed: we represent it as an opaque type
NMVariantAttributeSpec that callers must query to the library for the
specific object type and pass to the parse function.
2017-03-06 15:20:25 +01:00
Lubomir Rintel
61722fdeb1 libnm-core/trivial: fix a typo in doc comment 2017-02-25 22:17:46 +01:00
Thomas Haller
1525b44714 utils: support unknown numeric values in nm_utils_enum_to_str() and nm_utils_enum_from_str()
- for nm_utils_enum_to_str(), whenever encounter a numeric value
  that has no expression as enum/flag, encode the value numerically.
  For enums, encode it as decimal. For flags, encode it as hexadecimal
  (with 0x prefix).
  Also check that an existing value_nick cannot be wrongly interpreted
  as a integer, and if they would, encode them instead as integers only.

- Likewise, in nm_utils_enum_from_str() accept numerical values
  and for nm_utils_enum_get_values() return enum nicks that look
  like numeric values in their numeric form only.

- In nm_utils_enum_from_str(), don't use g_strsplit(), but clone the
  string only once and manipulate it inplace.

- Accept '\n' and '\r' as additional delimiters for flags.

- For consistency, also return an err_token for enum types. If the caller
  doesn't care about that, he should simply not pass the out-argument.
2017-02-20 13:45:32 +01:00
Beniamino Galvani
8950d18362 libnm-core: add _nm_utils_enum_to_str_full()
Unfortunately nm_utils_enum_to_str() doesn't allow to specify the
separator between enum values. Since the function is public API and
can't be modified now, add a new internal function which accepts the
separator as argument.
2017-02-20 13:45:32 +01:00
Beniamino Galvani
4d3ec516bd libnm-core: add missing transfer annotation for nm_utils_enum_to_str() 2017-02-20 13:44:08 +01:00
Thomas Haller
7c6c8f0d8b all: cleanup switch fall-through comments for -Wimplicit-fallthrough warning
The -Wimplicit-fallthrough=3 warning is quite flexible of accepting
a fall-through warning.

Some comments were missing or not detected correctly.

Thereby, also change all other comments to follow the exact
same pattern.
2017-02-06 16:45:20 +01:00
Thomas Haller
11bc3f191e all: use nm_utils_strv_find_first() from shared/nm-utils 2017-02-04 17:55:30 +01:00
Beniamino Galvani
660bb1a48f libnm-core: return NULL from _nm_utils_slist_to_strv for empty lists
The function is used, among others, in the get_property() of many
objects to return a boxed strv from a list. The default value for a
boxed strv property is NULL, but _nm_utils_slist_to_strv() returns a
pointer to an array with zero elements when the list is empty.

Change the function to return NULL if the input list is empty.
2017-02-02 13:45:27 +01:00
Lubomir Rintel
f9be6540b0 libnm-core: deprecate nm_utils_iface_valid_name()
We don't use it internally and a better version exists. Not like we
could drop it or anything.
2017-01-06 15:11:56 +01:00
Lubomir Rintel
ac0563d784 libnm-core: add nm_utils_is_valid_iface_name()
Version of nm_utils_iface_valid_name() with error reporting.
2017-01-06 15:11:56 +01:00
Thomas Haller
16aeac5c3e libnm: add nm_utils_version() function to query libnm version at runtime
if (nm_utils_version () < NM_ENCODE_VERSION (1, 5, 0))
        g_error ("Requires at least version 1.5.0");
2016-12-20 16:51:58 +01:00
Jiří Klimeš
3803739808 libnm-core: (trivial) fix a typo 2016-12-15 15:31:56 +01:00
Thomas Haller
eb77d4ed28 libnm-core/utils: remove unused code from nm_utils_hwaddr_matches()
Makes coverity complain.
2016-12-13 18:49:34 +01:00
Thomas Haller
55fb25f59d libnm: force unsigned integer comparisong in nm_utils_bin2hexstr()
Not really a big issue, without the case the intergers
would be compared as ssize_t, which is large enough for
final_len, but possibly not for buflen.
2016-11-28 10:21:24 +01:00
Thomas Haller
3aa41c6e17 libnm: merge hwaddr_aton() and nm_utils_hexstr2bin()
Have nm_utils_hexstr2bin() take over the allocated buffer
via g_bytes_new_take().
2016-11-28 10:06:27 +01:00
Thomas Haller
d7e535aee5 libnm/trivial: rename internal function _bin2str() 2016-11-28 08:50:35 +01:00
Thomas Haller
4cc2f8906c libnm: drop _bin2str() util
It was only used by two places, inline it.
2016-11-27 13:57:13 +01:00
Thomas Haller
542d1c257d libnm: implement nm_utils_bin2hexstr() via _bin2str_buf() 2016-11-27 13:54:23 +01:00
Thomas Haller
5ea2fc776c libnm/trivial: move code 2016-11-27 13:54:23 +01:00
Thomas Haller
34eb8d513f libnm/trivial: move hexstr<>bin conversion functions
nm_utils_hexstr2bin() is quite similar to hwaddr_aton(), and
nm_utils_bin2hexstr() is similar to _bin2str_buf().

Move them close to each other. Maybe one day they should be
consolidated. But their API is slightly different, so the
consolidation would require some effort.

For now, just move them close to each other, so that it's
clearer that two similar (but distinct) functions exists.
2016-11-13 16:01:18 +01:00
Thomas Haller
e36f6a7e52 libnm: add nm_utils_uuid_generate_buf() util 2016-11-09 12:07:33 +01:00
Thomas Haller
37bf643213 libnm: don't initialize buffer for uuid_unparse_lower() with zeros 2016-11-09 12:07:33 +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
0e47b327dc libnm: move backported symbols from libnm-core to libnm
Backported symbols only make sense for libnm itself, not for
libnm-core which is statically linked with NetworkManager and
nm-ifcace-helper. Declaring the symbols in libnm-core, means
that NetworkManager binary also contains them, although there
are not used.

Move them to libnm.
2016-10-13 21:33:33 +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
Pellaeon Lin
4d7cf9a16d libnm: fix typo g_size to gsize
Fixes: 32f78ae6c3

https://bugzilla.gnome.org/show_bug.cgi?id=772062
2016-09-27 18:08:13 +02:00
Thomas Haller
32f78ae6c3 libnm: expose nm_utils_is_json_object() utility function
Since we possibly already link against libjansson, we can also expose some
helper utils which allows nmcli to do basic validation of JSON without
requiring to duplicate the effort of using libjansson.

Also, tighten up the cecks to ensure that we have a JSON object at hand.
We are really interested in that and not of arrays or literals.
2016-09-27 10:56:42 +02:00
Thomas Haller
c3ecca225c core: add _nm_utils_array_find_binary_search()
Also add nm_cmp_uint32_p_with_data(). Will be used later.
2016-09-23 15:49:29 +02:00
Thomas Haller
08f5681b0e core: const arguments for _nm_utils_ptrarray_find_*() functions 2016-09-23 15:34:17 +02:00
Thomas Haller
814784aa46 libnm: fix regression serializing empty "cloned-mac-address"
For "cloned-mac-address", the empty string "" is an invalid
value that is rejected by verify().

Commit 8eed671 changed how the property is serialized to D-Bus.
Before, it was serialized using _nm_utils_hwaddr_to_dbus().
For invalid or empty addresses, this would not serialize the
value on D-Bus (or before commit 76aa6f8e0, it would create
a bogus value with no array elements).

With commit 8eed671, the cloned-mac-address gets also serialized
as "assigned-mac-address" via _nm_utils_hwaddr_cloned_data_synth(),
which would pass on invalid strings that the server would then reject.

That breaks for example nmtui. Try editing a connection with
"cloned-mac-address" set to NULL. Note, as long as you don't edit
the cloned MAC address in nmtui, you can save the modification.
Once you start modifying the entry, you can no longer set an empty
MAC address as the server now receives the invalid empty string.
Thus, the "OK" button fails with
   Unable to save connection:
   802-3-ethernet.cloned-mac-address:
   is not a valid MAC address
It also means, nmtui cannot modify the "cloned-mac-address" field to
become empty.

Fix that problem at various places by coercing "" to NULL.

Fixes: 8eed67122c

https://bugzilla.redhat.com/show_bug.cgi?id=1372799
2016-09-06 16:11:23 +02:00
Thomas Haller
d060958a6d libnm: minor cleanup of _bin2str() 2016-07-10 13:51:57 +02:00
Thomas Haller
36856ba610 all: reuse _nm_utils_hwaddr_ntoa() for converting binary to string 2016-07-10 13:44:58 +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
76aa6f8e0d libnm: don't serialize empty hardware address on D-Bus
_nm_utils_hwaddr_to_dbus() would serialize invalid hardware addresses
as "'cloned-mac-address': <@ay []>".

An empty array is treated the same as no hardware address set,
so we should not serialize it in the first place.

This is a change in behavior on how the connection is exported
on D-Bus, but it should not have any bad consequences.

We need this as we later want to deprecate the 'cloned-mac-address'
D-Bus field and overwrite it via a 'assigned-mac-address' field.
In this case, the "<@ay []>" is interfering. While it could be worked
around by treating an empty MAC address as "unset", fix it instead
and just not serialize it.
2016-06-30 08:29:54 +02:00
Thomas Haller
fc527a237c libnm: add internal util function _nm_utils_hwaddr_length() 2016-06-30 08:29:54 +02:00
Thomas Haller
bc1014a93d all: replace _nm_utils_string_in_list() with g_strv_contains() 2016-06-17 12:25:33 +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
Thomas Haller
81499355b0 libnm: don't require initialized @out_encrypted argument in nm_utils_file_is_private_key()
No need to require the caller to initialize the optional out-argument.
Otherwise we get:

  (nm-connection-editor:2471): libnm-CRITICAL **: nm_utils_file_is_private_key: assertion 'out_encrypted == NULL || *out_encrypted == FALSE' failed

https://bugzilla.gnome.org/show_bug.cgi?id=763578

Fixes: 1c4f41c610
2016-05-13 14:26:32 +02:00