From ddd88763921a1534081ed28e36f6712a85449005 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 23 Jul 2011 00:47:17 +0200 Subject: util: user parse_uid() wherever applicable --- src/execute.c | 6 +++--- src/loginctl.c | 28 ++++++++++++++++++---------- src/logind.c | 6 +++--- src/pam-module.c | 20 ++++++++++---------- src/sd-login.c | 8 ++------ src/tmpfiles.c | 12 ++++++------ src/util.c | 6 +++--- 7 files changed, 45 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/execute.c b/src/execute.c index 92f4eafd35..40af7d65f1 100644 --- a/src/execute.c +++ b/src/execute.c @@ -551,7 +551,7 @@ static int restore_confirm_stdio(const ExecContext *context, static int get_group_creds(const char *groupname, gid_t *gid) { struct group *g; - unsigned long lu; + gid_t id; assert(groupname); assert(gid); @@ -564,9 +564,9 @@ static int get_group_creds(const char *groupname, gid_t *gid) { return 0; } - if (safe_atolu(groupname, &lu) >= 0) { + if (parse_gid(groupname, &id) >= 0) { errno = 0; - g = getgrgid((gid_t) lu); + g = getgrgid(id); } else { errno = 0; g = getgrnam(groupname); diff --git a/src/loginctl.c b/src/loginctl.c index 829213e3d0..08ee804f43 100644 --- a/src/loginctl.c +++ b/src/loginctl.c @@ -1058,9 +1058,10 @@ static int show(DBusConnection *bus, char **args, unsigned n) { } } else if (strstr(args[0], "user")) { - uint32_t uid; + uid_t uid; + uint32_t u; - if (safe_atou(args[i], &uid) < 0) { + if (parse_uid(args[i], &uid) < 0) { struct passwd *pw; pw = getpwnam(args[i]); @@ -1084,8 +1085,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) { goto finish; } + u = (uint32_t) uid; if (!dbus_message_append_args(m, - DBUS_TYPE_UINT32, &uid, + DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID)) { log_error("Could not append arguments to message."); ret = -ENOMEM; @@ -1282,7 +1284,8 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { b = streq(args[0], "enable-linger"); for (i = 1; i < n; i++) { - uint32_t uid; + uint32_t u; + uid_t uid; m = dbus_message_new_method_call( "org.freedesktop.login1", @@ -1295,7 +1298,7 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(args[i], &uid) < 0) { + if (parse_uid(args[i], &uid) < 0) { struct passwd *pw; errno = 0; @@ -1309,8 +1312,9 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) { uid = pw->pw_uid; } + u = (uint32_t) uid; if (!dbus_message_append_args(m, - DBUS_TYPE_UINT32, &uid, + DBUS_TYPE_UINT32, &u, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_BOOLEAN, &interactive, DBUS_TYPE_INVALID)) { @@ -1356,6 +1360,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { for (i = 1; i < n; i++) { uint32_t u; + uid_t uid; m = dbus_message_new_method_call( "org.freedesktop.login1", @@ -1368,7 +1373,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(args[i], &u) < 0) { + if (parse_uid(args[i], &uid) < 0) { struct passwd *pw; errno = 0; @@ -1379,9 +1384,10 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - u = pw->pw_uid; + uid = pw->pw_uid; } + u = (uint32_t) uid; if (!dbus_message_append_args(m, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INVALID)) { @@ -1429,6 +1435,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { arg_kill_who = "all"; for (i = 1; i < n; i++) { + uid_t uid; uint32_t u; m = dbus_message_new_method_call( @@ -1442,7 +1449,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - if (safe_atou32(args[i], &u) < 0) { + if (parse_uid(args[i], &uid) < 0) { struct passwd *pw; errno = 0; @@ -1453,9 +1460,10 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) { goto finish; } - u = pw->pw_uid; + uid = pw->pw_uid; } + u = (uint32_t) uid; if (!dbus_message_append_args(m, DBUS_TYPE_UINT32, &u, DBUS_TYPE_INT32, arg_signal, diff --git a/src/logind.c b/src/logind.c index f96ace2315..a081501a8c 100644 --- a/src/logind.c +++ b/src/logind.c @@ -509,19 +509,19 @@ int manager_enumerate_users(Manager *m) { } while ((de = readdir(d))) { - unsigned long ul; + uid_t uid; User *u; if (!dirent_is_file(de)) continue; - k = safe_atolu(de->d_name, &ul); + k = parse_uid(de->d_name, &uid); if (k < 0) { log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k)); continue; } - u = hashmap_get(m->users, ULONG_TO_PTR(ul)); + u = hashmap_get(m->users, ULONG_TO_PTR(uid)); if (!u) { unlinkat(dirfd(d), de->d_name, 0); continue; diff --git a/src/pam-module.c b/src/pam-module.c index dfeab97f53..a3ce2468d5 100644 --- a/src/pam-module.c +++ b/src/pam-module.c @@ -180,14 +180,14 @@ static int get_user_data( * it probably contains a uid of the host system. */ if (read_one_line_file("/proc/self/loginuid", &s) >= 0) { - uint32_t u; + uid_t uid; - r = safe_atou32(s, &u); + r = parse_uid(s, &uid); free(s); - if (r >= 0 && u != (uint32_t) -1 && u > 0) { + if (r >= 0 && uid != (uint32_t) -1) { have_loginuid = true; - pw = pam_modutil_getpwuid(handle, u); + pw = pam_modutil_getpwuid(handle, uid); } } } @@ -239,10 +239,10 @@ static bool check_user_lists( } STRV_FOREACH(l, kill_exclude_users) { - uint32_t id; + uid_t u; - if (safe_atou32(*l, &id) >= 0) - if ((uid_t) id == uid) + if (parse_uid(*l, &u) >= 0) + if (u == uid) return false; if (name && streq(name, *l)) @@ -253,10 +253,10 @@ static bool check_user_lists( return true; STRV_FOREACH(l, kill_only_users) { - uint32_t id; + uid_t u; - if (safe_atou32(*l, &id) >= 0) - if ((uid_t) id == uid) + if (parse_uid(*l, &u) >= 0) + if (u == uid) return true; if (name && streq(name, *l)) diff --git a/src/sd-login.c b/src/sd-login.c index 6dfc2d086e..b7ae870af4 100644 --- a/src/sd-login.c +++ b/src/sd-login.c @@ -349,14 +349,10 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) { if (!s) return -EIO; - r = safe_atolu(s, &ul); + r = parse_uid(s, uid); free(s); - if (r < 0) - return r; - - *uid = (uid_t) ul; - return 0; + return r; } _public_ int sd_session_get_seat(const char *session, char **seat) { diff --git a/src/tmpfiles.c b/src/tmpfiles.c index feb2a21463..a1b2f8b1d2 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -757,13 +757,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } if (user && !streq(user, "-")) { - unsigned long lu; + uid_t uid; struct passwd *p; if (streq(user, "root") || streq(user, "0")) i->uid = 0; - else if (safe_atolu(user, &lu) >= 0) - i->uid = (uid_t) lu; + else if (parse_uid(user, &uid) >= 0) + i->uid = uid; else if ((p = getpwnam(user))) i->uid = p->pw_uid; else { @@ -776,13 +776,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } if (group && !streq(group, "-")) { - unsigned long lu; + gid_t gid; struct group *g; if (streq(group, "root") || streq(group, "0")) i->gid = 0; - else if (safe_atolu(group, &lu) >= 0) - i->gid = (gid_t) lu; + else if (parse_gid(group, &gid) >= 0) + i->gid = gid; else if ((g = getgrnam(group))) i->gid = g->gr_gid; else { diff --git a/src/util.c b/src/util.c index 2d4f229193..3a82ef7600 100644 --- a/src/util.c +++ b/src/util.c @@ -5262,7 +5262,7 @@ int socket_from_display(const char *display, char **path) { int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { struct passwd *p; - unsigned long lu; + uid_t u; assert(username); assert(*username); @@ -5281,9 +5281,9 @@ int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **h return 0; } - if (safe_atolu(*username, &lu) >= 0) { + if (parse_uid(*username, &u) >= 0) { errno = 0; - p = getpwuid((uid_t) lu); + p = getpwuid(u); /* If there are multiple users with the same id, make * sure to leave $USER to the configured value instead -- cgit v1.2.3-54-g00ecf