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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
23 #include "gdbusauth.h"
25 #include "gdbusauthmechanismanon.h"
26 #include "gdbusauthmechanismexternal.h"
27 #include "gdbusauthmechanismsha1.h"
28 #include "gdbusauthobserver.h"
30 #include "gdbuserror.h"
31 #include "gdbusutils.h"
32 #include "gioenumtypes.h"
33 #include "gcredentials.h"
34 #include "gdbusprivate.h"
35 #include "giostream.h"
36 #include "gdatainputstream.h"
37 #include "gdataoutputstream.h"
40 #include "gnetworking.h"
41 #include "gunixconnection.h"
42 #include "gunixcredentialsmessage.h"
49 debug_print (const gchar
*message
, ...)
51 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
58 _g_dbus_debug_print_lock ();
60 va_start (var_args
, message
);
61 s
= g_strdup_vprintf (message
, var_args
);
64 str
= g_string_new (NULL
);
65 for (n
= 0; s
[n
] != '\0'; n
++)
67 if (G_UNLIKELY (s
[n
] == '\r'))
68 g_string_append (str
, "\\r");
69 else if (G_UNLIKELY (s
[n
] == '\n'))
70 g_string_append (str
, "\\n");
72 g_string_append_c (str
, s
[n
]);
74 g_print ("GDBus-debug:Auth: %s\n", str
->str
);
75 g_string_free (str
, TRUE
);
78 _g_dbus_debug_print_unlock ();
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_WITH_PRIVATE (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_free_full (auth
->priv
->available_mechanisms
, (GDestroyNotify
) mechanism_free
);
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 gobject_class
= G_OBJECT_CLASS (klass
);
168 gobject_class
->get_property
= _g_dbus_auth_get_property
;
169 gobject_class
->set_property
= _g_dbus_auth_set_property
;
170 gobject_class
->finalize
= _g_dbus_auth_finalize
;
172 g_object_class_install_property (gobject_class
,
174 g_param_spec_object ("stream",
176 P_("The underlying GIOStream used for I/O"),
180 G_PARAM_CONSTRUCT_ONLY
|
181 G_PARAM_STATIC_NAME
|
182 G_PARAM_STATIC_BLURB
|
183 G_PARAM_STATIC_NICK
));
187 mechanism_free (Mechanism
*m
)
193 add_mechanism (GDBusAuth
*auth
,
194 GDBusAuthObserver
*observer
,
195 GType mechanism_type
)
199 name
= _g_dbus_auth_mechanism_get_name (mechanism_type
);
200 if (observer
== NULL
|| g_dbus_auth_observer_allow_mechanism (observer
, name
))
203 m
= g_new0 (Mechanism
, 1);
205 m
->priority
= _g_dbus_auth_mechanism_get_priority (mechanism_type
);
206 m
->gtype
= mechanism_type
;
207 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_dbus_auth_get_instance_private (auth
);
229 _g_dbus_auth_add_mechs (GDBusAuth
*auth
,
230 GDBusAuthObserver
*observer
)
232 /* TODO: trawl extension points */
233 add_mechanism (auth
, observer
, G_TYPE_DBUS_AUTH_MECHANISM_ANON
);
234 add_mechanism (auth
, observer
, G_TYPE_DBUS_AUTH_MECHANISM_SHA1
);
235 add_mechanism (auth
, observer
, G_TYPE_DBUS_AUTH_MECHANISM_EXTERNAL
);
237 auth
->priv
->available_mechanisms
= g_list_sort (auth
->priv
->available_mechanisms
,
238 (GCompareFunc
) mech_compare_func
);
242 find_mech_by_name (GDBusAuth
*auth
,
250 for (l
= auth
->priv
->available_mechanisms
; l
!= NULL
; l
= l
->next
)
252 Mechanism
*m
= l
->data
;
253 if (g_strcmp0 (name
, m
->name
) == 0)
265 _g_dbus_auth_new (GIOStream
*stream
)
267 return g_object_new (G_TYPE_DBUS_AUTH
,
272 /* ---------------------------------------------------------------------------------------------------- */
273 /* like g_data_input_stream_read_line() but sets error if there's no content to read */
275 _my_g_data_input_stream_read_line (GDataInputStream
*dis
,
276 gsize
*out_line_length
,
277 GCancellable
*cancellable
,
282 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
284 ret
= g_data_input_stream_read_line (dis
,
288 if (ret
== NULL
&& error
!= NULL
&& *error
== NULL
)
290 g_set_error_literal (error
,
293 _("Unexpected lack of content trying to read a line"));
299 /* This function is to avoid situations like this
301 * BEGIN\r\nl\0\0\1...
303 * e.g. where we read into the first D-Bus message while waiting for
304 * the final line from the client (TODO: file bug against gio for
308 _my_g_input_stream_read_line_safe (GInputStream
*i
,
309 gsize
*out_line_length
,
310 GCancellable
*cancellable
,
316 gboolean last_was_cr
;
318 str
= g_string_new (NULL
);
323 num_read
= g_input_stream_read (i
,
332 if (error
!= NULL
&& *error
== NULL
)
334 g_set_error_literal (error
,
337 _("Unexpected lack of content trying to (safely) read a line"));
342 g_string_append_c (str
, (gint
) c
);
347 g_assert (str
->len
>= 2);
348 g_string_set_size (str
, str
->len
- 2);
352 last_was_cr
= (c
== 0x0d);
356 if (out_line_length
!= NULL
)
357 *out_line_length
= str
->len
;
358 return g_string_free (str
, FALSE
);
361 g_assert (error
== NULL
|| *error
!= NULL
);
362 g_string_free (str
, TRUE
);
366 /* ---------------------------------------------------------------------------------------------------- */
369 hexdecode (const gchar
*str
,
378 s
= g_string_new (NULL
);
380 for (n
= 0; str
[n
] != '\0'; n
+= 2)
386 upper_nibble
= g_ascii_xdigit_value (str
[n
]);
387 lower_nibble
= g_ascii_xdigit_value (str
[n
+ 1]);
388 if (upper_nibble
== -1 || lower_nibble
== -1)
393 "Error hexdecoding string '%s' around position %d",
397 value
= (upper_nibble
<<4) | lower_nibble
;
398 g_string_append_c (s
, value
);
402 ret
= g_string_free (s
, FALSE
);
409 g_string_free (s
, TRUE
);
414 /* ---------------------------------------------------------------------------------------------------- */
416 static GDBusAuthMechanism
*
417 client_choose_mech_and_send_initial_response (GDBusAuth
*auth
,
418 GCredentials
*credentials_that_were_sent
,
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)
467 debug_print ("CLIENT: Exhausted all available mechanisms");
469 available
= g_strjoinv (", ", (gchar
**) supported_auth_mechs
);
471 tried_str
= g_string_new (NULL
);
472 for (n
= 0; n
< attempted_auth_mechs
->len
; n
++)
475 g_string_append (tried_str
, ", ");
476 g_string_append (tried_str
, attempted_auth_mechs
->pdata
[n
]);
481 _("Exhausted all available authentication mechanisms (tried: %s) (available: %s)"),
484 g_string_free (tried_str
, TRUE
);
489 /* OK, decided on a mechanism - let's do this thing */
490 mech
= g_object_new (auth_mech_to_use_gtype
,
491 "stream", auth
->priv
->stream
,
492 "credentials", credentials_that_were_sent
,
494 debug_print ("CLIENT: Trying mechanism '%s'", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
495 g_ptr_array_add (attempted_auth_mechs
, (gpointer
) _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
497 /* the auth mechanism may not be supported
498 * (for example, EXTERNAL only works if credentials were exchanged)
500 if (!_g_dbus_auth_mechanism_is_supported (mech
))
502 debug_print ("CLIENT: Mechanism '%s' says it is not supported", _g_dbus_auth_mechanism_get_name (auth_mech_to_use_gtype
));
503 g_object_unref (mech
);
508 initial_response_len
= 0;
509 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 GDBusCapabilityFlags offered_capabilities
,
560 GDBusCapabilityFlags
*out_negotiated_capabilities
,
561 GCancellable
*cancellable
,
565 GDataInputStream
*dis
;
566 GDataOutputStream
*dos
;
567 GCredentials
*credentials
;
571 gchar
**supported_auth_mechs
;
572 GPtrArray
*attempted_auth_mechs
;
573 GDBusAuthMechanism
*mech
;
575 GDBusCapabilityFlags negotiated_capabilities
;
577 debug_print ("CLIENT: initiating");
579 _g_dbus_auth_add_mechs (auth
, observer
);
582 supported_auth_mechs
= NULL
;
583 attempted_auth_mechs
= g_ptr_array_new ();
585 negotiated_capabilities
= 0;
588 dis
= G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth
->priv
->stream
)));
589 dos
= G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth
->priv
->stream
)));
590 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis
), FALSE
);
591 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos
), FALSE
);
593 g_data_input_stream_set_newline_type (dis
, G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
596 if (G_IS_UNIX_CONNECTION (auth
->priv
->stream
))
598 credentials
= g_credentials_new ();
599 if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth
->priv
->stream
),
606 if (!g_data_output_stream_put_byte (dos
, '\0', cancellable
, error
))
610 if (!g_data_output_stream_put_byte (dos
, '\0', cancellable
, error
))
614 if (credentials
!= NULL
)
616 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
618 s
= g_credentials_to_string (credentials
);
619 debug_print ("CLIENT: sent credentials '%s'", s
);
625 debug_print ("CLIENT: didn't send any credentials");
628 /* TODO: to reduce roundtrips, try to pick an auth mechanism to start with */
630 /* Get list of supported authentication mechanisms */
632 debug_print ("CLIENT: writing '%s'", s
);
633 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
635 state
= CLIENT_STATE_WAITING_FOR_REJECT
;
641 case CLIENT_STATE_WAITING_FOR_REJECT
:
642 debug_print ("CLIENT: WaitingForReject");
643 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
646 debug_print ("CLIENT: WaitingForReject, read '%s'", line
);
649 if (!g_str_has_prefix (line
, "REJECTED "))
654 "In WaitingForReject: Expected 'REJECTED am1 am2 ... amN', got '%s'",
659 if (supported_auth_mechs
== NULL
)
661 supported_auth_mechs
= g_strsplit (line
+ sizeof ("REJECTED ") - 1, " ", 0);
663 for (n
= 0; supported_auth_mechs
!= NULL
&& supported_auth_mechs
[n
] != NULL
; n
++)
664 g_printerr ("supported_auth_mechs[%d] = '%s'\n", n
, supported_auth_mechs
[n
]);
668 mech
= client_choose_mech_and_send_initial_response (auth
,
670 (const gchar
* const *) supported_auth_mechs
,
671 attempted_auth_mechs
,
677 if (_g_dbus_auth_mechanism_client_get_state (mech
) == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
)
678 state
= CLIENT_STATE_WAITING_FOR_DATA
;
680 state
= CLIENT_STATE_WAITING_FOR_OK
;
683 case CLIENT_STATE_WAITING_FOR_OK
:
684 debug_print ("CLIENT: WaitingForOK");
685 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
688 debug_print ("CLIENT: WaitingForOK, read '%s'", line
);
689 if (g_str_has_prefix (line
, "OK "))
691 if (!g_dbus_is_guid (line
+ 3))
696 "Invalid OK response '%s'",
701 ret_guid
= g_strdup (line
+ 3);
704 if (offered_capabilities
& G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
)
706 s
= "NEGOTIATE_UNIX_FD\r\n";
707 debug_print ("CLIENT: writing '%s'", s
);
708 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
710 state
= CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
;
715 debug_print ("CLIENT: writing '%s'", s
);
716 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
718 /* and we're done! */
722 else if (g_str_has_prefix (line
, "REJECTED "))
724 goto choose_mechanism
;
728 /* TODO: handle other valid responses */
732 "In WaitingForOk: unexpected response '%s'",
739 case CLIENT_STATE_WAITING_FOR_AGREE_UNIX_FD
:
740 debug_print ("CLIENT: WaitingForAgreeUnixFD");
741 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
744 debug_print ("CLIENT: WaitingForAgreeUnixFD, read='%s'", line
);
745 if (g_strcmp0 (line
, "AGREE_UNIX_FD") == 0)
748 negotiated_capabilities
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
750 debug_print ("CLIENT: writing '%s'", s
);
751 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
753 /* and we're done! */
756 else if (g_str_has_prefix (line
, "ERROR") && (line
[5] == 0 || g_ascii_isspace (line
[5])))
758 //g_strstrip (line + 5); g_debug ("bah, no unix_fd: '%s'", line + 5);
761 debug_print ("CLIENT: writing '%s'", s
);
762 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
764 /* and we're done! */
769 /* TODO: handle other valid responses */
773 "In WaitingForAgreeUnixFd: unexpected response '%s'",
780 case CLIENT_STATE_WAITING_FOR_DATA
:
781 debug_print ("CLIENT: WaitingForData");
782 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
785 debug_print ("CLIENT: WaitingForData, read='%s'", line
);
786 if (g_str_has_prefix (line
, "DATA "))
790 gsize decoded_data_len
= 0;
792 encoded
= g_strdup (line
+ 5);
794 g_strstrip (encoded
);
795 decoded_data
= hexdecode (encoded
, &decoded_data_len
, error
);
797 if (decoded_data
== NULL
)
799 g_prefix_error (error
, "DATA response is malformed: ");
800 /* invalid encoding, disconnect! */
803 _g_dbus_auth_mechanism_client_data_receive (mech
, decoded_data
, decoded_data_len
);
804 g_free (decoded_data
);
806 if (_g_dbus_auth_mechanism_client_get_state (mech
) == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
)
811 data
= _g_dbus_auth_mechanism_client_data_send (mech
, &data_len
);
812 encoded_data
= _g_dbus_hexencode (data
, data_len
);
813 s
= g_strdup_printf ("DATA %s\r\n", encoded_data
);
814 g_free (encoded_data
);
816 debug_print ("CLIENT: writing '%s'", s
);
817 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
824 state
= CLIENT_STATE_WAITING_FOR_OK
;
826 else if (g_str_has_prefix (line
, "REJECTED "))
828 /* could be the chosen authentication method just doesn't work. Try
831 goto choose_mechanism
;
838 "In WaitingForData: unexpected response '%s'",
846 g_assert_not_reached ();
850 }; /* main authentication client loop */
854 g_object_unref (mech
);
855 g_ptr_array_unref (attempted_auth_mechs
);
856 g_strfreev (supported_auth_mechs
);
857 g_object_unref (dis
);
858 g_object_unref (dos
);
860 /* ensure return value is NULL if error is set */
861 if (error
!= NULL
&& *error
!= NULL
)
867 if (ret_guid
!= NULL
)
869 if (out_negotiated_capabilities
!= NULL
)
870 *out_negotiated_capabilities
= negotiated_capabilities
;
873 if (credentials
!= NULL
)
874 g_object_unref (credentials
);
876 debug_print ("CLIENT: Done, authenticated=%d", ret_guid
!= NULL
);
881 /* ---------------------------------------------------------------------------------------------------- */
884 get_auth_mechanisms (GDBusAuth
*auth
,
885 gboolean allow_anonymous
,
888 const gchar
*separator
)
894 str
= g_string_new (prefix
);
896 for (l
= auth
->priv
->available_mechanisms
; l
!= NULL
; l
= l
->next
)
898 Mechanism
*m
= l
->data
;
900 if (!allow_anonymous
&& g_strcmp0 (m
->name
, "ANONYMOUS") == 0)
904 g_string_append (str
, separator
);
905 g_string_append (str
, m
->name
);
909 g_string_append (str
, suffix
);
910 return g_string_free (str
, FALSE
);
916 SERVER_STATE_WAITING_FOR_AUTH
,
917 SERVER_STATE_WAITING_FOR_DATA
,
918 SERVER_STATE_WAITING_FOR_BEGIN
922 _g_dbus_auth_run_server (GDBusAuth
*auth
,
923 GDBusAuthObserver
*observer
,
925 gboolean allow_anonymous
,
926 GDBusCapabilityFlags offered_capabilities
,
927 GDBusCapabilityFlags
*out_negotiated_capabilities
,
928 GCredentials
**out_received_credentials
,
929 GCancellable
*cancellable
,
934 GDataInputStream
*dis
;
935 GDataOutputStream
*dos
;
939 GDBusAuthMechanism
*mech
;
941 GDBusCapabilityFlags negotiated_capabilities
;
942 GCredentials
*credentials
;
944 debug_print ("SERVER: initiating");
946 _g_dbus_auth_add_mechs (auth
, observer
);
952 negotiated_capabilities
= 0;
955 if (!g_dbus_is_guid (guid
))
960 "The given guid '%s' is not valid",
965 dis
= G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (auth
->priv
->stream
)));
966 dos
= G_DATA_OUTPUT_STREAM (g_data_output_stream_new (g_io_stream_get_output_stream (auth
->priv
->stream
)));
967 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (dis
), FALSE
);
968 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (dos
), FALSE
);
970 g_data_input_stream_set_newline_type (dis
, G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
972 /* first read the NUL-byte */
974 if (G_IS_UNIX_CONNECTION (auth
->priv
->stream
))
977 credentials
= g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth
->priv
->stream
),
980 if (credentials
== NULL
&& !g_error_matches (local_error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
982 g_propagate_error (error
, local_error
);
989 (void)g_data_input_stream_read_byte (dis
, cancellable
, &local_error
);
990 if (local_error
!= NULL
)
992 g_propagate_error (error
, local_error
);
998 (void)g_data_input_stream_read_byte (dis
, cancellable
, &local_error
);
999 if (local_error
!= NULL
)
1001 g_propagate_error (error
, local_error
);
1005 if (credentials
!= NULL
)
1007 if (G_UNLIKELY (_g_dbus_debug_authentication ()))
1009 s
= g_credentials_to_string (credentials
);
1010 debug_print ("SERVER: received credentials '%s'", s
);
1016 debug_print ("SERVER: didn't receive any credentials");
1019 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1024 case SERVER_STATE_WAITING_FOR_AUTH
:
1025 debug_print ("SERVER: WaitingForAuth");
1026 line
= _my_g_data_input_stream_read_line (dis
, &line_length
, cancellable
, error
);
1027 debug_print ("SERVER: WaitingForAuth, read '%s'", line
);
1030 if (g_strcmp0 (line
, "AUTH") == 0)
1032 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1033 debug_print ("SERVER: writing '%s'", s
);
1034 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1043 else if (g_str_has_prefix (line
, "AUTH "))
1046 const gchar
*encoded
;
1047 const gchar
*mech_name
;
1048 GType auth_mech_to_use_gtype
;
1050 tokens
= g_strsplit (line
, " ", 0);
1052 switch (g_strv_length (tokens
))
1055 /* no initial response */
1056 mech_name
= tokens
[1];
1061 /* initial response */
1062 mech_name
= tokens
[1];
1063 encoded
= tokens
[2];
1070 "Unexpected line '%s' while in WaitingForAuth state",
1072 g_strfreev (tokens
);
1079 /* TODO: record that the client has attempted to use this mechanism */
1080 //g_debug ("client is trying '%s'", mech_name);
1082 auth_mech_to_use_gtype
= find_mech_by_name (auth
, mech_name
);
1083 if ((auth_mech_to_use_gtype
== (GType
) 0) ||
1084 (!allow_anonymous
&& g_strcmp0 (mech_name
, "ANONYMOUS") == 0))
1086 /* We don't support this auth mechanism */
1087 g_strfreev (tokens
);
1088 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1089 debug_print ("SERVER: writing '%s'", s
);
1090 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1097 /* stay in WAITING FOR AUTH */
1098 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1102 gchar
*initial_response
;
1103 gsize initial_response_len
;
1105 g_clear_object (&mech
);
1106 mech
= g_object_new (auth_mech_to_use_gtype
,
1107 "stream", auth
->priv
->stream
,
1108 "credentials", credentials
,
1111 initial_response
= NULL
;
1112 initial_response_len
= 0;
1113 if (encoded
!= NULL
)
1115 initial_response
= hexdecode (encoded
, &initial_response_len
, error
);
1116 if (initial_response
== NULL
)
1118 g_prefix_error (error
, "Initial response is malformed: ");
1119 /* invalid encoding, disconnect! */
1120 g_strfreev (tokens
);
1125 _g_dbus_auth_mechanism_server_initiate (mech
,
1127 initial_response_len
);
1128 g_free (initial_response
);
1129 g_strfreev (tokens
);
1132 switch (_g_dbus_auth_mechanism_server_get_state (mech
))
1134 case G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED
:
1135 if (observer
!= NULL
&&
1136 !g_dbus_auth_observer_authorize_authenticated_peer (observer
,
1141 g_set_error_literal (error
,
1144 _("Cancelled via GDBusAuthObserver::authorize-authenticated-peer"));
1149 s
= g_strdup_printf ("OK %s\r\n", guid
);
1150 debug_print ("SERVER: writing '%s'", s
);
1151 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1157 state
= SERVER_STATE_WAITING_FOR_BEGIN
;
1161 case G_DBUS_AUTH_MECHANISM_STATE_REJECTED
:
1162 s
= get_auth_mechanisms (auth
, allow_anonymous
, "REJECTED ", "\r\n", " ");
1163 debug_print ("SERVER: writing '%s'", s
);
1164 if (!g_data_output_stream_put_string (dos
, s
, cancellable
, error
))
1170 state
= SERVER_STATE_WAITING_FOR_AUTH
;
1173 case G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA
:
1174 state
= SERVER_STATE_WAITING_FOR_DATA
;
1177 case G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND
:
1182 data
= _g_dbus_auth_mechanism_server_data_send (mech
, &data_len
);
1185 gchar
*encoded_data
;
1187 encoded_data
= _g_dbus_hexencode (data
, data_len
);
1188 s
= g_strdup_printf ("DATA %s\r\n", encoded_data
);
1189 g_free (encoded_data
);
1192 debug_print ("SERVER: writing '%s'", s
);
1193 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 /* ---------------------------------------------------------------------------------------------------- */