/src/settings/plugins/ifnet/connection_parser.c
C | 3065 lines | 2568 code | 311 blank | 186 comment | 438 complexity | 54d6293ebe5a56028711311388915d1f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
- /*
- * Mu Qiao <qiaomuf@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Copyright (C) 1999-2010 Gentoo Foundation, Inc.
- */
- #include "config.h"
- #include <string.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <netinet/ether.h>
- #include <errno.h>
- #include <glib/gi18n.h>
- #include <nm-setting-connection.h>
- #include <nm-setting-ip4-config.h>
- #include <nm-setting-ip6-config.h>
- #include <nm-setting-ppp.h>
- #include <nm-setting-pppoe.h>
- #include <nm-setting-wired.h>
- #include <nm-setting-wireless.h>
- #include <nm-setting-8021x.h>
- #include <nm-system-config-interface.h>
- #include <nm-utils.h>
- #include "net_utils.h"
- #include "wpa_parser.h"
- #include "connection_parser.h"
- #include "nm-ifnet-connection.h"
- static void
- update_connection_id (NMConnection *connection, const char *conn_name)
- {
- gchar *idstr = NULL;
- gchar *uuid_base = NULL;
- gchar *uuid = NULL;
- int name_len;
- NMSettingConnection *setting;
- name_len = strlen (conn_name);
- if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
- idstr = utils_hexstr2bin (conn_name + 2, name_len - 2);
- } else
- idstr = g_strdup_printf ("%s", conn_name);
- uuid_base = idstr;
- uuid = nm_utils_uuid_generate_from_string (uuid_base);
- setting = nm_connection_get_setting_connection (connection);
- g_object_set (setting, NM_SETTING_CONNECTION_ID, idstr,
- NM_SETTING_CONNECTION_UUID, uuid, NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "update_connection_setting_from_config_block: name:%s, id:%s, uuid: %s",
- conn_name, idstr, uuid);
- g_free (uuid);
- g_free (idstr);
- }
- static gboolean eap_simple_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error);
- static gboolean eap_tls_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error);
- static gboolean eap_peap_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error);
- static gboolean eap_ttls_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error);
- typedef struct {
- const char *method;
- gboolean (*reader) (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error);
- gboolean wifi_phase2_only;
- } EAPReader;
- static EAPReader eap_readers[] = {
- {"md5", eap_simple_reader, TRUE},
- {"pap", eap_simple_reader, TRUE},
- {"chap", eap_simple_reader, TRUE},
- {"mschap", eap_simple_reader, TRUE},
- {"mschapv2", eap_simple_reader, TRUE},
- {"leap", eap_simple_reader, TRUE},
- {"tls", eap_tls_reader, FALSE},
- {"peap", eap_peap_reader, FALSE},
- {"ttls", eap_ttls_reader, FALSE},
- {NULL, NULL}
- };
- /* reading identity and password */
- static gboolean
- eap_simple_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error)
- {
- const char *value;
- /* identity */
- value = wpa_get_value (ssid, "identity");
- if (!value) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_IDENTITY for EAP method '%s'.",
- eap_method);
- return FALSE;
- }
- g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, value, NULL);
- /* password */
- value = wpa_get_value (ssid, "password");
- if (!value) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_PASSWORD for EAP method '%s'.",
- eap_method);
- return FALSE;
- }
- g_object_set (s_8021x, NM_SETTING_802_1X_PASSWORD, value, NULL);
- return TRUE;
- }
- static char *
- get_cert (const char *ssid, const char *key, const char *basepath)
- {
- const char *orig;
- /* If it's a relative path, convert to absolute using 'basepath' */
- orig = wpa_get_value (ssid, key);
- if (g_path_is_absolute (orig))
- return g_strdup (orig);
- return g_strdup_printf ("%s/%s", basepath, orig);
- }
- static gboolean
- eap_tls_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error)
- {
- const char *value;
- char *ca_cert = NULL;
- char *client_cert = NULL;
- char *privkey = NULL;
- const char *privkey_password = NULL;
- gboolean success = FALSE;
- NMSetting8021xCKFormat privkey_format = NM_SETTING_802_1X_CK_FORMAT_UNKNOWN;
- /* identity */
- value = wpa_get_value (ssid, "identity");
- if (!value) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_IDENTITY for EAP method '%s'.",
- eap_method);
- return FALSE;
- }
- g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, value, NULL);
- /* ca cert */
- ca_cert = get_cert (ssid, phase2 ? "ca_cert2" : "ca_cert", basepath);
- if (ca_cert) {
- if (phase2) {
- if (!nm_setting_802_1x_set_phase2_ca_cert (s_8021x,
- ca_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL, error))
- goto done;
- } else {
- if (!nm_setting_802_1x_set_ca_cert (s_8021x,
- ca_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL, error))
- goto done;
- }
- } else {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: missing %s for EAP"
- " method '%s'; this is insecure!",
- phase2 ? "IEEE_8021X_INNER_CA_CERT" :
- "IEEE_8021X_CA_CERT", eap_method);
- }
- /* Private key password */
- privkey_password = wpa_get_value (ssid,
- phase2 ? "private_key_passwd2" :
- "private_key_passwd");
- if (!privkey_password) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing %s for EAP method '%s'.",
- phase2 ? "IEEE_8021X_INNER_PRIVATE_KEY_PASSWORD" :
- "IEEE_8021X_PRIVATE_KEY_PASSWORD", eap_method);
- goto done;
- }
- /* The private key itself */
- privkey = get_cert (ssid, phase2 ? "private_key2" : "private_key", basepath);
- if (!privkey) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing %s for EAP method '%s'.",
- phase2 ? "IEEE_8021X_INNER_PRIVATE_KEY" :
- "IEEE_8021X_PRIVATE_KEY", eap_method);
- goto done;
- }
- if (phase2) {
- if (!nm_setting_802_1x_set_phase2_private_key (s_8021x,
- privkey,
- privkey_password,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- &privkey_format,
- error))
- goto done;
- } else {
- if (!nm_setting_802_1x_set_private_key (s_8021x,
- privkey,
- privkey_password,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- &privkey_format, error))
- goto done;
- }
- /* Only set the client certificate if the private key is not PKCS#12 format,
- * as NM (due to supplicant restrictions) requires. If the key was PKCS#12,
- * then nm_setting_802_1x_set_private_key() already set the client certificate
- * to the same value as the private key.
- */
- if (privkey_format == NM_SETTING_802_1X_CK_FORMAT_RAW_KEY
- || privkey_format == NM_SETTING_802_1X_CK_FORMAT_X509) {
- client_cert = get_cert (ssid, phase2 ? "client_cert2" : "client_cert", basepath);
- if (!client_cert) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing %s for EAP method '%s'.",
- phase2 ? "IEEE_8021X_INNER_CLIENT_CERT" :
- "IEEE_8021X_CLIENT_CERT", eap_method);
- goto done;
- }
- if (phase2) {
- if (!nm_setting_802_1x_set_phase2_client_cert (s_8021x,
- client_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL,
- error))
- goto done;
- } else {
- if (!nm_setting_802_1x_set_client_cert (s_8021x,
- client_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL, error))
- goto done;
- }
- }
- success = TRUE;
- done:
- g_free (ca_cert);
- g_free (client_cert);
- g_free (privkey);
- return success;
- }
- static gboolean
- eap_peap_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error)
- {
- char *ca_cert = NULL;
- const char *inner_auth = NULL;
- const char *peapver = NULL;
- char **list = NULL, **iter, *lower;
- gboolean success = FALSE;
- ca_cert = get_cert (ssid, "ca_cert", basepath);
- if (ca_cert) {
- if (!nm_setting_802_1x_set_ca_cert (s_8021x,
- ca_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL, error))
- goto done;
- } else {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: missing "
- "IEEE_8021X_CA_CERT for EAP method '%s'; this is"
- " insecure!", eap_method);
- }
- peapver = wpa_get_value (ssid, "phase1");
- /* peap version, default is automatic */
- if (peapver && strstr (peapver, "peapver")) {
- if (strstr (peapver, "peapver=0"))
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPVER,
- "0", NULL);
- else if (strstr (peapver, "peapver=1"))
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPVER,
- "1", NULL);
- else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown IEEE_8021X_PEAP_VERSION value '%s'",
- peapver);
- goto done;
- }
- }
- /* peaplabel */
- if (peapver && strstr (peapver, "peaplabel=1"))
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPLABEL, "1",
- NULL);
- inner_auth = wpa_get_value (ssid, "phase2");
- if (!inner_auth) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_INNER_AUTH_METHODS.");
- goto done;
- }
- /* Handle options for the inner auth method */
- list = g_strsplit (inner_auth, " ", 0);
- for (iter = list; iter && *iter; iter++) {
- gchar *pos = NULL;
- if (!strlen (*iter))
- continue;
- if (!(pos = strstr (*iter, "MSCHAPV2"))
- || !(pos = strstr (*iter, "MD5"))
- || !(pos = strstr (*iter, "GTC"))) {
- if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error))
- goto done;
- } else if (!(pos = strstr (*iter, "TLS"))) {
- if (!eap_tls_reader (pos, ssid, s_8021x, TRUE, basepath, error))
- goto done;
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown IEEE_8021X_INNER_AUTH_METHOD '%s'.",
- *iter);
- goto done;
- }
- pos = strchr (*iter, '=');
- pos++;
- lower = g_ascii_strdown (pos, -1);
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, lower,
- NULL);
- g_free (lower);
- break;
- }
- if (!nm_setting_802_1x_get_phase2_auth (s_8021x)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "No valid IEEE_8021X_INNER_AUTH_METHODS found.");
- goto done;
- }
- success = TRUE;
- done:
- g_free (ca_cert);
- if (list)
- g_strfreev (list);
- return success;
- }
- static gboolean
- eap_ttls_reader (const char *eap_method,
- const char *ssid,
- NMSetting8021x *s_8021x,
- gboolean phase2,
- const char *basepath,
- GError **error)
- {
- gboolean success = FALSE;
- const char *anon_ident = NULL;
- char *ca_cert = NULL;
- const char *tmp;
- char **list = NULL, **iter, *inner_auth = NULL;
- /* ca cert */
- ca_cert = get_cert (ssid, "ca_cert", basepath);
- if (ca_cert) {
- if (!nm_setting_802_1x_set_ca_cert (s_8021x,
- ca_cert,
- NM_SETTING_802_1X_CK_SCHEME_PATH,
- NULL, error))
- goto done;
- } else {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: missing "
- "IEEE_8021X_CA_CERT for EAP method '%s'; this is"
- " insecure!", eap_method);
- }
- /* anonymous indentity for tls */
- anon_ident = wpa_get_value (ssid, "anonymous_identity");
- if (anon_ident && strlen (anon_ident))
- g_object_set (s_8021x, NM_SETTING_802_1X_ANONYMOUS_IDENTITY,
- anon_ident, NULL);
- tmp = wpa_get_value (ssid, "phase2");
- if (!tmp) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_INNER_AUTH_METHODS.");
- goto done;
- }
- /* Handle options for the inner auth method */
- inner_auth = g_ascii_strdown (tmp, -1);
- list = g_strsplit (inner_auth, " ", 0);
- for (iter = list; iter && *iter; iter++) {
- gchar *pos = NULL;
- if (!strlen (*iter))
- continue;
- if ((pos = strstr (*iter, "mschapv2")) != NULL
- || (pos = strstr (*iter, "mschap")) != NULL
- || (pos = strstr (*iter, "pap")) != NULL
- || (pos = strstr (*iter, "chap")) != NULL) {
- if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error))
- goto done;
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTH,
- pos, NULL);
- } else if ((pos = strstr (*iter, "tls")) != NULL) {
- if (!eap_tls_reader (pos, ssid, s_8021x, TRUE, basepath, error))
- goto done;
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTHEAP,
- "tls", NULL);
- } else if ((pos = strstr (*iter, "mschapv2")) != NULL
- || (pos = strstr (*iter, "md5")) != NULL) {
- if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error)) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME, "SIMPLE ERROR");
- goto done;
- }
- g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTHEAP,
- pos, NULL);
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown IEEE_8021X_INNER_AUTH_METHOD '%s'.",
- *iter);
- goto done;
- }
- break;
- }
- success = TRUE;
- done:
- g_free (ca_cert);
- if (list)
- g_strfreev (list);
- g_free (inner_auth);
- return success;
- }
- /* type is already decided by net_parser, this function is just used to
- * doing tansformation*/
- static const gchar *
- guess_connection_type (const char *conn_name)
- {
- const gchar *type = ifnet_get_data (conn_name, "type");
- const gchar *ret_type = NULL;
- if (!g_strcmp0 (type, "ppp"))
- ret_type = NM_SETTING_PPPOE_SETTING_NAME;
- if (!g_strcmp0 (type, "wireless"))
- ret_type = NM_SETTING_WIRELESS_SETTING_NAME;
- if (!ret_type)
- ret_type = NM_SETTING_WIRED_SETTING_NAME;
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "guessed connection type (%s) = %s", conn_name, ret_type);
- return ret_type;
- }
- /* Reading mac address for setting connection option.
- * Unmanaged device mac address is required by NetworkManager*/
- static gboolean
- read_mac_address (const char *conn_name, GByteArray **array, GError **error)
- {
- const char *value = ifnet_get_data (conn_name, "mac");
- if (!value || !strlen (value))
- return TRUE;
- *array = nm_utils_hwaddr_atoba (value, ARPHRD_ETHER);
- if (!*array) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "The MAC address '%s' was invalid.", value);
- return FALSE;
- }
- return TRUE;
- }
- static void
- make_wired_connection_setting (NMConnection *connection,
- const char *conn_name,
- GError **error)
- {
- GByteArray *mac = NULL;
- NMSettingWired *s_wired = NULL;
- const char *value = NULL;
- s_wired = NM_SETTING_WIRED (nm_setting_wired_new ());
- /* mtu_xxx */
- value = ifnet_get_data (conn_name, "mtu");
- if (value) {
- long int mtu;
- errno = 0;
- mtu = strtol (value, NULL, 10);
- if (errno || mtu < 0 || mtu > 65535) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: invalid MTU '%s' for %s",
- value, conn_name);
- } else
- g_object_set (s_wired, NM_SETTING_WIRED_MTU,
- (guint32) mtu, NULL);
- }
- if (read_mac_address (conn_name, &mac, error)) {
- if (mac) {
- g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS,
- mac, NULL);
- g_byte_array_free (mac, TRUE);
- }
- } else {
- g_object_unref (s_wired);
- s_wired = NULL;
- }
- if (s_wired)
- nm_connection_add_setting (connection, NM_SETTING (s_wired));
- }
- /* add NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME,
- * NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID in future*/
- static void
- make_ip4_setting (NMConnection *connection,
- const char *conn_name,
- GError **error)
- {
- NMSettingIP4Config *ip4_setting =
- NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ());
- const char *value, *method;
- gboolean is_static_block = is_static_ip4 (conn_name);
- ip_block *iblock = NULL;
- /* set dhcp options (dhcp_xxx) */
- value = ifnet_get_data (conn_name, "dhcp");
- g_object_set (ip4_setting, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, value
- && strstr (value, "nodns") ? TRUE : FALSE,
- NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, value
- && strstr (value, "nogateway") ? TRUE : FALSE, NULL);
- if (!is_static_block) {
- method = ifnet_get_data (conn_name, "config");
- if (!method){
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown config for %s", conn_name);
- g_object_unref (ip4_setting);
- return;
- }
- if (strstr (method, "dhcp"))
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_METHOD,
- NM_SETTING_IP4_CONFIG_METHOD_AUTO,
- NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
- else if (strstr (method, "autoip")) {
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_METHOD,
- NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
- NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
- nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
- return;
- } else if (strstr (method, "shared")) {
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_METHOD,
- NM_SETTING_IP4_CONFIG_METHOD_SHARED,
- NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
- nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
- return;
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown config for %s", conn_name);
- g_object_unref (ip4_setting);
- return;
- }
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using %s method for %s",
- method, conn_name);
- }else {
- iblock = convert_ip4_config_block (conn_name);
- if (!iblock) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Ifnet plugin: can't aquire ip configuration for %s",
- conn_name);
- g_object_unref (ip4_setting);
- return;
- }
- /************** add all ip settings to the connection**********/
- while (iblock) {
- ip_block *current_iblock;
- NMIP4Address *ip4_addr = nm_ip4_address_new ();
- nm_ip4_address_set_address (ip4_addr, iblock->ip);
- nm_ip4_address_set_prefix (ip4_addr,
- nm_utils_ip4_netmask_to_prefix
- (iblock->netmask));
- /* currently all the IPs has the same gateway */
- nm_ip4_address_set_gateway (ip4_addr, iblock->gateway);
- if (iblock->gateway)
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES,
- TRUE, NULL);
- if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr))
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "ignoring duplicate IP4 address");
- nm_ip4_address_unref (ip4_addr);
- current_iblock = iblock;
- iblock = iblock->next;
- destroy_ip_block (current_iblock);
- }
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
- NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, !has_default_ip4_route (conn_name),
- NULL);
- }
- /* add dhcp hostname and client id */
- if (!is_static_block && strstr (method, "dhcp")) {
- gchar *dhcp_hostname, *client_id;
- get_dhcp_hostname_and_client_id (&dhcp_hostname, &client_id);
- if (dhcp_hostname) {
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME,
- dhcp_hostname, NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "DHCP hostname: %s",
- dhcp_hostname);
- g_free (dhcp_hostname);
- }
- if (client_id) {
- g_object_set (ip4_setting,
- NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID,
- client_id, NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "DHCP client id: %s",
- client_id);
- g_free (client_id);
- }
- }
- /* add all IPv4 dns servers, IPv6 servers will be ignored */
- set_ip4_dns_servers (ip4_setting, conn_name);
- /* DNS searches */
- value = ifnet_get_data (conn_name, "dns_search");
- if (value) {
- char *stripped = g_strdup (value);
- char **searches = NULL;
- strip_string (stripped, '"');
- searches = g_strsplit (stripped, " ", 0);
- if (searches) {
- char **item;
- for (item = searches; *item; item++) {
- if (strlen (*item)) {
- if (!nm_setting_ip4_config_add_dns_search (ip4_setting, *item))
- PLUGIN_WARN
- (IFNET_PLUGIN_NAME,
- " warning: duplicate DNS domain '%s'",
- *item);
- }
- }
- g_strfreev (searches);
- }
- }
- /* static routes */
- iblock = convert_ip4_routes_block (conn_name);
- while (iblock) {
- ip_block *current_iblock = iblock;
- const char *metric_str;
- char *stripped;
- long int metric;
- NMIP4Route *route = nm_ip4_route_new ();
- nm_ip4_route_set_dest (route, iblock->ip);
- nm_ip4_route_set_next_hop (route, iblock->gateway);
- nm_ip4_route_set_prefix (route,
- nm_utils_ip4_netmask_to_prefix
- (iblock->netmask));
- if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
- metric = strtol (metric_str, NULL, 10);
- nm_ip4_route_set_metric (route, (guint32) metric);
- } else {
- metric_str = ifnet_get_global_data ("metric");
- if (metric_str) {
- stripped = g_strdup (metric_str);
- strip_string (stripped, '"');
- metric = strtol (metric_str, NULL, 10);
- nm_ip4_route_set_metric (route,
- (guint32) metric);
- g_free (stripped);
- }
- }
- if (!nm_setting_ip4_config_add_route (ip4_setting, route))
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "warning: duplicate IP4 route");
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "new IP4 route:%d\n", iblock->ip);
- nm_ip4_route_unref (route);
- current_iblock = iblock;
- iblock = iblock->next;
- destroy_ip_block (current_iblock);
- }
- /* Finally add setting to connection */
- nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
- }
- static void
- make_ip6_setting (NMConnection *connection,
- const char *conn_name,
- GError **error)
- {
- NMSettingIP6Config *s_ip6 = NULL;
- gboolean is_static_block = is_static_ip6 (conn_name);
- // used to disable IPv6
- gboolean ipv6_enabled = FALSE;
- gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
- const char *value;
- ip6_block *iblock;
- gboolean never_default = !has_default_ip6_route (conn_name);
- s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
- if (!s_ip6) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Could not allocate IP6 setting");
- return;
- }
- value = ifnet_get_data (conn_name, "enable_ipv6");
- if (value && is_true (value))
- ipv6_enabled = TRUE;
- //FIXME Handle other methods that NM supports in future
- // Currently only Manual and DHCP are supported
- if (!ipv6_enabled) {
- g_object_set (s_ip6,
- NM_SETTING_IP6_CONFIG_METHOD,
- NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL);
- goto done;
- } else if (!is_static_block) {
- // config_eth* contains "dhcp6"
- method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
- never_default = FALSE;
- }
- // else if (!has_ip6_address(conn_name))
- // doesn't have "dhcp6" && doesn't have any ipv6 address
- // method = NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL;
- else
- // doesn't have "dhcp6" && has at least one ipv6 address
- method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "IPv6 for %s enabled, using %s",
- conn_name, method);
- g_object_set (s_ip6,
- NM_SETTING_IP6_CONFIG_METHOD, method,
- NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, FALSE,
- NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, FALSE,
- NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, never_default, NULL);
- /* Make manual settings */
- if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) {
- ip6_block *current_iblock;
- iblock = convert_ip6_config_block (conn_name);
- if (!iblock) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Ifnet plugin: can't aquire ip6 configuration for %s",
- conn_name);
- goto error;
- }
- /* add all IPv6 addresses */
- while (iblock) {
- NMIP6Address *ip6_addr = nm_ip6_address_new ();
- nm_ip6_address_set_address (ip6_addr, iblock->ip);
- nm_ip6_address_set_prefix (ip6_addr, iblock->prefix);
- if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) {
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "ipv6 addresses count: %d",
- nm_setting_ip6_config_get_num_addresses
- (s_ip6));
- } else {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "ignoring duplicate IP4 address");
- }
- nm_ip6_address_unref (ip6_addr);
- current_iblock = iblock;
- iblock = iblock->next;
- destroy_ip6_block (current_iblock);
- }
- } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) {
- /* - autoconf or DHCPv6 stuff goes here */
- }
- // DNS Servers, set NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS TRUE here
- set_ip6_dns_servers (s_ip6, conn_name);
- /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIP4Config */
- // Add routes
- iblock = convert_ip6_routes_block (conn_name);
- if (iblock)
- g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES,
- TRUE, NULL);
- /* Add all IPv6 routes */
- while (iblock) {
- ip6_block *current_iblock = iblock;
- const char *metric_str;
- char *stripped;
- long int metric = 1;
- NMIP6Route *route = nm_ip6_route_new ();
- nm_ip6_route_set_dest (route, iblock->ip);
- nm_ip6_route_set_next_hop (route, iblock->next_hop);
- nm_ip6_route_set_prefix (route, iblock->prefix);
- /* metric is not per routes configuration right now
- * global metric is also supported (metric="x") */
- if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
- metric = strtol (metric_str, NULL, 10);
- nm_ip6_route_set_metric (route, (guint32) metric);
- } else {
- metric_str = ifnet_get_global_data ("metric");
- if (metric_str) {
- stripped = g_strdup (metric_str);
- strip_string (stripped, '"');
- metric = strtol (metric_str, NULL, 10);
- nm_ip6_route_set_metric (route,
- (guint32) metric);
- g_free (stripped);
- } else
- nm_ip6_route_set_metric (route, (guint32) 1);
- }
- if (!nm_setting_ip6_config_add_route (s_ip6, route))
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: duplicate IP6 route");
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, " info: new IP6 route");
- nm_ip6_route_unref (route);
- current_iblock = iblock;
- iblock = iblock->next;
- destroy_ip6_block (current_iblock);
- }
- done:
- nm_connection_add_setting (connection, NM_SETTING (s_ip6));
- return;
- error:
- g_object_unref (s_ip6);
- PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: Ignore IPv6 for %s",
- conn_name);
- return;
- }
- static NMSetting *
- make_wireless_connection_setting (const char *conn_name,
- NMSetting8021x **s_8021x,
- GError **error)
- {
- GByteArray *array, *mac = NULL;
- NMSettingWireless *wireless_setting = NULL;
- gboolean adhoc = FALSE;
- const char *value;
- const char *type;
- /* PPP over WIFI is not supported yet */
- g_return_val_if_fail (conn_name != NULL
- && strcmp (ifnet_get_data (conn_name, "type"),
- "ppp") != 0, NULL);
- type = ifnet_get_data (conn_name, "type");
- if (strcmp (type, "ppp") == 0) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "PPP over WIFI is not supported yet");
- return NULL;
- }
- wireless_setting = NM_SETTING_WIRELESS (nm_setting_wireless_new ());
- if (read_mac_address (conn_name, &mac, error)) {
- if (mac) {
- g_object_set (wireless_setting,
- NM_SETTING_WIRELESS_MAC_ADDRESS, mac,
- NULL);
- g_byte_array_free (mac, TRUE);
- }
- } else {
- g_object_unref (wireless_setting);
- return NULL;
- }
- /* handle ssid (hex and ascii) */
- if (conn_name) {
- gsize ssid_len = 0, value_len = strlen (conn_name);
- const char *p;
- char *tmp, *converted = NULL;
- ssid_len = value_len;
- if ((value_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
- /* Hex representation */
- if (value_len % 2) {
- g_set_error (error, ifnet_plugin_error_quark (),
- 0,
- "Invalid SSID '%s' size (looks like hex but length not multiple of 2)",
- conn_name);
- goto error;
- }
- // ignore "0x"
- p = conn_name + 2;
- if (!is_hex (p)) {
- g_set_error (error,
- ifnet_plugin_error_quark (),
- 0,
- "Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)",
- conn_name, *p);
- goto error;
- }
- tmp = utils_hexstr2bin (p, value_len - 2);
- ssid_len = (value_len - 2) / 2;
- converted = g_malloc0 (ssid_len + 1);
- memcpy (converted, tmp, ssid_len);
- g_free (tmp);
- }
- if (ssid_len > 32 || ssid_len == 0) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)",
- conn_name, ssid_len);
- goto error;
- }
- array = g_byte_array_sized_new (ssid_len);
- g_byte_array_append (array, (const guint8 *) (converted ? converted : conn_name), ssid_len);
- g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, array, NULL);
- g_byte_array_free (array, TRUE);
- g_free (converted);
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing SSID");
- goto error;
- }
- /* mode=0: infrastructure
- * mode=1: adhoc */
- value = wpa_get_value (conn_name, "mode");
- if (value)
- adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
- if (exist_ssid (conn_name)) {
- const char *mode = adhoc ? "adhoc" : "infrastructure";
- g_object_set (wireless_setting, NM_SETTING_WIRELESS_MODE, mode,
- NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using mode: %s", mode);
- }
- /* BSSID setting */
- value = wpa_get_value (conn_name, "bssid");
- if (value) {
- GByteArray *bssid;
- bssid = nm_utils_hwaddr_atoba (value, ARPHRD_ETHER);
- if (!bssid) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid BSSID '%s'", value);
- goto error;
- }
- g_object_set (wireless_setting, NM_SETTING_WIRELESS_BSSID,
- bssid, NULL);
- g_byte_array_free (bssid, TRUE);
- }
- /* mtu_ssid="xx" */
- value = ifnet_get_data (conn_name, "mtu");
- if (value) {
- long int mtu;
- errno = 0;
- mtu = strtol (value, NULL, 10);
- if (errno || mtu < 0 || mtu > 50000) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: invalid MTU '%s' for %s",
- value, conn_name);
- } else
- g_object_set (wireless_setting, NM_SETTING_WIRELESS_MTU,
- (guint32) mtu, NULL);
- }
- PLUGIN_PRINT (IFNET_PLUGIN_NAME, "wireless_setting added for %s",
- conn_name);
- return NM_SETTING (wireless_setting);
- error:
- if (wireless_setting)
- g_object_unref (wireless_setting);
- return NULL;
- }
- static NMSettingWirelessSecurity *
- make_leap_setting (const char *ssid, GError **error)
- {
- NMSettingWirelessSecurity *wsec;
- const char *value;
- wsec =
- NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
- value = wpa_get_value (ssid, "key_mgmt");
- if (!value || strcmp (value, "IEEE8021X"))
- goto error; /* Not LEAP */
- value = wpa_get_value (ssid, "eap");
- if (!value || strcasecmp (value, "LEAP"))
- goto error; /* Not LEAP */
- value = wpa_get_value (ssid, "password");
- if (value && strlen (value))
- g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD,
- value, NULL);
- value = wpa_get_value (ssid, "identity");
- if (!value || !strlen (value)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing LEAP identity");
- goto error;
- }
- g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, value,
- NULL);
- g_object_set (wsec,
- NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x",
- NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", NULL);
- return wsec;
- error:
- if (wsec)
- g_object_unref (wsec);
- return NULL;
- }
- static gboolean
- add_one_wep_key (const char *ssid,
- const char *key,
- int key_idx,
- NMSettingWirelessSecurity *s_wsec,
- GError **error)
- {
- const char *value;
- char *converted = NULL;
- gboolean success = FALSE;
- g_return_val_if_fail (ssid != NULL, FALSE);
- g_return_val_if_fail (key != NULL, FALSE);
- g_return_val_if_fail (key_idx >= 0 && key_idx <= 3, FALSE);
- g_return_val_if_fail (s_wsec != NULL, FALSE);
- value = wpa_get_value (ssid, key);
- if (!value)
- return TRUE;
- /* Validate keys */
- if (strlen (value) == 10 || strlen (value) == 26) {
- /* Hexadecimal WEP key */
- if (!is_hex (value)) {
- g_set_error (error, ifnet_plugin_error_quark (),
- 0, "Invalid hexadecimal WEP key.");
- goto out;
- }
- converted = g_strdup (value);
- } else if (value[0] == '"'
- && (strlen (value) == 7 || strlen (value) == 15)) {
- /* ASCII passphrase */
- char *tmp = g_strdup (value);
- char *p = strip_string (tmp, '"');
- if (!is_ascii (p)) {
- g_set_error (error, ifnet_plugin_error_quark (),
- 0, "Invalid ASCII WEP passphrase.");
- g_free (tmp);
- goto out;
- }
- converted = utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2);
- g_free (tmp);
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid WEP key length. Key: %s", value);
- goto out;
- }
- if (converted) {
- nm_setting_wireless_security_set_wep_key (s_wsec, key_idx, converted);
- g_free (converted);
- success = TRUE;
- }
- out:
- return success;
- }
- static gboolean
- add_wep_keys (const char *ssid,
- NMSettingWirelessSecurity *s_wsec,
- GError **error)
- {
- if (!add_one_wep_key (ssid, "wep_key0", 0, s_wsec, error))
- return FALSE;
- if (!add_one_wep_key (ssid, "wep_key1", 1, s_wsec, error))
- return FALSE;
- if (!add_one_wep_key (ssid, "wep_key2", 2, s_wsec, error))
- return FALSE;
- if (!add_one_wep_key (ssid, "wep_key3", 3, s_wsec, error))
- return FALSE;
- return TRUE;
- }
- static NMSettingWirelessSecurity *
- make_wep_setting (const char *ssid, GError **error)
- {
- const char *auth_alg, *value;
- int default_key_idx = 0;
- NMSettingWirelessSecurity *s_wireless_sec;
- s_wireless_sec =
- NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
- g_object_set (s_wireless_sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
- "none", NULL);
- /* default key index */
- value = wpa_get_value (ssid, "wep_tx_keyidx");
- if (value) {
- default_key_idx = atoi (value);
- if (default_key_idx >= 0 && default_key_idx <= 3) {
- g_object_set (s_wireless_sec,
- NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX,
- default_key_idx, NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "Default key index: %d", default_key_idx);
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid default WEP key '%s'", value);
- goto error;
- }
- }
- if (!add_wep_keys (ssid, s_wireless_sec, error))
- goto error;
- /* If there's a default key, ensure that key exists */
- if ((default_key_idx == 1)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 1)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Default WEP key index was 2, but no valid KEY2 exists.");
- goto error;
- } else if ((default_key_idx == 2)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec,
- 2)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Default WEP key index was 3, but no valid KEY3 exists.");
- goto error;
- } else if ((default_key_idx == 3)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec,
- 3)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Default WEP key index was 4, but no valid KEY4 exists.");
- goto error;
- }
- /* authentication algorithms */
- auth_alg = wpa_get_value (ssid, "auth_alg");
- if (auth_alg) {
- if (strcmp (auth_alg, "OPEN") == 0) {
- g_object_set (s_wireless_sec,
- NM_SETTING_WIRELESS_SECURITY_AUTH_ALG,
- "open", NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "WEP: Use open system authentication");
- } else if (strcmp (auth_alg, "SHARED") == 0) {
- g_object_set (s_wireless_sec,
- NM_SETTING_WIRELESS_SECURITY_AUTH_ALG,
- "shared", NULL);
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "WEP: Use shared system authentication");
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid WEP authentication algorithm '%s'",
- auth_alg);
- goto error;
- }
- }
- if (!nm_setting_wireless_security_get_wep_key (s_wireless_sec, 0)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 1)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 2)
- && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 3)
- && !nm_setting_wireless_security_get_wep_tx_keyidx (s_wireless_sec)) {
- if (auth_alg && !strcmp (auth_alg, "shared")) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "WEP Shared Key authentication is invalid for "
- "unencrypted connections.");
- goto error;
- }
- /* Unencrypted */
- g_object_unref (s_wireless_sec);
- s_wireless_sec = NULL;
- }
- return s_wireless_sec;
- error:
- if (s_wireless_sec)
- g_object_unref (s_wireless_sec);
- return NULL;
- }
- static char *
- parse_wpa_psk (const char *psk, GError **error)
- {
- char *hashed = NULL;
- gboolean quoted = FALSE;
- if (!psk) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing WPA_PSK for WPA-PSK key management");
- return NULL;
- }
- /* Passphrase must be between 10 and 66 characters in length becuase WPA
- * hex keys are exactly 64 characters (no quoting), and WPA passphrases
- * are between 8 and 63 characters (inclusive), plus optional quoting if
- * the passphrase contains spaces.
- */
- if (psk[0] == '"' && psk[strlen (psk) - 1] == '"')
- quoted = TRUE;
- if (!quoted && (strlen (psk) == 64)) {
- /* Verify the hex PSK; 64 digits */
- if (!is_hex (psk)) {
- g_set_error (error, ifnet_plugin_error_quark (),
- 0,
- "Invalid WPA_PSK (contains non-hexadecimal characters)");
- goto out;
- }
- hashed = g_strdup (psk);
- } else {
- char *stripped = g_strdup (psk);
- strip_string (stripped, '"');
- /* Length check */
- if (strlen (stripped) < 8 || strlen (stripped) > 63) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid WPA_PSK (passphrases must be between "
- "8 and 63 characters long (inclusive))");
- g_free (stripped);
- goto out;
- }
- hashed = g_strdup (stripped);
- g_free (stripped);
- }
- if (!hashed) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Invalid WPA_PSK (doesn't look like a passphrase or hex key)");
- goto out;
- }
- out:
- return hashed;
- }
- static gboolean
- fill_wpa_ciphers (const char *ssid,
- NMSettingWirelessSecurity *wsec,
- gboolean group,
- gboolean adhoc)
- {
- const char *value;
- char **list = NULL, **iter;
- int i = 0;
- value = wpa_get_value (ssid, group ? "group" : "pairwise");
- if (!value)
- return TRUE;
- list = g_strsplit_set (value, " ", 0);
- for (iter = list; iter && *iter; iter++, i++) {
- /* Ad-Hoc configurations cannot have pairwise ciphers, and can only
- * have one group cipher. Ignore any additional group ciphers and
- * any pairwise ciphers specified.
- */
- if (adhoc) {
- if (group && (i > 0)) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: ignoring group cipher '%s' (only one group cipher allowed in Ad-Hoc mode)",
- *iter);
- continue;
- } else if (!group) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: ignoring pairwise cipher '%s' (pairwise not used in Ad-Hoc mode)",
- *iter);
- continue;
- }
- }
- if (!strcmp (*iter, "CCMP")) {
- if (group)
- nm_setting_wireless_security_add_group (wsec,
- "ccmp");
- else
- nm_setting_wireless_security_add_pairwise (wsec,
- "ccmp");
- } else if (!strcmp (*iter, "TKIP")) {
- if (group)
- nm_setting_wireless_security_add_group (wsec,
- "tkip");
- else
- nm_setting_wireless_security_add_pairwise (wsec,
- "tkip");
- } else if (group && !strcmp (*iter, "WEP104"))
- nm_setting_wireless_security_add_group (wsec, "wep104");
- else if (group && !strcmp (*iter, "WEP40"))
- nm_setting_wireless_security_add_group (wsec, "wep40");
- else {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: ignoring invalid %s cipher '%s'",
- group ? "CIPHER_GROUP" : "CIPHER_PAIRWISE",
- *iter);
- }
- }
- if (list)
- g_strfreev (list);
- return TRUE;
- }
- static NMSetting8021x *
- fill_8021x (const char *ssid,
- const char *key_mgmt,
- gboolean wifi,
- const char *basepath,
- GError **error)
- {
- NMSetting8021x *s_8021x;
- const char *value;
- char **list, **iter;
- value = wpa_get_value (ssid, "eap");
- if (!value) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Missing IEEE_8021X_EAP_METHODS for key management '%s'",
- key_mgmt);
- return NULL;
- }
- list = g_strsplit (value, " ", 0);
- s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
- /* Validate and handle each EAP method */
- for (iter = list; iter && *iter; iter++) {
- EAPReader *eap = &eap_readers[0];
- gboolean found = FALSE;
- char *lower = NULL;
- lower = g_ascii_strdown (*iter, -1);
- while (eap->method && !found) {
- if (strcmp (eap->method, lower))
- goto next;
- /* Some EAP methods don't provide keying material, thus they
- * cannot be used with WiFi unless they are an inner method
- * used with TTLS or PEAP or whatever.
- */
- if (wifi && eap->wifi_phase2_only) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: ignored invalid "
- "IEEE_8021X_EAP_METHOD '%s'; not allowed for wifi.",
- lower);
- goto next;
- }
- /* Parse EAP method specific options */
- if (!(*eap->reader) (lower, ssid, s_8021x, FALSE, basepath, error)) {
- g_free (lower);
- goto error;
- }
- nm_setting_802_1x_add_eap_method (s_8021x, lower);
- found = TRUE;
- next:
- eap++;
- }
- if (!found) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- " warning: ignored unknown"
- "IEEE_8021X_EAP_METHOD '%s'.", lower);
- }
- g_free (lower);
- }
- g_strfreev (list);
- if (nm_setting_802_1x_get_num_eap_methods (s_8021x) == 0) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "No valid EAP methods found in IEEE_8021X_EAP_METHODS.");
- goto error;
- }
- return s_8021x;
- error:
- g_object_unref (s_8021x);
- return NULL;
- }
- static NMSettingWirelessSecurity *
- make_wpa_setting (const char *ssid,
- const char *basepath,
- NMSetting8021x **s_8021x,
- GError **error)
- {
- NMSettingWirelessSecurity *wsec;
- const char *value;
- char *lower;
- gboolean adhoc = FALSE;
- if (!exist_ssid (ssid)) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "No security info found for ssid: %s", ssid);
- return NULL;
- }
- wsec = NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
- /* mode=1: adhoc
- * mode=0: infrastructure */
- value = wpa_get_value (ssid, "mode");
- if (value)
- adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
- value = wpa_get_value (ssid, "key_mgmt");
- /* Not WPA or Dynamic WEP */
- if (!value)
- goto error;
- if (strcmp (value, "WPA-PSK") && strcmp (value, "WPA-EAP"))
- goto error;
- /* Pairwise and Group ciphers */
- fill_wpa_ciphers (ssid, wsec, FALSE, adhoc);
- fill_wpa_ciphers (ssid, wsec, TRUE, adhoc);
- /* WPA and/or RSN */
- if (adhoc) {
- /* Ad-Hoc mode only supports WPA proto for now */
- nm_setting_wireless_security_add_proto (wsec, "wpa");
- } else {
- nm_setting_wireless_security_add_proto (wsec, "wpa");
- nm_setting_wireless_security_add_proto (wsec, "rsn");
- }
- if (!strcmp (value, "WPA-PSK")) {
- char *psk = parse_wpa_psk (wpa_get_value (ssid, "psk"), error);
- if (!psk)
- goto error;
- g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_PSK, psk,
- NULL);
- g_free (psk);
- if (adhoc)
- g_object_set (wsec,
- NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
- "wpa-none", NULL);
- else
- g_object_set (wsec,
- NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
- "wpa-psk", NULL);
- } else if (!strcmp (value, "WPA-EAP") || !strcmp (value, "IEEE8021X")) {
- if (adhoc) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Ad-Hoc mode cannot be used with KEY_MGMT type '%s'",
- value);
- goto error;
- }
- *s_8021x = fill_8021x (ssid, value, TRUE, basepath, error);
- if (!*s_8021x)
- goto error;
- lower = g_ascii_strdown (value, -1);
- g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
- lower, NULL);
- g_free (lower);
- } else {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Unknown wireless KEY_MGMT type '%s'", value);
- goto error;
- }
- return wsec;
- error:
- if (wsec)
- g_object_unref (wsec);
- return NULL;
- }
- static NMSettingWirelessSecurity *
- make_wireless_security_setting (const char *conn_name,
- const char *basepath,
- NMSetting8021x **s_8021x,
- GError ** error)
- {
- NMSettingWirelessSecurity *wsec = NULL;
- const char *ssid;
- gboolean adhoc = FALSE;
- const char *value;
- g_return_val_if_fail (conn_name != NULL
- && strcmp (ifnet_get_data (conn_name, "type"),
- "ppp") != 0, NULL);
- if (!wpa_get_value (conn_name, "ssid"))
- return NULL;
- PLUGIN_PRINT (IFNET_PLUGIN_NAME,
- "updating wireless security settings (%s).", conn_name);
- ssid = conn_name;
- value = wpa_get_value (ssid, "mode");
- if (value)
- adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
- if (!adhoc) {
- wsec = make_leap_setting (ssid, error);
- if (error && *error)
- goto error;
- }
- if (!wsec) {
- wsec = make_wpa_setting (ssid, basepath, s_8021x, error);
- if (error && *error)
- goto error;
- }
- if (!wsec) {
- wsec = make_wep_setting (ssid, error);
- if (error && *error)
- goto error;
- }
- if (!wsec) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "Can't handle security information for ssid: %s",
- conn_name);
- }
- return wsec;
- error:
- return NULL;
- }
- /* Currently only support username and password */
- static void
- make_pppoe_connection_setting (NMConnection *connection,
- const char *conn_name,
- GError **error)
- {
- NMSettingPPPOE *s_pppoe;
- NMSettingPPP *s_ppp;
- const char *value;
- s_pppoe = NM_SETTING_PPPOE (nm_setting_pppoe_new ());
- /* username */
- value = ifnet_get_data (conn_name, "username");
- if (!value) {
- g_set_error (error, ifnet_plugin_error_quark (), 0,
- "ppp requires at lease a username");
- return;
- }
- g_object_set (s_pppoe, NM_SETTING_PPPOE_USERNAME, value, NULL);
- /* password */
- value = ifnet_get_data (conn_name, "password");
- if (!value) {
- value = "";
- }
- g_object_set (s_pppoe, NM_SETTING_PPPOE_PASSWORD, value, NULL);
- nm_connection_add_setting (connection, NM_SETTING (s_pppoe));
- /* PPP setting */
- s_ppp = (NMSettingPPP *) nm_setting_ppp_new ();
- nm_connection_add_setting (connection, NM_SETTING (s_ppp));
- }
- NMConnection *
- ifnet_update_connection_from_config_block (const char *conn_name,
- const char *basepath,
- GError **error)
- {
- const gchar *type = NULL;
- NMConnection *connection = NULL;
- NMSettingConnection *setting = NULL;
- NMSetting8021x *s_8021x = NULL;
- NMSettingWirelessSecurity *wsec = NULL;
- gboolean auto_conn = TRUE;
- const char *value = NULL;
- gboolean success = FALSE;
- connection = nm_connection_new ();
- if (!connection)
- return NULL;
- setting = nm_connection_get_setting_connection (connection);
- if (!setting) {
- setting = NM_SETTING_CONNECTION (nm_setting_connection_new ());
- g_assert (setting);
- nm_connection_add_setting (connection, NM_SETTING (setting));
- }
- type = guess_connection_type (conn_name);
- value = ifnet_get_data (conn_name, "auto");
- if (value && !strcmp (value, "false"))
- auto_conn = FALSE;
- update_connection_id (connection, conn_name);
- g_object_set (setting, NM_SETTING_CONNECTION_TYPE, type,
- NM_SETTING_CONNECTION_READ_ONLY, FALSE,
- NM_SETTING_CONNECTION_AUTOCONNECT, auto_conn, NULL);
- if (!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)
- || !strcmp (NM_SETTING_PPPOE_SETTING_NAME, type)) {
- /* wired setting */
- make_wired_connection_setting (connection, conn_name, error);
- if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "Found error: %s", (*error)->message);
- goto error;
- }
- /* pppoe setting */
- if (!strcmp (NM_SETTING_PPPOE_SETTING_NAME, type))
- make_pppoe_connection_setting (connection, conn_name,
- error);
- if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "Found error: %s", (*error)->message);
- goto error;
- }
- } else if (!strcmp (NM_SETTING_WIRELESS_SETTING_NAME, type)) {
- /* wireless setting */
- NMSetting *wireless_setting;
- wireless_setting = make_wireless_connection_setting (conn_name, &s_8021x, error);
- if (!wireless_setting)
- goto error;
- nm_connection_add_setting (connection, wireless_setting);
- if (error && *error) {
- PLUGIN_WARN (IFNET_PLUGIN_NAME,
- "Found error: …
Large files files are truncated, but you can click here to view the full file