Initial GDBus code-drop from GDBus-standalone repo
[platform/upstream/glib.git] / gio / gdbusauthobserver.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2009 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 <glib/gi18n.h>
26
27 #include "gdbusauthobserver.h"
28 #include "gio-marshal.h"
29 #include "gcredentials.h"
30 #include "gioenumtypes.h"
31 #include "giostream.h"
32
33 /**
34  * SECTION:gdbusauthobserver
35  * @short_description: Object used for authenticating connections
36  * @include: gdbus/gdbus.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_deny_authenticated_peer (GDBusAuthObserver *observer,
49  *                             GIOStream         *stream,
50  *                             GCredentials      *credentials,
51  *                             gpointer           user_data)
52  * {
53  *   gboolean deny;
54  *   deny = TRUE;
55  *   if (credentials != NULL &&
56  *       g_credentials_has_unix_user (credentials) &&
57  *       g_credentials_get_unix_user (credentials) == getuid ())
58  *     deny = FALSE;
59  *   return deny;
60  * }
61  *
62  * static gboolean
63  * on_new_connection (GDBusServer     *server,
64  *                    GDBusConnection *connection,
65  *                    gpointer         user_data)
66  * {
67  *   /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
68  * }
69  *
70  * [...]
71  *
72  * GDBusAuthObserver *observer;
73  * GDBusServer *server;
74  * GError *error;
75  *
76  * error = NULL;
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,
80  *                                  observer,
81  *                                  NULL, /<!-- -->* GCancellable *<!-- -->/
82  *                                  &error);
83  * g_signal_connect (observer,
84  *                   "deny-authenticated-peer",
85  *                   G_CALLBACK (on_deny_authenticated_peer),
86  *                   NULL);
87  * g_signal_connect (server,
88  *                   "new-connection",
89  *                   G_CALLBACK (on_new_connection),
90  *                   NULL);
91  * g_object_unref (observer);
92  * g_dbus_server_start (server);
93  * </programlisting></example>
94  */
95
96 struct _GDBusAuthObserverPrivate
97 {
98   gint foo;
99 };
100
101 enum
102 {
103   DENY_AUTHENTICATED_PEER_SIGNAL,
104   LAST_SIGNAL,
105 };
106
107 static guint signals[LAST_SIGNAL] = { 0 };
108
109 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
110
111 /* ---------------------------------------------------------------------------------------------------- */
112
113 static void
114 g_dbus_auth_observer_finalize (GObject *object)
115 {
116   //GDBusAuthObserver *observer = G_DBUS_AUTH_OBSERVER (object);
117
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);
120 }
121
122 static gboolean
123 g_dbus_auth_observer_deny_authenticated_peer_real (GDBusAuthObserver  *observer,
124                                                    GIOStream          *stream,
125                                                    GCredentials       *credentials)
126 {
127   return FALSE;
128 }
129
130 static void
131 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
132 {
133   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134
135   gobject_class->finalize     = g_dbus_auth_observer_finalize;
136
137   klass->deny_authenticated_peer = g_dbus_auth_observer_deny_authenticated_peer_real;
138
139   /**
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.
144    *
145    * Emitted to check if a peer that is successfully authenticated
146    * should be denied.
147    *
148    * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
149    */
150   signals[DENY_AUTHENTICATED_PEER_SIGNAL] =
151     g_signal_new ("deny-authenticated-peer",
152                   G_TYPE_DBUS_AUTH_OBSERVER,
153                   G_SIGNAL_RUN_LAST,
154                   G_STRUCT_OFFSET (GDBusAuthObserverClass, deny_authenticated_peer),
155                   g_signal_accumulator_true_handled,
156                   NULL, /* accu_data */
157                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
158                   G_TYPE_BOOLEAN,
159                   2,
160                   G_TYPE_IO_STREAM,
161                   G_TYPE_CREDENTIALS);
162
163
164   g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
165 }
166
167 static void
168 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
169 {
170   /* not used for now */
171   observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
172                                                 G_TYPE_DBUS_AUTH_OBSERVER,
173                                                 GDBusAuthObserverPrivate);;
174 }
175
176 /**
177  * g_dbus_auth_observer_new:
178  *
179  * Creates a new #GDBusAuthObserver object.
180  *
181  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
182  */
183 GDBusAuthObserver *
184 g_dbus_auth_observer_new (void)
185 {
186   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
187 }
188
189 /* ---------------------------------------------------------------------------------------------------- */
190
191 /**
192  * g_dbus_auth_observer_deny_authenticated_peer:
193  * @observer: A #GDBusAuthObserver.
194  * @stream: A #GIOStream for the #GDBusConnection.
195  * @credentials: Credentials received from the peer or %NULL.
196  *
197  * Emits the #GDBusAuthObserver::deny-authenticated-peer signal on @observer.
198  *
199  * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
200  */
201 gboolean
202 g_dbus_auth_observer_deny_authenticated_peer (GDBusAuthObserver  *observer,
203                                               GIOStream          *stream,
204                                               GCredentials       *credentials)
205 {
206   gboolean denied;
207
208   denied = FALSE;
209   g_signal_emit (observer,
210                  signals[DENY_AUTHENTICATED_PEER_SIGNAL],
211                  0,
212                  stream,
213                  credentials,
214                  &denied);
215   return denied;
216 }
217
218