xref: /titanic_50/usr/src/cmd/pwck/pwck.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1995-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <locale.h>
43*7c478bd9Sstevel@tonic-gate #include <errno.h>
44*7c478bd9Sstevel@tonic-gate #include <unistd.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	ERROR1	"Too many/few fields"
47*7c478bd9Sstevel@tonic-gate #define	ERROR2	"Bad character(s) in logname"
48*7c478bd9Sstevel@tonic-gate #define	ERROR2a "First char in logname not alphabetic"
49*7c478bd9Sstevel@tonic-gate #define	ERROR2b "Logname field NULL"
50*7c478bd9Sstevel@tonic-gate #define	ERROR2c "Logname contains no lower-case letters"
51*7c478bd9Sstevel@tonic-gate #define	ERROR3	"Logname too long/short"
52*7c478bd9Sstevel@tonic-gate #define	ERROR4	"Invalid UID"
53*7c478bd9Sstevel@tonic-gate #define	ERROR5	"Invalid GID"
54*7c478bd9Sstevel@tonic-gate #define	ERROR6	"Login directory not found"
55*7c478bd9Sstevel@tonic-gate #define	ERROR6a	"Login directory null"
56*7c478bd9Sstevel@tonic-gate #define	ERROR7	"Optional shell file not found"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static int eflag, code = 0;
59*7c478bd9Sstevel@tonic-gate static int badc;
60*7c478bd9Sstevel@tonic-gate static int lc;
61*7c478bd9Sstevel@tonic-gate static char buf[512];
62*7c478bd9Sstevel@tonic-gate static void error(char *);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	int delim[512];
67*7c478bd9Sstevel@tonic-gate 	char logbuf[512];
68*7c478bd9Sstevel@tonic-gate 	FILE *fptr;
69*7c478bd9Sstevel@tonic-gate 	struct stat obuf;
70*7c478bd9Sstevel@tonic-gate 	uid_t uid;
71*7c478bd9Sstevel@tonic-gate 	gid_t gid;
72*7c478bd9Sstevel@tonic-gate 	int i, j, colons;
73*7c478bd9Sstevel@tonic-gate 	char *pw_file;
74*7c478bd9Sstevel@tonic-gate 	struct stat stat_buf;
75*7c478bd9Sstevel@tonic-gate 	char *str, *lastc;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
80*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (argc == 1)
85*7c478bd9Sstevel@tonic-gate 		pw_file = "/etc/passwd";
86*7c478bd9Sstevel@tonic-gate 	else
87*7c478bd9Sstevel@tonic-gate 		pw_file = argv[1];
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if ((fptr = fopen(pw_file, "r")) == NULL) {
90*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot open %s\n"), pw_file);
91*7c478bd9Sstevel@tonic-gate 		exit(1);
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (fstat(fileno(fptr), &stat_buf) < 0) {
95*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("fstat failed for %s\n"),
96*7c478bd9Sstevel@tonic-gate 		    pw_file);
97*7c478bd9Sstevel@tonic-gate 		(void) fclose(fptr);
98*7c478bd9Sstevel@tonic-gate 		exit(1);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (stat_buf.st_size == 0) {
102*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("file %s is empty\n"), pw_file);
103*7c478bd9Sstevel@tonic-gate 		(void) fclose(fptr);
104*7c478bd9Sstevel@tonic-gate 		exit(1);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fptr) != NULL) {
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		colons = 0;
110*7c478bd9Sstevel@tonic-gate 		badc = 0;
111*7c478bd9Sstevel@tonic-gate 		lc = 0;
112*7c478bd9Sstevel@tonic-gate 		eflag = 0;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		/* Check that entry is not a nameservice redirection */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		if (buf[0] == '+' || buf[0] == '-')  {
117*7c478bd9Sstevel@tonic-gate 			/*
118*7c478bd9Sstevel@tonic-gate 			 * Should set flag here to allow special case checking
119*7c478bd9Sstevel@tonic-gate 			 * in the rest of the code,
120*7c478bd9Sstevel@tonic-gate 			 * but for now, we'll just ignore this entry.
121*7c478bd9Sstevel@tonic-gate 			 */
122*7c478bd9Sstevel@tonic-gate 			continue;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 		/* Check number of fields */
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		for (i = 0; buf[i] != NULL; i++)
128*7c478bd9Sstevel@tonic-gate 			if (buf[i] == ':') {
129*7c478bd9Sstevel@tonic-gate 				delim[colons] = i;
130*7c478bd9Sstevel@tonic-gate 				++colons;
131*7c478bd9Sstevel@tonic-gate 			}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		if (colons != 6) {
134*7c478bd9Sstevel@tonic-gate 			error(ERROR1);
135*7c478bd9Sstevel@tonic-gate 			continue;
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 		delim[6] = i - 1;
138*7c478bd9Sstevel@tonic-gate 		delim[7] = NULL;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		/*
141*7c478bd9Sstevel@tonic-gate 		 * Check the first char is alpha; the rest alphanumeric;
142*7c478bd9Sstevel@tonic-gate 		 * and that the name does not consist solely of uppercase
143*7c478bd9Sstevel@tonic-gate 		 * alpha chars
144*7c478bd9Sstevel@tonic-gate 		 */
145*7c478bd9Sstevel@tonic-gate 		if (buf[0] == ':')
146*7c478bd9Sstevel@tonic-gate 			error(ERROR2b);
147*7c478bd9Sstevel@tonic-gate 		else if (!isalpha(buf[0]))
148*7c478bd9Sstevel@tonic-gate 			error(ERROR2a);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		for (i = 0; buf[i] != ':'; i++) {
151*7c478bd9Sstevel@tonic-gate 			if (!isalnum(buf[i]) &&
152*7c478bd9Sstevel@tonic-gate 				buf[i] != '_' &&
153*7c478bd9Sstevel@tonic-gate 				buf[i] != '-' &&
154*7c478bd9Sstevel@tonic-gate 				buf[i] != '.')
155*7c478bd9Sstevel@tonic-gate 				badc++;
156*7c478bd9Sstevel@tonic-gate 			else if (islower(buf[i]))
157*7c478bd9Sstevel@tonic-gate 				lc++;
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 		if (lc == 0)
160*7c478bd9Sstevel@tonic-gate 			error(ERROR2c);
161*7c478bd9Sstevel@tonic-gate 		if (badc > 0)
162*7c478bd9Sstevel@tonic-gate 			error(ERROR2);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 		/* Check for valid number of characters in logname */
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 		if (i <= 0 || i > 8)
167*7c478bd9Sstevel@tonic-gate 			error(ERROR3);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		/* Check that UID is numeric and <= MAXUID */
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		errno = 0;
172*7c478bd9Sstevel@tonic-gate 		str = &buf[delim[1] + 1];
173*7c478bd9Sstevel@tonic-gate 		uid = strtol(str, &lastc, 10);
174*7c478bd9Sstevel@tonic-gate 		if (lastc != str + (delim[2] - delim[1]) - 1 ||
175*7c478bd9Sstevel@tonic-gate 		    uid < 0 || uid > MAXUID || errno == ERANGE)
176*7c478bd9Sstevel@tonic-gate 			error(ERROR4);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 		/* Check that GID is numeric and <= MAXUID */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		errno = 0;
181*7c478bd9Sstevel@tonic-gate 		str = &buf[delim[2] + 1];
182*7c478bd9Sstevel@tonic-gate 		gid = strtol(str, &lastc, 10);
183*7c478bd9Sstevel@tonic-gate 		if (lastc != str + (delim[3] - delim[2]) - 1 ||
184*7c478bd9Sstevel@tonic-gate 		    gid < 0 || gid > MAXUID || errno == ERANGE)
185*7c478bd9Sstevel@tonic-gate 			error(ERROR5);
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		/* Check initial working directory */
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		for (j = 0, i = (delim[4] + 1); i < delim[5]; j++, i++)
190*7c478bd9Sstevel@tonic-gate 			logbuf[j] = buf[i];
191*7c478bd9Sstevel@tonic-gate 		logbuf[j] = '\0';
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		if (logbuf[0] == NULL)
194*7c478bd9Sstevel@tonic-gate 			error(ERROR6a);
195*7c478bd9Sstevel@tonic-gate 		else if ((stat(logbuf, &obuf)) == -1)
196*7c478bd9Sstevel@tonic-gate 			error(ERROR6);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 		/* Check program to use as shell  */
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		if ((buf[(delim[5] + 1)]) != '\n') {
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 			for (j = 0, i = (delim[5] + 1); i < delim[6]; j++, i++)
203*7c478bd9Sstevel@tonic-gate 				logbuf[j] = buf[i];
204*7c478bd9Sstevel@tonic-gate 			logbuf[j] = '\0';
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 			if (strcmp(logbuf, "*") == 0)	/* subsystem login */
207*7c478bd9Sstevel@tonic-gate 				continue;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 			if ((stat(logbuf, &obuf)) == -1)
210*7c478bd9Sstevel@tonic-gate 				error(ERROR7);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < 512; j++)
213*7c478bd9Sstevel@tonic-gate 				logbuf[j] = NULL;
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	(void) fclose(fptr);
217*7c478bd9Sstevel@tonic-gate 	return (code);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /* Error printing routine */
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate static void
223*7c478bd9Sstevel@tonic-gate error(char *msg)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	if (!eflag) {
226*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n%s", buf);
227*7c478bd9Sstevel@tonic-gate 		code = 1;
228*7c478bd9Sstevel@tonic-gate 		++eflag;
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	if (!badc)
231*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s\n", gettext(msg));
232*7c478bd9Sstevel@tonic-gate 	else {
233*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%d %s\n", badc, gettext(msg));
234*7c478bd9Sstevel@tonic-gate 		badc = 0;
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate }
237