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, _DBUS_INT_MAX))
301 if (!_dbus_string_init (&auth->outgoing, _DBUS_INT_MAX))
304 if (!_dbus_string_init (&auth->identity, _DBUS_INT_MAX))
307 if (!_dbus_string_init (&auth->context, _DBUS_INT_MAX))
310 if (!_dbus_string_init (&auth->challenge, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
480 if (!_dbus_string_init (&tmp2, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
627 if (!_dbus_string_init (&client_hash, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
791 if (!_dbus_string_copy_len (data, i, j - i,
795 if (!_dbus_string_init (&server_challenge, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
873 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
876 if (!_dbus_string_init (&client_challenge, _DBUS_INT_MAX))
879 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
882 if (!_dbus_string_init (&correct_hash, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
1239 if (!_dbus_string_init (&base64_response, _DBUS_INT_MAX))
1241 _dbus_string_free (&mech);
1245 if (!_dbus_string_init (&decoded_response, _DBUS_INT_MAX))
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, _DBUS_INT_MAX))
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)))
1346 _dbus_string_get_const_data (&decoded, &s);
1347 _dbus_verbose ("data: '%s'\n", s);
1351 if (!(* auth->mech->server_data_func) (auth, &decoded))
1353 _dbus_string_free (&decoded);
1357 _dbus_string_free (&decoded);
1361 if (!_dbus_string_append (&auth->outgoing,
1362 "ERROR \"Not currently in an auth conversation\"\r\n"))
1370 process_error_server (DBusAuth *auth,
1371 const DBusString *command,
1372 const DBusString *args)
1378 /* return FALSE if no memory, TRUE if all OK */
1380 get_word (const DBusString *str,
1386 _dbus_string_skip_blank (str, *start, start);
1387 _dbus_string_find_blank (str, *start, &i);
1391 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1401 record_mechanisms (DBusAuth *auth,
1402 const DBusString *command,
1403 const DBusString *args)
1408 if (auth->already_got_mechanisms)
1411 len = _dbus_string_get_length (args);
1417 const DBusAuthMechanismHandler *mech;
1419 if (!_dbus_string_init (&m, _DBUS_INT_MAX))
1422 if (!get_word (args, &next, &m))
1425 mech = find_mech (&m);
1429 /* FIXME right now we try mechanisms in the order
1430 * the server lists them; should we do them in
1431 * some more deterministic order?
1433 * Probably in all_mechanisms order, our order of
1434 * preference. Of course when the server is us,
1435 * it lists things in that order anyhow.
1438 _dbus_verbose ("Adding mechanism %s to list we will try\n",
1441 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1449 _dbus_string_get_const_data (&m, &s);
1450 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1454 _dbus_string_free (&m);
1457 auth->already_got_mechanisms = TRUE;
1462 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1468 client_try_next_mechanism (DBusAuth *auth)
1470 const DBusAuthMechanismHandler *mech;
1471 DBusString auth_command;
1473 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1476 mech = DBUS_AUTH_CLIENT (auth)->mechs_to_try->data;
1478 if (!_dbus_string_init (&auth_command, _DBUS_INT_MAX))
1481 if (!_dbus_string_append (&auth_command,
1484 _dbus_string_free (&auth_command);
1488 if (!_dbus_string_append (&auth_command,
1491 _dbus_string_free (&auth_command);
1495 if (mech->client_initial_response_func != NULL)
1497 if (!_dbus_string_append (&auth_command, " "))
1499 _dbus_string_free (&auth_command);
1503 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1505 _dbus_string_free (&auth_command);
1510 if (!_dbus_string_append (&auth_command,
1513 _dbus_string_free (&auth_command);
1517 if (!_dbus_string_copy (&auth_command, 0,
1519 _dbus_string_get_length (&auth->outgoing)))
1521 _dbus_string_free (&auth_command);
1526 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1528 _dbus_verbose ("Trying mechanism %s\n",
1529 auth->mech->mechanism);
1531 _dbus_string_free (&auth_command);
1537 process_rejected (DBusAuth *auth,
1538 const DBusString *command,
1539 const DBusString *args)
1541 shutdown_mech (auth);
1543 if (!auth->already_got_mechanisms)
1545 if (!record_mechanisms (auth, command, args))
1549 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1551 client_try_next_mechanism (auth);
1556 auth->need_disconnect = TRUE;
1563 process_ok (DBusAuth *auth,
1564 const DBusString *command,
1565 const DBusString *args)
1567 if (!_dbus_string_append (&auth->outgoing,
1571 auth->authenticated_pending_output = TRUE;
1577 process_data_client (DBusAuth *auth,
1578 const DBusString *command,
1579 const DBusString *args)
1581 if (auth->mech != NULL)
1585 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1588 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1590 _dbus_string_free (&decoded);
1594 #ifdef DBUS_ENABLE_VERBOSE_MODE
1595 if (_dbus_string_validate_ascii (&decoded, 0,
1596 _dbus_string_get_length (&decoded)))
1599 _dbus_string_get_const_data (&decoded, &s);
1600 _dbus_verbose ("data: '%s'\n", s);
1604 if (!(* auth->mech->client_data_func) (auth, &decoded))
1606 _dbus_string_free (&decoded);
1610 _dbus_string_free (&decoded);
1614 if (!_dbus_string_append (&auth->outgoing,
1615 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1623 process_error_client (DBusAuth *auth,
1624 const DBusString *command,
1625 const DBusString *args)
1631 process_unknown (DBusAuth *auth,
1632 const DBusString *command,
1633 const DBusString *args)
1635 if (!_dbus_string_append (&auth->outgoing,
1636 "ERROR \"Unknown command\"\r\n"))
1642 /* returns whether to call it again right away */
1644 process_command (DBusAuth *auth)
1652 /* _dbus_verbose (" trying process_command()\n"); */
1657 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1660 if (!_dbus_string_init (&command, _DBUS_INT_MAX))
1662 auth->needed_memory = TRUE;
1666 if (!_dbus_string_init (&args, _DBUS_INT_MAX))
1668 _dbus_string_free (&command);
1669 auth->needed_memory = TRUE;
1673 if (eol > _DBUS_ONE_MEGABYTE)
1675 /* This is a giant line, someone is trying to hose us. */
1676 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1682 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1685 if (!_dbus_string_validate_ascii (&command, 0,
1686 _dbus_string_get_length (&command)))
1688 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1689 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1697 _dbus_string_get_const_data (&command, &q);
1698 _dbus_verbose ("got command \"%s\"\n", q);
1701 _dbus_string_find_blank (&command, 0, &i);
1702 _dbus_string_skip_blank (&command, i, &j);
1705 _dbus_string_delete (&command, i, j - i);
1707 if (!_dbus_string_move (&command, i, &args, 0))
1711 while (auth->handlers[i].command != NULL)
1713 if (_dbus_string_equal_c_str (&command,
1714 auth->handlers[i].command))
1716 _dbus_verbose ("Processing auth command %s\n",
1717 auth->handlers[i].command);
1719 if (!(* auth->handlers[i].func) (auth, &command, &args))
1727 if (auth->handlers[i].command == NULL)
1729 if (!process_unknown (auth, &command, &args))
1735 /* We've succeeded in processing the whole command so drop it out
1736 * of the incoming buffer and return TRUE to try another command.
1739 _dbus_string_delete (&auth->incoming, 0, eol);
1742 _dbus_string_delete (&auth->incoming, 0, 2);
1747 _dbus_string_free (&args);
1748 _dbus_string_free (&command);
1751 auth->needed_memory = TRUE;
1753 auth->needed_memory = FALSE;
1762 * @addtogroup DBusAuth
1767 * Creates a new auth conversation object for the server side.
1768 * See doc/dbus-sasl-profile.txt for full details on what
1771 * @returns the new object or #NULL if no memory
1774 _dbus_auth_server_new (void)
1777 DBusAuthServer *server_auth;
1779 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1783 auth->handlers = server_handlers;
1785 server_auth = DBUS_AUTH_SERVER (auth);
1787 /* perhaps this should be per-mechanism with a lower
1790 server_auth->failures = 0;
1791 server_auth->max_failures = 6;
1797 * Creates a new auth conversation object for the client side.
1798 * See doc/dbus-sasl-profile.txt for full details on what
1801 * @returns the new object or #NULL if no memory
1804 _dbus_auth_client_new (void)
1808 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1812 auth->handlers = client_handlers;
1814 /* Add a default mechanism to try */
1815 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1816 (void*) &all_mechanisms[0]))
1818 _dbus_auth_unref (auth);
1822 /* Now try the mechanism we just added */
1823 if (!client_try_next_mechanism (auth))
1825 _dbus_auth_unref (auth);
1833 * Increments the refcount of an auth object.
1835 * @param auth the auth conversation
1838 _dbus_auth_ref (DBusAuth *auth)
1840 _dbus_assert (auth != NULL);
1842 auth->refcount += 1;
1846 * Decrements the refcount of an auth object.
1848 * @param auth the auth conversation
1851 _dbus_auth_unref (DBusAuth *auth)
1853 _dbus_assert (auth != NULL);
1854 _dbus_assert (auth->refcount > 0);
1856 auth->refcount -= 1;
1857 if (auth->refcount == 0)
1859 shutdown_mech (auth);
1861 if (DBUS_AUTH_IS_CLIENT (auth))
1863 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1867 _dbus_keyring_unref (auth->keyring);
1869 _dbus_string_free (&auth->context);
1870 _dbus_string_free (&auth->challenge);
1871 _dbus_string_free (&auth->identity);
1872 _dbus_string_free (&auth->incoming);
1873 _dbus_string_free (&auth->outgoing);
1879 * @param auth the auth conversation object
1880 * @returns #TRUE if we're in a final state
1882 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1885 * Analyzes buffered input and moves the auth conversation forward,
1886 * returning the new state of the auth conversation.
1888 * @param auth the auth conversation
1889 * @returns the new state
1892 _dbus_auth_do_work (DBusAuth *auth)
1894 auth->needed_memory = FALSE;
1896 /* Max amount we'll buffer up before deciding someone's on crack */
1897 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1901 if (DBUS_AUTH_IN_END_STATE (auth))
1904 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1905 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1907 auth->need_disconnect = TRUE;
1908 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1912 if (auth->mech == NULL &&
1913 auth->already_got_mechanisms &&
1914 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1916 auth->need_disconnect = TRUE;
1917 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1921 while (process_command (auth));
1923 if (DBUS_AUTH_IS_SERVER (auth) &&
1924 DBUS_AUTH_SERVER (auth)->failures >=
1925 DBUS_AUTH_SERVER (auth)->max_failures)
1926 auth->need_disconnect = TRUE;
1928 if (auth->need_disconnect)
1929 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1930 else if (auth->authenticated)
1932 if (_dbus_string_get_length (&auth->incoming) > 0)
1933 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1935 return DBUS_AUTH_STATE_AUTHENTICATED;
1937 else if (auth->needed_memory)
1938 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1939 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1940 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1942 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1946 * Gets bytes that need to be sent to the peer we're conversing with.
1947 * After writing some bytes, _dbus_auth_bytes_sent() must be called
1948 * to notify the auth object that they were written.
1950 * @param auth the auth conversation
1951 * @param str return location for a ref to the buffer to send
1952 * @returns #FALSE if nothing to send
1955 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
1956 const DBusString **str)
1958 _dbus_assert (auth != NULL);
1959 _dbus_assert (str != NULL);
1963 if (DBUS_AUTH_IN_END_STATE (auth))
1966 if (_dbus_string_get_length (&auth->outgoing) == 0)
1969 *str = &auth->outgoing;
1975 * Notifies the auth conversation object that
1976 * the given number of bytes of the outgoing buffer
1977 * have been written out.
1979 * @param auth the auth conversation
1980 * @param bytes_sent number of bytes written out
1983 _dbus_auth_bytes_sent (DBusAuth *auth,
1988 _dbus_string_get_const_data (&auth->outgoing, &s);
1989 _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent, s);
1992 _dbus_string_delete (&auth->outgoing,
1995 if (auth->authenticated_pending_output &&
1996 _dbus_string_get_length (&auth->outgoing) == 0)
1997 auth->authenticated = TRUE;
2001 * Get a buffer to be used for reading bytes from the peer we're conversing
2002 * with. Bytes should be appended to this buffer.
2004 * @param auth the auth conversation
2005 * @param buffer return location for buffer to append bytes to
2008 _dbus_auth_get_buffer (DBusAuth *auth,
2009 DBusString **buffer)
2011 _dbus_assert (auth != NULL);
2012 _dbus_assert (!auth->buffer_outstanding);
2014 *buffer = &auth->incoming;
2016 auth->buffer_outstanding = TRUE;
2020 * Returns a buffer with new data read into it.
2022 * @param auth the auth conversation
2023 * @param buffer the buffer being returned
2024 * @param bytes_read number of new bytes added
2027 _dbus_auth_return_buffer (DBusAuth *auth,
2031 _dbus_assert (buffer == &auth->incoming);
2032 _dbus_assert (auth->buffer_outstanding);
2034 auth->buffer_outstanding = FALSE;
2038 * Returns leftover bytes that were not used as part of the auth
2039 * conversation. These bytes will be part of the message stream
2040 * instead. This function may not be called until authentication has
2043 * @param auth the auth conversation
2044 * @param str return location for pointer to string of unused bytes
2047 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2048 const DBusString **str)
2050 if (!DBUS_AUTH_IN_END_STATE (auth))
2053 *str = &auth->incoming;
2058 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2059 * after we've gotten them and successfully moved them elsewhere.
2061 * @param auth the auth conversation
2064 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2066 if (!DBUS_AUTH_IN_END_STATE (auth))
2069 _dbus_string_set_length (&auth->incoming, 0);
2073 * Called post-authentication, indicates whether we need to encode
2074 * the message stream with _dbus_auth_encode_data() prior to
2075 * sending it to the peer.
2077 * @param auth the auth conversation
2078 * @returns #TRUE if we need to encode the stream
2081 _dbus_auth_needs_encoding (DBusAuth *auth)
2083 if (!auth->authenticated)
2086 if (auth->mech != NULL)
2088 if (DBUS_AUTH_IS_CLIENT (auth))
2089 return auth->mech->client_encode_func != NULL;
2091 return auth->mech->server_encode_func != NULL;
2098 * Called post-authentication, encodes a block of bytes for sending to
2099 * the peer. If no encoding was negotiated, just copies the bytes
2100 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2102 * @param auth the auth conversation
2103 * @param plaintext the plain text data
2104 * @param encoded initialized string to where encoded data is appended
2105 * @returns #TRUE if we had enough memory and successfully encoded
2108 _dbus_auth_encode_data (DBusAuth *auth,
2109 const DBusString *plaintext,
2110 DBusString *encoded)
2112 _dbus_assert (plaintext != encoded);
2114 if (!auth->authenticated)
2117 if (_dbus_auth_needs_encoding (auth))
2119 if (DBUS_AUTH_IS_CLIENT (auth))
2120 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2122 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2126 return _dbus_string_copy (plaintext, 0, encoded,
2127 _dbus_string_get_length (encoded));
2132 * Called post-authentication, indicates whether we need to decode
2133 * the message stream with _dbus_auth_decode_data() after
2134 * receiving it from the peer.
2136 * @param auth the auth conversation
2137 * @returns #TRUE if we need to encode the stream
2140 _dbus_auth_needs_decoding (DBusAuth *auth)
2142 if (!auth->authenticated)
2145 if (auth->mech != NULL)
2147 if (DBUS_AUTH_IS_CLIENT (auth))
2148 return auth->mech->client_decode_func != NULL;
2150 return auth->mech->server_decode_func != NULL;
2158 * Called post-authentication, decodes a block of bytes received from
2159 * the peer. If no encoding was negotiated, just copies the bytes (you
2160 * can avoid this by checking _dbus_auth_needs_decoding()).
2162 * @todo We need to be able to distinguish "out of memory" error
2163 * from "the data is hosed" error.
2165 * @param auth the auth conversation
2166 * @param encoded the encoded data
2167 * @param plaintext initialized string where decoded data is appended
2168 * @returns #TRUE if we had enough memory and successfully decoded
2171 _dbus_auth_decode_data (DBusAuth *auth,
2172 const DBusString *encoded,
2173 DBusString *plaintext)
2175 _dbus_assert (plaintext != encoded);
2177 if (!auth->authenticated)
2180 if (_dbus_auth_needs_decoding (auth))
2182 if (DBUS_AUTH_IS_CLIENT (auth))
2183 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2185 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2189 return _dbus_string_copy (encoded, 0, plaintext,
2190 _dbus_string_get_length (plaintext));
2195 * Sets credentials received via reliable means from the operating
2198 * @param auth the auth conversation
2199 * @param credentials the credentials received
2202 _dbus_auth_set_credentials (DBusAuth *auth,
2203 const DBusCredentials *credentials)
2205 auth->credentials = *credentials;
2209 * Gets the identity we authorized the client as. Apps may have
2210 * different policies as to what identities they allow.
2212 * @param auth the auth conversation
2213 * @param credentials the credentials we've authorized
2216 _dbus_auth_get_identity (DBusAuth *auth,
2217 DBusCredentials *credentials)
2219 if (auth->authenticated)
2221 *credentials = auth->authorized_identity;
2225 credentials->pid = -1;
2226 credentials->uid = -1;
2227 credentials->gid = -1;
2232 * Sets the "authentication context" which scopes cookies
2233 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2235 * @param auth the auth conversation
2236 * @param context the context
2237 * @returns #FALSE if no memory
2240 _dbus_auth_set_context (DBusAuth *auth,
2241 const DBusString *context)
2243 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2244 &auth->context, 0, _dbus_string_get_length (context));
2249 #ifdef DBUS_BUILD_TESTS
2250 #include "dbus-test.h"
2251 #include "dbus-auth-script.h"
2255 process_test_subdir (const DBusString *test_base_dir,
2258 DBusString test_directory;
2259 DBusString filename;
2267 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
2268 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2270 _dbus_string_init_const (&filename, subdir);
2272 if (!_dbus_string_copy (test_base_dir, 0,
2273 &test_directory, 0))
2274 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2276 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2277 _dbus_assert_not_reached ("couldn't allocate full path");
2279 _dbus_string_free (&filename);
2280 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
2281 _dbus_assert_not_reached ("didn't allocate filename string\n");
2283 dbus_error_init (&error);
2284 dir = _dbus_directory_open (&test_directory, &error);
2288 _dbus_string_get_const_data (&test_directory, &s);
2289 _dbus_warn ("Could not open %s: %s\n", s,
2291 dbus_error_free (&error);
2295 printf ("Testing:\n");
2298 while (_dbus_directory_get_next_file (dir, &filename, &error))
2300 DBusString full_path;
2302 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
2303 _dbus_assert_not_reached ("couldn't init string");
2305 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2306 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2308 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2309 _dbus_assert_not_reached ("couldn't concat file to dir");
2311 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2313 const char *filename_c;
2314 _dbus_string_get_const_data (&filename, &filename_c);
2315 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2317 _dbus_string_free (&full_path);
2323 _dbus_string_get_const_data (&filename, &s);
2324 printf (" %s\n", s);
2327 if (!_dbus_auth_script_run (&full_path))
2329 _dbus_string_free (&full_path);
2333 _dbus_string_free (&full_path);
2336 if (dbus_error_is_set (&error))
2339 _dbus_string_get_const_data (&test_directory, &s);
2340 _dbus_warn ("Could not get next file in %s: %s\n",
2342 dbus_error_free (&error);
2351 _dbus_directory_close (dir);
2352 _dbus_string_free (&test_directory);
2353 _dbus_string_free (&filename);
2359 process_test_dirs (const char *test_data_dir)
2361 DBusString test_directory;
2366 _dbus_string_init_const (&test_directory, test_data_dir);
2368 if (!process_test_subdir (&test_directory, "auth"))
2375 _dbus_string_free (&test_directory);
2381 _dbus_auth_test (const char *test_data_dir)
2384 if (test_data_dir == NULL)
2387 if (!process_test_dirs (test_data_dir))
2393 #endif /* DBUS_BUILD_TESTS */