Merge branch 'vendor/GMP' into gcc441
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / dns_gr.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: dns_gr.c,v 1.1.2.1.4.1 2004/03/09 08:33:34 marka Exp $";
20 #endif
21
22 /*
23  * dns_gr.c --- this file contains the functions for accessing
24  *      group information from Hesiod.
25  */
26
27 #include "port_before.h"
28
29 #ifndef WANT_IRS_GR
30 static int __bind_irs_gr_unneeded;
31 #else
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <unistd.h>
41
42 #include <sys/types.h>
43 #include <netinet/in.h> 
44 #include <arpa/nameser.h>
45 #include <resolv.h>
46
47 #include <isc/memcluster.h>
48
49 #include <irs.h>
50
51 #include "port_after.h"
52
53 #include "irs_p.h"
54 #include "hesiod.h"
55 #include "dns_p.h"
56
57 /* Types. */
58
59 struct pvt {
60         /*
61          * This is our private accessor data.  It has a shared hesiod context.
62          */
63         struct dns_p *  dns;
64         /*
65          * Need space to store the entries read from the group file.
66          * The members list also needs space per member, and the
67          * strings making up the user names must be allocated
68          * somewhere.  Rather than doing lots of small allocations,
69          * we keep one buffer and resize it as needed.
70          */
71         struct group    group;
72         size_t          nmemb;          /* Malloc'd max index of gr_mem[]. */
73         char *          membuf;
74         size_t          membufsize;
75 };
76
77 /* Forward. */
78
79 static struct group *   gr_next(struct irs_gr *);
80 static struct group *   gr_byname(struct irs_gr *, const char *);
81 static struct group *   gr_bygid(struct irs_gr *, gid_t);
82 static void             gr_rewind(struct irs_gr *);
83 static void             gr_close(struct irs_gr *);
84 static int              gr_list(struct irs_gr *, const char *,
85                                 gid_t, gid_t *, int *);
86 static void             gr_minimize(struct irs_gr *);
87 static struct __res_state * gr_res_get(struct irs_gr *);
88 static void             gr_res_set(struct irs_gr *,
89                                    struct __res_state *,
90                                    void (*)(void *));
91
92 static struct group *   get_hes_group(struct irs_gr *this,
93                                       const char *name,
94                                       const char *type);
95
96 /* Public. */
97
98 struct irs_gr *
99 irs_dns_gr(struct irs_acc *this) {
100         struct dns_p *dns = (struct dns_p *)this->private;
101         struct irs_gr *gr;
102         struct pvt *pvt;
103
104         if (!dns || !dns->hes_ctx) {
105                 errno = ENODEV;
106                 return (NULL);
107         }
108         if (!(pvt = memget(sizeof *pvt))) {
109                 errno = ENOMEM;
110                 return (NULL);
111         }
112         memset(pvt, 0, sizeof *pvt);
113         pvt->dns = dns;
114         if (!(gr = memget(sizeof *gr))) {
115                 memput(pvt, sizeof *pvt);
116                 errno = ENOMEM;
117                 return (NULL);
118         }
119         memset(gr, 0x5e, sizeof *gr);
120         gr->private = pvt;
121         gr->next = gr_next;
122         gr->byname = gr_byname;
123         gr->bygid = gr_bygid;
124         gr->rewind = gr_rewind;
125         gr->close = gr_close;
126         gr->list = gr_list;
127         gr->minimize = gr_minimize;
128         gr->res_get = gr_res_get;
129         gr->res_set = gr_res_set;
130         return (gr);
131 }
132
133 /* methods */
134
135 static void
136 gr_close(struct irs_gr *this) {
137         struct pvt *pvt = (struct pvt *)this->private;
138
139         if (pvt->group.gr_mem)
140                 free(pvt->group.gr_mem);
141         if (pvt->membuf)
142                 free(pvt->membuf);
143         memput(pvt, sizeof *pvt);
144         memput(this, sizeof *this);
145 }
146
147 static struct group *
148 gr_next(struct irs_gr *this) {
149
150         UNUSED(this);
151
152         return (NULL);
153 }
154
155 static struct group *
156 gr_byname(struct irs_gr *this, const char *name) {
157         return (get_hes_group(this, name, "group"));
158 }
159
160 static struct group *
161 gr_bygid(struct irs_gr *this, gid_t gid) {
162         char name[32];
163
164         sprintf(name, "%ld", (long)gid);
165         return (get_hes_group(this, name, "gid"));
166 }
167
168 static void
169 gr_rewind(struct irs_gr *this) {
170
171         UNUSED(this);
172
173         /* NOOP */
174 }
175
176 static int
177 gr_list(struct irs_gr *this, const char *name,
178         gid_t basegid, gid_t *groups, int *ngroups)
179 {
180         UNUSED(this);
181         UNUSED(name);
182         UNUSED(basegid);
183         UNUSED(groups);
184
185         *ngroups = 0;
186         /* There's some way to do this in Hesiod. */
187         return (-1);
188 }
189
190 static void
191 gr_minimize(struct irs_gr *this) {
192
193         UNUSED(this);
194         /* NOOP */
195 }
196
197 /* Private. */
198
199 static struct group *
200 get_hes_group(struct irs_gr *this, const char *name, const char *type) {
201         struct pvt *pvt = (struct pvt *)this->private;
202         char **hes_list, *cp, **new;
203         size_t num_members = 0;
204         u_long t;
205
206         hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type);
207         if (!hes_list)
208                 return (NULL);
209
210         /*
211          * Copy the returned hesiod string into storage space.
212          */
213         if (pvt->membuf)
214                 free(pvt->membuf);
215         pvt->membuf = strdup(*hes_list);
216         hesiod_free_list(pvt->dns->hes_ctx, hes_list);
217
218         cp = pvt->membuf;
219         pvt->group.gr_name = cp;
220         if (!(cp = strchr(cp, ':')))
221                 goto cleanup;
222         *cp++ = '\0';
223         
224         pvt->group.gr_passwd = cp;
225         if (!(cp = strchr(cp, ':')))
226                 goto cleanup;
227         *cp++ = '\0';
228
229         errno = 0;
230         t = strtoul(cp, NULL, 10);
231         if (errno == ERANGE)
232                 goto cleanup;
233         pvt->group.gr_gid = (gid_t) t;
234         if (!(cp = strchr(cp, ':')))
235                 goto cleanup;
236         cp++;
237
238         /*
239          * Parse the members out.
240          */
241         while (*cp) {
242                 if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
243                         pvt->nmemb += 10;
244                         new = realloc(pvt->group.gr_mem,
245                                       pvt->nmemb * sizeof(char *));
246                         if (new == NULL)
247                                 goto cleanup;
248                         pvt->group.gr_mem = new;
249                 }
250                 pvt->group.gr_mem[num_members++] = cp;
251                 if (!(cp = strchr(cp, ',')))
252                         break;
253                 *cp++ = '\0';
254         }
255         if (!pvt->group.gr_mem) {
256                 pvt->group.gr_mem = malloc(sizeof(char*));
257                 if (!pvt->group.gr_mem)
258                         goto cleanup;
259         }
260         pvt->group.gr_mem[num_members] = NULL;
261         
262         return (&pvt->group);
263         
264  cleanup:       
265         if (pvt->group.gr_mem) {
266                 free(pvt->group.gr_mem);
267                 pvt->group.gr_mem = NULL;
268         }
269         if (pvt->membuf) {
270                 free(pvt->membuf);
271                 pvt->membuf = NULL;
272         }
273         return (NULL);
274 }
275
276 static struct __res_state *
277 gr_res_get(struct irs_gr *this) {
278         struct pvt *pvt = (struct pvt *)this->private;
279         struct dns_p *dns = pvt->dns;
280
281         return (__hesiod_res_get(dns->hes_ctx));
282 }
283
284 static void
285 gr_res_set(struct irs_gr *this, struct __res_state * res,
286            void (*free_res)(void *)) {
287         struct pvt *pvt = (struct pvt *)this->private;
288         struct dns_p *dns = pvt->dns;
289
290         __hesiod_res_set(dns->hes_ctx, res, free_res);
291 }
292
293 #endif /* WANT_IRS_GR */