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