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.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-auth.h"
26 #include "dbus-string.h"
27 #include "dbus-list.h"
28 #include "dbus-internals.h"
29 #include "dbus-keyring.h"
31 #include "dbus-protocol.h"
32 #include "dbus-credentials.h"
35 * @defgroup DBusAuth Authentication
36 * @ingroup DBusInternals
37 * @brief DBusAuth object
39 * DBusAuth manages the authentication negotiation when a connection
40 * is first established, and also manage any encryption used over a
43 * @todo some SASL profiles require sending the empty string as a
44 * challenge/response, but we don't currently allow that in our
47 * @todo right now sometimes both ends will block waiting for input
48 * from the other end, e.g. if there's an error during
51 * @todo the cookie keyring needs to be cached globally not just
52 * per-auth (which raises threadsafety issues too)
54 * @todo grep FIXME in dbus-auth.c
58 * @defgroup DBusAuthInternals Authentication implementation details
59 * @ingroup DBusInternals
60 * @brief DBusAuth implementation details
62 * Private details of authentication code.
68 * This function appends an initial client response to the given string
70 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
71 DBusString *response);
74 * This function processes a block of data received from the peer.
75 * i.e. handles a DATA command.
77 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
78 const DBusString *data);
81 * This function encodes a block of data from the peer.
83 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
84 const DBusString *data,
88 * This function decodes a block of data from the peer.
90 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
91 const DBusString *data,
95 * This function is called when the mechanism is abandoned.
97 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
100 * Virtual table representing a particular auth mechanism.
104 const char *mechanism; /**< Name of the mechanism */
105 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
106 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
107 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
108 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
109 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
110 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
111 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
112 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
113 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
114 } DBusAuthMechanismHandler;
117 * Enumeration for the known authentication commands.
120 DBUS_AUTH_COMMAND_AUTH,
121 DBUS_AUTH_COMMAND_CANCEL,
122 DBUS_AUTH_COMMAND_DATA,
123 DBUS_AUTH_COMMAND_BEGIN,
124 DBUS_AUTH_COMMAND_REJECTED,
125 DBUS_AUTH_COMMAND_OK,
126 DBUS_AUTH_COMMAND_ERROR,
127 DBUS_AUTH_COMMAND_UNKNOWN,
128 DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD,
129 DBUS_AUTH_COMMAND_AGREE_UNIX_FD
133 * Auth state function, determines the reaction to incoming events for
134 * a particular state. Returns whether we had enough memory to
135 * complete the operation.
137 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
138 DBusAuthCommand command,
139 const DBusString *args);
142 * Information about a auth state.
146 const char *name; /**< Name of the state */
147 DBusAuthStateFunction handler; /**< State function for this state */
151 * Internal members of DBusAuth.
155 int refcount; /**< reference count */
156 const char *side; /**< Client or server */
158 DBusString incoming; /**< Incoming data buffer */
159 DBusString outgoing; /**< Outgoing data buffer */
161 const DBusAuthStateData *state; /**< Current protocol state */
163 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
165 DBusString identity; /**< Current identity we're authorizing
169 DBusCredentials *credentials; /**< Credentials read from socket
172 DBusCredentials *authorized_identity; /**< Credentials that are authorized */
174 DBusCredentials *desired_identity; /**< Identity client has requested */
176 DBusString context; /**< Cookie scope */
177 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
178 int cookie_id; /**< ID of cookie to use */
179 DBusString challenge; /**< Challenge sent to client */
181 char **allowed_mechs; /**< Mechanisms we're allowed to use,
182 * or #NULL if we can use any
185 unsigned int needed_memory : 1; /**< We needed memory to continue since last
186 * successful getting something done
188 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
189 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
190 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
192 unsigned int unix_fd_possible : 1; /**< This side could do unix fd passing */
193 unsigned int unix_fd_negotiated : 1; /**< Unix fd was successfully negotiated */
197 * "Subclass" of DBusAuth for client side
201 DBusAuth base; /**< Parent class */
203 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
205 DBusString guid_from_server; /**< GUID received from server */
210 * "Subclass" of DBusAuth for server side.
214 DBusAuth base; /**< Parent class */
216 int failures; /**< Number of times client has been rejected */
217 int max_failures; /**< Number of times we reject before disconnect */
219 DBusString guid; /**< Our globally unique ID in hex encoding */
223 static void goto_state (DBusAuth *auth,
224 const DBusAuthStateData *new_state);
225 static dbus_bool_t send_auth (DBusAuth *auth,
226 const DBusAuthMechanismHandler *mech);
227 static dbus_bool_t send_data (DBusAuth *auth,
229 static dbus_bool_t send_rejected (DBusAuth *auth);
230 static dbus_bool_t send_error (DBusAuth *auth,
231 const char *message);
232 static dbus_bool_t send_ok (DBusAuth *auth);
233 static dbus_bool_t send_begin (DBusAuth *auth);
234 static dbus_bool_t send_cancel (DBusAuth *auth);
235 static dbus_bool_t send_negotiate_unix_fd (DBusAuth *auth);
236 static dbus_bool_t send_agree_unix_fd (DBusAuth *auth);
242 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
243 DBusAuthCommand command,
244 const DBusString *args);
245 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
246 DBusAuthCommand command,
247 const DBusString *args);
248 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
249 DBusAuthCommand command,
250 const DBusString *args);
252 static const DBusAuthStateData server_state_waiting_for_auth = {
253 "WaitingForAuth", handle_server_state_waiting_for_auth
255 static const DBusAuthStateData server_state_waiting_for_data = {
256 "WaitingForData", handle_server_state_waiting_for_data
258 static const DBusAuthStateData server_state_waiting_for_begin = {
259 "WaitingForBegin", handle_server_state_waiting_for_begin
266 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
267 DBusAuthCommand command,
268 const DBusString *args);
269 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
270 DBusAuthCommand command,
271 const DBusString *args);
272 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
273 DBusAuthCommand command,
274 const DBusString *args);
275 static dbus_bool_t handle_client_state_waiting_for_agree_unix_fd (DBusAuth *auth,
276 DBusAuthCommand command,
277 const DBusString *args);
279 static const DBusAuthStateData client_state_need_send_auth = {
282 static const DBusAuthStateData client_state_waiting_for_data = {
283 "WaitingForData", handle_client_state_waiting_for_data
285 static const DBusAuthStateData client_state_waiting_for_ok = {
286 "WaitingForOK", handle_client_state_waiting_for_ok
288 static const DBusAuthStateData client_state_waiting_for_reject = {
289 "WaitingForReject", handle_client_state_waiting_for_reject
291 static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
292 "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
296 * Common terminal states. Terminal states have handler == NULL.
299 static const DBusAuthStateData common_state_authenticated = {
300 "Authenticated", NULL
303 static const DBusAuthStateData common_state_need_disconnect = {
304 "NeedDisconnect", NULL
307 static const char auth_side_client[] = "client";
308 static const char auth_side_server[] = "server";
310 * @param auth the auth conversation
311 * @returns #TRUE if the conversation is the server side
313 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
315 * @param auth the auth conversation
316 * @returns #TRUE if the conversation is the client side
318 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
320 * @param auth the auth conversation
321 * @returns auth cast to DBusAuthClient
323 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
325 * @param auth the auth conversation
326 * @returns auth cast to DBusAuthServer
328 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
331 * The name of the auth ("client" or "server")
332 * @param auth the auth conversation
335 #define DBUS_AUTH_NAME(auth) ((auth)->side)
338 _dbus_auth_new (int size)
342 auth = dbus_malloc0 (size);
348 auth->keyring = NULL;
349 auth->cookie_id = -1;
351 /* note that we don't use the max string length feature,
352 * because you can't use that feature if you're going to
353 * try to recover from out-of-memory (it creates
354 * what looks like unrecoverable inability to alloc
355 * more space in the string). But we do handle
356 * overlong buffers in _dbus_auth_do_work().
359 if (!_dbus_string_init (&auth->incoming))
362 if (!_dbus_string_init (&auth->outgoing))
365 if (!_dbus_string_init (&auth->identity))
368 if (!_dbus_string_init (&auth->context))
371 if (!_dbus_string_init (&auth->challenge))
374 /* default context if none is specified */
375 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
378 auth->credentials = _dbus_credentials_new ();
379 if (auth->credentials == NULL)
382 auth->authorized_identity = _dbus_credentials_new ();
383 if (auth->authorized_identity == NULL)
386 auth->desired_identity = _dbus_credentials_new ();
387 if (auth->desired_identity == NULL)
394 _dbus_credentials_unref (auth->desired_identity);
397 _dbus_credentials_unref (auth->authorized_identity);
399 _dbus_credentials_unref (auth->credentials);
401 /* last alloc was an append to context, which is freed already below */ ;
403 _dbus_string_free (&auth->challenge);
405 _dbus_string_free (&auth->context);
407 _dbus_string_free (&auth->identity);
409 _dbus_string_free (&auth->outgoing);
411 _dbus_string_free (&auth->incoming);
418 shutdown_mech (DBusAuth *auth)
420 /* Cancel any auth */
421 auth->already_asked_for_initial_response = FALSE;
422 _dbus_string_set_length (&auth->identity, 0);
424 _dbus_credentials_clear (auth->authorized_identity);
425 _dbus_credentials_clear (auth->desired_identity);
427 if (auth->mech != NULL)
429 _dbus_verbose ("%s: Shutting down mechanism %s\n",
430 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
432 if (DBUS_AUTH_IS_CLIENT (auth))
433 (* auth->mech->client_shutdown_func) (auth);
435 (* auth->mech->server_shutdown_func) (auth);
442 * DBUS_COOKIE_SHA1 mechanism
445 /* Returns TRUE but with an empty string hash if the
446 * cookie_id isn't known. As with all this code
447 * TRUE just means we had enough memory.
450 sha1_compute_hash (DBusAuth *auth,
452 const DBusString *server_challenge,
453 const DBusString *client_challenge,
460 _dbus_assert (auth->keyring != NULL);
464 if (!_dbus_string_init (&cookie))
467 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
471 if (_dbus_string_get_length (&cookie) == 0)
477 if (!_dbus_string_init (&to_hash))
480 if (!_dbus_string_copy (server_challenge, 0,
481 &to_hash, _dbus_string_get_length (&to_hash)))
484 if (!_dbus_string_append (&to_hash, ":"))
487 if (!_dbus_string_copy (client_challenge, 0,
488 &to_hash, _dbus_string_get_length (&to_hash)))
491 if (!_dbus_string_append (&to_hash, ":"))
494 if (!_dbus_string_copy (&cookie, 0,
495 &to_hash, _dbus_string_get_length (&to_hash)))
498 if (!_dbus_sha_compute (&to_hash, hash))
504 _dbus_string_zero (&to_hash);
505 _dbus_string_free (&to_hash);
507 _dbus_string_zero (&cookie);
508 _dbus_string_free (&cookie);
512 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
513 * entropy, we use 128. This is the number of bytes in the random
516 #define N_CHALLENGE_BYTES (128/8)
519 sha1_handle_first_client_response (DBusAuth *auth,
520 const DBusString *data)
522 /* We haven't sent a challenge yet, we're expecting a desired
523 * username from the client.
532 _dbus_string_set_length (&auth->challenge, 0);
534 if (_dbus_string_get_length (data) > 0)
536 if (_dbus_string_get_length (&auth->identity) > 0)
538 /* Tried to send two auth identities, wtf */
539 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
540 DBUS_AUTH_NAME (auth));
541 return send_rejected (auth);
545 /* this is our auth identity */
546 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
551 if (!_dbus_credentials_add_from_user (auth->desired_identity, data))
553 _dbus_verbose ("%s: Did not get a valid username from client\n",
554 DBUS_AUTH_NAME (auth));
555 return send_rejected (auth);
558 if (!_dbus_string_init (&tmp))
561 if (!_dbus_string_init (&tmp2))
563 _dbus_string_free (&tmp);
567 /* we cache the keyring for speed, so here we drop it if it's the
568 * wrong one. FIXME caching the keyring here is useless since we use
569 * a different DBusAuth for every connection.
572 !_dbus_keyring_is_for_credentials (auth->keyring,
573 auth->desired_identity))
575 _dbus_keyring_unref (auth->keyring);
576 auth->keyring = NULL;
579 if (auth->keyring == NULL)
581 dbus_error_init (&error);
582 auth->keyring = _dbus_keyring_new_for_credentials (auth->desired_identity,
586 if (auth->keyring == NULL)
588 if (dbus_error_has_name (&error,
589 DBUS_ERROR_NO_MEMORY))
591 dbus_error_free (&error);
596 _DBUS_ASSERT_ERROR_IS_SET (&error);
597 _dbus_verbose ("%s: Error loading keyring: %s\n",
598 DBUS_AUTH_NAME (auth), error.message);
599 if (send_rejected (auth))
600 retval = TRUE; /* retval is only about mem */
601 dbus_error_free (&error);
607 _dbus_assert (!dbus_error_is_set (&error));
611 _dbus_assert (auth->keyring != NULL);
613 dbus_error_init (&error);
614 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
615 if (auth->cookie_id < 0)
617 _DBUS_ASSERT_ERROR_IS_SET (&error);
618 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
619 DBUS_AUTH_NAME (auth), error.message);
620 if (send_rejected (auth))
622 dbus_error_free (&error);
627 _dbus_assert (!dbus_error_is_set (&error));
630 if (!_dbus_string_copy (&auth->context, 0,
631 &tmp2, _dbus_string_get_length (&tmp2)))
634 if (!_dbus_string_append (&tmp2, " "))
637 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
640 if (!_dbus_string_append (&tmp2, " "))
643 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
646 _dbus_string_set_length (&auth->challenge, 0);
647 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
650 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
651 _dbus_string_get_length (&tmp2)))
654 if (!send_data (auth, &tmp2))
657 goto_state (auth, &server_state_waiting_for_data);
661 _dbus_string_zero (&tmp);
662 _dbus_string_free (&tmp);
663 _dbus_string_zero (&tmp2);
664 _dbus_string_free (&tmp2);
670 sha1_handle_second_client_response (DBusAuth *auth,
671 const DBusString *data)
673 /* We are expecting a response which is the hex-encoded client
674 * challenge, space, then SHA-1 hash of the concatenation of our
675 * challenge, ":", client challenge, ":", secret key, all
679 DBusString client_challenge;
680 DBusString client_hash;
682 DBusString correct_hash;
686 if (!_dbus_string_find_blank (data, 0, &i))
688 _dbus_verbose ("%s: no space separator in client response\n",
689 DBUS_AUTH_NAME (auth));
690 return send_rejected (auth);
693 if (!_dbus_string_init (&client_challenge))
696 if (!_dbus_string_init (&client_hash))
699 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
703 _dbus_string_skip_blank (data, i, &i);
705 if (!_dbus_string_copy_len (data, i,
706 _dbus_string_get_length (data) - i,
711 if (_dbus_string_get_length (&client_challenge) == 0 ||
712 _dbus_string_get_length (&client_hash) == 0)
714 _dbus_verbose ("%s: zero-length client challenge or hash\n",
715 DBUS_AUTH_NAME (auth));
716 if (send_rejected (auth))
721 if (!_dbus_string_init (&correct_hash))
724 if (!sha1_compute_hash (auth, auth->cookie_id,
730 /* if cookie_id was invalid, then we get an empty hash */
731 if (_dbus_string_get_length (&correct_hash) == 0)
733 if (send_rejected (auth))
738 if (!_dbus_string_equal (&client_hash, &correct_hash))
740 if (send_rejected (auth))
745 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
746 auth->desired_identity))
749 /* Copy process ID from the socket credentials if it's there
751 if (!_dbus_credentials_add_credential (auth->authorized_identity,
752 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
759 _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
760 DBUS_AUTH_NAME (auth));
765 _dbus_string_zero (&correct_hash);
766 _dbus_string_free (&correct_hash);
768 _dbus_string_zero (&client_hash);
769 _dbus_string_free (&client_hash);
771 _dbus_string_free (&client_challenge);
777 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
778 const DBusString *data)
780 if (auth->cookie_id < 0)
781 return sha1_handle_first_client_response (auth, data);
783 return sha1_handle_second_client_response (auth, data);
787 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
789 auth->cookie_id = -1;
790 _dbus_string_set_length (&auth->challenge, 0);
794 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
795 DBusString *response)
802 if (!_dbus_string_init (&username))
805 if (!_dbus_append_user_from_current_process (&username))
808 if (!_dbus_string_hex_encode (&username, 0,
810 _dbus_string_get_length (response)))
816 _dbus_string_free (&username);
822 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
823 const DBusString *data)
825 /* The data we get from the server should be the cookie context
826 * name, the cookie ID, and the server challenge, separated by
827 * spaces. We send back our challenge string and the correct hash.
831 DBusString cookie_id_str;
832 DBusString server_challenge;
833 DBusString client_challenge;
834 DBusString correct_hash;
841 if (!_dbus_string_find_blank (data, 0, &i))
843 if (send_error (auth,
844 "Server did not send context/ID/challenge properly"))
849 if (!_dbus_string_init (&context))
852 if (!_dbus_string_copy_len (data, 0, i,
856 _dbus_string_skip_blank (data, i, &i);
857 if (!_dbus_string_find_blank (data, i, &j))
859 if (send_error (auth,
860 "Server did not send context/ID/challenge properly"))
865 if (!_dbus_string_init (&cookie_id_str))
868 if (!_dbus_string_copy_len (data, i, j - i,
872 if (!_dbus_string_init (&server_challenge))
876 _dbus_string_skip_blank (data, i, &i);
877 j = _dbus_string_get_length (data);
879 if (!_dbus_string_copy_len (data, i, j - i,
880 &server_challenge, 0))
883 if (!_dbus_keyring_validate_context (&context))
885 if (send_error (auth, "Server sent invalid cookie context"))
890 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
892 if (send_error (auth, "Could not parse cookie ID as an integer"))
897 if (_dbus_string_get_length (&server_challenge) == 0)
899 if (send_error (auth, "Empty server challenge string"))
904 if (auth->keyring == NULL)
908 dbus_error_init (&error);
909 auth->keyring = _dbus_keyring_new_for_credentials (NULL,
913 if (auth->keyring == NULL)
915 if (dbus_error_has_name (&error,
916 DBUS_ERROR_NO_MEMORY))
918 dbus_error_free (&error);
923 _DBUS_ASSERT_ERROR_IS_SET (&error);
925 _dbus_verbose ("%s: Error loading keyring: %s\n",
926 DBUS_AUTH_NAME (auth), error.message);
928 if (send_error (auth, "Could not load cookie file"))
929 retval = TRUE; /* retval is only about mem */
931 dbus_error_free (&error);
937 _dbus_assert (!dbus_error_is_set (&error));
941 _dbus_assert (auth->keyring != NULL);
943 if (!_dbus_string_init (&tmp))
946 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
949 if (!_dbus_string_init (&client_challenge))
952 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
955 if (!_dbus_string_init (&correct_hash))
958 if (!sha1_compute_hash (auth, val,
964 if (_dbus_string_get_length (&correct_hash) == 0)
966 /* couldn't find the cookie ID or something */
967 if (send_error (auth, "Don't have the requested cookie ID"))
972 _dbus_string_set_length (&tmp, 0);
974 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
975 _dbus_string_get_length (&tmp)))
978 if (!_dbus_string_append (&tmp, " "))
981 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
982 _dbus_string_get_length (&tmp)))
985 if (!send_data (auth, &tmp))
991 _dbus_string_zero (&correct_hash);
992 _dbus_string_free (&correct_hash);
994 _dbus_string_free (&client_challenge);
996 _dbus_string_zero (&tmp);
997 _dbus_string_free (&tmp);
999 _dbus_string_free (&server_challenge);
1001 _dbus_string_free (&cookie_id_str);
1003 _dbus_string_free (&context);
1009 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1011 auth->cookie_id = -1;
1012 _dbus_string_set_length (&auth->challenge, 0);
1016 * EXTERNAL mechanism
1020 handle_server_data_external_mech (DBusAuth *auth,
1021 const DBusString *data)
1023 if (_dbus_credentials_are_anonymous (auth->credentials))
1025 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1026 DBUS_AUTH_NAME (auth));
1027 return send_rejected (auth);
1030 if (_dbus_string_get_length (data) > 0)
1032 if (_dbus_string_get_length (&auth->identity) > 0)
1034 /* Tried to send two auth identities, wtf */
1035 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1036 DBUS_AUTH_NAME (auth));
1037 return send_rejected (auth);
1041 /* this is our auth identity */
1042 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1047 /* Poke client for an auth identity, if none given */
1048 if (_dbus_string_get_length (&auth->identity) == 0 &&
1049 !auth->already_asked_for_initial_response)
1051 if (send_data (auth, NULL))
1053 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1054 DBUS_AUTH_NAME (auth));
1055 auth->already_asked_for_initial_response = TRUE;
1056 goto_state (auth, &server_state_waiting_for_data);
1063 _dbus_credentials_clear (auth->desired_identity);
1065 /* If auth->identity is still empty here, then client
1066 * responded with an empty string after we poked it for
1067 * an initial response. This means to try to auth the
1068 * identity provided in the credentials.
1070 if (_dbus_string_get_length (&auth->identity) == 0)
1072 if (!_dbus_credentials_add_credentials (auth->desired_identity,
1075 return FALSE; /* OOM */
1080 if (!_dbus_credentials_add_from_user (auth->desired_identity,
1083 _dbus_verbose ("%s: could not get credentials from uid string\n",
1084 DBUS_AUTH_NAME (auth));
1085 return send_rejected (auth);
1089 if (_dbus_credentials_are_anonymous (auth->desired_identity))
1091 _dbus_verbose ("%s: desired user %s is no good\n",
1092 DBUS_AUTH_NAME (auth),
1093 _dbus_string_get_const_data (&auth->identity));
1094 return send_rejected (auth);
1097 if (_dbus_credentials_are_superset (auth->credentials,
1098 auth->desired_identity))
1100 /* client has authenticated */
1101 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
1102 auth->desired_identity))
1105 /* also copy process ID from the socket credentials
1107 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1108 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1112 /* also copy audit data from the socket credentials
1114 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1115 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1119 if (!send_ok (auth))
1122 _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1123 DBUS_AUTH_NAME (auth));
1129 _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1130 DBUS_AUTH_NAME (auth));
1131 return send_rejected (auth);
1136 handle_server_shutdown_external_mech (DBusAuth *auth)
1142 handle_client_initial_response_external_mech (DBusAuth *auth,
1143 DBusString *response)
1145 /* We always append our UID as an initial response, so the server
1146 * doesn't have to send back an empty challenge to check whether we
1147 * want to specify an identity. i.e. this avoids a round trip that
1148 * the spec for the EXTERNAL mechanism otherwise requires.
1150 DBusString plaintext;
1152 if (!_dbus_string_init (&plaintext))
1155 if (!_dbus_append_user_from_current_process (&plaintext))
1158 if (!_dbus_string_hex_encode (&plaintext, 0,
1160 _dbus_string_get_length (response)))
1163 _dbus_string_free (&plaintext);
1168 _dbus_string_free (&plaintext);
1173 handle_client_data_external_mech (DBusAuth *auth,
1174 const DBusString *data)
1181 handle_client_shutdown_external_mech (DBusAuth *auth)
1187 * ANONYMOUS mechanism
1191 handle_server_data_anonymous_mech (DBusAuth *auth,
1192 const DBusString *data)
1194 if (_dbus_string_get_length (data) > 0)
1196 /* Client is allowed to send "trace" data, the only defined
1197 * meaning is that if it contains '@' it is an email address,
1198 * and otherwise it is anything else, and it's supposed to be
1201 if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1203 _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1204 DBUS_AUTH_NAME (auth));
1205 return send_rejected (auth);
1208 _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1209 DBUS_AUTH_NAME (auth),
1210 _dbus_string_get_const_data (data));
1213 /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1214 _dbus_credentials_clear (auth->desired_identity);
1216 /* Copy process ID from the socket credentials
1218 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1219 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1223 /* Anonymous is always allowed */
1224 if (!send_ok (auth))
1227 _dbus_verbose ("%s: authenticated client as anonymous\n",
1228 DBUS_AUTH_NAME (auth));
1234 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1240 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1241 DBusString *response)
1243 /* Our initial response is a "trace" string which must be valid UTF-8
1244 * and must be an email address if it contains '@'.
1245 * We just send the dbus implementation info, like a user-agent or
1246 * something, because... why not. There's nothing guaranteed here
1247 * though, we could change it later.
1249 DBusString plaintext;
1251 if (!_dbus_string_init (&plaintext))
1254 if (!_dbus_string_append (&plaintext,
1255 "libdbus " DBUS_VERSION_STRING))
1258 if (!_dbus_string_hex_encode (&plaintext, 0,
1260 _dbus_string_get_length (response)))
1263 _dbus_string_free (&plaintext);
1268 _dbus_string_free (&plaintext);
1273 handle_client_data_anonymous_mech (DBusAuth *auth,
1274 const DBusString *data)
1281 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1286 /* Put mechanisms here in order of preference.
1287 * Right now we have:
1289 * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1290 * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1291 * - ANONYMOUS checks nothing but doesn't auth the person as a user
1293 * We might ideally add a mechanism to chain to Cyrus SASL so we can
1294 * use its mechanisms as well.
1297 static const DBusAuthMechanismHandler
1298 all_mechanisms[] = {
1300 handle_server_data_external_mech,
1302 handle_server_shutdown_external_mech,
1303 handle_client_initial_response_external_mech,
1304 handle_client_data_external_mech,
1306 handle_client_shutdown_external_mech },
1307 { "DBUS_COOKIE_SHA1",
1308 handle_server_data_cookie_sha1_mech,
1310 handle_server_shutdown_cookie_sha1_mech,
1311 handle_client_initial_response_cookie_sha1_mech,
1312 handle_client_data_cookie_sha1_mech,
1314 handle_client_shutdown_cookie_sha1_mech },
1316 handle_server_data_anonymous_mech,
1318 handle_server_shutdown_anonymous_mech,
1319 handle_client_initial_response_anonymous_mech,
1320 handle_client_data_anonymous_mech,
1322 handle_client_shutdown_anonymous_mech },
1326 static const DBusAuthMechanismHandler*
1327 find_mech (const DBusString *name,
1328 char **allowed_mechs)
1332 if (allowed_mechs != NULL &&
1333 !_dbus_string_array_contains ((const char**) allowed_mechs,
1334 _dbus_string_get_const_data (name)))
1338 while (all_mechanisms[i].mechanism != NULL)
1340 if (_dbus_string_equal_c_str (name,
1341 all_mechanisms[i].mechanism))
1343 return &all_mechanisms[i];
1352 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1354 DBusString auth_command;
1356 if (!_dbus_string_init (&auth_command))
1359 if (!_dbus_string_append (&auth_command,
1362 _dbus_string_free (&auth_command);
1366 if (!_dbus_string_append (&auth_command,
1369 _dbus_string_free (&auth_command);
1373 if (mech->client_initial_response_func != NULL)
1375 if (!_dbus_string_append (&auth_command, " "))
1377 _dbus_string_free (&auth_command);
1381 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1383 _dbus_string_free (&auth_command);
1388 if (!_dbus_string_append (&auth_command,
1391 _dbus_string_free (&auth_command);
1395 if (!_dbus_string_copy (&auth_command, 0,
1397 _dbus_string_get_length (&auth->outgoing)))
1399 _dbus_string_free (&auth_command);
1403 _dbus_string_free (&auth_command);
1404 shutdown_mech (auth);
1406 goto_state (auth, &client_state_waiting_for_data);
1412 send_data (DBusAuth *auth, DBusString *data)
1416 if (data == NULL || _dbus_string_get_length (data) == 0)
1417 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1420 old_len = _dbus_string_get_length (&auth->outgoing);
1421 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1424 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1425 _dbus_string_get_length (&auth->outgoing)))
1428 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1434 _dbus_string_set_length (&auth->outgoing, old_len);
1441 send_rejected (DBusAuth *auth)
1444 DBusAuthServer *server_auth;
1447 if (!_dbus_string_init (&command))
1450 if (!_dbus_string_append (&command,
1455 while (all_mechanisms[i].mechanism != NULL)
1457 if (!_dbus_string_append (&command,
1461 if (!_dbus_string_append (&command,
1462 all_mechanisms[i].mechanism))
1468 if (!_dbus_string_append (&command, "\r\n"))
1471 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1472 _dbus_string_get_length (&auth->outgoing)))
1475 shutdown_mech (auth);
1477 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1478 server_auth = DBUS_AUTH_SERVER (auth);
1479 server_auth->failures += 1;
1481 if (server_auth->failures >= server_auth->max_failures)
1482 goto_state (auth, &common_state_need_disconnect);
1484 goto_state (auth, &server_state_waiting_for_auth);
1486 _dbus_string_free (&command);
1491 _dbus_string_free (&command);
1496 send_error (DBusAuth *auth, const char *message)
1498 return _dbus_string_append_printf (&auth->outgoing,
1499 "ERROR \"%s\"\r\n", message);
1503 send_ok (DBusAuth *auth)
1507 orig_len = _dbus_string_get_length (&auth->outgoing);
1509 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1510 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1513 _dbus_string_get_length (&auth->outgoing)) &&
1514 _dbus_string_append (&auth->outgoing, "\r\n"))
1516 goto_state (auth, &server_state_waiting_for_begin);
1521 _dbus_string_set_length (&auth->outgoing, orig_len);
1527 send_begin (DBusAuth *auth)
1530 if (!_dbus_string_append (&auth->outgoing,
1534 goto_state (auth, &common_state_authenticated);
1539 process_ok(DBusAuth *auth,
1540 const DBusString *args_from_ok) {
1544 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1545 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1547 /* We decode the hex string to binary, using guid_from_server as scratch... */
1550 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1551 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1554 /* now clear out the scratch */
1555 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1557 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1560 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1561 end_of_hex, _dbus_string_get_length (args_from_ok));
1562 goto_state (auth, &common_state_need_disconnect);
1566 if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1567 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1571 _dbus_verbose ("Got GUID '%s' from the server\n",
1572 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1574 if (auth->unix_fd_possible)
1575 return send_negotiate_unix_fd(auth);
1577 _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1578 return send_begin (auth);
1582 send_cancel (DBusAuth *auth)
1584 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1586 goto_state (auth, &client_state_waiting_for_reject);
1594 process_data (DBusAuth *auth,
1595 const DBusString *args,
1596 DBusAuthDataFunction data_func)
1601 if (!_dbus_string_init (&decoded))
1604 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1606 _dbus_string_free (&decoded);
1610 if (_dbus_string_get_length (args) != end)
1612 _dbus_string_free (&decoded);
1613 if (!send_error (auth, "Invalid hex encoding"))
1619 #ifdef DBUS_ENABLE_VERBOSE_MODE
1620 if (_dbus_string_validate_ascii (&decoded, 0,
1621 _dbus_string_get_length (&decoded)))
1622 _dbus_verbose ("%s: data: '%s'\n",
1623 DBUS_AUTH_NAME (auth),
1624 _dbus_string_get_const_data (&decoded));
1627 if (!(* data_func) (auth, &decoded))
1629 _dbus_string_free (&decoded);
1633 _dbus_string_free (&decoded);
1638 send_negotiate_unix_fd (DBusAuth *auth)
1640 if (!_dbus_string_append (&auth->outgoing,
1641 "NEGOTIATE_UNIX_FD\r\n"))
1644 goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1649 send_agree_unix_fd (DBusAuth *auth)
1651 _dbus_assert(auth->unix_fd_possible);
1653 auth->unix_fd_negotiated = TRUE;
1654 _dbus_verbose("Agreed to UNIX FD passing\n");
1656 if (!_dbus_string_append (&auth->outgoing,
1657 "AGREE_UNIX_FD\r\n"))
1660 goto_state (auth, &server_state_waiting_for_begin);
1665 handle_auth (DBusAuth *auth, const DBusString *args)
1667 if (_dbus_string_get_length (args) == 0)
1669 /* No args to the auth, send mechanisms */
1670 if (!send_rejected (auth))
1679 DBusString hex_response;
1681 _dbus_string_find_blank (args, 0, &i);
1683 if (!_dbus_string_init (&mech))
1686 if (!_dbus_string_init (&hex_response))
1688 _dbus_string_free (&mech);
1692 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1695 _dbus_string_skip_blank (args, i, &i);
1696 if (!_dbus_string_copy (args, i, &hex_response, 0))
1699 auth->mech = find_mech (&mech, auth->allowed_mechs);
1700 if (auth->mech != NULL)
1702 _dbus_verbose ("%s: Trying mechanism %s\n",
1703 DBUS_AUTH_NAME (auth),
1704 auth->mech->mechanism);
1706 if (!process_data (auth, &hex_response,
1707 auth->mech->server_data_func))
1712 /* Unsupported mechanism */
1713 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1714 DBUS_AUTH_NAME (auth),
1715 _dbus_string_get_const_data (&mech));
1717 if (!send_rejected (auth))
1721 _dbus_string_free (&mech);
1722 _dbus_string_free (&hex_response);
1728 _dbus_string_free (&mech);
1729 _dbus_string_free (&hex_response);
1735 handle_server_state_waiting_for_auth (DBusAuth *auth,
1736 DBusAuthCommand command,
1737 const DBusString *args)
1741 case DBUS_AUTH_COMMAND_AUTH:
1742 return handle_auth (auth, args);
1744 case DBUS_AUTH_COMMAND_CANCEL:
1745 case DBUS_AUTH_COMMAND_DATA:
1746 return send_error (auth, "Not currently in an auth conversation");
1748 case DBUS_AUTH_COMMAND_BEGIN:
1749 goto_state (auth, &common_state_need_disconnect);
1752 case DBUS_AUTH_COMMAND_ERROR:
1753 return send_rejected (auth);
1755 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1756 return send_error (auth, "Need to authenticate first");
1758 case DBUS_AUTH_COMMAND_REJECTED:
1759 case DBUS_AUTH_COMMAND_OK:
1760 case DBUS_AUTH_COMMAND_UNKNOWN:
1761 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1763 return send_error (auth, "Unknown command");
1768 handle_server_state_waiting_for_data (DBusAuth *auth,
1769 DBusAuthCommand command,
1770 const DBusString *args)
1774 case DBUS_AUTH_COMMAND_AUTH:
1775 return send_error (auth, "Sent AUTH while another AUTH in progress");
1777 case DBUS_AUTH_COMMAND_CANCEL:
1778 case DBUS_AUTH_COMMAND_ERROR:
1779 return send_rejected (auth);
1781 case DBUS_AUTH_COMMAND_DATA:
1782 return process_data (auth, args, auth->mech->server_data_func);
1784 case DBUS_AUTH_COMMAND_BEGIN:
1785 goto_state (auth, &common_state_need_disconnect);
1788 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1789 return send_error (auth, "Need to authenticate first");
1791 case DBUS_AUTH_COMMAND_REJECTED:
1792 case DBUS_AUTH_COMMAND_OK:
1793 case DBUS_AUTH_COMMAND_UNKNOWN:
1794 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1796 return send_error (auth, "Unknown command");
1801 handle_server_state_waiting_for_begin (DBusAuth *auth,
1802 DBusAuthCommand command,
1803 const DBusString *args)
1807 case DBUS_AUTH_COMMAND_AUTH:
1808 return send_error (auth, "Sent AUTH while expecting BEGIN");
1810 case DBUS_AUTH_COMMAND_DATA:
1811 return send_error (auth, "Sent DATA while expecting BEGIN");
1813 case DBUS_AUTH_COMMAND_BEGIN:
1814 goto_state (auth, &common_state_authenticated);
1817 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1818 if (auth->unix_fd_possible)
1819 return send_agree_unix_fd(auth);
1821 return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1823 case DBUS_AUTH_COMMAND_REJECTED:
1824 case DBUS_AUTH_COMMAND_OK:
1825 case DBUS_AUTH_COMMAND_UNKNOWN:
1826 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1828 return send_error (auth, "Unknown command");
1830 case DBUS_AUTH_COMMAND_CANCEL:
1831 case DBUS_AUTH_COMMAND_ERROR:
1832 return send_rejected (auth);
1836 /* return FALSE if no memory, TRUE if all OK */
1838 get_word (const DBusString *str,
1844 _dbus_string_skip_blank (str, *start, start);
1845 _dbus_string_find_blank (str, *start, &i);
1849 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1859 record_mechanisms (DBusAuth *auth,
1860 const DBusString *args)
1865 if (auth->already_got_mechanisms)
1868 len = _dbus_string_get_length (args);
1874 const DBusAuthMechanismHandler *mech;
1876 if (!_dbus_string_init (&m))
1879 if (!get_word (args, &next, &m))
1881 _dbus_string_free (&m);
1885 mech = find_mech (&m, auth->allowed_mechs);
1889 /* FIXME right now we try mechanisms in the order
1890 * the server lists them; should we do them in
1891 * some more deterministic order?
1893 * Probably in all_mechanisms order, our order of
1894 * preference. Of course when the server is us,
1895 * it lists things in that order anyhow.
1898 if (mech != &all_mechanisms[0])
1900 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1901 DBUS_AUTH_NAME (auth), mech->mechanism);
1903 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1906 _dbus_string_free (&m);
1912 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1913 DBUS_AUTH_NAME (auth), mech->mechanism);
1918 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1919 DBUS_AUTH_NAME (auth),
1920 _dbus_string_get_const_data (&m));
1923 _dbus_string_free (&m);
1926 auth->already_got_mechanisms = TRUE;
1931 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1937 process_rejected (DBusAuth *auth, const DBusString *args)
1939 const DBusAuthMechanismHandler *mech;
1940 DBusAuthClient *client;
1942 client = DBUS_AUTH_CLIENT (auth);
1944 if (!auth->already_got_mechanisms)
1946 if (!record_mechanisms (auth, args))
1950 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1952 mech = client->mechs_to_try->data;
1954 if (!send_auth (auth, mech))
1957 _dbus_list_pop_first (&client->mechs_to_try);
1959 _dbus_verbose ("%s: Trying mechanism %s\n",
1960 DBUS_AUTH_NAME (auth),
1966 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1967 DBUS_AUTH_NAME (auth));
1968 goto_state (auth, &common_state_need_disconnect);
1976 handle_client_state_waiting_for_data (DBusAuth *auth,
1977 DBusAuthCommand command,
1978 const DBusString *args)
1980 _dbus_assert (auth->mech != NULL);
1984 case DBUS_AUTH_COMMAND_DATA:
1985 return process_data (auth, args, auth->mech->client_data_func);
1987 case DBUS_AUTH_COMMAND_REJECTED:
1988 return process_rejected (auth, args);
1990 case DBUS_AUTH_COMMAND_OK:
1991 return process_ok(auth, args);
1993 case DBUS_AUTH_COMMAND_ERROR:
1994 return send_cancel (auth);
1996 case DBUS_AUTH_COMMAND_AUTH:
1997 case DBUS_AUTH_COMMAND_CANCEL:
1998 case DBUS_AUTH_COMMAND_BEGIN:
1999 case DBUS_AUTH_COMMAND_UNKNOWN:
2000 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2001 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2003 return send_error (auth, "Unknown command");
2008 handle_client_state_waiting_for_ok (DBusAuth *auth,
2009 DBusAuthCommand command,
2010 const DBusString *args)
2014 case DBUS_AUTH_COMMAND_REJECTED:
2015 return process_rejected (auth, args);
2017 case DBUS_AUTH_COMMAND_OK:
2018 return process_ok(auth, args);
2020 case DBUS_AUTH_COMMAND_DATA:
2021 case DBUS_AUTH_COMMAND_ERROR:
2022 return send_cancel (auth);
2024 case DBUS_AUTH_COMMAND_AUTH:
2025 case DBUS_AUTH_COMMAND_CANCEL:
2026 case DBUS_AUTH_COMMAND_BEGIN:
2027 case DBUS_AUTH_COMMAND_UNKNOWN:
2028 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2029 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2031 return send_error (auth, "Unknown command");
2036 handle_client_state_waiting_for_reject (DBusAuth *auth,
2037 DBusAuthCommand command,
2038 const DBusString *args)
2042 case DBUS_AUTH_COMMAND_REJECTED:
2043 return process_rejected (auth, args);
2045 case DBUS_AUTH_COMMAND_AUTH:
2046 case DBUS_AUTH_COMMAND_CANCEL:
2047 case DBUS_AUTH_COMMAND_DATA:
2048 case DBUS_AUTH_COMMAND_BEGIN:
2049 case DBUS_AUTH_COMMAND_OK:
2050 case DBUS_AUTH_COMMAND_ERROR:
2051 case DBUS_AUTH_COMMAND_UNKNOWN:
2052 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2053 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2055 goto_state (auth, &common_state_need_disconnect);
2061 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2062 DBusAuthCommand command,
2063 const DBusString *args)
2067 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2068 _dbus_assert(auth->unix_fd_possible);
2069 auth->unix_fd_negotiated = TRUE;
2070 _dbus_verbose("Successfully negotiated UNIX FD passing\n");
2071 return send_begin (auth);
2073 case DBUS_AUTH_COMMAND_ERROR:
2074 _dbus_assert(auth->unix_fd_possible);
2075 auth->unix_fd_negotiated = FALSE;
2076 _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2077 return send_begin (auth);
2079 case DBUS_AUTH_COMMAND_OK:
2080 case DBUS_AUTH_COMMAND_DATA:
2081 case DBUS_AUTH_COMMAND_REJECTED:
2082 case DBUS_AUTH_COMMAND_AUTH:
2083 case DBUS_AUTH_COMMAND_CANCEL:
2084 case DBUS_AUTH_COMMAND_BEGIN:
2085 case DBUS_AUTH_COMMAND_UNKNOWN:
2086 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2088 return send_error (auth, "Unknown command");
2093 * Mapping from command name to enum
2096 const char *name; /**< Name of the command */
2097 DBusAuthCommand command; /**< Corresponding enum */
2098 } DBusAuthCommandName;
2100 static const DBusAuthCommandName auth_command_names[] = {
2101 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2102 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2103 { "DATA", DBUS_AUTH_COMMAND_DATA },
2104 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2105 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2106 { "OK", DBUS_AUTH_COMMAND_OK },
2107 { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2108 { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2109 { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2112 static DBusAuthCommand
2113 lookup_command_from_name (DBusString *command)
2117 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2119 if (_dbus_string_equal_c_str (command,
2120 auth_command_names[i].name))
2121 return auth_command_names[i].command;
2124 return DBUS_AUTH_COMMAND_UNKNOWN;
2128 goto_state (DBusAuth *auth,
2129 const DBusAuthStateData *state)
2131 _dbus_verbose ("%s: going from state %s to state %s\n",
2132 DBUS_AUTH_NAME (auth),
2136 auth->state = state;
2139 /* returns whether to call it again right away */
2141 process_command (DBusAuth *auth)
2143 DBusAuthCommand command;
2150 /* _dbus_verbose ("%s: trying process_command()\n"); */
2155 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2158 if (!_dbus_string_init (&line))
2160 auth->needed_memory = TRUE;
2164 if (!_dbus_string_init (&args))
2166 _dbus_string_free (&line);
2167 auth->needed_memory = TRUE;
2171 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2174 if (!_dbus_string_validate_ascii (&line, 0,
2175 _dbus_string_get_length (&line)))
2177 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2178 DBUS_AUTH_NAME (auth));
2179 if (!send_error (auth, "Command contained non-ASCII"))
2185 _dbus_verbose ("%s: got command \"%s\"\n",
2186 DBUS_AUTH_NAME (auth),
2187 _dbus_string_get_const_data (&line));
2189 _dbus_string_find_blank (&line, 0, &i);
2190 _dbus_string_skip_blank (&line, i, &j);
2193 _dbus_string_delete (&line, i, j - i);
2195 if (!_dbus_string_move (&line, i, &args, 0))
2198 /* FIXME 1.0 we should probably validate that only the allowed
2199 * chars are in the command name
2202 command = lookup_command_from_name (&line);
2203 if (!(* auth->state->handler) (auth, command, &args))
2208 /* We've succeeded in processing the whole command so drop it out
2209 * of the incoming buffer and return TRUE to try another command.
2212 _dbus_string_delete (&auth->incoming, 0, eol);
2215 _dbus_string_delete (&auth->incoming, 0, 2);
2220 _dbus_string_free (&args);
2221 _dbus_string_free (&line);
2224 auth->needed_memory = TRUE;
2226 auth->needed_memory = FALSE;
2235 * @addtogroup DBusAuth
2240 * Creates a new auth conversation object for the server side.
2241 * See doc/dbus-sasl-profile.txt for full details on what
2244 * @returns the new object or #NULL if no memory
2247 _dbus_auth_server_new (const DBusString *guid)
2250 DBusAuthServer *server_auth;
2251 DBusString guid_copy;
2253 if (!_dbus_string_init (&guid_copy))
2256 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2258 _dbus_string_free (&guid_copy);
2262 auth = _dbus_auth_new (sizeof (DBusAuthServer));
2265 _dbus_string_free (&guid_copy);
2269 auth->side = auth_side_server;
2270 auth->state = &server_state_waiting_for_auth;
2272 server_auth = DBUS_AUTH_SERVER (auth);
2274 server_auth->guid = guid_copy;
2276 /* perhaps this should be per-mechanism with a lower
2279 server_auth->failures = 0;
2280 server_auth->max_failures = 6;
2286 * Creates a new auth conversation object for the client side.
2287 * See doc/dbus-sasl-profile.txt for full details on what
2290 * @returns the new object or #NULL if no memory
2293 _dbus_auth_client_new (void)
2296 DBusString guid_str;
2298 if (!_dbus_string_init (&guid_str))
2301 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2304 _dbus_string_free (&guid_str);
2308 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2310 auth->side = auth_side_client;
2311 auth->state = &client_state_need_send_auth;
2313 /* Start the auth conversation by sending AUTH for our default
2315 if (!send_auth (auth, &all_mechanisms[0]))
2317 _dbus_auth_unref (auth);
2325 * Creates a new auth conversation object for the client side of kdbus.
2326 * In fact it only initialize structures and sets authenticated state
2327 * because of different authentication-like mechanism in kdbus - policies
2328 * TODO Probably to be checked and modified when kdbus will be documented
2330 * @returns the new object or #NULL if no memory
2333 _dbus_auth_client_new_kdbus (void)
2336 DBusString guid_str;
2338 if (!_dbus_string_init (&guid_str))
2341 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2344 _dbus_string_free (&guid_str);
2348 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2350 auth->side = auth_side_client;
2351 auth->state = &common_state_authenticated;
2352 auth->unix_fd_negotiated = FALSE;
2354 /* Start the auth conversation by sending AUTH for our default
2356 /* if (!send_auth (auth, &all_mechanisms[0]))
2358 _dbus_auth_unref (auth);
2366 * Increments the refcount of an auth object.
2368 * @param auth the auth conversation
2369 * @returns the auth conversation
2372 _dbus_auth_ref (DBusAuth *auth)
2374 _dbus_assert (auth != NULL);
2376 auth->refcount += 1;
2382 * Decrements the refcount of an auth object.
2384 * @param auth the auth conversation
2387 _dbus_auth_unref (DBusAuth *auth)
2389 _dbus_assert (auth != NULL);
2390 _dbus_assert (auth->refcount > 0);
2392 auth->refcount -= 1;
2393 if (auth->refcount == 0)
2395 shutdown_mech (auth);
2397 if (DBUS_AUTH_IS_CLIENT (auth))
2399 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2400 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2404 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2406 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2410 _dbus_keyring_unref (auth->keyring);
2412 _dbus_string_free (&auth->context);
2413 _dbus_string_free (&auth->challenge);
2414 _dbus_string_free (&auth->identity);
2415 _dbus_string_free (&auth->incoming);
2416 _dbus_string_free (&auth->outgoing);
2418 dbus_free_string_array (auth->allowed_mechs);
2420 _dbus_credentials_unref (auth->credentials);
2421 _dbus_credentials_unref (auth->authorized_identity);
2422 _dbus_credentials_unref (auth->desired_identity);
2429 * Sets an array of authentication mechanism names
2430 * that we are willing to use.
2432 * @param auth the auth conversation
2433 * @param mechanisms #NULL-terminated array of mechanism names
2434 * @returns #FALSE if no memory
2437 _dbus_auth_set_mechanisms (DBusAuth *auth,
2438 const char **mechanisms)
2442 if (mechanisms != NULL)
2444 copy = _dbus_dup_string_array (mechanisms);
2451 dbus_free_string_array (auth->allowed_mechs);
2453 auth->allowed_mechs = copy;
2459 * @param auth the auth conversation object
2460 * @returns #TRUE if we're in a final state
2462 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2465 * Analyzes buffered input and moves the auth conversation forward,
2466 * returning the new state of the auth conversation.
2468 * @param auth the auth conversation
2469 * @returns the new state
2472 _dbus_auth_do_work (DBusAuth *auth)
2474 auth->needed_memory = FALSE;
2476 /* Max amount we'll buffer up before deciding someone's on crack */
2477 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2481 if (DBUS_AUTH_IN_END_STATE (auth))
2484 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2485 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2487 goto_state (auth, &common_state_need_disconnect);
2488 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2489 DBUS_AUTH_NAME (auth));
2493 while (process_command (auth));
2495 if (auth->needed_memory)
2496 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2497 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2498 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2499 else if (auth->state == &common_state_need_disconnect)
2500 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2501 else if (auth->state == &common_state_authenticated)
2502 return DBUS_AUTH_STATE_AUTHENTICATED;
2503 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2507 * Gets bytes that need to be sent to the peer we're conversing with.
2508 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2509 * to notify the auth object that they were written.
2511 * @param auth the auth conversation
2512 * @param str return location for a ref to the buffer to send
2513 * @returns #FALSE if nothing to send
2516 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2517 const DBusString **str)
2519 _dbus_assert (auth != NULL);
2520 _dbus_assert (str != NULL);
2524 if (_dbus_string_get_length (&auth->outgoing) == 0)
2527 *str = &auth->outgoing;
2533 * Notifies the auth conversation object that
2534 * the given number of bytes of the outgoing buffer
2535 * have been written out.
2537 * @param auth the auth conversation
2538 * @param bytes_sent number of bytes written out
2541 _dbus_auth_bytes_sent (DBusAuth *auth,
2544 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2545 DBUS_AUTH_NAME (auth),
2547 _dbus_string_get_const_data (&auth->outgoing));
2549 _dbus_string_delete (&auth->outgoing,
2554 * Get a buffer to be used for reading bytes from the peer we're conversing
2555 * with. Bytes should be appended to this buffer.
2557 * @param auth the auth conversation
2558 * @param buffer return location for buffer to append bytes to
2561 _dbus_auth_get_buffer (DBusAuth *auth,
2562 DBusString **buffer)
2564 _dbus_assert (auth != NULL);
2565 _dbus_assert (!auth->buffer_outstanding);
2567 *buffer = &auth->incoming;
2569 auth->buffer_outstanding = TRUE;
2573 * Returns a buffer with new data read into it.
2575 * @param auth the auth conversation
2576 * @param buffer the buffer being returned
2577 * @param bytes_read number of new bytes added
2580 _dbus_auth_return_buffer (DBusAuth *auth,
2584 _dbus_assert (buffer == &auth->incoming);
2585 _dbus_assert (auth->buffer_outstanding);
2587 auth->buffer_outstanding = FALSE;
2591 * Returns leftover bytes that were not used as part of the auth
2592 * conversation. These bytes will be part of the message stream
2593 * instead. This function may not be called until authentication has
2596 * @param auth the auth conversation
2597 * @param str return location for pointer to string of unused bytes
2600 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2601 const DBusString **str)
2603 if (!DBUS_AUTH_IN_END_STATE (auth))
2606 *str = &auth->incoming;
2611 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2612 * after we've gotten them and successfully moved them elsewhere.
2614 * @param auth the auth conversation
2617 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2619 if (!DBUS_AUTH_IN_END_STATE (auth))
2622 _dbus_string_set_length (&auth->incoming, 0);
2626 * Called post-authentication, indicates whether we need to encode
2627 * the message stream with _dbus_auth_encode_data() prior to
2628 * sending it to the peer.
2630 * @param auth the auth conversation
2631 * @returns #TRUE if we need to encode the stream
2634 _dbus_auth_needs_encoding (DBusAuth *auth)
2636 if (auth->state != &common_state_authenticated)
2639 if (auth->mech != NULL)
2641 if (DBUS_AUTH_IS_CLIENT (auth))
2642 return auth->mech->client_encode_func != NULL;
2644 return auth->mech->server_encode_func != NULL;
2651 * Called post-authentication, encodes a block of bytes for sending to
2652 * the peer. If no encoding was negotiated, just copies the bytes
2653 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2655 * @param auth the auth conversation
2656 * @param plaintext the plain text data
2657 * @param encoded initialized string to where encoded data is appended
2658 * @returns #TRUE if we had enough memory and successfully encoded
2661 _dbus_auth_encode_data (DBusAuth *auth,
2662 const DBusString *plaintext,
2663 DBusString *encoded)
2665 _dbus_assert (plaintext != encoded);
2667 if (auth->state != &common_state_authenticated)
2670 if (_dbus_auth_needs_encoding (auth))
2672 if (DBUS_AUTH_IS_CLIENT (auth))
2673 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2675 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2679 return _dbus_string_copy (plaintext, 0, encoded,
2680 _dbus_string_get_length (encoded));
2685 * Called post-authentication, indicates whether we need to decode
2686 * the message stream with _dbus_auth_decode_data() after
2687 * receiving it from the peer.
2689 * @param auth the auth conversation
2690 * @returns #TRUE if we need to encode the stream
2693 _dbus_auth_needs_decoding (DBusAuth *auth)
2695 if (auth->state != &common_state_authenticated)
2698 if (auth->mech != NULL)
2700 if (DBUS_AUTH_IS_CLIENT (auth))
2701 return auth->mech->client_decode_func != NULL;
2703 return auth->mech->server_decode_func != NULL;
2711 * Called post-authentication, decodes a block of bytes received from
2712 * the peer. If no encoding was negotiated, just copies the bytes (you
2713 * can avoid this by checking _dbus_auth_needs_decoding()).
2715 * @todo 1.0? We need to be able to distinguish "out of memory" error
2716 * from "the data is hosed" error.
2718 * @param auth the auth conversation
2719 * @param encoded the encoded data
2720 * @param plaintext initialized string where decoded data is appended
2721 * @returns #TRUE if we had enough memory and successfully decoded
2724 _dbus_auth_decode_data (DBusAuth *auth,
2725 const DBusString *encoded,
2726 DBusString *plaintext)
2728 _dbus_assert (plaintext != encoded);
2730 if (auth->state != &common_state_authenticated)
2733 if (_dbus_auth_needs_decoding (auth))
2735 if (DBUS_AUTH_IS_CLIENT (auth))
2736 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2738 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2742 return _dbus_string_copy (encoded, 0, plaintext,
2743 _dbus_string_get_length (plaintext));
2748 * Sets credentials received via reliable means from the operating
2751 * @param auth the auth conversation
2752 * @param credentials the credentials received
2753 * @returns #FALSE on OOM
2756 _dbus_auth_set_credentials (DBusAuth *auth,
2757 DBusCredentials *credentials)
2759 _dbus_credentials_clear (auth->credentials);
2760 return _dbus_credentials_add_credentials (auth->credentials,
2765 * Gets the identity we authorized the client as. Apps may have
2766 * different policies as to what identities they allow.
2768 * Returned credentials are not a copy and should not be modified
2770 * @param auth the auth conversation
2771 * @returns the credentials we've authorized BY REFERENCE do not modify
2774 _dbus_auth_get_identity (DBusAuth *auth)
2776 if (auth->state == &common_state_authenticated)
2778 return auth->authorized_identity;
2782 /* FIXME instead of this, keep an empty credential around that
2783 * doesn't require allocation or something
2785 /* return empty credentials */
2786 _dbus_assert (_dbus_credentials_are_empty (auth->authorized_identity));
2787 return auth->authorized_identity;
2792 * Gets the GUID from the server if we've authenticated; gets
2794 * @param auth the auth object
2795 * @returns the GUID in ASCII hex format
2798 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2800 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2802 if (auth->state == &common_state_authenticated)
2803 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2809 * Sets the "authentication context" which scopes cookies
2810 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2812 * @param auth the auth conversation
2813 * @param context the context
2814 * @returns #FALSE if no memory
2817 _dbus_auth_set_context (DBusAuth *auth,
2818 const DBusString *context)
2820 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2821 &auth->context, 0, _dbus_string_get_length (context));
2825 * Sets whether unix fd passing is potentially on the transport and
2826 * hence shall be negotiated.
2828 * @param auth the auth conversation
2829 * @param b TRUE when unix fd passing shall be negotiated, otherwise FALSE
2832 _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
2834 auth->unix_fd_possible = b;
2838 * Queries whether unix fd passing was successfully negotiated.
2840 * @param auth the auth conversion
2841 * @returns #TRUE when unix fd passing was negotiated.
2844 _dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
2846 return auth->unix_fd_negotiated;
2851 /* tests in dbus-auth-util.c */