iostat - add read/write details to output
[dragonfly.git] / contrib / bind / lib / bind / irs / lcl_gr.c
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
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.
20  *
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.
32  */
33
34 /*
35  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
37  *
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.
41  *
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.
49  */
50
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: lcl_gr.c,v 1.3 2005/04/27 04:56:30 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 */
56
57 /* extern */
58
59 #include "port_before.h"
60
61 #ifndef WANT_IRS_PW
62 static int __bind_irs_gr_unneeded;
63 #else
64
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
69 #include <resolv.h>
70
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>
78
79 #include <irs.h>
80 #include <isc/memcluster.h>
81
82 #include "irs_p.h"
83 #include "lcl_p.h"
84 #include "irp_p.h"
85
86 #include "port_after.h"
87
88
89 /* Types. */
90
91 struct pvt {
92         FILE *          fp;
93         /*%<
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.
99          */
100         struct group    group;
101         size_t          nmemb;          /*%< Malloc'd max index of gr_mem[]. */
102         char *          membuf;
103         size_t          membufsize;
104 };
105
106 /* Forward. */
107
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 *);
114
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 *);
118
119 /* Portability. */
120
121 #ifndef SEEK_SET
122 # define SEEK_SET 0
123 #endif
124
125 /* Public. */
126
127 struct irs_gr *
128 irs_lcl_gr(struct irs_acc *this) {
129         struct irs_gr *gr;
130         struct pvt *pvt;
131
132         UNUSED(this);
133
134         if (!(gr = memget(sizeof *gr))) {
135                 errno = ENOMEM;
136                 return (NULL);
137         }
138         memset(gr, 0x5e, sizeof *gr);
139         if (!(pvt = memget(sizeof *pvt))) {
140                 memput(gr, sizeof *gr);
141                 errno = ENOMEM;
142                 return (NULL);
143         }
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);
156 }
157
158 /* Methods. */
159
160 static void
161 gr_close(struct irs_gr *this) {
162         struct pvt *pvt = (struct pvt *)this->private;
163
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);
172 }
173
174 static struct group *
175 gr_next(struct irs_gr *this) {
176         struct pvt *pvt = (struct pvt *)this->private;
177
178         if (!pvt->fp && !grstart(pvt))
179                 return (NULL);
180         return (grscan(this, 0, 0, NULL));
181 }
182
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));
188 }
189
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));
195 }
196
197 static void
198 gr_rewind(struct irs_gr *this) {
199         (void) grstart((struct pvt *)this->private);
200 }
201
202 static void
203 gr_minimize(struct irs_gr *this) {
204         struct pvt *pvt = (struct pvt *)this->private;
205
206         if (pvt->fp != NULL) {
207                 (void)fclose(pvt->fp);
208                 pvt->fp = NULL;
209         }
210 }
211
212 /* Private. */
213
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);
220         }
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);
226         }
227         return (1);
228 }
229
230 #define INITIAL_NMEMB   30                      /*%< about 120 bytes */
231 #define INITIAL_BUFSIZ  (INITIAL_NMEMB * 8)     /*%< about 240 bytes */
232 static char *
233 grnext(struct pvt *pvt) {
234         char *w, *e;
235         int ch;
236
237         /* Make sure we have a buffer. */
238         if (pvt->membuf == NULL) {
239                 pvt->membuf = malloc(INITIAL_BUFSIZ);
240                 if (pvt->membuf == NULL) {
241  enomem:
242                         errno = ENOMEM;
243                         return (NULL);
244                 }
245                 pvt->membufsize = INITIAL_BUFSIZ;
246         }
247
248         /* Read until EOF or EOL. */
249         w = pvt->membuf;
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. */
253                 if (w + 1 == e) {
254                         size_t o = w - pvt->membuf;
255                         size_t n = pvt->membufsize * 2;
256                         char *t = realloc(pvt->membuf, n);
257
258                         if (t == NULL)
259                                 goto enomem;
260                         pvt->membuf = t;
261                         pvt->membufsize = n;
262                         w = pvt->membuf + o;
263                         e = pvt->membuf + pvt->membufsize;
264                 }
265                 /* Store it. */
266                 *w++ = (char)ch;
267         }
268
269         /* Hitting EOF on the first character really does mean EOF. */
270         if (w == pvt->membuf && ch == EOF) {
271                 errno = ENOENT;
272                 return (NULL);
273         }
274
275         /* Last line of /etc/group need not end with \n; we don't care. */
276         *w = '\0';
277         return (pvt->membuf);
278 }
279
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;
283         size_t n;
284         char *bp, **m, *p;
285
286         /* Read lines until we find one that matches our search criteria. */
287         for (;;) {
288                 if ((bp = grnext(pvt)) == NULL)
289                         return (NULL);
290
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)
295                         continue;
296                 if (bp == NULL || *bp == '\0')
297                         goto corrupt;
298
299                 /* Skip past the password field. */
300                 pvt->group.gr_passwd = strsep(&bp, ":");
301                 if (bp == NULL || *bp == '\0')
302                         goto corrupt;
303
304                 /* Checking for a gid. */
305                 if ((p = strsep(&bp, ":")) == NULL)
306                         continue;
307                 /*
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.
310                  */
311                 if (p == NULL || *p == '\0') {
312  corrupt:
313                         /* warning: corrupted %s file!", _PATH_GROUP */
314                         continue;
315                 }
316                 pvt->group.gr_gid = atoi(p);
317                 if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid)
318                         continue;
319
320                 /* We want this record. */
321                 break;
322         }
323
324         /*
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.
330          */
331         n = 1;
332         if (bp != NULL)
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)
337                         n = INITIAL_NMEMB;
338                 if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL)
339                         return (NULL);
340                 pvt->group.gr_mem = m;
341                 pvt->nmemb = n;
342         }
343
344         /* Set the name pointers. */
345         for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;)
346                 if (p[0] != '\0')
347                         *m++ = p;
348         *m = NULL;
349
350         return (&pvt->group);
351 }
352
353 #endif /* WANT_IRS_GR */
354 /*! \file */