xref: /csrg-svn/usr.bin/tip/tip.c (revision 25905)
119806Sdist /*
219806Sdist  * Copyright (c) 1983 Regents of the University of California.
319806Sdist  * All rights reserved.  The Berkeley software License Agreement
419806Sdist  * specifies the terms and conditions for redistribution.
519806Sdist  */
619806Sdist 
713279Ssam #ifndef lint
819806Sdist char copyright[] =
919806Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1019806Sdist  All rights reserved.\n";
1119806Sdist #endif not lint
125136Ssam 
1319806Sdist #ifndef lint
14*25905Skarels static char sccsid[] = "@(#)tip.c	5.3 (Berkeley) 01/13/86";
1519806Sdist #endif not lint
1619806Sdist 
173696Sroot /*
185136Ssam  * tip - UNIX link to other systems
193696Sroot  *  tip [-v] [-speed] system-name
205136Ssam  * or
215136Ssam  *  cu phone-number [-s speed] [-l line] [-a acu]
223696Sroot  */
233696Sroot #include "tip.h"
243696Sroot 
253696Sroot /*
263696Sroot  * Baud rate mapping table
273696Sroot  */
283696Sroot int bauds[] = {
293696Sroot 	0, 50, 75, 110, 134, 150, 200, 300, 600,
303696Sroot 	1200, 1800, 2400, 4800, 9600, 19200, -1
313696Sroot };
323696Sroot 
334004Ssam int	disc = OTTYDISC;		/* tip normally runs this way */
343696Sroot int	intprompt();
353696Sroot int	timeout();
365136Ssam int	cleanup();
375257Sshannon char	*sname();
3825550Sdonn char	PNbuf[256];			/* This limits the size of a number */
395257Sshannon extern char *sprintf();
403696Sroot 
413696Sroot main(argc, argv)
424962Ssam 	char *argv[];
433696Sroot {
443696Sroot 	char *system = NOSTR;
453696Sroot 	register int i;
465257Sshannon 	register char *p;
475257Sshannon 	char sbuf[12];
483696Sroot 
495257Sshannon 	if (equal(sname(argv[0]), "cu")) {
50*25905Skarels 		cumode = 1;
515136Ssam 		cumain(argc, argv);
525136Ssam 		goto cucommon;
535136Ssam 	}
545136Ssam 
553696Sroot 	if (argc > 4) {
563696Sroot 		fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
573696Sroot 		exit(1);
583696Sroot 	}
593696Sroot 	if (!isatty(0)) {
603696Sroot 		fprintf(stderr, "tip: must be interactive\n");
613696Sroot 		exit(1);
623696Sroot 	}
635257Sshannon 
645257Sshannon 	for (; argc > 1; argv++, argc--) {
655257Sshannon 		if (argv[1][0] != '-')
665257Sshannon 			system = argv[1];
675257Sshannon 		else switch (argv[1][1]) {
685257Sshannon 
695257Sshannon 		case 'v':
705257Sshannon 			vflag++;
715257Sshannon 			break;
725257Sshannon 
735257Sshannon 		case '0': case '1': case '2': case '3': case '4':
745257Sshannon 		case '5': case '6': case '7': case '8': case '9':
755257Sshannon 			BR = atoi(&argv[1][1]);
765257Sshannon 			break;
775257Sshannon 
785257Sshannon 		default:
795257Sshannon 			fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
805257Sshannon 			break;
815257Sshannon 		}
825257Sshannon 	}
835257Sshannon 
847593Sshannon 	if (system == NOSTR)
857593Sshannon 		goto notnumber;
865257Sshannon 	for (p = system; *p; p++)
875257Sshannon 		if (isalpha(*p))
885257Sshannon 			goto notnumber;
8925550Sdonn 	/*
9025550Sdonn 	 * System name is really a phone number...
9125550Sdonn 	 * Copy the number then stomp on the original (in case the number
9225550Sdonn 	 *	is private, we don't want 'ps' or 'w' to find it).
9325550Sdonn 	 */
9425550Sdonn 	if (strlen(system) > sizeof PNbuf - 1) {
9525550Sdonn 		fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
9625550Sdonn 			sizeof PNbuf - 1);
9725550Sdonn 		exit(1);
9825550Sdonn 	}
9925550Sdonn 	strncpy( PNbuf, system, sizeof PNbuf - 1 );
10025550Sdonn 	for (p = system; *p; p++)
10125550Sdonn 		*p = '\0';
10225550Sdonn 	PN = PNbuf;
1035257Sshannon 	system = sprintf(sbuf, "tip%d", BR);
1045257Sshannon 
1055257Sshannon notnumber:
1063696Sroot 	signal(SIGINT, cleanup);
1073696Sroot 	signal(SIGQUIT, cleanup);
1083696Sroot 	signal(SIGHUP, cleanup);
1093696Sroot 	signal(SIGTERM, cleanup);
1103898Ssam 
1113696Sroot 	if ((i = hunt(system)) == 0) {
1123696Sroot 		printf("all ports busy\n");
1133696Sroot 		exit(3);
1143696Sroot 	}
1153696Sroot 	if (i == -1) {
1163696Sroot 		printf("link down\n");
1174232Ssam 		delock(uucplock);
1183696Sroot 		exit(3);
1193696Sroot 	}
1203719Ssam 	setbuf(stdout, NULL);
1213696Sroot 	loginit();
1225257Sshannon 
1233696Sroot 	/*
1243696Sroot 	 * Kludge, their's no easy way to get the initialization
1253696Sroot 	 *   in the right order, so force it here
1263696Sroot 	 */
1273696Sroot 	if ((PH = getenv("PHONES")) == NOSTR)
1283696Sroot 		PH = "/etc/phones";
1293696Sroot 	vinit();				/* init variables */
13013430Ssam 	setparity("even");			/* set the parity table */
1314004Ssam 	if ((i = speed(number(value(BAUDRATE)))) == NULL) {
1323696Sroot 		printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
1333696Sroot 		delock(uucplock);
1343696Sroot 		exit(3);
1353696Sroot 	}
1363898Ssam 
1374004Ssam 	/*
138*25905Skarels 	 * Now that we have the logfile and the ACU open
139*25905Skarels 	 *  return to the real uid and gid.  These things will
140*25905Skarels 	 *  be closed on exit.  Swap real and effective uid's
141*25905Skarels 	 *  so we can get the original permissions back
142*25905Skarels 	 *  for removing the uucp lock.
143*25905Skarels 	 */
144*25905Skarels 	gid = getgid();
145*25905Skarels 	egid = getegid();
146*25905Skarels 	uid = getuid();
147*25905Skarels 	euid = geteuid();
148*25905Skarels 	setregid(egid, gid);
149*25905Skarels 	setreuid(euid, uid);
150*25905Skarels 
151*25905Skarels 	/*
1524004Ssam 	 * Hardwired connections require the
1534004Ssam 	 *  line speed set before they make any transmissions
1544004Ssam 	 *  (this is particularly true of things like a DF03-AC)
1554004Ssam 	 */
1564004Ssam 	if (HW)
1574004Ssam 		ttysetup(i);
1583898Ssam 	if (p = connect()) {
1593898Ssam 		printf("\07%s\n[EOT]\n", p);
160*25905Skarels 		setreuid(uid, euid);
161*25905Skarels 		setregid(gid, egid);
1623898Ssam 		delock(uucplock);
1633898Ssam 		exit(1);
1643898Ssam 	}
1654004Ssam 	if (!HW)
1664004Ssam 		ttysetup(i);
1675136Ssam cucommon:
1683696Sroot 	/*
1695136Ssam 	 * From here down the code is shared with
1705136Ssam 	 * the "cu" version of tip.
1713696Sroot 	 */
172*25905Skarels 
1734004Ssam 	ioctl(0, TIOCGETP, (char *)&defarg);
1744004Ssam 	ioctl(0, TIOCGETC, (char *)&defchars);
17512478Sroot 	ioctl(0, TIOCGLTC, (char *)&deflchars);
1764004Ssam 	ioctl(0, TIOCGETD, (char *)&odisc);
1773696Sroot 	arg = defarg;
1783696Sroot 	arg.sg_flags = ANYP | CBREAK;
1793696Sroot 	tchars = defchars;
1803696Sroot 	tchars.t_intrc = tchars.t_quitc = -1;
18112478Sroot 	ltchars = deflchars;
18212478Sroot 	ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
18312478Sroot 		= ltchars.t_lnextc = -1;
1843696Sroot 	raw();
1854004Ssam 
1863696Sroot 	pipe(fildes); pipe(repdes);
1873696Sroot 	signal(SIGALRM, timeout);
1883898Ssam 
1893898Ssam 	/*
1903898Ssam 	 * Everything's set up now:
19117236Shelge 	 *	connection established (hardwired or dialup)
1923898Ssam 	 *	line conditioned (baud rate, mode, etc.)
1933898Ssam 	 *	internal data structures (variables)
1943898Ssam 	 * so, fork one process for local side and one for remote.
1953898Ssam 	 */
1965136Ssam 	printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1973696Sroot 	if (pid = fork())
1983696Sroot 		tipin();
1993696Sroot 	else
2003696Sroot 		tipout();
2013696Sroot 	/*NOTREACHED*/
2023696Sroot }
2033696Sroot 
2043696Sroot cleanup()
2053696Sroot {
20613279Ssam 
207*25905Skarels 	if (uid != getuid()) {
208*25905Skarels 		setreuid(uid, euid);
209*25905Skarels 		setregid(gid, egid);
210*25905Skarels 	}
2113696Sroot 	delock(uucplock);
2123898Ssam 	if (odisc)
2133898Ssam 		ioctl(0, TIOCSETD, (char *)&odisc);
2143696Sroot 	exit(0);
2153696Sroot }
2163696Sroot 
2173696Sroot /*
2183696Sroot  * put the controlling keyboard into raw mode
2193696Sroot  */
2203696Sroot raw()
2213696Sroot {
22213279Ssam 
2233696Sroot 	ioctl(0, TIOCSETP, &arg);
2243696Sroot 	ioctl(0, TIOCSETC, &tchars);
22512478Sroot 	ioctl(0, TIOCSLTC, &ltchars);
2264004Ssam 	ioctl(0, TIOCSETD, (char *)&disc);
2273696Sroot }
2283696Sroot 
2293696Sroot 
2303696Sroot /*
2313696Sroot  * return keyboard to normal mode
2323696Sroot  */
2333696Sroot unraw()
2343696Sroot {
23513279Ssam 
2364004Ssam 	ioctl(0, TIOCSETD, (char *)&odisc);
2374004Ssam 	ioctl(0, TIOCSETP, (char *)&defarg);
2384004Ssam 	ioctl(0, TIOCSETC, (char *)&defchars);
23912478Sroot 	ioctl(0, TIOCSLTC, (char *)&deflchars);
2403696Sroot }
2413696Sroot 
24213279Ssam static	jmp_buf promptbuf;
24313279Ssam 
2443696Sroot /*
2453696Sroot  * Print string ``s'', then read a string
2463696Sroot  *  in from the terminal.  Handles signals & allows use of
2473696Sroot  *  normal erase and kill characters.
2483696Sroot  */
2493696Sroot prompt(s, p)
2503696Sroot 	char *s;
2513696Sroot 	register char *p;
2523696Sroot {
2533696Sroot 	register char *b = p;
25413279Ssam 	int (*oint)(), (*oquit)();
2553696Sroot 
2563696Sroot 	stoprompt = 0;
25713279Ssam 	oint = signal(SIGINT, intprompt);
25813279Ssam 	oint = signal(SIGQUIT, SIG_IGN);
2593696Sroot 	unraw();
2603696Sroot 	printf("%s", s);
26113279Ssam 	if (setjmp(promptbuf) == 0)
26213279Ssam 		while ((*p = getchar()) != EOF && *p != '\n')
26313279Ssam 			p++;
2643696Sroot 	*p = '\0';
26513279Ssam 
2663696Sroot 	raw();
26713279Ssam 	signal(SIGINT, oint);
26813279Ssam 	signal(SIGQUIT, oint);
26913279Ssam 	return (stoprompt || p == b);
2703696Sroot }
2713696Sroot 
2723696Sroot /*
2733696Sroot  * Interrupt service routine during prompting
2743696Sroot  */
2753696Sroot intprompt()
2763696Sroot {
27713279Ssam 
2783696Sroot 	signal(SIGINT, SIG_IGN);
2793696Sroot 	stoprompt = 1;
2803696Sroot 	printf("\r\n");
28113279Ssam 	longjmp(promptbuf, 1);
2823696Sroot }
2833696Sroot 
2843696Sroot /*
2853696Sroot  * ****TIPIN   TIPIN****
2863696Sroot  */
2873696Sroot tipin()
2883696Sroot {
2893696Sroot 	char gch, bol = 1;
2903696Sroot 
2913796Ssam 	/*
2923796Ssam 	 * Kinda klugey here...
2933796Ssam 	 *   check for scripting being turned on from the .tiprc file,
2943796Ssam 	 *   but be careful about just using setscript(), as we may
2953796Ssam 	 *   send a SIGEMT before tipout has a chance to set up catching
2963796Ssam 	 *   it; so wait a second, then setscript()
2973796Ssam 	 */
2983796Ssam 	if (boolean(value(SCRIPT))) {
2993796Ssam 		sleep(1);
3003796Ssam 		setscript();
3013796Ssam 	}
3023796Ssam 
3033696Sroot 	while (1) {
3043696Sroot 		gch = getchar()&0177;
3053696Sroot 		if ((gch == character(value(ESCAPE))) && bol) {
3063696Sroot 			if (!(gch = escape()))
3073696Sroot 				continue;
3085136Ssam 		} else if (!cumode && gch == character(value(RAISECHAR))) {
3093696Sroot 			boolean(value(RAISE)) = !boolean(value(RAISE));
3103696Sroot 			continue;
3113696Sroot 		} else if (gch == '\r') {
3123696Sroot 			bol = 1;
31313139Sralph 			pwrite(FD, &gch, 1);
31413139Sralph 			if (boolean(value(HALFDUPLEX)))
31513139Sralph 				printf("\r\n");
3163696Sroot 			continue;
3175136Ssam 		} else if (!cumode && gch == character(value(FORCE)))
3183696Sroot 			gch = getchar()&0177;
3193696Sroot 		bol = any(gch, value(EOL));
3203696Sroot 		if (boolean(value(RAISE)) && islower(gch))
32113139Sralph 			gch = toupper(gch);
32213139Sralph 		pwrite(FD, &gch, 1);
32313139Sralph 		if (boolean(value(HALFDUPLEX)))
32413139Sralph 			printf("%c", gch);
3253696Sroot 	}
3263696Sroot }
3273696Sroot 
3283696Sroot /*
3293696Sroot  * Escape handler --
3303696Sroot  *  called on recognition of ``escapec'' at the beginning of a line
3313696Sroot  */
3323696Sroot escape()
3333696Sroot {
3343696Sroot 	register char gch;
3353696Sroot 	register esctable_t *p;
3363696Sroot 	char c = character(value(ESCAPE));
3373696Sroot 	extern esctable_t etable[];
3383696Sroot 
3393696Sroot 	gch = (getchar()&0177);
3403696Sroot 	for (p = etable; p->e_char; p++)
3413696Sroot 		if (p->e_char == gch) {
3423696Sroot 			if ((p->e_flags&PRIV) && getuid())
3433696Sroot 				continue;
3443696Sroot 			printf("%s", ctrl(c));
3453696Sroot 			(*p->e_func)(gch);
34613279Ssam 			return (0);
3473696Sroot 		}
3484146Ssam 	/* ESCAPE ESCAPE forces ESCAPE */
3494146Ssam 	if (c != gch)
35013139Sralph 		pwrite(FD, &c, 1);
35113279Ssam 	return (gch);
3523696Sroot }
3533696Sroot 
3543696Sroot speed(n)
35513279Ssam 	int n;
3563696Sroot {
3573696Sroot 	register int *p;
3583696Sroot 
3593696Sroot 	for (p = bauds; *p != -1;  p++)
3603696Sroot 		if (*p == n)
36113279Ssam 			return (p - bauds);
36213279Ssam 	return (NULL);
3633696Sroot }
3643696Sroot 
3653696Sroot any(c, p)
3663696Sroot 	register char c, *p;
3673696Sroot {
36813279Ssam 	while (p && *p)
3693696Sroot 		if (*p++ == c)
37013279Ssam 			return (1);
37113279Ssam 	return (0);
3723696Sroot }
3733696Sroot 
3743696Sroot size(s)
3753696Sroot 	register char	*s;
3763696Sroot {
37713279Ssam 	register int i = 0;
3783696Sroot 
37913279Ssam 	while (s && *s++)
38013279Ssam 		i++;
38113279Ssam 	return (i);
3823696Sroot }
3833696Sroot 
3843696Sroot char *
3853696Sroot interp(s)
3863696Sroot 	register char *s;
3873696Sroot {
3883696Sroot 	static char buf[256];
3893696Sroot 	register char *p = buf, c, *q;
3903696Sroot 
3913696Sroot 	while (c = *s++) {
3923696Sroot 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
3933696Sroot 			if (*q++ == c) {
3943696Sroot 				*p++ = '\\'; *p++ = *q;
3953696Sroot 				goto next;
3963696Sroot 			}
3973696Sroot 		if (c < 040) {
3983696Sroot 			*p++ = '^'; *p++ = c + 'A'-1;
3993696Sroot 		} else if (c == 0177) {
4003696Sroot 			*p++ = '^'; *p++ = '?';
4013696Sroot 		} else
4023696Sroot 			*p++ = c;
4033696Sroot 	next:
4043696Sroot 		;
4053696Sroot 	}
4063696Sroot 	*p = '\0';
40713279Ssam 	return (buf);
4083696Sroot }
4093696Sroot 
4103696Sroot char *
4113696Sroot ctrl(c)
4123696Sroot 	char c;
4133696Sroot {
4143696Sroot 	static char s[3];
4153696Sroot 
4163696Sroot 	if (c < 040 || c == 0177) {
4173696Sroot 		s[0] = '^';
4183696Sroot 		s[1] = c == 0177 ? '?' : c+'A'-1;
4193696Sroot 		s[2] = '\0';
4203696Sroot 	} else {
4213696Sroot 		s[0] = c;
4223696Sroot 		s[1] = '\0';
4233696Sroot 	}
42413279Ssam 	return (s);
4253696Sroot }
4263696Sroot 
4273696Sroot /*
4283696Sroot  * Help command
4293696Sroot  */
4303696Sroot help(c)
4313696Sroot 	char c;
4323696Sroot {
4333696Sroot 	register esctable_t *p;
4343696Sroot 	extern esctable_t etable[];
4353696Sroot 
4363696Sroot 	printf("%c\r\n", c);
4373696Sroot 	for (p = etable; p->e_char; p++) {
4383696Sroot 		if ((p->e_flags&PRIV) && getuid())
4393696Sroot 			continue;
4403696Sroot 		printf("%2s", ctrl(character(value(ESCAPE))));
4413696Sroot 		printf("%-2s %c   %s\r\n", ctrl(p->e_char),
4423696Sroot 			p->e_flags&EXP ? '*': ' ', p->e_help);
4433696Sroot 	}
4443696Sroot }
4454004Ssam 
4464004Ssam /*
4474004Ssam  * Set up the "remote" tty's state
4484004Ssam  */
4494004Ssam ttysetup(speed)
45013279Ssam 	int speed;
4514004Ssam {
4524004Ssam 	unsigned bits = LDECCTQ;
4534004Ssam 
4544004Ssam 	arg.sg_ispeed = arg.sg_ospeed = speed;
45513279Ssam 	arg.sg_flags = RAW;
45613139Sralph 	if (boolean(value(TAND)))
45713279Ssam 		arg.sg_flags |= TANDEM;
4584004Ssam 	ioctl(FD, TIOCSETP, (char *)&arg);
4594004Ssam 	ioctl(FD, TIOCLBIS, (char *)&bits);
4604004Ssam }
4615257Sshannon 
4625257Sshannon /*
4635257Sshannon  * Return "simple" name from a file name,
4645257Sshannon  * strip leading directories.
4655257Sshannon  */
4665257Sshannon char *
4675257Sshannon sname(s)
4685257Sshannon 	register char *s;
4695257Sshannon {
4705257Sshannon 	register char *p = s;
4715257Sshannon 
4725257Sshannon 	while (*s)
4735257Sshannon 		if (*s++ == '/')
4745257Sshannon 			p = s;
4755257Sshannon 	return (p);
4765257Sshannon }
47713139Sralph 
47813139Sralph static char partab[0200];
47913139Sralph 
48013139Sralph /*
48113279Ssam  * Do a write to the remote machine with the correct parity.
48213279Ssam  * We are doing 8 bit wide output, so we just generate a character
48313139Sralph  * with the right parity and output it.
48413139Sralph  */
48513139Sralph pwrite(fd, buf, n)
48613139Sralph 	int fd;
48713139Sralph 	char *buf;
48813139Sralph 	register int n;
48913139Sralph {
49013139Sralph 	register int i;
49113279Ssam 	register char *bp;
49215188Ssam 	extern int errno;
49313139Sralph 
49413279Ssam 	bp = buf;
49513279Ssam 	for (i = 0; i < n; i++) {
49613139Sralph 		*bp = partab[(*bp) & 0177];
49713279Ssam 		bp++;
49813139Sralph 	}
49915188Ssam 	if (write(fd, buf, n) < 0) {
50015188Ssam 		if (errno == EIO)
50115188Ssam 			abort("Lost carrier.");
50215188Ssam 		/* this is questionable */
50315188Ssam 		perror("write");
50415188Ssam 	}
50513139Sralph }
50613139Sralph 
50713139Sralph /*
50813279Ssam  * Build a parity table with appropriate high-order bit.
50913139Sralph  */
51013430Ssam setparity(defparity)
51113430Ssam 	char *defparity;
51213139Sralph {
51313279Ssam 	register int i;
51413139Sralph 	char *parity;
51513279Ssam 	extern char evenpartab[];
51613139Sralph 
51713139Sralph 	if (value(PARITY) == NOSTR)
51813430Ssam 		value(PARITY) = defparity;
51913139Sralph 	parity = value(PARITY);
52013139Sralph 	for (i = 0; i < 0200; i++)
52113279Ssam 		partab[i] = evenpartab[i];
52213279Ssam 	if (equal(parity, "even"))
52313279Ssam 		return;
52413139Sralph 	if (equal(parity, "odd")) {
52513139Sralph 		for (i = 0; i < 0200; i++)
52613139Sralph 			partab[i] ^= 0200;	/* reverse bit 7 */
52713279Ssam 		return;
52813139Sralph 	}
52913279Ssam 	if (equal(parity, "none") || equal(parity, "zero")) {
53013139Sralph 		for (i = 0; i < 0200; i++)
53113139Sralph 			partab[i] &= ~0200;	/* turn off bit 7 */
53213279Ssam 		return;
53313139Sralph 	}
53413279Ssam 	if (equal(parity, "one")) {
53513139Sralph 		for (i = 0; i < 0200; i++)
53613139Sralph 			partab[i] |= 0200;	/* turn on bit 7 */
53713279Ssam 		return;
53813139Sralph 	}
53913279Ssam 	fprintf(stderr, "%s: unknown parity value\n", PA);
54013279Ssam 	fflush(stderr);
54113139Sralph }
542