Commit graph

119 commits

Author SHA1 Message Date
Thomas Haller
dbcb1d6d97 core: let nm_utils_secret_key_read() handle failures internally
and add nm_utils_secret_key_get() to cache the secret-key, to only
obtain it once.

nm_utils_secret_key_read() is not expected to fail. However, in case
of an unexpected error, don't propagate the error to the caller,
but instead handle it internally.

That means, in case of error:
  - log a warning within nm_utils_secret_key_read() itself.
  - always return a generated secret-key. In case of error, the
    key won't be persisted (obviously). But the caller can ignore
    the error and just proceed with an in-memory key.

Hence, also add nm_utils_secret_key_get() to cache the key. This way,
we only try to actually generate/read the secret-key once.  Since that
might fail and return an in-memory key, we must for future invocations
return the same key, without generating a new one.
2018-05-28 14:58:24 +02:00
Thomas Haller
d0f1dc654e core: ensure NUL terminated secret_key buffer
The secret_key is binary random data, so one shouldn't ever use it as a
NUL terminated string directly.

Still, just ensure that the entire buffer is always NUL terminated.
2018-05-28 14:58:24 +02:00
Thomas Haller
009b7d61ad core: minor cleanup using helpers NM_IN_STRSET() and nm_utils_strdict_get_keys() 2018-05-16 08:45:42 +02:00
Lubomir Rintel
e69d386975 all: use the elvis operator wherever possible
Coccinelle:

  @@
  expression a, b;
  @@
  -a ? a : b
  +a ?: b

Applied with:

  spatch --sp-file ternary.cocci --in-place --smpl-spacing --dir .

With some manual adjustments on spots that Cocci didn't catch for
reasons unknown.

Thanks to the marvelous effort of the GNU compiler developer we can now
spare a couple of bits that could be used for more important things,
like this commit message. Standards commitees yet have to catch up.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
8e066af7d5 core-utils: don't load modules not ending with ".so"
This prevents attempts to load garbage from the module directory.
2018-05-09 13:16:59 +02:00
Beniamino Galvani
1b5925ce88 all: remove consecutive empty lines
Normalize coding style by removing consecutive empty lines from C
sources and headers.

https://github.com/NetworkManager/NetworkManager/pull/108
2018-04-30 16:24:52 +02:00
Thomas Haller
86b54a65e6 core: add nm_utils_strdict_to_variant() helper 2018-04-23 15:43:39 +02:00
Beniamino Galvani
aca671fff0 all: replace "it's" with "its" where needed 2018-04-18 14:14:07 +02:00
Thomas Haller
e5e8f86c3d shared: move nm_utils_get_start_time_for_pid() to shared/nm-utils
We will also use it in nmcli later. It will be needed when we replace
polkit_unix_process_new_for_owner(). Which is still far down the road.
2018-04-16 16:03:14 +02:00
Thomas Haller
1b532b5fdc core: minor cleanup of nm_utils_g_value_set_strv()
Add some assertion, use an unsigned loop variable (that matches
GPtrArray.len type), and add a comment.
2018-04-13 09:09:46 +02:00
Thomas Haller
b0bf9b2b9b core: explicitly pass D-Bus path to nm_utils_log_connection_diff()
No longer rely on nm_connection_get_path() being meaningful in server.
It also was wrong. During update, nm_settings_connection_update()
would call
  nm_utils_log_connection_diff (replace_connection, NM_CONNECTION (self), ...
where replace_connection has no path set, and nothing was logged.

Fix it, by explicitly passing the D-Bus path. Also, because
nm-core-utils.c should be independent of nm-dbus-object.h.
2018-04-13 09:09:46 +02:00
Thomas Haller
868c3cedfd dhcp: remove unused nm_utils_resolve_conf_parse() function 2018-03-20 21:03:20 +01:00
Thomas Haller
7459548f23 core: return remaining lifetime from nm_utils_lifetime_get()
nm_utils_lifetime_get() already has so many arguments.
Essentially, the function returned %TRUE if and only if the
lifetime was greater then zero.

Combine the return value and the output argument for the lifetime.

It also matches better the function name: to get the lifetime.
2018-02-09 17:40:01 +01:00
Thomas Haller
54b2e8d679 platform: allow omitting output arguments for nm_utils_lifetime_get()
At various places we don't need the output argument. Allow to omit it,
which cleans up the caller.
2018-02-07 13:42:24 +01:00
Beniamino Galvani
e91f1a7d2a dns: introduce routing domains
Similarly to what systemd-resolved does, introduce the concept of
"routing" domain, which is a domain in the search list that is used
only to decide the interface over which a query must be forwarded, but
is not used to complete unqualified host names. Routing domains are
those starting with a tilde ('~') before the actual domain name.

Domains without the initial tilde are used both for completing
unqualified names and for the routing decision.
2018-01-12 13:42:08 +01:00
Thomas Haller
96ae9309e7 core: don't use const integers where const expression is needed 2017-12-15 11:48:38 +01:00
Thomas Haller
a7087b1f05 core: avoid dereferencing NULL in nm_utils_resolve_conf_parse()
Found by coverity.

Fixes: 8f1ef161f4
2017-12-12 11:15:38 +01:00
Thomas Haller
1e572ebf87 core: replace "dup()" by "fcntl(fd, F_DUPFD_CLOEXEC, 0)" 2017-11-27 14:03:00 +01:00
Thomas Haller
5b29c2e5b9 all: use nm_close() instead of close() 2017-11-14 15:10:42 +01:00
Thomas Haller
287d1aee77 all: avoid coverity warnings about "Missing Initialization"
31. NetworkManager-1.9.2/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c:974:
uninit_use_in_call: Using uninitialized value "contents_rest" when
calling "__strtok_r_1c".

33. NetworkManager-1.9.2/src/nm-core-utils.c:1957:
uninit_use: Using uninitialized value "s".

148. NetworkManager-1.9.2/src/nm-core-utils.c:1924:
uninit_use_in_call: Using uninitialized value "s" when calling
"nm_strstrip_avoid_copy".
2017-10-30 14:13:15 +01:00
Thomas Haller
4a8a5495a9 all: avoid coverity warnings about "Wrong Check of Return Value"
30. NetworkManager-1.9.2/src/settings/plugins/keyfile/nms-keyfile-writer.c:218:
check_return: Calling "g_mkdir_with_parents" without checking return
value (as is done elsewhere 4 out of 5
 times).

25. NetworkManager-1.9.2/src/platform/nm-linux-platform.c:3969:
check_return: Calling "_nl_send_nlmsg" without checking return value (as
is done elsewhere 4 out of 5 times).

34. NetworkManager-1.9.2/src/nm-core-utils.c:2843:
negative_returns: "fd2" is passed to a parameter that cannot be negative.

26. NetworkManager-1.9.2/src/devices/wwan/nm-modem-broadband.c:897:
check_return: Calling "nm_utils_parse_inaddr_bin" without checking
return value (as is done elsewhere 4 out of 5 times).

3. NetworkManager-1.9.2/src/devices/bluetooth/nm-bluez5-manager.c:386:
check_return: Calling "g_variant_lookup" without checking return value
(as is done elsewhere 79 out of 83 times).

16. NetworkManager-1.9.2/libnm-util/nm-setting.c:405:
check_return: Calling "nm_g_object_set_property" without checking return
value (as is done elsewhere 4 out of 5 times).
2017-10-30 14:10:56 +01:00
Thomas Haller
cd271d5cb1 core: add nm_utils_sysctl_ip_conf_is_path() util 2017-10-24 16:05:40 +02:00
Thomas Haller
32b3eb1181 core: merge IPv4 and IPv6 implementation of nm_utils_ip4_property_path()
and nm_utils_ip6_property_path().

Also, rename to nm_utils_sysctl_ip_conf_path().
2017-10-24 16:05:40 +02:00
Thomas Haller
6e01238a40 core: don't use static buffer for nm_utils_ip4_property_path()
and nm_utils_ip6_property_path(). The API with static buffers
looks a bit nicer. But I think they are dangerous, because
we tend to pass the buffer down several layers of the stack, and
it's not immediately clear, that we don't overwrite the static
buffer again (which we probably did not, but it's hard to verify
that there is no bug there).
2017-10-24 16:04:46 +02:00
Thomas Haller
de4742333a core: add option to pass ownership of file descriptor to nm_utils_fd_get_contents()
In many scenarios, we have no use for the file descriptor
after nm_utils_fd_get_contents(). We just want to read it
and close it.

API wise, it would be nice that the get_contents() function never
closes the passed in fd and it's always responsibility of the caller.

However, that costs an additional dup() syscall that could
be avoided, if we allow the function to (optionally) close
the file descriptor.
2017-10-19 15:49:58 +02:00
Beniamino Galvani
597072296a core: don't close input fd in nm_utils_fd_get_contents()
The function should not close the input file descriptor; however
fdopen() associates the fd to the new stream so that when the stream
is closed, the fd is too. The result is a double close() and the
second call can in certain cases affect a wrong fd.

Use a duplicate fd for the stream.

Fixes: 1d9bdad1df

https://bugzilla.redhat.com/show_bug.cgi?id=1451236
2017-10-19 09:03:28 +02:00
Thomas Haller
2f56de7492 all: add helper functions for nm_hash_update*()
By using a macro, we don't cast all the types to guint. Instead,
we use their native types directly. Hence, we don't need
nm_hash_update_uint64() nor nm_hash_update_ptr().
Also, for types smaller then guint like char, we save hashing
the all zero bytes.
2017-10-18 13:29:22 +02:00
Thomas Haller
0e9e35e309 all: refactor hashing by introducing NMHashState
The privious NM_HASH_* macros directly operated on a guint value
and were thus close to the actual implementation.

Replace them by adding a NMHashState struct and accessors to
update the hash state. This hides the implementation better
and would allow us to carry more state. For example, we could
switch to siphash24() transparently.

For now, we still do a form basically djb2 hashing, albeit with
differing start seed.

Also add nm_hash_str() and nm_str_hash():

- nm_hash_str() is our own string hashing implementation

- nm_str_hash() is our own string implementation, but with a
  GHashFunc signature, suitable to pass it to g_hash_table_new().
  Also, it has this name in order to remind you of g_str_hash(),
  which it is replacing.
2017-10-18 13:05:00 +02:00
Thomas Haller
281d2d9fad shared: split random and hash utils
"nm-utils/nm-shared-utils.h" shall contain utility function without other
dependencies. It is intended to be used by other projects as-is.

nm_utils_random_bytes() requires getrandom() and a HAVE_GETRANDOM configure
check. That makes it more cumbersome to re-use "nm-shared-utils.h", in
cases where you don't care about nm_utils_random_bytes().

Split nm_utils_random_bytes() out to a separate file.

Same for hash utils, which depend on nm_utils_random_bytes(). Also, hash
utils will eventually be extended to use siphash24.
2017-10-17 20:02:59 +02:00
Thomas Haller
4a2798434e core: introduce NM_HASH_INIT() to initialize hash seed
Introduce a NM_HASH_INIT() function. It makes the places
where we initialize a hash with a certain seed visually clear.

Also, move them from "shared/nm-utils/nm-shared-utils.h" to
"shared/nm-utils/nm-macros-internal.h". We might want to
have NM_HASH_INIT() non-inline (hence, define it in the
source file).
2017-10-13 12:47:55 +02:00
Thomas Haller
b01a453ca2 core: add nm_utils_random_bytes() and use getrandom()
Add a new function nm_utils_random_bytes().

This function now preferably uses getrandom() syscall if it is
available.

As fallback, it always tries to fill the buffer from /dev/urandom.
If it cannot, as last fallback it uses GRand, which cannot fail.
Hence, the function always sets some (pseudo) random bytes.

It also returns FALSE if the obtained bytes are possibly not good
randomness.
2017-10-13 12:47:55 +02:00
Thomas Haller
93ea7a5905 shared: move nm_utils_fd_*() from src/ to shared/nm-utils/
The functions are general purpose and independent from NetworkManager core.
Move them to "shared/nm-utils/" so they can be used independently.
2017-10-13 12:47:55 +02:00
Thomas Haller
32142a63e8 ndisc: rename NMNDiscPreference to NMIcmpv6RouterPref
We encounter the same enum in 3 forms:

  - NMNDiscPreference in NetworkManager
  - "enum ndp_route_preference" in <ndp.h>
  - ICMPV6_ROUTER_PREF_* in <linux/icmpv6.h>

Move our enum to nm-core-utils.h, so that it can be used
by platform code as well (platform code should not include
ndisc/nm-ndisc.h).

Also, NMNDiscPreference was not numerically identical to their
native values (meaning: it shuffled the names and numbers).
Make them all numerically equal, so that they can be used in
the same context.
This means, while previously we could compare NMNDiscPreference
directly according to their priority, we now need _preference_to_priority().
On the other hand, we could omit translate_preference() -- but actually,
we still have _route_preference_coerce() because pref comes from libndp
and is thus untrusted. We still have to range check it.
2017-10-12 10:27:27 +02:00
Thomas Haller
8f1ef161f4 core: refactor parsing resolve.conf
- merge the IPv4 and IPv6 implementations. They are for the most
  part identical. Also, they are independent of NMIP4Config/NMIP6Config.

- parse the entire file at once. Don't parse it twice, once for the
  name servers and once for the options. This also avoids loading
  /etc/resolv.conf twice, as it would be done before.
2017-10-09 22:05:35 +02:00
Thomas Haller
1348b7c83a shared: move utils NM_UTILS_ENUM2STR*, NM_UTILS_FLAGS2STR*, and nm_utils_to_string_buffer_init()
These are just simple utility functions, not tied to NetworkManager
core. Move them to shared/nm-utils, so that they may be reused
otherwise.
2017-10-05 11:50:31 +02:00
Thomas Haller
a2dcdbe042 core/utils: extend nm_utils_flags2str() for multi-value flags and name for zero
Allow passing a pretty name for the zero flag 0, like "none".

Also, don't require flags to be power-of-two. Instead, allow names for
multiple flags. For example an "all" name. By specifying multi-value
flags first, their nick will be supersede the more specific flags.
Probably it doesn't make sense in usual cases, but nm_utils_flags2str()
should prevent such use.
2017-10-05 11:50:31 +02:00
Thomas Haller
dd0617547d shared: add NMIPAddr struct 2017-09-18 20:14:09 +02:00
Thomas Haller
5c42cdb287 all: use _nm_utils_ip4_*() utils functions 2017-09-05 18:44:04 +02:00
Lubomir Rintel
1b3a0208d9 all,trivial: include kernel versions and release dates in comments
This will make us stop worry how relevant are chunks of compat code with
older kernels when deciding whether it's worth supporting/testing them.
As if we actually were testing old kernels.
2017-08-24 10:48:03 +02:00
Thomas Haller
54f8c2ac22 core: add nm_utils_ip6_address_same_prefix_cmp() helper
Useful for sorting/comparing.
2017-07-31 15:13:31 +02:00
Thomas Haller
b2d21817ad core: fix setting output argument in nm_utils_stable_id_parse()
This bug has no consequences, as the only caller already initialized
the output variable to NULL.

Fixes: f0d40525df
2017-07-25 13:09:45 +02:00
Thomas Haller
5e5aa39c97 core: allow omitting @src argument in nm_utils_ip6_address_clear_host_address()
For convenience, to clear the address inplace, allow to leave @src NULL,
instead of requiring to set @src to @dst.

The only problem is, if you make use of this extended behavior and later backport
the use to an older branch, ensure that you cherry-pick this commit too.
That is easy to miss, but you are testing the backport, right?
2017-07-25 06:44:13 +02:00
Thomas Haller
6d9c3eab38 platform: let NMPObject implement NMDedupIndexObj 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
Thomas Haller
f387614b91 core: print the connection.type in nm_utils_log_connection_diff() 2017-06-07 09:07:17 +02:00
Yuri Chornoivan
0050e8bd34 all: fix typos in documentation, translated strings and comments
https://bugzilla.gnome.org/show_bug.cgi?id=783173
2017-05-28 17:33:37 +02:00
Beniamino Galvani
1d08391ed1 all: fix typo 'entires' -> 'entries'
(cherry picked from commit a4cbfe6f0a)
2017-05-05 19:16:39 +02:00
Thomas Haller
cfd8cf54df core: make dad_counter argument guint32 type
The dad_counter is hashed into the resulting address. Since we
want the hashing to be independent of the architecture, we always
hash 32 bit of dad_counter. Make the dad_counter argument of
type guint32 for consistency.

In practice this has no effect because:
  - for all our (current!) architectues, guint is the same as
    guint32.
  - all callers of nm_utils_ipv6_addr_set_stable_privacy() keep
    their dad-counter argument as guint8, so they never even pass
    numbers larger then 255.
  - nm_utils_ipv6_addr_set_stable_privacy() limits dad_counter
    further against RFC7217_IDGEN_RETRIES.

(cherry picked from commit 951e5f5bf8)
2017-04-27 17:21:36 +02:00
Thomas Haller
9f7433f869 core: avoid generating reserved IPv6 interface identifiers
https://tools.ietf.org/html/rfc7217 says:

  The resulting Interface Identifier SHOULD be compared against the
  reserved IPv6 Interface Identifiers [RFC5453] [IANA-RESERVED-IID]
  and against those Interface Identifiers already employed in an
  address of the same network interface and the same network
  prefix.  In the event that an unacceptable identifier has been
  generated, this situation SHOULD be handled in the same way as
  the case of duplicate addresses (see Section 6).

In case of conflict, this suggests to create a new address incrementing
the DAD counter, etc. Don't do that. If we generate an address of the
reserved region, just rehash it right away. Note that the actual address
anyway appears random, so this re-hashing is just as good as incrementing
the DAD counter and going through the entire process again.

Note that now we no longer generate certain addresses like we did
previously. But realize that we now merely reject (1 + 16777216 + 128)
addresses out of 2^64. So, the likelyhood of of a user accidentally
generating an address that is suddenly rejected is in the order of
10e-13 (1 / 1,099,503,173,697). Which is not astronomically, but still
extreeeemely unlikely.

Also, the whole process is anyway build on the idea that somebody else
might generate conflicting addresses (DAD). It means, there was always
the extremely tiny chance that the address you generated last time is
suddenly taken by somebody else. So, this change appears to a user
like these reserved addresses are now claimed by another (non existing)
host and a different address gets generated -- business as usual, as
far as SLAAC is concerned.

(cherry picked from commit f15c4961ad)
2017-04-27 17:21:35 +02:00
Thomas Haller
8ac1bf76bd core: move NMIPAddr to nm-core-utils.h
(cherry picked from commit 67da0a28db)
2017-04-27 17:21:33 +02:00