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