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>
86 #ifndef TIOCPKT_WINDOW
87 #define TIOCPKT_WINDOW 0x80
90 /* concession to Sun */
96 struct termios deftty;
98 int family = PF_UNSPEC;
101 u_char escapechar = '~';
103 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
104 struct winsize winsize;
106 void catch_child(int);
107 void copytochild(int);
108 void doit(long) __dead2;
109 void done(int) __dead2;
111 u_int getescape(const char *);
114 void msg(const char *);
117 void sendwindow(void);
121 void usage(void) __dead2;
126 main(int argc, char *argv[])
132 int argoff, ch, dflag, Dflag, one;
134 char *host, *localname, *p, *user, term[1024];
136 struct sockaddr_storage ss;
141 argoff = dflag = Dflag = 0;
143 host = localname = user = NULL;
145 if ((p = rindex(argv[0], '/')))
150 if (strcmp(p, "rlogin"))
153 /* handle "rlogin host flags" */
154 if (!host && argc > 2 && argv[1][0] != '-') {
159 #define OPTIONS "468DEde:i:l:"
160 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
184 escapechar = getescape(optarg);
188 errx(1, "-i user: permission denied");
200 /* if haven't gotten a host yet, do so */
201 if (!host && !(host = argv[optind++]))
207 if (!(pw = getpwuid(uid = getuid())))
208 errx(1, "unknown user id");
212 localname = pw->pw_name;
215 sp = getservbyname("login", "tcp");
217 errx(1, "login/tcp: unknown service");
219 if ((p = getenv("TERM")) != NULL)
220 (void)strlcpy(term, p, sizeof(term));
222 if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
223 /* start at 2 to include the / */
224 for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
226 if (len + len2 < sizeof(term))
227 (void)snprintf(term + len, len2 + 1, "/%d", ospeed);
230 (void)get_window_size(0, &winsize);
232 (void)signal(SIGPIPE, lostpeer);
233 /* will use SIGUSR1 for window size hack, so hold it off */
234 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
236 * We set SIGURG and SIGUSR1 below so that an
237 * incoming signal will be held pending rather than being
238 * discarded. Note that these routines will be ready to get
239 * a signal by the time that they are unblocked below.
241 (void)signal(SIGURG, copytochild);
242 (void)signal(SIGUSR1, writeroob);
244 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
250 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
253 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
254 warn("setsockopt NODELAY (ignored)");
257 one = IPTOS_LOWDELAY;
258 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
259 ss.ss_family == AF_INET) {
260 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
262 warn("setsockopt TOS (ignored)");
264 if (ss.ss_family == AF_INET)
265 warn("setsockopt getsockname failed");
278 (void)signal(SIGINT, SIG_IGN);
288 if (reader(omask) == 0) {
289 msg("connection closed");
293 msg("\007connection closed");
298 * We may still own the socket, and may have a pending SIGURG (or might
299 * receive one soon) that we really want to send to the reader. When
300 * one of these comes in, the trap copytochild simply copies such
301 * signals to the child. We can now unblock SIGURG and SIGUSR1
302 * that were set above.
304 (void)sigsetmask(omask);
305 (void)signal(SIGCHLD, catch_child);
307 msg("closed connection");
311 /* trap a signal, unless it is being ignored. */
315 int omask = sigblock(sigmask(sig));
317 if (signal(sig, exit) == SIG_IGN)
318 (void)signal(sig, SIG_IGN);
319 (void)sigsetmask(omask);
329 /* make sure catch_child does not snap it up */
330 (void)signal(SIGCHLD, SIG_DFL);
331 if (kill(child, SIGKILL) >= 0)
332 while ((w = wait(&wstatus)) > 0 && w != child);
340 * This is called when the reader process gets the out-of-band (urgent)
341 * request to turn on the window-changing protocol.
345 writeroob(int signo __unused)
347 if (dosigwinch == 0) {
349 (void)signal(SIGWINCH, sigwinch);
356 catch_child(int signo __unused)
362 pid = wait3(&status, WNOHANG|WUNTRACED, NULL);
365 /* if the child (reader) dies, just quit */
366 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
367 done(WTERMSIG(status) | WEXITSTATUS(status));
373 * writer: write to remote: 0 -> line.
375 * ~^Z suspend rlogin process.
376 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
384 bol = 1; /* beginning of line */
387 n = read(STDIN_FILENO, &c, 1);
389 if (n < 0 && errno == EINTR)
394 * If we're at the beginning of the line and recognize a
395 * command character, then we echo locally. Otherwise,
396 * characters are echo'd remotely. If the command character
397 * is doubled, this acts as a force and local echo is
402 if (!noescape && c == escapechar) {
408 if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
412 if (CCEQ(deftty.c_cc[VSUSP], c) ||
413 CCEQ(deftty.c_cc[VDSUSP], c)) {
420 (void)write(rem, &escapechar, 1);
423 if (write(rem, &c, 1) == 0) {
427 bol = CCEQ(deftty.c_cc[VKILL], c) ||
428 CCEQ(deftty.c_cc[VEOF], c) ||
429 CCEQ(deftty.c_cc[VINTR], c) ||
430 CCEQ(deftty.c_cc[VSUSP], c) ||
431 c == '\r' || c == '\n';
447 } else if (c == 0177) {
454 (void)write(STDOUT_FILENO, buf, p - buf);
461 (void)signal(SIGCHLD, SIG_IGN);
462 (void)kill(CCEQ(deftty.c_cc[VSUSP], cmdc) ? 0 : getpid(), SIGTSTP);
463 (void)signal(SIGCHLD, catch_child);
465 sigwinch(0); /* check for size changes */
470 sigwinch(int signo __unused)
474 if (dosigwinch && get_window_size(0, &ws) == 0 &&
475 bcmp(&ws, &winsize, sizeof(ws))) {
482 * Send the window size to the server via the magic escape
488 char obuf[4 + sizeof (struct winsize)];
494 ws.ws_row = htons(winsize.ws_row);
495 ws.ws_col = htons(winsize.ws_col);
496 ws.ws_xpixel = htons(winsize.ws_xpixel);
497 ws.ws_ypixel = htons(winsize.ws_ypixel);
498 bcopy(&ws, obuf + 4, sizeof(ws));
500 (void)write(rem, obuf, sizeof(obuf));
504 * reader: read from remote: line -> 1
510 int rcvcnt, rcvstate;
512 char rcvbuf[8 * 1024];
516 oob(int signo __unused)
520 char waste[BUFSIZ], mark;
523 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
527 * Urgent data not here yet. It may not be possible
528 * to send it yet if we are blocked for output and
529 * our input buffer is full.
531 if (rcvcnt < (int)sizeof(rcvbuf)) {
532 n = read(rem, rcvbuf + rcvcnt,
533 sizeof(rcvbuf) - rcvcnt);
538 n = read(rem, waste, sizeof(waste));
547 if (mark & TIOCPKT_WINDOW) {
548 /* Let server know about window size changes */
549 (void)kill(ppid, SIGUSR1);
551 if (!eight && (mark & TIOCPKT_NOSTOP)) {
552 (void)tcgetattr(0, &tty);
553 tty.c_iflag &= ~IXON;
554 (void)tcsetattr(0, TCSANOW, &tty);
556 if (!eight && (mark & TIOCPKT_DOSTOP)) {
557 (void)tcgetattr(0, &tty);
558 tty.c_iflag |= (deftty.c_iflag & IXON);
559 (void)tcsetattr(0, TCSANOW, &tty);
561 if (mark & TIOCPKT_FLUSHWRITE) {
562 (void)tcflush(1, TCIOFLUSH);
564 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
570 n = read(rem, waste, sizeof (waste));
575 * Don't want any pending data to be output, so clear the recv
576 * buffer. If we were hanging on a write when interrupted,
577 * don't want it to restart. If we were reading, restart
584 /* oob does not do FLUSHREAD (alas!) */
587 * If we filled the receive buffer while a read was pending, longjmp
588 * to the top to restart appropriately. Don't abort a pending write,
589 * however, or we won't know how much was written.
591 if (rcvd && rcvstate == READING)
595 /* reader: read from remote: line -> 1 */
604 (void)signal(SIGTTOU, SIG_IGN);
605 (void)signal(SIGURG, oob);
606 (void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
608 (void)fcntl(rem, F_SETOWN, pid);
609 (void)setjmp(rcvtop);
610 (void)sigsetmask(omask);
613 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
615 n = write(STDOUT_FILENO, bufp, remaining);
627 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
646 (void)tcsetattr(0, TCSANOW, &deftty);
649 (void)tcgetattr(0, &deftty);
651 /* This is loosely derived from sys/kern/tty_compat.c. */
652 tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
653 tty.c_iflag &= ~ICRNL;
654 tty.c_oflag &= ~OPOST;
658 tty.c_iflag &= IXOFF;
659 tty.c_cflag &= ~(CSIZE|PARENB);
662 (void)tcsetattr(0, TCSANOW, &tty);
671 lostpeer(int signo __unused)
673 (void)signal(SIGPIPE, SIG_IGN);
674 msg("\007connection closed");
678 /* copy SIGURGs to the child process via SIGUSR1. */
681 copytochild(int signo __unused)
683 (void)kill(child, SIGUSR1);
689 (void)fprintf(stderr, "rlogin: %s\r\n", str);
695 (void)fprintf(stderr,
696 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
702 getescape(const char *p)
707 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
709 /* otherwise, \nnn */
710 if (*p == '\\' && len >= 2 && len <= 4) {
711 val = strtol(++p, NULL, 8);
715 if (*p < '0' || *p > '8')
719 msg("illegal option value -- e");