D-Bus  1.10.24
dbus-auth.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-auth.c Authentication
3  *
4  * Copyright (C) 2002, 2003, 2004 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-auth.h"
26 #include "dbus-string.h"
27 #include "dbus-list.h"
28 #include "dbus-internals.h"
29 #include "dbus-keyring.h"
30 #include "dbus-sha.h"
31 #include "dbus-protocol.h"
32 #include "dbus-credentials.h"
33 
71  DBusString *response);
72 
78  const DBusString *data);
79 
84  const DBusString *data,
85  DBusString *encoded);
86 
91  const DBusString *data,
92  DBusString *decoded);
93 
97 typedef void (* DBusAuthShutdownFunction) (DBusAuth *auth);
98 
102 typedef struct
103 {
104  const char *mechanism;
115 
119 typedef enum {
120  DBUS_AUTH_COMMAND_AUTH,
121  DBUS_AUTH_COMMAND_CANCEL,
122  DBUS_AUTH_COMMAND_DATA,
123  DBUS_AUTH_COMMAND_BEGIN,
124  DBUS_AUTH_COMMAND_REJECTED,
125  DBUS_AUTH_COMMAND_OK,
126  DBUS_AUTH_COMMAND_ERROR,
127  DBUS_AUTH_COMMAND_UNKNOWN,
128  DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD,
129  DBUS_AUTH_COMMAND_AGREE_UNIX_FD
131 
138  DBusAuthCommand command,
139  const DBusString *args);
140 
144 typedef struct
145 {
146  const char *name;
149 
153 struct DBusAuth
154 {
155  int refcount;
156  const char *side;
178  int cookie_id;
181  char **allowed_mechs;
185  unsigned int needed_memory : 1;
188  unsigned int already_got_mechanisms : 1;
190  unsigned int buffer_outstanding : 1;
192  unsigned int unix_fd_possible : 1;
193  unsigned int unix_fd_negotiated : 1;
194 };
195 
199 typedef struct
200 {
208 
212 typedef struct
213 {
216  int failures;
222 
223 static void goto_state (DBusAuth *auth,
224  const DBusAuthStateData *new_state);
225 static dbus_bool_t send_auth (DBusAuth *auth,
226  const DBusAuthMechanismHandler *mech);
227 static dbus_bool_t send_data (DBusAuth *auth,
228  DBusString *data);
229 static dbus_bool_t send_rejected (DBusAuth *auth);
230 static dbus_bool_t send_error (DBusAuth *auth,
231  const char *message);
232 static dbus_bool_t send_ok (DBusAuth *auth);
233 static dbus_bool_t send_begin (DBusAuth *auth);
234 static dbus_bool_t send_cancel (DBusAuth *auth);
235 static dbus_bool_t send_negotiate_unix_fd (DBusAuth *auth);
236 static dbus_bool_t send_agree_unix_fd (DBusAuth *auth);
237 
242 static dbus_bool_t handle_server_state_waiting_for_auth (DBusAuth *auth,
243  DBusAuthCommand command,
244  const DBusString *args);
245 static dbus_bool_t handle_server_state_waiting_for_data (DBusAuth *auth,
246  DBusAuthCommand command,
247  const DBusString *args);
248 static dbus_bool_t handle_server_state_waiting_for_begin (DBusAuth *auth,
249  DBusAuthCommand command,
250  const DBusString *args);
251 
252 static const DBusAuthStateData server_state_waiting_for_auth = {
253  "WaitingForAuth", handle_server_state_waiting_for_auth
254 };
255 static const DBusAuthStateData server_state_waiting_for_data = {
256  "WaitingForData", handle_server_state_waiting_for_data
257 };
258 static const DBusAuthStateData server_state_waiting_for_begin = {
259  "WaitingForBegin", handle_server_state_waiting_for_begin
260 };
261 
266 static dbus_bool_t handle_client_state_waiting_for_data (DBusAuth *auth,
267  DBusAuthCommand command,
268  const DBusString *args);
269 static dbus_bool_t handle_client_state_waiting_for_ok (DBusAuth *auth,
270  DBusAuthCommand command,
271  const DBusString *args);
272 static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
273  DBusAuthCommand command,
274  const DBusString *args);
275 static dbus_bool_t handle_client_state_waiting_for_agree_unix_fd (DBusAuth *auth,
276  DBusAuthCommand command,
277  const DBusString *args);
278 
279 static const DBusAuthStateData client_state_need_send_auth = {
280  "NeedSendAuth", NULL
281 };
282 static const DBusAuthStateData client_state_waiting_for_data = {
283  "WaitingForData", handle_client_state_waiting_for_data
284 };
285 /* The WaitingForOK state doesn't appear to be used.
286  * See https://bugs.freedesktop.org/show_bug.cgi?id=97298 */
287 _DBUS_GNUC_UNUSED
288 static const DBusAuthStateData client_state_waiting_for_ok = {
289  "WaitingForOK", handle_client_state_waiting_for_ok
290 };
291 static const DBusAuthStateData client_state_waiting_for_reject = {
292  "WaitingForReject", handle_client_state_waiting_for_reject
293 };
294 static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
295  "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
296 };
297 
302 static const DBusAuthStateData common_state_authenticated = {
303  "Authenticated", NULL
304 };
305 
306 static const DBusAuthStateData common_state_need_disconnect = {
307  "NeedDisconnect", NULL
308 };
309 
310 static const char auth_side_client[] = "client";
311 static const char auth_side_server[] = "server";
316 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
317 
321 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
322 
326 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
327 
331 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
332 
338 #define DBUS_AUTH_NAME(auth) ((auth)->side)
339 
340 static DBusAuth*
341 _dbus_auth_new (int size)
342 {
343  DBusAuth *auth;
344 
345  auth = dbus_malloc0 (size);
346  if (auth == NULL)
347  return NULL;
348 
349  auth->refcount = 1;
350 
351  auth->keyring = NULL;
352  auth->cookie_id = -1;
353 
354  /* note that we don't use the max string length feature,
355  * because you can't use that feature if you're going to
356  * try to recover from out-of-memory (it creates
357  * what looks like unrecoverable inability to alloc
358  * more space in the string). But we do handle
359  * overlong buffers in _dbus_auth_do_work().
360  */
361 
362  if (!_dbus_string_init (&auth->incoming))
363  goto enomem_0;
364 
365  if (!_dbus_string_init (&auth->outgoing))
366  goto enomem_1;
367 
368  if (!_dbus_string_init (&auth->identity))
369  goto enomem_2;
370 
371  if (!_dbus_string_init (&auth->context))
372  goto enomem_3;
373 
374  if (!_dbus_string_init (&auth->challenge))
375  goto enomem_4;
376 
377  /* default context if none is specified */
378  if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
379  goto enomem_5;
380 
382  if (auth->credentials == NULL)
383  goto enomem_6;
384 
386  if (auth->authorized_identity == NULL)
387  goto enomem_7;
388 
390  if (auth->desired_identity == NULL)
391  goto enomem_8;
392 
393  return auth;
394 
395 #if 0
396  enomem_9:
398 #endif
399  enomem_8:
401  enomem_7:
403  enomem_6:
404  /* last alloc was an append to context, which is freed already below */ ;
405  enomem_5:
406  _dbus_string_free (&auth->challenge);
407  enomem_4:
408  _dbus_string_free (&auth->context);
409  enomem_3:
410  _dbus_string_free (&auth->identity);
411  enomem_2:
412  _dbus_string_free (&auth->outgoing);
413  enomem_1:
414  _dbus_string_free (&auth->incoming);
415  enomem_0:
416  dbus_free (auth);
417  return NULL;
418 }
419 
420 static void
421 shutdown_mech (DBusAuth *auth)
422 {
423  /* Cancel any auth */
425  _dbus_string_set_length (&auth->identity, 0);
426 
429 
430  if (auth->mech != NULL)
431  {
432  _dbus_verbose ("%s: Shutting down mechanism %s\n",
433  DBUS_AUTH_NAME (auth), auth->mech->mechanism);
434 
435  if (DBUS_AUTH_IS_CLIENT (auth))
436  (* auth->mech->client_shutdown_func) (auth);
437  else
438  (* auth->mech->server_shutdown_func) (auth);
439 
440  auth->mech = NULL;
441  }
442 }
443 
444 /*
445  * DBUS_COOKIE_SHA1 mechanism
446  */
447 
448 /* Returns TRUE but with an empty string hash if the
449  * cookie_id isn't known. As with all this code
450  * TRUE just means we had enough memory.
451  */
452 static dbus_bool_t
453 sha1_compute_hash (DBusAuth *auth,
454  int cookie_id,
455  const DBusString *server_challenge,
456  const DBusString *client_challenge,
457  DBusString *hash)
458 {
459  DBusString cookie;
460  DBusString to_hash;
461  dbus_bool_t retval;
462 
463  _dbus_assert (auth->keyring != NULL);
464 
465  retval = FALSE;
466 
467  if (!_dbus_string_init (&cookie))
468  return FALSE;
469 
470  if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
471  &cookie))
472  goto out_0;
473 
474  if (_dbus_string_get_length (&cookie) == 0)
475  {
476  retval = TRUE;
477  goto out_0;
478  }
479 
480  if (!_dbus_string_init (&to_hash))
481  goto out_0;
482 
483  if (!_dbus_string_copy (server_challenge, 0,
484  &to_hash, _dbus_string_get_length (&to_hash)))
485  goto out_1;
486 
487  if (!_dbus_string_append (&to_hash, ":"))
488  goto out_1;
489 
490  if (!_dbus_string_copy (client_challenge, 0,
491  &to_hash, _dbus_string_get_length (&to_hash)))
492  goto out_1;
493 
494  if (!_dbus_string_append (&to_hash, ":"))
495  goto out_1;
496 
497  if (!_dbus_string_copy (&cookie, 0,
498  &to_hash, _dbus_string_get_length (&to_hash)))
499  goto out_1;
500 
501  if (!_dbus_sha_compute (&to_hash, hash))
502  goto out_1;
503 
504  retval = TRUE;
505 
506  out_1:
507  _dbus_string_zero (&to_hash);
508  _dbus_string_free (&to_hash);
509  out_0:
510  _dbus_string_zero (&cookie);
511  _dbus_string_free (&cookie);
512  return retval;
513 }
514 
519 #define N_CHALLENGE_BYTES (128/8)
520 
521 static dbus_bool_t
522 sha1_handle_first_client_response (DBusAuth *auth,
523  const DBusString *data)
524 {
525  /* We haven't sent a challenge yet, we're expecting a desired
526  * username from the client.
527  */
528  DBusString tmp;
529  DBusString tmp2;
530  dbus_bool_t retval = FALSE;
531  DBusError error = DBUS_ERROR_INIT;
532 
534 
535  if (_dbus_string_get_length (data) > 0)
536  {
537  if (_dbus_string_get_length (&auth->identity) > 0)
538  {
539  /* Tried to send two auth identities, wtf */
540  _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
541  DBUS_AUTH_NAME (auth));
542  return send_rejected (auth);
543  }
544  else
545  {
546  /* this is our auth identity */
547  if (!_dbus_string_copy (data, 0, &auth->identity, 0))
548  return FALSE;
549  }
550  }
551 
553  {
554  _dbus_verbose ("%s: Did not get a valid username from client\n",
555  DBUS_AUTH_NAME (auth));
556  return send_rejected (auth);
557  }
558 
559  if (!_dbus_string_init (&tmp))
560  return FALSE;
561 
562  if (!_dbus_string_init (&tmp2))
563  {
564  _dbus_string_free (&tmp);
565  return FALSE;
566  }
567 
568  /* we cache the keyring for speed, so here we drop it if it's the
569  * wrong one. FIXME caching the keyring here is useless since we use
570  * a different DBusAuth for every connection.
571  */
572  if (auth->keyring &&
574  auth->desired_identity))
575  {
577  auth->keyring = NULL;
578  }
579 
580  if (auth->keyring == NULL)
581  {
583  &auth->context,
584  &error);
585 
586  if (auth->keyring == NULL)
587  {
588  if (dbus_error_has_name (&error,
590  {
591  dbus_error_free (&error);
592  goto out;
593  }
594  else
595  {
596  _DBUS_ASSERT_ERROR_IS_SET (&error);
597  _dbus_verbose ("%s: Error loading keyring: %s\n",
598  DBUS_AUTH_NAME (auth), error.message);
599  if (send_rejected (auth))
600  retval = TRUE; /* retval is only about mem */
601  dbus_error_free (&error);
602  goto out;
603  }
604  }
605  else
606  {
607  _dbus_assert (!dbus_error_is_set (&error));
608  }
609  }
610 
611  _dbus_assert (auth->keyring != NULL);
612 
613  auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
614  if (auth->cookie_id < 0)
615  {
616  _DBUS_ASSERT_ERROR_IS_SET (&error);
617  _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
618  DBUS_AUTH_NAME (auth), error.message);
619  if (send_rejected (auth))
620  retval = TRUE;
621  dbus_error_free (&error);
622  goto out;
623  }
624  else
625  {
626  _dbus_assert (!dbus_error_is_set (&error));
627  }
628 
629  if (!_dbus_string_copy (&auth->context, 0,
630  &tmp2, _dbus_string_get_length (&tmp2)))
631  goto out;
632 
633  if (!_dbus_string_append (&tmp2, " "))
634  goto out;
635 
636  if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
637  goto out;
638 
639  if (!_dbus_string_append (&tmp2, " "))
640  goto out;
641 
642  if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES, &error))
643  {
645  {
646  dbus_error_free (&error);
647  goto out;
648  }
649  else
650  {
651  _DBUS_ASSERT_ERROR_IS_SET (&error);
652  _dbus_verbose ("%s: Error generating challenge: %s\n",
653  DBUS_AUTH_NAME (auth), error.message);
654  if (send_rejected (auth))
655  retval = TRUE; /* retval is only about mem */
656 
657  dbus_error_free (&error);
658  goto out;
659  }
660  }
661 
663  if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
664  goto out;
665 
666  if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
667  _dbus_string_get_length (&tmp2)))
668  goto out;
669 
670  if (!send_data (auth, &tmp2))
671  goto out;
672 
673  goto_state (auth, &server_state_waiting_for_data);
674  retval = TRUE;
675 
676  out:
677  _dbus_string_zero (&tmp);
678  _dbus_string_free (&tmp);
679  _dbus_string_zero (&tmp2);
680  _dbus_string_free (&tmp2);
681 
682  return retval;
683 }
684 
685 static dbus_bool_t
686 sha1_handle_second_client_response (DBusAuth *auth,
687  const DBusString *data)
688 {
689  /* We are expecting a response which is the hex-encoded client
690  * challenge, space, then SHA-1 hash of the concatenation of our
691  * challenge, ":", client challenge, ":", secret key, all
692  * hex-encoded.
693  */
694  int i;
695  DBusString client_challenge;
696  DBusString client_hash;
697  dbus_bool_t retval;
698  DBusString correct_hash;
699 
700  retval = FALSE;
701 
702  if (!_dbus_string_find_blank (data, 0, &i))
703  {
704  _dbus_verbose ("%s: no space separator in client response\n",
705  DBUS_AUTH_NAME (auth));
706  return send_rejected (auth);
707  }
708 
709  if (!_dbus_string_init (&client_challenge))
710  goto out_0;
711 
712  if (!_dbus_string_init (&client_hash))
713  goto out_1;
714 
715  if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
716  0))
717  goto out_2;
718 
719  _dbus_string_skip_blank (data, i, &i);
720 
721  if (!_dbus_string_copy_len (data, i,
722  _dbus_string_get_length (data) - i,
723  &client_hash,
724  0))
725  goto out_2;
726 
727  if (_dbus_string_get_length (&client_challenge) == 0 ||
728  _dbus_string_get_length (&client_hash) == 0)
729  {
730  _dbus_verbose ("%s: zero-length client challenge or hash\n",
731  DBUS_AUTH_NAME (auth));
732  if (send_rejected (auth))
733  retval = TRUE;
734  goto out_2;
735  }
736 
737  if (!_dbus_string_init (&correct_hash))
738  goto out_2;
739 
740  if (!sha1_compute_hash (auth, auth->cookie_id,
741  &auth->challenge,
742  &client_challenge,
743  &correct_hash))
744  goto out_3;
745 
746  /* if cookie_id was invalid, then we get an empty hash */
747  if (_dbus_string_get_length (&correct_hash) == 0)
748  {
749  if (send_rejected (auth))
750  retval = TRUE;
751  goto out_3;
752  }
753 
754  if (!_dbus_string_equal (&client_hash, &correct_hash))
755  {
756  if (send_rejected (auth))
757  retval = TRUE;
758  goto out_3;
759  }
760 
762  auth->desired_identity))
763  goto out_3;
764 
765  /* Copy process ID from the socket credentials if it's there
766  */
768  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
769  auth->credentials))
770  goto out_3;
771 
772  if (!send_ok (auth))
773  goto out_3;
774 
775  _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
776  DBUS_AUTH_NAME (auth));
777 
778  retval = TRUE;
779 
780  out_3:
781  _dbus_string_zero (&correct_hash);
782  _dbus_string_free (&correct_hash);
783  out_2:
784  _dbus_string_zero (&client_hash);
785  _dbus_string_free (&client_hash);
786  out_1:
787  _dbus_string_free (&client_challenge);
788  out_0:
789  return retval;
790 }
791 
792 static dbus_bool_t
793 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
794  const DBusString *data)
795 {
796  if (auth->cookie_id < 0)
797  return sha1_handle_first_client_response (auth, data);
798  else
799  return sha1_handle_second_client_response (auth, data);
800 }
801 
802 static void
803 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
804 {
805  auth->cookie_id = -1;
807 }
808 
809 static dbus_bool_t
810 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
811  DBusString *response)
812 {
813  DBusString username;
814  dbus_bool_t retval;
815 
816  retval = FALSE;
817 
818  if (!_dbus_string_init (&username))
819  return FALSE;
820 
822  goto out_0;
823 
824  if (!_dbus_string_hex_encode (&username, 0,
825  response,
826  _dbus_string_get_length (response)))
827  goto out_0;
828 
829  retval = TRUE;
830 
831  out_0:
832  _dbus_string_free (&username);
833 
834  return retval;
835 }
836 
837 static dbus_bool_t
838 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
839  const DBusString *data)
840 {
841  /* The data we get from the server should be the cookie context
842  * name, the cookie ID, and the server challenge, separated by
843  * spaces. We send back our challenge string and the correct hash.
844  */
845  dbus_bool_t retval = FALSE;
846  DBusString context;
847  DBusString cookie_id_str;
848  DBusString server_challenge;
849  DBusString client_challenge;
850  DBusString correct_hash;
851  DBusString tmp;
852  int i, j;
853  long val;
854  DBusError error = DBUS_ERROR_INIT;
855 
856  if (!_dbus_string_find_blank (data, 0, &i))
857  {
858  if (send_error (auth,
859  "Server did not send context/ID/challenge properly"))
860  retval = TRUE;
861  goto out_0;
862  }
863 
864  if (!_dbus_string_init (&context))
865  goto out_0;
866 
867  if (!_dbus_string_copy_len (data, 0, i,
868  &context, 0))
869  goto out_1;
870 
871  _dbus_string_skip_blank (data, i, &i);
872  if (!_dbus_string_find_blank (data, i, &j))
873  {
874  if (send_error (auth,
875  "Server did not send context/ID/challenge properly"))
876  retval = TRUE;
877  goto out_1;
878  }
879 
880  if (!_dbus_string_init (&cookie_id_str))
881  goto out_1;
882 
883  if (!_dbus_string_copy_len (data, i, j - i,
884  &cookie_id_str, 0))
885  goto out_2;
886 
887  if (!_dbus_string_init (&server_challenge))
888  goto out_2;
889 
890  i = j;
891  _dbus_string_skip_blank (data, i, &i);
892  j = _dbus_string_get_length (data);
893 
894  if (!_dbus_string_copy_len (data, i, j - i,
895  &server_challenge, 0))
896  goto out_3;
897 
898  if (!_dbus_keyring_validate_context (&context))
899  {
900  if (send_error (auth, "Server sent invalid cookie context"))
901  retval = TRUE;
902  goto out_3;
903  }
904 
905  if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
906  {
907  if (send_error (auth, "Could not parse cookie ID as an integer"))
908  retval = TRUE;
909  goto out_3;
910  }
911 
912  if (_dbus_string_get_length (&server_challenge) == 0)
913  {
914  if (send_error (auth, "Empty server challenge string"))
915  retval = TRUE;
916  goto out_3;
917  }
918 
919  if (auth->keyring == NULL)
920  {
922  &context,
923  &error);
924 
925  if (auth->keyring == NULL)
926  {
927  if (dbus_error_has_name (&error,
929  {
930  dbus_error_free (&error);
931  goto out_3;
932  }
933  else
934  {
935  _DBUS_ASSERT_ERROR_IS_SET (&error);
936 
937  _dbus_verbose ("%s: Error loading keyring: %s\n",
938  DBUS_AUTH_NAME (auth), error.message);
939 
940  if (send_error (auth, "Could not load cookie file"))
941  retval = TRUE; /* retval is only about mem */
942 
943  dbus_error_free (&error);
944  goto out_3;
945  }
946  }
947  else
948  {
949  _dbus_assert (!dbus_error_is_set (&error));
950  }
951  }
952 
953  _dbus_assert (auth->keyring != NULL);
954 
955  if (!_dbus_string_init (&tmp))
956  goto out_3;
957 
958  if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES, &error))
959  {
961  {
962  dbus_error_free (&error);
963  goto out_4;
964  }
965  else
966  {
967  _DBUS_ASSERT_ERROR_IS_SET (&error);
968 
969  _dbus_verbose ("%s: Failed to generate challenge: %s\n",
970  DBUS_AUTH_NAME (auth), error.message);
971 
972  if (send_error (auth, "Failed to generate challenge"))
973  retval = TRUE; /* retval is only about mem */
974 
975  dbus_error_free (&error);
976  goto out_4;
977  }
978  }
979 
980  if (!_dbus_string_init (&client_challenge))
981  goto out_4;
982 
983  if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
984  goto out_5;
985 
986  if (!_dbus_string_init (&correct_hash))
987  goto out_5;
988 
989  if (!sha1_compute_hash (auth, val,
990  &server_challenge,
991  &client_challenge,
992  &correct_hash))
993  goto out_6;
994 
995  if (_dbus_string_get_length (&correct_hash) == 0)
996  {
997  /* couldn't find the cookie ID or something */
998  if (send_error (auth, "Don't have the requested cookie ID"))
999  retval = TRUE;
1000  goto out_6;
1001  }
1002 
1003  _dbus_string_set_length (&tmp, 0);
1004 
1005  if (!_dbus_string_copy (&client_challenge, 0, &tmp,
1006  _dbus_string_get_length (&tmp)))
1007  goto out_6;
1008 
1009  if (!_dbus_string_append (&tmp, " "))
1010  goto out_6;
1011 
1012  if (!_dbus_string_copy (&correct_hash, 0, &tmp,
1013  _dbus_string_get_length (&tmp)))
1014  goto out_6;
1015 
1016  if (!send_data (auth, &tmp))
1017  goto out_6;
1018 
1019  retval = TRUE;
1020 
1021  out_6:
1022  _dbus_string_zero (&correct_hash);
1023  _dbus_string_free (&correct_hash);
1024  out_5:
1025  _dbus_string_free (&client_challenge);
1026  out_4:
1027  _dbus_string_zero (&tmp);
1028  _dbus_string_free (&tmp);
1029  out_3:
1030  _dbus_string_free (&server_challenge);
1031  out_2:
1032  _dbus_string_free (&cookie_id_str);
1033  out_1:
1034  _dbus_string_free (&context);
1035  out_0:
1036  return retval;
1037 }
1038 
1039 static void
1040 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1041 {
1042  auth->cookie_id = -1;
1043  _dbus_string_set_length (&auth->challenge, 0);
1044 }
1045 
1046 /*
1047  * EXTERNAL mechanism
1048  */
1049 
1050 static dbus_bool_t
1051 handle_server_data_external_mech (DBusAuth *auth,
1052  const DBusString *data)
1053 {
1055  {
1056  _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1057  DBUS_AUTH_NAME (auth));
1058  return send_rejected (auth);
1059  }
1060 
1061  if (_dbus_string_get_length (data) > 0)
1062  {
1063  if (_dbus_string_get_length (&auth->identity) > 0)
1064  {
1065  /* Tried to send two auth identities, wtf */
1066  _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1067  DBUS_AUTH_NAME (auth));
1068  return send_rejected (auth);
1069  }
1070  else
1071  {
1072  /* this is our auth identity */
1073  if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1074  return FALSE;
1075  }
1076  }
1077 
1078  /* Poke client for an auth identity, if none given */
1079  if (_dbus_string_get_length (&auth->identity) == 0 &&
1081  {
1082  if (send_data (auth, NULL))
1083  {
1084  _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1085  DBUS_AUTH_NAME (auth));
1087  goto_state (auth, &server_state_waiting_for_data);
1088  return TRUE;
1089  }
1090  else
1091  return FALSE;
1092  }
1093 
1095 
1096  /* If auth->identity is still empty here, then client
1097  * responded with an empty string after we poked it for
1098  * an initial response. This means to try to auth the
1099  * identity provided in the credentials.
1100  */
1101  if (_dbus_string_get_length (&auth->identity) == 0)
1102  {
1104  auth->credentials))
1105  {
1106  return FALSE; /* OOM */
1107  }
1108  }
1109  else
1110  {
1112  &auth->identity))
1113  {
1114  _dbus_verbose ("%s: could not get credentials from uid string\n",
1115  DBUS_AUTH_NAME (auth));
1116  return send_rejected (auth);
1117  }
1118  }
1119 
1121  {
1122  _dbus_verbose ("%s: desired user %s is no good\n",
1123  DBUS_AUTH_NAME (auth),
1124  _dbus_string_get_const_data (&auth->identity));
1125  return send_rejected (auth);
1126  }
1127 
1129  auth->desired_identity))
1130  {
1131  /* client has authenticated */
1133  auth->desired_identity))
1134  return FALSE;
1135 
1136  /* also copy misc process info from the socket credentials
1137  */
1139  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1140  auth->credentials))
1141  return FALSE;
1142 
1144  DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1145  auth->credentials))
1146  return FALSE;
1147 
1149  DBUS_CREDENTIAL_LINUX_SECURITY_LABEL,
1150  auth->credentials))
1151  return FALSE;
1152 
1153  if (!send_ok (auth))
1154  return FALSE;
1155 
1156  _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1157  DBUS_AUTH_NAME (auth));
1158 
1159  return TRUE;
1160  }
1161  else
1162  {
1163  _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1164  DBUS_AUTH_NAME (auth));
1165  return send_rejected (auth);
1166  }
1167 }
1168 
1169 static void
1170 handle_server_shutdown_external_mech (DBusAuth *auth)
1171 {
1172 
1173 }
1174 
1175 static dbus_bool_t
1176 handle_client_initial_response_external_mech (DBusAuth *auth,
1177  DBusString *response)
1178 {
1179  /* We always append our UID as an initial response, so the server
1180  * doesn't have to send back an empty challenge to check whether we
1181  * want to specify an identity. i.e. this avoids a round trip that
1182  * the spec for the EXTERNAL mechanism otherwise requires.
1183  */
1184  DBusString plaintext;
1185 
1186  if (!_dbus_string_init (&plaintext))
1187  return FALSE;
1188 
1189  if (!_dbus_append_user_from_current_process (&plaintext))
1190  goto failed;
1191 
1192  if (!_dbus_string_hex_encode (&plaintext, 0,
1193  response,
1194  _dbus_string_get_length (response)))
1195  goto failed;
1196 
1197  _dbus_string_free (&plaintext);
1198 
1199  return TRUE;
1200 
1201  failed:
1202  _dbus_string_free (&plaintext);
1203  return FALSE;
1204 }
1205 
1206 static dbus_bool_t
1207 handle_client_data_external_mech (DBusAuth *auth,
1208  const DBusString *data)
1209 {
1210 
1211  return TRUE;
1212 }
1213 
1214 static void
1215 handle_client_shutdown_external_mech (DBusAuth *auth)
1216 {
1217 
1218 }
1219 
1220 /*
1221  * ANONYMOUS mechanism
1222  */
1223 
1224 static dbus_bool_t
1225 handle_server_data_anonymous_mech (DBusAuth *auth,
1226  const DBusString *data)
1227 {
1228  if (_dbus_string_get_length (data) > 0)
1229  {
1230  /* Client is allowed to send "trace" data, the only defined
1231  * meaning is that if it contains '@' it is an email address,
1232  * and otherwise it is anything else, and it's supposed to be
1233  * UTF-8
1234  */
1235  if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1236  {
1237  _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1238  DBUS_AUTH_NAME (auth));
1239  return send_rejected (auth);
1240  }
1241 
1242  _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1243  DBUS_AUTH_NAME (auth),
1244  _dbus_string_get_const_data (data));
1245  }
1246 
1247  /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1249 
1250  /* Copy process ID from the socket credentials
1251  */
1253  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1254  auth->credentials))
1255  return FALSE;
1256 
1257  /* Anonymous is always allowed */
1258  if (!send_ok (auth))
1259  return FALSE;
1260 
1261  _dbus_verbose ("%s: authenticated client as anonymous\n",
1262  DBUS_AUTH_NAME (auth));
1263 
1264  return TRUE;
1265 }
1266 
1267 static void
1268 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1269 {
1270 
1271 }
1272 
1273 static dbus_bool_t
1274 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1275  DBusString *response)
1276 {
1277  /* Our initial response is a "trace" string which must be valid UTF-8
1278  * and must be an email address if it contains '@'.
1279  * We just send the dbus implementation info, like a user-agent or
1280  * something, because... why not. There's nothing guaranteed here
1281  * though, we could change it later.
1282  */
1283  DBusString plaintext;
1284 
1285  if (!_dbus_string_init (&plaintext))
1286  return FALSE;
1287 
1288  if (!_dbus_string_append (&plaintext,
1289  "libdbus " DBUS_VERSION_STRING))
1290  goto failed;
1291 
1292  if (!_dbus_string_hex_encode (&plaintext, 0,
1293  response,
1294  _dbus_string_get_length (response)))
1295  goto failed;
1296 
1297  _dbus_string_free (&plaintext);
1298 
1299  return TRUE;
1300 
1301  failed:
1302  _dbus_string_free (&plaintext);
1303  return FALSE;
1304 }
1305 
1306 static dbus_bool_t
1307 handle_client_data_anonymous_mech (DBusAuth *auth,
1308  const DBusString *data)
1309 {
1310 
1311  return TRUE;
1312 }
1313 
1314 static void
1315 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1316 {
1317 
1318 }
1319 
1320 /* Put mechanisms here in order of preference.
1321  * Right now we have:
1322  *
1323  * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1324  * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1325  * - ANONYMOUS checks nothing but doesn't auth the person as a user
1326  *
1327  * We might ideally add a mechanism to chain to Cyrus SASL so we can
1328  * use its mechanisms as well.
1329  *
1330  */
1331 static const DBusAuthMechanismHandler
1332 all_mechanisms[] = {
1333  { "EXTERNAL",
1334  handle_server_data_external_mech,
1335  NULL, NULL,
1336  handle_server_shutdown_external_mech,
1337  handle_client_initial_response_external_mech,
1338  handle_client_data_external_mech,
1339  NULL, NULL,
1340  handle_client_shutdown_external_mech },
1341  { "DBUS_COOKIE_SHA1",
1342  handle_server_data_cookie_sha1_mech,
1343  NULL, NULL,
1344  handle_server_shutdown_cookie_sha1_mech,
1345  handle_client_initial_response_cookie_sha1_mech,
1346  handle_client_data_cookie_sha1_mech,
1347  NULL, NULL,
1348  handle_client_shutdown_cookie_sha1_mech },
1349  { "ANONYMOUS",
1350  handle_server_data_anonymous_mech,
1351  NULL, NULL,
1352  handle_server_shutdown_anonymous_mech,
1353  handle_client_initial_response_anonymous_mech,
1354  handle_client_data_anonymous_mech,
1355  NULL, NULL,
1356  handle_client_shutdown_anonymous_mech },
1357  { NULL, NULL }
1358 };
1359 
1360 static const DBusAuthMechanismHandler*
1361 find_mech (const DBusString *name,
1362  char **allowed_mechs)
1363 {
1364  int i;
1365 
1366  if (allowed_mechs != NULL &&
1367  !_dbus_string_array_contains ((const char**) allowed_mechs,
1368  _dbus_string_get_const_data (name)))
1369  return NULL;
1370 
1371  i = 0;
1372  while (all_mechanisms[i].mechanism != NULL)
1373  {
1374  if (_dbus_string_equal_c_str (name,
1375  all_mechanisms[i].mechanism))
1376 
1377  return &all_mechanisms[i];
1378 
1379  ++i;
1380  }
1381 
1382  return NULL;
1383 }
1384 
1385 static dbus_bool_t
1386 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1387 {
1388  DBusString auth_command;
1389 
1390  if (!_dbus_string_init (&auth_command))
1391  return FALSE;
1392 
1393  if (!_dbus_string_append (&auth_command,
1394  "AUTH "))
1395  {
1396  _dbus_string_free (&auth_command);
1397  return FALSE;
1398  }
1399 
1400  if (!_dbus_string_append (&auth_command,
1401  mech->mechanism))
1402  {
1403  _dbus_string_free (&auth_command);
1404  return FALSE;
1405  }
1406 
1407  if (mech->client_initial_response_func != NULL)
1408  {
1409  if (!_dbus_string_append (&auth_command, " "))
1410  {
1411  _dbus_string_free (&auth_command);
1412  return FALSE;
1413  }
1414 
1415  if (!(* mech->client_initial_response_func) (auth, &auth_command))
1416  {
1417  _dbus_string_free (&auth_command);
1418  return FALSE;
1419  }
1420  }
1421 
1422  if (!_dbus_string_append (&auth_command,
1423  "\r\n"))
1424  {
1425  _dbus_string_free (&auth_command);
1426  return FALSE;
1427  }
1428 
1429  if (!_dbus_string_copy (&auth_command, 0,
1430  &auth->outgoing,
1431  _dbus_string_get_length (&auth->outgoing)))
1432  {
1433  _dbus_string_free (&auth_command);
1434  return FALSE;
1435  }
1436 
1437  _dbus_string_free (&auth_command);
1438  shutdown_mech (auth);
1439  auth->mech = mech;
1440  goto_state (auth, &client_state_waiting_for_data);
1441 
1442  return TRUE;
1443 }
1444 
1445 static dbus_bool_t
1446 send_data (DBusAuth *auth, DBusString *data)
1447 {
1448  int old_len;
1449 
1450  if (data == NULL || _dbus_string_get_length (data) == 0)
1451  return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1452  else
1453  {
1454  old_len = _dbus_string_get_length (&auth->outgoing);
1455  if (!_dbus_string_append (&auth->outgoing, "DATA "))
1456  goto out;
1457 
1458  if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1459  _dbus_string_get_length (&auth->outgoing)))
1460  goto out;
1461 
1462  if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1463  goto out;
1464 
1465  return TRUE;
1466 
1467  out:
1468  _dbus_string_set_length (&auth->outgoing, old_len);
1469 
1470  return FALSE;
1471  }
1472 }
1473 
1474 static dbus_bool_t
1475 send_rejected (DBusAuth *auth)
1476 {
1477  DBusString command;
1478  DBusAuthServer *server_auth;
1479  int i;
1480 
1481  if (!_dbus_string_init (&command))
1482  return FALSE;
1483 
1484  if (!_dbus_string_append (&command,
1485  "REJECTED"))
1486  goto nomem;
1487 
1488  i = 0;
1489  while (all_mechanisms[i].mechanism != NULL)
1490  {
1491  if (!_dbus_string_append (&command,
1492  " "))
1493  goto nomem;
1494 
1495  if (!_dbus_string_append (&command,
1496  all_mechanisms[i].mechanism))
1497  goto nomem;
1498 
1499  ++i;
1500  }
1501 
1502  if (!_dbus_string_append (&command, "\r\n"))
1503  goto nomem;
1504 
1505  if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1506  _dbus_string_get_length (&auth->outgoing)))
1507  goto nomem;
1508 
1509  shutdown_mech (auth);
1510 
1512  server_auth = DBUS_AUTH_SERVER (auth);
1513  server_auth->failures += 1;
1514 
1515  if (server_auth->failures >= server_auth->max_failures)
1516  goto_state (auth, &common_state_need_disconnect);
1517  else
1518  goto_state (auth, &server_state_waiting_for_auth);
1519 
1520  _dbus_string_free (&command);
1521 
1522  return TRUE;
1523 
1524  nomem:
1525  _dbus_string_free (&command);
1526  return FALSE;
1527 }
1528 
1529 static dbus_bool_t
1530 send_error (DBusAuth *auth, const char *message)
1531 {
1532  return _dbus_string_append_printf (&auth->outgoing,
1533  "ERROR \"%s\"\r\n", message);
1534 }
1535 
1536 static dbus_bool_t
1537 send_ok (DBusAuth *auth)
1538 {
1539  int orig_len;
1540 
1541  orig_len = _dbus_string_get_length (&auth->outgoing);
1542 
1543  if (_dbus_string_append (&auth->outgoing, "OK ") &&
1544  _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1545  0,
1546  &auth->outgoing,
1547  _dbus_string_get_length (&auth->outgoing)) &&
1548  _dbus_string_append (&auth->outgoing, "\r\n"))
1549  {
1550  goto_state (auth, &server_state_waiting_for_begin);
1551  return TRUE;
1552  }
1553  else
1554  {
1555  _dbus_string_set_length (&auth->outgoing, orig_len);
1556  return FALSE;
1557  }
1558 }
1559 
1560 static dbus_bool_t
1561 send_begin (DBusAuth *auth)
1562 {
1563 
1564  if (!_dbus_string_append (&auth->outgoing,
1565  "BEGIN\r\n"))
1566  return FALSE;
1567 
1568  goto_state (auth, &common_state_authenticated);
1569  return TRUE;
1570 }
1571 
1572 static dbus_bool_t
1573 process_ok(DBusAuth *auth,
1574  const DBusString *args_from_ok) {
1575 
1576  int end_of_hex;
1577 
1578  /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1579  _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1580 
1581  /* We decode the hex string to binary, using guid_from_server as scratch... */
1582 
1583  end_of_hex = 0;
1584  if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1585  & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1586  return FALSE;
1587 
1588  /* now clear out the scratch */
1589  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1590 
1591  if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1592  end_of_hex == 0)
1593  {
1594  _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1595  end_of_hex, _dbus_string_get_length (args_from_ok));
1596  goto_state (auth, &common_state_need_disconnect);
1597  return TRUE;
1598  }
1599 
1600  if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1601  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1602  return FALSE;
1603  }
1604 
1605  _dbus_verbose ("Got GUID '%s' from the server\n",
1606  _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1607 
1608  if (auth->unix_fd_possible)
1609  {
1610  if (!send_negotiate_unix_fd (auth))
1611  {
1612  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1613  return FALSE;
1614  }
1615 
1616  return TRUE;
1617  }
1618 
1619  _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1620  return send_begin (auth);
1621 }
1622 
1623 static dbus_bool_t
1624 send_cancel (DBusAuth *auth)
1625 {
1626  if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1627  {
1628  goto_state (auth, &client_state_waiting_for_reject);
1629  return TRUE;
1630  }
1631  else
1632  return FALSE;
1633 }
1634 
1635 static dbus_bool_t
1636 process_data (DBusAuth *auth,
1637  const DBusString *args,
1638  DBusAuthDataFunction data_func)
1639 {
1640  int end;
1641  DBusString decoded;
1642 
1643  if (!_dbus_string_init (&decoded))
1644  return FALSE;
1645 
1646  if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1647  {
1648  _dbus_string_free (&decoded);
1649  return FALSE;
1650  }
1651 
1652  if (_dbus_string_get_length (args) != end)
1653  {
1654  _dbus_string_free (&decoded);
1655  if (!send_error (auth, "Invalid hex encoding"))
1656  return FALSE;
1657 
1658  return TRUE;
1659  }
1660 
1661 #ifdef DBUS_ENABLE_VERBOSE_MODE
1662  if (_dbus_string_validate_ascii (&decoded, 0,
1663  _dbus_string_get_length (&decoded)))
1664  _dbus_verbose ("%s: data: '%s'\n",
1665  DBUS_AUTH_NAME (auth),
1666  _dbus_string_get_const_data (&decoded));
1667 #endif
1668 
1669  if (!(* data_func) (auth, &decoded))
1670  {
1671  _dbus_string_free (&decoded);
1672  return FALSE;
1673  }
1674 
1675  _dbus_string_free (&decoded);
1676  return TRUE;
1677 }
1678 
1679 static dbus_bool_t
1680 send_negotiate_unix_fd (DBusAuth *auth)
1681 {
1682  if (!_dbus_string_append (&auth->outgoing,
1683  "NEGOTIATE_UNIX_FD\r\n"))
1684  return FALSE;
1685 
1686  goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1687  return TRUE;
1688 }
1689 
1690 static dbus_bool_t
1691 send_agree_unix_fd (DBusAuth *auth)
1692 {
1694 
1695  auth->unix_fd_negotiated = TRUE;
1696  _dbus_verbose("Agreed to UNIX FD passing\n");
1697 
1698  if (!_dbus_string_append (&auth->outgoing,
1699  "AGREE_UNIX_FD\r\n"))
1700  return FALSE;
1701 
1702  goto_state (auth, &server_state_waiting_for_begin);
1703  return TRUE;
1704 }
1705 
1706 static dbus_bool_t
1707 handle_auth (DBusAuth *auth, const DBusString *args)
1708 {
1709  if (_dbus_string_get_length (args) == 0)
1710  {
1711  /* No args to the auth, send mechanisms */
1712  if (!send_rejected (auth))
1713  return FALSE;
1714 
1715  return TRUE;
1716  }
1717  else
1718  {
1719  int i;
1720  DBusString mech;
1721  DBusString hex_response;
1722 
1723  _dbus_string_find_blank (args, 0, &i);
1724 
1725  if (!_dbus_string_init (&mech))
1726  return FALSE;
1727 
1728  if (!_dbus_string_init (&hex_response))
1729  {
1730  _dbus_string_free (&mech);
1731  return FALSE;
1732  }
1733 
1734  if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1735  goto failed;
1736 
1737  _dbus_string_skip_blank (args, i, &i);
1738  if (!_dbus_string_copy (args, i, &hex_response, 0))
1739  goto failed;
1740 
1741  auth->mech = find_mech (&mech, auth->allowed_mechs);
1742  if (auth->mech != NULL)
1743  {
1744  _dbus_verbose ("%s: Trying mechanism %s\n",
1745  DBUS_AUTH_NAME (auth),
1746  auth->mech->mechanism);
1747 
1748  if (!process_data (auth, &hex_response,
1749  auth->mech->server_data_func))
1750  goto failed;
1751  }
1752  else
1753  {
1754  /* Unsupported mechanism */
1755  _dbus_verbose ("%s: Unsupported mechanism %s\n",
1756  DBUS_AUTH_NAME (auth),
1757  _dbus_string_get_const_data (&mech));
1758 
1759  if (!send_rejected (auth))
1760  goto failed;
1761  }
1762 
1763  _dbus_string_free (&mech);
1764  _dbus_string_free (&hex_response);
1765 
1766  return TRUE;
1767 
1768  failed:
1769  auth->mech = NULL;
1770  _dbus_string_free (&mech);
1771  _dbus_string_free (&hex_response);
1772  return FALSE;
1773  }
1774 }
1775 
1776 static dbus_bool_t
1777 handle_server_state_waiting_for_auth (DBusAuth *auth,
1778  DBusAuthCommand command,
1779  const DBusString *args)
1780 {
1781  switch (command)
1782  {
1783  case DBUS_AUTH_COMMAND_AUTH:
1784  return handle_auth (auth, args);
1785 
1786  case DBUS_AUTH_COMMAND_CANCEL:
1787  case DBUS_AUTH_COMMAND_DATA:
1788  return send_error (auth, "Not currently in an auth conversation");
1789 
1790  case DBUS_AUTH_COMMAND_BEGIN:
1791  goto_state (auth, &common_state_need_disconnect);
1792  return TRUE;
1793 
1794  case DBUS_AUTH_COMMAND_ERROR:
1795  return send_rejected (auth);
1796 
1797  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1798  return send_error (auth, "Need to authenticate first");
1799 
1800  case DBUS_AUTH_COMMAND_REJECTED:
1801  case DBUS_AUTH_COMMAND_OK:
1802  case DBUS_AUTH_COMMAND_UNKNOWN:
1803  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1804  default:
1805  return send_error (auth, "Unknown command");
1806  }
1807 }
1808 
1809 static dbus_bool_t
1810 handle_server_state_waiting_for_data (DBusAuth *auth,
1811  DBusAuthCommand command,
1812  const DBusString *args)
1813 {
1814  switch (command)
1815  {
1816  case DBUS_AUTH_COMMAND_AUTH:
1817  return send_error (auth, "Sent AUTH while another AUTH in progress");
1818 
1819  case DBUS_AUTH_COMMAND_CANCEL:
1820  case DBUS_AUTH_COMMAND_ERROR:
1821  return send_rejected (auth);
1822 
1823  case DBUS_AUTH_COMMAND_DATA:
1824  return process_data (auth, args, auth->mech->server_data_func);
1825 
1826  case DBUS_AUTH_COMMAND_BEGIN:
1827  goto_state (auth, &common_state_need_disconnect);
1828  return TRUE;
1829 
1830  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1831  return send_error (auth, "Need to authenticate first");
1832 
1833  case DBUS_AUTH_COMMAND_REJECTED:
1834  case DBUS_AUTH_COMMAND_OK:
1835  case DBUS_AUTH_COMMAND_UNKNOWN:
1836  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1837  default:
1838  return send_error (auth, "Unknown command");
1839  }
1840 }
1841 
1842 static dbus_bool_t
1843 handle_server_state_waiting_for_begin (DBusAuth *auth,
1844  DBusAuthCommand command,
1845  const DBusString *args)
1846 {
1847  switch (command)
1848  {
1849  case DBUS_AUTH_COMMAND_AUTH:
1850  return send_error (auth, "Sent AUTH while expecting BEGIN");
1851 
1852  case DBUS_AUTH_COMMAND_DATA:
1853  return send_error (auth, "Sent DATA while expecting BEGIN");
1854 
1855  case DBUS_AUTH_COMMAND_BEGIN:
1856  goto_state (auth, &common_state_authenticated);
1857  return TRUE;
1858 
1859  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1860  if (auth->unix_fd_possible)
1861  return send_agree_unix_fd(auth);
1862  else
1863  return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1864 
1865  case DBUS_AUTH_COMMAND_REJECTED:
1866  case DBUS_AUTH_COMMAND_OK:
1867  case DBUS_AUTH_COMMAND_UNKNOWN:
1868  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1869  default:
1870  return send_error (auth, "Unknown command");
1871 
1872  case DBUS_AUTH_COMMAND_CANCEL:
1873  case DBUS_AUTH_COMMAND_ERROR:
1874  return send_rejected (auth);
1875  }
1876 }
1877 
1878 /* return FALSE if no memory, TRUE if all OK */
1879 static dbus_bool_t
1880 get_word (const DBusString *str,
1881  int *start,
1882  DBusString *word)
1883 {
1884  int i;
1885 
1886  _dbus_string_skip_blank (str, *start, start);
1887  _dbus_string_find_blank (str, *start, &i);
1888 
1889  if (i > *start)
1890  {
1891  if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1892  return FALSE;
1893 
1894  *start = i;
1895  }
1896 
1897  return TRUE;
1898 }
1899 
1900 static dbus_bool_t
1901 record_mechanisms (DBusAuth *auth,
1902  const DBusString *args)
1903 {
1904  int next;
1905  int len;
1906 
1907  if (auth->already_got_mechanisms)
1908  return TRUE;
1909 
1910  len = _dbus_string_get_length (args);
1911 
1912  next = 0;
1913  while (next < len)
1914  {
1915  DBusString m;
1916  const DBusAuthMechanismHandler *mech;
1917 
1918  if (!_dbus_string_init (&m))
1919  goto nomem;
1920 
1921  if (!get_word (args, &next, &m))
1922  {
1923  _dbus_string_free (&m);
1924  goto nomem;
1925  }
1926 
1927  mech = find_mech (&m, auth->allowed_mechs);
1928 
1929  if (mech != NULL)
1930  {
1931  /* FIXME right now we try mechanisms in the order
1932  * the server lists them; should we do them in
1933  * some more deterministic order?
1934  *
1935  * Probably in all_mechanisms order, our order of
1936  * preference. Of course when the server is us,
1937  * it lists things in that order anyhow.
1938  */
1939 
1940  if (mech != &all_mechanisms[0])
1941  {
1942  _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1943  DBUS_AUTH_NAME (auth), mech->mechanism);
1944 
1945  if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1946  (void*) mech))
1947  {
1948  _dbus_string_free (&m);
1949  goto nomem;
1950  }
1951  }
1952  else
1953  {
1954  _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1955  DBUS_AUTH_NAME (auth), mech->mechanism);
1956  }
1957  }
1958  else
1959  {
1960  _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1961  DBUS_AUTH_NAME (auth),
1962  _dbus_string_get_const_data (&m));
1963  }
1964 
1965  _dbus_string_free (&m);
1966  }
1967 
1968  auth->already_got_mechanisms = TRUE;
1969 
1970  return TRUE;
1971 
1972  nomem:
1973  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1974 
1975  return FALSE;
1976 }
1977 
1978 static dbus_bool_t
1979 process_rejected (DBusAuth *auth, const DBusString *args)
1980 {
1981  const DBusAuthMechanismHandler *mech;
1982  DBusAuthClient *client;
1983 
1984  client = DBUS_AUTH_CLIENT (auth);
1985 
1986  if (!auth->already_got_mechanisms)
1987  {
1988  if (!record_mechanisms (auth, args))
1989  return FALSE;
1990  }
1991 
1992  if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1993  {
1994  mech = client->mechs_to_try->data;
1995 
1996  if (!send_auth (auth, mech))
1997  return FALSE;
1998 
2000 
2001  _dbus_verbose ("%s: Trying mechanism %s\n",
2002  DBUS_AUTH_NAME (auth),
2003  mech->mechanism);
2004  }
2005  else
2006  {
2007  /* Give up */
2008  _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2009  DBUS_AUTH_NAME (auth));
2010  goto_state (auth, &common_state_need_disconnect);
2011  }
2012 
2013  return TRUE;
2014 }
2015 
2016 
2017 static dbus_bool_t
2018 handle_client_state_waiting_for_data (DBusAuth *auth,
2019  DBusAuthCommand command,
2020  const DBusString *args)
2021 {
2022  _dbus_assert (auth->mech != NULL);
2023 
2024  switch (command)
2025  {
2026  case DBUS_AUTH_COMMAND_DATA:
2027  return process_data (auth, args, auth->mech->client_data_func);
2028 
2029  case DBUS_AUTH_COMMAND_REJECTED:
2030  return process_rejected (auth, args);
2031 
2032  case DBUS_AUTH_COMMAND_OK:
2033  return process_ok(auth, args);
2034 
2035  case DBUS_AUTH_COMMAND_ERROR:
2036  return send_cancel (auth);
2037 
2038  case DBUS_AUTH_COMMAND_AUTH:
2039  case DBUS_AUTH_COMMAND_CANCEL:
2040  case DBUS_AUTH_COMMAND_BEGIN:
2041  case DBUS_AUTH_COMMAND_UNKNOWN:
2042  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2043  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2044  default:
2045  return send_error (auth, "Unknown command");
2046  }
2047 }
2048 
2049 static dbus_bool_t
2050 handle_client_state_waiting_for_ok (DBusAuth *auth,
2051  DBusAuthCommand command,
2052  const DBusString *args)
2053 {
2054  switch (command)
2055  {
2056  case DBUS_AUTH_COMMAND_REJECTED:
2057  return process_rejected (auth, args);
2058 
2059  case DBUS_AUTH_COMMAND_OK:
2060  return process_ok(auth, args);
2061 
2062  case DBUS_AUTH_COMMAND_DATA:
2063  case DBUS_AUTH_COMMAND_ERROR:
2064  return send_cancel (auth);
2065 
2066  case DBUS_AUTH_COMMAND_AUTH:
2067  case DBUS_AUTH_COMMAND_CANCEL:
2068  case DBUS_AUTH_COMMAND_BEGIN:
2069  case DBUS_AUTH_COMMAND_UNKNOWN:
2070  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2071  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2072  default:
2073  return send_error (auth, "Unknown command");
2074  }
2075 }
2076 
2077 static dbus_bool_t
2078 handle_client_state_waiting_for_reject (DBusAuth *auth,
2079  DBusAuthCommand command,
2080  const DBusString *args)
2081 {
2082  switch (command)
2083  {
2084  case DBUS_AUTH_COMMAND_REJECTED:
2085  return process_rejected (auth, args);
2086 
2087  case DBUS_AUTH_COMMAND_AUTH:
2088  case DBUS_AUTH_COMMAND_CANCEL:
2089  case DBUS_AUTH_COMMAND_DATA:
2090  case DBUS_AUTH_COMMAND_BEGIN:
2091  case DBUS_AUTH_COMMAND_OK:
2092  case DBUS_AUTH_COMMAND_ERROR:
2093  case DBUS_AUTH_COMMAND_UNKNOWN:
2094  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2095  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2096  default:
2097  goto_state (auth, &common_state_need_disconnect);
2098  return TRUE;
2099  }
2100 }
2101 
2102 static dbus_bool_t
2103 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2104  DBusAuthCommand command,
2105  const DBusString *args)
2106 {
2107  switch (command)
2108  {
2109  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2111  auth->unix_fd_negotiated = TRUE;
2112  _dbus_verbose("Successfully negotiated UNIX FD passing\n");
2113  return send_begin (auth);
2114 
2115  case DBUS_AUTH_COMMAND_ERROR:
2117  auth->unix_fd_negotiated = FALSE;
2118  _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2119  return send_begin (auth);
2120 
2121  case DBUS_AUTH_COMMAND_OK:
2122  case DBUS_AUTH_COMMAND_DATA:
2123  case DBUS_AUTH_COMMAND_REJECTED:
2124  case DBUS_AUTH_COMMAND_AUTH:
2125  case DBUS_AUTH_COMMAND_CANCEL:
2126  case DBUS_AUTH_COMMAND_BEGIN:
2127  case DBUS_AUTH_COMMAND_UNKNOWN:
2128  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2129  default:
2130  return send_error (auth, "Unknown command");
2131  }
2132 }
2133 
2137 typedef struct {
2138  const char *name;
2141 
2142 static const DBusAuthCommandName auth_command_names[] = {
2143  { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2144  { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2145  { "DATA", DBUS_AUTH_COMMAND_DATA },
2146  { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2147  { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2148  { "OK", DBUS_AUTH_COMMAND_OK },
2149  { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2150  { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2151  { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2152 };
2153 
2154 static DBusAuthCommand
2155 lookup_command_from_name (DBusString *command)
2156 {
2157  int i;
2158 
2159  for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2160  {
2161  if (_dbus_string_equal_c_str (command,
2162  auth_command_names[i].name))
2163  return auth_command_names[i].command;
2164  }
2165 
2166  return DBUS_AUTH_COMMAND_UNKNOWN;
2167 }
2168 
2169 static void
2170 goto_state (DBusAuth *auth,
2171  const DBusAuthStateData *state)
2172 {
2173  _dbus_verbose ("%s: going from state %s to state %s\n",
2174  DBUS_AUTH_NAME (auth),
2175  auth->state->name,
2176  state->name);
2177 
2178  auth->state = state;
2179 }
2180 
2181 /* returns whether to call it again right away */
2182 static dbus_bool_t
2183 process_command (DBusAuth *auth)
2184 {
2185  DBusAuthCommand command;
2186  DBusString line;
2187  DBusString args;
2188  int eol;
2189  int i, j;
2190  dbus_bool_t retval;
2191 
2192  /* _dbus_verbose ("%s: trying process_command()\n"); */
2193 
2194  retval = FALSE;
2195 
2196  eol = 0;
2197  if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2198  return FALSE;
2199 
2200  if (!_dbus_string_init (&line))
2201  {
2202  auth->needed_memory = TRUE;
2203  return FALSE;
2204  }
2205 
2206  if (!_dbus_string_init (&args))
2207  {
2208  _dbus_string_free (&line);
2209  auth->needed_memory = TRUE;
2210  return FALSE;
2211  }
2212 
2213  if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2214  goto out;
2215 
2216  if (!_dbus_string_validate_ascii (&line, 0,
2217  _dbus_string_get_length (&line)))
2218  {
2219  _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2220  DBUS_AUTH_NAME (auth));
2221  if (!send_error (auth, "Command contained non-ASCII"))
2222  goto out;
2223  else
2224  goto next_command;
2225  }
2226 
2227  _dbus_verbose ("%s: got command \"%s\"\n",
2228  DBUS_AUTH_NAME (auth),
2229  _dbus_string_get_const_data (&line));
2230 
2231  _dbus_string_find_blank (&line, 0, &i);
2232  _dbus_string_skip_blank (&line, i, &j);
2233 
2234  if (j > i)
2235  _dbus_string_delete (&line, i, j - i);
2236 
2237  if (!_dbus_string_move (&line, i, &args, 0))
2238  goto out;
2239 
2240  /* FIXME 1.0 we should probably validate that only the allowed
2241  * chars are in the command name
2242  */
2243 
2244  command = lookup_command_from_name (&line);
2245  if (!(* auth->state->handler) (auth, command, &args))
2246  goto out;
2247 
2248  next_command:
2249 
2250  /* We've succeeded in processing the whole command so drop it out
2251  * of the incoming buffer and return TRUE to try another command.
2252  */
2253 
2254  _dbus_string_delete (&auth->incoming, 0, eol);
2255 
2256  /* kill the \r\n */
2257  _dbus_string_delete (&auth->incoming, 0, 2);
2258 
2259  retval = TRUE;
2260 
2261  out:
2262  _dbus_string_free (&args);
2263  _dbus_string_free (&line);
2264 
2265  if (!retval)
2266  auth->needed_memory = TRUE;
2267  else
2268  auth->needed_memory = FALSE;
2269 
2270  return retval;
2271 }
2272 
2273 
2288 DBusAuth*
2290 {
2291  DBusAuth *auth;
2292  DBusAuthServer *server_auth;
2293  DBusString guid_copy;
2294 
2295  if (!_dbus_string_init (&guid_copy))
2296  return NULL;
2297 
2298  if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2299  {
2300  _dbus_string_free (&guid_copy);
2301  return NULL;
2302  }
2303 
2304  auth = _dbus_auth_new (sizeof (DBusAuthServer));
2305  if (auth == NULL)
2306  {
2307  _dbus_string_free (&guid_copy);
2308  return NULL;
2309  }
2310 
2311  auth->side = auth_side_server;
2312  auth->state = &server_state_waiting_for_auth;
2313 
2314  server_auth = DBUS_AUTH_SERVER (auth);
2315 
2316  server_auth->guid = guid_copy;
2317 
2318  /* perhaps this should be per-mechanism with a lower
2319  * max
2320  */
2321  server_auth->failures = 0;
2322  server_auth->max_failures = 6;
2323 
2324  return auth;
2325 }
2326 
2334 DBusAuth*
2336 {
2337  DBusAuth *auth;
2338  DBusString guid_str;
2339 
2340  if (!_dbus_string_init (&guid_str))
2341  return NULL;
2342 
2343  auth = _dbus_auth_new (sizeof (DBusAuthClient));
2344  if (auth == NULL)
2345  {
2346  _dbus_string_free (&guid_str);
2347  return NULL;
2348  }
2349 
2350  DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2351 
2352  auth->side = auth_side_client;
2353  auth->state = &client_state_need_send_auth;
2354 
2355  /* Start the auth conversation by sending AUTH for our default
2356  * mechanism */
2357  if (!send_auth (auth, &all_mechanisms[0]))
2358  {
2359  _dbus_auth_unref (auth);
2360  return NULL;
2361  }
2362 
2363  return auth;
2364 }
2365 
2372 DBusAuth *
2374 {
2375  _dbus_assert (auth != NULL);
2376 
2377  auth->refcount += 1;
2378 
2379  return auth;
2380 }
2381 
2387 void
2389 {
2390  _dbus_assert (auth != NULL);
2391  _dbus_assert (auth->refcount > 0);
2392 
2393  auth->refcount -= 1;
2394  if (auth->refcount == 0)
2395  {
2396  shutdown_mech (auth);
2397 
2398  if (DBUS_AUTH_IS_CLIENT (auth))
2399  {
2400  _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2401  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2402  }
2403  else
2404  {
2406 
2407  _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2408  }
2409 
2410  if (auth->keyring)
2411  _dbus_keyring_unref (auth->keyring);
2412 
2413  _dbus_string_free (&auth->context);
2414  _dbus_string_free (&auth->challenge);
2415  _dbus_string_free (&auth->identity);
2416  _dbus_string_free (&auth->incoming);
2417  _dbus_string_free (&auth->outgoing);
2418 
2420 
2424 
2425  dbus_free (auth);
2426  }
2427 }
2428 
2439  const char **mechanisms)
2440 {
2441  char **copy;
2442 
2443  if (mechanisms != NULL)
2444  {
2445  copy = _dbus_dup_string_array (mechanisms);
2446  if (copy == NULL)
2447  return FALSE;
2448  }
2449  else
2450  copy = NULL;
2451 
2453 
2454  auth->allowed_mechs = copy;
2455 
2456  return TRUE;
2457 }
2458 
2463 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2464 
2472 DBusAuthState
2474 {
2475  auth->needed_memory = FALSE;
2476 
2477  /* Max amount we'll buffer up before deciding someone's on crack */
2478 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2479 
2480  do
2481  {
2482  if (DBUS_AUTH_IN_END_STATE (auth))
2483  break;
2484 
2485  if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2486  _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2487  {
2488  goto_state (auth, &common_state_need_disconnect);
2489  _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2490  DBUS_AUTH_NAME (auth));
2491  break;
2492  }
2493  }
2494  while (process_command (auth));
2495 
2496  if (auth->needed_memory)
2497  return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2498  else if (_dbus_string_get_length (&auth->outgoing) > 0)
2499  return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2500  else if (auth->state == &common_state_need_disconnect)
2501  return DBUS_AUTH_STATE_NEED_DISCONNECT;
2502  else if (auth->state == &common_state_authenticated)
2503  return DBUS_AUTH_STATE_AUTHENTICATED;
2504  else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2505 }
2506 
2518  const DBusString **str)
2519 {
2520  _dbus_assert (auth != NULL);
2521  _dbus_assert (str != NULL);
2522 
2523  *str = NULL;
2524 
2525  if (_dbus_string_get_length (&auth->outgoing) == 0)
2526  return FALSE;
2527 
2528  *str = &auth->outgoing;
2529 
2530  return TRUE;
2531 }
2532 
2541 void
2543  int bytes_sent)
2544 {
2545  _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2546  DBUS_AUTH_NAME (auth),
2547  bytes_sent,
2548  _dbus_string_get_const_data (&auth->outgoing));
2549 
2550  _dbus_string_delete (&auth->outgoing,
2551  0, bytes_sent);
2552 }
2553 
2561 void
2563  DBusString **buffer)
2564 {
2565  _dbus_assert (auth != NULL);
2567 
2568  *buffer = &auth->incoming;
2569 
2570  auth->buffer_outstanding = TRUE;
2571 }
2572 
2579 void
2581  DBusString *buffer)
2582 {
2583  _dbus_assert (buffer == &auth->incoming);
2585 
2586  auth->buffer_outstanding = FALSE;
2587 }
2588 
2598 void
2600  const DBusString **str)
2601 {
2602  if (!DBUS_AUTH_IN_END_STATE (auth))
2603  return;
2604 
2605  *str = &auth->incoming;
2606 }
2607 
2608 
2615 void
2617 {
2618  if (!DBUS_AUTH_IN_END_STATE (auth))
2619  return;
2620 
2621  _dbus_string_set_length (&auth->incoming, 0);
2622 }
2623 
2634 {
2635  if (auth->state != &common_state_authenticated)
2636  return FALSE;
2637 
2638  if (auth->mech != NULL)
2639  {
2640  if (DBUS_AUTH_IS_CLIENT (auth))
2641  return auth->mech->client_encode_func != NULL;
2642  else
2643  return auth->mech->server_encode_func != NULL;
2644  }
2645  else
2646  return FALSE;
2647 }
2648 
2661  const DBusString *plaintext,
2662  DBusString *encoded)
2663 {
2664  _dbus_assert (plaintext != encoded);
2665 
2666  if (auth->state != &common_state_authenticated)
2667  return FALSE;
2668 
2669  if (_dbus_auth_needs_encoding (auth))
2670  {
2671  if (DBUS_AUTH_IS_CLIENT (auth))
2672  return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2673  else
2674  return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2675  }
2676  else
2677  {
2678  return _dbus_string_copy (plaintext, 0, encoded,
2679  _dbus_string_get_length (encoded));
2680  }
2681 }
2682 
2693 {
2694  if (auth->state != &common_state_authenticated)
2695  return FALSE;
2696 
2697  if (auth->mech != NULL)
2698  {
2699  if (DBUS_AUTH_IS_CLIENT (auth))
2700  return auth->mech->client_decode_func != NULL;
2701  else
2702  return auth->mech->server_decode_func != NULL;
2703  }
2704  else
2705  return FALSE;
2706 }
2707 
2708 
2724  const DBusString *encoded,
2725  DBusString *plaintext)
2726 {
2727  _dbus_assert (plaintext != encoded);
2728 
2729  if (auth->state != &common_state_authenticated)
2730  return FALSE;
2731 
2732  if (_dbus_auth_needs_decoding (auth))
2733  {
2734  if (DBUS_AUTH_IS_CLIENT (auth))
2735  return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2736  else
2737  return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2738  }
2739  else
2740  {
2741  return _dbus_string_copy (encoded, 0, plaintext,
2742  _dbus_string_get_length (plaintext));
2743  }
2744 }
2745 
2756  DBusCredentials *credentials)
2757 {
2760  credentials);
2761 }
2762 
2774 {
2775  if (auth->state == &common_state_authenticated)
2776  {
2777  return auth->authorized_identity;
2778  }
2779  else
2780  {
2781  /* FIXME instead of this, keep an empty credential around that
2782  * doesn't require allocation or something
2783  */
2784  /* return empty credentials */
2786  return auth->authorized_identity;
2787  }
2788 }
2789 
2796 const char*
2798 {
2800 
2801  if (auth->state == &common_state_authenticated)
2802  return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2803  else
2804  return NULL;
2805 }
2806 
2817  const DBusString *context)
2818 {
2819  return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2820  &auth->context, 0, _dbus_string_get_length (context));
2821 }
2822 
2830 void
2832 {
2833  auth->unix_fd_possible = b;
2834 }
2835 
2844 {
2845  return auth->unix_fd_negotiated;
2846 }
2847 
2850 /* tests in dbus-auth-util.c */
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:935
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
Definition: dbus-sysdeps.c:435
const char * message
public error message field
Definition: dbus-errors.h:51
void _dbus_auth_delete_unused_bytes(DBusAuth *auth)
Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes() after we&#39;ve gotten them and succes...
Definition: dbus-auth.c:2616
#define NULL
A null pointer, defined appropriately for C or C++.
void _dbus_auth_get_unused_bytes(DBusAuth *auth, const DBusString **str)
Returns leftover bytes that were not used as part of the auth conversation.
Definition: dbus-auth.c:2599
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition: dbus-keyring.c:677
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2013
DBusAuthDecodeFunction client_decode_func
Function on client side for decode.
Definition: dbus-auth.c:112
DBusAuthEncodeFunction server_encode_func
Function on server side to encode.
Definition: dbus-auth.c:106
dbus_bool_t(* DBusAuthDecodeFunction)(DBusAuth *auth, const DBusString *data, DBusString *decoded)
This function decodes a block of data from the peer.
Definition: dbus-auth.c:90
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
dbus_bool_t _dbus_credentials_add_credential(DBusCredentials *credentials, DBusCredentialType which, DBusCredentials *other_credentials)
Merge the given credential found in the second object into the first object, overwriting the first ob...
DBusAuthCommand
Enumeration for the known authentication commands.
Definition: dbus-auth.c:119
dbus_bool_t _dbus_auth_needs_decoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to decode the message stream with _dbus_auth_de...
Definition: dbus-auth.c:2692
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2259
unsigned int buffer_outstanding
Buffer is &quot;checked out&quot; for reading data into.
Definition: dbus-auth.c:190
DBusList * mechs_to_try
Mechanisms we got from the server that we&#39;re going to try using.
Definition: dbus-auth.c:203
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:354
dbus_bool_t _dbus_credentials_are_superset(DBusCredentials *credentials, DBusCredentials *possible_subset)
Checks whether the first credentials object contains all the credentials found in the second credenti...
dbus_bool_t _dbus_auth_encode_data(DBusAuth *auth, const DBusString *plaintext, DBusString *encoded)
Called post-authentication, encodes a block of bytes for sending to the peer.
Definition: dbus-auth.c:2660
Internals of DBusKeyring.
Definition: dbus-keyring.c:111
dbus_bool_t(* DBusAuthEncodeFunction)(DBusAuth *auth, const DBusString *data, DBusString *encoded)
This function encodes a block of data from the peer.
Definition: dbus-auth.c:83
dbus_bool_t _dbus_auth_set_context(DBusAuth *auth, const DBusString *context)
Sets the &quot;authentication context&quot; which scopes cookies with the DBUS_COOKIE_SHA1 auth mechanism for e...
Definition: dbus-auth.c:2816
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void * data
Data stored at this element.
Definition: dbus-list.h:38
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2580
DBusAuthState _dbus_auth_do_work(DBusAuth *auth)
Analyzes buffered input and moves the auth conversation forward, returning the new state of the auth ...
Definition: dbus-auth.c:2473
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
const char * mechanism
Name of the mechanism.
Definition: dbus-auth.c:104
unsigned int unix_fd_negotiated
Unix fd was successfully negotiated.
Definition: dbus-auth.c:193
dbus_bool_t _dbus_keyring_get_hex_key(DBusKeyring *keyring, int key_id, DBusString *hex_key)
Gets the hex-encoded secret key for the given ID.
dbus_bool_t _dbus_auth_set_mechanisms(DBusAuth *auth, const char **mechanisms)
Sets an array of authentication mechanism names that we are willing to use.
Definition: dbus-auth.c:2438
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBusAuthShutdownFunction server_shutdown_func
Function on server side to shut down.
Definition: dbus-auth.c:108
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1283
DBusKeyring * keyring
Keyring for cookie mechanism.
Definition: dbus-auth.c:177
DBusString context
Cookie scope.
Definition: dbus-auth.c:176
dbus_bool_t _dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
Queries whether unix fd passing was successfully negotiated.
Definition: dbus-auth.c:2843
void _dbus_credentials_clear(DBusCredentials *credentials)
Clear all credentials in the object.
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition: dbus-string.c:1604
DBusString guid
Our globally unique ID in hex encoding.
Definition: dbus-auth.c:219
const char * side
Client or server.
Definition: dbus-auth.c:156
dbus_bool_t _dbus_credentials_add_credentials(DBusCredentials *credentials, DBusCredentials *other_credentials)
Merge all credentials found in the second object into the first object, overwriting the first object ...
DBusString guid_from_server
GUID received from server.
Definition: dbus-auth.c:205
DBusCredentials * _dbus_auth_get_identity(DBusAuth *auth)
Gets the identity we authorized the client as.
Definition: dbus-auth.c:2773
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we&#39;re conversing with.
Definition: dbus-auth.c:2562
DBusString challenge
Challenge sent to client.
Definition: dbus-auth.c:179
dbus_bool_t _dbus_auth_decode_data(DBusAuth *auth, const DBusString *encoded, DBusString *plaintext)
Called post-authentication, decodes a block of bytes received from the peer.
Definition: dbus-auth.c:2723
DBusCredentials * authorized_identity
Credentials that are authorized.
Definition: dbus-auth.c:172
DBusAuthDecodeFunction server_decode_func
Function on server side to decode.
Definition: dbus-auth.c:107
dbus_bool_t _dbus_string_move(DBusString *source, int start, DBusString *dest, int insert_at)
Moves the end of one string into another string.
Definition: dbus-string.c:1259
dbus_bool_t _dbus_append_user_from_current_process(DBusString *str)
Append to the string the identity we would like to have when we authenticate, on UNIX this is the cur...
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition: dbus-string.c:2152
void _dbus_auth_bytes_sent(DBusAuth *auth, int bytes_sent)
Notifies the auth conversation object that the given number of bytes of the outgoing buffer have been...
Definition: dbus-auth.c:2542
Internal members of DBusAuth.
Definition: dbus-auth.c:153
DBusInitialResponseFunction client_initial_response_func
Function on client side to handle initial response.
Definition: dbus-auth.c:109
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
&quot;Subclass&quot; of DBusAuth for server side.
Definition: dbus-auth.c:212
DBusAuthStateFunction handler
State function for this state.
Definition: dbus-auth.c:147
DBusAuthDataFunction client_data_func
Function on client side for DATA.
Definition: dbus-auth.c:110
const DBusAuthStateData * state
Current protocol state.
Definition: dbus-auth.c:161
dbus_bool_t _dbus_string_replace_len(const DBusString *source, int start, int len, DBusString *dest, int replace_at, int replace_len)
Replaces a segment of dest string with a segment of source string.
Definition: dbus-string.c:1404
&quot;Subclass&quot; of DBusAuth for client side
Definition: dbus-auth.c:199
DBusCredentials * desired_identity
Identity client has requested.
Definition: dbus-auth.c:174
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition: dbus-string.c:1803
DBusAuth * _dbus_auth_server_new(const DBusString *guid)
Creates a new auth conversation object for the server side.
Definition: dbus-auth.c:2289
unsigned int needed_memory
We needed memory to continue since last successful getting something done.
Definition: dbus-auth.c:185
#define DBUS_AUTH_NAME(auth)
The name of the auth (&quot;client&quot; or &quot;server&quot;)
Definition: dbus-auth.c:338
DBusAuth * _dbus_auth_ref(DBusAuth *auth)
Increments the refcount of an auth object.
Definition: dbus-auth.c:2373
unsigned int already_asked_for_initial_response
Already sent a blank challenge to get an initial response.
Definition: dbus-auth.c:189
void _dbus_string_delete(DBusString *str, int start, int len)
Deletes a segment of a DBusString with length len starting at start.
Definition: dbus-string.c:1193
DBusString identity
Current identity we&#39;re authorizing as.
Definition: dbus-auth.c:165
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:270
unsigned int already_got_mechanisms
Client already got mech list.
Definition: dbus-auth.c:188
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1114
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition: dbus-string.c:2685
int cookie_id
ID of cookie to use.
Definition: dbus-auth.c:178
Information about a auth state.
Definition: dbus-auth.c:144
Object representing an exception.
Definition: dbus-errors.h:48
int _dbus_keyring_get_best_key(DBusKeyring *keyring, DBusError *error)
Gets a recent key to use for authentication.
Definition: dbus-keyring.c:945
dbus_bool_t(* DBusAuthStateFunction)(DBusAuth *auth, DBusAuthCommand command, const DBusString *args)
Auth state function, determines the reaction to incoming events for a particular state.
Definition: dbus-auth.c:137
dbus_bool_t _dbus_string_validate_utf8(const DBusString *str, int start, int len)
Checks that the given range of the string is valid UTF-8.
Definition: dbus-string.c:2555
DBusAuth base
Parent class.
Definition: dbus-auth.c:201
DBusAuthShutdownFunction client_shutdown_func
Function on client side for shutdown.
Definition: dbus-auth.c:113
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
Definition: dbus-list.c:649
int refcount
reference count
Definition: dbus-auth.c:155
const char * _dbus_auth_get_guid_from_server(DBusAuth *auth)
Gets the GUID from the server if we&#39;ve authenticated; gets NULL otherwise.
Definition: dbus-auth.c:2797
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
char ** allowed_mechs
Mechanisms we&#39;re allowed to use, or NULL if we can use any.
Definition: dbus-auth.c:181
const char * name
Name of the command.
Definition: dbus-auth.c:2138
#define DBUS_AUTH_CLIENT(auth)
Definition: dbus-auth.c:326
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
#define TRUE
Expands to &quot;1&quot;.
int failures
Number of times client has been rejected.
Definition: dbus-auth.c:216
#define DBUS_AUTH_IS_SERVER(auth)
Definition: dbus-auth.c:316
#define N_CHALLENGE_BYTES
http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of entropy, we use 128.
Definition: dbus-auth.c:519
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
Definition: dbus-string.c:1765
DBusString incoming
Incoming data buffer.
Definition: dbus-auth.c:158
dbus_bool_t _dbus_auth_set_credentials(DBusAuth *auth, DBusCredentials *credentials)
Sets credentials received via reliable means from the operating system.
Definition: dbus-auth.c:2755
dbus_bool_t _dbus_keyring_is_for_credentials(DBusKeyring *keyring, DBusCredentials *credentials)
Checks whether the keyring is for the same user as the given credentials.
Definition: dbus-keyring.c:984
void _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
Sets whether unix fd passing is potentially on the transport and hence shall be negotiated.
Definition: dbus-auth.c:2831
const char * name
Name of the state.
Definition: dbus-auth.c:146
dbus_bool_t(* DBusAuthDataFunction)(DBusAuth *auth, const DBusString *data)
This function processes a block of data received from the peer.
Definition: dbus-auth.c:77
DBusAuthEncodeFunction client_encode_func
Function on client side for encode.
Definition: dbus-auth.c:111
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
DBusKeyring * _dbus_keyring_new_for_credentials(DBusCredentials *credentials, const DBusString *context, DBusError *error)
Creates a new keyring that lives in the ~/.dbus-keyrings directory of the user represented by credent...
Definition: dbus-keyring.c:705
DBusAuthDataFunction server_data_func
Function on server side for DATA.
Definition: dbus-auth.c:105
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition: dbus-string.c:2309
A node in a linked list.
Definition: dbus-list.h:34
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:749
dbus_bool_t _dbus_string_array_contains(const char **array, const char *str)
Checks whether a string array contains the given string.
int max_failures
Number of times we reject before disconnect.
Definition: dbus-auth.c:217
void _dbus_auth_unref(DBusAuth *auth)
Decrements the refcount of an auth object.
Definition: dbus-auth.c:2388
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of securely random bytes, using the best mechanism we can come up with...
dbus_bool_t _dbus_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we&#39;re conversing with.
Definition: dbus-auth.c:2517
Mapping from command name to enum.
Definition: dbus-auth.c:2137
Virtual table representing a particular auth mechanism.
Definition: dbus-auth.c:102
void(* DBusAuthShutdownFunction)(DBusAuth *auth)
This function is called when the mechanism is abandoned.
Definition: dbus-auth.c:97
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
#define FALSE
Expands to &quot;0&quot;.
const DBusAuthMechanismHandler * mech
Current auth mechanism.
Definition: dbus-auth.c:163
#define DBUS_AUTH_SERVER(auth)
Definition: dbus-auth.c:331
unsigned int unix_fd_possible
This side could do unix fd passing.
Definition: dbus-auth.c:192
dbus_bool_t _dbus_sha_compute(const DBusString *data, DBusString *ascii_output)
Computes the ASCII hex-encoded shasum of the given data and appends it to the output string...
Definition: dbus-sha.c:483
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:802
dbus_bool_t _dbus_string_copy_len(const DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_copy(), but can copy a segment from the middle of the source string.
Definition: dbus-string.c:1375
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
Definition: dbus-memory.c:531
dbus_bool_t _dbus_auth_needs_encoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to encode the message stream with _dbus_auth_en...
Definition: dbus-auth.c:2633
DBusCredentials * credentials
Credentials read from socket.
Definition: dbus-auth.c:169
DBusAuth * _dbus_auth_client_new(void)
Creates a new auth conversation object for the client side.
Definition: dbus-auth.c:2335
dbus_bool_t _dbus_string_validate_ascii(const DBusString *str, int start, int len)
Checks that the given range of the string is valid ASCII with no nul bytes.
Definition: dbus-string.c:2450
DBusAuth base
Parent class.
Definition: dbus-auth.c:214
dbus_bool_t(* DBusInitialResponseFunction)(DBusAuth *auth, DBusString *response)
This function appends an initial client response to the given string.
Definition: dbus-auth.c:70
dbus_bool_t _dbus_keyring_validate_context(const DBusString *context)
Checks whether the context is a valid context.
Definition: dbus-keyring.c:849
#define DBUS_AUTH_IN_END_STATE(auth)
Definition: dbus-auth.c:2463
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
DBusString outgoing
Outgoing data buffer.
Definition: dbus-auth.c:159
dbus_bool_t _dbus_credentials_are_empty(DBusCredentials *credentials)
Checks whether a credentials object contains anything.
#define DBUS_AUTH_IS_CLIENT(auth)
Definition: dbus-auth.c:321
DBusAuthCommand command
Corresponding enum.
Definition: dbus-auth.c:2139
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:542
dbus_bool_t _dbus_credentials_add_from_user(DBusCredentials *credentials, const DBusString *username)
Adds the credentials corresponding to the given username.