From 5ec4ab3113dcc813b6040d7ded38e297df99dc0e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 28 Nov 2013 21:06:15 -0600 Subject: Add xgetpwnam() to lib/xwrap.c. --- lib/lib.h | 1 + lib/xwrap.c | 11 +++++++++-- toys/lsb/passwd.c | 12 +++++------- toys/pending/groupadd.c | 3 +-- toys/pending/tftpd.c | 6 +----- toys/posix/id.c | 6 ++---- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index e5d4a1d3..2d3e2734 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -113,6 +113,7 @@ void xmkpath(char *path, int mode); void xsetuid(uid_t uid); struct passwd *xgetpwuid(uid_t uid); struct group *xgetgrgid(gid_t gid); +struct passwd *xgetpwnam(char *name); char *xreadlink(char *name); long xparsetime(char *arg, long units, long *fraction); void xpidfile(char *name); diff --git a/lib/xwrap.c b/lib/xwrap.c index 71ea9209..c0c8a44b 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -402,17 +402,24 @@ void xsetuid(uid_t uid) struct passwd *xgetpwuid(uid_t uid) { struct passwd *pwd = getpwuid(uid); - if (!pwd) error_exit(NULL); + if (!pwd) error_exit("bad uid %ld", (long)uid); return pwd; } struct group *xgetgrgid(gid_t gid) { struct group *group = getgrgid(gid); - if (!group) error_exit(NULL); + if (!group) error_exit("bad gid %ld", (long)gid); return group; } +struct passwd *xgetpwnam(char *name) +{ + struct passwd *up = getpwnam(name); + if (!up) error_exit("bad user '%s'", name); + return up; +} + // This can return null (meaning file not found). It just won't return null // for memory allocation reasons. char *xreadlink(char *name) diff --git a/toys/lsb/passwd.c b/toys/lsb/passwd.c index 1784c049..f3338361 100644 --- a/toys/lsb/passwd.c +++ b/toys/lsb/passwd.c @@ -103,17 +103,15 @@ void passwd_main(void) int ret = -1; myuid = getuid(); - if ((myuid) && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) - error_exit("You need to be root to do these actions\n"); + if (myuid && (toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) + error_exit("Not root"); - pw = getpwuid(myuid); - if (!pw) error_exit("Unknown uid '%u'",myuid); + pw = xgetpwuid(myuid); - if (toys.optargs[0]) name = toys.optargs[0]; + if (*toys.optargs) name = toys.optargs[0]; else name = xstrdup(pw->pw_name); - pw = getpwnam(name); - if (!pw) error_exit("Unknown user '%s'",name); + pw = xgetpwnam(name); if (myuid && (myuid != pw->pw_uid)) error_exit("You need to be root to change '%s' password\n", name); diff --git a/toys/pending/groupadd.c b/toys/pending/groupadd.c index ab290e54..8ff539eb 100644 --- a/toys/pending/groupadd.c +++ b/toys/pending/groupadd.c @@ -79,8 +79,7 @@ void groupadd_main(void) if (toys.optc == 2) { //add user to group //toys.optargs[0]- user, toys.optargs[1] - group - if (!getpwnam(toys.optargs[0])) - error_exit("user '%s' does not exist", toys.optargs[0]); + xgetpwnam(*toys.optargs); if (!(grp = getgrnam(toys.optargs[1]))) error_exit("group '%s' does not exist", toys.optargs[1]); if (!grp->gr_mem) entry = xmsprintf("%s", *toys.optargs); diff --git a/toys/pending/tftpd.c b/toys/pending/tftpd.c index 3e7264b5..ea8d3eac 100644 --- a/toys/pending/tftpd.c +++ b/toys/pending/tftpd.c @@ -249,11 +249,7 @@ void tftpd_main(void) error_exit(NULL); } - if (toys.optflags & FLAG_u) { - struct passwd *pw = getpwnam(TT.user); - if (!pw) error_exit("unknown user %s", TT.user); - TT.pw = pw; - } + if (TT.user) TT.pw = xgetpwnam(TT.user); if (*toys.optargs) { if (chroot(*toys.optargs)) perror_exit("can't change root directory to '%s'", *toys.optargs); diff --git a/toys/posix/id.c b/toys/posix/id.c index f40f6c1b..8b68d4de 100644 --- a/toys/posix/id.c +++ b/toys/posix/id.c @@ -67,12 +67,10 @@ void do_id(char *username) // check if a username is given if (username) { - if (!(pw = getpwnam(username))) - error_exit("no such user '%s'", username); + pw = xgetpwnam(username); uid = euid = pw->pw_uid; gid = egid = pw->pw_gid; - if (cmd_groups) - printf("%s : ", pw->pw_name); + if (cmd_groups) printf("%s : ", pw->pw_name); } i = flags & FLAG_r; -- cgit v1.2.3