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
23 #include "dbus-auth.h"
24 #include "dbus-string.h"
25 #include "dbus-list.h"
26 #include "dbus-internals.h"
27 #include "dbus-keyring.h"
29 #include "dbus-protocol.h"
30 #include "dbus-credentials.h"
33 * @defgroup DBusAuth Authentication
34 * @ingroup DBusInternals
35 * @brief DBusAuth object
37 * DBusAuth manages the authentication negotiation when a connection
38 * is first established, and also manage any encryption used over a
41 * @todo some SASL profiles require sending the empty string as a
42 * challenge/response, but we don't currently allow that in our
45 * @todo right now sometimes both ends will block waiting for input
46 * from the other end, e.g. if there's an error during
49 * @todo the cookie keyring needs to be cached globally not just
50 * per-auth (which raises threadsafety issues too)
52 * @todo grep FIXME in dbus-auth.c
56 * @defgroup DBusAuthInternals Authentication implementation details
57 * @ingroup DBusInternals
58 * @brief DBusAuth implementation details
60 * Private details of authentication code.
66 * This function appends an initial client response to the given string
68 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
69 DBusString *response);
72 * This function processes a block of data received from the peer.
73 * i.e. handles a DATA command.
75 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
76 const DBusString *data);
79 * This function encodes a block of data from the peer.
81 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
82 const DBusString *data,
86 * This function decodes a block of data from the peer.
88 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
89 const DBusString *data,
93 * This function is called when the mechanism is abandoned.
95 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
98 * Virtual table representing a particular auth mechanism.
102 const char *mechanism; /**< Name of the mechanism */
103 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
104 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
105 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
106 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
107 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
108 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
109 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
110 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
111 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
112 } DBusAuthMechanismHandler;
115 * Enumeration for the known authentication commands.
118 DBUS_AUTH_COMMAND_AUTH,
119 DBUS_AUTH_COMMAND_CANCEL,
120 DBUS_AUTH_COMMAND_DATA,
121 DBUS_AUTH_COMMAND_BEGIN,
122 DBUS_AUTH_COMMAND_REJECTED,
123 DBUS_AUTH_COMMAND_OK,
124 DBUS_AUTH_COMMAND_ERROR,
125 DBUS_AUTH_COMMAND_UNKNOWN,
126 DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD,
127 DBUS_AUTH_COMMAND_AGREE_UNIX_FD
131 * Auth state function, determines the reaction to incoming events for
132 * a particular state. Returns whether we had enough memory to
133 * complete the operation.
135 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
136 DBusAuthCommand command,
137 const DBusString *args);
140 * Information about a auth state.
144 const char *name; /**< Name of the state */
145 DBusAuthStateFunction handler; /**< State function for this state */
149 * Internal members of DBusAuth.
153 int refcount; /**< reference count */
154 const char *side; /**< Client or server */
156 DBusString incoming; /**< Incoming data buffer */
157 DBusString outgoing; /**< Outgoing data buffer */
159 const DBusAuthStateData *state; /**< Current protocol state */
161 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
163 DBusString identity; /**< Current identity we're authorizing
167 DBusCredentials *credentials; /**< Credentials read from socket
170 DBusCredentials *authorized_identity; /**< Credentials that are authorized */
172 DBusCredentials *desired_identity; /**< Identity client has requested */
174 DBusString context; /**< Cookie scope */
175 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
176 int cookie_id; /**< ID of cookie to use */
177 DBusString challenge; /**< Challenge sent to client */
179 char **allowed_mechs; /**< Mechanisms we're allowed to use,
180 * or #NULL if we can use any
183 unsigned int needed_memory : 1; /**< We needed memory to continue since last
184 * successful getting something done
186 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
187 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
188 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
190 unsigned int unix_fd_possible : 1; /**< This side could do unix fd passing */
191 unsigned int unix_fd_negotiated : 1; /**< Unix fd was successfully negotiated */
195 * "Subclass" of DBusAuth for client side
199 DBusAuth base; /**< Parent class */
201 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
203 DBusString guid_from_server; /**< GUID received from server */
208 * "Subclass" of DBusAuth for server side.
212 DBusAuth base; /**< Parent class */
214 int failures; /**< Number of times client has been rejected */
215 int max_failures; /**< Number of times we reject before disconnect */
217 DBusString guid; /**< Our globally unique ID in hex encoding */
221 static void goto_state (DBusAuth *auth,
222 const DBusAuthStateData *new_state);
223 static dbus_bool_t send_auth (DBusAuth *auth,
224 const DBusAuthMechanismHandler *mech);
225 static dbus_bool_t send_data (DBusAuth *auth,
227 static dbus_bool_t send_rejected (DBusAuth *auth);
228 static dbus_bool_t send_error (DBusAuth *auth,
229 const char *message);
230 static dbus_bool_t send_ok (DBusAuth *auth);
231 static dbus_bool_t send_begin (DBusAuth *auth);
232 static dbus_bool_t send_cancel (DBusAuth *auth);
233 static dbus_bool_t send_negotiate_unix_fd (DBusAuth *auth);
234 static dbus_bool_t send_agree_unix_fd (DBusAuth *auth);
240 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
241 DBusAuthCommand command,
242 const DBusString *args);
243 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
244 DBusAuthCommand command,
245 const DBusString *args);
246 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
247 DBusAuthCommand command,
248 const DBusString *args);
250 static const DBusAuthStateData server_state_waiting_for_auth = {
251 "WaitingForAuth", handle_server_state_waiting_for_auth
253 static const DBusAuthStateData server_state_waiting_for_data = {
254 "WaitingForData", handle_server_state_waiting_for_data
256 static const DBusAuthStateData server_state_waiting_for_begin = {
257 "WaitingForBegin", handle_server_state_waiting_for_begin
264 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
265 DBusAuthCommand command,
266 const DBusString *args);
267 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
268 DBusAuthCommand command,
269 const DBusString *args);
270 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
271 DBusAuthCommand command,
272 const DBusString *args);
273 static dbus_bool_t handle_client_state_waiting_for_agree_unix_fd (DBusAuth *auth,
274 DBusAuthCommand command,
275 const DBusString *args);
277 static const DBusAuthStateData client_state_need_send_auth = {
280 static const DBusAuthStateData client_state_waiting_for_data = {
281 "WaitingForData", handle_client_state_waiting_for_data
283 static const DBusAuthStateData client_state_waiting_for_ok = {
284 "WaitingForOK", handle_client_state_waiting_for_ok
286 static const DBusAuthStateData client_state_waiting_for_reject = {
287 "WaitingForReject", handle_client_state_waiting_for_reject
289 static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
290 "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
294 * Common terminal states. Terminal states have handler == NULL.
297 static const DBusAuthStateData common_state_authenticated = {
298 "Authenticated", NULL
301 static const DBusAuthStateData common_state_need_disconnect = {
302 "NeedDisconnect", NULL
305 static const char auth_side_client[] = "client";
306 static const char auth_side_server[] = "server";
308 * @param auth the auth conversation
309 * @returns #TRUE if the conversation is the server side
311 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
313 * @param auth the auth conversation
314 * @returns #TRUE if the conversation is the client side
316 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
318 * @param auth the auth conversation
319 * @returns auth cast to DBusAuthClient
321 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
323 * @param auth the auth conversation
324 * @returns auth cast to DBusAuthServer
326 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
329 * The name of the auth ("client" or "server")
330 * @param auth the auth conversation
333 #define DBUS_AUTH_NAME(auth) ((auth)->side)
336 _dbus_auth_new (int size)
340 auth = dbus_malloc0 (size);
346 auth->keyring = NULL;
347 auth->cookie_id = -1;
349 /* note that we don't use the max string length feature,
350 * because you can't use that feature if you're going to
351 * try to recover from out-of-memory (it creates
352 * what looks like unrecoverable inability to alloc
353 * more space in the string). But we do handle
354 * overlong buffers in _dbus_auth_do_work().
357 if (!_dbus_string_init (&auth->incoming))
360 if (!_dbus_string_init (&auth->outgoing))
363 if (!_dbus_string_init (&auth->identity))
366 if (!_dbus_string_init (&auth->context))
369 if (!_dbus_string_init (&auth->challenge))
372 /* default context if none is specified */
373 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
376 auth->credentials = _dbus_credentials_new ();
377 if (auth->credentials == NULL)
380 auth->authorized_identity = _dbus_credentials_new ();
381 if (auth->authorized_identity == NULL)
384 auth->desired_identity = _dbus_credentials_new ();
385 if (auth->desired_identity == NULL)
392 _dbus_credentials_unref (auth->desired_identity);
395 _dbus_credentials_unref (auth->authorized_identity);
397 _dbus_credentials_unref (auth->credentials);
399 /* last alloc was an append to context, which is freed already below */ ;
401 _dbus_string_free (&auth->challenge);
403 _dbus_string_free (&auth->context);
405 _dbus_string_free (&auth->identity);
407 _dbus_string_free (&auth->outgoing);
409 _dbus_string_free (&auth->incoming);
416 shutdown_mech (DBusAuth *auth)
418 /* Cancel any auth */
419 auth->already_asked_for_initial_response = FALSE;
420 _dbus_string_set_length (&auth->identity, 0);
422 _dbus_credentials_clear (auth->authorized_identity);
423 _dbus_credentials_clear (auth->desired_identity);
425 if (auth->mech != NULL)
427 _dbus_verbose ("%s: Shutting down mechanism %s\n",
428 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
430 if (DBUS_AUTH_IS_CLIENT (auth))
431 (* auth->mech->client_shutdown_func) (auth);
433 (* auth->mech->server_shutdown_func) (auth);
440 * DBUS_COOKIE_SHA1 mechanism
443 /* Returns TRUE but with an empty string hash if the
444 * cookie_id isn't known. As with all this code
445 * TRUE just means we had enough memory.
448 sha1_compute_hash (DBusAuth *auth,
450 const DBusString *server_challenge,
451 const DBusString *client_challenge,
458 _dbus_assert (auth->keyring != NULL);
462 if (!_dbus_string_init (&cookie))
465 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
469 if (_dbus_string_get_length (&cookie) == 0)
475 if (!_dbus_string_init (&to_hash))
478 if (!_dbus_string_copy (server_challenge, 0,
479 &to_hash, _dbus_string_get_length (&to_hash)))
482 if (!_dbus_string_append (&to_hash, ":"))
485 if (!_dbus_string_copy (client_challenge, 0,
486 &to_hash, _dbus_string_get_length (&to_hash)))
489 if (!_dbus_string_append (&to_hash, ":"))
492 if (!_dbus_string_copy (&cookie, 0,
493 &to_hash, _dbus_string_get_length (&to_hash)))
496 if (!_dbus_sha_compute (&to_hash, hash))
502 _dbus_string_zero (&to_hash);
503 _dbus_string_free (&to_hash);
505 _dbus_string_zero (&cookie);
506 _dbus_string_free (&cookie);
510 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
511 * entropy, we use 128. This is the number of bytes in the random
514 #define N_CHALLENGE_BYTES (128/8)
517 sha1_handle_first_client_response (DBusAuth *auth,
518 const DBusString *data)
520 /* We haven't sent a challenge yet, we're expecting a desired
521 * username from the client.
530 _dbus_string_set_length (&auth->challenge, 0);
532 if (_dbus_string_get_length (data) > 0)
534 if (_dbus_string_get_length (&auth->identity) > 0)
536 /* Tried to send two auth identities, wtf */
537 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
538 DBUS_AUTH_NAME (auth));
539 return send_rejected (auth);
543 /* this is our auth identity */
544 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
549 if (!_dbus_credentials_add_from_user (auth->desired_identity, data))
551 _dbus_verbose ("%s: Did not get a valid username from client\n",
552 DBUS_AUTH_NAME (auth));
553 return send_rejected (auth);
556 if (!_dbus_string_init (&tmp))
559 if (!_dbus_string_init (&tmp2))
561 _dbus_string_free (&tmp);
565 /* we cache the keyring for speed, so here we drop it if it's the
566 * wrong one. FIXME caching the keyring here is useless since we use
567 * a different DBusAuth for every connection.
570 !_dbus_keyring_is_for_credentials (auth->keyring,
571 auth->desired_identity))
573 _dbus_keyring_unref (auth->keyring);
574 auth->keyring = NULL;
577 if (auth->keyring == NULL)
579 dbus_error_init (&error);
580 auth->keyring = _dbus_keyring_new_for_credentials (auth->desired_identity,
584 if (auth->keyring == NULL)
586 if (dbus_error_has_name (&error,
587 DBUS_ERROR_NO_MEMORY))
589 dbus_error_free (&error);
594 _DBUS_ASSERT_ERROR_IS_SET (&error);
595 _dbus_verbose ("%s: Error loading keyring: %s\n",
596 DBUS_AUTH_NAME (auth), error.message);
597 if (send_rejected (auth))
598 retval = TRUE; /* retval is only about mem */
599 dbus_error_free (&error);
605 _dbus_assert (!dbus_error_is_set (&error));
609 _dbus_assert (auth->keyring != NULL);
611 dbus_error_init (&error);
612 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
613 if (auth->cookie_id < 0)
615 _DBUS_ASSERT_ERROR_IS_SET (&error);
616 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
617 DBUS_AUTH_NAME (auth), error.message);
618 if (send_rejected (auth))
620 dbus_error_free (&error);
625 _dbus_assert (!dbus_error_is_set (&error));
628 if (!_dbus_string_copy (&auth->context, 0,
629 &tmp2, _dbus_string_get_length (&tmp2)))
632 if (!_dbus_string_append (&tmp2, " "))
635 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
638 if (!_dbus_string_append (&tmp2, " "))
641 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
644 _dbus_string_set_length (&auth->challenge, 0);
645 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
648 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
649 _dbus_string_get_length (&tmp2)))
652 if (!send_data (auth, &tmp2))
655 goto_state (auth, &server_state_waiting_for_data);
659 _dbus_string_zero (&tmp);
660 _dbus_string_free (&tmp);
661 _dbus_string_zero (&tmp2);
662 _dbus_string_free (&tmp2);
668 sha1_handle_second_client_response (DBusAuth *auth,
669 const DBusString *data)
671 /* We are expecting a response which is the hex-encoded client
672 * challenge, space, then SHA-1 hash of the concatenation of our
673 * challenge, ":", client challenge, ":", secret key, all
677 DBusString client_challenge;
678 DBusString client_hash;
680 DBusString correct_hash;
684 if (!_dbus_string_find_blank (data, 0, &i))
686 _dbus_verbose ("%s: no space separator in client response\n",
687 DBUS_AUTH_NAME (auth));
688 return send_rejected (auth);
691 if (!_dbus_string_init (&client_challenge))
694 if (!_dbus_string_init (&client_hash))
697 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
701 _dbus_string_skip_blank (data, i, &i);
703 if (!_dbus_string_copy_len (data, i,
704 _dbus_string_get_length (data) - i,
709 if (_dbus_string_get_length (&client_challenge) == 0 ||
710 _dbus_string_get_length (&client_hash) == 0)
712 _dbus_verbose ("%s: zero-length client challenge or hash\n",
713 DBUS_AUTH_NAME (auth));
714 if (send_rejected (auth))
719 if (!_dbus_string_init (&correct_hash))
722 if (!sha1_compute_hash (auth, auth->cookie_id,
728 /* if cookie_id was invalid, then we get an empty hash */
729 if (_dbus_string_get_length (&correct_hash) == 0)
731 if (send_rejected (auth))
736 if (!_dbus_string_equal (&client_hash, &correct_hash))
738 if (send_rejected (auth))
743 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
744 auth->desired_identity))
747 /* Copy process ID from the socket credentials if it's there
749 if (!_dbus_credentials_add_credential (auth->authorized_identity,
750 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
757 _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
758 DBUS_AUTH_NAME (auth));
763 _dbus_string_zero (&correct_hash);
764 _dbus_string_free (&correct_hash);
766 _dbus_string_zero (&client_hash);
767 _dbus_string_free (&client_hash);
769 _dbus_string_free (&client_challenge);
775 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
776 const DBusString *data)
778 if (auth->cookie_id < 0)
779 return sha1_handle_first_client_response (auth, data);
781 return sha1_handle_second_client_response (auth, data);
785 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
787 auth->cookie_id = -1;
788 _dbus_string_set_length (&auth->challenge, 0);
792 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
793 DBusString *response)
800 if (!_dbus_string_init (&username))
803 if (!_dbus_append_user_from_current_process (&username))
806 if (!_dbus_string_hex_encode (&username, 0,
808 _dbus_string_get_length (response)))
814 _dbus_string_free (&username);
820 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
821 const DBusString *data)
823 /* The data we get from the server should be the cookie context
824 * name, the cookie ID, and the server challenge, separated by
825 * spaces. We send back our challenge string and the correct hash.
829 DBusString cookie_id_str;
830 DBusString server_challenge;
831 DBusString client_challenge;
832 DBusString correct_hash;
839 if (!_dbus_string_find_blank (data, 0, &i))
841 if (send_error (auth,
842 "Server did not send context/ID/challenge properly"))
847 if (!_dbus_string_init (&context))
850 if (!_dbus_string_copy_len (data, 0, i,
854 _dbus_string_skip_blank (data, i, &i);
855 if (!_dbus_string_find_blank (data, i, &j))
857 if (send_error (auth,
858 "Server did not send context/ID/challenge properly"))
863 if (!_dbus_string_init (&cookie_id_str))
866 if (!_dbus_string_copy_len (data, i, j - i,
870 if (!_dbus_string_init (&server_challenge))
874 _dbus_string_skip_blank (data, i, &i);
875 j = _dbus_string_get_length (data);
877 if (!_dbus_string_copy_len (data, i, j - i,
878 &server_challenge, 0))
881 if (!_dbus_keyring_validate_context (&context))
883 if (send_error (auth, "Server sent invalid cookie context"))
888 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
890 if (send_error (auth, "Could not parse cookie ID as an integer"))
895 if (_dbus_string_get_length (&server_challenge) == 0)
897 if (send_error (auth, "Empty server challenge string"))
902 if (auth->keyring == NULL)
906 dbus_error_init (&error);
907 auth->keyring = _dbus_keyring_new_for_credentials (NULL,
911 if (auth->keyring == NULL)
913 if (dbus_error_has_name (&error,
914 DBUS_ERROR_NO_MEMORY))
916 dbus_error_free (&error);
921 _DBUS_ASSERT_ERROR_IS_SET (&error);
923 _dbus_verbose ("%s: Error loading keyring: %s\n",
924 DBUS_AUTH_NAME (auth), error.message);
926 if (send_error (auth, "Could not load cookie file"))
927 retval = TRUE; /* retval is only about mem */
929 dbus_error_free (&error);
935 _dbus_assert (!dbus_error_is_set (&error));
939 _dbus_assert (auth->keyring != NULL);
941 if (!_dbus_string_init (&tmp))
944 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
947 if (!_dbus_string_init (&client_challenge))
950 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
953 if (!_dbus_string_init (&correct_hash))
956 if (!sha1_compute_hash (auth, val,
962 if (_dbus_string_get_length (&correct_hash) == 0)
964 /* couldn't find the cookie ID or something */
965 if (send_error (auth, "Don't have the requested cookie ID"))
970 _dbus_string_set_length (&tmp, 0);
972 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
973 _dbus_string_get_length (&tmp)))
976 if (!_dbus_string_append (&tmp, " "))
979 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
980 _dbus_string_get_length (&tmp)))
983 if (!send_data (auth, &tmp))
989 _dbus_string_zero (&correct_hash);
990 _dbus_string_free (&correct_hash);
992 _dbus_string_free (&client_challenge);
994 _dbus_string_zero (&tmp);
995 _dbus_string_free (&tmp);
997 _dbus_string_free (&server_challenge);
999 _dbus_string_free (&cookie_id_str);
1001 _dbus_string_free (&context);
1007 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1009 auth->cookie_id = -1;
1010 _dbus_string_set_length (&auth->challenge, 0);
1014 * EXTERNAL mechanism
1018 handle_server_data_external_mech (DBusAuth *auth,
1019 const DBusString *data)
1021 if (_dbus_credentials_are_anonymous (auth->credentials))
1023 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1024 DBUS_AUTH_NAME (auth));
1025 return send_rejected (auth);
1028 if (_dbus_string_get_length (data) > 0)
1030 if (_dbus_string_get_length (&auth->identity) > 0)
1032 /* Tried to send two auth identities, wtf */
1033 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1034 DBUS_AUTH_NAME (auth));
1035 return send_rejected (auth);
1039 /* this is our auth identity */
1040 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1045 /* Poke client for an auth identity, if none given */
1046 if (_dbus_string_get_length (&auth->identity) == 0 &&
1047 !auth->already_asked_for_initial_response)
1049 if (send_data (auth, NULL))
1051 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1052 DBUS_AUTH_NAME (auth));
1053 auth->already_asked_for_initial_response = TRUE;
1054 goto_state (auth, &server_state_waiting_for_data);
1061 _dbus_credentials_clear (auth->desired_identity);
1063 /* If auth->identity is still empty here, then client
1064 * responded with an empty string after we poked it for
1065 * an initial response. This means to try to auth the
1066 * identity provided in the credentials.
1068 if (_dbus_string_get_length (&auth->identity) == 0)
1070 if (!_dbus_credentials_add_credentials (auth->desired_identity,
1073 return FALSE; /* OOM */
1078 if (!_dbus_credentials_add_from_user (auth->desired_identity,
1081 _dbus_verbose ("%s: could not get credentials from uid string\n",
1082 DBUS_AUTH_NAME (auth));
1083 return send_rejected (auth);
1087 if (_dbus_credentials_are_anonymous (auth->desired_identity))
1089 _dbus_verbose ("%s: desired user %s is no good\n",
1090 DBUS_AUTH_NAME (auth),
1091 _dbus_string_get_const_data (&auth->identity));
1092 return send_rejected (auth);
1095 if (_dbus_credentials_are_superset (auth->credentials,
1096 auth->desired_identity))
1098 /* client has authenticated */
1099 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
1100 auth->desired_identity))
1103 /* also copy process ID from the socket credentials
1105 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1106 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1110 /* also copy audit data from the socket credentials
1112 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1113 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1117 if (!send_ok (auth))
1120 _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1121 DBUS_AUTH_NAME (auth));
1127 _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1128 DBUS_AUTH_NAME (auth));
1129 return send_rejected (auth);
1134 handle_server_shutdown_external_mech (DBusAuth *auth)
1140 handle_client_initial_response_external_mech (DBusAuth *auth,
1141 DBusString *response)
1143 /* We always append our UID as an initial response, so the server
1144 * doesn't have to send back an empty challenge to check whether we
1145 * want to specify an identity. i.e. this avoids a round trip that
1146 * the spec for the EXTERNAL mechanism otherwise requires.
1148 DBusString plaintext;
1150 if (!_dbus_string_init (&plaintext))
1153 if (!_dbus_append_user_from_current_process (&plaintext))
1156 if (!_dbus_string_hex_encode (&plaintext, 0,
1158 _dbus_string_get_length (response)))
1161 _dbus_string_free (&plaintext);
1166 _dbus_string_free (&plaintext);
1171 handle_client_data_external_mech (DBusAuth *auth,
1172 const DBusString *data)
1179 handle_client_shutdown_external_mech (DBusAuth *auth)
1185 * ANONYMOUS mechanism
1189 handle_server_data_anonymous_mech (DBusAuth *auth,
1190 const DBusString *data)
1192 if (_dbus_string_get_length (data) > 0)
1194 /* Client is allowed to send "trace" data, the only defined
1195 * meaning is that if it contains '@' it is an email address,
1196 * and otherwise it is anything else, and it's supposed to be
1199 if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1201 _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1202 DBUS_AUTH_NAME (auth));
1205 DBusString plaintext;
1207 _dbus_string_init_const (&plaintext, "D-Bus " DBUS_VERSION_STRING);
1208 _dbus_string_init (&encoded);
1209 _dbus_string_hex_encode (&plaintext, 0,
1212 _dbus_verbose ("%s: try '%s'\n",
1213 DBUS_AUTH_NAME (auth), _dbus_string_get_const_data (&encoded));
1215 return send_rejected (auth);
1218 _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1219 DBUS_AUTH_NAME (auth),
1220 _dbus_string_get_const_data (data));
1223 /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1224 _dbus_credentials_clear (auth->desired_identity);
1226 /* Copy process ID from the socket credentials
1228 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1229 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1233 /* Anonymous is always allowed */
1234 if (!send_ok (auth))
1237 _dbus_verbose ("%s: authenticated client as anonymous\n",
1238 DBUS_AUTH_NAME (auth));
1244 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1250 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1251 DBusString *response)
1253 /* Our initial response is a "trace" string which must be valid UTF-8
1254 * and must be an email address if it contains '@'.
1255 * We just send the dbus implementation info, like a user-agent or
1256 * something, because... why not. There's nothing guaranteed here
1257 * though, we could change it later.
1259 DBusString plaintext;
1261 if (!_dbus_string_init (&plaintext))
1264 if (!_dbus_string_append (&plaintext,
1265 "libdbus " DBUS_VERSION_STRING))
1268 if (!_dbus_string_hex_encode (&plaintext, 0,
1270 _dbus_string_get_length (response)))
1273 _dbus_string_free (&plaintext);
1278 _dbus_string_free (&plaintext);
1283 handle_client_data_anonymous_mech (DBusAuth *auth,
1284 const DBusString *data)
1291 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1296 /* Put mechanisms here in order of preference.
1297 * Right now we have:
1299 * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1300 * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1301 * - ANONYMOUS checks nothing but doesn't auth the person as a user
1303 * We might ideally add a mechanism to chain to Cyrus SASL so we can
1304 * use its mechanisms as well.
1307 static const DBusAuthMechanismHandler
1308 all_mechanisms[] = {
1310 handle_server_data_external_mech,
1312 handle_server_shutdown_external_mech,
1313 handle_client_initial_response_external_mech,
1314 handle_client_data_external_mech,
1316 handle_client_shutdown_external_mech },
1317 { "DBUS_COOKIE_SHA1",
1318 handle_server_data_cookie_sha1_mech,
1320 handle_server_shutdown_cookie_sha1_mech,
1321 handle_client_initial_response_cookie_sha1_mech,
1322 handle_client_data_cookie_sha1_mech,
1324 handle_client_shutdown_cookie_sha1_mech },
1326 handle_server_data_anonymous_mech,
1328 handle_server_shutdown_anonymous_mech,
1329 handle_client_initial_response_anonymous_mech,
1330 handle_client_data_anonymous_mech,
1332 handle_client_shutdown_anonymous_mech },
1336 static const DBusAuthMechanismHandler*
1337 find_mech (const DBusString *name,
1338 char **allowed_mechs)
1342 if (allowed_mechs != NULL &&
1343 !_dbus_string_array_contains ((const char**) allowed_mechs,
1344 _dbus_string_get_const_data (name)))
1348 while (all_mechanisms[i].mechanism != NULL)
1350 if (_dbus_string_equal_c_str (name,
1351 all_mechanisms[i].mechanism))
1353 return &all_mechanisms[i];
1362 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1364 DBusString auth_command;
1366 if (!_dbus_string_init (&auth_command))
1369 if (!_dbus_string_append (&auth_command,
1372 _dbus_string_free (&auth_command);
1376 if (!_dbus_string_append (&auth_command,
1379 _dbus_string_free (&auth_command);
1383 if (mech->client_initial_response_func != NULL)
1385 if (!_dbus_string_append (&auth_command, " "))
1387 _dbus_string_free (&auth_command);
1391 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1393 _dbus_string_free (&auth_command);
1398 if (!_dbus_string_append (&auth_command,
1401 _dbus_string_free (&auth_command);
1405 if (!_dbus_string_copy (&auth_command, 0,
1407 _dbus_string_get_length (&auth->outgoing)))
1409 _dbus_string_free (&auth_command);
1413 _dbus_string_free (&auth_command);
1414 shutdown_mech (auth);
1416 goto_state (auth, &client_state_waiting_for_data);
1422 send_data (DBusAuth *auth, DBusString *data)
1426 if (data == NULL || _dbus_string_get_length (data) == 0)
1427 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1430 old_len = _dbus_string_get_length (&auth->outgoing);
1431 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1434 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1435 _dbus_string_get_length (&auth->outgoing)))
1438 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1444 _dbus_string_set_length (&auth->outgoing, old_len);
1451 send_rejected (DBusAuth *auth)
1454 DBusAuthServer *server_auth;
1457 if (!_dbus_string_init (&command))
1460 if (!_dbus_string_append (&command,
1465 while (all_mechanisms[i].mechanism != NULL)
1467 if (!_dbus_string_append (&command,
1471 if (!_dbus_string_append (&command,
1472 all_mechanisms[i].mechanism))
1478 if (!_dbus_string_append (&command, "\r\n"))
1481 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1482 _dbus_string_get_length (&auth->outgoing)))
1485 shutdown_mech (auth);
1487 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1488 server_auth = DBUS_AUTH_SERVER (auth);
1489 server_auth->failures += 1;
1491 if (server_auth->failures >= server_auth->max_failures)
1492 goto_state (auth, &common_state_need_disconnect);
1494 goto_state (auth, &server_state_waiting_for_auth);
1496 _dbus_string_free (&command);
1501 _dbus_string_free (&command);
1506 send_error (DBusAuth *auth, const char *message)
1508 return _dbus_string_append_printf (&auth->outgoing,
1509 "ERROR \"%s\"\r\n", message);
1513 send_ok (DBusAuth *auth)
1517 orig_len = _dbus_string_get_length (&auth->outgoing);
1519 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1520 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1523 _dbus_string_get_length (&auth->outgoing)) &&
1524 _dbus_string_append (&auth->outgoing, "\r\n"))
1526 goto_state (auth, &server_state_waiting_for_begin);
1531 _dbus_string_set_length (&auth->outgoing, orig_len);
1537 send_begin (DBusAuth *auth)
1540 if (!_dbus_string_append (&auth->outgoing,
1544 goto_state (auth, &common_state_authenticated);
1549 process_ok(DBusAuth *auth,
1550 const DBusString *args_from_ok) {
1554 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1555 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1557 /* We decode the hex string to binary, using guid_from_server as scratch... */
1560 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1561 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1564 /* now clear out the scratch */
1565 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1567 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1570 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1571 end_of_hex, _dbus_string_get_length (args_from_ok));
1572 goto_state (auth, &common_state_need_disconnect);
1576 if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1577 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1581 _dbus_verbose ("Got GUID '%s' from the server\n",
1582 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1584 if (auth->unix_fd_possible)
1585 return send_negotiate_unix_fd(auth);
1587 _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1588 return send_begin (auth);
1592 send_cancel (DBusAuth *auth)
1594 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1596 goto_state (auth, &client_state_waiting_for_reject);
1604 process_data (DBusAuth *auth,
1605 const DBusString *args,
1606 DBusAuthDataFunction data_func)
1611 if (!_dbus_string_init (&decoded))
1614 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1616 _dbus_string_free (&decoded);
1620 if (_dbus_string_get_length (args) != end)
1622 _dbus_string_free (&decoded);
1623 if (!send_error (auth, "Invalid hex encoding"))
1629 #ifdef DBUS_ENABLE_VERBOSE_MODE
1630 if (_dbus_string_validate_ascii (&decoded, 0,
1631 _dbus_string_get_length (&decoded)))
1632 _dbus_verbose ("%s: data: '%s'\n",
1633 DBUS_AUTH_NAME (auth),
1634 _dbus_string_get_const_data (&decoded));
1637 if (!(* data_func) (auth, &decoded))
1639 _dbus_string_free (&decoded);
1643 _dbus_string_free (&decoded);
1648 send_negotiate_unix_fd (DBusAuth *auth)
1650 if (!_dbus_string_append (&auth->outgoing,
1651 "NEGOTIATE_UNIX_FD\r\n"))
1654 goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1659 send_agree_unix_fd (DBusAuth *auth)
1661 _dbus_assert(auth->unix_fd_possible);
1663 auth->unix_fd_negotiated = TRUE;
1664 _dbus_verbose("Agreed to UNIX FD passing\n");
1666 if (!_dbus_string_append (&auth->outgoing,
1667 "AGREE_UNIX_FD\r\n"))
1670 goto_state (auth, &server_state_waiting_for_begin);
1675 handle_auth (DBusAuth *auth, const DBusString *args)
1677 if (_dbus_string_get_length (args) == 0)
1679 /* No args to the auth, send mechanisms */
1680 if (!send_rejected (auth))
1689 DBusString hex_response;
1691 _dbus_string_find_blank (args, 0, &i);
1693 if (!_dbus_string_init (&mech))
1696 if (!_dbus_string_init (&hex_response))
1698 _dbus_string_free (&mech);
1702 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1705 _dbus_string_skip_blank (args, i, &i);
1706 if (!_dbus_string_copy (args, i, &hex_response, 0))
1709 auth->mech = find_mech (&mech, auth->allowed_mechs);
1710 if (auth->mech != NULL)
1712 _dbus_verbose ("%s: Trying mechanism %s\n",
1713 DBUS_AUTH_NAME (auth),
1714 auth->mech->mechanism);
1716 if (!process_data (auth, &hex_response,
1717 auth->mech->server_data_func))
1722 /* Unsupported mechanism */
1723 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1724 DBUS_AUTH_NAME (auth),
1725 _dbus_string_get_const_data (&mech));
1727 if (!send_rejected (auth))
1731 _dbus_string_free (&mech);
1732 _dbus_string_free (&hex_response);
1738 _dbus_string_free (&mech);
1739 _dbus_string_free (&hex_response);
1745 handle_server_state_waiting_for_auth (DBusAuth *auth,
1746 DBusAuthCommand command,
1747 const DBusString *args)
1751 case DBUS_AUTH_COMMAND_AUTH:
1752 return handle_auth (auth, args);
1754 case DBUS_AUTH_COMMAND_CANCEL:
1755 case DBUS_AUTH_COMMAND_DATA:
1756 return send_error (auth, "Not currently in an auth conversation");
1758 case DBUS_AUTH_COMMAND_BEGIN:
1759 goto_state (auth, &common_state_need_disconnect);
1762 case DBUS_AUTH_COMMAND_ERROR:
1763 return send_rejected (auth);
1765 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1766 return send_error (auth, "Need to authenticate first");
1768 case DBUS_AUTH_COMMAND_REJECTED:
1769 case DBUS_AUTH_COMMAND_OK:
1770 case DBUS_AUTH_COMMAND_UNKNOWN:
1771 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1773 return send_error (auth, "Unknown command");
1778 handle_server_state_waiting_for_data (DBusAuth *auth,
1779 DBusAuthCommand command,
1780 const DBusString *args)
1784 case DBUS_AUTH_COMMAND_AUTH:
1785 return send_error (auth, "Sent AUTH while another AUTH in progress");
1787 case DBUS_AUTH_COMMAND_CANCEL:
1788 case DBUS_AUTH_COMMAND_ERROR:
1789 return send_rejected (auth);
1791 case DBUS_AUTH_COMMAND_DATA:
1792 return process_data (auth, args, auth->mech->server_data_func);
1794 case DBUS_AUTH_COMMAND_BEGIN:
1795 goto_state (auth, &common_state_need_disconnect);
1798 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1799 return send_error (auth, "Need to authenticate first");
1801 case DBUS_AUTH_COMMAND_REJECTED:
1802 case DBUS_AUTH_COMMAND_OK:
1803 case DBUS_AUTH_COMMAND_UNKNOWN:
1804 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1806 return send_error (auth, "Unknown command");
1811 handle_server_state_waiting_for_begin (DBusAuth *auth,
1812 DBusAuthCommand command,
1813 const DBusString *args)
1817 case DBUS_AUTH_COMMAND_AUTH:
1818 return send_error (auth, "Sent AUTH while expecting BEGIN");
1820 case DBUS_AUTH_COMMAND_DATA:
1821 return send_error (auth, "Sent DATA while expecting BEGIN");
1823 case DBUS_AUTH_COMMAND_BEGIN:
1824 goto_state (auth, &common_state_authenticated);
1827 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1828 if (auth->unix_fd_possible)
1829 return send_agree_unix_fd(auth);
1831 return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1833 case DBUS_AUTH_COMMAND_REJECTED:
1834 case DBUS_AUTH_COMMAND_OK:
1835 case DBUS_AUTH_COMMAND_UNKNOWN:
1836 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1838 return send_error (auth, "Unknown command");
1840 case DBUS_AUTH_COMMAND_CANCEL:
1841 case DBUS_AUTH_COMMAND_ERROR:
1842 return send_rejected (auth);
1846 /* return FALSE if no memory, TRUE if all OK */
1848 get_word (const DBusString *str,
1854 _dbus_string_skip_blank (str, *start, start);
1855 _dbus_string_find_blank (str, *start, &i);
1859 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1869 record_mechanisms (DBusAuth *auth,
1870 const DBusString *args)
1875 if (auth->already_got_mechanisms)
1878 len = _dbus_string_get_length (args);
1884 const DBusAuthMechanismHandler *mech;
1886 if (!_dbus_string_init (&m))
1889 if (!get_word (args, &next, &m))
1891 _dbus_string_free (&m);
1895 mech = find_mech (&m, auth->allowed_mechs);
1899 /* FIXME right now we try mechanisms in the order
1900 * the server lists them; should we do them in
1901 * some more deterministic order?
1903 * Probably in all_mechanisms order, our order of
1904 * preference. Of course when the server is us,
1905 * it lists things in that order anyhow.
1908 if (mech != &all_mechanisms[0])
1910 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1911 DBUS_AUTH_NAME (auth), mech->mechanism);
1913 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1916 _dbus_string_free (&m);
1922 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1923 DBUS_AUTH_NAME (auth), mech->mechanism);
1928 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1929 DBUS_AUTH_NAME (auth),
1930 _dbus_string_get_const_data (&m));
1933 _dbus_string_free (&m);
1936 auth->already_got_mechanisms = TRUE;
1941 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1947 process_rejected (DBusAuth *auth, const DBusString *args)
1949 const DBusAuthMechanismHandler *mech;
1950 DBusAuthClient *client;
1952 client = DBUS_AUTH_CLIENT (auth);
1954 if (!auth->already_got_mechanisms)
1956 if (!record_mechanisms (auth, args))
1960 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1962 mech = client->mechs_to_try->data;
1964 if (!send_auth (auth, mech))
1967 _dbus_list_pop_first (&client->mechs_to_try);
1969 _dbus_verbose ("%s: Trying mechanism %s\n",
1970 DBUS_AUTH_NAME (auth),
1976 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1977 DBUS_AUTH_NAME (auth));
1978 goto_state (auth, &common_state_need_disconnect);
1986 handle_client_state_waiting_for_data (DBusAuth *auth,
1987 DBusAuthCommand command,
1988 const DBusString *args)
1990 _dbus_assert (auth->mech != NULL);
1994 case DBUS_AUTH_COMMAND_DATA:
1995 return process_data (auth, args, auth->mech->client_data_func);
1997 case DBUS_AUTH_COMMAND_REJECTED:
1998 return process_rejected (auth, args);
2000 case DBUS_AUTH_COMMAND_OK:
2001 return process_ok(auth, args);
2003 case DBUS_AUTH_COMMAND_ERROR:
2004 return send_cancel (auth);
2006 case DBUS_AUTH_COMMAND_AUTH:
2007 case DBUS_AUTH_COMMAND_CANCEL:
2008 case DBUS_AUTH_COMMAND_BEGIN:
2009 case DBUS_AUTH_COMMAND_UNKNOWN:
2010 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2011 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2013 return send_error (auth, "Unknown command");
2018 handle_client_state_waiting_for_ok (DBusAuth *auth,
2019 DBusAuthCommand command,
2020 const DBusString *args)
2024 case DBUS_AUTH_COMMAND_REJECTED:
2025 return process_rejected (auth, args);
2027 case DBUS_AUTH_COMMAND_OK:
2028 return process_ok(auth, args);
2030 case DBUS_AUTH_COMMAND_DATA:
2031 case DBUS_AUTH_COMMAND_ERROR:
2032 return send_cancel (auth);
2034 case DBUS_AUTH_COMMAND_AUTH:
2035 case DBUS_AUTH_COMMAND_CANCEL:
2036 case DBUS_AUTH_COMMAND_BEGIN:
2037 case DBUS_AUTH_COMMAND_UNKNOWN:
2038 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2039 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2041 return send_error (auth, "Unknown command");
2046 handle_client_state_waiting_for_reject (DBusAuth *auth,
2047 DBusAuthCommand command,
2048 const DBusString *args)
2052 case DBUS_AUTH_COMMAND_REJECTED:
2053 return process_rejected (auth, args);
2055 case DBUS_AUTH_COMMAND_AUTH:
2056 case DBUS_AUTH_COMMAND_CANCEL:
2057 case DBUS_AUTH_COMMAND_DATA:
2058 case DBUS_AUTH_COMMAND_BEGIN:
2059 case DBUS_AUTH_COMMAND_OK:
2060 case DBUS_AUTH_COMMAND_ERROR:
2061 case DBUS_AUTH_COMMAND_UNKNOWN:
2062 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2063 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2065 goto_state (auth, &common_state_need_disconnect);
2071 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2072 DBusAuthCommand command,
2073 const DBusString *args)
2077 case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2078 _dbus_assert(auth->unix_fd_possible);
2079 auth->unix_fd_negotiated = TRUE;
2080 _dbus_verbose("Sucessfully negotiated UNIX FD passing\n");
2081 return send_begin (auth);
2083 case DBUS_AUTH_COMMAND_ERROR:
2084 _dbus_assert(auth->unix_fd_possible);
2085 auth->unix_fd_negotiated = FALSE;
2086 _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2087 return send_begin (auth);
2089 case DBUS_AUTH_COMMAND_OK:
2090 case DBUS_AUTH_COMMAND_DATA:
2091 case DBUS_AUTH_COMMAND_REJECTED:
2092 case DBUS_AUTH_COMMAND_AUTH:
2093 case DBUS_AUTH_COMMAND_CANCEL:
2094 case DBUS_AUTH_COMMAND_BEGIN:
2095 case DBUS_AUTH_COMMAND_UNKNOWN:
2096 case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2098 return send_error (auth, "Unknown command");
2103 * Mapping from command name to enum
2106 const char *name; /**< Name of the command */
2107 DBusAuthCommand command; /**< Corresponding enum */
2108 } DBusAuthCommandName;
2110 static const DBusAuthCommandName auth_command_names[] = {
2111 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2112 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2113 { "DATA", DBUS_AUTH_COMMAND_DATA },
2114 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2115 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2116 { "OK", DBUS_AUTH_COMMAND_OK },
2117 { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2118 { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2119 { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2122 static DBusAuthCommand
2123 lookup_command_from_name (DBusString *command)
2127 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2129 if (_dbus_string_equal_c_str (command,
2130 auth_command_names[i].name))
2131 return auth_command_names[i].command;
2134 return DBUS_AUTH_COMMAND_UNKNOWN;
2138 goto_state (DBusAuth *auth,
2139 const DBusAuthStateData *state)
2141 _dbus_verbose ("%s: going from state %s to state %s\n",
2142 DBUS_AUTH_NAME (auth),
2146 auth->state = state;
2149 /* returns whether to call it again right away */
2151 process_command (DBusAuth *auth)
2153 DBusAuthCommand command;
2160 /* _dbus_verbose ("%s: trying process_command()\n"); */
2165 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2168 if (!_dbus_string_init (&line))
2170 auth->needed_memory = TRUE;
2174 if (!_dbus_string_init (&args))
2176 _dbus_string_free (&line);
2177 auth->needed_memory = TRUE;
2181 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2184 if (!_dbus_string_validate_ascii (&line, 0,
2185 _dbus_string_get_length (&line)))
2187 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2188 DBUS_AUTH_NAME (auth));
2189 if (!send_error (auth, "Command contained non-ASCII"))
2195 _dbus_verbose ("%s: got command \"%s\"\n",
2196 DBUS_AUTH_NAME (auth),
2197 _dbus_string_get_const_data (&line));
2199 _dbus_string_find_blank (&line, 0, &i);
2200 _dbus_string_skip_blank (&line, i, &j);
2203 _dbus_string_delete (&line, i, j - i);
2205 if (!_dbus_string_move (&line, i, &args, 0))
2208 /* FIXME 1.0 we should probably validate that only the allowed
2209 * chars are in the command name
2212 command = lookup_command_from_name (&line);
2213 if (!(* auth->state->handler) (auth, command, &args))
2218 /* We've succeeded in processing the whole command so drop it out
2219 * of the incoming buffer and return TRUE to try another command.
2222 _dbus_string_delete (&auth->incoming, 0, eol);
2225 _dbus_string_delete (&auth->incoming, 0, 2);
2230 _dbus_string_free (&args);
2231 _dbus_string_free (&line);
2234 auth->needed_memory = TRUE;
2236 auth->needed_memory = FALSE;
2245 * @addtogroup DBusAuth
2250 * Creates a new auth conversation object for the server side.
2251 * See doc/dbus-sasl-profile.txt for full details on what
2254 * @returns the new object or #NULL if no memory
2257 _dbus_auth_server_new (const DBusString *guid)
2260 DBusAuthServer *server_auth;
2261 DBusString guid_copy;
2263 if (!_dbus_string_init (&guid_copy))
2266 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2268 _dbus_string_free (&guid_copy);
2272 auth = _dbus_auth_new (sizeof (DBusAuthServer));
2275 _dbus_string_free (&guid_copy);
2279 auth->side = auth_side_server;
2280 auth->state = &server_state_waiting_for_auth;
2282 server_auth = DBUS_AUTH_SERVER (auth);
2284 server_auth->guid = guid_copy;
2286 /* perhaps this should be per-mechanism with a lower
2289 server_auth->failures = 0;
2290 server_auth->max_failures = 6;
2296 * Creates a new auth conversation object for the client side.
2297 * See doc/dbus-sasl-profile.txt for full details on what
2300 * @returns the new object or #NULL if no memory
2303 _dbus_auth_client_new (void)
2306 DBusString guid_str;
2308 if (!_dbus_string_init (&guid_str))
2311 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2314 _dbus_string_free (&guid_str);
2318 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2320 auth->side = auth_side_client;
2321 auth->state = &client_state_need_send_auth;
2323 /* Start the auth conversation by sending AUTH for our default
2325 if (!send_auth (auth, &all_mechanisms[0]))
2327 _dbus_auth_unref (auth);
2335 * Increments the refcount of an auth object.
2337 * @param auth the auth conversation
2338 * @returns the auth conversation
2341 _dbus_auth_ref (DBusAuth *auth)
2343 _dbus_assert (auth != NULL);
2345 auth->refcount += 1;
2351 * Decrements the refcount of an auth object.
2353 * @param auth the auth conversation
2356 _dbus_auth_unref (DBusAuth *auth)
2358 _dbus_assert (auth != NULL);
2359 _dbus_assert (auth->refcount > 0);
2361 auth->refcount -= 1;
2362 if (auth->refcount == 0)
2364 shutdown_mech (auth);
2366 if (DBUS_AUTH_IS_CLIENT (auth))
2368 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2369 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2373 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2375 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2379 _dbus_keyring_unref (auth->keyring);
2381 _dbus_string_free (&auth->context);
2382 _dbus_string_free (&auth->challenge);
2383 _dbus_string_free (&auth->identity);
2384 _dbus_string_free (&auth->incoming);
2385 _dbus_string_free (&auth->outgoing);
2387 dbus_free_string_array (auth->allowed_mechs);
2389 _dbus_credentials_unref (auth->credentials);
2390 _dbus_credentials_unref (auth->authorized_identity);
2391 _dbus_credentials_unref (auth->desired_identity);
2398 * Sets an array of authentication mechanism names
2399 * that we are willing to use.
2401 * @param auth the auth conversation
2402 * @param mechanisms #NULL-terminated array of mechanism names
2403 * @returns #FALSE if no memory
2406 _dbus_auth_set_mechanisms (DBusAuth *auth,
2407 const char **mechanisms)
2411 if (mechanisms != NULL)
2413 copy = _dbus_dup_string_array (mechanisms);
2420 dbus_free_string_array (auth->allowed_mechs);
2422 auth->allowed_mechs = copy;
2428 * @param auth the auth conversation object
2429 * @returns #TRUE if we're in a final state
2431 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2434 * Analyzes buffered input and moves the auth conversation forward,
2435 * returning the new state of the auth conversation.
2437 * @param auth the auth conversation
2438 * @returns the new state
2441 _dbus_auth_do_work (DBusAuth *auth)
2443 auth->needed_memory = FALSE;
2445 /* Max amount we'll buffer up before deciding someone's on crack */
2446 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2450 if (DBUS_AUTH_IN_END_STATE (auth))
2453 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2454 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2456 goto_state (auth, &common_state_need_disconnect);
2457 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2458 DBUS_AUTH_NAME (auth));
2462 while (process_command (auth));
2464 if (auth->needed_memory)
2465 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2466 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2467 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2468 else if (auth->state == &common_state_need_disconnect)
2469 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2470 else if (auth->state == &common_state_authenticated)
2471 return DBUS_AUTH_STATE_AUTHENTICATED;
2472 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2476 * Gets bytes that need to be sent to the peer we're conversing with.
2477 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2478 * to notify the auth object that they were written.
2480 * @param auth the auth conversation
2481 * @param str return location for a ref to the buffer to send
2482 * @returns #FALSE if nothing to send
2485 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2486 const DBusString **str)
2488 _dbus_assert (auth != NULL);
2489 _dbus_assert (str != NULL);
2493 if (_dbus_string_get_length (&auth->outgoing) == 0)
2496 *str = &auth->outgoing;
2502 * Notifies the auth conversation object that
2503 * the given number of bytes of the outgoing buffer
2504 * have been written out.
2506 * @param auth the auth conversation
2507 * @param bytes_sent number of bytes written out
2510 _dbus_auth_bytes_sent (DBusAuth *auth,
2513 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2514 DBUS_AUTH_NAME (auth),
2516 _dbus_string_get_const_data (&auth->outgoing));
2518 _dbus_string_delete (&auth->outgoing,
2523 * Get a buffer to be used for reading bytes from the peer we're conversing
2524 * with. Bytes should be appended to this buffer.
2526 * @param auth the auth conversation
2527 * @param buffer return location for buffer to append bytes to
2530 _dbus_auth_get_buffer (DBusAuth *auth,
2531 DBusString **buffer)
2533 _dbus_assert (auth != NULL);
2534 _dbus_assert (!auth->buffer_outstanding);
2536 *buffer = &auth->incoming;
2538 auth->buffer_outstanding = TRUE;
2542 * Returns a buffer with new data read into it.
2544 * @param auth the auth conversation
2545 * @param buffer the buffer being returned
2546 * @param bytes_read number of new bytes added
2549 _dbus_auth_return_buffer (DBusAuth *auth,
2553 _dbus_assert (buffer == &auth->incoming);
2554 _dbus_assert (auth->buffer_outstanding);
2556 auth->buffer_outstanding = FALSE;
2560 * Returns leftover bytes that were not used as part of the auth
2561 * conversation. These bytes will be part of the message stream
2562 * instead. This function may not be called until authentication has
2565 * @param auth the auth conversation
2566 * @param str return location for pointer to string of unused bytes
2569 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2570 const DBusString **str)
2572 if (!DBUS_AUTH_IN_END_STATE (auth))
2575 *str = &auth->incoming;
2580 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2581 * after we've gotten them and successfully moved them elsewhere.
2583 * @param auth the auth conversation
2586 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2588 if (!DBUS_AUTH_IN_END_STATE (auth))
2591 _dbus_string_set_length (&auth->incoming, 0);
2595 * Called post-authentication, indicates whether we need to encode
2596 * the message stream with _dbus_auth_encode_data() prior to
2597 * sending it to the peer.
2599 * @param auth the auth conversation
2600 * @returns #TRUE if we need to encode the stream
2603 _dbus_auth_needs_encoding (DBusAuth *auth)
2605 if (auth->state != &common_state_authenticated)
2608 if (auth->mech != NULL)
2610 if (DBUS_AUTH_IS_CLIENT (auth))
2611 return auth->mech->client_encode_func != NULL;
2613 return auth->mech->server_encode_func != NULL;
2620 * Called post-authentication, encodes a block of bytes for sending to
2621 * the peer. If no encoding was negotiated, just copies the bytes
2622 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2624 * @param auth the auth conversation
2625 * @param plaintext the plain text data
2626 * @param encoded initialized string to where encoded data is appended
2627 * @returns #TRUE if we had enough memory and successfully encoded
2630 _dbus_auth_encode_data (DBusAuth *auth,
2631 const DBusString *plaintext,
2632 DBusString *encoded)
2634 _dbus_assert (plaintext != encoded);
2636 if (auth->state != &common_state_authenticated)
2639 if (_dbus_auth_needs_encoding (auth))
2641 if (DBUS_AUTH_IS_CLIENT (auth))
2642 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2644 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2648 return _dbus_string_copy (plaintext, 0, encoded,
2649 _dbus_string_get_length (encoded));
2654 * Called post-authentication, indicates whether we need to decode
2655 * the message stream with _dbus_auth_decode_data() after
2656 * receiving it from the peer.
2658 * @param auth the auth conversation
2659 * @returns #TRUE if we need to encode the stream
2662 _dbus_auth_needs_decoding (DBusAuth *auth)
2664 if (auth->state != &common_state_authenticated)
2667 if (auth->mech != NULL)
2669 if (DBUS_AUTH_IS_CLIENT (auth))
2670 return auth->mech->client_decode_func != NULL;
2672 return auth->mech->server_decode_func != NULL;
2680 * Called post-authentication, decodes a block of bytes received from
2681 * the peer. If no encoding was negotiated, just copies the bytes (you
2682 * can avoid this by checking _dbus_auth_needs_decoding()).
2684 * @todo 1.0? We need to be able to distinguish "out of memory" error
2685 * from "the data is hosed" error.
2687 * @param auth the auth conversation
2688 * @param encoded the encoded data
2689 * @param plaintext initialized string where decoded data is appended
2690 * @returns #TRUE if we had enough memory and successfully decoded
2693 _dbus_auth_decode_data (DBusAuth *auth,
2694 const DBusString *encoded,
2695 DBusString *plaintext)
2697 _dbus_assert (plaintext != encoded);
2699 if (auth->state != &common_state_authenticated)
2702 if (_dbus_auth_needs_decoding (auth))
2704 if (DBUS_AUTH_IS_CLIENT (auth))
2705 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2707 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2711 return _dbus_string_copy (encoded, 0, plaintext,
2712 _dbus_string_get_length (plaintext));
2717 * Sets credentials received via reliable means from the operating
2720 * @param auth the auth conversation
2721 * @param credentials the credentials received
2722 * @returns #FALSE on OOM
2725 _dbus_auth_set_credentials (DBusAuth *auth,
2726 DBusCredentials *credentials)
2728 _dbus_credentials_clear (auth->credentials);
2729 return _dbus_credentials_add_credentials (auth->credentials,
2734 * Gets the identity we authorized the client as. Apps may have
2735 * different policies as to what identities they allow.
2737 * Returned credentials are not a copy and should not be modified
2739 * @param auth the auth conversation
2740 * @returns the credentials we've authorized BY REFERENCE do not modify
2743 _dbus_auth_get_identity (DBusAuth *auth)
2745 if (auth->state == &common_state_authenticated)
2747 return auth->authorized_identity;
2751 /* FIXME instead of this, keep an empty credential around that
2752 * doesn't require allocation or something
2754 /* return empty credentials */
2755 _dbus_assert (_dbus_credentials_are_empty (auth->authorized_identity));
2756 return auth->authorized_identity;
2761 * Gets the GUID from the server if we've authenticated; gets
2763 * @param auth the auth object
2764 * @returns the GUID in ASCII hex format
2767 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2769 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2771 if (auth->state == &common_state_authenticated)
2772 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2778 * Sets the "authentication context" which scopes cookies
2779 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2781 * @param auth the auth conversation
2782 * @param context the context
2783 * @returns #FALSE if no memory
2786 _dbus_auth_set_context (DBusAuth *auth,
2787 const DBusString *context)
2789 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2790 &auth->context, 0, _dbus_string_get_length (context));
2794 * Sets whether unix fd passing is potentially on the transport and
2795 * hence shall be negotiated.
2797 * @param auth the auth conversation
2798 * @param b TRUE when unix fd passing shall be negotiated, otherwise FALSE
2801 _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
2803 auth->unix_fd_possible = b;
2807 * Queries whether unix fd passing was sucessfully negotiated.
2809 * @param auth the auth conversion
2810 * @returns #TRUE when unix fd passing was negotiated.
2813 _dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
2815 return auth->unix_fd_negotiated;
2820 /* tests in dbus-auth-util.c */