2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.bin/tip/tip/tip.c,v 1.12.2.2 2001/06/02 08:08:24 phk Exp $";
51 void ttysetup (int speed);
54 * tip - UNIX link to other systems
55 * tip [-v] [-speed] system-name
57 * cu phone-number [-s speed] [-l line] [-a acu]
62 #include <sys/types.h>
66 #include "pathnames.h"
69 * Baud rate mapping table
73 0, 50, 75, 110, 134, 150, 200, 300, 600,
74 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
79 int disc = OTTYDISC; /* tip normally runs this way */
88 char PNbuf[256]; /* This limits the size of a number */
90 static void usage __P((void));
91 void setparity __P((char *));
92 void xpwrite __P((int, char *, int));
93 char escape __P((void));
94 void tipin __P((void));
95 int prompt __P((char *, char *, size_t));
96 void unraw __P((void));
97 void shell_uid __P((void));
98 void daemon_uid __P((void));
99 void user_uid __P((void));
100 int speed __P((int));
106 char *system = NOSTR;
116 #if INCLUDE_CU_INTERFACE
117 if (equal(sname(argv[0]), "cu")) {
122 #endif /* INCLUDE_CU_INTERFACE */
127 errx(1, "must be interactive");
129 for (; argc > 1; argv++, argc--) {
130 if (argv[1][0] != '-')
132 else switch (argv[1][1]) {
138 case '0': case '1': case '2': case '3': case '4':
139 case '5': case '6': case '7': case '8': case '9':
140 BR = atoi(&argv[1][1]);
144 warnx("%s, unknown option", argv[1]);
151 if (isalpha(*system))
154 * System name is really a phone number...
155 * Copy the number then stomp on the original (in case the number
156 * is private, we don't want 'ps' or 'w' to find it).
158 if (strlen(system) > sizeof(PNbuf) - 1)
159 errx(1, "phone number too long (max = %d bytes)", sizeof PNbuf - 1);
160 strncpy(PNbuf, system, sizeof(PNbuf) - 1);
161 for (p = system; *p; p++)
164 (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
168 (void)signal(SIGINT, cleanup);
169 (void)signal(SIGQUIT, cleanup);
170 (void)signal(SIGHUP, cleanup);
171 (void)signal(SIGTERM, cleanup);
172 (void)signal(SIGUSR1, tipdone);
174 if ((i = hunt(system)) == 0) {
175 printf("all ports busy\n");
179 printf("link down\n");
180 (void)uu_unlock(uucplock);
183 setbuf(stdout, NULL);
187 * Kludge, their's no easy way to get the initialization
188 * in the right order, so force it here
190 if ((PH = getenv("PHONES")) == NOSTR)
192 vinit(); /* init variables */
193 setparity("even"); /* set the parity table */
194 if ((i = speed(number(value(BAUDRATE)))) == 0) {
195 printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
196 (void)uu_unlock(uucplock);
201 * Now that we have the logfile and the ACU open
202 * return to the real uid and gid. These things will
203 * be closed on exit. Swap real and effective uid's
204 * so we can get the original permissions back
205 * for removing the uucp lock.
210 * Hardwired connections require the
211 * line speed set before they make any transmissions
212 * (this is particularly true of things like a DF03-AC)
216 if ((p = connect())) {
217 printf("\07%s\n[EOT]\n", p);
219 (void)uu_unlock(uucplock);
226 * From here down the code is shared with
227 * the "cu" version of tip.
231 tcgetattr (0, &otermios);
233 #ifndef _POSIX_SOURCE
234 ctermios.c_iflag = (IMAXBEL|IXANY|ISTRIP|IXON|BRKINT);
235 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOCTL|ECHOE|ECHOKE);
237 ctermios.c_iflag = (ISTRIP|IXON|BRKINT);
238 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOE);
240 ctermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
241 ctermios.c_cc[VINTR] = ctermios.c_cc[VQUIT] = -1;
242 ctermios.c_cc[VSUSP] = ctermios.c_cc[VDSUSP] = ctermios.c_cc[VDISCARD] =
243 ctermios.c_cc[VLNEXT] = -1;
244 #else /* HAVE_TERMIOS */
245 ioctl(0, TIOCGETP, (char *)&defarg);
246 ioctl(0, TIOCGETC, (char *)&defchars);
247 ioctl(0, TIOCGLTC, (char *)&deflchars);
248 ioctl(0, TIOCGETD, (char *)&odisc);
250 arg.sg_flags = ANYP | CBREAK;
252 tchars.t_intrc = tchars.t_quitc = -1;
254 ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
255 = ltchars.t_lnextc = -1;
256 #endif /* HAVE_TERMIOS */
259 pipe(fildes); pipe(repdes);
260 (void)signal(SIGALRM, timeout);
263 * Everything's set up now:
264 * connection established (hardwired or dialup)
265 * line conditioned (baud rate, mode, etc.)
266 * internal data structures (variables)
267 * so, fork one process for local side and one for remote.
269 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
271 if (LI != NOSTR && tiplink (LI, 0) != 0) {
272 tipabort ("login failed");
285 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
303 (void)uu_unlock(uucplock);
306 ioctl(0, TIOCSETD, (char *)&odisc);
317 * Muck with user ID's. We are setuid to the owner of the lock
318 * directory when we start. user_uid() reverses real and effective
319 * ID's after startup, to run with the user's permissions.
320 * daemon_uid() switches back to the privileged uid for unlocking.
321 * Finally, to avoid running a shell with the wrong real uid,
322 * shell_uid() sets real and effective uid's to the user's real ID.
324 static int uidswapped;
329 if (uidswapped == 0) {
352 * put the controlling keyboard into raw mode
358 tcsetattr (0, TCSANOW, &ctermios);
359 #else /* HAVE_TERMIOS */
361 ioctl(0, TIOCSETP, &arg);
362 ioctl(0, TIOCSETC, &tchars);
363 ioctl(0, TIOCSLTC, <chars);
364 ioctl(0, TIOCSETD, (char *)&disc);
365 #endif /* HAVE_TERMIOS */
370 * return keyboard to normal mode
376 tcsetattr (0, TCSANOW, &otermios);
377 #else /* HAVE_TERMIOS */
379 ioctl(0, TIOCSETD, (char *)&odisc);
380 ioctl(0, TIOCSETP, (char *)&defarg);
381 ioctl(0, TIOCSETC, (char *)&defchars);
382 ioctl(0, TIOCSLTC, (char *)&deflchars);
383 #endif /* HAVE_TERMIOS */
386 static jmp_buf promptbuf;
389 * Print string ``s'', then read a string
390 * in from the terminal. Handles signals & allows use of
391 * normal erase and kill characters.
399 register char *b = p;
403 oint = signal(SIGINT, intprompt);
404 oquit = signal(SIGQUIT, SIG_IGN);
407 if (setjmp(promptbuf) == 0)
408 while ((*p = getchar()) != EOF && *p != '\n' && --sz > 0)
413 (void)signal(SIGINT, oint);
414 (void)signal(SIGQUIT, oquit);
415 return (stoprompt || p == b);
419 * Interrupt service routine during prompting
425 (void)signal(SIGINT, SIG_IGN);
428 longjmp(promptbuf, 1);
432 * ****TIPIN TIPIN****
443 * Kinda klugey here...
444 * check for scripting being turned on from the .tiprc file,
445 * but be careful about just using setscript(), as we may
446 * send a SIGEMT before tipout has a chance to set up catching
447 * it; so wait a second, then setscript()
449 if (boolean(value(SCRIPT))) {
459 if ((gch == character(value(ESCAPE))) && bol) {
460 if (!(gch = escape()))
462 } else if (!cumode && gch == character(value(RAISECHAR))) {
463 boolean(value(RAISE)) = !boolean(value(RAISE));
465 } else if (gch == '\r') {
467 xpwrite(FD, &gch, 1);
468 if (boolean(value(HALFDUPLEX)))
471 } else if (!cumode && gch == character(value(FORCE))) {
477 bol = any(gch, value(EOL));
478 if (boolean(value(RAISE)) && islower(gch))
480 xpwrite(FD, &gch, 1);
481 if (boolean(value(HALFDUPLEX)))
486 extern esctable_t etable[];
490 * called on recognition of ``escapec'' at the beginning of a line
496 register esctable_t *p;
497 char c = character(value(ESCAPE));
504 for (p = etable; p->e_char; p++)
505 if (p->e_char == gch) {
506 if ((p->e_flags&PRIV) && uid)
508 printf("%s", ctrl(c));
512 /* ESCAPE ESCAPE forces ESCAPE */
525 register CONST int *p;
527 for (p = bauds; *p != -1; p++)
559 static char buf[256];
560 register char *p = buf, c, *q;
563 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
565 *p++ = '\\'; *p++ = *q;
569 *p++ = '^'; *p++ = c + 'A'-1;
570 } else if (c == 0177) {
571 *p++ = '^'; *p++ = '?';
587 if (c < 040 || c == 0177) {
589 s[1] = c == 0177 ? '?' : c+'A'-1;
605 register esctable_t *p;
608 for (p = etable; p->e_char; p++) {
609 if ((p->e_flags&PRIV) && uid)
611 printf("%2s", ctrl(character(value(ESCAPE))));
612 printf("%-2s %c %s\r\n", ctrl(p->e_char),
613 p->e_flags&EXP ? '*': ' ', p->e_help);
618 * Set up the "remote" tty's state
624 struct termios termios;
625 tcgetattr (FD, &termios);
626 if (boolean(value(TAND)))
627 termios.c_iflag = IXOFF;
630 #ifndef _POSIX_SOURCE
631 termios.c_lflag = (PENDIN|ECHOKE|ECHOE);
633 termios.c_lflag = (PENDIN|ECHOE);
635 termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
636 termios.c_ispeed = termios.c_ospeed = speed;
637 tcsetattr (FD, TCSANOW, &termios);
638 #else /* HAVE_TERMIOS */
639 unsigned bits = LDECCTQ;
641 arg.sg_ispeed = arg.sg_ospeed = speed;
643 if (boolean(value(TAND)))
644 arg.sg_flags |= TANDEM;
645 ioctl(FD, TIOCSETP, (char *)&arg);
646 ioctl(FD, TIOCLBIS, (char *)&bits);
647 #endif /* HAVE_TERMIOS */
651 * Return "simple" name from a file name,
652 * strip leading directories.
658 register char *p = s;
666 static char partab[0200];
670 * Do a write to the remote machine with the correct parity.
671 * We are doing 8 bit wide output, so we just generate a character
672 * with the right parity and output it.
685 for (i = 0; i < n; i++) {
686 *bp = partab[(*bp) & 0177];
689 if (write(fd, buf, n) < 0) {
691 tipabort("Lost carrier.");
693 tipabort("tty not available.");
694 tipabort("Something wrong...");
699 * Build a parity table with appropriate high-order bit.
705 register int i, flip, clr, set;
707 extern char evenpartab[];
709 if (value(PARITY) == NOSTR)
710 value(PARITY) = defparity;
711 parity = value(PARITY);
712 if (equal(parity, "none")) {
720 if (equal(parity, "odd"))
721 flip = 0200; /* reverse bit 7 */
722 else if (equal(parity, "zero"))
723 clr = 0177; /* turn off bit 7 */
724 else if (equal(parity, "one"))
725 set = 0200; /* turn on bit 7 */
726 else if (!equal(parity, "even")) {
727 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
728 (void) fflush(stderr);
730 for (i = 0; i < 0200; i++)
731 partab[i] = (evenpartab[i] ^ flip) | (set & clr);