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", NULL, NULL,
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", NULL, NULL,
146 G_TYPE_CREDENTIALS,
147 G_PARAM_READABLE |
148 G_PARAM_WRITABLE |
149 G_PARAM_CONSTRUCT_ONLY |
150 G_PARAM_STATIC_NAME |
151 G_PARAM_STATIC_BLURB |
152 G_PARAM_STATIC_NICK));
153 }
154
155 static void
156 _g_dbus_auth_mechanism_init (GDBusAuthMechanism *mechanism)
157 {
158 mechanism->priv = _g_dbus_auth_mechanism_get_instance_private (mechanism);
159 }
160
161 /* ---------------------------------------------------------------------------------------------------- */
162
163 GIOStream *
164 _g_dbus_auth_mechanism_get_stream (GDBusAuthMechanism *mechanism)
165 {
166 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
167 return mechanism->priv->stream;
168 }
169
170 GCredentials *
171 _g_dbus_auth_mechanism_get_credentials (GDBusAuthMechanism *mechanism)
172 {
173 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
174 return mechanism->priv->credentials;
175 }
176
177 /* ---------------------------------------------------------------------------------------------------- */
178
179 const gchar *
180 _g_dbus_auth_mechanism_get_name (GType mechanism_type)
181 {
182 const gchar *name;
183 GDBusAuthMechanismClass *klass;
184
185 g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), NULL);
186
187 klass = g_type_class_ref (mechanism_type);
188 g_assert (klass != NULL);
189 name = klass->get_name ();
190 //g_type_class_unref (klass);
191
192 return name;
193 }
194
195 gint
196 _g_dbus_auth_mechanism_get_priority (GType mechanism_type)
197 {
198 gint priority;
199 GDBusAuthMechanismClass *klass;
200
201 g_return_val_if_fail (g_type_is_a (mechanism_type, G_TYPE_DBUS_AUTH_MECHANISM), 0);
202
203 klass = g_type_class_ref (mechanism_type);
204 g_assert (klass != NULL);
205 priority = klass->get_priority ();
206 //g_type_class_unref (klass);
207
208 return priority;
209 }
210
211 /* ---------------------------------------------------------------------------------------------------- */
212
213 gboolean
214 _g_dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
215 {
216 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), FALSE);
217 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
218 }
219
220 gchar *
221 _g_dbus_auth_mechanism_encode_data (GDBusAuthMechanism *mechanism,
222 const gchar *data,
223 gsize data_len,
224 gsize *out_data_len)
225 {
226 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
227 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->encode_data (mechanism, data, data_len, out_data_len);
228 }
229
230
231 gchar *
232 _g_dbus_auth_mechanism_decode_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)->decode_data (mechanism, data, data_len, out_data_len);
239 }
240
241 /* ---------------------------------------------------------------------------------------------------- */
242
243 GDBusAuthMechanismState
244 _g_dbus_auth_mechanism_server_get_state (GDBusAuthMechanism *mechanism)
245 {
246 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
247 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_state (mechanism);
248 }
249
250 void
251 _g_dbus_auth_mechanism_server_initiate (GDBusAuthMechanism *mechanism,
252 const gchar *initial_response,
253 gsize initial_response_len)
254 {
255 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
256 G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_initiate (mechanism, initial_response, initial_response_len);
257 }
258
259 void
260 _g_dbus_auth_mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
261 const gchar *data,
262 gsize data_len)
263 {
264 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
265 G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_receive (mechanism, data, data_len);
266 }
267
268 gchar *
269 _g_dbus_auth_mechanism_server_data_send (GDBusAuthMechanism *mechanism,
270 gsize *out_data_len)
271 {
272 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
273 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_data_send (mechanism, out_data_len);
274 }
275
276 gchar *
277 _g_dbus_auth_mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
278 {
279 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
280 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_get_reject_reason (mechanism);
281 }
282
283 void
284 _g_dbus_auth_mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
285 {
286 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism));
287 G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->server_shutdown (mechanism);
288 }
289
290 /* ---------------------------------------------------------------------------------------------------- */
291
292 GDBusAuthMechanismState
293 _g_dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
294 {
295 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
296 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
297 }
298
299 gchar *
300 _g_dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
301 GDBusConnectionFlags conn_flags,
302 gsize *out_initial_response_len)
303 {
304 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM (mechanism), NULL);
305 return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
306 conn_flags,
307 out_initial_response_len);
308 }
309
310 void
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 *
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
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 /* ---------------------------------------------------------------------------------------------------- */