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