xref: /netbsd-src/external/bsd/libbind/dist/irs/dns_gr.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: dns_gr.c,v 1.1.1.2 2012/09/09 16:07:52 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (c) 1996-1999 by Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: dns_gr.c,v 1.4 2005/04/27 04:56:21 sra Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos /*! \file
25b5677b36Schristos  * \brief
26b5677b36Schristos  * dns_gr.c --- this file contains the functions for accessing
27b5677b36Schristos  * 	group information from Hesiod.
28b5677b36Schristos  */
29b5677b36Schristos 
30b5677b36Schristos #include "port_before.h"
31b5677b36Schristos 
32b5677b36Schristos #ifndef WANT_IRS_GR
33b5677b36Schristos static int __bind_irs_gr_unneeded;
34b5677b36Schristos #else
35b5677b36Schristos 
36b5677b36Schristos #include <sys/param.h>
37b5677b36Schristos #include <sys/types.h>
38b5677b36Schristos 
39b5677b36Schristos #include <stdio.h>
40b5677b36Schristos #include <stdlib.h>
41b5677b36Schristos #include <string.h>
42b5677b36Schristos #include <errno.h>
43b5677b36Schristos #include <unistd.h>
44b5677b36Schristos 
45b5677b36Schristos #include <sys/types.h>
46b5677b36Schristos #include <netinet/in.h>
47b5677b36Schristos #include <arpa/nameser.h>
48b5677b36Schristos #include <resolv.h>
49b5677b36Schristos 
50b5677b36Schristos #include <isc/memcluster.h>
51b5677b36Schristos 
52b5677b36Schristos #include <irs.h>
53b5677b36Schristos 
54b5677b36Schristos #include "port_after.h"
55b5677b36Schristos 
56b5677b36Schristos #include "irs_p.h"
57b5677b36Schristos #include "hesiod.h"
58b5677b36Schristos #include "dns_p.h"
59b5677b36Schristos 
60b5677b36Schristos /* Types. */
61b5677b36Schristos 
62b5677b36Schristos struct pvt {
63b5677b36Schristos 	/*
64b5677b36Schristos 	 * This is our private accessor data.  It has a shared hesiod context.
65b5677b36Schristos 	 */
66b5677b36Schristos 	struct dns_p *	dns;
67b5677b36Schristos 	/*
68b5677b36Schristos 	 * Need space to store the entries read from the group file.
69b5677b36Schristos 	 * The members list also needs space per member, and the
70b5677b36Schristos 	 * strings making up the user names must be allocated
71b5677b36Schristos 	 * somewhere.  Rather than doing lots of small allocations,
72b5677b36Schristos 	 * we keep one buffer and resize it as needed.
73b5677b36Schristos 	 */
74b5677b36Schristos 	struct group	group;
75b5677b36Schristos 	size_t		nmemb;		/*%< Malloc'd max index of gr_mem[]. */
76b5677b36Schristos 	char *		membuf;
77b5677b36Schristos 	size_t		membufsize;
78b5677b36Schristos };
79b5677b36Schristos 
80b5677b36Schristos /* Forward. */
81b5677b36Schristos 
82b5677b36Schristos static struct group *	gr_next(struct irs_gr *);
83b5677b36Schristos static struct group *	gr_byname(struct irs_gr *, const char *);
84b5677b36Schristos static struct group *	gr_bygid(struct irs_gr *, gid_t);
85b5677b36Schristos static void		gr_rewind(struct irs_gr *);
86b5677b36Schristos static void		gr_close(struct irs_gr *);
87b5677b36Schristos static int		gr_list(struct irs_gr *, const char *,
88b5677b36Schristos 				gid_t, gid_t *, int *);
89b5677b36Schristos static void		gr_minimize(struct irs_gr *);
90b5677b36Schristos static struct __res_state * gr_res_get(struct irs_gr *);
91b5677b36Schristos static void		gr_res_set(struct irs_gr *,
92b5677b36Schristos 				   struct __res_state *,
93b5677b36Schristos 				   void (*)(void *));
94b5677b36Schristos 
95b5677b36Schristos static struct group *	get_hes_group(struct irs_gr *this,
96b5677b36Schristos 				      const char *name,
97b5677b36Schristos 				      const char *type);
98b5677b36Schristos 
99b5677b36Schristos /* Public. */
100b5677b36Schristos 
101b5677b36Schristos struct irs_gr *
irs_dns_gr(struct irs_acc * this)102b5677b36Schristos irs_dns_gr(struct irs_acc *this) {
103b5677b36Schristos 	struct dns_p *dns = (struct dns_p *)this->private;
104b5677b36Schristos 	struct irs_gr *gr;
105b5677b36Schristos 	struct pvt *pvt;
106b5677b36Schristos 
107b5677b36Schristos 	if (!dns || !dns->hes_ctx) {
108b5677b36Schristos 		errno = ENODEV;
109b5677b36Schristos 		return (NULL);
110b5677b36Schristos 	}
111b5677b36Schristos 	if (!(pvt = memget(sizeof *pvt))) {
112b5677b36Schristos 		errno = ENOMEM;
113b5677b36Schristos 		return (NULL);
114b5677b36Schristos 	}
115b5677b36Schristos 	memset(pvt, 0, sizeof *pvt);
116b5677b36Schristos 	pvt->dns = dns;
117b5677b36Schristos 	if (!(gr = memget(sizeof *gr))) {
118b5677b36Schristos 		memput(pvt, sizeof *pvt);
119b5677b36Schristos 		errno = ENOMEM;
120b5677b36Schristos 		return (NULL);
121b5677b36Schristos 	}
122b5677b36Schristos 	memset(gr, 0x5e, sizeof *gr);
123b5677b36Schristos 	gr->private = pvt;
124b5677b36Schristos 	gr->next = gr_next;
125b5677b36Schristos 	gr->byname = gr_byname;
126b5677b36Schristos 	gr->bygid = gr_bygid;
127b5677b36Schristos 	gr->rewind = gr_rewind;
128b5677b36Schristos 	gr->close = gr_close;
129b5677b36Schristos 	gr->list = gr_list;
130b5677b36Schristos 	gr->minimize = gr_minimize;
131b5677b36Schristos 	gr->res_get = gr_res_get;
132b5677b36Schristos 	gr->res_set = gr_res_set;
133b5677b36Schristos 	return (gr);
134b5677b36Schristos }
135b5677b36Schristos 
136b5677b36Schristos /* methods */
137b5677b36Schristos 
138b5677b36Schristos static void
gr_close(struct irs_gr * this)139b5677b36Schristos gr_close(struct irs_gr *this) {
140b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
141b5677b36Schristos 
142b5677b36Schristos 	if (pvt->group.gr_mem)
143b5677b36Schristos 		free(pvt->group.gr_mem);
144b5677b36Schristos 	if (pvt->membuf)
145b5677b36Schristos 		free(pvt->membuf);
146b5677b36Schristos 	memput(pvt, sizeof *pvt);
147b5677b36Schristos 	memput(this, sizeof *this);
148b5677b36Schristos }
149b5677b36Schristos 
150b5677b36Schristos static struct group *
gr_next(struct irs_gr * this)151b5677b36Schristos gr_next(struct irs_gr *this) {
152b5677b36Schristos 
153b5677b36Schristos 	UNUSED(this);
154b5677b36Schristos 
155b5677b36Schristos 	return (NULL);
156b5677b36Schristos }
157b5677b36Schristos 
158b5677b36Schristos static struct group *
gr_byname(struct irs_gr * this,const char * name)159b5677b36Schristos gr_byname(struct irs_gr *this, const char *name) {
160b5677b36Schristos 	return (get_hes_group(this, name, "group"));
161b5677b36Schristos }
162b5677b36Schristos 
163b5677b36Schristos static struct group *
gr_bygid(struct irs_gr * this,gid_t gid)164b5677b36Schristos gr_bygid(struct irs_gr *this, gid_t gid) {
165b5677b36Schristos 	char name[32];
166b5677b36Schristos 
167b5677b36Schristos 	sprintf(name, "%ld", (long)gid);
168b5677b36Schristos 	return (get_hes_group(this, name, "gid"));
169b5677b36Schristos }
170b5677b36Schristos 
171b5677b36Schristos static void
gr_rewind(struct irs_gr * this)172b5677b36Schristos gr_rewind(struct irs_gr *this) {
173b5677b36Schristos 
174b5677b36Schristos 	UNUSED(this);
175b5677b36Schristos 
176b5677b36Schristos 	/* NOOP */
177b5677b36Schristos }
178b5677b36Schristos 
179b5677b36Schristos static int
gr_list(struct irs_gr * this,const char * name,gid_t basegid,gid_t * groups,int * ngroups)180b5677b36Schristos gr_list(struct irs_gr *this, const char *name,
181b5677b36Schristos 	gid_t basegid, gid_t *groups, int *ngroups)
182b5677b36Schristos {
183b5677b36Schristos 	UNUSED(this);
184b5677b36Schristos 	UNUSED(name);
185b5677b36Schristos 	UNUSED(basegid);
186b5677b36Schristos 	UNUSED(groups);
187b5677b36Schristos 
188b5677b36Schristos 	*ngroups = 0;
189b5677b36Schristos 	/* There's some way to do this in Hesiod. */
190b5677b36Schristos 	return (-1);
191b5677b36Schristos }
192b5677b36Schristos 
193b5677b36Schristos static void
gr_minimize(struct irs_gr * this)194b5677b36Schristos gr_minimize(struct irs_gr *this) {
195b5677b36Schristos 
196b5677b36Schristos 	UNUSED(this);
197b5677b36Schristos 	/* NOOP */
198b5677b36Schristos }
199b5677b36Schristos 
200b5677b36Schristos /* Private. */
201b5677b36Schristos 
202b5677b36Schristos static struct group *
get_hes_group(struct irs_gr * this,const char * name,const char * type)203b5677b36Schristos get_hes_group(struct irs_gr *this, const char *name, const char *type) {
204b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
205b5677b36Schristos 	char **hes_list, *cp, **new;
206b5677b36Schristos 	size_t num_members = 0;
207b5677b36Schristos 	u_long t;
208b5677b36Schristos 
209b5677b36Schristos 	hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type);
210b5677b36Schristos 	if (!hes_list)
211b5677b36Schristos 		return (NULL);
212b5677b36Schristos 
213b5677b36Schristos 	/*
214b5677b36Schristos 	 * Copy the returned hesiod string into storage space.
215b5677b36Schristos 	 */
216b5677b36Schristos 	if (pvt->membuf)
217b5677b36Schristos 		free(pvt->membuf);
218b5677b36Schristos 	pvt->membuf = strdup(*hes_list);
219b5677b36Schristos 	hesiod_free_list(pvt->dns->hes_ctx, hes_list);
220b5677b36Schristos 
221b5677b36Schristos 	cp = pvt->membuf;
222b5677b36Schristos 	pvt->group.gr_name = cp;
223b5677b36Schristos 	if (!(cp = strchr(cp, ':')))
224b5677b36Schristos 		goto cleanup;
225b5677b36Schristos 	*cp++ = '\0';
226b5677b36Schristos 
227b5677b36Schristos 	pvt->group.gr_passwd = cp;
228b5677b36Schristos 	if (!(cp = strchr(cp, ':')))
229b5677b36Schristos 		goto cleanup;
230b5677b36Schristos 	*cp++ = '\0';
231b5677b36Schristos 
232b5677b36Schristos 	errno = 0;
233b5677b36Schristos 	t = strtoul(cp, NULL, 10);
234b5677b36Schristos 	if (errno == ERANGE)
235b5677b36Schristos 		goto cleanup;
236b5677b36Schristos 	pvt->group.gr_gid = (gid_t) t;
237b5677b36Schristos 	if (!(cp = strchr(cp, ':')))
238b5677b36Schristos 		goto cleanup;
239b5677b36Schristos 	cp++;
240b5677b36Schristos 
241b5677b36Schristos 	/*
242b5677b36Schristos 	 * Parse the members out.
243b5677b36Schristos 	 */
244b5677b36Schristos 	while (*cp) {
245b5677b36Schristos 		if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
246b5677b36Schristos 			pvt->nmemb += 10;
247b5677b36Schristos 			new = realloc(pvt->group.gr_mem,
248b5677b36Schristos 				      pvt->nmemb * sizeof(char *));
249b5677b36Schristos 			if (new == NULL)
250b5677b36Schristos 				goto cleanup;
251b5677b36Schristos 			pvt->group.gr_mem = new;
252b5677b36Schristos 		}
253b5677b36Schristos 		pvt->group.gr_mem[num_members++] = cp;
254b5677b36Schristos 		if (!(cp = strchr(cp, ',')))
255b5677b36Schristos 			break;
256b5677b36Schristos 		*cp++ = '\0';
257b5677b36Schristos 	}
258b5677b36Schristos 	if (!pvt->group.gr_mem) {
259b5677b36Schristos 		pvt->group.gr_mem = malloc(sizeof(char*));
260b5677b36Schristos 		if (!pvt->group.gr_mem)
261b5677b36Schristos 			goto cleanup;
262b5677b36Schristos 	}
263b5677b36Schristos 	pvt->group.gr_mem[num_members] = NULL;
264b5677b36Schristos 
265b5677b36Schristos 	return (&pvt->group);
266b5677b36Schristos 
267b5677b36Schristos  cleanup:
268b5677b36Schristos 	if (pvt->group.gr_mem) {
269b5677b36Schristos 		free(pvt->group.gr_mem);
270b5677b36Schristos 		pvt->group.gr_mem = NULL;
271b5677b36Schristos 	}
272b5677b36Schristos 	if (pvt->membuf) {
273b5677b36Schristos 		free(pvt->membuf);
274b5677b36Schristos 		pvt->membuf = NULL;
275b5677b36Schristos 	}
276b5677b36Schristos 	return (NULL);
277b5677b36Schristos }
278b5677b36Schristos 
279b5677b36Schristos static struct __res_state *
gr_res_get(struct irs_gr * this)280b5677b36Schristos gr_res_get(struct irs_gr *this) {
281b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
282b5677b36Schristos 	struct dns_p *dns = pvt->dns;
283b5677b36Schristos 
284b5677b36Schristos 	return (__hesiod_res_get(dns->hes_ctx));
285b5677b36Schristos }
286b5677b36Schristos 
287b5677b36Schristos static void
gr_res_set(struct irs_gr * this,struct __res_state * res,void (* free_res)(void *))288b5677b36Schristos gr_res_set(struct irs_gr *this, struct __res_state * res,
289b5677b36Schristos 	   void (*free_res)(void *)) {
290b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
291b5677b36Schristos 	struct dns_p *dns = pvt->dns;
292b5677b36Schristos 
293b5677b36Schristos 	__hesiod_res_set(dns->hes_ctx, res, free_res);
294b5677b36Schristos }
295b5677b36Schristos 
296b5677b36Schristos #endif /* WANT_IRS_GR */
297