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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1988, 1989, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)rlogind.c 8.1 (Berkeley) 6/4/93
31 * $FreeBSD: src/libexec/rlogind/rlogind.c,v 1.29.2.5 2000/12/07 15:02:31 ru Exp $
35 * remote login server:
39 * terminal_type/speed\0
43 #define FD_SETSIZE 16 /* don't need many bits for select */
44 #include <sys/types.h>
45 #include <sys/param.h>
47 #include <sys/ioctl.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <arpa/inet.h>
67 #include "pathnames.h"
70 #ifndef TIOCPKT_WINDOW
71 #define TIOCPKT_WINDOW 0x80
74 #define ARGSTR "Dalnx"
76 /* wrapper for KAME-special getnameinfo() */
77 #ifndef NI_WITHSCOPEID
78 #define NI_WITHSCOPEID 0
81 extern int __check_rhosts_file;
82 extern char **environ;
86 static char lusername[NMAX+1], rusername[NMAX+1];
87 static char term[64] = "TERM=";
88 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
89 static int keepalive = 1;
90 static int check_all = 0;
93 static struct passwd *pwd;
101 struct sockaddr_in su_sin;
102 struct sockaddr_in6 su_sin6;
104 #define su_len su_si.si_len
105 #define su_family su_si.si_family
106 #define su_port su_si.si_port
108 static void doit(int, union sockunion *);
109 static int control(int, char *, int);
110 static void protocol(int, int);
111 static void cleanup(int);
112 static void fatal(int, const char *, int);
113 static int do_rlogin(union sockunion *);
114 static void getstr(char *, int, const char *);
115 static void setup_term(int);
116 static void usage(void);
120 main(int argc, char *argv[])
122 union sockunion from;
126 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
129 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
138 __check_rhosts_file = 0;
156 fromlen = sizeof (from);
157 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
158 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
159 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
163 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
164 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
166 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
167 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
168 if (from.su_family == AF_INET)
171 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
172 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
180 static char line[MAXPATHLEN];
181 static int confirmed;
183 static struct winsize win = { 0, 0, 0, 0 };
187 doit(int f, union sockunion *fromp)
189 int master, pid, on = 1;
190 int authenticated = 0;
191 char hostname[2 * MAXHOSTNAMELEN + 1];
192 char nameinfo[2 * INET6_ADDRSTRLEN + 1];
203 realhostname_sa(hostname, sizeof(hostname) - 1,
204 (struct sockaddr *)fromp, fromp->su_len);
206 fromp->su_port = ntohs((u_short)fromp->su_port);
207 hostname[sizeof(hostname) - 1] = '\0';
210 if ((fromp->su_family != AF_INET
212 && fromp->su_family != AF_INET6
215 fromp->su_port >= IPPORT_RESERVED ||
216 fromp->su_port < IPPORT_RESERVED/2) {
217 getnameinfo((struct sockaddr *)fromp,
219 nameinfo, sizeof(nameinfo), NULL, 0,
220 NI_NUMERICHOST|NI_WITHSCOPEID);
222 syslog(LOG_NOTICE, "Connection from %s on illegal port",
224 fatal(f, "Permission denied", 0);
227 if (fromp->su_family == AF_INET)
229 u_char optbuf[BUFSIZ/3];
230 socklen_t optsize = sizeof(optbuf), i;
234 if ((ip = getprotobyname("ip")) != NULL)
235 ipproto = ip->p_proto;
237 ipproto = IPPROTO_IP;
238 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
239 &optsize) == 0 && optsize != 0) {
240 for (i = 0; i < optsize; ) {
242 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
244 "Connection refused from %s with IP option %s",
245 inet_ntoa(fromp->su_sin.sin_addr),
246 c == IPOPT_LSRR ? "LSRR" : "SSRR");
251 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
256 if (do_rlogin(fromp) == 0)
259 if (confirmed == 0) {
261 confirmed = 1; /* we sent the null! */
265 des_enc_write(f, SECURE_MESSAGE, strlen(SECURE_MESSAGE),
266 schedule, &kdata->session);
271 pid = forkpty(&master, line, NULL, &win);
274 fatal(f, "Out of ptys", 0);
276 fatal(f, "Forkpty", 1);
279 if (f > 2) /* f should always be 0, but... */
282 if (*lusername=='-') {
283 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
285 fatal(STDERR_FILENO, "invalid user", 0);
288 execl(_PATH_LOGIN, "login", "-p",
289 "-h", hostname, "-f", lusername, NULL);
291 execl(_PATH_LOGIN, "login", "-p",
292 "-h", hostname, lusername, NULL);
293 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
298 * If encrypted, don't turn on NBIO or the des read/write
299 * routines will croak.
304 ioctl(f, FIONBIO, &on);
305 ioctl(master, FIONBIO, &on);
306 ioctl(master, TIOCPKT, &on);
307 signal(SIGCHLD, cleanup);
309 signal(SIGCHLD, SIG_IGN);
313 static char magic[2] = { 0377, 0377 };
314 static char oobdata[] = {TIOCPKT_WINDOW};
317 * Handle a "control" request (signaled by magic being present)
318 * in the data stream. For now, we are only willing to handle
319 * window size changes.
322 control(int pty, char *cp, int n)
326 if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
328 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
329 bcopy(cp+4, (char *)&w, sizeof(w));
330 w.ws_row = ntohs(w.ws_row);
331 w.ws_col = ntohs(w.ws_col);
332 w.ws_xpixel = ntohs(w.ws_xpixel);
333 w.ws_ypixel = ntohs(w.ws_ypixel);
334 ioctl(pty, TIOCSWINSZ, &w);
335 return (4+sizeof (w));
339 * rlogin "protocol" machine.
342 protocol(int f, int p)
344 char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
345 int pcc = 0, fcc = 0;
350 * Must ignore SIGTTOU, otherwise we'll stop
351 * when we try and set slave pty's window shape
352 * (our controlling tty is the master pty).
354 signal(SIGTTOU, SIG_IGN);
355 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
360 if (nfd > FD_SETSIZE) {
361 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
362 fatal(f, "internal error (select mask too small)", 0);
365 fd_set ibits, obits, ebits, *omask;
384 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
387 fatal(f, "select", 1);
390 /* shouldn't happen... */
394 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
395 if (FD_ISSET(p, &ebits)) {
396 cc = read(p, &cntl, 1);
397 if (cc == 1 && pkcontrol(cntl)) {
399 send(f, &cntl, 1, MSG_OOB);
400 if (cntl & TIOCPKT_FLUSHWRITE) {
406 if (FD_ISSET(f, &ibits)) {
409 fcc = des_enc_read(f, fibuf, sizeof(fibuf),
410 schedule, &kdata->session);
413 fcc = read(f, fibuf, sizeof(fibuf));
414 if (fcc < 0 && errno == EWOULDBLOCK)
425 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
426 if (cp[0] == magic[0] &&
428 left = fcc - (cp-fibuf);
429 n = control(p, cp, left);
433 bcopy(cp+n, cp, left);
438 FD_SET(p, &obits); /* try write */
442 if (FD_ISSET(p, &obits) && fcc > 0) {
443 cc = write(p, fbp, fcc);
450 if (FD_ISSET(p, &ibits)) {
451 pcc = read(p, pibuf, sizeof (pibuf));
453 if (pcc < 0 && errno == EWOULDBLOCK)
457 else if (pibuf[0] == 0) {
462 FD_SET(f, &obits); /* try write */
464 if (pkcontrol(pibuf[0])) {
465 pibuf[0] |= oobdata[0];
466 send(f, &pibuf[0], 1, MSG_OOB);
471 if ((FD_ISSET(f, &obits)) && pcc > 0) {
474 cc = des_enc_write(f, pbp, pcc,
475 schedule, &kdata->session);
478 cc = write(f, pbp, pcc);
479 if (cc < 0 && errno == EWOULDBLOCK) {
481 * This happens when we try write after read
482 * from p, but some old kernels balk at large
483 * writes even when select returns true.
485 if (!FD_ISSET(p, &ibits))
498 cleanup(int signo __unused)
502 p = line + sizeof(_PATH_DEV) - 1;
512 shutdown(netf, SHUT_RDWR);
517 fatal(int f, const char *msg, int syserr)
520 char buf[BUFSIZ], *bp = buf;
523 * Prepend binary one to message if we haven't sent
524 * the magic null as confirmation.
527 *bp++ = '\01'; /* error indicator */
529 len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
530 msg, strerror(errno));
532 len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
533 write(f, buf, bp + len - buf);
538 do_rlogin(union sockunion *dest)
541 getstr(rusername, sizeof(rusername), "remuser too long");
542 getstr(lusername, sizeof(lusername), "locuser too long");
543 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
545 pwd = getpwnam(lusername);
548 /* XXX why don't we syslog() failure? */
550 return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
555 getstr(char *buf, int cnt, const char *errmsg)
560 if (read(0, &c, 1) != 1)
563 fatal(STDOUT_FILENO, errmsg, 0);
571 register char *cp = index(term+ENVSIZE, '/');
580 cp = index(speed, '/');
583 cfsetspeed(&tt, atoi(speed));
586 tt.c_iflag = TTYDEF_IFLAG;
587 tt.c_oflag = TTYDEF_OFLAG;
588 tt.c_lflag = TTYDEF_LFLAG;
589 tcsetattr(fd, TCSAFLUSH, &tt);
594 cp = index(speed, '/');
598 cfsetspeed(&tt, atoi(speed));
599 tcsetattr(fd, TCSAFLUSH, &tt);
611 syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");