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"
35 * SECTION:gdbusauthobserver
36 * @short_description: Object used for authenticating connections
39 * The #GDBusAuthObserver type provides a mechanism for participating
40 * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
41 * peers. Simply instantiate a #GDBusAuthObserver and connect to the
42 * signals you are interested in. Note that new signals may be added
45 * For example, if you only want to allow D-Bus connections from
46 * processes owned by the same uid as the server, you would do this:
47 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
49 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
51 * GCredentials *credentials,
55 * gboolean authorized;
58 * me = g_credentials_new ();
60 * if (credentials != NULL &&
61 * !g_credentials_is_same_user (credentials, me))
64 * g_object_unref (me);
70 * on_new_connection (GDBusServer *server,
71 * GDBusConnection *connection,
74 * /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
79 * GDBusAuthObserver *observer;
80 * GDBusServer *server;
84 * observer = g_dbus_auth_observer_new ();
85 * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
86 * G_DBUS_SERVER_FLAGS_NONE,
88 * NULL, /<!-- -->* GCancellable *<!-- -->/
90 * g_signal_connect (observer,
91 * "authorize-authenticated-peer",
92 * G_CALLBACK (on_authorize_authenticated_peer),
94 * g_signal_connect (server,
96 * G_CALLBACK (on_new_connection),
98 * g_object_unref (observer);
99 * g_dbus_server_start (server);
100 * </programlisting></example>
103 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
106 * GDBusAuthObserverClass:
107 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
109 * Class structure for #GDBusAuthObserverClass.
113 struct _GDBusAuthObserverClass
116 GObjectClass parent_class;
121 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
123 GCredentials *credentials);
129 * The #GDBusAuthObserver structure contains only private data and
130 * should only be accessed using the provided API.
134 struct _GDBusAuthObserver
136 GObject parent_instance;
141 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
145 static guint signals[LAST_SIGNAL] = { 0 };
147 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
149 /* ---------------------------------------------------------------------------------------------------- */
152 g_dbus_auth_observer_finalize (GObject *object)
154 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
158 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
160 GCredentials *credentials)
166 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
168 const GValue *handler_return,
171 gboolean continue_emission;
172 gboolean signal_handled;
174 signal_handled = g_value_get_boolean (handler_return);
175 g_value_set_boolean (return_accu, signal_handled);
176 continue_emission = signal_handled;
178 return continue_emission;
182 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
186 gobject_class->finalize = g_dbus_auth_observer_finalize;
188 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
191 * GDBusAuthObserver::authorize-authenticated-peer:
192 * @observer: The #GDBusAuthObserver emitting the signal.
193 * @stream: A #GIOStream for the #GDBusConnection.
194 * @credentials: Credentials received from the peer or %NULL.
196 * Emitted to check if a peer that is successfully authenticated
199 * Returns: %TRUE if the peer is authorized, %FALSE if not.
203 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
204 g_signal_new ("authorize-authenticated-peer",
205 G_TYPE_DBUS_AUTH_OBSERVER,
207 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
208 _g_signal_accumulator_false_handled,
209 NULL, /* accu_data */
210 _gio_marshal_BOOLEAN__OBJECT_OBJECT,
218 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
223 * g_dbus_auth_observer_new:
225 * Creates a new #GDBusAuthObserver object.
227 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
232 g_dbus_auth_observer_new (void)
234 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
237 /* ---------------------------------------------------------------------------------------------------- */
240 * g_dbus_auth_observer_authorize_authenticated_peer:
241 * @observer: A #GDBusAuthObserver.
242 * @stream: A #GIOStream for the #GDBusConnection.
243 * @credentials: Credentials received from the peer or %NULL.
245 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
247 * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
252 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
254 GCredentials *credentials)
259 g_signal_emit (observer,
260 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
270 #define __G_DBUS_AUTH_OBSERVER_C__
271 #include "gioaliasdef.c"