2 * Copyright (c) 1983, 1988, 1989, 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
43 static const char copyright[] =
44 "@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
45 The Regents of the University of California. All rights reserved.\n";
49 static const char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93";
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
56 * remote login server:
60 * terminal_type/speed\0
64 #define FD_SETSIZE 16 /* don't need many bits for select */
65 #include <sys/types.h>
66 #include <sys/param.h>
68 #include <sys/ioctl.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
77 #include <arpa/inet.h>
91 #ifndef TIOCPKT_WINDOW
92 #define TIOCPKT_WINDOW 0x80
99 char lusername[NMAX+1], rusername[NMAX+1];
100 static char term[64] = "TERM=";
101 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
114 struct sockaddr_in su_sin;
115 struct sockaddr_in6 su_sin6;
117 #define su_len su_si.si_len
118 #define su_family su_si.si_family
119 #define su_port su_si.si_port
121 void doit(int, union sockunion *);
122 int control(int, char *, int);
123 void protocol(int, int);
125 void fatal(int, char *, int);
126 int do_rlogin(union sockunion *);
127 void getstr(char *, int, char *);
128 void setup_term(int);
129 int do_krb_login(struct sockaddr_in *);
134 main(int argc, char *argv[])
136 extern int __check_rhosts_file;
137 union sockunion from;
141 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
144 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
153 __check_rhosts_file = 0;
166 fromlen = sizeof (from);
167 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
168 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
169 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
173 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
174 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
176 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
177 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
178 if (from.su_family == AF_INET)
181 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
182 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
191 char line[MAXPATHLEN];
194 struct winsize win = { 0, 0, 0, 0 };
198 doit(int f, union sockunion *fromp)
200 int master, pid, on = 1;
201 int authenticated = 0;
202 char hostname[2 * MAXHOSTNAMELEN + 1];
203 char nameinfo[2 * INET6_ADDRSTRLEN + 1];
214 realhostname_sa(hostname, sizeof(hostname) - 1,
215 (struct sockaddr *)fromp, fromp->su_len);
217 fromp->su_port = ntohs((u_short)fromp->su_port);
218 hostname[sizeof(hostname) - 1] = '\0';
221 if ((fromp->su_family != AF_INET
223 && fromp->su_family != AF_INET6
226 fromp->su_port >= IPPORT_RESERVED ||
227 fromp->su_port < IPPORT_RESERVED/2) {
228 getnameinfo((struct sockaddr *)fromp,
230 nameinfo, sizeof(nameinfo), NULL, 0,
233 syslog(LOG_NOTICE, "Connection from %s on illegal port",
235 fatal(f, "Permission denied", 0);
238 if (fromp->su_family == AF_INET)
240 u_char optbuf[BUFSIZ/3];
241 socklen_t optsize = sizeof(optbuf);
245 if ((ip = getprotobyname("ip")) != NULL)
246 ipproto = ip->p_proto;
248 ipproto = IPPROTO_IP;
249 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
250 &optsize) == 0 && optsize != 0) {
251 for (i = 0; i < optsize; ) {
252 u_char c = optbuf[i];
253 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
255 "Connection refused from %s with IP option %s",
256 inet_ntoa(fromp->su_sin.sin_addr),
257 c == IPOPT_LSRR ? "LSRR" : "SSRR");
262 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
267 if (do_rlogin(fromp) == 0)
270 if (confirmed == 0) {
272 confirmed = 1; /* we sent the null! */
276 pid = forkpty(&master, line, NULL, &win);
279 fatal(f, "Out of ptys", 0);
281 fatal(f, "Forkpty", 1);
284 if (f > 2) /* f should always be 0, but... */
287 if (*lusername=='-') {
288 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
290 fatal(STDERR_FILENO, "invalid user", 0);
293 execl(_PATH_LOGIN, "login", "-p",
294 "-h", hostname, "-f", lusername, (char *)NULL);
296 execl(_PATH_LOGIN, "login", "-p",
297 "-h", hostname, lusername, (char *)NULL);
298 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
301 ioctl(f, FIONBIO, &on);
302 ioctl(master, FIONBIO, &on);
303 ioctl(master, TIOCPKT, &on);
304 signal(SIGCHLD, cleanup);
306 signal(SIGCHLD, SIG_IGN);
310 char magic[2] = { 0377, 0377 };
311 char oobdata[] = {TIOCPKT_WINDOW};
314 * Handle a "control" request (signaled by magic being present)
315 * in the data stream. For now, we are only willing to handle
316 * window size changes.
319 control(int pty, char *cp, int n)
323 if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
325 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
326 bcopy(cp+4, (char *)&w, sizeof(w));
327 w.ws_row = ntohs(w.ws_row);
328 w.ws_col = ntohs(w.ws_col);
329 w.ws_xpixel = ntohs(w.ws_xpixel);
330 w.ws_ypixel = ntohs(w.ws_ypixel);
331 (void)ioctl(pty, TIOCSWINSZ, &w);
332 return (4+sizeof (w));
336 * rlogin "protocol" machine.
339 protocol(int f, int p)
341 char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
342 int pcc = 0, fcc = 0;
347 * Must ignore SIGTTOU, otherwise we'll stop
348 * when we try and set slave pty's window shape
349 * (our controlling tty is the master pty).
351 (void) signal(SIGTTOU, SIG_IGN);
352 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
357 if (nfd > FD_SETSIZE) {
358 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
359 fatal(f, "internal error (select mask too small)", 0);
362 fd_set ibits, obits, ebits, *omask;
367 omask = (fd_set *)NULL;
381 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
384 fatal(f, "select", 1);
387 /* shouldn't happen... */
391 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
392 if (FD_ISSET(p, &ebits)) {
393 cc = read(p, &cntl, 1);
394 if (cc == 1 && pkcontrol(cntl)) {
396 send(f, &cntl, 1, MSG_OOB);
397 if (cntl & TIOCPKT_FLUSHWRITE) {
403 if (FD_ISSET(f, &ibits)) {
404 fcc = read(f, fibuf, sizeof(fibuf));
405 if (fcc < 0 && errno == EWOULDBLOCK)
416 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
417 if (cp[0] == magic[0] &&
419 left = fcc - (cp-fibuf);
420 n = control(p, cp, left);
424 bcopy(cp+n, cp, left);
429 FD_SET(p, &obits); /* try write */
433 if (FD_ISSET(p, &obits) && fcc > 0) {
434 cc = write(p, fbp, fcc);
441 if (FD_ISSET(p, &ibits)) {
442 pcc = read(p, pibuf, sizeof (pibuf));
444 if (pcc < 0 && errno == EWOULDBLOCK)
448 else if (pibuf[0] == 0) {
450 FD_SET(f, &obits); /* try write */
452 if (pkcontrol(pibuf[0])) {
453 pibuf[0] |= oobdata[0];
454 send(f, &pibuf[0], 1, MSG_OOB);
459 if ((FD_ISSET(f, &obits)) && pcc > 0) {
460 cc = write(f, pbp, pcc);
461 if (cc < 0 && errno == EWOULDBLOCK) {
463 * This happens when we try write after read
464 * from p, but some old kernels balk at large
465 * writes even when select returns true.
467 if (!FD_ISSET(p, &ibits))
484 p = line + sizeof(_PATH_DEV) - 1;
487 (void)chflags(line, 0);
488 (void)chmod(line, 0666);
489 (void)chown(line, 0, 0);
491 (void)chflags(line, 0);
492 (void)chmod(line, 0666);
493 (void)chown(line, 0, 0);
494 shutdown(netf, SHUT_RDWR);
499 fatal(int f, char *msg, int syserr)
502 char buf[BUFSIZ], *bp = buf;
505 * Prepend binary one to message if we haven't sent
506 * the magic null as confirmation.
509 *bp++ = '\01'; /* error indicator */
511 len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
512 msg, strerror(errno));
514 len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
517 (void) write(f, buf, bp + len - buf);
522 do_rlogin(union sockunion *dest)
525 getstr(rusername, sizeof(rusername), "remuser too long");
526 getstr(lusername, sizeof(lusername), "locuser too long");
527 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
529 pwd = getpwnam(lusername);
532 /* XXX why don't we syslog() failure? */
534 return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
539 getstr(char *buf, int cnt, char *errmsg)
544 if (read(STDIN_FILENO, &c, 1) != 1)
547 fatal(STDOUT_FILENO, errmsg, 0);
552 extern char **environ;
557 char *cp = index(term+ENVSIZE, '/');
566 cp = index(speed, '/');
569 cfsetspeed(&tt, atoi(speed));
572 tt.c_iflag = TTYDEF_IFLAG;
573 tt.c_oflag = TTYDEF_OFLAG;
574 tt.c_lflag = TTYDEF_LFLAG;
575 tcsetattr(fd, TCSAFLUSH, &tt);
580 cp = index(speed, '/');
584 cfsetspeed(&tt, atoi(speed));
585 tcsetattr(fd, TCSAFLUSH, &tt);
597 syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");