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"
51 debug_print (const gchar
*message
, ...)
53 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
60 _g_dbus_debug_print_lock ();
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
);
80 _g_dbus_debug_print_unlock ();
91 static void mechanism_free (Mechanism
*m
);
93 struct _GDBusAuthPrivate
97 /* A list of available Mechanism, sorted according to priority */
98 GList
*available_mechanisms
;
107 G_DEFINE_TYPE (GDBusAuth
, _g_dbus_auth
, G_TYPE_OBJECT
);
109 /* ---------------------------------------------------------------------------------------------------- */
112 _g_dbus_auth_finalize (GObject
*object
)
114 GDBusAuth
*auth
= G_DBUS_AUTH (object
);
116 if (auth
->priv
->stream
!= NULL
)
117 g_object_unref (auth
->priv
->stream
);
118 g_list_foreach (auth
->priv
->available_mechanisms
, (GFunc
) mechanism_free
, NULL
);
119 g_list_free (auth
->priv
->available_mechanisms
);
121 if (G_OBJECT_CLASS (_g_dbus_auth_parent_class
)->finalize
!= NULL
)
122 G_OBJECT_CLASS (_g_dbus_auth_parent_class
)->finalize (object
);
126 _g_dbus_auth_get_property (GObject
*object
,
131 GDBusAuth
*auth
= G_DBUS_AUTH (object
);
136 g_value_set_object (value
, auth
->priv
->stream
);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
146 _g_dbus_auth_set_property (GObject
*object
,
151 GDBusAuth
*auth
= G_DBUS_AUTH (object
);
156 auth
->priv
->stream
= g_value_dup_object (value
);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
166 _g_dbus_auth_class_init (GDBusAuthClass
*klass
)
168 GObjectClass
*gobject_class
;
170 g_type_class_add_private (klass
, sizeof (GDBusAuthPrivate
));
172 gobject_class
= G_OBJECT_CLASS (klass
);
173 gobject_class
->get_property
= _g_dbus_auth_get_property
;
174 gobject_class
->set_property
= _g_dbus_auth_set_property
;
175 gobject_class
->finalize
= _g_dbus_auth_finalize
;
177 g_object_class_install_property (gobject_class
,
179 g_param_spec_object ("stream",
181 P_("The underlying GIOStream used for I/O"),
185 G_PARAM_CONSTRUCT_ONLY
|
186 G_PARAM_STATIC_NAME
|
187 G_PARAM_STATIC_BLURB
|
188 G_PARAM_STATIC_NICK
));
192 mechanism_free (Mechanism
*m
)
198 add_mechanism (GDBusAuth
*auth
,
199 GType mechanism_type
)
203 m
= g_new0 (Mechanism
, 1);
204 m
->name
= _g_dbus_auth_mechanism_get_name (mechanism_type
);
205 m
->priority
= _g_dbus_auth_mechanism_get_priority (mechanism_type
);
206 m
->gtype
= mechanism_type
;
208 auth
->priv
->available_mechanisms
= g_list_prepend (auth
->priv
->available_mechanisms
, m
);
212 mech_compare_func (Mechanism
*a
, Mechanism
*b
)
215 /* ensure deterministic order */
216 ret
= b
->priority
- a
->priority
;
218 ret
= g_strcmp0 (b
->name
, a
->name
);
223 _g_dbus_auth_init (GDBusAuth
*auth
)
225 auth
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (auth
, G_TYPE_DBUS_AUTH
, GDBusAuthPrivate
);
227 /* TODO: trawl extension points */
228 add_mechanism (auth
, G_TYPE_DBUS_AUTH_MECHANISM_ANON
);
229 add_mechanism (auth
, G_TYPE_DBUS_AUTH_MECHANISM_SHA1
);
230 add_mechanism (auth
, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL
);
232 auth
->priv
->available_mechanisms
= g_list_sort (auth
->priv
->available_mechanisms
,
233 (GCompareFunc
) mech_compare_func
);
237 find_mech_by_name (GDBusAuth
*auth
,
245 for (l
= auth
->priv
->available_mechanisms
; l
!= NULL
; l
= l
->next
)
247 Mechanism
*m
= l
->data
;
248 if (g_strcmp0 (name
, m
->name
) == 0)
260 _g_dbus_auth_new (GIOStream
*stream
)
262 return g_object_new (G_TYPE_DBUS_AUTH
,
267 /* ---------------------------------------------------------------------------------------------------- */
268 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
270 _my_g_data_input_stream_read_line (GDataInputStream
*dis
,
271 gsize
*out_line_length
,
272 GCancellable
*cancellable
,
277 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
279 ret
= g_data_input_stream_read_line (dis
,
283 if (ret
== NULL
&& error
!= NULL
&& *error
== NULL
)
285 g_set_error_literal (error
,
288 _("Unexpected lack of content trying to read a line"));
294 /* This function is to avoid situations like this
296 * BEGIN\r\nl\0\0\1...
298 * e.g. where we read into the first D-Bus message while waiting for
299 * the final line from the client (TODO: file bug against gio for
303 _my_g_input_stream_read_line_safe (GInputStream
*i
,
304 gsize
*out_line_length
,
305 GCancellable
*cancellable
,
311 gboolean last_was_cr
;
313 str
= g_string_new (NULL
);
318 num_read
= g_input_stream_read (i
,
327 if (error
!= NULL
&& *error
== NULL
)
329 g_set_error_literal (error
,
332 _("Unexpected lack of content trying to (safely) read a line"));
337 g_string_append_c (str
, (gint
) c
);
342 g_assert (str
->len
>= 2);
343 g_string_set_size (str
, str
->len
- 2);
347 last_was_cr
= (c
== 0x0d);
351 if (out_line_length
!= NULL
)
352 *out_line_length
= str
->len
;
353 return g_string_free (str
, FALSE
);
356 g_assert (error
== NULL
|| *error
!= NULL
);
357 g_string_free (str
, TRUE
);
361 /* ---------------------------------------------------------------------------------------------------- */
364 append_nibble (GString
*s
, gint val
)
366 g_string_append_c (s
, val
>= 10 ? ('a' + val
- 10) : ('0' + val
));
370 hexdecode (const gchar
*str
,
379 s
= g_string_new (NULL
);
381 for (n
= 0; str
[n
] != '\0'; n
+= 2)
387 upper_nibble
= g_ascii_xdigit_value (str
[n
]);
388 lower_nibble
= g_ascii_xdigit_value (str
[n
+ 1]);
389 if (upper_nibble
== -1 || lower_nibble
== -1)
394 "Error hexdecoding string `%s' around position %d",
398 value
= (upper_nibble
<<4) | lower_nibble
;
399 g_string_append_c (s
, value
);
402 ret
= g_string_free (s
, FALSE
);
407 g_string_free (s
, TRUE
);
413 hexencode (const gchar
*str
)
418 s
= g_string_new (NULL
);
419 for (n
= 0; str
[n
] != '\0'; n
++)
425 val
= ((const guchar
*) str
)[n
];
426 upper_nibble
= val
>> 4;
427 lower_nibble
= val
& 0x0f;
429 append_nibble (s
, upper_nibble
);
430 append_nibble (s
, lower_nibble
);
433 return g_string_free (s
, FALSE
);
436 /* ---------------------------------------------------------------------------------------------------- */
438 static GDBusAuthMechanism
*
439 client_choose_mech_and_send_initial_response (GDBusAuth
*auth
,
440 GCredentials
*credentials_that_were_sent
,
441 const gchar
* const *supported_auth_mechs
,
442 GPtrArray
*attempted_auth_mechs
,
443 GDataOutputStream
*dos
,
444 GCancellable
*cancellable
,
447 GDBusAuthMechanism
*mech
;
448 GType auth_mech_to_use_gtype
;
451 gchar
*initial_response
;
452 gsize initial_response_len
;
459 debug_print ("CLIENT: Trying to choose mechanism");
461 /* find an authentication mechanism to try, if any */
462 auth_mech_to_use_gtype
= (GType
) 0;
463 for (n
= 0; supported_auth_mechs
[n
] != NULL
; n
++)
465 gboolean attempted_already
;
466 attempted_already
= FALSE
;
467 for (m
= 0; m
< attempted_auth_mechs
->len
; m
++)
469 if (g_strcmp0 (supported_auth_mechs
[n
], attempted_auth_mechs
->pdata
[m
]) == 0)
471 attempted_already
= TRUE
;
475 if (!attempted_already
)
477 auth_mech_to_use_gtype
= find_mech_by_name (auth
, supported_auth_mechs
[n
]);
478 if (auth_mech_to_use_gtype
!= (GType
) 0)
483 if (auth_mech_to_use_gtype
== (GType
) 0)
489 debug_print ("CLIENT: Exhausted all available mechanisms");
491 available
= g_strjoinv (", ", (gchar
**) supported_auth_mechs
);
493 tried_str
= g_string_new (NULL
);
494 for (n
= 0; n
< attempted_auth_mechs
->len
; n
++)
497 g_string_append (tried_str
, ", ");
498 g_string_append (tried_str
, attempted_auth_mechs
->pdata
[n
]);
503 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
506 g_string_free (tried_str
, TRUE
);
511 /* OK, decided on a mechanism - let's do this thing */
512 mech
= g_object_new (auth_mech_to_use_gtype
,
513 "stream", auth
->priv
->stream
,
514 "credentials", credentials_that_were_sent
,
516 debug_print ("CLIENT: Trying mechanism `%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
517 g_ptr_array_add (attempted_auth_mechs
, (gpointer
) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
519 /* the auth mechanism may not be supported
520 * (for example, EXTERNAL only works if credentials were exchanged)
522 if (!_g_dbus_auth_mechanism_is_supported (mech
))
524 debug_print ("CLIENT: Mechanism `%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
525 g_object_unref (mech
);
530 initial_response_len
= -1;
531 initial_response
= _g_dbus_auth_mechanism_client_initiate (mech
,
532 &initial_response_len
);
534 g_printerr ("using auth mechanism with name `%s' of type `%s' with initial response `%s'\n",
535 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
),
536 g_type_name (G_TYPE_FROM_INSTANCE (mech
)),
539 if (initial_response
!= NULL
)
541 //g_printerr ("initial_response = `%s'\n", initial_response);
542 encoded
= hexencode (initial_response
);
543 s
= g_strdup_printf ("AUTH %s %s\r\n",
544 _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
),
546 g_free (initial_response
);
551 s
= g_strdup_printf ("AUTH %s\r\n", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
553 debug_print ("CLIENT: writing `%s'", s
);
554 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
556 g_object_unref (mech
);
568 /* ---------------------------------------------------------------------------------------------------- */
572 CLIENT_STATE_WAITING_FOR_DATA
,
573 CLIENT_STATE_WAITING_FOR_OK
,
574 CLIENT_STATE_WAITING_FOR_REJECT
,
575 CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
579 _g_dbus_auth_run_client (GDBusAuth
*auth
,
580 GDBusCapabilityFlags offered_capabilities
,
581 GDBusCapabilityFlags
*out_negotiated_capabilities
,
582 GCancellable
*cancellable
,
586 GDataInputStream
*dis
;
587 GDataOutputStream
*dos
;
588 GCredentials
*credentials
;
592 gchar
**supported_auth_mechs
;
593 GPtrArray
*attempted_auth_mechs
;
594 GDBusAuthMechanism
*mech
;
596 GDBusCapabilityFlags negotiated_capabilities
;
598 debug_print ("CLIENT: initiating");
601 supported_auth_mechs
= NULL
;
602 attempted_auth_mechs
= g_ptr_array_new ();
604 negotiated_capabilities
= 0;
607 dis
= G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth
->priv
->stream
)));
608 dos
= G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth
->priv
->stream
)));
609 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis
), FALSE
);
610 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos
), FALSE
);
612 g_data_input_stream_set_newline_type (dis
, G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
615 if (G_IS_UNIX_CONNECTION (auth
->priv
->stream
) && g_unix_credentials_message_is_supported ())
617 credentials
= g_credentials_new ();
618 if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth
->priv
->stream
),
625 if (!g_data_output_stream_put_byte (dos
, '\0', cancellable
, error
))
629 if (!g_data_output_stream_put_byte (dos
, '\0', cancellable
, error
))
633 if (credentials
!= NULL
)
635 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
637 s
= g_credentials_to_string (credentials
);
638 debug_print ("CLIENT: sent credentials `%s'", s
);
644 debug_print ("CLIENT: didn't send any credentials");
647 /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
649 /* Get list of supported authentication mechanisms */
651 debug_print ("CLIENT: writing `%s'", s
);
652 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
654 state
= CLIENT_STATE_WAITING_FOR_REJECT
;
660 case CLIENT_STATE_WAITING_FOR_REJECT
:
661 debug_print ("CLIENT: WaitingForReject");
662 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
665 debug_print ("CLIENT: WaitingForReject, read '%s'", line
);
667 if (!g_str_has_prefix (line
, "REJECTED "))
672 "In WaitingForReject: Expected `REJECTED am1 am2 ... amN', got `%s'",
677 if (supported_auth_mechs
== NULL
)
679 supported_auth_mechs
= g_strsplit (line
+ sizeof ("REJECTED ") - 1, " ", 0);
681 for (n
= 0; supported_auth_mechs
!= NULL
&& supported_auth_mechs
[n
] != NULL
; n
++)
682 g_printerr ("supported_auth_mechs[%d] = `%s'\n", n
, supported_auth_mechs
[n
]);
686 mech
= client_choose_mech_and_send_initial_response (auth
,
688 (const gchar
* const *) supported_auth_mechs
,
689 attempted_auth_mechs
,
695 if (_g_dbus_auth_mechanism_client_get_state (mech
) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
)
696 state
= CLIENT_STATE_WAITING_FOR_DATA
;
698 state
= CLIENT_STATE_WAITING_FOR_OK
;
701 case CLIENT_STATE_WAITING_FOR_OK
:
702 debug_print ("CLIENT: WaitingForOK");
703 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
706 debug_print ("CLIENT: WaitingForOK, read `%s'", line
);
707 if (g_str_has_prefix (line
, "OK "))
709 if (!g_dbus_is_guid (line
+ 3))
714 "Invalid OK response `%s'",
719 ret_guid
= g_strdup (line
+ 3);
722 if (offered_capabilities
& G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
)
724 s
= "NEGOTIATE_UNIX_FD\r\n";
725 debug_print ("CLIENT: writing `%s'", s
);
726 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
728 state
= CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
;
733 debug_print ("CLIENT: writing `%s'", s
);
734 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
736 /* and we're done! */
740 else if (g_str_has_prefix (line
, "REJECTED "))
746 /* TODO: handle other valid responses */
750 "In WaitingForOk: unexpected response `%s'",
757 case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
:
758 debug_print ("CLIENT: WaitingForAgreeUnixFD");
759 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
762 debug_print ("CLIENT: WaitingForAgreeUnixFD, read=`%s'", line
);
763 if (g_strcmp0 (line
, "AGREE_UNIX_FD") == 0)
766 negotiated_capabilities
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
768 debug_print ("CLIENT: writing `%s'", s
);
769 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
771 /* and we're done! */
774 else if (g_str_has_prefix (line
, "ERROR") && (line
[5] == 0 || g_ascii_isspace (line
[5])))
776 //g_strstrip (line + 5); g_debug ("bah, no unix_fd: `%s'", line + 5);
779 debug_print ("CLIENT: writing `%s'", s
);
780 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
782 /* and we're done! */
787 /* TODO: handle other valid responses */
791 "In WaitingForAgreeUnixFd: unexpected response `%s'",
798 case CLIENT_STATE_WAITING_FOR_DATA
:
799 debug_print ("CLIENT: WaitingForData");
800 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
803 debug_print ("CLIENT: WaitingForData, read=`%s'", line
);
804 if (g_str_has_prefix (line
, "DATA "))
808 gsize decoded_data_len
= 0;
810 encoded
= g_strdup (line
+ 5);
812 g_strstrip (encoded
);
813 decoded_data
= hexdecode (encoded
, &decoded_data_len
, error
);
815 if (decoded_data
== NULL
)
817 g_prefix_error (error
, "DATA response is malformed: ");
818 /* invalid encoding, disconnect! */
821 _g_dbus_auth_mechanism_client_data_receive (mech
, decoded_data
, decoded_data_len
);
822 g_free (decoded_data
);
824 if (_g_dbus_auth_mechanism_client_get_state (mech
) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
)
829 data
= _g_dbus_auth_mechanism_client_data_send (mech
, &data_len
);
830 encoded_data
= hexencode (data
);
831 s
= g_strdup_printf ("DATA %s\r\n", encoded_data
);
832 g_free (encoded_data
);
834 debug_print ("CLIENT: writing `%s'", s
);
835 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
842 state
= CLIENT_STATE_WAITING_FOR_OK
;
849 "In WaitingForData: unexpected response `%s'",
857 g_assert_not_reached ();
861 }; /* main authentication client loop */
865 g_object_unref (mech
);
866 g_ptr_array_unref (attempted_auth_mechs
);
867 g_strfreev (supported_auth_mechs
);
868 g_object_unref (dis
);
869 g_object_unref (dos
);
871 /* ensure return value is NULL if error is set */
872 if (error
!= NULL
&& *error
!= NULL
)
878 if (ret_guid
!= NULL
)
880 if (out_negotiated_capabilities
!= NULL
)
881 *out_negotiated_capabilities
= negotiated_capabilities
;
884 if (credentials
!= NULL
)
885 g_object_unref (credentials
);
887 debug_print ("CLIENT: Done, authenticated=%d", ret_guid
!= NULL
);
892 /* ---------------------------------------------------------------------------------------------------- */
895 get_auth_mechanisms (GDBusAuth
*auth
,
896 gboolean allow_anonymous
,
899 const gchar
*separator
)
905 str
= g_string_new (prefix
);
907 for (l
= auth
->priv
->available_mechanisms
; l
!= NULL
; l
= l
->next
)
909 Mechanism
*m
= l
->data
;
911 if (!allow_anonymous
&& g_strcmp0 (m
->name
, "ANONYMOUS") == 0)
915 g_string_append (str
, separator
);
916 g_string_append (str
, m
->name
);
920 g_string_append (str
, suffix
);
921 return g_string_free (str
, FALSE
);
927 SERVER_STATE_WAITING_FOR_AUTH
,
928 SERVER_STATE_WAITING_FOR_DATA
,
929 SERVER_STATE_WAITING_FOR_BEGIN
933 _g_dbus_auth_run_server (GDBusAuth
*auth
,
934 GDBusAuthObserver
*observer
,
936 gboolean allow_anonymous
,
937 GDBusCapabilityFlags offered_capabilities
,
938 GDBusCapabilityFlags
*out_negotiated_capabilities
,
939 GCredentials
**out_received_credentials
,
940 GCancellable
*cancellable
,
945 GDataInputStream
*dis
;
946 GDataOutputStream
*dos
;
951 GDBusAuthMechanism
*mech
;
953 GDBusCapabilityFlags negotiated_capabilities
;
954 GCredentials
*credentials
;
956 debug_print ("SERVER: initiating");
962 negotiated_capabilities
= 0;
965 if (!g_dbus_is_guid (guid
))
970 "The given guid `%s' is not valid",
975 dis
= G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth
->priv
->stream
)));
976 dos
= G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth
->priv
->stream
)));
977 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis
), FALSE
);
978 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos
), FALSE
);
980 g_data_input_stream_set_newline_type (dis
, G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
982 /* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */
984 if (G_IS_UNIX_CONNECTION (auth
->priv
->stream
) && g_unix_credentials_message_is_supported ())
987 credentials
= g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth
->priv
->stream
),
990 if (credentials
== NULL
)
992 g_propagate_error (error
, local_error
);
999 byte
= g_data_input_stream_read_byte (dis
, cancellable
, &local_error
);
1000 if (local_error
!= NULL
)
1002 g_propagate_error (error
, local_error
);
1008 byte
= g_data_input_stream_read_byte (dis
, cancellable
, &local_error
);
1009 if (local_error
!= NULL
)
1011 g_propagate_error (error
, local_error
);
1015 if (credentials
!= NULL
)
1017 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1019 s
= g_credentials_to_string (credentials
);
1020 debug_print ("SERVER: received credentials `%s'", s
);
1026 debug_print ("SERVER: didn't receive any credentials");
1029 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1034 case SERVER_STATE_WAITING_FOR_AUTH
:
1035 debug_print ("SERVER: WaitingForAuth");
1036 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
1037 debug_print ("SERVER: WaitingForAuth, read `%s'", line
);
1040 if (g_strcmp0 (line
, "AUTH") == 0)
1042 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1043 debug_print ("SERVER: writing `%s'", s
);
1044 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1052 else if (g_str_has_prefix (line
, "AUTH "))
1055 const gchar
*encoded
;
1056 const gchar
*mech_name
;
1057 GType auth_mech_to_use_gtype
;
1059 tokens
= g_strsplit (line
, " ", 0);
1062 switch (g_strv_length (tokens
))
1065 /* no initial response */
1066 mech_name
= tokens
[1];
1071 /* initial response */
1072 mech_name
= tokens
[1];
1073 encoded
= tokens
[2];
1080 "Unexpected line `%s' while in WaitingForAuth state",
1082 g_strfreev (tokens
);
1086 /* TODO: record that the client has attempted to use this mechanism */
1087 //g_debug ("client is trying `%s'", mech_name);
1089 auth_mech_to_use_gtype
= find_mech_by_name (auth
, mech_name
);
1090 if ((auth_mech_to_use_gtype
== (GType
) 0) ||
1091 (!allow_anonymous
&& g_strcmp0 (mech_name
, "ANONYMOUS") == 0))
1093 /* We don't support this auth mechanism */
1094 g_strfreev (tokens
);
1095 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1096 debug_print ("SERVER: writing `%s'", s
);
1097 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1104 /* stay in WAITING FOR AUTH */
1105 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1109 gchar
*initial_response
;
1110 gsize initial_response_len
;
1112 mech
= g_object_new (auth_mech_to_use_gtype
,
1113 "stream", auth
->priv
->stream
,
1114 "credentials", credentials
,
1117 initial_response
= NULL
;
1118 initial_response_len
= 0;
1119 if (encoded
!= NULL
)
1121 initial_response
= hexdecode (encoded
, &initial_response_len
, error
);
1122 if (initial_response
== NULL
)
1124 g_prefix_error (error
, "Initial response is malformed: ");
1125 /* invalid encoding, disconnect! */
1126 g_strfreev (tokens
);
1131 _g_dbus_auth_mechanism_server_initiate (mech
,
1133 initial_response_len
);
1134 g_free (initial_response
);
1135 g_strfreev (tokens
);
1138 switch (_g_dbus_auth_mechanism_server_get_state (mech
))
1140 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED
:
1141 if (observer
!= NULL
&&
1142 !g_dbus_auth_observer_authorize_authenticated_peer (observer
,
1147 g_set_error_literal (error
,
1150 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1155 s
= g_strdup_printf ("OK %s\r\n", guid
);
1156 debug_print ("SERVER: writing `%s'", s
);
1157 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1163 state
= SERVER_STATE_WAITING_FOR_BEGIN
;
1167 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED
:
1168 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1169 debug_print ("SERVER: writing `%s'", s
);
1170 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1176 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1179 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
:
1180 state
= SERVER_STATE_WAITING_FOR_DATA
;
1183 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
:
1187 gchar
*encoded_data
;
1188 data
= _g_dbus_auth_mechanism_server_data_send (mech
, &data_len
);
1189 encoded_data
= hexencode (data
);
1190 s
= g_strdup_printf ("DATA %s\r\n", encoded_data
);
1191 g_free (encoded_data
);
1193 debug_print ("SERVER: writing `%s'", s
);
1194 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1206 g_assert_not_reached ();
1216 "Unexpected line `%s' while in WaitingForAuth state",
1223 case SERVER_STATE_WAITING_FOR_DATA
:
1224 debug_print ("SERVER: WaitingForData");
1225 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
1226 debug_print ("SERVER: WaitingForData, read `%s'", line
);
1229 if (g_str_has_prefix (line
, "DATA "))
1232 gchar
*decoded_data
;
1233 gsize decoded_data_len
= 0;
1235 encoded
= g_strdup (line
+ 5);
1237 g_strstrip (encoded
);
1238 decoded_data
= hexdecode (encoded
, &decoded_data_len
, error
);
1240 if (decoded_data
== NULL
)
1242 g_prefix_error (error
, "DATA response is malformed: ");
1243 /* invalid encoding, disconnect! */
1246 _g_dbus_auth_mechanism_server_data_receive (mech
, decoded_data
, decoded_data_len
);
1247 g_free (decoded_data
);
1248 /* oh man, this goto-crap is so ugly.. really need to rewrite the state machine */
1256 "Unexpected line `%s' while in WaitingForData state",
1262 case SERVER_STATE_WAITING_FOR_BEGIN
:
1263 debug_print ("SERVER: WaitingForBegin");
1264 /* Use extremely slow (but reliable) line reader - this basically
1265 * does a recvfrom() system call per character
1267 * (the problem with using GDataInputStream's read_line is that because of
1268 * buffering it might start reading into the first D-Bus message that
1269 * appears after "BEGIN\r\n"....)
1271 line
= _my_g_input_stream_read_line_safe (g_io_stream_get_input_stream (auth
->priv
->stream
),
1275 debug_print ("SERVER: WaitingForBegin, read `%s'", line
);
1278 if (g_strcmp0 (line
, "BEGIN") == 0)
1285 else if (g_strcmp0 (line
, "NEGOTIATE_UNIX_FD") == 0)
1288 if (offered_capabilities
& G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
)
1290 negotiated_capabilities
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
1291 s
= "AGREE_UNIX_FD\r\n";
1292 debug_print ("SERVER: writing `%s'", s
);
1293 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1298 s
= "ERROR \"fd passing not offered\"\r\n";
1299 debug_print ("SERVER: writing `%s'", s
);
1300 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1306 g_debug ("Unexpected line `%s' while in WaitingForBegin state", line
);
1308 s
= "ERROR \"Unknown Command\"\r\n";
1309 debug_print ("SERVER: writing `%s'", s
);
1310 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1316 g_assert_not_reached ();
1322 g_set_error_literal (error
,
1325 "Not implemented (server)");
1329 g_object_unref (mech
);
1331 g_object_unref (dis
);
1333 g_object_unref (dos
);
1335 /* ensure return value is FALSE if error is set */
1336 if (error
!= NULL
&& *error
!= NULL
)
1343 if (out_negotiated_capabilities
!= NULL
)
1344 *out_negotiated_capabilities
= negotiated_capabilities
;
1345 if (out_received_credentials
!= NULL
)
1346 *out_received_credentials
= credentials
!= NULL
? g_object_ref (credentials
) : NULL
;
1349 if (credentials
!= NULL
)
1350 g_object_unref (credentials
);
1352 debug_print ("SERVER: Done, authenticated=%d", ret
);
1357 /* ---------------------------------------------------------------------------------------------------- */