rlogind.c revision 1.13 1 /* $NetBSD: rlogind.c,v 1.13 1998/03/30 01:53:07 mrg 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.13 1998/03/30 01:53:07 mrg 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_CONS, 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((u_short)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 (!bcmp(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 FD_SET(p, &ebits);
393 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
394 if (errno == EINTR)
395 continue;
396 fatal(f, "select", 1);
397 }
398 if (n == 0) {
399 /* shouldn't happen... */
400 sleep(5);
401 continue;
402 }
403 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
404 if (FD_ISSET(p, &ebits)) {
405 cc = read(p, &cntl, 1);
406 if (cc == 1 && pkcontrol(cntl)) {
407 cntl |= oobdata[0];
408 send(f, &cntl, 1, MSG_OOB);
409 if (cntl & TIOCPKT_FLUSHWRITE) {
410 pcc = 0;
411 FD_CLR(p, &ibits);
412 }
413 }
414 }
415 if (FD_ISSET(f, &ibits)) {
416 fcc = read(f, fibuf, sizeof(fibuf));
417 if (fcc < 0 && errno == EWOULDBLOCK)
418 fcc = 0;
419 else {
420 char *cp;
421 int left, n;
422
423 if (fcc <= 0)
424 break;
425 fbp = fibuf;
426
427 top:
428 for (cp = fibuf; cp < fibuf+fcc-1; cp++)
429 if (cp[0] == magic[0] &&
430 cp[1] == magic[1]) {
431 left = fcc - (cp-fibuf);
432 n = control(p, cp, left);
433 if (n) {
434 left -= n;
435 if (left > 0)
436 bcopy(cp+n, cp,
437 left);
438 fcc -= n;
439 goto top; /* n^2 */
440 }
441 }
442 FD_SET(p, &obits); /* try write */
443 }
444 }
445
446 if (FD_ISSET(p, &obits) && fcc > 0) {
447 cc = write(p, fbp, fcc);
448 if (cc > 0) {
449 fcc -= cc;
450 fbp += cc;
451 }
452 }
453
454 if (FD_ISSET(p, &ibits)) {
455 pcc = read(p, pibuf, sizeof (pibuf));
456 pbp = pibuf;
457 if (pcc < 0 && errno == EWOULDBLOCK)
458 pcc = 0;
459 else if (pcc <= 0)
460 break;
461 else if (pibuf[0] == 0) {
462 pbp++, pcc--;
463 FD_SET(f, &obits); /* try write */
464 } else {
465 if (pkcontrol(pibuf[0])) {
466 pibuf[0] |= oobdata[0];
467 send(f, &pibuf[0], 1, MSG_OOB);
468 }
469 pcc = 0;
470 }
471 }
472 if ((FD_ISSET(f, &obits)) && pcc > 0) {
473 cc = write(f, pbp, pcc);
474 if (cc < 0 && errno == EWOULDBLOCK) {
475 /*
476 * This happens when we try write after read
477 * from p, but some old kernels balk at large
478 * writes even when select returns true.
479 */
480 if (!FD_ISSET(p, &ibits))
481 sleep(5);
482 continue;
483 }
484 if (cc > 0) {
485 pcc -= cc;
486 pbp += cc;
487 }
488 }
489 }
490 }
491
492 void
493 cleanup(signo)
494 int signo;
495 {
496 char *p;
497
498 p = line + sizeof(_PATH_DEV) - 1;
499 if (logout(p))
500 logwtmp(p, "", "");
501 (void)chmod(line, 0666);
502 (void)chown(line, 0, 0);
503 *p = 'p';
504 (void)chmod(line, 0666);
505 (void)chown(line, 0, 0);
506 shutdown(netf, 2);
507 exit(1);
508 }
509
510 void
511 fatal(f, msg, syserr)
512 int f;
513 char *msg;
514 int syserr;
515 {
516 int len;
517 char buf[BUFSIZ], *bp = buf;
518
519 /*
520 * Prepend binary one to message if we haven't sent
521 * the magic null as confirmation.
522 */
523 if (!confirmed)
524 *bp++ = '\01'; /* error indicator */
525 if (syserr)
526 len = sprintf(bp, "rlogind: %s: %s.\r\n",
527 msg, strerror(errno));
528 else
529 len = sprintf(bp, "rlogind: %s.\r\n", msg);
530 (void) write(f, buf, bp + len - buf);
531 exit(1);
532 }
533
534 int
535 do_rlogin(dest, host)
536 struct sockaddr_in *dest;
537 char *host;
538 {
539 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c */
540 int retval;
541
542 getstr(rusername, sizeof(rusername), "remuser too long");
543 getstr(lusername, sizeof(lusername), "locuser too long");
544 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
545
546 pwd = getpwnam(lusername);
547 if (pwd == NULL) {
548 syslog(LOG_INFO,
549 "%s@%s as %s: unknown login.", rusername, host, lusername);
550 return (-1);
551 }
552 retval = iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0, rusername,
553 lusername);
554 /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
555 if (retval == 0) {
556 if (log_success)
557 syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
558 rusername, host, lusername);
559 } else {
560 if (__rcmd_errstr)
561 syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
562 rusername, host, lusername, __rcmd_errstr);
563 else
564 syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
565 rusername, host, lusername);
566 }
567 return(retval);
568 }
569
570 void
571 getstr(buf, cnt, errmsg)
572 char *buf;
573 int cnt;
574 char *errmsg;
575 {
576 char c;
577
578 do {
579 if (read(0, &c, 1) != 1)
580 exit(1);
581 if (--cnt < 0)
582 fatal(STDOUT_FILENO, errmsg, 0);
583 *buf++ = c;
584 } while (c != 0);
585 }
586
587 extern char **environ;
588
589 void
590 setup_term(fd)
591 int fd;
592 {
593 char *cp = index(term+ENVSIZE, '/');
594 char *speed;
595 struct termios tt;
596
597 #ifndef notyet
598 tcgetattr(fd, &tt);
599 if (cp) {
600 *cp++ = '\0';
601 speed = cp;
602 cp = index(speed, '/');
603 if (cp)
604 *cp++ = '\0';
605 cfsetspeed(&tt, atoi(speed));
606 }
607
608 tt.c_iflag = TTYDEF_IFLAG;
609 tt.c_oflag = TTYDEF_OFLAG;
610 tt.c_lflag = TTYDEF_LFLAG;
611 tcsetattr(fd, TCSAFLUSH, &tt);
612 #else
613 if (cp) {
614 *cp++ = '\0';
615 speed = cp;
616 cp = index(speed, '/');
617 if (cp)
618 *cp++ = '\0';
619 tcgetattr(fd, &tt);
620 cfsetspeed(&tt, atoi(speed));
621 tcsetattr(fd, TCSAFLUSH, &tt);
622 }
623 #endif
624
625 env[0] = term;
626 env[1] = 0;
627 environ = env;
628 }
629
630
631 void
632 usage()
633 {
634 syslog(LOG_ERR, "usage: rlogind [-alnL]");
635 }
636
637 /*
638 * Check whether host h is in our local domain,
639 * defined as sharing the last two components of the domain part,
640 * or the entire domain part if the local domain has only one component.
641 * If either name is unqualified (contains no '.'),
642 * assume that the host is local, as it will be
643 * interpreted as such.
644 */
645 int
646 local_domain(h)
647 char *h;
648 {
649 char localhost[MAXHOSTNAMELEN];
650 char *p1, *p2;
651
652 localhost[0] = 0;
653 (void) gethostname(localhost, sizeof(localhost));
654 p1 = topdomain(localhost);
655 p2 = topdomain(h);
656 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
657 return (1);
658 return (0);
659 }
660
661 char *
662 topdomain(h)
663 char *h;
664 {
665 char *p;
666 char *maybe = NULL;
667 int dots = 0;
668
669 for (p = h + strlen(h); p >= h; p--) {
670 if (*p == '.') {
671 if (++dots == 2)
672 return (p);
673 maybe = p;
674 }
675 }
676 return (maybe);
677 }
678