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