From b5def64de55a2a39374221bb2c618e52880a6339 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 16 Sep 2018 02:44:08 +0000 Subject: Use uid_from_user(3) and gid_from_group(3) in utilities that do repeated lookups. OK tb@ --- usr.bin/find/function.c | 16 +++++-------- usr.bin/fstat/fstat.c | 15 ++++++++----- usr.bin/newsyslog/newsyslog.c | 52 +++++++++++++++++++------------------------ usr.bin/pkill/pkill.c | 14 ++++++------ usr.bin/top/username.c | 11 ++++----- usr.bin/xinstall/xinstall.c | 25 ++++++++++++--------- 6 files changed, 63 insertions(+), 70 deletions(-) (limited to 'usr.bin') 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) -- cgit v1.2.3