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 $
41 void ttysetup (int speed);
44 * tip - UNIX link to other systems
45 * tip [-v] [-speed] system-name
47 * cu phone-number [-s speed] [-l line] [-a acu]
52 #include <sys/types.h>
56 #include "pathnames.h"
59 * Baud rate mapping table
63 0, 50, 75, 110, 134, 150, 200, 300, 600,
64 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
69 int disc = OTTYDISC; /* tip normally runs this way */
78 char PNbuf[256]; /* This limits the size of a number */
80 static void usage(void);
81 void setparity(char *);
82 void xpwrite(int, char *, int);
85 int prompt(char *, char *, size_t);
88 void daemon_uid(void);
106 #if INCLUDE_CU_INTERFACE
107 if (equal(sname(argv[0]), "cu")) {
112 #endif /* INCLUDE_CU_INTERFACE */
117 errx(1, "must be interactive");
119 for (; argc > 1; argv++, argc--) {
120 if (argv[1][0] != '-')
122 else switch (argv[1][1]) {
128 case '0': case '1': case '2': case '3': case '4':
129 case '5': case '6': case '7': case '8': case '9':
130 BR = atoi(&argv[1][1]);
134 warnx("%s, unknown option", argv[1]);
141 if (isalpha(*system))
144 * System name is really a phone number...
145 * Copy the number then stomp on the original (in case the number
146 * is private, we don't want 'ps' or 'w' to find it).
148 if (strlen(system) > sizeof(PNbuf) - 1)
149 errx(1, "phone number too long (max = %d bytes)", sizeof PNbuf - 1);
150 strncpy(PNbuf, system, sizeof(PNbuf) - 1);
151 for (p = system; *p; p++)
154 (void)snprintf(sbuf, sizeof(sbuf), "tip%ld", BR);
158 (void)signal(SIGINT, cleanup);
159 (void)signal(SIGQUIT, cleanup);
160 (void)signal(SIGHUP, cleanup);
161 (void)signal(SIGTERM, cleanup);
162 (void)signal(SIGUSR1, tipdone);
164 if ((i = hunt(system)) == 0) {
165 printf("all ports busy\n");
169 printf("link down\n");
170 (void)uu_unlock(uucplock);
173 setbuf(stdout, NULL);
177 * Kludge, their's no easy way to get the initialization
178 * in the right order, so force it here
180 if ((PH = getenv("PHONES")) == NULL)
182 vinit(); /* init variables */
183 setparity("even"); /* set the parity table */
184 if ((i = speed(number(value(BAUDRATE)))) == 0) {
185 printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
186 (void)uu_unlock(uucplock);
191 * Now that we have the logfile and the ACU open
192 * return to the real uid and gid. These things will
193 * be closed on exit. Swap real and effective uid's
194 * so we can get the original permissions back
195 * for removing the uucp lock.
200 * Hardwired connections require the
201 * line speed set before they make any transmissions
202 * (this is particularly true of things like a DF03-AC)
206 if ((p = connect())) {
207 printf("\07%s\n[EOT]\n", p);
209 (void)uu_unlock(uucplock);
216 * From here down the code is shared with
217 * the "cu" version of tip.
221 tcgetattr (0, &otermios);
223 #ifndef _POSIX_SOURCE
224 ctermios.c_iflag = (IMAXBEL|IXANY|ISTRIP|IXON|BRKINT);
225 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOCTL|ECHOE|ECHOKE);
227 ctermios.c_iflag = (ISTRIP|IXON|BRKINT);
228 ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOE);
230 ctermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
231 ctermios.c_cc[VINTR] = ctermios.c_cc[VQUIT] = -1;
232 ctermios.c_cc[VSUSP] = ctermios.c_cc[VDSUSP] = ctermios.c_cc[VDISCARD] =
233 ctermios.c_cc[VLNEXT] = -1;
234 #else /* HAVE_TERMIOS */
235 ioctl(0, TIOCGETP, (char *)&defarg);
236 ioctl(0, TIOCGETC, (char *)&defchars);
237 ioctl(0, TIOCGLTC, (char *)&deflchars);
238 ioctl(0, TIOCGETD, (char *)&odisc);
240 arg.sg_flags = ANYP | CBREAK;
242 tchars.t_intrc = tchars.t_quitc = -1;
244 ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
245 = ltchars.t_lnextc = -1;
246 #endif /* HAVE_TERMIOS */
249 pipe(fildes); pipe(repdes);
250 (void)signal(SIGALRM, timeout);
253 * Everything's set up now:
254 * connection established (hardwired or dialup)
255 * line conditioned (baud rate, mode, etc.)
256 * internal data structures (variables)
257 * so, fork one process for local side and one for remote.
259 printf(cumode ? "Connected\r\n" : "\07connected\r\n");
261 if (LI != NULL && tiplink (LI, 0) != 0) {
262 tipabort ("login failed");
275 fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
293 (void)uu_unlock(uucplock);
296 ioctl(0, TIOCSETD, (char *)&odisc);
307 * Muck with user ID's. We are setuid to the owner of the lock
308 * directory when we start. user_uid() reverses real and effective
309 * ID's after startup, to run with the user's permissions.
310 * daemon_uid() switches back to the privileged uid for unlocking.
311 * Finally, to avoid running a shell with the wrong real uid,
312 * shell_uid() sets real and effective uid's to the user's real ID.
314 static int uidswapped;
319 if (uidswapped == 0) {
342 * put the controlling keyboard into raw mode
348 tcsetattr (0, TCSANOW, &ctermios);
349 #else /* HAVE_TERMIOS */
351 ioctl(0, TIOCSETP, &arg);
352 ioctl(0, TIOCSETC, &tchars);
353 ioctl(0, TIOCSLTC, <chars);
354 ioctl(0, TIOCSETD, (char *)&disc);
355 #endif /* HAVE_TERMIOS */
360 * return keyboard to normal mode
366 tcsetattr (0, TCSANOW, &otermios);
367 #else /* HAVE_TERMIOS */
369 ioctl(0, TIOCSETD, (char *)&odisc);
370 ioctl(0, TIOCSETP, (char *)&defarg);
371 ioctl(0, TIOCSETC, (char *)&defchars);
372 ioctl(0, TIOCSLTC, (char *)&deflchars);
373 #endif /* HAVE_TERMIOS */
376 static jmp_buf promptbuf;
379 * Print string ``s'', then read a string
380 * in from the terminal. Handles signals & allows use of
381 * normal erase and kill characters.
393 oint = signal(SIGINT, intprompt);
394 oquit = signal(SIGQUIT, SIG_IGN);
397 if (setjmp(promptbuf) == 0)
398 while ((*p = getchar()) != EOF && *p != '\n' && --sz > 0)
403 (void)signal(SIGINT, oint);
404 (void)signal(SIGQUIT, oquit);
405 return (stoprompt || p == b);
409 * Interrupt service routine during prompting
415 (void)signal(SIGINT, SIG_IGN);
418 longjmp(promptbuf, 1);
422 * ****TIPIN TIPIN****
433 * Kinda klugey here...
434 * check for scripting being turned on from the .tiprc file,
435 * but be careful about just using setscript(), as we may
436 * send a SIGEMT before tipout has a chance to set up catching
437 * it; so wait a second, then setscript()
439 if (boolean(value(SCRIPT))) {
449 if ((gch == character(value(ESCAPE))) && bol) {
450 if (!(gch = escape()))
452 } else if (!cumode && gch == character(value(RAISECHAR))) {
453 boolean(value(RAISE)) = !boolean(value(RAISE));
455 } else if (gch == '\r') {
457 xpwrite(FD, &gch, 1);
458 if (boolean(value(HALFDUPLEX)))
461 } else if (!cumode && gch == character(value(FORCE))) {
467 bol = any(gch, value(EOL));
468 if (boolean(value(RAISE)) && islower(gch))
470 xpwrite(FD, &gch, 1);
471 if (boolean(value(HALFDUPLEX)))
476 extern esctable_t etable[];
480 * called on recognition of ``escapec'' at the beginning of a line
487 char c = character(value(ESCAPE));
494 for (p = etable; p->e_char; p++)
495 if (p->e_char == gch) {
496 if ((p->e_flags&PRIV) && uid)
498 printf("%s", ctrl(c));
502 /* ESCAPE ESCAPE forces ESCAPE */
517 for (p = bauds; *p != -1; p++)
549 static char buf[256];
550 char *p = buf, c, *q;
553 for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
555 *p++ = '\\'; *p++ = *q;
559 *p++ = '^'; *p++ = c + 'A'-1;
560 } else if (c == 0177) {
561 *p++ = '^'; *p++ = '?';
577 if (c < 040 || c == 0177) {
579 s[1] = c == 0177 ? '?' : c+'A'-1;
598 for (p = etable; p->e_char; p++) {
599 if ((p->e_flags&PRIV) && uid)
601 printf("%2s", ctrl(character(value(ESCAPE))));
602 printf("%-2s %c %s\r\n", ctrl(p->e_char),
603 p->e_flags&EXP ? '*': ' ', p->e_help);
608 * Set up the "remote" tty's state
614 struct termios termios;
615 tcgetattr (FD, &termios);
616 if (boolean(value(TAND)))
617 termios.c_iflag = IXOFF;
620 #ifndef _POSIX_SOURCE
621 termios.c_lflag = (PENDIN|ECHOKE|ECHOE);
623 termios.c_lflag = (PENDIN|ECHOE);
625 termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
626 termios.c_ispeed = termios.c_ospeed = speed;
627 tcsetattr (FD, TCSANOW, &termios);
628 #else /* HAVE_TERMIOS */
629 unsigned bits = LDECCTQ;
631 arg.sg_ispeed = arg.sg_ospeed = speed;
633 if (boolean(value(TAND)))
634 arg.sg_flags |= TANDEM;
635 ioctl(FD, TIOCSETP, (char *)&arg);
636 ioctl(FD, TIOCLBIS, (char *)&bits);
637 #endif /* HAVE_TERMIOS */
641 * Return "simple" name from a file name,
642 * strip leading directories.
656 static char partab[0200];
660 * Do a write to the remote machine with the correct parity.
661 * We are doing 8 bit wide output, so we just generate a character
662 * with the right parity and output it.
675 for (i = 0; i < n; i++) {
676 *bp = partab[(*bp) & 0177];
679 if (write(fd, buf, n) < 0) {
681 tipabort("Lost carrier.");
683 tipabort("tty not available.");
684 tipabort("Something wrong...");
689 * Build a parity table with appropriate high-order bit.
695 int i, flip, clr, set;
697 extern char evenpartab[];
699 if (value(PARITY) == NULL)
700 value(PARITY) = defparity;
701 parity = value(PARITY);
702 if (equal(parity, "none")) {
710 if (equal(parity, "odd"))
711 flip = 0200; /* reverse bit 7 */
712 else if (equal(parity, "zero"))
713 clr = 0177; /* turn off bit 7 */
714 else if (equal(parity, "one"))
715 set = 0200; /* turn on bit 7 */
716 else if (!equal(parity, "even")) {
717 (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
718 (void) fflush(stderr);
720 for (i = 0; i < 0200; i++)
721 partab[i] = (evenpartab[i] ^ flip) | (set & clr);