2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: lcl_gr.c,v 1.2.18.1 2005/04/27 05:01:02 sra Exp $";
53 /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
54 /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */
55 #endif /* LIBC_SCCS and not lint */
59 #include "port_before.h"
62 static int __bind_irs_gr_unneeded;
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
80 #include <isc/memcluster.h>
86 #include "port_after.h"
94 * Need space to store the entries read from the group file.
95 * The members list also needs space per member, and the
96 * strings making up the user names must be allocated
97 * somewhere. Rather than doing lots of small allocations,
98 * we keep one buffer and resize it as needed.
101 size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */
108 static void gr_close(struct irs_gr *);
109 static struct group * gr_next(struct irs_gr *);
110 static struct group * gr_byname(struct irs_gr *, const char *);
111 static struct group * gr_bygid(struct irs_gr *, gid_t);
112 static void gr_rewind(struct irs_gr *);
113 static void gr_minimize(struct irs_gr *);
115 static int grstart(struct pvt *);
116 static char * grnext(struct pvt *);
117 static struct group * grscan(struct irs_gr *, int, gid_t, const char *);
128 irs_lcl_gr(struct irs_acc *this) {
134 if (!(gr = memget(sizeof *gr))) {
138 memset(gr, 0x5e, sizeof *gr);
139 if (!(pvt = memget(sizeof *pvt))) {
140 memput(gr, sizeof *gr);
144 memset(pvt, 0, sizeof *pvt);
146 gr->close = gr_close;
148 gr->byname = gr_byname;
149 gr->bygid = gr_bygid;
150 gr->rewind = gr_rewind;
151 gr->list = make_group_list;
152 gr->minimize = gr_minimize;
161 gr_close(struct irs_gr *this) {
162 struct pvt *pvt = (struct pvt *)this->private;
165 (void)fclose(pvt->fp);
166 if (pvt->group.gr_mem)
167 free(pvt->group.gr_mem);
170 memput(pvt, sizeof *pvt);
171 memput(this, sizeof *this);
174 static struct group *
175 gr_next(struct irs_gr *this) {
176 struct pvt *pvt = (struct pvt *)this->private;
178 if (!pvt->fp && !grstart(pvt))
180 return (grscan(this, 0, 0, NULL));
183 static struct group *
184 gr_byname(struct irs_gr *this, const char *name) {
185 if (!grstart((struct pvt *)this->private))
187 return (grscan(this, 1, 0, name));
190 static struct group *
191 gr_bygid(struct irs_gr *this, gid_t gid) {
192 if (!grstart((struct pvt *)this->private))
194 return (grscan(this, 1, gid, NULL));
198 gr_rewind(struct irs_gr *this) {
199 (void) grstart((struct pvt *)this->private);
203 gr_minimize(struct irs_gr *this) {
204 struct pvt *pvt = (struct pvt *)this->private;
206 if (pvt->fp != NULL) {
207 (void)fclose(pvt->fp);
215 grstart(struct pvt *pvt) {
217 if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
219 (void)fclose(pvt->fp);
221 if (!(pvt->fp = fopen(_PATH_GROUP, "r")))
223 if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
230 #define INITIAL_NMEMB 30 /*%< about 120 bytes */
231 #define INITIAL_BUFSIZ (INITIAL_NMEMB * 8) /*%< about 240 bytes */
233 grnext(struct pvt *pvt) {
237 /* Make sure we have a buffer. */
238 if (pvt->membuf == NULL) {
239 pvt->membuf = malloc(INITIAL_BUFSIZ);
240 if (pvt->membuf == NULL) {
245 pvt->membufsize = INITIAL_BUFSIZ;
248 /* Read until EOF or EOL. */
250 e = pvt->membuf + pvt->membufsize;
251 while ((ch = fgetc(pvt->fp)) != EOF && ch != '\n') {
252 /* Make sure we have room for this character and a \0. */
254 size_t o = w - pvt->membuf;
255 size_t n = pvt->membufsize * 2;
256 char *t = realloc(pvt->membuf, n);
263 e = pvt->membuf + pvt->membufsize;
269 /* Hitting EOF on the first character really does mean EOF. */
270 if (w == pvt->membuf && ch == EOF) {
275 /* Last line of /etc/group need not end with \n; we don't care. */
277 return (pvt->membuf);
280 static struct group *
281 grscan(struct irs_gr *this, int search, gid_t gid, const char *name) {
282 struct pvt *pvt = (struct pvt *)this->private;
286 /* Read lines until we find one that matches our search criteria. */
288 if ((bp = grnext(pvt)) == NULL)
291 /* Optimize the usual case of searching for a name. */
292 pvt->group.gr_name = strsep(&bp, ":");
293 if (search && name != NULL &&
294 strcmp(pvt->group.gr_name, name) != 0)
296 if (bp == NULL || *bp == '\0')
299 /* Skip past the password field. */
300 pvt->group.gr_passwd = strsep(&bp, ":");
301 if (bp == NULL || *bp == '\0')
304 /* Checking for a gid. */
305 if ((p = strsep(&bp, ":")) == NULL)
308 * Unlike the tests above, the test below is supposed to be
309 * testing 'p' and not 'bp', in case you think it's a typo.
311 if (p == NULL || *p == '\0') {
313 /* warning: corrupted %s file!", _PATH_GROUP */
316 pvt->group.gr_gid = atoi(p);
317 if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid)
320 /* We want this record. */
325 * Count commas to find out how many members there might be.
326 * Note that commas separate, so if there is one comma there
327 * can be two members (group:*:id:user1,user2). Add another
328 * to account for the NULL terminator. As above, allocate
329 * largest of INITIAL_NMEMB, or 2*n.
333 for (n = 2, p = bp; (p = strpbrk(p, ", ")) != NULL; ++n)
334 p += strspn(p, ", ");
335 if (n > pvt->nmemb || pvt->group.gr_mem == NULL) {
336 if ((n *= 2) < INITIAL_NMEMB)
338 if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL)
340 pvt->group.gr_mem = m;
344 /* Set the name pointers. */
345 for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;)
350 return (&pvt->group);
353 #endif /* WANT_IRS_GR */