tip.c revision 1.36 1 /* $NetBSD: tip.c,v 1.36 2006/04/03 02:01:28 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.36 2006/04/03 02:01:28 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 errx(3, "link down\n");
145 }
146 setbuf(stdout, NULL);
147 #ifdef ACULOG
148 loginit();
149 #endif
150
151 /*
152 * Kludge, their's no easy way to get the initialization
153 * in the right order, so force it here
154 */
155 if ((PH = getenv("PHONES")) == NULL)
156 PH = path_phones;
157 vinit(); /* init variables */
158 setparity("none"); /* set the parity table */
159
160 /*
161 * Hardwired connections require the
162 * line speed set before they make any transmissions
163 * (this is particularly true of things like a DF03-AC)
164 */
165 if (HW) {
166 if (ttysetup(i) != 0) {
167 errx(3, "bad baud rate %d",
168 (int)number(value(BAUDRATE)));
169 }
170 }
171 if ((q = connect()) != NULL) {
172 errx(1, "\07%s\n[EOT]\n", q);
173 }
174 if (!HW) {
175 if (ttysetup(i) != 0) {
176 errx(3, "bad baud rate %d",
177 (int)number(value(BAUDRATE)));
178 }
179 }
180
181
182 /*
183 * Direct connections with no carrier require using O_NONBLOCK on
184 * open, but we don't want to keep O_NONBLOCK after open because it
185 * will cause busy waits.
186 */
187 if (DC &&
188 ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
189 fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
190 err(1, "can't clear O_NONBLOCK");
191 }
192
193 cucommon:
194 /*
195 * From here down the code is shared with
196 * the "cu" version of tip.
197 */
198
199 tcgetattr(0, &defterm);
200 term = defterm;
201 term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
202 term.c_iflag &= ~(INPCK|ICRNL);
203 term.c_oflag &= ~OPOST;
204 term.c_cc[VMIN] = 1;
205 term.c_cc[VTIME] = 0;
206 defchars = term;
207 term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
208 term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
209 term.c_cc[VLNEXT] = _POSIX_VDISABLE;
210 raw();
211
212 pipe(fildes); pipe(repdes);
213 (void)signal(SIGALRM, alrmtimeout);
214
215 /*
216 * Everything's set up now:
217 * connection established (hardwired or dialup)
218 * line conditioned (baud rate, mode, etc.)
219 * internal data structures (variables)
220 * so, fork one process for local side and one for remote.
221 */
222 printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
223 switch (pid = fork()) {
224 default:
225 tipin();
226 break;
227 case 0:
228 tipout();
229 break;
230 case -1:
231 err(1, "can't fork");
232 }
233 /*NOTREACHED*/
234 exit(0); /* XXX: pacify gcc */
235 }
236
237 void
238 cleanup(int dummy)
239 {
240
241 if (odisc)
242 ioctl(0, TIOCSETD, (char *)&odisc);
243 exit(0);
244 }
245
246 /*
247 * put the controlling keyboard into raw mode
248 */
249 void
250 raw(void)
251 {
252
253 tcsetattr(0, TCSADRAIN, &term);
254 }
255
256
257 /*
258 * return keyboard to normal mode
259 */
260 void
261 unraw(void)
262 {
263
264 tcsetattr(0, TCSADRAIN, &defterm);
265 }
266
267 static jmp_buf promptbuf;
268
269 /*
270 * Print string ``s'', then read a string
271 * in from the terminal. Handles signals & allows use of
272 * normal erase and kill characters.
273 */
274 int
275 prompt(const char *s, char *p, size_t l)
276 {
277 int c;
278 char *b = p;
279 sig_t oint, oquit;
280
281 #if __GNUC__ /* XXX: pacify gcc */
282 (void)&p;
283 #endif
284
285 stoprompt = 0;
286 oint = signal(SIGINT, intprompt);
287 oquit = signal(SIGQUIT, SIG_IGN);
288 unraw();
289 printf("%s", s);
290 if (setjmp(promptbuf) == 0)
291 while ((c = getchar()) != -1 && (*p = c) != '\n' &&
292 b + l > p)
293 p++;
294 *p = '\0';
295
296 raw();
297 (void)signal(SIGINT, oint);
298 (void)signal(SIGQUIT, oquit);
299 return (stoprompt || p == b);
300 }
301
302 /*
303 * Interrupt service routine during prompting
304 */
305 void
306 intprompt(int dummy)
307 {
308
309 (void)signal(SIGINT, SIG_IGN);
310 stoprompt = 1;
311 printf("\r\n");
312 longjmp(promptbuf, 1);
313 }
314
315 /*
316 * ****TIPIN TIPIN****
317 */
318 void
319 tipin(void)
320 {
321 char gch, bol = 1;
322
323 /*
324 * Kinda klugey here...
325 * check for scripting being turned on from the .tiprc file,
326 * but be careful about just using setscript(), as we may
327 * send a SIGEMT before tipout has a chance to set up catching
328 * it; so wait a second, then setscript()
329 */
330 if (boolean(value(SCRIPT))) {
331 sleep(1);
332 setscript();
333 }
334
335 while (1) {
336 gch = getchar()&STRIP_PAR;
337 if ((gch == character(value(ESCAPE))) && bol) {
338 if (!(gch = escape()))
339 continue;
340 } else if (!cumode &&
341 gch && gch == character(value(RAISECHAR))) {
342 setboolean(value(RAISE), !boolean(value(RAISE)));
343 continue;
344 } else if (gch == '\r') {
345 bol = 1;
346 xpwrite(FD, &gch, 1);
347 if (boolean(value(HALFDUPLEX)))
348 printf("\r\n");
349 continue;
350 } else if (!cumode && gch && gch == character(value(FORCE)))
351 gch = getchar()&STRIP_PAR;
352 bol = any(gch, value(EOL));
353 if (boolean(value(RAISE)) && islower((unsigned char)gch))
354 gch = toupper((unsigned char)gch);
355 xpwrite(FD, &gch, 1);
356 if (boolean(value(HALFDUPLEX)))
357 printf("%c", gch);
358 }
359 }
360
361 /*
362 * Escape handler --
363 * called on recognition of ``escapec'' at the beginning of a line
364 */
365 int
366 escape(void)
367 {
368 char gch;
369 esctable_t *p;
370 char c = character(value(ESCAPE));
371
372 gch = (getchar()&STRIP_PAR);
373 for (p = etable; p->e_char; p++)
374 if (p->e_char == gch) {
375 if ((p->e_flags&PRIV) && uid)
376 continue;
377 printf("%s", ctrl(c));
378 (*p->e_func)(gch);
379 return (0);
380 }
381 /* ESCAPE ESCAPE forces ESCAPE */
382 if (c != gch)
383 xpwrite(FD, &c, 1);
384 return (gch);
385 }
386
387 int
388 any(char c, const char *p)
389 {
390
391 while (p && *p)
392 if (*p++ == c)
393 return (1);
394 return (0);
395 }
396
397 char *
398 interp(const char *s)
399 {
400 static char buf[256];
401 char *p = buf, c;
402 const char *q;
403
404 while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
405 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
406 if (*q++ == c) {
407 *p++ = '\\'; *p++ = *q;
408 goto next;
409 }
410 if (c < 040) {
411 *p++ = '^'; *p++ = c + 'A'-1;
412 } else if (c == 0177) {
413 *p++ = '^'; *p++ = '?';
414 } else
415 *p++ = c;
416 next:
417 ;
418 }
419 *p = '\0';
420 return (buf);
421 }
422
423 char *
424 ctrl(char c)
425 {
426 static char s[3];
427
428 if (c < 040 || c == 0177) {
429 s[0] = '^';
430 s[1] = c == 0177 ? '?' : c+'A'-1;
431 s[2] = '\0';
432 } else {
433 s[0] = c;
434 s[1] = '\0';
435 }
436 return (s);
437 }
438
439 /*
440 * Help command
441 */
442 void
443 help(char c)
444 {
445 esctable_t *p;
446
447 printf("%c\r\n", c);
448 for (p = etable; p->e_char; p++) {
449 if ((p->e_flags&PRIV) && uid)
450 continue;
451 printf("%2s", ctrl(character(value(ESCAPE))));
452 printf("%-2s %c %s\r\n", ctrl(p->e_char),
453 p->e_flags&EXP ? '*': ' ', p->e_help);
454 }
455 }
456
457 /*
458 * Set up the "remote" tty's state
459 */
460 int
461 ttysetup(int spd)
462 {
463 struct termios cntrl;
464
465 tcgetattr(FD, &cntrl);
466 cfsetospeed(&cntrl, spd);
467 cfsetispeed(&cntrl, spd);
468 cntrl.c_cflag &= ~(CSIZE|PARENB);
469 cntrl.c_cflag |= CS8;
470 if (DC)
471 cntrl.c_cflag |= CLOCAL;
472 if (boolean(value(HARDWAREFLOW)))
473 cntrl.c_cflag |= CRTSCTS;
474 cntrl.c_iflag &= ~(ISTRIP|ICRNL);
475 cntrl.c_oflag &= ~OPOST;
476 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
477 cntrl.c_cc[VMIN] = 1;
478 cntrl.c_cc[VTIME] = 0;
479 if (boolean(value(TAND)))
480 cntrl.c_iflag |= IXOFF;
481 return(tcsetattr(FD, TCSAFLUSH, &cntrl));
482 }
483
484 static char partab[0200];
485
486 /*
487 * Do a write to the remote machine with the correct parity.
488 * We are doing 8 bit wide output, so we just generate a character
489 * with the right parity and output it.
490 */
491 void
492 xpwrite(int fd, char *buf, int n)
493 {
494 int i;
495 char *bp;
496
497 bp = buf;
498 if (bits8 == 0)
499 for (i = 0; i < n; i++) {
500 *bp = partab[(*bp) & 0177];
501 bp++;
502 }
503 if (write(fd, buf, n) < 0) {
504 if (errno == EIO)
505 tipabort("Lost carrier.");
506 /* this is questionable */
507 warn("write");
508 }
509 }
510
511 /*
512 * Build a parity table with appropriate high-order bit.
513 */
514 void
515 setparity(const char *defparity)
516 {
517 int i, flip, clr, set;
518 const char *parity;
519 static char *curpar;
520
521 if (value(PARITY) == NULL || (value(PARITY))[0] == '\0') {
522 if (curpar != NULL)
523 free(curpar);
524 value(PARITY) = curpar = strdup(defparity);
525 }
526 parity = value(PARITY);
527 if (equal(parity, "none")) {
528 bits8 = 1;
529 return;
530 }
531 bits8 = 0;
532 flip = 0;
533 clr = 0377;
534 set = 0;
535 if (equal(parity, "odd"))
536 flip = 0200; /* reverse bit 7 */
537 else if (equal(parity, "zero"))
538 clr = 0177; /* turn off bit 7 */
539 else if (equal(parity, "one"))
540 set = 0200; /* turn on bit 7 */
541 else if (!equal(parity, "even")) {
542 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
543 (void) fflush(stderr);
544 }
545 for (i = 0; i < 0200; i++)
546 partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
547 }
548