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