1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-auth.h"
24 #include "dbus-string.h"
25 #include "dbus-list.h"
26 #include "dbus-internals.h"
27 #include "dbus-keyring.h"
29 #include "dbus-userdb.h"
32 * @defgroup DBusAuth Authentication
33 * @ingroup DBusInternals
34 * @brief DBusAuth object
36 * DBusAuth manages the authentication negotiation when a connection
37 * is first established, and also manage any encryption used over a
40 * @todo some SASL profiles require sending the empty string as a
41 * challenge/response, but we don't currently allow that in our
44 * @todo DBusAuth really needs to be rewritten as an explicit state
45 * machine. Right now it's too hard to prove to yourself by inspection
48 * @todo right now sometimes both ends will block waiting for input
49 * from the other end, e.g. if there's an error during
52 * @todo the cookie keyring needs to be cached globally not just
53 * per-auth (which raises threadsafety issues too)
55 * @todo grep FIXME in dbus-auth.c
59 * @defgroup DBusAuthInternals Authentication implementation details
60 * @ingroup DBusInternals
61 * @brief DBusAuth implementation details
63 * Private details of authentication code.
69 * Processes a command. Returns whether we had enough memory to
70 * complete the operation.
72 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
73 const DBusString *command,
74 const DBusString *args);
79 DBusProcessAuthCommandFunction func;
80 } DBusAuthCommandHandler;
83 * This function appends an initial client response to the given string
85 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
86 DBusString *response);
89 * This function processes a block of data received from the peer.
90 * i.e. handles a DATA command.
92 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
93 const DBusString *data);
96 * This function encodes a block of data from the peer.
98 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
99 const DBusString *data,
100 DBusString *encoded);
103 * This function decodes a block of data from the peer.
105 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
106 const DBusString *data,
107 DBusString *decoded);
110 * This function is called when the mechanism is abandoned.
112 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
116 const char *mechanism;
117 DBusAuthDataFunction server_data_func;
118 DBusAuthEncodeFunction server_encode_func;
119 DBusAuthDecodeFunction server_decode_func;
120 DBusAuthShutdownFunction server_shutdown_func;
121 DBusInitialResponseFunction client_initial_response_func;
122 DBusAuthDataFunction client_data_func;
123 DBusAuthEncodeFunction client_encode_func;
124 DBusAuthDecodeFunction client_decode_func;
125 DBusAuthShutdownFunction client_shutdown_func;
126 } DBusAuthMechanismHandler;
129 * Internal members of DBusAuth.
133 int refcount; /**< reference count */
135 DBusString incoming; /**< Incoming data buffer */
136 DBusString outgoing; /**< Outgoing data buffer */
138 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
140 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
142 DBusString identity; /**< Current identity we're authorizing
146 DBusCredentials credentials; /**< Credentials read from socket,
150 DBusCredentials authorized_identity; /**< Credentials that are authorized */
152 DBusCredentials desired_identity; /**< Identity client has requested */
154 DBusString context; /**< Cookie scope */
155 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
156 int cookie_id; /**< ID of cookie to use */
157 DBusString challenge; /**< Challenge sent to client */
159 char **allowed_mechs; /**< Mechanisms we're allowed to use,
160 * or #NULL if we can use any
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 * The name of the auth ("client" or "server")
266 * @param auth the auth conversation
269 #define DBUS_AUTH_NAME(auth) (DBUS_AUTH_IS_SERVER(auth) ? "server" : "client")
272 _dbus_auth_new (int size)
276 auth = dbus_malloc0 (size);
282 _dbus_credentials_clear (&auth->credentials);
283 _dbus_credentials_clear (&auth->authorized_identity);
284 _dbus_credentials_clear (&auth->desired_identity);
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))
300 if (!_dbus_string_init (&auth->outgoing))
303 if (!_dbus_string_init (&auth->identity))
306 if (!_dbus_string_init (&auth->context))
309 if (!_dbus_string_init (&auth->challenge))
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->outgoing);
327 _dbus_string_free (&auth->incoming);
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 _dbus_credentials_clear (&auth->authorized_identity);
343 _dbus_credentials_clear (&auth->desired_identity);
345 if (auth->mech != NULL)
347 _dbus_verbose ("%s: Shutting down mechanism %s\n",
348 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
350 if (DBUS_AUTH_IS_CLIENT (auth))
351 (* auth->mech->client_shutdown_func) (auth);
353 (* auth->mech->server_shutdown_func) (auth);
359 /* Returns TRUE but with an empty string hash if the
360 * cookie_id isn't known. As with all this code
361 * TRUE just means we had enough memory.
364 sha1_compute_hash (DBusAuth *auth,
366 const DBusString *server_challenge,
367 const DBusString *client_challenge,
374 _dbus_assert (auth->keyring != NULL);
378 if (!_dbus_string_init (&cookie))
381 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
385 if (_dbus_string_get_length (&cookie) == 0)
391 if (!_dbus_string_init (&to_hash))
394 if (!_dbus_string_copy (server_challenge, 0,
395 &to_hash, _dbus_string_get_length (&to_hash)))
398 if (!_dbus_string_append (&to_hash, ":"))
401 if (!_dbus_string_copy (client_challenge, 0,
402 &to_hash, _dbus_string_get_length (&to_hash)))
405 if (!_dbus_string_append (&to_hash, ":"))
408 if (!_dbus_string_copy (&cookie, 0,
409 &to_hash, _dbus_string_get_length (&to_hash)))
412 if (!_dbus_sha_compute (&to_hash, hash))
418 _dbus_string_zero (&to_hash);
419 _dbus_string_free (&to_hash);
421 _dbus_string_zero (&cookie);
422 _dbus_string_free (&cookie);
426 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
427 * entropy, we use 128. This is the number of bytes in the random
430 #define N_CHALLENGE_BYTES (128/8)
433 sha1_handle_first_client_response (DBusAuth *auth,
434 const DBusString *data)
436 /* We haven't sent a challenge yet, we're expecting a desired
437 * username from the client.
447 _dbus_string_set_length (&auth->challenge, 0);
449 if (_dbus_string_get_length (data) > 0)
451 if (_dbus_string_get_length (&auth->identity) > 0)
453 /* Tried to send two auth identities, wtf */
454 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
455 DBUS_AUTH_NAME (auth));
456 return send_rejected (auth);
460 /* this is our auth identity */
461 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
466 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
468 _dbus_verbose ("%s: Did not get a valid username from client\n",
469 DBUS_AUTH_NAME (auth));
470 return send_rejected (auth);
473 if (!_dbus_string_init (&tmp))
476 if (!_dbus_string_init (&tmp2))
478 _dbus_string_free (&tmp);
482 old_len = _dbus_string_get_length (&auth->outgoing);
484 /* we cache the keyring for speed, so here we drop it if it's the
485 * wrong one. FIXME caching the keyring here is useless since we use
486 * a different DBusAuth for every connection.
489 !_dbus_keyring_is_for_user (auth->keyring,
492 _dbus_keyring_unref (auth->keyring);
493 auth->keyring = NULL;
496 if (auth->keyring == NULL)
500 dbus_error_init (&error);
501 auth->keyring = _dbus_keyring_new_homedir (data,
505 if (auth->keyring == NULL)
507 if (dbus_error_has_name (&error,
508 DBUS_ERROR_NO_MEMORY))
510 dbus_error_free (&error);
515 _DBUS_ASSERT_ERROR_IS_SET (&error);
516 _dbus_verbose ("%s: Error loading keyring: %s\n",
517 DBUS_AUTH_NAME (auth), error.message);
518 if (send_rejected (auth))
519 retval = TRUE; /* retval is only about mem */
520 dbus_error_free (&error);
526 _dbus_assert (!dbus_error_is_set (&error));
530 _dbus_assert (auth->keyring != NULL);
532 dbus_error_init (&error);
533 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
534 if (auth->cookie_id < 0)
536 _DBUS_ASSERT_ERROR_IS_SET (&error);
537 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
538 DBUS_AUTH_NAME (auth), error.message);
539 if (send_rejected (auth))
541 dbus_error_free (&error);
546 _dbus_assert (!dbus_error_is_set (&error));
549 if (!_dbus_string_copy (&auth->context, 0,
550 &tmp2, _dbus_string_get_length (&tmp2)))
553 if (!_dbus_string_append (&tmp2, " "))
556 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
559 if (!_dbus_string_append (&tmp2, " "))
562 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
565 _dbus_string_set_length (&auth->challenge, 0);
566 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
569 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
570 _dbus_string_get_length (&tmp2)))
573 if (!_dbus_string_append (&auth->outgoing,
577 if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
578 _dbus_string_get_length (&auth->outgoing)))
581 if (!_dbus_string_append (&auth->outgoing,
588 _dbus_string_zero (&tmp);
589 _dbus_string_free (&tmp);
590 _dbus_string_zero (&tmp2);
591 _dbus_string_free (&tmp2);
593 _dbus_string_set_length (&auth->outgoing, old_len);
598 sha1_handle_second_client_response (DBusAuth *auth,
599 const DBusString *data)
601 /* We are expecting a response which is the hex-encoded client
602 * challenge, space, then SHA-1 hash of the concatenation of our
603 * challenge, ":", client challenge, ":", secret key, all
607 DBusString client_challenge;
608 DBusString client_hash;
610 DBusString correct_hash;
614 if (!_dbus_string_find_blank (data, 0, &i))
616 _dbus_verbose ("%s: no space separator in client response\n",
617 DBUS_AUTH_NAME (auth));
618 return send_rejected (auth);
621 if (!_dbus_string_init (&client_challenge))
624 if (!_dbus_string_init (&client_hash))
627 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
631 _dbus_string_skip_blank (data, i, &i);
633 if (!_dbus_string_copy_len (data, i,
634 _dbus_string_get_length (data) - i,
639 if (_dbus_string_get_length (&client_challenge) == 0 ||
640 _dbus_string_get_length (&client_hash) == 0)
642 _dbus_verbose ("%s: zero-length client challenge or hash\n",
643 DBUS_AUTH_NAME (auth));
644 if (send_rejected (auth))
649 if (!_dbus_string_init (&correct_hash))
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 ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
678 DBUS_AUTH_NAME (auth), 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_username_from_current_process (&username))
725 if (!_dbus_string_base64_encode (username, 0,
727 _dbus_string_get_length (response)))
737 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
738 const DBusString *data)
740 /* The data we get from the server should be the cookie context
741 * name, the cookie ID, and the server challenge, separated by
742 * spaces. We send back our challenge string and the correct hash.
746 DBusString cookie_id_str;
747 DBusString server_challenge;
748 DBusString client_challenge;
749 DBusString correct_hash;
757 if (!_dbus_string_find_blank (data, 0, &i))
759 if (_dbus_string_append (&auth->outgoing,
760 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
765 if (!_dbus_string_init (&context))
768 if (!_dbus_string_copy_len (data, 0, i,
772 _dbus_string_skip_blank (data, i, &i);
773 if (!_dbus_string_find_blank (data, i, &j))
775 if (_dbus_string_append (&auth->outgoing,
776 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
781 if (!_dbus_string_init (&cookie_id_str))
784 if (!_dbus_string_copy_len (data, i, j - i,
788 if (!_dbus_string_init (&server_challenge))
792 _dbus_string_skip_blank (data, i, &i);
793 j = _dbus_string_get_length (data);
795 if (!_dbus_string_copy_len (data, i, j - i,
796 &server_challenge, 0))
799 if (!_dbus_keyring_validate_context (&context))
801 if (_dbus_string_append (&auth->outgoing,
802 "ERROR \"Server sent invalid cookie context\"\r\n"))
807 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
809 if (_dbus_string_append (&auth->outgoing,
810 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
815 if (_dbus_string_get_length (&server_challenge) == 0)
817 if (_dbus_string_append (&auth->outgoing,
818 "ERROR \"Empty server challenge string\"\r\n"))
823 if (auth->keyring == NULL)
827 dbus_error_init (&error);
828 auth->keyring = _dbus_keyring_new_homedir (NULL,
832 if (auth->keyring == NULL)
834 if (dbus_error_has_name (&error,
835 DBUS_ERROR_NO_MEMORY))
837 dbus_error_free (&error);
842 _DBUS_ASSERT_ERROR_IS_SET (&error);
844 _dbus_verbose ("%s: Error loading keyring: %s\n",
845 DBUS_AUTH_NAME (auth), error.message);
847 if (_dbus_string_append (&auth->outgoing,
848 "ERROR \"Could not load cookie file\"\r\n"))
849 retval = TRUE; /* retval is only about mem */
851 dbus_error_free (&error);
857 _dbus_assert (!dbus_error_is_set (&error));
861 _dbus_assert (auth->keyring != NULL);
863 if (!_dbus_string_init (&tmp))
866 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
869 if (!_dbus_string_init (&client_challenge))
872 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
875 if (!_dbus_string_init (&correct_hash))
878 if (!sha1_compute_hash (auth, val,
884 if (_dbus_string_get_length (&correct_hash) == 0)
886 /* couldn't find the cookie ID or something */
887 if (_dbus_string_append (&auth->outgoing,
888 "ERROR \"Don't have the requested cookie ID\"\r\n"))
893 _dbus_string_set_length (&tmp, 0);
895 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
896 _dbus_string_get_length (&tmp)))
899 if (!_dbus_string_append (&tmp, " "))
902 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
903 _dbus_string_get_length (&tmp)))
906 old_len = _dbus_string_get_length (&auth->outgoing);
907 if (!_dbus_string_append (&auth->outgoing, "DATA "))
910 if (!_dbus_string_base64_encode (&tmp, 0,
912 _dbus_string_get_length (&auth->outgoing)))
914 _dbus_string_set_length (&auth->outgoing, old_len);
918 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
920 _dbus_string_set_length (&auth->outgoing, old_len);
927 _dbus_string_zero (&correct_hash);
928 _dbus_string_free (&correct_hash);
930 _dbus_string_free (&client_challenge);
932 _dbus_string_zero (&tmp);
933 _dbus_string_free (&tmp);
935 _dbus_string_free (&server_challenge);
937 _dbus_string_free (&cookie_id_str);
939 _dbus_string_free (&context);
945 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
947 auth->cookie_id = -1;
948 _dbus_string_set_length (&auth->challenge, 0);
952 handle_server_data_external_mech (DBusAuth *auth,
953 const DBusString *data)
955 if (auth->credentials.uid == DBUS_UID_UNSET)
957 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
958 DBUS_AUTH_NAME (auth));
959 return send_rejected (auth);
962 if (_dbus_string_get_length (data) > 0)
964 if (_dbus_string_get_length (&auth->identity) > 0)
966 /* Tried to send two auth identities, wtf */
967 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
968 DBUS_AUTH_NAME (auth));
969 return send_rejected (auth);
973 /* this is our auth identity */
974 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
979 /* Poke client for an auth identity, if none given */
980 if (_dbus_string_get_length (&auth->identity) == 0 &&
981 !auth->already_asked_for_initial_response)
983 if (_dbus_string_append (&auth->outgoing,
986 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
987 DBUS_AUTH_NAME (auth));
988 auth->already_asked_for_initial_response = TRUE;
995 _dbus_credentials_clear (&auth->desired_identity);
997 /* If auth->identity is still empty here, then client
998 * responded with an empty string after we poked it for
999 * an initial response. This means to try to auth the
1000 * identity provided in the credentials.
1002 if (_dbus_string_get_length (&auth->identity) == 0)
1004 auth->desired_identity.uid = auth->credentials.uid;
1008 if (!_dbus_uid_from_string (&auth->identity,
1009 &auth->desired_identity.uid))
1011 _dbus_verbose ("%s: could not get credentials from uid string\n",
1012 DBUS_AUTH_NAME (auth));
1013 return send_rejected (auth);
1017 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1019 _dbus_verbose ("%s: desired user %s is no good\n",
1020 DBUS_AUTH_NAME (auth),
1021 _dbus_string_get_const_data (&auth->identity));
1022 return send_rejected (auth);
1025 if (_dbus_credentials_match (&auth->desired_identity,
1026 &auth->credentials))
1028 /* client has authenticated */
1029 if (!_dbus_string_append (&auth->outgoing,
1033 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1034 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1035 DBUS_AUTH_NAME (auth),
1036 auth->desired_identity.uid,
1037 auth->credentials.uid);
1039 auth->authorized_identity.uid = auth->desired_identity.uid;
1041 auth->authenticated_pending_begin = TRUE;
1047 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1048 " gid="DBUS_GID_FORMAT
1049 " do not allow uid="DBUS_UID_FORMAT
1050 " gid="DBUS_GID_FORMAT"\n",
1051 DBUS_AUTH_NAME (auth),
1052 auth->credentials.uid, auth->credentials.gid,
1053 auth->desired_identity.uid, auth->desired_identity.gid);
1054 return send_rejected (auth);
1059 handle_server_shutdown_external_mech (DBusAuth *auth)
1065 handle_client_initial_response_external_mech (DBusAuth *auth,
1066 DBusString *response)
1068 /* We always append our UID as an initial response, so the server
1069 * doesn't have to send back an empty challenge to check whether we
1070 * want to specify an identity. i.e. this avoids a round trip that
1071 * the spec for the EXTERNAL mechanism otherwise requires.
1073 DBusString plaintext;
1075 if (!_dbus_string_init (&plaintext))
1078 if (!_dbus_string_append_uint (&plaintext,
1082 if (!_dbus_string_base64_encode (&plaintext, 0,
1084 _dbus_string_get_length (response)))
1087 _dbus_string_free (&plaintext);
1092 _dbus_string_free (&plaintext);
1097 handle_client_data_external_mech (DBusAuth *auth,
1098 const DBusString *data)
1105 handle_client_shutdown_external_mech (DBusAuth *auth)
1110 /* Put mechanisms here in order of preference.
1111 * What I eventually want to have is:
1113 * - a mechanism that checks UNIX domain socket credentials
1114 * - a simple magic cookie mechanism like X11 or ICE
1115 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1116 * offers such as Kerberos, X509, whatever.
1119 static const DBusAuthMechanismHandler
1120 all_mechanisms[] = {
1122 handle_server_data_external_mech,
1124 handle_server_shutdown_external_mech,
1125 handle_client_initial_response_external_mech,
1126 handle_client_data_external_mech,
1128 handle_client_shutdown_external_mech },
1129 { "DBUS_COOKIE_SHA1",
1130 handle_server_data_cookie_sha1_mech,
1132 handle_server_shutdown_cookie_sha1_mech,
1133 handle_client_initial_response_cookie_sha1_mech,
1134 handle_client_data_cookie_sha1_mech,
1136 handle_client_shutdown_cookie_sha1_mech },
1140 static const DBusAuthMechanismHandler*
1141 find_mech (const DBusString *name,
1142 char **allowed_mechs)
1146 if (allowed_mechs != NULL &&
1147 !_dbus_string_array_contains ((const char**) allowed_mechs,
1148 _dbus_string_get_const_data (name)))
1152 while (all_mechanisms[i].mechanism != NULL)
1154 if (_dbus_string_equal_c_str (name,
1155 all_mechanisms[i].mechanism))
1157 return &all_mechanisms[i];
1166 send_rejected (DBusAuth *auth)
1169 DBusAuthServer *server_auth;
1172 if (!_dbus_string_init (&command))
1175 if (!_dbus_string_append (&command,
1180 while (all_mechanisms[i].mechanism != NULL)
1182 if (!_dbus_string_append (&command,
1186 if (!_dbus_string_append (&command,
1187 all_mechanisms[i].mechanism))
1193 if (!_dbus_string_append (&command, "\r\n"))
1196 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1197 _dbus_string_get_length (&auth->outgoing)))
1200 shutdown_mech (auth);
1202 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1203 server_auth = DBUS_AUTH_SERVER (auth);
1204 server_auth->failures += 1;
1206 _dbus_string_free (&command);
1211 _dbus_string_free (&command);
1216 process_auth (DBusAuth *auth,
1217 const DBusString *command,
1218 const DBusString *args)
1222 /* We are already using a mechanism, client is on crack */
1223 if (!_dbus_string_append (&auth->outgoing,
1224 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1229 else if (_dbus_string_get_length (args) == 0)
1231 /* No args to the auth, send mechanisms */
1232 if (!send_rejected (auth))
1241 DBusString base64_response;
1242 DBusString decoded_response;
1244 _dbus_string_find_blank (args, 0, &i);
1246 if (!_dbus_string_init (&mech))
1249 if (!_dbus_string_init (&base64_response))
1251 _dbus_string_free (&mech);
1255 if (!_dbus_string_init (&decoded_response))
1257 _dbus_string_free (&mech);
1258 _dbus_string_free (&base64_response);
1262 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1265 if (!_dbus_string_copy (args, i, &base64_response, 0))
1268 if (!_dbus_string_base64_decode (&base64_response, 0,
1269 &decoded_response, 0))
1272 auth->mech = find_mech (&mech, auth->allowed_mechs);
1273 if (auth->mech != NULL)
1275 _dbus_verbose ("%s: Trying mechanism %s with initial response of %d bytes\n",
1276 DBUS_AUTH_NAME (auth),
1277 auth->mech->mechanism,
1278 _dbus_string_get_length (&decoded_response));
1280 if (!(* auth->mech->server_data_func) (auth,
1286 /* Unsupported mechanism */
1287 if (!send_rejected (auth))
1291 _dbus_string_free (&mech);
1292 _dbus_string_free (&base64_response);
1293 _dbus_string_free (&decoded_response);
1299 _dbus_string_free (&mech);
1300 _dbus_string_free (&base64_response);
1301 _dbus_string_free (&decoded_response);
1307 process_cancel (DBusAuth *auth,
1308 const DBusString *command,
1309 const DBusString *args)
1311 if (!send_rejected (auth))
1318 process_begin (DBusAuth *auth,
1319 const DBusString *command,
1320 const DBusString *args)
1322 if (auth->authenticated_pending_begin)
1323 auth->authenticated = TRUE;
1326 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1329 shutdown_mech (auth);
1336 process_data_server (DBusAuth *auth,
1337 const DBusString *command,
1338 const DBusString *args)
1340 if (auth->mech != NULL)
1344 if (!_dbus_string_init (&decoded))
1347 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1349 _dbus_string_free (&decoded);
1353 #ifdef DBUS_ENABLE_VERBOSE_MODE
1354 if (_dbus_string_validate_ascii (&decoded, 0,
1355 _dbus_string_get_length (&decoded)))
1356 _dbus_verbose ("%s: data: '%s'\n",
1357 DBUS_AUTH_NAME (auth),
1358 _dbus_string_get_const_data (&decoded));
1361 if (!(* auth->mech->server_data_func) (auth, &decoded))
1363 _dbus_string_free (&decoded);
1367 _dbus_string_free (&decoded);
1371 if (!_dbus_string_append (&auth->outgoing,
1372 "ERROR \"Not currently in an auth conversation\"\r\n"))
1380 process_error_server (DBusAuth *auth,
1381 const DBusString *command,
1382 const DBusString *args)
1384 /* Server got error from client, reject the auth,
1385 * as we don't have anything more intelligent to do.
1387 if (!send_rejected (auth))
1393 /* return FALSE if no memory, TRUE if all OK */
1395 get_word (const DBusString *str,
1401 _dbus_string_skip_blank (str, *start, start);
1402 _dbus_string_find_blank (str, *start, &i);
1406 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1416 record_mechanisms (DBusAuth *auth,
1417 const DBusString *command,
1418 const DBusString *args)
1423 if (auth->already_got_mechanisms)
1426 len = _dbus_string_get_length (args);
1432 const DBusAuthMechanismHandler *mech;
1434 if (!_dbus_string_init (&m))
1437 if (!get_word (args, &next, &m))
1439 _dbus_string_free (&m);
1443 mech = find_mech (&m, auth->allowed_mechs);
1447 /* FIXME right now we try mechanisms in the order
1448 * the server lists them; should we do them in
1449 * some more deterministic order?
1451 * Probably in all_mechanisms order, our order of
1452 * preference. Of course when the server is us,
1453 * it lists things in that order anyhow.
1456 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1457 DBUS_AUTH_NAME (auth), mech->mechanism);
1459 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1462 _dbus_string_free (&m);
1468 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1469 DBUS_AUTH_NAME (auth),
1470 _dbus_string_get_const_data (&m));
1473 _dbus_string_free (&m);
1476 auth->already_got_mechanisms = TRUE;
1481 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1487 client_try_next_mechanism (DBusAuth *auth)
1489 const DBusAuthMechanismHandler *mech;
1490 DBusString auth_command;
1491 DBusAuthClient *client;
1493 client = DBUS_AUTH_CLIENT (auth);
1495 /* Pop any mechs not in the list of allowed mechanisms */
1497 while (client->mechs_to_try != NULL)
1499 mech = client->mechs_to_try->data;
1501 if (auth->allowed_mechs != NULL &&
1502 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1505 /* don't try this one after all */
1506 _dbus_verbose ("%s: Mechanism %s isn't in the list of allowed mechanisms\n",
1507 DBUS_AUTH_NAME (auth), mech->mechanism);
1509 _dbus_list_pop_first (& client->mechs_to_try);
1512 break; /* we'll try this one */
1518 if (!_dbus_string_init (&auth_command))
1521 if (!_dbus_string_append (&auth_command,
1524 _dbus_string_free (&auth_command);
1528 if (!_dbus_string_append (&auth_command,
1531 _dbus_string_free (&auth_command);
1535 if (mech->client_initial_response_func != NULL)
1537 if (!_dbus_string_append (&auth_command, " "))
1539 _dbus_string_free (&auth_command);
1543 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1545 _dbus_string_free (&auth_command);
1550 if (!_dbus_string_append (&auth_command,
1553 _dbus_string_free (&auth_command);
1557 if (!_dbus_string_copy (&auth_command, 0,
1559 _dbus_string_get_length (&auth->outgoing)))
1561 _dbus_string_free (&auth_command);
1566 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1568 _dbus_verbose ("%s: Trying mechanism %s\n",
1569 DBUS_AUTH_NAME (auth),
1570 auth->mech->mechanism);
1572 _dbus_string_free (&auth_command);
1578 process_rejected (DBusAuth *auth,
1579 const DBusString *command,
1580 const DBusString *args)
1582 shutdown_mech (auth);
1584 if (!auth->already_got_mechanisms)
1586 if (!record_mechanisms (auth, command, args))
1590 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1592 if (!client_try_next_mechanism (auth))
1598 auth->need_disconnect = TRUE;
1605 process_ok (DBusAuth *auth,
1606 const DBusString *command,
1607 const DBusString *args)
1609 if (!_dbus_string_append (&auth->outgoing,
1613 auth->authenticated_pending_output = TRUE;
1619 process_data_client (DBusAuth *auth,
1620 const DBusString *command,
1621 const DBusString *args)
1623 if (auth->mech != NULL)
1627 if (!_dbus_string_init (&decoded))
1630 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1632 _dbus_string_free (&decoded);
1636 #ifdef DBUS_ENABLE_VERBOSE_MODE
1637 if (_dbus_string_validate_ascii (&decoded, 0,
1638 _dbus_string_get_length (&decoded)))
1640 _dbus_verbose ("%s: data: '%s'\n",
1641 DBUS_AUTH_NAME (auth),
1642 _dbus_string_get_const_data (&decoded));
1646 if (!(* auth->mech->client_data_func) (auth, &decoded))
1648 _dbus_string_free (&decoded);
1652 _dbus_string_free (&decoded);
1656 if (!_dbus_string_append (&auth->outgoing,
1657 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1665 process_error_client (DBusAuth *auth,
1666 const DBusString *command,
1667 const DBusString *args)
1669 /* Cancel current mechanism, as we don't have anything
1670 * more clever to do.
1672 if (!_dbus_string_append (&auth->outgoing,
1680 process_unknown (DBusAuth *auth,
1681 const DBusString *command,
1682 const DBusString *args)
1684 if (!_dbus_string_append (&auth->outgoing,
1685 "ERROR \"Unknown command\"\r\n"))
1691 /* returns whether to call it again right away */
1693 process_command (DBusAuth *auth)
1701 /* _dbus_verbose ("%s: trying process_command()\n"); */
1706 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1709 if (!_dbus_string_init (&command))
1711 auth->needed_memory = TRUE;
1715 if (!_dbus_string_init (&args))
1717 _dbus_string_free (&command);
1718 auth->needed_memory = TRUE;
1722 if (eol > _DBUS_ONE_MEGABYTE)
1724 /* This is a giant line, someone is trying to hose us. */
1725 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1731 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1734 if (!_dbus_string_validate_ascii (&command, 0,
1735 _dbus_string_get_length (&command)))
1737 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1738 DBUS_AUTH_NAME (auth));
1739 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1745 _dbus_verbose ("%s: got command \"%s\"\n",
1746 DBUS_AUTH_NAME (auth),
1747 _dbus_string_get_const_data (&command));
1749 _dbus_string_find_blank (&command, 0, &i);
1750 _dbus_string_skip_blank (&command, i, &j);
1753 _dbus_string_delete (&command, i, j - i);
1755 if (!_dbus_string_move (&command, i, &args, 0))
1759 while (auth->handlers[i].command != NULL)
1761 if (_dbus_string_equal_c_str (&command,
1762 auth->handlers[i].command))
1764 _dbus_verbose ("%s: Processing auth command %s\n",
1765 DBUS_AUTH_NAME (auth),
1766 auth->handlers[i].command);
1768 if (!(* auth->handlers[i].func) (auth, &command, &args))
1776 if (auth->handlers[i].command == NULL)
1778 if (!process_unknown (auth, &command, &args))
1784 /* We've succeeded in processing the whole command so drop it out
1785 * of the incoming buffer and return TRUE to try another command.
1788 _dbus_string_delete (&auth->incoming, 0, eol);
1791 _dbus_string_delete (&auth->incoming, 0, 2);
1796 _dbus_string_free (&args);
1797 _dbus_string_free (&command);
1800 auth->needed_memory = TRUE;
1802 auth->needed_memory = FALSE;
1811 * @addtogroup DBusAuth
1816 * Creates a new auth conversation object for the server side.
1817 * See doc/dbus-sasl-profile.txt for full details on what
1820 * @returns the new object or #NULL if no memory
1823 _dbus_auth_server_new (void)
1826 DBusAuthServer *server_auth;
1828 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1832 auth->handlers = server_handlers;
1834 server_auth = DBUS_AUTH_SERVER (auth);
1836 /* perhaps this should be per-mechanism with a lower
1839 server_auth->failures = 0;
1840 server_auth->max_failures = 6;
1846 * Creates a new auth conversation object for the client side.
1847 * See doc/dbus-sasl-profile.txt for full details on what
1850 * @returns the new object or #NULL if no memory
1853 _dbus_auth_client_new (void)
1857 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1861 auth->handlers = client_handlers;
1863 /* Add a default mechanism to try */
1864 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1865 (void*) &all_mechanisms[0]))
1867 _dbus_auth_unref (auth);
1871 /* Now try the mechanism we just added */
1872 if (!client_try_next_mechanism (auth))
1874 _dbus_auth_unref (auth);
1882 * Increments the refcount of an auth object.
1884 * @param auth the auth conversation
1887 _dbus_auth_ref (DBusAuth *auth)
1889 _dbus_assert (auth != NULL);
1891 auth->refcount += 1;
1895 * Decrements the refcount of an auth object.
1897 * @param auth the auth conversation
1900 _dbus_auth_unref (DBusAuth *auth)
1902 _dbus_assert (auth != NULL);
1903 _dbus_assert (auth->refcount > 0);
1905 auth->refcount -= 1;
1906 if (auth->refcount == 0)
1908 shutdown_mech (auth);
1910 if (DBUS_AUTH_IS_CLIENT (auth))
1912 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1916 _dbus_keyring_unref (auth->keyring);
1918 _dbus_string_free (&auth->context);
1919 _dbus_string_free (&auth->challenge);
1920 _dbus_string_free (&auth->identity);
1921 _dbus_string_free (&auth->incoming);
1922 _dbus_string_free (&auth->outgoing);
1924 dbus_free_string_array (auth->allowed_mechs);
1931 * Sets an array of authentication mechanism names
1932 * that we are willing to use.
1934 * @param auth the auth conversation
1935 * @param mechanisms #NULL-terminated array of mechanism names
1936 * @returns #FALSE if no memory
1939 _dbus_auth_set_mechanisms (DBusAuth *auth,
1940 const char **mechanisms)
1944 if (mechanisms != NULL)
1946 copy = _dbus_dup_string_array (mechanisms);
1953 dbus_free_string_array (auth->allowed_mechs);
1955 auth->allowed_mechs = copy;
1961 * @param auth the auth conversation object
1962 * @returns #TRUE if we're in a final state
1964 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1967 * Analyzes buffered input and moves the auth conversation forward,
1968 * returning the new state of the auth conversation.
1970 * @param auth the auth conversation
1971 * @returns the new state
1974 _dbus_auth_do_work (DBusAuth *auth)
1976 auth->needed_memory = FALSE;
1978 /* Max amount we'll buffer up before deciding someone's on crack */
1979 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1983 if (DBUS_AUTH_IN_END_STATE (auth))
1986 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1987 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1989 auth->need_disconnect = TRUE;
1990 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
1991 DBUS_AUTH_NAME (auth));
1995 if (auth->mech == NULL &&
1996 auth->already_got_mechanisms &&
1997 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1999 auth->need_disconnect = TRUE;
2000 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2001 DBUS_AUTH_NAME (auth));
2005 while (process_command (auth));
2007 if (DBUS_AUTH_IS_SERVER (auth) &&
2008 DBUS_AUTH_SERVER (auth)->failures >=
2009 DBUS_AUTH_SERVER (auth)->max_failures)
2010 auth->need_disconnect = TRUE;
2012 if (auth->need_disconnect)
2013 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2014 else if (auth->authenticated)
2016 if (_dbus_string_get_length (&auth->incoming) > 0)
2017 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
2019 return DBUS_AUTH_STATE_AUTHENTICATED;
2021 else if (auth->needed_memory)
2022 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2023 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2024 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2026 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2030 * Gets bytes that need to be sent to the peer we're conversing with.
2031 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2032 * to notify the auth object that they were written.
2034 * @param auth the auth conversation
2035 * @param str return location for a ref to the buffer to send
2036 * @returns #FALSE if nothing to send
2039 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2040 const DBusString **str)
2042 _dbus_assert (auth != NULL);
2043 _dbus_assert (str != NULL);
2047 if (DBUS_AUTH_IN_END_STATE (auth))
2050 if (_dbus_string_get_length (&auth->outgoing) == 0)
2053 *str = &auth->outgoing;
2059 * Notifies the auth conversation object that
2060 * the given number of bytes of the outgoing buffer
2061 * have been written out.
2063 * @param auth the auth conversation
2064 * @param bytes_sent number of bytes written out
2067 _dbus_auth_bytes_sent (DBusAuth *auth,
2070 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2071 DBUS_AUTH_NAME (auth),
2073 _dbus_string_get_const_data (&auth->outgoing));
2075 _dbus_string_delete (&auth->outgoing,
2078 if (auth->authenticated_pending_output &&
2079 _dbus_string_get_length (&auth->outgoing) == 0)
2080 auth->authenticated = TRUE;
2084 * Get a buffer to be used for reading bytes from the peer we're conversing
2085 * with. Bytes should be appended to this buffer.
2087 * @param auth the auth conversation
2088 * @param buffer return location for buffer to append bytes to
2091 _dbus_auth_get_buffer (DBusAuth *auth,
2092 DBusString **buffer)
2094 _dbus_assert (auth != NULL);
2095 _dbus_assert (!auth->buffer_outstanding);
2097 *buffer = &auth->incoming;
2099 auth->buffer_outstanding = TRUE;
2103 * Returns a buffer with new data read into it.
2105 * @param auth the auth conversation
2106 * @param buffer the buffer being returned
2107 * @param bytes_read number of new bytes added
2110 _dbus_auth_return_buffer (DBusAuth *auth,
2114 _dbus_assert (buffer == &auth->incoming);
2115 _dbus_assert (auth->buffer_outstanding);
2117 auth->buffer_outstanding = FALSE;
2121 * Returns leftover bytes that were not used as part of the auth
2122 * conversation. These bytes will be part of the message stream
2123 * instead. This function may not be called until authentication has
2126 * @param auth the auth conversation
2127 * @param str return location for pointer to string of unused bytes
2130 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2131 const DBusString **str)
2133 if (!DBUS_AUTH_IN_END_STATE (auth))
2136 *str = &auth->incoming;
2141 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2142 * after we've gotten them and successfully moved them elsewhere.
2144 * @param auth the auth conversation
2147 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2149 if (!DBUS_AUTH_IN_END_STATE (auth))
2152 _dbus_string_set_length (&auth->incoming, 0);
2156 * Called post-authentication, indicates whether we need to encode
2157 * the message stream with _dbus_auth_encode_data() prior to
2158 * sending it to the peer.
2160 * @param auth the auth conversation
2161 * @returns #TRUE if we need to encode the stream
2164 _dbus_auth_needs_encoding (DBusAuth *auth)
2166 if (!auth->authenticated)
2169 if (auth->mech != NULL)
2171 if (DBUS_AUTH_IS_CLIENT (auth))
2172 return auth->mech->client_encode_func != NULL;
2174 return auth->mech->server_encode_func != NULL;
2181 * Called post-authentication, encodes a block of bytes for sending to
2182 * the peer. If no encoding was negotiated, just copies the bytes
2183 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2185 * @param auth the auth conversation
2186 * @param plaintext the plain text data
2187 * @param encoded initialized string to where encoded data is appended
2188 * @returns #TRUE if we had enough memory and successfully encoded
2191 _dbus_auth_encode_data (DBusAuth *auth,
2192 const DBusString *plaintext,
2193 DBusString *encoded)
2195 _dbus_assert (plaintext != encoded);
2197 if (!auth->authenticated)
2200 if (_dbus_auth_needs_encoding (auth))
2202 if (DBUS_AUTH_IS_CLIENT (auth))
2203 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2205 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2209 return _dbus_string_copy (plaintext, 0, encoded,
2210 _dbus_string_get_length (encoded));
2215 * Called post-authentication, indicates whether we need to decode
2216 * the message stream with _dbus_auth_decode_data() after
2217 * receiving it from the peer.
2219 * @param auth the auth conversation
2220 * @returns #TRUE if we need to encode the stream
2223 _dbus_auth_needs_decoding (DBusAuth *auth)
2225 if (!auth->authenticated)
2228 if (auth->mech != NULL)
2230 if (DBUS_AUTH_IS_CLIENT (auth))
2231 return auth->mech->client_decode_func != NULL;
2233 return auth->mech->server_decode_func != NULL;
2241 * Called post-authentication, decodes a block of bytes received from
2242 * the peer. If no encoding was negotiated, just copies the bytes (you
2243 * can avoid this by checking _dbus_auth_needs_decoding()).
2245 * @todo We need to be able to distinguish "out of memory" error
2246 * from "the data is hosed" error.
2248 * @param auth the auth conversation
2249 * @param encoded the encoded data
2250 * @param plaintext initialized string where decoded data is appended
2251 * @returns #TRUE if we had enough memory and successfully decoded
2254 _dbus_auth_decode_data (DBusAuth *auth,
2255 const DBusString *encoded,
2256 DBusString *plaintext)
2258 _dbus_assert (plaintext != encoded);
2260 if (!auth->authenticated)
2263 if (_dbus_auth_needs_decoding (auth))
2265 if (DBUS_AUTH_IS_CLIENT (auth))
2266 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2268 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2272 return _dbus_string_copy (encoded, 0, plaintext,
2273 _dbus_string_get_length (plaintext));
2278 * Sets credentials received via reliable means from the operating
2281 * @param auth the auth conversation
2282 * @param credentials the credentials received
2285 _dbus_auth_set_credentials (DBusAuth *auth,
2286 const DBusCredentials *credentials)
2288 auth->credentials = *credentials;
2292 * Gets the identity we authorized the client as. Apps may have
2293 * different policies as to what identities they allow.
2295 * @param auth the auth conversation
2296 * @param credentials the credentials we've authorized
2299 _dbus_auth_get_identity (DBusAuth *auth,
2300 DBusCredentials *credentials)
2302 if (auth->authenticated)
2303 *credentials = auth->authorized_identity;
2305 _dbus_credentials_clear (credentials);
2309 * Sets the "authentication context" which scopes cookies
2310 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2312 * @param auth the auth conversation
2313 * @param context the context
2314 * @returns #FALSE if no memory
2317 _dbus_auth_set_context (DBusAuth *auth,
2318 const DBusString *context)
2320 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2321 &auth->context, 0, _dbus_string_get_length (context));
2326 #ifdef DBUS_BUILD_TESTS
2327 #include "dbus-test.h"
2328 #include "dbus-auth-script.h"
2332 process_test_subdir (const DBusString *test_base_dir,
2335 DBusString test_directory;
2336 DBusString filename;
2344 if (!_dbus_string_init (&test_directory))
2345 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2347 _dbus_string_init_const (&filename, subdir);
2349 if (!_dbus_string_copy (test_base_dir, 0,
2350 &test_directory, 0))
2351 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2353 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2354 _dbus_assert_not_reached ("couldn't allocate full path");
2356 _dbus_string_free (&filename);
2357 if (!_dbus_string_init (&filename))
2358 _dbus_assert_not_reached ("didn't allocate filename string\n");
2360 dbus_error_init (&error);
2361 dir = _dbus_directory_open (&test_directory, &error);
2364 _dbus_warn ("Could not open %s: %s\n",
2365 _dbus_string_get_const_data (&test_directory),
2367 dbus_error_free (&error);
2371 printf ("Testing:\n");
2374 while (_dbus_directory_get_next_file (dir, &filename, &error))
2376 DBusString full_path;
2378 if (!_dbus_string_init (&full_path))
2379 _dbus_assert_not_reached ("couldn't init string");
2381 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2382 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2384 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2385 _dbus_assert_not_reached ("couldn't concat file to dir");
2387 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2389 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2390 _dbus_string_get_const_data (&filename));
2391 _dbus_string_free (&full_path);
2395 printf (" %s\n", _dbus_string_get_const_data (&filename));
2397 if (!_dbus_auth_script_run (&full_path))
2399 _dbus_string_free (&full_path);
2403 _dbus_string_free (&full_path);
2406 if (dbus_error_is_set (&error))
2408 _dbus_warn ("Could not get next file in %s: %s\n",
2409 _dbus_string_get_const_data (&test_directory), error.message);
2410 dbus_error_free (&error);
2419 _dbus_directory_close (dir);
2420 _dbus_string_free (&test_directory);
2421 _dbus_string_free (&filename);
2427 process_test_dirs (const char *test_data_dir)
2429 DBusString test_directory;
2434 _dbus_string_init_const (&test_directory, test_data_dir);
2436 if (!process_test_subdir (&test_directory, "auth"))
2443 _dbus_string_free (&test_directory);
2449 _dbus_auth_test (const char *test_data_dir)
2452 if (test_data_dir == NULL)
2455 if (!process_test_dirs (test_data_dir))
2461 #endif /* DBUS_BUILD_TESTS */