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-credentials.h"
33 * @defgroup DBusAuth Authentication
34 * @ingroup DBusInternals
35 * @brief DBusAuth object
37 * DBusAuth manages the authentication negotiation when a connection
38 * is first established, and also manage any encryption used over a
41 * @todo some SASL profiles require sending the empty string as a
42 * challenge/response, but we don't currently allow that in our
45 * @todo right now sometimes both ends will block waiting for input
46 * from the other end, e.g. if there's an error during
49 * @todo the cookie keyring needs to be cached globally not just
50 * per-auth (which raises threadsafety issues too)
52 * @todo grep FIXME in dbus-auth.c
56 * @defgroup DBusAuthInternals Authentication implementation details
57 * @ingroup DBusInternals
58 * @brief DBusAuth implementation details
60 * Private details of authentication code.
66 * This function appends an initial client response to the given string
68 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
69 DBusString *response);
72 * This function processes a block of data received from the peer.
73 * i.e. handles a DATA command.
75 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
76 const DBusString *data);
79 * This function encodes a block of data from the peer.
81 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
82 const DBusString *data,
86 * This function decodes a block of data from the peer.
88 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
89 const DBusString *data,
93 * This function is called when the mechanism is abandoned.
95 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
98 * Virtual table representing a particular auth mechanism.
102 const char *mechanism; /**< Name of the mechanism */
103 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
104 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
105 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
106 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
107 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
108 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
109 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
110 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
111 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
112 } DBusAuthMechanismHandler;
115 * Enumeration for the known authentication commands.
118 DBUS_AUTH_COMMAND_AUTH,
119 DBUS_AUTH_COMMAND_CANCEL,
120 DBUS_AUTH_COMMAND_DATA,
121 DBUS_AUTH_COMMAND_BEGIN,
122 DBUS_AUTH_COMMAND_REJECTED,
123 DBUS_AUTH_COMMAND_OK,
124 DBUS_AUTH_COMMAND_ERROR,
125 DBUS_AUTH_COMMAND_UNKNOWN
129 * Auth state function, determines the reaction to incoming events for
130 * a particular state. Returns whether we had enough memory to
131 * complete the operation.
133 typedef dbus_bool_t (* DBusAuthStateFunction) (DBusAuth *auth,
134 DBusAuthCommand command,
135 const DBusString *args);
138 * Information about a auth state.
142 const char *name; /**< Name of the state */
143 DBusAuthStateFunction handler; /**< State function for this state */
147 * Internal members of DBusAuth.
151 int refcount; /**< reference count */
152 const char *side; /**< Client or server */
154 DBusString incoming; /**< Incoming data buffer */
155 DBusString outgoing; /**< Outgoing data buffer */
157 const DBusAuthStateData *state; /**< Current protocol state */
159 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
161 DBusString identity; /**< Current identity we're authorizing
165 DBusCredentials *credentials; /**< Credentials read from socket
168 DBusCredentials *authorized_identity; /**< Credentials that are authorized */
170 DBusCredentials *desired_identity; /**< Identity client has requested */
172 DBusString context; /**< Cookie scope */
173 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
174 int cookie_id; /**< ID of cookie to use */
175 DBusString challenge; /**< Challenge sent to client */
177 char **allowed_mechs; /**< Mechanisms we're allowed to use,
178 * or #NULL if we can use any
181 unsigned int needed_memory : 1; /**< We needed memory to continue since last
182 * successful getting something done
184 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
185 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
186 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
190 * "Subclass" of DBusAuth for client side
194 DBusAuth base; /**< Parent class */
196 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
198 DBusString guid_from_server; /**< GUID received from server */
203 * "Subclass" of DBusAuth for server side.
207 DBusAuth base; /**< Parent class */
209 int failures; /**< Number of times client has been rejected */
210 int max_failures; /**< Number of times we reject before disconnect */
212 DBusString guid; /**< Our globally unique ID in hex encoding */
216 static void goto_state (DBusAuth *auth,
217 const DBusAuthStateData *new_state);
218 static dbus_bool_t send_auth (DBusAuth *auth,
219 const DBusAuthMechanismHandler *mech);
220 static dbus_bool_t send_data (DBusAuth *auth,
222 static dbus_bool_t send_rejected (DBusAuth *auth);
223 static dbus_bool_t send_error (DBusAuth *auth,
224 const char *message);
225 static dbus_bool_t send_ok (DBusAuth *auth);
226 static dbus_bool_t send_begin (DBusAuth *auth,
227 const DBusString *args_from_ok);
228 static dbus_bool_t send_cancel (DBusAuth *auth);
234 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
235 DBusAuthCommand command,
236 const DBusString *args);
237 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
238 DBusAuthCommand command,
239 const DBusString *args);
240 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
241 DBusAuthCommand command,
242 const DBusString *args);
244 static const DBusAuthStateData server_state_waiting_for_auth = {
245 "WaitingForAuth", handle_server_state_waiting_for_auth
247 static const DBusAuthStateData server_state_waiting_for_data = {
248 "WaitingForData", handle_server_state_waiting_for_data
250 static const DBusAuthStateData server_state_waiting_for_begin = {
251 "WaitingForBegin", handle_server_state_waiting_for_begin
258 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
259 DBusAuthCommand command,
260 const DBusString *args);
261 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
262 DBusAuthCommand command,
263 const DBusString *args);
264 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
265 DBusAuthCommand command,
266 const DBusString *args);
268 static const DBusAuthStateData client_state_need_send_auth = {
271 static const DBusAuthStateData client_state_waiting_for_data = {
272 "WaitingForData", handle_client_state_waiting_for_data
274 static const DBusAuthStateData client_state_waiting_for_ok = {
275 "WaitingForOK", handle_client_state_waiting_for_ok
277 static const DBusAuthStateData client_state_waiting_for_reject = {
278 "WaitingForReject", handle_client_state_waiting_for_reject
282 * Common terminal states. Terminal states have handler == NULL.
285 static const DBusAuthStateData common_state_authenticated = {
286 "Authenticated", NULL
289 static const DBusAuthStateData common_state_need_disconnect = {
290 "NeedDisconnect", NULL
293 static const char auth_side_client[] = "client";
294 static const char auth_side_server[] = "server";
296 * @param auth the auth conversation
297 * @returns #TRUE if the conversation is the server side
299 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
301 * @param auth the auth conversation
302 * @returns #TRUE if the conversation is the client side
304 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
306 * @param auth the auth conversation
307 * @returns auth cast to DBusAuthClient
309 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
311 * @param auth the auth conversation
312 * @returns auth cast to DBusAuthServer
314 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
317 * The name of the auth ("client" or "server")
318 * @param auth the auth conversation
321 #define DBUS_AUTH_NAME(auth) ((auth)->side)
324 _dbus_auth_new (int size)
328 auth = dbus_malloc0 (size);
334 auth->keyring = NULL;
335 auth->cookie_id = -1;
337 /* note that we don't use the max string length feature,
338 * because you can't use that feature if you're going to
339 * try to recover from out-of-memory (it creates
340 * what looks like unrecoverable inability to alloc
341 * more space in the string). But we do handle
342 * overlong buffers in _dbus_auth_do_work().
345 if (!_dbus_string_init (&auth->incoming))
348 if (!_dbus_string_init (&auth->outgoing))
351 if (!_dbus_string_init (&auth->identity))
354 if (!_dbus_string_init (&auth->context))
357 if (!_dbus_string_init (&auth->challenge))
360 /* default context if none is specified */
361 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
364 auth->credentials = _dbus_credentials_new ();
365 if (auth->credentials == NULL)
368 auth->authorized_identity = _dbus_credentials_new ();
369 if (auth->authorized_identity == NULL)
372 auth->desired_identity = _dbus_credentials_new ();
373 if (auth->desired_identity == NULL)
380 _dbus_credentials_unref (auth->desired_identity);
383 _dbus_credentials_unref (auth->authorized_identity);
385 _dbus_credentials_unref (auth->credentials);
387 /* last alloc was an append to context, which is freed already below */ ;
389 _dbus_string_free (&auth->challenge);
391 _dbus_string_free (&auth->context);
393 _dbus_string_free (&auth->identity);
395 _dbus_string_free (&auth->outgoing);
397 _dbus_string_free (&auth->incoming);
404 shutdown_mech (DBusAuth *auth)
406 /* Cancel any auth */
407 auth->already_asked_for_initial_response = FALSE;
408 _dbus_string_set_length (&auth->identity, 0);
410 _dbus_credentials_clear (auth->authorized_identity);
411 _dbus_credentials_clear (auth->desired_identity);
413 if (auth->mech != NULL)
415 _dbus_verbose ("%s: Shutting down mechanism %s\n",
416 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
418 if (DBUS_AUTH_IS_CLIENT (auth))
419 (* auth->mech->client_shutdown_func) (auth);
421 (* auth->mech->server_shutdown_func) (auth);
427 /* Returns TRUE but with an empty string hash if the
428 * cookie_id isn't known. As with all this code
429 * TRUE just means we had enough memory.
432 sha1_compute_hash (DBusAuth *auth,
434 const DBusString *server_challenge,
435 const DBusString *client_challenge,
442 _dbus_assert (auth->keyring != NULL);
446 if (!_dbus_string_init (&cookie))
449 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
453 if (_dbus_string_get_length (&cookie) == 0)
459 if (!_dbus_string_init (&to_hash))
462 if (!_dbus_string_copy (server_challenge, 0,
463 &to_hash, _dbus_string_get_length (&to_hash)))
466 if (!_dbus_string_append (&to_hash, ":"))
469 if (!_dbus_string_copy (client_challenge, 0,
470 &to_hash, _dbus_string_get_length (&to_hash)))
473 if (!_dbus_string_append (&to_hash, ":"))
476 if (!_dbus_string_copy (&cookie, 0,
477 &to_hash, _dbus_string_get_length (&to_hash)))
480 if (!_dbus_sha_compute (&to_hash, hash))
486 _dbus_string_zero (&to_hash);
487 _dbus_string_free (&to_hash);
489 _dbus_string_zero (&cookie);
490 _dbus_string_free (&cookie);
494 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
495 * entropy, we use 128. This is the number of bytes in the random
498 #define N_CHALLENGE_BYTES (128/8)
501 sha1_handle_first_client_response (DBusAuth *auth,
502 const DBusString *data)
504 /* We haven't sent a challenge yet, we're expecting a desired
505 * username from the client.
514 _dbus_string_set_length (&auth->challenge, 0);
516 if (_dbus_string_get_length (data) > 0)
518 if (_dbus_string_get_length (&auth->identity) > 0)
520 /* Tried to send two auth identities, wtf */
521 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
522 DBUS_AUTH_NAME (auth));
523 return send_rejected (auth);
527 /* this is our auth identity */
528 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
533 if (!_dbus_credentials_add_from_username (auth->desired_identity, data))
535 _dbus_verbose ("%s: Did not get a valid username from client\n",
536 DBUS_AUTH_NAME (auth));
537 return send_rejected (auth);
540 if (!_dbus_string_init (&tmp))
543 if (!_dbus_string_init (&tmp2))
545 _dbus_string_free (&tmp);
549 /* we cache the keyring for speed, so here we drop it if it's the
550 * wrong one. FIXME caching the keyring here is useless since we use
551 * a different DBusAuth for every connection.
554 !_dbus_keyring_is_for_user (auth->keyring,
557 _dbus_keyring_unref (auth->keyring);
558 auth->keyring = NULL;
561 if (auth->keyring == NULL)
563 dbus_error_init (&error);
564 auth->keyring = _dbus_keyring_new_homedir (data,
568 if (auth->keyring == NULL)
570 if (dbus_error_has_name (&error,
571 DBUS_ERROR_NO_MEMORY))
573 dbus_error_free (&error);
578 _DBUS_ASSERT_ERROR_IS_SET (&error);
579 _dbus_verbose ("%s: Error loading keyring: %s\n",
580 DBUS_AUTH_NAME (auth), error.message);
581 if (send_rejected (auth))
582 retval = TRUE; /* retval is only about mem */
583 dbus_error_free (&error);
589 _dbus_assert (!dbus_error_is_set (&error));
593 _dbus_assert (auth->keyring != NULL);
595 dbus_error_init (&error);
596 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
597 if (auth->cookie_id < 0)
599 _DBUS_ASSERT_ERROR_IS_SET (&error);
600 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
601 DBUS_AUTH_NAME (auth), error.message);
602 if (send_rejected (auth))
604 dbus_error_free (&error);
609 _dbus_assert (!dbus_error_is_set (&error));
612 if (!_dbus_string_copy (&auth->context, 0,
613 &tmp2, _dbus_string_get_length (&tmp2)))
616 if (!_dbus_string_append (&tmp2, " "))
619 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
622 if (!_dbus_string_append (&tmp2, " "))
625 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
628 _dbus_string_set_length (&auth->challenge, 0);
629 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
632 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
633 _dbus_string_get_length (&tmp2)))
636 if (!send_data (auth, &tmp2))
639 goto_state (auth, &server_state_waiting_for_data);
643 _dbus_string_zero (&tmp);
644 _dbus_string_free (&tmp);
645 _dbus_string_zero (&tmp2);
646 _dbus_string_free (&tmp2);
652 sha1_handle_second_client_response (DBusAuth *auth,
653 const DBusString *data)
655 /* We are expecting a response which is the hex-encoded client
656 * challenge, space, then SHA-1 hash of the concatenation of our
657 * challenge, ":", client challenge, ":", secret key, all
661 DBusString client_challenge;
662 DBusString client_hash;
664 DBusString correct_hash;
668 if (!_dbus_string_find_blank (data, 0, &i))
670 _dbus_verbose ("%s: no space separator in client response\n",
671 DBUS_AUTH_NAME (auth));
672 return send_rejected (auth);
675 if (!_dbus_string_init (&client_challenge))
678 if (!_dbus_string_init (&client_hash))
681 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
685 _dbus_string_skip_blank (data, i, &i);
687 if (!_dbus_string_copy_len (data, i,
688 _dbus_string_get_length (data) - i,
693 if (_dbus_string_get_length (&client_challenge) == 0 ||
694 _dbus_string_get_length (&client_hash) == 0)
696 _dbus_verbose ("%s: zero-length client challenge or hash\n",
697 DBUS_AUTH_NAME (auth));
698 if (send_rejected (auth))
703 if (!_dbus_string_init (&correct_hash))
706 if (!sha1_compute_hash (auth, auth->cookie_id,
712 /* if cookie_id was invalid, then we get an empty hash */
713 if (_dbus_string_get_length (&correct_hash) == 0)
715 if (send_rejected (auth))
720 if (!_dbus_string_equal (&client_hash, &correct_hash))
722 if (send_rejected (auth))
727 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
728 auth->desired_identity))
734 _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
735 DBUS_AUTH_NAME (auth));
740 _dbus_string_zero (&correct_hash);
741 _dbus_string_free (&correct_hash);
743 _dbus_string_zero (&client_hash);
744 _dbus_string_free (&client_hash);
746 _dbus_string_free (&client_challenge);
752 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
753 const DBusString *data)
755 if (auth->cookie_id < 0)
756 return sha1_handle_first_client_response (auth, data);
758 return sha1_handle_second_client_response (auth, data);
762 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
764 auth->cookie_id = -1;
765 _dbus_string_set_length (&auth->challenge, 0);
769 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
770 DBusString *response)
772 const DBusString *username;
777 if (!_dbus_username_from_current_process (&username))
780 if (!_dbus_string_hex_encode (username, 0,
782 _dbus_string_get_length (response)))
792 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
793 const DBusString *data)
795 /* The data we get from the server should be the cookie context
796 * name, the cookie ID, and the server challenge, separated by
797 * spaces. We send back our challenge string and the correct hash.
801 DBusString cookie_id_str;
802 DBusString server_challenge;
803 DBusString client_challenge;
804 DBusString correct_hash;
811 if (!_dbus_string_find_blank (data, 0, &i))
813 if (send_error (auth,
814 "Server did not send context/ID/challenge properly"))
819 if (!_dbus_string_init (&context))
822 if (!_dbus_string_copy_len (data, 0, i,
826 _dbus_string_skip_blank (data, i, &i);
827 if (!_dbus_string_find_blank (data, i, &j))
829 if (send_error (auth,
830 "Server did not send context/ID/challenge properly"))
835 if (!_dbus_string_init (&cookie_id_str))
838 if (!_dbus_string_copy_len (data, i, j - i,
842 if (!_dbus_string_init (&server_challenge))
846 _dbus_string_skip_blank (data, i, &i);
847 j = _dbus_string_get_length (data);
849 if (!_dbus_string_copy_len (data, i, j - i,
850 &server_challenge, 0))
853 if (!_dbus_keyring_validate_context (&context))
855 if (send_error (auth, "Server sent invalid cookie context"))
860 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
862 if (send_error (auth, "Could not parse cookie ID as an integer"))
867 if (_dbus_string_get_length (&server_challenge) == 0)
869 if (send_error (auth, "Empty server challenge string"))
874 if (auth->keyring == NULL)
878 dbus_error_init (&error);
879 auth->keyring = _dbus_keyring_new_homedir (NULL,
883 if (auth->keyring == NULL)
885 if (dbus_error_has_name (&error,
886 DBUS_ERROR_NO_MEMORY))
888 dbus_error_free (&error);
893 _DBUS_ASSERT_ERROR_IS_SET (&error);
895 _dbus_verbose ("%s: Error loading keyring: %s\n",
896 DBUS_AUTH_NAME (auth), error.message);
898 if (send_error (auth, "Could not load cookie file"))
899 retval = TRUE; /* retval is only about mem */
901 dbus_error_free (&error);
907 _dbus_assert (!dbus_error_is_set (&error));
911 _dbus_assert (auth->keyring != NULL);
913 if (!_dbus_string_init (&tmp))
916 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
919 if (!_dbus_string_init (&client_challenge))
922 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
925 if (!_dbus_string_init (&correct_hash))
928 if (!sha1_compute_hash (auth, val,
934 if (_dbus_string_get_length (&correct_hash) == 0)
936 /* couldn't find the cookie ID or something */
937 if (send_error (auth, "Don't have the requested cookie ID"))
942 _dbus_string_set_length (&tmp, 0);
944 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
945 _dbus_string_get_length (&tmp)))
948 if (!_dbus_string_append (&tmp, " "))
951 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
952 _dbus_string_get_length (&tmp)))
955 if (!send_data (auth, &tmp))
961 _dbus_string_zero (&correct_hash);
962 _dbus_string_free (&correct_hash);
964 _dbus_string_free (&client_challenge);
966 _dbus_string_zero (&tmp);
967 _dbus_string_free (&tmp);
969 _dbus_string_free (&server_challenge);
971 _dbus_string_free (&cookie_id_str);
973 _dbus_string_free (&context);
979 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
981 auth->cookie_id = -1;
982 _dbus_string_set_length (&auth->challenge, 0);
986 handle_server_data_external_mech (DBusAuth *auth,
987 const DBusString *data)
989 if (_dbus_credentials_are_empty (auth->credentials))
991 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
992 DBUS_AUTH_NAME (auth));
993 return send_rejected (auth);
996 if (_dbus_string_get_length (data) > 0)
998 if (_dbus_string_get_length (&auth->identity) > 0)
1000 /* Tried to send two auth identities, wtf */
1001 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1002 DBUS_AUTH_NAME (auth));
1003 return send_rejected (auth);
1007 /* this is our auth identity */
1008 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1013 /* Poke client for an auth identity, if none given */
1014 if (_dbus_string_get_length (&auth->identity) == 0 &&
1015 !auth->already_asked_for_initial_response)
1017 if (send_data (auth, NULL))
1019 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1020 DBUS_AUTH_NAME (auth));
1021 auth->already_asked_for_initial_response = TRUE;
1028 _dbus_credentials_clear (auth->desired_identity);
1030 /* If auth->identity is still empty here, then client
1031 * responded with an empty string after we poked it for
1032 * an initial response. This means to try to auth the
1033 * identity provided in the credentials.
1035 if (_dbus_string_get_length (&auth->identity) == 0)
1037 if (!_dbus_credentials_add_credentials (auth->desired_identity,
1040 return FALSE; /* OOM */
1045 if (!_dbus_credentials_parse_and_add_desired(auth->desired_identity,
1048 _dbus_verbose ("%s: could not get credentials from uid string\n",
1049 DBUS_AUTH_NAME (auth));
1050 return send_rejected (auth);
1054 if (_dbus_credentials_are_empty(auth->desired_identity))
1056 _dbus_verbose ("%s: desired user %s is no good\n",
1057 DBUS_AUTH_NAME (auth),
1058 _dbus_string_get_const_data (&auth->identity));
1059 return send_rejected (auth);
1062 if (_dbus_credentials_are_superset (auth->credentials,
1063 auth->desired_identity))
1065 /* client has authenticated */
1066 if (!_dbus_credentials_add_credentials (auth->authorized_identity,
1067 auth->desired_identity))
1070 /* also copy process ID from the socket credentials - FIXME this
1071 * should be done even if auth EXTERNAL not used
1073 if (!_dbus_credentials_add_credential (auth->authorized_identity,
1074 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1078 if (!send_ok (auth))
1081 _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1082 DBUS_AUTH_NAME (auth));
1088 _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1089 DBUS_AUTH_NAME (auth));
1090 return send_rejected (auth);
1095 handle_server_shutdown_external_mech (DBusAuth *auth)
1101 handle_client_initial_response_external_mech (DBusAuth *auth,
1102 DBusString *response)
1104 /* We always append our UID as an initial response, so the server
1105 * doesn't have to send back an empty challenge to check whether we
1106 * want to specify an identity. i.e. this avoids a round trip that
1107 * the spec for the EXTERNAL mechanism otherwise requires.
1109 DBusString plaintext;
1111 if (!_dbus_string_init (&plaintext))
1114 if (!_dbus_append_desired_identity (&plaintext))
1117 if (!_dbus_string_hex_encode (&plaintext, 0,
1119 _dbus_string_get_length (response)))
1122 _dbus_string_free (&plaintext);
1127 _dbus_string_free (&plaintext);
1132 handle_client_data_external_mech (DBusAuth *auth,
1133 const DBusString *data)
1140 handle_client_shutdown_external_mech (DBusAuth *auth)
1145 /* Put mechanisms here in order of preference.
1146 * What I eventually want to have is:
1148 * - a mechanism that checks UNIX domain socket credentials
1149 * - a simple magic cookie mechanism like X11 or ICE
1150 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1151 * offers such as Kerberos, X509, whatever.
1154 static const DBusAuthMechanismHandler
1155 all_mechanisms[] = {
1157 handle_server_data_external_mech,
1159 handle_server_shutdown_external_mech,
1160 handle_client_initial_response_external_mech,
1161 handle_client_data_external_mech,
1163 handle_client_shutdown_external_mech },
1164 { "DBUS_COOKIE_SHA1",
1165 handle_server_data_cookie_sha1_mech,
1167 handle_server_shutdown_cookie_sha1_mech,
1168 handle_client_initial_response_cookie_sha1_mech,
1169 handle_client_data_cookie_sha1_mech,
1171 handle_client_shutdown_cookie_sha1_mech },
1175 static const DBusAuthMechanismHandler*
1176 find_mech (const DBusString *name,
1177 char **allowed_mechs)
1181 if (allowed_mechs != NULL &&
1182 !_dbus_string_array_contains ((const char**) allowed_mechs,
1183 _dbus_string_get_const_data (name)))
1187 while (all_mechanisms[i].mechanism != NULL)
1189 if (_dbus_string_equal_c_str (name,
1190 all_mechanisms[i].mechanism))
1192 return &all_mechanisms[i];
1201 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1203 DBusString auth_command;
1205 if (!_dbus_string_init (&auth_command))
1208 if (!_dbus_string_append (&auth_command,
1211 _dbus_string_free (&auth_command);
1215 if (!_dbus_string_append (&auth_command,
1218 _dbus_string_free (&auth_command);
1222 if (mech->client_initial_response_func != NULL)
1224 if (!_dbus_string_append (&auth_command, " "))
1226 _dbus_string_free (&auth_command);
1230 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1232 _dbus_string_free (&auth_command);
1237 if (!_dbus_string_append (&auth_command,
1240 _dbus_string_free (&auth_command);
1244 if (!_dbus_string_copy (&auth_command, 0,
1246 _dbus_string_get_length (&auth->outgoing)))
1248 _dbus_string_free (&auth_command);
1252 _dbus_string_free (&auth_command);
1253 shutdown_mech (auth);
1255 goto_state (auth, &client_state_waiting_for_data);
1261 send_data (DBusAuth *auth, DBusString *data)
1265 if (data == NULL || _dbus_string_get_length (data) == 0)
1266 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1269 old_len = _dbus_string_get_length (&auth->outgoing);
1270 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1273 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1274 _dbus_string_get_length (&auth->outgoing)))
1277 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1283 _dbus_string_set_length (&auth->outgoing, old_len);
1290 send_rejected (DBusAuth *auth)
1293 DBusAuthServer *server_auth;
1296 if (!_dbus_string_init (&command))
1299 if (!_dbus_string_append (&command,
1304 while (all_mechanisms[i].mechanism != NULL)
1306 if (!_dbus_string_append (&command,
1310 if (!_dbus_string_append (&command,
1311 all_mechanisms[i].mechanism))
1317 if (!_dbus_string_append (&command, "\r\n"))
1320 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1321 _dbus_string_get_length (&auth->outgoing)))
1324 shutdown_mech (auth);
1326 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1327 server_auth = DBUS_AUTH_SERVER (auth);
1328 server_auth->failures += 1;
1330 if (server_auth->failures >= server_auth->max_failures)
1331 goto_state (auth, &common_state_need_disconnect);
1333 goto_state (auth, &server_state_waiting_for_auth);
1335 _dbus_string_free (&command);
1340 _dbus_string_free (&command);
1345 send_error (DBusAuth *auth, const char *message)
1347 return _dbus_string_append_printf (&auth->outgoing,
1348 "ERROR \"%s\"\r\n", message);
1352 send_ok (DBusAuth *auth)
1356 orig_len = _dbus_string_get_length (&auth->outgoing);
1358 if (_dbus_string_append (&auth->outgoing, "OK ") &&
1359 _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1362 _dbus_string_get_length (&auth->outgoing)) &&
1363 _dbus_string_append (&auth->outgoing, "\r\n"))
1365 goto_state (auth, &server_state_waiting_for_begin);
1370 _dbus_string_set_length (&auth->outgoing, orig_len);
1376 send_begin (DBusAuth *auth,
1377 const DBusString *args_from_ok)
1381 /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1382 _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1384 /* We decode the hex string to binary, using guid_from_server as scratch... */
1387 if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1388 & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1391 /* now clear out the scratch */
1392 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1394 if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1397 _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1398 end_of_hex, _dbus_string_get_length (args_from_ok));
1399 goto_state (auth, &common_state_need_disconnect);
1403 if (_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0) &&
1404 _dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
1406 _dbus_verbose ("Got GUID '%s' from the server\n",
1407 _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1409 goto_state (auth, &common_state_authenticated);
1414 _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1420 send_cancel (DBusAuth *auth)
1422 if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1424 goto_state (auth, &client_state_waiting_for_reject);
1432 process_data (DBusAuth *auth,
1433 const DBusString *args,
1434 DBusAuthDataFunction data_func)
1439 if (!_dbus_string_init (&decoded))
1442 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1444 _dbus_string_free (&decoded);
1448 if (_dbus_string_get_length (args) != end)
1450 _dbus_string_free (&decoded);
1451 if (!send_error (auth, "Invalid hex encoding"))
1457 #ifdef DBUS_ENABLE_VERBOSE_MODE
1458 if (_dbus_string_validate_ascii (&decoded, 0,
1459 _dbus_string_get_length (&decoded)))
1460 _dbus_verbose ("%s: data: '%s'\n",
1461 DBUS_AUTH_NAME (auth),
1462 _dbus_string_get_const_data (&decoded));
1465 if (!(* data_func) (auth, &decoded))
1467 _dbus_string_free (&decoded);
1471 _dbus_string_free (&decoded);
1476 handle_auth (DBusAuth *auth, const DBusString *args)
1478 if (_dbus_string_get_length (args) == 0)
1480 /* No args to the auth, send mechanisms */
1481 if (!send_rejected (auth))
1490 DBusString hex_response;
1492 _dbus_string_find_blank (args, 0, &i);
1494 if (!_dbus_string_init (&mech))
1497 if (!_dbus_string_init (&hex_response))
1499 _dbus_string_free (&mech);
1503 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1506 _dbus_string_skip_blank (args, i, &i);
1507 if (!_dbus_string_copy (args, i, &hex_response, 0))
1510 auth->mech = find_mech (&mech, auth->allowed_mechs);
1511 if (auth->mech != NULL)
1513 _dbus_verbose ("%s: Trying mechanism %s\n",
1514 DBUS_AUTH_NAME (auth),
1515 auth->mech->mechanism);
1517 if (!process_data (auth, &hex_response,
1518 auth->mech->server_data_func))
1523 /* Unsupported mechanism */
1524 _dbus_verbose ("%s: Unsupported mechanism %s\n",
1525 DBUS_AUTH_NAME (auth),
1526 _dbus_string_get_const_data (&mech));
1528 if (!send_rejected (auth))
1532 _dbus_string_free (&mech);
1533 _dbus_string_free (&hex_response);
1539 _dbus_string_free (&mech);
1540 _dbus_string_free (&hex_response);
1546 handle_server_state_waiting_for_auth (DBusAuth *auth,
1547 DBusAuthCommand command,
1548 const DBusString *args)
1552 case DBUS_AUTH_COMMAND_AUTH:
1553 return handle_auth (auth, args);
1555 case DBUS_AUTH_COMMAND_CANCEL:
1556 case DBUS_AUTH_COMMAND_DATA:
1557 return send_error (auth, "Not currently in an auth conversation");
1559 case DBUS_AUTH_COMMAND_BEGIN:
1560 goto_state (auth, &common_state_need_disconnect);
1563 case DBUS_AUTH_COMMAND_ERROR:
1564 return send_rejected (auth);
1566 case DBUS_AUTH_COMMAND_REJECTED:
1567 case DBUS_AUTH_COMMAND_OK:
1568 case DBUS_AUTH_COMMAND_UNKNOWN:
1570 return send_error (auth, "Unknown command");
1575 handle_server_state_waiting_for_data (DBusAuth *auth,
1576 DBusAuthCommand command,
1577 const DBusString *args)
1581 case DBUS_AUTH_COMMAND_AUTH:
1582 return send_error (auth, "Sent AUTH while another AUTH in progress");
1584 case DBUS_AUTH_COMMAND_CANCEL:
1585 case DBUS_AUTH_COMMAND_ERROR:
1586 return send_rejected (auth);
1588 case DBUS_AUTH_COMMAND_DATA:
1589 return process_data (auth, args, auth->mech->server_data_func);
1591 case DBUS_AUTH_COMMAND_BEGIN:
1592 goto_state (auth, &common_state_need_disconnect);
1595 case DBUS_AUTH_COMMAND_REJECTED:
1596 case DBUS_AUTH_COMMAND_OK:
1597 case DBUS_AUTH_COMMAND_UNKNOWN:
1599 return send_error (auth, "Unknown command");
1604 handle_server_state_waiting_for_begin (DBusAuth *auth,
1605 DBusAuthCommand command,
1606 const DBusString *args)
1610 case DBUS_AUTH_COMMAND_AUTH:
1611 return send_error (auth, "Sent AUTH while expecting BEGIN");
1613 case DBUS_AUTH_COMMAND_DATA:
1614 return send_error (auth, "Sent DATA while expecting BEGIN");
1616 case DBUS_AUTH_COMMAND_BEGIN:
1617 goto_state (auth, &common_state_authenticated);
1620 case DBUS_AUTH_COMMAND_REJECTED:
1621 case DBUS_AUTH_COMMAND_OK:
1622 case DBUS_AUTH_COMMAND_UNKNOWN:
1624 return send_error (auth, "Unknown command");
1626 case DBUS_AUTH_COMMAND_CANCEL:
1627 case DBUS_AUTH_COMMAND_ERROR:
1628 return send_rejected (auth);
1632 /* return FALSE if no memory, TRUE if all OK */
1634 get_word (const DBusString *str,
1640 _dbus_string_skip_blank (str, *start, start);
1641 _dbus_string_find_blank (str, *start, &i);
1645 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1655 record_mechanisms (DBusAuth *auth,
1656 const DBusString *args)
1661 if (auth->already_got_mechanisms)
1664 len = _dbus_string_get_length (args);
1670 const DBusAuthMechanismHandler *mech;
1672 if (!_dbus_string_init (&m))
1675 if (!get_word (args, &next, &m))
1677 _dbus_string_free (&m);
1681 mech = find_mech (&m, auth->allowed_mechs);
1685 /* FIXME right now we try mechanisms in the order
1686 * the server lists them; should we do them in
1687 * some more deterministic order?
1689 * Probably in all_mechanisms order, our order of
1690 * preference. Of course when the server is us,
1691 * it lists things in that order anyhow.
1694 if (mech != &all_mechanisms[0])
1696 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1697 DBUS_AUTH_NAME (auth), mech->mechanism);
1699 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1702 _dbus_string_free (&m);
1708 _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1709 DBUS_AUTH_NAME (auth), mech->mechanism);
1714 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1715 DBUS_AUTH_NAME (auth),
1716 _dbus_string_get_const_data (&m));
1719 _dbus_string_free (&m);
1722 auth->already_got_mechanisms = TRUE;
1727 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1733 process_rejected (DBusAuth *auth, const DBusString *args)
1735 const DBusAuthMechanismHandler *mech;
1736 DBusAuthClient *client;
1738 client = DBUS_AUTH_CLIENT (auth);
1740 if (!auth->already_got_mechanisms)
1742 if (!record_mechanisms (auth, args))
1746 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1748 mech = client->mechs_to_try->data;
1750 if (!send_auth (auth, mech))
1753 _dbus_list_pop_first (&client->mechs_to_try);
1755 _dbus_verbose ("%s: Trying mechanism %s\n",
1756 DBUS_AUTH_NAME (auth),
1762 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1763 DBUS_AUTH_NAME (auth));
1764 goto_state (auth, &common_state_need_disconnect);
1772 handle_client_state_waiting_for_data (DBusAuth *auth,
1773 DBusAuthCommand command,
1774 const DBusString *args)
1776 _dbus_assert (auth->mech != NULL);
1780 case DBUS_AUTH_COMMAND_DATA:
1781 return process_data (auth, args, auth->mech->client_data_func);
1783 case DBUS_AUTH_COMMAND_REJECTED:
1784 return process_rejected (auth, args);
1786 case DBUS_AUTH_COMMAND_OK:
1787 return send_begin (auth, args);
1789 case DBUS_AUTH_COMMAND_ERROR:
1790 return send_cancel (auth);
1792 case DBUS_AUTH_COMMAND_AUTH:
1793 case DBUS_AUTH_COMMAND_CANCEL:
1794 case DBUS_AUTH_COMMAND_BEGIN:
1795 case DBUS_AUTH_COMMAND_UNKNOWN:
1797 return send_error (auth, "Unknown command");
1802 handle_client_state_waiting_for_ok (DBusAuth *auth,
1803 DBusAuthCommand command,
1804 const DBusString *args)
1808 case DBUS_AUTH_COMMAND_REJECTED:
1809 return process_rejected (auth, args);
1811 case DBUS_AUTH_COMMAND_OK:
1812 return send_begin (auth, args);
1814 case DBUS_AUTH_COMMAND_DATA:
1815 case DBUS_AUTH_COMMAND_ERROR:
1816 return send_cancel (auth);
1818 case DBUS_AUTH_COMMAND_AUTH:
1819 case DBUS_AUTH_COMMAND_CANCEL:
1820 case DBUS_AUTH_COMMAND_BEGIN:
1821 case DBUS_AUTH_COMMAND_UNKNOWN:
1823 return send_error (auth, "Unknown command");
1828 handle_client_state_waiting_for_reject (DBusAuth *auth,
1829 DBusAuthCommand command,
1830 const DBusString *args)
1834 case DBUS_AUTH_COMMAND_REJECTED:
1835 return process_rejected (auth, args);
1837 case DBUS_AUTH_COMMAND_AUTH:
1838 case DBUS_AUTH_COMMAND_CANCEL:
1839 case DBUS_AUTH_COMMAND_DATA:
1840 case DBUS_AUTH_COMMAND_BEGIN:
1841 case DBUS_AUTH_COMMAND_OK:
1842 case DBUS_AUTH_COMMAND_ERROR:
1843 case DBUS_AUTH_COMMAND_UNKNOWN:
1845 goto_state (auth, &common_state_need_disconnect);
1851 * Mapping from command name to enum
1854 const char *name; /**< Name of the command */
1855 DBusAuthCommand command; /**< Corresponding enum */
1856 } DBusAuthCommandName;
1858 static const DBusAuthCommandName auth_command_names[] = {
1859 { "AUTH", DBUS_AUTH_COMMAND_AUTH },
1860 { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
1861 { "DATA", DBUS_AUTH_COMMAND_DATA },
1862 { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
1863 { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
1864 { "OK", DBUS_AUTH_COMMAND_OK },
1865 { "ERROR", DBUS_AUTH_COMMAND_ERROR }
1868 static DBusAuthCommand
1869 lookup_command_from_name (DBusString *command)
1873 for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
1875 if (_dbus_string_equal_c_str (command,
1876 auth_command_names[i].name))
1877 return auth_command_names[i].command;
1880 return DBUS_AUTH_COMMAND_UNKNOWN;
1884 goto_state (DBusAuth *auth, const DBusAuthStateData *state)
1886 _dbus_verbose ("%s: going from state %s to state %s\n",
1887 DBUS_AUTH_NAME (auth),
1891 auth->state = state;
1894 /* returns whether to call it again right away */
1896 process_command (DBusAuth *auth)
1898 DBusAuthCommand command;
1905 /* _dbus_verbose ("%s: trying process_command()\n"); */
1910 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1913 if (!_dbus_string_init (&line))
1915 auth->needed_memory = TRUE;
1919 if (!_dbus_string_init (&args))
1921 _dbus_string_free (&line);
1922 auth->needed_memory = TRUE;
1926 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
1929 if (!_dbus_string_validate_ascii (&line, 0,
1930 _dbus_string_get_length (&line)))
1932 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1933 DBUS_AUTH_NAME (auth));
1934 if (!send_error (auth, "Command contained non-ASCII"))
1940 _dbus_verbose ("%s: got command \"%s\"\n",
1941 DBUS_AUTH_NAME (auth),
1942 _dbus_string_get_const_data (&line));
1944 _dbus_string_find_blank (&line, 0, &i);
1945 _dbus_string_skip_blank (&line, i, &j);
1948 _dbus_string_delete (&line, i, j - i);
1950 if (!_dbus_string_move (&line, i, &args, 0))
1953 /* FIXME 1.0 we should probably validate that only the allowed
1954 * chars are in the command name
1957 command = lookup_command_from_name (&line);
1958 if (!(* auth->state->handler) (auth, command, &args))
1963 /* We've succeeded in processing the whole command so drop it out
1964 * of the incoming buffer and return TRUE to try another command.
1967 _dbus_string_delete (&auth->incoming, 0, eol);
1970 _dbus_string_delete (&auth->incoming, 0, 2);
1975 _dbus_string_free (&args);
1976 _dbus_string_free (&line);
1979 auth->needed_memory = TRUE;
1981 auth->needed_memory = FALSE;
1990 * @addtogroup DBusAuth
1995 * Creates a new auth conversation object for the server side.
1996 * See doc/dbus-sasl-profile.txt for full details on what
1999 * @returns the new object or #NULL if no memory
2002 _dbus_auth_server_new (const DBusString *guid)
2005 DBusAuthServer *server_auth;
2006 DBusString guid_copy;
2008 if (!_dbus_string_init (&guid_copy))
2011 if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2013 _dbus_string_free (&guid_copy);
2017 auth = _dbus_auth_new (sizeof (DBusAuthServer));
2020 _dbus_string_free (&guid_copy);
2024 auth->side = auth_side_server;
2025 auth->state = &server_state_waiting_for_auth;
2027 server_auth = DBUS_AUTH_SERVER (auth);
2029 server_auth->guid = guid_copy;
2031 /* perhaps this should be per-mechanism with a lower
2034 server_auth->failures = 0;
2035 server_auth->max_failures = 6;
2041 * Creates a new auth conversation object for the client side.
2042 * See doc/dbus-sasl-profile.txt for full details on what
2045 * @returns the new object or #NULL if no memory
2048 _dbus_auth_client_new (void)
2051 DBusString guid_str;
2053 if (!_dbus_string_init (&guid_str))
2056 auth = _dbus_auth_new (sizeof (DBusAuthClient));
2059 _dbus_string_free (&guid_str);
2063 DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2065 auth->side = auth_side_client;
2066 auth->state = &client_state_need_send_auth;
2068 /* Start the auth conversation by sending AUTH for our default
2070 if (!send_auth (auth, &all_mechanisms[0]))
2072 _dbus_auth_unref (auth);
2080 * Increments the refcount of an auth object.
2082 * @param auth the auth conversation
2083 * @returns the auth conversation
2086 _dbus_auth_ref (DBusAuth *auth)
2088 _dbus_assert (auth != NULL);
2090 auth->refcount += 1;
2096 * Decrements the refcount of an auth object.
2098 * @param auth the auth conversation
2101 _dbus_auth_unref (DBusAuth *auth)
2103 _dbus_assert (auth != NULL);
2104 _dbus_assert (auth->refcount > 0);
2106 auth->refcount -= 1;
2107 if (auth->refcount == 0)
2109 shutdown_mech (auth);
2111 if (DBUS_AUTH_IS_CLIENT (auth))
2113 _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2114 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2118 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
2120 _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2124 _dbus_keyring_unref (auth->keyring);
2126 _dbus_string_free (&auth->context);
2127 _dbus_string_free (&auth->challenge);
2128 _dbus_string_free (&auth->identity);
2129 _dbus_string_free (&auth->incoming);
2130 _dbus_string_free (&auth->outgoing);
2132 dbus_free_string_array (auth->allowed_mechs);
2134 _dbus_credentials_unref (auth->credentials);
2135 _dbus_credentials_unref (auth->authorized_identity);
2136 _dbus_credentials_unref (auth->desired_identity);
2143 * Sets an array of authentication mechanism names
2144 * that we are willing to use.
2146 * @param auth the auth conversation
2147 * @param mechanisms #NULL-terminated array of mechanism names
2148 * @returns #FALSE if no memory
2151 _dbus_auth_set_mechanisms (DBusAuth *auth,
2152 const char **mechanisms)
2156 if (mechanisms != NULL)
2158 copy = _dbus_dup_string_array (mechanisms);
2165 dbus_free_string_array (auth->allowed_mechs);
2167 auth->allowed_mechs = copy;
2173 * @param auth the auth conversation object
2174 * @returns #TRUE if we're in a final state
2176 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2179 * Analyzes buffered input and moves the auth conversation forward,
2180 * returning the new state of the auth conversation.
2182 * @param auth the auth conversation
2183 * @returns the new state
2186 _dbus_auth_do_work (DBusAuth *auth)
2188 auth->needed_memory = FALSE;
2190 /* Max amount we'll buffer up before deciding someone's on crack */
2191 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2195 if (DBUS_AUTH_IN_END_STATE (auth))
2198 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2199 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2201 goto_state (auth, &common_state_need_disconnect);
2202 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2203 DBUS_AUTH_NAME (auth));
2207 while (process_command (auth));
2209 if (auth->needed_memory)
2210 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2211 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2212 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2213 else if (auth->state == &common_state_need_disconnect)
2214 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2215 else if (auth->state == &common_state_authenticated)
2216 return DBUS_AUTH_STATE_AUTHENTICATED;
2217 else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2221 * Gets bytes that need to be sent to the peer we're conversing with.
2222 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2223 * to notify the auth object that they were written.
2225 * @param auth the auth conversation
2226 * @param str return location for a ref to the buffer to send
2227 * @returns #FALSE if nothing to send
2230 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2231 const DBusString **str)
2233 _dbus_assert (auth != NULL);
2234 _dbus_assert (str != NULL);
2238 if (_dbus_string_get_length (&auth->outgoing) == 0)
2241 *str = &auth->outgoing;
2247 * Notifies the auth conversation object that
2248 * the given number of bytes of the outgoing buffer
2249 * have been written out.
2251 * @param auth the auth conversation
2252 * @param bytes_sent number of bytes written out
2255 _dbus_auth_bytes_sent (DBusAuth *auth,
2258 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2259 DBUS_AUTH_NAME (auth),
2261 _dbus_string_get_const_data (&auth->outgoing));
2263 _dbus_string_delete (&auth->outgoing,
2268 * Get a buffer to be used for reading bytes from the peer we're conversing
2269 * with. Bytes should be appended to this buffer.
2271 * @param auth the auth conversation
2272 * @param buffer return location for buffer to append bytes to
2275 _dbus_auth_get_buffer (DBusAuth *auth,
2276 DBusString **buffer)
2278 _dbus_assert (auth != NULL);
2279 _dbus_assert (!auth->buffer_outstanding);
2281 *buffer = &auth->incoming;
2283 auth->buffer_outstanding = TRUE;
2287 * Returns a buffer with new data read into it.
2289 * @param auth the auth conversation
2290 * @param buffer the buffer being returned
2291 * @param bytes_read number of new bytes added
2294 _dbus_auth_return_buffer (DBusAuth *auth,
2298 _dbus_assert (buffer == &auth->incoming);
2299 _dbus_assert (auth->buffer_outstanding);
2301 auth->buffer_outstanding = FALSE;
2305 * Returns leftover bytes that were not used as part of the auth
2306 * conversation. These bytes will be part of the message stream
2307 * instead. This function may not be called until authentication has
2310 * @param auth the auth conversation
2311 * @param str return location for pointer to string of unused bytes
2314 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2315 const DBusString **str)
2317 if (!DBUS_AUTH_IN_END_STATE (auth))
2320 *str = &auth->incoming;
2325 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2326 * after we've gotten them and successfully moved them elsewhere.
2328 * @param auth the auth conversation
2331 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2333 if (!DBUS_AUTH_IN_END_STATE (auth))
2336 _dbus_string_set_length (&auth->incoming, 0);
2340 * Called post-authentication, indicates whether we need to encode
2341 * the message stream with _dbus_auth_encode_data() prior to
2342 * sending it to the peer.
2344 * @param auth the auth conversation
2345 * @returns #TRUE if we need to encode the stream
2348 _dbus_auth_needs_encoding (DBusAuth *auth)
2350 if (auth->state != &common_state_authenticated)
2353 if (auth->mech != NULL)
2355 if (DBUS_AUTH_IS_CLIENT (auth))
2356 return auth->mech->client_encode_func != NULL;
2358 return auth->mech->server_encode_func != NULL;
2365 * Called post-authentication, encodes a block of bytes for sending to
2366 * the peer. If no encoding was negotiated, just copies the bytes
2367 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2369 * @param auth the auth conversation
2370 * @param plaintext the plain text data
2371 * @param encoded initialized string to where encoded data is appended
2372 * @returns #TRUE if we had enough memory and successfully encoded
2375 _dbus_auth_encode_data (DBusAuth *auth,
2376 const DBusString *plaintext,
2377 DBusString *encoded)
2379 _dbus_assert (plaintext != encoded);
2381 if (auth->state != &common_state_authenticated)
2384 if (_dbus_auth_needs_encoding (auth))
2386 if (DBUS_AUTH_IS_CLIENT (auth))
2387 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2389 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2393 return _dbus_string_copy (plaintext, 0, encoded,
2394 _dbus_string_get_length (encoded));
2399 * Called post-authentication, indicates whether we need to decode
2400 * the message stream with _dbus_auth_decode_data() after
2401 * receiving it from the peer.
2403 * @param auth the auth conversation
2404 * @returns #TRUE if we need to encode the stream
2407 _dbus_auth_needs_decoding (DBusAuth *auth)
2409 if (auth->state != &common_state_authenticated)
2412 if (auth->mech != NULL)
2414 if (DBUS_AUTH_IS_CLIENT (auth))
2415 return auth->mech->client_decode_func != NULL;
2417 return auth->mech->server_decode_func != NULL;
2425 * Called post-authentication, decodes a block of bytes received from
2426 * the peer. If no encoding was negotiated, just copies the bytes (you
2427 * can avoid this by checking _dbus_auth_needs_decoding()).
2429 * @todo 1.0? We need to be able to distinguish "out of memory" error
2430 * from "the data is hosed" error.
2432 * @param auth the auth conversation
2433 * @param encoded the encoded data
2434 * @param plaintext initialized string where decoded data is appended
2435 * @returns #TRUE if we had enough memory and successfully decoded
2438 _dbus_auth_decode_data (DBusAuth *auth,
2439 const DBusString *encoded,
2440 DBusString *plaintext)
2442 _dbus_assert (plaintext != encoded);
2444 if (auth->state != &common_state_authenticated)
2447 if (_dbus_auth_needs_decoding (auth))
2449 if (DBUS_AUTH_IS_CLIENT (auth))
2450 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2452 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2456 return _dbus_string_copy (encoded, 0, plaintext,
2457 _dbus_string_get_length (plaintext));
2462 * Sets credentials received via reliable means from the operating
2465 * @param auth the auth conversation
2466 * @param credentials the credentials received
2467 * @returns #FALSE on OOM
2470 _dbus_auth_set_credentials (DBusAuth *auth,
2471 DBusCredentials *credentials)
2473 _dbus_credentials_clear (auth->credentials);
2474 return _dbus_credentials_add_credentials (auth->credentials,
2479 * Gets the identity we authorized the client as. Apps may have
2480 * different policies as to what identities they allow.
2482 * Returned credentials are not a copy and should not be modified
2484 * @param auth the auth conversation
2485 * @returns the credentials we've authorized BY REFERENCE do not modify
2488 _dbus_auth_get_identity (DBusAuth *auth)
2490 if (auth->state == &common_state_authenticated)
2492 return auth->authorized_identity;
2496 /* FIXME instead of this, keep an empty credential around that
2497 * doesn't require allocation or something
2499 /* return empty credentials */
2500 _dbus_assert (_dbus_credentials_are_empty (auth->authorized_identity));
2501 return auth->authorized_identity;
2506 * Gets the GUID from the server if we've authenticated; gets
2508 * @param auth the auth object
2509 * @returns the GUID in ASCII hex format
2512 _dbus_auth_get_guid_from_server (DBusAuth *auth)
2514 _dbus_assert (DBUS_AUTH_IS_CLIENT (auth));
2516 if (auth->state == &common_state_authenticated)
2517 return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2523 * Sets the "authentication context" which scopes cookies
2524 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2526 * @param auth the auth conversation
2527 * @param context the context
2528 * @returns #FALSE if no memory
2531 _dbus_auth_set_context (DBusAuth *auth,
2532 const DBusString *context)
2534 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2535 &auth->context, 0, _dbus_string_get_length (context));
2540 /* tests in dbus-auth-util.c */