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