xref: /titanic_54/usr/src/cmd/pwck/pwck.c (revision f48205be61a214698b763ff550ab9e657525104c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f48205beScasper  * Common Development and Distribution License (the "License").
6*f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/signal.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include <locale.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	ERROR1	"Too many/few fields"
467c478bd9Sstevel@tonic-gate #define	ERROR2	"Bad character(s) in logname"
477c478bd9Sstevel@tonic-gate #define	ERROR2a "First char in logname not alphabetic"
487c478bd9Sstevel@tonic-gate #define	ERROR2b "Logname field NULL"
497c478bd9Sstevel@tonic-gate #define	ERROR2c "Logname contains no lower-case letters"
507c478bd9Sstevel@tonic-gate #define	ERROR3	"Logname too long/short"
517c478bd9Sstevel@tonic-gate #define	ERROR4	"Invalid UID"
527c478bd9Sstevel@tonic-gate #define	ERROR5	"Invalid GID"
537c478bd9Sstevel@tonic-gate #define	ERROR6	"Login directory not found"
547c478bd9Sstevel@tonic-gate #define	ERROR6a	"Login directory null"
557c478bd9Sstevel@tonic-gate #define	ERROR7	"Optional shell file not found"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int eflag, code = 0;
587c478bd9Sstevel@tonic-gate static int badc;
597c478bd9Sstevel@tonic-gate static int lc;
607c478bd9Sstevel@tonic-gate static char buf[512];
617c478bd9Sstevel@tonic-gate static void error(char *);
627c478bd9Sstevel@tonic-gate 
6349335bdeSbasabi int
647c478bd9Sstevel@tonic-gate main(int argc, char **argv)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	int delim[512];
677c478bd9Sstevel@tonic-gate 	char logbuf[512];
687c478bd9Sstevel@tonic-gate 	FILE *fptr;
697c478bd9Sstevel@tonic-gate 	struct stat obuf;
707c478bd9Sstevel@tonic-gate 	uid_t uid;
717c478bd9Sstevel@tonic-gate 	gid_t gid;
727c478bd9Sstevel@tonic-gate 	int i, j, colons;
737c478bd9Sstevel@tonic-gate 	char *pw_file;
747c478bd9Sstevel@tonic-gate 	struct stat stat_buf;
757c478bd9Sstevel@tonic-gate 	char *str, *lastc;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
807c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (argc == 1)
857c478bd9Sstevel@tonic-gate 		pw_file = "/etc/passwd";
867c478bd9Sstevel@tonic-gate 	else
877c478bd9Sstevel@tonic-gate 		pw_file = argv[1];
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if ((fptr = fopen(pw_file, "r")) == NULL) {
907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("cannot open %s\n"), pw_file);
917c478bd9Sstevel@tonic-gate 		exit(1);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (fstat(fileno(fptr), &stat_buf) < 0) {
957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("fstat failed for %s\n"),
967c478bd9Sstevel@tonic-gate 		    pw_file);
977c478bd9Sstevel@tonic-gate 		(void) fclose(fptr);
987c478bd9Sstevel@tonic-gate 		exit(1);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (stat_buf.st_size == 0) {
1027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("file %s is empty\n"), pw_file);
1037c478bd9Sstevel@tonic-gate 		(void) fclose(fptr);
1047c478bd9Sstevel@tonic-gate 		exit(1);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fptr) != NULL) {
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		colons = 0;
1107c478bd9Sstevel@tonic-gate 		badc = 0;
1117c478bd9Sstevel@tonic-gate 		lc = 0;
1127c478bd9Sstevel@tonic-gate 		eflag = 0;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		/* Check that entry is not a nameservice redirection */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if (buf[0] == '+' || buf[0] == '-')  {
1177c478bd9Sstevel@tonic-gate 			/*
1187c478bd9Sstevel@tonic-gate 			 * Should set flag here to allow special case checking
1197c478bd9Sstevel@tonic-gate 			 * in the rest of the code,
1207c478bd9Sstevel@tonic-gate 			 * but for now, we'll just ignore this entry.
1217c478bd9Sstevel@tonic-gate 			 */
1227c478bd9Sstevel@tonic-gate 			continue;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		/* Check number of fields */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		for (i = 0; buf[i] != NULL; i++)
1287c478bd9Sstevel@tonic-gate 			if (buf[i] == ':') {
1297c478bd9Sstevel@tonic-gate 				delim[colons] = i;
1307c478bd9Sstevel@tonic-gate 				++colons;
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		if (colons != 6) {
1347c478bd9Sstevel@tonic-gate 			error(ERROR1);
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		delim[6] = i - 1;
1387c478bd9Sstevel@tonic-gate 		delim[7] = NULL;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		 * Check the first char is alpha; the rest alphanumeric;
1427c478bd9Sstevel@tonic-gate 		 * and that the name does not consist solely of uppercase
1437c478bd9Sstevel@tonic-gate 		 * alpha chars
1447c478bd9Sstevel@tonic-gate 		 */
1457c478bd9Sstevel@tonic-gate 		if (buf[0] == ':')
1467c478bd9Sstevel@tonic-gate 			error(ERROR2b);
1477c478bd9Sstevel@tonic-gate 		else if (!isalpha(buf[0]))
1487c478bd9Sstevel@tonic-gate 			error(ERROR2a);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		for (i = 0; buf[i] != ':'; i++) {
1517c478bd9Sstevel@tonic-gate 			if (!isalnum(buf[i]) &&
1527c478bd9Sstevel@tonic-gate 				buf[i] != '_' &&
1537c478bd9Sstevel@tonic-gate 				buf[i] != '-' &&
1547c478bd9Sstevel@tonic-gate 				buf[i] != '.')
1557c478bd9Sstevel@tonic-gate 				badc++;
1567c478bd9Sstevel@tonic-gate 			else if (islower(buf[i]))
1577c478bd9Sstevel@tonic-gate 				lc++;
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		if (lc == 0)
1607c478bd9Sstevel@tonic-gate 			error(ERROR2c);
1617c478bd9Sstevel@tonic-gate 		if (badc > 0)
1627c478bd9Sstevel@tonic-gate 			error(ERROR2);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/* Check for valid number of characters in logname */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if (i <= 0 || i > 8)
1677c478bd9Sstevel@tonic-gate 			error(ERROR3);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		/* Check that UID is numeric and <= MAXUID */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		errno = 0;
1727c478bd9Sstevel@tonic-gate 		str = &buf[delim[1] + 1];
1737c478bd9Sstevel@tonic-gate 		uid = strtol(str, &lastc, 10);
1747c478bd9Sstevel@tonic-gate 		if (lastc != str + (delim[2] - delim[1]) - 1 ||
175*f48205beScasper 		    uid > MAXUID || errno == ERANGE)
1767c478bd9Sstevel@tonic-gate 			error(ERROR4);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/* Check that GID is numeric and <= MAXUID */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		errno = 0;
1817c478bd9Sstevel@tonic-gate 		str = &buf[delim[2] + 1];
1827c478bd9Sstevel@tonic-gate 		gid = strtol(str, &lastc, 10);
1837c478bd9Sstevel@tonic-gate 		if (lastc != str + (delim[3] - delim[2]) - 1 ||
184*f48205beScasper 		    gid > MAXUID || errno == ERANGE)
1857c478bd9Sstevel@tonic-gate 			error(ERROR5);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		/* Check initial working directory */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		for (j = 0, i = (delim[4] + 1); i < delim[5]; j++, i++)
1907c478bd9Sstevel@tonic-gate 			logbuf[j] = buf[i];
1917c478bd9Sstevel@tonic-gate 		logbuf[j] = '\0';
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		if (logbuf[0] == NULL)
1947c478bd9Sstevel@tonic-gate 			error(ERROR6a);
1957c478bd9Sstevel@tonic-gate 		else if ((stat(logbuf, &obuf)) == -1)
1967c478bd9Sstevel@tonic-gate 			error(ERROR6);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		/* Check program to use as shell  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if ((buf[(delim[5] + 1)]) != '\n') {
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 			for (j = 0, i = (delim[5] + 1); i < delim[6]; j++, i++)
2037c478bd9Sstevel@tonic-gate 				logbuf[j] = buf[i];
2047c478bd9Sstevel@tonic-gate 			logbuf[j] = '\0';
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 			if (strcmp(logbuf, "*") == 0)	/* subsystem login */
2077c478bd9Sstevel@tonic-gate 				continue;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 			if ((stat(logbuf, &obuf)) == -1)
2107c478bd9Sstevel@tonic-gate 				error(ERROR7);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 			for (j = 0; j < 512; j++)
2137c478bd9Sstevel@tonic-gate 				logbuf[j] = NULL;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	(void) fclose(fptr);
2177c478bd9Sstevel@tonic-gate 	return (code);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /* Error printing routine */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static void
2237c478bd9Sstevel@tonic-gate error(char *msg)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	if (!eflag) {
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n%s", buf);
2277c478bd9Sstevel@tonic-gate 		code = 1;
2287c478bd9Sstevel@tonic-gate 		++eflag;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	if (!badc)
2317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s\n", gettext(msg));
2327c478bd9Sstevel@tonic-gate 	else {
2337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%d %s\n", badc, gettext(msg));
2347c478bd9Sstevel@tonic-gate 		badc = 0;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate }
237