Commit Graph

342 Commits

Author SHA1 Message Date
Thomas Haller
0cff18e055 libnm/team: indicate whether there were changes in _nm_utils_team_config_set() 2019-03-24 09:21:13 +01:00
Thomas Haller
f84323bf47 libnm/team: return early from _nm_utils_team_config_set() 2019-03-24 09:21:13 +01:00
Thomas Haller
1283d9d321 libnm/team: minor coding style cleanup 2019-03-24 09:21:13 +01:00
Thomas Haller
acf1cf61cf shared: add _nm_utils_strv_cmp_n() and _nm_utils_strv_equal() 2019-03-24 09:21:13 +01:00
Thomas Haller
0d178a968d libnm: rename and expose nm_utils_base64secret_decode() in libnm
A NetworkManager client requires an API to validate and decode
a base64 secret -- like it is used by WireGuard. If we don't have
this as part of the API, it's inconvenient. Expose it.

Rename it from _nm_utils_wireguard_decode_key(), to give it a more
general name.

Also, rename _nm_utils_wireguard_normalize_key() to
nm_utils_base64secret_normalize(). But this one we keep as internal
API. The user will care more about validating and decoding the base64
key. To convert the key back to base64, we don't need a public API in
libnm.

This is another ABI change since 1.16-rc1.

(cherry picked from commit e46ba01867)
2019-03-07 22:22:39 +01:00
Thomas Haller
b680d64b47 wireguard: accept all-zero private-key, public-key and preshared-key
- For PSK, an all-zero PSK means to don't do symmetric encryption. As such,
  at first it seems a bit odd when the user sets

      - preshared-key-flags != "4 (not-required)"

      - preshared-key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

  Here the user indicates that a PSK is required, but then provides an
  all-zero PSK that effectively disables it. Still, we should not reject
  such a configuration. This has the benefit that it allos the user for
  being prompted for a PSK, only to disable it by entering the all-zero key.

- For the private-key (and consequently the public-key), "public-key-flags=4"
  is rejected by libnm. A private key is always required for NetworkManager to
  configure the link. However, let's not care for all-zero keys either. If the user
  configures that, we just set that key. It's a valid setting as far as WireGuard
  (the kernel module) is concerned, so we shouldn't reject it.

(cherry picked from commit 78dccb8bb9)
2019-03-07 22:22:39 +01:00
Marco Trevisan (Treviño)
b5bbf8edc2 nm: Fix syntax on introspection annotations
Various annotations were added using multiple colons, while only one has
to be added or g-ir-introspect will consider them part of the description

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/94
(cherry picked from commit 73005fcf5b)
2019-03-07 10:09:23 +01:00
Thomas Haller
53b747fff5 all: move nm_utils_hexstr2bin*() to shared
libnm exposes simplified variants of hexstr2bin in its public API. I
think that was a mistake, because libnm should provide NetworkManager
specific utils. It should not provide such string functions.

However, nmcli used to need this, so it was added to libnm.

The better approach is to add it to our internally shared static
library, so that all interested components can make use of it.
2019-02-22 14:04:13 +01:00
Thomas Haller
b521f426ab libnm,cli: add NMSettingWireGuard
For now only add the core settings, no peers' data.

To support peers and the allowed-ips of the peers is more complicated
and will be done later. It's more complicated because these are nested
lists (allowed-ips) inside a list (peers). That is quite unusual and to
conveniently support that in D-Bus API, in keyfile format, in libnm,
and nmcli, is a effort.
Also, it's further complicated by the fact that each peer has a secret (the
preshared-key). Thus we probably need secret flags for each peer, which
is a novelty as well (until now we require a fixed set of secrets per
profile that is well known).
2019-02-22 11:00:10 +01:00
Beniamino Galvani
b5efcf08f4 all: move nm_utils_bin2hexstr_full() to shared
reuse++
2019-02-21 09:36:17 +01:00
Thomas Haller
713e879d76 libnm: add NMSockAddrEndpoint API
NMSockAddrEndpoint is an immutable structure that contains the endpoint
string of a service. It also includes the (naive) parsing of the host and
port/service parts.

This will be used for the endpoint of WireGuard's peers. But since endpoints
are not something specific to WireGuard, give it a general name (and
purpose) independent from WireGuard.

Essentially, this structure takes a string in a manner that libnm
understands, and uses it for node and service arguments for
getaddrinfo().

NMSockAddrEndpoint allows to have endpoints that are not parsable into
a host and port part. That is useful because our settings need to be
able to hold invalid values. That is for forward compatibility (server
sends a new endpoint format) and for better error handling (have
invalid settings that can be constructed without loss, but fail later
during the NMSetting:verify() step).
2019-02-14 08:00:29 +01:00
Thomas Haller
d93845e2c2 libnm: add internal _nm_utils_secret_flags_validate() util 2019-02-14 08:00:29 +01:00
Thomas Haller
d216e2f305 libnm: fix usage of nm_streq() macro
Yes, C has a preprocessor and nm_streq() currently is a macro.

Still, macros should very much behave like regular functions.
For example, no unexpected side-effects aside what a regular function
would have, evaluating all arguments exactly once, or no side-effects
w.r.t. the order in which arguments are evaluated.

In some cases, we deviate from that for good reasons. For example
NM_IN_SET() may not evaluate all arguments. _LOGD() may not evaluate
any arguments, and NM_UTILS_LOOKUP_STR_DEFINE() is not a function-like
macro at all.

Still, that is not the case here. We avoid to misuse macros to write
code that does not look like C.
2019-02-13 16:03:23 +01:00
Thomas Haller
9beed4f661 all: replace strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
a3370af3a8 all: drop unnecessary includes of <errno.h> and <string.h>
"nm-macros-interal.h" already includes <errno.h> and <string.h>.
No need to include it everywhere else too.
2019-02-12 08:50:28 +01:00
Thomas Haller
814bcf5575 libnm: avoid "-Wmissing-braces" warning for intializing NMUuid 2019-02-08 20:14:50 +01:00
Thomas Haller
d25ed0820c all: don't use "static inline" in source files
For static functions inside a module, the compiler determines on its own
whether to inline the function.

Also, "inline" was used at some places that don't immediatly look like
candidates for inlining. It was most likely a copy&paste error.
2019-02-06 09:31:00 +01:00
Thomas Haller
efbcac25b8 libnm-core: fix and cleanup validation of WPS-method for Wi-Fi P2P setting
The check for Wi-Fi P2P's wps-method was not correct.

While at it, move the logic to validate WPS-method flags in an utility
function.
2019-02-01 17:02:57 +01:00
Thomas Haller
e8bf89a906 libnm: pass serialization flags to settings synth_func()
We will need access to the serialization flags from within the synth_func().

That will be for WireGuard's peers. Peers are a list of complex, structured
elements, and some fields (the peer's preshared-key) are secret and
others are not. So when serializing the peers, we need to know whether
to include secrets or not.

Instead of letting _nm_setting_to_dbus() check the flags, pass them
down.

While at it, don't pass the property_name argument. Instead, pass the
entire meta-data information we have. Most synth functions don't care
about the property or the name either way. But we should not pre-filter
information that we have at hand. Just pass it to the synth function.
If the synth function would be public API, that would be a reason to be
careful about what we pass. But it isn't and it only has one caller.
So passing it along is fine. Also, do it now when adding the flags
argument, as we touch all synth implementations anyway.
2019-01-07 10:09:10 +01:00
Thomas Haller
b9ebaf3a2b libnm: discourage static buffer in nm_utils_inet*_ntop() API
nm_utils_inet[46]_ntop() are public API of libnm, so we cannot change
it. However, discourage the use of the static buffer.
2018-12-19 09:23:08 +01:00
Thomas Haller
a51c09dc12 all: don't use static buffer for nm_utils_inet*_ntop()
While nm_utils_inet*_ntop() accepts a %NULL buffer to fallback
to a static buffer, don't do that.

I find the possibility of using a static buffer here error prone
and something that should be avoided. There is of course the downside,
that in some cases it requires an additional line of code to allocate
the buffer on the stack as auto-variable.
2018-12-19 09:23:08 +01:00
Thomas Haller
e442e3881e core: implement nm_utils_ip4_netmask_to_prefix() via __builtin_ctz()
Taken from systemd's in4_addr_netmask_to_prefixlen().

Yes, this adds the requirement that "int" is 32 bits. But systemd
already has the same requirement in u32ctz(), hence we anyway cannot
build on other architectures. If that is ever necessary, it's easy
to adjust.
2018-12-19 09:23:08 +01:00
Beniamino Galvani
769e0726a8 cli: strictly validate SR-IOV attributes
Report an error when the user tries to add an unknown attribute
instead of silently accepting (and ignoring) it.

Note that this commit also changes the behavior of public API
nm_utils_sriov_vf_from_str() to return an error when an unknown
attribute is found. I think the previous behavior was buggy as wrong
attributes were simply ignored without any way for the user to know.

Fixes: a9b4532fa7
2018-12-12 14:18:53 +01:00
Patrick Talbert
f44c793f6c team: add support for 'vlanid' link-watchers property
Add support for the teaming arp_ping link watcher 'vlanid' property.

Signed-off-by: Patrick Talbert <ptalbert@redhat.com>

[thaller@redhat.com: minor fixes to original patch]

https://bugzilla.redhat.com/show_bug.cgi?id=1652931
2018-12-12 14:07:30 +01:00
Thomas Haller
140a5e3316 all: make use of NM_MAKE_STRV() macro 2018-12-01 15:16:48 +01:00
Thomas Haller
eb9f950a33 all: cleanup GChecksum handling
- prefer nm_auto_free_checksum over explicit free.
- use nm_utils_checksum_get_digest*().
- prefer defines for digest length.
- assume g_checksum_new() cannot fail.
2018-11-13 18:30:03 +01:00
Thomas Haller
01239e99d7 libnm: add nm_utils_uuid_is_null() helper 2018-10-31 11:34:31 +01:00
Thomas Haller
4db431191c libnm: add nm_utils_uuid_generate_from_string_bin() function 2018-10-31 11:34:31 +01:00
Thomas Haller
070a4d9355 libnm: add support for SHA1 based version 5 UUIDs
The entire point of using version 3/5 UUIDs is to generate
stable UUIDs based on a string. It's usually important that
we don't change the UUID generation algorithm later on.

Since we didn't have a version 5 implementation, we would always
resort to the MD5 based version 3. Version 5 is recommended by RFC 4122:

   o  Choose either MD5 [4] or SHA-1 [8] as the hash algorithm; If
      backward compatibility is not an issue, SHA-1 is preferred.

Add a version 5 implementation so we can use it in the future.

All test values are generated with python's uuid module or OSSP uuid.
2018-10-31 11:34:31 +01:00
Thomas Haller
c150b0fa29 libnm/trivial: rename uuid type VARIANT3 to VERSION3
In RFC 4122, this is called "version 3", not "variant 3". While for
UUIDs there is also a concept of "variants", that is something else.

Fix naming.
2018-10-31 09:41:12 +01:00
Thomas Haller
88b081fce4 libnm: expose UUID utils as internal API
We link against libuuid.so, but it was entirely internal to
libnm-core. We only exposed UUIDs in string form.

Add API to also handle UUIDs in binary form.

Note that libuuid already defines a type "uuid_t". However,
don't use it and instead use our own typedef NMUuid.
Reasons:

  - uuid.h should be internal to libnm-core (nm-utils.c specifically),
    and not be used by or exposed it other parts of the code.

  - uuid_t is a typedef for a guchar[16] array. Typedefs
    for arrays are confusing, because depending on whether
    it's an automatic variable or a pointer in a function argument,
    they behave differently regarding whether to take their address
    or not and usage of "sizeof()".
2018-10-31 09:41:12 +01:00
Beniamino Galvani
9b04b871a0 libnm-core: fix coverity warning
3. NetworkManager-1.14.0/libnm-core/nm-utils.c:4944: var_compare_op: Comparing "str" to null implies that "str" might be null.
 4. NetworkManager-1.14.0/libnm-core/nm-utils.c:4958: var_deref_op: Dereferencing null pointer "str".
 #  4956|
 #  4957|   	/* do some very basic validation to see if this might be a JSON object. */
 #  4958|-> 	if (str[0] == '{') {
 #  4959|   		gsize l;
 #  4960|
2018-10-06 10:03:48 +02:00
Thomas Haller
be6c7fa5f6 libnm: cleanup _nm_utils_hexstr2bin*() helper
Add 3 variants of _nm_utils_hexstr2bin*():

  - _nm_utils_hexstr2bin_full(), which takes a preallocated
    buffer and fills it.
  - _nm_utils_hexstr2bin_alloc() which returns a malloc'ed
    buffer
  - _nm_utils_hexstr2bin_buf(), which fills a preallocated
    buffer of a specific size.
2018-09-30 16:13:42 +02:00
Thomas Haller
b537c0388a all: drop _nm_utils_bin2hexstr()
We already have nm_utils_bin2hexstr() and _nm_utils_bin2hexstr_full().
This is confusing.

  - nm_utils_bin2hexstr() is public API of libnm. Also, it has
    a last argument @final_len to truncate the string at that
    length.
    It uses no delimiter and lower-case characters.

  - _nm_utils_bin2hexstr_full() does not do any truncation, but
    it has options to specify a delimiter, the character case,
    and to update a given buffer in-place. Also, like
    nm_utils_bin2hexstr() and _nm_utils_bin2hexstr() it can
    allocate a new buffer on demand.

  - _nm_utils_bin2hexstr() would use ':' as delimiter and make
    the case configurable. Also, it would always allocate the returned
    buffer.

It's too much and confusing. Drop _nm_utils_bin2hexstr() which is internal
API and just a wrapper around _nm_utils_bin2hexstr_full().
2018-09-30 13:36:57 +02:00
Thomas Haller
21df8d38ef libnm: return output buffer from _nm_utils_bin2hexstr_full()
It's just more convenient, as it allows better chaining.

Also, allow passing %NULL as @out buffer. It's clear how
large the output buffer must be, so for convenience let the
function (optionally) allocate a new buffer.

This behavior of whether to
  - take @out, fill it, and return @out
  - take no @out, allocate new buffer, fill and and return it
is slightly error prone. But it was already error prone before, when
it would accept an input buffer without explicit buffer length. I think
this makes it more safe, because in the common case the caller can avoid
pre-allocating a buffer of the right size and the function gets it
right.
2018-09-30 13:36:57 +02:00
Thomas Haller
6714440669 all/trivial: rename hexstr<>bin conversion functions
"bin2str" and "str2bin" are not very clear. These strings are
hex-strings. Rename.
2018-09-30 13:33:46 +02:00
luz.paz
58510ed566 docs: misc. typos pt2
Remainder of typos found using `codespell -q 3 --skip="./shared,./src/systemd,*.po" -I ../NetworkManager-word-whitelist.txt` whereby whitelist consists of:
 ```
ans
busses
cace
cna
conexant
crasher
iff
liftime
creat
nd
sav
technik
uint
```

https://github.com/NetworkManager/NetworkManager/pull/205
2018-09-17 11:26:13 +02:00
Thomas Haller
ff8777cdde libnm/trivial: whitespace
(cherry picked from commit 3b5912f08d)
2018-09-14 17:29:05 +02:00
Thomas Haller
74a3d184b0 libnm: document nm_utils_parse_variant_attributes() returning floating references
See-also: https://bugzilla.redhat.com/show_bug.cgi?id=1594887
(cherry picked from commit e645aeb12c)
2018-09-14 17:29:05 +02:00
Thomas Haller
d01b37c46f libnm: fix memleak in _nm_utils_ssid_to_string_arr()
Fixes: 5cd4e6f3e6
(cherry picked from commit 54e1f73e0c)
2018-09-13 16:18:34 +02:00
Thomas Haller
986ca94a36 libnm: assert in nm_utils_is_uuid() for valid argument
nm_utils_is_uuid() is public API. Commonly we check input arguments for such
functions with g_return_*() assertions.
2018-09-06 07:41:22 +02:00
Thomas Haller
a9406ca4a7 libnm-core: expose _nm_utils_str2bin_full() as internal API
We only exposed wrappers around this function, but all of them have
different behavior, and none exposes all possible features. For example,
nm_utils_hexstr2bin() strips leading "0x", but it does not clear
the data on failure (nm_explicit_bzero()). Instead of adding more
wrappers, expose the underlying implementation, so that callers may
use the function the way they want it.
2018-09-04 07:38:30 +02:00
Thomas Haller
f961dcb806 libnm/crypto: move and mark nm_utils_rsa_key_encrypt() as test code
nm_utils_rsa_key_encrypt() is internal API which is only uesd for testing.
Move it to nm-crypto.h (where it fits better) and rename it to make the
testing-aspect obvious.
2018-09-04 07:38:30 +02:00
Thomas Haller
896a47da53 libnm/crypto: refactor nm_crypto_load_and_verify_certificate() and return GBytes
The GBytes has a suitable cleanup function, which zeros the certificate
from memory.

Also, all callers that require the certificate, actually later converted
it into a GBytes anyway. This way, they can re-used the same instance
(avoiding an additionaly copying of the data), and they will properly
clear the memory when freed.
2018-09-04 07:38:30 +02:00
Thomas Haller
c172675c13 libnm/crypto: rename libnm crypto API to have consistent NM prefix
Follow our convention, that items in headers are all named with
an "NM" prefix.

Also, "nm-crypto-impl.h" contains internal functions that are to be implemented
by the corresponding crypto backends. Distinguish their names as well.
2018-09-04 07:38:30 +02:00
Thomas Haller
4106f2968d libnm/crypto: rename libnm's crypto files
"crypto.h" did not follow our common NM style naming. Rename
the files.
2018-09-04 07:38:30 +02:00
Thomas Haller
9ca12145a3 libnm/crypto: adjust argument types for crypto_md5_hash()
There should be a clear distinction between whether an array
is a NUL terminated string or binary with a length.

crypto_md5_hash() is already complicated enough. Adjust it's
API to only support binary arguments, and thus have "guint8 *" type.
2018-09-04 07:38:30 +02:00
Thomas Haller
b8a57fb272 libnm-core: remove unused utlity functions for GSList
I think GSList is not a great data type. Most of the time when we used
it, we better had choosen another data type.

These utility functions were unused, and I think we should use GSList
less.

Drop them.
2018-08-30 11:17:09 +02:00
Thomas Haller
5cd4e6f3e6 wifi: don't use GBytesArray for NMWifiAP's ssid
GBytes makes more sense, because it's immutable.

Also, since at other places we use GBytes, having
different types is combersome and requires needless
conversions.

Also:

- avoid nm_utils_escape_ssid() instead of _nm_utils_ssid_to_string().
  We use nm_utils_escape_ssid() when we want to log the SSID. However, it
  does not escape newlines, which is bad.

- also no longer use nm_utils_same_ssid(). Since it no longer
  treated trailing NUL special, it is not different from
  g_bytes_equal().

- also, don't use nm_utils_ssid_to_utf8() for logging anymore.
  For logging, _nm_utils_ssid_escape_utf8safe() is better because
  it is loss-less escaping which can be unambigously reverted.
2018-08-22 10:49:34 +02:00
Thomas Haller
dba19ebd7d all: avoid useless cast of g_free() to GDestroyNotify 2018-08-22 10:49:34 +02:00