diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-05-14 00:03:01 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-01-04 15:28:02 +0100 |
commit | 0e80e355b25629eb3a3dc077113f4ca1475f9f9c (patch) | |
tree | ebd66ab3abad5b5e808b54b4b2ae661623279518 /src | |
parent | homed: fix home_count_bad_authentication() counting (diff) | |
download | systemd-0e80e355b25629eb3a3dc077113f4ca1475f9f9c.tar.xz systemd-0e80e355b25629eb3a3dc077113f4ca1475f9f9c.zip |
homed: when empty username is passed to bus calls, operate on client's UID
Diffstat (limited to 'src')
-rw-r--r-- | src/home/homed-manager-bus.c | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/src/home/homed-manager-bus.c b/src/home/homed-manager-bus.c index b5dffb2c69..cc32a0e852 100644 --- a/src/home/homed-manager-bus.c +++ b/src/home/homed-manager-bus.c @@ -61,6 +61,53 @@ static int property_get_auto_login( return sd_bus_message_close_container(reply); } +static int lookup_user_name( + Manager *m, + sd_bus_message *message, + const char *user_name, + sd_bus_error *error, + Home **ret) { + + Home *h; + int r; + + assert(m); + assert(message); + assert(user_name); + assert(ret); + + if (isempty(user_name)) { + _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; + uid_t uid; + + /* If an empty user name is specified, then identify caller's EUID and find home by that. */ + + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); + if (r < 0) + return r; + + r = sd_bus_creds_get_euid(creds, &uid); + if (r < 0) + return r; + + h = hashmap_get(m->homes_by_uid, UID_TO_PTR(uid)); + if (!h) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "Client's UID " UID_FMT " not managed.", uid); + + } else { + + if (!valid_user_group_name(user_name, 0)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name); + + h = hashmap_get(m->homes_by_name, user_name); + if (!h) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name); + } + + *ret = h; + return 0; +} + static int method_get_home_by_name( sd_bus_message *message, void *userdata, @@ -77,12 +124,10 @@ static int method_get_home_by_name( r = sd_bus_message_read(message, "s", &user_name); if (r < 0) return r; - if (!valid_user_group_name(user_name, 0)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name); - h = hashmap_get(m->homes_by_name, user_name); - if (!h) - return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name); + r = lookup_user_name(m, message, user_name, error, &h); + if (r < 0) + return r; r = bus_home_path(h, &path); if (r < 0) @@ -204,12 +249,10 @@ static int method_get_user_record_by_name( r = sd_bus_message_read(message, "s", &user_name); if (r < 0) return r; - if (!valid_user_group_name(user_name, 0)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name); - h = hashmap_get(m->homes_by_name, user_name); - if (!h) - return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name); + r = lookup_user_name(m, message, user_name, error, &h); + if (r < 0) + return r; r = bus_home_get_record_json(h, message, &json, &incomplete); if (r < 0) @@ -278,12 +321,9 @@ static int generic_home_method( if (r < 0) return r; - if (!valid_user_group_name(user_name, 0)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "User name %s is not valid", user_name); - - h = hashmap_get(m->homes_by_name, user_name); - if (!h) - return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_HOME, "No home for user %s known", user_name); + r = lookup_user_name(m, message, user_name, error, &h); + if (r < 0) + return r; return handler(message, h, error); } |