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"
31 /* See doc/dbus-sasl-profile.txt */
34 * @defgroup DBusAuth Authentication
35 * @ingroup DBusInternals
36 * @brief DBusAuth object
38 * DBusAuth manages the authentication negotiation when a connection
39 * is first established, and also manage any encryption used over a
42 * @todo some SASL profiles require sending the empty string as a
43 * challenge/response, but we don't currently allow that in our
46 * @todo DBusAuth really needs to be rewritten as an explicit state
47 * machine. Right now it's too hard to prove to yourself by inspection
50 * @todo right now sometimes both ends will block waiting for input
51 * from the other end, e.g. if there's an error during
54 * @todo the cookie keyring needs to be cached globally not just
55 * per-auth (which raises threadsafety issues too)
57 * @todo grep FIXME in dbus-auth.c
61 * @defgroup DBusAuthInternals Authentication implementation details
62 * @ingroup DBusInternals
63 * @brief DBusAuth implementation details
65 * Private details of authentication code.
71 * Processes a command. Returns whether we had enough memory to
72 * complete the operation.
74 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
75 const DBusString *command,
76 const DBusString *args);
81 DBusProcessAuthCommandFunction func;
82 } DBusAuthCommandHandler;
85 * This function appends an initial client response to the given string
87 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
88 DBusString *response);
91 * This function processes a block of data received from the peer.
92 * i.e. handles a DATA command.
94 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
95 const DBusString *data);
98 * This function encodes a block of data from the peer.
100 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
101 const DBusString *data,
102 DBusString *encoded);
105 * This function decodes a block of data from the peer.
107 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
108 const DBusString *data,
109 DBusString *decoded);
112 * This function is called when the mechanism is abandoned.
114 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
118 const char *mechanism;
119 DBusAuthDataFunction server_data_func;
120 DBusAuthEncodeFunction server_encode_func;
121 DBusAuthDecodeFunction server_decode_func;
122 DBusAuthShutdownFunction server_shutdown_func;
123 DBusInitialResponseFunction client_initial_response_func;
124 DBusAuthDataFunction client_data_func;
125 DBusAuthEncodeFunction client_encode_func;
126 DBusAuthDecodeFunction client_decode_func;
127 DBusAuthShutdownFunction client_shutdown_func;
128 } DBusAuthMechanismHandler;
131 * Internal members of DBusAuth.
135 int refcount; /**< reference count */
137 DBusString incoming; /**< Incoming data buffer */
138 DBusString outgoing; /**< Outgoing data buffer */
140 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
142 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
144 DBusString identity; /**< Current identity we're authorizing
148 DBusCredentials credentials; /**< Credentials read from socket,
152 DBusCredentials authorized_identity; /**< Credentials that are authorized */
154 DBusCredentials desired_identity; /**< Identity client has requested */
156 DBusString context; /**< Cookie scope */
157 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
158 int cookie_id; /**< ID of cookie to use */
159 DBusString challenge; /**< Challenge sent to client */
161 char **allowed_mechs; /**< Mechanisms we're allowed to use,
162 * or #NULL if we can use any
165 unsigned int needed_memory : 1; /**< We needed memory to continue since last
166 * successful getting something done
168 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
169 unsigned int authenticated : 1; /**< We are authenticated */
170 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
171 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
172 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
173 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
174 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
181 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
189 int failures; /**< Number of times client has been rejected */
190 int max_failures; /**< Number of times we reject before disconnect */
194 static dbus_bool_t process_auth (DBusAuth *auth,
195 const DBusString *command,
196 const DBusString *args);
197 static dbus_bool_t process_cancel (DBusAuth *auth,
198 const DBusString *command,
199 const DBusString *args);
200 static dbus_bool_t process_begin (DBusAuth *auth,
201 const DBusString *command,
202 const DBusString *args);
203 static dbus_bool_t process_data_server (DBusAuth *auth,
204 const DBusString *command,
205 const DBusString *args);
206 static dbus_bool_t process_error_server (DBusAuth *auth,
207 const DBusString *command,
208 const DBusString *args);
209 static dbus_bool_t process_rejected (DBusAuth *auth,
210 const DBusString *command,
211 const DBusString *args);
212 static dbus_bool_t process_ok (DBusAuth *auth,
213 const DBusString *command,
214 const DBusString *args);
215 static dbus_bool_t process_data_client (DBusAuth *auth,
216 const DBusString *command,
217 const DBusString *args);
218 static dbus_bool_t process_error_client (DBusAuth *auth,
219 const DBusString *command,
220 const DBusString *args);
223 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
224 static dbus_bool_t send_rejected (DBusAuth *auth);
226 static DBusAuthCommandHandler
227 server_handlers[] = {
228 { "AUTH", process_auth },
229 { "CANCEL", process_cancel },
230 { "BEGIN", process_begin },
231 { "DATA", process_data_server },
232 { "ERROR", process_error_server },
236 static DBusAuthCommandHandler
237 client_handlers[] = {
238 { "REJECTED", process_rejected },
239 { "OK", process_ok },
240 { "DATA", process_data_client },
241 { "ERROR", process_error_client },
246 * @param auth the auth conversation
247 * @returns #TRUE if the conversation is the server side
249 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
251 * @param auth the auth conversation
252 * @returns #TRUE if the conversation is the client side
254 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
256 * @param auth the auth conversation
257 * @returns auth cast to DBusAuthClient
259 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
261 * @param auth the auth conversation
262 * @returns auth cast to DBusAuthServer
264 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
267 * The name of the auth ("client" or "server")
268 * @param auth the auth conversation
271 #define DBUS_AUTH_NAME(auth) (DBUS_AUTH_IS_SERVER(auth) ? "server" : "client")
274 _dbus_auth_new (int size)
278 auth = dbus_malloc0 (size);
284 _dbus_credentials_clear (&auth->credentials);
285 _dbus_credentials_clear (&auth->authorized_identity);
286 _dbus_credentials_clear (&auth->desired_identity);
288 auth->keyring = NULL;
289 auth->cookie_id = -1;
291 /* note that we don't use the max string length feature,
292 * because you can't use that feature if you're going to
293 * try to recover from out-of-memory (it creates
294 * what looks like unrecoverable inability to alloc
295 * more space in the string). But we do handle
296 * overlong buffers in _dbus_auth_do_work().
299 if (!_dbus_string_init (&auth->incoming))
302 if (!_dbus_string_init (&auth->outgoing))
305 if (!_dbus_string_init (&auth->identity))
308 if (!_dbus_string_init (&auth->context))
311 if (!_dbus_string_init (&auth->challenge))
314 /* default context if none is specified */
315 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
321 _dbus_string_free (&auth->challenge);
323 _dbus_string_free (&auth->context);
325 _dbus_string_free (&auth->identity);
327 _dbus_string_free (&auth->outgoing);
329 _dbus_string_free (&auth->incoming);
336 shutdown_mech (DBusAuth *auth)
338 /* Cancel any auth */
339 auth->authenticated_pending_begin = FALSE;
340 auth->authenticated = FALSE;
341 auth->already_asked_for_initial_response = FALSE;
342 _dbus_string_set_length (&auth->identity, 0);
344 _dbus_credentials_clear (&auth->authorized_identity);
345 _dbus_credentials_clear (&auth->desired_identity);
347 if (auth->mech != NULL)
349 _dbus_verbose ("%s: Shutting down mechanism %s\n",
350 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
352 if (DBUS_AUTH_IS_CLIENT (auth))
353 (* auth->mech->client_shutdown_func) (auth);
355 (* auth->mech->server_shutdown_func) (auth);
361 /* Returns TRUE but with an empty string hash if the
362 * cookie_id isn't known. As with all this code
363 * TRUE just means we had enough memory.
366 sha1_compute_hash (DBusAuth *auth,
368 const DBusString *server_challenge,
369 const DBusString *client_challenge,
376 _dbus_assert (auth->keyring != NULL);
380 if (!_dbus_string_init (&cookie))
383 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
387 if (_dbus_string_get_length (&cookie) == 0)
393 if (!_dbus_string_init (&to_hash))
396 if (!_dbus_string_copy (server_challenge, 0,
397 &to_hash, _dbus_string_get_length (&to_hash)))
400 if (!_dbus_string_append (&to_hash, ":"))
403 if (!_dbus_string_copy (client_challenge, 0,
404 &to_hash, _dbus_string_get_length (&to_hash)))
407 if (!_dbus_string_append (&to_hash, ":"))
410 if (!_dbus_string_copy (&cookie, 0,
411 &to_hash, _dbus_string_get_length (&to_hash)))
414 if (!_dbus_sha_compute (&to_hash, hash))
420 _dbus_string_zero (&to_hash);
421 _dbus_string_free (&to_hash);
423 _dbus_string_zero (&cookie);
424 _dbus_string_free (&cookie);
428 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
429 * entropy, we use 128. This is the number of bytes in the random
432 #define N_CHALLENGE_BYTES (128/8)
435 sha1_handle_first_client_response (DBusAuth *auth,
436 const DBusString *data)
438 /* We haven't sent a challenge yet, we're expecting a desired
439 * username from the client.
449 _dbus_string_set_length (&auth->challenge, 0);
451 if (_dbus_string_get_length (data) > 0)
453 if (_dbus_string_get_length (&auth->identity) > 0)
455 /* Tried to send two auth identities, wtf */
456 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
457 DBUS_AUTH_NAME (auth));
458 return send_rejected (auth);
462 /* this is our auth identity */
463 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
468 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
470 _dbus_verbose ("%s: Did not get a valid username from client\n",
471 DBUS_AUTH_NAME (auth));
472 return send_rejected (auth);
475 if (!_dbus_string_init (&tmp))
478 if (!_dbus_string_init (&tmp2))
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 ("%s: Error loading keyring: %s\n",
519 DBUS_AUTH_NAME (auth), error.message);
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 ("%s: Could not get a cookie ID to send to client: %s\n",
540 DBUS_AUTH_NAME (auth), error.message);
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 ("%s: no space separator in client response\n",
619 DBUS_AUTH_NAME (auth));
620 return send_rejected (auth);
623 if (!_dbus_string_init (&client_challenge))
626 if (!_dbus_string_init (&client_hash))
629 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
633 _dbus_string_skip_blank (data, i, &i);
635 if (!_dbus_string_copy_len (data, i,
636 _dbus_string_get_length (data) - i,
641 if (_dbus_string_get_length (&client_challenge) == 0 ||
642 _dbus_string_get_length (&client_hash) == 0)
644 _dbus_verbose ("%s: zero-length client challenge or hash\n",
645 DBUS_AUTH_NAME (auth));
646 if (send_rejected (auth))
651 if (!_dbus_string_init (&correct_hash))
654 if (!sha1_compute_hash (auth, auth->cookie_id,
660 /* if cookie_id was invalid, then we get an empty hash */
661 if (_dbus_string_get_length (&correct_hash) == 0)
663 if (send_rejected (auth))
668 if (!_dbus_string_equal (&client_hash, &correct_hash))
670 if (send_rejected (auth))
675 if (!_dbus_string_append (&auth->outgoing,
679 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
680 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
682 auth->authorized_identity = auth->desired_identity;
683 auth->authenticated_pending_begin = TRUE;
687 _dbus_string_zero (&correct_hash);
688 _dbus_string_free (&correct_hash);
690 _dbus_string_zero (&client_hash);
691 _dbus_string_free (&client_hash);
693 _dbus_string_free (&client_challenge);
699 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
700 const DBusString *data)
702 if (auth->cookie_id < 0)
703 return sha1_handle_first_client_response (auth, data);
705 return sha1_handle_second_client_response (auth, data);
709 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
711 auth->cookie_id = -1;
712 _dbus_string_set_length (&auth->challenge, 0);
716 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
717 DBusString *response)
719 const DBusString *username;
724 if (!_dbus_username_from_current_process (&username))
727 if (!_dbus_string_base64_encode (username, 0,
729 _dbus_string_get_length (response)))
739 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
740 const DBusString *data)
742 /* The data we get from the server should be the cookie context
743 * name, the cookie ID, and the server challenge, separated by
744 * spaces. We send back our challenge string and the correct hash.
748 DBusString cookie_id_str;
749 DBusString server_challenge;
750 DBusString client_challenge;
751 DBusString correct_hash;
759 if (!_dbus_string_find_blank (data, 0, &i))
761 if (_dbus_string_append (&auth->outgoing,
762 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
767 if (!_dbus_string_init (&context))
770 if (!_dbus_string_copy_len (data, 0, i,
774 _dbus_string_skip_blank (data, i, &i);
775 if (!_dbus_string_find_blank (data, i, &j))
777 if (_dbus_string_append (&auth->outgoing,
778 "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
783 if (!_dbus_string_init (&cookie_id_str))
786 if (!_dbus_string_copy_len (data, i, j - i,
790 if (!_dbus_string_init (&server_challenge))
794 _dbus_string_skip_blank (data, i, &i);
795 j = _dbus_string_get_length (data);
797 if (!_dbus_string_copy_len (data, i, j - i,
798 &server_challenge, 0))
801 if (!_dbus_keyring_validate_context (&context))
803 if (_dbus_string_append (&auth->outgoing,
804 "ERROR \"Server sent invalid cookie context\"\r\n"))
809 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
811 if (_dbus_string_append (&auth->outgoing,
812 "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
817 if (_dbus_string_get_length (&server_challenge) == 0)
819 if (_dbus_string_append (&auth->outgoing,
820 "ERROR \"Empty server challenge string\"\r\n"))
825 if (auth->keyring == NULL)
829 dbus_error_init (&error);
830 auth->keyring = _dbus_keyring_new_homedir (NULL,
834 if (auth->keyring == NULL)
836 if (dbus_error_has_name (&error,
837 DBUS_ERROR_NO_MEMORY))
839 dbus_error_free (&error);
844 _DBUS_ASSERT_ERROR_IS_SET (&error);
846 _dbus_verbose ("%s: Error loading keyring: %s\n",
847 DBUS_AUTH_NAME (auth), error.message);
849 if (_dbus_string_append (&auth->outgoing,
850 "ERROR \"Could not load cookie file\"\r\n"))
851 retval = TRUE; /* retval is only about mem */
853 dbus_error_free (&error);
859 _dbus_assert (!dbus_error_is_set (&error));
863 _dbus_assert (auth->keyring != NULL);
865 if (!_dbus_string_init (&tmp))
868 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
871 if (!_dbus_string_init (&client_challenge))
874 if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
877 if (!_dbus_string_init (&correct_hash))
880 if (!sha1_compute_hash (auth, val,
886 if (_dbus_string_get_length (&correct_hash) == 0)
888 /* couldn't find the cookie ID or something */
889 if (_dbus_string_append (&auth->outgoing,
890 "ERROR \"Don't have the requested cookie ID\"\r\n"))
895 _dbus_string_set_length (&tmp, 0);
897 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
898 _dbus_string_get_length (&tmp)))
901 if (!_dbus_string_append (&tmp, " "))
904 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
905 _dbus_string_get_length (&tmp)))
908 old_len = _dbus_string_get_length (&auth->outgoing);
909 if (!_dbus_string_append (&auth->outgoing, "DATA "))
912 if (!_dbus_string_base64_encode (&tmp, 0,
914 _dbus_string_get_length (&auth->outgoing)))
916 _dbus_string_set_length (&auth->outgoing, old_len);
920 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
922 _dbus_string_set_length (&auth->outgoing, old_len);
929 _dbus_string_zero (&correct_hash);
930 _dbus_string_free (&correct_hash);
932 _dbus_string_free (&client_challenge);
934 _dbus_string_zero (&tmp);
935 _dbus_string_free (&tmp);
937 _dbus_string_free (&server_challenge);
939 _dbus_string_free (&cookie_id_str);
941 _dbus_string_free (&context);
947 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
949 auth->cookie_id = -1;
950 _dbus_string_set_length (&auth->challenge, 0);
954 handle_server_data_external_mech (DBusAuth *auth,
955 const DBusString *data)
957 if (auth->credentials.uid == DBUS_UID_UNSET)
959 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
960 DBUS_AUTH_NAME (auth));
961 return send_rejected (auth);
964 if (_dbus_string_get_length (data) > 0)
966 if (_dbus_string_get_length (&auth->identity) > 0)
968 /* Tried to send two auth identities, wtf */
969 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
970 DBUS_AUTH_NAME (auth));
971 return send_rejected (auth);
975 /* this is our auth identity */
976 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
981 /* Poke client for an auth identity, if none given */
982 if (_dbus_string_get_length (&auth->identity) == 0 &&
983 !auth->already_asked_for_initial_response)
985 if (_dbus_string_append (&auth->outgoing,
988 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
989 DBUS_AUTH_NAME (auth));
990 auth->already_asked_for_initial_response = TRUE;
997 _dbus_credentials_clear (&auth->desired_identity);
999 /* If auth->identity is still empty here, then client
1000 * responded with an empty string after we poked it for
1001 * an initial response. This means to try to auth the
1002 * identity provided in the credentials.
1004 if (_dbus_string_get_length (&auth->identity) == 0)
1006 auth->desired_identity.uid = auth->credentials.uid;
1010 if (!_dbus_uid_from_string (&auth->identity,
1011 &auth->desired_identity.uid))
1013 _dbus_verbose ("%s: could not get credentials from uid string\n",
1014 DBUS_AUTH_NAME (auth));
1015 return send_rejected (auth);
1019 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1021 _dbus_verbose ("%s: desired user %s is no good\n",
1022 DBUS_AUTH_NAME (auth),
1023 _dbus_string_get_const_data (&auth->identity));
1024 return send_rejected (auth);
1027 if (_dbus_credentials_match (&auth->desired_identity,
1028 &auth->credentials))
1030 /* client has authenticated */
1031 if (!_dbus_string_append (&auth->outgoing,
1035 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1036 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1037 DBUS_AUTH_NAME (auth),
1038 auth->desired_identity.uid,
1039 auth->credentials.uid);
1041 auth->authorized_identity.uid = auth->desired_identity.uid;
1043 auth->authenticated_pending_begin = TRUE;
1049 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1050 " gid="DBUS_GID_FORMAT
1051 " do not allow uid="DBUS_UID_FORMAT
1052 " gid="DBUS_GID_FORMAT"\n",
1053 DBUS_AUTH_NAME (auth),
1054 auth->credentials.uid, auth->credentials.gid,
1055 auth->desired_identity.uid, auth->desired_identity.gid);
1056 return send_rejected (auth);
1061 handle_server_shutdown_external_mech (DBusAuth *auth)
1067 handle_client_initial_response_external_mech (DBusAuth *auth,
1068 DBusString *response)
1070 /* We always append our UID as an initial response, so the server
1071 * doesn't have to send back an empty challenge to check whether we
1072 * want to specify an identity. i.e. this avoids a round trip that
1073 * the spec for the EXTERNAL mechanism otherwise requires.
1075 DBusString plaintext;
1077 if (!_dbus_string_init (&plaintext))
1080 if (!_dbus_string_append_uint (&plaintext,
1084 if (!_dbus_string_base64_encode (&plaintext, 0,
1086 _dbus_string_get_length (response)))
1089 _dbus_string_free (&plaintext);
1094 _dbus_string_free (&plaintext);
1099 handle_client_data_external_mech (DBusAuth *auth,
1100 const DBusString *data)
1107 handle_client_shutdown_external_mech (DBusAuth *auth)
1112 /* Put mechanisms here in order of preference.
1113 * What I eventually want to have is:
1115 * - a mechanism that checks UNIX domain socket credentials
1116 * - a simple magic cookie mechanism like X11 or ICE
1117 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1118 * offers such as Kerberos, X509, whatever.
1121 static const DBusAuthMechanismHandler
1122 all_mechanisms[] = {
1124 handle_server_data_external_mech,
1126 handle_server_shutdown_external_mech,
1127 handle_client_initial_response_external_mech,
1128 handle_client_data_external_mech,
1130 handle_client_shutdown_external_mech },
1131 { "DBUS_COOKIE_SHA1",
1132 handle_server_data_cookie_sha1_mech,
1134 handle_server_shutdown_cookie_sha1_mech,
1135 handle_client_initial_response_cookie_sha1_mech,
1136 handle_client_data_cookie_sha1_mech,
1138 handle_client_shutdown_cookie_sha1_mech },
1142 static const DBusAuthMechanismHandler*
1143 find_mech (const DBusString *name,
1144 char **allowed_mechs)
1148 if (allowed_mechs != NULL &&
1149 !_dbus_string_array_contains ((const char**) allowed_mechs,
1150 _dbus_string_get_const_data (name)))
1154 while (all_mechanisms[i].mechanism != NULL)
1156 if (_dbus_string_equal_c_str (name,
1157 all_mechanisms[i].mechanism))
1159 return &all_mechanisms[i];
1168 send_rejected (DBusAuth *auth)
1171 DBusAuthServer *server_auth;
1174 if (!_dbus_string_init (&command))
1177 if (!_dbus_string_append (&command,
1182 while (all_mechanisms[i].mechanism != NULL)
1184 if (!_dbus_string_append (&command,
1188 if (!_dbus_string_append (&command,
1189 all_mechanisms[i].mechanism))
1195 if (!_dbus_string_append (&command, "\r\n"))
1198 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1199 _dbus_string_get_length (&auth->outgoing)))
1202 shutdown_mech (auth);
1204 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1205 server_auth = DBUS_AUTH_SERVER (auth);
1206 server_auth->failures += 1;
1208 _dbus_string_free (&command);
1213 _dbus_string_free (&command);
1218 process_auth (DBusAuth *auth,
1219 const DBusString *command,
1220 const DBusString *args)
1224 /* We are already using a mechanism, client is on crack */
1225 if (!_dbus_string_append (&auth->outgoing,
1226 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1231 else if (_dbus_string_get_length (args) == 0)
1233 /* No args to the auth, send mechanisms */
1234 if (!send_rejected (auth))
1243 DBusString base64_response;
1244 DBusString decoded_response;
1246 _dbus_string_find_blank (args, 0, &i);
1248 if (!_dbus_string_init (&mech))
1251 if (!_dbus_string_init (&base64_response))
1253 _dbus_string_free (&mech);
1257 if (!_dbus_string_init (&decoded_response))
1259 _dbus_string_free (&mech);
1260 _dbus_string_free (&base64_response);
1264 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1267 if (!_dbus_string_copy (args, i, &base64_response, 0))
1270 if (!_dbus_string_base64_decode (&base64_response, 0,
1271 &decoded_response, 0))
1274 auth->mech = find_mech (&mech, auth->allowed_mechs);
1275 if (auth->mech != NULL)
1277 _dbus_verbose ("%s: Trying mechanism %s with initial response of %d bytes\n",
1278 DBUS_AUTH_NAME (auth),
1279 auth->mech->mechanism,
1280 _dbus_string_get_length (&decoded_response));
1282 if (!(* auth->mech->server_data_func) (auth,
1288 /* Unsupported mechanism */
1289 if (!send_rejected (auth))
1293 _dbus_string_free (&mech);
1294 _dbus_string_free (&base64_response);
1295 _dbus_string_free (&decoded_response);
1301 _dbus_string_free (&mech);
1302 _dbus_string_free (&base64_response);
1303 _dbus_string_free (&decoded_response);
1309 process_cancel (DBusAuth *auth,
1310 const DBusString *command,
1311 const DBusString *args)
1313 if (!send_rejected (auth))
1320 process_begin (DBusAuth *auth,
1321 const DBusString *command,
1322 const DBusString *args)
1324 if (auth->authenticated_pending_begin)
1325 auth->authenticated = TRUE;
1328 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1331 shutdown_mech (auth);
1338 process_data_server (DBusAuth *auth,
1339 const DBusString *command,
1340 const DBusString *args)
1342 if (auth->mech != NULL)
1346 if (!_dbus_string_init (&decoded))
1349 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1351 _dbus_string_free (&decoded);
1355 #ifdef DBUS_ENABLE_VERBOSE_MODE
1356 if (_dbus_string_validate_ascii (&decoded, 0,
1357 _dbus_string_get_length (&decoded)))
1358 _dbus_verbose ("%s: data: '%s'\n",
1359 DBUS_AUTH_NAME (auth),
1360 _dbus_string_get_const_data (&decoded));
1363 if (!(* auth->mech->server_data_func) (auth, &decoded))
1365 _dbus_string_free (&decoded);
1369 _dbus_string_free (&decoded);
1373 if (!_dbus_string_append (&auth->outgoing,
1374 "ERROR \"Not currently in an auth conversation\"\r\n"))
1382 process_error_server (DBusAuth *auth,
1383 const DBusString *command,
1384 const DBusString *args)
1386 /* Server got error from client, reject the auth,
1387 * as we don't have anything more intelligent to do.
1389 if (!send_rejected (auth))
1395 /* return FALSE if no memory, TRUE if all OK */
1397 get_word (const DBusString *str,
1403 _dbus_string_skip_blank (str, *start, start);
1404 _dbus_string_find_blank (str, *start, &i);
1408 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1418 record_mechanisms (DBusAuth *auth,
1419 const DBusString *command,
1420 const DBusString *args)
1425 if (auth->already_got_mechanisms)
1428 len = _dbus_string_get_length (args);
1434 const DBusAuthMechanismHandler *mech;
1436 if (!_dbus_string_init (&m))
1439 if (!get_word (args, &next, &m))
1441 _dbus_string_free (&m);
1445 mech = find_mech (&m, auth->allowed_mechs);
1449 /* FIXME right now we try mechanisms in the order
1450 * the server lists them; should we do them in
1451 * some more deterministic order?
1453 * Probably in all_mechanisms order, our order of
1454 * preference. Of course when the server is us,
1455 * it lists things in that order anyhow.
1458 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1459 DBUS_AUTH_NAME (auth), mech->mechanism);
1461 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1464 _dbus_string_free (&m);
1470 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1471 DBUS_AUTH_NAME (auth),
1472 _dbus_string_get_const_data (&m));
1475 _dbus_string_free (&m);
1478 auth->already_got_mechanisms = TRUE;
1483 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1489 client_try_next_mechanism (DBusAuth *auth)
1491 const DBusAuthMechanismHandler *mech;
1492 DBusString auth_command;
1493 DBusAuthClient *client;
1495 client = DBUS_AUTH_CLIENT (auth);
1497 /* Pop any mechs not in the list of allowed mechanisms */
1499 while (client->mechs_to_try != NULL)
1501 mech = client->mechs_to_try->data;
1503 if (auth->allowed_mechs != NULL &&
1504 !_dbus_string_array_contains ((const char**) auth->allowed_mechs,
1507 /* don't try this one after all */
1508 _dbus_verbose ("%s: Mechanism %s isn't in the list of allowed mechanisms\n",
1509 DBUS_AUTH_NAME (auth), mech->mechanism);
1511 _dbus_list_pop_first (& client->mechs_to_try);
1514 break; /* we'll try this one */
1520 if (!_dbus_string_init (&auth_command))
1523 if (!_dbus_string_append (&auth_command,
1526 _dbus_string_free (&auth_command);
1530 if (!_dbus_string_append (&auth_command,
1533 _dbus_string_free (&auth_command);
1537 if (mech->client_initial_response_func != NULL)
1539 if (!_dbus_string_append (&auth_command, " "))
1541 _dbus_string_free (&auth_command);
1545 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1547 _dbus_string_free (&auth_command);
1552 if (!_dbus_string_append (&auth_command,
1555 _dbus_string_free (&auth_command);
1559 if (!_dbus_string_copy (&auth_command, 0,
1561 _dbus_string_get_length (&auth->outgoing)))
1563 _dbus_string_free (&auth_command);
1568 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1570 _dbus_verbose ("%s: Trying mechanism %s\n",
1571 DBUS_AUTH_NAME (auth),
1572 auth->mech->mechanism);
1574 _dbus_string_free (&auth_command);
1580 process_rejected (DBusAuth *auth,
1581 const DBusString *command,
1582 const DBusString *args)
1584 shutdown_mech (auth);
1586 if (!auth->already_got_mechanisms)
1588 if (!record_mechanisms (auth, command, args))
1592 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1594 if (!client_try_next_mechanism (auth))
1600 auth->need_disconnect = TRUE;
1607 process_ok (DBusAuth *auth,
1608 const DBusString *command,
1609 const DBusString *args)
1611 if (!_dbus_string_append (&auth->outgoing,
1615 auth->authenticated_pending_output = TRUE;
1621 process_data_client (DBusAuth *auth,
1622 const DBusString *command,
1623 const DBusString *args)
1625 if (auth->mech != NULL)
1629 if (!_dbus_string_init (&decoded))
1632 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1634 _dbus_string_free (&decoded);
1638 #ifdef DBUS_ENABLE_VERBOSE_MODE
1639 if (_dbus_string_validate_ascii (&decoded, 0,
1640 _dbus_string_get_length (&decoded)))
1642 _dbus_verbose ("%s: data: '%s'\n",
1643 DBUS_AUTH_NAME (auth),
1644 _dbus_string_get_const_data (&decoded));
1648 if (!(* auth->mech->client_data_func) (auth, &decoded))
1650 _dbus_string_free (&decoded);
1654 _dbus_string_free (&decoded);
1658 if (!_dbus_string_append (&auth->outgoing,
1659 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1667 process_error_client (DBusAuth *auth,
1668 const DBusString *command,
1669 const DBusString *args)
1671 /* Cancel current mechanism, as we don't have anything
1672 * more clever to do.
1674 if (!_dbus_string_append (&auth->outgoing,
1682 process_unknown (DBusAuth *auth,
1683 const DBusString *command,
1684 const DBusString *args)
1686 if (!_dbus_string_append (&auth->outgoing,
1687 "ERROR \"Unknown command\"\r\n"))
1693 /* returns whether to call it again right away */
1695 process_command (DBusAuth *auth)
1703 /* _dbus_verbose ("%s: trying process_command()\n"); */
1708 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1711 if (!_dbus_string_init (&command))
1713 auth->needed_memory = TRUE;
1717 if (!_dbus_string_init (&args))
1719 _dbus_string_free (&command);
1720 auth->needed_memory = TRUE;
1724 if (eol > _DBUS_ONE_MEGABYTE)
1726 /* This is a giant line, someone is trying to hose us. */
1727 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1733 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1736 if (!_dbus_string_validate_ascii (&command, 0,
1737 _dbus_string_get_length (&command)))
1739 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1740 DBUS_AUTH_NAME (auth));
1741 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1747 _dbus_verbose ("%s: got command \"%s\"\n",
1748 DBUS_AUTH_NAME (auth),
1749 _dbus_string_get_const_data (&command));
1751 _dbus_string_find_blank (&command, 0, &i);
1752 _dbus_string_skip_blank (&command, i, &j);
1755 _dbus_string_delete (&command, i, j - i);
1757 if (!_dbus_string_move (&command, i, &args, 0))
1761 while (auth->handlers[i].command != NULL)
1763 if (_dbus_string_equal_c_str (&command,
1764 auth->handlers[i].command))
1766 _dbus_verbose ("%s: Processing auth command %s\n",
1767 DBUS_AUTH_NAME (auth),
1768 auth->handlers[i].command);
1770 if (!(* auth->handlers[i].func) (auth, &command, &args))
1778 if (auth->handlers[i].command == NULL)
1780 if (!process_unknown (auth, &command, &args))
1786 /* We've succeeded in processing the whole command so drop it out
1787 * of the incoming buffer and return TRUE to try another command.
1790 _dbus_string_delete (&auth->incoming, 0, eol);
1793 _dbus_string_delete (&auth->incoming, 0, 2);
1798 _dbus_string_free (&args);
1799 _dbus_string_free (&command);
1802 auth->needed_memory = TRUE;
1804 auth->needed_memory = FALSE;
1813 * @addtogroup DBusAuth
1818 * Creates a new auth conversation object for the server side.
1819 * See doc/dbus-sasl-profile.txt for full details on what
1822 * @returns the new object or #NULL if no memory
1825 _dbus_auth_server_new (void)
1828 DBusAuthServer *server_auth;
1830 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1834 auth->handlers = server_handlers;
1836 server_auth = DBUS_AUTH_SERVER (auth);
1838 /* perhaps this should be per-mechanism with a lower
1841 server_auth->failures = 0;
1842 server_auth->max_failures = 6;
1848 * Creates a new auth conversation object for the client side.
1849 * See doc/dbus-sasl-profile.txt for full details on what
1852 * @returns the new object or #NULL if no memory
1855 _dbus_auth_client_new (void)
1859 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1863 auth->handlers = client_handlers;
1865 /* Add a default mechanism to try */
1866 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1867 (void*) &all_mechanisms[0]))
1869 _dbus_auth_unref (auth);
1873 /* Now try the mechanism we just added */
1874 if (!client_try_next_mechanism (auth))
1876 _dbus_auth_unref (auth);
1884 * Increments the refcount of an auth object.
1886 * @param auth the auth conversation
1889 _dbus_auth_ref (DBusAuth *auth)
1891 _dbus_assert (auth != NULL);
1893 auth->refcount += 1;
1897 * Decrements the refcount of an auth object.
1899 * @param auth the auth conversation
1902 _dbus_auth_unref (DBusAuth *auth)
1904 _dbus_assert (auth != NULL);
1905 _dbus_assert (auth->refcount > 0);
1907 auth->refcount -= 1;
1908 if (auth->refcount == 0)
1910 shutdown_mech (auth);
1912 if (DBUS_AUTH_IS_CLIENT (auth))
1914 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1918 _dbus_keyring_unref (auth->keyring);
1920 _dbus_string_free (&auth->context);
1921 _dbus_string_free (&auth->challenge);
1922 _dbus_string_free (&auth->identity);
1923 _dbus_string_free (&auth->incoming);
1924 _dbus_string_free (&auth->outgoing);
1926 dbus_free_string_array (auth->allowed_mechs);
1933 * Sets an array of authentication mechanism names
1934 * that we are willing to use.
1936 * @param auth the auth conversation
1937 * @param mechanisms #NULL-terminated array of mechanism names
1938 * @returns #FALSE if no memory
1941 _dbus_auth_set_mechanisms (DBusAuth *auth,
1942 const char **mechanisms)
1946 if (mechanisms != NULL)
1948 copy = _dbus_dup_string_array (mechanisms);
1955 dbus_free_string_array (auth->allowed_mechs);
1957 auth->allowed_mechs = copy;
1963 * @param auth the auth conversation object
1964 * @returns #TRUE if we're in a final state
1966 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1969 * Analyzes buffered input and moves the auth conversation forward,
1970 * returning the new state of the auth conversation.
1972 * @param auth the auth conversation
1973 * @returns the new state
1976 _dbus_auth_do_work (DBusAuth *auth)
1978 auth->needed_memory = FALSE;
1980 /* Max amount we'll buffer up before deciding someone's on crack */
1981 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1985 if (DBUS_AUTH_IN_END_STATE (auth))
1988 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1989 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1991 auth->need_disconnect = TRUE;
1992 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
1993 DBUS_AUTH_NAME (auth));
1997 if (auth->mech == NULL &&
1998 auth->already_got_mechanisms &&
1999 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
2001 auth->need_disconnect = TRUE;
2002 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2003 DBUS_AUTH_NAME (auth));
2007 while (process_command (auth));
2009 if (DBUS_AUTH_IS_SERVER (auth) &&
2010 DBUS_AUTH_SERVER (auth)->failures >=
2011 DBUS_AUTH_SERVER (auth)->max_failures)
2012 auth->need_disconnect = TRUE;
2014 if (auth->need_disconnect)
2015 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2016 else if (auth->authenticated)
2018 if (_dbus_string_get_length (&auth->incoming) > 0)
2019 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
2021 return DBUS_AUTH_STATE_AUTHENTICATED;
2023 else if (auth->needed_memory)
2024 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2025 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2026 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2028 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2032 * Gets bytes that need to be sent to the peer we're conversing with.
2033 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2034 * to notify the auth object that they were written.
2036 * @param auth the auth conversation
2037 * @param str return location for a ref to the buffer to send
2038 * @returns #FALSE if nothing to send
2041 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2042 const DBusString **str)
2044 _dbus_assert (auth != NULL);
2045 _dbus_assert (str != NULL);
2049 if (DBUS_AUTH_IN_END_STATE (auth))
2052 if (_dbus_string_get_length (&auth->outgoing) == 0)
2055 *str = &auth->outgoing;
2061 * Notifies the auth conversation object that
2062 * the given number of bytes of the outgoing buffer
2063 * have been written out.
2065 * @param auth the auth conversation
2066 * @param bytes_sent number of bytes written out
2069 _dbus_auth_bytes_sent (DBusAuth *auth,
2072 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2073 DBUS_AUTH_NAME (auth),
2075 _dbus_string_get_const_data (&auth->outgoing));
2077 _dbus_string_delete (&auth->outgoing,
2080 if (auth->authenticated_pending_output &&
2081 _dbus_string_get_length (&auth->outgoing) == 0)
2082 auth->authenticated = TRUE;
2086 * Get a buffer to be used for reading bytes from the peer we're conversing
2087 * with. Bytes should be appended to this buffer.
2089 * @param auth the auth conversation
2090 * @param buffer return location for buffer to append bytes to
2093 _dbus_auth_get_buffer (DBusAuth *auth,
2094 DBusString **buffer)
2096 _dbus_assert (auth != NULL);
2097 _dbus_assert (!auth->buffer_outstanding);
2099 *buffer = &auth->incoming;
2101 auth->buffer_outstanding = TRUE;
2105 * Returns a buffer with new data read into it.
2107 * @param auth the auth conversation
2108 * @param buffer the buffer being returned
2109 * @param bytes_read number of new bytes added
2112 _dbus_auth_return_buffer (DBusAuth *auth,
2116 _dbus_assert (buffer == &auth->incoming);
2117 _dbus_assert (auth->buffer_outstanding);
2119 auth->buffer_outstanding = FALSE;
2123 * Returns leftover bytes that were not used as part of the auth
2124 * conversation. These bytes will be part of the message stream
2125 * instead. This function may not be called until authentication has
2128 * @param auth the auth conversation
2129 * @param str return location for pointer to string of unused bytes
2132 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2133 const DBusString **str)
2135 if (!DBUS_AUTH_IN_END_STATE (auth))
2138 *str = &auth->incoming;
2143 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2144 * after we've gotten them and successfully moved them elsewhere.
2146 * @param auth the auth conversation
2149 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2151 if (!DBUS_AUTH_IN_END_STATE (auth))
2154 _dbus_string_set_length (&auth->incoming, 0);
2158 * Called post-authentication, indicates whether we need to encode
2159 * the message stream with _dbus_auth_encode_data() prior to
2160 * sending it to the peer.
2162 * @param auth the auth conversation
2163 * @returns #TRUE if we need to encode the stream
2166 _dbus_auth_needs_encoding (DBusAuth *auth)
2168 if (!auth->authenticated)
2171 if (auth->mech != NULL)
2173 if (DBUS_AUTH_IS_CLIENT (auth))
2174 return auth->mech->client_encode_func != NULL;
2176 return auth->mech->server_encode_func != NULL;
2183 * Called post-authentication, encodes a block of bytes for sending to
2184 * the peer. If no encoding was negotiated, just copies the bytes
2185 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2187 * @param auth the auth conversation
2188 * @param plaintext the plain text data
2189 * @param encoded initialized string to where encoded data is appended
2190 * @returns #TRUE if we had enough memory and successfully encoded
2193 _dbus_auth_encode_data (DBusAuth *auth,
2194 const DBusString *plaintext,
2195 DBusString *encoded)
2197 _dbus_assert (plaintext != encoded);
2199 if (!auth->authenticated)
2202 if (_dbus_auth_needs_encoding (auth))
2204 if (DBUS_AUTH_IS_CLIENT (auth))
2205 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2207 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2211 return _dbus_string_copy (plaintext, 0, encoded,
2212 _dbus_string_get_length (encoded));
2217 * Called post-authentication, indicates whether we need to decode
2218 * the message stream with _dbus_auth_decode_data() after
2219 * receiving it from the peer.
2221 * @param auth the auth conversation
2222 * @returns #TRUE if we need to encode the stream
2225 _dbus_auth_needs_decoding (DBusAuth *auth)
2227 if (!auth->authenticated)
2230 if (auth->mech != NULL)
2232 if (DBUS_AUTH_IS_CLIENT (auth))
2233 return auth->mech->client_decode_func != NULL;
2235 return auth->mech->server_decode_func != NULL;
2243 * Called post-authentication, decodes a block of bytes received from
2244 * the peer. If no encoding was negotiated, just copies the bytes (you
2245 * can avoid this by checking _dbus_auth_needs_decoding()).
2247 * @todo We need to be able to distinguish "out of memory" error
2248 * from "the data is hosed" error.
2250 * @param auth the auth conversation
2251 * @param encoded the encoded data
2252 * @param plaintext initialized string where decoded data is appended
2253 * @returns #TRUE if we had enough memory and successfully decoded
2256 _dbus_auth_decode_data (DBusAuth *auth,
2257 const DBusString *encoded,
2258 DBusString *plaintext)
2260 _dbus_assert (plaintext != encoded);
2262 if (!auth->authenticated)
2265 if (_dbus_auth_needs_decoding (auth))
2267 if (DBUS_AUTH_IS_CLIENT (auth))
2268 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2270 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2274 return _dbus_string_copy (encoded, 0, plaintext,
2275 _dbus_string_get_length (plaintext));
2280 * Sets credentials received via reliable means from the operating
2283 * @param auth the auth conversation
2284 * @param credentials the credentials received
2287 _dbus_auth_set_credentials (DBusAuth *auth,
2288 const DBusCredentials *credentials)
2290 auth->credentials = *credentials;
2294 * Gets the identity we authorized the client as. Apps may have
2295 * different policies as to what identities they allow.
2297 * @param auth the auth conversation
2298 * @param credentials the credentials we've authorized
2301 _dbus_auth_get_identity (DBusAuth *auth,
2302 DBusCredentials *credentials)
2304 if (auth->authenticated)
2305 *credentials = auth->authorized_identity;
2307 _dbus_credentials_clear (credentials);
2311 * Sets the "authentication context" which scopes cookies
2312 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2314 * @param auth the auth conversation
2315 * @param context the context
2316 * @returns #FALSE if no memory
2319 _dbus_auth_set_context (DBusAuth *auth,
2320 const DBusString *context)
2322 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2323 &auth->context, 0, _dbus_string_get_length (context));
2328 #ifdef DBUS_BUILD_TESTS
2329 #include "dbus-test.h"
2330 #include "dbus-auth-script.h"
2334 process_test_subdir (const DBusString *test_base_dir,
2337 DBusString test_directory;
2338 DBusString filename;
2346 if (!_dbus_string_init (&test_directory))
2347 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2349 _dbus_string_init_const (&filename, subdir);
2351 if (!_dbus_string_copy (test_base_dir, 0,
2352 &test_directory, 0))
2353 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2355 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2356 _dbus_assert_not_reached ("couldn't allocate full path");
2358 _dbus_string_free (&filename);
2359 if (!_dbus_string_init (&filename))
2360 _dbus_assert_not_reached ("didn't allocate filename string\n");
2362 dbus_error_init (&error);
2363 dir = _dbus_directory_open (&test_directory, &error);
2366 _dbus_warn ("Could not open %s: %s\n",
2367 _dbus_string_get_const_data (&test_directory),
2369 dbus_error_free (&error);
2373 printf ("Testing:\n");
2376 while (_dbus_directory_get_next_file (dir, &filename, &error))
2378 DBusString full_path;
2380 if (!_dbus_string_init (&full_path))
2381 _dbus_assert_not_reached ("couldn't init string");
2383 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2384 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2386 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2387 _dbus_assert_not_reached ("couldn't concat file to dir");
2389 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2391 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2392 _dbus_string_get_const_data (&filename));
2393 _dbus_string_free (&full_path);
2397 printf (" %s\n", _dbus_string_get_const_data (&filename));
2399 if (!_dbus_auth_script_run (&full_path))
2401 _dbus_string_free (&full_path);
2405 _dbus_string_free (&full_path);
2408 if (dbus_error_is_set (&error))
2410 _dbus_warn ("Could not get next file in %s: %s\n",
2411 _dbus_string_get_const_data (&test_directory), error.message);
2412 dbus_error_free (&error);
2421 _dbus_directory_close (dir);
2422 _dbus_string_free (&test_directory);
2423 _dbus_string_free (&filename);
2429 process_test_dirs (const char *test_data_dir)
2431 DBusString test_directory;
2436 _dbus_string_init_const (&test_directory, test_data_dir);
2438 if (!process_test_subdir (&test_directory, "auth"))
2445 _dbus_string_free (&test_directory);
2451 _dbus_auth_test (const char *test_data_dir)
2454 if (test_data_dir == NULL)
2457 if (!process_test_dirs (test_data_dir))
2463 #endif /* DBUS_BUILD_TESTS */