1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.0
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-userdb.h"
32 * @defgroup DBusAuth Authentication
33 * @ingroup DBusInternals
34 * @brief DBusAuth object
36 * DBusAuth manages the authentication negotiation when a connection
37 * is first established, and also manage any encryption used over a
40 * @todo some SASL profiles require sending the empty string as a
41 * challenge/response, but we don't currently allow that in our
44 * @todo DBusAuth really needs to be rewritten as an explicit state
45 * machine. Right now it's too hard to prove to yourself by inspection
48 * @todo right now sometimes both ends will block waiting for input
49 * from the other end, e.g. if there's an error during
52 * @todo the cookie keyring needs to be cached globally not just
53 * per-auth (which raises threadsafety issues too)
55 * @todo grep FIXME in dbus-auth.c
59 * @defgroup DBusAuthInternals Authentication implementation details
60 * @ingroup DBusInternals
61 * @brief DBusAuth implementation details
63 * Private details of authentication code.
69 * This function appends an initial client response to the given string
71 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
72 DBusString *response);
75 * This function processes a block of data received from the peer.
76 * i.e. handles a DATA command.
78 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
79 const DBusString *data);
82 * This function encodes a block of data from the peer.
84 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
85 const DBusString *data,
89 * This function decodes a block of data from the peer.
91 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
92 const DBusString *data,
96 * This function is called when the mechanism is abandoned.
98 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
101 * Virtual table representing a particular auth mechanism.
105 const char *mechanism; /**< Name of the mechanism */
106 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
107 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
108 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
109 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
110 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
111 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
112 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
113 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
114 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
115 } DBusAuthMechanismHandler;
118 * Enumeration for the known authentication commands.
121 DBUS_AUTH_COMMAND_AUTH,
122 DBUS_AUTH_COMMAND_CANCEL,
123 DBUS_AUTH_COMMAND_DATA,
124 DBUS_AUTH_COMMAND_BEGIN,
125 DBUS_AUTH_COMMAND_REJECTED,
126 DBUS_AUTH_COMMAND_OK,
127 DBUS_AUTH_COMMAND_ERROR,
128 DBUS_AUTH_COMMAND_UNKNOWN
132 * Auth state function, determines the reaction to incoming events for
133 * a particular state. Returns whether we had enough memory to
134 * complete the operation.
136 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
137 DBusAuthCommand command,
138 const DBusString *args);
141 * Information about a auth state.
145 const char *name; /**< Name of the state */
146 DBusAuthStateFunction handler; /**< State function for this state */
150 * Internal members of DBusAuth.
154 int refcount; /**< reference count */
155 const char *side; /**< Client or server */
157 DBusString incoming; /**< Incoming data buffer */
158 DBusString outgoing; /**< Outgoing data buffer */
160 const DBusAuthStateData *state; /**< Current protocol state */
162 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
164 DBusString identity; /**< Current identity we're authorizing
168 DBusCredentials credentials; /**< Credentials read from socket,
172 DBusCredentials authorized_identity; /**< Credentials that are authorized */
174 DBusCredentials desired_identity; /**< Identity client has requested */
176 DBusString context; /**< Cookie scope */
177 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
178 int cookie_id; /**< ID of cookie to use */
179 DBusString challenge; /**< Challenge sent to client */
181 char **allowed_mechs; /**< Mechanisms we're allowed to use,
182 * or #NULL if we can use any
185 unsigned int needed_memory : 1; /**< We needed memory to continue since last
186 * successful getting something done
188 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
189 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
190 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
194 * "Subclass" of DBusAuth for client side
198 DBusAuth base; /**< Parent class */
200 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
205 * "Subclass" of DBusAuth for server side.
209 DBusAuth base; /**< Parent class */
211 int failures; /**< Number of times client has been rejected */
212 int max_failures; /**< Number of times we reject before disconnect */
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 static dbus_bool_t send_cancel (DBusAuth *auth);
233 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
234 DBusAuthCommand command,
235 const DBusString *args);
236 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
237 DBusAuthCommand command,
238 const DBusString *args);
239 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
240 DBusAuthCommand command,
241 const DBusString *args);
243 static const DBusAuthStateData server_state_waiting_for_auth = {
244 "WaitingForAuth", handle_server_state_waiting_for_auth
246 static const DBusAuthStateData server_state_waiting_for_data = {
247 "WaitingForData", handle_server_state_waiting_for_data
249 static const DBusAuthStateData server_state_waiting_for_begin = {
250 "WaitingForBegin", handle_server_state_waiting_for_begin
257 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
258 DBusAuthCommand command,
259 const DBusString *args);
260 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
261 DBusAuthCommand command,
262 const DBusString *args);
263 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
264 DBusAuthCommand command,
265 const DBusString *args);
267 static const DBusAuthStateData client_state_need_send_auth = {
270 static const DBusAuthStateData client_state_waiting_for_data = {
271 "WaitingForData", handle_client_state_waiting_for_data
273 static const DBusAuthStateData client_state_waiting_for_ok = {
274 "WaitingForOK", handle_client_state_waiting_for_ok
276 static const DBusAuthStateData client_state_waiting_for_reject = {
277 "WaitingForReject", handle_client_state_waiting_for_reject
281 * Common terminal states. Terminal states have handler == NULL.
284 static const DBusAuthStateData common_state_authenticated = {
285 "Authenticated", NULL
288 static const DBusAuthStateData common_state_need_disconnect = {
289 "NeedDisconnect", NULL
292 static const char auth_side_client[] = "client";
293 static const char auth_side_server[] = "server";
295 * @param auth the auth conversation
296 * @returns #TRUE if the conversation is the server side
298 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
300 * @param auth the auth conversation
301 * @returns #TRUE if the conversation is the client side
303 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
305 * @param auth the auth conversation
306 * @returns auth cast to DBusAuthClient
308 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
310 * @param auth the auth conversation
311 * @returns auth cast to DBusAuthServer
313 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
316 * The name of the auth ("client" or "server")
317 * @param auth the auth conversation
320 #define DBUS_AUTH_NAME(auth) ((auth)->side)
323 _dbus_auth_new (int size)
327 auth = dbus_malloc0 (size);
333 _dbus_credentials_clear (&auth->credentials);
334 _dbus_credentials_clear (&auth->authorized_identity);
335 _dbus_credentials_clear (&auth->desired_identity);
337 auth->keyring = NULL;
338 auth->cookie_id = -1;
340 /* note that we don't use the max string length feature,
341 * because you can't use that feature if you're going to
342 * try to recover from out-of-memory (it creates
343 * what looks like unrecoverable inability to alloc
344 * more space in the string). But we do handle
345 * overlong buffers in _dbus_auth_do_work().
348 if (!_dbus_string_init (&auth->incoming))
351 if (!_dbus_string_init (&auth->outgoing))
354 if (!_dbus_string_init (&auth->identity))
357 if (!_dbus_string_init (&auth->context))
360 if (!_dbus_string_init (&auth->challenge))
363 /* default context if none is specified */
364 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
370 _dbus_string_free (&auth->challenge);
372 _dbus_string_free (&auth->context);
374 _dbus_string_free (&auth->identity);
376 _dbus_string_free (&auth->outgoing);
378 _dbus_string_free (&auth->incoming);
385 shutdown_mech (DBusAuth *auth)
387 /* Cancel any auth */
388 auth->already_asked_for_initial_response = FALSE;
389 _dbus_string_set_length (&auth->identity, 0);
391 _dbus_credentials_clear (&auth->authorized_identity);
392 _dbus_credentials_clear (&auth->desired_identity);
394 if (auth->mech != NULL)
396 _dbus_verbose ("%s: Shutting down mechanism %s\n",
397 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
399 if (DBUS_AUTH_IS_CLIENT (auth))
400 (* auth->mech->client_shutdown_func) (auth);
402 (* auth->mech->server_shutdown_func) (auth);
408 /* Returns TRUE but with an empty string hash if the
409 * cookie_id isn't known. As with all this code
410 * TRUE just means we had enough memory.
413 sha1_compute_hash (DBusAuth *auth,
415 const DBusString *server_challenge,
416 const DBusString *client_challenge,
423 _dbus_assert (auth->keyring != NULL);
427 if (!_dbus_string_init (&cookie))
430 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
434 if (_dbus_string_get_length (&cookie) == 0)
440 if (!_dbus_string_init (&to_hash))
443 if (!_dbus_string_copy (server_challenge, 0,
444 &to_hash, _dbus_string_get_length (&to_hash)))
447 if (!_dbus_string_append (&to_hash, ":"))
450 if (!_dbus_string_copy (client_challenge, 0,
451 &to_hash, _dbus_string_get_length (&to_hash)))
454 if (!_dbus_string_append (&to_hash, ":"))
457 if (!_dbus_string_copy (&cookie, 0,
458 &to_hash, _dbus_string_get_length (&to_hash)))
461 if (!_dbus_sha_compute (&to_hash, hash))
467 _dbus_string_zero (&to_hash);
468 _dbus_string_free (&to_hash);
470 _dbus_string_zero (&cookie);
471 _dbus_string_free (&cookie);
475 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
476 * entropy, we use 128. This is the number of bytes in the random
479 #define N_CHALLENGE_BYTES (128/8)
482 sha1_handle_first_client_response (DBusAuth *auth,
483 const DBusString *data)
485 /* We haven't sent a challenge yet, we're expecting a desired
486 * username from the client.
495 _dbus_string_set_length (&auth->challenge, 0);
497 if (_dbus_string_get_length (data) > 0)
499 if (_dbus_string_get_length (&auth->identity) > 0)
501 /* Tried to send two auth identities, wtf */
502 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
503 DBUS_AUTH_NAME (auth));
504 return send_rejected (auth);
508 /* this is our auth identity */
509 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
514 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
516 _dbus_verbose ("%s: Did not get a valid username from client\n",
517 DBUS_AUTH_NAME (auth));
518 return send_rejected (auth);
521 if (!_dbus_string_init (&tmp))
524 if (!_dbus_string_init (&tmp2))
526 _dbus_string_free (&tmp);
530 /* we cache the keyring for speed, so here we drop it if it's the
531 * wrong one. FIXME caching the keyring here is useless since we use
532 * a different DBusAuth for every connection.
535 !_dbus_keyring_is_for_user (auth->keyring,
538 _dbus_keyring_unref (auth->keyring);
539 auth->keyring = NULL;
542 if (auth->keyring == NULL)
546 dbus_error_init (&error);
547 auth->keyring = _dbus_keyring_new_homedir (data,
551 if (auth->keyring == NULL)
553 if (dbus_error_has_name (&error,
554 DBUS_ERROR_NO_MEMORY))
556 dbus_error_free (&error);
561 _DBUS_ASSERT_ERROR_IS_SET (&error);
562 _dbus_verbose ("%s: Error loading keyring: %s\n",
563 DBUS_AUTH_NAME (auth), error.message);
564 if (send_rejected (auth))
565 retval = TRUE; /* retval is only about mem */
566 dbus_error_free (&error);
572 _dbus_assert (!dbus_error_is_set (&error));
576 _dbus_assert (auth->keyring != NULL);
578 dbus_error_init (&error);
579 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
580 if (auth->cookie_id < 0)
582 _DBUS_ASSERT_ERROR_IS_SET (&error);
583 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
584 DBUS_AUTH_NAME (auth), error.message);
585 if (send_rejected (auth))
587 dbus_error_free (&error);
592 _dbus_assert (!dbus_error_is_set (&error));
595 if (!_dbus_string_copy (&auth->context, 0,
596 &tmp2, _dbus_string_get_length (&tmp2)))
599 if (!_dbus_string_append (&tmp2, " "))
602 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
605 if (!_dbus_string_append (&tmp2, " "))
608 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
611 _dbus_string_set_length (&auth->challenge, 0);
612 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
615 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
616 _dbus_string_get_length (&tmp2)))
619 if (!send_data (auth, &tmp2))
622 goto_state (auth, &server_state_waiting_for_data);
626 _dbus_string_zero (&tmp);
627 _dbus_string_free (&tmp);
628 _dbus_string_zero (&tmp2);
629 _dbus_string_free (&tmp2);
635 sha1_handle_second_client_response (DBusAuth *auth,
636 const DBusString *data)
638 /* We are expecting a response which is the hex-encoded client
639 * challenge, space, then SHA-1 hash of the concatenation of our
640 * challenge, ":", client challenge, ":", secret key, all
644 DBusString client_challenge;
645 DBusString client_hash;
647 DBusString correct_hash;
651 if (!_dbus_string_find_blank (data, 0, &i))
653 _dbus_verbose ("%s: no space separator in client response\n",
654 DBUS_AUTH_NAME (auth));
655 return send_rejected (auth);
658 if (!_dbus_string_init (&client_challenge))
661 if (!_dbus_string_init (&client_hash))
664 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
668 _dbus_string_skip_blank (data, i, &i);
670 if (!_dbus_string_copy_len (data, i,
671 _dbus_string_get_length (data) - i,
676 if (_dbus_string_get_length (&client_challenge) == 0 ||
677 _dbus_string_get_length (&client_hash) == 0)
679 _dbus_verbose ("%s: zero-length client challenge or hash\n",
680 DBUS_AUTH_NAME (auth));
681 if (send_rejected (auth))
686 if (!_dbus_string_init (&correct_hash))
689 if (!sha1_compute_hash (auth, auth->cookie_id,
695 /* if cookie_id was invalid, then we get an empty hash */
696 if (_dbus_string_get_length (&correct_hash) == 0)
698 if (send_rejected (auth))
703 if (!_dbus_string_equal (&client_hash, &correct_hash))
705 if (send_rejected (auth))
713 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
714 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
716 auth->authorized_identity = auth->desired_identity;
720 _dbus_string_zero (&correct_hash);
721 _dbus_string_free (&correct_hash);
723 _dbus_string_zero (&client_hash);
724 _dbus_string_free (&client_hash);
726 _dbus_string_free (&client_challenge);
732 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
733 const DBusString *data)
735 if (auth->cookie_id < 0)
736 return sha1_handle_first_client_response (auth, data);
738 return sha1_handle_second_client_response (auth, data);
742 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
744 auth->cookie_id = -1;
745 _dbus_string_set_length (&auth->challenge, 0);
749 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
750 DBusString *response)
752 const DBusString *username;
757 if (!_dbus_username_from_current_process (&username))
760 if (!_dbus_string_hex_encode (username, 0,
762 _dbus_string_get_length (response)))
772 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
773 const DBusString *data)
775 /* The data we get from the server should be the cookie context
776 * name, the cookie ID, and the server challenge, separated by
777 * spaces. We send back our challenge string and the correct hash.
781 DBusString cookie_id_str;
782 DBusString server_challenge;
783 DBusString client_challenge;
784 DBusString correct_hash;
791 if (!_dbus_string_find_blank (data, 0, &i))
793 if (send_error (auth,
794 "Server did not send context/ID/challenge properly"))
799 if (!_dbus_string_init (&context))
802 if (!_dbus_string_copy_len (data, 0, i,
806 _dbus_string_skip_blank (data, i, &i);
807 if (!_dbus_string_find_blank (data, i, &j))
809 if (send_error (auth,
810 "Server did not send context/ID/challenge properly"))
815 if (!_dbus_string_init (&cookie_id_str))
818 if (!_dbus_string_copy_len (data, i, j - i,
822 if (!_dbus_string_init (&server_challenge))
826 _dbus_string_skip_blank (data, i, &i);
827 j = _dbus_string_get_length (data);
829 if (!_dbus_string_copy_len (data, i, j - i,
830 &server_challenge, 0))
833 if (!_dbus_keyring_validate_context (&context))
835 if (send_error (auth, "Server sent invalid cookie context"))
840 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
842 if (send_error (auth, "Could not parse cookie ID as an integer"))
847 if (_dbus_string_get_length (&server_challenge) == 0)
849 if (send_error (auth, "Empty server challenge string"))
854 if (auth->keyring == NULL)
858 dbus_error_init (&error);
859 auth->keyring = _dbus_keyring_new_homedir (NULL,
863 if (auth->keyring == NULL)
865 if (dbus_error_has_name (&error,
866 DBUS_ERROR_NO_MEMORY))
868 dbus_error_free (&error);
873 _DBUS_ASSERT_ERROR_IS_SET (&error);
875 _dbus_verbose ("%s: Error loading keyring: %s\n",
876 DBUS_AUTH_NAME (auth), error.message);
878 if (send_error (auth, "Could not load cookie file"))
879 retval = TRUE; /* retval is only about mem */
881 dbus_error_free (&error);
887 _dbus_assert (!dbus_error_is_set (&error));
891 _dbus_assert (auth->keyring != NULL);
893 if (!_dbus_string_init (&tmp))
896 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
899 if (!_dbus_string_init (&client_challenge))
902 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
905 if (!_dbus_string_init (&correct_hash))
908 if (!sha1_compute_hash (auth, val,
914 if (_dbus_string_get_length (&correct_hash) == 0)
916 /* couldn't find the cookie ID or something */
917 if (send_error (auth, "Don't have the requested cookie ID"))
922 _dbus_string_set_length (&tmp, 0);
924 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
925 _dbus_string_get_length (&tmp)))
928 if (!_dbus_string_append (&tmp, " "))
931 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
932 _dbus_string_get_length (&tmp)))
935 if (!send_data (auth, &tmp))
941 _dbus_string_zero (&correct_hash);
942 _dbus_string_free (&correct_hash);
944 _dbus_string_free (&client_challenge);
946 _dbus_string_zero (&tmp);
947 _dbus_string_free (&tmp);
949 _dbus_string_free (&server_challenge);
951 _dbus_string_free (&cookie_id_str);
953 _dbus_string_free (&context);
959 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
961 auth->cookie_id = -1;
962 _dbus_string_set_length (&auth->challenge, 0);
966 handle_server_data_external_mech (DBusAuth *auth,
967 const DBusString *data)
969 if (auth->credentials.uid == DBUS_UID_UNSET)
971 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
972 DBUS_AUTH_NAME (auth));
973 return send_rejected (auth);
976 if (_dbus_string_get_length (data) > 0)
978 if (_dbus_string_get_length (&auth->identity) > 0)
980 /* Tried to send two auth identities, wtf */
981 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
982 DBUS_AUTH_NAME (auth));
983 return send_rejected (auth);
987 /* this is our auth identity */
988 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
993 /* Poke client for an auth identity, if none given */
994 if (_dbus_string_get_length (&auth->identity) == 0 &&
995 !auth->already_asked_for_initial_response)
997 if (send_data (auth, NULL))
999 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1000 DBUS_AUTH_NAME (auth));
1001 auth->already_asked_for_initial_response = TRUE;
1008 _dbus_credentials_clear (&auth->desired_identity);
1010 /* If auth->identity is still empty here, then client
1011 * responded with an empty string after we poked it for
1012 * an initial response. This means to try to auth the
1013 * identity provided in the credentials.
1015 if (_dbus_string_get_length (&auth->identity) == 0)
1017 auth->desired_identity.uid = auth->credentials.uid;
1021 if (!_dbus_uid_from_string (&auth->identity,
1022 &auth->desired_identity.uid))
1024 _dbus_verbose ("%s: could not get credentials from uid string\n",
1025 DBUS_AUTH_NAME (auth));
1026 return send_rejected (auth);
1030 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1032 _dbus_verbose ("%s: desired user %s is no good\n",
1033 DBUS_AUTH_NAME (auth),
1034 _dbus_string_get_const_data (&auth->identity));
1035 return send_rejected (auth);
1038 if (_dbus_credentials_match (&auth->desired_identity,
1039 &auth->credentials))
1041 /* client has authenticated */
1042 if (!send_ok (auth))
1045 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1046 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1047 DBUS_AUTH_NAME (auth),
1048 auth->desired_identity.uid,
1049 auth->credentials.uid);
1051 auth->authorized_identity.uid = auth->desired_identity.uid;
1057 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1058 " gid="DBUS_GID_FORMAT
1059 " do not allow uid="DBUS_UID_FORMAT
1060 " gid="DBUS_GID_FORMAT"\n",
1061 DBUS_AUTH_NAME (auth),
1062 auth->credentials.uid, auth->credentials.gid,
1063 auth->desired_identity.uid, auth->desired_identity.gid);
1064 return send_rejected (auth);
1069 handle_server_shutdown_external_mech (DBusAuth *auth)
1075 handle_client_initial_response_external_mech (DBusAuth *auth,
1076 DBusString *response)
1078 /* We always append our UID as an initial response, so the server
1079 * doesn't have to send back an empty challenge to check whether we
1080 * want to specify an identity. i.e. this avoids a round trip that
1081 * the spec for the EXTERNAL mechanism otherwise requires.
1083 DBusString plaintext;
1085 if (!_dbus_string_init (&plaintext))
1088 if (!_dbus_string_append_uint (&plaintext,
1092 if (!_dbus_string_hex_encode (&plaintext, 0,
1094 _dbus_string_get_length (response)))
1097 _dbus_string_free (&plaintext);
1102 _dbus_string_free (&plaintext);
1107 handle_client_data_external_mech (DBusAuth *auth,
1108 const DBusString *data)
1115 handle_client_shutdown_external_mech (DBusAuth *auth)
1120 /* Put mechanisms here in order of preference.
1121 * What I eventually want to have is:
1123 * - a mechanism that checks UNIX domain socket credentials
1124 * - a simple magic cookie mechanism like X11 or ICE
1125 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1126 * offers such as Kerberos, X509, whatever.
1129 static const DBusAuthMechanismHandler
1130 all_mechanisms[] = {
1132 handle_server_data_external_mech,
1134 handle_server_shutdown_external_mech,
1135 handle_client_initial_response_external_mech,
1136 handle_client_data_external_mech,
1138 handle_client_shutdown_external_mech },
1139 { "DBUS_COOKIE_SHA1",
1140 handle_server_data_cookie_sha1_mech,
1142 handle_server_shutdown_cookie_sha1_mech,
1143 handle_client_initial_response_cookie_sha1_mech,
1144 handle_client_data_cookie_sha1_mech,
1146 handle_client_shutdown_cookie_sha1_mech },
1150 static const DBusAuthMechanismHandler*
1151 find_mech (const DBusString *name,
1152 char **allowed_mechs)
1156 if (allowed_mechs != NULL &&
1157 !_dbus_string_array_contains ((const char**) allowed_mechs,
1158 _dbus_string_get_const_data (name)))
1162 while (all_mechanisms[i].mechanism != NULL)
1164 if (_dbus_string_equal_c_str (name,
1165 all_mechanisms[i].mechanism))
1167 return &all_mechanisms[i];
1176 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1178 DBusString auth_command;
1180 if (!_dbus_string_init (&auth_command))
1183 if (!_dbus_string_append (&auth_command,
1186 _dbus_string_free (&auth_command);
1190 if (!_dbus_string_append (&auth_command,
1193 _dbus_string_free (&auth_command);
1197 if (mech->client_initial_response_func != NULL)
1199 if (!_dbus_string_append (&auth_command, " "))
1201 _dbus_string_free (&auth_command);
1205 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1207 _dbus_string_free (&auth_command);
1212 if (!_dbus_string_append (&auth_command,
1215 _dbus_string_free (&auth_command);
1219 if (!_dbus_string_copy (&auth_command, 0,
1221 _dbus_string_get_length (&auth->outgoing)))
1223 _dbus_string_free (&auth_command);
1227 _dbus_string_free (&auth_command);
1228 shutdown_mech (auth);
1230 goto_state (auth, &client_state_waiting_for_data);
1236 send_data (DBusAuth *auth, DBusString *data)
1240 if (data == NULL || _dbus_string_get_length (data) == 0)
1241 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1244 old_len = _dbus_string_get_length (&auth->outgoing);
1245 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1248 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1249 _dbus_string_get_length (&auth->outgoing)))
1252 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1258 _dbus_string_set_length (&auth->outgoing, old_len);
1265 send_rejected (DBusAuth *auth)
1268 DBusAuthServer *server_auth;
1271 if (!_dbus_string_init (&command))
1274 if (!_dbus_string_append (&command,
1279 while (all_mechanisms[i].mechanism != NULL)
1281 if (!_dbus_string_append (&command,
1285 if (!_dbus_string_append (&command,
1286 all_mechanisms[i].mechanism))
1292 if (!_dbus_string_append (&command, "\r\n"))
1295 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1296 _dbus_string_get_length (&auth->outgoing)))
1299 shutdown_mech (auth);
1301 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1302 server_auth = DBUS_AUTH_SERVER (auth);
1303 server_auth->failures += 1;
1305 if (server_auth->failures >= server_auth->max_failures)
1306 goto_state (auth, &common_state_need_disconnect);
1308 goto_state (auth, &server_state_waiting_for_auth);
1310 _dbus_string_free (&command);
1315 _dbus_string_free (&command);
1320 send_error (DBusAuth *auth, const char *message)
1322 return _dbus_string_append_printf (&auth->outgoing,
1323 "ERROR \"%s\"\r\n", message);
1327 send_ok (DBusAuth *auth)
1329 if (_dbus_string_append (&auth->outgoing, "OK\r\n"))
1331 goto_state (auth, &server_state_waiting_for_begin);
1339 send_begin (DBusAuth *auth)
1341 if (_dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
1343 goto_state (auth, &common_state_authenticated);
1351 send_cancel (DBusAuth *auth)
1353 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1355 goto_state (auth, &client_state_waiting_for_reject);
1363 process_data (DBusAuth *auth,
1364 const DBusString *args,
1365 DBusAuthDataFunction data_func)
1370 if (!_dbus_string_init (&decoded))
1373 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1375 _dbus_string_free (&decoded);
1379 if (_dbus_string_get_length (args) != end)
1381 _dbus_string_free (&decoded);
1382 if (!send_error (auth, "Invalid hex encoding"))
1388 #ifdef DBUS_ENABLE_VERBOSE_MODE
1389 if (_dbus_string_validate_ascii (&decoded, 0,
1390 _dbus_string_get_length (&decoded)))
1391 _dbus_verbose ("%s: data: '%s'\n",
1392 DBUS_AUTH_NAME (auth),
1393 _dbus_string_get_const_data (&decoded));
1396 if (!(* data_func) (auth, &decoded))
1398 _dbus_string_free (&decoded);
1402 _dbus_string_free (&decoded);
1407 handle_auth (DBusAuth *auth, const DBusString *args)
1409 if (_dbus_string_get_length (args) == 0)
1411 /* No args to the auth, send mechanisms */
1412 if (!send_rejected (auth))
1421 DBusString hex_response;
1423 _dbus_string_find_blank (args, 0, &i);
1425 if (!_dbus_string_init (&mech))
1428 if (!_dbus_string_init (&hex_response))
1430 _dbus_string_free (&mech);
1434 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1437 _dbus_string_skip_blank (args, i, &i);
1438 if (!_dbus_string_copy (args, i, &hex_response, 0))
1441 auth->mech = find_mech (&mech, auth->allowed_mechs);
1442 if (auth->mech != NULL)
1444 _dbus_verbose ("%s: Trying mechanism %s\n",
1445 DBUS_AUTH_NAME (auth),
1446 auth->mech->mechanism);
1448 if (!process_data (auth, &hex_response,
1449 auth->mech->server_data_func))
1454 /* Unsupported mechanism */
1455 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1456 DBUS_AUTH_NAME (auth),
1457 _dbus_string_get_const_data (&mech));
1459 if (!send_rejected (auth))
1463 _dbus_string_free (&mech);
1464 _dbus_string_free (&hex_response);
1470 _dbus_string_free (&mech);
1471 _dbus_string_free (&hex_response);
1477 handle_server_state_waiting_for_auth (DBusAuth *auth,
1478 DBusAuthCommand command,
1479 const DBusString *args)
1483 case DBUS_AUTH_COMMAND_AUTH:
1484 return handle_auth (auth, args);
1486 case DBUS_AUTH_COMMAND_CANCEL:
1487 case DBUS_AUTH_COMMAND_DATA:
1488 return send_error (auth, "Not currently in an auth conversation");
1490 case DBUS_AUTH_COMMAND_BEGIN:
1491 goto_state (auth, &common_state_need_disconnect);
1494 case DBUS_AUTH_COMMAND_ERROR:
1495 return send_rejected (auth);
1497 case DBUS_AUTH_COMMAND_REJECTED:
1498 case DBUS_AUTH_COMMAND_OK:
1499 case DBUS_AUTH_COMMAND_UNKNOWN:
1501 return send_error (auth, "Unknown command");
1506 handle_server_state_waiting_for_data (DBusAuth *auth,
1507 DBusAuthCommand command,
1508 const DBusString *args)
1512 case DBUS_AUTH_COMMAND_AUTH:
1513 return send_error (auth, "Sent AUTH while another AUTH in progress");
1515 case DBUS_AUTH_COMMAND_CANCEL:
1516 case DBUS_AUTH_COMMAND_ERROR:
1517 return send_rejected (auth);
1519 case DBUS_AUTH_COMMAND_DATA:
1520 return process_data (auth, args, auth->mech->server_data_func);
1522 case DBUS_AUTH_COMMAND_BEGIN:
1523 goto_state (auth, &common_state_need_disconnect);
1526 case DBUS_AUTH_COMMAND_REJECTED:
1527 case DBUS_AUTH_COMMAND_OK:
1528 case DBUS_AUTH_COMMAND_UNKNOWN:
1530 return send_error (auth, "Unknown command");
1535 handle_server_state_waiting_for_begin (DBusAuth *auth,
1536 DBusAuthCommand command,
1537 const DBusString *args)
1541 case DBUS_AUTH_COMMAND_AUTH:
1542 return send_error (auth, "Sent AUTH while expecting BEGIN");
1544 case DBUS_AUTH_COMMAND_DATA:
1545 return send_error (auth, "Sent DATA while expecting BEGIN");
1547 case DBUS_AUTH_COMMAND_BEGIN:
1548 goto_state (auth, &common_state_authenticated);
1551 case DBUS_AUTH_COMMAND_REJECTED:
1552 case DBUS_AUTH_COMMAND_OK:
1553 case DBUS_AUTH_COMMAND_UNKNOWN:
1555 return send_error (auth, "Unknown command");
1557 case DBUS_AUTH_COMMAND_CANCEL:
1558 case DBUS_AUTH_COMMAND_ERROR:
1559 return send_rejected (auth);
1563 /* return FALSE if no memory, TRUE if all OK */
1565 get_word (const DBusString *str,
1571 _dbus_string_skip_blank (str, *start, start);
1572 _dbus_string_find_blank (str, *start, &i);
1576 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1586 record_mechanisms (DBusAuth *auth,
1587 const DBusString *args)
1592 if (auth->already_got_mechanisms)
1595 len = _dbus_string_get_length (args);
1601 const DBusAuthMechanismHandler *mech;
1603 if (!_dbus_string_init (&m))
1606 if (!get_word (args, &next, &m))
1608 _dbus_string_free (&m);
1612 mech = find_mech (&m, auth->allowed_mechs);
1616 /* FIXME right now we try mechanisms in the order
1617 * the server lists them; should we do them in
1618 * some more deterministic order?
1620 * Probably in all_mechanisms order, our order of
1621 * preference. Of course when the server is us,
1622 * it lists things in that order anyhow.
1625 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1626 DBUS_AUTH_NAME (auth), mech->mechanism);
1628 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1631 _dbus_string_free (&m);
1637 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1638 DBUS_AUTH_NAME (auth),
1639 _dbus_string_get_const_data (&m));
1642 _dbus_string_free (&m);
1645 auth->already_got_mechanisms = TRUE;
1650 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1656 process_rejected (DBusAuth *auth, const DBusString *args)
1658 const DBusAuthMechanismHandler *mech;
1659 DBusAuthClient *client;
1661 client = DBUS_AUTH_CLIENT (auth);
1663 if (!auth->already_got_mechanisms)
1665 if (!record_mechanisms (auth, args))
1669 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1671 mech = client->mechs_to_try->data;
1673 if (!send_auth (auth, mech))
1676 _dbus_list_pop_first (&client->mechs_to_try);
1678 _dbus_verbose ("%s: Trying mechanism %s\n",
1679 DBUS_AUTH_NAME (auth),
1685 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1686 DBUS_AUTH_NAME (auth));
1687 goto_state (auth, &common_state_need_disconnect);
1695 handle_client_state_waiting_for_data (DBusAuth *auth,
1696 DBusAuthCommand command,
1697 const DBusString *args)
1699 _dbus_assert (auth->mech != NULL);
1703 case DBUS_AUTH_COMMAND_DATA:
1704 return process_data (auth, args, auth->mech->client_data_func);
1706 case DBUS_AUTH_COMMAND_REJECTED:
1707 return process_rejected (auth, args);
1709 case DBUS_AUTH_COMMAND_OK:
1710 return send_begin (auth);
1712 case DBUS_AUTH_COMMAND_ERROR:
1713 return send_cancel (auth);
1715 case DBUS_AUTH_COMMAND_AUTH:
1716 case DBUS_AUTH_COMMAND_CANCEL:
1717 case DBUS_AUTH_COMMAND_BEGIN:
1718 case DBUS_AUTH_COMMAND_UNKNOWN:
1720 return send_error (auth, "Unknown command");
1725 handle_client_state_waiting_for_ok (DBusAuth *auth,
1726 DBusAuthCommand command,
1727 const DBusString *args)
1731 case DBUS_AUTH_COMMAND_REJECTED:
1732 return process_rejected (auth, args);
1734 case DBUS_AUTH_COMMAND_OK:
1735 return send_begin (auth);
1737 case DBUS_AUTH_COMMAND_DATA:
1738 case DBUS_AUTH_COMMAND_ERROR:
1739 return send_cancel (auth);
1741 case DBUS_AUTH_COMMAND_AUTH:
1742 case DBUS_AUTH_COMMAND_CANCEL:
1743 case DBUS_AUTH_COMMAND_BEGIN:
1744 case DBUS_AUTH_COMMAND_UNKNOWN:
1746 return send_error (auth, "Unknown command");
1751 handle_client_state_waiting_for_reject (DBusAuth *auth,
1752 DBusAuthCommand command,
1753 const DBusString *args)
1757 case DBUS_AUTH_COMMAND_REJECTED:
1758 return process_rejected (auth, args);
1760 case DBUS_AUTH_COMMAND_AUTH:
1761 case DBUS_AUTH_COMMAND_CANCEL:
1762 case DBUS_AUTH_COMMAND_DATA:
1763 case DBUS_AUTH_COMMAND_BEGIN:
1764 case DBUS_AUTH_COMMAND_OK:
1765 case DBUS_AUTH_COMMAND_ERROR:
1766 case DBUS_AUTH_COMMAND_UNKNOWN:
1768 goto_state (auth, &common_state_need_disconnect);
1774 * Mapping from command name to enum
1777 const char *name; /**< Name of the command */
1778 DBusAuthCommand command; /**< Corresponding enum */
1779 } DBusAuthCommandName;
1781 static DBusAuthCommandName auth_command_names[] = {
1782 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
1783 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
1784 { "DATA", DBUS_AUTH_COMMAND_DATA },
1785 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
1786 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
1787 { "OK", DBUS_AUTH_COMMAND_OK },
1788 { "ERROR", DBUS_AUTH_COMMAND_ERROR }
1791 static DBusAuthCommand
1792 lookup_command_from_name (DBusString *command)
1796 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
1798 if (_dbus_string_equal_c_str (command,
1799 auth_command_names[i].name))
1800 return auth_command_names[i].command;
1803 return DBUS_AUTH_COMMAND_UNKNOWN;
1807 goto_state (DBusAuth *auth, const DBusAuthStateData *state)
1809 _dbus_verbose ("%s: going from state %s to state %s\n",
1810 DBUS_AUTH_NAME (auth),
1814 auth->state = state;
1817 /* returns whether to call it again right away */
1819 process_command (DBusAuth *auth)
1821 DBusAuthCommand command;
1828 /* _dbus_verbose ("%s: trying process_command()\n"); */
1833 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1836 if (!_dbus_string_init (&line))
1838 auth->needed_memory = TRUE;
1842 if (!_dbus_string_init (&args))
1844 _dbus_string_free (&line);
1845 auth->needed_memory = TRUE;
1849 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
1852 if (!_dbus_string_validate_ascii (&line, 0,
1853 _dbus_string_get_length (&line)))
1855 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1856 DBUS_AUTH_NAME (auth));
1857 if (!send_error (auth, "Command contained non-ASCII"))
1863 _dbus_verbose ("%s: got command \"%s\"\n",
1864 DBUS_AUTH_NAME (auth),
1865 _dbus_string_get_const_data (&line));
1867 _dbus_string_find_blank (&line, 0, &i);
1868 _dbus_string_skip_blank (&line, i, &j);
1871 _dbus_string_delete (&line, i, j - i);
1873 if (!_dbus_string_move (&line, i, &args, 0))
1876 command = lookup_command_from_name (&line);
1877 if (!(* auth->state->handler) (auth, command, &args))
1882 /* We've succeeded in processing the whole command so drop it out
1883 * of the incoming buffer and return TRUE to try another command.
1886 _dbus_string_delete (&auth->incoming, 0, eol);
1889 _dbus_string_delete (&auth->incoming, 0, 2);
1894 _dbus_string_free (&args);
1895 _dbus_string_free (&line);
1898 auth->needed_memory = TRUE;
1900 auth->needed_memory = FALSE;
1909 * @addtogroup DBusAuth
1914 * Creates a new auth conversation object for the server side.
1915 * See doc/dbus-sasl-profile.txt for full details on what
1918 * @returns the new object or #NULL if no memory
1921 _dbus_auth_server_new (void)
1924 DBusAuthServer *server_auth;
1926 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1930 auth->side = auth_side_server;
1931 auth->state = &server_state_waiting_for_auth;
1933 server_auth = DBUS_AUTH_SERVER (auth);
1935 /* perhaps this should be per-mechanism with a lower
1938 server_auth->failures = 0;
1939 server_auth->max_failures = 6;
1945 * Creates a new auth conversation object for the client side.
1946 * See doc/dbus-sasl-profile.txt for full details on what
1949 * @returns the new object or #NULL if no memory
1952 _dbus_auth_client_new (void)
1956 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1960 auth->side = auth_side_client;
1961 auth->state = &client_state_need_send_auth;
1963 /* Start the auth conversation by sending AUTH for our default
1965 if (!send_auth (auth, &all_mechanisms[0]))
1967 _dbus_auth_unref (auth);
1975 * Increments the refcount of an auth object.
1977 * @param auth the auth conversation
1978 * @returns the auth conversation
1981 _dbus_auth_ref (DBusAuth *auth)
1983 _dbus_assert (auth != NULL);
1985 auth->refcount += 1;
1991 * Decrements the refcount of an auth object.
1993 * @param auth the auth conversation
1996 _dbus_auth_unref (DBusAuth *auth)
1998 _dbus_assert (auth != NULL);
1999 _dbus_assert (auth->refcount > 0);
2001 auth->refcount -= 1;
2002 if (auth->refcount == 0)
2004 shutdown_mech (auth);
2006 if (DBUS_AUTH_IS_CLIENT (auth))
2008 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2012 _dbus_keyring_unref (auth->keyring);
2014 _dbus_string_free (&auth->context);
2015 _dbus_string_free (&auth->challenge);
2016 _dbus_string_free (&auth->identity);
2017 _dbus_string_free (&auth->incoming);
2018 _dbus_string_free (&auth->outgoing);
2020 dbus_free_string_array (auth->allowed_mechs);
2027 * Sets an array of authentication mechanism names
2028 * that we are willing to use.
2030 * @param auth the auth conversation
2031 * @param mechanisms #NULL-terminated array of mechanism names
2032 * @returns #FALSE if no memory
2035 _dbus_auth_set_mechanisms (DBusAuth *auth,
2036 const char **mechanisms)
2040 if (mechanisms != NULL)
2042 copy = _dbus_dup_string_array (mechanisms);
2049 dbus_free_string_array (auth->allowed_mechs);
2051 auth->allowed_mechs = copy;
2057 * @param auth the auth conversation object
2058 * @returns #TRUE if we're in a final state
2060 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2063 * Analyzes buffered input and moves the auth conversation forward,
2064 * returning the new state of the auth conversation.
2066 * @param auth the auth conversation
2067 * @returns the new state
2070 _dbus_auth_do_work (DBusAuth *auth)
2072 auth->needed_memory = FALSE;
2074 /* Max amount we'll buffer up before deciding someone's on crack */
2075 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2079 if (DBUS_AUTH_IN_END_STATE (auth))
2082 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2083 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2085 goto_state (auth, &common_state_need_disconnect);
2086 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2087 DBUS_AUTH_NAME (auth));
2091 while (process_command (auth));
2093 if (auth->needed_memory)
2094 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2095 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2096 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2097 else if (auth->state == &common_state_need_disconnect)
2098 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2099 else if (auth->state == &common_state_authenticated)
2100 return DBUS_AUTH_STATE_AUTHENTICATED;
2101 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2105 * Gets bytes that need to be sent to the peer we're conversing with.
2106 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2107 * to notify the auth object that they were written.
2109 * @param auth the auth conversation
2110 * @param str return location for a ref to the buffer to send
2111 * @returns #FALSE if nothing to send
2114 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2115 const DBusString **str)
2117 _dbus_assert (auth != NULL);
2118 _dbus_assert (str != NULL);
2122 if (_dbus_string_get_length (&auth->outgoing) == 0)
2125 *str = &auth->outgoing;
2131 * Notifies the auth conversation object that
2132 * the given number of bytes of the outgoing buffer
2133 * have been written out.
2135 * @param auth the auth conversation
2136 * @param bytes_sent number of bytes written out
2139 _dbus_auth_bytes_sent (DBusAuth *auth,
2142 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2143 DBUS_AUTH_NAME (auth),
2145 _dbus_string_get_const_data (&auth->outgoing));
2147 _dbus_string_delete (&auth->outgoing,
2152 * Get a buffer to be used for reading bytes from the peer we're conversing
2153 * with. Bytes should be appended to this buffer.
2155 * @param auth the auth conversation
2156 * @param buffer return location for buffer to append bytes to
2159 _dbus_auth_get_buffer (DBusAuth *auth,
2160 DBusString **buffer)
2162 _dbus_assert (auth != NULL);
2163 _dbus_assert (!auth->buffer_outstanding);
2165 *buffer = &auth->incoming;
2167 auth->buffer_outstanding = TRUE;
2171 * Returns a buffer with new data read into it.
2173 * @param auth the auth conversation
2174 * @param buffer the buffer being returned
2175 * @param bytes_read number of new bytes added
2178 _dbus_auth_return_buffer (DBusAuth *auth,
2182 _dbus_assert (buffer == &auth->incoming);
2183 _dbus_assert (auth->buffer_outstanding);
2185 auth->buffer_outstanding = FALSE;
2189 * Returns leftover bytes that were not used as part of the auth
2190 * conversation. These bytes will be part of the message stream
2191 * instead. This function may not be called until authentication has
2194 * @param auth the auth conversation
2195 * @param str return location for pointer to string of unused bytes
2198 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2199 const DBusString **str)
2201 if (!DBUS_AUTH_IN_END_STATE (auth))
2204 *str = &auth->incoming;
2209 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2210 * after we've gotten them and successfully moved them elsewhere.
2212 * @param auth the auth conversation
2215 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2217 if (!DBUS_AUTH_IN_END_STATE (auth))
2220 _dbus_string_set_length (&auth->incoming, 0);
2224 * Called post-authentication, indicates whether we need to encode
2225 * the message stream with _dbus_auth_encode_data() prior to
2226 * sending it to the peer.
2228 * @param auth the auth conversation
2229 * @returns #TRUE if we need to encode the stream
2232 _dbus_auth_needs_encoding (DBusAuth *auth)
2234 if (auth->state != &common_state_authenticated)
2237 if (auth->mech != NULL)
2239 if (DBUS_AUTH_IS_CLIENT (auth))
2240 return auth->mech->client_encode_func != NULL;
2242 return auth->mech->server_encode_func != NULL;
2249 * Called post-authentication, encodes a block of bytes for sending to
2250 * the peer. If no encoding was negotiated, just copies the bytes
2251 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2253 * @param auth the auth conversation
2254 * @param plaintext the plain text data
2255 * @param encoded initialized string to where encoded data is appended
2256 * @returns #TRUE if we had enough memory and successfully encoded
2259 _dbus_auth_encode_data (DBusAuth *auth,
2260 const DBusString *plaintext,
2261 DBusString *encoded)
2263 _dbus_assert (plaintext != encoded);
2265 if (auth->state != &common_state_authenticated)
2268 if (_dbus_auth_needs_encoding (auth))
2270 if (DBUS_AUTH_IS_CLIENT (auth))
2271 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2273 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2277 return _dbus_string_copy (plaintext, 0, encoded,
2278 _dbus_string_get_length (encoded));
2283 * Called post-authentication, indicates whether we need to decode
2284 * the message stream with _dbus_auth_decode_data() after
2285 * receiving it from the peer.
2287 * @param auth the auth conversation
2288 * @returns #TRUE if we need to encode the stream
2291 _dbus_auth_needs_decoding (DBusAuth *auth)
2293 if (auth->state != &common_state_authenticated)
2296 if (auth->mech != NULL)
2298 if (DBUS_AUTH_IS_CLIENT (auth))
2299 return auth->mech->client_decode_func != NULL;
2301 return auth->mech->server_decode_func != NULL;
2309 * Called post-authentication, decodes a block of bytes received from
2310 * the peer. If no encoding was negotiated, just copies the bytes (you
2311 * can avoid this by checking _dbus_auth_needs_decoding()).
2313 * @todo We need to be able to distinguish "out of memory" error
2314 * from "the data is hosed" error.
2316 * @param auth the auth conversation
2317 * @param encoded the encoded data
2318 * @param plaintext initialized string where decoded data is appended
2319 * @returns #TRUE if we had enough memory and successfully decoded
2322 _dbus_auth_decode_data (DBusAuth *auth,
2323 const DBusString *encoded,
2324 DBusString *plaintext)
2326 _dbus_assert (plaintext != encoded);
2328 if (auth->state != &common_state_authenticated)
2331 if (_dbus_auth_needs_decoding (auth))
2333 if (DBUS_AUTH_IS_CLIENT (auth))
2334 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2336 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2340 return _dbus_string_copy (encoded, 0, plaintext,
2341 _dbus_string_get_length (plaintext));
2346 * Sets credentials received via reliable means from the operating
2349 * @param auth the auth conversation
2350 * @param credentials the credentials received
2353 _dbus_auth_set_credentials (DBusAuth *auth,
2354 const DBusCredentials *credentials)
2356 auth->credentials = *credentials;
2360 * Gets the identity we authorized the client as. Apps may have
2361 * different policies as to what identities they allow.
2363 * @param auth the auth conversation
2364 * @param credentials the credentials we've authorized
2367 _dbus_auth_get_identity (DBusAuth *auth,
2368 DBusCredentials *credentials)
2370 if (auth->state == &common_state_authenticated)
2371 *credentials = auth->authorized_identity;
2373 _dbus_credentials_clear (credentials);
2377 * Sets the "authentication context" which scopes cookies
2378 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2380 * @param auth the auth conversation
2381 * @param context the context
2382 * @returns #FALSE if no memory
2385 _dbus_auth_set_context (DBusAuth *auth,
2386 const DBusString *context)
2388 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2389 &auth->context, 0, _dbus_string_get_length (context));
2394 #ifdef DBUS_BUILD_TESTS
2395 #include "dbus-test.h"
2396 #include "dbus-auth-script.h"
2400 process_test_subdir (const DBusString *test_base_dir,
2403 DBusString test_directory;
2404 DBusString filename;
2412 if (!_dbus_string_init (&test_directory))
2413 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2415 _dbus_string_init_const (&filename, subdir);
2417 if (!_dbus_string_copy (test_base_dir, 0,
2418 &test_directory, 0))
2419 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2421 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2422 _dbus_assert_not_reached ("couldn't allocate full path");
2424 _dbus_string_free (&filename);
2425 if (!_dbus_string_init (&filename))
2426 _dbus_assert_not_reached ("didn't allocate filename string\n");
2428 dbus_error_init (&error);
2429 dir = _dbus_directory_open (&test_directory, &error);
2432 _dbus_warn ("Could not open %s: %s\n",
2433 _dbus_string_get_const_data (&test_directory),
2435 dbus_error_free (&error);
2439 printf ("Testing %s:\n", subdir);
2442 while (_dbus_directory_get_next_file (dir, &filename, &error))
2444 DBusString full_path;
2446 if (!_dbus_string_init (&full_path))
2447 _dbus_assert_not_reached ("couldn't init string");
2449 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2450 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2452 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2453 _dbus_assert_not_reached ("couldn't concat file to dir");
2455 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2457 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2458 _dbus_string_get_const_data (&filename));
2459 _dbus_string_free (&full_path);
2463 printf (" %s\n", _dbus_string_get_const_data (&filename));
2465 if (!_dbus_auth_script_run (&full_path))
2467 _dbus_string_free (&full_path);
2471 _dbus_string_free (&full_path);
2474 if (dbus_error_is_set (&error))
2476 _dbus_warn ("Could not get next file in %s: %s\n",
2477 _dbus_string_get_const_data (&test_directory), error.message);
2478 dbus_error_free (&error);
2487 _dbus_directory_close (dir);
2488 _dbus_string_free (&test_directory);
2489 _dbus_string_free (&filename);
2495 process_test_dirs (const char *test_data_dir)
2497 DBusString test_directory;
2502 _dbus_string_init_const (&test_directory, test_data_dir);
2504 if (!process_test_subdir (&test_directory, "auth"))
2511 _dbus_string_free (&test_directory);
2517 _dbus_auth_test (const char *test_data_dir)
2520 if (test_data_dir == NULL)
2523 if (!process_test_dirs (test_data_dir))
2529 #endif /* DBUS_BUILD_TESTS */