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.29 (Berkeley) 01/25/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, 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: /* OLD_RCMD is same as 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) 231 authenticated++; 232 else if (retval > 0) 233 fatal(f, krb_err_txt[retval]); 234 } else 235 #endif 236 if (fromp->sin_family != AF_INET || 237 fromp->sin_port >= IPPORT_RESERVED || 238 fromp->sin_port < IPPORT_RESERVED/2) { 239 syslog(LOG_NOTICE, "Connection from %s on illegal port", 240 inet_ntoa(fromp->sin_addr)); 241 fatal(f, "Permission denied"); 242 } 243 write(f, "", 1); 244 245 if (do_rlogin(hp->h_name) == 0) { 246 if (hostok) 247 authenticated++; 248 else 249 write(f, "rlogind: Host address mismatch.\r\n", 250 sizeof("rlogind: Host address mismatch.\r\n") - 1); 251 } 252 253 for (c = 'p'; c <= 's'; c++) { 254 struct stat stb; 255 line = "/dev/ptyXX"; 256 line[strlen("/dev/pty")] = c; 257 line[strlen("/dev/ptyp")] = '0'; 258 if (stat(line, &stb) < 0) 259 break; 260 for (i = 0; i < 16; i++) { 261 line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i]; 262 p = open(line, O_RDWR); 263 if (p > 0) 264 goto gotpty; 265 } 266 } 267 fatal(f, "Out of ptys"); 268 /*NOTREACHED*/ 269 gotpty: 270 (void) ioctl(p, TIOCSWINSZ, &win); 271 netf = f; 272 line[strlen("/dev/")] = 't'; 273 t = open(line, O_RDWR); 274 if (t < 0) 275 fatalperror(f, line); 276 if (fchmod(t, 0)) 277 fatalperror(f, line); 278 (void)signal(SIGHUP, SIG_IGN); 279 vhangup(); 280 (void)signal(SIGHUP, SIG_DFL); 281 t = open(line, O_RDWR); 282 if (t < 0) 283 fatalperror(f, line); 284 setup_term(t); 285 #ifdef DEBUG 286 { 287 int tt = open("/dev/tty", O_RDWR); 288 if (tt > 0) { 289 (void)ioctl(tt, TIOCNOTTY, 0); 290 (void)close(tt); 291 } 292 } 293 #endif 294 pid = fork(); 295 if (pid < 0) 296 fatalperror(f, ""); 297 if (pid == 0) { 298 if (setsid() < 0) 299 fatalperror(f, "setsid"); 300 if (ioctl(t, TIOCSCTTY, 0) < 0) 301 fatalperror(f, "ioctl(sctty)"); 302 close(f), close(p); 303 dup2(t, 0), dup2(t, 1), dup2(t, 2); 304 close(t); 305 if (authenticated) 306 execl("/bin/login", "login", "-p", 307 "-h", hp->h_name, "-f", lusername, 0); 308 else 309 execl("/bin/login", "login", "-p", 310 "-h", hp->h_name, lusername, 0); 311 fatalperror(2, "/bin/login"); 312 /*NOTREACHED*/ 313 } 314 close(t); 315 316 #ifdef KERBEROS 317 /* 318 * If encrypted, don't turn on NBIO or the des read/write 319 * routines will croak. 320 */ 321 322 if (encrypt) 323 (void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE)); 324 else 325 #endif 326 ioctl(f, FIONBIO, &on); 327 ioctl(p, FIONBIO, &on); 328 ioctl(p, TIOCPKT, &on); 329 signal(SIGTSTP, SIG_IGN); 330 signal(SIGCHLD, cleanup); 331 setpgrp(0, 0); 332 protocol(f, p); 333 signal(SIGCHLD, SIG_IGN); 334 cleanup(); 335 } 336 337 char magic[2] = { 0377, 0377 }; 338 char oobdata[] = {TIOCPKT_WINDOW}; 339 340 /* 341 * Handle a "control" request (signaled by magic being present) 342 * in the data stream. For now, we are only willing to handle 343 * window size changes. 344 */ 345 control(pty, cp, n) 346 int pty; 347 char *cp; 348 int n; 349 { 350 struct winsize w; 351 352 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 353 return (0); 354 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 355 bcopy(cp+4, (char *)&w, sizeof(w)); 356 w.ws_row = ntohs(w.ws_row); 357 w.ws_col = ntohs(w.ws_col); 358 w.ws_xpixel = ntohs(w.ws_xpixel); 359 w.ws_ypixel = ntohs(w.ws_ypixel); 360 (void)ioctl(pty, TIOCSWINSZ, &w); 361 return (4+sizeof (w)); 362 } 363 364 /* 365 * rlogin "protocol" machine. 366 */ 367 protocol(f, p) 368 int f, p; 369 { 370 char pibuf[1024], fibuf[1024], *pbp, *fbp; 371 register pcc = 0, fcc = 0; 372 int cc, nfd, pmask, fmask; 373 char cntl; 374 375 /* 376 * Must ignore SIGTTOU, otherwise we'll stop 377 * when we try and set slave pty's window shape 378 * (our controlling tty is the master pty). 379 */ 380 (void) signal(SIGTTOU, SIG_IGN); 381 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 382 if (f > p) 383 nfd = f + 1; 384 else 385 nfd = p + 1; 386 fmask = 1 << f; 387 pmask = 1 << p; 388 for (;;) { 389 int ibits, obits, ebits; 390 391 ibits = 0; 392 obits = 0; 393 if (fcc) 394 obits |= pmask; 395 else 396 ibits |= fmask; 397 if (pcc >= 0) 398 if (pcc) 399 obits |= fmask; 400 else 401 ibits |= pmask; 402 ebits = pmask; 403 if (select(nfd, &ibits, obits ? &obits : (int *)NULL, 404 &ebits, 0) < 0) { 405 if (errno == EINTR) 406 continue; 407 fatalperror(f, "select"); 408 } 409 if (ibits == 0 && obits == 0 && ebits == 0) { 410 /* shouldn't happen... */ 411 sleep(5); 412 continue; 413 } 414 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 415 if (ebits & pmask) { 416 cc = read(p, &cntl, 1); 417 if (cc == 1 && pkcontrol(cntl)) { 418 cntl |= oobdata[0]; 419 send(f, &cntl, 1, MSG_OOB); 420 if (cntl & TIOCPKT_FLUSHWRITE) { 421 pcc = 0; 422 ibits &= ~pmask; 423 } 424 } 425 } 426 if (ibits & fmask) { 427 #ifdef KERBEROS 428 if (encrypt) 429 fcc = des_read(f, fibuf, sizeof(fibuf)); 430 else 431 #endif 432 fcc = read(f, fibuf, sizeof(fibuf)); 433 if (fcc < 0 && errno == EWOULDBLOCK) 434 fcc = 0; 435 else { 436 register char *cp; 437 int left, n; 438 439 if (fcc <= 0) 440 break; 441 fbp = fibuf; 442 443 top: 444 for (cp = fibuf; cp < fibuf+fcc-1; cp++) 445 if (cp[0] == magic[0] && 446 cp[1] == magic[1]) { 447 left = fcc - (cp-fibuf); 448 n = control(p, cp, left); 449 if (n) { 450 left -= n; 451 if (left > 0) 452 bcopy(cp+n, cp, left); 453 fcc -= n; 454 goto top; /* n^2 */ 455 } 456 } 457 obits |= pmask; /* try write */ 458 } 459 } 460 461 if ((obits & pmask) && fcc > 0) { 462 cc = write(p, fbp, fcc); 463 if (cc > 0) { 464 fcc -= cc; 465 fbp += cc; 466 } 467 } 468 469 if (ibits & pmask) { 470 pcc = read(p, pibuf, sizeof (pibuf)); 471 pbp = pibuf; 472 if (pcc < 0 && errno == EWOULDBLOCK) 473 pcc = 0; 474 else if (pcc <= 0) 475 break; 476 else if (pibuf[0] == 0) { 477 pbp++, pcc--; 478 #ifdef KERBEROS 479 if (!encrypt) 480 #endif 481 obits |= fmask; /* try a write */ 482 } else { 483 if (pkcontrol(pibuf[0])) { 484 pibuf[0] |= oobdata[0]; 485 send(f, &pibuf[0], 1, MSG_OOB); 486 } 487 pcc = 0; 488 } 489 } 490 if ((obits & fmask) && pcc > 0) { 491 #ifdef KERBEROS 492 if (encrypt) 493 cc = des_write(f, pbp, pcc); 494 else 495 #endif 496 cc = write(f, pbp, pcc); 497 if (cc < 0 && errno == EWOULDBLOCK) { 498 /* also shouldn't happen */ 499 sleep(5); 500 continue; 501 } 502 if (cc > 0) { 503 pcc -= cc; 504 pbp += cc; 505 } 506 } 507 } 508 } 509 510 cleanup() 511 { 512 char *p; 513 514 p = line + sizeof("/dev/") - 1; 515 if (logout(p)) 516 logwtmp(p, "", ""); 517 (void)chmod(line, 0666); 518 (void)chown(line, 0, 0); 519 *p = 'p'; 520 (void)chmod(line, 0666); 521 (void)chown(line, 0, 0); 522 shutdown(netf, 2); 523 exit(1); 524 } 525 526 fatal(f, msg) 527 int f; 528 char *msg; 529 { 530 char buf[BUFSIZ]; 531 532 buf[0] = '\01'; /* error indicator */ 533 (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 534 (void) write(f, buf, strlen(buf)); 535 exit(1); 536 } 537 538 fatalperror(f, msg) 539 int f; 540 char *msg; 541 { 542 char buf[BUFSIZ]; 543 extern int sys_nerr; 544 extern char *sys_errlist[]; 545 546 if ((unsigned)errno < sys_nerr) 547 (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 548 else 549 (void) sprintf(buf, "%s: Error %d", msg, errno); 550 fatal(f, buf); 551 } 552 553 do_rlogin(host) 554 char *host; 555 { 556 557 getstr(rusername, sizeof(rusername), "remuser too long"); 558 getstr(lusername, sizeof(lusername), "locuser too long"); 559 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long"); 560 561 if (getuid()) 562 return(-1); 563 pwd = getpwnam(lusername); 564 if (pwd == NULL) 565 return(-1); 566 return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); 567 } 568 569 570 getstr(buf, cnt, errmsg) 571 char *buf; 572 int cnt; 573 char *errmsg; 574 { 575 char c; 576 577 do { 578 if (read(0, &c, 1) != 1) 579 exit(1); 580 if (--cnt < 0) 581 fatal(1, errmsg); 582 *buf++ = c; 583 } while (c != 0); 584 } 585 586 extern char **environ; 587 588 setup_term(fd) 589 int fd; 590 { 591 struct termios tt; 592 register char *cp = index(term+ENVSIZE, '/'); 593 char *speed; 594 595 tcgetattr(fd, &tt); 596 if (cp) { 597 *cp++ = '\0'; 598 speed = cp; 599 cp = index(speed, '/'); 600 if (cp) 601 *cp++ = '\0'; 602 cfsetspeed(&tt, atoi(speed)); 603 } 604 tt.c_iflag = BRKINT|ICRNL|IXON|ISTRIP|IEXTEN|IMAXBEL; 605 tt.c_oflag = OPOST|ONLCR|OXTABS; 606 tt.c_lflag = ISIG|ICANON|ECHO; 607 tcsetattr(fd, TCSADFLUSH, &tt); 608 609 env[0] = term; 610 env[1] = 0; 611 environ = env; 612 } 613 614 #ifdef KERBEROS 615 #define VERSION_SIZE 9 616 617 /* 618 * Do the remote kerberos login to the named host with the 619 * given inet address 620 * 621 * Return 0 on valid authorization 622 * Return -1 on valid authentication, no authorization 623 * Return >0 for error conditions 624 */ 625 do_krb_login(host, dest, encrypt) 626 char *host; 627 struct sockaddr_in *dest; 628 int encrypt; 629 { 630 int rc; 631 char instance[INST_SZ], version[VERSION_SIZE]; 632 long authopts = 0L; /* !mutual */ 633 struct sockaddr_in faddr; 634 635 if (getuid()) 636 return(KFAILURE); 637 638 kdata = (AUTH_DAT *) auth_buf; 639 ticket = (KTEXT) tick_buf; 640 strcpy(instance, "*"); 641 642 if (encrypt) { 643 rc = sizeof(faddr); 644 if (getsockname(0, &faddr, &rc)) 645 return(-1); 646 authopts = KOPT_DO_MUTUAL; 647 rc = krb_recvauth( 648 authopts, 0, 649 ticket, "rcmd", 650 instance, dest, &faddr, 651 kdata, "", schedule, version); 652 des_set_key(kdata->session, schedule); 653 654 } else { 655 rc = krb_recvauth( 656 authopts, 0, 657 ticket, "rcmd", 658 instance, dest, (struct sockaddr_in *) 0, 659 kdata, "", (bit_64 *) 0, version); 660 } 661 662 if (rc != KSUCCESS) 663 return(rc); 664 665 if ((rc = krb_kntoln(kdata, rusername)) != KSUCCESS) 666 return(rc); 667 668 getstr(lusername, sizeof(lusername), "locuser"); 669 /* get the "cmd" in the rcmd protocol */ 670 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type"); 671 672 pwd = getpwnam(lusername); 673 if (pwd == NULL) 674 return(-1); 675 676 /* returns nonzero for no access */ 677 /* return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); */ 678 if (kuserok(kdata,lusername) != 0) 679 return(-1); 680 681 return(0); 682 683 } 684 685 #endif /* KERBEROS */ 686 687 usage() 688 { 689 #ifdef KERBEROS 690 syslog(LOG_ERR, "usage: rlogind [-k | -v] [-l] [-n]"); 691 #else 692 syslog(LOG_ERR, "usage: rlogind [-l] [-n]"); 693 #endif 694 } 695 696 /* 697 * Check whether host h is in our local domain, 698 * as determined by the part of the name following 699 * the first '.' in its name and in ours. 700 * If either name is unqualified (contains no '.'), 701 * assume that the host is local, as it will be 702 * interpreted as such. 703 */ 704 local_domain(h) 705 char *h; 706 { 707 char localhost[MAXHOSTNAMELEN]; 708 char *p1, *p2 = index(h, '.'); 709 710 (void) gethostname(localhost, sizeof(localhost)); 711 p1 = index(localhost, '.'); 712 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 713 return(1); 714 return(0); 715 } 716