1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusauthmechanismanon.h"
26 #include "gdbuserror.h"
27 #include "gioenumtypes.h"
32 struct _GDBusAuthMechanismAnonPrivate
36 GDBusAuthMechanismState state;
39 static gint mechanism_get_priority (void);
40 static const gchar *mechanism_get_name (void);
42 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
43 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
47 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
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,
58 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
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,
68 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
70 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
72 /* ---------------------------------------------------------------------------------------------------- */
74 G_DEFINE_TYPE (GDBusAuthMechanismAnon, _g_dbus_auth_mechanism_anon, G_TYPE_DBUS_AUTH_MECHANISM);
76 /* ---------------------------------------------------------------------------------------------------- */
79 _g_dbus_auth_mechanism_anon_finalize (GObject *object)
81 //GDBusAuthMechanismAnon *mechanism = G_DBUS_AUTH_MECHANISM_ANON (object);
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);
88 _g_dbus_auth_mechanism_anon_class_init (GDBusAuthMechanismAnonClass *klass)
90 GObjectClass *gobject_class;
91 GDBusAuthMechanismClass *mechanism_class;
93 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismAnonPrivate));
95 gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = _g_dbus_auth_mechanism_anon_finalize;
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;
118 _g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon *mechanism)
120 mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
121 G_TYPE_DBUS_AUTH_MECHANISM_ANON,
122 GDBusAuthMechanismAnonPrivate);
125 /* ---------------------------------------------------------------------------------------------------- */
129 mechanism_get_priority (void)
131 /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
137 mechanism_get_name (void)
143 mechanism_is_supported (GDBusAuthMechanism *mechanism)
145 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), FALSE);
150 mechanism_encode_data (GDBusAuthMechanism *mechanism,
160 mechanism_decode_data (GDBusAuthMechanism *mechanism,
168 /* ---------------------------------------------------------------------------------------------------- */
170 static GDBusAuthMechanismState
171 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
173 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
178 return m->priv->state;
182 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
183 const gchar *initial_response,
184 gsize initial_response_len)
186 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
191 if (initial_response != NULL)
192 g_debug ("ANONYMOUS: initial_response was `%s'", initial_response);
194 m->priv->is_server = TRUE;
195 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
199 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
203 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
209 /* can never end up here because we are never in the WAITING_FOR_DATA state */
210 g_assert_not_reached ();
214 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
217 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
223 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
224 g_assert_not_reached ();
230 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
232 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
238 /* can never end up here because we are never in the REJECTED state */
239 g_assert_not_reached ();
245 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
247 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
252 m->priv->is_server = FALSE;
255 /* ---------------------------------------------------------------------------------------------------- */
257 static GDBusAuthMechanismState
258 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
260 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
265 return m->priv->state;
269 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
270 gsize *out_initial_response_len)
272 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
277 m->priv->is_client = TRUE;
278 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
280 *out_initial_response_len = -1;
282 /* just return our library name and version */
283 return g_strdup ("GDBus 0.1");
287 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
291 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
297 /* can never end up here because we are never in the WAITING_FOR_DATA state */
298 g_assert_not_reached ();
302 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
305 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
311 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
312 g_assert_not_reached ();
318 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
320 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
325 m->priv->is_client = FALSE;
328 /* ---------------------------------------------------------------------------------------------------- */
330 #define __G_DBUS_AUTH_MECHANISM_ANON_C__
331 #include "gioaliasdef.c"