1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusauth.h"
27 #include "gdbusauthmechanismanon.h"
28 #include "gdbusauthmechanismexternal.h"
29 #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 "gcredentialsprivate.h"
37 #include "gdbusprivate.h"
38 #include "giostream.h"
39 #include "gdatainputstream.h"
40 #include "gdataoutputstream.h"
42 #include "gnetworking.h"
43 #include "gunixconnection.h"
44 #include "gunixcredentialsmessage.h"
50 debug_print (const gchar *message, ...)
52 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
59 _g_dbus_debug_print_lock ();
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);
79 _g_dbus_debug_print_unlock ();
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_WITH_PRIVATE (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_free_full (auth->priv->available_mechanisms, (GDestroyNotify) mechanism_free);
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 gobject_class = G_OBJECT_CLASS (klass);
169 gobject_class->get_property = _g_dbus_auth_get_property;
170 gobject_class->set_property = _g_dbus_auth_set_property;
171 gobject_class->finalize = _g_dbus_auth_finalize;
173 g_object_class_install_property (gobject_class,
175 g_param_spec_object ("stream", NULL, NULL,
179 G_PARAM_CONSTRUCT_ONLY |
180 G_PARAM_STATIC_NAME |
181 G_PARAM_STATIC_BLURB |
182 G_PARAM_STATIC_NICK));
186 mechanism_free (Mechanism *m)
192 add_mechanism (GDBusAuth *auth,
193 GDBusAuthObserver *observer,
194 GType mechanism_type)
198 name = _g_dbus_auth_mechanism_get_name (mechanism_type);
199 if (observer == NULL || g_dbus_auth_observer_allow_mechanism (observer, name))
202 m = g_new0 (Mechanism, 1);
204 m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
205 m->gtype = mechanism_type;
206 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_dbus_auth_get_instance_private (auth);
228 _g_dbus_auth_add_mechs (GDBusAuth *auth,
229 GDBusAuthObserver *observer)
231 /* TODO: trawl extension points */
232 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
233 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
234 add_mechanism (auth, observer, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
236 auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
237 (GCompareFunc) mech_compare_func);
241 find_mech_by_name (GDBusAuth *auth,
249 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
251 Mechanism *m = l->data;
252 if (g_strcmp0 (name, m->name) == 0)
264 _g_dbus_auth_new (GIOStream *stream)
266 return g_object_new (G_TYPE_DBUS_AUTH,
271 /* ---------------------------------------------------------------------------------------------------- */
272 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
274 _my_g_data_input_stream_read_line (GDataInputStream *dis,
275 gsize *out_line_length,
276 GCancellable *cancellable,
281 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
283 ret = g_data_input_stream_read_line (dis,
287 if (ret == NULL && error != NULL && *error == NULL)
289 g_set_error_literal (error,
292 _("Unexpected lack of content trying to read a line"));
298 /* This function is to avoid situations like this
300 * BEGIN\r\nl\0\0\1...
302 * e.g. where we read into the first D-Bus message while waiting for
303 * the final line from the client (TODO: file bug against gio for
307 _my_g_input_stream_read_line_safe (GInputStream *i,
308 gsize *out_line_length,
309 GCancellable *cancellable,
315 gboolean last_was_cr;
317 str = g_string_new (NULL);
322 num_read = g_input_stream_read (i,
331 if (error != NULL && *error == NULL)
333 g_set_error_literal (error,
336 _("Unexpected lack of content trying to (safely) read a line"));
341 g_string_append_c (str, (gint) c);
346 g_assert (str->len >= 2);
347 g_string_set_size (str, str->len - 2);
351 last_was_cr = (c == 0x0d);
355 if (out_line_length != NULL)
356 *out_line_length = str->len;
357 return g_string_free (str, FALSE);
360 g_assert (error == NULL || *error != NULL);
361 g_string_free (str, TRUE);
365 /* ---------------------------------------------------------------------------------------------------- */
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);
401 ret = g_string_free (s, FALSE);
408 g_string_free (s, TRUE);
413 /* ---------------------------------------------------------------------------------------------------- */
415 static GDBusAuthMechanism *
416 client_choose_mech_and_send_initial_response (GDBusAuth *auth,
417 GCredentials *credentials_that_were_sent,
418 GDBusConnectionFlags conn_flags,
419 const gchar* const *supported_auth_mechs,
420 GPtrArray *attempted_auth_mechs,
421 GDataOutputStream *dos,
422 GCancellable *cancellable,
425 GDBusAuthMechanism *mech;
426 GType auth_mech_to_use_gtype;
429 gchar *initial_response;
430 gsize initial_response_len;
437 debug_print ("CLIENT: Trying to choose mechanism");
439 /* find an authentication mechanism to try, if any */
440 auth_mech_to_use_gtype = (GType) 0;
441 for (n = 0; supported_auth_mechs[n] != NULL; n++)
443 gboolean attempted_already;
444 attempted_already = FALSE;
445 for (m = 0; m < attempted_auth_mechs->len; m++)
447 if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
449 attempted_already = TRUE;
453 if (!attempted_already)
455 auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
456 if (auth_mech_to_use_gtype != (GType) 0)
461 if (auth_mech_to_use_gtype == (GType) 0)
466 debug_print ("CLIENT: Exhausted all available mechanisms");
468 available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
470 tried_str = g_string_new (NULL);
471 for (n = 0; n < attempted_auth_mechs->len; n++)
474 g_string_append (tried_str, ", ");
475 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
480 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
483 g_string_free (tried_str, TRUE);
488 /* OK, decided on a mechanism - let's do this thing */
489 mech = g_object_new (auth_mech_to_use_gtype,
490 "stream", auth->priv->stream,
491 "credentials", credentials_that_were_sent,
493 debug_print ("CLIENT: Trying mechanism '%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
494 g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
496 /* the auth mechanism may not be supported
497 * (for example, EXTERNAL only works if credentials were exchanged)
499 if (!_g_dbus_auth_mechanism_is_supported (mech))
501 debug_print ("CLIENT: Mechanism '%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
502 g_object_unref (mech);
507 initial_response_len = 0;
508 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
510 &initial_response_len);
512 g_printerr ("using auth mechanism with name '%s' of type '%s' with initial response '%s'\n",
513 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
514 g_type_name (G_TYPE_FROM_INSTANCE (mech)),
517 if (initial_response != NULL)
519 //g_printerr ("initial_response = '%s'\n", initial_response);
520 encoded = _g_dbus_hexencode (initial_response, initial_response_len);
521 s = g_strdup_printf ("AUTH %s %s\r\n",
522 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
524 g_free (initial_response);
529 s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
531 debug_print ("CLIENT: writing '%s'", s);
532 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
534 g_object_unref (mech);
546 /* ---------------------------------------------------------------------------------------------------- */
550 CLIENT_STATE_WAITING_FOR_DATA,
551 CLIENT_STATE_WAITING_FOR_OK,
552 CLIENT_STATE_WAITING_FOR_REJECT,
553 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
557 _g_dbus_auth_run_client (GDBusAuth *auth,
558 GDBusAuthObserver *observer,
559 GDBusConnectionFlags conn_flags,
560 GDBusCapabilityFlags offered_capabilities,
561 GDBusCapabilityFlags *out_negotiated_capabilities,
562 GCancellable *cancellable,
566 GDataInputStream *dis;
567 GDataOutputStream *dos;
568 GCredentials *credentials;
572 gchar **supported_auth_mechs;
573 GPtrArray *attempted_auth_mechs;
574 GDBusAuthMechanism *mech;
576 GDBusCapabilityFlags negotiated_capabilities;
578 g_return_val_if_fail ((conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT), NULL);
579 g_return_val_if_fail (!(conn_flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER), NULL);
581 debug_print ("CLIENT: initiating");
583 _g_dbus_auth_add_mechs (auth, observer);
586 supported_auth_mechs = NULL;
587 attempted_auth_mechs = g_ptr_array_new ();
589 negotiated_capabilities = 0;
592 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
593 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
594 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
595 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
597 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
600 if (G_IS_UNIX_CONNECTION (auth->priv->stream))
602 credentials = g_credentials_new ();
603 if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
610 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
614 if (!g_data_output_stream_put_byte (dos, '\0', cancellable, error))
618 if (credentials != NULL)
620 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
622 s = g_credentials_to_string (credentials);
623 debug_print ("CLIENT: sent credentials '%s'", s);
629 debug_print ("CLIENT: didn't send any credentials");
632 /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
634 /* Get list of supported authentication mechanisms */
636 debug_print ("CLIENT: writing '%s'", s);
637 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
639 state = CLIENT_STATE_WAITING_FOR_REJECT;
645 case CLIENT_STATE_WAITING_FOR_REJECT:
646 debug_print ("CLIENT: WaitingForReject");
647 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
650 debug_print ("CLIENT: WaitingForReject, read '%s'", line);
653 if (!g_str_has_prefix (line, "REJECTED "))
658 "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
663 if (supported_auth_mechs == NULL)
665 supported_auth_mechs = g_strsplit (line + sizeof ("REJECTED ") - 1, " ", 0);
667 for (n = 0; supported_auth_mechs != NULL && supported_auth_mechs[n] != NULL; n++)
668 g_printerr ("supported_auth_mechs[%d] = '%s'\n", n, supported_auth_mechs[n]);
672 mech = client_choose_mech_and_send_initial_response (auth,
675 (const gchar* const *) supported_auth_mechs,
676 attempted_auth_mechs,
682 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA)
683 state = CLIENT_STATE_WAITING_FOR_DATA;
685 state = CLIENT_STATE_WAITING_FOR_OK;
688 case CLIENT_STATE_WAITING_FOR_OK:
689 debug_print ("CLIENT: WaitingForOK");
690 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
693 debug_print ("CLIENT: WaitingForOK, read '%s'", line);
694 if (g_str_has_prefix (line, "OK "))
696 if (!g_dbus_is_guid (line + 3))
701 "Invalid OK response '%s'",
706 ret_guid = g_strdup (line + 3);
709 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
711 s = "NEGOTIATE_UNIX_FD\r\n";
712 debug_print ("CLIENT: writing '%s'", s);
713 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
715 state = CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD;
720 debug_print ("CLIENT: writing '%s'", s);
721 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
723 /* and we're done! */
727 else if (g_str_has_prefix (line, "REJECTED "))
729 goto choose_mechanism;
733 /* TODO: handle other valid responses */
737 "In WaitingForOk: unexpected response '%s'",
744 case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD:
745 debug_print ("CLIENT: WaitingForAgreeUnixFD");
746 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
749 debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line);
750 if (g_strcmp0 (line, "AGREE_UNIX_FD") == 0)
753 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
755 debug_print ("CLIENT: writing '%s'", s);
756 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
758 /* and we're done! */
761 else if (g_str_has_prefix (line, "ERROR") && (line[5] == 0 || g_ascii_isspace (line[5])))
763 //g_strstrip (line + 5); g_debug ("bah, no unix_fd: '%s'", line + 5);
766 debug_print ("CLIENT: writing '%s'", s);
767 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
769 /* and we're done! */
774 /* TODO: handle other valid responses */
778 "In WaitingForAgreeUnixFd: unexpected response '%s'",
785 case CLIENT_STATE_WAITING_FOR_DATA:
786 debug_print ("CLIENT: WaitingForData");
787 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
790 debug_print ("CLIENT: WaitingForData, read='%s'", line);
791 if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
795 gsize decoded_data_len = 0;
797 encoded = g_strdup (line + 4);
799 g_strstrip (encoded);
800 decoded_data = hexdecode (encoded, &decoded_data_len, error);
802 if (decoded_data == NULL)
804 g_prefix_error (error, "DATA response is malformed: ");
805 /* invalid encoding, disconnect! */
808 _g_dbus_auth_mechanism_client_data_receive (mech, decoded_data, decoded_data_len);
809 g_free (decoded_data);
811 if (_g_dbus_auth_mechanism_client_get_state (mech) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND)
816 data = _g_dbus_auth_mechanism_client_data_send (mech, &data_len);
820 s = g_strdup ("DATA\r\n");
824 gchar *encoded_data = _g_dbus_hexencode (data, data_len);
826 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
827 g_free (encoded_data);
831 debug_print ("CLIENT: writing '%s'", s);
832 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
839 state = CLIENT_STATE_WAITING_FOR_OK;
841 else if (g_str_has_prefix (line, "REJECTED "))
843 /* could be the chosen authentication method just doesn't work. Try
846 goto choose_mechanism;
853 "In WaitingForData: unexpected response '%s'",
861 g_assert_not_reached ();
865 }; /* main authentication client loop */
869 g_object_unref (mech);
870 g_ptr_array_unref (attempted_auth_mechs);
871 g_strfreev (supported_auth_mechs);
872 g_object_unref (dis);
873 g_object_unref (dos);
875 /* ensure return value is NULL if error is set */
876 if (error != NULL && *error != NULL)
882 if (ret_guid != NULL)
884 if (out_negotiated_capabilities != NULL)
885 *out_negotiated_capabilities = negotiated_capabilities;
888 if (credentials != NULL)
889 g_object_unref (credentials);
891 debug_print ("CLIENT: Done, authenticated=%d", ret_guid != NULL);
896 /* ---------------------------------------------------------------------------------------------------- */
899 get_auth_mechanisms (GDBusAuth *auth,
900 gboolean allow_anonymous,
903 const gchar *separator)
909 str = g_string_new (prefix);
911 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
913 Mechanism *m = l->data;
915 if (!allow_anonymous && g_strcmp0 (m->name, "ANONYMOUS") == 0)
919 g_string_append (str, separator);
920 g_string_append (str, m->name);
924 g_string_append (str, suffix);
925 return g_string_free (str, FALSE);
931 SERVER_STATE_WAITING_FOR_AUTH,
932 SERVER_STATE_WAITING_FOR_DATA,
933 SERVER_STATE_WAITING_FOR_BEGIN
937 _g_dbus_auth_run_server (GDBusAuth *auth,
938 GDBusAuthObserver *observer,
940 gboolean allow_anonymous,
941 gboolean require_same_user,
942 GDBusCapabilityFlags offered_capabilities,
943 GDBusCapabilityFlags *out_negotiated_capabilities,
944 GCredentials **out_received_credentials,
945 GCancellable *cancellable,
950 GDataOutputStream *dos;
954 GDBusAuthMechanism *mech;
956 GDBusCapabilityFlags negotiated_capabilities;
957 GCredentials *credentials;
958 GCredentials *own_credentials = NULL;
960 debug_print ("SERVER: initiating");
962 _g_dbus_auth_add_mechs (auth, observer);
967 negotiated_capabilities = 0;
970 if (!g_dbus_is_guid (guid))
975 "The given GUID '%s' is not valid",
980 /* We use an extremely slow (but reliable) line reader for input
981 * instead of something buffered - this basically does a recvfrom()
982 * system call per character
984 * (the problem with using GDataInputStream's read_line is that
985 * because of buffering it might start reading into the first D-Bus
986 * message that appears after "BEGIN\r\n"....)
989 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
990 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
992 /* read the NUL-byte, possibly with credentials attached */
993 #ifndef G_CREDENTIALS_PREFER_MESSAGE_PASSING
994 if (G_IS_SOCKET_CONNECTION (auth->priv->stream))
996 GSocket *sock = g_socket_connection_get_socket (G_SOCKET_CONNECTION (auth->priv->stream));
999 credentials = g_socket_get_credentials (sock, &local_error);
1001 if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1003 g_propagate_error (error, local_error);
1008 /* Clear the error indicator, so we can retry with
1009 * g_unix_connection_receive_credentials() if necessary */
1010 g_clear_error (&local_error);
1015 if (credentials == NULL && G_IS_UNIX_CONNECTION (auth->priv->stream))
1018 credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
1021 if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
1023 g_propagate_error (error, local_error);
1026 g_clear_error (&local_error);
1034 num_read = g_input_stream_read (g_io_stream_get_input_stream (auth->priv->stream),
1036 cancellable, &local_error);
1037 if (num_read != 1 || local_error != NULL)
1039 if (local_error == NULL)
1040 g_set_error_literal (error,
1043 _ ("Unexpected lack of content trying to read a byte"));
1045 g_propagate_error (error, local_error);
1050 if (credentials != NULL)
1052 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1054 s = g_credentials_to_string (credentials);
1055 debug_print ("SERVER: received credentials '%s'", s);
1061 debug_print ("SERVER: didn't receive any credentials");
1064 own_credentials = g_credentials_new ();
1066 state = SERVER_STATE_WAITING_FOR_AUTH;
1071 case SERVER_STATE_WAITING_FOR_AUTH:
1072 debug_print ("SERVER: WaitingForAuth");
1073 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1077 debug_print ("SERVER: WaitingForAuth, read '%s'", line);
1080 if (g_strcmp0 (line, "AUTH") == 0)
1082 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1083 debug_print ("SERVER: writing '%s'", s);
1084 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1093 else if (g_str_has_prefix (line, "AUTH "))
1096 const gchar *encoded;
1097 const gchar *mech_name;
1098 GType auth_mech_to_use_gtype;
1100 tokens = g_strsplit (line, " ", 0);
1102 switch (g_strv_length (tokens))
1105 /* no initial response */
1106 mech_name = tokens[1];
1111 /* initial response */
1112 mech_name = tokens[1];
1113 encoded = tokens[2];
1120 "Unexpected line '%s' while in WaitingForAuth state",
1122 g_strfreev (tokens);
1129 /* TODO: record that the client has attempted to use this mechanism */
1130 //g_debug ("client is trying '%s'", mech_name);
1132 auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1133 if ((auth_mech_to_use_gtype == (GType) 0) ||
1134 (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1136 /* We don't support this auth mechanism */
1137 g_strfreev (tokens);
1138 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1139 debug_print ("SERVER: writing '%s'", s);
1140 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1147 /* stay in WAITING FOR AUTH */
1148 state = SERVER_STATE_WAITING_FOR_AUTH;
1152 gchar *initial_response;
1153 gsize initial_response_len;
1155 g_clear_object (&mech);
1156 mech = g_object_new (auth_mech_to_use_gtype,
1157 "stream", auth->priv->stream,
1158 "credentials", credentials,
1161 initial_response = NULL;
1162 initial_response_len = 0;
1163 if (encoded != NULL)
1165 initial_response = hexdecode (encoded, &initial_response_len, error);
1166 if (initial_response == NULL)
1168 g_prefix_error (error, "Initial response is malformed: ");
1169 /* invalid encoding, disconnect! */
1170 g_strfreev (tokens);
1175 _g_dbus_auth_mechanism_server_initiate (mech,
1177 initial_response_len);
1178 g_free (initial_response);
1179 g_strfreev (tokens);
1182 switch (_g_dbus_auth_mechanism_server_get_state (mech))
1184 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1185 if (require_same_user &&
1186 (credentials == NULL ||
1187 !g_credentials_is_same_user (credentials, own_credentials, NULL)))
1190 g_set_error_literal (error,
1193 _("User IDs must be the same for peer and server"));
1196 else if (observer != NULL &&
1197 !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1202 g_set_error_literal (error,
1205 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1210 s = g_strdup_printf ("OK %s\r\n", guid);
1211 debug_print ("SERVER: writing '%s'", s);
1212 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1218 state = SERVER_STATE_WAITING_FOR_BEGIN;
1222 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1223 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1224 debug_print ("SERVER: writing '%s'", s);
1225 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1231 state = SERVER_STATE_WAITING_FOR_AUTH;
1234 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1235 state = SERVER_STATE_WAITING_FOR_DATA;
1238 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1243 data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1249 s = g_strdup ("DATA\r\n");
1253 gchar *encoded_data = _g_dbus_hexencode (data, data_len);
1255 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1256 g_free (encoded_data);
1261 debug_print ("SERVER: writing '%s'", s);
1262 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1275 g_assert_not_reached ();
1285 "Unexpected line '%s' while in WaitingForAuth state",
1292 case SERVER_STATE_WAITING_FOR_DATA:
1293 debug_print ("SERVER: WaitingForData");
1294 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1298 debug_print ("SERVER: WaitingForData, read '%s'", line);
1301 if (g_str_equal (line, "DATA") || g_str_has_prefix (line, "DATA "))
1304 gchar *decoded_data;
1305 gsize decoded_data_len = 0;
1307 encoded = g_strdup (line + 4);
1309 g_strstrip (encoded);
1310 decoded_data = hexdecode (encoded, &decoded_data_len, error);
1312 if (decoded_data == NULL)
1314 g_prefix_error (error, "DATA response is malformed: ");
1315 /* invalid encoding, disconnect! */
1318 _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1319 g_free (decoded_data);
1320 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1328 "Unexpected line '%s' while in WaitingForData state",
1334 case SERVER_STATE_WAITING_FOR_BEGIN:
1335 debug_print ("SERVER: WaitingForBegin");
1336 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1342 debug_print ("SERVER: WaitingForBegin, read '%s'", line);
1343 if (g_strcmp0 (line, "BEGIN") == 0)
1350 else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1353 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1355 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1356 s = "AGREE_UNIX_FD\r\n";
1357 debug_print ("SERVER: writing '%s'", s);
1358 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1363 s = "ERROR \"fd passing not offered\"\r\n";
1364 debug_print ("SERVER: writing '%s'", s);
1365 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1371 g_debug ("Unexpected line '%s' while in WaitingForBegin state", line);
1373 s = "ERROR \"Unknown Command\"\r\n";
1374 debug_print ("SERVER: writing '%s'", s);
1375 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1381 g_assert_not_reached ();
1387 g_set_error_literal (error,
1390 "Not implemented (server)");
1393 g_clear_object (&mech);
1394 g_clear_object (&dos);
1395 g_clear_object (&own_credentials);
1397 /* ensure return value is FALSE if error is set */
1398 if (error != NULL && *error != NULL)
1405 if (out_negotiated_capabilities != NULL)
1406 *out_negotiated_capabilities = negotiated_capabilities;
1407 if (out_received_credentials != NULL)
1408 *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1411 if (credentials != NULL)
1412 g_object_unref (credentials);
1414 debug_print ("SERVER: Done, authenticated=%d", ret);
1419 /* ---------------------------------------------------------------------------------------------------- */