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