tip.c revision 1.5 1 /*
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)tip.c 5.15 (Berkeley) 2/4/91";*/
42 static char rcsid[] = "$Id: tip.c,v 1.5 1994/04/20 17:21:28 pk Exp $";
43 #endif /* not lint */
44
45 /*
46 * tip - UNIX link to other systems
47 * tip [-v] [-speed] system-name
48 * or
49 * cu phone-number [-s speed] [-l line] [-a acu]
50 */
51 #include "tip.h"
52 #include "pathnames.h"
53
54 /*
55 * Baud rate mapping table
56 */
57 int bauds[] = {
58 0, 50, 75, 110, 134, 150, 200, 300, 600,
59 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
60 };
61
62 int disc = OTTYDISC; /* tip normally runs this way */
63 void intprompt();
64 void timeout();
65 void cleanup();
66 char *sname();
67 char PNbuf[256]; /* This limits the size of a number */
68
69 main(argc, argv)
70 char *argv[];
71 {
72 char *system = NOSTR;
73 register int i;
74 register char *p;
75 char sbuf[12];
76
77 gid = getgid();
78 egid = getegid();
79 uid = getuid();
80 euid = geteuid();
81 if (equal(sname(argv[0]), "cu")) {
82 cumode = 1;
83 cumain(argc, argv);
84 goto cucommon;
85 }
86
87 if (argc > 4) {
88 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
89 exit(1);
90 }
91 if (!isatty(0)) {
92 fprintf(stderr, "tip: must be interactive\n");
93 exit(1);
94 }
95
96 for (; argc > 1; argv++, argc--) {
97 if (argv[1][0] != '-')
98 system = argv[1];
99 else switch (argv[1][1]) {
100
101 case 'v':
102 vflag++;
103 break;
104
105 case '0': case '1': case '2': case '3': case '4':
106 case '5': case '6': case '7': case '8': case '9':
107 BR = atoi(&argv[1][1]);
108 break;
109
110 default:
111 fprintf(stderr, "tip: %s, unknown option\n", argv[1]);
112 break;
113 }
114 }
115
116 if (system == NOSTR)
117 goto notnumber;
118 if (isalpha(*system))
119 goto notnumber;
120 /*
121 * System name is really a phone number...
122 * Copy the number then stomp on the original (in case the number
123 * is private, we don't want 'ps' or 'w' to find it).
124 */
125 if (strlen(system) > sizeof PNbuf - 1) {
126 fprintf(stderr, "tip: phone number too long (max = %d bytes)\n",
127 sizeof PNbuf - 1);
128 exit(1);
129 }
130 strncpy( PNbuf, system, sizeof PNbuf - 1 );
131 for (p = system; *p; p++)
132 *p = '\0';
133 PN = PNbuf;
134 (void)sprintf(sbuf, "tip%d", BR);
135 system = sbuf;
136
137 notnumber:
138 (void)signal(SIGINT, cleanup);
139 (void)signal(SIGQUIT, cleanup);
140 (void)signal(SIGHUP, cleanup);
141 (void)signal(SIGTERM, cleanup);
142
143 if ((i = hunt(system)) == 0) {
144 printf("all ports busy\n");
145 exit(3);
146 }
147 if (i == -1) {
148 printf("link down\n");
149 (void)uu_unlock(uucplock);
150 exit(3);
151 }
152 setbuf(stdout, NULL);
153 loginit();
154
155 /*
156 * Now that we have the logfile and the ACU open
157 * return to the real uid and gid. These things will
158 * be closed on exit. Swap real and effective uid's
159 * so we can get the original permissions back
160 * for removing the uucp lock.
161 */
162 user_uid();
163
164 /*
165 * Kludge, their's no easy way to get the initialization
166 * in the right order, so force it here
167 */
168 if ((PH = getenv("PHONES")) == NOSTR)
169 PH = _PATH_PHONES;
170 vinit(); /* init variables */
171 setparity("even"); /* set the parity table */
172 if ((i = speed(number(value(BAUDRATE)))) == NULL) {
173 printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
174 daemon_uid();
175 (void)uu_unlock(uucplock);
176 exit(3);
177 }
178
179 /*
180 * Hardwired connections require the
181 * line speed set before they make any transmissions
182 * (this is particularly true of things like a DF03-AC)
183 */
184 if (HW)
185 ttysetup(i);
186 if (p = connect()) {
187 printf("\07%s\n[EOT]\n", p);
188 daemon_uid();
189 (void)uu_unlock(uucplock);
190 exit(1);
191 }
192 if (!HW)
193 ttysetup(i);
194 cucommon:
195 /*
196 * From here down the code is shared with
197 * the "cu" version of tip.
198 */
199
200 ioctl(0, TIOCGETP, (char *)&defarg);
201 ioctl(0, TIOCGETC, (char *)&defchars);
202 ioctl(0, TIOCGLTC, (char *)&deflchars);
203 ioctl(0, TIOCGETD, (char *)&odisc);
204 arg = defarg;
205 arg.sg_flags = ANYP | CBREAK;
206 tchars = defchars;
207 tchars.t_intrc = tchars.t_quitc = -1;
208 ltchars = deflchars;
209 ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
210 = ltchars.t_lnextc = -1;
211 raw();
212
213 pipe(fildes); pipe(repdes);
214 (void)signal(SIGALRM, timeout);
215
216 /*
217 * Everything's set up now:
218 * connection established (hardwired or dialup)
219 * line conditioned (baud rate, mode, etc.)
220 * internal data structures (variables)
221 * so, fork one process for local side and one for remote.
222 */
223 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
224 if (pid = fork())
225 tipin();
226 else
227 tipout();
228 /*NOTREACHED*/
229 }
230
231 void
232 cleanup()
233 {
234
235 daemon_uid();
236 (void)uu_unlock(uucplock);
237 if (odisc)
238 ioctl(0, TIOCSETD, (char *)&odisc);
239 exit(0);
240 }
241
242 /*
243 * Muck with user ID's. We are setuid to the owner of the lock
244 * directory when we start. user_uid() reverses real and effective
245 * ID's after startup, to run with the user's permissions.
246 * daemon_uid() switches back to the privileged uid for unlocking.
247 * Finally, to avoid running a shell with the wrong real uid,
248 * shell_uid() sets real and effective uid's to the user's real ID.
249 */
250 static int uidswapped;
251
252 user_uid()
253 {
254 if (uidswapped == 0) {
255 setegid(gid);
256 seteuid(uid);
257 uidswapped = 1;
258 }
259 }
260
261 daemon_uid()
262 {
263
264 if (uidswapped) {
265 seteuid(euid);
266 setegid(egid);
267 uidswapped = 0;
268 }
269 }
270
271 shell_uid()
272 {
273
274 setgid(gid);
275 setuid(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, <chars);
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 /*
391 * Escape handler --
392 * called on recognition of ``escapec'' at the beginning of a line
393 */
394 escape()
395 {
396 register char gch;
397 register esctable_t *p;
398 char c = character(value(ESCAPE));
399 extern esctable_t etable[];
400
401 gch = (getchar()&0177);
402 for (p = etable; p->e_char; p++)
403 if (p->e_char == gch) {
404 if ((p->e_flags&PRIV) && uid)
405 continue;
406 printf("%s", ctrl(c));
407 (*p->e_func)(gch);
408 return (0);
409 }
410 /* ESCAPE ESCAPE forces ESCAPE */
411 if (c != gch)
412 pwrite(FD, &c, 1);
413 return (gch);
414 }
415
416 speed(n)
417 int n;
418 {
419 register int *p;
420
421 for (p = bauds; *p != -1; p++)
422 if (*p == n)
423 return (p - bauds);
424 return (NULL);
425 }
426
427 any(c, p)
428 register char c, *p;
429 {
430 while (p && *p)
431 if (*p++ == c)
432 return (1);
433 return (0);
434 }
435
436 size(s)
437 register char *s;
438 {
439 register int i = 0;
440
441 while (s && *s++)
442 i++;
443 return (i);
444 }
445
446 char *
447 interp(s)
448 register char *s;
449 {
450 static char buf[256];
451 register char *p = buf, c, *q;
452
453 while (c = *s++) {
454 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
455 if (*q++ == c) {
456 *p++ = '\\'; *p++ = *q;
457 goto next;
458 }
459 if (c < 040) {
460 *p++ = '^'; *p++ = c + 'A'-1;
461 } else if (c == 0177) {
462 *p++ = '^'; *p++ = '?';
463 } else
464 *p++ = c;
465 next:
466 ;
467 }
468 *p = '\0';
469 return (buf);
470 }
471
472 char *
473 ctrl(c)
474 char c;
475 {
476 static char s[3];
477
478 if (c < 040 || c == 0177) {
479 s[0] = '^';
480 s[1] = c == 0177 ? '?' : c+'A'-1;
481 s[2] = '\0';
482 } else {
483 s[0] = c;
484 s[1] = '\0';
485 }
486 return (s);
487 }
488
489 /*
490 * Help command
491 */
492 help(c)
493 char c;
494 {
495 register esctable_t *p;
496 extern esctable_t etable[];
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 static char *mbp; static sz;
560
561 if (mbp == 0 || n > sz) {
562 if (mbp)
563 free(mbp);
564 mbp = (char *) malloc(n);
565 sz = n;
566 }
567
568 bp = mbp;
569 for (i = 0; i < n; i++)
570 *bp++ = partab[*buf++ & 0177];
571 bp = mbp;
572 }
573 if (write(fd, bp, n) < 0) {
574 if (errno == EIO)
575 tipabort("Lost carrier.");
576 /* this is questionable */
577 perror("write");
578 }
579 }
580
581 /*
582 * Build a parity table with appropriate high-order bit.
583 */
584 setparity(defparity)
585 char *defparity;
586 {
587 register int i, flip, clr, set;
588 char *parity;
589 extern char evenpartab[];
590
591 if (value(PARITY) == NOSTR)
592 value(PARITY) = defparity;
593 parity = value(PARITY);
594 if (equal(parity, "none")) {
595 bits8 = 1;
596 return;
597 }
598 bits8 = 0;
599 flip = 0;
600 clr = 0377;
601 set = 0;
602 if (equal(parity, "odd"))
603 flip = 0200; /* reverse bit 7 */
604 else if (equal(parity, "zero"))
605 clr = 0177; /* turn off bit 7 */
606 else if (equal(parity, "one"))
607 set = 0200; /* turn on bit 7 */
608 else if (!equal(parity, "even")) {
609 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
610 (void) fflush(stderr);
611 }
612 for (i = 0; i < 0200; i++)
613 partab[i] = evenpartab[i] ^ flip | set & clr;
614 }
615