1 /* $NetBSD: rshd.c,v 1.24 2001/09/24 13:22:31 wiz Exp $ */ 2 3 /* 4 * Copyright (C) 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by WIDE Project and 18 * its contributors. 19 * 4. Neither the name of the project nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /*- 37 * Copyright (c) 1988, 1989, 1992, 1993, 1994 38 * The Regents of the University of California. All rights reserved. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the University of 51 * California, Berkeley and its contributors. 52 * 4. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 */ 68 69 #include <sys/cdefs.h> 70 #ifndef lint 71 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\ 72 The Regents of the University of California. All rights reserved.\n"); 73 #if 0 74 static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94"; 75 #else 76 __RCSID("$NetBSD: rshd.c,v 1.24 2001/09/24 13:22:31 wiz Exp $"); 77 #endif 78 #endif /* not lint */ 79 80 /* 81 * remote shell server: 82 * [port]\0 83 * remuser\0 84 * locuser\0 85 * command\0 86 * data 87 */ 88 #include <sys/param.h> 89 #include <sys/ioctl.h> 90 #include <sys/time.h> 91 #include <sys/socket.h> 92 93 #include <netinet/in.h> 94 #include <arpa/inet.h> 95 #include <netdb.h> 96 97 #include <errno.h> 98 #include <fcntl.h> 99 #include <paths.h> 100 #include <pwd.h> 101 #include <signal.h> 102 #include <stdio.h> 103 #include <stdlib.h> 104 #include <string.h> 105 #include <syslog.h> 106 #include <unistd.h> 107 #ifdef LOGIN_CAP 108 #include <login_cap.h> 109 #endif 110 111 int keepalive = 1; 112 int check_all; 113 int log_success; /* If TRUE, log all successful accesses */ 114 int sent_null; 115 116 void doit __P((struct sockaddr *)); 117 void error __P((const char *, ...)) 118 __attribute__((__format__(__printf__, 1, 2))); 119 void getstr __P((char *, int, char *)); 120 int local_domain __P((char *)); 121 char *topdomain __P((char *)); 122 void usage __P((void)); 123 int main __P((int, char *[])); 124 125 #define OPTIONS "alnL" 126 extern int __check_rhosts_file; 127 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */ 128 129 int 130 main(argc, argv) 131 int argc; 132 char *argv[]; 133 { 134 struct linger linger; 135 int ch, on = 1, fromlen; 136 struct sockaddr_storage from; 137 138 openlog("rshd", LOG_PID, LOG_DAEMON); 139 140 opterr = 0; 141 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 142 switch (ch) { 143 case 'a': 144 check_all = 1; 145 break; 146 case 'l': 147 __check_rhosts_file = 0; 148 break; 149 case 'n': 150 keepalive = 0; 151 break; 152 case 'L': 153 log_success = 1; 154 break; 155 case '?': 156 default: 157 usage(); 158 break; 159 } 160 161 argc -= optind; 162 argv += optind; 163 164 fromlen = sizeof (from); /* xxx */ 165 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { 166 syslog(LOG_ERR, "getpeername: %m"); 167 _exit(1); 168 } 169 #if 0 170 if (((struct sockaddr *)&from)->sa_family == AF_INET6 && 171 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) && 172 sizeof(struct sockaddr_in) <= sizeof(from)) { 173 struct sockaddr_in sin; 174 struct sockaddr_in6 *sin6; 175 const int off = sizeof(struct sockaddr_in6) - 176 sizeof(struct sockaddr_in); 177 178 sin6 = (struct sockaddr_in6 *)&from; 179 memset(&sin, 0, sizeof(sin)); 180 sin.sin_family = AF_INET; 181 sin.sin_len = sizeof(struct sockaddr_in); 182 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off], 183 sizeof(sin.sin_addr)); 184 memcpy(&from, &sin, sizeof(sin)); 185 fromlen = sin.sin_len; 186 } 187 #else 188 if (((struct sockaddr *)&from)->sa_family == AF_INET6 && 189 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) { 190 char hbuf[NI_MAXHOST]; 191 if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf, 192 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) { 193 strncpy(hbuf, "invalid", sizeof(hbuf)); 194 } 195 syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n", 196 hbuf); 197 exit(1); 198 } 199 #endif 200 if (keepalive && 201 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 202 sizeof(on)) < 0) 203 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 204 linger.l_onoff = 1; 205 linger.l_linger = 60; /* XXX */ 206 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger, 207 sizeof (linger)) < 0) 208 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m"); 209 doit((struct sockaddr *)&from); 210 /* NOTREACHED */ 211 #ifdef __GNUC__ 212 exit(0); 213 #endif 214 } 215 216 char username[20] = "USER="; 217 char homedir[64] = "HOME="; 218 char shell[64] = "SHELL="; 219 char path[100] = "PATH="; 220 char *envinit[] = 221 {homedir, shell, path, username, 0}; 222 char **environ; 223 224 void 225 doit(fromp) 226 struct sockaddr *fromp; 227 { 228 struct passwd *pwd; 229 in_port_t port; 230 fd_set ready, readfrom; 231 int cc, nfd, pv[2], pid, s = -1; /* XXX gcc */ 232 int one = 1; 233 char *hostname, *errorstr, *errorhost = NULL; /* XXX gcc */ 234 const char *cp; 235 char sig, buf[BUFSIZ]; 236 char cmdbuf[NCARGS+1], locuser[16], remuser[16]; 237 char remotehost[2 * MAXHOSTNAMELEN + 1]; 238 char hostnamebuf[2 * MAXHOSTNAMELEN + 1]; 239 #ifdef LOGIN_CAP 240 login_cap_t *lc; 241 #endif 242 char naddr[NI_MAXHOST]; 243 char saddr[NI_MAXHOST]; 244 char raddr[NI_MAXHOST]; 245 char pbuf[NI_MAXSERV]; 246 int af = fromp->sa_family; 247 u_int16_t *portp; 248 struct addrinfo hints, *res, *res0; 249 int gaierror; 250 #ifdef NI_WITHSCOPEID 251 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID; 252 #else 253 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; 254 #endif 255 256 (void) signal(SIGINT, SIG_DFL); 257 (void) signal(SIGQUIT, SIG_DFL); 258 (void) signal(SIGTERM, SIG_DFL); 259 #ifdef DEBUG 260 { int t = open(_PATH_TTY, 2); 261 if (t >= 0) { 262 ioctl(t, TIOCNOTTY, (char *)0); 263 (void) close(t); 264 } 265 } 266 #endif 267 switch (af) { 268 case AF_INET: 269 portp = &((struct sockaddr_in *)fromp)->sin_port; 270 break; 271 #ifdef INET6 272 case AF_INET6: 273 portp = &((struct sockaddr_in6 *)fromp)->sin6_port; 274 break; 275 #endif 276 default: 277 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af); 278 exit(1); 279 } 280 if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr), 281 pbuf, sizeof(pbuf), niflags) != 0) { 282 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af); 283 exit(1); 284 } 285 #ifdef IP_OPTIONS 286 if (af == AF_INET) 287 { 288 u_char optbuf[BUFSIZ/3], *cp; 289 char lbuf[BUFSIZ], *lp; 290 int optsize = sizeof(optbuf), ipproto; 291 struct protoent *ip; 292 293 if ((ip = getprotobyname("ip")) != NULL) 294 ipproto = ip->p_proto; 295 else 296 ipproto = IPPROTO_IP; 297 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) && 298 optsize != 0) { 299 lp = lbuf; 300 for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3) 301 sprintf(lp, " %2.2x", *cp); 302 syslog(LOG_NOTICE, 303 "Connection received from %s using IP options (ignored):%s", 304 naddr, lbuf); 305 if (setsockopt(0, ipproto, IP_OPTIONS, 306 (char *)NULL, optsize) != 0) { 307 syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m"); 308 exit(1); 309 } 310 } 311 } 312 #endif 313 314 if (ntohs(*portp) >= IPPORT_RESERVED 315 || ntohs(*portp) < IPPORT_RESERVED/2) { 316 syslog(LOG_NOTICE|LOG_AUTH, 317 "Connection from %s on illegal port %u", 318 naddr, ntohs(*portp)); 319 exit(1); 320 } 321 322 (void) alarm(60); 323 port = 0; 324 for (;;) { 325 char c; 326 327 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) { 328 if (cc < 0) 329 syslog(LOG_ERR, "read: %m"); 330 shutdown(0, SHUT_RDWR); 331 exit(1); 332 } 333 if (c == 0) 334 break; 335 port = port * 10 + c - '0'; 336 } 337 338 (void) alarm(0); 339 if (port != 0) { 340 int lport = IPPORT_RESERVED - 1; 341 s = rresvport_af(&lport, af); 342 if (s < 0) { 343 syslog(LOG_ERR, "can't get stderr port: %m"); 344 exit(1); 345 } 346 if (port >= IPPORT_RESERVED) { 347 syslog(LOG_ERR, "2nd port not reserved\n"); 348 exit(1); 349 } 350 *portp = htons(port); 351 if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) { 352 syslog(LOG_ERR, "connect second port %d: %m", port); 353 exit(1); 354 } 355 } 356 357 358 #ifdef notdef 359 /* from inetd, socket is already on 0, 1, 2 */ 360 dup2(f, 0); 361 dup2(f, 1); 362 dup2(f, 2); 363 #endif 364 errorstr = NULL; 365 if (getnameinfo(fromp, fromp->sa_len, saddr, sizeof(saddr), 366 NULL, 0, NI_NAMEREQD) == 0) { 367 /* 368 * If name returned by getnameinfo is in our domain, 369 * attempt to verify that we haven't been fooled by someone 370 * in a remote net; look up the name and check that this 371 * address corresponds to the name. 372 */ 373 hostname = saddr; 374 res0 = NULL; 375 if (check_all || local_domain(saddr)) { 376 strncpy(remotehost, saddr, sizeof(remotehost) - 1); 377 remotehost[sizeof(remotehost) - 1] = 0; 378 errorhost = remotehost; 379 memset(&hints, 0, sizeof(hints)); 380 hints.ai_family = fromp->sa_family; 381 hints.ai_socktype = SOCK_STREAM; 382 hints.ai_flags = AI_CANONNAME; 383 gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0); 384 if (gaierror) { 385 syslog(LOG_NOTICE, 386 "Couldn't look up address for %s: %s", 387 remotehost, gai_strerror(gaierror)); 388 errorstr = 389 "Couldn't look up address for your host (%s)\n"; 390 hostname = naddr; 391 } else { 392 for (res = res0; res; res = res->ai_next) { 393 if (res->ai_family != fromp->sa_family) 394 continue; 395 if (res->ai_addrlen != fromp->sa_len) 396 continue; 397 if (getnameinfo(res->ai_addr, 398 res->ai_addrlen, 399 raddr, sizeof(raddr), NULL, 0, 400 niflags) == 0 401 && strcmp(naddr, raddr) == 0) { 402 hostname = res->ai_canonname 403 ? res->ai_canonname 404 : saddr; 405 break; 406 } 407 } 408 if (res == NULL) { 409 syslog(LOG_NOTICE, 410 "Host addr %s not listed for host %s", 411 naddr, res0->ai_canonname 412 ? res0->ai_canonname 413 : saddr); 414 errorstr = 415 "Host address mismatch for %s\n"; 416 hostname = naddr; 417 } 418 } 419 } 420 hostname = strncpy(hostnamebuf, hostname, 421 sizeof(hostnamebuf) - 1); 422 if (res0) 423 freeaddrinfo(res0); 424 } else { 425 errorhost = hostname = strncpy(hostnamebuf, 426 naddr, sizeof(hostnamebuf) - 1); 427 } 428 429 hostnamebuf[sizeof(hostnamebuf) - 1] = '\0'; 430 431 getstr(remuser, sizeof(remuser), "remuser"); 432 getstr(locuser, sizeof(locuser), "locuser"); 433 getstr(cmdbuf, sizeof(cmdbuf), "command"); 434 setpwent(); 435 pwd = getpwnam(locuser); 436 if (pwd == NULL) { 437 syslog(LOG_INFO|LOG_AUTH, 438 "%s@%s as %s: unknown login. cmd='%.80s'", 439 remuser, hostname, locuser, cmdbuf); 440 if (errorstr == NULL) 441 errorstr = "Login incorrect.\n"; 442 goto fail; 443 } 444 #ifdef LOGIN_CAP 445 lc = login_getclass(pwd ? pwd->pw_class : NULL); 446 #endif 447 448 if (chdir(pwd->pw_dir) < 0) { 449 #ifdef LOGIN_CAP 450 if (chdir("/") < 0 || 451 login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0)) { 452 syslog(LOG_INFO|LOG_AUTH, 453 "%s@%s as %s: no home directory. cmd='%.80s'", 454 remuser, hostname, locuser, cmdbuf); 455 error("No remote home directory.\n"); 456 exit(0); 457 } 458 #else 459 (void) chdir("/"); 460 #ifdef notdef 461 syslog(LOG_INFO|LOG_AUTH, 462 "%s@%s as %s: no home directory. cmd='%.80s'", 463 remuser, hostname, locuser, cmdbuf); 464 error("No remote directory.\n"); 465 exit(1); 466 #endif /* notdef */ 467 #endif /* LOGIN_CAP */ 468 } 469 470 471 if (errorstr || 472 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' && 473 iruserok_sa(fromp, fromp->sa_len, pwd->pw_uid == 0, remuser, 474 locuser) < 0)) { 475 if (__rcmd_errstr) 476 syslog(LOG_INFO|LOG_AUTH, 477 "%s@%s as %s: permission denied (%s). cmd='%.80s'", 478 remuser, hostname, locuser, __rcmd_errstr, 479 cmdbuf); 480 else 481 syslog(LOG_INFO|LOG_AUTH, 482 "%s@%s as %s: permission denied. cmd='%.80s'", 483 remuser, hostname, locuser, cmdbuf); 484 fail: 485 if (errorstr == NULL) 486 errorstr = "Permission denied.\n"; 487 error(errorstr, errorhost); 488 exit(1); 489 } 490 491 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) { 492 error("Logins currently disabled.\n"); 493 exit(1); 494 } 495 496 (void) write(STDERR_FILENO, "\0", 1); 497 sent_null = 1; 498 499 if (port) { 500 if (pipe(pv) < 0) { 501 error("Can't make pipe.\n"); 502 exit(1); 503 } 504 pid = fork(); 505 if (pid == -1) { 506 error("Can't fork; try again.\n"); 507 exit(1); 508 } 509 if (pid) { 510 { 511 (void) close(0); 512 (void) close(1); 513 } 514 (void) close(2); 515 (void) close(pv[1]); 516 517 FD_ZERO(&readfrom); 518 FD_SET(s, &readfrom); 519 FD_SET(pv[0], &readfrom); 520 if (pv[0] > s) 521 nfd = pv[0]; 522 else 523 nfd = s; 524 ioctl(pv[0], FIONBIO, (char *)&one); 525 526 /* should set s nbio! */ 527 nfd++; 528 do { 529 ready = readfrom; 530 if (select(nfd, &ready, (fd_set *)0, 531 (fd_set *)0, (struct timeval *)0) < 0) 532 break; 533 if (FD_ISSET(s, &ready)) { 534 int ret; 535 536 ret = read(s, &sig, 1); 537 if (ret <= 0) 538 FD_CLR(s, &readfrom); 539 else 540 killpg(pid, sig); 541 } 542 if (FD_ISSET(pv[0], &ready)) { 543 errno = 0; 544 cc = read(pv[0], buf, sizeof(buf)); 545 if (cc <= 0) { 546 shutdown(s, SHUT_RDWR); 547 FD_CLR(pv[0], &readfrom); 548 } else { 549 (void) write(s, buf, cc); 550 } 551 } 552 553 } while (FD_ISSET(s, &readfrom) || 554 FD_ISSET(pv[0], &readfrom)); 555 exit(0); 556 } 557 setpgrp(0, getpid()); 558 (void) close(s); 559 (void) close(pv[0]); 560 dup2(pv[1], 2); 561 close(pv[1]); 562 } 563 #if BSD > 43 564 if (setlogin(pwd->pw_name) < 0) 565 syslog(LOG_ERR, "setlogin() failed: %m"); 566 #endif 567 568 if (*pwd->pw_shell == '\0') 569 pwd->pw_shell = _PATH_BSHELL; 570 #ifdef LOGIN_CAP 571 { 572 char *sh; 573 574 if((sh = login_getcapstr(lc, "shell", NULL, NULL))) { 575 if(!(sh = strdup(sh))) { 576 syslog(LOG_ERR, "Cannot alloc mem"); 577 exit(1); 578 } 579 pwd->pw_shell = sh; 580 } 581 } 582 #endif 583 environ = envinit; 584 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 585 strcat(path, _PATH_DEFPATH); 586 strncat(shell, pwd->pw_shell, sizeof(shell)-7); 587 strncat(username, pwd->pw_name, sizeof(username)-6); 588 #ifdef LOGIN_CAP 589 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) { 590 syslog(LOG_ERR, "setusercontext: %m"); 591 exit(1); 592 } 593 login_close(lc); 594 #else 595 (void) setgid((gid_t)pwd->pw_gid); 596 initgroups(pwd->pw_name, pwd->pw_gid); 597 (void) setuid((uid_t)pwd->pw_uid); 598 #endif 599 600 endpwent(); 601 if (log_success || pwd->pw_uid == 0) { 602 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'", 603 remuser, hostname, locuser, cmdbuf); 604 } 605 cp = strrchr(pwd->pw_shell, '/'); 606 if (cp) 607 cp++; 608 else 609 cp = pwd->pw_shell; 610 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0); 611 perror(pwd->pw_shell); 612 exit(1); 613 } 614 615 /* 616 * Report error to client. Note: can't be used until second socket has 617 * connected to client, or older clients will hang waiting for that 618 * connection first. 619 */ 620 #if __STDC__ 621 #include <stdarg.h> 622 #else 623 #include <varargs.h> 624 #endif 625 626 void 627 #if __STDC__ 628 error(const char *fmt, ...) 629 #else 630 error(fmt, va_alist) 631 char *fmt; 632 va_dcl 633 #endif 634 { 635 va_list ap; 636 int len; 637 char *bp, buf[BUFSIZ]; 638 #if __STDC__ 639 va_start(ap, fmt); 640 #else 641 va_start(ap); 642 #endif 643 bp = buf; 644 if (sent_null == 0) { 645 *bp++ = 1; 646 len = 1; 647 } else 648 len = 0; 649 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap); 650 (void)write(STDERR_FILENO, buf, len + strlen(bp)); 651 va_end(ap); 652 } 653 654 void 655 getstr(buf, cnt, err) 656 char *buf, *err; 657 int cnt; 658 { 659 char c; 660 661 do { 662 if (read(STDIN_FILENO, &c, 1) != 1) 663 exit(1); 664 *buf++ = c; 665 if (--cnt == 0) { 666 error("%s too long\n", err); 667 exit(1); 668 } 669 } while (c != 0); 670 } 671 672 /* 673 * Check whether host h is in our local domain, 674 * defined as sharing the last two components of the domain part, 675 * or the entire domain part if the local domain has only one component. 676 * If either name is unqualified (contains no '.'), 677 * assume that the host is local, as it will be 678 * interpreted as such. 679 */ 680 int 681 local_domain(h) 682 char *h; 683 { 684 char localhost[MAXHOSTNAMELEN + 1]; 685 char *p1, *p2; 686 687 localhost[0] = 0; 688 (void)gethostname(localhost, sizeof(localhost)); 689 localhost[sizeof(localhost) - 1] = '\0'; 690 p1 = topdomain(localhost); 691 p2 = topdomain(h); 692 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 693 return (1); 694 return (0); 695 } 696 697 char * 698 topdomain(h) 699 char *h; 700 { 701 char *p, *maybe = NULL; 702 int dots = 0; 703 704 for (p = h + strlen(h); p >= h; p--) { 705 if (*p == '.') { 706 if (++dots == 2) 707 return (p); 708 maybe = p; 709 } 710 } 711 return (maybe); 712 } 713 714 void 715 usage() 716 { 717 718 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS); 719 exit(2); 720 } 721