xref: /netbsd-src/external/bsd/libbind/dist/irs/lcl_gr.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: lcl_gr.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 1989, 1993, 1995
5b5677b36Schristos  *	The Regents of the University of California.  All rights reserved.
6b5677b36Schristos  *
7b5677b36Schristos  * Redistribution and use in source and binary forms, with or without
8b5677b36Schristos  * modification, are permitted provided that the following conditions
9b5677b36Schristos  * are met:
10b5677b36Schristos  * 1. Redistributions of source code must retain the above copyright
11b5677b36Schristos  *    notice, this list of conditions and the following disclaimer.
12b5677b36Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13b5677b36Schristos  *    notice, this list of conditions and the following disclaimer in the
14b5677b36Schristos  *    documentation and/or other materials provided with the distribution.
15b5677b36Schristos  * 3. All advertising materials mentioning features or use of this software
16b5677b36Schristos  *    must display the following acknowledgement:
17b5677b36Schristos  *	This product includes software developed by the University of
18b5677b36Schristos  *	California, Berkeley and its contributors.
19b5677b36Schristos  * 4. Neither the name of the University nor the names of its contributors
20b5677b36Schristos  *    may be used to endorse or promote products derived from this software
21b5677b36Schristos  *    without specific prior written permission.
22b5677b36Schristos  *
23b5677b36Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24b5677b36Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b5677b36Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b5677b36Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27b5677b36Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b5677b36Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b5677b36Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b5677b36Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b5677b36Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b5677b36Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b5677b36Schristos  * SUCH DAMAGE.
34b5677b36Schristos  */
35b5677b36Schristos 
36b5677b36Schristos /*
37b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38b5677b36Schristos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
39b5677b36Schristos  *
40b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
41b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
42b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
43b5677b36Schristos  *
44b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
47b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51b5677b36Schristos  */
52b5677b36Schristos 
53b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
54b5677b36Schristos static const char rcsid[] = "Id: lcl_gr.c,v 1.3 2005/04/27 04:56:30 sra Exp ";
55b5677b36Schristos /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
56b5677b36Schristos /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $	*/
57b5677b36Schristos #endif /* LIBC_SCCS and not lint */
58b5677b36Schristos 
59b5677b36Schristos /* extern */
60b5677b36Schristos 
61b5677b36Schristos #include "port_before.h"
62b5677b36Schristos 
63b5677b36Schristos #ifndef WANT_IRS_PW
64b5677b36Schristos static int __bind_irs_gr_unneeded;
65b5677b36Schristos #else
66b5677b36Schristos 
67b5677b36Schristos #include <sys/param.h>
68b5677b36Schristos #include <sys/types.h>
69b5677b36Schristos #include <netinet/in.h>
70b5677b36Schristos #include <arpa/nameser.h>
71b5677b36Schristos #include <resolv.h>
72b5677b36Schristos 
73b5677b36Schristos #include <errno.h>
74b5677b36Schristos #include <fcntl.h>
75b5677b36Schristos #include <grp.h>
76b5677b36Schristos #include <stdio.h>
77b5677b36Schristos #include <stdlib.h>
78b5677b36Schristos #include <string.h>
79b5677b36Schristos #include <unistd.h>
80b5677b36Schristos 
81b5677b36Schristos #include <irs.h>
82b5677b36Schristos #include <isc/memcluster.h>
83b5677b36Schristos 
84b5677b36Schristos #include "irs_p.h"
85b5677b36Schristos #include "lcl_p.h"
86b5677b36Schristos #include "irp_p.h"
87b5677b36Schristos 
88b5677b36Schristos #include "port_after.h"
89b5677b36Schristos 
90b5677b36Schristos 
91b5677b36Schristos /* Types. */
92b5677b36Schristos 
93b5677b36Schristos struct pvt {
94b5677b36Schristos 	FILE *		fp;
95b5677b36Schristos 	/*%<
96b5677b36Schristos 	 * Need space to store the entries read from the group file.
97b5677b36Schristos 	 * The members list also needs space per member, and the
98b5677b36Schristos 	 * strings making up the user names must be allocated
99b5677b36Schristos 	 * somewhere.  Rather than doing lots of small allocations,
100b5677b36Schristos 	 * we keep one buffer and resize it as needed.
101b5677b36Schristos 	 */
102b5677b36Schristos 	struct group	group;
103b5677b36Schristos 	size_t		nmemb;		/*%< Malloc'd max index of gr_mem[]. */
104b5677b36Schristos 	char *		membuf;
105b5677b36Schristos 	size_t		membufsize;
106b5677b36Schristos };
107b5677b36Schristos 
108b5677b36Schristos /* Forward. */
109b5677b36Schristos 
110b5677b36Schristos static void		gr_close(struct irs_gr *);
111b5677b36Schristos static struct group *	gr_next(struct irs_gr *);
112b5677b36Schristos static struct group *	gr_byname(struct irs_gr *, const char *);
113b5677b36Schristos static struct group *	gr_bygid(struct irs_gr *, gid_t);
114b5677b36Schristos static void		gr_rewind(struct irs_gr *);
115b5677b36Schristos static void		gr_minimize(struct irs_gr *);
116b5677b36Schristos 
117b5677b36Schristos static int		grstart(struct pvt *);
118b5677b36Schristos static char *		grnext(struct pvt *);
119b5677b36Schristos static struct group *	grscan(struct irs_gr *, int, gid_t, const char *);
120b5677b36Schristos 
121b5677b36Schristos /* Portability. */
122b5677b36Schristos 
123b5677b36Schristos #ifndef SEEK_SET
124b5677b36Schristos # define SEEK_SET 0
125b5677b36Schristos #endif
126b5677b36Schristos 
127b5677b36Schristos /* Public. */
128b5677b36Schristos 
129b5677b36Schristos struct irs_gr *
irs_lcl_gr(struct irs_acc * this)130b5677b36Schristos irs_lcl_gr(struct irs_acc *this) {
131b5677b36Schristos 	struct irs_gr *gr;
132b5677b36Schristos 	struct pvt *pvt;
133b5677b36Schristos 
134b5677b36Schristos 	UNUSED(this);
135b5677b36Schristos 
136b5677b36Schristos 	if (!(gr = memget(sizeof *gr))) {
137b5677b36Schristos 		errno = ENOMEM;
138b5677b36Schristos 		return (NULL);
139b5677b36Schristos 	}
140b5677b36Schristos 	memset(gr, 0x5e, sizeof *gr);
141b5677b36Schristos 	if (!(pvt = memget(sizeof *pvt))) {
142b5677b36Schristos 		memput(gr, sizeof *gr);
143b5677b36Schristos 		errno = ENOMEM;
144b5677b36Schristos 		return (NULL);
145b5677b36Schristos 	}
146b5677b36Schristos 	memset(pvt, 0, sizeof *pvt);
147b5677b36Schristos 	gr->private = pvt;
148b5677b36Schristos 	gr->close = gr_close;
149b5677b36Schristos 	gr->next = gr_next;
150b5677b36Schristos 	gr->byname = gr_byname;
151b5677b36Schristos 	gr->bygid = gr_bygid;
152b5677b36Schristos 	gr->rewind = gr_rewind;
153b5677b36Schristos 	gr->list = make_group_list;
154b5677b36Schristos 	gr->minimize = gr_minimize;
155b5677b36Schristos 	gr->res_get = NULL;
156b5677b36Schristos 	gr->res_set = NULL;
157b5677b36Schristos 	return (gr);
158b5677b36Schristos }
159b5677b36Schristos 
160b5677b36Schristos /* Methods. */
161b5677b36Schristos 
162b5677b36Schristos static void
gr_close(struct irs_gr * this)163b5677b36Schristos gr_close(struct irs_gr *this) {
164b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
165b5677b36Schristos 
166b5677b36Schristos 	if (pvt->fp)
167b5677b36Schristos 		(void)fclose(pvt->fp);
168b5677b36Schristos 	if (pvt->group.gr_mem)
169b5677b36Schristos 		free(pvt->group.gr_mem);
170b5677b36Schristos 	if (pvt->membuf)
171b5677b36Schristos 		free(pvt->membuf);
172b5677b36Schristos 	memput(pvt, sizeof *pvt);
173b5677b36Schristos 	memput(this, sizeof *this);
174b5677b36Schristos }
175b5677b36Schristos 
176b5677b36Schristos static struct group *
gr_next(struct irs_gr * this)177b5677b36Schristos gr_next(struct irs_gr *this) {
178b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
179b5677b36Schristos 
180b5677b36Schristos 	if (!pvt->fp && !grstart(pvt))
181b5677b36Schristos 		return (NULL);
182b5677b36Schristos 	return (grscan(this, 0, 0, NULL));
183b5677b36Schristos }
184b5677b36Schristos 
185b5677b36Schristos static struct group *
gr_byname(struct irs_gr * this,const char * name)186b5677b36Schristos gr_byname(struct irs_gr *this, const char *name) {
187b5677b36Schristos 	if (!grstart((struct pvt *)this->private))
188b5677b36Schristos 		return (NULL);
189b5677b36Schristos 	return (grscan(this, 1, 0, name));
190b5677b36Schristos }
191b5677b36Schristos 
192b5677b36Schristos static struct group *
gr_bygid(struct irs_gr * this,gid_t gid)193b5677b36Schristos gr_bygid(struct irs_gr *this, gid_t gid) {
194b5677b36Schristos 	if (!grstart((struct pvt *)this->private))
195b5677b36Schristos 		return (NULL);
196b5677b36Schristos 	return (grscan(this, 1, gid, NULL));
197b5677b36Schristos }
198b5677b36Schristos 
199b5677b36Schristos static void
gr_rewind(struct irs_gr * this)200b5677b36Schristos gr_rewind(struct irs_gr *this) {
201b5677b36Schristos 	(void) grstart((struct pvt *)this->private);
202b5677b36Schristos }
203b5677b36Schristos 
204b5677b36Schristos static void
gr_minimize(struct irs_gr * this)205b5677b36Schristos gr_minimize(struct irs_gr *this) {
206b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
207b5677b36Schristos 
208b5677b36Schristos 	if (pvt->fp != NULL) {
209b5677b36Schristos 		(void)fclose(pvt->fp);
210b5677b36Schristos 		pvt->fp = NULL;
211b5677b36Schristos 	}
212b5677b36Schristos }
213b5677b36Schristos 
214b5677b36Schristos /* Private. */
215b5677b36Schristos 
216b5677b36Schristos static int
grstart(struct pvt * pvt)217b5677b36Schristos grstart(struct pvt *pvt) {
218b5677b36Schristos 	if (pvt->fp) {
219b5677b36Schristos 		if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
220b5677b36Schristos 			return (1);
221b5677b36Schristos 		(void)fclose(pvt->fp);
222b5677b36Schristos 	}
223b5677b36Schristos 	if (!(pvt->fp = fopen(_PATH_GROUP, "r")))
224b5677b36Schristos 		return (0);
225b5677b36Schristos 	if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
226b5677b36Schristos 		fclose(pvt->fp);
227b5677b36Schristos 		return (0);
228b5677b36Schristos 	}
229b5677b36Schristos 	return (1);
230b5677b36Schristos }
231b5677b36Schristos 
232b5677b36Schristos #define	INITIAL_NMEMB	30			/*%< about 120 bytes */
233b5677b36Schristos #define	INITIAL_BUFSIZ	(INITIAL_NMEMB * 8)	/*%< about 240 bytes */
234b5677b36Schristos static char *
grnext(struct pvt * pvt)235b5677b36Schristos grnext(struct pvt *pvt) {
236b5677b36Schristos 	char *w, *e;
237b5677b36Schristos 	int ch;
238b5677b36Schristos 
239b5677b36Schristos 	/* Make sure we have a buffer. */
240b5677b36Schristos 	if (pvt->membuf == NULL) {
241b5677b36Schristos 		pvt->membuf = malloc(INITIAL_BUFSIZ);
242b5677b36Schristos 		if (pvt->membuf == NULL) {
243b5677b36Schristos  enomem:
244b5677b36Schristos 			errno = ENOMEM;
245b5677b36Schristos 			return (NULL);
246b5677b36Schristos 		}
247b5677b36Schristos 		pvt->membufsize = INITIAL_BUFSIZ;
248b5677b36Schristos 	}
249b5677b36Schristos 
250b5677b36Schristos 	/* Read until EOF or EOL. */
251b5677b36Schristos 	w = pvt->membuf;
252b5677b36Schristos 	e = pvt->membuf + pvt->membufsize;
253b5677b36Schristos 	while ((ch = fgetc(pvt->fp)) != EOF && ch != '\n') {
254b5677b36Schristos 		/* Make sure we have room for this character and a \0. */
255b5677b36Schristos 		if (w + 1 == e) {
256b5677b36Schristos 			size_t o = w - pvt->membuf;
257b5677b36Schristos 			size_t n = pvt->membufsize * 2;
258b5677b36Schristos 			char *t = realloc(pvt->membuf, n);
259b5677b36Schristos 
260b5677b36Schristos 			if (t == NULL)
261b5677b36Schristos 				goto enomem;
262b5677b36Schristos 			pvt->membuf = t;
263b5677b36Schristos 			pvt->membufsize = n;
264b5677b36Schristos 			w = pvt->membuf + o;
265b5677b36Schristos 			e = pvt->membuf + pvt->membufsize;
266b5677b36Schristos 		}
267b5677b36Schristos 		/* Store it. */
268b5677b36Schristos 		*w++ = (char)ch;
269b5677b36Schristos 	}
270b5677b36Schristos 
271b5677b36Schristos 	/* Hitting EOF on the first character really does mean EOF. */
272b5677b36Schristos 	if (w == pvt->membuf && ch == EOF) {
273b5677b36Schristos 		errno = ENOENT;
274b5677b36Schristos 		return (NULL);
275b5677b36Schristos 	}
276b5677b36Schristos 
277b5677b36Schristos 	/* Last line of /etc/group need not end with \n; we don't care. */
278b5677b36Schristos 	*w = '\0';
279b5677b36Schristos 	return (pvt->membuf);
280b5677b36Schristos }
281b5677b36Schristos 
282b5677b36Schristos static struct group *
grscan(struct irs_gr * this,int search,gid_t gid,const char * name)283b5677b36Schristos grscan(struct irs_gr *this, int search, gid_t gid, const char *name) {
284b5677b36Schristos 	struct pvt *pvt = (struct pvt *)this->private;
285b5677b36Schristos 	size_t n;
286b5677b36Schristos 	char *bp, **m, *p;
287b5677b36Schristos 
288b5677b36Schristos 	/* Read lines until we find one that matches our search criteria. */
289b5677b36Schristos 	for (;;) {
290b5677b36Schristos 		if ((bp = grnext(pvt)) == NULL)
291b5677b36Schristos 			return (NULL);
292b5677b36Schristos 
293b5677b36Schristos 		/* Optimize the usual case of searching for a name. */
294b5677b36Schristos 		pvt->group.gr_name = strsep(&bp, ":");
295b5677b36Schristos 		if (search && name != NULL &&
296b5677b36Schristos 		    strcmp(pvt->group.gr_name, name) != 0)
297b5677b36Schristos 			continue;
298b5677b36Schristos 		if (bp == NULL || *bp == '\0')
299b5677b36Schristos 			goto corrupt;
300b5677b36Schristos 
301b5677b36Schristos 		/* Skip past the password field. */
302b5677b36Schristos 		pvt->group.gr_passwd = strsep(&bp, ":");
303b5677b36Schristos 		if (bp == NULL || *bp == '\0')
304b5677b36Schristos 			goto corrupt;
305b5677b36Schristos 
306b5677b36Schristos 		/* Checking for a gid. */
307b5677b36Schristos 		if ((p = strsep(&bp, ":")) == NULL)
308b5677b36Schristos 			continue;
309b5677b36Schristos 		/*
310b5677b36Schristos 		 * Unlike the tests above, the test below is supposed to be
311b5677b36Schristos 		 * testing 'p' and not 'bp', in case you think it's a typo.
312b5677b36Schristos 		 */
313b5677b36Schristos 		if (p == NULL || *p == '\0') {
314b5677b36Schristos  corrupt:
315b5677b36Schristos 			/* warning: corrupted %s file!", _PATH_GROUP */
316b5677b36Schristos 			continue;
317b5677b36Schristos 		}
318b5677b36Schristos 		pvt->group.gr_gid = atoi(p);
319b5677b36Schristos 		if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid)
320b5677b36Schristos 			continue;
321b5677b36Schristos 
322b5677b36Schristos 		/* We want this record. */
323b5677b36Schristos 		break;
324b5677b36Schristos 	}
325b5677b36Schristos 
326b5677b36Schristos 	/*
327b5677b36Schristos 	 * Count commas to find out how many members there might be.
328b5677b36Schristos 	 * Note that commas separate, so if there is one comma there
329b5677b36Schristos 	 * can be two members (group:*:id:user1,user2).  Add another
330b5677b36Schristos 	 * to account for the NULL terminator.  As above, allocate
331b5677b36Schristos 	 * largest of INITIAL_NMEMB, or 2*n.
332b5677b36Schristos 	 */
333b5677b36Schristos 	n = 1;
334b5677b36Schristos 	if (bp != NULL)
335b5677b36Schristos 		for (n = 2, p = bp; (p = strpbrk(p, ", ")) != NULL; ++n)
336b5677b36Schristos 			p += strspn(p, ", ");
337b5677b36Schristos 	if (n > pvt->nmemb || pvt->group.gr_mem == NULL) {
338b5677b36Schristos 		if ((n *= 2) < INITIAL_NMEMB)
339b5677b36Schristos 			n = INITIAL_NMEMB;
340b5677b36Schristos 		if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL)
341b5677b36Schristos 			return (NULL);
342b5677b36Schristos 		pvt->group.gr_mem = m;
343b5677b36Schristos 		pvt->nmemb = n;
344b5677b36Schristos 	}
345b5677b36Schristos 
346b5677b36Schristos 	/* Set the name pointers. */
347b5677b36Schristos 	for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;)
348b5677b36Schristos 		if (p[0] != '\0')
349b5677b36Schristos 			*m++ = p;
350b5677b36Schristos 	*m = NULL;
351b5677b36Schristos 
352b5677b36Schristos 	return (&pvt->group);
353b5677b36Schristos }
354b5677b36Schristos 
355b5677b36Schristos #endif /* WANT_IRS_GR */
356b5677b36Schristos /*! \file */
357