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
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)tip.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/tip/tip/tip.c,v 1.12.2.2 2001/06/02 08:08:24 phk Exp $
36 * $DragonFly: src/usr.bin/tip/tip/tip.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
42 void ttysetup (int speed);
45 * tip - UNIX link to other systems
46 * tip [-v] [-speed] system-name
48 * cu phone-number [-s speed] [-l line] [-a acu]
53 #include <sys/types.h>
57 #include "pathnames.h"
60 * Baud rate mapping table
64 0, 50, 75, 110, 134, 150, 200, 300, 600,
65 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
70 int disc = OTTYDISC; /* tip normally runs this way */
79 char PNbuf[256]; /* This limits the size of a number */
81 static void usage __P((void));
82 void setparity __P((char *));
83 void xpwrite __P((int, char *, int));
84 char escape __P((void));
85 void tipin __P((void));
86 int prompt __P((char *, char *, size_t));
87 void unraw __P((void));
88 void shell_uid __P((void));
89 void daemon_uid __P((void));
90 void user_uid __P((void));
107 #if INCLUDE_CU_INTERFACE
108 if (equal(sname(argv[0]), "cu")) {
113 #endif /* INCLUDE_CU_INTERFACE */
118 errx(1, "must be interactive");
120 for (; argc > 1; argv++, argc--) {
121 if (argv[1][0] != '-')
123 else switch (argv[1][1]) {
129 case '0': case '1': case '2': case '3': case '4':
130 case '5': case '6': case '7': case '8': case '9':
131 BR = atoi(&argv[1][1]);
135 warnx("%s, unknown option", argv[1]);
142 if (isalpha(*system))
145 * System name is really a phone number...
146 * Copy the number then stomp on the original (in case the number
147 * is private, we don't want 'ps' or 'w' to find it).
149 if (strlen(system) > sizeof(PNbuf) - 1)
150 errx(1, "phone number too long (max = %d bytes)", sizeof PNbuf - 1);
151 strncpy(PNbuf, system, sizeof(PNbuf) - 1);
152 for (p = system; *p; p++)
155 (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
159 (void)signal(SIGINT, cleanup);
160 (void)signal(SIGQUIT, cleanup);
161 (void)signal(SIGHUP, cleanup);
162 (void)signal(SIGTERM, cleanup);
163 (void)signal(SIGUSR1, tipdone);
165 if ((i = hunt(system)) == 0) {
166 printf("all ports busy\n");
170 printf("link down\n");
171 (void)uu_unlock(uucplock);
174 setbuf(stdout, NULL);
178 * Kludge, their's no easy way to get the initialization
179 * in the right order, so force it here
181 if ((PH = getenv("PHONES")) == NOSTR)
183 vinit(); /* init variables */
184 setparity("even"); /* set the parity table */
185 if ((i = speed(number(value(BAUDRATE)))) == 0) {
186 printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
187 (void)uu_unlock(uucplock);
192 * Now that we have the logfile and the ACU open
193 * return to the real uid and gid. These things will
194 * be closed on exit. Swap real and effective uid's
195 * so we can get the original permissions back
196 * for removing the uucp lock.
201 * Hardwired connections require the
202 * line speed set before they make any transmissions
203 * (this is particularly true of things like a DF03-AC)
207 if ((p = connect())) {
208 printf("\07%s\n[EOT]\n", p);
210 (void)uu_unlock(uucplock);
217 * From here down the code is shared with
218 * the "cu" version of tip.
222 tcgetattr (0, &otermios);
224 #ifndef _POSIX_SOURCE
225 ctermios.c_iflag = (IMAXBEL|IXANY|ISTRIP|IXON|BRKINT);
226 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOCTL|ECHOE|ECHOKE);
228 ctermios.c_iflag = (ISTRIP|IXON|BRKINT);
229 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOE);
231 ctermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
232 ctermios.c_cc[VINTR] = ctermios.c_cc[VQUIT] = -1;
233 ctermios.c_cc[VSUSP] = ctermios.c_cc[VDSUSP] = ctermios.c_cc[VDISCARD] =
234 ctermios.c_cc[VLNEXT] = -1;
235 #else /* HAVE_TERMIOS */
236 ioctl(0, TIOCGETP, (char *)&defarg);
237 ioctl(0, TIOCGETC, (char *)&defchars);
238 ioctl(0, TIOCGLTC, (char *)&deflchars);
239 ioctl(0, TIOCGETD, (char *)&odisc);
241 arg.sg_flags = ANYP | CBREAK;
243 tchars.t_intrc = tchars.t_quitc = -1;
245 ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
246 = ltchars.t_lnextc = -1;
247 #endif /* HAVE_TERMIOS */
250 pipe(fildes); pipe(repdes);
251 (void)signal(SIGALRM, timeout);
254 * Everything's set up now:
255 * connection established (hardwired or dialup)
256 * line conditioned (baud rate, mode, etc.)
257 * internal data structures (variables)
258 * so, fork one process for local side and one for remote.
260 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
262 if (LI != NOSTR && tiplink (LI, 0) != 0) {
263 tipabort ("login failed");
276 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
294 (void)uu_unlock(uucplock);
297 ioctl(0, TIOCSETD, (char *)&odisc);
308 * Muck with user ID's. We are setuid to the owner of the lock
309 * directory when we start. user_uid() reverses real and effective
310 * ID's after startup, to run with the user's permissions.
311 * daemon_uid() switches back to the privileged uid for unlocking.
312 * Finally, to avoid running a shell with the wrong real uid,
313 * shell_uid() sets real and effective uid's to the user's real ID.
315 static int uidswapped;
320 if (uidswapped == 0) {
343 * put the controlling keyboard into raw mode
349 tcsetattr (0, TCSANOW, &ctermios);
350 #else /* HAVE_TERMIOS */
352 ioctl(0, TIOCSETP, &arg);
353 ioctl(0, TIOCSETC, &tchars);
354 ioctl(0, TIOCSLTC, <chars);
355 ioctl(0, TIOCSETD, (char *)&disc);
356 #endif /* HAVE_TERMIOS */
361 * return keyboard to normal mode
367 tcsetattr (0, TCSANOW, &otermios);
368 #else /* HAVE_TERMIOS */
370 ioctl(0, TIOCSETD, (char *)&odisc);
371 ioctl(0, TIOCSETP, (char *)&defarg);
372 ioctl(0, TIOCSETC, (char *)&defchars);
373 ioctl(0, TIOCSLTC, (char *)&deflchars);
374 #endif /* HAVE_TERMIOS */
377 static jmp_buf promptbuf;
380 * Print string ``s'', then read a string
381 * in from the terminal. Handles signals & allows use of
382 * normal erase and kill characters.
390 register char *b = p;
394 oint = signal(SIGINT, intprompt);
395 oquit = signal(SIGQUIT, SIG_IGN);
398 if (setjmp(promptbuf) == 0)
399 while ((*p = getchar()) != EOF && *p != '\n' && --sz > 0)
404 (void)signal(SIGINT, oint);
405 (void)signal(SIGQUIT, oquit);
406 return (stoprompt || p == b);
410 * Interrupt service routine during prompting
416 (void)signal(SIGINT, SIG_IGN);
419 longjmp(promptbuf, 1);
423 * ****TIPIN TIPIN****
434 * Kinda klugey here...
435 * check for scripting being turned on from the .tiprc file,
436 * but be careful about just using setscript(), as we may
437 * send a SIGEMT before tipout has a chance to set up catching
438 * it; so wait a second, then setscript()
440 if (boolean(value(SCRIPT))) {
450 if ((gch == character(value(ESCAPE))) && bol) {
451 if (!(gch = escape()))
453 } else if (!cumode && gch == character(value(RAISECHAR))) {
454 boolean(value(RAISE)) = !boolean(value(RAISE));
456 } else if (gch == '\r') {
458 xpwrite(FD, &gch, 1);
459 if (boolean(value(HALFDUPLEX)))
462 } else if (!cumode && gch == character(value(FORCE))) {
468 bol = any(gch, value(EOL));
469 if (boolean(value(RAISE)) && islower(gch))
471 xpwrite(FD, &gch, 1);
472 if (boolean(value(HALFDUPLEX)))
477 extern esctable_t etable[];
481 * called on recognition of ``escapec'' at the beginning of a line
487 register esctable_t *p;
488 char c = character(value(ESCAPE));
495 for (p = etable; p->e_char; p++)
496 if (p->e_char == gch) {
497 if ((p->e_flags&PRIV) && uid)
499 printf("%s", ctrl(c));
503 /* ESCAPE ESCAPE forces ESCAPE */
516 register CONST int *p;
518 for (p = bauds; *p != -1; p++)
550 static char buf[256];
551 register char *p = buf, c, *q;
554 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
556 *p++ = '\\'; *p++ = *q;
560 *p++ = '^'; *p++ = c + 'A'-1;
561 } else if (c == 0177) {
562 *p++ = '^'; *p++ = '?';
578 if (c < 040 || c == 0177) {
580 s[1] = c == 0177 ? '?' : c+'A'-1;
596 register esctable_t *p;
599 for (p = etable; p->e_char; p++) {
600 if ((p->e_flags&PRIV) && uid)
602 printf("%2s", ctrl(character(value(ESCAPE))));
603 printf("%-2s %c %s\r\n", ctrl(p->e_char),
604 p->e_flags&EXP ? '*': ' ', p->e_help);
609 * Set up the "remote" tty's state
615 struct termios termios;
616 tcgetattr (FD, &termios);
617 if (boolean(value(TAND)))
618 termios.c_iflag = IXOFF;
621 #ifndef _POSIX_SOURCE
622 termios.c_lflag = (PENDIN|ECHOKE|ECHOE);
624 termios.c_lflag = (PENDIN|ECHOE);
626 termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
627 termios.c_ispeed = termios.c_ospeed = speed;
628 tcsetattr (FD, TCSANOW, &termios);
629 #else /* HAVE_TERMIOS */
630 unsigned bits = LDECCTQ;
632 arg.sg_ispeed = arg.sg_ospeed = speed;
634 if (boolean(value(TAND)))
635 arg.sg_flags |= TANDEM;
636 ioctl(FD, TIOCSETP, (char *)&arg);
637 ioctl(FD, TIOCLBIS, (char *)&bits);
638 #endif /* HAVE_TERMIOS */
642 * Return "simple" name from a file name,
643 * strip leading directories.
649 register char *p = s;
657 static char partab[0200];
661 * Do a write to the remote machine with the correct parity.
662 * We are doing 8 bit wide output, so we just generate a character
663 * with the right parity and output it.
676 for (i = 0; i < n; i++) {
677 *bp = partab[(*bp) & 0177];
680 if (write(fd, buf, n) < 0) {
682 tipabort("Lost carrier.");
684 tipabort("tty not available.");
685 tipabort("Something wrong...");
690 * Build a parity table with appropriate high-order bit.
696 register int i, flip, clr, set;
698 extern char evenpartab[];
700 if (value(PARITY) == NOSTR)
701 value(PARITY) = defparity;
702 parity = value(PARITY);
703 if (equal(parity, "none")) {
711 if (equal(parity, "odd"))
712 flip = 0200; /* reverse bit 7 */
713 else if (equal(parity, "zero"))
714 clr = 0177; /* turn off bit 7 */
715 else if (equal(parity, "one"))
716 set = 0200; /* turn on bit 7 */
717 else if (!equal(parity, "even")) {
718 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
719 (void) fflush(stderr);
721 for (i = 0; i < 0200; i++)
722 partab[i] = (evenpartab[i] ^ flip) | (set & clr);