getnetgrent.c revision 1.18 1 1.18 christos /* $NetBSD: getnetgrent.c,v 1.18 1999/01/18 20:38:01 christos 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.18 christos __RCSID("$NetBSD: getnetgrent.c,v 1.18 1999/01/18 20:38:01 christos 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.1 mycroft #include <stdio.h>
42 1.9 christos #define _NETGROUP_PRIVATE
43 1.4 christos #include <netgroup.h>
44 1.4 christos #include <string.h>
45 1.4 christos #include <fcntl.h>
46 1.4 christos #include <err.h>
47 1.4 christos #include <ctype.h>
48 1.17 lukem #include <nsswitch.h>
49 1.3 cgd #include <stdlib.h>
50 1.11 lukem #include <stringlist.h>
51 1.4 christos #include <db.h>
52 1.10 cgd #ifdef YP
53 1.17 lukem #include <rpc/rpc.h>
54 1.10 cgd #include <rpcsvc/ypclnt.h>
55 1.17 lukem #include <rpcsvc/yp_prot.h>
56 1.13 jtc #endif
57 1.17 lukem
58 1.13 jtc #ifdef __weak_alias
59 1.13 jtc __weak_alias(endnetgrent,_endnetgrent);
60 1.13 jtc __weak_alias(getnetgrent,_getnetgrent);
61 1.13 jtc __weak_alias(innetgr,_innetgr);
62 1.13 jtc __weak_alias(setnetgrent,_setnetgrent);
63 1.10 cgd #endif
64 1.4 christos
65 1.4 christos #define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s)
66 1.9 christos #define _NG_EMPTY(s) ((s) == NULL ? "" : s)
67 1.4 christos #define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n')
68 1.1 mycroft
69 1.4 christos static const char _ngstar[] = "*";
70 1.4 christos static const char _ngoomem[] = "netgroup: %m";
71 1.6 christos static struct netgroup *_nghead = (struct netgroup *)NULL;
72 1.6 christos static struct netgroup *_nglist = (struct netgroup *)NULL;
73 1.4 christos static DB *_ng_db;
74 1.1 mycroft
75 1.16 mycroft static int getstring __P((char **, int, __aconst char **));
76 1.4 christos static struct netgroup *getnetgroup __P((char **));
77 1.17 lukem static int lookup __P((char *, char **, int));
78 1.17 lukem static void addgroup __P((StringList *, char *));
79 1.4 christos static int in_check __P((const char *, const char *,
80 1.4 christos const char *, struct netgroup *));
81 1.17 lukem static int in_find __P((StringList *, char *, const char *,
82 1.4 christos const char *, const char *));
83 1.17 lukem static char *in_lookup1 __P((const char *, const char *, int));
84 1.4 christos static int in_lookup __P((const char *, const char *,
85 1.17 lukem const char *, int));
86 1.4 christos
87 1.4 christos /*
88 1.4 christos * getstring(): Get a string delimited by the character, skipping leading and
89 1.4 christos * trailing blanks and advancing the pointer
90 1.4 christos */
91 1.6 christos static int
92 1.6 christos getstring(pp, del, str)
93 1.4 christos char **pp;
94 1.4 christos int del;
95 1.16 mycroft char __aconst **str;
96 1.4 christos {
97 1.14 perry size_t len;
98 1.4 christos char *sp, *ep, *dp;
99 1.4 christos
100 1.4 christos /* skip leading blanks */
101 1.4 christos for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
102 1.4 christos continue;
103 1.4 christos
104 1.4 christos /* accumulate till delimiter or space */
105 1.4 christos for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
106 1.4 christos continue;
107 1.4 christos
108 1.4 christos /* hunt for the delimiter */
109 1.4 christos for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
110 1.4 christos continue;
111 1.4 christos
112 1.6 christos if (*dp != del) {
113 1.6 christos *str = NULL;
114 1.6 christos return 0;
115 1.6 christos }
116 1.4 christos
117 1.4 christos *pp = ++dp;
118 1.4 christos
119 1.14 perry len = (ep - sp) + 1;
120 1.14 perry if (len > 1) {
121 1.14 perry dp = malloc(len);
122 1.6 christos if (dp == NULL)
123 1.12 christos err(1, _ngoomem);
124 1.14 perry memcpy(dp, sp, len);
125 1.14 perry dp[len - 1] = '\0';
126 1.6 christos } else
127 1.6 christos dp = NULL;
128 1.1 mycroft
129 1.6 christos *str = dp;
130 1.6 christos return 1;
131 1.4 christos }
132 1.4 christos
133 1.4 christos
134 1.4 christos /*
135 1.4 christos * getnetgroup(): Parse a netgroup, and advance the pointer
136 1.4 christos */
137 1.4 christos static struct netgroup *
138 1.4 christos getnetgroup(pp)
139 1.4 christos char **pp;
140 1.4 christos {
141 1.4 christos struct netgroup *ng = malloc(sizeof(struct netgroup));
142 1.4 christos
143 1.4 christos if (ng == NULL)
144 1.12 christos err(1, _ngoomem);
145 1.4 christos
146 1.4 christos (*pp)++; /* skip '(' */
147 1.6 christos if (!getstring(pp, ',', &ng->ng_host))
148 1.4 christos goto badhost;
149 1.4 christos
150 1.6 christos if (!getstring(pp, ',', &ng->ng_user))
151 1.4 christos goto baduser;
152 1.4 christos
153 1.6 christos if (!getstring(pp, ')', &ng->ng_domain))
154 1.4 christos goto baddomain;
155 1.4 christos
156 1.4 christos #ifdef DEBUG_NG
157 1.9 christos {
158 1.9 christos char buf[1024];
159 1.9 christos (void) fprintf(stderr, "netgroup %s\n",
160 1.9 christos _ng_print(buf, sizeof(buf), ng));
161 1.9 christos }
162 1.4 christos #endif
163 1.4 christos return ng;
164 1.4 christos
165 1.4 christos baddomain:
166 1.6 christos if (ng->ng_user)
167 1.15 mycroft free((char *)ng->ng_user);
168 1.4 christos baduser:
169 1.6 christos if (ng->ng_host)
170 1.15 mycroft free((char *)ng->ng_host);
171 1.4 christos badhost:
172 1.4 christos free(ng);
173 1.4 christos return NULL;
174 1.4 christos }
175 1.4 christos
176 1.4 christos
177 1.17 lukem static int _local_lookup __P((void *, void *, va_list));
178 1.17 lukem
179 1.18 christos /*ARGSUSED*/
180 1.4 christos static int
181 1.17 lukem _local_lookup(rv, cb_data, ap)
182 1.17 lukem void *rv;
183 1.17 lukem void *cb_data;
184 1.17 lukem va_list ap;
185 1.17 lukem {
186 1.17 lukem char *name = va_arg(ap, char *);
187 1.17 lukem char **line = va_arg(ap, char **);
188 1.17 lukem int bywhat = va_arg(ap, int);
189 1.17 lukem
190 1.17 lukem DBT key, data;
191 1.17 lukem size_t len;
192 1.17 lukem char *ks;
193 1.17 lukem int r;
194 1.4 christos
195 1.17 lukem if (_ng_db == NULL)
196 1.17 lukem return NS_UNAVAIL;
197 1.4 christos
198 1.17 lukem len = strlen(name) + 2;
199 1.17 lukem ks = malloc(len);
200 1.17 lukem if (ks == NULL)
201 1.17 lukem err(1, _ngoomem);
202 1.4 christos
203 1.17 lukem ks[0] = bywhat;
204 1.17 lukem memcpy(&ks[1], name, len - 1);
205 1.4 christos
206 1.17 lukem key.data = (u_char *) ks;
207 1.17 lukem key.size = len;
208 1.4 christos
209 1.17 lukem r = (_ng_db->get) (_ng_db, &key, &data, 0);
210 1.17 lukem free(ks);
211 1.17 lukem switch (r) {
212 1.17 lukem case 0:
213 1.17 lukem break;
214 1.17 lukem case 1:
215 1.17 lukem return NS_NOTFOUND;
216 1.17 lukem case -1:
217 1.17 lukem return NS_UNAVAIL;
218 1.17 lukem }
219 1.17 lukem
220 1.17 lukem *line = strdup(data.data);
221 1.17 lukem if (*line == NULL)
222 1.17 lukem return NS_UNAVAIL;
223 1.17 lukem return NS_SUCCESS;
224 1.17 lukem }
225 1.4 christos
226 1.4 christos #ifdef YP
227 1.17 lukem static int _nis_lookup __P((void *, void *, va_list));
228 1.4 christos
229 1.18 christos /*ARGSUSED*/
230 1.17 lukem static int
231 1.17 lukem _nis_lookup(rv, cb_data, ap)
232 1.17 lukem void *rv;
233 1.17 lukem void *cb_data;
234 1.17 lukem va_list ap;
235 1.17 lukem {
236 1.17 lukem char *name = va_arg(ap, char *);
237 1.17 lukem char **line = va_arg(ap, char **);
238 1.17 lukem int bywhat = va_arg(ap, int);
239 1.17 lukem
240 1.17 lukem static char *__ypdomain;
241 1.17 lukem int i;
242 1.17 lukem char *map = NULL;
243 1.4 christos
244 1.17 lukem if(__ypdomain == NULL) {
245 1.17 lukem switch (yp_get_default_domain(&__ypdomain)) {
246 1.17 lukem case 0:
247 1.6 christos break;
248 1.17 lukem case YPERR_RESRC:
249 1.17 lukem return NS_TRYAGAIN;
250 1.6 christos default:
251 1.17 lukem return NS_UNAVAIL;
252 1.6 christos }
253 1.17 lukem }
254 1.17 lukem
255 1.17 lukem switch (bywhat) {
256 1.17 lukem case _NG_KEYBYNAME:
257 1.17 lukem map = "netgroup";
258 1.17 lukem break;
259 1.17 lukem
260 1.17 lukem case _NG_KEYBYUSER:
261 1.17 lukem map = "netgroup.byuser";
262 1.17 lukem break;
263 1.17 lukem
264 1.17 lukem case _NG_KEYBYHOST:
265 1.17 lukem map = "netgroup.byhost";
266 1.17 lukem break;
267 1.17 lukem
268 1.17 lukem default:
269 1.17 lukem abort();
270 1.17 lukem break;
271 1.17 lukem }
272 1.4 christos
273 1.4 christos
274 1.17 lukem *line = NULL;
275 1.17 lukem switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
276 1.17 lukem case 0:
277 1.17 lukem return NS_SUCCESS;
278 1.17 lukem case YPERR_KEY:
279 1.17 lukem if (*line)
280 1.17 lukem free(*line);
281 1.17 lukem return NS_NOTFOUND;
282 1.17 lukem default:
283 1.17 lukem if (*line)
284 1.17 lukem free(*line);
285 1.17 lukem return NS_UNAVAIL;
286 1.6 christos }
287 1.17 lukem /* NOTREACHED */
288 1.17 lukem }
289 1.4 christos #endif
290 1.4 christos
291 1.17 lukem /*
292 1.17 lukem * lookup(): Find the given key in the database or yp, and return its value
293 1.17 lukem * in *line; returns 1 if key was found, 0 otherwise
294 1.17 lukem */
295 1.17 lukem static int
296 1.17 lukem lookup(name, line, bywhat)
297 1.17 lukem char *name;
298 1.17 lukem char **line;
299 1.17 lukem int bywhat;
300 1.17 lukem {
301 1.17 lukem int r;
302 1.17 lukem static ns_dtab dtab[] = {
303 1.17 lukem NS_FILES_CB(_local_lookup, NULL),
304 1.17 lukem NS_DNS_CB(_nis_lookup, NULL),
305 1.17 lukem { NULL, NULL, NULL }
306 1.17 lukem };
307 1.17 lukem
308 1.17 lukem r = nsdispatch(NULL, dtab, NSDB_NETGROUP, name, line, bywhat);
309 1.17 lukem return (r == NS_SUCCESS) ? 1 : 0;
310 1.1 mycroft }
311 1.1 mycroft
312 1.1 mycroft /*
313 1.4 christos * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
314 1.4 christos * line was empty or a comment _NG_GROUP: line had a netgroup definition,
315 1.4 christos * returned in ng _NG_NAME: line had a netgroup name, returned in name
316 1.4 christos *
317 1.4 christos * Public since used by netgroup_mkdb
318 1.1 mycroft */
319 1.1 mycroft int
320 1.4 christos _ng_parse(p, name, ng)
321 1.4 christos char **p;
322 1.4 christos char **name;
323 1.4 christos struct netgroup **ng;
324 1.1 mycroft {
325 1.4 christos while (**p) {
326 1.4 christos if (**p == '#')
327 1.4 christos /* comment */
328 1.4 christos return _NG_NONE;
329 1.4 christos
330 1.4 christos while (**p && _NG_ISSPACE(**p))
331 1.4 christos /* skipblank */
332 1.4 christos (*p)++;
333 1.4 christos
334 1.4 christos if (**p == '(') {
335 1.4 christos if ((*ng = getnetgroup(p)) == NULL) {
336 1.12 christos warnx("netgroup: Syntax error `%s'", *p);
337 1.4 christos return _NG_ERROR;
338 1.4 christos }
339 1.4 christos return _NG_GROUP;
340 1.4 christos } else {
341 1.17 lukem char *np;
342 1.17 lukem size_t i;
343 1.1 mycroft
344 1.4 christos for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
345 1.4 christos continue;
346 1.4 christos if (np != *p) {
347 1.4 christos i = (*p - np) + 1;
348 1.4 christos *name = malloc(i);
349 1.4 christos if (*name == NULL)
350 1.12 christos err(1, _ngoomem);
351 1.4 christos memcpy(*name, np, i);
352 1.4 christos (*name)[i - 1] = '\0';
353 1.4 christos return _NG_NAME;
354 1.4 christos }
355 1.4 christos }
356 1.1 mycroft }
357 1.4 christos return _NG_NONE;
358 1.1 mycroft }
359 1.1 mycroft
360 1.4 christos
361 1.1 mycroft /*
362 1.4 christos * addgroup(): Recursively add all the members of the netgroup to this group
363 1.1 mycroft */
364 1.4 christos static void
365 1.17 lukem addgroup(sl, grp)
366 1.11 lukem StringList *sl;
367 1.11 lukem char *grp;
368 1.1 mycroft {
369 1.4 christos char *line, *p;
370 1.4 christos struct netgroup *ng;
371 1.4 christos char *name;
372 1.4 christos
373 1.4 christos #ifdef DEBUG_NG
374 1.4 christos (void) fprintf(stderr, "addgroup(%s)\n", grp);
375 1.4 christos #endif
376 1.4 christos /* check for cycles */
377 1.11 lukem if (sl_find(sl, grp) != NULL) {
378 1.6 christos free(grp);
379 1.12 christos warnx("netgroup: Cycle in group `%s'", grp);
380 1.4 christos return;
381 1.4 christos }
382 1.11 lukem sl_add(sl, grp);
383 1.4 christos
384 1.4 christos /* Lookup this netgroup */
385 1.17 lukem line = NULL;
386 1.17 lukem if (!lookup(grp, &line, _NG_KEYBYNAME)) {
387 1.17 lukem if (line != NULL)
388 1.17 lukem free(line);
389 1.4 christos return;
390 1.17 lukem }
391 1.4 christos
392 1.4 christos p = line;
393 1.4 christos
394 1.4 christos for (;;) {
395 1.4 christos switch (_ng_parse(&p, &name, &ng)) {
396 1.4 christos case _NG_NONE:
397 1.4 christos /* Done with the line */
398 1.4 christos free(line);
399 1.4 christos return;
400 1.4 christos
401 1.4 christos case _NG_GROUP:
402 1.4 christos /* new netgroup */
403 1.4 christos /* add to the list */
404 1.4 christos ng->ng_next = _nglist;
405 1.4 christos _nglist = ng;
406 1.4 christos break;
407 1.4 christos
408 1.4 christos case _NG_NAME:
409 1.4 christos /* netgroup name */
410 1.17 lukem addgroup(sl, name);
411 1.4 christos break;
412 1.1 mycroft
413 1.4 christos case _NG_ERROR:
414 1.4 christos return;
415 1.4 christos
416 1.4 christos default:
417 1.4 christos abort();
418 1.4 christos return;
419 1.4 christos }
420 1.1 mycroft }
421 1.1 mycroft }
422 1.1 mycroft
423 1.4 christos
424 1.1 mycroft /*
425 1.4 christos * in_check(): Compare the spec with the netgroup
426 1.1 mycroft */
427 1.4 christos static int
428 1.4 christos in_check(host, user, domain, ng)
429 1.4 christos const char *host;
430 1.4 christos const char *user;
431 1.4 christos const char *domain;
432 1.4 christos struct netgroup *ng;
433 1.1 mycroft {
434 1.6 christos if ((host != NULL) && (ng->ng_host != NULL)
435 1.4 christos && strcmp(ng->ng_host, host) != 0)
436 1.4 christos return 0;
437 1.4 christos
438 1.6 christos if ((user != NULL) && (ng->ng_user != NULL)
439 1.4 christos && strcmp(ng->ng_user, user) != 0)
440 1.4 christos return 0;
441 1.4 christos
442 1.6 christos if ((domain != NULL) && (ng->ng_domain != NULL)
443 1.4 christos && strcmp(ng->ng_domain, domain) != 0)
444 1.4 christos return 0;
445 1.1 mycroft
446 1.4 christos return 1;
447 1.1 mycroft }
448 1.1 mycroft
449 1.4 christos
450 1.1 mycroft /*
451 1.4 christos * in_find(): Find a match for the host, user, domain spec
452 1.1 mycroft */
453 1.1 mycroft static int
454 1.17 lukem in_find(sl, grp, host, user, domain)
455 1.11 lukem StringList *sl;
456 1.11 lukem char *grp;
457 1.11 lukem const char *host;
458 1.11 lukem const char *user;
459 1.11 lukem const char *domain;
460 1.1 mycroft {
461 1.4 christos char *line, *p;
462 1.4 christos int i;
463 1.4 christos struct netgroup *ng;
464 1.4 christos char *name;
465 1.4 christos
466 1.4 christos #ifdef DEBUG_NG
467 1.4 christos (void) fprintf(stderr, "in_find(%s)\n", grp);
468 1.4 christos #endif
469 1.4 christos /* check for cycles */
470 1.11 lukem if (sl_find(sl, grp) != NULL) {
471 1.6 christos free(grp);
472 1.12 christos warnx("netgroup: Cycle in group `%s'", grp);
473 1.4 christos return 0;
474 1.4 christos }
475 1.11 lukem sl_add(sl, grp);
476 1.1 mycroft
477 1.4 christos /* Lookup this netgroup */
478 1.17 lukem line = NULL;
479 1.17 lukem if (!lookup(grp, &line, _NG_KEYBYNAME)) {
480 1.17 lukem if (line)
481 1.17 lukem free(line);
482 1.4 christos return 0;
483 1.17 lukem }
484 1.4 christos
485 1.4 christos p = line;
486 1.4 christos
487 1.4 christos for (;;) {
488 1.4 christos switch (_ng_parse(&p, &name, &ng)) {
489 1.4 christos case _NG_NONE:
490 1.4 christos /* Done with the line */
491 1.4 christos free(line);
492 1.4 christos return 0;
493 1.4 christos
494 1.4 christos case _NG_GROUP:
495 1.4 christos /* new netgroup */
496 1.4 christos i = in_check(host, user, domain, ng);
497 1.6 christos if (ng->ng_host != NULL)
498 1.15 mycroft free((char *)ng->ng_host);
499 1.6 christos if (ng->ng_user != NULL)
500 1.15 mycroft free((char *)ng->ng_user);
501 1.6 christos if (ng->ng_domain != NULL)
502 1.15 mycroft free((char *)ng->ng_domain);
503 1.4 christos free(ng);
504 1.4 christos if (i) {
505 1.4 christos free(line);
506 1.4 christos return 1;
507 1.4 christos }
508 1.1 mycroft break;
509 1.4 christos
510 1.4 christos case _NG_NAME:
511 1.4 christos /* netgroup name */
512 1.17 lukem if (in_find(sl, name, host, user, domain)) {
513 1.4 christos free(line);
514 1.4 christos return 1;
515 1.1 mycroft }
516 1.4 christos break;
517 1.4 christos
518 1.4 christos case _NG_ERROR:
519 1.4 christos free(line);
520 1.4 christos return 0;
521 1.4 christos
522 1.4 christos default:
523 1.4 christos abort();
524 1.4 christos return 0;
525 1.1 mycroft }
526 1.1 mycroft }
527 1.4 christos }
528 1.4 christos
529 1.4 christos
530 1.4 christos /*
531 1.4 christos * _ng_makekey(): Make a key from the two names given. The key is of the form
532 1.4 christos * <name1>.<name2> Names strings are replaced with * if they are empty;
533 1.4 christos */
534 1.4 christos char *
535 1.4 christos _ng_makekey(s1, s2, len)
536 1.4 christos const char *s1, *s2;
537 1.4 christos size_t len;
538 1.4 christos {
539 1.4 christos char *buf = malloc(len);
540 1.4 christos if (buf == NULL)
541 1.12 christos err(1, _ngoomem);
542 1.4 christos (void) snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
543 1.4 christos return buf;
544 1.9 christos }
545 1.9 christos
546 1.9 christos void
547 1.9 christos _ng_print(buf, len, ng)
548 1.9 christos char *buf;
549 1.9 christos size_t len;
550 1.9 christos const struct netgroup *ng;
551 1.9 christos {
552 1.9 christos (void) snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
553 1.9 christos _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
554 1.4 christos }
555 1.4 christos
556 1.4 christos
557 1.4 christos /*
558 1.4 christos * in_lookup1(): Fast lookup for a key in the appropriate map
559 1.4 christos */
560 1.4 christos static char *
561 1.17 lukem in_lookup1(key, domain, map)
562 1.4 christos const char *key;
563 1.4 christos const char *domain;
564 1.4 christos int map;
565 1.4 christos {
566 1.4 christos char *line;
567 1.4 christos size_t len;
568 1.4 christos char *ptr;
569 1.4 christos int res;
570 1.4 christos
571 1.4 christos len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
572 1.4 christos ptr = _ng_makekey(key, domain, len);
573 1.17 lukem res = lookup(ptr, &line, map);
574 1.4 christos free(ptr);
575 1.4 christos return res ? line : NULL;
576 1.4 christos }
577 1.4 christos
578 1.4 christos
579 1.4 christos /*
580 1.4 christos * in_lookup(): Fast lookup for a key in the appropriate map
581 1.4 christos */
582 1.4 christos static int
583 1.17 lukem in_lookup(group, key, domain, map)
584 1.4 christos const char *group;
585 1.4 christos const char *key;
586 1.4 christos const char *domain;
587 1.4 christos int map;
588 1.4 christos {
589 1.4 christos size_t len;
590 1.4 christos char *ptr, *line;
591 1.4 christos
592 1.4 christos if (domain != NULL) {
593 1.4 christos /* Domain specified; look in "group.domain" and "*.domain" */
594 1.17 lukem if ((line = in_lookup1(key, domain, map)) == NULL)
595 1.17 lukem line = in_lookup1(NULL, domain, map);
596 1.4 christos }
597 1.4 christos else
598 1.4 christos line = NULL;
599 1.4 christos
600 1.4 christos if (line == NULL) {
601 1.4 christos /*
602 1.4 christos * domain not specified or domain lookup failed; look in
603 1.4 christos * "group.*" and "*.*"
604 1.4 christos */
605 1.17 lukem if (((line = in_lookup1(key, NULL, map)) == NULL) &&
606 1.17 lukem ((line = in_lookup1(NULL, NULL, map)) == NULL))
607 1.4 christos return 0;
608 1.4 christos }
609 1.4 christos
610 1.4 christos len = strlen(group);
611 1.4 christos
612 1.4 christos for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
613 1.4 christos /* Make sure we did not find a substring */
614 1.4 christos if ((ptr != line && ptr[-1] != ',') ||
615 1.4 christos (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
616 1.4 christos ptr++;
617 1.4 christos else {
618 1.4 christos free(line);
619 1.4 christos return 1;
620 1.1 mycroft }
621 1.4 christos
622 1.4 christos free(line);
623 1.4 christos return 0;
624 1.4 christos }
625 1.4 christos
626 1.4 christos
627 1.4 christos void
628 1.4 christos endnetgrent()
629 1.4 christos {
630 1.4 christos for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
631 1.4 christos _nghead = _nglist->ng_next;
632 1.6 christos if (_nglist->ng_host != NULL)
633 1.15 mycroft free((char *)_nglist->ng_host);
634 1.6 christos if (_nglist->ng_user != NULL)
635 1.15 mycroft free((char *)_nglist->ng_user);
636 1.6 christos if (_nglist->ng_domain != NULL)
637 1.15 mycroft free((char *)_nglist->ng_domain);
638 1.4 christos free(_nglist);
639 1.4 christos }
640 1.4 christos
641 1.4 christos if (_ng_db) {
642 1.4 christos (void) (_ng_db->close) (_ng_db);
643 1.4 christos _ng_db = NULL;
644 1.4 christos }
645 1.4 christos }
646 1.4 christos
647 1.4 christos
648 1.4 christos void
649 1.4 christos setnetgrent(ng)
650 1.4 christos const char *ng;
651 1.4 christos {
652 1.11 lukem StringList *sl = sl_init();
653 1.17 lukem char *ng_copy;
654 1.4 christos
655 1.4 christos /* Cleanup any previous storage */
656 1.4 christos if (_nghead != NULL)
657 1.4 christos endnetgrent();
658 1.4 christos
659 1.4 christos if (_ng_db == NULL)
660 1.4 christos _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
661 1.4 christos
662 1.4 christos ng_copy = strdup(ng);
663 1.4 christos if (ng_copy == NULL)
664 1.12 christos err(1, _ngoomem);
665 1.17 lukem addgroup(sl, ng_copy);
666 1.4 christos _nghead = _nglist;
667 1.11 lukem sl_free(sl, 1);
668 1.4 christos }
669 1.4 christos
670 1.4 christos
671 1.4 christos int
672 1.4 christos getnetgrent(host, user, domain)
673 1.4 christos const char **host;
674 1.4 christos const char **user;
675 1.4 christos const char **domain;
676 1.4 christos {
677 1.4 christos if (_nglist == NULL)
678 1.4 christos return 0;
679 1.4 christos
680 1.4 christos *host = _nglist->ng_host;
681 1.4 christos *user = _nglist->ng_user;
682 1.4 christos *domain = _nglist->ng_domain;
683 1.4 christos
684 1.4 christos _nglist = _nglist->ng_next;
685 1.4 christos
686 1.4 christos return 1;
687 1.4 christos }
688 1.4 christos
689 1.4 christos
690 1.4 christos int
691 1.4 christos innetgr(grp, host, user, domain)
692 1.4 christos const char *grp, *host, *user, *domain;
693 1.4 christos {
694 1.4 christos int found;
695 1.11 lukem StringList *sl;
696 1.4 christos
697 1.4 christos if (_ng_db == NULL)
698 1.4 christos _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
699 1.4 christos
700 1.4 christos /* Try the fast lookup first */
701 1.4 christos if (host != NULL && user == NULL) {
702 1.17 lukem if (in_lookup(grp, host, domain, _NG_KEYBYHOST))
703 1.4 christos return 1;
704 1.4 christos } else if (host == NULL && user != NULL) {
705 1.17 lukem if (in_lookup(grp, user, domain, _NG_KEYBYUSER))
706 1.4 christos return 1;
707 1.4 christos }
708 1.4 christos /* If a domainname is given, we would have found a match */
709 1.4 christos if (domain != NULL)
710 1.4 christos return 0;
711 1.4 christos
712 1.4 christos /* Too bad need the slow recursive way */
713 1.11 lukem sl = sl_init();
714 1.17 lukem found = in_find(sl, strdup(grp), host, user, domain);
715 1.11 lukem sl_free(sl, 1);
716 1.4 christos
717 1.4 christos return found;
718 1.1 mycroft }
719