OSDN Git Service

Add xgetpwnam() to lib/xwrap.c.
authorRob Landley <rob@landley.net>
Fri, 29 Nov 2013 03:06:15 +0000 (21:06 -0600)
committerRob Landley <rob@landley.net>
Fri, 29 Nov 2013 03:06:15 +0000 (21:06 -0600)
lib/lib.h
lib/xwrap.c
toys/lsb/passwd.c
toys/pending/groupadd.c
toys/pending/tftpd.c
toys/posix/id.c

index e5d4a1d..2d3e273 100644 (file)
--- 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);
index 71ea920..c0c8a44 100644 (file)
@@ -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)
index 1784c04..f333836 100644 (file)
@@ -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);
index ab290e5..8ff539e 100644 (file)
@@ -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);
index 3e7264b..ea8d3ea 100644 (file)
@@ -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);
index f40f6c1..8b68d4d 100644 (file)
@@ -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;