diff options
| author | Michael Biebl <biebl@debian.org> | 2015-12-24 00:24:58 +0100 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2015-12-24 00:24:58 +0100 |
| commit | 54f6333410ffd570e62717d9e77c5c987175e397 (patch) | |
| tree | d0698cc29d0f53c0b6d2c68d35ee2e9d72153948 /clients/cli | |
| parent | a6ece1a2aa19a6268335c87d4fdef20123dd04a5 (diff) | |
Imported Upstream version 1.0.10 upstream/1.0.10
Diffstat (limited to 'clients/cli')
| -rw-r--r-- | clients/cli/Makefile.am | 6 | ||||
| -rw-r--r-- | clients/cli/Makefile.in | 28 | ||||
| -rw-r--r-- | clients/cli/common.c | 110 | ||||
| -rw-r--r-- | clients/cli/connections.c | 12 | ||||
| -rw-r--r-- | clients/cli/settings.c | 30 |
5 files changed, 161 insertions, 25 deletions
diff --git a/clients/cli/Makefile.am b/clients/cli/Makefile.am index eef99cba..2f394098 100644 --- a/clients/cli/Makefile.am +++ b/clients/cli/Makefile.am @@ -14,7 +14,9 @@ AM_CPPFLAGS = \ $(GLIB_CFLAGS) \ -DG_LOG_DOMAIN=\""nmcli"\" \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ - -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" + -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" \ + -DNMCONFDIR=\"$(nmconfdir)\" \ + -DNMLIBDIR=\"$(libdir)\" nmcli_SOURCES = \ agent.c \ @@ -38,6 +40,8 @@ nmcli_SOURCES = \ \ $(srcdir)/../common/nm-secret-agent-simple.c \ $(srcdir)/../common/nm-secret-agent-simple.h \ + $(srcdir)/../common/nm-vpn-helpers.c \ + $(srcdir)/../common/nm-vpn-helpers.h \ $(NULL) nmcli_LDADD = \ diff --git a/clients/cli/Makefile.in b/clients/cli/Makefile.in index d79f0e70..2abbf580 100644 --- a/clients/cli/Makefile.in +++ b/clients/cli/Makefile.in @@ -122,6 +122,8 @@ am__nmcli_SOURCES_DIST = agent.c agent.h common.c common.h \ utils.h polkit-agent.c polkit-agent.h \ $(srcdir)/../common/nm-secret-agent-simple.c \ $(srcdir)/../common/nm-secret-agent-simple.h \ + $(srcdir)/../common/nm-vpn-helpers.c \ + $(srcdir)/../common/nm-vpn-helpers.h \ $(srcdir)/../common/nm-polkit-listener.c \ $(srcdir)/../common/nm-polkit-listener.h @WITH_POLKIT_AGENT_TRUE@am__objects_1 = nm-polkit-listener.$(OBJEXT) @@ -129,7 +131,7 @@ am_nmcli_OBJECTS = agent.$(OBJEXT) common.$(OBJEXT) \ connections.$(OBJEXT) devices.$(OBJEXT) general.$(OBJEXT) \ settings.$(OBJEXT) nmcli.$(OBJEXT) utils.$(OBJEXT) \ polkit-agent.$(OBJEXT) nm-secret-agent-simple.$(OBJEXT) \ - $(am__objects_1) + nm-vpn-helpers.$(OBJEXT) $(am__objects_1) nmcli_OBJECTS = $(am_nmcli_OBJECTS) am__DEPENDENCIES_1 = @WITH_POLKIT_AGENT_TRUE@am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) @@ -472,14 +474,17 @@ AM_CPPFLAGS = -I${top_srcdir} -I${top_builddir} \ -I${top_srcdir}/clients/common $(GLIB_CFLAGS) \ -DG_LOG_DOMAIN=\""nmcli"\" \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ - -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" $(am__append_1) + -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" \ + -DNMCONFDIR=\"$(nmconfdir)\" -DNMLIBDIR=\"$(libdir)\" \ + $(am__append_1) nmcli_SOURCES = agent.c agent.h common.c common.h connections.c \ connections.h devices.c devices.h general.c general.h \ settings.c settings.h nmcli.c nmcli.h utils.c utils.h \ polkit-agent.c polkit-agent.h \ $(srcdir)/../common/nm-secret-agent-simple.c \ - $(srcdir)/../common/nm-secret-agent-simple.h $(NULL) \ - $(am__append_2) + $(srcdir)/../common/nm-secret-agent-simple.h \ + $(srcdir)/../common/nm-vpn-helpers.c \ + $(srcdir)/../common/nm-vpn-helpers.h $(NULL) $(am__append_2) nmcli_LDADD = $(GLIB_LIBS) $(READLINE_LIBS) \ $(top_builddir)/libnm/libnm.la $(am__append_3) @BUILD_SETTING_DOCS_TRUE@BUILT_SOURCES = settings-docs.c @@ -587,6 +592,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/general.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nm-polkit-listener.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nm-secret-agent-simple.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nm-vpn-helpers.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nmcli.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/polkit-agent.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/settings.Po@am__quote@ @@ -627,6 +633,20 @@ nm-secret-agent-simple.obj: $(srcdir)/../common/nm-secret-agent-simple.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o nm-secret-agent-simple.obj `if test -f '$(srcdir)/../common/nm-secret-agent-simple.c'; then $(CYGPATH_W) '$(srcdir)/../common/nm-secret-agent-simple.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../common/nm-secret-agent-simple.c'; fi` +nm-vpn-helpers.o: $(srcdir)/../common/nm-vpn-helpers.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT nm-vpn-helpers.o -MD -MP -MF $(DEPDIR)/nm-vpn-helpers.Tpo -c -o nm-vpn-helpers.o `test -f '$(srcdir)/../common/nm-vpn-helpers.c' || echo '$(srcdir)/'`$(srcdir)/../common/nm-vpn-helpers.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/nm-vpn-helpers.Tpo $(DEPDIR)/nm-vpn-helpers.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(srcdir)/../common/nm-vpn-helpers.c' object='nm-vpn-helpers.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o nm-vpn-helpers.o `test -f '$(srcdir)/../common/nm-vpn-helpers.c' || echo '$(srcdir)/'`$(srcdir)/../common/nm-vpn-helpers.c + +nm-vpn-helpers.obj: $(srcdir)/../common/nm-vpn-helpers.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT nm-vpn-helpers.obj -MD -MP -MF $(DEPDIR)/nm-vpn-helpers.Tpo -c -o nm-vpn-helpers.obj `if test -f '$(srcdir)/../common/nm-vpn-helpers.c'; then $(CYGPATH_W) '$(srcdir)/../common/nm-vpn-helpers.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../common/nm-vpn-helpers.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/nm-vpn-helpers.Tpo $(DEPDIR)/nm-vpn-helpers.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(srcdir)/../common/nm-vpn-helpers.c' object='nm-vpn-helpers.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o nm-vpn-helpers.obj `if test -f '$(srcdir)/../common/nm-vpn-helpers.c'; then $(CYGPATH_W) '$(srcdir)/../common/nm-vpn-helpers.c'; else $(CYGPATH_W) '$(srcdir)/$(srcdir)/../common/nm-vpn-helpers.c'; fi` + nm-polkit-listener.o: $(srcdir)/../common/nm-polkit-listener.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT nm-polkit-listener.o -MD -MP -MF $(DEPDIR)/nm-polkit-listener.Tpo -c -o nm-polkit-listener.o `test -f '$(srcdir)/../common/nm-polkit-listener.c' || echo '$(srcdir)/'`$(srcdir)/../common/nm-polkit-listener.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/nm-polkit-listener.Tpo $(DEPDIR)/nm-polkit-listener.Po diff --git a/clients/cli/common.c b/clients/cli/common.c index 35676d0f..86f91d58 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -32,6 +32,7 @@ #include "nm-glib-compat.h" +#include "nm-vpn-helpers.h" #include "common.h" #include "utils.h" @@ -927,15 +928,96 @@ nmc_find_connection (const GPtrArray *connections, } static gboolean +vpn_openconnect_get_secrets (NMConnection *connection, GPtrArray *secrets) +{ + GError *error = NULL; + NMSettingVpn *s_vpn; + const char *vpn_type, *gw, *port; + char *cookie = NULL; + char *gateway = NULL; + char *gwcert = NULL; + int status = 0; + int i; + gboolean ret; + + if (!connection) + return FALSE; + + if (!nm_connection_is_type (connection, NM_SETTING_VPN_SETTING_NAME)) + return FALSE; + + s_vpn = nm_connection_get_setting_vpn (connection); + vpn_type = nm_setting_vpn_get_service_type (s_vpn); + if (g_strcmp0 (vpn_type, NM_DBUS_INTERFACE ".openconnect")) + return FALSE; + + /* Get gateway and port */ + gw = nm_setting_vpn_get_data_item (s_vpn, "gateway"); + port = gw ? strrchr (gw, ':') : NULL; + + /* Interactively authenticate to OpenConnect server and get secrets */ + ret = nm_vpn_openconnect_authenticate_helper (gw, &cookie, &gateway, &gwcert, &status, &error); + if (!ret) { + g_printerr (_("Error: openconnect failed: %s\n"), error->message); + g_clear_error (&error); + return FALSE; + } + + if (WIFEXITED (status)) { + if (WEXITSTATUS (status) != 0) + g_printerr (_("Error: openconnect failed with status %d\n"), WEXITSTATUS (status)); + } else if (WIFSIGNALED (status)) + g_printerr (_("Error: openconnect failed with signal %d\n"), WTERMSIG (status)); + + /* Append port to the host value */ + if (gateway && port) { + char *tmp = gateway; + gateway = g_strdup_printf ("%s%s", gateway, port); + g_free (tmp); + } + + /* Fill secrets to the array */ + for (i = 0; i < secrets->len; i++) { + NMSecretAgentSimpleSecret *secret = secrets->pdata[i]; + + if (!g_strcmp0 (secret->vpn_type, vpn_type)) { + if (!g_strcmp0 (secret->vpn_property, "cookie")) { + g_free (secret->value); + secret->value = cookie; + cookie = NULL; + } else if (!g_strcmp0 (secret->vpn_property, "gateway")) { + g_free (secret->value); + secret->value = gateway; + gateway = NULL; + } else if (!g_strcmp0 (secret->vpn_property, "gwcert")) { + g_free (secret->value); + secret->value = gwcert; + gwcert = NULL; + } + } + } + g_free (cookie); + g_free (gateway); + g_free (gwcert); + + return TRUE; +} + +static gboolean get_secrets_from_user (const char *request_id, const char *title, const char *msg, + NMConnection *connection, gboolean ask, GHashTable *pwds_hash, GPtrArray *secrets) { int i; + /* Check if there is a VPN OpenConnect secret to ask for */ + if (ask) + vpn_openconnect_get_secrets (connection, secrets); + for (i = 0; i < secrets->len; i++) { NMSecretAgentSimpleSecret *secret = secrets->pdata[i]; char *pwd = NULL; @@ -945,17 +1027,23 @@ get_secrets_from_user (const char *request_id, if (pwds_hash && (pwd = g_hash_table_lookup (pwds_hash, secret->prop_name))) { pwd = g_strdup (pwd); } else { - g_print ("%s\n", msg); if (ask) { if (secret->value) { - /* Prefill the password if we have it. */ - rl_startup_hook = nmc_rl_set_deftext; - nmc_rl_pre_input_deftext = g_strdup (secret->value); + if (!g_strcmp0 (secret->vpn_type, NM_DBUS_INTERFACE ".openconnect")) { + /* Do not present and ask user for openconnect secrets, we already have them */ + continue; + } else { + /* Prefill the password if we have it. */ + rl_startup_hook = nmc_rl_set_deftext; + nmc_rl_pre_input_deftext = g_strdup (secret->value); + } } + g_print ("%s\n", msg); pwd = nmc_readline ("%s (%s): ", secret->name, secret->prop_name); if (!pwd) pwd = g_strdup (""); } else { + g_print ("%s\n", msg); g_printerr (_("Warning: password for '%s' not given in 'passwd-file' " "and nmcli cannot ask without '--ask' option.\n"), secret->prop_name); @@ -993,12 +1081,24 @@ nmc_secrets_requested (NMSecretAgentSimple *agent, gpointer user_data) { NmCli *nmc = (NmCli *) user_data; + NMConnection *connection = NULL; + char *path, *p; gboolean success = FALSE; if (nmc->print_output == NMC_PRINT_PRETTY) nmc_terminal_erase_line (); - success = get_secrets_from_user (request_id, title, msg, nmc->in_editor || nmc->ask, + /* Find the connection for the request */ + path = g_strdup (request_id); + if (path) { + p = strrchr (path, '/'); + if (p) + *p = '\0'; + connection = nmc_find_connection (nmc->connections, "path", path, NULL); + g_free (path); + } + + success = get_secrets_from_user (request_id, title, msg, connection, nmc->in_editor || nmc->ask, nmc->pwds_hash, secrets); if (success) nm_secret_agent_simple_response (agent, request_id, secrets); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 55bde29a..e5aaa658 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -9087,12 +9087,6 @@ do_connection_reload (NmCli *nmc, int argc, char **argv) nmc->return_value = NMC_RESULT_SUCCESS; nmc->should_wait = FALSE; - if (!nm_client_get_nm_running (nmc->client)) { - g_string_printf (nmc->return_text, _("Error: NetworkManager is not running.")); - nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING; - return nmc->return_value; - } - if (!nm_client_reload_connections (nmc->client, NULL, &error)) { g_string_printf (nmc->return_text, _("Error: failed to reload connections: %s."), error->message); @@ -9113,12 +9107,6 @@ do_connection_load (NmCli *nmc, int argc, char **argv) nmc->return_value = NMC_RESULT_SUCCESS; nmc->should_wait = FALSE; - if (!nm_client_get_nm_running (nmc->client)) { - g_string_printf (nmc->return_text, _("Error: NetworkManager is not running.")); - nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING; - return nmc->return_value; - } - if (argc == 0) { g_string_printf (nmc->return_text, _("Error: No connection specified.")); nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 8a6b5fa8..6a3c90c2 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -4150,13 +4150,26 @@ nmc_property_vlan_remove_egress_priority_map (NMSetting *setting, } /* --- NM_SETTING_VPN_SETTING_NAME property setter functions --- */ +/* Validate value of vpn 'data' and 'secret' options */ +static const char * +_validate_vpn_hash_value (const char *option, const char *value, GError **error) +{ + /* nm_setting_vpn_add_data_item() and nm_setting_vpn_add_secret() does not + * allow empty strings */ + if (!value || !*value) { + g_set_error (error, 1, 0, _("'%s' cannot be empty"), option); + return NULL; + } + return value; +} + /* 'data' */ DEFINE_SETTER_OPTIONS (nmc_property_vpn_set_data, NM_SETTING_VPN, NMSettingVpn, nm_setting_vpn_add_data_item, NULL, - NULL) + _validate_vpn_hash_value) DEFINE_REMOVER_OPTION (nmc_property_vpn_remove_option_data, NM_SETTING_VPN, nm_setting_vpn_remove_data_item) @@ -4167,7 +4180,7 @@ DEFINE_SETTER_OPTIONS (nmc_property_vpn_set_secrets, NMSettingVpn, nm_setting_vpn_add_secret, NULL, - NULL) + _validate_vpn_hash_value) DEFINE_REMOVER_OPTION (nmc_property_vpn_remove_option_secret, NM_SETTING_VPN, nm_setting_vpn_remove_secret) @@ -4273,12 +4286,23 @@ nmc_property_wired_set_s390_nettype (NMSetting *setting, const char *prop, const DEFINE_ALLOWED_VAL_FUNC (nmc_property_wired_allowed_s390_nettype, wired_valid_s390_nettypes) /* 's390-options' */ +/* Validate value of 's390-options' */ +static const char * +_validate_s390_option_value (const char *option, const char *value, GError **error) +{ + /* nm_setting_wired_add_s390_option() requires value len in <1,199> interval */ + if (!value || !*value || strlen (value) >= 200) { + g_set_error (error, 1, 0, _("'%s' string value should consist of 1 - 199 characters"), option); + return NULL; + } + return value; +} DEFINE_SETTER_OPTIONS (nmc_property_wired_set_s390_options, NM_SETTING_WIRED, NMSettingWired, nm_setting_wired_add_s390_option, nm_setting_wired_get_valid_s390_options, - NULL) + _validate_s390_option_value) DEFINE_REMOVER_OPTION (nmc_property_wired_remove_option_s390_options, NM_SETTING_WIRED, nm_setting_wired_remove_s390_option) |