getnetgrent.c revision 1.40 1 1.40 rtr /* $NetBSD: getnetgrent.c,v 1.40 2008/04/05 08:01:54 rtr Exp $ */
2 1.8 cgd
3 1.1 mycroft /*
4 1.4 christos * Copyright (c) 1994 Christos Zoulas
5 1.4 christos * All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.4 christos * This product includes software developed by Christos Zoulas.
18 1.4 christos * 4. The name of the author may not be used to endorse or promote products
19 1.4 christos * derived from this software without specific prior written permission.
20 1.1 mycroft *
21 1.4 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.4 christos * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.4 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.4 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.4 christos * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 mycroft * SUCH DAMAGE.
32 1.1 mycroft */
33 1.1 mycroft
34 1.12 christos #include <sys/cdefs.h>
35 1.1 mycroft #if defined(LIBC_SCCS) && !defined(lint)
36 1.40 rtr __RCSID("$NetBSD: getnetgrent.c,v 1.40 2008/04/05 08:01:54 rtr Exp $");
37 1.1 mycroft #endif /* LIBC_SCCS and not lint */
38 1.1 mycroft
39 1.12 christos #include "namespace.h"
40 1.9 christos #include <sys/types.h>
41 1.24 lukem
42 1.24 lukem #include <assert.h>
43 1.24 lukem #include <ctype.h>
44 1.24 lukem #include <db.h>
45 1.24 lukem #include <err.h>
46 1.24 lukem #include <fcntl.h>
47 1.9 christos #define _NETGROUP_PRIVATE
48 1.33 christos #include <stringlist.h>
49 1.4 christos #include <netgroup.h>
50 1.17 lukem #include <nsswitch.h>
51 1.29 wiz #include <stdarg.h>
52 1.24 lukem #include <stdio.h>
53 1.3 cgd #include <stdlib.h>
54 1.24 lukem #include <string.h>
55 1.24 lukem
56 1.10 cgd #ifdef YP
57 1.17 lukem #include <rpc/rpc.h>
58 1.10 cgd #include <rpcsvc/ypclnt.h>
59 1.17 lukem #include <rpcsvc/yp_prot.h>
60 1.23 lukem #endif
61 1.23 lukem
62 1.13 jtc #ifdef __weak_alias
63 1.27 mycroft __weak_alias(endnetgrent,_endnetgrent)
64 1.27 mycroft __weak_alias(getnetgrent,_getnetgrent)
65 1.27 mycroft __weak_alias(innetgr,_innetgr)
66 1.27 mycroft __weak_alias(setnetgrent,_setnetgrent)
67 1.10 cgd #endif
68 1.4 christos
69 1.4 christos #define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s)
70 1.9 christos #define _NG_EMPTY(s) ((s) == NULL ? "" : s)
71 1.4 christos #define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n')
72 1.1 mycroft
73 1.4 christos static const char _ngstar[] = "*";
74 1.30 christos static struct netgroup *_nghead = NULL;
75 1.30 christos static struct netgroup *_nglist = NULL;
76 1.4 christos static DB *_ng_db;
77 1.1 mycroft
78 1.30 christos static int getstring(char **, int, __aconst char **);
79 1.30 christos static struct netgroup *getnetgroup(char **);
80 1.30 christos static int lookup(char *, char **, int);
81 1.30 christos static int addgroup(StringList *, char *);
82 1.30 christos static int in_check(const char *, const char *, const char *,
83 1.30 christos struct netgroup *);
84 1.30 christos static int in_find(StringList *, char *, const char *, const char *,
85 1.30 christos const char *);
86 1.30 christos static char *in_lookup1(const char *, const char *, int);
87 1.30 christos static int in_lookup(const char *, const char *, const char *, int);
88 1.4 christos
89 1.32 christos #ifdef NSSRC_FILES
90 1.21 thorpej static const ns_src default_files_nis[] = {
91 1.22 lukem { NSSRC_FILES, NS_SUCCESS | NS_NOTFOUND },
92 1.21 thorpej #ifdef YP
93 1.21 thorpej { NSSRC_NIS, NS_SUCCESS },
94 1.21 thorpej #endif
95 1.37 christos { 0, 0 },
96 1.21 thorpej };
97 1.32 christos #endif
98 1.21 thorpej
99 1.4 christos /*
100 1.4 christos * getstring(): Get a string delimited by the character, skipping leading and
101 1.4 christos * trailing blanks and advancing the pointer
102 1.4 christos */
103 1.6 christos static int
104 1.30 christos getstring(char **pp, int del, char __aconst **str)
105 1.4 christos {
106 1.14 perry size_t len;
107 1.4 christos char *sp, *ep, *dp;
108 1.4 christos
109 1.24 lukem _DIAGASSERT(pp != NULL);
110 1.24 lukem _DIAGASSERT(str != NULL);
111 1.24 lukem
112 1.4 christos /* skip leading blanks */
113 1.4 christos for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
114 1.4 christos continue;
115 1.4 christos
116 1.4 christos /* accumulate till delimiter or space */
117 1.4 christos for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
118 1.4 christos continue;
119 1.4 christos
120 1.4 christos /* hunt for the delimiter */
121 1.4 christos for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
122 1.4 christos continue;
123 1.4 christos
124 1.6 christos if (*dp != del) {
125 1.6 christos *str = NULL;
126 1.6 christos return 0;
127 1.6 christos }
128 1.4 christos
129 1.4 christos *pp = ++dp;
130 1.4 christos
131 1.14 perry len = (ep - sp) + 1;
132 1.14 perry if (len > 1) {
133 1.14 perry dp = malloc(len);
134 1.6 christos if (dp == NULL)
135 1.26 lukem return 0;
136 1.30 christos (void)memcpy(dp, sp, len);
137 1.14 perry dp[len - 1] = '\0';
138 1.6 christos } else
139 1.6 christos dp = NULL;
140 1.1 mycroft
141 1.6 christos *str = dp;
142 1.6 christos return 1;
143 1.4 christos }
144 1.4 christos
145 1.4 christos
146 1.4 christos /*
147 1.4 christos * getnetgroup(): Parse a netgroup, and advance the pointer
148 1.4 christos */
149 1.4 christos static struct netgroup *
150 1.4 christos getnetgroup(pp)
151 1.4 christos char **pp;
152 1.4 christos {
153 1.24 lukem struct netgroup *ng;
154 1.24 lukem
155 1.24 lukem _DIAGASSERT(pp != NULL);
156 1.24 lukem _DIAGASSERT(*pp != NULL);
157 1.4 christos
158 1.24 lukem ng = malloc(sizeof(struct netgroup));
159 1.4 christos if (ng == NULL)
160 1.26 lukem return NULL;
161 1.4 christos
162 1.4 christos (*pp)++; /* skip '(' */
163 1.6 christos if (!getstring(pp, ',', &ng->ng_host))
164 1.4 christos goto badhost;
165 1.4 christos
166 1.6 christos if (!getstring(pp, ',', &ng->ng_user))
167 1.4 christos goto baduser;
168 1.4 christos
169 1.6 christos if (!getstring(pp, ')', &ng->ng_domain))
170 1.4 christos goto baddomain;
171 1.4 christos
172 1.4 christos #ifdef DEBUG_NG
173 1.9 christos {
174 1.9 christos char buf[1024];
175 1.9 christos (void) fprintf(stderr, "netgroup %s\n",
176 1.9 christos _ng_print(buf, sizeof(buf), ng));
177 1.9 christos }
178 1.4 christos #endif
179 1.4 christos return ng;
180 1.4 christos
181 1.4 christos baddomain:
182 1.6 christos if (ng->ng_user)
183 1.30 christos free(ng->ng_user);
184 1.4 christos baduser:
185 1.6 christos if (ng->ng_host)
186 1.30 christos free(ng->ng_host);
187 1.4 christos badhost:
188 1.4 christos free(ng);
189 1.4 christos return NULL;
190 1.4 christos }
191 1.4 christos
192 1.35 dogcow void
193 1.32 christos _ng_cycle(const char *grp, const StringList *sl)
194 1.32 christos {
195 1.32 christos size_t i;
196 1.32 christos warnx("netgroup: Cycle in group `%s'", grp);
197 1.32 christos (void)fprintf(stderr, "groups: ");
198 1.32 christos for (i = 0; i < sl->sl_cur; i++)
199 1.32 christos (void)fprintf(stderr, "%s ", sl->sl_str[i]);
200 1.32 christos (void)fprintf(stderr, "\n");
201 1.32 christos }
202 1.4 christos
203 1.30 christos static int _local_lookup(void *, void *, va_list);
204 1.17 lukem
205 1.18 christos /*ARGSUSED*/
206 1.4 christos static int
207 1.30 christos _local_lookup(void *rv, void *cb_data, va_list ap)
208 1.17 lukem {
209 1.17 lukem char *name = va_arg(ap, char *);
210 1.17 lukem char **line = va_arg(ap, char **);
211 1.17 lukem int bywhat = va_arg(ap, int);
212 1.17 lukem
213 1.17 lukem DBT key, data;
214 1.17 lukem size_t len;
215 1.17 lukem char *ks;
216 1.17 lukem int r;
217 1.4 christos
218 1.17 lukem if (_ng_db == NULL)
219 1.17 lukem return NS_UNAVAIL;
220 1.4 christos
221 1.17 lukem len = strlen(name) + 2;
222 1.17 lukem ks = malloc(len);
223 1.17 lukem if (ks == NULL)
224 1.26 lukem return NS_UNAVAIL;
225 1.4 christos
226 1.17 lukem ks[0] = bywhat;
227 1.30 christos (void)memcpy(&ks[1], name, len - 1);
228 1.4 christos
229 1.30 christos key.data = (u_char *)ks;
230 1.17 lukem key.size = len;
231 1.4 christos
232 1.30 christos r = (*_ng_db->get)(_ng_db, &key, &data, 0);
233 1.17 lukem free(ks);
234 1.17 lukem switch (r) {
235 1.17 lukem case 0:
236 1.17 lukem break;
237 1.17 lukem case 1:
238 1.17 lukem return NS_NOTFOUND;
239 1.17 lukem case -1:
240 1.22 lukem /* XXX: call endnetgrent() here ? */
241 1.17 lukem return NS_UNAVAIL;
242 1.17 lukem }
243 1.17 lukem
244 1.17 lukem *line = strdup(data.data);
245 1.17 lukem if (*line == NULL)
246 1.17 lukem return NS_UNAVAIL;
247 1.17 lukem return NS_SUCCESS;
248 1.17 lukem }
249 1.4 christos
250 1.4 christos #ifdef YP
251 1.30 christos static int _nis_lookup(void *, void *, va_list);
252 1.4 christos
253 1.18 christos /*ARGSUSED*/
254 1.17 lukem static int
255 1.30 christos _nis_lookup(void *rv, void *cb_data, va_list ap)
256 1.17 lukem {
257 1.17 lukem char *name = va_arg(ap, char *);
258 1.17 lukem char **line = va_arg(ap, char **);
259 1.17 lukem int bywhat = va_arg(ap, int);
260 1.17 lukem
261 1.17 lukem static char *__ypdomain;
262 1.17 lukem int i;
263 1.30 christos const char *map = NULL;
264 1.4 christos
265 1.17 lukem if(__ypdomain == NULL) {
266 1.17 lukem switch (yp_get_default_domain(&__ypdomain)) {
267 1.17 lukem case 0:
268 1.6 christos break;
269 1.17 lukem case YPERR_RESRC:
270 1.17 lukem return NS_TRYAGAIN;
271 1.6 christos default:
272 1.17 lukem return NS_UNAVAIL;
273 1.6 christos }
274 1.17 lukem }
275 1.17 lukem
276 1.17 lukem switch (bywhat) {
277 1.17 lukem case _NG_KEYBYNAME:
278 1.17 lukem map = "netgroup";
279 1.17 lukem break;
280 1.17 lukem
281 1.17 lukem case _NG_KEYBYUSER:
282 1.17 lukem map = "netgroup.byuser";
283 1.17 lukem break;
284 1.17 lukem
285 1.17 lukem case _NG_KEYBYHOST:
286 1.17 lukem map = "netgroup.byhost";
287 1.17 lukem break;
288 1.17 lukem
289 1.17 lukem default:
290 1.17 lukem abort();
291 1.17 lukem }
292 1.4 christos
293 1.17 lukem *line = NULL;
294 1.17 lukem switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
295 1.17 lukem case 0:
296 1.17 lukem return NS_SUCCESS;
297 1.17 lukem case YPERR_KEY:
298 1.17 lukem if (*line)
299 1.17 lukem free(*line);
300 1.17 lukem return NS_NOTFOUND;
301 1.17 lukem default:
302 1.17 lukem if (*line)
303 1.17 lukem free(*line);
304 1.17 lukem return NS_UNAVAIL;
305 1.6 christos }
306 1.17 lukem /* NOTREACHED */
307 1.17 lukem }
308 1.4 christos #endif
309 1.4 christos
310 1.32 christos #ifdef NSSRC_FILES
311 1.17 lukem /*
312 1.17 lukem * lookup(): Find the given key in the database or yp, and return its value
313 1.17 lukem * in *line; returns 1 if key was found, 0 otherwise
314 1.17 lukem */
315 1.17 lukem static int
316 1.30 christos lookup(char *name, char **line, int bywhat)
317 1.17 lukem {
318 1.17 lukem int r;
319 1.20 lukem static const ns_dtab dtab[] = {
320 1.19 lukem NS_FILES_CB(_local_lookup, NULL)
321 1.21 thorpej NS_NIS_CB(_nis_lookup, NULL)
322 1.37 christos NS_NULL_CB
323 1.17 lukem };
324 1.17 lukem
325 1.24 lukem _DIAGASSERT(name != NULL);
326 1.24 lukem _DIAGASSERT(line != NULL);
327 1.24 lukem
328 1.21 thorpej r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "lookup", default_files_nis,
329 1.19 lukem name, line, bywhat);
330 1.17 lukem return (r == NS_SUCCESS) ? 1 : 0;
331 1.1 mycroft }
332 1.32 christos #else
333 1.32 christos static int
334 1.32 christos _local_lookupv(int *rv, void *cbdata, ...)
335 1.32 christos {
336 1.32 christos int e;
337 1.32 christos va_list ap;
338 1.32 christos va_start(ap, cbdata);
339 1.32 christos e = _local_lookup(rv, cbdata, ap);
340 1.32 christos va_end(ap);
341 1.32 christos return e;
342 1.32 christos }
343 1.32 christos
344 1.32 christos static int
345 1.32 christos lookup(name, line, bywhat)
346 1.32 christos char *name;
347 1.32 christos char **line;
348 1.32 christos int bywhat;
349 1.32 christos {
350 1.32 christos return _local_lookupv(NULL, NULL, name, line, bywhat) == NS_SUCCESS;
351 1.32 christos }
352 1.32 christos #endif
353 1.1 mycroft
354 1.1 mycroft /*
355 1.4 christos * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
356 1.4 christos * line was empty or a comment _NG_GROUP: line had a netgroup definition,
357 1.4 christos * returned in ng _NG_NAME: line had a netgroup name, returned in name
358 1.4 christos *
359 1.4 christos * Public since used by netgroup_mkdb
360 1.1 mycroft */
361 1.1 mycroft int
362 1.30 christos _ng_parse(char **p, char **name, struct netgroup **ng)
363 1.1 mycroft {
364 1.24 lukem
365 1.24 lukem _DIAGASSERT(p != NULL);
366 1.24 lukem _DIAGASSERT(*p != NULL);
367 1.24 lukem _DIAGASSERT(name != NULL);
368 1.24 lukem _DIAGASSERT(ng != NULL);
369 1.24 lukem
370 1.4 christos while (**p) {
371 1.4 christos if (**p == '#')
372 1.4 christos /* comment */
373 1.4 christos return _NG_NONE;
374 1.4 christos
375 1.4 christos while (**p && _NG_ISSPACE(**p))
376 1.4 christos /* skipblank */
377 1.4 christos (*p)++;
378 1.4 christos
379 1.4 christos if (**p == '(') {
380 1.26 lukem if ((*ng = getnetgroup(p)) == NULL)
381 1.4 christos return _NG_ERROR;
382 1.4 christos return _NG_GROUP;
383 1.4 christos } else {
384 1.17 lukem char *np;
385 1.17 lukem size_t i;
386 1.1 mycroft
387 1.4 christos for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
388 1.4 christos continue;
389 1.4 christos if (np != *p) {
390 1.4 christos i = (*p - np) + 1;
391 1.4 christos *name = malloc(i);
392 1.4 christos if (*name == NULL)
393 1.26 lukem return _NG_ERROR;
394 1.30 christos (void)memcpy(*name, np, i);
395 1.4 christos (*name)[i - 1] = '\0';
396 1.4 christos return _NG_NAME;
397 1.4 christos }
398 1.4 christos }
399 1.1 mycroft }
400 1.4 christos return _NG_NONE;
401 1.1 mycroft }
402 1.1 mycroft
403 1.4 christos
404 1.1 mycroft /*
405 1.26 lukem * addgroup(): Recursively add all the members of the netgroup to this group.
406 1.26 lukem * returns 0 upon failure, nonzero upon success.
407 1.26 lukem * grp is not a valid pointer after return (either free(3)ed or allocated
408 1.26 lukem * to a stringlist). in either case, it shouldn't be used again.
409 1.1 mycroft */
410 1.26 lukem static int
411 1.30 christos addgroup(StringList *sl, char *grp)
412 1.1 mycroft {
413 1.4 christos char *line, *p;
414 1.4 christos struct netgroup *ng;
415 1.4 christos char *name;
416 1.4 christos
417 1.24 lukem _DIAGASSERT(sl != NULL);
418 1.24 lukem _DIAGASSERT(grp != NULL);
419 1.24 lukem
420 1.4 christos #ifdef DEBUG_NG
421 1.30 christos (void)fprintf(stderr, "addgroup(%s)\n", grp);
422 1.4 christos #endif
423 1.4 christos /* check for cycles */
424 1.11 lukem if (sl_find(sl, grp) != NULL) {
425 1.32 christos _ng_cycle(grp, sl);
426 1.6 christos free(grp);
427 1.26 lukem return 0;
428 1.26 lukem }
429 1.26 lukem if (sl_add(sl, grp) == -1) {
430 1.26 lukem free(grp);
431 1.26 lukem return 0;
432 1.4 christos }
433 1.4 christos
434 1.4 christos /* Lookup this netgroup */
435 1.17 lukem line = NULL;
436 1.17 lukem if (!lookup(grp, &line, _NG_KEYBYNAME)) {
437 1.32 christos if (line)
438 1.17 lukem free(line);
439 1.26 lukem return 0;
440 1.17 lukem }
441 1.4 christos
442 1.4 christos p = line;
443 1.4 christos
444 1.4 christos for (;;) {
445 1.4 christos switch (_ng_parse(&p, &name, &ng)) {
446 1.4 christos case _NG_NONE:
447 1.4 christos /* Done with the line */
448 1.4 christos free(line);
449 1.26 lukem return 1;
450 1.4 christos
451 1.4 christos case _NG_GROUP:
452 1.4 christos /* new netgroup */
453 1.4 christos /* add to the list */
454 1.4 christos ng->ng_next = _nglist;
455 1.4 christos _nglist = ng;
456 1.4 christos break;
457 1.4 christos
458 1.4 christos case _NG_NAME:
459 1.4 christos /* netgroup name */
460 1.26 lukem if (!addgroup(sl, name))
461 1.26 lukem return 0;
462 1.4 christos break;
463 1.1 mycroft
464 1.4 christos case _NG_ERROR:
465 1.26 lukem return 0;
466 1.4 christos
467 1.4 christos default:
468 1.4 christos abort();
469 1.4 christos }
470 1.1 mycroft }
471 1.1 mycroft }
472 1.1 mycroft
473 1.4 christos
474 1.1 mycroft /*
475 1.4 christos * in_check(): Compare the spec with the netgroup
476 1.1 mycroft */
477 1.4 christos static int
478 1.30 christos in_check(const char *host, const char *user, const char *domain,
479 1.30 christos struct netgroup *ng)
480 1.1 mycroft {
481 1.24 lukem
482 1.24 lukem /* host may be NULL */
483 1.24 lukem /* user may be NULL */
484 1.24 lukem /* domain may be NULL */
485 1.24 lukem _DIAGASSERT(ng != NULL);
486 1.24 lukem
487 1.6 christos if ((host != NULL) && (ng->ng_host != NULL)
488 1.4 christos && strcmp(ng->ng_host, host) != 0)
489 1.4 christos return 0;
490 1.4 christos
491 1.6 christos if ((user != NULL) && (ng->ng_user != NULL)
492 1.4 christos && strcmp(ng->ng_user, user) != 0)
493 1.4 christos return 0;
494 1.4 christos
495 1.6 christos if ((domain != NULL) && (ng->ng_domain != NULL)
496 1.4 christos && strcmp(ng->ng_domain, domain) != 0)
497 1.4 christos return 0;
498 1.1 mycroft
499 1.4 christos return 1;
500 1.1 mycroft }
501 1.1 mycroft
502 1.4 christos
503 1.1 mycroft /*
504 1.26 lukem * in_find(): Find a match for the host, user, domain spec.
505 1.26 lukem * grp is not a valid pointer after return (either free(3)ed or allocated
506 1.26 lukem * to a stringlist). in either case, it shouldn't be used again.
507 1.1 mycroft */
508 1.1 mycroft static int
509 1.30 christos in_find(StringList *sl, char *grp, const char *host, const char *user,
510 1.30 christos const char *domain)
511 1.1 mycroft {
512 1.4 christos char *line, *p;
513 1.4 christos int i;
514 1.4 christos struct netgroup *ng;
515 1.4 christos char *name;
516 1.4 christos
517 1.24 lukem _DIAGASSERT(sl != NULL);
518 1.24 lukem _DIAGASSERT(grp != NULL);
519 1.24 lukem /* host may be NULL */
520 1.24 lukem /* user may be NULL */
521 1.24 lukem /* domain may be NULL */
522 1.24 lukem
523 1.4 christos #ifdef DEBUG_NG
524 1.30 christos (void)fprintf(stderr, "in_find(%s)\n", grp);
525 1.4 christos #endif
526 1.4 christos /* check for cycles */
527 1.11 lukem if (sl_find(sl, grp) != NULL) {
528 1.32 christos _ng_cycle(grp, sl);
529 1.6 christos free(grp);
530 1.4 christos return 0;
531 1.4 christos }
532 1.26 lukem if (sl_add(sl, grp) == -1) {
533 1.26 lukem free(grp);
534 1.26 lukem return 0;
535 1.26 lukem }
536 1.1 mycroft
537 1.4 christos /* Lookup this netgroup */
538 1.17 lukem line = NULL;
539 1.17 lukem if (!lookup(grp, &line, _NG_KEYBYNAME)) {
540 1.17 lukem if (line)
541 1.17 lukem free(line);
542 1.4 christos return 0;
543 1.17 lukem }
544 1.4 christos
545 1.4 christos p = line;
546 1.4 christos
547 1.4 christos for (;;) {
548 1.4 christos switch (_ng_parse(&p, &name, &ng)) {
549 1.4 christos case _NG_NONE:
550 1.4 christos /* Done with the line */
551 1.4 christos free(line);
552 1.4 christos return 0;
553 1.4 christos
554 1.4 christos case _NG_GROUP:
555 1.4 christos /* new netgroup */
556 1.4 christos i = in_check(host, user, domain, ng);
557 1.6 christos if (ng->ng_host != NULL)
558 1.30 christos free(ng->ng_host);
559 1.6 christos if (ng->ng_user != NULL)
560 1.30 christos free(ng->ng_user);
561 1.6 christos if (ng->ng_domain != NULL)
562 1.30 christos free(ng->ng_domain);
563 1.4 christos free(ng);
564 1.4 christos if (i) {
565 1.4 christos free(line);
566 1.4 christos return 1;
567 1.4 christos }
568 1.1 mycroft break;
569 1.4 christos
570 1.4 christos case _NG_NAME:
571 1.4 christos /* netgroup name */
572 1.17 lukem if (in_find(sl, name, host, user, domain)) {
573 1.4 christos free(line);
574 1.4 christos return 1;
575 1.1 mycroft }
576 1.4 christos break;
577 1.4 christos
578 1.4 christos case _NG_ERROR:
579 1.4 christos free(line);
580 1.4 christos return 0;
581 1.4 christos
582 1.4 christos default:
583 1.4 christos abort();
584 1.1 mycroft }
585 1.1 mycroft }
586 1.4 christos }
587 1.4 christos
588 1.4 christos /*
589 1.4 christos * _ng_makekey(): Make a key from the two names given. The key is of the form
590 1.4 christos * <name1>.<name2> Names strings are replaced with * if they are empty;
591 1.26 lukem * Returns NULL if there's a problem.
592 1.4 christos */
593 1.4 christos char *
594 1.30 christos _ng_makekey(const char *s1, const char *s2, size_t len)
595 1.4 christos {
596 1.24 lukem char *buf;
597 1.24 lukem
598 1.24 lukem /* s1 may be NULL */
599 1.24 lukem /* s2 may be NULL */
600 1.24 lukem
601 1.24 lukem buf = malloc(len);
602 1.26 lukem if (buf != NULL)
603 1.30 christos (void)snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
604 1.4 christos return buf;
605 1.9 christos }
606 1.9 christos
607 1.9 christos void
608 1.30 christos _ng_print(char *buf, size_t len, const struct netgroup *ng)
609 1.9 christos {
610 1.24 lukem _DIAGASSERT(buf != NULL);
611 1.24 lukem _DIAGASSERT(ng != NULL);
612 1.24 lukem
613 1.30 christos (void)snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
614 1.9 christos _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
615 1.4 christos }
616 1.4 christos
617 1.4 christos
618 1.4 christos /*
619 1.4 christos * in_lookup1(): Fast lookup for a key in the appropriate map
620 1.4 christos */
621 1.4 christos static char *
622 1.30 christos in_lookup1(const char *key, const char *domain, int map)
623 1.4 christos {
624 1.4 christos char *line;
625 1.4 christos size_t len;
626 1.4 christos char *ptr;
627 1.4 christos int res;
628 1.4 christos
629 1.24 lukem /* key may be NULL */
630 1.24 lukem /* domain may be NULL */
631 1.24 lukem
632 1.4 christos len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
633 1.4 christos ptr = _ng_makekey(key, domain, len);
634 1.26 lukem if (ptr == NULL)
635 1.26 lukem return NULL;
636 1.17 lukem res = lookup(ptr, &line, map);
637 1.4 christos free(ptr);
638 1.4 christos return res ? line : NULL;
639 1.4 christos }
640 1.4 christos
641 1.4 christos
642 1.4 christos /*
643 1.4 christos * in_lookup(): Fast lookup for a key in the appropriate map
644 1.4 christos */
645 1.4 christos static int
646 1.30 christos in_lookup(const char *group, const char *key, const char *domain, int map)
647 1.4 christos {
648 1.4 christos size_t len;
649 1.4 christos char *ptr, *line;
650 1.4 christos
651 1.24 lukem _DIAGASSERT(group != NULL);
652 1.24 lukem /* key may be NULL */
653 1.24 lukem /* domain may be NULL */
654 1.24 lukem
655 1.4 christos if (domain != NULL) {
656 1.4 christos /* Domain specified; look in "group.domain" and "*.domain" */
657 1.17 lukem if ((line = in_lookup1(key, domain, map)) == NULL)
658 1.17 lukem line = in_lookup1(NULL, domain, map);
659 1.26 lukem } else
660 1.4 christos line = NULL;
661 1.4 christos
662 1.4 christos if (line == NULL) {
663 1.30 christos /*
664 1.30 christos * domain not specified or domain lookup failed; look in
665 1.30 christos * "group.*" and "*.*"
666 1.30 christos */
667 1.17 lukem if (((line = in_lookup1(key, NULL, map)) == NULL) &&
668 1.17 lukem ((line = in_lookup1(NULL, NULL, map)) == NULL))
669 1.4 christos return 0;
670 1.4 christos }
671 1.4 christos
672 1.4 christos len = strlen(group);
673 1.4 christos
674 1.4 christos for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
675 1.4 christos /* Make sure we did not find a substring */
676 1.4 christos if ((ptr != line && ptr[-1] != ',') ||
677 1.4 christos (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
678 1.4 christos ptr++;
679 1.4 christos else {
680 1.4 christos free(line);
681 1.4 christos return 1;
682 1.1 mycroft }
683 1.4 christos
684 1.4 christos free(line);
685 1.4 christos return 0;
686 1.4 christos }
687 1.4 christos
688 1.38 oster /*ARGSUSED*/
689 1.38 oster static int
690 1.39 oster _local_endnetgrent(void *rv, void *cb_data, va_list ap)
691 1.4 christos {
692 1.4 christos for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
693 1.4 christos _nghead = _nglist->ng_next;
694 1.6 christos if (_nglist->ng_host != NULL)
695 1.30 christos free(_nglist->ng_host);
696 1.6 christos if (_nglist->ng_user != NULL)
697 1.30 christos free(_nglist->ng_user);
698 1.6 christos if (_nglist->ng_domain != NULL)
699 1.30 christos free(_nglist->ng_domain);
700 1.4 christos free(_nglist);
701 1.4 christos }
702 1.4 christos
703 1.4 christos if (_ng_db) {
704 1.30 christos (void)(*_ng_db->close)(_ng_db);
705 1.4 christos _ng_db = NULL;
706 1.4 christos }
707 1.38 oster
708 1.38 oster return NS_SUCCESS;
709 1.4 christos }
710 1.4 christos
711 1.38 oster /*ARGSUSED*/
712 1.38 oster static int
713 1.39 oster _local_setnetgrent(void *rv, void *cb_data, va_list ap)
714 1.4 christos {
715 1.38 oster const char *ng = va_arg(ap, const char *);
716 1.26 lukem StringList *sl;
717 1.17 lukem char *ng_copy;
718 1.4 christos
719 1.24 lukem _DIAGASSERT(ng != NULL);
720 1.24 lukem
721 1.26 lukem sl = sl_init();
722 1.26 lukem if (sl == NULL)
723 1.38 oster return NS_TRYAGAIN;
724 1.26 lukem
725 1.4 christos /* Cleanup any previous storage */
726 1.4 christos if (_nghead != NULL)
727 1.4 christos endnetgrent();
728 1.4 christos
729 1.4 christos if (_ng_db == NULL)
730 1.4 christos _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
731 1.4 christos
732 1.4 christos ng_copy = strdup(ng);
733 1.26 lukem if (ng_copy != NULL)
734 1.26 lukem addgroup(sl, ng_copy);
735 1.4 christos _nghead = _nglist;
736 1.11 lukem sl_free(sl, 1);
737 1.38 oster
738 1.38 oster return NS_SUCCESS;
739 1.4 christos }
740 1.4 christos
741 1.38 oster /*ARGSUSED*/
742 1.38 oster static int
743 1.39 oster _local_getnetgrent(void *rv, void *cb_data, va_list ap)
744 1.38 oster {
745 1.38 oster int *retval = va_arg(ap, int *);
746 1.38 oster const char **host = va_arg(ap, const char **);
747 1.38 oster const char **user = va_arg(ap, const char **);
748 1.38 oster const char **domain = va_arg(ap, const char **);
749 1.4 christos
750 1.24 lukem _DIAGASSERT(host != NULL);
751 1.24 lukem _DIAGASSERT(user != NULL);
752 1.24 lukem _DIAGASSERT(domain != NULL);
753 1.24 lukem
754 1.38 oster *retval = 0;
755 1.38 oster
756 1.4 christos if (_nglist == NULL)
757 1.38 oster return NS_TRYAGAIN;
758 1.4 christos
759 1.4 christos *host = _nglist->ng_host;
760 1.4 christos *user = _nglist->ng_user;
761 1.4 christos *domain = _nglist->ng_domain;
762 1.4 christos
763 1.4 christos _nglist = _nglist->ng_next;
764 1.4 christos
765 1.38 oster *retval = 1;
766 1.38 oster
767 1.38 oster return NS_SUCCESS;
768 1.4 christos }
769 1.4 christos
770 1.38 oster /*ARGSUSED*/
771 1.38 oster static int
772 1.39 oster _local_innetgr(void *rv, void *cb_data, va_list ap)
773 1.38 oster {
774 1.38 oster int *retval = va_arg(ap, int *);
775 1.38 oster const char *grp = va_arg(ap, const char *);
776 1.38 oster const char *host = va_arg(ap, const char *);
777 1.38 oster const char *user = va_arg(ap, const char *);
778 1.38 oster const char *domain = va_arg(ap, const char *);
779 1.4 christos
780 1.4 christos int found;
781 1.11 lukem StringList *sl;
782 1.30 christos char *grcpy;
783 1.24 lukem
784 1.24 lukem _DIAGASSERT(grp != NULL);
785 1.24 lukem /* host may be NULL */
786 1.24 lukem /* user may be NULL */
787 1.24 lukem /* domain may be NULL */
788 1.4 christos
789 1.4 christos if (_ng_db == NULL)
790 1.4 christos _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
791 1.4 christos
792 1.4 christos /* Try the fast lookup first */
793 1.4 christos if (host != NULL && user == NULL) {
794 1.38 oster if (in_lookup(grp, host, domain, _NG_KEYBYHOST)) {
795 1.38 oster *retval = 1;
796 1.38 oster return NS_SUCCESS;
797 1.38 oster }
798 1.4 christos } else if (host == NULL && user != NULL) {
799 1.38 oster if (in_lookup(grp, user, domain, _NG_KEYBYUSER)) {
800 1.38 oster *retval = 1;
801 1.38 oster return NS_SUCCESS;
802 1.38 oster }
803 1.4 christos }
804 1.4 christos /* If a domainname is given, we would have found a match */
805 1.38 oster if (domain != NULL) {
806 1.38 oster *retval = 0;
807 1.38 oster return NS_SUCCESS;
808 1.38 oster }
809 1.4 christos
810 1.4 christos /* Too bad need the slow recursive way */
811 1.11 lukem sl = sl_init();
812 1.38 oster if (sl == NULL) {
813 1.38 oster *retval = 0;
814 1.38 oster return NS_SUCCESS;
815 1.38 oster }
816 1.31 christos if ((grcpy = strdup(grp)) == NULL) {
817 1.31 christos sl_free(sl, 1);
818 1.38 oster *retval = 0;
819 1.38 oster return NS_SUCCESS;
820 1.31 christos }
821 1.30 christos found = in_find(sl, grcpy, host, user, domain);
822 1.11 lukem sl_free(sl, 1);
823 1.4 christos
824 1.38 oster *retval = found;
825 1.38 oster return NS_SUCCESS;
826 1.38 oster }
827 1.38 oster
828 1.39 oster #ifdef YP
829 1.38 oster
830 1.39 oster /*ARGSUSED*/
831 1.39 oster static int
832 1.39 oster _nis_endnetgrent(void *rv, void *cb_data, va_list ap)
833 1.39 oster {
834 1.39 oster return _local_endnetgrent(rv, cb_data, ap);
835 1.39 oster }
836 1.38 oster
837 1.39 oster /*ARGSUSED*/
838 1.39 oster static int
839 1.39 oster _nis_setnetgrent(void *rv, void *cb_data, va_list ap)
840 1.39 oster {
841 1.39 oster return _local_setnetgrent(rv, cb_data, ap);
842 1.39 oster }
843 1.38 oster
844 1.39 oster /*ARGSUSED*/
845 1.39 oster static int
846 1.39 oster _nis_getnetgrent(void *rv, void *cb_data, va_list ap)
847 1.39 oster {
848 1.39 oster return _local_getnetgrent(rv, cb_data, ap);
849 1.39 oster }
850 1.38 oster
851 1.39 oster /*ARGSUSED*/
852 1.39 oster static int
853 1.39 oster _nis_innetgr(void *rv, void *cb_data, va_list ap)
854 1.39 oster {
855 1.39 oster return _local_innetgr(rv, cb_data, ap);
856 1.39 oster }
857 1.39 oster
858 1.39 oster #endif
859 1.39 oster
860 1.39 oster
861 1.39 oster #ifdef NSSRC_FILES
862 1.38 oster void
863 1.38 oster endnetgrent(void)
864 1.38 oster {
865 1.38 oster static const ns_dtab dtab[] = {
866 1.39 oster NS_FILES_CB(_local_endnetgrent, NULL)
867 1.38 oster NS_NIS_CB(_nis_endnetgrent, NULL)
868 1.38 oster NS_NULL_CB
869 1.38 oster };
870 1.38 oster
871 1.38 oster (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "endnetgrent",
872 1.38 oster __nsdefaultcompat);
873 1.38 oster }
874 1.39 oster #else
875 1.39 oster static int
876 1.39 oster _local_endnetgrentv(int *rv, void *cbdata, ...)
877 1.39 oster {
878 1.39 oster int e;
879 1.39 oster va_list ap;
880 1.39 oster va_start(ap, cbdata);
881 1.39 oster e = _local_endnetgrent(rv, cbdata, ap);
882 1.39 oster va_end(ap);
883 1.39 oster return e;
884 1.39 oster }
885 1.38 oster
886 1.39 oster void
887 1.39 oster endnetgrent(void)
888 1.39 oster {
889 1.39 oster (void)_local_endnetgrentv(NULL, NULL, NULL);
890 1.39 oster }
891 1.39 oster #endif
892 1.38 oster
893 1.39 oster #ifdef NSSRC_FILES
894 1.38 oster void
895 1.38 oster setnetgrent(const char *ng)
896 1.38 oster {
897 1.38 oster static const ns_dtab dtab[] = {
898 1.39 oster NS_FILES_CB(_local_setnetgrent, NULL)
899 1.38 oster NS_NIS_CB(_nis_setnetgrent, NULL)
900 1.38 oster NS_NULL_CB
901 1.38 oster };
902 1.38 oster
903 1.40 rtr (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "setnetgrent",
904 1.38 oster __nsdefaultnis, ng);
905 1.38 oster }
906 1.39 oster #else
907 1.39 oster static int
908 1.39 oster _local_setnetgrentv(int *rv, void *cbdata, ...)
909 1.39 oster {
910 1.39 oster int e;
911 1.39 oster va_list ap;
912 1.39 oster va_start(ap, cbdata);
913 1.39 oster e = _local_setnetgrent(rv, cbdata, ap);
914 1.39 oster va_end(ap);
915 1.39 oster return e;
916 1.39 oster }
917 1.38 oster
918 1.39 oster void
919 1.39 oster setnetgrent(const char *ng)
920 1.39 oster {
921 1.39 oster (void) _local_setnetgrentv(NULL, NULL,ng);
922 1.39 oster }
923 1.39 oster
924 1.39 oster #endif
925 1.38 oster
926 1.39 oster #ifdef NSSRC_FILES
927 1.38 oster int
928 1.38 oster getnetgrent(const char **host, const char **user, const char **domain)
929 1.38 oster {
930 1.38 oster int r, retval;
931 1.38 oster static const ns_dtab dtab[] = {
932 1.39 oster NS_FILES_CB(_local_getnetgrent, NULL)
933 1.38 oster NS_NIS_CB(_nis_getnetgrent, NULL)
934 1.38 oster NS_NULL_CB
935 1.38 oster };
936 1.38 oster
937 1.38 oster r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "getnetgrent",
938 1.38 oster __nsdefaultnis, &retval, host, user, domain);
939 1.38 oster
940 1.38 oster return (r == NS_SUCCESS) ? retval : 0;
941 1.38 oster }
942 1.39 oster #else
943 1.39 oster static int
944 1.39 oster _local_getnetgrentv(int *rv, void *cbdata, ...)
945 1.39 oster {
946 1.39 oster int e;
947 1.39 oster va_list ap;
948 1.39 oster va_start(ap, cbdata);
949 1.39 oster e = _local_getnetgrent(rv, cbdata, ap);
950 1.39 oster va_end(ap);
951 1.39 oster return e;
952 1.39 oster }
953 1.38 oster
954 1.39 oster int
955 1.39 oster getnetgrent(const char **host, const char **user, const char **domain)
956 1.39 oster {
957 1.39 oster return _local_getnetgrentv(NULL, NULL, host, user, domain) == NS_SUCCESS;
958 1.39 oster }
959 1.39 oster #endif
960 1.38 oster
961 1.39 oster #ifdef NSSRC_FILES
962 1.38 oster int
963 1.39 oster innetgr(const char *grp, const char *host, const char *user,
964 1.39 oster const char *domain)
965 1.38 oster {
966 1.38 oster int r, retval;
967 1.38 oster static const ns_dtab dtab[] = {
968 1.39 oster NS_FILES_CB(_local_innetgr, NULL)
969 1.38 oster NS_NIS_CB(_nis_innetgr, NULL)
970 1.38 oster NS_NULL_CB
971 1.38 oster };
972 1.38 oster
973 1.38 oster r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "innetgr",
974 1.38 oster __nsdefaultnis, &retval, grp, host, user, domain);
975 1.38 oster
976 1.38 oster return (r == NS_SUCCESS) ? retval : 0;
977 1.1 mycroft }
978 1.39 oster #else
979 1.39 oster static int
980 1.39 oster _local_innetgrv(int *rv, void *cbdata, ...)
981 1.39 oster {
982 1.39 oster int e;
983 1.39 oster va_list ap;
984 1.39 oster va_start(ap, cbdata);
985 1.39 oster e = _local_innetgr(rv, cbdata, ap);
986 1.39 oster va_end(ap);
987 1.39 oster return e;
988 1.39 oster }
989 1.39 oster
990 1.39 oster int
991 1.39 oster innetgr(const char *grp, const char *host, const char *user,
992 1.39 oster const char *domain)
993 1.39 oster {
994 1.39 oster return _local_innetgrv(NULL, NULL, grp, host, user, domain) == NS_SUCCESS;
995 1.39 oster }
996 1.39 oster #endif
997