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