1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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 "gdbusauth.h"
26 #include "gdbusauthmechanismanon.h"
27 #include "gdbusauthmechanismexternal.h"
28 #include "gdbusauthmechanismsha1.h"
30 #include "gdbusauthobserver.h"
32 #include "gdbuserror.h"
33 #include "gdbusutils.h"
34 #include "gioenumtypes.h"
35 #include "gcredentials.h"
36 #include "gdbusprivate.h"
39 #include <gio/gunixconnection.h>
40 #include "gunixcredentialsmessage.h"
41 #include <sys/types.h>
42 #include <sys/socket.h>
48 #define DEBUG_ENABLED 1
51 debug_print (const gchar *message, ...)
54 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
61 va_start (var_args, message);
62 s = g_strdup_vprintf (message, var_args);
65 str = g_string_new (NULL);
66 for (n = 0; s[n] != '\0'; n++)
68 if (G_UNLIKELY (s[n] == '\r'))
69 g_string_append (str, "\\r");
70 else if (G_UNLIKELY (s[n] == '\n'))
71 g_string_append (str, "\\n");
73 g_string_append_c (str, s[n]);
75 g_print ("GDBus-debug:Auth: %s\n", str->str);
76 g_string_free (str, TRUE);
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 (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_foreach (auth->priv->available_mechanisms, (GFunc) mechanism_free, NULL);
117 g_list_free (auth->priv->available_mechanisms);
119 if (G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize != NULL)
120 G_OBJECT_CLASS (_g_dbus_auth_parent_class)->finalize (object);
124 _g_dbus_auth_get_property (GObject *object,
129 GDBusAuth *auth = G_DBUS_AUTH (object);
134 g_value_set_object (value, auth->priv->stream);
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 _g_dbus_auth_set_property (GObject *object,
149 GDBusAuth *auth = G_DBUS_AUTH (object);
154 auth->priv->stream = g_value_dup_object (value);
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164 _g_dbus_auth_class_init (GDBusAuthClass *klass)
166 GObjectClass *gobject_class;
168 g_type_class_add_private (klass, sizeof (GDBusAuthPrivate));
170 gobject_class = G_OBJECT_CLASS (klass);
171 gobject_class->get_property = _g_dbus_auth_get_property;
172 gobject_class->set_property = _g_dbus_auth_set_property;
173 gobject_class->finalize = _g_dbus_auth_finalize;
175 g_object_class_install_property (gobject_class,
177 g_param_spec_object ("stream",
179 _("The underlying GIOStream used for I/O"),
183 G_PARAM_CONSTRUCT_ONLY |
184 G_PARAM_STATIC_NAME |
185 G_PARAM_STATIC_BLURB |
186 G_PARAM_STATIC_NICK));
190 mechanism_free (Mechanism *m)
196 add_mechanism (GDBusAuth *auth,
197 GType mechanism_type)
201 m = g_new0 (Mechanism, 1);
202 m->name = _g_dbus_auth_mechanism_get_name (mechanism_type);
203 m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
204 m->gtype = mechanism_type;
206 auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
210 mech_compare_func (Mechanism *a, Mechanism *b)
213 /* ensure deterministic order */
214 ret = b->priority - a->priority;
216 ret = g_strcmp0 (b->name, a->name);
221 _g_dbus_auth_init (GDBusAuth *auth)
223 auth->priv = G_TYPE_INSTANCE_GET_PRIVATE (auth, G_TYPE_DBUS_AUTH, GDBusAuthPrivate);
225 /* TODO: trawl extension points */
226 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
227 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
228 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
230 auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
231 (GCompareFunc) mech_compare_func);
235 find_mech_by_name (GDBusAuth *auth,
243 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
245 Mechanism *m = l->data;
246 if (g_strcmp0 (name, m->name) == 0)
258 _g_dbus_auth_new (GIOStream *stream)
260 return g_object_new (G_TYPE_DBUS_AUTH,
265 /* ---------------------------------------------------------------------------------------------------- */
266 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
268 _my_g_data_input_stream_read_line (GDataInputStream *dis,
269 gsize *out_line_length,
270 GCancellable *cancellable,
275 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
277 ret = g_data_input_stream_read_line (dis,
281 if (ret == NULL && error != NULL && *error == NULL)
283 g_set_error_literal (error,
286 _("Unexpected lack of content trying to read a line"));
292 /* This function is to avoid situations like this
294 * BEGIN\r\nl\0\0\1...
296 * e.g. where we read into the first D-Bus message while waiting for
297 * the final line from the client (TODO: file bug against gio for
301 _my_g_input_stream_read_line_safe (GInputStream *i,
302 gsize *out_line_length,
303 GCancellable *cancellable,
309 gboolean last_was_cr;
311 str = g_string_new (NULL);
316 num_read = g_input_stream_read (i,
325 if (error != NULL && *error == NULL)
327 g_set_error_literal (error,
330 _("Unexpected lack of content trying to (safely) read a line"));
335 g_string_append_c (str, (gint) c);
340 g_assert (str->len >= 2);
341 g_string_set_size (str, str->len - 2);
345 last_was_cr = (c == 0x0d);
349 if (out_line_length != NULL)
350 *out_line_length = str->len;
351 return g_string_free (str, FALSE);
354 g_assert (error == NULL || *error != NULL);
355 g_string_free (str, TRUE);
359 /* ---------------------------------------------------------------------------------------------------- */
362 append_nibble (GString *s, gint val)
364 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
368 hexdecode (const gchar *str,
377 s = g_string_new (NULL);
379 for (n = 0; str[n] != '\0'; n += 2)
385 upper_nibble = g_ascii_xdigit_value (str[n]);
386 lower_nibble = g_ascii_xdigit_value (str[n + 1]);
387 if (upper_nibble == -1 || lower_nibble == -1)
392 "Error hexdecoding string `%s' around position %d",
396 value = (upper_nibble<<4) | lower_nibble;
397 g_string_append_c (s, value);
400 ret = g_string_free (s, FALSE);
405 g_string_free (s, TRUE);
411 hexencode (const gchar *str)
416 s = g_string_new (NULL);
417 for (n = 0; str[n] != '\0'; n++)
423 val = ((const guchar *) str)[n];
424 upper_nibble = val >> 4;
425 lower_nibble = val & 0x0f;
427 append_nibble (s, upper_nibble);
428 append_nibble (s, lower_nibble);
431 return g_string_free (s, FALSE);
434 /* ---------------------------------------------------------------------------------------------------- */
436 static GDBusAuthMechanism *
437 client_choose_mech_and_send_initial_response (GDBusAuth *auth,
438 GCredentials *credentials_that_were_sent,
439 const gchar* const *supported_auth_mechs,
440 GPtrArray *attempted_auth_mechs,
441 GDataOutputStream *dos,
442 GCancellable *cancellable,
445 GDBusAuthMechanism *mech;
446 GType auth_mech_to_use_gtype;
449 gchar *initial_response;
450 gsize initial_response_len;
457 debug_print ("CLIENT: Trying to choose mechanism");
459 /* find an authentication mechanism to try, if any */
460 auth_mech_to_use_gtype = (GType) 0;
461 for (n = 0; supported_auth_mechs[n] != NULL; n++)
463 gboolean attempted_already;
464 attempted_already = FALSE;
465 for (m = 0; m < attempted_auth_mechs->len; m++)
467 if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
469 attempted_already = TRUE;
473 if (!attempted_already)
475 auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
476 if (auth_mech_to_use_gtype != (GType) 0)
481 if (auth_mech_to_use_gtype == (GType) 0)
487 debug_print ("CLIENT: Exhausted all available mechanisms");
489 available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
491 tried_str = g_string_new (NULL);
492 for (n = 0; n < attempted_auth_mechs->len; n++)
495 g_string_append (tried_str, ", ");
496 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
501 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
504 g_string_free (tried_str, TRUE);
509 /* OK, decided on a mechanism - let's do this thing */
510 mech = g_object_new (auth_mech_to_use_gtype,
511 "stream", auth->priv->stream,
512 "credentials", credentials_that_were_sent,
514 debug_print ("CLIENT: Trying mechanism `%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
515 g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
517 /* the auth mechanism may not be supported
518 * (for example, EXTERNAL only works if credentials were exchanged)
520 if (!_g_dbus_auth_mechanism_is_supported (mech))
522 debug_print ("CLIENT: Mechanism `%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
523 g_object_unref (mech);
528 initial_response_len = -1;
529 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
530 &initial_response_len);
532 g_printerr ("using auth mechanism with name `%s' of type `%s' with initial response `%s'\n",
533 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
534 g_type_name (G_TYPE_FROM_INSTANCE (mech)),
537 if (initial_response != NULL)
539 //g_printerr ("initial_response = `%s'\n", initial_response);
540 encoded = hexencode (initial_response);
541 s = g_strdup_printf ("AUTH %s %s\r\n",
542 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
544 g_free (initial_response);
549 s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
551 debug_print ("CLIENT: writing `%s'", s);
552 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
554 g_object_unref (mech);
566 /* ---------------------------------------------------------------------------------------------------- */
570 CLIENT_STATE_WAITING_FOR_DATA,
571 CLIENT_STATE_WAITING_FOR_OK,
572 CLIENT_STATE_WAITING_FOR_REJECT,
573 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
577 _g_dbus_auth_run_client (GDBusAuth *auth,
578 GDBusCapabilityFlags offered_capabilities,
579 GDBusCapabilityFlags *out_negotiated_capabilities,
580 GCancellable *cancellable,
584 GDataInputStream *dis;
585 GDataOutputStream *dos;
586 GCredentials *credentials;
590 gchar **supported_auth_mechs;
591 GPtrArray *attempted_auth_mechs;
592 GDBusAuthMechanism *mech;
594 GDBusCapabilityFlags negotiated_capabilities;
596 debug_print ("CLIENT: initiating");
599 supported_auth_mechs = NULL;
600 attempted_auth_mechs = g_ptr_array_new ();
602 negotiated_capabilities = 0;
605 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
606 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
608 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
611 if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
613 credentials = g_credentials_new_for_process ();
614 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 rountrips, 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_deny_authenticated_peer (observer,
1141 g_set_error_literal (error,
1144 _("Cancelled via GDBusAuthObserver::deny-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"