D-Bus  1.10.6
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,
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 static const DBusAuthStateData client_state_waiting_for_ok = {
286  "WaitingForOK", handle_client_state_waiting_for_ok
287 };
288 static const DBusAuthStateData client_state_waiting_for_reject = {
289  "WaitingForReject", handle_client_state_waiting_for_reject
290 };
291 static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
292  "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
293 };
294 
299 static const DBusAuthStateData common_state_authenticated = {
300  "Authenticated", NULL
301 };
302 
303 static const DBusAuthStateData common_state_need_disconnect = {
304  "NeedDisconnect", NULL
305 };
306 
307 static const char auth_side_client[] = "client";
308 static const char auth_side_server[] = "server";
313 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->side == auth_side_server)
314 
318 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->side == auth_side_client)
319 
323 #define DBUS_AUTH_CLIENT(auth) ((DBusAuthClient*)(auth))
324 
328 #define DBUS_AUTH_SERVER(auth) ((DBusAuthServer*)(auth))
329 
335 #define DBUS_AUTH_NAME(auth) ((auth)->side)
336 
337 static DBusAuth*
338 _dbus_auth_new (int size)
339 {
340  DBusAuth *auth;
341 
342  auth = dbus_malloc0 (size);
343  if (auth == NULL)
344  return NULL;
345 
346  auth->refcount = 1;
347 
348  auth->keyring = NULL;
349  auth->cookie_id = -1;
350 
351  /* note that we don't use the max string length feature,
352  * because you can't use that feature if you're going to
353  * try to recover from out-of-memory (it creates
354  * what looks like unrecoverable inability to alloc
355  * more space in the string). But we do handle
356  * overlong buffers in _dbus_auth_do_work().
357  */
358 
359  if (!_dbus_string_init (&auth->incoming))
360  goto enomem_0;
361 
362  if (!_dbus_string_init (&auth->outgoing))
363  goto enomem_1;
364 
365  if (!_dbus_string_init (&auth->identity))
366  goto enomem_2;
367 
368  if (!_dbus_string_init (&auth->context))
369  goto enomem_3;
370 
371  if (!_dbus_string_init (&auth->challenge))
372  goto enomem_4;
373 
374  /* default context if none is specified */
375  if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
376  goto enomem_5;
377 
379  if (auth->credentials == NULL)
380  goto enomem_6;
381 
383  if (auth->authorized_identity == NULL)
384  goto enomem_7;
385 
387  if (auth->desired_identity == NULL)
388  goto enomem_8;
389 
390  return auth;
391 
392 #if 0
393  enomem_9:
395 #endif
396  enomem_8:
398  enomem_7:
400  enomem_6:
401  /* last alloc was an append to context, which is freed already below */ ;
402  enomem_5:
403  _dbus_string_free (&auth->challenge);
404  enomem_4:
405  _dbus_string_free (&auth->context);
406  enomem_3:
407  _dbus_string_free (&auth->identity);
408  enomem_2:
409  _dbus_string_free (&auth->outgoing);
410  enomem_1:
411  _dbus_string_free (&auth->incoming);
412  enomem_0:
413  dbus_free (auth);
414  return NULL;
415 }
416 
417 static void
418 shutdown_mech (DBusAuth *auth)
419 {
420  /* Cancel any auth */
422  _dbus_string_set_length (&auth->identity, 0);
423 
426 
427  if (auth->mech != NULL)
428  {
429  _dbus_verbose ("%s: Shutting down mechanism %s\n",
430  DBUS_AUTH_NAME (auth), auth->mech->mechanism);
431 
432  if (DBUS_AUTH_IS_CLIENT (auth))
433  (* auth->mech->client_shutdown_func) (auth);
434  else
435  (* auth->mech->server_shutdown_func) (auth);
436 
437  auth->mech = NULL;
438  }
439 }
440 
441 /*
442  * DBUS_COOKIE_SHA1 mechanism
443  */
444 
445 /* Returns TRUE but with an empty string hash if the
446  * cookie_id isn't known. As with all this code
447  * TRUE just means we had enough memory.
448  */
449 static dbus_bool_t
450 sha1_compute_hash (DBusAuth *auth,
451  int cookie_id,
452  const DBusString *server_challenge,
453  const DBusString *client_challenge,
454  DBusString *hash)
455 {
456  DBusString cookie;
457  DBusString to_hash;
458  dbus_bool_t retval;
459 
460  _dbus_assert (auth->keyring != NULL);
461 
462  retval = FALSE;
463 
464  if (!_dbus_string_init (&cookie))
465  return FALSE;
466 
468  &cookie))
469  goto out_0;
470 
471  if (_dbus_string_get_length (&cookie) == 0)
472  {
473  retval = TRUE;
474  goto out_0;
475  }
476 
477  if (!_dbus_string_init (&to_hash))
478  goto out_0;
479 
480  if (!_dbus_string_copy (server_challenge, 0,
481  &to_hash, _dbus_string_get_length (&to_hash)))
482  goto out_1;
483 
484  if (!_dbus_string_append (&to_hash, ":"))
485  goto out_1;
486 
487  if (!_dbus_string_copy (client_challenge, 0,
488  &to_hash, _dbus_string_get_length (&to_hash)))
489  goto out_1;
490 
491  if (!_dbus_string_append (&to_hash, ":"))
492  goto out_1;
493 
494  if (!_dbus_string_copy (&cookie, 0,
495  &to_hash, _dbus_string_get_length (&to_hash)))
496  goto out_1;
497 
498  if (!_dbus_sha_compute (&to_hash, hash))
499  goto out_1;
500 
501  retval = TRUE;
502 
503  out_1:
504  _dbus_string_zero (&to_hash);
505  _dbus_string_free (&to_hash);
506  out_0:
507  _dbus_string_zero (&cookie);
508  _dbus_string_free (&cookie);
509  return retval;
510 }
511 
516 #define N_CHALLENGE_BYTES (128/8)
517 
518 static dbus_bool_t
519 sha1_handle_first_client_response (DBusAuth *auth,
520  const DBusString *data)
521 {
522  /* We haven't sent a challenge yet, we're expecting a desired
523  * username from the client.
524  */
525  DBusString tmp;
526  DBusString tmp2;
527  dbus_bool_t retval = FALSE;
528  DBusError error = DBUS_ERROR_INIT;
529  DBusCredentials *myself = NULL;
530 
532 
533  if (_dbus_string_get_length (data) > 0)
534  {
535  if (_dbus_string_get_length (&auth->identity) > 0)
536  {
537  /* Tried to send two auth identities, wtf */
538  _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
539  DBUS_AUTH_NAME (auth));
540  return send_rejected (auth);
541  }
542  else
543  {
544  /* this is our auth identity */
545  if (!_dbus_string_copy (data, 0, &auth->identity, 0))
546  return FALSE;
547  }
548  }
549 
551  {
552  _dbus_verbose ("%s: Did not get a valid username from client\n",
553  DBUS_AUTH_NAME (auth));
554  return send_rejected (auth);
555  }
556 
557  if (!_dbus_string_init (&tmp))
558  return FALSE;
559 
560  if (!_dbus_string_init (&tmp2))
561  {
562  _dbus_string_free (&tmp);
563  return FALSE;
564  }
565 
567 
568  if (myself == NULL)
569  goto out;
570 
571  if (!_dbus_credentials_same_user (myself, auth->desired_identity))
572  {
573  /*
574  * DBUS_COOKIE_SHA1 is not suitable for authenticating that the
575  * client is anyone other than the user owning the process
576  * containing the DBusServer: we probably aren't allowed to write
577  * to other users' home directories. Even if we can (for example
578  * uid 0 on traditional Unix or CAP_DAC_OVERRIDE on Linux), we
579  * must not, because the other user controls their home directory,
580  * and could carry out symlink attacks to make us read from or
581  * write to unintended locations. It's difficult to avoid symlink
582  * attacks in a portable way, so we just don't try. This isn't a
583  * regression, because DBUS_COOKIE_SHA1 never worked for other
584  * users anyway.
585  */
586  _dbus_verbose ("%s: client tried to authenticate as \"%s\", "
587  "but that doesn't match this process",
588  DBUS_AUTH_NAME (auth),
589  _dbus_string_get_const_data (data));
590  retval = send_rejected (auth);
591  goto out;
592  }
593 
594  /* we cache the keyring for speed, so here we drop it if it's the
595  * wrong one. FIXME caching the keyring here is useless since we use
596  * a different DBusAuth for every connection.
597  */
598  if (auth->keyring &&
600  auth->desired_identity))
601  {
603  auth->keyring = NULL;
604  }
605 
606  if (auth->keyring == NULL)
607  {
609  &auth->context,
610  &error);
611 
612  if (auth->keyring == NULL)
613  {
614  if (dbus_error_has_name (&error,
616  {
617  dbus_error_free (&error);
618  goto out;
619  }
620  else
621  {
622  _DBUS_ASSERT_ERROR_IS_SET (&error);
623  _dbus_verbose ("%s: Error loading keyring: %s\n",
624  DBUS_AUTH_NAME (auth), error.message);
625  if (send_rejected (auth))
626  retval = TRUE; /* retval is only about mem */
627  dbus_error_free (&error);
628  goto out;
629  }
630  }
631  else
632  {
633  _dbus_assert (!dbus_error_is_set (&error));
634  }
635  }
636 
637  _dbus_assert (auth->keyring != NULL);
638 
639  auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
640  if (auth->cookie_id < 0)
641  {
642  _DBUS_ASSERT_ERROR_IS_SET (&error);
643  _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
644  DBUS_AUTH_NAME (auth), error.message);
645  if (send_rejected (auth))
646  retval = TRUE;
647  dbus_error_free (&error);
648  goto out;
649  }
650  else
651  {
652  _dbus_assert (!dbus_error_is_set (&error));
653  }
654 
655  if (!_dbus_string_copy (&auth->context, 0,
656  &tmp2, _dbus_string_get_length (&tmp2)))
657  goto out;
658 
659  if (!_dbus_string_append (&tmp2, " "))
660  goto out;
661 
662  if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
663  goto out;
664 
665  if (!_dbus_string_append (&tmp2, " "))
666  goto out;
667 
668  if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES, &error))
669  {
671  {
672  dbus_error_free (&error);
673  goto out;
674  }
675  else
676  {
677  _DBUS_ASSERT_ERROR_IS_SET (&error);
678  _dbus_verbose ("%s: Error generating challenge: %s\n",
679  DBUS_AUTH_NAME (auth), error.message);
680  if (send_rejected (auth))
681  retval = TRUE; /* retval is only about mem */
682 
683  dbus_error_free (&error);
684  goto out;
685  }
686  }
687 
689  if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
690  goto out;
691 
692  if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
693  _dbus_string_get_length (&tmp2)))
694  goto out;
695 
696  if (!send_data (auth, &tmp2))
697  goto out;
698 
699  goto_state (auth, &server_state_waiting_for_data);
700  retval = TRUE;
701 
702  out:
703  _dbus_string_zero (&tmp);
704  _dbus_string_free (&tmp);
705  _dbus_string_zero (&tmp2);
706  _dbus_string_free (&tmp2);
707 
708  if (myself != NULL)
709  _dbus_credentials_unref (myself);
710 
711  return retval;
712 }
713 
714 static dbus_bool_t
715 sha1_handle_second_client_response (DBusAuth *auth,
716  const DBusString *data)
717 {
718  /* We are expecting a response which is the hex-encoded client
719  * challenge, space, then SHA-1 hash of the concatenation of our
720  * challenge, ":", client challenge, ":", secret key, all
721  * hex-encoded.
722  */
723  int i;
724  DBusString client_challenge;
725  DBusString client_hash;
726  dbus_bool_t retval;
727  DBusString correct_hash;
728 
729  retval = FALSE;
730 
731  if (!_dbus_string_find_blank (data, 0, &i))
732  {
733  _dbus_verbose ("%s: no space separator in client response\n",
734  DBUS_AUTH_NAME (auth));
735  return send_rejected (auth);
736  }
737 
738  if (!_dbus_string_init (&client_challenge))
739  goto out_0;
740 
741  if (!_dbus_string_init (&client_hash))
742  goto out_1;
743 
744  if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
745  0))
746  goto out_2;
747 
748  _dbus_string_skip_blank (data, i, &i);
749 
750  if (!_dbus_string_copy_len (data, i,
751  _dbus_string_get_length (data) - i,
752  &client_hash,
753  0))
754  goto out_2;
755 
756  if (_dbus_string_get_length (&client_challenge) == 0 ||
757  _dbus_string_get_length (&client_hash) == 0)
758  {
759  _dbus_verbose ("%s: zero-length client challenge or hash\n",
760  DBUS_AUTH_NAME (auth));
761  if (send_rejected (auth))
762  retval = TRUE;
763  goto out_2;
764  }
765 
766  if (!_dbus_string_init (&correct_hash))
767  goto out_2;
768 
769  if (!sha1_compute_hash (auth, auth->cookie_id,
770  &auth->challenge,
771  &client_challenge,
772  &correct_hash))
773  goto out_3;
774 
775  /* if cookie_id was invalid, then we get an empty hash */
776  if (_dbus_string_get_length (&correct_hash) == 0)
777  {
778  if (send_rejected (auth))
779  retval = TRUE;
780  goto out_3;
781  }
782 
783  if (!_dbus_string_equal (&client_hash, &correct_hash))
784  {
785  if (send_rejected (auth))
786  retval = TRUE;
787  goto out_3;
788  }
789 
791  auth->desired_identity))
792  goto out_3;
793 
794  /* Copy process ID from the socket credentials if it's there
795  */
797  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
798  auth->credentials))
799  goto out_3;
800 
801  if (!send_ok (auth))
802  goto out_3;
803 
804  _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
805  DBUS_AUTH_NAME (auth));
806 
807  retval = TRUE;
808 
809  out_3:
810  _dbus_string_zero (&correct_hash);
811  _dbus_string_free (&correct_hash);
812  out_2:
813  _dbus_string_zero (&client_hash);
814  _dbus_string_free (&client_hash);
815  out_1:
816  _dbus_string_free (&client_challenge);
817  out_0:
818  return retval;
819 }
820 
821 static dbus_bool_t
822 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
823  const DBusString *data)
824 {
825  if (auth->cookie_id < 0)
826  return sha1_handle_first_client_response (auth, data);
827  else
828  return sha1_handle_second_client_response (auth, data);
829 }
830 
831 static void
832 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
833 {
834  auth->cookie_id = -1;
836 }
837 
838 static dbus_bool_t
839 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
840  DBusString *response)
841 {
842  DBusString username;
843  dbus_bool_t retval;
844 
845  retval = FALSE;
846 
847  if (!_dbus_string_init (&username))
848  return FALSE;
849 
851  goto out_0;
852 
853  if (!_dbus_string_hex_encode (&username, 0,
854  response,
855  _dbus_string_get_length (response)))
856  goto out_0;
857 
858  retval = TRUE;
859 
860  out_0:
861  _dbus_string_free (&username);
862 
863  return retval;
864 }
865 
866 static dbus_bool_t
867 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
868  const DBusString *data)
869 {
870  /* The data we get from the server should be the cookie context
871  * name, the cookie ID, and the server challenge, separated by
872  * spaces. We send back our challenge string and the correct hash.
873  */
874  dbus_bool_t retval = FALSE;
876  DBusString cookie_id_str;
877  DBusString server_challenge;
878  DBusString client_challenge;
879  DBusString correct_hash;
880  DBusString tmp;
881  int i, j;
882  long val;
883  DBusError error = DBUS_ERROR_INIT;
884 
885  if (!_dbus_string_find_blank (data, 0, &i))
886  {
887  if (send_error (auth,
888  "Server did not send context/ID/challenge properly"))
889  retval = TRUE;
890  goto out_0;
891  }
892 
893  if (!_dbus_string_init (&context))
894  goto out_0;
895 
896  if (!_dbus_string_copy_len (data, 0, i,
897  &context, 0))
898  goto out_1;
899 
900  _dbus_string_skip_blank (data, i, &i);
901  if (!_dbus_string_find_blank (data, i, &j))
902  {
903  if (send_error (auth,
904  "Server did not send context/ID/challenge properly"))
905  retval = TRUE;
906  goto out_1;
907  }
908 
909  if (!_dbus_string_init (&cookie_id_str))
910  goto out_1;
911 
912  if (!_dbus_string_copy_len (data, i, j - i,
913  &cookie_id_str, 0))
914  goto out_2;
915 
916  if (!_dbus_string_init (&server_challenge))
917  goto out_2;
918 
919  i = j;
920  _dbus_string_skip_blank (data, i, &i);
921  j = _dbus_string_get_length (data);
922 
923  if (!_dbus_string_copy_len (data, i, j - i,
924  &server_challenge, 0))
925  goto out_3;
926 
927  if (!_dbus_keyring_validate_context (&context))
928  {
929  if (send_error (auth, "Server sent invalid cookie context"))
930  retval = TRUE;
931  goto out_3;
932  }
933 
934  if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
935  {
936  if (send_error (auth, "Could not parse cookie ID as an integer"))
937  retval = TRUE;
938  goto out_3;
939  }
940 
941  if (_dbus_string_get_length (&server_challenge) == 0)
942  {
943  if (send_error (auth, "Empty server challenge string"))
944  retval = TRUE;
945  goto out_3;
946  }
947 
948  if (auth->keyring == NULL)
949  {
951  &context,
952  &error);
953 
954  if (auth->keyring == NULL)
955  {
956  if (dbus_error_has_name (&error,
958  {
959  dbus_error_free (&error);
960  goto out_3;
961  }
962  else
963  {
964  _DBUS_ASSERT_ERROR_IS_SET (&error);
965 
966  _dbus_verbose ("%s: Error loading keyring: %s\n",
967  DBUS_AUTH_NAME (auth), error.message);
968 
969  if (send_error (auth, "Could not load cookie file"))
970  retval = TRUE; /* retval is only about mem */
971 
972  dbus_error_free (&error);
973  goto out_3;
974  }
975  }
976  else
977  {
978  _dbus_assert (!dbus_error_is_set (&error));
979  }
980  }
981 
982  _dbus_assert (auth->keyring != NULL);
983 
984  if (!_dbus_string_init (&tmp))
985  goto out_3;
986 
987  if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES, &error))
988  {
990  {
991  dbus_error_free (&error);
992  goto out_4;
993  }
994  else
995  {
996  _DBUS_ASSERT_ERROR_IS_SET (&error);
997 
998  _dbus_verbose ("%s: Failed to generate challenge: %s\n",
999  DBUS_AUTH_NAME (auth), error.message);
1000 
1001  if (send_error (auth, "Failed to generate challenge"))
1002  retval = TRUE; /* retval is only about mem */
1003 
1004  dbus_error_free (&error);
1005  goto out_4;
1006  }
1007  }
1008 
1009  if (!_dbus_string_init (&client_challenge))
1010  goto out_4;
1011 
1012  if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
1013  goto out_5;
1014 
1015  if (!_dbus_string_init (&correct_hash))
1016  goto out_5;
1017 
1018  if (!sha1_compute_hash (auth, val,
1019  &server_challenge,
1020  &client_challenge,
1021  &correct_hash))
1022  goto out_6;
1023 
1024  if (_dbus_string_get_length (&correct_hash) == 0)
1025  {
1026  /* couldn't find the cookie ID or something */
1027  if (send_error (auth, "Don't have the requested cookie ID"))
1028  retval = TRUE;
1029  goto out_6;
1030  }
1031 
1032  _dbus_string_set_length (&tmp, 0);
1033 
1034  if (!_dbus_string_copy (&client_challenge, 0, &tmp,
1035  _dbus_string_get_length (&tmp)))
1036  goto out_6;
1037 
1038  if (!_dbus_string_append (&tmp, " "))
1039  goto out_6;
1040 
1041  if (!_dbus_string_copy (&correct_hash, 0, &tmp,
1042  _dbus_string_get_length (&tmp)))
1043  goto out_6;
1044 
1045  if (!send_data (auth, &tmp))
1046  goto out_6;
1047 
1048  retval = TRUE;
1049 
1050  out_6:
1051  _dbus_string_zero (&correct_hash);
1052  _dbus_string_free (&correct_hash);
1053  out_5:
1054  _dbus_string_free (&client_challenge);
1055  out_4:
1056  _dbus_string_zero (&tmp);
1057  _dbus_string_free (&tmp);
1058  out_3:
1059  _dbus_string_free (&server_challenge);
1060  out_2:
1061  _dbus_string_free (&cookie_id_str);
1062  out_1:
1063  _dbus_string_free (&context);
1064  out_0:
1065  return retval;
1066 }
1067 
1068 static void
1069 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1070 {
1071  auth->cookie_id = -1;
1072  _dbus_string_set_length (&auth->challenge, 0);
1073 }
1074 
1075 /*
1076  * EXTERNAL mechanism
1077  */
1078 
1079 static dbus_bool_t
1080 handle_server_data_external_mech (DBusAuth *auth,
1081  const DBusString *data)
1082 {
1084  {
1085  _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1086  DBUS_AUTH_NAME (auth));
1087  return send_rejected (auth);
1088  }
1089 
1090  if (_dbus_string_get_length (data) > 0)
1091  {
1092  if (_dbus_string_get_length (&auth->identity) > 0)
1093  {
1094  /* Tried to send two auth identities, wtf */
1095  _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1096  DBUS_AUTH_NAME (auth));
1097  return send_rejected (auth);
1098  }
1099  else
1100  {
1101  /* this is our auth identity */
1102  if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1103  return FALSE;
1104  }
1105  }
1106 
1107  /* Poke client for an auth identity, if none given */
1108  if (_dbus_string_get_length (&auth->identity) == 0 &&
1110  {
1111  if (send_data (auth, NULL))
1112  {
1113  _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1114  DBUS_AUTH_NAME (auth));
1116  goto_state (auth, &server_state_waiting_for_data);
1117  return TRUE;
1118  }
1119  else
1120  return FALSE;
1121  }
1122 
1124 
1125  /* If auth->identity is still empty here, then client
1126  * responded with an empty string after we poked it for
1127  * an initial response. This means to try to auth the
1128  * identity provided in the credentials.
1129  */
1130  if (_dbus_string_get_length (&auth->identity) == 0)
1131  {
1133  auth->credentials))
1134  {
1135  return FALSE; /* OOM */
1136  }
1137  }
1138  else
1139  {
1141  &auth->identity))
1142  {
1143  _dbus_verbose ("%s: could not get credentials from uid string\n",
1144  DBUS_AUTH_NAME (auth));
1145  return send_rejected (auth);
1146  }
1147  }
1148 
1150  {
1151  _dbus_verbose ("%s: desired user %s is no good\n",
1152  DBUS_AUTH_NAME (auth),
1153  _dbus_string_get_const_data (&auth->identity));
1154  return send_rejected (auth);
1155  }
1156 
1158  auth->desired_identity))
1159  {
1160  /* client has authenticated */
1162  auth->desired_identity))
1163  return FALSE;
1164 
1165  /* also copy misc process info from the socket credentials
1166  */
1168  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1169  auth->credentials))
1170  return FALSE;
1171 
1173  DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1174  auth->credentials))
1175  return FALSE;
1176 
1178  DBUS_CREDENTIAL_LINUX_SECURITY_LABEL,
1179  auth->credentials))
1180  return FALSE;
1181 
1182  if (!send_ok (auth))
1183  return FALSE;
1184 
1185  _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1186  DBUS_AUTH_NAME (auth));
1187 
1188  return TRUE;
1189  }
1190  else
1191  {
1192  _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1193  DBUS_AUTH_NAME (auth));
1194  return send_rejected (auth);
1195  }
1196 }
1197 
1198 static void
1199 handle_server_shutdown_external_mech (DBusAuth *auth)
1200 {
1201 
1202 }
1203 
1204 static dbus_bool_t
1205 handle_client_initial_response_external_mech (DBusAuth *auth,
1206  DBusString *response)
1207 {
1208  /* We always append our UID as an initial response, so the server
1209  * doesn't have to send back an empty challenge to check whether we
1210  * want to specify an identity. i.e. this avoids a round trip that
1211  * the spec for the EXTERNAL mechanism otherwise requires.
1212  */
1213  DBusString plaintext;
1214 
1215  if (!_dbus_string_init (&plaintext))
1216  return FALSE;
1217 
1218  if (!_dbus_append_user_from_current_process (&plaintext))
1219  goto failed;
1220 
1221  if (!_dbus_string_hex_encode (&plaintext, 0,
1222  response,
1223  _dbus_string_get_length (response)))
1224  goto failed;
1225 
1226  _dbus_string_free (&plaintext);
1227 
1228  return TRUE;
1229 
1230  failed:
1231  _dbus_string_free (&plaintext);
1232  return FALSE;
1233 }
1234 
1235 static dbus_bool_t
1236 handle_client_data_external_mech (DBusAuth *auth,
1237  const DBusString *data)
1238 {
1239 
1240  return TRUE;
1241 }
1242 
1243 static void
1244 handle_client_shutdown_external_mech (DBusAuth *auth)
1245 {
1246 
1247 }
1248 
1249 /*
1250  * ANONYMOUS mechanism
1251  */
1252 
1253 static dbus_bool_t
1254 handle_server_data_anonymous_mech (DBusAuth *auth,
1255  const DBusString *data)
1256 {
1257  if (_dbus_string_get_length (data) > 0)
1258  {
1259  /* Client is allowed to send "trace" data, the only defined
1260  * meaning is that if it contains '@' it is an email address,
1261  * and otherwise it is anything else, and it's supposed to be
1262  * UTF-8
1263  */
1264  if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1265  {
1266  _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1267  DBUS_AUTH_NAME (auth));
1268  return send_rejected (auth);
1269  }
1270 
1271  _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1272  DBUS_AUTH_NAME (auth),
1273  _dbus_string_get_const_data (data));
1274  }
1275 
1276  /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1278 
1279  /* Copy process ID from the socket credentials
1280  */
1282  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1283  auth->credentials))
1284  return FALSE;
1285 
1286  /* Anonymous is always allowed */
1287  if (!send_ok (auth))
1288  return FALSE;
1289 
1290  _dbus_verbose ("%s: authenticated client as anonymous\n",
1291  DBUS_AUTH_NAME (auth));
1292 
1293  return TRUE;
1294 }
1295 
1296 static void
1297 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1298 {
1299 
1300 }
1301 
1302 static dbus_bool_t
1303 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1304  DBusString *response)
1305 {
1306  /* Our initial response is a "trace" string which must be valid UTF-8
1307  * and must be an email address if it contains '@'.
1308  * We just send the dbus implementation info, like a user-agent or
1309  * something, because... why not. There's nothing guaranteed here
1310  * though, we could change it later.
1311  */
1312  DBusString plaintext;
1313 
1314  if (!_dbus_string_init (&plaintext))
1315  return FALSE;
1316 
1317  if (!_dbus_string_append (&plaintext,
1318  "libdbus " DBUS_VERSION_STRING))
1319  goto failed;
1320 
1321  if (!_dbus_string_hex_encode (&plaintext, 0,
1322  response,
1323  _dbus_string_get_length (response)))
1324  goto failed;
1325 
1326  _dbus_string_free (&plaintext);
1327 
1328  return TRUE;
1329 
1330  failed:
1331  _dbus_string_free (&plaintext);
1332  return FALSE;
1333 }
1334 
1335 static dbus_bool_t
1336 handle_client_data_anonymous_mech (DBusAuth *auth,
1337  const DBusString *data)
1338 {
1339 
1340  return TRUE;
1341 }
1342 
1343 static void
1344 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1345 {
1346 
1347 }
1348 
1349 /* Put mechanisms here in order of preference.
1350  * Right now we have:
1351  *
1352  * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1353  * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1354  * - ANONYMOUS checks nothing but doesn't auth the person as a user
1355  *
1356  * We might ideally add a mechanism to chain to Cyrus SASL so we can
1357  * use its mechanisms as well.
1358  *
1359  */
1360 static const DBusAuthMechanismHandler
1361 all_mechanisms[] = {
1362  { "EXTERNAL",
1363  handle_server_data_external_mech,
1364  NULL, NULL,
1365  handle_server_shutdown_external_mech,
1366  handle_client_initial_response_external_mech,
1367  handle_client_data_external_mech,
1368  NULL, NULL,
1369  handle_client_shutdown_external_mech },
1370  { "DBUS_COOKIE_SHA1",
1371  handle_server_data_cookie_sha1_mech,
1372  NULL, NULL,
1373  handle_server_shutdown_cookie_sha1_mech,
1374  handle_client_initial_response_cookie_sha1_mech,
1375  handle_client_data_cookie_sha1_mech,
1376  NULL, NULL,
1377  handle_client_shutdown_cookie_sha1_mech },
1378  { "ANONYMOUS",
1379  handle_server_data_anonymous_mech,
1380  NULL, NULL,
1381  handle_server_shutdown_anonymous_mech,
1382  handle_client_initial_response_anonymous_mech,
1383  handle_client_data_anonymous_mech,
1384  NULL, NULL,
1385  handle_client_shutdown_anonymous_mech },
1386  { NULL, NULL }
1387 };
1388 
1389 static const DBusAuthMechanismHandler*
1390 find_mech (const DBusString *name,
1391  char **allowed_mechs)
1392 {
1393  int i;
1394 
1395  if (allowed_mechs != NULL &&
1396  !_dbus_string_array_contains ((const char**) allowed_mechs,
1397  _dbus_string_get_const_data (name)))
1398  return NULL;
1399 
1400  i = 0;
1401  while (all_mechanisms[i].mechanism != NULL)
1402  {
1403  if (_dbus_string_equal_c_str (name,
1404  all_mechanisms[i].mechanism))
1405 
1406  return &all_mechanisms[i];
1407 
1408  ++i;
1409  }
1410 
1411  return NULL;
1412 }
1413 
1414 static dbus_bool_t
1415 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1416 {
1417  DBusString auth_command;
1418 
1419  if (!_dbus_string_init (&auth_command))
1420  return FALSE;
1421 
1422  if (!_dbus_string_append (&auth_command,
1423  "AUTH "))
1424  {
1425  _dbus_string_free (&auth_command);
1426  return FALSE;
1427  }
1428 
1429  if (!_dbus_string_append (&auth_command,
1430  mech->mechanism))
1431  {
1432  _dbus_string_free (&auth_command);
1433  return FALSE;
1434  }
1435 
1436  if (mech->client_initial_response_func != NULL)
1437  {
1438  if (!_dbus_string_append (&auth_command, " "))
1439  {
1440  _dbus_string_free (&auth_command);
1441  return FALSE;
1442  }
1443 
1444  if (!(* mech->client_initial_response_func) (auth, &auth_command))
1445  {
1446  _dbus_string_free (&auth_command);
1447  return FALSE;
1448  }
1449  }
1450 
1451  if (!_dbus_string_append (&auth_command,
1452  "\r\n"))
1453  {
1454  _dbus_string_free (&auth_command);
1455  return FALSE;
1456  }
1457 
1458  if (!_dbus_string_copy (&auth_command, 0,
1459  &auth->outgoing,
1460  _dbus_string_get_length (&auth->outgoing)))
1461  {
1462  _dbus_string_free (&auth_command);
1463  return FALSE;
1464  }
1465 
1466  _dbus_string_free (&auth_command);
1467  shutdown_mech (auth);
1468  auth->mech = mech;
1469  goto_state (auth, &client_state_waiting_for_data);
1470 
1471  return TRUE;
1472 }
1473 
1474 static dbus_bool_t
1475 send_data (DBusAuth *auth, DBusString *data)
1476 {
1477  int old_len;
1478 
1479  if (data == NULL || _dbus_string_get_length (data) == 0)
1480  return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1481  else
1482  {
1483  old_len = _dbus_string_get_length (&auth->outgoing);
1484  if (!_dbus_string_append (&auth->outgoing, "DATA "))
1485  goto out;
1486 
1487  if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1488  _dbus_string_get_length (&auth->outgoing)))
1489  goto out;
1490 
1491  if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1492  goto out;
1493 
1494  return TRUE;
1495 
1496  out:
1497  _dbus_string_set_length (&auth->outgoing, old_len);
1498 
1499  return FALSE;
1500  }
1501 }
1502 
1503 static dbus_bool_t
1504 send_rejected (DBusAuth *auth)
1505 {
1506  DBusString command;
1507  DBusAuthServer *server_auth;
1508  int i;
1509 
1510  if (!_dbus_string_init (&command))
1511  return FALSE;
1512 
1513  if (!_dbus_string_append (&command,
1514  "REJECTED"))
1515  goto nomem;
1516 
1517  i = 0;
1518  while (all_mechanisms[i].mechanism != NULL)
1519  {
1520  if (!_dbus_string_append (&command,
1521  " "))
1522  goto nomem;
1523 
1524  if (!_dbus_string_append (&command,
1525  all_mechanisms[i].mechanism))
1526  goto nomem;
1527 
1528  ++i;
1529  }
1530 
1531  if (!_dbus_string_append (&command, "\r\n"))
1532  goto nomem;
1533 
1534  if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1535  _dbus_string_get_length (&auth->outgoing)))
1536  goto nomem;
1537 
1538  shutdown_mech (auth);
1539 
1541  server_auth = DBUS_AUTH_SERVER (auth);
1542  server_auth->failures += 1;
1543 
1544  if (server_auth->failures >= server_auth->max_failures)
1545  goto_state (auth, &common_state_need_disconnect);
1546  else
1547  goto_state (auth, &server_state_waiting_for_auth);
1548 
1549  _dbus_string_free (&command);
1550 
1551  return TRUE;
1552 
1553  nomem:
1554  _dbus_string_free (&command);
1555  return FALSE;
1556 }
1557 
1558 static dbus_bool_t
1559 send_error (DBusAuth *auth, const char *message)
1560 {
1561  return _dbus_string_append_printf (&auth->outgoing,
1562  "ERROR \"%s\"\r\n", message);
1563 }
1564 
1565 static dbus_bool_t
1566 send_ok (DBusAuth *auth)
1567 {
1568  int orig_len;
1569 
1570  orig_len = _dbus_string_get_length (&auth->outgoing);
1571 
1572  if (_dbus_string_append (&auth->outgoing, "OK ") &&
1573  _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1574  0,
1575  &auth->outgoing,
1576  _dbus_string_get_length (&auth->outgoing)) &&
1577  _dbus_string_append (&auth->outgoing, "\r\n"))
1578  {
1579  goto_state (auth, &server_state_waiting_for_begin);
1580  return TRUE;
1581  }
1582  else
1583  {
1584  _dbus_string_set_length (&auth->outgoing, orig_len);
1585  return FALSE;
1586  }
1587 }
1588 
1589 static dbus_bool_t
1590 send_begin (DBusAuth *auth)
1591 {
1592 
1593  if (!_dbus_string_append (&auth->outgoing,
1594  "BEGIN\r\n"))
1595  return FALSE;
1596 
1597  goto_state (auth, &common_state_authenticated);
1598  return TRUE;
1599 }
1600 
1601 static dbus_bool_t
1602 process_ok(DBusAuth *auth,
1603  const DBusString *args_from_ok) {
1604 
1605  int end_of_hex;
1606 
1607  /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1608  _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1609 
1610  /* We decode the hex string to binary, using guid_from_server as scratch... */
1611 
1612  end_of_hex = 0;
1613  if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1614  & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1615  return FALSE;
1616 
1617  /* now clear out the scratch */
1618  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1619 
1620  if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1621  end_of_hex == 0)
1622  {
1623  _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1624  end_of_hex, _dbus_string_get_length (args_from_ok));
1625  goto_state (auth, &common_state_need_disconnect);
1626  return TRUE;
1627  }
1628 
1629  if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1630  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1631  return FALSE;
1632  }
1633 
1634  _dbus_verbose ("Got GUID '%s' from the server\n",
1635  _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1636 
1637  if (auth->unix_fd_possible)
1638  return send_negotiate_unix_fd(auth);
1639 
1640  _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1641  return send_begin (auth);
1642 }
1643 
1644 static dbus_bool_t
1645 send_cancel (DBusAuth *auth)
1646 {
1647  if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1648  {
1649  goto_state (auth, &client_state_waiting_for_reject);
1650  return TRUE;
1651  }
1652  else
1653  return FALSE;
1654 }
1655 
1656 static dbus_bool_t
1657 process_data (DBusAuth *auth,
1658  const DBusString *args,
1659  DBusAuthDataFunction data_func)
1660 {
1661  int end;
1662  DBusString decoded;
1663 
1664  if (!_dbus_string_init (&decoded))
1665  return FALSE;
1666 
1667  if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1668  {
1669  _dbus_string_free (&decoded);
1670  return FALSE;
1671  }
1672 
1673  if (_dbus_string_get_length (args) != end)
1674  {
1675  _dbus_string_free (&decoded);
1676  if (!send_error (auth, "Invalid hex encoding"))
1677  return FALSE;
1678 
1679  return TRUE;
1680  }
1681 
1682 #ifdef DBUS_ENABLE_VERBOSE_MODE
1683  if (_dbus_string_validate_ascii (&decoded, 0,
1684  _dbus_string_get_length (&decoded)))
1685  _dbus_verbose ("%s: data: '%s'\n",
1686  DBUS_AUTH_NAME (auth),
1687  _dbus_string_get_const_data (&decoded));
1688 #endif
1689 
1690  if (!(* data_func) (auth, &decoded))
1691  {
1692  _dbus_string_free (&decoded);
1693  return FALSE;
1694  }
1695 
1696  _dbus_string_free (&decoded);
1697  return TRUE;
1698 }
1699 
1700 static dbus_bool_t
1701 send_negotiate_unix_fd (DBusAuth *auth)
1702 {
1703  if (!_dbus_string_append (&auth->outgoing,
1704  "NEGOTIATE_UNIX_FD\r\n"))
1705  return FALSE;
1706 
1707  goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1708  return TRUE;
1709 }
1710 
1711 static dbus_bool_t
1712 send_agree_unix_fd (DBusAuth *auth)
1713 {
1715 
1716  auth->unix_fd_negotiated = TRUE;
1717  _dbus_verbose("Agreed to UNIX FD passing\n");
1718 
1719  if (!_dbus_string_append (&auth->outgoing,
1720  "AGREE_UNIX_FD\r\n"))
1721  return FALSE;
1722 
1723  goto_state (auth, &server_state_waiting_for_begin);
1724  return TRUE;
1725 }
1726 
1727 static dbus_bool_t
1728 handle_auth (DBusAuth *auth, const DBusString *args)
1729 {
1730  if (_dbus_string_get_length (args) == 0)
1731  {
1732  /* No args to the auth, send mechanisms */
1733  if (!send_rejected (auth))
1734  return FALSE;
1735 
1736  return TRUE;
1737  }
1738  else
1739  {
1740  int i;
1741  DBusString mech;
1742  DBusString hex_response;
1743 
1744  _dbus_string_find_blank (args, 0, &i);
1745 
1746  if (!_dbus_string_init (&mech))
1747  return FALSE;
1748 
1749  if (!_dbus_string_init (&hex_response))
1750  {
1751  _dbus_string_free (&mech);
1752  return FALSE;
1753  }
1754 
1755  if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1756  goto failed;
1757 
1758  _dbus_string_skip_blank (args, i, &i);
1759  if (!_dbus_string_copy (args, i, &hex_response, 0))
1760  goto failed;
1761 
1762  auth->mech = find_mech (&mech, auth->allowed_mechs);
1763  if (auth->mech != NULL)
1764  {
1765  _dbus_verbose ("%s: Trying mechanism %s\n",
1766  DBUS_AUTH_NAME (auth),
1767  auth->mech->mechanism);
1768 
1769  if (!process_data (auth, &hex_response,
1770  auth->mech->server_data_func))
1771  goto failed;
1772  }
1773  else
1774  {
1775  /* Unsupported mechanism */
1776  _dbus_verbose ("%s: Unsupported mechanism %s\n",
1777  DBUS_AUTH_NAME (auth),
1778  _dbus_string_get_const_data (&mech));
1779 
1780  if (!send_rejected (auth))
1781  goto failed;
1782  }
1783 
1784  _dbus_string_free (&mech);
1785  _dbus_string_free (&hex_response);
1786 
1787  return TRUE;
1788 
1789  failed:
1790  auth->mech = NULL;
1791  _dbus_string_free (&mech);
1792  _dbus_string_free (&hex_response);
1793  return FALSE;
1794  }
1795 }
1796 
1797 static dbus_bool_t
1798 handle_server_state_waiting_for_auth (DBusAuth *auth,
1799  DBusAuthCommand command,
1800  const DBusString *args)
1801 {
1802  switch (command)
1803  {
1804  case DBUS_AUTH_COMMAND_AUTH:
1805  return handle_auth (auth, args);
1806 
1807  case DBUS_AUTH_COMMAND_CANCEL:
1808  case DBUS_AUTH_COMMAND_DATA:
1809  return send_error (auth, "Not currently in an auth conversation");
1810 
1811  case DBUS_AUTH_COMMAND_BEGIN:
1812  goto_state (auth, &common_state_need_disconnect);
1813  return TRUE;
1814 
1815  case DBUS_AUTH_COMMAND_ERROR:
1816  return send_rejected (auth);
1817 
1818  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1819  return send_error (auth, "Need to authenticate first");
1820 
1821  case DBUS_AUTH_COMMAND_REJECTED:
1822  case DBUS_AUTH_COMMAND_OK:
1823  case DBUS_AUTH_COMMAND_UNKNOWN:
1824  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1825  default:
1826  return send_error (auth, "Unknown command");
1827  }
1828 }
1829 
1830 static dbus_bool_t
1831 handle_server_state_waiting_for_data (DBusAuth *auth,
1832  DBusAuthCommand command,
1833  const DBusString *args)
1834 {
1835  switch (command)
1836  {
1837  case DBUS_AUTH_COMMAND_AUTH:
1838  return send_error (auth, "Sent AUTH while another AUTH in progress");
1839 
1840  case DBUS_AUTH_COMMAND_CANCEL:
1841  case DBUS_AUTH_COMMAND_ERROR:
1842  return send_rejected (auth);
1843 
1844  case DBUS_AUTH_COMMAND_DATA:
1845  return process_data (auth, args, auth->mech->server_data_func);
1846 
1847  case DBUS_AUTH_COMMAND_BEGIN:
1848  goto_state (auth, &common_state_need_disconnect);
1849  return TRUE;
1850 
1851  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1852  return send_error (auth, "Need to authenticate first");
1853 
1854  case DBUS_AUTH_COMMAND_REJECTED:
1855  case DBUS_AUTH_COMMAND_OK:
1856  case DBUS_AUTH_COMMAND_UNKNOWN:
1857  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1858  default:
1859  return send_error (auth, "Unknown command");
1860  }
1861 }
1862 
1863 static dbus_bool_t
1864 handle_server_state_waiting_for_begin (DBusAuth *auth,
1865  DBusAuthCommand command,
1866  const DBusString *args)
1867 {
1868  switch (command)
1869  {
1870  case DBUS_AUTH_COMMAND_AUTH:
1871  return send_error (auth, "Sent AUTH while expecting BEGIN");
1872 
1873  case DBUS_AUTH_COMMAND_DATA:
1874  return send_error (auth, "Sent DATA while expecting BEGIN");
1875 
1876  case DBUS_AUTH_COMMAND_BEGIN:
1877  goto_state (auth, &common_state_authenticated);
1878  return TRUE;
1879 
1880  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1881  if (auth->unix_fd_possible)
1882  return send_agree_unix_fd(auth);
1883  else
1884  return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1885 
1886  case DBUS_AUTH_COMMAND_REJECTED:
1887  case DBUS_AUTH_COMMAND_OK:
1888  case DBUS_AUTH_COMMAND_UNKNOWN:
1889  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1890  default:
1891  return send_error (auth, "Unknown command");
1892 
1893  case DBUS_AUTH_COMMAND_CANCEL:
1894  case DBUS_AUTH_COMMAND_ERROR:
1895  return send_rejected (auth);
1896  }
1897 }
1898 
1899 /* return FALSE if no memory, TRUE if all OK */
1900 static dbus_bool_t
1901 get_word (const DBusString *str,
1902  int *start,
1903  DBusString *word)
1904 {
1905  int i;
1906 
1907  _dbus_string_skip_blank (str, *start, start);
1908  _dbus_string_find_blank (str, *start, &i);
1909 
1910  if (i > *start)
1911  {
1912  if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1913  return FALSE;
1914 
1915  *start = i;
1916  }
1917 
1918  return TRUE;
1919 }
1920 
1921 static dbus_bool_t
1922 record_mechanisms (DBusAuth *auth,
1923  const DBusString *args)
1924 {
1925  int next;
1926  int len;
1927 
1928  if (auth->already_got_mechanisms)
1929  return TRUE;
1930 
1931  len = _dbus_string_get_length (args);
1932 
1933  next = 0;
1934  while (next < len)
1935  {
1936  DBusString m;
1938 
1939  if (!_dbus_string_init (&m))
1940  goto nomem;
1941 
1942  if (!get_word (args, &next, &m))
1943  {
1944  _dbus_string_free (&m);
1945  goto nomem;
1946  }
1947 
1948  mech = find_mech (&m, auth->allowed_mechs);
1949 
1950  if (mech != NULL)
1951  {
1952  /* FIXME right now we try mechanisms in the order
1953  * the server lists them; should we do them in
1954  * some more deterministic order?
1955  *
1956  * Probably in all_mechanisms order, our order of
1957  * preference. Of course when the server is us,
1958  * it lists things in that order anyhow.
1959  */
1960 
1961  if (mech != &all_mechanisms[0])
1962  {
1963  _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1964  DBUS_AUTH_NAME (auth), mech->mechanism);
1965 
1966  if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1967  (void*) mech))
1968  {
1969  _dbus_string_free (&m);
1970  goto nomem;
1971  }
1972  }
1973  else
1974  {
1975  _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1976  DBUS_AUTH_NAME (auth), mech->mechanism);
1977  }
1978  }
1979  else
1980  {
1981  _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1982  DBUS_AUTH_NAME (auth),
1983  _dbus_string_get_const_data (&m));
1984  }
1985 
1986  _dbus_string_free (&m);
1987  }
1988 
1989  auth->already_got_mechanisms = TRUE;
1990 
1991  return TRUE;
1992 
1993  nomem:
1994  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1995 
1996  return FALSE;
1997 }
1998 
1999 static dbus_bool_t
2000 process_rejected (DBusAuth *auth, const DBusString *args)
2001 {
2003  DBusAuthClient *client;
2004 
2005  client = DBUS_AUTH_CLIENT (auth);
2006 
2007  if (!auth->already_got_mechanisms)
2008  {
2009  if (!record_mechanisms (auth, args))
2010  return FALSE;
2011  }
2012 
2013  if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
2014  {
2015  mech = client->mechs_to_try->data;
2016 
2017  if (!send_auth (auth, mech))
2018  return FALSE;
2019 
2021 
2022  _dbus_verbose ("%s: Trying mechanism %s\n",
2023  DBUS_AUTH_NAME (auth),
2024  mech->mechanism);
2025  }
2026  else
2027  {
2028  /* Give up */
2029  _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
2030  DBUS_AUTH_NAME (auth));
2031  goto_state (auth, &common_state_need_disconnect);
2032  }
2033 
2034  return TRUE;
2035 }
2036 
2037 
2038 static dbus_bool_t
2039 handle_client_state_waiting_for_data (DBusAuth *auth,
2040  DBusAuthCommand command,
2041  const DBusString *args)
2042 {
2043  _dbus_assert (auth->mech != NULL);
2044 
2045  switch (command)
2046  {
2047  case DBUS_AUTH_COMMAND_DATA:
2048  return process_data (auth, args, auth->mech->client_data_func);
2049 
2050  case DBUS_AUTH_COMMAND_REJECTED:
2051  return process_rejected (auth, args);
2052 
2053  case DBUS_AUTH_COMMAND_OK:
2054  return process_ok(auth, args);
2055 
2056  case DBUS_AUTH_COMMAND_ERROR:
2057  return send_cancel (auth);
2058 
2059  case DBUS_AUTH_COMMAND_AUTH:
2060  case DBUS_AUTH_COMMAND_CANCEL:
2061  case DBUS_AUTH_COMMAND_BEGIN:
2062  case DBUS_AUTH_COMMAND_UNKNOWN:
2063  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2064  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2065  default:
2066  return send_error (auth, "Unknown command");
2067  }
2068 }
2069 
2070 static dbus_bool_t
2071 handle_client_state_waiting_for_ok (DBusAuth *auth,
2072  DBusAuthCommand command,
2073  const DBusString *args)
2074 {
2075  switch (command)
2076  {
2077  case DBUS_AUTH_COMMAND_REJECTED:
2078  return process_rejected (auth, args);
2079 
2080  case DBUS_AUTH_COMMAND_OK:
2081  return process_ok(auth, args);
2082 
2083  case DBUS_AUTH_COMMAND_DATA:
2084  case DBUS_AUTH_COMMAND_ERROR:
2085  return send_cancel (auth);
2086 
2087  case DBUS_AUTH_COMMAND_AUTH:
2088  case DBUS_AUTH_COMMAND_CANCEL:
2089  case DBUS_AUTH_COMMAND_BEGIN:
2090  case DBUS_AUTH_COMMAND_UNKNOWN:
2091  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2092  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2093  default:
2094  return send_error (auth, "Unknown command");
2095  }
2096 }
2097 
2098 static dbus_bool_t
2099 handle_client_state_waiting_for_reject (DBusAuth *auth,
2100  DBusAuthCommand command,
2101  const DBusString *args)
2102 {
2103  switch (command)
2104  {
2105  case DBUS_AUTH_COMMAND_REJECTED:
2106  return process_rejected (auth, args);
2107 
2108  case DBUS_AUTH_COMMAND_AUTH:
2109  case DBUS_AUTH_COMMAND_CANCEL:
2110  case DBUS_AUTH_COMMAND_DATA:
2111  case DBUS_AUTH_COMMAND_BEGIN:
2112  case DBUS_AUTH_COMMAND_OK:
2113  case DBUS_AUTH_COMMAND_ERROR:
2114  case DBUS_AUTH_COMMAND_UNKNOWN:
2115  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2116  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2117  default:
2118  goto_state (auth, &common_state_need_disconnect);
2119  return TRUE;
2120  }
2121 }
2122 
2123 static dbus_bool_t
2124 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2125  DBusAuthCommand command,
2126  const DBusString *args)
2127 {
2128  switch (command)
2129  {
2130  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2132  auth->unix_fd_negotiated = TRUE;
2133  _dbus_verbose("Successfully negotiated UNIX FD passing\n");
2134  return send_begin (auth);
2135 
2136  case DBUS_AUTH_COMMAND_ERROR:
2138  auth->unix_fd_negotiated = FALSE;
2139  _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2140  return send_begin (auth);
2141 
2142  case DBUS_AUTH_COMMAND_OK:
2143  case DBUS_AUTH_COMMAND_DATA:
2144  case DBUS_AUTH_COMMAND_REJECTED:
2145  case DBUS_AUTH_COMMAND_AUTH:
2146  case DBUS_AUTH_COMMAND_CANCEL:
2147  case DBUS_AUTH_COMMAND_BEGIN:
2148  case DBUS_AUTH_COMMAND_UNKNOWN:
2149  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2150  default:
2151  return send_error (auth, "Unknown command");
2152  }
2153 }
2154 
2158 typedef struct {
2159  const char *name;
2162 
2163 static const DBusAuthCommandName auth_command_names[] = {
2164  { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2165  { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2166  { "DATA", DBUS_AUTH_COMMAND_DATA },
2167  { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2168  { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2169  { "OK", DBUS_AUTH_COMMAND_OK },
2170  { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2171  { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2172  { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2173 };
2174 
2175 static DBusAuthCommand
2176 lookup_command_from_name (DBusString *command)
2177 {
2178  int i;
2179 
2180  for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2181  {
2182  if (_dbus_string_equal_c_str (command,
2183  auth_command_names[i].name))
2184  return auth_command_names[i].command;
2185  }
2186 
2187  return DBUS_AUTH_COMMAND_UNKNOWN;
2188 }
2189 
2190 static void
2191 goto_state (DBusAuth *auth,
2192  const DBusAuthStateData *state)
2193 {
2194  _dbus_verbose ("%s: going from state %s to state %s\n",
2195  DBUS_AUTH_NAME (auth),
2196  auth->state->name,
2197  state->name);
2198 
2199  auth->state = state;
2200 }
2201 
2202 /* returns whether to call it again right away */
2203 static dbus_bool_t
2204 process_command (DBusAuth *auth)
2205 {
2206  DBusAuthCommand command;
2207  DBusString line;
2208  DBusString args;
2209  int eol;
2210  int i, j;
2211  dbus_bool_t retval;
2212 
2213  /* _dbus_verbose ("%s: trying process_command()\n"); */
2214 
2215  retval = FALSE;
2216 
2217  eol = 0;
2218  if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2219  return FALSE;
2220 
2221  if (!_dbus_string_init (&line))
2222  {
2223  auth->needed_memory = TRUE;
2224  return FALSE;
2225  }
2226 
2227  if (!_dbus_string_init (&args))
2228  {
2229  _dbus_string_free (&line);
2230  auth->needed_memory = TRUE;
2231  return FALSE;
2232  }
2233 
2234  if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2235  goto out;
2236 
2237  if (!_dbus_string_validate_ascii (&line, 0,
2238  _dbus_string_get_length (&line)))
2239  {
2240  _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2241  DBUS_AUTH_NAME (auth));
2242  if (!send_error (auth, "Command contained non-ASCII"))
2243  goto out;
2244  else
2245  goto next_command;
2246  }
2247 
2248  _dbus_verbose ("%s: got command \"%s\"\n",
2249  DBUS_AUTH_NAME (auth),
2250  _dbus_string_get_const_data (&line));
2251 
2252  _dbus_string_find_blank (&line, 0, &i);
2253  _dbus_string_skip_blank (&line, i, &j);
2254 
2255  if (j > i)
2256  _dbus_string_delete (&line, i, j - i);
2257 
2258  if (!_dbus_string_move (&line, i, &args, 0))
2259  goto out;
2260 
2261  /* FIXME 1.0 we should probably validate that only the allowed
2262  * chars are in the command name
2263  */
2264 
2265  command = lookup_command_from_name (&line);
2266  if (!(* auth->state->handler) (auth, command, &args))
2267  goto out;
2268 
2269  next_command:
2270 
2271  /* We've succeeded in processing the whole command so drop it out
2272  * of the incoming buffer and return TRUE to try another command.
2273  */
2274 
2275  _dbus_string_delete (&auth->incoming, 0, eol);
2276 
2277  /* kill the \r\n */
2278  _dbus_string_delete (&auth->incoming, 0, 2);
2279 
2280  retval = TRUE;
2281 
2282  out:
2283  _dbus_string_free (&args);
2284  _dbus_string_free (&line);
2285 
2286  if (!retval)
2287  auth->needed_memory = TRUE;
2288  else
2289  auth->needed_memory = FALSE;
2290 
2291  return retval;
2292 }
2293 
2294 
2309 DBusAuth*
2311 {
2312  DBusAuth *auth;
2313  DBusAuthServer *server_auth;
2314  DBusString guid_copy;
2315 
2316  if (!_dbus_string_init (&guid_copy))
2317  return NULL;
2318 
2319  if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2320  {
2321  _dbus_string_free (&guid_copy);
2322  return NULL;
2323  }
2324 
2325  auth = _dbus_auth_new (sizeof (DBusAuthServer));
2326  if (auth == NULL)
2327  {
2328  _dbus_string_free (&guid_copy);
2329  return NULL;
2330  }
2331 
2332  auth->side = auth_side_server;
2333  auth->state = &server_state_waiting_for_auth;
2334 
2335  server_auth = DBUS_AUTH_SERVER (auth);
2336 
2337  server_auth->guid = guid_copy;
2338 
2339  /* perhaps this should be per-mechanism with a lower
2340  * max
2341  */
2342  server_auth->failures = 0;
2343  server_auth->max_failures = 6;
2344 
2345  return auth;
2346 }
2347 
2355 DBusAuth*
2357 {
2358  DBusAuth *auth;
2359  DBusString guid_str;
2360 
2361  if (!_dbus_string_init (&guid_str))
2362  return NULL;
2363 
2364  auth = _dbus_auth_new (sizeof (DBusAuthClient));
2365  if (auth == NULL)
2366  {
2367  _dbus_string_free (&guid_str);
2368  return NULL;
2369  }
2370 
2371  DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2372 
2373  auth->side = auth_side_client;
2374  auth->state = &client_state_need_send_auth;
2375 
2376  /* Start the auth conversation by sending AUTH for our default
2377  * mechanism */
2378  if (!send_auth (auth, &all_mechanisms[0]))
2379  {
2380  _dbus_auth_unref (auth);
2381  return NULL;
2382  }
2383 
2384  return auth;
2385 }
2386 
2393 DBusAuth *
2395 {
2396  _dbus_assert (auth != NULL);
2397 
2398  auth->refcount += 1;
2399 
2400  return auth;
2401 }
2402 
2408 void
2410 {
2411  _dbus_assert (auth != NULL);
2412  _dbus_assert (auth->refcount > 0);
2413 
2414  auth->refcount -= 1;
2415  if (auth->refcount == 0)
2416  {
2417  shutdown_mech (auth);
2418 
2419  if (DBUS_AUTH_IS_CLIENT (auth))
2420  {
2421  _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2422  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2423  }
2424  else
2425  {
2427 
2428  _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2429  }
2430 
2431  if (auth->keyring)
2432  _dbus_keyring_unref (auth->keyring);
2433 
2434  _dbus_string_free (&auth->context);
2435  _dbus_string_free (&auth->challenge);
2436  _dbus_string_free (&auth->identity);
2437  _dbus_string_free (&auth->incoming);
2438  _dbus_string_free (&auth->outgoing);
2439 
2441 
2445 
2446  dbus_free (auth);
2447  }
2448 }
2449 
2460  const char **mechanisms)
2461 {
2462  char **copy;
2463 
2464  if (mechanisms != NULL)
2465  {
2466  copy = _dbus_dup_string_array (mechanisms);
2467  if (copy == NULL)
2468  return FALSE;
2469  }
2470  else
2471  copy = NULL;
2472 
2474 
2475  auth->allowed_mechs = copy;
2476 
2477  return TRUE;
2478 }
2479 
2484 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2485 
2493 DBusAuthState
2495 {
2496  auth->needed_memory = FALSE;
2497 
2498  /* Max amount we'll buffer up before deciding someone's on crack */
2499 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2500 
2501  do
2502  {
2503  if (DBUS_AUTH_IN_END_STATE (auth))
2504  break;
2505 
2506  if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2507  _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2508  {
2509  goto_state (auth, &common_state_need_disconnect);
2510  _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2511  DBUS_AUTH_NAME (auth));
2512  break;
2513  }
2514  }
2515  while (process_command (auth));
2516 
2517  if (auth->needed_memory)
2518  return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2519  else if (_dbus_string_get_length (&auth->outgoing) > 0)
2520  return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2521  else if (auth->state == &common_state_need_disconnect)
2522  return DBUS_AUTH_STATE_NEED_DISCONNECT;
2523  else if (auth->state == &common_state_authenticated)
2524  return DBUS_AUTH_STATE_AUTHENTICATED;
2525  else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2526 }
2527 
2539  const DBusString **str)
2540 {
2541  _dbus_assert (auth != NULL);
2542  _dbus_assert (str != NULL);
2543 
2544  *str = NULL;
2545 
2546  if (_dbus_string_get_length (&auth->outgoing) == 0)
2547  return FALSE;
2548 
2549  *str = &auth->outgoing;
2550 
2551  return TRUE;
2552 }
2553 
2562 void
2564  int bytes_sent)
2565 {
2566  _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2567  DBUS_AUTH_NAME (auth),
2568  bytes_sent,
2569  _dbus_string_get_const_data (&auth->outgoing));
2570 
2571  _dbus_string_delete (&auth->outgoing,
2572  0, bytes_sent);
2573 }
2574 
2582 void
2584  DBusString **buffer)
2585 {
2586  _dbus_assert (auth != NULL);
2588 
2589  *buffer = &auth->incoming;
2590 
2591  auth->buffer_outstanding = TRUE;
2592 }
2593 
2600 void
2602  DBusString *buffer)
2603 {
2604  _dbus_assert (buffer == &auth->incoming);
2606 
2607  auth->buffer_outstanding = FALSE;
2608 }
2609 
2619 void
2621  const DBusString **str)
2622 {
2623  if (!DBUS_AUTH_IN_END_STATE (auth))
2624  return;
2625 
2626  *str = &auth->incoming;
2627 }
2628 
2629 
2636 void
2638 {
2639  if (!DBUS_AUTH_IN_END_STATE (auth))
2640  return;
2641 
2642  _dbus_string_set_length (&auth->incoming, 0);
2643 }
2644 
2655 {
2656  if (auth->state != &common_state_authenticated)
2657  return FALSE;
2658 
2659  if (auth->mech != NULL)
2660  {
2661  if (DBUS_AUTH_IS_CLIENT (auth))
2662  return auth->mech->client_encode_func != NULL;
2663  else
2664  return auth->mech->server_encode_func != NULL;
2665  }
2666  else
2667  return FALSE;
2668 }
2669 
2682  const DBusString *plaintext,
2683  DBusString *encoded)
2684 {
2685  _dbus_assert (plaintext != encoded);
2686 
2687  if (auth->state != &common_state_authenticated)
2688  return FALSE;
2689 
2690  if (_dbus_auth_needs_encoding (auth))
2691  {
2692  if (DBUS_AUTH_IS_CLIENT (auth))
2693  return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2694  else
2695  return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2696  }
2697  else
2698  {
2699  return _dbus_string_copy (plaintext, 0, encoded,
2700  _dbus_string_get_length (encoded));
2701  }
2702 }
2703 
2714 {
2715  if (auth->state != &common_state_authenticated)
2716  return FALSE;
2717 
2718  if (auth->mech != NULL)
2719  {
2720  if (DBUS_AUTH_IS_CLIENT (auth))
2721  return auth->mech->client_decode_func != NULL;
2722  else
2723  return auth->mech->server_decode_func != NULL;
2724  }
2725  else
2726  return FALSE;
2727 }
2728 
2729 
2745  const DBusString *encoded,
2746  DBusString *plaintext)
2747 {
2748  _dbus_assert (plaintext != encoded);
2749 
2750  if (auth->state != &common_state_authenticated)
2751  return FALSE;
2752 
2753  if (_dbus_auth_needs_decoding (auth))
2754  {
2755  if (DBUS_AUTH_IS_CLIENT (auth))
2756  return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2757  else
2758  return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2759  }
2760  else
2761  {
2762  return _dbus_string_copy (encoded, 0, plaintext,
2763  _dbus_string_get_length (plaintext));
2764  }
2765 }
2766 
2778 {
2781  credentials);
2782 }
2783 
2795 {
2796  if (auth->state == &common_state_authenticated)
2797  {
2798  return auth->authorized_identity;
2799  }
2800  else
2801  {
2802  /* FIXME instead of this, keep an empty credential around that
2803  * doesn't require allocation or something
2804  */
2805  /* return empty credentials */
2807  return auth->authorized_identity;
2808  }
2809 }
2810 
2817 const char*
2819 {
2821 
2822  if (auth->state == &common_state_authenticated)
2823  return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2824  else
2825  return NULL;
2826 }
2827 
2838  const DBusString *context)
2839 {
2840  return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2841  &auth->context, 0, _dbus_string_get_length (context));
2842 }
2843 
2851 void
2853 {
2854  auth->unix_fd_possible = b;
2855 }
2856 
2865 {
2866  return auth->unix_fd_negotiated;
2867 }
2868 
2871 /* 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:2637
#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:2620
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
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:2713
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 "checked out" 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:2681
Internals of DBusKeyring.
Definition: dbus-keyring.c:111
dbus_bool_t _dbus_auth_set_context(DBusAuth *auth, const DBusString *context)
Sets the "authentication context" which scopes cookies with the DBUS_COOKIE_SHA1 auth mechanism for e...
Definition: dbus-auth.c:2837
#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:2601
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:2494
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:2459
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(* DBusAuthDataFunction)(DBusAuth *auth, const DBusString *data)
This function processes a block of data received from the peer.
Definition: dbus-auth.c:77
dbus_bool_t _dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
Queries whether unix fd passing was successfully negotiated.
Definition: dbus-auth.c:2864
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:2794
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:2583
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:2744
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:2563
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
"Subclass" 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
"Subclass" 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
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with credentials (user ID and process ID) from the current process.
DBusAuth * _dbus_auth_server_new(const DBusString *guid)
Creates a new auth conversation object for the server side.
Definition: dbus-auth.c:2310
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 ("client" or "server")
Definition: dbus-auth.c:335
DBusAuth * _dbus_auth_ref(DBusAuth *auth)
Increments the refcount of an auth object.
Definition: dbus-auth.c:2394
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(* 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_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
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
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 _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:2818
#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:2159
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
#define DBUS_AUTH_CLIENT(auth)
Definition: dbus-auth.c:323
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 "1".
int failures
Number of times client has been rejected.
Definition: dbus-auth.c:216
#define DBUS_AUTH_IS_SERVER(auth)
Definition: dbus-auth.c:313
void(* DBusAuthShutdownFunction)(DBusAuth *auth)
This function is called when the mechanism is abandoned.
Definition: dbus-auth.c:97
#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:516
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:2776
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:2852
const char * name
Name of the state.
Definition: dbus-auth.c:146
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(* 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_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:2409
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:2538
Mapping from command name to enum.
Definition: dbus-auth.c:2158
Virtual table representing a particular auth mechanism.
Definition: dbus-auth.c:102
#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 "0".
const DBusAuthMechanismHandler * mech
Current auth mechanism.
Definition: dbus-auth.c:163
#define DBUS_AUTH_SERVER(auth)
Definition: dbus-auth.c:328
unsigned int unix_fd_possible
This side could do unix fd passing.
Definition: dbus-auth.c:192
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
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:2654
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:2356
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 _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:2484
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:318
DBusAuthCommand command
Corresponding enum.
Definition: dbus-auth.c:2160
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.