1 /* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft 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.12 1995/06/03 22:33:34 mycroft 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 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 50 51 #include <signal.h> 52 #include <fcntl.h> 53 #include <netdb.h> 54 #include <unistd.h> 55 #include <pwd.h> 56 #include <errno.h> 57 #include <stdio.h> 58 #include <ctype.h> 59 #include <string.h> 60 #include <syslog.h> 61 62 int __ivaliduser __P((FILE *, u_long, const char *, const char *)); 63 static int __icheckhost __P((u_int32_t, const char *)); 64 static char *__gethostloop __P((u_int32_t)); 65 66 int 67 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 68 char **ahost; 69 u_short rport; 70 const char *locuser, *remuser, *cmd; 71 int *fd2p; 72 { 73 struct hostent *hp; 74 struct sockaddr_in sin, from; 75 fd_set reads; 76 int oldmask; 77 pid_t pid; 78 int s, lport, timo; 79 char c; 80 81 pid = getpid(); 82 hp = gethostbyname(*ahost); 83 if (hp == NULL) { 84 herror(*ahost); 85 return (-1); 86 } 87 *ahost = hp->h_name; 88 oldmask = sigblock(sigmask(SIGURG)); 89 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 90 s = rresvport(&lport); 91 if (s < 0) { 92 if (errno == EAGAIN) 93 (void)fprintf(stderr, 94 "rcmd: socket: All ports in use\n"); 95 else 96 (void)fprintf(stderr, "rcmd: socket: %s\n", 97 strerror(errno)); 98 sigsetmask(oldmask); 99 return (-1); 100 } 101 fcntl(s, F_SETOWN, pid); 102 bzero(&sin, sizeof sin); 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 FD_ZERO(&reads); 157 FD_SET(s, &reads); 158 FD_SET(s2, &reads); 159 errno = 0; 160 if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || 161 !FD_ISSET(s2, &reads)) { 162 if (errno != 0) 163 (void)fprintf(stderr, 164 "rcmd: select (setting up stderr): %s\n", 165 strerror(errno)); 166 else 167 (void)fprintf(stderr, 168 "select: protocol failure in circuit setup\n"); 169 (void)close(s2); 170 goto bad; 171 } 172 s3 = accept(s2, (struct sockaddr *)&from, &len); 173 (void)close(s2); 174 if (s3 < 0) { 175 (void)fprintf(stderr, 176 "rcmd: accept: %s\n", strerror(errno)); 177 lport = 0; 178 goto bad; 179 } 180 *fd2p = s3; 181 from.sin_port = ntohs(from.sin_port); 182 if (from.sin_family != AF_INET || 183 from.sin_port >= IPPORT_RESERVED || 184 from.sin_port < IPPORT_RESERVED / 2) { 185 (void)fprintf(stderr, 186 "socket: protocol failure in circuit setup.\n"); 187 goto bad2; 188 } 189 } 190 (void)write(s, locuser, strlen(locuser)+1); 191 (void)write(s, remuser, strlen(remuser)+1); 192 (void)write(s, cmd, strlen(cmd)+1); 193 if (read(s, &c, 1) != 1) { 194 (void)fprintf(stderr, 195 "rcmd: %s: %s\n", *ahost, strerror(errno)); 196 goto bad2; 197 } 198 if (c != 0) { 199 while (read(s, &c, 1) == 1) { 200 (void)write(STDERR_FILENO, &c, 1); 201 if (c == '\n') 202 break; 203 } 204 goto bad2; 205 } 206 sigsetmask(oldmask); 207 return (s); 208 bad2: 209 if (lport) 210 (void)close(*fd2p); 211 bad: 212 (void)close(s); 213 sigsetmask(oldmask); 214 return (-1); 215 } 216 217 int 218 rresvport(alport) 219 int *alport; 220 { 221 struct sockaddr_in sin; 222 int s; 223 224 bzero(&sin, sizeof sin); 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((u_long)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, raddrl, luser, ruser) 360 FILE *hostf; 361 u_long raddrl; 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 = (char *)-1; 370 char domain[MAXHOSTNAMELEN]; 371 u_int32_t raddr = (u_int32_t)raddrl; 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 if (*p == '#') 384 continue; 385 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 386 *p = isupper(*p) ? tolower(*p) : *p; 387 p++; 388 } 389 if (*p == ' ' || *p == '\t') { 390 *p++ = '\0'; 391 while (*p == ' ' || *p == '\t') 392 p++; 393 user = p; 394 while (*p != '\n' && *p != ' ' && 395 *p != '\t' && *p != '\0') 396 p++; 397 } else 398 user = p; 399 *p = '\0'; 400 401 if (p == buf) 402 continue; 403 404 auser = *user ? user : luser; 405 ahost = buf; 406 407 /* 408 * innetgr() must lookup a hostname (we do not attempt 409 * to change the semantics so that netgroups may have 410 * #.#.#.# addresses in the list.) 411 */ 412 if (ahost[0] == '+') 413 switch (ahost[1]) { 414 case '\0': 415 hostok = 1; 416 break; 417 case '@': 418 if (rhost == (char *)-1) 419 rhost = __gethostloop(raddr); 420 hostok = 0; 421 if (rhost) 422 hostok = innetgr(&ahost[2], rhost, 423 NULL, domain); 424 break; 425 default: 426 hostok = __icheckhost(raddr, &ahost[1]); 427 break; 428 } 429 else if (ahost[0] == '-') 430 switch (ahost[1]) { 431 case '\0': 432 hostok = -1; 433 break; 434 case '@': 435 if (rhost == (char *)-1) 436 rhost = __gethostloop(raddr); 437 hostok = 0; 438 if (rhost) 439 hostok = -innetgr(&ahost[2], rhost, 440 NULL, domain); 441 break; 442 default: 443 hostok = -__icheckhost(raddr, &ahost[1]); 444 break; 445 } 446 else 447 hostok = __icheckhost(raddr, ahost); 448 449 450 if (auser[0] == '+') 451 switch (auser[1]) { 452 case '\0': 453 userok = 1; 454 break; 455 case '@': 456 userok = innetgr(&auser[2], NULL, ruser, 457 domain); 458 break; 459 default: 460 userok = strcmp(ruser, &auser[1]) ? 0 : 1; 461 break; 462 } 463 else if (auser[0] == '-') 464 switch (auser[1]) { 465 case '\0': 466 userok = -1; 467 break; 468 case '@': 469 userok = -innetgr(&auser[2], NULL, ruser, 470 domain); 471 break; 472 default: 473 userok = strcmp(ruser, &auser[1]) ? 0 : -1; 474 break; 475 } 476 else 477 userok = strcmp(ruser, auser) ? 0 : 1; 478 479 /* Check if one component did not match */ 480 if (hostok == 0 || userok == 0) 481 continue; 482 483 /* Check if we got a forbidden pair */ 484 if (userok <= -1 || hostok <= -1) 485 return (-1); 486 487 /* Check if we got a valid pair */ 488 if (hostok >= 1 && userok >= 1) 489 return (0); 490 } 491 return (-1); 492 } 493 494 /* 495 * Returns "true" if match, 0 if no match. If we do not find any 496 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. 497 */ 498 static int 499 __icheckhost(raddr, lhost) 500 u_int32_t raddr; 501 const char *lhost; 502 { 503 register struct hostent *hp; 504 register char **pp; 505 struct in_addr in; 506 507 hp = gethostbyname(lhost); 508 if (hp != NULL) { 509 /* Spin through ip addresses. */ 510 for (pp = hp->h_addr_list; *pp; ++pp) 511 if (!bcmp(&raddr, *pp, sizeof(raddr))) 512 return (1); 513 } 514 515 in.s_addr = raddr; 516 if (strcmp(lhost, inet_ntoa(in)) == 0) 517 return (1); 518 return (0); 519 } 520 521 /* 522 * Return the hostname associated with the supplied address. 523 * Do a reverse lookup as well for security. If a loop cannot 524 * be found, pack the result of inet_ntoa() into the string. 525 */ 526 static char * 527 __gethostloop(raddr) 528 u_int32_t raddr; 529 { 530 static char remotehost[MAXHOSTNAMELEN]; 531 struct hostent *hp; 532 struct in_addr in; 533 534 hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET); 535 if (hp == NULL) 536 return (NULL); 537 538 /* 539 * Look up the name and check that the supplied 540 * address is in the list 541 */ 542 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 543 remotehost[sizeof(remotehost) - 1] = '\0'; 544 hp = gethostbyname(remotehost); 545 if (hp == NULL) 546 return (NULL); 547 548 for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++) 549 if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr))) 550 return (remotehost); 551 552 /* 553 * either the DNS adminstrator has made a configuration 554 * mistake, or someone has attempted to spoof us 555 */ 556 in.s_addr = raddr; 557 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", 558 inet_ntoa(in), hp->h_name); 559 return (NULL); 560 } 561