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