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>
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;
100 u_char escapechar = '~';
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)
110 unsigned short ws_row, ws_col;
111 unsigned short ws_xpixel, ws_ypixel;
114 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
116 struct winsize winsize;
118 void catch_child __P((int));
119 void copytochild __P((int));
120 void doit __P((long)) __dead2;
121 void done __P((int)) __dead2;
122 void echo __P((char));
123 u_int getescape __P((char *));
124 void lostpeer __P((int));
125 void mode __P((int));
126 void msg __P((char *));
128 int reader __P((int));
129 void sendwindow __P((void));
130 void setsignal __P((int));
131 void sigwinch __P((int));
132 void stop __P((char));
133 void usage __P((void)) __dead2;
134 void writer __P((void));
135 void writeroob __P((int));
138 int get_window_size __P((int, struct winsize *));
152 int argoff, ch, dflag, Dflag, one, uid;
153 char *host, *localname, *p, *user, term[1024];
158 argoff = dflag = Dflag = 0;
160 host = localname = user = NULL;
162 if ((p = rindex(argv[0], '/')))
167 if (strcmp(p, "rlogin"))
170 /* handle "rlogin host flags" */
171 if (!host && argc > 2 && argv[1][0] != '-') {
177 #define OPTIONS "8DEKLde:i:k:l:x"
179 #define OPTIONS "8DEKLde:i:l:"
181 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
205 escapechar = getescape(optarg);
209 errx(1, "-i user: permission denied");
214 dest_realm = dst_realm_buf;
215 (void)strncpy(dest_realm, optarg, REALM_SZ);
234 /* if haven't gotten a host yet, do so */
235 if (!host && !(host = argv[optind++]))
241 if (!(pw = getpwuid(uid = getuid())))
242 errx(1, "unknown user id");
246 localname = pw->pw_name;
250 k = auth_getval("auth_list");
251 if (k && !strstr(k, "kerberos"))
254 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
257 warn("can't get entry for %s/tcp service",
258 doencrypt ? "eklogin" : "klogin");
263 sp = getservbyname("login", "tcp");
265 errx(1, "login/tcp: unknown service");
267 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
269 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
271 term[MAX_TERM_LENGTH] = '\0';
272 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
273 (void)strcat(term, "/");
274 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
277 (void)get_window_size(0, &winsize);
279 (void)signal(SIGPIPE, lostpeer);
280 /* will use SIGUSR1 for window size hack, so hold it off */
281 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
283 * We set SIGURG and SIGUSR1 below so that an
284 * incoming signal will be held pending rather than being
285 * discarded. Note that these routines will be ready to get
286 * a signal by the time that they are unblocked below.
288 (void)signal(SIGURG, copytochild);
289 (void)signal(SIGUSR1, writeroob);
296 if (dest_realm == NULL)
297 dest_realm = krb_realmofhost(host);
301 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
302 dest_realm, &cred, schedule);
303 des_set_key(&cred.session, schedule);
306 rem = krcmd(&host, sp->s_port, user, term, 0,
312 sp = getservbyname("login", "tcp");
314 errx(1, "unknown service login/tcp");
315 if (errno == ECONNREFUSED)
316 warn("remote host doesn't support Kerberos");
318 warn("can't provide Kerberos auth data");
319 newargv = malloc((argc + 2) * sizeof(*newargv));
322 newargv[0] = argv[0];
324 for(i = 1; i < argc; ++i)
325 newargv[i + 1] = argv[i];
326 newargv[argc + 1] = NULL;
327 execv(_PATH_RLOGIN, newargv);
332 errx(1, "the -x flag requires Kerberos authentication");
334 rem = rcmd(&host, sp->s_port, localname, user, term, 0);
337 rem = rcmd(&host, sp->s_port, localname, user, term, 0);
338 #endif /* KERBEROS */
344 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
347 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
348 warn("setsockopt NODELAY (ignored)");
350 one = IPTOS_LOWDELAY;
351 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
352 warn("setsockopt TOS (ignored)");
359 int child, defflags, deflflags, tabflag;
360 char deferase, defkill;
362 struct ltchars defltc;
363 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
364 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
372 (void)ioctl(0, TIOCGETP, (char *)&sb);
373 defflags = sb.sg_flags;
374 tabflag = defflags & TBDELAY;
375 defflags &= ECHO | CRMOD;
376 deferase = sb.sg_erase;
377 defkill = sb.sg_kill;
378 (void)ioctl(0, TIOCLGET, &deflflags);
379 (void)ioctl(0, TIOCGETC, &deftc);
380 notc.t_startc = deftc.t_startc;
381 notc.t_stopc = deftc.t_stopc;
382 (void)ioctl(0, TIOCGLTC, &defltc);
383 (void)signal(SIGINT, SIG_IGN);
393 if (reader(omask) == 0) {
394 msg("connection closed.");
398 msg("\007connection closed.");
403 * We may still own the socket, and may have a pending SIGURG (or might
404 * receive one soon) that we really want to send to the reader. When
405 * one of these comes in, the trap copytochild simply copies such
406 * signals to the child. We can now unblock SIGURG and SIGUSR1
407 * that were set above.
409 (void)sigsetmask(omask);
410 (void)signal(SIGCHLD, catch_child);
412 msg("closed connection.");
416 /* trap a signal, unless it is being ignored. */
421 int omask = sigblock(sigmask(sig));
423 if (signal(sig, exit) == SIG_IGN)
424 (void)signal(sig, SIG_IGN);
425 (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.
454 if (dosigwinch == 0) {
456 (void)signal(SIGWINCH, sigwinch);
469 pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
472 /* if the child (reader) dies, just quit */
473 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
474 done((int)(status.w_termsig | status.w_retcode));
480 * writer: write to remote: 0 -> line.
482 * ~^Z suspend rlogin process.
483 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
488 register int bol, local, n;
491 bol = 1; /* beginning of line */
494 n = read(STDIN_FILENO, &c, 1);
496 if (n < 0 && errno == EINTR)
501 * If we're at the beginning of the line and recognize a
502 * command character, then we echo locally. Otherwise,
503 * characters are echo'd remotely. If the command character
504 * is doubled, this acts as a force and local echo is
509 if (!noescape && c == escapechar) {
515 if (c == '.' || c == deftc.t_eofc) {
519 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
529 (void)des_enc_write(rem,
530 (char *)&escapechar, 1,
531 schedule, &cred.session);
535 (void)write(rem, &escapechar, 1);
541 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
548 if (write(rem, &c, 1) == 0) {
552 bol = c == defkill || c == deftc.t_eofc ||
553 c == deftc.t_intrc || c == defltc.t_suspc ||
554 c == '\r' || c == '\n';
560 echo(register char c)
575 } else if (c == 0177) {
582 (void)write(STDOUT_FILENO, buf, p - buf);
594 (void)signal(SIGCHLD, SIG_IGN);
595 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
596 (void)signal(SIGCHLD, catch_child);
598 sigwinch(0); /* check for size changes */
607 if (dosigwinch && get_window_size(0, &ws) == 0 &&
608 bcmp(&ws, &winsize, sizeof(ws))) {
615 * Send the window size to the server via the magic escape
621 char obuf[4 + sizeof (struct winsize)];
623 wp = (struct winsize *)(obuf+4);
628 wp->ws_row = htons(winsize.ws_row);
629 wp->ws_col = htons(winsize.ws_col);
630 wp->ws_xpixel = htons(winsize.ws_xpixel);
631 wp->ws_ypixel = htons(winsize.ws_ypixel);
636 (void)des_enc_write(rem, obuf, sizeof(obuf),
637 schedule, &cred.session);
641 (void)write(rem, obuf, sizeof(obuf));
645 * reader: read from remote: line -> 1
651 int ppid, rcvcnt, rcvstate;
652 char rcvbuf[8 * 1024];
659 int atmark, n, out, rcvd;
660 char waste[BUFSIZ], mark;
664 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
668 * Urgent data not here yet. It may not be possible
669 * to send it yet if we are blocked for output and
670 * our input buffer is full.
672 if (rcvcnt < sizeof(rcvbuf)) {
673 n = read(rem, rcvbuf + rcvcnt,
674 sizeof(rcvbuf) - rcvcnt);
679 n = read(rem, waste, sizeof(waste));
688 if (mark & TIOCPKT_WINDOW) {
689 /* Let server know about window size changes */
690 (void)kill(ppid, SIGUSR1);
692 if (!eight && (mark & TIOCPKT_NOSTOP)) {
693 (void)ioctl(0, TIOCGETP, (char *)&sb);
694 sb.sg_flags &= ~CBREAK;
696 (void)ioctl(0, TIOCSETN, (char *)&sb);
699 (void)ioctl(0, TIOCSETC, (char *)¬c);
701 if (!eight && (mark & TIOCPKT_DOSTOP)) {
702 (void)ioctl(0, TIOCGETP, (char *)&sb);
704 sb.sg_flags |= CBREAK;
705 (void)ioctl(0, TIOCSETN, (char *)&sb);
706 notc.t_stopc = deftc.t_stopc;
707 notc.t_startc = deftc.t_startc;
708 (void)ioctl(0, TIOCSETC, (char *)¬c);
710 if (mark & TIOCPKT_FLUSHWRITE) {
711 (void)ioctl(1, TIOCFLUSH, (char *)&out);
713 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
719 n = read(rem, waste, sizeof (waste));
724 * Don't want any pending data to be output, so clear the recv
725 * buffer. If we were hanging on a write when interrupted,
726 * don't want it to restart. If we were reading, restart
733 /* oob does not do FLUSHREAD (alas!) */
736 * If we filled the receive buffer while a read was pending, longjmp
737 * to the top to restart appropriately. Don't abort a pending write,
738 * however, or we won't know how much was written.
740 if (rcvd && rcvstate == READING)
744 /* reader: read from remote: line -> 1 */
749 int pid, n, remaining;
752 #if BSD >= 43 || defined(SUNOS4)
753 pid = getpid(); /* modern systems use positives for pid */
755 pid = -getpid(); /* old broken systems use negatives */
757 (void)signal(SIGTTOU, SIG_IGN);
758 (void)signal(SIGURG, oob);
760 (void)fcntl(rem, F_SETOWN, pid);
761 (void)setjmp(rcvtop);
762 (void)sigsetmask(omask);
765 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
767 n = write(STDOUT_FILENO, bufp, remaining);
782 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
783 schedule, &cred.session);
787 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
808 (void)ioctl(0, TIOCGETP, (char *)&sb);
809 (void)ioctl(0, TIOCLGET, (char *)&lflags);
812 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
813 sb.sg_flags |= defflags|tabflag;
816 sb.sg_kill = defkill;
817 sb.sg_erase = deferase;
821 sb.sg_flags |= (eight ? RAW : CBREAK);
822 sb.sg_flags &= ~defflags;
823 /* preserve tab delays, but turn off XTABS */
824 if ((sb.sg_flags & TBDELAY) == XTABS)
825 sb.sg_flags &= ~TBDELAY;
828 sb.sg_kill = sb.sg_erase = -1;
835 (void)ioctl(0, TIOCSLTC, (char *)ltc);
836 (void)ioctl(0, TIOCSETC, (char *)tc);
837 (void)ioctl(0, TIOCSETN, (char *)&sb);
838 (void)ioctl(0, TIOCLSET, (char *)&lflags);
845 (void)signal(SIGPIPE, SIG_IGN);
846 msg("\007connection closed.");
850 /* copy SIGURGs to the child process. */
855 (void)kill(child, SIGURG);
862 (void)fprintf(stderr, "rlogin: %s\r\n", str);
868 (void)fprintf(stderr,
869 "usage: rlogin [-%s]%s[-e char] [-i localname] [-l username] host\n",
872 "8DEKLdx", " [-k realm] ");
874 "8DEKLd", " [-k realm] ");
883 * The following routine provides compatibility (such as it is) between older
884 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
888 get_window_size(fd, wp)
895 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
897 wp->ws_row = ts.ts_lines;
898 wp->ws_col = ts.ts_cols;
912 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
914 /* otherwise, \nnn */
915 if (*p == '\\' && len >= 2 && len <= 4) {
916 val = strtol(++p, NULL, 8);
920 if (*p < '0' || *p > '8')
924 msg("illegal option value -- e");