2 * Copyright (c) 1983, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)rlogin.c 8.1 (Berkeley) 6/6/93";
42 static const char rcsid[] =
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>
79 #include "../../bin/rcp/pathnames.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 __P((int));
120 void copytochild __P((int));
121 void doit __P((long)) __dead2;
122 void done __P((int)) __dead2;
123 void echo __P((char));
124 u_int getescape __P((char *));
125 void lostpeer __P((int));
126 void mode __P((int));
127 void msg __P((char *));
129 int reader __P((int));
130 void sendwindow __P((void));
131 void setsignal __P((int));
132 void sigwinch __P((int));
133 void stop __P((char));
134 void usage __P((void)) __dead2;
135 void writer __P((void));
136 void writeroob __P((int));
139 int get_window_size __P((int, struct winsize *));
153 int argoff, ch, dflag, Dflag, one, uid;
154 char *host, *localname, *p, *user, term[1024];
158 struct sockaddr_storage ss;
161 argoff = dflag = Dflag = 0;
163 host = localname = user = NULL;
165 if ((p = rindex(argv[0], '/')))
170 if (strcmp(p, "rlogin"))
173 /* handle "rlogin host flags" */
174 if (!host && argc > 2 && argv[1][0] != '-') {
180 #define OPTIONS "468DEKLde:i:k:l:x"
182 #define OPTIONS "468DEKLde:i:l:"
184 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
216 escapechar = getescape(optarg);
220 errx(1, "-i user: permission denied");
225 dest_realm = dst_realm_buf;
226 (void)strncpy(dest_realm, optarg, REALM_SZ);
245 /* if haven't gotten a host yet, do so */
246 if (!host && !(host = argv[optind++]))
252 if (!(pw = getpwuid(uid = getuid())))
253 errx(1, "unknown user id");
257 localname = pw->pw_name;
261 k = auth_getval("auth_list");
262 if (k && !strstr(k, "kerberos"))
265 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
268 warn("can't get entry for %s/tcp service",
269 doencrypt ? "eklogin" : "klogin");
274 sp = getservbyname("login", "tcp");
276 errx(1, "login/tcp: unknown service");
278 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
280 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
282 term[MAX_TERM_LENGTH] = '\0';
283 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
284 (void)strcat(term, "/");
285 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
288 (void)get_window_size(0, &winsize);
290 (void)signal(SIGPIPE, lostpeer);
291 /* will use SIGUSR1 for window size hack, so hold it off */
292 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
294 * We set SIGURG and SIGUSR1 below so that an
295 * incoming signal will be held pending rather than being
296 * discarded. Note that these routines will be ready to get
297 * a signal by the time that they are unblocked below.
299 (void)signal(SIGURG, copytochild);
300 (void)signal(SIGUSR1, writeroob);
307 if (dest_realm == NULL)
308 dest_realm = krb_realmofhost(host);
312 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
313 dest_realm, &cred, schedule);
314 des_set_key(&cred.session, schedule);
317 rem = krcmd(&host, sp->s_port, user, term, 0,
323 sp = getservbyname("login", "tcp");
325 errx(1, "unknown service login/tcp");
326 if (errno == ECONNREFUSED)
327 warn("remote host doesn't support Kerberos");
329 warn("can't provide Kerberos auth data");
330 newargv = malloc((argc + 2) * sizeof(*newargv));
333 newargv[0] = argv[0];
335 for(i = 1; i < argc; ++i)
336 newargv[i + 1] = argv[i];
337 newargv[argc + 1] = NULL;
338 execv(_PATH_RLOGIN, newargv);
343 errx(1, "the -x flag requires Kerberos authentication");
345 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0,
349 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
350 #endif /* KERBEROS */
356 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
359 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
360 warn("setsockopt NODELAY (ignored)");
363 one = IPTOS_LOWDELAY;
364 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
365 ss.ss_family == AF_INET) {
366 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
368 warn("setsockopt TOS (ignored)");
370 if (ss.ss_family == AF_INET)
371 warn("setsockopt getsockname failed");
378 int child, defflags, deflflags, tabflag;
379 char deferase, defkill;
381 struct ltchars defltc;
382 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
383 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
391 (void)ioctl(0, TIOCGETP, (char *)&sb);
392 defflags = sb.sg_flags;
393 tabflag = defflags & TBDELAY;
394 defflags &= ECHO | CRMOD;
395 deferase = sb.sg_erase;
396 defkill = sb.sg_kill;
397 (void)ioctl(0, TIOCLGET, &deflflags);
398 (void)ioctl(0, TIOCGETC, &deftc);
399 notc.t_startc = deftc.t_startc;
400 notc.t_stopc = deftc.t_stopc;
401 (void)ioctl(0, TIOCGLTC, &defltc);
402 (void)signal(SIGINT, SIG_IGN);
412 if (reader(omask) == 0) {
413 msg("connection closed.");
417 msg("\007connection closed.");
422 * We may still own the socket, and may have a pending SIGURG (or might
423 * receive one soon) that we really want to send to the reader. When
424 * one of these comes in, the trap copytochild simply copies such
425 * signals to the child. We can now unblock SIGURG and SIGUSR1
426 * that were set above.
428 (void)sigsetmask(omask);
429 (void)signal(SIGCHLD, catch_child);
431 msg("closed connection.");
435 /* trap a signal, unless it is being ignored. */
440 int omask = sigblock(sigmask(sig));
442 if (signal(sig, exit) == SIG_IGN)
443 (void)signal(sig, SIG_IGN);
444 (void)sigsetmask(omask);
455 /* make sure catch_child does not snap it up */
456 (void)signal(SIGCHLD, SIG_DFL);
457 if (kill(child, SIGKILL) >= 0)
458 while ((w = wait(&wstatus)) > 0 && w != child);
466 * This is called when the reader process gets the out-of-band (urgent)
467 * request to turn on the window-changing protocol.
473 if (dosigwinch == 0) {
475 (void)signal(SIGWINCH, sigwinch);
488 pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
491 /* if the child (reader) dies, just quit */
492 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
493 done((int)(status.w_termsig | status.w_retcode));
499 * writer: write to remote: 0 -> line.
501 * ~^Z suspend rlogin process.
502 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
507 register int bol, local, n;
510 bol = 1; /* beginning of line */
513 n = read(STDIN_FILENO, &c, 1);
515 if (n < 0 && errno == EINTR)
520 * If we're at the beginning of the line and recognize a
521 * command character, then we echo locally. Otherwise,
522 * characters are echo'd remotely. If the command character
523 * is doubled, this acts as a force and local echo is
528 if (!noescape && c == escapechar) {
534 if (c == '.' || c == deftc.t_eofc) {
538 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
548 (void)des_enc_write(rem,
549 (char *)&escapechar, 1,
550 schedule, &cred.session);
554 (void)write(rem, &escapechar, 1);
560 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
567 if (write(rem, &c, 1) == 0) {
571 bol = c == defkill || c == deftc.t_eofc ||
572 c == deftc.t_intrc || c == defltc.t_suspc ||
573 c == '\r' || c == '\n';
579 echo(register char c)
594 } else if (c == 0177) {
601 (void)write(STDOUT_FILENO, buf, p - buf);
613 (void)signal(SIGCHLD, SIG_IGN);
614 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
615 (void)signal(SIGCHLD, catch_child);
617 sigwinch(0); /* check for size changes */
626 if (dosigwinch && get_window_size(0, &ws) == 0 &&
627 bcmp(&ws, &winsize, sizeof(ws))) {
634 * Send the window size to the server via the magic escape
640 char obuf[4 + sizeof (struct winsize)];
642 wp = (struct winsize *)(obuf+4);
647 wp->ws_row = htons(winsize.ws_row);
648 wp->ws_col = htons(winsize.ws_col);
649 wp->ws_xpixel = htons(winsize.ws_xpixel);
650 wp->ws_ypixel = htons(winsize.ws_ypixel);
655 (void)des_enc_write(rem, obuf, sizeof(obuf),
656 schedule, &cred.session);
660 (void)write(rem, obuf, sizeof(obuf));
664 * reader: read from remote: line -> 1
670 int ppid, rcvcnt, rcvstate;
671 char rcvbuf[8 * 1024];
678 int atmark, n, out, rcvd;
679 char waste[BUFSIZ], mark;
683 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
687 * Urgent data not here yet. It may not be possible
688 * to send it yet if we are blocked for output and
689 * our input buffer is full.
691 if (rcvcnt < sizeof(rcvbuf)) {
692 n = read(rem, rcvbuf + rcvcnt,
693 sizeof(rcvbuf) - rcvcnt);
698 n = read(rem, waste, sizeof(waste));
707 if (mark & TIOCPKT_WINDOW) {
708 /* Let server know about window size changes */
709 (void)kill(ppid, SIGUSR1);
711 if (!eight && (mark & TIOCPKT_NOSTOP)) {
712 (void)ioctl(0, TIOCGETP, (char *)&sb);
713 sb.sg_flags &= ~CBREAK;
715 (void)ioctl(0, TIOCSETN, (char *)&sb);
718 (void)ioctl(0, TIOCSETC, (char *)¬c);
720 if (!eight && (mark & TIOCPKT_DOSTOP)) {
721 (void)ioctl(0, TIOCGETP, (char *)&sb);
723 sb.sg_flags |= CBREAK;
724 (void)ioctl(0, TIOCSETN, (char *)&sb);
725 notc.t_stopc = deftc.t_stopc;
726 notc.t_startc = deftc.t_startc;
727 (void)ioctl(0, TIOCSETC, (char *)¬c);
729 if (mark & TIOCPKT_FLUSHWRITE) {
730 (void)ioctl(1, TIOCFLUSH, (char *)&out);
732 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
738 n = read(rem, waste, sizeof (waste));
743 * Don't want any pending data to be output, so clear the recv
744 * buffer. If we were hanging on a write when interrupted,
745 * don't want it to restart. If we were reading, restart
752 /* oob does not do FLUSHREAD (alas!) */
755 * If we filled the receive buffer while a read was pending, longjmp
756 * to the top to restart appropriately. Don't abort a pending write,
757 * however, or we won't know how much was written.
759 if (rcvd && rcvstate == READING)
763 /* reader: read from remote: line -> 1 */
768 int pid, n, remaining;
771 #if BSD >= 43 || defined(SUNOS4)
772 pid = getpid(); /* modern systems use positives for pid */
774 pid = -getpid(); /* old broken systems use negatives */
776 (void)signal(SIGTTOU, SIG_IGN);
777 (void)signal(SIGURG, oob);
779 (void)fcntl(rem, F_SETOWN, pid);
780 (void)setjmp(rcvtop);
781 (void)sigsetmask(omask);
784 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
786 n = write(STDOUT_FILENO, bufp, remaining);
801 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
802 schedule, &cred.session);
806 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
827 (void)ioctl(0, TIOCGETP, (char *)&sb);
828 (void)ioctl(0, TIOCLGET, (char *)&lflags);
831 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
832 sb.sg_flags |= defflags|tabflag;
835 sb.sg_kill = defkill;
836 sb.sg_erase = deferase;
840 sb.sg_flags |= (eight ? RAW : CBREAK);
841 sb.sg_flags &= ~defflags;
842 /* preserve tab delays, but turn off XTABS */
843 if ((sb.sg_flags & TBDELAY) == XTABS)
844 sb.sg_flags &= ~TBDELAY;
847 sb.sg_kill = sb.sg_erase = -1;
854 (void)ioctl(0, TIOCSLTC, (char *)ltc);
855 (void)ioctl(0, TIOCSETC, (char *)tc);
856 (void)ioctl(0, TIOCSETN, (char *)&sb);
857 (void)ioctl(0, TIOCLSET, (char *)&lflags);
864 (void)signal(SIGPIPE, SIG_IGN);
865 msg("\007connection closed.");
869 /* copy SIGURGs to the child process. */
874 (void)kill(child, SIGURG);
881 (void)fprintf(stderr, "rlogin: %s\r\n", str);
887 (void)fprintf(stderr,
888 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
891 "8DEKLdx", " [-k realm] ");
893 "8DEKLd", " [-k realm] ");
902 * The following routine provides compatibility (such as it is) between older
903 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
907 get_window_size(fd, wp)
914 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
916 wp->ws_row = ts.ts_lines;
917 wp->ws_col = ts.ts_cols;
931 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
933 /* otherwise, \nnn */
934 if (*p == '\\' && len >= 2 && len <= 4) {
935 val = strtol(++p, NULL, 8);
939 if (*p < '0' || *p > '8')
943 msg("illegal option value -- e");