xref: /onnv-gate/usr/src/cmd/ttymon/tmexpress.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include	<stdio.h>
33*0Sstevel@tonic-gate #include	<stdlib.h>
34*0Sstevel@tonic-gate #include	<unistd.h>
35*0Sstevel@tonic-gate #include	<fcntl.h>
36*0Sstevel@tonic-gate #include	<errno.h>
37*0Sstevel@tonic-gate #include	<ctype.h>
38*0Sstevel@tonic-gate #include	<string.h>
39*0Sstevel@tonic-gate #include	<signal.h>
40*0Sstevel@tonic-gate #include	<sys/stat.h>
41*0Sstevel@tonic-gate #include	<utmpx.h>
42*0Sstevel@tonic-gate #include	<pwd.h>
43*0Sstevel@tonic-gate #include	<dirent.h>
44*0Sstevel@tonic-gate #include	<sys/param.h>
45*0Sstevel@tonic-gate #include	<sys/acl.h>
46*0Sstevel@tonic-gate #include	"ttymon.h"
47*0Sstevel@tonic-gate #include	"tmextern.h"
48*0Sstevel@tonic-gate #include	"tmstruct.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static	char	devbuf[BUFSIZ];
51*0Sstevel@tonic-gate static	char	*devname;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static	int	parse_args();
54*0Sstevel@tonic-gate static	void	ttymon_options();
55*0Sstevel@tonic-gate static	void	getty_options();
56*0Sstevel@tonic-gate static	void	usage();
57*0Sstevel@tonic-gate static	char	*find_ttyname();
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate extern	void	tmchild();
60*0Sstevel@tonic-gate extern	int	vml();
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate void		revokedevaccess(char *, uid_t, gid_t, mode_t);
63*0Sstevel@tonic-gate /* cannot include libdevinfo.h */
64*0Sstevel@tonic-gate extern int di_devperm_logout(const char *);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * ttymon_express - This is call when ttymon is invoked with args
68*0Sstevel@tonic-gate  *		    or invoked as getty
69*0Sstevel@tonic-gate  *		  - This special version of ttymon will monitor
70*0Sstevel@tonic-gate  *		    one port only
71*0Sstevel@tonic-gate  *		  - It is intended to be used when some process
72*0Sstevel@tonic-gate  *		    wants to have a login session on the fly
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate void
75*0Sstevel@tonic-gate ttymon_express(int argc, char **argv)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	struct	pmtab	*pmtab;
78*0Sstevel@tonic-gate 	struct	sigaction	sigact;
79*0Sstevel@tonic-gate 	extern	int	Retry;
80*0Sstevel@tonic-gate 	extern	void	open_device();
81*0Sstevel@tonic-gate 	extern	void	read_ttydefs();
82*0Sstevel@tonic-gate 	extern	int	checkut_line();
83*0Sstevel@tonic-gate #ifdef	DEBUG
84*0Sstevel@tonic-gate 	extern	FILE	*Debugfp;
85*0Sstevel@tonic-gate 	extern	void	opendebug();
86*0Sstevel@tonic-gate #endif
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate #ifdef	DEBUG
89*0Sstevel@tonic-gate 	opendebug(TRUE);
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	sigact.sa_flags = 0;
93*0Sstevel@tonic-gate 	sigact.sa_handler = SIG_IGN;
94*0Sstevel@tonic-gate 	(void) sigemptyset(&sigact.sa_mask);
95*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &sigact, NULL);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	if ((pmtab = ALLOC_PMTAB) == PNULL) {
98*0Sstevel@tonic-gate 		log("ttymon_express: ALLOC_PMTAB failed");
99*0Sstevel@tonic-gate 		exit(1);
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (parse_args(argc, argv, pmtab) != 0) {
103*0Sstevel@tonic-gate 		log("ttymon_express: parse_args failed");
104*0Sstevel@tonic-gate 		exit(1);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	read_ttydefs(NULL, FALSE);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if ((pmtab->p_device != NULL) && (*(pmtab->p_device) != '\0') &&
110*0Sstevel@tonic-gate 	    strcmp(pmtab->p_device, "/dev/console") == 0) {
111*0Sstevel@tonic-gate 		while (checkut_line(pmtab->p_device))
112*0Sstevel@tonic-gate 			sleep(15);
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if ((pmtab->p_device == NULL) || (*(pmtab->p_device) == '\0')) {
116*0Sstevel@tonic-gate 		devname = find_ttyname(0);
117*0Sstevel@tonic-gate 		if ((devname == NULL) || (*devname == '\0')) {
118*0Sstevel@tonic-gate 			log("ttyname cannot find the device on fd 0");
119*0Sstevel@tonic-gate 			exit(1);
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 		pmtab->p_device = devname;
122*0Sstevel@tonic-gate #ifdef	DEBUG
123*0Sstevel@tonic-gate 		debug("ttymon_express: devname = %s", devname);
124*0Sstevel@tonic-gate #endif
125*0Sstevel@tonic-gate 		/*
126*0Sstevel@tonic-gate 		 * become session leader
127*0Sstevel@tonic-gate 		 * fd 0 is closed and reopened just to make sure
128*0Sstevel@tonic-gate 		 * controlling tty is set up right
129*0Sstevel@tonic-gate 		 */
130*0Sstevel@tonic-gate 		(void) setsid();
131*0Sstevel@tonic-gate 		(void) close(0);
132*0Sstevel@tonic-gate 		revokedevaccess(pmtab->p_device, 0, 0, 0);
133*0Sstevel@tonic-gate 		if (open(pmtab->p_device, O_RDWR) < 0) {
134*0Sstevel@tonic-gate 			log("open %s failed: %s", pmtab->p_device,
135*0Sstevel@tonic-gate 			    strerror(errno));
136*0Sstevel@tonic-gate 			exit(1);
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 		if ((pmtab->p_modules != NULL) &&
139*0Sstevel@tonic-gate 		    (*(pmtab->p_modules) != '\0')) {
140*0Sstevel@tonic-gate 			if (push_linedisc(0, pmtab->p_modules,
141*0Sstevel@tonic-gate 			    pmtab->p_device) == -1)
142*0Sstevel@tonic-gate 				exit(1);
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 		if (initial_termio(0, pmtab) == -1)
145*0Sstevel@tonic-gate 			exit(1);
146*0Sstevel@tonic-gate 		di_devperm_logout((const char *)pmtab->p_device);
147*0Sstevel@tonic-gate 	} else {
148*0Sstevel@tonic-gate 		(void) setsid();
149*0Sstevel@tonic-gate 		(void) close(0);
150*0Sstevel@tonic-gate 		Retry = FALSE;
151*0Sstevel@tonic-gate 		open_device(pmtab);
152*0Sstevel@tonic-gate 		if (Retry)		/* open failed */
153*0Sstevel@tonic-gate 			exit(1);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	tmchild(pmtab);
156*0Sstevel@tonic-gate 	exit(1);	/*NOTREACHED*/
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate  * parse_arg	- parse cmd line arguments
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate static	int
163*0Sstevel@tonic-gate parse_args(int argc, char **argv, struct pmtab *pmtab)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	static	char	p_server[] = "/usr/bin/login";
166*0Sstevel@tonic-gate 	extern	char	*lastname();
167*0Sstevel@tonic-gate 	extern	void	getty_account();
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/* initialize fields to some default first */
170*0Sstevel@tonic-gate 	pmtab->p_tag = "";
171*0Sstevel@tonic-gate 	pmtab->p_flags = 0;
172*0Sstevel@tonic-gate 	pmtab->p_identity = "root";
173*0Sstevel@tonic-gate 	pmtab->p_res1 = "reserved";
174*0Sstevel@tonic-gate 	pmtab->p_res2 = "reserved";
175*0Sstevel@tonic-gate 	pmtab->p_res3 = "reserved";
176*0Sstevel@tonic-gate 	pmtab->p_uid = 0;
177*0Sstevel@tonic-gate 	pmtab->p_gid = 0;
178*0Sstevel@tonic-gate 	pmtab->p_dir = "/";
179*0Sstevel@tonic-gate 	pmtab->p_ttyflags = 0;
180*0Sstevel@tonic-gate 	pmtab->p_count = 0;
181*0Sstevel@tonic-gate 	pmtab->p_server = p_server;
182*0Sstevel@tonic-gate 	pmtab->p_timeout = 0;
183*0Sstevel@tonic-gate 	pmtab->p_modules = "";
184*0Sstevel@tonic-gate 	pmtab->p_prompt = "login: ";
185*0Sstevel@tonic-gate 	pmtab->p_dmsg = "";
186*0Sstevel@tonic-gate 	pmtab->p_termtype = "";
187*0Sstevel@tonic-gate 	pmtab->p_device = "";
188*0Sstevel@tonic-gate 	pmtab->p_status = GETTY;
189*0Sstevel@tonic-gate 	if (strcmp(lastname(argv[0]), "getty") == 0) {
190*0Sstevel@tonic-gate 		pmtab->p_ttylabel = "300";
191*0Sstevel@tonic-gate 		getty_options(argc, argv, pmtab);
192*0Sstevel@tonic-gate 	} else {
193*0Sstevel@tonic-gate 		pmtab->p_ttylabel = "9600";
194*0Sstevel@tonic-gate 		ttymon_options(argc, argv, pmtab);
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 	if ((pmtab->p_device != NULL) && (*(pmtab->p_device) != '\0'))
197*0Sstevel@tonic-gate 		getty_account(pmtab->p_device); /* utmp accounting */
198*0Sstevel@tonic-gate 	return (0);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * 	ttymon_options - scan and check args for ttymon express
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate static	void
207*0Sstevel@tonic-gate ttymon_options(int argc, char **argv, struct pmtab *pmtab)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	int 	c;			/* option letter */
210*0Sstevel@tonic-gate 	char 	*timeout;
211*0Sstevel@tonic-gate 	int  	gflag = 0;		/* -g seen */
212*0Sstevel@tonic-gate 	int	size = 0;
213*0Sstevel@tonic-gate 	char	tbuf[BUFSIZ];
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	extern	char	*optarg;
216*0Sstevel@tonic-gate 	extern	int	optind;
217*0Sstevel@tonic-gate 	extern	void	copystr();
218*0Sstevel@tonic-gate 	extern	char	*strsave();
219*0Sstevel@tonic-gate 	extern	char	*getword();
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "T:gd:ht:p:m:l:")) != -1) {
222*0Sstevel@tonic-gate 		switch (c) {
223*0Sstevel@tonic-gate 		case 'g':
224*0Sstevel@tonic-gate 			gflag = 1;
225*0Sstevel@tonic-gate 			break;
226*0Sstevel@tonic-gate 		case 'd':
227*0Sstevel@tonic-gate 			pmtab->p_device = optarg;
228*0Sstevel@tonic-gate 			break;
229*0Sstevel@tonic-gate 		case 'h':
230*0Sstevel@tonic-gate 			pmtab->p_ttyflags &= ~H_FLAG;
231*0Sstevel@tonic-gate 			break;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		case 'T':
234*0Sstevel@tonic-gate 			pmtab->p_termtype = optarg;
235*0Sstevel@tonic-gate 			break;
236*0Sstevel@tonic-gate /*
237*0Sstevel@tonic-gate  *		case 'b':
238*0Sstevel@tonic-gate  *			pmtab->p_ttyflags |= B_FLAG;
239*0Sstevel@tonic-gate  *			pmtab->p_ttyflags |= R_FLAG;
240*0Sstevel@tonic-gate  *			break;
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate 		case 't':
243*0Sstevel@tonic-gate 			timeout = optarg;
244*0Sstevel@tonic-gate 			while (*optarg) {
245*0Sstevel@tonic-gate 				if (!isdigit(*optarg++)) {
246*0Sstevel@tonic-gate 					log("Invalid argument for "
247*0Sstevel@tonic-gate 					    "\"-t\" -- number expected.");
248*0Sstevel@tonic-gate 					usage();
249*0Sstevel@tonic-gate 				}
250*0Sstevel@tonic-gate 			}
251*0Sstevel@tonic-gate 			pmtab->p_timeout = atoi(timeout);
252*0Sstevel@tonic-gate 			break;
253*0Sstevel@tonic-gate 		case 'p':
254*0Sstevel@tonic-gate 			copystr(tbuf, optarg);
255*0Sstevel@tonic-gate 			pmtab->p_prompt = strsave(getword(tbuf, &size, TRUE));
256*0Sstevel@tonic-gate 			break;
257*0Sstevel@tonic-gate 		case 'm':
258*0Sstevel@tonic-gate 			pmtab->p_modules = optarg;
259*0Sstevel@tonic-gate 			if (vml(pmtab->p_modules) != 0)
260*0Sstevel@tonic-gate 				usage();
261*0Sstevel@tonic-gate 			break;
262*0Sstevel@tonic-gate 		case 'l':
263*0Sstevel@tonic-gate 			pmtab->p_ttylabel = optarg;
264*0Sstevel@tonic-gate 			break;
265*0Sstevel@tonic-gate 		case '?':
266*0Sstevel@tonic-gate 			usage();
267*0Sstevel@tonic-gate 			break;	/*NOTREACHED*/
268*0Sstevel@tonic-gate 		}
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 	if (optind < argc)
271*0Sstevel@tonic-gate 		usage();
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if (!gflag)
274*0Sstevel@tonic-gate 		usage();
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /*
278*0Sstevel@tonic-gate  * usage - print out a usage message
279*0Sstevel@tonic-gate  */
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate static 	void
282*0Sstevel@tonic-gate usage()
283*0Sstevel@tonic-gate {
284*0Sstevel@tonic-gate 	char	*umsg = "Usage: ttymon\n  ttymon -g [-h] [-d device] "
285*0Sstevel@tonic-gate 	    "[-l ttylabel] [-t timeout] [-p prompt] [-m modules]\n";
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (isatty(STDERR_FILENO))
288*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s", umsg);
289*0Sstevel@tonic-gate 	else
290*0Sstevel@tonic-gate 		cons_printf(umsg);
291*0Sstevel@tonic-gate 	exit(1);
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate  *	getty_options	- this is cut from getty.c
296*0Sstevel@tonic-gate  *			- it scan getty cmd args
297*0Sstevel@tonic-gate  *			- modification is made to stuff args in pmtab
298*0Sstevel@tonic-gate  */
299*0Sstevel@tonic-gate static	void
300*0Sstevel@tonic-gate getty_options(argc, argv, pmtab)
301*0Sstevel@tonic-gate int argc;
302*0Sstevel@tonic-gate char **argv;
303*0Sstevel@tonic-gate struct	pmtab	*pmtab;
304*0Sstevel@tonic-gate {
305*0Sstevel@tonic-gate 	char	*ptr;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	/*
308*0Sstevel@tonic-gate 	 * the pre-4.0 getty's hang_up_line() is a no-op.
309*0Sstevel@tonic-gate 	 * For compatibility, H_FLAG cannot be set for this "getty".
310*0Sstevel@tonic-gate 	 */
311*0Sstevel@tonic-gate 	pmtab->p_ttyflags &= ~(H_FLAG);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	while (--argc && **++argv == '-') {
314*0Sstevel@tonic-gate 		for (ptr = *argv + 1; *ptr; ptr++)
315*0Sstevel@tonic-gate 		switch (*ptr) {
316*0Sstevel@tonic-gate 		case 'h':
317*0Sstevel@tonic-gate 			break;
318*0Sstevel@tonic-gate 		case 't':
319*0Sstevel@tonic-gate 			if (isdigit(*++ptr)) {
320*0Sstevel@tonic-gate 				(void) sscanf(ptr, "%d", &(pmtab->p_timeout));
321*0Sstevel@tonic-gate 				while (isdigit(*++ptr));
322*0Sstevel@tonic-gate 				ptr--;
323*0Sstevel@tonic-gate 			} else if (--argc) {
324*0Sstevel@tonic-gate 				if (isdigit(*(ptr = *++argv)))
325*0Sstevel@tonic-gate 					(void) sscanf(ptr, "%d",
326*0Sstevel@tonic-gate 					    &(pmtab->p_timeout));
327*0Sstevel@tonic-gate 				else {
328*0Sstevel@tonic-gate 					log("getty: timeout argument <%s> "
329*0Sstevel@tonic-gate 					    "invalid", *argv);
330*0Sstevel@tonic-gate 					exit(1);
331*0Sstevel@tonic-gate 				}
332*0Sstevel@tonic-gate 			}
333*0Sstevel@tonic-gate 			break;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		case 'c':
336*0Sstevel@tonic-gate 			log("Use \"sttydefs -l\" to check /etc/ttydefs.");
337*0Sstevel@tonic-gate 			exit(0);
338*0Sstevel@tonic-gate 		default:
339*0Sstevel@tonic-gate 			break;
340*0Sstevel@tonic-gate 		}
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if (argc < 1) {
344*0Sstevel@tonic-gate 		log("getty: no terminal line specified.");
345*0Sstevel@tonic-gate 		exit(1);
346*0Sstevel@tonic-gate 	} else {
347*0Sstevel@tonic-gate 		(void) strcat(devbuf, "/dev/");
348*0Sstevel@tonic-gate 		(void) strcat(devbuf, *argv);
349*0Sstevel@tonic-gate 		pmtab->p_device = devbuf;
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if (--argc > 0) {
353*0Sstevel@tonic-gate 		pmtab->p_ttylabel = *++argv;
354*0Sstevel@tonic-gate 	}
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/*
357*0Sstevel@tonic-gate 	 * every thing after this will be ignored
358*0Sstevel@tonic-gate 	 * i.e. termtype and linedisc are ignored
359*0Sstevel@tonic-gate 	 */
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate /*
363*0Sstevel@tonic-gate  * find_ttyname(fd) 	- find the name of device associated with fd.
364*0Sstevel@tonic-gate  *			- it first tries utmpx to see if an entry exists
365*0Sstevel@tonic-gate  *			- with my pid and ut_line is defined. If ut_line
366*0Sstevel@tonic-gate  *			- is defined, it will see if the major and minor
367*0Sstevel@tonic-gate  *			- number of fd and devname from utmpx match.
368*0Sstevel@tonic-gate  *			- If utmpx search fails, ttyname(fd) will be called.
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate static	char	*
371*0Sstevel@tonic-gate find_ttyname(fd)
372*0Sstevel@tonic-gate int	fd;
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	pid_t ownpid;
375*0Sstevel@tonic-gate 	struct utmpx *u;
376*0Sstevel@tonic-gate 	static	struct	stat	statf, statu;
377*0Sstevel@tonic-gate 	static	char	buf[BUFSIZ];
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	ownpid = getpid();
380*0Sstevel@tonic-gate 	setutxent();
381*0Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
382*0Sstevel@tonic-gate 		if (u->ut_pid == ownpid) {
383*0Sstevel@tonic-gate 			if (strlen(u->ut_line) != 0) {
384*0Sstevel@tonic-gate 				if (*(u->ut_line) != '/') {
385*0Sstevel@tonic-gate 					(void) strcpy(buf, "/dev/");
386*0Sstevel@tonic-gate 					(void) strncat(buf, u->ut_line,
387*0Sstevel@tonic-gate 						sizeof (u->ut_line));
388*0Sstevel@tonic-gate 				} else {
389*0Sstevel@tonic-gate 					(void) strncat(buf, u->ut_line,
390*0Sstevel@tonic-gate 					    sizeof (u->ut_line));
391*0Sstevel@tonic-gate 				}
392*0Sstevel@tonic-gate 			}
393*0Sstevel@tonic-gate 			else
394*0Sstevel@tonic-gate 				u = NULL;
395*0Sstevel@tonic-gate 			break;
396*0Sstevel@tonic-gate 		}
397*0Sstevel@tonic-gate 	}
398*0Sstevel@tonic-gate 	endutxent();
399*0Sstevel@tonic-gate 	if ((u != NULL) &&
400*0Sstevel@tonic-gate 		(fstat(fd, &statf) == 0) &&
401*0Sstevel@tonic-gate 		(stat(buf, &statu) == 0) &&
402*0Sstevel@tonic-gate 		(statf.st_dev == statu.st_dev) &&
403*0Sstevel@tonic-gate 		(statf.st_rdev == statu.st_rdev)) {
404*0Sstevel@tonic-gate #ifdef	DEBUG
405*0Sstevel@tonic-gate 			debug("ttymon_express: find device name from utmpx.");
406*0Sstevel@tonic-gate #endif
407*0Sstevel@tonic-gate 			return (buf);
408*0Sstevel@tonic-gate 	} else {
409*0Sstevel@tonic-gate #ifdef	DEBUG
410*0Sstevel@tonic-gate 		debug("ttymon_express: calling ttyname to find device name.");
411*0Sstevel@tonic-gate #endif
412*0Sstevel@tonic-gate 		return (ttyname(fd));
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate /*
417*0Sstevel@tonic-gate  * Revoke all access to a device node and make sure that there are
418*0Sstevel@tonic-gate  * no interposed streams devices attached.  Must be called before a
419*0Sstevel@tonic-gate  * device is actually opened.
420*0Sstevel@tonic-gate  * When fdetach is called, the underlying device node is revealed; it
421*0Sstevel@tonic-gate  * will have the previous owner and that owner can re-attach; so we
422*0Sstevel@tonic-gate  * retry until we win.
423*0Sstevel@tonic-gate  * Ignore non-existent devices.
424*0Sstevel@tonic-gate  */
425*0Sstevel@tonic-gate void
426*0Sstevel@tonic-gate revokedevaccess(char *dev, uid_t uid, gid_t gid, mode_t mode)
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	do {
429*0Sstevel@tonic-gate 		if (chown(dev, uid, gid) == -1)
430*0Sstevel@tonic-gate 			return;
431*0Sstevel@tonic-gate 	} while (fdetach(dev) == 0);
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	/* Remove ACLs */
434*0Sstevel@tonic-gate 	if (acl(dev, GETACLCNT, 0, NULL) > MIN_ACL_ENTRIES) {
435*0Sstevel@tonic-gate 		aclent_t acls[3];
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 		acls[0].a_type = USER_OBJ;
438*0Sstevel@tonic-gate 		acls[0].a_id = uid;
439*0Sstevel@tonic-gate 		acls[0].a_perm = 0;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 		acls[1].a_type = GROUP_OBJ;
442*0Sstevel@tonic-gate 		acls[1].a_id = gid;
443*0Sstevel@tonic-gate 		acls[1].a_perm = 0;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 		acls[2].a_type = OTHER_OBJ;
446*0Sstevel@tonic-gate 		acls[2].a_id = 0;
447*0Sstevel@tonic-gate 		acls[2].a_perm = 0;
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 		(void) acl(dev, SETACL, 3, acls);
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	(void) chmod(dev, mode);
453*0Sstevel@tonic-gate }
454