Commit graph

50 commits

Author SHA1 Message Date
Mary Strodl
52d08008b7
wwan: retry after no operator code
We've had a few rare instances where a modem stopped retrying
to autoconnect because it briefly didn't have an operator code.

This isn't a permanent failure, so we shouldn't abort completely
for it.
2025-10-10 08:16:57 -04:00
Íñigo Huguet
48fc40e1ca core: rename unmanaged flag SLEEPING->MANAGER_DISABLED
The flag is used for both sleeping and networking disabled conditions.
This is because internally they share logic, but it's not obvious for
users and it has caused confusion in the past when investigating why
devices didn't become managed. Make it explicit that it can be because
of either reason.

It would be better to create two separate flags, actually, and it
doesn't seem complex, but better not to risk introducing bugs for that
little benefit.

Logs before:
  device (enp4s0): state change: disconnected -> unmanaged (reason 'unmanaged-sleeping' ...

Logs before:
  device (enp4s0): state change: disconnected -> unmanaged (reason 'unmanaged-nm-disabled' ...
2025-09-23 09:17:07 +02:00
Íñigo Huguet
f6d6a7e2eb core, libnm: add the "networking off" reason
When we disable networking with `nmcli networking off` the reason that
is logged is "sleeping". Explain instead that networking is disabled.

Before:
  device (lo): state change: activated -> deactivating (reason 'sleeping' ...

After:
  device (lo): state change: activated -> deactivating (reason 'networking-off' ...
2025-09-23 09:17:07 +02:00
Íñigo Huguet
f5c5bcc99d libnmc: fix typo of missing close parenthesis 2025-08-25 13:35:33 +00:00
Andika Triwidada
079a754b1e libnmc/client-utils: typo fix, add closing parenthesis
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2059
2024-10-30 11:05:50 +01:00
Beniamino Galvani
bb6881f88c format: run nm-code-format
Reformat with:

  clang-format version 19.1.0 (Fedora 19.1.0-1.fc41)

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2046
2024-10-04 11:07:35 +02:00
Fernando Fernandez Mancera
79221f79a2 src: drop most slave references from the code
While we cannot remove all the references to "slave" we can remove most
of them.
2024-08-09 15:47:32 +02:00
Fernando Fernandez Mancera
090d617017 src: drop most master references from the code
While we cannot remove all the references to "master" we can remove most
of them.
2024-08-09 15:47:32 +02:00
Lubomir Rintel
b87845b584 libnmc/secret-agent-simple: advise if WPS PBC enrollment is active
Print a different message on "nmcli --ask d wifi connect" if WPS
enrollment is in progress.

This is the same as GNOME Shell's secret agent (and perhaps
others) do.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1961
2024-07-10 13:15:04 +02:00
Íñigo Huguet
18240bb72d libnmc: don't strip prefix tags from secret names
The daemon is now capable of understanding and removing these prefix
tags by itself. It is better than this is not a responsibility of the
secret agent because it requires changes in all secret agents to work
properly (see https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/1536).

If the secret agent knows what these prefix tags are, it can remove them
only in the text that is displayed in the UI, but maintaining the
original string as the secret name that is returned to the daemon.

Secret agents that doesn't know what these prefix tags are won't do
anything with them, and they will also return the same string as secret
name, as expected. The only drawback is that they might display the full
string to the user, which is not a nice UX but it will at least work.

Also, allow to translate the secret name for the UI in libnmc.
2024-06-18 14:00:38 +00:00
Beniamino Galvani
6af2fb351c core, libnm: expose the reason for unmanaged devices
A common source for doubts and questions from users is about why
devices are unmanaged. Unfortunately NM doesn't expose that
information properly via D-Bus and so it's not available in nmcli.

The device D-Bus object has two properties that are strictly related:
"state" and "state-reason". The latter represents the reason for the
current state. Introduce new reasons to indicate the possible causes
for the unmanaged state. Note that a device can be unmanaged because
of multiple reasons at the same time, we only return one.

Before:

  $ nmcli -f GENERAL.DEVICE,GENERAL.TYPE,GENERAL.STATE,GENERAL.reason device show

  GENERAL.DEVICE:                         enp7s0
  GENERAL.TYPE:                           ethernet
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         0 (No reason given)

  GENERAL.DEVICE:                         tun0
  GENERAL.TYPE:                           tun
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         0 (No reason given)

  GENERAL.DEVICE:                         hwsim0
  GENERAL.TYPE:                           unknown
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         0 (No reason given)

After:

  $ nmcli -f GENERAL.DEVICE,GENERAL.TYPE,GENERAL.STATE,GENERAL.reason device show

  GENERAL.DEVICE:                         enp7s0
  GENERAL.TYPE:                           ethernet
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         76 (The device is unmanaged by user decision via settings plugin ("unmanaged-devices" for keyfile or "NM_CONTROLLED=no" for ifcfg-rh))

  GENERAL.DEVICE:                         tun0
  GENERAL.TYPE:                           tun
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         75 (The device is unmanaged by explicit user decision (e.g. 'nmcli device set $DEV managed no')

  GENERAL.DEVICE:                         hwsim0
  GENERAL.TYPE:                           unknown
  GENERAL.STATE:                          10 (unmanaged)
  GENERAL.REASON:                         69 (The device is unmanaged because the device type is unmanaged by default)

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1887
2024-03-20 15:25:09 +01:00
Beniamino Galvani
f8e020c29e device: support creating generic devices via device-handler
If the device-handler of the generic connection is set, the connection
is virtual and the device is created by invoking the device-handler
via NetworkManager-dispatcher service.

With this change, a generic device now represents two different device
classes:

 - existing interfaces that are not natively supported or recognized
   by NetworkManager. Those devices have the `has_device_handler`
   property set to FALSE;

 - interfaces that are created by NM by invoking the device-handler;
   they have `has_device_handler` set to TRUE.

(cherry picked from commit df6c35ec75)
2024-02-21 11:49:19 +01:00
Íñigo Huguet
27c701ebfb libnmc: allow user input in ECHO mode for 2FA challenges
Depending on the type of challenge used in the 2FA authentication, the
user input doesn't need to be hidden and sometimes it's even undesired
(it makes more difficult to enter the text).

Allow to VPN plugins to indicate that a secret that is being requested
is a 2FA challenge with ECHO mode enabled:
- When using auth dialog: accept a new option "ForceEcho" that can be
  set to TRUE to enable ECHO.
- When using the fallback method: recognize the prefix
  "x-dynamic-challenge-echo". This indicate both that ECHO should be enabled
  and that this is a 2FA challenge (see previous commit).

The correct way to enable echo mode from VPN plugins is doing both
things: pass the hint prefixed with "x-dynamic-challenge-echo" and add the
option "ForceEcho=true" for the auth dialog.

An attempt to support ECHO mode from NM-openvpn was made by passing
"IsSecret=false", but it didn't work because nm-secret-agent-simple
ignores returned values for which "IsSecret=false". It's not a good idea
to start accepting them because we could break other plugins, and anyway
the challenge response is actually a secret, so it is better to keep it
as such and add this new "ForceEcho" option.

This is backwards compatible because existing plugins were not using the
tag nor the auth dialog option. Withouth them, the previous behaviour is
preserved. On the contrary, plugins that want to use this new feature
will need to bump their NM version dependency because old daemons will
not handle correctly the prefix tag.

Secret agents will need to be updated to check secret->force_echo if
they want to support this feature. Until they update, the only drawback
is that ECHO mode will be ignored and the user's input will be hidden.

Updated nmcli and nmtui to support ECHO mode.

(cherry picked from commit 2ab56e82d4)
2024-02-21 11:31:49 +01:00
Íñigo Huguet
345bd1b187 libnmc: fix secrets request on 2nd stage of 2FA authentication
Clients using nm-secret-agent-simple always asked for some default VPN
secrets, which are dependent on the VPN service, when the auth dialog
can't be used and the fallback method is used instead.

When using 2FA this has to be avoided in the 2nd step because those
default secrets were already requested and validated in the 1st step.

Fix it by adding a new "x-dynamic-challenge" prefix tag that can be used
in the hints received from the VPN plugin. This tag indicates that we
are in the 2nd step of a 2FA authentication. This way we know that we
don't have to request the default secrets this time. Note that the tag
name doesn't explicitly mention VPNs so it can be reused for other type
of connections in the future.

As the default secrets were requested always unconditionally when using
the fallback method, there is no possible workaround to this problem
that avoids having to change libnm-client.

The change is backwards compatible because VPN plugins were not using
the tag and the previous behaviour does not change if the tag is not
used. However, VPN plugins that want to properly support 2FA
aunthentication will need to bump the NM version dependency because
old daemons won't handle properly a hint with the new prefix tag.

Finally, move the macro that defines the "x-vpn-message:" tag in a public
header so it is more visible for users. It has been renamed and prefixed
with the NM_ namespace so it shouldn't collide with macros defined in
the VPN plugins.

(cherry picked from commit c5f46bae43)
2024-02-21 11:31:48 +01:00
Fernando Fernandez Mancera
e68bedd28d all: reformat code to clang shipped with Fedora 39 2023-12-06 10:37:24 +01:00
Mohammed Sadiq
d9b06a95c9 libnmc-base/utils: Improve qr code bottom border
The bottom border of the generated QR code had a different thickness
compared to other borders.

Improve it by using Upper Half Block so that all borders have similar
thickness.
2023-10-02 15:57:41 +00:00
Thomas Haller
c0c8eb347d
libnmc: fix openconnect option "--cafile" in nm_vpn_openconnect_authenticate_helper()
Fixes: 97f2a368f1 ('libnmc-base: add supported options for OpenConnect CLI authentication')
2023-05-12 13:25:58 +02:00
Thomas Haller
a8ba0ea4c7
libnmc: drop redundant defines for array lengths
- use G_N_ELEMENTS() macro instead of having separate defines. The separate
  defines mean that when we check g_return_val_if_fail(oc_argc <= OC_ARGS_MAX, FALSE)
  that we must double check that OC_ARGS_MAX is really the size of the array
  that we want to check.

- replace g_return_val_if_fail() with nm_assert(). In this case, it should be
  very clear by review that the buffer is indeed large enough and the assertion
  holds. Use nm_assert().

- use unsigned integer for the loop variables. While int theoretically
  might exploit undefined behavior of signed overflow, we should instead
  use unsigned at places where it's appropriate (for example, those
  variables are compared against G_N_ELEMENTS() which gives a size_t type.

- declare auto variables on separate lines.

- make the global variable oc_property_args static and const. The const
  means the linker will put it into read-only memory, so we would get
  a crash on accidental modification.
2023-05-12 12:48:00 +02:00
David Woodhouse
ddce34054e libnmc-base: fix port extraction for openconnect auth
With old versions of openconnect we need to extract the port# from the
initial URL and then append it to the hostname we eventually get back.

Using strrchr(gw, ':') isn't going to work right with IPv6 literals,
ad we should also be dropping any path element.

So switch to using an int for the port instead of a string, and import a
cut-down variant of openconnect's internal_parse_url() which does
*largely* the same thing with strrchr() but is saved by using the 'end'
value returned from strtol() and insisting that the port is the very
end of the host part of the URL.
2023-05-11 13:15:53 +01:00
David Woodhouse
f791b98284 libnmc-base: report explicit error if not gateway configured for openconnect
Rather than letting openconnect run, and whine that there's no gateway,
and making the user scroll up past the openconnect usage information,
give them an explicit error.
2023-05-11 13:15:53 +01:00
David Woodhouse
715921a1fd nmcli, nmtui: reduce duplication around openconnect auth helper
Pull a bunch of stuff into nm_vpn_openconnect_authenticate_helper() that
both callers were doing for themselves, and make its API a bit simpler.
It's given the NMSettingVpn and the GPtrArray of secrets, and it simply
succeeds or fails.
2023-05-11 13:15:53 +01:00
David Woodhouse
97f2a368f1 libnmc-base: add supported options for OpenConnect CLI authentication
Ideally, we wouldn't have this hard-coded in NetworkManager itself; we
would invoke a tool to do it for us, like the GUI auth-dialog, which
can live in the NetworkManager-openconnect repository and be kept up
to date as new options are added.

To start with though, let's bring it into sync. We don't add new options
that often, and this will cover the majority of use cases.
2023-05-11 13:15:53 +01:00
David Woodhouse
f8d82c7f10 nmcli, nmtui: update authentication for OpenConnect
Since OpenConnect 8.20, 'openconnect --authenticate' will return the
full gateway URL, including the hostname and the path. This allows
servers behind SNI-based proxies to work. To ensure we end up at the
same IP address even behind round-robin DNS, there is a separate
--resolve argument.

Update nmcli/nmtui to use this, as NetworkManager-openconnect does.

Shift some of the logic into the nm_vpn_openconnect_authenticate_helper()
function instead of duplicating it in the callers.

Also, pass the correct protocol in rather than only supporting Cisco
AnyConnect.
2023-05-11 13:15:53 +01:00
Thomas Haller
ce414933a7
core: use nm_io_fcntl_setfl_update_nonblock() helper 2023-04-04 08:43:21 +02:00
Thomas Haller
31fd8f60cf
all: use G_SPAWN_CLOEXEC_PIPES with g_spawn_async_with_pipes()
G_SPAWN_CLOEXEC_PIPES is supported since glib 2.40, which we already
depend on.
2023-04-04 08:43:20 +02:00
Thomas Haller
20bd6b6803
core: fix setting non-blocking FD in nm_utils_spawn_helper()
Fixes: df1d214b2e ('clients: polkit-agent: implement polkit agent without using libpolkit')
2023-04-04 08:43:20 +02:00
Corentin Noël
5d28a0dd89
doc: replace all (allow-none) annotations by (optional) and/or (nullable)
The (allow-none) annotation is deprecated since a long time now, it is better to
use (nullable) and/or (optional) which clarifies what it means with the (out)
annotation.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1551
2023-03-27 11:49:43 +02:00
Thomas Haller
0b1177cb18
all: use _NM_G_TYPE_CHECK_INSTANCE_CAST() for internal uses
G_TYPE_CHECK_INSTANCE_CAST() can trigger a "-Wcast-align":

    src/core/devices/nm-device-macvlan.c: In function 'parent_changed_notify':
    /usr/include/glib-2.0/gobject/gtype.h:2421:42: error: cast increases required alignment of target type [-Werror=cast-align]
     2421 | #  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
          |                                          ^
    /usr/include/glib-2.0/gobject/gtype.h:501:66: note: in expansion of macro '_G_TYPE_CIC'
      501 | #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
          |                                                                  ^~~~~~~~~~~
    src/core/devices/nm-device-macvlan.h:13:6: note: in expansion of macro 'G_TYPE_CHECK_INSTANCE_CAST'
       13 |     (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_DEVICE_MACVLAN, NMDeviceMacvlan))
          |      ^~~~~~~~~~~~~~~~~~~~~~~~~~

Avoid that by using _NM_G_TYPE_CHECK_INSTANCE_CAST().

This can only be done for our internal usages. The public headers
of libnm are not changed.
2022-12-16 10:55:03 +01:00
Thomas Haller
3fb8c0f614
clang-format: reformat code with clang-format 15.0.4-1.fc37
This is the version shipped in Fedora 37. As Fedora 37 is now out, the
core developers switch to it. Our gitlab-ci will also use that as base
image for the check-{patch.tree} tests and to generate the pages. There
is a need that everybody agrees on which clang-format version to use,
and that version should be the one of the currently used Fedora release.

Also update the used Fedora image in "contrib/scripts/nm-code-format-container.sh"
script.

The gitlab-ci still needs update in the following commit. The change
in isolation will break the "check-tree" test.
2022-11-23 09:17:21 +01:00
Thomas Haller
c8f3d50329
libnmc/trivial: rename _device_state_to_string() function
_device_state_externally_to_string() is a better name for what it does.
2022-07-21 22:03:33 +02:00
Christian Glombek
f00e747beb
libnm-client: Add public nm_conn_wireguard_import() func
This commit moves the `nm_vpn_wireguard_import()` function
implementation from `libnmc-base` to `libnm-client-impl`, renaming it to
`nm_conn_wireguard_import()`.

A new `nm_conn_utils` header file is added in `libnm-client-public`.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/1031

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1299
2022-07-21 14:53:26 +02:00
Thomas Haller
d8a4b3bec2
all: reformat with clang-format (clang-tools-extra-14.0.0-1.fc36) and update gitlab-ci to f36 2022-07-06 11:06:53 +02:00
Thomas Haller
532f3e34a8
glib-aux: drop nm_str_buf_init() for NM_STR_BUF_INIT()
NM_STR_BUF_INIT() and nm_str_buf_init() were pretty much redundant. Drop one of
them.

Usually our pattern is that we don't have functions that return structs.
But NM_STR_BUF_INIT() returns a struct, because it's convenient to use
with

  nm_auto_str_buf NMStrBuf strbuf = NM_STR_BUF_INIT(...);

So use that variant instead.
2022-05-09 19:18:30 +02:00
Till Maas
f1b3dd4eb5
libnmc: Remove verbose logging
Don't log the failure to spawn the auth dialog. This is polluting the
terminal when using nmcli when activating an OpenVPN profile if
/usr/libexec/nm-openvpn-auth-dialog is not available. Since nmcli can
still ask for the credentials, the missing auth dialog does not block
the activation, so the "warning" level is too much. Since it is a
library, any output to the terminal is bad, therefore remove the
logging.

Signed-off-by: Till Maas <opensource@till.name>
2022-03-04 18:09:02 +01:00
Sigurd Rønningen Jenssen
164840a33c
libnmc: fix typo in passwd-file example
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1096
2022-02-14 13:47:56 +01:00
Thomas Haller
615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00
Thomas Haller
db53e5f3cd nmcli: fix import WireGuard profile with DNS domain and address family disabled
In NetworkManager, a profile cannot have "ipvx.dns" or "ipvx.dns-search"
while the corresponding IP method is disabled. Together with the oddity
that in NetworkManager DNS settings are separate per IPv4 and IPv6, this
causes problems:

  $ cat wg0.conf
  [Interface]
  PrivateKey = CBXpiLxQ98TLISJ2cypEFtQb/djzYzENyy0jzhWa/UA=
  Address = 192.168.1.100
  DNS = 10.11.12.13, foobar.de

  [Peer]
  PublicKey = Wus1sBzZiQkyxr6ZitUFNvfYD7KJkwTsWlcxvJ/4SHI=
  Endpoint = 1.2.3.4:51827
  AllowedIPs = 0.0.0.0/0

  $ nmcli connection import type wireguard file wg0.conf
  Error: failed to import 'wg0.conf': Failed to create WireGuard connection: ipv6.dns-search: this property is not allowed for 'method=disabled'.

Fixes: 3ab082ed96 ('cli: support dns-search for import of WireGuard profiles')
2021-11-02 09:15:01 +00:00
Thomas Haller
ea49b50651
all: add some README.md files describing the purpose of our sources 2021-08-19 17:51:11 +02:00
Thomas Haller
3587cbd827
all: rename nm_utils_strsplit_set*() to nm_strsplit_set*() 2021-08-02 09:26:47 +02:00
Thomas Haller
4e109bacab
clang-format: use "IndentPPDirectives:None" instead of "BeforeHash"
Subjectively, I think this looks better.
2021-07-09 08:49:06 +02:00
Michael Catanzaro
c447a4886d
libnmc-base: don't set DNS priority when importing Wireguard connections
This is actually trying *too* hard to prevent DNS leaks, breaking normal
expected use of split DNS. Let systemd-resolved handle sending our DNS
queries to the right place instead.

It's true that NetworkManager is trying to emulate the behavior of
wg-quick here, and wg-quick uses 'resolvconf -x' to attempt to set
"exclusive" DNS. But with systemd-resolved this is implemented by
setting a ~. routing domain for the Wireguard interface. That is a
*really* big hammer already, since Domain=~. overrides +DefaultRoute,
ensuring most DNS queries can only go to other interfaces with Domain=~.
NetworkManager follows systemd-resolved's recommended convention by only
applying Domain=~. to other "privacy VPNs" since 1.26.6. Setting DNS
priority only prevents *domain-specific* "leaks", which are almost
always desired. For example, it prevents using both the Wireguard VPN
and a corporate VPN at the same time.

Note that all of the justification behind !688 applies here as well.

See-also: https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/688
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/585

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/901
2021-06-23 20:31:14 +02:00
Thomas Haller
cb933c5955
libnmc-base: make transfer of memory ownership in try_spawn_vpn_auth_helper() clearer
This is confusing Coverity:

  Error: RESOURCE_LEAK (CWE-772): [#def249] [important]
  NetworkManager-1.31.5/src/libnmc-base/nm-secret-agent-simple.c:810: alloc_fn: Storage is returned from allocation function "g_string_free".
  NetworkManager-1.31.5/src/libnmc-base/nm-secret-agent-simple.c:810: var_assign: Assigning: "auth_dialog_request_str" = storage returned from "g_string_free(auth_dialog_request, 0)".
  NetworkManager-1.31.5/src/libnmc-base/nm-secret-agent-simple.c:822: noescape: Resource "auth_dialog_request_str" is not freed or pointed-to in "g_output_stream_write_async".
  NetworkManager-1.31.5/src/libnmc-base/nm-secret-agent-simple.c:822: noescape: Resource "auth_dialog_request_str" is not freed or pointed-to in "g_output_stream_write_async".
  NetworkManager-1.31.5/src/libnmc-base/nm-secret-agent-simple.c:838: leaked_storage: Variable "auth_dialog_request_str" going out of scope leaks the storage it points to.
  #  836|                                 data);
  #  837|
  #  838|->     return TRUE;
  #  839|   }
  #  840|

Maybe this works better to avoid the warning. At least, it also
documents it better to the reader.
2021-05-27 10:42:10 +02:00
Thomas Haller
d15eb7dd6e
libnmc-base: optionally accept "ignore" in nmc_string_to_ternary() for NMTernary value 2021-05-12 22:25:11 +02:00
Thomas Haller
2c628e4762
libnmc-base:fix leak in NMSecretAgentSimple's request_secrets_from_ui()
Found by Coverity:

  Error: RESOURCE_LEAK (CWE-772): [#def271] [important]
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:874: alloc_fn: Storage is returned from allocation function "nm_utils_ssid_to_utf8".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:874: var_assign: Assigning: "ssid_utf8" = storage returned from "nm_utils_ssid_to_utf8(g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid))".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:877: noescape: Resource "ssid_utf8" is not freed or pointed-to in "g_strdup_printf".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:882: leaked_storage: Variable "ssid_utf8" going out of scope leaks the storage it points to.
  #  880|
  #  881|           if (!add_wireless_secrets(request, secrets))
  #  882|->             goto out_fail;
  #  883|       } else if (nm_connection_is_type(request->connection, NM_SETTING_WIRED_SETTING_NAME)) {
  #  884|           title = _("Wired 802.1X authentication");

  Error: RESOURCE_LEAK (CWE-772): [#def272] [important]
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:874: alloc_fn: Storage is returned from allocation function "nm_utils_ssid_to_utf8".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:874: var_assign: Assigning: "ssid_utf8" = storage returned from "nm_utils_ssid_to_utf8(g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid))".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:877: noescape: Resource "ssid_utf8" is not freed or pointed-to in "g_strdup_printf".
  NetworkManager-1.31.3/src/libnmc-base/nm-secret-agent-simple.c:883: leaked_storage: Variable "ssid_utf8" going out of scope leaks the storage it points to.
  #  881|           if (!add_wireless_secrets(request, secrets))
  #  882|               goto out_fail;
  #  883|->     } else if (nm_connection_is_type(request->connection, NM_SETTING_WIRED_SETTING_NAME)) {
  #  884|           title = _("Wired 802.1X authentication");
  #  885|           msg   = g_strdup_printf(_("Secrets are required to access the wired network %s"),

Fixes: 3fbabde4c3 ('libnm-core: replace GByteArray with pointer + length in some APIs')
2021-05-11 13:56:49 +02:00
Thomas Haller
7df7d46bfe
cli: make nmc_string_to_ternary() more flexible
- use strstrip() to remove leading and trailing whitespace
- use _nm_utils_ascii_str_to_int64() for parsing numeric values
  like -1, 0 and 1. In particular, this now also allows passing
  the numeric values.
- also accept "default" as valid value for NM_TERNARY_DEFAULT.

With this change, nmc_string_to_ternary() can also parse everything that
we commonly and currently parse with _nm_utils_enum_from_str_full()
and NM_TYPE_TERNARY. This will allow to configure ternary values in
a more flexible way.
2021-05-03 10:11:21 +02:00
Thomas Haller
bd9f941a3a
cli: make nmc_string_to_bool() more flexible
- use strstrip() to remove leading and trailing whitespace
- use _nm_utils_ascii_str_to_int64() for parsing numeric values
  like 0 and 1. The difference is small, for one, it also accepts
  hex numbers like 0x1. More interestingly, it uses our common
  number parsing function, and we will later do the same for
  parsing ternaries.
2021-05-03 10:11:21 +02:00
Thomas Haller
1731fcf35e
cli: improve error message about ambigious text in nmc_string_to_bool()
The "on x off" is confusing. Use "on, off".
2021-05-03 10:11:21 +02:00
Thomas Haller
f62ecc97c8
cli: use proper GError codes in "nm-client-utils.c"
g_set_error(error, 1, 0, ...) is not right. "1" is not a valid GQuark,
we should initialize proper error instances.

Use nm_utils_error_set() for that.

Also, the code previously hacked the numeric value "1" to indicate
ambiguous text. Add and use a new error code NM_UTILS_ERROR_AMBIGUOUS
for that.
2021-05-03 10:11:21 +02:00
Thomas Haller
1f33a59a0f
cli: use const argument for nmc_string_is_valid()
With a const argument, we can make variables static const,
which means the linker loads the memory as read only.

Also, use NM_CAST_STRV_CC() macro, which casts the argument
accordingly.
2021-05-03 10:11:20 +02:00
Thomas Haller
54976f23cd
build: move "clients/common/" to "src/libnmc-{base,setting}/" 2021-03-02 08:38:25 +01:00