1dbf37598Sbostic /*-
2*89782b75Spendry * Copyright (c) 1990, 1993, 1994
3ec699835Sbostic * The Regents of the University of California. All rights reserved.
4dbf37598Sbostic *
5dbf37598Sbostic * %sccs.include.redist.c%
6dbf37598Sbostic */
7dbf37598Sbostic
8dbf37598Sbostic #ifndef lint
9*89782b75Spendry static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 04/02/94";
10dbf37598Sbostic #endif /* not lint */
11dbf37598Sbostic
12dbf37598Sbostic /*
13dbf37598Sbostic * This module is used to "verify" password entries by chpass(1) and
14dbf37598Sbostic * pwd_mkdb(8).
15dbf37598Sbostic */
16dbf37598Sbostic
17dbf37598Sbostic #include <sys/param.h>
1845269520Spendry
1945269520Spendry #include <err.h>
20dbf37598Sbostic #include <fcntl.h>
21dbf37598Sbostic #include <pwd.h>
22dbf37598Sbostic #include <errno.h>
23dbf37598Sbostic #include <stdio.h>
24dbf37598Sbostic #include <string.h>
25dbf37598Sbostic #include <stdlib.h>
2645269520Spendry #include <unistd.h>
27dbf37598Sbostic
2845269520Spendry #include "pw_scan.h"
29dbf37598Sbostic
3045269520Spendry int
pw_scan(bp,pw)31dbf37598Sbostic pw_scan(bp, pw)
32dbf37598Sbostic char *bp;
33dbf37598Sbostic struct passwd *pw;
34dbf37598Sbostic {
3545269520Spendry long id;
3645269520Spendry int root;
3745269520Spendry char *p, *sh;
38dbf37598Sbostic
39dbf37598Sbostic if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
40dbf37598Sbostic goto fmt;
41dbf37598Sbostic root = !strcmp(pw->pw_name, "root");
42dbf37598Sbostic
43dbf37598Sbostic if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
44dbf37598Sbostic goto fmt;
45dbf37598Sbostic
46dbf37598Sbostic if (!(p = strsep(&bp, ":"))) /* uid */
47dbf37598Sbostic goto fmt;
48dbf37598Sbostic id = atol(p);
49dbf37598Sbostic if (root && id) {
5045269520Spendry warnx("root uid should be 0");
51dbf37598Sbostic return (0);
52dbf37598Sbostic }
53dbf37598Sbostic if (id > USHRT_MAX) {
5445269520Spendry warnx("%s > max uid value (%d)", p, USHRT_MAX);
55dbf37598Sbostic return (0);
56dbf37598Sbostic }
57dbf37598Sbostic pw->pw_uid = id;
58dbf37598Sbostic
59dbf37598Sbostic if (!(p = strsep(&bp, ":"))) /* gid */
60dbf37598Sbostic goto fmt;
61dbf37598Sbostic id = atol(p);
62dbf37598Sbostic if (id > USHRT_MAX) {
6345269520Spendry warnx("%s > max gid value (%d)", p, USHRT_MAX);
64dbf37598Sbostic return (0);
65dbf37598Sbostic }
66dbf37598Sbostic pw->pw_gid = id;
67dbf37598Sbostic
68dbf37598Sbostic pw->pw_class = strsep(&bp, ":"); /* class */
69dbf37598Sbostic if (!(p = strsep(&bp, ":"))) /* change */
70dbf37598Sbostic goto fmt;
71dbf37598Sbostic pw->pw_change = atol(p);
72dbf37598Sbostic if (!(p = strsep(&bp, ":"))) /* expire */
73dbf37598Sbostic goto fmt;
74dbf37598Sbostic pw->pw_expire = atol(p);
75dbf37598Sbostic pw->pw_gecos = strsep(&bp, ":"); /* gecos */
76dbf37598Sbostic pw->pw_dir = strsep(&bp, ":"); /* directory */
77dbf37598Sbostic if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
78dbf37598Sbostic goto fmt;
79dbf37598Sbostic
80dbf37598Sbostic p = pw->pw_shell;
81dbf37598Sbostic if (root && *p) /* empty == /bin/sh */
82dbf37598Sbostic for (setusershell();;) {
83dbf37598Sbostic if (!(sh = getusershell())) {
8445269520Spendry warnx("warning, unknown root shell");
85dbf37598Sbostic break;
86dbf37598Sbostic }
87dbf37598Sbostic if (!strcmp(p, sh))
88dbf37598Sbostic break;
89dbf37598Sbostic }
90dbf37598Sbostic
91dbf37598Sbostic if (p = strsep(&bp, ":")) { /* too many */
9245269520Spendry fmt: warnx("corrupted entry");
93dbf37598Sbostic return (0);
94dbf37598Sbostic }
95dbf37598Sbostic return (1);
96dbf37598Sbostic }
97