00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <config.h>
00024
00025 #ifdef DBUS_BUILD_TESTS
00026
00027 #include "dbus-auth-script.h"
00028 #include "dbus-auth.h"
00029 #include "dbus-string.h"
00030 #include "dbus-hash.h"
00031 #include "dbus-credentials.h"
00032 #include "dbus-internals.h"
00033
00045
00046
00047
00048 static dbus_bool_t
00049 append_quoted_string (DBusString *dest,
00050 const DBusString *quoted)
00051 {
00052 dbus_bool_t in_quotes = FALSE;
00053 dbus_bool_t in_backslash = FALSE;
00054 int i;
00055
00056 i = 0;
00057 while (i < _dbus_string_get_length (quoted))
00058 {
00059 unsigned char b;
00060
00061 b = _dbus_string_get_byte (quoted, i);
00062
00063 if (in_backslash)
00064 {
00065 unsigned char a;
00066
00067 if (b == 'r')
00068 a = '\r';
00069 else if (b == 'n')
00070 a = '\n';
00071 else if (b == '\\')
00072 a = '\\';
00073 else
00074 {
00075 _dbus_warn ("bad backslashed byte %c\n", b);
00076 return FALSE;
00077 }
00078
00079 if (!_dbus_string_append_byte (dest, a))
00080 return FALSE;
00081
00082 in_backslash = FALSE;
00083 }
00084 else if (b == '\\')
00085 {
00086 in_backslash = TRUE;
00087 }
00088 else if (in_quotes)
00089 {
00090 if (b == '\'')
00091 in_quotes = FALSE;
00092 else
00093 {
00094 if (!_dbus_string_append_byte (dest, b))
00095 return FALSE;
00096 }
00097 }
00098 else
00099 {
00100 if (b == '\'')
00101 in_quotes = TRUE;
00102 else if (b == ' ' || b == '\n' || b == '\t')
00103 break;
00104 else
00105 {
00106 if (!_dbus_string_append_byte (dest, b))
00107 return FALSE;
00108 }
00109 }
00110
00111 ++i;
00112 }
00113
00114 return TRUE;
00115 }
00116
00117 static dbus_bool_t
00118 same_first_word (const DBusString *a,
00119 const DBusString *b)
00120 {
00121 int first_a_blank, first_b_blank;
00122
00123 _dbus_string_find_blank (a, 0, &first_a_blank);
00124 _dbus_string_find_blank (b, 0, &first_b_blank);
00125
00126 if (first_a_blank != first_b_blank)
00127 return FALSE;
00128
00129 return _dbus_string_equal_len (a, b, first_a_blank);
00130 }
00131
00132 static DBusAuthState
00133 auth_state_from_string (const DBusString *str)
00134 {
00135 if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
00136 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
00137 else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
00138 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
00139 else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
00140 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
00141 else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
00142 return DBUS_AUTH_STATE_NEED_DISCONNECT;
00143 else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
00144 return DBUS_AUTH_STATE_AUTHENTICATED;
00145 else
00146 return -1;
00147 }
00148
00149 static const char*
00150 auth_state_to_string (DBusAuthState state)
00151 {
00152 switch (state)
00153 {
00154 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
00155 return "WAITING_FOR_INPUT";
00156 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
00157 return "WAITING_FOR_MEMORY";
00158 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
00159 return "HAVE_BYTES_TO_SEND";
00160 case DBUS_AUTH_STATE_NEED_DISCONNECT:
00161 return "NEED_DISCONNECT";
00162 case DBUS_AUTH_STATE_AUTHENTICATED:
00163 return "AUTHENTICATED";
00164 }
00165
00166 return "unknown";
00167 }
00168
00169 static char **
00170 split_string (DBusString *str)
00171 {
00172 int i, j, k, count, end;
00173 char **array;
00174
00175 end = _dbus_string_get_length (str);
00176
00177 i = 0;
00178 _dbus_string_skip_blank (str, i, &i);
00179 for (count = 0; i < end; count++)
00180 {
00181 _dbus_string_find_blank (str, i, &i);
00182 _dbus_string_skip_blank (str, i, &i);
00183 }
00184
00185 array = dbus_new0 (char *, count + 1);
00186 if (array == NULL)
00187 return NULL;
00188
00189 i = 0;
00190 _dbus_string_skip_blank (str, i, &i);
00191 for (k = 0; k < count; k++)
00192 {
00193 _dbus_string_find_blank (str, i, &j);
00194
00195 array[k] = dbus_malloc (j - i + 1);
00196 if (array[k] == NULL)
00197 {
00198 dbus_free_string_array (array);
00199 return NULL;
00200 }
00201 memcpy (array[k],
00202 _dbus_string_get_const_data_len (str, i, j - i), j - i);
00203 array[k][j - i] = '\0';
00204
00205 _dbus_string_skip_blank (str, j, &i);
00206 }
00207 array[k] = NULL;
00208
00209 return array;
00210 }
00211
00212 static void
00213 auth_set_unix_credentials(DBusAuth *auth,
00214 dbus_uid_t uid,
00215 dbus_pid_t pid)
00216 {
00217 DBusCredentials *credentials;
00218
00219 credentials = _dbus_credentials_new ();
00220 if (credentials == NULL)
00221 _dbus_assert_not_reached ("no memory");
00222
00223 if (uid != DBUS_UID_UNSET)
00224 _dbus_credentials_add_unix_uid (credentials, uid);
00225 if (pid != DBUS_PID_UNSET)
00226 _dbus_credentials_add_unix_pid (credentials, pid);
00227
00228 _dbus_auth_set_credentials (auth, credentials);
00229
00230 _dbus_credentials_unref (credentials);
00231 }
00232
00243 dbus_bool_t
00244 _dbus_auth_script_run (const DBusString *filename)
00245 {
00246 DBusString file;
00247 DBusError error = DBUS_ERROR_INIT;
00248 DBusString line;
00249 dbus_bool_t retval;
00250 int line_no;
00251 DBusAuth *auth;
00252 DBusString from_auth;
00253 DBusAuthState state;
00254 DBusString context;
00255 DBusString guid;
00256
00257 retval = FALSE;
00258 auth = NULL;
00259
00260 _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
00261 _dbus_string_init_const (&context, "org_freedesktop_test");
00262
00263 if (!_dbus_string_init (&file))
00264 return FALSE;
00265
00266 if (!_dbus_string_init (&line))
00267 {
00268 _dbus_string_free (&file);
00269 return FALSE;
00270 }
00271
00272 if (!_dbus_string_init (&from_auth))
00273 {
00274 _dbus_string_free (&file);
00275 _dbus_string_free (&line);
00276 return FALSE;
00277 }
00278
00279 if (!_dbus_file_get_contents (&file, filename, &error)) {
00280 _dbus_warn ("Getting contents of %s failed: %s\n",
00281 _dbus_string_get_const_data (filename), error.message);
00282 dbus_error_free (&error);
00283 goto out;
00284 }
00285
00286 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
00287 line_no = 0;
00288
00289 next_iteration:
00290 while (_dbus_string_pop_line (&file, &line))
00291 {
00292 line_no += 1;
00293
00294
00295
00296 _dbus_string_delete_leading_blanks (&line);
00297
00298 if (auth != NULL)
00299 {
00300 while ((state = _dbus_auth_do_work (auth)) ==
00301 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
00302 {
00303 const DBusString *tmp;
00304 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
00305 {
00306 int count = _dbus_string_get_length (tmp);
00307
00308 if (_dbus_string_copy (tmp, 0, &from_auth,
00309 _dbus_string_get_length (&from_auth)))
00310 _dbus_auth_bytes_sent (auth, count);
00311 }
00312 }
00313 }
00314
00315 if (_dbus_string_get_length (&line) == 0)
00316 {
00317
00318 goto next_iteration;
00319 }
00320 else if (_dbus_string_starts_with_c_str (&line,
00321 "#"))
00322 {
00323
00324 goto next_iteration;
00325 }
00326 #ifdef DBUS_WIN
00327 else if (_dbus_string_starts_with_c_str (&line,
00328 "WIN_ONLY"))
00329 {
00330
00331 goto next_iteration;
00332 }
00333 else if (_dbus_string_starts_with_c_str (&line,
00334 "UNIX_ONLY"))
00335 {
00336
00337 _dbus_warn ("skipping unix only auth script\n");
00338 retval = TRUE;
00339 goto out;
00340 }
00341 #endif
00342 #ifdef DBUS_UNIX
00343 else if (_dbus_string_starts_with_c_str (&line,
00344 "UNIX_ONLY"))
00345 {
00346
00347 goto next_iteration;
00348 }
00349 else if (_dbus_string_starts_with_c_str (&line,
00350 "WIN_ONLY"))
00351 {
00352
00353 _dbus_warn ("skipping windows only auth script\n");
00354 retval = TRUE;
00355 goto out;
00356 }
00357 #endif
00358 else if (_dbus_string_starts_with_c_str (&line,
00359 "CLIENT"))
00360 {
00361 DBusCredentials *creds;
00362
00363 if (auth != NULL)
00364 {
00365 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
00366 goto out;
00367 }
00368
00369 auth = _dbus_auth_client_new ();
00370 if (auth == NULL)
00371 {
00372 _dbus_warn ("no memory to create DBusAuth\n");
00373 goto out;
00374 }
00375
00376
00377 _dbus_auth_ref (auth);
00378 _dbus_auth_unref (auth);
00379
00380 creds = _dbus_credentials_new_from_current_process ();
00381 if (creds == NULL)
00382 {
00383 _dbus_warn ("no memory for credentials\n");
00384 _dbus_auth_unref (auth);
00385 auth = NULL;
00386 goto out;
00387 }
00388
00389 if (!_dbus_auth_set_credentials (auth, creds))
00390 {
00391 _dbus_warn ("no memory for setting credentials\n");
00392 _dbus_auth_unref (auth);
00393 auth = NULL;
00394 _dbus_credentials_unref (creds);
00395 goto out;
00396 }
00397
00398 _dbus_credentials_unref (creds);
00399 }
00400 else if (_dbus_string_starts_with_c_str (&line,
00401 "SERVER"))
00402 {
00403 DBusCredentials *creds;
00404
00405 if (auth != NULL)
00406 {
00407 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
00408 goto out;
00409 }
00410
00411 auth = _dbus_auth_server_new (&guid);
00412 if (auth == NULL)
00413 {
00414 _dbus_warn ("no memory to create DBusAuth\n");
00415 goto out;
00416 }
00417
00418
00419 _dbus_auth_ref (auth);
00420 _dbus_auth_unref (auth);
00421
00422 creds = _dbus_credentials_new_from_current_process ();
00423 if (creds == NULL)
00424 {
00425 _dbus_warn ("no memory for credentials\n");
00426 _dbus_auth_unref (auth);
00427 auth = NULL;
00428 goto out;
00429 }
00430
00431 if (!_dbus_auth_set_credentials (auth, creds))
00432 {
00433 _dbus_warn ("no memory for setting credentials\n");
00434 _dbus_auth_unref (auth);
00435 auth = NULL;
00436 _dbus_credentials_unref (creds);
00437 goto out;
00438 }
00439
00440 _dbus_credentials_unref (creds);
00441
00442 _dbus_auth_set_context (auth, &context);
00443 }
00444 else if (auth == NULL)
00445 {
00446 _dbus_warn ("must specify CLIENT or SERVER\n");
00447 goto out;
00448
00449 }
00450 else if (_dbus_string_starts_with_c_str (&line,
00451 "NO_CREDENTIALS"))
00452 {
00453 auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
00454 }
00455 else if (_dbus_string_starts_with_c_str (&line,
00456 "ROOT_CREDENTIALS"))
00457 {
00458 auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
00459 }
00460 else if (_dbus_string_starts_with_c_str (&line,
00461 "SILLY_CREDENTIALS"))
00462 {
00463 auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
00464 }
00465 else if (_dbus_string_starts_with_c_str (&line,
00466 "ALLOWED_MECHS"))
00467 {
00468 char **mechs;
00469
00470 _dbus_string_delete_first_word (&line);
00471 mechs = split_string (&line);
00472 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
00473 dbus_free_string_array (mechs);
00474 }
00475 else if (_dbus_string_starts_with_c_str (&line,
00476 "SEND"))
00477 {
00478 DBusString to_send;
00479
00480 _dbus_string_delete_first_word (&line);
00481
00482 if (!_dbus_string_init (&to_send))
00483 {
00484 _dbus_warn ("no memory to allocate string\n");
00485 goto out;
00486 }
00487
00488 if (!append_quoted_string (&to_send, &line))
00489 {
00490 _dbus_warn ("failed to append quoted string line %d\n",
00491 line_no);
00492 _dbus_string_free (&to_send);
00493 goto out;
00494 }
00495
00496 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
00497
00498 if (!_dbus_string_append (&to_send, "\r\n"))
00499 {
00500 _dbus_warn ("failed to append \r\n from line %d\n",
00501 line_no);
00502 _dbus_string_free (&to_send);
00503 goto out;
00504 }
00505
00506
00507 {
00508 int where;
00509
00510 if (_dbus_string_find (&to_send, 0,
00511 "USERID_HEX", &where))
00512 {
00513 DBusString username;
00514
00515 if (!_dbus_string_init (&username))
00516 {
00517 _dbus_warn ("no memory for userid\n");
00518 _dbus_string_free (&to_send);
00519 goto out;
00520 }
00521
00522 if (!_dbus_append_user_from_current_process (&username))
00523 {
00524 _dbus_warn ("no memory for userid\n");
00525 _dbus_string_free (&username);
00526 _dbus_string_free (&to_send);
00527 goto out;
00528 }
00529
00530 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
00531
00532 if (!_dbus_string_hex_encode (&username, 0,
00533 &to_send, where))
00534 {
00535 _dbus_warn ("no memory to subst USERID_HEX\n");
00536 _dbus_string_free (&username);
00537 _dbus_string_free (&to_send);
00538 goto out;
00539 }
00540
00541 _dbus_string_free (&username);
00542 }
00543 else if (_dbus_string_find (&to_send, 0,
00544 "USERNAME_HEX", &where))
00545 {
00546 DBusString username;
00547
00548 if (!_dbus_string_init (&username))
00549 {
00550 _dbus_warn ("no memory for username\n");
00551 _dbus_string_free (&to_send);
00552 goto out;
00553 }
00554
00555 if (!_dbus_append_user_from_current_process (&username))
00556 {
00557 _dbus_warn ("no memory for username\n");
00558 _dbus_string_free (&username);
00559 _dbus_string_free (&to_send);
00560 goto out;
00561 }
00562
00563 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
00564
00565 if (!_dbus_string_hex_encode (&username, 0,
00566 &to_send, where))
00567 {
00568 _dbus_warn ("no memory to subst USERNAME_HEX\n");
00569 _dbus_string_free (&username);
00570 _dbus_string_free (&to_send);
00571 goto out;
00572 }
00573
00574 _dbus_string_free (&username);
00575 }
00576 }
00577
00578 {
00579 DBusString *buffer;
00580
00581 _dbus_auth_get_buffer (auth, &buffer);
00582 if (!_dbus_string_copy (&to_send, 0,
00583 buffer, _dbus_string_get_length (buffer)))
00584 {
00585 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
00586 _dbus_string_free (&to_send);
00587 _dbus_auth_return_buffer (auth, buffer, 0);
00588 goto out;
00589 }
00590
00591 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
00592 }
00593
00594 _dbus_string_free (&to_send);
00595 }
00596 else if (_dbus_string_starts_with_c_str (&line,
00597 "EXPECT_STATE"))
00598 {
00599 DBusAuthState expected;
00600
00601 _dbus_string_delete_first_word (&line);
00602
00603 expected = auth_state_from_string (&line);
00604 if (expected < 0)
00605 {
00606 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
00607 goto parse_failed;
00608 }
00609
00610 if (expected != state)
00611 {
00612 _dbus_warn ("expected auth state %s but got %s on line %d\n",
00613 auth_state_to_string (expected),
00614 auth_state_to_string (state),
00615 line_no);
00616 goto out;
00617 }
00618 }
00619 else if (_dbus_string_starts_with_c_str (&line,
00620 "EXPECT_COMMAND"))
00621 {
00622 DBusString received;
00623
00624 _dbus_string_delete_first_word (&line);
00625
00626 if (!_dbus_string_init (&received))
00627 {
00628 _dbus_warn ("no mem to allocate string received\n");
00629 goto out;
00630 }
00631
00632 if (!_dbus_string_pop_line (&from_auth, &received))
00633 {
00634 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
00635 _dbus_string_get_const_data (&line), line_no);
00636 _dbus_string_free (&received);
00637 goto out;
00638 }
00639
00640 if (!same_first_word (&received, &line))
00641 {
00642 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
00643 line_no,
00644 _dbus_string_get_const_data (&line),
00645 _dbus_string_get_const_data (&received));
00646 _dbus_string_free (&received);
00647 goto out;
00648 }
00649
00650 _dbus_string_free (&received);
00651 }
00652 else if (_dbus_string_starts_with_c_str (&line,
00653 "EXPECT_UNUSED"))
00654 {
00655 DBusString expected;
00656 const DBusString *unused;
00657
00658 _dbus_string_delete_first_word (&line);
00659
00660 if (!_dbus_string_init (&expected))
00661 {
00662 _dbus_warn ("no mem to allocate string expected\n");
00663 goto out;
00664 }
00665
00666 if (!append_quoted_string (&expected, &line))
00667 {
00668 _dbus_warn ("failed to append quoted string line %d\n",
00669 line_no);
00670 _dbus_string_free (&expected);
00671 goto out;
00672 }
00673
00674 _dbus_auth_get_unused_bytes (auth, &unused);
00675
00676 if (_dbus_string_equal (&expected, unused))
00677 {
00678 _dbus_auth_delete_unused_bytes (auth);
00679 _dbus_string_free (&expected);
00680 }
00681 else
00682 {
00683 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
00684 _dbus_string_get_const_data (&expected),
00685 _dbus_string_get_const_data (unused));
00686 _dbus_string_free (&expected);
00687 goto out;
00688 }
00689 }
00690 else if (_dbus_string_starts_with_c_str (&line,
00691 "EXPECT_HAVE_NO_CREDENTIALS"))
00692 {
00693 DBusCredentials *authorized_identity;
00694
00695 authorized_identity = _dbus_auth_get_identity (auth);
00696 if (!_dbus_credentials_are_anonymous (authorized_identity))
00697 {
00698 _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized\n");
00699 goto out;
00700 }
00701 }
00702 else if (_dbus_string_starts_with_c_str (&line,
00703 "EXPECT_HAVE_SOME_CREDENTIALS"))
00704 {
00705 DBusCredentials *authorized_identity;
00706
00707 authorized_identity = _dbus_auth_get_identity (auth);
00708 if (_dbus_credentials_are_anonymous (authorized_identity))
00709 {
00710 _dbus_warn ("Expected to have some credentials, but we don't\n");
00711 goto out;
00712 }
00713 }
00714 else if (_dbus_string_starts_with_c_str (&line,
00715 "EXPECT"))
00716 {
00717 DBusString expected;
00718
00719 _dbus_string_delete_first_word (&line);
00720
00721 if (!_dbus_string_init (&expected))
00722 {
00723 _dbus_warn ("no mem to allocate string expected\n");
00724 goto out;
00725 }
00726
00727 if (!append_quoted_string (&expected, &line))
00728 {
00729 _dbus_warn ("failed to append quoted string line %d\n",
00730 line_no);
00731 _dbus_string_free (&expected);
00732 goto out;
00733 }
00734
00735 if (_dbus_string_equal_len (&expected, &from_auth,
00736 _dbus_string_get_length (&expected)))
00737 {
00738 _dbus_string_delete (&from_auth, 0,
00739 _dbus_string_get_length (&expected));
00740 _dbus_string_free (&expected);
00741 }
00742 else
00743 {
00744 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
00745 _dbus_string_get_const_data (&expected),
00746 _dbus_string_get_const_data (&from_auth));
00747 _dbus_string_free (&expected);
00748 goto out;
00749 }
00750 }
00751 else
00752 goto parse_failed;
00753
00754 goto next_iteration;
00755
00756 parse_failed:
00757 {
00758 _dbus_warn ("couldn't process line %d \"%s\"\n",
00759 line_no, _dbus_string_get_const_data (&line));
00760 goto out;
00761 }
00762 }
00763
00764 if (auth == NULL)
00765 {
00766 _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER\n");
00767 goto out;
00768 }
00769 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
00770 {
00771 const DBusString *unused;
00772
00773 _dbus_auth_get_unused_bytes (auth, &unused);
00774
00775 if (_dbus_string_get_length (unused) > 0)
00776 {
00777 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
00778 goto out;
00779 }
00780 }
00781
00782 if (_dbus_string_get_length (&from_auth) > 0)
00783 {
00784 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
00785 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
00786 goto out;
00787 }
00788
00789 retval = TRUE;
00790
00791 out:
00792 if (auth)
00793 _dbus_auth_unref (auth);
00794
00795 _dbus_string_free (&file);
00796 _dbus_string_free (&line);
00797 _dbus_string_free (&from_auth);
00798
00799 return retval;
00800 }
00801
00803 #endif