]>
Commit | Line | Data |
---|---|---|
03fb6eb0 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ad21edcc A |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
03fb6eb0 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
ad21edcc | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
03fb6eb0 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ad21edcc A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
03fb6eb0 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* | |
25 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | |
26 | * Copyright (c) 1983, 1993, 1994 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * This product includes software developed by Theo de Raadt. | |
42 | * 4. Neither the name of the University nor the names of its contributors | |
43 | * may be used to endorse or promote products derived from this software | |
44 | * without specific prior written permission. | |
45 | * | |
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
56 | * SUCH DAMAGE. | |
3b7c7bd7 A |
57 | * |
58 | * $FreeBSD: src/lib/libc/net/rcmd.c,v 1.23.2.5 2001/03/05 10:47:11 obrien Exp $ | |
03fb6eb0 A |
59 | */ |
60 | ||
61 | #if defined(LIBC_SCCS) && !defined(lint) | |
3b7c7bd7 | 62 | static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; |
03fb6eb0 A |
63 | #endif /* LIBC_SCCS and not lint */ |
64 | ||
65 | #include <sys/param.h> | |
66 | #include <sys/socket.h> | |
67 | #include <sys/stat.h> | |
68 | ||
69 | #include <netinet/in.h> | |
70 | #include <arpa/inet.h> | |
71 | #include <netinfo/ni_util.h> | |
72 | ||
73 | #include <signal.h> | |
74 | #include <fcntl.h> | |
75 | #include <netdb.h> | |
76 | #include <unistd.h> | |
77 | #include <pwd.h> | |
78 | #include <errno.h> | |
79 | #include <stdio.h> | |
80 | #include <ctype.h> | |
81 | #include <string.h> | |
3b7c7bd7 A |
82 | #ifdef YP |
83 | #include <rpc/rpc.h> | |
84 | #include <rpcsvc/yp_prot.h> | |
85 | #include <rpcsvc/ypclnt.h> | |
86 | #endif | |
ccd4a120 | 87 | #include <arpa/nameser8_compat.h> |
3b7c7bd7 A |
88 | |
89 | /* wrapper for KAME-special getnameinfo() */ | |
90 | #ifndef NI_WITHSCOPEID | |
91 | #define NI_WITHSCOPEID 0 | |
92 | #endif | |
93 | ||
94 | #ifndef socklen_t | |
95 | #define socklen_t int | |
96 | #endif | |
03fb6eb0 | 97 | |
3b7c7bd7 | 98 | extern int innetgr __P(( const char *, const char *, const char *, const char * )); |
ccd4a120 | 99 | extern int bindresvport_sa(int, struct sockaddr *); |
03fb6eb0 | 100 | |
3b7c7bd7 A |
101 | #define max(a, b) ((a > b) ? a : b) |
102 | ||
103 | int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *)); | |
104 | int __ivaliduser_af __P((FILE *,const void *, const char *, const char *, | |
105 | int, int)); | |
106 | int __ivaliduser_sa __P((FILE *, const struct sockaddr *, socklen_t, | |
107 | const char *,const char *)); | |
108 | static int __icheckhost __P((const struct sockaddr *, socklen_t, | |
109 | const char *)); | |
03fb6eb0 | 110 | |
3b7c7bd7 A |
111 | int |
112 | rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) | |
113 | char **ahost; | |
114 | u_short rport; | |
115 | const char *locuser, *remuser, *cmd; | |
116 | int *fd2p; | |
117 | int af; | |
118 | { | |
119 | struct addrinfo hints, *res, *ai; | |
120 | struct sockaddr_storage from; | |
121 | fd_set reads; | |
122 | long oldmask; | |
123 | pid_t pid; | |
124 | int s, aport, lport, timo, error; | |
125 | char c; | |
126 | int refused, nres; | |
127 | char num[8], paddr[NI_MAXHOST]; | |
128 | static char canonnamebuf[MAXDNAME]; /* is it proper here? */ | |
03fb6eb0 A |
129 | |
130 | pid = getpid(); | |
3b7c7bd7 A |
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)); | |
03fb6eb0 A |
145 | return (-1); |
146 | } | |
03fb6eb0 | 147 | |
3b7c7bd7 A |
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; | |
03fb6eb0 A |
158 | oldmask = sigblock(sigmask(SIGURG)); |
159 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | |
3b7c7bd7 | 160 | s = rresvport_af(&lport, ai->ai_family); |
03fb6eb0 | 161 | if (s < 0) { |
3b7c7bd7 A |
162 | if (errno != EAGAIN && ai->ai_next) { |
163 | ai = ai->ai_next; | |
164 | continue; | |
165 | } | |
03fb6eb0 A |
166 | if (errno == EAGAIN) |
167 | (void)fprintf(stderr, | |
168 | "rcmd: socket: All ports in use\n"); | |
169 | else | |
170 | (void)fprintf(stderr, "rcmd: socket: %s\n", | |
171 | strerror(errno)); | |
3b7c7bd7 | 172 | freeaddrinfo(res); |
03fb6eb0 A |
173 | sigsetmask(oldmask); |
174 | return (-1); | |
175 | } | |
176 | fcntl(s, F_SETOWN, pid); | |
3b7c7bd7 | 177 | if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) |
03fb6eb0 A |
178 | break; |
179 | (void)close(s); | |
180 | if (errno == EADDRINUSE) { | |
181 | lport--; | |
182 | continue; | |
183 | } | |
3b7c7bd7 A |
184 | if (errno == ECONNREFUSED) |
185 | refused = 1; | |
186 | if (ai->ai_next == NULL && (!refused || timo > 16)) { | |
187 | (void)fprintf(stderr, "%s: %s\n", | |
188 | *ahost, strerror(errno)); | |
189 | freeaddrinfo(res); | |
190 | sigsetmask(oldmask); | |
191 | return (-1); | |
03fb6eb0 | 192 | } |
3b7c7bd7 | 193 | if (nres > 1) { |
03fb6eb0 A |
194 | int oerrno = errno; |
195 | ||
3b7c7bd7 A |
196 | getnameinfo(ai->ai_addr, ai->ai_addrlen, |
197 | paddr, sizeof(paddr), | |
198 | NULL, 0, | |
199 | NI_NUMERICHOST|NI_WITHSCOPEID); | |
03fb6eb0 | 200 | (void)fprintf(stderr, "connect to address %s: ", |
3b7c7bd7 | 201 | paddr); |
03fb6eb0 A |
202 | errno = oerrno; |
203 | perror(0); | |
03fb6eb0 | 204 | } |
3b7c7bd7 A |
205 | if ((ai = ai->ai_next) == NULL) { |
206 | /* refused && timo <= 16 */ | |
207 | struct timespec time_to_sleep, time_remaining; | |
208 | ||
209 | time_to_sleep.tv_sec = timo; | |
210 | time_to_sleep.tv_nsec = 0; | |
211 | (void)nanosleep(&time_to_sleep, &time_remaining); | |
212 | timo *= 2; | |
213 | ai = res; | |
214 | refused = 0; | |
215 | } | |
216 | if (nres > 1) { | |
217 | getnameinfo(ai->ai_addr, ai->ai_addrlen, | |
218 | paddr, sizeof(paddr), | |
219 | NULL, 0, | |
220 | NI_NUMERICHOST|NI_WITHSCOPEID); | |
221 | fprintf(stderr, "Trying %s...\n", paddr); | |
222 | } | |
03fb6eb0 A |
223 | } |
224 | #if 0 | |
225 | /* | |
226 | * try to rresvport() to the same port. This will make rresvport() | |
227 | * fail it's first bind, resulting in it choosing a random port. | |
228 | */ | |
229 | lport--; | |
230 | #endif | |
231 | if (fd2p == 0) { | |
232 | write(s, "", 1); | |
233 | lport = 0; | |
234 | } else { | |
235 | char num[8]; | |
3b7c7bd7 A |
236 | int s2 = rresvport_af(&lport, ai->ai_family), s3; |
237 | int len = ai->ai_addrlen; | |
238 | int nfds; | |
03fb6eb0 A |
239 | |
240 | if (s2 < 0) | |
241 | goto bad; | |
03fb6eb0 A |
242 | listen(s2, 1); |
243 | (void)snprintf(num, sizeof(num), "%d", lport); | |
244 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | |
245 | (void)fprintf(stderr, | |
246 | "rcmd: write (setting up stderr): %s\n", | |
247 | strerror(errno)); | |
248 | (void)close(s2); | |
249 | goto bad; | |
250 | } | |
3b7c7bd7 A |
251 | nfds = max(s, s2)+1; |
252 | if(nfds > FD_SETSIZE) { | |
253 | fprintf(stderr, "rcmd: too many files\n"); | |
254 | (void)close(s2); | |
255 | goto bad; | |
256 | } | |
03fb6eb0 | 257 | again: |
3b7c7bd7 A |
258 | FD_ZERO(&reads); |
259 | FD_SET(s, &reads); | |
260 | FD_SET(s2, &reads); | |
03fb6eb0 | 261 | errno = 0; |
3b7c7bd7 | 262 | if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ |
03fb6eb0 A |
263 | if (errno != 0) |
264 | (void)fprintf(stderr, | |
265 | "rcmd: select (setting up stderr): %s\n", | |
266 | strerror(errno)); | |
267 | else | |
268 | (void)fprintf(stderr, | |
269 | "select: protocol failure in circuit setup\n"); | |
270 | (void)close(s2); | |
271 | goto bad; | |
272 | } | |
273 | s3 = accept(s2, (struct sockaddr *)&from, &len); | |
3b7c7bd7 A |
274 | switch (from.ss_family) { |
275 | case AF_INET: | |
276 | aport = ntohs(((struct sockaddr_in *)&from)->sin_port); | |
277 | break; | |
278 | #ifdef INET6 | |
279 | case AF_INET6: | |
280 | aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); | |
281 | break; | |
282 | #endif | |
283 | default: | |
284 | aport = 0; /* error */ | |
285 | break; | |
286 | } | |
03fb6eb0 A |
287 | /* |
288 | * XXX careful for ftp bounce attacks. If discovered, shut them | |
289 | * down and check for the real auxiliary channel to connect. | |
290 | */ | |
3b7c7bd7 | 291 | if (aport == 20) { |
03fb6eb0 A |
292 | close(s3); |
293 | goto again; | |
294 | } | |
295 | (void)close(s2); | |
296 | if (s3 < 0) { | |
297 | (void)fprintf(stderr, | |
298 | "rcmd: accept: %s\n", strerror(errno)); | |
299 | lport = 0; | |
300 | goto bad; | |
301 | } | |
302 | *fd2p = s3; | |
3b7c7bd7 | 303 | if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { |
03fb6eb0 A |
304 | (void)fprintf(stderr, |
305 | "socket: protocol failure in circuit setup.\n"); | |
306 | goto bad2; | |
307 | } | |
308 | } | |
309 | (void)write(s, locuser, strlen(locuser)+1); | |
310 | (void)write(s, remuser, strlen(remuser)+1); | |
311 | (void)write(s, cmd, strlen(cmd)+1); | |
312 | if (read(s, &c, 1) != 1) { | |
313 | (void)fprintf(stderr, | |
314 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | |
315 | goto bad2; | |
316 | } | |
317 | if (c != 0) { | |
318 | while (read(s, &c, 1) == 1) { | |
319 | (void)write(STDERR_FILENO, &c, 1); | |
320 | if (c == '\n') | |
321 | break; | |
322 | } | |
323 | goto bad2; | |
324 | } | |
325 | sigsetmask(oldmask); | |
3b7c7bd7 | 326 | freeaddrinfo(res); |
03fb6eb0 A |
327 | return (s); |
328 | bad2: | |
329 | if (lport) | |
330 | (void)close(*fd2p); | |
331 | bad: | |
03fb6eb0 A |
332 | (void)close(s); |
333 | sigsetmask(oldmask); | |
3b7c7bd7 | 334 | freeaddrinfo(res); |
03fb6eb0 A |
335 | return (-1); |
336 | } | |
337 | ||
ccd4a120 A |
338 | int |
339 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | |
340 | char **ahost; | |
341 | in_port_t rport; | |
342 | const char *locuser, *remuser, *cmd; | |
343 | int *fd2p; | |
344 | { | |
345 | return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); | |
346 | } | |
347 | ||
03fb6eb0 | 348 | int |
3b7c7bd7 A |
349 | rresvport(port) |
350 | int *port; | |
351 | { | |
352 | return rresvport_af(port, AF_INET); | |
353 | } | |
354 | ||
355 | int | |
356 | rresvport_af(alport, family) | |
357 | int *alport, family; | |
03fb6eb0 | 358 | { |
03fb6eb0 | 359 | int s; |
3b7c7bd7 A |
360 | struct sockaddr_storage ss; |
361 | u_short *sport; | |
362 | ||
363 | memset(&ss, 0, sizeof(ss)); | |
364 | ss.ss_family = family; | |
365 | switch (family) { | |
366 | case AF_INET: | |
367 | ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); | |
368 | sport = &((struct sockaddr_in *)&ss)->sin_port; | |
369 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; | |
370 | break; | |
371 | #ifdef INET6 | |
372 | case AF_INET6: | |
373 | ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); | |
374 | sport = &((struct sockaddr_in6 *)&ss)->sin6_port; | |
375 | ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; | |
376 | break; | |
377 | #endif | |
378 | default: | |
379 | errno = EAFNOSUPPORT; | |
380 | return -1; | |
381 | } | |
03fb6eb0 | 382 | |
3b7c7bd7 | 383 | s = socket(ss.ss_family, SOCK_STREAM, 0); |
03fb6eb0 A |
384 | if (s < 0) |
385 | return (-1); | |
3b7c7bd7 A |
386 | #if 0 /* compat_exact_traditional_rresvport_semantics */ |
387 | sin.sin_port = htons((u_short)*alport); | |
388 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | |
389 | return (s); | |
390 | if (errno != EADDRINUSE) { | |
391 | (void)close(s); | |
392 | return (-1); | |
03fb6eb0 | 393 | } |
3b7c7bd7 A |
394 | #endif |
395 | *sport = 0; | |
396 | if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { | |
03fb6eb0 A |
397 | (void)close(s); |
398 | return (-1); | |
399 | } | |
3b7c7bd7 | 400 | *alport = (int)ntohs(*sport); |
03fb6eb0 A |
401 | return (s); |
402 | } | |
403 | ||
404 | int __check_rhosts_file = 1; | |
405 | char *__rcmd_errstr; | |
406 | ||
3b7c7bd7 A |
407 | /* |
408 | * AF independent extension of iruserok. | |
409 | * | |
410 | * Returns 0 if ok, -1 if not ok. | |
411 | */ | |
412 | int | |
413 | iruserok_sa(ra, rlen, superuser, ruser, luser) | |
414 | const void *ra; | |
415 | int rlen; | |
416 | int superuser; | |
417 | const char *ruser, *luser; | |
03fb6eb0 A |
418 | { |
419 | register char *cp; | |
420 | struct stat sbuf; | |
421 | struct passwd *pwd; | |
422 | FILE *hostf; | |
423 | uid_t uid; | |
424 | int first; | |
425 | char pbuf[MAXPATHLEN]; | |
3b7c7bd7 A |
426 | const struct sockaddr *raddr; |
427 | struct sockaddr_storage ss; | |
428 | ||
429 | /* avoid alignment issue */ | |
430 | if (rlen > sizeof(ss)) | |
431 | return(-1); | |
432 | memcpy(&ss, ra, rlen); | |
433 | raddr = (struct sockaddr *)&ss; | |
03fb6eb0 A |
434 | |
435 | first = 1; | |
436 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | |
437 | again: | |
438 | if (hostf) { | |
3b7c7bd7 | 439 | if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { |
03fb6eb0 A |
440 | (void)fclose(hostf); |
441 | return (0); | |
442 | } | |
443 | (void)fclose(hostf); | |
444 | } | |
445 | if (first == 1 && (__check_rhosts_file || superuser)) { | |
446 | first = 0; | |
447 | if ((pwd = getpwnam(luser)) == NULL) | |
448 | return (-1); | |
449 | (void)strcpy(pbuf, pwd->pw_dir); | |
450 | (void)strcat(pbuf, "/.rhosts"); | |
451 | ||
452 | /* | |
453 | * Change effective uid while opening .rhosts. If root and | |
454 | * reading an NFS mounted file system, can't read files that | |
455 | * are protected read/write owner only. | |
456 | */ | |
457 | uid = geteuid(); | |
458 | (void)seteuid(pwd->pw_uid); | |
459 | hostf = fopen(pbuf, "r"); | |
460 | (void)seteuid(uid); | |
461 | ||
462 | if (hostf == NULL) | |
463 | return (-1); | |
464 | /* | |
465 | * If not a regular file, or is owned by someone other than | |
466 | * user or root or if writeable by anyone but the owner, quit. | |
467 | */ | |
468 | cp = NULL; | |
469 | if (lstat(pbuf, &sbuf) < 0) | |
470 | cp = ".rhosts lstat failed"; | |
471 | else if (!S_ISREG(sbuf.st_mode)) | |
472 | cp = ".rhosts not regular file"; | |
473 | else if (fstat(fileno(hostf), &sbuf) < 0) | |
474 | cp = ".rhosts fstat failed"; | |
475 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | |
476 | cp = "bad .rhosts owner"; | |
477 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | |
478 | cp = ".rhosts writeable by other than owner"; | |
479 | /* If there were any problems, quit. */ | |
480 | if (cp) { | |
481 | __rcmd_errstr = cp; | |
482 | (void)fclose(hostf); | |
483 | return (-1); | |
484 | } | |
485 | goto again; | |
486 | } | |
487 | return (-1); | |
488 | } | |
489 | ||
ccd4a120 A |
490 | int |
491 | ruserok(rhost, superuser, ruser, luser) | |
492 | const char *rhost, *ruser, *luser; | |
493 | int superuser; | |
494 | { | |
495 | struct addrinfo hints, *res, *r; | |
496 | int error; | |
497 | ||
498 | memset(&hints, 0, sizeof(hints)); | |
499 | hints.ai_family = PF_UNSPEC; | |
500 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | |
501 | error = getaddrinfo(rhost, "0", &hints, &res); | |
502 | if (error) | |
503 | return (-1); | |
504 | ||
505 | for (r = res; r; r = r->ai_next) { | |
506 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | |
507 | luser) == 0) { | |
508 | freeaddrinfo(res); | |
509 | return (0); | |
510 | } | |
511 | } | |
512 | freeaddrinfo(res); | |
513 | return (-1); | |
514 | } | |
515 | ||
516 | /* | |
517 | * New .rhosts strategy: We are passed an ip address. We spin through | |
518 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | |
519 | * has ip addresses, we don't have to trust a nameserver. When it | |
520 | * contains hostnames, we spin through the list of addresses the nameserver | |
521 | * gives us and look for a match. | |
522 | * | |
523 | * Returns 0 if ok, -1 if not ok. | |
524 | */ | |
525 | int | |
526 | iruserok(raddr, superuser, ruser, luser) | |
527 | unsigned long raddr; | |
528 | int superuser; | |
529 | const char *ruser, *luser; | |
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 iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, | |
538 | ruser, luser); | |
539 | } | |
540 | ||
03fb6eb0 A |
541 | /* |
542 | * XXX | |
543 | * Don't make static, used by lpd(8). | |
544 | * | |
545 | * Returns 0 if ok, -1 if not ok. | |
546 | */ | |
547 | int | |
3b7c7bd7 | 548 | __ivaliduser(hostf, raddr, luser, ruser) |
03fb6eb0 | 549 | FILE *hostf; |
3b7c7bd7 | 550 | u_int32_t raddr; |
03fb6eb0 A |
551 | const char *luser, *ruser; |
552 | { | |
3b7c7bd7 A |
553 | struct sockaddr_in sin; |
554 | ||
555 | memset(&sin, 0, sizeof(sin)); | |
556 | sin.sin_family = AF_INET; | |
557 | sin.sin_len = sizeof(struct sockaddr_in); | |
558 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | |
559 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | |
560 | luser, ruser); | |
561 | } | |
562 | ||
563 | /* | |
564 | * Returns 0 if ok, -1 if not ok. | |
565 | * | |
566 | * XXX obsolete API. | |
567 | */ | |
568 | int | |
569 | __ivaliduser_af(hostf, raddr, luser, ruser, af, len) | |
570 | FILE *hostf; | |
571 | const void *raddr; | |
572 | const char *luser, *ruser; | |
573 | int af, len; | |
574 | { | |
575 | struct sockaddr *sa = NULL; | |
576 | struct sockaddr_in *sin = NULL; | |
577 | #ifdef INET6 | |
578 | struct sockaddr_in6 *sin6 = NULL; | |
579 | #endif | |
580 | struct sockaddr_storage ss; | |
581 | ||
582 | memset(&ss, 0, sizeof(ss)); | |
583 | switch (af) { | |
584 | case AF_INET: | |
585 | if (len != sizeof(sin->sin_addr)) | |
586 | return -1; | |
587 | sin = (struct sockaddr_in *)&ss; | |
588 | sin->sin_family = AF_INET; | |
589 | sin->sin_len = sizeof(struct sockaddr_in); | |
590 | memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); | |
591 | break; | |
592 | #ifdef INET6 | |
593 | case AF_INET6: | |
594 | if (len != sizeof(sin6->sin6_addr)) | |
595 | return -1; | |
596 | /* you will lose scope info */ | |
597 | sin6 = (struct sockaddr_in6 *)&ss; | |
598 | sin6->sin6_family = AF_INET6; | |
599 | sin6->sin6_len = sizeof(struct sockaddr_in6); | |
600 | memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); | |
601 | break; | |
602 | #endif | |
603 | default: | |
604 | return -1; | |
605 | } | |
03fb6eb0 | 606 | |
3b7c7bd7 A |
607 | sa = (struct sockaddr *)&ss; |
608 | return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); | |
609 | } | |
610 | ||
611 | /* | |
612 | * Returns 0 if ok, -1 if not ok. | |
613 | */ | |
614 | int | |
615 | __ivaliduser_sa(hostf, raddr, salen, luser, ruser) | |
616 | FILE *hostf; | |
617 | const struct sockaddr *raddr; | |
618 | socklen_t salen; | |
619 | const char *luser, *ruser; | |
620 | { | |
621 | register char *user, *p; | |
622 | int ch; | |
623 | char buf[MAXHOSTNAMELEN + 128]; /* host + login */ | |
624 | char hname[MAXHOSTNAMELEN]; | |
625 | /* Presumed guilty until proven innocent. */ | |
626 | int userok = 0, hostok = 0; | |
627 | #ifdef YP | |
628 | char *ypdomain; | |
629 | ||
630 | if (yp_get_default_domain(&ypdomain)) | |
631 | ypdomain = NULL; | |
632 | #else | |
633 | #define ypdomain NULL | |
634 | #endif | |
ccd4a120 A |
635 | /* |
636 | * We try to get the hostname back for netgroup matching. | |
637 | * However, the .rosts file may contain IP addresses as well | |
638 | * as names, so a name is not always required. | |
639 | */ | |
640 | getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, NI_NAMEREQD); | |
03fb6eb0 | 641 | |
3b7c7bd7 | 642 | while (fgets(buf, sizeof(buf), hostf)) { |
03fb6eb0 | 643 | p = buf; |
3b7c7bd7 A |
644 | /* Skip lines that are too long. */ |
645 | if (strchr(p, '\n') == NULL) { | |
646 | while ((ch = getc(hostf)) != '\n' && ch != EOF); | |
03fb6eb0 | 647 | continue; |
03fb6eb0 | 648 | } |
3b7c7bd7 A |
649 | if (*p == '\n' || *p == '#') { |
650 | /* comment... */ | |
03fb6eb0 | 651 | continue; |
3b7c7bd7 A |
652 | } |
653 | while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { | |
654 | *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; | |
655 | p++; | |
656 | } | |
03fb6eb0 A |
657 | if (*p == ' ' || *p == '\t') { |
658 | *p++ = '\0'; | |
3b7c7bd7 | 659 | while (*p == ' ' || *p == '\t') |
03fb6eb0 | 660 | p++; |
03fb6eb0 A |
661 | user = p; |
662 | while (*p != '\n' && *p != ' ' && | |
3b7c7bd7 | 663 | *p != '\t' && *p != '\0') |
03fb6eb0 | 664 | p++; |
03fb6eb0 A |
665 | } else |
666 | user = p; | |
667 | *p = '\0'; | |
03fb6eb0 | 668 | /* |
3b7c7bd7 A |
669 | * Do +/- and +@/-@ checking. This looks really nasty, |
670 | * but it matches SunOS's behavior so far as I can tell. | |
03fb6eb0 | 671 | */ |
3b7c7bd7 A |
672 | switch(buf[0]) { |
673 | case '+': | |
674 | if (!buf[1]) { /* '+' matches all hosts */ | |
03fb6eb0 A |
675 | hostok = 1; |
676 | break; | |
03fb6eb0 | 677 | } |
3b7c7bd7 A |
678 | if (buf[1] == '@') /* match a host by netgroup */ |
679 | hostok = innetgr((char *)&buf[2], | |
680 | (char *)&hname, NULL, ypdomain); | |
681 | else /* match a host by addr */ | |
682 | hostok = __icheckhost(raddr, salen, | |
683 | (char *)&buf[1]); | |
684 | break; | |
685 | case '-': /* reject '-' hosts and all their users */ | |
686 | if (buf[1] == '@') { | |
687 | if (innetgr((char *)&buf[2], | |
688 | (char *)&hname, NULL, ypdomain)) | |
689 | return(-1); | |
690 | } else { | |
691 | if (__icheckhost(raddr, salen, | |
692 | (char *)&buf[1])) | |
693 | return(-1); | |
03fb6eb0 | 694 | } |
3b7c7bd7 A |
695 | break; |
696 | default: /* if no '+' or '-', do a simple match */ | |
697 | hostok = __icheckhost(raddr, salen, buf); | |
698 | break; | |
699 | } | |
700 | switch(*user) { | |
701 | case '+': | |
702 | if (!*(user+1)) { /* '+' matches all users */ | |
03fb6eb0 A |
703 | userok = 1; |
704 | break; | |
03fb6eb0 | 705 | } |
3b7c7bd7 A |
706 | if (*(user+1) == '@') /* match a user by netgroup */ |
707 | userok = innetgr(user+2, NULL, ruser, ypdomain); | |
708 | else /* match a user by direct specification */ | |
709 | userok = !(strcmp(ruser, user+1)); | |
710 | break; | |
711 | case '-': /* if we matched a hostname, */ | |
712 | if (hostok) { /* check for user field rejections */ | |
713 | if (!*(user+1)) | |
714 | return(-1); | |
715 | if (*(user+1) == '@') { | |
716 | if (innetgr(user+2, NULL, | |
717 | ruser, ypdomain)) | |
718 | return(-1); | |
719 | } else { | |
720 | if (!strcmp(ruser, user+1)) | |
721 | return(-1); | |
722 | } | |
03fb6eb0 | 723 | } |
3b7c7bd7 A |
724 | break; |
725 | default: /* no rejections: try to match the user */ | |
726 | if (hostok) | |
727 | userok = !(strcmp(ruser,*user ? user : luser)); | |
728 | break; | |
729 | } | |
730 | if (hostok && userok) | |
731 | return(0); | |
03fb6eb0 | 732 | } |
03fb6eb0 A |
733 | return (-1); |
734 | } | |
735 | ||
736 | /* | |
3b7c7bd7 A |
737 | * Returns "true" if match, 0 if no match. |
738 | * | |
739 | * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion | |
740 | * if af == AF_INET6. | |
03fb6eb0 A |
741 | */ |
742 | static int | |
3b7c7bd7 A |
743 | __icheckhost(raddr, salen, lhost) |
744 | const struct sockaddr *raddr; | |
745 | socklen_t salen; | |
746 | const char *lhost; | |
03fb6eb0 | 747 | { |
3b7c7bd7 A |
748 | struct sockaddr_in sin; |
749 | struct sockaddr_in6 *sin6; | |
750 | struct addrinfo hints, *res, *r; | |
751 | int error; | |
752 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | |
753 | ||
754 | if (raddr->sa_family == AF_INET6) { | |
755 | sin6 = (struct sockaddr_in6 *)raddr; | |
756 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { | |
757 | memset(&sin, 0, sizeof(sin)); | |
758 | sin.sin_family = AF_INET; | |
759 | sin.sin_len = sizeof(struct sockaddr_in); | |
760 | memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], | |
761 | sizeof(sin.sin_addr)); | |
762 | raddr = (struct sockaddr *)&sin; | |
763 | salen = sin.sin_len; | |
764 | } | |
03fb6eb0 A |
765 | } |
766 | ||
3b7c7bd7 A |
767 | h1[0] = '\0'; |
768 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | |
769 | NI_NUMERICHOST | NI_WITHSCOPEID) != 0) | |
770 | return (0); | |
771 | ||
772 | /* Resolve laddr into sockaddr */ | |
773 | memset(&hints, 0, sizeof(hints)); | |
774 | hints.ai_family = raddr->sa_family; | |
775 | hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ | |
776 | res = NULL; | |
777 | error = getaddrinfo(lhost, "0", &hints, &res); | |
778 | if (error) | |
779 | return (0); | |
780 | ||
781 | for (r = res; r ; r = r->ai_next) { | |
782 | h2[0] = '\0'; | |
783 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | |
784 | NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) | |
785 | continue; | |
786 | if (strcmp(h1, h2) == 0) { | |
787 | freeaddrinfo(res); | |
788 | return (1); | |
789 | } | |
790 | } | |
03fb6eb0 | 791 | |
3b7c7bd7 A |
792 | /* No match. */ |
793 | freeaddrinfo(res); | |
794 | return (0); | |
03fb6eb0 | 795 | } |