tip.c revision 1.34 1 /* $NetBSD: tip.c,v 1.34 2006/04/02 19:16:22 tls 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: tip.c,v 1.34 2006/04/02 19:16:22 tls 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 int escape __P((void));
55 int main __P((int, char **));
56 void intprompt __P((int));
57 void tipin __P((void));
58
59 char PNbuf[256]; /* This limits the size of a number */
60
61 static char path_phones[] = _PATH_PHONES;
62
63 int
64 main(argc, argv)
65 int argc;
66 char *argv[];
67 {
68 char *System = NULL;
69 int i;
70 char *p;
71 const char *q;
72 char sbuf[12];
73 int fcarg;
74
75 gid = getgid();
76 egid = getegid();
77 uid = getuid();
78 euid = geteuid();
79 if (equal(basename(argv[0]), "cu")) {
80 cumode = 1;
81 cumain(argc, argv);
82 goto cucommon;
83 }
84
85 if (argc > 4) {
86 fprintf(stderr, "usage: %s [-v] [-speed] [system-name]\n",
87 getprogname());
88 exit(1);
89 }
90 if (!isatty(0)) {
91 fprintf(stderr, "%s: must be interactive\n", getprogname());
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 warnx("%s, unknown option", argv[1]);
111 break;
112 }
113 }
114
115 if (System == NULL)
116 goto notnumber;
117 if (isalpha((unsigned char)*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 errx(1, "phone number too long (max = %d bytes)",
126 (int)sizeof(PNbuf) - 1);
127 }
128 (void)strlcpy(PNbuf, System, sizeof(PNbuf));
129 for (p = System; *p; p++)
130 *p = '\0';
131 PN = PNbuf;
132 (void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR);
133 System = sbuf;
134
135 notnumber:
136 (void)signal(SIGINT, cleanup);
137 (void)signal(SIGQUIT, cleanup);
138 (void)signal(SIGHUP, cleanup);
139 (void)signal(SIGTERM, cleanup);
140
141 if ((i = hunt(System)) == 0) {
142 printf("all ports busy\n");
143 exit(3);
144 }
145 if (i == -1) {
146 printf("link down\n");
147 (void)uu_unlock(uucplock);
148 exit(3);
149 }
150 setbuf(stdout, NULL);
151 #ifdef ACULOG
152 loginit();
153 #endif
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")) == NULL)
169 PH = path_phones;
170 vinit(); /* init variables */
171 setparity("none"); /* set the parity table */
172
173 /*
174 * Hardwired connections require the
175 * line speed set before they make any transmissions
176 * (this is particularly true of things like a DF03-AC)
177 */
178 if (HW) {
179 if (ttysetup(i) != 0) {
180 warnx("bad baud rate %d",
181 (int)number(value(BAUDRATE)));
182 daemon_uid();
183 (void)uu_unlock(uucplock);
184 exit(3);
185 }
186 }
187 if ((q = connect()) != NULL) {
188 printf("\07%s\n[EOT]\n", q);
189 daemon_uid();
190 (void)uu_unlock(uucplock);
191 exit(1);
192 }
193 if (!HW) {
194 if (ttysetup(i) != 0) {
195 warnx("bad baud rate %d",
196 (int)number(value(BAUDRATE)));
197 daemon_uid();
198 (void)uu_unlock(uucplock);
199 exit(3);
200 }
201 }
202
203
204 /*
205 * Direct connections with no carrier require using O_NONBLOCK on
206 * open, but we don't want to keep O_NONBLOCK after open because it
207 * will cause busy waits.
208 */
209 if (DC &&
210 ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
211 fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
212 warn("can't clear O_NONBLOCK");
213 daemon_uid();
214 (void)uu_unlock(uucplock);
215 exit(1);
216 }
217
218 cucommon:
219 /*
220 * From here down the code is shared with
221 * the "cu" version of tip.
222 */
223
224 tcgetattr(0, &defterm);
225 term = defterm;
226 term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
227 term.c_iflag &= ~(INPCK|ICRNL);
228 term.c_oflag &= ~OPOST;
229 term.c_cc[VMIN] = 1;
230 term.c_cc[VTIME] = 0;
231 defchars = term;
232 term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
233 term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
234 term.c_cc[VLNEXT] = _POSIX_VDISABLE;
235 raw();
236
237 pipe(fildes); pipe(repdes);
238 (void)signal(SIGALRM, alrmtimeout);
239
240 /*
241 * Everything's set up now:
242 * connection established (hardwired or dialup)
243 * line conditioned (baud rate, mode, etc.)
244 * internal data structures (variables)
245 * so, fork one process for local side and one for remote.
246 */
247 printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
248 switch (pid = fork()) {
249 default:
250 tipin();
251 break;
252 case 0:
253 tipout();
254 break;
255 case -1:
256 err(1, "can't fork");
257 }
258 /*NOTREACHED*/
259 exit(0); /* XXX: pacify gcc */
260 }
261
262 void
263 cleanup(dummy)
264 int dummy;
265 {
266
267 daemon_uid();
268 (void)uu_unlock(uucplock);
269 if (odisc)
270 ioctl(0, TIOCSETD, (char *)&odisc);
271 exit(0);
272 }
273
274 /*
275 * Muck with user ID's. We are setuid to the owner of the lock
276 * directory when we start. user_uid() reverses real and effective
277 * ID's after startup, to run with the user's permissions.
278 * daemon_uid() switches back to the privileged uid for unlocking.
279 * Finally, to avoid running a shell with the wrong real uid,
280 * shell_uid() sets real and effective uid's to the user's real ID.
281 */
282 static int uidswapped;
283
284 void
285 user_uid()
286 {
287
288 if (uidswapped == 0) {
289 seteuid(uid);
290 uidswapped = 1;
291 }
292 }
293
294 void
295 daemon_uid()
296 {
297
298 if (uidswapped) {
299 seteuid(euid);
300 uidswapped = 0;
301 }
302 }
303
304 void
305 shell_uid()
306 {
307
308 seteuid(uid);
309 }
310
311 /*
312 * put the controlling keyboard into raw mode
313 */
314 void
315 raw()
316 {
317
318 tcsetattr(0, TCSADRAIN, &term);
319 }
320
321
322 /*
323 * return keyboard to normal mode
324 */
325 void
326 unraw()
327 {
328
329 tcsetattr(0, TCSADRAIN, &defterm);
330 }
331
332 static jmp_buf promptbuf;
333
334 /*
335 * Print string ``s'', then read a string
336 * in from the terminal. Handles signals & allows use of
337 * normal erase and kill characters.
338 */
339 int
340 prompt(s, p, l)
341 const char *s;
342 char *p;
343 size_t l;
344 {
345 int c;
346 char *b = p;
347 sig_t oint, oquit;
348
349 #if __GNUC__ /* XXX: pacify gcc */
350 (void)&p;
351 #endif
352
353 stoprompt = 0;
354 oint = signal(SIGINT, intprompt);
355 oquit = signal(SIGQUIT, SIG_IGN);
356 unraw();
357 printf("%s", s);
358 if (setjmp(promptbuf) == 0)
359 while ((c = getchar()) != -1 && (*p = c) != '\n' &&
360 b + l > p)
361 p++;
362 *p = '\0';
363
364 raw();
365 (void)signal(SIGINT, oint);
366 (void)signal(SIGQUIT, oquit);
367 return (stoprompt || p == b);
368 }
369
370 /*
371 * Interrupt service routine during prompting
372 */
373 void
374 intprompt(dummy)
375 int dummy;
376 {
377
378 (void)signal(SIGINT, SIG_IGN);
379 stoprompt = 1;
380 printf("\r\n");
381 longjmp(promptbuf, 1);
382 }
383
384 /*
385 * ****TIPIN TIPIN****
386 */
387 void
388 tipin()
389 {
390 char gch, bol = 1;
391
392 /*
393 * Kinda klugey here...
394 * check for scripting being turned on from the .tiprc file,
395 * but be careful about just using setscript(), as we may
396 * send a SIGEMT before tipout has a chance to set up catching
397 * it; so wait a second, then setscript()
398 */
399 if (boolean(value(SCRIPT))) {
400 sleep(1);
401 setscript();
402 }
403
404 while (1) {
405 gch = getchar()&STRIP_PAR;
406 if ((gch == character(value(ESCAPE))) && bol) {
407 if (!(gch = escape()))
408 continue;
409 } else if (!cumode &&
410 gch && gch == character(value(RAISECHAR))) {
411 setboolean(value(RAISE), !boolean(value(RAISE)));
412 continue;
413 } else if (gch == '\r') {
414 bol = 1;
415 xpwrite(FD, &gch, 1);
416 if (boolean(value(HALFDUPLEX)))
417 printf("\r\n");
418 continue;
419 } else if (!cumode && gch && gch == character(value(FORCE)))
420 gch = getchar()&STRIP_PAR;
421 bol = any(gch, value(EOL));
422 if (boolean(value(RAISE)) && islower((unsigned char)gch))
423 gch = toupper((unsigned char)gch);
424 xpwrite(FD, &gch, 1);
425 if (boolean(value(HALFDUPLEX)))
426 printf("%c", gch);
427 }
428 }
429
430 /*
431 * Escape handler --
432 * called on recognition of ``escapec'' at the beginning of a line
433 */
434 int
435 escape()
436 {
437 char gch;
438 esctable_t *p;
439 char c = character(value(ESCAPE));
440
441 gch = (getchar()&STRIP_PAR);
442 for (p = etable; p->e_char; p++)
443 if (p->e_char == gch) {
444 if ((p->e_flags&PRIV) && uid)
445 continue;
446 printf("%s", ctrl(c));
447 (*p->e_func)(gch);
448 return (0);
449 }
450 /* ESCAPE ESCAPE forces ESCAPE */
451 if (c != gch)
452 xpwrite(FD, &c, 1);
453 return (gch);
454 }
455
456 int
457 any(c, p)
458 char c;
459 const char *p;
460 {
461
462 while (p && *p)
463 if (*p++ == c)
464 return (1);
465 return (0);
466 }
467
468 char *
469 interp(s)
470 const char *s;
471 {
472 static char buf[256];
473 char *p = buf, c;
474 const char *q;
475
476 while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
477 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
478 if (*q++ == c) {
479 *p++ = '\\'; *p++ = *q;
480 goto next;
481 }
482 if (c < 040) {
483 *p++ = '^'; *p++ = c + 'A'-1;
484 } else if (c == 0177) {
485 *p++ = '^'; *p++ = '?';
486 } else
487 *p++ = c;
488 next:
489 ;
490 }
491 *p = '\0';
492 return (buf);
493 }
494
495 char *
496 ctrl(c)
497 char c;
498 {
499 static char s[3];
500
501 if (c < 040 || c == 0177) {
502 s[0] = '^';
503 s[1] = c == 0177 ? '?' : c+'A'-1;
504 s[2] = '\0';
505 } else {
506 s[0] = c;
507 s[1] = '\0';
508 }
509 return (s);
510 }
511
512 /*
513 * Help command
514 */
515 void
516 help(c)
517 char c;
518 {
519 esctable_t *p;
520
521 printf("%c\r\n", c);
522 for (p = etable; p->e_char; p++) {
523 if ((p->e_flags&PRIV) && uid)
524 continue;
525 printf("%2s", ctrl(character(value(ESCAPE))));
526 printf("%-2s %c %s\r\n", ctrl(p->e_char),
527 p->e_flags&EXP ? '*': ' ', p->e_help);
528 }
529 }
530
531 /*
532 * Set up the "remote" tty's state
533 */
534 int
535 ttysetup(spd)
536 int spd;
537 {
538 struct termios cntrl;
539
540 tcgetattr(FD, &cntrl);
541 cfsetospeed(&cntrl, spd);
542 cfsetispeed(&cntrl, spd);
543 cntrl.c_cflag &= ~(CSIZE|PARENB);
544 cntrl.c_cflag |= CS8;
545 if (DC)
546 cntrl.c_cflag |= CLOCAL;
547 if (boolean(value(HARDWAREFLOW)))
548 cntrl.c_cflag |= CRTSCTS;
549 cntrl.c_iflag &= ~(ISTRIP|ICRNL);
550 cntrl.c_oflag &= ~OPOST;
551 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
552 cntrl.c_cc[VMIN] = 1;
553 cntrl.c_cc[VTIME] = 0;
554 if (boolean(value(TAND)))
555 cntrl.c_iflag |= IXOFF;
556 return(tcsetattr(FD, TCSAFLUSH, &cntrl));
557 }
558
559 static char partab[0200];
560
561 /*
562 * Do a write to the remote machine with the correct parity.
563 * We are doing 8 bit wide output, so we just generate a character
564 * with the right parity and output it.
565 */
566 void
567 xpwrite(fd, buf, n)
568 int fd;
569 char *buf;
570 int n;
571 {
572 int i;
573 char *bp;
574
575 bp = buf;
576 if (bits8 == 0)
577 for (i = 0; i < n; i++) {
578 *bp = partab[(*bp) & 0177];
579 bp++;
580 }
581 if (write(fd, buf, n) < 0) {
582 if (errno == EIO)
583 tipabort("Lost carrier.");
584 /* this is questionable */
585 warn("write");
586 }
587 }
588
589 /*
590 * Build a parity table with appropriate high-order bit.
591 */
592 void
593 setparity(defparity)
594 const char *defparity;
595 {
596 int i, flip, clr, set;
597 const char *parity;
598 static char *curpar;
599
600 if (value(PARITY) == NULL || (value(PARITY))[0] == '\0') {
601 if (curpar != NULL)
602 free(curpar);
603 value(PARITY) = curpar = strdup(defparity);
604 }
605 parity = value(PARITY);
606 if (equal(parity, "none")) {
607 bits8 = 1;
608 return;
609 }
610 bits8 = 0;
611 flip = 0;
612 clr = 0377;
613 set = 0;
614 if (equal(parity, "odd"))
615 flip = 0200; /* reverse bit 7 */
616 else if (equal(parity, "zero"))
617 clr = 0177; /* turn off bit 7 */
618 else if (equal(parity, "one"))
619 set = 0200; /* turn on bit 7 */
620 else if (!equal(parity, "even")) {
621 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
622 (void) fflush(stderr);
623 }
624 for (i = 0; i < 0200; i++)
625 partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
626 }
627