cli: add 'password' option for 'nmcli device wifi hotspot'

It allows user provided password to be used to secure the hotspot.
Otherwise, nmcli will generate a suitable password.
This commit is contained in:
Jiří Klimeš
2015-10-13 17:48:06 +02:00
parent d6427d7198
commit 781d24f1dd
3 changed files with 58 additions and 14 deletions

View File

@@ -268,6 +268,7 @@ usage (void)
" wifi connect <(B)SSID> [password <password>] [wep-key-type key|phrase] [ifname <ifname>]\n" " wifi connect <(B)SSID> [password <password>] [wep-key-type key|phrase] [ifname <ifname>]\n"
" [bssid <BSSID>] [name <name>] [private yes|no] [hidden yes|no]\n\n" " [bssid <BSSID>] [name <name>] [private yes|no] [hidden yes|no]\n\n"
" wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>] [band a|bg] [channel <channel>]\n\n" " wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>] [band a|bg] [channel <channel>]\n\n"
" [password <password>]\n\n"
" wifi rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]\n\n" " wifi rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]\n\n"
)); ));
} }
@@ -373,7 +374,7 @@ usage_device_wifi (void)
"assumed that IP configuration is obtained via DHCP.\n" "assumed that IP configuration is obtained via DHCP.\n"
"\n" "\n"
"ARGUMENTS := wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>]\n" "ARGUMENTS := wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>]\n"
" [band a|bg] [channel <channel>]\n" " [band a|bg] [channel <channel>] [password <password>]\n"
"\n" "\n"
"Create a Wi-Fi hotspot. Use 'connection down' or 'device disconnect'\n" "Create a Wi-Fi hotspot. Use 'connection down' or 'device disconnect'\n"
"to stop the hotspot.\n" "to stop the hotspot.\n"
@@ -383,6 +384,7 @@ usage_device_wifi (void)
"ssid - SSID of the hotspot\n" "ssid - SSID of the hotspot\n"
"band - Wi-Fi band to use\n" "band - Wi-Fi band to use\n"
"channel - Wi-Fi channel to use\n" "channel - Wi-Fi channel to use\n"
"password - password to use for the hotspot\n"
"\n" "\n"
"ARGUMENTS := rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]\n" "ARGUMENTS := rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]\n"
"\n" "\n"
@@ -2763,12 +2765,15 @@ generate_wep_key (char *key, size_t len)
key[10] = '\0'; key[10] = '\0';
} }
static void static gboolean
set_wireless_security_for_hotspot (NMSettingWirelessSecurity *s_wsec, set_wireless_security_for_hotspot (NMSettingWirelessSecurity *s_wsec,
const char *wifi_mode, const char *wifi_mode,
NMDeviceWifiCapabilities caps) NMDeviceWifiCapabilities caps,
const char *password,
GError **error)
{ {
char key[11]; char generated_key[11];
const char *key;
const char *key_mgmt; const char *key_mgmt;
if (g_strcmp0 (wifi_mode, NM_SETTING_WIRELESS_MODE_AP) == 0) { if (g_strcmp0 (wifi_mode, NM_SETTING_WIRELESS_MODE_AP) == 0) {
@@ -2776,35 +2781,54 @@ set_wireless_security_for_hotspot (NMSettingWirelessSecurity *s_wsec,
nm_setting_wireless_security_add_proto (s_wsec, "rsn"); nm_setting_wireless_security_add_proto (s_wsec, "rsn");
nm_setting_wireless_security_add_pairwise (s_wsec, "ccmp"); nm_setting_wireless_security_add_pairwise (s_wsec, "ccmp");
nm_setting_wireless_security_add_group (s_wsec, "ccmp"); nm_setting_wireless_security_add_group (s_wsec, "ccmp");
generate_wpa_key (key, sizeof (key));
key_mgmt = "wpa-psk"; key_mgmt = "wpa-psk";
} else if (caps & NM_WIFI_DEVICE_CAP_WPA) { } else if (caps & NM_WIFI_DEVICE_CAP_WPA) {
nm_setting_wireless_security_add_proto (s_wsec, "wpa"); nm_setting_wireless_security_add_proto (s_wsec, "wpa");
nm_setting_wireless_security_add_pairwise (s_wsec, "tkip"); nm_setting_wireless_security_add_pairwise (s_wsec, "tkip");
nm_setting_wireless_security_add_group (s_wsec, "tkip"); nm_setting_wireless_security_add_group (s_wsec, "tkip");
generate_wpa_key (key, sizeof (key));
key_mgmt = "wpa-psk"; key_mgmt = "wpa-psk";
} else { } else
generate_wep_key (key, sizeof (key));
key_mgmt = "none"; key_mgmt = "none";
} } else
} else {
generate_wep_key (key, sizeof (key));
key_mgmt = "none"; key_mgmt = "none";
}
if (g_strcmp0 (key_mgmt, "wpa-psk") == 0) { if (g_strcmp0 (key_mgmt, "wpa-psk") == 0) {
/* use WPA */
if (password) {
if (!nm_utils_wpa_psk_valid (password)) {
g_set_error (error, NMCLI_ERROR, 0, _("'%s' is not valid WPA PSK"), password);
return FALSE;
}
key = password;
} else {
generate_wpa_key (generated_key, sizeof (generated_key));
key = generated_key;
}
g_object_set (s_wsec, g_object_set (s_wsec,
NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt,
NM_SETTING_WIRELESS_SECURITY_PSK, key, NM_SETTING_WIRELESS_SECURITY_PSK, key,
NULL); NULL);
} else { } else {
/* use WEP */
if (password) {
if (!nm_utils_wep_key_valid (password, NM_WEP_KEY_TYPE_KEY)) {
g_set_error (error, NMCLI_ERROR, 0,
_("'%s' is not valid WEP key (it should be 5 or 13 ASCII chars)"),
password);
return FALSE;
}
key = password;
} else {
generate_wep_key (generated_key, sizeof (generated_key));
key = generated_key;
}
g_object_set (s_wsec, g_object_set (s_wsec,
NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt,
NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, key, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, key,
NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, NM_WEP_KEY_TYPE_KEY, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, NM_WEP_KEY_TYPE_KEY,
NULL); NULL);
} }
return TRUE;
} }
static NMCResultCode static NMCResultCode
@@ -2819,6 +2843,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
const char *band = NULL; const char *band = NULL;
const char *channel = NULL; const char *channel = NULL;
unsigned long channel_int; unsigned long channel_int;
const char *password = NULL;
NMDevice *device = NULL; NMDevice *device = NULL;
int devices_idx; int devices_idx;
const GPtrArray *devices; const GPtrArray *devices;
@@ -2829,6 +2854,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
NMSettingWirelessSecurity *s_wsec; NMSettingWirelessSecurity *s_wsec;
NMSettingIPConfig *s_ip4, *s_ip6; NMSettingIPConfig *s_ip4, *s_ip6;
GBytes *ssid_bytes; GBytes *ssid_bytes;
GError *error = NULL;
/* Set default timeout waiting for operation completion. */ /* Set default timeout waiting for operation completion. */
if (nmc->timeout == -1) if (nmc->timeout == -1)
@@ -2881,6 +2907,13 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
channel = *argv; channel = *argv;
} else if (strcmp (*argv, "password") == 0) {
if (next_arg (&argc, &argv) != 0) {
g_string_printf (nmc->return_text, _("Error: %s argument is missing."), *(argv-1));
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
}
password = *argv;
} else { } else {
g_string_printf (nmc->return_text, _("Error: Unknown parameter %s."), *argv); g_string_printf (nmc->return_text, _("Error: Unknown parameter %s."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
@@ -2967,7 +3000,13 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
nm_connection_add_setting (connection, NM_SETTING (s_wsec)); nm_connection_add_setting (connection, NM_SETTING (s_wsec));
set_wireless_security_for_hotspot (s_wsec, wifi_mode, caps); if (!set_wireless_security_for_hotspot (s_wsec, wifi_mode, caps, password, &error)) {
g_object_unref (connection);
g_string_printf (nmc->return_text, _("Error: Invalid 'password': %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_clear_error (&error);
goto error;
}
s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip4)); nm_connection_add_setting (connection, NM_SETTING (s_ip4));

View File

@@ -1346,7 +1346,7 @@ _nmcli()
;; ;;
h|ho|hot|hots|hotsp|hotspo|hotspot) h|ho|hot|hots|hotsp|hotspo|hotspot)
_nmcli_array_delete_at words 0 2 _nmcli_array_delete_at words 0 2
OPTIONS=(ifname con-name ssid band channel) OPTIONS=(ifname con-name ssid band channel password)
_nmcli_compl_ARGS _nmcli_compl_ARGS
;; ;;
r|re|res|resc|resca|rescan) r|re|res|resc|resca|rescan)

View File

@@ -854,6 +854,7 @@ Otherwise the SSID would not be found and the connection attempt would fail.
.RE .RE
.TP .TP
.B wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>] [band a|bg] [channel <channel>] .B wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>] [band a|bg] [channel <channel>]
.B [password <password>]
.br .br
Create a Wi-Fi hotspot. The command creates a hotspot connection profile according to Create a Wi-Fi hotspot. The command creates a hotspot connection profile according to
Wi-Fi device capabilities and activates it on the device. The hotspot is secured with WPA Wi-Fi device capabilities and activates it on the device. The hotspot is secured with WPA
@@ -873,6 +874,10 @@ Parameters of the hotspot can be influenced by the optional parameters:
\(en Wi-Fi band to use \(en Wi-Fi band to use
.IP \fIchannel\fP 10 .IP \fIchannel\fP 10
\(en Wi-Fi channel to use \(en Wi-Fi channel to use
.IP \fIpassword\fP 10
\(en password to use for the created hotspot. If not provided,
nmcli will generate a password. The password is either WPA
pre-shared key or WEP key.
.RE .RE
.TP .TP
.B wifi rescan [ifname <ifname>] [[ssid <SSID>] ...] .B wifi rescan [ifname <ifname>] [[ssid <SSID>] ...]