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 "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 "gdbusprivate.h"
37 #include "giostream.h"
38 #include "gdatainputstream.h"
39 #include "gdataoutputstream.h"
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include "gunixconnection.h"
45 #include "gunixcredentialsmessage.h"
52 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);
88 static void mechanism_free (Mechanism *m);
90 struct _GDBusAuthPrivate
94 /* A list of available Mechanism, sorted according to priority */
95 GList *available_mechanisms;
104 G_DEFINE_TYPE (GDBusAuth, _g_dbus_auth, G_TYPE_OBJECT);
106 /* ---------------------------------------------------------------------------------------------------- */
109 _g_dbus_auth_finalize (GObject *object)
111 GDBusAuth *auth = G_DBUS_AUTH (object);
113 if (auth->priv->stream != NULL)
114 g_object_unref (auth->priv->stream);
115 g_list_foreach (auth->priv->available_mechanisms, (GFunc) mechanism_free, NULL);
116 g_list_free (auth->priv->available_mechanisms);
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 g_type_class_add_private (klass, sizeof (GDBusAuthPrivate));
169 gobject_class = G_OBJECT_CLASS (klass);
170 gobject_class->get_property = _g_dbus_auth_get_property;
171 gobject_class->set_property = _g_dbus_auth_set_property;
172 gobject_class->finalize = _g_dbus_auth_finalize;
174 g_object_class_install_property (gobject_class,
176 g_param_spec_object ("stream",
178 P_("The underlying GIOStream used for I/O"),
182 G_PARAM_CONSTRUCT_ONLY |
183 G_PARAM_STATIC_NAME |
184 G_PARAM_STATIC_BLURB |
185 G_PARAM_STATIC_NICK));
189 mechanism_free (Mechanism *m)
195 add_mechanism (GDBusAuth *auth,
196 GType mechanism_type)
200 m = g_new0 (Mechanism, 1);
201 m->name = _g_dbus_auth_mechanism_get_name (mechanism_type);
202 m->priority = _g_dbus_auth_mechanism_get_priority (mechanism_type);
203 m->gtype = mechanism_type;
205 auth->priv->available_mechanisms = g_list_prepend (auth->priv->available_mechanisms, m);
209 mech_compare_func (Mechanism *a, Mechanism *b)
212 /* ensure deterministic order */
213 ret = b->priority - a->priority;
215 ret = g_strcmp0 (b->name, a->name);
220 _g_dbus_auth_init (GDBusAuth *auth)
222 auth->priv = G_TYPE_INSTANCE_GET_PRIVATE (auth, G_TYPE_DBUS_AUTH, GDBusAuthPrivate);
224 /* TODO: trawl extension points */
225 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_ANON);
226 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_SHA1);
227 add_mechanism (auth, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL);
229 auth->priv->available_mechanisms = g_list_sort (auth->priv->available_mechanisms,
230 (GCompareFunc) mech_compare_func);
234 find_mech_by_name (GDBusAuth *auth,
242 for (l = auth->priv->available_mechanisms; l != NULL; l = l->next)
244 Mechanism *m = l->data;
245 if (g_strcmp0 (name, m->name) == 0)
257 _g_dbus_auth_new (GIOStream *stream)
259 return g_object_new (G_TYPE_DBUS_AUTH,
264 /* ---------------------------------------------------------------------------------------------------- */
265 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
267 _my_g_data_input_stream_read_line (GDataInputStream *dis,
268 gsize *out_line_length,
269 GCancellable *cancellable,
274 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
276 ret = g_data_input_stream_read_line (dis,
280 if (ret == NULL && error != NULL && *error == NULL)
282 g_set_error_literal (error,
285 _("Unexpected lack of content trying to read a line"));
291 /* This function is to avoid situations like this
293 * BEGIN\r\nl\0\0\1...
295 * e.g. where we read into the first D-Bus message while waiting for
296 * the final line from the client (TODO: file bug against gio for
300 _my_g_input_stream_read_line_safe (GInputStream *i,
301 gsize *out_line_length,
302 GCancellable *cancellable,
308 gboolean last_was_cr;
310 str = g_string_new (NULL);
315 num_read = g_input_stream_read (i,
324 if (error != NULL && *error == NULL)
326 g_set_error_literal (error,
329 _("Unexpected lack of content trying to (safely) read a line"));
334 g_string_append_c (str, (gint) c);
339 g_assert (str->len >= 2);
340 g_string_set_size (str, str->len - 2);
344 last_was_cr = (c == 0x0d);
348 if (out_line_length != NULL)
349 *out_line_length = str->len;
350 return g_string_free (str, FALSE);
353 g_assert (error == NULL || *error != NULL);
354 g_string_free (str, TRUE);
358 /* ---------------------------------------------------------------------------------------------------- */
361 append_nibble (GString *s, gint val)
363 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
367 hexdecode (const gchar *str,
376 s = g_string_new (NULL);
378 for (n = 0; str[n] != '\0'; n += 2)
384 upper_nibble = g_ascii_xdigit_value (str[n]);
385 lower_nibble = g_ascii_xdigit_value (str[n + 1]);
386 if (upper_nibble == -1 || lower_nibble == -1)
391 "Error hexdecoding string `%s' around position %d",
395 value = (upper_nibble<<4) | lower_nibble;
396 g_string_append_c (s, value);
399 ret = g_string_free (s, FALSE);
404 g_string_free (s, TRUE);
410 hexencode (const gchar *str)
415 s = g_string_new (NULL);
416 for (n = 0; str[n] != '\0'; n++)
422 val = ((const guchar *) str)[n];
423 upper_nibble = val >> 4;
424 lower_nibble = val & 0x0f;
426 append_nibble (s, upper_nibble);
427 append_nibble (s, lower_nibble);
430 return g_string_free (s, FALSE);
433 /* ---------------------------------------------------------------------------------------------------- */
435 static GDBusAuthMechanism *
436 client_choose_mech_and_send_initial_response (GDBusAuth *auth,
437 GCredentials *credentials_that_were_sent,
438 const gchar* const *supported_auth_mechs,
439 GPtrArray *attempted_auth_mechs,
440 GDataOutputStream *dos,
441 GCancellable *cancellable,
444 GDBusAuthMechanism *mech;
445 GType auth_mech_to_use_gtype;
448 gchar *initial_response;
449 gsize initial_response_len;
456 debug_print ("CLIENT: Trying to choose mechanism");
458 /* find an authentication mechanism to try, if any */
459 auth_mech_to_use_gtype = (GType) 0;
460 for (n = 0; supported_auth_mechs[n] != NULL; n++)
462 gboolean attempted_already;
463 attempted_already = FALSE;
464 for (m = 0; m < attempted_auth_mechs->len; m++)
466 if (g_strcmp0 (supported_auth_mechs[n], attempted_auth_mechs->pdata[m]) == 0)
468 attempted_already = TRUE;
472 if (!attempted_already)
474 auth_mech_to_use_gtype = find_mech_by_name (auth, supported_auth_mechs[n]);
475 if (auth_mech_to_use_gtype != (GType) 0)
480 if (auth_mech_to_use_gtype == (GType) 0)
486 debug_print ("CLIENT: Exhausted all available mechanisms");
488 available = g_strjoinv (", ", (gchar **) supported_auth_mechs);
490 tried_str = g_string_new (NULL);
491 for (n = 0; n < attempted_auth_mechs->len; n++)
494 g_string_append (tried_str, ", ");
495 g_string_append (tried_str, attempted_auth_mechs->pdata[n]);
500 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
503 g_string_free (tried_str, TRUE);
508 /* OK, decided on a mechanism - let's do this thing */
509 mech = g_object_new (auth_mech_to_use_gtype,
510 "stream", auth->priv->stream,
511 "credentials", credentials_that_were_sent,
513 debug_print ("CLIENT: Trying mechanism `%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
514 g_ptr_array_add (attempted_auth_mechs, (gpointer) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
516 /* the auth mechanism may not be supported
517 * (for example, EXTERNAL only works if credentials were exchanged)
519 if (!_g_dbus_auth_mechanism_is_supported (mech))
521 debug_print ("CLIENT: Mechanism `%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
522 g_object_unref (mech);
527 initial_response_len = -1;
528 initial_response = _g_dbus_auth_mechanism_client_initiate (mech,
529 &initial_response_len);
531 g_printerr ("using auth mechanism with name `%s' of type `%s' with initial response `%s'\n",
532 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
533 g_type_name (G_TYPE_FROM_INSTANCE (mech)),
536 if (initial_response != NULL)
538 //g_printerr ("initial_response = `%s'\n", initial_response);
539 encoded = hexencode (initial_response);
540 s = g_strdup_printf ("AUTH %s %s\r\n",
541 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype),
543 g_free (initial_response);
548 s = g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype));
550 debug_print ("CLIENT: writing `%s'", s);
551 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
553 g_object_unref (mech);
565 /* ---------------------------------------------------------------------------------------------------- */
569 CLIENT_STATE_WAITING_FOR_DATA,
570 CLIENT_STATE_WAITING_FOR_OK,
571 CLIENT_STATE_WAITING_FOR_REJECT,
572 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
576 _g_dbus_auth_run_client (GDBusAuth *auth,
577 GDBusCapabilityFlags offered_capabilities,
578 GDBusCapabilityFlags *out_negotiated_capabilities,
579 GCancellable *cancellable,
583 GDataInputStream *dis;
584 GDataOutputStream *dos;
585 GCredentials *credentials;
589 gchar **supported_auth_mechs;
590 GPtrArray *attempted_auth_mechs;
591 GDBusAuthMechanism *mech;
593 GDBusCapabilityFlags negotiated_capabilities;
595 debug_print ("CLIENT: initiating");
598 supported_auth_mechs = NULL;
599 attempted_auth_mechs = g_ptr_array_new ();
601 negotiated_capabilities = 0;
604 dis = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth->priv->stream)));
605 dos = G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth->priv->stream)));
606 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
607 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
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);
864 g_object_unref (dis);
865 g_object_unref (dos);
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)));
973 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis), FALSE);
974 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos), FALSE);
976 g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
978 /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */
980 if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
983 credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
986 if (credentials == NULL)
988 g_propagate_error (error, local_error);
995 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
996 if (local_error != NULL)
998 g_propagate_error (error, local_error);
1004 byte = g_data_input_stream_read_byte (dis, cancellable, &local_error);
1005 if (local_error != NULL)
1007 g_propagate_error (error, local_error);
1011 if (credentials != NULL)
1013 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1015 s = g_credentials_to_string (credentials);
1016 debug_print ("SERVER: received credentials `%s'", s);
1022 debug_print ("SERVER: didn't receive any credentials");
1025 state = SERVER_STATE_WAITING_FOR_AUTH;
1030 case SERVER_STATE_WAITING_FOR_AUTH:
1031 debug_print ("SERVER: WaitingForAuth");
1032 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1033 debug_print ("SERVER: WaitingForAuth, read `%s'", line);
1036 if (g_strcmp0 (line, "AUTH") == 0)
1038 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1039 debug_print ("SERVER: writing `%s'", s);
1040 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1048 else if (g_str_has_prefix (line, "AUTH "))
1051 const gchar *encoded;
1052 const gchar *mech_name;
1053 GType auth_mech_to_use_gtype;
1055 tokens = g_strsplit (line, " ", 0);
1058 switch (g_strv_length (tokens))
1061 /* no initial response */
1062 mech_name = tokens[1];
1067 /* initial response */
1068 mech_name = tokens[1];
1069 encoded = tokens[2];
1076 "Unexpected line `%s' while in WaitingForAuth state",
1078 g_strfreev (tokens);
1082 /* TODO: record that the client has attempted to use this mechanism */
1083 //g_debug ("client is trying `%s'", mech_name);
1085 auth_mech_to_use_gtype = find_mech_by_name (auth, mech_name);
1086 if ((auth_mech_to_use_gtype == (GType) 0) ||
1087 (!allow_anonymous && g_strcmp0 (mech_name, "ANONYMOUS") == 0))
1089 /* We don't support this auth mechanism */
1090 g_strfreev (tokens);
1091 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1092 debug_print ("SERVER: writing `%s'", s);
1093 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1100 /* stay in WAITING FOR AUTH */
1101 state = SERVER_STATE_WAITING_FOR_AUTH;
1105 gchar *initial_response;
1106 gsize initial_response_len;
1108 mech = g_object_new (auth_mech_to_use_gtype,
1109 "stream", auth->priv->stream,
1110 "credentials", credentials,
1113 initial_response = NULL;
1114 initial_response_len = 0;
1115 if (encoded != NULL)
1117 initial_response = hexdecode (encoded, &initial_response_len, error);
1118 if (initial_response == NULL)
1120 g_prefix_error (error, "Initial response is malformed: ");
1121 /* invalid encoding, disconnect! */
1122 g_strfreev (tokens);
1127 _g_dbus_auth_mechanism_server_initiate (mech,
1129 initial_response_len);
1130 g_free (initial_response);
1131 g_strfreev (tokens);
1134 switch (_g_dbus_auth_mechanism_server_get_state (mech))
1136 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED:
1137 if (observer != NULL &&
1138 !g_dbus_auth_observer_authorize_authenticated_peer (observer,
1143 g_set_error_literal (error,
1146 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1151 s = g_strdup_printf ("OK %s\r\n", guid);
1152 debug_print ("SERVER: writing `%s'", s);
1153 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1159 state = SERVER_STATE_WAITING_FOR_BEGIN;
1163 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED:
1164 s = get_auth_mechanisms (auth, allow_anonymous, "REJECTED ", "\r\n", " ");
1165 debug_print ("SERVER: writing `%s'", s);
1166 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1172 state = SERVER_STATE_WAITING_FOR_AUTH;
1175 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA:
1176 state = SERVER_STATE_WAITING_FOR_DATA;
1179 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND:
1183 gchar *encoded_data;
1184 data = _g_dbus_auth_mechanism_server_data_send (mech, &data_len);
1185 encoded_data = hexencode (data);
1186 s = g_strdup_printf ("DATA %s\r\n", encoded_data);
1187 g_free (encoded_data);
1189 debug_print ("SERVER: writing `%s'", s);
1190 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1202 g_assert_not_reached ();
1212 "Unexpected line `%s' while in WaitingForAuth state",
1219 case SERVER_STATE_WAITING_FOR_DATA:
1220 debug_print ("SERVER: WaitingForData");
1221 line = _my_g_data_input_stream_read_line (dis, &line_length, cancellable, error);
1222 debug_print ("SERVER: WaitingForData, read `%s'", line);
1225 if (g_str_has_prefix (line, "DATA "))
1228 gchar *decoded_data;
1229 gsize decoded_data_len;
1231 encoded = g_strdup (line + 5);
1233 g_strstrip (encoded);
1234 decoded_data = hexdecode (encoded, &decoded_data_len, error);
1236 if (decoded_data == NULL)
1238 g_prefix_error (error, "DATA response is malformed: ");
1239 /* invalid encoding, disconnect! */
1242 _g_dbus_auth_mechanism_server_data_receive (mech, decoded_data, decoded_data_len);
1243 g_free (decoded_data);
1244 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1252 "Unexpected line `%s' while in WaitingForData state",
1258 case SERVER_STATE_WAITING_FOR_BEGIN:
1259 debug_print ("SERVER: WaitingForBegin");
1260 /* Use extremely slow (but reliable) line reader - this basically
1261 * does a recvfrom() system call per character
1263 * (the problem with using GDataInputStream's read_line is that because of
1264 * buffering it might start reading into the first D-Bus message that
1265 * appears after "BEGIN\r\n"....)
1267 line = _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth->priv->stream),
1271 debug_print ("SERVER: WaitingForBegin, read `%s'", line);
1274 if (g_strcmp0 (line, "BEGIN") == 0)
1281 else if (g_strcmp0 (line, "NEGOTIATE_UNIX_FD") == 0)
1284 if (offered_capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING)
1286 negotiated_capabilities |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
1287 s = "AGREE_UNIX_FD\r\n";
1288 debug_print ("SERVER: writing `%s'", s);
1289 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1294 s = "ERROR \"fd passing not offered\"\r\n";
1295 debug_print ("SERVER: writing `%s'", s);
1296 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1302 g_debug ("Unexpected line `%s' while in WaitingForBegin state", line);
1304 s = "ERROR \"Unknown Command\"\r\n";
1305 debug_print ("SERVER: writing `%s'", s);
1306 if (!g_data_output_stream_put_string (dos, s, cancellable, error))
1312 g_assert_not_reached ();
1318 g_set_error_literal (error,
1321 "Not implemented (server)");
1325 g_object_unref (mech);
1327 g_object_unref (dis);
1329 g_object_unref (dos);
1331 /* ensure return value is FALSE if error is set */
1332 if (error != NULL && *error != NULL)
1339 if (out_negotiated_capabilities != NULL)
1340 *out_negotiated_capabilities = negotiated_capabilities;
1341 if (out_received_credentials != NULL)
1342 *out_received_credentials = credentials != NULL ? g_object_ref (credentials) : NULL;
1345 if (credentials != NULL)
1346 g_object_unref (credentials);
1348 debug_print ("SERVER: Done, authenticated=%d", ret);
1353 /* ---------------------------------------------------------------------------------------------------- */
1355 #define __G_DBUS_AUTH_C__
1356 #include "gioaliasdef.c"