device: renew dhcp leases on awake for software devices
[NetworkManager.git] / src / nm-config-data.c
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) 2011 Red Hat, Inc.
19  * Copyright (C) 2013 Thomas Bechtold <thomasbechtold@jpberlin.de>
20  */
21
22 #include "nm-default.h"
23
24 #include "nm-config-data.h"
25
26 #include <string.h>
27
28 #include "nm-config.h"
29 #include "nm-device.h"
30 #include "nm-core-internal.h"
31 #include "nm-keyfile-internal.h"
32
33 typedef struct {
34         char *group_name;
35         gboolean stop_match;
36         struct {
37                 /* have a separate boolean field @has, because a @spec with
38                  * value %NULL does not necessarily mean, that the property
39                  * "match-device" was unspecified. */
40                 gboolean has;
41                 GSList *spec;
42         } match_device;
43 } ConnectionInfo;
44
45 typedef struct {
46         char *config_main_file;
47         char *config_description;
48
49         GKeyFile *keyfile;
50         GKeyFile *keyfile_user;
51         GKeyFile *keyfile_intern;
52
53         /* A zero-terminated list of pre-processed information from the
54          * [connection] sections. This is to speed up lookup. */
55         ConnectionInfo *connection_infos;
56
57         struct {
58                 char *uri;
59                 char *response;
60                 guint interval;
61         } connectivity;
62
63         struct {
64                 char **arr;
65                 GSList *specs;
66                 GSList *specs_config;
67         } no_auto_default;
68
69         GSList *ignore_carrier;
70         GSList *assume_ipv6ll_only;
71
72         char *dns_mode;
73         char *rc_manager;
74
75         NMGlobalDnsConfig *global_dns;
76
77         /* mutable field */
78         char *value_cached;
79 } NMConfigDataPrivate;
80
81 struct _NMGlobalDnsDomain {
82         char *name;
83         char **servers;
84         char **options;
85 };
86
87 struct _NMGlobalDnsConfig {
88         char **searches;
89         char **options;
90         GHashTable *domains;
91         char **domain_list;
92         gboolean internal;
93 };
94
95 enum {
96         PROP_0,
97         PROP_CONFIG_MAIN_FILE,
98         PROP_CONFIG_DESCRIPTION,
99         PROP_KEYFILE_USER,
100         PROP_KEYFILE_INTERN,
101         PROP_CONNECTIVITY_URI,
102         PROP_CONNECTIVITY_INTERVAL,
103         PROP_CONNECTIVITY_RESPONSE,
104         PROP_NO_AUTO_DEFAULT,
105
106         LAST_PROP
107 };
108
109
110 G_DEFINE_TYPE (NMConfigData, nm_config_data, G_TYPE_OBJECT)
111
112 #define NM_CONFIG_DATA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CONFIG_DATA, NMConfigDataPrivate))
113
114 /************************************************************************/
115
116 #define _HAS_PREFIX(str, prefix) \
117         ({ \
118                 const char *_str = (str); \
119                 g_str_has_prefix ( _str, ""prefix"") && _str[NM_STRLEN(prefix)] != '\0'; \
120         })
121
122 /************************************************************************/
123
124 const char *
125 nm_config_data_get_config_main_file (const NMConfigData *self)
126 {
127         g_return_val_if_fail (self, NULL);
128
129         return NM_CONFIG_DATA_GET_PRIVATE (self)->config_main_file;
130 }
131
132 const char *
133 nm_config_data_get_config_description (const NMConfigData *self)
134 {
135         g_return_val_if_fail (self, NULL);
136
137         return NM_CONFIG_DATA_GET_PRIVATE (self)->config_description;
138 }
139
140 gboolean
141 nm_config_data_has_group (const NMConfigData *self, const char *group)
142 {
143         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
144         g_return_val_if_fail (group && *group, FALSE);
145
146         return g_key_file_has_group (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group);
147 }
148
149 char *
150 nm_config_data_get_value (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags)
151 {
152         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
153         g_return_val_if_fail (group && *group, NULL);
154         g_return_val_if_fail (key && *key, NULL);
155
156         return nm_config_keyfile_get_value (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, flags);
157 }
158
159 const char *nm_config_data_get_value_cached (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags)
160 {
161         NMConfigDataPrivate *priv;
162
163         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
164         g_return_val_if_fail (group && *group, NULL);
165         g_return_val_if_fail (key && *key, NULL);
166
167         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
168
169         /* we modify @value_cached. In C++ jargon, the field is mutable. */
170         g_free (priv->value_cached);
171         priv->value_cached = nm_config_keyfile_get_value (priv->keyfile, group, key, flags);
172         return priv->value_cached;
173 }
174
175 gboolean
176 nm_config_data_has_value (const NMConfigData *self, const char *group, const char *key, NMConfigGetValueFlags flags)
177 {
178         gs_free char *value = NULL;
179
180         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
181         g_return_val_if_fail (group && *group, FALSE);
182         g_return_val_if_fail (key && *key, FALSE);
183
184         value = nm_config_keyfile_get_value (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, flags);
185         return !!value;
186 }
187
188 gint
189 nm_config_data_get_value_boolean (const NMConfigData *self, const char *group, const char *key, gint default_value)
190 {
191         char *str;
192         gint value = default_value;
193
194         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), default_value);
195         g_return_val_if_fail (group && *group, default_value);
196         g_return_val_if_fail (key && *key, default_value);
197
198         /* when parsing the boolean, base it on the raw value from g_key_file_get_value(). */
199         str = g_key_file_get_value (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, key, NULL);
200         if (str) {
201                 value = nm_config_parse_boolean (str, default_value);
202                 g_free (str);
203         }
204         return value;
205 }
206
207 const char *
208 nm_config_data_get_connectivity_uri (const NMConfigData *self)
209 {
210         g_return_val_if_fail (self, NULL);
211
212         return NM_CONFIG_DATA_GET_PRIVATE (self)->connectivity.uri;
213 }
214
215 const guint
216 nm_config_data_get_connectivity_interval (const NMConfigData *self)
217 {
218         g_return_val_if_fail (self, 0);
219
220         return NM_CONFIG_DATA_GET_PRIVATE (self)->connectivity.interval;
221 }
222
223 const char *
224 nm_config_data_get_connectivity_response (const NMConfigData *self)
225 {
226         g_return_val_if_fail (self != NULL, NULL);
227
228         return NM_CONFIG_DATA_GET_PRIVATE (self)->connectivity.response;
229 }
230
231 const char *const*
232 nm_config_data_get_no_auto_default (const NMConfigData *self)
233 {
234         g_return_val_if_fail (self, FALSE);
235
236         return (const char *const*) NM_CONFIG_DATA_GET_PRIVATE (self)->no_auto_default.arr;
237 }
238
239 gboolean
240 nm_config_data_get_no_auto_default_for_device (const NMConfigData *self, NMDevice *device)
241 {
242         NMConfigDataPrivate *priv;
243
244         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
245         g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
246
247         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
248         return    nm_device_spec_match_list (device, priv->no_auto_default.specs)
249                || nm_device_spec_match_list (device, priv->no_auto_default.specs_config);
250 }
251
252 const char *
253 nm_config_data_get_dns_mode (const NMConfigData *self)
254 {
255         g_return_val_if_fail (self, NULL);
256
257         return NM_CONFIG_DATA_GET_PRIVATE (self)->dns_mode;
258 }
259
260 const char *
261 nm_config_data_get_rc_manager (const NMConfigData *self)
262 {
263         g_return_val_if_fail (self, NULL);
264
265         return NM_CONFIG_DATA_GET_PRIVATE (self)->rc_manager;
266 }
267
268 gboolean
269 nm_config_data_get_ignore_carrier (const NMConfigData *self, NMDevice *device)
270 {
271         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
272         g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
273
274         return nm_device_spec_match_list (device, NM_CONFIG_DATA_GET_PRIVATE (self)->ignore_carrier);
275 }
276
277 gboolean
278 nm_config_data_get_assume_ipv6ll_only (const NMConfigData *self, NMDevice *device)
279 {
280         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
281         g_return_val_if_fail (NM_IS_DEVICE (device), FALSE);
282
283         return nm_device_spec_match_list (device, NM_CONFIG_DATA_GET_PRIVATE (self)->assume_ipv6ll_only);
284 }
285
286 GKeyFile *
287 nm_config_data_clone_keyfile_intern (const NMConfigData *self)
288 {
289         NMConfigDataPrivate *priv;
290         GKeyFile *keyfile;
291
292         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
293
294         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
295
296         keyfile = nm_config_create_keyfile ();
297         if (priv->keyfile_intern)
298                 _nm_keyfile_copy (keyfile, priv->keyfile_intern);
299         return keyfile;
300 }
301
302 GKeyFile *
303 _nm_config_data_get_keyfile (const NMConfigData *self)
304 {
305         return NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile;
306 }
307
308 GKeyFile *
309 _nm_config_data_get_keyfile_intern (const NMConfigData *self)
310 {
311         return NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile_intern;
312 }
313
314 GKeyFile *
315 _nm_config_data_get_keyfile_user (const NMConfigData *self)
316 {
317         return NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile_user;
318 }
319
320 /************************************************************************/
321
322 /**
323  * nm_config_data_get_groups:
324  * @self: the #NMConfigData instance
325  *
326  * Returns: (transfer full): the list of groups in the configuration. The order
327  * of the section is undefined, as the configuration gets merged from multiple
328  * sources.
329  */
330 char **
331 nm_config_data_get_groups (const NMConfigData *self)
332 {
333         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
334
335         return g_key_file_get_groups (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, NULL);
336 }
337
338 char **
339 nm_config_data_get_keys (const NMConfigData *self, const char *group)
340 {
341         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
342         g_return_val_if_fail (group && *group, NULL);
343
344         return g_key_file_get_keys (NM_CONFIG_DATA_GET_PRIVATE (self)->keyfile, group, NULL, NULL);
345 }
346
347 /**
348  * nm_config_data_is_intern_atomic_group:
349  * @self:
350  * @group: name of the group to check.
351  *
352  * whether a configuration group @group exists and is entirely overwritten
353  * by internal configuration, i.e. whether it is an atomic group that is
354  * overwritten.
355  *
356  * It doesn't say, that there actually is a user setting that was overwritten. That
357  * means there could be no corresponding section defined in user configuration
358  * that required overwriting.
359  *
360  * Returns: %TRUE if @group exists and is an atomic group set via internal configuration.
361  */
362 gboolean
363 nm_config_data_is_intern_atomic_group (const NMConfigData *self, const char *group)
364 {
365         NMConfigDataPrivate *priv;
366
367         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), FALSE);
368         g_return_val_if_fail (group && *group, FALSE);
369
370         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
371
372         if (   !priv->keyfile_intern
373             || !g_key_file_has_key (priv->keyfile_intern, group, NM_CONFIG_KEYFILE_KEY_ATOMIC_SECTION_WAS, NULL))
374                 return FALSE;
375
376         /* we have a .was entry for the section. That means that the section would be overwritten
377          * from user configuration. But it doesn't mean that the merged configuration contains this
378          * groups, because the internal setting could hide the user section.
379          * Only return TRUE, if we actually have such a group in the merged configuration.*/
380         return g_key_file_has_group (priv->keyfile, group);
381 }
382
383 /************************************************************************/
384
385 static GKeyFile *
386 _merge_keyfiles (GKeyFile *keyfile_user, GKeyFile *keyfile_intern)
387 {
388         gs_strfreev char **groups = NULL;
389         guint g, k;
390         GKeyFile *keyfile;
391         gsize ngroups;
392
393         keyfile = nm_config_create_keyfile ();
394         if (keyfile_user)
395                 _nm_keyfile_copy (keyfile, keyfile_user);
396         if (!keyfile_intern)
397                 return keyfile;
398
399         groups = g_key_file_get_groups (keyfile_intern, &ngroups);
400         if (!groups)
401                 return keyfile;
402
403         /* we must reverse the order of the connection settings so that we
404          * have lowest priority last. */
405         _nm_config_sort_groups (groups, ngroups);
406         for (g = 0; groups[g]; g++) {
407                 const char *group = groups[g];
408                 gs_strfreev char **keys = NULL;
409                 gboolean is_intern, is_atomic = FALSE;
410
411                 keys = g_key_file_get_keys (keyfile_intern, group, NULL, NULL);
412                 if (!keys)
413                         continue;
414
415                 is_intern = g_str_has_prefix (group, NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN);
416                 if (   !is_intern
417                     && g_key_file_has_key (keyfile_intern, group, NM_CONFIG_KEYFILE_KEY_ATOMIC_SECTION_WAS, NULL)) {
418                         /* the entire section is atomically overwritten by @keyfile_intern. */
419                         g_key_file_remove_group (keyfile, group, NULL);
420                         is_atomic = TRUE;
421                 }
422
423                 for (k = 0; keys[k]; k++) {
424                         const char *key = keys[k];
425                         gs_free char *value = NULL;
426
427                         if (is_atomic && strcmp (key, NM_CONFIG_KEYFILE_KEY_ATOMIC_SECTION_WAS) == 0)
428                                 continue;
429
430                         if (   !is_intern && !is_atomic
431                             && _HAS_PREFIX (key, NM_CONFIG_KEYFILE_KEYPREFIX_WAS)) {
432                                 const char *key_base = &key[NM_STRLEN (NM_CONFIG_KEYFILE_KEYPREFIX_WAS)];
433
434                                 if (!g_key_file_has_key (keyfile_intern, group, key_base, NULL))
435                                         g_key_file_remove_key (keyfile, group, key_base, NULL);
436                                 continue;
437                         }
438                         if (!is_intern && !is_atomic && _HAS_PREFIX (key, NM_CONFIG_KEYFILE_KEYPREFIX_SET))
439                                 continue;
440
441                         value = g_key_file_get_value (keyfile_intern, group, key, NULL);
442                         g_key_file_set_value (keyfile, group, key, value);
443                 }
444         }
445         return keyfile;
446 }
447
448 /************************************************************************/
449
450 static int
451 _nm_config_data_log_sort (const char **pa, const char **pb, gpointer dummy)
452 {
453         gboolean a_is_connection, b_is_connection;
454         gboolean a_is_intern, b_is_intern;
455         const char *a = *pa;
456         const char *b = *pb;
457
458         /* we sort intern groups to the end. */
459         a_is_intern = g_str_has_prefix (a, NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN);
460         b_is_intern = g_str_has_prefix (b, NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN);
461
462         if (a_is_intern && b_is_intern)
463                 return 0;
464         if (a_is_intern)
465                 return 1;
466         if (b_is_intern)
467                 return -1;
468
469         /* we sort connection groups before intern groups (to the end). */
470         a_is_connection = a && g_str_has_prefix (a, NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION);
471         b_is_connection = b && g_str_has_prefix (b, NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION);
472
473         if (a_is_connection && b_is_connection) {
474                 /* if both are connection groups, we want the explicit [connection] group first. */
475                 a_is_connection = a[NM_STRLEN (NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION)] == '\0';
476                 b_is_connection = b[NM_STRLEN (NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION)] == '\0';
477
478                 if (a_is_connection != b_is_connection) {
479                         if (a_is_connection)
480                                 return -1;
481                         return 1;
482                 }
483                 /* the sections are ordered lowest-priority first. Reverse their order. */
484                 return pa < pb ? 1 : -1;
485         }
486         if (a_is_connection && !b_is_connection)
487                 return 1;
488         if (b_is_connection && !a_is_connection)
489                 return -1;
490
491         /* no reordering. */
492         return 0;
493 }
494
495 void
496 nm_config_data_log (const NMConfigData *self,
497                     const char *prefix,
498                     const char *key_prefix,
499                     /* FILE* */ gpointer print_stream)
500 {
501         NMConfigDataPrivate *priv;
502         gs_strfreev char **groups = NULL;
503         gsize ngroups;
504         guint g, k;
505         FILE *stream = print_stream;
506
507         g_return_if_fail (NM_IS_CONFIG_DATA (self));
508
509         if (!stream && !nm_logging_enabled (LOGL_DEBUG, LOGD_CORE))
510                 return;
511
512         if (!prefix)
513                 prefix = "";
514         if (!key_prefix)
515                 key_prefix = "";
516
517 #define _LOG(stream, prefix, ...) \
518         G_STMT_START { \
519                 if (!stream) \
520                         _nm_log (LOGL_DEBUG, LOGD_CORE, 0, "%s"_NM_UTILS_MACRO_FIRST(__VA_ARGS__)"%s", prefix _NM_UTILS_MACRO_REST (__VA_ARGS__), ""); \
521                 else \
522                         fprintf (stream, "%s"_NM_UTILS_MACRO_FIRST(__VA_ARGS__)"%s", prefix _NM_UTILS_MACRO_REST (__VA_ARGS__), "\n"); \
523         } G_STMT_END
524
525         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
526
527         groups = g_key_file_get_groups (priv->keyfile, &ngroups);
528         if (!groups)
529                 ngroups = 0;
530
531         if (groups && groups[0]) {
532                 g_qsort_with_data (groups, ngroups,
533                                    sizeof (char *),
534                                    (GCompareDataFunc) _nm_config_data_log_sort,
535                                    NULL);
536         }
537
538         if (!stream)
539                 _LOG (stream, prefix, "config-data[%p]: %lu groups", self, (unsigned long) ngroups);
540
541         for (g = 0; g < ngroups; g++) {
542                 const char *group = groups[g];
543                 gs_strfreev char **keys = NULL;
544                 gboolean is_atomic;
545
546                 is_atomic = nm_config_data_is_intern_atomic_group (self, group);
547
548                 _LOG (stream, prefix, "");
549                 _LOG (stream, prefix, "[%s]%s", group, is_atomic && !stream ? " # atomic section" : "");
550
551                 keys = g_key_file_get_keys (priv->keyfile, group, NULL, NULL);
552                 for (k = 0; keys && keys[k]; k++) {
553                         const char *key = keys[k];
554                         gs_free char *value = NULL;
555
556                         value = g_key_file_get_value (priv->keyfile, group, key, NULL);
557                         _LOG (stream, prefix, "%s%s=%s", key_prefix, key, value);
558                 }
559         }
560
561 #undef _LOG
562 }
563
564 /************************************************************************/
565
566 const char *const *
567 nm_global_dns_config_get_searches (const NMGlobalDnsConfig *dns)
568 {
569         g_return_val_if_fail (dns, NULL);
570
571         return (const char *const *) dns->searches;
572 }
573
574 const char *const *
575 nm_global_dns_config_get_options (const NMGlobalDnsConfig *dns)
576 {
577         g_return_val_if_fail (dns, NULL);
578
579         return (const char *const *) dns->options;
580 }
581
582 guint
583 nm_global_dns_config_get_num_domains (const NMGlobalDnsConfig *dns)
584 {
585         g_return_val_if_fail (dns, 0);
586         g_return_val_if_fail (dns->domains, 0);
587
588         return g_hash_table_size (dns->domains);
589 }
590
591 NMGlobalDnsDomain *
592 nm_global_dns_config_get_domain (const NMGlobalDnsConfig *dns, guint i)
593 {
594         NMGlobalDnsDomain *domain;
595
596         g_return_val_if_fail (dns, NULL);
597         g_return_val_if_fail (dns->domains, NULL);
598         g_return_val_if_fail (dns->domain_list, NULL);
599         g_return_val_if_fail (i < g_strv_length (dns->domain_list), NULL);
600
601         domain = g_hash_table_lookup (dns->domains, dns->domain_list[i]);
602         g_return_val_if_fail (domain, NULL);
603
604         return domain;
605 }
606
607 NMGlobalDnsDomain *nm_global_dns_config_lookup_domain (const NMGlobalDnsConfig *dns, const char *name)
608 {
609         g_return_val_if_fail (dns, NULL);
610         g_return_val_if_fail (dns->domains, NULL);
611         g_return_val_if_fail (name, NULL);
612
613         return g_hash_table_lookup (dns->domains, name);
614 }
615
616 const char *
617 nm_global_dns_domain_get_name (const NMGlobalDnsDomain *domain)
618 {
619         g_return_val_if_fail (domain, NULL);
620
621         return (const char *) domain->name;
622 }
623
624 const char *const *
625 nm_global_dns_domain_get_servers (const NMGlobalDnsDomain *domain)
626 {
627         g_return_val_if_fail (domain, NULL);
628
629         return (const char *const *) domain->servers;
630 }
631
632 const char *const *
633 nm_global_dns_domain_get_options (const NMGlobalDnsDomain *domain)
634 {
635         g_return_val_if_fail (domain, NULL);
636         return (const char *const *) domain->options;
637 }
638
639 gboolean
640 nm_global_dns_config_is_internal (const NMGlobalDnsConfig *dns)
641 {
642         return dns->internal;
643 }
644
645 gboolean
646 nm_global_dns_config_is_empty (const NMGlobalDnsConfig *dns)
647 {
648         g_return_val_if_fail (dns, TRUE);
649         g_return_val_if_fail (dns->domains, TRUE);
650
651         return    (!dns->searches || g_strv_length (dns->searches) == 0)
652                && (!dns->options || g_strv_length (dns->options) == 0)
653                && g_hash_table_size (dns->domains) == 0;
654 }
655
656 void
657 nm_global_dns_config_update_checksum (const NMGlobalDnsConfig *dns, GChecksum *sum)
658 {
659         NMGlobalDnsDomain *domain;
660         GList *keys, *key;
661         guint i;
662
663         g_return_if_fail (dns);
664         g_return_if_fail (dns->domains);
665         g_return_if_fail (sum);
666
667         for (i = 0; dns->searches && dns->searches[i]; i++)
668                 g_checksum_update (sum, (guchar *) dns->searches[i], strlen (dns->searches[i]));
669         for (i = 0; dns->options && dns->options[i]; i++)
670                 g_checksum_update (sum, (guchar *) dns->options[i], strlen (dns->options[i]));
671
672         keys = g_list_sort (g_hash_table_get_keys (dns->domains), (GCompareFunc) strcmp);
673         for (key = keys; key; key = g_list_next (key)) {
674
675                 domain = g_hash_table_lookup (dns->domains, key->data);
676                 g_assert (domain != NULL);
677                 g_checksum_update (sum, (guchar *) domain->name, strlen (domain->name));
678
679                 for (i = 0; domain->servers && domain->servers[i]; i++)
680                         g_checksum_update (sum, (guchar *) domain->servers[i], strlen (domain->servers[i]));
681                 for (i = 0; domain->options && domain->options[i]; i++)
682                         g_checksum_update (sum, (guchar *) domain->options[i], strlen (domain->options[i]));
683         }
684         g_list_free (keys);
685 }
686
687 static void
688 global_dns_domain_free (NMGlobalDnsDomain  *domain)
689 {
690         if (domain) {
691                 g_free (domain->name);
692                 g_strfreev (domain->servers);
693                 g_strfreev (domain->options);
694                 g_free (domain);
695         }
696 }
697
698 void
699 nm_global_dns_config_free (NMGlobalDnsConfig *conf)
700 {
701         if (conf) {
702                 g_strfreev (conf->searches);
703                 g_strfreev (conf->options);
704                 g_free (conf->domain_list);
705                 g_hash_table_unref (conf->domains);
706                 g_free (conf);
707         }
708 }
709
710 NMGlobalDnsConfig *
711 nm_config_data_get_global_dns_config (const NMConfigData *self)
712 {
713         g_return_val_if_fail (NM_IS_CONFIG_DATA (self), NULL);
714
715         return NM_CONFIG_DATA_GET_PRIVATE (self)->global_dns;
716 }
717
718 static void
719 global_dns_config_update_domain_list (NMGlobalDnsConfig *dns)
720 {
721         guint length;
722
723         g_free (dns->domain_list);
724         dns->domain_list = (char **) g_hash_table_get_keys_as_array (dns->domains, &length);
725 }
726
727 static NMGlobalDnsConfig *
728 load_global_dns (GKeyFile *keyfile, gboolean internal)
729 {
730         NMGlobalDnsConfig *conf;
731         char *group, *domain_prefix;
732         gs_strfreev char **groups = NULL;
733         int g, i, j, domain_prefix_len;
734         gboolean default_found = FALSE;
735         char **strv;
736
737         group = internal
738                 ? NM_CONFIG_KEYFILE_GROUP_INTERN_GLOBAL_DNS
739                 : NM_CONFIG_KEYFILE_GROUP_GLOBAL_DNS;
740         domain_prefix = internal
741                         ? NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN_GLOBAL_DNS_DOMAIN
742                         : NM_CONFIG_KEYFILE_GROUPPREFIX_GLOBAL_DNS_DOMAIN;
743         domain_prefix_len = strlen (domain_prefix);
744
745         if (!nm_config_keyfile_has_global_dns_config (keyfile, internal))
746                 return NULL;
747
748         conf = g_malloc0 (sizeof (NMGlobalDnsConfig));
749         conf->domains = g_hash_table_new_full (g_str_hash, g_str_equal,
750                                                g_free, (GDestroyNotify) global_dns_domain_free);
751
752         strv = g_key_file_get_string_list (keyfile, group, "searches", NULL, NULL);
753         if (strv)
754                 conf->searches = _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
755
756         strv = g_key_file_get_string_list (keyfile, group, "options", NULL, NULL);
757         if (strv) {
758                 _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
759                 for (i = 0, j = 0; strv[i]; i++) {
760                         if (_nm_utils_dns_option_validate (strv[i], NULL, NULL, TRUE, NULL))
761                                 strv[j++] = strv[i];
762                         else
763                                 g_free (strv[i]);
764                 }
765                 strv[j] = NULL;
766                 conf->options = strv;
767         }
768
769         groups = g_key_file_get_groups (keyfile, NULL);
770         for (g = 0; groups[g]; g++) {
771                 char *name;
772                 char **servers = NULL, **options = NULL;
773                 NMGlobalDnsDomain *domain;
774
775                 if (   !g_str_has_prefix (groups[g], domain_prefix)
776                     || !groups[g][domain_prefix_len])
777                         continue;
778
779                 strv = g_key_file_get_string_list (keyfile, groups[g], "servers", NULL, NULL);
780                 if (strv) {
781                         _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
782                         for (i = 0, j = 0; strv[i]; i++) {
783                                 if (   nm_utils_ipaddr_valid (AF_INET, strv[i])
784                                     || nm_utils_ipaddr_valid (AF_INET6, strv[i]))
785                                         strv[j++] = strv[i];
786                                 else
787                                         g_free (strv[i]);
788                         }
789                         if (j) {
790                                 strv[j] = NULL;
791                                 servers = strv;
792                         }
793                         else
794                                 g_free (strv);
795                 }
796
797                 if (!servers)
798                         continue;
799
800                 strv = g_key_file_get_string_list (keyfile, groups[g], "options", NULL, NULL);
801                 if (strv)
802                         options = _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
803
804                 name = strdup (&groups[g][domain_prefix_len]);
805                 domain = g_malloc0 (sizeof (NMGlobalDnsDomain));
806                 domain->name = name;
807                 domain->servers = servers;
808                 domain->options = options;
809
810                 g_hash_table_insert (conf->domains, strdup (name), domain);
811
812                 if (!strcmp (name, "*"))
813                         default_found = TRUE;
814         }
815
816         if (!default_found) {
817                 nm_log_dbg (LOGD_CORE, "%s global DNS configuration is missing default domain, ignore it",
818                             internal ? "internal" : "user");
819                 nm_global_dns_config_free (conf);
820                 return NULL;
821         }
822
823         conf->internal = internal;
824         global_dns_config_update_domain_list (conf);
825         return conf;
826 }
827
828
829 void
830 nm_global_dns_config_to_dbus (const NMGlobalDnsConfig *dns, GValue *value)
831 {
832         GVariantBuilder conf_builder, domains_builder, domain_builder;
833         NMGlobalDnsDomain *domain;
834         GHashTableIter iter;
835
836         g_variant_builder_init (&conf_builder, G_VARIANT_TYPE ("a{sv}"));
837         if (!dns)
838                 goto out;
839
840         if (dns->searches) {
841                 g_variant_builder_add (&conf_builder, "{sv}", "searches",
842                                        g_variant_new_strv ((const char *const *) dns->searches, -1));
843         }
844
845         if (dns->options) {
846                 g_variant_builder_add (&conf_builder, "{sv}", "options",
847                                        g_variant_new_strv ((const char *const *) dns->options, -1));
848         }
849
850         g_variant_builder_init (&domains_builder, G_VARIANT_TYPE ("a{sv}"));
851
852         g_hash_table_iter_init (&iter, dns->domains);
853         while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &domain)) {
854
855                 g_variant_builder_init (&domain_builder, G_VARIANT_TYPE ("a{sv}"));
856
857                 if (domain->servers) {
858                         g_variant_builder_add (&domain_builder, "{sv}", "servers",
859                                                g_variant_new_strv ((const char *const *) domain->servers, -1));
860                 }
861                 if (domain->options) {
862                         g_variant_builder_add (&domain_builder, "{sv}", "options",
863                                                g_variant_new_strv ((const char *const *) domain->options, -1));
864                 }
865
866                 g_variant_builder_add (&domains_builder, "{sv}", domain->name,
867                                        g_variant_builder_end (&domain_builder));
868         }
869
870         g_variant_builder_add (&conf_builder, "{sv}", "domains",
871                                g_variant_builder_end (&domains_builder));
872 out:
873         g_value_take_variant (value, g_variant_builder_end (&conf_builder));
874 }
875
876 static NMGlobalDnsDomain *
877 global_dns_domain_from_dbus (char *name, GVariant *variant)
878 {
879         NMGlobalDnsDomain *domain;
880         GVariantIter iter;
881         char **strv, *key;
882         GVariant *val;
883         int i, j;
884
885         if (!g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{sv}")))
886                 return NULL;
887
888         domain = g_malloc0 (sizeof (NMGlobalDnsDomain));
889         domain->name = g_strdup (name);
890
891         g_variant_iter_init (&iter, variant);
892         while (g_variant_iter_next (&iter, "{&sv}", &key, &val)) {
893
894                 if (   !g_strcmp0 (key, "servers")
895                     && g_variant_is_of_type (val, G_VARIANT_TYPE ("as"))) {
896                         strv = g_variant_dup_strv (val, NULL);
897                         _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
898                         for (i = 0, j = 0; strv && strv[i]; i++) {
899                                 if (   nm_utils_ipaddr_valid (AF_INET, strv[i])
900                                     || nm_utils_ipaddr_valid (AF_INET6, strv[i]))
901                                         strv[j++] = strv[i];
902                                 else
903                                         g_free (strv[i]);
904                         }
905                         if (j) {
906                                 strv[j] = NULL;
907                                 domain->servers = strv;
908                         } else
909                                 g_free (strv);
910                 } else if (   !g_strcmp0 (key, "options")
911                            && g_variant_is_of_type (val, G_VARIANT_TYPE ("as"))) {
912                         strv = g_variant_dup_strv (val, NULL);
913                         domain->options = _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
914                 }
915
916                 g_variant_unref (val);
917         }
918
919         /* At least one server is required */
920         if (!domain->servers) {
921                 global_dns_domain_free (domain);
922                 return NULL;
923         }
924
925         return domain;
926 }
927
928 NMGlobalDnsConfig *
929 nm_global_dns_config_from_dbus (const GValue *value, GError **error)
930 {
931         NMGlobalDnsConfig *dns_config;
932         GVariant *variant, *val;
933         GVariantIter iter;
934         char **strv, *key;
935         int i, j;
936
937         if (!G_VALUE_HOLDS_VARIANT (value)) {
938                 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
939                              "invalid value type");
940                 return NULL;
941         }
942
943         variant = g_value_get_variant (value);
944         if (!g_variant_is_of_type (variant, G_VARIANT_TYPE ("a{sv}"))) {
945                 g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
946                              "invalid variant type");
947                 return NULL;
948         }
949
950         dns_config = g_malloc0 (sizeof (NMGlobalDnsConfig));
951         dns_config->domains = g_hash_table_new_full (g_str_hash, g_str_equal,
952                                                      g_free, (GDestroyNotify) global_dns_domain_free);
953
954         g_variant_iter_init (&iter, variant);
955         while (g_variant_iter_next (&iter, "{&sv}", &key, &val)) {
956
957                 if (   !g_strcmp0 (key, "searches")
958                     && g_variant_is_of_type (val, G_VARIANT_TYPE ("as"))) {
959                         strv = g_variant_dup_strv (val, NULL);
960                         dns_config->searches = _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
961                 } else if (   !g_strcmp0 (key, "options")
962                            && g_variant_is_of_type (val, G_VARIANT_TYPE ("as"))) {
963                         strv = g_variant_dup_strv (val, NULL);
964                         _nm_utils_strv_cleanup (strv, TRUE, TRUE, TRUE);
965
966                         for (i = 0, j = 0; strv && strv[i]; i++) {
967                                 if (_nm_utils_dns_option_validate (strv[i], NULL, NULL, TRUE, NULL))
968                                         strv[j++] = strv[i];
969                                 else
970                                         g_free (strv[i]);
971                         }
972
973                         if (strv)
974                                 strv[j] = NULL;
975
976                         dns_config->options = strv;
977                 } else if (   !g_strcmp0 (key, "domains")
978                            && g_variant_is_of_type (val, G_VARIANT_TYPE ("a{sv}"))) {
979                         NMGlobalDnsDomain *domain;
980                         GVariantIter domain_iter;
981                         GVariant *v;
982                         char *k;
983
984                         g_variant_iter_init (&domain_iter, val);
985                         while (g_variant_iter_next (&domain_iter, "{&sv}", &k, &v)) {
986                                 if (k) {
987                                         domain = global_dns_domain_from_dbus (k, v);
988                                         if (domain)
989                                                 g_hash_table_insert (dns_config->domains, strdup (k), domain);
990                                 }
991                                 g_variant_unref (v);
992                         }
993                 }
994                 g_variant_unref (val);
995         }
996
997         /* An empty value is valid and clears the internal configuration */
998         if (   !nm_global_dns_config_is_empty (dns_config)
999             && !nm_global_dns_config_lookup_domain (dns_config, "*")) {
1000                 g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED,
1001                                      "Global DNS configuration is missing the default domain");
1002                 nm_global_dns_config_free (dns_config);
1003                 return NULL;
1004         }
1005
1006         global_dns_config_update_domain_list (dns_config);
1007         return dns_config;
1008 }
1009
1010 static gboolean
1011 global_dns_equal (NMGlobalDnsConfig *old, NMGlobalDnsConfig *new)
1012 {
1013         NMGlobalDnsDomain *domain_old, *domain_new;
1014         gpointer key, value_old, value_new;
1015         GHashTableIter iter;
1016
1017         if (old == new)
1018                 return TRUE;
1019
1020         if (!old || !new)
1021                 return FALSE;
1022
1023         if (   !_nm_utils_strv_equal (old->options, new->options)
1024             || !_nm_utils_strv_equal (old->searches, new->searches))
1025                 return FALSE;
1026
1027         if ((!old->domains || !new->domains) && old->domains != new->domains)
1028                 return FALSE;
1029
1030         if (g_hash_table_size (old->domains) != g_hash_table_size (new->domains))
1031                 return FALSE;
1032
1033         g_hash_table_iter_init (&iter, old->domains);
1034         while (g_hash_table_iter_next (&iter, &key, &value_old)) {
1035                 value_new = g_hash_table_lookup (new->domains, key);
1036                 if (!value_new)
1037                         return FALSE;
1038
1039                 domain_old = value_old;
1040                 domain_new = value_new;
1041
1042                 if (   !_nm_utils_strv_equal (domain_old->options, domain_new->options)
1043                     || !_nm_utils_strv_equal (domain_old->servers, domain_new->servers))
1044                         return FALSE;
1045         }
1046
1047         return TRUE;
1048 }
1049
1050 /************************************************************************/
1051
1052 char *
1053 nm_config_data_get_connection_default (const NMConfigData *self,
1054                                        const char *property,
1055                                        NMDevice *device)
1056 {
1057         NMConfigDataPrivate *priv;
1058         const ConnectionInfo *connection_info;
1059
1060         g_return_val_if_fail (self, NULL);
1061         g_return_val_if_fail (property && *property, NULL);
1062         g_return_val_if_fail (strchr (property, '.'), NULL);
1063
1064         priv = NM_CONFIG_DATA_GET_PRIVATE (self);
1065
1066         if (!priv->connection_infos)
1067                 return NULL;
1068
1069         for (connection_info = &priv->connection_infos[0]; connection_info->group_name; connection_info++) {
1070                 char *value;
1071                 gboolean match;
1072
1073                 /* FIXME: Here we use g_key_file_get_string(). This should be in sync with what keyfile-reader
1074                  * does.
1075                  *
1076                  * Unfortunately that is currently not possible because keyfile-reader does the two steps
1077                  * string_to_value(keyfile_to_string(keyfile)) in one. Optimally, keyfile library would
1078                  * expose both functions, and we would return here keyfile_to_string(keyfile).
1079                  * The caller then could convert the string to the proper value via string_to_value(value). */
1080                 value = g_key_file_get_string (priv->keyfile, connection_info->group_name, property, NULL);
1081                 if (!value && !connection_info->stop_match)
1082                         continue;
1083
1084                 match = TRUE;
1085                 if (connection_info->match_device.has)
1086                         match = device && nm_device_spec_match_list (device, connection_info->match_device.spec);
1087
1088                 if (match)
1089                         return value;
1090                 g_free (value);
1091         }
1092         return NULL;
1093 }
1094
1095 static void
1096 _get_connection_info_init (ConnectionInfo *connection_info, GKeyFile *keyfile, char *group)
1097 {
1098         /* pass ownership of @group on... */
1099         connection_info->group_name = group;
1100
1101         connection_info->match_device.spec = nm_config_get_match_spec (keyfile,
1102                                                                        group,
1103                                                                        "match-device",
1104                                                                        &connection_info->match_device.has);
1105         connection_info->stop_match = nm_config_keyfile_get_boolean (keyfile, group, "stop-match", FALSE);
1106 }
1107
1108 static ConnectionInfo *
1109 _get_connection_infos (GKeyFile *keyfile)
1110 {
1111         char **groups;
1112         gsize i, j, ngroups;
1113         char *connection_tag = NULL;
1114         ConnectionInfo *connection_infos = NULL;
1115
1116         /* get the list of existing [connection.\+] sections that we consider
1117          * for nm_config_data_get_connection_default().
1118          *
1119          * We expect the sections in their right order, with lowest priority
1120          * first. Only exception is the (literal) [connection] section, which
1121          * we will always reorder to the end. */
1122         groups = g_key_file_get_groups (keyfile, &ngroups);
1123         if (!groups)
1124                 ngroups = 0;
1125         else if (ngroups > 0) {
1126                 for (i = 0, j = 0; i < ngroups; i++) {
1127                         if (g_str_has_prefix (groups[i], NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION)) {
1128                                 if (groups[i][NM_STRLEN (NM_CONFIG_KEYFILE_GROUPPREFIX_CONNECTION)] == '\0')
1129                                         connection_tag = groups[i];
1130                                 else
1131                                         groups[j++] = groups[i];
1132                         } else
1133                                 g_free (groups[i]);
1134                 }
1135                 ngroups = j;
1136         }
1137
1138         connection_infos = g_new0 (ConnectionInfo, ngroups + 1 + (connection_tag ? 1 : 0));
1139         for (i = 0; i < ngroups; i++) {
1140                 /* pass ownership of @group on... */
1141                 _get_connection_info_init (&connection_infos[i], keyfile, groups[ngroups - i - 1]);
1142         }
1143         if (connection_tag) {
1144                 /* pass ownership of @connection_tag on... */
1145                 _get_connection_info_init (&connection_infos[i], keyfile, connection_tag);
1146         }
1147         g_free (groups);
1148
1149         return connection_infos;
1150 }
1151
1152 /************************************************************************/
1153
1154 static gboolean
1155 _slist_str_equals (GSList *a, GSList *b)
1156 {
1157         while (a && b && g_strcmp0 (a->data, b->data) == 0) {
1158                 a = a->next;
1159                 b = b->next;
1160         }
1161         return !a && !b;
1162 }
1163
1164 NMConfigChangeFlags
1165 nm_config_data_diff (NMConfigData *old_data, NMConfigData *new_data)
1166 {
1167         NMConfigChangeFlags changes = NM_CONFIG_CHANGE_NONE;
1168         NMConfigDataPrivate *priv_old, *priv_new;
1169
1170         g_return_val_if_fail (NM_IS_CONFIG_DATA (old_data), NM_CONFIG_CHANGE_NONE);
1171         g_return_val_if_fail (NM_IS_CONFIG_DATA (new_data), NM_CONFIG_CHANGE_NONE);
1172
1173         priv_old = NM_CONFIG_DATA_GET_PRIVATE (old_data);
1174         priv_new = NM_CONFIG_DATA_GET_PRIVATE (new_data);
1175
1176         if (!_nm_keyfile_equals (priv_old->keyfile_user, priv_new->keyfile_user, TRUE))
1177                 changes |= NM_CONFIG_CHANGE_VALUES | NM_CONFIG_CHANGE_VALUES_USER;
1178
1179         if (!_nm_keyfile_equals (priv_old->keyfile_intern, priv_new->keyfile_intern, TRUE))
1180                 changes |= NM_CONFIG_CHANGE_VALUES | NM_CONFIG_CHANGE_VALUES_INTERN;
1181
1182         if (   g_strcmp0 (nm_config_data_get_config_main_file (old_data), nm_config_data_get_config_main_file (new_data)) != 0
1183             || g_strcmp0 (nm_config_data_get_config_description (old_data), nm_config_data_get_config_description (new_data)) != 0)
1184                 changes |= NM_CONFIG_CHANGE_CONFIG_FILES;
1185
1186         if (   nm_config_data_get_connectivity_interval (old_data) != nm_config_data_get_connectivity_interval (new_data)
1187             || g_strcmp0 (nm_config_data_get_connectivity_uri (old_data), nm_config_data_get_connectivity_uri (new_data))
1188             || g_strcmp0 (nm_config_data_get_connectivity_response (old_data), nm_config_data_get_connectivity_response (new_data)))
1189                 changes |= NM_CONFIG_CHANGE_CONNECTIVITY;
1190
1191         if (   !_slist_str_equals (priv_old->no_auto_default.specs, priv_new->no_auto_default.specs)
1192             || !_slist_str_equals (priv_old->no_auto_default.specs_config, priv_new->no_auto_default.specs_config))
1193                 changes |= NM_CONFIG_CHANGE_NO_AUTO_DEFAULT;
1194
1195         if (g_strcmp0 (nm_config_data_get_dns_mode (old_data), nm_config_data_get_dns_mode (new_data)))
1196                 changes |= NM_CONFIG_CHANGE_DNS_MODE;
1197
1198         if (g_strcmp0 (nm_config_data_get_rc_manager (old_data), nm_config_data_get_rc_manager (new_data)))
1199                 changes |= NM_CONFIG_CHANGE_RC_MANAGER;
1200
1201         if (!global_dns_equal (priv_old->global_dns, priv_new->global_dns))
1202                 changes |= NM_CONFIG_CHANGE_GLOBAL_DNS_CONFIG;
1203
1204         return changes;
1205 }
1206
1207 /************************************************************************/
1208
1209 static void
1210 get_property (GObject *object,
1211               guint prop_id,
1212               GValue *value,
1213               GParamSpec *pspec)
1214 {
1215         NMConfigData *self = NM_CONFIG_DATA (object);
1216
1217         switch (prop_id) {
1218         case PROP_CONFIG_MAIN_FILE:
1219                 g_value_set_string (value, nm_config_data_get_config_main_file (self));
1220                 break;
1221         case PROP_CONFIG_DESCRIPTION:
1222                 g_value_set_string (value, nm_config_data_get_config_description (self));
1223                 break;
1224         case PROP_CONNECTIVITY_URI:
1225                 g_value_set_string (value, nm_config_data_get_connectivity_uri (self));
1226                 break;
1227         case PROP_CONNECTIVITY_INTERVAL:
1228                 g_value_set_uint (value, nm_config_data_get_connectivity_interval (self));
1229                 break;
1230         case PROP_CONNECTIVITY_RESPONSE:
1231                 g_value_set_string (value, nm_config_data_get_connectivity_response (self));
1232                 break;
1233         default:
1234                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1235                 break;
1236         }
1237 }
1238
1239 static void
1240 set_property (GObject *object,
1241               guint prop_id,
1242               const GValue *value,
1243               GParamSpec *pspec)
1244 {
1245         NMConfigData *self = NM_CONFIG_DATA (object);
1246         NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (self);
1247
1248         /* This type is immutable. All properties are construct only. */
1249         switch (prop_id) {
1250         case PROP_CONFIG_MAIN_FILE:
1251                 priv->config_main_file = g_value_dup_string (value);
1252                 break;
1253         case PROP_CONFIG_DESCRIPTION:
1254                 priv->config_description = g_value_dup_string (value);
1255                 break;
1256         case PROP_KEYFILE_USER:
1257                 priv->keyfile_user = g_value_dup_boxed (value);
1258                 if (   priv->keyfile_user
1259                     && !_nm_keyfile_has_values (priv->keyfile_user)) {
1260                         g_key_file_unref (priv->keyfile_user);
1261                         priv->keyfile_user = NULL;
1262                 }
1263                 break;
1264         case PROP_KEYFILE_INTERN:
1265                 priv->keyfile_intern = g_value_dup_boxed (value);
1266                 if (   priv->keyfile_intern
1267                     && !_nm_keyfile_has_values (priv->keyfile_intern)) {
1268                         g_key_file_unref (priv->keyfile_intern);
1269                         priv->keyfile_intern = NULL;
1270                 }
1271                 break;
1272         case PROP_NO_AUTO_DEFAULT:
1273                 {
1274                         char **value_arr = g_value_get_boxed (value);
1275                         guint i, j = 0;
1276
1277                         priv->no_auto_default.arr = g_new (char *, g_strv_length (value_arr) + 1);
1278                         priv->no_auto_default.specs = NULL;
1279
1280                         for (i = 0; value_arr && value_arr[i]; i++) {
1281                                 if (   *value_arr[i]
1282                                     && nm_utils_hwaddr_valid (value_arr[i], -1)
1283                                     && _nm_utils_strv_find_first (value_arr, i, value_arr[i]) < 0) {
1284                                         priv->no_auto_default.arr[j++] = g_strdup (value_arr[i]);
1285                                         priv->no_auto_default.specs = g_slist_prepend (priv->no_auto_default.specs, g_strdup_printf ("mac:%s", value_arr[i]));
1286                                 }
1287                         }
1288                         priv->no_auto_default.arr[j++] = NULL;
1289                         priv->no_auto_default.specs = g_slist_reverse (priv->no_auto_default.specs);
1290                 }
1291                 break;
1292         default:
1293                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1294                 break;
1295         }
1296 }
1297
1298 static void
1299 dispose (GObject *object)
1300 {
1301 }
1302
1303 static void
1304 finalize (GObject *gobject)
1305 {
1306         NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (gobject);
1307         guint i;
1308
1309         g_free (priv->config_main_file);
1310         g_free (priv->config_description);
1311
1312         g_free (priv->connectivity.uri);
1313         g_free (priv->connectivity.response);
1314
1315         g_slist_free_full (priv->no_auto_default.specs, g_free);
1316         g_slist_free_full (priv->no_auto_default.specs_config, g_free);
1317         g_strfreev (priv->no_auto_default.arr);
1318
1319         g_free (priv->dns_mode);
1320         g_free (priv->rc_manager);
1321
1322         g_slist_free_full (priv->ignore_carrier, g_free);
1323         g_slist_free_full (priv->assume_ipv6ll_only, g_free);
1324
1325         nm_global_dns_config_free (priv->global_dns);
1326
1327         if (priv->connection_infos) {
1328                 for (i = 0; priv->connection_infos[i].group_name; i++) {
1329                         g_free (priv->connection_infos[i].group_name);
1330                         g_slist_free_full (priv->connection_infos[i].match_device.spec, g_free);
1331                 }
1332                 g_free (priv->connection_infos);
1333         }
1334
1335         g_key_file_unref (priv->keyfile);
1336         if (priv->keyfile_user)
1337                 g_key_file_unref (priv->keyfile_user);
1338         if (priv->keyfile_intern)
1339                 g_key_file_unref (priv->keyfile_intern);
1340
1341         G_OBJECT_CLASS (nm_config_data_parent_class)->finalize (gobject);
1342
1343         g_free (priv->value_cached);
1344 }
1345
1346 static void
1347 nm_config_data_init (NMConfigData *self)
1348 {
1349 }
1350
1351 static void
1352 constructed (GObject *object)
1353 {
1354         NMConfigData *self = NM_CONFIG_DATA (object);
1355         NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (self);
1356         char *interval;
1357
1358         priv->keyfile = _merge_keyfiles (priv->keyfile_user, priv->keyfile_intern);
1359
1360         priv->connection_infos = _get_connection_infos (priv->keyfile);
1361
1362         priv->connectivity.uri = nm_strstrip (g_key_file_get_string (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_CONNECTIVITY, "uri", NULL));
1363         priv->connectivity.response = g_key_file_get_string (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_CONNECTIVITY, "response", NULL);
1364
1365         /* On missing config value, fallback to 300. On invalid value, disable connectivity checking by setting
1366          * the interval to zero. */
1367         interval = g_key_file_get_string (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_CONNECTIVITY, "interval", NULL);
1368         priv->connectivity.interval = interval
1369             ? _nm_utils_ascii_str_to_int64 (interval, 10, 0, G_MAXUINT, 0)
1370             : NM_CONFIG_DEFAULT_CONNECTIVITY_INTERVAL;
1371         g_free (interval);
1372
1373         priv->dns_mode = nm_strstrip (g_key_file_get_string (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_MAIN, "dns", NULL));
1374         priv->rc_manager = nm_strstrip (g_key_file_get_string (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_MAIN, "rc-manager", NULL));
1375
1376         priv->ignore_carrier = nm_config_get_match_spec (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_MAIN, "ignore-carrier", NULL);
1377         priv->assume_ipv6ll_only = nm_config_get_match_spec (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_MAIN, "assume-ipv6ll-only", NULL);
1378
1379         priv->no_auto_default.specs_config = nm_config_get_match_spec (priv->keyfile, NM_CONFIG_KEYFILE_GROUP_MAIN, "no-auto-default", NULL);
1380
1381         priv->global_dns = load_global_dns (priv->keyfile_user, FALSE);
1382         if (!priv->global_dns)
1383                 priv->global_dns = load_global_dns (priv->keyfile_intern, TRUE);
1384
1385         G_OBJECT_CLASS (nm_config_data_parent_class)->constructed (object);
1386 }
1387
1388 NMConfigData *
1389 nm_config_data_new (const char *config_main_file,
1390                     const char *config_description,
1391                     const char *const*no_auto_default,
1392                     GKeyFile *keyfile_user,
1393                     GKeyFile *keyfile_intern)
1394 {
1395         return g_object_new (NM_TYPE_CONFIG_DATA,
1396                              NM_CONFIG_DATA_CONFIG_MAIN_FILE, config_main_file,
1397                              NM_CONFIG_DATA_CONFIG_DESCRIPTION, config_description,
1398                              NM_CONFIG_DATA_KEYFILE_USER, keyfile_user,
1399                              NM_CONFIG_DATA_KEYFILE_INTERN, keyfile_intern,
1400                              NM_CONFIG_DATA_NO_AUTO_DEFAULT, no_auto_default,
1401                              NULL);
1402 }
1403
1404 NMConfigData *
1405 nm_config_data_new_update_keyfile_intern (const NMConfigData *base, GKeyFile *keyfile_intern)
1406 {
1407         NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (base);
1408
1409         return g_object_new (NM_TYPE_CONFIG_DATA,
1410                              NM_CONFIG_DATA_CONFIG_MAIN_FILE, priv->config_main_file,
1411                              NM_CONFIG_DATA_CONFIG_DESCRIPTION, priv->config_description,
1412                              NM_CONFIG_DATA_KEYFILE_USER, priv->keyfile_user, /* the keyfile is unchanged. It's safe to share it. */
1413                              NM_CONFIG_DATA_KEYFILE_INTERN, keyfile_intern,
1414                              NM_CONFIG_DATA_NO_AUTO_DEFAULT, priv->no_auto_default.arr,
1415                              NULL);
1416 }
1417
1418 NMConfigData *
1419 nm_config_data_new_update_no_auto_default (const NMConfigData *base,
1420                                            const char *const*no_auto_default)
1421 {
1422         NMConfigDataPrivate *priv = NM_CONFIG_DATA_GET_PRIVATE (base);
1423
1424         return g_object_new (NM_TYPE_CONFIG_DATA,
1425                              NM_CONFIG_DATA_CONFIG_MAIN_FILE, priv->config_main_file,
1426                              NM_CONFIG_DATA_CONFIG_DESCRIPTION, priv->config_description,
1427                              NM_CONFIG_DATA_KEYFILE_USER, priv->keyfile_user, /* the keyfile is unchanged. It's safe to share it. */
1428                              NM_CONFIG_DATA_KEYFILE_INTERN, priv->keyfile_intern,
1429                              NM_CONFIG_DATA_NO_AUTO_DEFAULT, no_auto_default,
1430                              NULL);
1431 }
1432
1433 static void
1434 nm_config_data_class_init (NMConfigDataClass *config_class)
1435 {
1436         GObjectClass *object_class = G_OBJECT_CLASS (config_class);
1437
1438         g_type_class_add_private (config_class, sizeof (NMConfigDataPrivate));
1439
1440         object_class->constructed = constructed;
1441         object_class->dispose = dispose;
1442         object_class->finalize = finalize;
1443         object_class->get_property = get_property;
1444         object_class->set_property = set_property;
1445
1446         g_object_class_install_property
1447             (object_class, PROP_CONFIG_MAIN_FILE,
1448              g_param_spec_string (NM_CONFIG_DATA_CONFIG_MAIN_FILE, "", "",
1449                                   NULL,
1450                                   G_PARAM_READWRITE |
1451                                   G_PARAM_CONSTRUCT_ONLY |
1452                                   G_PARAM_STATIC_STRINGS));
1453
1454         g_object_class_install_property
1455             (object_class, PROP_CONFIG_DESCRIPTION,
1456              g_param_spec_string (NM_CONFIG_DATA_CONFIG_DESCRIPTION, "", "",
1457                                   NULL,
1458                                   G_PARAM_READWRITE |
1459                                   G_PARAM_CONSTRUCT_ONLY |
1460                                   G_PARAM_STATIC_STRINGS));
1461
1462         g_object_class_install_property
1463               (object_class, PROP_KEYFILE_USER,
1464                g_param_spec_boxed (NM_CONFIG_DATA_KEYFILE_USER, "", "",
1465                                    G_TYPE_KEY_FILE,
1466                                    G_PARAM_WRITABLE |
1467                                    G_PARAM_CONSTRUCT_ONLY |
1468                                    G_PARAM_STATIC_STRINGS));
1469
1470         g_object_class_install_property
1471               (object_class, PROP_KEYFILE_INTERN,
1472                g_param_spec_boxed (NM_CONFIG_DATA_KEYFILE_INTERN, "", "",
1473                                    G_TYPE_KEY_FILE,
1474                                    G_PARAM_WRITABLE |
1475                                    G_PARAM_CONSTRUCT_ONLY |
1476                                    G_PARAM_STATIC_STRINGS));
1477
1478         g_object_class_install_property
1479             (object_class, PROP_CONNECTIVITY_URI,
1480              g_param_spec_string (NM_CONFIG_DATA_CONNECTIVITY_URI, "", "",
1481                                   NULL,
1482                                   G_PARAM_READABLE |
1483                                   G_PARAM_STATIC_STRINGS));
1484
1485         g_object_class_install_property
1486             (object_class, PROP_CONNECTIVITY_INTERVAL,
1487              g_param_spec_uint (NM_CONFIG_DATA_CONNECTIVITY_INTERVAL, "", "",
1488                                 0, G_MAXUINT, 0,
1489                                 G_PARAM_READABLE |
1490                                 G_PARAM_STATIC_STRINGS));
1491
1492         g_object_class_install_property
1493             (object_class, PROP_CONNECTIVITY_RESPONSE,
1494              g_param_spec_string (NM_CONFIG_DATA_CONNECTIVITY_RESPONSE, "", "",
1495                                   NULL,
1496                                   G_PARAM_READABLE |
1497                                   G_PARAM_STATIC_STRINGS));
1498
1499         g_object_class_install_property
1500             (object_class, PROP_NO_AUTO_DEFAULT,
1501              g_param_spec_boxed (NM_CONFIG_DATA_NO_AUTO_DEFAULT, "", "",
1502                                  G_TYPE_STRV,
1503                                  G_PARAM_WRITABLE |
1504                                  G_PARAM_CONSTRUCT_ONLY |
1505                                  G_PARAM_STATIC_STRINGS));
1506
1507 }
1508