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 * 4. 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 char lusername[NMAX+1], rusername[NMAX+1];
87 static char term[64] = "TERM=";
88 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
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 void doit(int, union sockunion *);
109 int control(int, char *, int);
110 void protocol(int, int);
112 void fatal(int, const char *, int);
113 int do_rlogin(union sockunion *);
114 void getstr(char *, int, const char *);
115 void setup_term(int);
116 int do_krb_login(struct sockaddr_in *);
121 main(int argc, char *argv[])
123 union sockunion from;
127 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
130 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
139 __check_rhosts_file = 0;
157 fromlen = sizeof (from);
158 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
159 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
160 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
164 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
165 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
167 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
168 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
169 if (from.su_family == AF_INET)
172 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
173 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
182 char line[MAXPATHLEN];
185 struct winsize win = { 0, 0, 0, 0 };
189 doit(int f, union sockunion *fromp)
191 int master, pid, on = 1;
192 int authenticated = 0;
193 char hostname[2 * MAXHOSTNAMELEN + 1];
194 char nameinfo[2 * INET6_ADDRSTRLEN + 1];
205 realhostname_sa(hostname, sizeof(hostname) - 1,
206 (struct sockaddr *)fromp, fromp->su_len);
208 fromp->su_port = ntohs((u_short)fromp->su_port);
209 hostname[sizeof(hostname) - 1] = '\0';
212 if ((fromp->su_family != AF_INET
214 && fromp->su_family != AF_INET6
217 fromp->su_port >= IPPORT_RESERVED ||
218 fromp->su_port < IPPORT_RESERVED/2) {
219 getnameinfo((struct sockaddr *)fromp,
221 nameinfo, sizeof(nameinfo), NULL, 0,
222 NI_NUMERICHOST|NI_WITHSCOPEID);
224 syslog(LOG_NOTICE, "Connection from %s on illegal port",
226 fatal(f, "Permission denied", 0);
229 if (fromp->su_family == AF_INET)
231 u_char optbuf[BUFSIZ/3];
232 socklen_t optsize = sizeof(optbuf), i;
236 if ((ip = getprotobyname("ip")) != NULL)
237 ipproto = ip->p_proto;
239 ipproto = IPPROTO_IP;
240 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
241 &optsize) == 0 && optsize != 0) {
242 for (i = 0; i < optsize; ) {
244 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
246 "Connection refused from %s with IP option %s",
247 inet_ntoa(fromp->su_sin.sin_addr),
248 c == IPOPT_LSRR ? "LSRR" : "SSRR");
253 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
258 if (do_rlogin(fromp) == 0)
261 if (confirmed == 0) {
263 confirmed = 1; /* we sent the null! */
267 des_enc_write(f, SECURE_MESSAGE, strlen(SECURE_MESSAGE),
268 schedule, &kdata->session);
273 pid = forkpty(&master, line, NULL, &win);
276 fatal(f, "Out of ptys", 0);
278 fatal(f, "Forkpty", 1);
281 if (f > 2) /* f should always be 0, but... */
284 if (*lusername=='-') {
285 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
287 fatal(STDERR_FILENO, "invalid user", 0);
290 execl(_PATH_LOGIN, "login", "-p",
291 "-h", hostname, "-f", lusername, NULL);
293 execl(_PATH_LOGIN, "login", "-p",
294 "-h", hostname, lusername, NULL);
295 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
300 * If encrypted, don't turn on NBIO or the des read/write
301 * routines will croak.
306 ioctl(f, FIONBIO, &on);
307 ioctl(master, FIONBIO, &on);
308 ioctl(master, TIOCPKT, &on);
309 signal(SIGCHLD, cleanup);
311 signal(SIGCHLD, SIG_IGN);
315 char magic[2] = { 0377, 0377 };
316 char oobdata[] = {TIOCPKT_WINDOW};
319 * Handle a "control" request (signaled by magic being present)
320 * in the data stream. For now, we are only willing to handle
321 * window size changes.
324 control(int pty, char *cp, int n)
328 if (n < 4 + (int)sizeof(w) || cp[2] != 's' || cp[3] != 's')
330 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
331 bcopy(cp+4, (char *)&w, sizeof(w));
332 w.ws_row = ntohs(w.ws_row);
333 w.ws_col = ntohs(w.ws_col);
334 w.ws_xpixel = ntohs(w.ws_xpixel);
335 w.ws_ypixel = ntohs(w.ws_ypixel);
336 ioctl(pty, TIOCSWINSZ, &w);
337 return (4+sizeof (w));
341 * rlogin "protocol" machine.
344 protocol(int f, int p)
346 char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
347 int pcc = 0, fcc = 0;
352 * Must ignore SIGTTOU, otherwise we'll stop
353 * when we try and set slave pty's window shape
354 * (our controlling tty is the master pty).
356 signal(SIGTTOU, SIG_IGN);
357 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
362 if (nfd > FD_SETSIZE) {
363 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
364 fatal(f, "internal error (select mask too small)", 0);
367 fd_set ibits, obits, ebits, *omask;
386 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
389 fatal(f, "select", 1);
392 /* shouldn't happen... */
396 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
397 if (FD_ISSET(p, &ebits)) {
398 cc = read(p, &cntl, 1);
399 if (cc == 1 && pkcontrol(cntl)) {
401 send(f, &cntl, 1, MSG_OOB);
402 if (cntl & TIOCPKT_FLUSHWRITE) {
408 if (FD_ISSET(f, &ibits)) {
411 fcc = des_enc_read(f, fibuf, sizeof(fibuf),
412 schedule, &kdata->session);
415 fcc = read(f, fibuf, sizeof(fibuf));
416 if (fcc < 0 && errno == EWOULDBLOCK)
427 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
428 if (cp[0] == magic[0] &&
430 left = fcc - (cp-fibuf);
431 n = control(p, cp, left);
435 bcopy(cp+n, cp, left);
440 FD_SET(p, &obits); /* try write */
444 if (FD_ISSET(p, &obits) && fcc > 0) {
445 cc = write(p, fbp, fcc);
452 if (FD_ISSET(p, &ibits)) {
453 pcc = read(p, pibuf, sizeof (pibuf));
455 if (pcc < 0 && errno == EWOULDBLOCK)
459 else if (pibuf[0] == 0) {
464 FD_SET(f, &obits); /* try write */
466 if (pkcontrol(pibuf[0])) {
467 pibuf[0] |= oobdata[0];
468 send(f, &pibuf[0], 1, MSG_OOB);
473 if ((FD_ISSET(f, &obits)) && pcc > 0) {
476 cc = des_enc_write(f, pbp, pcc,
477 schedule, &kdata->session);
480 cc = write(f, pbp, pcc);
481 if (cc < 0 && errno == EWOULDBLOCK) {
483 * This happens when we try write after read
484 * from p, but some old kernels balk at large
485 * writes even when select returns true.
487 if (!FD_ISSET(p, &ibits))
500 cleanup(int signo __unused)
504 p = line + sizeof(_PATH_DEV) - 1;
514 shutdown(netf, SHUT_RDWR);
519 fatal(int f, const char *msg, int syserr)
522 char buf[BUFSIZ], *bp = buf;
525 * Prepend binary one to message if we haven't sent
526 * the magic null as confirmation.
529 *bp++ = '\01'; /* error indicator */
531 len = snprintf(bp, sizeof(buf), "rlogind: %s: %s.\r\n",
532 msg, strerror(errno));
534 len = snprintf(bp, sizeof(buf), "rlogind: %s.\r\n", msg);
535 write(f, buf, bp + len - buf);
540 do_rlogin(union sockunion *dest)
543 getstr(rusername, sizeof(rusername), "remuser too long");
544 getstr(lusername, sizeof(lusername), "locuser too long");
545 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
547 pwd = getpwnam(lusername);
550 /* XXX why don't we syslog() failure? */
552 return (iruserok_sa(dest, dest->su_len, pwd->pw_uid == 0, rusername,
557 getstr(char *buf, int cnt, const char *errmsg)
562 if (read(0, &c, 1) != 1)
565 fatal(STDOUT_FILENO, errmsg, 0);
573 register char *cp = index(term+ENVSIZE, '/');
582 cp = index(speed, '/');
585 cfsetspeed(&tt, atoi(speed));
588 tt.c_iflag = TTYDEF_IFLAG;
589 tt.c_oflag = TTYDEF_OFLAG;
590 tt.c_lflag = TTYDEF_LFLAG;
591 tcsetattr(fd, TCSAFLUSH, &tt);
596 cp = index(speed, '/');
600 cfsetspeed(&tt, atoi(speed));
601 tcsetattr(fd, TCSAFLUSH, &tt);
613 syslog(LOG_ERR, "usage: rlogind [-" ARGSTR "]");