1 /* $NetBSD: rcmd.c,v 1.16 1996/12/18 04:53:11 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993, 1994 5 * The Regents of the University of California. 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 the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 39 #else 40 static char *rcsid = "$NetBSD: rcmd.c,v 1.16 1996/12/18 04:53:11 thorpej Exp $"; 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include <sys/param.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #include <sys/poll.h> 48 49 #include <netinet/in.h> 50 #include <arpa/inet.h> 51 52 #include <signal.h> 53 #include <fcntl.h> 54 #include <netdb.h> 55 #include <unistd.h> 56 #include <pwd.h> 57 #include <errno.h> 58 #include <stdio.h> 59 #include <ctype.h> 60 #include <string.h> 61 #include <syslog.h> 62 63 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 64 static int __icheckhost __P((u_int32_t, const char *)); 65 static char *__gethostloop __P((u_int32_t)); 66 67 int 68 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 69 char **ahost; 70 u_short rport; 71 const char *locuser, *remuser, *cmd; 72 int *fd2p; 73 { 74 struct hostent *hp; 75 struct sockaddr_in sin, from; 76 struct pollfd reads[2]; 77 int oldmask; 78 pid_t pid; 79 int s, lport, timo; 80 char c; 81 82 pid = getpid(); 83 hp = gethostbyname(*ahost); 84 if (hp == NULL) { 85 herror(*ahost); 86 return (-1); 87 } 88 *ahost = hp->h_name; 89 oldmask = sigblock(sigmask(SIGURG)); 90 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 91 s = rresvport(&lport); 92 if (s < 0) { 93 if (errno == EAGAIN) 94 (void)fprintf(stderr, 95 "rcmd: socket: All ports in use\n"); 96 else 97 (void)fprintf(stderr, "rcmd: socket: %s\n", 98 strerror(errno)); 99 sigsetmask(oldmask); 100 return (-1); 101 } 102 fcntl(s, F_SETOWN, pid); 103 sin.sin_len = sizeof(struct sockaddr_in); 104 sin.sin_family = hp->h_addrtype; 105 sin.sin_port = rport; 106 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 107 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 108 break; 109 (void)close(s); 110 if (errno == EADDRINUSE) { 111 lport--; 112 continue; 113 } 114 if (errno == ECONNREFUSED && timo <= 16) { 115 (void)sleep(timo); 116 timo *= 2; 117 continue; 118 } 119 if (hp->h_addr_list[1] != NULL) { 120 int oerrno = errno; 121 122 (void)fprintf(stderr, "connect to address %s: ", 123 inet_ntoa(sin.sin_addr)); 124 errno = oerrno; 125 perror(0); 126 hp->h_addr_list++; 127 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 128 (void)fprintf(stderr, "Trying %s...\n", 129 inet_ntoa(sin.sin_addr)); 130 continue; 131 } 132 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); 133 sigsetmask(oldmask); 134 return (-1); 135 } 136 lport--; 137 if (fd2p == 0) { 138 write(s, "", 1); 139 lport = 0; 140 } else { 141 char num[8]; 142 int s2 = rresvport(&lport), s3; 143 int len = sizeof(from); 144 145 if (s2 < 0) 146 goto bad; 147 listen(s2, 1); 148 (void)snprintf(num, sizeof(num), "%d", lport); 149 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 150 (void)fprintf(stderr, 151 "rcmd: write (setting up stderr): %s\n", 152 strerror(errno)); 153 (void)close(s2); 154 goto bad; 155 } 156 reads[0].fd = s; 157 reads[1].fd = s2; 158 reads[0].events = reads[1].events = reads[0].revents = 159 reads[1].revents = POLLIN; 160 errno = 0; 161 if (poll(reads, 2, INFTIM) < 1 || 162 (reads[1].revents & POLLIN) == 0) { 163 if (errno != 0) 164 (void)fprintf(stderr, 165 "rcmd: poll (setting up stderr): %s\n", 166 strerror(errno)); 167 else 168 (void)fprintf(stderr, 169 "poll: protocol failure in circuit setup\n"); 170 (void)close(s2); 171 goto bad; 172 } 173 s3 = accept(s2, (struct sockaddr *)&from, &len); 174 (void)close(s2); 175 if (s3 < 0) { 176 (void)fprintf(stderr, 177 "rcmd: accept: %s\n", strerror(errno)); 178 lport = 0; 179 goto bad; 180 } 181 *fd2p = s3; 182 from.sin_port = ntohs(from.sin_port); 183 if (from.sin_family != AF_INET || 184 from.sin_port >= IPPORT_RESERVED || 185 from.sin_port < IPPORT_RESERVED / 2) { 186 (void)fprintf(stderr, 187 "socket: protocol failure in circuit setup.\n"); 188 goto bad2; 189 } 190 } 191 (void)write(s, locuser, strlen(locuser)+1); 192 (void)write(s, remuser, strlen(remuser)+1); 193 (void)write(s, cmd, strlen(cmd)+1); 194 if (read(s, &c, 1) != 1) { 195 (void)fprintf(stderr, 196 "rcmd: %s: %s\n", *ahost, strerror(errno)); 197 goto bad2; 198 } 199 if (c != 0) { 200 while (read(s, &c, 1) == 1) { 201 (void)write(STDERR_FILENO, &c, 1); 202 if (c == '\n') 203 break; 204 } 205 goto bad2; 206 } 207 sigsetmask(oldmask); 208 return (s); 209 bad2: 210 if (lport) 211 (void)close(*fd2p); 212 bad: 213 (void)close(s); 214 sigsetmask(oldmask); 215 return (-1); 216 } 217 218 int 219 rresvport(alport) 220 int *alport; 221 { 222 struct sockaddr_in sin; 223 int s; 224 225 sin.sin_len = sizeof(struct sockaddr_in); 226 sin.sin_family = AF_INET; 227 sin.sin_addr.s_addr = INADDR_ANY; 228 s = socket(AF_INET, SOCK_STREAM, 0); 229 if (s < 0) 230 return (-1); 231 for (;;) { 232 sin.sin_port = htons((u_short)*alport); 233 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 234 return (s); 235 if (errno != EADDRINUSE) { 236 (void)close(s); 237 return (-1); 238 } 239 (*alport)--; 240 if (*alport == IPPORT_RESERVED/2) { 241 (void)close(s); 242 errno = EAGAIN; /* close */ 243 return (-1); 244 } 245 } 246 } 247 248 int __check_rhosts_file = 1; 249 char *__rcmd_errstr; 250 251 int 252 ruserok(rhost, superuser, ruser, luser) 253 const char *rhost, *ruser, *luser; 254 int superuser; 255 { 256 struct hostent *hp; 257 char **ap; 258 int i; 259 #define MAXADDRS 35 260 u_int32_t addrs[MAXADDRS + 1]; 261 262 if ((hp = gethostbyname(rhost)) == NULL) 263 return (-1); 264 for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) 265 bcopy(*ap, &addrs[i], sizeof(addrs[i])); 266 addrs[i] = 0; 267 268 for (i = 0; i < MAXADDRS && addrs[i]; i++) 269 if (iruserok(addrs[i], superuser, ruser, luser) == 0) 270 return (0); 271 return (-1); 272 } 273 274 /* 275 * New .rhosts strategy: We are passed an ip address. We spin through 276 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 277 * has ip addresses, we don't have to trust a nameserver. When it 278 * contains hostnames, we spin through the list of addresses the nameserver 279 * gives us and look for a match. 280 * 281 * Returns 0 if ok, -1 if not ok. 282 */ 283 int 284 iruserok(raddr, superuser, ruser, luser) 285 u_int32_t raddr; 286 int superuser; 287 const char *ruser, *luser; 288 { 289 register char *cp; 290 struct stat sbuf; 291 struct passwd *pwd; 292 FILE *hostf; 293 uid_t uid; 294 int first; 295 char pbuf[MAXPATHLEN]; 296 297 first = 1; 298 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 299 again: 300 if (hostf) { 301 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { 302 (void)fclose(hostf); 303 return (0); 304 } 305 (void)fclose(hostf); 306 } 307 if (first == 1 && (__check_rhosts_file || superuser)) { 308 first = 0; 309 if ((pwd = getpwnam(luser)) == NULL) 310 return (-1); 311 (void)strcpy(pbuf, pwd->pw_dir); 312 (void)strcat(pbuf, "/.rhosts"); 313 314 /* 315 * Change effective uid while opening .rhosts. If root and 316 * reading an NFS mounted file system, can't read files that 317 * are protected read/write owner only. 318 */ 319 uid = geteuid(); 320 (void)seteuid(pwd->pw_uid); 321 hostf = fopen(pbuf, "r"); 322 (void)seteuid(uid); 323 324 if (hostf == NULL) 325 return (-1); 326 /* 327 * If not a regular file, or is owned by someone other than 328 * user or root or if writeable by anyone but the owner, quit. 329 */ 330 cp = NULL; 331 if (lstat(pbuf, &sbuf) < 0) 332 cp = ".rhosts lstat failed"; 333 else if (!S_ISREG(sbuf.st_mode)) 334 cp = ".rhosts not regular file"; 335 else if (fstat(fileno(hostf), &sbuf) < 0) 336 cp = ".rhosts fstat failed"; 337 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 338 cp = "bad .rhosts owner"; 339 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 340 cp = ".rhosts writeable by other than owner"; 341 /* If there were any problems, quit. */ 342 if (cp) { 343 __rcmd_errstr = cp; 344 (void)fclose(hostf); 345 return (-1); 346 } 347 goto again; 348 } 349 return (-1); 350 } 351 352 /* 353 * XXX 354 * Don't make static, used by lpd(8). 355 * 356 * Returns 0 if ok, -1 if not ok. 357 */ 358 int 359 __ivaliduser(hostf, raddr, luser, ruser) 360 FILE *hostf; 361 u_int32_t raddr; 362 const char *luser, *ruser; 363 { 364 register char *user, *p; 365 int ch; 366 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 367 const char *auser, *ahost; 368 int hostok, userok; 369 char *rhost = NULL; 370 int firsttime = 1; 371 char domain[MAXHOSTNAMELEN]; 372 373 getdomainname(domain, sizeof(domain)); 374 375 while (fgets(buf, sizeof(buf), hostf)) { 376 p = buf; 377 /* Skip lines that are too long. */ 378 if (strchr(p, '\n') == NULL) { 379 while ((ch = getc(hostf)) != '\n' && ch != EOF) 380 ; 381 continue; 382 } 383 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 384 *p = isupper(*p) ? tolower(*p) : *p; 385 p++; 386 } 387 if (*p == ' ' || *p == '\t') { 388 *p++ = '\0'; 389 while (*p == ' ' || *p == '\t') 390 p++; 391 user = p; 392 while (*p != '\n' && *p != ' ' && 393 *p != '\t' && *p != '\0') 394 p++; 395 } else 396 user = p; 397 *p = '\0'; 398 399 if (p == buf) 400 continue; 401 402 auser = *user ? user : luser; 403 ahost = buf; 404 405 if (ahost[0] == '+') 406 switch (ahost[1]) { 407 case '\0': 408 hostok = 1; 409 break; 410 411 case '@': 412 if (firsttime) { 413 rhost = __gethostloop(raddr); 414 firsttime = 0; 415 } 416 if (rhost) 417 hostok = innetgr(&ahost[2], rhost, 418 NULL, domain); 419 else 420 hostok = 0; 421 break; 422 423 default: 424 hostok = __icheckhost(raddr, &ahost[1]); 425 break; 426 } 427 else if (ahost[0] == '-') 428 switch (ahost[1]) { 429 case '\0': 430 hostok = -1; 431 break; 432 433 case '@': 434 if (firsttime) { 435 rhost = __gethostloop(raddr); 436 firsttime = 0; 437 } 438 if (rhost) 439 hostok = -innetgr(&ahost[2], rhost, 440 NULL, domain); 441 else 442 hostok = 0; 443 break; 444 445 default: 446 hostok = -__icheckhost(raddr, &ahost[1]); 447 break; 448 } 449 else 450 hostok = __icheckhost(raddr, ahost); 451 452 453 if (auser[0] == '+') 454 switch (auser[1]) { 455 case '\0': 456 userok = 1; 457 break; 458 459 case '@': 460 userok = innetgr(&auser[2], NULL, ruser, 461 domain); 462 break; 463 464 default: 465 userok = strcmp(ruser, &auser[1]) == 0; 466 break; 467 } 468 else if (auser[0] == '-') 469 switch (auser[1]) { 470 case '\0': 471 userok = -1; 472 break; 473 474 case '@': 475 userok = -innetgr(&auser[2], NULL, ruser, 476 domain); 477 break; 478 479 default: 480 userok = -(strcmp(ruser, &auser[1]) == 0); 481 break; 482 } 483 else 484 userok = strcmp(ruser, auser) == 0; 485 486 /* Check if one component did not match */ 487 if (hostok == 0 || userok == 0) 488 continue; 489 490 /* Check if we got a forbidden pair */ 491 if (userok == -1 || hostok == -1) 492 return -1; 493 494 /* Check if we got a valid pair */ 495 if (hostok == 1 && userok == 1) 496 return 0; 497 } 498 return -1; 499 } 500 501 /* 502 * Returns "true" if match, 0 if no match. 503 */ 504 static int 505 __icheckhost(raddr, lhost) 506 u_int32_t raddr; 507 const char *lhost; 508 { 509 register struct hostent *hp; 510 register u_int32_t laddr; 511 register char **pp; 512 513 /* Try for raw ip address first. */ 514 if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1) 515 return (raddr == laddr); 516 517 /* Better be a hostname. */ 518 if ((hp = gethostbyname(lhost)) == NULL) 519 return (0); 520 521 /* Spin through ip addresses. */ 522 for (pp = hp->h_addr_list; *pp; ++pp) 523 if (!bcmp(&raddr, *pp, sizeof(u_int32_t))) 524 return (1); 525 526 /* No match. */ 527 return (0); 528 } 529 530 /* 531 * Return the hostname associated with the supplied address. 532 * Do a reverse lookup as well for security. If a loop cannot 533 * be found, pack the result of inet_ntoa() into the string. 534 */ 535 static char * 536 __gethostloop(raddr) 537 u_int32_t raddr; 538 { 539 static char remotehost[MAXHOSTNAMELEN]; 540 struct hostent *hp; 541 struct in_addr in; 542 543 hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET); 544 if (hp == NULL) 545 return (NULL); 546 547 /* 548 * Look up the name and check that the supplied 549 * address is in the list 550 */ 551 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 552 remotehost[sizeof(remotehost) - 1] = '\0'; 553 hp = gethostbyname(remotehost); 554 if (hp == NULL) 555 return (NULL); 556 557 for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++) 558 if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr))) 559 return (remotehost); 560 561 /* 562 * either the DNS adminstrator has made a configuration 563 * mistake, or someone has attempted to spoof us 564 */ 565 in.s_addr = raddr; 566 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 567 inet_ntoa(in), hp->h_name); 568 return (NULL); 569 } 570