1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-auth.h"
24 #include "dbus-string.h"
25 #include "dbus-list.h"
26 #include "dbus-internals.h"
27 #include "dbus-keyring.h"
30 /* See doc/dbus-sasl-profile.txt */
33 * @defgroup DBusAuth Authentication
34 * @ingroup DBusInternals
35 * @brief DBusAuth object
37 * DBusAuth manages the authentication negotiation when a connection
38 * is first established, and also manage any encryption used over a
41 * The file doc/dbus-sasl-profile.txt documents the network protocol
42 * used for authentication.
44 * @todo some SASL profiles require sending the empty string as a
45 * challenge/response, but we don't currently allow that in our
48 * @todo DBusAuth really needs to be rewritten as an explicit state
49 * machine. Right now it's too hard to prove to yourself by inspection
52 * @todo right now sometimes both ends will block waiting for input
53 * from the other end, e.g. if there's an error during
56 * @todo the cookie keyring needs to be cached globally not just
57 * per-auth (which raises threadsafety issues too)
59 * @todo grep FIXME in dbus-auth.c
63 * @defgroup DBusAuthInternals Authentication implementation details
64 * @ingroup DBusInternals
65 * @brief DBusAuth implementation details
67 * Private details of authentication code.
73 * Processes a command. Returns whether we had enough memory to
74 * complete the operation.
76 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
77 const DBusString *command,
78 const DBusString *args);
83 DBusProcessAuthCommandFunction func;
84 } DBusAuthCommandHandler;
87 * This function appends an initial client response to the given string
89 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
90 DBusString *response);
93 * This function processes a block of data received from the peer.
94 * i.e. handles a DATA command.
96 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
97 const DBusString *data);
100 * This function encodes a block of data from the peer.
102 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
103 const DBusString *data,
104 DBusString *encoded);
107 * This function decodes a block of data from the peer.
109 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
110 const DBusString *data,
111 DBusString *decoded);
114 * This function is called when the mechanism is abandoned.
116 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
120 const char *mechanism;
121 DBusAuthDataFunction server_data_func;
122 DBusAuthEncodeFunction server_encode_func;
123 DBusAuthDecodeFunction server_decode_func;
124 DBusAuthShutdownFunction server_shutdown_func;
125 DBusInitialResponseFunction client_initial_response_func;
126 DBusAuthDataFunction client_data_func;
127 DBusAuthEncodeFunction client_encode_func;
128 DBusAuthDecodeFunction client_decode_func;
129 DBusAuthShutdownFunction client_shutdown_func;
130 } DBusAuthMechanismHandler;
133 * Internal members of DBusAuth.
137 int refcount; /**< reference count */
139 DBusString incoming; /**< Incoming data buffer */
140 DBusString outgoing; /**< Outgoing data buffer */
142 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
144 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
146 DBusString identity; /**< Current identity we're authorizing
150 DBusCredentials credentials; /**< Credentials read from socket,
154 DBusCredentials authorized_identity; /**< Credentials that are authorized */
156 DBusCredentials desired_identity; /**< Identity client has requested */
158 DBusString context; /**< Cookie scope */
159 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
160 int cookie_id; /**< ID of cookie to use */
161 DBusString challenge; /**< Challenge sent to client */
163 char **allowed_mechs; /**< Mechanisms we're allowed to use,
164 * or #NULL if we can use any
167 unsigned int needed_memory : 1; /**< We needed memory to continue since last
168 * successful getting something done
170 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
171 unsigned int authenticated : 1; /**< We are authenticated */
172 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
173 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
174 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
175 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
176 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
183 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
191 int failures; /**< Number of times client has been rejected */
192 int max_failures; /**< Number of times we reject before disconnect */
196 static dbus_bool_t process_auth (DBusAuth *auth,
197 const DBusString *command,
198 const DBusString *args);
199 static dbus_bool_t process_cancel (DBusAuth *auth,
200 const DBusString *command,
201 const DBusString *args);
202 static dbus_bool_t process_begin (DBusAuth *auth,
203 const DBusString *command,
204 const DBusString *args);
205 static dbus_bool_t process_data_server (DBusAuth *auth,
206 const DBusString *command,
207 const DBusString *args);
208 static dbus_bool_t process_error_server (DBusAuth *auth,
209 const DBusString *command,
210 const DBusString *args);
211 static dbus_bool_t process_rejected (DBusAuth *auth,
212 const DBusString *command,
213 const DBusString *args);
214 static dbus_bool_t process_ok (DBusAuth *auth,
215 const DBusString *command,
216 const DBusString *args);
217 static dbus_bool_t process_data_client (DBusAuth *auth,
218 const DBusString *command,
219 const DBusString *args);
220 static dbus_bool_t process_error_client (DBusAuth *auth,
221 const DBusString *command,
222 const DBusString *args);
225 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
226 static dbus_bool_t send_rejected (DBusAuth *auth);
228 static DBusAuthCommandHandler
229 server_handlers[] = {
230 { "AUTH", process_auth },
231 { "CANCEL", process_cancel },
232 { "BEGIN", process_begin },
233 { "DATA", process_data_server },
234 { "ERROR", process_error_server },
238 static DBusAuthCommandHandler
239 client_handlers[] = {
240 { "REJECTED", process_rejected },
241 { "OK", process_ok },
242 { "DATA", process_data_client },
243 { "ERROR", process_error_client },
248 * @param auth the auth conversation
249 * @returns #TRUE if the conversation is the server side
251 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
253 * @param auth the auth conversation
254 * @returns #TRUE if the conversation is the client side
256 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
258 * @param auth the auth conversation
259 * @returns auth cast to DBusAuthClient
261 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
263 * @param auth the auth conversation
264 * @returns auth cast to DBusAuthServer
266 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
269 _dbus_auth_new (int size)
273 auth = dbus_malloc0 (size);
279 auth->credentials.pid = -1;
280 auth->credentials.uid = -1;
281 auth->credentials.gid = -1;
283 auth->authorized_identity.pid = -1;
284 auth->authorized_identity.uid = -1;
285 auth->authorized_identity.gid = -1;
287 auth->desired_identity.pid = -1;
288 auth->desired_identity.uid = -1;
289 auth->desired_identity.gid = -1;
291 auth->keyring = NULL;
292 auth->cookie_id = -1;
294 /* note that we don't use the max string length feature,
295 * because you can't use that feature if you're going to
296 * try to recover from out-of-memory (it creates
297 * what looks like unrecoverable inability to alloc
298 * more space in the string). But we do handle
299 * overlong buffers in _dbus_auth_do_work().
302 if (!_dbus_string_init (&auth->incoming))
305 if (!_dbus_string_init (&auth->outgoing))
308 if (!_dbus_string_init (&auth->identity))
311 if (!_dbus_string_init (&auth->context))
314 if (!_dbus_string_init (&auth->challenge))
317 /* default context if none is specified */
318 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
324 _dbus_string_free (&auth->challenge);
326 _dbus_string_free (&auth->context);
328 _dbus_string_free (&auth->identity);
330 _dbus_string_free (&auth->outgoing);
332 _dbus_string_free (&auth->incoming);
339 shutdown_mech (DBusAuth *auth)
341 /* Cancel any auth */
342 auth->authenticated_pending_begin = FALSE;
343 auth->authenticated = FALSE;
344 auth->already_asked_for_initial_response = FALSE;
345 _dbus_string_set_length (&auth->identity, 0);
347 auth->authorized_identity.pid = -1;
348 auth->authorized_identity.uid = -1;
349 auth->authorized_identity.gid = -1;
351 auth->desired_identity.pid = -1;
352 auth->desired_identity.uid = -1;
353 auth->desired_identity.gid = -1;
355 if (auth->mech != NULL)
357 _dbus_verbose ("Shutting down mechanism %s\n",
358 auth->mech->mechanism);
360 if (DBUS_AUTH_IS_CLIENT (auth))
361 (* auth->mech->client_shutdown_func) (auth);
363 (* auth->mech->server_shutdown_func) (auth);
369 /* Returns TRUE but with an empty string hash if the
370 * cookie_id isn't known. As with all this code
371 * TRUE just means we had enough memory.
374 sha1_compute_hash (DBusAuth *auth,
376 const DBusString *server_challenge,
377 const DBusString *client_challenge,
384 _dbus_assert (auth->keyring != NULL);
388 if (!_dbus_string_init (&cookie))
391 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
395 if (_dbus_string_get_length (&cookie) == 0)
401 if (!_dbus_string_init (&to_hash))
404 if (!_dbus_string_copy (server_challenge, 0,
405 &to_hash, _dbus_string_get_length (&to_hash)))
408 if (!_dbus_string_append (&to_hash, ":"))
411 if (!_dbus_string_copy (client_challenge, 0,
412 &to_hash, _dbus_string_get_length (&to_hash)))
415 if (!_dbus_string_append (&to_hash, ":"))
418 if (!_dbus_string_copy (&cookie, 0,
419 &to_hash, _dbus_string_get_length (&to_hash)))
422 if (!_dbus_sha_compute (&to_hash, hash))
428 _dbus_string_zero (&to_hash);
429 _dbus_string_free (&to_hash);
431 _dbus_string_zero (&cookie);
432 _dbus_string_free (&cookie);
436 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
437 * entropy, we use 128. This is the number of bytes in the random
440 #define N_CHALLENGE_BYTES (128/8)
443 sha1_handle_first_client_response (DBusAuth *auth,
444 const DBusString *data)
446 /* We haven't sent a challenge yet, we're expecting a desired
447 * username from the client.
457 _dbus_string_set_length (&auth->challenge, 0);
459 if (_dbus_string_get_length (data) > 0)
461 if (_dbus_string_get_length (&auth->identity) > 0)
463 /* Tried to send two auth identities, wtf */
464 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
465 return send_rejected (auth);
469 /* this is our auth identity */
470 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
475 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
477 _dbus_verbose ("Did not get a valid username from client\n");
478 return send_rejected (auth);
481 if (!_dbus_string_init (&tmp))
484 if (!_dbus_string_init (&tmp2))
486 _dbus_string_free (&tmp);
490 old_len = _dbus_string_get_length (&auth->outgoing);
492 /* we cache the keyring for speed, so here we drop it if it's the
493 * wrong one. FIXME caching the keyring here is useless since we use
494 * a different DBusAuth for every connection.
497 !_dbus_keyring_is_for_user (auth->keyring,
500 _dbus_keyring_unref (auth->keyring);
501 auth->keyring = NULL;
504 if (auth->keyring == NULL)
508 dbus_error_init (&error);
509 auth->keyring = _dbus_keyring_new_homedir (data,
513 if (auth->keyring == NULL)
515 if (dbus_error_has_name (&error,
516 DBUS_ERROR_NO_MEMORY))
518 dbus_error_free (&error);
523 _DBUS_ASSERT_ERROR_IS_SET (&error);
524 _dbus_verbose ("Error loading keyring: %s\n",
526 if (send_rejected (auth))
527 retval = TRUE; /* retval is only about mem */
528 dbus_error_free (&error);
534 _dbus_assert (!dbus_error_is_set (&error));
538 _dbus_assert (auth->keyring != NULL);
540 dbus_error_init (&error);
541 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
542 if (auth->cookie_id < 0)
544 _DBUS_ASSERT_ERROR_IS_SET (&error);
545 _dbus_verbose ("Could not get a cookie ID to send to client: %s\n",
547 if (send_rejected (auth))
549 dbus_error_free (&error);
554 _dbus_assert (!dbus_error_is_set (&error));
557 if (!_dbus_string_copy (&auth->context, 0,
558 &tmp2, _dbus_string_get_length (&tmp2)))
561 if (!_dbus_string_append (&tmp2, " "))
564 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
567 if (!_dbus_string_append (&tmp2, " "))
570 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
573 _dbus_string_set_length (&auth->challenge, 0);
574 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
577 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
578 _dbus_string_get_length (&tmp2)))
581 if (!_dbus_string_append (&auth->outgoing,
585 if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
586 _dbus_string_get_length (&auth->outgoing)))
589 if (!_dbus_string_append (&auth->outgoing,
596 _dbus_string_zero (&tmp);
597 _dbus_string_free (&tmp);
598 _dbus_string_zero (&tmp2);
599 _dbus_string_free (&tmp2);
601 _dbus_string_set_length (&auth->outgoing, old_len);
606 sha1_handle_second_client_response (DBusAuth *auth,
607 const DBusString *data)
609 /* We are expecting a response which is the hex-encoded client
610 * challenge, space, then SHA-1 hash of the concatenation of our
611 * challenge, ":", client challenge, ":", secret key, all
615 DBusString client_challenge;
616 DBusString client_hash;
618 DBusString correct_hash;
622 if (!_dbus_string_find_blank (data, 0, &i))
624 _dbus_verbose ("no space separator in client response\n");
625 return send_rejected (auth);
628 if (!_dbus_string_init (&client_challenge))
631 if (!_dbus_string_init (&client_hash))
634 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
638 _dbus_string_skip_blank (data, i, &i);
640 if (!_dbus_string_copy_len (data, i,
641 _dbus_string_get_length (data) - i,
646 if (_dbus_string_get_length (&client_challenge) == 0 ||
647 _dbus_string_get_length (&client_hash) == 0)
649 _dbus_verbose ("zero-length client challenge or hash\n");
650 if (send_rejected (auth))
655 if (!_dbus_string_init (&correct_hash))
658 if (!sha1_compute_hash (auth, auth->cookie_id,
664 /* if cookie_id was invalid, then we get an empty hash */
665 if (_dbus_string_get_length (&correct_hash) == 0)
667 if (send_rejected (auth))
672 if (!_dbus_string_equal (&client_hash, &correct_hash))
674 if (send_rejected (auth))
679 if (!_dbus_string_append (&auth->outgoing,
683 _dbus_verbose ("authenticated client with UID %d using DBUS_COOKIE_SHA1\n",
684 auth->desired_identity.uid);
686 auth->authorized_identity = auth->desired_identity;
687 auth->authenticated_pending_begin = TRUE;
691 _dbus_string_zero (&correct_hash);
692 _dbus_string_free (&correct_hash);
694 _dbus_string_zero (&client_hash);
695 _dbus_string_free (&client_hash);
697 _dbus_string_free (&client_challenge);
703 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
704 const DBusString *data)
706 if (auth->cookie_id < 0)
707 return sha1_handle_first_client_response (auth, data);
709 return sha1_handle_second_client_response (auth, data);
713 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
715 auth->cookie_id = -1;
716 _dbus_string_set_length (&auth->challenge, 0);
720 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
721 DBusString *response)
723 const DBusString *username;
728 if (!_dbus_user_info_from_current_process (&username,
732 if (!_dbus_string_base64_encode (username, 0,
734 _dbus_string_get_length (response)))
743 /* FIXME if we send the server an error, right now both sides
744 * just hang. Server has to reject on getting an error, or
745 * client has to cancel. Should be in the spec.
748 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
749 const DBusString *data)
751 /* The data we get from the server should be the cookie context
752 * name, the cookie ID, and the server challenge, separated by
753 * spaces. We send back our challenge string and the correct hash.
757 DBusString cookie_id_str;
758 DBusString server_challenge;
759 DBusString client_challenge;
760 DBusString correct_hash;
768 if (!_dbus_string_find_blank (data, 0, &i))
770 if (_dbus_string_append (&auth->outgoing,
771 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
776 if (!_dbus_string_init (&context))
779 if (!_dbus_string_copy_len (data, 0, i,
783 _dbus_string_skip_blank (data, i, &i);
784 if (!_dbus_string_find_blank (data, i, &j))
786 if (_dbus_string_append (&auth->outgoing,
787 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
792 if (!_dbus_string_init (&cookie_id_str))
795 if (!_dbus_string_copy_len (data, i, j - i,
799 if (!_dbus_string_init (&server_challenge))
803 _dbus_string_skip_blank (data, i, &i);
804 j = _dbus_string_get_length (data);
806 if (!_dbus_string_copy_len (data, i, j - i,
807 &server_challenge, 0))
810 if (!_dbus_keyring_validate_context (&context))
812 if (_dbus_string_append (&auth->outgoing,
813 "ERROR \"Server sent invalid cookie context\"\r\n"))
818 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
820 if (_dbus_string_append (&auth->outgoing,
821 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
826 if (_dbus_string_get_length (&server_challenge) == 0)
828 if (_dbus_string_append (&auth->outgoing,
829 "ERROR \"Empty server challenge string\"\r\n"))
834 if (auth->keyring == NULL)
838 dbus_error_init (&error);
839 auth->keyring = _dbus_keyring_new_homedir (NULL,
843 if (auth->keyring == NULL)
845 if (dbus_error_has_name (&error,
846 DBUS_ERROR_NO_MEMORY))
848 dbus_error_free (&error);
853 _DBUS_ASSERT_ERROR_IS_SET (&error);
855 _dbus_verbose ("Error loading keyring: %s\n",
858 if (_dbus_string_append (&auth->outgoing,
859 "ERROR \"Could not load cookie file\"\r\n"))
860 retval = TRUE; /* retval is only about mem */
862 dbus_error_free (&error);
868 _dbus_assert (!dbus_error_is_set (&error));
872 _dbus_assert (auth->keyring != NULL);
874 if (!_dbus_string_init (&tmp))
877 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
880 if (!_dbus_string_init (&client_challenge))
883 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
886 if (!_dbus_string_init (&correct_hash))
889 if (!sha1_compute_hash (auth, val,
895 if (_dbus_string_get_length (&correct_hash) == 0)
897 /* couldn't find the cookie ID or something */
898 if (_dbus_string_append (&auth->outgoing,
899 "ERROR \"Don't have the requested cookie ID\"\r\n"))
904 _dbus_string_set_length (&tmp, 0);
906 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
907 _dbus_string_get_length (&tmp)))
910 if (!_dbus_string_append (&tmp, " "))
913 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
914 _dbus_string_get_length (&tmp)))
917 old_len = _dbus_string_get_length (&auth->outgoing);
918 if (!_dbus_string_append (&auth->outgoing, "DATA "))
921 if (!_dbus_string_base64_encode (&tmp, 0,
923 _dbus_string_get_length (&auth->outgoing)))
925 _dbus_string_set_length (&auth->outgoing, old_len);
929 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
931 _dbus_string_set_length (&auth->outgoing, old_len);
938 _dbus_string_zero (&correct_hash);
939 _dbus_string_free (&correct_hash);
941 _dbus_string_free (&client_challenge);
943 _dbus_string_zero (&tmp);
944 _dbus_string_free (&tmp);
946 _dbus_string_free (&server_challenge);
948 _dbus_string_free (&cookie_id_str);
950 _dbus_string_free (&context);
956 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
958 auth->cookie_id = -1;
959 _dbus_string_set_length (&auth->challenge, 0);
963 handle_server_data_external_mech (DBusAuth *auth,
964 const DBusString *data)
966 if (auth->credentials.uid < 0)
968 _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
969 return send_rejected (auth);
972 if (_dbus_string_get_length (data) > 0)
974 if (_dbus_string_get_length (&auth->identity) > 0)
976 /* Tried to send two auth identities, wtf */
977 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
978 return send_rejected (auth);
982 /* this is our auth identity */
983 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
988 /* Poke client for an auth identity, if none given */
989 if (_dbus_string_get_length (&auth->identity) == 0 &&
990 !auth->already_asked_for_initial_response)
992 if (_dbus_string_append (&auth->outgoing,
995 _dbus_verbose ("sending empty challenge asking client for auth identity\n");
996 auth->already_asked_for_initial_response = TRUE;
1003 auth->desired_identity.pid = -1;
1004 auth->desired_identity.uid = -1;
1005 auth->desired_identity.gid = -1;
1007 /* If auth->identity is still empty here, then client
1008 * responded with an empty string after we poked it for
1009 * an initial response. This means to try to auth the
1010 * identity provided in the credentials.
1012 if (_dbus_string_get_length (&auth->identity) == 0)
1014 auth->desired_identity.uid = auth->credentials.uid;
1018 if (!_dbus_credentials_from_uid_string (&auth->identity,
1019 &auth->desired_identity))
1021 _dbus_verbose ("could not get credentials from uid string\n");
1022 return send_rejected (auth);
1026 if (auth->desired_identity.uid < 0)
1028 _dbus_verbose ("desired UID %d is no good\n", auth->desired_identity.uid);
1029 return send_rejected (auth);
1032 if (_dbus_credentials_match (&auth->desired_identity,
1033 &auth->credentials))
1035 /* client has authenticated */
1036 if (!_dbus_string_append (&auth->outgoing,
1040 _dbus_verbose ("authenticated client with UID %d matching socket credentials UID %d\n",
1041 auth->desired_identity.uid,
1042 auth->credentials.uid);
1044 auth->authorized_identity.uid = auth->desired_identity.uid;
1046 auth->authenticated_pending_begin = TRUE;
1052 _dbus_verbose ("credentials uid=%d gid=%d do not allow uid=%d gid=%d\n",
1053 auth->credentials.uid, auth->credentials.gid,
1054 auth->desired_identity.uid, auth->desired_identity.gid);
1055 return send_rejected (auth);
1060 handle_server_shutdown_external_mech (DBusAuth *auth)
1066 handle_client_initial_response_external_mech (DBusAuth *auth,
1067 DBusString *response)
1069 /* We always append our UID as an initial response, so the server
1070 * doesn't have to send back an empty challenge to check whether we
1071 * want to specify an identity. i.e. this avoids a round trip that
1072 * the spec for the EXTERNAL mechanism otherwise requires.
1074 DBusString plaintext;
1076 if (!_dbus_string_init (&plaintext))
1079 if (!_dbus_string_append_our_uid (&plaintext))
1082 if (!_dbus_string_base64_encode (&plaintext, 0,
1084 _dbus_string_get_length (response)))
1087 _dbus_string_free (&plaintext);
1092 _dbus_string_free (&plaintext);
1097 handle_client_data_external_mech (DBusAuth *auth,
1098 const DBusString *data)
1105 handle_client_shutdown_external_mech (DBusAuth *auth)
1110 /* Put mechanisms here in order of preference.
1111 * What I eventually want to have is:
1113 * - a mechanism that checks UNIX domain socket credentials
1114 * - a simple magic cookie mechanism like X11 or ICE
1115 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1116 * offers such as Kerberos, X509, whatever.
1119 static const DBusAuthMechanismHandler
1120 all_mechanisms[] = {
1122 handle_server_data_external_mech,
1124 handle_server_shutdown_external_mech,
1125 handle_client_initial_response_external_mech,
1126 handle_client_data_external_mech,
1128 handle_client_shutdown_external_mech },
1129 { "DBUS_COOKIE_SHA1",
1130 handle_server_data_cookie_sha1_mech,
1132 handle_server_shutdown_cookie_sha1_mech,
1133 handle_client_initial_response_cookie_sha1_mech,
1134 handle_client_data_cookie_sha1_mech,
1136 handle_client_shutdown_cookie_sha1_mech },
1140 static const DBusAuthMechanismHandler*
1141 find_mech (const DBusString *name,
1142 char **allowed_mechs)
1146 if (allowed_mechs != NULL &&
1147 !_dbus_string_array_contains ((const char**) allowed_mechs,
1148 _dbus_string_get_const_data (name)))
1152 while (all_mechanisms[i].mechanism != NULL)
1154 if (_dbus_string_equal_c_str (name,
1155 all_mechanisms[i].mechanism))
1157 return &all_mechanisms[i];
1166 send_rejected (DBusAuth *auth)
1169 DBusAuthServer *server_auth;
1172 if (!_dbus_string_init (&command))
1175 if (!_dbus_string_append (&command,
1180 while (all_mechanisms[i].mechanism != NULL)
1182 if (!_dbus_string_append (&command,
1186 if (!_dbus_string_append (&command,
1187 all_mechanisms[i].mechanism))
1193 if (!_dbus_string_append (&command, "\r\n"))
1196 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1197 _dbus_string_get_length (&auth->outgoing)))
1200 shutdown_mech (auth);
1202 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1203 server_auth = DBUS_AUTH_SERVER (auth);
1204 server_auth->failures += 1;
1206 _dbus_string_free (&command);
1211 _dbus_string_free (&command);
1216 process_auth (DBusAuth *auth,
1217 const DBusString *command,
1218 const DBusString *args)
1222 /* We are already using a mechanism, client is on crack */
1223 if (!_dbus_string_append (&auth->outgoing,
1224 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1229 else if (_dbus_string_get_length (args) == 0)
1231 /* No args to the auth, send mechanisms */
1232 if (!send_rejected (auth))
1241 DBusString base64_response;
1242 DBusString decoded_response;
1244 _dbus_string_find_blank (args, 0, &i);
1246 if (!_dbus_string_init (&mech))
1249 if (!_dbus_string_init (&base64_response))
1251 _dbus_string_free (&mech);
1255 if (!_dbus_string_init (&decoded_response))
1257 _dbus_string_free (&mech);
1258 _dbus_string_free (&base64_response);
1262 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1265 if (!_dbus_string_copy (args, i, &base64_response, 0))
1268 if (!_dbus_string_base64_decode (&base64_response, 0,
1269 &decoded_response, 0))
1272 auth->mech = find_mech (&mech, auth->allowed_mechs);
1273 if (auth->mech != NULL)
1275 _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
1276 auth->mech->mechanism,
1277 _dbus_string_get_length (&decoded_response));
1279 if (!(* auth->mech->server_data_func) (auth,
1285 /* Unsupported mechanism */
1286 if (!send_rejected (auth))
1290 _dbus_string_free (&mech);
1291 _dbus_string_free (&base64_response);
1292 _dbus_string_free (&decoded_response);
1298 _dbus_string_free (&mech);
1299 _dbus_string_free (&base64_response);
1300 _dbus_string_free (&decoded_response);
1306 process_cancel (DBusAuth *auth,
1307 const DBusString *command,
1308 const DBusString *args)
1310 shutdown_mech (auth);
1316 process_begin (DBusAuth *auth,
1317 const DBusString *command,
1318 const DBusString *args)
1320 if (auth->authenticated_pending_begin)
1321 auth->authenticated = TRUE;
1324 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1327 shutdown_mech (auth);
1334 process_data_server (DBusAuth *auth,
1335 const DBusString *command,
1336 const DBusString *args)
1338 if (auth->mech != NULL)
1342 if (!_dbus_string_init (&decoded))
1345 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1347 _dbus_string_free (&decoded);
1351 #ifdef DBUS_ENABLE_VERBOSE_MODE
1352 if (_dbus_string_validate_ascii (&decoded, 0,
1353 _dbus_string_get_length (&decoded)))
1354 _dbus_verbose ("data: '%s'\n", _dbus_string_get_const_data (&decoded));
1357 if (!(* auth->mech->server_data_func) (auth, &decoded))
1359 _dbus_string_free (&decoded);
1363 _dbus_string_free (&decoded);
1367 if (!_dbus_string_append (&auth->outgoing,
1368 "ERROR \"Not currently in an auth conversation\"\r\n"))
1376 process_error_server (DBusAuth *auth,
1377 const DBusString *command,
1378 const DBusString *args)
1384 /* return FALSE if no memory, TRUE if all OK */
1386 get_word (const DBusString *str,
1392 _dbus_string_skip_blank (str, *start, start);
1393 _dbus_string_find_blank (str, *start, &i);
1397 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1407 record_mechanisms (DBusAuth *auth,
1408 const DBusString *command,
1409 const DBusString *args)
1414 if (auth->already_got_mechanisms)
1417 len = _dbus_string_get_length (args);
1423 const DBusAuthMechanismHandler *mech;
1425 if (!_dbus_string_init (&m))
1428 if (!get_word (args, &next, &m))
1431 mech = find_mech (&m, auth->allowed_mechs);
1435 /* FIXME right now we try mechanisms in the order
1436 * the server lists them; should we do them in
1437 * some more deterministic order?
1439 * Probably in all_mechanisms order, our order of
1440 * preference. Of course when the server is us,
1441 * it lists things in that order anyhow.
1444 _dbus_verbose ("Adding mechanism %s to list we will try\n",
1447 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1453 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1454 _dbus_string_get_const_data (&m));
1457 _dbus_string_free (&m);
1460 auth->already_got_mechanisms = TRUE;
1465 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1471 client_try_next_mechanism (DBusAuth *auth)
1473 const DBusAuthMechanismHandler *mech;
1474 DBusString auth_command;
1475 DBusAuthClient *client;
1477 client = DBUS_AUTH_CLIENT (auth);
1479 /* Pop any mechs not in the list of allowed mechanisms */
1481 while (client->mechs_to_try != NULL)
1483 mech = client->mechs_to_try->data;
1485 if (auth->allowed_mechs != NULL &&
1486 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1489 /* don't try this one after all */
1490 _dbus_verbose ("Mechanism %s isn't in the list of allowed mechanisms\n",
1493 _dbus_list_pop_first (& client->mechs_to_try);
1496 break; /* we'll try this one */
1502 if (!_dbus_string_init (&auth_command))
1505 if (!_dbus_string_append (&auth_command,
1508 _dbus_string_free (&auth_command);
1512 if (!_dbus_string_append (&auth_command,
1515 _dbus_string_free (&auth_command);
1519 if (mech->client_initial_response_func != NULL)
1521 if (!_dbus_string_append (&auth_command, " "))
1523 _dbus_string_free (&auth_command);
1527 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1529 _dbus_string_free (&auth_command);
1534 if (!_dbus_string_append (&auth_command,
1537 _dbus_string_free (&auth_command);
1541 if (!_dbus_string_copy (&auth_command, 0,
1543 _dbus_string_get_length (&auth->outgoing)))
1545 _dbus_string_free (&auth_command);
1550 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1552 _dbus_verbose ("Trying mechanism %s\n",
1553 auth->mech->mechanism);
1555 _dbus_string_free (&auth_command);
1561 process_rejected (DBusAuth *auth,
1562 const DBusString *command,
1563 const DBusString *args)
1565 shutdown_mech (auth);
1567 if (!auth->already_got_mechanisms)
1569 if (!record_mechanisms (auth, command, args))
1573 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1575 client_try_next_mechanism (auth);
1580 auth->need_disconnect = TRUE;
1587 process_ok (DBusAuth *auth,
1588 const DBusString *command,
1589 const DBusString *args)
1591 if (!_dbus_string_append (&auth->outgoing,
1595 auth->authenticated_pending_output = TRUE;
1601 process_data_client (DBusAuth *auth,
1602 const DBusString *command,
1603 const DBusString *args)
1605 if (auth->mech != NULL)
1609 if (!_dbus_string_init (&decoded))
1612 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1614 _dbus_string_free (&decoded);
1618 #ifdef DBUS_ENABLE_VERBOSE_MODE
1619 if (_dbus_string_validate_ascii (&decoded, 0,
1620 _dbus_string_get_length (&decoded)))
1622 _dbus_verbose ("data: '%s'\n",
1623 _dbus_string_get_const_data (&decoded));
1627 if (!(* auth->mech->client_data_func) (auth, &decoded))
1629 _dbus_string_free (&decoded);
1633 _dbus_string_free (&decoded);
1637 if (!_dbus_string_append (&auth->outgoing,
1638 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1646 process_error_client (DBusAuth *auth,
1647 const DBusString *command,
1648 const DBusString *args)
1654 process_unknown (DBusAuth *auth,
1655 const DBusString *command,
1656 const DBusString *args)
1658 if (!_dbus_string_append (&auth->outgoing,
1659 "ERROR \"Unknown command\"\r\n"))
1665 /* returns whether to call it again right away */
1667 process_command (DBusAuth *auth)
1675 /* _dbus_verbose (" trying process_command()\n"); */
1680 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1683 if (!_dbus_string_init (&command))
1685 auth->needed_memory = TRUE;
1689 if (!_dbus_string_init (&args))
1691 _dbus_string_free (&command);
1692 auth->needed_memory = TRUE;
1696 if (eol > _DBUS_ONE_MEGABYTE)
1698 /* This is a giant line, someone is trying to hose us. */
1699 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1705 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1708 if (!_dbus_string_validate_ascii (&command, 0,
1709 _dbus_string_get_length (&command)))
1711 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1712 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1718 _dbus_verbose ("got command \"%s\"\n", _dbus_string_get_const_data (&command));
1720 _dbus_string_find_blank (&command, 0, &i);
1721 _dbus_string_skip_blank (&command, i, &j);
1724 _dbus_string_delete (&command, i, j - i);
1726 if (!_dbus_string_move (&command, i, &args, 0))
1730 while (auth->handlers[i].command != NULL)
1732 if (_dbus_string_equal_c_str (&command,
1733 auth->handlers[i].command))
1735 _dbus_verbose ("Processing auth command %s\n",
1736 auth->handlers[i].command);
1738 if (!(* auth->handlers[i].func) (auth, &command, &args))
1746 if (auth->handlers[i].command == NULL)
1748 if (!process_unknown (auth, &command, &args))
1754 /* We've succeeded in processing the whole command so drop it out
1755 * of the incoming buffer and return TRUE to try another command.
1758 _dbus_string_delete (&auth->incoming, 0, eol);
1761 _dbus_string_delete (&auth->incoming, 0, 2);
1766 _dbus_string_free (&args);
1767 _dbus_string_free (&command);
1770 auth->needed_memory = TRUE;
1772 auth->needed_memory = FALSE;
1781 * @addtogroup DBusAuth
1786 * Creates a new auth conversation object for the server side.
1787 * See doc/dbus-sasl-profile.txt for full details on what
1790 * @returns the new object or #NULL if no memory
1793 _dbus_auth_server_new (void)
1796 DBusAuthServer *server_auth;
1798 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1802 auth->handlers = server_handlers;
1804 server_auth = DBUS_AUTH_SERVER (auth);
1806 /* perhaps this should be per-mechanism with a lower
1809 server_auth->failures = 0;
1810 server_auth->max_failures = 6;
1816 * Creates a new auth conversation object for the client side.
1817 * See doc/dbus-sasl-profile.txt for full details on what
1820 * @returns the new object or #NULL if no memory
1823 _dbus_auth_client_new (void)
1827 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1831 auth->handlers = client_handlers;
1833 /* Add a default mechanism to try */
1834 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1835 (void*) &all_mechanisms[0]))
1837 _dbus_auth_unref (auth);
1841 /* Now try the mechanism we just added */
1842 if (!client_try_next_mechanism (auth))
1844 _dbus_auth_unref (auth);
1852 * Increments the refcount of an auth object.
1854 * @param auth the auth conversation
1857 _dbus_auth_ref (DBusAuth *auth)
1859 _dbus_assert (auth != NULL);
1861 auth->refcount += 1;
1865 * Decrements the refcount of an auth object.
1867 * @param auth the auth conversation
1870 _dbus_auth_unref (DBusAuth *auth)
1872 _dbus_assert (auth != NULL);
1873 _dbus_assert (auth->refcount > 0);
1875 auth->refcount -= 1;
1876 if (auth->refcount == 0)
1878 shutdown_mech (auth);
1880 if (DBUS_AUTH_IS_CLIENT (auth))
1882 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1886 _dbus_keyring_unref (auth->keyring);
1888 _dbus_string_free (&auth->context);
1889 _dbus_string_free (&auth->challenge);
1890 _dbus_string_free (&auth->identity);
1891 _dbus_string_free (&auth->incoming);
1892 _dbus_string_free (&auth->outgoing);
1894 dbus_free_string_array (auth->allowed_mechs);
1901 * Sets an array of authentication mechanism names
1902 * that we are willing to use.
1904 * @param auth the auth conversation
1905 * @param mechanisms #NULL-terminated array of mechanism names
1906 * @returns #FALSE if no memory
1909 _dbus_auth_set_mechanisms (DBusAuth *auth,
1910 const char **mechanisms)
1914 if (mechanisms != NULL)
1916 copy = _dbus_dup_string_array (mechanisms);
1923 dbus_free_string_array (auth->allowed_mechs);
1925 auth->allowed_mechs = copy;
1931 * @param auth the auth conversation object
1932 * @returns #TRUE if we're in a final state
1934 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1937 * Analyzes buffered input and moves the auth conversation forward,
1938 * returning the new state of the auth conversation.
1940 * @param auth the auth conversation
1941 * @returns the new state
1944 _dbus_auth_do_work (DBusAuth *auth)
1946 auth->needed_memory = FALSE;
1948 /* Max amount we'll buffer up before deciding someone's on crack */
1949 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1953 if (DBUS_AUTH_IN_END_STATE (auth))
1956 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1957 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1959 auth->need_disconnect = TRUE;
1960 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1964 if (auth->mech == NULL &&
1965 auth->already_got_mechanisms &&
1966 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1968 auth->need_disconnect = TRUE;
1969 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1973 while (process_command (auth));
1975 if (DBUS_AUTH_IS_SERVER (auth) &&
1976 DBUS_AUTH_SERVER (auth)->failures >=
1977 DBUS_AUTH_SERVER (auth)->max_failures)
1978 auth->need_disconnect = TRUE;
1980 if (auth->need_disconnect)
1981 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1982 else if (auth->authenticated)
1984 if (_dbus_string_get_length (&auth->incoming) > 0)
1985 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1987 return DBUS_AUTH_STATE_AUTHENTICATED;
1989 else if (auth->needed_memory)
1990 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1991 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1992 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1994 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1998 * Gets bytes that need to be sent to the peer we're conversing with.
1999 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2000 * to notify the auth object that they were written.
2002 * @param auth the auth conversation
2003 * @param str return location for a ref to the buffer to send
2004 * @returns #FALSE if nothing to send
2007 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2008 const DBusString **str)
2010 _dbus_assert (auth != NULL);
2011 _dbus_assert (str != NULL);
2015 if (DBUS_AUTH_IN_END_STATE (auth))
2018 if (_dbus_string_get_length (&auth->outgoing) == 0)
2021 *str = &auth->outgoing;
2027 * Notifies the auth conversation object that
2028 * the given number of bytes of the outgoing buffer
2029 * have been written out.
2031 * @param auth the auth conversation
2032 * @param bytes_sent number of bytes written out
2035 _dbus_auth_bytes_sent (DBusAuth *auth,
2038 _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent,
2039 _dbus_string_get_const_data (&auth->outgoing));
2041 _dbus_string_delete (&auth->outgoing,
2044 if (auth->authenticated_pending_output &&
2045 _dbus_string_get_length (&auth->outgoing) == 0)
2046 auth->authenticated = TRUE;
2050 * Get a buffer to be used for reading bytes from the peer we're conversing
2051 * with. Bytes should be appended to this buffer.
2053 * @param auth the auth conversation
2054 * @param buffer return location for buffer to append bytes to
2057 _dbus_auth_get_buffer (DBusAuth *auth,
2058 DBusString **buffer)
2060 _dbus_assert (auth != NULL);
2061 _dbus_assert (!auth->buffer_outstanding);
2063 *buffer = &auth->incoming;
2065 auth->buffer_outstanding = TRUE;
2069 * Returns a buffer with new data read into it.
2071 * @param auth the auth conversation
2072 * @param buffer the buffer being returned
2073 * @param bytes_read number of new bytes added
2076 _dbus_auth_return_buffer (DBusAuth *auth,
2080 _dbus_assert (buffer == &auth->incoming);
2081 _dbus_assert (auth->buffer_outstanding);
2083 auth->buffer_outstanding = FALSE;
2087 * Returns leftover bytes that were not used as part of the auth
2088 * conversation. These bytes will be part of the message stream
2089 * instead. This function may not be called until authentication has
2092 * @param auth the auth conversation
2093 * @param str return location for pointer to string of unused bytes
2096 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2097 const DBusString **str)
2099 if (!DBUS_AUTH_IN_END_STATE (auth))
2102 *str = &auth->incoming;
2107 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2108 * after we've gotten them and successfully moved them elsewhere.
2110 * @param auth the auth conversation
2113 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2115 if (!DBUS_AUTH_IN_END_STATE (auth))
2118 _dbus_string_set_length (&auth->incoming, 0);
2122 * Called post-authentication, indicates whether we need to encode
2123 * the message stream with _dbus_auth_encode_data() prior to
2124 * sending it to the peer.
2126 * @param auth the auth conversation
2127 * @returns #TRUE if we need to encode the stream
2130 _dbus_auth_needs_encoding (DBusAuth *auth)
2132 if (!auth->authenticated)
2135 if (auth->mech != NULL)
2137 if (DBUS_AUTH_IS_CLIENT (auth))
2138 return auth->mech->client_encode_func != NULL;
2140 return auth->mech->server_encode_func != NULL;
2147 * Called post-authentication, encodes a block of bytes for sending to
2148 * the peer. If no encoding was negotiated, just copies the bytes
2149 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2151 * @param auth the auth conversation
2152 * @param plaintext the plain text data
2153 * @param encoded initialized string to where encoded data is appended
2154 * @returns #TRUE if we had enough memory and successfully encoded
2157 _dbus_auth_encode_data (DBusAuth *auth,
2158 const DBusString *plaintext,
2159 DBusString *encoded)
2161 _dbus_assert (plaintext != encoded);
2163 if (!auth->authenticated)
2166 if (_dbus_auth_needs_encoding (auth))
2168 if (DBUS_AUTH_IS_CLIENT (auth))
2169 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2171 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2175 return _dbus_string_copy (plaintext, 0, encoded,
2176 _dbus_string_get_length (encoded));
2181 * Called post-authentication, indicates whether we need to decode
2182 * the message stream with _dbus_auth_decode_data() after
2183 * receiving it from the peer.
2185 * @param auth the auth conversation
2186 * @returns #TRUE if we need to encode the stream
2189 _dbus_auth_needs_decoding (DBusAuth *auth)
2191 if (!auth->authenticated)
2194 if (auth->mech != NULL)
2196 if (DBUS_AUTH_IS_CLIENT (auth))
2197 return auth->mech->client_decode_func != NULL;
2199 return auth->mech->server_decode_func != NULL;
2207 * Called post-authentication, decodes a block of bytes received from
2208 * the peer. If no encoding was negotiated, just copies the bytes (you
2209 * can avoid this by checking _dbus_auth_needs_decoding()).
2211 * @todo We need to be able to distinguish "out of memory" error
2212 * from "the data is hosed" error.
2214 * @param auth the auth conversation
2215 * @param encoded the encoded data
2216 * @param plaintext initialized string where decoded data is appended
2217 * @returns #TRUE if we had enough memory and successfully decoded
2220 _dbus_auth_decode_data (DBusAuth *auth,
2221 const DBusString *encoded,
2222 DBusString *plaintext)
2224 _dbus_assert (plaintext != encoded);
2226 if (!auth->authenticated)
2229 if (_dbus_auth_needs_decoding (auth))
2231 if (DBUS_AUTH_IS_CLIENT (auth))
2232 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2234 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2238 return _dbus_string_copy (encoded, 0, plaintext,
2239 _dbus_string_get_length (plaintext));
2244 * Sets credentials received via reliable means from the operating
2247 * @param auth the auth conversation
2248 * @param credentials the credentials received
2251 _dbus_auth_set_credentials (DBusAuth *auth,
2252 const DBusCredentials *credentials)
2254 auth->credentials = *credentials;
2258 * Gets the identity we authorized the client as. Apps may have
2259 * different policies as to what identities they allow.
2261 * @param auth the auth conversation
2262 * @param credentials the credentials we've authorized
2265 _dbus_auth_get_identity (DBusAuth *auth,
2266 DBusCredentials *credentials)
2268 if (auth->authenticated)
2270 *credentials = auth->authorized_identity;
2274 credentials->pid = -1;
2275 credentials->uid = -1;
2276 credentials->gid = -1;
2281 * Sets the "authentication context" which scopes cookies
2282 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2284 * @param auth the auth conversation
2285 * @param context the context
2286 * @returns #FALSE if no memory
2289 _dbus_auth_set_context (DBusAuth *auth,
2290 const DBusString *context)
2292 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2293 &auth->context, 0, _dbus_string_get_length (context));
2298 #ifdef DBUS_BUILD_TESTS
2299 #include "dbus-test.h"
2300 #include "dbus-auth-script.h"
2304 process_test_subdir (const DBusString *test_base_dir,
2307 DBusString test_directory;
2308 DBusString filename;
2316 if (!_dbus_string_init (&test_directory))
2317 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2319 _dbus_string_init_const (&filename, subdir);
2321 if (!_dbus_string_copy (test_base_dir, 0,
2322 &test_directory, 0))
2323 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2325 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2326 _dbus_assert_not_reached ("couldn't allocate full path");
2328 _dbus_string_free (&filename);
2329 if (!_dbus_string_init (&filename))
2330 _dbus_assert_not_reached ("didn't allocate filename string\n");
2332 dbus_error_init (&error);
2333 dir = _dbus_directory_open (&test_directory, &error);
2336 _dbus_warn ("Could not open %s: %s\n",
2337 _dbus_string_get_const_data (&test_directory),
2339 dbus_error_free (&error);
2343 printf ("Testing:\n");
2346 while (_dbus_directory_get_next_file (dir, &filename, &error))
2348 DBusString full_path;
2350 if (!_dbus_string_init (&full_path))
2351 _dbus_assert_not_reached ("couldn't init string");
2353 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2354 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2356 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2357 _dbus_assert_not_reached ("couldn't concat file to dir");
2359 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2361 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2362 _dbus_string_get_const_data (&filename));
2363 _dbus_string_free (&full_path);
2367 printf (" %s\n", _dbus_string_get_const_data (&filename));
2369 if (!_dbus_auth_script_run (&full_path))
2371 _dbus_string_free (&full_path);
2375 _dbus_string_free (&full_path);
2378 if (dbus_error_is_set (&error))
2380 _dbus_warn ("Could not get next file in %s: %s\n",
2381 _dbus_string_get_const_data (&test_directory), error.message);
2382 dbus_error_free (&error);
2391 _dbus_directory_close (dir);
2392 _dbus_string_free (&test_directory);
2393 _dbus_string_free (&filename);
2399 process_test_dirs (const char *test_data_dir)
2401 DBusString test_directory;
2406 _dbus_string_init_const (&test_directory, test_data_dir);
2408 if (!process_test_subdir (&test_directory, "auth"))
2415 _dbus_string_free (&test_directory);
2421 _dbus_auth_test (const char *test_data_dir)
2424 if (test_data_dir == NULL)
2427 if (!process_test_dirs (test_data_dir))
2433 #endif /* DBUS_BUILD_TESTS */