Home | History | Annotate | Line # | Download | only in tip
tip.c revision 1.14
      1 /*	$NetBSD: tip.c,v 1.14 1997/05/14 00:20:05 mellon Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1983, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)tip.c	8.1 (Berkeley) 6/6/93";
     45 #endif
     46 static char rcsid[] = "$NetBSD: tip.c,v 1.14 1997/05/14 00:20:05 mellon Exp $";
     47 #endif /* not lint */
     48 
     49 /*
     50  * tip - UNIX link to other systems
     51  *  tip [-v] [-speed] system-name
     52  * or
     53  *  cu phone-number [-s speed] [-l line] [-a acu]
     54  */
     55 #include "tip.h"
     56 #include "pathnames.h"
     57 
     58 /*
     59  * Baud rate mapping table
     60  */
     61 int rates[] = {
     62 	0, 50, 75, 110, 134, 150, 200, 300, 600,
     63 	1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
     64 };
     65 
     66 int	disc = TTYDISC;		/* tip normally runs this way */
     67 void	intprompt();
     68 void	timeout();
     69 void	cleanup();
     70 char	*sname();
     71 char	PNbuf[256];			/* This limits the size of a number */
     72 
     73 main(argc, argv)
     74 	char *argv[];
     75 {
     76 	char *system = NOSTR;
     77 	register int i;
     78 	register char *p;
     79 	char sbuf[12];
     80 
     81 	gid = getgid();
     82 	egid = getegid();
     83 	uid = getuid();
     84 	euid = geteuid();
     85 	if (equal(sname(argv[0]), "cu")) {
     86 		cumode = 1;
     87 		cumain(argc, argv);
     88 		goto cucommon;
     89 	}
     90 
     91 	if (argc > 4) {
     92 		fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
     93 		exit(1);
     94 	}
     95 	if (!isatty(0)) {
     96 		fprintf(stderr, "tip: must be interactive\n");
     97 		exit(1);
     98 	}
     99 
    100 	for (; argc > 1; argv++, argc--) {
    101 		if (argv[1][0] != '-')
    102 			system = argv[1];
    103 		else switch (argv[1][1]) {
    104 
    105 		case 'v':
    106 			vflag++;
    107 			break;
    108 
    109 		case '0': case '1': case '2': case '3': case '4':
    110 		case '5': case '6': case '7': case '8': case '9':
    111 			BR = atoi(&argv[1][1]);
    112 			break;
    113 
    114 		default:
    115 			fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
    116 			break;
    117 		}
    118 	}
    119 
    120 	if (system == NOSTR)
    121 		goto notnumber;
    122 	if (isalpha(*system))
    123 		goto notnumber;
    124 	/*
    125 	 * System name is really a phone number...
    126 	 * Copy the number then stomp on the original (in case the number
    127 	 *	is private, we don't want 'ps' or 'w' to find it).
    128 	 */
    129 	if (strlen(system) > sizeof PNbuf - 1) {
    130 		fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
    131 			sizeof PNbuf - 1);
    132 		exit(1);
    133 	}
    134 	strncpy( PNbuf, system, sizeof PNbuf - 1 );
    135 	for (p = system; *p; p++)
    136 		*p = '\0';
    137 	PN = PNbuf;
    138 	(void)snprintf(sbuf, sizeof sbuf, "tip%d", BR);
    139 	system = sbuf;
    140 
    141 notnumber:
    142 	(void)signal(SIGINT, cleanup);
    143 	(void)signal(SIGQUIT, cleanup);
    144 	(void)signal(SIGHUP, cleanup);
    145 	(void)signal(SIGTERM, cleanup);
    146 
    147 	if ((i = hunt(system)) == 0) {
    148 		printf("all ports busy\n");
    149 		exit(3);
    150 	}
    151 	if (i == -1) {
    152 		printf("link down\n");
    153 		(void)uu_unlock(uucplock);
    154 		exit(3);
    155 	}
    156 	setbuf(stdout, NULL);
    157 	loginit();
    158 
    159 	/*
    160 	 * Now that we have the logfile and the ACU open
    161 	 *  return to the real uid and gid.  These things will
    162 	 *  be closed on exit.  Swap real and effective uid's
    163 	 *  so we can get the original permissions back
    164 	 *  for removing the uucp lock.
    165 	 */
    166 	user_uid();
    167 
    168 	/*
    169 	 * Kludge, their's no easy way to get the initialization
    170 	 *   in the right order, so force it here
    171 	 */
    172 	if ((PH = getenv("PHONES")) == NOSTR)
    173 		PH = _PATH_PHONES;
    174 	vinit();				/* init variables */
    175 	setparity("even");			/* set the parity table */
    176 	if ((i = speed(number(value(BAUDRATE)))) == 0) {
    177 		printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
    178 		daemon_uid();
    179 		(void)uu_unlock(uucplock);
    180 		exit(3);
    181 	}
    182 
    183 	/*
    184 	 * Hardwired connections require the
    185 	 *  line speed set before they make any transmissions
    186 	 *  (this is particularly true of things like a DF03-AC)
    187 	 */
    188 	if (HW)
    189 		ttysetup(i);
    190 	if (p = connect()) {
    191 		printf("\07%s\n[EOT]\n", p);
    192 		daemon_uid();
    193 		(void)uu_unlock(uucplock);
    194 		exit(1);
    195 	}
    196 	if (!HW)
    197 		ttysetup(i);
    198 cucommon:
    199 	/*
    200 	 * From here down the code is shared with
    201 	 * the "cu" version of tip.
    202 	 */
    203 
    204 	tcgetattr(0, &defterm);
    205 	term = defterm;
    206 	term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
    207 	term.c_iflag &= ~(INPCK|ICRNL);
    208 	term.c_oflag &= ~OPOST;
    209 	term.c_cc[VMIN] = 1;
    210 	term.c_cc[VTIME] = 0;
    211 	defchars = term;
    212 	term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
    213 		term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
    214 	 	term.c_cc[VLNEXT] = _POSIX_VDISABLE;
    215 	raw();
    216 
    217 	pipe(fildes); pipe(repdes);
    218 	(void)signal(SIGALRM, timeout);
    219 
    220 	/*
    221 	 * Everything's set up now:
    222 	 *	connection established (hardwired or dialup)
    223 	 *	line conditioned (baud rate, mode, etc.)
    224 	 *	internal data structures (variables)
    225 	 * so, fork one process for local side and one for remote.
    226 	 */
    227 	printf(cumode ? "Connected\r\n" : "\07connected\r\n");
    228 	if (pid = fork())
    229 		tipin();
    230 	else
    231 		tipout();
    232 	/*NOTREACHED*/
    233 }
    234 
    235 void
    236 cleanup()
    237 {
    238 
    239 	daemon_uid();
    240 	(void)uu_unlock(uucplock);
    241 	if (odisc)
    242 		ioctl(0, TIOCSETD, (char *)&odisc);
    243 	exit(0);
    244 }
    245 
    246 /*
    247  * Muck with user ID's.  We are setuid to the owner of the lock
    248  * directory when we start.  user_uid() reverses real and effective
    249  * ID's after startup, to run with the user's permissions.
    250  * daemon_uid() switches back to the privileged uid for unlocking.
    251  * Finally, to avoid running a shell with the wrong real uid,
    252  * shell_uid() sets real and effective uid's to the user's real ID.
    253  */
    254 static int uidswapped;
    255 
    256 user_uid()
    257 {
    258 	if (uidswapped == 0) {
    259 		seteuid(uid);
    260 		uidswapped = 1;
    261 	}
    262 }
    263 
    264 daemon_uid()
    265 {
    266 
    267 	if (uidswapped) {
    268 		seteuid(euid);
    269 		uidswapped = 0;
    270 	}
    271 }
    272 
    273 shell_uid()
    274 {
    275 	seteuid(uid);
    276 }
    277 
    278 /*
    279  * put the controlling keyboard into raw mode
    280  */
    281 raw()
    282 {
    283 	tcsetattr(0, TCSADRAIN, &term);
    284 }
    285 
    286 
    287 /*
    288  * return keyboard to normal mode
    289  */
    290 unraw()
    291 {
    292 	tcsetattr(0, TCSADRAIN, &defterm);
    293 }
    294 
    295 static	jmp_buf promptbuf;
    296 
    297 /*
    298  * Print string ``s'', then read a string
    299  *  in from the terminal.  Handles signals & allows use of
    300  *  normal erase and kill characters.
    301  */
    302 prompt(s, p)
    303 	char *s;
    304 	register char *p;
    305 {
    306 	register int c;
    307 	register char *b = p;
    308 	sig_t oint, oquit;
    309 
    310 	stoprompt = 0;
    311 	oint = signal(SIGINT, intprompt);
    312 	oquit = signal(SIGQUIT, SIG_IGN);
    313 	unraw();
    314 	printf("%s", s);
    315 	if (setjmp(promptbuf) == 0)
    316 		while ((c = getchar()) != -1 && (*p = c) != '\n')
    317 			p++;
    318 	*p = '\0';
    319 
    320 	raw();
    321 	(void)signal(SIGINT, oint);
    322 	(void)signal(SIGQUIT, oquit);
    323 	return (stoprompt || p == b);
    324 }
    325 
    326 /*
    327  * Interrupt service routine during prompting
    328  */
    329 void
    330 intprompt()
    331 {
    332 
    333 	(void)signal(SIGINT, SIG_IGN);
    334 	stoprompt = 1;
    335 	printf("\r\n");
    336 	longjmp(promptbuf, 1);
    337 }
    338 
    339 /*
    340  * ****TIPIN   TIPIN****
    341  */
    342 tipin()
    343 {
    344 	char gch, bol = 1;
    345 
    346 	/*
    347 	 * Kinda klugey here...
    348 	 *   check for scripting being turned on from the .tiprc file,
    349 	 *   but be careful about just using setscript(), as we may
    350 	 *   send a SIGEMT before tipout has a chance to set up catching
    351 	 *   it; so wait a second, then setscript()
    352 	 */
    353 	if (boolean(value(SCRIPT))) {
    354 		sleep(1);
    355 		setscript();
    356 	}
    357 
    358 	while (1) {
    359 		gch = getchar()&STRIP_PAR;
    360 		if ((gch == character(value(ESCAPE))) && bol) {
    361 			if (!(gch = escape()))
    362 				continue;
    363 		} else if (!cumode && gch == character(value(RAISECHAR))) {
    364 			setboolean(value(RAISE), !boolean(value(RAISE)));
    365 			continue;
    366 		} else if (gch == '\r') {
    367 			bol = 1;
    368 			pwrite(FD, &gch, 1);
    369 			if (boolean(value(HALFDUPLEX)))
    370 				printf("\r\n");
    371 			continue;
    372 		} else if (!cumode && gch == character(value(FORCE)))
    373 			gch = getchar()&STRIP_PAR;
    374 		bol = any(gch, value(EOL));
    375 		if (boolean(value(RAISE)) && islower(gch))
    376 			gch = toupper(gch);
    377 		pwrite(FD, &gch, 1);
    378 		if (boolean(value(HALFDUPLEX)))
    379 			printf("%c", gch);
    380 	}
    381 }
    382 
    383 extern esctable_t etable[];
    384 
    385 /*
    386  * Escape handler --
    387  *  called on recognition of ``escapec'' at the beginning of a line
    388  */
    389 escape()
    390 {
    391 	register char gch;
    392 	register esctable_t *p;
    393 	char c = character(value(ESCAPE));
    394 
    395 	gch = (getchar()&STRIP_PAR);
    396 	for (p = etable; p->e_char; p++)
    397 		if (p->e_char == gch) {
    398 			if ((p->e_flags&PRIV) && uid)
    399 				continue;
    400 			printf("%s", ctrl(c));
    401 			(*p->e_func)(gch);
    402 			return (0);
    403 		}
    404 	/* ESCAPE ESCAPE forces ESCAPE */
    405 	if (c != gch)
    406 		pwrite(FD, &c, 1);
    407 	return (gch);
    408 }
    409 
    410 speed(n)
    411 	int n;
    412 {
    413 	register int *p;
    414 
    415 	for (p = rates; *p != -1;  p++)
    416 		if (*p == n)
    417 			return n;
    418 	return 0;
    419 }
    420 
    421 any(c, p)
    422 	register char c, *p;
    423 {
    424 	while (p && *p)
    425 		if (*p++ == c)
    426 			return (1);
    427 	return (0);
    428 }
    429 
    430 size(s)
    431 	register char	*s;
    432 {
    433 	register int i = 0;
    434 
    435 	while (s && *s++)
    436 		i++;
    437 	return (i);
    438 }
    439 
    440 char *
    441 interp(s)
    442 	register char *s;
    443 {
    444 	static char buf[256];
    445 	register char *p = buf, c, *q;
    446 
    447 	while (c = *s++) {
    448 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
    449 			if (*q++ == c) {
    450 				*p++ = '\\'; *p++ = *q;
    451 				goto next;
    452 			}
    453 		if (c < 040) {
    454 			*p++ = '^'; *p++ = c + 'A'-1;
    455 		} else if (c == 0177) {
    456 			*p++ = '^'; *p++ = '?';
    457 		} else
    458 			*p++ = c;
    459 	next:
    460 		;
    461 	}
    462 	*p = '\0';
    463 	return (buf);
    464 }
    465 
    466 char *
    467 ctrl(c)
    468 	char c;
    469 {
    470 	static char s[3];
    471 
    472 	if (c < 040 || c == 0177) {
    473 		s[0] = '^';
    474 		s[1] = c == 0177 ? '?' : c+'A'-1;
    475 		s[2] = '\0';
    476 	} else {
    477 		s[0] = c;
    478 		s[1] = '\0';
    479 	}
    480 	return (s);
    481 }
    482 
    483 /*
    484  * Help command
    485  */
    486 help(c)
    487 	char c;
    488 {
    489 	register esctable_t *p;
    490 
    491 	printf("%c\r\n", c);
    492 	for (p = etable; p->e_char; p++) {
    493 		if ((p->e_flags&PRIV) && uid)
    494 			continue;
    495 		printf("%2s", ctrl(character(value(ESCAPE))));
    496 		printf("%-2s %c   %s\r\n", ctrl(p->e_char),
    497 			p->e_flags&EXP ? '*': ' ', p->e_help);
    498 	}
    499 }
    500 
    501 /*
    502  * Set up the "remote" tty's state
    503  */
    504 ttysetup(speed)
    505 	int speed;
    506 {
    507 	struct termios	cntrl;
    508 
    509 	tcgetattr(FD, &cntrl);
    510 	cfsetospeed(&cntrl, speed);
    511 	cfsetispeed(&cntrl, speed);
    512 	cntrl.c_cflag &= ~(CSIZE|PARENB);
    513 	cntrl.c_cflag |= CS8;
    514 	if (DC)
    515 		cntrl.c_cflag |= CLOCAL;
    516 	cntrl.c_iflag &= ~(ISTRIP|ICRNL);
    517 	cntrl.c_oflag &= ~OPOST;
    518 	cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
    519 	cntrl.c_cc[VMIN] = 1;
    520 	cntrl.c_cc[VTIME] = 0;
    521 	if (boolean(value(TAND)))
    522 		cntrl.c_iflag |= IXOFF;
    523 	tcsetattr(FD, TCSAFLUSH, &cntrl);
    524 }
    525 
    526 /*
    527  * Return "simple" name from a file name,
    528  * strip leading directories.
    529  */
    530 char *
    531 sname(s)
    532 	register char *s;
    533 {
    534 	register char *p = s;
    535 
    536 	while (*s)
    537 		if (*s++ == '/')
    538 			p = s;
    539 	return (p);
    540 }
    541 
    542 static char partab[0200];
    543 
    544 /*
    545  * Do a write to the remote machine with the correct parity.
    546  * We are doing 8 bit wide output, so we just generate a character
    547  * with the right parity and output it.
    548  */
    549 pwrite(fd, buf, n)
    550 	int fd;
    551 	char *buf;
    552 	register int n;
    553 {
    554 	register int i;
    555 	register char *bp;
    556 	extern int errno;
    557 
    558 	bp = buf;
    559 	if (bits8 == 0)
    560 		for (i = 0; i < n; i++) {
    561 			*bp = partab[(*bp) & 0177];
    562 			bp++;
    563 		}
    564 	if (write(fd, buf, n) < 0) {
    565 		if (errno == EIO)
    566 			tipabort("Lost carrier.");
    567 		/* this is questionable */
    568 		perror("write");
    569 	}
    570 }
    571 
    572 /*
    573  * Build a parity table with appropriate high-order bit.
    574  */
    575 setparity(defparity)
    576 	char *defparity;
    577 {
    578 	register int i, flip, clr, set;
    579 	char *parity;
    580 	extern unsigned char evenpartab[];
    581 
    582 	if (value(PARITY) == NOSTR)
    583 		value(PARITY) = defparity;
    584 	parity = value(PARITY);
    585 	if (equal(parity, "none")) {
    586 		bits8 = 1;
    587 		return;
    588 	}
    589 	bits8 = 0;
    590 	flip = 0;
    591 	clr = 0377;
    592 	set = 0;
    593 	if (equal(parity, "odd"))
    594 		flip = 0200;			/* reverse bit 7 */
    595 	else if (equal(parity, "zero"))
    596 		clr = 0177;			/* turn off bit 7 */
    597 	else if (equal(parity, "one"))
    598 		set = 0200;			/* turn on bit 7 */
    599 	else if (!equal(parity, "even")) {
    600 		(void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
    601 		(void) fflush(stderr);
    602 	}
    603 	for (i = 0; i < 0200; i++)
    604 		partab[i] = (evenpartab[i] ^ flip | set) & clr;
    605 }
    606