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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include "gdbusauth.h"
30 #include "gdbusauthmechanismanon.h"
31 #include "gdbusauthmechanismexternal.h"
32 #include "gdbusauthmechanismsha1.h"
33 #include "gdbusauthobserver.h"
35 #include "gdbuserror.h"
36 #include "gdbusutils.h"
37 #include "gioenumtypes.h"
38 #include "gcredentials.h"
39 #include "gdbusprivate.h"
42 #include "gunixconnection.h"
43 #include "gunixcredentialsmessage.h"
49 #define DEBUG_ENABLED 0
52 debug_print (const gchar *message, ...)
55 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
62 va_start (var_args, message);
63 s = g_strdup_vprintf (message, var_args);
66 str = g_string_new (NULL);
67 for (n = 0; s[n] != '\0'; n++)
69 if (G_UNLIKELY (s[n] == '\r'))
70 g_string_append (str, "\\r");
71 else if (G_UNLIKELY (s[n] == '\n'))
72 g_string_append (str, "\\n");
74 g_string_append_c (str, s[n]);
76 g_print ("GDBus-debug:Auth: %s\n", str->str);
77 g_string_free (str, TRUE);
90 static void mechanism_free (Mechanism *m);
92 struct _GDBusAuthPrivate
96 /* A list of available Mechanism, sorted according to priority */
97 GList *available_mechanisms;
106 G_DEFINE_TYPE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT);
108 /* ---------------------------------------------------------------------------------------------------- */
111 _g_dbus_auth_finalize (GObject *object)
113 GDBusAuth *auth = G_DBUS_AUTH (object);
115 if (auth->priv->stream != NULL)
116 g_object_unref (auth->priv->stream);
117 g_list_foreach (auth->priv->available_mechanisms, (GFunc) mechanism_free, NULL);
118 g_list_free (auth->priv->available_mechanisms);
120 if (G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize != NULL)
121 G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize (object);
125 _g_dbus_auth_get_property (GObject *object,
130 GDBusAuth *auth = G_DBUS_AUTH (object);
135 g_value_set_object (value, auth->priv->stream);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 _g_dbus_auth_set_property (GObject *object,
150 GDBusAuth *auth = G_DBUS_AUTH (object);
155 auth->priv->stream = g_value_dup_object (value);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 _g_dbus_auth_class_init (GDBusAuthClass *klass)
167 GObjectClass *gobject_class;
169 g_type_class_add_private (klass, sizeof (GDBusAuthPrivate));
171 gobject_class = G_OBJECT_CLASS (klass);
172 gobject_class->get_property = _g_dbus_auth_get_property;
173 gobject_class->set_property = _g_dbus_auth_set_property;
174 gobject_class->finalize = _g_dbus_auth_finalize;
176 g_object_class_install_property (gobject_class,
178 g_param_spec_object ("stream",
180 P_("The underlying GIOStream used for I/O"),
184 G_PARAM_CONSTRUCT_ONLY |
185 G_PARAM_STATIC_NAME |
186 G_PARAM_STATIC_BLURB |
187 G_PARAM_STATIC_NICK));
191 mechanism_free (Mechanism *m)
197 add_mechanism (GDBusAuth *auth,
198 GType mechanism_type)
202 m = g_new0 (Mechanism, 1);
203 m->name = _g_dbus_auth_mechanism_get_name (mechanism_type);
204 m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
205 m->gtype = mechanism_type;
207 auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
211 mech_compare_func (Mechanism *a, Mechanism *b)
214 /* ensure deterministic order */
215 ret = b->priority - a->priority;
217 ret = g_strcmp0 (b->name, a->name);
222 _g_dbus_auth_init (GDBusAuth *auth)
224 auth->priv = G_TYPE_INSTANCE_GET_PRIVATE (auth, G_TYPE_DBUS_AUTH, GDBusAuthPrivate);
226 /* TODO: trawl extension points */
227 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
228 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
229 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
231 auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
232 (GCompareFunc) mech_compare_func);
236 find_mech_by_name (GDBusAuth *auth,
244 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
246 Mechanism *m = l->data;
247 if (g_strcmp0 (name, m->name) == 0)
259 _g_dbus_auth_new (GIOStream *stream)
261 return g_object_new (G_TYPE_DBUS_AUTH,
266 /* ---------------------------------------------------------------------------------------------------- */
267 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
269 _my_g_data_input_stream_read_line (GDataInputStream *dis,
270 gsize *out_line_length,
271 GCancellable *cancellable,
276 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
278 ret = g_data_input_stream_read_line (dis,
282 if (ret == NULL && error != NULL && *error == NULL)
284 g_set_error_literal (error,
287 _("Unexpected lack of content trying to read a line"));
293 /* This function is to avoid situations like this
295 * BEGIN\r\nl\0\0\1...
297 * e.g. where we read into the first D-Bus message while waiting for
298 * the final line from the client (TODO: file bug against gio for
302 _my_g_input_stream_read_line_safe (GInputStream *i,
303 gsize *out_line_length,
304 GCancellable *cancellable,
310 gboolean last_was_cr;
312 str = g_string_new (NULL);
317 num_read = g_input_stream_read (i,
326 if (error != NULL && *error == NULL)
328 g_set_error_literal (error,
331 _("Unexpected lack of content trying to (safely) read a line"));
336 g_string_append_c (str, (gint) c);
341 g_assert (str->len >= 2);
342 g_string_set_size (str, str->len - 2);
346 last_was_cr = (c == 0x0d);
350 if (out_line_length != NULL)
351 *out_line_length = str->len;
352 return g_string_free (str, FALSE);
355 g_assert (error == NULL || *error != NULL);
356 g_string_free (str, TRUE);
360 /* ---------------------------------------------------------------------------------------------------- */
363 append_nibble (GString *s, gint val)
365 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
369 hexdecode (const gchar *str,
378 s = g_string_new (NULL);
380 for (n = 0; str[n] != '\0'; n += 2)
386 upper_nibble = g_ascii_xdigit_value (str[n]);
387 lower_nibble = g_ascii_xdigit_value (str[n + 1]);
388 if (upper_nibble == -1 || lower_nibble == -1)
393 "Error hexdecoding string `%s' around position %d",
397 value = (upper_nibble<<4) | lower_nibble;
398 g_string_append_c (s, value);
401 ret = g_string_free (s, FALSE);
406 g_string_free (s, TRUE);
412 hexencode (const gchar *str)
417 s = g_string_new (NULL);
418 for (n = 0; str[n] != '\0'; n++)
424 val = ((const guchar *) str)[n];
425 upper_nibble = val >> 4;
426 lower_nibble = val & 0x0f;
428 append_nibble (s, upper_nibble);
429 append_nibble (s, lower_nibble);
432 return g_string_free (s, FALSE);
435 /* ---------------------------------------------------------------------------------------------------- */
437 static GDBusAuthMechanism *
438 client_choose_mech_and_send_initial_response (GDBusAuth *auth,
439 GCredentials *credentials_that_were_sent,
440 const gchar* const *supported_auth_mechs,
441 GPtrArray *attempted_auth_mechs,
442 GDataOutputStream *dos,
443 GCancellable *cancellable,
446 GDBusAuthMechanism *mech;
447 GType auth_mech_to_use_gtype;
450 gchar *initial_response;
451 gsize initial_response_len;
458 debug_print ("CLIENT: Trying to choose mechanism");
460 /* find an authentication mechanism to try, if any */
461 auth_mech_to_use_gtype = (GType) 0;
462 for (n = 0; supported_auth_mechs[n] != NULL; n++)
464 gboolean attempted_already;
465 attempted_already = FALSE;
466 for (m = 0; m < attempted_auth_mechs->len; m++)
468 if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
470 attempted_already = TRUE;
474 if (!attempted_already)
476 auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
477 if (auth_mech_to_use_gtype != (GType) 0)
482 if (auth_mech_to_use_gtype == (GType) 0)
488 debug_print ("CLIENT: Exhausted all available mechanisms");
490 available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
492 tried_str = g_string_new (NULL);
493 for (n = 0; n < attempted_auth_mechs->len; n++)
496 g_string_append (tried_str, ", ");
497 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
502 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
505 g_string_free (tried_str, TRUE);
510 /* OK, decided on a mechanism - let's do this thing */
511 mech = g_object_new (auth_mech_to_use_gtype,
512 "stream", auth->priv->stream,
513 "credentials", credentials_that_were_sent,
515 debug_print ("CLIENT: Trying mechanism `%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
516 g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
518 /* the auth mechanism may not be supported
519 * (for example, EXTERNAL only works if credentials were exchanged)
521 if (!_g_dbus_auth_mechanism_is_supported (mech))
523 debug_print ("CLIENT: Mechanism `%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
524 g_object_unref (mech);
529 initial_response_len = -1;
530 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
531 &initial_response_len);
533 g_printerr ("using auth mechanism with name `%s' of type `%s' with initial response `%s'\n",
534 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
535 g_type_name (G_TYPE_FROM_INSTANCE (mech)),
538 if (initial_response != NULL)
540 //g_printerr ("initial_response = `%s'\n", initial_response);
541 encoded = hexencode (initial_response);
542 s = g_strdup_printf ("AUTH %s %s\r\n",
543 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
545 g_free (initial_response);
550 s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
552 debug_print ("CLIENT: writing `%s'", s);
553 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
555 g_object_unref (mech);
567 /* ---------------------------------------------------------------------------------------------------- */
571 CLIENT_STATE_WAITING_FOR_DATA,
572 CLIENT_STATE_WAITING_FOR_OK,
573 CLIENT_STATE_WAITING_FOR_REJECT,
574 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
578 _g_dbus_auth_run_client (GDBusAuth *auth,
579 GDBusCapabilityFlags offered_capabilities,
580 GDBusCapabilityFlags *out_negotiated_capabilities,
581 GCancellable *cancellable,
585 GDataInputStream *dis;
586 GDataOutputStream *dos;
587 GCredentials *credentials;
591 gchar **supported_auth_mechs;
592 GPtrArray *attempted_auth_mechs;
593 GDBusAuthMechanism *mech;
595 GDBusCapabilityFlags negotiated_capabilities;
597 debug_print ("CLIENT: initiating");
600 supported_auth_mechs = NULL;
601 attempted_auth_mechs = g_ptr_array_new ();
603 negotiated_capabilities = 0;
606 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
607 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
609 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
612 if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
614 credentials = g_credentials_new ();
615 if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
622 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
626 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
630 if (credentials != NULL)
632 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
634 s = g_credentials_to_string (credentials);
635 debug_print ("CLIENT: sent credentials `%s'", s);
641 debug_print ("CLIENT: didn't send any credentials");
644 /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
646 /* Get list of supported authentication mechanisms */
648 debug_print ("CLIENT: writing `%s'", s);
649 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
651 state = CLIENT_STATE_WAITING_FOR_REJECT;
657 case CLIENT_STATE_WAITING_FOR_REJECT:
658 debug_print ("CLIENT: WaitingForReject");
659 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
662 debug_print ("CLIENT: WaitingForReject, read '%s'", line);
664 if (!g_str_has_prefix (line, "REJECTED "))
669 "In WaitingForReject: Expected `REJECTED am1 am2 ... amN', got `%s'",
674 if (supported_auth_mechs == NULL)
676 supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 0);
678 for (n = 0; supported_auth_mechs != NULL && supported_auth_mechs[n] != NULL; n++)
679 g_printerr ("supported_auth_mechs[%d] = `%s'\n", n, supported_auth_mechs[n]);
683 mech = client_choose_mech_and_send_initial_response (auth,
685 (const gchar* const *) supported_auth_mechs,
686 attempted_auth_mechs,
692 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA)
693 state = CLIENT_STATE_WAITING_FOR_DATA;
695 state = CLIENT_STATE_WAITING_FOR_OK;
698 case CLIENT_STATE_WAITING_FOR_OK:
699 debug_print ("CLIENT: WaitingForOK");
700 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
703 debug_print ("CLIENT: WaitingForOK, read `%s'", line);
704 if (g_str_has_prefix (line, "OK "))
706 if (!g_dbus_is_guid (line + 3))
711 "Invalid OK response `%s'",
716 ret_guid = g_strdup (line + 3);
719 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
721 s = "NEGOTIATE_UNIX_FD\r\n";
722 debug_print ("CLIENT: writing `%s'", s);
723 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
725 state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
730 debug_print ("CLIENT: writing `%s'", s);
731 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
733 /* and we're done! */
737 else if (g_str_has_prefix (line, "REJECTED "))
743 /* TODO: handle other valid responses */
747 "In WaitingForOk: unexpected response `%s'",
754 case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD:
755 debug_print ("CLIENT: WaitingForAgreeUnixFD");
756 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
759 debug_print ("CLIENT: WaitingForAgreeUnixFD, read=`%s'", line);
760 if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
762 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
764 debug_print ("CLIENT: writing `%s'", s);
765 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
767 /* and we're done! */
770 else if (g_str_has_prefix (line, "ERROR") && (line[5] == 0 || g_ascii_isspace (line[5])))
772 //g_strstrip (line + 5); g_debug ("bah, no unix_fd: `%s'", line + 5);
775 debug_print ("CLIENT: writing `%s'", s);
776 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
778 /* and we're done! */
783 /* TODO: handle other valid responses */
787 "In WaitingForAgreeUnixFd: unexpected response `%s'",
794 case CLIENT_STATE_WAITING_FOR_DATA:
795 debug_print ("CLIENT: WaitingForData");
796 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
799 debug_print ("CLIENT: WaitingForData, read=`%s'", line);
800 if (g_str_has_prefix (line, "DATA "))
804 gsize decoded_data_len;
806 encoded = g_strdup (line + 5);
808 g_strstrip (encoded);
809 decoded_data = hexdecode (encoded, &decoded_data_len, error);
811 if (decoded_data == NULL)
813 g_prefix_error (error, "DATA response is malformed: ");
814 /* invalid encoding, disconnect! */
817 _g_dbus_auth_mechanism_client_data_receive (mech, decoded_data, decoded_data_len);
818 g_free (decoded_data);
820 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND)
825 data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
826 encoded_data = hexencode (data);
827 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
828 g_free (encoded_data);
830 debug_print ("CLIENT: writing `%s'", s);
831 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
838 state = CLIENT_STATE_WAITING_FOR_OK;
845 "In WaitingForData: unexpected response `%s'",
853 g_assert_not_reached ();
857 }; /* main authentication client loop */
861 g_object_unref (mech);
862 g_ptr_array_unref (attempted_auth_mechs);
863 g_strfreev (supported_auth_mechs);
867 /* ensure return value is NULL if error is set */
868 if (error != NULL && *error != NULL)
874 if (ret_guid != NULL)
876 if (out_negotiated_capabilities != NULL)
877 *out_negotiated_capabilities = negotiated_capabilities;
880 if (credentials != NULL)
881 g_object_unref (credentials);
883 debug_print ("CLIENT: Done, authenticated=%d", ret_guid != NULL);
888 /* ---------------------------------------------------------------------------------------------------- */
891 get_auth_mechanisms (GDBusAuth *auth,
892 gboolean allow_anonymous,
895 const gchar *separator)
901 str = g_string_new (prefix);
903 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
905 Mechanism *m = l->data;
907 if (!allow_anonymous && g_strcmp0 (m->name, "ANONYMOUS") == 0)
911 g_string_append (str, separator);
912 g_string_append (str, m->name);
916 g_string_append (str, suffix);
917 return g_string_free (str, FALSE);
923 SERVER_STATE_WAITING_FOR_AUTH,
924 SERVER_STATE_WAITING_FOR_DATA,
925 SERVER_STATE_WAITING_FOR_BEGIN
929 _g_dbus_auth_run_server (GDBusAuth *auth,
930 GDBusAuthObserver *observer,
932 gboolean allow_anonymous,
933 GDBusCapabilityFlags offered_capabilities,
934 GDBusCapabilityFlags *out_negotiated_capabilities,
935 GCredentials **out_received_credentials,
936 GCancellable *cancellable,
941 GDataInputStream *dis;
942 GDataOutputStream *dos;
947 GDBusAuthMechanism *mech;
949 GDBusCapabilityFlags negotiated_capabilities;
950 GCredentials *credentials;
952 debug_print ("SERVER: initiating");
958 negotiated_capabilities = 0;
961 if (!g_dbus_is_guid (guid))
966 "The given guid `%s' is not valid",
971 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
972 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
974 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
976 /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */
978 if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
981 credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
984 if (credentials == NULL)
986 g_propagate_error (error, local_error);
993 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
994 if (local_error != NULL)
996 g_propagate_error (error, local_error);
1002 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
1003 if (local_error != NULL)
1005 g_propagate_error (error, local_error);
1009 if (credentials != NULL)
1011 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1013 s = g_credentials_to_string (credentials);
1014 debug_print ("SERVER: received credentials `%s'", s);
1020 debug_print ("SERVER: didn't receive any credentials");
1023 state = SERVER_STATE_WAITING_FOR_AUTH;
1028 case SERVER_STATE_WAITING_FOR_AUTH:
1029 debug_print ("SERVER: WaitingForAuth");
1030 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1031 debug_print ("SERVER: WaitingForAuth, read `%s'", line);
1034 if (g_strcmp0 (line, "AUTH") == 0)
1036 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1037 debug_print ("SERVER: writing `%s'", s);
1038 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1046 else if (g_str_has_prefix (line, "AUTH "))
1049 const gchar *encoded;
1050 const gchar *mech_name;
1051 GType auth_mech_to_use_gtype;
1053 tokens = g_strsplit (line, " ", 0);
1056 switch (g_strv_length (tokens))
1059 /* no initial response */
1060 mech_name = tokens[1];
1065 /* initial response */
1066 mech_name = tokens[1];
1067 encoded = tokens[2];
1074 "Unexpected line `%s' while in WaitingForAuth state",
1076 g_strfreev (tokens);
1080 /* TODO: record that the client has attempted to use this mechanism */
1081 //g_debug ("client is trying `%s'", mech_name);
1083 auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1084 if ((auth_mech_to_use_gtype == (GType) 0) ||
1085 (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1087 /* We don't support this auth mechanism */
1088 g_strfreev (tokens);
1089 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1090 debug_print ("SERVER: writing `%s'", s);
1091 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1098 /* stay in WAITING FOR AUTH */
1099 state = SERVER_STATE_WAITING_FOR_AUTH;
1103 gchar *initial_response;
1104 gsize initial_response_len;
1106 mech = g_object_new (auth_mech_to_use_gtype,
1107 "stream", auth->priv->stream,
1108 "credentials", credentials,
1111 initial_response = NULL;
1112 initial_response_len = 0;
1113 if (encoded != NULL)
1115 initial_response = hexdecode (encoded, &initial_response_len, error);
1116 if (initial_response == NULL)
1118 g_prefix_error (error, "Initial response is malformed: ");
1119 /* invalid encoding, disconnect! */
1120 g_strfreev (tokens);
1125 _g_dbus_auth_mechanism_server_initiate (mech,
1127 initial_response_len);
1128 g_free (initial_response);
1129 g_strfreev (tokens);
1132 switch (_g_dbus_auth_mechanism_server_get_state (mech))
1134 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1135 if (observer != NULL &&
1136 !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1141 g_set_error_literal (error,
1144 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1149 s = g_strdup_printf ("OK %s\r\n", guid);
1150 debug_print ("SERVER: writing `%s'", s);
1151 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1157 state = SERVER_STATE_WAITING_FOR_BEGIN;
1161 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1162 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1163 debug_print ("SERVER: writing `%s'", s);
1164 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1170 state = SERVER_STATE_WAITING_FOR_AUTH;
1173 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1174 state = SERVER_STATE_WAITING_FOR_DATA;
1177 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1181 gchar *encoded_data;
1182 data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1183 encoded_data = hexencode (data);
1184 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1185 g_free (encoded_data);
1187 debug_print ("SERVER: writing `%s'", s);
1188 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1200 g_assert_not_reached ();
1210 "Unexpected line `%s' while in WaitingForAuth state",
1217 case SERVER_STATE_WAITING_FOR_DATA:
1218 debug_print ("SERVER: WaitingForData");
1219 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1220 debug_print ("SERVER: WaitingForData, read `%s'", line);
1223 if (g_str_has_prefix (line, "DATA "))
1226 gchar *decoded_data;
1227 gsize decoded_data_len;
1229 encoded = g_strdup (line + 5);
1231 g_strstrip (encoded);
1232 decoded_data = hexdecode (encoded, &decoded_data_len, error);
1234 if (decoded_data == NULL)
1236 g_prefix_error (error, "DATA response is malformed: ");
1237 /* invalid encoding, disconnect! */
1240 _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1241 g_free (decoded_data);
1242 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1250 "Unexpected line `%s' while in WaitingForData state",
1256 case SERVER_STATE_WAITING_FOR_BEGIN:
1257 debug_print ("SERVER: WaitingForBegin");
1258 /* Use extremely slow (but reliable) line reader - this basically
1259 * does a recvfrom() system call per character
1261 * (the problem with using GDataInputStream's read_line is that because of
1262 * buffering it might start reading into the first D-Bus message that
1263 * appears after "BEGIN\r\n"....)
1265 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1269 debug_print ("SERVER: WaitingForBegin, read `%s'", line);
1272 if (g_strcmp0 (line, "BEGIN") == 0)
1279 else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1281 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1283 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1284 s = "AGREE_UNIX_FD\r\n";
1285 debug_print ("SERVER: writing `%s'", s);
1286 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1291 s = "ERROR \"fd passing not offered\"\r\n";
1292 debug_print ("SERVER: writing `%s'", s);
1293 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1299 g_debug ("Unexpected line `%s' while in WaitingForBegin state", line);
1301 s = "ERROR \"Unknown Command\"\r\n";
1302 debug_print ("SERVER: writing `%s'", s);
1303 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1309 g_assert_not_reached ();
1315 g_set_error_literal (error,
1318 "Not implemented (server)");
1322 g_object_unref (mech);
1328 /* ensure return value is FALSE if error is set */
1329 if (error != NULL && *error != NULL)
1336 if (out_negotiated_capabilities != NULL)
1337 *out_negotiated_capabilities = negotiated_capabilities;
1338 if (out_received_credentials != NULL)
1339 *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1342 if (credentials != NULL)
1343 g_object_unref (credentials);
1345 debug_print ("SERVER: Done, authenticated=%d", ret);
1350 /* ---------------------------------------------------------------------------------------------------- */
1352 #define __G_DBUS_AUTH_C__
1353 #include "gioaliasdef.c"