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"
31 struct _GDBusAuthMechanismAnonPrivate
35 GDBusAuthMechanismState state;
38 static gint mechanism_get_priority (void);
39 static const gchar *mechanism_get_name (void);
41 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
42 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
46 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
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,
57 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
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,
67 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
69 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
71 /* ---------------------------------------------------------------------------------------------------- */
73 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismAnon, _g_dbus_auth_mechanism_anon, G_TYPE_DBUS_AUTH_MECHANISM)
75 /* ---------------------------------------------------------------------------------------------------- */
78 _g_dbus_auth_mechanism_anon_finalize (GObject *object)
80 //GDBusAuthMechanismAnon *mechanism = G_DBUS_AUTH_MECHANISM_ANON (object);
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);
87 _g_dbus_auth_mechanism_anon_class_init (GDBusAuthMechanismAnonClass *klass)
89 GObjectClass *gobject_class;
90 GDBusAuthMechanismClass *mechanism_class;
92 gobject_class = G_OBJECT_CLASS (klass);
93 gobject_class->finalize = _g_dbus_auth_mechanism_anon_finalize;
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;
115 _g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon *mechanism)
117 mechanism->priv = _g_dbus_auth_mechanism_anon_get_instance_private (mechanism);
120 /* ---------------------------------------------------------------------------------------------------- */
124 mechanism_get_priority (void)
126 /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
132 mechanism_get_name (void)
138 mechanism_is_supported (GDBusAuthMechanism *mechanism)
140 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism), FALSE);
145 mechanism_encode_data (GDBusAuthMechanism *mechanism,
155 mechanism_decode_data (GDBusAuthMechanism *mechanism,
163 /* ---------------------------------------------------------------------------------------------------- */
165 static GDBusAuthMechanismState
166 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
168 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
173 return m->priv->state;
177 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
178 const gchar *initial_response,
179 gsize initial_response_len)
181 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
186 //g_debug ("ANONYMOUS: initial_response was '%s'", initial_response);
188 m->priv->is_server = TRUE;
189 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
193 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
197 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
203 /* can never end up here because we are never in the WAITING_FOR_DATA state */
204 g_assert_not_reached ();
208 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
211 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
217 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
218 g_assert_not_reached ();
224 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
226 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
232 /* can never end up here because we are never in the REJECTED state */
233 g_assert_not_reached ();
239 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
241 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
246 m->priv->is_server = FALSE;
249 /* ---------------------------------------------------------------------------------------------------- */
251 static GDBusAuthMechanismState
252 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
254 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
259 return m->priv->state;
263 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
264 gsize *out_initial_response_len)
266 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
271 m->priv->is_client = TRUE;
272 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
274 *out_initial_response_len = -1;
276 /* just return our library name and version */
277 return g_strdup ("GDBus 0.1");
281 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
285 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
291 /* can never end up here because we are never in the WAITING_FOR_DATA state */
292 g_assert_not_reached ();
296 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
299 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
305 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
306 g_assert_not_reached ();
312 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
314 GDBusAuthMechanismAnon *m = G_DBUS_AUTH_MECHANISM_ANON (mechanism);
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);
319 m->priv->is_client = FALSE;
322 /* ---------------------------------------------------------------------------------------------------- */