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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
23 #include "gdbusauthmechanismanon.h"
24 #include "gdbuserror.h"
25 #include "gioenumtypes.h"
29 struct _GDBusAuthMechanismAnonPrivate
33 GDBusAuthMechanismState state
;
36 static gint
mechanism_get_priority (void);
37 static const gchar
*mechanism_get_name (void);
39 static gboolean
mechanism_is_supported (GDBusAuthMechanism
*mechanism
);
40 static gchar
*mechanism_encode_data (GDBusAuthMechanism
*mechanism
,
44 static gchar
*mechanism_decode_data (GDBusAuthMechanism
*mechanism
,
48 static GDBusAuthMechanismState
mechanism_server_get_state (GDBusAuthMechanism
*mechanism
);
49 static void mechanism_server_initiate (GDBusAuthMechanism
*mechanism
,
50 const gchar
*initial_response
,
51 gsize initial_response_len
);
52 static void mechanism_server_data_receive (GDBusAuthMechanism
*mechanism
,
55 static gchar
*mechanism_server_data_send (GDBusAuthMechanism
*mechanism
,
57 static gchar
*mechanism_server_get_reject_reason (GDBusAuthMechanism
*mechanism
);
58 static void mechanism_server_shutdown (GDBusAuthMechanism
*mechanism
);
59 static GDBusAuthMechanismState
mechanism_client_get_state (GDBusAuthMechanism
*mechanism
);
60 static gchar
*mechanism_client_initiate (GDBusAuthMechanism
*mechanism
,
61 gsize
*out_initial_response_len
);
62 static void mechanism_client_data_receive (GDBusAuthMechanism
*mechanism
,
65 static gchar
*mechanism_client_data_send (GDBusAuthMechanism
*mechanism
,
67 static void mechanism_client_shutdown (GDBusAuthMechanism
*mechanism
);
69 /* ---------------------------------------------------------------------------------------------------- */
71 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismAnon
, _g_dbus_auth_mechanism_anon
, G_TYPE_DBUS_AUTH_MECHANISM
)
73 /* ---------------------------------------------------------------------------------------------------- */
76 _g_dbus_auth_mechanism_anon_finalize (GObject
*object
)
78 //GDBusAuthMechanismAnon *mechanism = G_DBUS_AUTH_MECHANISM_ANON (object);
80 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class
)->finalize
!= NULL
)
81 G_OBJECT_CLASS (_g_dbus_auth_mechanism_anon_parent_class
)->finalize (object
);
85 _g_dbus_auth_mechanism_anon_class_init (GDBusAuthMechanismAnonClass
*klass
)
87 GObjectClass
*gobject_class
;
88 GDBusAuthMechanismClass
*mechanism_class
;
90 gobject_class
= G_OBJECT_CLASS (klass
);
91 gobject_class
->finalize
= _g_dbus_auth_mechanism_anon_finalize
;
93 mechanism_class
= G_DBUS_AUTH_MECHANISM_CLASS (klass
);
94 mechanism_class
->get_priority
= mechanism_get_priority
;
95 mechanism_class
->get_name
= mechanism_get_name
;
96 mechanism_class
->is_supported
= mechanism_is_supported
;
97 mechanism_class
->encode_data
= mechanism_encode_data
;
98 mechanism_class
->decode_data
= mechanism_decode_data
;
99 mechanism_class
->server_get_state
= mechanism_server_get_state
;
100 mechanism_class
->server_initiate
= mechanism_server_initiate
;
101 mechanism_class
->server_data_receive
= mechanism_server_data_receive
;
102 mechanism_class
->server_data_send
= mechanism_server_data_send
;
103 mechanism_class
->server_get_reject_reason
= mechanism_server_get_reject_reason
;
104 mechanism_class
->server_shutdown
= mechanism_server_shutdown
;
105 mechanism_class
->client_get_state
= mechanism_client_get_state
;
106 mechanism_class
->client_initiate
= mechanism_client_initiate
;
107 mechanism_class
->client_data_receive
= mechanism_client_data_receive
;
108 mechanism_class
->client_data_send
= mechanism_client_data_send
;
109 mechanism_class
->client_shutdown
= mechanism_client_shutdown
;
113 _g_dbus_auth_mechanism_anon_init (GDBusAuthMechanismAnon
*mechanism
)
115 mechanism
->priv
= _g_dbus_auth_mechanism_anon_get_instance_private (mechanism
);
118 /* ---------------------------------------------------------------------------------------------------- */
122 mechanism_get_priority (void)
124 /* We prefer ANONYMOUS to most other mechanism (such as DBUS_COOKIE_SHA1) but not to EXTERNAL */
130 mechanism_get_name (void)
136 mechanism_is_supported (GDBusAuthMechanism
*mechanism
)
138 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), FALSE
);
143 mechanism_encode_data (GDBusAuthMechanism
*mechanism
,
153 mechanism_decode_data (GDBusAuthMechanism
*mechanism
,
161 /* ---------------------------------------------------------------------------------------------------- */
163 static GDBusAuthMechanismState
164 mechanism_server_get_state (GDBusAuthMechanism
*mechanism
)
166 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
168 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), G_DBUS_AUTH_MECHANISM_STATE_INVALID
);
169 g_return_val_if_fail (m
->priv
->is_server
&& !m
->priv
->is_client
, G_DBUS_AUTH_MECHANISM_STATE_INVALID
);
171 return m
->priv
->state
;
175 mechanism_server_initiate (GDBusAuthMechanism
*mechanism
,
176 const gchar
*initial_response
,
177 gsize initial_response_len
)
179 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
181 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
));
182 g_return_if_fail (!m
->priv
->is_server
&& !m
->priv
->is_client
);
184 //g_debug ("ANONYMOUS: initial_response was '%s'", initial_response);
186 m
->priv
->is_server
= TRUE
;
187 m
->priv
->state
= G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED
;
191 mechanism_server_data_receive (GDBusAuthMechanism
*mechanism
,
195 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
197 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
));
198 g_return_if_fail (m
->priv
->is_server
&& !m
->priv
->is_client
);
199 g_return_if_fail (m
->priv
->state
== G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
);
201 /* can never end up here because we are never in the WAITING_FOR_DATA state */
202 g_assert_not_reached ();
206 mechanism_server_data_send (GDBusAuthMechanism
*mechanism
,
209 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
211 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), NULL
);
212 g_return_val_if_fail (m
->priv
->is_server
&& !m
->priv
->is_client
, NULL
);
213 g_return_val_if_fail (m
->priv
->state
== G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
, NULL
);
215 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
216 g_assert_not_reached ();
222 mechanism_server_get_reject_reason (GDBusAuthMechanism
*mechanism
)
224 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
226 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), NULL
);
227 g_return_val_if_fail (m
->priv
->is_server
&& !m
->priv
->is_client
, NULL
);
228 g_return_val_if_fail (m
->priv
->state
== G_DBUS_AUTH_MECHANISM_STATE_REJECTED
, NULL
);
230 /* can never end up here because we are never in the REJECTED state */
231 g_assert_not_reached ();
237 mechanism_server_shutdown (GDBusAuthMechanism
*mechanism
)
239 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
241 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
));
242 g_return_if_fail (m
->priv
->is_server
&& !m
->priv
->is_client
);
244 m
->priv
->is_server
= FALSE
;
247 /* ---------------------------------------------------------------------------------------------------- */
249 static GDBusAuthMechanismState
250 mechanism_client_get_state (GDBusAuthMechanism
*mechanism
)
252 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
254 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), G_DBUS_AUTH_MECHANISM_STATE_INVALID
);
255 g_return_val_if_fail (m
->priv
->is_client
&& !m
->priv
->is_server
, G_DBUS_AUTH_MECHANISM_STATE_INVALID
);
257 return m
->priv
->state
;
261 mechanism_client_initiate (GDBusAuthMechanism
*mechanism
,
262 gsize
*out_initial_response_len
)
264 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
266 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), NULL
);
267 g_return_val_if_fail (!m
->priv
->is_server
&& !m
->priv
->is_client
, NULL
);
269 m
->priv
->is_client
= TRUE
;
270 m
->priv
->state
= G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED
;
272 *out_initial_response_len
= -1;
274 /* just return our library name and version */
275 return g_strdup ("GDBus 0.1");
279 mechanism_client_data_receive (GDBusAuthMechanism
*mechanism
,
283 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
285 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
));
286 g_return_if_fail (m
->priv
->is_client
&& !m
->priv
->is_server
);
287 g_return_if_fail (m
->priv
->state
== G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
);
289 /* can never end up here because we are never in the WAITING_FOR_DATA state */
290 g_assert_not_reached ();
294 mechanism_client_data_send (GDBusAuthMechanism
*mechanism
,
297 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
299 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
), NULL
);
300 g_return_val_if_fail (m
->priv
->is_client
&& !m
->priv
->is_server
, NULL
);
301 g_return_val_if_fail (m
->priv
->state
== G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
, NULL
);
303 /* can never end up here because we are never in the HAVE_DATA_TO_SEND state */
304 g_assert_not_reached ();
310 mechanism_client_shutdown (GDBusAuthMechanism
*mechanism
)
312 GDBusAuthMechanismAnon
*m
= G_DBUS_AUTH_MECHANISM_ANON (mechanism
);
314 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_ANON (mechanism
));
315 g_return_if_fail (m
->priv
->is_client
&& !m
->priv
->is_server
);
317 m
->priv
->is_client
= FALSE
;
320 /* ---------------------------------------------------------------------------------------------------- */