1 /* 2 * Copyright (c) 1983, 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)rlogind.c 5.31 (Berkeley) 01/27/89"; 26 #endif /* not lint */ 27 28 /* 29 * remote login server: 30 * \0 31 * remuser\0 32 * locuser\0 33 * terminal_type/speed\0 34 * data 35 */ 36 37 #include <stdio.h> 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 #include <sys/socket.h> 41 #include <sys/wait.h> 42 #include <sys/file.h> 43 #include <sys/param.h> 44 45 #include <netinet/in.h> 46 47 #include <errno.h> 48 #include <pwd.h> 49 #include <signal.h> 50 #include <sys/ioctl.h> 51 #include <sys/termios.h> 52 #include <stdio.h> 53 #include <netdb.h> 54 #include <syslog.h> 55 #include <strings.h> 56 57 #ifndef TIOCPKT_WINDOW 58 #define TIOCPKT_WINDOW 0x80 59 #endif 60 61 #ifdef KERBEROS 62 #include <kerberos/krb.h> 63 #define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n" 64 65 AUTH_DAT *kdata; 66 KTEXT ticket; 67 u_char auth_buf[sizeof(AUTH_DAT)]; 68 u_char tick_buf[sizeof(KTEXT_ST)]; 69 Key_schedule schedule; 70 int encrypt = 0, retval, use_kerberos = 0, vacuous = 0; 71 int do_krb_login(); 72 73 #define OLD_RCMD 0x00 74 #define KERB_RCMD 0x00 75 #define KERB_RCMD_MUTUAL 0x03 76 77 #define ARGSTR "lnkv" 78 #else 79 #define ARGSTR "ln" 80 #endif /* KERBEROS */ 81 82 char *env[2]; 83 #define NMAX 30 84 char lusername[NMAX+1], rusername[NMAX+1]; 85 static char term[64] = "TERM="; 86 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */ 87 int keepalive = 1; 88 89 #define SUPERUSER(pwd) ((pwd)->pw_uid == 0) 90 91 extern int errno; 92 int reapchild(); 93 struct passwd *getpwnam(), *pwd; 94 char *malloc(); 95 96 main(argc, argv) 97 int argc; 98 char **argv; 99 { 100 extern int opterr, optind, _check_rhosts_file; 101 int ch; 102 int on = 1, fromlen; 103 struct sockaddr_in from; 104 105 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH); 106 107 opterr = 0; 108 while ((ch = getopt(argc, argv, ARGSTR)) != EOF) 109 switch (ch) { 110 case 'l': 111 _check_rhosts_file = 0; 112 break; 113 case 'n': 114 keepalive = 0; 115 break; 116 #ifdef KERBEROS 117 case 'k': 118 use_kerberos = 1; 119 break; 120 case 'v': 121 vacuous = 1; 122 break; 123 #endif 124 case '?': 125 default: 126 usage(); 127 break; 128 } 129 argc -= optind; 130 argv += optind; 131 132 #ifdef KERBEROS 133 if (use_kerberos && vacuous) { 134 usage(); 135 fatal("only one of -k and -v allowed\n"); 136 } 137 #endif 138 fromlen = sizeof (from); 139 if (getpeername(0, &from, &fromlen) < 0) { 140 syslog(LOG_ERR,"Can't get peer name of remote host: %m"); 141 fatalperror("Can't get peer name of remote host"); 142 } 143 if (keepalive && 144 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) 145 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 146 doit(0, &from); 147 } 148 149 int child; 150 int cleanup(); 151 int netf; 152 char *line; 153 extern char *inet_ntoa(); 154 155 struct winsize win = { 0, 0, 0, 0 }; 156 157 158 doit(f, fromp) 159 int f; 160 struct sockaddr_in *fromp; 161 { 162 int i, p, t, pid, on = 1; 163 int authenticated = 0, hostok = 0; 164 register struct hostent *hp; 165 char remotehost[2 * MAXHOSTNAMELEN + 1]; 166 struct hostent hostent; 167 char c; 168 169 alarm(60); 170 read(f, &c, 1); 171 172 #ifdef KERBEROS 173 /* 174 * XXX: 1st char tells us which client we're talking to 175 */ 176 switch (c) { 177 178 case OLD_RCMD: /* NB: OLD_RCMD == KERB_RCMD */ 179 if (vacuous) 180 fatal(f, "Remote host requires Kerberos authentication"); 181 break; 182 183 case KERB_RCMD_MUTUAL: 184 encrypt = 1; 185 break; 186 187 default: 188 fatal(f, "Remote protocol error"); 189 } 190 #else 191 if (c != 0) 192 exit(1); 193 #endif 194 195 alarm(0); 196 fromp->sin_port = ntohs((u_short)fromp->sin_port); 197 hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 198 fromp->sin_family); 199 if (hp == 0) { 200 /* 201 * Only the name is used below. 202 */ 203 hp = &hostent; 204 hp->h_name = inet_ntoa(fromp->sin_addr); 205 hostok++; 206 } else if (local_domain(hp->h_name)) { 207 /* 208 * If name returned by gethostbyaddr is in our domain, 209 * attempt to verify that we haven't been fooled by someone 210 * in a remote net; look up the name and check that this 211 * address corresponds to the name. 212 */ 213 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 214 remotehost[sizeof(remotehost) - 1] = 0; 215 hp = gethostbyname(remotehost); 216 if (hp) 217 for (; hp->h_addr_list[0]; hp->h_addr_list++) 218 if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr, 219 sizeof(fromp->sin_addr))) { 220 hostok++; 221 break; 222 } 223 } else 224 hostok++; 225 226 #ifdef KERBEROS 227 if (use_kerberos) { 228 retval = do_krb_login(hp->h_name, fromp, encrypt); 229 write(f, &c, 1); 230 if (retval == 0 && hostok) 231 authenticated++; 232 else if (retval > 0) 233 fatal(f, krb_err_txt[retval]); 234 else if(!hostok) 235 fatal(f, "krlogind: Host address mismatch.\r\n"); 236 } else 237 #endif 238 { 239 if (fromp->sin_family != AF_INET || 240 fromp->sin_port >= IPPORT_RESERVED || 241 fromp->sin_port < IPPORT_RESERVED/2) { 242 syslog(LOG_NOTICE, "Connection from %s on illegal port", 243 inet_ntoa(fromp->sin_addr)); 244 fatal(f, "Permission denied"); 245 } 246 write(f, "", 1); 247 248 if (do_rlogin(hp->h_name) == 0) { 249 if (hostok) 250 authenticated++; 251 else 252 write(f, "rlogind: Host address mismatch.\r\n", 253 sizeof("rlogind: Host address mismatch.\r\n") - 1); 254 } 255 } 256 257 for (c = 'p'; c <= 's'; c++) { 258 struct stat stb; 259 line = "/dev/ptyXX"; 260 line[strlen("/dev/pty")] = c; 261 line[strlen("/dev/ptyp")] = '0'; 262 if (stat(line, &stb) < 0) 263 break; 264 for (i = 0; i < 16; i++) { 265 line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i]; 266 p = open(line, O_RDWR); 267 if (p > 0) 268 goto gotpty; 269 } 270 } 271 fatal(f, "Out of ptys"); 272 /*NOTREACHED*/ 273 gotpty: 274 (void) ioctl(p, TIOCSWINSZ, &win); 275 netf = f; 276 line[strlen("/dev/")] = 't'; 277 t = open(line, O_RDWR); 278 if (t < 0) 279 fatalperror(f, line); 280 if (fchmod(t, 0)) 281 fatalperror(f, line); 282 (void)signal(SIGHUP, SIG_IGN); 283 vhangup(); 284 (void)signal(SIGHUP, SIG_DFL); 285 t = open(line, O_RDWR); 286 if (t < 0) 287 fatalperror(f, line); 288 setup_term(t); 289 #ifdef DEBUG 290 { 291 int tt = open("/dev/tty", O_RDWR); 292 if (tt > 0) { 293 (void)ioctl(tt, TIOCNOTTY, 0); 294 (void)close(tt); 295 } 296 } 297 #endif 298 pid = fork(); 299 if (pid < 0) 300 fatalperror(f, ""); 301 if (pid == 0) { 302 if (setsid() < 0) 303 fatalperror(f, "setsid"); 304 if (ioctl(t, TIOCSCTTY, 0) < 0) 305 fatalperror(f, "ioctl(sctty)"); 306 close(f), close(p); 307 dup2(t, 0), dup2(t, 1), dup2(t, 2); 308 close(t); 309 if (authenticated) 310 execl("/bin/login", "login", "-p", 311 "-h", hp->h_name, "-f", lusername, 0); 312 else 313 execl("/bin/login", "login", "-p", 314 "-h", hp->h_name, lusername, 0); 315 fatalperror(2, "/bin/login"); 316 /*NOTREACHED*/ 317 } 318 close(t); 319 320 #ifdef KERBEROS 321 /* 322 * If encrypted, don't turn on NBIO or the des read/write 323 * routines will croak. 324 */ 325 326 if (encrypt) 327 (void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE)); 328 else 329 #endif 330 ioctl(f, FIONBIO, &on); 331 ioctl(p, FIONBIO, &on); 332 ioctl(p, TIOCPKT, &on); 333 signal(SIGTSTP, SIG_IGN); 334 signal(SIGCHLD, cleanup); 335 setpgrp(0, 0); 336 protocol(f, p); 337 signal(SIGCHLD, SIG_IGN); 338 cleanup(); 339 } 340 341 char magic[2] = { 0377, 0377 }; 342 char oobdata[] = {TIOCPKT_WINDOW}; 343 344 /* 345 * Handle a "control" request (signaled by magic being present) 346 * in the data stream. For now, we are only willing to handle 347 * window size changes. 348 */ 349 control(pty, cp, n) 350 int pty; 351 char *cp; 352 int n; 353 { 354 struct winsize w; 355 356 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 357 return (0); 358 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 359 bcopy(cp+4, (char *)&w, sizeof(w)); 360 w.ws_row = ntohs(w.ws_row); 361 w.ws_col = ntohs(w.ws_col); 362 w.ws_xpixel = ntohs(w.ws_xpixel); 363 w.ws_ypixel = ntohs(w.ws_ypixel); 364 (void)ioctl(pty, TIOCSWINSZ, &w); 365 return (4+sizeof (w)); 366 } 367 368 /* 369 * rlogin "protocol" machine. 370 */ 371 protocol(f, p) 372 int f, p; 373 { 374 char pibuf[1024], fibuf[1024], *pbp, *fbp; 375 register pcc = 0, fcc = 0; 376 int cc, nfd, pmask, fmask; 377 char cntl; 378 379 /* 380 * Must ignore SIGTTOU, otherwise we'll stop 381 * when we try and set slave pty's window shape 382 * (our controlling tty is the master pty). 383 */ 384 (void) signal(SIGTTOU, SIG_IGN); 385 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 386 if (f > p) 387 nfd = f + 1; 388 else 389 nfd = p + 1; 390 fmask = 1 << f; 391 pmask = 1 << p; 392 for (;;) { 393 int ibits, obits, ebits; 394 395 ibits = 0; 396 obits = 0; 397 if (fcc) 398 obits |= pmask; 399 else 400 ibits |= fmask; 401 if (pcc >= 0) 402 if (pcc) 403 obits |= fmask; 404 else 405 ibits |= pmask; 406 ebits = pmask; 407 if (select(nfd, &ibits, obits ? &obits : (int *)NULL, 408 &ebits, 0) < 0) { 409 if (errno == EINTR) 410 continue; 411 fatalperror(f, "select"); 412 } 413 if (ibits == 0 && obits == 0 && ebits == 0) { 414 /* shouldn't happen... */ 415 sleep(5); 416 continue; 417 } 418 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 419 if (ebits & pmask) { 420 cc = read(p, &cntl, 1); 421 if (cc == 1 && pkcontrol(cntl)) { 422 cntl |= oobdata[0]; 423 send(f, &cntl, 1, MSG_OOB); 424 if (cntl & TIOCPKT_FLUSHWRITE) { 425 pcc = 0; 426 ibits &= ~pmask; 427 } 428 } 429 } 430 if (ibits & fmask) { 431 #ifdef KERBEROS 432 if (encrypt) 433 fcc = des_read(f, fibuf, sizeof(fibuf)); 434 else 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 obits |= pmask; /* try write */ 462 } 463 } 464 465 if ((obits & pmask) && fcc > 0) { 466 cc = write(p, fbp, fcc); 467 if (cc > 0) { 468 fcc -= cc; 469 fbp += cc; 470 } 471 } 472 473 if (ibits & pmask) { 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 KERBEROS 483 if (!encrypt) 484 #endif 485 obits |= fmask; /* try a write */ 486 } else { 487 if (pkcontrol(pibuf[0])) { 488 pibuf[0] |= oobdata[0]; 489 send(f, &pibuf[0], 1, MSG_OOB); 490 } 491 pcc = 0; 492 } 493 } 494 if ((obits & fmask) && pcc > 0) { 495 #ifdef KERBEROS 496 if (encrypt) 497 cc = des_write(f, pbp, pcc); 498 else 499 #endif 500 cc = write(f, pbp, pcc); 501 if (cc < 0 && errno == EWOULDBLOCK) { 502 /* also shouldn't happen */ 503 sleep(5); 504 continue; 505 } 506 if (cc > 0) { 507 pcc -= cc; 508 pbp += cc; 509 } 510 } 511 } 512 } 513 514 cleanup() 515 { 516 char *p; 517 518 p = line + sizeof("/dev/") - 1; 519 if (logout(p)) 520 logwtmp(p, "", ""); 521 (void)chmod(line, 0666); 522 (void)chown(line, 0, 0); 523 *p = 'p'; 524 (void)chmod(line, 0666); 525 (void)chown(line, 0, 0); 526 shutdown(netf, 2); 527 exit(1); 528 } 529 530 fatal(f, msg) 531 int f; 532 char *msg; 533 { 534 char buf[BUFSIZ]; 535 536 buf[0] = '\01'; /* error indicator */ 537 (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 538 (void) write(f, buf, strlen(buf)); 539 exit(1); 540 } 541 542 fatalperror(f, msg) 543 int f; 544 char *msg; 545 { 546 char buf[BUFSIZ]; 547 extern int sys_nerr; 548 extern char *sys_errlist[]; 549 550 if ((unsigned)errno < sys_nerr) 551 (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 552 else 553 (void) sprintf(buf, "%s: Error %d", msg, errno); 554 fatal(f, buf); 555 } 556 557 do_rlogin(host) 558 char *host; 559 { 560 561 getstr(rusername, sizeof(rusername), "remuser too long"); 562 getstr(lusername, sizeof(lusername), "locuser too long"); 563 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long"); 564 565 if (getuid()) 566 return(-1); 567 pwd = getpwnam(lusername); 568 if (pwd == NULL) 569 return(-1); 570 return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); 571 } 572 573 574 getstr(buf, cnt, errmsg) 575 char *buf; 576 int cnt; 577 char *errmsg; 578 { 579 char c; 580 581 do { 582 if (read(0, &c, 1) != 1) 583 exit(1); 584 if (--cnt < 0) 585 fatal(1, errmsg); 586 *buf++ = c; 587 } while (c != 0); 588 } 589 590 extern char **environ; 591 592 setup_term(fd) 593 int fd; 594 { 595 struct termios tt; 596 register char *cp = index(term+ENVSIZE, '/'); 597 char *speed; 598 599 tcgetattr(fd, &tt); 600 if (cp) { 601 *cp++ = '\0'; 602 speed = cp; 603 cp = index(speed, '/'); 604 if (cp) 605 *cp++ = '\0'; 606 cfsetspeed(&tt, atoi(speed)); 607 } 608 tt.c_iflag = BRKINT|ICRNL|IXON|ISTRIP|IEXTEN|IMAXBEL; 609 tt.c_oflag = OPOST|ONLCR|OXTABS; 610 tt.c_lflag = ISIG|ICANON|ECHO; 611 tcsetattr(fd, TCSADFLUSH, &tt); 612 613 env[0] = term; 614 env[1] = 0; 615 environ = env; 616 } 617 618 #ifdef KERBEROS 619 #define VERSION_SIZE 9 620 621 /* 622 * Do the remote kerberos login to the named host with the 623 * given inet address 624 * 625 * Return 0 on valid authorization 626 * Return -1 on valid authentication, no authorization 627 * Return >0 for error conditions 628 */ 629 do_krb_login(host, dest, encrypt) 630 char *host; 631 struct sockaddr_in *dest; 632 int encrypt; 633 { 634 int rc; 635 char instance[INST_SZ], version[VERSION_SIZE]; 636 long authopts = 0L; /* !mutual */ 637 struct sockaddr_in faddr; 638 639 if (getuid()) 640 return(KFAILURE); 641 642 kdata = (AUTH_DAT *) auth_buf; 643 ticket = (KTEXT) tick_buf; 644 strcpy(instance, "*"); 645 646 if (encrypt) { 647 rc = sizeof(faddr); 648 if (getsockname(0, &faddr, &rc)) 649 return(-1); 650 authopts = KOPT_DO_MUTUAL; 651 rc = krb_recvauth( 652 authopts, 0, 653 ticket, "rcmd", 654 instance, dest, &faddr, 655 kdata, "", schedule, version); 656 des_set_key(kdata->session, schedule); 657 658 } else { 659 rc = krb_recvauth( 660 authopts, 0, 661 ticket, "rcmd", 662 instance, dest, (struct sockaddr_in *) 0, 663 kdata, "", (bit_64 *) 0, version); 664 } 665 666 if (rc != KSUCCESS) 667 return(rc); 668 669 if ((rc = krb_kntoln(kdata, rusername)) != KSUCCESS) 670 return(rc); 671 672 getstr(lusername, sizeof(lusername), "locuser"); 673 /* get the "cmd" in the rcmd protocol */ 674 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type"); 675 676 pwd = getpwnam(lusername); 677 if (pwd == NULL) 678 return(-1); 679 680 /* returns nonzero for no access */ 681 /* return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); */ 682 if (kuserok(kdata,lusername) != 0) 683 return(-1); 684 685 return(0); 686 687 } 688 689 #endif /* KERBEROS */ 690 691 usage() 692 { 693 #ifdef KERBEROS 694 syslog(LOG_ERR, "usage: rlogind [-k | -v] [-l] [-n]"); 695 #else 696 syslog(LOG_ERR, "usage: rlogind [-l] [-n]"); 697 #endif 698 } 699 700 /* 701 * Check whether host h is in our local domain, 702 * as determined by the part of the name following 703 * the first '.' in its name and in ours. 704 * If either name is unqualified (contains no '.'), 705 * assume that the host is local, as it will be 706 * interpreted as such. 707 */ 708 local_domain(h) 709 char *h; 710 { 711 char localhost[MAXHOSTNAMELEN]; 712 char *p1, *p2 = index(h, '.'); 713 714 (void) gethostname(localhost, sizeof(localhost)); 715 p1 = index(localhost, '.'); 716 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 717 return(1); 718 return(0); 719 } 720