From 51fb9e5fe9bf561fc961742b007204bfe3d8ac95 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 12 Jan 2018 16:06:22 +0000 Subject: [PATCH 3/7] _dbus_credentials_add_from_user: Add proper error reporting While I'm changing its signature anyway, I might as well fix a long-standing FIXME. Signed-off-by: Simon McVittie --- dbus/dbus-auth.c | 30 ++++++++++++++++++++++++------ dbus/dbus-sysdeps-win.c | 15 +++++++++++---- dbus/dbus-sysdeps.h | 6 ++++-- dbus/dbus-userdb.c | 15 ++++++++++----- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/dbus/dbus-auth.c b/dbus/dbus-auth.c index 37d8d4c9..06de633a 100644 --- a/dbus/dbus-auth.c +++ b/dbus/dbus-auth.c @@ -549,10 +549,18 @@ sha1_handle_first_client_response (DBusAuth *auth, } } - if (!_dbus_credentials_add_from_user (auth->desired_identity, data)) + if (!_dbus_credentials_add_from_user (auth->desired_identity, data, + &error)) { - _dbus_verbose ("%s: Did not get a valid username from client\n", - DBUS_AUTH_NAME (auth)); + if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + { + dbus_error_free (&error); + goto out; + } + + _dbus_verbose ("%s: Did not get a valid username from client: %s\n", + DBUS_AUTH_NAME (auth), error.message); + dbus_error_free (&error); return send_rejected (auth); } @@ -1108,11 +1116,21 @@ handle_server_data_external_mech (DBusAuth *auth, } else { + DBusError error = DBUS_ERROR_INIT; + if (!_dbus_credentials_add_from_user (auth->desired_identity, - &auth->identity)) + &auth->identity, + &error)) { - _dbus_verbose ("%s: could not get credentials from uid string\n", - DBUS_AUTH_NAME (auth)); + if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + { + dbus_error_free (&error); + return FALSE; + } + + _dbus_verbose ("%s: could not get credentials from uid string: %s\n", + DBUS_AUTH_NAME (auth), error.message); + dbus_error_free (&error); return send_rejected (auth); } } diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 90a55a7c..4ad87edc 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -2064,11 +2064,18 @@ _dbus_concat_dir_and_file (DBusString *dir, * @returns #TRUE if the username existed and we got some credentials */ dbus_bool_t -_dbus_credentials_add_from_user (DBusCredentials *credentials, - const DBusString *username) +_dbus_credentials_add_from_user (DBusCredentials *credentials, + const DBusString *username, + DBusError *error) { - return _dbus_credentials_add_windows_sid (credentials, - _dbus_string_get_const_data(username)); + if (!_dbus_credentials_add_windows_sid (credentials, + _dbus_string_get_const_data (username))) + { + _DBUS_SET_OOM (error); + return FALSE; + } + + return TRUE; } /** diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 6892247f..48d3e766 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -240,8 +240,10 @@ dbus_bool_t _dbus_read_credentials_socket (DBusSocket client_fd, dbus_bool_t _dbus_send_credentials_socket (DBusSocket server_fd, DBusError *error); -dbus_bool_t _dbus_credentials_add_from_user (DBusCredentials *credentials, - const DBusString *username); +dbus_bool_t _dbus_credentials_add_from_user (DBusCredentials *credentials, + const DBusString *username, + DBusError *error); + dbus_bool_t _dbus_credentials_add_from_current_process (DBusCredentials *credentials); DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_append_user_from_current_process (DBusString *str); diff --git a/dbus/dbus-userdb.c b/dbus/dbus-userdb.c index 6199b855..b96aef43 100644 --- a/dbus/dbus-userdb.c +++ b/dbus/dbus-userdb.c @@ -524,8 +524,9 @@ _dbus_homedir_from_uid (dbus_uid_t uid, * @returns #TRUE if the username existed and we got some credentials */ dbus_bool_t -_dbus_credentials_add_from_user (DBusCredentials *credentials, - const DBusString *username) +_dbus_credentials_add_from_user (DBusCredentials *credentials, + const DBusString *username, + DBusError *error) { DBusUserDatabase *db; const DBusUserInfo *info; @@ -542,19 +543,22 @@ _dbus_credentials_add_from_user (DBusCredentials *credentials, return TRUE; } - /* FIXME: this can't distinguish ENOMEM from other errors */ if (!_dbus_user_database_lock_system ()) - return FALSE; + { + _DBUS_SET_OOM (error); + return FALSE; + } db = _dbus_user_database_get_system (); if (db == NULL) { _dbus_user_database_unlock_system (); + _DBUS_SET_OOM (error); return FALSE; } if (!_dbus_user_database_get_username (db, username, - &info, NULL)) + &info, error)) { _dbus_user_database_unlock_system (); return FALSE; @@ -563,6 +567,7 @@ _dbus_credentials_add_from_user (DBusCredentials *credentials, if (!_dbus_credentials_add_unix_uid(credentials, info->uid)) { _dbus_user_database_unlock_system (); + _DBUS_SET_OOM (error); return FALSE; } -- 2.15.1