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>
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"
40 #include "gnetworking.h"
41 #include "gunixconnection.h"
42 #include "gunixcredentialsmessage.h"
49 debug_print (const gchar *message, ...)
51 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
58 _g_dbus_debug_print_lock ();
60 va_start (var_args, message);
61 s = g_strdup_vprintf (message, 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");
72 g_string_append_c (str, s[n]);
74 g_print ("GDBus-debug:Auth: %s\n", str->str);
75 g_string_free (str, TRUE);
78 _g_dbus_debug_print_unlock ();
89 static void mechanism_free (Mechanism *m);
91 struct _GDBusAuthPrivate
95 /* A list of available Mechanism, sorted according to priority */
96 GList *available_mechanisms;
105 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT)
107 /* ---------------------------------------------------------------------------------------------------- */
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);
123 _g_dbus_auth_get_property (GObject *object,
128 GDBusAuth *auth = G_DBUS_AUTH (object);
133 g_value_set_object (value, auth->priv->stream);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 _g_dbus_auth_set_property (GObject *object,
148 GDBusAuth *auth = G_DBUS_AUTH (object);
153 auth->priv->stream = g_value_dup_object (value);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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,
174 g_param_spec_object ("stream",
176 P_("The underlying GIOStream used for I/O"),
180 G_PARAM_CONSTRUCT_ONLY |
181 G_PARAM_STATIC_NAME |
182 G_PARAM_STATIC_BLURB |
183 G_PARAM_STATIC_NICK));
187 mechanism_free (Mechanism *m)
193 add_mechanism (GDBusAuth *auth,
194 GDBusAuthObserver *observer,
195 GType mechanism_type)
199 name = _g_dbus_auth_mechanism_get_name (mechanism_type);
200 if (observer == NULL || g_dbus_auth_observer_allow_mechanism (observer, name))
203 m = g_new0 (Mechanism, 1);
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);
212 mech_compare_func (Mechanism *a, Mechanism *b)
215 /* ensure deterministic order */
216 ret = b->priority - a->priority;
218 ret = g_strcmp0 (b->name, a->name);
223 _g_dbus_auth_init (GDBusAuth *auth)
225 auth->priv = _g_dbus_auth_get_instance_private (auth);
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);
242 find_mech_by_name (GDBusAuth *auth,
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)
265 _g_dbus_auth_new (GIOStream *stream)
267 return g_object_new (G_TYPE_DBUS_AUTH,
272 /* ---------------------------------------------------------------------------------------------------- */
273 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
275 _my_g_data_input_stream_read_line (GDataInputStream *dis,
276 gsize *out_line_length,
277 GCancellable *cancellable,
282 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
284 ret = g_data_input_stream_read_line (dis,
288 if (ret == NULL && error != NULL && *error == NULL)
290 g_set_error_literal (error,
293 _("Unexpected lack of content trying to read a line"));
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
308 _my_g_input_stream_read_line_safe (GInputStream *i,
309 gsize *out_line_length,
310 GCancellable *cancellable,
316 gboolean last_was_cr;
318 str = g_string_new (NULL);
323 num_read = g_input_stream_read (i,
332 if (error != NULL && *error == NULL)
334 g_set_error_literal (error,
337 _("Unexpected lack of content trying to (safely) read a line"));
342 g_string_append_c (str, (gint) c);
347 g_assert (str->len >= 2);
348 g_string_set_size (str, str->len - 2);
352 last_was_cr = (c == 0x0d);
356 if (out_line_length != NULL)
357 *out_line_length = str->len;
358 return g_string_free (str, FALSE);
361 g_assert (error == NULL || *error != NULL);
362 g_string_free (str, TRUE);
366 /* ---------------------------------------------------------------------------------------------------- */
369 append_nibble (GString *s, gint val)
371 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
375 hexdecode (const gchar *str,
384 s = g_string_new (NULL);
386 for (n = 0; str[n] != '\0'; n += 2)
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)
399 "Error hexdecoding string '%s' around position %d",
403 value = (upper_nibble<<4) | lower_nibble;
404 g_string_append_c (s, value);
407 ret = g_string_free (s, FALSE);
412 g_string_free (s, TRUE);
418 hexencode (const gchar *str)
423 s = g_string_new (NULL);
424 for (n = 0; str[n] != '\0'; n++)
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,
452 GDBusAuthMechanism *mech;
453 GType auth_mech_to_use_gtype;
456 gchar *initial_response;
457 gsize initial_response_len;
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;
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)
488 if (auth_mech_to_use_gtype == (GType) 0)
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++)
502 g_string_append (tried_str, ", ");
503 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
508 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
511 g_string_free (tried_str, TRUE);
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,
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);
535 initial_response_len = -1;
536 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
537 &initial_response_len);
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)),
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),
551 g_free (initial_response);
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);
573 /* ---------------------------------------------------------------------------------------------------- */
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
584 _g_dbus_auth_run_client (GDBusAuth *auth,
585 GDBusAuthObserver *observer,
586 GDBusCapabilityFlags offered_capabilities,
587 GDBusCapabilityFlags *out_negotiated_capabilities,
588 GCancellable *cancellable,
592 GDataInputStream *dis;
593 GDataOutputStream *dos;
594 GCredentials *credentials;
598 gchar **supported_auth_mechs;
599 GPtrArray *attempted_auth_mechs;
600 GDBusAuthMechanism *mech;
602 GDBusCapabilityFlags negotiated_capabilities;
604 debug_print ("CLIENT: initiating");
606 _g_dbus_auth_add_mechs (auth, observer);
609 supported_auth_mechs = NULL;
610 attempted_auth_mechs = g_ptr_array_new ();
612 negotiated_capabilities = 0;
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);
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),
633 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
637 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
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);
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 */
659 debug_print ("CLIENT: writing '%s'", s);
660 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
662 state = CLIENT_STATE_WAITING_FOR_REJECT;
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);
673 debug_print ("CLIENT: WaitingForReject, read '%s'", line);
676 if (!g_str_has_prefix (line, "REJECTED "))
681 "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
686 if (supported_auth_mechs == NULL)
688 supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 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]);
695 mech = client_choose_mech_and_send_initial_response (auth,
697 (const gchar* const *) supported_auth_mechs,
698 attempted_auth_mechs,
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;
707 state = CLIENT_STATE_WAITING_FOR_OK;
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);
715 debug_print ("CLIENT: WaitingForOK, read '%s'", line);
716 if (g_str_has_prefix (line, "OK "))
718 if (!g_dbus_is_guid (line + 3))
723 "Invalid OK response '%s'",
728 ret_guid = g_strdup (line + 3);
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))
737 state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
742 debug_print ("CLIENT: writing '%s'", s);
743 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
745 /* and we're done! */
749 else if (g_str_has_prefix (line, "REJECTED "))
751 goto choose_mechanism;
755 /* TODO: handle other valid responses */
759 "In WaitingForOk: unexpected response '%s'",
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);
771 debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line);
772 if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
775 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
777 debug_print ("CLIENT: writing '%s'", s);
778 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
780 /* and we're done! */
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);
788 debug_print ("CLIENT: writing '%s'", s);
789 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
791 /* and we're done! */
796 /* TODO: handle other valid responses */
800 "In WaitingForAgreeUnixFd: unexpected response '%s'",
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);
812 debug_print ("CLIENT: WaitingForData, read='%s'", line);
813 if (g_str_has_prefix (line, "DATA "))
817 gsize decoded_data_len = 0;
819 encoded = g_strdup (line + 5);
821 g_strstrip (encoded);
822 decoded_data = hexdecode (encoded, &decoded_data_len, error);
824 if (decoded_data == NULL)
826 g_prefix_error (error, "DATA response is malformed: ");
827 /* invalid encoding, disconnect! */
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)
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);
843 debug_print ("CLIENT: writing '%s'", s);
844 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
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
858 goto choose_mechanism;
865 "In WaitingForData: unexpected response '%s'",
873 g_assert_not_reached ();
877 }; /* main authentication client loop */
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)
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);
908 /* ---------------------------------------------------------------------------------------------------- */
911 get_auth_mechanisms (GDBusAuth *auth,
912 gboolean allow_anonymous,
915 const gchar *separator)
921 str = g_string_new (prefix);
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)
931 g_string_append (str, separator);
932 g_string_append (str, m->name);
936 g_string_append (str, suffix);
937 return g_string_free (str, FALSE);
943 SERVER_STATE_WAITING_FOR_AUTH,
944 SERVER_STATE_WAITING_FOR_DATA,
945 SERVER_STATE_WAITING_FOR_BEGIN
949 _g_dbus_auth_run_server (GDBusAuth *auth,
950 GDBusAuthObserver *observer,
952 gboolean allow_anonymous,
953 GDBusCapabilityFlags offered_capabilities,
954 GDBusCapabilityFlags *out_negotiated_capabilities,
955 GCredentials **out_received_credentials,
956 GCancellable *cancellable,
961 GDataInputStream *dis;
962 GDataOutputStream *dos;
967 GDBusAuthMechanism *mech;
969 GDBusCapabilityFlags negotiated_capabilities;
970 GCredentials *credentials;
972 debug_print ("SERVER: initiating");
974 _g_dbus_auth_add_mechs (auth, observer);
980 negotiated_capabilities = 0;
983 if (!g_dbus_is_guid (guid))
988 "The given guid '%s' is not valid",
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) */
1002 if (G_IS_UNIX_CONNECTION (auth->priv->stream))
1005 credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
1008 if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1010 g_propagate_error (error, local_error);
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);
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);
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);
1046 debug_print ("SERVER: didn't receive any credentials");
1049 state = SERVER_STATE_WAITING_FOR_AUTH;
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);
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))
1072 else if (g_str_has_prefix (line, "AUTH "))
1075 const gchar *encoded;
1076 const gchar *mech_name;
1077 GType auth_mech_to_use_gtype;
1079 tokens = g_strsplit (line, " ", 0);
1082 switch (g_strv_length (tokens))
1085 /* no initial response */
1086 mech_name = tokens[1];
1091 /* initial response */
1092 mech_name = tokens[1];
1093 encoded = tokens[2];
1100 "Unexpected line '%s' while in WaitingForAuth state",
1102 g_strfreev (tokens);
1106 /* TODO: record that the client has attempted to use this mechanism */
1107 //g_debug ("client is trying '%s'", mech_name);
1109 auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1110 if ((auth_mech_to_use_gtype == (GType) 0) ||
1111 (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1113 /* We don't support this auth mechanism */
1114 g_strfreev (tokens);
1115 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1116 debug_print ("SERVER: writing '%s'", s);
1117 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1124 /* stay in WAITING FOR AUTH */
1125 state = SERVER_STATE_WAITING_FOR_AUTH;
1129 gchar *initial_response;
1130 gsize initial_response_len;
1132 mech = g_object_new (auth_mech_to_use_gtype,
1133 "stream", auth->priv->stream,
1134 "credentials", credentials,
1137 initial_response = NULL;
1138 initial_response_len = 0;
1139 if (encoded != NULL)
1141 initial_response = hexdecode (encoded, &initial_response_len, error);
1142 if (initial_response == NULL)
1144 g_prefix_error (error, "Initial response is malformed: ");
1145 /* invalid encoding, disconnect! */
1146 g_strfreev (tokens);
1151 _g_dbus_auth_mechanism_server_initiate (mech,
1153 initial_response_len);
1154 g_free (initial_response);
1155 g_strfreev (tokens);
1158 switch (_g_dbus_auth_mechanism_server_get_state (mech))
1160 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1161 if (observer != NULL &&
1162 !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1167 g_set_error_literal (error,
1170 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1175 s = g_strdup_printf ("OK %s\r\n", guid);
1176 debug_print ("SERVER: writing '%s'", s);
1177 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1183 state = SERVER_STATE_WAITING_FOR_BEGIN;
1187 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1188 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1189 debug_print ("SERVER: writing '%s'", s);
1190 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1196 state = SERVER_STATE_WAITING_FOR_AUTH;
1199 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1200 state = SERVER_STATE_WAITING_FOR_DATA;
1203 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1207 gchar *encoded_data;
1208 data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1209 encoded_data = hexencode (data);
1210 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1211 g_free (encoded_data);
1213 debug_print ("SERVER: writing '%s'", s);
1214 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1226 g_assert_not_reached ();
1236 "Unexpected line '%s' while in WaitingForAuth state",
1243 case SERVER_STATE_WAITING_FOR_DATA:
1244 debug_print ("SERVER: WaitingForData");
1245 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1246 debug_print ("SERVER: WaitingForData, read '%s'", line);
1249 if (g_str_has_prefix (line, "DATA "))
1252 gchar *decoded_data;
1253 gsize decoded_data_len = 0;
1255 encoded = g_strdup (line + 5);
1257 g_strstrip (encoded);
1258 decoded_data = hexdecode (encoded, &decoded_data_len, error);
1260 if (decoded_data == NULL)
1262 g_prefix_error (error, "DATA response is malformed: ");
1263 /* invalid encoding, disconnect! */
1266 _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1267 g_free (decoded_data);
1268 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1276 "Unexpected line '%s' while in WaitingForData state",
1282 case SERVER_STATE_WAITING_FOR_BEGIN:
1283 debug_print ("SERVER: WaitingForBegin");
1284 /* Use extremely slow (but reliable) line reader - this basically
1285 * does a recvfrom() system call per character
1287 * (the problem with using GDataInputStream's read_line is that because of
1288 * buffering it might start reading into the first D-Bus message that
1289 * appears after "BEGIN\r\n"....)
1291 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1295 debug_print ("SERVER: WaitingForBegin, read '%s'", line);
1298 if (g_strcmp0 (line, "BEGIN") == 0)
1305 else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1308 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1310 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1311 s = "AGREE_UNIX_FD\r\n";
1312 debug_print ("SERVER: writing '%s'", s);
1313 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1318 s = "ERROR \"fd passing not offered\"\r\n";
1319 debug_print ("SERVER: writing '%s'", s);
1320 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1326 g_debug ("Unexpected line '%s' while in WaitingForBegin state", line);
1328 s = "ERROR \"Unknown Command\"\r\n";
1329 debug_print ("SERVER: writing '%s'", s);
1330 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1336 g_assert_not_reached ();
1342 g_set_error_literal (error,
1345 "Not implemented (server)");
1349 g_object_unref (mech);
1351 g_object_unref (dis);
1353 g_object_unref (dos);
1355 /* ensure return value is FALSE if error is set */
1356 if (error != NULL && *error != NULL)
1363 if (out_negotiated_capabilities != NULL)
1364 *out_negotiated_capabilities = negotiated_capabilities;
1365 if (out_received_credentials != NULL)
1366 *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1369 if (credentials != NULL)
1370 g_object_unref (credentials);
1372 debug_print ("SERVER: Done, authenticated=%d", ret);
1377 /* ---------------------------------------------------------------------------------------------------- */