2 * Copyright (c) 1983, 1988, 1989, 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, 1988, 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static const char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93";
44 static const char rcsid[] =
49 * remote login server:
53 * terminal_type/speed\0
57 #define FD_SETSIZE 16 /* don't need many bits for select */
58 #include <sys/types.h>
59 #include <sys/param.h>
61 #include <sys/ioctl.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/tcp.h>
70 #include <arpa/inet.h>
81 #include "pathnames.h"
84 #include <security/pam_appl.h>
85 #include <security/pam_misc.h>
88 #ifndef TIOCPKT_WINDOW
89 #define TIOCPKT_WINDOW 0x80
92 #define ARGSTR "Dalnx"
96 char lusername[NMAX+1], rusername[NMAX+1];
97 static char term[64] = "TERM=";
98 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
105 void doit __P((int, struct sockaddr_in *));
106 int control __P((int, char *, int));
107 void protocol __P((int, int));
108 void cleanup __P((int));
109 void fatal __P((int, char *, int));
110 int do_rlogin __P((struct sockaddr_in *));
111 void getstr __P((char *, int, char *));
112 void setup_term __P((int));
113 int do_krb_login __P((struct sockaddr_in *));
114 void usage __P((void));
117 extern int auth_pam __P((char *));
125 extern int __check_rhosts_file;
126 struct sockaddr_in from;
129 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
132 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
141 __check_rhosts_file = 0;
159 fromlen = sizeof (from);
160 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
161 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
162 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
166 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
167 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
169 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
170 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
172 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
173 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
181 char line[MAXPATHLEN];
184 struct winsize win = { 0, 0, 0, 0 };
190 struct sockaddr_in *fromp;
192 int master, pid, on = 1;
193 int authenticated = 0;
194 char hostname[MAXHOSTNAMELEN];
204 fromp->sin_port = ntohs((u_short)fromp->sin_port);
205 realhostname(hostname, sizeof(hostname) - 1, &fromp->sin_addr);
206 hostname[sizeof(hostname) - 1] = '\0';
209 if (fromp->sin_family != AF_INET ||
210 fromp->sin_port >= IPPORT_RESERVED ||
211 fromp->sin_port < IPPORT_RESERVED/2) {
212 syslog(LOG_NOTICE, "Connection from %s on illegal port",
213 inet_ntoa(fromp->sin_addr));
214 fatal(f, "Permission denied", 0);
218 u_char optbuf[BUFSIZ/3];
219 int optsize = sizeof(optbuf), ipproto, i;
222 if ((ip = getprotobyname("ip")) != NULL)
223 ipproto = ip->p_proto;
225 ipproto = IPPROTO_IP;
226 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
227 &optsize) == 0 && optsize != 0) {
228 for (i = 0; i < optsize; ) {
229 u_char c = optbuf[i];
230 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
232 "Connection refused from %s with IP option %s",
233 inet_ntoa(fromp->sin_addr),
234 c == IPOPT_LSRR ? "LSRR" : "SSRR");
239 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
244 if (do_rlogin(fromp) == 0)
247 if (confirmed == 0) {
249 confirmed = 1; /* we sent the null! */
253 (void) des_enc_write(f,
255 strlen(SECURE_MESSAGE),
256 schedule, &kdata->session);
260 pid = forkpty(&master, line, NULL, &win);
263 fatal(f, "Out of ptys", 0);
265 fatal(f, "Forkpty", 1);
268 if (f > 2) /* f should always be 0, but... */
271 if (*lusername=='-') {
272 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
274 fatal(STDERR_FILENO, "invalid user", 0);
277 execl(_PATH_LOGIN, "login", "-p",
278 "-h", hostname, "-f", lusername, (char *)NULL);
280 execl(_PATH_LOGIN, "login", "-p",
281 "-h", hostname, lusername, (char *)NULL);
282 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
287 * If encrypted, don't turn on NBIO or the des read/write
288 * routines will croak.
293 ioctl(f, FIONBIO, &on);
294 ioctl(master, FIONBIO, &on);
295 ioctl(master, TIOCPKT, &on);
296 signal(SIGCHLD, cleanup);
298 signal(SIGCHLD, SIG_IGN);
302 char magic[2] = { 0377, 0377 };
303 char oobdata[] = {TIOCPKT_WINDOW};
306 * Handle a "control" request (signaled by magic being present)
307 * in the data stream. For now, we are only willing to handle
308 * window size changes.
318 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
320 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
321 bcopy(cp+4, (char *)&w, sizeof(w));
322 w.ws_row = ntohs(w.ws_row);
323 w.ws_col = ntohs(w.ws_col);
324 w.ws_xpixel = ntohs(w.ws_xpixel);
325 w.ws_ypixel = ntohs(w.ws_ypixel);
326 (void)ioctl(pty, TIOCSWINSZ, &w);
327 return (4+sizeof (w));
331 * rlogin "protocol" machine.
337 char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
338 int pcc = 0, fcc = 0;
343 * Must ignore SIGTTOU, otherwise we'll stop
344 * when we try and set slave pty's window shape
345 * (our controlling tty is the master pty).
347 (void) signal(SIGTTOU, SIG_IGN);
348 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
353 if (nfd > FD_SETSIZE) {
354 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
355 fatal(f, "internal error (select mask too small)", 0);
358 fd_set ibits, obits, ebits, *omask;
363 omask = (fd_set *)NULL;
377 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
380 fatal(f, "select", 1);
383 /* shouldn't happen... */
387 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
388 if (FD_ISSET(p, &ebits)) {
389 cc = read(p, &cntl, 1);
390 if (cc == 1 && pkcontrol(cntl)) {
392 send(f, &cntl, 1, MSG_OOB);
393 if (cntl & TIOCPKT_FLUSHWRITE) {
399 if (FD_ISSET(f, &ibits)) {
402 fcc = des_enc_read(f, fibuf, sizeof(fibuf),
403 schedule, &kdata->session);
406 fcc = read(f, fibuf, sizeof(fibuf));
407 if (fcc < 0 && errno == EWOULDBLOCK)
418 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
419 if (cp[0] == magic[0] &&
421 left = fcc - (cp-fibuf);
422 n = control(p, cp, left);
426 bcopy(cp+n, cp, left);
431 FD_SET(p, &obits); /* try write */
435 if (FD_ISSET(p, &obits) && fcc > 0) {
436 cc = write(p, fbp, fcc);
443 if (FD_ISSET(p, &ibits)) {
444 pcc = read(p, pibuf, sizeof (pibuf));
446 if (pcc < 0 && errno == EWOULDBLOCK)
450 else if (pibuf[0] == 0) {
455 FD_SET(f, &obits); /* try write */
457 if (pkcontrol(pibuf[0])) {
458 pibuf[0] |= oobdata[0];
459 send(f, &pibuf[0], 1, MSG_OOB);
464 if ((FD_ISSET(f, &obits)) && pcc > 0) {
467 cc = des_enc_write(f, pbp, pcc,
468 schedule, &kdata->session);
471 cc = write(f, pbp, pcc);
472 if (cc < 0 && errno == EWOULDBLOCK) {
474 * This happens when we try write after read
475 * from p, but some old kernels balk at large
476 * writes even when select returns true.
478 if (!FD_ISSET(p, &ibits))
496 p = line + sizeof(_PATH_DEV) - 1;
499 (void)chflags(line, 0);
500 (void)chmod(line, 0666);
501 (void)chown(line, 0, 0);
503 (void)chflags(line, 0);
504 (void)chmod(line, 0666);
505 (void)chown(line, 0, 0);
511 fatal(f, msg, syserr)
517 char buf[BUFSIZ], *bp = buf;
520 * Prepend binary one to message if we haven't sent
521 * the magic null as confirmation.
524 *bp++ = '\01'; /* error indicator */
526 len = sprintf(bp, "rlogind: %s: %s.\r\n",
527 msg, strerror(errno));
529 len = sprintf(bp, "rlogind: %s.\r\n", msg);
530 (void) write(f, buf, bp + len - buf);
536 struct sockaddr_in *dest;
540 getstr(rusername, sizeof(rusername), "remuser too long");
541 getstr(lusername, sizeof(lusername), "locuser too long");
542 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
545 retval = auth_pam(lusername);
549 syslog(LOG_ERR, "PAM authentication failed");
553 "User %s failed PAM authentication", lusername);
558 pwd = getpwnam(lusername);
561 /* XXX why don't we syslog() failure? */
562 return (iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0,
563 rusername, lusername));
567 getstr(buf, cnt, errmsg)
575 if (read(0, &c, 1) != 1)
578 fatal(STDOUT_FILENO, errmsg, 0);
583 extern char **environ;
589 register char *cp = index(term+ENVSIZE, '/');
598 cp = index(speed, '/');
601 cfsetspeed(&tt, atoi(speed));
604 tt.c_iflag = TTYDEF_IFLAG;
605 tt.c_oflag = TTYDEF_OFLAG;
606 tt.c_lflag = TTYDEF_LFLAG;
607 tcsetattr(fd, TCSAFLUSH, &tt);
612 cp = index(speed, '/');
616 cfsetspeed(&tt, atoi(speed));
617 tcsetattr(fd, TCSAFLUSH, &tt);
629 syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");