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.3.18.1 2005/04/27 05:00:54 sra Exp $";
24 * dns_gr.c --- this file contains the functions for accessing
25 * group information from Hesiod.
28 #include "port_before.h"
31 static int __bind_irs_gr_unneeded;
34 #include <sys/param.h>
35 #include <sys/types.h>
43 #include <sys/types.h>
44 #include <netinet/in.h>
45 #include <arpa/nameser.h>
48 #include <isc/memcluster.h>
52 #include "port_after.h"
62 * This is our private accessor data. It has a shared hesiod context.
66 * Need space to store the entries read from the group file.
67 * The members list also needs space per member, and the
68 * strings making up the user names must be allocated
69 * somewhere. Rather than doing lots of small allocations,
70 * we keep one buffer and resize it as needed.
73 size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */
80 static struct group * gr_next(struct irs_gr *);
81 static struct group * gr_byname(struct irs_gr *, const char *);
82 static struct group * gr_bygid(struct irs_gr *, gid_t);
83 static void gr_rewind(struct irs_gr *);
84 static void gr_close(struct irs_gr *);
85 static int gr_list(struct irs_gr *, const char *,
86 gid_t, gid_t *, int *);
87 static void gr_minimize(struct irs_gr *);
88 static struct __res_state * gr_res_get(struct irs_gr *);
89 static void gr_res_set(struct irs_gr *,
93 static struct group * get_hes_group(struct irs_gr *this,
100 irs_dns_gr(struct irs_acc *this) {
101 struct dns_p *dns = (struct dns_p *)this->private;
105 if (!dns || !dns->hes_ctx) {
109 if (!(pvt = memget(sizeof *pvt))) {
113 memset(pvt, 0, sizeof *pvt);
115 if (!(gr = memget(sizeof *gr))) {
116 memput(pvt, sizeof *pvt);
120 memset(gr, 0x5e, sizeof *gr);
123 gr->byname = gr_byname;
124 gr->bygid = gr_bygid;
125 gr->rewind = gr_rewind;
126 gr->close = gr_close;
128 gr->minimize = gr_minimize;
129 gr->res_get = gr_res_get;
130 gr->res_set = gr_res_set;
137 gr_close(struct irs_gr *this) {
138 struct pvt *pvt = (struct pvt *)this->private;
140 if (pvt->group.gr_mem)
141 free(pvt->group.gr_mem);
144 memput(pvt, sizeof *pvt);
145 memput(this, sizeof *this);
148 static struct group *
149 gr_next(struct irs_gr *this) {
156 static struct group *
157 gr_byname(struct irs_gr *this, const char *name) {
158 return (get_hes_group(this, name, "group"));
161 static struct group *
162 gr_bygid(struct irs_gr *this, gid_t gid) {
165 sprintf(name, "%ld", (long)gid);
166 return (get_hes_group(this, name, "gid"));
170 gr_rewind(struct irs_gr *this) {
178 gr_list(struct irs_gr *this, const char *name,
179 gid_t basegid, gid_t *groups, int *ngroups)
187 /* There's some way to do this in Hesiod. */
192 gr_minimize(struct irs_gr *this) {
200 static struct group *
201 get_hes_group(struct irs_gr *this, const char *name, const char *type) {
202 struct pvt *pvt = (struct pvt *)this->private;
203 char **hes_list, *cp, **new;
204 size_t num_members = 0;
207 hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type);
212 * Copy the returned hesiod string into storage space.
216 pvt->membuf = strdup(*hes_list);
217 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
220 pvt->group.gr_name = cp;
221 if (!(cp = strchr(cp, ':')))
225 pvt->group.gr_passwd = cp;
226 if (!(cp = strchr(cp, ':')))
231 t = strtoul(cp, NULL, 10);
234 pvt->group.gr_gid = (gid_t) t;
235 if (!(cp = strchr(cp, ':')))
240 * Parse the members out.
243 if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
245 new = realloc(pvt->group.gr_mem,
246 pvt->nmemb * sizeof(char *));
249 pvt->group.gr_mem = new;
251 pvt->group.gr_mem[num_members++] = cp;
252 if (!(cp = strchr(cp, ',')))
256 if (!pvt->group.gr_mem) {
257 pvt->group.gr_mem = malloc(sizeof(char*));
258 if (!pvt->group.gr_mem)
261 pvt->group.gr_mem[num_members] = NULL;
263 return (&pvt->group);
266 if (pvt->group.gr_mem) {
267 free(pvt->group.gr_mem);
268 pvt->group.gr_mem = NULL;
277 static struct __res_state *
278 gr_res_get(struct irs_gr *this) {
279 struct pvt *pvt = (struct pvt *)this->private;
280 struct dns_p *dns = pvt->dns;
282 return (__hesiod_res_get(dns->hes_ctx));
286 gr_res_set(struct irs_gr *this, struct __res_state * res,
287 void (*free_res)(void *)) {
288 struct pvt *pvt = (struct pvt *)this->private;
289 struct dns_p *dns = pvt->dns;
291 __hesiod_res_set(dns->hes_ctx, res, free_res);
294 #endif /* WANT_IRS_GR */