gio/: fully remove gioalias hacks
[platform/upstream/glib.git] / gio / gdbusauthobserver.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gdbusauthobserver.h"
26 #include "gio-marshal.h"
27 #include "gcredentials.h"
28 #include "gioenumtypes.h"
29 #include "giostream.h"
30
31 #include "glibintl.h"
32
33 /**
34  * SECTION:gdbusauthobserver
35  * @short_description: Object used for authenticating connections
36  * @include: gio/gio.h
37  *
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
43  *
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>
47  * static gboolean
48  * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
49  *                                  GIOStream         *stream,
50  *                                  GCredentials      *credentials,
51  *                                  gpointer           user_data)
52  * {
53  *   GCredentials *me;
54  *   gboolean authorized;
55  *
56  *   authorized = FALSE;
57  *   me = g_credentials_new ();
58  *
59  *   if (credentials != NULL &&
60  *       !g_credentials_is_same_user (credentials, me))
61  *     authorized = TRUE;
62  *
63  *   g_object_unref (me);
64  *
65  *   return authorized;
66  * }
67  *
68  * static gboolean
69  * on_new_connection (GDBusServer     *server,
70  *                    GDBusConnection *connection,
71  *                    gpointer         user_data)
72  * {
73  *   /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
74  * }
75  *
76  * [...]
77  *
78  * GDBusAuthObserver *observer;
79  * GDBusServer *server;
80  * GError *error;
81  *
82  * error = NULL;
83  * observer = g_dbus_auth_observer_new ();
84  * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
85  *                                  G_DBUS_SERVER_FLAGS_NONE,
86  *                                  observer,
87  *                                  NULL, /<!-- -->* GCancellable *<!-- -->/
88  *                                  &error);
89  * g_signal_connect (observer,
90  *                   "authorize-authenticated-peer",
91  *                   G_CALLBACK (on_authorize_authenticated_peer),
92  *                   NULL);
93  * g_signal_connect (server,
94  *                   "new-connection",
95  *                   G_CALLBACK (on_new_connection),
96  *                   NULL);
97  * g_object_unref (observer);
98  * g_dbus_server_start (server);
99  * </programlisting></example>
100  */
101
102 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
103
104 /**
105  * GDBusAuthObserverClass:
106  * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
107  *
108  * Class structure for #GDBusAuthObserverClass.
109  *
110  * Since: 2.26
111  */
112 struct _GDBusAuthObserverClass
113 {
114   /*< private >*/
115   GObjectClass parent_class;
116
117   /*< public >*/
118
119   /* Signals */
120   gboolean (*authorize_authenticated_peer) (GDBusAuthObserver  *observer,
121                                             GIOStream          *stream,
122                                             GCredentials       *credentials);
123 };
124
125 /**
126  * GDBusAuthObserver:
127  *
128  * The #GDBusAuthObserver structure contains only private data and
129  * should only be accessed using the provided API.
130  *
131  * Since: 2.26
132  */
133 struct _GDBusAuthObserver
134 {
135   GObject parent_instance;
136 };
137
138 enum
139 {
140   AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
141   LAST_SIGNAL,
142 };
143
144 static guint signals[LAST_SIGNAL] = { 0 };
145
146 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
147
148 /* ---------------------------------------------------------------------------------------------------- */
149
150 static void
151 g_dbus_auth_observer_finalize (GObject *object)
152 {
153   G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
154 }
155
156 static gboolean
157 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver  *observer,
158                                                         GIOStream          *stream,
159                                                         GCredentials       *credentials)
160 {
161   return TRUE;
162 }
163
164 gboolean
165 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
166                                      GValue                *return_accu,
167                                      const GValue          *handler_return,
168                                      gpointer               dummy)
169 {
170   gboolean continue_emission;
171   gboolean signal_handled;
172
173   signal_handled = g_value_get_boolean (handler_return);
174   g_value_set_boolean (return_accu, signal_handled);
175   continue_emission = signal_handled;
176
177   return continue_emission;
178 }
179
180 static void
181 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
182 {
183   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184
185   gobject_class->finalize = g_dbus_auth_observer_finalize;
186
187   klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
188
189   /**
190    * GDBusAuthObserver::authorize-authenticated-peer:
191    * @observer: The #GDBusAuthObserver emitting the signal.
192    * @stream: A #GIOStream for the #GDBusConnection.
193    * @credentials: Credentials received from the peer or %NULL.
194    *
195    * Emitted to check if a peer that is successfully authenticated
196    * is authorized.
197    *
198    * Returns: %TRUE if the peer is authorized, %FALSE if not.
199    *
200    * Since: 2.26
201    */
202   signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
203     g_signal_new ("authorize-authenticated-peer",
204                   G_TYPE_DBUS_AUTH_OBSERVER,
205                   G_SIGNAL_RUN_LAST,
206                   G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
207                   _g_signal_accumulator_false_handled,
208                   NULL, /* accu_data */
209                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
210                   G_TYPE_BOOLEAN,
211                   2,
212                   G_TYPE_IO_STREAM,
213                   G_TYPE_CREDENTIALS);
214 }
215
216 static void
217 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
218 {
219 }
220
221 /**
222  * g_dbus_auth_observer_new:
223  *
224  * Creates a new #GDBusAuthObserver object.
225  *
226  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
227  *
228  * Since: 2.26
229  */
230 GDBusAuthObserver *
231 g_dbus_auth_observer_new (void)
232 {
233   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
234 }
235
236 /* ---------------------------------------------------------------------------------------------------- */
237
238 /**
239  * g_dbus_auth_observer_authorize_authenticated_peer:
240  * @observer: A #GDBusAuthObserver.
241  * @stream: A #GIOStream for the #GDBusConnection.
242  * @credentials: Credentials received from the peer or %NULL.
243  *
244  * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
245  *
246  * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
247  *
248  * Since: 2.26
249  */
250 gboolean
251 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver  *observer,
252                                                    GIOStream          *stream,
253                                                    GCredentials       *credentials)
254 {
255   gboolean denied;
256
257   denied = FALSE;
258   g_signal_emit (observer,
259                  signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
260                  0,
261                  stream,
262                  credentials,
263                  &denied);
264   return denied;
265 }