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 right now sometimes both ends will block waiting for input
46 * from the other end, e.g. if there's an error during
49 * @todo the cookie keyring needs to be cached globally not just
50 * per-auth (which raises threadsafety issues too)
52 * @todo grep FIXME in dbus-auth.c
56 * @defgroup DBusAuthInternals Authentication implementation details
57 * @ingroup DBusInternals
58 * @brief DBusAuth implementation details
60 * Private details of authentication code.
66 * This function appends an initial client response to the given string
68 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
69 DBusString *response);
72 * This function processes a block of data received from the peer.
73 * i.e. handles a DATA command.
75 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
76 const DBusString *data);
79 * This function encodes a block of data from the peer.
81 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
82 const DBusString *data,
86 * This function decodes a block of data from the peer.
88 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
89 const DBusString *data,
93 * This function is called when the mechanism is abandoned.
95 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
98 * Virtual table representing a particular auth mechanism.
102 const char *mechanism; /**< Name of the mechanism */
103 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
104 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
105 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
106 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
107 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
108 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
109 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
110 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
111 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
112 } DBusAuthMechanismHandler;
115 * Enumeration for the known authentication commands.
118 DBUS_AUTH_COMMAND_AUTH,
119 DBUS_AUTH_COMMAND_CANCEL,
120 DBUS_AUTH_COMMAND_DATA,
121 DBUS_AUTH_COMMAND_BEGIN,
122 DBUS_AUTH_COMMAND_REJECTED,
123 DBUS_AUTH_COMMAND_OK,
124 DBUS_AUTH_COMMAND_ERROR,
125 DBUS_AUTH_COMMAND_UNKNOWN
129 * Auth state function, determines the reaction to incoming events for
130 * a particular state. Returns whether we had enough memory to
131 * complete the operation.
133 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
134 DBusAuthCommand command,
135 const DBusString *args);
138 * Information about a auth state.
142 const char *name; /**< Name of the state */
143 DBusAuthStateFunction handler; /**< State function for this state */
147 * Internal members of DBusAuth.
151 int refcount; /**< reference count */
152 const char *side; /**< Client or server */
154 DBusString incoming; /**< Incoming data buffer */
155 DBusString outgoing; /**< Outgoing data buffer */
157 const DBusAuthStateData *state; /**< Current protocol state */
159 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
161 DBusString identity; /**< Current identity we're authorizing
165 DBusCredentials credentials; /**< Credentials read from socket,
169 DBusCredentials authorized_identity; /**< Credentials that are authorized */
171 DBusCredentials desired_identity; /**< Identity client has requested */
173 DBusString context; /**< Cookie scope */
174 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
175 int cookie_id; /**< ID of cookie to use */
176 DBusString challenge; /**< Challenge sent to client */
178 char **allowed_mechs; /**< Mechanisms we're allowed to use,
179 * or #NULL if we can use any
182 unsigned int needed_memory : 1; /**< We needed memory to continue since last
183 * successful getting something done
185 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
186 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
187 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
191 * "Subclass" of DBusAuth for client side
195 DBusAuth base; /**< Parent class */
197 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
199 DBusString guid_from_server; /**< GUID received from server */
204 * "Subclass" of DBusAuth for server side.
208 DBusAuth base; /**< Parent class */
210 int failures; /**< Number of times client has been rejected */
211 int max_failures; /**< Number of times we reject before disconnect */
213 DBusString guid; /**< Our globally unique ID in hex encoding */
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 const DBusString *args_from_ok);
229 static dbus_bool_t send_cancel (DBusAuth *auth);
235 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
236 DBusAuthCommand command,
237 const DBusString *args);
238 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
239 DBusAuthCommand command,
240 const DBusString *args);
241 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
242 DBusAuthCommand command,
243 const DBusString *args);
245 static const DBusAuthStateData server_state_waiting_for_auth = {
246 "WaitingForAuth", handle_server_state_waiting_for_auth
248 static const DBusAuthStateData server_state_waiting_for_data = {
249 "WaitingForData", handle_server_state_waiting_for_data
251 static const DBusAuthStateData server_state_waiting_for_begin = {
252 "WaitingForBegin", handle_server_state_waiting_for_begin
259 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
260 DBusAuthCommand command,
261 const DBusString *args);
262 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
263 DBusAuthCommand command,
264 const DBusString *args);
265 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
266 DBusAuthCommand command,
267 const DBusString *args);
269 static const DBusAuthStateData client_state_need_send_auth = {
272 static const DBusAuthStateData client_state_waiting_for_data = {
273 "WaitingForData", handle_client_state_waiting_for_data
275 static const DBusAuthStateData client_state_waiting_for_ok = {
276 "WaitingForOK", handle_client_state_waiting_for_ok
278 static const DBusAuthStateData client_state_waiting_for_reject = {
279 "WaitingForReject", handle_client_state_waiting_for_reject
283 * Common terminal states. Terminal states have handler == NULL.
286 static const DBusAuthStateData common_state_authenticated = {
287 "Authenticated", NULL
290 static const DBusAuthStateData common_state_need_disconnect = {
291 "NeedDisconnect", NULL
294 static const char auth_side_client[] = "client";
295 static const char auth_side_server[] = "server";
297 * @param auth the auth conversation
298 * @returns #TRUE if the conversation is the server side
300 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
302 * @param auth the auth conversation
303 * @returns #TRUE if the conversation is the client side
305 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
307 * @param auth the auth conversation
308 * @returns auth cast to DBusAuthClient
310 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
312 * @param auth the auth conversation
313 * @returns auth cast to DBusAuthServer
315 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
318 * The name of the auth ("client" or "server")
319 * @param auth the auth conversation
322 #define DBUS_AUTH_NAME(auth) ((auth)->side)
325 _dbus_auth_new (int size)
329 auth = dbus_malloc0 (size);
335 _dbus_credentials_clear (&auth->credentials);
336 _dbus_credentials_clear (&auth->authorized_identity);
337 _dbus_credentials_clear (&auth->desired_identity);
339 auth->keyring = NULL;
340 auth->cookie_id = -1;
342 /* note that we don't use the max string length feature,
343 * because you can't use that feature if you're going to
344 * try to recover from out-of-memory (it creates
345 * what looks like unrecoverable inability to alloc
346 * more space in the string). But we do handle
347 * overlong buffers in _dbus_auth_do_work().
350 if (!_dbus_string_init (&auth->incoming))
353 if (!_dbus_string_init (&auth->outgoing))
356 if (!_dbus_string_init (&auth->identity))
359 if (!_dbus_string_init (&auth->context))
362 if (!_dbus_string_init (&auth->challenge))
365 /* default context if none is specified */
366 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
372 _dbus_string_free (&auth->challenge);
374 _dbus_string_free (&auth->context);
376 _dbus_string_free (&auth->identity);
378 _dbus_string_free (&auth->outgoing);
380 _dbus_string_free (&auth->incoming);
387 shutdown_mech (DBusAuth *auth)
389 /* Cancel any auth */
390 auth->already_asked_for_initial_response = FALSE;
391 _dbus_string_set_length (&auth->identity, 0);
393 _dbus_credentials_clear (&auth->authorized_identity);
394 _dbus_credentials_clear (&auth->desired_identity);
396 if (auth->mech != NULL)
398 _dbus_verbose ("%s: Shutting down mechanism %s\n",
399 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
401 if (DBUS_AUTH_IS_CLIENT (auth))
402 (* auth->mech->client_shutdown_func) (auth);
404 (* auth->mech->server_shutdown_func) (auth);
410 /* Returns TRUE but with an empty string hash if the
411 * cookie_id isn't known. As with all this code
412 * TRUE just means we had enough memory.
415 sha1_compute_hash (DBusAuth *auth,
417 const DBusString *server_challenge,
418 const DBusString *client_challenge,
425 _dbus_assert (auth->keyring != NULL);
429 if (!_dbus_string_init (&cookie))
432 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
436 if (_dbus_string_get_length (&cookie) == 0)
442 if (!_dbus_string_init (&to_hash))
445 if (!_dbus_string_copy (server_challenge, 0,
446 &to_hash, _dbus_string_get_length (&to_hash)))
449 if (!_dbus_string_append (&to_hash, ":"))
452 if (!_dbus_string_copy (client_challenge, 0,
453 &to_hash, _dbus_string_get_length (&to_hash)))
456 if (!_dbus_string_append (&to_hash, ":"))
459 if (!_dbus_string_copy (&cookie, 0,
460 &to_hash, _dbus_string_get_length (&to_hash)))
463 if (!_dbus_sha_compute (&to_hash, hash))
469 _dbus_string_zero (&to_hash);
470 _dbus_string_free (&to_hash);
472 _dbus_string_zero (&cookie);
473 _dbus_string_free (&cookie);
477 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
478 * entropy, we use 128. This is the number of bytes in the random
481 #define N_CHALLENGE_BYTES (128/8)
484 sha1_handle_first_client_response (DBusAuth *auth,
485 const DBusString *data)
487 /* We haven't sent a challenge yet, we're expecting a desired
488 * username from the client.
497 _dbus_string_set_length (&auth->challenge, 0);
499 if (_dbus_string_get_length (data) > 0)
501 if (_dbus_string_get_length (&auth->identity) > 0)
503 /* Tried to send two auth identities, wtf */
504 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
505 DBUS_AUTH_NAME (auth));
506 return send_rejected (auth);
510 /* this is our auth identity */
511 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
516 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
518 _dbus_verbose ("%s: Did not get a valid username from client\n",
519 DBUS_AUTH_NAME (auth));
520 return send_rejected (auth);
523 if (!_dbus_string_init (&tmp))
526 if (!_dbus_string_init (&tmp2))
528 _dbus_string_free (&tmp);
532 /* we cache the keyring for speed, so here we drop it if it's the
533 * wrong one. FIXME caching the keyring here is useless since we use
534 * a different DBusAuth for every connection.
537 !_dbus_keyring_is_for_user (auth->keyring,
540 _dbus_keyring_unref (auth->keyring);
541 auth->keyring = NULL;
544 if (auth->keyring == NULL)
548 dbus_error_init (&error);
549 auth->keyring = _dbus_keyring_new_homedir (data,
553 if (auth->keyring == NULL)
555 if (dbus_error_has_name (&error,
556 DBUS_ERROR_NO_MEMORY))
558 dbus_error_free (&error);
563 _DBUS_ASSERT_ERROR_IS_SET (&error);
564 _dbus_verbose ("%s: Error loading keyring: %s\n",
565 DBUS_AUTH_NAME (auth), error.message);
566 if (send_rejected (auth))
567 retval = TRUE; /* retval is only about mem */
568 dbus_error_free (&error);
574 _dbus_assert (!dbus_error_is_set (&error));
578 _dbus_assert (auth->keyring != NULL);
580 dbus_error_init (&error);
581 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
582 if (auth->cookie_id < 0)
584 _DBUS_ASSERT_ERROR_IS_SET (&error);
585 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
586 DBUS_AUTH_NAME (auth), error.message);
587 if (send_rejected (auth))
589 dbus_error_free (&error);
594 _dbus_assert (!dbus_error_is_set (&error));
597 if (!_dbus_string_copy (&auth->context, 0,
598 &tmp2, _dbus_string_get_length (&tmp2)))
601 if (!_dbus_string_append (&tmp2, " "))
604 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
607 if (!_dbus_string_append (&tmp2, " "))
610 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
613 _dbus_string_set_length (&auth->challenge, 0);
614 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
617 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
618 _dbus_string_get_length (&tmp2)))
621 if (!send_data (auth, &tmp2))
624 goto_state (auth, &server_state_waiting_for_data);
628 _dbus_string_zero (&tmp);
629 _dbus_string_free (&tmp);
630 _dbus_string_zero (&tmp2);
631 _dbus_string_free (&tmp2);
637 sha1_handle_second_client_response (DBusAuth *auth,
638 const DBusString *data)
640 /* We are expecting a response which is the hex-encoded client
641 * challenge, space, then SHA-1 hash of the concatenation of our
642 * challenge, ":", client challenge, ":", secret key, all
646 DBusString client_challenge;
647 DBusString client_hash;
649 DBusString correct_hash;
653 if (!_dbus_string_find_blank (data, 0, &i))
655 _dbus_verbose ("%s: no space separator in client response\n",
656 DBUS_AUTH_NAME (auth));
657 return send_rejected (auth);
660 if (!_dbus_string_init (&client_challenge))
663 if (!_dbus_string_init (&client_hash))
666 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
670 _dbus_string_skip_blank (data, i, &i);
672 if (!_dbus_string_copy_len (data, i,
673 _dbus_string_get_length (data) - i,
678 if (_dbus_string_get_length (&client_challenge) == 0 ||
679 _dbus_string_get_length (&client_hash) == 0)
681 _dbus_verbose ("%s: zero-length client challenge or hash\n",
682 DBUS_AUTH_NAME (auth));
683 if (send_rejected (auth))
688 if (!_dbus_string_init (&correct_hash))
691 if (!sha1_compute_hash (auth, auth->cookie_id,
697 /* if cookie_id was invalid, then we get an empty hash */
698 if (_dbus_string_get_length (&correct_hash) == 0)
700 if (send_rejected (auth))
705 if (!_dbus_string_equal (&client_hash, &correct_hash))
707 if (send_rejected (auth))
715 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
716 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
718 auth->authorized_identity = auth->desired_identity;
722 _dbus_string_zero (&correct_hash);
723 _dbus_string_free (&correct_hash);
725 _dbus_string_zero (&client_hash);
726 _dbus_string_free (&client_hash);
728 _dbus_string_free (&client_challenge);
734 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
735 const DBusString *data)
737 if (auth->cookie_id < 0)
738 return sha1_handle_first_client_response (auth, data);
740 return sha1_handle_second_client_response (auth, data);
744 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
746 auth->cookie_id = -1;
747 _dbus_string_set_length (&auth->challenge, 0);
751 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
752 DBusString *response)
754 const DBusString *username;
759 if (!_dbus_username_from_current_process (&username))
762 if (!_dbus_string_hex_encode (username, 0,
764 _dbus_string_get_length (response)))
774 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
775 const DBusString *data)
777 /* The data we get from the server should be the cookie context
778 * name, the cookie ID, and the server challenge, separated by
779 * spaces. We send back our challenge string and the correct hash.
783 DBusString cookie_id_str;
784 DBusString server_challenge;
785 DBusString client_challenge;
786 DBusString correct_hash;
793 if (!_dbus_string_find_blank (data, 0, &i))
795 if (send_error (auth,
796 "Server did not send context/ID/challenge properly"))
801 if (!_dbus_string_init (&context))
804 if (!_dbus_string_copy_len (data, 0, i,
808 _dbus_string_skip_blank (data, i, &i);
809 if (!_dbus_string_find_blank (data, i, &j))
811 if (send_error (auth,
812 "Server did not send context/ID/challenge properly"))
817 if (!_dbus_string_init (&cookie_id_str))
820 if (!_dbus_string_copy_len (data, i, j - i,
824 if (!_dbus_string_init (&server_challenge))
828 _dbus_string_skip_blank (data, i, &i);
829 j = _dbus_string_get_length (data);
831 if (!_dbus_string_copy_len (data, i, j - i,
832 &server_challenge, 0))
835 if (!_dbus_keyring_validate_context (&context))
837 if (send_error (auth, "Server sent invalid cookie context"))
842 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
844 if (send_error (auth, "Could not parse cookie ID as an integer"))
849 if (_dbus_string_get_length (&server_challenge) == 0)
851 if (send_error (auth, "Empty server challenge string"))
856 if (auth->keyring == NULL)
860 dbus_error_init (&error);
861 auth->keyring = _dbus_keyring_new_homedir (NULL,
865 if (auth->keyring == NULL)
867 if (dbus_error_has_name (&error,
868 DBUS_ERROR_NO_MEMORY))
870 dbus_error_free (&error);
875 _DBUS_ASSERT_ERROR_IS_SET (&error);
877 _dbus_verbose ("%s: Error loading keyring: %s\n",
878 DBUS_AUTH_NAME (auth), error.message);
880 if (send_error (auth, "Could not load cookie file"))
881 retval = TRUE; /* retval is only about mem */
883 dbus_error_free (&error);
889 _dbus_assert (!dbus_error_is_set (&error));
893 _dbus_assert (auth->keyring != NULL);
895 if (!_dbus_string_init (&tmp))
898 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
901 if (!_dbus_string_init (&client_challenge))
904 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
907 if (!_dbus_string_init (&correct_hash))
910 if (!sha1_compute_hash (auth, val,
916 if (_dbus_string_get_length (&correct_hash) == 0)
918 /* couldn't find the cookie ID or something */
919 if (send_error (auth, "Don't have the requested cookie ID"))
924 _dbus_string_set_length (&tmp, 0);
926 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
927 _dbus_string_get_length (&tmp)))
930 if (!_dbus_string_append (&tmp, " "))
933 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
934 _dbus_string_get_length (&tmp)))
937 if (!send_data (auth, &tmp))
943 _dbus_string_zero (&correct_hash);
944 _dbus_string_free (&correct_hash);
946 _dbus_string_free (&client_challenge);
948 _dbus_string_zero (&tmp);
949 _dbus_string_free (&tmp);
951 _dbus_string_free (&server_challenge);
953 _dbus_string_free (&cookie_id_str);
955 _dbus_string_free (&context);
961 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
963 auth->cookie_id = -1;
964 _dbus_string_set_length (&auth->challenge, 0);
968 handle_server_data_external_mech (DBusAuth *auth,
969 const DBusString *data)
971 if (auth->credentials.uid == DBUS_UID_UNSET)
973 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
974 DBUS_AUTH_NAME (auth));
975 return send_rejected (auth);
978 if (_dbus_string_get_length (data) > 0)
980 if (_dbus_string_get_length (&auth->identity) > 0)
982 /* Tried to send two auth identities, wtf */
983 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
984 DBUS_AUTH_NAME (auth));
985 return send_rejected (auth);
989 /* this is our auth identity */
990 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
995 /* Poke client for an auth identity, if none given */
996 if (_dbus_string_get_length (&auth->identity) == 0 &&
997 !auth->already_asked_for_initial_response)
999 if (send_data (auth, NULL))
1001 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1002 DBUS_AUTH_NAME (auth));
1003 auth->already_asked_for_initial_response = TRUE;
1010 _dbus_credentials_clear (&auth->desired_identity);
1012 /* If auth->identity is still empty here, then client
1013 * responded with an empty string after we poked it for
1014 * an initial response. This means to try to auth the
1015 * identity provided in the credentials.
1017 if (_dbus_string_get_length (&auth->identity) == 0)
1019 auth->desired_identity.uid = auth->credentials.uid;
1023 if (!_dbus_parse_uid (&auth->identity,
1024 &auth->desired_identity.uid))
1026 _dbus_verbose ("%s: could not get credentials from uid string\n",
1027 DBUS_AUTH_NAME (auth));
1028 return send_rejected (auth);
1032 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1034 _dbus_verbose ("%s: desired user %s is no good\n",
1035 DBUS_AUTH_NAME (auth),
1036 _dbus_string_get_const_data (&auth->identity));
1037 return send_rejected (auth);
1040 if (_dbus_credentials_match (&auth->desired_identity,
1041 &auth->credentials))
1043 /* client has authenticated */
1044 if (!send_ok (auth))
1047 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1048 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1049 DBUS_AUTH_NAME (auth),
1050 auth->desired_identity.uid,
1051 auth->credentials.uid);
1053 auth->authorized_identity.pid = auth->credentials.pid;
1054 auth->authorized_identity.uid = auth->desired_identity.uid;
1059 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1060 " gid="DBUS_GID_FORMAT
1061 " do not allow uid="DBUS_UID_FORMAT
1062 " gid="DBUS_GID_FORMAT"\n",
1063 DBUS_AUTH_NAME (auth),
1064 auth->credentials.uid, auth->credentials.gid,
1065 auth->desired_identity.uid, auth->desired_identity.gid);
1066 return send_rejected (auth);
1071 handle_server_shutdown_external_mech (DBusAuth *auth)
1077 handle_client_initial_response_external_mech (DBusAuth *auth,
1078 DBusString *response)
1080 /* We always append our UID as an initial response, so the server
1081 * doesn't have to send back an empty challenge to check whether we
1082 * want to specify an identity. i.e. this avoids a round trip that
1083 * the spec for the EXTERNAL mechanism otherwise requires.
1085 DBusString plaintext;
1087 if (!_dbus_string_init (&plaintext))
1090 if (!_dbus_string_append_uint (&plaintext,
1094 if (!_dbus_string_hex_encode (&plaintext, 0,
1096 _dbus_string_get_length (response)))
1099 _dbus_string_free (&plaintext);
1104 _dbus_string_free (&plaintext);
1109 handle_client_data_external_mech (DBusAuth *auth,
1110 const DBusString *data)
1117 handle_client_shutdown_external_mech (DBusAuth *auth)
1122 /* Put mechanisms here in order of preference.
1123 * What I eventually want to have is:
1125 * - a mechanism that checks UNIX domain socket credentials
1126 * - a simple magic cookie mechanism like X11 or ICE
1127 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1128 * offers such as Kerberos, X509, whatever.
1131 static const DBusAuthMechanismHandler
1132 all_mechanisms[] = {
1134 handle_server_data_external_mech,
1136 handle_server_shutdown_external_mech,
1137 handle_client_initial_response_external_mech,
1138 handle_client_data_external_mech,
1140 handle_client_shutdown_external_mech },
1141 { "DBUS_COOKIE_SHA1",
1142 handle_server_data_cookie_sha1_mech,
1144 handle_server_shutdown_cookie_sha1_mech,
1145 handle_client_initial_response_cookie_sha1_mech,
1146 handle_client_data_cookie_sha1_mech,
1148 handle_client_shutdown_cookie_sha1_mech },
1152 static const DBusAuthMechanismHandler*
1153 find_mech (const DBusString *name,
1154 char **allowed_mechs)
1158 if (allowed_mechs != NULL &&
1159 !_dbus_string_array_contains ((const char**) allowed_mechs,
1160 _dbus_string_get_const_data (name)))
1164 while (all_mechanisms[i].mechanism != NULL)
1166 if (_dbus_string_equal_c_str (name,
1167 all_mechanisms[i].mechanism))
1169 return &all_mechanisms[i];
1178 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1180 DBusString auth_command;
1182 if (!_dbus_string_init (&auth_command))
1185 if (!_dbus_string_append (&auth_command,
1188 _dbus_string_free (&auth_command);
1192 if (!_dbus_string_append (&auth_command,
1195 _dbus_string_free (&auth_command);
1199 if (mech->client_initial_response_func != NULL)
1201 if (!_dbus_string_append (&auth_command, " "))
1203 _dbus_string_free (&auth_command);
1207 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1209 _dbus_string_free (&auth_command);
1214 if (!_dbus_string_append (&auth_command,
1217 _dbus_string_free (&auth_command);
1221 if (!_dbus_string_copy (&auth_command, 0,
1223 _dbus_string_get_length (&auth->outgoing)))
1225 _dbus_string_free (&auth_command);
1229 _dbus_string_free (&auth_command);
1230 shutdown_mech (auth);
1232 goto_state (auth, &client_state_waiting_for_data);
1238 send_data (DBusAuth *auth, DBusString *data)
1242 if (data == NULL || _dbus_string_get_length (data) == 0)
1243 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1246 old_len = _dbus_string_get_length (&auth->outgoing);
1247 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1250 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1251 _dbus_string_get_length (&auth->outgoing)))
1254 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1260 _dbus_string_set_length (&auth->outgoing, old_len);
1267 send_rejected (DBusAuth *auth)
1270 DBusAuthServer *server_auth;
1273 if (!_dbus_string_init (&command))
1276 if (!_dbus_string_append (&command,
1281 while (all_mechanisms[i].mechanism != NULL)
1283 if (!_dbus_string_append (&command,
1287 if (!_dbus_string_append (&command,
1288 all_mechanisms[i].mechanism))
1294 if (!_dbus_string_append (&command, "\r\n"))
1297 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1298 _dbus_string_get_length (&auth->outgoing)))
1301 shutdown_mech (auth);
1303 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1304 server_auth = DBUS_AUTH_SERVER (auth);
1305 server_auth->failures += 1;
1307 if (server_auth->failures >= server_auth->max_failures)
1308 goto_state (auth, &common_state_need_disconnect);
1310 goto_state (auth, &server_state_waiting_for_auth);
1312 _dbus_string_free (&command);
1317 _dbus_string_free (&command);
1322 send_error (DBusAuth *auth, const char *message)
1324 return _dbus_string_append_printf (&auth->outgoing,
1325 "ERROR \"%s\"\r\n", message);
1329 send_ok (DBusAuth *auth)
1333 orig_len = _dbus_string_get_length (&auth->outgoing);
1335 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1336 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1339 _dbus_string_get_length (&auth->outgoing)) &&
1340 _dbus_string_append (&auth->outgoing, "\r\n"))
1342 goto_state (auth, &server_state_waiting_for_begin);
1347 _dbus_string_set_length (&auth->outgoing, orig_len);
1353 send_begin (DBusAuth *auth,
1354 const DBusString *args_from_ok)
1358 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1359 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1361 /* We decode the hex string to binary, using guid_from_server as scratch... */
1364 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1365 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1368 /* now clear out the scratch */
1369 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1371 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1374 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1375 end_of_hex, _dbus_string_get_length (args_from_ok));
1376 goto_state (auth, &common_state_need_disconnect);
1380 if (_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0) &&
1381 _dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
1383 _dbus_verbose ("Got GUID '%s' from the server\n",
1384 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1386 goto_state (auth, &common_state_authenticated);
1391 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1397 send_cancel (DBusAuth *auth)
1399 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1401 goto_state (auth, &client_state_waiting_for_reject);
1409 process_data (DBusAuth *auth,
1410 const DBusString *args,
1411 DBusAuthDataFunction data_func)
1416 if (!_dbus_string_init (&decoded))
1419 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1421 _dbus_string_free (&decoded);
1425 if (_dbus_string_get_length (args) != end)
1427 _dbus_string_free (&decoded);
1428 if (!send_error (auth, "Invalid hex encoding"))
1434 #ifdef DBUS_ENABLE_VERBOSE_MODE
1435 if (_dbus_string_validate_ascii (&decoded, 0,
1436 _dbus_string_get_length (&decoded)))
1437 _dbus_verbose ("%s: data: '%s'\n",
1438 DBUS_AUTH_NAME (auth),
1439 _dbus_string_get_const_data (&decoded));
1442 if (!(* data_func) (auth, &decoded))
1444 _dbus_string_free (&decoded);
1448 _dbus_string_free (&decoded);
1453 handle_auth (DBusAuth *auth, const DBusString *args)
1455 if (_dbus_string_get_length (args) == 0)
1457 /* No args to the auth, send mechanisms */
1458 if (!send_rejected (auth))
1467 DBusString hex_response;
1469 _dbus_string_find_blank (args, 0, &i);
1471 if (!_dbus_string_init (&mech))
1474 if (!_dbus_string_init (&hex_response))
1476 _dbus_string_free (&mech);
1480 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1483 _dbus_string_skip_blank (args, i, &i);
1484 if (!_dbus_string_copy (args, i, &hex_response, 0))
1487 auth->mech = find_mech (&mech, auth->allowed_mechs);
1488 if (auth->mech != NULL)
1490 _dbus_verbose ("%s: Trying mechanism %s\n",
1491 DBUS_AUTH_NAME (auth),
1492 auth->mech->mechanism);
1494 if (!process_data (auth, &hex_response,
1495 auth->mech->server_data_func))
1500 /* Unsupported mechanism */
1501 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1502 DBUS_AUTH_NAME (auth),
1503 _dbus_string_get_const_data (&mech));
1505 if (!send_rejected (auth))
1509 _dbus_string_free (&mech);
1510 _dbus_string_free (&hex_response);
1516 _dbus_string_free (&mech);
1517 _dbus_string_free (&hex_response);
1523 handle_server_state_waiting_for_auth (DBusAuth *auth,
1524 DBusAuthCommand command,
1525 const DBusString *args)
1529 case DBUS_AUTH_COMMAND_AUTH:
1530 return handle_auth (auth, args);
1532 case DBUS_AUTH_COMMAND_CANCEL:
1533 case DBUS_AUTH_COMMAND_DATA:
1534 return send_error (auth, "Not currently in an auth conversation");
1536 case DBUS_AUTH_COMMAND_BEGIN:
1537 goto_state (auth, &common_state_need_disconnect);
1540 case DBUS_AUTH_COMMAND_ERROR:
1541 return send_rejected (auth);
1543 case DBUS_AUTH_COMMAND_REJECTED:
1544 case DBUS_AUTH_COMMAND_OK:
1545 case DBUS_AUTH_COMMAND_UNKNOWN:
1547 return send_error (auth, "Unknown command");
1552 handle_server_state_waiting_for_data (DBusAuth *auth,
1553 DBusAuthCommand command,
1554 const DBusString *args)
1558 case DBUS_AUTH_COMMAND_AUTH:
1559 return send_error (auth, "Sent AUTH while another AUTH in progress");
1561 case DBUS_AUTH_COMMAND_CANCEL:
1562 case DBUS_AUTH_COMMAND_ERROR:
1563 return send_rejected (auth);
1565 case DBUS_AUTH_COMMAND_DATA:
1566 return process_data (auth, args, auth->mech->server_data_func);
1568 case DBUS_AUTH_COMMAND_BEGIN:
1569 goto_state (auth, &common_state_need_disconnect);
1572 case DBUS_AUTH_COMMAND_REJECTED:
1573 case DBUS_AUTH_COMMAND_OK:
1574 case DBUS_AUTH_COMMAND_UNKNOWN:
1576 return send_error (auth, "Unknown command");
1581 handle_server_state_waiting_for_begin (DBusAuth *auth,
1582 DBusAuthCommand command,
1583 const DBusString *args)
1587 case DBUS_AUTH_COMMAND_AUTH:
1588 return send_error (auth, "Sent AUTH while expecting BEGIN");
1590 case DBUS_AUTH_COMMAND_DATA:
1591 return send_error (auth, "Sent DATA while expecting BEGIN");
1593 case DBUS_AUTH_COMMAND_BEGIN:
1594 goto_state (auth, &common_state_authenticated);
1597 case DBUS_AUTH_COMMAND_REJECTED:
1598 case DBUS_AUTH_COMMAND_OK:
1599 case DBUS_AUTH_COMMAND_UNKNOWN:
1601 return send_error (auth, "Unknown command");
1603 case DBUS_AUTH_COMMAND_CANCEL:
1604 case DBUS_AUTH_COMMAND_ERROR:
1605 return send_rejected (auth);
1609 /* return FALSE if no memory, TRUE if all OK */
1611 get_word (const DBusString *str,
1617 _dbus_string_skip_blank (str, *start, start);
1618 _dbus_string_find_blank (str, *start, &i);
1622 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1632 record_mechanisms (DBusAuth *auth,
1633 const DBusString *args)
1638 if (auth->already_got_mechanisms)
1641 len = _dbus_string_get_length (args);
1647 const DBusAuthMechanismHandler *mech;
1649 if (!_dbus_string_init (&m))
1652 if (!get_word (args, &next, &m))
1654 _dbus_string_free (&m);
1658 mech = find_mech (&m, auth->allowed_mechs);
1662 /* FIXME right now we try mechanisms in the order
1663 * the server lists them; should we do them in
1664 * some more deterministic order?
1666 * Probably in all_mechanisms order, our order of
1667 * preference. Of course when the server is us,
1668 * it lists things in that order anyhow.
1671 if (mech != &all_mechanisms[0])
1673 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1674 DBUS_AUTH_NAME (auth), mech->mechanism);
1676 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1679 _dbus_string_free (&m);
1685 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1686 DBUS_AUTH_NAME (auth), mech->mechanism);
1691 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1692 DBUS_AUTH_NAME (auth),
1693 _dbus_string_get_const_data (&m));
1696 _dbus_string_free (&m);
1699 auth->already_got_mechanisms = TRUE;
1704 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1710 process_rejected (DBusAuth *auth, const DBusString *args)
1712 const DBusAuthMechanismHandler *mech;
1713 DBusAuthClient *client;
1715 client = DBUS_AUTH_CLIENT (auth);
1717 if (!auth->already_got_mechanisms)
1719 if (!record_mechanisms (auth, args))
1723 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1725 mech = client->mechs_to_try->data;
1727 if (!send_auth (auth, mech))
1730 _dbus_list_pop_first (&client->mechs_to_try);
1732 _dbus_verbose ("%s: Trying mechanism %s\n",
1733 DBUS_AUTH_NAME (auth),
1739 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1740 DBUS_AUTH_NAME (auth));
1741 goto_state (auth, &common_state_need_disconnect);
1749 handle_client_state_waiting_for_data (DBusAuth *auth,
1750 DBusAuthCommand command,
1751 const DBusString *args)
1753 _dbus_assert (auth->mech != NULL);
1757 case DBUS_AUTH_COMMAND_DATA:
1758 return process_data (auth, args, auth->mech->client_data_func);
1760 case DBUS_AUTH_COMMAND_REJECTED:
1761 return process_rejected (auth, args);
1763 case DBUS_AUTH_COMMAND_OK:
1764 return send_begin (auth, args);
1766 case DBUS_AUTH_COMMAND_ERROR:
1767 return send_cancel (auth);
1769 case DBUS_AUTH_COMMAND_AUTH:
1770 case DBUS_AUTH_COMMAND_CANCEL:
1771 case DBUS_AUTH_COMMAND_BEGIN:
1772 case DBUS_AUTH_COMMAND_UNKNOWN:
1774 return send_error (auth, "Unknown command");
1779 handle_client_state_waiting_for_ok (DBusAuth *auth,
1780 DBusAuthCommand command,
1781 const DBusString *args)
1785 case DBUS_AUTH_COMMAND_REJECTED:
1786 return process_rejected (auth, args);
1788 case DBUS_AUTH_COMMAND_OK:
1789 return send_begin (auth, args);
1791 case DBUS_AUTH_COMMAND_DATA:
1792 case DBUS_AUTH_COMMAND_ERROR:
1793 return send_cancel (auth);
1795 case DBUS_AUTH_COMMAND_AUTH:
1796 case DBUS_AUTH_COMMAND_CANCEL:
1797 case DBUS_AUTH_COMMAND_BEGIN:
1798 case DBUS_AUTH_COMMAND_UNKNOWN:
1800 return send_error (auth, "Unknown command");
1805 handle_client_state_waiting_for_reject (DBusAuth *auth,
1806 DBusAuthCommand command,
1807 const DBusString *args)
1811 case DBUS_AUTH_COMMAND_REJECTED:
1812 return process_rejected (auth, args);
1814 case DBUS_AUTH_COMMAND_AUTH:
1815 case DBUS_AUTH_COMMAND_CANCEL:
1816 case DBUS_AUTH_COMMAND_DATA:
1817 case DBUS_AUTH_COMMAND_BEGIN:
1818 case DBUS_AUTH_COMMAND_OK:
1819 case DBUS_AUTH_COMMAND_ERROR:
1820 case DBUS_AUTH_COMMAND_UNKNOWN:
1822 goto_state (auth, &common_state_need_disconnect);
1828 * Mapping from command name to enum
1831 const char *name; /**< Name of the command */
1832 DBusAuthCommand command; /**< Corresponding enum */
1833 } DBusAuthCommandName;
1835 static const DBusAuthCommandName auth_command_names[] = {
1836 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
1837 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
1838 { "DATA", DBUS_AUTH_COMMAND_DATA },
1839 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
1840 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
1841 { "OK", DBUS_AUTH_COMMAND_OK },
1842 { "ERROR", DBUS_AUTH_COMMAND_ERROR }
1845 static DBusAuthCommand
1846 lookup_command_from_name (DBusString *command)
1850 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
1852 if (_dbus_string_equal_c_str (command,
1853 auth_command_names[i].name))
1854 return auth_command_names[i].command;
1857 return DBUS_AUTH_COMMAND_UNKNOWN;
1861 goto_state (DBusAuth *auth, const DBusAuthStateData *state)
1863 _dbus_verbose ("%s: going from state %s to state %s\n",
1864 DBUS_AUTH_NAME (auth),
1868 auth->state = state;
1871 /* returns whether to call it again right away */
1873 process_command (DBusAuth *auth)
1875 DBusAuthCommand command;
1882 /* _dbus_verbose ("%s: trying process_command()\n"); */
1887 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1890 if (!_dbus_string_init (&line))
1892 auth->needed_memory = TRUE;
1896 if (!_dbus_string_init (&args))
1898 _dbus_string_free (&line);
1899 auth->needed_memory = TRUE;
1903 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
1906 if (!_dbus_string_validate_ascii (&line, 0,
1907 _dbus_string_get_length (&line)))
1909 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1910 DBUS_AUTH_NAME (auth));
1911 if (!send_error (auth, "Command contained non-ASCII"))
1917 _dbus_verbose ("%s: got command \"%s\"\n",
1918 DBUS_AUTH_NAME (auth),
1919 _dbus_string_get_const_data (&line));
1921 _dbus_string_find_blank (&line, 0, &i);
1922 _dbus_string_skip_blank (&line, i, &j);
1925 _dbus_string_delete (&line, i, j - i);
1927 if (!_dbus_string_move (&line, i, &args, 0))
1930 /* FIXME we should probably validate that only the allowed
1931 * chars are in the command name
1934 command = lookup_command_from_name (&line);
1935 if (!(* auth->state->handler) (auth, command, &args))
1940 /* We've succeeded in processing the whole command so drop it out
1941 * of the incoming buffer and return TRUE to try another command.
1944 _dbus_string_delete (&auth->incoming, 0, eol);
1947 _dbus_string_delete (&auth->incoming, 0, 2);
1952 _dbus_string_free (&args);
1953 _dbus_string_free (&line);
1956 auth->needed_memory = TRUE;
1958 auth->needed_memory = FALSE;
1967 * @addtogroup DBusAuth
1972 * Creates a new auth conversation object for the server side.
1973 * See doc/dbus-sasl-profile.txt for full details on what
1976 * @returns the new object or #NULL if no memory
1979 _dbus_auth_server_new (const DBusString *guid)
1982 DBusAuthServer *server_auth;
1983 DBusString guid_copy;
1985 if (!_dbus_string_init (&guid_copy))
1988 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
1990 _dbus_string_free (&guid_copy);
1994 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1997 _dbus_string_free (&guid_copy);
2001 auth->side = auth_side_server;
2002 auth->state = &server_state_waiting_for_auth;
2004 server_auth = DBUS_AUTH_SERVER (auth);
2006 server_auth->guid = guid_copy;
2008 /* perhaps this should be per-mechanism with a lower
2011 server_auth->failures = 0;
2012 server_auth->max_failures = 6;
2018 * Creates a new auth conversation object for the client side.
2019 * See doc/dbus-sasl-profile.txt for full details on what
2022 * @returns the new object or #NULL if no memory
2025 _dbus_auth_client_new (void)
2028 DBusString guid_str;
2030 if (!_dbus_string_init (&guid_str))
2033 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2036 _dbus_string_free (&guid_str);
2040 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2042 auth->side = auth_side_client;
2043 auth->state = &client_state_need_send_auth;
2045 /* Start the auth conversation by sending AUTH for our default
2047 if (!send_auth (auth, &all_mechanisms[0]))
2049 _dbus_auth_unref (auth);
2057 * Increments the refcount of an auth object.
2059 * @param auth the auth conversation
2060 * @returns the auth conversation
2063 _dbus_auth_ref (DBusAuth *auth)
2065 _dbus_assert (auth != NULL);
2067 auth->refcount += 1;
2073 * Decrements the refcount of an auth object.
2075 * @param auth the auth conversation
2078 _dbus_auth_unref (DBusAuth *auth)
2080 _dbus_assert (auth != NULL);
2081 _dbus_assert (auth->refcount > 0);
2083 auth->refcount -= 1;
2084 if (auth->refcount == 0)
2086 shutdown_mech (auth);
2088 if (DBUS_AUTH_IS_CLIENT (auth))
2090 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2091 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2095 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2097 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2101 _dbus_keyring_unref (auth->keyring);
2103 _dbus_string_free (&auth->context);
2104 _dbus_string_free (&auth->challenge);
2105 _dbus_string_free (&auth->identity);
2106 _dbus_string_free (&auth->incoming);
2107 _dbus_string_free (&auth->outgoing);
2109 dbus_free_string_array (auth->allowed_mechs);
2116 * Sets an array of authentication mechanism names
2117 * that we are willing to use.
2119 * @param auth the auth conversation
2120 * @param mechanisms #NULL-terminated array of mechanism names
2121 * @returns #FALSE if no memory
2124 _dbus_auth_set_mechanisms (DBusAuth *auth,
2125 const char **mechanisms)
2129 if (mechanisms != NULL)
2131 copy = _dbus_dup_string_array (mechanisms);
2138 dbus_free_string_array (auth->allowed_mechs);
2140 auth->allowed_mechs = copy;
2146 * @param auth the auth conversation object
2147 * @returns #TRUE if we're in a final state
2149 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2152 * Analyzes buffered input and moves the auth conversation forward,
2153 * returning the new state of the auth conversation.
2155 * @param auth the auth conversation
2156 * @returns the new state
2159 _dbus_auth_do_work (DBusAuth *auth)
2161 auth->needed_memory = FALSE;
2163 /* Max amount we'll buffer up before deciding someone's on crack */
2164 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2168 if (DBUS_AUTH_IN_END_STATE (auth))
2171 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2172 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2174 goto_state (auth, &common_state_need_disconnect);
2175 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2176 DBUS_AUTH_NAME (auth));
2180 while (process_command (auth));
2182 if (auth->needed_memory)
2183 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2184 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2185 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2186 else if (auth->state == &common_state_need_disconnect)
2187 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2188 else if (auth->state == &common_state_authenticated)
2189 return DBUS_AUTH_STATE_AUTHENTICATED;
2190 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2194 * Gets bytes that need to be sent to the peer we're conversing with.
2195 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2196 * to notify the auth object that they were written.
2198 * @param auth the auth conversation
2199 * @param str return location for a ref to the buffer to send
2200 * @returns #FALSE if nothing to send
2203 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2204 const DBusString **str)
2206 _dbus_assert (auth != NULL);
2207 _dbus_assert (str != NULL);
2211 if (_dbus_string_get_length (&auth->outgoing) == 0)
2214 *str = &auth->outgoing;
2220 * Notifies the auth conversation object that
2221 * the given number of bytes of the outgoing buffer
2222 * have been written out.
2224 * @param auth the auth conversation
2225 * @param bytes_sent number of bytes written out
2228 _dbus_auth_bytes_sent (DBusAuth *auth,
2231 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2232 DBUS_AUTH_NAME (auth),
2234 _dbus_string_get_const_data (&auth->outgoing));
2236 _dbus_string_delete (&auth->outgoing,
2241 * Get a buffer to be used for reading bytes from the peer we're conversing
2242 * with. Bytes should be appended to this buffer.
2244 * @param auth the auth conversation
2245 * @param buffer return location for buffer to append bytes to
2248 _dbus_auth_get_buffer (DBusAuth *auth,
2249 DBusString **buffer)
2251 _dbus_assert (auth != NULL);
2252 _dbus_assert (!auth->buffer_outstanding);
2254 *buffer = &auth->incoming;
2256 auth->buffer_outstanding = TRUE;
2260 * Returns a buffer with new data read into it.
2262 * @param auth the auth conversation
2263 * @param buffer the buffer being returned
2264 * @param bytes_read number of new bytes added
2267 _dbus_auth_return_buffer (DBusAuth *auth,
2271 _dbus_assert (buffer == &auth->incoming);
2272 _dbus_assert (auth->buffer_outstanding);
2274 auth->buffer_outstanding = FALSE;
2278 * Returns leftover bytes that were not used as part of the auth
2279 * conversation. These bytes will be part of the message stream
2280 * instead. This function may not be called until authentication has
2283 * @param auth the auth conversation
2284 * @param str return location for pointer to string of unused bytes
2287 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2288 const DBusString **str)
2290 if (!DBUS_AUTH_IN_END_STATE (auth))
2293 *str = &auth->incoming;
2298 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2299 * after we've gotten them and successfully moved them elsewhere.
2301 * @param auth the auth conversation
2304 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2306 if (!DBUS_AUTH_IN_END_STATE (auth))
2309 _dbus_string_set_length (&auth->incoming, 0);
2313 * Called post-authentication, indicates whether we need to encode
2314 * the message stream with _dbus_auth_encode_data() prior to
2315 * sending it to the peer.
2317 * @param auth the auth conversation
2318 * @returns #TRUE if we need to encode the stream
2321 _dbus_auth_needs_encoding (DBusAuth *auth)
2323 if (auth->state != &common_state_authenticated)
2326 if (auth->mech != NULL)
2328 if (DBUS_AUTH_IS_CLIENT (auth))
2329 return auth->mech->client_encode_func != NULL;
2331 return auth->mech->server_encode_func != NULL;
2338 * Called post-authentication, encodes a block of bytes for sending to
2339 * the peer. If no encoding was negotiated, just copies the bytes
2340 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2342 * @param auth the auth conversation
2343 * @param plaintext the plain text data
2344 * @param encoded initialized string to where encoded data is appended
2345 * @returns #TRUE if we had enough memory and successfully encoded
2348 _dbus_auth_encode_data (DBusAuth *auth,
2349 const DBusString *plaintext,
2350 DBusString *encoded)
2352 _dbus_assert (plaintext != encoded);
2354 if (auth->state != &common_state_authenticated)
2357 if (_dbus_auth_needs_encoding (auth))
2359 if (DBUS_AUTH_IS_CLIENT (auth))
2360 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2362 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2366 return _dbus_string_copy (plaintext, 0, encoded,
2367 _dbus_string_get_length (encoded));
2372 * Called post-authentication, indicates whether we need to decode
2373 * the message stream with _dbus_auth_decode_data() after
2374 * receiving it from the peer.
2376 * @param auth the auth conversation
2377 * @returns #TRUE if we need to encode the stream
2380 _dbus_auth_needs_decoding (DBusAuth *auth)
2382 if (auth->state != &common_state_authenticated)
2385 if (auth->mech != NULL)
2387 if (DBUS_AUTH_IS_CLIENT (auth))
2388 return auth->mech->client_decode_func != NULL;
2390 return auth->mech->server_decode_func != NULL;
2398 * Called post-authentication, decodes a block of bytes received from
2399 * the peer. If no encoding was negotiated, just copies the bytes (you
2400 * can avoid this by checking _dbus_auth_needs_decoding()).
2402 * @todo We need to be able to distinguish "out of memory" error
2403 * from "the data is hosed" error.
2405 * @param auth the auth conversation
2406 * @param encoded the encoded data
2407 * @param plaintext initialized string where decoded data is appended
2408 * @returns #TRUE if we had enough memory and successfully decoded
2411 _dbus_auth_decode_data (DBusAuth *auth,
2412 const DBusString *encoded,
2413 DBusString *plaintext)
2415 _dbus_assert (plaintext != encoded);
2417 if (auth->state != &common_state_authenticated)
2420 if (_dbus_auth_needs_decoding (auth))
2422 if (DBUS_AUTH_IS_CLIENT (auth))
2423 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2425 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2429 return _dbus_string_copy (encoded, 0, plaintext,
2430 _dbus_string_get_length (plaintext));
2435 * Sets credentials received via reliable means from the operating
2438 * @param auth the auth conversation
2439 * @param credentials the credentials received
2442 _dbus_auth_set_credentials (DBusAuth *auth,
2443 const DBusCredentials *credentials)
2445 auth->credentials = *credentials;
2449 * Gets the identity we authorized the client as. Apps may have
2450 * different policies as to what identities they allow.
2452 * @param auth the auth conversation
2453 * @param credentials the credentials we've authorized
2456 _dbus_auth_get_identity (DBusAuth *auth,
2457 DBusCredentials *credentials)
2459 if (auth->state == &common_state_authenticated)
2460 *credentials = auth->authorized_identity;
2462 _dbus_credentials_clear (credentials);
2466 * Gets the GUID from the server if we've authenticated; gets
2468 * @param auth the auth object
2469 * @returns the GUID in ASCII hex format
2472 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2474 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2476 if (auth->state == &common_state_authenticated)
2477 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2483 * Sets the "authentication context" which scopes cookies
2484 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2486 * @param auth the auth conversation
2487 * @param context the context
2488 * @returns #FALSE if no memory
2491 _dbus_auth_set_context (DBusAuth *auth,
2492 const DBusString *context)
2494 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2495 &auth->context, 0, _dbus_string_get_length (context));
2500 /* tests in dbus-auth-util.c */