rlogind.c revision 1.19 1 /* $NetBSD: rlogind.c,v 1.19 1999/12/31 08:01:26 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1983, 1988, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #if 0
41 static char sccsid[] = "@(#)rlogind.c 8.2 (Berkeley) 4/28/95";
42 #else
43 __RCSID("$NetBSD: rlogind.c,v 1.19 1999/12/31 08:01:26 itojun Exp $");
44 #endif
45 #endif /* not lint */
46
47 /*
48 * remote login server:
49 * \0
50 * remuser\0
51 * locuser\0
52 * terminal_type/speed\0
53 * data
54 */
55
56 #define FD_SETSIZE 16 /* don't need many bits for select */
57 #include <sys/param.h>
58 #include <sys/stat.h>
59 #include <sys/ioctl.h>
60 #include <signal.h>
61 #include <termios.h>
62
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <arpa/inet.h>
68 #include <netdb.h>
69
70 #include <pwd.h>
71 #include <syslog.h>
72 #include <errno.h>
73 #include <stdio.h>
74 #include <unistd.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <util.h>
78 #include <utmp.h>
79 #include "pathnames.h"
80
81 #ifndef TIOCPKT_WINDOW
82 #define TIOCPKT_WINDOW 0x80
83 #endif
84
85 #define OPTIONS "alnL"
86
87 char *env[2];
88 #define NMAX 30
89 char lusername[NMAX+1], rusername[NMAX+1];
90 static char term[64] = "TERM=";
91 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
92 int keepalive = 1;
93 int check_all = 0;
94 int log_success = 0;
95
96 struct passwd *pwd;
97
98 void doit __P((int, struct sockaddr_in *));
99 int control __P((int, char *, int));
100 void protocol __P((int, int));
101 void cleanup __P((int));
102 void fatal __P((int, char *, int));
103 int do_rlogin __P((struct sockaddr_in *, char *));
104 void getstr __P((char *, int, char *));
105 void setup_term __P((int));
106 int do_krb_login __P((struct sockaddr_in *));
107 void usage __P((void));
108 int local_domain __P((char *));
109 char *topdomain __P((char *));
110 int main __P((int, char *[]));
111
112 int
113 main(argc, argv)
114 int argc;
115 char *argv[];
116 {
117 extern int __check_rhosts_file;
118 struct sockaddr_in from;
119 int ch, fromlen, on;
120
121 openlog("rlogind", LOG_PID, LOG_AUTH);
122
123 opterr = 0;
124 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
125 switch (ch) {
126 case 'a':
127 check_all = 1;
128 break;
129 case 'l':
130 __check_rhosts_file = 0;
131 break;
132 case 'n':
133 keepalive = 0;
134 break;
135 case 'L':
136 log_success = 1;
137 break;
138 case '?':
139 default:
140 usage();
141 break;
142 }
143 argc -= optind;
144 argv += optind;
145
146 fromlen = sizeof (from);
147 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
148 syslog(LOG_ERR,"Can't get peer name of remote host: %m");
149 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
150 }
151 on = 1;
152 if (keepalive &&
153 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
154 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
155 on = IPTOS_LOWDELAY;
156 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
157 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
158 doit(0, &from);
159 /* NOTREACHED */
160 #ifdef __GNUC__
161 exit(0);
162 #endif
163 }
164
165 int child;
166 int netf;
167 char line[MAXPATHLEN];
168 int confirmed;
169
170 struct winsize win = { 0, 0, 0, 0 };
171
172
173 void
174 doit(f, fromp)
175 int f;
176 struct sockaddr_in *fromp;
177 {
178 int master, pid, on = 1;
179 int authenticated = 0;
180 struct hostent *hp;
181 char utmphost[UT_HOSTSIZE + 1];
182 char *hostname;
183 char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
184 char c;
185
186 alarm(60);
187 read(f, &c, 1);
188
189 if (c != 0)
190 exit(1);
191
192 alarm(0);
193 fromp->sin_port = ntohs((in_port_t)fromp->sin_port);
194 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(struct in_addr),
195 fromp->sin_family);
196 if (hp) {
197 /*
198 * If name returned by gethostbyaddr is in our domain,
199 * attempt to verify that we haven't been fooled by someone
200 * in a remote net; look up the name and check that this
201 * address corresponds to the name.
202 */
203 hostname = hp->h_name;
204 if (check_all || local_domain(hp->h_name)) {
205 strncpy(hostnamebuf, hp->h_name,
206 sizeof(hostnamebuf) - 1);
207 hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
208 hp = gethostbyname(hostnamebuf);
209 if (hp == NULL) {
210 syslog(LOG_INFO,
211 "Couldn't look up address for %s",
212 hostnamebuf);
213 hostname = inet_ntoa(fromp->sin_addr);
214 } else for (; ; hp->h_addr_list++) {
215 if (hp->h_addr_list[0] == NULL) {
216 syslog(LOG_NOTICE,
217 "Host addr %s not listed for host %s",
218 inet_ntoa(fromp->sin_addr),
219 hp->h_name);
220 hostname = inet_ntoa(fromp->sin_addr);
221 break;
222 }
223 if (!memcmp(hp->h_addr_list[0],
224 (caddr_t)&fromp->sin_addr,
225 sizeof(fromp->sin_addr))) {
226 hostname = hp->h_name;
227 break;
228 }
229 }
230 }
231 hostname = strncpy(hostnamebuf, hostname,
232 sizeof(hostnamebuf) - 1);
233 } else
234 hostname = strncpy(hostnamebuf, inet_ntoa(fromp->sin_addr),
235 sizeof(hostnamebuf) - 1);
236
237 hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
238
239 if (strlen(hostname) < sizeof(utmphost))
240 (void)strcpy(utmphost, hostname);
241 else
242 (void)strncpy(utmphost, inet_ntoa(fromp->sin_addr),
243 sizeof(utmphost));
244 utmphost[sizeof(utmphost) - 1] = '\0';
245
246 if (fromp->sin_family != AF_INET ||
247 fromp->sin_port >= IPPORT_RESERVED ||
248 fromp->sin_port < IPPORT_RESERVED/2) {
249 syslog(LOG_NOTICE, "Connection from %s on illegal port",
250 inet_ntoa(fromp->sin_addr));
251 fatal(f, "Permission denied", 0);
252 }
253 #ifdef IP_OPTIONS
254 {
255 u_char optbuf[BUFSIZ/3], *cp;
256 char lbuf[BUFSIZ], *lp;
257 int optsize = sizeof(optbuf), ipproto;
258 struct protoent *ip;
259
260 if ((ip = getprotobyname("ip")) != NULL)
261 ipproto = ip->p_proto;
262 else
263 ipproto = IPPROTO_IP;
264 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
265 &optsize) == 0 && optsize != 0) {
266 lp = lbuf;
267 for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
268 sprintf(lp, " %2.2x", *cp);
269 syslog(LOG_NOTICE,
270 "Connection received using IP options (ignored):%s",
271 lbuf);
272 if (setsockopt(0, ipproto, IP_OPTIONS,
273 (char *)NULL, optsize) != 0) {
274 syslog(LOG_ERR,
275 "setsockopt IP_OPTIONS NULL: %m");
276 exit(1);
277 }
278 }
279 }
280 #endif
281 if (do_rlogin(fromp, hostname) == 0)
282 authenticated++;
283 if (confirmed == 0) {
284 write(f, "", 1);
285 confirmed = 1; /* we sent the null! */
286 }
287 netf = f;
288
289 pid = forkpty(&master, line, NULL, &win);
290 if (pid < 0) {
291 if (errno == ENOENT)
292 fatal(f, "Out of ptys", 0);
293 else
294 fatal(f, "Forkpty", 1);
295 }
296 if (pid == 0) {
297 if (f > 2) /* f should always be 0, but... */
298 (void) close(f);
299 setup_term(0);
300 if (authenticated)
301 execl(_PATH_LOGIN, "login", "-p",
302 "-h", utmphost, "-f", "--", lusername, (char *)0);
303 else
304 execl(_PATH_LOGIN, "login", "-p",
305 "-h", utmphost, "--", lusername, (char *)0);
306 fatal(STDERR_FILENO, _PATH_LOGIN, 1);
307 /*NOTREACHED*/
308 }
309 ioctl(f, FIONBIO, &on);
310 ioctl(master, FIONBIO, &on);
311 ioctl(master, TIOCPKT, &on);
312 signal(SIGCHLD, cleanup);
313 protocol(f, master);
314 signal(SIGCHLD, SIG_IGN);
315 cleanup(0);
316 }
317
318 char magic[2] = { 0377, 0377 };
319 char oobdata[] = {TIOCPKT_WINDOW};
320
321 /*
322 * Handle a "control" request (signaled by magic being present)
323 * in the data stream. For now, we are only willing to handle
324 * window size changes.
325 */
326 int
327 control(pty, cp, n)
328 int pty;
329 char *cp;
330 int n;
331 {
332 struct winsize w;
333
334 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
335 return (0);
336 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
337 memmove(&w, cp+4, sizeof(w));
338 w.ws_row = ntohs(w.ws_row);
339 w.ws_col = ntohs(w.ws_col);
340 w.ws_xpixel = ntohs(w.ws_xpixel);
341 w.ws_ypixel = ntohs(w.ws_ypixel);
342 (void)ioctl(pty, TIOCSWINSZ, &w);
343 return (4+sizeof (w));
344 }
345
346 /*
347 * rlogin "protocol" machine.
348 */
349 void
350 protocol(f, p)
351 int f, p;
352 {
353 char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
354 /* XXX gcc above */
355 int pcc = 0, fcc = 0;
356 int cc, nfd, n;
357 char cntl;
358
359 /*
360 * Must ignore SIGTTOU, otherwise we'll stop
361 * when we try and set slave pty's window shape
362 * (our controlling tty is the master pty).
363 */
364 (void) signal(SIGTTOU, SIG_IGN);
365 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
366 if (f > p)
367 nfd = f + 1;
368 else
369 nfd = p + 1;
370 if (nfd > FD_SETSIZE) {
371 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
372 fatal(f, "internal error (select mask too small)", 0);
373 }
374 for (;;) {
375 fd_set ibits, obits, ebits, *omask;
376
377 FD_ZERO(&ebits);
378 FD_ZERO(&ibits);
379 FD_ZERO(&obits);
380 omask = (fd_set *)NULL;
381 if (fcc) {
382 FD_SET(p, &obits);
383 omask = &obits;
384 } else
385 FD_SET(f, &ibits);
386 if (pcc >= 0) {
387 if (pcc) {
388 FD_SET(f, &obits);
389 omask = &obits;
390 } else
391 FD_SET(p, &ibits);
392 }
393 FD_SET(p, &ebits);
394 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
395 if (errno == EINTR)
396 continue;
397 fatal(f, "select", 1);
398 }
399 if (n == 0) {
400 /* shouldn't happen... */
401 sleep(5);
402 continue;
403 }
404 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
405 if (FD_ISSET(p, &ebits)) {
406 cc = read(p, &cntl, 1);
407 if (cc == 1 && pkcontrol(cntl)) {
408 cntl |= oobdata[0];
409 send(f, &cntl, 1, MSG_OOB);
410 if (cntl & TIOCPKT_FLUSHWRITE) {
411 pcc = 0;
412 FD_CLR(p, &ibits);
413 }
414 }
415 }
416 if (FD_ISSET(f, &ibits)) {
417 fcc = read(f, fibuf, sizeof(fibuf));
418 if (fcc < 0 && errno == EWOULDBLOCK)
419 fcc = 0;
420 else {
421 char *cp;
422 int left, n;
423
424 if (fcc <= 0)
425 break;
426 fbp = fibuf;
427
428 top:
429 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
430 if (cp[0] == magic[0] &&
431 cp[1] == magic[1]) {
432 left = fcc - (cp-fibuf);
433 n = control(p, cp, left);
434 if (n) {
435 left -= n;
436 if (left > 0)
437 memmove(cp,
438 cp+n,
439 left);
440 fcc -= n;
441 goto top; /* n^2 */
442 }
443 }
444 FD_SET(p, &obits); /* try write */
445 }
446 }
447
448 if (FD_ISSET(p, &obits) && fcc > 0) {
449 cc = write(p, fbp, fcc);
450 if (cc > 0) {
451 fcc -= cc;
452 fbp += cc;
453 }
454 }
455
456 if (FD_ISSET(p, &ibits)) {
457 pcc = read(p, pibuf, sizeof (pibuf));
458 pbp = pibuf;
459 if (pcc < 0 && errno == EWOULDBLOCK)
460 pcc = 0;
461 else if (pcc <= 0)
462 break;
463 else if (pibuf[0] == 0) {
464 pbp++, pcc--;
465 FD_SET(f, &obits); /* try write */
466 } else {
467 if (pkcontrol(pibuf[0])) {
468 pibuf[0] |= oobdata[0];
469 send(f, &pibuf[0], 1, MSG_OOB);
470 }
471 pcc = 0;
472 }
473 }
474 if ((FD_ISSET(f, &obits)) && pcc > 0) {
475 cc = write(f, pbp, pcc);
476 if (cc < 0 && errno == EWOULDBLOCK) {
477 /*
478 * This happens when we try write after read
479 * from p, but some old kernels balk at large
480 * writes even when select returns true.
481 */
482 if (!FD_ISSET(p, &ibits))
483 sleep(5);
484 continue;
485 }
486 if (cc > 0) {
487 pcc -= cc;
488 pbp += cc;
489 }
490 }
491 }
492 }
493
494 void
495 cleanup(signo)
496 int signo;
497 {
498 char *p, c;
499
500 p = line + sizeof(_PATH_DEV) - 1;
501 if (logout(p))
502 logwtmp(p, "", "");
503 (void)chmod(line, 0666);
504 (void)chown(line, 0, 0);
505 c = *p; *p = 'p';
506 (void)chmod(line, 0666);
507 (void)chown(line, 0, 0);
508 *p = c;
509 if (ttyaction(line, "rlogind", "root"))
510 syslog(LOG_ERR, "%s: ttyaction failed", line);
511 shutdown(netf, 2);
512 exit(1);
513 }
514
515 void
516 fatal(f, msg, syserr)
517 int f;
518 char *msg;
519 int syserr;
520 {
521 int len;
522 char buf[BUFSIZ], *bp = buf;
523
524 /*
525 * Prepend binary one to message if we haven't sent
526 * the magic null as confirmation.
527 */
528 if (!confirmed)
529 *bp++ = '\01'; /* error indicator */
530 if (syserr)
531 len = sprintf(bp, "rlogind: %s: %s.\r\n",
532 msg, strerror(errno));
533 else
534 len = sprintf(bp, "rlogind: %s.\r\n", msg);
535 (void) write(f, buf, bp + len - buf);
536 exit(1);
537 }
538
539 int
540 do_rlogin(dest, host)
541 struct sockaddr_in *dest;
542 char *host;
543 {
544 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c */
545 int retval;
546
547 getstr(rusername, sizeof(rusername), "remuser too long");
548 getstr(lusername, sizeof(lusername), "locuser too long");
549 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
550
551 pwd = getpwnam(lusername);
552 if (pwd == NULL) {
553 syslog(LOG_INFO,
554 "%s@%s as %s: unknown login.", rusername, host, lusername);
555 return (-1);
556 }
557 retval = iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0, rusername,
558 lusername);
559 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
560 if (retval == 0) {
561 if (log_success)
562 syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
563 rusername, host, lusername);
564 } else {
565 if (__rcmd_errstr)
566 syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
567 rusername, host, lusername, __rcmd_errstr);
568 else
569 syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
570 rusername, host, lusername);
571 }
572 return(retval);
573 }
574
575 void
576 getstr(buf, cnt, errmsg)
577 char *buf;
578 int cnt;
579 char *errmsg;
580 {
581 char c;
582
583 do {
584 if (read(0, &c, 1) != 1)
585 exit(1);
586 if (--cnt < 0)
587 fatal(STDOUT_FILENO, errmsg, 0);
588 *buf++ = c;
589 } while (c != 0);
590 }
591
592 extern char **environ;
593
594 void
595 setup_term(fd)
596 int fd;
597 {
598 char *cp = index(term+ENVSIZE, '/');
599 char *speed;
600 struct termios tt;
601
602 #ifndef notyet
603 tcgetattr(fd, &tt);
604 if (cp) {
605 *cp++ = '\0';
606 speed = cp;
607 cp = index(speed, '/');
608 if (cp)
609 *cp++ = '\0';
610 cfsetspeed(&tt, atoi(speed));
611 }
612
613 tt.c_iflag = TTYDEF_IFLAG;
614 tt.c_oflag = TTYDEF_OFLAG;
615 tt.c_lflag = TTYDEF_LFLAG;
616 tcsetattr(fd, TCSAFLUSH, &tt);
617 #else
618 if (cp) {
619 *cp++ = '\0';
620 speed = cp;
621 cp = index(speed, '/');
622 if (cp)
623 *cp++ = '\0';
624 tcgetattr(fd, &tt);
625 cfsetspeed(&tt, atoi(speed));
626 tcsetattr(fd, TCSAFLUSH, &tt);
627 }
628 #endif
629
630 env[0] = term;
631 env[1] = 0;
632 environ = env;
633 }
634
635
636 void
637 usage()
638 {
639 syslog(LOG_ERR, "usage: rlogind [-alnL]");
640 }
641
642 /*
643 * Check whether host h is in our local domain,
644 * defined as sharing the last two components of the domain part,
645 * or the entire domain part if the local domain has only one component.
646 * If either name is unqualified (contains no '.'),
647 * assume that the host is local, as it will be
648 * interpreted as such.
649 */
650 int
651 local_domain(h)
652 char *h;
653 {
654 char localhost[MAXHOSTNAMELEN + 1];
655 char *p1, *p2;
656
657 localhost[0] = 0;
658 (void) gethostname(localhost, sizeof(localhost));
659 localhost[sizeof(localhost) - 1] = '\0';
660 p1 = topdomain(localhost);
661 p2 = topdomain(h);
662 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
663 return (1);
664 return (0);
665 }
666
667 char *
668 topdomain(h)
669 char *h;
670 {
671 char *p;
672 char *maybe = NULL;
673 int dots = 0;
674
675 for (p = h + strlen(h); p >= h; p--) {
676 if (*p == '.') {
677 if (++dots == 2)
678 return (p);
679 maybe = p;
680 }
681 }
682 return (maybe);
683 }
684