libnm: don't check if the agent is still registered when unregistering
[NetworkManager.git] / libnm / nm-secret-agent-old.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301 USA.
17  *
18  * Copyright 2010 - 2011 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-dbus-interface.h"
26 #include "nm-secret-agent-old.h"
27 #include "nm-enum-types.h"
28 #include "nm-dbus-helpers.h"
29 #include "nm-simple-connection.h"
30 #include "nm-core-internal.h"
31
32 #include "nmdbus-secret-agent.h"
33 #include "nmdbus-agent-manager.h"
34
35 static void nm_secret_agent_old_initable_iface_init (GInitableIface *iface);
36 static void nm_secret_agent_old_async_initable_iface_init (GAsyncInitableIface *iface);
37 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (NMSecretAgentOld, nm_secret_agent_old, G_TYPE_OBJECT,
38                                   G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_secret_agent_old_initable_iface_init);
39                                   G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_secret_agent_old_async_initable_iface_init);
40                                   )
41
42 #define NM_SECRET_AGENT_OLD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SECRET_AGENT_OLD, NMSecretAgentOldPrivate))
43
44 typedef struct {
45         gboolean registered;
46         gboolean registering;
47         NMSecretAgentCapabilities capabilities;
48
49         GDBusConnection *bus;
50         gboolean private_bus;
51         gboolean session_bus;
52         NMDBusAgentManager *manager_proxy;
53         NMDBusSecretAgent *dbus_secret_agent;
54
55         /* GetSecretsInfo structs of in-flight GetSecrets requests */
56         GSList *pending_gets;
57
58         char *identifier;
59         gboolean auto_register;
60         gboolean suppress_auto;
61 } NMSecretAgentOldPrivate;
62
63 enum {
64         PROP_0,
65         PROP_IDENTIFIER,
66         PROP_AUTO_REGISTER,
67         PROP_REGISTERED,
68         PROP_CAPABILITIES,
69
70         LAST_PROP
71 };
72
73 /*************************************************************/
74
75 static void
76 _internal_unregister (NMSecretAgentOld *self)
77 {
78         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
79
80         if (priv->registered) {
81                 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (priv->dbus_secret_agent));
82                 priv->registered = FALSE;
83                 priv->registering = FALSE;
84                 g_object_notify (G_OBJECT (self), NM_SECRET_AGENT_OLD_REGISTERED);
85         }
86 }
87
88 typedef struct {
89         char *path;
90         char *setting_name;
91         GDBusMethodInvocation *context;
92 } GetSecretsInfo;
93
94 static void
95 get_secrets_info_finalize (NMSecretAgentOld *self, GetSecretsInfo *info)
96 {
97         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
98
99         g_return_if_fail (info != NULL);
100
101         priv->pending_gets = g_slist_remove (priv->pending_gets, info);
102
103         g_free (info->path);
104         g_free (info->setting_name);
105         memset (info, 0, sizeof (*info));
106         g_free (info);
107 }
108
109 static inline gboolean
110 should_auto_register (NMSecretAgentOld *self)
111 {
112         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
113
114         return (   priv->auto_register
115                 && !priv->suppress_auto
116                 && !priv->registered
117                 && !priv->registering);
118 }
119
120 static void
121 name_owner_changed (GObject *proxy,
122                     GParamSpec *pspec,
123                     gpointer user_data)
124 {
125         NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (user_data);
126         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
127         GSList *iter;
128         char *owner;
129
130         owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy));
131         if (owner != NULL) {
132                 if (should_auto_register (self))
133                         nm_secret_agent_old_register_async (self, NULL, NULL, NULL);
134                 g_free (owner);
135         } else {
136                 /* Cancel any pending secrets requests */
137                 for (iter = priv->pending_gets; iter; iter = g_slist_next (iter)) {
138                         GetSecretsInfo *info = iter->data;
139
140                         NM_SECRET_AGENT_OLD_GET_CLASS (self)->cancel_get_secrets (self,
141                                                                               info->path,
142                                                                               info->setting_name);
143                 }
144                 g_slist_free (priv->pending_gets);
145                 priv->pending_gets = NULL;
146
147                 _internal_unregister (self);
148         }
149 }
150
151 static gboolean
152 verify_sender (NMSecretAgentOld *self,
153                GDBusMethodInvocation *context,
154                GError **error)
155 {
156         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
157         char *nm_owner;
158         const char *sender;
159         guint32 sender_uid;
160         GVariant *ret;
161         GError *local = NULL;
162
163         g_return_val_if_fail (context != NULL, FALSE);
164
165         /* Private bus connection is always to NetworkManager, which is always
166          * UID 0.
167          */
168         if (priv->private_bus)
169                 return TRUE;
170
171         /* Verify that the sender is the same as NetworkManager's bus name owner. */
172
173         nm_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (priv->manager_proxy));
174         if (!nm_owner) {
175                 g_set_error_literal (error,
176                                      NM_SECRET_AGENT_ERROR,
177                                      NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
178                                      "NetworkManager bus name owner unknown.");
179                 return FALSE;
180         }
181
182         sender = g_dbus_method_invocation_get_sender (context);
183         if (!sender) {
184                 g_set_error_literal (error,
185                                      NM_SECRET_AGENT_ERROR,
186                                      NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
187                                      "Failed to get request sender.");
188                 g_free (nm_owner);
189                 return FALSE;
190         }
191
192         /* Check that the sender matches the current NM bus name owner */
193         if (strcmp (sender, nm_owner) != 0) {
194                 g_set_error_literal (error,
195                                      NM_SECRET_AGENT_ERROR,
196                                      NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
197                                      "Request sender does not match NetworkManager bus name owner.");
198                 g_free (nm_owner);
199                 return FALSE;
200         }
201         g_free (nm_owner);
202
203         /* If we're connected to the session bus, then this must be a test program,
204          * so skip the UID check.
205          */
206         if (priv->session_bus)
207                 return TRUE;
208
209         /* Check the UID of the sender */
210         ret = g_dbus_connection_call_sync (priv->bus,
211                                            DBUS_SERVICE_DBUS,
212                                            DBUS_PATH_DBUS,
213                                            DBUS_INTERFACE_DBUS,
214                                            "GetConnectionUnixUser",
215                                            g_variant_new ("(s)", sender),
216                                            G_VARIANT_TYPE ("(u)"),
217                                            G_DBUS_CALL_FLAGS_NONE, -1,
218                                            NULL, &local);
219         if (!ret) {
220                 char *remote_error = g_dbus_error_get_remote_error (local);
221
222                 g_dbus_error_strip_remote_error (local);
223                 g_set_error (error,
224                              NM_SECRET_AGENT_ERROR,
225                              NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
226                              "Failed to request unix user: (%s) %s.",
227                              remote_error ? remote_error : "",
228                              local->message);
229                 g_free (remote_error);
230                 g_error_free (local);
231                 return FALSE;
232         }
233         g_variant_get (ret, "(u)", &sender_uid);
234         g_variant_unref (ret);
235
236         /* We only accept requests from NM, which always runs as root */
237         if (0 != sender_uid) {
238                 g_set_error_literal (error,
239                                      NM_SECRET_AGENT_ERROR,
240                                      NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
241                                      "Request sender is not root.");
242                 return FALSE;
243         }
244
245         return TRUE;
246 }
247
248 static gboolean
249 verify_request (NMSecretAgentOld *self,
250                 GDBusMethodInvocation *context,
251                 GVariant *connection_dict,
252                 const char *connection_path,
253                 NMConnection **out_connection,
254                 GError **error)
255 {
256         NMConnection *connection = NULL;
257         GError *local = NULL;
258
259         if (!verify_sender (self, context, error))
260                 return FALSE;
261
262         /* No connection?  If the sender verified, then we allow the request */
263         if (connection_dict == NULL)
264                 return TRUE;
265
266         /* If we have a connection dictionary, we require a path too */
267         if (connection_path == NULL) {
268                 g_set_error_literal (error,
269                                      NM_SECRET_AGENT_ERROR,
270                                      NM_SECRET_AGENT_ERROR_INVALID_CONNECTION,
271                                      "Invalid connection: no connection path given.");
272                 return FALSE;
273         }
274
275         /* Make sure the given connection is valid */
276         g_assert (out_connection);
277         connection = _nm_simple_connection_new_from_dbus (connection_dict, NM_SETTING_PARSE_FLAGS_BEST_EFFORT, &local);
278         if (connection) {
279                 nm_connection_set_path (connection, connection_path);
280                 *out_connection = connection;
281         } else {
282                 g_set_error (error,
283                              NM_SECRET_AGENT_ERROR,
284                              NM_SECRET_AGENT_ERROR_INVALID_CONNECTION,
285                              "Invalid connection: %s", local->message);
286                 g_clear_error (&local);
287         }
288
289         return !!connection;
290 }
291
292 static void
293 get_secrets_cb (NMSecretAgentOld *self,
294                 NMConnection *connection,
295                 GVariant *secrets,
296                 GError *error,
297                 gpointer user_data)
298 {
299         GetSecretsInfo *info = user_data;
300
301         if (error)
302                 g_dbus_method_invocation_return_gerror (info->context, error);
303         else {
304                 g_variant_take_ref (secrets);
305                 g_dbus_method_invocation_return_value (info->context,
306                                                        g_variant_new ("(@a{sa{sv}})", secrets));
307         }
308
309         /* Remove the request from internal tracking */
310         get_secrets_info_finalize (self, info);
311 }
312
313 static void
314 impl_secret_agent_old_get_secrets (NMSecretAgentOld *self,
315                                    GDBusMethodInvocation *context,
316                                    GVariant *connection_dict,
317                                    const char *connection_path,
318                                    const char *setting_name,
319                                    const char * const *hints,
320                                    guint flags,
321                                    gpointer user_data)
322 {
323         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
324         GError *error = NULL;
325         NMConnection *connection = NULL;
326         GetSecretsInfo *info;
327
328         /* Make sure the request comes from NetworkManager and is valid */
329         if (!verify_request (self, context, connection_dict, connection_path, &connection, &error)) {
330                 g_dbus_method_invocation_take_error (context, error);
331                 return;
332         }
333
334         info = g_malloc0 (sizeof (GetSecretsInfo));
335         info->path = g_strdup (connection_path);
336         info->setting_name = g_strdup (setting_name);
337         info->context = context;
338         priv->pending_gets = g_slist_append (priv->pending_gets, info);
339
340         NM_SECRET_AGENT_OLD_GET_CLASS (self)->get_secrets (self,
341                                                        connection,
342                                                        connection_path,
343                                                        setting_name,
344                                                        (const char **) hints,
345                                                        flags,
346                                                        get_secrets_cb,
347                                                        info);
348         g_object_unref (connection);
349 }
350
351 static GetSecretsInfo *
352 find_get_secrets_info (GSList *list, const char *path, const char *setting_name)
353 {
354         GSList *iter;
355
356         for (iter = list; iter; iter = g_slist_next (iter)) {
357                 GetSecretsInfo *candidate = iter->data;
358
359                 if (   g_strcmp0 (path, candidate->path) == 0
360                     && g_strcmp0 (setting_name, candidate->setting_name) == 0)
361                         return candidate;
362         }
363         return NULL;
364 }
365
366 static void
367 impl_secret_agent_old_cancel_get_secrets (NMSecretAgentOld *self,
368                                           GDBusMethodInvocation *context,
369                                           const char *connection_path,
370                                           const char *setting_name,
371                                           gpointer user_data)
372 {
373         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
374         GError *error = NULL;
375         GetSecretsInfo *info;
376
377         /* Make sure the request comes from NetworkManager and is valid */
378         if (!verify_request (self, context, NULL, NULL, NULL, &error)) {
379                 g_dbus_method_invocation_take_error (context, error);
380                 return;
381         }
382
383         info = find_get_secrets_info (priv->pending_gets, connection_path, setting_name);
384         if (!info) {
385                 g_dbus_method_invocation_return_error (context,
386                                                        NM_SECRET_AGENT_ERROR,
387                                                        NM_SECRET_AGENT_ERROR_FAILED,
388                                                        "No secrets request in progress for this connection.");
389                 return;
390         }
391
392         /* Send the cancel request up to the subclass and finalize it */
393         NM_SECRET_AGENT_OLD_GET_CLASS (self)->cancel_get_secrets (self,
394                                                               info->path,
395                                                               info->setting_name);
396         g_dbus_method_invocation_return_value (context, NULL);
397 }
398
399 static void
400 save_secrets_cb (NMSecretAgentOld *self,
401                  NMConnection *connection,
402                  GError *error,
403                  gpointer user_data)
404 {
405         GDBusMethodInvocation *context = user_data;
406
407         if (error)
408                 g_dbus_method_invocation_return_gerror (context, error);
409         else
410                 g_dbus_method_invocation_return_value (context, NULL);
411 }
412
413 static void
414 impl_secret_agent_old_save_secrets (NMSecretAgentOld *self,
415                                     GDBusMethodInvocation *context,
416                                     GVariant *connection_dict,
417                                     const char *connection_path,
418                                     gpointer user_data)
419 {
420         GError *error = NULL;
421         NMConnection *connection = NULL;
422
423         /* Make sure the request comes from NetworkManager and is valid */
424         if (!verify_request (self, context, connection_dict, connection_path, &connection, &error)) {
425                 g_dbus_method_invocation_take_error (context, error);
426                 return;
427         }
428
429         NM_SECRET_AGENT_OLD_GET_CLASS (self)->save_secrets (self,
430                                                         connection,
431                                                         connection_path,
432                                                         save_secrets_cb,
433                                                         context);
434         g_object_unref (connection);
435 }
436
437 static void
438 delete_secrets_cb (NMSecretAgentOld *self,
439                    NMConnection *connection,
440                    GError *error,
441                    gpointer user_data)
442 {
443         GDBusMethodInvocation *context = user_data;
444
445         if (error)
446                 g_dbus_method_invocation_return_gerror (context, error);
447         else
448                 g_dbus_method_invocation_return_value (context, NULL);
449 }
450
451 static void
452 impl_secret_agent_old_delete_secrets (NMSecretAgentOld *self,
453                                       GDBusMethodInvocation *context,
454                                       GVariant *connection_dict,
455                                       const char *connection_path,
456                                       gpointer user_data)
457 {
458         GError *error = NULL;
459         NMConnection *connection = NULL;
460
461         /* Make sure the request comes from NetworkManager and is valid */
462         if (!verify_request (self, context, connection_dict, connection_path, &connection, &error)) {
463                 g_dbus_method_invocation_take_error (context, error);
464                 return;
465         }
466
467         NM_SECRET_AGENT_OLD_GET_CLASS (self)->delete_secrets (self,
468                                                           connection,
469                                                           connection_path,
470                                                           delete_secrets_cb,
471                                                           context);
472         g_object_unref (connection);
473 }
474
475 /**************************************************************/
476
477 static gboolean
478 check_nm_running (NMSecretAgentOld *self, GError **error)
479 {
480         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
481         char *owner;
482
483         if (priv->private_bus)
484                 return TRUE;
485         owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (priv->manager_proxy));
486         if (owner) {
487                 g_free (owner);
488                 return TRUE;
489         }
490
491         g_set_error (error, NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED,
492                      "NetworkManager is not running");
493         return FALSE;
494 }
495
496 /**************************************************************/
497
498 /**
499  * nm_secret_agent_old_register:
500  * @self: a #NMSecretAgentOld
501  * @cancellable: a #GCancellable, or %NULL
502  * @error: return location for a #GError, or %NULL
503  *
504  * Registers the #NMSecretAgentOld with the NetworkManager secret manager,
505  * indicating to NetworkManager that the agent is able to provide and save
506  * secrets for connections on behalf of its user.
507  *
508  * It is a programmer error to attempt to register an agent that is already
509  * registered, or in the process of registering.
510  *
511  * Returns: %TRUE if registration was successful, %FALSE on error.
512  **/
513 gboolean
514 nm_secret_agent_old_register (NMSecretAgentOld *self,
515                               GCancellable *cancellable,
516                               GError **error)
517 {
518         NMSecretAgentOldPrivate *priv;
519         NMSecretAgentOldClass *class;
520
521         g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
522
523         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
524
525         g_return_val_if_fail (priv->registered == FALSE, FALSE);
526         g_return_val_if_fail (priv->registering == FALSE, FALSE);
527         g_return_val_if_fail (priv->bus != NULL, FALSE);
528         g_return_val_if_fail (priv->manager_proxy != NULL, FALSE);
529
530         /* Also make sure the subclass can actually respond to secrets requests */
531         class = NM_SECRET_AGENT_OLD_GET_CLASS (self);
532         g_return_val_if_fail (class->get_secrets != NULL, FALSE);
533         g_return_val_if_fail (class->save_secrets != NULL, FALSE);
534         g_return_val_if_fail (class->delete_secrets != NULL, FALSE);
535
536         if (!check_nm_running (self, error))
537                 return FALSE;
538
539         priv->suppress_auto = FALSE;
540
541         /* Export our secret agent interface before registering with the manager */
542         if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->dbus_secret_agent),
543                                                priv->bus,
544                                                NM_DBUS_PATH_SECRET_AGENT,
545                                                error))
546                 return FALSE;
547
548         priv->registering = TRUE;
549         if (nmdbus_agent_manager_call_register_with_capabilities_sync (priv->manager_proxy,
550                                                                        priv->identifier,
551                                                                        priv->capabilities,
552                                                                        cancellable, NULL))
553                 goto success;
554
555         /* Might be an old NetworkManager that doesn't support capabilities;
556          * fall back to old Register() method instead.
557          */
558         if (nmdbus_agent_manager_call_register_sync (priv->manager_proxy,
559                                                      priv->identifier,
560                                                      cancellable, error))
561                 goto success;
562
563         /* Failure */
564         priv->registering = FALSE;
565         _internal_unregister (self);
566         return FALSE;
567
568 success:
569         priv->registering = FALSE;
570         priv->registered = TRUE;
571         g_object_notify (G_OBJECT (self), NM_SECRET_AGENT_OLD_REGISTERED);
572         return TRUE;
573 }
574
575 static void
576 reg_result (NMSecretAgentOld *self, GSimpleAsyncResult *simple, GError *error)
577 {
578         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
579
580         priv->registering = FALSE;
581
582         if (error) {
583                 g_simple_async_result_take_error (simple, error);
584                 g_simple_async_result_complete (simple);
585
586                 /* If registration failed we shouldn't expose ourselves on the bus */
587                 _internal_unregister (self);
588         } else {
589                 priv->registered = TRUE;
590                 g_object_notify (G_OBJECT (self), NM_SECRET_AGENT_OLD_REGISTERED);
591
592                 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
593                 g_simple_async_result_complete (simple);
594         }
595
596         g_object_unref (simple);
597 }
598
599 static void
600 reg_request_cb (GObject *proxy,
601                 GAsyncResult *result,
602                 gpointer user_data)
603 {
604         GSimpleAsyncResult *simple = user_data;
605         NMSecretAgentOld *self;
606         NMSecretAgentOldPrivate *priv;
607         GError *error = NULL;
608
609         self = NM_SECRET_AGENT_OLD (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
610         g_object_unref (self); /* drop extra ref added by get_source_object() */
611         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
612
613         if (!nmdbus_agent_manager_call_register_finish (NMDBUS_AGENT_MANAGER (proxy), result, &error))
614                 g_dbus_error_strip_remote_error (error);
615         reg_result (self, simple, error);
616         g_clear_error (&error);
617 }
618
619 static void
620 reg_with_caps_cb (GObject *proxy,
621                   GAsyncResult *result,
622                   gpointer user_data)
623 {
624         GSimpleAsyncResult *simple = user_data;
625         NMSecretAgentOld *self;
626         NMSecretAgentOldPrivate *priv;
627
628         self = NM_SECRET_AGENT_OLD (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
629         g_object_unref (self); /* drop extra ref added by get_source_object() */
630         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
631
632         if (nmdbus_agent_manager_call_register_with_capabilities_finish (NMDBUS_AGENT_MANAGER (proxy), result, NULL)) {
633                 reg_result (self, simple, NULL);
634                 return;
635         }
636
637         /* Might be an old NetworkManager that doesn't support capabilities;
638          * fall back to old Register() method instead.
639          */
640         nmdbus_agent_manager_call_register (priv->manager_proxy,
641                                             priv->identifier,
642                                             NULL, reg_request_cb, simple);
643 }
644
645 /**
646  * nm_secret_agent_old_register_async:
647  * @self: a #NMSecretAgentOld
648  * @cancellable: a #GCancellable, or %NULL
649  * @callback: callback to call when the agent is registered
650  * @user_data: data for @callback
651  *
652  * Asynchronously registers the #NMSecretAgentOld with the NetworkManager secret
653  * manager, indicating to NetworkManager that the agent is able to provide and
654  * save secrets for connections on behalf of its user.
655  *
656  * It is a programmer error to attempt to register an agent that is already
657  * registered, or in the process of registering.
658  **/
659 void
660 nm_secret_agent_old_register_async (NMSecretAgentOld *self,
661                                     GCancellable *cancellable,
662                                     GAsyncReadyCallback callback,
663                                     gpointer user_data)
664 {
665         NMSecretAgentOldPrivate *priv;
666         NMSecretAgentOldClass *class;
667         GSimpleAsyncResult *simple;
668         GError *error = NULL;
669
670         g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
671
672         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
673
674         g_return_if_fail (priv->registered == FALSE);
675         g_return_if_fail (priv->registering == FALSE);
676         g_return_if_fail (priv->bus != NULL);
677         g_return_if_fail (priv->manager_proxy != NULL);
678
679         /* Also make sure the subclass can actually respond to secrets requests */
680         class = NM_SECRET_AGENT_OLD_GET_CLASS (self);
681         g_return_if_fail (class->get_secrets != NULL);
682         g_return_if_fail (class->save_secrets != NULL);
683         g_return_if_fail (class->delete_secrets != NULL);
684
685         simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
686                                             nm_secret_agent_old_register_async);
687
688         if (!check_nm_running (self, &error)) {
689                 g_simple_async_result_take_error (simple, error);
690                 g_simple_async_result_complete_in_idle (simple);
691                 g_object_unref (simple);
692                 return;
693         }
694
695         /* Export our secret agent interface before registering with the manager */
696         if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->dbus_secret_agent),
697                                                priv->bus,
698                                                NM_DBUS_PATH_SECRET_AGENT,
699                                                &error)) {
700                 g_simple_async_result_take_error (simple, error);
701                 g_simple_async_result_complete_in_idle (simple);
702                 g_object_unref (simple);
703                 return;
704         }
705
706         priv->suppress_auto = FALSE;
707         priv->registering = TRUE;
708
709         nmdbus_agent_manager_call_register_with_capabilities (priv->manager_proxy,
710                                                               priv->identifier,
711                                                               priv->capabilities,
712                                                               NULL,
713                                                               reg_with_caps_cb, simple);
714 }
715
716 /**
717  * nm_secret_agent_old_register_finish:
718  * @self: a #NMSecretAgentOld
719  * @result: the result passed to the #GAsyncReadyCallback
720  * @error: return location for a #GError, or %NULL
721  *
722  * Gets the result of a call to nm_secret_agent_old_register_async().
723  *
724  * Returns: %TRUE if registration was successful, %FALSE on error.
725  **/
726 gboolean
727 nm_secret_agent_old_register_finish (NMSecretAgentOld *self,
728                                      GAsyncResult *result,
729                                      GError **error)
730 {
731         g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), nm_secret_agent_old_register_async), FALSE);
732
733         if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
734                 return FALSE;
735         else
736                 return TRUE;
737 }
738
739 /**
740  * nm_secret_agent_old_unregister:
741  * @self: a #NMSecretAgentOld
742  * @cancellable: a #GCancellable, or %NULL
743  * @error: return location for a #GError, or %NULL
744  *
745  * Unregisters the #NMSecretAgentOld with the NetworkManager secret manager,
746  * indicating to NetworkManager that the agent will no longer provide or
747  * store secrets on behalf of this user.
748  *
749  * Returns: %TRUE if unregistration was successful, %FALSE on error
750  **/
751 gboolean
752 nm_secret_agent_old_unregister (NMSecretAgentOld *self,
753                                 GCancellable *cancellable,
754                                 GError **error)
755 {
756         NMSecretAgentOldPrivate *priv;
757         gboolean success;
758
759         g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
760
761         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
762
763         g_return_val_if_fail (priv->bus != NULL, FALSE);
764         g_return_val_if_fail (priv->manager_proxy != NULL, FALSE);
765
766         priv->suppress_auto = TRUE;
767
768         success = nmdbus_agent_manager_call_unregister_sync (priv->manager_proxy, cancellable, error);
769         if (error && *error)
770                 g_dbus_error_strip_remote_error (*error);
771         _internal_unregister (self);
772
773         return success;
774 }
775
776 static void
777 unregister_cb (GObject *proxy, GAsyncResult *result, gpointer user_data)
778 {
779         GSimpleAsyncResult *simple = user_data;
780         NMSecretAgentOld *self;
781         GError *error = NULL;
782
783         self = NM_SECRET_AGENT_OLD (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
784         g_object_unref (self); /* drop extra ref added by get_source_object() */
785
786         _internal_unregister (self);
787
788         if (nmdbus_agent_manager_call_unregister_finish (NMDBUS_AGENT_MANAGER (proxy),
789                                                          result, &error))
790                 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
791         else {
792                 g_dbus_error_strip_remote_error (error);
793                 g_simple_async_result_take_error (simple, error);
794         }
795
796         g_simple_async_result_complete (simple);
797         g_object_unref (simple);
798 }
799
800 /**
801  * nm_secret_agent_old_unregister_async:
802  * @self: a #NMSecretAgentOld
803  * @cancellable: a #GCancellable, or %NULL
804  * @callback: callback to call when the agent is unregistered
805  * @user_data: data for @callback
806  *
807  * Asynchronously unregisters the #NMSecretAgentOld with the NetworkManager secret
808  * manager, indicating to NetworkManager that the agent will no longer provide
809  * or store secrets on behalf of this user.
810  **/
811 void
812 nm_secret_agent_old_unregister_async (NMSecretAgentOld *self,
813                                       GCancellable *cancellable,
814                                       GAsyncReadyCallback callback,
815                                       gpointer user_data)
816 {
817         NMSecretAgentOldPrivate *priv;
818         GSimpleAsyncResult *simple;
819         GError *error = NULL;
820
821         g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
822
823         priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
824
825         g_return_if_fail (priv->bus != NULL);
826         g_return_if_fail (priv->manager_proxy != NULL);
827
828         simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
829                                             nm_secret_agent_old_unregister_async);
830
831         if (!check_nm_running (self, &error)) {
832                 g_simple_async_result_take_error (simple, error);
833                 g_simple_async_result_complete_in_idle (simple);
834                 g_object_unref (simple);
835                 return;
836         }
837
838         priv->suppress_auto = TRUE;
839
840         nmdbus_agent_manager_call_unregister (priv->manager_proxy, cancellable,
841                                               unregister_cb, simple);
842 }
843
844 /**
845  * nm_secret_agent_old_unregister_finish:
846  * @self: a #NMSecretAgentOld
847  * @result: the result passed to the #GAsyncReadyCallback
848  * @error: return location for a #GError, or %NULL
849  *
850  * Gets the result of a call to nm_secret_agent_old_unregister_async().
851  *
852  * Returns: %TRUE if unregistration was successful, %FALSE on error.
853  **/
854 gboolean
855 nm_secret_agent_old_unregister_finish (NMSecretAgentOld *self,
856                                        GAsyncResult *result,
857                                        GError **error)
858 {
859         g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), nm_secret_agent_old_unregister_async), FALSE);
860
861         if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
862                 return FALSE;
863         else
864                 return TRUE;
865 }
866
867 /**
868  * nm_secret_agent_old_get_registered:
869  * @self: a #NMSecretAgentOld
870  *
871  * Returns: a %TRUE if the agent is registered, %FALSE if it is not.
872  **/
873 gboolean
874 nm_secret_agent_old_get_registered (NMSecretAgentOld *self)
875 {
876         g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
877
878         return NM_SECRET_AGENT_OLD_GET_PRIVATE (self)->registered;
879 }
880
881 /**************************************************************/
882
883 /**
884  * nm_secret_agent_old_get_secrets:
885  * @self: a #NMSecretAgentOld
886  * @connection: the #NMConnection for which we're asked secrets
887  * @setting_name: the name of the secret setting
888  * @hints: (array zero-terminated=1): hints to the agent
889  * @flags: flags that modify the behavior of the request
890  * @callback: (scope async): a callback, to be invoked when the operation is done
891  * @user_data: (closure): caller-specific data to be passed to @callback
892  *
893  * Asynchronously retrieves secrets belonging to @connection for the
894  * setting @setting_name.  @flags indicate specific behavior that the secret
895  * agent should use when performing the request, for example returning only
896  * existing secrets without user interaction, or requesting entirely new
897  * secrets from the user.
898  *
899  * Virtual: get_secrets
900  */
901 void
902 nm_secret_agent_old_get_secrets (NMSecretAgentOld *self,
903                                  NMConnection *connection,
904                                  const char *setting_name,
905                                  const char **hints,
906                                  NMSecretAgentGetSecretsFlags flags,
907                                  NMSecretAgentOldGetSecretsFunc callback,
908                                  gpointer user_data)
909 {
910         g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
911         g_return_if_fail (NM_IS_CONNECTION (connection));
912         g_return_if_fail (nm_connection_get_path (connection));
913         g_return_if_fail (setting_name != NULL);
914         g_return_if_fail (strlen (setting_name) > 0);
915         g_return_if_fail (!(flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_ONLY_SYSTEM));
916         g_return_if_fail (!(flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_NO_ERRORS));
917         g_return_if_fail (callback != NULL);
918
919         NM_SECRET_AGENT_OLD_GET_CLASS (self)->get_secrets (self,
920                                                        connection,
921                                                        nm_connection_get_path (connection),
922                                                        setting_name,
923                                                        hints,
924                                                        flags,
925                                                        callback,
926                                                        user_data);
927 }
928
929 /**
930  * nm_secret_agent_old_save_secrets:
931  * @self: a #NMSecretAgentOld
932  * @connection: a #NMConnection
933  * @callback: (scope async): a callback, to be invoked when the operation is done
934  * @user_data: (closure): caller-specific data to be passed to @callback
935  *
936  * Asynchronously ensures that all secrets inside @connection are stored to
937  * disk.
938  *
939  * Virtual: save_secrets
940  */
941 void
942 nm_secret_agent_old_save_secrets (NMSecretAgentOld *self,
943                                   NMConnection *connection,
944                                   NMSecretAgentOldSaveSecretsFunc callback,
945                                   gpointer user_data)
946 {
947         g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
948         g_return_if_fail (NM_IS_CONNECTION (connection));
949         g_return_if_fail (nm_connection_get_path (connection));
950
951         NM_SECRET_AGENT_OLD_GET_CLASS (self)->save_secrets (self,
952                                                         connection,
953                                                         nm_connection_get_path (connection),
954                                                         callback,
955                                                         user_data);
956 }
957
958 /**
959  * nm_secret_agent_old_delete_secrets:
960  * @self: a #NMSecretAgentOld
961  * @connection: a #NMConnection
962  * @callback: (scope async): a callback, to be invoked when the operation is done
963  * @user_data: (closure): caller-specific data to be passed to @callback
964  *
965  * Asynchronously asks the agent to delete all saved secrets belonging to
966  * @connection.
967  *
968  * Virtual: delete_secrets
969  */
970 void
971 nm_secret_agent_old_delete_secrets (NMSecretAgentOld *self,
972                                     NMConnection *connection,
973                                     NMSecretAgentOldDeleteSecretsFunc callback,
974                                     gpointer user_data)
975 {
976         g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
977         g_return_if_fail (NM_IS_CONNECTION (connection));
978         g_return_if_fail (nm_connection_get_path (connection));
979
980         NM_SECRET_AGENT_OLD_GET_CLASS (self)->delete_secrets (self,
981                                                           connection,
982                                                           nm_connection_get_path (connection),
983                                                           callback,
984                                                           user_data);
985 }
986
987 /**************************************************************/
988
989 static gboolean
990 validate_identifier (const char *identifier)
991 {
992         const char *p = identifier;
993         size_t id_len;
994
995         /* Length between 3 and 255 characters inclusive */
996         id_len = strlen (identifier);
997         if (id_len < 3 || id_len > 255)
998                 return FALSE;
999
1000         if ((identifier[0] == '.') || (identifier[id_len - 1] == '.'))
1001                 return FALSE;
1002
1003         /* FIXME: do complete validation here */
1004         while (p && *p) {
1005                 if (!g_ascii_isalnum (*p) && (*p != '_') && (*p != '-') && (*p != '.'))
1006                         return FALSE;
1007                 if ((*p == '.') && (*(p + 1) == '.'))
1008                         return FALSE;
1009                 p++;
1010         }
1011
1012         return TRUE;
1013 }
1014
1015 static void
1016 nm_secret_agent_old_init (NMSecretAgentOld *self)
1017 {
1018         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1019
1020         priv->dbus_secret_agent = nmdbus_secret_agent_skeleton_new ();
1021         _nm_dbus_bind_properties (self, priv->dbus_secret_agent);
1022         _nm_dbus_bind_methods (self, priv->dbus_secret_agent,
1023                                "GetSecrets", impl_secret_agent_old_get_secrets,
1024                                "CancelGetSecrets", impl_secret_agent_old_cancel_get_secrets,
1025                                "DeleteSecrets", impl_secret_agent_old_delete_secrets,
1026                                "SaveSecrets", impl_secret_agent_old_save_secrets,
1027                                NULL);
1028 }
1029
1030 static void
1031 init_common (NMSecretAgentOld *self)
1032 {
1033         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1034
1035         priv->private_bus = _nm_dbus_is_connection_private (priv->bus);
1036
1037         if (priv->private_bus == FALSE) {
1038                 priv->session_bus = _nm_dbus_bus_type () == G_BUS_TYPE_SESSION;
1039
1040                 g_signal_connect (priv->manager_proxy, "notify::g-name-owner",
1041                                   G_CALLBACK (name_owner_changed), self);
1042         }
1043 }
1044
1045 static gboolean
1046 init_sync (GInitable *initable, GCancellable *cancellable, GError **error)
1047 {
1048         NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (initable);
1049         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1050         GDBusProxy *proxy;
1051
1052         priv->bus = _nm_dbus_new_connection (cancellable, error);
1053         if (!priv->bus)
1054                 return FALSE;
1055
1056         proxy = _nm_dbus_new_proxy_for_connection (priv->bus,
1057                                                    NM_DBUS_PATH_AGENT_MANAGER,
1058                                                    NM_DBUS_INTERFACE_AGENT_MANAGER,
1059                                                    cancellable, error);
1060         if (!proxy)
1061                 return FALSE;
1062         priv->manager_proxy = NMDBUS_AGENT_MANAGER (proxy);
1063
1064         init_common (self);
1065
1066         if (priv->auto_register)
1067                 return nm_secret_agent_old_register (self, cancellable, error);
1068         else
1069                 return TRUE;
1070 }
1071
1072 typedef struct {
1073         NMSecretAgentOld *self;
1074         GCancellable *cancellable;
1075         GSimpleAsyncResult *simple;
1076 } NMSecretAgentOldInitData;
1077
1078 static void
1079 init_async_complete (NMSecretAgentOldInitData *init_data, GError *error)
1080 {
1081         if (!error)
1082                 g_simple_async_result_set_op_res_gboolean (init_data->simple, TRUE);
1083         else
1084                 g_simple_async_result_take_error (init_data->simple, error);
1085
1086         g_simple_async_result_complete_in_idle (init_data->simple);
1087
1088         g_object_unref (init_data->simple);
1089         g_clear_object (&init_data->cancellable);
1090         g_slice_free (NMSecretAgentOldInitData, init_data);
1091 }
1092
1093 static void
1094 init_async_registered (GObject *object, GAsyncResult *result, gpointer user_data)
1095 {
1096         NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (object);
1097         NMSecretAgentOldInitData *init_data = user_data;
1098         GError *error = NULL;
1099
1100         nm_secret_agent_old_register_finish (self, result, &error);
1101         init_async_complete (init_data, error);
1102 }
1103
1104 static void
1105 init_async_got_proxy (GObject *object, GAsyncResult *result, gpointer user_data)
1106 {
1107         NMSecretAgentOldInitData *init_data = user_data;
1108         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (init_data->self);
1109         GDBusProxy *proxy;
1110         GError *error = NULL;
1111
1112         proxy = _nm_dbus_new_proxy_for_connection_finish (result, &error);
1113         if (!proxy) {
1114                 init_async_complete (init_data, error);
1115                 return;
1116         }
1117         priv->manager_proxy = NMDBUS_AGENT_MANAGER (proxy);
1118
1119         init_common (init_data->self);
1120
1121         if (priv->auto_register) {
1122                 nm_secret_agent_old_register_async (init_data->self, init_data->cancellable,
1123                                                 init_async_registered, init_data);
1124         } else
1125                 init_async_complete (init_data, NULL);
1126 }
1127
1128 static void
1129 init_async_got_bus (GObject *initable, GAsyncResult *result, gpointer user_data)
1130 {
1131         NMSecretAgentOldInitData *init_data = user_data;
1132         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (init_data->self);
1133         GError *error = NULL;
1134
1135         priv->bus = _nm_dbus_new_connection_finish (result, &error);
1136         if (!priv->bus) {
1137                 init_async_complete (init_data, error);
1138                 return;
1139         }
1140
1141         _nm_dbus_new_proxy_for_connection_async (priv->bus,
1142                                                  NM_DBUS_PATH_AGENT_MANAGER,
1143                                                  NM_DBUS_INTERFACE_AGENT_MANAGER,
1144                                                  init_data->cancellable,
1145                                                  init_async_got_proxy, init_data);
1146 }
1147
1148 static void
1149 init_async (GAsyncInitable *initable, int io_priority,
1150             GCancellable *cancellable, GAsyncReadyCallback callback,
1151             gpointer user_data)
1152 {
1153         NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (initable);
1154         NMSecretAgentOldInitData *init_data;
1155
1156         init_data = g_slice_new (NMSecretAgentOldInitData);
1157         init_data->self = self;
1158         init_data->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1159
1160         init_data->simple = g_simple_async_result_new (G_OBJECT (initable), callback,
1161                                                        user_data, init_async);
1162
1163         _nm_dbus_new_connection_async (cancellable, init_async_got_bus, init_data);
1164 }
1165
1166 static gboolean
1167 init_finish (GAsyncInitable *initable, GAsyncResult *result, GError **error)
1168 {
1169         GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1170
1171         if (g_simple_async_result_propagate_error (simple, error))
1172                 return FALSE;
1173         else
1174                 return TRUE;
1175 }
1176
1177 static void
1178 get_property (GObject *object,
1179               guint prop_id,
1180               GValue *value,
1181               GParamSpec *pspec)
1182 {
1183         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (object);
1184
1185         switch (prop_id) {
1186         case PROP_IDENTIFIER:
1187                 g_value_set_string (value, priv->identifier);
1188                 break;
1189         case PROP_AUTO_REGISTER:
1190                 g_value_set_boolean (value, priv->auto_register);
1191                 break;
1192         case PROP_REGISTERED:
1193                 g_value_set_boolean (value, priv->registered);
1194                 break;
1195         case PROP_CAPABILITIES:
1196                 g_value_set_flags (value, priv->capabilities);
1197                 break;
1198         default:
1199                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1200                 break;
1201         }
1202 }
1203
1204 static void
1205 set_property (GObject *object,
1206               guint prop_id,
1207               const GValue *value,
1208               GParamSpec *pspec)
1209 {
1210         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (object);
1211         const char *identifier;
1212
1213         switch (prop_id) {
1214         case PROP_IDENTIFIER:
1215                 identifier = g_value_get_string (value);
1216
1217                 g_return_if_fail (validate_identifier (identifier));
1218
1219                 g_free (priv->identifier);
1220                 priv->identifier = g_strdup (identifier);
1221                 break;
1222         case PROP_AUTO_REGISTER:
1223                 priv->auto_register = g_value_get_boolean (value);
1224                 break;
1225         case PROP_CAPABILITIES:
1226                 priv->capabilities = g_value_get_flags (value);
1227                 break;
1228         default:
1229                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1230                 break;
1231         }
1232 }
1233
1234 static void
1235 dispose (GObject *object)
1236 {
1237         NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (object);
1238         NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1239
1240         if (priv->registered)
1241                 nm_secret_agent_old_unregister_async (self, NULL, NULL, NULL);
1242
1243         g_clear_pointer (&priv->identifier, g_free);
1244
1245         while (priv->pending_gets)
1246                 get_secrets_info_finalize (self, priv->pending_gets->data);
1247
1248         g_signal_handlers_disconnect_matched (priv->dbus_secret_agent, G_SIGNAL_MATCH_DATA,
1249                                               0, 0, NULL, NULL, self);
1250         g_object_unref (priv->dbus_secret_agent);
1251
1252         g_clear_object (&priv->manager_proxy);
1253         g_clear_object (&priv->bus);
1254
1255         G_OBJECT_CLASS (nm_secret_agent_old_parent_class)->dispose (object);
1256 }
1257
1258 static void
1259 nm_secret_agent_old_class_init (NMSecretAgentOldClass *class)
1260 {
1261         GObjectClass *object_class = G_OBJECT_CLASS (class);
1262
1263         g_type_class_add_private (class, sizeof (NMSecretAgentOldPrivate));
1264
1265         /* Virtual methods */
1266         object_class->dispose = dispose;
1267         object_class->get_property = get_property;
1268         object_class->set_property = set_property;
1269
1270         /**
1271          * NMSecretAgentOld:identifier:
1272          *
1273          * Identifies this agent; only one agent in each user session may use the
1274          * same identifier.  Identifier formatting follows the same rules as
1275          * D-Bus bus names with the exception that the ':' character is not
1276          * allowed.  The valid set of characters is "[A-Z][a-z][0-9]_-." and the
1277          * identifier is limited in length to 255 characters with a minimum
1278          * of 3 characters.  An example valid identifier is 'org.gnome.nm-applet'
1279          * (without quotes).
1280          **/
1281         g_object_class_install_property
1282                 (object_class, PROP_IDENTIFIER,
1283                  g_param_spec_string (NM_SECRET_AGENT_OLD_IDENTIFIER, "", "",
1284                                       NULL,
1285                                       G_PARAM_READWRITE |
1286                                       G_PARAM_CONSTRUCT_ONLY |
1287                                       G_PARAM_STATIC_STRINGS));
1288
1289         /**
1290          * NMSecretAgentOld:auto-register:
1291          *
1292          * If %TRUE (the default), the agent will always be registered when
1293          * NetworkManager is running; if NetworkManager exits and restarts, the
1294          * agent will re-register itself automatically.
1295          *
1296          * In particular, if this property is %TRUE at construct time, then the
1297          * agent will register itself with NetworkManager during
1298          * construction/initialization, and initialization will fail with an error
1299          * if the agent is unable to register itself.
1300          *
1301          * If the property is %FALSE, the agent will not automatically register with
1302          * NetworkManager, and nm_secret_agent_old_register() or
1303          * nm_secret_agent_old_register_async() must be called to register it.
1304          *
1305          * Calling nm_secret_agent_old_unregister() will suppress auto-registration
1306          * until nm_secret_agent_old_register() is called, which re-enables
1307          * auto-registration. This ensures that the agent remains un-registered when
1308          * you expect it to be unregistered.
1309          **/
1310         g_object_class_install_property
1311                 (object_class, PROP_AUTO_REGISTER,
1312                  g_param_spec_boolean (NM_SECRET_AGENT_OLD_AUTO_REGISTER, "", "",
1313                                        TRUE,
1314                                        G_PARAM_READWRITE |
1315                                        G_PARAM_CONSTRUCT |
1316                                        G_PARAM_STATIC_STRINGS));
1317
1318         /**
1319          * NMSecretAgentOld:registered:
1320          *
1321          * %TRUE if the agent is registered with NetworkManager, %FALSE if not.
1322          **/
1323         g_object_class_install_property
1324                 (object_class, PROP_REGISTERED,
1325                  g_param_spec_boolean (NM_SECRET_AGENT_OLD_REGISTERED, "", "",
1326                                        FALSE,
1327                                        G_PARAM_READABLE |
1328                                        G_PARAM_STATIC_STRINGS));
1329
1330         /**
1331          * NMSecretAgentOld:capabilities:
1332          *
1333          * A bitfield of %NMSecretAgentCapabilities.
1334          **/
1335         g_object_class_install_property
1336                 (object_class, PROP_CAPABILITIES,
1337                  g_param_spec_flags (NM_SECRET_AGENT_OLD_CAPABILITIES, "", "",
1338                                      NM_TYPE_SECRET_AGENT_CAPABILITIES,
1339                                      NM_SECRET_AGENT_CAPABILITY_NONE,
1340                                      G_PARAM_READWRITE |
1341                                      G_PARAM_CONSTRUCT |
1342                                      G_PARAM_STATIC_STRINGS));
1343
1344         _nm_dbus_register_proxy_type (NM_DBUS_INTERFACE_AGENT_MANAGER,
1345                                       NMDBUS_TYPE_AGENT_MANAGER_PROXY);
1346 }
1347
1348 static void
1349 nm_secret_agent_old_initable_iface_init (GInitableIface *iface)
1350 {
1351         iface->init = init_sync;
1352 }
1353
1354 static void
1355 nm_secret_agent_old_async_initable_iface_init (GAsyncInitableIface *iface)
1356 {
1357         iface->init_async = init_async;
1358         iface->init_finish = init_finish;
1359 }