1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusauthobserver.h"
26 #include "gio-marshal.h"
27 #include "gcredentials.h"
28 #include "gioenumtypes.h"
29 #include "giostream.h"
34 * SECTION:gdbusauthobserver
35 * @short_description: Object used for authenticating connections
38 * The #GDBusAuthObserver type provides a mechanism for participating
39 * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
40 * peers. Simply instantiate a #GDBusAuthObserver and connect to the
41 * signals you are interested in. Note that new signals may be added
44 * For example, if you only want to allow D-Bus connections from
45 * processes owned by the same uid as the server, you would do this:
46 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
48 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
50 * GCredentials *credentials,
54 * gboolean authorized;
57 * me = g_credentials_new ();
59 * if (credentials != NULL &&
60 * !g_credentials_is_same_user (credentials, me))
63 * g_object_unref (me);
69 * on_new_connection (GDBusServer *server,
70 * GDBusConnection *connection,
73 * /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
78 * GDBusAuthObserver *observer;
79 * GDBusServer *server;
83 * observer = g_dbus_auth_observer_new ();
84 * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
85 * G_DBUS_SERVER_FLAGS_NONE,
87 * NULL, /<!-- -->* GCancellable *<!-- -->/
89 * g_signal_connect (observer,
90 * "authorize-authenticated-peer",
91 * G_CALLBACK (on_authorize_authenticated_peer),
93 * g_signal_connect (server,
95 * G_CALLBACK (on_new_connection),
97 * g_object_unref (observer);
98 * g_dbus_server_start (server);
99 * </programlisting></example>
102 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
105 * GDBusAuthObserverClass:
106 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
108 * Class structure for #GDBusAuthObserverClass.
112 struct _GDBusAuthObserverClass
115 GObjectClass parent_class;
120 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
122 GCredentials *credentials);
128 * The #GDBusAuthObserver structure contains only private data and
129 * should only be accessed using the provided API.
133 struct _GDBusAuthObserver
135 GObject parent_instance;
140 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
144 static guint signals[LAST_SIGNAL] = { 0 };
146 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
148 /* ---------------------------------------------------------------------------------------------------- */
151 g_dbus_auth_observer_finalize (GObject *object)
153 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
157 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
159 GCredentials *credentials)
165 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
167 const GValue *handler_return,
170 gboolean continue_emission;
171 gboolean signal_handled;
173 signal_handled = g_value_get_boolean (handler_return);
174 g_value_set_boolean (return_accu, signal_handled);
175 continue_emission = signal_handled;
177 return continue_emission;
181 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 gobject_class->finalize = g_dbus_auth_observer_finalize;
187 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
190 * GDBusAuthObserver::authorize-authenticated-peer:
191 * @observer: The #GDBusAuthObserver emitting the signal.
192 * @stream: A #GIOStream for the #GDBusConnection.
193 * @credentials: Credentials received from the peer or %NULL.
195 * Emitted to check if a peer that is successfully authenticated
198 * Returns: %TRUE if the peer is authorized, %FALSE if not.
202 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
203 g_signal_new ("authorize-authenticated-peer",
204 G_TYPE_DBUS_AUTH_OBSERVER,
206 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
207 _g_signal_accumulator_false_handled,
208 NULL, /* accu_data */
209 _gio_marshal_BOOLEAN__OBJECT_OBJECT,
217 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
222 * g_dbus_auth_observer_new:
224 * Creates a new #GDBusAuthObserver object.
226 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
231 g_dbus_auth_observer_new (void)
233 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
236 /* ---------------------------------------------------------------------------------------------------- */
239 * g_dbus_auth_observer_authorize_authenticated_peer:
240 * @observer: A #GDBusAuthObserver.
241 * @stream: A #GIOStream for the #GDBusConnection.
242 * @credentials: Credentials received from the peer or %NULL.
244 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
246 * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
251 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
253 GCredentials *credentials)
258 g_signal_emit (observer,
259 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],