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