rlogind.c revision 1592
1/*- 2 * Copyright (c) 1983, 1988, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1983, 1988, 1989, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93"; 42#endif /* not lint */ 43 44/* 45 * remote login server: 46 * \0 47 * remuser\0 48 * locuser\0 49 * terminal_type/speed\0 50 * data 51 */ 52 53#define FD_SETSIZE 16 /* don't need many bits for select */ 54#include <sys/param.h> 55#include <sys/stat.h> 56#include <sys/ioctl.h> 57#include <signal.h> 58#include <termios.h> 59 60#include <sys/socket.h> 61#include <netinet/in.h> 62#include <netinet/in_systm.h> 63#include <netinet/ip.h> 64#include <arpa/inet.h> 65#include <netdb.h> 66 67#include <pwd.h> 68#include <syslog.h> 69#include <errno.h> 70#include <stdio.h> 71#include <unistd.h> 72#include <stdlib.h> 73#include <string.h> 74#include "pathnames.h" 75 76#ifndef TIOCPKT_WINDOW 77#define TIOCPKT_WINDOW 0x80 78#endif 79 80#ifdef KERBEROS 81#include <kerberosIV/des.h> 82#include <kerberosIV/krb.h> 83#define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n" 84 85AUTH_DAT *kdata; 86KTEXT ticket; 87u_char auth_buf[sizeof(AUTH_DAT)]; 88u_char tick_buf[sizeof(KTEXT_ST)]; 89Key_schedule schedule; 90int doencrypt, retval, use_kerberos, vacuous; 91 92#define ARGSTR "alnkvx" 93#else 94#define ARGSTR "aln" 95#endif /* KERBEROS */ 96 97char *env[2]; 98#define NMAX 30 99char lusername[NMAX+1], rusername[NMAX+1]; 100static char term[64] = "TERM="; 101#define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */ 102int keepalive = 1; 103int check_all = 0; 104 105struct passwd *pwd; 106 107void doit __P((int, struct sockaddr_in *)); 108int control __P((int, char *, int)); 109void protocol __P((int, int)); 110void cleanup __P((int)); 111void fatal __P((int, char *, int)); 112int do_rlogin __P((struct sockaddr_in *)); 113void getstr __P((char *, int, char *)); 114void setup_term __P((int)); 115int do_krb_login __P((struct sockaddr_in *)); 116void usage __P((void)); 117int local_domain __P((char *)); 118char *topdomain __P((char *)); 119 120int 121main(argc, argv) 122 int argc; 123 char *argv[]; 124{ 125 extern int __check_rhosts_file; 126 struct sockaddr_in from; 127 int ch, fromlen, on; 128 129 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH); 130 131 opterr = 0; 132 while ((ch = getopt(argc, argv, ARGSTR)) != EOF) 133 switch (ch) { 134 case 'a': 135 check_all = 1; 136 break; 137 case 'l': 138 __check_rhosts_file = 0; 139 break; 140 case 'n': 141 keepalive = 0; 142 break; 143#ifdef KERBEROS 144 case 'k': 145 use_kerberos = 1; 146 break; 147 case 'v': 148 vacuous = 1; 149 break; 150#ifdef CRYPT 151 case 'x': 152 doencrypt = 1; 153 break; 154#endif 155#endif 156 case '?': 157 default: 158 usage(); 159 break; 160 } 161 argc -= optind; 162 argv += optind; 163 164#ifdef KERBEROS 165 if (use_kerberos && vacuous) { 166 usage(); 167 fatal(STDERR_FILENO, "only one of -k and -v allowed", 0); 168 } 169#endif 170 fromlen = sizeof (from); 171 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 172 syslog(LOG_ERR,"Can't get peer name of remote host: %m"); 173 fatal(STDERR_FILENO, "Can't get peer name of remote host", 1); 174 } 175 on = 1; 176 if (keepalive && 177 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) 178 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 179 on = IPTOS_LOWDELAY; 180 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 181 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 182 doit(0, &from); 183} 184 185int child; 186int netf; 187char line[MAXPATHLEN]; 188int confirmed; 189 190struct winsize win = { 0, 0, 0, 0 }; 191 192 193void 194doit(f, fromp) 195 int f; 196 struct sockaddr_in *fromp; 197{ 198 int master, pid, on = 1; 199 int authenticated = 0; 200 register struct hostent *hp; 201 char hostname[2 * MAXHOSTNAMELEN + 1]; 202 char c; 203 204 alarm(60); 205 read(f, &c, 1); 206 207 if (c != 0) 208 exit(1); 209#ifdef KERBEROS 210 if (vacuous) 211 fatal(f, "Remote host requires Kerberos authentication", 0); 212#endif 213 214 alarm(0); 215 fromp->sin_port = ntohs((u_short)fromp->sin_port); 216 hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(struct in_addr), 217 fromp->sin_family); 218 if (hp) 219 (void)strcpy(hostname, hp->h_name); 220 else 221 (void)strcpy(hostname, inet_ntoa(fromp->sin_addr)); 222 223#ifdef KERBEROS 224 if (use_kerberos) { 225 retval = do_krb_login(fromp); 226 if (retval == 0) 227 authenticated++; 228 else if (retval > 0) 229 fatal(f, krb_err_txt[retval], 0); 230 write(f, &c, 1); 231 confirmed = 1; /* we sent the null! */ 232 } else 233#endif 234 { 235 if (fromp->sin_family != AF_INET || 236 fromp->sin_port >= IPPORT_RESERVED || 237 fromp->sin_port < IPPORT_RESERVED/2) { 238 syslog(LOG_NOTICE, "Connection from %s on illegal port", 239 inet_ntoa(fromp->sin_addr)); 240 fatal(f, "Permission denied", 0); 241 } 242#ifdef IP_OPTIONS 243 { 244 u_char optbuf[BUFSIZ/3], *cp; 245 char lbuf[BUFSIZ], *lp; 246 int optsize = sizeof(optbuf), ipproto; 247 struct protoent *ip; 248 249 if ((ip = getprotobyname("ip")) != NULL) 250 ipproto = ip->p_proto; 251 else 252 ipproto = IPPROTO_IP; 253 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, 254 &optsize) == 0 && optsize != 0) { 255 lp = lbuf; 256 for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3) 257 sprintf(lp, " %2.2x", *cp); 258 syslog(LOG_NOTICE, 259 "Connection received using IP options (ignored):%s", 260 lbuf); 261 if (setsockopt(0, ipproto, IP_OPTIONS, 262 (char *)NULL, optsize) != 0) { 263 syslog(LOG_ERR, 264 "setsockopt IP_OPTIONS NULL: %m"); 265 exit(1); 266 } 267 } 268 } 269#endif 270 if (do_rlogin(fromp) == 0) 271 authenticated++; 272 } 273 if (confirmed == 0) { 274 write(f, "", 1); 275 confirmed = 1; /* we sent the null! */ 276 } 277#ifdef KERBEROS 278#ifdef CRYPT 279 if (doencrypt) 280 (void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE) - 1); 281#endif 282#endif 283 netf = f; 284 285 pid = forkpty(&master, line, NULL, &win); 286 if (pid < 0) { 287 if (errno == ENOENT) 288 fatal(f, "Out of ptys", 0); 289 else 290 fatal(f, "Forkpty", 1); 291 } 292 if (pid == 0) { 293 if (f > 2) /* f should always be 0, but... */ 294 (void) close(f); 295 setup_term(0); 296 if (authenticated) { 297#ifdef KERBEROS 298 if (use_kerberos && (pwd->pw_uid == 0)) 299 syslog(LOG_INFO|LOG_AUTH, 300 "ROOT Kerberos login from %s.%s@%s on %s\n", 301 kdata->pname, kdata->pinst, kdata->prealm, 302 hostname); 303#endif 304 305 execl(_PATH_LOGIN, "login", "-p", 306 "-h", hostname, "-f", lusername, (char *)NULL); 307 } else 308 execl(_PATH_LOGIN, "login", "-p", 309 "-h", hostname, lusername, (char *)NULL); 310 fatal(STDERR_FILENO, _PATH_LOGIN, 1); 311 /*NOTREACHED*/ 312 } 313#ifdef CRYPT 314#ifdef KERBEROS 315 /* 316 * If encrypted, don't turn on NBIO or the des read/write 317 * routines will croak. 318 */ 319 320 if (!doencrypt) 321#endif 322#endif 323 ioctl(f, FIONBIO, &on); 324 ioctl(master, FIONBIO, &on); 325 ioctl(master, TIOCPKT, &on); 326 signal(SIGCHLD, cleanup); 327 protocol(f, master); 328 signal(SIGCHLD, SIG_IGN); 329 cleanup(0); 330} 331 332char magic[2] = { 0377, 0377 }; 333char oobdata[] = {TIOCPKT_WINDOW}; 334 335/* 336 * Handle a "control" request (signaled by magic being present) 337 * in the data stream. For now, we are only willing to handle 338 * window size changes. 339 */ 340int 341control(pty, cp, n) 342 int pty; 343 char *cp; 344 int n; 345{ 346 struct winsize w; 347 348 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 349 return (0); 350 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 351 bcopy(cp+4, (char *)&w, sizeof(w)); 352 w.ws_row = ntohs(w.ws_row); 353 w.ws_col = ntohs(w.ws_col); 354 w.ws_xpixel = ntohs(w.ws_xpixel); 355 w.ws_ypixel = ntohs(w.ws_ypixel); 356 (void)ioctl(pty, TIOCSWINSZ, &w); 357 return (4+sizeof (w)); 358} 359 360/* 361 * rlogin "protocol" machine. 362 */ 363void 364protocol(f, p) 365 register int f, p; 366{ 367 char pibuf[1024+1], fibuf[1024], *pbp, *fbp; 368 register pcc = 0, fcc = 0; 369 int cc, nfd, n; 370 char cntl; 371 372 /* 373 * Must ignore SIGTTOU, otherwise we'll stop 374 * when we try and set slave pty's window shape 375 * (our controlling tty is the master pty). 376 */ 377 (void) signal(SIGTTOU, SIG_IGN); 378 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 379 if (f > p) 380 nfd = f + 1; 381 else 382 nfd = p + 1; 383 if (nfd > FD_SETSIZE) { 384 syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE"); 385 fatal(f, "internal error (select mask too small)", 0); 386 } 387 for (;;) { 388 fd_set ibits, obits, ebits, *omask; 389 390 FD_ZERO(&ebits); 391 FD_ZERO(&ibits); 392 FD_ZERO(&obits); 393 omask = (fd_set *)NULL; 394 if (fcc) { 395 FD_SET(p, &obits); 396 omask = &obits; 397 } else 398 FD_SET(f, &ibits); 399 if (pcc >= 0) 400 if (pcc) { 401 FD_SET(f, &obits); 402 omask = &obits; 403 } else 404 FD_SET(p, &ibits); 405 FD_SET(p, &ebits); 406 if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) { 407 if (errno == EINTR) 408 continue; 409 fatal(f, "select", 1); 410 } 411 if (n == 0) { 412 /* shouldn't happen... */ 413 sleep(5); 414 continue; 415 } 416#define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 417 if (FD_ISSET(p, &ebits)) { 418 cc = read(p, &cntl, 1); 419 if (cc == 1 && pkcontrol(cntl)) { 420 cntl |= oobdata[0]; 421 send(f, &cntl, 1, MSG_OOB); 422 if (cntl & TIOCPKT_FLUSHWRITE) { 423 pcc = 0; 424 FD_CLR(p, &ibits); 425 } 426 } 427 } 428 if (FD_ISSET(f, &ibits)) { 429#ifdef CRYPT 430#ifdef KERBEROS 431 if (doencrypt) 432 fcc = des_read(f, fibuf, sizeof(fibuf)); 433 else 434#endif 435#endif 436 fcc = read(f, fibuf, sizeof(fibuf)); 437 if (fcc < 0 && errno == EWOULDBLOCK) 438 fcc = 0; 439 else { 440 register char *cp; 441 int left, n; 442 443 if (fcc <= 0) 444 break; 445 fbp = fibuf; 446 447 top: 448 for (cp = fibuf; cp < fibuf+fcc-1; cp++) 449 if (cp[0] == magic[0] && 450 cp[1] == magic[1]) { 451 left = fcc - (cp-fibuf); 452 n = control(p, cp, left); 453 if (n) { 454 left -= n; 455 if (left > 0) 456 bcopy(cp+n, cp, left); 457 fcc -= n; 458 goto top; /* n^2 */ 459 } 460 } 461 FD_SET(p, &obits); /* try write */ 462 } 463 } 464 465 if (FD_ISSET(p, &obits) && fcc > 0) { 466 cc = write(p, fbp, fcc); 467 if (cc > 0) { 468 fcc -= cc; 469 fbp += cc; 470 } 471 } 472 473 if (FD_ISSET(p, &ibits)) { 474 pcc = read(p, pibuf, sizeof (pibuf)); 475 pbp = pibuf; 476 if (pcc < 0 && errno == EWOULDBLOCK) 477 pcc = 0; 478 else if (pcc <= 0) 479 break; 480 else if (pibuf[0] == 0) { 481 pbp++, pcc--; 482#ifdef CRYPT 483#ifdef KERBEROS 484 if (!doencrypt) 485#endif 486#endif 487 FD_SET(f, &obits); /* try write */ 488 } else { 489 if (pkcontrol(pibuf[0])) { 490 pibuf[0] |= oobdata[0]; 491 send(f, &pibuf[0], 1, MSG_OOB); 492 } 493 pcc = 0; 494 } 495 } 496 if ((FD_ISSET(f, &obits)) && pcc > 0) { 497#ifdef CRYPT 498#ifdef KERBEROS 499 if (doencrypt) 500 cc = des_write(f, pbp, pcc); 501 else 502#endif 503#endif 504 cc = write(f, pbp, pcc); 505 if (cc < 0 && errno == EWOULDBLOCK) { 506 /* 507 * This happens when we try write after read 508 * from p, but some old kernels balk at large 509 * writes even when select returns true. 510 */ 511 if (!FD_ISSET(p, &ibits)) 512 sleep(5); 513 continue; 514 } 515 if (cc > 0) { 516 pcc -= cc; 517 pbp += cc; 518 } 519 } 520 } 521} 522 523void 524cleanup(signo) 525 int signo; 526{ 527 char *p; 528 529 p = line + sizeof(_PATH_DEV) - 1; 530 if (logout(p)) 531 logwtmp(p, "", ""); 532 (void)chmod(line, 0666); 533 (void)chown(line, 0, 0); 534 *p = 'p'; 535 (void)chmod(line, 0666); 536 (void)chown(line, 0, 0); 537 shutdown(netf, 2); 538 exit(1); 539} 540 541void 542fatal(f, msg, syserr) 543 int f; 544 char *msg; 545 int syserr; 546{ 547 int len; 548 char buf[BUFSIZ], *bp = buf; 549 550 /* 551 * Prepend binary one to message if we haven't sent 552 * the magic null as confirmation. 553 */ 554 if (!confirmed) 555 *bp++ = '\01'; /* error indicator */ 556 if (syserr) 557 len = sprintf(bp, "rlogind: %s: %s.\r\n", 558 msg, strerror(errno)); 559 else 560 len = sprintf(bp, "rlogind: %s.\r\n", msg); 561 (void) write(f, buf, bp + len - buf); 562 exit(1); 563} 564 565int 566do_rlogin(dest) 567 struct sockaddr_in *dest; 568{ 569 getstr(rusername, sizeof(rusername), "remuser too long"); 570 getstr(lusername, sizeof(lusername), "locuser too long"); 571 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long"); 572 573 pwd = getpwnam(lusername); 574 if (pwd == NULL) 575 return (-1); 576 if (pwd->pw_uid == 0) 577 return (-1); 578 /* XXX why don't we syslog() failure? */ 579 return (iruserok(dest->sin_addr.s_addr, 0, rusername, lusername)); 580} 581 582void 583getstr(buf, cnt, errmsg) 584 char *buf; 585 int cnt; 586 char *errmsg; 587{ 588 char c; 589 590 do { 591 if (read(0, &c, 1) != 1) 592 exit(1); 593 if (--cnt < 0) 594 fatal(STDOUT_FILENO, errmsg, 0); 595 *buf++ = c; 596 } while (c != 0); 597} 598 599extern char **environ; 600 601void 602setup_term(fd) 603 int fd; 604{ 605 register char *cp = index(term+ENVSIZE, '/'); 606 char *speed; 607 struct termios tt; 608 609#ifndef notyet 610 tcgetattr(fd, &tt); 611 if (cp) { 612 *cp++ = '\0'; 613 speed = cp; 614 cp = index(speed, '/'); 615 if (cp) 616 *cp++ = '\0'; 617 cfsetspeed(&tt, atoi(speed)); 618 } 619 620 tt.c_iflag = TTYDEF_IFLAG; 621 tt.c_oflag = TTYDEF_OFLAG; 622 tt.c_lflag = TTYDEF_LFLAG; 623 tcsetattr(fd, TCSAFLUSH, &tt); 624#else 625 if (cp) { 626 *cp++ = '\0'; 627 speed = cp; 628 cp = index(speed, '/'); 629 if (cp) 630 *cp++ = '\0'; 631 tcgetattr(fd, &tt); 632 cfsetspeed(&tt, atoi(speed)); 633 tcsetattr(fd, TCSAFLUSH, &tt); 634 } 635#endif 636 637 env[0] = term; 638 env[1] = 0; 639 environ = env; 640} 641 642#ifdef KERBEROS 643#define VERSION_SIZE 9 644 645/* 646 * Do the remote kerberos login to the named host with the 647 * given inet address 648 * 649 * Return 0 on valid authorization 650 * Return -1 on valid authentication, no authorization 651 * Return >0 for error conditions 652 */ 653int 654do_krb_login(dest) 655 struct sockaddr_in *dest; 656{ 657 int rc; 658 char instance[INST_SZ], version[VERSION_SIZE]; 659 long authopts = 0L; /* !mutual */ 660 struct sockaddr_in faddr; 661 662 kdata = (AUTH_DAT *) auth_buf; 663 ticket = (KTEXT) tick_buf; 664 665 instance[0] = '*'; 666 instance[1] = '\0'; 667 668#ifdef CRYPT 669 if (doencrypt) { 670 rc = sizeof(faddr); 671 if (getsockname(0, (struct sockaddr *)&faddr, &rc)) 672 return (-1); 673 authopts = KOPT_DO_MUTUAL; 674 rc = krb_recvauth( 675 authopts, 0, 676 ticket, "rcmd", 677 instance, dest, &faddr, 678 kdata, "", schedule, version); 679 des_set_key(kdata->session, schedule); 680 681 } else 682#endif 683 rc = krb_recvauth( 684 authopts, 0, 685 ticket, "rcmd", 686 instance, dest, (struct sockaddr_in *) 0, 687 kdata, "", (bit_64 *) 0, version); 688 689 if (rc != KSUCCESS) 690 return (rc); 691 692 getstr(lusername, sizeof(lusername), "locuser"); 693 /* get the "cmd" in the rcmd protocol */ 694 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type"); 695 696 pwd = getpwnam(lusername); 697 if (pwd == NULL) 698 return (-1); 699 700 /* returns nonzero for no access */ 701 if (kuserok(kdata, lusername) != 0) 702 return (-1); 703 704 return (0); 705 706} 707#endif /* KERBEROS */ 708 709void 710usage() 711{ 712#ifdef KERBEROS 713 syslog(LOG_ERR, "usage: rlogind [-aln] [-k | -v]"); 714#else 715 syslog(LOG_ERR, "usage: rlogind [-aln]"); 716#endif 717} 718 719/* 720 * Check whether host h is in our local domain, 721 * defined as sharing the last two components of the domain part, 722 * or the entire domain part if the local domain has only one component. 723 * If either name is unqualified (contains no '.'), 724 * assume that the host is local, as it will be 725 * interpreted as such. 726 */ 727int 728local_domain(h) 729 char *h; 730{ 731 char localhost[MAXHOSTNAMELEN]; 732 char *p1, *p2; 733 734 localhost[0] = 0; 735 (void) gethostname(localhost, sizeof(localhost)); 736 p1 = topdomain(localhost); 737 p2 = topdomain(h); 738 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 739 return (1); 740 return (0); 741} 742 743char * 744topdomain(h) 745 char *h; 746{ 747 register char *p; 748 char *maybe = NULL; 749 int dots = 0; 750 751 for (p = h + strlen(h); p >= h; p--) { 752 if (*p == '.') { 753 if (++dots == 2) 754 return (p); 755 maybe = p; 756 } 757 } 758 return (maybe); 759} 760