Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / util.c
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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: util.c,v 1.1.206.1 2004/03/09 08:33:39 marka Exp $";
20 #endif
21
22 #include "port_before.h"
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <resolv.h>
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include <irs.h>
37
38 #include "port_after.h"
39
40 #include "irs_p.h"
41
42 #ifdef SPRINTF_CHAR
43 # define SPRINTF(x) strlen(sprintf/**/x)
44 #else
45 # define SPRINTF(x) sprintf x
46 #endif
47
48 void
49 map_v4v6_address(const char *src, char *dst) {
50         u_char *p = (u_char *)dst;
51         char tmp[NS_INADDRSZ];
52         int i;
53
54         /* Stash a temporary copy so our caller can update in place. */
55         memcpy(tmp, src, NS_INADDRSZ);
56         /* Mark this ipv6 addr as a mapped ipv4. */
57         for (i = 0; i < 10; i++)
58                 *p++ = 0x00;
59         *p++ = 0xff;
60         *p++ = 0xff;
61         /* Retrieve the saved copy and we're done. */
62         memcpy((void*)p, tmp, NS_INADDRSZ);
63 }
64
65 int
66 make_group_list(struct irs_gr *this, const char *name,
67         gid_t basegid, gid_t *groups, int *ngroups)
68 {
69         struct group *grp;
70         int i, ng;
71         int ret, maxgroups;
72
73         ret = -1;
74         ng = 0;
75         maxgroups = *ngroups;
76         /*
77          * When installing primary group, duplicate it;
78          * the first element of groups is the effective gid
79          * and will be overwritten when a setgid file is executed.
80          */
81         if (ng >= maxgroups)
82                 goto done;
83         groups[ng++] = basegid;
84         if (ng >= maxgroups)
85                 goto done;
86         groups[ng++] = basegid;
87         /*
88          * Scan the group file to find additional groups.
89          */
90         (*this->rewind)(this);
91         while ((grp = (*this->next)(this)) != NULL) {
92                 if ((gid_t)grp->gr_gid == basegid)
93                         continue;
94                 for (i = 0; grp->gr_mem[i]; i++) {
95                         if (!strcmp(grp->gr_mem[i], name)) {
96                                 if (ng >= maxgroups)
97                                         goto done;
98                                 groups[ng++] = grp->gr_gid;
99                                 break;
100                         }
101                 }
102         }
103         ret = 0;
104  done:
105         *ngroups = ng;
106         return (ret);
107 }