~ubuntu-branches/ubuntu/oneiric/network-manager/oneiric

« back to all changes in this revision

Viewing changes to .pc/libnl3-port.patch/src/NetworkManagerUtils.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2011-08-08 10:17:28 UTC
  • Revision ID: james.westby@ubuntu.com-20110808101728-nbhqa7aztcwetla2
Tags: 0.8.9997+git.20110721t045648.36db194-0ubuntu2
* debian/control: bump libnl Build-Depends to libnl3-dev.
* debian/patches/libnl3-port.patch: backport changes from NM's libnl3 git
  branch to port to libnl3 APIs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager -- Network link manager
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 *
 
18
 * Copyright (C) 2004 - 2011 Red Hat, Inc.
 
19
 * Copyright (C) 2005 - 2008 Novell, Inc.
 
20
 */
 
21
 
 
22
#include <glib.h>
 
23
#include <errno.h>
 
24
#include <fcntl.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <unistd.h>
 
28
#include <ctype.h>
 
29
 
 
30
#include "NetworkManagerUtils.h"
 
31
#include "nm-utils.h"
 
32
#include "nm-logging.h"
 
33
#include "nm-device.h"
 
34
#include "nm-device-wifi.h"
 
35
#include "nm-device-ethernet.h"
 
36
#include "nm-dbus-manager.h"
 
37
#include "nm-dispatcher-action.h"
 
38
#include "nm-dbus-glib-types.h"
 
39
#include "nm-setting-connection.h"
 
40
#include "nm-setting-ip4-config.h"
 
41
#include "nm-setting-ip6-config.h"
 
42
#include "nm-setting-wireless.h"
 
43
#include "nm-setting-wireless-security.h"
 
44
#include "nm-manager-auth.h"
 
45
 
 
46
#include <netlink/addr.h>
 
47
#include <netinet/in.h>
 
48
 
 
49
/*
 
50
 * nm_ethernet_address_is_valid
 
51
 *
 
52
 * Compares an Ethernet address against known invalid addresses.
 
53
 *
 
54
 */
 
55
gboolean
 
56
nm_ethernet_address_is_valid (const struct ether_addr *test_addr)
 
57
{
 
58
        guint8 invalid_addr1[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 
59
        guint8 invalid_addr2[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
60
        guint8 invalid_addr3[ETH_ALEN] = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44};
 
61
        guint8 invalid_addr4[ETH_ALEN] = {0x00, 0x30, 0xb4, 0x00, 0x00, 0x00}; /* prism54 dummy MAC */
 
62
 
 
63
        g_return_val_if_fail (test_addr != NULL, FALSE);
 
64
 
 
65
        /* Compare the AP address the card has with invalid ethernet MAC addresses. */
 
66
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr1, ETH_ALEN))
 
67
                return FALSE;
 
68
 
 
69
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr2, ETH_ALEN))
 
70
                return FALSE;
 
71
 
 
72
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr3, ETH_ALEN))
 
73
                return FALSE;
 
74
 
 
75
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr4, ETH_ALEN))
 
76
                return FALSE;
 
77
 
 
78
        if (test_addr->ether_addr_octet[0] & 1)                 /* Multicast addresses */
 
79
                return FALSE;
 
80
        
 
81
        return TRUE;
 
82
}
 
83
 
 
84
 
 
85
int
 
86
nm_spawn_process (const char *args)
 
87
{
 
88
        gint num_args;
 
89
        char **argv = NULL;
 
90
        int status = -1;
 
91
        GError *error = NULL;
 
92
 
 
93
        g_return_val_if_fail (args != NULL, -1);
 
94
 
 
95
        if (!g_shell_parse_argv (args, &num_args, &argv, &error)) {
 
96
                nm_log_warn (LOGD_CORE, "could not parse arguments for '%s': %s", args, error->message);
 
97
                g_error_free (error);
 
98
                return -1;
 
99
        }
 
100
 
 
101
        if (!g_spawn_sync ("/", argv, NULL, 0, NULL, NULL, NULL, NULL, &status, &error)) {
 
102
                nm_log_warn (LOGD_CORE, "could not spawn process '%s': %s", args, error->message);
 
103
                g_error_free (error);
 
104
        }
 
105
 
 
106
        g_strfreev (argv);
 
107
        return status;
 
108
}
 
109
 
 
110
/*
 
111
 * nm_utils_ip4_netmask_to_prefix
 
112
 *
 
113
 * Figure out the network prefix from a netmask.  Netmask
 
114
 * MUST be in network byte order.
 
115
 *
 
116
 */
 
117
guint32
 
118
nm_utils_ip4_netmask_to_prefix (guint32 netmask)
 
119
{
 
120
        guchar *p, *end;
 
121
        guint32 prefix = 0;
 
122
 
 
123
        p = (guchar *) &netmask;
 
124
        end = p + sizeof (guint32);
 
125
 
 
126
        while ((*p == 0xFF) && p < end) {
 
127
                prefix += 8;
 
128
                p++;
 
129
        }
 
130
 
 
131
        if (p < end) {
 
132
                guchar v = *p;
 
133
 
 
134
                while (v) {
 
135
                        prefix++;
 
136
                        v <<= 1;
 
137
                }
 
138
        }
 
139
 
 
140
        return prefix;
 
141
}
 
142
 
 
143
/*
 
144
 * nm_utils_ip4_prefix_to_netmask
 
145
 *
 
146
 * Figure out the netmask from a prefix.
 
147
 *
 
148
 */
 
149
guint32
 
150
nm_utils_ip4_prefix_to_netmask (guint32 prefix)
 
151
{
 
152
        guint32 msk = 0x80000000;
 
153
        guint32 netmask = 0;
 
154
 
 
155
        while (prefix > 0) {
 
156
                netmask |= msk;
 
157
                msk >>= 1;
 
158
                prefix--;
 
159
        }
 
160
 
 
161
        return (guint32) htonl (netmask);
 
162
}
 
163
 
 
164
char *
 
165
nm_ether_ntop (const struct ether_addr *mac)
 
166
{
 
167
        /* we like leading zeros and all-caps, instead
 
168
         * of what glibc's ether_ntop() gives us
 
169
         */
 
170
        return g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X",
 
171
                                mac->ether_addr_octet[0], mac->ether_addr_octet[1],
 
172
                                mac->ether_addr_octet[2], mac->ether_addr_octet[3],
 
173
                                mac->ether_addr_octet[4], mac->ether_addr_octet[5]);
 
174
}
 
175
 
 
176
void
 
177
nm_utils_merge_ip4_config (NMIP4Config *ip4_config, NMSettingIP4Config *setting)
 
178
{
 
179
        int i, j;
 
180
 
 
181
        if (!setting)
 
182
                return; /* Defaults are just fine */
 
183
 
 
184
        if (nm_setting_ip4_config_get_ignore_auto_dns (setting)) {
 
185
                nm_ip4_config_reset_nameservers (ip4_config);
 
186
                nm_ip4_config_reset_domains (ip4_config);
 
187
                nm_ip4_config_reset_searches (ip4_config);
 
188
        }
 
189
 
 
190
        if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
 
191
                nm_ip4_config_reset_routes (ip4_config);
 
192
 
 
193
        for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) {
 
194
                guint32 ns;
 
195
                gboolean found = FALSE;
 
196
 
 
197
                /* Avoid dupes */
 
198
                ns = nm_setting_ip4_config_get_dns (setting, i);
 
199
                for (j = 0; j < nm_ip4_config_get_num_nameservers (ip4_config); j++) {
 
200
                        if (nm_ip4_config_get_nameserver (ip4_config, j) == ns) {
 
201
                                found = TRUE;
 
202
                                break;
 
203
                        }
 
204
                }
 
205
 
 
206
                if (!found)
 
207
                        nm_ip4_config_add_nameserver (ip4_config, ns);
 
208
        }
 
209
 
 
210
        /* DNS search domains */
 
211
        for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) {
 
212
                const char *search = nm_setting_ip4_config_get_dns_search (setting, i);
 
213
                gboolean found = FALSE;
 
214
 
 
215
                /* Avoid dupes */
 
216
                for (j = 0; j < nm_ip4_config_get_num_searches (ip4_config); j++) {
 
217
                        if (!strcmp (search, nm_ip4_config_get_search (ip4_config, j))) {
 
218
                                found = TRUE;
 
219
                                break;
 
220
                        }
 
221
                }
 
222
 
 
223
                if (!found)
 
224
                        nm_ip4_config_add_search (ip4_config, search);
 
225
        }
 
226
 
 
227
        /* IPv4 addresses */
 
228
        for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) {
 
229
                NMIP4Address *setting_addr = nm_setting_ip4_config_get_address (setting, i);
 
230
                guint32 num;
 
231
 
 
232
                num = nm_ip4_config_get_num_addresses (ip4_config);
 
233
                for (j = 0; j < num; j++) {
 
234
                        NMIP4Address *cfg_addr = nm_ip4_config_get_address (ip4_config, j);
 
235
 
 
236
                        /* Dupe, override with user-specified address */
 
237
                        if (nm_ip4_address_get_address (cfg_addr) == nm_ip4_address_get_address (setting_addr)) {
 
238
                                nm_ip4_config_replace_address (ip4_config, j, setting_addr);
 
239
                                break;
 
240
                        }
 
241
                }
 
242
 
 
243
                if (j == num)
 
244
                        nm_ip4_config_add_address (ip4_config, setting_addr);
 
245
        }
 
246
 
 
247
        /* IPv4 routes */
 
248
        for (i = 0; i < nm_setting_ip4_config_get_num_routes (setting); i++) {
 
249
                NMIP4Route *setting_route = nm_setting_ip4_config_get_route (setting, i);
 
250
                guint32 num;
 
251
 
 
252
                num = nm_ip4_config_get_num_routes (ip4_config);
 
253
                for (j = 0; j < num; j++) {
 
254
                        NMIP4Route *cfg_route = nm_ip4_config_get_route (ip4_config, j);
 
255
 
 
256
                        /* Dupe, override with user-specified route */
 
257
                        if (   (nm_ip4_route_get_dest (cfg_route) == nm_ip4_route_get_dest (setting_route))
 
258
                            && (nm_ip4_route_get_prefix (cfg_route) == nm_ip4_route_get_prefix (setting_route))
 
259
                            && (nm_ip4_route_get_next_hop (cfg_route) == nm_ip4_route_get_next_hop (setting_route))) {
 
260
                                nm_ip4_config_replace_route (ip4_config, j, setting_route);
 
261
                                break;
 
262
                        }
 
263
                }
 
264
 
 
265
                if (j == num)
 
266
                        nm_ip4_config_add_route (ip4_config, setting_route);
 
267
        }
 
268
 
 
269
        if (nm_setting_ip4_config_get_never_default (setting))
 
270
                nm_ip4_config_set_never_default (ip4_config, TRUE);
 
271
}
 
272
 
 
273
static inline gboolean
 
274
ip6_addresses_equal (const struct in6_addr *a, const struct in6_addr *b)
 
275
{
 
276
        return memcmp (a, b, sizeof (struct in6_addr)) == 0;
 
277
}
 
278
 
 
279
/* This is exactly identical to nm_utils_merge_ip4_config, with s/4/6/,
 
280
 * except that we can't compare addresses with ==.
 
281
 */
 
282
void
 
283
nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting)
 
284
{
 
285
        int i, j;
 
286
 
 
287
        if (!setting)
 
288
                return; /* Defaults are just fine */
 
289
 
 
290
        if (nm_setting_ip6_config_get_ignore_auto_dns (setting)) {
 
291
                nm_ip6_config_reset_nameservers (ip6_config);
 
292
                nm_ip6_config_reset_domains (ip6_config);
 
293
                nm_ip6_config_reset_searches (ip6_config);
 
294
        }
 
295
 
 
296
        if (nm_setting_ip6_config_get_ignore_auto_routes (setting))
 
297
                nm_ip6_config_reset_routes (ip6_config);
 
298
 
 
299
        for (i = 0; i < nm_setting_ip6_config_get_num_dns (setting); i++) {
 
300
                const struct in6_addr *ns;
 
301
                gboolean found = FALSE;
 
302
 
 
303
                /* Avoid dupes */
 
304
                ns = nm_setting_ip6_config_get_dns (setting, i);
 
305
                for (j = 0; j < nm_ip6_config_get_num_nameservers (ip6_config); j++) {
 
306
                        if (ip6_addresses_equal (nm_ip6_config_get_nameserver (ip6_config, j), ns)) {
 
307
                                found = TRUE;
 
308
                                break;
 
309
                        }
 
310
                }
 
311
 
 
312
                if (!found)
 
313
                        nm_ip6_config_add_nameserver (ip6_config, ns);
 
314
        }
 
315
 
 
316
        /* DNS search domains */
 
317
        for (i = 0; i < nm_setting_ip6_config_get_num_dns_searches (setting); i++) {
 
318
                const char *search = nm_setting_ip6_config_get_dns_search (setting, i);
 
319
                gboolean found = FALSE;
 
320
 
 
321
                /* Avoid dupes */
 
322
                for (j = 0; j < nm_ip6_config_get_num_searches (ip6_config); j++) {
 
323
                        if (!strcmp (search, nm_ip6_config_get_search (ip6_config, j))) {
 
324
                                found = TRUE;
 
325
                                break;
 
326
                        }
 
327
                }
 
328
 
 
329
                if (!found)
 
330
                        nm_ip6_config_add_search (ip6_config, search);
 
331
        }
 
332
 
 
333
        /* IPv6 addresses */
 
334
        for (i = 0; i < nm_setting_ip6_config_get_num_addresses (setting); i++) {
 
335
                NMIP6Address *setting_addr = nm_setting_ip6_config_get_address (setting, i);
 
336
                guint32 num;
 
337
 
 
338
                num = nm_ip6_config_get_num_addresses (ip6_config);
 
339
                for (j = 0; j < num; j++) {
 
340
                        NMIP6Address *cfg_addr = nm_ip6_config_get_address (ip6_config, j);
 
341
 
 
342
                        /* Dupe, override with user-specified address */
 
343
                        if (ip6_addresses_equal (nm_ip6_address_get_address (cfg_addr), nm_ip6_address_get_address (setting_addr))) {
 
344
                                nm_ip6_config_replace_address (ip6_config, j, setting_addr);
 
345
                                break;
 
346
                        }
 
347
                }
 
348
 
 
349
                if (j == num)
 
350
                        nm_ip6_config_add_address (ip6_config, setting_addr);
 
351
        }
 
352
 
 
353
        /* IPv6 routes */
 
354
        for (i = 0; i < nm_setting_ip6_config_get_num_routes (setting); i++) {
 
355
                NMIP6Route *setting_route = nm_setting_ip6_config_get_route (setting, i);
 
356
                guint32 num;
 
357
 
 
358
                num = nm_ip6_config_get_num_routes (ip6_config);
 
359
                for (j = 0; j < num; j++) {
 
360
                        NMIP6Route *cfg_route = nm_ip6_config_get_route (ip6_config, j);
 
361
 
 
362
                        /* Dupe, override with user-specified route */
 
363
                        if (   ip6_addresses_equal (nm_ip6_route_get_dest (cfg_route), nm_ip6_route_get_dest (setting_route))
 
364
                            && (nm_ip6_route_get_prefix (cfg_route) == nm_ip6_route_get_prefix (setting_route))
 
365
                                && ip6_addresses_equal (nm_ip6_route_get_next_hop (cfg_route), nm_ip6_route_get_next_hop (setting_route))) {
 
366
                                nm_ip6_config_replace_route (ip6_config, j, setting_route);
 
367
                                break;
 
368
                        }
 
369
                }
 
370
 
 
371
                if (j == num)
 
372
                        nm_ip6_config_add_route (ip6_config, setting_route);
 
373
        }
 
374
 
 
375
        if (nm_setting_ip6_config_get_never_default (setting))
 
376
                nm_ip6_config_set_never_default (ip6_config, TRUE);
 
377
}
 
378
 
 
379
static void
 
380
dump_object_to_props (GObject *object, GHashTable *hash)
 
381
{
 
382
        GParamSpec **pspecs;
 
383
        guint len = 0, i;
 
384
 
 
385
        pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &len);
 
386
        for (i = 0; i < len; i++) {
 
387
                value_hash_add_object_property (hash,
 
388
                                                pspecs[i]->name,
 
389
                                                object,
 
390
                                                pspecs[i]->name,
 
391
                                                pspecs[i]->value_type);
 
392
        }
 
393
        g_free (pspecs);
 
394
}
 
395
 
 
396
static void
 
397
dump_dhcp4_to_props (NMDHCP4Config *config, GHashTable *hash)
 
398
{
 
399
        GSList *options, *iter;
 
400
 
 
401
        options = nm_dhcp4_config_list_options (config);
 
402
        for (iter = options; iter; iter = g_slist_next (iter)) {
 
403
                const char *option = (const char *) iter->data;
 
404
                const char *val;
 
405
 
 
406
                val = nm_dhcp4_config_get_option (config, option);
 
407
                value_hash_add_str (hash, option, val);
 
408
        }
 
409
        g_slist_free (options);
 
410
}
 
411
 
 
412
static void
 
413
dump_dhcp6_to_props (NMDHCP6Config *config, GHashTable *hash)
 
414
{
 
415
        GSList *options, *iter;
 
416
 
 
417
        options = nm_dhcp6_config_list_options (config);
 
418
        for (iter = options; iter; iter = g_slist_next (iter)) {
 
419
                const char *option = (const char *) iter->data;
 
420
                const char *val;
 
421
 
 
422
                val = nm_dhcp6_config_get_option (config, option);
 
423
                value_hash_add_str (hash, option, val);
 
424
        }
 
425
        g_slist_free (options);
 
426
}
 
427
 
 
428
static void
 
429
fill_device_props (NMDevice *device,
 
430
                   GHashTable *dev_hash,
 
431
                   GHashTable *ip4_hash,
 
432
                   GHashTable *ip6_hash,
 
433
                   GHashTable *dhcp4_hash,
 
434
                   GHashTable *dhcp6_hash)
 
435
{
 
436
        NMIP4Config *ip4_config;
 
437
        NMIP6Config *ip6_config;
 
438
        NMDHCP4Config *dhcp4_config;
 
439
        NMDHCP6Config *dhcp6_config;
 
440
 
 
441
        /* If the action is for a VPN, send the VPN's IP interface instead of the device's */
 
442
        value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_IP_INTERFACE, nm_device_get_ip_iface (device));
 
443
        value_hash_add_str (dev_hash, NMD_DEVICE_PROPS_INTERFACE, nm_device_get_iface (device));
 
444
        value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_TYPE, nm_device_get_device_type (device));
 
445
        value_hash_add_uint (dev_hash, NMD_DEVICE_PROPS_STATE, nm_device_get_state (device));
 
446
        value_hash_add_object_path (dev_hash, NMD_DEVICE_PROPS_PATH, nm_device_get_path (device));
 
447
 
 
448
        ip4_config = nm_device_get_ip4_config (device);
 
449
        if (ip4_config)
 
450
                dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
 
451
 
 
452
        ip6_config = nm_device_get_ip6_config (device);
 
453
        if (ip6_config)
 
454
                dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
 
455
 
 
456
        dhcp4_config = nm_device_get_dhcp4_config (device);
 
457
        if (dhcp4_config)
 
458
                dump_dhcp4_to_props (dhcp4_config, dhcp4_hash);
 
459
 
 
460
        dhcp6_config = nm_device_get_dhcp6_config (device);
 
461
        if (dhcp6_config)
 
462
                dump_dhcp6_to_props (dhcp6_config, dhcp6_hash);
 
463
}
 
464
 
 
465
static void
 
466
fill_vpn_props (NMIP4Config *ip4_config,
 
467
                NMIP6Config *ip6_config,
 
468
                GHashTable *ip4_hash,
 
469
                GHashTable *ip6_hash)
 
470
{
 
471
        if (ip4_config)
 
472
                dump_object_to_props (G_OBJECT (ip4_config), ip4_hash);
 
473
        if (ip6_config)
 
474
                dump_object_to_props (G_OBJECT (ip6_config), ip6_hash);
 
475
}
 
476
 
 
477
static void
 
478
dispatcher_done_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
 
479
{
 
480
        dbus_g_proxy_end_call (proxy, call, NULL, G_TYPE_INVALID);
 
481
        g_object_unref (proxy);
 
482
}
 
483
 
 
484
void
 
485
nm_utils_call_dispatcher (const char *action,
 
486
                          NMConnection *connection,
 
487
                          NMDevice *device,
 
488
                          const char *vpn_iface,
 
489
                          NMIP4Config *vpn_ip4_config,
 
490
                          NMIP6Config *vpn_ip6_config)
 
491
{
 
492
        NMDBusManager *dbus_mgr;
 
493
        DBusGProxy *proxy;
 
494
        DBusGConnection *g_connection;
 
495
        GHashTable *connection_hash;
 
496
        GHashTable *connection_props;
 
497
        GHashTable *device_props;
 
498
        GHashTable *device_ip4_props;
 
499
        GHashTable *device_ip6_props;
 
500
        GHashTable *device_dhcp4_props;
 
501
        GHashTable *device_dhcp6_props;
 
502
        GHashTable *vpn_ip4_props;
 
503
        GHashTable *vpn_ip6_props;
 
504
 
 
505
        g_return_if_fail (action != NULL);
 
506
 
 
507
        /* All actions except 'hostname' require a device */
 
508
        if (strcmp (action, "hostname") != 0)
 
509
                g_return_if_fail (NM_IS_DEVICE (device));
 
510
        /* VPN actions require at least an IPv4 config (for now) */
 
511
        if (strcmp (action, "vpn-up") == 0)
 
512
                g_return_if_fail (vpn_ip4_config != NULL);
 
513
 
 
514
        dbus_mgr = nm_dbus_manager_get ();
 
515
        g_connection = nm_dbus_manager_get_connection (dbus_mgr);
 
516
        proxy = dbus_g_proxy_new_for_name (g_connection,
 
517
                                           NM_DISPATCHER_DBUS_SERVICE,
 
518
                                           NM_DISPATCHER_DBUS_PATH,
 
519
                                           NM_DISPATCHER_DBUS_IFACE);
 
520
        if (!proxy) {
 
521
                nm_log_err (LOGD_CORE, "could not get dispatcher proxy!");
 
522
                g_object_unref (dbus_mgr);
 
523
                return;
 
524
        }
 
525
 
 
526
        if (connection) {
 
527
                connection_hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_NO_SECRETS);
 
528
 
 
529
                connection_props = value_hash_create ();
 
530
 
 
531
                /* path */
 
532
                value_hash_add_object_path (connection_props,
 
533
                                                                        NMD_CONNECTION_PROPS_PATH,
 
534
                                                                        nm_connection_get_path (connection));
 
535
        } else {
 
536
                connection_hash = value_hash_create ();
 
537
                connection_props = value_hash_create ();
 
538
        }
 
539
 
 
540
        device_props = value_hash_create ();
 
541
        device_ip4_props = value_hash_create ();
 
542
        device_ip6_props = value_hash_create ();
 
543
        device_dhcp4_props = value_hash_create ();
 
544
        device_dhcp6_props = value_hash_create ();
 
545
        vpn_ip4_props = value_hash_create ();
 
546
        vpn_ip6_props = value_hash_create ();
 
547
 
 
548
        /* hostname actions only send the hostname */
 
549
        if (strcmp (action, "hostname") != 0) {
 
550
                fill_device_props (device,
 
551
                                       device_props,
 
552
                                       device_ip4_props,
 
553
                                       device_ip6_props,
 
554
                                       device_dhcp4_props,
 
555
                                       device_dhcp6_props);
 
556
                if (vpn_iface)
 
557
                        fill_vpn_props (vpn_ip4_config, NULL, vpn_ip4_props, vpn_ip6_props);
 
558
        }
 
559
 
 
560
        /* Do a non-blocking call, but wait for the reply, because dbus-glib
 
561
         * sometimes needs time to complete internal housekeeping.  If we use
 
562
         * dbus_g_proxy_call_no_reply(), that housekeeping (specifically the
 
563
         * GetNameOwner response) doesn't complete and we run into an assert
 
564
         * on unreffing the proxy.
 
565
         */
 
566
        dbus_g_proxy_begin_call_with_timeout (proxy, "Action",
 
567
                                              dispatcher_done_cb,
 
568
                                              dbus_mgr,       /* automatically unref the dbus mgr when call is done */
 
569
                                              g_object_unref,
 
570
                                              5000,
 
571
                                              G_TYPE_STRING, action,
 
572
                                              DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, connection_hash,
 
573
                                              DBUS_TYPE_G_MAP_OF_VARIANT, connection_props,
 
574
                                              DBUS_TYPE_G_MAP_OF_VARIANT, device_props,
 
575
                                              DBUS_TYPE_G_MAP_OF_VARIANT, device_ip4_props,
 
576
                                              DBUS_TYPE_G_MAP_OF_VARIANT, device_ip6_props,
 
577
                                              DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp4_props,
 
578
                                              DBUS_TYPE_G_MAP_OF_VARIANT, device_dhcp6_props,
 
579
                                              G_TYPE_STRING, vpn_iface ? vpn_iface : "",
 
580
                                              DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip4_props,
 
581
                                              DBUS_TYPE_G_MAP_OF_VARIANT, vpn_ip6_props,
 
582
                                              G_TYPE_INVALID);
 
583
        g_hash_table_destroy (connection_hash);
 
584
        g_hash_table_destroy (connection_props);
 
585
        g_hash_table_destroy (device_props);
 
586
        g_hash_table_destroy (device_ip4_props);
 
587
        g_hash_table_destroy (device_ip6_props);
 
588
        g_hash_table_destroy (device_dhcp4_props);
 
589
        g_hash_table_destroy (device_dhcp6_props);
 
590
        g_hash_table_destroy (vpn_ip4_props);
 
591
        g_hash_table_destroy (vpn_ip6_props);
 
592
}
 
593
 
 
594
gboolean
 
595
nm_match_spec_hwaddr (const GSList *specs, const char *hwaddr)
 
596
{
 
597
        const GSList *iter;
 
598
        char *hwaddr_match, *p;
 
599
 
 
600
        g_return_val_if_fail (hwaddr != NULL, FALSE);
 
601
 
 
602
        p = hwaddr_match = g_strdup_printf ("mac:%s", hwaddr);
 
603
 
 
604
        while (*p) {
 
605
                *p = g_ascii_tolower (*p);
 
606
                p++;
 
607
        }
 
608
 
 
609
        for (iter = specs; iter; iter = g_slist_next (iter)) {
 
610
                if (!strcmp ((const char *) iter->data, hwaddr_match)) {
 
611
                        g_free (hwaddr_match);
 
612
                        return TRUE;
 
613
                }
 
614
        }
 
615
 
 
616
        g_free (hwaddr_match);
 
617
        return FALSE;
 
618
}
 
619
 
 
620
#define BUFSIZE 10
 
621
 
 
622
static gboolean
 
623
parse_subchannels (const char *subchannels, guint32 *a, guint32 *b, guint32 *c)
 
624
{
 
625
        long unsigned int tmp;
 
626
        char buf[BUFSIZE + 1];
 
627
        const char *p = subchannels;
 
628
        int i = 0;
 
629
        char *pa = NULL, *pb = NULL, *pc = NULL;
 
630
 
 
631
        g_return_val_if_fail (subchannels != NULL, FALSE);
 
632
        g_return_val_if_fail (a != NULL, FALSE);
 
633
        g_return_val_if_fail (*a == 0, FALSE);
 
634
        g_return_val_if_fail (b != NULL, FALSE);
 
635
        g_return_val_if_fail (*b == 0, FALSE);
 
636
        g_return_val_if_fail (c != NULL, FALSE);
 
637
        g_return_val_if_fail (*c == 0, FALSE);
 
638
 
 
639
        /* sanity check */
 
640
        if (!isxdigit (subchannels[0]))
 
641
                return FALSE;
 
642
 
 
643
        /* Get the first channel */
 
644
        while (*p && (*p != ',')) {
 
645
                if (!isxdigit (*p) && (*p != '.'))
 
646
                        return FALSE;  /* Invalid chars */
 
647
                if (i >= BUFSIZE)
 
648
                        return FALSE;  /* Too long to be a subchannel */
 
649
                buf[i++] = *p++;
 
650
        }
 
651
        buf[i] = '\0';
 
652
 
 
653
        /* and grab each of its elements, there should be 3 */
 
654
        pa = &buf[0];
 
655
        pb = strchr (buf, '.');
 
656
        if (pb)
 
657
                pc = strchr (pb + 1, '.');
 
658
        if (!pa || !pb || !pc)
 
659
                return FALSE;
 
660
 
 
661
        /* Split the string */
 
662
        *pb++ = '\0';
 
663
        *pc++ = '\0';
 
664
 
 
665
        errno = 0;
 
666
        tmp = strtoul (pa, NULL, 16);
 
667
        if (errno)
 
668
                return FALSE;
 
669
        *a = (guint32) tmp;
 
670
 
 
671
        errno = 0;
 
672
        tmp = strtoul (pb, NULL, 16);
 
673
        if (errno)
 
674
                return FALSE;
 
675
        *b = (guint32) tmp;
 
676
 
 
677
        errno = 0;
 
678
        tmp = strtoul (pc, NULL, 16);
 
679
        if (errno)
 
680
                return FALSE;
 
681
        *c = (guint32) tmp;
 
682
 
 
683
        return TRUE;
 
684
}
 
685
 
 
686
#define SUBCHAN_TAG "s390-subchannels:"
 
687
 
 
688
gboolean
 
689
nm_match_spec_s390_subchannels (const GSList *specs, const char *subchannels)
 
690
{
 
691
        const GSList *iter;
 
692
        guint32 a = 0, b = 0, c = 0;
 
693
        guint32 spec_a = 0, spec_b = 0, spec_c = 0;
 
694
 
 
695
        g_return_val_if_fail (subchannels != NULL, FALSE);
 
696
 
 
697
        if (!parse_subchannels (subchannels, &a, &b, &c))
 
698
                return FALSE;
 
699
 
 
700
        for (iter = specs; iter; iter = g_slist_next (iter)) {
 
701
                const char *spec = iter->data;
 
702
 
 
703
                if (!strncmp (spec, SUBCHAN_TAG, strlen (SUBCHAN_TAG))) {
 
704
                        spec += strlen (SUBCHAN_TAG);
 
705
                        if (parse_subchannels (spec, &spec_a, &spec_b, &spec_c)) {
 
706
                                if (a == spec_a && b == spec_b && c == spec_c)
 
707
                                        return TRUE;
 
708
                        }
 
709
                }
 
710
        }
 
711
 
 
712
        return FALSE;
 
713
}
 
714
 
 
715
const char *
 
716
nm_utils_get_shared_wifi_permission (NMConnection *connection)
 
717
{
 
718
        NMSettingWireless *s_wifi;
 
719
        NMSettingWirelessSecurity *s_wsec;
 
720
        NMSettingIP4Config *s_ip4;
 
721
        const char *method = NULL;
 
722
 
 
723
        s_ip4 = nm_connection_get_setting_ip4_config (connection);
 
724
        if (s_ip4)
 
725
                method = nm_setting_ip4_config_get_method (s_ip4);
 
726
 
 
727
        if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0)
 
728
                return NULL;  /* Not shared */
 
729
 
 
730
        s_wifi = nm_connection_get_setting_wireless (connection);
 
731
        if (s_wifi) {
 
732
                s_wsec = nm_connection_get_setting_wireless_security (connection);
 
733
                if (nm_setting_wireless_get_security (s_wifi) || s_wsec)
 
734
                        return NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED;
 
735
                else
 
736
                        return NM_AUTH_PERMISSION_WIFI_SHARE_OPEN;
 
737
        }
 
738
 
 
739
        return NULL;
 
740
}
 
741
 
 
742
/*********************************/
 
743
 
 
744
static void
 
745
nm_gvalue_destroy (gpointer data)
 
746
{
 
747
        GValue *value = (GValue *) data;
 
748
 
 
749
        g_value_unset (value);
 
750
        g_slice_free (GValue, value);
 
751
}
 
752
 
 
753
GHashTable *
 
754
value_hash_create (void)
 
755
{
 
756
        return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, nm_gvalue_destroy);
 
757
}
 
758
 
 
759
void
 
760
value_hash_add (GHashTable *hash,
 
761
                                const char *key,
 
762
                                GValue *value)
 
763
{
 
764
        g_hash_table_insert (hash, g_strdup (key), value);
 
765
}
 
766
 
 
767
void
 
768
value_hash_add_str (GHashTable *hash,
 
769
                                        const char *key,
 
770
                                        const char *str)
 
771
{
 
772
        GValue *value;
 
773
 
 
774
        value = g_slice_new0 (GValue);
 
775
        g_value_init (value, G_TYPE_STRING);
 
776
        g_value_set_string (value, str);
 
777
 
 
778
        value_hash_add (hash, key, value);
 
779
}
 
780
 
 
781
void
 
782
value_hash_add_object_path (GHashTable *hash,
 
783
                                                        const char *key,
 
784
                                                        const char *op)
 
785
{
 
786
        GValue *value;
 
787
 
 
788
        value = g_slice_new0 (GValue);
 
789
        g_value_init (value, DBUS_TYPE_G_OBJECT_PATH);
 
790
        g_value_set_boxed (value, op);
 
791
 
 
792
        value_hash_add (hash, key, value);
 
793
}
 
794
 
 
795
void
 
796
value_hash_add_uint (GHashTable *hash,
 
797
                                         const char *key,
 
798
                                         guint32 val)
 
799
{
 
800
        GValue *value;
 
801
 
 
802
        value = g_slice_new0 (GValue);
 
803
        g_value_init (value, G_TYPE_UINT);
 
804
        g_value_set_uint (value, val);
 
805
 
 
806
        value_hash_add (hash, key, value);
 
807
}
 
808
 
 
809
void
 
810
value_hash_add_bool (GHashTable *hash,
 
811
                                         const char *key,
 
812
                                         gboolean val)
 
813
{
 
814
        GValue *value;
 
815
 
 
816
        value = g_slice_new0 (GValue);
 
817
        g_value_init (value, G_TYPE_BOOLEAN);
 
818
        g_value_set_boolean (value, val);
 
819
 
 
820
        value_hash_add (hash, key, value);
 
821
}
 
822
 
 
823
void
 
824
value_hash_add_object_property (GHashTable *hash,
 
825
                                const char *key,
 
826
                                GObject *object,
 
827
                                const char *prop,
 
828
                                GType val_type)
 
829
{
 
830
        GValue *value;
 
831
 
 
832
        value = g_slice_new0 (GValue);
 
833
        g_value_init (value, val_type);
 
834
        g_object_get_property (object, prop, value);
 
835
        value_hash_add (hash, key, value);
 
836
}
 
837
 
 
838
gboolean
 
839
nm_utils_do_sysctl (const char *path, const char *value)
 
840
{
 
841
        int fd, len, nwrote, total;
 
842
 
 
843
        fd = open (path, O_WRONLY | O_TRUNC);
 
844
        if (fd == -1)
 
845
                return FALSE;
 
846
 
 
847
        len = strlen (value);
 
848
        total = 0;
 
849
        do {
 
850
                nwrote = write (fd, value + total, len - total);
 
851
                if (nwrote == -1) {
 
852
                        if (errno == EINTR)
 
853
                                continue;
 
854
                        close (fd);
 
855
                        return FALSE;
 
856
                }
 
857
                total += nwrote;
 
858
        } while (total < len);
 
859
 
 
860
        close (fd);
 
861
        return TRUE;
 
862
}
 
863
 
 
864
gboolean
 
865
nm_utils_get_proc_sys_net_value (const char *path,
 
866
                                 const char *iface,
 
867
                                 guint32 *out_value)
 
868
{
 
869
        GError *error = NULL;
 
870
        char *contents = NULL;
 
871
        gboolean success = FALSE;
 
872
        long int tmp;
 
873
 
 
874
        if (!g_file_get_contents (path, &contents, NULL, &error)) {
 
875
                nm_log_dbg (LOGD_DEVICE, "(%s): error reading %s: (%d) %s",
 
876
                            iface, path,
 
877
                            error ? error->code : -1,
 
878
                            error && error->message ? error->message : "(unknown)");
 
879
                g_clear_error (&error);
 
880
        } else {
 
881
                errno = 0;
 
882
                tmp = strtol (contents, NULL, 10);
 
883
                if ((errno == 0) && (tmp == 0 || tmp == 1)) {
 
884
                        *out_value = (guint32) tmp;
 
885
                        success = TRUE;
 
886
                }
 
887
                g_free (contents);
 
888
        }
 
889
 
 
890
        return success;
 
891
}
 
892
 
 
893
static char *
 
894
get_new_connection_name (const GSList *existing,
 
895
                         const char *format,
 
896
                         const char *preferred)
 
897
{
 
898
        GSList *names = NULL;
 
899
        const GSList *iter;
 
900
        char *cname = NULL;
 
901
        int i = 0;
 
902
        gboolean preferred_found = FALSE;
 
903
 
 
904
        for (iter = existing; iter; iter = g_slist_next (iter)) {
 
905
                NMConnection *candidate = NM_CONNECTION (iter->data);
 
906
                const char *id;
 
907
 
 
908
                id = nm_connection_get_id (candidate);
 
909
                g_assert (id);
 
910
                names = g_slist_append (names, (gpointer) id);
 
911
 
 
912
                if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
 
913
                        preferred_found = TRUE;
 
914
        }
 
915
 
 
916
        /* Return the preferred name if it was unique */
 
917
        if (preferred && !preferred_found) {
 
918
                g_slist_free (names);
 
919
                return g_strdup (preferred);
 
920
        }
 
921
 
 
922
        /* Otherwise find the next available unique connection name using the given
 
923
         * connection name template.
 
924
         */
 
925
        while (!cname && (i++ < 10000)) {
 
926
                char *temp;
 
927
                gboolean found = FALSE;
 
928
 
 
929
                temp = g_strdup_printf (format, i);
 
930
                for (iter = names; iter; iter = g_slist_next (iter)) {
 
931
                        if (!strcmp (iter->data, temp)) {
 
932
                                found = TRUE;
 
933
                                break;
 
934
                        }
 
935
                }
 
936
                if (!found)
 
937
                        cname = temp;
 
938
                else
 
939
                        g_free (temp);
 
940
        }
 
941
 
 
942
        g_slist_free (names);
 
943
        return cname;
 
944
}
 
945
 
 
946
void
 
947
nm_utils_complete_generic (NMConnection *connection,
 
948
                           const char *ctype,
 
949
                           const GSList *existing,
 
950
                           const char *format,
 
951
                           const char *preferred,
 
952
                           gboolean default_enable_ipv6)
 
953
{
 
954
        NMSettingConnection *s_con;
 
955
        NMSettingIP4Config *s_ip4;
 
956
        NMSettingIP6Config *s_ip6;
 
957
        const char *method;
 
958
        char *id, *uuid;
 
959
 
 
960
        s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
 
961
        if (!s_con) {
 
962
                s_con = (NMSettingConnection *) nm_setting_connection_new ();
 
963
                nm_connection_add_setting (connection, NM_SETTING (s_con));
 
964
        }
 
965
        g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_TYPE, ctype, NULL);
 
966
 
 
967
        if (!nm_setting_connection_get_uuid (s_con)) {
 
968
                uuid = nm_utils_uuid_generate ();
 
969
                g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_UUID, uuid, NULL);
 
970
                g_free (uuid);
 
971
        }
 
972
 
 
973
        /* Add a connection ID if absent */
 
974
        if (!nm_setting_connection_get_id (s_con)) {
 
975
                id = get_new_connection_name (existing, format, preferred);
 
976
                g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_ID, id, NULL);
 
977
                g_free (id);
 
978
        }
 
979
 
 
980
        /* Add an 'auto' IPv4 connection if present */
 
981
        s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
 
982
        if (!s_ip4) {
 
983
                s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
 
984
                nm_connection_add_setting (connection, NM_SETTING (s_ip4));
 
985
        }
 
986
        method = nm_setting_ip4_config_get_method (s_ip4);
 
987
        if (!method) {
 
988
                g_object_set (G_OBJECT (s_ip4),
 
989
                              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
 
990
                              NULL);
 
991
        }
 
992
 
 
993
        /* Add an 'auto' IPv6 setting if allowed and not preset */
 
994
        s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
 
995
        if (!s_ip6 && default_enable_ipv6) {
 
996
                s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
 
997
                nm_connection_add_setting (connection, NM_SETTING (s_ip6));
 
998
        }
 
999
        if (s_ip6 && !nm_setting_ip6_config_get_method (s_ip6)) {
 
1000
                g_object_set (G_OBJECT (s_ip6),
 
1001
                              NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
 
1002
                              NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE,
 
1003
                              NULL);
 
1004
        }
 
1005
}
 
1006