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