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 unsigned int needed_memory : 1; /**< We needed memory to continue since last
164 * successful getting something done
166 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
167 unsigned int authenticated : 1; /**< We are authenticated */
168 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
169 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
170 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
171 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
172 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
179 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
187 int failures; /**< Number of times client has been rejected */
188 int max_failures; /**< Number of times we reject before disconnect */
192 static dbus_bool_t process_auth (DBusAuth *auth,
193 const DBusString *command,
194 const DBusString *args);
195 static dbus_bool_t process_cancel (DBusAuth *auth,
196 const DBusString *command,
197 const DBusString *args);
198 static dbus_bool_t process_begin (DBusAuth *auth,
199 const DBusString *command,
200 const DBusString *args);
201 static dbus_bool_t process_data_server (DBusAuth *auth,
202 const DBusString *command,
203 const DBusString *args);
204 static dbus_bool_t process_error_server (DBusAuth *auth,
205 const DBusString *command,
206 const DBusString *args);
207 static dbus_bool_t process_rejected (DBusAuth *auth,
208 const DBusString *command,
209 const DBusString *args);
210 static dbus_bool_t process_ok (DBusAuth *auth,
211 const DBusString *command,
212 const DBusString *args);
213 static dbus_bool_t process_data_client (DBusAuth *auth,
214 const DBusString *command,
215 const DBusString *args);
216 static dbus_bool_t process_error_client (DBusAuth *auth,
217 const DBusString *command,
218 const DBusString *args);
221 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
222 static dbus_bool_t send_rejected (DBusAuth *auth);
224 static DBusAuthCommandHandler
225 server_handlers[] = {
226 { "AUTH", process_auth },
227 { "CANCEL", process_cancel },
228 { "BEGIN", process_begin },
229 { "DATA", process_data_server },
230 { "ERROR", process_error_server },
234 static DBusAuthCommandHandler
235 client_handlers[] = {
236 { "REJECTED", process_rejected },
237 { "OK", process_ok },
238 { "DATA", process_data_client },
239 { "ERROR", process_error_client },
244 * @param auth the auth conversation
245 * @returns #TRUE if the conversation is the server side
247 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
249 * @param auth the auth conversation
250 * @returns #TRUE if the conversation is the client side
252 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
254 * @param auth the auth conversation
255 * @returns auth cast to DBusAuthClient
257 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
259 * @param auth the auth conversation
260 * @returns auth cast to DBusAuthServer
262 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
265 _dbus_auth_new (int size)
269 auth = dbus_malloc0 (size);
275 auth->credentials.pid = -1;
276 auth->credentials.uid = -1;
277 auth->credentials.gid = -1;
279 auth->authorized_identity.pid = -1;
280 auth->authorized_identity.uid = -1;
281 auth->authorized_identity.gid = -1;
283 auth->desired_identity.pid = -1;
284 auth->desired_identity.uid = -1;
285 auth->desired_identity.gid = -1;
287 auth->keyring = NULL;
288 auth->cookie_id = -1;
290 /* note that we don't use the max string length feature,
291 * because you can't use that feature if you're going to
292 * try to recover from out-of-memory (it creates
293 * what looks like unrecoverable inability to alloc
294 * more space in the string). But we do handle
295 * overlong buffers in _dbus_auth_do_work().
298 if (!_dbus_string_init (&auth->incoming))
301 if (!_dbus_string_init (&auth->outgoing))
304 if (!_dbus_string_init (&auth->identity))
307 if (!_dbus_string_init (&auth->context))
310 if (!_dbus_string_init (&auth->challenge))
313 /* default context if none is specified */
314 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
320 _dbus_string_free (&auth->challenge);
322 _dbus_string_free (&auth->context);
324 _dbus_string_free (&auth->identity);
326 _dbus_string_free (&auth->outgoing);
328 _dbus_string_free (&auth->incoming);
335 shutdown_mech (DBusAuth *auth)
337 /* Cancel any auth */
338 auth->authenticated_pending_begin = FALSE;
339 auth->authenticated = FALSE;
340 auth->already_asked_for_initial_response = FALSE;
341 _dbus_string_set_length (&auth->identity, 0);
343 auth->authorized_identity.pid = -1;
344 auth->authorized_identity.uid = -1;
345 auth->authorized_identity.gid = -1;
347 auth->desired_identity.pid = -1;
348 auth->desired_identity.uid = -1;
349 auth->desired_identity.gid = -1;
351 if (auth->mech != NULL)
353 _dbus_verbose ("Shutting down mechanism %s\n",
354 auth->mech->mechanism);
356 if (DBUS_AUTH_IS_CLIENT (auth))
357 (* auth->mech->client_shutdown_func) (auth);
359 (* auth->mech->server_shutdown_func) (auth);
365 /* Returns TRUE but with an empty string hash if the
366 * cookie_id isn't known. As with all this code
367 * TRUE just means we had enough memory.
370 sha1_compute_hash (DBusAuth *auth,
372 const DBusString *server_challenge,
373 const DBusString *client_challenge,
380 _dbus_assert (auth->keyring != NULL);
384 if (!_dbus_string_init (&cookie))
387 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
391 if (_dbus_string_get_length (&cookie) == 0)
397 if (!_dbus_string_init (&to_hash))
400 if (!_dbus_string_copy (server_challenge, 0,
401 &to_hash, _dbus_string_get_length (&to_hash)))
404 if (!_dbus_string_append (&to_hash, ":"))
407 if (!_dbus_string_copy (client_challenge, 0,
408 &to_hash, _dbus_string_get_length (&to_hash)))
411 if (!_dbus_string_append (&to_hash, ":"))
414 if (!_dbus_string_copy (&cookie, 0,
415 &to_hash, _dbus_string_get_length (&to_hash)))
418 if (!_dbus_sha_compute (&to_hash, hash))
424 _dbus_string_zero (&to_hash);
425 _dbus_string_free (&to_hash);
427 _dbus_string_zero (&cookie);
428 _dbus_string_free (&cookie);
432 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
433 * entropy, we use 128. This is the number of bytes in the random
436 #define N_CHALLENGE_BYTES (128/8)
439 sha1_handle_first_client_response (DBusAuth *auth,
440 const DBusString *data)
442 /* We haven't sent a challenge yet, we're expecting a desired
443 * username from the client.
453 _dbus_string_set_length (&auth->challenge, 0);
455 if (_dbus_string_get_length (data) > 0)
457 if (_dbus_string_get_length (&auth->identity) > 0)
459 /* Tried to send two auth identities, wtf */
460 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
461 return send_rejected (auth);
465 /* this is our auth identity */
466 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
471 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
473 _dbus_verbose ("Did not get a valid username from client\n");
474 return send_rejected (auth);
477 if (!_dbus_string_init (&tmp))
480 if (!_dbus_string_init (&tmp2))
482 _dbus_string_free (&tmp);
486 old_len = _dbus_string_get_length (&auth->outgoing);
488 /* we cache the keyring for speed, so here we drop it if it's the
489 * wrong one. FIXME caching the keyring here is useless since we use
490 * a different DBusAuth for every connection.
493 !_dbus_keyring_is_for_user (auth->keyring,
496 _dbus_keyring_unref (auth->keyring);
497 auth->keyring = NULL;
500 if (auth->keyring == NULL)
504 dbus_error_init (&error);
505 auth->keyring = _dbus_keyring_new_homedir (data,
509 if (auth->keyring == NULL)
511 if (dbus_error_has_name (&error,
512 DBUS_ERROR_NO_MEMORY))
514 dbus_error_free (&error);
519 _DBUS_ASSERT_ERROR_IS_SET (&error);
520 _dbus_verbose ("Error loading keyring: %s\n",
522 if (send_rejected (auth))
523 retval = TRUE; /* retval is only about mem */
524 dbus_error_free (&error);
530 _dbus_assert (!dbus_error_is_set (&error));
534 _dbus_assert (auth->keyring != NULL);
536 dbus_error_init (&error);
537 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
538 if (auth->cookie_id < 0)
540 _DBUS_ASSERT_ERROR_IS_SET (&error);
541 _dbus_verbose ("Could not get a cookie ID to send to client: %s\n",
543 if (send_rejected (auth))
545 dbus_error_free (&error);
550 _dbus_assert (!dbus_error_is_set (&error));
553 if (!_dbus_string_copy (&auth->context, 0,
554 &tmp2, _dbus_string_get_length (&tmp2)))
557 if (!_dbus_string_append (&tmp2, " "))
560 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
563 if (!_dbus_string_append (&tmp2, " "))
566 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
569 _dbus_string_set_length (&auth->challenge, 0);
570 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
573 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
574 _dbus_string_get_length (&tmp2)))
577 if (!_dbus_string_append (&auth->outgoing,
581 if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
582 _dbus_string_get_length (&auth->outgoing)))
585 if (!_dbus_string_append (&auth->outgoing,
592 _dbus_string_zero (&tmp);
593 _dbus_string_free (&tmp);
594 _dbus_string_zero (&tmp2);
595 _dbus_string_free (&tmp2);
597 _dbus_string_set_length (&auth->outgoing, old_len);
602 sha1_handle_second_client_response (DBusAuth *auth,
603 const DBusString *data)
605 /* We are expecting a response which is the hex-encoded client
606 * challenge, space, then SHA-1 hash of the concatenation of our
607 * challenge, ":", client challenge, ":", secret key, all
611 DBusString client_challenge;
612 DBusString client_hash;
614 DBusString correct_hash;
618 if (!_dbus_string_find_blank (data, 0, &i))
620 _dbus_verbose ("no space separator in client response\n");
621 return send_rejected (auth);
624 if (!_dbus_string_init (&client_challenge))
627 if (!_dbus_string_init (&client_hash))
630 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
634 _dbus_string_skip_blank (data, i, &i);
636 if (!_dbus_string_copy_len (data, i,
637 _dbus_string_get_length (data) - i,
642 if (_dbus_string_get_length (&client_challenge) == 0 ||
643 _dbus_string_get_length (&client_hash) == 0)
645 _dbus_verbose ("zero-length client challenge or hash\n");
646 if (send_rejected (auth))
651 if (!_dbus_string_init (&correct_hash))
654 if (!sha1_compute_hash (auth, auth->cookie_id,
660 /* if cookie_id was invalid, then we get an empty hash */
661 if (_dbus_string_get_length (&correct_hash) == 0)
663 if (send_rejected (auth))
668 if (!_dbus_string_equal (&client_hash, &correct_hash))
670 if (send_rejected (auth))
675 if (!_dbus_string_append (&auth->outgoing,
679 _dbus_verbose ("authenticated client with UID %d using DBUS_COOKIE_SHA1\n",
680 auth->desired_identity.uid);
682 auth->authorized_identity = auth->desired_identity;
683 auth->authenticated_pending_begin = TRUE;
687 _dbus_string_zero (&correct_hash);
688 _dbus_string_free (&correct_hash);
690 _dbus_string_zero (&client_hash);
691 _dbus_string_free (&client_hash);
693 _dbus_string_free (&client_challenge);
699 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
700 const DBusString *data)
702 if (auth->cookie_id < 0)
703 return sha1_handle_first_client_response (auth, data);
705 return sha1_handle_second_client_response (auth, data);
709 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
711 auth->cookie_id = -1;
712 _dbus_string_set_length (&auth->challenge, 0);
716 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
717 DBusString *response)
719 const DBusString *username;
724 if (!_dbus_user_info_from_current_process (&username,
728 if (!_dbus_string_base64_encode (username, 0,
730 _dbus_string_get_length (response)))
739 /* FIXME if we send the server an error, right now both sides
740 * just hang. Server has to reject on getting an error, or
741 * client has to cancel. Should be in the spec.
744 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
745 const DBusString *data)
747 /* The data we get from the server should be the cookie context
748 * name, the cookie ID, and the server challenge, separated by
749 * spaces. We send back our challenge string and the correct hash.
753 DBusString cookie_id_str;
754 DBusString server_challenge;
755 DBusString client_challenge;
756 DBusString correct_hash;
764 if (!_dbus_string_find_blank (data, 0, &i))
766 if (_dbus_string_append (&auth->outgoing,
767 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
772 if (!_dbus_string_init (&context))
775 if (!_dbus_string_copy_len (data, 0, i,
779 _dbus_string_skip_blank (data, i, &i);
780 if (!_dbus_string_find_blank (data, i, &j))
782 if (_dbus_string_append (&auth->outgoing,
783 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
788 if (!_dbus_string_init (&cookie_id_str))
791 if (!_dbus_string_copy_len (data, i, j - i,
795 if (!_dbus_string_init (&server_challenge))
799 _dbus_string_skip_blank (data, i, &i);
800 j = _dbus_string_get_length (data);
802 if (!_dbus_string_copy_len (data, i, j - i,
803 &server_challenge, 0))
806 if (!_dbus_keyring_validate_context (&context))
808 if (_dbus_string_append (&auth->outgoing,
809 "ERROR \"Server sent invalid cookie context\"\r\n"))
814 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
816 if (_dbus_string_append (&auth->outgoing,
817 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
822 if (_dbus_string_get_length (&server_challenge) == 0)
824 if (_dbus_string_append (&auth->outgoing,
825 "ERROR \"Empty server challenge string\"\r\n"))
830 if (auth->keyring == NULL)
834 dbus_error_init (&error);
835 auth->keyring = _dbus_keyring_new_homedir (NULL,
839 if (auth->keyring == NULL)
841 if (dbus_error_has_name (&error,
842 DBUS_ERROR_NO_MEMORY))
844 dbus_error_free (&error);
849 _DBUS_ASSERT_ERROR_IS_SET (&error);
851 _dbus_verbose ("Error loading keyring: %s\n",
854 if (_dbus_string_append (&auth->outgoing,
855 "ERROR \"Could not load cookie file\"\r\n"))
856 retval = TRUE; /* retval is only about mem */
858 dbus_error_free (&error);
864 _dbus_assert (!dbus_error_is_set (&error));
868 _dbus_assert (auth->keyring != NULL);
870 if (!_dbus_string_init (&tmp))
873 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
876 if (!_dbus_string_init (&client_challenge))
879 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
882 if (!_dbus_string_init (&correct_hash))
885 if (!sha1_compute_hash (auth, val,
891 if (_dbus_string_get_length (&correct_hash) == 0)
893 /* couldn't find the cookie ID or something */
894 if (_dbus_string_append (&auth->outgoing,
895 "ERROR \"Don't have the requested cookie ID\"\r\n"))
900 _dbus_string_set_length (&tmp, 0);
902 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
903 _dbus_string_get_length (&tmp)))
906 if (!_dbus_string_append (&tmp, " "))
909 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
910 _dbus_string_get_length (&tmp)))
913 old_len = _dbus_string_get_length (&auth->outgoing);
914 if (!_dbus_string_append (&auth->outgoing, "DATA "))
917 if (!_dbus_string_base64_encode (&tmp, 0,
919 _dbus_string_get_length (&auth->outgoing)))
921 _dbus_string_set_length (&auth->outgoing, old_len);
925 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
927 _dbus_string_set_length (&auth->outgoing, old_len);
934 _dbus_string_zero (&correct_hash);
935 _dbus_string_free (&correct_hash);
937 _dbus_string_free (&client_challenge);
939 _dbus_string_zero (&tmp);
940 _dbus_string_free (&tmp);
942 _dbus_string_free (&server_challenge);
944 _dbus_string_free (&cookie_id_str);
946 _dbus_string_free (&context);
952 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
954 auth->cookie_id = -1;
955 _dbus_string_set_length (&auth->challenge, 0);
959 handle_server_data_external_mech (DBusAuth *auth,
960 const DBusString *data)
962 if (auth->credentials.uid < 0)
964 _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
965 return send_rejected (auth);
968 if (_dbus_string_get_length (data) > 0)
970 if (_dbus_string_get_length (&auth->identity) > 0)
972 /* Tried to send two auth identities, wtf */
973 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
974 return send_rejected (auth);
978 /* this is our auth identity */
979 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
984 /* Poke client for an auth identity, if none given */
985 if (_dbus_string_get_length (&auth->identity) == 0 &&
986 !auth->already_asked_for_initial_response)
988 if (_dbus_string_append (&auth->outgoing,
991 _dbus_verbose ("sending empty challenge asking client for auth identity\n");
992 auth->already_asked_for_initial_response = TRUE;
999 auth->desired_identity.pid = -1;
1000 auth->desired_identity.uid = -1;
1001 auth->desired_identity.gid = -1;
1003 /* If auth->identity is still empty here, then client
1004 * responded with an empty string after we poked it for
1005 * an initial response. This means to try to auth the
1006 * identity provided in the credentials.
1008 if (_dbus_string_get_length (&auth->identity) == 0)
1010 auth->desired_identity.uid = auth->credentials.uid;
1014 if (!_dbus_credentials_from_uid_string (&auth->identity,
1015 &auth->desired_identity))
1017 _dbus_verbose ("could not get credentials from uid string\n");
1018 return send_rejected (auth);
1022 if (auth->desired_identity.uid < 0)
1024 _dbus_verbose ("desired UID %d is no good\n", auth->desired_identity.uid);
1025 return send_rejected (auth);
1028 if (_dbus_credentials_match (&auth->desired_identity,
1029 &auth->credentials))
1031 /* client has authenticated */
1032 if (!_dbus_string_append (&auth->outgoing,
1036 _dbus_verbose ("authenticated client with UID %d matching socket credentials UID %d\n",
1037 auth->desired_identity.uid,
1038 auth->credentials.uid);
1040 auth->authorized_identity.uid = auth->desired_identity.uid;
1042 auth->authenticated_pending_begin = TRUE;
1048 _dbus_verbose ("credentials uid=%d gid=%d do not allow uid=%d gid=%d\n",
1049 auth->credentials.uid, auth->credentials.gid,
1050 auth->desired_identity.uid, auth->desired_identity.gid);
1051 return send_rejected (auth);
1056 handle_server_shutdown_external_mech (DBusAuth *auth)
1062 handle_client_initial_response_external_mech (DBusAuth *auth,
1063 DBusString *response)
1065 /* We always append our UID as an initial response, so the server
1066 * doesn't have to send back an empty challenge to check whether we
1067 * want to specify an identity. i.e. this avoids a round trip that
1068 * the spec for the EXTERNAL mechanism otherwise requires.
1070 DBusString plaintext;
1072 if (!_dbus_string_init (&plaintext))
1075 if (!_dbus_string_append_our_uid (&plaintext))
1078 if (!_dbus_string_base64_encode (&plaintext, 0,
1080 _dbus_string_get_length (response)))
1083 _dbus_string_free (&plaintext);
1088 _dbus_string_free (&plaintext);
1093 handle_client_data_external_mech (DBusAuth *auth,
1094 const DBusString *data)
1101 handle_client_shutdown_external_mech (DBusAuth *auth)
1106 /* Put mechanisms here in order of preference.
1107 * What I eventually want to have is:
1109 * - a mechanism that checks UNIX domain socket credentials
1110 * - a simple magic cookie mechanism like X11 or ICE
1111 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1112 * offers such as Kerberos, X509, whatever.
1115 static const DBusAuthMechanismHandler
1116 all_mechanisms[] = {
1118 handle_server_data_external_mech,
1120 handle_server_shutdown_external_mech,
1121 handle_client_initial_response_external_mech,
1122 handle_client_data_external_mech,
1124 handle_client_shutdown_external_mech },
1125 { "DBUS_COOKIE_SHA1",
1126 handle_server_data_cookie_sha1_mech,
1128 handle_server_shutdown_cookie_sha1_mech,
1129 handle_client_initial_response_cookie_sha1_mech,
1130 handle_client_data_cookie_sha1_mech,
1132 handle_client_shutdown_cookie_sha1_mech },
1136 static const DBusAuthMechanismHandler*
1137 find_mech (const DBusString *name)
1142 while (all_mechanisms[i].mechanism != NULL)
1144 if (_dbus_string_equal_c_str (name,
1145 all_mechanisms[i].mechanism))
1147 return &all_mechanisms[i];
1156 send_rejected (DBusAuth *auth)
1159 DBusAuthServer *server_auth;
1162 if (!_dbus_string_init (&command))
1165 if (!_dbus_string_append (&command,
1170 while (all_mechanisms[i].mechanism != NULL)
1172 if (!_dbus_string_append (&command,
1176 if (!_dbus_string_append (&command,
1177 all_mechanisms[i].mechanism))
1183 if (!_dbus_string_append (&command, "\r\n"))
1186 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1187 _dbus_string_get_length (&auth->outgoing)))
1190 shutdown_mech (auth);
1192 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1193 server_auth = DBUS_AUTH_SERVER (auth);
1194 server_auth->failures += 1;
1196 _dbus_string_free (&command);
1201 _dbus_string_free (&command);
1206 process_auth (DBusAuth *auth,
1207 const DBusString *command,
1208 const DBusString *args)
1212 /* We are already using a mechanism, client is on crack */
1213 if (!_dbus_string_append (&auth->outgoing,
1214 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1219 else if (_dbus_string_get_length (args) == 0)
1221 /* No args to the auth, send mechanisms */
1222 if (!send_rejected (auth))
1231 DBusString base64_response;
1232 DBusString decoded_response;
1234 _dbus_string_find_blank (args, 0, &i);
1236 if (!_dbus_string_init (&mech))
1239 if (!_dbus_string_init (&base64_response))
1241 _dbus_string_free (&mech);
1245 if (!_dbus_string_init (&decoded_response))
1247 _dbus_string_free (&mech);
1248 _dbus_string_free (&base64_response);
1252 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1255 if (!_dbus_string_copy (args, i, &base64_response, 0))
1258 if (!_dbus_string_base64_decode (&base64_response, 0,
1259 &decoded_response, 0))
1262 auth->mech = find_mech (&mech);
1263 if (auth->mech != NULL)
1265 _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
1266 auth->mech->mechanism,
1267 _dbus_string_get_length (&decoded_response));
1269 if (!(* auth->mech->server_data_func) (auth,
1275 /* Unsupported mechanism */
1276 if (!send_rejected (auth))
1280 _dbus_string_free (&mech);
1281 _dbus_string_free (&base64_response);
1282 _dbus_string_free (&decoded_response);
1288 _dbus_string_free (&mech);
1289 _dbus_string_free (&base64_response);
1290 _dbus_string_free (&decoded_response);
1296 process_cancel (DBusAuth *auth,
1297 const DBusString *command,
1298 const DBusString *args)
1300 shutdown_mech (auth);
1306 process_begin (DBusAuth *auth,
1307 const DBusString *command,
1308 const DBusString *args)
1310 if (auth->authenticated_pending_begin)
1311 auth->authenticated = TRUE;
1314 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1317 shutdown_mech (auth);
1324 process_data_server (DBusAuth *auth,
1325 const DBusString *command,
1326 const DBusString *args)
1328 if (auth->mech != NULL)
1332 if (!_dbus_string_init (&decoded))
1335 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1337 _dbus_string_free (&decoded);
1341 #ifdef DBUS_ENABLE_VERBOSE_MODE
1342 if (_dbus_string_validate_ascii (&decoded, 0,
1343 _dbus_string_get_length (&decoded)))
1344 _dbus_verbose ("data: '%s'\n", _dbus_string_get_const_data (&decoded));
1347 if (!(* auth->mech->server_data_func) (auth, &decoded))
1349 _dbus_string_free (&decoded);
1353 _dbus_string_free (&decoded);
1357 if (!_dbus_string_append (&auth->outgoing,
1358 "ERROR \"Not currently in an auth conversation\"\r\n"))
1366 process_error_server (DBusAuth *auth,
1367 const DBusString *command,
1368 const DBusString *args)
1374 /* return FALSE if no memory, TRUE if all OK */
1376 get_word (const DBusString *str,
1382 _dbus_string_skip_blank (str, *start, start);
1383 _dbus_string_find_blank (str, *start, &i);
1387 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1397 record_mechanisms (DBusAuth *auth,
1398 const DBusString *command,
1399 const DBusString *args)
1404 if (auth->already_got_mechanisms)
1407 len = _dbus_string_get_length (args);
1413 const DBusAuthMechanismHandler *mech;
1415 if (!_dbus_string_init (&m))
1418 if (!get_word (args, &next, &m))
1421 mech = find_mech (&m);
1425 /* FIXME right now we try mechanisms in the order
1426 * the server lists them; should we do them in
1427 * some more deterministic order?
1429 * Probably in all_mechanisms order, our order of
1430 * preference. Of course when the server is us,
1431 * it lists things in that order anyhow.
1434 _dbus_verbose ("Adding mechanism %s to list we will try\n",
1437 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1443 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1444 _dbus_string_get_const_data (&m));
1447 _dbus_string_free (&m);
1450 auth->already_got_mechanisms = TRUE;
1455 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1461 client_try_next_mechanism (DBusAuth *auth)
1463 const DBusAuthMechanismHandler *mech;
1464 DBusString auth_command;
1466 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1469 mech = DBUS_AUTH_CLIENT (auth)->mechs_to_try->data;
1471 if (!_dbus_string_init (&auth_command))
1474 if (!_dbus_string_append (&auth_command,
1477 _dbus_string_free (&auth_command);
1481 if (!_dbus_string_append (&auth_command,
1484 _dbus_string_free (&auth_command);
1488 if (mech->client_initial_response_func != NULL)
1490 if (!_dbus_string_append (&auth_command, " "))
1492 _dbus_string_free (&auth_command);
1496 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1498 _dbus_string_free (&auth_command);
1503 if (!_dbus_string_append (&auth_command,
1506 _dbus_string_free (&auth_command);
1510 if (!_dbus_string_copy (&auth_command, 0,
1512 _dbus_string_get_length (&auth->outgoing)))
1514 _dbus_string_free (&auth_command);
1519 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1521 _dbus_verbose ("Trying mechanism %s\n",
1522 auth->mech->mechanism);
1524 _dbus_string_free (&auth_command);
1530 process_rejected (DBusAuth *auth,
1531 const DBusString *command,
1532 const DBusString *args)
1534 shutdown_mech (auth);
1536 if (!auth->already_got_mechanisms)
1538 if (!record_mechanisms (auth, command, args))
1542 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1544 client_try_next_mechanism (auth);
1549 auth->need_disconnect = TRUE;
1556 process_ok (DBusAuth *auth,
1557 const DBusString *command,
1558 const DBusString *args)
1560 if (!_dbus_string_append (&auth->outgoing,
1564 auth->authenticated_pending_output = TRUE;
1570 process_data_client (DBusAuth *auth,
1571 const DBusString *command,
1572 const DBusString *args)
1574 if (auth->mech != NULL)
1578 if (!_dbus_string_init (&decoded))
1581 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1583 _dbus_string_free (&decoded);
1587 #ifdef DBUS_ENABLE_VERBOSE_MODE
1588 if (_dbus_string_validate_ascii (&decoded, 0,
1589 _dbus_string_get_length (&decoded)))
1591 _dbus_verbose ("data: '%s'\n",
1592 _dbus_string_get_const_data (&decoded));
1596 if (!(* auth->mech->client_data_func) (auth, &decoded))
1598 _dbus_string_free (&decoded);
1602 _dbus_string_free (&decoded);
1606 if (!_dbus_string_append (&auth->outgoing,
1607 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1615 process_error_client (DBusAuth *auth,
1616 const DBusString *command,
1617 const DBusString *args)
1623 process_unknown (DBusAuth *auth,
1624 const DBusString *command,
1625 const DBusString *args)
1627 if (!_dbus_string_append (&auth->outgoing,
1628 "ERROR \"Unknown command\"\r\n"))
1634 /* returns whether to call it again right away */
1636 process_command (DBusAuth *auth)
1644 /* _dbus_verbose (" trying process_command()\n"); */
1649 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1652 if (!_dbus_string_init (&command))
1654 auth->needed_memory = TRUE;
1658 if (!_dbus_string_init (&args))
1660 _dbus_string_free (&command);
1661 auth->needed_memory = TRUE;
1665 if (eol > _DBUS_ONE_MEGABYTE)
1667 /* This is a giant line, someone is trying to hose us. */
1668 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1674 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1677 if (!_dbus_string_validate_ascii (&command, 0,
1678 _dbus_string_get_length (&command)))
1680 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1681 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1687 _dbus_verbose ("got command \"%s\"\n", _dbus_string_get_const_data (&command));
1689 _dbus_string_find_blank (&command, 0, &i);
1690 _dbus_string_skip_blank (&command, i, &j);
1693 _dbus_string_delete (&command, i, j - i);
1695 if (!_dbus_string_move (&command, i, &args, 0))
1699 while (auth->handlers[i].command != NULL)
1701 if (_dbus_string_equal_c_str (&command,
1702 auth->handlers[i].command))
1704 _dbus_verbose ("Processing auth command %s\n",
1705 auth->handlers[i].command);
1707 if (!(* auth->handlers[i].func) (auth, &command, &args))
1715 if (auth->handlers[i].command == NULL)
1717 if (!process_unknown (auth, &command, &args))
1723 /* We've succeeded in processing the whole command so drop it out
1724 * of the incoming buffer and return TRUE to try another command.
1727 _dbus_string_delete (&auth->incoming, 0, eol);
1730 _dbus_string_delete (&auth->incoming, 0, 2);
1735 _dbus_string_free (&args);
1736 _dbus_string_free (&command);
1739 auth->needed_memory = TRUE;
1741 auth->needed_memory = FALSE;
1750 * @addtogroup DBusAuth
1755 * Creates a new auth conversation object for the server side.
1756 * See doc/dbus-sasl-profile.txt for full details on what
1759 * @returns the new object or #NULL if no memory
1762 _dbus_auth_server_new (void)
1765 DBusAuthServer *server_auth;
1767 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1771 auth->handlers = server_handlers;
1773 server_auth = DBUS_AUTH_SERVER (auth);
1775 /* perhaps this should be per-mechanism with a lower
1778 server_auth->failures = 0;
1779 server_auth->max_failures = 6;
1785 * Creates a new auth conversation object for the client side.
1786 * See doc/dbus-sasl-profile.txt for full details on what
1789 * @returns the new object or #NULL if no memory
1792 _dbus_auth_client_new (void)
1796 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1800 auth->handlers = client_handlers;
1802 /* Add a default mechanism to try */
1803 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1804 (void*) &all_mechanisms[0]))
1806 _dbus_auth_unref (auth);
1810 /* Now try the mechanism we just added */
1811 if (!client_try_next_mechanism (auth))
1813 _dbus_auth_unref (auth);
1821 * Increments the refcount of an auth object.
1823 * @param auth the auth conversation
1826 _dbus_auth_ref (DBusAuth *auth)
1828 _dbus_assert (auth != NULL);
1830 auth->refcount += 1;
1834 * Decrements the refcount of an auth object.
1836 * @param auth the auth conversation
1839 _dbus_auth_unref (DBusAuth *auth)
1841 _dbus_assert (auth != NULL);
1842 _dbus_assert (auth->refcount > 0);
1844 auth->refcount -= 1;
1845 if (auth->refcount == 0)
1847 shutdown_mech (auth);
1849 if (DBUS_AUTH_IS_CLIENT (auth))
1851 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1855 _dbus_keyring_unref (auth->keyring);
1857 _dbus_string_free (&auth->context);
1858 _dbus_string_free (&auth->challenge);
1859 _dbus_string_free (&auth->identity);
1860 _dbus_string_free (&auth->incoming);
1861 _dbus_string_free (&auth->outgoing);
1867 * @param auth the auth conversation object
1868 * @returns #TRUE if we're in a final state
1870 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1873 * Analyzes buffered input and moves the auth conversation forward,
1874 * returning the new state of the auth conversation.
1876 * @param auth the auth conversation
1877 * @returns the new state
1880 _dbus_auth_do_work (DBusAuth *auth)
1882 auth->needed_memory = FALSE;
1884 /* Max amount we'll buffer up before deciding someone's on crack */
1885 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1889 if (DBUS_AUTH_IN_END_STATE (auth))
1892 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1893 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1895 auth->need_disconnect = TRUE;
1896 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1900 if (auth->mech == NULL &&
1901 auth->already_got_mechanisms &&
1902 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1904 auth->need_disconnect = TRUE;
1905 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1909 while (process_command (auth));
1911 if (DBUS_AUTH_IS_SERVER (auth) &&
1912 DBUS_AUTH_SERVER (auth)->failures >=
1913 DBUS_AUTH_SERVER (auth)->max_failures)
1914 auth->need_disconnect = TRUE;
1916 if (auth->need_disconnect)
1917 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1918 else if (auth->authenticated)
1920 if (_dbus_string_get_length (&auth->incoming) > 0)
1921 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1923 return DBUS_AUTH_STATE_AUTHENTICATED;
1925 else if (auth->needed_memory)
1926 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1927 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1928 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1930 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1934 * Gets bytes that need to be sent to the peer we're conversing with.
1935 * After writing some bytes, _dbus_auth_bytes_sent() must be called
1936 * to notify the auth object that they were written.
1938 * @param auth the auth conversation
1939 * @param str return location for a ref to the buffer to send
1940 * @returns #FALSE if nothing to send
1943 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
1944 const DBusString **str)
1946 _dbus_assert (auth != NULL);
1947 _dbus_assert (str != NULL);
1951 if (DBUS_AUTH_IN_END_STATE (auth))
1954 if (_dbus_string_get_length (&auth->outgoing) == 0)
1957 *str = &auth->outgoing;
1963 * Notifies the auth conversation object that
1964 * the given number of bytes of the outgoing buffer
1965 * have been written out.
1967 * @param auth the auth conversation
1968 * @param bytes_sent number of bytes written out
1971 _dbus_auth_bytes_sent (DBusAuth *auth,
1974 _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent,
1975 _dbus_string_get_const_data (&auth->outgoing));
1977 _dbus_string_delete (&auth->outgoing,
1980 if (auth->authenticated_pending_output &&
1981 _dbus_string_get_length (&auth->outgoing) == 0)
1982 auth->authenticated = TRUE;
1986 * Get a buffer to be used for reading bytes from the peer we're conversing
1987 * with. Bytes should be appended to this buffer.
1989 * @param auth the auth conversation
1990 * @param buffer return location for buffer to append bytes to
1993 _dbus_auth_get_buffer (DBusAuth *auth,
1994 DBusString **buffer)
1996 _dbus_assert (auth != NULL);
1997 _dbus_assert (!auth->buffer_outstanding);
1999 *buffer = &auth->incoming;
2001 auth->buffer_outstanding = TRUE;
2005 * Returns a buffer with new data read into it.
2007 * @param auth the auth conversation
2008 * @param buffer the buffer being returned
2009 * @param bytes_read number of new bytes added
2012 _dbus_auth_return_buffer (DBusAuth *auth,
2016 _dbus_assert (buffer == &auth->incoming);
2017 _dbus_assert (auth->buffer_outstanding);
2019 auth->buffer_outstanding = FALSE;
2023 * Returns leftover bytes that were not used as part of the auth
2024 * conversation. These bytes will be part of the message stream
2025 * instead. This function may not be called until authentication has
2028 * @param auth the auth conversation
2029 * @param str return location for pointer to string of unused bytes
2032 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2033 const DBusString **str)
2035 if (!DBUS_AUTH_IN_END_STATE (auth))
2038 *str = &auth->incoming;
2043 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2044 * after we've gotten them and successfully moved them elsewhere.
2046 * @param auth the auth conversation
2049 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2051 if (!DBUS_AUTH_IN_END_STATE (auth))
2054 _dbus_string_set_length (&auth->incoming, 0);
2058 * Called post-authentication, indicates whether we need to encode
2059 * the message stream with _dbus_auth_encode_data() prior to
2060 * sending it to the peer.
2062 * @param auth the auth conversation
2063 * @returns #TRUE if we need to encode the stream
2066 _dbus_auth_needs_encoding (DBusAuth *auth)
2068 if (!auth->authenticated)
2071 if (auth->mech != NULL)
2073 if (DBUS_AUTH_IS_CLIENT (auth))
2074 return auth->mech->client_encode_func != NULL;
2076 return auth->mech->server_encode_func != NULL;
2083 * Called post-authentication, encodes a block of bytes for sending to
2084 * the peer. If no encoding was negotiated, just copies the bytes
2085 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2087 * @param auth the auth conversation
2088 * @param plaintext the plain text data
2089 * @param encoded initialized string to where encoded data is appended
2090 * @returns #TRUE if we had enough memory and successfully encoded
2093 _dbus_auth_encode_data (DBusAuth *auth,
2094 const DBusString *plaintext,
2095 DBusString *encoded)
2097 _dbus_assert (plaintext != encoded);
2099 if (!auth->authenticated)
2102 if (_dbus_auth_needs_encoding (auth))
2104 if (DBUS_AUTH_IS_CLIENT (auth))
2105 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2107 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2111 return _dbus_string_copy (plaintext, 0, encoded,
2112 _dbus_string_get_length (encoded));
2117 * Called post-authentication, indicates whether we need to decode
2118 * the message stream with _dbus_auth_decode_data() after
2119 * receiving it from the peer.
2121 * @param auth the auth conversation
2122 * @returns #TRUE if we need to encode the stream
2125 _dbus_auth_needs_decoding (DBusAuth *auth)
2127 if (!auth->authenticated)
2130 if (auth->mech != NULL)
2132 if (DBUS_AUTH_IS_CLIENT (auth))
2133 return auth->mech->client_decode_func != NULL;
2135 return auth->mech->server_decode_func != NULL;
2143 * Called post-authentication, decodes a block of bytes received from
2144 * the peer. If no encoding was negotiated, just copies the bytes (you
2145 * can avoid this by checking _dbus_auth_needs_decoding()).
2147 * @todo We need to be able to distinguish "out of memory" error
2148 * from "the data is hosed" error.
2150 * @param auth the auth conversation
2151 * @param encoded the encoded data
2152 * @param plaintext initialized string where decoded data is appended
2153 * @returns #TRUE if we had enough memory and successfully decoded
2156 _dbus_auth_decode_data (DBusAuth *auth,
2157 const DBusString *encoded,
2158 DBusString *plaintext)
2160 _dbus_assert (plaintext != encoded);
2162 if (!auth->authenticated)
2165 if (_dbus_auth_needs_decoding (auth))
2167 if (DBUS_AUTH_IS_CLIENT (auth))
2168 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2170 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2174 return _dbus_string_copy (encoded, 0, plaintext,
2175 _dbus_string_get_length (plaintext));
2180 * Sets credentials received via reliable means from the operating
2183 * @param auth the auth conversation
2184 * @param credentials the credentials received
2187 _dbus_auth_set_credentials (DBusAuth *auth,
2188 const DBusCredentials *credentials)
2190 auth->credentials = *credentials;
2194 * Gets the identity we authorized the client as. Apps may have
2195 * different policies as to what identities they allow.
2197 * @param auth the auth conversation
2198 * @param credentials the credentials we've authorized
2201 _dbus_auth_get_identity (DBusAuth *auth,
2202 DBusCredentials *credentials)
2204 if (auth->authenticated)
2206 *credentials = auth->authorized_identity;
2210 credentials->pid = -1;
2211 credentials->uid = -1;
2212 credentials->gid = -1;
2217 * Sets the "authentication context" which scopes cookies
2218 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2220 * @param auth the auth conversation
2221 * @param context the context
2222 * @returns #FALSE if no memory
2225 _dbus_auth_set_context (DBusAuth *auth,
2226 const DBusString *context)
2228 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2229 &auth->context, 0, _dbus_string_get_length (context));
2234 #ifdef DBUS_BUILD_TESTS
2235 #include "dbus-test.h"
2236 #include "dbus-auth-script.h"
2240 process_test_subdir (const DBusString *test_base_dir,
2243 DBusString test_directory;
2244 DBusString filename;
2252 if (!_dbus_string_init (&test_directory))
2253 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2255 _dbus_string_init_const (&filename, subdir);
2257 if (!_dbus_string_copy (test_base_dir, 0,
2258 &test_directory, 0))
2259 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2261 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2262 _dbus_assert_not_reached ("couldn't allocate full path");
2264 _dbus_string_free (&filename);
2265 if (!_dbus_string_init (&filename))
2266 _dbus_assert_not_reached ("didn't allocate filename string\n");
2268 dbus_error_init (&error);
2269 dir = _dbus_directory_open (&test_directory, &error);
2272 _dbus_warn ("Could not open %s: %s\n",
2273 _dbus_string_get_const_data (&test_directory),
2275 dbus_error_free (&error);
2279 printf ("Testing:\n");
2282 while (_dbus_directory_get_next_file (dir, &filename, &error))
2284 DBusString full_path;
2286 if (!_dbus_string_init (&full_path))
2287 _dbus_assert_not_reached ("couldn't init string");
2289 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2290 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2292 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2293 _dbus_assert_not_reached ("couldn't concat file to dir");
2295 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2297 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2298 _dbus_string_get_const_data (&filename));
2299 _dbus_string_free (&full_path);
2303 printf (" %s\n", _dbus_string_get_const_data (&filename));
2305 if (!_dbus_auth_script_run (&full_path))
2307 _dbus_string_free (&full_path);
2311 _dbus_string_free (&full_path);
2314 if (dbus_error_is_set (&error))
2316 _dbus_warn ("Could not get next file in %s: %s\n",
2317 _dbus_string_get_const_data (&test_directory), error.message);
2318 dbus_error_free (&error);
2327 _dbus_directory_close (dir);
2328 _dbus_string_free (&test_directory);
2329 _dbus_string_free (&filename);
2335 process_test_dirs (const char *test_data_dir)
2337 DBusString test_directory;
2342 _dbus_string_init_const (&test_directory, test_data_dir);
2344 if (!process_test_subdir (&test_directory, "auth"))
2351 _dbus_string_free (&test_directory);
2357 _dbus_auth_test (const char *test_data_dir)
2360 if (test_data_dir == NULL)
2363 if (!process_test_dirs (test_data_dir))
2369 #endif /* DBUS_BUILD_TESTS */