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