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