4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
37 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
53 #if defined(LIBC_SCCS) && !defined(lint)
54 static 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 */
61 #include "port_before.h"
64 static int __bind_irs_gr_unneeded
;
67 #include <sys/param.h>
68 #include <sys/types.h>
69 #include <netinet/in.h>
70 #include <arpa/nameser.h>
82 #include <isc/memcluster.h>
88 #include "port_after.h"
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.
103 size_t nmemb
; /*%< Malloc'd max index of gr_mem[]. */
110 static void gr_close(struct irs_gr
*);
111 static struct group
* gr_next(struct irs_gr
*);
112 static struct group
* gr_byname(struct irs_gr
*, const char *);
113 static struct group
* gr_bygid(struct irs_gr
*, gid_t
);
114 static void gr_rewind(struct irs_gr
*);
115 static void gr_minimize(struct irs_gr
*);
117 static int grstart(struct pvt
*);
118 static char * grnext(struct pvt
*);
119 static struct group
* grscan(struct irs_gr
*, int, gid_t
, const char *);
130 irs_lcl_gr(struct irs_acc
*this) {
136 if (!(gr
= memget(sizeof *gr
))) {
140 memset(gr
, 0x5e, sizeof *gr
);
141 if (!(pvt
= memget(sizeof *pvt
))) {
142 memput(gr
, sizeof *gr
);
146 memset(pvt
, 0, sizeof *pvt
);
148 gr
->close
= gr_close
;
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
;
163 gr_close(struct irs_gr
*this) {
164 struct pvt
*pvt
= (struct pvt
*)this->private;
167 (void)fclose(pvt
->fp
);
168 if (pvt
->group
.gr_mem
)
169 free(pvt
->group
.gr_mem
);
172 memput(pvt
, sizeof *pvt
);
173 memput(this, sizeof *this);
176 static struct group
*
177 gr_next(struct irs_gr
*this) {
178 struct pvt
*pvt
= (struct pvt
*)this->private;
180 if (!pvt
->fp
&& !grstart(pvt
))
182 return (grscan(this, 0, 0, NULL
));
185 static struct group
*
186 gr_byname(struct irs_gr
*this, const char *name
) {
187 if (!grstart((struct pvt
*)this->private))
189 return (grscan(this, 1, 0, name
));
192 static struct group
*
193 gr_bygid(struct irs_gr
*this, gid_t gid
) {
194 if (!grstart((struct pvt
*)this->private))
196 return (grscan(this, 1, gid
, NULL
));
200 gr_rewind(struct irs_gr
*this) {
201 (void) grstart((struct pvt
*)this->private);
205 gr_minimize(struct irs_gr
*this) {
206 struct pvt
*pvt
= (struct pvt
*)this->private;
208 if (pvt
->fp
!= NULL
) {
209 (void)fclose(pvt
->fp
);
217 grstart(struct pvt
*pvt
) {
219 if (fseek(pvt
->fp
, 0L, SEEK_SET
) == 0)
221 (void)fclose(pvt
->fp
);
223 if (!(pvt
->fp
= fopen(_PATH_GROUP
, "r")))
225 if (fcntl(fileno(pvt
->fp
), F_SETFD
, 1) < 0) {
232 #define INITIAL_NMEMB 30 /*%< about 120 bytes */
233 #define INITIAL_BUFSIZ (INITIAL_NMEMB * 8) /*%< about 240 bytes */
235 grnext(struct pvt
*pvt
) {
239 /* Make sure we have a buffer. */
240 if (pvt
->membuf
== NULL
) {
241 pvt
->membuf
= malloc(INITIAL_BUFSIZ
);
242 if (pvt
->membuf
== NULL
) {
247 pvt
->membufsize
= INITIAL_BUFSIZ
;
250 /* Read until EOF or EOL. */
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. */
256 size_t o
= w
- pvt
->membuf
;
257 size_t n
= pvt
->membufsize
* 2;
258 char *t
= realloc(pvt
->membuf
, n
);
265 e
= pvt
->membuf
+ pvt
->membufsize
;
271 /* Hitting EOF on the first character really does mean EOF. */
272 if (w
== pvt
->membuf
&& ch
== EOF
) {
277 /* Last line of /etc/group need not end with \n; we don't care. */
279 return (pvt
->membuf
);
282 static struct group
*
283 grscan(struct irs_gr
*this, int search
, gid_t gid
, const char *name
) {
284 struct pvt
*pvt
= (struct pvt
*)this->private;
288 /* Read lines until we find one that matches our search criteria. */
290 if ((bp
= grnext(pvt
)) == NULL
)
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)
298 if (bp
== NULL
|| *bp
== '\0')
301 /* Skip past the password field. */
302 pvt
->group
.gr_passwd
= strsep(&bp
, ":");
303 if (bp
== NULL
|| *bp
== '\0')
306 /* Checking for a gid. */
307 if ((p
= strsep(&bp
, ":")) == NULL
)
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.
313 if (p
== NULL
|| *p
== '\0') {
315 /* warning: corrupted %s file!", _PATH_GROUP */
318 pvt
->group
.gr_gid
= atoi(p
);
319 if (search
&& name
== NULL
&& (gid_t
)pvt
->group
.gr_gid
!= gid
)
322 /* We want this record. */
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.
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
)
340 if ((m
= realloc(pvt
->group
.gr_mem
, n
* sizeof *m
)) == NULL
)
342 pvt
->group
.gr_mem
= m
;
346 /* Set the name pointers. */
347 for (m
= pvt
->group
.gr_mem
; (p
= strsep(&bp
, ", ")) != NULL
;)
352 return (&pvt
->group
);
355 #endif /* WANT_IRS_GR */