~network-manager/network-manager/ubuntu.hardy.07

« back to all changes in this revision

Viewing changes to system-settings/plugins/ifupdown/parser.c

* merge 0.7~~svn20080905t025540+eni0 snapshot to hardy branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
 
2
 
 
3
/* NetworkManager system settings service (ifupdown)
 
4
 *
 
5
 * Alexander Sack <asac@ubuntu.com>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 *
 
21
 * (C) Copyright 2008 Canonical Ltd.
 
22
 */
1
23
 
2
24
#include <string.h>
3
25
#include <arpa/inet.h>
13
35
#include <nm-system-config-interface.h>
14
36
#include <nm-utils.h>
15
37
 
16
 
#include "shvar.h"
17
38
#include "parser.h"
18
39
#include "plugin.h"
19
40
#include "sha1.h"
72
93
 
73
94
        result = g_malloc0 (len * 2 + 1); 
74
95
        for (i = 0; i < len; i++) 
75
 
                { 
76
 
                        result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf]; 
77
 
                        result[2*i+1] = hex_digits[bytes[i] & 0xf]; 
78
 
                } 
 
96
        { 
 
97
                result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf]; 
 
98
                result[2*i+1] = hex_digits[bytes[i] & 0xf]; 
 
99
        } 
79
100
        /* Cut converted key off at the correct length for this cipher type */ 
80
101
        if (final_len > -1) 
81
102
                result[final_len] = '\0'; 
93
114
map_by_mapping(struct _Mapping *mapping, const gchar *key)
94
115
{
95
116
        struct _Mapping *curr = mapping;
96
 
        g_debug("mapping by mapping (%s)", key);
97
117
        while(curr->domain) {
98
 
                g_debug("mapping by mapping (%s == %s)", curr->domain, key);
99
118
                if(!strcmp(curr->domain, key))
100
119
                        return curr->target;
101
120
                curr++;
105
124
 
106
125
static void
107
126
update_wireless_setting_from_if_block(NMConnection *connection,
108
 
                                      if_block *block)
 
127
                                                           if_block *block)
109
128
{
110
129
        gint wpa_l= strlen("wpa-");
111
130
        gint wireless_l= strlen("wireless-");
117
136
                { NULL, NULL}
118
137
        };
119
138
 
120
 
        NMSettingWireless *wireless_setting;
 
139
        NMSettingWireless *wireless_setting = NULL;
121
140
 
122
141
        if(value && !strcmp("ppp", value)) {
123
142
                return;
124
143
        }
125
144
 
126
 
        wireless_setting = NM_SETTING_WIRELESS(nm_connection_get_setting
127
 
                                               (connection, NM_TYPE_SETTING_WIRELESS));
128
 
 
129
 
        g_debug("update wireless settings (%s).", block->name);
130
 
        if(!wireless_setting) {
131
 
                wireless_setting = NM_SETTING_WIRELESS(nm_setting_wireless_new());
132
 
        }
 
145
        PLUGIN_PRINT ("SCPlugin-Ifupdown", "update wireless settings (%s).", block->name);
 
146
        wireless_setting = NM_SETTING_WIRELESS(nm_setting_wireless_new());
133
147
 
134
148
        while(curr) {
135
 
                g_debug("testing key/value (%s/%s)", curr->key, curr->data);
136
149
                if(strlen(curr->key) > wireless_l &&
137
150
                   !strncmp("wireless-", curr->key, wireless_l)) {
138
151
                        const gchar* newkey = map_by_mapping(mapping, curr->key+wireless_l);
139
 
                        g_debug("setting wireless newkey(%s)=data(%s)", newkey, curr->data);
 
152
                        PLUGIN_PRINT ("SCPlugin-Ifupdown", "wireless setting key: %s='%s'",
 
153
                                            newkey, curr->data);
140
154
                        g_object_set(wireless_setting,
141
155
                                           newkey, curr->data,
142
156
                                           NULL);
148
162
                                gint len = strlen(curr->data);
149
163
                                wireless_setting->ssid = g_byte_array_sized_new (len);
150
164
                                g_byte_array_append (wireless_setting->ssid, (const guint8 *) curr->data, len);
151
 
                                g_debug("ssid = %d", wireless_setting->ssid->len);
 
165
                                PLUGIN_PRINT("SCPlugin-Ifupdown", "setting wpa ssid = %d", wireless_setting->ssid->len);
152
166
                        } else if(newkey) {
153
167
 
154
168
                                g_object_set(wireless_setting,
155
 
                                             newkey, curr->data,
156
 
                                             NULL);
157
 
                        } else {
158
 
                                g_debug("unknown wpa- key (%s) found in /etc/network/interfaces"
159
 
                                        " iface (%s) definition", curr->key, block->name);
 
169
                                                   newkey, curr->data,
 
170
                                                   NULL);
 
171
                                PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wpa newkey(%s)=data(%s)", newkey, curr->data);
160
172
                        }
161
 
                        g_debug("setting 1 wpa newkey(%s)=data(%s)", newkey, curr->data);
162
 
                } else {
163
 
                        g_debug(" => not a wireless setting!");
164
173
                }
165
174
                curr = curr->next;
166
175
        }
167
 
        nm_connection_add_setting(connection, NM_SETTING(wireless_setting));
 
176
        nm_connection_add_setting(connection, (NMSetting*) wireless_setting);
168
177
}
169
178
 
170
179
typedef gchar* (*IfupdownStrDupeFunc) (gpointer value, gpointer data);
 
180
typedef gpointer (*IfupdownStrToTypeFunc) (const gchar* value);
171
181
 
172
182
static char*
173
183
normalize_dupe (gpointer value, gpointer data) {
196
206
        return normalized;
197
207
}
198
208
 
 
209
static gpointer
 
210
string_to_glist_of_strings(const gchar* data)
 
211
{
 
212
        GSList *ret = NULL;
 
213
        gchar *string = (gchar*) data;
 
214
        while(string) {
 
215
                gchar* next = NULL;
 
216
                if( (next = strchr(string, ' '))  ||
 
217
                    (next = strchr(string, '\t')) ||
 
218
                    (next = strchr(string, '\0')) ) {
 
219
 
 
220
                        gchar *part = g_strndup(string, (next - string));
 
221
                        ret = g_slist_append(ret, part);
 
222
                        if (*next)
 
223
                                string = next+1;
 
224
                        else
 
225
                                string = NULL;
 
226
                } else {
 
227
                        string = NULL;
 
228
                }
 
229
        }
 
230
        return ret;
 
231
}
 
232
 
199
233
static void
200
234
update_wireless_security_setting_from_if_block(NMConnection *connection,
201
 
                                               if_block *block)
 
235
                                                                          if_block *block)
202
236
{
203
237
        gint wpa_l= strlen("wpa-");
204
238
        gint wireless_l= strlen("wireless-");
214
248
                {"pairwise", "pairwise"},
215
249
                {"proto", "proto"},
216
250
                {"pin", "pin"},
 
251
                {"wep-key0", "wep-key0"},
 
252
                {"wep-key1", "wep-key1"},
 
253
                {"wep-key2", "wep-key2"},
 
254
                {"wep-key3", "wep-key3"},
217
255
                { NULL, NULL}
218
256
        };
219
257
 
227
265
                {"pairwise", normalize_tolower},
228
266
                {"proto", normalize_tolower},
229
267
                {"pin", normalize_dupe},
230
 
                { NULL, NULL}
231
 
        };
 
268
                {"wep-key0", normalize_dupe},
 
269
                {"wep-key1", normalize_dupe},
 
270
                {"wep-key2", normalize_dupe},
 
271
                {"wep-key3", normalize_dupe},
 
272
                { NULL, NULL}
 
273
        };
 
274
 
 
275
        struct _Mapping type_mapping[] = {
 
276
                {"group", string_to_glist_of_strings},
 
277
                {"pairwise", string_to_glist_of_strings},
 
278
                {"proto", string_to_glist_of_strings},
 
279
                { NULL, NULL}
 
280
        };
 
281
 
232
282
 
233
283
        NMSettingWirelessSecurity *wireless_security_setting;
234
284
        NMSettingWireless *s_wireless;
235
 
                
 
285
        gboolean security = FALSE;
236
286
 
237
287
        if(value && !strcmp("ppp", value)) {
238
288
                return;
239
289
        }
240
290
 
241
291
        s_wireless = NM_SETTING_WIRELESS(nm_connection_get_setting(connection,
242
 
                                                                   NM_TYPE_SETTING_WIRELESS));
243
 
        if(!s_wireless)
244
 
          g_warning("no wireless setting for wireless-security settings");
 
292
                                                                                                    NM_TYPE_SETTING_WIRELESS));
 
293
        g_return_if_fail(s_wireless);
245
294
 
246
 
        g_debug("update wireless settings (%s).", block->name);
 
295
        PLUGIN_PRINT ("SCPlugin-Ifupdown","update wireless security settings (%s).", block->name);
247
296
        wireless_security_setting =
248
297
                NM_SETTING_WIRELESS_SECURITY(nm_setting_wireless_security_new());
249
298
 
250
299
        while(curr) {
251
 
                g_debug("testing key/value (%s/%s)", curr->key, curr->data);
252
300
                if(strlen(curr->key) > wireless_l &&
253
301
                   !strncmp("wireless-", curr->key, wireless_l)) {
254
302
 
255
303
                        gchar *property_value = NULL;
 
304
                        gpointer property_value2 = NULL;
256
305
                        const gchar* newkey = map_by_mapping(mapping, curr->key+wireless_l);
257
306
                        IfupdownStrDupeFunc func = map_by_mapping (dupe_mapping, curr->key+wireless_l);
 
307
                        IfupdownStrToTypeFunc func1 = map_by_mapping (type_mapping, curr->key+wireless_l);
258
308
                        if(!newkey || !func) {
259
309
                                g_warning("no (wireless) mapping found for key: %s", curr->key);
260
310
                                goto next;
261
311
                        }
262
312
                        property_value = (*func) (curr->data, connection);
263
 
                        g_debug("setting wireless security newkey(%s)=data(%s)",
264
 
                                   newkey, property_value);
 
313
                        PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wireless security key: %s=%s",
 
314
                                            newkey, property_value);
 
315
                        if(func1)
 
316
                                property_value2 = (*func1) (property_value);
 
317
                    
265
318
                        g_object_set(wireless_security_setting,
266
 
                                           newkey, property_value,
 
319
                                           newkey, property_value2 ? property_value2 : property_value,
267
320
                                           NULL);
 
321
                        security = TRUE;
268
322
                        g_free(property_value);
 
323
                        if(property_value)
 
324
                                g_free(property_value2);
269
325
                } else if(strlen(curr->key) > wpa_l &&
270
326
                                !strncmp("wpa-", curr->key, wpa_l)) {
271
327
 
272
328
                        gchar *property_value = NULL;
 
329
                        gpointer property_value2 = NULL;
273
330
                        const gchar* newkey = map_by_mapping(mapping, curr->key+wpa_l);
274
331
                        IfupdownStrDupeFunc func = map_by_mapping (dupe_mapping, curr->key+wpa_l);
 
332
                        IfupdownStrToTypeFunc func1 = map_by_mapping (type_mapping, curr->key+wpa_l);
275
333
                        if(!newkey || !func) {
276
 
                                g_debug("no (wpa) mapping found for key: %s", curr->key);
277
334
                                goto next;
278
335
                        }
279
336
                        property_value = (*func) (curr->data, connection);
280
 
                        g_debug("setting wpa newkey(%s)=data(%s)", newkey, property_value);
 
337
                        PLUGIN_PRINT ("SCPlugin-Ifupdown", "setting wpa security key: %s=%s",
 
338
                                            newkey,
 
339
#ifdef DEBUG_SECRETS
 
340
                                            property_value
 
341
#else // DEBUG_SECRETS
 
342
                                            !strcmp("key", newkey) ||
 
343
                                            !strcmp("password", newkey) ||
 
344
                                            !strcmp("pin", newkey) ||
 
345
                                            !strcmp("psk", newkey) ||
 
346
                                            !strcmp("wep-key0", newkey) ||
 
347
                                            !strcmp("wep-key1", newkey) ||
 
348
                                            !strcmp("wep-key2", newkey) ||
 
349
                                            !strcmp("wep-key3", newkey) ||
 
350
                                            NULL ?
 
351
                                            "<omitted>" : property_value
 
352
#endif // DEBUG_SECRETS
 
353
                                            );
 
354
 
 
355
                        if(func1)
 
356
                                property_value2 = (*func1) (property_value);
 
357
                    
281
358
                        g_object_set(wireless_security_setting,
282
 
                                           newkey, property_value,
 
359
                                           newkey, property_value2 ? property_value2 : property_value,
283
360
                                           NULL);
 
361
                        security = TRUE;
284
362
                        g_free(property_value);
285
 
                } else {
286
 
                        g_debug(" => not a wireless setting!");
287
363
                }
288
364
        next:
289
365
                curr = curr->next;
290
366
        }
291
367
 
292
 
        nm_connection_add_setting(connection, NM_SETTING(wireless_security_setting));
293
 
 
294
 
        g_object_set(s_wireless,
295
 
                           NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
296
 
                           NULL);
 
368
 
 
369
        if(security) {
 
370
                nm_connection_add_setting(connection, NM_SETTING(wireless_security_setting));
 
371
                g_object_set(s_wireless, NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NULL);
 
372
        }
 
373
 
297
374
}
298
375
 
299
376
static void
319
396
 
320
397
static void
321
398
update_ip4_setting_from_if_block(NMConnection *connection,
322
 
                                 if_block *block)
 
399
                                                   if_block *block)
323
400
{
324
401
 
325
402
        NMSettingIP4Config *ip4_setting = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new());
340
417
                const char *netmask_v = ifparser_getkey(block, "netmask");
341
418
                const char *gateway_v = ifparser_getkey(block, "gateway");
342
419
                const char *nameserver_v = ifparser_getkey(block, "dns-nameserver");
 
420
                const char *nameservers_v = ifparser_getkey(block, "dns-nameservers");
 
421
                GSList* nameservers_list = NULL;
 
422
                GSList* nameservers_list_i = NULL;
343
423
                GError *error = NULL;
344
424
 
 
425
                if(nameservers_v)
 
426
                        nameservers_list_i = nameservers_list = string_to_glist_of_strings (nameservers_v);
 
427
                if(nameserver_v)
 
428
                        nameservers_list_i = nameservers_list = g_slist_append(nameservers_list, g_strdup(nameserver_v));
 
429
 
345
430
                if (inet_pton (AF_INET, address_v, &tmp_ip4_addr))
346
431
                        ip4config->address = tmp_ip4_addr.s_addr;
347
432
                else
362
447
 
363
448
                PLUGIN_PRINT("SCPlugin-Ifupdown", "addresses count: %d", g_slist_length(ip4_addresses));
364
449
 
365
 
                if (nameserver_v && inet_pton (AF_INET, nameserver_v, &tmp_ip4_addr))
366
 
                        g_array_append_vals(nameserver, &tmp_ip4_addr.s_addr, 1);
367
 
 
368
 
                else if (nameserver_v)
369
 
                        g_set_error (&error, eni_plugin_error_quark (), 0,
370
 
                                           "Invalid %s IP4 address '%s'", "nameserver", nameserver_v);
371
 
                else
 
450
                while(nameservers_list_i) {
 
451
                        gchar *dns = nameservers_list_i->data;
 
452
                        nameservers_list_i = nameservers_list_i -> next;
 
453
                        if(!dns)
 
454
                                continue;
 
455
                        if (inet_pton (AF_INET, dns, &tmp_ip4_addr))
 
456
                                g_array_append_vals(nameserver, &tmp_ip4_addr.s_addr, 1);
 
457
                        else
 
458
                                g_set_error (&error, eni_plugin_error_quark (), 0,
 
459
                                                   "Invalid %s IP4 address nameserver '%s'", "nameserver", dns);
 
460
                }
 
461
                if (!nameserver->len)
372
462
                        PLUGIN_PRINT("SCPlugin-Ifupdown", "No dns-nameserver configured in /etc/network/interfaces");
373
463
 
374
 
                g_object_set(ip4_setting,
375
 
                                   NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
376
 
                                   NULL);               
377
 
 
378
464
                ip4_setting->addresses = ip4_addresses;
379
465
                ip4_setting->dns = nameserver;
 
466
 
 
467
                g_object_set(ip4_setting,
 
468
                                   NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
 
469
                                   NULL);
 
470
 
 
471
                g_slist_foreach (nameservers_list, (GFunc) g_free, NULL);
 
472
                g_slist_free (nameservers_list);
380
473
        }
381
474
 
382
475
        nm_connection_add_setting(connection, NM_SETTING(ip4_setting));
384
477
 
385
478
void
386
479
ifupdown_update_connection_from_if_block(NMConnection *connection,
387
 
                                         if_block *block)
 
480
                                                                 if_block *block)
388
481
{
389
482
        const char *type = NULL;
390
483
        char *idstr = NULL;
 
484
        char *uuid_base = NULL;
 
485
        GError *verify_error =NULL;
 
486
 
391
487
        NMSettingConnection *connection_setting =
392
488
                NM_SETTING_CONNECTION(nm_connection_get_setting
393
489
                                                  (connection, NM_TYPE_SETTING_CONNECTION));
399
495
 
400
496
        type = _ifupdownplugin_guess_connection_type (block);
401
497
        idstr = g_strconcat("Ifupdown (", block->name,")", NULL);
 
498
        uuid_base = idstr;
402
499
 
403
500
        g_object_set (connection_setting,
404
501
                            "type", type,
405
 
                            "id", idstr,
 
502
                            "id", idstr,            
406
503
                            NULL);
 
504
        connection_setting->uuid =
 
505
                nm_utils_uuid_generate_from_string(uuid_base);
407
506
 
408
 
        PLUGIN_PRINT("SCPlugin-Ifupdown", "update_connection_setting_from_if_block (%s) - set type %s",
409
 
                           block->name, type);
410
 
        PLUGIN_PRINT("SCPlugin-Ifupdown",
411
 
                           "update_connection_setting_from_if_block (%s) - set autoconnect %d",
412
 
                           block->name, ((gboolean) strcmp("dhcp", type) == 0));
413
 
        PLUGIN_PRINT("SCPlugin-Ifupdown",
414
 
                           "update_connection_setting_from_if_block (%s) - set id %s",
415
 
                           block->name, idstr);
 
507
        PLUGIN_PRINT("SCPlugin-Ifupdown", "update_connection_setting_from_if_block: name:%s, type:%s, autoconnect:%d, id:%s, uuid: %s",
 
508
                           block->name, type,
 
509
                           ((gboolean) strcmp("dhcp", type) == 0),
 
510
                           idstr,
 
511
                           connection_setting->uuid);
416
512
 
417
513
        if(!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)) {
418
514
                update_wired_setting_from_if_block (connection, block); 
423
519
        }
424
520
 
425
521
        update_ip4_setting_from_if_block(connection, block);
 
522
 
 
523
        if(!nm_connection_verify(connection, &verify_error)) {
 
524
                nm_warning("connection broken: %s (%d)",
 
525
                                 verify_error->message, verify_error->code);
 
526
        }
 
527
 
 
528
        g_free(idstr);
426
529
}