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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
129 * Auth state function, determines the reaction to incoming events for
130 * a particular state. Returns whether we had enough memory to
131 * complete the operation.
133 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
134 DBusAuthCommand command,
135 const DBusString *args);
138 * Information about a auth state.
142 const char *name; /**< Name of the state */
143 DBusAuthStateFunction handler; /**< State function for this state */
147 * Internal members of DBusAuth.
151 int refcount; /**< reference count */
152 const char *side; /**< Client or server */
154 DBusString incoming; /**< Incoming data buffer */
155 DBusString outgoing; /**< Outgoing data buffer */
157 const DBusAuthStateData *state; /**< Current protocol state */
159 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
161 DBusString identity; /**< Current identity we're authorizing
165 DBusCredentials *credentials; /**< Credentials read from socket
168 DBusCredentials *authorized_identity; /**< Credentials that are authorized */
170 DBusCredentials *desired_identity; /**< Identity client has requested */
172 DBusString context; /**< Cookie scope */
173 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
174 int cookie_id; /**< ID of cookie to use */
175 DBusString challenge; /**< Challenge sent to client */
177 char **allowed_mechs; /**< Mechanisms we're allowed to use,
178 * or #NULL if we can use any
181 unsigned int needed_memory : 1; /**< We needed memory to continue since last
182 * successful getting something done
184 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
185 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
186 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
190 * "Subclass" of DBusAuth for client side
194 DBusAuth base; /**< Parent class */
196 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
198 DBusString guid_from_server; /**< GUID received from server */
203 * "Subclass" of DBusAuth for server side.
207 DBusAuth base; /**< Parent class */
209 int failures; /**< Number of times client has been rejected */
210 int max_failures; /**< Number of times we reject before disconnect */
212 DBusString guid; /**< Our globally unique ID in hex encoding */
216 static void goto_state (DBusAuth *auth,
217 const DBusAuthStateData *new_state);
218 static dbus_bool_t send_auth (DBusAuth *auth,
219 const DBusAuthMechanismHandler *mech);
220 static dbus_bool_t send_data (DBusAuth *auth,
222 static dbus_bool_t send_rejected (DBusAuth *auth);
223 static dbus_bool_t send_error (DBusAuth *auth,
224 const char *message);
225 static dbus_bool_t send_ok (DBusAuth *auth);
226 static dbus_bool_t send_begin (DBusAuth *auth,
227 const DBusString *args_from_ok);
228 static dbus_bool_t send_cancel (DBusAuth *auth);
234 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
235 DBusAuthCommand command,
236 const DBusString *args);
237 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
238 DBusAuthCommand command,
239 const DBusString *args);
240 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
241 DBusAuthCommand command,
242 const DBusString *args);
244 static const DBusAuthStateData server_state_waiting_for_auth = {
245 "WaitingForAuth", handle_server_state_waiting_for_auth
247 static const DBusAuthStateData server_state_waiting_for_data = {
248 "WaitingForData", handle_server_state_waiting_for_data
250 static const DBusAuthStateData server_state_waiting_for_begin = {
251 "WaitingForBegin", handle_server_state_waiting_for_begin
258 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
259 DBusAuthCommand command,
260 const DBusString *args);
261 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
262 DBusAuthCommand command,
263 const DBusString *args);
264 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
265 DBusAuthCommand command,
266 const DBusString *args);
268 static const DBusAuthStateData client_state_need_send_auth = {
271 static const DBusAuthStateData client_state_waiting_for_data = {
272 "WaitingForData", handle_client_state_waiting_for_data
274 static const DBusAuthStateData client_state_waiting_for_ok = {
275 "WaitingForOK", handle_client_state_waiting_for_ok
277 static const DBusAuthStateData client_state_waiting_for_reject = {
278 "WaitingForReject", handle_client_state_waiting_for_reject
282 * Common terminal states. Terminal states have handler == NULL.
285 static const DBusAuthStateData common_state_authenticated = {
286 "Authenticated", NULL
289 static const DBusAuthStateData common_state_need_disconnect = {
290 "NeedDisconnect", NULL
293 static const char auth_side_client[] = "client";
294 static const char auth_side_server[] = "server";
296 * @param auth the auth conversation
297 * @returns #TRUE if the conversation is the server side
299 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
301 * @param auth the auth conversation
302 * @returns #TRUE if the conversation is the client side
304 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
306 * @param auth the auth conversation
307 * @returns auth cast to DBusAuthClient
309 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
311 * @param auth the auth conversation
312 * @returns auth cast to DBusAuthServer
314 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
317 * The name of the auth ("client" or "server")
318 * @param auth the auth conversation
321 #define DBUS_AUTH_NAME(auth) ((auth)->side)
324 _dbus_auth_new (int size)
328 auth = dbus_malloc0 (size);
334 auth->keyring = NULL;
335 auth->cookie_id = -1;
337 /* note that we don't use the max string length feature,
338 * because you can't use that feature if you're going to
339 * try to recover from out-of-memory (it creates
340 * what looks like unrecoverable inability to alloc
341 * more space in the string). But we do handle
342 * overlong buffers in _dbus_auth_do_work().
345 if (!_dbus_string_init (&auth->incoming))
348 if (!_dbus_string_init (&auth->outgoing))
351 if (!_dbus_string_init (&auth->identity))
354 if (!_dbus_string_init (&auth->context))
357 if (!_dbus_string_init (&auth->challenge))
360 /* default context if none is specified */
361 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
364 auth->credentials = _dbus_credentials_new ();
365 if (auth->credentials == NULL)
368 auth->authorized_identity = _dbus_credentials_new ();
369 if (auth->authorized_identity == NULL)
372 auth->desired_identity = _dbus_credentials_new ();
373 if (auth->desired_identity == NULL)
380 _dbus_credentials_unref (auth->desired_identity);
383 _dbus_credentials_unref (auth->authorized_identity);
385 _dbus_credentials_unref (auth->credentials);
387 /* last alloc was an append to context, which is freed already below */ ;
389 _dbus_string_free (&auth->challenge);
391 _dbus_string_free (&auth->context);
393 _dbus_string_free (&auth->identity);
395 _dbus_string_free (&auth->outgoing);
397 _dbus_string_free (&auth->incoming);
404 shutdown_mech (DBusAuth *auth)
406 /* Cancel any auth */
407 auth->already_asked_for_initial_response = FALSE;
408 _dbus_string_set_length (&auth->identity, 0);
410 _dbus_credentials_clear (auth->authorized_identity);
411 _dbus_credentials_clear (auth->desired_identity);
413 if (auth->mech != NULL)
415 _dbus_verbose ("%s: Shutting down mechanism %s\n",
416 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
418 if (DBUS_AUTH_IS_CLIENT (auth))
419 (* auth->mech->client_shutdown_func) (auth);
421 (* auth->mech->server_shutdown_func) (auth);
428 * DBUS_COOKIE_SHA1 mechanism
431 /* Returns TRUE but with an empty string hash if the
432 * cookie_id isn't known. As with all this code
433 * TRUE just means we had enough memory.
436 sha1_compute_hash (DBusAuth *auth,
438 const DBusString *server_challenge,
439 const DBusString *client_challenge,
446 _dbus_assert (auth->keyring != NULL);
450 if (!_dbus_string_init (&cookie))
453 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
457 if (_dbus_string_get_length (&cookie) == 0)
463 if (!_dbus_string_init (&to_hash))
466 if (!_dbus_string_copy (server_challenge, 0,
467 &to_hash, _dbus_string_get_length (&to_hash)))
470 if (!_dbus_string_append (&to_hash, ":"))
473 if (!_dbus_string_copy (client_challenge, 0,
474 &to_hash, _dbus_string_get_length (&to_hash)))
477 if (!_dbus_string_append (&to_hash, ":"))
480 if (!_dbus_string_copy (&cookie, 0,
481 &to_hash, _dbus_string_get_length (&to_hash)))
484 if (!_dbus_sha_compute (&to_hash, hash))
490 _dbus_string_zero (&to_hash);
491 _dbus_string_free (&to_hash);
493 _dbus_string_zero (&cookie);
494 _dbus_string_free (&cookie);
498 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
499 * entropy, we use 128. This is the number of bytes in the random
502 #define N_CHALLENGE_BYTES (128/8)
505 sha1_handle_first_client_response (DBusAuth *auth,
506 const DBusString *data)
508 /* We haven't sent a challenge yet, we're expecting a desired
509 * username from the client.
518 _dbus_string_set_length (&auth->challenge, 0);
520 if (_dbus_string_get_length (data) > 0)
522 if (_dbus_string_get_length (&auth->identity) > 0)
524 /* Tried to send two auth identities, wtf */
525 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
526 DBUS_AUTH_NAME (auth));
527 return send_rejected (auth);
531 /* this is our auth identity */
532 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
537 if (!_dbus_credentials_add_from_user (auth->desired_identity, data))
539 _dbus_verbose ("%s: Did not get a valid username from client\n",
540 DBUS_AUTH_NAME (auth));
541 return send_rejected (auth);
544 if (!_dbus_string_init (&tmp))
547 if (!_dbus_string_init (&tmp2))
549 _dbus_string_free (&tmp);
553 /* we cache the keyring for speed, so here we drop it if it's the
554 * wrong one. FIXME caching the keyring here is useless since we use
555 * a different DBusAuth for every connection.
558 !_dbus_keyring_is_for_credentials (auth->keyring,
559 auth->desired_identity))
561 _dbus_keyring_unref (auth->keyring);
562 auth->keyring = NULL;
565 if (auth->keyring == NULL)
567 dbus_error_init (&error);
568 auth->keyring = _dbus_keyring_new_for_credentials (auth->desired_identity,
572 if (auth->keyring == NULL)
574 if (dbus_error_has_name (&error,
575 DBUS_ERROR_NO_MEMORY))
577 dbus_error_free (&error);
582 _DBUS_ASSERT_ERROR_IS_SET (&error);
583 _dbus_verbose ("%s: Error loading keyring: %s\n",
584 DBUS_AUTH_NAME (auth), error.message);
585 if (send_rejected (auth))
586 retval = TRUE; /* retval is only about mem */
587 dbus_error_free (&error);
593 _dbus_assert (!dbus_error_is_set (&error));
597 _dbus_assert (auth->keyring != NULL);
599 dbus_error_init (&error);
600 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
601 if (auth->cookie_id < 0)
603 _DBUS_ASSERT_ERROR_IS_SET (&error);
604 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
605 DBUS_AUTH_NAME (auth), error.message);
606 if (send_rejected (auth))
608 dbus_error_free (&error);
613 _dbus_assert (!dbus_error_is_set (&error));
616 if (!_dbus_string_copy (&auth->context, 0,
617 &tmp2, _dbus_string_get_length (&tmp2)))
620 if (!_dbus_string_append (&tmp2, " "))
623 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
626 if (!_dbus_string_append (&tmp2, " "))
629 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
632 _dbus_string_set_length (&auth->challenge, 0);
633 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
636 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
637 _dbus_string_get_length (&tmp2)))
640 if (!send_data (auth, &tmp2))
643 goto_state (auth, &server_state_waiting_for_data);
647 _dbus_string_zero (&tmp);
648 _dbus_string_free (&tmp);
649 _dbus_string_zero (&tmp2);
650 _dbus_string_free (&tmp2);
656 sha1_handle_second_client_response (DBusAuth *auth,
657 const DBusString *data)
659 /* We are expecting a response which is the hex-encoded client
660 * challenge, space, then SHA-1 hash of the concatenation of our
661 * challenge, ":", client challenge, ":", secret key, all
665 DBusString client_challenge;
666 DBusString client_hash;
668 DBusString correct_hash;
672 if (!_dbus_string_find_blank (data, 0, &i))
674 _dbus_verbose ("%s: no space separator in client response\n",
675 DBUS_AUTH_NAME (auth));
676 return send_rejected (auth);
679 if (!_dbus_string_init (&client_challenge))
682 if (!_dbus_string_init (&client_hash))
685 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
689 _dbus_string_skip_blank (data, i, &i);
691 if (!_dbus_string_copy_len (data, i,
692 _dbus_string_get_length (data) - i,
697 if (_dbus_string_get_length (&client_challenge) == 0 ||
698 _dbus_string_get_length (&client_hash) == 0)
700 _dbus_verbose ("%s: zero-length client challenge or hash\n",
701 DBUS_AUTH_NAME (auth));
702 if (send_rejected (auth))
707 if (!_dbus_string_init (&correct_hash))
710 if (!sha1_compute_hash (auth, auth->cookie_id,
716 /* if cookie_id was invalid, then we get an empty hash */
717 if (_dbus_string_get_length (&correct_hash) == 0)
719 if (send_rejected (auth))
724 if (!_dbus_string_equal (&client_hash, &correct_hash))
726 if (send_rejected (auth))
731 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
732 auth->desired_identity))
735 /* Copy process ID from the socket credentials if it's there
737 if (!_dbus_credentials_add_credential (auth->authorized_identity,
738 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
745 _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
746 DBUS_AUTH_NAME (auth));
751 _dbus_string_zero (&correct_hash);
752 _dbus_string_free (&correct_hash);
754 _dbus_string_zero (&client_hash);
755 _dbus_string_free (&client_hash);
757 _dbus_string_free (&client_challenge);
763 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
764 const DBusString *data)
766 if (auth->cookie_id < 0)
767 return sha1_handle_first_client_response (auth, data);
769 return sha1_handle_second_client_response (auth, data);
773 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
775 auth->cookie_id = -1;
776 _dbus_string_set_length (&auth->challenge, 0);
780 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
781 DBusString *response)
788 if (!_dbus_string_init (&username))
791 if (!_dbus_append_user_from_current_process (&username))
794 if (!_dbus_string_hex_encode (&username, 0,
796 _dbus_string_get_length (response)))
802 _dbus_string_free (&username);
808 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
809 const DBusString *data)
811 /* The data we get from the server should be the cookie context
812 * name, the cookie ID, and the server challenge, separated by
813 * spaces. We send back our challenge string and the correct hash.
817 DBusString cookie_id_str;
818 DBusString server_challenge;
819 DBusString client_challenge;
820 DBusString correct_hash;
827 if (!_dbus_string_find_blank (data, 0, &i))
829 if (send_error (auth,
830 "Server did not send context/ID/challenge properly"))
835 if (!_dbus_string_init (&context))
838 if (!_dbus_string_copy_len (data, 0, i,
842 _dbus_string_skip_blank (data, i, &i);
843 if (!_dbus_string_find_blank (data, i, &j))
845 if (send_error (auth,
846 "Server did not send context/ID/challenge properly"))
851 if (!_dbus_string_init (&cookie_id_str))
854 if (!_dbus_string_copy_len (data, i, j - i,
858 if (!_dbus_string_init (&server_challenge))
862 _dbus_string_skip_blank (data, i, &i);
863 j = _dbus_string_get_length (data);
865 if (!_dbus_string_copy_len (data, i, j - i,
866 &server_challenge, 0))
869 if (!_dbus_keyring_validate_context (&context))
871 if (send_error (auth, "Server sent invalid cookie context"))
876 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
878 if (send_error (auth, "Could not parse cookie ID as an integer"))
883 if (_dbus_string_get_length (&server_challenge) == 0)
885 if (send_error (auth, "Empty server challenge string"))
890 if (auth->keyring == NULL)
894 dbus_error_init (&error);
895 auth->keyring = _dbus_keyring_new_for_credentials (NULL,
899 if (auth->keyring == NULL)
901 if (dbus_error_has_name (&error,
902 DBUS_ERROR_NO_MEMORY))
904 dbus_error_free (&error);
909 _DBUS_ASSERT_ERROR_IS_SET (&error);
911 _dbus_verbose ("%s: Error loading keyring: %s\n",
912 DBUS_AUTH_NAME (auth), error.message);
914 if (send_error (auth, "Could not load cookie file"))
915 retval = TRUE; /* retval is only about mem */
917 dbus_error_free (&error);
923 _dbus_assert (!dbus_error_is_set (&error));
927 _dbus_assert (auth->keyring != NULL);
929 if (!_dbus_string_init (&tmp))
932 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
935 if (!_dbus_string_init (&client_challenge))
938 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
941 if (!_dbus_string_init (&correct_hash))
944 if (!sha1_compute_hash (auth, val,
950 if (_dbus_string_get_length (&correct_hash) == 0)
952 /* couldn't find the cookie ID or something */
953 if (send_error (auth, "Don't have the requested cookie ID"))
958 _dbus_string_set_length (&tmp, 0);
960 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
961 _dbus_string_get_length (&tmp)))
964 if (!_dbus_string_append (&tmp, " "))
967 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
968 _dbus_string_get_length (&tmp)))
971 if (!send_data (auth, &tmp))
977 _dbus_string_zero (&correct_hash);
978 _dbus_string_free (&correct_hash);
980 _dbus_string_free (&client_challenge);
982 _dbus_string_zero (&tmp);
983 _dbus_string_free (&tmp);
985 _dbus_string_free (&server_challenge);
987 _dbus_string_free (&cookie_id_str);
989 _dbus_string_free (&context);
995 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
997 auth->cookie_id = -1;
998 _dbus_string_set_length (&auth->challenge, 0);
1002 * EXTERNAL mechanism
1006 handle_server_data_external_mech (DBusAuth *auth,
1007 const DBusString *data)
1009 if (_dbus_credentials_are_anonymous (auth->credentials))
1011 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1012 DBUS_AUTH_NAME (auth));
1013 return send_rejected (auth);
1016 if (_dbus_string_get_length (data) > 0)
1018 if (_dbus_string_get_length (&auth->identity) > 0)
1020 /* Tried to send two auth identities, wtf */
1021 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1022 DBUS_AUTH_NAME (auth));
1023 return send_rejected (auth);
1027 /* this is our auth identity */
1028 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1033 /* Poke client for an auth identity, if none given */
1034 if (_dbus_string_get_length (&auth->identity) == 0 &&
1035 !auth->already_asked_for_initial_response)
1037 if (send_data (auth, NULL))
1039 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1040 DBUS_AUTH_NAME (auth));
1041 auth->already_asked_for_initial_response = TRUE;
1042 goto_state (auth, &server_state_waiting_for_data);
1049 _dbus_credentials_clear (auth->desired_identity);
1051 /* If auth->identity is still empty here, then client
1052 * responded with an empty string after we poked it for
1053 * an initial response. This means to try to auth the
1054 * identity provided in the credentials.
1056 if (_dbus_string_get_length (&auth->identity) == 0)
1058 if (!_dbus_credentials_add_credentials (auth->desired_identity,
1061 return FALSE; /* OOM */
1066 if (!_dbus_credentials_add_from_user (auth->desired_identity,
1069 _dbus_verbose ("%s: could not get credentials from uid string\n",
1070 DBUS_AUTH_NAME (auth));
1071 return send_rejected (auth);
1075 if (_dbus_credentials_are_anonymous (auth->desired_identity))
1077 _dbus_verbose ("%s: desired user %s is no good\n",
1078 DBUS_AUTH_NAME (auth),
1079 _dbus_string_get_const_data (&auth->identity));
1080 return send_rejected (auth);
1083 if (_dbus_credentials_are_superset (auth->credentials,
1084 auth->desired_identity))
1086 /* client has authenticated */
1087 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
1088 auth->desired_identity))
1091 /* also copy process ID from the socket credentials
1093 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1094 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1098 if (!send_ok (auth))
1101 _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1102 DBUS_AUTH_NAME (auth));
1108 _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1109 DBUS_AUTH_NAME (auth));
1110 return send_rejected (auth);
1115 handle_server_shutdown_external_mech (DBusAuth *auth)
1121 handle_client_initial_response_external_mech (DBusAuth *auth,
1122 DBusString *response)
1124 /* We always append our UID as an initial response, so the server
1125 * doesn't have to send back an empty challenge to check whether we
1126 * want to specify an identity. i.e. this avoids a round trip that
1127 * the spec for the EXTERNAL mechanism otherwise requires.
1129 DBusString plaintext;
1131 if (!_dbus_string_init (&plaintext))
1134 if (!_dbus_append_user_from_current_process (&plaintext))
1137 if (!_dbus_string_hex_encode (&plaintext, 0,
1139 _dbus_string_get_length (response)))
1142 _dbus_string_free (&plaintext);
1147 _dbus_string_free (&plaintext);
1152 handle_client_data_external_mech (DBusAuth *auth,
1153 const DBusString *data)
1160 handle_client_shutdown_external_mech (DBusAuth *auth)
1166 * ANONYMOUS mechanism
1170 handle_server_data_anonymous_mech (DBusAuth *auth,
1171 const DBusString *data)
1173 if (_dbus_string_get_length (data) > 0)
1175 /* Client is allowed to send "trace" data, the only defined
1176 * meaning is that if it contains '@' it is an email address,
1177 * and otherwise it is anything else, and it's supposed to be
1180 if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1182 _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1183 DBUS_AUTH_NAME (auth));
1186 DBusString plaintext;
1188 _dbus_string_init_const (&plaintext, "D-Bus " VERSION);
1189 _dbus_string_init (&encoded);
1190 _dbus_string_hex_encode (&plaintext, 0,
1193 _dbus_verbose ("%s: try '%s'\n",
1194 DBUS_AUTH_NAME (auth), _dbus_string_get_const_data (&encoded));
1196 return send_rejected (auth);
1199 _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1200 DBUS_AUTH_NAME (auth),
1201 _dbus_string_get_const_data (data));
1204 /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1205 _dbus_credentials_clear (auth->desired_identity);
1207 /* Copy process ID from the socket credentials
1209 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1210 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1214 /* Anonymous is always allowed */
1215 if (!send_ok (auth))
1218 _dbus_verbose ("%s: authenticated client as anonymous\n",
1219 DBUS_AUTH_NAME (auth));
1225 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1231 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1232 DBusString *response)
1234 /* Our initial response is a "trace" string which must be valid UTF-8
1235 * and must be an email address if it contains '@'.
1236 * We just send the dbus implementation info, like a user-agent or
1237 * something, because... why not. There's nothing guaranteed here
1238 * though, we could change it later.
1240 DBusString plaintext;
1242 if (!_dbus_string_init (&plaintext))
1245 if (!_dbus_string_append (&plaintext,
1246 "libdbus " VERSION))
1249 if (!_dbus_string_hex_encode (&plaintext, 0,
1251 _dbus_string_get_length (response)))
1254 _dbus_string_free (&plaintext);
1259 _dbus_string_free (&plaintext);
1264 handle_client_data_anonymous_mech (DBusAuth *auth,
1265 const DBusString *data)
1272 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1277 /* Put mechanisms here in order of preference.
1278 * Right now we have:
1280 * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1281 * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1282 * - ANONYMOUS checks nothing but doesn't auth the person as a user
1284 * We might ideally add a mechanism to chain to Cyrus SASL so we can
1285 * use its mechanisms as well.
1288 static const DBusAuthMechanismHandler
1289 all_mechanisms[] = {
1291 handle_server_data_external_mech,
1293 handle_server_shutdown_external_mech,
1294 handle_client_initial_response_external_mech,
1295 handle_client_data_external_mech,
1297 handle_client_shutdown_external_mech },
1298 { "DBUS_COOKIE_SHA1",
1299 handle_server_data_cookie_sha1_mech,
1301 handle_server_shutdown_cookie_sha1_mech,
1302 handle_client_initial_response_cookie_sha1_mech,
1303 handle_client_data_cookie_sha1_mech,
1305 handle_client_shutdown_cookie_sha1_mech },
1307 handle_server_data_anonymous_mech,
1309 handle_server_shutdown_anonymous_mech,
1310 handle_client_initial_response_anonymous_mech,
1311 handle_client_data_anonymous_mech,
1313 handle_client_shutdown_anonymous_mech },
1317 static const DBusAuthMechanismHandler*
1318 find_mech (const DBusString *name,
1319 char **allowed_mechs)
1323 if (allowed_mechs != NULL &&
1324 !_dbus_string_array_contains ((const char**) allowed_mechs,
1325 _dbus_string_get_const_data (name)))
1329 while (all_mechanisms[i].mechanism != NULL)
1331 if (_dbus_string_equal_c_str (name,
1332 all_mechanisms[i].mechanism))
1334 return &all_mechanisms[i];
1343 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1345 DBusString auth_command;
1347 if (!_dbus_string_init (&auth_command))
1350 if (!_dbus_string_append (&auth_command,
1353 _dbus_string_free (&auth_command);
1357 if (!_dbus_string_append (&auth_command,
1360 _dbus_string_free (&auth_command);
1364 if (mech->client_initial_response_func != NULL)
1366 if (!_dbus_string_append (&auth_command, " "))
1368 _dbus_string_free (&auth_command);
1372 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1374 _dbus_string_free (&auth_command);
1379 if (!_dbus_string_append (&auth_command,
1382 _dbus_string_free (&auth_command);
1386 if (!_dbus_string_copy (&auth_command, 0,
1388 _dbus_string_get_length (&auth->outgoing)))
1390 _dbus_string_free (&auth_command);
1394 _dbus_string_free (&auth_command);
1395 shutdown_mech (auth);
1397 goto_state (auth, &client_state_waiting_for_data);
1403 send_data (DBusAuth *auth, DBusString *data)
1407 if (data == NULL || _dbus_string_get_length (data) == 0)
1408 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1411 old_len = _dbus_string_get_length (&auth->outgoing);
1412 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1415 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1416 _dbus_string_get_length (&auth->outgoing)))
1419 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1425 _dbus_string_set_length (&auth->outgoing, old_len);
1432 send_rejected (DBusAuth *auth)
1435 DBusAuthServer *server_auth;
1438 if (!_dbus_string_init (&command))
1441 if (!_dbus_string_append (&command,
1446 while (all_mechanisms[i].mechanism != NULL)
1448 if (!_dbus_string_append (&command,
1452 if (!_dbus_string_append (&command,
1453 all_mechanisms[i].mechanism))
1459 if (!_dbus_string_append (&command, "\r\n"))
1462 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1463 _dbus_string_get_length (&auth->outgoing)))
1466 shutdown_mech (auth);
1468 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1469 server_auth = DBUS_AUTH_SERVER (auth);
1470 server_auth->failures += 1;
1472 if (server_auth->failures >= server_auth->max_failures)
1473 goto_state (auth, &common_state_need_disconnect);
1475 goto_state (auth, &server_state_waiting_for_auth);
1477 _dbus_string_free (&command);
1482 _dbus_string_free (&command);
1487 send_error (DBusAuth *auth, const char *message)
1489 return _dbus_string_append_printf (&auth->outgoing,
1490 "ERROR \"%s\"\r\n", message);
1494 send_ok (DBusAuth *auth)
1498 orig_len = _dbus_string_get_length (&auth->outgoing);
1500 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1501 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1504 _dbus_string_get_length (&auth->outgoing)) &&
1505 _dbus_string_append (&auth->outgoing, "\r\n"))
1507 goto_state (auth, &server_state_waiting_for_begin);
1512 _dbus_string_set_length (&auth->outgoing, orig_len);
1518 send_begin (DBusAuth *auth,
1519 const DBusString *args_from_ok)
1523 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1524 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1526 /* We decode the hex string to binary, using guid_from_server as scratch... */
1529 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1530 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1533 /* now clear out the scratch */
1534 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1536 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1539 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1540 end_of_hex, _dbus_string_get_length (args_from_ok));
1541 goto_state (auth, &common_state_need_disconnect);
1545 if (_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0) &&
1546 _dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
1548 _dbus_verbose ("Got GUID '%s' from the server\n",
1549 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1551 goto_state (auth, &common_state_authenticated);
1556 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1562 send_cancel (DBusAuth *auth)
1564 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1566 goto_state (auth, &client_state_waiting_for_reject);
1574 process_data (DBusAuth *auth,
1575 const DBusString *args,
1576 DBusAuthDataFunction data_func)
1581 if (!_dbus_string_init (&decoded))
1584 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1586 _dbus_string_free (&decoded);
1590 if (_dbus_string_get_length (args) != end)
1592 _dbus_string_free (&decoded);
1593 if (!send_error (auth, "Invalid hex encoding"))
1599 #ifdef DBUS_ENABLE_VERBOSE_MODE
1600 if (_dbus_string_validate_ascii (&decoded, 0,
1601 _dbus_string_get_length (&decoded)))
1602 _dbus_verbose ("%s: data: '%s'\n",
1603 DBUS_AUTH_NAME (auth),
1604 _dbus_string_get_const_data (&decoded));
1607 if (!(* data_func) (auth, &decoded))
1609 _dbus_string_free (&decoded);
1613 _dbus_string_free (&decoded);
1618 handle_auth (DBusAuth *auth, const DBusString *args)
1620 if (_dbus_string_get_length (args) == 0)
1622 /* No args to the auth, send mechanisms */
1623 if (!send_rejected (auth))
1632 DBusString hex_response;
1634 _dbus_string_find_blank (args, 0, &i);
1636 if (!_dbus_string_init (&mech))
1639 if (!_dbus_string_init (&hex_response))
1641 _dbus_string_free (&mech);
1645 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1648 _dbus_string_skip_blank (args, i, &i);
1649 if (!_dbus_string_copy (args, i, &hex_response, 0))
1652 auth->mech = find_mech (&mech, auth->allowed_mechs);
1653 if (auth->mech != NULL)
1655 _dbus_verbose ("%s: Trying mechanism %s\n",
1656 DBUS_AUTH_NAME (auth),
1657 auth->mech->mechanism);
1659 if (!process_data (auth, &hex_response,
1660 auth->mech->server_data_func))
1665 /* Unsupported mechanism */
1666 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1667 DBUS_AUTH_NAME (auth),
1668 _dbus_string_get_const_data (&mech));
1670 if (!send_rejected (auth))
1674 _dbus_string_free (&mech);
1675 _dbus_string_free (&hex_response);
1681 _dbus_string_free (&mech);
1682 _dbus_string_free (&hex_response);
1688 handle_server_state_waiting_for_auth (DBusAuth *auth,
1689 DBusAuthCommand command,
1690 const DBusString *args)
1694 case DBUS_AUTH_COMMAND_AUTH:
1695 return handle_auth (auth, args);
1697 case DBUS_AUTH_COMMAND_CANCEL:
1698 case DBUS_AUTH_COMMAND_DATA:
1699 return send_error (auth, "Not currently in an auth conversation");
1701 case DBUS_AUTH_COMMAND_BEGIN:
1702 goto_state (auth, &common_state_need_disconnect);
1705 case DBUS_AUTH_COMMAND_ERROR:
1706 return send_rejected (auth);
1708 case DBUS_AUTH_COMMAND_REJECTED:
1709 case DBUS_AUTH_COMMAND_OK:
1710 case DBUS_AUTH_COMMAND_UNKNOWN:
1712 return send_error (auth, "Unknown command");
1717 handle_server_state_waiting_for_data (DBusAuth *auth,
1718 DBusAuthCommand command,
1719 const DBusString *args)
1723 case DBUS_AUTH_COMMAND_AUTH:
1724 return send_error (auth, "Sent AUTH while another AUTH in progress");
1726 case DBUS_AUTH_COMMAND_CANCEL:
1727 case DBUS_AUTH_COMMAND_ERROR:
1728 return send_rejected (auth);
1730 case DBUS_AUTH_COMMAND_DATA:
1731 return process_data (auth, args, auth->mech->server_data_func);
1733 case DBUS_AUTH_COMMAND_BEGIN:
1734 goto_state (auth, &common_state_need_disconnect);
1737 case DBUS_AUTH_COMMAND_REJECTED:
1738 case DBUS_AUTH_COMMAND_OK:
1739 case DBUS_AUTH_COMMAND_UNKNOWN:
1741 return send_error (auth, "Unknown command");
1746 handle_server_state_waiting_for_begin (DBusAuth *auth,
1747 DBusAuthCommand command,
1748 const DBusString *args)
1752 case DBUS_AUTH_COMMAND_AUTH:
1753 return send_error (auth, "Sent AUTH while expecting BEGIN");
1755 case DBUS_AUTH_COMMAND_DATA:
1756 return send_error (auth, "Sent DATA while expecting BEGIN");
1758 case DBUS_AUTH_COMMAND_BEGIN:
1759 goto_state (auth, &common_state_authenticated);
1762 case DBUS_AUTH_COMMAND_REJECTED:
1763 case DBUS_AUTH_COMMAND_OK:
1764 case DBUS_AUTH_COMMAND_UNKNOWN:
1766 return send_error (auth, "Unknown command");
1768 case DBUS_AUTH_COMMAND_CANCEL:
1769 case DBUS_AUTH_COMMAND_ERROR:
1770 return send_rejected (auth);
1774 /* return FALSE if no memory, TRUE if all OK */
1776 get_word (const DBusString *str,
1782 _dbus_string_skip_blank (str, *start, start);
1783 _dbus_string_find_blank (str, *start, &i);
1787 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1797 record_mechanisms (DBusAuth *auth,
1798 const DBusString *args)
1803 if (auth->already_got_mechanisms)
1806 len = _dbus_string_get_length (args);
1812 const DBusAuthMechanismHandler *mech;
1814 if (!_dbus_string_init (&m))
1817 if (!get_word (args, &next, &m))
1819 _dbus_string_free (&m);
1823 mech = find_mech (&m, auth->allowed_mechs);
1827 /* FIXME right now we try mechanisms in the order
1828 * the server lists them; should we do them in
1829 * some more deterministic order?
1831 * Probably in all_mechanisms order, our order of
1832 * preference. Of course when the server is us,
1833 * it lists things in that order anyhow.
1836 if (mech != &all_mechanisms[0])
1838 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1839 DBUS_AUTH_NAME (auth), mech->mechanism);
1841 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1844 _dbus_string_free (&m);
1850 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1851 DBUS_AUTH_NAME (auth), mech->mechanism);
1856 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1857 DBUS_AUTH_NAME (auth),
1858 _dbus_string_get_const_data (&m));
1861 _dbus_string_free (&m);
1864 auth->already_got_mechanisms = TRUE;
1869 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1875 process_rejected (DBusAuth *auth, const DBusString *args)
1877 const DBusAuthMechanismHandler *mech;
1878 DBusAuthClient *client;
1880 client = DBUS_AUTH_CLIENT (auth);
1882 if (!auth->already_got_mechanisms)
1884 if (!record_mechanisms (auth, args))
1888 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1890 mech = client->mechs_to_try->data;
1892 if (!send_auth (auth, mech))
1895 _dbus_list_pop_first (&client->mechs_to_try);
1897 _dbus_verbose ("%s: Trying mechanism %s\n",
1898 DBUS_AUTH_NAME (auth),
1904 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1905 DBUS_AUTH_NAME (auth));
1906 goto_state (auth, &common_state_need_disconnect);
1914 handle_client_state_waiting_for_data (DBusAuth *auth,
1915 DBusAuthCommand command,
1916 const DBusString *args)
1918 _dbus_assert (auth->mech != NULL);
1922 case DBUS_AUTH_COMMAND_DATA:
1923 return process_data (auth, args, auth->mech->client_data_func);
1925 case DBUS_AUTH_COMMAND_REJECTED:
1926 return process_rejected (auth, args);
1928 case DBUS_AUTH_COMMAND_OK:
1929 return send_begin (auth, args);
1931 case DBUS_AUTH_COMMAND_ERROR:
1932 return send_cancel (auth);
1934 case DBUS_AUTH_COMMAND_AUTH:
1935 case DBUS_AUTH_COMMAND_CANCEL:
1936 case DBUS_AUTH_COMMAND_BEGIN:
1937 case DBUS_AUTH_COMMAND_UNKNOWN:
1939 return send_error (auth, "Unknown command");
1944 handle_client_state_waiting_for_ok (DBusAuth *auth,
1945 DBusAuthCommand command,
1946 const DBusString *args)
1950 case DBUS_AUTH_COMMAND_REJECTED:
1951 return process_rejected (auth, args);
1953 case DBUS_AUTH_COMMAND_OK:
1954 return send_begin (auth, args);
1956 case DBUS_AUTH_COMMAND_DATA:
1957 case DBUS_AUTH_COMMAND_ERROR:
1958 return send_cancel (auth);
1960 case DBUS_AUTH_COMMAND_AUTH:
1961 case DBUS_AUTH_COMMAND_CANCEL:
1962 case DBUS_AUTH_COMMAND_BEGIN:
1963 case DBUS_AUTH_COMMAND_UNKNOWN:
1965 return send_error (auth, "Unknown command");
1970 handle_client_state_waiting_for_reject (DBusAuth *auth,
1971 DBusAuthCommand command,
1972 const DBusString *args)
1976 case DBUS_AUTH_COMMAND_REJECTED:
1977 return process_rejected (auth, args);
1979 case DBUS_AUTH_COMMAND_AUTH:
1980 case DBUS_AUTH_COMMAND_CANCEL:
1981 case DBUS_AUTH_COMMAND_DATA:
1982 case DBUS_AUTH_COMMAND_BEGIN:
1983 case DBUS_AUTH_COMMAND_OK:
1984 case DBUS_AUTH_COMMAND_ERROR:
1985 case DBUS_AUTH_COMMAND_UNKNOWN:
1987 goto_state (auth, &common_state_need_disconnect);
1993 * Mapping from command name to enum
1996 const char *name; /**< Name of the command */
1997 DBusAuthCommand command; /**< Corresponding enum */
1998 } DBusAuthCommandName;
2000 static const DBusAuthCommandName auth_command_names[] = {
2001 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2002 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2003 { "DATA", DBUS_AUTH_COMMAND_DATA },
2004 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2005 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2006 { "OK", DBUS_AUTH_COMMAND_OK },
2007 { "ERROR", DBUS_AUTH_COMMAND_ERROR }
2010 static DBusAuthCommand
2011 lookup_command_from_name (DBusString *command)
2015 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2017 if (_dbus_string_equal_c_str (command,
2018 auth_command_names[i].name))
2019 return auth_command_names[i].command;
2022 return DBUS_AUTH_COMMAND_UNKNOWN;
2026 goto_state (DBusAuth *auth,
2027 const DBusAuthStateData *state)
2029 _dbus_verbose ("%s: going from state %s to state %s\n",
2030 DBUS_AUTH_NAME (auth),
2034 auth->state = state;
2037 /* returns whether to call it again right away */
2039 process_command (DBusAuth *auth)
2041 DBusAuthCommand command;
2048 /* _dbus_verbose ("%s: trying process_command()\n"); */
2053 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2056 if (!_dbus_string_init (&line))
2058 auth->needed_memory = TRUE;
2062 if (!_dbus_string_init (&args))
2064 _dbus_string_free (&line);
2065 auth->needed_memory = TRUE;
2069 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2072 if (!_dbus_string_validate_ascii (&line, 0,
2073 _dbus_string_get_length (&line)))
2075 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2076 DBUS_AUTH_NAME (auth));
2077 if (!send_error (auth, "Command contained non-ASCII"))
2083 _dbus_verbose ("%s: got command \"%s\"\n",
2084 DBUS_AUTH_NAME (auth),
2085 _dbus_string_get_const_data (&line));
2087 _dbus_string_find_blank (&line, 0, &i);
2088 _dbus_string_skip_blank (&line, i, &j);
2091 _dbus_string_delete (&line, i, j - i);
2093 if (!_dbus_string_move (&line, i, &args, 0))
2096 /* FIXME 1.0 we should probably validate that only the allowed
2097 * chars are in the command name
2100 command = lookup_command_from_name (&line);
2101 if (!(* auth->state->handler) (auth, command, &args))
2106 /* We've succeeded in processing the whole command so drop it out
2107 * of the incoming buffer and return TRUE to try another command.
2110 _dbus_string_delete (&auth->incoming, 0, eol);
2113 _dbus_string_delete (&auth->incoming, 0, 2);
2118 _dbus_string_free (&args);
2119 _dbus_string_free (&line);
2122 auth->needed_memory = TRUE;
2124 auth->needed_memory = FALSE;
2133 * @addtogroup DBusAuth
2138 * Creates a new auth conversation object for the server side.
2139 * See doc/dbus-sasl-profile.txt for full details on what
2142 * @returns the new object or #NULL if no memory
2145 _dbus_auth_server_new (const DBusString *guid)
2148 DBusAuthServer *server_auth;
2149 DBusString guid_copy;
2151 if (!_dbus_string_init (&guid_copy))
2154 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2156 _dbus_string_free (&guid_copy);
2160 auth = _dbus_auth_new (sizeof (DBusAuthServer));
2163 _dbus_string_free (&guid_copy);
2167 auth->side = auth_side_server;
2168 auth->state = &server_state_waiting_for_auth;
2170 server_auth = DBUS_AUTH_SERVER (auth);
2172 server_auth->guid = guid_copy;
2174 /* perhaps this should be per-mechanism with a lower
2177 server_auth->failures = 0;
2178 server_auth->max_failures = 6;
2184 * Creates a new auth conversation object for the client side.
2185 * See doc/dbus-sasl-profile.txt for full details on what
2188 * @returns the new object or #NULL if no memory
2191 _dbus_auth_client_new (void)
2194 DBusString guid_str;
2196 if (!_dbus_string_init (&guid_str))
2199 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2202 _dbus_string_free (&guid_str);
2206 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2208 auth->side = auth_side_client;
2209 auth->state = &client_state_need_send_auth;
2211 /* Start the auth conversation by sending AUTH for our default
2213 if (!send_auth (auth, &all_mechanisms[0]))
2215 _dbus_auth_unref (auth);
2223 * Increments the refcount of an auth object.
2225 * @param auth the auth conversation
2226 * @returns the auth conversation
2229 _dbus_auth_ref (DBusAuth *auth)
2231 _dbus_assert (auth != NULL);
2233 auth->refcount += 1;
2239 * Decrements the refcount of an auth object.
2241 * @param auth the auth conversation
2244 _dbus_auth_unref (DBusAuth *auth)
2246 _dbus_assert (auth != NULL);
2247 _dbus_assert (auth->refcount > 0);
2249 auth->refcount -= 1;
2250 if (auth->refcount == 0)
2252 shutdown_mech (auth);
2254 if (DBUS_AUTH_IS_CLIENT (auth))
2256 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2257 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2261 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2263 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2267 _dbus_keyring_unref (auth->keyring);
2269 _dbus_string_free (&auth->context);
2270 _dbus_string_free (&auth->challenge);
2271 _dbus_string_free (&auth->identity);
2272 _dbus_string_free (&auth->incoming);
2273 _dbus_string_free (&auth->outgoing);
2275 dbus_free_string_array (auth->allowed_mechs);
2277 _dbus_credentials_unref (auth->credentials);
2278 _dbus_credentials_unref (auth->authorized_identity);
2279 _dbus_credentials_unref (auth->desired_identity);
2286 * Sets an array of authentication mechanism names
2287 * that we are willing to use.
2289 * @param auth the auth conversation
2290 * @param mechanisms #NULL-terminated array of mechanism names
2291 * @returns #FALSE if no memory
2294 _dbus_auth_set_mechanisms (DBusAuth *auth,
2295 const char **mechanisms)
2299 if (mechanisms != NULL)
2301 copy = _dbus_dup_string_array (mechanisms);
2308 dbus_free_string_array (auth->allowed_mechs);
2310 auth->allowed_mechs = copy;
2316 * @param auth the auth conversation object
2317 * @returns #TRUE if we're in a final state
2319 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2322 * Analyzes buffered input and moves the auth conversation forward,
2323 * returning the new state of the auth conversation.
2325 * @param auth the auth conversation
2326 * @returns the new state
2329 _dbus_auth_do_work (DBusAuth *auth)
2331 auth->needed_memory = FALSE;
2333 /* Max amount we'll buffer up before deciding someone's on crack */
2334 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2338 if (DBUS_AUTH_IN_END_STATE (auth))
2341 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2342 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2344 goto_state (auth, &common_state_need_disconnect);
2345 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2346 DBUS_AUTH_NAME (auth));
2350 while (process_command (auth));
2352 if (auth->needed_memory)
2353 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2354 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2355 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2356 else if (auth->state == &common_state_need_disconnect)
2357 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2358 else if (auth->state == &common_state_authenticated)
2359 return DBUS_AUTH_STATE_AUTHENTICATED;
2360 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2364 * Gets bytes that need to be sent to the peer we're conversing with.
2365 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2366 * to notify the auth object that they were written.
2368 * @param auth the auth conversation
2369 * @param str return location for a ref to the buffer to send
2370 * @returns #FALSE if nothing to send
2373 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2374 const DBusString **str)
2376 _dbus_assert (auth != NULL);
2377 _dbus_assert (str != NULL);
2381 if (_dbus_string_get_length (&auth->outgoing) == 0)
2384 *str = &auth->outgoing;
2390 * Notifies the auth conversation object that
2391 * the given number of bytes of the outgoing buffer
2392 * have been written out.
2394 * @param auth the auth conversation
2395 * @param bytes_sent number of bytes written out
2398 _dbus_auth_bytes_sent (DBusAuth *auth,
2401 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2402 DBUS_AUTH_NAME (auth),
2404 _dbus_string_get_const_data (&auth->outgoing));
2406 _dbus_string_delete (&auth->outgoing,
2411 * Get a buffer to be used for reading bytes from the peer we're conversing
2412 * with. Bytes should be appended to this buffer.
2414 * @param auth the auth conversation
2415 * @param buffer return location for buffer to append bytes to
2418 _dbus_auth_get_buffer (DBusAuth *auth,
2419 DBusString **buffer)
2421 _dbus_assert (auth != NULL);
2422 _dbus_assert (!auth->buffer_outstanding);
2424 *buffer = &auth->incoming;
2426 auth->buffer_outstanding = TRUE;
2430 * Returns a buffer with new data read into it.
2432 * @param auth the auth conversation
2433 * @param buffer the buffer being returned
2434 * @param bytes_read number of new bytes added
2437 _dbus_auth_return_buffer (DBusAuth *auth,
2441 _dbus_assert (buffer == &auth->incoming);
2442 _dbus_assert (auth->buffer_outstanding);
2444 auth->buffer_outstanding = FALSE;
2448 * Returns leftover bytes that were not used as part of the auth
2449 * conversation. These bytes will be part of the message stream
2450 * instead. This function may not be called until authentication has
2453 * @param auth the auth conversation
2454 * @param str return location for pointer to string of unused bytes
2457 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2458 const DBusString **str)
2460 if (!DBUS_AUTH_IN_END_STATE (auth))
2463 *str = &auth->incoming;
2468 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2469 * after we've gotten them and successfully moved them elsewhere.
2471 * @param auth the auth conversation
2474 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2476 if (!DBUS_AUTH_IN_END_STATE (auth))
2479 _dbus_string_set_length (&auth->incoming, 0);
2483 * Called post-authentication, indicates whether we need to encode
2484 * the message stream with _dbus_auth_encode_data() prior to
2485 * sending it to the peer.
2487 * @param auth the auth conversation
2488 * @returns #TRUE if we need to encode the stream
2491 _dbus_auth_needs_encoding (DBusAuth *auth)
2493 if (auth->state != &common_state_authenticated)
2496 if (auth->mech != NULL)
2498 if (DBUS_AUTH_IS_CLIENT (auth))
2499 return auth->mech->client_encode_func != NULL;
2501 return auth->mech->server_encode_func != NULL;
2508 * Called post-authentication, encodes a block of bytes for sending to
2509 * the peer. If no encoding was negotiated, just copies the bytes
2510 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2512 * @param auth the auth conversation
2513 * @param plaintext the plain text data
2514 * @param encoded initialized string to where encoded data is appended
2515 * @returns #TRUE if we had enough memory and successfully encoded
2518 _dbus_auth_encode_data (DBusAuth *auth,
2519 const DBusString *plaintext,
2520 DBusString *encoded)
2522 _dbus_assert (plaintext != encoded);
2524 if (auth->state != &common_state_authenticated)
2527 if (_dbus_auth_needs_encoding (auth))
2529 if (DBUS_AUTH_IS_CLIENT (auth))
2530 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2532 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2536 return _dbus_string_copy (plaintext, 0, encoded,
2537 _dbus_string_get_length (encoded));
2542 * Called post-authentication, indicates whether we need to decode
2543 * the message stream with _dbus_auth_decode_data() after
2544 * receiving it from the peer.
2546 * @param auth the auth conversation
2547 * @returns #TRUE if we need to encode the stream
2550 _dbus_auth_needs_decoding (DBusAuth *auth)
2552 if (auth->state != &common_state_authenticated)
2555 if (auth->mech != NULL)
2557 if (DBUS_AUTH_IS_CLIENT (auth))
2558 return auth->mech->client_decode_func != NULL;
2560 return auth->mech->server_decode_func != NULL;
2568 * Called post-authentication, decodes a block of bytes received from
2569 * the peer. If no encoding was negotiated, just copies the bytes (you
2570 * can avoid this by checking _dbus_auth_needs_decoding()).
2572 * @todo 1.0? We need to be able to distinguish "out of memory" error
2573 * from "the data is hosed" error.
2575 * @param auth the auth conversation
2576 * @param encoded the encoded data
2577 * @param plaintext initialized string where decoded data is appended
2578 * @returns #TRUE if we had enough memory and successfully decoded
2581 _dbus_auth_decode_data (DBusAuth *auth,
2582 const DBusString *encoded,
2583 DBusString *plaintext)
2585 _dbus_assert (plaintext != encoded);
2587 if (auth->state != &common_state_authenticated)
2590 if (_dbus_auth_needs_decoding (auth))
2592 if (DBUS_AUTH_IS_CLIENT (auth))
2593 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2595 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2599 return _dbus_string_copy (encoded, 0, plaintext,
2600 _dbus_string_get_length (plaintext));
2605 * Sets credentials received via reliable means from the operating
2608 * @param auth the auth conversation
2609 * @param credentials the credentials received
2610 * @returns #FALSE on OOM
2613 _dbus_auth_set_credentials (DBusAuth *auth,
2614 DBusCredentials *credentials)
2616 _dbus_credentials_clear (auth->credentials);
2617 return _dbus_credentials_add_credentials (auth->credentials,
2622 * Gets the identity we authorized the client as. Apps may have
2623 * different policies as to what identities they allow.
2625 * Returned credentials are not a copy and should not be modified
2627 * @param auth the auth conversation
2628 * @returns the credentials we've authorized BY REFERENCE do not modify
2631 _dbus_auth_get_identity (DBusAuth *auth)
2633 if (auth->state == &common_state_authenticated)
2635 return auth->authorized_identity;
2639 /* FIXME instead of this, keep an empty credential around that
2640 * doesn't require allocation or something
2642 /* return empty credentials */
2643 _dbus_assert (_dbus_credentials_are_empty (auth->authorized_identity));
2644 return auth->authorized_identity;
2649 * Gets the GUID from the server if we've authenticated; gets
2651 * @param auth the auth object
2652 * @returns the GUID in ASCII hex format
2655 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2657 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2659 if (auth->state == &common_state_authenticated)
2660 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2666 * Sets the "authentication context" which scopes cookies
2667 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2669 * @param auth the auth conversation
2670 * @param context the context
2671 * @returns #FALSE if no memory
2674 _dbus_auth_set_context (DBusAuth *auth,
2675 const DBusString *context)
2677 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2678 &auth->context, 0, _dbus_string_get_length (context));
2683 /* tests in dbus-auth-util.c */