summary refs log tree commit diff
diff options
context:
space:
mode:
authorGerrit Pape <pape@smarden.org>2007-04-17 07:49:09 +0000
committerGerrit Pape <pape@innominate.com>2007-04-17 07:49:09 +0000
commitcf45c27c0b81d495e961ac83a8bbd9b6569047a7 (patch)
tree423d0f22e8ec954db6c1060d50463ac64bc5921b
parent157727221652084a17037478826af086127a6956 (diff)
downloadrunit-cf45c27c0b81d495e961ac83a8bbd9b6569047a7.tar.gz
runit-cf45c27c0b81d495e961ac83a8bbd9b6569047a7.tar.xz
runit-cf45c27c0b81d495e961ac83a8bbd9b6569047a7.zip
chpst.c, uidgid.c: improve error reporting if getpwnam() or getgrnam() fails
...cleanup.
-rw-r--r--package/CHANGES2
-rw-r--r--src/chpst.c30
-rw-r--r--src/uidgid.c40
3 files changed, 37 insertions, 35 deletions
diff --git a/package/CHANGES b/package/CHANGES
index 194b90b..c87d85a 100644
--- a/package/CHANGES
+++ b/package/CHANGES
@@ -2,6 +2,8 @@
     number of log files svlogd should maintain has been reduced; add hint
     about increasing the buffer size if lots of data is to be processed
     in short time.
+  * chpst.c, uidgid.c: improve error reporting if getpwnam() or getgrnam()
+    fails; cleanup.
 
 1.7.2
 Tue, 21 Nov 2006 15:13:47 +0000
diff --git a/src/chpst.c b/src/chpst.c
index 9df2fb6..8f84928 100644
--- a/src/chpst.c
+++ b/src/chpst.c
@@ -64,17 +64,17 @@ unsigned int lockdelay;
 void suidgid(char *user, unsigned int ext) {
   struct uidgid ugid;
 
-  if (ext) {
+  if (ext)
     if (! uidgids_get(&ugid, user)) {
-      if (*user == ':')
-        fatalx("invalid uid/gids", user +1);
-      else
+      if (*user == ':') fatalx("invalid uid/gids", user +1);
+      if (errno) fatal("unable to get password/group file entry");
       fatalx("unknown user/group", user);
     }
-  }
-    else
-    if (! uidgid_get(&ugid, user))
+  else
+    if (! uidgid_get(&ugid, user)) {
+      if (errno) fatal("unable to get password file entry");
       fatalx("unknown account", user);
+    }
   if (setgroups(ugid.gids, ugid.gid) == -1) fatal("unable to setgroups");
   if (setgid(*ugid.gid) == -1) fatal("unable to setgid");
   if (prot_uid(ugid.uid) == -1) fatal("unable to setuid");
@@ -84,17 +84,17 @@ void euidgid(char *user, unsigned int ext) {
   struct uidgid ugid;
   char bufnum[FMT_ULONG];
 
-  if (ext) {
+  if (ext)
     if (! uidgids_get(&ugid, user)) {
-      if (*user == ':')
-        fatalx("invalid uid/gids", user +1);
-      else
+      if (*user == ':') fatalx("invalid uid/gids", user +1);
+      if (errno) fatal("unable to get password/group file entry");
       fatalx("unknown user/group", user);
     }
-  }
-    else
-    if (! uidgid_get(&ugid, user))
+  else
+    if (! uidgid_get(&ugid, user)) {
+      if (errno) fatal("unable to get password file entry");
       fatalx("unknown account", user);
+    }
   bufnum[fmt_ulong(bufnum, *ugid.gid)] =0;
   if (! pathexec_env("GID", bufnum)) die_nomem();
   bufnum[fmt_ulong(bufnum, ugid.uid)] =0;
@@ -332,7 +332,6 @@ int main(int argc, const char *const *argv) {
     if (chdir(root) == -1) fatal2("unable to change directory", root);
     if (chroot(".") == -1) fatal("unable to change root directory");
   }
-  slimit();
   if (nicelvl) {
     errno =0;
     if (nice(nicelvl) == -1) if (errno) fatal("unable to set nice level");
@@ -343,6 +342,7 @@ int main(int argc, const char *const *argv) {
   if (nostdin) if (close(0) == -1) fatal("unable to close stdin");
   if (nostdout) if (close(1) == -1) fatal("unable to close stdout");
   if (nostderr) if (close(2) == -1) fatal("unable to close stderr");
+  slimit();
   pathexec(argv);
   fatal2("unable to run", *argv);
   return(0);
diff --git a/src/uidgid.c b/src/uidgid.c
index 718d7a9..befa754 100644
--- a/src/uidgid.c
+++ b/src/uidgid.c
@@ -42,10 +42,10 @@ unsigned int uidgids_get(struct uidgid *u, char *ug) {
   int i, d =0;
 
   if (*ug == ':') return(uidgids_set(u, ug +1));
-    if (ug[(d =str_chr(ug, ':'))] == ':') {
-      ug[d] =0;
-      g =ug +d +1;
-    }
+  if (ug[(d =str_chr(ug, ':'))] == ':') {
+    ug[d] =0;
+    g =ug +d +1;
+  }
   if (! (pwd =getpwnam(ug))) { if (g) ug[d] =':'; return(0); }
   u->uid =pwd->pw_uid;
   if (! g) {
@@ -53,22 +53,22 @@ unsigned int uidgids_get(struct uidgid *u, char *ug) {
     u->gids =1;
     return(1);
   }
-    ug[d] =':';
-    for (i =0; i < 60; ++i) {
-      if (g[(d =str_chr(g, ':'))] == ':') {
-        g[d] =0;
-        if (! (gr =getgrnam(g))) { g[d] =':'; return(0); }
-        g[d] =':';
-        u->gid[i] =gr->gr_gid;
-        g +=d +1;
-      }
-      else {
-        if (! (gr =getgrnam(g))) return(0);
-        u->gid[i++] =gr->gr_gid;
-        break;
-      }
+  ug[d] =':';
+  for (i =0; i < 60; ++i) {
+    if (g[(d =str_chr(g, ':'))] == ':') {
+      g[d] =0;
+      if (! (gr =getgrnam(g))) { g[d] =':'; return(0); }
+      g[d] =':';
+      u->gid[i] =gr->gr_gid;
+      g +=d +1;
+    }
+    else {
+      if (! (gr =getgrnam(g))) return(0);
+      u->gid[i++] =gr->gr_gid;
+      break;
     }
-    u->gid[i] =0;
-    u->gids =i;
+  }
+  u->gid[i] =0;
+  u->gids =i;
   return(1);
 }