changeset 1130:6df194c6de88 draft

Add xgetpwnam() to lib/xwrap.c.
author Rob Landley <rob@landley.net>
date Thu, 28 Nov 2013 21:06:15 -0600
parents c644f85444d0
children f9678ea553c8
files lib/lib.h lib/xwrap.c toys/lsb/passwd.c toys/pending/groupadd.c toys/pending/tftpd.c toys/posix/id.c
diffstat 6 files changed, 19 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.h	Thu Nov 28 20:18:04 2013 -0600
+++ b/lib/lib.h	Thu Nov 28 21:06:15 2013 -0600
@@ -113,6 +113,7 @@
 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);
--- a/lib/xwrap.c	Thu Nov 28 20:18:04 2013 -0600
+++ b/lib/xwrap.c	Thu Nov 28 21:06:15 2013 -0600
@@ -402,17 +402,24 @@
 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)
--- a/toys/lsb/passwd.c	Thu Nov 28 20:18:04 2013 -0600
+++ b/toys/lsb/passwd.c	Thu Nov 28 21:06:15 2013 -0600
@@ -103,17 +103,15 @@
   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);
--- a/toys/pending/groupadd.c	Thu Nov 28 20:18:04 2013 -0600
+++ b/toys/pending/groupadd.c	Thu Nov 28 21:06:15 2013 -0600
@@ -79,8 +79,7 @@
 
   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);
--- a/toys/pending/tftpd.c	Thu Nov 28 20:18:04 2013 -0600
+++ b/toys/pending/tftpd.c	Thu Nov 28 21:06:15 2013 -0600
@@ -249,11 +249,7 @@
     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);
--- a/toys/posix/id.c	Thu Nov 28 20:18:04 2013 -0600
+++ b/toys/posix/id.c	Thu Nov 28 21:06:15 2013 -0600
@@ -67,12 +67,10 @@
 
   // 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;