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