Home | History | Annotate | Line # | Download | only in tip
tip.c revision 1.47
      1 /*	$NetBSD: tip.c,v 1.47 2007/02/16 20:25:33 hubertf 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 <ctype.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.47 2007/02/16 20:25:33 hubertf 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 		(void)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 			(void)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 		(void)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 = tip_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 	(void)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 	(void)pipe(attndes);
    221 	(void)pipe(fildes);
    222 	(void)pipe(repdes);
    223 	(void)signal(SIGALRM, alrmtimeout);
    224 
    225 	/*
    226 	 * Everything's set up now:
    227 	 *	connection established (hardwired or dialup)
    228 	 *	line conditioned (baud rate, mode, etc.)
    229 	 *	internal data structures (variables)
    230 	 * so, fork one process for local side and one for remote.
    231 	 */
    232 	(void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
    233 	switch (pid = fork()) {
    234 	default:
    235 		tipin();
    236 		break;
    237 	case 0:
    238 		tipout();
    239 		break;
    240 	case -1:
    241 		err(1, "can't fork");
    242 	}
    243 	/*NOTREACHED*/
    244 	exit(0);	/* XXX: pacify gcc */
    245 }
    246 
    247 void
    248 tipusage(void)
    249 {
    250 	(void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n",
    251 	    getprogname());
    252 	(void)fprintf(stderr, "       %s [-v] [-speed] phone-number\n",
    253 	    getprogname());
    254 	exit(1);
    255 }
    256 
    257 void
    258 /*ARGSUSED*/
    259 cleanup(int dummy __unused)
    260 {
    261 
    262 	if (odisc)
    263 		(void)ioctl(0, TIOCSETD, &odisc);
    264 	exit(0);
    265 }
    266 
    267 /*
    268  * put the controlling keyboard into raw mode
    269  */
    270 void
    271 raw(void)
    272 {
    273 
    274 	(void)tcsetattr(0, TCSADRAIN, &term);
    275 }
    276 
    277 
    278 /*
    279  * return keyboard to normal mode
    280  */
    281 void
    282 unraw(void)
    283 {
    284 
    285 	(void)tcsetattr(0, TCSADRAIN, &defterm);
    286 }
    287 
    288 static	jmp_buf promptbuf;
    289 
    290 /*
    291  * Print string ``s'', then read a string
    292  *  in from the terminal.  Handles signals & allows use of
    293  *  normal erase and kill characters.
    294  */
    295 int
    296 prompt(const char *s, char *volatile p, size_t l)
    297 {
    298 	int c;
    299 	char *b = p;
    300 	sig_t oint, oquit;
    301 
    302 	stoprompt = 0;
    303 	oint = signal(SIGINT, intprompt);
    304 	oquit = signal(SIGQUIT, SIG_IGN);
    305 	unraw();
    306 	(void)printf("%s", s);
    307 	if (setjmp(promptbuf) == 0)
    308 		while ((c = getchar()) != -1 && (*p = c) != '\n' &&
    309 		    b + l > p)
    310 			p++;
    311 	*p = '\0';
    312 
    313 	raw();
    314 	(void)signal(SIGINT, oint);
    315 	(void)signal(SIGQUIT, oquit);
    316 	return (stoprompt || p == b);
    317 }
    318 
    319 /*
    320  * Interrupt service routine during prompting
    321  */
    322 void
    323 /*ARGSUSED*/
    324 intprompt(int dummy __unused)
    325 {
    326 
    327 	(void)signal(SIGINT, SIG_IGN);
    328 	stoprompt = 1;
    329 	(void)printf("\r\n");
    330 	longjmp(promptbuf, 1);
    331 }
    332 
    333 /*
    334  * ****TIPIN   TIPIN****
    335  */
    336 void
    337 tipin(void)
    338 {
    339 	char gch, bol = 1;
    340 
    341 	/*
    342 	 * Kinda klugey here...
    343 	 *   check for scripting being turned on from the .tiprc file,
    344 	 *   but be careful about just using setscript(), as we may
    345 	 *   send a SIGEMT before tipout has a chance to set up catching
    346 	 *   it; so wait a second, then setscript()
    347 	 */
    348 	if (boolean(value(SCRIPT))) {
    349 		(void)sleep(1);
    350 		setscript();
    351 	}
    352 
    353 	for (;;) {
    354 		gch = getchar()&STRIP_PAR;
    355 		if ((gch == character(value(ESCAPE))) && bol) {
    356 			if (!(gch = escape()))
    357 				continue;
    358 		} else if (!cumode &&
    359 		    gch && gch == character(value(RAISECHAR))) {
    360 			setboolean(value(RAISE), !boolean(value(RAISE)));
    361 			continue;
    362 		} else if (gch == '\r') {
    363 			bol = 1;
    364 			xpwrite(FD, &gch, 1);
    365 			if (boolean(value(HALFDUPLEX)))
    366 				(void)printf("\r\n");
    367 			continue;
    368 		} else if (!cumode && gch && gch == character(value(FORCE)))
    369 			gch = getchar()&STRIP_PAR;
    370 		bol = any(gch, value(EOL));
    371 		if (boolean(value(RAISE)) && islower((unsigned char)gch))
    372 			gch = toupper((unsigned char)gch);
    373 		xpwrite(FD, &gch, 1);
    374 		if (boolean(value(HALFDUPLEX)))
    375 			(void)printf("%c", gch);
    376 	}
    377 }
    378 
    379 /*
    380  * Escape handler --
    381  *  called on recognition of ``escapec'' at the beginning of a line
    382  */
    383 int
    384 escape(void)
    385 {
    386 	char gch;
    387 	esctable_t *p;
    388 	char c = character(value(ESCAPE));
    389 
    390 	gch = (getchar()&STRIP_PAR);
    391 	for (p = etable; p->e_char; p++)
    392 		if (p->e_char == gch) {
    393 			if ((p->e_flags&PRIV) && uid)
    394 				continue;
    395 			(void)printf("%s", ctrl(c));
    396 			(*p->e_func)(gch);
    397 			return (0);
    398 		}
    399 	/* ESCAPE ESCAPE forces ESCAPE */
    400 	if (c != gch)
    401 		xpwrite(FD, &c, 1);
    402 	return (gch);
    403 }
    404 
    405 int
    406 any(char c, const char *p)
    407 {
    408 
    409 	while (p && *p)
    410 		if (*p++ == c)
    411 			return (1);
    412 	return (0);
    413 }
    414 
    415 char *
    416 interp(const char *s)
    417 {
    418 	static char buf[256];
    419 	char *p = buf, c;
    420 	const char *q;
    421 
    422 	while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
    423 		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
    424 			if (*q++ == c) {
    425 				*p++ = '\\'; *p++ = *q;
    426 				goto next;
    427 			}
    428 		if (c < 040) {
    429 			*p++ = '^'; *p++ = c + 'A'-1;
    430 		} else if (c == 0177) {
    431 			*p++ = '^'; *p++ = '?';
    432 		} else
    433 			*p++ = c;
    434 	next:
    435 		;
    436 	}
    437 	*p = '\0';
    438 	return (buf);
    439 }
    440 
    441 char *
    442 ctrl(char c)
    443 {
    444 	static char s[3];
    445 
    446 	if (c < 040 || c == 0177) {
    447 		s[0] = '^';
    448 		s[1] = c == 0177 ? '?' : c+'A'-1;
    449 		s[2] = '\0';
    450 	} else {
    451 		s[0] = c;
    452 		s[1] = '\0';
    453 	}
    454 	return (s);
    455 }
    456 
    457 /*
    458  * Help command
    459  */
    460 void
    461 help(char c)
    462 {
    463 	esctable_t *p;
    464 
    465 	(void)printf("%c\r\n", c);
    466 	for (p = etable; p->e_char; p++) {
    467 		if ((p->e_flags&PRIV) && uid)
    468 			continue;
    469 		(void)printf("%2s", ctrl(character(value(ESCAPE))));
    470 		(void)printf("%-2s %c   %s\r\n", ctrl(p->e_char),
    471 			p->e_flags&EXP ? '*': ' ', p->e_help);
    472 	}
    473 }
    474 
    475 /*
    476  * Set up the "remote" tty's state
    477  */
    478 int
    479 ttysetup(speed_t spd)
    480 {
    481 	struct termios	cntrl;
    482 
    483 	(void)tcgetattr(FD, &cntrl);
    484 	(void)cfsetospeed(&cntrl, spd);
    485 	(void)cfsetispeed(&cntrl, spd);
    486 	cntrl.c_cflag &= ~(CSIZE|PARENB);
    487 	cntrl.c_cflag |= CS8;
    488 	if (DC)
    489 		cntrl.c_cflag |= CLOCAL;
    490 	if (boolean(value(HARDWAREFLOW)))
    491 		cntrl.c_cflag |= CRTSCTS;
    492 	cntrl.c_iflag &= ~(ISTRIP|ICRNL);
    493 	cntrl.c_oflag &= ~OPOST;
    494 	cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
    495 	cntrl.c_cc[VMIN] = 1;
    496 	cntrl.c_cc[VTIME] = 0;
    497 	if (boolean(value(TAND)))
    498 		cntrl.c_iflag |= IXOFF;
    499 	return tcsetattr(FD, TCSAFLUSH, &cntrl);
    500 }
    501 
    502 static char partab[0200];
    503 
    504 /*
    505  * Do a write to the remote machine with the correct parity.
    506  * We are doing 8 bit wide output, so we just generate a character
    507  * with the right parity and output it.
    508  */
    509 void
    510 xpwrite(int fd, char *buf, size_t n)
    511 {
    512 	int i;
    513 	char *bp;
    514 
    515 	bp = buf;
    516 	if (bits8 == 0)
    517 		for (i = 0; i < n; i++) {
    518 			*bp = partab[(*bp) & 0177];
    519 			bp++;
    520 		}
    521 	if (write(fd, buf, n) < 0) {
    522 		if (errno == EIO)
    523 			tipabort("Lost carrier.");
    524 		/* this is questionable */
    525 		warn("write");
    526 	}
    527 }
    528 
    529 /*
    530  * Build a parity table with appropriate high-order bit.
    531  */
    532 void
    533 setparity(const char *defparity)
    534 {
    535 	int i, flip, clr, set;
    536 	const char *parity;
    537 	static char *curpar;
    538 
    539 	if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') {
    540 		if (curpar != NULL)
    541 			free(curpar);
    542 		value(PARITY) = curpar = strdup(defparity);
    543 	}
    544 	parity = value(PARITY);
    545 	if (equal(parity, "none")) {
    546 		bits8 = 1;
    547 		return;
    548 	}
    549 	bits8 = 0;
    550 	flip = 0;
    551 	clr = 0377;
    552 	set = 0;
    553 	if (equal(parity, "odd"))
    554 		flip = 0200;			/* reverse bit 7 */
    555 	else if (equal(parity, "zero"))
    556 		clr = 0177;			/* turn off bit 7 */
    557 	else if (equal(parity, "one"))
    558 		set = 0200;			/* turn on bit 7 */
    559 	else if (!equal(parity, "even")) {
    560 		(void)fprintf(stderr, "%s: unknown parity value\r\n", parity);
    561 		(void)fflush(stderr);
    562 	}
    563 	for (i = 0; i < 0200; i++)
    564 		partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
    565 }
    566