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.1 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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20 
21 #include "config.h"
22 
23 #include "gdbusauthmechanism.h"
24 #include "gcredentials.h"
25 #include "gdbuserror.h"
26 #include "gioenumtypes.h"
27 #include "giostream.h"
28 
29 #include "glibintl.h"
30 
31 /* ---------------------------------------------------------------------------------------------------- */
32 
33 struct _GDBusAuthMechanismPrivate
34 {
35   GIOStream *stream;
36   GCredentials *credentials;
37 };
38 
39 enum
40 {
41   PROP_0,
42   PROP_STREAM,
43   PROP_CREDENTIALS
44 };
45 
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GDBusAuthMechanism,_g_dbus_auth_mechanism,G_TYPE_OBJECT)46 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GDBusAuthMechanism, _g_dbus_auth_mechanism, G_TYPE_OBJECT)
47 
48 /* ---------------------------------------------------------------------------------------------------- */
49 
50 static void
51 _g_dbus_auth_mechanism_finalize (GObject *object)
52 {
53   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
54 
55   if (mechanism->priv->stream != NULL)
56     g_object_unref (mechanism->priv->stream);
57   if (mechanism->priv->credentials != NULL)
58     g_object_unref (mechanism->priv->credentials);
59 
60   G_OBJECT_CLASS (_g_dbus_auth_mechanism_parent_class)->finalize (object);
61 }
62 
63 static void
_g_dbus_auth_mechanism_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)64 _g_dbus_auth_mechanism_get_property (GObject    *object,
65                                      guint       prop_id,
66                                      GValue     *value,
67                                      GParamSpec *pspec)
68 {
69   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
70 
71   switch (prop_id)
72     {
73     case PROP_STREAM:
74       g_value_set_object (value, mechanism->priv->stream);
75       break;
76 
77     case PROP_CREDENTIALS:
78       g_value_set_object (value, mechanism->priv->credentials);
79       break;
80 
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83       break;
84     }
85 }
86 
87 static void
_g_dbus_auth_mechanism_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)88 _g_dbus_auth_mechanism_set_property (GObject      *object,
89                                      guint         prop_id,
90                                      const GValue *value,
91                                      GParamSpec   *pspec)
92 {
93   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
94 
95   switch (prop_id)
96     {
97     case PROP_STREAM:
98       mechanism->priv->stream = g_value_dup_object (value);
99       break;
100 
101     case PROP_CREDENTIALS:
102       mechanism->priv->credentials = g_value_dup_object (value);
103       break;
104 
105     default:
106       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107       break;
108     }
109 }
110 
111 static void
_g_dbus_auth_mechanism_class_init(GDBusAuthMechanismClass * klass)112 _g_dbus_auth_mechanism_class_init (GDBusAuthMechanismClass *klass)
113 {
114   GObjectClass *gobject_class;
115 
116   gobject_class = G_OBJECT_CLASS (klass);
117   gobject_class->get_property = _g_dbus_auth_mechanism_get_property;
118   gobject_class->set_property = _g_dbus_auth_mechanism_set_property;
119   gobject_class->finalize     = _g_dbus_auth_mechanism_finalize;
120 
121   g_object_class_install_property (gobject_class,
122                                    PROP_STREAM,
123                                    g_param_spec_object ("stream",
124                                                         P_("IO Stream"),
125                                                         P_("The underlying GIOStream used for I/O"),
126                                                         G_TYPE_IO_STREAM,
127                                                         G_PARAM_READABLE |
128                                                         G_PARAM_WRITABLE |
129                                                         G_PARAM_CONSTRUCT_ONLY |
130                                                         G_PARAM_STATIC_NAME |
131                                                         G_PARAM_STATIC_BLURB |
132                                                         G_PARAM_STATIC_NICK));
133 
134   /**
135    * GDBusAuthMechanism:credentials:
136    *
137    * If authenticating as a server, this property contains the
138    * received credentials, if any.
139    *
140    * If authenticating as a client, the property contains the
141    * credentials that were sent, if any.
142    */
143   g_object_class_install_property (gobject_class,
144                                    PROP_CREDENTIALS,
145                                    g_param_spec_object ("credentials",
146                                                         P_("Credentials"),
147                                                         P_("The credentials of the remote peer"),
148                                                         G_TYPE_CREDENTIALS,
149                                                         G_PARAM_READABLE |
150                                                         G_PARAM_WRITABLE |
151                                                         G_PARAM_CONSTRUCT_ONLY |
152                                                         G_PARAM_STATIC_NAME |
153                                                         G_PARAM_STATIC_BLURB |
154                                                         G_PARAM_STATIC_NICK));
155 }
156 
157 static void
_g_dbus_auth_mechanism_init(GDBusAuthMechanism * mechanism)158 _g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
159 {
160   mechanism->priv = _g_dbus_auth_mechanism_get_instance_private (mechanism);
161 }
162 
163 /* ---------------------------------------------------------------------------------------------------- */
164 
165 GIOStream *
_g_dbus_auth_mechanism_get_stream(GDBusAuthMechanism * mechanism)166 _g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
167 {
168   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
169   return mechanism->priv->stream;
170 }
171 
172 GCredentials *
_g_dbus_auth_mechanism_get_credentials(GDBusAuthMechanism * mechanism)173 _g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
174 {
175   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
176   return mechanism->priv->credentials;
177 }
178 
179 /* ---------------------------------------------------------------------------------------------------- */
180 
181 const gchar *
_g_dbus_auth_mechanism_get_name(GType mechanism_type)182 _g_dbus_auth_mechanism_get_name (GType mechanism_type)
183 {
184   const gchar *name;
185   GDBusAuthMechanismClass *klass;
186 
187   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
188 
189   klass = g_type_class_ref (mechanism_type);
190   g_assert (klass != NULL);
191   name = klass->get_name ();
192   //g_type_class_unref (klass);
193 
194   return name;
195 }
196 
197 gint
_g_dbus_auth_mechanism_get_priority(GType mechanism_type)198 _g_dbus_auth_mechanism_get_priority (GType mechanism_type)
199 {
200   gint priority;
201   GDBusAuthMechanismClass *klass;
202 
203   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
204 
205   klass = g_type_class_ref (mechanism_type);
206   g_assert (klass != NULL);
207   priority = klass->get_priority ();
208   //g_type_class_unref (klass);
209 
210   return priority;
211 }
212 
213 /* ---------------------------------------------------------------------------------------------------- */
214 
215 gboolean
_g_dbus_auth_mechanism_is_supported(GDBusAuthMechanism * mechanism)216 _g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
217 {
218   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
219   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
220 }
221 
222 gchar *
_g_dbus_auth_mechanism_encode_data(GDBusAuthMechanism * mechanism,const gchar * data,gsize data_len,gsize * out_data_len)223 _g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
224                                     const gchar        *data,
225                                     gsize               data_len,
226                                     gsize              *out_data_len)
227 {
228   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
229   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
230 }
231 
232 
233 gchar *
_g_dbus_auth_mechanism_decode_data(GDBusAuthMechanism * mechanism,const gchar * data,gsize data_len,gsize * out_data_len)234 _g_dbus_auth_mechanism_decode_data (GDBusAuthMechanism *mechanism,
235                                     const gchar        *data,
236                                     gsize               data_len,
237                                     gsize              *out_data_len)
238 {
239   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
240   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->decode_data (mechanism, data, data_len, out_data_len);
241 }
242 
243 /* ---------------------------------------------------------------------------------------------------- */
244 
245 GDBusAuthMechanismState
_g_dbus_auth_mechanism_server_get_state(GDBusAuthMechanism * mechanism)246 _g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
247 {
248   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
249   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
250 }
251 
252 void
_g_dbus_auth_mechanism_server_initiate(GDBusAuthMechanism * mechanism,const gchar * initial_response,gsize initial_response_len)253 _g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
254                                         const gchar        *initial_response,
255                                         gsize               initial_response_len)
256 {
257   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
258   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
259 }
260 
261 void
_g_dbus_auth_mechanism_server_data_receive(GDBusAuthMechanism * mechanism,const gchar * data,gsize data_len)262 _g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
263                                             const gchar        *data,
264                                             gsize               data_len)
265 {
266   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
267   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
268 }
269 
270 gchar *
_g_dbus_auth_mechanism_server_data_send(GDBusAuthMechanism * mechanism,gsize * out_data_len)271 _g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
272                                          gsize              *out_data_len)
273 {
274   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
275   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
276 }
277 
278 gchar *
_g_dbus_auth_mechanism_server_get_reject_reason(GDBusAuthMechanism * mechanism)279 _g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
280 {
281   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
282   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
283 }
284 
285 void
_g_dbus_auth_mechanism_server_shutdown(GDBusAuthMechanism * mechanism)286 _g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
287 {
288   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
289   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
290 }
291 
292 /* ---------------------------------------------------------------------------------------------------- */
293 
294 GDBusAuthMechanismState
_g_dbus_auth_mechanism_client_get_state(GDBusAuthMechanism * mechanism)295 _g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
296 {
297   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
298   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
299 }
300 
301 gchar *
_g_dbus_auth_mechanism_client_initiate(GDBusAuthMechanism * mechanism,gsize * out_initial_response_len)302 _g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
303                                         gsize              *out_initial_response_len)
304 {
305   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
306   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
307                                                                        out_initial_response_len);
308 }
309 
310 void
_g_dbus_auth_mechanism_client_data_receive(GDBusAuthMechanism * mechanism,const gchar * data,gsize data_len)311 _g_dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
312                                             const gchar        *data,
313                                             gsize               data_len)
314 {
315   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
316   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
317 }
318 
319 gchar *
_g_dbus_auth_mechanism_client_data_send(GDBusAuthMechanism * mechanism,gsize * out_data_len)320 _g_dbus_auth_mechanism_client_data_send (GDBusAuthMechanism *mechanism,
321                                          gsize              *out_data_len)
322 {
323   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
324   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_send (mechanism, out_data_len);
325 }
326 
327 void
_g_dbus_auth_mechanism_client_shutdown(GDBusAuthMechanism * mechanism)328 _g_dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
329 {
330   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
331   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
332 }
333 
334 /* ---------------------------------------------------------------------------------------------------- */
335