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