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