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