1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-auth.h"
27 #include "dbus-string.h"
28 #include "dbus-list.h"
29 #include "dbus-internals.h"
30 #include "dbus-keyring.h"
32 #include "dbus-protocol.h"
33 #include "dbus-credentials.h"
36 * @defgroup DBusAuth Authentication
37 * @ingroup DBusInternals
38 * @brief DBusAuth object
40 * DBusAuth manages the authentication negotiation when a connection
41 * is first established, and also manage any encryption used over a
44 * @todo some SASL profiles require sending the empty string as a
45 * challenge/response, but we don't currently allow that in our
48 * @todo right now sometimes both ends will block waiting for input
49 * from the other end, e.g. if there's an error during
52 * @todo the cookie keyring needs to be cached globally not just
53 * per-auth (which raises threadsafety issues too)
55 * @todo grep FIXME in dbus-auth.c
59 * @defgroup DBusAuthInternals Authentication implementation details
60 * @ingroup DBusInternals
61 * @brief DBusAuth implementation details
63 * Private details of authentication code.
69 * This function appends an initial client response to the given string
71 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
72 DBusString *response);
75 * This function processes a block of data received from the peer.
76 * i.e. handles a DATA command.
78 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
79 const DBusString *data);
82 * This function encodes a block of data from the peer.
84 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
85 const DBusString *data,
89 * This function decodes a block of data from the peer.
91 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
92 const DBusString *data,
96 * This function is called when the mechanism is abandoned.
98 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
101 * Virtual table representing a particular auth mechanism.
105 const char *mechanism; /**< Name of the mechanism */
106 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
107 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
108 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
109 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
110 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
111 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
112 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
113 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
114 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
115 } DBusAuthMechanismHandler;
118 * Enumeration for the known authentication commands.
121 DBUS_AUTH_COMMAND_AUTH,
122 DBUS_AUTH_COMMAND_CANCEL,
123 DBUS_AUTH_COMMAND_DATA,
124 DBUS_AUTH_COMMAND_BEGIN,
125 DBUS_AUTH_COMMAND_REJECTED,
126 DBUS_AUTH_COMMAND_OK,
127 DBUS_AUTH_COMMAND_ERROR,
128 DBUS_AUTH_COMMAND_UNKNOWN,
129 DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD,
130 DBUS_AUTH_COMMAND_AGREE_UNIX_FD
134 * Auth state function, determines the reaction to incoming events for
135 * a particular state. Returns whether we had enough memory to
136 * complete the operation.
138 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
139 DBusAuthCommand command,
140 const DBusString *args);
143 * Information about a auth state.
147 const char *name; /**< Name of the state */
148 DBusAuthStateFunction handler; /**< State function for this state */
152 * Internal members of DBusAuth.
156 int refcount; /**< reference count */
157 const char *side; /**< Client or server */
159 DBusString incoming; /**< Incoming data buffer */
160 DBusString outgoing; /**< Outgoing data buffer */
162 const DBusAuthStateData *state; /**< Current protocol state */
164 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
166 DBusString identity; /**< Current identity we're authorizing
170 DBusCredentials *credentials; /**< Credentials read from socket
173 DBusCredentials *authorized_identity; /**< Credentials that are authorized */
175 DBusCredentials *desired_identity; /**< Identity client has requested */
177 DBusString context; /**< Cookie scope */
178 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
179 int cookie_id; /**< ID of cookie to use */
180 DBusString challenge; /**< Challenge sent to client */
182 char **allowed_mechs; /**< Mechanisms we're allowed to use,
183 * or #NULL if we can use any
186 unsigned int needed_memory : 1; /**< We needed memory to continue since last
187 * successful getting something done
189 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
190 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
191 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
193 unsigned int unix_fd_possible : 1; /**< This side could do unix fd passing */
194 unsigned int unix_fd_negotiated : 1; /**< Unix fd was successfully negotiated */
198 * "Subclass" of DBusAuth for client side
202 DBusAuth base; /**< Parent class */
204 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
206 DBusString guid_from_server; /**< GUID received from server */
211 * "Subclass" of DBusAuth for server side.
215 DBusAuth base; /**< Parent class */
217 int failures; /**< Number of times client has been rejected */
218 int max_failures; /**< Number of times we reject before disconnect */
220 DBusString guid; /**< Our globally unique ID in hex encoding */
224 static void goto_state (DBusAuth *auth,
225 const DBusAuthStateData *new_state);
226 static dbus_bool_t send_auth (DBusAuth *auth,
227 const DBusAuthMechanismHandler *mech);
228 static dbus_bool_t send_data (DBusAuth *auth,
230 static dbus_bool_t send_rejected (DBusAuth *auth);
231 static dbus_bool_t send_error (DBusAuth *auth,
232 const char *message);
233 static dbus_bool_t send_ok (DBusAuth *auth);
234 static dbus_bool_t send_begin (DBusAuth *auth);
235 static dbus_bool_t send_cancel (DBusAuth *auth);
236 static dbus_bool_t send_negotiate_unix_fd (DBusAuth *auth);
237 static dbus_bool_t send_agree_unix_fd (DBusAuth *auth);
243 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
244 DBusAuthCommand command,
245 const DBusString *args);
246 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
247 DBusAuthCommand command,
248 const DBusString *args);
249 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
250 DBusAuthCommand command,
251 const DBusString *args);
253 static const DBusAuthStateData server_state_waiting_for_auth = {
254 "WaitingForAuth", handle_server_state_waiting_for_auth
256 static const DBusAuthStateData server_state_waiting_for_data = {
257 "WaitingForData", handle_server_state_waiting_for_data
259 static const DBusAuthStateData server_state_waiting_for_begin = {
260 "WaitingForBegin", handle_server_state_waiting_for_begin
267 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
268 DBusAuthCommand command,
269 const DBusString *args);
270 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
271 DBusAuthCommand command,
272 const DBusString *args);
273 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
274 DBusAuthCommand command,
275 const DBusString *args);
276 static dbus_bool_t handle_client_state_waiting_for_agree_unix_fd (DBusAuth *auth,
277 DBusAuthCommand command,
278 const DBusString *args);
280 static const DBusAuthStateData client_state_need_send_auth = {
283 static const DBusAuthStateData client_state_waiting_for_data = {
284 "WaitingForData", handle_client_state_waiting_for_data
286 static const DBusAuthStateData client_state_waiting_for_ok = {
287 "WaitingForOK", handle_client_state_waiting_for_ok
289 static const DBusAuthStateData client_state_waiting_for_reject = {
290 "WaitingForReject", handle_client_state_waiting_for_reject
292 static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
293 "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
297 * Common terminal states. Terminal states have handler == NULL.
300 static const DBusAuthStateData common_state_authenticated = {
301 "Authenticated", NULL
304 static const DBusAuthStateData common_state_need_disconnect = {
305 "NeedDisconnect", NULL
308 static const char auth_side_client[] = "client";
309 static const char auth_side_server[] = "server";
311 * @param auth the auth conversation
312 * @returns #TRUE if the conversation is the server side
314 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
316 * @param auth the auth conversation
317 * @returns #TRUE if the conversation is the client side
319 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
321 * @param auth the auth conversation
322 * @returns auth cast to DBusAuthClient
324 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
326 * @param auth the auth conversation
327 * @returns auth cast to DBusAuthServer
329 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
332 * The name of the auth ("client" or "server")
333 * @param auth the auth conversation
336 #define DBUS_AUTH_NAME(auth) ((auth)->side)
339 _dbus_auth_new (int size)
343 auth = dbus_malloc0 (size);
349 auth->keyring = NULL;
350 auth->cookie_id = -1;
352 /* note that we don't use the max string length feature,
353 * because you can't use that feature if you're going to
354 * try to recover from out-of-memory (it creates
355 * what looks like unrecoverable inability to alloc
356 * more space in the string). But we do handle
357 * overlong buffers in _dbus_auth_do_work().
360 if (!_dbus_string_init (&auth->incoming))
363 if (!_dbus_string_init (&auth->outgoing))
366 if (!_dbus_string_init (&auth->identity))
369 if (!_dbus_string_init (&auth->context))
372 if (!_dbus_string_init (&auth->challenge))
375 /* default context if none is specified */
376 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
379 auth->credentials = _dbus_credentials_new ();
380 if (auth->credentials == NULL)
383 auth->authorized_identity = _dbus_credentials_new ();
384 if (auth->authorized_identity == NULL)
387 auth->desired_identity = _dbus_credentials_new ();
388 if (auth->desired_identity == NULL)
395 _dbus_credentials_unref (auth->desired_identity);
398 _dbus_credentials_unref (auth->authorized_identity);
400 _dbus_credentials_unref (auth->credentials);
402 /* last alloc was an append to context, which is freed already below */ ;
404 _dbus_string_free (&auth->challenge);
406 _dbus_string_free (&auth->context);
408 _dbus_string_free (&auth->identity);
410 _dbus_string_free (&auth->outgoing);
412 _dbus_string_free (&auth->incoming);
419 shutdown_mech (DBusAuth *auth)
421 /* Cancel any auth */
422 auth->already_asked_for_initial_response = FALSE;
423 _dbus_string_set_length (&auth->identity, 0);
425 _dbus_credentials_clear (auth->authorized_identity);
426 _dbus_credentials_clear (auth->desired_identity);
428 if (auth->mech != NULL)
430 _dbus_verbose ("%s: Shutting down mechanism %s\n",
431 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
433 if (DBUS_AUTH_IS_CLIENT (auth))
434 (* auth->mech->client_shutdown_func) (auth);
436 (* auth->mech->server_shutdown_func) (auth);
443 * DBUS_COOKIE_SHA1 mechanism
446 /* Returns TRUE but with an empty string hash if the
447 * cookie_id isn't known. As with all this code
448 * TRUE just means we had enough memory.
451 sha1_compute_hash (DBusAuth *auth,
453 const DBusString *server_challenge,
454 const DBusString *client_challenge,
461 _dbus_assert (auth->keyring != NULL);
465 if (!_dbus_string_init (&cookie))
468 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
472 if (_dbus_string_get_length (&cookie) == 0)
478 if (!_dbus_string_init (&to_hash))
481 if (!_dbus_string_copy (server_challenge, 0,
482 &to_hash, _dbus_string_get_length (&to_hash)))
485 if (!_dbus_string_append (&to_hash, ":"))
488 if (!_dbus_string_copy (client_challenge, 0,
489 &to_hash, _dbus_string_get_length (&to_hash)))
492 if (!_dbus_string_append (&to_hash, ":"))
495 if (!_dbus_string_copy (&cookie, 0,
496 &to_hash, _dbus_string_get_length (&to_hash)))
499 if (!_dbus_sha_compute (&to_hash, hash))
505 _dbus_string_zero (&to_hash);
506 _dbus_string_free (&to_hash);
508 _dbus_string_zero (&cookie);
509 _dbus_string_free (&cookie);
513 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
514 * entropy, we use 128. This is the number of bytes in the random
517 #define N_CHALLENGE_BYTES (128/8)
520 sha1_handle_first_client_response (DBusAuth *auth,
521 const DBusString *data)
523 /* We haven't sent a challenge yet, we're expecting a desired
524 * username from the client.
533 _dbus_string_set_length (&auth->challenge, 0);
535 if (_dbus_string_get_length (data) > 0)
537 if (_dbus_string_get_length (&auth->identity) > 0)
539 /* Tried to send two auth identities, wtf */
540 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
541 DBUS_AUTH_NAME (auth));
542 return send_rejected (auth);
546 /* this is our auth identity */
547 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
552 if (!_dbus_credentials_add_from_user (auth->desired_identity, data))
554 _dbus_verbose ("%s: Did not get a valid username from client\n",
555 DBUS_AUTH_NAME (auth));
556 return send_rejected (auth);
559 if (!_dbus_string_init (&tmp))
562 if (!_dbus_string_init (&tmp2))
564 _dbus_string_free (&tmp);
568 /* we cache the keyring for speed, so here we drop it if it's the
569 * wrong one. FIXME caching the keyring here is useless since we use
570 * a different DBusAuth for every connection.
573 !_dbus_keyring_is_for_credentials (auth->keyring,
574 auth->desired_identity))
576 _dbus_keyring_unref (auth->keyring);
577 auth->keyring = NULL;
580 if (auth->keyring == NULL)
582 dbus_error_init (&error);
583 auth->keyring = _dbus_keyring_new_for_credentials (auth->desired_identity,
587 if (auth->keyring == NULL)
589 if (dbus_error_has_name (&error,
590 DBUS_ERROR_NO_MEMORY))
592 dbus_error_free (&error);
597 _DBUS_ASSERT_ERROR_IS_SET (&error);
598 _dbus_verbose ("%s: Error loading keyring: %s\n",
599 DBUS_AUTH_NAME (auth), error.message);
600 if (send_rejected (auth))
601 retval = TRUE; /* retval is only about mem */
602 dbus_error_free (&error);
608 _dbus_assert (!dbus_error_is_set (&error));
612 _dbus_assert (auth->keyring != NULL);
614 dbus_error_init (&error);
615 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
616 if (auth->cookie_id < 0)
618 _DBUS_ASSERT_ERROR_IS_SET (&error);
619 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
620 DBUS_AUTH_NAME (auth), error.message);
621 if (send_rejected (auth))
623 dbus_error_free (&error);
628 _dbus_assert (!dbus_error_is_set (&error));
631 if (!_dbus_string_copy (&auth->context, 0,
632 &tmp2, _dbus_string_get_length (&tmp2)))
635 if (!_dbus_string_append (&tmp2, " "))
638 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
641 if (!_dbus_string_append (&tmp2, " "))
644 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
647 _dbus_string_set_length (&auth->challenge, 0);
648 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
651 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
652 _dbus_string_get_length (&tmp2)))
655 if (!send_data (auth, &tmp2))
658 goto_state (auth, &server_state_waiting_for_data);
662 _dbus_string_zero (&tmp);
663 _dbus_string_free (&tmp);
664 _dbus_string_zero (&tmp2);
665 _dbus_string_free (&tmp2);
671 sha1_handle_second_client_response (DBusAuth *auth,
672 const DBusString *data)
674 /* We are expecting a response which is the hex-encoded client
675 * challenge, space, then SHA-1 hash of the concatenation of our
676 * challenge, ":", client challenge, ":", secret key, all
680 DBusString client_challenge;
681 DBusString client_hash;
683 DBusString correct_hash;
687 if (!_dbus_string_find_blank (data, 0, &i))
689 _dbus_verbose ("%s: no space separator in client response\n",
690 DBUS_AUTH_NAME (auth));
691 return send_rejected (auth);
694 if (!_dbus_string_init (&client_challenge))
697 if (!_dbus_string_init (&client_hash))
700 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
704 _dbus_string_skip_blank (data, i, &i);
706 if (!_dbus_string_copy_len (data, i,
707 _dbus_string_get_length (data) - i,
712 if (_dbus_string_get_length (&client_challenge) == 0 ||
713 _dbus_string_get_length (&client_hash) == 0)
715 _dbus_verbose ("%s: zero-length client challenge or hash\n",
716 DBUS_AUTH_NAME (auth));
717 if (send_rejected (auth))
722 if (!_dbus_string_init (&correct_hash))
725 if (!sha1_compute_hash (auth, auth->cookie_id,
731 /* if cookie_id was invalid, then we get an empty hash */
732 if (_dbus_string_get_length (&correct_hash) == 0)
734 if (send_rejected (auth))
739 if (!_dbus_string_equal (&client_hash, &correct_hash))
741 if (send_rejected (auth))
746 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
747 auth->desired_identity))
750 /* Copy process ID from the socket credentials if it's there
752 if (!_dbus_credentials_add_credential (auth->authorized_identity,
753 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
760 _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
761 DBUS_AUTH_NAME (auth));
766 _dbus_string_zero (&correct_hash);
767 _dbus_string_free (&correct_hash);
769 _dbus_string_zero (&client_hash);
770 _dbus_string_free (&client_hash);
772 _dbus_string_free (&client_challenge);
778 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
779 const DBusString *data)
781 if (auth->cookie_id < 0)
782 return sha1_handle_first_client_response (auth, data);
784 return sha1_handle_second_client_response (auth, data);
788 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
790 auth->cookie_id = -1;
791 _dbus_string_set_length (&auth->challenge, 0);
795 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
796 DBusString *response)
803 if (!_dbus_string_init (&username))
806 if (!_dbus_append_user_from_current_process (&username))
809 if (!_dbus_string_hex_encode (&username, 0,
811 _dbus_string_get_length (response)))
817 _dbus_string_free (&username);
823 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
824 const DBusString *data)
826 /* The data we get from the server should be the cookie context
827 * name, the cookie ID, and the server challenge, separated by
828 * spaces. We send back our challenge string and the correct hash.
832 DBusString cookie_id_str;
833 DBusString server_challenge;
834 DBusString client_challenge;
835 DBusString correct_hash;
842 if (!_dbus_string_find_blank (data, 0, &i))
844 if (send_error (auth,
845 "Server did not send context/ID/challenge properly"))
850 if (!_dbus_string_init (&context))
853 if (!_dbus_string_copy_len (data, 0, i,
857 _dbus_string_skip_blank (data, i, &i);
858 if (!_dbus_string_find_blank (data, i, &j))
860 if (send_error (auth,
861 "Server did not send context/ID/challenge properly"))
866 if (!_dbus_string_init (&cookie_id_str))
869 if (!_dbus_string_copy_len (data, i, j - i,
873 if (!_dbus_string_init (&server_challenge))
877 _dbus_string_skip_blank (data, i, &i);
878 j = _dbus_string_get_length (data);
880 if (!_dbus_string_copy_len (data, i, j - i,
881 &server_challenge, 0))
884 if (!_dbus_keyring_validate_context (&context))
886 if (send_error (auth, "Server sent invalid cookie context"))
891 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
893 if (send_error (auth, "Could not parse cookie ID as an integer"))
898 if (_dbus_string_get_length (&server_challenge) == 0)
900 if (send_error (auth, "Empty server challenge string"))
905 if (auth->keyring == NULL)
909 dbus_error_init (&error);
910 auth->keyring = _dbus_keyring_new_for_credentials (NULL,
914 if (auth->keyring == NULL)
916 if (dbus_error_has_name (&error,
917 DBUS_ERROR_NO_MEMORY))
919 dbus_error_free (&error);
924 _DBUS_ASSERT_ERROR_IS_SET (&error);
926 _dbus_verbose ("%s: Error loading keyring: %s\n",
927 DBUS_AUTH_NAME (auth), error.message);
929 if (send_error (auth, "Could not load cookie file"))
930 retval = TRUE; /* retval is only about mem */
932 dbus_error_free (&error);
938 _dbus_assert (!dbus_error_is_set (&error));
942 _dbus_assert (auth->keyring != NULL);
944 if (!_dbus_string_init (&tmp))
947 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
950 if (!_dbus_string_init (&client_challenge))
953 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
956 if (!_dbus_string_init (&correct_hash))
959 if (!sha1_compute_hash (auth, val,
965 if (_dbus_string_get_length (&correct_hash) == 0)
967 /* couldn't find the cookie ID or something */
968 if (send_error (auth, "Don't have the requested cookie ID"))
973 _dbus_string_set_length (&tmp, 0);
975 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
976 _dbus_string_get_length (&tmp)))
979 if (!_dbus_string_append (&tmp, " "))
982 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
983 _dbus_string_get_length (&tmp)))
986 if (!send_data (auth, &tmp))
992 _dbus_string_zero (&correct_hash);
993 _dbus_string_free (&correct_hash);
995 _dbus_string_free (&client_challenge);
997 _dbus_string_zero (&tmp);
998 _dbus_string_free (&tmp);
1000 _dbus_string_free (&server_challenge);
1002 _dbus_string_free (&cookie_id_str);
1004 _dbus_string_free (&context);
1010 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1012 auth->cookie_id = -1;
1013 _dbus_string_set_length (&auth->challenge, 0);
1017 * EXTERNAL mechanism
1021 handle_server_data_external_mech (DBusAuth *auth,
1022 const DBusString *data)
1024 if (_dbus_credentials_are_anonymous (auth->credentials))
1026 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1027 DBUS_AUTH_NAME (auth));
1028 return send_rejected (auth);
1031 if (_dbus_string_get_length (data) > 0)
1033 if (_dbus_string_get_length (&auth->identity) > 0)
1035 /* Tried to send two auth identities, wtf */
1036 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1037 DBUS_AUTH_NAME (auth));
1038 return send_rejected (auth);
1042 /* this is our auth identity */
1043 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1048 /* Poke client for an auth identity, if none given */
1049 if (_dbus_string_get_length (&auth->identity) == 0 &&
1050 !auth->already_asked_for_initial_response)
1052 if (send_data (auth, NULL))
1054 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1055 DBUS_AUTH_NAME (auth));
1056 auth->already_asked_for_initial_response = TRUE;
1057 goto_state (auth, &server_state_waiting_for_data);
1064 _dbus_credentials_clear (auth->desired_identity);
1066 /* If auth->identity is still empty here, then client
1067 * responded with an empty string after we poked it for
1068 * an initial response. This means to try to auth the
1069 * identity provided in the credentials.
1071 if (_dbus_string_get_length (&auth->identity) == 0)
1073 if (!_dbus_credentials_add_credentials (auth->desired_identity,
1076 return FALSE; /* OOM */
1081 if (!_dbus_credentials_add_from_user (auth->desired_identity,
1084 _dbus_verbose ("%s: could not get credentials from uid string\n",
1085 DBUS_AUTH_NAME (auth));
1086 return send_rejected (auth);
1090 if (_dbus_credentials_are_anonymous (auth->desired_identity))
1092 _dbus_verbose ("%s: desired user %s is no good\n",
1093 DBUS_AUTH_NAME (auth),
1094 _dbus_string_get_const_data (&auth->identity));
1095 return send_rejected (auth);
1098 if (_dbus_credentials_are_superset (auth->credentials,
1099 auth->desired_identity))
1101 /* client has authenticated */
1102 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
1103 auth->desired_identity))
1106 /* also copy process ID from the socket credentials
1108 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1109 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1113 /* also copy audit data from the socket credentials
1115 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1116 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1120 if (!send_ok (auth))
1123 _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1124 DBUS_AUTH_NAME (auth));
1130 _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1131 DBUS_AUTH_NAME (auth));
1132 return send_rejected (auth);
1137 handle_server_shutdown_external_mech (DBusAuth *auth)
1143 handle_client_initial_response_external_mech (DBusAuth *auth,
1144 DBusString *response)
1146 /* We always append our UID as an initial response, so the server
1147 * doesn't have to send back an empty challenge to check whether we
1148 * want to specify an identity. i.e. this avoids a round trip that
1149 * the spec for the EXTERNAL mechanism otherwise requires.
1151 DBusString plaintext;
1153 if (!_dbus_string_init (&plaintext))
1156 if (!_dbus_append_user_from_current_process (&plaintext))
1159 if (!_dbus_string_hex_encode (&plaintext, 0,
1161 _dbus_string_get_length (response)))
1164 _dbus_string_free (&plaintext);
1169 _dbus_string_free (&plaintext);
1174 handle_client_data_external_mech (DBusAuth *auth,
1175 const DBusString *data)
1182 handle_client_shutdown_external_mech (DBusAuth *auth)
1188 * ANONYMOUS mechanism
1192 handle_server_data_anonymous_mech (DBusAuth *auth,
1193 const DBusString *data)
1195 if (_dbus_string_get_length (data) > 0)
1197 /* Client is allowed to send "trace" data, the only defined
1198 * meaning is that if it contains '@' it is an email address,
1199 * and otherwise it is anything else, and it's supposed to be
1202 if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1204 _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1205 DBUS_AUTH_NAME (auth));
1206 return send_rejected (auth);
1209 _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1210 DBUS_AUTH_NAME (auth),
1211 _dbus_string_get_const_data (data));
1214 /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1215 _dbus_credentials_clear (auth->desired_identity);
1217 /* Copy process ID from the socket credentials
1219 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1220 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1224 /* Anonymous is always allowed */
1225 if (!send_ok (auth))
1228 _dbus_verbose ("%s: authenticated client as anonymous\n",
1229 DBUS_AUTH_NAME (auth));
1235 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1241 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1242 DBusString *response)
1244 /* Our initial response is a "trace" string which must be valid UTF-8
1245 * and must be an email address if it contains '@'.
1246 * We just send the dbus implementation info, like a user-agent or
1247 * something, because... why not. There's nothing guaranteed here
1248 * though, we could change it later.
1250 DBusString plaintext;
1252 if (!_dbus_string_init (&plaintext))
1255 if (!_dbus_string_append (&plaintext,
1256 "libdbus " DBUS_VERSION_STRING))
1259 if (!_dbus_string_hex_encode (&plaintext, 0,
1261 _dbus_string_get_length (response)))
1264 _dbus_string_free (&plaintext);
1269 _dbus_string_free (&plaintext);
1274 handle_client_data_anonymous_mech (DBusAuth *auth,
1275 const DBusString *data)
1282 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1287 /* Put mechanisms here in order of preference.
1288 * Right now we have:
1290 * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1291 * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1292 * - ANONYMOUS checks nothing but doesn't auth the person as a user
1294 * We might ideally add a mechanism to chain to Cyrus SASL so we can
1295 * use its mechanisms as well.
1298 static const DBusAuthMechanismHandler
1299 all_mechanisms[] = {
1301 handle_server_data_external_mech,
1303 handle_server_shutdown_external_mech,
1304 handle_client_initial_response_external_mech,
1305 handle_client_data_external_mech,
1307 handle_client_shutdown_external_mech },
1308 { "DBUS_COOKIE_SHA1",
1309 handle_server_data_cookie_sha1_mech,
1311 handle_server_shutdown_cookie_sha1_mech,
1312 handle_client_initial_response_cookie_sha1_mech,
1313 handle_client_data_cookie_sha1_mech,
1315 handle_client_shutdown_cookie_sha1_mech },
1317 handle_server_data_anonymous_mech,
1319 handle_server_shutdown_anonymous_mech,
1320 handle_client_initial_response_anonymous_mech,
1321 handle_client_data_anonymous_mech,
1323 handle_client_shutdown_anonymous_mech },
1327 static const DBusAuthMechanismHandler*
1328 find_mech (const DBusString *name,
1329 char **allowed_mechs)
1333 if (allowed_mechs != NULL &&
1334 !_dbus_string_array_contains ((const char**) allowed_mechs,
1335 _dbus_string_get_const_data (name)))
1339 while (all_mechanisms[i].mechanism != NULL)
1341 if (_dbus_string_equal_c_str (name,
1342 all_mechanisms[i].mechanism))
1344 return &all_mechanisms[i];
1353 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1355 DBusString auth_command;
1357 if (!_dbus_string_init (&auth_command))
1360 if (!_dbus_string_append (&auth_command,
1363 _dbus_string_free (&auth_command);
1367 if (!_dbus_string_append (&auth_command,
1370 _dbus_string_free (&auth_command);
1374 if (mech->client_initial_response_func != NULL)
1376 if (!_dbus_string_append (&auth_command, " "))
1378 _dbus_string_free (&auth_command);
1382 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1384 _dbus_string_free (&auth_command);
1389 if (!_dbus_string_append (&auth_command,
1392 _dbus_string_free (&auth_command);
1396 if (!_dbus_string_copy (&auth_command, 0,
1398 _dbus_string_get_length (&auth->outgoing)))
1400 _dbus_string_free (&auth_command);
1404 _dbus_string_free (&auth_command);
1405 shutdown_mech (auth);
1407 goto_state (auth, &client_state_waiting_for_data);
1413 send_data (DBusAuth *auth, DBusString *data)
1417 if (data == NULL || _dbus_string_get_length (data) == 0)
1418 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1421 old_len = _dbus_string_get_length (&auth->outgoing);
1422 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1425 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1426 _dbus_string_get_length (&auth->outgoing)))
1429 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1435 _dbus_string_set_length (&auth->outgoing, old_len);
1442 send_rejected (DBusAuth *auth)
1445 DBusAuthServer *server_auth;
1448 if (!_dbus_string_init (&command))
1451 if (!_dbus_string_append (&command,
1456 while (all_mechanisms[i].mechanism != NULL)
1458 if (!_dbus_string_append (&command,
1462 if (!_dbus_string_append (&command,
1463 all_mechanisms[i].mechanism))
1469 if (!_dbus_string_append (&command, "\r\n"))
1472 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1473 _dbus_string_get_length (&auth->outgoing)))
1476 shutdown_mech (auth);
1478 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1479 server_auth = DBUS_AUTH_SERVER (auth);
1480 server_auth->failures += 1;
1482 if (server_auth->failures >= server_auth->max_failures)
1483 goto_state (auth, &common_state_need_disconnect);
1485 goto_state (auth, &server_state_waiting_for_auth);
1487 _dbus_string_free (&command);
1492 _dbus_string_free (&command);
1497 send_error (DBusAuth *auth, const char *message)
1499 return _dbus_string_append_printf (&auth->outgoing,
1500 "ERROR \"%s\"\r\n", message);
1504 send_ok (DBusAuth *auth)
1508 orig_len = _dbus_string_get_length (&auth->outgoing);
1510 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1511 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1514 _dbus_string_get_length (&auth->outgoing)) &&
1515 _dbus_string_append (&auth->outgoing, "\r\n"))
1517 goto_state (auth, &server_state_waiting_for_begin);
1522 _dbus_string_set_length (&auth->outgoing, orig_len);
1528 send_begin (DBusAuth *auth)
1531 if (!_dbus_string_append (&auth->outgoing,
1535 goto_state (auth, &common_state_authenticated);
1540 process_ok(DBusAuth *auth,
1541 const DBusString *args_from_ok) {
1545 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1546 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1548 /* We decode the hex string to binary, using guid_from_server as scratch... */
1551 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1552 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1555 /* now clear out the scratch */
1556 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1558 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1561 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1562 end_of_hex, _dbus_string_get_length (args_from_ok));
1563 goto_state (auth, &common_state_need_disconnect);
1567 if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1568 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1572 _dbus_verbose ("Got GUID '%s' from the server\n",
1573 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1575 if (auth->unix_fd_possible)
1576 return send_negotiate_unix_fd(auth);
1578 _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1579 return send_begin (auth);
1583 send_cancel (DBusAuth *auth)
1585 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1587 goto_state (auth, &client_state_waiting_for_reject);
1595 process_data (DBusAuth *auth,
1596 const DBusString *args,
1597 DBusAuthDataFunction data_func)
1602 if (!_dbus_string_init (&decoded))
1605 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1607 _dbus_string_free (&decoded);
1611 if (_dbus_string_get_length (args) != end)
1613 _dbus_string_free (&decoded);
1614 if (!send_error (auth, "Invalid hex encoding"))
1620 #ifdef DBUS_ENABLE_VERBOSE_MODE
1621 if (_dbus_string_validate_ascii (&decoded, 0,
1622 _dbus_string_get_length (&decoded)))
1623 _dbus_verbose ("%s: data: '%s'\n",
1624 DBUS_AUTH_NAME (auth),
1625 _dbus_string_get_const_data (&decoded));
1628 if (!(* data_func) (auth, &decoded))
1630 _dbus_string_free (&decoded);
1634 _dbus_string_free (&decoded);
1639 send_negotiate_unix_fd (DBusAuth *auth)
1641 if (!_dbus_string_append (&auth->outgoing,
1642 "NEGOTIATE_UNIX_FD\r\n"))
1645 goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1650 send_agree_unix_fd (DBusAuth *auth)
1652 _dbus_assert(auth->unix_fd_possible);
1654 auth->unix_fd_negotiated = TRUE;
1655 _dbus_verbose("Agreed to UNIX FD passing\n");
1657 if (!_dbus_string_append (&auth->outgoing,
1658 "AGREE_UNIX_FD\r\n"))
1661 goto_state (auth, &server_state_waiting_for_begin);
1666 handle_auth (DBusAuth *auth, const DBusString *args)
1668 if (_dbus_string_get_length (args) == 0)
1670 /* No args to the auth, send mechanisms */
1671 if (!send_rejected (auth))
1680 DBusString hex_response;
1682 _dbus_string_find_blank (args, 0, &i);
1684 if (!_dbus_string_init (&mech))
1687 if (!_dbus_string_init (&hex_response))
1689 _dbus_string_free (&mech);
1693 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1696 _dbus_string_skip_blank (args, i, &i);
1697 if (!_dbus_string_copy (args, i, &hex_response, 0))
1700 auth->mech = find_mech (&mech, auth->allowed_mechs);
1701 if (auth->mech != NULL)
1703 _dbus_verbose ("%s: Trying mechanism %s\n",
1704 DBUS_AUTH_NAME (auth),
1705 auth->mech->mechanism);
1707 if (!process_data (auth, &hex_response,
1708 auth->mech->server_data_func))
1713 /* Unsupported mechanism */
1714 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1715 DBUS_AUTH_NAME (auth),
1716 _dbus_string_get_const_data (&mech));
1718 if (!send_rejected (auth))
1722 _dbus_string_free (&mech);
1723 _dbus_string_free (&hex_response);
1729 _dbus_string_free (&mech);
1730 _dbus_string_free (&hex_response);
1736 handle_server_state_waiting_for_auth (DBusAuth *auth,
1737 DBusAuthCommand command,
1738 const DBusString *args)
1742 case DBUS_AUTH_COMMAND_AUTH:
1743 return handle_auth (auth, args);
1745 case DBUS_AUTH_COMMAND_CANCEL:
1746 case DBUS_AUTH_COMMAND_DATA:
1747 return send_error (auth, "Not currently in an auth conversation");
1749 case DBUS_AUTH_COMMAND_BEGIN:
1750 goto_state (auth, &common_state_need_disconnect);
1753 case DBUS_AUTH_COMMAND_ERROR:
1754 return send_rejected (auth);
1756 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1757 return send_error (auth, "Need to authenticate first");
1759 case DBUS_AUTH_COMMAND_REJECTED:
1760 case DBUS_AUTH_COMMAND_OK:
1761 case DBUS_AUTH_COMMAND_UNKNOWN:
1762 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1764 return send_error (auth, "Unknown command");
1769 handle_server_state_waiting_for_data (DBusAuth *auth,
1770 DBusAuthCommand command,
1771 const DBusString *args)
1775 case DBUS_AUTH_COMMAND_AUTH:
1776 return send_error (auth, "Sent AUTH while another AUTH in progress");
1778 case DBUS_AUTH_COMMAND_CANCEL:
1779 case DBUS_AUTH_COMMAND_ERROR:
1780 return send_rejected (auth);
1782 case DBUS_AUTH_COMMAND_DATA:
1783 return process_data (auth, args, auth->mech->server_data_func);
1785 case DBUS_AUTH_COMMAND_BEGIN:
1786 goto_state (auth, &common_state_need_disconnect);
1789 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1790 return send_error (auth, "Need to authenticate first");
1792 case DBUS_AUTH_COMMAND_REJECTED:
1793 case DBUS_AUTH_COMMAND_OK:
1794 case DBUS_AUTH_COMMAND_UNKNOWN:
1795 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1797 return send_error (auth, "Unknown command");
1802 handle_server_state_waiting_for_begin (DBusAuth *auth,
1803 DBusAuthCommand command,
1804 const DBusString *args)
1808 case DBUS_AUTH_COMMAND_AUTH:
1809 return send_error (auth, "Sent AUTH while expecting BEGIN");
1811 case DBUS_AUTH_COMMAND_DATA:
1812 return send_error (auth, "Sent DATA while expecting BEGIN");
1814 case DBUS_AUTH_COMMAND_BEGIN:
1815 goto_state (auth, &common_state_authenticated);
1818 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1819 if (auth->unix_fd_possible)
1820 return send_agree_unix_fd(auth);
1822 return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1824 case DBUS_AUTH_COMMAND_REJECTED:
1825 case DBUS_AUTH_COMMAND_OK:
1826 case DBUS_AUTH_COMMAND_UNKNOWN:
1827 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1829 return send_error (auth, "Unknown command");
1831 case DBUS_AUTH_COMMAND_CANCEL:
1832 case DBUS_AUTH_COMMAND_ERROR:
1833 return send_rejected (auth);
1837 /* return FALSE if no memory, TRUE if all OK */
1839 get_word (const DBusString *str,
1845 _dbus_string_skip_blank (str, *start, start);
1846 _dbus_string_find_blank (str, *start, &i);
1850 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1860 record_mechanisms (DBusAuth *auth,
1861 const DBusString *args)
1866 if (auth->already_got_mechanisms)
1869 len = _dbus_string_get_length (args);
1875 const DBusAuthMechanismHandler *mech;
1877 if (!_dbus_string_init (&m))
1880 if (!get_word (args, &next, &m))
1882 _dbus_string_free (&m);
1886 mech = find_mech (&m, auth->allowed_mechs);
1890 /* FIXME right now we try mechanisms in the order
1891 * the server lists them; should we do them in
1892 * some more deterministic order?
1894 * Probably in all_mechanisms order, our order of
1895 * preference. Of course when the server is us,
1896 * it lists things in that order anyhow.
1899 if (mech != &all_mechanisms[0])
1901 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1902 DBUS_AUTH_NAME (auth), mech->mechanism);
1904 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1907 _dbus_string_free (&m);
1913 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1914 DBUS_AUTH_NAME (auth), mech->mechanism);
1919 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1920 DBUS_AUTH_NAME (auth),
1921 _dbus_string_get_const_data (&m));
1924 _dbus_string_free (&m);
1927 auth->already_got_mechanisms = TRUE;
1932 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1938 process_rejected (DBusAuth *auth, const DBusString *args)
1940 const DBusAuthMechanismHandler *mech;
1941 DBusAuthClient *client;
1943 client = DBUS_AUTH_CLIENT (auth);
1945 if (!auth->already_got_mechanisms)
1947 if (!record_mechanisms (auth, args))
1951 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1953 mech = client->mechs_to_try->data;
1955 if (!send_auth (auth, mech))
1958 _dbus_list_pop_first (&client->mechs_to_try);
1960 _dbus_verbose ("%s: Trying mechanism %s\n",
1961 DBUS_AUTH_NAME (auth),
1967 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1968 DBUS_AUTH_NAME (auth));
1969 goto_state (auth, &common_state_need_disconnect);
1977 handle_client_state_waiting_for_data (DBusAuth *auth,
1978 DBusAuthCommand command,
1979 const DBusString *args)
1981 _dbus_assert (auth->mech != NULL);
1985 case DBUS_AUTH_COMMAND_DATA:
1986 return process_data (auth, args, auth->mech->client_data_func);
1988 case DBUS_AUTH_COMMAND_REJECTED:
1989 return process_rejected (auth, args);
1991 case DBUS_AUTH_COMMAND_OK:
1992 return process_ok(auth, args);
1994 case DBUS_AUTH_COMMAND_ERROR:
1995 return send_cancel (auth);
1997 case DBUS_AUTH_COMMAND_AUTH:
1998 case DBUS_AUTH_COMMAND_CANCEL:
1999 case DBUS_AUTH_COMMAND_BEGIN:
2000 case DBUS_AUTH_COMMAND_UNKNOWN:
2001 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2002 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2004 return send_error (auth, "Unknown command");
2009 handle_client_state_waiting_for_ok (DBusAuth *auth,
2010 DBusAuthCommand command,
2011 const DBusString *args)
2015 case DBUS_AUTH_COMMAND_REJECTED:
2016 return process_rejected (auth, args);
2018 case DBUS_AUTH_COMMAND_OK:
2019 return process_ok(auth, args);
2021 case DBUS_AUTH_COMMAND_DATA:
2022 case DBUS_AUTH_COMMAND_ERROR:
2023 return send_cancel (auth);
2025 case DBUS_AUTH_COMMAND_AUTH:
2026 case DBUS_AUTH_COMMAND_CANCEL:
2027 case DBUS_AUTH_COMMAND_BEGIN:
2028 case DBUS_AUTH_COMMAND_UNKNOWN:
2029 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2030 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2032 return send_error (auth, "Unknown command");
2037 handle_client_state_waiting_for_reject (DBusAuth *auth,
2038 DBusAuthCommand command,
2039 const DBusString *args)
2043 case DBUS_AUTH_COMMAND_REJECTED:
2044 return process_rejected (auth, args);
2046 case DBUS_AUTH_COMMAND_AUTH:
2047 case DBUS_AUTH_COMMAND_CANCEL:
2048 case DBUS_AUTH_COMMAND_DATA:
2049 case DBUS_AUTH_COMMAND_BEGIN:
2050 case DBUS_AUTH_COMMAND_OK:
2051 case DBUS_AUTH_COMMAND_ERROR:
2052 case DBUS_AUTH_COMMAND_UNKNOWN:
2053 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2054 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2056 goto_state (auth, &common_state_need_disconnect);
2062 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2063 DBusAuthCommand command,
2064 const DBusString *args)
2068 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2069 _dbus_assert(auth->unix_fd_possible);
2070 auth->unix_fd_negotiated = TRUE;
2071 _dbus_verbose("Successfully negotiated UNIX FD passing\n");
2072 return send_begin (auth);
2074 case DBUS_AUTH_COMMAND_ERROR:
2075 _dbus_assert(auth->unix_fd_possible);
2076 auth->unix_fd_negotiated = FALSE;
2077 _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2078 return send_begin (auth);
2080 case DBUS_AUTH_COMMAND_OK:
2081 case DBUS_AUTH_COMMAND_DATA:
2082 case DBUS_AUTH_COMMAND_REJECTED:
2083 case DBUS_AUTH_COMMAND_AUTH:
2084 case DBUS_AUTH_COMMAND_CANCEL:
2085 case DBUS_AUTH_COMMAND_BEGIN:
2086 case DBUS_AUTH_COMMAND_UNKNOWN:
2087 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2089 return send_error (auth, "Unknown command");
2094 * Mapping from command name to enum
2097 const char *name; /**< Name of the command */
2098 DBusAuthCommand command; /**< Corresponding enum */
2099 } DBusAuthCommandName;
2101 static const DBusAuthCommandName auth_command_names[] = {
2102 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2103 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2104 { "DATA", DBUS_AUTH_COMMAND_DATA },
2105 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2106 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2107 { "OK", DBUS_AUTH_COMMAND_OK },
2108 { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2109 { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2110 { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2113 static DBusAuthCommand
2114 lookup_command_from_name (DBusString *command)
2118 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2120 if (_dbus_string_equal_c_str (command,
2121 auth_command_names[i].name))
2122 return auth_command_names[i].command;
2125 return DBUS_AUTH_COMMAND_UNKNOWN;
2129 goto_state (DBusAuth *auth,
2130 const DBusAuthStateData *state)
2132 _dbus_verbose ("%s: going from state %s to state %s\n",
2133 DBUS_AUTH_NAME (auth),
2137 auth->state = state;
2140 /* returns whether to call it again right away */
2142 process_command (DBusAuth *auth)
2144 DBusAuthCommand command;
2151 /* _dbus_verbose ("%s: trying process_command()\n"); */
2156 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2159 if (!_dbus_string_init (&line))
2161 auth->needed_memory = TRUE;
2165 if (!_dbus_string_init (&args))
2167 _dbus_string_free (&line);
2168 auth->needed_memory = TRUE;
2172 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2175 if (!_dbus_string_validate_ascii (&line, 0,
2176 _dbus_string_get_length (&line)))
2178 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2179 DBUS_AUTH_NAME (auth));
2180 if (!send_error (auth, "Command contained non-ASCII"))
2186 _dbus_verbose ("%s: got command \"%s\"\n",
2187 DBUS_AUTH_NAME (auth),
2188 _dbus_string_get_const_data (&line));
2190 _dbus_string_find_blank (&line, 0, &i);
2191 _dbus_string_skip_blank (&line, i, &j);
2194 _dbus_string_delete (&line, i, j - i);
2196 if (!_dbus_string_move (&line, i, &args, 0))
2199 /* FIXME 1.0 we should probably validate that only the allowed
2200 * chars are in the command name
2203 command = lookup_command_from_name (&line);
2204 if (!(* auth->state->handler) (auth, command, &args))
2209 /* We've succeeded in processing the whole command so drop it out
2210 * of the incoming buffer and return TRUE to try another command.
2213 _dbus_string_delete (&auth->incoming, 0, eol);
2216 _dbus_string_delete (&auth->incoming, 0, 2);
2221 _dbus_string_free (&args);
2222 _dbus_string_free (&line);
2225 auth->needed_memory = TRUE;
2227 auth->needed_memory = FALSE;
2236 * @addtogroup DBusAuth
2241 * Creates a new auth conversation object for the server side.
2242 * See http://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
2243 * for full details on what this object does.
2245 * @returns the new object or #NULL if no memory
2248 _dbus_auth_server_new (const DBusString *guid)
2251 DBusAuthServer *server_auth;
2252 DBusString guid_copy;
2254 if (!_dbus_string_init (&guid_copy))
2257 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2259 _dbus_string_free (&guid_copy);
2263 auth = _dbus_auth_new (sizeof (DBusAuthServer));
2266 _dbus_string_free (&guid_copy);
2270 auth->side = auth_side_server;
2271 auth->state = &server_state_waiting_for_auth;
2273 server_auth = DBUS_AUTH_SERVER (auth);
2275 server_auth->guid = guid_copy;
2277 /* perhaps this should be per-mechanism with a lower
2280 server_auth->failures = 0;
2281 server_auth->max_failures = 6;
2287 * Creates a new auth conversation object for the client side.
2288 * See http://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
2289 * for full details on what this object does.
2291 * @returns the new object or #NULL if no memory
2294 _dbus_auth_client_new (void)
2297 DBusString guid_str;
2299 if (!_dbus_string_init (&guid_str))
2302 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2305 _dbus_string_free (&guid_str);
2309 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2311 auth->side = auth_side_client;
2312 auth->state = &client_state_need_send_auth;
2314 /* Start the auth conversation by sending AUTH for our default
2316 if (!send_auth (auth, &all_mechanisms[0]))
2318 _dbus_auth_unref (auth);
2326 * Creates a new auth conversation object for the client side of kdbus.
2327 * In fact it only initialize structures and sets authenticated state
2328 * because of different authentication-like mechanism in kdbus - policies
2329 * TODO Probably to be checked and modified when kdbus will be documented
2331 * @returns the new object or #NULL if no memory
2334 _dbus_auth_client_new_kdbus (void)
2337 DBusString guid_str;
2339 if (!_dbus_string_init (&guid_str))
2342 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2345 _dbus_string_free (&guid_str);
2349 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2351 auth->side = auth_side_client;
2352 auth->state = &common_state_authenticated;
2353 auth->unix_fd_negotiated = TRUE;
2355 /* Start the auth conversation by sending AUTH for our default
2357 /* if (!send_auth (auth, &all_mechanisms[0]))
2359 _dbus_auth_unref (auth);
2367 * Increments the refcount of an auth object.
2369 * @param auth the auth conversation
2370 * @returns the auth conversation
2373 _dbus_auth_ref (DBusAuth *auth)
2375 _dbus_assert (auth != NULL);
2377 auth->refcount += 1;
2383 * Decrements the refcount of an auth object.
2385 * @param auth the auth conversation
2388 _dbus_auth_unref (DBusAuth *auth)
2390 _dbus_assert (auth != NULL);
2391 _dbus_assert (auth->refcount > 0);
2393 auth->refcount -= 1;
2394 if (auth->refcount == 0)
2396 shutdown_mech (auth);
2398 if (DBUS_AUTH_IS_CLIENT (auth))
2400 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2401 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2405 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2407 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2411 _dbus_keyring_unref (auth->keyring);
2413 _dbus_string_free (&auth->context);
2414 _dbus_string_free (&auth->challenge);
2415 _dbus_string_free (&auth->identity);
2416 _dbus_string_free (&auth->incoming);
2417 _dbus_string_free (&auth->outgoing);
2419 dbus_free_string_array (auth->allowed_mechs);
2421 _dbus_credentials_unref (auth->credentials);
2422 _dbus_credentials_unref (auth->authorized_identity);
2423 _dbus_credentials_unref (auth->desired_identity);
2430 * Sets an array of authentication mechanism names
2431 * that we are willing to use.
2433 * @param auth the auth conversation
2434 * @param mechanisms #NULL-terminated array of mechanism names
2435 * @returns #FALSE if no memory
2438 _dbus_auth_set_mechanisms (DBusAuth *auth,
2439 const char **mechanisms)
2443 if (mechanisms != NULL)
2445 copy = _dbus_dup_string_array (mechanisms);
2452 dbus_free_string_array (auth->allowed_mechs);
2454 auth->allowed_mechs = copy;
2460 * @param auth the auth conversation object
2461 * @returns #TRUE if we're in a final state
2463 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2466 * Analyzes buffered input and moves the auth conversation forward,
2467 * returning the new state of the auth conversation.
2469 * @param auth the auth conversation
2470 * @returns the new state
2473 _dbus_auth_do_work (DBusAuth *auth)
2475 auth->needed_memory = FALSE;
2477 /* Max amount we'll buffer up before deciding someone's on crack */
2478 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2482 if (DBUS_AUTH_IN_END_STATE (auth))
2485 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2486 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2488 goto_state (auth, &common_state_need_disconnect);
2489 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2490 DBUS_AUTH_NAME (auth));
2494 while (process_command (auth));
2496 if (auth->needed_memory)
2497 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2498 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2499 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2500 else if (auth->state == &common_state_need_disconnect)
2501 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2502 else if (auth->state == &common_state_authenticated)
2503 return DBUS_AUTH_STATE_AUTHENTICATED;
2504 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2508 * Gets bytes that need to be sent to the peer we're conversing with.
2509 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2510 * to notify the auth object that they were written.
2512 * @param auth the auth conversation
2513 * @param str return location for a ref to the buffer to send
2514 * @returns #FALSE if nothing to send
2517 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2518 const DBusString **str)
2520 _dbus_assert (auth != NULL);
2521 _dbus_assert (str != NULL);
2525 if (_dbus_string_get_length (&auth->outgoing) == 0)
2528 *str = &auth->outgoing;
2534 * Notifies the auth conversation object that
2535 * the given number of bytes of the outgoing buffer
2536 * have been written out.
2538 * @param auth the auth conversation
2539 * @param bytes_sent number of bytes written out
2542 _dbus_auth_bytes_sent (DBusAuth *auth,
2545 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2546 DBUS_AUTH_NAME (auth),
2548 _dbus_string_get_const_data (&auth->outgoing));
2550 _dbus_string_delete (&auth->outgoing,
2555 * Get a buffer to be used for reading bytes from the peer we're conversing
2556 * with. Bytes should be appended to this buffer.
2558 * @param auth the auth conversation
2559 * @param buffer return location for buffer to append bytes to
2562 _dbus_auth_get_buffer (DBusAuth *auth,
2563 DBusString **buffer)
2565 _dbus_assert (auth != NULL);
2566 _dbus_assert (!auth->buffer_outstanding);
2568 *buffer = &auth->incoming;
2570 auth->buffer_outstanding = TRUE;
2574 * Returns a buffer with new data read into it.
2576 * @param auth the auth conversation
2577 * @param buffer the buffer being returned
2578 * @param bytes_read number of new bytes added
2581 _dbus_auth_return_buffer (DBusAuth *auth,
2585 _dbus_assert (buffer == &auth->incoming);
2586 _dbus_assert (auth->buffer_outstanding);
2588 auth->buffer_outstanding = FALSE;
2592 * Returns leftover bytes that were not used as part of the auth
2593 * conversation. These bytes will be part of the message stream
2594 * instead. This function may not be called until authentication has
2597 * @param auth the auth conversation
2598 * @param str return location for pointer to string of unused bytes
2601 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2602 const DBusString **str)
2604 if (!DBUS_AUTH_IN_END_STATE (auth))
2607 *str = &auth->incoming;
2612 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2613 * after we've gotten them and successfully moved them elsewhere.
2615 * @param auth the auth conversation
2618 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2620 if (!DBUS_AUTH_IN_END_STATE (auth))
2623 _dbus_string_set_length (&auth->incoming, 0);
2627 * Called post-authentication, indicates whether we need to encode
2628 * the message stream with _dbus_auth_encode_data() prior to
2629 * sending it to the peer.
2631 * @param auth the auth conversation
2632 * @returns #TRUE if we need to encode the stream
2635 _dbus_auth_needs_encoding (DBusAuth *auth)
2637 if (auth->state != &common_state_authenticated)
2640 if (auth->mech != NULL)
2642 if (DBUS_AUTH_IS_CLIENT (auth))
2643 return auth->mech->client_encode_func != NULL;
2645 return auth->mech->server_encode_func != NULL;
2652 * Called post-authentication, encodes a block of bytes for sending to
2653 * the peer. If no encoding was negotiated, just copies the bytes
2654 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2656 * @param auth the auth conversation
2657 * @param plaintext the plain text data
2658 * @param encoded initialized string to where encoded data is appended
2659 * @returns #TRUE if we had enough memory and successfully encoded
2662 _dbus_auth_encode_data (DBusAuth *auth,
2663 const DBusString *plaintext,
2664 DBusString *encoded)
2666 _dbus_assert (plaintext != encoded);
2668 if (auth->state != &common_state_authenticated)
2671 if (_dbus_auth_needs_encoding (auth))
2673 if (DBUS_AUTH_IS_CLIENT (auth))
2674 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2676 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2680 return _dbus_string_copy (plaintext, 0, encoded,
2681 _dbus_string_get_length (encoded));
2686 * Called post-authentication, indicates whether we need to decode
2687 * the message stream with _dbus_auth_decode_data() after
2688 * receiving it from the peer.
2690 * @param auth the auth conversation
2691 * @returns #TRUE if we need to encode the stream
2694 _dbus_auth_needs_decoding (DBusAuth *auth)
2696 if (auth->state != &common_state_authenticated)
2699 if (auth->mech != NULL)
2701 if (DBUS_AUTH_IS_CLIENT (auth))
2702 return auth->mech->client_decode_func != NULL;
2704 return auth->mech->server_decode_func != NULL;
2712 * Called post-authentication, decodes a block of bytes received from
2713 * the peer. If no encoding was negotiated, just copies the bytes (you
2714 * can avoid this by checking _dbus_auth_needs_decoding()).
2716 * @todo 1.0? We need to be able to distinguish "out of memory" error
2717 * from "the data is hosed" error.
2719 * @param auth the auth conversation
2720 * @param encoded the encoded data
2721 * @param plaintext initialized string where decoded data is appended
2722 * @returns #TRUE if we had enough memory and successfully decoded
2725 _dbus_auth_decode_data (DBusAuth *auth,
2726 const DBusString *encoded,
2727 DBusString *plaintext)
2729 _dbus_assert (plaintext != encoded);
2731 if (auth->state != &common_state_authenticated)
2734 if (_dbus_auth_needs_decoding (auth))
2736 if (DBUS_AUTH_IS_CLIENT (auth))
2737 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2739 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2743 return _dbus_string_copy (encoded, 0, plaintext,
2744 _dbus_string_get_length (plaintext));
2749 * Sets credentials received via reliable means from the operating
2752 * @param auth the auth conversation
2753 * @param credentials the credentials received
2754 * @returns #FALSE on OOM
2757 _dbus_auth_set_credentials (DBusAuth *auth,
2758 DBusCredentials *credentials)
2760 _dbus_credentials_clear (auth->credentials);
2761 return _dbus_credentials_add_credentials (auth->credentials,
2766 * Gets the identity we authorized the client as. Apps may have
2767 * different policies as to what identities they allow.
2769 * Returned credentials are not a copy and should not be modified
2771 * @param auth the auth conversation
2772 * @returns the credentials we've authorized BY REFERENCE do not modify
2775 _dbus_auth_get_identity (DBusAuth *auth)
2777 if (auth->state == &common_state_authenticated)
2779 return auth->authorized_identity;
2783 /* FIXME instead of this, keep an empty credential around that
2784 * doesn't require allocation or something
2786 /* return empty credentials */
2787 _dbus_assert (_dbus_credentials_are_empty (auth->authorized_identity));
2788 return auth->authorized_identity;
2793 * Gets the GUID from the server if we've authenticated; gets
2795 * @param auth the auth object
2796 * @returns the GUID in ASCII hex format
2799 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2801 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2803 if (auth->state == &common_state_authenticated)
2804 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2810 * Sets the "authentication context" which scopes cookies
2811 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2813 * @param auth the auth conversation
2814 * @param context the context
2815 * @returns #FALSE if no memory
2818 _dbus_auth_set_context (DBusAuth *auth,
2819 const DBusString *context)
2821 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2822 &auth->context, 0, _dbus_string_get_length (context));
2826 * Sets whether unix fd passing is potentially on the transport and
2827 * hence shall be negotiated.
2829 * @param auth the auth conversation
2830 * @param b TRUE when unix fd passing shall be negotiated, otherwise FALSE
2833 _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
2835 auth->unix_fd_possible = b;
2839 * Queries whether unix fd passing was successfully negotiated.
2841 * @param auth the auth conversion
2842 * @returns #TRUE when unix fd passing was negotiated.
2845 _dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
2847 return auth->unix_fd_negotiated;
2852 /* tests in dbus-auth-util.c */