1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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"
30 /* See doc/dbus-sasl-profile.txt */
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 * The file doc/dbus-sasl-profile.txt documents the network protocol
42 * used for authentication.
44 * @todo some SASL profiles require sending the empty string as a
45 * challenge/response, but we don't currently allow that in our
48 * @todo DBusAuth really needs to be rewritten as an explicit state
49 * machine. Right now it's too hard to prove to yourself by inspection
52 * @todo right now sometimes both ends will block waiting for input
53 * from the other end, e.g. if there's an error during
56 * @todo the cookie keyring needs to be cached globally not just
57 * per-auth (which raises threadsafety issues too)
59 * @todo grep FIXME in dbus-auth.c
63 * @defgroup DBusAuthInternals Authentication implementation details
64 * @ingroup DBusInternals
65 * @brief DBusAuth implementation details
67 * Private details of authentication code.
73 * Processes a command. Returns whether we had enough memory to
74 * complete the operation.
76 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
77 const DBusString *command,
78 const DBusString *args);
83 DBusProcessAuthCommandFunction func;
84 } DBusAuthCommandHandler;
87 * This function appends an initial client response to the given string
89 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
90 DBusString *response);
93 * This function processes a block of data received from the peer.
94 * i.e. handles a DATA command.
96 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
97 const DBusString *data);
100 * This function encodes a block of data from the peer.
102 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
103 const DBusString *data,
104 DBusString *encoded);
107 * This function decodes a block of data from the peer.
109 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
110 const DBusString *data,
111 DBusString *decoded);
114 * This function is called when the mechanism is abandoned.
116 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
120 const char *mechanism;
121 DBusAuthDataFunction server_data_func;
122 DBusAuthEncodeFunction server_encode_func;
123 DBusAuthDecodeFunction server_decode_func;
124 DBusAuthShutdownFunction server_shutdown_func;
125 DBusInitialResponseFunction client_initial_response_func;
126 DBusAuthDataFunction client_data_func;
127 DBusAuthEncodeFunction client_encode_func;
128 DBusAuthDecodeFunction client_decode_func;
129 DBusAuthShutdownFunction client_shutdown_func;
130 } DBusAuthMechanismHandler;
133 * Internal members of DBusAuth.
137 int refcount; /**< reference count */
139 DBusString incoming; /**< Incoming data buffer */
140 DBusString outgoing; /**< Outgoing data buffer */
142 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
144 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
146 DBusString identity; /**< Current identity we're authorizing
150 DBusCredentials credentials; /**< Credentials read from socket,
154 DBusCredentials authorized_identity; /**< Credentials that are authorized */
156 DBusCredentials desired_identity; /**< Identity client has requested */
158 DBusString context; /**< Cookie scope */
159 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
160 int cookie_id; /**< ID of cookie to use */
161 DBusString challenge; /**< Challenge sent to client */
163 char **allowed_mechs; /**< Mechanisms we're allowed to use,
164 * or #NULL if we can use any
167 unsigned int needed_memory : 1; /**< We needed memory to continue since last
168 * successful getting something done
170 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
171 unsigned int authenticated : 1; /**< We are authenticated */
172 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
173 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
174 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
175 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
176 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
183 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
191 int failures; /**< Number of times client has been rejected */
192 int max_failures; /**< Number of times we reject before disconnect */
196 static dbus_bool_t process_auth (DBusAuth *auth,
197 const DBusString *command,
198 const DBusString *args);
199 static dbus_bool_t process_cancel (DBusAuth *auth,
200 const DBusString *command,
201 const DBusString *args);
202 static dbus_bool_t process_begin (DBusAuth *auth,
203 const DBusString *command,
204 const DBusString *args);
205 static dbus_bool_t process_data_server (DBusAuth *auth,
206 const DBusString *command,
207 const DBusString *args);
208 static dbus_bool_t process_error_server (DBusAuth *auth,
209 const DBusString *command,
210 const DBusString *args);
211 static dbus_bool_t process_rejected (DBusAuth *auth,
212 const DBusString *command,
213 const DBusString *args);
214 static dbus_bool_t process_ok (DBusAuth *auth,
215 const DBusString *command,
216 const DBusString *args);
217 static dbus_bool_t process_data_client (DBusAuth *auth,
218 const DBusString *command,
219 const DBusString *args);
220 static dbus_bool_t process_error_client (DBusAuth *auth,
221 const DBusString *command,
222 const DBusString *args);
225 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
226 static dbus_bool_t send_rejected (DBusAuth *auth);
228 static DBusAuthCommandHandler
229 server_handlers[] = {
230 { "AUTH", process_auth },
231 { "CANCEL", process_cancel },
232 { "BEGIN", process_begin },
233 { "DATA", process_data_server },
234 { "ERROR", process_error_server },
238 static DBusAuthCommandHandler
239 client_handlers[] = {
240 { "REJECTED", process_rejected },
241 { "OK", process_ok },
242 { "DATA", process_data_client },
243 { "ERROR", process_error_client },
248 * @param auth the auth conversation
249 * @returns #TRUE if the conversation is the server side
251 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
253 * @param auth the auth conversation
254 * @returns #TRUE if the conversation is the client side
256 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
258 * @param auth the auth conversation
259 * @returns auth cast to DBusAuthClient
261 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
263 * @param auth the auth conversation
264 * @returns auth cast to DBusAuthServer
266 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
269 _dbus_auth_new (int size)
273 auth = dbus_malloc0 (size);
279 _dbus_credentials_clear (&auth->credentials);
280 _dbus_credentials_clear (&auth->authorized_identity);
281 _dbus_credentials_clear (&auth->desired_identity);
283 auth->keyring = NULL;
284 auth->cookie_id = -1;
286 /* note that we don't use the max string length feature,
287 * because you can't use that feature if you're going to
288 * try to recover from out-of-memory (it creates
289 * what looks like unrecoverable inability to alloc
290 * more space in the string). But we do handle
291 * overlong buffers in _dbus_auth_do_work().
294 if (!_dbus_string_init (&auth->incoming))
297 if (!_dbus_string_init (&auth->outgoing))
300 if (!_dbus_string_init (&auth->identity))
303 if (!_dbus_string_init (&auth->context))
306 if (!_dbus_string_init (&auth->challenge))
309 /* default context if none is specified */
310 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
316 _dbus_string_free (&auth->challenge);
318 _dbus_string_free (&auth->context);
320 _dbus_string_free (&auth->identity);
322 _dbus_string_free (&auth->outgoing);
324 _dbus_string_free (&auth->incoming);
331 shutdown_mech (DBusAuth *auth)
333 /* Cancel any auth */
334 auth->authenticated_pending_begin = FALSE;
335 auth->authenticated = FALSE;
336 auth->already_asked_for_initial_response = FALSE;
337 _dbus_string_set_length (&auth->identity, 0);
339 _dbus_credentials_clear (&auth->authorized_identity);
340 _dbus_credentials_clear (&auth->desired_identity);
342 if (auth->mech != NULL)
344 _dbus_verbose ("Shutting down mechanism %s\n",
345 auth->mech->mechanism);
347 if (DBUS_AUTH_IS_CLIENT (auth))
348 (* auth->mech->client_shutdown_func) (auth);
350 (* auth->mech->server_shutdown_func) (auth);
356 /* Returns TRUE but with an empty string hash if the
357 * cookie_id isn't known. As with all this code
358 * TRUE just means we had enough memory.
361 sha1_compute_hash (DBusAuth *auth,
363 const DBusString *server_challenge,
364 const DBusString *client_challenge,
371 _dbus_assert (auth->keyring != NULL);
375 if (!_dbus_string_init (&cookie))
378 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
382 if (_dbus_string_get_length (&cookie) == 0)
388 if (!_dbus_string_init (&to_hash))
391 if (!_dbus_string_copy (server_challenge, 0,
392 &to_hash, _dbus_string_get_length (&to_hash)))
395 if (!_dbus_string_append (&to_hash, ":"))
398 if (!_dbus_string_copy (client_challenge, 0,
399 &to_hash, _dbus_string_get_length (&to_hash)))
402 if (!_dbus_string_append (&to_hash, ":"))
405 if (!_dbus_string_copy (&cookie, 0,
406 &to_hash, _dbus_string_get_length (&to_hash)))
409 if (!_dbus_sha_compute (&to_hash, hash))
415 _dbus_string_zero (&to_hash);
416 _dbus_string_free (&to_hash);
418 _dbus_string_zero (&cookie);
419 _dbus_string_free (&cookie);
423 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
424 * entropy, we use 128. This is the number of bytes in the random
427 #define N_CHALLENGE_BYTES (128/8)
430 sha1_handle_first_client_response (DBusAuth *auth,
431 const DBusString *data)
433 /* We haven't sent a challenge yet, we're expecting a desired
434 * username from the client.
444 _dbus_string_set_length (&auth->challenge, 0);
446 if (_dbus_string_get_length (data) > 0)
448 if (_dbus_string_get_length (&auth->identity) > 0)
450 /* Tried to send two auth identities, wtf */
451 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
452 return send_rejected (auth);
456 /* this is our auth identity */
457 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
462 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
464 _dbus_verbose ("Did not get a valid username from client\n");
465 return send_rejected (auth);
468 if (!_dbus_string_init (&tmp))
471 if (!_dbus_string_init (&tmp2))
473 _dbus_string_free (&tmp);
477 old_len = _dbus_string_get_length (&auth->outgoing);
479 /* we cache the keyring for speed, so here we drop it if it's the
480 * wrong one. FIXME caching the keyring here is useless since we use
481 * a different DBusAuth for every connection.
484 !_dbus_keyring_is_for_user (auth->keyring,
487 _dbus_keyring_unref (auth->keyring);
488 auth->keyring = NULL;
491 if (auth->keyring == NULL)
495 dbus_error_init (&error);
496 auth->keyring = _dbus_keyring_new_homedir (data,
500 if (auth->keyring == NULL)
502 if (dbus_error_has_name (&error,
503 DBUS_ERROR_NO_MEMORY))
505 dbus_error_free (&error);
510 _DBUS_ASSERT_ERROR_IS_SET (&error);
511 _dbus_verbose ("Error loading keyring: %s\n",
513 if (send_rejected (auth))
514 retval = TRUE; /* retval is only about mem */
515 dbus_error_free (&error);
521 _dbus_assert (!dbus_error_is_set (&error));
525 _dbus_assert (auth->keyring != NULL);
527 dbus_error_init (&error);
528 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
529 if (auth->cookie_id < 0)
531 _DBUS_ASSERT_ERROR_IS_SET (&error);
532 _dbus_verbose ("Could not get a cookie ID to send to client: %s\n",
534 if (send_rejected (auth))
536 dbus_error_free (&error);
541 _dbus_assert (!dbus_error_is_set (&error));
544 if (!_dbus_string_copy (&auth->context, 0,
545 &tmp2, _dbus_string_get_length (&tmp2)))
548 if (!_dbus_string_append (&tmp2, " "))
551 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
554 if (!_dbus_string_append (&tmp2, " "))
557 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
560 _dbus_string_set_length (&auth->challenge, 0);
561 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
564 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
565 _dbus_string_get_length (&tmp2)))
568 if (!_dbus_string_append (&auth->outgoing,
572 if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
573 _dbus_string_get_length (&auth->outgoing)))
576 if (!_dbus_string_append (&auth->outgoing,
583 _dbus_string_zero (&tmp);
584 _dbus_string_free (&tmp);
585 _dbus_string_zero (&tmp2);
586 _dbus_string_free (&tmp2);
588 _dbus_string_set_length (&auth->outgoing, old_len);
593 sha1_handle_second_client_response (DBusAuth *auth,
594 const DBusString *data)
596 /* We are expecting a response which is the hex-encoded client
597 * challenge, space, then SHA-1 hash of the concatenation of our
598 * challenge, ":", client challenge, ":", secret key, all
602 DBusString client_challenge;
603 DBusString client_hash;
605 DBusString correct_hash;
609 if (!_dbus_string_find_blank (data, 0, &i))
611 _dbus_verbose ("no space separator in client response\n");
612 return send_rejected (auth);
615 if (!_dbus_string_init (&client_challenge))
618 if (!_dbus_string_init (&client_hash))
621 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
625 _dbus_string_skip_blank (data, i, &i);
627 if (!_dbus_string_copy_len (data, i,
628 _dbus_string_get_length (data) - i,
633 if (_dbus_string_get_length (&client_challenge) == 0 ||
634 _dbus_string_get_length (&client_hash) == 0)
636 _dbus_verbose ("zero-length client challenge or hash\n");
637 if (send_rejected (auth))
642 if (!_dbus_string_init (&correct_hash))
645 if (!sha1_compute_hash (auth, auth->cookie_id,
651 /* if cookie_id was invalid, then we get an empty hash */
652 if (_dbus_string_get_length (&correct_hash) == 0)
654 if (send_rejected (auth))
659 if (!_dbus_string_equal (&client_hash, &correct_hash))
661 if (send_rejected (auth))
666 if (!_dbus_string_append (&auth->outgoing,
670 _dbus_verbose ("authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
671 auth->desired_identity.uid);
673 auth->authorized_identity = auth->desired_identity;
674 auth->authenticated_pending_begin = TRUE;
678 _dbus_string_zero (&correct_hash);
679 _dbus_string_free (&correct_hash);
681 _dbus_string_zero (&client_hash);
682 _dbus_string_free (&client_hash);
684 _dbus_string_free (&client_challenge);
690 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
691 const DBusString *data)
693 if (auth->cookie_id < 0)
694 return sha1_handle_first_client_response (auth, data);
696 return sha1_handle_second_client_response (auth, data);
700 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
702 auth->cookie_id = -1;
703 _dbus_string_set_length (&auth->challenge, 0);
707 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
708 DBusString *response)
710 const DBusString *username;
715 if (!_dbus_user_info_from_current_process (&username,
719 if (!_dbus_string_base64_encode (username, 0,
721 _dbus_string_get_length (response)))
730 /* FIXME if we send the server an error, right now both sides
731 * just hang. Server has to reject on getting an error, or
732 * client has to cancel. Should be in the spec.
735 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
736 const DBusString *data)
738 /* The data we get from the server should be the cookie context
739 * name, the cookie ID, and the server challenge, separated by
740 * spaces. We send back our challenge string and the correct hash.
744 DBusString cookie_id_str;
745 DBusString server_challenge;
746 DBusString client_challenge;
747 DBusString correct_hash;
755 if (!_dbus_string_find_blank (data, 0, &i))
757 if (_dbus_string_append (&auth->outgoing,
758 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
763 if (!_dbus_string_init (&context))
766 if (!_dbus_string_copy_len (data, 0, i,
770 _dbus_string_skip_blank (data, i, &i);
771 if (!_dbus_string_find_blank (data, i, &j))
773 if (_dbus_string_append (&auth->outgoing,
774 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
779 if (!_dbus_string_init (&cookie_id_str))
782 if (!_dbus_string_copy_len (data, i, j - i,
786 if (!_dbus_string_init (&server_challenge))
790 _dbus_string_skip_blank (data, i, &i);
791 j = _dbus_string_get_length (data);
793 if (!_dbus_string_copy_len (data, i, j - i,
794 &server_challenge, 0))
797 if (!_dbus_keyring_validate_context (&context))
799 if (_dbus_string_append (&auth->outgoing,
800 "ERROR \"Server sent invalid cookie context\"\r\n"))
805 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
807 if (_dbus_string_append (&auth->outgoing,
808 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
813 if (_dbus_string_get_length (&server_challenge) == 0)
815 if (_dbus_string_append (&auth->outgoing,
816 "ERROR \"Empty server challenge string\"\r\n"))
821 if (auth->keyring == NULL)
825 dbus_error_init (&error);
826 auth->keyring = _dbus_keyring_new_homedir (NULL,
830 if (auth->keyring == NULL)
832 if (dbus_error_has_name (&error,
833 DBUS_ERROR_NO_MEMORY))
835 dbus_error_free (&error);
840 _DBUS_ASSERT_ERROR_IS_SET (&error);
842 _dbus_verbose ("Error loading keyring: %s\n",
845 if (_dbus_string_append (&auth->outgoing,
846 "ERROR \"Could not load cookie file\"\r\n"))
847 retval = TRUE; /* retval is only about mem */
849 dbus_error_free (&error);
855 _dbus_assert (!dbus_error_is_set (&error));
859 _dbus_assert (auth->keyring != NULL);
861 if (!_dbus_string_init (&tmp))
864 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
867 if (!_dbus_string_init (&client_challenge))
870 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
873 if (!_dbus_string_init (&correct_hash))
876 if (!sha1_compute_hash (auth, val,
882 if (_dbus_string_get_length (&correct_hash) == 0)
884 /* couldn't find the cookie ID or something */
885 if (_dbus_string_append (&auth->outgoing,
886 "ERROR \"Don't have the requested cookie ID\"\r\n"))
891 _dbus_string_set_length (&tmp, 0);
893 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
894 _dbus_string_get_length (&tmp)))
897 if (!_dbus_string_append (&tmp, " "))
900 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
901 _dbus_string_get_length (&tmp)))
904 old_len = _dbus_string_get_length (&auth->outgoing);
905 if (!_dbus_string_append (&auth->outgoing, "DATA "))
908 if (!_dbus_string_base64_encode (&tmp, 0,
910 _dbus_string_get_length (&auth->outgoing)))
912 _dbus_string_set_length (&auth->outgoing, old_len);
916 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
918 _dbus_string_set_length (&auth->outgoing, old_len);
925 _dbus_string_zero (&correct_hash);
926 _dbus_string_free (&correct_hash);
928 _dbus_string_free (&client_challenge);
930 _dbus_string_zero (&tmp);
931 _dbus_string_free (&tmp);
933 _dbus_string_free (&server_challenge);
935 _dbus_string_free (&cookie_id_str);
937 _dbus_string_free (&context);
943 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
945 auth->cookie_id = -1;
946 _dbus_string_set_length (&auth->challenge, 0);
950 handle_server_data_external_mech (DBusAuth *auth,
951 const DBusString *data)
953 if (auth->credentials.uid == DBUS_UID_UNSET)
955 _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
956 return send_rejected (auth);
959 if (_dbus_string_get_length (data) > 0)
961 if (_dbus_string_get_length (&auth->identity) > 0)
963 /* Tried to send two auth identities, wtf */
964 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
965 return send_rejected (auth);
969 /* this is our auth identity */
970 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
975 /* Poke client for an auth identity, if none given */
976 if (_dbus_string_get_length (&auth->identity) == 0 &&
977 !auth->already_asked_for_initial_response)
979 if (_dbus_string_append (&auth->outgoing,
982 _dbus_verbose ("sending empty challenge asking client for auth identity\n");
983 auth->already_asked_for_initial_response = TRUE;
990 _dbus_credentials_clear (&auth->desired_identity);
992 /* If auth->identity is still empty here, then client
993 * responded with an empty string after we poked it for
994 * an initial response. This means to try to auth the
995 * identity provided in the credentials.
997 if (_dbus_string_get_length (&auth->identity) == 0)
999 auth->desired_identity.uid = auth->credentials.uid;
1003 if (!_dbus_credentials_from_uid_string (&auth->identity,
1004 &auth->desired_identity))
1006 _dbus_verbose ("could not get credentials from uid string\n");
1007 return send_rejected (auth);
1011 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1013 _dbus_verbose ("desired user %s is no good\n",
1014 _dbus_string_get_const_data (&auth->identity));
1015 return send_rejected (auth);
1018 if (_dbus_credentials_match (&auth->desired_identity,
1019 &auth->credentials))
1021 /* client has authenticated */
1022 if (!_dbus_string_append (&auth->outgoing,
1026 _dbus_verbose ("authenticated client with UID "DBUS_UID_FORMAT
1027 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1028 auth->desired_identity.uid,
1029 auth->credentials.uid);
1031 auth->authorized_identity.uid = auth->desired_identity.uid;
1033 auth->authenticated_pending_begin = TRUE;
1039 _dbus_verbose ("credentials uid="DBUS_UID_FORMAT
1040 " gid="DBUS_GID_FORMAT
1041 " do not allow uid="DBUS_UID_FORMAT
1042 " gid="DBUS_GID_FORMAT"\n",
1043 auth->credentials.uid, auth->credentials.gid,
1044 auth->desired_identity.uid, auth->desired_identity.gid);
1045 return send_rejected (auth);
1050 handle_server_shutdown_external_mech (DBusAuth *auth)
1056 handle_client_initial_response_external_mech (DBusAuth *auth,
1057 DBusString *response)
1059 /* We always append our UID as an initial response, so the server
1060 * doesn't have to send back an empty challenge to check whether we
1061 * want to specify an identity. i.e. this avoids a round trip that
1062 * the spec for the EXTERNAL mechanism otherwise requires.
1064 DBusString plaintext;
1066 if (!_dbus_string_init (&plaintext))
1069 if (!_dbus_string_append_our_uid (&plaintext))
1072 if (!_dbus_string_base64_encode (&plaintext, 0,
1074 _dbus_string_get_length (response)))
1077 _dbus_string_free (&plaintext);
1082 _dbus_string_free (&plaintext);
1087 handle_client_data_external_mech (DBusAuth *auth,
1088 const DBusString *data)
1095 handle_client_shutdown_external_mech (DBusAuth *auth)
1100 /* Put mechanisms here in order of preference.
1101 * What I eventually want to have is:
1103 * - a mechanism that checks UNIX domain socket credentials
1104 * - a simple magic cookie mechanism like X11 or ICE
1105 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1106 * offers such as Kerberos, X509, whatever.
1109 static const DBusAuthMechanismHandler
1110 all_mechanisms[] = {
1112 handle_server_data_external_mech,
1114 handle_server_shutdown_external_mech,
1115 handle_client_initial_response_external_mech,
1116 handle_client_data_external_mech,
1118 handle_client_shutdown_external_mech },
1119 { "DBUS_COOKIE_SHA1",
1120 handle_server_data_cookie_sha1_mech,
1122 handle_server_shutdown_cookie_sha1_mech,
1123 handle_client_initial_response_cookie_sha1_mech,
1124 handle_client_data_cookie_sha1_mech,
1126 handle_client_shutdown_cookie_sha1_mech },
1130 static const DBusAuthMechanismHandler*
1131 find_mech (const DBusString *name,
1132 char **allowed_mechs)
1136 if (allowed_mechs != NULL &&
1137 !_dbus_string_array_contains ((const char**) allowed_mechs,
1138 _dbus_string_get_const_data (name)))
1142 while (all_mechanisms[i].mechanism != NULL)
1144 if (_dbus_string_equal_c_str (name,
1145 all_mechanisms[i].mechanism))
1147 return &all_mechanisms[i];
1156 send_rejected (DBusAuth *auth)
1159 DBusAuthServer *server_auth;
1162 if (!_dbus_string_init (&command))
1165 if (!_dbus_string_append (&command,
1170 while (all_mechanisms[i].mechanism != NULL)
1172 if (!_dbus_string_append (&command,
1176 if (!_dbus_string_append (&command,
1177 all_mechanisms[i].mechanism))
1183 if (!_dbus_string_append (&command, "\r\n"))
1186 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1187 _dbus_string_get_length (&auth->outgoing)))
1190 shutdown_mech (auth);
1192 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1193 server_auth = DBUS_AUTH_SERVER (auth);
1194 server_auth->failures += 1;
1196 _dbus_string_free (&command);
1201 _dbus_string_free (&command);
1206 process_auth (DBusAuth *auth,
1207 const DBusString *command,
1208 const DBusString *args)
1212 /* We are already using a mechanism, client is on crack */
1213 if (!_dbus_string_append (&auth->outgoing,
1214 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1219 else if (_dbus_string_get_length (args) == 0)
1221 /* No args to the auth, send mechanisms */
1222 if (!send_rejected (auth))
1231 DBusString base64_response;
1232 DBusString decoded_response;
1234 _dbus_string_find_blank (args, 0, &i);
1236 if (!_dbus_string_init (&mech))
1239 if (!_dbus_string_init (&base64_response))
1241 _dbus_string_free (&mech);
1245 if (!_dbus_string_init (&decoded_response))
1247 _dbus_string_free (&mech);
1248 _dbus_string_free (&base64_response);
1252 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1255 if (!_dbus_string_copy (args, i, &base64_response, 0))
1258 if (!_dbus_string_base64_decode (&base64_response, 0,
1259 &decoded_response, 0))
1262 auth->mech = find_mech (&mech, auth->allowed_mechs);
1263 if (auth->mech != NULL)
1265 _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
1266 auth->mech->mechanism,
1267 _dbus_string_get_length (&decoded_response));
1269 if (!(* auth->mech->server_data_func) (auth,
1275 /* Unsupported mechanism */
1276 if (!send_rejected (auth))
1280 _dbus_string_free (&mech);
1281 _dbus_string_free (&base64_response);
1282 _dbus_string_free (&decoded_response);
1288 _dbus_string_free (&mech);
1289 _dbus_string_free (&base64_response);
1290 _dbus_string_free (&decoded_response);
1296 process_cancel (DBusAuth *auth,
1297 const DBusString *command,
1298 const DBusString *args)
1300 shutdown_mech (auth);
1306 process_begin (DBusAuth *auth,
1307 const DBusString *command,
1308 const DBusString *args)
1310 if (auth->authenticated_pending_begin)
1311 auth->authenticated = TRUE;
1314 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1317 shutdown_mech (auth);
1324 process_data_server (DBusAuth *auth,
1325 const DBusString *command,
1326 const DBusString *args)
1328 if (auth->mech != NULL)
1332 if (!_dbus_string_init (&decoded))
1335 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1337 _dbus_string_free (&decoded);
1341 #ifdef DBUS_ENABLE_VERBOSE_MODE
1342 if (_dbus_string_validate_ascii (&decoded, 0,
1343 _dbus_string_get_length (&decoded)))
1344 _dbus_verbose ("data: '%s'\n", _dbus_string_get_const_data (&decoded));
1347 if (!(* auth->mech->server_data_func) (auth, &decoded))
1349 _dbus_string_free (&decoded);
1353 _dbus_string_free (&decoded);
1357 if (!_dbus_string_append (&auth->outgoing,
1358 "ERROR \"Not currently in an auth conversation\"\r\n"))
1366 process_error_server (DBusAuth *auth,
1367 const DBusString *command,
1368 const DBusString *args)
1374 /* return FALSE if no memory, TRUE if all OK */
1376 get_word (const DBusString *str,
1382 _dbus_string_skip_blank (str, *start, start);
1383 _dbus_string_find_blank (str, *start, &i);
1387 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1397 record_mechanisms (DBusAuth *auth,
1398 const DBusString *command,
1399 const DBusString *args)
1404 if (auth->already_got_mechanisms)
1407 len = _dbus_string_get_length (args);
1413 const DBusAuthMechanismHandler *mech;
1415 if (!_dbus_string_init (&m))
1418 if (!get_word (args, &next, &m))
1421 mech = find_mech (&m, auth->allowed_mechs);
1425 /* FIXME right now we try mechanisms in the order
1426 * the server lists them; should we do them in
1427 * some more deterministic order?
1429 * Probably in all_mechanisms order, our order of
1430 * preference. Of course when the server is us,
1431 * it lists things in that order anyhow.
1434 _dbus_verbose ("Adding mechanism %s to list we will try\n",
1437 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1443 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1444 _dbus_string_get_const_data (&m));
1447 _dbus_string_free (&m);
1450 auth->already_got_mechanisms = TRUE;
1455 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1461 client_try_next_mechanism (DBusAuth *auth)
1463 const DBusAuthMechanismHandler *mech;
1464 DBusString auth_command;
1465 DBusAuthClient *client;
1467 client = DBUS_AUTH_CLIENT (auth);
1469 /* Pop any mechs not in the list of allowed mechanisms */
1471 while (client->mechs_to_try != NULL)
1473 mech = client->mechs_to_try->data;
1475 if (auth->allowed_mechs != NULL &&
1476 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1479 /* don't try this one after all */
1480 _dbus_verbose ("Mechanism %s isn't in the list of allowed mechanisms\n",
1483 _dbus_list_pop_first (& client->mechs_to_try);
1486 break; /* we'll try this one */
1492 if (!_dbus_string_init (&auth_command))
1495 if (!_dbus_string_append (&auth_command,
1498 _dbus_string_free (&auth_command);
1502 if (!_dbus_string_append (&auth_command,
1505 _dbus_string_free (&auth_command);
1509 if (mech->client_initial_response_func != NULL)
1511 if (!_dbus_string_append (&auth_command, " "))
1513 _dbus_string_free (&auth_command);
1517 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1519 _dbus_string_free (&auth_command);
1524 if (!_dbus_string_append (&auth_command,
1527 _dbus_string_free (&auth_command);
1531 if (!_dbus_string_copy (&auth_command, 0,
1533 _dbus_string_get_length (&auth->outgoing)))
1535 _dbus_string_free (&auth_command);
1540 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1542 _dbus_verbose ("Trying mechanism %s\n",
1543 auth->mech->mechanism);
1545 _dbus_string_free (&auth_command);
1551 process_rejected (DBusAuth *auth,
1552 const DBusString *command,
1553 const DBusString *args)
1555 shutdown_mech (auth);
1557 if (!auth->already_got_mechanisms)
1559 if (!record_mechanisms (auth, command, args))
1563 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1565 client_try_next_mechanism (auth);
1570 auth->need_disconnect = TRUE;
1577 process_ok (DBusAuth *auth,
1578 const DBusString *command,
1579 const DBusString *args)
1581 if (!_dbus_string_append (&auth->outgoing,
1585 auth->authenticated_pending_output = TRUE;
1591 process_data_client (DBusAuth *auth,
1592 const DBusString *command,
1593 const DBusString *args)
1595 if (auth->mech != NULL)
1599 if (!_dbus_string_init (&decoded))
1602 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1604 _dbus_string_free (&decoded);
1608 #ifdef DBUS_ENABLE_VERBOSE_MODE
1609 if (_dbus_string_validate_ascii (&decoded, 0,
1610 _dbus_string_get_length (&decoded)))
1612 _dbus_verbose ("data: '%s'\n",
1613 _dbus_string_get_const_data (&decoded));
1617 if (!(* auth->mech->client_data_func) (auth, &decoded))
1619 _dbus_string_free (&decoded);
1623 _dbus_string_free (&decoded);
1627 if (!_dbus_string_append (&auth->outgoing,
1628 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1636 process_error_client (DBusAuth *auth,
1637 const DBusString *command,
1638 const DBusString *args)
1644 process_unknown (DBusAuth *auth,
1645 const DBusString *command,
1646 const DBusString *args)
1648 if (!_dbus_string_append (&auth->outgoing,
1649 "ERROR \"Unknown command\"\r\n"))
1655 /* returns whether to call it again right away */
1657 process_command (DBusAuth *auth)
1665 /* _dbus_verbose (" trying process_command()\n"); */
1670 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1673 if (!_dbus_string_init (&command))
1675 auth->needed_memory = TRUE;
1679 if (!_dbus_string_init (&args))
1681 _dbus_string_free (&command);
1682 auth->needed_memory = TRUE;
1686 if (eol > _DBUS_ONE_MEGABYTE)
1688 /* This is a giant line, someone is trying to hose us. */
1689 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1695 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1698 if (!_dbus_string_validate_ascii (&command, 0,
1699 _dbus_string_get_length (&command)))
1701 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1702 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1708 _dbus_verbose ("got command \"%s\"\n", _dbus_string_get_const_data (&command));
1710 _dbus_string_find_blank (&command, 0, &i);
1711 _dbus_string_skip_blank (&command, i, &j);
1714 _dbus_string_delete (&command, i, j - i);
1716 if (!_dbus_string_move (&command, i, &args, 0))
1720 while (auth->handlers[i].command != NULL)
1722 if (_dbus_string_equal_c_str (&command,
1723 auth->handlers[i].command))
1725 _dbus_verbose ("Processing auth command %s\n",
1726 auth->handlers[i].command);
1728 if (!(* auth->handlers[i].func) (auth, &command, &args))
1736 if (auth->handlers[i].command == NULL)
1738 if (!process_unknown (auth, &command, &args))
1744 /* We've succeeded in processing the whole command so drop it out
1745 * of the incoming buffer and return TRUE to try another command.
1748 _dbus_string_delete (&auth->incoming, 0, eol);
1751 _dbus_string_delete (&auth->incoming, 0, 2);
1756 _dbus_string_free (&args);
1757 _dbus_string_free (&command);
1760 auth->needed_memory = TRUE;
1762 auth->needed_memory = FALSE;
1771 * @addtogroup DBusAuth
1776 * Creates a new auth conversation object for the server side.
1777 * See doc/dbus-sasl-profile.txt for full details on what
1780 * @returns the new object or #NULL if no memory
1783 _dbus_auth_server_new (void)
1786 DBusAuthServer *server_auth;
1788 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1792 auth->handlers = server_handlers;
1794 server_auth = DBUS_AUTH_SERVER (auth);
1796 /* perhaps this should be per-mechanism with a lower
1799 server_auth->failures = 0;
1800 server_auth->max_failures = 6;
1806 * Creates a new auth conversation object for the client side.
1807 * See doc/dbus-sasl-profile.txt for full details on what
1810 * @returns the new object or #NULL if no memory
1813 _dbus_auth_client_new (void)
1817 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1821 auth->handlers = client_handlers;
1823 /* Add a default mechanism to try */
1824 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1825 (void*) &all_mechanisms[0]))
1827 _dbus_auth_unref (auth);
1831 /* Now try the mechanism we just added */
1832 if (!client_try_next_mechanism (auth))
1834 _dbus_auth_unref (auth);
1842 * Increments the refcount of an auth object.
1844 * @param auth the auth conversation
1847 _dbus_auth_ref (DBusAuth *auth)
1849 _dbus_assert (auth != NULL);
1851 auth->refcount += 1;
1855 * Decrements the refcount of an auth object.
1857 * @param auth the auth conversation
1860 _dbus_auth_unref (DBusAuth *auth)
1862 _dbus_assert (auth != NULL);
1863 _dbus_assert (auth->refcount > 0);
1865 auth->refcount -= 1;
1866 if (auth->refcount == 0)
1868 shutdown_mech (auth);
1870 if (DBUS_AUTH_IS_CLIENT (auth))
1872 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1876 _dbus_keyring_unref (auth->keyring);
1878 _dbus_string_free (&auth->context);
1879 _dbus_string_free (&auth->challenge);
1880 _dbus_string_free (&auth->identity);
1881 _dbus_string_free (&auth->incoming);
1882 _dbus_string_free (&auth->outgoing);
1884 dbus_free_string_array (auth->allowed_mechs);
1891 * Sets an array of authentication mechanism names
1892 * that we are willing to use.
1894 * @param auth the auth conversation
1895 * @param mechanisms #NULL-terminated array of mechanism names
1896 * @returns #FALSE if no memory
1899 _dbus_auth_set_mechanisms (DBusAuth *auth,
1900 const char **mechanisms)
1904 if (mechanisms != NULL)
1906 copy = _dbus_dup_string_array (mechanisms);
1913 dbus_free_string_array (auth->allowed_mechs);
1915 auth->allowed_mechs = copy;
1921 * @param auth the auth conversation object
1922 * @returns #TRUE if we're in a final state
1924 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1927 * Analyzes buffered input and moves the auth conversation forward,
1928 * returning the new state of the auth conversation.
1930 * @param auth the auth conversation
1931 * @returns the new state
1934 _dbus_auth_do_work (DBusAuth *auth)
1936 auth->needed_memory = FALSE;
1938 /* Max amount we'll buffer up before deciding someone's on crack */
1939 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1943 if (DBUS_AUTH_IN_END_STATE (auth))
1946 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1947 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1949 auth->need_disconnect = TRUE;
1950 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1954 if (auth->mech == NULL &&
1955 auth->already_got_mechanisms &&
1956 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1958 auth->need_disconnect = TRUE;
1959 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1963 while (process_command (auth));
1965 if (DBUS_AUTH_IS_SERVER (auth) &&
1966 DBUS_AUTH_SERVER (auth)->failures >=
1967 DBUS_AUTH_SERVER (auth)->max_failures)
1968 auth->need_disconnect = TRUE;
1970 if (auth->need_disconnect)
1971 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1972 else if (auth->authenticated)
1974 if (_dbus_string_get_length (&auth->incoming) > 0)
1975 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1977 return DBUS_AUTH_STATE_AUTHENTICATED;
1979 else if (auth->needed_memory)
1980 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1981 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1982 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1984 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1988 * Gets bytes that need to be sent to the peer we're conversing with.
1989 * After writing some bytes, _dbus_auth_bytes_sent() must be called
1990 * to notify the auth object that they were written.
1992 * @param auth the auth conversation
1993 * @param str return location for a ref to the buffer to send
1994 * @returns #FALSE if nothing to send
1997 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
1998 const DBusString **str)
2000 _dbus_assert (auth != NULL);
2001 _dbus_assert (str != NULL);
2005 if (DBUS_AUTH_IN_END_STATE (auth))
2008 if (_dbus_string_get_length (&auth->outgoing) == 0)
2011 *str = &auth->outgoing;
2017 * Notifies the auth conversation object that
2018 * the given number of bytes of the outgoing buffer
2019 * have been written out.
2021 * @param auth the auth conversation
2022 * @param bytes_sent number of bytes written out
2025 _dbus_auth_bytes_sent (DBusAuth *auth,
2028 _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent,
2029 _dbus_string_get_const_data (&auth->outgoing));
2031 _dbus_string_delete (&auth->outgoing,
2034 if (auth->authenticated_pending_output &&
2035 _dbus_string_get_length (&auth->outgoing) == 0)
2036 auth->authenticated = TRUE;
2040 * Get a buffer to be used for reading bytes from the peer we're conversing
2041 * with. Bytes should be appended to this buffer.
2043 * @param auth the auth conversation
2044 * @param buffer return location for buffer to append bytes to
2047 _dbus_auth_get_buffer (DBusAuth *auth,
2048 DBusString **buffer)
2050 _dbus_assert (auth != NULL);
2051 _dbus_assert (!auth->buffer_outstanding);
2053 *buffer = &auth->incoming;
2055 auth->buffer_outstanding = TRUE;
2059 * Returns a buffer with new data read into it.
2061 * @param auth the auth conversation
2062 * @param buffer the buffer being returned
2063 * @param bytes_read number of new bytes added
2066 _dbus_auth_return_buffer (DBusAuth *auth,
2070 _dbus_assert (buffer == &auth->incoming);
2071 _dbus_assert (auth->buffer_outstanding);
2073 auth->buffer_outstanding = FALSE;
2077 * Returns leftover bytes that were not used as part of the auth
2078 * conversation. These bytes will be part of the message stream
2079 * instead. This function may not be called until authentication has
2082 * @param auth the auth conversation
2083 * @param str return location for pointer to string of unused bytes
2086 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2087 const DBusString **str)
2089 if (!DBUS_AUTH_IN_END_STATE (auth))
2092 *str = &auth->incoming;
2097 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2098 * after we've gotten them and successfully moved them elsewhere.
2100 * @param auth the auth conversation
2103 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2105 if (!DBUS_AUTH_IN_END_STATE (auth))
2108 _dbus_string_set_length (&auth->incoming, 0);
2112 * Called post-authentication, indicates whether we need to encode
2113 * the message stream with _dbus_auth_encode_data() prior to
2114 * sending it to the peer.
2116 * @param auth the auth conversation
2117 * @returns #TRUE if we need to encode the stream
2120 _dbus_auth_needs_encoding (DBusAuth *auth)
2122 if (!auth->authenticated)
2125 if (auth->mech != NULL)
2127 if (DBUS_AUTH_IS_CLIENT (auth))
2128 return auth->mech->client_encode_func != NULL;
2130 return auth->mech->server_encode_func != NULL;
2137 * Called post-authentication, encodes a block of bytes for sending to
2138 * the peer. If no encoding was negotiated, just copies the bytes
2139 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2141 * @param auth the auth conversation
2142 * @param plaintext the plain text data
2143 * @param encoded initialized string to where encoded data is appended
2144 * @returns #TRUE if we had enough memory and successfully encoded
2147 _dbus_auth_encode_data (DBusAuth *auth,
2148 const DBusString *plaintext,
2149 DBusString *encoded)
2151 _dbus_assert (plaintext != encoded);
2153 if (!auth->authenticated)
2156 if (_dbus_auth_needs_encoding (auth))
2158 if (DBUS_AUTH_IS_CLIENT (auth))
2159 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2161 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2165 return _dbus_string_copy (plaintext, 0, encoded,
2166 _dbus_string_get_length (encoded));
2171 * Called post-authentication, indicates whether we need to decode
2172 * the message stream with _dbus_auth_decode_data() after
2173 * receiving it from the peer.
2175 * @param auth the auth conversation
2176 * @returns #TRUE if we need to encode the stream
2179 _dbus_auth_needs_decoding (DBusAuth *auth)
2181 if (!auth->authenticated)
2184 if (auth->mech != NULL)
2186 if (DBUS_AUTH_IS_CLIENT (auth))
2187 return auth->mech->client_decode_func != NULL;
2189 return auth->mech->server_decode_func != NULL;
2197 * Called post-authentication, decodes a block of bytes received from
2198 * the peer. If no encoding was negotiated, just copies the bytes (you
2199 * can avoid this by checking _dbus_auth_needs_decoding()).
2201 * @todo We need to be able to distinguish "out of memory" error
2202 * from "the data is hosed" error.
2204 * @param auth the auth conversation
2205 * @param encoded the encoded data
2206 * @param plaintext initialized string where decoded data is appended
2207 * @returns #TRUE if we had enough memory and successfully decoded
2210 _dbus_auth_decode_data (DBusAuth *auth,
2211 const DBusString *encoded,
2212 DBusString *plaintext)
2214 _dbus_assert (plaintext != encoded);
2216 if (!auth->authenticated)
2219 if (_dbus_auth_needs_decoding (auth))
2221 if (DBUS_AUTH_IS_CLIENT (auth))
2222 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2224 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2228 return _dbus_string_copy (encoded, 0, plaintext,
2229 _dbus_string_get_length (plaintext));
2234 * Sets credentials received via reliable means from the operating
2237 * @param auth the auth conversation
2238 * @param credentials the credentials received
2241 _dbus_auth_set_credentials (DBusAuth *auth,
2242 const DBusCredentials *credentials)
2244 auth->credentials = *credentials;
2248 * Gets the identity we authorized the client as. Apps may have
2249 * different policies as to what identities they allow.
2251 * @param auth the auth conversation
2252 * @param credentials the credentials we've authorized
2255 _dbus_auth_get_identity (DBusAuth *auth,
2256 DBusCredentials *credentials)
2258 if (auth->authenticated)
2259 *credentials = auth->authorized_identity;
2261 _dbus_credentials_clear (credentials);
2265 * Sets the "authentication context" which scopes cookies
2266 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2268 * @param auth the auth conversation
2269 * @param context the context
2270 * @returns #FALSE if no memory
2273 _dbus_auth_set_context (DBusAuth *auth,
2274 const DBusString *context)
2276 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2277 &auth->context, 0, _dbus_string_get_length (context));
2282 #ifdef DBUS_BUILD_TESTS
2283 #include "dbus-test.h"
2284 #include "dbus-auth-script.h"
2288 process_test_subdir (const DBusString *test_base_dir,
2291 DBusString test_directory;
2292 DBusString filename;
2300 if (!_dbus_string_init (&test_directory))
2301 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2303 _dbus_string_init_const (&filename, subdir);
2305 if (!_dbus_string_copy (test_base_dir, 0,
2306 &test_directory, 0))
2307 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2309 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2310 _dbus_assert_not_reached ("couldn't allocate full path");
2312 _dbus_string_free (&filename);
2313 if (!_dbus_string_init (&filename))
2314 _dbus_assert_not_reached ("didn't allocate filename string\n");
2316 dbus_error_init (&error);
2317 dir = _dbus_directory_open (&test_directory, &error);
2320 _dbus_warn ("Could not open %s: %s\n",
2321 _dbus_string_get_const_data (&test_directory),
2323 dbus_error_free (&error);
2327 printf ("Testing:\n");
2330 while (_dbus_directory_get_next_file (dir, &filename, &error))
2332 DBusString full_path;
2334 if (!_dbus_string_init (&full_path))
2335 _dbus_assert_not_reached ("couldn't init string");
2337 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2338 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2340 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2341 _dbus_assert_not_reached ("couldn't concat file to dir");
2343 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2345 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2346 _dbus_string_get_const_data (&filename));
2347 _dbus_string_free (&full_path);
2351 printf (" %s\n", _dbus_string_get_const_data (&filename));
2353 if (!_dbus_auth_script_run (&full_path))
2355 _dbus_string_free (&full_path);
2359 _dbus_string_free (&full_path);
2362 if (dbus_error_is_set (&error))
2364 _dbus_warn ("Could not get next file in %s: %s\n",
2365 _dbus_string_get_const_data (&test_directory), error.message);
2366 dbus_error_free (&error);
2375 _dbus_directory_close (dir);
2376 _dbus_string_free (&test_directory);
2377 _dbus_string_free (&filename);
2383 process_test_dirs (const char *test_data_dir)
2385 DBusString test_directory;
2390 _dbus_string_init_const (&test_directory, test_data_dir);
2392 if (!process_test_subdir (&test_directory, "auth"))
2399 _dbus_string_free (&test_directory);
2405 _dbus_auth_test (const char *test_data_dir)
2408 if (test_data_dir == NULL)
2411 if (!process_test_dirs (test_data_dir))
2417 #endif /* DBUS_BUILD_TESTS */