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"
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_base64_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_base64_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_base64_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_base64_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 base64_response;
1254 DBusString decoded_response;
1256 _dbus_string_find_blank (args, 0, &i);
1258 if (!_dbus_string_init (&mech))
1261 if (!_dbus_string_init (&base64_response))
1263 _dbus_string_free (&mech);
1267 if (!_dbus_string_init (&decoded_response))
1269 _dbus_string_free (&mech);
1270 _dbus_string_free (&base64_response);
1274 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1277 if (!_dbus_string_copy (args, i, &base64_response, 0))
1280 if (!_dbus_string_base64_decode (&base64_response, 0,
1281 &decoded_response, 0))
1284 auth->mech = find_mech (&mech, auth->allowed_mechs);
1285 if (auth->mech != NULL)
1287 _dbus_verbose ("%s: Trying mechanism %s with initial response of %d bytes\n",
1288 DBUS_AUTH_NAME (auth),
1289 auth->mech->mechanism,
1290 _dbus_string_get_length (&decoded_response));
1292 if (!(* auth->mech->server_data_func) (auth,
1298 /* Unsupported mechanism */
1299 if (!send_rejected (auth))
1303 _dbus_string_free (&mech);
1304 _dbus_string_free (&base64_response);
1305 _dbus_string_free (&decoded_response);
1311 _dbus_string_free (&mech);
1312 _dbus_string_free (&base64_response);
1313 _dbus_string_free (&decoded_response);
1319 process_cancel (DBusAuth *auth,
1320 const DBusString *command,
1321 const DBusString *args)
1323 if (!send_rejected (auth))
1330 process_begin (DBusAuth *auth,
1331 const DBusString *command,
1332 const DBusString *args)
1334 if (auth->authenticated_pending_begin)
1335 auth->authenticated = TRUE;
1338 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1341 shutdown_mech (auth);
1348 process_data_server (DBusAuth *auth,
1349 const DBusString *command,
1350 const DBusString *args)
1352 if (auth->mech != NULL)
1356 if (!_dbus_string_init (&decoded))
1359 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1361 _dbus_string_free (&decoded);
1365 #ifdef DBUS_ENABLE_VERBOSE_MODE
1366 if (_dbus_string_validate_ascii (&decoded, 0,
1367 _dbus_string_get_length (&decoded)))
1368 _dbus_verbose ("%s: data: '%s'\n",
1369 DBUS_AUTH_NAME (auth),
1370 _dbus_string_get_const_data (&decoded));
1373 if (!(* auth->mech->server_data_func) (auth, &decoded))
1375 _dbus_string_free (&decoded);
1379 _dbus_string_free (&decoded);
1383 if (!_dbus_string_append (&auth->outgoing,
1384 "ERROR \"Not currently in an auth conversation\"\r\n"))
1392 process_error_server (DBusAuth *auth,
1393 const DBusString *command,
1394 const DBusString *args)
1396 /* Server got error from client, reject the auth,
1397 * as we don't have anything more intelligent to do.
1399 if (!send_rejected (auth))
1405 /* return FALSE if no memory, TRUE if all OK */
1407 get_word (const DBusString *str,
1413 _dbus_string_skip_blank (str, *start, start);
1414 _dbus_string_find_blank (str, *start, &i);
1418 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1428 record_mechanisms (DBusAuth *auth,
1429 const DBusString *command,
1430 const DBusString *args)
1435 if (auth->already_got_mechanisms)
1438 len = _dbus_string_get_length (args);
1444 const DBusAuthMechanismHandler *mech;
1446 if (!_dbus_string_init (&m))
1449 if (!get_word (args, &next, &m))
1451 _dbus_string_free (&m);
1455 mech = find_mech (&m, auth->allowed_mechs);
1459 /* FIXME right now we try mechanisms in the order
1460 * the server lists them; should we do them in
1461 * some more deterministic order?
1463 * Probably in all_mechanisms order, our order of
1464 * preference. Of course when the server is us,
1465 * it lists things in that order anyhow.
1468 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1469 DBUS_AUTH_NAME (auth), mech->mechanism);
1471 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1474 _dbus_string_free (&m);
1480 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1481 DBUS_AUTH_NAME (auth),
1482 _dbus_string_get_const_data (&m));
1485 _dbus_string_free (&m);
1488 auth->already_got_mechanisms = TRUE;
1493 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1499 client_try_next_mechanism (DBusAuth *auth)
1501 const DBusAuthMechanismHandler *mech;
1502 DBusString auth_command;
1503 DBusAuthClient *client;
1505 client = DBUS_AUTH_CLIENT (auth);
1507 /* Pop any mechs not in the list of allowed mechanisms */
1509 while (client->mechs_to_try != NULL)
1511 mech = client->mechs_to_try->data;
1513 if (auth->allowed_mechs != NULL &&
1514 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1517 /* don't try this one after all */
1518 _dbus_verbose ("%s: Mechanism %s isn't in the list of allowed mechanisms\n",
1519 DBUS_AUTH_NAME (auth), mech->mechanism);
1521 _dbus_list_pop_first (& client->mechs_to_try);
1524 break; /* we'll try this one */
1530 if (!_dbus_string_init (&auth_command))
1533 if (!_dbus_string_append (&auth_command,
1536 _dbus_string_free (&auth_command);
1540 if (!_dbus_string_append (&auth_command,
1543 _dbus_string_free (&auth_command);
1547 if (mech->client_initial_response_func != NULL)
1549 if (!_dbus_string_append (&auth_command, " "))
1551 _dbus_string_free (&auth_command);
1555 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1557 _dbus_string_free (&auth_command);
1562 if (!_dbus_string_append (&auth_command,
1565 _dbus_string_free (&auth_command);
1569 if (!_dbus_string_copy (&auth_command, 0,
1571 _dbus_string_get_length (&auth->outgoing)))
1573 _dbus_string_free (&auth_command);
1578 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1580 _dbus_verbose ("%s: Trying mechanism %s\n",
1581 DBUS_AUTH_NAME (auth),
1582 auth->mech->mechanism);
1584 _dbus_string_free (&auth_command);
1590 process_rejected (DBusAuth *auth,
1591 const DBusString *command,
1592 const DBusString *args)
1594 shutdown_mech (auth);
1596 if (!auth->already_got_mechanisms)
1598 if (!record_mechanisms (auth, command, args))
1602 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1604 if (!client_try_next_mechanism (auth))
1610 auth->need_disconnect = TRUE;
1617 process_ok (DBusAuth *auth,
1618 const DBusString *command,
1619 const DBusString *args)
1621 if (!_dbus_string_append (&auth->outgoing,
1625 auth->authenticated_pending_output = TRUE;
1631 process_data_client (DBusAuth *auth,
1632 const DBusString *command,
1633 const DBusString *args)
1635 if (auth->mech != NULL)
1639 if (!_dbus_string_init (&decoded))
1642 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1644 _dbus_string_free (&decoded);
1648 #ifdef DBUS_ENABLE_VERBOSE_MODE
1649 if (_dbus_string_validate_ascii (&decoded, 0,
1650 _dbus_string_get_length (&decoded)))
1652 _dbus_verbose ("%s: data: '%s'\n",
1653 DBUS_AUTH_NAME (auth),
1654 _dbus_string_get_const_data (&decoded));
1658 if (!(* auth->mech->client_data_func) (auth, &decoded))
1660 _dbus_string_free (&decoded);
1664 _dbus_string_free (&decoded);
1668 if (!_dbus_string_append (&auth->outgoing,
1669 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1677 process_error_client (DBusAuth *auth,
1678 const DBusString *command,
1679 const DBusString *args)
1681 /* Cancel current mechanism, as we don't have anything
1682 * more clever to do.
1684 if (!_dbus_string_append (&auth->outgoing,
1692 process_unknown (DBusAuth *auth,
1693 const DBusString *command,
1694 const DBusString *args)
1696 if (!_dbus_string_append (&auth->outgoing,
1697 "ERROR \"Unknown command\"\r\n"))
1703 /* returns whether to call it again right away */
1705 process_command (DBusAuth *auth)
1713 /* _dbus_verbose ("%s: trying process_command()\n"); */
1718 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1721 if (!_dbus_string_init (&command))
1723 auth->needed_memory = TRUE;
1727 if (!_dbus_string_init (&args))
1729 _dbus_string_free (&command);
1730 auth->needed_memory = TRUE;
1734 if (eol > _DBUS_ONE_MEGABYTE)
1736 /* This is a giant line, someone is trying to hose us. */
1737 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1743 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1746 if (!_dbus_string_validate_ascii (&command, 0,
1747 _dbus_string_get_length (&command)))
1749 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1750 DBUS_AUTH_NAME (auth));
1751 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1757 _dbus_verbose ("%s: got command \"%s\"\n",
1758 DBUS_AUTH_NAME (auth),
1759 _dbus_string_get_const_data (&command));
1761 _dbus_string_find_blank (&command, 0, &i);
1762 _dbus_string_skip_blank (&command, i, &j);
1765 _dbus_string_delete (&command, i, j - i);
1767 if (!_dbus_string_move (&command, i, &args, 0))
1771 while (auth->handlers[i].command != NULL)
1773 if (_dbus_string_equal_c_str (&command,
1774 auth->handlers[i].command))
1776 _dbus_verbose ("%s: Processing auth command %s\n",
1777 DBUS_AUTH_NAME (auth),
1778 auth->handlers[i].command);
1780 if (!(* auth->handlers[i].func) (auth, &command, &args))
1788 if (auth->handlers[i].command == NULL)
1790 if (!process_unknown (auth, &command, &args))
1796 /* We've succeeded in processing the whole command so drop it out
1797 * of the incoming buffer and return TRUE to try another command.
1800 _dbus_string_delete (&auth->incoming, 0, eol);
1803 _dbus_string_delete (&auth->incoming, 0, 2);
1808 _dbus_string_free (&args);
1809 _dbus_string_free (&command);
1812 auth->needed_memory = TRUE;
1814 auth->needed_memory = FALSE;
1823 * @addtogroup DBusAuth
1828 * Creates a new auth conversation object for the server side.
1829 * See doc/dbus-sasl-profile.txt for full details on what
1832 * @returns the new object or #NULL if no memory
1835 _dbus_auth_server_new (void)
1838 DBusAuthServer *server_auth;
1840 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1844 auth->handlers = server_handlers;
1846 server_auth = DBUS_AUTH_SERVER (auth);
1848 /* perhaps this should be per-mechanism with a lower
1851 server_auth->failures = 0;
1852 server_auth->max_failures = 6;
1858 * Creates a new auth conversation object for the client side.
1859 * See doc/dbus-sasl-profile.txt for full details on what
1862 * @returns the new object or #NULL if no memory
1865 _dbus_auth_client_new (void)
1869 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1873 auth->handlers = client_handlers;
1875 /* Add a default mechanism to try */
1876 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1877 (void*) &all_mechanisms[0]))
1879 _dbus_auth_unref (auth);
1883 /* Now try the mechanism we just added */
1884 if (!client_try_next_mechanism (auth))
1886 _dbus_auth_unref (auth);
1894 * Increments the refcount of an auth object.
1896 * @param auth the auth conversation
1897 * @returns the auth conversation
1900 _dbus_auth_ref (DBusAuth *auth)
1902 _dbus_assert (auth != NULL);
1904 auth->refcount += 1;
1910 * Decrements the refcount of an auth object.
1912 * @param auth the auth conversation
1915 _dbus_auth_unref (DBusAuth *auth)
1917 _dbus_assert (auth != NULL);
1918 _dbus_assert (auth->refcount > 0);
1920 auth->refcount -= 1;
1921 if (auth->refcount == 0)
1923 shutdown_mech (auth);
1925 if (DBUS_AUTH_IS_CLIENT (auth))
1927 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1931 _dbus_keyring_unref (auth->keyring);
1933 _dbus_string_free (&auth->context);
1934 _dbus_string_free (&auth->challenge);
1935 _dbus_string_free (&auth->identity);
1936 _dbus_string_free (&auth->incoming);
1937 _dbus_string_free (&auth->outgoing);
1939 dbus_free_string_array (auth->allowed_mechs);
1946 * Sets an array of authentication mechanism names
1947 * that we are willing to use.
1949 * @param auth the auth conversation
1950 * @param mechanisms #NULL-terminated array of mechanism names
1951 * @returns #FALSE if no memory
1954 _dbus_auth_set_mechanisms (DBusAuth *auth,
1955 const char **mechanisms)
1959 if (mechanisms != NULL)
1961 copy = _dbus_dup_string_array (mechanisms);
1968 dbus_free_string_array (auth->allowed_mechs);
1970 auth->allowed_mechs = copy;
1976 * @param auth the auth conversation object
1977 * @returns #TRUE if we're in a final state
1979 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1982 * Analyzes buffered input and moves the auth conversation forward,
1983 * returning the new state of the auth conversation.
1985 * @param auth the auth conversation
1986 * @returns the new state
1989 _dbus_auth_do_work (DBusAuth *auth)
1991 auth->needed_memory = FALSE;
1993 /* Max amount we'll buffer up before deciding someone's on crack */
1994 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1998 if (DBUS_AUTH_IN_END_STATE (auth))
2001 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2002 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2004 auth->need_disconnect = TRUE;
2005 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2006 DBUS_AUTH_NAME (auth));
2010 if (auth->mech == NULL &&
2011 auth->already_got_mechanisms &&
2012 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
2014 auth->need_disconnect = TRUE;
2015 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2016 DBUS_AUTH_NAME (auth));
2020 while (process_command (auth));
2022 if (DBUS_AUTH_IS_SERVER (auth) &&
2023 DBUS_AUTH_SERVER (auth)->failures >=
2024 DBUS_AUTH_SERVER (auth)->max_failures)
2025 auth->need_disconnect = TRUE;
2027 if (auth->need_disconnect)
2028 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2029 else if (auth->authenticated)
2031 if (_dbus_string_get_length (&auth->incoming) > 0)
2032 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
2034 return DBUS_AUTH_STATE_AUTHENTICATED;
2036 else if (auth->needed_memory)
2037 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2038 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2039 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2041 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2045 * Gets bytes that need to be sent to the peer we're conversing with.
2046 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2047 * to notify the auth object that they were written.
2049 * @param auth the auth conversation
2050 * @param str return location for a ref to the buffer to send
2051 * @returns #FALSE if nothing to send
2054 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2055 const DBusString **str)
2057 _dbus_assert (auth != NULL);
2058 _dbus_assert (str != NULL);
2062 if (DBUS_AUTH_IN_END_STATE (auth))
2065 if (_dbus_string_get_length (&auth->outgoing) == 0)
2068 *str = &auth->outgoing;
2074 * Notifies the auth conversation object that
2075 * the given number of bytes of the outgoing buffer
2076 * have been written out.
2078 * @param auth the auth conversation
2079 * @param bytes_sent number of bytes written out
2082 _dbus_auth_bytes_sent (DBusAuth *auth,
2085 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2086 DBUS_AUTH_NAME (auth),
2088 _dbus_string_get_const_data (&auth->outgoing));
2090 _dbus_string_delete (&auth->outgoing,
2093 if (auth->authenticated_pending_output &&
2094 _dbus_string_get_length (&auth->outgoing) == 0)
2095 auth->authenticated = TRUE;
2099 * Get a buffer to be used for reading bytes from the peer we're conversing
2100 * with. Bytes should be appended to this buffer.
2102 * @param auth the auth conversation
2103 * @param buffer return location for buffer to append bytes to
2106 _dbus_auth_get_buffer (DBusAuth *auth,
2107 DBusString **buffer)
2109 _dbus_assert (auth != NULL);
2110 _dbus_assert (!auth->buffer_outstanding);
2112 *buffer = &auth->incoming;
2114 auth->buffer_outstanding = TRUE;
2118 * Returns a buffer with new data read into it.
2120 * @param auth the auth conversation
2121 * @param buffer the buffer being returned
2122 * @param bytes_read number of new bytes added
2125 _dbus_auth_return_buffer (DBusAuth *auth,
2129 _dbus_assert (buffer == &auth->incoming);
2130 _dbus_assert (auth->buffer_outstanding);
2132 auth->buffer_outstanding = FALSE;
2136 * Returns leftover bytes that were not used as part of the auth
2137 * conversation. These bytes will be part of the message stream
2138 * instead. This function may not be called until authentication has
2141 * @param auth the auth conversation
2142 * @param str return location for pointer to string of unused bytes
2145 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2146 const DBusString **str)
2148 if (!DBUS_AUTH_IN_END_STATE (auth))
2151 *str = &auth->incoming;
2156 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2157 * after we've gotten them and successfully moved them elsewhere.
2159 * @param auth the auth conversation
2162 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2164 if (!DBUS_AUTH_IN_END_STATE (auth))
2167 _dbus_string_set_length (&auth->incoming, 0);
2171 * Called post-authentication, indicates whether we need to encode
2172 * the message stream with _dbus_auth_encode_data() prior to
2173 * sending it to the peer.
2175 * @param auth the auth conversation
2176 * @returns #TRUE if we need to encode the stream
2179 _dbus_auth_needs_encoding (DBusAuth *auth)
2181 if (!auth->authenticated)
2184 if (auth->mech != NULL)
2186 if (DBUS_AUTH_IS_CLIENT (auth))
2187 return auth->mech->client_encode_func != NULL;
2189 return auth->mech->server_encode_func != NULL;
2196 * Called post-authentication, encodes a block of bytes for sending to
2197 * the peer. If no encoding was negotiated, just copies the bytes
2198 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2200 * @param auth the auth conversation
2201 * @param plaintext the plain text data
2202 * @param encoded initialized string to where encoded data is appended
2203 * @returns #TRUE if we had enough memory and successfully encoded
2206 _dbus_auth_encode_data (DBusAuth *auth,
2207 const DBusString *plaintext,
2208 DBusString *encoded)
2210 _dbus_assert (plaintext != encoded);
2212 if (!auth->authenticated)
2215 if (_dbus_auth_needs_encoding (auth))
2217 if (DBUS_AUTH_IS_CLIENT (auth))
2218 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2220 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2224 return _dbus_string_copy (plaintext, 0, encoded,
2225 _dbus_string_get_length (encoded));
2230 * Called post-authentication, indicates whether we need to decode
2231 * the message stream with _dbus_auth_decode_data() after
2232 * receiving it from the peer.
2234 * @param auth the auth conversation
2235 * @returns #TRUE if we need to encode the stream
2238 _dbus_auth_needs_decoding (DBusAuth *auth)
2240 if (!auth->authenticated)
2243 if (auth->mech != NULL)
2245 if (DBUS_AUTH_IS_CLIENT (auth))
2246 return auth->mech->client_decode_func != NULL;
2248 return auth->mech->server_decode_func != NULL;
2256 * Called post-authentication, decodes a block of bytes received from
2257 * the peer. If no encoding was negotiated, just copies the bytes (you
2258 * can avoid this by checking _dbus_auth_needs_decoding()).
2260 * @todo We need to be able to distinguish "out of memory" error
2261 * from "the data is hosed" error.
2263 * @param auth the auth conversation
2264 * @param encoded the encoded data
2265 * @param plaintext initialized string where decoded data is appended
2266 * @returns #TRUE if we had enough memory and successfully decoded
2269 _dbus_auth_decode_data (DBusAuth *auth,
2270 const DBusString *encoded,
2271 DBusString *plaintext)
2273 _dbus_assert (plaintext != encoded);
2275 if (!auth->authenticated)
2278 if (_dbus_auth_needs_decoding (auth))
2280 if (DBUS_AUTH_IS_CLIENT (auth))
2281 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2283 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2287 return _dbus_string_copy (encoded, 0, plaintext,
2288 _dbus_string_get_length (plaintext));
2293 * Sets credentials received via reliable means from the operating
2296 * @param auth the auth conversation
2297 * @param credentials the credentials received
2300 _dbus_auth_set_credentials (DBusAuth *auth,
2301 const DBusCredentials *credentials)
2303 auth->credentials = *credentials;
2307 * Gets the identity we authorized the client as. Apps may have
2308 * different policies as to what identities they allow.
2310 * @param auth the auth conversation
2311 * @param credentials the credentials we've authorized
2314 _dbus_auth_get_identity (DBusAuth *auth,
2315 DBusCredentials *credentials)
2317 if (auth->authenticated)
2318 *credentials = auth->authorized_identity;
2320 _dbus_credentials_clear (credentials);
2324 * Sets the "authentication context" which scopes cookies
2325 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2327 * @param auth the auth conversation
2328 * @param context the context
2329 * @returns #FALSE if no memory
2332 _dbus_auth_set_context (DBusAuth *auth,
2333 const DBusString *context)
2335 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2336 &auth->context, 0, _dbus_string_get_length (context));
2341 #ifdef DBUS_BUILD_TESTS
2342 #include "dbus-test.h"
2343 #include "dbus-auth-script.h"
2347 process_test_subdir (const DBusString *test_base_dir,
2350 DBusString test_directory;
2351 DBusString filename;
2359 if (!_dbus_string_init (&test_directory))
2360 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2362 _dbus_string_init_const (&filename, subdir);
2364 if (!_dbus_string_copy (test_base_dir, 0,
2365 &test_directory, 0))
2366 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2368 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2369 _dbus_assert_not_reached ("couldn't allocate full path");
2371 _dbus_string_free (&filename);
2372 if (!_dbus_string_init (&filename))
2373 _dbus_assert_not_reached ("didn't allocate filename string\n");
2375 dbus_error_init (&error);
2376 dir = _dbus_directory_open (&test_directory, &error);
2379 _dbus_warn ("Could not open %s: %s\n",
2380 _dbus_string_get_const_data (&test_directory),
2382 dbus_error_free (&error);
2386 printf ("Testing %s:\n", subdir);
2389 while (_dbus_directory_get_next_file (dir, &filename, &error))
2391 DBusString full_path;
2393 if (!_dbus_string_init (&full_path))
2394 _dbus_assert_not_reached ("couldn't init string");
2396 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2397 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2399 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2400 _dbus_assert_not_reached ("couldn't concat file to dir");
2402 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2404 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2405 _dbus_string_get_const_data (&filename));
2406 _dbus_string_free (&full_path);
2410 printf (" %s\n", _dbus_string_get_const_data (&filename));
2412 if (!_dbus_auth_script_run (&full_path))
2414 _dbus_string_free (&full_path);
2418 _dbus_string_free (&full_path);
2421 if (dbus_error_is_set (&error))
2423 _dbus_warn ("Could not get next file in %s: %s\n",
2424 _dbus_string_get_const_data (&test_directory), error.message);
2425 dbus_error_free (&error);
2434 _dbus_directory_close (dir);
2435 _dbus_string_free (&test_directory);
2436 _dbus_string_free (&filename);
2442 process_test_dirs (const char *test_data_dir)
2444 DBusString test_directory;
2449 _dbus_string_init_const (&test_directory, test_data_dir);
2451 if (!process_test_subdir (&test_directory, "auth"))
2458 _dbus_string_free (&test_directory);
2464 _dbus_auth_test (const char *test_data_dir)
2467 if (test_data_dir == NULL)
2470 if (!process_test_dirs (test_data_dir))
2476 #endif /* DBUS_BUILD_TESTS */