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