Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / lcl_gr.c
blob5564c08b1c587b9f4b8d232142246e596d537e71
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
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.1.2.1 2004/03/09 09:17:32 marka 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 */
57 /* extern */
59 #include "port_before.h"
61 #ifndef WANT_IRS_PW
62 static int __bind_irs_gr_unneeded;
63 #else
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
69 #include <resolv.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <grp.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
79 #include <irs.h>
80 #include <isc/memcluster.h>
82 #include "irs_p.h"
83 #include "lcl_p.h"
84 #include "irp_p.h"
86 #include "port_after.h"
89 /* Types. */
91 struct pvt {
92 FILE * fp;
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.
100 struct group group;
101 size_t nmemb; /* Malloc'd max index of gr_mem[]. */
102 char * membuf;
103 size_t membufsize;
106 /* Forward. */
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 *);
119 /* Portability. */
121 #ifndef SEEK_SET
122 # define SEEK_SET 0
123 #endif
125 /* Public. */
127 struct irs_gr *
128 irs_lcl_gr(struct irs_acc *this) {
129 struct irs_gr *gr;
130 struct pvt *pvt;
132 UNUSED(this);
134 if (!(gr = memget(sizeof *gr))) {
135 errno = ENOMEM;
136 return (NULL);
138 memset(gr, 0x5e, sizeof *gr);
139 if (!(pvt = memget(sizeof *pvt))) {
140 memput(gr, sizeof *gr);
141 errno = ENOMEM;
142 return (NULL);
144 memset(pvt, 0, sizeof *pvt);
145 gr->private = pvt;
146 gr->close = gr_close;
147 gr->next = gr_next;
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;
153 gr->res_get = NULL;
154 gr->res_set = NULL;
155 return (gr);
158 /* Methods. */
160 static void
161 gr_close(struct irs_gr *this) {
162 struct pvt *pvt = (struct pvt *)this->private;
164 if (pvt->fp)
165 (void)fclose(pvt->fp);
166 if (pvt->group.gr_mem)
167 free(pvt->group.gr_mem);
168 if (pvt->membuf)
169 free(pvt->membuf);
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))
179 return (NULL);
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))
186 return (NULL);
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))
193 return (NULL);
194 return (grscan(this, 1, gid, NULL));
197 static void
198 gr_rewind(struct irs_gr *this) {
199 (void) grstart((struct pvt *)this->private);
202 static void
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);
208 pvt->fp = NULL;
212 /* Private. */
214 static int
215 grstart(struct pvt *pvt) {
216 if (pvt->fp) {
217 if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
218 return (1);
219 (void)fclose(pvt->fp);
221 if (!(pvt->fp = fopen(_PATH_GROUP, "r")))
222 return (0);
223 if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
224 fclose(pvt->fp);
225 return (0);
227 return (1);
230 #define INITIAL_NMEMB 30 /* about 120 bytes */
231 #define INITIAL_BUFSIZ (INITIAL_NMEMB * 8) /* about 240 bytes */
233 static char *
234 grnext(struct pvt *pvt) {
235 char *w, *e;
236 int ch;
238 /* Make sure we have a buffer. */
239 if (pvt->membuf == NULL) {
240 pvt->membuf = malloc(INITIAL_BUFSIZ);
241 if (pvt->membuf == NULL) {
242 enomem:
243 errno = ENOMEM;
244 return (NULL);
246 pvt->membufsize = INITIAL_BUFSIZ;
249 /* Read until EOF or EOL. */
250 w = pvt->membuf;
251 e = pvt->membuf + pvt->membufsize;
252 while ((ch = fgetc(pvt->fp)) != EOF && ch != '\n') {
253 /* Make sure we have room for this character and a \0. */
254 if (w + 1 == e) {
255 size_t o = w - pvt->membuf;
256 size_t n = pvt->membufsize * 2;
257 char *t = realloc(pvt->membuf, n);
259 if (t == NULL)
260 goto enomem;
261 pvt->membuf = t;
262 pvt->membufsize = n;
263 w = pvt->membuf + o;
264 e = pvt->membuf + pvt->membufsize;
266 /* Store it. */
267 *w++ = (char)ch;
270 /* Hitting EOF on the first character really does mean EOF. */
271 if (w == pvt->membuf && ch == EOF) {
272 errno = ENOENT;
273 return (NULL);
276 /* Last line of /etc/group need not end with \n; we don't care. */
277 *w = '\0';
278 return (pvt->membuf);
281 static struct group *
282 grscan(struct irs_gr *this, int search, gid_t gid, const char *name) {
283 struct pvt *pvt = (struct pvt *)this->private;
284 size_t n;
285 char *bp, **m, *p;
287 /* Read lines until we find one that matches our search criteria. */
288 for (;;) {
289 if ((bp = grnext(pvt)) == NULL)
290 return (NULL);
292 /* Optimize the usual case of searching for a name. */
293 pvt->group.gr_name = strsep(&bp, ":");
294 if (search && name != NULL &&
295 strcmp(pvt->group.gr_name, name) != 0)
296 continue;
297 if (bp == NULL || *bp == '\0')
298 goto corrupt;
300 /* Skip past the password field. */
301 pvt->group.gr_passwd = strsep(&bp, ":");
302 if (bp == NULL || *bp == '\0')
303 goto corrupt;
305 /* Checking for a gid. */
306 if ((p = strsep(&bp, ":")) == NULL)
307 continue;
309 * Unlike the tests above, the test below is supposed to be
310 * testing 'p' and not 'bp', in case you think it's a typo.
312 if (p == NULL || *p == '\0') {
313 corrupt:
314 /* warning: corrupted %s file!", _PATH_GROUP */
315 continue;
317 pvt->group.gr_gid = atoi(p);
318 if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid)
319 continue;
321 /* We want this record. */
322 break;
326 * Count commas to find out how many members there might be.
327 * Note that commas separate, so if there is one comma there
328 * can be two members (group:*:id:user1,user2). Add another
329 * to account for the NULL terminator. As above, allocate
330 * largest of INITIAL_NMEMB, or 2*n.
332 n = 1;
333 if (bp != NULL)
334 for (n = 2, p = bp; (p = strpbrk(p, ", ")) != NULL; ++n)
335 p += strspn(p, ", ");
336 if (n > pvt->nmemb || pvt->group.gr_mem == NULL) {
337 if ((n *= 2) < INITIAL_NMEMB)
338 n = INITIAL_NMEMB;
339 if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL)
340 return (NULL);
341 pvt->group.gr_mem = m;
342 pvt->nmemb = n;
345 /* Set the name pointers. */
346 for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;)
347 if (p[0] != '\0')
348 *m++ = p;
349 *m = NULL;
351 return (&pvt->group);
354 #endif /* WANT_IRS_GR */