-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-auth.c Authentication
*
* Copyright (C) 2002, 2003, 2004 Red Hat Inc.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+
+#include <config.h>
#include "dbus-auth.h"
#include "dbus-string.h"
#include "dbus-list.h"
#include "dbus-sha.h"
#include "dbus-protocol.h"
#include "dbus-credentials.h"
+#include "dbus-authorization.h"
/**
* @defgroup DBusAuth Authentication
DBUS_AUTH_COMMAND_REJECTED,
DBUS_AUTH_COMMAND_OK,
DBUS_AUTH_COMMAND_ERROR,
- DBUS_AUTH_COMMAND_UNKNOWN
+ DBUS_AUTH_COMMAND_UNKNOWN,
+ DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD,
+ DBUS_AUTH_COMMAND_AGREE_UNIX_FD
} DBusAuthCommand;
/**
unsigned int already_got_mechanisms : 1; /**< Client already got mech list */
unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
+
+ unsigned int unix_fd_possible : 1; /**< This side could do unix fd passing */
+ unsigned int unix_fd_negotiated : 1; /**< Unix fd was successfully negotiated */
};
/**
{
DBusAuth base; /**< Parent class */
+ DBusAuthorization *authorization; /* DBus Authorization callbacks */
+
int failures; /**< Number of times client has been rejected */
int max_failures; /**< Number of times we reject before disconnect */
static dbus_bool_t send_error (DBusAuth *auth,
const char *message);
static dbus_bool_t send_ok (DBusAuth *auth);
-static dbus_bool_t send_begin (DBusAuth *auth,
- const DBusString *args_from_ok);
+static dbus_bool_t send_begin (DBusAuth *auth);
static dbus_bool_t send_cancel (DBusAuth *auth);
+static dbus_bool_t send_negotiate_unix_fd (DBusAuth *auth);
+static dbus_bool_t send_agree_unix_fd (DBusAuth *auth);
/**
* Client states
static dbus_bool_t handle_client_state_waiting_for_reject (DBusAuth *auth,
DBusAuthCommand command,
const DBusString *args);
+static dbus_bool_t handle_client_state_waiting_for_agree_unix_fd (DBusAuth *auth,
+ DBusAuthCommand command,
+ const DBusString *args);
static const DBusAuthStateData client_state_need_send_auth = {
"NeedSendAuth", NULL
static const DBusAuthStateData client_state_waiting_for_reject = {
"WaitingForReject", handle_client_state_waiting_for_reject
};
-
+static const DBusAuthStateData client_state_waiting_for_agree_unix_fd = {
+ "WaitingForAgreeUnixFD", handle_client_state_waiting_for_agree_unix_fd
+};
+
/**
* Common terminal states. Terminal states have handler == NULL.
*/
}
}
- if (!_dbus_credentials_add_from_username (auth->desired_identity, data))
+ if (!_dbus_credentials_add_from_user (auth->desired_identity, data))
{
_dbus_verbose ("%s: Did not get a valid username from client\n",
DBUS_AUTH_NAME (auth));
* a different DBusAuth for every connection.
*/
if (auth->keyring &&
- !_dbus_keyring_is_for_user (auth->keyring,
- data))
+ !_dbus_keyring_is_for_credentials (auth->keyring,
+ auth->desired_identity))
{
_dbus_keyring_unref (auth->keyring);
auth->keyring = NULL;
if (auth->keyring == NULL)
{
dbus_error_init (&error);
- auth->keyring = _dbus_keyring_new_homedir (data,
- &auth->context,
- &error);
+ auth->keyring = _dbus_keyring_new_for_credentials (auth->desired_identity,
+ &auth->context,
+ &error);
if (auth->keyring == NULL)
{
if (!_dbus_credentials_add_credentials (auth->authorized_identity,
auth->desired_identity))
goto out_3;
+
+ /* Copy process ID from the socket credentials if it's there
+ */
+ if (!_dbus_credentials_add_credential (auth->authorized_identity,
+ DBUS_CREDENTIAL_UNIX_PROCESS_ID,
+ auth->credentials))
+ goto out_3;
if (!send_ok (auth))
goto out_3;
handle_client_initial_response_cookie_sha1_mech (DBusAuth *auth,
DBusString *response)
{
- const DBusString *username;
+ DBusString username;
dbus_bool_t retval;
retval = FALSE;
- if (!_dbus_username_from_current_process (&username))
+ if (!_dbus_string_init (&username))
+ return FALSE;
+
+ if (!_dbus_append_user_from_current_process (&username))
goto out_0;
- if (!_dbus_string_hex_encode (username, 0,
+ if (!_dbus_string_hex_encode (&username, 0,
response,
_dbus_string_get_length (response)))
goto out_0;
retval = TRUE;
out_0:
+ _dbus_string_free (&username);
+
return retval;
}
DBusError error;
dbus_error_init (&error);
- auth->keyring = _dbus_keyring_new_homedir (NULL,
- &context,
- &error);
+ auth->keyring = _dbus_keyring_new_for_credentials (NULL,
+ &context,
+ &error);
if (auth->keyring == NULL)
{
handle_server_data_external_mech (DBusAuth *auth,
const DBusString *data)
{
- if (_dbus_credentials_are_empty (auth->credentials))
+ if (_dbus_credentials_are_anonymous (auth->credentials))
{
_dbus_verbose ("%s: no credentials, mechanism EXTERNAL can't authenticate\n",
DBUS_AUTH_NAME (auth));
_dbus_verbose ("%s: sending empty challenge asking client for auth identity\n",
DBUS_AUTH_NAME (auth));
auth->already_asked_for_initial_response = TRUE;
+ goto_state (auth, &server_state_waiting_for_data);
return TRUE;
}
else
}
else
{
- if (!_dbus_credentials_parse_and_add_desired(auth->desired_identity,
- &auth->identity))
+ if (!_dbus_credentials_add_from_user (auth->desired_identity,
+ &auth->identity))
{
_dbus_verbose ("%s: could not get credentials from uid string\n",
DBUS_AUTH_NAME (auth));
}
}
- if (_dbus_credentials_are_empty (auth->desired_identity))
+ if (_dbus_credentials_are_anonymous (auth->desired_identity))
{
_dbus_verbose ("%s: desired user %s is no good\n",
DBUS_AUTH_NAME (auth),
auth->desired_identity))
return FALSE;
- /* also copy process ID from the socket credentials - FIXME this
- * should be done even if auth EXTERNAL not used
+ /* also copy process ID from the socket credentials
*/
if (!_dbus_credentials_add_credential (auth->authorized_identity,
DBUS_CREDENTIAL_UNIX_PROCESS_ID,
auth->credentials))
return FALSE;
-
- if (!send_ok (auth))
+
+ /* also copy audit data from the socket credentials
+ */
+ if (!_dbus_credentials_add_credential (auth->authorized_identity,
+ DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
+ auth->credentials))
return FALSE;
- _dbus_verbose ("%s: authenticated client based on socket credentials\n",
- DBUS_AUTH_NAME (auth));
+ /* Do a first authorization of the transport, in order to REJECT
+ * immediately connection if needed (FDO#39720), transport will
+ * re-authorize later, but it will close the connection on fail,
+ * we want to REJECT now if possible */
+ if (_dbus_authorization_do_authorization (DBUS_AUTH_SERVER (auth)->authorization,
+ auth->authorized_identity))
+ {
+ if (!send_ok (auth))
+ return FALSE;
+ }
+ else
+ {
+ _dbus_verbose ("%s: desired identity does not match server identity: "
+ "not authorized\n", DBUS_AUTH_NAME (auth));
+ return send_rejected (auth);
+ }
+
+ _dbus_verbose ("%s: authenticated and authorized client based on "
+ "socket credentials\n", DBUS_AUTH_NAME (auth));
return TRUE;
}
if (!_dbus_string_init (&plaintext))
return FALSE;
- if (!_dbus_append_desired_identity (&plaintext))
+ if (!_dbus_append_user_from_current_process (&plaintext))
goto failed;
if (!_dbus_string_hex_encode (&plaintext, 0,
{
_dbus_verbose ("%s: Received invalid UTF-8 trace data from ANONYMOUS client\n",
DBUS_AUTH_NAME (auth));
-
- {
- DBusString plaintext;
- DBusString encoded;
- _dbus_string_init_const (&plaintext, "D-Bus " VERSION);
- _dbus_string_init (&encoded);
- _dbus_string_hex_encode (&plaintext, 0,
- &encoded,
- 0);
- _dbus_verbose ("%s: try '%s'\n",
- DBUS_AUTH_NAME (auth), _dbus_string_get_const_data (&encoded));
- }
return send_rejected (auth);
}
/* We want to be anonymous (clear in case some other protocol got midway through I guess) */
_dbus_credentials_clear (auth->desired_identity);
+ /* Copy process ID from the socket credentials
+ */
+ if (!_dbus_credentials_add_credential (auth->authorized_identity,
+ DBUS_CREDENTIAL_UNIX_PROCESS_ID,
+ auth->credentials))
+ return FALSE;
+
/* Anonymous is always allowed */
if (!send_ok (auth))
return FALSE;
return FALSE;
if (!_dbus_string_append (&plaintext,
- "libdbus " VERSION))
+ "libdbus " DBUS_VERSION_STRING))
goto failed;
if (!_dbus_string_hex_encode (&plaintext, 0,
}
static dbus_bool_t
-send_begin (DBusAuth *auth,
- const DBusString *args_from_ok)
+send_begin (DBusAuth *auth)
{
+
+ if (!_dbus_string_append (&auth->outgoing,
+ "BEGIN\r\n"))
+ return FALSE;
+
+ goto_state (auth, &common_state_authenticated);
+ return TRUE;
+}
+
+static dbus_bool_t
+process_ok(DBusAuth *auth,
+ const DBusString *args_from_ok) {
+
int end_of_hex;
/* "args_from_ok" should be the GUID, whitespace already pulled off the front */
return TRUE;
}
- if (_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0) &&
- _dbus_string_append (&auth->outgoing, "BEGIN\r\n"))
- {
- _dbus_verbose ("Got GUID '%s' from the server\n",
- _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
-
- goto_state (auth, &common_state_authenticated);
- return TRUE;
- }
- else
- {
+ if (!_dbus_string_copy (args_from_ok, 0, &DBUS_AUTH_CLIENT (auth)->guid_from_server, 0)) {
_dbus_string_set_length (& DBUS_AUTH_CLIENT (auth)->guid_from_server, 0);
return FALSE;
- }
+ }
+
+ _dbus_verbose ("Got GUID '%s' from the server\n",
+ _dbus_string_get_const_data (& DBUS_AUTH_CLIENT (auth)->guid_from_server));
+
+ if (auth->unix_fd_possible)
+ return send_negotiate_unix_fd(auth);
+
+ _dbus_verbose("Not negotiating unix fd passing, since not possible\n");
+ return send_begin (auth);
}
static dbus_bool_t
}
static dbus_bool_t
+send_negotiate_unix_fd (DBusAuth *auth)
+{
+ if (!_dbus_string_append (&auth->outgoing,
+ "NEGOTIATE_UNIX_FD\r\n"))
+ return FALSE;
+
+ goto_state (auth, &client_state_waiting_for_agree_unix_fd);
+ return TRUE;
+}
+
+static dbus_bool_t
+send_agree_unix_fd (DBusAuth *auth)
+{
+ _dbus_assert(auth->unix_fd_possible);
+
+ auth->unix_fd_negotiated = TRUE;
+ _dbus_verbose("Agreed to UNIX FD passing\n");
+
+ if (!_dbus_string_append (&auth->outgoing,
+ "AGREE_UNIX_FD\r\n"))
+ return FALSE;
+
+ goto_state (auth, &server_state_waiting_for_begin);
+ return TRUE;
+}
+
+static dbus_bool_t
handle_auth (DBusAuth *auth, const DBusString *args)
{
if (_dbus_string_get_length (args) == 0)
case DBUS_AUTH_COMMAND_ERROR:
return send_rejected (auth);
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ return send_error (auth, "Need to authenticate first");
+
case DBUS_AUTH_COMMAND_REJECTED:
case DBUS_AUTH_COMMAND_OK:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
return send_error (auth, "Unknown command");
}
goto_state (auth, &common_state_need_disconnect);
return TRUE;
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ return send_error (auth, "Need to authenticate first");
+
case DBUS_AUTH_COMMAND_REJECTED:
case DBUS_AUTH_COMMAND_OK:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
return send_error (auth, "Unknown command");
}
goto_state (auth, &common_state_authenticated);
return TRUE;
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ if (auth->unix_fd_possible)
+ return send_agree_unix_fd(auth);
+ else
+ return send_error(auth, "Unix FD passing not supported, not authenticated or otherwise not possible");
+
case DBUS_AUTH_COMMAND_REJECTED:
case DBUS_AUTH_COMMAND_OK:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
return send_error (auth, "Unknown command");
return process_rejected (auth, args);
case DBUS_AUTH_COMMAND_OK:
- return send_begin (auth, args);
+ return process_ok(auth, args);
case DBUS_AUTH_COMMAND_ERROR:
return send_cancel (auth);
case DBUS_AUTH_COMMAND_CANCEL:
case DBUS_AUTH_COMMAND_BEGIN:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
return send_error (auth, "Unknown command");
}
return process_rejected (auth, args);
case DBUS_AUTH_COMMAND_OK:
- return send_begin (auth, args);
+ return process_ok(auth, args);
case DBUS_AUTH_COMMAND_DATA:
case DBUS_AUTH_COMMAND_ERROR:
case DBUS_AUTH_COMMAND_CANCEL:
case DBUS_AUTH_COMMAND_BEGIN:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
return send_error (auth, "Unknown command");
}
case DBUS_AUTH_COMMAND_OK:
case DBUS_AUTH_COMMAND_ERROR:
case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
default:
goto_state (auth, &common_state_need_disconnect);
return TRUE;
}
}
+static dbus_bool_t
+handle_client_state_waiting_for_agree_unix_fd(DBusAuth *auth,
+ DBusAuthCommand command,
+ const DBusString *args)
+{
+ switch (command)
+ {
+ case DBUS_AUTH_COMMAND_AGREE_UNIX_FD:
+ _dbus_assert(auth->unix_fd_possible);
+ auth->unix_fd_negotiated = TRUE;
+ _dbus_verbose("Successfully negotiated UNIX FD passing\n");
+ return send_begin (auth);
+
+ case DBUS_AUTH_COMMAND_ERROR:
+ _dbus_assert(auth->unix_fd_possible);
+ auth->unix_fd_negotiated = FALSE;
+ _dbus_verbose("Failed to negotiate UNIX FD passing\n");
+ return send_begin (auth);
+
+ case DBUS_AUTH_COMMAND_OK:
+ case DBUS_AUTH_COMMAND_DATA:
+ case DBUS_AUTH_COMMAND_REJECTED:
+ case DBUS_AUTH_COMMAND_AUTH:
+ case DBUS_AUTH_COMMAND_CANCEL:
+ case DBUS_AUTH_COMMAND_BEGIN:
+ case DBUS_AUTH_COMMAND_UNKNOWN:
+ case DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD:
+ default:
+ return send_error (auth, "Unknown command");
+ }
+}
+
/**
* Mapping from command name to enum
*/
} DBusAuthCommandName;
static const DBusAuthCommandName auth_command_names[] = {
- { "AUTH", DBUS_AUTH_COMMAND_AUTH },
- { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
- { "DATA", DBUS_AUTH_COMMAND_DATA },
- { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
- { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
- { "OK", DBUS_AUTH_COMMAND_OK },
- { "ERROR", DBUS_AUTH_COMMAND_ERROR }
+ { "AUTH", DBUS_AUTH_COMMAND_AUTH },
+ { "CANCEL", DBUS_AUTH_COMMAND_CANCEL },
+ { "DATA", DBUS_AUTH_COMMAND_DATA },
+ { "BEGIN", DBUS_AUTH_COMMAND_BEGIN },
+ { "REJECTED", DBUS_AUTH_COMMAND_REJECTED },
+ { "OK", DBUS_AUTH_COMMAND_OK },
+ { "ERROR", DBUS_AUTH_COMMAND_ERROR },
+ { "NEGOTIATE_UNIX_FD", DBUS_AUTH_COMMAND_NEGOTIATE_UNIX_FD },
+ { "AGREE_UNIX_FD", DBUS_AUTH_COMMAND_AGREE_UNIX_FD }
};
static DBusAuthCommand
/**
* Creates a new auth conversation object for the server side.
- * See doc/dbus-sasl-profile.txt for full details on what
- * this object does.
+ * See http://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
+ * for full details on what this object does.
*
* @returns the new object or #NULL if no memory
*/
DBusAuth*
-_dbus_auth_server_new (const DBusString *guid)
+_dbus_auth_server_new (const DBusString *guid,
+ DBusAuthorization *authorization)
{
DBusAuth *auth;
DBusAuthServer *server_auth;
server_auth = DBUS_AUTH_SERVER (auth);
server_auth->guid = guid_copy;
-
+ server_auth->authorization = _dbus_authorization_ref (authorization);
+
/* perhaps this should be per-mechanism with a lower
* max
*/
/**
* Creates a new auth conversation object for the client side.
- * See doc/dbus-sasl-profile.txt for full details on what
- * this object does.
+ * See http://dbus.freedesktop.org/doc/dbus-specification.html#auth-protocol
+ * for full details on what this object does.
*
* @returns the new object or #NULL if no memory
*/
_dbus_assert (DBUS_AUTH_IS_SERVER (auth));
_dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
+ _dbus_authorization_unref (DBUS_AUTH_SERVER (auth)->authorization);
}
if (auth->keyring)
&auth->context, 0, _dbus_string_get_length (context));
}
+/**
+ * Sets whether unix fd passing is potentially on the transport and
+ * hence shall be negotiated.
+ *
+ * @param auth the auth conversation
+ * @param b TRUE when unix fd passing shall be negotiated, otherwise FALSE
+ */
+void
+_dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
+{
+ auth->unix_fd_possible = b;
+}
+
+/**
+ * Queries whether unix fd passing was successfully negotiated.
+ *
+ * @param auth the auth conversion
+ * @returns #TRUE when unix fd passing was negotiated.
+ */
+dbus_bool_t
+_dbus_auth_get_unix_fd_negotiated(DBusAuth *auth)
+{
+ return auth->unix_fd_negotiated;
+}
+
/** @} */
/* tests in dbus-auth-util.c */