PageRenderTime 486ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/src/settings/plugins/ifnet/connection_parser.c

https://bitbucket.org/advaitraut/networkmanager
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

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
  2. /*
  3. * Mu Qiao <qiaomuf@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Copyright (C) 1999-2010 Gentoo Foundation, Inc.
  20. */
  21. #include "config.h"
  22. #include <string.h>
  23. #include <arpa/inet.h>
  24. #include <stdlib.h>
  25. #include <netinet/ether.h>
  26. #include <errno.h>
  27. #include <glib/gi18n.h>
  28. #include <nm-setting-connection.h>
  29. #include <nm-setting-ip4-config.h>
  30. #include <nm-setting-ip6-config.h>
  31. #include <nm-setting-ppp.h>
  32. #include <nm-setting-pppoe.h>
  33. #include <nm-setting-wired.h>
  34. #include <nm-setting-wireless.h>
  35. #include <nm-setting-8021x.h>
  36. #include <nm-system-config-interface.h>
  37. #include <nm-utils.h>
  38. #include "net_utils.h"
  39. #include "wpa_parser.h"
  40. #include "connection_parser.h"
  41. #include "nm-ifnet-connection.h"
  42. static void
  43. update_connection_id (NMConnection *connection, const char *conn_name)
  44. {
  45. gchar *idstr = NULL;
  46. gchar *uuid_base = NULL;
  47. gchar *uuid = NULL;
  48. int name_len;
  49. NMSettingConnection *setting;
  50. name_len = strlen (conn_name);
  51. if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
  52. idstr = utils_hexstr2bin (conn_name + 2, name_len - 2);
  53. } else
  54. idstr = g_strdup_printf ("%s", conn_name);
  55. uuid_base = idstr;
  56. uuid = nm_utils_uuid_generate_from_string (uuid_base);
  57. setting = nm_connection_get_setting_connection (connection);
  58. g_object_set (setting, NM_SETTING_CONNECTION_ID, idstr,
  59. NM_SETTING_CONNECTION_UUID, uuid, NULL);
  60. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  61. "update_connection_setting_from_config_block: name:%s, id:%s, uuid: %s",
  62. conn_name, idstr, uuid);
  63. g_free (uuid);
  64. g_free (idstr);
  65. }
  66. static gboolean eap_simple_reader (const char *eap_method,
  67. const char *ssid,
  68. NMSetting8021x *s_8021x,
  69. gboolean phase2,
  70. const char *basepath,
  71. GError **error);
  72. static gboolean eap_tls_reader (const char *eap_method,
  73. const char *ssid,
  74. NMSetting8021x *s_8021x,
  75. gboolean phase2,
  76. const char *basepath,
  77. GError **error);
  78. static gboolean eap_peap_reader (const char *eap_method,
  79. const char *ssid,
  80. NMSetting8021x *s_8021x,
  81. gboolean phase2,
  82. const char *basepath,
  83. GError **error);
  84. static gboolean eap_ttls_reader (const char *eap_method,
  85. const char *ssid,
  86. NMSetting8021x *s_8021x,
  87. gboolean phase2,
  88. const char *basepath,
  89. GError **error);
  90. typedef struct {
  91. const char *method;
  92. gboolean (*reader) (const char *eap_method,
  93. const char *ssid,
  94. NMSetting8021x *s_8021x,
  95. gboolean phase2,
  96. const char *basepath,
  97. GError **error);
  98. gboolean wifi_phase2_only;
  99. } EAPReader;
  100. static EAPReader eap_readers[] = {
  101. {"md5", eap_simple_reader, TRUE},
  102. {"pap", eap_simple_reader, TRUE},
  103. {"chap", eap_simple_reader, TRUE},
  104. {"mschap", eap_simple_reader, TRUE},
  105. {"mschapv2", eap_simple_reader, TRUE},
  106. {"leap", eap_simple_reader, TRUE},
  107. {"tls", eap_tls_reader, FALSE},
  108. {"peap", eap_peap_reader, FALSE},
  109. {"ttls", eap_ttls_reader, FALSE},
  110. {NULL, NULL}
  111. };
  112. /* reading identity and password */
  113. static gboolean
  114. eap_simple_reader (const char *eap_method,
  115. const char *ssid,
  116. NMSetting8021x *s_8021x,
  117. gboolean phase2,
  118. const char *basepath,
  119. GError **error)
  120. {
  121. const char *value;
  122. /* identity */
  123. value = wpa_get_value (ssid, "identity");
  124. if (!value) {
  125. g_set_error (error, ifnet_plugin_error_quark (), 0,
  126. "Missing IEEE_8021X_IDENTITY for EAP method '%s'.",
  127. eap_method);
  128. return FALSE;
  129. }
  130. g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, value, NULL);
  131. /* password */
  132. value = wpa_get_value (ssid, "password");
  133. if (!value) {
  134. g_set_error (error, ifnet_plugin_error_quark (), 0,
  135. "Missing IEEE_8021X_PASSWORD for EAP method '%s'.",
  136. eap_method);
  137. return FALSE;
  138. }
  139. g_object_set (s_8021x, NM_SETTING_802_1X_PASSWORD, value, NULL);
  140. return TRUE;
  141. }
  142. static char *
  143. get_cert (const char *ssid, const char *key, const char *basepath)
  144. {
  145. const char *orig;
  146. /* If it's a relative path, convert to absolute using 'basepath' */
  147. orig = wpa_get_value (ssid, key);
  148. if (g_path_is_absolute (orig))
  149. return g_strdup (orig);
  150. return g_strdup_printf ("%s/%s", basepath, orig);
  151. }
  152. static gboolean
  153. eap_tls_reader (const char *eap_method,
  154. const char *ssid,
  155. NMSetting8021x *s_8021x,
  156. gboolean phase2,
  157. const char *basepath,
  158. GError **error)
  159. {
  160. const char *value;
  161. char *ca_cert = NULL;
  162. char *client_cert = NULL;
  163. char *privkey = NULL;
  164. const char *privkey_password = NULL;
  165. gboolean success = FALSE;
  166. NMSetting8021xCKFormat privkey_format = NM_SETTING_802_1X_CK_FORMAT_UNKNOWN;
  167. /* identity */
  168. value = wpa_get_value (ssid, "identity");
  169. if (!value) {
  170. g_set_error (error, ifnet_plugin_error_quark (), 0,
  171. "Missing IEEE_8021X_IDENTITY for EAP method '%s'.",
  172. eap_method);
  173. return FALSE;
  174. }
  175. g_object_set (s_8021x, NM_SETTING_802_1X_IDENTITY, value, NULL);
  176. /* ca cert */
  177. ca_cert = get_cert (ssid, phase2 ? "ca_cert2" : "ca_cert", basepath);
  178. if (ca_cert) {
  179. if (phase2) {
  180. if (!nm_setting_802_1x_set_phase2_ca_cert (s_8021x,
  181. ca_cert,
  182. NM_SETTING_802_1X_CK_SCHEME_PATH,
  183. NULL, error))
  184. goto done;
  185. } else {
  186. if (!nm_setting_802_1x_set_ca_cert (s_8021x,
  187. ca_cert,
  188. NM_SETTING_802_1X_CK_SCHEME_PATH,
  189. NULL, error))
  190. goto done;
  191. }
  192. } else {
  193. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  194. " warning: missing %s for EAP"
  195. " method '%s'; this is insecure!",
  196. phase2 ? "IEEE_8021X_INNER_CA_CERT" :
  197. "IEEE_8021X_CA_CERT", eap_method);
  198. }
  199. /* Private key password */
  200. privkey_password = wpa_get_value (ssid,
  201. phase2 ? "private_key_passwd2" :
  202. "private_key_passwd");
  203. if (!privkey_password) {
  204. g_set_error (error, ifnet_plugin_error_quark (), 0,
  205. "Missing %s for EAP method '%s'.",
  206. phase2 ? "IEEE_8021X_INNER_PRIVATE_KEY_PASSWORD" :
  207. "IEEE_8021X_PRIVATE_KEY_PASSWORD", eap_method);
  208. goto done;
  209. }
  210. /* The private key itself */
  211. privkey = get_cert (ssid, phase2 ? "private_key2" : "private_key", basepath);
  212. if (!privkey) {
  213. g_set_error (error, ifnet_plugin_error_quark (), 0,
  214. "Missing %s for EAP method '%s'.",
  215. phase2 ? "IEEE_8021X_INNER_PRIVATE_KEY" :
  216. "IEEE_8021X_PRIVATE_KEY", eap_method);
  217. goto done;
  218. }
  219. if (phase2) {
  220. if (!nm_setting_802_1x_set_phase2_private_key (s_8021x,
  221. privkey,
  222. privkey_password,
  223. NM_SETTING_802_1X_CK_SCHEME_PATH,
  224. &privkey_format,
  225. error))
  226. goto done;
  227. } else {
  228. if (!nm_setting_802_1x_set_private_key (s_8021x,
  229. privkey,
  230. privkey_password,
  231. NM_SETTING_802_1X_CK_SCHEME_PATH,
  232. &privkey_format, error))
  233. goto done;
  234. }
  235. /* Only set the client certificate if the private key is not PKCS#12 format,
  236. * as NM (due to supplicant restrictions) requires. If the key was PKCS#12,
  237. * then nm_setting_802_1x_set_private_key() already set the client certificate
  238. * to the same value as the private key.
  239. */
  240. if (privkey_format == NM_SETTING_802_1X_CK_FORMAT_RAW_KEY
  241. || privkey_format == NM_SETTING_802_1X_CK_FORMAT_X509) {
  242. client_cert = get_cert (ssid, phase2 ? "client_cert2" : "client_cert", basepath);
  243. if (!client_cert) {
  244. g_set_error (error, ifnet_plugin_error_quark (), 0,
  245. "Missing %s for EAP method '%s'.",
  246. phase2 ? "IEEE_8021X_INNER_CLIENT_CERT" :
  247. "IEEE_8021X_CLIENT_CERT", eap_method);
  248. goto done;
  249. }
  250. if (phase2) {
  251. if (!nm_setting_802_1x_set_phase2_client_cert (s_8021x,
  252. client_cert,
  253. NM_SETTING_802_1X_CK_SCHEME_PATH,
  254. NULL,
  255. error))
  256. goto done;
  257. } else {
  258. if (!nm_setting_802_1x_set_client_cert (s_8021x,
  259. client_cert,
  260. NM_SETTING_802_1X_CK_SCHEME_PATH,
  261. NULL, error))
  262. goto done;
  263. }
  264. }
  265. success = TRUE;
  266. done:
  267. g_free (ca_cert);
  268. g_free (client_cert);
  269. g_free (privkey);
  270. return success;
  271. }
  272. static gboolean
  273. eap_peap_reader (const char *eap_method,
  274. const char *ssid,
  275. NMSetting8021x *s_8021x,
  276. gboolean phase2,
  277. const char *basepath,
  278. GError **error)
  279. {
  280. char *ca_cert = NULL;
  281. const char *inner_auth = NULL;
  282. const char *peapver = NULL;
  283. char **list = NULL, **iter, *lower;
  284. gboolean success = FALSE;
  285. ca_cert = get_cert (ssid, "ca_cert", basepath);
  286. if (ca_cert) {
  287. if (!nm_setting_802_1x_set_ca_cert (s_8021x,
  288. ca_cert,
  289. NM_SETTING_802_1X_CK_SCHEME_PATH,
  290. NULL, error))
  291. goto done;
  292. } else {
  293. PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: missing "
  294. "IEEE_8021X_CA_CERT for EAP method '%s'; this is"
  295. " insecure!", eap_method);
  296. }
  297. peapver = wpa_get_value (ssid, "phase1");
  298. /* peap version, default is automatic */
  299. if (peapver && strstr (peapver, "peapver")) {
  300. if (strstr (peapver, "peapver=0"))
  301. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPVER,
  302. "0", NULL);
  303. else if (strstr (peapver, "peapver=1"))
  304. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPVER,
  305. "1", NULL);
  306. else {
  307. g_set_error (error, ifnet_plugin_error_quark (), 0,
  308. "Unknown IEEE_8021X_PEAP_VERSION value '%s'",
  309. peapver);
  310. goto done;
  311. }
  312. }
  313. /* peaplabel */
  314. if (peapver && strstr (peapver, "peaplabel=1"))
  315. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE1_PEAPLABEL, "1",
  316. NULL);
  317. inner_auth = wpa_get_value (ssid, "phase2");
  318. if (!inner_auth) {
  319. g_set_error (error, ifnet_plugin_error_quark (), 0,
  320. "Missing IEEE_8021X_INNER_AUTH_METHODS.");
  321. goto done;
  322. }
  323. /* Handle options for the inner auth method */
  324. list = g_strsplit (inner_auth, " ", 0);
  325. for (iter = list; iter && *iter; iter++) {
  326. gchar *pos = NULL;
  327. if (!strlen (*iter))
  328. continue;
  329. if (!(pos = strstr (*iter, "MSCHAPV2"))
  330. || !(pos = strstr (*iter, "MD5"))
  331. || !(pos = strstr (*iter, "GTC"))) {
  332. if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error))
  333. goto done;
  334. } else if (!(pos = strstr (*iter, "TLS"))) {
  335. if (!eap_tls_reader (pos, ssid, s_8021x, TRUE, basepath, error))
  336. goto done;
  337. } else {
  338. g_set_error (error, ifnet_plugin_error_quark (), 0,
  339. "Unknown IEEE_8021X_INNER_AUTH_METHOD '%s'.",
  340. *iter);
  341. goto done;
  342. }
  343. pos = strchr (*iter, '=');
  344. pos++;
  345. lower = g_ascii_strdown (pos, -1);
  346. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, lower,
  347. NULL);
  348. g_free (lower);
  349. break;
  350. }
  351. if (!nm_setting_802_1x_get_phase2_auth (s_8021x)) {
  352. g_set_error (error, ifnet_plugin_error_quark (), 0,
  353. "No valid IEEE_8021X_INNER_AUTH_METHODS found.");
  354. goto done;
  355. }
  356. success = TRUE;
  357. done:
  358. g_free (ca_cert);
  359. if (list)
  360. g_strfreev (list);
  361. return success;
  362. }
  363. static gboolean
  364. eap_ttls_reader (const char *eap_method,
  365. const char *ssid,
  366. NMSetting8021x *s_8021x,
  367. gboolean phase2,
  368. const char *basepath,
  369. GError **error)
  370. {
  371. gboolean success = FALSE;
  372. const char *anon_ident = NULL;
  373. char *ca_cert = NULL;
  374. const char *tmp;
  375. char **list = NULL, **iter, *inner_auth = NULL;
  376. /* ca cert */
  377. ca_cert = get_cert (ssid, "ca_cert", basepath);
  378. if (ca_cert) {
  379. if (!nm_setting_802_1x_set_ca_cert (s_8021x,
  380. ca_cert,
  381. NM_SETTING_802_1X_CK_SCHEME_PATH,
  382. NULL, error))
  383. goto done;
  384. } else {
  385. PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: missing "
  386. "IEEE_8021X_CA_CERT for EAP method '%s'; this is"
  387. " insecure!", eap_method);
  388. }
  389. /* anonymous indentity for tls */
  390. anon_ident = wpa_get_value (ssid, "anonymous_identity");
  391. if (anon_ident && strlen (anon_ident))
  392. g_object_set (s_8021x, NM_SETTING_802_1X_ANONYMOUS_IDENTITY,
  393. anon_ident, NULL);
  394. tmp = wpa_get_value (ssid, "phase2");
  395. if (!tmp) {
  396. g_set_error (error, ifnet_plugin_error_quark (), 0,
  397. "Missing IEEE_8021X_INNER_AUTH_METHODS.");
  398. goto done;
  399. }
  400. /* Handle options for the inner auth method */
  401. inner_auth = g_ascii_strdown (tmp, -1);
  402. list = g_strsplit (inner_auth, " ", 0);
  403. for (iter = list; iter && *iter; iter++) {
  404. gchar *pos = NULL;
  405. if (!strlen (*iter))
  406. continue;
  407. if ((pos = strstr (*iter, "mschapv2")) != NULL
  408. || (pos = strstr (*iter, "mschap")) != NULL
  409. || (pos = strstr (*iter, "pap")) != NULL
  410. || (pos = strstr (*iter, "chap")) != NULL) {
  411. if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error))
  412. goto done;
  413. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTH,
  414. pos, NULL);
  415. } else if ((pos = strstr (*iter, "tls")) != NULL) {
  416. if (!eap_tls_reader (pos, ssid, s_8021x, TRUE, basepath, error))
  417. goto done;
  418. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTHEAP,
  419. "tls", NULL);
  420. } else if ((pos = strstr (*iter, "mschapv2")) != NULL
  421. || (pos = strstr (*iter, "md5")) != NULL) {
  422. if (!eap_simple_reader (pos, ssid, s_8021x, TRUE, basepath, error)) {
  423. PLUGIN_WARN (IFNET_PLUGIN_NAME, "SIMPLE ERROR");
  424. goto done;
  425. }
  426. g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTHEAP,
  427. pos, NULL);
  428. } else {
  429. g_set_error (error, ifnet_plugin_error_quark (), 0,
  430. "Unknown IEEE_8021X_INNER_AUTH_METHOD '%s'.",
  431. *iter);
  432. goto done;
  433. }
  434. break;
  435. }
  436. success = TRUE;
  437. done:
  438. g_free (ca_cert);
  439. if (list)
  440. g_strfreev (list);
  441. g_free (inner_auth);
  442. return success;
  443. }
  444. /* type is already decided by net_parser, this function is just used to
  445. * doing tansformation*/
  446. static const gchar *
  447. guess_connection_type (const char *conn_name)
  448. {
  449. const gchar *type = ifnet_get_data (conn_name, "type");
  450. const gchar *ret_type = NULL;
  451. if (!g_strcmp0 (type, "ppp"))
  452. ret_type = NM_SETTING_PPPOE_SETTING_NAME;
  453. if (!g_strcmp0 (type, "wireless"))
  454. ret_type = NM_SETTING_WIRELESS_SETTING_NAME;
  455. if (!ret_type)
  456. ret_type = NM_SETTING_WIRED_SETTING_NAME;
  457. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  458. "guessed connection type (%s) = %s", conn_name, ret_type);
  459. return ret_type;
  460. }
  461. /* Reading mac address for setting connection option.
  462. * Unmanaged device mac address is required by NetworkManager*/
  463. static gboolean
  464. read_mac_address (const char *conn_name, GByteArray **array, GError **error)
  465. {
  466. const char *value = ifnet_get_data (conn_name, "mac");
  467. if (!value || !strlen (value))
  468. return TRUE;
  469. *array = nm_utils_hwaddr_atoba (value, ARPHRD_ETHER);
  470. if (!*array) {
  471. g_set_error (error, ifnet_plugin_error_quark (), 0,
  472. "The MAC address '%s' was invalid.", value);
  473. return FALSE;
  474. }
  475. return TRUE;
  476. }
  477. static void
  478. make_wired_connection_setting (NMConnection *connection,
  479. const char *conn_name,
  480. GError **error)
  481. {
  482. GByteArray *mac = NULL;
  483. NMSettingWired *s_wired = NULL;
  484. const char *value = NULL;
  485. s_wired = NM_SETTING_WIRED (nm_setting_wired_new ());
  486. /* mtu_xxx */
  487. value = ifnet_get_data (conn_name, "mtu");
  488. if (value) {
  489. long int mtu;
  490. errno = 0;
  491. mtu = strtol (value, NULL, 10);
  492. if (errno || mtu < 0 || mtu > 65535) {
  493. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  494. " warning: invalid MTU '%s' for %s",
  495. value, conn_name);
  496. } else
  497. g_object_set (s_wired, NM_SETTING_WIRED_MTU,
  498. (guint32) mtu, NULL);
  499. }
  500. if (read_mac_address (conn_name, &mac, error)) {
  501. if (mac) {
  502. g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS,
  503. mac, NULL);
  504. g_byte_array_free (mac, TRUE);
  505. }
  506. } else {
  507. g_object_unref (s_wired);
  508. s_wired = NULL;
  509. }
  510. if (s_wired)
  511. nm_connection_add_setting (connection, NM_SETTING (s_wired));
  512. }
  513. /* add NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME,
  514. * NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID in future*/
  515. static void
  516. make_ip4_setting (NMConnection *connection,
  517. const char *conn_name,
  518. GError **error)
  519. {
  520. NMSettingIP4Config *ip4_setting =
  521. NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ());
  522. const char *value, *method;
  523. gboolean is_static_block = is_static_ip4 (conn_name);
  524. ip_block *iblock = NULL;
  525. /* set dhcp options (dhcp_xxx) */
  526. value = ifnet_get_data (conn_name, "dhcp");
  527. g_object_set (ip4_setting, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, value
  528. && strstr (value, "nodns") ? TRUE : FALSE,
  529. NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, value
  530. && strstr (value, "nogateway") ? TRUE : FALSE, NULL);
  531. if (!is_static_block) {
  532. method = ifnet_get_data (conn_name, "config");
  533. if (!method){
  534. g_set_error (error, ifnet_plugin_error_quark (), 0,
  535. "Unknown config for %s", conn_name);
  536. g_object_unref (ip4_setting);
  537. return;
  538. }
  539. if (strstr (method, "dhcp"))
  540. g_object_set (ip4_setting,
  541. NM_SETTING_IP4_CONFIG_METHOD,
  542. NM_SETTING_IP4_CONFIG_METHOD_AUTO,
  543. NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
  544. else if (strstr (method, "autoip")) {
  545. g_object_set (ip4_setting,
  546. NM_SETTING_IP4_CONFIG_METHOD,
  547. NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
  548. NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
  549. nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
  550. return;
  551. } else if (strstr (method, "shared")) {
  552. g_object_set (ip4_setting,
  553. NM_SETTING_IP4_CONFIG_METHOD,
  554. NM_SETTING_IP4_CONFIG_METHOD_SHARED,
  555. NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL);
  556. nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
  557. return;
  558. } else {
  559. g_set_error (error, ifnet_plugin_error_quark (), 0,
  560. "Unknown config for %s", conn_name);
  561. g_object_unref (ip4_setting);
  562. return;
  563. }
  564. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using %s method for %s",
  565. method, conn_name);
  566. }else {
  567. iblock = convert_ip4_config_block (conn_name);
  568. if (!iblock) {
  569. g_set_error (error, ifnet_plugin_error_quark (), 0,
  570. "Ifnet plugin: can't aquire ip configuration for %s",
  571. conn_name);
  572. g_object_unref (ip4_setting);
  573. return;
  574. }
  575. /************** add all ip settings to the connection**********/
  576. while (iblock) {
  577. ip_block *current_iblock;
  578. NMIP4Address *ip4_addr = nm_ip4_address_new ();
  579. nm_ip4_address_set_address (ip4_addr, iblock->ip);
  580. nm_ip4_address_set_prefix (ip4_addr,
  581. nm_utils_ip4_netmask_to_prefix
  582. (iblock->netmask));
  583. /* currently all the IPs has the same gateway */
  584. nm_ip4_address_set_gateway (ip4_addr, iblock->gateway);
  585. if (iblock->gateway)
  586. g_object_set (ip4_setting,
  587. NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES,
  588. TRUE, NULL);
  589. if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr))
  590. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  591. "ignoring duplicate IP4 address");
  592. nm_ip4_address_unref (ip4_addr);
  593. current_iblock = iblock;
  594. iblock = iblock->next;
  595. destroy_ip_block (current_iblock);
  596. }
  597. g_object_set (ip4_setting,
  598. NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
  599. NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, !has_default_ip4_route (conn_name),
  600. NULL);
  601. }
  602. /* add dhcp hostname and client id */
  603. if (!is_static_block && strstr (method, "dhcp")) {
  604. gchar *dhcp_hostname, *client_id;
  605. get_dhcp_hostname_and_client_id (&dhcp_hostname, &client_id);
  606. if (dhcp_hostname) {
  607. g_object_set (ip4_setting,
  608. NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME,
  609. dhcp_hostname, NULL);
  610. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "DHCP hostname: %s",
  611. dhcp_hostname);
  612. g_free (dhcp_hostname);
  613. }
  614. if (client_id) {
  615. g_object_set (ip4_setting,
  616. NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID,
  617. client_id, NULL);
  618. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "DHCP client id: %s",
  619. client_id);
  620. g_free (client_id);
  621. }
  622. }
  623. /* add all IPv4 dns servers, IPv6 servers will be ignored */
  624. set_ip4_dns_servers (ip4_setting, conn_name);
  625. /* DNS searches */
  626. value = ifnet_get_data (conn_name, "dns_search");
  627. if (value) {
  628. char *stripped = g_strdup (value);
  629. char **searches = NULL;
  630. strip_string (stripped, '"');
  631. searches = g_strsplit (stripped, " ", 0);
  632. if (searches) {
  633. char **item;
  634. for (item = searches; *item; item++) {
  635. if (strlen (*item)) {
  636. if (!nm_setting_ip4_config_add_dns_search (ip4_setting, *item))
  637. PLUGIN_WARN
  638. (IFNET_PLUGIN_NAME,
  639. " warning: duplicate DNS domain '%s'",
  640. *item);
  641. }
  642. }
  643. g_strfreev (searches);
  644. }
  645. }
  646. /* static routes */
  647. iblock = convert_ip4_routes_block (conn_name);
  648. while (iblock) {
  649. ip_block *current_iblock = iblock;
  650. const char *metric_str;
  651. char *stripped;
  652. long int metric;
  653. NMIP4Route *route = nm_ip4_route_new ();
  654. nm_ip4_route_set_dest (route, iblock->ip);
  655. nm_ip4_route_set_next_hop (route, iblock->gateway);
  656. nm_ip4_route_set_prefix (route,
  657. nm_utils_ip4_netmask_to_prefix
  658. (iblock->netmask));
  659. if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
  660. metric = strtol (metric_str, NULL, 10);
  661. nm_ip4_route_set_metric (route, (guint32) metric);
  662. } else {
  663. metric_str = ifnet_get_global_data ("metric");
  664. if (metric_str) {
  665. stripped = g_strdup (metric_str);
  666. strip_string (stripped, '"');
  667. metric = strtol (metric_str, NULL, 10);
  668. nm_ip4_route_set_metric (route,
  669. (guint32) metric);
  670. g_free (stripped);
  671. }
  672. }
  673. if (!nm_setting_ip4_config_add_route (ip4_setting, route))
  674. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  675. "warning: duplicate IP4 route");
  676. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  677. "new IP4 route:%d\n", iblock->ip);
  678. nm_ip4_route_unref (route);
  679. current_iblock = iblock;
  680. iblock = iblock->next;
  681. destroy_ip_block (current_iblock);
  682. }
  683. /* Finally add setting to connection */
  684. nm_connection_add_setting (connection, NM_SETTING (ip4_setting));
  685. }
  686. static void
  687. make_ip6_setting (NMConnection *connection,
  688. const char *conn_name,
  689. GError **error)
  690. {
  691. NMSettingIP6Config *s_ip6 = NULL;
  692. gboolean is_static_block = is_static_ip6 (conn_name);
  693. // used to disable IPv6
  694. gboolean ipv6_enabled = FALSE;
  695. gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
  696. const char *value;
  697. ip6_block *iblock;
  698. gboolean never_default = !has_default_ip6_route (conn_name);
  699. s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
  700. if (!s_ip6) {
  701. g_set_error (error, ifnet_plugin_error_quark (), 0,
  702. "Could not allocate IP6 setting");
  703. return;
  704. }
  705. value = ifnet_get_data (conn_name, "enable_ipv6");
  706. if (value && is_true (value))
  707. ipv6_enabled = TRUE;
  708. //FIXME Handle other methods that NM supports in future
  709. // Currently only Manual and DHCP are supported
  710. if (!ipv6_enabled) {
  711. g_object_set (s_ip6,
  712. NM_SETTING_IP6_CONFIG_METHOD,
  713. NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL);
  714. goto done;
  715. } else if (!is_static_block) {
  716. // config_eth* contains "dhcp6"
  717. method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
  718. never_default = FALSE;
  719. }
  720. // else if (!has_ip6_address(conn_name))
  721. // doesn't have "dhcp6" && doesn't have any ipv6 address
  722. // method = NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL;
  723. else
  724. // doesn't have "dhcp6" && has at least one ipv6 address
  725. method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
  726. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "IPv6 for %s enabled, using %s",
  727. conn_name, method);
  728. g_object_set (s_ip6,
  729. NM_SETTING_IP6_CONFIG_METHOD, method,
  730. NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, FALSE,
  731. NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, FALSE,
  732. NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, never_default, NULL);
  733. /* Make manual settings */
  734. if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) {
  735. ip6_block *current_iblock;
  736. iblock = convert_ip6_config_block (conn_name);
  737. if (!iblock) {
  738. g_set_error (error, ifnet_plugin_error_quark (), 0,
  739. "Ifnet plugin: can't aquire ip6 configuration for %s",
  740. conn_name);
  741. goto error;
  742. }
  743. /* add all IPv6 addresses */
  744. while (iblock) {
  745. NMIP6Address *ip6_addr = nm_ip6_address_new ();
  746. nm_ip6_address_set_address (ip6_addr, iblock->ip);
  747. nm_ip6_address_set_prefix (ip6_addr, iblock->prefix);
  748. if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) {
  749. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  750. "ipv6 addresses count: %d",
  751. nm_setting_ip6_config_get_num_addresses
  752. (s_ip6));
  753. } else {
  754. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  755. "ignoring duplicate IP4 address");
  756. }
  757. nm_ip6_address_unref (ip6_addr);
  758. current_iblock = iblock;
  759. iblock = iblock->next;
  760. destroy_ip6_block (current_iblock);
  761. }
  762. } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) {
  763. /* - autoconf or DHCPv6 stuff goes here */
  764. }
  765. // DNS Servers, set NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS TRUE here
  766. set_ip6_dns_servers (s_ip6, conn_name);
  767. /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIP4Config */
  768. // Add routes
  769. iblock = convert_ip6_routes_block (conn_name);
  770. if (iblock)
  771. g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES,
  772. TRUE, NULL);
  773. /* Add all IPv6 routes */
  774. while (iblock) {
  775. ip6_block *current_iblock = iblock;
  776. const char *metric_str;
  777. char *stripped;
  778. long int metric = 1;
  779. NMIP6Route *route = nm_ip6_route_new ();
  780. nm_ip6_route_set_dest (route, iblock->ip);
  781. nm_ip6_route_set_next_hop (route, iblock->next_hop);
  782. nm_ip6_route_set_prefix (route, iblock->prefix);
  783. /* metric is not per routes configuration right now
  784. * global metric is also supported (metric="x") */
  785. if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
  786. metric = strtol (metric_str, NULL, 10);
  787. nm_ip6_route_set_metric (route, (guint32) metric);
  788. } else {
  789. metric_str = ifnet_get_global_data ("metric");
  790. if (metric_str) {
  791. stripped = g_strdup (metric_str);
  792. strip_string (stripped, '"');
  793. metric = strtol (metric_str, NULL, 10);
  794. nm_ip6_route_set_metric (route,
  795. (guint32) metric);
  796. g_free (stripped);
  797. } else
  798. nm_ip6_route_set_metric (route, (guint32) 1);
  799. }
  800. if (!nm_setting_ip6_config_add_route (s_ip6, route))
  801. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  802. " warning: duplicate IP6 route");
  803. PLUGIN_PRINT (IFNET_PLUGIN_NAME, " info: new IP6 route");
  804. nm_ip6_route_unref (route);
  805. current_iblock = iblock;
  806. iblock = iblock->next;
  807. destroy_ip6_block (current_iblock);
  808. }
  809. done:
  810. nm_connection_add_setting (connection, NM_SETTING (s_ip6));
  811. return;
  812. error:
  813. g_object_unref (s_ip6);
  814. PLUGIN_WARN (IFNET_PLUGIN_NAME, " warning: Ignore IPv6 for %s",
  815. conn_name);
  816. return;
  817. }
  818. static NMSetting *
  819. make_wireless_connection_setting (const char *conn_name,
  820. NMSetting8021x **s_8021x,
  821. GError **error)
  822. {
  823. GByteArray *array, *mac = NULL;
  824. NMSettingWireless *wireless_setting = NULL;
  825. gboolean adhoc = FALSE;
  826. const char *value;
  827. const char *type;
  828. /* PPP over WIFI is not supported yet */
  829. g_return_val_if_fail (conn_name != NULL
  830. && strcmp (ifnet_get_data (conn_name, "type"),
  831. "ppp") != 0, NULL);
  832. type = ifnet_get_data (conn_name, "type");
  833. if (strcmp (type, "ppp") == 0) {
  834. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  835. "PPP over WIFI is not supported yet");
  836. return NULL;
  837. }
  838. wireless_setting = NM_SETTING_WIRELESS (nm_setting_wireless_new ());
  839. if (read_mac_address (conn_name, &mac, error)) {
  840. if (mac) {
  841. g_object_set (wireless_setting,
  842. NM_SETTING_WIRELESS_MAC_ADDRESS, mac,
  843. NULL);
  844. g_byte_array_free (mac, TRUE);
  845. }
  846. } else {
  847. g_object_unref (wireless_setting);
  848. return NULL;
  849. }
  850. /* handle ssid (hex and ascii) */
  851. if (conn_name) {
  852. gsize ssid_len = 0, value_len = strlen (conn_name);
  853. const char *p;
  854. char *tmp, *converted = NULL;
  855. ssid_len = value_len;
  856. if ((value_len > 2) && (g_str_has_prefix (conn_name, "0x"))) {
  857. /* Hex representation */
  858. if (value_len % 2) {
  859. g_set_error (error, ifnet_plugin_error_quark (),
  860. 0,
  861. "Invalid SSID '%s' size (looks like hex but length not multiple of 2)",
  862. conn_name);
  863. goto error;
  864. }
  865. // ignore "0x"
  866. p = conn_name + 2;
  867. if (!is_hex (p)) {
  868. g_set_error (error,
  869. ifnet_plugin_error_quark (),
  870. 0,
  871. "Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)",
  872. conn_name, *p);
  873. goto error;
  874. }
  875. tmp = utils_hexstr2bin (p, value_len - 2);
  876. ssid_len = (value_len - 2) / 2;
  877. converted = g_malloc0 (ssid_len + 1);
  878. memcpy (converted, tmp, ssid_len);
  879. g_free (tmp);
  880. }
  881. if (ssid_len > 32 || ssid_len == 0) {
  882. g_set_error (error, ifnet_plugin_error_quark (), 0,
  883. "Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)",
  884. conn_name, ssid_len);
  885. goto error;
  886. }
  887. array = g_byte_array_sized_new (ssid_len);
  888. g_byte_array_append (array, (const guint8 *) (converted ? converted : conn_name), ssid_len);
  889. g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, array, NULL);
  890. g_byte_array_free (array, TRUE);
  891. g_free (converted);
  892. } else {
  893. g_set_error (error, ifnet_plugin_error_quark (), 0,
  894. "Missing SSID");
  895. goto error;
  896. }
  897. /* mode=0: infrastructure
  898. * mode=1: adhoc */
  899. value = wpa_get_value (conn_name, "mode");
  900. if (value)
  901. adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
  902. if (exist_ssid (conn_name)) {
  903. const char *mode = adhoc ? "adhoc" : "infrastructure";
  904. g_object_set (wireless_setting, NM_SETTING_WIRELESS_MODE, mode,
  905. NULL);
  906. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "Using mode: %s", mode);
  907. }
  908. /* BSSID setting */
  909. value = wpa_get_value (conn_name, "bssid");
  910. if (value) {
  911. GByteArray *bssid;
  912. bssid = nm_utils_hwaddr_atoba (value, ARPHRD_ETHER);
  913. if (!bssid) {
  914. g_set_error (error, ifnet_plugin_error_quark (), 0,
  915. "Invalid BSSID '%s'", value);
  916. goto error;
  917. }
  918. g_object_set (wireless_setting, NM_SETTING_WIRELESS_BSSID,
  919. bssid, NULL);
  920. g_byte_array_free (bssid, TRUE);
  921. }
  922. /* mtu_ssid="xx" */
  923. value = ifnet_get_data (conn_name, "mtu");
  924. if (value) {
  925. long int mtu;
  926. errno = 0;
  927. mtu = strtol (value, NULL, 10);
  928. if (errno || mtu < 0 || mtu > 50000) {
  929. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  930. " warning: invalid MTU '%s' for %s",
  931. value, conn_name);
  932. } else
  933. g_object_set (wireless_setting, NM_SETTING_WIRELESS_MTU,
  934. (guint32) mtu, NULL);
  935. }
  936. PLUGIN_PRINT (IFNET_PLUGIN_NAME, "wireless_setting added for %s",
  937. conn_name);
  938. return NM_SETTING (wireless_setting);
  939. error:
  940. if (wireless_setting)
  941. g_object_unref (wireless_setting);
  942. return NULL;
  943. }
  944. static NMSettingWirelessSecurity *
  945. make_leap_setting (const char *ssid, GError **error)
  946. {
  947. NMSettingWirelessSecurity *wsec;
  948. const char *value;
  949. wsec =
  950. NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
  951. value = wpa_get_value (ssid, "key_mgmt");
  952. if (!value || strcmp (value, "IEEE8021X"))
  953. goto error; /* Not LEAP */
  954. value = wpa_get_value (ssid, "eap");
  955. if (!value || strcasecmp (value, "LEAP"))
  956. goto error; /* Not LEAP */
  957. value = wpa_get_value (ssid, "password");
  958. if (value && strlen (value))
  959. g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD,
  960. value, NULL);
  961. value = wpa_get_value (ssid, "identity");
  962. if (!value || !strlen (value)) {
  963. g_set_error (error, ifnet_plugin_error_quark (), 0,
  964. "Missing LEAP identity");
  965. goto error;
  966. }
  967. g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, value,
  968. NULL);
  969. g_object_set (wsec,
  970. NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x",
  971. NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", NULL);
  972. return wsec;
  973. error:
  974. if (wsec)
  975. g_object_unref (wsec);
  976. return NULL;
  977. }
  978. static gboolean
  979. add_one_wep_key (const char *ssid,
  980. const char *key,
  981. int key_idx,
  982. NMSettingWirelessSecurity *s_wsec,
  983. GError **error)
  984. {
  985. const char *value;
  986. char *converted = NULL;
  987. gboolean success = FALSE;
  988. g_return_val_if_fail (ssid != NULL, FALSE);
  989. g_return_val_if_fail (key != NULL, FALSE);
  990. g_return_val_if_fail (key_idx >= 0 && key_idx <= 3, FALSE);
  991. g_return_val_if_fail (s_wsec != NULL, FALSE);
  992. value = wpa_get_value (ssid, key);
  993. if (!value)
  994. return TRUE;
  995. /* Validate keys */
  996. if (strlen (value) == 10 || strlen (value) == 26) {
  997. /* Hexadecimal WEP key */
  998. if (!is_hex (value)) {
  999. g_set_error (error, ifnet_plugin_error_quark (),
  1000. 0, "Invalid hexadecimal WEP key.");
  1001. goto out;
  1002. }
  1003. converted = g_strdup (value);
  1004. } else if (value[0] == '"'
  1005. && (strlen (value) == 7 || strlen (value) == 15)) {
  1006. /* ASCII passphrase */
  1007. char *tmp = g_strdup (value);
  1008. char *p = strip_string (tmp, '"');
  1009. if (!is_ascii (p)) {
  1010. g_set_error (error, ifnet_plugin_error_quark (),
  1011. 0, "Invalid ASCII WEP passphrase.");
  1012. g_free (tmp);
  1013. goto out;
  1014. }
  1015. converted = utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2);
  1016. g_free (tmp);
  1017. } else {
  1018. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1019. "Invalid WEP key length. Key: %s", value);
  1020. goto out;
  1021. }
  1022. if (converted) {
  1023. nm_setting_wireless_security_set_wep_key (s_wsec, key_idx, converted);
  1024. g_free (converted);
  1025. success = TRUE;
  1026. }
  1027. out:
  1028. return success;
  1029. }
  1030. static gboolean
  1031. add_wep_keys (const char *ssid,
  1032. NMSettingWirelessSecurity *s_wsec,
  1033. GError **error)
  1034. {
  1035. if (!add_one_wep_key (ssid, "wep_key0", 0, s_wsec, error))
  1036. return FALSE;
  1037. if (!add_one_wep_key (ssid, "wep_key1", 1, s_wsec, error))
  1038. return FALSE;
  1039. if (!add_one_wep_key (ssid, "wep_key2", 2, s_wsec, error))
  1040. return FALSE;
  1041. if (!add_one_wep_key (ssid, "wep_key3", 3, s_wsec, error))
  1042. return FALSE;
  1043. return TRUE;
  1044. }
  1045. static NMSettingWirelessSecurity *
  1046. make_wep_setting (const char *ssid, GError **error)
  1047. {
  1048. const char *auth_alg, *value;
  1049. int default_key_idx = 0;
  1050. NMSettingWirelessSecurity *s_wireless_sec;
  1051. s_wireless_sec =
  1052. NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
  1053. g_object_set (s_wireless_sec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
  1054. "none", NULL);
  1055. /* default key index */
  1056. value = wpa_get_value (ssid, "wep_tx_keyidx");
  1057. if (value) {
  1058. default_key_idx = atoi (value);
  1059. if (default_key_idx >= 0 && default_key_idx <= 3) {
  1060. g_object_set (s_wireless_sec,
  1061. NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX,
  1062. default_key_idx, NULL);
  1063. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  1064. "Default key index: %d", default_key_idx);
  1065. } else {
  1066. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1067. "Invalid default WEP key '%s'", value);
  1068. goto error;
  1069. }
  1070. }
  1071. if (!add_wep_keys (ssid, s_wireless_sec, error))
  1072. goto error;
  1073. /* If there's a default key, ensure that key exists */
  1074. if ((default_key_idx == 1)
  1075. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 1)) {
  1076. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1077. "Default WEP key index was 2, but no valid KEY2 exists.");
  1078. goto error;
  1079. } else if ((default_key_idx == 2)
  1080. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec,
  1081. 2)) {
  1082. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1083. "Default WEP key index was 3, but no valid KEY3 exists.");
  1084. goto error;
  1085. } else if ((default_key_idx == 3)
  1086. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec,
  1087. 3)) {
  1088. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1089. "Default WEP key index was 4, but no valid KEY4 exists.");
  1090. goto error;
  1091. }
  1092. /* authentication algorithms */
  1093. auth_alg = wpa_get_value (ssid, "auth_alg");
  1094. if (auth_alg) {
  1095. if (strcmp (auth_alg, "OPEN") == 0) {
  1096. g_object_set (s_wireless_sec,
  1097. NM_SETTING_WIRELESS_SECURITY_AUTH_ALG,
  1098. "open", NULL);
  1099. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  1100. "WEP: Use open system authentication");
  1101. } else if (strcmp (auth_alg, "SHARED") == 0) {
  1102. g_object_set (s_wireless_sec,
  1103. NM_SETTING_WIRELESS_SECURITY_AUTH_ALG,
  1104. "shared", NULL);
  1105. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  1106. "WEP: Use shared system authentication");
  1107. } else {
  1108. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1109. "Invalid WEP authentication algorithm '%s'",
  1110. auth_alg);
  1111. goto error;
  1112. }
  1113. }
  1114. if (!nm_setting_wireless_security_get_wep_key (s_wireless_sec, 0)
  1115. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 1)
  1116. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 2)
  1117. && !nm_setting_wireless_security_get_wep_key (s_wireless_sec, 3)
  1118. && !nm_setting_wireless_security_get_wep_tx_keyidx (s_wireless_sec)) {
  1119. if (auth_alg && !strcmp (auth_alg, "shared")) {
  1120. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1121. "WEP Shared Key authentication is invalid for "
  1122. "unencrypted connections.");
  1123. goto error;
  1124. }
  1125. /* Unencrypted */
  1126. g_object_unref (s_wireless_sec);
  1127. s_wireless_sec = NULL;
  1128. }
  1129. return s_wireless_sec;
  1130. error:
  1131. if (s_wireless_sec)
  1132. g_object_unref (s_wireless_sec);
  1133. return NULL;
  1134. }
  1135. static char *
  1136. parse_wpa_psk (const char *psk, GError **error)
  1137. {
  1138. char *hashed = NULL;
  1139. gboolean quoted = FALSE;
  1140. if (!psk) {
  1141. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1142. "Missing WPA_PSK for WPA-PSK key management");
  1143. return NULL;
  1144. }
  1145. /* Passphrase must be between 10 and 66 characters in length becuase WPA
  1146. * hex keys are exactly 64 characters (no quoting), and WPA passphrases
  1147. * are between 8 and 63 characters (inclusive), plus optional quoting if
  1148. * the passphrase contains spaces.
  1149. */
  1150. if (psk[0] == '"' && psk[strlen (psk) - 1] == '"')
  1151. quoted = TRUE;
  1152. if (!quoted && (strlen (psk) == 64)) {
  1153. /* Verify the hex PSK; 64 digits */
  1154. if (!is_hex (psk)) {
  1155. g_set_error (error, ifnet_plugin_error_quark (),
  1156. 0,
  1157. "Invalid WPA_PSK (contains non-hexadecimal characters)");
  1158. goto out;
  1159. }
  1160. hashed = g_strdup (psk);
  1161. } else {
  1162. char *stripped = g_strdup (psk);
  1163. strip_string (stripped, '"');
  1164. /* Length check */
  1165. if (strlen (stripped) < 8 || strlen (stripped) > 63) {
  1166. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1167. "Invalid WPA_PSK (passphrases must be between "
  1168. "8 and 63 characters long (inclusive))");
  1169. g_free (stripped);
  1170. goto out;
  1171. }
  1172. hashed = g_strdup (stripped);
  1173. g_free (stripped);
  1174. }
  1175. if (!hashed) {
  1176. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1177. "Invalid WPA_PSK (doesn't look like a passphrase or hex key)");
  1178. goto out;
  1179. }
  1180. out:
  1181. return hashed;
  1182. }
  1183. static gboolean
  1184. fill_wpa_ciphers (const char *ssid,
  1185. NMSettingWirelessSecurity *wsec,
  1186. gboolean group,
  1187. gboolean adhoc)
  1188. {
  1189. const char *value;
  1190. char **list = NULL, **iter;
  1191. int i = 0;
  1192. value = wpa_get_value (ssid, group ? "group" : "pairwise");
  1193. if (!value)
  1194. return TRUE;
  1195. list = g_strsplit_set (value, " ", 0);
  1196. for (iter = list; iter && *iter; iter++, i++) {
  1197. /* Ad-Hoc configurations cannot have pairwise ciphers, and can only
  1198. * have one group cipher. Ignore any additional group ciphers and
  1199. * any pairwise ciphers specified.
  1200. */
  1201. if (adhoc) {
  1202. if (group && (i > 0)) {
  1203. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1204. " warning: ignoring group cipher '%s' (only one group cipher allowed in Ad-Hoc mode)",
  1205. *iter);
  1206. continue;
  1207. } else if (!group) {
  1208. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1209. " warning: ignoring pairwise cipher '%s' (pairwise not used in Ad-Hoc mode)",
  1210. *iter);
  1211. continue;
  1212. }
  1213. }
  1214. if (!strcmp (*iter, "CCMP")) {
  1215. if (group)
  1216. nm_setting_wireless_security_add_group (wsec,
  1217. "ccmp");
  1218. else
  1219. nm_setting_wireless_security_add_pairwise (wsec,
  1220. "ccmp");
  1221. } else if (!strcmp (*iter, "TKIP")) {
  1222. if (group)
  1223. nm_setting_wireless_security_add_group (wsec,
  1224. "tkip");
  1225. else
  1226. nm_setting_wireless_security_add_pairwise (wsec,
  1227. "tkip");
  1228. } else if (group && !strcmp (*iter, "WEP104"))
  1229. nm_setting_wireless_security_add_group (wsec, "wep104");
  1230. else if (group && !strcmp (*iter, "WEP40"))
  1231. nm_setting_wireless_security_add_group (wsec, "wep40");
  1232. else {
  1233. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1234. " warning: ignoring invalid %s cipher '%s'",
  1235. group ? "CIPHER_GROUP" : "CIPHER_PAIRWISE",
  1236. *iter);
  1237. }
  1238. }
  1239. if (list)
  1240. g_strfreev (list);
  1241. return TRUE;
  1242. }
  1243. static NMSetting8021x *
  1244. fill_8021x (const char *ssid,
  1245. const char *key_mgmt,
  1246. gboolean wifi,
  1247. const char *basepath,
  1248. GError **error)
  1249. {
  1250. NMSetting8021x *s_8021x;
  1251. const char *value;
  1252. char **list, **iter;
  1253. value = wpa_get_value (ssid, "eap");
  1254. if (!value) {
  1255. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1256. "Missing IEEE_8021X_EAP_METHODS for key management '%s'",
  1257. key_mgmt);
  1258. return NULL;
  1259. }
  1260. list = g_strsplit (value, " ", 0);
  1261. s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
  1262. /* Validate and handle each EAP method */
  1263. for (iter = list; iter && *iter; iter++) {
  1264. EAPReader *eap = &eap_readers[0];
  1265. gboolean found = FALSE;
  1266. char *lower = NULL;
  1267. lower = g_ascii_strdown (*iter, -1);
  1268. while (eap->method && !found) {
  1269. if (strcmp (eap->method, lower))
  1270. goto next;
  1271. /* Some EAP methods don't provide keying material, thus they
  1272. * cannot be used with WiFi unless they are an inner method
  1273. * used with TTLS or PEAP or whatever.
  1274. */
  1275. if (wifi && eap->wifi_phase2_only) {
  1276. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1277. " warning: ignored invalid "
  1278. "IEEE_8021X_EAP_METHOD '%s'; not allowed for wifi.",
  1279. lower);
  1280. goto next;
  1281. }
  1282. /* Parse EAP method specific options */
  1283. if (!(*eap->reader) (lower, ssid, s_8021x, FALSE, basepath, error)) {
  1284. g_free (lower);
  1285. goto error;
  1286. }
  1287. nm_setting_802_1x_add_eap_method (s_8021x, lower);
  1288. found = TRUE;
  1289. next:
  1290. eap++;
  1291. }
  1292. if (!found) {
  1293. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1294. " warning: ignored unknown"
  1295. "IEEE_8021X_EAP_METHOD '%s'.", lower);
  1296. }
  1297. g_free (lower);
  1298. }
  1299. g_strfreev (list);
  1300. if (nm_setting_802_1x_get_num_eap_methods (s_8021x) == 0) {
  1301. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1302. "No valid EAP methods found in IEEE_8021X_EAP_METHODS.");
  1303. goto error;
  1304. }
  1305. return s_8021x;
  1306. error:
  1307. g_object_unref (s_8021x);
  1308. return NULL;
  1309. }
  1310. static NMSettingWirelessSecurity *
  1311. make_wpa_setting (const char *ssid,
  1312. const char *basepath,
  1313. NMSetting8021x **s_8021x,
  1314. GError **error)
  1315. {
  1316. NMSettingWirelessSecurity *wsec;
  1317. const char *value;
  1318. char *lower;
  1319. gboolean adhoc = FALSE;
  1320. if (!exist_ssid (ssid)) {
  1321. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1322. "No security info found for ssid: %s", ssid);
  1323. return NULL;
  1324. }
  1325. wsec = NM_SETTING_WIRELESS_SECURITY (nm_setting_wireless_security_new ());
  1326. /* mode=1: adhoc
  1327. * mode=0: infrastructure */
  1328. value = wpa_get_value (ssid, "mode");
  1329. if (value)
  1330. adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
  1331. value = wpa_get_value (ssid, "key_mgmt");
  1332. /* Not WPA or Dynamic WEP */
  1333. if (!value)
  1334. goto error;
  1335. if (strcmp (value, "WPA-PSK") && strcmp (value, "WPA-EAP"))
  1336. goto error;
  1337. /* Pairwise and Group ciphers */
  1338. fill_wpa_ciphers (ssid, wsec, FALSE, adhoc);
  1339. fill_wpa_ciphers (ssid, wsec, TRUE, adhoc);
  1340. /* WPA and/or RSN */
  1341. if (adhoc) {
  1342. /* Ad-Hoc mode only supports WPA proto for now */
  1343. nm_setting_wireless_security_add_proto (wsec, "wpa");
  1344. } else {
  1345. nm_setting_wireless_security_add_proto (wsec, "wpa");
  1346. nm_setting_wireless_security_add_proto (wsec, "rsn");
  1347. }
  1348. if (!strcmp (value, "WPA-PSK")) {
  1349. char *psk = parse_wpa_psk (wpa_get_value (ssid, "psk"), error);
  1350. if (!psk)
  1351. goto error;
  1352. g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_PSK, psk,
  1353. NULL);
  1354. g_free (psk);
  1355. if (adhoc)
  1356. g_object_set (wsec,
  1357. NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
  1358. "wpa-none", NULL);
  1359. else
  1360. g_object_set (wsec,
  1361. NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
  1362. "wpa-psk", NULL);
  1363. } else if (!strcmp (value, "WPA-EAP") || !strcmp (value, "IEEE8021X")) {
  1364. if (adhoc) {
  1365. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1366. "Ad-Hoc mode cannot be used with KEY_MGMT type '%s'",
  1367. value);
  1368. goto error;
  1369. }
  1370. *s_8021x = fill_8021x (ssid, value, TRUE, basepath, error);
  1371. if (!*s_8021x)
  1372. goto error;
  1373. lower = g_ascii_strdown (value, -1);
  1374. g_object_set (wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT,
  1375. lower, NULL);
  1376. g_free (lower);
  1377. } else {
  1378. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1379. "Unknown wireless KEY_MGMT type '%s'", value);
  1380. goto error;
  1381. }
  1382. return wsec;
  1383. error:
  1384. if (wsec)
  1385. g_object_unref (wsec);
  1386. return NULL;
  1387. }
  1388. static NMSettingWirelessSecurity *
  1389. make_wireless_security_setting (const char *conn_name,
  1390. const char *basepath,
  1391. NMSetting8021x **s_8021x,
  1392. GError ** error)
  1393. {
  1394. NMSettingWirelessSecurity *wsec = NULL;
  1395. const char *ssid;
  1396. gboolean adhoc = FALSE;
  1397. const char *value;
  1398. g_return_val_if_fail (conn_name != NULL
  1399. && strcmp (ifnet_get_data (conn_name, "type"),
  1400. "ppp") != 0, NULL);
  1401. if (!wpa_get_value (conn_name, "ssid"))
  1402. return NULL;
  1403. PLUGIN_PRINT (IFNET_PLUGIN_NAME,
  1404. "updating wireless security settings (%s).", conn_name);
  1405. ssid = conn_name;
  1406. value = wpa_get_value (ssid, "mode");
  1407. if (value)
  1408. adhoc = strcmp (value, "1") == 0 ? TRUE : FALSE;
  1409. if (!adhoc) {
  1410. wsec = make_leap_setting (ssid, error);
  1411. if (error && *error)
  1412. goto error;
  1413. }
  1414. if (!wsec) {
  1415. wsec = make_wpa_setting (ssid, basepath, s_8021x, error);
  1416. if (error && *error)
  1417. goto error;
  1418. }
  1419. if (!wsec) {
  1420. wsec = make_wep_setting (ssid, error);
  1421. if (error && *error)
  1422. goto error;
  1423. }
  1424. if (!wsec) {
  1425. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1426. "Can't handle security information for ssid: %s",
  1427. conn_name);
  1428. }
  1429. return wsec;
  1430. error:
  1431. return NULL;
  1432. }
  1433. /* Currently only support username and password */
  1434. static void
  1435. make_pppoe_connection_setting (NMConnection *connection,
  1436. const char *conn_name,
  1437. GError **error)
  1438. {
  1439. NMSettingPPPOE *s_pppoe;
  1440. NMSettingPPP *s_ppp;
  1441. const char *value;
  1442. s_pppoe = NM_SETTING_PPPOE (nm_setting_pppoe_new ());
  1443. /* username */
  1444. value = ifnet_get_data (conn_name, "username");
  1445. if (!value) {
  1446. g_set_error (error, ifnet_plugin_error_quark (), 0,
  1447. "ppp requires at lease a username");
  1448. return;
  1449. }
  1450. g_object_set (s_pppoe, NM_SETTING_PPPOE_USERNAME, value, NULL);
  1451. /* password */
  1452. value = ifnet_get_data (conn_name, "password");
  1453. if (!value) {
  1454. value = "";
  1455. }
  1456. g_object_set (s_pppoe, NM_SETTING_PPPOE_PASSWORD, value, NULL);
  1457. nm_connection_add_setting (connection, NM_SETTING (s_pppoe));
  1458. /* PPP setting */
  1459. s_ppp = (NMSettingPPP *) nm_setting_ppp_new ();
  1460. nm_connection_add_setting (connection, NM_SETTING (s_ppp));
  1461. }
  1462. NMConnection *
  1463. ifnet_update_connection_from_config_block (const char *conn_name,
  1464. const char *basepath,
  1465. GError **error)
  1466. {
  1467. const gchar *type = NULL;
  1468. NMConnection *connection = NULL;
  1469. NMSettingConnection *setting = NULL;
  1470. NMSetting8021x *s_8021x = NULL;
  1471. NMSettingWirelessSecurity *wsec = NULL;
  1472. gboolean auto_conn = TRUE;
  1473. const char *value = NULL;
  1474. gboolean success = FALSE;
  1475. connection = nm_connection_new ();
  1476. if (!connection)
  1477. return NULL;
  1478. setting = nm_connection_get_setting_connection (connection);
  1479. if (!setting) {
  1480. setting = NM_SETTING_CONNECTION (nm_setting_connection_new ());
  1481. g_assert (setting);
  1482. nm_connection_add_setting (connection, NM_SETTING (setting));
  1483. }
  1484. type = guess_connection_type (conn_name);
  1485. value = ifnet_get_data (conn_name, "auto");
  1486. if (value && !strcmp (value, "false"))
  1487. auto_conn = FALSE;
  1488. update_connection_id (connection, conn_name);
  1489. g_object_set (setting, NM_SETTING_CONNECTION_TYPE, type,
  1490. NM_SETTING_CONNECTION_READ_ONLY, FALSE,
  1491. NM_SETTING_CONNECTION_AUTOCONNECT, auto_conn, NULL);
  1492. if (!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)
  1493. || !strcmp (NM_SETTING_PPPOE_SETTING_NAME, type)) {
  1494. /* wired setting */
  1495. make_wired_connection_setting (connection, conn_name, error);
  1496. if (error && *error) {
  1497. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1498. "Found error: %s", (*error)->message);
  1499. goto error;
  1500. }
  1501. /* pppoe setting */
  1502. if (!strcmp (NM_SETTING_PPPOE_SETTING_NAME, type))
  1503. make_pppoe_connection_setting (connection, conn_name,
  1504. error);
  1505. if (error && *error) {
  1506. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1507. "Found error: %s", (*error)->message);
  1508. goto error;
  1509. }
  1510. } else if (!strcmp (NM_SETTING_WIRELESS_SETTING_NAME, type)) {
  1511. /* wireless setting */
  1512. NMSetting *wireless_setting;
  1513. wireless_setting = make_wireless_connection_setting (conn_name, &s_8021x, error);
  1514. if (!wireless_setting)
  1515. goto error;
  1516. nm_connection_add_setting (connection, wireless_setting);
  1517. if (error && *error) {
  1518. PLUGIN_WARN (IFNET_PLUGIN_NAME,
  1519. "Found error: …

Large files files are truncated, but you can click here to view the full file