Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gdbusauthmechanismanon.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 "gdbusauthmechanismanon.h"
26 #include "gdbuserror.h"
27 #include "gioenumtypes.h"
28
29 #include "glibintl.h"
30 #include "gioalias.h"
31
32 struct _GDBusAuthMechanismAnonPrivate
33 {
34   gboolean is_client;
35   gboolean is_server;
36   GDBusAuthMechanismState state;
37 };
38
39 static gint                     mechanism_get_priority              (void);
40 static const gchar             *mechanism_get_name                  (void);
41
42 static gboolean                 mechanism_is_supported              (GDBusAuthMechanism   *mechanism);
43 static gchar                   *mechanism_encode_data               (GDBusAuthMechanism   *mechanism,
44                                                                      const gchar          *data,
45                                                                      gsize                 data_len,
46                                                                      gsize                *out_data_len);
47 static gchar                   *mechanism_decode_data               (GDBusAuthMechanism   *mechanism,
48                                                                      const gchar          *data,
49                                                                      gsize                 data_len,
50                                                                      gsize                *out_data_len);
51 static GDBusAuthMechanismState  mechanism_server_get_state          (GDBusAuthMechanism   *mechanism);
52 static void                     mechanism_server_initiate           (GDBusAuthMechanism   *mechanism,
53                                                                      const gchar          *initial_response,
54                                                                      gsize                 initial_response_len);
55 static void                     mechanism_server_data_receive       (GDBusAuthMechanism   *mechanism,
56                                                                      const gchar          *data,
57                                                                      gsize                 data_len);
58 static gchar                   *mechanism_server_data_send          (GDBusAuthMechanism   *mechanism,
59                                                                      gsize                *out_data_len);
60 static gchar                   *mechanism_server_get_reject_reason  (GDBusAuthMechanism   *mechanism);
61 static void                     mechanism_server_shutdown           (GDBusAuthMechanism   *mechanism);
62 static GDBusAuthMechanismState  mechanism_client_get_state          (GDBusAuthMechanism   *mechanism);
63 static gchar                   *mechanism_client_initiate           (GDBusAuthMechanism   *mechanism,
64                                                                      gsize                *out_initial_response_len);
65 static void                     mechanism_client_data_receive       (GDBusAuthMechanism   *mechanism,
66                                                                      const gchar          *data,
67                                                                      gsize                 data_len);
68 static gchar                   *mechanism_client_data_send          (GDBusAuthMechanism   *mechanism,
69                                                                      gsize                *out_data_len);
70 static void                     mechanism_client_shutdown           (GDBusAuthMechanism   *mechanism);
71
72 /* ---------------------------------------------------------------------------------------------------- */
73
74 G_DEFINE_TYPE (GDBusAuthMechanismAnon, _g_dbus_auth_mechanism_anon, G_TYPE_DBUS_AUTH_MECHANISM);
75
76 /* ---------------------------------------------------------------------------------------------------- */
77
78 static void
79 _g_dbus_auth_mechanism_anon_finalize (GObject *object)
80 {
81   //GDBusAuthMechanismAnon *mechanism = G_DBUS_AUTH_MECHANISM_ANON (object);
82
83   if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class)->finalize != NULL)
84     G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class)->finalize (object);
85 }
86
87 static void
88 _g_dbus_auth_mechanism_anon_class_init (GDBusAuthMechanismAnonClass *klass)
89 {
90   GObjectClass *gobject_class;
91   GDBusAuthMechanismClass *mechanism_class;
92
93   g_type_class_add_private (klass, sizeof (GDBusAuthMechanismAnonPrivate));
94
95   gobject_class = G_OBJECT_CLASS (klass);
96   gobject_class->finalize = _g_dbus_auth_mechanism_anon_finalize;
97
98   mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
99   mechanism_class->get_priority              = mechanism_get_priority;
100   mechanism_class->get_name                  = mechanism_get_name;
101   mechanism_class->is_supported              = mechanism_is_supported;
102   mechanism_class->encode_data               = mechanism_encode_data;
103   mechanism_class->decode_data               = mechanism_decode_data;
104   mechanism_class->server_get_state          = mechanism_server_get_state;
105   mechanism_class->server_initiate           = mechanism_server_initiate;
106   mechanism_class->server_data_receive       = mechanism_server_data_receive;
107   mechanism_class->server_data_send          = mechanism_server_data_send;
108   mechanism_class->server_get_reject_reason  = mechanism_server_get_reject_reason;
109   mechanism_class->server_shutdown           = mechanism_server_shutdown;
110   mechanism_class->client_get_state          = mechanism_client_get_state;
111   mechanism_class->client_initiate           = mechanism_client_initiate;
112   mechanism_class->client_data_receive       = mechanism_client_data_receive;
113   mechanism_class->client_data_send          = mechanism_client_data_send;
114   mechanism_class->client_shutdown           = mechanism_client_shutdown;
115 }
116
117 static void
118 _g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon *mechanism)
119 {
120   mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
121                                                  G_TYPE_DBUS_AUTH_MECHANISM_ANON,
122                                                  GDBusAuthMechanismAnonPrivate);
123 }
124
125 /* ---------------------------------------------------------------------------------------------------- */
126
127
128 static gint
129 mechanism_get_priority (void)
130 {
131   /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
132   return 50;
133 }
134
135
136 static const gchar *
137 mechanism_get_name (void)
138 {
139   return "ANONYMOUS";
140 }
141
142 static gboolean
143 mechanism_is_supported (GDBusAuthMechanism *mechanism)
144 {
145   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), FALSE);
146   return TRUE;
147 }
148
149 static gchar *
150 mechanism_encode_data (GDBusAuthMechanism   *mechanism,
151                        const gchar          *data,
152                        gsize                 data_len,
153                        gsize                *out_data_len)
154 {
155   return NULL;
156 }
157
158
159 static gchar *
160 mechanism_decode_data (GDBusAuthMechanism   *mechanism,
161                        const gchar          *data,
162                        gsize                 data_len,
163                        gsize                *out_data_len)
164 {
165   return NULL;
166 }
167
168 /* ---------------------------------------------------------------------------------------------------- */
169
170 static GDBusAuthMechanismState
171 mechanism_server_get_state (GDBusAuthMechanism   *mechanism)
172 {
173   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
174
175   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
176   g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
177
178   return m->priv->state;
179 }
180
181 static void
182 mechanism_server_initiate (GDBusAuthMechanism   *mechanism,
183                            const gchar          *initial_response,
184                            gsize                 initial_response_len)
185 {
186   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
187
188   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
189   g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
190
191   if (initial_response != NULL)
192     g_debug ("ANONYMOUS: initial_response was `%s'", initial_response);
193
194   m->priv->is_server = TRUE;
195   m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
196 }
197
198 static void
199 mechanism_server_data_receive (GDBusAuthMechanism   *mechanism,
200                                const gchar          *data,
201                                gsize                 data_len)
202 {
203   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
204
205   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
206   g_return_if_fail (m->priv->is_server && !m->priv->is_client);
207   g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
208
209   /* can never end up here because we are never in the WAITING_FOR_DATA state */
210   g_assert_not_reached ();
211 }
212
213 static gchar *
214 mechanism_server_data_send (GDBusAuthMechanism   *mechanism,
215                             gsize                *out_data_len)
216 {
217   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
218
219   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
220   g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
221   g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
222
223   /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
224   g_assert_not_reached ();
225
226   return NULL;
227 }
228
229 static gchar *
230 mechanism_server_get_reject_reason (GDBusAuthMechanism   *mechanism)
231 {
232   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
233
234   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
235   g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
236   g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
237
238   /* can never end up here because we are never in the REJECTED state */
239   g_assert_not_reached ();
240
241   return NULL;
242 }
243
244 static void
245 mechanism_server_shutdown (GDBusAuthMechanism   *mechanism)
246 {
247   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
248
249   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
250   g_return_if_fail (m->priv->is_server && !m->priv->is_client);
251
252   m->priv->is_server = FALSE;
253 }
254
255 /* ---------------------------------------------------------------------------------------------------- */
256
257 static GDBusAuthMechanismState
258 mechanism_client_get_state (GDBusAuthMechanism   *mechanism)
259 {
260   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
261
262   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
263   g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
264
265   return m->priv->state;
266 }
267
268 static gchar *
269 mechanism_client_initiate (GDBusAuthMechanism   *mechanism,
270                            gsize                *out_initial_response_len)
271 {
272   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
273
274   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
275   g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
276
277   m->priv->is_client = TRUE;
278   m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
279
280   *out_initial_response_len = -1;
281
282   /* just return our library name and version */
283   return g_strdup ("GDBus 0.1");
284 }
285
286 static void
287 mechanism_client_data_receive (GDBusAuthMechanism   *mechanism,
288                                const gchar          *data,
289                                gsize                 data_len)
290 {
291   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
292
293   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
294   g_return_if_fail (m->priv->is_client && !m->priv->is_server);
295   g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
296
297   /* can never end up here because we are never in the WAITING_FOR_DATA state */
298   g_assert_not_reached ();
299 }
300
301 static gchar *
302 mechanism_client_data_send (GDBusAuthMechanism   *mechanism,
303                             gsize                *out_data_len)
304 {
305   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
306
307   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), NULL);
308   g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
309   g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
310
311   /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
312   g_assert_not_reached ();
313
314   return NULL;
315 }
316
317 static void
318 mechanism_client_shutdown (GDBusAuthMechanism   *mechanism)
319 {
320   GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
321
322   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism));
323   g_return_if_fail (m->priv->is_client && !m->priv->is_server);
324
325   m->priv->is_client = FALSE;
326 }
327
328 /* ---------------------------------------------------------------------------------------------------- */
329
330 #define __G_DBUS_AUTH_MECHANISM_ANON_C__
331 #include "gioaliasdef.c"