gitignore
[glib.git] / gio / gdbusauthobserver.c
blobb3a13532daaad0674673847e73cc8ad9ce14b02c
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>
23 #include "config.h"
25 #include "gdbusauthobserver.h"
26 #include "gcredentials.h"
27 #include "gioenumtypes.h"
28 #include "giostream.h"
29 #include "gdbusprivate.h"
31 #include "glibintl.h"
33 /**
34 * SECTION:gdbusauthobserver
35 * @short_description: Object used for authenticating connections
36 * @include: gio/gio.h
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
42 * in the future
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>
48 * static gboolean
49 * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
50 * GIOStream *stream,
51 * GCredentials *credentials,
52 * gpointer user_data)
53 * {
54 * gboolean authorized;
56 * authorized = FALSE;
57 * if (credentials != NULL)
58 * {
59 * GCredentials *own_credentials;
60 * own_credentials = g_credentials_new ();
61 * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
62 * authorized = TRUE;
63 * g_object_unref (own_credentials);
64 * }
66 * return authorized;
67 * }
68 * </programlisting></example>
71 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
73 /**
74 * GDBusAuthObserverClass:
75 * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
77 * Class structure for #GDBusAuthObserverClass.
79 * Since: 2.26
81 struct _GDBusAuthObserverClass
83 /*< private >*/
84 GObjectClass parent_class;
86 /*< public >*/
88 /* Signals */
89 gboolean (*authorize_authenticated_peer) (GDBusAuthObserver *observer,
90 GIOStream *stream,
91 GCredentials *credentials);
94 /**
95 * GDBusAuthObserver:
97 * The #GDBusAuthObserver structure contains only private data and
98 * should only be accessed using the provided API.
100 * Since: 2.26
102 struct _GDBusAuthObserver
104 GObject parent_instance;
107 enum
109 AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
110 LAST_SIGNAL,
113 static guint signals[LAST_SIGNAL] = { 0 };
115 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
117 /* ---------------------------------------------------------------------------------------------------- */
119 static void
120 g_dbus_auth_observer_finalize (GObject *object)
122 G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
125 static gboolean
126 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver *observer,
127 GIOStream *stream,
128 GCredentials *credentials)
130 return TRUE;
133 static void
134 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138 gobject_class->finalize = g_dbus_auth_observer_finalize;
140 klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
143 * GDBusAuthObserver::authorize-authenticated-peer:
144 * @observer: The #GDBusAuthObserver emitting the signal.
145 * @stream: A #GIOStream for the #GDBusConnection.
146 * @credentials: Credentials received from the peer or %NULL.
148 * Emitted to check if a peer that is successfully authenticated
149 * is authorized.
151 * Returns: %TRUE if the peer is authorized, %FALSE if not.
153 * Since: 2.26
155 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
156 g_signal_new ("authorize-authenticated-peer",
157 G_TYPE_DBUS_AUTH_OBSERVER,
158 G_SIGNAL_RUN_LAST,
159 G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
160 _g_signal_accumulator_false_handled,
161 NULL, /* accu_data */
162 NULL,
163 G_TYPE_BOOLEAN,
165 G_TYPE_IO_STREAM,
166 G_TYPE_CREDENTIALS);
169 static void
170 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
175 * g_dbus_auth_observer_new:
177 * Creates a new #GDBusAuthObserver object.
179 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
181 * Since: 2.26
183 GDBusAuthObserver *
184 g_dbus_auth_observer_new (void)
186 return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
189 /* ---------------------------------------------------------------------------------------------------- */
192 * g_dbus_auth_observer_authorize_authenticated_peer:
193 * @observer: A #GDBusAuthObserver.
194 * @stream: A #GIOStream for the #GDBusConnection.
195 * @credentials: Credentials received from the peer or %NULL.
197 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
199 * Returns: %TRUE if the peer is authorized, %FALSE if not.
201 * Since: 2.26
203 gboolean
204 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver *observer,
205 GIOStream *stream,
206 GCredentials *credentials)
208 gboolean denied;
210 denied = FALSE;
211 g_signal_emit (observer,
212 signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
214 stream,
215 credentials,
216 &denied);
217 return denied;