D-Bus 1.12.20

dbus-auth-script.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-auth-script.c Test DBusAuth using a special script file (internal to D-Bus implementation)
3 *
4 * Copyright (C) 2003 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 #include <config.h>
24
25 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
26
27 #include "dbus-auth-script.h"
28
29 #include <stdio.h>
30
31 #include "dbus-auth.h"
32 #include "dbus-string.h"
33 #include "dbus-hash.h"
34 #include "dbus-credentials.h"
35 #include "dbus-internals.h"
36
37 #include "test/test-utils.h"
38
50 /* this is slightly different from the other append_quoted_string
51 * in dbus-message-builder.c
52 */
53 static dbus_bool_t
54 append_quoted_string (DBusString *dest,
55 const DBusString *quoted)
56 {
57 dbus_bool_t in_quotes = FALSE;
58 dbus_bool_t in_backslash = FALSE;
59 int i;
60
61 i = 0;
62 while (i < _dbus_string_get_length (quoted))
63 {
64 unsigned char b;
65
66 b = _dbus_string_get_byte (quoted, i);
67
68 if (in_backslash)
69 {
70 unsigned char a;
71
72 if (b == 'r')
73 a = '\r';
74 else if (b == 'n')
75 a = '\n';
76 else if (b == '\\')
77 a = '\\';
78 else
79 {
80 _dbus_warn ("bad backslashed byte %c", b);
81 return FALSE;
82 }
83
84 if (!_dbus_string_append_byte (dest, a))
85 return FALSE;
86
87 in_backslash = FALSE;
88 }
89 else if (b == '\\')
90 {
91 in_backslash = TRUE;
92 }
93 else if (in_quotes)
94 {
95 if (b == '\'')
96 in_quotes = FALSE;
97 else
98 {
99 if (!_dbus_string_append_byte (dest, b))
100 return FALSE;
101 }
102 }
103 else
104 {
105 if (b == '\'')
106 in_quotes = TRUE;
107 else if (b == ' ' || b == '\n' || b == '\t')
108 break; /* end on whitespace if not quoted */
109 else
110 {
111 if (!_dbus_string_append_byte (dest, b))
112 return FALSE;
113 }
114 }
115
116 ++i;
117 }
118
119 return TRUE;
120 }
121
122 static dbus_bool_t
123 same_first_word (const DBusString *a,
124 const DBusString *b)
125 {
126 int first_a_blank, first_b_blank;
127
128 _dbus_string_find_blank (a, 0, &first_a_blank);
129 _dbus_string_find_blank (b, 0, &first_b_blank);
130
131 if (first_a_blank != first_b_blank)
132 return FALSE;
133
134 return _dbus_string_equal_len (a, b, first_a_blank);
135 }
136
137 static DBusAuthState
138 auth_state_from_string (const DBusString *str)
139 {
140 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
141 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
142 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
143 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
144 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
145 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
146 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
147 return DBUS_AUTH_STATE_NEED_DISCONNECT;
148 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
149 return DBUS_AUTH_STATE_AUTHENTICATED;
150 else
151 return DBUS_AUTH_STATE_INVALID;
152 }
153
154 static const char*
155 auth_state_to_string (DBusAuthState state)
156 {
157 switch (state)
158 {
159 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
160 return "WAITING_FOR_INPUT";
161 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
162 return "WAITING_FOR_MEMORY";
163 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
164 return "HAVE_BYTES_TO_SEND";
165 case DBUS_AUTH_STATE_NEED_DISCONNECT:
166 return "NEED_DISCONNECT";
167 case DBUS_AUTH_STATE_AUTHENTICATED:
168 return "AUTHENTICATED";
169 case DBUS_AUTH_STATE_INVALID:
170 return "INVALID";
171 default:
172 break;
173 }
174
175 return "unknown";
176 }
177
178 static char **
179 split_string (DBusString *str)
180 {
181 int i, j, k, count, end;
182 char **array;
183
184 end = _dbus_string_get_length (str);
185
186 i = 0;
187 _dbus_string_skip_blank (str, i, &i);
188 for (count = 0; i < end; count++)
189 {
190 _dbus_string_find_blank (str, i, &i);
191 _dbus_string_skip_blank (str, i, &i);
192 }
193
194 array = dbus_new0 (char *, count + 1);
195 if (array == NULL)
196 return NULL;
197
198 i = 0;
199 _dbus_string_skip_blank (str, i, &i);
200 for (k = 0; k < count; k++)
201 {
202 _dbus_string_find_blank (str, i, &j);
203
204 array[k] = dbus_malloc (j - i + 1);
205 if (array[k] == NULL)
206 {
207 dbus_free_string_array (array);
208 return NULL;
209 }
210 memcpy (array[k],
211 _dbus_string_get_const_data_len (str, i, j - i), j - i);
212 array[k][j - i] = '\0';
213
214 _dbus_string_skip_blank (str, j, &i);
215 }
216 array[k] = NULL;
217
218 return array;
219 }
220
221 static void
222 auth_set_unix_credentials(DBusAuth *auth,
223 dbus_uid_t uid,
224 dbus_pid_t pid)
225 {
226 DBusCredentials *credentials;
227
228 credentials = _dbus_credentials_new ();
229 if (credentials == NULL)
230 _dbus_assert_not_reached ("no memory");
231
232 if (uid != DBUS_UID_UNSET)
233 {
234 if (!_dbus_credentials_add_unix_uid (credentials, uid))
235 _dbus_assert_not_reached ("no memory");
236 }
237 if (pid != DBUS_PID_UNSET)
238 {
239 if (!_dbus_credentials_add_pid (credentials, pid))
240 _dbus_assert_not_reached ("no memory");
241 }
242 _dbus_auth_set_credentials (auth, credentials);
243
244 _dbus_credentials_unref (credentials);
245 }
246
257 dbus_bool_t
258 _dbus_auth_script_run (const DBusString *filename)
259 {
260 DBusString file;
261 DBusError error = DBUS_ERROR_INIT;
262 DBusString line;
263 dbus_bool_t retval;
264 int line_no;
265 DBusAuth *auth;
266 DBusString from_auth;
267 DBusAuthState state;
268 DBusString context;
269 DBusString guid;
270
271 retval = FALSE;
272 auth = NULL;
273
274 _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
275 _dbus_string_init_const (&context, "org_freedesktop_test");
276
277 if (!_dbus_string_init (&file))
278 return FALSE;
279
280 if (!_dbus_string_init (&line))
281 {
282 _dbus_string_free (&file);
283 return FALSE;
284 }
285
286 if (!_dbus_string_init (&from_auth))
287 {
288 _dbus_string_free (&file);
289 _dbus_string_free (&line);
290 return FALSE;
291 }
292
293 if (!_dbus_file_get_contents (&file, filename, &error)) {
294 _dbus_warn ("Getting contents of %s failed: %s",
295 _dbus_string_get_const_data (filename), error.message);
296 dbus_error_free (&error);
297 goto out;
298 }
299
300 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
301 line_no = 0;
302
303 next_iteration:
304 while (_dbus_string_pop_line (&file, &line))
305 {
306 line_no += 1;
307
308 /* _dbus_warn ("%s", _dbus_string_get_const_data (&line)); */
309
310 _dbus_string_delete_leading_blanks (&line);
311
312 if (auth != NULL)
313 {
314 while ((state = _dbus_auth_do_work (auth)) ==
315 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
316 {
317 const DBusString *tmp;
318 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
319 {
320 int count = _dbus_string_get_length (tmp);
321
322 if (_dbus_string_copy (tmp, 0, &from_auth,
323 _dbus_string_get_length (&from_auth)))
324 _dbus_auth_bytes_sent (auth, count);
325 }
326 }
327 }
328
329 if (_dbus_string_get_length (&line) == 0)
330 {
331 /* empty line */
332 goto next_iteration;
333 }
334 else if (_dbus_string_starts_with_c_str (&line,
335 "#"))
336 {
337 /* Ignore this comment */
338 goto next_iteration;
339 }
340 #ifdef DBUS_WIN
341 else if (_dbus_string_starts_with_c_str (&line,
342 "WIN_ONLY"))
343 {
344 /* Ignore this line */
345 goto next_iteration;
346 }
347 else if (_dbus_string_starts_with_c_str (&line,
348 "UNIX_ONLY"))
349 {
350 /* skip this file */
351 fprintf (stderr, "skipping unix only auth script\n");
352 retval = TRUE;
353 goto out;
354 }
355 #endif
356 #ifdef DBUS_UNIX
357 else if (_dbus_string_starts_with_c_str (&line,
358 "UNIX_ONLY"))
359 {
360 /* Ignore this line */
361 goto next_iteration;
362 }
363 else if (_dbus_string_starts_with_c_str (&line,
364 "WIN_ONLY"))
365 {
366 /* skip this file */
367 fprintf (stderr, "skipping windows only auth script\n");
368 retval = TRUE;
369 goto out;
370 }
371 #endif
372 else if (_dbus_string_starts_with_c_str (&line,
373 "CLIENT"))
374 {
375 DBusCredentials *creds;
376
377 if (auth != NULL)
378 {
379 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)");
380 goto out;
381 }
382
383 auth = _dbus_auth_client_new ();
384 if (auth == NULL)
385 {
386 _dbus_warn ("no memory to create DBusAuth");
387 goto out;
388 }
389
390 /* test ref/unref */
391 _dbus_auth_ref (auth);
392 _dbus_auth_unref (auth);
393
394 creds = _dbus_credentials_new_from_current_process ();
395 if (creds == NULL)
396 {
397 _dbus_warn ("no memory for credentials");
398 _dbus_auth_unref (auth);
399 auth = NULL;
400 goto out;
401 }
402
403 if (!_dbus_auth_set_credentials (auth, creds))
404 {
405 _dbus_warn ("no memory for setting credentials");
406 _dbus_auth_unref (auth);
407 auth = NULL;
408 _dbus_credentials_unref (creds);
409 goto out;
410 }
411
412 _dbus_credentials_unref (creds);
413 }
414 else if (_dbus_string_starts_with_c_str (&line,
415 "SERVER"))
416 {
417 DBusCredentials *creds;
418
419 if (auth != NULL)
420 {
421 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)");
422 goto out;
423 }
424
425 auth = _dbus_auth_server_new (&guid);
426 if (auth == NULL)
427 {
428 _dbus_warn ("no memory to create DBusAuth");
429 goto out;
430 }
431
432 /* test ref/unref */
433 _dbus_auth_ref (auth);
434 _dbus_auth_unref (auth);
435
436 creds = _dbus_credentials_new_from_current_process ();
437 if (creds == NULL)
438 {
439 _dbus_warn ("no memory for credentials");
440 _dbus_auth_unref (auth);
441 auth = NULL;
442 goto out;
443 }
444
445 if (!_dbus_auth_set_credentials (auth, creds))
446 {
447 _dbus_warn ("no memory for setting credentials");
448 _dbus_auth_unref (auth);
449 auth = NULL;
450 _dbus_credentials_unref (creds);
451 goto out;
452 }
453
454 _dbus_credentials_unref (creds);
455
456 _dbus_auth_set_context (auth, &context);
457 }
458 else if (auth == NULL)
459 {
460 _dbus_warn ("must specify CLIENT or SERVER");
461 goto out;
462
463 }
464 else if (_dbus_string_starts_with_c_str (&line,
465 "NO_CREDENTIALS"))
466 {
467 auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
468 }
469 else if (_dbus_string_starts_with_c_str (&line,
470 "ROOT_CREDENTIALS"))
471 {
472 auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
473 }
474 else if (_dbus_string_starts_with_c_str (&line,
475 "SILLY_CREDENTIALS"))
476 {
477 auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
478 }
479 else if (_dbus_string_starts_with_c_str (&line,
480 "ALLOWED_MECHS"))
481 {
482 char **mechs;
483
484 _dbus_string_delete_first_word (&line);
485 mechs = split_string (&line);
486 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
487 dbus_free_string_array (mechs);
488 }
489 else if (_dbus_string_starts_with_c_str (&line,
490 "SEND"))
491 {
492 DBusString to_send;
493
494 _dbus_string_delete_first_word (&line);
495
496 if (!_dbus_string_init (&to_send))
497 {
498 _dbus_warn ("no memory to allocate string");
499 goto out;
500 }
501
502 if (!append_quoted_string (&to_send, &line))
503 {
504 _dbus_warn ("failed to append quoted string line %d",
505 line_no);
506 _dbus_string_free (&to_send);
507 goto out;
508 }
509
510 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
511
512 if (!_dbus_string_append (&to_send, "\r\n"))
513 {
514 _dbus_warn ("failed to append \\r\\n from line %d",
515 line_no);
516 _dbus_string_free (&to_send);
517 goto out;
518 }
519
520 /* Replace USERID_HEX with our username in hex */
521 {
522 int where;
523
524 if (_dbus_string_find (&to_send, 0, "WRONG_USERID_HEX", &where))
525 {
526 /* This must be checked for before USERID_HEX, because
527 * that's a substring. */
528 DBusString uid;
529
530 if (!_dbus_string_init (&uid))
531 {
532 _dbus_warn ("no memory for uid");
533 _dbus_string_free (&to_send);
534 goto out;
535 }
536
537 if (!_dbus_test_append_different_uid (&uid))
538 {
539 _dbus_warn ("no memory for uid");
540 _dbus_string_free (&to_send);
541 _dbus_string_free (&uid);
542 goto out;
543 }
544
545 _dbus_string_delete (&to_send, where,
546 (int) strlen ("WRONG_USERID_HEX"));
547
548 if (!_dbus_string_hex_encode (&uid, 0, &to_send, where))
549 {
550 _dbus_warn ("no memory to subst WRONG_USERID_HEX");
551 _dbus_string_free (&to_send);
552 _dbus_string_free (&uid);
553 goto out;
554 }
555
556 _dbus_string_free (&uid);
557 }
558 else if (_dbus_string_find (&to_send, 0,
559 "USERID_HEX", &where))
560 {
561 DBusString username;
562
563 if (!_dbus_string_init (&username))
564 {
565 _dbus_warn ("no memory for userid");
566 _dbus_string_free (&to_send);
567 goto out;
568 }
569
570 if (!_dbus_append_user_from_current_process (&username))
571 {
572 _dbus_warn ("no memory for userid");
573 _dbus_string_free (&username);
574 _dbus_string_free (&to_send);
575 goto out;
576 }
577
578 _dbus_string_delete (&to_send, where, (int) strlen ("USERID_HEX"));
579
580 if (!_dbus_string_hex_encode (&username, 0,
581 &to_send, where))
582 {
583 _dbus_warn ("no memory to subst USERID_HEX");
584 _dbus_string_free (&username);
585 _dbus_string_free (&to_send);
586 goto out;
587 }
588
589 _dbus_string_free (&username);
590 }
591 else if (_dbus_string_find (&to_send, 0,
592 "WRONG_USERNAME_HEX", &where))
593 {
594 /* This must be checked for before USERNAME_HEX, because
595 * that's a substring. */
596 #ifdef DBUS_UNIX
597 DBusString username;
598
599 if (!_dbus_string_init (&username))
600 {
601 _dbus_warn ("no memory for username");
602 _dbus_string_free (&to_send);
603 goto out;
604 }
605
606 if (!_dbus_test_append_different_username (&username))
607 {
608 _dbus_warn ("no memory for username");
609 _dbus_string_free (&to_send);
610 _dbus_string_free (&username);
611 goto out;
612 }
613
614 _dbus_string_delete (&to_send, where,
615 (int) strlen ("WRONG_USERNAME_HEX"));
616
617 if (!_dbus_string_hex_encode (&username, 0,
618 &to_send, where))
619 {
620 _dbus_warn ("no memory to subst WRONG_USERNAME_HEX");
621 _dbus_string_free (&to_send);
622 _dbus_string_free (&username);
623 goto out;
624 }
625
626 _dbus_string_free (&username);
627 #else
628 /* No authentication mechanism uses the login name on
629 * Windows, so there's no point in it appearing in an
630 * auth script that is not UNIX_ONLY. */
631 _dbus_warn ("WRONG_USERNAME_HEX cannot be used on Windows");
632 _dbus_string_free (&to_send);
633 goto out;
634 #endif
635 }
636 else if (_dbus_string_find (&to_send, 0,
637 "USERNAME_HEX", &where))
638 {
639 DBusString username;
640
641 if (!_dbus_string_init (&username))
642 {
643 _dbus_warn ("no memory for username");
644 _dbus_string_free (&to_send);
645 goto out;
646 }
647
648 if (!_dbus_append_user_from_current_process (&username))
649 {
650 _dbus_warn ("no memory for username");
651 _dbus_string_free (&username);
652 _dbus_string_free (&to_send);
653 goto out;
654 }
655
656 _dbus_string_delete (&to_send, where, (int) strlen ("USERNAME_HEX"));
657
658 if (!_dbus_string_hex_encode (&username, 0,
659 &to_send, where))
660 {
661 _dbus_warn ("no memory to subst USERNAME_HEX");
662 _dbus_string_free (&username);
663 _dbus_string_free (&to_send);
664 goto out;
665 }
666
667 _dbus_string_free (&username);
668 }
669 }
670
671 {
672 DBusString *buffer;
673
674 _dbus_auth_get_buffer (auth, &buffer);
675 if (!_dbus_string_copy (&to_send, 0,
676 buffer, _dbus_string_get_length (buffer)))
677 {
678 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state");
679 _dbus_string_free (&to_send);
680 _dbus_auth_return_buffer (auth, buffer);
681 goto out;
682 }
683
684 _dbus_auth_return_buffer (auth, buffer);
685 }
686
687 _dbus_string_free (&to_send);
688 }
689 else if (_dbus_string_starts_with_c_str (&line,
690 "EXPECT_STATE"))
691 {
692 DBusAuthState expected;
693
694 _dbus_string_delete_first_word (&line);
695
696 expected = auth_state_from_string (&line);
697 if (expected < 0)
698 {
699 _dbus_warn ("bad auth state given to EXPECT_STATE");
700 goto parse_failed;
701 }
702
703 if (expected != state)
704 {
705 _dbus_warn ("expected auth state %s but got %s on line %d",
706 auth_state_to_string (expected),
707 auth_state_to_string (state),
708 line_no);
709 goto out;
710 }
711 }
712 else if (_dbus_string_starts_with_c_str (&line,
713 "EXPECT_COMMAND"))
714 {
715 DBusString received;
716
717 _dbus_string_delete_first_word (&line);
718
719 if (!_dbus_string_init (&received))
720 {
721 _dbus_warn ("no mem to allocate string received");
722 goto out;
723 }
724
725 if (!_dbus_string_pop_line (&from_auth, &received))
726 {
727 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d",
728 _dbus_string_get_const_data (&line), line_no);
729 _dbus_string_free (&received);
730 goto out;
731 }
732
733 if (!same_first_word (&received, &line))
734 {
735 _dbus_warn ("line %d expected command '%s' and got '%s'",
736 line_no,
737 _dbus_string_get_const_data (&line),
738 _dbus_string_get_const_data (&received));
739 _dbus_string_free (&received);
740 goto out;
741 }
742
743 _dbus_string_free (&received);
744 }
745 else if (_dbus_string_starts_with_c_str (&line,
746 "EXPECT_UNUSED"))
747 {
748 DBusString expected;
749 const DBusString *unused;
750
751 _dbus_string_delete_first_word (&line);
752
753 if (!_dbus_string_init (&expected))
754 {
755 _dbus_warn ("no mem to allocate string expected");
756 goto out;
757 }
758
759 if (!append_quoted_string (&expected, &line))
760 {
761 _dbus_warn ("failed to append quoted string line %d",
762 line_no);
763 _dbus_string_free (&expected);
764 goto out;
765 }
766
767 _dbus_auth_get_unused_bytes (auth, &unused);
768
769 if (_dbus_string_equal (&expected, unused))
770 {
771 _dbus_auth_delete_unused_bytes (auth);
772 _dbus_string_free (&expected);
773 }
774 else
775 {
776 _dbus_warn ("Expected unused bytes '%s' and have '%s'",
777 _dbus_string_get_const_data (&expected),
778 _dbus_string_get_const_data (unused));
779 _dbus_string_free (&expected);
780 goto out;
781 }
782 }
783 else if (_dbus_string_starts_with_c_str (&line,
784 "EXPECT_HAVE_NO_CREDENTIALS"))
785 {
786 DBusCredentials *authorized_identity;
787
788 authorized_identity = _dbus_auth_get_identity (auth);
789 if (!_dbus_credentials_are_anonymous (authorized_identity))
790 {
791 _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized");
792 goto out;
793 }
794 }
795 else if (_dbus_string_starts_with_c_str (&line,
796 "EXPECT_HAVE_SOME_CREDENTIALS"))
797 {
798 DBusCredentials *authorized_identity;
799
800 authorized_identity = _dbus_auth_get_identity (auth);
801 if (_dbus_credentials_are_anonymous (authorized_identity))
802 {
803 _dbus_warn ("Expected to have some credentials, but we don't");
804 goto out;
805 }
806 }
807 else if (_dbus_string_starts_with_c_str (&line,
808 "EXPECT"))
809 {
810 DBusString expected;
811
812 _dbus_string_delete_first_word (&line);
813
814 if (!_dbus_string_init (&expected))
815 {
816 _dbus_warn ("no mem to allocate string expected");
817 goto out;
818 }
819
820 if (!append_quoted_string (&expected, &line))
821 {
822 _dbus_warn ("failed to append quoted string line %d",
823 line_no);
824 _dbus_string_free (&expected);
825 goto out;
826 }
827
828 if (_dbus_string_equal_len (&expected, &from_auth,
829 _dbus_string_get_length (&expected)))
830 {
831 _dbus_string_delete (&from_auth, 0,
832 _dbus_string_get_length (&expected));
833 _dbus_string_free (&expected);
834 }
835 else
836 {
837 _dbus_warn ("Expected exact string '%s' and have '%s'",
838 _dbus_string_get_const_data (&expected),
839 _dbus_string_get_const_data (&from_auth));
840 _dbus_string_free (&expected);
841 goto out;
842 }
843 }
844 else
845 goto parse_failed;
846
847 goto next_iteration; /* skip parse_failed */
848
849 parse_failed:
850 {
851 _dbus_warn ("couldn't process line %d \"%s\"",
852 line_no, _dbus_string_get_const_data (&line));
853 goto out;
854 }
855 }
856
857 if (auth == NULL)
858 {
859 _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER");
860 goto out;
861 }
862 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
863 {
864 const DBusString *unused;
865
866 _dbus_auth_get_unused_bytes (auth, &unused);
867
868 if (_dbus_string_get_length (unused) > 0)
869 {
870 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)");
871 goto out;
872 }
873 }
874
875 if (_dbus_string_get_length (&from_auth) > 0)
876 {
877 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth");
878 _dbus_warn ("Leftover data: %s", _dbus_string_get_const_data (&from_auth));
879 goto out;
880 }
881
882 retval = TRUE;
883
884 out:
885 if (auth)
886 _dbus_auth_unref (auth);
887
888 _dbus_string_free (&file);
889 _dbus_string_free (&line);
890 _dbus_string_free (&from_auth);
891
892 return retval;
893 }
894
896 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
_dbus_auth_set_context
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:2843
_dbus_auth_return_buffer
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2607
_dbus_auth_do_work
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:2500
_dbus_string_copy
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:1283
_dbus_file_get_contents
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
Definition: dbus-file-unix.c:55
_dbus_string_equal_len
dbus_bool_t _dbus_string_equal_len(const DBusString *a, const DBusString *b, int len)
Tests two DBusString for equality up to the given length.
Definition: dbus-string.c:2056
_dbus_string_append_byte
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1157
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
_dbus_auth_set_mechanisms
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:2465
_dbus_auth_get_buffer
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:2589
TRUE
#define TRUE
_dbus_string_find
dbus_bool_t _dbus_string_find(const DBusString *str, int start, const char *substr, int *found)
Finds the given substring in the string, returning TRUE and filling in the byte index where the subst...
Definition: dbus-string.c:1604
_dbus_auth_bytes_sent
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:2569
_dbus_auth_server_new
DBusAuth * _dbus_auth_server_new(const DBusString *guid)
Creates a new auth conversation object for the server side.
Definition: dbus-auth.c:2316
_dbus_auth_get_identity
DBusCredentials * _dbus_auth_get_identity(DBusAuth *auth)
Gets the identity we authorized the client as.
Definition: dbus-auth.c:2800
dbus_malloc
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:462
DBusString
Definition: dbus-string.h:43
_dbus_credentials_add_pid
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
Definition: dbus-credentials.c:152
_dbus_credentials_new_from_current_process
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with credentials (user ID and process ID) from the current process.
Definition: dbus-credentials.c:95
_dbus_auth_ref
DBusAuth * _dbus_auth_ref(DBusAuth *auth)
Increments the refcount of an auth object.
Definition: dbus-auth.c:2400
DBusCredentials
Definition: dbus-credentials.c:48
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:132
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
_dbus_string_find_blank
dbus_bool_t _dbus_string_find_blank(const DBusString *str, int start, int *found)
Finds a blank (space or tab) in the string.
Definition: dbus-string.c:1765
FALSE
#define FALSE
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Definition: dbus-errors.h:62
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:141
DBusAuth
Internal members of DBusAuth.
Definition: dbus-auth.c:154
_dbus_string_pop_line
dbus_bool_t _dbus_string_pop_line(DBusString *source, DBusString *dest)
Assigns a newline-terminated or \r\n-terminated line from the front of the string to the given dest s...
Definition: dbus-string.c:1909
DBUS_PID_UNSET
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:139
_dbus_string_delete
void _dbus_string_delete(DBusString *str, int start, int len)
Deletes a segment of a DBusString with length len starting at start.
Definition: dbus-string.c:1193
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Definition: dbus-internals.h:163
dbus_error_free
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_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition: dbus-internals.c:230
_dbus_auth_set_credentials
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:2782
_dbus_auth_get_bytes_to_send
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:2544
_dbus_credentials_are_anonymous
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
Definition: dbus-credentials.c:408
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
_dbus_string_skip_blank
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition: dbus-string.c:1803
_dbus_string_hex_encode
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2259
_dbus_append_user_from_current_process
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...
Definition: dbus-sysdeps-unix.c:2741
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition: dbus-credentials.c:130
dbus_free_string_array
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:750
_dbus_auth_unref
void _dbus_auth_unref(DBusAuth *auth)
Decrements the refcount of an auth object.
Definition: dbus-auth.c:2415
_dbus_string_starts_with_c_str
dbus_bool_t _dbus_string_starts_with_c_str(const DBusString *a, const char *c_str)
Checks whether a string starts with the given C string.
Definition: dbus-string.c:2188
DBusError::message
const char * message
public error message field
Definition: dbus-errors.h:51
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:935
_dbus_auth_delete_unused_bytes
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:2643
_dbus_auth_client_new
DBusAuth * _dbus_auth_client_new(void)
Creates a new auth conversation object for the client side.
Definition: dbus-auth.c:2362
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
_dbus_auth_get_unused_bytes
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:2626
_dbus_credentials_new
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
Definition: dbus-credentials.c:71
NULL
#define NULL
_dbus_string_equal
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2013
_dbus_credentials_add_unix_uid
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
Definition: dbus-credentials.c:167