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;
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 dbus_error_init (&error);
00280 if (!_dbus_file_get_contents (&file, filename, &error)) {
00281 _dbus_warn ("Getting contents of %s failed: %s\n",
00282 _dbus_string_get_const_data (filename), error.message);
00283 dbus_error_free (&error);
00284 goto out;
00285 }
00286
00287 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
00288 line_no = 0;
00289
00290 next_iteration:
00291 while (_dbus_string_pop_line (&file, &line))
00292 {
00293 line_no += 1;
00294
00295
00296
00297 _dbus_string_delete_leading_blanks (&line);
00298
00299 if (auth != NULL)
00300 {
00301 while ((state = _dbus_auth_do_work (auth)) ==
00302 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
00303 {
00304 const DBusString *tmp;
00305 if (_dbus_auth_get_bytes_to_send (auth, &tmp))
00306 {
00307 int count = _dbus_string_get_length (tmp);
00308
00309 if (_dbus_string_copy (tmp, 0, &from_auth,
00310 _dbus_string_get_length (&from_auth)))
00311 _dbus_auth_bytes_sent (auth, count);
00312 }
00313 }
00314 }
00315
00316 if (_dbus_string_get_length (&line) == 0)
00317 {
00318
00319 goto next_iteration;
00320 }
00321 else if (_dbus_string_starts_with_c_str (&line,
00322 "#"))
00323 {
00324
00325 goto next_iteration;
00326 }
00327 else if (_dbus_string_starts_with_c_str (&line,
00328 "CLIENT"))
00329 {
00330 DBusCredentials *creds;
00331
00332 if (auth != NULL)
00333 {
00334 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
00335 goto out;
00336 }
00337
00338 auth = _dbus_auth_client_new ();
00339 if (auth == NULL)
00340 {
00341 _dbus_warn ("no memory to create DBusAuth\n");
00342 goto out;
00343 }
00344
00345
00346 _dbus_auth_ref (auth);
00347 _dbus_auth_unref (auth);
00348
00349 creds = _dbus_credentials_new_from_current_process ();
00350 if (creds == NULL)
00351 {
00352 _dbus_warn ("no memory for credentials\n");
00353 _dbus_auth_unref (auth);
00354 auth = NULL;
00355 goto out;
00356 }
00357
00358 if (!_dbus_auth_set_credentials (auth, creds))
00359 {
00360 _dbus_warn ("no memory for setting credentials\n");
00361 _dbus_auth_unref (auth);
00362 auth = NULL;
00363 _dbus_credentials_unref (creds);
00364 goto out;
00365 }
00366
00367 _dbus_credentials_unref (creds);
00368 }
00369 else if (_dbus_string_starts_with_c_str (&line,
00370 "SERVER"))
00371 {
00372 DBusCredentials *creds;
00373
00374 if (auth != NULL)
00375 {
00376 _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
00377 goto out;
00378 }
00379
00380 auth = _dbus_auth_server_new (&guid);
00381 if (auth == NULL)
00382 {
00383 _dbus_warn ("no memory to create DBusAuth\n");
00384 goto out;
00385 }
00386
00387
00388 _dbus_auth_ref (auth);
00389 _dbus_auth_unref (auth);
00390
00391 creds = _dbus_credentials_new_from_current_process ();
00392 if (creds == NULL)
00393 {
00394 _dbus_warn ("no memory for credentials\n");
00395 _dbus_auth_unref (auth);
00396 auth = NULL;
00397 goto out;
00398 }
00399
00400 if (!_dbus_auth_set_credentials (auth, creds))
00401 {
00402 _dbus_warn ("no memory for setting credentials\n");
00403 _dbus_auth_unref (auth);
00404 auth = NULL;
00405 _dbus_credentials_unref (creds);
00406 goto out;
00407 }
00408
00409 _dbus_credentials_unref (creds);
00410
00411 _dbus_auth_set_context (auth, &context);
00412 }
00413 else if (auth == NULL)
00414 {
00415 _dbus_warn ("must specify CLIENT or SERVER\n");
00416 goto out;
00417
00418 }
00419 else if (_dbus_string_starts_with_c_str (&line,
00420 "NO_CREDENTIALS"))
00421 {
00422 auth_set_unix_credentials (auth, DBUS_UID_UNSET, DBUS_PID_UNSET);
00423 }
00424 else if (_dbus_string_starts_with_c_str (&line,
00425 "ROOT_CREDENTIALS"))
00426 {
00427 auth_set_unix_credentials (auth, 0, DBUS_PID_UNSET);
00428 }
00429 else if (_dbus_string_starts_with_c_str (&line,
00430 "SILLY_CREDENTIALS"))
00431 {
00432 auth_set_unix_credentials (auth, 4312, DBUS_PID_UNSET);
00433 }
00434 else if (_dbus_string_starts_with_c_str (&line,
00435 "ALLOWED_MECHS"))
00436 {
00437 char **mechs;
00438
00439 _dbus_string_delete_first_word (&line);
00440 mechs = split_string (&line);
00441 _dbus_auth_set_mechanisms (auth, (const char **) mechs);
00442 dbus_free_string_array (mechs);
00443 }
00444 else if (_dbus_string_starts_with_c_str (&line,
00445 "SEND"))
00446 {
00447 DBusString to_send;
00448
00449 _dbus_string_delete_first_word (&line);
00450
00451 if (!_dbus_string_init (&to_send))
00452 {
00453 _dbus_warn ("no memory to allocate string\n");
00454 goto out;
00455 }
00456
00457 if (!append_quoted_string (&to_send, &line))
00458 {
00459 _dbus_warn ("failed to append quoted string line %d\n",
00460 line_no);
00461 _dbus_string_free (&to_send);
00462 goto out;
00463 }
00464
00465 _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
00466
00467 if (!_dbus_string_append (&to_send, "\r\n"))
00468 {
00469 _dbus_warn ("failed to append \r\n from line %d\n",
00470 line_no);
00471 _dbus_string_free (&to_send);
00472 goto out;
00473 }
00474
00475
00476 {
00477 int where;
00478
00479 if (_dbus_string_find (&to_send, 0,
00480 "USERID_HEX", &where))
00481 {
00482 DBusString username;
00483
00484 if (!_dbus_string_init (&username))
00485 {
00486 _dbus_warn ("no memory for userid\n");
00487 _dbus_string_free (&to_send);
00488 goto out;
00489 }
00490
00491 if (!_dbus_append_user_from_current_process (&username))
00492 {
00493 _dbus_warn ("no memory for userid\n");
00494 _dbus_string_free (&username);
00495 _dbus_string_free (&to_send);
00496 goto out;
00497 }
00498
00499 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
00500
00501 if (!_dbus_string_hex_encode (&username, 0,
00502 &to_send, where))
00503 {
00504 _dbus_warn ("no memory to subst USERID_HEX\n");
00505 _dbus_string_free (&username);
00506 _dbus_string_free (&to_send);
00507 goto out;
00508 }
00509
00510 _dbus_string_free (&username);
00511 }
00512 else if (_dbus_string_find (&to_send, 0,
00513 "USERNAME_HEX", &where))
00514 {
00515 DBusString username;
00516
00517 if (!_dbus_string_init (&username))
00518 {
00519 _dbus_warn ("no memory for username\n");
00520 _dbus_string_free (&to_send);
00521 goto out;
00522 }
00523
00524 if (!_dbus_append_user_from_current_process (&username))
00525 {
00526 _dbus_warn ("no memory for username\n");
00527 _dbus_string_free (&username);
00528 _dbus_string_free (&to_send);
00529 goto out;
00530 }
00531
00532 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
00533
00534 if (!_dbus_string_hex_encode (&username, 0,
00535 &to_send, where))
00536 {
00537 _dbus_warn ("no memory to subst USERNAME_HEX\n");
00538 _dbus_string_free (&username);
00539 _dbus_string_free (&to_send);
00540 goto out;
00541 }
00542
00543 _dbus_string_free (&username);
00544 }
00545 }
00546
00547 {
00548 DBusString *buffer;
00549
00550 _dbus_auth_get_buffer (auth, &buffer);
00551 if (!_dbus_string_copy (&to_send, 0,
00552 buffer, _dbus_string_get_length (buffer)))
00553 {
00554 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
00555 _dbus_string_free (&to_send);
00556 _dbus_auth_return_buffer (auth, buffer, 0);
00557 goto out;
00558 }
00559
00560 _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
00561 }
00562
00563 _dbus_string_free (&to_send);
00564 }
00565 else if (_dbus_string_starts_with_c_str (&line,
00566 "EXPECT_STATE"))
00567 {
00568 DBusAuthState expected;
00569
00570 _dbus_string_delete_first_word (&line);
00571
00572 expected = auth_state_from_string (&line);
00573 if (expected < 0)
00574 {
00575 _dbus_warn ("bad auth state given to EXPECT_STATE\n");
00576 goto parse_failed;
00577 }
00578
00579 if (expected != state)
00580 {
00581 _dbus_warn ("expected auth state %s but got %s on line %d\n",
00582 auth_state_to_string (expected),
00583 auth_state_to_string (state),
00584 line_no);
00585 goto out;
00586 }
00587 }
00588 else if (_dbus_string_starts_with_c_str (&line,
00589 "EXPECT_COMMAND"))
00590 {
00591 DBusString received;
00592
00593 _dbus_string_delete_first_word (&line);
00594
00595 if (!_dbus_string_init (&received))
00596 {
00597 _dbus_warn ("no mem to allocate string received\n");
00598 goto out;
00599 }
00600
00601 if (!_dbus_string_pop_line (&from_auth, &received))
00602 {
00603 _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
00604 _dbus_string_get_const_data (&line), line_no);
00605 _dbus_string_free (&received);
00606 goto out;
00607 }
00608
00609 if (!same_first_word (&received, &line))
00610 {
00611 _dbus_warn ("line %d expected command '%s' and got '%s'\n",
00612 line_no,
00613 _dbus_string_get_const_data (&line),
00614 _dbus_string_get_const_data (&received));
00615 _dbus_string_free (&received);
00616 goto out;
00617 }
00618
00619 _dbus_string_free (&received);
00620 }
00621 else if (_dbus_string_starts_with_c_str (&line,
00622 "EXPECT_UNUSED"))
00623 {
00624 DBusString expected;
00625 const DBusString *unused;
00626
00627 _dbus_string_delete_first_word (&line);
00628
00629 if (!_dbus_string_init (&expected))
00630 {
00631 _dbus_warn ("no mem to allocate string expected\n");
00632 goto out;
00633 }
00634
00635 if (!append_quoted_string (&expected, &line))
00636 {
00637 _dbus_warn ("failed to append quoted string line %d\n",
00638 line_no);
00639 _dbus_string_free (&expected);
00640 goto out;
00641 }
00642
00643 _dbus_auth_get_unused_bytes (auth, &unused);
00644
00645 if (_dbus_string_equal (&expected, unused))
00646 {
00647 _dbus_auth_delete_unused_bytes (auth);
00648 _dbus_string_free (&expected);
00649 }
00650 else
00651 {
00652 _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
00653 _dbus_string_get_const_data (&expected),
00654 _dbus_string_get_const_data (unused));
00655 _dbus_string_free (&expected);
00656 goto out;
00657 }
00658 }
00659 else if (_dbus_string_starts_with_c_str (&line,
00660 "EXPECT_HAVE_NO_CREDENTIALS"))
00661 {
00662 DBusCredentials *authorized_identity;
00663
00664 authorized_identity = _dbus_auth_get_identity (auth);
00665 if (!_dbus_credentials_are_anonymous (authorized_identity))
00666 {
00667 _dbus_warn ("Expected anonymous login or failed login, but some credentials were authorized\n");
00668 goto out;
00669 }
00670 }
00671 else if (_dbus_string_starts_with_c_str (&line,
00672 "EXPECT_HAVE_SOME_CREDENTIALS"))
00673 {
00674 DBusCredentials *authorized_identity;
00675
00676 authorized_identity = _dbus_auth_get_identity (auth);
00677 if (_dbus_credentials_are_anonymous (authorized_identity))
00678 {
00679 _dbus_warn ("Expected to have some credentials, but we don't\n");
00680 goto out;
00681 }
00682 }
00683 else if (_dbus_string_starts_with_c_str (&line,
00684 "EXPECT"))
00685 {
00686 DBusString expected;
00687
00688 _dbus_string_delete_first_word (&line);
00689
00690 if (!_dbus_string_init (&expected))
00691 {
00692 _dbus_warn ("no mem to allocate string expected\n");
00693 goto out;
00694 }
00695
00696 if (!append_quoted_string (&expected, &line))
00697 {
00698 _dbus_warn ("failed to append quoted string line %d\n",
00699 line_no);
00700 _dbus_string_free (&expected);
00701 goto out;
00702 }
00703
00704 if (_dbus_string_equal_len (&expected, &from_auth,
00705 _dbus_string_get_length (&expected)))
00706 {
00707 _dbus_string_delete (&from_auth, 0,
00708 _dbus_string_get_length (&expected));
00709 _dbus_string_free (&expected);
00710 }
00711 else
00712 {
00713 _dbus_warn ("Expected exact string '%s' and have '%s'\n",
00714 _dbus_string_get_const_data (&expected),
00715 _dbus_string_get_const_data (&from_auth));
00716 _dbus_string_free (&expected);
00717 goto out;
00718 }
00719 }
00720 else
00721 goto parse_failed;
00722
00723 goto next_iteration;
00724
00725 parse_failed:
00726 {
00727 _dbus_warn ("couldn't process line %d \"%s\"\n",
00728 line_no, _dbus_string_get_const_data (&line));
00729 goto out;
00730 }
00731 }
00732
00733 if (auth == NULL)
00734 {
00735 _dbus_warn ("Auth script is bogus, did not even have CLIENT or SERVER\n");
00736 goto out;
00737 }
00738 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
00739 {
00740 const DBusString *unused;
00741
00742 _dbus_auth_get_unused_bytes (auth, &unused);
00743
00744 if (_dbus_string_get_length (unused) > 0)
00745 {
00746 _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
00747 goto out;
00748 }
00749 }
00750
00751 if (_dbus_string_get_length (&from_auth) > 0)
00752 {
00753 _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
00754 _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
00755 goto out;
00756 }
00757
00758 retval = TRUE;
00759
00760 out:
00761 if (auth)
00762 _dbus_auth_unref (auth);
00763
00764 _dbus_string_free (&file);
00765 _dbus_string_free (&line);
00766 _dbus_string_free (&from_auth);
00767
00768 return retval;
00769 }
00770
00772 #endif