xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/util.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
25*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: util.c,v 1.12 2001/05/29 05:49:21 marka Exp $";
26*7c478bd9Sstevel@tonic-gate #endif
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "port_before.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
32*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
33*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
34*7c478bd9Sstevel@tonic-gate #include <resolv.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <irs.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "port_after.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include "irs_p.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
49*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
50*7c478bd9Sstevel@tonic-gate #else
51*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
52*7c478bd9Sstevel@tonic-gate #endif
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate void
55*7c478bd9Sstevel@tonic-gate map_v4v6_address(const char *src, char *dst) {
56*7c478bd9Sstevel@tonic-gate 	u_char *p = (u_char *)dst;
57*7c478bd9Sstevel@tonic-gate 	char tmp[NS_INADDRSZ];
58*7c478bd9Sstevel@tonic-gate 	int i;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	/* Stash a temporary copy so our caller can update in place. */
61*7c478bd9Sstevel@tonic-gate 	memcpy(tmp, src, NS_INADDRSZ);
62*7c478bd9Sstevel@tonic-gate 	/* Mark this ipv6 addr as a mapped ipv4. */
63*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 10; i++)
64*7c478bd9Sstevel@tonic-gate 		*p++ = 0x00;
65*7c478bd9Sstevel@tonic-gate 	*p++ = 0xff;
66*7c478bd9Sstevel@tonic-gate 	*p++ = 0xff;
67*7c478bd9Sstevel@tonic-gate 	/* Retrieve the saved copy and we're done. */
68*7c478bd9Sstevel@tonic-gate 	memcpy((void*)p, tmp, NS_INADDRSZ);
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int
72*7c478bd9Sstevel@tonic-gate make_group_list(struct irs_gr *this, const char *name,
73*7c478bd9Sstevel@tonic-gate 	gid_t basegid, gid_t *groups, int *ngroups)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	struct group *grp;
76*7c478bd9Sstevel@tonic-gate 	int i, ng;
77*7c478bd9Sstevel@tonic-gate 	int ret, maxgroups;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	ret = -1;
80*7c478bd9Sstevel@tonic-gate 	ng = 0;
81*7c478bd9Sstevel@tonic-gate 	maxgroups = *ngroups;
82*7c478bd9Sstevel@tonic-gate 	/*
83*7c478bd9Sstevel@tonic-gate 	 * When installing primary group, duplicate it;
84*7c478bd9Sstevel@tonic-gate 	 * the first element of groups is the effective gid
85*7c478bd9Sstevel@tonic-gate 	 * and will be overwritten when a setgid file is executed.
86*7c478bd9Sstevel@tonic-gate 	 */
87*7c478bd9Sstevel@tonic-gate 	if (ng >= maxgroups)
88*7c478bd9Sstevel@tonic-gate 		goto done;
89*7c478bd9Sstevel@tonic-gate 	groups[ng++] = basegid;
90*7c478bd9Sstevel@tonic-gate 	if (ng >= maxgroups)
91*7c478bd9Sstevel@tonic-gate 		goto done;
92*7c478bd9Sstevel@tonic-gate 	groups[ng++] = basegid;
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Scan the group file to find additional groups.
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	(*this->rewind)(this);
97*7c478bd9Sstevel@tonic-gate 	while ((grp = (*this->next)(this)) != NULL) {
98*7c478bd9Sstevel@tonic-gate 		if ((gid_t)grp->gr_gid == basegid)
99*7c478bd9Sstevel@tonic-gate 			continue;
100*7c478bd9Sstevel@tonic-gate 		for (i = 0; grp->gr_mem[i]; i++) {
101*7c478bd9Sstevel@tonic-gate 			if (!strcmp(grp->gr_mem[i], name)) {
102*7c478bd9Sstevel@tonic-gate 				if (ng >= maxgroups)
103*7c478bd9Sstevel@tonic-gate 					goto done;
104*7c478bd9Sstevel@tonic-gate 				groups[ng++] = grp->gr_gid;
105*7c478bd9Sstevel@tonic-gate 				break;
106*7c478bd9Sstevel@tonic-gate 			}
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	ret = 0;
110*7c478bd9Sstevel@tonic-gate  done:
111*7c478bd9Sstevel@tonic-gate 	*ngroups = ng;
112*7c478bd9Sstevel@tonic-gate 	return (ret);
113*7c478bd9Sstevel@tonic-gate }
114