GDBus: Add new symbols to gio.symbols
[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 "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_deny_authenticated_peer (GDBusAuthObserver *observer,
50  *                             GIOStream         *stream,
51  *                             GCredentials      *credentials,
52  *                             gpointer           user_data)
53  * {
54  *   gboolean deny;
55  *   deny = TRUE;
56  *   if (credentials != NULL &&
57  *       g_credentials_has_unix_user (credentials) &&
58  *       g_credentials_get_unix_user (credentials) == getuid ())
59  *     deny = FALSE;
60  *   return deny;
61  * }
62  *
63  * static gboolean
64  * on_new_connection (GDBusServer     *server,
65  *                    GDBusConnection *connection,
66  *                    gpointer         user_data)
67  * {
68  *   /<!-- -->* Guaranteed here that @connection is from a process owned by the same user *<!-- -->/
69  * }
70  *
71  * [...]
72  *
73  * GDBusAuthObserver *observer;
74  * GDBusServer *server;
75  * GError *error;
76  *
77  * error = NULL;
78  * observer = g_dbus_auth_observer_new ();
79  * server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/my-app-name",
80  *                                  G_DBUS_SERVER_FLAGS_NONE,
81  *                                  observer,
82  *                                  NULL, /<!-- -->* GCancellable *<!-- -->/
83  *                                  &error);
84  * g_signal_connect (observer,
85  *                   "deny-authenticated-peer",
86  *                   G_CALLBACK (on_deny_authenticated_peer),
87  *                   NULL);
88  * g_signal_connect (server,
89  *                   "new-connection",
90  *                   G_CALLBACK (on_new_connection),
91  *                   NULL);
92  * g_object_unref (observer);
93  * g_dbus_server_start (server);
94  * </programlisting></example>
95  */
96
97 struct _GDBusAuthObserverPrivate
98 {
99   gint foo;
100 };
101
102 enum
103 {
104   DENY_AUTHENTICATED_PEER_SIGNAL,
105   LAST_SIGNAL,
106 };
107
108 static guint signals[LAST_SIGNAL] = { 0 };
109
110 G_DEFINE_TYPE (GDBusAuthObserver, g_dbus_auth_observer, G_TYPE_OBJECT);
111
112 /* ---------------------------------------------------------------------------------------------------- */
113
114 static void
115 g_dbus_auth_observer_finalize (GObject *object)
116 {
117   //GDBusAuthObserver *observer = G_DBUS_AUTH_OBSERVER (object);
118
119   if (G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize != NULL)
120     G_OBJECT_CLASS (g_dbus_auth_observer_parent_class)->finalize (object);
121 }
122
123 static gboolean
124 g_dbus_auth_observer_deny_authenticated_peer_real (GDBusAuthObserver  *observer,
125                                                    GIOStream          *stream,
126                                                    GCredentials       *credentials)
127 {
128   return FALSE;
129 }
130
131 static void
132 g_dbus_auth_observer_class_init (GDBusAuthObserverClass *klass)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135
136   gobject_class->finalize     = g_dbus_auth_observer_finalize;
137
138   klass->deny_authenticated_peer = g_dbus_auth_observer_deny_authenticated_peer_real;
139
140   /**
141    * GDBusAuthObserver::deny-authenticated-peer:
142    * @observer: The #GDBusAuthObserver emitting the signal.
143    * @stream: A #GIOStream for the #GDBusConnection.
144    * @credentials: Credentials received from the peer or %NULL.
145    *
146    * Emitted to check if a peer that is successfully authenticated
147    * should be denied.
148    *
149    * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
150    *
151    * Since: 2.26
152    */
153   signals[DENY_AUTHENTICATED_PEER_SIGNAL] =
154     g_signal_new ("deny-authenticated-peer",
155                   G_TYPE_DBUS_AUTH_OBSERVER,
156                   G_SIGNAL_RUN_LAST,
157                   G_STRUCT_OFFSET (GDBusAuthObserverClass, deny_authenticated_peer),
158                   g_signal_accumulator_true_handled,
159                   NULL, /* accu_data */
160                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
161                   G_TYPE_BOOLEAN,
162                   2,
163                   G_TYPE_IO_STREAM,
164                   G_TYPE_CREDENTIALS);
165
166
167   g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
168 }
169
170 static void
171 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
172 {
173   /* not used for now */
174   observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
175                                                 G_TYPE_DBUS_AUTH_OBSERVER,
176                                                 GDBusAuthObserverPrivate);;
177 }
178
179 /**
180  * g_dbus_auth_observer_new:
181  *
182  * Creates a new #GDBusAuthObserver object.
183  *
184  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
185  *
186  * Since: 2.26
187  */
188 GDBusAuthObserver *
189 g_dbus_auth_observer_new (void)
190 {
191   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
192 }
193
194 /* ---------------------------------------------------------------------------------------------------- */
195
196 /**
197  * g_dbus_auth_observer_deny_authenticated_peer:
198  * @observer: A #GDBusAuthObserver.
199  * @stream: A #GIOStream for the #GDBusConnection.
200  * @credentials: Credentials received from the peer or %NULL.
201  *
202  * Emits the #GDBusAuthObserver::deny-authenticated-peer signal on @observer.
203  *
204  * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
205  *
206  * Since: 2.26
207  */
208 gboolean
209 g_dbus_auth_observer_deny_authenticated_peer (GDBusAuthObserver  *observer,
210                                               GIOStream          *stream,
211                                               GCredentials       *credentials)
212 {
213   gboolean denied;
214
215   denied = FALSE;
216   g_signal_emit (observer,
217                  signals[DENY_AUTHENTICATED_PEER_SIGNAL],
218                  0,
219                  stream,
220                  credentials,
221                  &denied);
222   return denied;
223 }
224
225
226
227 #define __G_DBUS_AUTH_OBSERVER_C__
228 #include "gioaliasdef.c"