1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2011 Red Hat, Inc.
*
*/
#include <config.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include "nm-core-internal.h"
#include "nm-dispatcher-utils.h"
#include "nm-dispatcher-api.h"
/*******************************************/
static gboolean
parse_main (GKeyFile *kf,
GVariant **out_con_dict,
GVariant **out_con_props,
char **out_expected_iface,
char **out_action,
char **out_vpn_ip_iface,
GError **error)
{
char *uuid, *id;
NMConnection *connection;
NMSettingConnection *s_con;
GVariantBuilder props;
*out_expected_iface = g_key_file_get_string (kf, "main", "expected-iface", error);
if (*out_expected_iface == NULL)
return FALSE;
*out_vpn_ip_iface = g_key_file_get_string (kf, "main", "vpn-ip-iface", NULL);
*out_action = g_key_file_get_string (kf, "main", "action", error);
if (*out_action == NULL)
return FALSE;
uuid = g_key_file_get_string (kf, "main", "uuid", error);
if (uuid == NULL)
return FALSE;
id = g_key_file_get_string (kf, "main", "id", error);
if (id == NULL)
return FALSE;
connection = nm_simple_connection_new ();
g_assert (connection);
s_con = (NMSettingConnection *) nm_setting_connection_new ();
g_assert (s_con);
g_object_set (s_con,
NM_SETTING_CONNECTION_UUID, uuid,
NM_SETTING_CONNECTION_ID, id,
NULL);
g_free (uuid);
g_free (id);
nm_connection_add_setting (connection, NM_SETTING (s_con));
*out_con_dict = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL);
g_object_unref (connection);
g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&props, "{sv}",
"connection-path",
g_variant_new_object_path ("/org/freedesktop/NetworkManager/Connections/5"));
*out_con_props = g_variant_builder_end (&props);
return TRUE;
}
static gboolean
parse_device (GKeyFile *kf, GVariant **out_device_props, GError **error)
{
GVariantBuilder props;
char *tmp;
gint i;
g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
i = g_key_file_get_integer (kf, "device", "state", error);
if (i == 0)
return FALSE;
g_variant_builder_add (&props, "{sv}",
NMD_DEVICE_PROPS_STATE,
g_variant_new_uint32 (i));
i = g_key_file_get_integer (kf, "device", "type", error);
if (i == 0)
return FALSE;
g_variant_builder_add (&props, "{sv}",
NMD_DEVICE_PROPS_TYPE,
g_variant_new_uint32 (i));
tmp = g_key_file_get_string (kf, "device", "interface", error);
if (tmp == NULL)
return FALSE;
g_variant_builder_add (&props, "{sv}",
NMD_DEVICE_PROPS_INTERFACE,
g_variant_new_string (tmp));
g_free (tmp);
tmp = g_key_file_get_string (kf, "device", "ip-interface", error);
if (tmp == NULL)
return FALSE;
g_variant_builder_add (&props, "{sv}",
NMD_DEVICE_PROPS_IP_INTERFACE,
g_variant_new_string (tmp));
g_free (tmp);
tmp = g_key_file_get_string (kf, "device", "path", error);
if (tmp == NULL)
return FALSE;
g_variant_builder_add (&props, "{sv}",
NMD_DEVICE_PROPS_PATH,
g_variant_new_object_path (tmp));
g_free (tmp);
*out_device_props = g_variant_builder_end (&props);
return TRUE;
}
static gboolean
add_uint_array (GKeyFile *kf,
GVariantBuilder *props,
const char *section,
const char *key,
GError **error)
{
char *tmp;
char **split, **iter;
GArray *items;
tmp = g_key_file_get_string (kf, section, key, error);
if (tmp == NULL) {
g_clear_error (error);
return TRUE;
}
split = g_strsplit_set (tmp, " ", -1);
g_free (tmp);
if (g_strv_length (split) > 0) {
items = g_array_sized_new (FALSE, TRUE, sizeof (guint32), g_strv_length (split));
for (iter = split; iter && *iter; iter++) {
if (strlen (g_strstrip (*iter))) {
guint32 addr;
g_assert_cmpint (inet_pton (AF_INET, *iter, &addr), ==, 1);
g_array_append_val (items, addr);
}
}
g_variant_builder_add (props, "{sv}", key,
g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
items->data, items->len,
sizeof (guint32)));
g_array_unref (items);
}
g_strfreev (split);
return TRUE;
}
static gboolean
parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **error)
{
GVariantBuilder props;
char *tmp;
char **split, **iter;
GPtrArray *addresses, *routes;
const char *gateway = NULL;
g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
/* search domains */
/* Use char** for domains. (DBUS_TYPE_G_ARRAY_OF_STRING of NMIP4Config
* becomes G_TYPE_STRV when sending the value over D-Bus)
*/
tmp = g_key_file_get_string (kf, section, "domains", error);
if (tmp == NULL)
return FALSE;
split = g_strsplit_set (tmp, " ", -1);
g_free (tmp);
if (g_strv_length (split) > 0) {
for (iter = split; iter && *iter; iter++)
g_strstrip (*iter);
g_variant_builder_add (&props, "{sv}", "domains", g_variant_new_strv ((gpointer) split, -1));
g_strfreev (split);
}
/* nameservers */
if (!add_uint_array (kf, &props, "ip4", "nameservers", error))
return FALSE;
/* wins-servers */
if (!add_uint_array (kf, &props, "ip4", "wins-servers", error))
return FALSE;
/* Addresses */
tmp = g_key_file_get_string (kf, section, "addresses", error);
if (tmp == NULL)
return FALSE;
split = g_strsplit_set (tmp, ",", -1);
g_free (tmp);
if (g_strv_length (split) > 0) {
addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref);
for (iter = split; iter && *iter; iter++) {
NMIPAddress *addr;
char *ip, *prefix;
if (strlen (g_strstrip (*iter)) == 0)
continue;
ip = *iter;
prefix = strchr (ip, '/');
g_assert (prefix);
*prefix++ = '\0';
if (addresses->len == 0) {
gateway = strchr (prefix, ' ');
g_assert (gateway);
gateway++;
}
addr = nm_ip_address_new (AF_INET, ip, (guint) atoi (prefix), error);
if (!addr) {
g_ptr_array_unref (addresses);
return FALSE;
}
g_ptr_array_add (addresses, addr);
}
g_variant_builder_add (&props, "{sv}", "addresses",
nm_utils_ip4_addresses_to_variant (addresses, gateway));
g_ptr_array_unref (addresses);
}
g_strfreev (split);
/* Routes */
tmp = g_key_file_get_string (kf, section, "routes", error);
g_clear_error (error);
if (tmp) {
split = g_strsplit_set (tmp, ",", -1);
g_free (tmp);
if (g_strv_length (split) > 0) {
routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref);
for (iter = split; iter && *iter; iter++) {
NMIPRoute *route;
char *dest, *prefix, *next_hop, *metric;
if (strlen (g_strstrip (*iter)) == 0)
continue;
dest = *iter;
prefix = strchr (dest, '/');
g_assert (prefix);
*prefix++ = '\0';
next_hop = strchr (prefix, ' ');
g_assert (next_hop);
next_hop++;
metric = strchr (next_hop, ' ');
g_assert (metric);
metric++;
route = nm_ip_route_new (AF_INET,
dest, (guint) atoi (prefix),
next_hop, (guint) atoi (metric),
error);
if (!route) {
g_ptr_array_unref (routes);
return FALSE;
}
g_ptr_array_add (routes, route);
}
g_variant_builder_add (&props, "{sv}", "routes",
nm_utils_ip4_routes_to_variant (routes));
g_ptr_array_unref (routes);
}
g_strfreev (split);
}
*out_props = g_variant_builder_end (&props);
return TRUE;
}
static gboolean
parse_dhcp (GKeyFile *kf,
const char *group_name,
GVariant **out_props,
GError **error)
{
char **keys, **iter, *val;
GVariantBuilder props;
keys = g_key_file_get_keys (kf, group_name, NULL, error);
if (!keys)
return FALSE;
g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
for (iter = keys; iter && *iter; iter++) {
val = g_key_file_get_string (kf, group_name, *iter, error);
if (!val)
return FALSE;
g_variant_builder_add (&props, "{sv}", *iter, g_variant_new_string (val));
g_free (val);
}
*out_props = g_variant_builder_end (&props);
return TRUE;
}
static gboolean
get_dispatcher_file (const char *file,
GVariant **out_con_dict,
GVariant **out_con_props,
GVariant **out_device_props,
GVariant **out_device_ip4_props,
GVariant **out_device_ip6_props,
GVariant **out_device_dhcp4_props,
GVariant **out_device_dhcp6_props,
char **out_vpn_ip_iface,
GVariant **out_vpn_ip4_props,
GVariant **out_vpn_ip6_props,
char **out_expected_iface,
char **out_action,
GHashTable **out_env,
GError **error)
{
GKeyFile *kf;
gboolean success = FALSE;
char **keys, **iter, *val;
kf = g_key_file_new ();
if (!g_key_file_load_from_file (kf, file, G_KEY_FILE_NONE, error))
return FALSE;
if (!parse_main (kf,
out_con_dict,
out_con_props,
out_expected_iface,
out_action,
out_vpn_ip_iface,
error))
goto out;
if (!parse_device (kf, out_device_props, error))
goto out;
if (g_key_file_has_group (kf, "ip4")) {
if (!parse_ip4 (kf, out_device_ip4_props, "ip4", error))
goto out;
}
if (g_key_file_has_group (kf, "dhcp4")) {
if (!parse_dhcp (kf, "dhcp4", out_device_dhcp4_props, error))
goto out;
}
if (g_key_file_has_group (kf, "dhcp6")) {
if (!parse_dhcp (kf, "dhcp6", out_device_dhcp4_props, error))
goto out;
}
g_assert (g_key_file_has_group (kf, "env"));
keys = g_key_file_get_keys (kf, "env", NULL, error);
*out_env = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (iter = keys; iter && *iter; iter++) {
val = g_key_file_get_string (kf, "env", *iter, error);
if (!val)
goto out;
g_hash_table_insert (*out_env,
g_strdup_printf ("%s=%s", *iter, val),
GUINT_TO_POINTER (1));
g_free (val);
}
g_strfreev (keys);
success = TRUE;
out:
g_key_file_free (kf);
return success;
}
/*******************************************/
static void
test_generic (const char *file, const char *override_vpn_ip_iface)
{
GVariant *con_dict = NULL;
GVariant *con_props = NULL;
GVariant *device_props = NULL;
GVariant *device_ip4_props = NULL;
GVariant *device_ip6_props = NULL;
GVariant *device_dhcp4_props = NULL;
GVariant *device_dhcp6_props = NULL;
char *vpn_ip_iface = NULL;
GVariant *vpn_ip4_props = NULL;
GVariant *vpn_ip6_props = NULL;
char *expected_iface = NULL;
char *action = NULL;
char *out_iface = NULL;
GHashTable *expected_env = NULL;
GError *error = NULL;
gboolean success;
char *p;
char **denv, **iter;
/* Read in the test file */
p = g_build_filename (SRCDIR, file, NULL);
success = get_dispatcher_file (p,
&con_dict,
&con_props,
&device_props,
&device_ip4_props,
&device_ip6_props,
&device_dhcp4_props,
&device_dhcp6_props,
&vpn_ip_iface,
&vpn_ip4_props,
&vpn_ip6_props,
&expected_iface,
&action,
&expected_env,
&error);
g_free (p);
g_assert_no_error (error);
g_assert (success);
/* Get the environment from the dispatcher code */
denv = nm_dispatcher_utils_construct_envp (action,
con_dict,
con_props,
device_props,
device_ip4_props,
device_ip6_props,
device_dhcp4_props,
device_dhcp6_props,
override_vpn_ip_iface ? override_vpn_ip_iface : vpn_ip_iface,
vpn_ip4_props,
vpn_ip6_props,
&out_iface);
/* Print out environment for now */
#ifdef DEBUG
g_message ("\n******* Generated environment:");
for (iter = denv; iter && *iter; iter++)
g_message (" %s", *iter);
#endif
#ifdef DEBUG
{
GHashTableIter k;
const char *key;
g_message ("\n******* Expected environment:");
g_hash_table_iter_init (&k, expected_env);
while (g_hash_table_iter_next (&k, (gpointer) &key, NULL))
g_message (" %s", key);
}
#endif
g_assert_cmpint (g_strv_length (denv), ==, g_hash_table_size (expected_env));
/* Compare dispatcher generated env and expected env */
for (iter = denv; iter && *iter; iter++) {
gpointer foo;
const char *i_value = *iter;
if (strstr (i_value, "PATH=") == i_value) {
g_assert_cmpstr (&i_value[strlen("PATH=")], ==, g_getenv ("PATH"));
/* The path is constructed dynamically. Ignore the actual value. */
i_value = "PATH=";
}
foo = g_hash_table_lookup (expected_env, i_value);
if (!foo)
g_warning ("Failed to find %s in environment", i_value);
g_assert (foo);
}
g_assert_cmpstr (expected_iface, ==, out_iface);
g_free (out_iface);
g_free (vpn_ip_iface);
g_free (expected_iface);
g_free (action);
g_variant_unref (con_dict);
g_variant_unref (con_props);
g_variant_unref (device_props);
if (device_ip4_props)
g_variant_unref (device_ip4_props);
if (device_ip6_props)
g_variant_unref (device_ip6_props);
if (device_dhcp4_props)
g_variant_unref (device_dhcp4_props);
if (device_dhcp6_props)
g_variant_unref (device_dhcp6_props);
if (vpn_ip4_props)
g_variant_unref (vpn_ip4_props);
if (vpn_ip6_props)
g_variant_unref (vpn_ip6_props);
g_hash_table_destroy (expected_env);
}
/*******************************************/
static void
test_old_up (void)
{
test_generic ("dispatcher-old-up", NULL);
}
static void
test_old_down (void)
{
test_generic ("dispatcher-old-down", NULL);
}
static void
test_old_vpn_up (void)
{
test_generic ("dispatcher-old-vpn-up", NULL);
}
static void
test_old_vpn_down (void)
{
test_generic ("dispatcher-old-vpn-down", NULL);
}
static void
test_up_empty_vpn_iface (void)
{
/* Test that an empty VPN iface variable, like is passed through D-Bus
* from NM, is ignored by the dispatcher environment construction code.
*/
test_generic ("dispatcher-old-up", "");
}
/*******************************************/
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
g_test_add_func ("/dispatcher/old_up", test_old_up);
g_test_add_func ("/dispatcher/old_down", test_old_down);
g_test_add_func ("/dispatcher/old_vpn_up", test_old_vpn_up);
g_test_add_func ("/dispatcher/old_vpn_down", test_old_vpn_down);
g_test_add_func ("/dispatcher/up_empty_vpn_iface", test_up_empty_vpn_iface);
return g_test_run ();
}
|