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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1983, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)rlogin.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/rlogin/rlogin.c,v 1.30 2002/04/28 11:16:43 markm Exp $
42 * rlogin - remote login
44 #include <sys/param.h>
45 #include <sys/socket.h>
47 #include <sys/resource.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
71 #include <openssl/des.h>
77 Key_schedule schedule;
78 int use_kerberos = 1, doencrypt;
79 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
82 #ifndef TIOCPKT_WINDOW
83 #define TIOCPKT_WINDOW 0x80
86 /* concession to Sun */
91 int eight, litout, rem;
92 int family = PF_UNSPEC;
95 u_char escapechar = '~';
97 const char *speeds[] = {
98 "0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
99 "1800", "2400", "4800", "9600", "19200", "38400", "57600", "115200"
100 #define MAX_SPEED_LENGTH (sizeof("115200") - 1)
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(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, uid;
133 char *host, *localname, *p, *user, term[1024];
137 struct sockaddr_storage ss;
140 argoff = dflag = Dflag = 0;
142 host = localname = user = NULL;
144 if ((p = strrchr(argv[0], '/')))
149 if (strcmp(p, "rlogin"))
152 /* handle "rlogin host flags" */
153 if (!host && argc > 2 && argv[1][0] != '-') {
159 #define OPTIONS "468DEKLde:i:k:l:x"
161 #define OPTIONS "468DEKLde:i:l:"
163 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
195 escapechar = getescape(optarg);
199 errx(1, "-i user: permission denied");
204 dest_realm = dst_realm_buf;
205 (void)strncpy(dest_realm, optarg, REALM_SZ);
224 /* if haven't gotten a host yet, do so */
225 if (!host && !(host = argv[optind++]))
231 if (!(pw = getpwuid(uid = getuid())))
232 errx(1, "unknown user id");
236 localname = pw->pw_name;
240 k = auth_getval("auth_list");
241 if (k && !strstr(k, "kerberos"))
244 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
247 warn("can't get entry for %s/tcp service",
248 doencrypt ? "eklogin" : "klogin");
253 sp = getservbyname("login", "tcp");
255 errx(1, "login/tcp: unknown service");
257 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
259 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
261 term[MAX_TERM_LENGTH] = '\0';
262 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
263 (void)strcat(term, "/");
264 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
267 (void)get_window_size(0, &winsize);
269 (void)signal(SIGPIPE, lostpeer);
270 /* will use SIGUSR1 for window size hack, so hold it off */
271 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
273 * We set SIGURG and SIGUSR1 below so that an
274 * incoming signal will be held pending rather than being
275 * discarded. Note that these routines will be ready to get
276 * a signal by the time that they are unblocked below.
278 (void)signal(SIGURG, copytochild);
279 (void)signal(SIGUSR1, writeroob);
286 if (dest_realm == NULL)
287 dest_realm = krb_realmofhost(host);
291 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
292 dest_realm, &cred, schedule);
293 des_set_key(&cred.session, schedule);
296 rem = krcmd(&host, sp->s_port, user, term, 0,
302 sp = getservbyname("login", "tcp");
304 errx(1, "unknown service login/tcp");
305 if (errno == ECONNREFUSED)
306 warn("remote host doesn't support Kerberos");
308 warn("can't provide Kerberos auth data");
309 newargv = malloc((argc + 2) * sizeof(*newargv));
312 newargv[0] = argv[0];
314 for(i = 1; i < argc; ++i)
315 newargv[i + 1] = argv[i];
316 newargv[argc + 1] = NULL;
317 execv(_PATH_RLOGIN, newargv);
322 errx(1, "the -x flag requires Kerberos authentication");
324 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0,
328 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
329 #endif /* KERBEROS */
335 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
338 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
339 warn("setsockopt NODELAY (ignored)");
342 one = IPTOS_LOWDELAY;
343 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
344 ss.ss_family == AF_INET) {
345 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
347 warn("setsockopt TOS (ignored)");
349 if (ss.ss_family == AF_INET)
350 warn("setsockopt getsockname failed");
357 int child, defflags, deflflags, tabflag;
358 char deferase, defkill;
360 struct ltchars defltc;
361 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
362 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
369 (void)ioctl(0, TIOCGETP, (char *)&sb);
370 defflags = sb.sg_flags;
371 tabflag = defflags & TBDELAY;
372 defflags &= ECHO | CRMOD;
373 deferase = sb.sg_erase;
374 defkill = sb.sg_kill;
375 (void)ioctl(0, TIOCLGET, &deflflags);
376 (void)ioctl(0, TIOCGETC, &deftc);
377 notc.t_startc = deftc.t_startc;
378 notc.t_stopc = deftc.t_stopc;
379 (void)ioctl(0, TIOCGLTC, &defltc);
380 (void)signal(SIGINT, SIG_IGN);
390 if (reader(omask) == 0) {
391 msg("connection closed.");
395 msg("\007connection closed.");
400 * We may still own the socket, and may have a pending SIGURG (or might
401 * receive one soon) that we really want to send to the reader. When
402 * one of these comes in, the trap copytochild simply copies such
403 * signals to the child. We can now unblock SIGURG and SIGUSR1
404 * that were set above.
406 (void)sigsetmask(omask);
407 (void)signal(SIGCHLD, catch_child);
409 msg("closed connection.");
413 /* trap a signal, unless it is being ignored. */
417 int omask = sigblock(sigmask(sig));
419 if (signal(sig, exit) == SIG_IGN)
420 (void)signal(sig, SIG_IGN);
421 (void)sigsetmask(omask);
431 /* make sure catch_child does not snap it up */
432 (void)signal(SIGCHLD, SIG_DFL);
433 if (kill(child, SIGKILL) >= 0)
434 while ((w = wait(&wstatus)) > 0 && w != child);
442 * This is called when the reader process gets the out-of-band (urgent)
443 * request to turn on the window-changing protocol.
446 writeroob(int signo __unused)
448 if (dosigwinch == 0) {
450 (void)signal(SIGWINCH, sigwinch);
456 catch_child(int signo __unused)
462 pid = wait3(&status, WNOHANG|WUNTRACED, NULL);
465 /* if the child (reader) dies, just quit */
466 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
467 done(WTERMSIG(status) | WEXITSTATUS(status));
473 * writer: write to remote: 0 -> line.
475 * ~^Z suspend rlogin process.
476 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
484 bol = 1; /* beginning of line */
487 n = read(STDIN_FILENO, &c, 1);
489 if (n < 0 && errno == EINTR)
494 * If we're at the beginning of the line and recognize a
495 * command character, then we echo locally. Otherwise,
496 * characters are echo'd remotely. If the command character
497 * is doubled, this acts as a force and local echo is
502 if (!noescape && c == escapechar) {
508 if (c == '.' || c == deftc.t_eofc) {
512 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
522 (void)des_enc_write(rem,
523 (char *)&escapechar, 1,
524 schedule, &cred.session);
528 (void)write(rem, &escapechar, 1);
534 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
541 if (write(rem, &c, 1) == 0) {
545 bol = c == defkill || c == deftc.t_eofc ||
546 c == deftc.t_intrc || c == defltc.t_suspc ||
547 c == '\r' || c == '\n';
563 } else if (c == 0177) {
570 (void)write(STDOUT_FILENO, buf, p - buf);
577 (void)signal(SIGCHLD, SIG_IGN);
578 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
579 (void)signal(SIGCHLD, catch_child);
581 sigwinch(0); /* check for size changes */
585 sigwinch(int signo __unused)
589 if (dosigwinch && get_window_size(0, &ws) == 0 &&
590 bcmp(&ws, &winsize, sizeof(ws))) {
597 * Send the window size to the server via the magic escape
603 char obuf[4 + sizeof (struct winsize)];
605 wp = (struct winsize *)(obuf+4);
610 wp->ws_row = htons(winsize.ws_row);
611 wp->ws_col = htons(winsize.ws_col);
612 wp->ws_xpixel = htons(winsize.ws_xpixel);
613 wp->ws_ypixel = htons(winsize.ws_ypixel);
618 (void)des_enc_write(rem, obuf, sizeof(obuf),
619 schedule, &cred.session);
623 (void)write(rem, obuf, sizeof(obuf));
627 * reader: read from remote: line -> 1
633 int ppid, rcvcnt, rcvstate;
634 char rcvbuf[8 * 1024];
637 oob(int signo __unused)
640 int atmark, n, out, rcvd;
641 char waste[BUFSIZ], mark;
645 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
649 * Urgent data not here yet. It may not be possible
650 * to send it yet if we are blocked for output and
651 * our input buffer is full.
653 if (rcvcnt < (int)sizeof(rcvbuf)) {
654 n = read(rem, rcvbuf + rcvcnt,
655 sizeof(rcvbuf) - rcvcnt);
660 n = read(rem, waste, sizeof(waste));
669 if (mark & TIOCPKT_WINDOW) {
670 /* Let server know about window size changes */
671 (void)kill(ppid, SIGUSR1);
673 if (!eight && (mark & TIOCPKT_NOSTOP)) {
674 (void)ioctl(0, TIOCGETP, (char *)&sb);
675 sb.sg_flags &= ~CBREAK;
677 (void)ioctl(0, TIOCSETN, (char *)&sb);
680 (void)ioctl(0, TIOCSETC, (char *)¬c);
682 if (!eight && (mark & TIOCPKT_DOSTOP)) {
683 (void)ioctl(0, TIOCGETP, (char *)&sb);
685 sb.sg_flags |= CBREAK;
686 (void)ioctl(0, TIOCSETN, (char *)&sb);
687 notc.t_stopc = deftc.t_stopc;
688 notc.t_startc = deftc.t_startc;
689 (void)ioctl(0, TIOCSETC, (char *)¬c);
691 if (mark & TIOCPKT_FLUSHWRITE) {
692 (void)ioctl(1, TIOCFLUSH, (char *)&out);
694 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
700 n = read(rem, waste, sizeof (waste));
705 * Don't want any pending data to be output, so clear the recv
706 * buffer. If we were hanging on a write when interrupted,
707 * don't want it to restart. If we were reading, restart
714 /* oob does not do FLUSHREAD (alas!) */
717 * If we filled the receive buffer while a read was pending, longjmp
718 * to the top to restart appropriately. Don't abort a pending write,
719 * however, or we won't know how much was written.
721 if (rcvd && rcvstate == READING)
725 /* reader: read from remote: line -> 1 */
729 int pid, n, remaining;
732 #if BSD >= 43 || defined(SUNOS4)
733 pid = getpid(); /* modern systems use positives for pid */
735 pid = -getpid(); /* old broken systems use negatives */
737 (void)signal(SIGTTOU, SIG_IGN);
738 (void)signal(SIGURG, oob);
739 (void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
741 (void)fcntl(rem, F_SETOWN, pid);
742 (void)setjmp(rcvtop);
743 (void)sigsetmask(omask);
746 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
748 n = write(STDOUT_FILENO, bufp, remaining);
763 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
764 schedule, &cred.session);
768 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
788 (void)ioctl(0, TIOCGETP, (char *)&sb);
789 (void)ioctl(0, TIOCLGET, (char *)&lflags);
792 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
793 sb.sg_flags |= defflags|tabflag;
796 sb.sg_kill = defkill;
797 sb.sg_erase = deferase;
801 sb.sg_flags |= (eight ? RAW : CBREAK);
802 sb.sg_flags &= ~defflags;
803 /* preserve tab delays, but turn off XTABS */
804 if ((sb.sg_flags & TBDELAY) == XTABS)
805 sb.sg_flags &= ~TBDELAY;
808 sb.sg_kill = sb.sg_erase = -1;
815 (void)ioctl(0, TIOCSLTC, (char *)ltc);
816 (void)ioctl(0, TIOCSETC, (char *)tc);
817 (void)ioctl(0, TIOCSETN, (char *)&sb);
818 (void)ioctl(0, TIOCLSET, (char *)&lflags);
822 lostpeer(int signo __unused)
824 (void)signal(SIGPIPE, SIG_IGN);
825 msg("\007connection closed.");
829 /* copy SIGURGs to the child process via SIGUSR1. */
831 copytochild(int signo __unused)
833 (void)kill(child, SIGUSR1);
839 (void)fprintf(stderr, "rlogin: %s\r\n", str);
845 (void)fprintf(stderr,
846 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
849 "8DEKLdx", " [-k realm] ");
851 "8DEKLd", " [-k realm] ");
865 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
867 /* otherwise, \nnn */
868 if (*p == '\\' && len >= 2 && len <= 4) {
869 val = strtol(++p, NULL, 8);
873 if (*p < '0' || *p > '8')
877 msg("illegal option value -- e");