GDBus: Hide class structures for classes we don't want to be subclassed
[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 #include "gioalias.h"
33
34 /**
35  * SECTION:gdbusauthobserver
36  * @short_description: Object used for authenticating connections
37  * @include: gio/gio.h
38  *
39  * The #GDBusAuthObserver type provides a mechanism for participating
40  * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
41  * peers. Simply instantiate a #GDBusAuthObserver and connect to the
42  * signals you are interested in. Note that new signals may be added
43  * in the future
44  *
45  * For example, if you only want to allow D-Bus connections from
46  * processes owned by the same uid as the server, you would do this:
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  *   GCredentials *me;
55  *   gboolean authorized;
56  *
57  *   authorized = FALSE;
58  *   me = g_credentials_new ();
59  *
60  *   if (credentials != NULL &&
61  *       !g_credentials_is_same_user (credentials, me))
62  *     authorized = TRUE;
63  *
64  *   g_object_unref (me);
65  *
66  *   return authorized;
67  * }
68  *
69  * static gboolean
70  * on_new_connection (GDBusServer     *server,
71  *                    GDBusConnection *connection,
72  *                    gpointer         user_data)
73  * {
74  *   /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
75  * }
76  *
77  * [...]
78  *
79  * GDBusAuthObserver *observer;
80  * GDBusServer *server;
81  * GError *error;
82  *
83  * error = NULL;
84  * observer = g_dbus_auth_observer_new ();
85  * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
86  *                                  G_DBUS_SERVER_FLAGS_NONE,
87  *                                  observer,
88  *                                  NULL, /<!-- -->* GCancellable *<!-- -->/
89  *                                  &error);
90  * g_signal_connect (observer,
91  *                   "authorize-authenticated-peer",
92  *                   G_CALLBACK (on_authorize_authenticated_peer),
93  *                   NULL);
94  * g_signal_connect (server,
95  *                   "new-connection",
96  *                   G_CALLBACK (on_new_connection),
97  *                   NULL);
98  * g_object_unref (observer);
99  * g_dbus_server_start (server);
100  * </programlisting></example>
101  */
102
103 /**
104  * GDBusAuthObserverClass:
105  * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
106  *
107  * Class structure for #GDBusAuthObserverClass.
108  *
109  * Since: 2.26
110  */
111 struct _GDBusAuthObserverClass
112 {
113   /*< private >*/
114   GObjectClass parent_class;
115
116   /*< public >*/
117
118   /* Signals */
119   gboolean (*authorize_authenticated_peer) (GDBusAuthObserver  *observer,
120                                             GIOStream          *stream,
121                                             GCredentials       *credentials);
122 };
123
124 struct _GDBusAuthObserverPrivate
125 {
126   gint foo;
127 };
128
129 enum
130 {
131   AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
132   LAST_SIGNAL,
133 };
134
135 static guint signals[LAST_SIGNAL] = { 0 };
136
137 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
138
139 /* ---------------------------------------------------------------------------------------------------- */
140
141 static void
142 g_dbus_auth_observer_finalize (GObject *object)
143 {
144   G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
145 }
146
147 static gboolean
148 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver  *observer,
149                                                         GIOStream          *stream,
150                                                         GCredentials       *credentials)
151 {
152   return TRUE;
153 }
154
155 gboolean
156 _g_signal_accumulator_false_handled (GSignalInvocationHint *ihint,
157                                      GValue                *return_accu,
158                                      const GValue          *handler_return,
159                                      gpointer               dummy)
160 {
161   gboolean continue_emission;
162   gboolean signal_handled;
163
164   signal_handled = g_value_get_boolean (handler_return);
165   g_value_set_boolean (return_accu, signal_handled);
166   continue_emission = signal_handled;
167
168   return continue_emission;
169 }
170
171 static void
172 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
173 {
174   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
175
176   gobject_class->finalize = g_dbus_auth_observer_finalize;
177
178   klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
179
180   /**
181    * GDBusAuthObserver::authorize-authenticated-peer:
182    * @observer: The #GDBusAuthObserver emitting the signal.
183    * @stream: A #GIOStream for the #GDBusConnection.
184    * @credentials: Credentials received from the peer or %NULL.
185    *
186    * Emitted to check if a peer that is successfully authenticated
187    * is authorized.
188    *
189    * Returns: %TRUE if the peer is authorized, %FALSE if not.
190    *
191    * Since: 2.26
192    */
193   signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
194     g_signal_new ("authorize-authenticated-peer",
195                   G_TYPE_DBUS_AUTH_OBSERVER,
196                   G_SIGNAL_RUN_LAST,
197                   G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
198                   _g_signal_accumulator_false_handled,
199                   NULL, /* accu_data */
200                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
201                   G_TYPE_BOOLEAN,
202                   2,
203                   G_TYPE_IO_STREAM,
204                   G_TYPE_CREDENTIALS);
205
206
207   g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
208 }
209
210 static void
211 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
212 {
213   /* not used for now */
214   observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
215                                                 G_TYPE_DBUS_AUTH_OBSERVER,
216                                                 GDBusAuthObserverPrivate);
217 }
218
219 /**
220  * g_dbus_auth_observer_new:
221  *
222  * Creates a new #GDBusAuthObserver object.
223  *
224  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
225  *
226  * Since: 2.26
227  */
228 GDBusAuthObserver *
229 g_dbus_auth_observer_new (void)
230 {
231   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
232 }
233
234 /* ---------------------------------------------------------------------------------------------------- */
235
236 /**
237  * g_dbus_auth_observer_authorize_authenticated_peer:
238  * @observer: A #GDBusAuthObserver.
239  * @stream: A #GIOStream for the #GDBusConnection.
240  * @credentials: Credentials received from the peer or %NULL.
241  *
242  * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
243  *
244  * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
245  *
246  * Since: 2.26
247  */
248 gboolean
249 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver  *observer,
250                                                    GIOStream          *stream,
251                                                    GCredentials       *credentials)
252 {
253   gboolean denied;
254
255   denied = FALSE;
256   g_signal_emit (observer,
257                  signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
258                  0,
259                  stream,
260                  credentials,
261                  &denied);
262   return denied;
263 }
264
265
266
267 #define __G_DBUS_AUTH_OBSERVER_C__
268 #include "gioaliasdef.c"