1
0
mirror of https://github.com/systemd/systemd.git synced 2025-09-09 17:44:49 +03:00

util: user parse_uid() wherever applicable

This commit is contained in:
Lennart Poettering
2011-07-23 00:47:17 +02:00
parent 034a2a52ac
commit ddd8876392
8 changed files with 47 additions and 41 deletions

2
TODO
View File

@@ -20,6 +20,8 @@ F15 External:
Features: Features:
* fix CUPS .path unit for globbing
* move PAM code into its own binary * move PAM code into its own binary
* logind: ensure ACLs are updated on login and logout * logind: ensure ACLs are updated on login and logout

View File

@@ -551,7 +551,7 @@ static int restore_confirm_stdio(const ExecContext *context,
static int get_group_creds(const char *groupname, gid_t *gid) { static int get_group_creds(const char *groupname, gid_t *gid) {
struct group *g; struct group *g;
unsigned long lu; gid_t id;
assert(groupname); assert(groupname);
assert(gid); assert(gid);
@@ -564,9 +564,9 @@ static int get_group_creds(const char *groupname, gid_t *gid) {
return 0; return 0;
} }
if (safe_atolu(groupname, &lu) >= 0) { if (parse_gid(groupname, &id) >= 0) {
errno = 0; errno = 0;
g = getgrgid((gid_t) lu); g = getgrgid(id);
} else { } else {
errno = 0; errno = 0;
g = getgrnam(groupname); g = getgrnam(groupname);

View File

@@ -1058,9 +1058,10 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
} }
} else if (strstr(args[0], "user")) { } 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; struct passwd *pw;
pw = getpwnam(args[i]); pw = getpwnam(args[i]);
@@ -1084,8 +1085,9 @@ static int show(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
u = (uint32_t) uid;
if (!dbus_message_append_args(m, if (!dbus_message_append_args(m,
DBUS_TYPE_UINT32, &uid, DBUS_TYPE_UINT32, &u,
DBUS_TYPE_INVALID)) { DBUS_TYPE_INVALID)) {
log_error("Could not append arguments to message."); log_error("Could not append arguments to message.");
ret = -ENOMEM; ret = -ENOMEM;
@@ -1282,7 +1284,8 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
b = streq(args[0], "enable-linger"); b = streq(args[0], "enable-linger");
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
uint32_t uid; uint32_t u;
uid_t uid;
m = dbus_message_new_method_call( m = dbus_message_new_method_call(
"org.freedesktop.login1", "org.freedesktop.login1",
@@ -1295,7 +1298,7 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
if (safe_atou32(args[i], &uid) < 0) { if (parse_uid(args[i], &uid) < 0) {
struct passwd *pw; struct passwd *pw;
errno = 0; errno = 0;
@@ -1309,8 +1312,9 @@ static int enable_linger(DBusConnection *bus, char **args, unsigned n) {
uid = pw->pw_uid; uid = pw->pw_uid;
} }
u = (uint32_t) uid;
if (!dbus_message_append_args(m, if (!dbus_message_append_args(m,
DBUS_TYPE_UINT32, &uid, DBUS_TYPE_UINT32, &u,
DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_BOOLEAN, &b,
DBUS_TYPE_BOOLEAN, &interactive, DBUS_TYPE_BOOLEAN, &interactive,
DBUS_TYPE_INVALID)) { DBUS_TYPE_INVALID)) {
@@ -1356,6 +1360,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
uint32_t u; uint32_t u;
uid_t uid;
m = dbus_message_new_method_call( m = dbus_message_new_method_call(
"org.freedesktop.login1", "org.freedesktop.login1",
@@ -1368,7 +1373,7 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
if (safe_atou32(args[i], &u) < 0) { if (parse_uid(args[i], &uid) < 0) {
struct passwd *pw; struct passwd *pw;
errno = 0; errno = 0;
@@ -1379,9 +1384,10 @@ static int terminate_user(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
u = pw->pw_uid; uid = pw->pw_uid;
} }
u = (uint32_t) uid;
if (!dbus_message_append_args(m, if (!dbus_message_append_args(m,
DBUS_TYPE_UINT32, &u, DBUS_TYPE_UINT32, &u,
DBUS_TYPE_INVALID)) { DBUS_TYPE_INVALID)) {
@@ -1429,6 +1435,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) {
arg_kill_who = "all"; arg_kill_who = "all";
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
uid_t uid;
uint32_t u; uint32_t u;
m = dbus_message_new_method_call( m = dbus_message_new_method_call(
@@ -1442,7 +1449,7 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
if (safe_atou32(args[i], &u) < 0) { if (parse_uid(args[i], &uid) < 0) {
struct passwd *pw; struct passwd *pw;
errno = 0; errno = 0;
@@ -1453,9 +1460,10 @@ static int kill_user(DBusConnection *bus, char **args, unsigned n) {
goto finish; goto finish;
} }
u = pw->pw_uid; uid = pw->pw_uid;
} }
u = (uint32_t) uid;
if (!dbus_message_append_args(m, if (!dbus_message_append_args(m,
DBUS_TYPE_UINT32, &u, DBUS_TYPE_UINT32, &u,
DBUS_TYPE_INT32, arg_signal, DBUS_TYPE_INT32, arg_signal,

View File

@@ -509,19 +509,19 @@ int manager_enumerate_users(Manager *m) {
} }
while ((de = readdir(d))) { while ((de = readdir(d))) {
unsigned long ul; uid_t uid;
User *u; User *u;
if (!dirent_is_file(de)) if (!dirent_is_file(de))
continue; continue;
k = safe_atolu(de->d_name, &ul); k = parse_uid(de->d_name, &uid);
if (k < 0) { if (k < 0) {
log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k)); log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k));
continue; continue;
} }
u = hashmap_get(m->users, ULONG_TO_PTR(ul)); u = hashmap_get(m->users, ULONG_TO_PTR(uid));
if (!u) { if (!u) {
unlinkat(dirfd(d), de->d_name, 0); unlinkat(dirfd(d), de->d_name, 0);
continue; continue;

View File

@@ -180,14 +180,14 @@ static int get_user_data(
* it probably contains a uid of the host system. */ * it probably contains a uid of the host system. */
if (read_one_line_file("/proc/self/loginuid", &s) >= 0) { 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); free(s);
if (r >= 0 && u != (uint32_t) -1 && u > 0) { if (r >= 0 && uid != (uint32_t) -1) {
have_loginuid = true; 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) { STRV_FOREACH(l, kill_exclude_users) {
uint32_t id; uid_t u;
if (safe_atou32(*l, &id) >= 0) if (parse_uid(*l, &u) >= 0)
if ((uid_t) id == uid) if (u == uid)
return false; return false;
if (name && streq(name, *l)) if (name && streq(name, *l))
@@ -253,10 +253,10 @@ static bool check_user_lists(
return true; return true;
STRV_FOREACH(l, kill_only_users) { STRV_FOREACH(l, kill_only_users) {
uint32_t id; uid_t u;
if (safe_atou32(*l, &id) >= 0) if (parse_uid(*l, &u) >= 0)
if ((uid_t) id == uid) if (u == uid)
return true; return true;
if (name && streq(name, *l)) if (name && streq(name, *l))

View File

@@ -349,14 +349,10 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
if (!s) if (!s)
return -EIO; return -EIO;
r = safe_atolu(s, &ul); r = parse_uid(s, uid);
free(s); free(s);
if (r < 0) return r;
return r;
*uid = (uid_t) ul;
return 0;
} }
_public_ int sd_session_get_seat(const char *session, char **seat) { _public_ int sd_session_get_seat(const char *session, char **seat) {

View File

@@ -757,13 +757,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
if (user && !streq(user, "-")) { if (user && !streq(user, "-")) {
unsigned long lu; uid_t uid;
struct passwd *p; struct passwd *p;
if (streq(user, "root") || streq(user, "0")) if (streq(user, "root") || streq(user, "0"))
i->uid = 0; i->uid = 0;
else if (safe_atolu(user, &lu) >= 0) else if (parse_uid(user, &uid) >= 0)
i->uid = (uid_t) lu; i->uid = uid;
else if ((p = getpwnam(user))) else if ((p = getpwnam(user)))
i->uid = p->pw_uid; i->uid = p->pw_uid;
else { else {
@@ -776,13 +776,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
} }
if (group && !streq(group, "-")) { if (group && !streq(group, "-")) {
unsigned long lu; gid_t gid;
struct group *g; struct group *g;
if (streq(group, "root") || streq(group, "0")) if (streq(group, "root") || streq(group, "0"))
i->gid = 0; i->gid = 0;
else if (safe_atolu(group, &lu) >= 0) else if (parse_gid(group, &gid) >= 0)
i->gid = (gid_t) lu; i->gid = gid;
else if ((g = getgrnam(group))) else if ((g = getgrnam(group)))
i->gid = g->gr_gid; i->gid = g->gr_gid;
else { else {

View File

@@ -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) { int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) {
struct passwd *p; struct passwd *p;
unsigned long lu; uid_t u;
assert(username); assert(username);
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; return 0;
} }
if (safe_atolu(*username, &lu) >= 0) { if (parse_uid(*username, &u) >= 0) {
errno = 0; errno = 0;
p = getpwuid((uid_t) lu); p = getpwuid(u);
/* If there are multiple users with the same id, make /* If there are multiple users with the same id, make
* sure to leave $USER to the configured value instead * sure to leave $USER to the configured value instead