Commit Graph

118 Commits

Author SHA1 Message Date
Beniamino Galvani
fd8d5a0c7a cli: don't create a NMClient for the 'connection reload' command
It is a waste of resources instantiating a NMClient, filling the
object cache and then throwing everything away without using it. This
can take seconds on slow systems with many objects. Since the
ReloadConnections doesn't need anything from the cache, just execute
the D-Bus method call directly.
2019-09-17 09:31:34 +02:00
Lubomir Rintel
24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Thomas Haller
43d93e7c1f cli: tab complete only full D-Bus paths if there is no prefix
When tab completing connections by path name without any already
typed parts, only suggest full D-Bus paths. The numbers are duplicate
and not preferred.

Before:

    $ nmcli connection show path <TAB>
    Display all 118 possibilities? (y or n)
    1                                            46                                           /org/freedesktop/NetworkManager/Settings/29
    10                                           47                                           /org/freedesktop/NetworkManager/Settings/3
    11                                           48                                           /org/freedesktop/NetworkManager/Settings/30
    12                                           49                                           /org/freedesktop/NetworkManager/Settings/31
    13                                           5                                            /org/freedesktop/NetworkManager/Settings/32

Afterwards:

    $ nmcli connection show path <TAB>
    /org/freedesktop/NetworkManager/Settings/1   /org/freedesktop/NetworkManager/Settings/28  /org/freedesktop/NetworkManager/Settings/46
    ...

and

    $ nmcli connection modify path 4<TAB>
    4   40  41  42  43  44  45  46  47  48  49
2019-04-15 20:50:50 +02:00
Thomas Haller
68bd018a88 cli: prefer matching connections by "uuid" instead of "id"
Scripts should refer to connections by UUID. That means, whenever a
UUID matches, that is really what the user wants. The specifier "uuid"
must have precedence over "id" (and all others). That means, we must
search all connections.

For example:

    $ UUIDS=($(nmcli -g TYPE,UUID connection show | sed -n 's/802-11-wireless://p'))
    $ nmcli -f connection.id,connection.uuid connection show "${UUIDS[@]}"

in this case we must preferrably match by UUID, regardless of whether
a "connection.id" exists.

Note that if you have:

    $ nmcli connection show | grep fdf7b2d2-2858-3938-9b14-7f1b514a9a00
    b                                     fdf7b2d2-2858-3938-9b14-7f1b514a9a00  ethernet   --
    fdf7b2d2-2858-3938-9b14-7f1b514a9a00  ab9f3891-3420-335e-89da-f14c1b94c540  ethernet   --

then certain commands will still select all matching connections:

    $ nmcli -f connection.id,connection.uuid --mode multiline connection show fdf7b2d2-2858-3938-9b14-7f1b514a9a00
    connection.id:                          fdf7b2d2-2858-3938-9b14-7f1b514a9a00
    connection.uuid:                        ab9f3891-3420-335e-89da-f14c1b94c540

    connection.id:                          b
    connection.uuid:                        fdf7b2d2-2858-3938-9b14-7f1b514a9a00

This only makes a difference for commands that must pick only one profile:

    $ nmcli connection modify fdf7b2d2-2858-3938-9b14-7f1b514a9a00 con-name new-name
2019-04-15 20:50:50 +02:00
Thomas Haller
87d16e935c cli: drop unnecessary NULL sentinel from nmc_complete_strings()
Since commit 62b939de4e ('cli: add nmc_complete_strv() which takes a
string array for completion that may contain NULL'), the sentinel is
no longer needed.
2019-04-15 20:50:50 +02:00
Thomas Haller
c5a247c4c0 cli: add nmc_complete_strv() which takes a string array for completion that may contain NULL
This will allow for a convenient calling pattern when some elements
should be printed optionally.

(cherry picked from commit 62b939de4e)
2019-03-07 22:22:39 +01:00
Thomas Haller
55ce9d9de9 clients/secret-agent: allow suppressing prompting the entry-id when requesting secrets
When asking for the preshared-key for WireGuard peers, the secret request
will be very verbose with redundant information. Allow suppressing the entry
id from the prompt.
2019-02-14 08:00:29 +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
93c848ca03 clients: don't tread secret agent as NMSecretAgentOld
Most of the times we actually need a NMSecretAgentSimple typed pointer.
This way, need need to cast less.

But even if we would need to cast more, it's better to have pointers
point to the actual type, not merely to avoid shortcomings of C.
2019-02-05 08:27:42 +01:00
Thomas Haller
0c17d34a7d cli: drop gettext() wrappers for no_l10n to-string functions
In most cases, we don't want the translated string (only marked
for translation, and then programatically the caller deciedes
whether to translate or not).

The few places that always call gettext() can do it explicitly.

Now, that our functions are all "no_l10n" by default, rename them.
2018-12-11 09:23:47 +01:00
Lubomir Rintel
05d6c993dd cli: initialize readline before installing the redisplay handler
Otherwise readline decides to initialize terminal handling at the first
readline call, and if that happens at the point it sees our
non-echoing rl_redisplay.

At that point, unless already intialized, readline wrongly convinces itself we
do our own handling of terminal peculiarities (such as cursor movement, or
erases).  We do not -- we merely wrap the stock rl_redisplay(), temporarily
hiding the actual characters.

The rl_initialize() in nmc_readline_echo()s fixes broken line editing in
password prompts that weren't preceded a previous non-password prompt.
The other one is there for consistency only. (I guess we should be
initializing readline before use anyway; although it seems to initialize
itself anyway if we fail to do so...)

https://github.com/NetworkManager/NetworkManager/pull/241
2018-10-23 15:42:19 +02:00
Thomas Haller
411243c654 cli: don't use global nm_cli in nmc_readline_*()
Globals are bad. Don't let nmc_readline_helper() access
nm_cli.

Instead, pass nmc_config along. nmc_config albeit being
a complex struct, is much more begning:

  - the configuration nmc_config is initialized early on
    and afterwards immutable.
  - it only contains simple fields, which affect the behavior.
  - it's not a global. While passing around the complex configuration
    struct, it is clear that all callpaths don't access additional
    global information.
2018-10-10 09:55:45 +02:00
Lubomir Rintel
de7a159e69 cli: print per-device & per-AF connectivity status 2018-09-24 15:38:08 +02:00
Thomas Haller
6bfab6796f cli: fix reading "vpn.secrets.*" from passwd-file
Due to a bug, we required VPN secrets to be prefixed with
"vpn.secret." instead of "vpn.secrets.". This was a change
in behavior with 1.12.0 release.

Fix it, to restore the old behavior. For backward compatibility
to the broken behavior, adjust parse_passwords() to treat accept
that as well.

https://bugzilla.redhat.com/show_bug.cgi?id=1628833
https://github.com/NetworkManager/NetworkManager/pull/201

Fixes: 0601b5d725
(cherry picked from commit 5815ae8c60)
2018-09-14 15:17:53 +02:00
Thomas Haller
ba350a3495 cli: add additional user-data argument to get_fcn()
The function nmc_print() receives a list of "targets". These are essentially
the rows that should be printed (while the "fields" list represents the columns).

When filling the cells with values, it calles repeatedly get_fcn() on the
column descriptors (fields), by passing each row (target).

The caller must be well aware that the fields and targets are
compatible. For example, in some cases the targets are NMDevice
instances and the target type must correspond to what get_fcn()
expects.

Add another user-data pointer that is passed on along with the
targets. That is useful, if we have a list of targets/rows, but
pass in additional data that applies to all rows alike.

It is still unused.
2018-07-09 15:43:55 +02:00
Thomas Haller
940979a5a6 cli: merge IPv4 and IPv6 versions of ip/dhcp config print 2018-07-09 15:43:55 +02:00
Thomas Haller
bddf482fa1 cli/trivial: move code 2018-07-09 15:43:55 +02:00
Thomas Haller
ff273b8221 cli: drop duplicate IPv6 property metadata 2018-07-09 15:43:55 +02:00
Thomas Haller
096ac93f8a cli: rework printing of dhcp options 2018-07-09 15:43:55 +02:00
Thomas Haller
dba41ee1e1 cli: return NULL strv array from _ip_config_get_routes() if there are no routes
The property getter for certain properties tries to return
a strv array.

In this case, the result should be identical, whether an
empty strv array or NULL is returned.

Let _ip_config_get_routes() return %NULL if there are no routes.
This should have no practical difference, but it actually exposes
a bug in "cli/common/utils.c", which was previously hidden by
not commonly returning %NULL. This bug will be fixed in the
next commit.
2018-06-15 11:48:16 +02:00
Lubomir Rintel
d52573d125 cli: allow specifying the connection by its filename
This is handy for users who configure NetworkManager by dropping files
into /etc.
2018-06-11 15:09:42 +02:00
Thomas Haller
fc475f25f2 cli: use macro for list of argument for NmcMetaGenericInfo.get_fcn()
The reasons are:

- I want to locate all implmenetations of the get_fcn() handler. By
  consistently using this macro, you can just grep for the macro and
  find them all.

- all implementations should follow the same style. This macro
  enforces the same names for arguments and avoids copy&paste.

- if we are going to add or change an argument, it becomes easier.
  That's because we can easily identify all implementation and can
  change arguments in one place.
2018-05-14 17:53:03 +02:00
Thomas Haller
69d5fc9bce cli: add and use macro for creating NmcMetaGenericInfo parent groups 2018-05-14 17:53:03 +02:00
Thomas Haller
4b3297271e cli: rework connection handling for multiple results
Functions like nmc_find_connection() and nmc_find_active_connection()
can easily find multiple matching results. For example, the
"connection.id" in NetworkManager is not enforced to be unique,
so if the user adds multiple connections with the same name,
they should be all selected.

The previous API had a @pos argument, that allowed to iterate over
the results. Change that, to return all matches in a GPtrArray.

Also, extend connection-show and other places, to anticipate that
a connection might be active multiple times in any moment.
2018-05-14 17:53:03 +02:00
Thomas Haller
5e34a4b424 cli: move find_active_connection() to common.c
Beside nmc_find_connection(), which is very similar.

Also, drop unused argument @cons.
2018-05-14 17:53:03 +02:00
Thomas Haller
f0a2686545 cli: cleanup code paths in "connections.c" 2018-05-14 17:53:03 +02:00
Lubomir Rintel
31aa2cfe29 cli: use a palette to implement coloring
This basically replaces the (NMMetaTermColor, NMMetaTermFormat) combo
with NMMetaColor that describes the colored element semantically as
opposed to storing the raw attributes.

A (currently static) paletted is used to translate the semantic color
code to the actual ANSI controle sequence. This matches what
terminal-colors.d(5) schemes use, making it convenient to implement
customizable palettes.
2018-05-10 14:36:58 +02:00
Thomas Haller
f2109cb810 cli: sort DHCP options in connection show output
Otherwise, the output is unstable and changes every time.
2018-04-23 15:44:44 +02:00
Thomas Haller
5e69b8b9f1 cli: merge IPv4 and IPv6 variants of print_dhcp_config() 2018-04-23 15:43:39 +02:00
Beniamino Galvani
50414353e1 clients: set @out_is_default in property getters 2018-04-13 17:02:55 +02:00
Beniamino Galvani
1f2319dcb9 clients: change nm_meta_abstract_info_get() to report defaults
Return a boolean to indicate whether the value is the default one, so
that the caller can choose to hide it.
2018-04-13 17:02:55 +02:00
Beniamino Galvani
6cba687bb3 cli: fix completion of help sub-command
'help' is completed without considering other alternatives:

 # nmcli connection modify h<TAB>
 help

After the patch:
 # nmcli connection modify h<TAB>
 help
 home-wifi

Fixes: 29bb6ae4fe
2018-02-13 16:09:16 +01:00
Lubomir Rintel
9d95e1f175 clients/cli: use a nicer password prompt
Makes sense in order for the user to know that they're actually typing
the password (edited just to illustrate the point, the actual output was
shamefully messy and perhaps needs fixing too):

  $ nmcli c up Wrathmosphere
  Passwords or encryption keys are required to access the wireless network 'Wrathmosphere'.
  Password (802-1x.password): *********
2017-12-18 14:47:37 +01:00
Lubomir Rintel
7c3e1d926a clients/cli: don't store a password in history
History is probably even not useful at all outside the interactive edit
mode, but that is another story. This just avoids awkward surprises,
such as:

https://bugzilla.gnome.org/show_bug.cgi?id=791200
2017-12-18 14:47:31 +01:00
Thomas Haller
0601b5d725 clients: some cleanup of requesting VPN secrets 2017-11-23 14:44:25 +01:00
Thomas Haller
3ee8de20c4 all: include "nm-utils/nm-hash-utils.h" by default
Next we will use siphash24() instead of the glib version g_direct_hash() or
g_str_hash(). Hence, the "nm-utils/nm-hash-utils.h" header becomes very
fundamental and will be needed basically everywhere.

Instead of requiring the users to include them, let it be included via
"nm-default.h" header.
2017-11-16 11:49:51 +01:00
Thomas Haller
f3146de41b libnm: avoid unnecessary copies accessing NMIPRoute's attributes
We want to support large number of routes. Reduce the number
of copies, by adding internal accessor functions.

Also, work around a complaint from coverity:

  46. NetworkManager-1.9.2/libnm-core/nm-utils.c:1987:
  dereference: Dereferencing a null pointer "names".
2017-10-30 14:12:41 +01:00
Thomas Haller
3434261811 core,clients: use our own string hashing function nm_str_hash()
Replace the usage of g_str_hash() with our own nm_str_hash().

GLib's g_str_hash() uses djb2 hashing function, just like we
do at the moment. The only difference is, that we use a diffrent
seed value.

Note, that we initialize the hash seed with random data (by calling
getrandom() or reading /dev/urandom). That is a change compared to
before.

This change of the hashing function and accessing the random pool
might be undesired for libnm/libnm-core. Hence, the change is not
done there as it possibly changes behavior for public API. Maybe
we should do that later though.

At this point, there isn't much of a change. This patch becomes
interesting, if we decide to use a different hashing algorithm.
2017-10-18 13:05:00 +02:00
Beniamino Galvani
aa964ae969 build: move nm-client-utils.c to libnmc-base.a
In a later commit we'll add a new generic client function used by
nmcli and nmtui. nm-client-utils.c seems the right place for it, so
move the file to the base library that is used by both clients.

While at it, also put in that file some functions that will be needed
by nmtui.
2017-10-16 17:25:35 +02:00
Thomas Haller
a22b017830 cli: show additional route attributes in IP4_CONFIG output 2017-09-26 19:36:51 +02:00
Thomas Haller
ccb83e34a3 cli: refactor printing IP6 device info 2017-09-26 19:36:51 +02:00
Beniamino Galvani
cd3a561251 cli: fix failed assertion in readline helper
The INT signal can arrive after a new line has been processed in
nmc_readline_helper(). In such case, the handler gets uninstalled by
readline_cb() and nmc_seen_sigint() returns TRUE. However it's an
error to call rl_callback_read_char() without handler, don't do it.

Fixes the following:
 "readline: readline_callback_read_char() called with no handler!"
 #0 __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
 #1 __GI_abort () at abort.c:90
 #2 rl_callback_read_char () at ../callback.c:116
 #3 nmc_readline_helper (prompt=prompt@entry=0x2aa0d229080 "nmcli> ") at clients/cli/common.c:1387
 #4 nmc_readline (prompt_fmt=prompt_fmt@entry=0x2aa0036ac9e "%s") at clients/cli/common.c:1448
 #5 do_connection_edit (connection=0x2aa0d215440, nmc=0x2aa00391298 <nm_cli>) at clients/cli/connections.c:7072

Fixes: 995229181c

https://bugzilla.redhat.com/show_bug.cgi?id=1458311
2017-06-15 10:03:58 +02:00
Beniamino Galvani
ed824102ff cli: fix completion of connection types
Return only main settings and the special slave types.
2017-04-20 14:34:20 +02:00
Thomas Haller
b629b98687 cli: move completion for connection type to meta data 2017-04-12 14:12:20 +02:00
Thomas Haller
41b31051f2 cli: use nmc_print() to output device's IP4 info
The IP4 info adds a new type: to expose strv arguments
for addresses, etc.
2017-04-12 14:00:00 +02:00
Thomas Haller
c5c48d4f5f cli: move global "loop" variable to nmcli.h header 2017-04-12 11:24:04 +02:00
Thomas Haller
f0d91455ca cli: move show_secret field to NmcConfig
The show-secret property is basically a part of the current
configuration, relevant during printing. It will be passed
on to nmc_print(), and hence must be part of NmcConfig.
2017-04-12 11:24:04 +02:00
Thomas Haller
9276655975 cli: move NmcMetaGenericInfo to "utils.h" 2017-04-05 16:53:06 +02:00
Thomas Haller
bfb9fd0d2f cli: split tracking of meta data out of NmcOutputField
When generating output data, nmcli iterates over a list of
property-descriptors (nmc_fields_ip4_config), creates an intermediate
array (output_data) and finally prints it.

However, previously both the meta data (nmc_fields_ip4_config) and
the intermediate format use the same type NmcOutputField. This means,
certain fields are relevant to describe a property, and other fields
are output/formatting fields.

Split this up. Now, the meta data is tracked in form of an NMMetaAbstractInfo
lists. This separates the information about properties from intermediate steps
during creation of the output.

Note that currently functions like print_ip4_config() still have all the
knowledge about how to generate the output. That is wrong, instead, the
meta data (NMMetaAbstractInfo) should describe how to create the output
and then all those functions could be replaced. This means, later we want
to add more knowledge to the NMMetaAbstractInfo, so it is important to
keep them separate from NmcOutputField.
2017-04-05 16:53:06 +02:00
Thomas Haller
aae721d0df cli: pass arguments for print_data separately of NmcOutputData
Don't pass on large structs of input arguments. It only convolutes
which arguments are passed, and where they come from.
2017-04-05 16:53:06 +02:00