118480Sralph #ifndef lint
2*25364Sbloom static char sccsid[] = "@(#)acucntrl.c	5.6 (Berkeley) 11/01/85";
318480Sralph #endif
418480Sralph 
518480Sralph /*  acucntrl - turn around tty line between dialin and dialout
618480Sralph  *
718480Sralph  * Usage:	acucntrl {enable,disable} /dev/ttydX
818480Sralph  *
918480Sralph  * History:
1018480Sralph  *	First written by Allan Wilkes (fisher!allan)
1118480Sralph  *
1218480Sralph  *	Modified June 8,1983 by W.Sebok (astrovax!wls) to poke kernel rather
1318480Sralph  * 	than use kernel hack to turn on/off modem control, using subroutine
1418480Sralph  *	stolen from program written by Tsutomu Shimomura
1518480Sralph  *	{astrovax,escher}!tsutomu
1618480Sralph  *
1718480Sralph  *	Worked over many times by W.Sebok (i.e. hacked to death)
1818480Sralph  *
1918480Sralph  * Operation:
2018480Sralph  *   disable (i.e. setup for dialing out)
2118480Sralph  *	(1) check input arguments
2218480Sralph  *	(2) look in /etc/utmp to check that the line is not in use by another
2318480Sralph  *	(3) disable modem control on terminal
2418480Sralph  *	(4) check for carrier on device
2518480Sralph  *	(5) change owner of device to real id
2618480Sralph  *	(6) edit /etc/ttys,  changing the first character of the appropriate
2718480Sralph  *	    line to 0
2818480Sralph  *	(7) send a hangup to process 1 to poke init to disable getty
2918480Sralph  *	(8) post uid name in capitals in /etc/utmp to let world know device has
3018480Sralph  *	    been grabbed
3118480Sralph  *	(9) make sure that DTR is on
3218480Sralph  *
3318480Sralph  *   enable (i.e.) restore for dialin
3418480Sralph  *	(1) check input arguments
3518480Sralph  *	(2) look in /etc/utmp to check that the line is not in use by another
3618480Sralph  *	(3) make sure modem control on terminal is disabled
3718480Sralph  *	(4) turn off DTR to make sure line is hung up
3818480Sralph  *	(5) condition line: clear exclusive use and set hangup on close modes
3918480Sralph  *	(6) turn on modem control
4018480Sralph  *	(7) edit /etc/ttys,  changing the first character of the appropriate
4118480Sralph  *	    line to 1
4218480Sralph  *	(8) send a hangup to process 1 to poke init to enable getty
4318480Sralph  *	(9) clear uid name for /etc/utmp
4418480Sralph  */
4518480Sralph 
4618480Sralph /* #define SENSECARRIER */
4718480Sralph 
4818480Sralph #include "uucp.h"
4918480Sralph #include <sys/buf.h>
5018614Sralph #include <signal.h>
5118480Sralph #include <sys/conf.h>
5218480Sralph #ifdef BSD4_2
5323583Sbloom #include <vaxuba/ubavar.h>
5418614Sralph #else
5518480Sralph #include <sys/ubavar.h>
5618614Sralph #endif
5718480Sralph #include <sys/stat.h>
5818480Sralph #include <nlist.h>
5918480Sralph #include <sgtty.h>
6018480Sralph #include <utmp.h>
6118480Sralph #include <pwd.h>
6218480Sralph #include <stdio.h>
6325123Sbloom #include <sys/file.h>
6418480Sralph 
6518480Sralph #define NDZLINE	8	/* lines/dz */
6618480Sralph #define NDHLINE	16	/* lines/dh */
6718480Sralph #define NDMFLINE 8	/* lines/dmf */
6818480Sralph 
6918480Sralph #define DZ11	1
7018480Sralph #define DH11	2
7118480Sralph #define DMF	3
7218480Sralph 
7318480Sralph #define NLVALUE(val)	(nl[val].n_value)
7418480Sralph 
7518480Sralph struct nlist nl[] = {
7618480Sralph #define CDEVSW	0
7718480Sralph 	{ "_cdevsw" },
7818480Sralph 
7918480Sralph #define DZOPEN	1
8018480Sralph 	{ "_dzopen" },
8118480Sralph #define DZINFO	2
8218480Sralph 	{ "_dzinfo" },
8318480Sralph #define NDZ11	3
8418480Sralph 	{ "_dz_cnt" },
8518480Sralph #define DZSCAR	4
8618480Sralph 	{ "_dzsoftCAR" },
8718480Sralph 
8818480Sralph #define DHOPEN	5
8918480Sralph 	{ "_dhopen" },
9018480Sralph #define DHINFO	6
9118480Sralph 	{ "_dhinfo" },
9218480Sralph #define NDH11	7
9318480Sralph 	{ "_ndh11" },
9418480Sralph #define DHSCAR	8
9518480Sralph 	{ "_dhsoftCAR" },
9618480Sralph 
9718480Sralph #define DMFOPEN	9
9818480Sralph 	{ "_dmfopen" },
9918480Sralph #define DMFINFO	10
10018480Sralph 	{ "_dmfinfo" },
10118480Sralph #define NDMF	11
10218480Sralph 	{ "_ndmf" },
10318480Sralph #define DMFSCAR	12
10418480Sralph 	{ "_dmfsoftCAR" },
10518480Sralph 
10618480Sralph 	{ "\0" }
10718480Sralph };
10818480Sralph 
10918480Sralph #define ENABLE	1
11018480Sralph #define DISABLE	0
11118480Sralph 
11218480Sralph char Etcutmp[] = "/etc/utmp";
11318480Sralph char Etcttys[] = "/etc/ttys";
11423723Sbloom #ifdef BSD4_3
11525123Sbloom FILE *ttysfile, *nttysfile;
11623723Sbloom char NEtcttys[] = "/etc/ttys.new";
11723723Sbloom extern long ftell();
11823723Sbloom #endif BSD4_3
11918480Sralph char Devhome[] = "/dev";
12018480Sralph 
12118480Sralph char usage[] = "Usage: acucntrl {dis|en}able ttydX\n";
12218480Sralph 
12318480Sralph struct utmp utmp;
12418480Sralph char resettty, resetmodem;
12518480Sralph int etcutmp;
12618614Sralph off_t utmploc;
12718614Sralph off_t ttyslnbeg;
12818480Sralph 
12918480Sralph #define NAMSIZ	sizeof(utmp.ut_name)
13018480Sralph #define	LINSIZ	sizeof(utmp.ut_line)
13118480Sralph 
13218480Sralph main(argc, argv)
13318480Sralph int argc; char *argv[];
13418480Sralph {
13518480Sralph 	register char *p;
13618480Sralph 	register int i;
13718480Sralph 	char uname[NAMSIZ], Uname[NAMSIZ];
13818480Sralph 	int enable ;
13918480Sralph 	char *device;
14018480Sralph 	int devfile;
14118480Sralph 	int uid, gid;
14218614Sralph 	off_t lseek();
14318480Sralph 	struct passwd *getpwuid();
14418480Sralph 	char *rindex();
14518480Sralph 	extern int errno;
14618480Sralph 	extern char *sys_errlist[];
14718480Sralph 
14818480Sralph 	/* check input arguments */
14918480Sralph 	if (argc!=3) {
15018480Sralph 		fprintf(stderr, usage);
15118480Sralph 		exit(1);
15218480Sralph 	}
15318480Sralph 
15418480Sralph 	/* interpret command type */
15523723Sbloom 	if (prefix(argv[1], "disable")  || strcmp(argv[1], "dialout")==0)
15618480Sralph 		enable = 0;
15723723Sbloom 	else if (prefix(argv[1], "enable")  || strcmp(argv[1], "dialin")==0)
15818480Sralph 		enable = 1;
15918480Sralph 	else {
16018480Sralph 		fprintf(stderr, usage);
16118480Sralph 		exit(1);
16218480Sralph 	}
16318480Sralph 
16423723Sbloom 	device = rindex(argv[2], '/');
16518480Sralph 	device = (device == NULL) ? argv[2]: device+1;
16618480Sralph 
16718480Sralph 	/* only recognize devices of the form ttydX */
16823723Sbloom 	if (strncmp(device, "ttyd", 4)!=0) {
16923723Sbloom 		fprintf(stderr, "Bad Device Name %s", device);
17018480Sralph 		exit(1);
17118480Sralph 	}
17218480Sralph 
17318480Sralph 	opnttys(device);
17418480Sralph 
17518480Sralph 	/* Get nlist info */
17623723Sbloom 	nlist("/vmunix", nl);
17718480Sralph 
17818480Sralph 	/* Chdir to /dev */
17918480Sralph 	if(chdir(Devhome) < 0) {
18018480Sralph 		fprintf(stderr, "Cannot chdir to %s: %s\r\n",
18118480Sralph 			Devhome, sys_errlist[errno]);
18218480Sralph 		exit(1);
18318480Sralph 	}
18418480Sralph 
18518480Sralph 	/* Get uid information */
18618480Sralph 	uid = getuid();
18718480Sralph 	gid = getgid();
18818480Sralph 
18918480Sralph 	p = getpwuid(uid)->pw_name;
19018480Sralph 	if (p==NULL) {
19123723Sbloom 		fprintf(stderr, "cannot get uid name\n");
19218480Sralph 		exit(1);
19318480Sralph 	}
19418480Sralph 
19518480Sralph 	/*  to upper case */
19618480Sralph 	i = 0;
19718480Sralph 	do {
19818480Sralph 		uname[i] = *p;
19918480Sralph 		Uname[i] = (*p>='a' && *p<='z') ? (*p - ('a'-'A')) : *p;
20018480Sralph 		i++; p++;
20118480Sralph 	} while (*p && i<NAMSIZ);
20218480Sralph 
20318480Sralph 
20418480Sralph 	/* check to see if line is being used */
20518480Sralph 	if( (etcutmp = open(Etcutmp, 2)) < 0) {
20623723Sbloom 		fprintf(stderr, "On open %s open: %s\n",
20723723Sbloom 			Etcutmp, sys_errlist[errno]);
20818480Sralph 		exit(1);
20918480Sralph 	}
21018480Sralph 
21123723Sbloom 	(void)lseek(etcutmp, utmploc, 0);
21218480Sralph 
21323723Sbloom 	i = read(etcutmp, (char *)&utmp, sizeof(struct utmp));
21418480Sralph 
21518480Sralph 	if(
21618480Sralph 		i == sizeof(struct utmp) &&
21718480Sralph 		utmp.ut_line[0] != '\0'  &&
21818480Sralph 		utmp.ut_name[0] != '\0'  &&
21918480Sralph 		(
22023723Sbloom 			!upcase(utmp.ut_name, NAMSIZ) ||
22118480Sralph 			(
22218480Sralph 				uid != 0 &&
22323723Sbloom 				strncmp(utmp.ut_name, Uname, NAMSIZ) != 0
22418480Sralph 			)
22518480Sralph 		)
22618480Sralph 	) {
22718480Sralph 		fprintf(stderr, "%s in use by %s\n", device, utmp.ut_name);
22818480Sralph 		exit(2);
22918480Sralph 	}
23018480Sralph 
23118480Sralph 	/* Disable modem control */
23223723Sbloom 	if (setmodem(device, DISABLE) < 0) {
23323723Sbloom 		fprintf(stderr, "Unable to disable modem control\n");
23418480Sralph 		exit(1);
23518480Sralph 	}
23618480Sralph 
23718480Sralph 	if (enable) {
23818480Sralph 		if((devfile = open(device, 1)) < 0) {
23923723Sbloom 			fprintf(stderr, "On open of %s: %s\n",
24018480Sralph 				device, sys_errlist[errno]);
24123723Sbloom 			(void)setmodem(device, resetmodem);
24218480Sralph 			exit(1);
24318480Sralph 		}
24418480Sralph 		/* Try one last time to hang up */
24523723Sbloom 		if (ioctl(devfile, (int)TIOCCDTR, (char *)0) < 0)
24623723Sbloom 			fprintf(stderr, "On TIOCCDTR ioctl: %s\n",
24718480Sralph 				sys_errlist[errno]);
24818480Sralph 
24923723Sbloom 		if (ioctl(devfile, (int)TIOCNXCL, (char *)0) < 0)
25018480Sralph 			fprintf(stderr,
25118480Sralph 			    "Cannot clear Exclusive Use on %s: %s\n",
25218480Sralph 				device, sys_errlist[errno]);
25318480Sralph 
25423723Sbloom 		if (ioctl(devfile, (int)TIOCHPCL, (char *)0) < 0)
25518480Sralph 			fprintf(stderr,
25618480Sralph 			    "Cannot set hangup on close on %s: %s\n",
25718480Sralph 				device, sys_errlist[errno]);
25818480Sralph 
25918480Sralph 		i = resetmodem;
26018480Sralph 
26123723Sbloom 		if (setmodem(device, ENABLE) < 0) {
26223723Sbloom 			fprintf(stderr, "Cannot Enable modem control\n");
26323723Sbloom 			(void)setmodem(device, i);
26418480Sralph 			exit(1);
26518480Sralph 		}
26618480Sralph 		resetmodem=i;
26718480Sralph 
26818614Sralph 		if (settys(ENABLE)) {
26923723Sbloom 			fprintf(stderr, "%s already enabled\n", device);
27018614Sralph 		} else {
27123723Sbloom 			pokeinit(device, Uname, enable);
27218614Sralph 		}
27323723Sbloom 		post(device, "");
27418480Sralph 
27518480Sralph 	} else {
27618480Sralph #if defined(TIOCMGET) && defined(SENSECARRIER)
27718480Sralph 		if (uid!=0) {
27818480Sralph 			int linestat = 0;
27918480Sralph 
28018480Sralph 			/* check for presence of carrier */
28118480Sralph 			sleep(2); /* need time after modem control turnoff */
28218480Sralph 
28318480Sralph 			if((devfile = open(device, 1)) < 0) {
28423723Sbloom 				fprintf(stderr, "On open of %s: %s\n",
28518480Sralph 					device, sys_errlist[errno]);
28623723Sbloom 				(void)setmodem(device, resetmodem);
28718480Sralph 				exit(1);
28818480Sralph 			}
28918480Sralph 
29023723Sbloom 			(void)ioctl(devfile, TIOCMGET, &linestat);
29118480Sralph 
29218480Sralph 			if (linestat&TIOCM_CAR) {
29323723Sbloom 				fprintf(stderr, "%s is in use (Carrier On)\n",
29418480Sralph 					device);
29523723Sbloom 				(void)setmodem(device, resetmodem);
29618480Sralph 				exit(2);
29718480Sralph 			}
29818480Sralph 			(void)close(devfile);
29918480Sralph 		}
30018480Sralph #endif TIOCMGET
30118480Sralph 		/* chown device */
30218480Sralph 		if(chown(device, uid, gid) < 0)
30318480Sralph 			fprintf(stderr, "Cannot chown %s: %s\n",
30418480Sralph 				device, sys_errlist[errno]);
30518480Sralph 
30618480Sralph 
30718480Sralph 		/* poke init */
30818614Sralph 		if(settys(DISABLE)) {
30923723Sbloom 			fprintf(stderr, "%s already disabled\n", device);
31018614Sralph 		} else {
31123723Sbloom 			pokeinit(device, Uname, enable);
31218614Sralph 		}
31323723Sbloom 		post(device, Uname);
31425123Sbloom 		if((devfile = open(device, O_RDWR|O_NDELAY)) < 0) {
31518480Sralph 			fprintf(stderr, "On %s open: %s\n",
31618480Sralph 				device, sys_errlist[errno]);
31718480Sralph 		} else {
31818614Sralph 			if(ioctl(devfile, (int)TIOCSDTR, (char *)0) < 0)
31918480Sralph 				fprintf(stderr,
32018480Sralph 				    "Cannot set DTR on %s: %s\n",
32118480Sralph 					device, sys_errlist[errno]);
32218480Sralph 		}
32318480Sralph 	}
32418480Sralph 
32518480Sralph 	exit(0);
32618480Sralph }
32718480Sralph 
32818480Sralph /* return true if no lower case */
32923723Sbloom upcase(str, len)
33018480Sralph register char *str;
33118480Sralph register int len;
33218480Sralph {
33318480Sralph 	for (; *str, --len >= 0 ; str++)
33418480Sralph 		if (*str>='a' && *str<='z')
33518480Sralph 			return(0);
33618480Sralph 	return(1);
33718480Sralph }
33818480Sralph 
33918480Sralph /* Post name to public */
34023723Sbloom post(device, name)
34118480Sralph char *device, *name;
34218480Sralph {
34318614Sralph 	(void)time((time_t *)&utmp.ut_time);
34423583Sbloom 	strncpy(utmp.ut_line, device, LINSIZ);
34523583Sbloom 	strncpy(utmp.ut_name, name,  NAMSIZ);
34625123Sbloom 	if (lseek(etcutmp, utmploc, 0) < 0)
34723723Sbloom 		fprintf(stderr, "on lseek in /etc/utmp: %s",
34818480Sralph 			sys_errlist[errno]);
34925123Sbloom 	if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0)
35023723Sbloom 		fprintf(stderr, "on write in /etc/utmp: %s",
35118480Sralph 			sys_errlist[errno]);
35218480Sralph }
35318480Sralph 
35418480Sralph /* poke process 1 and wait for it to do its thing */
35523723Sbloom pokeinit(device, uname, enable)
35618480Sralph char *uname, *device; int enable;
35718480Sralph {
35818480Sralph 	struct utmp utmp;
35918614Sralph 	register int i;
36018480Sralph 
36118480Sralph 	post(device, uname);
36218480Sralph 
36318480Sralph 	/* poke init */
36418480Sralph 	if (kill(1, SIGHUP)) {
36518480Sralph 		fprintf(stderr,
36618480Sralph 		    "Cannot send hangup to init process: %s\n",
36718480Sralph 			sys_errlist[errno]);
36818614Sralph 		(void)settys(resettty);
36923723Sbloom 		(void)setmodem(device, resetmodem);
37018480Sralph 		exit(1);
37118480Sralph 	}
37218480Sralph 
37318480Sralph 	if (enable)
37418480Sralph 		return;
37518480Sralph 
37618480Sralph 	/* wait till init has responded, clearing the utmp entry */
37725123Sbloom 	i = 100;
37818480Sralph 	do {
37918480Sralph 		sleep(1);
38025123Sbloom 		if (lseek(etcutmp, utmploc, 0) < 0)
38123723Sbloom 			fprintf(stderr, "On lseek in /etc/utmp: %s",
38218480Sralph 				sys_errlist[errno]);
38325123Sbloom 		if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0)
38423723Sbloom 			fprintf(stderr, "On read from /etc/utmp: %s",
38518480Sralph 				sys_errlist[errno]);
38618614Sralph 	} while (utmp.ut_name[0] != '\0' && --i > 0);
38718480Sralph }
38818480Sralph 
38923723Sbloom #ifdef BSD4_3
39018480Sralph /* identify terminal line in ttys */
39118480Sralph opnttys(device)
39218480Sralph char *device;
39318480Sralph {
39423723Sbloom 	register int  ndevice;
39523723Sbloom 	register char *p;
39623723Sbloom 	char *index();
39723723Sbloom 	char linebuf[BUFSIZ];
39823723Sbloom 
39925123Sbloom 	ttysfile = NULL;
40025123Sbloom 	do {
40125123Sbloom 		if (ttysfile != NULL) {
40225123Sbloom 			fclose(ttysfile);
40325123Sbloom 			sleep(5);
40425123Sbloom 		}
40525123Sbloom 		ttysfile = fopen(Etcttys, "r");
40625123Sbloom 		if(ttysfile == NULL) {
40725123Sbloom 			fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
40825123Sbloom 				sys_errlist[errno]);
40925123Sbloom 			exit(1);
41025123Sbloom 		}
41125123Sbloom 	} while (flock(fileno(ttysfile), LOCK_NB|LOCK_EX) < 0);
41223723Sbloom 	nttysfile = fopen(NEtcttys, "w");
41323723Sbloom 	if(nttysfile == NULL) {
41423723Sbloom 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
41523723Sbloom 			sys_errlist[errno]);
41623723Sbloom 		exit(1);
41723723Sbloom 	}
41823723Sbloom 
41923723Sbloom 	ndevice = strlen(device);
42025123Sbloom 	utmploc = sizeof(utmp);
42123723Sbloom 
42223723Sbloom 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
42325123Sbloom 		if(strncmp(device, linebuf, ndevice) == 0)
42425123Sbloom 			return;
42525123Sbloom 		ttyslnbeg += strlen(linebuf);
42623723Sbloom 		utmploc += sizeof(utmp);
42723723Sbloom 		if (fputs(linebuf, nttysfile) == NULL) {
42823723Sbloom 			fprintf(stderr, "On %s write: %s\n",
42923723Sbloom 				Etcttys, sys_errlist[errno]);
43023723Sbloom 			exit(1);
43123723Sbloom 		}
43223723Sbloom 
43323723Sbloom 	}
43423723Sbloom 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
43523723Sbloom 	exit(1);
43623723Sbloom }
43723723Sbloom 
43823723Sbloom /* modify appropriate line in /etc/ttys to turn on/off the device */
43923723Sbloom settys(enable)
44023723Sbloom int enable;
44123723Sbloom {
44223723Sbloom 	register char *cp, *cp2;
44323723Sbloom 	char lbuf[BUFSIZ];
44423723Sbloom 	int i;
44523723Sbloom 	char c1, c2;
44623723Sbloom 
44725123Sbloom 	(void) fseek(ttysfile, ttyslnbeg, 0);
44825123Sbloom 	if(fgets(lbuf, BUFSIZ, ttysfile) == NULL) {
44923723Sbloom 		fprintf(stderr, "On %s read: %s\n",
45023723Sbloom 			Etcttys, sys_errlist[errno]);
45123723Sbloom 		exit(1);
45223723Sbloom 	}
45323723Sbloom 	/* format is now */
45423723Sbloom 	/* ttyd0 std.100 dialup on secure # comment */
455*25364Sbloom 	/* except, 2nd item may have embedded spaces inside quotes, Hubert */
45623723Sbloom 	cp = lbuf;
45723723Sbloom 	for (i=0;*cp && i<3;i++) {
458*25364Sbloom 		if (*cp == '"') {
45923723Sbloom 			cp++;
460*25364Sbloom 			while (*cp && *cp != '"')
461*25364Sbloom 				cp++;
462*25364Sbloom 			if (*cp != '\0')
463*25364Sbloom 				cp++;
464*25364Sbloom 		}else {
465*25364Sbloom 			while (*cp && *cp != ' ' && *cp != '\t')
466*25364Sbloom 				cp++;
467*25364Sbloom 		}
46823723Sbloom 		while (*cp && (*cp == ' ' || *cp == '\t'))
46923723Sbloom 			cp++;
47023723Sbloom 	}
47123723Sbloom 	if (*cp == '\0') {
47223723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
47323723Sbloom 		exit(1);
47423723Sbloom 	}
47523723Sbloom 	c1 = *--cp;
47623723Sbloom 	*cp++ = '\0';
47723723Sbloom 	cp2 = cp;
47823723Sbloom 	while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
47923723Sbloom 		cp++;
48023723Sbloom 	if (*cp == '\0') {
48123723Sbloom 		fprintf(stderr,"Badly formatted line in /etc/ttys:\n%s", lbuf);
48223723Sbloom 		exit(1);
48323723Sbloom 	}
48423723Sbloom 	c2 = *cp;
48523723Sbloom 	*cp++ = '\0';
48623723Sbloom 	while (*cp && (*cp == ' ' || *cp == '\t'))
48723723Sbloom 		cp++;
48823723Sbloom 	resettty = strcmp("on", cp2) != 0;
48925123Sbloom 	fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
49025123Sbloom 	if (ferror(nttysfile)) {
49123723Sbloom 		fprintf(stderr, "On %s fprintf: %s\n",
49223723Sbloom 			NEtcttys, sys_errlist[errno]);
49323723Sbloom 		exit(1);
49423723Sbloom 	}
49525123Sbloom 	while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) {
49625123Sbloom 		if (fputs(lbuf, nttysfile) == NULL) {
49723723Sbloom 			fprintf(stderr, "On %s write: %s\n",
49823723Sbloom 				NEtcttys, sys_errlist[errno]);
49923723Sbloom 			exit(1);
50023723Sbloom 		}
50123723Sbloom 	}
50223723Sbloom 
50323723Sbloom 	if (enable^resettty)
50423723Sbloom 		(void) unlink(NEtcttys);
50523723Sbloom 	else {
50623723Sbloom 		struct stat statb;
50723723Sbloom 		if (stat(Etcttys, &statb) == 0) {
50825123Sbloom 			fchmod(fileno(nttysfile) ,statb.st_mode);
50925123Sbloom 			fchown(fileno(nttysfile), statb.st_uid, statb.st_gid);
51023723Sbloom 		}
51123723Sbloom 		(void) rename(NEtcttys, Etcttys);
51223723Sbloom 	}
51325123Sbloom 	(void) fclose(nttysfile);
51425123Sbloom 	(void) fclose(ttysfile);
51523723Sbloom 	return enable^resettty;
51623723Sbloom }
51723723Sbloom 
51823723Sbloom #else !BSD4_3
51923723Sbloom 
52023723Sbloom /* identify terminal line in ttys */
52123723Sbloom opnttys(device)
52223723Sbloom char *device;
52323723Sbloom {
52418480Sralph 	register FILE *ttysfile;
52518480Sralph 	register int  ndevice, lnsiz;
52618480Sralph 	register char *p;
52718480Sralph 	char *index();
52823723Sbloom 	char linebuf[BUFSIZ];
52918480Sralph 
53018480Sralph 	ttysfile = fopen(Etcttys, "r");
53118480Sralph 	if(ttysfile == NULL) {
53218480Sralph 		fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
53318480Sralph 			sys_errlist[errno]);
53418480Sralph 		exit(1);
53518480Sralph 	}
53618480Sralph 
53718480Sralph 	ndevice = strlen(device);
53818480Sralph 	ttyslnbeg = 0;
53918480Sralph 	utmploc = 0;
54018480Sralph 
54118480Sralph 	while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
54218480Sralph 		lnsiz = strlen(linebuf);
54323723Sbloom 		if ((p = index(linebuf, '\n')) != NULL)
54418480Sralph 			*p = '\0';
54518480Sralph 		if(strncmp(device, &linebuf[2], ndevice) == 0) {
54618480Sralph 			(void)fclose(ttysfile);
54718480Sralph 			return;
54818480Sralph 		}
54918480Sralph 		ttyslnbeg += lnsiz;
55025123Sbloom 		utmploc += sizeof(utmp);
55118480Sralph 	}
55218480Sralph 	fprintf(stderr, "%s not found in %s\n", device, Etcttys);
55318480Sralph 	exit(1);
55418480Sralph }
55518480Sralph 
55618480Sralph /* modify appropriate line in /etc/ttys to turn on/off the device */
55718480Sralph settys(enable)
55818480Sralph int enable;
55918480Sralph {
56018480Sralph 	int ittysfil;
56123723Sbloom 	char out, in;
56218480Sralph 
56318480Sralph 	ittysfil = open(Etcttys, 2);
56423723Sbloom 	if(ittysfil < 0) {
56518480Sralph 		fprintf(stderr, "Cannot open %s for output: %s\n",
56618480Sralph 			Etcttys, sys_errlist[errno]);
56718480Sralph 		exit(1);
56818480Sralph 	}
56923723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
57023723Sbloom 	if(read(ittysfil, &in, 1)<0) {
57123723Sbloom 		fprintf(stderr, "On %s write: %s\n",
57218480Sralph 			Etcttys, sys_errlist[errno]);
57318480Sralph 		exit(1);
57418480Sralph 	}
57518480Sralph 	resettty = (in == '1');
57618480Sralph 	out = enable ? '1' : '0';
57723723Sbloom 	(void)lseek(ittysfil, ttyslnbeg, 0);
57823723Sbloom 	if(write(ittysfil, &out, 1)<0) {
57923723Sbloom 		fprintf(stderr, "On %s write: %s\n",
58018480Sralph 			Etcttys, sys_errlist[errno]);
58118480Sralph 		exit(1);
58218480Sralph 	}
58318480Sralph 	(void)close(ittysfil);
58418614Sralph 	return(in==out);
58518480Sralph }
58623723Sbloom #endif !BSD4_3
58718480Sralph 
58818480Sralph /*
58923723Sbloom  * Excerpted from (June 8, 1983 W.Sebok)
59018480Sralph  * > ttymodem.c - enable/disable modem control for tty lines.
59118480Sralph  * >
59218480Sralph  * > Knows about DZ11s and DH11/DM11s.
59318480Sralph  * > 23.3.83 - TS
59423723Sbloom  * > modified to know about DMF's  (hasn't been tested) Nov 8, 1984 - WLS
59518480Sralph  */
59618480Sralph 
59718480Sralph 
59823723Sbloom setmodem(ttyline, enable)
59918480Sralph char *ttyline; int enable;
60018480Sralph {
60118480Sralph 	dev_t dev;
60218480Sralph 	int kmem;
60323723Sbloom 	int unit, line, nlines, addr, tflags;
60418614Sralph 	int devtype=0;
60518614Sralph 	char cflags; short sflags;
60618614Sralph #ifdef BSD4_2
60718614Sralph 	int flags;
60818614Sralph #else
60918614Sralph 	short flags;
61018614Sralph #endif
61118480Sralph 	struct uba_device *ubinfo;
61218480Sralph 	struct stat statb;
61318480Sralph 	struct cdevsw cdevsw;
61418480Sralph 
61518480Sralph 	if(nl[CDEVSW].n_type == 0) {
61623723Sbloom 		fprintf(stderr, "No namelist.\n");
61718480Sralph 		return(-1);
61818480Sralph 	}
61918480Sralph 
62018480Sralph 	if((kmem = open("/dev/kmem", 2)) < 0) {
62123723Sbloom 		fprintf(stderr, "/dev/kmem open: %s\n", sys_errlist[errno]);
62218480Sralph 		return(-1);
62318480Sralph 	}
62418480Sralph 
62523723Sbloom 	if(stat(ttyline, &statb) < 0) {
62623723Sbloom 		fprintf(stderr, "%s stat: %s\n", ttyline, sys_errlist[errno]);
62718480Sralph 		return(-1);
62818480Sralph 	}
62918480Sralph 
63018614Sralph 	if((statb.st_mode&S_IFMT) != S_IFCHR) {
63123723Sbloom 		fprintf(stderr, "%s is not a character device.\n",ttyline);
63218480Sralph 		return(-1);
63318480Sralph 	}
63418480Sralph 
63518480Sralph 	dev = statb.st_rdev;
63618480Sralph 	(void)lseek(kmem,
63718614Sralph 		(off_t) &(((struct cdevsw *)NLVALUE(CDEVSW))[major(dev)]),0);
63823723Sbloom 	(void)read(kmem, (char *) &cdevsw, sizeof cdevsw);
63918480Sralph 
64018480Sralph 	if((int)(cdevsw.d_open) == NLVALUE(DZOPEN)) {
64118480Sralph 		devtype = DZ11;
64218480Sralph 		unit = minor(dev) / NDZLINE;
64318480Sralph 		line = minor(dev) % NDZLINE;
64418480Sralph 		addr = (int) &(((int *)NLVALUE(DZINFO))[unit]);
64523723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDZ11), 0);
64618480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DHOPEN)) {
64718480Sralph 		devtype = DH11;
64818480Sralph 		unit = minor(dev) / NDHLINE;
64918480Sralph 		line = minor(dev) % NDHLINE;
65018480Sralph 		addr = (int) &(((int *)NLVALUE(DHINFO))[unit]);
65123723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDH11), 0);
65218480Sralph 	} else if((int)(cdevsw.d_open) == NLVALUE(DMFOPEN)) {
65318480Sralph 		devtype = DMF;
65418480Sralph 		unit = minor(dev) / NDMFLINE;
65518480Sralph 		line = minor(dev) % NDMFLINE;
65618480Sralph 		addr = (int) &(((int *)NLVALUE(DMFINFO))[unit]);
65723723Sbloom 		(void)lseek(kmem, (off_t) NLVALUE(NDMF), 0);
65818480Sralph 	} else {
65923723Sbloom 		fprintf(stderr, "Device %s (%d/%d) unknown.\n", ttyline,
66023723Sbloom 		    major(dev), minor(dev));
66118480Sralph 		return(-1);
66218480Sralph 	}
66318480Sralph 
66423723Sbloom 	(void)read(kmem, (char *) &nlines, sizeof nlines);
66518480Sralph 	if(minor(dev) >= nlines) {
66623723Sbloom 		fprintf(stderr, "Sub-device %d does not exist (only %d).\n",
66723723Sbloom 		    minor(dev), nlines);
66818480Sralph 		return(-1);
66918480Sralph 	}
67018480Sralph 
67123723Sbloom 	(void)lseek(kmem, (off_t)addr, 0);
67223723Sbloom 	(void)read(kmem, (char *) &ubinfo, sizeof ubinfo);
67323723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
67423723Sbloom 	(void)read(kmem, (char *) &flags, sizeof flags);
67518480Sralph 
67618480Sralph 	tflags = 1<<line;
67718480Sralph 	resetmodem = ((flags&tflags) == 0);
67818480Sralph 	flags = enable ? (flags & ~tflags) : (flags | tflags);
67923723Sbloom 	(void)lseek(kmem, (off_t) &(ubinfo->ui_flags), 0);
68023723Sbloom 	(void)write(kmem, (char *) &flags, sizeof flags);
68118480Sralph 	switch(devtype) {
68218480Sralph 		case DZ11:
68318480Sralph 			if((addr = NLVALUE(DZSCAR)) == 0) {
68423723Sbloom 				fprintf(stderr, "No dzsoftCAR.\n");
68518480Sralph 				return(-1);
68618480Sralph 			}
68718614Sralph 			cflags = flags;
68823723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
68923723Sbloom 			(void)write(kmem, (char *) &cflags, sizeof cflags);
69018480Sralph 			break;
69118480Sralph 		case DH11:
69218480Sralph 			if((addr = NLVALUE(DHSCAR)) == 0) {
69323723Sbloom 				fprintf(stderr, "No dhsoftCAR.\n");
69418480Sralph 				return(-1);
69518480Sralph 			}
69618614Sralph 			sflags = flags;
69723723Sbloom 			(void)lseek(kmem, (off_t) &(((short *)addr)[unit]), 0);
69823723Sbloom 			(void)write(kmem, (char *) &sflags, sizeof sflags);
69918480Sralph 			break;
70018480Sralph 		case DMF:
70118480Sralph 			if((addr = NLVALUE(DMFSCAR)) == 0) {
70223723Sbloom 				fprintf(stderr, "No dmfsoftCAR.\n");
70318480Sralph 				return(-1);
70418480Sralph 			}
70518614Sralph 			cflags = flags;
70623723Sbloom 			(void)lseek(kmem, (off_t) &(((char *)addr)[unit]), 0);
70723723Sbloom 			(void)write(kmem, (char *) &flags, sizeof cflags);
70818480Sralph 			break;
70918480Sralph 		default:
71023723Sbloom 			fprintf(stderr, "Unknown device type\n");
71118480Sralph 			return(-1);
71218480Sralph 	}
71318480Sralph 	return(0);
71418480Sralph }
71518480Sralph 
71618480Sralph prefix(s1, s2)
71718480Sralph 	register char *s1, *s2;
71818480Sralph {
71918480Sralph 	register char c;
72018480Sralph 
72118480Sralph 	while ((c = *s1++) == *s2++)
72218480Sralph 		if (c == '\0')
72318480Sralph 			return (1);
72418480Sralph 	return (c == '\0');
72518480Sralph }
726