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