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