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