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