1 /* -*- mode: C; c-file-style: "gnu" -*- */
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-userdb.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 DBusAuth really needs to be rewritten as an explicit state
46 * machine. Right now it's too hard to prove to yourself by inspection
49 * @todo right now sometimes both ends will block waiting for input
50 * from the other end, e.g. if there's an error during
53 * @todo the cookie keyring needs to be cached globally not just
54 * per-auth (which raises threadsafety issues too)
56 * @todo grep FIXME in dbus-auth.c
60 * @defgroup DBusAuthInternals Authentication implementation details
61 * @ingroup DBusInternals
62 * @brief DBusAuth implementation details
64 * Private details of authentication code.
70 * This function appends an initial client response to the given string
72 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
73 DBusString *response);
76 * This function processes a block of data received from the peer.
77 * i.e. handles a DATA command.
79 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
80 const DBusString *data);
83 * This function encodes a block of data from the peer.
85 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
86 const DBusString *data,
90 * This function decodes a block of data from the peer.
92 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
93 const DBusString *data,
97 * This function is called when the mechanism is abandoned.
99 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
102 * Virtual table representing a particular auth mechanism.
106 const char *mechanism; /**< Name of the mechanism */
107 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
108 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
109 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
110 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
111 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
112 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
113 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
114 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
115 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
116 } DBusAuthMechanismHandler;
119 * Enumeration for the known authentication commands.
122 DBUS_AUTH_COMMAND_AUTH,
123 DBUS_AUTH_COMMAND_CANCEL,
124 DBUS_AUTH_COMMAND_DATA,
125 DBUS_AUTH_COMMAND_BEGIN,
126 DBUS_AUTH_COMMAND_REJECTED,
127 DBUS_AUTH_COMMAND_OK,
128 DBUS_AUTH_COMMAND_ERROR,
129 DBUS_AUTH_COMMAND_UNKNOWN
133 * Auth state function, determines the reaction to incoming events for
134 * a particular state. Returns whether we had enough memory to
135 * complete the operation.
137 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
138 DBusAuthCommand command,
139 const DBusString *args);
142 * Information about a auth state.
146 const char *name; /**< Name of the state */
147 DBusAuthStateFunction handler; /**< State function for this state */
151 * Internal members of DBusAuth.
155 int refcount; /**< reference count */
156 const char *side; /**< Client or server */
158 DBusString incoming; /**< Incoming data buffer */
159 DBusString outgoing; /**< Outgoing data buffer */
161 const DBusAuthStateData *state; /**< Current protocol state */
163 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
165 DBusString identity; /**< Current identity we're authorizing
169 DBusCredentials credentials; /**< Credentials read from socket,
173 DBusCredentials authorized_identity; /**< Credentials that are authorized */
175 DBusCredentials desired_identity; /**< Identity client has requested */
177 DBusString context; /**< Cookie scope */
178 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
179 int cookie_id; /**< ID of cookie to use */
180 DBusString challenge; /**< Challenge sent to client */
182 char **allowed_mechs; /**< Mechanisms we're allowed to use,
183 * or #NULL if we can use any
186 unsigned int needed_memory : 1; /**< We needed memory to continue since last
187 * successful getting something done
189 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
190 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
191 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
195 * "Subclass" of DBusAuth for client side
199 DBusAuth base; /**< Parent class */
201 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
206 * "Subclass" of DBusAuth for server side.
210 DBusAuth base; /**< Parent class */
212 int failures; /**< Number of times client has been rejected */
213 int max_failures; /**< Number of times we reject before disconnect */
217 static void goto_state (DBusAuth *auth,
218 const DBusAuthStateData *new_state);
219 static dbus_bool_t send_auth (DBusAuth *auth,
220 const DBusAuthMechanismHandler *mech);
221 static dbus_bool_t send_data (DBusAuth *auth,
223 static dbus_bool_t send_rejected (DBusAuth *auth);
224 static dbus_bool_t send_error (DBusAuth *auth,
225 const char *message);
226 static dbus_bool_t send_ok (DBusAuth *auth);
227 static dbus_bool_t send_begin (DBusAuth *auth);
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 _dbus_credentials_clear (&auth->credentials);
335 _dbus_credentials_clear (&auth->authorized_identity);
336 _dbus_credentials_clear (&auth->desired_identity);
338 auth->keyring = NULL;
339 auth->cookie_id = -1;
341 /* note that we don't use the max string length feature,
342 * because you can't use that feature if you're going to
343 * try to recover from out-of-memory (it creates
344 * what looks like unrecoverable inability to alloc
345 * more space in the string). But we do handle
346 * overlong buffers in _dbus_auth_do_work().
349 if (!_dbus_string_init (&auth->incoming))
352 if (!_dbus_string_init (&auth->outgoing))
355 if (!_dbus_string_init (&auth->identity))
358 if (!_dbus_string_init (&auth->context))
361 if (!_dbus_string_init (&auth->challenge))
364 /* default context if none is specified */
365 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
371 _dbus_string_free (&auth->challenge);
373 _dbus_string_free (&auth->context);
375 _dbus_string_free (&auth->identity);
377 _dbus_string_free (&auth->outgoing);
379 _dbus_string_free (&auth->incoming);
386 shutdown_mech (DBusAuth *auth)
388 /* Cancel any auth */
389 auth->already_asked_for_initial_response = FALSE;
390 _dbus_string_set_length (&auth->identity, 0);
392 _dbus_credentials_clear (&auth->authorized_identity);
393 _dbus_credentials_clear (&auth->desired_identity);
395 if (auth->mech != NULL)
397 _dbus_verbose ("%s: Shutting down mechanism %s\n",
398 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
400 if (DBUS_AUTH_IS_CLIENT (auth))
401 (* auth->mech->client_shutdown_func) (auth);
403 (* auth->mech->server_shutdown_func) (auth);
409 /* Returns TRUE but with an empty string hash if the
410 * cookie_id isn't known. As with all this code
411 * TRUE just means we had enough memory.
414 sha1_compute_hash (DBusAuth *auth,
416 const DBusString *server_challenge,
417 const DBusString *client_challenge,
424 _dbus_assert (auth->keyring != NULL);
428 if (!_dbus_string_init (&cookie))
431 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
435 if (_dbus_string_get_length (&cookie) == 0)
441 if (!_dbus_string_init (&to_hash))
444 if (!_dbus_string_copy (server_challenge, 0,
445 &to_hash, _dbus_string_get_length (&to_hash)))
448 if (!_dbus_string_append (&to_hash, ":"))
451 if (!_dbus_string_copy (client_challenge, 0,
452 &to_hash, _dbus_string_get_length (&to_hash)))
455 if (!_dbus_string_append (&to_hash, ":"))
458 if (!_dbus_string_copy (&cookie, 0,
459 &to_hash, _dbus_string_get_length (&to_hash)))
462 if (!_dbus_sha_compute (&to_hash, hash))
468 _dbus_string_zero (&to_hash);
469 _dbus_string_free (&to_hash);
471 _dbus_string_zero (&cookie);
472 _dbus_string_free (&cookie);
476 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
477 * entropy, we use 128. This is the number of bytes in the random
480 #define N_CHALLENGE_BYTES (128/8)
483 sha1_handle_first_client_response (DBusAuth *auth,
484 const DBusString *data)
486 /* We haven't sent a challenge yet, we're expecting a desired
487 * username from the client.
496 _dbus_string_set_length (&auth->challenge, 0);
498 if (_dbus_string_get_length (data) > 0)
500 if (_dbus_string_get_length (&auth->identity) > 0)
502 /* Tried to send two auth identities, wtf */
503 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
504 DBUS_AUTH_NAME (auth));
505 return send_rejected (auth);
509 /* this is our auth identity */
510 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
515 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
517 _dbus_verbose ("%s: Did not get a valid username from client\n",
518 DBUS_AUTH_NAME (auth));
519 return send_rejected (auth);
522 if (!_dbus_string_init (&tmp))
525 if (!_dbus_string_init (&tmp2))
527 _dbus_string_free (&tmp);
531 /* we cache the keyring for speed, so here we drop it if it's the
532 * wrong one. FIXME caching the keyring here is useless since we use
533 * a different DBusAuth for every connection.
536 !_dbus_keyring_is_for_user (auth->keyring,
539 _dbus_keyring_unref (auth->keyring);
540 auth->keyring = NULL;
543 if (auth->keyring == NULL)
547 dbus_error_init (&error);
548 auth->keyring = _dbus_keyring_new_homedir (data,
552 if (auth->keyring == NULL)
554 if (dbus_error_has_name (&error,
555 DBUS_ERROR_NO_MEMORY))
557 dbus_error_free (&error);
562 _DBUS_ASSERT_ERROR_IS_SET (&error);
563 _dbus_verbose ("%s: Error loading keyring: %s\n",
564 DBUS_AUTH_NAME (auth), error.message);
565 if (send_rejected (auth))
566 retval = TRUE; /* retval is only about mem */
567 dbus_error_free (&error);
573 _dbus_assert (!dbus_error_is_set (&error));
577 _dbus_assert (auth->keyring != NULL);
579 dbus_error_init (&error);
580 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
581 if (auth->cookie_id < 0)
583 _DBUS_ASSERT_ERROR_IS_SET (&error);
584 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
585 DBUS_AUTH_NAME (auth), error.message);
586 if (send_rejected (auth))
588 dbus_error_free (&error);
593 _dbus_assert (!dbus_error_is_set (&error));
596 if (!_dbus_string_copy (&auth->context, 0,
597 &tmp2, _dbus_string_get_length (&tmp2)))
600 if (!_dbus_string_append (&tmp2, " "))
603 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
606 if (!_dbus_string_append (&tmp2, " "))
609 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
612 _dbus_string_set_length (&auth->challenge, 0);
613 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
616 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
617 _dbus_string_get_length (&tmp2)))
620 if (!send_data (auth, &tmp2))
623 goto_state (auth, &server_state_waiting_for_data);
627 _dbus_string_zero (&tmp);
628 _dbus_string_free (&tmp);
629 _dbus_string_zero (&tmp2);
630 _dbus_string_free (&tmp2);
636 sha1_handle_second_client_response (DBusAuth *auth,
637 const DBusString *data)
639 /* We are expecting a response which is the hex-encoded client
640 * challenge, space, then SHA-1 hash of the concatenation of our
641 * challenge, ":", client challenge, ":", secret key, all
645 DBusString client_challenge;
646 DBusString client_hash;
648 DBusString correct_hash;
652 if (!_dbus_string_find_blank (data, 0, &i))
654 _dbus_verbose ("%s: no space separator in client response\n",
655 DBUS_AUTH_NAME (auth));
656 return send_rejected (auth);
659 if (!_dbus_string_init (&client_challenge))
662 if (!_dbus_string_init (&client_hash))
665 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
669 _dbus_string_skip_blank (data, i, &i);
671 if (!_dbus_string_copy_len (data, i,
672 _dbus_string_get_length (data) - i,
677 if (_dbus_string_get_length (&client_challenge) == 0 ||
678 _dbus_string_get_length (&client_hash) == 0)
680 _dbus_verbose ("%s: zero-length client challenge or hash\n",
681 DBUS_AUTH_NAME (auth));
682 if (send_rejected (auth))
687 if (!_dbus_string_init (&correct_hash))
690 if (!sha1_compute_hash (auth, auth->cookie_id,
696 /* if cookie_id was invalid, then we get an empty hash */
697 if (_dbus_string_get_length (&correct_hash) == 0)
699 if (send_rejected (auth))
704 if (!_dbus_string_equal (&client_hash, &correct_hash))
706 if (send_rejected (auth))
714 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
715 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
717 auth->authorized_identity = auth->desired_identity;
721 _dbus_string_zero (&correct_hash);
722 _dbus_string_free (&correct_hash);
724 _dbus_string_zero (&client_hash);
725 _dbus_string_free (&client_hash);
727 _dbus_string_free (&client_challenge);
733 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
734 const DBusString *data)
736 if (auth->cookie_id < 0)
737 return sha1_handle_first_client_response (auth, data);
739 return sha1_handle_second_client_response (auth, data);
743 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
745 auth->cookie_id = -1;
746 _dbus_string_set_length (&auth->challenge, 0);
750 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
751 DBusString *response)
753 const DBusString *username;
758 if (!_dbus_username_from_current_process (&username))
761 if (!_dbus_string_hex_encode (username, 0,
763 _dbus_string_get_length (response)))
773 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
774 const DBusString *data)
776 /* The data we get from the server should be the cookie context
777 * name, the cookie ID, and the server challenge, separated by
778 * spaces. We send back our challenge string and the correct hash.
782 DBusString cookie_id_str;
783 DBusString server_challenge;
784 DBusString client_challenge;
785 DBusString correct_hash;
792 if (!_dbus_string_find_blank (data, 0, &i))
794 if (send_error (auth,
795 "Server did not send context/ID/challenge properly"))
800 if (!_dbus_string_init (&context))
803 if (!_dbus_string_copy_len (data, 0, i,
807 _dbus_string_skip_blank (data, i, &i);
808 if (!_dbus_string_find_blank (data, i, &j))
810 if (send_error (auth,
811 "Server did not send context/ID/challenge properly"))
816 if (!_dbus_string_init (&cookie_id_str))
819 if (!_dbus_string_copy_len (data, i, j - i,
823 if (!_dbus_string_init (&server_challenge))
827 _dbus_string_skip_blank (data, i, &i);
828 j = _dbus_string_get_length (data);
830 if (!_dbus_string_copy_len (data, i, j - i,
831 &server_challenge, 0))
834 if (!_dbus_keyring_validate_context (&context))
836 if (send_error (auth, "Server sent invalid cookie context"))
841 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
843 if (send_error (auth, "Could not parse cookie ID as an integer"))
848 if (_dbus_string_get_length (&server_challenge) == 0)
850 if (send_error (auth, "Empty server challenge string"))
855 if (auth->keyring == NULL)
859 dbus_error_init (&error);
860 auth->keyring = _dbus_keyring_new_homedir (NULL,
864 if (auth->keyring == NULL)
866 if (dbus_error_has_name (&error,
867 DBUS_ERROR_NO_MEMORY))
869 dbus_error_free (&error);
874 _DBUS_ASSERT_ERROR_IS_SET (&error);
876 _dbus_verbose ("%s: Error loading keyring: %s\n",
877 DBUS_AUTH_NAME (auth), error.message);
879 if (send_error (auth, "Could not load cookie file"))
880 retval = TRUE; /* retval is only about mem */
882 dbus_error_free (&error);
888 _dbus_assert (!dbus_error_is_set (&error));
892 _dbus_assert (auth->keyring != NULL);
894 if (!_dbus_string_init (&tmp))
897 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
900 if (!_dbus_string_init (&client_challenge))
903 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
906 if (!_dbus_string_init (&correct_hash))
909 if (!sha1_compute_hash (auth, val,
915 if (_dbus_string_get_length (&correct_hash) == 0)
917 /* couldn't find the cookie ID or something */
918 if (send_error (auth, "Don't have the requested cookie ID"))
923 _dbus_string_set_length (&tmp, 0);
925 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
926 _dbus_string_get_length (&tmp)))
929 if (!_dbus_string_append (&tmp, " "))
932 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
933 _dbus_string_get_length (&tmp)))
936 if (!send_data (auth, &tmp))
942 _dbus_string_zero (&correct_hash);
943 _dbus_string_free (&correct_hash);
945 _dbus_string_free (&client_challenge);
947 _dbus_string_zero (&tmp);
948 _dbus_string_free (&tmp);
950 _dbus_string_free (&server_challenge);
952 _dbus_string_free (&cookie_id_str);
954 _dbus_string_free (&context);
960 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
962 auth->cookie_id = -1;
963 _dbus_string_set_length (&auth->challenge, 0);
967 handle_server_data_external_mech (DBusAuth *auth,
968 const DBusString *data)
970 if (auth->credentials.uid == DBUS_UID_UNSET)
972 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
973 DBUS_AUTH_NAME (auth));
974 return send_rejected (auth);
977 if (_dbus_string_get_length (data) > 0)
979 if (_dbus_string_get_length (&auth->identity) > 0)
981 /* Tried to send two auth identities, wtf */
982 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
983 DBUS_AUTH_NAME (auth));
984 return send_rejected (auth);
988 /* this is our auth identity */
989 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
994 /* Poke client for an auth identity, if none given */
995 if (_dbus_string_get_length (&auth->identity) == 0 &&
996 !auth->already_asked_for_initial_response)
998 if (send_data (auth, NULL))
1000 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1001 DBUS_AUTH_NAME (auth));
1002 auth->already_asked_for_initial_response = TRUE;
1009 _dbus_credentials_clear (&auth->desired_identity);
1011 /* If auth->identity is still empty here, then client
1012 * responded with an empty string after we poked it for
1013 * an initial response. This means to try to auth the
1014 * identity provided in the credentials.
1016 if (_dbus_string_get_length (&auth->identity) == 0)
1018 auth->desired_identity.uid = auth->credentials.uid;
1022 if (!_dbus_parse_uid (&auth->identity,
1023 &auth->desired_identity.uid))
1025 _dbus_verbose ("%s: could not get credentials from uid string\n",
1026 DBUS_AUTH_NAME (auth));
1027 return send_rejected (auth);
1031 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1033 _dbus_verbose ("%s: desired user %s is no good\n",
1034 DBUS_AUTH_NAME (auth),
1035 _dbus_string_get_const_data (&auth->identity));
1036 return send_rejected (auth);
1039 if (_dbus_credentials_match (&auth->desired_identity,
1040 &auth->credentials))
1042 /* client has authenticated */
1043 if (!send_ok (auth))
1046 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1047 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1048 DBUS_AUTH_NAME (auth),
1049 auth->desired_identity.uid,
1050 auth->credentials.uid);
1052 auth->authorized_identity.pid = auth->credentials.pid;
1053 auth->authorized_identity.uid = auth->desired_identity.uid;
1058 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1059 " gid="DBUS_GID_FORMAT
1060 " do not allow uid="DBUS_UID_FORMAT
1061 " gid="DBUS_GID_FORMAT"\n",
1062 DBUS_AUTH_NAME (auth),
1063 auth->credentials.uid, auth->credentials.gid,
1064 auth->desired_identity.uid, auth->desired_identity.gid);
1065 return send_rejected (auth);
1070 handle_server_shutdown_external_mech (DBusAuth *auth)
1076 handle_client_initial_response_external_mech (DBusAuth *auth,
1077 DBusString *response)
1079 /* We always append our UID as an initial response, so the server
1080 * doesn't have to send back an empty challenge to check whether we
1081 * want to specify an identity. i.e. this avoids a round trip that
1082 * the spec for the EXTERNAL mechanism otherwise requires.
1084 DBusString plaintext;
1086 if (!_dbus_string_init (&plaintext))
1089 if (!_dbus_string_append_uint (&plaintext,
1093 if (!_dbus_string_hex_encode (&plaintext, 0,
1095 _dbus_string_get_length (response)))
1098 _dbus_string_free (&plaintext);
1103 _dbus_string_free (&plaintext);
1108 handle_client_data_external_mech (DBusAuth *auth,
1109 const DBusString *data)
1116 handle_client_shutdown_external_mech (DBusAuth *auth)
1121 /* Put mechanisms here in order of preference.
1122 * What I eventually want to have is:
1124 * - a mechanism that checks UNIX domain socket credentials
1125 * - a simple magic cookie mechanism like X11 or ICE
1126 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1127 * offers such as Kerberos, X509, whatever.
1130 static const DBusAuthMechanismHandler
1131 all_mechanisms[] = {
1133 handle_server_data_external_mech,
1135 handle_server_shutdown_external_mech,
1136 handle_client_initial_response_external_mech,
1137 handle_client_data_external_mech,
1139 handle_client_shutdown_external_mech },
1140 { "DBUS_COOKIE_SHA1",
1141 handle_server_data_cookie_sha1_mech,
1143 handle_server_shutdown_cookie_sha1_mech,
1144 handle_client_initial_response_cookie_sha1_mech,
1145 handle_client_data_cookie_sha1_mech,
1147 handle_client_shutdown_cookie_sha1_mech },
1151 static const DBusAuthMechanismHandler*
1152 find_mech (const DBusString *name,
1153 char **allowed_mechs)
1157 if (allowed_mechs != NULL &&
1158 !_dbus_string_array_contains ((const char**) allowed_mechs,
1159 _dbus_string_get_const_data (name)))
1163 while (all_mechanisms[i].mechanism != NULL)
1165 if (_dbus_string_equal_c_str (name,
1166 all_mechanisms[i].mechanism))
1168 return &all_mechanisms[i];
1177 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1179 DBusString auth_command;
1181 if (!_dbus_string_init (&auth_command))
1184 if (!_dbus_string_append (&auth_command,
1187 _dbus_string_free (&auth_command);
1191 if (!_dbus_string_append (&auth_command,
1194 _dbus_string_free (&auth_command);
1198 if (mech->client_initial_response_func != NULL)
1200 if (!_dbus_string_append (&auth_command, " "))
1202 _dbus_string_free (&auth_command);
1206 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1208 _dbus_string_free (&auth_command);
1213 if (!_dbus_string_append (&auth_command,
1216 _dbus_string_free (&auth_command);
1220 if (!_dbus_string_copy (&auth_command, 0,
1222 _dbus_string_get_length (&auth->outgoing)))
1224 _dbus_string_free (&auth_command);
1228 _dbus_string_free (&auth_command);
1229 shutdown_mech (auth);
1231 goto_state (auth, &client_state_waiting_for_data);
1237 send_data (DBusAuth *auth, DBusString *data)
1241 if (data == NULL || _dbus_string_get_length (data) == 0)
1242 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1245 old_len = _dbus_string_get_length (&auth->outgoing);
1246 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1249 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1250 _dbus_string_get_length (&auth->outgoing)))
1253 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1259 _dbus_string_set_length (&auth->outgoing, old_len);
1266 send_rejected (DBusAuth *auth)
1269 DBusAuthServer *server_auth;
1272 if (!_dbus_string_init (&command))
1275 if (!_dbus_string_append (&command,
1280 while (all_mechanisms[i].mechanism != NULL)
1282 if (!_dbus_string_append (&command,
1286 if (!_dbus_string_append (&command,
1287 all_mechanisms[i].mechanism))
1293 if (!_dbus_string_append (&command, "\r\n"))
1296 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1297 _dbus_string_get_length (&auth->outgoing)))
1300 shutdown_mech (auth);
1302 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1303 server_auth = DBUS_AUTH_SERVER (auth);
1304 server_auth->failures += 1;
1306 if (server_auth->failures >= server_auth->max_failures)
1307 goto_state (auth, &common_state_need_disconnect);
1309 goto_state (auth, &server_state_waiting_for_auth);
1311 _dbus_string_free (&command);
1316 _dbus_string_free (&command);
1321 send_error (DBusAuth *auth, const char *message)
1323 return _dbus_string_append_printf (&auth->outgoing,
1324 "ERROR \"%s\"\r\n", message);
1328 send_ok (DBusAuth *auth)
1330 if (_dbus_string_append (&auth->outgoing, "OK\r\n"))
1332 goto_state (auth, &server_state_waiting_for_begin);
1340 send_begin (DBusAuth *auth)
1342 if (_dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
1344 goto_state (auth, &common_state_authenticated);
1352 send_cancel (DBusAuth *auth)
1354 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1356 goto_state (auth, &client_state_waiting_for_reject);
1364 process_data (DBusAuth *auth,
1365 const DBusString *args,
1366 DBusAuthDataFunction data_func)
1371 if (!_dbus_string_init (&decoded))
1374 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1376 _dbus_string_free (&decoded);
1380 if (_dbus_string_get_length (args) != end)
1382 _dbus_string_free (&decoded);
1383 if (!send_error (auth, "Invalid hex encoding"))
1389 #ifdef DBUS_ENABLE_VERBOSE_MODE
1390 if (_dbus_string_validate_ascii (&decoded, 0,
1391 _dbus_string_get_length (&decoded)))
1392 _dbus_verbose ("%s: data: '%s'\n",
1393 DBUS_AUTH_NAME (auth),
1394 _dbus_string_get_const_data (&decoded));
1397 if (!(* data_func) (auth, &decoded))
1399 _dbus_string_free (&decoded);
1403 _dbus_string_free (&decoded);
1408 handle_auth (DBusAuth *auth, const DBusString *args)
1410 if (_dbus_string_get_length (args) == 0)
1412 /* No args to the auth, send mechanisms */
1413 if (!send_rejected (auth))
1422 DBusString hex_response;
1424 _dbus_string_find_blank (args, 0, &i);
1426 if (!_dbus_string_init (&mech))
1429 if (!_dbus_string_init (&hex_response))
1431 _dbus_string_free (&mech);
1435 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1438 _dbus_string_skip_blank (args, i, &i);
1439 if (!_dbus_string_copy (args, i, &hex_response, 0))
1442 auth->mech = find_mech (&mech, auth->allowed_mechs);
1443 if (auth->mech != NULL)
1445 _dbus_verbose ("%s: Trying mechanism %s\n",
1446 DBUS_AUTH_NAME (auth),
1447 auth->mech->mechanism);
1449 if (!process_data (auth, &hex_response,
1450 auth->mech->server_data_func))
1455 /* Unsupported mechanism */
1456 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1457 DBUS_AUTH_NAME (auth),
1458 _dbus_string_get_const_data (&mech));
1460 if (!send_rejected (auth))
1464 _dbus_string_free (&mech);
1465 _dbus_string_free (&hex_response);
1471 _dbus_string_free (&mech);
1472 _dbus_string_free (&hex_response);
1478 handle_server_state_waiting_for_auth (DBusAuth *auth,
1479 DBusAuthCommand command,
1480 const DBusString *args)
1484 case DBUS_AUTH_COMMAND_AUTH:
1485 return handle_auth (auth, args);
1487 case DBUS_AUTH_COMMAND_CANCEL:
1488 case DBUS_AUTH_COMMAND_DATA:
1489 return send_error (auth, "Not currently in an auth conversation");
1491 case DBUS_AUTH_COMMAND_BEGIN:
1492 goto_state (auth, &common_state_need_disconnect);
1495 case DBUS_AUTH_COMMAND_ERROR:
1496 return send_rejected (auth);
1498 case DBUS_AUTH_COMMAND_REJECTED:
1499 case DBUS_AUTH_COMMAND_OK:
1500 case DBUS_AUTH_COMMAND_UNKNOWN:
1502 return send_error (auth, "Unknown command");
1507 handle_server_state_waiting_for_data (DBusAuth *auth,
1508 DBusAuthCommand command,
1509 const DBusString *args)
1513 case DBUS_AUTH_COMMAND_AUTH:
1514 return send_error (auth, "Sent AUTH while another AUTH in progress");
1516 case DBUS_AUTH_COMMAND_CANCEL:
1517 case DBUS_AUTH_COMMAND_ERROR:
1518 return send_rejected (auth);
1520 case DBUS_AUTH_COMMAND_DATA:
1521 return process_data (auth, args, auth->mech->server_data_func);
1523 case DBUS_AUTH_COMMAND_BEGIN:
1524 goto_state (auth, &common_state_need_disconnect);
1527 case DBUS_AUTH_COMMAND_REJECTED:
1528 case DBUS_AUTH_COMMAND_OK:
1529 case DBUS_AUTH_COMMAND_UNKNOWN:
1531 return send_error (auth, "Unknown command");
1536 handle_server_state_waiting_for_begin (DBusAuth *auth,
1537 DBusAuthCommand command,
1538 const DBusString *args)
1542 case DBUS_AUTH_COMMAND_AUTH:
1543 return send_error (auth, "Sent AUTH while expecting BEGIN");
1545 case DBUS_AUTH_COMMAND_DATA:
1546 return send_error (auth, "Sent DATA while expecting BEGIN");
1548 case DBUS_AUTH_COMMAND_BEGIN:
1549 goto_state (auth, &common_state_authenticated);
1552 case DBUS_AUTH_COMMAND_REJECTED:
1553 case DBUS_AUTH_COMMAND_OK:
1554 case DBUS_AUTH_COMMAND_UNKNOWN:
1556 return send_error (auth, "Unknown command");
1558 case DBUS_AUTH_COMMAND_CANCEL:
1559 case DBUS_AUTH_COMMAND_ERROR:
1560 return send_rejected (auth);
1564 /* return FALSE if no memory, TRUE if all OK */
1566 get_word (const DBusString *str,
1572 _dbus_string_skip_blank (str, *start, start);
1573 _dbus_string_find_blank (str, *start, &i);
1577 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1587 record_mechanisms (DBusAuth *auth,
1588 const DBusString *args)
1593 if (auth->already_got_mechanisms)
1596 len = _dbus_string_get_length (args);
1602 const DBusAuthMechanismHandler *mech;
1604 if (!_dbus_string_init (&m))
1607 if (!get_word (args, &next, &m))
1609 _dbus_string_free (&m);
1613 mech = find_mech (&m, auth->allowed_mechs);
1617 /* FIXME right now we try mechanisms in the order
1618 * the server lists them; should we do them in
1619 * some more deterministic order?
1621 * Probably in all_mechanisms order, our order of
1622 * preference. Of course when the server is us,
1623 * it lists things in that order anyhow.
1626 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1627 DBUS_AUTH_NAME (auth), mech->mechanism);
1629 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1632 _dbus_string_free (&m);
1638 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1639 DBUS_AUTH_NAME (auth),
1640 _dbus_string_get_const_data (&m));
1643 _dbus_string_free (&m);
1646 auth->already_got_mechanisms = TRUE;
1651 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1657 process_rejected (DBusAuth *auth, const DBusString *args)
1659 const DBusAuthMechanismHandler *mech;
1660 DBusAuthClient *client;
1662 client = DBUS_AUTH_CLIENT (auth);
1664 if (!auth->already_got_mechanisms)
1666 if (!record_mechanisms (auth, args))
1670 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1672 mech = client->mechs_to_try->data;
1674 if (!send_auth (auth, mech))
1677 _dbus_list_pop_first (&client->mechs_to_try);
1679 _dbus_verbose ("%s: Trying mechanism %s\n",
1680 DBUS_AUTH_NAME (auth),
1686 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1687 DBUS_AUTH_NAME (auth));
1688 goto_state (auth, &common_state_need_disconnect);
1696 handle_client_state_waiting_for_data (DBusAuth *auth,
1697 DBusAuthCommand command,
1698 const DBusString *args)
1700 _dbus_assert (auth->mech != NULL);
1704 case DBUS_AUTH_COMMAND_DATA:
1705 return process_data (auth, args, auth->mech->client_data_func);
1707 case DBUS_AUTH_COMMAND_REJECTED:
1708 return process_rejected (auth, args);
1710 case DBUS_AUTH_COMMAND_OK:
1711 return send_begin (auth);
1713 case DBUS_AUTH_COMMAND_ERROR:
1714 return send_cancel (auth);
1716 case DBUS_AUTH_COMMAND_AUTH:
1717 case DBUS_AUTH_COMMAND_CANCEL:
1718 case DBUS_AUTH_COMMAND_BEGIN:
1719 case DBUS_AUTH_COMMAND_UNKNOWN:
1721 return send_error (auth, "Unknown command");
1726 handle_client_state_waiting_for_ok (DBusAuth *auth,
1727 DBusAuthCommand command,
1728 const DBusString *args)
1732 case DBUS_AUTH_COMMAND_REJECTED:
1733 return process_rejected (auth, args);
1735 case DBUS_AUTH_COMMAND_OK:
1736 return send_begin (auth);
1738 case DBUS_AUTH_COMMAND_DATA:
1739 case DBUS_AUTH_COMMAND_ERROR:
1740 return send_cancel (auth);
1742 case DBUS_AUTH_COMMAND_AUTH:
1743 case DBUS_AUTH_COMMAND_CANCEL:
1744 case DBUS_AUTH_COMMAND_BEGIN:
1745 case DBUS_AUTH_COMMAND_UNKNOWN:
1747 return send_error (auth, "Unknown command");
1752 handle_client_state_waiting_for_reject (DBusAuth *auth,
1753 DBusAuthCommand command,
1754 const DBusString *args)
1758 case DBUS_AUTH_COMMAND_REJECTED:
1759 return process_rejected (auth, args);
1761 case DBUS_AUTH_COMMAND_AUTH:
1762 case DBUS_AUTH_COMMAND_CANCEL:
1763 case DBUS_AUTH_COMMAND_DATA:
1764 case DBUS_AUTH_COMMAND_BEGIN:
1765 case DBUS_AUTH_COMMAND_OK:
1766 case DBUS_AUTH_COMMAND_ERROR:
1767 case DBUS_AUTH_COMMAND_UNKNOWN:
1769 goto_state (auth, &common_state_need_disconnect);
1775 * Mapping from command name to enum
1778 const char *name; /**< Name of the command */
1779 DBusAuthCommand command; /**< Corresponding enum */
1780 } DBusAuthCommandName;
1782 static DBusAuthCommandName auth_command_names[] = {
1783 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
1784 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
1785 { "DATA", DBUS_AUTH_COMMAND_DATA },
1786 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
1787 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
1788 { "OK", DBUS_AUTH_COMMAND_OK },
1789 { "ERROR", DBUS_AUTH_COMMAND_ERROR }
1792 static DBusAuthCommand
1793 lookup_command_from_name (DBusString *command)
1797 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
1799 if (_dbus_string_equal_c_str (command,
1800 auth_command_names[i].name))
1801 return auth_command_names[i].command;
1804 return DBUS_AUTH_COMMAND_UNKNOWN;
1808 goto_state (DBusAuth *auth, const DBusAuthStateData *state)
1810 _dbus_verbose ("%s: going from state %s to state %s\n",
1811 DBUS_AUTH_NAME (auth),
1815 auth->state = state;
1818 /* returns whether to call it again right away */
1820 process_command (DBusAuth *auth)
1822 DBusAuthCommand command;
1829 /* _dbus_verbose ("%s: trying process_command()\n"); */
1834 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1837 if (!_dbus_string_init (&line))
1839 auth->needed_memory = TRUE;
1843 if (!_dbus_string_init (&args))
1845 _dbus_string_free (&line);
1846 auth->needed_memory = TRUE;
1850 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
1853 if (!_dbus_string_validate_ascii (&line, 0,
1854 _dbus_string_get_length (&line)))
1856 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1857 DBUS_AUTH_NAME (auth));
1858 if (!send_error (auth, "Command contained non-ASCII"))
1864 _dbus_verbose ("%s: got command \"%s\"\n",
1865 DBUS_AUTH_NAME (auth),
1866 _dbus_string_get_const_data (&line));
1868 _dbus_string_find_blank (&line, 0, &i);
1869 _dbus_string_skip_blank (&line, i, &j);
1872 _dbus_string_delete (&line, i, j - i);
1874 if (!_dbus_string_move (&line, i, &args, 0))
1877 /* FIXME we should probably validate that only the allowed
1878 * chars are in the command name
1881 command = lookup_command_from_name (&line);
1882 if (!(* auth->state->handler) (auth, command, &args))
1887 /* We've succeeded in processing the whole command so drop it out
1888 * of the incoming buffer and return TRUE to try another command.
1891 _dbus_string_delete (&auth->incoming, 0, eol);
1894 _dbus_string_delete (&auth->incoming, 0, 2);
1899 _dbus_string_free (&args);
1900 _dbus_string_free (&line);
1903 auth->needed_memory = TRUE;
1905 auth->needed_memory = FALSE;
1914 * @addtogroup DBusAuth
1919 * Creates a new auth conversation object for the server side.
1920 * See doc/dbus-sasl-profile.txt for full details on what
1923 * @returns the new object or #NULL if no memory
1926 _dbus_auth_server_new (void)
1929 DBusAuthServer *server_auth;
1931 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1935 auth->side = auth_side_server;
1936 auth->state = &server_state_waiting_for_auth;
1938 server_auth = DBUS_AUTH_SERVER (auth);
1940 /* perhaps this should be per-mechanism with a lower
1943 server_auth->failures = 0;
1944 server_auth->max_failures = 6;
1950 * Creates a new auth conversation object for the client side.
1951 * See doc/dbus-sasl-profile.txt for full details on what
1954 * @returns the new object or #NULL if no memory
1957 _dbus_auth_client_new (void)
1961 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1965 auth->side = auth_side_client;
1966 auth->state = &client_state_need_send_auth;
1968 /* Start the auth conversation by sending AUTH for our default
1970 if (!send_auth (auth, &all_mechanisms[0]))
1972 _dbus_auth_unref (auth);
1980 * Increments the refcount of an auth object.
1982 * @param auth the auth conversation
1983 * @returns the auth conversation
1986 _dbus_auth_ref (DBusAuth *auth)
1988 _dbus_assert (auth != NULL);
1990 auth->refcount += 1;
1996 * Decrements the refcount of an auth object.
1998 * @param auth the auth conversation
2001 _dbus_auth_unref (DBusAuth *auth)
2003 _dbus_assert (auth != NULL);
2004 _dbus_assert (auth->refcount > 0);
2006 auth->refcount -= 1;
2007 if (auth->refcount == 0)
2009 shutdown_mech (auth);
2011 if (DBUS_AUTH_IS_CLIENT (auth))
2013 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2017 _dbus_keyring_unref (auth->keyring);
2019 _dbus_string_free (&auth->context);
2020 _dbus_string_free (&auth->challenge);
2021 _dbus_string_free (&auth->identity);
2022 _dbus_string_free (&auth->incoming);
2023 _dbus_string_free (&auth->outgoing);
2025 dbus_free_string_array (auth->allowed_mechs);
2032 * Sets an array of authentication mechanism names
2033 * that we are willing to use.
2035 * @param auth the auth conversation
2036 * @param mechanisms #NULL-terminated array of mechanism names
2037 * @returns #FALSE if no memory
2040 _dbus_auth_set_mechanisms (DBusAuth *auth,
2041 const char **mechanisms)
2045 if (mechanisms != NULL)
2047 copy = _dbus_dup_string_array (mechanisms);
2054 dbus_free_string_array (auth->allowed_mechs);
2056 auth->allowed_mechs = copy;
2062 * @param auth the auth conversation object
2063 * @returns #TRUE if we're in a final state
2065 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2068 * Analyzes buffered input and moves the auth conversation forward,
2069 * returning the new state of the auth conversation.
2071 * @param auth the auth conversation
2072 * @returns the new state
2075 _dbus_auth_do_work (DBusAuth *auth)
2077 auth->needed_memory = FALSE;
2079 /* Max amount we'll buffer up before deciding someone's on crack */
2080 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2084 if (DBUS_AUTH_IN_END_STATE (auth))
2087 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2088 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2090 goto_state (auth, &common_state_need_disconnect);
2091 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2092 DBUS_AUTH_NAME (auth));
2096 while (process_command (auth));
2098 if (auth->needed_memory)
2099 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2100 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2101 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2102 else if (auth->state == &common_state_need_disconnect)
2103 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2104 else if (auth->state == &common_state_authenticated)
2105 return DBUS_AUTH_STATE_AUTHENTICATED;
2106 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2110 * Gets bytes that need to be sent to the peer we're conversing with.
2111 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2112 * to notify the auth object that they were written.
2114 * @param auth the auth conversation
2115 * @param str return location for a ref to the buffer to send
2116 * @returns #FALSE if nothing to send
2119 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2120 const DBusString **str)
2122 _dbus_assert (auth != NULL);
2123 _dbus_assert (str != NULL);
2127 if (_dbus_string_get_length (&auth->outgoing) == 0)
2130 *str = &auth->outgoing;
2136 * Notifies the auth conversation object that
2137 * the given number of bytes of the outgoing buffer
2138 * have been written out.
2140 * @param auth the auth conversation
2141 * @param bytes_sent number of bytes written out
2144 _dbus_auth_bytes_sent (DBusAuth *auth,
2147 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2148 DBUS_AUTH_NAME (auth),
2150 _dbus_string_get_const_data (&auth->outgoing));
2152 _dbus_string_delete (&auth->outgoing,
2157 * Get a buffer to be used for reading bytes from the peer we're conversing
2158 * with. Bytes should be appended to this buffer.
2160 * @param auth the auth conversation
2161 * @param buffer return location for buffer to append bytes to
2164 _dbus_auth_get_buffer (DBusAuth *auth,
2165 DBusString **buffer)
2167 _dbus_assert (auth != NULL);
2168 _dbus_assert (!auth->buffer_outstanding);
2170 *buffer = &auth->incoming;
2172 auth->buffer_outstanding = TRUE;
2176 * Returns a buffer with new data read into it.
2178 * @param auth the auth conversation
2179 * @param buffer the buffer being returned
2180 * @param bytes_read number of new bytes added
2183 _dbus_auth_return_buffer (DBusAuth *auth,
2187 _dbus_assert (buffer == &auth->incoming);
2188 _dbus_assert (auth->buffer_outstanding);
2190 auth->buffer_outstanding = FALSE;
2194 * Returns leftover bytes that were not used as part of the auth
2195 * conversation. These bytes will be part of the message stream
2196 * instead. This function may not be called until authentication has
2199 * @param auth the auth conversation
2200 * @param str return location for pointer to string of unused bytes
2203 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2204 const DBusString **str)
2206 if (!DBUS_AUTH_IN_END_STATE (auth))
2209 *str = &auth->incoming;
2214 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2215 * after we've gotten them and successfully moved them elsewhere.
2217 * @param auth the auth conversation
2220 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2222 if (!DBUS_AUTH_IN_END_STATE (auth))
2225 _dbus_string_set_length (&auth->incoming, 0);
2229 * Called post-authentication, indicates whether we need to encode
2230 * the message stream with _dbus_auth_encode_data() prior to
2231 * sending it to the peer.
2233 * @param auth the auth conversation
2234 * @returns #TRUE if we need to encode the stream
2237 _dbus_auth_needs_encoding (DBusAuth *auth)
2239 if (auth->state != &common_state_authenticated)
2242 if (auth->mech != NULL)
2244 if (DBUS_AUTH_IS_CLIENT (auth))
2245 return auth->mech->client_encode_func != NULL;
2247 return auth->mech->server_encode_func != NULL;
2254 * Called post-authentication, encodes a block of bytes for sending to
2255 * the peer. If no encoding was negotiated, just copies the bytes
2256 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2258 * @param auth the auth conversation
2259 * @param plaintext the plain text data
2260 * @param encoded initialized string to where encoded data is appended
2261 * @returns #TRUE if we had enough memory and successfully encoded
2264 _dbus_auth_encode_data (DBusAuth *auth,
2265 const DBusString *plaintext,
2266 DBusString *encoded)
2268 _dbus_assert (plaintext != encoded);
2270 if (auth->state != &common_state_authenticated)
2273 if (_dbus_auth_needs_encoding (auth))
2275 if (DBUS_AUTH_IS_CLIENT (auth))
2276 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2278 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2282 return _dbus_string_copy (plaintext, 0, encoded,
2283 _dbus_string_get_length (encoded));
2288 * Called post-authentication, indicates whether we need to decode
2289 * the message stream with _dbus_auth_decode_data() after
2290 * receiving it from the peer.
2292 * @param auth the auth conversation
2293 * @returns #TRUE if we need to encode the stream
2296 _dbus_auth_needs_decoding (DBusAuth *auth)
2298 if (auth->state != &common_state_authenticated)
2301 if (auth->mech != NULL)
2303 if (DBUS_AUTH_IS_CLIENT (auth))
2304 return auth->mech->client_decode_func != NULL;
2306 return auth->mech->server_decode_func != NULL;
2314 * Called post-authentication, decodes a block of bytes received from
2315 * the peer. If no encoding was negotiated, just copies the bytes (you
2316 * can avoid this by checking _dbus_auth_needs_decoding()).
2318 * @todo We need to be able to distinguish "out of memory" error
2319 * from "the data is hosed" error.
2321 * @param auth the auth conversation
2322 * @param encoded the encoded data
2323 * @param plaintext initialized string where decoded data is appended
2324 * @returns #TRUE if we had enough memory and successfully decoded
2327 _dbus_auth_decode_data (DBusAuth *auth,
2328 const DBusString *encoded,
2329 DBusString *plaintext)
2331 _dbus_assert (plaintext != encoded);
2333 if (auth->state != &common_state_authenticated)
2336 if (_dbus_auth_needs_decoding (auth))
2338 if (DBUS_AUTH_IS_CLIENT (auth))
2339 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2341 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2345 return _dbus_string_copy (encoded, 0, plaintext,
2346 _dbus_string_get_length (plaintext));
2351 * Sets credentials received via reliable means from the operating
2354 * @param auth the auth conversation
2355 * @param credentials the credentials received
2358 _dbus_auth_set_credentials (DBusAuth *auth,
2359 const DBusCredentials *credentials)
2361 auth->credentials = *credentials;
2365 * Gets the identity we authorized the client as. Apps may have
2366 * different policies as to what identities they allow.
2368 * @param auth the auth conversation
2369 * @param credentials the credentials we've authorized
2372 _dbus_auth_get_identity (DBusAuth *auth,
2373 DBusCredentials *credentials)
2375 if (auth->state == &common_state_authenticated)
2376 *credentials = auth->authorized_identity;
2378 _dbus_credentials_clear (credentials);
2382 * Sets the "authentication context" which scopes cookies
2383 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2385 * @param auth the auth conversation
2386 * @param context the context
2387 * @returns #FALSE if no memory
2390 _dbus_auth_set_context (DBusAuth *auth,
2391 const DBusString *context)
2393 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2394 &auth->context, 0, _dbus_string_get_length (context));
2399 /* tests in dbus-auth-util.c */