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
5f48205beScasper * Common Development and Distribution License (the "License").
6f48205beScasper * 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*0a1278f2SGary Mills * Copyright (c) 2013 Gary Mills
23*0a1278f2SGary Mills *
24f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
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>
44*0a1278f2SGary Mills #include <limits.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define ERROR1 "Too many/few fields"
477c478bd9Sstevel@tonic-gate #define ERROR2 "Bad character(s) in logname"
487c478bd9Sstevel@tonic-gate #define ERROR2a "First char in logname not alphabetic"
497c478bd9Sstevel@tonic-gate #define ERROR2b "Logname field NULL"
507c478bd9Sstevel@tonic-gate #define ERROR2c "Logname contains no lower-case letters"
517c478bd9Sstevel@tonic-gate #define ERROR3 "Logname too long/short"
527c478bd9Sstevel@tonic-gate #define ERROR4 "Invalid UID"
537c478bd9Sstevel@tonic-gate #define ERROR5 "Invalid GID"
547c478bd9Sstevel@tonic-gate #define ERROR6 "Login directory not found"
557c478bd9Sstevel@tonic-gate #define ERROR6a "Login directory null"
567c478bd9Sstevel@tonic-gate #define ERROR7 "Optional shell file not found"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static int eflag, code = 0;
597c478bd9Sstevel@tonic-gate static int badc;
607c478bd9Sstevel@tonic-gate static int lc;
617c478bd9Sstevel@tonic-gate static char buf[512];
627c478bd9Sstevel@tonic-gate static void error(char *);
637c478bd9Sstevel@tonic-gate
6449335bdeSbasabi int
main(int argc,char ** argv)657c478bd9Sstevel@tonic-gate main(int argc, char **argv)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate int delim[512];
687c478bd9Sstevel@tonic-gate char logbuf[512];
697c478bd9Sstevel@tonic-gate FILE *fptr;
707c478bd9Sstevel@tonic-gate struct stat obuf;
717c478bd9Sstevel@tonic-gate uid_t uid;
727c478bd9Sstevel@tonic-gate gid_t gid;
737c478bd9Sstevel@tonic-gate int i, j, colons;
747c478bd9Sstevel@tonic-gate char *pw_file;
757c478bd9Sstevel@tonic-gate struct stat stat_buf;
767c478bd9Sstevel@tonic-gate char *str, *lastc;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
817c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if (argc == 1)
867c478bd9Sstevel@tonic-gate pw_file = "/etc/passwd";
877c478bd9Sstevel@tonic-gate else
887c478bd9Sstevel@tonic-gate pw_file = argv[1];
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if ((fptr = fopen(pw_file, "r")) == NULL) {
917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cannot open %s\n"), pw_file);
927c478bd9Sstevel@tonic-gate exit(1);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate if (fstat(fileno(fptr), &stat_buf) < 0) {
967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("fstat failed for %s\n"),
977c478bd9Sstevel@tonic-gate pw_file);
987c478bd9Sstevel@tonic-gate (void) fclose(fptr);
997c478bd9Sstevel@tonic-gate exit(1);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate if (stat_buf.st_size == 0) {
1037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("file %s is empty\n"), pw_file);
1047c478bd9Sstevel@tonic-gate (void) fclose(fptr);
1057c478bd9Sstevel@tonic-gate exit(1);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fptr) != NULL) {
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate colons = 0;
1117c478bd9Sstevel@tonic-gate badc = 0;
1127c478bd9Sstevel@tonic-gate lc = 0;
1137c478bd9Sstevel@tonic-gate eflag = 0;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate /* Check that entry is not a nameservice redirection */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate if (buf[0] == '+' || buf[0] == '-') {
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate * Should set flag here to allow special case checking
1207c478bd9Sstevel@tonic-gate * in the rest of the code,
1217c478bd9Sstevel@tonic-gate * but for now, we'll just ignore this entry.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate continue;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /* Check number of fields */
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate for (i = 0; buf[i] != NULL; i++)
1297c478bd9Sstevel@tonic-gate if (buf[i] == ':') {
1307c478bd9Sstevel@tonic-gate delim[colons] = i;
1317c478bd9Sstevel@tonic-gate ++colons;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if (colons != 6) {
1357c478bd9Sstevel@tonic-gate error(ERROR1);
1367c478bd9Sstevel@tonic-gate continue;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate delim[6] = i - 1;
1397c478bd9Sstevel@tonic-gate delim[7] = NULL;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * Check the first char is alpha; the rest alphanumeric;
1437c478bd9Sstevel@tonic-gate * and that the name does not consist solely of uppercase
1447c478bd9Sstevel@tonic-gate * alpha chars
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate if (buf[0] == ':')
1477c478bd9Sstevel@tonic-gate error(ERROR2b);
1487c478bd9Sstevel@tonic-gate else if (!isalpha(buf[0]))
1497c478bd9Sstevel@tonic-gate error(ERROR2a);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate for (i = 0; buf[i] != ':'; i++) {
1527c478bd9Sstevel@tonic-gate if (!isalnum(buf[i]) &&
1537c478bd9Sstevel@tonic-gate buf[i] != '_' &&
1547c478bd9Sstevel@tonic-gate buf[i] != '-' &&
1557c478bd9Sstevel@tonic-gate buf[i] != '.')
1567c478bd9Sstevel@tonic-gate badc++;
1577c478bd9Sstevel@tonic-gate else if (islower(buf[i]))
1587c478bd9Sstevel@tonic-gate lc++;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate if (lc == 0)
1617c478bd9Sstevel@tonic-gate error(ERROR2c);
1627c478bd9Sstevel@tonic-gate if (badc > 0)
1637c478bd9Sstevel@tonic-gate error(ERROR2);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /* Check for valid number of characters in logname */
1667c478bd9Sstevel@tonic-gate
167*0a1278f2SGary Mills if (i <= 0 || i > LOGNAME_MAX)
1687c478bd9Sstevel@tonic-gate error(ERROR3);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate /* Check that UID is numeric and <= MAXUID */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate errno = 0;
1737c478bd9Sstevel@tonic-gate str = &buf[delim[1] + 1];
1747c478bd9Sstevel@tonic-gate uid = strtol(str, &lastc, 10);
1757c478bd9Sstevel@tonic-gate if (lastc != str + (delim[2] - delim[1]) - 1 ||
176f48205beScasper uid > MAXUID || errno == ERANGE)
1777c478bd9Sstevel@tonic-gate error(ERROR4);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /* Check that GID is numeric and <= MAXUID */
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate errno = 0;
1827c478bd9Sstevel@tonic-gate str = &buf[delim[2] + 1];
1837c478bd9Sstevel@tonic-gate gid = strtol(str, &lastc, 10);
1847c478bd9Sstevel@tonic-gate if (lastc != str + (delim[3] - delim[2]) - 1 ||
185f48205beScasper gid > MAXUID || errno == ERANGE)
1867c478bd9Sstevel@tonic-gate error(ERROR5);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /* Check initial working directory */
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate for (j = 0, i = (delim[4] + 1); i < delim[5]; j++, i++)
1917c478bd9Sstevel@tonic-gate logbuf[j] = buf[i];
1927c478bd9Sstevel@tonic-gate logbuf[j] = '\0';
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (logbuf[0] == NULL)
1957c478bd9Sstevel@tonic-gate error(ERROR6a);
1967c478bd9Sstevel@tonic-gate else if ((stat(logbuf, &obuf)) == -1)
1977c478bd9Sstevel@tonic-gate error(ERROR6);
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate /* Check program to use as shell */
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if ((buf[(delim[5] + 1)]) != '\n') {
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate for (j = 0, i = (delim[5] + 1); i < delim[6]; j++, i++)
2047c478bd9Sstevel@tonic-gate logbuf[j] = buf[i];
2057c478bd9Sstevel@tonic-gate logbuf[j] = '\0';
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate if (strcmp(logbuf, "*") == 0) /* subsystem login */
2087c478bd9Sstevel@tonic-gate continue;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if ((stat(logbuf, &obuf)) == -1)
2117c478bd9Sstevel@tonic-gate error(ERROR7);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate for (j = 0; j < 512; j++)
2147c478bd9Sstevel@tonic-gate logbuf[j] = NULL;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate (void) fclose(fptr);
2187c478bd9Sstevel@tonic-gate return (code);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* Error printing routine */
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate static void
error(char * msg)2247c478bd9Sstevel@tonic-gate error(char *msg)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate if (!eflag) {
2277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n%s", buf);
2287c478bd9Sstevel@tonic-gate code = 1;
2297c478bd9Sstevel@tonic-gate ++eflag;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate if (!badc)
2327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%s\n", gettext(msg));
2337c478bd9Sstevel@tonic-gate else {
2347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t%d %s\n", badc, gettext(msg));
2357c478bd9Sstevel@tonic-gate badc = 0;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate }
238