From cf45c27c0b81d495e961ac83a8bbd9b6569047a7 Mon Sep 17 00:00:00 2001 From: Gerrit Pape Date: Tue, 17 Apr 2007 07:49:09 +0000 Subject: chpst.c, uidgid.c: improve error reporting if getpwnam() or getgrnam() fails ...cleanup. --- src/chpst.c | 30 +++++++++++++++--------------- src/uidgid.c | 40 ++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 35 deletions(-) (limited to 'src') 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); } -- cgit 1.4.1