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