Commit graph

32 commits

Author SHA1 Message Date
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