386BSD 0.1 development
[unix-history] / usr / src / usr.sbin / pwd_mkdb / pw_scan.c
CommitLineData
a8ba39a3
WJ
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)pw_scan.c 5.1 (Berkeley) 2/12/91";
36#endif /* not lint */
37
38/*
39 * This module is used to "verify" password entries by chpass(1) and
40 * pwd_mkdb(8).
41 */
42
43#include <sys/param.h>
44#include <fcntl.h>
45#include <pwd.h>
46#include <errno.h>
47#include <stdio.h>
48#include <string.h>
49#include <stdlib.h>
50
51extern char *progname;
52
53pw_scan(bp, pw)
54 char *bp;
55 struct passwd *pw;
56{
57 register long id;
58 register int root;
59 register char *p, *sh;
60 char *getusershell();
61
62 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
63 goto fmt;
64 root = !strcmp(pw->pw_name, "root");
65
66 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
67 goto fmt;
68
69 if (!(p = strsep(&bp, ":"))) /* uid */
70 goto fmt;
71 id = atol(p);
72 if (root && id) {
73 (void)fprintf(stderr, "%s: root uid should be 0", progname);
74 return(0);
75 }
76 if (id > USHRT_MAX) {
77 (void)fprintf(stderr,
78 "%s: %s > max uid value (%d)", progname, p, USHRT_MAX);
79 return(0);
80 }
81 pw->pw_uid = id;
82
83 if (!(p = strsep(&bp, ":"))) /* gid */
84 goto fmt;
85 id = atol(p);
86 if (id > USHRT_MAX) {
87 (void)fprintf(stderr,
88 "%s: %s > max gid value (%d)", progname, p, USHRT_MAX);
89 return(0);
90 }
91 pw->pw_gid = id;
92
93 pw->pw_class = strsep(&bp, ":"); /* class */
94 if (!(p = strsep(&bp, ":"))) /* change */
95 goto fmt;
96 pw->pw_change = atol(p);
97 if (!(p = strsep(&bp, ":"))) /* expire */
98 goto fmt;
99 pw->pw_expire = atol(p);
100 pw->pw_gecos = strsep(&bp, ":"); /* gecos */
101 pw->pw_dir = strsep(&bp, ":"); /* directory */
102 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
103 goto fmt;
104
105 p = pw->pw_shell;
106 if (root && *p) /* empty == /bin/sh */
107 for (setusershell();;) {
108 if (!(sh = getusershell())) {
109 (void)fprintf(stderr,
110 "%s: warning, unknown root shell\n",
111 progname);
112 break;
113 }
114 if (!strcmp(p, sh))
115 break;
116 }
117
118 if (p = strsep(&bp, ":")) { /* too many */
119fmt: (void)fprintf(stderr, "%s: corrupted entry\n", progname);
120 return(0);
121 }
122 return(1);
123}