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