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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
23 #include "gdbusauthobserver.h"
24 #include "gcredentials.h"
25 #include "gioenumtypes.h"
26 #include "giostream.h"
27 #include "gdbusprivate.h"
32 * SECTION:gdbusauthobserver
33 * @short_description: Object used for authenticating connections
36 * The #GDBusAuthObserver type provides a mechanism for participating
37 * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
38 * peers. Simply instantiate a #GDBusAuthObserver and connect to the
39 * signals you are interested in. Note that new signals may be added
42 * ## Controlling Authentication # {#auth-observer}
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 use a
46 * signal handler like the following:
48 * |[<!-- language="C" -->
50 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
52 * GCredentials *credentials,
55 * gboolean authorized;
58 * if (credentials != NULL)
60 * GCredentials *own_credentials;
61 * own_credentials = g_credentials_new ();
62 * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
64 * g_object_unref (own_credentials);
72 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
75 * GDBusAuthObserverClass:
76 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
78 * Class structure for #GDBusAuthObserverClass.
82 struct _GDBusAuthObserverClass
85 GObjectClass parent_class;
90 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
92 GCredentials *credentials);
94 gboolean (*allow_mechanism) (GDBusAuthObserver *observer,
95 const gchar *mechanism);
101 * The #GDBusAuthObserver structure contains only private data and
102 * should only be accessed using the provided API.
106 struct _GDBusAuthObserver
108 GObject parent_instance;
113 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
114 ALLOW_MECHANISM_SIGNAL,
118 static guint signals[LAST_SIGNAL] = { 0 };
120 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
122 /* ---------------------------------------------------------------------------------------------------- */
125 g_dbus_auth_observer_finalize (GObject *object)
127 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
131 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
133 GCredentials *credentials)
139 g_dbus_auth_observer_allow_mechanism_real (GDBusAuthObserver *observer,
140 const gchar *mechanism)
146 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
148 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->finalize = g_dbus_auth_observer_finalize;
152 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
153 klass->allow_mechanism = g_dbus_auth_observer_allow_mechanism_real;
156 * GDBusAuthObserver::authorize-authenticated-peer:
157 * @observer: The #GDBusAuthObserver emitting the signal.
158 * @stream: A #GIOStream for the #GDBusConnection.
159 * @credentials: (allow-none): Credentials received from the peer or %NULL.
161 * Emitted to check if a peer that is successfully authenticated
164 * Returns: %TRUE if the peer is authorized, %FALSE if not.
168 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
169 g_signal_new ("authorize-authenticated-peer",
170 G_TYPE_DBUS_AUTH_OBSERVER,
172 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
173 _g_signal_accumulator_false_handled,
174 NULL, /* accu_data */
182 * GDBusAuthObserver::allow-mechanism:
183 * @observer: The #GDBusAuthObserver emitting the signal.
184 * @mechanism: The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
186 * Emitted to check if @mechanism is allowed to be used.
188 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
192 signals[ALLOW_MECHANISM_SIGNAL] =
193 g_signal_new ("allow-mechanism",
194 G_TYPE_DBUS_AUTH_OBSERVER,
196 G_STRUCT_OFFSET (GDBusAuthObserverClass, allow_mechanism),
197 _g_signal_accumulator_false_handled,
198 NULL, /* accu_data */
206 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
211 * g_dbus_auth_observer_new:
213 * Creates a new #GDBusAuthObserver object.
215 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
220 g_dbus_auth_observer_new (void)
222 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
225 /* ---------------------------------------------------------------------------------------------------- */
228 * g_dbus_auth_observer_authorize_authenticated_peer:
229 * @observer: A #GDBusAuthObserver.
230 * @stream: A #GIOStream for the #GDBusConnection.
231 * @credentials: (allow-none): Credentials received from the peer or %NULL.
233 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
235 * Returns: %TRUE if the peer is authorized, %FALSE if not.
240 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
242 GCredentials *credentials)
247 g_signal_emit (observer,
248 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
257 * g_dbus_auth_observer_allow_mechanism:
258 * @observer: A #GDBusAuthObserver.
259 * @mechanism: The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
261 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
263 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
268 g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver *observer,
269 const gchar *mechanism)
274 g_signal_emit (observer,
275 signals[ALLOW_MECHANISM_SIGNAL],