-/* $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 $ */
/*
uid_t
a_uid(const char *s, int silent)
{
- struct passwd *pw;
const char *errstr;
uid_t uid;
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);
gid_t
a_gid(const char *s)
{
- struct group *gr;
const char *errstr;
gid_t gid;
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);
-/* $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 $ */
/*-
struct kinfo_proc *kp, **kinfo;
struct varent *vent;
struct winsize ws;
- struct passwd *pwd;
dev_t ttydev;
pid_t pid;
uid_t uid;
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':
-/* $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 $ */
/*-
int rval = 1;
uid_t uid;
char *cp;
- struct passwd *pwd;
if (!checkactive())
return 1;
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;
}
-/* $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.
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);
}
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);
}
-/* $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
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;
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;
-/* $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 <Todd.Miller@courtesan.com>
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
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;
-/* $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 <Todd.Miller@courtesan.com>
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);
{
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;
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';
-/* $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 $ */
/*-
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;
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) {
-/* $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
}
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);
}
-/* $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 $ */
/*
#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);
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)
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)
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) {
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) {