nis_gr.c revision 1.1.1.1.14.1 1 1.1.1.1.14.1 yamt /* $NetBSD: nis_gr.c,v 1.1.1.1.14.1 2012/10/30 18:55:30 yamt Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1989, 1993, 1995
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. All advertising materials mentioning features or use of this software
16 1.1 christos * must display the following acknowledgement:
17 1.1 christos * This product includes software developed by the University of
18 1.1 christos * California, Berkeley and its contributors.
19 1.1 christos * 4. Neither the name of the University nor the names of its contributors
20 1.1 christos * may be used to endorse or promote products derived from this software
21 1.1 christos * without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 christos * SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /*
37 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38 1.1 christos * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
39 1.1 christos *
40 1.1 christos * Permission to use, copy, modify, and distribute this software for any
41 1.1 christos * purpose with or without fee is hereby granted, provided that the above
42 1.1 christos * copyright notice and this permission notice appear in all copies.
43 1.1 christos *
44 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
47 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 1.1 christos */
52 1.1 christos
53 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
54 1.1.1.1.14.1 yamt static const char rcsid[] = "Id: nis_gr.c,v 1.4 2005/04/27 04:56:32 sra Exp ";
55 1.1 christos /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
56 1.1 christos /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */
57 1.1 christos #endif /* LIBC_SCCS and not lint */
58 1.1 christos
59 1.1 christos /* Imports */
60 1.1 christos
61 1.1 christos #include "port_before.h"
62 1.1 christos
63 1.1 christos #if !defined(WANT_IRS_GR) || !defined(WANT_IRS_NIS)
64 1.1 christos static int __bind_irs_gr_unneeded;
65 1.1 christos #else
66 1.1 christos
67 1.1 christos #include <sys/param.h>
68 1.1 christos #include <sys/types.h>
69 1.1 christos #include <netinet/in.h>
70 1.1 christos #include <arpa/nameser.h>
71 1.1 christos #include <resolv.h>
72 1.1 christos #include <isc/memcluster.h>
73 1.1 christos
74 1.1 christos #include <rpc/rpc.h>
75 1.1 christos #include <rpc/xdr.h>
76 1.1 christos #include <rpcsvc/yp_prot.h>
77 1.1 christos #include <rpcsvc/ypclnt.h>
78 1.1 christos
79 1.1 christos #include <errno.h>
80 1.1 christos #include <grp.h>
81 1.1 christos #include <stdio.h>
82 1.1 christos #include <stdlib.h>
83 1.1 christos #include <string.h>
84 1.1 christos #include <unistd.h>
85 1.1 christos
86 1.1 christos #include <isc/memcluster.h>
87 1.1 christos
88 1.1 christos #include <irs.h>
89 1.1 christos
90 1.1 christos #include "port_after.h"
91 1.1 christos
92 1.1 christos #include "irs_p.h"
93 1.1 christos #include "nis_p.h"
94 1.1 christos
95 1.1 christos /* Definitions */
96 1.1 christos
97 1.1 christos struct pvt {
98 1.1 christos int needrewind;
99 1.1 christos char * nis_domain;
100 1.1 christos char * curkey_data;
101 1.1 christos int curkey_len;
102 1.1 christos char * curval_data;
103 1.1 christos int curval_len;
104 1.1 christos /*%<
105 1.1 christos * Need space to store the entries read from the group file.
106 1.1 christos * The members list also needs space per member, and the
107 1.1 christos * strings making up the user names must be allocated
108 1.1 christos * somewhere. Rather than doing lots of small allocations,
109 1.1 christos * we keep one buffer and resize it as needed.
110 1.1 christos */
111 1.1 christos struct group group;
112 1.1 christos size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */
113 1.1 christos char * membuf;
114 1.1 christos size_t membufsize;
115 1.1 christos };
116 1.1 christos
117 1.1 christos enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
118 1.1 christos
119 1.1 christos static /*const*/ char group_bygid[] = "group.bygid";
120 1.1 christos static /*const*/ char group_byname[] = "group.byname";
121 1.1 christos
122 1.1 christos /* Forward */
123 1.1 christos
124 1.1 christos static void gr_close(struct irs_gr *);
125 1.1 christos static struct group * gr_next(struct irs_gr *);
126 1.1 christos static struct group * gr_byname(struct irs_gr *, const char *);
127 1.1 christos static struct group * gr_bygid(struct irs_gr *, gid_t);
128 1.1 christos static void gr_rewind(struct irs_gr *);
129 1.1 christos static void gr_minimize(struct irs_gr *);
130 1.1 christos
131 1.1 christos static struct group * makegroupent(struct irs_gr *);
132 1.1 christos static void nisfree(struct pvt *, enum do_what);
133 1.1 christos
134 1.1 christos /* Public */
135 1.1 christos
136 1.1 christos struct irs_gr *
137 1.1 christos irs_nis_gr(struct irs_acc *this) {
138 1.1 christos struct irs_gr *gr;
139 1.1 christos struct pvt *pvt;
140 1.1 christos
141 1.1 christos if (!(gr = memget(sizeof *gr))) {
142 1.1 christos errno = ENOMEM;
143 1.1 christos return (NULL);
144 1.1 christos }
145 1.1 christos memset(gr, 0x5e, sizeof *gr);
146 1.1 christos if (!(pvt = memget(sizeof *pvt))) {
147 1.1 christos memput(gr, sizeof *gr);
148 1.1 christos errno = ENOMEM;
149 1.1 christos return (NULL);
150 1.1 christos }
151 1.1 christos memset(pvt, 0, sizeof *pvt);
152 1.1 christos pvt->needrewind = 1;
153 1.1 christos pvt->nis_domain = ((struct nis_p *)this->private)->domain;
154 1.1 christos gr->private = pvt;
155 1.1 christos gr->close = gr_close;
156 1.1 christos gr->next = gr_next;
157 1.1 christos gr->byname = gr_byname;
158 1.1 christos gr->bygid = gr_bygid;
159 1.1 christos gr->rewind = gr_rewind;
160 1.1 christos gr->list = make_group_list;
161 1.1 christos gr->minimize = gr_minimize;
162 1.1 christos gr->res_get = NULL;
163 1.1 christos gr->res_set = NULL;
164 1.1 christos return (gr);
165 1.1 christos }
166 1.1 christos
167 1.1 christos /* Methods */
168 1.1 christos
169 1.1 christos static void
170 1.1 christos gr_close(struct irs_gr *this) {
171 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
172 1.1 christos
173 1.1 christos if (pvt->group.gr_mem)
174 1.1 christos free(pvt->group.gr_mem);
175 1.1 christos if (pvt->membuf)
176 1.1 christos free(pvt->membuf);
177 1.1 christos memput(pvt, sizeof *pvt);
178 1.1 christos memput(this, sizeof *this);
179 1.1 christos }
180 1.1 christos
181 1.1 christos static struct group *
182 1.1 christos gr_next(struct irs_gr *this) {
183 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
184 1.1 christos struct group *rval;
185 1.1 christos int r;
186 1.1 christos
187 1.1 christos do {
188 1.1 christos if (pvt->needrewind) {
189 1.1 christos nisfree(pvt, do_all);
190 1.1 christos r = yp_first(pvt->nis_domain, group_byname,
191 1.1 christos &pvt->curkey_data, &pvt->curkey_len,
192 1.1 christos &pvt->curval_data, &pvt->curval_len);
193 1.1 christos pvt->needrewind = 0;
194 1.1 christos } else {
195 1.1 christos char *newkey_data;
196 1.1 christos int newkey_len;
197 1.1 christos
198 1.1 christos nisfree(pvt, do_val);
199 1.1 christos r = yp_next(pvt->nis_domain, group_byname,
200 1.1 christos pvt->curkey_data, pvt->curkey_len,
201 1.1 christos &newkey_data, &newkey_len,
202 1.1 christos &pvt->curval_data, &pvt->curval_len);
203 1.1 christos nisfree(pvt, do_key);
204 1.1 christos pvt->curkey_data = newkey_data;
205 1.1 christos pvt->curkey_len = newkey_len;
206 1.1 christos }
207 1.1 christos if (r != 0) {
208 1.1 christos errno = ENOENT;
209 1.1 christos return (NULL);
210 1.1 christos }
211 1.1 christos rval = makegroupent(this);
212 1.1 christos } while (rval == NULL);
213 1.1 christos return (rval);
214 1.1 christos }
215 1.1 christos
216 1.1 christos static struct group *
217 1.1 christos gr_byname(struct irs_gr *this, const char *name) {
218 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
219 1.1 christos int r;
220 1.1 christos
221 1.1 christos nisfree(pvt, do_val);
222 1.1 christos r = yp_match(pvt->nis_domain, group_byname, name, strlen(name),
223 1.1 christos &pvt->curval_data, &pvt->curval_len);
224 1.1 christos if (r != 0) {
225 1.1 christos errno = ENOENT;
226 1.1 christos return (NULL);
227 1.1 christos }
228 1.1 christos return (makegroupent(this));
229 1.1 christos }
230 1.1 christos
231 1.1 christos static struct group *
232 1.1 christos gr_bygid(struct irs_gr *this, gid_t gid) {
233 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
234 1.1 christos char tmp[sizeof "4294967295"];
235 1.1 christos int r;
236 1.1 christos
237 1.1 christos nisfree(pvt, do_val);
238 1.1 christos (void) sprintf(tmp, "%u", (unsigned int)gid);
239 1.1 christos r = yp_match(pvt->nis_domain, group_bygid, tmp, strlen(tmp),
240 1.1 christos &pvt->curval_data, &pvt->curval_len);
241 1.1 christos if (r != 0) {
242 1.1 christos errno = ENOENT;
243 1.1 christos return (NULL);
244 1.1 christos }
245 1.1 christos return (makegroupent(this));
246 1.1 christos }
247 1.1 christos
248 1.1 christos static void
249 1.1 christos gr_rewind(struct irs_gr *this) {
250 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
251 1.1 christos
252 1.1 christos pvt->needrewind = 1;
253 1.1 christos }
254 1.1 christos
255 1.1 christos static void
256 1.1 christos gr_minimize(struct irs_gr *this) {
257 1.1 christos UNUSED(this);
258 1.1 christos /* NOOP */
259 1.1 christos }
260 1.1 christos
261 1.1 christos /* Private */
262 1.1 christos
263 1.1 christos static struct group *
264 1.1 christos makegroupent(struct irs_gr *this) {
265 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
266 1.1 christos unsigned int num_members = 0;
267 1.1 christos char *cp, **new;
268 1.1 christos u_long t;
269 1.1 christos
270 1.1 christos if (pvt->group.gr_mem) {
271 1.1 christos free(pvt->group.gr_mem);
272 1.1 christos pvt->group.gr_mem = NULL;
273 1.1 christos pvt->nmemb = 0;
274 1.1 christos }
275 1.1 christos if (pvt->membuf)
276 1.1 christos free(pvt->membuf);
277 1.1 christos pvt->membuf = pvt->curval_data;
278 1.1 christos pvt->curval_data = NULL;
279 1.1 christos
280 1.1 christos cp = pvt->membuf;
281 1.1 christos pvt->group.gr_name = cp;
282 1.1 christos if (!(cp = strchr(cp, ':')))
283 1.1 christos goto cleanup;
284 1.1 christos *cp++ = '\0';
285 1.1 christos
286 1.1 christos pvt->group.gr_passwd = cp;
287 1.1 christos if (!(cp = strchr(cp, ':')))
288 1.1 christos goto cleanup;
289 1.1 christos *cp++ = '\0';
290 1.1 christos
291 1.1 christos errno = 0;
292 1.1 christos t = strtoul(cp, NULL, 10);
293 1.1 christos if (errno == ERANGE)
294 1.1 christos goto cleanup;
295 1.1 christos pvt->group.gr_gid = (gid_t) t;
296 1.1 christos if (!(cp = strchr(cp, ':')))
297 1.1 christos goto cleanup;
298 1.1 christos cp++;
299 1.1 christos
300 1.1 christos if (*cp && cp[strlen(cp)-1] == '\n')
301 1.1 christos cp[strlen(cp)-1] = '\0';
302 1.1 christos
303 1.1 christos /*
304 1.1 christos * Parse the members out.
305 1.1 christos */
306 1.1 christos while (*cp) {
307 1.1 christos if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
308 1.1 christos pvt->nmemb += 10;
309 1.1 christos new = realloc(pvt->group.gr_mem,
310 1.1 christos pvt->nmemb * sizeof(char *));
311 1.1 christos if (new == NULL)
312 1.1 christos goto cleanup;
313 1.1 christos pvt->group.gr_mem = new;
314 1.1 christos }
315 1.1 christos pvt->group.gr_mem[num_members++] = cp;
316 1.1 christos if (!(cp = strchr(cp, ',')))
317 1.1 christos break;
318 1.1 christos *cp++ = '\0';
319 1.1 christos }
320 1.1 christos if (pvt->group.gr_mem == NULL) {
321 1.1 christos pvt->group.gr_mem = malloc(sizeof(char*));
322 1.1 christos if (!pvt->group.gr_mem)
323 1.1 christos goto cleanup;
324 1.1 christos pvt->nmemb = 1;
325 1.1 christos }
326 1.1 christos pvt->group.gr_mem[num_members] = NULL;
327 1.1 christos
328 1.1 christos return (&pvt->group);
329 1.1 christos
330 1.1 christos cleanup:
331 1.1 christos if (pvt->group.gr_mem) {
332 1.1 christos free(pvt->group.gr_mem);
333 1.1 christos pvt->group.gr_mem = NULL;
334 1.1 christos pvt->nmemb = 0;
335 1.1 christos }
336 1.1 christos if (pvt->membuf) {
337 1.1 christos free(pvt->membuf);
338 1.1 christos pvt->membuf = NULL;
339 1.1 christos }
340 1.1 christos return (NULL);
341 1.1 christos }
342 1.1 christos
343 1.1 christos static void
344 1.1 christos nisfree(struct pvt *pvt, enum do_what do_what) {
345 1.1 christos if ((do_what & do_key) && pvt->curkey_data) {
346 1.1 christos free(pvt->curkey_data);
347 1.1 christos pvt->curkey_data = NULL;
348 1.1 christos }
349 1.1 christos if ((do_what & do_val) && pvt->curval_data) {
350 1.1 christos free(pvt->curval_data);
351 1.1 christos pvt->curval_data = NULL;
352 1.1 christos }
353 1.1 christos }
354 1.1 christos
355 1.1 christos #endif /* WANT_IRS_GR && WANT_IRS_NIS */
356 1.1 christos /*! \file */
357