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 * For example, if you only want to allow D-Bus connections from
43 * processes owned by the same uid as the server, you would use a
44 * signal handler like the following:
45 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
47 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
49 * GCredentials *credentials,
52 * gboolean authorized;
55 * if (credentials != NULL)
57 * GCredentials *own_credentials;
58 * own_credentials = g_credentials_new ();
59 * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
61 * g_object_unref (own_credentials);
66 * </programlisting></example>
69 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
72 * GDBusAuthObserverClass:
73 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
75 * Class structure for #GDBusAuthObserverClass.
79 struct _GDBusAuthObserverClass
82 GObjectClass parent_class;
87 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
89 GCredentials *credentials);
91 gboolean (*allow_mechanism) (GDBusAuthObserver *observer,
92 const gchar *mechanism);
98 * The #GDBusAuthObserver structure contains only private data and
99 * should only be accessed using the provided API.
103 struct _GDBusAuthObserver
105 GObject parent_instance;
110 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
111 ALLOW_MECHANISM_SIGNAL,
115 static guint signals[LAST_SIGNAL] = { 0 };
117 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
119 /* ---------------------------------------------------------------------------------------------------- */
122 g_dbus_auth_observer_finalize (GObject *object)
124 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
128 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
130 GCredentials *credentials)
136 g_dbus_auth_observer_allow_mechanism_real (GDBusAuthObserver *observer,
137 const gchar *mechanism)
143 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
145 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
147 gobject_class->finalize = g_dbus_auth_observer_finalize;
149 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
150 klass->allow_mechanism = g_dbus_auth_observer_allow_mechanism_real;
153 * GDBusAuthObserver::authorize-authenticated-peer:
154 * @observer: The #GDBusAuthObserver emitting the signal.
155 * @stream: A #GIOStream for the #GDBusConnection.
156 * @credentials: (allow-none): Credentials received from the peer or %NULL.
158 * Emitted to check if a peer that is successfully authenticated
161 * Returns: %TRUE if the peer is authorized, %FALSE if not.
165 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
166 g_signal_new ("authorize-authenticated-peer",
167 G_TYPE_DBUS_AUTH_OBSERVER,
169 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
170 _g_signal_accumulator_false_handled,
171 NULL, /* accu_data */
179 * GDBusAuthObserver::allow-mechanism:
180 * @observer: The #GDBusAuthObserver emitting the signal.
181 * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>.
183 * Emitted to check if @mechanism is allowed to be used.
185 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
189 signals[ALLOW_MECHANISM_SIGNAL] =
190 g_signal_new ("allow-mechanism",
191 G_TYPE_DBUS_AUTH_OBSERVER,
193 G_STRUCT_OFFSET (GDBusAuthObserverClass, allow_mechanism),
194 _g_signal_accumulator_false_handled,
195 NULL, /* accu_data */
203 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
208 * g_dbus_auth_observer_new:
210 * Creates a new #GDBusAuthObserver object.
212 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
217 g_dbus_auth_observer_new (void)
219 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
222 /* ---------------------------------------------------------------------------------------------------- */
225 * g_dbus_auth_observer_authorize_authenticated_peer:
226 * @observer: A #GDBusAuthObserver.
227 * @stream: A #GIOStream for the #GDBusConnection.
228 * @credentials: (allow-none): Credentials received from the peer or %NULL.
230 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
232 * Returns: %TRUE if the peer is authorized, %FALSE if not.
237 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
239 GCredentials *credentials)
244 g_signal_emit (observer,
245 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
254 * g_dbus_auth_observer_allow_mechanism:
255 * @observer: A #GDBusAuthObserver.
256 * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>.
258 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
260 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
265 g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver *observer,
266 const gchar *mechanism)
271 g_signal_emit (observer,
272 signals[ALLOW_MECHANISM_SIGNAL],