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