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 */
178 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
186 int failures; /**< Number of times client has been rejected */
187 int max_failures; /**< Number of times we reject before disconnect */
191 static dbus_bool_t process_auth (DBusAuth *auth,
192 const DBusString *command,
193 const DBusString *args);
194 static dbus_bool_t process_cancel (DBusAuth *auth,
195 const DBusString *command,
196 const DBusString *args);
197 static dbus_bool_t process_begin (DBusAuth *auth,
198 const DBusString *command,
199 const DBusString *args);
200 static dbus_bool_t process_data_server (DBusAuth *auth,
201 const DBusString *command,
202 const DBusString *args);
203 static dbus_bool_t process_error_server (DBusAuth *auth,
204 const DBusString *command,
205 const DBusString *args);
206 static dbus_bool_t process_rejected (DBusAuth *auth,
207 const DBusString *command,
208 const DBusString *args);
209 static dbus_bool_t process_ok (DBusAuth *auth,
210 const DBusString *command,
211 const DBusString *args);
212 static dbus_bool_t process_data_client (DBusAuth *auth,
213 const DBusString *command,
214 const DBusString *args);
215 static dbus_bool_t process_error_client (DBusAuth *auth,
216 const DBusString *command,
217 const DBusString *args);
220 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
221 static dbus_bool_t send_rejected (DBusAuth *auth);
223 static DBusAuthCommandHandler
224 server_handlers[] = {
225 { "AUTH", process_auth },
226 { "CANCEL", process_cancel },
227 { "BEGIN", process_begin },
228 { "DATA", process_data_server },
229 { "ERROR", process_error_server },
233 static DBusAuthCommandHandler
234 client_handlers[] = {
235 { "REJECTED", process_rejected },
236 { "OK", process_ok },
237 { "DATA", process_data_client },
238 { "ERROR", process_error_client },
243 * @param auth the auth conversation
244 * @returns #TRUE if the conversation is the server side
246 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
248 * @param auth the auth conversation
249 * @returns #TRUE if the conversation is the client side
251 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
253 * @param auth the auth conversation
254 * @returns auth cast to DBusAuthClient
256 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
258 * @param auth the auth conversation
259 * @returns auth cast to DBusAuthServer
261 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
264 _dbus_auth_new (int size)
268 auth = dbus_malloc0 (size);
274 auth->credentials.pid = -1;
275 auth->credentials.uid = -1;
276 auth->credentials.gid = -1;
278 auth->authorized_identity.pid = -1;
279 auth->authorized_identity.uid = -1;
280 auth->authorized_identity.gid = -1;
282 auth->desired_identity.pid = -1;
283 auth->desired_identity.uid = -1;
284 auth->desired_identity.gid = -1;
286 auth->keyring = NULL;
287 auth->cookie_id = -1;
289 /* note that we don't use the max string length feature,
290 * because you can't use that feature if you're going to
291 * try to recover from out-of-memory (it creates
292 * what looks like unrecoverable inability to alloc
293 * more space in the string). But we do handle
294 * overlong buffers in _dbus_auth_do_work().
297 if (!_dbus_string_init (&auth->incoming, _DBUS_INT_MAX))
300 if (!_dbus_string_init (&auth->outgoing, _DBUS_INT_MAX))
303 if (!_dbus_string_init (&auth->identity, _DBUS_INT_MAX))
306 if (!_dbus_string_init (&auth->context, _DBUS_INT_MAX))
309 if (!_dbus_string_init (&auth->challenge, _DBUS_INT_MAX))
312 /* default context if none is specified */
313 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
319 _dbus_string_free (&auth->challenge);
321 _dbus_string_free (&auth->context);
323 _dbus_string_free (&auth->identity);
325 _dbus_string_free (&auth->incoming);
327 _dbus_string_free (&auth->outgoing);
334 shutdown_mech (DBusAuth *auth)
336 /* Cancel any auth */
337 auth->authenticated_pending_begin = FALSE;
338 auth->authenticated = FALSE;
339 auth->already_asked_for_initial_response = FALSE;
340 _dbus_string_set_length (&auth->identity, 0);
342 auth->authorized_identity.pid = -1;
343 auth->authorized_identity.uid = -1;
344 auth->authorized_identity.gid = -1;
346 auth->desired_identity.pid = -1;
347 auth->desired_identity.uid = -1;
348 auth->desired_identity.gid = -1;
350 if (auth->mech != NULL)
352 _dbus_verbose ("Shutting down mechanism %s\n",
353 auth->mech->mechanism);
355 if (DBUS_AUTH_IS_CLIENT (auth))
356 (* auth->mech->client_shutdown_func) (auth);
358 (* auth->mech->server_shutdown_func) (auth);
364 /* Returns TRUE but with an empty string hash if the
365 * cookie_id isn't known. As with all this code
366 * TRUE just means we had enough memory.
369 sha1_compute_hash (DBusAuth *auth,
371 const DBusString *server_challenge,
372 const DBusString *client_challenge,
379 _dbus_assert (auth->keyring != NULL);
383 if (!_dbus_string_init (&cookie, _DBUS_INT_MAX))
386 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
390 if (_dbus_string_get_length (&cookie) == 0)
396 if (!_dbus_string_init (&to_hash, _DBUS_INT_MAX))
399 if (!_dbus_string_copy (server_challenge, 0,
400 &to_hash, _dbus_string_get_length (&to_hash)))
403 if (!_dbus_string_append (&to_hash, ":"))
406 if (!_dbus_string_copy (client_challenge, 0,
407 &to_hash, _dbus_string_get_length (&to_hash)))
410 if (!_dbus_string_append (&to_hash, ":"))
413 if (!_dbus_string_copy (&cookie, 0,
414 &to_hash, _dbus_string_get_length (&to_hash)))
417 if (!_dbus_sha_compute (&to_hash, hash))
423 _dbus_string_zero (&to_hash);
424 _dbus_string_free (&to_hash);
426 _dbus_string_zero (&cookie);
427 _dbus_string_free (&cookie);
431 /* http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
432 * entropy, we use 128
434 #define N_CHALLENGE_BYTES (128/8)
437 sha1_handle_first_client_response (DBusAuth *auth,
438 const DBusString *data)
440 /* We haven't sent a challenge yet, we're expecting a desired
441 * username from the client.
451 _dbus_string_set_length (&auth->challenge, 0);
453 if (_dbus_string_get_length (data) > 0)
455 if (_dbus_string_get_length (&auth->identity) > 0)
457 /* Tried to send two auth identities, wtf */
458 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
459 return send_rejected (auth);
463 /* this is our auth identity */
464 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
469 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
471 _dbus_verbose ("Did not get a valid username from client\n");
472 return send_rejected (auth);
475 if (!_dbus_string_init (&tmp, _DBUS_INT_MAX))
478 if (!_dbus_string_init (&tmp2, _DBUS_INT_MAX))
480 _dbus_string_free (&tmp);
484 old_len = _dbus_string_get_length (&auth->outgoing);
486 /* we cache the keyring for speed, so here we drop it if it's the
487 * wrong one. FIXME caching the keyring here is useless since we use
488 * a different DBusAuth for every connection.
491 !_dbus_keyring_is_for_user (auth->keyring,
494 _dbus_keyring_unref (auth->keyring);
495 auth->keyring = NULL;
498 if (auth->keyring == NULL)
502 dbus_error_init (&error);
503 auth->keyring = _dbus_keyring_new_homedir (data,
507 if (auth->keyring == NULL)
509 if (dbus_error_has_name (&error,
510 DBUS_ERROR_NO_MEMORY))
512 dbus_error_free (&error);
517 _DBUS_ASSERT_ERROR_IS_SET (&error);
518 _dbus_verbose ("Error loading keyring: %s\n",
520 if (send_rejected (auth))
521 retval = TRUE; /* retval is only about mem */
522 dbus_error_free (&error);
528 _dbus_assert (!dbus_error_is_set (&error));
532 _dbus_assert (auth->keyring != NULL);
534 dbus_error_init (&error);
535 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
536 if (auth->cookie_id < 0)
538 _DBUS_ASSERT_ERROR_IS_SET (&error);
539 _dbus_verbose ("Could not get a cookie ID to send to client: %s\n",
541 if (send_rejected (auth))
543 dbus_error_free (&error);
548 _dbus_assert (!dbus_error_is_set (&error));
551 if (!_dbus_string_copy (&auth->context, 0,
552 &tmp2, _dbus_string_get_length (&tmp2)))
555 if (!_dbus_string_append (&tmp2, " "))
558 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
561 if (!_dbus_string_append (&tmp2, " "))
564 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
567 _dbus_string_set_length (&auth->challenge, 0);
568 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
571 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
572 _dbus_string_get_length (&tmp2)))
575 if (!_dbus_string_append (&auth->outgoing,
579 if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
580 _dbus_string_get_length (&auth->outgoing)))
583 if (!_dbus_string_append (&auth->outgoing,
590 _dbus_string_zero (&tmp);
591 _dbus_string_free (&tmp);
592 _dbus_string_zero (&tmp2);
593 _dbus_string_free (&tmp2);
595 _dbus_string_set_length (&auth->outgoing, old_len);
600 sha1_handle_second_client_response (DBusAuth *auth,
601 const DBusString *data)
603 /* We are expecting a response which is the hex-encoded client
604 * challenge, space, then SHA-1 hash of the concatenation of our
605 * challenge, ":", client challenge, ":", secret key, all
609 DBusString client_challenge;
610 DBusString client_hash;
612 DBusString correct_hash;
616 if (!_dbus_string_find_blank (data, 0, &i))
618 _dbus_verbose ("no space separator in client response\n");
619 return send_rejected (auth);
622 if (!_dbus_string_init (&client_challenge, _DBUS_INT_MAX))
625 if (!_dbus_string_init (&client_hash, _DBUS_INT_MAX))
628 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
632 _dbus_string_skip_blank (data, i, &i);
634 if (!_dbus_string_copy_len (data, i,
635 _dbus_string_get_length (data) - i,
640 if (_dbus_string_get_length (&client_challenge) == 0 ||
641 _dbus_string_get_length (&client_hash) == 0)
643 _dbus_verbose ("zero-length client challenge or hash\n");
644 if (send_rejected (auth))
649 if (!_dbus_string_init (&correct_hash, _DBUS_INT_MAX))
652 if (!sha1_compute_hash (auth, auth->cookie_id,
658 /* if cookie_id was invalid, then we get an empty hash */
659 if (_dbus_string_get_length (&correct_hash) == 0)
661 if (send_rejected (auth))
666 if (!_dbus_string_equal (&client_hash, &correct_hash))
668 if (send_rejected (auth))
673 if (!_dbus_string_append (&auth->outgoing,
677 _dbus_verbose ("authenticated client with UID %d using DBUS_COOKIE_SHA1\n",
678 auth->desired_identity.uid);
680 auth->authorized_identity = auth->desired_identity;
681 auth->authenticated_pending_begin = TRUE;
685 _dbus_string_zero (&correct_hash);
686 _dbus_string_free (&correct_hash);
688 _dbus_string_zero (&client_hash);
689 _dbus_string_free (&client_hash);
691 _dbus_string_free (&client_challenge);
697 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
698 const DBusString *data)
700 if (auth->cookie_id < 0)
701 return sha1_handle_first_client_response (auth, data);
703 return sha1_handle_second_client_response (auth, data);
707 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
709 auth->cookie_id = -1;
710 _dbus_string_set_length (&auth->challenge, 0);
714 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
715 DBusString *response)
717 const DBusString *username;
722 if (!_dbus_user_info_from_current_process (&username,
726 if (!_dbus_string_base64_encode (username, 0,
728 _dbus_string_get_length (response)))
737 /* FIXME if we send the server an error, right now both sides
738 * just hang. Server has to reject on getting an error, or
739 * client has to cancel. Should be in the spec.
742 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
743 const DBusString *data)
745 /* The data we get from the server should be the cookie context
746 * name, the cookie ID, and the server challenge, separated by
747 * spaces. We send back our challenge string and the correct hash.
751 DBusString cookie_id_str;
752 DBusString server_challenge;
753 DBusString client_challenge;
754 DBusString correct_hash;
762 if (!_dbus_string_find_blank (data, 0, &i))
764 if (_dbus_string_append (&auth->outgoing,
765 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
770 if (!_dbus_string_init (&context, _DBUS_INT_MAX))
773 if (!_dbus_string_copy_len (data, 0, i,
777 _dbus_string_skip_blank (data, i, &i);
778 if (!_dbus_string_find_blank (data, i, &j))
780 if (_dbus_string_append (&auth->outgoing,
781 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
786 if (!_dbus_string_init (&cookie_id_str, _DBUS_INT_MAX))
789 if (!_dbus_string_copy_len (data, i, j - i,
793 if (!_dbus_string_init (&server_challenge, _DBUS_INT_MAX))
797 _dbus_string_skip_blank (data, i, &i);
798 j = _dbus_string_get_length (data);
800 if (!_dbus_string_copy_len (data, i, j - i,
801 &server_challenge, 0))
804 if (!_dbus_keyring_validate_context (&context))
806 if (_dbus_string_append (&auth->outgoing,
807 "ERROR \"Server sent invalid cookie context\"\r\n"))
812 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
814 if (_dbus_string_append (&auth->outgoing,
815 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
820 if (_dbus_string_get_length (&server_challenge) == 0)
822 if (_dbus_string_append (&auth->outgoing,
823 "ERROR \"Empty server challenge string\"\r\n"))
828 if (auth->keyring == NULL)
832 dbus_error_init (&error);
833 auth->keyring = _dbus_keyring_new_homedir (NULL,
837 if (auth->keyring == NULL)
839 if (dbus_error_has_name (&error,
840 DBUS_ERROR_NO_MEMORY))
842 dbus_error_free (&error);
847 _DBUS_ASSERT_ERROR_IS_SET (&error);
849 _dbus_verbose ("Error loading keyring: %s\n",
852 if (_dbus_string_append (&auth->outgoing,
853 "ERROR \"Could not load cookie file\"\r\n"))
854 retval = TRUE; /* retval is only about mem */
856 dbus_error_free (&error);
862 _dbus_assert (!dbus_error_is_set (&error));
866 _dbus_assert (auth->keyring != NULL);
868 if (!_dbus_string_init (&tmp, _DBUS_INT_MAX))
871 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
874 if (!_dbus_string_init (&client_challenge, _DBUS_INT_MAX))
877 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
880 if (!_dbus_string_init (&correct_hash, _DBUS_INT_MAX))
883 if (!sha1_compute_hash (auth, val,
889 if (_dbus_string_get_length (&correct_hash) == 0)
891 /* couldn't find the cookie ID or something */
892 if (_dbus_string_append (&auth->outgoing,
893 "ERROR \"Don't have the requested cookie ID\"\r\n"))
898 _dbus_string_set_length (&tmp, 0);
900 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
901 _dbus_string_get_length (&tmp)))
904 if (!_dbus_string_append (&tmp, " "))
907 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
908 _dbus_string_get_length (&tmp)))
911 old_len = _dbus_string_get_length (&auth->outgoing);
912 if (!_dbus_string_append (&auth->outgoing, "DATA "))
915 if (!_dbus_string_base64_encode (&tmp, 0,
917 _dbus_string_get_length (&auth->outgoing)))
919 _dbus_string_set_length (&auth->outgoing, old_len);
923 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
925 _dbus_string_set_length (&auth->outgoing, old_len);
932 _dbus_string_zero (&correct_hash);
933 _dbus_string_free (&correct_hash);
935 _dbus_string_free (&client_challenge);
937 _dbus_string_zero (&tmp);
938 _dbus_string_free (&tmp);
940 _dbus_string_free (&server_challenge);
942 _dbus_string_free (&cookie_id_str);
944 _dbus_string_free (&context);
950 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
952 auth->cookie_id = -1;
953 _dbus_string_set_length (&auth->challenge, 0);
957 handle_server_data_external_mech (DBusAuth *auth,
958 const DBusString *data)
960 if (auth->credentials.uid < 0)
962 _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
963 return send_rejected (auth);
966 if (_dbus_string_get_length (data) > 0)
968 if (_dbus_string_get_length (&auth->identity) > 0)
970 /* Tried to send two auth identities, wtf */
971 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
972 return send_rejected (auth);
976 /* this is our auth identity */
977 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
982 /* Poke client for an auth identity, if none given */
983 if (_dbus_string_get_length (&auth->identity) == 0 &&
984 !auth->already_asked_for_initial_response)
986 if (_dbus_string_append (&auth->outgoing,
989 _dbus_verbose ("sending empty challenge asking client for auth identity\n");
990 auth->already_asked_for_initial_response = TRUE;
997 auth->desired_identity.pid = -1;
998 auth->desired_identity.uid = -1;
999 auth->desired_identity.gid = -1;
1001 /* If auth->identity is still empty here, then client
1002 * responded with an empty string after we poked it for
1003 * an initial response. This means to try to auth the
1004 * identity provided in the credentials.
1006 if (_dbus_string_get_length (&auth->identity) == 0)
1008 auth->desired_identity.uid = auth->credentials.uid;
1012 if (!_dbus_credentials_from_uid_string (&auth->identity,
1013 &auth->desired_identity))
1015 _dbus_verbose ("could not get credentials from uid string\n");
1016 return send_rejected (auth);
1020 if (auth->desired_identity.uid < 0)
1022 _dbus_verbose ("desired UID %d is no good\n", auth->desired_identity.uid);
1023 return send_rejected (auth);
1026 if (_dbus_credentials_match (&auth->desired_identity,
1027 &auth->credentials))
1029 /* client has authenticated */
1030 if (!_dbus_string_append (&auth->outgoing,
1034 _dbus_verbose ("authenticated client with UID %d matching socket credentials UID %d\n",
1035 auth->desired_identity.uid,
1036 auth->credentials.uid);
1038 auth->authorized_identity.uid = auth->desired_identity.uid;
1040 auth->authenticated_pending_begin = TRUE;
1046 _dbus_verbose ("credentials uid=%d gid=%d do not allow uid=%d gid=%d\n",
1047 auth->credentials.uid, auth->credentials.gid,
1048 auth->desired_identity.uid, auth->desired_identity.gid);
1049 return send_rejected (auth);
1054 handle_server_shutdown_external_mech (DBusAuth *auth)
1060 handle_client_initial_response_external_mech (DBusAuth *auth,
1061 DBusString *response)
1063 /* We always append our UID as an initial response, so the server
1064 * doesn't have to send back an empty challenge to check whether we
1065 * want to specify an identity. i.e. this avoids a round trip that
1066 * the spec for the EXTERNAL mechanism otherwise requires.
1068 DBusString plaintext;
1070 if (!_dbus_string_init (&plaintext, _DBUS_INT_MAX))
1073 if (!_dbus_string_append_our_uid (&plaintext))
1076 if (!_dbus_string_base64_encode (&plaintext, 0,
1078 _dbus_string_get_length (response)))
1081 _dbus_string_free (&plaintext);
1086 _dbus_string_free (&plaintext);
1091 handle_client_data_external_mech (DBusAuth *auth,
1092 const DBusString *data)
1099 handle_client_shutdown_external_mech (DBusAuth *auth)
1104 /* Put mechanisms here in order of preference.
1105 * What I eventually want to have is:
1107 * - a mechanism that checks UNIX domain socket credentials
1108 * - a simple magic cookie mechanism like X11 or ICE
1109 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1110 * offers such as Kerberos, X509, whatever.
1113 static const DBusAuthMechanismHandler
1114 all_mechanisms[] = {
1116 handle_server_data_external_mech,
1118 handle_server_shutdown_external_mech,
1119 handle_client_initial_response_external_mech,
1120 handle_client_data_external_mech,
1122 handle_client_shutdown_external_mech },
1123 { "DBUS_COOKIE_SHA1",
1124 handle_server_data_cookie_sha1_mech,
1126 handle_server_shutdown_cookie_sha1_mech,
1127 handle_client_initial_response_cookie_sha1_mech,
1128 handle_client_data_cookie_sha1_mech,
1130 handle_client_shutdown_cookie_sha1_mech },
1134 static const DBusAuthMechanismHandler*
1135 find_mech (const DBusString *name)
1140 while (all_mechanisms[i].mechanism != NULL)
1142 if (_dbus_string_equal_c_str (name,
1143 all_mechanisms[i].mechanism))
1145 return &all_mechanisms[i];
1154 send_rejected (DBusAuth *auth)
1157 DBusAuthServer *server_auth;
1160 if (!_dbus_string_init (&command, _DBUS_INT_MAX))
1163 if (!_dbus_string_append (&command,
1168 while (all_mechanisms[i].mechanism != NULL)
1170 if (!_dbus_string_append (&command,
1174 if (!_dbus_string_append (&command,
1175 all_mechanisms[i].mechanism))
1181 if (!_dbus_string_append (&command, "\r\n"))
1184 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1185 _dbus_string_get_length (&auth->outgoing)))
1188 shutdown_mech (auth);
1190 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1191 server_auth = DBUS_AUTH_SERVER (auth);
1192 server_auth->failures += 1;
1194 _dbus_string_free (&command);
1199 _dbus_string_free (&command);
1204 process_auth (DBusAuth *auth,
1205 const DBusString *command,
1206 const DBusString *args)
1210 /* We are already using a mechanism, client is on crack */
1211 if (!_dbus_string_append (&auth->outgoing,
1212 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1217 else if (_dbus_string_get_length (args) == 0)
1219 /* No args to the auth, send mechanisms */
1220 if (!send_rejected (auth))
1229 DBusString base64_response;
1230 DBusString decoded_response;
1232 _dbus_string_find_blank (args, 0, &i);
1234 if (!_dbus_string_init (&mech, _DBUS_INT_MAX))
1237 if (!_dbus_string_init (&base64_response, _DBUS_INT_MAX))
1239 _dbus_string_free (&mech);
1243 if (!_dbus_string_init (&decoded_response, _DBUS_INT_MAX))
1245 _dbus_string_free (&mech);
1246 _dbus_string_free (&base64_response);
1250 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1253 if (!_dbus_string_copy (args, i, &base64_response, 0))
1256 if (!_dbus_string_base64_decode (&base64_response, 0,
1257 &decoded_response, 0))
1260 auth->mech = find_mech (&mech);
1261 if (auth->mech != NULL)
1263 _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
1264 auth->mech->mechanism,
1265 _dbus_string_get_length (&decoded_response));
1267 if (!(* auth->mech->server_data_func) (auth,
1273 /* Unsupported mechanism */
1274 if (!send_rejected (auth))
1278 _dbus_string_free (&mech);
1279 _dbus_string_free (&base64_response);
1280 _dbus_string_free (&decoded_response);
1286 _dbus_string_free (&mech);
1287 _dbus_string_free (&base64_response);
1288 _dbus_string_free (&decoded_response);
1294 process_cancel (DBusAuth *auth,
1295 const DBusString *command,
1296 const DBusString *args)
1298 shutdown_mech (auth);
1304 process_begin (DBusAuth *auth,
1305 const DBusString *command,
1306 const DBusString *args)
1308 if (auth->authenticated_pending_begin)
1309 auth->authenticated = TRUE;
1312 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1315 shutdown_mech (auth);
1322 process_data_server (DBusAuth *auth,
1323 const DBusString *command,
1324 const DBusString *args)
1326 if (auth->mech != NULL)
1330 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1333 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1335 _dbus_string_free (&decoded);
1339 #ifdef DBUS_ENABLE_VERBOSE_MODE
1340 if (_dbus_string_validate_ascii (&decoded, 0,
1341 _dbus_string_get_length (&decoded)))
1344 _dbus_string_get_const_data (&decoded, &s);
1345 _dbus_verbose ("data: '%s'\n", s);
1349 if (!(* auth->mech->server_data_func) (auth, &decoded))
1351 _dbus_string_free (&decoded);
1355 _dbus_string_free (&decoded);
1359 if (!_dbus_string_append (&auth->outgoing,
1360 "ERROR \"Not currently in an auth conversation\"\r\n"))
1368 process_error_server (DBusAuth *auth,
1369 const DBusString *command,
1370 const DBusString *args)
1376 /* return FALSE if no memory, TRUE if all OK */
1378 get_word (const DBusString *str,
1384 _dbus_string_skip_blank (str, *start, start);
1385 _dbus_string_find_blank (str, *start, &i);
1389 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1399 record_mechanisms (DBusAuth *auth,
1400 const DBusString *command,
1401 const DBusString *args)
1406 if (auth->already_got_mechanisms)
1409 len = _dbus_string_get_length (args);
1415 const DBusAuthMechanismHandler *mech;
1417 if (!_dbus_string_init (&m, _DBUS_INT_MAX))
1420 if (!get_word (args, &next, &m))
1423 mech = find_mech (&m);
1427 /* FIXME right now we try mechanisms in the order
1428 * the server lists them; should we do them in
1429 * some more deterministic order?
1431 * Probably in all_mechanisms order, our order of
1432 * preference. Of course when the server is us,
1433 * it lists things in that order anyhow.
1436 _dbus_verbose ("Adding mechanism %s to list we will try\n",
1439 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1447 _dbus_string_get_const_data (&m, &s);
1448 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1452 _dbus_string_free (&m);
1455 auth->already_got_mechanisms = TRUE;
1460 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1466 client_try_next_mechanism (DBusAuth *auth)
1468 const DBusAuthMechanismHandler *mech;
1469 DBusString auth_command;
1471 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1474 mech = DBUS_AUTH_CLIENT (auth)->mechs_to_try->data;
1476 if (!_dbus_string_init (&auth_command, _DBUS_INT_MAX))
1479 if (!_dbus_string_append (&auth_command,
1482 _dbus_string_free (&auth_command);
1486 if (!_dbus_string_append (&auth_command,
1489 _dbus_string_free (&auth_command);
1493 if (mech->client_initial_response_func != NULL)
1495 if (!_dbus_string_append (&auth_command, " "))
1497 _dbus_string_free (&auth_command);
1501 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1503 _dbus_string_free (&auth_command);
1508 if (!_dbus_string_append (&auth_command,
1511 _dbus_string_free (&auth_command);
1515 if (!_dbus_string_copy (&auth_command, 0,
1517 _dbus_string_get_length (&auth->outgoing)))
1519 _dbus_string_free (&auth_command);
1524 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1526 _dbus_verbose ("Trying mechanism %s\n",
1527 auth->mech->mechanism);
1529 _dbus_string_free (&auth_command);
1535 process_rejected (DBusAuth *auth,
1536 const DBusString *command,
1537 const DBusString *args)
1539 shutdown_mech (auth);
1541 if (!auth->already_got_mechanisms)
1543 if (!record_mechanisms (auth, command, args))
1547 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1549 client_try_next_mechanism (auth);
1554 auth->need_disconnect = TRUE;
1561 process_ok (DBusAuth *auth,
1562 const DBusString *command,
1563 const DBusString *args)
1565 if (!_dbus_string_append (&auth->outgoing,
1569 auth->authenticated_pending_output = TRUE;
1575 process_data_client (DBusAuth *auth,
1576 const DBusString *command,
1577 const DBusString *args)
1579 if (auth->mech != NULL)
1583 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1586 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1588 _dbus_string_free (&decoded);
1592 #ifdef DBUS_ENABLE_VERBOSE_MODE
1593 if (_dbus_string_validate_ascii (&decoded, 0,
1594 _dbus_string_get_length (&decoded)))
1597 _dbus_string_get_const_data (&decoded, &s);
1598 _dbus_verbose ("data: '%s'\n", s);
1602 if (!(* auth->mech->client_data_func) (auth, &decoded))
1604 _dbus_string_free (&decoded);
1608 _dbus_string_free (&decoded);
1612 if (!_dbus_string_append (&auth->outgoing,
1613 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1621 process_error_client (DBusAuth *auth,
1622 const DBusString *command,
1623 const DBusString *args)
1629 process_unknown (DBusAuth *auth,
1630 const DBusString *command,
1631 const DBusString *args)
1633 if (!_dbus_string_append (&auth->outgoing,
1634 "ERROR \"Unknown command\"\r\n"))
1640 /* returns whether to call it again right away */
1642 process_command (DBusAuth *auth)
1650 /* _dbus_verbose (" trying process_command()\n"); */
1655 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1658 if (!_dbus_string_init (&command, _DBUS_INT_MAX))
1660 auth->needed_memory = TRUE;
1664 if (!_dbus_string_init (&args, _DBUS_INT_MAX))
1666 auth->needed_memory = TRUE;
1670 if (eol > _DBUS_ONE_MEGABYTE)
1672 /* This is a giant line, someone is trying to hose us. */
1673 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1679 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1682 if (!_dbus_string_validate_ascii (&command, 0,
1683 _dbus_string_get_length (&command)))
1685 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1686 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1694 _dbus_string_get_const_data (&command, &q);
1695 _dbus_verbose ("got command \"%s\"\n", q);
1698 _dbus_string_find_blank (&command, 0, &i);
1699 _dbus_string_skip_blank (&command, i, &j);
1702 _dbus_string_delete (&command, i, j - i);
1704 if (!_dbus_string_move (&command, i, &args, 0))
1708 while (auth->handlers[i].command != NULL)
1710 if (_dbus_string_equal_c_str (&command,
1711 auth->handlers[i].command))
1713 _dbus_verbose ("Processing auth command %s\n",
1714 auth->handlers[i].command);
1716 if (!(* auth->handlers[i].func) (auth, &command, &args))
1724 if (auth->handlers[i].command == NULL)
1726 if (!process_unknown (auth, &command, &args))
1732 /* We've succeeded in processing the whole command so drop it out
1733 * of the incoming buffer and return TRUE to try another command.
1736 _dbus_string_delete (&auth->incoming, 0, eol);
1739 _dbus_string_delete (&auth->incoming, 0, 2);
1744 _dbus_string_free (&args);
1745 _dbus_string_free (&command);
1748 auth->needed_memory = TRUE;
1750 auth->needed_memory = FALSE;
1759 * @addtogroup DBusAuth
1764 * Creates a new auth conversation object for the server side.
1765 * See doc/dbus-sasl-profile.txt for full details on what
1768 * @returns the new object or #NULL if no memory
1771 _dbus_auth_server_new (void)
1774 DBusAuthServer *server_auth;
1776 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1780 auth->handlers = server_handlers;
1782 server_auth = DBUS_AUTH_SERVER (auth);
1784 /* perhaps this should be per-mechanism with a lower
1787 server_auth->failures = 0;
1788 server_auth->max_failures = 6;
1794 * Creates a new auth conversation object for the client side.
1795 * See doc/dbus-sasl-profile.txt for full details on what
1798 * @returns the new object or #NULL if no memory
1801 _dbus_auth_client_new (void)
1805 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1809 auth->handlers = client_handlers;
1811 /* Add a default mechanism to try */
1812 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1813 (void*) &all_mechanisms[0]))
1815 _dbus_auth_unref (auth);
1819 /* Now try the mechanism we just added */
1820 if (!client_try_next_mechanism (auth))
1822 _dbus_auth_unref (auth);
1830 * Increments the refcount of an auth object.
1832 * @param auth the auth conversation
1835 _dbus_auth_ref (DBusAuth *auth)
1837 _dbus_assert (auth != NULL);
1839 auth->refcount += 1;
1843 * Decrements the refcount of an auth object.
1845 * @param auth the auth conversation
1848 _dbus_auth_unref (DBusAuth *auth)
1850 _dbus_assert (auth != NULL);
1851 _dbus_assert (auth->refcount > 0);
1853 auth->refcount -= 1;
1854 if (auth->refcount == 0)
1856 shutdown_mech (auth);
1858 if (DBUS_AUTH_IS_CLIENT (auth))
1860 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1864 _dbus_keyring_unref (auth->keyring);
1866 _dbus_string_free (&auth->identity);
1867 _dbus_string_free (&auth->incoming);
1868 _dbus_string_free (&auth->outgoing);
1874 * @param auth the auth conversation object
1875 * @returns #TRUE if we're in a final state
1877 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1880 * Analyzes buffered input and moves the auth conversation forward,
1881 * returning the new state of the auth conversation.
1883 * @param auth the auth conversation
1884 * @returns the new state
1887 _dbus_auth_do_work (DBusAuth *auth)
1889 auth->needed_memory = FALSE;
1891 /* Max amount we'll buffer up before deciding someone's on crack */
1892 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1896 if (DBUS_AUTH_IN_END_STATE (auth))
1899 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1900 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1902 auth->need_disconnect = TRUE;
1903 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1907 if (auth->mech == NULL &&
1908 auth->already_got_mechanisms &&
1909 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1911 auth->need_disconnect = TRUE;
1912 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1916 while (process_command (auth));
1918 if (DBUS_AUTH_IS_SERVER (auth) &&
1919 DBUS_AUTH_SERVER (auth)->failures >=
1920 DBUS_AUTH_SERVER (auth)->max_failures)
1921 auth->need_disconnect = TRUE;
1923 if (auth->need_disconnect)
1924 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1925 else if (auth->authenticated)
1927 if (_dbus_string_get_length (&auth->incoming) > 0)
1928 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1930 return DBUS_AUTH_STATE_AUTHENTICATED;
1932 else if (auth->needed_memory)
1933 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1934 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1935 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1937 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1941 * Gets bytes that need to be sent to the peer we're conversing with.
1942 * After writing some bytes, _dbus_auth_bytes_sent() must be called
1943 * to notify the auth object that they were written.
1945 * @param auth the auth conversation
1946 * @param str return location for a ref to the buffer to send
1947 * @returns #FALSE if nothing to send
1950 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
1951 const DBusString **str)
1953 _dbus_assert (auth != NULL);
1954 _dbus_assert (str != NULL);
1958 if (DBUS_AUTH_IN_END_STATE (auth))
1961 if (_dbus_string_get_length (&auth->outgoing) == 0)
1964 *str = &auth->outgoing;
1970 * Notifies the auth conversation object that
1971 * the given number of bytes of the outgoing buffer
1972 * have been written out.
1974 * @param auth the auth conversation
1975 * @param bytes_sent number of bytes written out
1978 _dbus_auth_bytes_sent (DBusAuth *auth,
1983 _dbus_string_get_const_data (&auth->outgoing, &s);
1984 _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent, s);
1987 _dbus_string_delete (&auth->outgoing,
1990 if (auth->authenticated_pending_output &&
1991 _dbus_string_get_length (&auth->outgoing) == 0)
1992 auth->authenticated = TRUE;
1996 * Stores bytes received from the peer we're conversing with.
1998 * @param auth the auth conversation
1999 * @param str the received bytes.
2000 * @returns #FALSE if not enough memory to store the bytes or we were already authenticated.
2003 _dbus_auth_bytes_received (DBusAuth *auth,
2004 const DBusString *str)
2006 _dbus_assert (auth != NULL);
2007 _dbus_assert (str != NULL);
2009 if (DBUS_AUTH_IN_END_STATE (auth))
2012 auth->needed_memory = FALSE;
2014 if (!_dbus_string_copy (str, 0,
2016 _dbus_string_get_length (&auth->incoming)))
2018 auth->needed_memory = TRUE;
2022 _dbus_auth_do_work (auth);
2028 * Returns leftover bytes that were not used as part of the auth
2029 * conversation. These bytes will be part of the message stream
2030 * instead. This function may not be called until authentication has
2033 * @param auth the auth conversation
2034 * @param str string to append the unused bytes to
2035 * @returns #FALSE if not enough memory to return the bytes
2038 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2041 if (!DBUS_AUTH_IN_END_STATE (auth))
2044 if (!_dbus_string_move (&auth->incoming,
2046 _dbus_string_get_length (str)))
2053 * Called post-authentication, indicates whether we need to encode
2054 * the message stream with _dbus_auth_encode_data() prior to
2055 * sending it to the peer.
2057 * @param auth the auth conversation
2058 * @returns #TRUE if we need to encode the stream
2061 _dbus_auth_needs_encoding (DBusAuth *auth)
2063 if (!auth->authenticated)
2066 if (auth->mech != NULL)
2068 if (DBUS_AUTH_IS_CLIENT (auth))
2069 return auth->mech->client_encode_func != NULL;
2071 return auth->mech->server_encode_func != NULL;
2078 * Called post-authentication, encodes a block of bytes for sending to
2079 * the peer. If no encoding was negotiated, just copies the bytes
2080 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2082 * @param auth the auth conversation
2083 * @param plaintext the plain text data
2084 * @param encoded initialized string to where encoded data is appended
2085 * @returns #TRUE if we had enough memory and successfully encoded
2088 _dbus_auth_encode_data (DBusAuth *auth,
2089 const DBusString *plaintext,
2090 DBusString *encoded)
2092 _dbus_assert (plaintext != encoded);
2094 if (!auth->authenticated)
2097 if (_dbus_auth_needs_encoding (auth))
2099 if (DBUS_AUTH_IS_CLIENT (auth))
2100 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2102 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2106 return _dbus_string_copy (plaintext, 0, encoded,
2107 _dbus_string_get_length (encoded));
2112 * Called post-authentication, indicates whether we need to decode
2113 * the message stream with _dbus_auth_decode_data() after
2114 * receiving it from the peer.
2116 * @param auth the auth conversation
2117 * @returns #TRUE if we need to encode the stream
2120 _dbus_auth_needs_decoding (DBusAuth *auth)
2122 if (!auth->authenticated)
2125 if (auth->mech != NULL)
2127 if (DBUS_AUTH_IS_CLIENT (auth))
2128 return auth->mech->client_decode_func != NULL;
2130 return auth->mech->server_decode_func != NULL;
2138 * Called post-authentication, decodes a block of bytes received from
2139 * the peer. If no encoding was negotiated, just copies the bytes (you
2140 * can avoid this by checking _dbus_auth_needs_decoding()).
2142 * @todo We need to be able to distinguish "out of memory" error
2143 * from "the data is hosed" error.
2145 * @param auth the auth conversation
2146 * @param encoded the encoded data
2147 * @param plaintext initialized string where decoded data is appended
2148 * @returns #TRUE if we had enough memory and successfully decoded
2151 _dbus_auth_decode_data (DBusAuth *auth,
2152 const DBusString *encoded,
2153 DBusString *plaintext)
2155 _dbus_assert (plaintext != encoded);
2157 if (!auth->authenticated)
2160 if (_dbus_auth_needs_decoding (auth))
2162 if (DBUS_AUTH_IS_CLIENT (auth))
2163 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2165 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2169 return _dbus_string_copy (encoded, 0, plaintext,
2170 _dbus_string_get_length (plaintext));
2175 * Sets credentials received via reliable means from the operating
2178 * @param auth the auth conversation
2179 * @param credentials the credentials received
2182 _dbus_auth_set_credentials (DBusAuth *auth,
2183 const DBusCredentials *credentials)
2185 auth->credentials = *credentials;
2189 * Gets the identity we authorized the client as. Apps may have
2190 * different policies as to what identities they allow.
2192 * @param auth the auth conversation
2193 * @param credentials the credentials we've authorized
2196 _dbus_auth_get_identity (DBusAuth *auth,
2197 DBusCredentials *credentials)
2199 if (auth->authenticated)
2201 *credentials = auth->authorized_identity;
2205 credentials->pid = -1;
2206 credentials->uid = -1;
2207 credentials->gid = -1;
2212 * Sets the "authentication context" which scopes cookies
2213 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2215 * @param auth the auth conversation
2216 * @param context the context
2217 * @returns #FALSE if no memory
2220 _dbus_auth_set_context (DBusAuth *auth,
2221 const DBusString *context)
2223 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2224 &auth->context, 0, _dbus_string_get_length (context));
2229 #ifdef DBUS_BUILD_TESTS
2230 #include "dbus-test.h"
2231 #include "dbus-auth-script.h"
2235 process_test_subdir (const DBusString *test_base_dir,
2238 DBusString test_directory;
2239 DBusString filename;
2247 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
2248 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2250 _dbus_string_init_const (&filename, subdir);
2252 if (!_dbus_string_copy (test_base_dir, 0,
2253 &test_directory, 0))
2254 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2256 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2257 _dbus_assert_not_reached ("couldn't allocate full path");
2259 _dbus_string_free (&filename);
2260 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
2261 _dbus_assert_not_reached ("didn't allocate filename string\n");
2263 dbus_error_init (&error);
2264 dir = _dbus_directory_open (&test_directory, &error);
2268 _dbus_string_get_const_data (&test_directory, &s);
2269 _dbus_warn ("Could not open %s: %s\n", s,
2271 dbus_error_free (&error);
2275 printf ("Testing:\n");
2278 while (_dbus_directory_get_next_file (dir, &filename, &error))
2280 DBusString full_path;
2282 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
2283 _dbus_assert_not_reached ("couldn't init string");
2285 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2286 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2288 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2289 _dbus_assert_not_reached ("couldn't concat file to dir");
2291 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2293 const char *filename_c;
2294 _dbus_string_get_const_data (&filename, &filename_c);
2295 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2297 _dbus_string_free (&full_path);
2303 _dbus_string_get_const_data (&filename, &s);
2304 printf (" %s\n", s);
2307 if (!_dbus_auth_script_run (&full_path))
2309 _dbus_string_free (&full_path);
2313 _dbus_string_free (&full_path);
2316 if (dbus_error_is_set (&error))
2319 _dbus_string_get_const_data (&test_directory, &s);
2320 _dbus_warn ("Could not get next file in %s: %s\n",
2322 dbus_error_free (&error);
2331 _dbus_directory_close (dir);
2332 _dbus_string_free (&test_directory);
2333 _dbus_string_free (&filename);
2339 process_test_dirs (const char *test_data_dir)
2341 DBusString test_directory;
2346 _dbus_string_init_const (&test_directory, test_data_dir);
2348 if (!process_test_subdir (&test_directory, "auth"))
2355 _dbus_string_free (&test_directory);
2361 _dbus_auth_test (const char *test_data_dir)
2364 if (test_data_dir == NULL)
2367 if (!process_test_dirs (test_data_dir))
2373 #endif /* DBUS_BUILD_TESTS */