rcmd.c revision 52859
145392Sbrian/* 245392Sbrian * Copyright (c) 1983, 1993, 1994 345392Sbrian * The Regents of the University of California. All rights reserved. 445392Sbrian * 545392Sbrian * Redistribution and use in source and binary forms, with or without 645392Sbrian * modification, are permitted provided that the following conditions 745392Sbrian * are met: 845392Sbrian * 1. Redistributions of source code must retain the above copyright 945392Sbrian * notice, this list of conditions and the following disclaimer. 1045392Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1145392Sbrian * notice, this list of conditions and the following disclaimer in the 1245392Sbrian * documentation and/or other materials provided with the distribution. 1345392Sbrian * 3. All advertising materials mentioning features or use of this software 1445392Sbrian * must display the following acknowledgement: 1545392Sbrian * This product includes software developed by the University of 1645392Sbrian * California, Berkeley and its contributors. 1745392Sbrian * 4. Neither the name of the University nor the names of its contributors 1845392Sbrian * may be used to endorse or promote products derived from this software 1945392Sbrian * without specific prior written permission. 2045392Sbrian * 2145392Sbrian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2245392Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2345392Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2445392Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2545392Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2645392Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2784225Sdillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2884225Sdillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2984225Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3045392Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31116344Smarkm * SUCH DAMAGE. 3245392Sbrian * 3345392Sbrian * $FreeBSD: head/lib/libc/net/rcmd.c 52859 1999-11-04 04:35:33Z ache $ 3445392Sbrian */ 3545392Sbrian 3645392Sbrian#if defined(LIBC_SCCS) && !defined(lint) 3745392Sbrianstatic char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 3845392Sbrian#endif /* LIBC_SCCS and not lint */ 3945392Sbrian 4045392Sbrian#include <sys/param.h> 4145392Sbrian#include <sys/socket.h> 4256590Sshin#include <sys/stat.h> 4356590Sshin 4456590Sshin#include <netinet/in.h> 4556590Sshin#include <arpa/inet.h> 4656590Sshin 4756590Sshin#include <signal.h> 4845392Sbrian#include <fcntl.h> 4945392Sbrian#include <netdb.h> 5045392Sbrian#include <unistd.h> 5174260Sbrian#include <pwd.h> 5245392Sbrian#include <errno.h> 5345392Sbrian#include <stdio.h> 5445392Sbrian#include <ctype.h> 5545392Sbrian#include <string.h> 56121193Smarkm#ifdef YP 5745392Sbrian#include <rpc/rpc.h> 5871753Sbrian#include <rpcsvc/yp_prot.h> 5971753Sbrian#include <rpcsvc/ypclnt.h> 6071753Sbrian#endif 6171753Sbrian 6271753Sbrianextern int innetgr __P(( const char *, const char *, const char *, const char * )); 6345392Sbrian 64183989Sdelphij#define max(a, b) ((a > b) ? a : b) 6571753Sbrian 6671753Sbrianint __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); 6771753Sbrianstatic int __icheckhost __P((u_int32_t, char *)); 6871753Sbrian 6971753Sbrianint 7071753Sbrianrcmd(ahost, rport, locuser, remuser, cmd, fd2p) 7171753Sbrian char **ahost; 7271753Sbrian u_short rport; 7371753Sbrian const char *locuser, *remuser, *cmd; 7471753Sbrian int *fd2p; 7571753Sbrian{ 7671753Sbrian struct hostent *hp; 7745392Sbrian struct sockaddr_in sin, from; 7845392Sbrian fd_set reads; 7945392Sbrian long oldmask; 8045392Sbrian pid_t pid; 8145392Sbrian int s, lport, timo; 8245392Sbrian char c; 8345392Sbrian 8445392Sbrian pid = getpid(); 8556590Sshin hp = gethostbyname(*ahost); 86184683Sdes if (hp == NULL) { 87184683Sdes herror(*ahost); 88184683Sdes return (-1); 89184683Sdes } 90184683Sdes *ahost = hp->h_name; 91184683Sdes oldmask = sigblock(sigmask(SIGURG)); 92184683Sdes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 93184683Sdes s = rresvport(&lport); 94184683Sdes if (s < 0) { 95184683Sdes if (errno == EAGAIN) 96184683Sdes (void)fprintf(stderr, 97184683Sdes "rcmd: socket: All ports in use\n"); 9856590Sshin else 9956590Sshin (void)fprintf(stderr, "rcmd: socket: %s\n", 10056590Sshin strerror(errno)); 10156590Sshin sigsetmask(oldmask); 10257789Sume return (-1); 103185277Savatar } 104121193Smarkm fcntl(s, F_SETOWN, pid); 105185277Savatar bzero(&sin, sizeof sin); 10656590Sshin sin.sin_len = sizeof(struct sockaddr_in); 10756590Sshin sin.sin_family = hp->h_addrtype; 10856590Sshin sin.sin_port = rport; 10980223Sume bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr)); 11080223Sume if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 11180223Sume break; 11280223Sume (void)close(s); 113184683Sdes if (errno == EADDRINUSE) { 11480223Sume lport--; 11580223Sume continue; 116184683Sdes } 11780223Sume if (errno == ECONNREFUSED && timo <= 16) { 118121193Smarkm (void)sleep(timo); 119121193Smarkm timo *= 2; 120121193Smarkm continue; 121121193Smarkm } 122121193Smarkm if (hp->h_addr_list[1] != NULL) { 12380223Sume int oerrno = errno; 124121193Smarkm 125121193Smarkm (void)fprintf(stderr, "connect to address %s: ", 12680223Sume inet_ntoa(sin.sin_addr)); 12780223Sume errno = oerrno; 12880223Sume perror(0); 12980223Sume hp->h_addr_list++; 130146187Sume bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr)); 13157789Sume (void)fprintf(stderr, "Trying %s...\n", 13256590Sshin inet_ntoa(sin.sin_addr)); 13356590Sshin continue; 13456590Sshin } 13556590Sshin (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); 13680223Sume sigsetmask(oldmask); 13780223Sume return (-1); 13880223Sume } 13956590Sshin lport--; 14057789Sume if (fd2p == 0) { 14156590Sshin write(s, "", 1); 14256590Sshin lport = 0; 14356590Sshin } else { 14480223Sume char num[8]; 14580223Sume int s2 = rresvport(&lport), s3; 14656590Sshin int len = sizeof(from); 14756590Sshin int nfds; 14856590Sshin 14956590Sshin if (s2 < 0) 15056590Sshin goto bad; 15156590Sshin listen(s2, 1); 15256590Sshin (void)snprintf(num, sizeof(num), "%d", lport); 15356590Sshin if (write(s, num, strlen(num)+1) != strlen(num)+1) { 15456590Sshin (void)fprintf(stderr, 15556590Sshin "rcmd: write (setting up stderr): %s\n", 15656590Sshin strerror(errno)); 15756590Sshin (void)close(s2); 15856590Sshin goto bad; 159184683Sdes } 16080223Sume nfds = max(s, s2)+1; 16180223Sume if(nfds > FD_SETSIZE) { 16280223Sume fprintf(stderr, "rcmd: too many files\n"); 16380223Sume (void)close(s2); 16480223Sume goto bad; 16580223Sume } 166184683Sdesagain: 167184683Sdes FD_ZERO(&reads); 168184683Sdes FD_SET(s, &reads); 16980223Sume FD_SET(s2, &reads); 17056590Sshin errno = 0; 17156590Sshin if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 17280223Sume if (errno != 0) 17357789Sume (void)fprintf(stderr, 17457789Sume "rcmd: select (setting up stderr): %s\n", 17557789Sume strerror(errno)); 17674260Sbrian else 17771753Sbrian (void)fprintf(stderr, 17871753Sbrian "select: protocol failure in circuit setup\n"); 17974260Sbrian (void)close(s2); 18071753Sbrian goto bad; 18171753Sbrian } 18271753Sbrian s3 = accept(s2, (struct sockaddr *)&from, &len); 18371753Sbrian /* 18474260Sbrian * XXX careful for ftp bounce attacks. If discovered, shut them 18556590Sshin * down and check for the real auxiliary channel to connect. 18656590Sshin */ 18756590Sshin if (from.sin_family == AF_INET && from.sin_port == htons(20)) { 18856590Sshin close(s3); 18956590Sshin goto again; 19056590Sshin } 19156590Sshin (void)close(s2); 19257789Sume if (s3 < 0) { 193146187Sume (void)fprintf(stderr, 19457789Sume "rcmd: accept: %s\n", strerror(errno)); 19556590Sshin lport = 0; 19656590Sshin goto bad; 19756590Sshin } 19856590Sshin *fd2p = s3; 19956590Sshin from.sin_port = ntohs((u_short)from.sin_port); 20056590Sshin if (from.sin_family != AF_INET || 201 from.sin_port >= IPPORT_RESERVED || 202 from.sin_port < IPPORT_RESERVED / 2) { 203 (void)fprintf(stderr, 204 "socket: protocol failure in circuit setup.\n"); 205 goto bad2; 206 } 207 } 208 (void)write(s, locuser, strlen(locuser)+1); 209 (void)write(s, remuser, strlen(remuser)+1); 210 (void)write(s, cmd, strlen(cmd)+1); 211 if (read(s, &c, 1) != 1) { 212 (void)fprintf(stderr, 213 "rcmd: %s: %s\n", *ahost, strerror(errno)); 214 goto bad2; 215 } 216 if (c != 0) { 217 while (read(s, &c, 1) == 1) { 218 (void)write(STDERR_FILENO, &c, 1); 219 if (c == '\n') 220 break; 221 } 222 goto bad2; 223 } 224 sigsetmask(oldmask); 225 return (s); 226bad2: 227 if (lport) 228 (void)close(*fd2p); 229bad: 230 (void)close(s); 231 sigsetmask(oldmask); 232 return (-1); 233} 234 235int 236rresvport(alport) 237 int *alport; 238{ 239 struct sockaddr_in sin; 240 int s; 241 242 bzero(&sin, sizeof sin); 243 sin.sin_len = sizeof(struct sockaddr_in); 244 sin.sin_family = AF_INET; 245 sin.sin_addr.s_addr = INADDR_ANY; 246 s = socket(AF_INET, SOCK_STREAM, 0); 247 if (s < 0) 248 return (-1); 249#if 0 /* compat_exact_traditional_rresvport_semantics */ 250 sin.sin_port = htons((u_short)*alport); 251 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 252 return (s); 253 if (errno != EADDRINUSE) { 254 (void)close(s); 255 return (-1); 256 } 257#endif 258 sin.sin_port = 0; 259 if (bindresvport(s, &sin) == -1) { 260 (void)close(s); 261 return (-1); 262 } 263 *alport = (int)ntohs(sin.sin_port); 264 return (s); 265} 266 267int __check_rhosts_file = 1; 268char *__rcmd_errstr; 269 270int 271ruserok(rhost, superuser, ruser, luser) 272 const char *rhost, *ruser, *luser; 273 int superuser; 274{ 275 struct hostent *hp; 276 u_int32_t addr; 277 char **ap; 278 279 if ((hp = gethostbyname(rhost)) == NULL) 280 return (-1); 281 for (ap = hp->h_addr_list; *ap; ++ap) { 282 bcopy(*ap, &addr, sizeof(addr)); 283 if (iruserok(addr, superuser, ruser, luser) == 0) 284 return (0); 285 } 286 return (-1); 287} 288 289/* 290 * New .rhosts strategy: We are passed an ip address. We spin through 291 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 292 * has ip addresses, we don't have to trust a nameserver. When it 293 * contains hostnames, we spin through the list of addresses the nameserver 294 * gives us and look for a match. 295 * 296 * Returns 0 if ok, -1 if not ok. 297 */ 298int 299iruserok(raddr, superuser, ruser, luser) 300 unsigned long raddr; 301 int superuser; 302 const char *ruser, *luser; 303{ 304 register char *cp; 305 struct stat sbuf; 306 struct passwd *pwd; 307 FILE *hostf; 308 uid_t uid; 309 int first; 310 char pbuf[MAXPATHLEN]; 311 312 first = 1; 313 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 314again: 315 if (hostf) { 316 if (__ivaliduser(hostf, (u_int32_t)raddr, luser, ruser) == 0) { 317 (void)fclose(hostf); 318 return (0); 319 } 320 (void)fclose(hostf); 321 } 322 if (first == 1 && (__check_rhosts_file || superuser)) { 323 first = 0; 324 if ((pwd = getpwnam(luser)) == NULL) 325 return (-1); 326 (void)strcpy(pbuf, pwd->pw_dir); 327 (void)strcat(pbuf, "/.rhosts"); 328 329 /* 330 * Change effective uid while opening .rhosts. If root and 331 * reading an NFS mounted file system, can't read files that 332 * are protected read/write owner only. 333 */ 334 uid = geteuid(); 335 (void)seteuid(pwd->pw_uid); 336 hostf = fopen(pbuf, "r"); 337 (void)seteuid(uid); 338 339 if (hostf == NULL) 340 return (-1); 341 /* 342 * If not a regular file, or is owned by someone other than 343 * user or root or if writeable by anyone but the owner, quit. 344 */ 345 cp = NULL; 346 if (lstat(pbuf, &sbuf) < 0) 347 cp = ".rhosts lstat failed"; 348 else if (!S_ISREG(sbuf.st_mode)) 349 cp = ".rhosts not regular file"; 350 else if (fstat(fileno(hostf), &sbuf) < 0) 351 cp = ".rhosts fstat failed"; 352 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 353 cp = "bad .rhosts owner"; 354 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 355 cp = ".rhosts writeable by other than owner"; 356 /* If there were any problems, quit. */ 357 if (cp) { 358 __rcmd_errstr = cp; 359 (void)fclose(hostf); 360 return (-1); 361 } 362 goto again; 363 } 364 return (-1); 365} 366 367/* 368 * XXX 369 * Don't make static, used by lpd(8). 370 * 371 * Returns 0 if ok, -1 if not ok. 372 */ 373int 374__ivaliduser(hostf, raddr, luser, ruser) 375 FILE *hostf; 376 u_int32_t raddr; 377 const char *luser, *ruser; 378{ 379 register char *user, *p; 380 int ch; 381 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 382 char hname[MAXHOSTNAMELEN]; 383 struct hostent *hp; 384 /* Presumed guilty until proven innocent. */ 385 int userok = 0, hostok = 0; 386#ifdef YP 387 char *ypdomain; 388 389 if (yp_get_default_domain(&ypdomain)) 390 ypdomain = NULL; 391#else 392#define ypdomain NULL 393#endif 394 /* We need to get the damn hostname back for netgroup matching. */ 395 if ((hp = gethostbyaddr((char *)&raddr, sizeof(u_int32_t), 396 AF_INET)) == NULL) 397 return (-1); 398 strncpy(hname, hp->h_name, sizeof(hname)); 399 hname[sizeof(hname) - 1] = '\0'; 400 401 while (fgets(buf, sizeof(buf), hostf)) { 402 p = buf; 403 /* Skip lines that are too long. */ 404 if (strchr(p, '\n') == NULL) { 405 while ((ch = getc(hostf)) != '\n' && ch != EOF); 406 continue; 407 } 408 if (*p == '\n' || *p == '#') { 409 /* comment... */ 410 continue; 411 } 412 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 413 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 414 p++; 415 } 416 if (*p == ' ' || *p == '\t') { 417 *p++ = '\0'; 418 while (*p == ' ' || *p == '\t') 419 p++; 420 user = p; 421 while (*p != '\n' && *p != ' ' && 422 *p != '\t' && *p != '\0') 423 p++; 424 } else 425 user = p; 426 *p = '\0'; 427 /* 428 * Do +/- and +@/-@ checking. This looks really nasty, 429 * but it matches SunOS's behavior so far as I can tell. 430 */ 431 switch(buf[0]) { 432 case '+': 433 if (!buf[1]) { /* '+' matches all hosts */ 434 hostok = 1; 435 break; 436 } 437 if (buf[1] == '@') /* match a host by netgroup */ 438 hostok = innetgr((char *)&buf[2], 439 (char *)&hname, NULL, ypdomain); 440 else /* match a host by addr */ 441 hostok = __icheckhost(raddr,(char *)&buf[1]); 442 break; 443 case '-': /* reject '-' hosts and all their users */ 444 if (buf[1] == '@') { 445 if (innetgr((char *)&buf[2], 446 (char *)&hname, NULL, ypdomain)) 447 return(-1); 448 } else { 449 if (__icheckhost(raddr,(char *)&buf[1])) 450 return(-1); 451 } 452 break; 453 default: /* if no '+' or '-', do a simple match */ 454 hostok = __icheckhost(raddr, buf); 455 break; 456 } 457 switch(*user) { 458 case '+': 459 if (!*(user+1)) { /* '+' matches all users */ 460 userok = 1; 461 break; 462 } 463 if (*(user+1) == '@') /* match a user by netgroup */ 464 userok = innetgr(user+2, NULL, ruser, ypdomain); 465 else /* match a user by direct specification */ 466 userok = !(strcmp(ruser, user+1)); 467 break; 468 case '-': /* if we matched a hostname, */ 469 if (hostok) { /* check for user field rejections */ 470 if (!*(user+1)) 471 return(-1); 472 if (*(user+1) == '@') { 473 if (innetgr(user+2, NULL, 474 ruser, ypdomain)) 475 return(-1); 476 } else { 477 if (!strcmp(ruser, user+1)) 478 return(-1); 479 } 480 } 481 break; 482 default: /* no rejections: try to match the user */ 483 if (hostok) 484 userok = !(strcmp(ruser,*user ? user : luser)); 485 break; 486 } 487 if (hostok && userok) 488 return(0); 489 } 490 return (-1); 491} 492 493/* 494 * Returns "true" if match, 0 if no match. 495 */ 496static int 497__icheckhost(raddr, lhost) 498 u_int32_t raddr; 499 register char *lhost; 500{ 501 register struct hostent *hp; 502 register u_int32_t laddr; 503 register char **pp; 504 505 /* Try for raw ip address first. */ 506 if (isdigit(*lhost) && (u_int32_t)(laddr = inet_addr(lhost)) != -1) 507 return (raddr == laddr); 508 509 /* Better be a hostname. */ 510 if ((hp = gethostbyname(lhost)) == NULL) 511 return (0); 512 513 /* Spin through ip addresses. */ 514 for (pp = hp->h_addr_list; *pp; ++pp) 515 if (!bcmp(&raddr, *pp, sizeof(u_int32_t))) 516 return (1); 517 518 /* No match. */ 519 return (0); 520} 521