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[] =
45 "$Id: rlogind.c,v 1.22 1999/04/07 08:27:42 brian Exp $";
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"
83 #ifndef TIOCPKT_WINDOW
84 #define TIOCPKT_WINDOW 0x80
90 #define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"
94 u_char auth_buf[sizeof(AUTH_DAT)];
95 u_char tick_buf[sizeof(KTEXT_ST)];
96 Key_schedule schedule;
97 int doencrypt, retval, use_kerberos, vacuous;
99 #define ARGSTR "Dalnkvx"
101 #define ARGSTR "Daln"
102 #endif /* KERBEROS */
106 char lusername[NMAX+1], rusername[NMAX+1];
107 static char term[64] = "TERM=";
108 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
115 void doit __P((int, struct sockaddr_in *));
116 int control __P((int, char *, int));
117 void protocol __P((int, int));
118 void cleanup __P((int));
119 void fatal __P((int, char *, int));
120 int do_rlogin __P((struct sockaddr_in *));
121 void getstr __P((char *, int, char *));
122 void setup_term __P((int));
123 int do_krb_login __P((struct sockaddr_in *));
124 void usage __P((void));
131 extern int __check_rhosts_file;
132 struct sockaddr_in from;
135 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
138 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
147 __check_rhosts_file = 0;
174 if (use_kerberos && vacuous) {
176 fatal(STDERR_FILENO, "only one of -k and -v allowed", 0);
179 fromlen = sizeof (from);
180 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
181 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
182 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
186 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
187 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
189 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
190 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
192 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
193 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
201 char line[MAXPATHLEN];
204 struct winsize win = { 0, 0, 0, 0 };
210 struct sockaddr_in *fromp;
212 int master, pid, on = 1;
213 int authenticated = 0;
214 char hostname[MAXHOSTNAMELEN];
224 fatal(f, "Remote host requires Kerberos authentication", 0);
228 fromp->sin_port = ntohs((u_short)fromp->sin_port);
229 realhostname(hostname, sizeof(hostname) - 1, &fromp->sin_addr);
230 hostname[sizeof(hostname) - 1] = '\0';
234 retval = do_krb_login(fromp);
238 fatal(f, krb_err_txt[retval], 0);
240 confirmed = 1; /* we sent the null! */
244 if (fromp->sin_family != AF_INET ||
245 fromp->sin_port >= IPPORT_RESERVED ||
246 fromp->sin_port < IPPORT_RESERVED/2) {
247 syslog(LOG_NOTICE, "Connection from %s on illegal port",
248 inet_ntoa(fromp->sin_addr));
249 fatal(f, "Permission denied", 0);
253 u_char optbuf[BUFSIZ/3];
254 int optsize = sizeof(optbuf), ipproto, i;
257 if ((ip = getprotobyname("ip")) != NULL)
258 ipproto = ip->p_proto;
260 ipproto = IPPROTO_IP;
261 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
262 &optsize) == 0 && optsize != 0) {
263 for (i = 0; i < optsize; ) {
264 u_char c = optbuf[i];
265 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
267 "Connection refused from %s with IP option %s",
268 inet_ntoa(fromp->sin_addr),
269 c == IPOPT_LSRR ? "LSRR" : "SSRR");
274 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
279 if (do_rlogin(fromp) == 0)
282 if (confirmed == 0) {
284 confirmed = 1; /* we sent the null! */
289 (void) des_enc_write(f,
291 strlen(SECURE_MESSAGE),
292 schedule, &kdata->session);
297 pid = forkpty(&master, line, NULL, &win);
300 fatal(f, "Out of ptys", 0);
302 fatal(f, "Forkpty", 1);
305 if (f > 2) /* f should always be 0, but... */
308 if (*lusername=='-') {
309 syslog(LOG_ERR, "tried to pass user \"%s\" to login",
311 fatal(STDERR_FILENO, "invalid user", 0);
315 if (use_kerberos && (pwd->pw_uid == 0))
316 syslog(LOG_INFO|LOG_AUTH,
317 "ROOT Kerberos login from %s.%s@%s on %s\n",
318 kdata->pname, kdata->pinst, kdata->prealm,
322 execl(_PATH_LOGIN, "login", "-p",
323 "-h", hostname, "-f", lusername, (char *)NULL);
325 execl(_PATH_LOGIN, "login", "-p",
326 "-h", hostname, lusername, (char *)NULL);
327 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
333 * If encrypted, don't turn on NBIO or the des read/write
334 * routines will croak.
340 ioctl(f, FIONBIO, &on);
341 ioctl(master, FIONBIO, &on);
342 ioctl(master, TIOCPKT, &on);
343 signal(SIGCHLD, cleanup);
345 signal(SIGCHLD, SIG_IGN);
349 char magic[2] = { 0377, 0377 };
350 char oobdata[] = {TIOCPKT_WINDOW};
353 * Handle a "control" request (signaled by magic being present)
354 * in the data stream. For now, we are only willing to handle
355 * window size changes.
365 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
367 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
368 bcopy(cp+4, (char *)&w, sizeof(w));
369 w.ws_row = ntohs(w.ws_row);
370 w.ws_col = ntohs(w.ws_col);
371 w.ws_xpixel = ntohs(w.ws_xpixel);
372 w.ws_ypixel = ntohs(w.ws_ypixel);
373 (void)ioctl(pty, TIOCSWINSZ, &w);
374 return (4+sizeof (w));
378 * rlogin "protocol" machine.
384 char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
385 int pcc = 0, fcc = 0;
390 * Must ignore SIGTTOU, otherwise we'll stop
391 * when we try and set slave pty's window shape
392 * (our controlling tty is the master pty).
394 (void) signal(SIGTTOU, SIG_IGN);
395 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
400 if (nfd > FD_SETSIZE) {
401 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
402 fatal(f, "internal error (select mask too small)", 0);
405 fd_set ibits, obits, ebits, *omask;
410 omask = (fd_set *)NULL;
424 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
427 fatal(f, "select", 1);
430 /* shouldn't happen... */
434 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
435 if (FD_ISSET(p, &ebits)) {
436 cc = read(p, &cntl, 1);
437 if (cc == 1 && pkcontrol(cntl)) {
439 send(f, &cntl, 1, MSG_OOB);
440 if (cntl & TIOCPKT_FLUSHWRITE) {
446 if (FD_ISSET(f, &ibits)) {
450 fcc = des_enc_read(f, fibuf, sizeof(fibuf),
451 schedule, &kdata->session);
455 fcc = read(f, fibuf, sizeof(fibuf));
456 if (fcc < 0 && errno == EWOULDBLOCK)
467 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
468 if (cp[0] == magic[0] &&
470 left = fcc - (cp-fibuf);
471 n = control(p, cp, left);
475 bcopy(cp+n, cp, left);
480 FD_SET(p, &obits); /* try write */
484 if (FD_ISSET(p, &obits) && fcc > 0) {
485 cc = write(p, fbp, fcc);
492 if (FD_ISSET(p, &ibits)) {
493 pcc = read(p, pibuf, sizeof (pibuf));
495 if (pcc < 0 && errno == EWOULDBLOCK)
499 else if (pibuf[0] == 0) {
506 FD_SET(f, &obits); /* try write */
508 if (pkcontrol(pibuf[0])) {
509 pibuf[0] |= oobdata[0];
510 send(f, &pibuf[0], 1, MSG_OOB);
515 if ((FD_ISSET(f, &obits)) && pcc > 0) {
519 cc = des_enc_write(f, pbp, pcc,
520 schedule, &kdata->session);
524 cc = write(f, pbp, pcc);
525 if (cc < 0 && errno == EWOULDBLOCK) {
527 * This happens when we try write after read
528 * from p, but some old kernels balk at large
529 * writes even when select returns true.
531 if (!FD_ISSET(p, &ibits))
549 p = line + sizeof(_PATH_DEV) - 1;
552 (void)chmod(line, 0666);
553 (void)chown(line, 0, 0);
555 (void)chmod(line, 0666);
556 (void)chown(line, 0, 0);
562 fatal(f, msg, syserr)
568 char buf[BUFSIZ], *bp = buf;
571 * Prepend binary one to message if we haven't sent
572 * the magic null as confirmation.
575 *bp++ = '\01'; /* error indicator */
577 len = sprintf(bp, "rlogind: %s: %s.\r\n",
578 msg, strerror(errno));
580 len = sprintf(bp, "rlogind: %s.\r\n", msg);
581 (void) write(f, buf, bp + len - buf);
587 struct sockaddr_in *dest;
589 getstr(rusername, sizeof(rusername), "remuser too long");
590 getstr(lusername, sizeof(lusername), "locuser too long");
591 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
593 pwd = getpwnam(lusername);
596 /* XXX why don't we syslog() failure? */
597 return (iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0,
598 rusername, lusername));
602 getstr(buf, cnt, errmsg)
610 if (read(0, &c, 1) != 1)
613 fatal(STDOUT_FILENO, errmsg, 0);
618 extern char **environ;
624 register char *cp = index(term+ENVSIZE, '/');
633 cp = index(speed, '/');
636 cfsetspeed(&tt, atoi(speed));
639 tt.c_iflag = TTYDEF_IFLAG;
640 tt.c_oflag = TTYDEF_OFLAG;
641 tt.c_lflag = TTYDEF_LFLAG;
642 tcsetattr(fd, TCSAFLUSH, &tt);
647 cp = index(speed, '/');
651 cfsetspeed(&tt, atoi(speed));
652 tcsetattr(fd, TCSAFLUSH, &tt);
662 #define VERSION_SIZE 9
665 * Do the remote kerberos login to the named host with the
668 * Return 0 on valid authorization
669 * Return -1 on valid authentication, no authorization
670 * Return >0 for error conditions
674 struct sockaddr_in *dest;
677 char instance[INST_SZ], version[VERSION_SIZE];
678 long authopts = 0L; /* !mutual */
679 struct sockaddr_in faddr;
681 kdata = (AUTH_DAT *) auth_buf;
682 ticket = (KTEXT) tick_buf;
690 if (getsockname(0, (struct sockaddr *)&faddr, &rc))
692 authopts = KOPT_DO_MUTUAL;
696 instance, dest, &faddr,
697 kdata, "", schedule, version);
698 des_set_key(&kdata->session, schedule);
705 instance, dest, (struct sockaddr_in *) 0,
706 kdata, "", NULL, version);
711 getstr(lusername, sizeof(lusername), "locuser");
712 /* get the "cmd" in the rcmd protocol */
713 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type");
715 pwd = getpwnam(lusername);
719 /* returns nonzero for no access */
720 if (kuserok(kdata, lusername) != 0)
726 #endif /* KERBEROS */
732 syslog(LOG_ERR, "usage: rlogind [-Daln] [-k | -v]");
734 syslog(LOG_ERR, "usage: rlogind [-Daln]");