D-Bus  1.8.22
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 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 
467  if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
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;
528  DBusError error;
529  DBusCredentials *myself;
530 
531  retval = FALSE;
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 
569 
570  if (myself == NULL)
571  goto out;
572 
573  if (!_dbus_credentials_same_user (myself, auth->desired_identity))
574  {
575  /*
576  * DBUS_COOKIE_SHA1 is not suitable for authenticating that the
577  * client is anyone other than the user owning the process
578  * containing the DBusServer: we probably aren't allowed to write
579  * to other users' home directories. Even if we can (for example
580  * uid 0 on traditional Unix or CAP_DAC_OVERRIDE on Linux), we
581  * must not, because the other user controls their home directory,
582  * and could carry out symlink attacks to make us read from or
583  * write to unintended locations. It's difficult to avoid symlink
584  * attacks in a portable way, so we just don't try. This isn't a
585  * regression, because DBUS_COOKIE_SHA1 never worked for other
586  * users anyway.
587  */
588  _dbus_verbose ("%s: client tried to authenticate as \"%s\", "
589  "but that doesn't match this process",
590  DBUS_AUTH_NAME (auth),
591  _dbus_string_get_const_data (data));
592  retval = send_rejected (auth);
593  goto out;
594  }
595 
596  /* we cache the keyring for speed, so here we drop it if it's the
597  * wrong one. FIXME caching the keyring here is useless since we use
598  * a different DBusAuth for every connection.
599  */
600  if (auth->keyring &&
602  auth->desired_identity))
603  {
605  auth->keyring = NULL;
606  }
607 
608  if (auth->keyring == NULL)
609  {
610  dbus_error_init (&error);
612  &auth->context,
613  &error);
614 
615  if (auth->keyring == NULL)
616  {
617  if (dbus_error_has_name (&error,
619  {
620  dbus_error_free (&error);
621  goto out;
622  }
623  else
624  {
625  _DBUS_ASSERT_ERROR_IS_SET (&error);
626  _dbus_verbose ("%s: Error loading keyring: %s\n",
627  DBUS_AUTH_NAME (auth), error.message);
628  if (send_rejected (auth))
629  retval = TRUE; /* retval is only about mem */
630  dbus_error_free (&error);
631  goto out;
632  }
633  }
634  else
635  {
636  _dbus_assert (!dbus_error_is_set (&error));
637  }
638  }
639 
640  _dbus_assert (auth->keyring != NULL);
641 
642  dbus_error_init (&error);
643  auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
644  if (auth->cookie_id < 0)
645  {
646  _DBUS_ASSERT_ERROR_IS_SET (&error);
647  _dbus_verbose ("%s: Could not get a cookie ID to send to client: %s\n",
648  DBUS_AUTH_NAME (auth), error.message);
649  if (send_rejected (auth))
650  retval = TRUE;
651  dbus_error_free (&error);
652  goto out;
653  }
654  else
655  {
656  _dbus_assert (!dbus_error_is_set (&error));
657  }
658 
659  if (!_dbus_string_copy (&auth->context, 0,
660  &tmp2, _dbus_string_get_length (&tmp2)))
661  goto out;
662 
663  if (!_dbus_string_append (&tmp2, " "))
664  goto out;
665 
666  if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
667  goto out;
668 
669  if (!_dbus_string_append (&tmp2, " "))
670  goto out;
671 
673  goto out;
674 
676  if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
677  goto out;
678 
679  if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
680  _dbus_string_get_length (&tmp2)))
681  goto out;
682 
683  if (!send_data (auth, &tmp2))
684  goto out;
685 
686  goto_state (auth, &server_state_waiting_for_data);
687  retval = TRUE;
688 
689  out:
690  _dbus_string_zero (&tmp);
691  _dbus_string_free (&tmp);
692  _dbus_string_zero (&tmp2);
693  _dbus_string_free (&tmp2);
694 
695  if (myself != NULL)
696  _dbus_credentials_unref (myself);
697 
698  return retval;
699 }
700 
701 static dbus_bool_t
702 sha1_handle_second_client_response (DBusAuth *auth,
703  const DBusString *data)
704 {
705  /* We are expecting a response which is the hex-encoded client
706  * challenge, space, then SHA-1 hash of the concatenation of our
707  * challenge, ":", client challenge, ":", secret key, all
708  * hex-encoded.
709  */
710  int i;
711  DBusString client_challenge;
712  DBusString client_hash;
713  dbus_bool_t retval;
714  DBusString correct_hash;
715 
716  retval = FALSE;
717 
718  if (!_dbus_string_find_blank (data, 0, &i))
719  {
720  _dbus_verbose ("%s: no space separator in client response\n",
721  DBUS_AUTH_NAME (auth));
722  return send_rejected (auth);
723  }
724 
725  if (!_dbus_string_init (&client_challenge))
726  goto out_0;
727 
728  if (!_dbus_string_init (&client_hash))
729  goto out_1;
730 
731  if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
732  0))
733  goto out_2;
734 
735  _dbus_string_skip_blank (data, i, &i);
736 
737  if (!_dbus_string_copy_len (data, i,
738  _dbus_string_get_length (data) - i,
739  &client_hash,
740  0))
741  goto out_2;
742 
743  if (_dbus_string_get_length (&client_challenge) == 0 ||
744  _dbus_string_get_length (&client_hash) == 0)
745  {
746  _dbus_verbose ("%s: zero-length client challenge or hash\n",
747  DBUS_AUTH_NAME (auth));
748  if (send_rejected (auth))
749  retval = TRUE;
750  goto out_2;
751  }
752 
753  if (!_dbus_string_init (&correct_hash))
754  goto out_2;
755 
756  if (!sha1_compute_hash (auth, auth->cookie_id,
757  &auth->challenge,
758  &client_challenge,
759  &correct_hash))
760  goto out_3;
761 
762  /* if cookie_id was invalid, then we get an empty hash */
763  if (_dbus_string_get_length (&correct_hash) == 0)
764  {
765  if (send_rejected (auth))
766  retval = TRUE;
767  goto out_3;
768  }
769 
770  if (!_dbus_string_equal (&client_hash, &correct_hash))
771  {
772  if (send_rejected (auth))
773  retval = TRUE;
774  goto out_3;
775  }
776 
778  auth->desired_identity))
779  goto out_3;
780 
781  /* Copy process ID from the socket credentials if it's there
782  */
784  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
785  auth->credentials))
786  goto out_3;
787 
788  if (!send_ok (auth))
789  goto out_3;
790 
791  _dbus_verbose ("%s: authenticated client using DBUS_COOKIE_SHA1\n",
792  DBUS_AUTH_NAME (auth));
793 
794  retval = TRUE;
795 
796  out_3:
797  _dbus_string_zero (&correct_hash);
798  _dbus_string_free (&correct_hash);
799  out_2:
800  _dbus_string_zero (&client_hash);
801  _dbus_string_free (&client_hash);
802  out_1:
803  _dbus_string_free (&client_challenge);
804  out_0:
805  return retval;
806 }
807 
808 static dbus_bool_t
809 handle_server_data_cookie_sha1_mech (DBusAuth *auth,
810  const DBusString *data)
811 {
812  if (auth->cookie_id < 0)
813  return sha1_handle_first_client_response (auth, data);
814  else
815  return sha1_handle_second_client_response (auth, data);
816 }
817 
818 static void
819 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
820 {
821  auth->cookie_id = -1;
823 }
824 
825 static dbus_bool_t
826 handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
827  DBusString *response)
828 {
829  DBusString username;
830  dbus_bool_t retval;
831 
832  retval = FALSE;
833 
834  if (!_dbus_string_init (&username))
835  return FALSE;
836 
838  goto out_0;
839 
840  if (!_dbus_string_hex_encode (&username, 0,
841  response,
842  _dbus_string_get_length (response)))
843  goto out_0;
844 
845  retval = TRUE;
846 
847  out_0:
848  _dbus_string_free (&username);
849 
850  return retval;
851 }
852 
853 static dbus_bool_t
854 handle_client_data_cookie_sha1_mech (DBusAuth *auth,
855  const DBusString *data)
856 {
857  /* The data we get from the server should be the cookie context
858  * name, the cookie ID, and the server challenge, separated by
859  * spaces. We send back our challenge string and the correct hash.
860  */
861  dbus_bool_t retval;
862  DBusString context;
863  DBusString cookie_id_str;
864  DBusString server_challenge;
865  DBusString client_challenge;
866  DBusString correct_hash;
867  DBusString tmp;
868  int i, j;
869  long val;
870 
871  retval = FALSE;
872 
873  if (!_dbus_string_find_blank (data, 0, &i))
874  {
875  if (send_error (auth,
876  "Server did not send context/ID/challenge properly"))
877  retval = TRUE;
878  goto out_0;
879  }
880 
881  if (!_dbus_string_init (&context))
882  goto out_0;
883 
884  if (!_dbus_string_copy_len (data, 0, i,
885  &context, 0))
886  goto out_1;
887 
888  _dbus_string_skip_blank (data, i, &i);
889  if (!_dbus_string_find_blank (data, i, &j))
890  {
891  if (send_error (auth,
892  "Server did not send context/ID/challenge properly"))
893  retval = TRUE;
894  goto out_1;
895  }
896 
897  if (!_dbus_string_init (&cookie_id_str))
898  goto out_1;
899 
900  if (!_dbus_string_copy_len (data, i, j - i,
901  &cookie_id_str, 0))
902  goto out_2;
903 
904  if (!_dbus_string_init (&server_challenge))
905  goto out_2;
906 
907  i = j;
908  _dbus_string_skip_blank (data, i, &i);
909  j = _dbus_string_get_length (data);
910 
911  if (!_dbus_string_copy_len (data, i, j - i,
912  &server_challenge, 0))
913  goto out_3;
914 
915  if (!_dbus_keyring_validate_context (&context))
916  {
917  if (send_error (auth, "Server sent invalid cookie context"))
918  retval = TRUE;
919  goto out_3;
920  }
921 
922  if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
923  {
924  if (send_error (auth, "Could not parse cookie ID as an integer"))
925  retval = TRUE;
926  goto out_3;
927  }
928 
929  if (_dbus_string_get_length (&server_challenge) == 0)
930  {
931  if (send_error (auth, "Empty server challenge string"))
932  retval = TRUE;
933  goto out_3;
934  }
935 
936  if (auth->keyring == NULL)
937  {
938  DBusError error;
939 
940  dbus_error_init (&error);
942  &context,
943  &error);
944 
945  if (auth->keyring == NULL)
946  {
947  if (dbus_error_has_name (&error,
949  {
950  dbus_error_free (&error);
951  goto out_3;
952  }
953  else
954  {
955  _DBUS_ASSERT_ERROR_IS_SET (&error);
956 
957  _dbus_verbose ("%s: Error loading keyring: %s\n",
958  DBUS_AUTH_NAME (auth), error.message);
959 
960  if (send_error (auth, "Could not load cookie file"))
961  retval = TRUE; /* retval is only about mem */
962 
963  dbus_error_free (&error);
964  goto out_3;
965  }
966  }
967  else
968  {
969  _dbus_assert (!dbus_error_is_set (&error));
970  }
971  }
972 
973  _dbus_assert (auth->keyring != NULL);
974 
975  if (!_dbus_string_init (&tmp))
976  goto out_3;
977 
979  goto out_4;
980 
981  if (!_dbus_string_init (&client_challenge))
982  goto out_4;
983 
984  if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
985  goto out_5;
986 
987  if (!_dbus_string_init (&correct_hash))
988  goto out_5;
989 
990  if (!sha1_compute_hash (auth, val,
991  &server_challenge,
992  &client_challenge,
993  &correct_hash))
994  goto out_6;
995 
996  if (_dbus_string_get_length (&correct_hash) == 0)
997  {
998  /* couldn't find the cookie ID or something */
999  if (send_error (auth, "Don't have the requested cookie ID"))
1000  retval = TRUE;
1001  goto out_6;
1002  }
1003 
1004  _dbus_string_set_length (&tmp, 0);
1005 
1006  if (!_dbus_string_copy (&client_challenge, 0, &tmp,
1007  _dbus_string_get_length (&tmp)))
1008  goto out_6;
1009 
1010  if (!_dbus_string_append (&tmp, " "))
1011  goto out_6;
1012 
1013  if (!_dbus_string_copy (&correct_hash, 0, &tmp,
1014  _dbus_string_get_length (&tmp)))
1015  goto out_6;
1016 
1017  if (!send_data (auth, &tmp))
1018  goto out_6;
1019 
1020  retval = TRUE;
1021 
1022  out_6:
1023  _dbus_string_zero (&correct_hash);
1024  _dbus_string_free (&correct_hash);
1025  out_5:
1026  _dbus_string_free (&client_challenge);
1027  out_4:
1028  _dbus_string_zero (&tmp);
1029  _dbus_string_free (&tmp);
1030  out_3:
1031  _dbus_string_free (&server_challenge);
1032  out_2:
1033  _dbus_string_free (&cookie_id_str);
1034  out_1:
1035  _dbus_string_free (&context);
1036  out_0:
1037  return retval;
1038 }
1039 
1040 static void
1041 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
1042 {
1043  auth->cookie_id = -1;
1044  _dbus_string_set_length (&auth->challenge, 0);
1045 }
1046 
1047 /*
1048  * EXTERNAL mechanism
1049  */
1050 
1051 static dbus_bool_t
1052 handle_server_data_external_mech (DBusAuth *auth,
1053  const DBusString *data)
1054 {
1056  {
1057  _dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
1058  DBUS_AUTH_NAME (auth));
1059  return send_rejected (auth);
1060  }
1061 
1062  if (_dbus_string_get_length (data) > 0)
1063  {
1064  if (_dbus_string_get_length (&auth->identity) > 0)
1065  {
1066  /* Tried to send two auth identities, wtf */
1067  _dbus_verbose ("%s: client tried to send auth identity, but we already have one\n",
1068  DBUS_AUTH_NAME (auth));
1069  return send_rejected (auth);
1070  }
1071  else
1072  {
1073  /* this is our auth identity */
1074  if (!_dbus_string_copy (data, 0, &auth->identity, 0))
1075  return FALSE;
1076  }
1077  }
1078 
1079  /* Poke client for an auth identity, if none given */
1080  if (_dbus_string_get_length (&auth->identity) == 0 &&
1082  {
1083  if (send_data (auth, NULL))
1084  {
1085  _dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
1086  DBUS_AUTH_NAME (auth));
1088  goto_state (auth, &server_state_waiting_for_data);
1089  return TRUE;
1090  }
1091  else
1092  return FALSE;
1093  }
1094 
1096 
1097  /* If auth->identity is still empty here, then client
1098  * responded with an empty string after we poked it for
1099  * an initial response. This means to try to auth the
1100  * identity provided in the credentials.
1101  */
1102  if (_dbus_string_get_length (&auth->identity) == 0)
1103  {
1105  auth->credentials))
1106  {
1107  return FALSE; /* OOM */
1108  }
1109  }
1110  else
1111  {
1113  &auth->identity))
1114  {
1115  _dbus_verbose ("%s: could not get credentials from uid string\n",
1116  DBUS_AUTH_NAME (auth));
1117  return send_rejected (auth);
1118  }
1119  }
1120 
1122  {
1123  _dbus_verbose ("%s: desired user %s is no good\n",
1124  DBUS_AUTH_NAME (auth),
1125  _dbus_string_get_const_data (&auth->identity));
1126  return send_rejected (auth);
1127  }
1128 
1130  auth->desired_identity))
1131  {
1132  /* client has authenticated */
1134  auth->desired_identity))
1135  return FALSE;
1136 
1137  /* also copy process ID from the socket credentials
1138  */
1140  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1141  auth->credentials))
1142  return FALSE;
1143 
1144  /* also copy audit data from the socket credentials
1145  */
1147  DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
1148  auth->credentials))
1149  return FALSE;
1150 
1151  if (!send_ok (auth))
1152  return FALSE;
1153 
1154  _dbus_verbose ("%s: authenticated client based on socket credentials\n",
1155  DBUS_AUTH_NAME (auth));
1156 
1157  return TRUE;
1158  }
1159  else
1160  {
1161  _dbus_verbose ("%s: desired identity not found in socket credentials\n",
1162  DBUS_AUTH_NAME (auth));
1163  return send_rejected (auth);
1164  }
1165 }
1166 
1167 static void
1168 handle_server_shutdown_external_mech (DBusAuth *auth)
1169 {
1170 
1171 }
1172 
1173 static dbus_bool_t
1174 handle_client_initial_response_external_mech (DBusAuth *auth,
1175  DBusString *response)
1176 {
1177  /* We always append our UID as an initial response, so the server
1178  * doesn't have to send back an empty challenge to check whether we
1179  * want to specify an identity. i.e. this avoids a round trip that
1180  * the spec for the EXTERNAL mechanism otherwise requires.
1181  */
1182  DBusString plaintext;
1183 
1184  if (!_dbus_string_init (&plaintext))
1185  return FALSE;
1186 
1187  if (!_dbus_append_user_from_current_process (&plaintext))
1188  goto failed;
1189 
1190  if (!_dbus_string_hex_encode (&plaintext, 0,
1191  response,
1192  _dbus_string_get_length (response)))
1193  goto failed;
1194 
1195  _dbus_string_free (&plaintext);
1196 
1197  return TRUE;
1198 
1199  failed:
1200  _dbus_string_free (&plaintext);
1201  return FALSE;
1202 }
1203 
1204 static dbus_bool_t
1205 handle_client_data_external_mech (DBusAuth *auth,
1206  const DBusString *data)
1207 {
1208 
1209  return TRUE;
1210 }
1211 
1212 static void
1213 handle_client_shutdown_external_mech (DBusAuth *auth)
1214 {
1215 
1216 }
1217 
1218 /*
1219  * ANONYMOUS mechanism
1220  */
1221 
1222 static dbus_bool_t
1223 handle_server_data_anonymous_mech (DBusAuth *auth,
1224  const DBusString *data)
1225 {
1226  if (_dbus_string_get_length (data) > 0)
1227  {
1228  /* Client is allowed to send "trace" data, the only defined
1229  * meaning is that if it contains '@' it is an email address,
1230  * and otherwise it is anything else, and it's supposed to be
1231  * UTF-8
1232  */
1233  if (!_dbus_string_validate_utf8 (data, 0, _dbus_string_get_length (data)))
1234  {
1235  _dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
1236  DBUS_AUTH_NAME (auth));
1237  return send_rejected (auth);
1238  }
1239 
1240  _dbus_verbose ("%s: ANONYMOUS client sent trace string: '%s'\n",
1241  DBUS_AUTH_NAME (auth),
1242  _dbus_string_get_const_data (data));
1243  }
1244 
1245  /* We want to be anonymous (clear in case some other protocol got midway through I guess) */
1247 
1248  /* Copy process ID from the socket credentials
1249  */
1251  DBUS_CREDENTIAL_UNIX_PROCESS_ID,
1252  auth->credentials))
1253  return FALSE;
1254 
1255  /* Anonymous is always allowed */
1256  if (!send_ok (auth))
1257  return FALSE;
1258 
1259  _dbus_verbose ("%s: authenticated client as anonymous\n",
1260  DBUS_AUTH_NAME (auth));
1261 
1262  return TRUE;
1263 }
1264 
1265 static void
1266 handle_server_shutdown_anonymous_mech (DBusAuth *auth)
1267 {
1268 
1269 }
1270 
1271 static dbus_bool_t
1272 handle_client_initial_response_anonymous_mech (DBusAuth *auth,
1273  DBusString *response)
1274 {
1275  /* Our initial response is a "trace" string which must be valid UTF-8
1276  * and must be an email address if it contains '@'.
1277  * We just send the dbus implementation info, like a user-agent or
1278  * something, because... why not. There's nothing guaranteed here
1279  * though, we could change it later.
1280  */
1281  DBusString plaintext;
1282 
1283  if (!_dbus_string_init (&plaintext))
1284  return FALSE;
1285 
1286  if (!_dbus_string_append (&plaintext,
1287  "libdbus " DBUS_VERSION_STRING))
1288  goto failed;
1289 
1290  if (!_dbus_string_hex_encode (&plaintext, 0,
1291  response,
1292  _dbus_string_get_length (response)))
1293  goto failed;
1294 
1295  _dbus_string_free (&plaintext);
1296 
1297  return TRUE;
1298 
1299  failed:
1300  _dbus_string_free (&plaintext);
1301  return FALSE;
1302 }
1303 
1304 static dbus_bool_t
1305 handle_client_data_anonymous_mech (DBusAuth *auth,
1306  const DBusString *data)
1307 {
1308 
1309  return TRUE;
1310 }
1311 
1312 static void
1313 handle_client_shutdown_anonymous_mech (DBusAuth *auth)
1314 {
1315 
1316 }
1317 
1318 /* Put mechanisms here in order of preference.
1319  * Right now we have:
1320  *
1321  * - EXTERNAL checks socket credentials (or in the future, other info from the OS)
1322  * - DBUS_COOKIE_SHA1 uses a cookie in the home directory, like xauth or ICE
1323  * - ANONYMOUS checks nothing but doesn't auth the person as a user
1324  *
1325  * We might ideally add a mechanism to chain to Cyrus SASL so we can
1326  * use its mechanisms as well.
1327  *
1328  */
1329 static const DBusAuthMechanismHandler
1330 all_mechanisms[] = {
1331  { "EXTERNAL",
1332  handle_server_data_external_mech,
1333  NULL, NULL,
1334  handle_server_shutdown_external_mech,
1335  handle_client_initial_response_external_mech,
1336  handle_client_data_external_mech,
1337  NULL, NULL,
1338  handle_client_shutdown_external_mech },
1339  { "DBUS_COOKIE_SHA1",
1340  handle_server_data_cookie_sha1_mech,
1341  NULL, NULL,
1342  handle_server_shutdown_cookie_sha1_mech,
1343  handle_client_initial_response_cookie_sha1_mech,
1344  handle_client_data_cookie_sha1_mech,
1345  NULL, NULL,
1346  handle_client_shutdown_cookie_sha1_mech },
1347  { "ANONYMOUS",
1348  handle_server_data_anonymous_mech,
1349  NULL, NULL,
1350  handle_server_shutdown_anonymous_mech,
1351  handle_client_initial_response_anonymous_mech,
1352  handle_client_data_anonymous_mech,
1353  NULL, NULL,
1354  handle_client_shutdown_anonymous_mech },
1355  { NULL, NULL }
1356 };
1357 
1358 static const DBusAuthMechanismHandler*
1359 find_mech (const DBusString *name,
1360  char **allowed_mechs)
1361 {
1362  int i;
1363 
1364  if (allowed_mechs != NULL &&
1365  !_dbus_string_array_contains ((const char**) allowed_mechs,
1366  _dbus_string_get_const_data (name)))
1367  return NULL;
1368 
1369  i = 0;
1370  while (all_mechanisms[i].mechanism != NULL)
1371  {
1372  if (_dbus_string_equal_c_str (name,
1373  all_mechanisms[i].mechanism))
1374 
1375  return &all_mechanisms[i];
1376 
1377  ++i;
1378  }
1379 
1380  return NULL;
1381 }
1382 
1383 static dbus_bool_t
1384 send_auth (DBusAuth *auth, const DBusAuthMechanismHandler *mech)
1385 {
1386  DBusString auth_command;
1387 
1388  if (!_dbus_string_init (&auth_command))
1389  return FALSE;
1390 
1391  if (!_dbus_string_append (&auth_command,
1392  "AUTH "))
1393  {
1394  _dbus_string_free (&auth_command);
1395  return FALSE;
1396  }
1397 
1398  if (!_dbus_string_append (&auth_command,
1399  mech->mechanism))
1400  {
1401  _dbus_string_free (&auth_command);
1402  return FALSE;
1403  }
1404 
1405  if (mech->client_initial_response_func != NULL)
1406  {
1407  if (!_dbus_string_append (&auth_command, " "))
1408  {
1409  _dbus_string_free (&auth_command);
1410  return FALSE;
1411  }
1412 
1413  if (!(* mech->client_initial_response_func) (auth, &auth_command))
1414  {
1415  _dbus_string_free (&auth_command);
1416  return FALSE;
1417  }
1418  }
1419 
1420  if (!_dbus_string_append (&auth_command,
1421  "\r\n"))
1422  {
1423  _dbus_string_free (&auth_command);
1424  return FALSE;
1425  }
1426 
1427  if (!_dbus_string_copy (&auth_command, 0,
1428  &auth->outgoing,
1429  _dbus_string_get_length (&auth->outgoing)))
1430  {
1431  _dbus_string_free (&auth_command);
1432  return FALSE;
1433  }
1434 
1435  _dbus_string_free (&auth_command);
1436  shutdown_mech (auth);
1437  auth->mech = mech;
1438  goto_state (auth, &client_state_waiting_for_data);
1439 
1440  return TRUE;
1441 }
1442 
1443 static dbus_bool_t
1444 send_data (DBusAuth *auth, DBusString *data)
1445 {
1446  int old_len;
1447 
1448  if (data == NULL || _dbus_string_get_length (data) == 0)
1449  return _dbus_string_append (&auth->outgoing, "DATA\r\n");
1450  else
1451  {
1452  old_len = _dbus_string_get_length (&auth->outgoing);
1453  if (!_dbus_string_append (&auth->outgoing, "DATA "))
1454  goto out;
1455 
1456  if (!_dbus_string_hex_encode (data, 0, &auth->outgoing,
1457  _dbus_string_get_length (&auth->outgoing)))
1458  goto out;
1459 
1460  if (!_dbus_string_append (&auth->outgoing, "\r\n"))
1461  goto out;
1462 
1463  return TRUE;
1464 
1465  out:
1466  _dbus_string_set_length (&auth->outgoing, old_len);
1467 
1468  return FALSE;
1469  }
1470 }
1471 
1472 static dbus_bool_t
1473 send_rejected (DBusAuth *auth)
1474 {
1475  DBusString command;
1476  DBusAuthServer *server_auth;
1477  int i;
1478 
1479  if (!_dbus_string_init (&command))
1480  return FALSE;
1481 
1482  if (!_dbus_string_append (&command,
1483  "REJECTED"))
1484  goto nomem;
1485 
1486  i = 0;
1487  while (all_mechanisms[i].mechanism != NULL)
1488  {
1489  if (!_dbus_string_append (&command,
1490  " "))
1491  goto nomem;
1492 
1493  if (!_dbus_string_append (&command,
1494  all_mechanisms[i].mechanism))
1495  goto nomem;
1496 
1497  ++i;
1498  }
1499 
1500  if (!_dbus_string_append (&command, "\r\n"))
1501  goto nomem;
1502 
1503  if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1504  _dbus_string_get_length (&auth->outgoing)))
1505  goto nomem;
1506 
1507  shutdown_mech (auth);
1508 
1510  server_auth = DBUS_AUTH_SERVER (auth);
1511  server_auth->failures += 1;
1512 
1513  if (server_auth->failures >= server_auth->max_failures)
1514  goto_state (auth, &common_state_need_disconnect);
1515  else
1516  goto_state (auth, &server_state_waiting_for_auth);
1517 
1518  _dbus_string_free (&command);
1519 
1520  return TRUE;
1521 
1522  nomem:
1523  _dbus_string_free (&command);
1524  return FALSE;
1525 }
1526 
1527 static dbus_bool_t
1528 send_error (DBusAuth *auth, const char *message)
1529 {
1530  return _dbus_string_append_printf (&auth->outgoing,
1531  "ERROR \"%s\"\r\n", message);
1532 }
1533 
1534 static dbus_bool_t
1535 send_ok (DBusAuth *auth)
1536 {
1537  int orig_len;
1538 
1539  orig_len = _dbus_string_get_length (&auth->outgoing);
1540 
1541  if (_dbus_string_append (&auth->outgoing, "OK ") &&
1542  _dbus_string_copy (& DBUS_AUTH_SERVER (auth)->guid,
1543  0,
1544  &auth->outgoing,
1545  _dbus_string_get_length (&auth->outgoing)) &&
1546  _dbus_string_append (&auth->outgoing, "\r\n"))
1547  {
1548  goto_state (auth, &server_state_waiting_for_begin);
1549  return TRUE;
1550  }
1551  else
1552  {
1553  _dbus_string_set_length (&auth->outgoing, orig_len);
1554  return FALSE;
1555  }
1556 }
1557 
1558 static dbus_bool_t
1559 send_begin (DBusAuth *auth)
1560 {
1561 
1562  if (!_dbus_string_append (&auth->outgoing,
1563  "BEGIN\r\n"))
1564  return FALSE;
1565 
1566  goto_state (auth, &common_state_authenticated);
1567  return TRUE;
1568 }
1569 
1570 static dbus_bool_t
1571 process_ok(DBusAuth *auth,
1572  const DBusString *args_from_ok) {
1573 
1574  int end_of_hex;
1575 
1576  /* "args_from_ok" should be the GUID, whitespace already pulled off the front */
1577  _dbus_assert (_dbus_string_get_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server) == 0);
1578 
1579  /* We decode the hex string to binary, using guid_from_server as scratch... */
1580 
1581  end_of_hex = 0;
1582  if (!_dbus_string_hex_decode (args_from_ok, 0, &end_of_hex,
1583  & DBUS_AUTH_CLIENT (auth)->guid_from_server, 0))
1584  return FALSE;
1585 
1586  /* now clear out the scratch */
1587  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1588 
1589  if (end_of_hex != _dbus_string_get_length (args_from_ok) ||
1590  end_of_hex == 0)
1591  {
1592  _dbus_verbose ("Bad GUID from server, parsed %d bytes and had %d bytes from server\n",
1593  end_of_hex, _dbus_string_get_length (args_from_ok));
1594  goto_state (auth, &common_state_need_disconnect);
1595  return TRUE;
1596  }
1597 
1598  if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
1599  _dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
1600  return FALSE;
1601  }
1602 
1603  _dbus_verbose ("Got GUID '%s' from the server\n",
1604  _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
1605 
1606  if (auth->unix_fd_possible)
1607  return send_negotiate_unix_fd(auth);
1608 
1609  _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
1610  return send_begin (auth);
1611 }
1612 
1613 static dbus_bool_t
1614 send_cancel (DBusAuth *auth)
1615 {
1616  if (_dbus_string_append (&auth->outgoing, "CANCEL\r\n"))
1617  {
1618  goto_state (auth, &client_state_waiting_for_reject);
1619  return TRUE;
1620  }
1621  else
1622  return FALSE;
1623 }
1624 
1625 static dbus_bool_t
1626 process_data (DBusAuth *auth,
1627  const DBusString *args,
1628  DBusAuthDataFunction data_func)
1629 {
1630  int end;
1631  DBusString decoded;
1632 
1633  if (!_dbus_string_init (&decoded))
1634  return FALSE;
1635 
1636  if (!_dbus_string_hex_decode (args, 0, &end, &decoded, 0))
1637  {
1638  _dbus_string_free (&decoded);
1639  return FALSE;
1640  }
1641 
1642  if (_dbus_string_get_length (args) != end)
1643  {
1644  _dbus_string_free (&decoded);
1645  if (!send_error (auth, "Invalid hex encoding"))
1646  return FALSE;
1647 
1648  return TRUE;
1649  }
1650 
1651 #ifdef DBUS_ENABLE_VERBOSE_MODE
1652  if (_dbus_string_validate_ascii (&decoded, 0,
1653  _dbus_string_get_length (&decoded)))
1654  _dbus_verbose ("%s: data: '%s'\n",
1655  DBUS_AUTH_NAME (auth),
1656  _dbus_string_get_const_data (&decoded));
1657 #endif
1658 
1659  if (!(* data_func) (auth, &decoded))
1660  {
1661  _dbus_string_free (&decoded);
1662  return FALSE;
1663  }
1664 
1665  _dbus_string_free (&decoded);
1666  return TRUE;
1667 }
1668 
1669 static dbus_bool_t
1670 send_negotiate_unix_fd (DBusAuth *auth)
1671 {
1672  if (!_dbus_string_append (&auth->outgoing,
1673  "NEGOTIATE_UNIX_FD\r\n"))
1674  return FALSE;
1675 
1676  goto_state (auth, &client_state_waiting_for_agree_unix_fd);
1677  return TRUE;
1678 }
1679 
1680 static dbus_bool_t
1681 send_agree_unix_fd (DBusAuth *auth)
1682 {
1684 
1685  auth->unix_fd_negotiated = TRUE;
1686  _dbus_verbose("Agreed to UNIX FD passing\n");
1687 
1688  if (!_dbus_string_append (&auth->outgoing,
1689  "AGREE_UNIX_FD\r\n"))
1690  return FALSE;
1691 
1692  goto_state (auth, &server_state_waiting_for_begin);
1693  return TRUE;
1694 }
1695 
1696 static dbus_bool_t
1697 handle_auth (DBusAuth *auth, const DBusString *args)
1698 {
1699  if (_dbus_string_get_length (args) == 0)
1700  {
1701  /* No args to the auth, send mechanisms */
1702  if (!send_rejected (auth))
1703  return FALSE;
1704 
1705  return TRUE;
1706  }
1707  else
1708  {
1709  int i;
1710  DBusString mech;
1711  DBusString hex_response;
1712 
1713  _dbus_string_find_blank (args, 0, &i);
1714 
1715  if (!_dbus_string_init (&mech))
1716  return FALSE;
1717 
1718  if (!_dbus_string_init (&hex_response))
1719  {
1720  _dbus_string_free (&mech);
1721  return FALSE;
1722  }
1723 
1724  if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1725  goto failed;
1726 
1727  _dbus_string_skip_blank (args, i, &i);
1728  if (!_dbus_string_copy (args, i, &hex_response, 0))
1729  goto failed;
1730 
1731  auth->mech = find_mech (&mech, auth->allowed_mechs);
1732  if (auth->mech != NULL)
1733  {
1734  _dbus_verbose ("%s: Trying mechanism %s\n",
1735  DBUS_AUTH_NAME (auth),
1736  auth->mech->mechanism);
1737 
1738  if (!process_data (auth, &hex_response,
1739  auth->mech->server_data_func))
1740  goto failed;
1741  }
1742  else
1743  {
1744  /* Unsupported mechanism */
1745  _dbus_verbose ("%s: Unsupported mechanism %s\n",
1746  DBUS_AUTH_NAME (auth),
1747  _dbus_string_get_const_data (&mech));
1748 
1749  if (!send_rejected (auth))
1750  goto failed;
1751  }
1752 
1753  _dbus_string_free (&mech);
1754  _dbus_string_free (&hex_response);
1755 
1756  return TRUE;
1757 
1758  failed:
1759  auth->mech = NULL;
1760  _dbus_string_free (&mech);
1761  _dbus_string_free (&hex_response);
1762  return FALSE;
1763  }
1764 }
1765 
1766 static dbus_bool_t
1767 handle_server_state_waiting_for_auth (DBusAuth *auth,
1768  DBusAuthCommand command,
1769  const DBusString *args)
1770 {
1771  switch (command)
1772  {
1773  case DBUS_AUTH_COMMAND_AUTH:
1774  return handle_auth (auth, args);
1775 
1776  case DBUS_AUTH_COMMAND_CANCEL:
1777  case DBUS_AUTH_COMMAND_DATA:
1778  return send_error (auth, "Not currently in an auth conversation");
1779 
1780  case DBUS_AUTH_COMMAND_BEGIN:
1781  goto_state (auth, &common_state_need_disconnect);
1782  return TRUE;
1783 
1784  case DBUS_AUTH_COMMAND_ERROR:
1785  return send_rejected (auth);
1786 
1787  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1788  return send_error (auth, "Need to authenticate first");
1789 
1790  case DBUS_AUTH_COMMAND_REJECTED:
1791  case DBUS_AUTH_COMMAND_OK:
1792  case DBUS_AUTH_COMMAND_UNKNOWN:
1793  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1794  default:
1795  return send_error (auth, "Unknown command");
1796  }
1797 }
1798 
1799 static dbus_bool_t
1800 handle_server_state_waiting_for_data (DBusAuth *auth,
1801  DBusAuthCommand command,
1802  const DBusString *args)
1803 {
1804  switch (command)
1805  {
1806  case DBUS_AUTH_COMMAND_AUTH:
1807  return send_error (auth, "Sent AUTH while another AUTH in progress");
1808 
1809  case DBUS_AUTH_COMMAND_CANCEL:
1810  case DBUS_AUTH_COMMAND_ERROR:
1811  return send_rejected (auth);
1812 
1813  case DBUS_AUTH_COMMAND_DATA:
1814  return process_data (auth, args, auth->mech->server_data_func);
1815 
1816  case DBUS_AUTH_COMMAND_BEGIN:
1817  goto_state (auth, &common_state_need_disconnect);
1818  return TRUE;
1819 
1820  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1821  return send_error (auth, "Need to authenticate first");
1822 
1823  case DBUS_AUTH_COMMAND_REJECTED:
1824  case DBUS_AUTH_COMMAND_OK:
1825  case DBUS_AUTH_COMMAND_UNKNOWN:
1826  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1827  default:
1828  return send_error (auth, "Unknown command");
1829  }
1830 }
1831 
1832 static dbus_bool_t
1833 handle_server_state_waiting_for_begin (DBusAuth *auth,
1834  DBusAuthCommand command,
1835  const DBusString *args)
1836 {
1837  switch (command)
1838  {
1839  case DBUS_AUTH_COMMAND_AUTH:
1840  return send_error (auth, "Sent AUTH while expecting BEGIN");
1841 
1842  case DBUS_AUTH_COMMAND_DATA:
1843  return send_error (auth, "Sent DATA while expecting BEGIN");
1844 
1845  case DBUS_AUTH_COMMAND_BEGIN:
1846  goto_state (auth, &common_state_authenticated);
1847  return TRUE;
1848 
1849  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
1850  if (auth->unix_fd_possible)
1851  return send_agree_unix_fd(auth);
1852  else
1853  return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
1854 
1855  case DBUS_AUTH_COMMAND_REJECTED:
1856  case DBUS_AUTH_COMMAND_OK:
1857  case DBUS_AUTH_COMMAND_UNKNOWN:
1858  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
1859  default:
1860  return send_error (auth, "Unknown command");
1861 
1862  case DBUS_AUTH_COMMAND_CANCEL:
1863  case DBUS_AUTH_COMMAND_ERROR:
1864  return send_rejected (auth);
1865  }
1866 }
1867 
1868 /* return FALSE if no memory, TRUE if all OK */
1869 static dbus_bool_t
1870 get_word (const DBusString *str,
1871  int *start,
1872  DBusString *word)
1873 {
1874  int i;
1875 
1876  _dbus_string_skip_blank (str, *start, start);
1877  _dbus_string_find_blank (str, *start, &i);
1878 
1879  if (i > *start)
1880  {
1881  if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1882  return FALSE;
1883 
1884  *start = i;
1885  }
1886 
1887  return TRUE;
1888 }
1889 
1890 static dbus_bool_t
1891 record_mechanisms (DBusAuth *auth,
1892  const DBusString *args)
1893 {
1894  int next;
1895  int len;
1896 
1897  if (auth->already_got_mechanisms)
1898  return TRUE;
1899 
1900  len = _dbus_string_get_length (args);
1901 
1902  next = 0;
1903  while (next < len)
1904  {
1905  DBusString m;
1906  const DBusAuthMechanismHandler *mech;
1907 
1908  if (!_dbus_string_init (&m))
1909  goto nomem;
1910 
1911  if (!get_word (args, &next, &m))
1912  {
1913  _dbus_string_free (&m);
1914  goto nomem;
1915  }
1916 
1917  mech = find_mech (&m, auth->allowed_mechs);
1918 
1919  if (mech != NULL)
1920  {
1921  /* FIXME right now we try mechanisms in the order
1922  * the server lists them; should we do them in
1923  * some more deterministic order?
1924  *
1925  * Probably in all_mechanisms order, our order of
1926  * preference. Of course when the server is us,
1927  * it lists things in that order anyhow.
1928  */
1929 
1930  if (mech != &all_mechanisms[0])
1931  {
1932  _dbus_verbose ("%s: Adding mechanism %s to list we will try\n",
1933  DBUS_AUTH_NAME (auth), mech->mechanism);
1934 
1935  if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1936  (void*) mech))
1937  {
1938  _dbus_string_free (&m);
1939  goto nomem;
1940  }
1941  }
1942  else
1943  {
1944  _dbus_verbose ("%s: Already tried mechanism %s; not adding to list we will try\n",
1945  DBUS_AUTH_NAME (auth), mech->mechanism);
1946  }
1947  }
1948  else
1949  {
1950  _dbus_verbose ("%s: Server offered mechanism \"%s\" that we don't know how to use\n",
1951  DBUS_AUTH_NAME (auth),
1952  _dbus_string_get_const_data (&m));
1953  }
1954 
1955  _dbus_string_free (&m);
1956  }
1957 
1958  auth->already_got_mechanisms = TRUE;
1959 
1960  return TRUE;
1961 
1962  nomem:
1963  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1964 
1965  return FALSE;
1966 }
1967 
1968 static dbus_bool_t
1969 process_rejected (DBusAuth *auth, const DBusString *args)
1970 {
1971  const DBusAuthMechanismHandler *mech;
1972  DBusAuthClient *client;
1973 
1974  client = DBUS_AUTH_CLIENT (auth);
1975 
1976  if (!auth->already_got_mechanisms)
1977  {
1978  if (!record_mechanisms (auth, args))
1979  return FALSE;
1980  }
1981 
1982  if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1983  {
1984  mech = client->mechs_to_try->data;
1985 
1986  if (!send_auth (auth, mech))
1987  return FALSE;
1988 
1990 
1991  _dbus_verbose ("%s: Trying mechanism %s\n",
1992  DBUS_AUTH_NAME (auth),
1993  mech->mechanism);
1994  }
1995  else
1996  {
1997  /* Give up */
1998  _dbus_verbose ("%s: Disconnecting because we are out of mechanisms to try using\n",
1999  DBUS_AUTH_NAME (auth));
2000  goto_state (auth, &common_state_need_disconnect);
2001  }
2002 
2003  return TRUE;
2004 }
2005 
2006 
2007 static dbus_bool_t
2008 handle_client_state_waiting_for_data (DBusAuth *auth,
2009  DBusAuthCommand command,
2010  const DBusString *args)
2011 {
2012  _dbus_assert (auth->mech != NULL);
2013 
2014  switch (command)
2015  {
2016  case DBUS_AUTH_COMMAND_DATA:
2017  return process_data (auth, args, auth->mech->client_data_func);
2018 
2019  case DBUS_AUTH_COMMAND_REJECTED:
2020  return process_rejected (auth, args);
2021 
2022  case DBUS_AUTH_COMMAND_OK:
2023  return process_ok(auth, args);
2024 
2025  case DBUS_AUTH_COMMAND_ERROR:
2026  return send_cancel (auth);
2027 
2028  case DBUS_AUTH_COMMAND_AUTH:
2029  case DBUS_AUTH_COMMAND_CANCEL:
2030  case DBUS_AUTH_COMMAND_BEGIN:
2031  case DBUS_AUTH_COMMAND_UNKNOWN:
2032  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2033  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2034  default:
2035  return send_error (auth, "Unknown command");
2036  }
2037 }
2038 
2039 static dbus_bool_t
2040 handle_client_state_waiting_for_ok (DBusAuth *auth,
2041  DBusAuthCommand command,
2042  const DBusString *args)
2043 {
2044  switch (command)
2045  {
2046  case DBUS_AUTH_COMMAND_REJECTED:
2047  return process_rejected (auth, args);
2048 
2049  case DBUS_AUTH_COMMAND_OK:
2050  return process_ok(auth, args);
2051 
2052  case DBUS_AUTH_COMMAND_DATA:
2053  case DBUS_AUTH_COMMAND_ERROR:
2054  return send_cancel (auth);
2055 
2056  case DBUS_AUTH_COMMAND_AUTH:
2057  case DBUS_AUTH_COMMAND_CANCEL:
2058  case DBUS_AUTH_COMMAND_BEGIN:
2059  case DBUS_AUTH_COMMAND_UNKNOWN:
2060  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2061  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2062  default:
2063  return send_error (auth, "Unknown command");
2064  }
2065 }
2066 
2067 static dbus_bool_t
2068 handle_client_state_waiting_for_reject (DBusAuth *auth,
2069  DBusAuthCommand command,
2070  const DBusString *args)
2071 {
2072  switch (command)
2073  {
2074  case DBUS_AUTH_COMMAND_REJECTED:
2075  return process_rejected (auth, args);
2076 
2077  case DBUS_AUTH_COMMAND_AUTH:
2078  case DBUS_AUTH_COMMAND_CANCEL:
2079  case DBUS_AUTH_COMMAND_DATA:
2080  case DBUS_AUTH_COMMAND_BEGIN:
2081  case DBUS_AUTH_COMMAND_OK:
2082  case DBUS_AUTH_COMMAND_ERROR:
2083  case DBUS_AUTH_COMMAND_UNKNOWN:
2084  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2085  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2086  default:
2087  goto_state (auth, &common_state_need_disconnect);
2088  return TRUE;
2089  }
2090 }
2091 
2092 static dbus_bool_t
2093 handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
2094  DBusAuthCommand command,
2095  const DBusString *args)
2096 {
2097  switch (command)
2098  {
2099  case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
2101  auth->unix_fd_negotiated = TRUE;
2102  _dbus_verbose("Successfully negotiated UNIX FD passing\n");
2103  return send_begin (auth);
2104 
2105  case DBUS_AUTH_COMMAND_ERROR:
2107  auth->unix_fd_negotiated = FALSE;
2108  _dbus_verbose("Failed to negotiate UNIX FD passing\n");
2109  return send_begin (auth);
2110 
2111  case DBUS_AUTH_COMMAND_OK:
2112  case DBUS_AUTH_COMMAND_DATA:
2113  case DBUS_AUTH_COMMAND_REJECTED:
2114  case DBUS_AUTH_COMMAND_AUTH:
2115  case DBUS_AUTH_COMMAND_CANCEL:
2116  case DBUS_AUTH_COMMAND_BEGIN:
2117  case DBUS_AUTH_COMMAND_UNKNOWN:
2118  case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
2119  default:
2120  return send_error (auth, "Unknown command");
2121  }
2122 }
2123 
2127 typedef struct {
2128  const char *name;
2131 
2132 static const DBusAuthCommandName auth_command_names[] = {
2133  { "AUTH", DBUS_AUTH_COMMAND_AUTH },
2134  { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
2135  { "DATA", DBUS_AUTH_COMMAND_DATA },
2136  { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
2137  { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
2138  { "OK", DBUS_AUTH_COMMAND_OK },
2139  { "ERROR", DBUS_AUTH_COMMAND_ERROR },
2140  { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
2141  { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
2142 };
2143 
2144 static DBusAuthCommand
2145 lookup_command_from_name (DBusString *command)
2146 {
2147  int i;
2148 
2149  for (i = 0; i < _DBUS_N_ELEMENTS (auth_command_names); i++)
2150  {
2151  if (_dbus_string_equal_c_str (command,
2152  auth_command_names[i].name))
2153  return auth_command_names[i].command;
2154  }
2155 
2156  return DBUS_AUTH_COMMAND_UNKNOWN;
2157 }
2158 
2159 static void
2160 goto_state (DBusAuth *auth,
2161  const DBusAuthStateData *state)
2162 {
2163  _dbus_verbose ("%s: going from state %s to state %s\n",
2164  DBUS_AUTH_NAME (auth),
2165  auth->state->name,
2166  state->name);
2167 
2168  auth->state = state;
2169 }
2170 
2171 /* returns whether to call it again right away */
2172 static dbus_bool_t
2173 process_command (DBusAuth *auth)
2174 {
2175  DBusAuthCommand command;
2176  DBusString line;
2177  DBusString args;
2178  int eol;
2179  int i, j;
2180  dbus_bool_t retval;
2181 
2182  /* _dbus_verbose ("%s: trying process_command()\n"); */
2183 
2184  retval = FALSE;
2185 
2186  eol = 0;
2187  if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
2188  return FALSE;
2189 
2190  if (!_dbus_string_init (&line))
2191  {
2192  auth->needed_memory = TRUE;
2193  return FALSE;
2194  }
2195 
2196  if (!_dbus_string_init (&args))
2197  {
2198  _dbus_string_free (&line);
2199  auth->needed_memory = TRUE;
2200  return FALSE;
2201  }
2202 
2203  if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &line, 0))
2204  goto out;
2205 
2206  if (!_dbus_string_validate_ascii (&line, 0,
2207  _dbus_string_get_length (&line)))
2208  {
2209  _dbus_verbose ("%s: Command contained non-ASCII chars or embedded nul\n",
2210  DBUS_AUTH_NAME (auth));
2211  if (!send_error (auth, "Command contained non-ASCII"))
2212  goto out;
2213  else
2214  goto next_command;
2215  }
2216 
2217  _dbus_verbose ("%s: got command \"%s\"\n",
2218  DBUS_AUTH_NAME (auth),
2219  _dbus_string_get_const_data (&line));
2220 
2221  _dbus_string_find_blank (&line, 0, &i);
2222  _dbus_string_skip_blank (&line, i, &j);
2223 
2224  if (j > i)
2225  _dbus_string_delete (&line, i, j - i);
2226 
2227  if (!_dbus_string_move (&line, i, &args, 0))
2228  goto out;
2229 
2230  /* FIXME 1.0 we should probably validate that only the allowed
2231  * chars are in the command name
2232  */
2233 
2234  command = lookup_command_from_name (&line);
2235  if (!(* auth->state->handler) (auth, command, &args))
2236  goto out;
2237 
2238  next_command:
2239 
2240  /* We've succeeded in processing the whole command so drop it out
2241  * of the incoming buffer and return TRUE to try another command.
2242  */
2243 
2244  _dbus_string_delete (&auth->incoming, 0, eol);
2245 
2246  /* kill the \r\n */
2247  _dbus_string_delete (&auth->incoming, 0, 2);
2248 
2249  retval = TRUE;
2250 
2251  out:
2252  _dbus_string_free (&args);
2253  _dbus_string_free (&line);
2254 
2255  if (!retval)
2256  auth->needed_memory = TRUE;
2257  else
2258  auth->needed_memory = FALSE;
2259 
2260  return retval;
2261 }
2262 
2263 
2278 DBusAuth*
2280 {
2281  DBusAuth *auth;
2282  DBusAuthServer *server_auth;
2283  DBusString guid_copy;
2284 
2285  if (!_dbus_string_init (&guid_copy))
2286  return NULL;
2287 
2288  if (!_dbus_string_copy (guid, 0, &guid_copy, 0))
2289  {
2290  _dbus_string_free (&guid_copy);
2291  return NULL;
2292  }
2293 
2294  auth = _dbus_auth_new (sizeof (DBusAuthServer));
2295  if (auth == NULL)
2296  {
2297  _dbus_string_free (&guid_copy);
2298  return NULL;
2299  }
2300 
2301  auth->side = auth_side_server;
2302  auth->state = &server_state_waiting_for_auth;
2303 
2304  server_auth = DBUS_AUTH_SERVER (auth);
2305 
2306  server_auth->guid = guid_copy;
2307 
2308  /* perhaps this should be per-mechanism with a lower
2309  * max
2310  */
2311  server_auth->failures = 0;
2312  server_auth->max_failures = 6;
2313 
2314  return auth;
2315 }
2316 
2324 DBusAuth*
2326 {
2327  DBusAuth *auth;
2328  DBusString guid_str;
2329 
2330  if (!_dbus_string_init (&guid_str))
2331  return NULL;
2332 
2333  auth = _dbus_auth_new (sizeof (DBusAuthClient));
2334  if (auth == NULL)
2335  {
2336  _dbus_string_free (&guid_str);
2337  return NULL;
2338  }
2339 
2340  DBUS_AUTH_CLIENT (auth)->guid_from_server = guid_str;
2341 
2342  auth->side = auth_side_client;
2343  auth->state = &client_state_need_send_auth;
2344 
2345  /* Start the auth conversation by sending AUTH for our default
2346  * mechanism */
2347  if (!send_auth (auth, &all_mechanisms[0]))
2348  {
2349  _dbus_auth_unref (auth);
2350  return NULL;
2351  }
2352 
2353  return auth;
2354 }
2355 
2362 DBusAuth *
2364 {
2365  _dbus_assert (auth != NULL);
2366 
2367  auth->refcount += 1;
2368 
2369  return auth;
2370 }
2371 
2377 void
2379 {
2380  _dbus_assert (auth != NULL);
2381  _dbus_assert (auth->refcount > 0);
2382 
2383  auth->refcount -= 1;
2384  if (auth->refcount == 0)
2385  {
2386  shutdown_mech (auth);
2387 
2388  if (DBUS_AUTH_IS_CLIENT (auth))
2389  {
2390  _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2391  _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
2392  }
2393  else
2394  {
2396 
2397  _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
2398  }
2399 
2400  if (auth->keyring)
2401  _dbus_keyring_unref (auth->keyring);
2402 
2403  _dbus_string_free (&auth->context);
2404  _dbus_string_free (&auth->challenge);
2405  _dbus_string_free (&auth->identity);
2406  _dbus_string_free (&auth->incoming);
2407  _dbus_string_free (&auth->outgoing);
2408 
2410 
2414 
2415  dbus_free (auth);
2416  }
2417 }
2418 
2429  const char **mechanisms)
2430 {
2431  char **copy;
2432 
2433  if (mechanisms != NULL)
2434  {
2435  copy = _dbus_dup_string_array (mechanisms);
2436  if (copy == NULL)
2437  return FALSE;
2438  }
2439  else
2440  copy = NULL;
2441 
2443 
2444  auth->allowed_mechs = copy;
2445 
2446  return TRUE;
2447 }
2448 
2453 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->state->handler == NULL)
2454 
2462 DBusAuthState
2464 {
2465  auth->needed_memory = FALSE;
2466 
2467  /* Max amount we'll buffer up before deciding someone's on crack */
2468 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
2469 
2470  do
2471  {
2472  if (DBUS_AUTH_IN_END_STATE (auth))
2473  break;
2474 
2475  if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
2476  _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
2477  {
2478  goto_state (auth, &common_state_need_disconnect);
2479  _dbus_verbose ("%s: Disconnecting due to excessive data buffered in auth phase\n",
2480  DBUS_AUTH_NAME (auth));
2481  break;
2482  }
2483  }
2484  while (process_command (auth));
2485 
2486  if (auth->needed_memory)
2487  return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
2488  else if (_dbus_string_get_length (&auth->outgoing) > 0)
2489  return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
2490  else if (auth->state == &common_state_need_disconnect)
2491  return DBUS_AUTH_STATE_NEED_DISCONNECT;
2492  else if (auth->state == &common_state_authenticated)
2493  return DBUS_AUTH_STATE_AUTHENTICATED;
2494  else return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
2495 }
2496 
2508  const DBusString **str)
2509 {
2510  _dbus_assert (auth != NULL);
2511  _dbus_assert (str != NULL);
2512 
2513  *str = NULL;
2514 
2515  if (_dbus_string_get_length (&auth->outgoing) == 0)
2516  return FALSE;
2517 
2518  *str = &auth->outgoing;
2519 
2520  return TRUE;
2521 }
2522 
2531 void
2533  int bytes_sent)
2534 {
2535  _dbus_verbose ("%s: Sent %d bytes of: %s\n",
2536  DBUS_AUTH_NAME (auth),
2537  bytes_sent,
2538  _dbus_string_get_const_data (&auth->outgoing));
2539 
2540  _dbus_string_delete (&auth->outgoing,
2541  0, bytes_sent);
2542 }
2543 
2551 void
2553  DBusString **buffer)
2554 {
2555  _dbus_assert (auth != NULL);
2557 
2558  *buffer = &auth->incoming;
2559 
2560  auth->buffer_outstanding = TRUE;
2561 }
2562 
2569 void
2571  DBusString *buffer)
2572 {
2573  _dbus_assert (buffer == &auth->incoming);
2575 
2576  auth->buffer_outstanding = FALSE;
2577 }
2578 
2588 void
2590  const DBusString **str)
2591 {
2592  if (!DBUS_AUTH_IN_END_STATE (auth))
2593  return;
2594 
2595  *str = &auth->incoming;
2596 }
2597 
2598 
2605 void
2607 {
2608  if (!DBUS_AUTH_IN_END_STATE (auth))
2609  return;
2610 
2611  _dbus_string_set_length (&auth->incoming, 0);
2612 }
2613 
2624 {
2625  if (auth->state != &common_state_authenticated)
2626  return FALSE;
2627 
2628  if (auth->mech != NULL)
2629  {
2630  if (DBUS_AUTH_IS_CLIENT (auth))
2631  return auth->mech->client_encode_func != NULL;
2632  else
2633  return auth->mech->server_encode_func != NULL;
2634  }
2635  else
2636  return FALSE;
2637 }
2638 
2651  const DBusString *plaintext,
2652  DBusString *encoded)
2653 {
2654  _dbus_assert (plaintext != encoded);
2655 
2656  if (auth->state != &common_state_authenticated)
2657  return FALSE;
2658 
2659  if (_dbus_auth_needs_encoding (auth))
2660  {
2661  if (DBUS_AUTH_IS_CLIENT (auth))
2662  return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2663  else
2664  return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2665  }
2666  else
2667  {
2668  return _dbus_string_copy (plaintext, 0, encoded,
2669  _dbus_string_get_length (encoded));
2670  }
2671 }
2672 
2683 {
2684  if (auth->state != &common_state_authenticated)
2685  return FALSE;
2686 
2687  if (auth->mech != NULL)
2688  {
2689  if (DBUS_AUTH_IS_CLIENT (auth))
2690  return auth->mech->client_decode_func != NULL;
2691  else
2692  return auth->mech->server_decode_func != NULL;
2693  }
2694  else
2695  return FALSE;
2696 }
2697 
2698 
2714  const DBusString *encoded,
2715  DBusString *plaintext)
2716 {
2717  _dbus_assert (plaintext != encoded);
2718 
2719  if (auth->state != &common_state_authenticated)
2720  return FALSE;
2721 
2722  if (_dbus_auth_needs_decoding (auth))
2723  {
2724  if (DBUS_AUTH_IS_CLIENT (auth))
2725  return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2726  else
2727  return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2728  }
2729  else
2730  {
2731  return _dbus_string_copy (encoded, 0, plaintext,
2732  _dbus_string_get_length (plaintext));
2733  }
2734 }
2735 
2746  DBusCredentials *credentials)
2747 {
2750  credentials);
2751 }
2752 
2764 {
2765  if (auth->state == &common_state_authenticated)
2766  {
2767  return auth->authorized_identity;
2768  }
2769  else
2770  {
2771  /* FIXME instead of this, keep an empty credential around that
2772  * doesn't require allocation or something
2773  */
2774  /* return empty credentials */
2776  return auth->authorized_identity;
2777  }
2778 }
2779 
2786 const char*
2788 {
2790 
2791  if (auth->state == &common_state_authenticated)
2792  return _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
2793  else
2794  return NULL;
2795 }
2796 
2807  const DBusString *context)
2808 {
2809  return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2810  &auth->context, 0, _dbus_string_get_length (context));
2811 }
2812 
2820 void
2822 {
2823  auth->unix_fd_possible = b;
2824 }
2825 
2834 {
2835  return auth->unix_fd_negotiated;
2836 }
2837 
2840 /* 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:918
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've gotten them and succes...
Definition: dbus-auth.c:2606
#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:2589
void _dbus_keyring_unref(DBusKeyring *keyring)
Decrements refcount and finalizes if it reaches zero.
Definition: dbus-keyring.c:681
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:1995
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:2682
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:2241
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're going to try using.
Definition: dbus-auth.c:203
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:2650
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 "authentication context" which scopes cookies with the DBUS_COOKIE_SHA1 auth mechanism for e...
Definition: dbus-auth.c:2806
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
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:2570
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:2463
void dbus_error_free(DBusError *error)
Frees an error that'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:2428
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
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...
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's copied to the d...
Definition: dbus-string.c:1265
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:2833
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:1586
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:2763
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we're conversing with.
Definition: dbus-auth.c:2552
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:2713
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:1241
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:2134
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:2532
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:1386
"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:1785
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:2279
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:2363
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:1175
DBusString identity
Current identity we'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:1096
void _dbus_string_zero(DBusString *str)
Clears all allocated bytes in the string to zero.
Definition: dbus-string.c:2667
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:949
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:2537
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've authenticated; gets NULL otherwise.
Definition: dbus-auth.c:2787
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
char ** allowed_mechs
Mechanisms we'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:2128
#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:242
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
#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:1747
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:2745
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:988
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:2821
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:709
DBusAuthDataFunction server_data_func
Function on server side for DATA.
Definition: dbus-auth.c:105
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
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:2291
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:2378
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're conversing with.
Definition: dbus-auth.c:2507
Mapping from command name to enum.
Definition: dbus-auth.c:2127
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 "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:785
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:1357
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:2623
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:2325
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:2432
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:853
#define DBUS_AUTH_IN_END_STATE(auth)
Definition: dbus-auth.c:2453
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.
dbus_bool_t _dbus_generate_random_bytes(DBusString *str, int n_bytes)
Generates the given number of random bytes, using the best mechanism we can come up with...
#define DBUS_AUTH_IS_CLIENT(auth)
Definition: dbus-auth.c:318
DBusAuthCommand command
Corresponding enum.
Definition: dbus-auth.c:2129
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.