libnm-core: add _nm_simple_connection_new_from_dbus() function
[NetworkManager.git] / libnm-core / nm-connection.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2013 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26
27 #include "nm-connection.h"
28 #include "nm-connection-private.h"
29 #include "nm-utils.h"
30 #include "nm-setting-private.h"
31 #include "nm-core-internal.h"
32
33 /**
34  * SECTION:nm-connection
35  * @short_description: Describes a connection to specific network or provider
36  *
37  * An #NMConnection describes all the settings and configuration values that
38  * are necessary to configure network devices for operation on a specific
39  * network.  Connections are the fundamental operating object for
40  * NetworkManager; no device is connected without a #NMConnection, or
41  * disconnected without having been connected with a #NMConnection.
42  *
43  * Each #NMConnection contains a list of #NMSetting objects usually referenced
44  * by name (using nm_connection_get_setting_by_name()) or by type (with
45  * nm_connection_get_setting()).  The settings describe the actual parameters
46  * with which the network devices are configured, including device-specific
47  * parameters (MTU, SSID, APN, channel, rate, etc) and IP-level parameters
48  * (addresses, routes, addressing methods, etc).
49  *
50  */
51
52 typedef struct {
53         NMConnection *self;
54
55         GHashTable *settings;
56
57         /* D-Bus path of the connection, if any */
58         char *path;
59 } NMConnectionPrivate;
60
61 static NMConnectionPrivate *nm_connection_get_private (NMConnection *connection);
62 #define NM_CONNECTION_GET_PRIVATE(o) (nm_connection_get_private ((NMConnection *)o))
63
64 G_DEFINE_INTERFACE (NMConnection, nm_connection, G_TYPE_OBJECT)
65
66 enum {
67         SECRETS_UPDATED,
68         SECRETS_CLEARED,
69         CHANGED,
70         LAST_SIGNAL
71 };
72
73 static guint signals[LAST_SIGNAL] = { 0 };
74
75
76 static NMSettingVerifyResult _nm_connection_verify (NMConnection *connection, GError **error);
77
78
79 /*************************************************************/
80
81 static void
82 setting_changed_cb (NMSetting *setting,
83                     GParamSpec *pspec,
84                     NMConnection *self)
85 {
86         g_signal_emit (self, signals[CHANGED], 0);
87 }
88
89 static gboolean
90 _setting_release (gpointer key, gpointer value, gpointer user_data)
91 {
92         g_signal_handlers_disconnect_by_func (user_data, setting_changed_cb, value);
93         return TRUE;
94 }
95
96 static void
97 _nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
98 {
99         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
100         const char *name = G_OBJECT_TYPE_NAME (setting);
101         NMSetting *s_old;
102
103         if ((s_old = g_hash_table_lookup (priv->settings, (gpointer) name)))
104                 g_signal_handlers_disconnect_by_func (s_old, setting_changed_cb, connection);
105         g_hash_table_insert (priv->settings, (gpointer) name, setting);
106         /* Listen for property changes so we can emit the 'changed' signal */
107         g_signal_connect (setting, "notify", (GCallback) setting_changed_cb, connection);
108 }
109
110 /**
111  * nm_connection_add_setting:
112  * @connection: a #NMConnection
113  * @setting: (transfer full): the #NMSetting to add to the connection object
114  *
115  * Adds a #NMSetting to the connection, replacing any previous #NMSetting of the
116  * same name which has previously been added to the #NMConnection.  The
117  * connection takes ownership of the #NMSetting object and does not increase
118  * the setting object's reference count.
119  **/
120 void
121 nm_connection_add_setting (NMConnection *connection, NMSetting *setting)
122 {
123         g_return_if_fail (NM_IS_CONNECTION (connection));
124         g_return_if_fail (NM_IS_SETTING (setting));
125
126         _nm_connection_add_setting (connection, setting);
127         g_signal_emit (connection, signals[CHANGED], 0);
128 }
129
130 /**
131  * nm_connection_remove_setting:
132  * @connection: a #NMConnection
133  * @setting_type: the #GType of the setting object to remove
134  *
135  * Removes the #NMSetting with the given #GType from the #NMConnection.  This
136  * operation dereferences the #NMSetting object.
137  **/
138 void
139 nm_connection_remove_setting (NMConnection *connection, GType setting_type)
140 {
141         NMConnectionPrivate *priv;
142         NMSetting *setting;
143         const char *setting_name;
144
145         g_return_if_fail (NM_IS_CONNECTION (connection));
146         g_return_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING));
147
148         priv = NM_CONNECTION_GET_PRIVATE (connection);
149         setting_name = g_type_name (setting_type);
150         setting = g_hash_table_lookup (priv->settings, setting_name);
151         if (setting) {
152                 g_signal_handlers_disconnect_by_func (setting, setting_changed_cb, connection);
153                 g_hash_table_remove (priv->settings, setting_name);
154                 g_signal_emit (connection, signals[CHANGED], 0);
155         }
156 }
157
158 /**
159  * nm_connection_get_setting:
160  * @connection: a #NMConnection
161  * @setting_type: the #GType of the setting object to return
162  *
163  * Gets the #NMSetting with the given #GType, if one has been previously added
164  * to the #NMConnection.
165  *
166  * Returns: (transfer none): the #NMSetting, or %NULL if no setting of that type was previously
167  * added to the #NMConnection
168  **/
169 NMSetting *
170 nm_connection_get_setting (NMConnection *connection, GType setting_type)
171 {
172         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
173         g_return_val_if_fail (g_type_is_a (setting_type, NM_TYPE_SETTING), NULL);
174
175         return (NMSetting *) g_hash_table_lookup (NM_CONNECTION_GET_PRIVATE (connection)->settings,
176                                                   g_type_name (setting_type));
177 }
178
179 /**
180  * nm_connection_get_setting_by_name:
181  * @connection: a #NMConnection
182  * @name: a setting name
183  *
184  * Gets the #NMSetting with the given name, if one has been previously added
185  * the #NMConnection.
186  *
187  * Returns: (transfer none): the #NMSetting, or %NULL if no setting with that name was previously
188  * added to the #NMConnection
189  **/
190 NMSetting *
191 nm_connection_get_setting_by_name (NMConnection *connection, const char *name)
192 {
193         GType type;
194
195         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
196         g_return_val_if_fail (name != NULL, NULL);
197
198         type = nm_setting_lookup_type (name);
199
200         return type ? nm_connection_get_setting (connection, type) : NULL;
201 }
202
203 static gboolean
204 validate_permissions_type (GVariant *variant, GError **error)
205 {
206         GVariant *s_con;
207         GVariant *permissions;
208         gboolean valid = TRUE;
209
210         /* Ensure the connection::permissions item (if present) is the correct
211          * type, otherwise the g_object_set() will throw a warning and ignore the
212          * error, leaving us with no permissions.
213          */
214         s_con = g_variant_lookup_value (variant, NM_SETTING_CONNECTION_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
215         if (!s_con)
216                 return TRUE;
217
218         permissions = g_variant_lookup_value (s_con, NM_SETTING_CONNECTION_PERMISSIONS, NULL);
219         if (permissions) {
220                 if (!g_variant_is_of_type (permissions, G_VARIANT_TYPE_STRING_ARRAY)) {
221                         g_set_error_literal (error,
222                                              NM_CONNECTION_ERROR,
223                                              NM_CONNECTION_ERROR_INVALID_PROPERTY,
224                                              _("wrong type; should be a list of strings."));
225                         g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_PERMISSIONS);
226                         valid = FALSE;
227                 }
228                 g_variant_unref (permissions);
229         }
230
231         g_variant_unref (s_con);
232         return valid;
233 }
234
235 /**
236  * _nm_connection_replace_settings:
237  * @connection: a #NMConnection
238  * @new_settings: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION, with the new settings
239  * @parse_flags: flags.
240  * @error: location to store error, or %NULL
241  *
242  * Replaces @connection's settings with @new_settings (which must be
243  * syntactically valid, and describe a known type of connection, but does not
244  * need to result in a connection that passes nm_connection_verify()).
245  *
246  * Returns: %TRUE if connection was updated, %FALSE if @new_settings could not
247  *   be deserialized (in which case @connection will be unchanged).
248  *   Only exception is the NM_SETTING_PARSE_FLAGS_NORMALIZE flag: if normalization
249  *   fails, the input @connection is already modified and the original settings
250  *   are lost.
251  **/
252 gboolean
253 _nm_connection_replace_settings (NMConnection *connection,
254                                  GVariant *new_settings,
255                                  NMSettingParseFlags parse_flags,
256                                  GError **error)
257 {
258         NMConnectionPrivate *priv;
259         GVariantIter iter;
260         const char *setting_name;
261         GVariant *setting_dict;
262         GSList *settings = NULL, *s;
263         gboolean changed, success;
264
265         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
266         g_return_val_if_fail (g_variant_is_of_type (new_settings, NM_VARIANT_TYPE_CONNECTION), FALSE);
267         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
268
269         nm_assert (!NM_FLAGS_ANY (parse_flags, ~NM_SETTING_PARSE_FLAGS_ALL));
270         nm_assert (!NM_FLAGS_ALL (parse_flags, NM_SETTING_PARSE_FLAGS_STRICT | NM_SETTING_PARSE_FLAGS_BEST_EFFORT));
271
272         priv = NM_CONNECTION_GET_PRIVATE (connection);
273
274         if (   !NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_BEST_EFFORT)
275             && !validate_permissions_type (new_settings, error))
276                 return FALSE;
277
278         g_variant_iter_init (&iter, new_settings);
279         while (g_variant_iter_next (&iter, "{&s@a{sv}}", &setting_name, &setting_dict)) {
280                 gs_unref_variant GVariant *setting_dict_free = NULL;
281                 GError *local = NULL;
282                 NMSetting *setting;
283                 GType type;
284
285                 setting_dict_free = setting_dict;
286
287                 type = nm_setting_lookup_type (setting_name);
288                 if (type == G_TYPE_INVALID) {
289                         if (NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_BEST_EFFORT))
290                                 continue;
291                         g_set_error_literal (error,
292                                              NM_CONNECTION_ERROR,
293                                              NM_CONNECTION_ERROR_INVALID_SETTING,
294                                              _("unknown setting name"));
295                         g_prefix_error (error, "%s: ", setting_name);
296                         g_slist_free_full (settings, g_object_unref);
297                         return FALSE;
298                 }
299
300                 for (s = settings; s; s = s->next) {
301                         if (G_OBJECT_TYPE (s->data) == type) {
302                                 if (NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_STRICT)) {
303                                         g_set_error_literal (error,
304                                                              NM_CONNECTION_ERROR,
305                                                              NM_CONNECTION_ERROR_INVALID_SETTING,
306                                                              _("duplicate setting name"));
307                                         g_prefix_error (error, "%s: ", setting_name);
308                                         g_slist_free_full (settings, g_object_unref);
309                                         return FALSE;
310                                 }
311                                 /* last wins. */
312                                 g_object_unref (s->data);
313                                 settings = g_slist_delete_link (settings, s);
314                                 break;
315                         }
316                 }
317
318                 setting = _nm_setting_new_from_dbus (type, setting_dict, new_settings, parse_flags, &local);
319
320                 if (!setting) {
321                         if (NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_BEST_EFFORT))
322                                 continue;
323                         g_propagate_error (error, local);
324                         g_slist_free_full (settings, g_object_unref);
325                         return FALSE;
326                 }
327
328                 settings = g_slist_prepend (settings, setting);
329         }
330
331         if (g_hash_table_size (priv->settings) > 0) {
332                 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
333                 changed = TRUE;
334         } else
335                 changed = (settings != NULL);
336
337         /* Note: @settings might be empty in which case the connection
338          * has no NMSetting instances... which is fine, just something
339          * to be aware of. */
340         for (s = settings; s; s = s->next)
341                 _nm_connection_add_setting (connection, s->data);
342
343         g_slist_free (settings);
344
345         /* If verification/normalization fails, the original connection
346          * is already lost. From an API point of view, it would be nicer
347          * not to touch the input argument if we fail at the end.
348          * However, that would require creating a temporary connection
349          * to validate it first. As none of the caller cares about the
350          * state of the @connection when normalization fails, just do it
351          * this way. */
352         if (NM_FLAGS_HAS (parse_flags, NM_SETTING_PARSE_FLAGS_NORMALIZE))
353                 success = nm_connection_normalize (connection, NULL, NULL, error);
354         else
355                 success = TRUE;
356
357         if (changed)
358                 g_signal_emit (connection, signals[CHANGED], 0);
359         return success;
360 }
361
362 /**
363  * nm_connection_replace_settings:
364  * @connection: a #NMConnection
365  * @new_settings: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION, with the new settings
366  * @error: location to store error, or %NULL
367  *
368  * Replaces @connection's settings with @new_settings (which must be
369  * syntactically valid, and describe a known type of connection, but does not
370  * need to result in a connection that passes nm_connection_verify()).
371  *
372  * Returns: %TRUE if connection was updated, %FALSE if @new_settings could not
373  *   be deserialized (in which case @connection will be unchanged).
374  **/
375 gboolean
376 nm_connection_replace_settings (NMConnection *connection,
377                                 GVariant *new_settings,
378                                 GError **error)
379 {
380         return _nm_connection_replace_settings (connection, new_settings, NM_SETTING_PARSE_FLAGS_NONE, error);
381 }
382
383 /**
384  * nm_connection_replace_settings_from_connection:
385  * @connection: a #NMConnection
386  * @new_connection: a #NMConnection to replace the settings of @connection with
387  *
388  * Deep-copies the settings of @new_connection and replaces the settings of @connection
389  * with the copied settings.
390  **/
391 void
392 nm_connection_replace_settings_from_connection (NMConnection *connection,
393                                                 NMConnection *new_connection)
394 {
395         NMConnectionPrivate *priv, *new_priv;
396         GHashTableIter iter;
397         NMSetting *setting;
398         gboolean changed;
399
400         g_return_if_fail (NM_IS_CONNECTION (connection));
401         g_return_if_fail (NM_IS_CONNECTION (new_connection));
402
403         /* When 'connection' and 'new_connection' are the same object simply return
404          * in order not to destroy 'connection'.
405          */
406         if (connection == new_connection)
407                 return;
408
409         /* No need to validate permissions like nm_connection_replace_settings()
410          * since we're dealing with an NMConnection which has already done that.
411          */
412
413         priv = NM_CONNECTION_GET_PRIVATE (connection);
414         new_priv = NM_CONNECTION_GET_PRIVATE (new_connection);
415
416         if ((changed = g_hash_table_size (priv->settings) > 0))
417                 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
418
419         if (g_hash_table_size (new_priv->settings)) {
420                 g_hash_table_iter_init (&iter, new_priv->settings);
421                 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
422                         _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
423                 changed = TRUE;
424         }
425
426         if (changed)
427                 g_signal_emit (connection, signals[CHANGED], 0);
428 }
429
430 /**
431  * nm_connection_clear_settings:
432  * @connection: a #NMConnection
433  *
434  * Deletes all of @connection's settings.
435  **/
436 void
437 nm_connection_clear_settings (NMConnection *connection)
438 {
439         NMConnectionPrivate *priv;
440
441         g_return_if_fail (NM_IS_CONNECTION (connection));
442
443         priv = NM_CONNECTION_GET_PRIVATE (connection);
444
445         if (g_hash_table_size (priv->settings) > 0) {
446                 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
447                 g_signal_emit (connection, signals[CHANGED], 0);
448         }
449 }
450
451 /**
452  * nm_connection_compare:
453  * @a: a #NMConnection
454  * @b: a second #NMConnection to compare with the first
455  * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
456  *
457  * Compares two #NMConnection objects for similarity, with comparison behavior
458  * modified by a set of flags.  See nm_setting_compare() for a description of
459  * each flag's behavior.
460  *
461  * Returns: %TRUE if the comparison succeeds, %FALSE if it does not
462  **/
463 gboolean
464 nm_connection_compare (NMConnection *a,
465                        NMConnection *b,
466                        NMSettingCompareFlags flags)
467 {
468         GHashTableIter iter;
469         NMSetting *src;
470
471         if (a == b)
472                 return TRUE;
473         if (!a || !b)
474                 return FALSE;
475
476         /* B / A: ensure settings in B that are not in A make the comparison fail */
477         if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (a)->settings) !=
478             g_hash_table_size (NM_CONNECTION_GET_PRIVATE (b)->settings))
479                 return FALSE;
480
481         /* A / B: ensure all settings in A match corresponding ones in B */
482         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (a)->settings);
483         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &src)) {
484                 NMSetting *cmp = nm_connection_get_setting (b, G_OBJECT_TYPE (src));
485
486                 if (!cmp || !nm_setting_compare (src, cmp, flags))
487                         return FALSE;
488         }
489
490         return TRUE;
491 }
492
493
494 static void
495 diff_one_connection (NMConnection *a,
496                      NMConnection *b,
497                      NMSettingCompareFlags flags,
498                      gboolean invert_results,
499                      GHashTable *diffs)
500 {
501         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (a);
502         GHashTableIter iter;
503         NMSetting *a_setting = NULL;
504
505         g_hash_table_iter_init (&iter, priv->settings);
506         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &a_setting)) {
507                 NMSetting *b_setting = NULL;
508                 const char *setting_name = nm_setting_get_name (a_setting);
509                 GHashTable *results;
510                 gboolean new_results = TRUE;
511
512                 if (b)
513                         b_setting = nm_connection_get_setting (b, G_OBJECT_TYPE (a_setting));
514
515                 results = g_hash_table_lookup (diffs, setting_name);
516                 if (results)
517                         new_results = FALSE;
518
519                 if (!nm_setting_diff (a_setting, b_setting, flags, invert_results, &results)) {
520                         if (new_results)
521                                 g_hash_table_insert (diffs, g_strdup (setting_name), results);
522                 }
523         }
524 }
525
526 /**
527  * nm_connection_diff:
528  * @a: a #NMConnection
529  * @b: a second #NMConnection to compare with the first
530  * @flags: compare flags, e.g. %NM_SETTING_COMPARE_FLAG_EXACT
531  * @out_settings: (element-type utf8 GLib.HashTable): if the
532  * connections differ, on return a hash table mapping setting names to
533  * second-level GHashTable (utf8 to guint32), which contains the key names that
534  * differ mapped to one or more of %NMSettingDiffResult as a bitfield
535  *
536  * Compares two #NMConnection objects for similarity, with comparison behavior
537  * modified by a set of flags.  See nm_setting_compare() for a description of
538  * each flag's behavior.  If the connections differ, settings and keys within
539  * each setting that differ are added to the returned @out_settings hash table.
540  * No values are returned, only key names.
541  *
542  * Returns: %TRUE if the connections contain the same values, %FALSE if they do
543  * not
544  **/
545 gboolean
546 nm_connection_diff (NMConnection *a,
547                     NMConnection *b,
548                     NMSettingCompareFlags flags,
549                     GHashTable **out_settings)
550 {
551         GHashTable *diffs;
552
553         g_return_val_if_fail (NM_IS_CONNECTION (a), FALSE);
554         g_return_val_if_fail (out_settings != NULL, FALSE);
555         g_return_val_if_fail (*out_settings == NULL, FALSE);
556         if (b)
557                 g_return_val_if_fail (NM_IS_CONNECTION (b), FALSE);
558
559         if (a == b)
560                 return TRUE;
561
562         diffs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
563
564         /* Diff A to B, then B to A to capture keys in B that aren't in A */
565         diff_one_connection (a, b, flags, FALSE, diffs);
566         if (b)
567                 diff_one_connection (b, a, flags, TRUE, diffs);
568
569         if (g_hash_table_size (diffs) == 0)
570                 g_hash_table_destroy (diffs);
571         else
572                 *out_settings = diffs;
573
574         return *out_settings ? FALSE : TRUE;
575 }
576
577 NMSetting *
578 _nm_connection_find_base_type_setting (NMConnection *connection)
579 {
580         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
581         GHashTableIter iter;
582         NMSetting *setting = NULL, *s_iter;
583
584         g_hash_table_iter_init (&iter, priv->settings);
585         while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &s_iter)) {
586                 if (!_nm_setting_is_base_type (s_iter))
587                         continue;
588
589                 if (setting) {
590                         /* FIXME: currently, if there is more than one matching base type,
591                          * we cannot detect the base setting.
592                          * See: https://bugzilla.gnome.org/show_bug.cgi?id=696936#c8 */
593                         return NULL;
594                 }
595                 setting = s_iter;
596         }
597         return setting;
598 }
599
600 static gboolean
601 _normalize_connection_uuid (NMConnection *self)
602 {
603         NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
604         char *uuid;
605
606         g_assert (s_con);
607
608         if (nm_setting_connection_get_uuid (s_con))
609                 return FALSE;
610
611         uuid = nm_utils_uuid_generate ();
612         g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
613         g_free (uuid);
614
615         return TRUE;
616 }
617
618 static gboolean
619 _normalize_connection_type (NMConnection *self)
620 {
621         NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
622         NMSetting *s_base = NULL;
623         const char *type;
624
625         type = nm_setting_connection_get_connection_type (s_con);
626
627         if (type) {
628                 s_base = nm_connection_get_setting_by_name (self, type);
629
630                 if (!s_base) {
631                         GType base_type = nm_setting_lookup_type (type);
632
633                         g_return_val_if_fail (base_type, FALSE);
634                         nm_connection_add_setting (self, g_object_new (base_type, NULL));
635                         return TRUE;
636                 }
637         } else {
638                 s_base =  _nm_connection_find_base_type_setting (self);
639                 g_return_val_if_fail (s_base, FALSE);
640
641                 type = nm_setting_get_name (s_base);
642                 g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, type, NULL);
643                 return TRUE;
644         }
645
646         return FALSE;
647 }
648
649 const char *
650 _nm_connection_detect_slave_type (NMConnection *connection, NMSetting **out_s_port)
651 {
652         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
653         GHashTableIter iter;
654         const char *slave_type = NULL;
655         NMSetting *s_port = NULL, *s_iter;
656
657         g_hash_table_iter_init (&iter, priv->settings);
658         while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &s_iter)) {
659                 const char *name = nm_setting_get_name (s_iter);
660                 const char *i_slave_type = NULL;
661
662                 if (!strcmp (name, NM_SETTING_BRIDGE_PORT_SETTING_NAME))
663                         i_slave_type = NM_SETTING_BRIDGE_SETTING_NAME;
664                 else if (!strcmp (name, NM_SETTING_TEAM_PORT_SETTING_NAME))
665                         i_slave_type = NM_SETTING_TEAM_SETTING_NAME;
666                 else
667                         continue;
668
669                 if (slave_type) {
670                         /* there are more then one matching port types, cannot detect the slave type. */
671                         slave_type = NULL;
672                         s_port = NULL;
673                         break;
674                 }
675                 slave_type = i_slave_type;
676                 s_port = s_iter;
677         }
678
679         if (out_s_port)
680                 *out_s_port = s_port;
681         return slave_type;
682 }
683
684 static gboolean
685 _normalize_connection_slave_type (NMConnection *self)
686 {
687         NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
688         const char *slave_type, *port_type;
689
690         if (!s_con)
691                 return FALSE;
692         if (!nm_setting_connection_get_master (s_con))
693                 return FALSE;
694
695         slave_type = nm_setting_connection_get_slave_type (s_con);
696         if (slave_type) {
697                 if (   _nm_setting_slave_type_is_valid (slave_type, &port_type)
698                     && port_type) {
699                         NMSetting *s_port;
700
701                         s_port = nm_connection_get_setting_by_name (self, port_type);
702                         if (!s_port) {
703                                 GType p_type = nm_setting_lookup_type (port_type);
704
705                                 g_return_val_if_fail (p_type, FALSE);
706                                 nm_connection_add_setting (self, g_object_new (p_type, NULL));
707                                 return TRUE;
708                         }
709                 }
710         } else {
711                 if ((slave_type = _nm_connection_detect_slave_type (self, NULL))) {
712                         g_object_set (s_con, NM_SETTING_CONNECTION_SLAVE_TYPE, slave_type, NULL);
713                         return TRUE;
714                 }
715         }
716         return FALSE;
717 }
718
719 static gboolean
720 _normalize_ip_config (NMConnection *self, GHashTable *parameters)
721 {
722         NMSettingConnection *s_con = nm_connection_get_setting_connection (self);
723         const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO;
724         const char *default_ip6_method = NULL;
725         NMSettingIPConfig *s_ip4, *s_ip6;
726         NMSetting *setting;
727
728         if (parameters)
729                 default_ip6_method = g_hash_table_lookup (parameters, NM_CONNECTION_NORMALIZE_PARAM_IP6_CONFIG_METHOD);
730         if (!default_ip6_method)
731                 default_ip6_method = NM_SETTING_IP6_CONFIG_METHOD_AUTO;
732
733         s_ip4 = nm_connection_get_setting_ip4_config (self);
734         s_ip6 = nm_connection_get_setting_ip6_config (self);
735
736         if (nm_setting_connection_get_master (s_con)) {
737                 /* Slave connections don't have IP configuration. */
738
739                 if (s_ip4)
740                         nm_connection_remove_setting (self, NM_TYPE_SETTING_IP4_CONFIG);
741
742                 if (s_ip6)
743                         nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG);
744
745                 return s_ip4 || s_ip6;
746         } else {
747                 /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no
748                  * IP6 setting was specified, then assume that means IP6 config is allowed
749                  * to fail. But if no IP4 setting was specified, assume the caller was just
750                  * being lazy.
751                  */
752                 if (!s_ip4) {
753                         setting = nm_setting_ip4_config_new ();
754
755                         g_object_set (setting,
756                                       NM_SETTING_IP_CONFIG_METHOD, default_ip4_method,
757                                       NULL);
758                         nm_connection_add_setting (self, setting);
759                 }
760                 if (!s_ip6) {
761                         setting = nm_setting_ip6_config_new ();
762
763                         g_object_set (setting,
764                                       NM_SETTING_IP_CONFIG_METHOD, default_ip6_method,
765                                       NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
766                                       NULL);
767                         nm_connection_add_setting (self, setting);
768                 }
769                 return !s_ip4 || !s_ip6;
770         }
771 }
772
773 static gboolean
774 _normalize_infiniband_mtu (NMConnection *self, GHashTable *parameters)
775 {
776         NMSettingInfiniband *s_infini = nm_connection_get_setting_infiniband (self);
777
778         if (s_infini) {
779                 const char *transport_mode = nm_setting_infiniband_get_transport_mode (s_infini);
780                 guint32 max_mtu = 0;
781
782                 if (transport_mode) {
783                         if (!strcmp (transport_mode, "datagram"))
784                                 max_mtu = 2044;
785                         else if (!strcmp (transport_mode, "connected"))
786                                 max_mtu = 65520;
787
788                         if (max_mtu && nm_setting_infiniband_get_mtu (s_infini) > max_mtu) {
789                                 g_object_set (s_infini, NM_SETTING_INFINIBAND_MTU, max_mtu, NULL);
790                                 return TRUE;
791                         }
792                 }
793         }
794         return FALSE;
795 }
796
797 static gboolean
798 _normalize_bond_mode (NMConnection *self, GHashTable *parameters)
799 {
800         NMSettingBond *s_bond = nm_connection_get_setting_bond (self);
801
802         /* Convert mode from numeric to string notation */
803         if (s_bond) {
804                 const char *mode = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_MODE);
805                 int mode_int = nm_utils_bond_mode_string_to_int (mode);
806
807                 if (mode_int != -1) {
808                         const char *mode_new = nm_utils_bond_mode_int_to_string (mode_int);
809                         if (g_strcmp0 (mode_new, mode) != 0) {
810                                 nm_setting_bond_add_option (s_bond, NM_SETTING_BOND_OPTION_MODE, mode_new);
811                                 return TRUE;
812                         }
813                 }
814         }
815         return FALSE;
816 }
817
818 /**
819  * nm_connection_verify:
820  * @connection: the #NMConnection to verify
821  * @error: location to store error, or %NULL
822  *
823  * Validates the connection and all its settings.  Each setting's properties
824  * have allowed values, and some values are dependent on other values.  For
825  * example, if a Wi-Fi connection is security enabled, the #NMSettingWireless
826  * setting object's 'security' property must contain the setting name of the
827  * #NMSettingWirelessSecurity object, which must also be present in the
828  * connection for the connection to be valid.  As another example, the
829  * #NMSettingWired object's 'mac-address' property must be a validly formatted
830  * MAC address.  The returned #GError contains information about which
831  * setting and which property failed validation, and how it failed validation.
832  *
833  * Returns: %TRUE if the connection is valid, %FALSE if it is not
834  **/
835 gboolean
836 nm_connection_verify (NMConnection *connection, GError **error)
837 {
838         NMSettingVerifyResult result;
839
840         result = _nm_connection_verify (connection, error);
841
842         /* we treat normalizable connections as valid. */
843         if (result == NM_SETTING_VERIFY_NORMALIZABLE)
844                 g_clear_error (error);
845
846         return result == NM_SETTING_VERIFY_SUCCESS || result == NM_SETTING_VERIFY_NORMALIZABLE;
847 }
848
849 static NMSettingVerifyResult
850 _nm_connection_verify (NMConnection *connection, GError **error)
851 {
852         NMConnectionPrivate *priv;
853         NMSettingConnection *s_con;
854         NMSettingIPConfig *s_ip4, *s_ip6;
855         GHashTableIter iter;
856         gpointer value;
857         GSList *all_settings = NULL, *setting_i;
858         NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR;
859         GError *normalizable_error = NULL;
860         NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS;
861
862         g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR);
863         g_return_val_if_fail (!error || !*error, NM_SETTING_VERIFY_ERROR);
864
865         priv = NM_CONNECTION_GET_PRIVATE (connection);
866
867         /* First, make sure there's at least 'connection' setting */
868         s_con = nm_connection_get_setting_connection (connection);
869         if (!s_con) {
870                 g_set_error_literal (error,
871                                      NM_CONNECTION_ERROR,
872                                      NM_CONNECTION_ERROR_MISSING_SETTING,
873                                      _("setting not found"));
874                 g_prefix_error (error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME);
875                 goto EXIT;
876         }
877
878         /* Build up the list of settings */
879         g_hash_table_iter_init (&iter, priv->settings);
880         while (g_hash_table_iter_next (&iter, NULL, &value)) {
881                 /* Order NMSettingConnection so that it will be verified first.
882                  * The reason is, that errors in this setting might be more fundamental
883                  * and should be checked and reported with higher priority.
884                  */
885                 if (value == s_con)
886                         all_settings = g_slist_append (all_settings, value);
887                 else
888                         all_settings = g_slist_prepend (all_settings, value);
889         }
890         all_settings = g_slist_reverse (all_settings);
891
892         /* Now, run the verify function of each setting */
893         for (setting_i = all_settings; setting_i; setting_i = setting_i->next) {
894                 GError *verify_error = NULL;
895                 NMSettingVerifyResult verify_result;
896
897                 /* verify all settings. We stop if we find the first non-normalizable
898                  * @NM_SETTING_VERIFY_ERROR. If we find normalizable errors we continue
899                  * but remember the error to return it to the user.
900                  * @NM_SETTING_VERIFY_NORMALIZABLE_ERROR has a higher priority then
901                  * @NM_SETTING_VERIFY_NORMALIZABLE, so, if we encounter such an error type,
902                  * we remember it instead (to return it as output).
903                  **/
904                 verify_result = _nm_setting_verify (NM_SETTING (setting_i->data), connection, &verify_error);
905                 if (verify_result == NM_SETTING_VERIFY_NORMALIZABLE ||
906                     verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR) {
907                         if (   verify_result == NM_SETTING_VERIFY_NORMALIZABLE_ERROR
908                             && normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE) {
909                                 /* NORMALIZABLE_ERROR has higher priority. */
910                                 g_clear_error (&normalizable_error);
911                         }
912                         if (!normalizable_error) {
913                                 g_propagate_error (&normalizable_error, verify_error);
914                                 verify_error = NULL;
915                                 normalizable_error_type = verify_result;
916                         }
917                 } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) {
918                         g_propagate_error (error, verify_error);
919                         g_slist_free (all_settings);
920                         g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success);
921                         goto EXIT;
922                 }
923                 g_clear_error (&verify_error);
924         }
925         g_slist_free (all_settings);
926
927         s_ip4 = nm_connection_get_setting_ip4_config (connection);
928         s_ip6 = nm_connection_get_setting_ip6_config (connection);
929
930         if (nm_setting_connection_get_master (s_con)) {
931                 if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS ||
932                     (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE))  && (s_ip4 || s_ip6)) {
933                         g_clear_error (&normalizable_error);
934                         g_set_error_literal (&normalizable_error,
935                                              NM_CONNECTION_ERROR,
936                                              NM_CONNECTION_ERROR_INVALID_SETTING,
937                                              _("setting not allowed in slave connection"));
938                         g_prefix_error (&normalizable_error, "%s: ",
939                                         s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME);
940                         /* having a slave with IP config *was* and is a verify() error. */
941                         normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
942                 }
943         } else {
944                 if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) {
945                         g_set_error_literal (&normalizable_error,
946                                              NM_CONNECTION_ERROR,
947                                              NM_CONNECTION_ERROR_MISSING_SETTING,
948                                              _("setting is required for non-slave connections"));
949                         g_prefix_error (&normalizable_error, "%s: ",
950                                         !s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME);
951                         /* having a master without IP config was not a verify() error, accept
952                          * it for backward compatibility. */
953                         normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE;
954                 }
955         }
956
957         if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) {
958                 g_propagate_error (error, normalizable_error);
959                 normalizable_error = NULL;
960                 success = normalizable_error_type;
961         } else
962                 success = NM_SETTING_VERIFY_SUCCESS;
963
964 EXIT:
965         g_clear_error (&normalizable_error);
966         return success;
967 }
968
969 /**
970  * nm_connection_verify_secrets:
971  * @connection: the #NMConnection to verify in
972  * @error: location to store error, or %NULL
973  *
974  * Verifies the secrets in the connection.
975  *
976  * Returns: %TRUE if the secrets are valid, %FALSE if they are not
977  *
978  * Since: 1.2
979  **/
980 gboolean
981 nm_connection_verify_secrets (NMConnection *connection, GError **error)
982 {
983         GHashTableIter iter;
984         NMSetting *setting;
985
986         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
987         g_return_val_if_fail (!error || !*error, FALSE);
988
989         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
990         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
991                 if (!nm_setting_verify_secrets (setting, connection, error))
992                         return FALSE;
993         }
994         return TRUE;
995 }
996
997 /**
998  * nm_connection_normalize:
999  * @connection: the #NMConnection to normalize
1000  * @parameters: (allow-none) (element-type utf8 gpointer): a #GHashTable with
1001  * normalization parameters to allow customization of the normalization by providing
1002  * specific arguments. Unknown arguments will be ignored and the default will be
1003  * used. The keys must be strings, hashed by g_str_hash() and g_str_equal() functions.
1004  * The values are opaque and depend on the parameter name.
1005  * @modified: (out) (allow-none): outputs whether any settings were modified.
1006  * @error: location to store error, or %NULL. Contains the reason,
1007  * why the connection is invalid, if the function returns an error.
1008  *
1009  * Does some basic normalization and fixup of well known inconsistencies
1010  * and deprecated fields. If the connection was modified in any way,
1011  * the output parameter @modified is set %TRUE.
1012  *
1013  * Finally the connection will be verified and %TRUE returns if the connection
1014  * is valid. As this function only performs some specific normalization steps
1015  * it cannot repair all connections. If the connection has errors that
1016  * cannot be normalized, the connection will not be modified.
1017  *
1018  * Returns: %TRUE if the connection is valid, %FALSE if it is not
1019  **/
1020 gboolean
1021 nm_connection_normalize (NMConnection *connection,
1022                          GHashTable *parameters,
1023                          gboolean *modified,
1024                          GError **error)
1025 {
1026         NMSettingVerifyResult success;
1027         gboolean was_modified = FALSE;
1028         GError *normalizable_error = NULL;
1029
1030         success = _nm_connection_verify (connection, &normalizable_error);
1031
1032         if (success == NM_SETTING_VERIFY_ERROR ||
1033             success == NM_SETTING_VERIFY_SUCCESS) {
1034                 if (normalizable_error)
1035                         g_propagate_error (error, normalizable_error);
1036                 if (modified)
1037                         *modified = FALSE;
1038                 if (success == NM_SETTING_VERIFY_ERROR && error && !*error) {
1039                         g_set_error_literal (error,
1040                                              NM_CONNECTION_ERROR,
1041                                              NM_CONNECTION_ERROR_FAILED,
1042                                              _("Unexpected failure to verify the connection"));
1043                         g_return_val_if_reached (FALSE);
1044                 }
1045                 return success == NM_SETTING_VERIFY_SUCCESS;
1046         }
1047         g_assert (success == NM_SETTING_VERIFY_NORMALIZABLE || success == NM_SETTING_VERIFY_NORMALIZABLE_ERROR);
1048         g_clear_error (&normalizable_error);
1049
1050         /* Try to perform all kind of normalizations on the settings to fix it.
1051          * We only do this, after verifying that the connection contains no un-normalizable
1052          * errors, because in that case we rather fail without touching the settings. */
1053
1054         was_modified |= _normalize_connection_uuid (connection);
1055         was_modified |= _normalize_connection_type (connection);
1056         was_modified |= _normalize_connection_slave_type (connection);
1057         was_modified |= _normalize_ip_config (connection, parameters);
1058         was_modified |= _normalize_infiniband_mtu (connection, parameters);
1059         was_modified |= _normalize_bond_mode (connection, parameters);
1060
1061         /* Verify anew. */
1062         success = _nm_connection_verify (connection, error);
1063
1064         if (modified)
1065                 *modified = was_modified;
1066
1067         if (success != NM_SETTING_VERIFY_SUCCESS) {
1068                 /* we would expect, that after normalization, the connection can be verified.
1069                  * Also treat NM_SETTING_VERIFY_NORMALIZABLE as failure, because there is something
1070                  * odd going on. */
1071                 if (error && !*error) {
1072                         g_set_error_literal (error,
1073                                              NM_CONNECTION_ERROR,
1074                                              NM_CONNECTION_ERROR_FAILED,
1075                                              _("Unexpected failure to normalize the connection"));
1076                 }
1077                 g_return_val_if_reached (FALSE);
1078         }
1079
1080         /* we would expect, that the connection was modified during normalization. */
1081         g_return_val_if_fail (was_modified, TRUE);
1082
1083         return TRUE;
1084 }
1085
1086 /**
1087  * nm_connection_update_secrets:
1088  * @connection: the #NMConnection
1089  * @setting_name: the setting object name to which the secrets apply
1090  * @secrets: a #GVariant of secrets, of type %NM_VARIANT_TYPE_CONNECTION
1091  *   or %NM_VARIANT_TYPE_SETTING
1092  * @error: location to store error, or %NULL
1093  *
1094  * Update the specified setting's secrets, given a dictionary of secrets
1095  * intended for that setting (deserialized from D-Bus for example).  Will also
1096  * extract the given setting's secrets hash if given a connection dictionary.
1097  * If @setting_name is %NULL, expects a fully serialized #NMConnection as
1098  * returned by nm_connection_to_dbus() and will update all secrets from all
1099  * settings contained in @secrets.
1100  *
1101  * Returns: %TRUE if the secrets were successfully updated, %FALSE if the update
1102  * failed (tried to update secrets for a setting that doesn't exist, etc)
1103  **/
1104 gboolean
1105 nm_connection_update_secrets (NMConnection *connection,
1106                               const char *setting_name,
1107                               GVariant *secrets,
1108                               GError **error)
1109 {
1110         NMSetting *setting;
1111         gboolean success = TRUE, updated = FALSE;
1112         GVariant *setting_dict = NULL;
1113         GVariantIter iter;
1114         const char *key;
1115         gboolean full_connection;
1116         int success_detail;
1117
1118         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1119         g_return_val_if_fail (   g_variant_is_of_type (secrets, NM_VARIANT_TYPE_SETTING)
1120                               || g_variant_is_of_type (secrets, NM_VARIANT_TYPE_CONNECTION), FALSE);
1121         if (error)
1122                 g_return_val_if_fail (*error == NULL, FALSE);
1123
1124         full_connection = g_variant_is_of_type (secrets, NM_VARIANT_TYPE_CONNECTION);
1125         g_return_val_if_fail (setting_name != NULL || full_connection, FALSE);
1126
1127         /* Empty @secrets means success */
1128         if (g_variant_n_children (secrets) == 0)
1129                 return TRUE;
1130
1131         if (setting_name) {
1132                 /* Update just one setting's secrets */
1133                 setting = nm_connection_get_setting_by_name (connection, setting_name);
1134                 if (!setting) {
1135                         g_set_error_literal (error,
1136                                              NM_CONNECTION_ERROR,
1137                                              NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1138                                              setting_name);
1139                         return FALSE;
1140                 }
1141
1142                 if (full_connection) {
1143                         setting_dict = g_variant_lookup_value (secrets, setting_name, NM_VARIANT_TYPE_SETTING);
1144                         if (!setting_dict) {
1145                                 /* The connection dictionary didn't contain any secrets for
1146                                  * @setting_name; just return success.
1147                                  */
1148                                 return TRUE;
1149                         }
1150                 }
1151
1152                 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1153                 success_detail = _nm_setting_update_secrets (setting,
1154                                                              setting_dict ? setting_dict : secrets,
1155                                                              error);
1156                 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1157
1158                 g_clear_pointer (&setting_dict, g_variant_unref);
1159
1160                 if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR)
1161                         return FALSE;
1162                 if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1163                         updated = TRUE;
1164         } else {
1165                 /* check first, whether all the settings exist... */
1166                 g_variant_iter_init (&iter, secrets);
1167                 while (g_variant_iter_next (&iter, "{&s@a{sv}}", &key, NULL)) {
1168                         setting = nm_connection_get_setting_by_name (connection, key);
1169                         if (!setting) {
1170                                 g_set_error_literal (error,
1171                                                      NM_CONNECTION_ERROR,
1172                                                      NM_CONNECTION_ERROR_SETTING_NOT_FOUND,
1173                                                      key);
1174                                 return FALSE;
1175                         }
1176                 }
1177
1178                 /* Update each setting with any secrets from the connection dictionary */
1179                 g_variant_iter_init (&iter, secrets);
1180                 while (g_variant_iter_next (&iter, "{&s@a{sv}}", &key, &setting_dict)) {
1181                         /* Update the secrets for this setting */
1182                         setting = nm_connection_get_setting_by_name (connection, key);
1183
1184                         g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1185                         success_detail = _nm_setting_update_secrets (setting, setting_dict, error);
1186                         g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1187
1188                         g_variant_unref (setting_dict);
1189
1190                         if (success_detail == NM_SETTING_UPDATE_SECRET_ERROR) {
1191                                 success = FALSE;
1192                                 break;
1193                         }
1194                         if (success_detail == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
1195                                 updated = TRUE;
1196                 }
1197         }
1198
1199         if (updated) {
1200                 g_signal_emit (connection, signals[SECRETS_UPDATED], 0, setting_name);
1201                 g_signal_emit (connection, signals[CHANGED], 0);
1202         }
1203
1204         return success;
1205 }
1206
1207 /**
1208  * nm_connection_need_secrets:
1209  * @connection: the #NMConnection
1210  * @hints: (out) (element-type utf8) (allow-none) (transfer container):
1211  *   the address of a pointer to a #GPtrArray, initialized to %NULL, which on
1212  *   return points to an allocated #GPtrArray containing the property names of
1213  *   secrets of the #NMSetting which may be required; the caller owns the array
1214  *   and must free the array itself with g_ptr_array_free(), but not free its
1215  *   elements
1216  *
1217  * Returns the name of the first setting object in the connection which would
1218  * need secrets to make a successful connection.  The returned hints are only
1219  * intended as a guide to what secrets may be required, because in some
1220  * circumstances, there is no way to conclusively determine exactly which
1221  * secrets are needed.
1222  *
1223  * Returns: the setting name of the #NMSetting object which has invalid or
1224  *   missing secrets
1225  **/
1226 const char *
1227 nm_connection_need_secrets (NMConnection *connection,
1228                             GPtrArray **hints)
1229 {
1230         NMConnectionPrivate *priv;
1231         GHashTableIter hiter;
1232         GSList *settings = NULL;
1233         GSList *iter;
1234         const char *name = NULL;
1235         NMSetting *setting;
1236
1237         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1238         if (hints)
1239                 g_return_val_if_fail (*hints == NULL, NULL);
1240
1241         priv = NM_CONNECTION_GET_PRIVATE (connection);
1242
1243         /* Get list of settings in priority order */
1244         g_hash_table_iter_init (&hiter, priv->settings);
1245         while (g_hash_table_iter_next (&hiter, NULL, (gpointer) &setting))
1246                 settings = g_slist_insert_sorted (settings, setting, _nm_setting_compare_priority);
1247
1248         for (iter = settings; iter; iter = g_slist_next (iter)) {
1249                 GPtrArray *secrets;
1250
1251                 setting = NM_SETTING (iter->data);
1252                 secrets = _nm_setting_need_secrets (setting);
1253                 if (secrets) {
1254                         if (hints)
1255                                 *hints = secrets;
1256                         else
1257                                 g_ptr_array_free (secrets, TRUE);
1258
1259                         name = nm_setting_get_name (setting);
1260                         break;
1261                 }
1262         }
1263
1264         g_slist_free (settings);
1265         return name;
1266 }
1267
1268 /**
1269  * nm_connection_clear_secrets:
1270  * @connection: the #NMConnection
1271  *
1272  * Clears and frees any secrets that may be stored in the connection, to avoid
1273  * keeping secret data in memory when not needed.
1274  **/
1275 void
1276 nm_connection_clear_secrets (NMConnection *connection)
1277 {
1278         GHashTableIter iter;
1279         NMSetting *setting;
1280         gboolean changed = FALSE;
1281
1282         g_return_if_fail (NM_IS_CONNECTION (connection));
1283
1284         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1285         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1286                 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1287                 changed |= _nm_setting_clear_secrets (setting);
1288                 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1289         }
1290
1291         g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1292         if (changed)
1293                 g_signal_emit (connection, signals[CHANGED], 0);
1294 }
1295
1296 /**
1297  * nm_connection_clear_secrets_with_flags:
1298  * @connection: the #NMConnection
1299  * @func: (scope call): function to be called to determine whether a
1300  *     specific secret should be cleared or not
1301  * @user_data: caller-supplied data passed to @func
1302  *
1303  * Clears and frees secrets determined by @func.
1304  **/
1305 void
1306 nm_connection_clear_secrets_with_flags (NMConnection *connection,
1307                                         NMSettingClearSecretsWithFlagsFn func,
1308                                         gpointer user_data)
1309 {
1310         GHashTableIter iter;
1311         NMSetting *setting;
1312         gboolean changed = FALSE;
1313
1314         g_return_if_fail (NM_IS_CONNECTION (connection));
1315
1316         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1317         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting)) {
1318                 g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection);
1319                 changed |= _nm_setting_clear_secrets_with_flags (setting, func, user_data);
1320                 g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection);
1321         }
1322
1323         g_signal_emit (connection, signals[SECRETS_CLEARED], 0);
1324         if (changed)
1325                 g_signal_emit (connection, signals[CHANGED], 0);
1326 }
1327
1328 /**
1329  * nm_connection_to_dbus:
1330  * @connection: the #NMConnection
1331  * @flags: serialization flags, e.g. %NM_CONNECTION_SERIALIZE_ALL
1332  *
1333  * Converts the #NMConnection into a #GVariant of type
1334  * %NM_VARIANT_TYPE_CONNECTION describing the connection, suitable for
1335  * marshalling over D-Bus or otherwise serializing.
1336  *
1337  * Returns: (transfer none): a new floating #GVariant describing the connection,
1338  * its settings, and each setting's properties.
1339  **/
1340 GVariant *
1341 nm_connection_to_dbus (NMConnection *connection,
1342                        NMConnectionSerializationFlags flags)
1343 {
1344         NMConnectionPrivate *priv;
1345         GVariantBuilder builder;
1346         GHashTableIter iter;
1347         gpointer key, data;
1348         GVariant *setting_dict, *ret;
1349
1350         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1351         priv = NM_CONNECTION_GET_PRIVATE (connection);
1352
1353         g_variant_builder_init (&builder, NM_VARIANT_TYPE_CONNECTION);
1354
1355         /* Add each setting's hash to the main hash */
1356         g_hash_table_iter_init (&iter, priv->settings);
1357         while (g_hash_table_iter_next (&iter, &key, &data)) {
1358                 NMSetting *setting = NM_SETTING (data);
1359
1360                 setting_dict = _nm_setting_to_dbus (setting, connection, flags);
1361                 if (setting_dict)
1362                         g_variant_builder_add (&builder, "{s@a{sv}}", nm_setting_get_name (setting), setting_dict);
1363         }
1364
1365         ret = g_variant_builder_end (&builder);
1366
1367         /* Don't send empty hashes */
1368         if (g_variant_n_children (ret) == 0) {
1369                 g_variant_unref (ret);
1370                 ret = NULL;
1371         }
1372
1373         return ret;
1374 }
1375
1376 /**
1377  * nm_connection_is_type:
1378  * @connection: the #NMConnection
1379  * @type: a setting name to check the connection's type against (like
1380  * %NM_SETTING_WIRELESS_SETTING_NAME or %NM_SETTING_WIRED_SETTING_NAME)
1381  *
1382  * A convenience function to check if the given @connection is a particular
1383  * type (ie wired, Wi-Fi, ppp, etc). Checks the #NMSettingConnection:type
1384  * property of the connection and matches that against @type.
1385  *
1386  * Returns: %TRUE if the connection is of the given @type, %FALSE if not
1387  **/
1388 gboolean
1389 nm_connection_is_type (NMConnection *connection, const char *type)
1390 {
1391         NMSettingConnection *s_con;
1392         const char *type2;
1393
1394         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
1395         g_return_val_if_fail (type != NULL, FALSE);
1396
1397         s_con = nm_connection_get_setting_connection (connection);
1398         if (!s_con)
1399                 return FALSE;
1400
1401         type2 = nm_setting_connection_get_connection_type (s_con);
1402
1403         return (g_strcmp0 (type2, type) == 0);
1404 }
1405
1406 static int
1407 _for_each_sort (NMSetting **p_a, NMSetting **p_b, void *unused)
1408 {
1409         NMSetting *a = *p_a;
1410         NMSetting *b = *p_b;
1411         int c;
1412
1413         c = _nm_setting_compare_priority (a, b);
1414         if (c != 0)
1415                 return c;
1416         return strcmp (nm_setting_get_name (a), nm_setting_get_name (b));
1417 }
1418
1419 /**
1420  * nm_connection_for_each_setting_value:
1421  * @connection: the #NMConnection
1422  * @func: (scope call): user-supplied function called for each setting's property
1423  * @user_data: user data passed to @func at each invocation
1424  *
1425  * Iterates over the properties of each #NMSetting object in the #NMConnection,
1426  * calling the supplied user function for each property.
1427  **/
1428 void
1429 nm_connection_for_each_setting_value (NMConnection *connection,
1430                                       NMSettingValueIterFn func,
1431                                       gpointer user_data)
1432 {
1433         NMConnectionPrivate *priv;
1434         gs_free NMSetting **arr_free = NULL;
1435         NMSetting *arr_temp[20], **arr;
1436         GHashTableIter iter;
1437         gpointer value;
1438         guint i, size;
1439
1440         g_return_if_fail (NM_IS_CONNECTION (connection));
1441         g_return_if_fail (func != NULL);
1442
1443         priv = NM_CONNECTION_GET_PRIVATE (connection);
1444
1445         size = g_hash_table_size (priv->settings);
1446         if (!size)
1447                 return;
1448
1449         if (size > G_N_ELEMENTS (arr_temp))
1450                 arr = arr_free = g_new (NMSetting *, size);
1451         else
1452                 arr = arr_temp;
1453
1454         g_hash_table_iter_init (&iter, priv->settings);
1455         for (i = 0; g_hash_table_iter_next (&iter, NULL, &value); i++)
1456                 arr[i] = NM_SETTING (value);
1457         g_assert (i == size);
1458
1459         /* sort the settings. This has an effect on the order in which keyfile
1460          * prints them. */
1461         if (size > 1)
1462                 g_qsort_with_data (arr, size, sizeof (NMSetting *), (GCompareDataFunc) _for_each_sort, NULL);
1463
1464         for (i = 0; i < size; i++)
1465                 nm_setting_enumerate_values (arr[i], func, user_data);
1466 }
1467
1468 /**
1469  * nm_connection_dump:
1470  * @connection: the #NMConnection
1471  *
1472  * Print the connection to stdout.  For debugging purposes ONLY, should NOT
1473  * be used for serialization of the connection or machine-parsed in any way. The
1474  * output format is not guaranteed to be stable and may change at any time.
1475  **/
1476 void
1477 nm_connection_dump (NMConnection *connection)
1478 {
1479         GHashTableIter iter;
1480         NMSetting *setting;
1481         const char *setting_name;
1482         char *str;
1483
1484         if (!connection)
1485                 return;
1486
1487         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1488         while (g_hash_table_iter_next (&iter, (gpointer) &setting_name, (gpointer) &setting)) {
1489                 str = nm_setting_to_string (setting);
1490                 g_print ("%s\n", str);
1491                 g_free (str);
1492         }
1493 }
1494
1495 /**
1496  * nm_connection_set_path:
1497  * @connection: the #NMConnection
1498  * @path: the D-Bus path of the connection as given by the settings service
1499  * which provides the connection
1500  *
1501  * Sets the D-Bus path of the connection.  This property is not serialized, and
1502  * is only for the reference of the caller.  Sets the #NMConnection:path
1503  * property.
1504  **/
1505 void
1506 nm_connection_set_path (NMConnection *connection, const char *path)
1507 {
1508         NMConnectionPrivate *priv;
1509
1510         g_return_if_fail (NM_IS_CONNECTION (connection));
1511
1512         priv = NM_CONNECTION_GET_PRIVATE (connection);
1513
1514         g_free (priv->path);
1515         priv->path = NULL;
1516
1517         if (path)
1518                 priv->path = g_strdup (path);
1519 }
1520
1521 /**
1522  * nm_connection_get_path:
1523  * @connection: the #NMConnection
1524  *
1525  * Returns the connection's D-Bus path.
1526  *
1527  * Returns: the D-Bus path of the connection, previously set by a call to
1528  * nm_connection_set_path().
1529  **/
1530 const char *
1531 nm_connection_get_path (NMConnection *connection)
1532 {
1533         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1534
1535         return NM_CONNECTION_GET_PRIVATE (connection)->path;
1536 }
1537
1538 /**
1539  * nm_connection_get_interface_name:
1540  * @connection: The #NMConnection
1541  *
1542  * Returns the interface name as stored in NMSettingConnection:interface_name.
1543  * If the connection contains no NMSettingConnection, it will return %NULL.
1544  *
1545  * For hardware devices and software devices created outside of NetworkManager,
1546  * this name is used to match the device. for software devices created by
1547  * NetworkManager, this is the name of the created interface.
1548  *
1549  * Returns: Name of the kernel interface or %NULL
1550  */
1551 const char *
1552 nm_connection_get_interface_name (NMConnection *connection)
1553 {
1554         NMSettingConnection *s_con;
1555
1556         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1557
1558         s_con = nm_connection_get_setting_connection (connection);
1559
1560         return s_con ? nm_setting_connection_get_interface_name (s_con) : NULL;
1561 }
1562
1563 gboolean
1564 _nm_connection_verify_required_interface_name (NMConnection *connection,
1565                                                GError **error)
1566 {
1567         const char *interface_name;
1568
1569         interface_name = nm_connection_get_interface_name (connection);
1570         if (interface_name)
1571                 return TRUE;
1572
1573         g_set_error_literal (error,
1574                              NM_CONNECTION_ERROR,
1575                              NM_CONNECTION_ERROR_MISSING_PROPERTY,
1576                              _("property is missing"));
1577         g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME);
1578         return FALSE;
1579 }
1580
1581 /**
1582  * nm_connection_get_uuid:
1583  * @connection: the #NMConnection
1584  *
1585  * A shortcut to return the UUID from the connection's #NMSettingConnection.
1586  *
1587  * Returns: the UUID from the connection's 'connection' setting
1588  **/
1589 const char *
1590 nm_connection_get_uuid (NMConnection *connection)
1591 {
1592         NMSettingConnection *s_con;
1593
1594         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1595
1596         s_con = nm_connection_get_setting_connection (connection);
1597         if (!s_con)
1598                 return NULL;
1599
1600         return nm_setting_connection_get_uuid (s_con);
1601 }
1602
1603 /**
1604  * nm_connection_get_id:
1605  * @connection: the #NMConnection
1606  *
1607  * A shortcut to return the ID from the connection's #NMSettingConnection.
1608  *
1609  * Returns: the ID from the connection's 'connection' setting
1610  **/
1611 const char *
1612 nm_connection_get_id (NMConnection *connection)
1613 {
1614         NMSettingConnection *s_con;
1615
1616         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1617
1618         s_con = nm_connection_get_setting_connection (connection);
1619         if (!s_con)
1620                 return NULL;
1621
1622         return nm_setting_connection_get_id (s_con);
1623 }
1624
1625 /**
1626  * nm_connection_get_connection_type:
1627  * @connection: the #NMConnection
1628  *
1629  * A shortcut to return the type from the connection's #NMSettingConnection.
1630  *
1631  * Returns: the type from the connection's 'connection' setting
1632  **/
1633 const char *
1634 nm_connection_get_connection_type (NMConnection *connection)
1635 {
1636         NMSettingConnection *s_con;
1637
1638         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1639
1640         s_con = nm_connection_get_setting_connection (connection);
1641         if (!s_con)
1642                 return NULL;
1643
1644         return nm_setting_connection_get_connection_type (s_con);
1645 }
1646
1647 /**
1648  * nm_connection_is_virtual:
1649  * @connection: an #NMConnection
1650  *
1651  * Checks if @connection refers to a virtual device (and thus can potentially be
1652  * activated even if the device it refers to doesn't exist).
1653  *
1654  * Returns: whether @connection refers to a virtual device
1655  */
1656 gboolean
1657 nm_connection_is_virtual (NMConnection *connection)
1658 {
1659         const char *type;
1660
1661         type = nm_connection_get_connection_type (connection);
1662         g_return_val_if_fail (type != NULL, FALSE);
1663
1664         if (   !strcmp (type, NM_SETTING_BOND_SETTING_NAME)
1665             || !strcmp (type, NM_SETTING_TEAM_SETTING_NAME)
1666             || !strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME)
1667             || !strcmp (type, NM_SETTING_VLAN_SETTING_NAME)
1668             || !strcmp (type, NM_SETTING_TUN_SETTING_NAME)
1669             || !strcmp (type, NM_SETTING_IP_TUNNEL_SETTING_NAME)
1670             || !strcmp (type, NM_SETTING_MACVLAN_SETTING_NAME)
1671             || !strcmp (type, NM_SETTING_VXLAN_SETTING_NAME))
1672                 return TRUE;
1673
1674         if (!strcmp (type, NM_SETTING_INFINIBAND_SETTING_NAME)) {
1675                 NMSettingInfiniband *s_ib;
1676
1677                 s_ib = nm_connection_get_setting_infiniband (connection);
1678                 g_return_val_if_fail (s_ib != NULL, FALSE);
1679                 return nm_setting_infiniband_get_virtual_interface_name (s_ib) != NULL;
1680         }
1681
1682         return FALSE;
1683 }
1684
1685 /**
1686  * nm_connection_get_virtual_device_description:
1687  * @connection: an #NMConnection for a virtual device type
1688  *
1689  * Returns the name that nm_device_disambiguate_names() would
1690  * return for the virtual device that would be created for @connection.
1691  * Eg, "VLAN (eth1.1)".
1692  *
1693  * Returns: (transfer full): the name of @connection's device,
1694  *   or %NULL if @connection is not a virtual connection type
1695  */
1696 char *
1697 nm_connection_get_virtual_device_description (NMConnection *connection)
1698 {
1699         const char *type;
1700         const char *iface = NULL, *display_type = NULL;
1701
1702         iface = nm_connection_get_interface_name (connection);
1703
1704         type = nm_connection_get_connection_type (connection);
1705         g_return_val_if_fail (type != NULL, FALSE);
1706
1707         if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1708                 display_type = _("Bond");
1709         else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1710                 display_type = _("Team");
1711         else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1712                 display_type = _("Bridge");
1713         else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1714                 display_type = _("VLAN");
1715         else if (!strcmp (type, NM_SETTING_INFINIBAND_SETTING_NAME)) {
1716                 display_type = _("InfiniBand");
1717                 iface = nm_setting_infiniband_get_virtual_interface_name (nm_connection_get_setting_infiniband (connection));
1718         }
1719
1720         if (!iface || !display_type)
1721                 return NULL;
1722
1723         return g_strdup_printf ("%s (%s)", display_type, iface);
1724 }
1725
1726 /*************************************************************/
1727
1728 /**
1729  * nm_connection_get_setting_802_1x:
1730  * @connection: the #NMConnection
1731  *
1732  * A shortcut to return any #NMSetting8021x the connection might contain.
1733  *
1734  * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1735  **/
1736 NMSetting8021x *
1737 nm_connection_get_setting_802_1x (NMConnection *connection)
1738 {
1739         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1740
1741         return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1742 }
1743
1744 /**
1745  * nm_connection_get_setting_bluetooth:
1746  * @connection: the #NMConnection
1747  *
1748  * A shortcut to return any #NMSettingBluetooth the connection might contain.
1749  *
1750  * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1751  **/
1752 NMSettingBluetooth *
1753 nm_connection_get_setting_bluetooth (NMConnection *connection)
1754 {
1755         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1756
1757         return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1758 }
1759
1760 /**
1761  * nm_connection_get_setting_bond:
1762  * @connection: the #NMConnection
1763  *
1764  * A shortcut to return any #NMSettingBond the connection might contain.
1765  *
1766  * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1767  **/
1768 NMSettingBond *
1769 nm_connection_get_setting_bond (NMConnection *connection)
1770 {
1771         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1772
1773         return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1774 }
1775
1776 /**
1777  * nm_connection_get_setting_team:
1778  * @connection: the #NMConnection
1779  *
1780  * A shortcut to return any #NMSettingTeam the connection might contain.
1781  *
1782  * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1783  **/
1784 NMSettingTeam *
1785 nm_connection_get_setting_team (NMConnection *connection)
1786 {
1787         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1788
1789         return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1790 }
1791
1792 /**
1793  * nm_connection_get_setting_team_port:
1794  * @connection: the #NMConnection
1795  *
1796  * A shortcut to return any #NMSettingTeamPort the connection might contain.
1797  *
1798  * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1799  **/
1800 NMSettingTeamPort *
1801 nm_connection_get_setting_team_port (NMConnection *connection)
1802 {
1803         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1804
1805         return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1806 }
1807
1808 /**
1809  * nm_connection_get_setting_bridge:
1810  * @connection: the #NMConnection
1811  *
1812  * A shortcut to return any #NMSettingBridge the connection might contain.
1813  *
1814  * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1815  **/
1816 NMSettingBridge *
1817 nm_connection_get_setting_bridge (NMConnection *connection)
1818 {
1819         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1820
1821         return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1822 }
1823
1824 /**
1825  * nm_connection_get_setting_cdma:
1826  * @connection: the #NMConnection
1827  *
1828  * A shortcut to return any #NMSettingCdma the connection might contain.
1829  *
1830  * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1831  **/
1832 NMSettingCdma *
1833 nm_connection_get_setting_cdma (NMConnection *connection)
1834 {
1835         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1836
1837         return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1838 }
1839
1840 /**
1841  * nm_connection_get_setting_connection:
1842  * @connection: the #NMConnection
1843  *
1844  * A shortcut to return any #NMSettingConnection the connection might contain.
1845  *
1846  * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1847  **/
1848 NMSettingConnection *
1849 nm_connection_get_setting_connection (NMConnection *connection)
1850 {
1851         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1852
1853         return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1854 }
1855
1856 /**
1857  * nm_connection_get_setting_dcb:
1858  * @connection: the #NMConnection
1859  *
1860  * A shortcut to return any #NMSettingDcb the connection might contain.
1861  *
1862  * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1863  **/
1864 NMSettingDcb *
1865 nm_connection_get_setting_dcb (NMConnection *connection)
1866 {
1867         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1868
1869         return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1870 }
1871
1872 /**
1873  * nm_connection_get_setting_generic:
1874  * @connection: the #NMConnection
1875  *
1876  * A shortcut to return any #NMSettingGeneric the connection might contain.
1877  *
1878  * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1879  **/
1880 NMSettingGeneric *
1881 nm_connection_get_setting_generic (NMConnection *connection)
1882 {
1883         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1884
1885         return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1886 }
1887
1888 /**
1889  * nm_connection_get_setting_gsm:
1890  * @connection: the #NMConnection
1891  *
1892  * A shortcut to return any #NMSettingGsm the connection might contain.
1893  *
1894  * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1895  **/
1896 NMSettingGsm *
1897 nm_connection_get_setting_gsm (NMConnection *connection)
1898 {
1899         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1900
1901         return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1902 }
1903
1904 /**
1905  * nm_connection_get_setting_infiniband:
1906  * @connection: the #NMConnection
1907  *
1908  * A shortcut to return any #NMSettingInfiniband the connection might contain.
1909  *
1910  * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1911  **/
1912 NMSettingInfiniband *
1913 nm_connection_get_setting_infiniband (NMConnection *connection)
1914 {
1915         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1916
1917         return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1918 }
1919
1920 /**
1921  * nm_connection_get_setting_ip4_config:
1922  * @connection: the #NMConnection
1923  *
1924  * A shortcut to return any #NMSettingIP4Config the connection might contain.
1925  *
1926  * Note that it returns the value as type #NMSettingIPConfig, since the vast
1927  * majority of IPv4-setting-related methods are on that type, not
1928  * #NMSettingIP4Config.
1929  *
1930  * Returns: (type NMSettingIP4Config) (transfer none): an #NMSettingIP4Config if the
1931  * connection contains one, otherwise %NULL
1932  **/
1933 NMSettingIPConfig *
1934 nm_connection_get_setting_ip4_config (NMConnection *connection)
1935 {
1936         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1937
1938         return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1939 }
1940
1941 /**
1942  * nm_connection_get_setting_ip_tunnel:
1943  * @connection: the #NMConnection
1944  *
1945  * A shortcut to return any #NMSettingIPTunnel the connection might contain.
1946  *
1947  * Returns: (transfer none): an #NMSettingIPTunnel if the connection contains one, otherwise %NULL
1948  *
1949  * Since: 1.2
1950  **/
1951 NMSettingIPTunnel *
1952 nm_connection_get_setting_ip_tunnel (NMConnection *connection)
1953 {
1954         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1955
1956         return (NMSettingIPTunnel *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP_TUNNEL);
1957 }
1958
1959 /**
1960  * nm_connection_get_setting_ip6_config:
1961  * @connection: the #NMConnection
1962  *
1963  * A shortcut to return any #NMSettingIP6Config the connection might contain.
1964  *
1965  * Note that it returns the value as type #NMSettingIPConfig, since the vast
1966  * majority of IPv6-setting-related methods are on that type, not
1967  * #NMSettingIP6Config.
1968  *
1969  * Returns: (type NMSettingIP6Config) (transfer none): an #NMSettingIP6Config if the
1970  * connection contains one, otherwise %NULL
1971  **/
1972 NMSettingIPConfig *
1973 nm_connection_get_setting_ip6_config (NMConnection *connection)
1974 {
1975         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1976
1977         return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1978 }
1979
1980 /**
1981  * nm_connection_get_setting_macvlan:
1982  * @connection: the #NMConnection
1983  *
1984  * A shortcut to return any #NMSettingMacvlan the connection might contain.
1985  *
1986  * Returns: (transfer none): an #NMSettingMacvlan if the connection contains one, otherwise %NULL
1987  *
1988  * Since: 1.2
1989  **/
1990 NMSettingMacvlan *
1991 nm_connection_get_setting_macvlan (NMConnection *connection)
1992 {
1993         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1994
1995         return (NMSettingMacvlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_MACVLAN);
1996 }
1997
1998 /**
1999  * nm_connection_get_setting_olpc_mesh:
2000  * @connection: the #NMConnection
2001  *
2002  * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
2003  *
2004  * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
2005  **/
2006 NMSettingOlpcMesh *
2007 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
2008 {
2009         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2010
2011         return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
2012 }
2013
2014 /**
2015  * nm_connection_get_setting_ppp:
2016  * @connection: the #NMConnection
2017  *
2018  * A shortcut to return any #NMSettingPpp the connection might contain.
2019  *
2020  * Returns: (transfer none): an #NMSettingPpp if the connection contains one, otherwise %NULL
2021  **/
2022 NMSettingPpp *
2023 nm_connection_get_setting_ppp (NMConnection *connection)
2024 {
2025         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2026
2027         return (NMSettingPpp *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
2028 }
2029
2030 /**
2031  * nm_connection_get_setting_pppoe:
2032  * @connection: the #NMConnection
2033  *
2034  * A shortcut to return any #NMSettingPppoe the connection might contain.
2035  *
2036  * Returns: (transfer none): an #NMSettingPppoe if the connection contains one, otherwise %NULL
2037  **/
2038 NMSettingPppoe *
2039 nm_connection_get_setting_pppoe (NMConnection *connection)
2040 {
2041         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2042
2043         return (NMSettingPppoe *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
2044 }
2045
2046 /**
2047  * nm_connection_get_setting_serial:
2048  * @connection: the #NMConnection
2049  *
2050  * A shortcut to return any #NMSettingSerial the connection might contain.
2051  *
2052  * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
2053  **/
2054 NMSettingSerial *
2055 nm_connection_get_setting_serial (NMConnection *connection)
2056 {
2057         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2058
2059         return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
2060 }
2061
2062 /**
2063  * nm_connection_get_setting_tun:
2064  * @connection: the #NMConnection
2065  *
2066  * A shortcut to return any #NMSettingTun the connection might contain.
2067  *
2068  * Returns: (transfer none): an #NMSettingTun if the connection contains one, otherwise %NULL
2069  *
2070  * Since: 1.2
2071  **/
2072 NMSettingTun *
2073 nm_connection_get_setting_tun (NMConnection *connection)
2074 {
2075         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2076
2077         return (NMSettingTun *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TUN);
2078 }
2079
2080 /**
2081  * nm_connection_get_setting_vpn:
2082  * @connection: the #NMConnection
2083  *
2084  * A shortcut to return any #NMSettingVpn the connection might contain.
2085  *
2086  * Returns: (transfer none): an #NMSettingVpn if the connection contains one, otherwise %NULL
2087  **/
2088 NMSettingVpn *
2089 nm_connection_get_setting_vpn (NMConnection *connection)
2090 {
2091         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2092
2093         return (NMSettingVpn *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
2094 }
2095
2096 /**
2097  * nm_connection_get_setting_vxlan:
2098  * @connection: the #NMConnection
2099  *
2100  * A shortcut to return any #NMSettingVxlan the connection might contain.
2101  *
2102  * Returns: (transfer none): an #NMSettingVxlan if the connection contains one, otherwise %NULL
2103  *
2104  * Since: 1.2
2105  **/
2106 NMSettingVxlan *
2107 nm_connection_get_setting_vxlan (NMConnection *connection)
2108 {
2109         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2110
2111         return (NMSettingVxlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VXLAN);
2112 }
2113
2114 /**
2115  * nm_connection_get_setting_wimax:
2116  * @connection: the #NMConnection
2117  *
2118  * A shortcut to return any #NMSettingWimax the connection might contain.
2119  *
2120  * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
2121  **/
2122 NMSettingWimax *
2123 nm_connection_get_setting_wimax (NMConnection *connection)
2124 {
2125         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2126
2127         return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
2128 }
2129
2130 /**
2131  * nm_connection_get_setting_wired:
2132  * @connection: the #NMConnection
2133  *
2134  * A shortcut to return any #NMSettingWired the connection might contain.
2135  *
2136  * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
2137  **/
2138 NMSettingWired *
2139 nm_connection_get_setting_wired (NMConnection *connection)
2140 {
2141         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2142
2143         return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
2144 }
2145
2146 /**
2147  * nm_connection_get_setting_adsl:
2148  * @connection: the #NMConnection
2149  *
2150  * A shortcut to return any #NMSettingAdsl the connection might contain.
2151  *
2152  * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
2153  **/
2154 NMSettingAdsl *
2155 nm_connection_get_setting_adsl (NMConnection *connection)
2156 {
2157         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2158
2159         return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
2160 }
2161
2162 /**
2163  * nm_connection_get_setting_wireless:
2164  * @connection: the #NMConnection
2165  *
2166  * A shortcut to return any #NMSettingWireless the connection might contain.
2167  *
2168  * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
2169  **/
2170 NMSettingWireless *
2171 nm_connection_get_setting_wireless (NMConnection *connection)
2172 {
2173         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2174
2175         return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
2176 }
2177
2178 /**
2179  * nm_connection_get_setting_wireless_security:
2180  * @connection: the #NMConnection
2181  *
2182  * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
2183  *
2184  * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
2185  **/
2186 NMSettingWirelessSecurity *
2187 nm_connection_get_setting_wireless_security (NMConnection *connection)
2188 {
2189         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2190
2191         return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2192 }
2193
2194 /**
2195  * nm_connection_get_setting_bridge_port:
2196  * @connection: the #NMConnection
2197  *
2198  * A shortcut to return any #NMSettingBridgePort the connection might contain.
2199  *
2200  * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2201  **/
2202 NMSettingBridgePort *
2203 nm_connection_get_setting_bridge_port (NMConnection *connection)
2204 {
2205         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2206
2207         return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2208 }
2209
2210 /**
2211  * nm_connection_get_setting_vlan:
2212  * @connection: the #NMConnection
2213  *
2214  * A shortcut to return any #NMSettingVlan the connection might contain.
2215  *
2216  * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2217  **/
2218 NMSettingVlan *
2219 nm_connection_get_setting_vlan (NMConnection *connection)
2220 {
2221         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2222
2223         return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2224 }
2225
2226 /*************************************************************/
2227
2228 static void
2229 nm_connection_private_free (NMConnectionPrivate *priv)
2230 {
2231         NMConnection *self = priv->self;
2232
2233         g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2234         g_hash_table_destroy (priv->settings);
2235         g_free (priv->path);
2236
2237         g_slice_free (NMConnectionPrivate, priv);
2238 }
2239
2240 static NMConnectionPrivate *
2241 nm_connection_get_private (NMConnection *connection)
2242 {
2243         NMConnectionPrivate *priv;
2244
2245         priv = g_object_get_data (G_OBJECT (connection), "NMConnectionPrivate");
2246         if (!priv) {
2247                 priv = g_slice_new0 (NMConnectionPrivate);
2248                 g_object_set_data_full (G_OBJECT (connection), "NMConnectionPrivate",
2249                                         priv, (GDestroyNotify) nm_connection_private_free);
2250
2251                 priv->self = connection;
2252                 priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2253         }
2254
2255         return priv;
2256 }
2257
2258 static void
2259 nm_connection_default_init (NMConnectionInterface *iface)
2260 {
2261         /* Signals */
2262
2263         /**
2264          * NMConnection::secrets-updated:
2265          * @connection: the object on which the signal is emitted
2266          * @setting_name: the setting name of the #NMSetting for which secrets were
2267          * updated
2268          *
2269          * The ::secrets-updated signal is emitted when the secrets of a setting
2270          * have been changed.
2271          */
2272         signals[SECRETS_UPDATED] =
2273                 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2274                               NM_TYPE_CONNECTION,
2275                               G_SIGNAL_RUN_FIRST,
2276                               G_STRUCT_OFFSET (NMConnectionInterface, secrets_updated),
2277                               NULL, NULL,
2278                               g_cclosure_marshal_VOID__STRING,
2279                               G_TYPE_NONE, 1,
2280                               G_TYPE_STRING);
2281
2282         /**
2283          * NMConnection::secrets-cleared:
2284          * @connection: the object on which the signal is emitted
2285          *
2286          * The ::secrets-cleared signal is emitted when the secrets of a connection
2287          * are cleared.
2288          */
2289         signals[SECRETS_CLEARED] =
2290                 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2291                               NM_TYPE_CONNECTION,
2292                               G_SIGNAL_RUN_FIRST,
2293                               G_STRUCT_OFFSET (NMConnectionInterface, secrets_cleared),
2294                               NULL, NULL,
2295                               g_cclosure_marshal_VOID__VOID,
2296                               G_TYPE_NONE, 0);
2297
2298         /**
2299          * NMConnection::changed:
2300          * @connection: the object on which the signal is emitted
2301          *
2302          * The ::changed signal is emitted when any property of any property
2303          * (including secrets) of any setting of the connection is modified,
2304          * or when settings are added or removed.
2305          */
2306         signals[CHANGED] =
2307                 g_signal_new (NM_CONNECTION_CHANGED,
2308                               NM_TYPE_CONNECTION,
2309                               G_SIGNAL_RUN_FIRST,
2310                               G_STRUCT_OFFSET (NMConnectionInterface, changed),
2311                               NULL, NULL,
2312                               g_cclosure_marshal_VOID__VOID,
2313                               G_TYPE_NONE, 0);
2314 }