Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / gen / getnetgrent.c
blobc036750791f01dbee51a0d5d238ce76ed804bb96
1 /* $NetBSD: getnetgrent.c,v 1.40 2008/04/05 08:01:54 rtr Exp $ */
3 /*
4 * Copyright (c) 1994 Christos Zoulas
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: getnetgrent.c,v 1.40 2008/04/05 08:01:54 rtr Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include <sys/types.h>
37 #include <assert.h>
38 #include <ctype.h>
39 #include <db.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #define _NETGROUP_PRIVATE
43 #include <stringlist.h>
44 #include <netgroup.h>
45 #include <nsswitch.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #ifdef YP
52 #include <rpc/rpc.h>
53 #include <rpcsvc/ypclnt.h>
54 #include <rpcsvc/yp_prot.h>
55 #endif
57 #ifdef __weak_alias
58 __weak_alias(endnetgrent,_endnetgrent)
59 __weak_alias(getnetgrent,_getnetgrent)
60 __weak_alias(innetgr,_innetgr)
61 __weak_alias(setnetgrent,_setnetgrent)
62 #endif
64 #define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s)
65 #define _NG_EMPTY(s) ((s) == NULL ? "" : s)
66 #define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n')
68 static const char _ngstar[] = "*";
69 static struct netgroup *_nghead = NULL;
70 static struct netgroup *_nglist = NULL;
71 static DB *_ng_db;
73 static int getstring(char **, int, __aconst char **);
74 static struct netgroup *getnetgroup(char **);
75 static int lookup(char *, char **, int);
76 static int addgroup(StringList *, char *);
77 static int in_check(const char *, const char *, const char *,
78 struct netgroup *);
79 static int in_find(StringList *, char *, const char *, const char *,
80 const char *);
81 static char *in_lookup1(const char *, const char *, int);
82 static int in_lookup(const char *, const char *, const char *, int);
84 #ifdef NSSRC_FILES
85 static const ns_src default_files_nis[] = {
86 { NSSRC_FILES, NS_SUCCESS | NS_NOTFOUND },
87 #ifdef YP
88 { NSSRC_NIS, NS_SUCCESS },
89 #endif
90 { 0, 0 },
92 #endif
95 * getstring(): Get a string delimited by the character, skipping leading and
96 * trailing blanks and advancing the pointer
98 static int
99 getstring(char **pp, int del, char __aconst **str)
101 size_t len;
102 char *sp, *ep, *dp;
104 _DIAGASSERT(pp != NULL);
105 _DIAGASSERT(str != NULL);
107 /* skip leading blanks */
108 for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
109 continue;
111 /* accumulate till delimiter or space */
112 for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
113 continue;
115 /* hunt for the delimiter */
116 for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
117 continue;
119 if (*dp != del) {
120 *str = NULL;
121 return 0;
124 *pp = ++dp;
126 len = (ep - sp) + 1;
127 if (len > 1) {
128 dp = malloc(len);
129 if (dp == NULL)
130 return 0;
131 (void)memcpy(dp, sp, len);
132 dp[len - 1] = '\0';
133 } else
134 dp = NULL;
136 *str = dp;
137 return 1;
142 * getnetgroup(): Parse a netgroup, and advance the pointer
144 static struct netgroup *
145 getnetgroup(pp)
146 char **pp;
148 struct netgroup *ng;
150 _DIAGASSERT(pp != NULL);
151 _DIAGASSERT(*pp != NULL);
153 ng = malloc(sizeof(struct netgroup));
154 if (ng == NULL)
155 return NULL;
157 (*pp)++; /* skip '(' */
158 if (!getstring(pp, ',', &ng->ng_host))
159 goto badhost;
161 if (!getstring(pp, ',', &ng->ng_user))
162 goto baduser;
164 if (!getstring(pp, ')', &ng->ng_domain))
165 goto baddomain;
167 #ifdef DEBUG_NG
169 char buf[1024];
170 (void) fprintf(stderr, "netgroup %s\n",
171 _ng_print(buf, sizeof(buf), ng));
173 #endif
174 return ng;
176 baddomain:
177 if (ng->ng_user)
178 free(ng->ng_user);
179 baduser:
180 if (ng->ng_host)
181 free(ng->ng_host);
182 badhost:
183 free(ng);
184 return NULL;
187 void
188 _ng_cycle(const char *grp, const StringList *sl)
190 size_t i;
191 warnx("netgroup: Cycle in group `%s'", grp);
192 (void)fprintf(stderr, "groups: ");
193 for (i = 0; i < sl->sl_cur; i++)
194 (void)fprintf(stderr, "%s ", sl->sl_str[i]);
195 (void)fprintf(stderr, "\n");
198 static int _local_lookup(void *, void *, va_list);
200 /*ARGSUSED*/
201 static int
202 _local_lookup(void *rv, void *cb_data, va_list ap)
204 char *name = va_arg(ap, char *);
205 char **line = va_arg(ap, char **);
206 int bywhat = va_arg(ap, int);
208 DBT key, data;
209 size_t len;
210 char *ks;
211 int r;
213 if (_ng_db == NULL)
214 return NS_UNAVAIL;
216 len = strlen(name) + 2;
217 ks = malloc(len);
218 if (ks == NULL)
219 return NS_UNAVAIL;
221 ks[0] = bywhat;
222 (void)memcpy(&ks[1], name, len - 1);
224 key.data = (u_char *)ks;
225 key.size = len;
227 r = (*_ng_db->get)(_ng_db, &key, &data, 0);
228 free(ks);
229 switch (r) {
230 case 0:
231 break;
232 case 1:
233 return NS_NOTFOUND;
234 case -1:
235 /* XXX: call endnetgrent() here ? */
236 return NS_UNAVAIL;
239 *line = strdup(data.data);
240 if (*line == NULL)
241 return NS_UNAVAIL;
242 return NS_SUCCESS;
245 #ifdef YP
246 static int _nis_lookup(void *, void *, va_list);
248 /*ARGSUSED*/
249 static int
250 _nis_lookup(void *rv, void *cb_data, va_list ap)
252 char *name = va_arg(ap, char *);
253 char **line = va_arg(ap, char **);
254 int bywhat = va_arg(ap, int);
256 static char *__ypdomain;
257 int i;
258 const char *map = NULL;
260 if(__ypdomain == NULL) {
261 switch (yp_get_default_domain(&__ypdomain)) {
262 case 0:
263 break;
264 case YPERR_RESRC:
265 return NS_TRYAGAIN;
266 default:
267 return NS_UNAVAIL;
271 switch (bywhat) {
272 case _NG_KEYBYNAME:
273 map = "netgroup";
274 break;
276 case _NG_KEYBYUSER:
277 map = "netgroup.byuser";
278 break;
280 case _NG_KEYBYHOST:
281 map = "netgroup.byhost";
282 break;
284 default:
285 abort();
288 *line = NULL;
289 switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
290 case 0:
291 return NS_SUCCESS;
292 case YPERR_KEY:
293 if (*line)
294 free(*line);
295 return NS_NOTFOUND;
296 default:
297 if (*line)
298 free(*line);
299 return NS_UNAVAIL;
301 /* NOTREACHED */
303 #endif
305 #ifdef NSSRC_FILES
307 * lookup(): Find the given key in the database or yp, and return its value
308 * in *line; returns 1 if key was found, 0 otherwise
310 static int
311 lookup(char *name, char **line, int bywhat)
313 int r;
314 static const ns_dtab dtab[] = {
315 NS_FILES_CB(_local_lookup, NULL)
316 NS_NIS_CB(_nis_lookup, NULL)
317 NS_NULL_CB
320 _DIAGASSERT(name != NULL);
321 _DIAGASSERT(line != NULL);
323 r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "lookup", default_files_nis,
324 name, line, bywhat);
325 return (r == NS_SUCCESS) ? 1 : 0;
327 #else
328 static int
329 _local_lookupv(int *rv, void *cbdata, ...)
331 int e;
332 va_list ap;
333 va_start(ap, cbdata);
334 e = _local_lookup(rv, cbdata, ap);
335 va_end(ap);
336 return e;
339 static int
340 lookup(name, line, bywhat)
341 char *name;
342 char **line;
343 int bywhat;
345 return _local_lookupv(NULL, NULL, name, line, bywhat) == NS_SUCCESS;
347 #endif
350 * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
351 * line was empty or a comment _NG_GROUP: line had a netgroup definition,
352 * returned in ng _NG_NAME: line had a netgroup name, returned in name
354 * Public since used by netgroup_mkdb
357 _ng_parse(char **p, char **name, struct netgroup **ng)
360 _DIAGASSERT(p != NULL);
361 _DIAGASSERT(*p != NULL);
362 _DIAGASSERT(name != NULL);
363 _DIAGASSERT(ng != NULL);
365 while (**p) {
366 if (**p == '#')
367 /* comment */
368 return _NG_NONE;
370 while (**p && _NG_ISSPACE(**p))
371 /* skipblank */
372 (*p)++;
374 if (**p == '(') {
375 if ((*ng = getnetgroup(p)) == NULL)
376 return _NG_ERROR;
377 return _NG_GROUP;
378 } else {
379 char *np;
380 size_t i;
382 for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
383 continue;
384 if (np != *p) {
385 i = (*p - np) + 1;
386 *name = malloc(i);
387 if (*name == NULL)
388 return _NG_ERROR;
389 (void)memcpy(*name, np, i);
390 (*name)[i - 1] = '\0';
391 return _NG_NAME;
395 return _NG_NONE;
400 * addgroup(): Recursively add all the members of the netgroup to this group.
401 * returns 0 upon failure, nonzero upon success.
402 * grp is not a valid pointer after return (either free(3)ed or allocated
403 * to a stringlist). in either case, it shouldn't be used again.
405 static int
406 addgroup(StringList *sl, char *grp)
408 char *line, *p;
409 struct netgroup *ng;
410 char *name;
412 _DIAGASSERT(sl != NULL);
413 _DIAGASSERT(grp != NULL);
415 #ifdef DEBUG_NG
416 (void)fprintf(stderr, "addgroup(%s)\n", grp);
417 #endif
418 /* check for cycles */
419 if (sl_find(sl, grp) != NULL) {
420 _ng_cycle(grp, sl);
421 free(grp);
422 return 0;
424 if (sl_add(sl, grp) == -1) {
425 free(grp);
426 return 0;
429 /* Lookup this netgroup */
430 line = NULL;
431 if (!lookup(grp, &line, _NG_KEYBYNAME)) {
432 if (line)
433 free(line);
434 return 0;
437 p = line;
439 for (;;) {
440 switch (_ng_parse(&p, &name, &ng)) {
441 case _NG_NONE:
442 /* Done with the line */
443 free(line);
444 return 1;
446 case _NG_GROUP:
447 /* new netgroup */
448 /* add to the list */
449 ng->ng_next = _nglist;
450 _nglist = ng;
451 break;
453 case _NG_NAME:
454 /* netgroup name */
455 if (!addgroup(sl, name))
456 return 0;
457 break;
459 case _NG_ERROR:
460 return 0;
462 default:
463 abort();
470 * in_check(): Compare the spec with the netgroup
472 static int
473 in_check(const char *host, const char *user, const char *domain,
474 struct netgroup *ng)
477 /* host may be NULL */
478 /* user may be NULL */
479 /* domain may be NULL */
480 _DIAGASSERT(ng != NULL);
482 if ((host != NULL) && (ng->ng_host != NULL)
483 && strcmp(ng->ng_host, host) != 0)
484 return 0;
486 if ((user != NULL) && (ng->ng_user != NULL)
487 && strcmp(ng->ng_user, user) != 0)
488 return 0;
490 if ((domain != NULL) && (ng->ng_domain != NULL)
491 && strcmp(ng->ng_domain, domain) != 0)
492 return 0;
494 return 1;
499 * in_find(): Find a match for the host, user, domain spec.
500 * grp is not a valid pointer after return (either free(3)ed or allocated
501 * to a stringlist). in either case, it shouldn't be used again.
503 static int
504 in_find(StringList *sl, char *grp, const char *host, const char *user,
505 const char *domain)
507 char *line, *p;
508 int i;
509 struct netgroup *ng;
510 char *name;
512 _DIAGASSERT(sl != NULL);
513 _DIAGASSERT(grp != NULL);
514 /* host may be NULL */
515 /* user may be NULL */
516 /* domain may be NULL */
518 #ifdef DEBUG_NG
519 (void)fprintf(stderr, "in_find(%s)\n", grp);
520 #endif
521 /* check for cycles */
522 if (sl_find(sl, grp) != NULL) {
523 _ng_cycle(grp, sl);
524 free(grp);
525 return 0;
527 if (sl_add(sl, grp) == -1) {
528 free(grp);
529 return 0;
532 /* Lookup this netgroup */
533 line = NULL;
534 if (!lookup(grp, &line, _NG_KEYBYNAME)) {
535 if (line)
536 free(line);
537 return 0;
540 p = line;
542 for (;;) {
543 switch (_ng_parse(&p, &name, &ng)) {
544 case _NG_NONE:
545 /* Done with the line */
546 free(line);
547 return 0;
549 case _NG_GROUP:
550 /* new netgroup */
551 i = in_check(host, user, domain, ng);
552 if (ng->ng_host != NULL)
553 free(ng->ng_host);
554 if (ng->ng_user != NULL)
555 free(ng->ng_user);
556 if (ng->ng_domain != NULL)
557 free(ng->ng_domain);
558 free(ng);
559 if (i) {
560 free(line);
561 return 1;
563 break;
565 case _NG_NAME:
566 /* netgroup name */
567 if (in_find(sl, name, host, user, domain)) {
568 free(line);
569 return 1;
571 break;
573 case _NG_ERROR:
574 free(line);
575 return 0;
577 default:
578 abort();
584 * _ng_makekey(): Make a key from the two names given. The key is of the form
585 * <name1>.<name2> Names strings are replaced with * if they are empty;
586 * Returns NULL if there's a problem.
588 char *
589 _ng_makekey(const char *s1, const char *s2, size_t len)
591 char *buf;
593 /* s1 may be NULL */
594 /* s2 may be NULL */
596 buf = malloc(len);
597 if (buf != NULL)
598 (void)snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
599 return buf;
602 void
603 _ng_print(char *buf, size_t len, const struct netgroup *ng)
605 _DIAGASSERT(buf != NULL);
606 _DIAGASSERT(ng != NULL);
608 (void)snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
609 _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
614 * in_lookup1(): Fast lookup for a key in the appropriate map
616 static char *
617 in_lookup1(const char *key, const char *domain, int map)
619 char *line;
620 size_t len;
621 char *ptr;
622 int res;
624 /* key may be NULL */
625 /* domain may be NULL */
627 len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
628 ptr = _ng_makekey(key, domain, len);
629 if (ptr == NULL)
630 return NULL;
631 res = lookup(ptr, &line, map);
632 free(ptr);
633 return res ? line : NULL;
638 * in_lookup(): Fast lookup for a key in the appropriate map
640 static int
641 in_lookup(const char *group, const char *key, const char *domain, int map)
643 size_t len;
644 char *ptr, *line;
646 _DIAGASSERT(group != NULL);
647 /* key may be NULL */
648 /* domain may be NULL */
650 if (domain != NULL) {
651 /* Domain specified; look in "group.domain" and "*.domain" */
652 if ((line = in_lookup1(key, domain, map)) == NULL)
653 line = in_lookup1(NULL, domain, map);
654 } else
655 line = NULL;
657 if (line == NULL) {
659 * domain not specified or domain lookup failed; look in
660 * "group.*" and "*.*"
662 if (((line = in_lookup1(key, NULL, map)) == NULL) &&
663 ((line = in_lookup1(NULL, NULL, map)) == NULL))
664 return 0;
667 len = strlen(group);
669 for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
670 /* Make sure we did not find a substring */
671 if ((ptr != line && ptr[-1] != ',') ||
672 (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
673 ptr++;
674 else {
675 free(line);
676 return 1;
679 free(line);
680 return 0;
683 /*ARGSUSED*/
684 static int
685 _local_endnetgrent(void *rv, void *cb_data, va_list ap)
687 for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
688 _nghead = _nglist->ng_next;
689 if (_nglist->ng_host != NULL)
690 free(_nglist->ng_host);
691 if (_nglist->ng_user != NULL)
692 free(_nglist->ng_user);
693 if (_nglist->ng_domain != NULL)
694 free(_nglist->ng_domain);
695 free(_nglist);
698 if (_ng_db) {
699 (void)(*_ng_db->close)(_ng_db);
700 _ng_db = NULL;
703 return NS_SUCCESS;
706 /*ARGSUSED*/
707 static int
708 _local_setnetgrent(void *rv, void *cb_data, va_list ap)
710 const char *ng = va_arg(ap, const char *);
711 StringList *sl;
712 char *ng_copy;
714 _DIAGASSERT(ng != NULL);
716 sl = sl_init();
717 if (sl == NULL)
718 return NS_TRYAGAIN;
720 /* Cleanup any previous storage */
721 if (_nghead != NULL)
722 endnetgrent();
724 if (_ng_db == NULL)
725 _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
727 ng_copy = strdup(ng);
728 if (ng_copy != NULL)
729 addgroup(sl, ng_copy);
730 _nghead = _nglist;
731 sl_free(sl, 1);
733 return NS_SUCCESS;
736 /*ARGSUSED*/
737 static int
738 _local_getnetgrent(void *rv, void *cb_data, va_list ap)
740 int *retval = va_arg(ap, int *);
741 const char **host = va_arg(ap, const char **);
742 const char **user = va_arg(ap, const char **);
743 const char **domain = va_arg(ap, const char **);
745 _DIAGASSERT(host != NULL);
746 _DIAGASSERT(user != NULL);
747 _DIAGASSERT(domain != NULL);
749 *retval = 0;
751 if (_nglist == NULL)
752 return NS_TRYAGAIN;
754 *host = _nglist->ng_host;
755 *user = _nglist->ng_user;
756 *domain = _nglist->ng_domain;
758 _nglist = _nglist->ng_next;
760 *retval = 1;
762 return NS_SUCCESS;
765 /*ARGSUSED*/
766 static int
767 _local_innetgr(void *rv, void *cb_data, va_list ap)
769 int *retval = va_arg(ap, int *);
770 const char *grp = va_arg(ap, const char *);
771 const char *host = va_arg(ap, const char *);
772 const char *user = va_arg(ap, const char *);
773 const char *domain = va_arg(ap, const char *);
775 int found;
776 StringList *sl;
777 char *grcpy;
779 _DIAGASSERT(grp != NULL);
780 /* host may be NULL */
781 /* user may be NULL */
782 /* domain may be NULL */
784 if (_ng_db == NULL)
785 _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
787 /* Try the fast lookup first */
788 if (host != NULL && user == NULL) {
789 if (in_lookup(grp, host, domain, _NG_KEYBYHOST)) {
790 *retval = 1;
791 return NS_SUCCESS;
793 } else if (host == NULL && user != NULL) {
794 if (in_lookup(grp, user, domain, _NG_KEYBYUSER)) {
795 *retval = 1;
796 return NS_SUCCESS;
799 /* If a domainname is given, we would have found a match */
800 if (domain != NULL) {
801 *retval = 0;
802 return NS_SUCCESS;
805 /* Too bad need the slow recursive way */
806 sl = sl_init();
807 if (sl == NULL) {
808 *retval = 0;
809 return NS_SUCCESS;
811 if ((grcpy = strdup(grp)) == NULL) {
812 sl_free(sl, 1);
813 *retval = 0;
814 return NS_SUCCESS;
816 found = in_find(sl, grcpy, host, user, domain);
817 sl_free(sl, 1);
819 *retval = found;
820 return NS_SUCCESS;
823 #ifdef YP
825 /*ARGSUSED*/
826 static int
827 _nis_endnetgrent(void *rv, void *cb_data, va_list ap)
829 return _local_endnetgrent(rv, cb_data, ap);
832 /*ARGSUSED*/
833 static int
834 _nis_setnetgrent(void *rv, void *cb_data, va_list ap)
836 return _local_setnetgrent(rv, cb_data, ap);
839 /*ARGSUSED*/
840 static int
841 _nis_getnetgrent(void *rv, void *cb_data, va_list ap)
843 return _local_getnetgrent(rv, cb_data, ap);
846 /*ARGSUSED*/
847 static int
848 _nis_innetgr(void *rv, void *cb_data, va_list ap)
850 return _local_innetgr(rv, cb_data, ap);
853 #endif
856 #ifdef NSSRC_FILES
857 void
858 endnetgrent(void)
860 static const ns_dtab dtab[] = {
861 NS_FILES_CB(_local_endnetgrent, NULL)
862 NS_NIS_CB(_nis_endnetgrent, NULL)
863 NS_NULL_CB
866 (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "endnetgrent",
867 __nsdefaultcompat);
869 #else
870 static int
871 _local_endnetgrentv(int *rv, void *cbdata, ...)
873 int e;
874 va_list ap;
875 va_start(ap, cbdata);
876 e = _local_endnetgrent(rv, cbdata, ap);
877 va_end(ap);
878 return e;
881 void
882 endnetgrent(void)
884 (void)_local_endnetgrentv(NULL, NULL, NULL);
886 #endif
888 #ifdef NSSRC_FILES
889 void
890 setnetgrent(const char *ng)
892 static const ns_dtab dtab[] = {
893 NS_FILES_CB(_local_setnetgrent, NULL)
894 NS_NIS_CB(_nis_setnetgrent, NULL)
895 NS_NULL_CB
898 (void) nsdispatch(NULL, dtab, NSDB_NETGROUP, "setnetgrent",
899 __nsdefaultnis, ng);
901 #else
902 static int
903 _local_setnetgrentv(int *rv, void *cbdata, ...)
905 int e;
906 va_list ap;
907 va_start(ap, cbdata);
908 e = _local_setnetgrent(rv, cbdata, ap);
909 va_end(ap);
910 return e;
913 void
914 setnetgrent(const char *ng)
916 (void) _local_setnetgrentv(NULL, NULL,ng);
919 #endif
921 #ifdef NSSRC_FILES
923 getnetgrent(const char **host, const char **user, const char **domain)
925 int r, retval;
926 static const ns_dtab dtab[] = {
927 NS_FILES_CB(_local_getnetgrent, NULL)
928 NS_NIS_CB(_nis_getnetgrent, NULL)
929 NS_NULL_CB
932 r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "getnetgrent",
933 __nsdefaultnis, &retval, host, user, domain);
935 return (r == NS_SUCCESS) ? retval : 0;
937 #else
938 static int
939 _local_getnetgrentv(int *rv, void *cbdata, ...)
941 int e;
942 va_list ap;
943 va_start(ap, cbdata);
944 e = _local_getnetgrent(rv, cbdata, ap);
945 va_end(ap);
946 return e;
950 getnetgrent(const char **host, const char **user, const char **domain)
952 return _local_getnetgrentv(NULL, NULL, host, user, domain) == NS_SUCCESS;
954 #endif
956 #ifdef NSSRC_FILES
958 innetgr(const char *grp, const char *host, const char *user,
959 const char *domain)
961 int r, retval;
962 static const ns_dtab dtab[] = {
963 NS_FILES_CB(_local_innetgr, NULL)
964 NS_NIS_CB(_nis_innetgr, NULL)
965 NS_NULL_CB
968 r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "innetgr",
969 __nsdefaultnis, &retval, grp, host, user, domain);
971 return (r == NS_SUCCESS) ? retval : 0;
973 #else
974 static int
975 _local_innetgrv(int *rv, void *cbdata, ...)
977 int e;
978 va_list ap;
979 va_start(ap, cbdata);
980 e = _local_innetgr(rv, cbdata, ap);
981 va_end(ap);
982 return e;
986 innetgr(const char *grp, const char *host, const char *user,
987 const char *domain)
989 return _local_innetgrv(NULL, NULL, grp, host, user, domain) == NS_SUCCESS;
991 #endif