From: millert Date: Sun, 16 Sep 2018 02:44:06 +0000 (+0000) Subject: Use uid_from_user(3) and gid_from_group(3) in utilities that X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=a0f924b8c9b18f7537c81c458bcb9a4846a4802d;p=openbsd Use uid_from_user(3) and gid_from_group(3) in utilities that do repeated lookups. OK tb@ --- diff --git a/bin/chmod/chmod.c b/bin/chmod/chmod.c index 62ad272f262..449ac707ea1 100644 --- a/bin/chmod/chmod.c +++ b/bin/chmod/chmod.c @@ -1,4 +1,4 @@ -/* $OpenBSD: chmod.c,v 1.42 2017/05/28 08:03:36 awolk Exp $ */ +/* $OpenBSD: chmod.c,v 1.43 2018/09/16 02:44:06 millert Exp $ */ /* $NetBSD: chmod.c,v 1.12 1995/03/21 09:02:09 cgd Exp $ */ /* @@ -293,7 +293,6 @@ done: uid_t a_uid(const char *s, int silent) { - struct passwd *pw; const char *errstr; uid_t uid; @@ -301,8 +300,8 @@ a_uid(const char *s, int silent) return ((uid_t)-1); /* User name was given. */ - if ((pw = getpwnam(s)) != NULL) - return (pw->pw_uid); + if (uid_from_user(s, &uid) != -1) + return (uid); /* UID was given. */ uid = (uid_t)strtonum(s, 0, UID_MAX, &errstr); @@ -323,7 +322,6 @@ a_uid(const char *s, int silent) gid_t a_gid(const char *s) { - struct group *gr; const char *errstr; gid_t gid; @@ -331,8 +329,8 @@ a_gid(const char *s) return ((gid_t)-1); /* Group name was given. */ - if ((gr = getgrnam(s)) != NULL) - return (gr->gr_gid); + if (gid_from_group(s, &gid) != -1) + return (gid); /* GID was given. */ gid = (gid_t)strtonum(s, 0, GID_MAX, &errstr); diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 6c054a5d34e..7892805e172 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ps.c,v 1.72 2018/08/08 14:38:31 deraadt Exp $ */ +/* $OpenBSD: ps.c,v 1.73 2018/09/16 02:44:06 millert Exp $ */ /* $NetBSD: ps.c,v 1.15 1995/05/18 20:33:25 mycroft Exp $ */ /*- @@ -92,7 +92,6 @@ main(int argc, char *argv[]) struct kinfo_proc *kp, **kinfo; struct varent *vent; struct winsize ws; - struct passwd *pwd; dev_t ttydev; pid_t pid; uid_t uid; @@ -217,11 +216,8 @@ main(int argc, char *argv[]) break; } case 'U': - pwd = getpwnam(optarg); - if (pwd == NULL) + if (uid_from_user(optarg, &uid) == -1) errx(1, "%s: no such user", optarg); - uid = pwd->pw_uid; - endpwent(); Uflag = xflg = 1; break; case 'u': diff --git a/sbin/fsdb/fsdb.c b/sbin/fsdb/fsdb.c index c82fc676bf4..49128eae52f 100644 --- a/sbin/fsdb/fsdb.c +++ b/sbin/fsdb/fsdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fsdb.c,v 1.31 2016/09/09 15:37:14 tb Exp $ */ +/* $OpenBSD: fsdb.c,v 1.32 2018/09/16 02:44:06 millert Exp $ */ /* $NetBSD: fsdb.c,v 1.7 1997/01/11 06:50:53 lukem Exp $ */ /*- @@ -760,7 +760,6 @@ CMDFUNCSTART(chowner) int rval = 1; uid_t uid; char *cp; - struct passwd *pwd; if (!checkactive()) return 1; @@ -768,9 +767,7 @@ CMDFUNCSTART(chowner) uid = strtoul(argv[1], &cp, 0); if (cp == argv[1] || *cp != '\0' ) { /* try looking up name */ - if ((pwd = getpwnam(argv[1]))) { - uid = pwd->pw_uid; - } else { + if (uid_from_user(argv[1], &uid) == -1) { warnx("bad uid `%s'", argv[1]); return 1; } diff --git a/sbin/pfctl/parse.y b/sbin/pfctl/parse.y index 9c1a6293709..0791c9c01d7 100644 --- a/sbin/pfctl/parse.y +++ b/sbin/pfctl/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.683 2018/09/06 15:07:33 kn Exp $ */ +/* $OpenBSD: parse.y,v 1.684 2018/09/16 02:44:06 millert Exp $ */ /* * Copyright (c) 2001 Markus Friedl. All rights reserved. @@ -2965,14 +2965,14 @@ uid : STRING { if (!strcmp($1, "unknown")) $$ = UID_MAX; else { - struct passwd *pw; + uid_t uid; - if ((pw = getpwnam($1)) == NULL) { + if (uid_from_user($1, &uid) == -1) { yyerror("unknown user %s", $1); free($1); YYERROR; } - $$ = pw->pw_uid; + $$ = uid; } free($1); } @@ -3043,14 +3043,14 @@ gid : STRING { if (!strcmp($1, "unknown")) $$ = GID_MAX; else { - struct group *grp; + gid_t gid; - if ((grp = getgrnam($1)) == NULL) { + if (gid_from_group($1, &gid) == -1) { yyerror("unknown group %s", $1); free($1); YYERROR; } - $$ = grp->gr_gid; + $$ = gid; } free($1); } diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index 12cd8e049f5..85a67a155fc 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -1,4 +1,4 @@ -/* $OpenBSD: function.c,v 1.45 2017/01/03 21:31:16 tedu Exp $ */ +/* $OpenBSD: function.c,v 1.46 2018/09/16 02:44:06 millert Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -933,20 +933,17 @@ PLAN * c_group(char *gname, char ***ignored, int unused) { PLAN *new; - struct group *g; gid_t gid; ftsoptions &= ~FTS_NOSTAT; - g = getgrnam(gname); - if (g == NULL) { + if (gid_from_group(gname, &gid) == -1) { const char *errstr; gid = strtonum(gname, 0, GID_MAX, &errstr); if (errstr) errx(1, "-group: %s: no such group", gname); - } else - gid = g->gr_gid; + } new = palloc(N_GROUP, f_group); new->g_data = gid; @@ -1543,20 +1540,17 @@ PLAN * c_user(char *username, char ***ignored, int unused) { PLAN *new; - struct passwd *p; uid_t uid; ftsoptions &= ~FTS_NOSTAT; - p = getpwnam(username); - if (p == NULL) { + if (uid_from_user(username, &uid) == -1) { const char *errstr; uid = strtonum(username, 0, UID_MAX, &errstr); if (errstr) errx(1, "-user: %s: no such user", username); - } else - uid = p->pw_uid; + } new = palloc(N_USER, f_user); new->u_data = uid; diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c index 783fda718d5..3671baa863a 100644 --- a/usr.bin/fstat/fstat.c +++ b/usr.bin/fstat/fstat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fstat.c,v 1.94 2018/09/13 15:23:32 millert Exp $ */ +/* $OpenBSD: fstat.c,v 1.95 2018/09/16 02:44:06 millert Exp $ */ /* * Copyright (c) 2009 Todd C. Miller @@ -159,6 +159,9 @@ main(int argc, char *argv[]) optstr = "fnop:su:vN:M:"; } + /* Keep passwd file open for faster lookups. */ + setpassent(1); + /* * fuser and fstat share three flags: -f, -s and -u. In both cases * -f is a boolean, but for -u fstat wants an argument while fuser @@ -217,15 +220,17 @@ main(int argc, char *argv[]) if (uflg++) usage(); if (!fuser) { - if (!(passwd = getpwnam(optarg))) { - arg = strtonum(optarg, 0, UID_MAX, + uid_t uid; + + if (uid_from_user(optarg, &uid) == -1) { + uid = strtonum(optarg, 0, UID_MAX, &errstr); if (errstr != NULL) { errx(1, "%s: unknown uid", optarg); } - } else - arg = passwd->pw_uid; + } + arg = uid; what = KERN_FILE_BYUID; } break; diff --git a/usr.bin/newsyslog/newsyslog.c b/usr.bin/newsyslog/newsyslog.c index a73b822bb60..7d78408e7a9 100644 --- a/usr.bin/newsyslog/newsyslog.c +++ b/usr.bin/newsyslog/newsyslog.c @@ -1,4 +1,4 @@ -/* $OpenBSD: newsyslog.c,v 1.108 2017/07/24 12:57:01 jca Exp $ */ +/* $OpenBSD: newsyslog.c,v 1.109 2018/09/16 02:44:06 millert Exp $ */ /* * Copyright (c) 1999, 2002, 2003 Todd C. Miller @@ -191,6 +191,10 @@ main(int argc, char **argv) TAILQ_INIT(&config); TAILQ_INIT(&runlist); + /* Keep passwd and group files open for faster lookups. */ + setpassent(1); + setgroupent(1); + ret = parse_file(&config, &listlen); if (argc == 0) TAILQ_CONCAT(&runlist, &config, next); @@ -468,8 +472,6 @@ parse_file(struct entrylist *list, int *nentries) { char line[BUFSIZ], *parse, *q, *errline, *group, *tmp, *ep; struct conf_entry *working; - struct passwd *pwd; - struct group *grp; struct stat sb; int lineno = 0; int ret = 0; @@ -510,36 +512,28 @@ nextline: if ((group = strchr(q, ':')) != NULL || (group = strrchr(q, '.')) != NULL) { *group++ = '\0'; - if (*q) { - if (!(isnumberstr(q))) { - if ((pwd = getpwnam(q)) == NULL) { - warnx("%s:%d: unknown user" - " %s --> skipping", - conf, lineno, q); - ret = 1; - goto nextline; - } - working->uid = pwd->pw_uid; - } else - working->uid = atoi(q); - } else + if (*q == '\0') { working->uid = (uid_t)-1; + } else if (isnumberstr(q)) { + working->uid = atoi(q); + } else if (uid_from_user(q, &working->uid) == -1) { + warnx("%s:%d: unknown user %s --> skipping", + conf, lineno, q); + ret = 1; + goto nextline; + } q = group; - if (*q) { - if (!(isnumberstr(q))) { - if ((grp = getgrnam(q)) == NULL) { - warnx("%s:%d: unknown group" - " %s --> skipping", - conf, lineno, q); - ret = 1; - goto nextline; - } - working->gid = grp->gr_gid; - } else - working->gid = atoi(q); - } else + if (*q == '\0') { working->gid = (gid_t)-1; + } else if (isnumberstr(q)) { + working->gid = atoi(q); + } else if (gid_from_group(q, &working->gid) == -1) { + warnx("%s:%d: unknown group %s --> skipping", + conf, lineno, q); + ret = 1; + goto nextline; + } q = parse = missing_field(sob(++parse), errline, lineno); *(parse = son(parse)) = '\0'; diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index a318e5968fa..4ceab86db19 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pkill.c,v 1.39 2016/10/10 02:22:59 gsoares Exp $ */ +/* $OpenBSD: pkill.c,v 1.40 2018/09/16 02:44:06 millert Exp $ */ /* $NetBSD: pkill.c,v 1.5 2002/10/27 11:49:34 kleink Exp $ */ /*- @@ -544,10 +544,10 @@ static void makelist(struct listhead *head, enum listtype type, char *src) { struct list *li; - struct passwd *pw; - struct group *gr; struct stat st; char *sp, *p, buf[PATH_MAX]; + uid_t uid; + gid_t gid; int empty; empty = 1; @@ -588,14 +588,14 @@ makelist(struct listhead *head, enum listtype type, char *src) switch (type) { case LT_USER: - if ((pw = getpwnam(sp)) == NULL) + if (uid_from_user(sp, &uid) == -1) errx(STATUS_BADUSAGE, "unknown user `%s'", sp); - li->li_number = pw->pw_uid; + li->li_number = uid; break; case LT_GROUP: - if ((gr = getgrnam(sp)) == NULL) + if (gid_from_group(sp, &gid) == -1) errx(STATUS_BADUSAGE, "unknown group `%s'", sp); - li->li_number = gr->gr_gid; + li->li_number = gid; break; case LT_TTY: if (strcmp(sp, "-") == 0) { diff --git a/usr.bin/top/username.c b/usr.bin/top/username.c index 21d8d396efa..c5c402c16f5 100644 --- a/usr.bin/top/username.c +++ b/usr.bin/top/username.c @@ -1,4 +1,4 @@ -/* $OpenBSD: username.c,v 1.18 2018/09/13 15:23:32 millert Exp $ */ +/* $OpenBSD: username.c,v 1.19 2018/09/16 02:44:06 millert Exp $ */ /* * Top users/processes display for Unix @@ -48,12 +48,9 @@ username(uid_t uid) } uid_t -userid(char *username) +userid(const char *username) { - struct passwd *pwd; + uid_t uid; - if ((pwd = getpwnam(username)) == NULL) - return ((uid_t)-1); - - return (pwd->pw_uid); + return uid_from_user(username, &uid); } diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index 9d866825107..c08d82eeed4 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xinstall.c,v 1.66 2017/08/21 21:41:13 deraadt Exp $ */ +/* $OpenBSD: xinstall.c,v 1.67 2018/09/16 02:44:07 millert Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* @@ -60,14 +60,12 @@ #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND) #define BACKUP_SUFFIX ".old" -struct passwd *pp; -struct group *gp; int dobackup, docompare, dodest, dodir, dopreserve, dostrip, safecopy; int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; char pathbuf[PATH_MAX], tempfile[PATH_MAX]; char *suffix = BACKUP_SUFFIX; -uid_t uid; -gid_t gid; +uid_t uid = (uid_t)-1; +gid_t gid = (gid_t)-1; void copy(int, char *, int, char *, off_t, int); int compare(int, const char *, off_t, int, const char *, off_t); @@ -89,6 +87,7 @@ main(int argc, char *argv[]) u_int iflags; int ch, no_target; char *flags, *to_name, *group = NULL, *owner = NULL; + const char *errstr; iflags = 0; while ((ch = getopt(argc, argv, "B:bCcDdFf:g:m:o:pSs")) != -1) @@ -161,12 +160,16 @@ main(int argc, char *argv[]) safecopy = 1; /* get group and owner id's */ - if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group)) - errx(1, "unknown group %s", group); - gid = (group) ? ((gp) ? gp->gr_gid : (gid_t)strtoul(group, NULL, 10)) : (gid_t)-1; - if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner)) - errx(1, "unknown user %s", owner); - uid = (owner) ? ((pp) ? pp->pw_uid : (uid_t)strtoul(owner, NULL, 10)) : (uid_t)-1; + if (group != NULL && gid_from_group(group, &gid) == -1) { + gid = strtonum(group, 0, GID_MAX, &errstr); + if (errstr != NULL) + errx(1, "unknown group %s", group); + } + if (owner != NULL && uid_from_user(owner, &uid) == -1) { + uid = strtonum(owner, 0, UID_MAX, &errstr); + if (errstr != NULL) + errx(1, "unknown user %s", owner); + } if (dodir) { for (; *argv != NULL; ++argv) diff --git a/usr.sbin/edquota/edquota.c b/usr.sbin/edquota/edquota.c index b2429231f1d..402c5511424 100644 --- a/usr.sbin/edquota/edquota.c +++ b/usr.sbin/edquota/edquota.c @@ -191,14 +191,11 @@ main(int argc, char *argv[]) int getentry(char *name, int quotatype, u_int *idp) { - struct passwd *pw; - struct group *gr; u_int id; switch(quotatype) { case USRQUOTA: - if ((pw = getpwnam(name))) { - *idp = pw->pw_uid; + if (uid_from_user(name, idp) != -1) { return 0; } else if (alldigits(name)) { if ((id = strtoul(name, NULL, 10)) <= UID_MAX) { @@ -209,8 +206,7 @@ getentry(char *name, int quotatype, u_int *idp) warnx("%s: no such user", name); break; case GRPQUOTA: - if ((gr = getgrnam(name))) { - *idp = gr->gr_gid; + if (gid_from_group(name, idp) != -1) { return 0; } else if (alldigits(name)) { if ((id = strtoul(name, NULL, 10)) <= GID_MAX) {