D-Bus  1.10.28
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\n", 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 -1;
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  }
170 
171  return "unknown";
172 }
173 
174 static char **
175 split_string (DBusString *str)
176 {
177  int i, j, k, count, end;
178  char **array;
179 
180  end = _dbus_string_get_length (str);
181 
182  i = 0;
183  _dbus_string_skip_blank (str, i, &i);
184  for (count = 0; i < end; count++)
185  {
186  _dbus_string_find_blank (str, i, &i);
187  _dbus_string_skip_blank (str, i, &i);
188  }
189 
190  array = dbus_new0 (char *, count + 1);
191  if (array == NULL)
192  return NULL;
193 
194  i = 0;
195  _dbus_string_skip_blank (str, i, &i);
196  for (k = 0; k < count; k++)
197  {
198  _dbus_string_find_blank (str, i, &j);
199 
200  array[k] = dbus_malloc (j - i + 1);
201  if (array[k] == NULL)
202  {
203  dbus_free_string_array (array);
204  return NULL;
205  }
206  memcpy (array[k],
207  _dbus_string_get_const_data_len (str, i, j - i), j - i);
208  array[k][j - i] = '\0';
209 
210  _dbus_string_skip_blank (str, j, &i);
211  }
212  array[k] = NULL;
213 
214  return array;
215 }
216 
217 static void
218 auth_set_unix_credentials(DBusAuth *auth,
219  dbus_uid_t uid,
220  dbus_pid_t pid)
221 {
222  DBusCredentials *credentials;
223 
224  credentials = _dbus_credentials_new ();
225  if (credentials == NULL)
226  _dbus_assert_not_reached ("no memory");
227 
228  if (uid != DBUS_UID_UNSET)
229  {
230  if (!_dbus_credentials_add_unix_uid (credentials, uid))
231  _dbus_assert_not_reached ("no memory");
232  }
233  if (pid != DBUS_PID_UNSET)
234  {
235  if (!_dbus_credentials_add_pid (credentials, pid))
236  _dbus_assert_not_reached ("no memory");
237  }
238  _dbus_auth_set_credentials (auth, credentials);
239 
240  _dbus_credentials_unref (credentials);
241 }
242 
254 _dbus_auth_script_run (const DBusString *filename)
255 {
256  DBusString file;
257  DBusError error = DBUS_ERROR_INIT;
258  DBusString line;
259  dbus_bool_t retval;
260  int line_no;
261  DBusAuth *auth;
262  DBusString from_auth;
263  DBusAuthState state;
264  DBusString context;
265  DBusString guid;
266 
267  retval = FALSE;
268  auth = NULL;
269 
270  _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
271  _dbus_string_init_const (&context, "org_freedesktop_test");
272 
273  if (!_dbus_string_init (&file))
274  return FALSE;
275 
276  if (!_dbus_string_init (&line))
277  {
278  _dbus_string_free (&file);
279  return FALSE;
280  }
281 
282  if (!_dbus_string_init (&from_auth))
283  {
284  _dbus_string_free (&file);
285  _dbus_string_free (&line);
286  return FALSE;
287  }
288 
289  if (!_dbus_file_get_contents (&file, filename, &error)) {
290  _dbus_warn ("Getting contents of %s failed: %s\n",
291  _dbus_string_get_const_data (filename), error.message);
292  dbus_error_free (&error);
293  goto out;
294  }
295 
296  state = DBUS_AUTH_STATE_NEED_DISCONNECT;
297  line_no = 0;
298 
299  next_iteration:
300  while (_dbus_string_pop_line (&file, &line))
301  {
302  line_no += 1;
303 
304  /* _dbus_warn ("%s\n", _dbus_string_get_const_data (&line)); */
305 
306  _dbus_string_delete_leading_blanks (&line);
307 
308  if (auth != NULL)
309  {
310  while ((state = _dbus_auth_do_work (auth)) ==
311  DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
312  {
313  const DBusString *tmp;
314  if (_dbus_auth_get_bytes_to_send (auth, &tmp))
315  {
316  int count = _dbus_string_get_length (tmp);
317 
318  if (_dbus_string_copy (tmp, 0, &from_auth,
319  _dbus_string_get_length (&from_auth)))
320  _dbus_auth_bytes_sent (auth, count);
321  }
322  }
323  }
324 
325  if (_dbus_string_get_length (&line) == 0)
326  {
327  /* empty line */
328  goto next_iteration;
329  }
330  else if (_dbus_string_starts_with_c_str (&line,
331  "#"))
332  {
333  /* Ignore this comment */
334  goto next_iteration;
335  }
336 #ifdef DBUS_WIN
337  else if (_dbus_string_starts_with_c_str (&line,
338  "WIN_ONLY"))
339  {
340  /* Ignore this line */
341  goto next_iteration;
342  }
343  else if (_dbus_string_starts_with_c_str (&line,
344  "UNIX_ONLY"))
345  {
346  /* skip this file */
347  fprintf (stderr, "skipping unix only auth script\n");
348  retval = TRUE;
349  goto out;
350  }
351 #endif
352 #ifdef DBUS_UNIX
353  else if (_dbus_string_starts_with_c_str (&line,
354  "UNIX_ONLY"))
355  {
356  /* Ignore this line */
357  goto next_iteration;
358  }
359  else if (_dbus_string_starts_with_c_str (&line,
360  "WIN_ONLY"))
361  {
362  /* skip this file */
363  fprintf (stderr, "skipping windows only auth script\n");
364  retval = TRUE;
365  goto out;
366  }
367 #endif
368  else if (_dbus_string_starts_with_c_str (&line,
369  "CLIENT"))
370  {
371  DBusCredentials *creds;
372 
373  if (auth != NULL)
374  {
375  _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
376  goto out;
377  }
378 
379  auth = _dbus_auth_client_new ();
380  if (auth == NULL)
381  {
382  _dbus_warn ("no memory to create DBusAuth\n");
383  goto out;
384  }
385 
386  /* test ref/unref */
387  _dbus_auth_ref (auth);
388  _dbus_auth_unref (auth);
389 
391  if (creds == NULL)
392  {
393  _dbus_warn ("no memory for credentials\n");
394  _dbus_auth_unref (auth);
395  auth = NULL;
396  goto out;
397  }
398 
399  if (!_dbus_auth_set_credentials (auth, creds))
400  {
401  _dbus_warn ("no memory for setting credentials\n");
402  _dbus_auth_unref (auth);
403  auth = NULL;
404  _dbus_credentials_unref (creds);
405  goto out;
406  }
407 
408  _dbus_credentials_unref (creds);
409  }
410  else if (_dbus_string_starts_with_c_str (&line,
411  "SERVER"))
412  {
413  DBusCredentials *creds;
414 
415  if (auth != NULL)
416  {
417  _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
418  goto out;
419  }
420 
421  auth = _dbus_auth_server_new (&guid);
422  if (auth == NULL)
423  {
424  _dbus_warn ("no memory to create DBusAuth\n");
425  goto out;
426  }
427 
428  /* test ref/unref */
429  _dbus_auth_ref (auth);
430  _dbus_auth_unref (auth);
431 
433  if (creds == NULL)
434  {
435  _dbus_warn ("no memory for credentials\n");
436  _dbus_auth_unref (auth);
437  auth = NULL;
438  goto out;
439  }
440 
441  if (!_dbus_auth_set_credentials (auth, creds))
442  {
443  _dbus_warn ("no memory for setting credentials\n");
444  _dbus_auth_unref (auth);
445  auth = NULL;
446  _dbus_credentials_unref (creds);
447  goto out;
448  }
449 
450  _dbus_credentials_unref (creds);
451 
452  _dbus_auth_set_context (auth, &context);
453  }
454  else if (auth == NULL)
455  {
456  _dbus_warn ("must specify CLIENT or SERVER\n");
457  goto out;
458 
459  }
460  else if (_dbus_string_starts_with_c_str (&line,
461  "NO_CREDENTIALS"))
462  {
463  auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
464  }
465  else if (_dbus_string_starts_with_c_str (&line,
466  "ROOT_CREDENTIALS"))
467  {
468  auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
469  }
470  else if (_dbus_string_starts_with_c_str (&line,
471  "SILLY_CREDENTIALS"))
472  {
473  auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
474  }
475  else if (_dbus_string_starts_with_c_str (&line,
476  "ALLOWED_MECHS"))
477  {
478  char **mechs;
479 
480  _dbus_string_delete_first_word (&line);
481  mechs = split_string (&line);
482  _dbus_auth_set_mechanisms (auth, (const char **) mechs);
483  dbus_free_string_array (mechs);
484  }
485  else if (_dbus_string_starts_with_c_str (&line,
486  "SEND"))
487  {
488  DBusString to_send;
489 
490  _dbus_string_delete_first_word (&line);
491 
492  if (!_dbus_string_init (&to_send))
493  {
494  _dbus_warn ("no memory to allocate string\n");
495  goto out;
496  }
497 
498  if (!append_quoted_string (&to_send, &line))
499  {
500  _dbus_warn ("failed to append quoted string line %d\n",
501  line_no);
502  _dbus_string_free (&to_send);
503  goto out;
504  }
505 
506  _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
507 
508  if (!_dbus_string_append (&to_send, "\r\n"))
509  {
510  _dbus_warn ("failed to append \r\n from line %d\n",
511  line_no);
512  _dbus_string_free (&to_send);
513  goto out;
514  }
515 
516  /* Replace USERID_HEX with our username in hex */
517  {
518  int where;
519 
520  if (_dbus_string_find (&to_send, 0, "WRONG_USERID_HEX", &where))
521  {
522  /* This must be checked for before USERID_HEX, because
523  * that's a substring. */
524  DBusString uid;
525 
526  if (!_dbus_string_init (&uid))
527  {
528  _dbus_warn ("no memory for uid");
529  _dbus_string_free (&to_send);
530  goto out;
531  }
532 
533  if (!_dbus_test_append_different_uid (&uid))
534  {
535  _dbus_warn ("no memory for uid");
536  _dbus_string_free (&to_send);
537  _dbus_string_free (&uid);
538  goto out;
539  }
540 
541  _dbus_string_delete (&to_send, where,
542  (int) strlen ("WRONG_USERID_HEX"));
543 
544  if (!_dbus_string_hex_encode (&uid, 0, &to_send, where))
545  {
546  _dbus_warn ("no memory to subst WRONG_USERID_HEX");
547  _dbus_string_free (&to_send);
548  _dbus_string_free (&uid);
549  goto out;
550  }
551 
552  _dbus_string_free (&uid);
553  }
554  else if (_dbus_string_find (&to_send, 0,
555  "USERID_HEX", &where))
556  {
557  DBusString username;
558 
559  if (!_dbus_string_init (&username))
560  {
561  _dbus_warn ("no memory for userid\n");
562  _dbus_string_free (&to_send);
563  goto out;
564  }
565 
567  {
568  _dbus_warn ("no memory for userid\n");
569  _dbus_string_free (&username);
570  _dbus_string_free (&to_send);
571  goto out;
572  }
573 
574  _dbus_string_delete (&to_send, where, (int) strlen ("USERID_HEX"));
575 
576  if (!_dbus_string_hex_encode (&username, 0,
577  &to_send, where))
578  {
579  _dbus_warn ("no memory to subst USERID_HEX\n");
580  _dbus_string_free (&username);
581  _dbus_string_free (&to_send);
582  goto out;
583  }
584 
585  _dbus_string_free (&username);
586  }
587  else if (_dbus_string_find (&to_send, 0,
588  "WRONG_USERNAME_HEX", &where))
589  {
590  /* This must be checked for before USERNAME_HEX, because
591  * that's a substring. */
592 #ifdef DBUS_UNIX
593  DBusString username;
594 
595  if (!_dbus_string_init (&username))
596  {
597  _dbus_warn ("no memory for username");
598  _dbus_string_free (&to_send);
599  goto out;
600  }
601 
602  if (!_dbus_test_append_different_username (&username))
603  {
604  _dbus_warn ("no memory for username");
605  _dbus_string_free (&to_send);
606  _dbus_string_free (&username);
607  goto out;
608  }
609 
610  _dbus_string_delete (&to_send, where,
611  (int) strlen ("WRONG_USERNAME_HEX"));
612 
613  if (!_dbus_string_hex_encode (&username, 0,
614  &to_send, where))
615  {
616  _dbus_warn ("no memory to subst WRONG_USERNAME_HEX");
617  _dbus_string_free (&to_send);
618  _dbus_string_free (&username);
619  goto out;
620  }
621 
622  _dbus_string_free (&username);
623 #else
624  /* No authentication mechanism uses the login name on
625  * Windows, so there's no point in it appearing in an
626  * auth script that is not UNIX_ONLY. */
627  _dbus_warn ("WRONG_USERNAME_HEX cannot be used on Windows");
628  _dbus_string_free (&to_send);
629  goto out;
630 #endif
631  }
632  else if (_dbus_string_find (&to_send, 0,
633  "USERNAME_HEX", &where))
634  {
635  DBusString username;
636 
637  if (!_dbus_string_init (&username))
638  {
639  _dbus_warn ("no memory for username\n");
640  _dbus_string_free (&to_send);
641  goto out;
642  }
643 
645  {
646  _dbus_warn ("no memory for username\n");
647  _dbus_string_free (&username);
648  _dbus_string_free (&to_send);
649  goto out;
650  }
651 
652  _dbus_string_delete (&to_send, where, (int) strlen ("USERNAME_HEX"));
653 
654  if (!_dbus_string_hex_encode (&username, 0,
655  &to_send, where))
656  {
657  _dbus_warn ("no memory to subst USERNAME_HEX\n");
658  _dbus_string_free (&username);
659  _dbus_string_free (&to_send);
660  goto out;
661  }
662 
663  _dbus_string_free (&username);
664  }
665  }
666 
667  {
668  DBusString *buffer;
669 
670  _dbus_auth_get_buffer (auth, &buffer);
671  if (!_dbus_string_copy (&to_send, 0,
672  buffer, _dbus_string_get_length (buffer)))
673  {
674  _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
675  _dbus_string_free (&to_send);
676  _dbus_auth_return_buffer (auth, buffer);
677  goto out;
678  }
679 
680  _dbus_auth_return_buffer (auth, buffer);
681  }
682 
683  _dbus_string_free (&to_send);
684  }
685  else if (_dbus_string_starts_with_c_str (&line,
686  "EXPECT_STATE"))
687  {
688  DBusAuthState expected;
689 
690  _dbus_string_delete_first_word (&line);
691 
692  expected = auth_state_from_string (&line);
693  if (expected < 0)
694  {
695  _dbus_warn ("bad auth state given to EXPECT_STATE\n");
696  goto parse_failed;
697  }
698 
699  if (expected != state)
700  {
701  _dbus_warn ("expected auth state %s but got %s on line %d\n",
702  auth_state_to_string (expected),
703  auth_state_to_string (state),
704  line_no);
705  goto out;
706  }
707  }
708  else if (_dbus_string_starts_with_c_str (&line,
709  "EXPECT_COMMAND"))
710  {
711  DBusString received;
712 
713  _dbus_string_delete_first_word (&line);
714 
715  if (!_dbus_string_init (&received))
716  {
717  _dbus_warn ("no mem to allocate string received\n");
718  goto out;
719  }
720 
721  if (!_dbus_string_pop_line (&from_auth, &received))
722  {
723  _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
724  _dbus_string_get_const_data (&line), line_no);
725  _dbus_string_free (&received);
726  goto out;
727  }
728 
729  if (!same_first_word (&received, &line))
730  {
731  _dbus_warn ("line %d expected command '%s' and got '%s'\n",
732  line_no,
733  _dbus_string_get_const_data (&line),
734  _dbus_string_get_const_data (&received));
735  _dbus_string_free (&received);
736  goto out;
737  }
738 
739  _dbus_string_free (&received);
740  }
741  else if (_dbus_string_starts_with_c_str (&line,
742  "EXPECT_UNUSED"))
743  {
744  DBusString expected;
745  const DBusString *unused;
746 
747  _dbus_string_delete_first_word (&line);
748 
749  if (!_dbus_string_init (&expected))
750  {
751  _dbus_warn ("no mem to allocate string expected\n");
752  goto out;
753  }
754 
755  if (!append_quoted_string (&expected, &line))
756  {
757  _dbus_warn ("failed to append quoted string line %d\n",
758  line_no);
759  _dbus_string_free (&expected);
760  goto out;
761  }
762 
763  _dbus_auth_get_unused_bytes (auth, &unused);
764 
765  if (_dbus_string_equal (&expected, unused))
766  {
768  _dbus_string_free (&expected);
769  }
770  else
771  {
772  _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
773  _dbus_string_get_const_data (&expected),
774  _dbus_string_get_const_data (unused));
775  _dbus_string_free (&expected);
776  goto out;
777  }
778  }
779  else if (_dbus_string_starts_with_c_str (&line,
780  "EXPECT_HAVE_NO_CREDENTIALS"))
781  {
782  DBusCredentials *authorized_identity;
783 
784  authorized_identity = _dbus_auth_get_identity (auth);
785  if (!_dbus_credentials_are_anonymous (authorized_identity))
786  {
787  _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized\n");
788  goto out;
789  }
790  }
791  else if (_dbus_string_starts_with_c_str (&line,
792  "EXPECT_HAVE_SOME_CREDENTIALS"))
793  {
794  DBusCredentials *authorized_identity;
795 
796  authorized_identity = _dbus_auth_get_identity (auth);
797  if (_dbus_credentials_are_anonymous (authorized_identity))
798  {
799  _dbus_warn ("Expected to have some credentials, but we don't\n");
800  goto out;
801  }
802  }
803  else if (_dbus_string_starts_with_c_str (&line,
804  "EXPECT"))
805  {
806  DBusString expected;
807 
808  _dbus_string_delete_first_word (&line);
809 
810  if (!_dbus_string_init (&expected))
811  {
812  _dbus_warn ("no mem to allocate string expected\n");
813  goto out;
814  }
815 
816  if (!append_quoted_string (&expected, &line))
817  {
818  _dbus_warn ("failed to append quoted string line %d\n",
819  line_no);
820  _dbus_string_free (&expected);
821  goto out;
822  }
823 
824  if (_dbus_string_equal_len (&expected, &from_auth,
825  _dbus_string_get_length (&expected)))
826  {
827  _dbus_string_delete (&from_auth, 0,
828  _dbus_string_get_length (&expected));
829  _dbus_string_free (&expected);
830  }
831  else
832  {
833  _dbus_warn ("Expected exact string '%s' and have '%s'\n",
834  _dbus_string_get_const_data (&expected),
835  _dbus_string_get_const_data (&from_auth));
836  _dbus_string_free (&expected);
837  goto out;
838  }
839  }
840  else
841  goto parse_failed;
842 
843  goto next_iteration; /* skip parse_failed */
844 
845  parse_failed:
846  {
847  _dbus_warn ("couldn't process line %d \"%s\"\n",
848  line_no, _dbus_string_get_const_data (&line));
849  goto out;
850  }
851  }
852 
853  if (auth == NULL)
854  {
855  _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER\n");
856  goto out;
857  }
858  else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
859  {
860  const DBusString *unused;
861 
862  _dbus_auth_get_unused_bytes (auth, &unused);
863 
864  if (_dbus_string_get_length (unused) > 0)
865  {
866  _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
867  goto out;
868  }
869  }
870 
871  if (_dbus_string_get_length (&from_auth) > 0)
872  {
873  _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
874  _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
875  goto out;
876  }
877 
878  retval = TRUE;
879 
880  out:
881  if (auth)
882  _dbus_auth_unref (auth);
883 
884  _dbus_string_free (&file);
885  _dbus_string_free (&line);
886  _dbus_string_free (&from_auth);
887 
888  return retval;
889 }
890 
892 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
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
const char * message
public error message field
Definition: dbus-errors.h:51
void _dbus_auth_delete_unused_bytes(DBusAuth *auth)
Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes() after we&#39;ve gotten them and succes...
Definition: dbus-auth.c:2640
#define NULL
A null pointer, defined appropriately for C or C++.
void _dbus_auth_get_unused_bytes(DBusAuth *auth, const DBusString **str)
Returns leftover bytes that were not used as part of the auth conversation.
Definition: dbus-auth.c:2623
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
Definition: dbus-string.c:2013
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_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
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:2840
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2604
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:2497
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
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.
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:2462
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that&#39;s copied to the d...
Definition: dbus-string.c:1283
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:112
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
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:114
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:105
DBusCredentials * _dbus_auth_get_identity(DBusAuth *auth)
Gets the identity we authorized the client as.
Definition: dbus-auth.c:2797
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we&#39;re conversing with.
Definition: dbus-auth.c:2586
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:461
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...
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
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:2566
Internal members of DBusAuth.
Definition: dbus-auth.c:153
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
void _dbus_string_skip_blank(const DBusString *str, int start, int *end)
Skips blanks from start, storing the first non-blank in *end (blank is space or tab).
Definition: dbus-string.c:1803
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with credentials (user ID and process ID) from the current process.
DBusAuth * _dbus_auth_server_new(const DBusString *guid)
Creates a new auth conversation object for the server side.
Definition: dbus-auth.c:2313
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
DBusAuth * _dbus_auth_ref(DBusAuth *auth)
Increments the refcount of an auth object.
Definition: dbus-auth.c:2397
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
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
Object representing an exception.
Definition: dbus-errors.h:48
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_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
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
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
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:2779
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:749
void _dbus_auth_unref(DBusAuth *auth)
Decrements the refcount of an auth object.
Definition: dbus-auth.c:2412
dbus_bool_t _dbus_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we&#39;re conversing with.
Definition: dbus-auth.c:2541
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:107
DBusAuth * _dbus_auth_client_new(void)
Creates a new auth conversation object for the client side.
Definition: dbus-auth.c:2359