1 /* 2 * Copyright (c) 1983, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/param.h> 39 #include <sys/socket.h> 40 #include <sys/stat.h> 41 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 44 45 #include <signal.h> 46 #include <fcntl.h> 47 #include <netdb.h> 48 #include <unistd.h> 49 #include <pwd.h> 50 #include <errno.h> 51 #include <stdio.h> 52 #include <ctype.h> 53 #include <string.h> 54 55 #define max(a, b) ((a > b) ? a : b) 56 57 int __ivaliduser __P((FILE *, u_long, const char *, const char *)); 58 static int __icheckhost __P((u_long, char *)); 59 60 int 61 rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 62 char **ahost; 63 u_short rport; 64 const char *locuser, *remuser, *cmd; 65 int *fd2p; 66 { 67 struct hostent *hp; 68 struct sockaddr_in sin, from; 69 fd_set reads; 70 long oldmask; 71 pid_t pid; 72 int s, lport, timo; 73 char c; 74 75 pid = getpid(); 76 hp = gethostbyname(*ahost); 77 if (hp == NULL) { 78 herror(*ahost); 79 return (-1); 80 } 81 *ahost = hp->h_name; 82 oldmask = sigblock(sigmask(SIGURG)); 83 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 84 s = rresvport(&lport); 85 if (s < 0) { 86 if (errno == EAGAIN) 87 (void)fprintf(stderr, 88 "rcmd: socket: All ports in use\n"); 89 else 90 (void)fprintf(stderr, "rcmd: socket: %s\n", 91 strerror(errno)); 92 sigsetmask(oldmask); 93 return (-1); 94 } 95 fcntl(s, F_SETOWN, pid); 96 sin.sin_family = hp->h_addrtype; 97 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 98 sin.sin_port = rport; 99 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 100 break; 101 (void)close(s); 102 if (errno == EADDRINUSE) { 103 lport--; 104 continue; 105 } 106 if (errno == ECONNREFUSED && timo <= 16) { 107 (void)sleep(timo); 108 timo *= 2; 109 continue; 110 } 111 if (hp->h_addr_list[1] != NULL) { 112 int oerrno = errno; 113 114 (void)fprintf(stderr, "connect to address %s: ", 115 inet_ntoa(sin.sin_addr)); 116 errno = oerrno; 117 perror(0); 118 hp->h_addr_list++; 119 bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); 120 (void)fprintf(stderr, "Trying %s...\n", 121 inet_ntoa(sin.sin_addr)); 122 continue; 123 } 124 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); 125 sigsetmask(oldmask); 126 return (-1); 127 } 128 lport--; 129 if (fd2p == 0) { 130 write(s, "", 1); 131 lport = 0; 132 } else { 133 char num[8]; 134 int s2 = rresvport(&lport), s3; 135 int len = sizeof(from); 136 int nfds; 137 138 if (s2 < 0) 139 goto bad; 140 listen(s2, 1); 141 (void)snprintf(num, sizeof(num), "%d", lport); 142 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 143 (void)fprintf(stderr, 144 "rcmd: write (setting up stderr): %s\n", 145 strerror(errno)); 146 (void)close(s2); 147 goto bad; 148 } 149 nfds = max(s, s2)+1; 150 if(nfds > FD_SETSIZE) { 151 fprintf(stderr, "rcmd: too many files\n"); 152 (void)close(s2); 153 goto bad; 154 } 155 FD_ZERO(&reads); 156 FD_SET(s, &reads); 157 FD_SET(s2, &reads); 158 errno = 0; 159 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 160 if (errno != 0) 161 (void)fprintf(stderr, 162 "rcmd: select (setting up stderr): %s\n", 163 strerror(errno)); 164 else 165 (void)fprintf(stderr, 166 "select: protocol failure in circuit setup\n"); 167 (void)close(s2); 168 goto bad; 169 } 170 s3 = accept(s2, (struct sockaddr *)&from, &len); 171 (void)close(s2); 172 if (s3 < 0) { 173 (void)fprintf(stderr, 174 "rcmd: accept: %s\n", strerror(errno)); 175 lport = 0; 176 goto bad; 177 } 178 *fd2p = s3; 179 from.sin_port = ntohs((u_short)from.sin_port); 180 if (from.sin_family != AF_INET || 181 from.sin_port >= IPPORT_RESERVED || 182 from.sin_port < IPPORT_RESERVED / 2) { 183 (void)fprintf(stderr, 184 "socket: protocol failure in circuit setup.\n"); 185 goto bad2; 186 } 187 } 188 (void)write(s, locuser, strlen(locuser)+1); 189 (void)write(s, remuser, strlen(remuser)+1); 190 (void)write(s, cmd, strlen(cmd)+1); 191 if (read(s, &c, 1) != 1) { 192 (void)fprintf(stderr, 193 "rcmd: %s: %s\n", *ahost, strerror(errno)); 194 goto bad2; 195 } 196 if (c != 0) { 197 while (read(s, &c, 1) == 1) { 198 (void)write(STDERR_FILENO, &c, 1); 199 if (c == '\n') 200 break; 201 } 202 goto bad2; 203 } 204 sigsetmask(oldmask); 205 return (s); 206 bad2: 207 if (lport) 208 (void)close(*fd2p); 209 bad: 210 (void)close(s); 211 sigsetmask(oldmask); 212 return (-1); 213 } 214 215 int 216 rresvport(alport) 217 int *alport; 218 { 219 struct sockaddr_in sin; 220 int s; 221 222 sin.sin_family = AF_INET; 223 sin.sin_addr.s_addr = INADDR_ANY; 224 s = socket(AF_INET, SOCK_STREAM, 0); 225 if (s < 0) 226 return (-1); 227 for (;;) { 228 sin.sin_port = htons((u_short)*alport); 229 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 230 return (s); 231 if (errno != EADDRINUSE) { 232 (void)close(s); 233 return (-1); 234 } 235 (*alport)--; 236 if (*alport == IPPORT_RESERVED/2) { 237 (void)close(s); 238 errno = EAGAIN; /* close */ 239 return (-1); 240 } 241 } 242 } 243 244 int __check_rhosts_file = 1; 245 char *__rcmd_errstr; 246 247 int 248 ruserok(rhost, superuser, ruser, luser) 249 const char *rhost, *ruser, *luser; 250 int superuser; 251 { 252 struct hostent *hp; 253 u_long addr; 254 char **ap; 255 256 if ((hp = gethostbyname(rhost)) == NULL) 257 return (-1); 258 for (ap = hp->h_addr_list; *ap; ++ap) { 259 bcopy(*ap, &addr, sizeof(addr)); 260 if (iruserok(addr, superuser, ruser, luser) == 0) 261 return (0); 262 } 263 return (-1); 264 } 265 266 /* 267 * New .rhosts strategy: We are passed an ip address. We spin through 268 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 269 * has ip addresses, we don't have to trust a nameserver. When it 270 * contains hostnames, we spin through the list of addresses the nameserver 271 * gives us and look for a match. 272 * 273 * Returns 0 if ok, -1 if not ok. 274 */ 275 int 276 iruserok(raddr, superuser, ruser, luser) 277 u_long raddr; 278 int superuser; 279 const char *ruser, *luser; 280 { 281 register char *cp; 282 struct stat sbuf; 283 struct passwd *pwd; 284 FILE *hostf; 285 uid_t uid; 286 int first; 287 char pbuf[MAXPATHLEN]; 288 289 first = 1; 290 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 291 again: 292 if (hostf) { 293 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { 294 (void)fclose(hostf); 295 return (0); 296 } 297 (void)fclose(hostf); 298 } 299 if (first == 1 && (__check_rhosts_file || superuser)) { 300 first = 0; 301 if ((pwd = getpwnam(luser)) == NULL) 302 return (-1); 303 (void)strcpy(pbuf, pwd->pw_dir); 304 (void)strcat(pbuf, "/.rhosts"); 305 306 /* 307 * Change effective uid while opening .rhosts. If root and 308 * reading an NFS mounted file system, can't read files that 309 * are protected read/write owner only. 310 */ 311 uid = geteuid(); 312 (void)seteuid(pwd->pw_uid); 313 hostf = fopen(pbuf, "r"); 314 (void)seteuid(uid); 315 316 if (hostf == NULL) 317 return (-1); 318 /* 319 * If not a regular file, or is owned by someone other than 320 * user or root or if writeable by anyone but the owner, quit. 321 */ 322 cp = NULL; 323 if (lstat(pbuf, &sbuf) < 0) 324 cp = ".rhosts lstat failed"; 325 else if (!S_ISREG(sbuf.st_mode)) 326 cp = ".rhosts not regular file"; 327 else if (fstat(fileno(hostf), &sbuf) < 0) 328 cp = ".rhosts fstat failed"; 329 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 330 cp = "bad .rhosts owner"; 331 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 332 cp = ".rhosts writeable by other than owner"; 333 /* If there were any problems, quit. */ 334 if (cp) { 335 __rcmd_errstr = cp; 336 (void)fclose(hostf); 337 return (-1); 338 } 339 goto again; 340 } 341 return (-1); 342 } 343 344 /* 345 * XXX 346 * Don't make static, used by lpd(8). 347 * 348 * Returns 0 if ok, -1 if not ok. 349 */ 350 int 351 __ivaliduser(hostf, raddr, luser, ruser) 352 FILE *hostf; 353 u_long raddr; 354 const char *luser, *ruser; 355 { 356 register char *user, *p; 357 int ch; 358 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 359 struct hostent *hp; 360 /* Presumed guilty until proven innocent. */ 361 int userok = 0, hostok = 0; 362 363 /* We need to get the damn hostname back for netgroup matching. */ 364 if ((hp = gethostbyaddr((char *)&raddr, sizeof(u_long), 365 AF_INET)) == NULL) 366 return (-1); 367 368 while (fgets(buf, sizeof(buf), hostf)) { 369 p = buf; 370 /* Skip lines that are too long. */ 371 if (strchr(p, '\n') == NULL) { 372 while ((ch = getc(hostf)) != '\n' && ch != EOF); 373 continue; 374 } 375 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 376 *p = isupper(*p) ? tolower(*p) : *p; 377 p++; 378 } 379 if (*p == ' ' || *p == '\t') { 380 *p++ = '\0'; 381 while (*p == ' ' || *p == '\t') 382 p++; 383 user = p; 384 while (*p != '\n' && *p != ' ' && 385 *p != '\t' && *p != '\0') 386 p++; 387 } else 388 user = p; 389 *p = '\0'; 390 /* 391 * Do +/- and +@/-@ checking. This looks really nasty, 392 * but it matches SunOS's behavior so far as I can tell. 393 */ 394 switch(buf[0]) { 395 case '+': 396 if (!buf[1]) { /* '+' matches all hosts */ 397 hostok = 1; 398 break; 399 } 400 if (buf[1] == '@') /* match a host by netgroup */ 401 hostok = innetgr((char *)&buf[2], hp->h_name, 402 NULL, NULL); 403 else /* match a host by addr */ 404 hostok = __icheckhost(raddr,(char *)&buf[1]); 405 break; 406 case '-': /* reject '-' hosts and all their users */ 407 if (buf[1] == '@') { 408 if (innetgr((char *)&buf[2], 409 hp->h_name, NULL, NULL)) 410 return(-1); 411 } else { 412 if (__icheckhost(raddr,(char *)&buf[1])) 413 return(-1); 414 } 415 break; 416 default: /* if no '+' or '-', do a simple match */ 417 hostok = __icheckhost(raddr, buf); 418 break; 419 } 420 switch(*user) { 421 case '+': 422 if (!*(user+1)) { /* '+' matches all users */ 423 userok = 1; 424 break; 425 } 426 if (*(user+1) == '@') /* match a user by netgroup */ 427 userok = innetgr(user+2, NULL, ruser, NULL); 428 else /* match a user by direct specification */ 429 userok = !(strcmp(ruser, user+1)); 430 break; 431 case '-': /* if we matched a hostname, */ 432 if (hostok) { /* check for user field rejections */ 433 if (!*(user+1)) 434 return(-1); 435 if (*(user+1) == '@') { 436 if (innetgr(user+2, NULL, 437 ruser, NULL)) 438 return(-1); 439 } else { 440 if (!strcmp(ruser, user+1)) 441 return(-1); 442 } 443 } 444 break; 445 default: /* no rejections: try to match the user */ 446 if (hostok) 447 userok = !(strcmp(ruser,*user ? user : luser)); 448 break; 449 } 450 if (hostok && userok) 451 return(0); 452 } 453 return (-1); 454 } 455 456 /* 457 * Returns "true" if match, 0 if no match. 458 */ 459 static int 460 __icheckhost(raddr, lhost) 461 u_long raddr; 462 register char *lhost; 463 { 464 register struct hostent *hp; 465 register u_long laddr; 466 register char **pp; 467 468 /* Try for raw ip address first. */ 469 if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1) 470 return (raddr == laddr); 471 472 /* Better be a hostname. */ 473 if ((hp = gethostbyname(lhost)) == NULL) 474 return (0); 475 476 /* Spin through ip addresses. */ 477 for (pp = hp->h_addr_list; *pp; ++pp) 478 if (!bcmp(&raddr, *pp, sizeof(u_long))) 479 return (1); 480 481 /* No match. */ 482 return (0); 483 } 484