1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
3
/* NetworkManager system settings service (ifupdown)
5
* Alexander Sack <asac@ubuntu.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
* (C) Copyright 2008 Canonical Ltd.
25
#include <arpa/inet.h>
27
#include <nm-connection.h>
28
#include <NetworkManager.h>
29
#include <nm-setting-connection.h>
30
#include <nm-setting-ip4-config.h>
31
#include <nm-setting-ppp.h>
32
#include <nm-setting-wired.h>
33
#include <nm-setting-wireless.h>
34
#include <nm-setting-8021x.h>
35
#include <nm-system-config-interface.h>
43
#define WPA_PMK_LEN 32
48
_ifupdownplugin_guess_connection_type (if_block *block)
50
if_data *curr = block->info;
51
const gchar* ret_type = NULL;
52
const gchar* value = ifparser_getkey(block, "inet");
53
if(value && !strcmp("ppp", value)) {
54
ret_type = NM_SETTING_PPP_SETTING_NAME;
57
while(!ret_type && curr) {
58
if(!strncmp("wireless-", curr->key, strlen("wireless-")) ||
59
!strncmp("wpa-", curr->key, strlen("wpa-"))) {
60
ret_type = NM_SETTING_WIRELESS_SETTING_NAME;
66
ret_type = NM_SETTING_WIRED_SETTING_NAME;
68
PLUGIN_PRINT("SCPluginIfupdown",
69
"guessed connection type (%s) = %s",
70
block->name, ret_type);
77
* Convert a byte-array into a hexadecimal string.
79
* Code originally by Alex Larsson <alexl@redhat.com> and
80
* copyright Red Hat, Inc. under terms of the LGPL.
84
utils_bin2hexstr (const char *bytes, int len, int final_len)
86
static char hex_digits[] = "0123456789abcdef";
90
g_return_val_if_fail (bytes != NULL, NULL);
91
g_return_val_if_fail (len > 0, NULL);
92
g_return_val_if_fail (len < 256, NULL); /* Arbitrary limit */
94
result = g_malloc0 (len * 2 + 1);
95
for (i = 0; i < len; i++)
97
result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf];
98
result[2*i+1] = hex_digits[bytes[i] & 0xf];
100
/* Cut converted key off at the correct length for this cipher type */
102
result[final_len] = '\0';
110
const gpointer target;
114
map_by_mapping(struct _Mapping *mapping, const gchar *key)
116
struct _Mapping *curr = mapping;
117
while(curr->domain) {
118
if(!strcmp(curr->domain, key))
126
update_wireless_setting_from_if_block(NMConnection *connection,
129
gint wpa_l= strlen("wpa-");
130
gint wireless_l= strlen("wireless-");
132
if_data *curr = block->info;
133
const gchar* value = ifparser_getkey (block, "inet");
134
struct _Mapping mapping[] = {
139
NMSettingWireless *wireless_setting = NULL;
141
if(value && !strcmp("ppp", value)) {
145
PLUGIN_PRINT ("SCPlugin-Ifupdown", "update wireless settings (%s).", block->name);
146
wireless_setting = NM_SETTING_WIRELESS(nm_setting_wireless_new());
149
if(strlen(curr->key) > wireless_l &&
150
!strncmp("wireless-", curr->key, wireless_l)) {
151
const gchar* newkey = map_by_mapping(mapping, curr->key+wireless_l);
152
PLUGIN_PRINT ("SCPlugin-Ifupdown", "wireless setting key: %s='%s'",
154
g_object_set(wireless_setting,
157
} else if(strlen(curr->key) > wpa_l &&
158
!strncmp("wpa-", curr->key, wpa_l)) {
159
const gchar* newkey = map_by_mapping(mapping, curr->key+wpa_l);
161
if(newkey && !strcmp("ssid", newkey)) {
162
gint len = strlen(curr->data);
163
wireless_setting->ssid = g_byte_array_sized_new (len);
164
g_byte_array_append (wireless_setting->ssid, (const guint8 *) curr->data, len);
165
PLUGIN_PRINT("SCPlugin-Ifupdown", "setting wpa ssid = %d", wireless_setting->ssid->len);
168
g_object_set(wireless_setting,
171
PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wpa newkey(%s)=data(%s)", newkey, curr->data);
176
nm_connection_add_setting(connection, (NMSetting*) wireless_setting);
179
typedef gchar* (*IfupdownStrDupeFunc) (gpointer value, gpointer data);
180
typedef gpointer (*IfupdownStrToTypeFunc) (const gchar* value);
183
normalize_dupe (gpointer value, gpointer data) {
184
return g_strdup(value);
188
normalize_tolower (gpointer value, gpointer data) {
189
return g_ascii_strdown(value, -1);
192
static char *normalize_psk (gpointer value, gpointer data) {
193
NMConnection *connection = data;
194
NMSettingWireless *s_wireless =
195
NM_SETTING_WIRELESS(nm_connection_get_setting(connection, NM_TYPE_SETTING_WIRELESS));
197
if (strlen (value) == 64) {
198
normalized = g_strdup (value);
201
unsigned char *buf = g_malloc0 (WPA_PMK_LEN * 2);
202
pbkdf2_sha1 (value, (char *) s_wireless->ssid->data, s_wireless->ssid->len, 4096, buf, WPA_PMK_LEN);
203
normalized = utils_bin2hexstr ((const char *) buf, WPA_PMK_LEN, WPA_PMK_LEN * 2);
210
string_to_glist_of_strings(const gchar* data)
213
gchar *string = (gchar*) data;
216
if( (next = strchr(string, ' ')) ||
217
(next = strchr(string, '\t')) ||
218
(next = strchr(string, '\0')) ) {
220
gchar *part = g_strndup(string, (next - string));
221
ret = g_slist_append(ret, part);
234
update_wireless_security_setting_from_if_block(NMConnection *connection,
237
gint wpa_l= strlen("wpa-");
238
gint wireless_l= strlen("wireless-");
239
if_data *curr = block->info;
240
const gchar* value = ifparser_getkey (block, "inet");
241
struct _Mapping mapping[] = {
243
{"identity", "identity"},
244
{"password", "password"},
246
{"key-mgmt", "key-mgmt"},
248
{"pairwise", "pairwise"},
251
{"wep-key0", "wep-key0"},
252
{"wep-key1", "wep-key1"},
253
{"wep-key2", "wep-key2"},
254
{"wep-key3", "wep-key3"},
255
{"wep-tx-keyidx", "wep-tx-keyidx"},
259
struct _Mapping dupe_mapping[] = {
260
{"psk", normalize_psk},
261
{"identity", normalize_dupe},
262
{"password", normalize_dupe},
263
{"key", normalize_dupe},
264
{"key-mgmt", normalize_tolower},
265
{"group", normalize_tolower},
266
{"pairwise", normalize_tolower},
267
{"proto", normalize_tolower},
268
{"pin", normalize_dupe},
269
{"wep-key0", normalize_dupe},
270
{"wep-key1", normalize_dupe},
271
{"wep-key2", normalize_dupe},
272
{"wep-key3", normalize_dupe},
273
{"wep-tx-keyidx", normalize_dupe},
277
struct _Mapping type_mapping[] = {
278
{"group", string_to_glist_of_strings},
279
{"pairwise", string_to_glist_of_strings},
280
{"proto", string_to_glist_of_strings},
285
NMSettingWirelessSecurity *wireless_security_setting;
286
NMSettingWireless *s_wireless;
287
gboolean security = FALSE;
289
if(value && !strcmp("ppp", value)) {
293
s_wireless = NM_SETTING_WIRELESS(nm_connection_get_setting(connection,
294
NM_TYPE_SETTING_WIRELESS));
295
g_return_if_fail(s_wireless);
297
PLUGIN_PRINT ("SCPlugin-Ifupdown","update wireless security settings (%s).", block->name);
298
wireless_security_setting =
299
NM_SETTING_WIRELESS_SECURITY(nm_setting_wireless_security_new());
302
if(strlen(curr->key) > wireless_l &&
303
!strncmp("wireless-", curr->key, wireless_l)) {
305
gchar *property_value = NULL;
306
gpointer property_value2 = NULL;
307
const gchar* newkey = map_by_mapping(mapping, curr->key+wireless_l);
308
IfupdownStrDupeFunc func = map_by_mapping (dupe_mapping, curr->key+wireless_l);
309
IfupdownStrToTypeFunc func1 = map_by_mapping (type_mapping, curr->key+wireless_l);
310
if(!newkey || !func) {
311
g_warning("no (wireless) mapping found for key: %s", curr->key);
314
property_value = (*func) (curr->data, connection);
315
PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wireless security key: %s=%s",
316
newkey, property_value);
318
property_value2 = (*func1) (property_value);
320
g_object_set(wireless_security_setting,
321
newkey, property_value2 ? property_value2 : property_value,
324
g_free(property_value);
326
g_free(property_value2);
327
} else if(strlen(curr->key) > wpa_l &&
328
!strncmp("wpa-", curr->key, wpa_l)) {
330
gchar *property_value = NULL;
331
gpointer property_value2 = NULL;
332
const gchar* newkey = map_by_mapping(mapping, curr->key+wpa_l);
333
IfupdownStrDupeFunc func = map_by_mapping (dupe_mapping, curr->key+wpa_l);
334
IfupdownStrToTypeFunc func1 = map_by_mapping (type_mapping, curr->key+wpa_l);
335
if(!newkey || !func) {
338
property_value = (*func) (curr->data, connection);
339
PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wpa security key: %s=%s",
343
#else // DEBUG_SECRETS
344
!strcmp("key", newkey) ||
345
!strcmp("password", newkey) ||
346
!strcmp("pin", newkey) ||
347
!strcmp("psk", newkey) ||
348
!strcmp("wep-key0", newkey) ||
349
!strcmp("wep-key1", newkey) ||
350
!strcmp("wep-key2", newkey) ||
351
!strcmp("wep-key3", newkey) ||
353
"<omitted>" : property_value
354
#endif // DEBUG_SECRETS
358
property_value2 = (*func1) (property_value);
360
g_object_set(wireless_security_setting,
361
newkey, property_value2 ? property_value2 : property_value,
364
g_free(property_value);
372
nm_connection_add_setting(connection, NM_SETTING(wireless_security_setting));
373
g_object_set(s_wireless, NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NULL);
379
update_wired_setting_from_if_block(NMConnection *connection,
382
NMSettingWired *s_wired = NULL;
383
s_wired = NM_SETTING_WIRED(nm_setting_wired_new());
384
nm_connection_add_setting(connection, NM_SETTING(s_wired));
388
eni_plugin_error_quark() {
389
static GQuark error_quark = 0;
392
error_quark = g_quark_from_static_string ("eni-plugin-error-quark");
400
update_ip4_setting_from_if_block(NMConnection *connection,
404
NMSettingIP4Config *ip4_setting = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new());
405
const char *type = ifparser_getkey(block, "inet");
406
gboolean is_static = type && !strcmp("static", type);
409
g_object_set(ip4_setting,
410
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
413
struct in_addr tmp_ip4_addr;
414
NMSettingIP4Address *ip4config = g_new0(NMSettingIP4Address, 1);
415
GSList *ip4_addresses = NULL;
416
GArray *nameserver = g_array_new (TRUE, TRUE, sizeof(guint32));
418
const char *address_v = ifparser_getkey(block, "address");
419
const char *netmask_v = ifparser_getkey(block, "netmask");
420
const char *gateway_v = ifparser_getkey(block, "gateway");
421
const char *nameserver_v = ifparser_getkey(block, "dns-nameserver");
422
const char *nameservers_v = ifparser_getkey(block, "dns-nameservers");
423
GSList* nameservers_list = NULL;
424
GSList* nameservers_list_i = NULL;
425
GError *error = NULL;
428
nameservers_list_i = nameservers_list = string_to_glist_of_strings (nameservers_v);
430
nameservers_list_i = nameservers_list = g_slist_append(nameservers_list, g_strdup(nameserver_v));
433
address_v = g_strdup ("0.0.0.0");
435
if (inet_pton (AF_INET, address_v, &tmp_ip4_addr))
436
ip4config->address = tmp_ip4_addr.s_addr;
438
g_set_error (&error, eni_plugin_error_quark (), 0,
439
"Invalid %s IP4 address '%s'", "address", address_v);
441
netmask_v = g_strdup( "255.255.255.255");
443
if (inet_pton (AF_INET, netmask_v, &tmp_ip4_addr))
444
ip4config->prefix = nm_utils_ip4_netmask_to_prefix(tmp_ip4_addr.s_addr);
446
g_set_error (&error, eni_plugin_error_quark (), 0,
447
"Invalid %s IP4 address '%s'", "netmask", netmask_v);
450
gateway_v = g_strdup (address_v);
452
if (inet_pton (AF_INET, gateway_v, &tmp_ip4_addr))
453
ip4config->gateway = tmp_ip4_addr.s_addr;
455
g_set_error (&error, eni_plugin_error_quark (), 0,
456
"Invalid %s IP4 address '%s'", "gateway", gateway_v);
458
ip4_addresses = g_slist_append(ip4_addresses, ip4config);
460
PLUGIN_PRINT("SCPlugin-Ifupdown", "addresses count: %d", g_slist_length(ip4_addresses));
462
while(nameservers_list_i) {
463
gchar *dns = nameservers_list_i->data;
464
nameservers_list_i = nameservers_list_i -> next;
467
if (inet_pton (AF_INET, dns, &tmp_ip4_addr))
468
g_array_append_vals(nameserver, &tmp_ip4_addr.s_addr, 1);
470
g_set_error (&error, eni_plugin_error_quark (), 0,
471
"Invalid %s IP4 address nameserver '%s'", "nameserver", dns);
473
if (!nameserver->len)
474
PLUGIN_PRINT("SCPlugin-Ifupdown", "No dns-nameserver configured in /etc/network/interfaces");
476
ip4_setting->addresses = ip4_addresses;
477
ip4_setting->dns = nameserver;
479
g_object_set(ip4_setting,
480
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
483
g_slist_foreach (nameservers_list, (GFunc) g_free, NULL);
484
g_slist_free (nameservers_list);
487
nm_connection_add_setting(connection, NM_SETTING(ip4_setting));
491
ifupdown_update_connection_from_if_block(NMConnection *connection,
494
const char *type = NULL;
496
char *uuid_base = NULL;
497
GError *verify_error =NULL;
499
NMSettingConnection *connection_setting =
500
NM_SETTING_CONNECTION(nm_connection_get_setting
501
(connection, NM_TYPE_SETTING_CONNECTION));
503
if(!connection_setting) {
504
connection_setting = NM_SETTING_CONNECTION(nm_setting_connection_new());
505
nm_connection_add_setting(connection, NM_SETTING(connection_setting));
508
type = _ifupdownplugin_guess_connection_type (block);
509
idstr = g_strconcat("Ifupdown (", block->name,")", NULL);
512
g_object_set (connection_setting,
516
connection_setting->uuid =
517
nm_utils_uuid_generate_from_string(uuid_base);
519
PLUGIN_PRINT("SCPlugin-Ifupdown", "update_connection_setting_from_if_block: name:%s, type:%s, autoconnect:%d, id:%s, uuid: %s",
521
((gboolean) strcmp("dhcp", type) == 0),
523
connection_setting->uuid);
525
if(!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)) {
526
update_wired_setting_from_if_block (connection, block);
528
else if(!strcmp (NM_SETTING_WIRELESS_SETTING_NAME, type)) {
529
update_wireless_setting_from_if_block (connection, block);
530
update_wireless_security_setting_from_if_block (connection, block);
533
update_ip4_setting_from_if_block(connection, block);
535
if(!nm_connection_verify(connection, &verify_error)) {
536
nm_warning("connection broken: %s (%d)",
537
verify_error->message, verify_error->code);