aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-08-18 21:33:27 -0500
committerRob Landley <rob@landley.net>2016-08-18 21:33:27 -0500
commit3d64b0cc95c5957e53474c3e50f143c61a057104 (patch)
tree0c32cb7e4d104aa5d3c1f7ee0dcb7ac1f45f3a20
parent05e1582ec435932a25db21fd3ae71fbf62a4b45d (diff)
downloadtoybox-3d64b0cc95c5957e53474c3e50f143c61a057104.tar.gz
Change xgetpwnamid/xgetgrnamid to xgetuid/xgetgid returning the id number
instead of a struct. This means it can return "12345" even if that user/group doesn't exist in /etc/passwd and similar. All the users were immediately dereferencing it to get pw_uid or gr_gid anyway, so just return it directly and adjust the users. This fixes things like "chown 12345:23456 filename".
-rw-r--r--lib/lib.h4
-rw-r--r--lib/xwrap.c36
-rw-r--r--toys/other/makedevs.c4
-rw-r--r--toys/posix/chgrp.c4
-rw-r--r--toys/posix/cp.c4
-rw-r--r--toys/posix/find.c4
6 files changed, 26 insertions, 30 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 67d53035..43d6b1ff 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -153,8 +153,8 @@ struct passwd *xgetpwuid(uid_t uid);
struct group *xgetgrgid(gid_t gid);
struct passwd *xgetpwnam(char *name);
struct group *xgetgrnam(char *name);
-struct passwd *xgetpwnamid(char *user);
-struct group *xgetgrnamid(char *group);
+unsigned xgetuid(char *name);
+unsigned xgetgid(char *name);
void xsetuser(struct passwd *pwd);
char *xreadlink(char *name);
long xparsetime(char *arg, long units, long *fraction);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index a7b7bfcb..48e02965 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -590,36 +590,32 @@ struct group *xgetgrgid(gid_t gid)
return group;
}
-struct passwd *xgetpwnamid(char *user)
+unsigned xgetuid(char *name)
{
- struct passwd *up = getpwnam(user);
- uid_t uid;
+ struct passwd *up = getpwnam(name);
+ char *s = 0;
+ long uid;
- if (!up) {
- char *s = 0;
+ if (up) return up->pw_uid;
- uid = estrtol(user, &s, 10);
- if (!errno && s && !*s) up = getpwuid(uid);
- }
- if (!up) perror_exit("user '%s'", user);
+ uid = estrtol(name, &s, 10);
+ if (!errno && s && !*s && uid>=0 && uid<=UINT_MAX) return uid;
- return up;
+ error_exit("bad user '%s'", name);
}
-struct group *xgetgrnamid(char *group)
+unsigned xgetgid(char *name)
{
- struct group *gr = getgrnam(group);
- gid_t gid;
+ struct group *gr = getgrnam(name);
+ char *s = 0;
+ long gid;
- if (!gr) {
- char *s = 0;
+ if (gr) return gr->gr_gid;
- gid = estrtol(group, &s, 10);
- if (!errno && s && !*s) gr = getgrgid(gid);
- }
- if (!gr) perror_exit("group '%s'", group);
+ gid = estrtol(name, &s, 10);
+ if (!errno && s && !*s && gid>=0 && gid<=UINT_MAX) return gid;
- return gr;
+ error_exit("bad group '%s'", name);
}
struct passwd *xgetpwnam(char *name)
diff --git a/toys/other/makedevs.c b/toys/other/makedevs.c
index ed91fd93..5e6a9822 100644
--- a/toys/other/makedevs.c
+++ b/toys/other/makedevs.c
@@ -78,8 +78,8 @@ void makedevs_main()
continue;
} else mode |= (mode_t[]){S_IFIFO, S_IFCHR, S_IFBLK, 0, 0}[i];
- uid = *user ? xgetpwnamid(user)->pw_uid : getuid();
- gid = *group ? xgetgrnamid(group)->gr_gid : getgid();
+ uid = *user ? xgetuid(user) : getuid();
+ gid = *group ? xgetgid(group) : getgid();
while (*node == '/') node++; // using relative path
diff --git a/toys/posix/chgrp.c b/toys/posix/chgrp.c
index e0690c93..62e9eb1d 100644
--- a/toys/posix/chgrp.c
+++ b/toys/posix/chgrp.c
@@ -87,11 +87,11 @@ void chgrp_main(void)
*(grp++) = 0;
TT.group_name = grp;
}
- if (*own) TT.owner = xgetpwnamid(TT.owner_name = own)->pw_uid;
+ if (*own) TT.owner = xgetuid(TT.owner_name = own);
} else TT.group_name = *toys.optargs;
if (TT.group_name && *TT.group_name)
- TT.group = xgetgrnamid(TT.group_name)->gr_gid;
+ TT.group = xgetgid(TT.group_name);
for (s=toys.optargs+1; *s; s++)
dirtree_flagread(*s, DIRTREE_SYMFOLLOW*!!(toys.optflags&(FLAG_H|FLAG_L)),
diff --git a/toys/posix/cp.c b/toys/posix/cp.c
index eafabcd1..0dd63a7d 100644
--- a/toys/posix/cp.c
+++ b/toys/posix/cp.c
@@ -499,8 +499,8 @@ void install_main(void)
if (flags & FLAG_v) toys.optflags |= cp_flag_v();
if (flags & (FLAG_p|FLAG_o|FLAG_g)) toys.optflags |= cp_flag_p();
- if (TT.i.user) TT.uid = xgetpwnamid(TT.i.user)->pw_uid;
- if (TT.i.group) TT.gid = xgetgrnamid(TT.i.group)->gr_gid;
+ if (TT.i.user) TT.uid = xgetuid(TT.i.user);
+ if (TT.i.group) TT.gid = xgetgid(TT.i.group);
TT.callback = install_node;
cp_main();
diff --git a/toys/posix/find.c b/toys/posix/find.c
index 02f7702d..86fc141f 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -398,8 +398,8 @@ static int do_find(struct dirtree *new)
udl = xmalloc(sizeof(*udl));
dlist_add_nomalloc(&TT.argdata, (void *)udl);
- if (*s == 'u') udl->u.uid = xgetpwnamid(ss[1])->pw_uid;
- else if (*s == 'g') udl->u.gid = xgetgrnamid(ss[1])->gr_gid;
+ if (*s == 'u') udl->u.uid = xgetuid(ss[1]);
+ else if (*s == 'g') udl->u.gid = xgetgid(ss[1]);
else {
struct stat st;