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.22.1.5 (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 * Automatic login protocol is done here, using login -f upon success, 37 * unless OLD_LOGIN is defined (then done in login, ala 4.2/4.3BSD). 38 */ 39 40 #include <stdio.h> 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 #include <sys/socket.h> 44 #include <sys/wait.h> 45 #include <sys/file.h> 46 47 #include <netinet/in.h> 48 49 #include <errno.h> 50 #include <pwd.h> 51 #include <signal.h> 52 #include <sgtty.h> 53 #include <stdio.h> 54 #include <netdb.h> 55 #include <syslog.h> 56 #include <strings.h> 57 58 #ifndef TIOCPKT_WINDOW 59 #define TIOCPKT_WINDOW 0x80 60 #endif 61 62 char *env[2]; 63 #define NMAX 30 64 char lusername[NMAX+1], rusername[NMAX+1]; 65 static char term[64] = "TERM="; 66 #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */ 67 int keepalive = 1; 68 69 #define SUPERUSER(pwd) ((pwd)->pw_uid == 0) 70 71 extern int errno; 72 int reapchild(); 73 struct passwd *getpwnam(), *pwd; 74 char *malloc(); 75 76 main(argc, argv) 77 int argc; 78 char **argv; 79 { 80 extern int opterr, optind, _check_rhosts_file; 81 int ch; 82 int on = 1, fromlen; 83 struct sockaddr_in from; 84 85 openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH); 86 87 opterr = 0; 88 while ((ch = getopt(argc, argv, "ln")) != EOF) 89 switch (ch) { 90 case 'l': 91 _check_rhosts_file = 0; 92 break; 93 case 'n': 94 keepalive = 0; 95 break; 96 case '?': 97 default: 98 syslog(LOG_ERR, "usage: rlogind [-l] [-n]"); 99 break; 100 } 101 argc -= optind; 102 argv += optind; 103 104 fromlen = sizeof (from); 105 if (getpeername(0, &from, &fromlen) < 0) { 106 syslog(LOG_ERR, "Couldn't get peer name of remote host: %m"); 107 fatalperror("Can't get peer name of host"); 108 } 109 if (keepalive && 110 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) 111 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 112 doit(0, &from); 113 } 114 115 int child; 116 int cleanup(); 117 int netf; 118 char *line; 119 extern char *inet_ntoa(); 120 121 struct winsize win = { 0, 0, 0, 0 }; 122 123 124 doit(f, fromp) 125 int f; 126 struct sockaddr_in *fromp; 127 { 128 int i, p, t, pid, on = 1; 129 #ifndef OLD_LOGIN 130 int authenticated = 0, hostok = 0; 131 char remotehost[2 * MAXHOSTNAMELEN + 1]; 132 #endif 133 register struct hostent *hp; 134 struct hostent hostent; 135 char c; 136 137 alarm(60); 138 read(f, &c, 1); 139 if (c != 0) 140 exit(1); 141 142 alarm(0); 143 fromp->sin_port = ntohs((u_short)fromp->sin_port); 144 hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 145 fromp->sin_family); 146 if (hp == 0) { 147 /* 148 * Only the name is used below. 149 */ 150 hp = &hostent; 151 hp->h_name = inet_ntoa(fromp->sin_addr); 152 hostok++; 153 } 154 #ifndef OLD_LOGIN 155 else if (local_domain(hp->h_name)) { 156 /* 157 * If name returned by gethostbyaddr is in our domain, 158 * attempt to verify that we haven't been fooled by someone 159 * in a remote net; look up the name and check that this 160 * address corresponds to the name. 161 */ 162 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 163 remotehost[sizeof(remotehost) - 1] = 0; 164 hp = gethostbyname(remotehost); 165 if (hp) 166 for (; hp->h_addr_list[0]; hp->h_addr_list++) 167 if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr, 168 sizeof(fromp->sin_addr))) { 169 hostok++; 170 break; 171 } 172 } else 173 hostok++; 174 #endif 175 176 if (fromp->sin_family != AF_INET || 177 fromp->sin_port >= IPPORT_RESERVED || 178 fromp->sin_port < IPPORT_RESERVED/2) { 179 syslog(LOG_NOTICE, "Connection from %s on illegal port", 180 inet_ntoa(fromp->sin_addr)); 181 fatal(f, "Permission denied"); 182 } 183 write(f, "", 1); 184 #ifndef OLD_LOGIN 185 if (do_rlogin(hp->h_name) == 0) { 186 if (hostok) 187 authenticated++; 188 else 189 write(f, "rlogind: Host address mismatch.\r\n", 190 sizeof("rlogind: Host address mismatch.\r\n") - 1); 191 } 192 #endif 193 194 for (c = 'p'; c <= 's'; c++) { 195 struct stat stb; 196 line = "/dev/ptyXX"; 197 line[strlen("/dev/pty")] = c; 198 line[strlen("/dev/ptyp")] = '0'; 199 if (stat(line, &stb) < 0) 200 break; 201 for (i = 0; i < 16; i++) { 202 line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i]; 203 p = open(line, O_RDWR); 204 if (p > 0) 205 goto gotpty; 206 } 207 } 208 fatal(f, "Out of ptys"); 209 /*NOTREACHED*/ 210 gotpty: 211 (void) ioctl(p, TIOCSWINSZ, &win); 212 netf = f; 213 line[strlen("/dev/")] = 't'; 214 t = open(line, O_RDWR); 215 if (t < 0) 216 fatalperror(f, line); 217 if (fchmod(t, 0)) 218 fatalperror(f, line); 219 (void)signal(SIGHUP, SIG_IGN); 220 vhangup(); 221 (void)signal(SIGHUP, SIG_DFL); 222 t = open(line, O_RDWR); 223 if (t < 0) 224 fatalperror(f, line); 225 setup_term(t); 226 #ifdef DEBUG 227 { 228 int tt = open("/dev/tty", O_RDWR); 229 if (tt > 0) { 230 (void)ioctl(tt, TIOCNOTTY, 0); 231 (void)close(tt); 232 } 233 } 234 #endif 235 pid = fork(); 236 if (pid < 0) 237 fatalperror(f, ""); 238 if (pid == 0) { 239 close(f), close(p); 240 dup2(t, 0), dup2(t, 1), dup2(t, 2); 241 close(t); 242 #ifdef OLD_LOGIN 243 execl("/bin/login", "login", "-r", hp->h_name, 0); 244 #else /* OLD_LOGIN */ 245 if (authenticated) 246 execl("/bin/login", "login", "-p", "-h", hp->h_name, 247 "-f", lusername, 0); 248 else 249 execl("/bin/login", "login", "-p", "-h", hp->h_name, 250 lusername, 0); 251 #endif /* OLD_LOGIN */ 252 fatalperror(2, "/bin/login"); 253 /*NOTREACHED*/ 254 } 255 close(t); 256 257 ioctl(f, FIONBIO, &on); 258 ioctl(p, FIONBIO, &on); 259 ioctl(p, TIOCPKT, &on); 260 signal(SIGTSTP, SIG_IGN); 261 signal(SIGCHLD, cleanup); 262 setpgrp(0, 0); 263 protocol(f, p); 264 signal(SIGCHLD, SIG_IGN); 265 cleanup(); 266 } 267 268 char magic[2] = { 0377, 0377 }; 269 char oobdata[] = {TIOCPKT_WINDOW}; 270 271 /* 272 * Handle a "control" request (signaled by magic being present) 273 * in the data stream. For now, we are only willing to handle 274 * window size changes. 275 */ 276 control(pty, cp, n) 277 int pty; 278 char *cp; 279 int n; 280 { 281 struct winsize w; 282 283 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 284 return (0); 285 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 286 bcopy(cp+4, (char *)&w, sizeof(w)); 287 w.ws_row = ntohs(w.ws_row); 288 w.ws_col = ntohs(w.ws_col); 289 w.ws_xpixel = ntohs(w.ws_xpixel); 290 w.ws_ypixel = ntohs(w.ws_ypixel); 291 (void)ioctl(pty, TIOCSWINSZ, &w); 292 return (4+sizeof (w)); 293 } 294 295 /* 296 * rlogin "protocol" machine. 297 */ 298 protocol(f, p) 299 int f, p; 300 { 301 char pibuf[1024], fibuf[1024], *pbp, *fbp; 302 register pcc = 0, fcc = 0; 303 int cc, nfd, pmask, fmask; 304 char cntl; 305 306 /* 307 * Must ignore SIGTTOU, otherwise we'll stop 308 * when we try and set slave pty's window shape 309 * (our controlling tty is the master pty). 310 */ 311 (void) signal(SIGTTOU, SIG_IGN); 312 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 313 if (f > p) 314 nfd = f + 1; 315 else 316 nfd = p + 1; 317 fmask = 1 << f; 318 pmask = 1 << p; 319 for (;;) { 320 int ibits, obits, ebits; 321 322 ibits = 0; 323 obits = 0; 324 if (fcc) 325 obits |= pmask; 326 else 327 ibits |= fmask; 328 if (pcc >= 0) 329 if (pcc) 330 obits |= fmask; 331 else 332 ibits |= pmask; 333 ebits = pmask; 334 if (select(nfd, &ibits, obits ? &obits : (int *)NULL, 335 &ebits, 0) < 0) { 336 if (errno == EINTR) 337 continue; 338 fatalperror(f, "select"); 339 } 340 if (ibits == 0 && obits == 0 && ebits == 0) { 341 /* shouldn't happen... */ 342 sleep(5); 343 continue; 344 } 345 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 346 if (ebits & pmask) { 347 cc = read(p, &cntl, 1); 348 if (cc == 1 && pkcontrol(cntl)) { 349 cntl |= oobdata[0]; 350 send(f, &cntl, 1, MSG_OOB); 351 if (cntl & TIOCPKT_FLUSHWRITE) { 352 pcc = 0; 353 ibits &= ~pmask; 354 } 355 } 356 } 357 if (ibits & fmask) { 358 fcc = read(f, fibuf, sizeof(fibuf)); 359 if (fcc < 0 && errno == EWOULDBLOCK) 360 fcc = 0; 361 else { 362 register char *cp; 363 int left, n; 364 365 if (fcc <= 0) 366 break; 367 fbp = fibuf; 368 369 top: 370 for (cp = fibuf; cp < fibuf+fcc-1; cp++) 371 if (cp[0] == magic[0] && 372 cp[1] == magic[1]) { 373 left = fcc - (cp-fibuf); 374 n = control(p, cp, left); 375 if (n) { 376 left -= n; 377 if (left > 0) 378 bcopy(cp+n, cp, left); 379 fcc -= n; 380 goto top; /* n^2 */ 381 } 382 } 383 obits |= pmask; /* try write */ 384 } 385 } 386 387 if ((obits & pmask) && fcc > 0) { 388 cc = write(p, fbp, fcc); 389 if (cc > 0) { 390 fcc -= cc; 391 fbp += cc; 392 } 393 } 394 395 if (ibits & pmask) { 396 pcc = read(p, pibuf, sizeof (pibuf)); 397 pbp = pibuf; 398 if (pcc < 0 && errno == EWOULDBLOCK) 399 pcc = 0; 400 else if (pcc <= 0) 401 break; 402 else if (pibuf[0] == 0) { 403 pbp++, pcc--; 404 obits |= fmask; /* try a write */ 405 } else { 406 if (pkcontrol(pibuf[0])) { 407 pibuf[0] |= oobdata[0]; 408 send(f, &pibuf[0], 1, MSG_OOB); 409 } 410 pcc = 0; 411 } 412 } 413 if ((obits & fmask) && pcc > 0) { 414 cc = write(f, pbp, pcc); 415 if (cc < 0 && errno == EWOULDBLOCK) { 416 /* also shouldn't happen */ 417 sleep(5); 418 continue; 419 } 420 if (cc > 0) { 421 pcc -= cc; 422 pbp += cc; 423 } 424 } 425 } 426 } 427 428 cleanup() 429 { 430 char *p; 431 432 p = line + sizeof("/dev/") - 1; 433 if (logout(p)) 434 logwtmp(p, "", ""); 435 (void)chmod(line, 0666); 436 (void)chown(line, 0, 0); 437 *p = 'p'; 438 (void)chmod(line, 0666); 439 (void)chown(line, 0, 0); 440 shutdown(netf, 2); 441 exit(1); 442 } 443 444 fatal(f, msg) 445 int f; 446 char *msg; 447 { 448 char buf[BUFSIZ]; 449 450 buf[0] = '\01'; /* error indicator */ 451 (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 452 (void) write(f, buf, strlen(buf)); 453 exit(1); 454 } 455 456 fatalperror(f, msg) 457 int f; 458 char *msg; 459 { 460 char buf[BUFSIZ]; 461 extern int sys_nerr; 462 extern char *sys_errlist[]; 463 464 if ((unsigned)errno < sys_nerr) 465 (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 466 else 467 (void) sprintf(buf, "%s: Error %d", msg, errno); 468 fatal(f, buf); 469 } 470 471 #ifndef OLD_LOGIN 472 do_rlogin(host) 473 char *host; 474 { 475 476 getstr(rusername, sizeof(rusername), "remuser too long"); 477 getstr(lusername, sizeof(lusername), "locuser too long"); 478 getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long"); 479 480 if (getuid()) 481 return(-1); 482 pwd = getpwnam(lusername); 483 if (pwd == NULL) 484 return(-1); 485 return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); 486 } 487 488 489 getstr(buf, cnt, errmsg) 490 char *buf; 491 int cnt; 492 char *errmsg; 493 { 494 char c; 495 496 do { 497 if (read(0, &c, 1) != 1) 498 exit(1); 499 if (--cnt < 0) 500 fatal(1, errmsg); 501 *buf++ = c; 502 } while (c != 0); 503 } 504 505 extern char **environ; 506 507 char *speeds[] = { 508 "0", "50", "75", "110", "134", "150", "200", "300", "600", 509 "1200", "1800", "2400", "4800", "9600", "19200", "38400", 510 }; 511 #define NSPEEDS (sizeof(speeds) / sizeof(speeds[0])) 512 513 setup_term(fd) 514 int fd; 515 { 516 register char *cp = index(term, '/'), **cpp; 517 struct sgttyb sgttyb; 518 char *speed; 519 520 (void)ioctl(fd, TIOCGETP, &sgttyb); 521 if (cp) { 522 *cp++ = '\0'; 523 speed = cp; 524 cp = index(speed, '/'); 525 if (cp) 526 *cp++ = '\0'; 527 for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++) 528 if (strcmp(*cpp, speed) == 0) { 529 sgttyb.sg_ispeed = sgttyb.sg_ospeed = cpp - speeds; 530 break; 531 } 532 } 533 sgttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; 534 (void)ioctl(fd, TIOCSETP, &sgttyb); 535 536 env[0] = term; 537 env[1] = 0; 538 environ = env; 539 } 540 541 /* 542 * Check whether host h is in our local domain, 543 * as determined by the part of the name following 544 * the first '.' in its name and in ours. 545 * If either name is unqualified (contains no '.'), 546 * assume that the host is local, as it will be 547 * interpreted as such. 548 */ 549 local_domain(h) 550 char *h; 551 { 552 char localhost[MAXHOSTNAMELEN]; 553 char *p1, *p2 = index(h, '.'); 554 555 (void) gethostname(localhost, sizeof(localhost)); 556 p1 = index(localhost, '.'); 557 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 558 return(1); 559 return(0); 560 } 561 #endif /* OLD_LOGIN */ 562