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: nis_gr.c,v 1.4 2005/04/27 04:56:32 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"
63 #if !defined(WANT_IRS_GR) || !defined(WANT_IRS_NIS)
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>
72 #include <isc/memcluster.h>
76 #include <rpcsvc/yp_prot.h>
77 #include <rpcsvc/ypclnt.h>
86 #include <isc/memcluster.h>
90 #include "port_after.h"
105 * Need space to store the entries read from the group file.
106 * The members list also needs space per member, and the
107 * strings making up the user names must be allocated
108 * somewhere. Rather than doing lots of small allocations,
109 * we keep one buffer and resize it as needed.
112 size_t nmemb
; /*%< Malloc'd max index of gr_mem[]. */
117 enum do_what
{ do_none
= 0x0, do_key
= 0x1, do_val
= 0x2, do_all
= 0x3 };
119 static /*const*/ char group_bygid
[] = "group.bygid";
120 static /*const*/ char group_byname
[] = "group.byname";
124 static void gr_close(struct irs_gr
*);
125 static struct group
* gr_next(struct irs_gr
*);
126 static struct group
* gr_byname(struct irs_gr
*, const char *);
127 static struct group
* gr_bygid(struct irs_gr
*, gid_t
);
128 static void gr_rewind(struct irs_gr
*);
129 static void gr_minimize(struct irs_gr
*);
131 static struct group
* makegroupent(struct irs_gr
*);
132 static void nisfree(struct pvt
*, enum do_what
);
137 irs_nis_gr(struct irs_acc
*this) {
141 if (!(gr
= memget(sizeof *gr
))) {
145 memset(gr
, 0x5e, sizeof *gr
);
146 if (!(pvt
= memget(sizeof *pvt
))) {
147 memput(gr
, sizeof *gr
);
151 memset(pvt
, 0, sizeof *pvt
);
153 pvt
->nis_domain
= ((struct nis_p
*)this->private)->domain
;
155 gr
->close
= gr_close
;
157 gr
->byname
= gr_byname
;
158 gr
->bygid
= gr_bygid
;
159 gr
->rewind
= gr_rewind
;
160 gr
->list
= make_group_list
;
161 gr
->minimize
= gr_minimize
;
170 gr_close(struct irs_gr
*this) {
171 struct pvt
*pvt
= (struct pvt
*)this->private;
173 if (pvt
->group
.gr_mem
)
174 free(pvt
->group
.gr_mem
);
177 memput(pvt
, sizeof *pvt
);
178 memput(this, sizeof *this);
181 static struct group
*
182 gr_next(struct irs_gr
*this) {
183 struct pvt
*pvt
= (struct pvt
*)this->private;
188 if (pvt
->needrewind
) {
189 nisfree(pvt
, do_all
);
190 r
= yp_first(pvt
->nis_domain
, group_byname
,
191 &pvt
->curkey_data
, &pvt
->curkey_len
,
192 &pvt
->curval_data
, &pvt
->curval_len
);
198 nisfree(pvt
, do_val
);
199 r
= yp_next(pvt
->nis_domain
, group_byname
,
200 pvt
->curkey_data
, pvt
->curkey_len
,
201 &newkey_data
, &newkey_len
,
202 &pvt
->curval_data
, &pvt
->curval_len
);
203 nisfree(pvt
, do_key
);
204 pvt
->curkey_data
= newkey_data
;
205 pvt
->curkey_len
= newkey_len
;
211 rval
= makegroupent(this);
212 } while (rval
== NULL
);
216 static struct group
*
217 gr_byname(struct irs_gr
*this, const char *name
) {
218 struct pvt
*pvt
= (struct pvt
*)this->private;
221 nisfree(pvt
, do_val
);
222 r
= yp_match(pvt
->nis_domain
, group_byname
, name
, strlen(name
),
223 &pvt
->curval_data
, &pvt
->curval_len
);
228 return (makegroupent(this));
231 static struct group
*
232 gr_bygid(struct irs_gr
*this, gid_t gid
) {
233 struct pvt
*pvt
= (struct pvt
*)this->private;
234 char tmp
[sizeof "4294967295"];
237 nisfree(pvt
, do_val
);
238 (void) sprintf(tmp
, "%u", (unsigned int)gid
);
239 r
= yp_match(pvt
->nis_domain
, group_bygid
, tmp
, strlen(tmp
),
240 &pvt
->curval_data
, &pvt
->curval_len
);
245 return (makegroupent(this));
249 gr_rewind(struct irs_gr
*this) {
250 struct pvt
*pvt
= (struct pvt
*)this->private;
256 gr_minimize(struct irs_gr
*this) {
263 static struct group
*
264 makegroupent(struct irs_gr
*this) {
265 struct pvt
*pvt
= (struct pvt
*)this->private;
266 unsigned int num_members
= 0;
270 if (pvt
->group
.gr_mem
) {
271 free(pvt
->group
.gr_mem
);
272 pvt
->group
.gr_mem
= NULL
;
277 pvt
->membuf
= pvt
->curval_data
;
278 pvt
->curval_data
= NULL
;
281 pvt
->group
.gr_name
= cp
;
282 if (!(cp
= strchr(cp
, ':')))
286 pvt
->group
.gr_passwd
= cp
;
287 if (!(cp
= strchr(cp
, ':')))
292 t
= strtoul(cp
, NULL
, 10);
295 pvt
->group
.gr_gid
= (gid_t
) t
;
296 if (!(cp
= strchr(cp
, ':')))
300 if (*cp
&& cp
[strlen(cp
)-1] == '\n')
301 cp
[strlen(cp
)-1] = '\0';
304 * Parse the members out.
307 if (num_members
+1 >= pvt
->nmemb
|| pvt
->group
.gr_mem
== NULL
) {
309 new = realloc(pvt
->group
.gr_mem
,
310 pvt
->nmemb
* sizeof(char *));
313 pvt
->group
.gr_mem
= new;
315 pvt
->group
.gr_mem
[num_members
++] = cp
;
316 if (!(cp
= strchr(cp
, ',')))
320 if (pvt
->group
.gr_mem
== NULL
) {
321 pvt
->group
.gr_mem
= malloc(sizeof(char*));
322 if (!pvt
->group
.gr_mem
)
326 pvt
->group
.gr_mem
[num_members
] = NULL
;
328 return (&pvt
->group
);
331 if (pvt
->group
.gr_mem
) {
332 free(pvt
->group
.gr_mem
);
333 pvt
->group
.gr_mem
= NULL
;
344 nisfree(struct pvt
*pvt
, enum do_what do_what
) {
345 if ((do_what
& do_key
) && pvt
->curkey_data
) {
346 free(pvt
->curkey_data
);
347 pvt
->curkey_data
= NULL
;
349 if ((do_what
& do_val
) && pvt
->curval_data
) {
350 free(pvt
->curval_data
);
351 pvt
->curval_data
= NULL
;
355 #endif /* WANT_IRS_GR && WANT_IRS_NIS */