Add Friulian translation
[glib.git] / gio / gdbusauth.c
bloba4458b242997d8a4d517383bc8c578710a59f994
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include "gdbusauth.h"
25 #include "gdbusauthmechanismanon.h"
26 #include "gdbusauthmechanismexternal.h"
27 #include "gdbusauthmechanismsha1.h"
28 #include "gdbusauthobserver.h"
30 #include "gdbuserror.h"
31 #include "gdbusutils.h"
32 #include "gioenumtypes.h"
33 #include "gcredentials.h"
34 #include "gdbusprivate.h"
35 #include "giostream.h"
36 #include "gdatainputstream.h"
37 #include "gdataoutputstream.h"
39 #ifdef G_OS_UNIX
40 #include "gnetworking.h"
41 #include "gunixconnection.h"
42 #include "gunixcredentialsmessage.h"
43 #endif
45 #include "glibintl.h"
47 G_GNUC_PRINTF(1, 2)
48 static void
49 debug_print (const gchar *message, ...)
51 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
53 gchar *s;
54 GString *str;
55 va_list var_args;
56 guint n;
58 _g_dbus_debug_print_lock ();
60 va_start (var_args, message);
61 s = g_strdup_vprintf (message, var_args);
62 va_end (var_args);
64 str = g_string_new (NULL);
65 for (n = 0; s[n] != '\0'; n++)
67 if (G_UNLIKELY (s[n] == '\r'))
68 g_string_append (str, "\\r");
69 else if (G_UNLIKELY (s[n] == '\n'))
70 g_string_append (str, "\\n");
71 else
72 g_string_append_c (str, s[n]);
74 g_print ("GDBus-debug:Auth: %s\n", str->str);
75 g_string_free (str, TRUE);
76 g_free (s);
78 _g_dbus_debug_print_unlock ();
82 typedef struct
84 const gchar *name;
85 gint priority;
86 GType gtype;
87 } Mechanism;
89 static void mechanism_free (Mechanism *m);
91 struct _GDBusAuthPrivate
93 GIOStream *stream;
95 /* A list of available Mechanism, sorted according to priority */
96 GList *available_mechanisms;
99 enum
101 PROP_0,
102 PROP_STREAM
105 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT)
107 /* ---------------------------------------------------------------------------------------------------- */
109 static void
110 _g_dbus_auth_finalize (GObject *object)
112 GDBusAuth *auth = G_DBUS_AUTH (object);
114 if (auth->priv->stream != NULL)
115 g_object_unref (auth->priv->stream);
116 g_list_free_full (auth->priv->available_mechanisms, (GDestroyNotify) mechanism_free);
118 if (G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize != NULL)
119 G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize (object);
122 static void
123 _g_dbus_auth_get_property (GObject *object,
124 guint prop_id,
125 GValue *value,
126 GParamSpec *pspec)
128 GDBusAuth *auth = G_DBUS_AUTH (object);
130 switch (prop_id)
132 case PROP_STREAM:
133 g_value_set_object (value, auth->priv->stream);
134 break;
136 default:
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 break;
142 static void
143 _g_dbus_auth_set_property (GObject *object,
144 guint prop_id,
145 const GValue *value,
146 GParamSpec *pspec)
148 GDBusAuth *auth = G_DBUS_AUTH (object);
150 switch (prop_id)
152 case PROP_STREAM:
153 auth->priv->stream = g_value_dup_object (value);
154 break;
156 default:
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 break;
162 static void
163 _g_dbus_auth_class_init (GDBusAuthClass *klass)
165 GObjectClass *gobject_class;
167 gobject_class = G_OBJECT_CLASS (klass);
168 gobject_class->get_property = _g_dbus_auth_get_property;
169 gobject_class->set_property = _g_dbus_auth_set_property;
170 gobject_class->finalize = _g_dbus_auth_finalize;
172 g_object_class_install_property (gobject_class,
173 PROP_STREAM,
174 g_param_spec_object ("stream",
175 P_("IO Stream"),
176 P_("The underlying GIOStream used for I/O"),
177 G_TYPE_IO_STREAM,
178 G_PARAM_READABLE |
179 G_PARAM_WRITABLE |
180 G_PARAM_CONSTRUCT_ONLY |
181 G_PARAM_STATIC_NAME |
182 G_PARAM_STATIC_BLURB |
183 G_PARAM_STATIC_NICK));
186 static void
187 mechanism_free (Mechanism *m)
189 g_free (m);
192 static void
193 add_mechanism (GDBusAuth *auth,
194 GDBusAuthObserver *observer,
195 GType mechanism_type)
197 const gchar *name;
199 name = _g_dbus_auth_mechanism_get_name (mechanism_type);
200 if (observer == NULL || g_dbus_auth_observer_allow_mechanism (observer, name))
202 Mechanism *m;
203 m = g_new0 (Mechanism, 1);
204 m->name = name;
205 m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
206 m->gtype = mechanism_type;
207 auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
211 static gint
212 mech_compare_func (Mechanism *a, Mechanism *b)
214 gint ret;
215 /* ensure deterministic order */
216 ret = b->priority - a->priority;
217 if (ret == 0)
218 ret = g_strcmp0 (b->name, a->name);
219 return ret;
222 static void
223 _g_dbus_auth_init (GDBusAuth *auth)
225 auth->priv = _g_dbus_auth_get_instance_private (auth);
228 static void
229 _g_dbus_auth_add_mechs (GDBusAuth *auth,
230 GDBusAuthObserver *observer)
232 /* TODO: trawl extension points */
233 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
234 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
235 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
237 auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
238 (GCompareFunc) mech_compare_func);
241 static GType
242 find_mech_by_name (GDBusAuth *auth,
243 const gchar *name)
245 GType ret;
246 GList *l;
248 ret = (GType) 0;
250 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
252 Mechanism *m = l->data;
253 if (g_strcmp0 (name, m->name) == 0)
255 ret = m->gtype;
256 goto out;
260 out:
261 return ret;
264 GDBusAuth *
265 _g_dbus_auth_new (GIOStream *stream)
267 return g_object_new (G_TYPE_DBUS_AUTH,
268 "stream", stream,
269 NULL);
272 /* ---------------------------------------------------------------------------------------------------- */
273 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
274 static gchar *
275 _my_g_data_input_stream_read_line (GDataInputStream *dis,
276 gsize *out_line_length,
277 GCancellable *cancellable,
278 GError **error)
280 gchar *ret;
282 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
284 ret = g_data_input_stream_read_line (dis,
285 out_line_length,
286 cancellable,
287 error);
288 if (ret == NULL && error != NULL && *error == NULL)
290 g_set_error_literal (error,
291 G_IO_ERROR,
292 G_IO_ERROR_FAILED,
293 _("Unexpected lack of content trying to read a line"));
296 return ret;
299 /* This function is to avoid situations like this
301 * BEGIN\r\nl\0\0\1...
303 * e.g. where we read into the first D-Bus message while waiting for
304 * the final line from the client (TODO: file bug against gio for
305 * this)
307 static gchar *
308 _my_g_input_stream_read_line_safe (GInputStream *i,
309 gsize *out_line_length,
310 GCancellable *cancellable,
311 GError **error)
313 GString *str;
314 gchar c;
315 gssize num_read;
316 gboolean last_was_cr;
318 str = g_string_new (NULL);
320 last_was_cr = FALSE;
321 while (TRUE)
323 num_read = g_input_stream_read (i,
326 cancellable,
327 error);
328 if (num_read == -1)
329 goto fail;
330 if (num_read == 0)
332 if (error != NULL && *error == NULL)
334 g_set_error_literal (error,
335 G_IO_ERROR,
336 G_IO_ERROR_FAILED,
337 _("Unexpected lack of content trying to (safely) read a line"));
339 goto fail;
342 g_string_append_c (str, (gint) c);
343 if (last_was_cr)
345 if (c == 0x0a)
347 g_assert (str->len >= 2);
348 g_string_set_size (str, str->len - 2);
349 goto out;
352 last_was_cr = (c == 0x0d);
355 out:
356 if (out_line_length != NULL)
357 *out_line_length = str->len;
358 return g_string_free (str, FALSE);
360 fail:
361 g_assert (error == NULL || *error != NULL);
362 g_string_free (str, TRUE);
363 return NULL;
366 /* ---------------------------------------------------------------------------------------------------- */
368 static void
369 append_nibble (GString *s, gint val)
371 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
374 static gchar *
375 hexdecode (const gchar *str,
376 gsize *out_len,
377 GError **error)
379 gchar *ret;
380 GString *s;
381 guint n;
383 ret = NULL;
384 s = g_string_new (NULL);
386 for (n = 0; str[n] != '\0'; n += 2)
388 gint upper_nibble;
389 gint lower_nibble;
390 guint value;
392 upper_nibble = g_ascii_xdigit_value (str[n]);
393 lower_nibble = g_ascii_xdigit_value (str[n + 1]);
394 if (upper_nibble == -1 || lower_nibble == -1)
396 g_set_error (error,
397 G_IO_ERROR,
398 G_IO_ERROR_FAILED,
399 "Error hexdecoding string '%s' around position %d",
400 str, n);
401 goto out;
403 value = (upper_nibble<<4) | lower_nibble;
404 g_string_append_c (s, value);
407 ret = g_string_free (s, FALSE);
408 s = NULL;
410 out:
411 if (s != NULL)
412 g_string_free (s, TRUE);
413 return ret;
416 /* TODO: take len */
417 static gchar *
418 hexencode (const gchar *str)
420 guint n;
421 GString *s;
423 s = g_string_new (NULL);
424 for (n = 0; str[n] != '\0'; n++)
426 gint val;
427 gint upper_nibble;
428 gint lower_nibble;
430 val = ((const guchar *) str)[n];
431 upper_nibble = val >> 4;
432 lower_nibble = val & 0x0f;
434 append_nibble (s, upper_nibble);
435 append_nibble (s, lower_nibble);
438 return g_string_free (s, FALSE);
441 /* ---------------------------------------------------------------------------------------------------- */
443 static GDBusAuthMechanism *
444 client_choose_mech_and_send_initial_response (GDBusAuth *auth,
445 GCredentials *credentials_that_were_sent,
446 const gchar* const *supported_auth_mechs,
447 GPtrArray *attempted_auth_mechs,
448 GDataOutputStream *dos,
449 GCancellable *cancellable,
450 GError **error)
452 GDBusAuthMechanism *mech;
453 GType auth_mech_to_use_gtype;
454 guint n;
455 guint m;
456 gchar *initial_response;
457 gsize initial_response_len;
458 gchar *encoded;
459 gchar *s;
461 again:
462 mech = NULL;
464 debug_print ("CLIENT: Trying to choose mechanism");
466 /* find an authentication mechanism to try, if any */
467 auth_mech_to_use_gtype = (GType) 0;
468 for (n = 0; supported_auth_mechs[n] != NULL; n++)
470 gboolean attempted_already;
471 attempted_already = FALSE;
472 for (m = 0; m < attempted_auth_mechs->len; m++)
474 if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
476 attempted_already = TRUE;
477 break;
480 if (!attempted_already)
482 auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
483 if (auth_mech_to_use_gtype != (GType) 0)
484 break;
488 if (auth_mech_to_use_gtype == (GType) 0)
490 guint n;
491 gchar *available;
492 GString *tried_str;
494 debug_print ("CLIENT: Exhausted all available mechanisms");
496 available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
498 tried_str = g_string_new (NULL);
499 for (n = 0; n < attempted_auth_mechs->len; n++)
501 if (n > 0)
502 g_string_append (tried_str, ", ");
503 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
505 g_set_error (error,
506 G_IO_ERROR,
507 G_IO_ERROR_FAILED,
508 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
509 tried_str->str,
510 available);
511 g_string_free (tried_str, TRUE);
512 g_free (available);
513 goto out;
516 /* OK, decided on a mechanism - let's do this thing */
517 mech = g_object_new (auth_mech_to_use_gtype,
518 "stream", auth->priv->stream,
519 "credentials", credentials_that_were_sent,
520 NULL);
521 debug_print ("CLIENT: Trying mechanism '%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
522 g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
524 /* the auth mechanism may not be supported
525 * (for example, EXTERNAL only works if credentials were exchanged)
527 if (!_g_dbus_auth_mechanism_is_supported (mech))
529 debug_print ("CLIENT: Mechanism '%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
530 g_object_unref (mech);
531 mech = NULL;
532 goto again;
535 initial_response_len = -1;
536 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
537 &initial_response_len);
538 #if 0
539 g_printerr ("using auth mechanism with name '%s' of type '%s' with initial response '%s'\n",
540 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
541 g_type_name (G_TYPE_FROM_INSTANCE (mech)),
542 initial_response);
543 #endif
544 if (initial_response != NULL)
546 //g_printerr ("initial_response = '%s'\n", initial_response);
547 encoded = hexencode (initial_response);
548 s = g_strdup_printf ("AUTH %s %s\r\n",
549 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
550 encoded);
551 g_free (initial_response);
552 g_free (encoded);
554 else
556 s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
558 debug_print ("CLIENT: writing '%s'", s);
559 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
561 g_object_unref (mech);
562 mech = NULL;
563 g_free (s);
564 goto out;
566 g_free (s);
568 out:
569 return mech;
573 /* ---------------------------------------------------------------------------------------------------- */
575 typedef enum
577 CLIENT_STATE_WAITING_FOR_DATA,
578 CLIENT_STATE_WAITING_FOR_OK,
579 CLIENT_STATE_WAITING_FOR_REJECT,
580 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
581 } ClientState;
583 gchar *
584 _g_dbus_auth_run_client (GDBusAuth *auth,
585 GDBusAuthObserver *observer,
586 GDBusCapabilityFlags offered_capabilities,
587 GDBusCapabilityFlags *out_negotiated_capabilities,
588 GCancellable *cancellable,
589 GError **error)
591 gchar *s;
592 GDataInputStream *dis;
593 GDataOutputStream *dos;
594 GCredentials *credentials;
595 gchar *ret_guid;
596 gchar *line;
597 gsize line_length;
598 gchar **supported_auth_mechs;
599 GPtrArray *attempted_auth_mechs;
600 GDBusAuthMechanism *mech;
601 ClientState state;
602 GDBusCapabilityFlags negotiated_capabilities;
604 debug_print ("CLIENT: initiating");
606 _g_dbus_auth_add_mechs (auth, observer);
608 ret_guid = NULL;
609 supported_auth_mechs = NULL;
610 attempted_auth_mechs = g_ptr_array_new ();
611 mech = NULL;
612 negotiated_capabilities = 0;
613 credentials = NULL;
615 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
616 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
617 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
618 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
620 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
622 #ifdef G_OS_UNIX
623 if (G_IS_UNIX_CONNECTION (auth->priv->stream))
625 credentials = g_credentials_new ();
626 if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
627 cancellable,
628 error))
629 goto out;
631 else
633 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
634 goto out;
636 #else
637 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
638 goto out;
639 #endif
641 if (credentials != NULL)
643 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
645 s = g_credentials_to_string (credentials);
646 debug_print ("CLIENT: sent credentials '%s'", s);
647 g_free (s);
650 else
652 debug_print ("CLIENT: didn't send any credentials");
655 /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
657 /* Get list of supported authentication mechanisms */
658 s = "AUTH\r\n";
659 debug_print ("CLIENT: writing '%s'", s);
660 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
661 goto out;
662 state = CLIENT_STATE_WAITING_FOR_REJECT;
664 while (TRUE)
666 switch (state)
668 case CLIENT_STATE_WAITING_FOR_REJECT:
669 debug_print ("CLIENT: WaitingForReject");
670 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
671 if (line == NULL)
672 goto out;
673 debug_print ("CLIENT: WaitingForReject, read '%s'", line);
675 choose_mechanism:
676 if (!g_str_has_prefix (line, "REJECTED "))
678 g_set_error (error,
679 G_IO_ERROR,
680 G_IO_ERROR_FAILED,
681 "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
682 line);
683 g_free (line);
684 goto out;
686 if (supported_auth_mechs == NULL)
688 supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 0);
689 #if 0
690 for (n = 0; supported_auth_mechs != NULL && supported_auth_mechs[n] != NULL; n++)
691 g_printerr ("supported_auth_mechs[%d] = '%s'\n", n, supported_auth_mechs[n]);
692 #endif
694 g_free (line);
695 mech = client_choose_mech_and_send_initial_response (auth,
696 credentials,
697 (const gchar* const *) supported_auth_mechs,
698 attempted_auth_mechs,
699 dos,
700 cancellable,
701 error);
702 if (mech == NULL)
703 goto out;
704 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA)
705 state = CLIENT_STATE_WAITING_FOR_DATA;
706 else
707 state = CLIENT_STATE_WAITING_FOR_OK;
708 break;
710 case CLIENT_STATE_WAITING_FOR_OK:
711 debug_print ("CLIENT: WaitingForOK");
712 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
713 if (line == NULL)
714 goto out;
715 debug_print ("CLIENT: WaitingForOK, read '%s'", line);
716 if (g_str_has_prefix (line, "OK "))
718 if (!g_dbus_is_guid (line + 3))
720 g_set_error (error,
721 G_IO_ERROR,
722 G_IO_ERROR_FAILED,
723 "Invalid OK response '%s'",
724 line);
725 g_free (line);
726 goto out;
728 ret_guid = g_strdup (line + 3);
729 g_free (line);
731 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
733 s = "NEGOTIATE_UNIX_FD\r\n";
734 debug_print ("CLIENT: writing '%s'", s);
735 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
736 goto out;
737 state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
739 else
741 s = "BEGIN\r\n";
742 debug_print ("CLIENT: writing '%s'", s);
743 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
744 goto out;
745 /* and we're done! */
746 goto out;
749 else if (g_str_has_prefix (line, "REJECTED "))
751 goto choose_mechanism;
753 else
755 /* TODO: handle other valid responses */
756 g_set_error (error,
757 G_IO_ERROR,
758 G_IO_ERROR_FAILED,
759 "In WaitingForOk: unexpected response '%s'",
760 line);
761 g_free (line);
762 goto out;
764 break;
766 case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD:
767 debug_print ("CLIENT: WaitingForAgreeUnixFD");
768 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
769 if (line == NULL)
770 goto out;
771 debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line);
772 if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
774 g_free (line);
775 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
776 s = "BEGIN\r\n";
777 debug_print ("CLIENT: writing '%s'", s);
778 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
779 goto out;
780 /* and we're done! */
781 goto out;
783 else if (g_str_has_prefix (line, "ERROR") && (line[5] == 0 || g_ascii_isspace (line[5])))
785 //g_strstrip (line + 5); g_debug ("bah, no unix_fd: '%s'", line + 5);
786 g_free (line);
787 s = "BEGIN\r\n";
788 debug_print ("CLIENT: writing '%s'", s);
789 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
790 goto out;
791 /* and we're done! */
792 goto out;
794 else
796 /* TODO: handle other valid responses */
797 g_set_error (error,
798 G_IO_ERROR,
799 G_IO_ERROR_FAILED,
800 "In WaitingForAgreeUnixFd: unexpected response '%s'",
801 line);
802 g_free (line);
803 goto out;
805 break;
807 case CLIENT_STATE_WAITING_FOR_DATA:
808 debug_print ("CLIENT: WaitingForData");
809 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
810 if (line == NULL)
811 goto out;
812 debug_print ("CLIENT: WaitingForData, read='%s'", line);
813 if (g_str_has_prefix (line, "DATA "))
815 gchar *encoded;
816 gchar *decoded_data;
817 gsize decoded_data_len = 0;
819 encoded = g_strdup (line + 5);
820 g_free (line);
821 g_strstrip (encoded);
822 decoded_data = hexdecode (encoded, &decoded_data_len, error);
823 g_free (encoded);
824 if (decoded_data == NULL)
826 g_prefix_error (error, "DATA response is malformed: ");
827 /* invalid encoding, disconnect! */
828 goto out;
830 _g_dbus_auth_mechanism_client_data_receive (mech, decoded_data, decoded_data_len);
831 g_free (decoded_data);
833 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND)
835 gchar *data;
836 gsize data_len;
837 gchar *encoded_data;
838 data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
839 encoded_data = hexencode (data);
840 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
841 g_free (encoded_data);
842 g_free (data);
843 debug_print ("CLIENT: writing '%s'", s);
844 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
846 g_free (s);
847 goto out;
849 g_free (s);
851 state = CLIENT_STATE_WAITING_FOR_OK;
853 else if (g_str_has_prefix (line, "REJECTED "))
855 /* could be the chosen authentication method just doesn't work. Try
856 * another one...
858 goto choose_mechanism;
860 else
862 g_set_error (error,
863 G_IO_ERROR,
864 G_IO_ERROR_FAILED,
865 "In WaitingForData: unexpected response '%s'",
866 line);
867 g_free (line);
868 goto out;
870 break;
872 default:
873 g_assert_not_reached ();
874 break;
877 }; /* main authentication client loop */
879 out:
880 if (mech != NULL)
881 g_object_unref (mech);
882 g_ptr_array_unref (attempted_auth_mechs);
883 g_strfreev (supported_auth_mechs);
884 g_object_unref (dis);
885 g_object_unref (dos);
887 /* ensure return value is NULL if error is set */
888 if (error != NULL && *error != NULL)
890 g_free (ret_guid);
891 ret_guid = NULL;
894 if (ret_guid != NULL)
896 if (out_negotiated_capabilities != NULL)
897 *out_negotiated_capabilities = negotiated_capabilities;
900 if (credentials != NULL)
901 g_object_unref (credentials);
903 debug_print ("CLIENT: Done, authenticated=%d", ret_guid != NULL);
905 return ret_guid;
908 /* ---------------------------------------------------------------------------------------------------- */
910 static gchar *
911 get_auth_mechanisms (GDBusAuth *auth,
912 gboolean allow_anonymous,
913 const gchar *prefix,
914 const gchar *suffix,
915 const gchar *separator)
917 GList *l;
918 GString *str;
919 gboolean need_sep;
921 str = g_string_new (prefix);
922 need_sep = FALSE;
923 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
925 Mechanism *m = l->data;
927 if (!allow_anonymous && g_strcmp0 (m->name, "ANONYMOUS") == 0)
928 continue;
930 if (need_sep)
931 g_string_append (str, separator);
932 g_string_append (str, m->name);
933 need_sep = TRUE;
936 g_string_append (str, suffix);
937 return g_string_free (str, FALSE);
941 typedef enum
943 SERVER_STATE_WAITING_FOR_AUTH,
944 SERVER_STATE_WAITING_FOR_DATA,
945 SERVER_STATE_WAITING_FOR_BEGIN
946 } ServerState;
948 gboolean
949 _g_dbus_auth_run_server (GDBusAuth *auth,
950 GDBusAuthObserver *observer,
951 const gchar *guid,
952 gboolean allow_anonymous,
953 GDBusCapabilityFlags offered_capabilities,
954 GDBusCapabilityFlags *out_negotiated_capabilities,
955 GCredentials **out_received_credentials,
956 GCancellable *cancellable,
957 GError **error)
959 gboolean ret;
960 ServerState state;
961 GDataInputStream *dis;
962 GDataOutputStream *dos;
963 GError *local_error;
964 guchar byte;
965 gchar *line;
966 gsize line_length;
967 GDBusAuthMechanism *mech;
968 gchar *s;
969 GDBusCapabilityFlags negotiated_capabilities;
970 GCredentials *credentials;
972 debug_print ("SERVER: initiating");
974 _g_dbus_auth_add_mechs (auth, observer);
976 ret = FALSE;
977 dis = NULL;
978 dos = NULL;
979 mech = NULL;
980 negotiated_capabilities = 0;
981 credentials = NULL;
983 if (!g_dbus_is_guid (guid))
985 g_set_error (error,
986 G_IO_ERROR,
987 G_IO_ERROR_FAILED,
988 "The given guid '%s' is not valid",
989 guid);
990 goto out;
993 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
994 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
995 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
996 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
998 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
1000 /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */
1001 #ifdef G_OS_UNIX
1002 if (G_IS_UNIX_CONNECTION (auth->priv->stream))
1004 local_error = NULL;
1005 credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
1006 cancellable,
1007 &local_error);
1008 if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1010 g_propagate_error (error, local_error);
1011 goto out;
1014 else
1016 local_error = NULL;
1017 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
1018 byte = byte; /* To avoid -Wunused-but-set-variable */
1019 if (local_error != NULL)
1021 g_propagate_error (error, local_error);
1022 goto out;
1025 #else
1026 local_error = NULL;
1027 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
1028 byte = byte; /* To avoid -Wunused-but-set-variable */
1029 if (local_error != NULL)
1031 g_propagate_error (error, local_error);
1032 goto out;
1034 #endif
1035 if (credentials != NULL)
1037 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1039 s = g_credentials_to_string (credentials);
1040 debug_print ("SERVER: received credentials '%s'", s);
1041 g_free (s);
1044 else
1046 debug_print ("SERVER: didn't receive any credentials");
1049 state = SERVER_STATE_WAITING_FOR_AUTH;
1050 while (TRUE)
1052 switch (state)
1054 case SERVER_STATE_WAITING_FOR_AUTH:
1055 debug_print ("SERVER: WaitingForAuth");
1056 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1057 debug_print ("SERVER: WaitingForAuth, read '%s'", line);
1058 if (line == NULL)
1059 goto out;
1060 if (g_strcmp0 (line, "AUTH") == 0)
1062 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1063 debug_print ("SERVER: writing '%s'", s);
1064 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1066 g_free (s);
1067 g_free (line);
1068 goto out;
1070 g_free (s);
1071 g_free (line);
1073 else if (g_str_has_prefix (line, "AUTH "))
1075 gchar **tokens;
1076 const gchar *encoded;
1077 const gchar *mech_name;
1078 GType auth_mech_to_use_gtype;
1080 tokens = g_strsplit (line, " ", 0);
1082 switch (g_strv_length (tokens))
1084 case 2:
1085 /* no initial response */
1086 mech_name = tokens[1];
1087 encoded = NULL;
1088 break;
1090 case 3:
1091 /* initial response */
1092 mech_name = tokens[1];
1093 encoded = tokens[2];
1094 break;
1096 default:
1097 g_set_error (error,
1098 G_IO_ERROR,
1099 G_IO_ERROR_FAILED,
1100 "Unexpected line '%s' while in WaitingForAuth state",
1101 line);
1102 g_strfreev (tokens);
1103 g_free (line);
1104 goto out;
1107 g_free (line);
1109 /* TODO: record that the client has attempted to use this mechanism */
1110 //g_debug ("client is trying '%s'", mech_name);
1112 auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1113 if ((auth_mech_to_use_gtype == (GType) 0) ||
1114 (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1116 /* We don't support this auth mechanism */
1117 g_strfreev (tokens);
1118 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1119 debug_print ("SERVER: writing '%s'", s);
1120 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1122 g_free (s);
1123 goto out;
1125 g_free (s);
1127 /* stay in WAITING FOR AUTH */
1128 state = SERVER_STATE_WAITING_FOR_AUTH;
1130 else
1132 gchar *initial_response;
1133 gsize initial_response_len;
1135 g_clear_object (&mech);
1136 mech = g_object_new (auth_mech_to_use_gtype,
1137 "stream", auth->priv->stream,
1138 "credentials", credentials,
1139 NULL);
1141 initial_response = NULL;
1142 initial_response_len = 0;
1143 if (encoded != NULL)
1145 initial_response = hexdecode (encoded, &initial_response_len, error);
1146 if (initial_response == NULL)
1148 g_prefix_error (error, "Initial response is malformed: ");
1149 /* invalid encoding, disconnect! */
1150 g_strfreev (tokens);
1151 goto out;
1155 _g_dbus_auth_mechanism_server_initiate (mech,
1156 initial_response,
1157 initial_response_len);
1158 g_free (initial_response);
1159 g_strfreev (tokens);
1161 change_state:
1162 switch (_g_dbus_auth_mechanism_server_get_state (mech))
1164 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1165 if (observer != NULL &&
1166 !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1167 auth->priv->stream,
1168 credentials))
1170 /* disconnect */
1171 g_set_error_literal (error,
1172 G_IO_ERROR,
1173 G_IO_ERROR_FAILED,
1174 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1175 goto out;
1177 else
1179 s = g_strdup_printf ("OK %s\r\n", guid);
1180 debug_print ("SERVER: writing '%s'", s);
1181 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1183 g_free (s);
1184 goto out;
1186 g_free (s);
1187 state = SERVER_STATE_WAITING_FOR_BEGIN;
1189 break;
1191 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1192 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1193 debug_print ("SERVER: writing '%s'", s);
1194 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1196 g_free (s);
1197 goto out;
1199 g_free (s);
1200 state = SERVER_STATE_WAITING_FOR_AUTH;
1201 break;
1203 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1204 state = SERVER_STATE_WAITING_FOR_DATA;
1205 break;
1207 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1209 gchar *data;
1210 gsize data_len;
1212 data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1213 if (data != NULL)
1215 gchar *encoded_data;
1217 encoded_data = hexencode (data);
1218 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1219 g_free (encoded_data);
1220 g_free (data);
1222 debug_print ("SERVER: writing '%s'", s);
1223 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1225 g_free (s);
1226 goto out;
1228 g_free (s);
1231 goto change_state;
1232 break;
1234 default:
1235 /* TODO */
1236 g_assert_not_reached ();
1237 break;
1241 else
1243 g_set_error (error,
1244 G_IO_ERROR,
1245 G_IO_ERROR_FAILED,
1246 "Unexpected line '%s' while in WaitingForAuth state",
1247 line);
1248 g_free (line);
1249 goto out;
1251 break;
1253 case SERVER_STATE_WAITING_FOR_DATA:
1254 debug_print ("SERVER: WaitingForData");
1255 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1256 debug_print ("SERVER: WaitingForData, read '%s'", line);
1257 if (line == NULL)
1258 goto out;
1259 if (g_str_has_prefix (line, "DATA "))
1261 gchar *encoded;
1262 gchar *decoded_data;
1263 gsize decoded_data_len = 0;
1265 encoded = g_strdup (line + 5);
1266 g_free (line);
1267 g_strstrip (encoded);
1268 decoded_data = hexdecode (encoded, &decoded_data_len, error);
1269 g_free (encoded);
1270 if (decoded_data == NULL)
1272 g_prefix_error (error, "DATA response is malformed: ");
1273 /* invalid encoding, disconnect! */
1274 goto out;
1276 _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1277 g_free (decoded_data);
1278 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1279 goto change_state;
1281 else
1283 g_set_error (error,
1284 G_IO_ERROR,
1285 G_IO_ERROR_FAILED,
1286 "Unexpected line '%s' while in WaitingForData state",
1287 line);
1288 g_free (line);
1290 goto out;
1292 case SERVER_STATE_WAITING_FOR_BEGIN:
1293 debug_print ("SERVER: WaitingForBegin");
1294 /* Use extremely slow (but reliable) line reader - this basically
1295 * does a recvfrom() system call per character
1297 * (the problem with using GDataInputStream's read_line is that because of
1298 * buffering it might start reading into the first D-Bus message that
1299 * appears after "BEGIN\r\n"....)
1301 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1302 &line_length,
1303 cancellable,
1304 error);
1305 debug_print ("SERVER: WaitingForBegin, read '%s'", line);
1306 if (line == NULL)
1307 goto out;
1308 if (g_strcmp0 (line, "BEGIN") == 0)
1310 /* YAY, done! */
1311 ret = TRUE;
1312 g_free (line);
1313 goto out;
1315 else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1317 g_free (line);
1318 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1320 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1321 s = "AGREE_UNIX_FD\r\n";
1322 debug_print ("SERVER: writing '%s'", s);
1323 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1324 goto out;
1326 else
1328 s = "ERROR \"fd passing not offered\"\r\n";
1329 debug_print ("SERVER: writing '%s'", s);
1330 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1331 goto out;
1334 else
1336 g_debug ("Unexpected line '%s' while in WaitingForBegin state", line);
1337 g_free (line);
1338 s = "ERROR \"Unknown Command\"\r\n";
1339 debug_print ("SERVER: writing '%s'", s);
1340 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1341 goto out;
1343 break;
1345 default:
1346 g_assert_not_reached ();
1347 break;
1352 g_set_error_literal (error,
1353 G_IO_ERROR,
1354 G_IO_ERROR_FAILED,
1355 "Not implemented (server)");
1357 out:
1358 if (mech != NULL)
1359 g_object_unref (mech);
1360 if (dis != NULL)
1361 g_object_unref (dis);
1362 if (dos != NULL)
1363 g_object_unref (dos);
1365 /* ensure return value is FALSE if error is set */
1366 if (error != NULL && *error != NULL)
1368 ret = FALSE;
1371 if (ret)
1373 if (out_negotiated_capabilities != NULL)
1374 *out_negotiated_capabilities = negotiated_capabilities;
1375 if (out_received_credentials != NULL)
1376 *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1379 if (credentials != NULL)
1380 g_object_unref (credentials);
1382 debug_print ("SERVER: Done, authenticated=%d", ret);
1384 return ret;
1387 /* ---------------------------------------------------------------------------------------------------- */