1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.0
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-auth.h"
24 #include "dbus-string.h"
25 #include "dbus-list.h"
26 #include "dbus-internals.h"
27 #include "dbus-keyring.h"
29 #include "dbus-userdb.h"
32 * @defgroup DBusAuth Authentication
33 * @ingroup DBusInternals
34 * @brief DBusAuth object
36 * DBusAuth manages the authentication negotiation when a connection
37 * is first established, and also manage any encryption used over a
40 * @todo some SASL profiles require sending the empty string as a
41 * challenge/response, but we don't currently allow that in our
44 * @todo DBusAuth really needs to be rewritten as an explicit state
45 * machine. Right now it's too hard to prove to yourself by inspection
48 * @todo right now sometimes both ends will block waiting for input
49 * from the other end, e.g. if there's an error during
52 * @todo the cookie keyring needs to be cached globally not just
53 * per-auth (which raises threadsafety issues too)
55 * @todo grep FIXME in dbus-auth.c
59 * @defgroup DBusAuthInternals Authentication implementation details
60 * @ingroup DBusInternals
61 * @brief DBusAuth implementation details
63 * Private details of authentication code.
69 * Processes a command. Returns whether we had enough memory to
70 * complete the operation.
72 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
73 const DBusString *command,
74 const DBusString *args);
77 * Handler for a given auth protocol command
81 const char *command; /**< Name of the command */
82 DBusProcessAuthCommandFunction func; /**< Function to handle the command */
83 } DBusAuthCommandHandler;
86 * This function appends an initial client response to the given string
88 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
89 DBusString *response);
92 * This function processes a block of data received from the peer.
93 * i.e. handles a DATA command.
95 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
96 const DBusString *data);
99 * This function encodes a block of data from the peer.
101 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
102 const DBusString *data,
103 DBusString *encoded);
106 * This function decodes a block of data from the peer.
108 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
109 const DBusString *data,
110 DBusString *decoded);
113 * This function is called when the mechanism is abandoned.
115 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
118 * Virtual table representing a particular auth mechanism.
122 const char *mechanism; /**< Name of the mechanism */
123 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
124 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
125 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
126 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
127 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
128 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
129 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
130 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
131 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
132 } DBusAuthMechanismHandler;
135 * Internal members of DBusAuth.
139 int refcount; /**< reference count */
141 DBusString incoming; /**< Incoming data buffer */
142 DBusString outgoing; /**< Outgoing data buffer */
144 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
146 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
148 DBusString identity; /**< Current identity we're authorizing
152 DBusCredentials credentials; /**< Credentials read from socket,
156 DBusCredentials authorized_identity; /**< Credentials that are authorized */
158 DBusCredentials desired_identity; /**< Identity client has requested */
160 DBusString context; /**< Cookie scope */
161 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
162 int cookie_id; /**< ID of cookie to use */
163 DBusString challenge; /**< Challenge sent to client */
165 char **allowed_mechs; /**< Mechanisms we're allowed to use,
166 * or #NULL if we can use any
169 unsigned int needed_memory : 1; /**< We needed memory to continue since last
170 * successful getting something done
172 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
173 unsigned int authenticated : 1; /**< We are authenticated */
174 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
175 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
176 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
177 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
178 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
182 * "Subclass" of DBusAuth for client side
186 DBusAuth base; /**< Parent class */
188 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
193 * "Subclass" of DBusAuth for server side.
197 DBusAuth base; /**< Parent class */
199 int failures; /**< Number of times client has been rejected */
200 int max_failures; /**< Number of times we reject before disconnect */
204 static dbus_bool_t process_auth (DBusAuth *auth,
205 const DBusString *command,
206 const DBusString *args);
207 static dbus_bool_t process_cancel (DBusAuth *auth,
208 const DBusString *command,
209 const DBusString *args);
210 static dbus_bool_t process_begin (DBusAuth *auth,
211 const DBusString *command,
212 const DBusString *args);
213 static dbus_bool_t process_data_server (DBusAuth *auth,
214 const DBusString *command,
215 const DBusString *args);
216 static dbus_bool_t process_error_server (DBusAuth *auth,
217 const DBusString *command,
218 const DBusString *args);
219 static dbus_bool_t process_rejected (DBusAuth *auth,
220 const DBusString *command,
221 const DBusString *args);
222 static dbus_bool_t process_ok (DBusAuth *auth,
223 const DBusString *command,
224 const DBusString *args);
225 static dbus_bool_t process_data_client (DBusAuth *auth,
226 const DBusString *command,
227 const DBusString *args);
228 static dbus_bool_t process_error_client (DBusAuth *auth,
229 const DBusString *command,
230 const DBusString *args);
233 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
234 static dbus_bool_t send_rejected (DBusAuth *auth);
236 static DBusAuthCommandHandler
237 server_handlers[] = {
238 { "AUTH", process_auth },
239 { "CANCEL", process_cancel },
240 { "BEGIN", process_begin },
241 { "DATA", process_data_server },
242 { "ERROR", process_error_server },
246 static DBusAuthCommandHandler
247 client_handlers[] = {
248 { "REJECTED", process_rejected },
249 { "OK", process_ok },
250 { "DATA", process_data_client },
251 { "ERROR", process_error_client },
256 * @param auth the auth conversation
257 * @returns #TRUE if the conversation is the server side
259 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
261 * @param auth the auth conversation
262 * @returns #TRUE if the conversation is the client side
264 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
266 * @param auth the auth conversation
267 * @returns auth cast to DBusAuthClient
269 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
271 * @param auth the auth conversation
272 * @returns auth cast to DBusAuthServer
274 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
277 * The name of the auth ("client" or "server")
278 * @param auth the auth conversation
281 #define DBUS_AUTH_NAME(auth) (DBUS_AUTH_IS_SERVER(auth) ? "server" : "client")
284 _dbus_auth_new (int size)
288 auth = dbus_malloc0 (size);
294 _dbus_credentials_clear (&auth->credentials);
295 _dbus_credentials_clear (&auth->authorized_identity);
296 _dbus_credentials_clear (&auth->desired_identity);
298 auth->keyring = NULL;
299 auth->cookie_id = -1;
301 /* note that we don't use the max string length feature,
302 * because you can't use that feature if you're going to
303 * try to recover from out-of-memory (it creates
304 * what looks like unrecoverable inability to alloc
305 * more space in the string). But we do handle
306 * overlong buffers in _dbus_auth_do_work().
309 if (!_dbus_string_init (&auth->incoming))
312 if (!_dbus_string_init (&auth->outgoing))
315 if (!_dbus_string_init (&auth->identity))
318 if (!_dbus_string_init (&auth->context))
321 if (!_dbus_string_init (&auth->challenge))
324 /* default context if none is specified */
325 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
331 _dbus_string_free (&auth->challenge);
333 _dbus_string_free (&auth->context);
335 _dbus_string_free (&auth->identity);
337 _dbus_string_free (&auth->outgoing);
339 _dbus_string_free (&auth->incoming);
346 shutdown_mech (DBusAuth *auth)
348 /* Cancel any auth */
349 auth->authenticated_pending_begin = FALSE;
350 auth->authenticated = FALSE;
351 auth->already_asked_for_initial_response = FALSE;
352 _dbus_string_set_length (&auth->identity, 0);
354 _dbus_credentials_clear (&auth->authorized_identity);
355 _dbus_credentials_clear (&auth->desired_identity);
357 if (auth->mech != NULL)
359 _dbus_verbose ("%s: Shutting down mechanism %s\n",
360 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
362 if (DBUS_AUTH_IS_CLIENT (auth))
363 (* auth->mech->client_shutdown_func) (auth);
365 (* auth->mech->server_shutdown_func) (auth);
371 /* Returns TRUE but with an empty string hash if the
372 * cookie_id isn't known. As with all this code
373 * TRUE just means we had enough memory.
376 sha1_compute_hash (DBusAuth *auth,
378 const DBusString *server_challenge,
379 const DBusString *client_challenge,
386 _dbus_assert (auth->keyring != NULL);
390 if (!_dbus_string_init (&cookie))
393 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
397 if (_dbus_string_get_length (&cookie) == 0)
403 if (!_dbus_string_init (&to_hash))
406 if (!_dbus_string_copy (server_challenge, 0,
407 &to_hash, _dbus_string_get_length (&to_hash)))
410 if (!_dbus_string_append (&to_hash, ":"))
413 if (!_dbus_string_copy (client_challenge, 0,
414 &to_hash, _dbus_string_get_length (&to_hash)))
417 if (!_dbus_string_append (&to_hash, ":"))
420 if (!_dbus_string_copy (&cookie, 0,
421 &to_hash, _dbus_string_get_length (&to_hash)))
424 if (!_dbus_sha_compute (&to_hash, hash))
430 _dbus_string_zero (&to_hash);
431 _dbus_string_free (&to_hash);
433 _dbus_string_zero (&cookie);
434 _dbus_string_free (&cookie);
438 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
439 * entropy, we use 128. This is the number of bytes in the random
442 #define N_CHALLENGE_BYTES (128/8)
445 sha1_handle_first_client_response (DBusAuth *auth,
446 const DBusString *data)
448 /* We haven't sent a challenge yet, we're expecting a desired
449 * username from the client.
459 _dbus_string_set_length (&auth->challenge, 0);
461 if (_dbus_string_get_length (data) > 0)
463 if (_dbus_string_get_length (&auth->identity) > 0)
465 /* Tried to send two auth identities, wtf */
466 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
467 DBUS_AUTH_NAME (auth));
468 return send_rejected (auth);
472 /* this is our auth identity */
473 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
478 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
480 _dbus_verbose ("%s: Did not get a valid username from client\n",
481 DBUS_AUTH_NAME (auth));
482 return send_rejected (auth);
485 if (!_dbus_string_init (&tmp))
488 if (!_dbus_string_init (&tmp2))
490 _dbus_string_free (&tmp);
494 old_len = _dbus_string_get_length (&auth->outgoing);
496 /* we cache the keyring for speed, so here we drop it if it's the
497 * wrong one. FIXME caching the keyring here is useless since we use
498 * a different DBusAuth for every connection.
501 !_dbus_keyring_is_for_user (auth->keyring,
504 _dbus_keyring_unref (auth->keyring);
505 auth->keyring = NULL;
508 if (auth->keyring == NULL)
512 dbus_error_init (&error);
513 auth->keyring = _dbus_keyring_new_homedir (data,
517 if (auth->keyring == NULL)
519 if (dbus_error_has_name (&error,
520 DBUS_ERROR_NO_MEMORY))
522 dbus_error_free (&error);
527 _DBUS_ASSERT_ERROR_IS_SET (&error);
528 _dbus_verbose ("%s: Error loading keyring: %s\n",
529 DBUS_AUTH_NAME (auth), error.message);
530 if (send_rejected (auth))
531 retval = TRUE; /* retval is only about mem */
532 dbus_error_free (&error);
538 _dbus_assert (!dbus_error_is_set (&error));
542 _dbus_assert (auth->keyring != NULL);
544 dbus_error_init (&error);
545 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
546 if (auth->cookie_id < 0)
548 _DBUS_ASSERT_ERROR_IS_SET (&error);
549 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
550 DBUS_AUTH_NAME (auth), error.message);
551 if (send_rejected (auth))
553 dbus_error_free (&error);
558 _dbus_assert (!dbus_error_is_set (&error));
561 if (!_dbus_string_copy (&auth->context, 0,
562 &tmp2, _dbus_string_get_length (&tmp2)))
565 if (!_dbus_string_append (&tmp2, " "))
568 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
571 if (!_dbus_string_append (&tmp2, " "))
574 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
577 _dbus_string_set_length (&auth->challenge, 0);
578 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
581 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
582 _dbus_string_get_length (&tmp2)))
585 if (!_dbus_string_append (&auth->outgoing,
589 if (!_dbus_string_hex_encode (&tmp2, 0, &auth->outgoing,
590 _dbus_string_get_length (&auth->outgoing)))
593 if (!_dbus_string_append (&auth->outgoing,
600 _dbus_string_zero (&tmp);
601 _dbus_string_free (&tmp);
602 _dbus_string_zero (&tmp2);
603 _dbus_string_free (&tmp2);
605 _dbus_string_set_length (&auth->outgoing, old_len);
610 sha1_handle_second_client_response (DBusAuth *auth,
611 const DBusString *data)
613 /* We are expecting a response which is the hex-encoded client
614 * challenge, space, then SHA-1 hash of the concatenation of our
615 * challenge, ":", client challenge, ":", secret key, all
619 DBusString client_challenge;
620 DBusString client_hash;
622 DBusString correct_hash;
626 if (!_dbus_string_find_blank (data, 0, &i))
628 _dbus_verbose ("%s: no space separator in client response\n",
629 DBUS_AUTH_NAME (auth));
630 return send_rejected (auth);
633 if (!_dbus_string_init (&client_challenge))
636 if (!_dbus_string_init (&client_hash))
639 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
643 _dbus_string_skip_blank (data, i, &i);
645 if (!_dbus_string_copy_len (data, i,
646 _dbus_string_get_length (data) - i,
651 if (_dbus_string_get_length (&client_challenge) == 0 ||
652 _dbus_string_get_length (&client_hash) == 0)
654 _dbus_verbose ("%s: zero-length client challenge or hash\n",
655 DBUS_AUTH_NAME (auth));
656 if (send_rejected (auth))
661 if (!_dbus_string_init (&correct_hash))
664 if (!sha1_compute_hash (auth, auth->cookie_id,
670 /* if cookie_id was invalid, then we get an empty hash */
671 if (_dbus_string_get_length (&correct_hash) == 0)
673 if (send_rejected (auth))
678 if (!_dbus_string_equal (&client_hash, &correct_hash))
680 if (send_rejected (auth))
685 if (!_dbus_string_append (&auth->outgoing,
689 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
690 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
692 auth->authorized_identity = auth->desired_identity;
693 auth->authenticated_pending_begin = TRUE;
697 _dbus_string_zero (&correct_hash);
698 _dbus_string_free (&correct_hash);
700 _dbus_string_zero (&client_hash);
701 _dbus_string_free (&client_hash);
703 _dbus_string_free (&client_challenge);
709 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
710 const DBusString *data)
712 if (auth->cookie_id < 0)
713 return sha1_handle_first_client_response (auth, data);
715 return sha1_handle_second_client_response (auth, data);
719 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
721 auth->cookie_id = -1;
722 _dbus_string_set_length (&auth->challenge, 0);
726 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
727 DBusString *response)
729 const DBusString *username;
734 if (!_dbus_username_from_current_process (&username))
737 if (!_dbus_string_hex_encode (username, 0,
739 _dbus_string_get_length (response)))
749 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
750 const DBusString *data)
752 /* The data we get from the server should be the cookie context
753 * name, the cookie ID, and the server challenge, separated by
754 * spaces. We send back our challenge string and the correct hash.
758 DBusString cookie_id_str;
759 DBusString server_challenge;
760 DBusString client_challenge;
761 DBusString correct_hash;
769 if (!_dbus_string_find_blank (data, 0, &i))
771 if (_dbus_string_append (&auth->outgoing,
772 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
777 if (!_dbus_string_init (&context))
780 if (!_dbus_string_copy_len (data, 0, i,
784 _dbus_string_skip_blank (data, i, &i);
785 if (!_dbus_string_find_blank (data, i, &j))
787 if (_dbus_string_append (&auth->outgoing,
788 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
793 if (!_dbus_string_init (&cookie_id_str))
796 if (!_dbus_string_copy_len (data, i, j - i,
800 if (!_dbus_string_init (&server_challenge))
804 _dbus_string_skip_blank (data, i, &i);
805 j = _dbus_string_get_length (data);
807 if (!_dbus_string_copy_len (data, i, j - i,
808 &server_challenge, 0))
811 if (!_dbus_keyring_validate_context (&context))
813 if (_dbus_string_append (&auth->outgoing,
814 "ERROR \"Server sent invalid cookie context\"\r\n"))
819 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
821 if (_dbus_string_append (&auth->outgoing,
822 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
827 if (_dbus_string_get_length (&server_challenge) == 0)
829 if (_dbus_string_append (&auth->outgoing,
830 "ERROR \"Empty server challenge string\"\r\n"))
835 if (auth->keyring == NULL)
839 dbus_error_init (&error);
840 auth->keyring = _dbus_keyring_new_homedir (NULL,
844 if (auth->keyring == NULL)
846 if (dbus_error_has_name (&error,
847 DBUS_ERROR_NO_MEMORY))
849 dbus_error_free (&error);
854 _DBUS_ASSERT_ERROR_IS_SET (&error);
856 _dbus_verbose ("%s: Error loading keyring: %s\n",
857 DBUS_AUTH_NAME (auth), error.message);
859 if (_dbus_string_append (&auth->outgoing,
860 "ERROR \"Could not load cookie file\"\r\n"))
861 retval = TRUE; /* retval is only about mem */
863 dbus_error_free (&error);
869 _dbus_assert (!dbus_error_is_set (&error));
873 _dbus_assert (auth->keyring != NULL);
875 if (!_dbus_string_init (&tmp))
878 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
881 if (!_dbus_string_init (&client_challenge))
884 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
887 if (!_dbus_string_init (&correct_hash))
890 if (!sha1_compute_hash (auth, val,
896 if (_dbus_string_get_length (&correct_hash) == 0)
898 /* couldn't find the cookie ID or something */
899 if (_dbus_string_append (&auth->outgoing,
900 "ERROR \"Don't have the requested cookie ID\"\r\n"))
905 _dbus_string_set_length (&tmp, 0);
907 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
908 _dbus_string_get_length (&tmp)))
911 if (!_dbus_string_append (&tmp, " "))
914 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
915 _dbus_string_get_length (&tmp)))
918 old_len = _dbus_string_get_length (&auth->outgoing);
919 if (!_dbus_string_append (&auth->outgoing, "DATA "))
922 if (!_dbus_string_hex_encode (&tmp, 0,
924 _dbus_string_get_length (&auth->outgoing)))
926 _dbus_string_set_length (&auth->outgoing, old_len);
930 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
932 _dbus_string_set_length (&auth->outgoing, old_len);
939 _dbus_string_zero (&correct_hash);
940 _dbus_string_free (&correct_hash);
942 _dbus_string_free (&client_challenge);
944 _dbus_string_zero (&tmp);
945 _dbus_string_free (&tmp);
947 _dbus_string_free (&server_challenge);
949 _dbus_string_free (&cookie_id_str);
951 _dbus_string_free (&context);
957 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
959 auth->cookie_id = -1;
960 _dbus_string_set_length (&auth->challenge, 0);
964 handle_server_data_external_mech (DBusAuth *auth,
965 const DBusString *data)
967 if (auth->credentials.uid == DBUS_UID_UNSET)
969 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
970 DBUS_AUTH_NAME (auth));
971 return send_rejected (auth);
974 if (_dbus_string_get_length (data) > 0)
976 if (_dbus_string_get_length (&auth->identity) > 0)
978 /* Tried to send two auth identities, wtf */
979 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
980 DBUS_AUTH_NAME (auth));
981 return send_rejected (auth);
985 /* this is our auth identity */
986 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
991 /* Poke client for an auth identity, if none given */
992 if (_dbus_string_get_length (&auth->identity) == 0 &&
993 !auth->already_asked_for_initial_response)
995 if (_dbus_string_append (&auth->outgoing,
998 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
999 DBUS_AUTH_NAME (auth));
1000 auth->already_asked_for_initial_response = TRUE;
1007 _dbus_credentials_clear (&auth->desired_identity);
1009 /* If auth->identity is still empty here, then client
1010 * responded with an empty string after we poked it for
1011 * an initial response. This means to try to auth the
1012 * identity provided in the credentials.
1014 if (_dbus_string_get_length (&auth->identity) == 0)
1016 auth->desired_identity.uid = auth->credentials.uid;
1020 if (!_dbus_uid_from_string (&auth->identity,
1021 &auth->desired_identity.uid))
1023 _dbus_verbose ("%s: could not get credentials from uid string\n",
1024 DBUS_AUTH_NAME (auth));
1025 return send_rejected (auth);
1029 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1031 _dbus_verbose ("%s: desired user %s is no good\n",
1032 DBUS_AUTH_NAME (auth),
1033 _dbus_string_get_const_data (&auth->identity));
1034 return send_rejected (auth);
1037 if (_dbus_credentials_match (&auth->desired_identity,
1038 &auth->credentials))
1040 /* client has authenticated */
1041 if (!_dbus_string_append (&auth->outgoing,
1045 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1046 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1047 DBUS_AUTH_NAME (auth),
1048 auth->desired_identity.uid,
1049 auth->credentials.uid);
1051 auth->authorized_identity.uid = auth->desired_identity.uid;
1053 auth->authenticated_pending_begin = TRUE;
1059 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1060 " gid="DBUS_GID_FORMAT
1061 " do not allow uid="DBUS_UID_FORMAT
1062 " gid="DBUS_GID_FORMAT"\n",
1063 DBUS_AUTH_NAME (auth),
1064 auth->credentials.uid, auth->credentials.gid,
1065 auth->desired_identity.uid, auth->desired_identity.gid);
1066 return send_rejected (auth);
1071 handle_server_shutdown_external_mech (DBusAuth *auth)
1077 handle_client_initial_response_external_mech (DBusAuth *auth,
1078 DBusString *response)
1080 /* We always append our UID as an initial response, so the server
1081 * doesn't have to send back an empty challenge to check whether we
1082 * want to specify an identity. i.e. this avoids a round trip that
1083 * the spec for the EXTERNAL mechanism otherwise requires.
1085 DBusString plaintext;
1087 if (!_dbus_string_init (&plaintext))
1090 if (!_dbus_string_append_uint (&plaintext,
1094 if (!_dbus_string_hex_encode (&plaintext, 0,
1096 _dbus_string_get_length (response)))
1099 _dbus_string_free (&plaintext);
1104 _dbus_string_free (&plaintext);
1109 handle_client_data_external_mech (DBusAuth *auth,
1110 const DBusString *data)
1117 handle_client_shutdown_external_mech (DBusAuth *auth)
1122 /* Put mechanisms here in order of preference.
1123 * What I eventually want to have is:
1125 * - a mechanism that checks UNIX domain socket credentials
1126 * - a simple magic cookie mechanism like X11 or ICE
1127 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1128 * offers such as Kerberos, X509, whatever.
1131 static const DBusAuthMechanismHandler
1132 all_mechanisms[] = {
1134 handle_server_data_external_mech,
1136 handle_server_shutdown_external_mech,
1137 handle_client_initial_response_external_mech,
1138 handle_client_data_external_mech,
1140 handle_client_shutdown_external_mech },
1141 { "DBUS_COOKIE_SHA1",
1142 handle_server_data_cookie_sha1_mech,
1144 handle_server_shutdown_cookie_sha1_mech,
1145 handle_client_initial_response_cookie_sha1_mech,
1146 handle_client_data_cookie_sha1_mech,
1148 handle_client_shutdown_cookie_sha1_mech },
1152 static const DBusAuthMechanismHandler*
1153 find_mech (const DBusString *name,
1154 char **allowed_mechs)
1158 if (allowed_mechs != NULL &&
1159 !_dbus_string_array_contains ((const char**) allowed_mechs,
1160 _dbus_string_get_const_data (name)))
1164 while (all_mechanisms[i].mechanism != NULL)
1166 if (_dbus_string_equal_c_str (name,
1167 all_mechanisms[i].mechanism))
1169 return &all_mechanisms[i];
1178 send_rejected (DBusAuth *auth)
1181 DBusAuthServer *server_auth;
1184 if (!_dbus_string_init (&command))
1187 if (!_dbus_string_append (&command,
1192 while (all_mechanisms[i].mechanism != NULL)
1194 if (!_dbus_string_append (&command,
1198 if (!_dbus_string_append (&command,
1199 all_mechanisms[i].mechanism))
1205 if (!_dbus_string_append (&command, "\r\n"))
1208 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1209 _dbus_string_get_length (&auth->outgoing)))
1212 shutdown_mech (auth);
1214 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1215 server_auth = DBUS_AUTH_SERVER (auth);
1216 server_auth->failures += 1;
1218 _dbus_string_free (&command);
1223 _dbus_string_free (&command);
1228 process_auth (DBusAuth *auth,
1229 const DBusString *command,
1230 const DBusString *args)
1234 /* We are already using a mechanism, client is on crack */
1235 if (!_dbus_string_append (&auth->outgoing,
1236 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1241 else if (_dbus_string_get_length (args) == 0)
1243 /* No args to the auth, send mechanisms */
1244 if (!send_rejected (auth))
1253 DBusString hex_response;
1254 DBusString decoded_response;
1256 _dbus_string_find_blank (args, 0, &i);
1258 if (!_dbus_string_init (&mech))
1261 if (!_dbus_string_init (&hex_response))
1263 _dbus_string_free (&mech);
1267 if (!_dbus_string_init (&decoded_response))
1269 _dbus_string_free (&mech);
1270 _dbus_string_free (&hex_response);
1274 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1277 _dbus_string_skip_blank (args, i, &i);
1278 if (!_dbus_string_copy (args, i, &hex_response, 0))
1281 if (!_dbus_string_hex_decode (&hex_response, 0, &end,
1282 &decoded_response, 0))
1285 if (_dbus_string_get_length (&hex_response) != end)
1287 if (!_dbus_string_append (&auth->outgoing,
1288 "ERROR \"Invalid hex encoding\"\r\n"))
1294 auth->mech = find_mech (&mech, auth->allowed_mechs);
1295 if (auth->mech != NULL)
1297 _dbus_verbose ("%s: Trying mechanism %s with initial response of %d bytes\n",
1298 DBUS_AUTH_NAME (auth),
1299 auth->mech->mechanism,
1300 _dbus_string_get_length (&decoded_response));
1302 if (!(* auth->mech->server_data_func) (auth,
1308 /* Unsupported mechanism */
1309 if (!send_rejected (auth))
1314 _dbus_string_free (&mech);
1315 _dbus_string_free (&hex_response);
1316 _dbus_string_free (&decoded_response);
1322 _dbus_string_free (&mech);
1323 _dbus_string_free (&hex_response);
1324 _dbus_string_free (&decoded_response);
1330 process_cancel (DBusAuth *auth,
1331 const DBusString *command,
1332 const DBusString *args)
1334 if (!send_rejected (auth))
1341 process_begin (DBusAuth *auth,
1342 const DBusString *command,
1343 const DBusString *args)
1345 if (auth->authenticated_pending_begin)
1346 auth->authenticated = TRUE;
1349 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1352 shutdown_mech (auth);
1359 process_data_server (DBusAuth *auth,
1360 const DBusString *command,
1361 const DBusString *args)
1365 if (auth->mech != NULL)
1369 if (!_dbus_string_init (&decoded))
1372 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1374 _dbus_string_free (&decoded);
1378 if (_dbus_string_get_length (args) != end)
1380 _dbus_string_free (&decoded);
1381 if (!_dbus_string_append (&auth->outgoing,
1382 "ERROR \"Invalid hex encoding\"\r\n"))
1388 #ifdef DBUS_ENABLE_VERBOSE_MODE
1389 if (_dbus_string_validate_ascii (&decoded, 0,
1390 _dbus_string_get_length (&decoded)))
1391 _dbus_verbose ("%s: data: '%s'\n",
1392 DBUS_AUTH_NAME (auth),
1393 _dbus_string_get_const_data (&decoded));
1396 if (!(* auth->mech->server_data_func) (auth, &decoded))
1398 _dbus_string_free (&decoded);
1402 _dbus_string_free (&decoded);
1406 if (!_dbus_string_append (&auth->outgoing,
1407 "ERROR \"Not currently in an auth conversation\"\r\n"))
1415 process_error_server (DBusAuth *auth,
1416 const DBusString *command,
1417 const DBusString *args)
1419 /* Server got error from client, reject the auth,
1420 * as we don't have anything more intelligent to do.
1422 if (!send_rejected (auth))
1428 /* return FALSE if no memory, TRUE if all OK */
1430 get_word (const DBusString *str,
1436 _dbus_string_skip_blank (str, *start, start);
1437 _dbus_string_find_blank (str, *start, &i);
1441 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1451 record_mechanisms (DBusAuth *auth,
1452 const DBusString *command,
1453 const DBusString *args)
1458 if (auth->already_got_mechanisms)
1461 len = _dbus_string_get_length (args);
1467 const DBusAuthMechanismHandler *mech;
1469 if (!_dbus_string_init (&m))
1472 if (!get_word (args, &next, &m))
1474 _dbus_string_free (&m);
1478 mech = find_mech (&m, auth->allowed_mechs);
1482 /* FIXME right now we try mechanisms in the order
1483 * the server lists them; should we do them in
1484 * some more deterministic order?
1486 * Probably in all_mechanisms order, our order of
1487 * preference. Of course when the server is us,
1488 * it lists things in that order anyhow.
1491 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1492 DBUS_AUTH_NAME (auth), mech->mechanism);
1494 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1497 _dbus_string_free (&m);
1503 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1504 DBUS_AUTH_NAME (auth),
1505 _dbus_string_get_const_data (&m));
1508 _dbus_string_free (&m);
1511 auth->already_got_mechanisms = TRUE;
1516 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1522 client_try_next_mechanism (DBusAuth *auth)
1524 const DBusAuthMechanismHandler *mech;
1525 DBusString auth_command;
1526 DBusAuthClient *client;
1528 client = DBUS_AUTH_CLIENT (auth);
1530 /* Pop any mechs not in the list of allowed mechanisms */
1532 while (client->mechs_to_try != NULL)
1534 mech = client->mechs_to_try->data;
1536 if (auth->allowed_mechs != NULL &&
1537 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1540 /* don't try this one after all */
1541 _dbus_verbose ("%s: Mechanism %s isn't in the list of allowed mechanisms\n",
1542 DBUS_AUTH_NAME (auth), mech->mechanism);
1544 _dbus_list_pop_first (& client->mechs_to_try);
1547 break; /* we'll try this one */
1553 if (!_dbus_string_init (&auth_command))
1556 if (!_dbus_string_append (&auth_command,
1559 _dbus_string_free (&auth_command);
1563 if (!_dbus_string_append (&auth_command,
1566 _dbus_string_free (&auth_command);
1570 if (mech->client_initial_response_func != NULL)
1572 if (!_dbus_string_append (&auth_command, " "))
1574 _dbus_string_free (&auth_command);
1578 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1580 _dbus_string_free (&auth_command);
1585 if (!_dbus_string_append (&auth_command,
1588 _dbus_string_free (&auth_command);
1592 if (!_dbus_string_copy (&auth_command, 0,
1594 _dbus_string_get_length (&auth->outgoing)))
1596 _dbus_string_free (&auth_command);
1601 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1603 _dbus_verbose ("%s: Trying mechanism %s\n",
1604 DBUS_AUTH_NAME (auth),
1605 auth->mech->mechanism);
1607 _dbus_string_free (&auth_command);
1613 process_rejected (DBusAuth *auth,
1614 const DBusString *command,
1615 const DBusString *args)
1617 shutdown_mech (auth);
1619 if (!auth->already_got_mechanisms)
1621 if (!record_mechanisms (auth, command, args))
1625 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1627 if (!client_try_next_mechanism (auth))
1633 auth->need_disconnect = TRUE;
1640 process_ok (DBusAuth *auth,
1641 const DBusString *command,
1642 const DBusString *args)
1644 if (!_dbus_string_append (&auth->outgoing,
1648 auth->authenticated_pending_output = TRUE;
1654 process_data_client (DBusAuth *auth,
1655 const DBusString *command,
1656 const DBusString *args)
1660 if (auth->mech != NULL)
1664 if (!_dbus_string_init (&decoded))
1667 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1669 _dbus_string_free (&decoded);
1673 if (_dbus_string_get_length (args) != end)
1675 _dbus_string_free (&decoded);
1676 if (!_dbus_string_append (&auth->outgoing,
1677 "ERROR \"Invalid hex encoding\"\r\n"))
1683 #ifdef DBUS_ENABLE_VERBOSE_MODE
1684 if (_dbus_string_validate_ascii (&decoded, 0,
1685 _dbus_string_get_length (&decoded)))
1687 _dbus_verbose ("%s: data: '%s'\n",
1688 DBUS_AUTH_NAME (auth),
1689 _dbus_string_get_const_data (&decoded));
1693 if (!(* auth->mech->client_data_func) (auth, &decoded))
1695 _dbus_string_free (&decoded);
1699 _dbus_string_free (&decoded);
1703 if (!_dbus_string_append (&auth->outgoing,
1704 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1712 process_error_client (DBusAuth *auth,
1713 const DBusString *command,
1714 const DBusString *args)
1716 /* Cancel current mechanism, as we don't have anything
1717 * more clever to do.
1719 if (!_dbus_string_append (&auth->outgoing,
1727 process_unknown (DBusAuth *auth,
1728 const DBusString *command,
1729 const DBusString *args)
1731 if (!_dbus_string_append (&auth->outgoing,
1732 "ERROR \"Unknown command\"\r\n"))
1738 /* returns whether to call it again right away */
1740 process_command (DBusAuth *auth)
1748 /* _dbus_verbose ("%s: trying process_command()\n"); */
1753 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1756 if (!_dbus_string_init (&command))
1758 auth->needed_memory = TRUE;
1762 if (!_dbus_string_init (&args))
1764 _dbus_string_free (&command);
1765 auth->needed_memory = TRUE;
1769 if (eol > _DBUS_ONE_MEGABYTE)
1771 /* This is a giant line, someone is trying to hose us. */
1772 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1778 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1781 if (!_dbus_string_validate_ascii (&command, 0,
1782 _dbus_string_get_length (&command)))
1784 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1785 DBUS_AUTH_NAME (auth));
1786 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1792 _dbus_verbose ("%s: got command \"%s\"\n",
1793 DBUS_AUTH_NAME (auth),
1794 _dbus_string_get_const_data (&command));
1796 _dbus_string_find_blank (&command, 0, &i);
1797 _dbus_string_skip_blank (&command, i, &j);
1800 _dbus_string_delete (&command, i, j - i);
1802 if (!_dbus_string_move (&command, i, &args, 0))
1806 while (auth->handlers[i].command != NULL)
1808 if (_dbus_string_equal_c_str (&command,
1809 auth->handlers[i].command))
1811 _dbus_verbose ("%s: Processing auth command %s\n",
1812 DBUS_AUTH_NAME (auth),
1813 auth->handlers[i].command);
1815 if (!(* auth->handlers[i].func) (auth, &command, &args))
1823 if (auth->handlers[i].command == NULL)
1825 if (!process_unknown (auth, &command, &args))
1831 /* We've succeeded in processing the whole command so drop it out
1832 * of the incoming buffer and return TRUE to try another command.
1835 _dbus_string_delete (&auth->incoming, 0, eol);
1838 _dbus_string_delete (&auth->incoming, 0, 2);
1843 _dbus_string_free (&args);
1844 _dbus_string_free (&command);
1847 auth->needed_memory = TRUE;
1849 auth->needed_memory = FALSE;
1858 * @addtogroup DBusAuth
1863 * Creates a new auth conversation object for the server side.
1864 * See doc/dbus-sasl-profile.txt for full details on what
1867 * @returns the new object or #NULL if no memory
1870 _dbus_auth_server_new (void)
1873 DBusAuthServer *server_auth;
1875 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1879 auth->handlers = server_handlers;
1881 server_auth = DBUS_AUTH_SERVER (auth);
1883 /* perhaps this should be per-mechanism with a lower
1886 server_auth->failures = 0;
1887 server_auth->max_failures = 6;
1893 * Creates a new auth conversation object for the client side.
1894 * See doc/dbus-sasl-profile.txt for full details on what
1897 * @returns the new object or #NULL if no memory
1900 _dbus_auth_client_new (void)
1904 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1908 auth->handlers = client_handlers;
1910 /* Add a default mechanism to try */
1911 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1912 (void*) &all_mechanisms[0]))
1914 _dbus_auth_unref (auth);
1918 /* Now try the mechanism we just added */
1919 if (!client_try_next_mechanism (auth))
1921 _dbus_auth_unref (auth);
1929 * Increments the refcount of an auth object.
1931 * @param auth the auth conversation
1932 * @returns the auth conversation
1935 _dbus_auth_ref (DBusAuth *auth)
1937 _dbus_assert (auth != NULL);
1939 auth->refcount += 1;
1945 * Decrements the refcount of an auth object.
1947 * @param auth the auth conversation
1950 _dbus_auth_unref (DBusAuth *auth)
1952 _dbus_assert (auth != NULL);
1953 _dbus_assert (auth->refcount > 0);
1955 auth->refcount -= 1;
1956 if (auth->refcount == 0)
1958 shutdown_mech (auth);
1960 if (DBUS_AUTH_IS_CLIENT (auth))
1962 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1966 _dbus_keyring_unref (auth->keyring);
1968 _dbus_string_free (&auth->context);
1969 _dbus_string_free (&auth->challenge);
1970 _dbus_string_free (&auth->identity);
1971 _dbus_string_free (&auth->incoming);
1972 _dbus_string_free (&auth->outgoing);
1974 dbus_free_string_array (auth->allowed_mechs);
1981 * Sets an array of authentication mechanism names
1982 * that we are willing to use.
1984 * @param auth the auth conversation
1985 * @param mechanisms #NULL-terminated array of mechanism names
1986 * @returns #FALSE if no memory
1989 _dbus_auth_set_mechanisms (DBusAuth *auth,
1990 const char **mechanisms)
1994 if (mechanisms != NULL)
1996 copy = _dbus_dup_string_array (mechanisms);
2003 dbus_free_string_array (auth->allowed_mechs);
2005 auth->allowed_mechs = copy;
2011 * @param auth the auth conversation object
2012 * @returns #TRUE if we're in a final state
2014 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
2017 * Analyzes buffered input and moves the auth conversation forward,
2018 * returning the new state of the auth conversation.
2020 * @param auth the auth conversation
2021 * @returns the new state
2024 _dbus_auth_do_work (DBusAuth *auth)
2026 auth->needed_memory = FALSE;
2028 /* Max amount we'll buffer up before deciding someone's on crack */
2029 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2033 if (DBUS_AUTH_IN_END_STATE (auth))
2036 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2037 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2039 auth->need_disconnect = TRUE;
2040 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2041 DBUS_AUTH_NAME (auth));
2045 if (auth->mech == NULL &&
2046 auth->already_got_mechanisms &&
2047 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
2049 auth->need_disconnect = TRUE;
2050 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2051 DBUS_AUTH_NAME (auth));
2055 while (process_command (auth));
2057 if (DBUS_AUTH_IS_SERVER (auth) &&
2058 DBUS_AUTH_SERVER (auth)->failures >=
2059 DBUS_AUTH_SERVER (auth)->max_failures)
2060 auth->need_disconnect = TRUE;
2062 if (auth->need_disconnect)
2063 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2064 else if (auth->authenticated)
2066 if (_dbus_string_get_length (&auth->incoming) > 0)
2067 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
2069 return DBUS_AUTH_STATE_AUTHENTICATED;
2071 else if (auth->needed_memory)
2072 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2073 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2074 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2076 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2080 * Gets bytes that need to be sent to the peer we're conversing with.
2081 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2082 * to notify the auth object that they were written.
2084 * @param auth the auth conversation
2085 * @param str return location for a ref to the buffer to send
2086 * @returns #FALSE if nothing to send
2089 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2090 const DBusString **str)
2092 _dbus_assert (auth != NULL);
2093 _dbus_assert (str != NULL);
2097 if (DBUS_AUTH_IN_END_STATE (auth))
2100 if (_dbus_string_get_length (&auth->outgoing) == 0)
2103 *str = &auth->outgoing;
2109 * Notifies the auth conversation object that
2110 * the given number of bytes of the outgoing buffer
2111 * have been written out.
2113 * @param auth the auth conversation
2114 * @param bytes_sent number of bytes written out
2117 _dbus_auth_bytes_sent (DBusAuth *auth,
2120 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2121 DBUS_AUTH_NAME (auth),
2123 _dbus_string_get_const_data (&auth->outgoing));
2125 _dbus_string_delete (&auth->outgoing,
2128 if (auth->authenticated_pending_output &&
2129 _dbus_string_get_length (&auth->outgoing) == 0)
2130 auth->authenticated = TRUE;
2134 * Get a buffer to be used for reading bytes from the peer we're conversing
2135 * with. Bytes should be appended to this buffer.
2137 * @param auth the auth conversation
2138 * @param buffer return location for buffer to append bytes to
2141 _dbus_auth_get_buffer (DBusAuth *auth,
2142 DBusString **buffer)
2144 _dbus_assert (auth != NULL);
2145 _dbus_assert (!auth->buffer_outstanding);
2147 *buffer = &auth->incoming;
2149 auth->buffer_outstanding = TRUE;
2153 * Returns a buffer with new data read into it.
2155 * @param auth the auth conversation
2156 * @param buffer the buffer being returned
2157 * @param bytes_read number of new bytes added
2160 _dbus_auth_return_buffer (DBusAuth *auth,
2164 _dbus_assert (buffer == &auth->incoming);
2165 _dbus_assert (auth->buffer_outstanding);
2167 auth->buffer_outstanding = FALSE;
2171 * Returns leftover bytes that were not used as part of the auth
2172 * conversation. These bytes will be part of the message stream
2173 * instead. This function may not be called until authentication has
2176 * @param auth the auth conversation
2177 * @param str return location for pointer to string of unused bytes
2180 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2181 const DBusString **str)
2183 if (!DBUS_AUTH_IN_END_STATE (auth))
2186 *str = &auth->incoming;
2191 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2192 * after we've gotten them and successfully moved them elsewhere.
2194 * @param auth the auth conversation
2197 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2199 if (!DBUS_AUTH_IN_END_STATE (auth))
2202 _dbus_string_set_length (&auth->incoming, 0);
2206 * Called post-authentication, indicates whether we need to encode
2207 * the message stream with _dbus_auth_encode_data() prior to
2208 * sending it to the peer.
2210 * @param auth the auth conversation
2211 * @returns #TRUE if we need to encode the stream
2214 _dbus_auth_needs_encoding (DBusAuth *auth)
2216 if (!auth->authenticated)
2219 if (auth->mech != NULL)
2221 if (DBUS_AUTH_IS_CLIENT (auth))
2222 return auth->mech->client_encode_func != NULL;
2224 return auth->mech->server_encode_func != NULL;
2231 * Called post-authentication, encodes a block of bytes for sending to
2232 * the peer. If no encoding was negotiated, just copies the bytes
2233 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2235 * @param auth the auth conversation
2236 * @param plaintext the plain text data
2237 * @param encoded initialized string to where encoded data is appended
2238 * @returns #TRUE if we had enough memory and successfully encoded
2241 _dbus_auth_encode_data (DBusAuth *auth,
2242 const DBusString *plaintext,
2243 DBusString *encoded)
2245 _dbus_assert (plaintext != encoded);
2247 if (!auth->authenticated)
2250 if (_dbus_auth_needs_encoding (auth))
2252 if (DBUS_AUTH_IS_CLIENT (auth))
2253 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2255 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2259 return _dbus_string_copy (plaintext, 0, encoded,
2260 _dbus_string_get_length (encoded));
2265 * Called post-authentication, indicates whether we need to decode
2266 * the message stream with _dbus_auth_decode_data() after
2267 * receiving it from the peer.
2269 * @param auth the auth conversation
2270 * @returns #TRUE if we need to encode the stream
2273 _dbus_auth_needs_decoding (DBusAuth *auth)
2275 if (!auth->authenticated)
2278 if (auth->mech != NULL)
2280 if (DBUS_AUTH_IS_CLIENT (auth))
2281 return auth->mech->client_decode_func != NULL;
2283 return auth->mech->server_decode_func != NULL;
2291 * Called post-authentication, decodes a block of bytes received from
2292 * the peer. If no encoding was negotiated, just copies the bytes (you
2293 * can avoid this by checking _dbus_auth_needs_decoding()).
2295 * @todo We need to be able to distinguish "out of memory" error
2296 * from "the data is hosed" error.
2298 * @param auth the auth conversation
2299 * @param encoded the encoded data
2300 * @param plaintext initialized string where decoded data is appended
2301 * @returns #TRUE if we had enough memory and successfully decoded
2304 _dbus_auth_decode_data (DBusAuth *auth,
2305 const DBusString *encoded,
2306 DBusString *plaintext)
2308 _dbus_assert (plaintext != encoded);
2310 if (!auth->authenticated)
2313 if (_dbus_auth_needs_decoding (auth))
2315 if (DBUS_AUTH_IS_CLIENT (auth))
2316 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2318 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2322 return _dbus_string_copy (encoded, 0, plaintext,
2323 _dbus_string_get_length (plaintext));
2328 * Sets credentials received via reliable means from the operating
2331 * @param auth the auth conversation
2332 * @param credentials the credentials received
2335 _dbus_auth_set_credentials (DBusAuth *auth,
2336 const DBusCredentials *credentials)
2338 auth->credentials = *credentials;
2342 * Gets the identity we authorized the client as. Apps may have
2343 * different policies as to what identities they allow.
2345 * @param auth the auth conversation
2346 * @param credentials the credentials we've authorized
2349 _dbus_auth_get_identity (DBusAuth *auth,
2350 DBusCredentials *credentials)
2352 if (auth->authenticated)
2353 *credentials = auth->authorized_identity;
2355 _dbus_credentials_clear (credentials);
2359 * Sets the "authentication context" which scopes cookies
2360 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2362 * @param auth the auth conversation
2363 * @param context the context
2364 * @returns #FALSE if no memory
2367 _dbus_auth_set_context (DBusAuth *auth,
2368 const DBusString *context)
2370 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2371 &auth->context, 0, _dbus_string_get_length (context));
2376 #ifdef DBUS_BUILD_TESTS
2377 #include "dbus-test.h"
2378 #include "dbus-auth-script.h"
2382 process_test_subdir (const DBusString *test_base_dir,
2385 DBusString test_directory;
2386 DBusString filename;
2394 if (!_dbus_string_init (&test_directory))
2395 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2397 _dbus_string_init_const (&filename, subdir);
2399 if (!_dbus_string_copy (test_base_dir, 0,
2400 &test_directory, 0))
2401 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2403 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2404 _dbus_assert_not_reached ("couldn't allocate full path");
2406 _dbus_string_free (&filename);
2407 if (!_dbus_string_init (&filename))
2408 _dbus_assert_not_reached ("didn't allocate filename string\n");
2410 dbus_error_init (&error);
2411 dir = _dbus_directory_open (&test_directory, &error);
2414 _dbus_warn ("Could not open %s: %s\n",
2415 _dbus_string_get_const_data (&test_directory),
2417 dbus_error_free (&error);
2421 printf ("Testing %s:\n", subdir);
2424 while (_dbus_directory_get_next_file (dir, &filename, &error))
2426 DBusString full_path;
2428 if (!_dbus_string_init (&full_path))
2429 _dbus_assert_not_reached ("couldn't init string");
2431 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2432 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2434 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2435 _dbus_assert_not_reached ("couldn't concat file to dir");
2437 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2439 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2440 _dbus_string_get_const_data (&filename));
2441 _dbus_string_free (&full_path);
2445 printf (" %s\n", _dbus_string_get_const_data (&filename));
2447 if (!_dbus_auth_script_run (&full_path))
2449 _dbus_string_free (&full_path);
2453 _dbus_string_free (&full_path);
2456 if (dbus_error_is_set (&error))
2458 _dbus_warn ("Could not get next file in %s: %s\n",
2459 _dbus_string_get_const_data (&test_directory), error.message);
2460 dbus_error_free (&error);
2469 _dbus_directory_close (dir);
2470 _dbus_string_free (&test_directory);
2471 _dbus_string_free (&filename);
2477 process_test_dirs (const char *test_data_dir)
2479 DBusString test_directory;
2484 _dbus_string_init_const (&test_directory, test_data_dir);
2486 if (!process_test_subdir (&test_directory, "auth"))
2493 _dbus_string_free (&test_directory);
2499 _dbus_auth_test (const char *test_data_dir)
2502 if (test_data_dir == NULL)
2505 if (!process_test_dirs (test_data_dir))
2511 #endif /* DBUS_BUILD_TESTS */