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