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