2 * Copyright (c) 1983, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char copyright[] =
43 "@(#) Copyright (c) 1983, 1990, 1993\n\
44 The Regents of the University of California. All rights reserved.\n";
49 static const char sccsid[] = "@(#)rlogin.c 8.1 (Berkeley) 6/6/93";
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
57 * rlogin - remote login
60 #include <sys/param.h>
61 #include <sys/ioctl.h>
62 #include <sys/socket.h>
64 #include <sys/resource.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/tcp.h>
87 #ifndef TIOCPKT_WINDOW
88 #define TIOCPKT_WINDOW 0x80
91 /* concession to Sun */
97 struct termios deftty;
99 int family = PF_UNSPEC;
102 u_char escapechar = '~';
104 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
105 struct winsize winsize;
107 void catch_child(int);
108 void copytochild(int);
109 void doit(long) __dead2;
110 void done(int) __dead2;
112 u_int getescape(const char *);
115 void msg(const char *);
118 void sendwindow(void);
122 void usage(void) __dead2;
127 main(int argc, char *argv[])
133 int argoff, ch, dflag, Dflag, one;
135 char *host, *localname, *p, *user, term[1024];
137 struct sockaddr_storage ss;
138 int i, len, len2, sslen;
140 argoff = dflag = Dflag = 0;
142 host = localname = user = NULL;
144 if ((p = rindex(argv[0], '/')))
149 if (strcmp(p, "rlogin"))
152 /* handle "rlogin host flags" */
153 if (!host && argc > 2 && argv[1][0] != '-') {
158 #define OPTIONS "468DEde:i:l:"
159 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
183 escapechar = getescape(optarg);
187 errx(1, "-i user: permission denied");
199 /* if haven't gotten a host yet, do so */
200 if (!host && !(host = argv[optind++]))
206 if (!(pw = getpwuid(uid = getuid())))
207 errx(1, "unknown user id");
211 localname = pw->pw_name;
214 sp = getservbyname("login", "tcp");
216 errx(1, "login/tcp: unknown service");
218 if ((p = getenv("TERM")) != NULL)
219 (void)strlcpy(term, p, sizeof(term));
221 if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
222 /* start at 2 to include the / */
223 for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
225 if (len + len2 < sizeof(term))
226 (void)snprintf(term + len, len2 + 1, "/%d", ospeed);
229 (void)get_window_size(0, &winsize);
231 (void)signal(SIGPIPE, lostpeer);
232 /* will use SIGUSR1 for window size hack, so hold it off */
233 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
235 * We set SIGURG and SIGUSR1 below so that an
236 * incoming signal will be held pending rather than being
237 * discarded. Note that these routines will be ready to get
238 * a signal by the time that they are unblocked below.
240 (void)signal(SIGURG, copytochild);
241 (void)signal(SIGUSR1, writeroob);
243 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
249 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
252 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
253 warn("setsockopt NODELAY (ignored)");
256 one = IPTOS_LOWDELAY;
257 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
258 ss.ss_family == AF_INET) {
259 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
261 warn("setsockopt TOS (ignored)");
263 if (ss.ss_family == AF_INET)
264 warn("setsockopt getsockname failed");
277 (void)signal(SIGINT, SIG_IGN);
287 if (reader(omask) == 0) {
288 msg("connection closed");
292 msg("\007connection closed");
297 * We may still own the socket, and may have a pending SIGURG (or might
298 * receive one soon) that we really want to send to the reader. When
299 * one of these comes in, the trap copytochild simply copies such
300 * signals to the child. We can now unblock SIGURG and SIGUSR1
301 * that were set above.
303 (void)sigsetmask(omask);
304 (void)signal(SIGCHLD, catch_child);
306 msg("closed connection");
310 /* trap a signal, unless it is being ignored. */
314 int omask = sigblock(sigmask(sig));
316 if (signal(sig, exit) == SIG_IGN)
317 (void)signal(sig, SIG_IGN);
318 (void)sigsetmask(omask);
328 /* make sure catch_child does not snap it up */
329 (void)signal(SIGCHLD, SIG_DFL);
330 if (kill(child, SIGKILL) >= 0)
331 while ((w = wait(&wstatus)) > 0 && w != child);
339 * This is called when the reader process gets the out-of-band (urgent)
340 * request to turn on the window-changing protocol.
344 writeroob(int signo __unused)
346 if (dosigwinch == 0) {
348 (void)signal(SIGWINCH, sigwinch);
355 catch_child(int signo __unused)
361 pid = wait3(&status, WNOHANG|WUNTRACED, NULL);
364 /* if the child (reader) dies, just quit */
365 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
366 done(WTERMSIG(status) | WEXITSTATUS(status));
372 * writer: write to remote: 0 -> line.
374 * ~^Z suspend rlogin process.
375 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
383 bol = 1; /* beginning of line */
386 n = read(STDIN_FILENO, &c, 1);
388 if (n < 0 && errno == EINTR)
393 * If we're at the beginning of the line and recognize a
394 * command character, then we echo locally. Otherwise,
395 * characters are echo'd remotely. If the command character
396 * is doubled, this acts as a force and local echo is
401 if (!noescape && c == escapechar) {
407 if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
411 if (CCEQ(deftty.c_cc[VSUSP], c) ||
412 CCEQ(deftty.c_cc[VDSUSP], c)) {
419 (void)write(rem, &escapechar, 1);
422 if (write(rem, &c, 1) == 0) {
426 bol = CCEQ(deftty.c_cc[VKILL], c) ||
427 CCEQ(deftty.c_cc[VEOF], c) ||
428 CCEQ(deftty.c_cc[VINTR], c) ||
429 CCEQ(deftty.c_cc[VSUSP], c) ||
430 c == '\r' || c == '\n';
446 } else if (c == 0177) {
453 (void)write(STDOUT_FILENO, buf, p - buf);
460 (void)signal(SIGCHLD, SIG_IGN);
461 (void)kill(CCEQ(deftty.c_cc[VSUSP], cmdc) ? 0 : getpid(), SIGTSTP);
462 (void)signal(SIGCHLD, catch_child);
464 sigwinch(0); /* check for size changes */
469 sigwinch(int signo __unused)
473 if (dosigwinch && get_window_size(0, &ws) == 0 &&
474 bcmp(&ws, &winsize, sizeof(ws))) {
481 * Send the window size to the server via the magic escape
487 char obuf[4 + sizeof (struct winsize)];
489 wp = (struct winsize *)(obuf+4);
494 wp->ws_row = htons(winsize.ws_row);
495 wp->ws_col = htons(winsize.ws_col);
496 wp->ws_xpixel = htons(winsize.ws_xpixel);
497 wp->ws_ypixel = htons(winsize.ws_ypixel);
499 (void)write(rem, obuf, sizeof(obuf));
503 * reader: read from remote: line -> 1
509 int rcvcnt, rcvstate;
511 char rcvbuf[8 * 1024];
515 oob(int signo __unused)
519 char waste[BUFSIZ], mark;
522 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
526 * Urgent data not here yet. It may not be possible
527 * to send it yet if we are blocked for output and
528 * our input buffer is full.
530 if (rcvcnt < (int)sizeof(rcvbuf)) {
531 n = read(rem, rcvbuf + rcvcnt,
532 sizeof(rcvbuf) - rcvcnt);
537 n = read(rem, waste, sizeof(waste));
546 if (mark & TIOCPKT_WINDOW) {
547 /* Let server know about window size changes */
548 (void)kill(ppid, SIGUSR1);
550 if (!eight && (mark & TIOCPKT_NOSTOP)) {
551 (void)tcgetattr(0, &tty);
552 tty.c_iflag &= ~IXON;
553 (void)tcsetattr(0, TCSANOW, &tty);
555 if (!eight && (mark & TIOCPKT_DOSTOP)) {
556 (void)tcgetattr(0, &tty);
557 tty.c_iflag |= (deftty.c_iflag & IXON);
558 (void)tcsetattr(0, TCSANOW, &tty);
560 if (mark & TIOCPKT_FLUSHWRITE) {
561 (void)tcflush(1, TCIOFLUSH);
563 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
569 n = read(rem, waste, sizeof (waste));
574 * Don't want any pending data to be output, so clear the recv
575 * buffer. If we were hanging on a write when interrupted,
576 * don't want it to restart. If we were reading, restart
583 /* oob does not do FLUSHREAD (alas!) */
586 * If we filled the receive buffer while a read was pending, longjmp
587 * to the top to restart appropriately. Don't abort a pending write,
588 * however, or we won't know how much was written.
590 if (rcvd && rcvstate == READING)
594 /* reader: read from remote: line -> 1 */
603 (void)signal(SIGTTOU, SIG_IGN);
604 (void)signal(SIGURG, oob);
605 (void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
607 (void)fcntl(rem, F_SETOWN, pid);
608 (void)setjmp(rcvtop);
609 (void)sigsetmask(omask);
612 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
614 n = write(STDOUT_FILENO, bufp, remaining);
626 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
645 (void)tcsetattr(0, TCSANOW, &deftty);
648 (void)tcgetattr(0, &deftty);
650 /* This is loosely derived from sys/kern/tty_compat.c. */
651 tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
652 tty.c_iflag &= ~ICRNL;
653 tty.c_oflag &= ~OPOST;
657 tty.c_iflag &= IXOFF;
658 tty.c_cflag &= ~(CSIZE|PARENB);
661 (void)tcsetattr(0, TCSANOW, &tty);
670 lostpeer(int signo __unused)
672 (void)signal(SIGPIPE, SIG_IGN);
673 msg("\007connection closed");
677 /* copy SIGURGs to the child process via SIGUSR1. */
680 copytochild(int signo __unused)
682 (void)kill(child, SIGUSR1);
688 (void)fprintf(stderr, "rlogin: %s\r\n", str);
694 (void)fprintf(stderr,
695 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
701 getescape(const char *p)
706 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
708 /* otherwise, \nnn */
709 if (*p == '\\' && len >= 2 && len <= 4) {
710 val = strtol(++p, NULL, 8);
714 if (*p < '0' || *p > '8')
718 msg("illegal option value -- e");