Branch data Line data Source code
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 "gdbusauth.h"
26 : :
27 : : #include "gdbusauthmechanismanon.h"
28 : : #include "gdbusauthmechanismexternal.h"
29 : : #include "gdbusauthmechanismsha1.h"
30 : : #include "gdbusauthobserver.h"
31 : :
32 : : #include "gdbuserror.h"
33 : : #include "gdbusutils.h"
34 : : #include "gioenumtypes.h"
35 : : #include "gcredentials.h"
36 : : #include "gcredentialsprivate.h"
37 : : #include "gdbusprivate.h"
38 : : #include "giostream.h"
39 : : #include "gdatainputstream.h"
40 : : #include "gdataoutputstream.h"
41 : :
42 : : #include "gnetworking.h"
43 : : #include "gunixconnection.h"
44 : : #include "gunixcredentialsmessage.h"
45 : :
46 : : #include "glibintl.h"
47 : :
48 : : G_GNUC_PRINTF(1, 2)
49 : : static void
50 : 38498 : debug_print (const gchar *message, ...)
51 : : {
52 : 38498 : if (G_UNLIKELY (_g_dbus_debug_authentication ()))
53 : : {
54 : : gchar *s;
55 : : GString *str;
56 : : va_list var_args;
57 : : guint n;
58 : :
59 : 0 : _g_dbus_debug_print_lock ();
60 : :
61 : 0 : va_start (var_args, message);
62 : 0 : s = g_strdup_vprintf (message, var_args);
63 : 0 : va_end (var_args);
64 : :
65 : 0 : str = g_string_new (NULL);
66 : 0 : for (n = 0; s[n] != '\0'; n++)
67 : : {
68 : 0 : if (G_UNLIKELY (s[n] == '\r'))
69 : 0 : g_string_append (str, "\\r");
70 : 0 : else if (G_UNLIKELY (s[n] == '\n'))
71 : 0 : g_string_append (str, "\\n");
72 : : else
73 : 0 : g_string_append_c (str, s[n]);
74 : 0 : }
75 : 0 : g_print ("GDBus-debug:Auth: %s\n", str->str);
76 : 0 : g_string_free (str, TRUE);
77 : 0 : g_free (s);
78 : :
79 : 0 : _g_dbus_debug_print_unlock ();
80 : 0 : }
81 : 38498 : }
82 : :
83 : : typedef struct
84 : : {
85 : : const gchar *name;
86 : : gint priority;
87 : : GType gtype;
88 : : } Mechanism;
89 : :
90 : : static void mechanism_free (Mechanism *m);
91 : :
92 : : struct _GDBusAuthPrivate
93 : : {
94 : : GIOStream *stream;
95 : :
96 : : /* A list of available Mechanism, sorted according to priority */
97 : : GList *available_mechanisms;
98 : : };
99 : :
100 : : enum
101 : : {
102 : : PROP_0,
103 : : PROP_STREAM
104 : : };
105 : :
106 : 9770 : G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT)
107 : :
108 : : /* ---------------------------------------------------------------------------------------------------- */
109 : :
110 : : static void
111 : 2189 : _g_dbus_auth_finalize (GObject *object)
112 : : {
113 : 2189 : GDBusAuth *auth = G_DBUS_AUTH (object);
114 : :
115 : 2189 : if (auth->priv->stream != NULL)
116 : 2189 : g_object_unref (auth->priv->stream);
117 : 2189 : g_list_free_full (auth->priv->available_mechanisms, (GDestroyNotify) mechanism_free);
118 : :
119 : 2189 : if (G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize != NULL)
120 : 2189 : G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize (object);
121 : 2189 : }
122 : :
123 : : static void
124 : 0 : _g_dbus_auth_get_property (GObject *object,
125 : : guint prop_id,
126 : : GValue *value,
127 : : GParamSpec *pspec)
128 : : {
129 : 0 : GDBusAuth *auth = G_DBUS_AUTH (object);
130 : :
131 : 0 : switch (prop_id)
132 : : {
133 : 0 : case PROP_STREAM:
134 : 0 : g_value_set_object (value, auth->priv->stream);
135 : 0 : break;
136 : :
137 : 0 : default:
138 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 : 0 : break;
140 : : }
141 : 0 : }
142 : :
143 : : static void
144 : 2457 : _g_dbus_auth_set_property (GObject *object,
145 : : guint prop_id,
146 : : const GValue *value,
147 : : GParamSpec *pspec)
148 : : {
149 : 2457 : GDBusAuth *auth = G_DBUS_AUTH (object);
150 : :
151 : 2457 : switch (prop_id)
152 : : {
153 : 2424 : case PROP_STREAM:
154 : 2457 : auth->priv->stream = g_value_dup_object (value);
155 : 2457 : break;
156 : :
157 : 0 : default:
158 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 : 0 : break;
160 : : }
161 : 2457 : }
162 : :
163 : : static void
164 : 105 : _g_dbus_auth_class_init (GDBusAuthClass *klass)
165 : : {
166 : : GObjectClass *gobject_class;
167 : :
168 : 105 : gobject_class = G_OBJECT_CLASS (klass);
169 : 105 : gobject_class->get_property = _g_dbus_auth_get_property;
170 : 105 : gobject_class->set_property = _g_dbus_auth_set_property;
171 : 105 : gobject_class->finalize = _g_dbus_auth_finalize;
172 : :
173 : 109 : g_object_class_install_property (gobject_class,
174 : : PROP_STREAM,
175 : 4 : g_param_spec_object ("stream", NULL, NULL,
176 : 4 : G_TYPE_IO_STREAM,
177 : : G_PARAM_READABLE |
178 : : G_PARAM_WRITABLE |
179 : : G_PARAM_CONSTRUCT_ONLY |
180 : : G_PARAM_STATIC_NAME |
181 : : G_PARAM_STATIC_BLURB |
182 : : G_PARAM_STATIC_NICK));
183 : 105 : }
184 : :
185 : : static void
186 : 6557 : mechanism_free (Mechanism *m)
187 : : {
188 : 6557 : g_free (m);
189 : 6557 : }
190 : :
191 : : static void
192 : 7371 : add_mechanism (GDBusAuth *auth,
193 : : GDBusAuthObserver *observer,
194 : : GType mechanism_type)
195 : : {
196 : : const gchar *name;
197 : :
198 : 7371 : name = _g_dbus_auth_mechanism_get_name (mechanism_type);
199 : 7371 : if (observer == NULL || g_dbus_auth_observer_allow_mechanism (observer, name))
200 : : {
201 : : Mechanism *m;
202 : 7105 : m = g_new0 (Mechanism, 1);
203 : 7105 : m->name = name;
204 : 7105 : m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
205 : 7105 : m->gtype = mechanism_type;
206 : 7105 : auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
207 : 95 : }
208 : 7371 : }
209 : :
210 : : static gint
211 : 4648 : mech_compare_func (Mechanism *a, Mechanism *b)
212 : : {
213 : : gint ret;
214 : : /* ensure deterministic order */
215 : 4648 : ret = b->priority - a->priority;
216 : 4648 : if (ret == 0)
217 : 0 : ret = g_strcmp0 (b->name, a->name);
218 : 4648 : return ret;
219 : : }
220 : :
221 : : static void
222 : 2457 : _g_dbus_auth_init (GDBusAuth *auth)
223 : : {
224 : 2457 : auth->priv = _g_dbus_auth_get_instance_private (auth);
225 : 2457 : }
226 : :
227 : : static void
228 : 2457 : _g_dbus_auth_add_mechs (GDBusAuth *auth,
229 : : GDBusAuthObserver *observer)
230 : : {
231 : : /* TODO: trawl extension points */
232 : 2457 : add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
233 : 2457 : add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
234 : 2457 : add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
235 : :
236 : 2457 : auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
237 : : (GCompareFunc) mech_compare_func);
238 : 2457 : }
239 : :
240 : : static GType
241 : 2788 : find_mech_by_name (GDBusAuth *auth,
242 : : const gchar *name)
243 : : {
244 : : GType ret;
245 : : GList *l;
246 : :
247 : 2788 : ret = (GType) 0;
248 : :
249 : 3397 : for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
250 : : {
251 : 3316 : Mechanism *m = l->data;
252 : 3316 : if (g_strcmp0 (name, m->name) == 0)
253 : : {
254 : 2707 : ret = m->gtype;
255 : 2707 : goto out;
256 : : }
257 : 57 : }
258 : :
259 : 81 : out:
260 : 2788 : return ret;
261 : : }
262 : :
263 : : static size_t
264 : 807 : get_longest_mechanism_name_length (GDBusAuth *auth)
265 : : {
266 : 807 : size_t len = 0;
267 : :
268 : 2784 : for (GList *l = auth->priv->available_mechanisms; l != NULL; l = l->next)
269 : : {
270 : 1977 : Mechanism *m = l->data;
271 : :
272 : 1977 : len = MAX (len, strlen (m->name));
273 : 130 : }
274 : :
275 : 807 : return len;
276 : : }
277 : :
278 : : GDBusAuth *
279 : 2457 : _g_dbus_auth_new (GIOStream *stream)
280 : : {
281 : 2490 : return g_object_new (G_TYPE_DBUS_AUTH,
282 : 33 : "stream", stream,
283 : : NULL);
284 : : }
285 : :
286 : : /* Arbitrarily chosen limit on the length of a DATA command payload, to prevent
287 : : * unbounded reads from malicious clients.
288 : : *
289 : : * - The ANONYMOUS mechanism doesn’t use DATA.
290 : : * - The EXTERNAL mechanism just uses it to transfer a decimal-encoded UID.
291 : : * - The DBUS_COOKIE_SHA1 mechanism transfers a challenge and a SHA1 hash. The
292 : : * hash is bounded in length, but the challenge is not, so could potentially
293 : : * hit this limit. It doesn’t seem unreasonable to bound the challenge to
294 : : * ~4KB though. GDBus itself generates a 16 byte challenge.
295 : : *
296 : : * See https://dbus.freedesktop.org/doc/dbus-specification.html#auth-command-data
297 : : */
298 : : #define MAX_DATA_PAYLOAD_LENGTH_BYTES 4096
299 : :
300 : : /* This function is to avoid situations like this
301 : : *
302 : : * BEGIN\r\nl\0\0\1...
303 : : *
304 : : * e.g. where we read into the first D-Bus message while waiting for
305 : : * the final line from the client (TODO: file bug against gio for
306 : : * this)
307 : : */
308 : : static gchar *
309 : 8970 : _my_g_input_stream_read_line_safe (GInputStream *i,
310 : : size_t max_line_length,
311 : : gsize *out_line_length,
312 : : GCancellable *cancellable,
313 : : GError **error)
314 : : {
315 : : GString *str;
316 : : gchar c;
317 : : gssize num_read;
318 : : gboolean last_was_cr;
319 : :
320 : 8970 : g_assert (max_line_length <= SIZE_MAX - 2);
321 : :
322 : 8970 : str = g_string_new (NULL);
323 : :
324 : 8970 : last_was_cr = FALSE;
325 : 7894 : while (TRUE)
326 : : {
327 : 252379 : if (str->len >= max_line_length + 2 /* allow for \r\n */)
328 : : {
329 : 2 : g_set_error_literal (error,
330 : 0 : G_IO_ERROR,
331 : : G_IO_ERROR_FAILED,
332 : 0 : _("Malformed D-Bus authentication line"));
333 : 2 : goto fail;
334 : : }
335 : :
336 : 260271 : num_read = g_input_stream_read (i,
337 : : &c,
338 : : 1,
339 : 7894 : cancellable,
340 : 7894 : error);
341 : 252377 : if (num_read == -1)
342 : 1 : goto fail;
343 : 252376 : if (num_read == 0)
344 : : {
345 : 10 : if (error != NULL && *error == NULL)
346 : : {
347 : 11 : g_set_error_literal (error,
348 : 1 : G_IO_ERROR,
349 : : G_IO_ERROR_FAILED,
350 : 1 : _("Unexpected lack of content trying to (safely) read a line"));
351 : 1 : }
352 : 10 : goto fail;
353 : : }
354 : :
355 : 252366 : g_string_append_c (str, (gint) c);
356 : 252366 : if (last_was_cr)
357 : : {
358 : 8957 : if (c == 0x0a)
359 : : {
360 : 8957 : g_assert (str->len >= 2);
361 : 8957 : g_string_set_size (str, str->len - 2);
362 : 8957 : goto out;
363 : : }
364 : 0 : }
365 : 243409 : last_was_cr = (c == 0x0d);
366 : : }
367 : :
368 : 8823 : out:
369 : 8957 : if (out_line_length != NULL)
370 : 8957 : *out_line_length = str->len;
371 : 8957 : return g_string_free (str, FALSE);
372 : :
373 : 12 : fail:
374 : 13 : g_assert (error == NULL || *error != NULL);
375 : 13 : g_string_free (str, TRUE);
376 : 13 : return NULL;
377 : 135 : }
378 : :
379 : : /* ---------------------------------------------------------------------------------------------------- */
380 : :
381 : : static gchar *
382 : 2219 : hexdecode (const gchar *str,
383 : : gsize *out_len,
384 : : GError **error)
385 : : {
386 : : gchar *ret;
387 : : GString *s;
388 : : guint n;
389 : :
390 : 2219 : ret = NULL;
391 : 2219 : s = g_string_new (NULL);
392 : :
393 : 13265 : for (n = 0; str[n] != '\0'; n += 2)
394 : : {
395 : : gint upper_nibble;
396 : : gint lower_nibble;
397 : : guint value;
398 : :
399 : 11046 : upper_nibble = g_ascii_xdigit_value (str[n]);
400 : 11046 : lower_nibble = g_ascii_xdigit_value (str[n + 1]);
401 : 11046 : if (upper_nibble == -1 || lower_nibble == -1)
402 : : {
403 : 0 : g_set_error (error,
404 : 0 : G_IO_ERROR,
405 : : G_IO_ERROR_FAILED,
406 : : "Error hexdecoding string '%s' around position %d",
407 : 0 : str, n);
408 : 0 : goto out;
409 : : }
410 : 11046 : value = (upper_nibble<<4) | lower_nibble;
411 : 11046 : g_string_append_c (s, value);
412 : 2636 : }
413 : :
414 : 2219 : *out_len = s->len;
415 : 2219 : ret = g_string_free (s, FALSE);
416 : 2219 : s = NULL;
417 : :
418 : 2161 : out:
419 : 2219 : if (s != NULL)
420 : : {
421 : 0 : *out_len = 0;
422 : 0 : g_string_free (s, TRUE);
423 : 0 : }
424 : 2219 : return ret;
425 : : }
426 : :
427 : : /* ---------------------------------------------------------------------------------------------------- */
428 : :
429 : : static GDBusAuthMechanism *
430 : 2029 : client_choose_mech_and_send_initial_response (GDBusAuth *auth,
431 : : GCredentials *credentials_that_were_sent,
432 : : GDBusConnectionFlags conn_flags,
433 : : const gchar* const *supported_auth_mechs,
434 : : GPtrArray *attempted_auth_mechs,
435 : : GDataOutputStream *dos,
436 : : GCancellable *cancellable,
437 : : GError **error)
438 : : {
439 : : GDBusAuthMechanism *mech;
440 : : GType auth_mech_to_use_gtype;
441 : : guint n;
442 : : guint m;
443 : : gchar *initial_response;
444 : : gsize initial_response_len;
445 : : gchar *encoded;
446 : 30 : gchar *s;
447 : :
448 : 2205 : again:
449 : 2235 : mech = NULL;
450 : :
451 : 2235 : debug_print ("CLIENT: Trying to choose mechanism");
452 : :
453 : : /* find an authentication mechanism to try, if any */
454 : 2235 : auth_mech_to_use_gtype = (GType) 0;
455 : 2456 : for (n = 0; supported_auth_mechs[n] != NULL; n++)
456 : : {
457 : : gboolean attempted_already;
458 : 2456 : attempted_already = FALSE;
459 : 2676 : for (m = 0; m < attempted_auth_mechs->len; m++)
460 : : {
461 : 440 : if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
462 : : {
463 : 220 : attempted_already = TRUE;
464 : 220 : break;
465 : : }
466 : 14 : }
467 : 2456 : if (!attempted_already)
468 : : {
469 : 2236 : auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
470 : 2236 : if (auth_mech_to_use_gtype != (GType) 0)
471 : 2235 : break;
472 : 0 : }
473 : 14 : }
474 : :
475 : 2235 : if (auth_mech_to_use_gtype == (GType) 0)
476 : : {
477 : : gchar *available;
478 : : GString *tried_str;
479 : :
480 : 0 : debug_print ("CLIENT: Exhausted all available mechanisms");
481 : :
482 : 0 : available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
483 : :
484 : 0 : tried_str = g_string_new (NULL);
485 : 0 : for (n = 0; n < attempted_auth_mechs->len; n++)
486 : : {
487 : 0 : if (n > 0)
488 : 0 : g_string_append (tried_str, ", ");
489 : 0 : g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
490 : 0 : }
491 : 0 : g_set_error (error,
492 : 0 : G_IO_ERROR,
493 : : G_IO_ERROR_FAILED,
494 : 0 : _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
495 : 0 : tried_str->str,
496 : 0 : available);
497 : 0 : g_string_free (tried_str, TRUE);
498 : 0 : g_free (available);
499 : 0 : goto out;
500 : : }
501 : :
502 : : /* OK, decided on a mechanism - let's do this thing */
503 : 2265 : mech = g_object_new (auth_mech_to_use_gtype,
504 : 2235 : "stream", auth->priv->stream,
505 : 30 : "credentials", credentials_that_were_sent,
506 : : NULL);
507 : 2235 : debug_print ("CLIENT: Trying mechanism '%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
508 : 2235 : g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
509 : :
510 : : /* the auth mechanism may not be supported
511 : : * (for example, EXTERNAL only works if credentials were exchanged)
512 : : */
513 : 2235 : if (!_g_dbus_auth_mechanism_is_supported (mech))
514 : : {
515 : 206 : debug_print ("CLIENT: Mechanism '%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
516 : 206 : g_object_unref (mech);
517 : 206 : mech = NULL;
518 : 206 : goto again;
519 : : }
520 : :
521 : 2029 : initial_response_len = 0;
522 : 2059 : initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
523 : 30 : conn_flags,
524 : : &initial_response_len);
525 : : #if 0
526 : : g_printerr ("using auth mechanism with name '%s' of type '%s' with initial response '%s'\n",
527 : : _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
528 : : g_type_name (G_TYPE_FROM_INSTANCE (mech)),
529 : : initial_response);
530 : : #endif
531 : 2029 : if (initial_response != NULL)
532 : : {
533 : : //g_printerr ("initial_response = '%s'\n", initial_response);
534 : 387 : encoded = _g_dbus_hexencode (initial_response, initial_response_len);
535 : 387 : s = g_strdup_printf ("AUTH %s %s\r\n",
536 : 30 : _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
537 : 30 : encoded);
538 : 387 : g_free (initial_response);
539 : 387 : g_free (encoded);
540 : 30 : }
541 : : else
542 : : {
543 : 1642 : s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
544 : : }
545 : 2029 : debug_print ("CLIENT: writing '%s'", s);
546 : 2029 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
547 : : {
548 : 0 : g_object_unref (mech);
549 : 0 : mech = NULL;
550 : 0 : g_free (s);
551 : 0 : goto out;
552 : : }
553 : 2029 : g_free (s);
554 : :
555 : 1999 : out:
556 : 2029 : return mech;
557 : : }
558 : :
559 : :
560 : : /* ---------------------------------------------------------------------------------------------------- */
561 : :
562 : : typedef enum
563 : : {
564 : : CLIENT_STATE_WAITING_FOR_DATA,
565 : : CLIENT_STATE_WAITING_FOR_OK,
566 : : CLIENT_STATE_WAITING_FOR_REJECT,
567 : : CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
568 : : } ClientState;
569 : :
570 : : gchar *
571 : 2018 : _g_dbus_auth_run_client (GDBusAuth *auth,
572 : : GDBusAuthObserver *observer,
573 : : GDBusConnectionFlags conn_flags,
574 : : GDBusCapabilityFlags offered_capabilities,
575 : : GDBusCapabilityFlags *out_negotiated_capabilities,
576 : : GCancellable *cancellable,
577 : : GError **error)
578 : : {
579 : : gchar *s;
580 : : GDataOutputStream *dos;
581 : : GCredentials *credentials;
582 : : gchar *ret_guid;
583 : : gchar *line;
584 : : gsize line_length;
585 : : gchar **supported_auth_mechs;
586 : : GPtrArray *attempted_auth_mechs;
587 : : GDBusAuthMechanism *mech;
588 : : ClientState state;
589 : : GDBusCapabilityFlags negotiated_capabilities;
590 : :
591 : 2018 : g_return_val_if_fail ((conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT), NULL);
592 : 2018 : g_return_val_if_fail (!(conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER), NULL);
593 : :
594 : 2018 : debug_print ("CLIENT: initiating");
595 : :
596 : 2018 : _g_dbus_auth_add_mechs (auth, observer);
597 : :
598 : 2018 : ret_guid = NULL;
599 : 2018 : supported_auth_mechs = NULL;
600 : 2018 : attempted_auth_mechs = g_ptr_array_new ();
601 : 2018 : mech = NULL;
602 : 2018 : negotiated_capabilities = 0;
603 : 2018 : credentials = NULL;
604 : :
605 : 2018 : dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
606 : 2018 : g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
607 : :
608 : : #ifdef G_OS_UNIX
609 : 2001 : if (G_IS_UNIX_CONNECTION (auth->priv->stream))
610 : : {
611 : 1791 : credentials = g_credentials_new ();
612 : 1791 : if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
613 : : cancellable,
614 : : error))
615 : 0 : goto out;
616 : : }
617 : : else
618 : : {
619 : 210 : if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
620 : 0 : goto out;
621 : : }
622 : : #else
623 : 17 : if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
624 : 0 : goto out;
625 : : #endif
626 : :
627 : 2018 : if (credentials != NULL)
628 : : {
629 : 1791 : if (G_UNLIKELY (_g_dbus_debug_authentication ()))
630 : : {
631 : 0 : s = g_credentials_to_string (credentials);
632 : 0 : debug_print ("CLIENT: sent credentials '%s'", s);
633 : 0 : g_free (s);
634 : 0 : }
635 : 0 : }
636 : : else
637 : : {
638 : 227 : debug_print ("CLIENT: didn't send any credentials");
639 : : }
640 : :
641 : : /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
642 : :
643 : : /* Get list of supported authentication mechanisms */
644 : 2018 : s = "AUTH\r\n";
645 : 2018 : debug_print ("CLIENT: writing '%s'", s);
646 : 2018 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
647 : 1 : goto out;
648 : 2017 : state = CLIENT_STATE_WAITING_FOR_REJECT;
649 : :
650 : 60 : while (TRUE)
651 : : {
652 : 7499 : switch (state)
653 : : {
654 : 2001 : case CLIENT_STATE_WAITING_FOR_REJECT:
655 : 2017 : debug_print ("CLIENT: WaitingForReject");
656 : 2033 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
657 : : MAX_DATA_PAYLOAD_LENGTH_BYTES,
658 : : &line_length,
659 : 16 : cancellable,
660 : 16 : error);
661 : 2017 : if (line == NULL)
662 : 2 : goto out;
663 : 2015 : debug_print ("CLIENT: WaitingForReject, read '%s'", line);
664 : :
665 : 1999 : choose_mechanism:
666 : 2059 : if (!g_str_has_prefix (line, "REJECTED "))
667 : : {
668 : 0 : g_set_error (error,
669 : 0 : G_IO_ERROR,
670 : : G_IO_ERROR_FAILED,
671 : : "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
672 : 0 : line);
673 : 0 : g_free (line);
674 : 0 : goto out;
675 : : }
676 : 2029 : if (supported_auth_mechs == NULL)
677 : : {
678 : 2015 : supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 0);
679 : : #if 0
680 : : for (n = 0; supported_auth_mechs != NULL && supported_auth_mechs[n] != NULL; n++)
681 : : g_printerr ("supported_auth_mechs[%d] = '%s'\n", n, supported_auth_mechs[n]);
682 : : #endif
683 : 16 : }
684 : 2029 : g_free (line);
685 : 2059 : mech = client_choose_mech_and_send_initial_response (auth,
686 : 30 : credentials,
687 : 30 : conn_flags,
688 : 30 : (const gchar* const *) supported_auth_mechs,
689 : 30 : attempted_auth_mechs,
690 : 30 : dos,
691 : 30 : cancellable,
692 : 30 : error);
693 : 2029 : if (mech == NULL)
694 : 0 : goto out;
695 : 2029 : if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA)
696 : 1665 : state = CLIENT_STATE_WAITING_FOR_DATA;
697 : : else
698 : 364 : state = CLIENT_STATE_WAITING_FOR_OK;
699 : 2029 : break;
700 : :
701 : 1999 : case CLIENT_STATE_WAITING_FOR_OK:
702 : 2029 : debug_print ("CLIENT: WaitingForOK");
703 : 2059 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
704 : : MAX_DATA_PAYLOAD_LENGTH_BYTES,
705 : : &line_length,
706 : 30 : cancellable,
707 : 30 : error);
708 : 2029 : if (line == NULL)
709 : 4 : goto out;
710 : 2025 : debug_print ("CLIENT: WaitingForOK, read '%s'", line);
711 : 2054 : if (g_str_has_prefix (line, "OK "))
712 : : {
713 : 2011 : if (!g_dbus_is_guid (line + 3))
714 : : {
715 : 0 : g_set_error (error,
716 : 0 : G_IO_ERROR,
717 : : G_IO_ERROR_FAILED,
718 : : "Invalid OK response '%s'",
719 : 0 : line);
720 : 0 : g_free (line);
721 : 0 : goto out;
722 : : }
723 : 2011 : ret_guid = g_strdup (line + 3);
724 : 2011 : g_free (line);
725 : :
726 : 2011 : if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
727 : : {
728 : 1788 : s = "NEGOTIATE_UNIX_FD\r\n";
729 : 1788 : debug_print ("CLIENT: writing '%s'", s);
730 : 1788 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
731 : 0 : goto out;
732 : 1788 : state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
733 : 0 : }
734 : : else
735 : : {
736 : 223 : s = "BEGIN\r\n";
737 : 223 : debug_print ("CLIENT: writing '%s'", s);
738 : 223 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
739 : 0 : goto out;
740 : : /* and we're done! */
741 : 223 : goto out;
742 : : }
743 : 0 : }
744 : 28 : else if (g_str_has_prefix (line, "REJECTED "))
745 : : {
746 : 14 : goto choose_mechanism;
747 : : }
748 : : else
749 : : {
750 : : /* TODO: handle other valid responses */
751 : 0 : g_set_error (error,
752 : 0 : G_IO_ERROR,
753 : : G_IO_ERROR_FAILED,
754 : : "In WaitingForOk: unexpected response '%s'",
755 : 0 : line);
756 : 0 : g_free (line);
757 : 0 : goto out;
758 : : }
759 : 1788 : break;
760 : :
761 : 1788 : case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD:
762 : 1788 : debug_print ("CLIENT: WaitingForAgreeUnixFD");
763 : 1788 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
764 : : MAX_DATA_PAYLOAD_LENGTH_BYTES,
765 : : &line_length,
766 : 0 : cancellable,
767 : 0 : error);
768 : 1788 : if (line == NULL)
769 : 0 : goto out;
770 : 1788 : debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line);
771 : 1788 : if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
772 : : {
773 : 1788 : g_free (line);
774 : 1788 : negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
775 : 1788 : s = "BEGIN\r\n";
776 : 1788 : debug_print ("CLIENT: writing '%s'", s);
777 : 1788 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
778 : 0 : goto out;
779 : : /* and we're done! */
780 : 1788 : goto out;
781 : : }
782 : 0 : else if (g_str_has_prefix (line, "ERROR") && (line[5] == 0 || g_ascii_isspace (line[5])))
783 : : {
784 : : //g_strstrip (line + 5); g_debug ("bah, no unix_fd: '%s'", line + 5);
785 : 0 : g_free (line);
786 : 0 : s = "BEGIN\r\n";
787 : 0 : debug_print ("CLIENT: writing '%s'", s);
788 : 0 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
789 : 0 : goto out;
790 : : /* and we're done! */
791 : 0 : goto out;
792 : : }
793 : : else
794 : : {
795 : : /* TODO: handle other valid responses */
796 : 0 : g_set_error (error,
797 : 0 : G_IO_ERROR,
798 : : G_IO_ERROR_FAILED,
799 : : "In WaitingForAgreeUnixFd: unexpected response '%s'",
800 : 0 : line);
801 : 0 : g_free (line);
802 : 0 : goto out;
803 : : }
804 : : break;
805 : :
806 : 1651 : case CLIENT_STATE_WAITING_FOR_DATA:
807 : 1665 : debug_print ("CLIENT: WaitingForData");
808 : 1679 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
809 : : MAX_DATA_PAYLOAD_LENGTH_BYTES,
810 : : &line_length,
811 : 14 : cancellable,
812 : 14 : error);
813 : 1665 : if (line == NULL)
814 : 0 : goto out;
815 : 1665 : debug_print ("CLIENT: WaitingForData, read='%s'", line);
816 : 1679 : if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
817 : 1651 : {
818 : : gchar *encoded;
819 : : gchar *decoded_data;
820 : 1665 : gsize decoded_data_len = 0;
821 : :
822 : 1665 : encoded = g_strdup (line + 4);
823 : 1665 : g_free (line);
824 : 1665 : g_strstrip (encoded);
825 : 1665 : decoded_data = hexdecode (encoded, &decoded_data_len, error);
826 : 1665 : g_free (encoded);
827 : 1665 : if (decoded_data == NULL)
828 : : {
829 : 0 : g_prefix_error (error, "DATA response is malformed: ");
830 : : /* invalid encoding, disconnect! */
831 : 0 : goto out;
832 : : }
833 : 1665 : _g_dbus_auth_mechanism_client_data_receive (mech, decoded_data, decoded_data_len);
834 : 1665 : g_free (decoded_data);
835 : :
836 : 1665 : if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND)
837 : : {
838 : : gchar *data;
839 : : gsize data_len;
840 : :
841 : 1665 : data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
842 : :
843 : 1665 : if (data_len == 0)
844 : : {
845 : 1642 : s = g_strdup ("DATA\r\n");
846 : 0 : }
847 : : else
848 : : {
849 : 23 : gchar *encoded_data = _g_dbus_hexencode (data, data_len);
850 : :
851 : 23 : s = g_strconcat ("DATA ", encoded_data, "\r\n", NULL);
852 : 23 : g_free (encoded_data);
853 : : }
854 : :
855 : 1665 : g_free (data);
856 : 1665 : debug_print ("CLIENT: writing '%s'", s);
857 : 1665 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
858 : : {
859 : 0 : g_free (s);
860 : 0 : goto out;
861 : : }
862 : 1665 : g_free (s);
863 : 14 : }
864 : 1665 : state = CLIENT_STATE_WAITING_FOR_OK;
865 : 14 : }
866 : 0 : else if (g_str_has_prefix (line, "REJECTED "))
867 : : {
868 : : /* could be the chosen authentication method just doesn't work. Try
869 : : * another one...
870 : : */
871 : 0 : goto choose_mechanism;
872 : : }
873 : : else
874 : : {
875 : 0 : g_set_error (error,
876 : 0 : G_IO_ERROR,
877 : : G_IO_ERROR_FAILED,
878 : : "In WaitingForData: unexpected response '%s'",
879 : 0 : line);
880 : 0 : g_free (line);
881 : 0 : goto out;
882 : : }
883 : 1665 : break;
884 : :
885 : 0 : default:
886 : : g_assert_not_reached ();
887 : 0 : break;
888 : : }
889 : :
890 : : }; /* main authentication client loop */
891 : :
892 : 2001 : out:
893 : 2018 : if (mech != NULL)
894 : 2015 : g_object_unref (mech);
895 : 2018 : g_ptr_array_unref (attempted_auth_mechs);
896 : 2018 : g_strfreev (supported_auth_mechs);
897 : 2018 : g_object_unref (dos);
898 : :
899 : : /* ensure return value is NULL if error is set */
900 : 2018 : if (error != NULL && *error != NULL)
901 : : {
902 : 7 : g_free (ret_guid);
903 : 7 : ret_guid = NULL;
904 : 2 : }
905 : :
906 : 2018 : if (ret_guid != NULL)
907 : : {
908 : 2011 : if (out_negotiated_capabilities != NULL)
909 : 2011 : *out_negotiated_capabilities = negotiated_capabilities;
910 : 15 : }
911 : :
912 : 2018 : if (credentials != NULL)
913 : 1791 : g_object_unref (credentials);
914 : :
915 : 2018 : debug_print ("CLIENT: Done, authenticated=%d", ret_guid != NULL);
916 : :
917 : 2018 : return ret_guid;
918 : 17 : }
919 : :
920 : : /* ---------------------------------------------------------------------------------------------------- */
921 : :
922 : : static gchar *
923 : 368 : get_auth_mechanisms (GDBusAuth *auth,
924 : : gboolean allow_anonymous,
925 : : const gchar *prefix,
926 : : const gchar *suffix,
927 : : const gchar *separator)
928 : : {
929 : : GList *l;
930 : : GString *str;
931 : : gboolean need_sep;
932 : :
933 : 368 : str = g_string_new (prefix);
934 : 368 : need_sep = FALSE;
935 : 1290 : for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
936 : : {
937 : 922 : Mechanism *m = l->data;
938 : :
939 : 922 : if (!allow_anonymous && g_strcmp0 (m->name, "ANONYMOUS") == 0)
940 : 72 : continue;
941 : :
942 : 850 : if (need_sep)
943 : 30 : g_string_append (str, separator);
944 : 850 : g_string_append (str, m->name);
945 : 850 : need_sep = TRUE;
946 : 60 : }
947 : :
948 : 30 : g_string_append (str, suffix);
949 : 368 : return g_string_free (str, FALSE);
950 : : }
951 : :
952 : :
953 : : typedef enum
954 : : {
955 : : SERVER_STATE_WAITING_FOR_AUTH,
956 : : SERVER_STATE_WAITING_FOR_DATA,
957 : : SERVER_STATE_WAITING_FOR_BEGIN
958 : : } ServerState;
959 : :
960 : : gboolean
961 : 439 : _g_dbus_auth_run_server (GDBusAuth *auth,
962 : : GDBusAuthObserver *observer,
963 : : const gchar *guid,
964 : : gboolean allow_anonymous,
965 : : gboolean require_same_user,
966 : : GDBusCapabilityFlags offered_capabilities,
967 : : GDBusCapabilityFlags *out_negotiated_capabilities,
968 : : GCredentials **out_received_credentials,
969 : : GCancellable *cancellable,
970 : : GError **error)
971 : : {
972 : : gboolean ret;
973 : : ServerState state;
974 : : GDataOutputStream *dos;
975 : : GError *local_error;
976 : : gchar *line;
977 : : gsize line_length;
978 : : GDBusAuthMechanism *mech;
979 : : gchar *s;
980 : : GDBusCapabilityFlags negotiated_capabilities;
981 : : GCredentials *credentials;
982 : 439 : GCredentials *own_credentials = NULL;
983 : :
984 : 439 : debug_print ("SERVER: initiating");
985 : :
986 : 439 : _g_dbus_auth_add_mechs (auth, observer);
987 : :
988 : 439 : ret = FALSE;
989 : 439 : dos = NULL;
990 : 439 : mech = NULL;
991 : 439 : negotiated_capabilities = 0;
992 : 439 : credentials = NULL;
993 : :
994 : 439 : if (!g_dbus_is_guid (guid))
995 : : {
996 : 0 : g_set_error (error,
997 : 0 : G_IO_ERROR,
998 : : G_IO_ERROR_FAILED,
999 : : "The given GUID '%s' is not valid",
1000 : 0 : guid);
1001 : 0 : goto out;
1002 : : }
1003 : :
1004 : : /* We use an extremely slow (but reliable) line reader for input
1005 : : * instead of something buffered - this basically does a recvfrom()
1006 : : * system call per character
1007 : : *
1008 : : * (the problem with using GDataInputStream's read_line is that
1009 : : * because of buffering it might start reading into the first D-Bus
1010 : : * message that appears after "BEGIN\r\n"....)
1011 : : */
1012 : :
1013 : 439 : dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
1014 : 439 : g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
1015 : :
1016 : : /* read the NUL-byte, possibly with credentials attached */
1017 : : #ifndef G_CREDENTIALS_PREFER_MESSAGE_PASSING
1018 : 455 : if (G_IS_SOCKET_CONNECTION (auth->priv->stream))
1019 : : {
1020 : 237 : GSocket *sock = g_socket_connection_get_socket (G_SOCKET_CONNECTION (auth->priv->stream));
1021 : :
1022 : 237 : local_error = NULL;
1023 : 237 : credentials = g_socket_get_credentials (sock, &local_error);
1024 : :
1025 : 237 : if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1026 : : {
1027 : 0 : g_propagate_error (error, local_error);
1028 : 0 : goto out;
1029 : : }
1030 : : else
1031 : : {
1032 : : /* Clear the error indicator, so we can retry with
1033 : : * g_unix_connection_receive_credentials() if necessary */
1034 : 237 : g_clear_error (&local_error);
1035 : : }
1036 : 16 : }
1037 : : #endif
1038 : :
1039 : 445 : if (credentials == NULL && G_IS_UNIX_CONNECTION (auth->priv->stream))
1040 : : {
1041 : 0 : local_error = NULL;
1042 : 0 : credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
1043 : 0 : cancellable,
1044 : : &local_error);
1045 : 0 : if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1046 : : {
1047 : 0 : g_propagate_error (error, local_error);
1048 : 0 : goto out;
1049 : : }
1050 : 0 : g_clear_error (&local_error);
1051 : 0 : }
1052 : : else
1053 : : {
1054 : : gchar c;
1055 : : gssize num_read;
1056 : :
1057 : 439 : local_error = NULL;
1058 : 455 : num_read = g_input_stream_read (g_io_stream_get_input_stream (auth->priv->stream),
1059 : : &c, 1,
1060 : 16 : cancellable, &local_error);
1061 : 439 : if (num_read != 1 || local_error != NULL)
1062 : : {
1063 : 0 : if (local_error == NULL)
1064 : 0 : g_set_error_literal (error,
1065 : 0 : G_IO_ERROR,
1066 : : G_IO_ERROR_FAILED,
1067 : 0 : _ ("Unexpected lack of content trying to read a byte"));
1068 : : else
1069 : 0 : g_propagate_error (error, local_error);
1070 : 0 : goto out;
1071 : : }
1072 : : }
1073 : :
1074 : 439 : if (credentials != NULL)
1075 : : {
1076 : 231 : if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1077 : : {
1078 : 0 : s = g_credentials_to_string (credentials);
1079 : 0 : debug_print ("SERVER: received credentials '%s'", s);
1080 : 0 : g_free (s);
1081 : 0 : }
1082 : 10 : }
1083 : : else
1084 : : {
1085 : 208 : debug_print ("SERVER: didn't receive any credentials");
1086 : : }
1087 : :
1088 : 439 : own_credentials = g_credentials_new ();
1089 : :
1090 : 439 : state = SERVER_STATE_WAITING_FOR_AUTH;
1091 : 75 : while (TRUE)
1092 : : {
1093 : 1471 : switch (state)
1094 : : {
1095 : 761 : case SERVER_STATE_WAITING_FOR_AUTH:
1096 : 807 : debug_print ("SERVER: WaitingForAuth");
1097 : 853 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1098 : 807 : strlen ("AUTH ") + get_longest_mechanism_name_length (auth) + strlen (" ") + MAX_DATA_PAYLOAD_LENGTH_BYTES,
1099 : : &line_length,
1100 : 46 : cancellable,
1101 : 46 : error);
1102 : 807 : debug_print ("SERVER: WaitingForAuth, read '%s'", line);
1103 : 807 : if (line == NULL)
1104 : 3 : goto out;
1105 : 804 : if (g_strcmp0 (line, "AUTH") == 0)
1106 : : {
1107 : 252 : s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1108 : 252 : debug_print ("SERVER: writing '%s'", s);
1109 : 252 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1110 : : {
1111 : 0 : g_free (s);
1112 : 0 : g_free (line);
1113 : 0 : goto out;
1114 : : }
1115 : 252 : g_free (s);
1116 : 252 : g_free (line);
1117 : 16 : }
1118 : 582 : else if (g_str_has_prefix (line, "AUTH "))
1119 : : {
1120 : : gchar **tokens;
1121 : : const gchar *encoded;
1122 : : const gchar *mech_name;
1123 : : GType auth_mech_to_use_gtype;
1124 : :
1125 : 552 : tokens = g_strsplit (line, " ", 0);
1126 : :
1127 : 552 : switch (g_strv_length (tokens))
1128 : : {
1129 : 4 : case 2:
1130 : : /* no initial response */
1131 : 4 : mech_name = tokens[1];
1132 : 4 : encoded = NULL;
1133 : 4 : break;
1134 : :
1135 : 518 : case 3:
1136 : : /* initial response */
1137 : 548 : mech_name = tokens[1];
1138 : 548 : encoded = tokens[2];
1139 : 548 : break;
1140 : :
1141 : 0 : default:
1142 : 0 : g_set_error (error,
1143 : 0 : G_IO_ERROR,
1144 : : G_IO_ERROR_FAILED,
1145 : : "Unexpected line '%s' while in WaitingForAuth state",
1146 : 0 : line);
1147 : 0 : g_strfreev (tokens);
1148 : 0 : g_free (line);
1149 : 0 : goto out;
1150 : : }
1151 : :
1152 : 552 : g_free (line);
1153 : :
1154 : : /* TODO: record that the client has attempted to use this mechanism */
1155 : : //g_debug ("client is trying '%s'", mech_name);
1156 : :
1157 : 552 : auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1158 : 579 : if ((auth_mech_to_use_gtype == (GType) 0) ||
1159 : 226 : (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1160 : : {
1161 : : /* We don't support this auth mechanism */
1162 : 80 : g_strfreev (tokens);
1163 : 80 : s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1164 : 80 : debug_print ("SERVER: writing '%s'", s);
1165 : 80 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1166 : : {
1167 : 0 : g_free (s);
1168 : 0 : goto out;
1169 : : }
1170 : 80 : g_free (s);
1171 : :
1172 : : /* stay in WAITING FOR AUTH */
1173 : 80 : state = SERVER_STATE_WAITING_FOR_AUTH;
1174 : 0 : }
1175 : : else
1176 : : {
1177 : : gchar *initial_response;
1178 : : gsize initial_response_len;
1179 : :
1180 : 472 : g_clear_object (&mech);
1181 : 944 : mech = g_object_new (auth_mech_to_use_gtype,
1182 : 472 : "stream", auth->priv->stream,
1183 : 30 : "credentials", credentials,
1184 : : NULL);
1185 : :
1186 : 472 : initial_response = NULL;
1187 : 472 : initial_response_len = 0;
1188 : 472 : if (encoded != NULL)
1189 : : {
1190 : 468 : initial_response = hexdecode (encoded, &initial_response_len, error);
1191 : 468 : if (initial_response == NULL)
1192 : : {
1193 : 0 : g_prefix_error (error, "Initial response is malformed: ");
1194 : : /* invalid encoding, disconnect! */
1195 : 0 : g_strfreev (tokens);
1196 : 3 : goto out;
1197 : : }
1198 : 30 : }
1199 : :
1200 : 502 : _g_dbus_auth_mechanism_server_initiate (mech,
1201 : 30 : initial_response,
1202 : 30 : initial_response_len);
1203 : 472 : g_free (initial_response);
1204 : 472 : g_strfreev (tokens);
1205 : :
1206 : 514 : change_state:
1207 : 644 : switch (_g_dbus_auth_mechanism_server_get_state (mech))
1208 : : {
1209 : 420 : case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1210 : 436 : if (require_same_user &&
1211 : 21 : (credentials == NULL ||
1212 : 21 : !g_credentials_is_same_user (credentials, own_credentials, NULL)))
1213 : : {
1214 : : /* disconnect */
1215 : 0 : g_set_error_literal (error,
1216 : 0 : G_IO_ERROR,
1217 : : G_IO_ERROR_FAILED,
1218 : 0 : _("User IDs must be the same for peer and server"));
1219 : 0 : goto out;
1220 : : }
1221 : 638 : else if (observer != NULL &&
1222 : 216 : !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1223 : 209 : auth->priv->stream,
1224 : 7 : credentials))
1225 : : {
1226 : : /* disconnect */
1227 : 5 : g_set_error_literal (error,
1228 : 1 : G_IO_ERROR,
1229 : : G_IO_ERROR_FAILED,
1230 : 1 : _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1231 : 4 : goto out;
1232 : : }
1233 : : else
1234 : : {
1235 : 432 : s = g_strconcat ("OK ", guid, "\r\n", NULL);
1236 : 432 : debug_print ("SERVER: writing '%s'", s);
1237 : 432 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1238 : : {
1239 : 0 : g_free (s);
1240 : 0 : goto out;
1241 : : }
1242 : 432 : g_free (s);
1243 : 432 : state = SERVER_STATE_WAITING_FOR_BEGIN;
1244 : : }
1245 : 432 : break;
1246 : :
1247 : 22 : case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1248 : 36 : s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1249 : 36 : debug_print ("SERVER: writing '%s'", s);
1250 : 36 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1251 : : {
1252 : 0 : g_free (s);
1253 : 0 : goto out;
1254 : : }
1255 : 36 : g_free (s);
1256 : 36 : state = SERVER_STATE_WAITING_FOR_AUTH;
1257 : 36 : break;
1258 : :
1259 : 72 : case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1260 : 86 : state = SERVER_STATE_WAITING_FOR_DATA;
1261 : 86 : break;
1262 : :
1263 : 72 : case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1264 : : {
1265 : : gchar *data;
1266 : : gsize data_len;
1267 : :
1268 : 86 : data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1269 : :
1270 : 86 : if (data != NULL)
1271 : : {
1272 : 86 : if (data_len == 0)
1273 : : {
1274 : 4 : s = g_strdup ("DATA\r\n");
1275 : 0 : }
1276 : : else
1277 : : {
1278 : 82 : gchar *encoded_data = _g_dbus_hexencode (data, data_len);
1279 : :
1280 : 82 : s = g_strconcat ("DATA ", encoded_data, "\r\n", NULL);
1281 : 82 : g_free (encoded_data);
1282 : : }
1283 : :
1284 : 86 : g_free (data);
1285 : :
1286 : 86 : debug_print ("SERVER: writing '%s'", s);
1287 : 86 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1288 : : {
1289 : 0 : g_free (s);
1290 : 0 : goto out;
1291 : : }
1292 : 86 : g_free (s);
1293 : 14 : }
1294 : : }
1295 : 86 : goto change_state;
1296 : : break;
1297 : :
1298 : 0 : default:
1299 : : /* TODO */
1300 : : g_assert_not_reached ();
1301 : 0 : break;
1302 : : }
1303 : : }
1304 : 43 : }
1305 : : else
1306 : : {
1307 : 0 : g_set_error (error,
1308 : 0 : G_IO_ERROR,
1309 : : G_IO_ERROR_FAILED,
1310 : : "Unexpected line '%s' while in WaitingForAuth state",
1311 : 0 : line);
1312 : 0 : g_free (line);
1313 : 0 : goto out;
1314 : : }
1315 : 886 : break;
1316 : :
1317 : 72 : case SERVER_STATE_WAITING_FOR_DATA:
1318 : 86 : debug_print ("SERVER: WaitingForData");
1319 : 100 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1320 : : strlen ("DATA ") + MAX_DATA_PAYLOAD_LENGTH_BYTES,
1321 : : &line_length,
1322 : 14 : cancellable,
1323 : 14 : error);
1324 : 86 : debug_print ("SERVER: WaitingForData, read '%s'", line);
1325 : 86 : if (line == NULL)
1326 : 0 : goto out;
1327 : 100 : if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
1328 : : {
1329 : : gchar *encoded;
1330 : : gchar *decoded_data;
1331 : 86 : gsize decoded_data_len = 0;
1332 : :
1333 : 86 : encoded = g_strdup (line + 4);
1334 : 86 : g_free (line);
1335 : 86 : g_strstrip (encoded);
1336 : 86 : decoded_data = hexdecode (encoded, &decoded_data_len, error);
1337 : 86 : g_free (encoded);
1338 : 86 : if (decoded_data == NULL)
1339 : : {
1340 : 0 : g_prefix_error (error, "DATA response is malformed: ");
1341 : : /* invalid encoding, disconnect! */
1342 : 0 : goto out;
1343 : : }
1344 : 86 : _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1345 : 86 : g_free (decoded_data);
1346 : : /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1347 : 86 : goto change_state;
1348 : : }
1349 : : else
1350 : : {
1351 : 0 : g_set_error (error,
1352 : 0 : G_IO_ERROR,
1353 : : G_IO_ERROR_FAILED,
1354 : : "Unexpected line '%s' while in WaitingForData state",
1355 : 0 : line);
1356 : 0 : g_free (line);
1357 : : }
1358 : 0 : goto out;
1359 : :
1360 : 563 : case SERVER_STATE_WAITING_FOR_BEGIN:
1361 : 578 : debug_print ("SERVER: WaitingForBegin");
1362 : 593 : line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1363 : : MAX (strlen ("BEGIN"), strlen ("NEGOTIATE_UNIX_FD")),
1364 : : &line_length,
1365 : 15 : cancellable,
1366 : 15 : error);
1367 : 578 : if (line == NULL)
1368 : 4 : goto out;
1369 : 574 : debug_print ("SERVER: WaitingForBegin, read '%s'", line);
1370 : 574 : if (g_strcmp0 (line, "BEGIN") == 0)
1371 : : {
1372 : : /* YAY, done! */
1373 : 428 : ret = TRUE;
1374 : 428 : g_free (line);
1375 : 428 : goto out;
1376 : : }
1377 : 146 : else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1378 : : {
1379 : 146 : g_free (line);
1380 : 146 : if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1381 : : {
1382 : 146 : negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1383 : 146 : s = "AGREE_UNIX_FD\r\n";
1384 : 146 : debug_print ("SERVER: writing '%s'", s);
1385 : 146 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1386 : 0 : goto out;
1387 : 0 : }
1388 : : else
1389 : : {
1390 : 0 : s = "ERROR \"fd passing not offered\"\r\n";
1391 : 0 : debug_print ("SERVER: writing '%s'", s);
1392 : 0 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1393 : 0 : goto out;
1394 : : }
1395 : 0 : }
1396 : : else
1397 : : {
1398 : 0 : g_debug ("Unexpected line '%s' while in WaitingForBegin state", line);
1399 : 0 : g_free (line);
1400 : 0 : s = "ERROR \"Unknown Command\"\r\n";
1401 : 0 : debug_print ("SERVER: writing '%s'", s);
1402 : 0 : if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1403 : 0 : goto out;
1404 : : }
1405 : 146 : break;
1406 : :
1407 : 0 : default:
1408 : : g_assert_not_reached ();
1409 : 0 : break;
1410 : : }
1411 : : }
1412 : :
1413 : :
1414 : : g_set_error_literal (error,
1415 : : G_IO_ERROR,
1416 : : G_IO_ERROR_FAILED,
1417 : : "Not implemented (server)");
1418 : :
1419 : 423 : out:
1420 : 439 : g_clear_object (&mech);
1421 : 439 : g_clear_object (&dos);
1422 : 439 : g_clear_object (&own_credentials);
1423 : :
1424 : : /* ensure return value is FALSE if error is set */
1425 : 439 : if (error != NULL && *error != NULL)
1426 : : {
1427 : 11 : ret = FALSE;
1428 : 1 : }
1429 : :
1430 : 439 : if (ret)
1431 : : {
1432 : 428 : if (out_negotiated_capabilities != NULL)
1433 : 428 : *out_negotiated_capabilities = negotiated_capabilities;
1434 : 428 : if (out_received_credentials != NULL)
1435 : 428 : *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1436 : 15 : }
1437 : :
1438 : 439 : if (credentials != NULL)
1439 : 231 : g_object_unref (credentials);
1440 : :
1441 : 439 : debug_print ("SERVER: Done, authenticated=%d", ret);
1442 : :
1443 : 439 : return ret;
1444 : : }
1445 : :
1446 : : /* ---------------------------------------------------------------------------------------------------- */
|