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