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.30 2002/04/28 11:16:43 markm Exp $
43 * $DragonFly: src/usr.bin/rlogin/rlogin.c,v 1.7 2005/02/15 01:24:49 cpressey 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>
76 #include <openssl/des.h>
82 Key_schedule schedule;
83 int use_kerberos = 1, doencrypt;
84 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
87 #ifndef TIOCPKT_WINDOW
88 #define TIOCPKT_WINDOW 0x80
91 /* concession to Sun */
96 int eight, litout, rem;
97 int family = PF_UNSPEC;
100 u_char escapechar = '~';
102 const char *speeds[] = {
103 "0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
104 "1800", "2400", "4800", "9600", "19200", "38400", "57600", "115200"
105 #define MAX_SPEED_LENGTH (sizeof("115200") - 1)
108 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
109 struct winsize winsize;
111 void catch_child(int);
112 void copytochild(int);
113 void doit(long) __dead2;
114 void done(int) __dead2;
116 u_int getescape(char *);
119 void msg(const char *);
122 void sendwindow(void);
126 void usage(void) __dead2;
131 main(int argc, char *argv[])
137 int argoff, ch, dflag, Dflag, one, uid;
138 char *host, *localname, *p, *user, term[1024];
142 struct sockaddr_storage ss;
145 argoff = dflag = Dflag = 0;
147 host = localname = user = NULL;
149 if ((p = strrchr(argv[0], '/')))
154 if (strcmp(p, "rlogin"))
157 /* handle "rlogin host flags" */
158 if (!host && argc > 2 && argv[1][0] != '-') {
164 #define OPTIONS "468DEKLde:i:k:l:x"
166 #define OPTIONS "468DEKLde:i:l:"
168 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
200 escapechar = getescape(optarg);
204 errx(1, "-i user: permission denied");
209 dest_realm = dst_realm_buf;
210 (void)strncpy(dest_realm, optarg, REALM_SZ);
229 /* if haven't gotten a host yet, do so */
230 if (!host && !(host = argv[optind++]))
236 if (!(pw = getpwuid(uid = getuid())))
237 errx(1, "unknown user id");
241 localname = pw->pw_name;
245 k = auth_getval("auth_list");
246 if (k && !strstr(k, "kerberos"))
249 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
252 warn("can't get entry for %s/tcp service",
253 doencrypt ? "eklogin" : "klogin");
258 sp = getservbyname("login", "tcp");
260 errx(1, "login/tcp: unknown service");
262 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
264 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
266 term[MAX_TERM_LENGTH] = '\0';
267 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
268 (void)strcat(term, "/");
269 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
272 (void)get_window_size(0, &winsize);
274 (void)signal(SIGPIPE, lostpeer);
275 /* will use SIGUSR1 for window size hack, so hold it off */
276 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
278 * We set SIGURG and SIGUSR1 below so that an
279 * incoming signal will be held pending rather than being
280 * discarded. Note that these routines will be ready to get
281 * a signal by the time that they are unblocked below.
283 (void)signal(SIGURG, copytochild);
284 (void)signal(SIGUSR1, writeroob);
291 if (dest_realm == NULL)
292 dest_realm = krb_realmofhost(host);
296 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
297 dest_realm, &cred, schedule);
298 des_set_key(&cred.session, schedule);
301 rem = krcmd(&host, sp->s_port, user, term, 0,
307 sp = getservbyname("login", "tcp");
309 errx(1, "unknown service login/tcp");
310 if (errno == ECONNREFUSED)
311 warn("remote host doesn't support Kerberos");
313 warn("can't provide Kerberos auth data");
314 newargv = malloc((argc + 2) * sizeof(*newargv));
317 newargv[0] = argv[0];
319 for(i = 1; i < argc; ++i)
320 newargv[i + 1] = argv[i];
321 newargv[argc + 1] = NULL;
322 execv(_PATH_RLOGIN, newargv);
327 errx(1, "the -x flag requires Kerberos authentication");
329 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0,
333 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
334 #endif /* KERBEROS */
340 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
343 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
344 warn("setsockopt NODELAY (ignored)");
347 one = IPTOS_LOWDELAY;
348 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
349 ss.ss_family == AF_INET) {
350 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
352 warn("setsockopt TOS (ignored)");
354 if (ss.ss_family == AF_INET)
355 warn("setsockopt getsockname failed");
362 int child, defflags, deflflags, tabflag;
363 char deferase, defkill;
365 struct ltchars defltc;
366 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
367 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
374 (void)ioctl(0, TIOCGETP, (char *)&sb);
375 defflags = sb.sg_flags;
376 tabflag = defflags & TBDELAY;
377 defflags &= ECHO | CRMOD;
378 deferase = sb.sg_erase;
379 defkill = sb.sg_kill;
380 (void)ioctl(0, TIOCLGET, &deflflags);
381 (void)ioctl(0, TIOCGETC, &deftc);
382 notc.t_startc = deftc.t_startc;
383 notc.t_stopc = deftc.t_stopc;
384 (void)ioctl(0, TIOCGLTC, &defltc);
385 (void)signal(SIGINT, SIG_IGN);
395 if (reader(omask) == 0) {
396 msg("connection closed.");
400 msg("\007connection closed.");
405 * We may still own the socket, and may have a pending SIGURG (or might
406 * receive one soon) that we really want to send to the reader. When
407 * one of these comes in, the trap copytochild simply copies such
408 * signals to the child. We can now unblock SIGURG and SIGUSR1
409 * that were set above.
411 (void)sigsetmask(omask);
412 (void)signal(SIGCHLD, catch_child);
414 msg("closed connection.");
418 /* trap a signal, unless it is being ignored. */
422 int omask = sigblock(sigmask(sig));
424 if (signal(sig, exit) == SIG_IGN)
425 (void)signal(sig, SIG_IGN);
426 (void)sigsetmask(omask);
436 /* make sure catch_child does not snap it up */
437 (void)signal(SIGCHLD, SIG_DFL);
438 if (kill(child, SIGKILL) >= 0)
439 while ((w = wait(&wstatus)) > 0 && w != child);
447 * This is called when the reader process gets the out-of-band (urgent)
448 * request to turn on the window-changing protocol.
451 writeroob(int signo __unused)
453 if (dosigwinch == 0) {
455 (void)signal(SIGWINCH, sigwinch);
461 catch_child(int signo __unused)
467 pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
470 /* if the child (reader) dies, just quit */
471 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
472 done((int)(status.w_termsig | status.w_retcode));
478 * writer: write to remote: 0 -> line.
480 * ~^Z suspend rlogin process.
481 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
489 bol = 1; /* beginning of line */
492 n = read(STDIN_FILENO, &c, 1);
494 if (n < 0 && errno == EINTR)
499 * If we're at the beginning of the line and recognize a
500 * command character, then we echo locally. Otherwise,
501 * characters are echo'd remotely. If the command character
502 * is doubled, this acts as a force and local echo is
507 if (!noescape && c == escapechar) {
513 if (c == '.' || c == deftc.t_eofc) {
517 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
527 (void)des_enc_write(rem,
528 (char *)&escapechar, 1,
529 schedule, &cred.session);
533 (void)write(rem, &escapechar, 1);
539 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
546 if (write(rem, &c, 1) == 0) {
550 bol = c == defkill || c == deftc.t_eofc ||
551 c == deftc.t_intrc || c == defltc.t_suspc ||
552 c == '\r' || c == '\n';
568 } else if (c == 0177) {
575 (void)write(STDOUT_FILENO, buf, p - buf);
582 (void)signal(SIGCHLD, SIG_IGN);
583 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
584 (void)signal(SIGCHLD, catch_child);
586 sigwinch(0); /* check for size changes */
590 sigwinch(int signo __unused)
594 if (dosigwinch && get_window_size(0, &ws) == 0 &&
595 bcmp(&ws, &winsize, sizeof(ws))) {
602 * Send the window size to the server via the magic escape
608 char obuf[4 + sizeof (struct winsize)];
610 wp = (struct winsize *)(obuf+4);
615 wp->ws_row = htons(winsize.ws_row);
616 wp->ws_col = htons(winsize.ws_col);
617 wp->ws_xpixel = htons(winsize.ws_xpixel);
618 wp->ws_ypixel = htons(winsize.ws_ypixel);
623 (void)des_enc_write(rem, obuf, sizeof(obuf),
624 schedule, &cred.session);
628 (void)write(rem, obuf, sizeof(obuf));
632 * reader: read from remote: line -> 1
638 int ppid, rcvcnt, rcvstate;
639 char rcvbuf[8 * 1024];
642 oob(int signo __unused)
645 int atmark, n, out, rcvd;
646 char waste[BUFSIZ], mark;
650 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
654 * Urgent data not here yet. It may not be possible
655 * to send it yet if we are blocked for output and
656 * our input buffer is full.
658 if (rcvcnt < (int)sizeof(rcvbuf)) {
659 n = read(rem, rcvbuf + rcvcnt,
660 sizeof(rcvbuf) - rcvcnt);
665 n = read(rem, waste, sizeof(waste));
674 if (mark & TIOCPKT_WINDOW) {
675 /* Let server know about window size changes */
676 (void)kill(ppid, SIGUSR1);
678 if (!eight && (mark & TIOCPKT_NOSTOP)) {
679 (void)ioctl(0, TIOCGETP, (char *)&sb);
680 sb.sg_flags &= ~CBREAK;
682 (void)ioctl(0, TIOCSETN, (char *)&sb);
685 (void)ioctl(0, TIOCSETC, (char *)¬c);
687 if (!eight && (mark & TIOCPKT_DOSTOP)) {
688 (void)ioctl(0, TIOCGETP, (char *)&sb);
690 sb.sg_flags |= CBREAK;
691 (void)ioctl(0, TIOCSETN, (char *)&sb);
692 notc.t_stopc = deftc.t_stopc;
693 notc.t_startc = deftc.t_startc;
694 (void)ioctl(0, TIOCSETC, (char *)¬c);
696 if (mark & TIOCPKT_FLUSHWRITE) {
697 (void)ioctl(1, TIOCFLUSH, (char *)&out);
699 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
705 n = read(rem, waste, sizeof (waste));
710 * Don't want any pending data to be output, so clear the recv
711 * buffer. If we were hanging on a write when interrupted,
712 * don't want it to restart. If we were reading, restart
719 /* oob does not do FLUSHREAD (alas!) */
722 * If we filled the receive buffer while a read was pending, longjmp
723 * to the top to restart appropriately. Don't abort a pending write,
724 * however, or we won't know how much was written.
726 if (rcvd && rcvstate == READING)
730 /* reader: read from remote: line -> 1 */
734 int pid, n, remaining;
737 #if BSD >= 43 || defined(SUNOS4)
738 pid = getpid(); /* modern systems use positives for pid */
740 pid = -getpid(); /* old broken systems use negatives */
742 (void)signal(SIGTTOU, SIG_IGN);
743 (void)signal(SIGURG, oob);
744 (void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
746 (void)fcntl(rem, F_SETOWN, pid);
747 (void)setjmp(rcvtop);
748 (void)sigsetmask(omask);
751 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
753 n = write(STDOUT_FILENO, bufp, remaining);
768 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
769 schedule, &cred.session);
773 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
793 (void)ioctl(0, TIOCGETP, (char *)&sb);
794 (void)ioctl(0, TIOCLGET, (char *)&lflags);
797 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
798 sb.sg_flags |= defflags|tabflag;
801 sb.sg_kill = defkill;
802 sb.sg_erase = deferase;
806 sb.sg_flags |= (eight ? RAW : CBREAK);
807 sb.sg_flags &= ~defflags;
808 /* preserve tab delays, but turn off XTABS */
809 if ((sb.sg_flags & TBDELAY) == XTABS)
810 sb.sg_flags &= ~TBDELAY;
813 sb.sg_kill = sb.sg_erase = -1;
820 (void)ioctl(0, TIOCSLTC, (char *)ltc);
821 (void)ioctl(0, TIOCSETC, (char *)tc);
822 (void)ioctl(0, TIOCSETN, (char *)&sb);
823 (void)ioctl(0, TIOCLSET, (char *)&lflags);
827 lostpeer(int signo __unused)
829 (void)signal(SIGPIPE, SIG_IGN);
830 msg("\007connection closed.");
834 /* copy SIGURGs to the child process via SIGUSR1. */
836 copytochild(int signo __unused)
838 (void)kill(child, SIGUSR1);
844 (void)fprintf(stderr, "rlogin: %s\r\n", str);
850 (void)fprintf(stderr,
851 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
854 "8DEKLdx", " [-k realm] ");
856 "8DEKLd", " [-k realm] ");
870 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
872 /* otherwise, \nnn */
873 if (*p == '\\' && len >= 2 && len <= 4) {
874 val = strtol(++p, NULL, 8);
878 if (*p < '0' || *p > '8')
882 msg("illegal option value -- e");