GDBus: Fix up i18n
[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
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_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    * Since: 2.26
151    */
152   signals[DENY_AUTHENTICATED_PEER_SIGNAL] =
153     g_signal_new ("deny-authenticated-peer",
154                   G_TYPE_DBUS_AUTH_OBSERVER,
155                   G_SIGNAL_RUN_LAST,
156                   G_STRUCT_OFFSET (GDBusAuthObserverClass, deny_authenticated_peer),
157                   g_signal_accumulator_true_handled,
158                   NULL, /* accu_data */
159                   _gio_marshal_BOOLEAN__OBJECT_OBJECT,
160                   G_TYPE_BOOLEAN,
161                   2,
162                   G_TYPE_IO_STREAM,
163                   G_TYPE_CREDENTIALS);
164
165
166   g_type_class_add_private (klass, sizeof (GDBusAuthObserverPrivate));
167 }
168
169 static void
170 g_dbus_auth_observer_init (GDBusAuthObserver *observer)
171 {
172   /* not used for now */
173   observer->priv = G_TYPE_INSTANCE_GET_PRIVATE (observer,
174                                                 G_TYPE_DBUS_AUTH_OBSERVER,
175                                                 GDBusAuthObserverPrivate);;
176 }
177
178 /**
179  * g_dbus_auth_observer_new:
180  *
181  * Creates a new #GDBusAuthObserver object.
182  *
183  * Returns: A #GDBusAuthObserver. Free with g_object_unref().
184  *
185  * Since: 2.26
186  */
187 GDBusAuthObserver *
188 g_dbus_auth_observer_new (void)
189 {
190   return g_object_new (G_TYPE_DBUS_AUTH_OBSERVER, NULL);
191 }
192
193 /* ---------------------------------------------------------------------------------------------------- */
194
195 /**
196  * g_dbus_auth_observer_deny_authenticated_peer:
197  * @observer: A #GDBusAuthObserver.
198  * @stream: A #GIOStream for the #GDBusConnection.
199  * @credentials: Credentials received from the peer or %NULL.
200  *
201  * Emits the #GDBusAuthObserver::deny-authenticated-peer signal on @observer.
202  *
203  * Returns: %TRUE if the peer should be denied, %FALSE otherwise.
204  *
205  * Since: 2.26
206  */
207 gboolean
208 g_dbus_auth_observer_deny_authenticated_peer (GDBusAuthObserver  *observer,
209                                               GIOStream          *stream,
210                                               GCredentials       *credentials)
211 {
212   gboolean denied;
213
214   denied = FALSE;
215   g_signal_emit (observer,
216                  signals[DENY_AUTHENTICATED_PEER_SIGNAL],
217                  0,
218                  stream,
219                  credentials,
220                  &denied);
221   return denied;
222 }
223
224