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 $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char copyright[] =
38 "@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
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 $";
50 * tip - UNIX link to other systems
51 * tip [-v] [-speed] system-name
53 * cu phone-number [-s speed] [-l line] [-a acu]
56 #include "pathnames.h"
58 int disc = TTYDISC; /* tip normally runs this way */
59 char PNbuf[256]; /* This limits the size of a number */
61 static void intprompt(int);
62 static void tipin(void);
63 static int escape(void);
66 main(int argc, char *argv[])
68 char *sys = NOSTR, sbuf[12], *p;
71 /* XXX preserve previous braindamaged behavior */
72 setboolean(value(DC), TRUE);
78 if (equal(__progname, "cu")) {
85 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
89 fprintf(stderr, "%s: must be interactive\n", __progname);
93 for (; argc > 1; argv++, argc--) {
94 if (argv[1][0] != '-')
96 else switch (argv[1][1]) {
106 case '0': case '1': case '2': case '3': case '4':
107 case '5': case '6': case '7': case '8': case '9':
108 BR = atoi(&argv[1][1]);
112 fprintf(stderr, "%s: %s, unknown option\n", __progname,
123 * System name is really a phone number...
124 * Copy the number then stomp on the original (in case the number
125 * is private, we don't want 'ps' or 'w' to find it).
127 if (strlen(sys) > sizeof PNbuf - 1) {
128 fprintf(stderr, "%s: phone number too long (max = %d bytes)\n",
129 __progname, (int)sizeof(PNbuf) - 1);
132 strlcpy(PNbuf, sys, sizeof PNbuf - 1);
133 for (p = sys; *p; p++)
136 (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
140 (void)signal(SIGINT, cleanup);
141 (void)signal(SIGQUIT, cleanup);
142 (void)signal(SIGHUP, cleanup);
143 (void)signal(SIGTERM, cleanup);
144 (void)signal(SIGCHLD, SIG_DFL);
146 if ((i = hunt(sys)) == 0) {
147 printf("all ports busy\n");
151 printf("link down\n");
152 (void)uu_unlock(uucplock);
155 setbuf(stdout, NULL);
159 * Now that we have the logfile and the ACU open
160 * return to the real uid and gid. These things will
161 * be closed on exit. Swap real and effective uid's
162 * so we can get the original permissions back
163 * for removing the uucp lock.
168 * Kludge, their's no easy way to get the initialization
169 * in the right order, so force it here
171 if ((PH = getenv("PHONES")) == NOSTR)
173 vinit(); /* init variables */
174 setparity("none"); /* set the parity table */
177 * Hardwired connections require the
178 * line speed set before they make any transmissions
179 * (this is particularly true of things like a DF03-AC)
181 if (HW && ttysetup(number(value(BAUDRATE)))) {
182 fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
183 number(value(BAUDRATE)));
185 (void)uu_unlock(uucplock);
189 printf("\07%s\n[EOT]\n", p);
191 (void)uu_unlock(uucplock);
194 if (!HW && ttysetup(number(value(BAUDRATE)))) {
195 fprintf(stderr, "%s: bad baud rate %ld\n", __progname,
196 number(value(BAUDRATE)));
198 (void)uu_unlock(uucplock);
203 * From here down the code is shared with
204 * the "cu" version of tip.
207 i = fcntl(FD, F_GETFL);
212 i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
218 tcgetattr(0, &defterm);
221 term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
222 term.c_iflag &= ~(INPCK|ICRNL);
223 term.c_oflag &= ~OPOST;
225 term.c_cc[VTIME] = 0;
227 term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
228 term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
229 term.c_cc[VLNEXT] = _POSIX_VDISABLE;
232 pipe(fildes); pipe(repdes);
233 (void)signal(SIGALRM, timeout);
235 if (value(LINEDISC) != TTYDISC) {
236 int ld = (int)(intptr_t)value(LINEDISC);
237 ioctl(FD, TIOCSETD, &ld);
241 * Everything's set up now:
242 * connection established (hardwired or dialup)
243 * line conditioned (baud rate, mode, etc.)
244 * internal data structures (variables)
245 * so, fork one process for local side and one for remote.
247 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
248 tipin_pid = getpid();
249 if ((tipout_pid = fork()))
261 (void)uu_unlock(uucplock);
263 ioctl(0, TIOCSETD, &odisc);
265 if (signo && tipout_pid) {
266 kill(tipout_pid, signo);
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.
280 static int uidswapped;
285 if (uidswapped == 0) {
309 * put the controlling keyboard into raw mode
314 tcsetattr(0, TCSADRAIN, &term);
319 * return keyboard to normal mode
325 tcsetattr(0, TCSADRAIN, &defterm);
329 * give up exclusive tty access
334 ioctl(FD, TIOCNXCL, 0);
337 static jmp_buf promptbuf;
340 * Print string ``s'', then read a string
341 * in from the terminal. Handles signals & allows use of
342 * normal erase and kill characters.
345 prompt(char *s, char *p, size_t sz)
352 oint = signal(SIGINT, intprompt);
353 oquit = signal(SIGQUIT, SIG_IGN);
356 if (setjmp(promptbuf) == 0)
357 while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
362 (void)signal(SIGINT, oint);
363 (void)signal(SIGQUIT, oquit);
364 return (stoprompt || p == b);
368 * Interrupt service routine during prompting
374 (void)signal(SIGINT, SIG_IGN);
377 longjmp(promptbuf, 1);
381 * ****TIPIN TIPIN****
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()
397 if (boolean(value(SCRIPT))) {
403 gch = getchar()&STRIP_PAR;
404 /* XXX does not check for EOF */
405 if ((gch == character(value(ESCAPE))) && bol) {
407 if (!(gch = escape()))
410 } else if (!cumode && gch == character(value(RAISECHAR))) {
411 setboolean(value(RAISE), !boolean(value(RAISE)));
413 } else if (gch == '\r') {
416 parwrite(FD, &ch, 1);
417 if (boolean(value(HALFDUPLEX)))
420 } else if (!cumode && gch == character(value(FORCE)))
421 gch = getchar()&STRIP_PAR;
422 bol = any(gch, value(EOL));
423 if (boolean(value(RAISE)) && islower(gch))
426 parwrite(FD, &ch, 1);
427 if (boolean(value(HALFDUPLEX)))
432 extern esctable_t etable[];
436 * called on recognition of ``escapec'' at the beginning of a line
443 char c = character(value(ESCAPE));
445 gch = (getchar()&STRIP_PAR);
446 /* XXX does not check for EOF */
447 for (p = etable; p->e_char; p++)
448 if (p->e_char == gch) {
449 if ((p->e_flags&PRIV) && uid)
451 printf("%s", ctrl(c));
455 /* ESCAPE ESCAPE forces ESCAPE */
484 static char buf[256];
485 char *p = buf, c, *q;
488 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
490 *p++ = '\\'; *p++ = *q;
494 *p++ = '^'; *p++ = c + 'A'-1;
495 } else if (c == 0177) {
496 *p++ = '^'; *p++ = '?';
511 if (c < 040 || c == 0177) {
513 s[1] = c == 0177 ? '?' : c+'A'-1;
531 for (p = etable; p->e_char; p++) {
532 if ((p->e_flags&PRIV) && uid)
534 printf("%2s", ctrl(character(value(ESCAPE))));
535 printf("%-2s %c %s\r\n", ctrl(p->e_char),
536 p->e_flags&EXP ? '*': ' ', p->e_help);
541 * Set up the "remote" tty's state
546 struct termios cntrl;
548 if (tcgetattr(FD, &cntrl))
550 cfsetspeed(&cntrl, speed);
551 cntrl.c_cflag &= ~(CSIZE|PARENB);
552 cntrl.c_cflag |= CS8;
553 if (boolean(value(DC)))
554 cntrl.c_cflag |= CLOCAL;
555 if (boolean(value(HARDWAREFLOW)))
556 cntrl.c_cflag |= CRTSCTS;
557 cntrl.c_iflag &= ~(ISTRIP|ICRNL);
558 cntrl.c_oflag &= ~OPOST;
559 cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
560 cntrl.c_cc[VMIN] = 1;
561 cntrl.c_cc[VTIME] = 0;
562 if (boolean(value(TAND)))
563 cntrl.c_iflag |= IXOFF;
564 return (tcsetattr(FD, TCSAFLUSH, &cntrl));
567 static char partab[0200];
570 * Do a write to the remote machine with the correct parity.
571 * We are doing 8 bit wide output, so we just generate a character
572 * with the right parity and output it.
575 parwrite(int fd, char *buf, size_t n)
582 for (i = 0; i < n; i++) {
583 *bp = partab[(*bp) & 0177];
586 if (write(fd, buf, n) < 0) {
587 if (errno == EIO || errno == ENXIO)
588 tipabort("Lost carrier.");
589 /* this is questionable */
595 * Build a parity table with appropriate high-order bit.
598 setparity(char *defparity)
600 int i, flip, clr, set;
602 extern const unsigned char evenpartab[];
604 if (value(PARITY) == NOSTR)
605 value(PARITY) = defparity;
606 parity = value(PARITY);
607 if (equal(parity, "none")) {
615 if (equal(parity, "odd"))
616 flip = 0200; /* reverse bit 7 */
617 else if (equal(parity, "zero"))
618 clr = 0177; /* turn off bit 7 */
619 else if (equal(parity, "one"))
620 set = 0200; /* turn on bit 7 */
621 else if (!equal(parity, "even")) {
622 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
623 (void) fflush(stderr);
625 for (i = 0; i < 0200; i++)
626 partab[i] = ((evenpartab[i] ^ flip) | set) & clr;