c440fb1b2824a87cca84ed87c9a5852a47f11480
[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 gboolean
331 hash_to_connection (NMConnection *connection, GHashTable *new, GError **error)
332 {
333         GHashTableIter iter;
334         const char *setting_name;
335         GHashTable *setting_hash;
336         gboolean changed, valid;
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         valid = nm_connection_verify (connection, error);
357         if (changed)
358                 g_signal_emit (connection, signals[CHANGED], 0);
359         return valid;
360 }
361
362 /**
363  * nm_connection_replace_settings:
364  * @connection: a #NMConnection
365  * @new_settings: (element-type utf8 GLib.HashTable): a #GHashTable of settings
366  * @error: location to store error, or %NULL
367  *
368  * Returns: %TRUE if the settings were valid and added to the connection, %FALSE
369  * if they were not
370  **/
371 gboolean
372 nm_connection_replace_settings (NMConnection *connection,
373                                 GHashTable *new_settings,
374                                 GError **error)
375 {
376         gboolean valid = FALSE;
377
378         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
379         g_return_val_if_fail (new_settings != NULL, FALSE);
380         if (error)
381                 g_return_val_if_fail (*error == NULL, FALSE);
382
383         if (validate_permissions_type (new_settings, error))
384                 valid = hash_to_connection (connection, new_settings, error);
385         return valid;
386 }
387
388 /**
389  * nm_connection_replace_settings_from_connection:
390  * @connection: a #NMConnection
391  * @new_connection: a #NMConnection to replace the settings of @connection with
392  * @error: location to store error, or %NULL
393  *
394  * Deep-copies the settings of @new_conenction and replaces the settings of @connection
395  * with the copied settings.
396  *
397  * Returns: %TRUE if the settings were valid after replacing the connection, %FALSE
398  * if they were not. Regardless of whether %TRUE or %FALSE is returned, the connection
399  * is successfully replaced. %FALSE only means, that the connection does not verify
400  * at the end of the operation.
401  *
402  * Since: 0.9.10
403  **/
404 gboolean
405 nm_connection_replace_settings_from_connection (NMConnection *connection,
406                                                 NMConnection *new_connection,
407                                                 GError **error)
408 {
409         NMConnectionPrivate *priv;
410         GHashTableIter iter;
411         NMSetting *setting;
412         gboolean changed, valid;
413
414         g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
415         g_return_val_if_fail (NM_IS_CONNECTION (new_connection), FALSE);
416         g_return_val_if_fail (!error || !*error, FALSE);
417
418         /* When 'connection' and 'new_connection' are the same object simply return
419          * in order not to destroy 'connection' */
420         if (connection == new_connection)
421                 return TRUE;
422
423         /* No need to validate permissions like nm_connection_replace_settings()
424          * since we're dealing with an NMConnection which has already done that.
425          */
426
427         priv = NM_CONNECTION_GET_PRIVATE (connection);
428         if ((changed = g_hash_table_size (priv->settings) > 0))
429                 g_hash_table_foreach_remove (priv->settings, _setting_release, connection);
430
431         if (g_hash_table_size (NM_CONNECTION_GET_PRIVATE (new_connection)->settings)) {
432                 g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (new_connection)->settings);
433                 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
434                         _nm_connection_add_setting (connection, nm_setting_duplicate (setting));
435                 changed = TRUE;
436         }
437
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         if (!hash_to_connection (connection, hash, error)) {
1468                 g_object_unref (connection);
1469                 return NULL;
1470         }
1471         return connection;
1472 }
1473
1474 /**
1475  * nm_connection_duplicate:
1476  * @connection: the #NMConnection to duplicate
1477  *
1478  * Duplicates a #NMConnection.
1479  *
1480  * Returns: (transfer full): a new #NMConnection containing the same settings and properties
1481  * as the source #NMConnection
1482  **/
1483 NMConnection *
1484 nm_connection_duplicate (NMConnection *connection)
1485 {
1486         NMConnection *dup;
1487         GHashTableIter iter;
1488         NMSetting *setting;
1489
1490         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1491
1492         dup = nm_connection_new ();
1493         nm_connection_set_path (dup, nm_connection_get_path (connection));
1494
1495         g_hash_table_iter_init (&iter, NM_CONNECTION_GET_PRIVATE (connection)->settings);
1496         while (g_hash_table_iter_next (&iter, NULL, (gpointer) &setting))
1497                 _nm_connection_add_setting (dup, nm_setting_duplicate (setting));
1498
1499         return dup;
1500 }
1501
1502 /**
1503  * nm_connection_get_uuid:
1504  * @connection: the #NMConnection
1505  *
1506  * A shortcut to return the UUID from the connection's #NMSettingConnection.
1507  *
1508  * Returns: the UUID from the connection's 'connection' setting
1509  **/
1510 const char *
1511 nm_connection_get_uuid (NMConnection *connection)
1512 {
1513         NMSettingConnection *s_con;
1514
1515         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1516
1517         s_con = nm_connection_get_setting_connection (connection);
1518         g_return_val_if_fail (s_con != NULL, NULL);
1519
1520         return nm_setting_connection_get_uuid (s_con);
1521 }
1522
1523 /**
1524  * nm_connection_get_id:
1525  * @connection: the #NMConnection
1526  *
1527  * A shortcut to return the ID from the connection's #NMSettingConnection.
1528  *
1529  * Returns: the ID from the connection's 'connection' setting
1530  **/
1531 const char *
1532 nm_connection_get_id (NMConnection *connection)
1533 {
1534         NMSettingConnection *s_con;
1535
1536         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1537
1538         s_con = nm_connection_get_setting_connection (connection);
1539         g_return_val_if_fail (s_con != NULL, NULL);
1540
1541         return nm_setting_connection_get_id (s_con);
1542 }
1543
1544 /**
1545  * nm_connection_get_connection_type:
1546  * @connection: the #NMConnection
1547  *
1548  * A shortcut to return the type from the connection's #NMSettingConnection.
1549  *
1550  * Returns: the type from the connection's 'connection' setting
1551  *
1552  * Since: 0.9.10
1553  **/
1554 const char *
1555 nm_connection_get_connection_type (NMConnection *connection)
1556 {
1557         NMSettingConnection *s_con;
1558
1559         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1560
1561         s_con = nm_connection_get_setting_connection (connection);
1562         g_return_val_if_fail (s_con != NULL, NULL);
1563
1564         return nm_setting_connection_get_connection_type (s_con);
1565 }
1566
1567 /**
1568  * nm_connection_get_virtual_device_description:
1569  * @connection: an #NMConnection for a virtual device type
1570  *
1571  * Returns the name that nm_device_disambiguate_names() would
1572  * return for the virtual device that would be created for @connection.
1573  * Eg, "VLAN (eth1.1)".
1574  *
1575  * Returns: (transfer full): the name of @connection's device,
1576  *   or %NULL if @connection is not a virtual connection type
1577  *
1578  * Since: 0.9.10
1579  */
1580 char *
1581 nm_connection_get_virtual_device_description (NMConnection *connection)
1582 {
1583         const char *iface, *type, *display_type;
1584         NMSettingConnection *s_con;
1585
1586         iface = nm_connection_get_virtual_iface_name (connection);
1587         if (!iface)
1588                 return NULL;
1589
1590         s_con = nm_connection_get_setting_connection (connection);
1591         g_return_val_if_fail (s_con != NULL, NULL);
1592         type = nm_setting_connection_get_connection_type (s_con);
1593
1594         if (!strcmp (type, NM_SETTING_BOND_SETTING_NAME))
1595                 display_type = _("Bond");
1596         else if (!strcmp (type, NM_SETTING_TEAM_SETTING_NAME))
1597                 display_type = _("Team");
1598         else if (!strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME))
1599                 display_type = _("Bridge");
1600         else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME))
1601                 display_type = _("VLAN");
1602         else {
1603                 g_warning ("Unrecognized virtual device type '%s'", type);
1604                 display_type = type;
1605         }
1606
1607         return g_strdup_printf ("%s (%s)", display_type, iface);
1608 }
1609
1610 /*************************************************************/
1611
1612 /**
1613  * nm_connection_get_setting_802_1x:
1614  * @connection: the #NMConnection
1615  *
1616  * A shortcut to return any #NMSetting8021x the connection might contain.
1617  *
1618  * Returns: (transfer none): an #NMSetting8021x if the connection contains one, otherwise %NULL
1619  **/
1620 NMSetting8021x *
1621 nm_connection_get_setting_802_1x (NMConnection *connection)
1622 {
1623         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1624
1625         return (NMSetting8021x *) nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
1626 }
1627
1628 /**
1629  * nm_connection_get_setting_bluetooth:
1630  * @connection: the #NMConnection
1631  *
1632  * A shortcut to return any #NMSettingBluetooth the connection might contain.
1633  *
1634  * Returns: (transfer none): an #NMSettingBluetooth if the connection contains one, otherwise %NULL
1635  **/
1636 NMSettingBluetooth *
1637 nm_connection_get_setting_bluetooth (NMConnection *connection)
1638 {
1639         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1640
1641         return (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
1642 }
1643
1644 /**
1645  * nm_connection_get_setting_bond:
1646  * @connection: the #NMConnection
1647  *
1648  * A shortcut to return any #NMSettingBond the connection might contain.
1649  *
1650  * Returns: (transfer none): an #NMSettingBond if the connection contains one, otherwise %NULL
1651  **/
1652 NMSettingBond *
1653 nm_connection_get_setting_bond (NMConnection *connection)
1654 {
1655         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1656
1657         return (NMSettingBond *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BOND);
1658 }
1659
1660 /**
1661  * nm_connection_get_setting_team:
1662  * @connection: the #NMConnection
1663  *
1664  * A shortcut to return any #NMSettingTeam the connection might contain.
1665  *
1666  * Returns: (transfer none): an #NMSettingTeam if the connection contains one, otherwise %NULL
1667  *
1668  * Since: 0.9.10
1669  **/
1670 NMSettingTeam *
1671 nm_connection_get_setting_team (NMConnection *connection)
1672 {
1673         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1674
1675         return (NMSettingTeam *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM);
1676 }
1677
1678 /**
1679  * nm_connection_get_setting_team_port:
1680  * @connection: the #NMConnection
1681  *
1682  * A shortcut to return any #NMSettingTeamPort the connection might contain.
1683  *
1684  * Returns: (transfer none): an #NMSettingTeamPort if the connection contains one, otherwise %NULL
1685  *
1686  * Since: 0.9.10
1687  **/
1688 NMSettingTeamPort *
1689 nm_connection_get_setting_team_port (NMConnection *connection)
1690 {
1691         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1692
1693         return (NMSettingTeamPort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_TEAM_PORT);
1694 }
1695
1696 /**
1697  * nm_connection_get_setting_bridge:
1698  * @connection: the #NMConnection
1699  *
1700  * A shortcut to return any #NMSettingBridge the connection might contain.
1701  *
1702  * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise %NULL
1703  **/
1704 NMSettingBridge *
1705 nm_connection_get_setting_bridge (NMConnection *connection)
1706 {
1707         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1708
1709         return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
1710 }
1711
1712 /**
1713  * nm_connection_get_setting_cdma:
1714  * @connection: the #NMConnection
1715  *
1716  * A shortcut to return any #NMSettingCdma the connection might contain.
1717  *
1718  * Returns: (transfer none): an #NMSettingCdma if the connection contains one, otherwise %NULL
1719  **/
1720 NMSettingCdma *
1721 nm_connection_get_setting_cdma (NMConnection *connection)
1722 {
1723         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1724
1725         return (NMSettingCdma *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CDMA);
1726 }
1727
1728 /**
1729  * nm_connection_get_setting_connection:
1730  * @connection: the #NMConnection
1731  *
1732  * A shortcut to return any #NMSettingConnection the connection might contain.
1733  *
1734  * Returns: (transfer none): an #NMSettingConnection if the connection contains one, otherwise %NULL
1735  **/
1736 NMSettingConnection *
1737 nm_connection_get_setting_connection (NMConnection *connection)
1738 {
1739         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1740
1741         return (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
1742 }
1743
1744 /**
1745  * nm_connection_get_setting_dcb:
1746  * @connection: the #NMConnection
1747  *
1748  * A shortcut to return any #NMSettingDcb the connection might contain.
1749  *
1750  * Returns: (transfer none): an #NMSettingDcb if the connection contains one, otherwise NULL
1751  *
1752  * Since: 0.9.10
1753  **/
1754 NMSettingDcb *
1755 nm_connection_get_setting_dcb (NMConnection *connection)
1756 {
1757         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1758
1759         return (NMSettingDcb *) nm_connection_get_setting (connection, NM_TYPE_SETTING_DCB);
1760 }
1761
1762 /**
1763  * nm_connection_get_setting_generic:
1764  * @connection: the #NMConnection
1765  *
1766  * A shortcut to return any #NMSettingGeneric the connection might contain.
1767  *
1768  * Returns: (transfer none): an #NMSettingGeneric if the connection contains one, otherwise NULL
1769  *
1770  * Since: 0.9.10
1771  **/
1772 NMSettingGeneric *
1773 nm_connection_get_setting_generic (NMConnection *connection)
1774 {
1775         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1776
1777         return (NMSettingGeneric *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GENERIC);
1778 }
1779
1780 /**
1781  * nm_connection_get_setting_gsm:
1782  * @connection: the #NMConnection
1783  *
1784  * A shortcut to return any #NMSettingGsm the connection might contain.
1785  *
1786  * Returns: (transfer none): an #NMSettingGsm if the connection contains one, otherwise %NULL
1787  **/
1788 NMSettingGsm *
1789 nm_connection_get_setting_gsm (NMConnection *connection)
1790 {
1791         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1792
1793         return (NMSettingGsm *) nm_connection_get_setting (connection, NM_TYPE_SETTING_GSM);
1794 }
1795
1796 /**
1797  * nm_connection_get_setting_infiniband:
1798  * @connection: the #NMConnection
1799  *
1800  * A shortcut to return any #NMSettingInfiniband the connection might contain.
1801  *
1802  * Returns: (transfer none): an #NMSettingInfiniband if the connection contains one, otherwise %NULL
1803  **/
1804 NMSettingInfiniband *
1805 nm_connection_get_setting_infiniband (NMConnection *connection)
1806 {
1807         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1808
1809         return (NMSettingInfiniband *) nm_connection_get_setting (connection, NM_TYPE_SETTING_INFINIBAND);
1810 }
1811
1812 /**
1813  * nm_connection_get_setting_ip4_config:
1814  * @connection: the #NMConnection
1815  *
1816  * A shortcut to return any #NMSettingIP4Config the connection might contain.
1817  *
1818  * Returns: (transfer none): an #NMSettingIP4Config if the connection contains one, otherwise %NULL
1819  **/
1820 NMSettingIP4Config *
1821 nm_connection_get_setting_ip4_config (NMConnection *connection)
1822 {
1823         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1824
1825         return (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
1826 }
1827
1828 /**
1829  * nm_connection_get_setting_ip6_config:
1830  * @connection: the #NMConnection
1831  *
1832  * A shortcut to return any #NMSettingIP6Config the connection might contain.
1833  *
1834  * Returns: (transfer none): an #NMSettingIP6Config if the connection contains one, otherwise %NULL
1835  **/
1836 NMSettingIP6Config *
1837 nm_connection_get_setting_ip6_config (NMConnection *connection)
1838 {
1839         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1840
1841         return (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG);
1842 }
1843
1844 /**
1845  * nm_connection_get_setting_olpc_mesh:
1846  * @connection: the #NMConnection
1847  *
1848  * A shortcut to return any #NMSettingOlpcMesh the connection might contain.
1849  *
1850  * Returns: (transfer none): an #NMSettingOlpcMesh if the connection contains one, otherwise %NULL
1851  **/
1852 NMSettingOlpcMesh *
1853 nm_connection_get_setting_olpc_mesh (NMConnection *connection)
1854 {
1855         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1856
1857         return (NMSettingOlpcMesh *) nm_connection_get_setting (connection, NM_TYPE_SETTING_OLPC_MESH);
1858 }
1859
1860 /**
1861  * nm_connection_get_setting_ppp:
1862  * @connection: the #NMConnection
1863  *
1864  * A shortcut to return any #NMSettingPPP the connection might contain.
1865  *
1866  * Returns: (transfer none): an #NMSettingPPP if the connection contains one, otherwise %NULL
1867  **/
1868 NMSettingPPP *
1869 nm_connection_get_setting_ppp (NMConnection *connection)
1870 {
1871         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1872
1873         return (NMSettingPPP *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP);
1874 }
1875
1876 /**
1877  * nm_connection_get_setting_pppoe:
1878  * @connection: the #NMConnection
1879  *
1880  * A shortcut to return any #NMSettingPPPOE the connection might contain.
1881  *
1882  * Returns: (transfer none): an #NMSettingPPPOE if the connection contains one, otherwise %NULL
1883  **/
1884 NMSettingPPPOE *
1885 nm_connection_get_setting_pppoe (NMConnection *connection)
1886 {
1887         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1888
1889         return (NMSettingPPPOE *) nm_connection_get_setting (connection, NM_TYPE_SETTING_PPPOE);
1890 }
1891
1892 /**
1893  * nm_connection_get_setting_serial:
1894  * @connection: the #NMConnection
1895  *
1896  * A shortcut to return any #NMSettingSerial the connection might contain.
1897  *
1898  * Returns: (transfer none): an #NMSettingSerial if the connection contains one, otherwise %NULL
1899  **/
1900 NMSettingSerial *
1901 nm_connection_get_setting_serial (NMConnection *connection)
1902 {
1903         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1904
1905         return (NMSettingSerial *) nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL);
1906 }
1907
1908 /**
1909  * nm_connection_get_setting_vpn:
1910  * @connection: the #NMConnection
1911  *
1912  * A shortcut to return any #NMSettingVPN the connection might contain.
1913  *
1914  * Returns: (transfer none): an #NMSettingVPN if the connection contains one, otherwise %NULL
1915  **/
1916 NMSettingVPN *
1917 nm_connection_get_setting_vpn (NMConnection *connection)
1918 {
1919         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1920
1921         return (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
1922 }
1923
1924 /**
1925  * nm_connection_get_setting_wimax:
1926  * @connection: the #NMConnection
1927  *
1928  * A shortcut to return any #NMSettingWimax the connection might contain.
1929  *
1930  * Returns: (transfer none): an #NMSettingWimax if the connection contains one, otherwise %NULL
1931  **/
1932 NMSettingWimax *
1933 nm_connection_get_setting_wimax (NMConnection *connection)
1934 {
1935         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1936
1937         return (NMSettingWimax *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIMAX);
1938 }
1939
1940 /**
1941  * nm_connection_get_setting_wired:
1942  * @connection: the #NMConnection
1943  *
1944  * A shortcut to return any #NMSettingWired the connection might contain.
1945  *
1946  * Returns: (transfer none): an #NMSettingWired if the connection contains one, otherwise %NULL
1947  **/
1948 NMSettingWired *
1949 nm_connection_get_setting_wired (NMConnection *connection)
1950 {
1951         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1952
1953         return (NMSettingWired *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED);
1954 }
1955
1956 /**
1957  * nm_connection_get_setting_adsl:
1958  * @connection: the #NMConnection
1959  *
1960  * A shortcut to return any #NMSettingAdsl the connection might contain.
1961  *
1962  * Returns: (transfer none): an #NMSettingAdsl if the connection contains one, otherwise %NULL
1963  **/
1964 NMSettingAdsl *
1965 nm_connection_get_setting_adsl (NMConnection *connection)
1966 {
1967         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1968
1969         return (NMSettingAdsl *) nm_connection_get_setting (connection, NM_TYPE_SETTING_ADSL);
1970 }
1971
1972 /**
1973  * nm_connection_get_setting_wireless:
1974  * @connection: the #NMConnection
1975  *
1976  * A shortcut to return any #NMSettingWireless the connection might contain.
1977  *
1978  * Returns: (transfer none): an #NMSettingWireless if the connection contains one, otherwise %NULL
1979  **/
1980 NMSettingWireless *
1981 nm_connection_get_setting_wireless (NMConnection *connection)
1982 {
1983         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
1984
1985         return (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
1986 }
1987
1988 /**
1989  * nm_connection_get_setting_wireless_security:
1990  * @connection: the #NMConnection
1991  *
1992  * A shortcut to return any #NMSettingWirelessSecurity the connection might contain.
1993  *
1994  * Returns: (transfer none): an #NMSettingWirelessSecurity if the connection contains one, otherwise %NULL
1995  **/
1996 NMSettingWirelessSecurity *
1997 nm_connection_get_setting_wireless_security (NMConnection *connection)
1998 {
1999         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2000
2001         return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
2002 }
2003
2004 /**
2005  * nm_connection_get_setting_bridge_port:
2006  * @connection: the #NMConnection
2007  *
2008  * A shortcut to return any #NMSettingBridgePort the connection might contain.
2009  *
2010  * Returns: (transfer none): an #NMSettingBridgePort if the connection contains one, otherwise %NULL
2011  **/
2012 NMSettingBridgePort *
2013 nm_connection_get_setting_bridge_port (NMConnection *connection)
2014 {
2015         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2016
2017         return (NMSettingBridgePort *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE_PORT);
2018 }
2019
2020 /**
2021  * nm_connection_get_setting_vlan:
2022  * @connection: the #NMConnection
2023  *
2024  * A shortcut to return any #NMSettingVlan the connection might contain.
2025  *
2026  * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise %NULL
2027  **/
2028 NMSettingVlan *
2029 nm_connection_get_setting_vlan (NMConnection *connection)
2030 {
2031         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
2032
2033         return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
2034 }
2035
2036 /*************************************************************/
2037
2038 static void
2039 nm_connection_init (NMConnection *connection)
2040 {
2041         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2042
2043         priv->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
2044 }
2045
2046 static void
2047 dispose (GObject *object)
2048 {
2049         NMConnection *self = NM_CONNECTION (object);
2050         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (self);
2051
2052         g_hash_table_foreach_remove (priv->settings, _setting_release, self);
2053
2054         G_OBJECT_CLASS (nm_connection_parent_class)->dispose (object);
2055 }
2056
2057 static void
2058 finalize (GObject *object)
2059 {
2060         NMConnection *connection = NM_CONNECTION (object);
2061         NMConnectionPrivate *priv = NM_CONNECTION_GET_PRIVATE (connection);
2062
2063         g_assert (g_hash_table_size (priv->settings) == 0);
2064         g_hash_table_destroy (priv->settings);
2065         g_free (priv->path);
2066
2067         G_OBJECT_CLASS (nm_connection_parent_class)->finalize (object);
2068 }
2069
2070 static void
2071 set_property (GObject *object, guint prop_id,
2072               const GValue *value, GParamSpec *pspec)
2073 {
2074         NMConnection *connection = NM_CONNECTION (object);
2075
2076         switch (prop_id) {
2077         case PROP_PATH:
2078                 nm_connection_set_path (connection, g_value_get_string (value));
2079                 break;
2080         default:
2081                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2082                 break;
2083         }
2084 }
2085
2086 static void
2087 get_property (GObject *object, guint prop_id,
2088               GValue *value, GParamSpec *pspec)
2089 {
2090         NMConnection *connection = NM_CONNECTION (object);
2091
2092         switch (prop_id) {
2093         case PROP_PATH:
2094                 g_value_set_string (value, nm_connection_get_path (connection));
2095                 break;
2096         default:
2097                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2098                 break;
2099         }
2100 }
2101
2102 static void
2103 nm_connection_class_init (NMConnectionClass *klass)
2104 {
2105         GObjectClass *object_class = G_OBJECT_CLASS (klass);
2106
2107         g_type_class_add_private (klass, sizeof (NMConnectionPrivate));
2108
2109         /* virtual methods */
2110         object_class->set_property = set_property;
2111         object_class->get_property = get_property;
2112         object_class->dispose = dispose;
2113         object_class->finalize = finalize;
2114
2115         /* Properties */
2116
2117         /**
2118          * NMConnection:path:
2119          *
2120          * The connection's D-Bus path, used only by the calling process as a record
2121          * of the D-Bus path of the connection as provided by a settings service.
2122          **/
2123         g_object_class_install_property
2124                 (object_class, PROP_PATH,
2125                  g_param_spec_string (NM_CONNECTION_PATH, "", "",
2126                                       NULL,
2127                                       G_PARAM_READWRITE |
2128                                       G_PARAM_CONSTRUCT |
2129                                       G_PARAM_STATIC_STRINGS));
2130
2131         /* Signals */
2132
2133         /**
2134          * NMConnection::secrets-updated:
2135          * @connection: the object on which the signal is emitted
2136          * @setting_name: the setting name of the #NMSetting for which secrets were
2137          * updated
2138          *
2139          * The ::secrets-updated signal is emitted when the secrets of a setting
2140          * have been changed.
2141          */
2142         signals[SECRETS_UPDATED] =
2143                 g_signal_new (NM_CONNECTION_SECRETS_UPDATED,
2144                               G_OBJECT_CLASS_TYPE (object_class),
2145                               G_SIGNAL_RUN_FIRST,
2146                               G_STRUCT_OFFSET (NMConnectionClass, secrets_updated),
2147                               NULL, NULL,
2148                               g_cclosure_marshal_VOID__STRING,
2149                               G_TYPE_NONE, 1,
2150                               G_TYPE_STRING);
2151
2152         /**
2153          * NMConnection::secrets-cleared:
2154          * @connection: the object on which the signal is emitted
2155          *
2156          * The ::secrets-cleared signal is emitted when the secrets of a connection
2157          * are cleared.
2158          */
2159         signals[SECRETS_CLEARED] =
2160                 g_signal_new (NM_CONNECTION_SECRETS_CLEARED,
2161                               G_OBJECT_CLASS_TYPE (object_class),
2162                               G_SIGNAL_RUN_FIRST,
2163                               0, NULL, NULL,
2164                               g_cclosure_marshal_VOID__VOID,
2165                               G_TYPE_NONE, 0);
2166
2167         /**
2168          * NMConnection::changed:
2169          * @connection: the object on which the signal is emitted
2170          *
2171          * The ::changed signal is emitted when any property of any property
2172          * (including secrets) of any setting of the connection is modified,
2173          * or when settings are added or removed.
2174          *
2175          * Since: 0.9.10
2176          */
2177         signals[CHANGED] =
2178                 g_signal_new (NM_CONNECTION_CHANGED,
2179                               G_OBJECT_CLASS_TYPE (object_class),
2180                               G_SIGNAL_RUN_FIRST,
2181                               0, NULL, NULL,
2182                               g_cclosure_marshal_VOID__VOID,
2183                               G_TYPE_NONE, 0);
2184 }