Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / gio / gdbusauthmechanism.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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
33 /* ---------------------------------------------------------------------------------------------------- */
34
35 struct _GDBusAuthMechanismPrivate
36 {
37   GIOStream *stream;
38   GCredentials *credentials;
39 };
40
41 enum
42 {
43   PROP_0,
44   PROP_STREAM,
45   PROP_CREDENTIALS
46 };
47
48 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GDBusAuthMechanism, _g_dbus_auth_mechanism, G_TYPE_OBJECT)
49
50 /* ---------------------------------------------------------------------------------------------------- */
51
52 static void
53 _g_dbus_auth_mechanism_finalize (GObject *object)
54 {
55   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
56
57   if (mechanism->priv->stream != NULL)
58     g_object_unref (mechanism->priv->stream);
59   if (mechanism->priv->credentials != NULL)
60     g_object_unref (mechanism->priv->credentials);
61
62   G_OBJECT_CLASS (_g_dbus_auth_mechanism_parent_class)->finalize (object);
63 }
64
65 static void
66 _g_dbus_auth_mechanism_get_property (GObject    *object,
67                                      guint       prop_id,
68                                      GValue     *value,
69                                      GParamSpec *pspec)
70 {
71   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
72
73   switch (prop_id)
74     {
75     case PROP_STREAM:
76       g_value_set_object (value, mechanism->priv->stream);
77       break;
78
79     case PROP_CREDENTIALS:
80       g_value_set_object (value, mechanism->priv->credentials);
81       break;
82
83     default:
84       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85       break;
86     }
87 }
88
89 static void
90 _g_dbus_auth_mechanism_set_property (GObject      *object,
91                                      guint         prop_id,
92                                      const GValue *value,
93                                      GParamSpec   *pspec)
94 {
95   GDBusAuthMechanism *mechanism = G_DBUS_AUTH_MECHANISM (object);
96
97   switch (prop_id)
98     {
99     case PROP_STREAM:
100       mechanism->priv->stream = g_value_dup_object (value);
101       break;
102
103     case PROP_CREDENTIALS:
104       mechanism->priv->credentials = g_value_dup_object (value);
105       break;
106
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109       break;
110     }
111 }
112
113 static void
114 _g_dbus_auth_mechanism_class_init (GDBusAuthMechanismClass *klass)
115 {
116   GObjectClass *gobject_class;
117
118   gobject_class = G_OBJECT_CLASS (klass);
119   gobject_class->get_property = _g_dbus_auth_mechanism_get_property;
120   gobject_class->set_property = _g_dbus_auth_mechanism_set_property;
121   gobject_class->finalize     = _g_dbus_auth_mechanism_finalize;
122
123   g_object_class_install_property (gobject_class,
124                                    PROP_STREAM,
125                                    g_param_spec_object ("stream",
126                                                         P_("IO Stream"),
127                                                         P_("The underlying GIOStream used for I/O"),
128                                                         G_TYPE_IO_STREAM,
129                                                         G_PARAM_READABLE |
130                                                         G_PARAM_WRITABLE |
131                                                         G_PARAM_CONSTRUCT_ONLY |
132                                                         G_PARAM_STATIC_NAME |
133                                                         G_PARAM_STATIC_BLURB |
134                                                         G_PARAM_STATIC_NICK));
135
136   /**
137    * GDBusAuthMechanism:credentials:
138    *
139    * If authenticating as a server, this property contains the
140    * received credentials, if any.
141    *
142    * If authenticating as a client, the property contains the
143    * credentials that were sent, if any.
144    */
145   g_object_class_install_property (gobject_class,
146                                    PROP_CREDENTIALS,
147                                    g_param_spec_object ("credentials",
148                                                         P_("Credentials"),
149                                                         P_("The credentials of the remote peer"),
150                                                         G_TYPE_CREDENTIALS,
151                                                         G_PARAM_READABLE |
152                                                         G_PARAM_WRITABLE |
153                                                         G_PARAM_CONSTRUCT_ONLY |
154                                                         G_PARAM_STATIC_NAME |
155                                                         G_PARAM_STATIC_BLURB |
156                                                         G_PARAM_STATIC_NICK));
157 }
158
159 static void
160 _g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
161 {
162   mechanism->priv = _g_dbus_auth_mechanism_get_instance_private (mechanism);
163 }
164
165 /* ---------------------------------------------------------------------------------------------------- */
166
167 GIOStream *
168 _g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
169 {
170   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
171   return mechanism->priv->stream;
172 }
173
174 GCredentials *
175 _g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
176 {
177   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
178   return mechanism->priv->credentials;
179 }
180
181 /* ---------------------------------------------------------------------------------------------------- */
182
183 const gchar *
184 _g_dbus_auth_mechanism_get_name (GType mechanism_type)
185 {
186   const gchar *name;
187   GDBusAuthMechanismClass *klass;
188
189   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
190
191   klass = g_type_class_ref (mechanism_type);
192   g_assert (klass != NULL);
193   name = klass->get_name ();
194   //g_type_class_unref (klass);
195
196   return name;
197 }
198
199 gint
200 _g_dbus_auth_mechanism_get_priority (GType mechanism_type)
201 {
202   gint priority;
203   GDBusAuthMechanismClass *klass;
204
205   g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
206
207   klass = g_type_class_ref (mechanism_type);
208   g_assert (klass != NULL);
209   priority = klass->get_priority ();
210   //g_type_class_unref (klass);
211
212   return priority;
213 }
214
215 /* ---------------------------------------------------------------------------------------------------- */
216
217 gboolean
218 _g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
219 {
220   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
221   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
222 }
223
224 gchar *
225 _g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
226                                     const gchar        *data,
227                                     gsize               data_len,
228                                     gsize              *out_data_len)
229 {
230   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
231   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
232 }
233
234
235 gchar *
236 _g_dbus_auth_mechanism_decode_data (GDBusAuthMechanism *mechanism,
237                                     const gchar        *data,
238                                     gsize               data_len,
239                                     gsize              *out_data_len)
240 {
241   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
242   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->decode_data (mechanism, data, data_len, out_data_len);
243 }
244
245 /* ---------------------------------------------------------------------------------------------------- */
246
247 GDBusAuthMechanismState
248 _g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
249 {
250   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
251   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
252 }
253
254 void
255 _g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
256                                         const gchar        *initial_response,
257                                         gsize               initial_response_len)
258 {
259   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
260   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
261 }
262
263 void
264 _g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
265                                             const gchar        *data,
266                                             gsize               data_len)
267 {
268   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
269   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
270 }
271
272 gchar *
273 _g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
274                                          gsize              *out_data_len)
275 {
276   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
277   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
278 }
279
280 gchar *
281 _g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
282 {
283   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
284   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
285 }
286
287 void
288 _g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
289 {
290   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
291   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
292 }
293
294 /* ---------------------------------------------------------------------------------------------------- */
295
296 GDBusAuthMechanismState
297 _g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
298 {
299   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
300   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
301 }
302
303 gchar *
304 _g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism   *mechanism,
305                                         GDBusConnectionFlags  conn_flags,
306                                         gsize                *out_initial_response_len)
307 {
308   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
309   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
310                                                                        conn_flags,
311                                                                        out_initial_response_len);
312 }
313
314 void
315 _g_dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
316                                             const gchar        *data,
317                                             gsize               data_len)
318 {
319   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
320   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
321 }
322
323 gchar *
324 _g_dbus_auth_mechanism_client_data_send (GDBusAuthMechanism *mechanism,
325                                          gsize              *out_data_len)
326 {
327   g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
328   return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_send (mechanism, out_data_len);
329 }
330
331 void
332 _g_dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
333 {
334   g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
335   G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
336 }
337
338 /* ---------------------------------------------------------------------------------------------------- */