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"
28 /* See doc/dbus-sasl-profile.txt */
31 * @defgroup DBusAuth Authentication
32 * @ingroup DBusInternals
33 * @brief DBusAuth object
35 * DBusAuth manages the authentication negotiation when a connection
36 * is first established, and also manage any encryption used over a
39 * The file doc/dbus-sasl-profile.txt documents the network protocol
40 * used for authentication.
42 * @todo some SASL profiles require sending the empty string as a
43 * challenge/response, but we don't currently allow that in our
48 * @defgroup DBusAuthInternals Authentication implementation details
49 * @ingroup DBusInternals
50 * @brief DBusAuth implementation details
52 * Private details of authentication code.
58 * Processes a command. Returns whether we had enough memory to
59 * complete the operation.
61 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth *auth,
62 const DBusString *command,
63 const DBusString *args);
68 DBusProcessAuthCommandFunction func;
69 } DBusAuthCommandHandler;
72 * This function appends an initial client response to the given string
74 typedef dbus_bool_t (* DBusInitialResponseFunction) (DBusAuth *auth,
75 DBusString *response);
78 * This function processes a block of data received from the peer.
79 * i.e. handles a DATA command.
81 typedef dbus_bool_t (* DBusAuthDataFunction) (DBusAuth *auth,
82 const DBusString *data);
85 * This function encodes a block of data from the peer.
87 typedef dbus_bool_t (* DBusAuthEncodeFunction) (DBusAuth *auth,
88 const DBusString *data,
92 * This function decodes a block of data from the peer.
94 typedef dbus_bool_t (* DBusAuthDecodeFunction) (DBusAuth *auth,
95 const DBusString *data,
99 * This function is called when the mechanism is abandoned.
101 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
105 const char *mechanism;
106 DBusAuthDataFunction server_data_func;
107 DBusAuthEncodeFunction server_encode_func;
108 DBusAuthDecodeFunction server_decode_func;
109 DBusAuthShutdownFunction server_shutdown_func;
110 DBusInitialResponseFunction client_initial_response_func;
111 DBusAuthDataFunction client_data_func;
112 DBusAuthEncodeFunction client_encode_func;
113 DBusAuthDecodeFunction client_decode_func;
114 DBusAuthShutdownFunction client_shutdown_func;
115 } DBusAuthMechanismHandler;
118 * Internal members of DBusAuth.
122 int refcount; /**< reference count */
124 DBusString incoming; /**< Incoming data buffer */
125 DBusString outgoing; /**< Outgoing data buffer */
127 const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
129 const DBusAuthMechanismHandler *mech; /**< Current auth mechanism */
131 DBusString identity; /**< Current identity we're authorizing
135 DBusCredentials credentials; /**< Credentials, fields may be -1 */
137 DBusCredentials authorized_identity; /**< Credentials that are authorized */
139 unsigned int needed_memory : 1; /**< We needed memory to continue since last
140 * successful getting something done
142 unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
143 unsigned int authenticated : 1; /**< We are authenticated */
144 unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
145 unsigned int authenticated_pending_begin : 1; /**< Authenticated once we get BEGIN */
146 unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
147 unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
154 DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
162 int failures; /**< Number of times client has been rejected */
163 int max_failures; /**< Number of times we reject before disconnect */
167 static dbus_bool_t process_auth (DBusAuth *auth,
168 const DBusString *command,
169 const DBusString *args);
170 static dbus_bool_t process_cancel (DBusAuth *auth,
171 const DBusString *command,
172 const DBusString *args);
173 static dbus_bool_t process_begin (DBusAuth *auth,
174 const DBusString *command,
175 const DBusString *args);
176 static dbus_bool_t process_data_server (DBusAuth *auth,
177 const DBusString *command,
178 const DBusString *args);
179 static dbus_bool_t process_error_server (DBusAuth *auth,
180 const DBusString *command,
181 const DBusString *args);
182 static dbus_bool_t process_rejected (DBusAuth *auth,
183 const DBusString *command,
184 const DBusString *args);
185 static dbus_bool_t process_ok (DBusAuth *auth,
186 const DBusString *command,
187 const DBusString *args);
188 static dbus_bool_t process_data_client (DBusAuth *auth,
189 const DBusString *command,
190 const DBusString *args);
191 static dbus_bool_t process_error_client (DBusAuth *auth,
192 const DBusString *command,
193 const DBusString *args);
196 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
197 static dbus_bool_t send_rejected (DBusAuth *auth);
199 static DBusAuthCommandHandler
200 server_handlers[] = {
201 { "AUTH", process_auth },
202 { "CANCEL", process_cancel },
203 { "BEGIN", process_begin },
204 { "DATA", process_data_server },
205 { "ERROR", process_error_server },
209 static DBusAuthCommandHandler
210 client_handlers[] = {
211 { "REJECTED", process_rejected },
212 { "OK", process_ok },
213 { "DATA", process_data_client },
214 { "ERROR", process_error_client },
219 * @param auth the auth conversation
220 * @returns #TRUE if the conversation is the server side
222 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
224 * @param auth the auth conversation
225 * @returns #TRUE if the conversation is the client side
227 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
229 * @param auth the auth conversation
230 * @returns auth cast to DBusAuthClient
232 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
234 * @param auth the auth conversation
235 * @returns auth cast to DBusAuthServer
237 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
240 _dbus_auth_new (int size)
244 auth = dbus_malloc0 (size);
250 auth->credentials.pid = -1;
251 auth->credentials.uid = -1;
252 auth->credentials.gid = -1;
254 auth->authorized_identity.pid = -1;
255 auth->authorized_identity.uid = -1;
256 auth->authorized_identity.gid = -1;
258 /* note that we don't use the max string length feature,
259 * because you can't use that feature if you're going to
260 * try to recover from out-of-memory (it creates
261 * what looks like unrecoverable inability to alloc
262 * more space in the string). But we do handle
263 * overlong buffers in _dbus_auth_do_work().
266 if (!_dbus_string_init (&auth->incoming, _DBUS_INT_MAX))
272 if (!_dbus_string_init (&auth->outgoing, _DBUS_INT_MAX))
274 _dbus_string_free (&auth->incoming);
279 if (!_dbus_string_init (&auth->identity, _DBUS_INT_MAX))
281 _dbus_string_free (&auth->incoming);
282 _dbus_string_free (&auth->outgoing);
291 shutdown_mech (DBusAuth *auth)
293 /* Cancel any auth */
294 auth->authenticated_pending_begin = FALSE;
295 auth->authenticated = FALSE;
296 auth->already_asked_for_initial_response = FALSE;
297 _dbus_string_set_length (&auth->identity, 0);
298 auth->authorized_identity.pid = -1;
299 auth->authorized_identity.uid = -1;
300 auth->authorized_identity.gid = -1;
302 if (auth->mech != NULL)
304 _dbus_verbose ("Shutting down mechanism %s\n",
305 auth->mech->mechanism);
307 if (DBUS_AUTH_IS_CLIENT (auth))
308 (* auth->mech->client_shutdown_func) (auth);
310 (* auth->mech->server_shutdown_func) (auth);
317 handle_server_data_stupid_test_mech (DBusAuth *auth,
318 const DBusString *data)
320 if (!_dbus_string_append (&auth->outgoing,
324 _dbus_credentials_from_current_process (&auth->authorized_identity);
326 auth->authenticated_pending_begin = TRUE;
332 handle_server_shutdown_stupid_test_mech (DBusAuth *auth)
338 handle_client_data_stupid_test_mech (DBusAuth *auth,
339 const DBusString *data)
346 handle_client_shutdown_stupid_test_mech (DBusAuth *auth)
351 /* the stupid test mech is a base64-encoded string;
352 * all the inefficiency, none of the security!
355 handle_encode_stupid_test_mech (DBusAuth *auth,
356 const DBusString *plaintext,
359 if (!_dbus_string_base64_encode (plaintext, 0, encoded,
360 _dbus_string_get_length (encoded)))
367 handle_decode_stupid_test_mech (DBusAuth *auth,
368 const DBusString *encoded,
369 DBusString *plaintext)
371 if (!_dbus_string_base64_decode (encoded, 0, plaintext,
372 _dbus_string_get_length (plaintext)))
379 handle_server_data_external_mech (DBusAuth *auth,
380 const DBusString *data)
382 DBusCredentials desired_identity;
384 if (auth->credentials.uid < 0)
386 _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
387 return send_rejected (auth);
390 if (_dbus_string_get_length (data) > 0)
392 if (_dbus_string_get_length (&auth->identity) > 0)
394 /* Tried to send two auth identities, wtf */
395 _dbus_verbose ("client tried to send auth identity, but we already have one\n");
396 return send_rejected (auth);
400 /* this is our auth identity */
401 if (!_dbus_string_copy (data, 0, &auth->identity, 0))
406 /* Poke client for an auth identity, if none given */
407 if (_dbus_string_get_length (&auth->identity) == 0 &&
408 !auth->already_asked_for_initial_response)
410 if (_dbus_string_append (&auth->outgoing,
413 _dbus_verbose ("sending empty challenge asking client for auth identity\n");
414 auth->already_asked_for_initial_response = TRUE;
421 desired_identity.pid = -1;
422 desired_identity.uid = -1;
423 desired_identity.gid = -1;
425 /* If auth->identity is still empty here, then client
426 * responded with an empty string after we poked it for
427 * an initial response. This means to try to auth the
428 * identity provided in the credentials.
430 if (_dbus_string_get_length (&auth->identity) == 0)
432 desired_identity.uid = auth->credentials.uid;
436 if (!_dbus_credentials_from_uid_string (&auth->identity,
439 _dbus_verbose ("could not get credentials from uid string\n");
440 return send_rejected (auth);
444 if (desired_identity.uid < 0)
446 _dbus_verbose ("desired UID %d is no good\n", desired_identity.uid);
447 return send_rejected (auth);
450 if (_dbus_credentials_match (&desired_identity,
453 /* client has authenticated */
454 _dbus_verbose ("authenticated client with UID %d matching socket credentials UID %d\n",
455 desired_identity.uid,
456 auth->credentials.uid);
458 if (!_dbus_string_append (&auth->outgoing,
462 auth->authorized_identity.uid = desired_identity.uid;
464 auth->authenticated_pending_begin = TRUE;
470 _dbus_verbose ("credentials uid=%d gid=%d do not allow uid=%d gid=%d\n",
471 auth->credentials.uid, auth->credentials.gid,
472 desired_identity.uid, desired_identity.gid);
473 return send_rejected (auth);
478 handle_server_shutdown_external_mech (DBusAuth *auth)
484 handle_client_initial_response_external_mech (DBusAuth *auth,
485 DBusString *response)
487 /* We always append our UID as an initial response, so the server
488 * doesn't have to send back an empty challenge to check whether we
489 * want to specify an identity. i.e. this avoids a round trip that
490 * the spec for the EXTERNAL mechanism otherwise requires.
492 DBusString plaintext;
494 if (!_dbus_string_init (&plaintext, _DBUS_INT_MAX))
497 if (!_dbus_string_append_our_uid (&plaintext))
500 if (!_dbus_string_base64_encode (&plaintext, 0,
502 _dbus_string_get_length (response)))
505 _dbus_string_free (&plaintext);
510 _dbus_string_free (&plaintext);
515 handle_client_data_external_mech (DBusAuth *auth,
516 const DBusString *data)
523 handle_client_shutdown_external_mech (DBusAuth *auth)
528 /* Put mechanisms here in order of preference.
529 * What I eventually want to have is:
531 * - a mechanism that checks UNIX domain socket credentials
532 * - a simple magic cookie mechanism like X11 or ICE
533 * - mechanisms that chain to Cyrus SASL, so we can use anything it
534 * offers such as Kerberos, X509, whatever.
537 static const DBusAuthMechanismHandler
540 handle_server_data_external_mech,
542 handle_server_shutdown_external_mech,
543 handle_client_initial_response_external_mech,
544 handle_client_data_external_mech,
546 handle_client_shutdown_external_mech },
547 /* Obviously this has to die for production use */
548 { "DBUS_STUPID_TEST_MECH",
549 handle_server_data_stupid_test_mech,
550 handle_encode_stupid_test_mech,
551 handle_decode_stupid_test_mech,
552 handle_server_shutdown_stupid_test_mech,
554 handle_client_data_stupid_test_mech,
555 handle_encode_stupid_test_mech,
556 handle_decode_stupid_test_mech,
557 handle_client_shutdown_stupid_test_mech },
561 static const DBusAuthMechanismHandler*
562 find_mech (const DBusString *name)
567 while (all_mechanisms[i].mechanism != NULL)
569 if (_dbus_string_equal_c_str (name,
570 all_mechanisms[i].mechanism))
572 return &all_mechanisms[i];
581 send_rejected (DBusAuth *auth)
584 DBusAuthServer *server_auth;
587 if (!_dbus_string_init (&command, _DBUS_INT_MAX))
590 if (!_dbus_string_append (&command,
595 while (all_mechanisms[i].mechanism != NULL)
597 if (!_dbus_string_append (&command,
601 if (!_dbus_string_append (&command,
602 all_mechanisms[i].mechanism))
608 if (!_dbus_string_append (&command, "\r\n"))
611 if (!_dbus_string_copy (&command, 0, &auth->outgoing,
612 _dbus_string_get_length (&auth->outgoing)))
615 shutdown_mech (auth);
617 _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
618 server_auth = DBUS_AUTH_SERVER (auth);
619 server_auth->failures += 1;
624 _dbus_string_free (&command);
629 process_auth (DBusAuth *auth,
630 const DBusString *command,
631 const DBusString *args)
635 /* We are already using a mechanism, client is on crack */
636 if (!_dbus_string_append (&auth->outgoing,
637 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
642 else if (_dbus_string_get_length (args) == 0)
644 /* No args to the auth, send mechanisms */
645 if (!send_rejected (auth))
654 DBusString base64_response;
655 DBusString decoded_response;
657 _dbus_string_find_blank (args, 0, &i);
659 if (!_dbus_string_init (&mech, _DBUS_INT_MAX))
662 if (!_dbus_string_init (&base64_response, _DBUS_INT_MAX))
664 _dbus_string_free (&mech);
668 if (!_dbus_string_init (&decoded_response, _DBUS_INT_MAX))
670 _dbus_string_free (&mech);
671 _dbus_string_free (&base64_response);
675 if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
678 if (!_dbus_string_copy (args, i, &base64_response, 0))
681 if (!_dbus_string_base64_decode (&base64_response, 0,
682 &decoded_response, 0))
685 auth->mech = find_mech (&mech);
686 if (auth->mech != NULL)
688 _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
689 auth->mech->mechanism,
690 _dbus_string_get_length (&decoded_response));
692 if (!(* auth->mech->server_data_func) (auth,
698 /* Unsupported mechanism */
699 if (!send_rejected (auth))
703 _dbus_string_free (&mech);
704 _dbus_string_free (&base64_response);
705 _dbus_string_free (&decoded_response);
711 _dbus_string_free (&mech);
712 _dbus_string_free (&base64_response);
713 _dbus_string_free (&decoded_response);
719 process_cancel (DBusAuth *auth,
720 const DBusString *command,
721 const DBusString *args)
723 shutdown_mech (auth);
729 process_begin (DBusAuth *auth,
730 const DBusString *command,
731 const DBusString *args)
733 if (auth->authenticated_pending_begin)
734 auth->authenticated = TRUE;
737 auth->need_disconnect = TRUE; /* client trying to send data before auth,
740 shutdown_mech (auth);
747 process_data_server (DBusAuth *auth,
748 const DBusString *command,
749 const DBusString *args)
751 if (auth->mech != NULL)
755 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
758 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
760 _dbus_string_free (&decoded);
764 if (!(* auth->mech->server_data_func) (auth, &decoded))
766 _dbus_string_free (&decoded);
770 _dbus_string_free (&decoded);
774 if (!_dbus_string_append (&auth->outgoing,
775 "ERROR \"Not currently in an auth conversation\"\r\n"))
783 process_error_server (DBusAuth *auth,
784 const DBusString *command,
785 const DBusString *args)
791 /* return FALSE if no memory, TRUE if all OK */
793 get_word (const DBusString *str,
799 _dbus_string_skip_blank (str, *start, start);
800 _dbus_string_find_blank (str, *start, &i);
804 if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
814 record_mechanisms (DBusAuth *auth,
815 const DBusString *command,
816 const DBusString *args)
821 if (auth->already_got_mechanisms)
824 len = _dbus_string_get_length (args);
830 const DBusAuthMechanismHandler *mech;
832 if (!_dbus_string_init (&m, _DBUS_INT_MAX))
835 if (!get_word (args, &next, &m))
838 mech = find_mech (&m);
842 /* FIXME right now we try mechanisms in the order
843 * the server lists them; should we do them in
844 * some more deterministic order?
846 * Probably in all_mechanisms order, our order of
847 * preference. Of course when the server is us,
848 * it lists things in that order anyhow.
851 _dbus_verbose ("Adding mechanism %s to list we will try\n",
854 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
862 _dbus_string_get_const_data (&m, &s);
863 _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
867 _dbus_string_free (&m);
870 auth->already_got_mechanisms = TRUE;
875 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
881 client_try_next_mechanism (DBusAuth *auth)
883 const DBusAuthMechanismHandler *mech;
884 DBusString auth_command;
886 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
889 mech = DBUS_AUTH_CLIENT (auth)->mechs_to_try->data;
891 if (!_dbus_string_init (&auth_command, _DBUS_INT_MAX))
894 if (!_dbus_string_append (&auth_command,
897 _dbus_string_free (&auth_command);
901 if (!_dbus_string_append (&auth_command,
904 _dbus_string_free (&auth_command);
908 if (mech->client_initial_response_func != NULL)
910 if (!_dbus_string_append (&auth_command, " "))
912 _dbus_string_free (&auth_command);
916 if (!(* mech->client_initial_response_func) (auth, &auth_command))
918 _dbus_string_free (&auth_command);
923 if (!_dbus_string_append (&auth_command,
926 _dbus_string_free (&auth_command);
930 if (!_dbus_string_copy (&auth_command, 0,
932 _dbus_string_get_length (&auth->outgoing)))
934 _dbus_string_free (&auth_command);
939 _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
941 _dbus_verbose ("Trying mechanism %s\n",
942 auth->mech->mechanism);
944 _dbus_string_free (&auth_command);
950 process_rejected (DBusAuth *auth,
951 const DBusString *command,
952 const DBusString *args)
954 shutdown_mech (auth);
956 if (!auth->already_got_mechanisms)
958 if (!record_mechanisms (auth, command, args))
962 if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
964 client_try_next_mechanism (auth);
969 auth->need_disconnect = TRUE;
976 process_ok (DBusAuth *auth,
977 const DBusString *command,
978 const DBusString *args)
980 if (!_dbus_string_append (&auth->outgoing,
984 auth->authenticated_pending_output = TRUE;
991 process_data_client (DBusAuth *auth,
992 const DBusString *command,
993 const DBusString *args)
995 if (auth->mech != NULL)
999 if (!_dbus_string_init (&decoded, _DBUS_INT_MAX))
1002 if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1004 _dbus_string_free (&decoded);
1008 if (!(* auth->mech->client_data_func) (auth, &decoded))
1010 _dbus_string_free (&decoded);
1014 _dbus_string_free (&decoded);
1018 if (!_dbus_string_append (&auth->outgoing,
1019 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1027 process_error_client (DBusAuth *auth,
1028 const DBusString *command,
1029 const DBusString *args)
1035 process_unknown (DBusAuth *auth,
1036 const DBusString *command,
1037 const DBusString *args)
1039 if (!_dbus_string_append (&auth->outgoing,
1040 "ERROR \"Unknown command\"\r\n"))
1046 /* returns whether to call it again right away */
1048 process_command (DBusAuth *auth)
1056 /* _dbus_verbose (" trying process_command()\n"); */
1061 if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1064 if (!_dbus_string_init (&command, _DBUS_INT_MAX))
1066 auth->needed_memory = TRUE;
1070 if (!_dbus_string_init (&args, _DBUS_INT_MAX))
1072 auth->needed_memory = TRUE;
1076 if (eol > _DBUS_ONE_MEGABYTE)
1078 /* This is a giant line, someone is trying to hose us. */
1079 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1085 if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1088 if (!_dbus_string_validate_ascii (&command, 0,
1089 _dbus_string_get_length (&command)))
1091 _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1092 if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1100 _dbus_string_get_const_data (&command, &q);
1101 _dbus_verbose ("got command \"%s\"\n", q);
1104 _dbus_string_find_blank (&command, 0, &i);
1105 _dbus_string_skip_blank (&command, i, &j);
1108 _dbus_string_delete (&command, i, j - i);
1110 if (!_dbus_string_move (&command, i, &args, 0))
1114 while (auth->handlers[i].command != NULL)
1116 if (_dbus_string_equal_c_str (&command,
1117 auth->handlers[i].command))
1119 _dbus_verbose ("Processing auth command %s\n",
1120 auth->handlers[i].command);
1122 if (!(* auth->handlers[i].func) (auth, &command, &args))
1130 if (auth->handlers[i].command == NULL)
1132 if (!process_unknown (auth, &command, &args))
1138 /* We've succeeded in processing the whole command so drop it out
1139 * of the incoming buffer and return TRUE to try another command.
1142 _dbus_string_delete (&auth->incoming, 0, eol);
1145 _dbus_string_delete (&auth->incoming, 0, 2);
1150 _dbus_string_free (&args);
1151 _dbus_string_free (&command);
1154 auth->needed_memory = TRUE;
1156 auth->needed_memory = FALSE;
1165 * @addtogroup DBusAuth
1170 * Creates a new auth conversation object for the server side.
1171 * See doc/dbus-sasl-profile.txt for full details on what
1174 * @returns the new object or #NULL if no memory
1177 _dbus_auth_server_new (void)
1180 DBusAuthServer *server_auth;
1182 auth = _dbus_auth_new (sizeof (DBusAuthServer));
1186 auth->handlers = server_handlers;
1188 server_auth = DBUS_AUTH_SERVER (auth);
1190 /* perhaps this should be per-mechanism with a lower
1193 server_auth->failures = 0;
1194 server_auth->max_failures = 6;
1200 * Creates a new auth conversation object for the client side.
1201 * See doc/dbus-sasl-profile.txt for full details on what
1204 * @returns the new object or #NULL if no memory
1207 _dbus_auth_client_new (void)
1211 auth = _dbus_auth_new (sizeof (DBusAuthClient));
1215 auth->handlers = client_handlers;
1217 /* Add a default mechanism to try */
1218 if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1219 (void*) &all_mechanisms[0]))
1221 _dbus_auth_unref (auth);
1225 /* Now try the mechanism we just added */
1226 if (!client_try_next_mechanism (auth))
1228 _dbus_auth_unref (auth);
1236 * Increments the refcount of an auth object.
1238 * @param auth the auth conversation
1241 _dbus_auth_ref (DBusAuth *auth)
1243 _dbus_assert (auth != NULL);
1245 auth->refcount += 1;
1249 * Decrements the refcount of an auth object.
1251 * @param auth the auth conversation
1254 _dbus_auth_unref (DBusAuth *auth)
1256 _dbus_assert (auth != NULL);
1257 _dbus_assert (auth->refcount > 0);
1259 auth->refcount -= 1;
1260 if (auth->refcount == 0)
1262 shutdown_mech (auth);
1264 if (DBUS_AUTH_IS_CLIENT (auth))
1266 _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1269 _dbus_string_free (&auth->identity);
1270 _dbus_string_free (&auth->incoming);
1271 _dbus_string_free (&auth->outgoing);
1277 * @param auth the auth conversation object
1278 * @returns #TRUE if we're in a final state
1280 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1283 * Analyzes buffered input and moves the auth conversation forward,
1284 * returning the new state of the auth conversation.
1286 * @param auth the auth conversation
1287 * @returns the new state
1290 _dbus_auth_do_work (DBusAuth *auth)
1292 auth->needed_memory = FALSE;
1294 /* Max amount we'll buffer up before deciding someone's on crack */
1295 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1299 if (DBUS_AUTH_IN_END_STATE (auth))
1302 if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1303 _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1305 auth->need_disconnect = TRUE;
1306 _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1310 if (auth->mech == NULL &&
1311 auth->already_got_mechanisms &&
1312 DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1314 auth->need_disconnect = TRUE;
1315 _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1319 while (process_command (auth));
1321 if (DBUS_AUTH_IS_SERVER (auth) &&
1322 DBUS_AUTH_SERVER (auth)->failures >=
1323 DBUS_AUTH_SERVER (auth)->max_failures)
1324 auth->need_disconnect = TRUE;
1326 if (auth->need_disconnect)
1327 return DBUS_AUTH_STATE_NEED_DISCONNECT;
1328 else if (auth->authenticated)
1330 if (_dbus_string_get_length (&auth->incoming) > 0)
1331 return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1333 return DBUS_AUTH_STATE_AUTHENTICATED;
1335 else if (auth->needed_memory)
1336 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1337 else if (_dbus_string_get_length (&auth->outgoing) > 0)
1338 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1340 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1344 * Gets bytes that need to be sent to the peer we're conversing with.
1345 * After writing some bytes, _dbus_auth_bytes_sent() must be called
1346 * to notify the auth object that they were written.
1348 * @param auth the auth conversation
1349 * @param str return location for a ref to the buffer to send
1350 * @returns #FALSE if nothing to send
1353 _dbus_auth_get_bytes_to_send (DBusAuth *auth,
1354 const DBusString **str)
1356 _dbus_assert (auth != NULL);
1357 _dbus_assert (str != NULL);
1361 if (DBUS_AUTH_IN_END_STATE (auth))
1364 if (_dbus_string_get_length (&auth->outgoing) == 0)
1367 *str = &auth->outgoing;
1373 * Notifies the auth conversation object that
1374 * the given number of bytes of the outgoing buffer
1375 * have been written out.
1377 * @param auth the auth conversation
1378 * @param bytes_sent number of bytes written out
1381 _dbus_auth_bytes_sent (DBusAuth *auth,
1384 _dbus_string_delete (&auth->outgoing,
1387 if (auth->authenticated_pending_output &&
1388 _dbus_string_get_length (&auth->outgoing) == 0)
1389 auth->authenticated = TRUE;
1393 * Stores bytes received from the peer we're conversing with.
1395 * @param auth the auth conversation
1396 * @param str the received bytes.
1397 * @returns #FALSE if not enough memory to store the bytes or we were already authenticated.
1400 _dbus_auth_bytes_received (DBusAuth *auth,
1401 const DBusString *str)
1403 _dbus_assert (auth != NULL);
1404 _dbus_assert (str != NULL);
1406 if (DBUS_AUTH_IN_END_STATE (auth))
1409 auth->needed_memory = FALSE;
1411 if (!_dbus_string_copy (str, 0,
1413 _dbus_string_get_length (&auth->incoming)))
1415 auth->needed_memory = TRUE;
1419 _dbus_auth_do_work (auth);
1425 * Returns leftover bytes that were not used as part of the auth
1426 * conversation. These bytes will be part of the message stream
1427 * instead. This function may not be called until authentication has
1430 * @param auth the auth conversation
1431 * @param str string to append the unused bytes to
1432 * @returns #FALSE if not enough memory to return the bytes
1435 _dbus_auth_get_unused_bytes (DBusAuth *auth,
1438 if (!DBUS_AUTH_IN_END_STATE (auth))
1441 if (!_dbus_string_move (&auth->incoming,
1443 _dbus_string_get_length (str)))
1450 * Called post-authentication, indicates whether we need to encode
1451 * the message stream with _dbus_auth_encode_data() prior to
1452 * sending it to the peer.
1454 * @param auth the auth conversation
1455 * @returns #TRUE if we need to encode the stream
1458 _dbus_auth_needs_encoding (DBusAuth *auth)
1460 if (!auth->authenticated)
1463 if (auth->mech != NULL)
1465 if (DBUS_AUTH_IS_CLIENT (auth))
1466 return auth->mech->client_encode_func != NULL;
1468 return auth->mech->server_encode_func != NULL;
1475 * Called post-authentication, encodes a block of bytes for sending to
1476 * the peer. If no encoding was negotiated, just copies the bytes
1477 * (you can avoid this by checking _dbus_auth_needs_encoding()).
1479 * @param auth the auth conversation
1480 * @param plaintext the plain text data
1481 * @param encoded initialized string to where encoded data is appended
1482 * @returns #TRUE if we had enough memory and successfully encoded
1485 _dbus_auth_encode_data (DBusAuth *auth,
1486 const DBusString *plaintext,
1487 DBusString *encoded)
1489 _dbus_assert (plaintext != encoded);
1491 if (!auth->authenticated)
1494 if (_dbus_auth_needs_encoding (auth))
1496 if (DBUS_AUTH_IS_CLIENT (auth))
1497 return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
1499 return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
1503 return _dbus_string_copy (plaintext, 0, encoded,
1504 _dbus_string_get_length (encoded));
1509 * Called post-authentication, indicates whether we need to decode
1510 * the message stream with _dbus_auth_decode_data() after
1511 * receiving it from the peer.
1513 * @param auth the auth conversation
1514 * @returns #TRUE if we need to encode the stream
1517 _dbus_auth_needs_decoding (DBusAuth *auth)
1519 if (!auth->authenticated)
1522 if (auth->mech != NULL)
1524 if (DBUS_AUTH_IS_CLIENT (auth))
1525 return auth->mech->client_decode_func != NULL;
1527 return auth->mech->server_decode_func != NULL;
1535 * Called post-authentication, decodes a block of bytes received from
1536 * the peer. If no encoding was negotiated, just copies the bytes (you
1537 * can avoid this by checking _dbus_auth_needs_decoding()).
1539 * @todo We need to be able to distinguish "out of memory" error
1540 * from "the data is hosed" error.
1542 * @param auth the auth conversation
1543 * @param encoded the encoded data
1544 * @param plaintext initialized string where decoded data is appended
1545 * @returns #TRUE if we had enough memory and successfully decoded
1548 _dbus_auth_decode_data (DBusAuth *auth,
1549 const DBusString *encoded,
1550 DBusString *plaintext)
1552 _dbus_assert (plaintext != encoded);
1554 if (!auth->authenticated)
1557 if (_dbus_auth_needs_decoding (auth))
1559 if (DBUS_AUTH_IS_CLIENT (auth))
1560 return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
1562 return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
1566 return _dbus_string_copy (encoded, 0, plaintext,
1567 _dbus_string_get_length (plaintext));
1572 * Sets credentials received via reliable means from the operating
1575 * @param auth the auth conversation
1576 * @param credentials the credentials received
1579 _dbus_auth_set_credentials (DBusAuth *auth,
1580 const DBusCredentials *credentials)
1582 auth->credentials = *credentials;
1586 * Gets the identity we authorized the client as. Apps may have
1587 * different policies as to what identities they allow.
1589 * @param auth the auth conversation
1590 * @param credentials the credentials we've authorized
1593 _dbus_auth_get_identity (DBusAuth *auth,
1594 DBusCredentials *credentials)
1596 if (auth->authenticated)
1598 *credentials = auth->authorized_identity;
1602 credentials->pid = -1;
1603 credentials->uid = -1;
1604 credentials->gid = -1;
1610 #ifdef DBUS_BUILD_TESTS
1611 #include "dbus-test.h"
1612 #include "dbus-auth-script.h"
1616 process_test_subdir (const DBusString *test_base_dir,
1619 DBusString test_directory;
1620 DBusString filename;
1623 DBusResultCode result;
1628 if (!_dbus_string_init (&test_directory, _DBUS_INT_MAX))
1629 _dbus_assert_not_reached ("didn't allocate test_directory\n");
1631 _dbus_string_init_const (&filename, subdir);
1633 if (!_dbus_string_copy (test_base_dir, 0,
1634 &test_directory, 0))
1635 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1637 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1638 _dbus_assert_not_reached ("couldn't allocate full path");
1640 _dbus_string_free (&filename);
1641 if (!_dbus_string_init (&filename, _DBUS_INT_MAX))
1642 _dbus_assert_not_reached ("didn't allocate filename string\n");
1644 dir = _dbus_directory_open (&test_directory, &result);
1648 _dbus_string_get_const_data (&test_directory, &s);
1649 _dbus_warn ("Could not open %s: %s\n", s,
1650 dbus_result_to_string (result));
1654 printf ("Testing:\n");
1656 result = DBUS_RESULT_SUCCESS;
1658 while (_dbus_directory_get_next_file (dir, &filename, &result))
1660 DBusString full_path;
1662 if (!_dbus_string_init (&full_path, _DBUS_INT_MAX))
1663 _dbus_assert_not_reached ("couldn't init string");
1665 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1666 _dbus_assert_not_reached ("couldn't copy dir to full_path");
1668 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1669 _dbus_assert_not_reached ("couldn't concat file to dir");
1671 if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
1673 const char *filename_c;
1674 _dbus_string_get_const_data (&filename, &filename_c);
1675 _dbus_verbose ("Skipping non-.auth-script file %s\n",
1682 _dbus_string_get_const_data (&filename, &s);
1683 printf (" %s\n", s);
1686 if (!_dbus_auth_script_run (&full_path))
1688 _dbus_string_free (&full_path);
1692 _dbus_string_free (&full_path);
1695 if (result != DBUS_RESULT_SUCCESS)
1698 _dbus_string_get_const_data (&test_directory, &s);
1699 _dbus_warn ("Could not get next file in %s: %s\n",
1700 s, dbus_result_to_string (result));
1709 _dbus_directory_close (dir);
1710 _dbus_string_free (&test_directory);
1711 _dbus_string_free (&filename);
1717 process_test_dirs (const char *test_data_dir)
1719 DBusString test_directory;
1724 _dbus_string_init_const (&test_directory, test_data_dir);
1726 if (!process_test_subdir (&test_directory, "auth"))
1733 _dbus_string_free (&test_directory);
1739 _dbus_auth_test (const char *test_data_dir)
1742 if (test_data_dir == NULL)
1745 if (!process_test_dirs (test_data_dir))
1751 #endif /* DBUS_BUILD_TESTS */