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