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 #include "tip.h"
58 #include "pathnames.h"
59
60 int disc = TTYDISC; /* tip normally runs this way */
61 char PNbuf[256]; /* This limits the size of a number */
62
63 static void intprompt(int);
64 static void tipin(void);
65 static int escape(void);
66
67 int
main(int argc,char * argv[])68 main(int argc, char *argv[])
69 {
70 char *sys = NOSTR, sbuf[12], *p;
71 int i;
72
73 /* XXX preserve previous braindamaged behavior */
74 setboolean(value(DC), TRUE);
75
76 gid = getgid();
77 egid = getegid();
78 uid = getuid();
79 euid = geteuid();
80 if (equal(__progname, "cu")) {
81 cumode = 1;
82 cumain(argc, argv);
83 goto cucommon;
84 }
85
86 if (argc > 4) {
87 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
88 exit(1);
89 }
90 if (!isatty(0)) {
91 fprintf(stderr, "%s: must be interactive\n", __progname);
92 exit(1);
93 }
94
95 for (; argc > 1; argv++, argc--) {
96 if (argv[1][0] != '-')
97 sys = argv[1];
98 else switch (argv[1][1]) {
99
100 case 'v':
101 vflag++;
102 break;
103
104 case 'n':
105 noesc++;
106 break;
107
108 case '0': case '1': case '2': case '3': case '4':
109 case '5': case '6': case '7': case '8': case '9':
110 BR = atoi(&argv[1][1]);
111 break;
112
113 default:
114 fprintf(stderr, "%s: %s, unknown option\n", __progname,
115 argv[1]);
116 break;
117 }
118 }
119
120 if (sys == NOSTR)
121 goto notnumber;
122 if (isalpha(*sys))
123 goto notnumber;
124 /*
125 * System name is really a phone number...
126 * Copy the number then stomp on the original (in case the number
127 * is private, we don't want 'ps' or 'w' to find it).
128 */
129 if (strlen(sys) > sizeof PNbuf - 1) {
130 fprintf(stderr, "%s: phone number too long (max = %d bytes)\n",
131 __progname, (int)sizeof(PNbuf) - 1);
132 exit(1);
133 }
134 strlcpy(PNbuf, sys, sizeof PNbuf - 1);
135 for (p = sys; *p; p++)
136 *p = '\0';
137 PN = PNbuf;
138 (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
139 sys = sbuf;
140
141 notnumber:
142 (void)signal(SIGINT, cleanup);
143 (void)signal(SIGQUIT, cleanup);
144 (void)signal(SIGHUP, cleanup);
145 (void)signal(SIGTERM, cleanup);
146 (void)signal(SIGCHLD, SIG_DFL);
147
148 if ((i = hunt(sys)) == 0) {
149 printf("all ports busy\n");
150 exit(3);
151 }
152 if (i == -1) {
153 printf("link down\n");
154 (void)uu_unlock(uucplock);
155 exit(3);
156 }
157 setbuf(stdout, NULL);
158 loginit();
159
160 /*
161 * Now that we have the logfile and the ACU open
162 * return to the real uid and gid. These things will
163 * be closed on exit. Swap real and effective uid's
164 * so we can get the original permissions back
165 * for removing the uucp lock.
166 */
167 user_uid();
168
169 /*
170 * Kludge, their's no easy way to get the initialization
171 * in the right order, so force it here
172 */
173 if ((PH = getenv("PHONES")) == NOSTR)
174 PH = _PATH_PHONES;
175 vinit(); /* init variables */
176 setparity("none"); /* set the parity table */
177
178 /*
179 * Hardwired connections require the
180 * line speed set before they make any transmissions
181 * (this is particularly true of things like a DF03-AC)
182 */
183 if (HW && ttysetup(number(value(BAUDRATE)))) {
184 fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
185 number(value(BAUDRATE)));
186 daemon_uid();
187 (void)uu_unlock(uucplock);
188 exit(3);
189 }
190 if ((p = con())) {
191 printf("\07%s\n[EOT]\n", p);
192 daemon_uid();
193 (void)uu_unlock(uucplock);
194 exit(1);
195 }
196 if (!HW && ttysetup(number(value(BAUDRATE)))) {
197 fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
198 number(value(BAUDRATE)));
199 daemon_uid();
200 (void)uu_unlock(uucplock);
201 exit(3);
202 }
203 cucommon:
204 /*
205 * From here down the code is shared with
206 * the "cu" version of tip.
207 */
208
209 i = fcntl(FD, F_GETFL);
210 if (i == -1) {
211 perror("fcntl");
212 cleanup(0);
213 }
214 i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
215 if (i == -1) {
216 perror("fcntl");
217 cleanup(0);
218 }
219
220 tcgetattr(0, &defterm);
221 gotdefterm = 1;
222 term = defterm;
223 term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
224 term.c_iflag &= ~(INPCK|ICRNL);
225 term.c_oflag &= ~OPOST;
226 term.c_cc[VMIN] = 1;
227 term.c_cc[VTIME] = 0;
228 defchars = term;
229 term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
230 term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
231 term.c_cc[VLNEXT] = _POSIX_VDISABLE;
232 raw();
233
234 pipe(fildes); pipe(repdes);
235 (void)signal(SIGALRM, timeout);
236
237 if (value(LINEDISC) != TTYDISC) {
238 int ld = (int)(intptr_t)value(LINEDISC);
239 ioctl(FD, TIOCSETD, &ld);
240 }
241
242 /*
243 * Everything's set up now:
244 * connection established (hardwired or dialup)
245 * line conditioned (baud rate, mode, etc.)
246 * internal data structures (variables)
247 * so, fork one process for local side and one for remote.
248 */
249 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
250 tipin_pid = getpid();
251 if ((tipout_pid = fork()))
252 tipin();
253 else
254 tipout();
255 /*NOTREACHED*/
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()&STRIP_PAR;
406 /* XXX does not check for EOF */
407 if ((gch == character(value(ESCAPE))) && bol) {
408 if (!noesc) {
409 if (!(gch = escape()))
410 continue;
411 }
412 } else if (!cumode && gch == character(value(RAISECHAR))) {
413 setboolean(value(RAISE), !boolean(value(RAISE)));
414 continue;
415 } else if (gch == '\r') {
416 bol = 1;
417 ch = gch;
418 parwrite(FD, &ch, 1);
419 if (boolean(value(HALFDUPLEX)))
420 printf("\r\n");
421 continue;
422 } else if (!cumode && gch == character(value(FORCE)))
423 gch = getchar()&STRIP_PAR;
424 bol = any(gch, value(EOL));
425 if (boolean(value(RAISE)) && islower(gch))
426 gch = toupper(gch);
427 ch = gch;
428 parwrite(FD, &ch, 1);
429 if (boolean(value(HALFDUPLEX)))
430 printf("%c", ch);
431 }
432 }
433
434 extern esctable_t etable[];
435
436 /*
437 * Escape handler --
438 * called on recognition of ``escapec'' at the beginning of a line
439 */
440 static int
escape(void)441 escape(void)
442 {
443 int gch;
444 esctable_t *p;
445 char c = character(value(ESCAPE));
446
447 gch = (getchar()&STRIP_PAR);
448 /* XXX does not check for EOF */
449 for (p = etable; p->e_char; p++)
450 if (p->e_char == gch) {
451 if ((p->e_flags&PRIV) && uid)
452 continue;
453 printf("%s", ctrl(c));
454 (*p->e_func)(gch);
455 return (0);
456 }
457 /* ESCAPE ESCAPE forces ESCAPE */
458 if (c != gch)
459 parwrite(FD, &c, 1);
460 return (gch);
461 }
462
463 int
any(int cc,char * p)464 any(int cc, char *p)
465 {
466 char c = cc;
467 while (p && *p)
468 if (*p++ == c)
469 return (1);
470 return (0);
471 }
472
473 size_t
size(char * s)474 size(char *s)
475 {
476 size_t i = 0;
477
478 while (s && *s++)
479 i++;
480 return (i);
481 }
482
483 char *
interp(char * s)484 interp(char *s)
485 {
486 static char buf[256];
487 char *p = buf, c, *q;
488
489 while ((c = *s++)) {
490 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
491 if (*q++ == c) {
492 *p++ = '\\'; *p++ = *q;
493 goto next;
494 }
495 if (c < 040) {
496 *p++ = '^'; *p++ = c + 'A'-1;
497 } else if (c == 0177) {
498 *p++ = '^'; *p++ = '?';
499 } else
500 *p++ = c;
501 next:
502 ;
503 }
504 *p = '\0';
505 return (buf);
506 }
507
508 char *
ctrl(char c)509 ctrl(char c)
510 {
511 static char s[3];
512
513 if (c < 040 || c == 0177) {
514 s[0] = '^';
515 s[1] = c == 0177 ? '?' : c+'A'-1;
516 s[2] = '\0';
517 } else {
518 s[0] = c;
519 s[1] = '\0';
520 }
521 return (s);
522 }
523
524 /*
525 * Help command
526 */
527 void
help(int c)528 help(int c)
529 {
530 esctable_t *p;
531
532 printf("%c\r\n", c);
533 for (p = etable; p->e_char; p++) {
534 if ((p->e_flags&PRIV) && uid)
535 continue;
536 printf("%2s", ctrl(character(value(ESCAPE))));
537 printf("%-2s %c %s\r\n", ctrl(p->e_char),
538 p->e_flags&EXP ? '*': ' ', p->e_help);
539 }
540 }
541
542 /*
543 * Set up the "remote" tty's state
544 */
545 int
ttysetup(int speed)546 ttysetup(int speed)
547 {
548 struct termios cntrl;
549
550 if (tcgetattr(FD, &cntrl))
551 return (-1);
552 cfsetspeed(&cntrl, speed);
553 cntrl.c_cflag &= ~(CSIZE|PARENB);
554 cntrl.c_cflag |= CS8;
555 if (boolean(value(DC)))
556 cntrl.c_cflag |= CLOCAL;
557 if (boolean(value(HARDWAREFLOW)))
558 cntrl.c_cflag |= CRTSCTS;
559 cntrl.c_iflag &= ~(ISTRIP|ICRNL);
560 cntrl.c_oflag &= ~OPOST;
561 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
562 cntrl.c_cc[VMIN] = 1;
563 cntrl.c_cc[VTIME] = 0;
564 if (boolean(value(TAND)))
565 cntrl.c_iflag |= IXOFF;
566 return (tcsetattr(FD, TCSAFLUSH, &cntrl));
567 }
568
569 static char partab[0200];
570
571 /*
572 * Do a write to the remote machine with the correct parity.
573 * We are doing 8 bit wide output, so we just generate a character
574 * with the right parity and output it.
575 */
576 void
parwrite(int fd,char * buf,size_t n)577 parwrite(int fd, char *buf, size_t n)
578 {
579 size_t i;
580 char *bp;
581
582 bp = buf;
583 if (bits8 == 0)
584 for (i = 0; i < n; i++) {
585 *bp = partab[(*bp) & 0177];
586 bp++;
587 }
588 if (write(fd, buf, n) < 0) {
589 if (errno == EIO || errno == ENXIO)
590 tipabort("Lost carrier.");
591 /* this is questionable */
592 perror("write");
593 }
594 }
595
596 /*
597 * Build a parity table with appropriate high-order bit.
598 */
599 void
setparity(char * defparity)600 setparity(char *defparity)
601 {
602 int i, flip, clr, set;
603 char *parity;
604 extern const unsigned char evenpartab[];
605
606 if (value(PARITY) == NOSTR)
607 value(PARITY) = defparity;
608 parity = value(PARITY);
609 if (equal(parity, "none")) {
610 bits8 = 1;
611 return;
612 }
613 bits8 = 0;
614 flip = 0;
615 clr = 0377;
616 set = 0;
617 if (equal(parity, "odd"))
618 flip = 0200; /* reverse bit 7 */
619 else if (equal(parity, "zero"))
620 clr = 0177; /* turn off bit 7 */
621 else if (equal(parity, "one"))
622 set = 0200; /* turn on bit 7 */
623 else if (!equal(parity, "even")) {
624 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
625 (void) fflush(stderr);
626 }
627 for (i = 0; i < 0200; i++)
628 partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
629 }
630