2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
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 $";
23 * dns_gr.c --- this file contains the functions for accessing
24 * group information from Hesiod.
27 #include "port_before.h"
30 static int __bind_irs_gr_unneeded;
33 #include <sys/param.h>
34 #include <sys/types.h>
42 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <arpa/nameser.h>
47 #include <isc/memcluster.h>
51 #include "port_after.h"
61 * This is our private accessor data. It has a shared hesiod context.
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.
72 size_t nmemb; /* Malloc'd max index of gr_mem[]. */
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 *,
92 static struct group * get_hes_group(struct irs_gr *this,
99 irs_dns_gr(struct irs_acc *this) {
100 struct dns_p *dns = (struct dns_p *)this->private;
104 if (!dns || !dns->hes_ctx) {
108 if (!(pvt = memget(sizeof *pvt))) {
112 memset(pvt, 0, sizeof *pvt);
114 if (!(gr = memget(sizeof *gr))) {
115 memput(pvt, sizeof *pvt);
119 memset(gr, 0x5e, sizeof *gr);
122 gr->byname = gr_byname;
123 gr->bygid = gr_bygid;
124 gr->rewind = gr_rewind;
125 gr->close = gr_close;
127 gr->minimize = gr_minimize;
128 gr->res_get = gr_res_get;
129 gr->res_set = gr_res_set;
136 gr_close(struct irs_gr *this) {
137 struct pvt *pvt = (struct pvt *)this->private;
139 if (pvt->group.gr_mem)
140 free(pvt->group.gr_mem);
143 memput(pvt, sizeof *pvt);
144 memput(this, sizeof *this);
147 static struct group *
148 gr_next(struct irs_gr *this) {
155 static struct group *
156 gr_byname(struct irs_gr *this, const char *name) {
157 return (get_hes_group(this, name, "group"));
160 static struct group *
161 gr_bygid(struct irs_gr *this, gid_t gid) {
164 sprintf(name, "%ld", (long)gid);
165 return (get_hes_group(this, name, "gid"));
169 gr_rewind(struct irs_gr *this) {
177 gr_list(struct irs_gr *this, const char *name,
178 gid_t basegid, gid_t *groups, int *ngroups)
186 /* There's some way to do this in Hesiod. */
191 gr_minimize(struct irs_gr *this) {
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;
206 hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type);
211 * Copy the returned hesiod string into storage space.
215 pvt->membuf = strdup(*hes_list);
216 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
219 pvt->group.gr_name = cp;
220 if (!(cp = strchr(cp, ':')))
224 pvt->group.gr_passwd = cp;
225 if (!(cp = strchr(cp, ':')))
230 t = strtoul(cp, NULL, 10);
233 pvt->group.gr_gid = (gid_t) t;
234 if (!(cp = strchr(cp, ':')))
239 * Parse the members out.
242 if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
244 new = realloc(pvt->group.gr_mem,
245 pvt->nmemb * sizeof(char *));
248 pvt->group.gr_mem = new;
250 pvt->group.gr_mem[num_members++] = cp;
251 if (!(cp = strchr(cp, ',')))
255 if (!pvt->group.gr_mem) {
256 pvt->group.gr_mem = malloc(sizeof(char*));
257 if (!pvt->group.gr_mem)
260 pvt->group.gr_mem[num_members] = NULL;
262 return (&pvt->group);
265 if (pvt->group.gr_mem) {
266 free(pvt->group.gr_mem);
267 pvt->group.gr_mem = NULL;
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;
281 return (__hesiod_res_get(dns->hes_ctx));
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;
290 __hesiod_res_set(dns->hes_ctx, res, free_res);
293 #endif /* WANT_IRS_GR */