Commit Graph

377 Commits

Author SHA1 Message Date
Thomas Haller
0cba6bf1d2 cli/trivial: move code
These helper function will be needed in the next commit to be earlier.
Helper functions like these, that operate solely on trival types (in
this case, converting an enum to a string), make generally sense to have
at the beginning of the source file. Because they themself have few/no
dependencies and are rather trivial and self contained.
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
4029d4df0c cli: rework converting enums to strings in "connections.c"
Also, let the enum2str function return a non-translated string.
The user may choose to call gettext() on it.
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
bcc9e58bfe cli: allow setting the colors with terminal-colors.d(5)
The present version of the specification is somewhat unclear at times,
Unclear points were discussed with the maintainers [1] and probably
some new version will address those.

https://www.spinics.net/lists/util-linux-ng/msg15222.html

Until then here's how the implementation copes with ambiguities
(after the discussion with util-linux maintainers):

1.) It is unclear whether multiple .schem files should override each
    other or be merged. We use the overriding behavior -- take the
    highest priority one and ignore the rest.

2.) We assume "name.schem" is more specific than "@term.schem".

3.) We assume the "Color name" are to be used as aliases for the color
    sequences and translate them to ANSI escape sequences.

4.) The "Escape sequences" are of no use since the specification
    pretty much assumes an ANSI terminal and none of the sequences make
    any sense in ANSI color codes. We don't support them.
    accept that.

5.) We don't implement TERMINAL_COLORS_DEBUG because it's unspecified
    what should it do.
2018-05-10 14:36:58 +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
Lubomir Rintel
9dfe825840 cli: rework enabling and disabling colors
This actually makes very little difference at the moment, but will make
things more confortable later on, when the logic of enabling/disabling
coloring will involve terminal-colors.d(5).

Instead of deciding whether to use colors lazily with use_colors(), it's
done very early on nmcli initialization and a boolean use_colors field
is stored in the NmcConfig instance instead of the raw tristate option
of NmcColorOption type (which is now confined to nmcli.c).

Wherever the NmcColorOption was used previously, the whole NmcConfig
instance is passed around. That might seem pointless (since only the
use_colors boolean is actually used at the moment), but will be utilized
to pass around the actual color palette in future.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
56a5b27389 cli: drop --prompt-color
It's undocumented, useless, somewhat expensive in volume of code and
probably just downright stupid. We'll get a more general way to set
colors.

Hacking in some code to keep this working wouldn't be too difficult, but
it seems entirely pointless.
2018-05-10 14:36:58 +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
f0c1efbf42 all: add and utilize nm_utils_is_separator()
It is meant to be rather similar in nature to isblank() or
g_ascii_isspace().

Sadly, isblank() is locale dependent while g_ascii_isspace() also considers
vertical whitespace as a space. That's no good for configuration files that
are strucutured into lines, which happens to be a pretty common case.
2018-05-10 14:35:52 +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
5e69b8b9f1 cli: merge IPv4 and IPv6 variants of print_dhcp_config() 2018-04-23 15:43:39 +02:00
Beniamino Galvani
e27963d17f cli: fix connections completion
Let the matching continue when we are autocompleting arguments and we
have already found 'id', 'uuid' or 'path'.

Before:

 # nmcli connection modify path<TAB>
 path

After:
 # nmcli connection modify path<TAB>
 path
 pathfinder-wifi
2018-02-13 16:09:16 +01:00
Lubomir Rintel
9bf0b32cd1 cli/connections: avoid using synchronous get_secrets()
With --ask it might call back to nmcli's agent, causing a deadlock
while the client is waiting for the response. Let's give the client
a chance to service the agent requests while waiting:

  $ nmcli --ask --show-secrets c show 'Oracle HQ'
  <hang>

This is probably still rather suboptimal and inefficient, since we
still serialize the calls and block on response. However, if we submit
multiple calls to GetSecrets, the daemon would start authorizing the
first one and fail the other ones immediately before the authorization
succeeds.

This could perhaps be addressed in the daemon, but let's settle for a
fix that's compatible with the current daemon for now.
2018-02-13 15:03:49 +01:00
Thomas Haller
e4839accf5 all: replace non-leading tabs with spaces
We commonly only allow tabs at the beginning of a line, not
afterwards. The reason for this style is so that the code
looks formated right with tabstop=4 and tabstop=8.
2018-02-07 13:32:04 +01:00
Lubomir Rintel
6672c5e92e all: get rid of a handful of unused-but-set variables 2017-12-18 13:29:32 +01:00
Beniamino Galvani
54c0572de3 cli: fix editor crash
Ensure @cmd_arg0 is not freed when returning it.

Fixes: 8869943594
2017-12-15 10:08:06 +01:00
Thomas Haller
5d3736ac65 cli: drop nmc_strsplit_set()
In most cases, it copies the entire strv needlessly.
We can do better.

Also, the max_tokens argument is handled wrongly (albeit
not used anywhere anymore).
2017-12-12 15:19:43 +01:00
Thomas Haller
8869943594 cli: refactor splitting of first command line argument
nmc_strsplit_set() handles max_token wrong. It cannot call
g_strsplit_set() with max_token first, and then split empty
words. You cannot use g_strsplit_set() to achieve what
nmc_strsplit_set() wants to do, unless you first split all
tokens, then them construct them together again -- thereby
loosing the delimiters.

Anyway, there are just a few caller that do essentially the same.

Refactor the code to not use nmc_strsplit_set().
2017-12-12 15:19:43 +01:00
Lubomir Rintel
92f8f30d47 clients: add tc qdisc support
What works:

  nmcli c add con-name dum0 ifname dum0 type dummy \
      tc.qdiscs 'ingress, root pfifo_fast'
  nmcli c modify dum0 -tc.qdiscs 'root pfifo_fast'
  nmcli c modify dum0 +tc.qdiscs 'root handle 666: fq_codel'
2017-12-11 10:52:23 +01:00
Beniamino Galvani
21cfeff777 cli: exit from main loop after 'quit' editor command
When we stop processing the editor command loop (after a 'quit'
command) we must quit the GLib main loop.

https://bugzilla.redhat.com/show_bug.cgi?id=1517401
2017-11-29 11:05:04 +01:00
Thomas Haller
2e974baa15 cli: use nm_clear_g_source() util in quit() functions 2017-11-29 10:19:12 +01:00
Beniamino Galvani
021d797089 cli: do completion only when needed on 'nmcli con down'
$ nmcli connection down p
 path
 Connection 'p' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)

Don't do completion when not requested.
2017-11-28 09:37:06 +01:00
Thomas Haller
93adadbdcb all: use nm_direct_hash() instead of g_direct_hash()
We also do this for libnm, where it causes visible changes
in behavior. But if somebody would rely on the hashing implementation
for hash tables, it would be seriously flawed.
2017-11-16 11:49:52 +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
5b29c2e5b9 all: use nm_close() instead of close() 2017-11-14 15:10:42 +01:00
Beniamino Galvani
61b666902b cli: enable secret-agent only after activation
Change the activation procedure for connections that require secrets
in the following way:

 - nmcli creates a secret-agent and leaves it disabled so that
   incoming requests are queued
 - nmcli calls ActivateConnection()
 - when the method returns success, the secret-agent gets enabled and
   all queued requests are processed

When the user activates a connection which is already auto-activating,
NM will deactivate the current active-connection and will call
CancelGetSecrets() for it before the new ActivateConnection()
returns. In this way, when the secret-agent is enabled by nmcli, we
have the guarantee that there aren't any queued requests for
the deactivating connections.

https://bugzilla.redhat.com/show_bug.cgi?id=1438476
2017-11-02 15:25:59 +01:00
Lubomir Rintel
aec8d36b3e clients: add support for ovs-bridge setting 2017-10-30 17:40:09 +01:00
Lubomir Rintel
6f73b4608a clients: add support for ovs-port setting 2017-10-30 17:40:09 +01:00
Lubomir Rintel
6dcd54b0de clients: add support for ovs-interface setting 2017-10-30 17:40:09 +01:00
Lubomir Rintel
e7ab81098a clients: add support for ovs-patch setting 2017-10-30 17:40:09 +01:00
Thomas Haller
53aa340476 nmcli: fix wrongly adding a generic setting when constructing connection
nmc_read_connection_properties() iterates over the input arguments, and
completes the connection.

Initially, the type is not yet known, in that case, we should not wrongly
assume that this is a generic connection. Later, when the type is specified,
con_settings() will return the correct settings.

Previously, this would wrongly add a [generic] section:

  $ nmcli connection add type ethernet ifname eth1 con-name ethie autoconnect no ipv4.method auto ethernet.cloned-mac-address random

With the fix, it still works to specify the type later:

  $ nmcli connection add ifname eth1 con-name ethie autoconnect no ipv4.method auto type ethernet

but it doesn't work, to specify the type after type-specific options:

  $ nmcli connection add ifname eth1 con-name ethie autoconnect no ipv4.method auto ethernet.cloned-mac-address random type ethernet
  Error: invalid or not allowed setting 'ethernet': 'ethernet' not among [connection, ipv4, ipv6, proxy].

The patch doesn't change the latter limitation.

Fixes: c5324ed285
2017-10-26 19:07:24 +02: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
1824cc9e3f clients: move get_effective_activation_state() to nm-clients-utils.c 2017-10-16 17:26:49 +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
Beniamino Galvani
aec559cea5 cli: split evaluation of activation state
The code used to determine the activation state is useful for other
clients as well, let's split it into a dedicated function that can be
shared.
2017-10-16 17:25:30 +02:00
Thomas Haller
889ecc98a1 cli: use cleanup attribute in editor_menu_main() 2017-09-26 15:11:58 +02:00
Thomas Haller
54490be96d cli: fix crash in interactive mode for "describe ."
https://bugzilla.gnome.org/show_bug.cgi?id=788104
2017-09-26 15:11:58 +02:00
Thomas Haller
f42b4960f8 cli: warn when adding connection with a name that already exists
As far as NetworkManager is concerned, the "connection.id" (also called
"con-name" in nmcli) is a pretty name and does not need to be unique.

UI components usually show the "connection.id" instead of the
"connection.uuid" identifier. It is hence likely, that the user
would not intentionally re-use the same name for multiple connection
profiles.

Print a warning to stderr when the user adds such a connection.

This only affects `nmcli connection add` and `nmcli connection import`,
but not `nmcli connection clone` and not interactive edit mode.

https://bugzilla.redhat.com/show_bug.cgi?id=1460796
2017-07-17 14:49:31 +02:00
Beniamino Galvani
f395a0c29b cli: don't track device state for VPN connections
Currently nmcli considers the state of the device associated to a
connection to determine the success of an activation; for VPNs the
device is the parent interface on which the VPN is established.

This means that VPNs on bond/bridge/team interfaces are reported as
connected immediately because of the special handling of master
devices state in check_activated().

The parent device state is not meaningful for VPNs, so don't track it.
2017-07-09 16:10:54 +02:00
Thomas Haller
c4ef6f2d2f cli: fix assertion in tab completion for gen_property_names()
$ nmcli connection edit type ethernet
  nmcli> describe x.y<TAB>
  (process:29799): libnmc-CRITICAL **: nm_meta_setting_info_editor_find_by_name: assertion 'setting_name' failed
2017-06-29 09:46:21 +02:00
Beniamino Galvani
f545ed486e cli: ask NMSettingConnection properties first in questionnaire mode
Since properties are asked only when the connection has the related
setting, ensure that the connection type is set early so that the base
type gets added to the connection before evaluating other settings.

After NMSettingConnection properties, ask properties for the base
setting and then all other settings.
2017-06-22 15:01:15 +02:00
Lubomir Rintel
aedeb3cbf4 clients: add support for Bluetooth NAP type 2017-05-31 20:17:42 +02:00
Thomas Haller
cd6449f162 cli: change check_activated() to ensure we don't miss a deactivation
In the previous code it is not clear to me that there won't be
a situation where we don't react on a state change, waiting for a
device-state-change that never comes.

Now, only wait for a better device-state reason if:

 - we have a device
 - and the ac-reason is unspecific (NM_ACTIVE_CONNECTION_STATE_REASON_DEVICE_DISCONNECTED)
 - and the device still references the current active connection
 - and the device state is not yet FAILED or DISCONNECTED.

The most important change is that we only wait longer, if the device's
active-connection is still the same as our current active connection.

I don't think this commit is really necessary, but I cannot understand
the previous logic.

See-also: 57a26fd2aa
2017-05-25 10:59:15 +02:00
Thomas Haller
4f69c1e85e cli: prettify check_activated() to by using switch() statement
No change in behavior.
2017-05-24 22:00:05 +02:00
Lubomir Rintel
6fde475b27 cli: shorten setting names to aliases in "nmcli c show" connection list
This basically uses "wifi" in place of "802-11-wireless" and analogously
for other setting types that have aliases unless a --terse option was
specified.

Apart from looking nicer it makes the default output less wide,
hopefully avoiding line breaks on 80 column terminals in basic use
cases.
2017-05-15 14:46:05 +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
Beniamino Galvani
07cba18036 cli: accept special slave connection types in editor 2017-04-20 14:34:20 +02:00
Beniamino Galvani
032a3b4712 cli: only return main setting names as connection types
Also, return the special slave types.
2017-04-20 14:34:20 +02:00