1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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 <glib/gi18n.h>
27 #include "gdbusauthobserver.h"
28 #include "gio-marshal.h"
29 #include "gcredentials.h"
30 #include "gioenumtypes.h"
31 #include "giostream.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 do this:
46 * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
48 * on_deny_authenticated_peer (GDBusAuthObserver *observer,
50 * GCredentials *credentials,
55 * if (credentials != NULL &&
56 * g_credentials_has_unix_user (credentials) &&
57 * g_credentials_get_unix_user (credentials) == getuid ())
63 * on_new_connection (GDBusServer *server,
64 * GDBusConnection *connection,
67 * /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
72 * GDBusAuthObserver *observer;
73 * GDBusServer *server;
77 * observer = g_dbus_auth_observer_new ();
78 * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
79 * G_DBUS_SERVER_FLAGS_NONE,
81 * NULL, /<!-- -->* GCancellable *<!-- -->/
83 * g_signal_connect (observer,
84 * "deny-authenticated-peer",
85 * G_CALLBACK (on_deny_authenticated_peer),
87 * g_signal_connect (server,
89 * G_CALLBACK (on_new_connection),
91 * g_object_unref (observer);
92 * g_dbus_server_start (server);
93 * </programlisting></example>
96 struct _GDBusAuthObserverPrivate
103 DENY_AUTHENTICATED_PEER_SIGNAL,
107 static guint signals[LAST_SIGNAL] = { 0 };
109 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
111 /* ---------------------------------------------------------------------------------------------------- */
114 g_dbus_auth_observer_finalize (GObject *object)
116 //GDBusAuthObserver *observer = G_DBUS_AUTH_OBSERVER (object);
118 if (G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize != NULL)
119 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
123 g_dbus_auth_observer_deny_authenticated_peer_real (GDBusAuthObserver *observer,
125 GCredentials *credentials)
131 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
133 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135 gobject_class->finalize = g_dbus_auth_observer_finalize;
137 klass->deny_authenticated_peer = g_dbus_auth_observer_deny_authenticated_peer_real;
140 * GDBusAuthObserver::deny-authenticated-peer:
141 * @observer: The #GDBusAuthObserver emitting the signal.
142 * @stream: A #GIOStream for the #GDBusConnection.
143 * @credentials: Credentials received from the peer or %NULL.
145 * Emitted to check if a peer that is successfully authenticated
148 * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
152 signals[DENY_AUTHENTICATED_PEER_SIGNAL] =
153 g_signal_new ("deny-authenticated-peer",
154 G_TYPE_DBUS_AUTH_OBSERVER,
156 G_STRUCT_OFFSET (GDBusAuthObserverClass, deny_authenticated_peer),
157 g_signal_accumulator_true_handled,
158 NULL, /* accu_data */
159 _gio_marshal_BOOLEAN__OBJECT_OBJECT,
166 g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
170 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
172 /* not used for now */
173 observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
174 G_TYPE_DBUS_AUTH_OBSERVER,
175 GDBusAuthObserverPrivate);;
179 * g_dbus_auth_observer_new:
181 * Creates a new #GDBusAuthObserver object.
183 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
188 g_dbus_auth_observer_new (void)
190 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
193 /* ---------------------------------------------------------------------------------------------------- */
196 * g_dbus_auth_observer_deny_authenticated_peer:
197 * @observer: A #GDBusAuthObserver.
198 * @stream: A #GIOStream for the #GDBusConnection.
199 * @credentials: Credentials received from the peer or %NULL.
201 * Emits the #GDBusAuthObserver::deny-authenticated-peer signal on @observer.
203 * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
208 g_dbus_auth_observer_deny_authenticated_peer (GDBusAuthObserver *observer,
210 GCredentials *credentials)
215 g_signal_emit (observer,
216 signals[DENY_AUTHENTICATED_PEER_SIGNAL],