146381Sbostic /*-
2*66650Spendry * Copyright (c) 1990, 1993, 1994
361863Sbostic * The Regents of the University of California. All rights reserved.
446381Sbostic *
546381Sbostic * %sccs.include.redist.c%
646381Sbostic */
746381Sbostic
846381Sbostic #ifndef lint
9*66650Spendry static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 04/02/94";
1046381Sbostic #endif /* not lint */
1146381Sbostic
1246381Sbostic /*
1346381Sbostic * This module is used to "verify" password entries by chpass(1) and
1446381Sbostic * pwd_mkdb(8).
1546381Sbostic */
1646381Sbostic
1746381Sbostic #include <sys/param.h>
1866581Spendry
1966581Spendry #include <err.h>
2046381Sbostic #include <fcntl.h>
2146381Sbostic #include <pwd.h>
2246381Sbostic #include <errno.h>
2346381Sbostic #include <stdio.h>
2446381Sbostic #include <string.h>
2546381Sbostic #include <stdlib.h>
2666581Spendry #include <unistd.h>
2746381Sbostic
2866581Spendry #include "pw_scan.h"
2946381Sbostic
3066581Spendry int
pw_scan(bp,pw)3146381Sbostic pw_scan(bp, pw)
3246381Sbostic char *bp;
3346381Sbostic struct passwd *pw;
3446381Sbostic {
3566581Spendry long id;
3666581Spendry int root;
3766581Spendry char *p, *sh;
3846381Sbostic
3946381Sbostic if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
4046381Sbostic goto fmt;
4146381Sbostic root = !strcmp(pw->pw_name, "root");
4246381Sbostic
4346381Sbostic if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
4446381Sbostic goto fmt;
4546381Sbostic
4646381Sbostic if (!(p = strsep(&bp, ":"))) /* uid */
4746381Sbostic goto fmt;
4846381Sbostic id = atol(p);
4946381Sbostic if (root && id) {
5066581Spendry warnx("root uid should be 0");
5166581Spendry return (0);
5246381Sbostic }
5346381Sbostic if (id > USHRT_MAX) {
5466581Spendry warnx("%s > max uid value (%d)", p, USHRT_MAX);
5566581Spendry return (0);
5646381Sbostic }
5746381Sbostic pw->pw_uid = id;
5846381Sbostic
5946381Sbostic if (!(p = strsep(&bp, ":"))) /* gid */
6046381Sbostic goto fmt;
6146381Sbostic id = atol(p);
6246381Sbostic if (id > USHRT_MAX) {
6366581Spendry warnx("%s > max gid value (%d)", p, USHRT_MAX);
6466581Spendry return (0);
6546381Sbostic }
6646381Sbostic pw->pw_gid = id;
6746381Sbostic
6846381Sbostic pw->pw_class = strsep(&bp, ":"); /* class */
6946381Sbostic if (!(p = strsep(&bp, ":"))) /* change */
7046381Sbostic goto fmt;
7146381Sbostic pw->pw_change = atol(p);
7246381Sbostic if (!(p = strsep(&bp, ":"))) /* expire */
7346381Sbostic goto fmt;
7446381Sbostic pw->pw_expire = atol(p);
7546381Sbostic pw->pw_gecos = strsep(&bp, ":"); /* gecos */
7646381Sbostic pw->pw_dir = strsep(&bp, ":"); /* directory */
7746381Sbostic if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
7846381Sbostic goto fmt;
7946381Sbostic
8046381Sbostic p = pw->pw_shell;
8146381Sbostic if (root && *p) /* empty == /bin/sh */
8246381Sbostic for (setusershell();;) {
8346381Sbostic if (!(sh = getusershell())) {
8466581Spendry warnx("warning, unknown root shell");
8546381Sbostic break;
8646381Sbostic }
8746381Sbostic if (!strcmp(p, sh))
8846381Sbostic break;
8946381Sbostic }
9046381Sbostic
9146381Sbostic if (p = strsep(&bp, ":")) { /* too many */
9266581Spendry fmt: warnx("corrupted entry");
9366581Spendry return (0);
9446381Sbostic }
9566581Spendry return (1);
9646381Sbostic }
97