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