xref: /titanic_50/usr/src/cmd/ttymon/tmexpress.c (revision aecfc01d1bad84e66649703f7fc2926ef70b34ba)
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
5fea9cb91Slq150181  * Common Development and Distribution License (the "License").
6fea9cb91Slq150181  * 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  */
21fea9cb91Slq150181 
227c478bd9Sstevel@tonic-gate /*
23*aecfc01dSrui zang - Sun Microsystems - Beijing China  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 #include	<stdio.h>
307c478bd9Sstevel@tonic-gate #include	<stdlib.h>
317c478bd9Sstevel@tonic-gate #include	<unistd.h>
327c478bd9Sstevel@tonic-gate #include	<fcntl.h>
337c478bd9Sstevel@tonic-gate #include	<errno.h>
347c478bd9Sstevel@tonic-gate #include	<ctype.h>
357c478bd9Sstevel@tonic-gate #include	<string.h>
367c478bd9Sstevel@tonic-gate #include	<signal.h>
377c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
387c478bd9Sstevel@tonic-gate #include	<utmpx.h>
397c478bd9Sstevel@tonic-gate #include	<pwd.h>
407c478bd9Sstevel@tonic-gate #include	<dirent.h>
417c478bd9Sstevel@tonic-gate #include	<sys/param.h>
427c478bd9Sstevel@tonic-gate #include	<sys/acl.h>
43fea9cb91Slq150181 #include	<sys/stat.h>
44fea9cb91Slq150181 #include	<sys/types.h>
45fea9cb91Slq150181 #include	<sys/console.h>
467c478bd9Sstevel@tonic-gate #include	"ttymon.h"
477c478bd9Sstevel@tonic-gate #include	"tmextern.h"
487c478bd9Sstevel@tonic-gate #include	"tmstruct.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static	char	devbuf[BUFSIZ];
517c478bd9Sstevel@tonic-gate static	char	*devname;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static	int	parse_args();
547c478bd9Sstevel@tonic-gate static	void	ttymon_options();
557c478bd9Sstevel@tonic-gate static	void	getty_options();
567c478bd9Sstevel@tonic-gate static	void	usage();
577c478bd9Sstevel@tonic-gate static	char	*find_ttyname();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate extern	void	tmchild();
607c478bd9Sstevel@tonic-gate extern	int	vml();
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void		revokedevaccess(char *, uid_t, gid_t, mode_t);
637c478bd9Sstevel@tonic-gate /* cannot include libdevinfo.h */
647c478bd9Sstevel@tonic-gate extern int di_devperm_logout(const char *);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * ttymon_express - This is call when ttymon is invoked with args
687c478bd9Sstevel@tonic-gate  *		    or invoked as getty
697c478bd9Sstevel@tonic-gate  *		  - This special version of ttymon will monitor
707c478bd9Sstevel@tonic-gate  *		    one port only
717c478bd9Sstevel@tonic-gate  *		  - It is intended to be used when some process
727c478bd9Sstevel@tonic-gate  *		    wants to have a login session on the fly
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate void
ttymon_express(int argc,char ** argv)757c478bd9Sstevel@tonic-gate ttymon_express(int argc, char **argv)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	struct	pmtab	*pmtab;
787c478bd9Sstevel@tonic-gate 	struct	sigaction	sigact;
797c478bd9Sstevel@tonic-gate 	extern	int	Retry;
807c478bd9Sstevel@tonic-gate 	extern	void	open_device();
817c478bd9Sstevel@tonic-gate 	extern	void	read_ttydefs();
827c478bd9Sstevel@tonic-gate 	extern	int	checkut_line();
837c478bd9Sstevel@tonic-gate #ifdef	DEBUG
847c478bd9Sstevel@tonic-gate 	extern	FILE	*Debugfp;
857c478bd9Sstevel@tonic-gate 	extern	void	opendebug();
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
897c478bd9Sstevel@tonic-gate 	opendebug(TRUE);
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	sigact.sa_flags = 0;
937c478bd9Sstevel@tonic-gate 	sigact.sa_handler = SIG_IGN;
947c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&sigact.sa_mask);
957c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sigact, NULL);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if ((pmtab = ALLOC_PMTAB) == PNULL) {
987c478bd9Sstevel@tonic-gate 		log("ttymon_express: ALLOC_PMTAB failed");
997c478bd9Sstevel@tonic-gate 		exit(1);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (parse_args(argc, argv, pmtab) != 0) {
1037c478bd9Sstevel@tonic-gate 		log("ttymon_express: parse_args failed");
1047c478bd9Sstevel@tonic-gate 		exit(1);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	read_ttydefs(NULL, FALSE);
1087c478bd9Sstevel@tonic-gate 
109*aecfc01dSrui zang - Sun Microsystems - Beijing China 	if ((pmtab->p_device != NULL) && (*(pmtab->p_device) != '\0'))
1107c478bd9Sstevel@tonic-gate 		while (checkut_line(pmtab->p_device))
1117c478bd9Sstevel@tonic-gate 			sleep(15);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((pmtab->p_device == NULL) || (*(pmtab->p_device) == '\0')) {
1147c478bd9Sstevel@tonic-gate 		devname = find_ttyname(0);
1157c478bd9Sstevel@tonic-gate 		if ((devname == NULL) || (*devname == '\0')) {
1167c478bd9Sstevel@tonic-gate 			log("ttyname cannot find the device on fd 0");
1177c478bd9Sstevel@tonic-gate 			exit(1);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		pmtab->p_device = devname;
1207c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1217c478bd9Sstevel@tonic-gate 		debug("ttymon_express: devname = %s", devname);
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate 		/*
1247c478bd9Sstevel@tonic-gate 		 * become session leader
1257c478bd9Sstevel@tonic-gate 		 * fd 0 is closed and reopened just to make sure
1267c478bd9Sstevel@tonic-gate 		 * controlling tty is set up right
1277c478bd9Sstevel@tonic-gate 		 */
1287c478bd9Sstevel@tonic-gate 		(void) setsid();
1297c478bd9Sstevel@tonic-gate 		(void) close(0);
1307c478bd9Sstevel@tonic-gate 		revokedevaccess(pmtab->p_device, 0, 0, 0);
1317c478bd9Sstevel@tonic-gate 		if (open(pmtab->p_device, O_RDWR) < 0) {
1327c478bd9Sstevel@tonic-gate 			log("open %s failed: %s", pmtab->p_device,
1337c478bd9Sstevel@tonic-gate 			    strerror(errno));
1347c478bd9Sstevel@tonic-gate 			exit(1);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		if ((pmtab->p_modules != NULL) &&
1377c478bd9Sstevel@tonic-gate 		    (*(pmtab->p_modules) != '\0')) {
1387c478bd9Sstevel@tonic-gate 			if (push_linedisc(0, pmtab->p_modules,
1397c478bd9Sstevel@tonic-gate 			    pmtab->p_device) == -1)
1407c478bd9Sstevel@tonic-gate 				exit(1);
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		if (initial_termio(0, pmtab) == -1)
1437c478bd9Sstevel@tonic-gate 			exit(1);
1447c478bd9Sstevel@tonic-gate 		di_devperm_logout((const char *)pmtab->p_device);
1457c478bd9Sstevel@tonic-gate 	} else {
1467c478bd9Sstevel@tonic-gate 		(void) setsid();
1477c478bd9Sstevel@tonic-gate 		(void) close(0);
1487c478bd9Sstevel@tonic-gate 		Retry = FALSE;
1497c478bd9Sstevel@tonic-gate 		open_device(pmtab);
1507c478bd9Sstevel@tonic-gate 		if (Retry)		/* open failed */
1517c478bd9Sstevel@tonic-gate 			exit(1);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	tmchild(pmtab);
1547c478bd9Sstevel@tonic-gate 	exit(1);	/*NOTREACHED*/
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * parse_arg	- parse cmd line arguments
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static	int
parse_args(int argc,char ** argv,struct pmtab * pmtab)1617c478bd9Sstevel@tonic-gate parse_args(int argc, char **argv, struct pmtab *pmtab)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	static	char	p_server[] = "/usr/bin/login";
1647c478bd9Sstevel@tonic-gate 	extern	char	*lastname();
1657c478bd9Sstevel@tonic-gate 	extern	void	getty_account();
166fea9cb91Slq150181 	static	char	termbuf[MAX_TERM_TYPE_LEN];
167fea9cb91Slq150181 	static	struct	cons_getterm cnterm = {sizeof (termbuf), termbuf};
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* initialize fields to some default first */
1707c478bd9Sstevel@tonic-gate 	pmtab->p_tag = "";
1717c478bd9Sstevel@tonic-gate 	pmtab->p_flags = 0;
1727c478bd9Sstevel@tonic-gate 	pmtab->p_identity = "root";
1737c478bd9Sstevel@tonic-gate 	pmtab->p_res1 = "reserved";
1747c478bd9Sstevel@tonic-gate 	pmtab->p_res2 = "reserved";
1757c478bd9Sstevel@tonic-gate 	pmtab->p_res3 = "reserved";
1767c478bd9Sstevel@tonic-gate 	pmtab->p_uid = 0;
1777c478bd9Sstevel@tonic-gate 	pmtab->p_gid = 0;
1787c478bd9Sstevel@tonic-gate 	pmtab->p_dir = "/";
1797c478bd9Sstevel@tonic-gate 	pmtab->p_ttyflags = 0;
1807c478bd9Sstevel@tonic-gate 	pmtab->p_count = 0;
1817c478bd9Sstevel@tonic-gate 	pmtab->p_server = p_server;
1827c478bd9Sstevel@tonic-gate 	pmtab->p_timeout = 0;
1837c478bd9Sstevel@tonic-gate 	pmtab->p_modules = "";
1847c478bd9Sstevel@tonic-gate 	pmtab->p_prompt = "login: ";
1857c478bd9Sstevel@tonic-gate 	pmtab->p_dmsg = "";
1867c478bd9Sstevel@tonic-gate 	pmtab->p_termtype = "";
1877c478bd9Sstevel@tonic-gate 	pmtab->p_device = "";
1887c478bd9Sstevel@tonic-gate 	pmtab->p_status = GETTY;
1897c478bd9Sstevel@tonic-gate 	if (strcmp(lastname(argv[0]), "getty") == 0) {
1907c478bd9Sstevel@tonic-gate 		pmtab->p_ttylabel = "300";
1917c478bd9Sstevel@tonic-gate 		getty_options(argc, argv, pmtab);
1927c478bd9Sstevel@tonic-gate 	} else {
193fea9cb91Slq150181 		int	cn_fd;
194fea9cb91Slq150181 
1957c478bd9Sstevel@tonic-gate 		pmtab->p_ttylabel = "9600";
1967c478bd9Sstevel@tonic-gate 		ttymon_options(argc, argv, pmtab);
197fea9cb91Slq150181 
198fea9cb91Slq150181 		/*
199fea9cb91Slq150181 		 * The following code is only reached if -g was specified.
200fea9cb91Slq150181 		 * It attempts to determine a suitable terminal type for
201fea9cb91Slq150181 		 * the console login process.
202fea9cb91Slq150181 		 *
203fea9cb91Slq150181 		 * If -d /dev/console also specified, we send an ioctl
204fea9cb91Slq150181 		 * to the console device to query the TERM type.
205fea9cb91Slq150181 		 *
206fea9cb91Slq150181 		 * If any of the tests, system calls, or ioctls fail
207fea9cb91Slq150181 		 * then pmtab->p_termtype retains its default value
208fea9cb91Slq150181 		 * of "".  otherwise it is set to a term type value
209fea9cb91Slq150181 		 * that was returned.
210fea9cb91Slq150181 		 */
211fea9cb91Slq150181 		if ((strlen(pmtab->p_termtype) == 0) &&
212fea9cb91Slq150181 		    (strcmp(pmtab->p_device, "/dev/console") == 0) &&
213fea9cb91Slq150181 		    ((cn_fd = open("/dev/console", O_RDONLY)) != -1)) {
214fea9cb91Slq150181 
215fea9cb91Slq150181 			if (ioctl(cn_fd, CONS_GETTERM, &cnterm) != -1)
216fea9cb91Slq150181 				pmtab->p_termtype = cnterm.cn_term_type;
217fea9cb91Slq150181 			(void) close(cn_fd);
2187c478bd9Sstevel@tonic-gate 		}
219fea9cb91Slq150181 	}
220fea9cb91Slq150181 
2217c478bd9Sstevel@tonic-gate 	if ((pmtab->p_device != NULL) && (*(pmtab->p_device) != '\0'))
2227c478bd9Sstevel@tonic-gate 		getty_account(pmtab->p_device); /* utmp accounting */
2237c478bd9Sstevel@tonic-gate 	return (0);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * 	ttymon_options - scan and check args for ttymon express
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static	void
ttymon_options(int argc,char ** argv,struct pmtab * pmtab)2327c478bd9Sstevel@tonic-gate ttymon_options(int argc, char **argv, struct pmtab *pmtab)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	int 	c;			/* option letter */
2357c478bd9Sstevel@tonic-gate 	char 	*timeout;
2367c478bd9Sstevel@tonic-gate 	int  	gflag = 0;		/* -g seen */
2377c478bd9Sstevel@tonic-gate 	int	size = 0;
2387c478bd9Sstevel@tonic-gate 	char	tbuf[BUFSIZ];
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	extern	char	*optarg;
2417c478bd9Sstevel@tonic-gate 	extern	int	optind;
2427c478bd9Sstevel@tonic-gate 	extern	void	copystr();
2437c478bd9Sstevel@tonic-gate 	extern	char	*strsave();
2447c478bd9Sstevel@tonic-gate 	extern	char	*getword();
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "T:gd:ht:p:m:l:")) != -1) {
2477c478bd9Sstevel@tonic-gate 		switch (c) {
2487c478bd9Sstevel@tonic-gate 		case 'g':
2497c478bd9Sstevel@tonic-gate 			gflag = 1;
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 		case 'd':
2527c478bd9Sstevel@tonic-gate 			pmtab->p_device = optarg;
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		case 'h':
2557c478bd9Sstevel@tonic-gate 			pmtab->p_ttyflags &= ~H_FLAG;
2567c478bd9Sstevel@tonic-gate 			break;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		case 'T':
2597c478bd9Sstevel@tonic-gate 			pmtab->p_termtype = optarg;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  *		case 'b':
2637c478bd9Sstevel@tonic-gate  *			pmtab->p_ttyflags |= B_FLAG;
2647c478bd9Sstevel@tonic-gate  *			pmtab->p_ttyflags |= R_FLAG;
2657c478bd9Sstevel@tonic-gate  *			break;
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate 		case 't':
2687c478bd9Sstevel@tonic-gate 			timeout = optarg;
2697c478bd9Sstevel@tonic-gate 			while (*optarg) {
2707c478bd9Sstevel@tonic-gate 				if (!isdigit(*optarg++)) {
2717c478bd9Sstevel@tonic-gate 					log("Invalid argument for "
2727c478bd9Sstevel@tonic-gate 					    "\"-t\" -- number expected.");
2737c478bd9Sstevel@tonic-gate 					usage();
2747c478bd9Sstevel@tonic-gate 				}
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 			pmtab->p_timeout = atoi(timeout);
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 		case 'p':
2797c478bd9Sstevel@tonic-gate 			copystr(tbuf, optarg);
2807c478bd9Sstevel@tonic-gate 			pmtab->p_prompt = strsave(getword(tbuf, &size, TRUE));
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		case 'm':
2837c478bd9Sstevel@tonic-gate 			pmtab->p_modules = optarg;
2847c478bd9Sstevel@tonic-gate 			if (vml(pmtab->p_modules) != 0)
2857c478bd9Sstevel@tonic-gate 				usage();
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case 'l':
2887c478bd9Sstevel@tonic-gate 			pmtab->p_ttylabel = optarg;
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		case '?':
2917c478bd9Sstevel@tonic-gate 			usage();
2927c478bd9Sstevel@tonic-gate 			break;	/*NOTREACHED*/
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	if (optind < argc)
2967c478bd9Sstevel@tonic-gate 		usage();
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (!gflag)
2997c478bd9Sstevel@tonic-gate 		usage();
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * usage - print out a usage message
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static 	void
usage()3077c478bd9Sstevel@tonic-gate usage()
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	char	*umsg = "Usage: ttymon\n  ttymon -g [-h] [-d device] "
3107c478bd9Sstevel@tonic-gate 	    "[-l ttylabel] [-t timeout] [-p prompt] [-m modules]\n";
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (isatty(STDERR_FILENO))
3137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", umsg);
3147c478bd9Sstevel@tonic-gate 	else
3157c478bd9Sstevel@tonic-gate 		cons_printf(umsg);
3167c478bd9Sstevel@tonic-gate 	exit(1);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  *	getty_options	- this is cut from getty.c
3217c478bd9Sstevel@tonic-gate  *			- it scan getty cmd args
3227c478bd9Sstevel@tonic-gate  *			- modification is made to stuff args in pmtab
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static	void
getty_options(argc,argv,pmtab)3257c478bd9Sstevel@tonic-gate getty_options(argc, argv, pmtab)
3267c478bd9Sstevel@tonic-gate int argc;
3277c478bd9Sstevel@tonic-gate char **argv;
3287c478bd9Sstevel@tonic-gate struct	pmtab	*pmtab;
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	char	*ptr;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * the pre-4.0 getty's hang_up_line() is a no-op.
3347c478bd9Sstevel@tonic-gate 	 * For compatibility, H_FLAG cannot be set for this "getty".
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	pmtab->p_ttyflags &= ~(H_FLAG);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	while (--argc && **++argv == '-') {
3397c478bd9Sstevel@tonic-gate 		for (ptr = *argv + 1; *ptr; ptr++)
3407c478bd9Sstevel@tonic-gate 		switch (*ptr) {
3417c478bd9Sstevel@tonic-gate 		case 'h':
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 		case 't':
3447c478bd9Sstevel@tonic-gate 			if (isdigit(*++ptr)) {
3457c478bd9Sstevel@tonic-gate 				(void) sscanf(ptr, "%d", &(pmtab->p_timeout));
3467c478bd9Sstevel@tonic-gate 				while (isdigit(*++ptr));
3477c478bd9Sstevel@tonic-gate 				ptr--;
3487c478bd9Sstevel@tonic-gate 			} else if (--argc) {
3497c478bd9Sstevel@tonic-gate 				if (isdigit(*(ptr = *++argv)))
3507c478bd9Sstevel@tonic-gate 					(void) sscanf(ptr, "%d",
3517c478bd9Sstevel@tonic-gate 					    &(pmtab->p_timeout));
3527c478bd9Sstevel@tonic-gate 				else {
3537c478bd9Sstevel@tonic-gate 					log("getty: timeout argument <%s> "
3547c478bd9Sstevel@tonic-gate 					    "invalid", *argv);
3557c478bd9Sstevel@tonic-gate 					exit(1);
3567c478bd9Sstevel@tonic-gate 				}
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		case 'c':
3617c478bd9Sstevel@tonic-gate 			log("Use \"sttydefs -l\" to check /etc/ttydefs.");
3627c478bd9Sstevel@tonic-gate 			exit(0);
3637c478bd9Sstevel@tonic-gate 		default:
3647c478bd9Sstevel@tonic-gate 			break;
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (argc < 1) {
3697c478bd9Sstevel@tonic-gate 		log("getty: no terminal line specified.");
3707c478bd9Sstevel@tonic-gate 		exit(1);
3717c478bd9Sstevel@tonic-gate 	} else {
3727c478bd9Sstevel@tonic-gate 		(void) strcat(devbuf, "/dev/");
3737c478bd9Sstevel@tonic-gate 		(void) strcat(devbuf, *argv);
3747c478bd9Sstevel@tonic-gate 		pmtab->p_device = devbuf;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (--argc > 0) {
3787c478bd9Sstevel@tonic-gate 		pmtab->p_ttylabel = *++argv;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * every thing after this will be ignored
3837c478bd9Sstevel@tonic-gate 	 * i.e. termtype and linedisc are ignored
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * find_ttyname(fd) 	- find the name of device associated with fd.
3897c478bd9Sstevel@tonic-gate  *			- it first tries utmpx to see if an entry exists
3907c478bd9Sstevel@tonic-gate  *			- with my pid and ut_line is defined. If ut_line
3917c478bd9Sstevel@tonic-gate  *			- is defined, it will see if the major and minor
3927c478bd9Sstevel@tonic-gate  *			- number of fd and devname from utmpx match.
3937c478bd9Sstevel@tonic-gate  *			- If utmpx search fails, ttyname(fd) will be called.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate static	char	*
find_ttyname(fd)3967c478bd9Sstevel@tonic-gate find_ttyname(fd)
3977c478bd9Sstevel@tonic-gate int	fd;
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	pid_t ownpid;
4007c478bd9Sstevel@tonic-gate 	struct utmpx *u;
4017c478bd9Sstevel@tonic-gate 	static	struct	stat	statf, statu;
4027c478bd9Sstevel@tonic-gate 	static	char	buf[BUFSIZ];
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	ownpid = getpid();
4057c478bd9Sstevel@tonic-gate 	setutxent();
4067c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
4077c478bd9Sstevel@tonic-gate 		if (u->ut_pid == ownpid) {
4087c478bd9Sstevel@tonic-gate 			if (strlen(u->ut_line) != 0) {
4097c478bd9Sstevel@tonic-gate 				if (*(u->ut_line) != '/') {
4107c478bd9Sstevel@tonic-gate 					(void) strcpy(buf, "/dev/");
4117c478bd9Sstevel@tonic-gate 					(void) strncat(buf, u->ut_line,
4127c478bd9Sstevel@tonic-gate 						sizeof (u->ut_line));
4137c478bd9Sstevel@tonic-gate 				} else {
4147c478bd9Sstevel@tonic-gate 					(void) strncat(buf, u->ut_line,
4157c478bd9Sstevel@tonic-gate 					    sizeof (u->ut_line));
4167c478bd9Sstevel@tonic-gate 				}
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 			else
4197c478bd9Sstevel@tonic-gate 				u = NULL;
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	endutxent();
4247c478bd9Sstevel@tonic-gate 	if ((u != NULL) &&
4257c478bd9Sstevel@tonic-gate 		(fstat(fd, &statf) == 0) &&
4267c478bd9Sstevel@tonic-gate 		(stat(buf, &statu) == 0) &&
4277c478bd9Sstevel@tonic-gate 		(statf.st_dev == statu.st_dev) &&
4287c478bd9Sstevel@tonic-gate 		(statf.st_rdev == statu.st_rdev)) {
4297c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4307c478bd9Sstevel@tonic-gate 			debug("ttymon_express: find device name from utmpx.");
4317c478bd9Sstevel@tonic-gate #endif
4327c478bd9Sstevel@tonic-gate 			return (buf);
4337c478bd9Sstevel@tonic-gate 	} else {
4347c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4357c478bd9Sstevel@tonic-gate 		debug("ttymon_express: calling ttyname to find device name.");
4367c478bd9Sstevel@tonic-gate #endif
4377c478bd9Sstevel@tonic-gate 		return (ttyname(fd));
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Revoke all access to a device node and make sure that there are
4437c478bd9Sstevel@tonic-gate  * no interposed streams devices attached.  Must be called before a
4447c478bd9Sstevel@tonic-gate  * device is actually opened.
4457c478bd9Sstevel@tonic-gate  * When fdetach is called, the underlying device node is revealed; it
4467c478bd9Sstevel@tonic-gate  * will have the previous owner and that owner can re-attach; so we
4477c478bd9Sstevel@tonic-gate  * retry until we win.
4487c478bd9Sstevel@tonic-gate  * Ignore non-existent devices.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate void
revokedevaccess(char * dev,uid_t uid,gid_t gid,mode_t mode)4517c478bd9Sstevel@tonic-gate revokedevaccess(char *dev, uid_t uid, gid_t gid, mode_t mode)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	do {
4547c478bd9Sstevel@tonic-gate 		if (chown(dev, uid, gid) == -1)
4557c478bd9Sstevel@tonic-gate 			return;
4567c478bd9Sstevel@tonic-gate 	} while (fdetach(dev) == 0);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/* Remove ACLs */
4597c478bd9Sstevel@tonic-gate 
460fa9e4066Sahrens 	(void) acl_strip(dev, uid, gid, mode);
4617c478bd9Sstevel@tonic-gate }
462