1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
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.
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.
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.
18 * Copyright 2010 - 2011 Red Hat, Inc.
21 #include "nm-default.h"
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"
32 #include "nmdbus-secret-agent.h"
33 #include "nmdbus-agent-manager.h"
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);
42 #define NM_SECRET_AGENT_OLD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SECRET_AGENT_OLD, NMSecretAgentOldPrivate))
47 NMSecretAgentCapabilities capabilities;
52 NMDBusAgentManager *manager_proxy;
53 NMDBusSecretAgent *dbus_secret_agent;
55 /* GetSecretsInfo structs of in-flight GetSecrets requests */
59 gboolean auto_register;
60 gboolean suppress_auto;
61 } NMSecretAgentOldPrivate;
73 /*************************************************************/
76 _internal_unregister (NMSecretAgentOld *self)
78 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
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);
91 GDBusMethodInvocation *context;
95 get_secrets_info_finalize (NMSecretAgentOld *self, GetSecretsInfo *info)
97 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
99 g_return_if_fail (info != NULL);
101 priv->pending_gets = g_slist_remove (priv->pending_gets, info);
104 g_free (info->setting_name);
105 memset (info, 0, sizeof (*info));
109 static inline gboolean
110 should_auto_register (NMSecretAgentOld *self)
112 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
114 return ( priv->auto_register
115 && !priv->suppress_auto
117 && !priv->registering);
121 name_owner_changed (GObject *proxy,
125 NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (user_data);
126 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
130 owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy));
132 if (should_auto_register (self))
133 nm_secret_agent_old_register_async (self, NULL, NULL, NULL);
136 /* Cancel any pending secrets requests */
137 for (iter = priv->pending_gets; iter; iter = g_slist_next (iter)) {
138 GetSecretsInfo *info = iter->data;
140 NM_SECRET_AGENT_OLD_GET_CLASS (self)->cancel_get_secrets (self,
144 g_slist_free (priv->pending_gets);
145 priv->pending_gets = NULL;
147 _internal_unregister (self);
152 verify_sender (NMSecretAgentOld *self,
153 GDBusMethodInvocation *context,
156 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
161 GError *local = NULL;
163 g_return_val_if_fail (context != NULL, FALSE);
165 /* Private bus connection is always to NetworkManager, which is always
168 if (priv->private_bus)
171 /* Verify that the sender is the same as NetworkManager's bus name owner. */
173 nm_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (priv->manager_proxy));
175 g_set_error_literal (error,
176 NM_SECRET_AGENT_ERROR,
177 NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
178 "NetworkManager bus name owner unknown.");
182 sender = g_dbus_method_invocation_get_sender (context);
184 g_set_error_literal (error,
185 NM_SECRET_AGENT_ERROR,
186 NM_SECRET_AGENT_ERROR_PERMISSION_DENIED,
187 "Failed to get request sender.");
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.");
203 /* If we're connected to the session bus, then this must be a test program,
204 * so skip the UID check.
206 if (priv->session_bus)
209 /* Check the UID of the sender */
210 ret = g_dbus_connection_call_sync (priv->bus,
214 "GetConnectionUnixUser",
215 g_variant_new ("(s)", sender),
216 G_VARIANT_TYPE ("(u)"),
217 G_DBUS_CALL_FLAGS_NONE, -1,
220 char *remote_error = g_dbus_error_get_remote_error (local);
222 g_dbus_error_strip_remote_error (local);
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 : "",
229 g_free (remote_error);
230 g_error_free (local);
233 g_variant_get (ret, "(u)", &sender_uid);
234 g_variant_unref (ret);
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.");
249 verify_request (NMSecretAgentOld *self,
250 GDBusMethodInvocation *context,
251 GVariant *connection_dict,
252 const char *connection_path,
253 NMConnection **out_connection,
256 NMConnection *connection = NULL;
257 GError *local = NULL;
259 if (!verify_sender (self, context, error))
262 /* No connection? If the sender verified, then we allow the request */
263 if (connection_dict == NULL)
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.");
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);
279 nm_connection_set_path (connection, connection_path);
280 *out_connection = connection;
283 NM_SECRET_AGENT_ERROR,
284 NM_SECRET_AGENT_ERROR_INVALID_CONNECTION,
285 "Invalid connection: %s", local->message);
286 g_clear_error (&local);
293 get_secrets_cb (NMSecretAgentOld *self,
294 NMConnection *connection,
299 GetSecretsInfo *info = user_data;
302 g_dbus_method_invocation_return_gerror (info->context, error);
304 g_variant_take_ref (secrets);
305 g_dbus_method_invocation_return_value (info->context,
306 g_variant_new ("(@a{sa{sv}})", secrets));
309 /* Remove the request from internal tracking */
310 get_secrets_info_finalize (self, info);
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,
323 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
324 GError *error = NULL;
325 NMConnection *connection = NULL;
326 GetSecretsInfo *info;
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);
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);
340 NM_SECRET_AGENT_OLD_GET_CLASS (self)->get_secrets (self,
344 (const char **) hints,
348 g_object_unref (connection);
351 static GetSecretsInfo *
352 find_get_secrets_info (GSList *list, const char *path, const char *setting_name)
356 for (iter = list; iter; iter = g_slist_next (iter)) {
357 GetSecretsInfo *candidate = iter->data;
359 if ( g_strcmp0 (path, candidate->path) == 0
360 && g_strcmp0 (setting_name, candidate->setting_name) == 0)
367 impl_secret_agent_old_cancel_get_secrets (NMSecretAgentOld *self,
368 GDBusMethodInvocation *context,
369 const char *connection_path,
370 const char *setting_name,
373 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
374 GError *error = NULL;
375 GetSecretsInfo *info;
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);
383 info = find_get_secrets_info (priv->pending_gets, connection_path, setting_name);
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.");
392 /* Send the cancel request up to the subclass and finalize it */
393 NM_SECRET_AGENT_OLD_GET_CLASS (self)->cancel_get_secrets (self,
396 g_dbus_method_invocation_return_value (context, NULL);
400 save_secrets_cb (NMSecretAgentOld *self,
401 NMConnection *connection,
405 GDBusMethodInvocation *context = user_data;
408 g_dbus_method_invocation_return_gerror (context, error);
410 g_dbus_method_invocation_return_value (context, NULL);
414 impl_secret_agent_old_save_secrets (NMSecretAgentOld *self,
415 GDBusMethodInvocation *context,
416 GVariant *connection_dict,
417 const char *connection_path,
420 GError *error = NULL;
421 NMConnection *connection = NULL;
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);
429 NM_SECRET_AGENT_OLD_GET_CLASS (self)->save_secrets (self,
434 g_object_unref (connection);
438 delete_secrets_cb (NMSecretAgentOld *self,
439 NMConnection *connection,
443 GDBusMethodInvocation *context = user_data;
446 g_dbus_method_invocation_return_gerror (context, error);
448 g_dbus_method_invocation_return_value (context, NULL);
452 impl_secret_agent_old_delete_secrets (NMSecretAgentOld *self,
453 GDBusMethodInvocation *context,
454 GVariant *connection_dict,
455 const char *connection_path,
458 GError *error = NULL;
459 NMConnection *connection = NULL;
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);
467 NM_SECRET_AGENT_OLD_GET_CLASS (self)->delete_secrets (self,
472 g_object_unref (connection);
475 /**************************************************************/
478 check_nm_running (NMSecretAgentOld *self, GError **error)
480 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
483 if (priv->private_bus)
485 owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (priv->manager_proxy));
491 g_set_error (error, NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED,
492 "NetworkManager is not running");
496 /**************************************************************/
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
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.
508 * It is a programmer error to attempt to register an agent that is already
509 * registered, or in the process of registering.
511 * Returns: %TRUE if registration was successful, %FALSE on error.
514 nm_secret_agent_old_register (NMSecretAgentOld *self,
515 GCancellable *cancellable,
518 NMSecretAgentOldPrivate *priv;
519 NMSecretAgentOldClass *class;
521 g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
523 priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
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);
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);
536 if (!check_nm_running (self, error))
539 priv->suppress_auto = FALSE;
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),
544 NM_DBUS_PATH_SECRET_AGENT,
548 priv->registering = TRUE;
549 if (nmdbus_agent_manager_call_register_with_capabilities_sync (priv->manager_proxy,
555 /* Might be an old NetworkManager that doesn't support capabilities;
556 * fall back to old Register() method instead.
558 if (nmdbus_agent_manager_call_register_sync (priv->manager_proxy,
564 priv->registering = FALSE;
565 _internal_unregister (self);
569 priv->registering = FALSE;
570 priv->registered = TRUE;
571 g_object_notify (G_OBJECT (self), NM_SECRET_AGENT_OLD_REGISTERED);
576 reg_result (NMSecretAgentOld *self, GSimpleAsyncResult *simple, GError *error)
578 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
580 priv->registering = FALSE;
583 g_simple_async_result_take_error (simple, error);
584 g_simple_async_result_complete (simple);
586 /* If registration failed we shouldn't expose ourselves on the bus */
587 _internal_unregister (self);
589 priv->registered = TRUE;
590 g_object_notify (G_OBJECT (self), NM_SECRET_AGENT_OLD_REGISTERED);
592 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
593 g_simple_async_result_complete (simple);
596 g_object_unref (simple);
600 reg_request_cb (GObject *proxy,
601 GAsyncResult *result,
604 GSimpleAsyncResult *simple = user_data;
605 NMSecretAgentOld *self;
606 NMSecretAgentOldPrivate *priv;
607 GError *error = NULL;
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);
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);
620 reg_with_caps_cb (GObject *proxy,
621 GAsyncResult *result,
624 GSimpleAsyncResult *simple = user_data;
625 NMSecretAgentOld *self;
626 NMSecretAgentOldPrivate *priv;
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);
632 if (nmdbus_agent_manager_call_register_with_capabilities_finish (NMDBUS_AGENT_MANAGER (proxy), result, NULL)) {
633 reg_result (self, simple, NULL);
637 /* Might be an old NetworkManager that doesn't support capabilities;
638 * fall back to old Register() method instead.
640 nmdbus_agent_manager_call_register (priv->manager_proxy,
642 NULL, reg_request_cb, simple);
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
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.
656 * It is a programmer error to attempt to register an agent that is already
657 * registered, or in the process of registering.
660 nm_secret_agent_old_register_async (NMSecretAgentOld *self,
661 GCancellable *cancellable,
662 GAsyncReadyCallback callback,
665 NMSecretAgentOldPrivate *priv;
666 NMSecretAgentOldClass *class;
667 GSimpleAsyncResult *simple;
668 GError *error = NULL;
670 g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
672 priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
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);
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);
685 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
686 nm_secret_agent_old_register_async);
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);
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),
698 NM_DBUS_PATH_SECRET_AGENT,
700 g_simple_async_result_take_error (simple, error);
701 g_simple_async_result_complete_in_idle (simple);
702 g_object_unref (simple);
706 priv->suppress_auto = FALSE;
707 priv->registering = TRUE;
709 nmdbus_agent_manager_call_register_with_capabilities (priv->manager_proxy,
713 reg_with_caps_cb, simple);
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
722 * Gets the result of a call to nm_secret_agent_old_register_async().
724 * Returns: %TRUE if registration was successful, %FALSE on error.
727 nm_secret_agent_old_register_finish (NMSecretAgentOld *self,
728 GAsyncResult *result,
731 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), nm_secret_agent_old_register_async), FALSE);
733 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
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
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.
749 * Returns: %TRUE if unregistration was successful, %FALSE on error
752 nm_secret_agent_old_unregister (NMSecretAgentOld *self,
753 GCancellable *cancellable,
756 NMSecretAgentOldPrivate *priv;
759 g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
761 priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
763 g_return_val_if_fail (priv->bus != NULL, FALSE);
764 g_return_val_if_fail (priv->manager_proxy != NULL, FALSE);
766 priv->suppress_auto = TRUE;
768 success = nmdbus_agent_manager_call_unregister_sync (priv->manager_proxy, cancellable, error);
770 g_dbus_error_strip_remote_error (*error);
771 _internal_unregister (self);
777 unregister_cb (GObject *proxy, GAsyncResult *result, gpointer user_data)
779 GSimpleAsyncResult *simple = user_data;
780 NMSecretAgentOld *self;
781 GError *error = NULL;
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() */
786 _internal_unregister (self);
788 if (nmdbus_agent_manager_call_unregister_finish (NMDBUS_AGENT_MANAGER (proxy),
790 g_simple_async_result_set_op_res_gboolean (simple, TRUE);
792 g_dbus_error_strip_remote_error (error);
793 g_simple_async_result_take_error (simple, error);
796 g_simple_async_result_complete (simple);
797 g_object_unref (simple);
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
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.
812 nm_secret_agent_old_unregister_async (NMSecretAgentOld *self,
813 GCancellable *cancellable,
814 GAsyncReadyCallback callback,
817 NMSecretAgentOldPrivate *priv;
818 GSimpleAsyncResult *simple;
819 GError *error = NULL;
821 g_return_if_fail (NM_IS_SECRET_AGENT_OLD (self));
823 priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
825 g_return_if_fail (priv->bus != NULL);
826 g_return_if_fail (priv->manager_proxy != NULL);
828 simple = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
829 nm_secret_agent_old_unregister_async);
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);
838 priv->suppress_auto = TRUE;
840 nmdbus_agent_manager_call_unregister (priv->manager_proxy, cancellable,
841 unregister_cb, simple);
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
850 * Gets the result of a call to nm_secret_agent_old_unregister_async().
852 * Returns: %TRUE if unregistration was successful, %FALSE on error.
855 nm_secret_agent_old_unregister_finish (NMSecretAgentOld *self,
856 GAsyncResult *result,
859 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self), nm_secret_agent_old_unregister_async), FALSE);
861 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
868 * nm_secret_agent_old_get_registered:
869 * @self: a #NMSecretAgentOld
871 * Returns: a %TRUE if the agent is registered, %FALSE if it is not.
874 nm_secret_agent_old_get_registered (NMSecretAgentOld *self)
876 g_return_val_if_fail (NM_IS_SECRET_AGENT_OLD (self), FALSE);
878 return NM_SECRET_AGENT_OLD_GET_PRIVATE (self)->registered;
881 /**************************************************************/
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
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.
899 * Virtual: get_secrets
902 nm_secret_agent_old_get_secrets (NMSecretAgentOld *self,
903 NMConnection *connection,
904 const char *setting_name,
906 NMSecretAgentGetSecretsFlags flags,
907 NMSecretAgentOldGetSecretsFunc callback,
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);
919 NM_SECRET_AGENT_OLD_GET_CLASS (self)->get_secrets (self,
921 nm_connection_get_path (connection),
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
936 * Asynchronously ensures that all secrets inside @connection are stored to
939 * Virtual: save_secrets
942 nm_secret_agent_old_save_secrets (NMSecretAgentOld *self,
943 NMConnection *connection,
944 NMSecretAgentOldSaveSecretsFunc callback,
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));
951 NM_SECRET_AGENT_OLD_GET_CLASS (self)->save_secrets (self,
953 nm_connection_get_path (connection),
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
965 * Asynchronously asks the agent to delete all saved secrets belonging to
968 * Virtual: delete_secrets
971 nm_secret_agent_old_delete_secrets (NMSecretAgentOld *self,
972 NMConnection *connection,
973 NMSecretAgentOldDeleteSecretsFunc callback,
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));
980 NM_SECRET_AGENT_OLD_GET_CLASS (self)->delete_secrets (self,
982 nm_connection_get_path (connection),
987 /**************************************************************/
990 validate_identifier (const char *identifier)
992 const char *p = identifier;
995 /* Length between 3 and 255 characters inclusive */
996 id_len = strlen (identifier);
997 if (id_len < 3 || id_len > 255)
1000 if ((identifier[0] == '.') || (identifier[id_len - 1] == '.'))
1003 /* FIXME: do complete validation here */
1005 if (!g_ascii_isalnum (*p) && (*p != '_') && (*p != '-') && (*p != '.'))
1007 if ((*p == '.') && (*(p + 1) == '.'))
1016 nm_secret_agent_old_init (NMSecretAgentOld *self)
1018 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
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,
1031 init_common (NMSecretAgentOld *self)
1033 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1035 priv->private_bus = _nm_dbus_is_connection_private (priv->bus);
1037 if (priv->private_bus == FALSE) {
1038 priv->session_bus = _nm_dbus_bus_type () == G_BUS_TYPE_SESSION;
1040 g_signal_connect (priv->manager_proxy, "notify::g-name-owner",
1041 G_CALLBACK (name_owner_changed), self);
1046 init_sync (GInitable *initable, GCancellable *cancellable, GError **error)
1048 NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (initable);
1049 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1052 priv->bus = _nm_dbus_new_connection (cancellable, error);
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);
1062 priv->manager_proxy = NMDBUS_AGENT_MANAGER (proxy);
1066 if (priv->auto_register)
1067 return nm_secret_agent_old_register (self, cancellable, error);
1073 NMSecretAgentOld *self;
1074 GCancellable *cancellable;
1075 GSimpleAsyncResult *simple;
1076 } NMSecretAgentOldInitData;
1079 init_async_complete (NMSecretAgentOldInitData *init_data, GError *error)
1082 g_simple_async_result_set_op_res_gboolean (init_data->simple, TRUE);
1084 g_simple_async_result_take_error (init_data->simple, error);
1086 g_simple_async_result_complete_in_idle (init_data->simple);
1088 g_object_unref (init_data->simple);
1089 g_clear_object (&init_data->cancellable);
1090 g_slice_free (NMSecretAgentOldInitData, init_data);
1094 init_async_registered (GObject *object, GAsyncResult *result, gpointer user_data)
1096 NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (object);
1097 NMSecretAgentOldInitData *init_data = user_data;
1098 GError *error = NULL;
1100 nm_secret_agent_old_register_finish (self, result, &error);
1101 init_async_complete (init_data, error);
1105 init_async_got_proxy (GObject *object, GAsyncResult *result, gpointer user_data)
1107 NMSecretAgentOldInitData *init_data = user_data;
1108 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (init_data->self);
1110 GError *error = NULL;
1112 proxy = _nm_dbus_new_proxy_for_connection_finish (result, &error);
1114 init_async_complete (init_data, error);
1117 priv->manager_proxy = NMDBUS_AGENT_MANAGER (proxy);
1119 init_common (init_data->self);
1121 if (priv->auto_register) {
1122 nm_secret_agent_old_register_async (init_data->self, init_data->cancellable,
1123 init_async_registered, init_data);
1125 init_async_complete (init_data, NULL);
1129 init_async_got_bus (GObject *initable, GAsyncResult *result, gpointer user_data)
1131 NMSecretAgentOldInitData *init_data = user_data;
1132 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (init_data->self);
1133 GError *error = NULL;
1135 priv->bus = _nm_dbus_new_connection_finish (result, &error);
1137 init_async_complete (init_data, error);
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);
1149 init_async (GAsyncInitable *initable, int io_priority,
1150 GCancellable *cancellable, GAsyncReadyCallback callback,
1153 NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (initable);
1154 NMSecretAgentOldInitData *init_data;
1156 init_data = g_slice_new (NMSecretAgentOldInitData);
1157 init_data->self = self;
1158 init_data->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1160 init_data->simple = g_simple_async_result_new (G_OBJECT (initable), callback,
1161 user_data, init_async);
1163 _nm_dbus_new_connection_async (cancellable, init_async_got_bus, init_data);
1167 init_finish (GAsyncInitable *initable, GAsyncResult *result, GError **error)
1169 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1171 if (g_simple_async_result_propagate_error (simple, error))
1178 get_property (GObject *object,
1183 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (object);
1186 case PROP_IDENTIFIER:
1187 g_value_set_string (value, priv->identifier);
1189 case PROP_AUTO_REGISTER:
1190 g_value_set_boolean (value, priv->auto_register);
1192 case PROP_REGISTERED:
1193 g_value_set_boolean (value, priv->registered);
1195 case PROP_CAPABILITIES:
1196 g_value_set_flags (value, priv->capabilities);
1199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1205 set_property (GObject *object,
1207 const GValue *value,
1210 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (object);
1211 const char *identifier;
1214 case PROP_IDENTIFIER:
1215 identifier = g_value_get_string (value);
1217 g_return_if_fail (validate_identifier (identifier));
1219 g_free (priv->identifier);
1220 priv->identifier = g_strdup (identifier);
1222 case PROP_AUTO_REGISTER:
1223 priv->auto_register = g_value_get_boolean (value);
1225 case PROP_CAPABILITIES:
1226 priv->capabilities = g_value_get_flags (value);
1229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1235 dispose (GObject *object)
1237 NMSecretAgentOld *self = NM_SECRET_AGENT_OLD (object);
1238 NMSecretAgentOldPrivate *priv = NM_SECRET_AGENT_OLD_GET_PRIVATE (self);
1240 if (priv->registered)
1241 nm_secret_agent_old_unregister_async (self, NULL, NULL, NULL);
1243 g_clear_pointer (&priv->identifier, g_free);
1245 while (priv->pending_gets)
1246 get_secrets_info_finalize (self, priv->pending_gets->data);
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);
1252 g_clear_object (&priv->manager_proxy);
1253 g_clear_object (&priv->bus);
1255 G_OBJECT_CLASS (nm_secret_agent_old_parent_class)->dispose (object);
1259 nm_secret_agent_old_class_init (NMSecretAgentOldClass *class)
1261 GObjectClass *object_class = G_OBJECT_CLASS (class);
1263 g_type_class_add_private (class, sizeof (NMSecretAgentOldPrivate));
1265 /* Virtual methods */
1266 object_class->dispose = dispose;
1267 object_class->get_property = get_property;
1268 object_class->set_property = set_property;
1271 * NMSecretAgentOld:identifier:
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'
1281 g_object_class_install_property
1282 (object_class, PROP_IDENTIFIER,
1283 g_param_spec_string (NM_SECRET_AGENT_OLD_IDENTIFIER, "", "",
1286 G_PARAM_CONSTRUCT_ONLY |
1287 G_PARAM_STATIC_STRINGS));
1290 * NMSecretAgentOld:auto-register:
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.
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.
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.
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.
1310 g_object_class_install_property
1311 (object_class, PROP_AUTO_REGISTER,
1312 g_param_spec_boolean (NM_SECRET_AGENT_OLD_AUTO_REGISTER, "", "",
1316 G_PARAM_STATIC_STRINGS));
1319 * NMSecretAgentOld:registered:
1321 * %TRUE if the agent is registered with NetworkManager, %FALSE if not.
1323 g_object_class_install_property
1324 (object_class, PROP_REGISTERED,
1325 g_param_spec_boolean (NM_SECRET_AGENT_OLD_REGISTERED, "", "",
1328 G_PARAM_STATIC_STRINGS));
1331 * NMSecretAgentOld:capabilities:
1333 * A bitfield of %NMSecretAgentCapabilities.
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,
1342 G_PARAM_STATIC_STRINGS));
1344 _nm_dbus_register_proxy_type (NM_DBUS_INTERFACE_AGENT_MANAGER,
1345 NMDBUS_TYPE_AGENT_MANAGER_PROXY);
1349 nm_secret_agent_old_initable_iface_init (GInitableIface *iface)
1351 iface->init = init_sync;
1355 nm_secret_agent_old_async_initable_iface_init (GAsyncInitableIface *iface)
1357 iface->init_async = init_async;
1358 iface->init_finish = init_finish;