Merge branch 'master' into gdbus-codegen
[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 #include "gdbusprivate.h"
31
32 #include "glibintl.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 use a
47  * signal handler like the following:
48  * <example id="auth-observer"><title>Controlling Authentication</title><programlisting>
49  * static gboolean
50  * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
51  *                                  GIOStream         *stream,
52  *                                  GCredentials      *credentials,
53  *                                  gpointer           user_data)
54  * {
55  *   gboolean authorized;
56  *
57  *   authorized = FALSE;
58  *   if (credentials != NULL)
59  *     {
60  *       GCredentials *own_credentials;
61  *       own_credentials = g_credentials_new ();
62  *       if (g_credentials_is_same_user (credentials, own_credentials, NULL))
63  *         authorized = TRUE;
64  *       g_object_unref (own_credentials);
65  *     }
66  *
67  *   return authorized;
68  * }
69  * </programlisting></example>
70  */
71
72 typedef struct _GDBusAuthObserverClass GDBusAuthObserverClass;
73
74 /**
75  * GDBusAuthObserverClass:
76  * @authorize_authenticated_peer: Signal class handler for the #GDBusAuthObserver::authorize-authenticated-peer signal.
77  *
78  * Class structure for #GDBusAuthObserverClass.
79  *
80  * Since: 2.26
81  */
82 struct _GDBusAuthObserverClass
83 {
84   /*< private >*/
85   GObjectClass parent_class;
86
87   /*< public >*/
88
89   /* Signals */
90   gboolean (*authorize_authenticated_peer) (GDBusAuthObserver  *observer,
91                                             GIOStream          *stream,
92                                             GCredentials       *credentials);
93 };
94
95 /**
96  * GDBusAuthObserver:
97  *
98  * The #GDBusAuthObserver structure contains only private data and
99  * should only be accessed using the provided API.
100  *
101  * Since: 2.26
102  */
103 struct _GDBusAuthObserver
104 {
105   GObject parent_instance;
106 };
107
108 enum
109 {
110   AUTHORIZE_AUTHENTICATED_PEER_SIGNAL,
111   LAST_SIGNAL,
112 };
113
114 static guint signals[LAST_SIGNAL] = { 0 };
115
116 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
117
118 /* ---------------------------------------------------------------------------------------------------- */
119
120 static void
121 g_dbus_auth_observer_finalize (GObject *object)
122 {
123   G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
124 }
125
126 static gboolean
127 g_dbus_auth_observer_authorize_authenticated_peer_real (GDBusAuthObserver  *observer,
128                                                         GIOStream          *stream,
129                                                         GCredentials       *credentials)
130 {
131   return TRUE;
132 }
133
134 static void
135 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
136 {
137   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
138
139   gobject_class->finalize = g_dbus_auth_observer_finalize;
140
141   klass->authorize_authenticated_peer = g_dbus_auth_observer_authorize_authenticated_peer_real;
142
143   /**
144    * GDBusAuthObserver::authorize-authenticated-peer:
145    * @observer: The #GDBusAuthObserver emitting the signal.
146    * @stream: A #GIOStream for the #GDBusConnection.
147    * @credentials: Credentials received from the peer or %NULL.
148    *
149    * Emitted to check if a peer that is successfully authenticated
150    * is authorized.
151    *
152    * Returns: %TRUE if the peer is authorized, %FALSE if not.
153    *
154    * Since: 2.26
155    */
156   signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL] =
157     g_signal_new ("authorize-authenticated-peer",
158                   G_TYPE_DBUS_AUTH_OBSERVER,
159                   G_SIGNAL_RUN_LAST,
160                   G_STRUCT_OFFSET (GDBusAuthObserverClass, authorize_authenticated_peer),
161                   _g_signal_accumulator_false_handled,
162                   NULL, /* accu_data */
163                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
164                   G_TYPE_BOOLEAN,
165                   2,
166                   G_TYPE_IO_STREAM,
167                   G_TYPE_CREDENTIALS);
168 }
169
170 static void
171 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
172 {
173 }
174
175 /**
176  * g_dbus_auth_observer_new:
177  *
178  * Creates a new #GDBusAuthObserver object.
179  *
180  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
181  *
182  * Since: 2.26
183  */
184 GDBusAuthObserver *
185 g_dbus_auth_observer_new (void)
186 {
187   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
188 }
189
190 /* ---------------------------------------------------------------------------------------------------- */
191
192 /**
193  * g_dbus_auth_observer_authorize_authenticated_peer:
194  * @observer: A #GDBusAuthObserver.
195  * @stream: A #GIOStream for the #GDBusConnection.
196  * @credentials: Credentials received from the peer or %NULL.
197  *
198  * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
199  *
200  * Returns: %TRUE if the peer is authorized, %FALSE if not.
201  *
202  * Since: 2.26
203  */
204 gboolean
205 g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver  *observer,
206                                                    GIOStream          *stream,
207                                                    GCredentials       *credentials)
208 {
209   gboolean denied;
210
211   denied = FALSE;
212   g_signal_emit (observer,
213                  signals[AUTHORIZE_AUTHENTICATED_PEER_SIGNAL],
214                  0,
215                  stream,
216                  credentials,
217                  &denied);
218   return denied;
219 }