25 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 27 #include "dbus-auth-script.h" 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" 37 #include "test/test-utils.h" 62 while (i < _dbus_string_get_length (quoted))
66 b = _dbus_string_get_byte (quoted, i);
107 else if (b ==
' ' || b ==
'\n' || b ==
'\t')
126 int first_a_blank, first_b_blank;
131 if (first_a_blank != first_b_blank)
138 auth_state_from_string (
const DBusString *str)
141 return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
143 return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
145 return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
147 return DBUS_AUTH_STATE_NEED_DISCONNECT;
149 return DBUS_AUTH_STATE_AUTHENTICATED;
155 auth_state_to_string (DBusAuthState state)
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";
177 int i, j, k, count, end;
180 end = _dbus_string_get_length (str);
184 for (count = 0; i < end; count++)
196 for (k = 0; k < count; k++)
201 if (array[k] ==
NULL)
207 _dbus_string_get_const_data_len (str, i, j - i), j - i);
208 array[k][j - i] =
'\0';
218 auth_set_unix_credentials(
DBusAuth *auth,
225 if (credentials ==
NULL)
254 _dbus_auth_script_run (
const DBusString *filename)
290 _dbus_warn (
"Getting contents of %s failed: %s\n",
291 _dbus_string_get_const_data (filename), error.
message);
296 state = DBUS_AUTH_STATE_NEED_DISCONNECT;
306 _dbus_string_delete_leading_blanks (&line);
311 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
316 int count = _dbus_string_get_length (tmp);
319 _dbus_string_get_length (&from_auth)))
325 if (_dbus_string_get_length (&line) == 0)
347 fprintf (stderr,
"skipping unix only auth script\n");
363 fprintf (stderr,
"skipping windows only auth script\n");
375 _dbus_warn (
"already created a DBusAuth (CLIENT or SERVER given twice)\n");
382 _dbus_warn (
"no memory to create DBusAuth\n");
401 _dbus_warn (
"no memory for setting credentials\n");
417 _dbus_warn (
"already created a DBusAuth (CLIENT or SERVER given twice)\n");
424 _dbus_warn (
"no memory to create DBusAuth\n");
443 _dbus_warn (
"no memory for setting credentials\n");
454 else if (auth ==
NULL)
456 _dbus_warn (
"must specify CLIENT or SERVER\n");
471 "SILLY_CREDENTIALS"))
480 _dbus_string_delete_first_word (&line);
481 mechs = split_string (&line);
490 _dbus_string_delete_first_word (&line);
494 _dbus_warn (
"no memory to allocate string\n");
498 if (!append_quoted_string (&to_send, &line))
500 _dbus_warn (
"failed to append quoted string line %d\n",
506 _dbus_verbose (
"Sending '%s'\n", _dbus_string_get_const_data (&to_send));
510 _dbus_warn (
"failed to append \r\n from line %d\n",
533 if (!_dbus_test_append_different_uid (&uid))
542 (
int) strlen (
"WRONG_USERID_HEX"));
546 _dbus_warn (
"no memory to subst WRONG_USERID_HEX");
555 "USERID_HEX", &where))
579 _dbus_warn (
"no memory to subst USERID_HEX\n");
588 "WRONG_USERNAME_HEX", &where))
602 if (!_dbus_test_append_different_username (&username))
611 (
int) strlen (
"WRONG_USERNAME_HEX"));
616 _dbus_warn (
"no memory to subst WRONG_USERNAME_HEX");
627 _dbus_warn (
"WRONG_USERNAME_HEX cannot be used on Windows");
633 "USERNAME_HEX", &where))
657 _dbus_warn (
"no memory to subst USERNAME_HEX\n");
672 buffer, _dbus_string_get_length (buffer)))
674 _dbus_warn (
"not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
688 DBusAuthState expected;
690 _dbus_string_delete_first_word (&line);
692 expected = auth_state_from_string (&line);
695 _dbus_warn (
"bad auth state given to EXPECT_STATE\n");
699 if (expected != state)
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),
713 _dbus_string_delete_first_word (&line);
717 _dbus_warn (
"no mem to allocate string received\n");
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);
729 if (!same_first_word (&received, &line))
731 _dbus_warn (
"line %d expected command '%s' and got '%s'\n",
733 _dbus_string_get_const_data (&line),
734 _dbus_string_get_const_data (&received));
747 _dbus_string_delete_first_word (&line);
751 _dbus_warn (
"no mem to allocate string expected\n");
755 if (!append_quoted_string (&expected, &line))
757 _dbus_warn (
"failed to append quoted string line %d\n",
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));
780 "EXPECT_HAVE_NO_CREDENTIALS"))
787 _dbus_warn (
"Expected anonymous login or failed login, but some credentials were authorized\n");
792 "EXPECT_HAVE_SOME_CREDENTIALS"))
799 _dbus_warn (
"Expected to have some credentials, but we don't\n");
808 _dbus_string_delete_first_word (&line);
812 _dbus_warn (
"no mem to allocate string expected\n");
816 if (!append_quoted_string (&expected, &line))
818 _dbus_warn (
"failed to append quoted string line %d\n",
825 _dbus_string_get_length (&expected)))
828 _dbus_string_get_length (&expected));
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));
847 _dbus_warn (
"couldn't process line %d \"%s\"\n",
848 line_no, _dbus_string_get_const_data (&line));
855 _dbus_warn (
"Auth script is bogus, did not even have CLIENT or SERVER\n");
858 else if (state == DBUS_AUTH_STATE_AUTHENTICATED)
864 if (_dbus_string_get_length (unused) > 0)
866 _dbus_warn (
"did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
871 if (_dbus_string_get_length (&from_auth) > 0)
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));
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
const char * message
public error message field
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...
#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.
dbus_bool_t _dbus_string_equal(const DBusString *a, const DBusString *b)
Tests two DBusString for equality.
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.
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.
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...
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer)
Returns a buffer with new data read into it.
DBusAuthState _dbus_auth_do_work(DBusAuth *auth)
Analyzes buffered input and moves the auth conversation forward, returning the new state of the auth ...
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...
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.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
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...
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
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...
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
unsigned long dbus_pid_t
A process ID.
DBusCredentials * _dbus_auth_get_identity(DBusAuth *auth)
Gets the identity we authorized the client as.
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.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
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().
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...
Internal members of DBusAuth.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
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).
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.
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...
DBusAuth * _dbus_auth_ref(DBusAuth *auth)
Increments the refcount of an auth object.
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.
Object representing an exception.
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.
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.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
#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.
dbus_bool_t _dbus_auth_set_credentials(DBusAuth *auth, DBusCredentials *credentials)
Sets credentials received via reliable means from the operating system.
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.
void _dbus_auth_unref(DBusAuth *auth)
Decrements the refcount of an auth object.
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.
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.
DBusAuth * _dbus_auth_client_new(void)
Creates a new auth conversation object for the client side.