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 "gcredentials.h"
27 #include "gioenumtypes.h"
28 #include "giostream.h"
29 #include "gdbusprivate.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 use a
46 * signal handler like the following:
47 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
49 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
51 * GCredentials *credentials,
54 * gboolean authorized;
57 * if (credentials != NULL)
59 * GCredentials *own_credentials;
60 * own_credentials = g_credentials_new ();
61 * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
63 * g_object_unref (own_credentials);
68 * </programlisting></example>
71 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
74 * GDBusAuthObserverClass:
75 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
77 * Class structure for #GDBusAuthObserverClass.
81 struct _GDBusAuthObserverClass
84 GObjectClass parent_class;
89 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
91 GCredentials *credentials);
93 gboolean (*allow_mechanism) (GDBusAuthObserver *observer,
94 const gchar *mechanism);
100 * The #GDBusAuthObserver structure contains only private data and
101 * should only be accessed using the provided API.
105 struct _GDBusAuthObserver
107 GObject parent_instance;
112 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
113 ALLOW_MECHANISM_SIGNAL,
117 static guint signals[LAST_SIGNAL] = { 0 };
119 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
121 /* ---------------------------------------------------------------------------------------------------- */
124 g_dbus_auth_observer_finalize (GObject *object)
126 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
130 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
132 GCredentials *credentials)
138 g_dbus_auth_observer_allow_mechanism_real (GDBusAuthObserver *observer,
139 const gchar *mechanism)
145 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149 gobject_class->finalize = g_dbus_auth_observer_finalize;
151 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
152 klass->allow_mechanism = g_dbus_auth_observer_allow_mechanism_real;
155 * GDBusAuthObserver::authorize-authenticated-peer:
156 * @observer: The #GDBusAuthObserver emitting the signal.
157 * @stream: A #GIOStream for the #GDBusConnection.
158 * @credentials: (allow-none): Credentials received from the peer or %NULL.
160 * Emitted to check if a peer that is successfully authenticated
163 * Returns: %TRUE if the peer is authorized, %FALSE if not.
167 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
168 g_signal_new ("authorize-authenticated-peer",
169 G_TYPE_DBUS_AUTH_OBSERVER,
171 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
172 _g_signal_accumulator_false_handled,
173 NULL, /* accu_data */
181 * GDBusAuthObserver::allow-mechanism:
182 * @observer: The #GDBusAuthObserver emitting the signal.
183 * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>.
185 * Emitted to check if @mechanism is allowed to be used.
187 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
191 signals[ALLOW_MECHANISM_SIGNAL] =
192 g_signal_new ("allow-mechanism",
193 G_TYPE_DBUS_AUTH_OBSERVER,
195 G_STRUCT_OFFSET (GDBusAuthObserverClass, allow_mechanism),
196 _g_signal_accumulator_false_handled,
197 NULL, /* accu_data */
205 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
210 * g_dbus_auth_observer_new:
212 * Creates a new #GDBusAuthObserver object.
214 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
219 g_dbus_auth_observer_new (void)
221 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
224 /* ---------------------------------------------------------------------------------------------------- */
227 * g_dbus_auth_observer_authorize_authenticated_peer:
228 * @observer: A #GDBusAuthObserver.
229 * @stream: A #GIOStream for the #GDBusConnection.
230 * @credentials: (allow-none): Credentials received from the peer or %NULL.
232 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
234 * Returns: %TRUE if the peer is authorized, %FALSE if not.
239 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
241 GCredentials *credentials)
246 g_signal_emit (observer,
247 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
256 * g_dbus_auth_observer_allow_mechanism:
257 * @observer: A #GDBusAuthObserver.
258 * @mechanism: The name of the mechanism, e.g. <literal>DBUS_COOKIE_SHA1</literal>.
260 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
262 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
267 g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver *observer,
268 const gchar *mechanism)
273 g_signal_emit (observer,
274 signals[ALLOW_MECHANISM_SIGNAL],