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.1 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"
30 #include "gmarshal-internal.h"
33 * SECTION:gdbusauthobserver
34 * @short_description: Object used for authenticating connections
37 * The #GDBusAuthObserver type provides a mechanism for participating
38 * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
39 * peers. Simply instantiate a #GDBusAuthObserver and connect to the
40 * signals you are interested in. Note that new signals may be added
43 * ## Controlling Authentication Mechanisms
45 * By default, a #GDBusServer or server-side #GDBusConnection will allow
46 * any authentication mechanism to be used. If you only
47 * want to allow D-Bus connections with the `EXTERNAL` mechanism,
48 * which makes use of credentials passing and is the recommended
49 * mechanism for modern Unix platforms such as Linux and the BSD family,
50 * you would use a signal handler like this:
52 * |[<!-- language="C" -->
54 * on_allow_mechanism (GDBusAuthObserver *observer,
55 * const gchar *mechanism,
58 * if (g_strcmp0 (mechanism, "EXTERNAL") == 0)
67 * ## Controlling Authorization # {#auth-observer}
69 * By default, a #GDBusServer or server-side #GDBusConnection will accept
70 * connections from any successfully authenticated user (but not from
71 * anonymous connections using the `ANONYMOUS` mechanism). If you only
72 * want to allow D-Bus connections from processes owned by the same uid
73 * as the server, since GLib 2.68, you should use the
74 * %G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flag. It’s equivalent
75 * to the following signal handler:
77 * |[<!-- language="C" -->
79 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
81 * GCredentials *credentials,
84 * gboolean authorized;
87 * if (credentials != NULL)
89 * GCredentials *own_credentials;
90 * own_credentials = g_credentials_new ();
91 * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
93 * g_object_unref (own_credentials);
101 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
104 * GDBusAuthObserverClass:
105 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
107 * Class structure for #GDBusAuthObserverClass.
111 struct _GDBusAuthObserverClass
114 GObjectClass parent_class;
119 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
121 GCredentials *credentials);
123 gboolean (*allow_mechanism) (GDBusAuthObserver *observer,
124 const gchar *mechanism);
130 * The #GDBusAuthObserver structure contains only private data and
131 * should only be accessed using the provided API.
135 struct _GDBusAuthObserver
137 GObject parent_instance;
142 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
143 ALLOW_MECHANISM_SIGNAL,
147 static guint signals[LAST_SIGNAL] = { 0 };
149 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT)
151 /* ---------------------------------------------------------------------------------------------------- */
154 g_dbus_auth_observer_finalize (GObject *object)
156 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
160 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
162 GCredentials *credentials)
168 g_dbus_auth_observer_allow_mechanism_real (GDBusAuthObserver *observer,
169 const gchar *mechanism)
175 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
177 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
179 gobject_class->finalize = g_dbus_auth_observer_finalize;
181 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
182 klass->allow_mechanism = g_dbus_auth_observer_allow_mechanism_real;
185 * GDBusAuthObserver::authorize-authenticated-peer:
186 * @observer: The #GDBusAuthObserver emitting the signal.
187 * @stream: A #GIOStream for the #GDBusConnection.
188 * @credentials: (nullable): Credentials received from the peer or %NULL.
190 * Emitted to check if a peer that is successfully authenticated
193 * Returns: %TRUE if the peer is authorized, %FALSE if not.
197 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
198 g_signal_new (I_("authorize-authenticated-peer"),
199 G_TYPE_DBUS_AUTH_OBSERVER,
201 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
202 _g_signal_accumulator_false_handled,
203 NULL, /* accu_data */
204 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECT,
209 g_signal_set_va_marshaller (signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
210 G_TYPE_FROM_CLASS (klass),
211 _g_cclosure_marshal_BOOLEAN__OBJECT_OBJECTv);
214 * GDBusAuthObserver::allow-mechanism:
215 * @observer: The #GDBusAuthObserver emitting the signal.
216 * @mechanism: The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
218 * Emitted to check if @mechanism is allowed to be used.
220 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
224 signals[ALLOW_MECHANISM_SIGNAL] =
225 g_signal_new (I_("allow-mechanism"),
226 G_TYPE_DBUS_AUTH_OBSERVER,
228 G_STRUCT_OFFSET (GDBusAuthObserverClass, allow_mechanism),
229 _g_signal_accumulator_false_handled,
230 NULL, /* accu_data */
231 _g_cclosure_marshal_BOOLEAN__STRING,
235 g_signal_set_va_marshaller (signals[ALLOW_MECHANISM_SIGNAL],
236 G_TYPE_FROM_CLASS (klass),
237 _g_cclosure_marshal_BOOLEAN__STRINGv);
241 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
246 * g_dbus_auth_observer_new:
248 * Creates a new #GDBusAuthObserver object.
250 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
255 g_dbus_auth_observer_new (void)
257 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
260 /* ---------------------------------------------------------------------------------------------------- */
263 * g_dbus_auth_observer_authorize_authenticated_peer:
264 * @observer: A #GDBusAuthObserver.
265 * @stream: A #GIOStream for the #GDBusConnection.
266 * @credentials: (nullable): Credentials received from the peer or %NULL.
268 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
270 * Returns: %TRUE if the peer is authorized, %FALSE if not.
275 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
277 GCredentials *credentials)
282 g_signal_emit (observer,
283 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
292 * g_dbus_auth_observer_allow_mechanism:
293 * @observer: A #GDBusAuthObserver.
294 * @mechanism: The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
296 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
298 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
303 g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver *observer,
304 const gchar *mechanism)
309 g_signal_emit (observer,
310 signals[ALLOW_MECHANISM_SIGNAL],