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: nis_gr.c,v 1.4 2005/04/27 04:56:32 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"
61 #if !defined(WANT_IRS_GR) || !defined(WANT_IRS_NIS)
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>
70 #include <isc/memcluster.h>
74 #include <rpcsvc/yp_prot.h>
75 #include <rpcsvc/ypclnt.h>
84 #include <isc/memcluster.h>
88 #include "port_after.h"
103 * Need space to store the entries read from the group file.
104 * The members list also needs space per member, and the
105 * strings making up the user names must be allocated
106 * somewhere. Rather than doing lots of small allocations,
107 * we keep one buffer and resize it as needed.
110 size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */
115 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
117 static /*const*/ char group_bygid[] = "group.bygid";
118 static /*const*/ char group_byname[] = "group.byname";
122 static void gr_close(struct irs_gr *);
123 static struct group * gr_next(struct irs_gr *);
124 static struct group * gr_byname(struct irs_gr *, const char *);
125 static struct group * gr_bygid(struct irs_gr *, gid_t);
126 static void gr_rewind(struct irs_gr *);
127 static void gr_minimize(struct irs_gr *);
129 static struct group * makegroupent(struct irs_gr *);
130 static void nisfree(struct pvt *, enum do_what);
135 irs_nis_gr(struct irs_acc *this) {
139 if (!(gr = memget(sizeof *gr))) {
143 memset(gr, 0x5e, sizeof *gr);
144 if (!(pvt = memget(sizeof *pvt))) {
145 memput(gr, sizeof *gr);
149 memset(pvt, 0, sizeof *pvt);
151 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
153 gr->close = gr_close;
155 gr->byname = gr_byname;
156 gr->bygid = gr_bygid;
157 gr->rewind = gr_rewind;
158 gr->list = make_group_list;
159 gr->minimize = gr_minimize;
168 gr_close(struct irs_gr *this) {
169 struct pvt *pvt = (struct pvt *)this->private;
171 if (pvt->group.gr_mem)
172 free(pvt->group.gr_mem);
175 memput(pvt, sizeof *pvt);
176 memput(this, sizeof *this);
179 static struct group *
180 gr_next(struct irs_gr *this) {
181 struct pvt *pvt = (struct pvt *)this->private;
186 if (pvt->needrewind) {
187 nisfree(pvt, do_all);
188 r = yp_first(pvt->nis_domain, group_byname,
189 &pvt->curkey_data, &pvt->curkey_len,
190 &pvt->curval_data, &pvt->curval_len);
196 nisfree(pvt, do_val);
197 r = yp_next(pvt->nis_domain, group_byname,
198 pvt->curkey_data, pvt->curkey_len,
199 &newkey_data, &newkey_len,
200 &pvt->curval_data, &pvt->curval_len);
201 nisfree(pvt, do_key);
202 pvt->curkey_data = newkey_data;
203 pvt->curkey_len = newkey_len;
209 rval = makegroupent(this);
210 } while (rval == NULL);
214 static struct group *
215 gr_byname(struct irs_gr *this, const char *name) {
216 struct pvt *pvt = (struct pvt *)this->private;
219 nisfree(pvt, do_val);
220 r = yp_match(pvt->nis_domain, group_byname, name, strlen(name),
221 &pvt->curval_data, &pvt->curval_len);
226 return (makegroupent(this));
229 static struct group *
230 gr_bygid(struct irs_gr *this, gid_t gid) {
231 struct pvt *pvt = (struct pvt *)this->private;
232 char tmp[sizeof "4294967295"];
235 nisfree(pvt, do_val);
236 (void) sprintf(tmp, "%u", (unsigned int)gid);
237 r = yp_match(pvt->nis_domain, group_bygid, tmp, strlen(tmp),
238 &pvt->curval_data, &pvt->curval_len);
243 return (makegroupent(this));
247 gr_rewind(struct irs_gr *this) {
248 struct pvt *pvt = (struct pvt *)this->private;
254 gr_minimize(struct irs_gr *this) {
261 static struct group *
262 makegroupent(struct irs_gr *this) {
263 struct pvt *pvt = (struct pvt *)this->private;
264 unsigned int num_members = 0;
268 if (pvt->group.gr_mem) {
269 free(pvt->group.gr_mem);
270 pvt->group.gr_mem = NULL;
275 pvt->membuf = pvt->curval_data;
276 pvt->curval_data = NULL;
279 pvt->group.gr_name = cp;
280 if (!(cp = strchr(cp, ':')))
284 pvt->group.gr_passwd = cp;
285 if (!(cp = strchr(cp, ':')))
290 t = strtoul(cp, NULL, 10);
293 pvt->group.gr_gid = (gid_t) t;
294 if (!(cp = strchr(cp, ':')))
298 if (*cp && cp[strlen(cp)-1] == '\n')
299 cp[strlen(cp)-1] = '\0';
302 * Parse the members out.
305 if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
307 new = realloc(pvt->group.gr_mem,
308 pvt->nmemb * sizeof(char *));
311 pvt->group.gr_mem = new;
313 pvt->group.gr_mem[num_members++] = cp;
314 if (!(cp = strchr(cp, ',')))
318 if (pvt->group.gr_mem == NULL) {
319 pvt->group.gr_mem = malloc(sizeof(char*));
320 if (!pvt->group.gr_mem)
324 pvt->group.gr_mem[num_members] = NULL;
326 return (&pvt->group);
329 if (pvt->group.gr_mem) {
330 free(pvt->group.gr_mem);
331 pvt->group.gr_mem = NULL;
342 nisfree(struct pvt *pvt, enum do_what do_what) {
343 if ((do_what & do_key) && pvt->curkey_data) {
344 free(pvt->curkey_data);
345 pvt->curkey_data = NULL;
347 if ((do_what & do_val) && pvt->curval_data) {
348 free(pvt->curval_data);
349 pvt->curval_data = NULL;
353 #endif /* WANT_IRS_GR && WANT_IRS_NIS */