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) 35static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 36#endif /* LIBC_SCCS and not lint */ 37#include <sys/cdefs.h>
| 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) 35static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 36#endif /* LIBC_SCCS and not lint */ 37#include <sys/cdefs.h>
|
38__FBSDID("$FreeBSD: head/lib/libc/net/rcmd.c 141920 2005-02-14 17:51:45Z stefanf $");
| 38__FBSDID("$FreeBSD: head/lib/libc/net/rcmd.c 146187 2005-05-13 16:31:11Z ume $");
|
39 40#include "namespace.h" 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/stat.h> 44 45#include <netinet/in.h> 46#include <arpa/inet.h> 47 48#include <signal.h> 49#include <fcntl.h> 50#include <netdb.h> 51#include <stdlib.h> 52#include <unistd.h> 53#include <pwd.h> 54#include <errno.h> 55#include <stdio.h> 56#include <ctype.h> 57#include <string.h> 58#include <rpc/rpc.h> 59#ifdef YP 60#include <rpcsvc/yp_prot.h> 61#include <rpcsvc/ypclnt.h> 62#endif 63#include <arpa/nameser.h> 64#include "un-namespace.h" 65
| 39 40#include "namespace.h" 41#include <sys/param.h> 42#include <sys/socket.h> 43#include <sys/stat.h> 44 45#include <netinet/in.h> 46#include <arpa/inet.h> 47 48#include <signal.h> 49#include <fcntl.h> 50#include <netdb.h> 51#include <stdlib.h> 52#include <unistd.h> 53#include <pwd.h> 54#include <errno.h> 55#include <stdio.h> 56#include <ctype.h> 57#include <string.h> 58#include <rpc/rpc.h> 59#ifdef YP 60#include <rpcsvc/yp_prot.h> 61#include <rpcsvc/ypclnt.h> 62#endif 63#include <arpa/nameser.h> 64#include "un-namespace.h" 65
|
66/* wrapper for KAME-special getnameinfo() */ 67#ifndef NI_WITHSCOPEID 68#define NI_WITHSCOPEID 0 69#endif 70
| |
71extern int innetgr( const char *, const char *, const char *, const char * ); 72 73#define max(a, b) ((a > b) ? a : b) 74 75int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 76int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 77int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 78 const char *); 79static int __icheckhost(const struct sockaddr *, socklen_t, const char *); 80 81char paddr[NI_MAXHOST]; 82 83int 84rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 85 char **ahost; 86 u_short rport; 87 const char *locuser, *remuser, *cmd; 88 int *fd2p; 89{ 90 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 91} 92 93int 94rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 95 char **ahost; 96 u_short rport; 97 const char *locuser, *remuser, *cmd; 98 int *fd2p; 99 int af; 100{ 101 struct addrinfo hints, *res, *ai; 102 struct sockaddr_storage from; 103 fd_set reads; 104 sigset_t oldmask, newmask; 105 pid_t pid; 106 int s, aport, lport, timo, error; 107 char c, *p; 108 int refused, nres; 109 char num[8]; 110 static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 111 112 /* call rcmdsh() with specified remote shell if appropriate. */ 113 if (!issetugid() && (p = getenv("RSH"))) { 114 struct servent *sp = getservbyname("shell", "tcp"); 115 116 if (sp && sp->s_port == rport) 117 return (rcmdsh(ahost, rport, locuser, remuser, 118 cmd, p)); 119 } 120 121 /* use rsh(1) if non-root and remote port is shell. */ 122 if (geteuid()) { 123 struct servent *sp = getservbyname("shell", "tcp"); 124 125 if (sp && sp->s_port == rport) 126 return (rcmdsh(ahost, rport, locuser, remuser, 127 cmd, NULL)); 128 } 129 130 pid = getpid(); 131 132 memset(&hints, 0, sizeof(hints)); 133 hints.ai_flags = AI_CANONNAME; 134 hints.ai_family = af; 135 hints.ai_socktype = SOCK_STREAM; 136 hints.ai_protocol = 0; 137 (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 138 error = getaddrinfo(*ahost, num, &hints, &res); 139 if (error) { 140 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 141 gai_strerror(error)); 142 if (error == EAI_SYSTEM) 143 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 144 strerror(errno)); 145 return (-1); 146 } 147 148 if (res->ai_canonname 149 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 150 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 151 *ahost = canonnamebuf; 152 } 153 nres = 0; 154 for (ai = res; ai; ai = ai->ai_next) 155 nres++; 156 ai = res; 157 refused = 0; 158 sigemptyset(&newmask); 159 sigaddset(&newmask, SIGURG); 160 _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 161 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 162 s = rresvport_af(&lport, ai->ai_family); 163 if (s < 0) { 164 if (errno != EAGAIN && ai->ai_next) { 165 ai = ai->ai_next; 166 continue; 167 } 168 if (errno == EAGAIN) 169 (void)fprintf(stderr, 170 "rcmd: socket: All ports in use\n"); 171 else 172 (void)fprintf(stderr, "rcmd: socket: %s\n", 173 strerror(errno)); 174 freeaddrinfo(res); 175 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 176 NULL); 177 return (-1); 178 } 179 _fcntl(s, F_SETOWN, pid); 180 if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 181 break; 182 (void)_close(s); 183 if (errno == EADDRINUSE) { 184 lport--; 185 continue; 186 } 187 if (errno == ECONNREFUSED) 188 refused = 1; 189 if (ai->ai_next == NULL && (!refused || timo > 16)) { 190 (void)fprintf(stderr, "%s: %s\n", 191 *ahost, strerror(errno)); 192 freeaddrinfo(res); 193 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 194 NULL); 195 return (-1); 196 } 197 if (nres > 1) { 198 int oerrno = errno; 199
| 66extern int innetgr( const char *, const char *, const char *, const char * ); 67 68#define max(a, b) ((a > b) ? a : b) 69 70int __ivaliduser(FILE *, u_int32_t, const char *, const char *); 71int __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 72int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 73 const char *); 74static int __icheckhost(const struct sockaddr *, socklen_t, const char *); 75 76char paddr[NI_MAXHOST]; 77 78int 79rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 80 char **ahost; 81 u_short rport; 82 const char *locuser, *remuser, *cmd; 83 int *fd2p; 84{ 85 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 86} 87 88int 89rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 90 char **ahost; 91 u_short rport; 92 const char *locuser, *remuser, *cmd; 93 int *fd2p; 94 int af; 95{ 96 struct addrinfo hints, *res, *ai; 97 struct sockaddr_storage from; 98 fd_set reads; 99 sigset_t oldmask, newmask; 100 pid_t pid; 101 int s, aport, lport, timo, error; 102 char c, *p; 103 int refused, nres; 104 char num[8]; 105 static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 106 107 /* call rcmdsh() with specified remote shell if appropriate. */ 108 if (!issetugid() && (p = getenv("RSH"))) { 109 struct servent *sp = getservbyname("shell", "tcp"); 110 111 if (sp && sp->s_port == rport) 112 return (rcmdsh(ahost, rport, locuser, remuser, 113 cmd, p)); 114 } 115 116 /* use rsh(1) if non-root and remote port is shell. */ 117 if (geteuid()) { 118 struct servent *sp = getservbyname("shell", "tcp"); 119 120 if (sp && sp->s_port == rport) 121 return (rcmdsh(ahost, rport, locuser, remuser, 122 cmd, NULL)); 123 } 124 125 pid = getpid(); 126 127 memset(&hints, 0, sizeof(hints)); 128 hints.ai_flags = AI_CANONNAME; 129 hints.ai_family = af; 130 hints.ai_socktype = SOCK_STREAM; 131 hints.ai_protocol = 0; 132 (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 133 error = getaddrinfo(*ahost, num, &hints, &res); 134 if (error) { 135 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 136 gai_strerror(error)); 137 if (error == EAI_SYSTEM) 138 fprintf(stderr, "rcmd: getaddrinfo: %s\n", 139 strerror(errno)); 140 return (-1); 141 } 142 143 if (res->ai_canonname 144 && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 145 strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 146 *ahost = canonnamebuf; 147 } 148 nres = 0; 149 for (ai = res; ai; ai = ai->ai_next) 150 nres++; 151 ai = res; 152 refused = 0; 153 sigemptyset(&newmask); 154 sigaddset(&newmask, SIGURG); 155 _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 156 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 157 s = rresvport_af(&lport, ai->ai_family); 158 if (s < 0) { 159 if (errno != EAGAIN && ai->ai_next) { 160 ai = ai->ai_next; 161 continue; 162 } 163 if (errno == EAGAIN) 164 (void)fprintf(stderr, 165 "rcmd: socket: All ports in use\n"); 166 else 167 (void)fprintf(stderr, "rcmd: socket: %s\n", 168 strerror(errno)); 169 freeaddrinfo(res); 170 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 171 NULL); 172 return (-1); 173 } 174 _fcntl(s, F_SETOWN, pid); 175 if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 176 break; 177 (void)_close(s); 178 if (errno == EADDRINUSE) { 179 lport--; 180 continue; 181 } 182 if (errno == ECONNREFUSED) 183 refused = 1; 184 if (ai->ai_next == NULL && (!refused || timo > 16)) { 185 (void)fprintf(stderr, "%s: %s\n", 186 *ahost, strerror(errno)); 187 freeaddrinfo(res); 188 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 189 NULL); 190 return (-1); 191 } 192 if (nres > 1) { 193 int oerrno = errno; 194
|
200 getnameinfo(ai->ai_addr, ai->ai_addrlen, 201 paddr, sizeof(paddr), 202 NULL, 0, 203 NI_NUMERICHOST|NI_WITHSCOPEID);
| 195 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 196 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
|
204 (void)fprintf(stderr, "connect to address %s: ", 205 paddr); 206 errno = oerrno; 207 perror(0); 208 } 209 if ((ai = ai->ai_next) == NULL) { 210 /* refused && timo <= 16 */ 211 struct timespec time_to_sleep, time_remaining; 212 213 time_to_sleep.tv_sec = timo; 214 time_to_sleep.tv_nsec = 0; 215 (void)_nanosleep(&time_to_sleep, &time_remaining); 216 timo *= 2; 217 ai = res; 218 refused = 0; 219 } 220 if (nres > 1) {
| 197 (void)fprintf(stderr, "connect to address %s: ", 198 paddr); 199 errno = oerrno; 200 perror(0); 201 } 202 if ((ai = ai->ai_next) == NULL) { 203 /* refused && timo <= 16 */ 204 struct timespec time_to_sleep, time_remaining; 205 206 time_to_sleep.tv_sec = timo; 207 time_to_sleep.tv_nsec = 0; 208 (void)_nanosleep(&time_to_sleep, &time_remaining); 209 timo *= 2; 210 ai = res; 211 refused = 0; 212 } 213 if (nres > 1) {
|
221 getnameinfo(ai->ai_addr, ai->ai_addrlen, 222 paddr, sizeof(paddr), 223 NULL, 0, 224 NI_NUMERICHOST|NI_WITHSCOPEID);
| 214 getnameinfo(ai->ai_addr, ai->ai_addrlen, paddr, 215 sizeof(paddr), NULL, 0, NI_NUMERICHOST);
|
225 fprintf(stderr, "Trying %s...\n", paddr); 226 } 227 } 228 lport--; 229 if (fd2p == 0) { 230 _write(s, "", 1); 231 lport = 0; 232 } else { 233 int s2 = rresvport_af(&lport, ai->ai_family), s3; 234 socklen_t len = ai->ai_addrlen; 235 int nfds; 236 237 if (s2 < 0) 238 goto bad; 239 _listen(s2, 1); 240 (void)snprintf(num, sizeof(num), "%d", lport); 241 if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 242 (void)fprintf(stderr, 243 "rcmd: write (setting up stderr): %s\n", 244 strerror(errno)); 245 (void)_close(s2); 246 goto bad; 247 } 248 nfds = max(s, s2)+1; 249 if(nfds > FD_SETSIZE) { 250 fprintf(stderr, "rcmd: too many files\n"); 251 (void)_close(s2); 252 goto bad; 253 } 254again: 255 FD_ZERO(&reads); 256 FD_SET(s, &reads); 257 FD_SET(s2, &reads); 258 errno = 0; 259 if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 260 if (errno != 0) 261 (void)fprintf(stderr, 262 "rcmd: select (setting up stderr): %s\n", 263 strerror(errno)); 264 else 265 (void)fprintf(stderr, 266 "select: protocol failure in circuit setup\n"); 267 (void)_close(s2); 268 goto bad; 269 } 270 s3 = _accept(s2, (struct sockaddr *)&from, &len); 271 switch (from.ss_family) { 272 case AF_INET: 273 aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 274 break; 275#ifdef INET6 276 case AF_INET6: 277 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 278 break; 279#endif 280 default: 281 aport = 0; /* error */ 282 break; 283 } 284 /* 285 * XXX careful for ftp bounce attacks. If discovered, shut them 286 * down and check for the real auxiliary channel to connect. 287 */ 288 if (aport == 20) { 289 _close(s3); 290 goto again; 291 } 292 (void)_close(s2); 293 if (s3 < 0) { 294 (void)fprintf(stderr, 295 "rcmd: accept: %s\n", strerror(errno)); 296 lport = 0; 297 goto bad; 298 } 299 *fd2p = s3; 300 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 301 (void)fprintf(stderr, 302 "socket: protocol failure in circuit setup.\n"); 303 goto bad2; 304 } 305 } 306 (void)_write(s, locuser, strlen(locuser)+1); 307 (void)_write(s, remuser, strlen(remuser)+1); 308 (void)_write(s, cmd, strlen(cmd)+1); 309 if (_read(s, &c, 1) != 1) { 310 (void)fprintf(stderr, 311 "rcmd: %s: %s\n", *ahost, strerror(errno)); 312 goto bad2; 313 } 314 if (c != 0) { 315 while (_read(s, &c, 1) == 1) { 316 (void)_write(STDERR_FILENO, &c, 1); 317 if (c == '\n') 318 break; 319 } 320 goto bad2; 321 } 322 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 323 freeaddrinfo(res); 324 return (s); 325bad2: 326 if (lport) 327 (void)_close(*fd2p); 328bad: 329 (void)_close(s); 330 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 331 freeaddrinfo(res); 332 return (-1); 333} 334 335int 336rresvport(port) 337 int *port; 338{ 339 return rresvport_af(port, AF_INET); 340} 341 342int 343rresvport_af(alport, family) 344 int *alport, family; 345{ 346 int s; 347 struct sockaddr_storage ss; 348 u_short *sport; 349 350 memset(&ss, 0, sizeof(ss)); 351 ss.ss_family = family; 352 switch (family) { 353 case AF_INET: 354 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 355 sport = &((struct sockaddr_in *)&ss)->sin_port; 356 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 357 break; 358#ifdef INET6 359 case AF_INET6: 360 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 361 sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 362 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 363 break; 364#endif 365 default: 366 errno = EAFNOSUPPORT; 367 return -1; 368 } 369 370 s = _socket(ss.ss_family, SOCK_STREAM, 0); 371 if (s < 0) 372 return (-1); 373#if 0 /* compat_exact_traditional_rresvport_semantics */ 374 sin.sin_port = htons((u_short)*alport); 375 if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 376 return (s); 377 if (errno != EADDRINUSE) { 378 (void)_close(s); 379 return (-1); 380 } 381#endif 382 *sport = 0; 383 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 384 (void)_close(s); 385 return (-1); 386 } 387 *alport = (int)ntohs(*sport); 388 return (s); 389} 390 391int __check_rhosts_file = 1; 392char *__rcmd_errstr; 393 394int 395ruserok(rhost, superuser, ruser, luser) 396 const char *rhost, *ruser, *luser; 397 int superuser; 398{ 399 struct addrinfo hints, *res, *r; 400 int error; 401 402 memset(&hints, 0, sizeof(hints)); 403 hints.ai_family = PF_UNSPEC; 404 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 405 error = getaddrinfo(rhost, "0", &hints, &res); 406 if (error) 407 return (-1); 408 409 for (r = res; r; r = r->ai_next) { 410 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 411 luser) == 0) { 412 freeaddrinfo(res); 413 return (0); 414 } 415 } 416 freeaddrinfo(res); 417 return (-1); 418} 419 420/* 421 * New .rhosts strategy: We are passed an ip address. We spin through 422 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 423 * has ip addresses, we don't have to trust a nameserver. When it 424 * contains hostnames, we spin through the list of addresses the nameserver 425 * gives us and look for a match. 426 * 427 * Returns 0 if ok, -1 if not ok. 428 */ 429int 430iruserok(raddr, superuser, ruser, luser) 431 unsigned long raddr; 432 int superuser; 433 const char *ruser, *luser; 434{ 435 struct sockaddr_in sin; 436 437 memset(&sin, 0, sizeof(sin)); 438 sin.sin_family = AF_INET; 439 sin.sin_len = sizeof(struct sockaddr_in); 440 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 441 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 442 ruser, luser); 443} 444 445/* 446 * AF independent extension of iruserok. 447 * 448 * Returns 0 if ok, -1 if not ok. 449 */ 450int 451iruserok_sa(ra, rlen, superuser, ruser, luser) 452 const void *ra; 453 int rlen; 454 int superuser; 455 const char *ruser, *luser; 456{ 457 char *cp; 458 struct stat sbuf; 459 struct passwd *pwd; 460 FILE *hostf; 461 uid_t uid; 462 int first; 463 char pbuf[MAXPATHLEN]; 464 const struct sockaddr *raddr; 465 struct sockaddr_storage ss; 466 467 /* avoid alignment issue */ 468 if (rlen > sizeof(ss)) 469 return(-1); 470 memcpy(&ss, ra, rlen); 471 raddr = (struct sockaddr *)&ss; 472 473 first = 1; 474 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 475again: 476 if (hostf) { 477 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 478 (void)fclose(hostf); 479 return (0); 480 } 481 (void)fclose(hostf); 482 } 483 if (first == 1 && (__check_rhosts_file || superuser)) { 484 first = 0; 485 if ((pwd = getpwnam(luser)) == NULL) 486 return (-1); 487 (void)strcpy(pbuf, pwd->pw_dir); 488 (void)strcat(pbuf, "/.rhosts"); 489 490 /* 491 * Change effective uid while opening .rhosts. If root and 492 * reading an NFS mounted file system, can't read files that 493 * are protected read/write owner only. 494 */ 495 uid = geteuid(); 496 (void)seteuid(pwd->pw_uid); 497 hostf = fopen(pbuf, "r"); 498 (void)seteuid(uid); 499 500 if (hostf == NULL) 501 return (-1); 502 /* 503 * If not a regular file, or is owned by someone other than 504 * user or root or if writeable by anyone but the owner, quit. 505 */ 506 cp = NULL; 507 if (lstat(pbuf, &sbuf) < 0) 508 cp = ".rhosts lstat failed"; 509 else if (!S_ISREG(sbuf.st_mode)) 510 cp = ".rhosts not regular file"; 511 else if (_fstat(fileno(hostf), &sbuf) < 0) 512 cp = ".rhosts fstat failed"; 513 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 514 cp = "bad .rhosts owner"; 515 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 516 cp = ".rhosts writeable by other than owner"; 517 /* If there were any problems, quit. */ 518 if (cp) { 519 __rcmd_errstr = cp; 520 (void)fclose(hostf); 521 return (-1); 522 } 523 goto again; 524 } 525 return (-1); 526} 527 528/* 529 * XXX 530 * Don't make static, used by lpd(8). 531 * 532 * Returns 0 if ok, -1 if not ok. 533 */ 534int 535__ivaliduser(hostf, raddr, luser, ruser) 536 FILE *hostf; 537 u_int32_t raddr; 538 const char *luser, *ruser; 539{ 540 struct sockaddr_in sin; 541 542 memset(&sin, 0, sizeof(sin)); 543 sin.sin_family = AF_INET; 544 sin.sin_len = sizeof(struct sockaddr_in); 545 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 546 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 547 luser, ruser); 548} 549 550/* 551 * Returns 0 if ok, -1 if not ok. 552 * 553 * XXX obsolete API. 554 */ 555int 556__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 557 FILE *hostf; 558 const void *raddr; 559 const char *luser, *ruser; 560 int af, len; 561{ 562 struct sockaddr *sa = NULL; 563 struct sockaddr_in *sin = NULL; 564#ifdef INET6 565 struct sockaddr_in6 *sin6 = NULL; 566#endif 567 struct sockaddr_storage ss; 568 569 memset(&ss, 0, sizeof(ss)); 570 switch (af) { 571 case AF_INET: 572 if (len != sizeof(sin->sin_addr)) 573 return -1; 574 sin = (struct sockaddr_in *)&ss; 575 sin->sin_family = AF_INET; 576 sin->sin_len = sizeof(struct sockaddr_in); 577 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 578 break; 579#ifdef INET6 580 case AF_INET6: 581 if (len != sizeof(sin6->sin6_addr)) 582 return -1; 583 /* you will lose scope info */ 584 sin6 = (struct sockaddr_in6 *)&ss; 585 sin6->sin6_family = AF_INET6; 586 sin6->sin6_len = sizeof(struct sockaddr_in6); 587 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 588 break; 589#endif 590 default: 591 return -1; 592 } 593 594 sa = (struct sockaddr *)&ss; 595 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 596} 597 598int 599__ivaliduser_sa(hostf, raddr, salen, luser, ruser) 600 FILE *hostf; 601 const struct sockaddr *raddr; 602 socklen_t salen; 603 const char *luser, *ruser; 604{ 605 char *user, *p; 606 int ch; 607 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 608 char hname[MAXHOSTNAMELEN]; 609 /* Presumed guilty until proven innocent. */ 610 int userok = 0, hostok = 0; 611#ifdef YP 612 char *ypdomain; 613 614 if (yp_get_default_domain(&ypdomain)) 615 ypdomain = NULL; 616#else 617#define ypdomain NULL 618#endif 619 /* We need to get the damn hostname back for netgroup matching. */ 620 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 621 NI_NAMEREQD) != 0) 622 hname[0] = '\0'; 623 624 while (fgets(buf, sizeof(buf), hostf)) { 625 p = buf; 626 /* Skip lines that are too long. */ 627 if (strchr(p, '\n') == NULL) { 628 while ((ch = getc(hostf)) != '\n' && ch != EOF); 629 continue; 630 } 631 if (*p == '\n' || *p == '#') { 632 /* comment... */ 633 continue; 634 } 635 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 636 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 637 p++; 638 } 639 if (*p == ' ' || *p == '\t') { 640 *p++ = '\0'; 641 while (*p == ' ' || *p == '\t') 642 p++; 643 user = p; 644 while (*p != '\n' && *p != ' ' && 645 *p != '\t' && *p != '\0') 646 p++; 647 } else 648 user = p; 649 *p = '\0'; 650 /* 651 * Do +/- and +@/-@ checking. This looks really nasty, 652 * but it matches SunOS's behavior so far as I can tell. 653 */ 654 switch(buf[0]) { 655 case '+': 656 if (!buf[1]) { /* '+' matches all hosts */ 657 hostok = 1; 658 break; 659 } 660 if (buf[1] == '@') /* match a host by netgroup */ 661 hostok = hname[0] != '\0' && 662 innetgr(&buf[2], hname, NULL, ypdomain); 663 else /* match a host by addr */ 664 hostok = __icheckhost(raddr, salen, 665 (char *)&buf[1]); 666 break; 667 case '-': /* reject '-' hosts and all their users */ 668 if (buf[1] == '@') { 669 if (hname[0] == '\0' || 670 innetgr(&buf[2], hname, NULL, ypdomain)) 671 return(-1); 672 } else { 673 if (__icheckhost(raddr, salen, 674 (char *)&buf[1])) 675 return(-1); 676 } 677 break; 678 default: /* if no '+' or '-', do a simple match */ 679 hostok = __icheckhost(raddr, salen, buf); 680 break; 681 } 682 switch(*user) { 683 case '+': 684 if (!*(user+1)) { /* '+' matches all users */ 685 userok = 1; 686 break; 687 } 688 if (*(user+1) == '@') /* match a user by netgroup */ 689 userok = innetgr(user+2, NULL, ruser, ypdomain); 690 else /* match a user by direct specification */ 691 userok = !(strcmp(ruser, user+1)); 692 break; 693 case '-': /* if we matched a hostname, */ 694 if (hostok) { /* check for user field rejections */ 695 if (!*(user+1)) 696 return(-1); 697 if (*(user+1) == '@') { 698 if (innetgr(user+2, NULL, 699 ruser, ypdomain)) 700 return(-1); 701 } else { 702 if (!strcmp(ruser, user+1)) 703 return(-1); 704 } 705 } 706 break; 707 default: /* no rejections: try to match the user */ 708 if (hostok) 709 userok = !(strcmp(ruser,*user ? user : luser)); 710 break; 711 } 712 if (hostok && userok) 713 return(0); 714 } 715 return (-1); 716} 717 718/* 719 * Returns "true" if match, 0 if no match.
| 216 fprintf(stderr, "Trying %s...\n", paddr); 217 } 218 } 219 lport--; 220 if (fd2p == 0) { 221 _write(s, "", 1); 222 lport = 0; 223 } else { 224 int s2 = rresvport_af(&lport, ai->ai_family), s3; 225 socklen_t len = ai->ai_addrlen; 226 int nfds; 227 228 if (s2 < 0) 229 goto bad; 230 _listen(s2, 1); 231 (void)snprintf(num, sizeof(num), "%d", lport); 232 if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 233 (void)fprintf(stderr, 234 "rcmd: write (setting up stderr): %s\n", 235 strerror(errno)); 236 (void)_close(s2); 237 goto bad; 238 } 239 nfds = max(s, s2)+1; 240 if(nfds > FD_SETSIZE) { 241 fprintf(stderr, "rcmd: too many files\n"); 242 (void)_close(s2); 243 goto bad; 244 } 245again: 246 FD_ZERO(&reads); 247 FD_SET(s, &reads); 248 FD_SET(s2, &reads); 249 errno = 0; 250 if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 251 if (errno != 0) 252 (void)fprintf(stderr, 253 "rcmd: select (setting up stderr): %s\n", 254 strerror(errno)); 255 else 256 (void)fprintf(stderr, 257 "select: protocol failure in circuit setup\n"); 258 (void)_close(s2); 259 goto bad; 260 } 261 s3 = _accept(s2, (struct sockaddr *)&from, &len); 262 switch (from.ss_family) { 263 case AF_INET: 264 aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 265 break; 266#ifdef INET6 267 case AF_INET6: 268 aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 269 break; 270#endif 271 default: 272 aport = 0; /* error */ 273 break; 274 } 275 /* 276 * XXX careful for ftp bounce attacks. If discovered, shut them 277 * down and check for the real auxiliary channel to connect. 278 */ 279 if (aport == 20) { 280 _close(s3); 281 goto again; 282 } 283 (void)_close(s2); 284 if (s3 < 0) { 285 (void)fprintf(stderr, 286 "rcmd: accept: %s\n", strerror(errno)); 287 lport = 0; 288 goto bad; 289 } 290 *fd2p = s3; 291 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 292 (void)fprintf(stderr, 293 "socket: protocol failure in circuit setup.\n"); 294 goto bad2; 295 } 296 } 297 (void)_write(s, locuser, strlen(locuser)+1); 298 (void)_write(s, remuser, strlen(remuser)+1); 299 (void)_write(s, cmd, strlen(cmd)+1); 300 if (_read(s, &c, 1) != 1) { 301 (void)fprintf(stderr, 302 "rcmd: %s: %s\n", *ahost, strerror(errno)); 303 goto bad2; 304 } 305 if (c != 0) { 306 while (_read(s, &c, 1) == 1) { 307 (void)_write(STDERR_FILENO, &c, 1); 308 if (c == '\n') 309 break; 310 } 311 goto bad2; 312 } 313 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 314 freeaddrinfo(res); 315 return (s); 316bad2: 317 if (lport) 318 (void)_close(*fd2p); 319bad: 320 (void)_close(s); 321 _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 322 freeaddrinfo(res); 323 return (-1); 324} 325 326int 327rresvport(port) 328 int *port; 329{ 330 return rresvport_af(port, AF_INET); 331} 332 333int 334rresvport_af(alport, family) 335 int *alport, family; 336{ 337 int s; 338 struct sockaddr_storage ss; 339 u_short *sport; 340 341 memset(&ss, 0, sizeof(ss)); 342 ss.ss_family = family; 343 switch (family) { 344 case AF_INET: 345 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 346 sport = &((struct sockaddr_in *)&ss)->sin_port; 347 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 348 break; 349#ifdef INET6 350 case AF_INET6: 351 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 352 sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 353 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 354 break; 355#endif 356 default: 357 errno = EAFNOSUPPORT; 358 return -1; 359 } 360 361 s = _socket(ss.ss_family, SOCK_STREAM, 0); 362 if (s < 0) 363 return (-1); 364#if 0 /* compat_exact_traditional_rresvport_semantics */ 365 sin.sin_port = htons((u_short)*alport); 366 if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 367 return (s); 368 if (errno != EADDRINUSE) { 369 (void)_close(s); 370 return (-1); 371 } 372#endif 373 *sport = 0; 374 if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 375 (void)_close(s); 376 return (-1); 377 } 378 *alport = (int)ntohs(*sport); 379 return (s); 380} 381 382int __check_rhosts_file = 1; 383char *__rcmd_errstr; 384 385int 386ruserok(rhost, superuser, ruser, luser) 387 const char *rhost, *ruser, *luser; 388 int superuser; 389{ 390 struct addrinfo hints, *res, *r; 391 int error; 392 393 memset(&hints, 0, sizeof(hints)); 394 hints.ai_family = PF_UNSPEC; 395 hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 396 error = getaddrinfo(rhost, "0", &hints, &res); 397 if (error) 398 return (-1); 399 400 for (r = res; r; r = r->ai_next) { 401 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 402 luser) == 0) { 403 freeaddrinfo(res); 404 return (0); 405 } 406 } 407 freeaddrinfo(res); 408 return (-1); 409} 410 411/* 412 * New .rhosts strategy: We are passed an ip address. We spin through 413 * hosts.equiv and .rhosts looking for a match. When the .rhosts only 414 * has ip addresses, we don't have to trust a nameserver. When it 415 * contains hostnames, we spin through the list of addresses the nameserver 416 * gives us and look for a match. 417 * 418 * Returns 0 if ok, -1 if not ok. 419 */ 420int 421iruserok(raddr, superuser, ruser, luser) 422 unsigned long raddr; 423 int superuser; 424 const char *ruser, *luser; 425{ 426 struct sockaddr_in sin; 427 428 memset(&sin, 0, sizeof(sin)); 429 sin.sin_family = AF_INET; 430 sin.sin_len = sizeof(struct sockaddr_in); 431 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 432 return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 433 ruser, luser); 434} 435 436/* 437 * AF independent extension of iruserok. 438 * 439 * Returns 0 if ok, -1 if not ok. 440 */ 441int 442iruserok_sa(ra, rlen, superuser, ruser, luser) 443 const void *ra; 444 int rlen; 445 int superuser; 446 const char *ruser, *luser; 447{ 448 char *cp; 449 struct stat sbuf; 450 struct passwd *pwd; 451 FILE *hostf; 452 uid_t uid; 453 int first; 454 char pbuf[MAXPATHLEN]; 455 const struct sockaddr *raddr; 456 struct sockaddr_storage ss; 457 458 /* avoid alignment issue */ 459 if (rlen > sizeof(ss)) 460 return(-1); 461 memcpy(&ss, ra, rlen); 462 raddr = (struct sockaddr *)&ss; 463 464 first = 1; 465 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 466again: 467 if (hostf) { 468 if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 469 (void)fclose(hostf); 470 return (0); 471 } 472 (void)fclose(hostf); 473 } 474 if (first == 1 && (__check_rhosts_file || superuser)) { 475 first = 0; 476 if ((pwd = getpwnam(luser)) == NULL) 477 return (-1); 478 (void)strcpy(pbuf, pwd->pw_dir); 479 (void)strcat(pbuf, "/.rhosts"); 480 481 /* 482 * Change effective uid while opening .rhosts. If root and 483 * reading an NFS mounted file system, can't read files that 484 * are protected read/write owner only. 485 */ 486 uid = geteuid(); 487 (void)seteuid(pwd->pw_uid); 488 hostf = fopen(pbuf, "r"); 489 (void)seteuid(uid); 490 491 if (hostf == NULL) 492 return (-1); 493 /* 494 * If not a regular file, or is owned by someone other than 495 * user or root or if writeable by anyone but the owner, quit. 496 */ 497 cp = NULL; 498 if (lstat(pbuf, &sbuf) < 0) 499 cp = ".rhosts lstat failed"; 500 else if (!S_ISREG(sbuf.st_mode)) 501 cp = ".rhosts not regular file"; 502 else if (_fstat(fileno(hostf), &sbuf) < 0) 503 cp = ".rhosts fstat failed"; 504 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 505 cp = "bad .rhosts owner"; 506 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 507 cp = ".rhosts writeable by other than owner"; 508 /* If there were any problems, quit. */ 509 if (cp) { 510 __rcmd_errstr = cp; 511 (void)fclose(hostf); 512 return (-1); 513 } 514 goto again; 515 } 516 return (-1); 517} 518 519/* 520 * XXX 521 * Don't make static, used by lpd(8). 522 * 523 * Returns 0 if ok, -1 if not ok. 524 */ 525int 526__ivaliduser(hostf, raddr, luser, ruser) 527 FILE *hostf; 528 u_int32_t raddr; 529 const char *luser, *ruser; 530{ 531 struct sockaddr_in sin; 532 533 memset(&sin, 0, sizeof(sin)); 534 sin.sin_family = AF_INET; 535 sin.sin_len = sizeof(struct sockaddr_in); 536 memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 537 return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 538 luser, ruser); 539} 540 541/* 542 * Returns 0 if ok, -1 if not ok. 543 * 544 * XXX obsolete API. 545 */ 546int 547__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 548 FILE *hostf; 549 const void *raddr; 550 const char *luser, *ruser; 551 int af, len; 552{ 553 struct sockaddr *sa = NULL; 554 struct sockaddr_in *sin = NULL; 555#ifdef INET6 556 struct sockaddr_in6 *sin6 = NULL; 557#endif 558 struct sockaddr_storage ss; 559 560 memset(&ss, 0, sizeof(ss)); 561 switch (af) { 562 case AF_INET: 563 if (len != sizeof(sin->sin_addr)) 564 return -1; 565 sin = (struct sockaddr_in *)&ss; 566 sin->sin_family = AF_INET; 567 sin->sin_len = sizeof(struct sockaddr_in); 568 memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 569 break; 570#ifdef INET6 571 case AF_INET6: 572 if (len != sizeof(sin6->sin6_addr)) 573 return -1; 574 /* you will lose scope info */ 575 sin6 = (struct sockaddr_in6 *)&ss; 576 sin6->sin6_family = AF_INET6; 577 sin6->sin6_len = sizeof(struct sockaddr_in6); 578 memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 579 break; 580#endif 581 default: 582 return -1; 583 } 584 585 sa = (struct sockaddr *)&ss; 586 return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 587} 588 589int 590__ivaliduser_sa(hostf, raddr, salen, luser, ruser) 591 FILE *hostf; 592 const struct sockaddr *raddr; 593 socklen_t salen; 594 const char *luser, *ruser; 595{ 596 char *user, *p; 597 int ch; 598 char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 599 char hname[MAXHOSTNAMELEN]; 600 /* Presumed guilty until proven innocent. */ 601 int userok = 0, hostok = 0; 602#ifdef YP 603 char *ypdomain; 604 605 if (yp_get_default_domain(&ypdomain)) 606 ypdomain = NULL; 607#else 608#define ypdomain NULL 609#endif 610 /* We need to get the damn hostname back for netgroup matching. */ 611 if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 612 NI_NAMEREQD) != 0) 613 hname[0] = '\0'; 614 615 while (fgets(buf, sizeof(buf), hostf)) { 616 p = buf; 617 /* Skip lines that are too long. */ 618 if (strchr(p, '\n') == NULL) { 619 while ((ch = getc(hostf)) != '\n' && ch != EOF); 620 continue; 621 } 622 if (*p == '\n' || *p == '#') { 623 /* comment... */ 624 continue; 625 } 626 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 627 *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 628 p++; 629 } 630 if (*p == ' ' || *p == '\t') { 631 *p++ = '\0'; 632 while (*p == ' ' || *p == '\t') 633 p++; 634 user = p; 635 while (*p != '\n' && *p != ' ' && 636 *p != '\t' && *p != '\0') 637 p++; 638 } else 639 user = p; 640 *p = '\0'; 641 /* 642 * Do +/- and +@/-@ checking. This looks really nasty, 643 * but it matches SunOS's behavior so far as I can tell. 644 */ 645 switch(buf[0]) { 646 case '+': 647 if (!buf[1]) { /* '+' matches all hosts */ 648 hostok = 1; 649 break; 650 } 651 if (buf[1] == '@') /* match a host by netgroup */ 652 hostok = hname[0] != '\0' && 653 innetgr(&buf[2], hname, NULL, ypdomain); 654 else /* match a host by addr */ 655 hostok = __icheckhost(raddr, salen, 656 (char *)&buf[1]); 657 break; 658 case '-': /* reject '-' hosts and all their users */ 659 if (buf[1] == '@') { 660 if (hname[0] == '\0' || 661 innetgr(&buf[2], hname, NULL, ypdomain)) 662 return(-1); 663 } else { 664 if (__icheckhost(raddr, salen, 665 (char *)&buf[1])) 666 return(-1); 667 } 668 break; 669 default: /* if no '+' or '-', do a simple match */ 670 hostok = __icheckhost(raddr, salen, buf); 671 break; 672 } 673 switch(*user) { 674 case '+': 675 if (!*(user+1)) { /* '+' matches all users */ 676 userok = 1; 677 break; 678 } 679 if (*(user+1) == '@') /* match a user by netgroup */ 680 userok = innetgr(user+2, NULL, ruser, ypdomain); 681 else /* match a user by direct specification */ 682 userok = !(strcmp(ruser, user+1)); 683 break; 684 case '-': /* if we matched a hostname, */ 685 if (hostok) { /* check for user field rejections */ 686 if (!*(user+1)) 687 return(-1); 688 if (*(user+1) == '@') { 689 if (innetgr(user+2, NULL, 690 ruser, ypdomain)) 691 return(-1); 692 } else { 693 if (!strcmp(ruser, user+1)) 694 return(-1); 695 } 696 } 697 break; 698 default: /* no rejections: try to match the user */ 699 if (hostok) 700 userok = !(strcmp(ruser,*user ? user : luser)); 701 break; 702 } 703 if (hostok && userok) 704 return(0); 705 } 706 return (-1); 707} 708 709/* 710 * Returns "true" if match, 0 if no match.
|
720 * 721 * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 722 * if af == AF_INET6.
| |
723 */ 724static int 725__icheckhost(raddr, salen, lhost) 726 const struct sockaddr *raddr; 727 socklen_t salen; 728 const char *lhost; 729{ 730 struct sockaddr_in sin; 731 struct sockaddr_in6 *sin6; 732 struct addrinfo hints, *res, *r; 733 int error; 734 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 735 736 if (raddr->sa_family == AF_INET6) { 737 sin6 = (struct sockaddr_in6 *)raddr; 738 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 739 memset(&sin, 0, sizeof(sin)); 740 sin.sin_family = AF_INET; 741 sin.sin_len = sizeof(struct sockaddr_in); 742 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 743 sizeof(sin.sin_addr)); 744 raddr = (struct sockaddr *)&sin; 745 salen = sin.sin_len; 746 } 747 } 748 749 h1[0] = '\0'; 750 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
| 711 */ 712static int 713__icheckhost(raddr, salen, lhost) 714 const struct sockaddr *raddr; 715 socklen_t salen; 716 const char *lhost; 717{ 718 struct sockaddr_in sin; 719 struct sockaddr_in6 *sin6; 720 struct addrinfo hints, *res, *r; 721 int error; 722 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 723 724 if (raddr->sa_family == AF_INET6) { 725 sin6 = (struct sockaddr_in6 *)raddr; 726 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 727 memset(&sin, 0, sizeof(sin)); 728 sin.sin_family = AF_INET; 729 sin.sin_len = sizeof(struct sockaddr_in); 730 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 731 sizeof(sin.sin_addr)); 732 raddr = (struct sockaddr *)&sin; 733 salen = sin.sin_len; 734 } 735 } 736 737 h1[0] = '\0'; 738 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
|
751 NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
| 739 NI_NUMERICHOST) != 0)
|
752 return (0); 753 754 /* Resolve laddr into sockaddr */ 755 memset(&hints, 0, sizeof(hints)); 756 hints.ai_family = raddr->sa_family; 757 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 758 res = NULL; 759 error = getaddrinfo(lhost, "0", &hints, &res); 760 if (error) 761 return (0); 762 763 for (r = res; r ; r = r->ai_next) { 764 h2[0] = '\0'; 765 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
| 740 return (0); 741 742 /* Resolve laddr into sockaddr */ 743 memset(&hints, 0, sizeof(hints)); 744 hints.ai_family = raddr->sa_family; 745 hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 746 res = NULL; 747 error = getaddrinfo(lhost, "0", &hints, &res); 748 if (error) 749 return (0); 750 751 for (r = res; r ; r = r->ai_next) { 752 h2[0] = '\0'; 753 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
|
766 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0)
| 754 NULL, 0, NI_NUMERICHOST) != 0)
|
767 continue; 768 if (strcmp(h1, h2) == 0) { 769 freeaddrinfo(res); 770 return (1); 771 } 772 } 773 774 /* No match. */ 775 freeaddrinfo(res); 776 return (0); 777}
| 755 continue; 756 if (strcmp(h1, h2) == 0) { 757 freeaddrinfo(res); 758 return (1); 759 } 760 } 761 762 /* No match. */ 763 freeaddrinfo(res); 764 return (0); 765}
|