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 2.0
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);
77 * Handler for a given auth protocol command
81 const char *command; /**< Name of the command */
82 DBusProcessAuthCommandFunction func; /**< Function to handle the command */
83 } DBusAuthCommandHandler;
86 * This function appends an initial client response to the given string
88 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
89 DBusString *response);
92 * This function processes a block of data received from the peer.
93 * i.e. handles a DATA command.
95 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
96 const DBusString *data);
99 * This function encodes a block of data from the peer.
101 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
102 const DBusString *data,
103 DBusString *encoded);
106 * This function decodes a block of data from the peer.
108 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
109 const DBusString *data,
110 DBusString *decoded);
113 * This function is called when the mechanism is abandoned.
115 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
118 * Virtual table representing a particular auth mechanism.
122 const char *mechanism; /**< Name of the mechanism */
123 DBusAuthDataFunction server_data_func; /**< Function on server side for DATA */
124 DBusAuthEncodeFunction server_encode_func; /**< Function on server side to encode */
125 DBusAuthDecodeFunction server_decode_func; /**< Function on server side to decode */
126 DBusAuthShutdownFunction server_shutdown_func; /**< Function on server side to shut down */
127 DBusInitialResponseFunction client_initial_response_func; /**< Function on client side to handle initial response */
128 DBusAuthDataFunction client_data_func; /**< Function on client side for DATA */
129 DBusAuthEncodeFunction client_encode_func; /**< Function on client side for encode */
130 DBusAuthDecodeFunction client_decode_func; /**< Function on client side for decode */
131 DBusAuthShutdownFunction client_shutdown_func; /**< Function on client side for shutdown */
132 } DBusAuthMechanismHandler;
135 * Internal members of DBusAuth.
139 int refcount; /**< reference count */
141 DBusString incoming; /**< Incoming data buffer */
142 DBusString outgoing; /**< Outgoing data buffer */
144 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
146 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
148 DBusString identity; /**< Current identity we're authorizing
152 DBusCredentials credentials; /**< Credentials read from socket,
156 DBusCredentials authorized_identity; /**< Credentials that are authorized */
158 DBusCredentials desired_identity; /**< Identity client has requested */
160 DBusString context; /**< Cookie scope */
161 DBusKeyring *keyring; /**< Keyring for cookie mechanism. */
162 int cookie_id; /**< ID of cookie to use */
163 DBusString challenge; /**< Challenge sent to client */
165 char **allowed_mechs; /**< Mechanisms we're allowed to use,
166 * or #NULL if we can use any
169 unsigned int needed_memory : 1; /**< We needed memory to continue since last
170 * successful getting something done
172 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
173 unsigned int authenticated : 1; /**< We are authenticated */
174 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
175 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
176 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
177 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
178 unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
182 * "Subclass" of DBusAuth for client side
186 DBusAuth base; /**< Parent class */
188 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
193 * "Subclass" of DBusAuth for server side.
197 DBusAuth base; /**< Parent class */
199 int failures; /**< Number of times client has been rejected */
200 int max_failures; /**< Number of times we reject before disconnect */
204 static dbus_bool_t process_auth (DBusAuth *auth,
205 const DBusString *command,
206 const DBusString *args);
207 static dbus_bool_t process_cancel (DBusAuth *auth,
208 const DBusString *command,
209 const DBusString *args);
210 static dbus_bool_t process_begin (DBusAuth *auth,
211 const DBusString *command,
212 const DBusString *args);
213 static dbus_bool_t process_data_server (DBusAuth *auth,
214 const DBusString *command,
215 const DBusString *args);
216 static dbus_bool_t process_error_server (DBusAuth *auth,
217 const DBusString *command,
218 const DBusString *args);
219 static dbus_bool_t process_rejected (DBusAuth *auth,
220 const DBusString *command,
221 const DBusString *args);
222 static dbus_bool_t process_ok (DBusAuth *auth,
223 const DBusString *command,
224 const DBusString *args);
225 static dbus_bool_t process_data_client (DBusAuth *auth,
226 const DBusString *command,
227 const DBusString *args);
228 static dbus_bool_t process_error_client (DBusAuth *auth,
229 const DBusString *command,
230 const DBusString *args);
233 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
234 static dbus_bool_t send_auth (DBusAuth *auth,
235 const DBusAuthMechanismHandler *mech);
236 static dbus_bool_t send_data (DBusAuth *auth,
238 static dbus_bool_t send_rejected (DBusAuth *auth);
239 static dbus_bool_t send_error (DBusAuth *auth,
240 const char *message);
241 static dbus_bool_t send_ok (DBusAuth *auth);
242 static dbus_bool_t send_begin (DBusAuth *auth);
243 static dbus_bool_t send_cancel (DBusAuth *auth);
245 static DBusAuthCommandHandler
246 server_handlers[] = {
247 { "AUTH", process_auth },
248 { "CANCEL", process_cancel },
249 { "BEGIN", process_begin },
250 { "DATA", process_data_server },
251 { "ERROR", process_error_server },
255 static DBusAuthCommandHandler
256 client_handlers[] = {
257 { "REJECTED", process_rejected },
258 { "OK", process_ok },
259 { "DATA", process_data_client },
260 { "ERROR", process_error_client },
265 * @param auth the auth conversation
266 * @returns #TRUE if the conversation is the server side
268 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
270 * @param auth the auth conversation
271 * @returns #TRUE if the conversation is the client side
273 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
275 * @param auth the auth conversation
276 * @returns auth cast to DBusAuthClient
278 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
280 * @param auth the auth conversation
281 * @returns auth cast to DBusAuthServer
283 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
286 * The name of the auth ("client" or "server")
287 * @param auth the auth conversation
290 #define DBUS_AUTH_NAME(auth) (DBUS_AUTH_IS_SERVER(auth) ? "server" : "client")
293 _dbus_auth_new (int size)
297 auth = dbus_malloc0 (size);
303 _dbus_credentials_clear (&auth->credentials);
304 _dbus_credentials_clear (&auth->authorized_identity);
305 _dbus_credentials_clear (&auth->desired_identity);
307 auth->keyring = NULL;
308 auth->cookie_id = -1;
310 /* note that we don't use the max string length feature,
311 * because you can't use that feature if you're going to
312 * try to recover from out-of-memory (it creates
313 * what looks like unrecoverable inability to alloc
314 * more space in the string). But we do handle
315 * overlong buffers in _dbus_auth_do_work().
318 if (!_dbus_string_init (&auth->incoming))
321 if (!_dbus_string_init (&auth->outgoing))
324 if (!_dbus_string_init (&auth->identity))
327 if (!_dbus_string_init (&auth->context))
330 if (!_dbus_string_init (&auth->challenge))
333 /* default context if none is specified */
334 if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
340 _dbus_string_free (&auth->challenge);
342 _dbus_string_free (&auth->context);
344 _dbus_string_free (&auth->identity);
346 _dbus_string_free (&auth->outgoing);
348 _dbus_string_free (&auth->incoming);
355 shutdown_mech (DBusAuth *auth)
357 /* Cancel any auth */
358 auth->authenticated_pending_begin = FALSE;
359 auth->authenticated = FALSE;
360 auth->already_asked_for_initial_response = FALSE;
361 _dbus_string_set_length (&auth->identity, 0);
363 _dbus_credentials_clear (&auth->authorized_identity);
364 _dbus_credentials_clear (&auth->desired_identity);
366 if (auth->mech != NULL)
368 _dbus_verbose ("%s: Shutting down mechanism %s\n",
369 DBUS_AUTH_NAME (auth), auth->mech->mechanism);
371 if (DBUS_AUTH_IS_CLIENT (auth))
372 (* auth->mech->client_shutdown_func) (auth);
374 (* auth->mech->server_shutdown_func) (auth);
380 /* Returns TRUE but with an empty string hash if the
381 * cookie_id isn't known. As with all this code
382 * TRUE just means we had enough memory.
385 sha1_compute_hash (DBusAuth *auth,
387 const DBusString *server_challenge,
388 const DBusString *client_challenge,
395 _dbus_assert (auth->keyring != NULL);
399 if (!_dbus_string_init (&cookie))
402 if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
406 if (_dbus_string_get_length (&cookie) == 0)
412 if (!_dbus_string_init (&to_hash))
415 if (!_dbus_string_copy (server_challenge, 0,
416 &to_hash, _dbus_string_get_length (&to_hash)))
419 if (!_dbus_string_append (&to_hash, ":"))
422 if (!_dbus_string_copy (client_challenge, 0,
423 &to_hash, _dbus_string_get_length (&to_hash)))
426 if (!_dbus_string_append (&to_hash, ":"))
429 if (!_dbus_string_copy (&cookie, 0,
430 &to_hash, _dbus_string_get_length (&to_hash)))
433 if (!_dbus_sha_compute (&to_hash, hash))
439 _dbus_string_zero (&to_hash);
440 _dbus_string_free (&to_hash);
442 _dbus_string_zero (&cookie);
443 _dbus_string_free (&cookie);
447 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
448 * entropy, we use 128. This is the number of bytes in the random
451 #define N_CHALLENGE_BYTES (128/8)
454 sha1_handle_first_client_response (DBusAuth *auth,
455 const DBusString *data)
457 /* We haven't sent a challenge yet, we're expecting a desired
458 * username from the client.
467 _dbus_string_set_length (&auth->challenge, 0);
469 if (_dbus_string_get_length (data) > 0)
471 if (_dbus_string_get_length (&auth->identity) > 0)
473 /* Tried to send two auth identities, wtf */
474 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
475 DBUS_AUTH_NAME (auth));
476 return send_rejected (auth);
480 /* this is our auth identity */
481 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
486 if (!_dbus_credentials_from_username (data, &auth->desired_identity))
488 _dbus_verbose ("%s: Did not get a valid username from client\n",
489 DBUS_AUTH_NAME (auth));
490 return send_rejected (auth);
493 if (!_dbus_string_init (&tmp))
496 if (!_dbus_string_init (&tmp2))
498 _dbus_string_free (&tmp);
502 /* we cache the keyring for speed, so here we drop it if it's the
503 * wrong one. FIXME caching the keyring here is useless since we use
504 * a different DBusAuth for every connection.
507 !_dbus_keyring_is_for_user (auth->keyring,
510 _dbus_keyring_unref (auth->keyring);
511 auth->keyring = NULL;
514 if (auth->keyring == NULL)
518 dbus_error_init (&error);
519 auth->keyring = _dbus_keyring_new_homedir (data,
523 if (auth->keyring == NULL)
525 if (dbus_error_has_name (&error,
526 DBUS_ERROR_NO_MEMORY))
528 dbus_error_free (&error);
533 _DBUS_ASSERT_ERROR_IS_SET (&error);
534 _dbus_verbose ("%s: Error loading keyring: %s\n",
535 DBUS_AUTH_NAME (auth), error.message);
536 if (send_rejected (auth))
537 retval = TRUE; /* retval is only about mem */
538 dbus_error_free (&error);
544 _dbus_assert (!dbus_error_is_set (&error));
548 _dbus_assert (auth->keyring != NULL);
550 dbus_error_init (&error);
551 auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
552 if (auth->cookie_id < 0)
554 _DBUS_ASSERT_ERROR_IS_SET (&error);
555 _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
556 DBUS_AUTH_NAME (auth), error.message);
557 if (send_rejected (auth))
559 dbus_error_free (&error);
564 _dbus_assert (!dbus_error_is_set (&error));
567 if (!_dbus_string_copy (&auth->context, 0,
568 &tmp2, _dbus_string_get_length (&tmp2)))
571 if (!_dbus_string_append (&tmp2, " "))
574 if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
577 if (!_dbus_string_append (&tmp2, " "))
580 if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
583 _dbus_string_set_length (&auth->challenge, 0);
584 if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
587 if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
588 _dbus_string_get_length (&tmp2)))
591 if (!send_data (auth, &tmp2))
597 _dbus_string_zero (&tmp);
598 _dbus_string_free (&tmp);
599 _dbus_string_zero (&tmp2);
600 _dbus_string_free (&tmp2);
606 sha1_handle_second_client_response (DBusAuth *auth,
607 const DBusString *data)
609 /* We are expecting a response which is the hex-encoded client
610 * challenge, space, then SHA-1 hash of the concatenation of our
611 * challenge, ":", client challenge, ":", secret key, all
615 DBusString client_challenge;
616 DBusString client_hash;
618 DBusString correct_hash;
622 if (!_dbus_string_find_blank (data, 0, &i))
624 _dbus_verbose ("%s: no space separator in client response\n",
625 DBUS_AUTH_NAME (auth));
626 return send_rejected (auth);
629 if (!_dbus_string_init (&client_challenge))
632 if (!_dbus_string_init (&client_hash))
635 if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
639 _dbus_string_skip_blank (data, i, &i);
641 if (!_dbus_string_copy_len (data, i,
642 _dbus_string_get_length (data) - i,
647 if (_dbus_string_get_length (&client_challenge) == 0 ||
648 _dbus_string_get_length (&client_hash) == 0)
650 _dbus_verbose ("%s: zero-length client challenge or hash\n",
651 DBUS_AUTH_NAME (auth));
652 if (send_rejected (auth))
657 if (!_dbus_string_init (&correct_hash))
660 if (!sha1_compute_hash (auth, auth->cookie_id,
666 /* if cookie_id was invalid, then we get an empty hash */
667 if (_dbus_string_get_length (&correct_hash) == 0)
669 if (send_rejected (auth))
674 if (!_dbus_string_equal (&client_hash, &correct_hash))
676 if (send_rejected (auth))
684 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT" using DBUS_COOKIE_SHA1\n",
685 DBUS_AUTH_NAME (auth), auth->desired_identity.uid);
687 auth->authorized_identity = auth->desired_identity;
688 auth->authenticated_pending_begin = TRUE;
692 _dbus_string_zero (&correct_hash);
693 _dbus_string_free (&correct_hash);
695 _dbus_string_zero (&client_hash);
696 _dbus_string_free (&client_hash);
698 _dbus_string_free (&client_challenge);
704 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
705 const DBusString *data)
707 if (auth->cookie_id < 0)
708 return sha1_handle_first_client_response (auth, data);
710 return sha1_handle_second_client_response (auth, data);
714 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
716 auth->cookie_id = -1;
717 _dbus_string_set_length (&auth->challenge, 0);
721 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
722 DBusString *response)
724 const DBusString *username;
729 if (!_dbus_username_from_current_process (&username))
732 if (!_dbus_string_hex_encode (username, 0,
734 _dbus_string_get_length (response)))
744 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
745 const DBusString *data)
747 /* The data we get from the server should be the cookie context
748 * name, the cookie ID, and the server challenge, separated by
749 * spaces. We send back our challenge string and the correct hash.
753 DBusString cookie_id_str;
754 DBusString server_challenge;
755 DBusString client_challenge;
756 DBusString correct_hash;
763 if (!_dbus_string_find_blank (data, 0, &i))
765 if (send_error (auth,
766 "Server did not send context/ID/challenge properly"))
771 if (!_dbus_string_init (&context))
774 if (!_dbus_string_copy_len (data, 0, i,
778 _dbus_string_skip_blank (data, i, &i);
779 if (!_dbus_string_find_blank (data, i, &j))
781 if (send_error (auth,
782 "Server did not send context/ID/challenge properly"))
787 if (!_dbus_string_init (&cookie_id_str))
790 if (!_dbus_string_copy_len (data, i, j - i,
794 if (!_dbus_string_init (&server_challenge))
798 _dbus_string_skip_blank (data, i, &i);
799 j = _dbus_string_get_length (data);
801 if (!_dbus_string_copy_len (data, i, j - i,
802 &server_challenge, 0))
805 if (!_dbus_keyring_validate_context (&context))
807 if (send_error (auth, "Server sent invalid cookie context"))
812 if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
814 if (send_error (auth, "Could not parse cookie ID as an integer"))
819 if (_dbus_string_get_length (&server_challenge) == 0)
821 if (send_error (auth, "Empty server challenge string"))
826 if (auth->keyring == NULL)
830 dbus_error_init (&error);
831 auth->keyring = _dbus_keyring_new_homedir (NULL,
835 if (auth->keyring == NULL)
837 if (dbus_error_has_name (&error,
838 DBUS_ERROR_NO_MEMORY))
840 dbus_error_free (&error);
845 _DBUS_ASSERT_ERROR_IS_SET (&error);
847 _dbus_verbose ("%s: Error loading keyring: %s\n",
848 DBUS_AUTH_NAME (auth), error.message);
850 if (send_error (auth, "Could not load cookie file"))
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 (send_error (auth, "Don't have the requested cookie ID"))
894 _dbus_string_set_length (&tmp, 0);
896 if (!_dbus_string_copy (&client_challenge, 0, &tmp,
897 _dbus_string_get_length (&tmp)))
900 if (!_dbus_string_append (&tmp, " "))
903 if (!_dbus_string_copy (&correct_hash, 0, &tmp,
904 _dbus_string_get_length (&tmp)))
907 if (!send_data (auth, &tmp))
913 _dbus_string_zero (&correct_hash);
914 _dbus_string_free (&correct_hash);
916 _dbus_string_free (&client_challenge);
918 _dbus_string_zero (&tmp);
919 _dbus_string_free (&tmp);
921 _dbus_string_free (&server_challenge);
923 _dbus_string_free (&cookie_id_str);
925 _dbus_string_free (&context);
931 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
933 auth->cookie_id = -1;
934 _dbus_string_set_length (&auth->challenge, 0);
938 handle_server_data_external_mech (DBusAuth *auth,
939 const DBusString *data)
941 if (auth->credentials.uid == DBUS_UID_UNSET)
943 _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
944 DBUS_AUTH_NAME (auth));
945 return send_rejected (auth);
948 if (_dbus_string_get_length (data) > 0)
950 if (_dbus_string_get_length (&auth->identity) > 0)
952 /* Tried to send two auth identities, wtf */
953 _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
954 DBUS_AUTH_NAME (auth));
955 return send_rejected (auth);
959 /* this is our auth identity */
960 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
965 /* Poke client for an auth identity, if none given */
966 if (_dbus_string_get_length (&auth->identity) == 0 &&
967 !auth->already_asked_for_initial_response)
969 if (send_data (auth, NULL))
971 _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
972 DBUS_AUTH_NAME (auth));
973 auth->already_asked_for_initial_response = TRUE;
980 _dbus_credentials_clear (&auth->desired_identity);
982 /* If auth->identity is still empty here, then client
983 * responded with an empty string after we poked it for
984 * an initial response. This means to try to auth the
985 * identity provided in the credentials.
987 if (_dbus_string_get_length (&auth->identity) == 0)
989 auth->desired_identity.uid = auth->credentials.uid;
993 if (!_dbus_uid_from_string (&auth->identity,
994 &auth->desired_identity.uid))
996 _dbus_verbose ("%s: could not get credentials from uid string\n",
997 DBUS_AUTH_NAME (auth));
998 return send_rejected (auth);
1002 if (auth->desired_identity.uid == DBUS_UID_UNSET)
1004 _dbus_verbose ("%s: desired user %s is no good\n",
1005 DBUS_AUTH_NAME (auth),
1006 _dbus_string_get_const_data (&auth->identity));
1007 return send_rejected (auth);
1010 if (_dbus_credentials_match (&auth->desired_identity,
1011 &auth->credentials))
1013 /* client has authenticated */
1014 if (!send_ok (auth))
1017 _dbus_verbose ("%s: authenticated client with UID "DBUS_UID_FORMAT
1018 " matching socket credentials UID "DBUS_UID_FORMAT"\n",
1019 DBUS_AUTH_NAME (auth),
1020 auth->desired_identity.uid,
1021 auth->credentials.uid);
1023 auth->authorized_identity.uid = auth->desired_identity.uid;
1025 auth->authenticated_pending_begin = TRUE;
1031 _dbus_verbose ("%s: credentials uid="DBUS_UID_FORMAT
1032 " gid="DBUS_GID_FORMAT
1033 " do not allow uid="DBUS_UID_FORMAT
1034 " gid="DBUS_GID_FORMAT"\n",
1035 DBUS_AUTH_NAME (auth),
1036 auth->credentials.uid, auth->credentials.gid,
1037 auth->desired_identity.uid, auth->desired_identity.gid);
1038 return send_rejected (auth);
1043 handle_server_shutdown_external_mech (DBusAuth *auth)
1049 handle_client_initial_response_external_mech (DBusAuth *auth,
1050 DBusString *response)
1052 /* We always append our UID as an initial response, so the server
1053 * doesn't have to send back an empty challenge to check whether we
1054 * want to specify an identity. i.e. this avoids a round trip that
1055 * the spec for the EXTERNAL mechanism otherwise requires.
1057 DBusString plaintext;
1059 if (!_dbus_string_init (&plaintext))
1062 if (!_dbus_string_append_uint (&plaintext,
1066 if (!_dbus_string_hex_encode (&plaintext, 0,
1068 _dbus_string_get_length (response)))
1071 _dbus_string_free (&plaintext);
1076 _dbus_string_free (&plaintext);
1081 handle_client_data_external_mech (DBusAuth *auth,
1082 const DBusString *data)
1089 handle_client_shutdown_external_mech (DBusAuth *auth)
1094 /* Put mechanisms here in order of preference.
1095 * What I eventually want to have is:
1097 * - a mechanism that checks UNIX domain socket credentials
1098 * - a simple magic cookie mechanism like X11 or ICE
1099 * - mechanisms that chain to Cyrus SASL, so we can use anything it
1100 * offers such as Kerberos, X509, whatever.
1103 static const DBusAuthMechanismHandler
1104 all_mechanisms[] = {
1106 handle_server_data_external_mech,
1108 handle_server_shutdown_external_mech,
1109 handle_client_initial_response_external_mech,
1110 handle_client_data_external_mech,
1112 handle_client_shutdown_external_mech },
1113 { "DBUS_COOKIE_SHA1",
1114 handle_server_data_cookie_sha1_mech,
1116 handle_server_shutdown_cookie_sha1_mech,
1117 handle_client_initial_response_cookie_sha1_mech,
1118 handle_client_data_cookie_sha1_mech,
1120 handle_client_shutdown_cookie_sha1_mech },
1124 static const DBusAuthMechanismHandler*
1125 find_mech (const DBusString *name,
1126 char **allowed_mechs)
1130 if (allowed_mechs != NULL &&
1131 !_dbus_string_array_contains ((const char**) allowed_mechs,
1132 _dbus_string_get_const_data (name)))
1136 while (all_mechanisms[i].mechanism != NULL)
1138 if (_dbus_string_equal_c_str (name,
1139 all_mechanisms[i].mechanism))
1141 return &all_mechanisms[i];
1150 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1152 DBusString auth_command;
1154 if (!_dbus_string_init (&auth_command))
1157 if (!_dbus_string_append (&auth_command,
1160 _dbus_string_free (&auth_command);
1164 if (!_dbus_string_append (&auth_command,
1167 _dbus_string_free (&auth_command);
1171 if (mech->client_initial_response_func != NULL)
1173 if (!_dbus_string_append (&auth_command, " "))
1175 _dbus_string_free (&auth_command);
1179 if (!(* mech->client_initial_response_func) (auth, &auth_command))
1181 _dbus_string_free (&auth_command);
1186 if (!_dbus_string_append (&auth_command,
1189 _dbus_string_free (&auth_command);
1193 if (!_dbus_string_copy (&auth_command, 0,
1195 _dbus_string_get_length (&auth->outgoing)))
1197 _dbus_string_free (&auth_command);
1201 _dbus_string_free (&auth_command);
1208 send_data (DBusAuth *auth, DBusString *data)
1212 if (data == NULL || _dbus_string_get_length (data) == 0)
1213 return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1216 old_len = _dbus_string_get_length (&auth->outgoing);
1217 if (!_dbus_string_append (&auth->outgoing, "DATA "))
1220 if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1221 _dbus_string_get_length (&auth->outgoing)))
1224 if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1230 _dbus_string_set_length (&auth->outgoing, old_len);
1237 send_rejected (DBusAuth *auth)
1240 DBusAuthServer *server_auth;
1243 if (!_dbus_string_init (&command))
1246 if (!_dbus_string_append (&command,
1251 while (all_mechanisms[i].mechanism != NULL)
1253 if (!_dbus_string_append (&command,
1257 if (!_dbus_string_append (&command,
1258 all_mechanisms[i].mechanism))
1264 if (!_dbus_string_append (&command, "\r\n"))
1267 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1268 _dbus_string_get_length (&auth->outgoing)))
1271 shutdown_mech (auth);
1273 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1274 server_auth = DBUS_AUTH_SERVER (auth);
1275 server_auth->failures += 1;
1277 if (server_auth->failures >= server_auth->max_failures)
1278 auth->need_disconnect = TRUE;
1280 _dbus_string_free (&command);
1285 _dbus_string_free (&command);
1290 send_error (DBusAuth *auth, const char *message)
1292 return _dbus_string_append_printf (&auth->outgoing,
1293 "ERROR \"%s\"\r\n", message);
1297 send_ok (DBusAuth *auth)
1299 return _dbus_string_append (&auth->outgoing, "OK\r\n");
1303 send_begin (DBusAuth *auth)
1305 return _dbus_string_append (&auth->outgoing, "BEGIN\r\n");
1309 send_cancel (DBusAuth *auth)
1311 return _dbus_string_append (&auth->outgoing, "CANCEL\r\n");
1315 process_auth (DBusAuth *auth,
1316 const DBusString *command,
1317 const DBusString *args)
1321 /* We are already using a mechanism, client is on crack */
1322 if (!send_error (auth, "Sent AUTH while another AUTH in progress"))
1327 else if (_dbus_string_get_length (args) == 0)
1329 /* No args to the auth, send mechanisms */
1330 if (!send_rejected (auth))
1339 DBusString hex_response;
1340 DBusString decoded_response;
1342 _dbus_string_find_blank (args, 0, &i);
1344 if (!_dbus_string_init (&mech))
1347 if (!_dbus_string_init (&hex_response))
1349 _dbus_string_free (&mech);
1353 if (!_dbus_string_init (&decoded_response))
1355 _dbus_string_free (&mech);
1356 _dbus_string_free (&hex_response);
1360 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1363 _dbus_string_skip_blank (args, i, &i);
1364 if (!_dbus_string_copy (args, i, &hex_response, 0))
1367 if (!_dbus_string_hex_decode (&hex_response, 0, &end,
1368 &decoded_response, 0))
1371 if (_dbus_string_get_length (&hex_response) != end)
1373 if (!send_error (auth, "Invalid hex encoding"))
1379 auth->mech = find_mech (&mech, auth->allowed_mechs);
1380 if (auth->mech != NULL)
1382 _dbus_verbose ("%s: Trying mechanism %s with initial response of %d bytes\n",
1383 DBUS_AUTH_NAME (auth),
1384 auth->mech->mechanism,
1385 _dbus_string_get_length (&decoded_response));
1387 if (!(* auth->mech->server_data_func) (auth,
1393 /* Unsupported mechanism */
1394 if (!send_rejected (auth))
1399 _dbus_string_free (&mech);
1400 _dbus_string_free (&hex_response);
1401 _dbus_string_free (&decoded_response);
1407 _dbus_string_free (&mech);
1408 _dbus_string_free (&hex_response);
1409 _dbus_string_free (&decoded_response);
1415 process_cancel (DBusAuth *auth,
1416 const DBusString *command,
1417 const DBusString *args)
1419 if (!send_rejected (auth))
1426 process_begin (DBusAuth *auth,
1427 const DBusString *command,
1428 const DBusString *args)
1430 if (auth->authenticated_pending_begin)
1431 auth->authenticated = TRUE;
1434 auth->need_disconnect = TRUE; /* client trying to send data before auth,
1437 shutdown_mech (auth);
1444 process_data_server (DBusAuth *auth,
1445 const DBusString *command,
1446 const DBusString *args)
1450 if (auth->mech != NULL)
1454 if (!_dbus_string_init (&decoded))
1457 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1459 _dbus_string_free (&decoded);
1463 if (_dbus_string_get_length (args) != end)
1465 _dbus_string_free (&decoded);
1466 if (!send_error (auth, "Invalid hex encoding"))
1472 #ifdef DBUS_ENABLE_VERBOSE_MODE
1473 if (_dbus_string_validate_ascii (&decoded, 0,
1474 _dbus_string_get_length (&decoded)))
1475 _dbus_verbose ("%s: data: '%s'\n",
1476 DBUS_AUTH_NAME (auth),
1477 _dbus_string_get_const_data (&decoded));
1480 if (!(* auth->mech->server_data_func) (auth, &decoded))
1482 _dbus_string_free (&decoded);
1486 _dbus_string_free (&decoded);
1490 if (!send_error (auth, "Not currently in an auth conversation"))
1498 process_error_server (DBusAuth *auth,
1499 const DBusString *command,
1500 const DBusString *args)
1502 /* Server got error from client, reject the auth,
1503 * as we don't have anything more intelligent to do.
1505 if (!send_rejected (auth))
1511 /* return FALSE if no memory, TRUE if all OK */
1513 get_word (const DBusString *str,
1519 _dbus_string_skip_blank (str, *start, start);
1520 _dbus_string_find_blank (str, *start, &i);
1524 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1534 record_mechanisms (DBusAuth *auth,
1535 const DBusString *command,
1536 const DBusString *args)
1541 if (auth->already_got_mechanisms)
1544 len = _dbus_string_get_length (args);
1550 const DBusAuthMechanismHandler *mech;
1552 if (!_dbus_string_init (&m))
1555 if (!get_word (args, &next, &m))
1557 _dbus_string_free (&m);
1561 mech = find_mech (&m, auth->allowed_mechs);
1565 /* FIXME right now we try mechanisms in the order
1566 * the server lists them; should we do them in
1567 * some more deterministic order?
1569 * Probably in all_mechanisms order, our order of
1570 * preference. Of course when the server is us,
1571 * it lists things in that order anyhow.
1574 _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1575 DBUS_AUTH_NAME (auth), mech->mechanism);
1577 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1580 _dbus_string_free (&m);
1586 _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1587 DBUS_AUTH_NAME (auth),
1588 _dbus_string_get_const_data (&m));
1591 _dbus_string_free (&m);
1594 auth->already_got_mechanisms = TRUE;
1599 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1605 client_try_next_mechanism (DBusAuth *auth)
1607 const DBusAuthMechanismHandler *mech;
1608 DBusAuthClient *client;
1610 client = DBUS_AUTH_CLIENT (auth);
1612 _dbus_assert (client->mechs_to_try != NULL);
1614 mech = client->mechs_to_try->data;
1616 if (!send_auth (auth, mech))
1619 _dbus_list_pop_first (&client->mechs_to_try);
1621 _dbus_verbose ("%s: Trying mechanism %s\n",
1622 DBUS_AUTH_NAME (auth),
1623 auth->mech->mechanism);
1629 process_rejected (DBusAuth *auth,
1630 const DBusString *command,
1631 const DBusString *args)
1633 shutdown_mech (auth);
1635 if (!auth->already_got_mechanisms)
1637 if (!record_mechanisms (auth, command, args))
1641 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1643 if (!client_try_next_mechanism (auth))
1649 _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1650 DBUS_AUTH_NAME (auth));
1651 auth->need_disconnect = TRUE;
1658 process_ok (DBusAuth *auth,
1659 const DBusString *command,
1660 const DBusString *args)
1662 if (!send_begin (auth))
1665 auth->authenticated_pending_output = TRUE;
1671 process_data_client (DBusAuth *auth,
1672 const DBusString *command,
1673 const DBusString *args)
1677 if (auth->mech != NULL)
1681 if (!_dbus_string_init (&decoded))
1684 if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1686 _dbus_string_free (&decoded);
1690 if (_dbus_string_get_length (args) != end)
1692 _dbus_string_free (&decoded);
1693 if (!send_error (auth, "Invalid hex encoding"))
1699 #ifdef DBUS_ENABLE_VERBOSE_MODE
1700 if (_dbus_string_validate_ascii (&decoded, 0,
1701 _dbus_string_get_length (&decoded)))
1703 _dbus_verbose ("%s: data: '%s'\n",
1704 DBUS_AUTH_NAME (auth),
1705 _dbus_string_get_const_data (&decoded));
1709 if (!(* auth->mech->client_data_func) (auth, &decoded))
1711 _dbus_string_free (&decoded);
1715 _dbus_string_free (&decoded);
1719 if (!send_error (auth, "Got DATA when not in an auth exchange"))
1727 process_error_client (DBusAuth *auth,
1728 const DBusString *command,
1729 const DBusString *args)
1731 /* Cancel current mechanism, as we don't have anything
1732 * more clever to do.
1734 if (!send_cancel (auth))
1741 process_unknown (DBusAuth *auth,
1742 const DBusString *command,
1743 const DBusString *args)
1745 if (!send_error (auth, "Unknown command"))
1751 /* returns whether to call it again right away */
1753 process_command (DBusAuth *auth)
1761 /* _dbus_verbose ("%s: trying process_command()\n"); */
1766 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1769 if (!_dbus_string_init (&command))
1771 auth->needed_memory = TRUE;
1775 if (!_dbus_string_init (&args))
1777 _dbus_string_free (&command);
1778 auth->needed_memory = TRUE;
1782 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1785 if (!_dbus_string_validate_ascii (&command, 0,
1786 _dbus_string_get_length (&command)))
1788 _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
1789 DBUS_AUTH_NAME (auth));
1790 if (!send_error (auth, "Command contained non-ASCII"))
1796 _dbus_verbose ("%s: got command \"%s\"\n",
1797 DBUS_AUTH_NAME (auth),
1798 _dbus_string_get_const_data (&command));
1800 _dbus_string_find_blank (&command, 0, &i);
1801 _dbus_string_skip_blank (&command, i, &j);
1804 _dbus_string_delete (&command, i, j - i);
1806 if (!_dbus_string_move (&command, i, &args, 0))
1810 while (auth->handlers[i].command != NULL)
1812 if (_dbus_string_equal_c_str (&command,
1813 auth->handlers[i].command))
1815 _dbus_verbose ("%s: Processing auth command %s\n",
1816 DBUS_AUTH_NAME (auth),
1817 auth->handlers[i].command);
1819 if (!(* auth->handlers[i].func) (auth, &command, &args))
1827 if (auth->handlers[i].command == NULL)
1829 if (!process_unknown (auth, &command, &args))
1835 /* We've succeeded in processing the whole command so drop it out
1836 * of the incoming buffer and return TRUE to try another command.
1839 _dbus_string_delete (&auth->incoming, 0, eol);
1842 _dbus_string_delete (&auth->incoming, 0, 2);
1847 _dbus_string_free (&args);
1848 _dbus_string_free (&command);
1851 auth->needed_memory = TRUE;
1853 auth->needed_memory = FALSE;
1862 * @addtogroup DBusAuth
1867 * Creates a new auth conversation object for the server side.
1868 * See doc/dbus-sasl-profile.txt for full details on what
1871 * @returns the new object or #NULL if no memory
1874 _dbus_auth_server_new (void)
1877 DBusAuthServer *server_auth;
1879 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1883 auth->handlers = server_handlers;
1885 server_auth = DBUS_AUTH_SERVER (auth);
1887 /* perhaps this should be per-mechanism with a lower
1890 server_auth->failures = 0;
1891 server_auth->max_failures = 6;
1897 * Creates a new auth conversation object for the client side.
1898 * See doc/dbus-sasl-profile.txt for full details on what
1901 * @returns the new object or #NULL if no memory
1904 _dbus_auth_client_new (void)
1908 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1912 auth->handlers = client_handlers;
1914 /* Start the auth conversation by sending AUTH for our default
1916 if (!send_auth (auth, &all_mechanisms[0]))
1918 _dbus_auth_unref (auth);
1926 * Increments the refcount of an auth object.
1928 * @param auth the auth conversation
1929 * @returns the auth conversation
1932 _dbus_auth_ref (DBusAuth *auth)
1934 _dbus_assert (auth != NULL);
1936 auth->refcount += 1;
1942 * Decrements the refcount of an auth object.
1944 * @param auth the auth conversation
1947 _dbus_auth_unref (DBusAuth *auth)
1949 _dbus_assert (auth != NULL);
1950 _dbus_assert (auth->refcount > 0);
1952 auth->refcount -= 1;
1953 if (auth->refcount == 0)
1955 shutdown_mech (auth);
1957 if (DBUS_AUTH_IS_CLIENT (auth))
1959 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1963 _dbus_keyring_unref (auth->keyring);
1965 _dbus_string_free (&auth->context);
1966 _dbus_string_free (&auth->challenge);
1967 _dbus_string_free (&auth->identity);
1968 _dbus_string_free (&auth->incoming);
1969 _dbus_string_free (&auth->outgoing);
1971 dbus_free_string_array (auth->allowed_mechs);
1978 * Sets an array of authentication mechanism names
1979 * that we are willing to use.
1981 * @param auth the auth conversation
1982 * @param mechanisms #NULL-terminated array of mechanism names
1983 * @returns #FALSE if no memory
1986 _dbus_auth_set_mechanisms (DBusAuth *auth,
1987 const char **mechanisms)
1991 if (mechanisms != NULL)
1993 copy = _dbus_dup_string_array (mechanisms);
2000 dbus_free_string_array (auth->allowed_mechs);
2002 auth->allowed_mechs = copy;
2008 * @param auth the auth conversation object
2009 * @returns #TRUE if we're in a final state
2011 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
2014 * Analyzes buffered input and moves the auth conversation forward,
2015 * returning the new state of the auth conversation.
2017 * @param auth the auth conversation
2018 * @returns the new state
2021 _dbus_auth_do_work (DBusAuth *auth)
2023 auth->needed_memory = FALSE;
2025 /* Max amount we'll buffer up before deciding someone's on crack */
2026 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2030 if (DBUS_AUTH_IN_END_STATE (auth))
2033 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2034 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2036 auth->need_disconnect = TRUE;
2037 _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2038 DBUS_AUTH_NAME (auth));
2042 while (process_command (auth));
2044 if (auth->need_disconnect)
2045 return DBUS_AUTH_STATE_NEED_DISCONNECT;
2046 else if (auth->authenticated)
2047 return DBUS_AUTH_STATE_AUTHENTICATED;
2048 else if (auth->needed_memory)
2049 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2050 else if (_dbus_string_get_length (&auth->outgoing) > 0)
2051 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2053 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2057 * Gets bytes that need to be sent to the peer we're conversing with.
2058 * After writing some bytes, _dbus_auth_bytes_sent() must be called
2059 * to notify the auth object that they were written.
2061 * @param auth the auth conversation
2062 * @param str return location for a ref to the buffer to send
2063 * @returns #FALSE if nothing to send
2066 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
2067 const DBusString **str)
2069 _dbus_assert (auth != NULL);
2070 _dbus_assert (str != NULL);
2074 if (DBUS_AUTH_IN_END_STATE (auth))
2077 if (_dbus_string_get_length (&auth->outgoing) == 0)
2080 *str = &auth->outgoing;
2086 * Notifies the auth conversation object that
2087 * the given number of bytes of the outgoing buffer
2088 * have been written out.
2090 * @param auth the auth conversation
2091 * @param bytes_sent number of bytes written out
2094 _dbus_auth_bytes_sent (DBusAuth *auth,
2097 _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2098 DBUS_AUTH_NAME (auth),
2100 _dbus_string_get_const_data (&auth->outgoing));
2102 _dbus_string_delete (&auth->outgoing,
2105 if (auth->authenticated_pending_output &&
2106 _dbus_string_get_length (&auth->outgoing) == 0)
2107 auth->authenticated = TRUE;
2111 * Get a buffer to be used for reading bytes from the peer we're conversing
2112 * with. Bytes should be appended to this buffer.
2114 * @param auth the auth conversation
2115 * @param buffer return location for buffer to append bytes to
2118 _dbus_auth_get_buffer (DBusAuth *auth,
2119 DBusString **buffer)
2121 _dbus_assert (auth != NULL);
2122 _dbus_assert (!auth->buffer_outstanding);
2124 *buffer = &auth->incoming;
2126 auth->buffer_outstanding = TRUE;
2130 * Returns a buffer with new data read into it.
2132 * @param auth the auth conversation
2133 * @param buffer the buffer being returned
2134 * @param bytes_read number of new bytes added
2137 _dbus_auth_return_buffer (DBusAuth *auth,
2141 _dbus_assert (buffer == &auth->incoming);
2142 _dbus_assert (auth->buffer_outstanding);
2144 auth->buffer_outstanding = FALSE;
2148 * Returns leftover bytes that were not used as part of the auth
2149 * conversation. These bytes will be part of the message stream
2150 * instead. This function may not be called until authentication has
2153 * @param auth the auth conversation
2154 * @param str return location for pointer to string of unused bytes
2157 _dbus_auth_get_unused_bytes (DBusAuth *auth,
2158 const DBusString **str)
2160 if (!DBUS_AUTH_IN_END_STATE (auth))
2163 *str = &auth->incoming;
2168 * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2169 * after we've gotten them and successfully moved them elsewhere.
2171 * @param auth the auth conversation
2174 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2176 if (!DBUS_AUTH_IN_END_STATE (auth))
2179 _dbus_string_set_length (&auth->incoming, 0);
2183 * Called post-authentication, indicates whether we need to encode
2184 * the message stream with _dbus_auth_encode_data() prior to
2185 * sending it to the peer.
2187 * @param auth the auth conversation
2188 * @returns #TRUE if we need to encode the stream
2191 _dbus_auth_needs_encoding (DBusAuth *auth)
2193 if (!auth->authenticated)
2196 if (auth->mech != NULL)
2198 if (DBUS_AUTH_IS_CLIENT (auth))
2199 return auth->mech->client_encode_func != NULL;
2201 return auth->mech->server_encode_func != NULL;
2208 * Called post-authentication, encodes a block of bytes for sending to
2209 * the peer. If no encoding was negotiated, just copies the bytes
2210 * (you can avoid this by checking _dbus_auth_needs_encoding()).
2212 * @param auth the auth conversation
2213 * @param plaintext the plain text data
2214 * @param encoded initialized string to where encoded data is appended
2215 * @returns #TRUE if we had enough memory and successfully encoded
2218 _dbus_auth_encode_data (DBusAuth *auth,
2219 const DBusString *plaintext,
2220 DBusString *encoded)
2222 _dbus_assert (plaintext != encoded);
2224 if (!auth->authenticated)
2227 if (_dbus_auth_needs_encoding (auth))
2229 if (DBUS_AUTH_IS_CLIENT (auth))
2230 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2232 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2236 return _dbus_string_copy (plaintext, 0, encoded,
2237 _dbus_string_get_length (encoded));
2242 * Called post-authentication, indicates whether we need to decode
2243 * the message stream with _dbus_auth_decode_data() after
2244 * receiving it from the peer.
2246 * @param auth the auth conversation
2247 * @returns #TRUE if we need to encode the stream
2250 _dbus_auth_needs_decoding (DBusAuth *auth)
2252 if (!auth->authenticated)
2255 if (auth->mech != NULL)
2257 if (DBUS_AUTH_IS_CLIENT (auth))
2258 return auth->mech->client_decode_func != NULL;
2260 return auth->mech->server_decode_func != NULL;
2268 * Called post-authentication, decodes a block of bytes received from
2269 * the peer. If no encoding was negotiated, just copies the bytes (you
2270 * can avoid this by checking _dbus_auth_needs_decoding()).
2272 * @todo We need to be able to distinguish "out of memory" error
2273 * from "the data is hosed" error.
2275 * @param auth the auth conversation
2276 * @param encoded the encoded data
2277 * @param plaintext initialized string where decoded data is appended
2278 * @returns #TRUE if we had enough memory and successfully decoded
2281 _dbus_auth_decode_data (DBusAuth *auth,
2282 const DBusString *encoded,
2283 DBusString *plaintext)
2285 _dbus_assert (plaintext != encoded);
2287 if (!auth->authenticated)
2290 if (_dbus_auth_needs_decoding (auth))
2292 if (DBUS_AUTH_IS_CLIENT (auth))
2293 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2295 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2299 return _dbus_string_copy (encoded, 0, plaintext,
2300 _dbus_string_get_length (plaintext));
2305 * Sets credentials received via reliable means from the operating
2308 * @param auth the auth conversation
2309 * @param credentials the credentials received
2312 _dbus_auth_set_credentials (DBusAuth *auth,
2313 const DBusCredentials *credentials)
2315 auth->credentials = *credentials;
2319 * Gets the identity we authorized the client as. Apps may have
2320 * different policies as to what identities they allow.
2322 * @param auth the auth conversation
2323 * @param credentials the credentials we've authorized
2326 _dbus_auth_get_identity (DBusAuth *auth,
2327 DBusCredentials *credentials)
2329 if (auth->authenticated)
2330 *credentials = auth->authorized_identity;
2332 _dbus_credentials_clear (credentials);
2336 * Sets the "authentication context" which scopes cookies
2337 * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2339 * @param auth the auth conversation
2340 * @param context the context
2341 * @returns #FALSE if no memory
2344 _dbus_auth_set_context (DBusAuth *auth,
2345 const DBusString *context)
2347 return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2348 &auth->context, 0, _dbus_string_get_length (context));
2353 #ifdef DBUS_BUILD_TESTS
2354 #include "dbus-test.h"
2355 #include "dbus-auth-script.h"
2359 process_test_subdir (const DBusString *test_base_dir,
2362 DBusString test_directory;
2363 DBusString filename;
2371 if (!_dbus_string_init (&test_directory))
2372 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2374 _dbus_string_init_const (&filename, subdir);
2376 if (!_dbus_string_copy (test_base_dir, 0,
2377 &test_directory, 0))
2378 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2380 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2381 _dbus_assert_not_reached ("couldn't allocate full path");
2383 _dbus_string_free (&filename);
2384 if (!_dbus_string_init (&filename))
2385 _dbus_assert_not_reached ("didn't allocate filename string\n");
2387 dbus_error_init (&error);
2388 dir = _dbus_directory_open (&test_directory, &error);
2391 _dbus_warn ("Could not open %s: %s\n",
2392 _dbus_string_get_const_data (&test_directory),
2394 dbus_error_free (&error);
2398 printf ("Testing %s:\n", subdir);
2401 while (_dbus_directory_get_next_file (dir, &filename, &error))
2403 DBusString full_path;
2405 if (!_dbus_string_init (&full_path))
2406 _dbus_assert_not_reached ("couldn't init string");
2408 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2409 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2411 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2412 _dbus_assert_not_reached ("couldn't concat file to dir");
2414 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2416 _dbus_verbose ("Skipping non-.auth-script file %s\n",
2417 _dbus_string_get_const_data (&filename));
2418 _dbus_string_free (&full_path);
2422 printf (" %s\n", _dbus_string_get_const_data (&filename));
2424 if (!_dbus_auth_script_run (&full_path))
2426 _dbus_string_free (&full_path);
2430 _dbus_string_free (&full_path);
2433 if (dbus_error_is_set (&error))
2435 _dbus_warn ("Could not get next file in %s: %s\n",
2436 _dbus_string_get_const_data (&test_directory), error.message);
2437 dbus_error_free (&error);
2446 _dbus_directory_close (dir);
2447 _dbus_string_free (&test_directory);
2448 _dbus_string_free (&filename);
2454 process_test_dirs (const char *test_data_dir)
2456 DBusString test_directory;
2461 _dbus_string_init_const (&test_directory, test_data_dir);
2463 if (!process_test_subdir (&test_directory, "auth"))
2470 _dbus_string_free (&test_directory);
2476 _dbus_auth_test (const char *test_data_dir)
2479 if (test_data_dir == NULL)
2482 if (!process_test_dirs (test_data_dir))
2488 #endif /* DBUS_BUILD_TESTS */