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