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
40 * @(#) Copyright (c) 1983, 1990, 1993 The Regents of the University of California. All rights reserved.
41 * @(#)rlogin.c 8.1 (Berkeley) 6/6/93
42 * $FreeBSD: src/usr.bin/rlogin/rlogin.c,v 1.24.2.2 2002/07/19 18:03:41 ru Exp $
43 * $DragonFly: src/usr.bin/rlogin/rlogin.c,v 1.4 2004/08/30 18:06:50 eirikn Exp $
47 * rlogin - remote login
49 #include <sys/param.h>
50 #include <sys/socket.h>
52 #include <sys/resource.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/tcp.h>
77 #include <openssl/des.h>
83 Key_schedule schedule;
84 int use_kerberos = 1, doencrypt;
85 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
88 #ifndef TIOCPKT_WINDOW
89 #define TIOCPKT_WINDOW 0x80
92 /* concession to Sun */
97 int eight, litout, rem;
98 int family = PF_UNSPEC;
101 u_char escapechar = '~';
104 "0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
105 "1800", "2400", "4800", "9600", "19200", "38400", "57600", "115200"
106 #define MAX_SPEED_LENGTH (sizeof("115200") - 1)
111 unsigned short ws_row, ws_col;
112 unsigned short ws_xpixel, ws_ypixel;
115 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
117 struct winsize winsize;
119 void catch_child(int);
120 void copytochild(int);
121 void doit(long) __dead2;
122 void done(int) __dead2;
124 u_int getescape(char *);
130 void sendwindow(void);
134 void usage(void) __dead2;
139 int get_window_size(int, struct winsize *);
151 int argoff, ch, dflag, Dflag, one, uid;
152 char *host, *localname, *p, *user, term[1024];
156 struct sockaddr_storage ss;
159 argoff = dflag = Dflag = 0;
161 host = localname = user = NULL;
163 if ((p = strrchr(argv[0], '/')))
168 if (strcmp(p, "rlogin"))
171 /* handle "rlogin host flags" */
172 if (!host && argc > 2 && argv[1][0] != '-') {
178 #define OPTIONS "468DEKLde:i:k:l:x"
180 #define OPTIONS "468DEKLde:i:l:"
182 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
214 escapechar = getescape(optarg);
218 errx(1, "-i user: permission denied");
223 dest_realm = dst_realm_buf;
224 (void)strncpy(dest_realm, optarg, REALM_SZ);
243 /* if haven't gotten a host yet, do so */
244 if (!host && !(host = argv[optind++]))
250 if (!(pw = getpwuid(uid = getuid())))
251 errx(1, "unknown user id");
255 localname = pw->pw_name;
259 k = auth_getval("auth_list");
260 if (k && !strstr(k, "kerberos"))
263 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
266 warn("can't get entry for %s/tcp service",
267 doencrypt ? "eklogin" : "klogin");
272 sp = getservbyname("login", "tcp");
274 errx(1, "login/tcp: unknown service");
276 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
278 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
280 term[MAX_TERM_LENGTH] = '\0';
281 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
282 (void)strcat(term, "/");
283 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
286 (void)get_window_size(0, &winsize);
288 (void)signal(SIGPIPE, lostpeer);
289 /* will use SIGUSR1 for window size hack, so hold it off */
290 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
292 * We set SIGURG and SIGUSR1 below so that an
293 * incoming signal will be held pending rather than being
294 * discarded. Note that these routines will be ready to get
295 * a signal by the time that they are unblocked below.
297 (void)signal(SIGURG, copytochild);
298 (void)signal(SIGUSR1, writeroob);
305 if (dest_realm == NULL)
306 dest_realm = krb_realmofhost(host);
310 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
311 dest_realm, &cred, schedule);
312 des_set_key(&cred.session, schedule);
315 rem = krcmd(&host, sp->s_port, user, term, 0,
321 sp = getservbyname("login", "tcp");
323 errx(1, "unknown service login/tcp");
324 if (errno == ECONNREFUSED)
325 warn("remote host doesn't support Kerberos");
327 warn("can't provide Kerberos auth data");
328 newargv = malloc((argc + 2) * sizeof(*newargv));
331 newargv[0] = argv[0];
333 for(i = 1; i < argc; ++i)
334 newargv[i + 1] = argv[i];
335 newargv[argc + 1] = NULL;
336 execv(_PATH_RLOGIN, newargv);
341 errx(1, "the -x flag requires Kerberos authentication");
343 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0,
347 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
348 #endif /* KERBEROS */
354 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
357 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
358 warn("setsockopt NODELAY (ignored)");
361 one = IPTOS_LOWDELAY;
362 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
363 ss.ss_family == AF_INET) {
364 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
366 warn("setsockopt TOS (ignored)");
368 if (ss.ss_family == AF_INET)
369 warn("setsockopt getsockname failed");
376 int child, defflags, deflflags, tabflag;
377 char deferase, defkill;
379 struct ltchars defltc;
380 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
381 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
389 (void)ioctl(0, TIOCGETP, (char *)&sb);
390 defflags = sb.sg_flags;
391 tabflag = defflags & TBDELAY;
392 defflags &= ECHO | CRMOD;
393 deferase = sb.sg_erase;
394 defkill = sb.sg_kill;
395 (void)ioctl(0, TIOCLGET, &deflflags);
396 (void)ioctl(0, TIOCGETC, &deftc);
397 notc.t_startc = deftc.t_startc;
398 notc.t_stopc = deftc.t_stopc;
399 (void)ioctl(0, TIOCGLTC, &defltc);
400 (void)signal(SIGINT, SIG_IGN);
410 if (reader(omask) == 0) {
411 msg("connection closed.");
415 msg("\007connection closed.");
420 * We may still own the socket, and may have a pending SIGURG (or might
421 * receive one soon) that we really want to send to the reader. When
422 * one of these comes in, the trap copytochild simply copies such
423 * signals to the child. We can now unblock SIGURG and SIGUSR1
424 * that were set above.
426 (void)sigsetmask(omask);
427 (void)signal(SIGCHLD, catch_child);
429 msg("closed connection.");
433 /* trap a signal, unless it is being ignored. */
438 int omask = sigblock(sigmask(sig));
440 if (signal(sig, exit) == SIG_IGN)
441 (void)signal(sig, SIG_IGN);
442 (void)sigsetmask(omask);
453 /* make sure catch_child does not snap it up */
454 (void)signal(SIGCHLD, SIG_DFL);
455 if (kill(child, SIGKILL) >= 0)
456 while ((w = wait(&wstatus)) > 0 && w != child);
464 * This is called when the reader process gets the out-of-band (urgent)
465 * request to turn on the window-changing protocol.
471 if (dosigwinch == 0) {
473 (void)signal(SIGWINCH, sigwinch);
486 pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
489 /* if the child (reader) dies, just quit */
490 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
491 done((int)(status.w_termsig | status.w_retcode));
497 * writer: write to remote: 0 -> line.
499 * ~^Z suspend rlogin process.
500 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
505 register int bol, local, n;
508 bol = 1; /* beginning of line */
511 n = read(STDIN_FILENO, &c, 1);
513 if (n < 0 && errno == EINTR)
518 * If we're at the beginning of the line and recognize a
519 * command character, then we echo locally. Otherwise,
520 * characters are echo'd remotely. If the command character
521 * is doubled, this acts as a force and local echo is
526 if (!noescape && c == escapechar) {
532 if (c == '.' || c == deftc.t_eofc) {
536 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
546 (void)des_enc_write(rem,
547 (char *)&escapechar, 1,
548 schedule, &cred.session);
552 (void)write(rem, &escapechar, 1);
558 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
565 if (write(rem, &c, 1) == 0) {
569 bol = c == defkill || c == deftc.t_eofc ||
570 c == deftc.t_intrc || c == defltc.t_suspc ||
571 c == '\r' || c == '\n';
577 echo(register char c)
592 } else if (c == 0177) {
599 (void)write(STDOUT_FILENO, buf, p - buf);
611 (void)signal(SIGCHLD, SIG_IGN);
612 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
613 (void)signal(SIGCHLD, catch_child);
615 sigwinch(0); /* check for size changes */
624 if (dosigwinch && get_window_size(0, &ws) == 0 &&
625 bcmp(&ws, &winsize, sizeof(ws))) {
632 * Send the window size to the server via the magic escape
638 char obuf[4 + sizeof (struct winsize)];
640 wp = (struct winsize *)(obuf+4);
645 wp->ws_row = htons(winsize.ws_row);
646 wp->ws_col = htons(winsize.ws_col);
647 wp->ws_xpixel = htons(winsize.ws_xpixel);
648 wp->ws_ypixel = htons(winsize.ws_ypixel);
653 (void)des_enc_write(rem, obuf, sizeof(obuf),
654 schedule, &cred.session);
658 (void)write(rem, obuf, sizeof(obuf));
662 * reader: read from remote: line -> 1
668 int ppid, rcvcnt, rcvstate;
669 char rcvbuf[8 * 1024];
676 int atmark, n, out, rcvd;
677 char waste[BUFSIZ], mark;
681 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
685 * Urgent data not here yet. It may not be possible
686 * to send it yet if we are blocked for output and
687 * our input buffer is full.
689 if (rcvcnt < sizeof(rcvbuf)) {
690 n = read(rem, rcvbuf + rcvcnt,
691 sizeof(rcvbuf) - rcvcnt);
696 n = read(rem, waste, sizeof(waste));
705 if (mark & TIOCPKT_WINDOW) {
706 /* Let server know about window size changes */
707 (void)kill(ppid, SIGUSR1);
709 if (!eight && (mark & TIOCPKT_NOSTOP)) {
710 (void)ioctl(0, TIOCGETP, (char *)&sb);
711 sb.sg_flags &= ~CBREAK;
713 (void)ioctl(0, TIOCSETN, (char *)&sb);
716 (void)ioctl(0, TIOCSETC, (char *)¬c);
718 if (!eight && (mark & TIOCPKT_DOSTOP)) {
719 (void)ioctl(0, TIOCGETP, (char *)&sb);
721 sb.sg_flags |= CBREAK;
722 (void)ioctl(0, TIOCSETN, (char *)&sb);
723 notc.t_stopc = deftc.t_stopc;
724 notc.t_startc = deftc.t_startc;
725 (void)ioctl(0, TIOCSETC, (char *)¬c);
727 if (mark & TIOCPKT_FLUSHWRITE) {
728 (void)ioctl(1, TIOCFLUSH, (char *)&out);
730 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
736 n = read(rem, waste, sizeof (waste));
741 * Don't want any pending data to be output, so clear the recv
742 * buffer. If we were hanging on a write when interrupted,
743 * don't want it to restart. If we were reading, restart
750 /* oob does not do FLUSHREAD (alas!) */
753 * If we filled the receive buffer while a read was pending, longjmp
754 * to the top to restart appropriately. Don't abort a pending write,
755 * however, or we won't know how much was written.
757 if (rcvd && rcvstate == READING)
761 /* reader: read from remote: line -> 1 */
766 int pid, n, remaining;
769 #if BSD >= 43 || defined(SUNOS4)
770 pid = getpid(); /* modern systems use positives for pid */
772 pid = -getpid(); /* old broken systems use negatives */
774 (void)signal(SIGTTOU, SIG_IGN);
775 (void)signal(SIGURG, oob);
777 (void)fcntl(rem, F_SETOWN, pid);
778 (void)setjmp(rcvtop);
779 (void)sigsetmask(omask);
782 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
784 n = write(STDOUT_FILENO, bufp, remaining);
799 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
800 schedule, &cred.session);
804 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
825 (void)ioctl(0, TIOCGETP, (char *)&sb);
826 (void)ioctl(0, TIOCLGET, (char *)&lflags);
829 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
830 sb.sg_flags |= defflags|tabflag;
833 sb.sg_kill = defkill;
834 sb.sg_erase = deferase;
838 sb.sg_flags |= (eight ? RAW : CBREAK);
839 sb.sg_flags &= ~defflags;
840 /* preserve tab delays, but turn off XTABS */
841 if ((sb.sg_flags & TBDELAY) == XTABS)
842 sb.sg_flags &= ~TBDELAY;
845 sb.sg_kill = sb.sg_erase = -1;
852 (void)ioctl(0, TIOCSLTC, (char *)ltc);
853 (void)ioctl(0, TIOCSETC, (char *)tc);
854 (void)ioctl(0, TIOCSETN, (char *)&sb);
855 (void)ioctl(0, TIOCLSET, (char *)&lflags);
862 (void)signal(SIGPIPE, SIG_IGN);
863 msg("\007connection closed.");
867 /* copy SIGURGs to the child process. */
872 (void)kill(child, SIGURG);
879 (void)fprintf(stderr, "rlogin: %s\r\n", str);
885 (void)fprintf(stderr,
886 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
889 "8DEKLdx", " [-k realm] ");
891 "8DEKLd", " [-k realm] ");
900 * The following routine provides compatibility (such as it is) between older
901 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
905 get_window_size(fd, wp)
912 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
914 wp->ws_row = ts.ts_lines;
915 wp->ws_col = ts.ts_cols;
929 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
931 /* otherwise, \nnn */
932 if (*p == '\\' && len >= 2 && len <= 4) {
933 val = strtol(++p, NULL, 8);
937 if (*p < '0' || *p > '8')
941 msg("illegal option value -- e");