Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.3 / lib / bind / irs / getnetgrent.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: getnetgrent.c,v 1.1.2.1.4.1 2004/03/09 08:33:36 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 /* Imports */
23
24 #include "port_before.h"
25
26 #if !defined(__BIND_NOSTATIC)
27
28 #include <sys/types.h>
29
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32
33 #include <errno.h>
34 #include <resolv.h>
35 #include <stdio.h>
36
37 #include <irs.h>
38
39 #include "port_after.h"
40
41 #include "irs_data.h"
42
43 /* Forward */
44
45 static struct net_data *init(void);
46
47
48 /* Public */
49
50 #ifndef SETNETGRENT_ARGS
51 #define SETNETGRENT_ARGS const char *netgroup
52 #endif
53 void
54 setnetgrent(SETNETGRENT_ARGS) {
55         struct net_data *net_data = init();
56
57         setnetgrent_p(netgroup, net_data);
58 }
59
60 void
61 endnetgrent(void) {
62         struct net_data *net_data = init();
63
64         endnetgrent_p(net_data);
65 }
66
67 #ifndef INNETGR_ARGS
68 #define INNETGR_ARGS const char *netgroup, const char *host, \
69                      const char *user, const char *domain
70 #endif
71 int
72 innetgr(INNETGR_ARGS) {
73         struct net_data *net_data = init();
74
75         return (innetgr_p(netgroup, host, user, domain, net_data));
76 }
77
78 int
79 getnetgrent(char **host, char **user, char **domain) {
80         struct net_data *net_data = init();
81         const char *ch, *cu, *cd;
82         int ret;
83
84         ret = getnetgrent_p(&ch, &cu, &cd, net_data);
85         if (ret != 1)
86                 return (ret);
87
88         DE_CONST(ch, *host);
89         DE_CONST(cu, *user);
90         DE_CONST(cd, *domain);
91         return (ret);
92 }
93
94 /* Shared private. */
95
96 void
97 setnetgrent_p(const char *netgroup, struct net_data *net_data) {
98         struct irs_ng *ng;
99
100         if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
101                 (*ng->rewind)(ng, netgroup);
102 }
103
104 void
105 endnetgrent_p(struct net_data *net_data) {
106         struct irs_ng *ng;
107
108         if (!net_data)
109                 return;
110         if ((ng = net_data->ng) != NULL)
111                 (*ng->close)(ng);
112         net_data->ng = NULL;
113 }
114
115 int
116 innetgr_p(const char *netgroup, const char *host,
117           const char *user, const char *domain,
118           struct net_data *net_data) {
119         struct irs_ng *ng;
120
121         if (!net_data || !(ng = net_data->ng))
122                 return (0);
123         return ((*ng->test)(ng, netgroup, host, user, domain));
124 }
125
126 int
127 getnetgrent_p(const char **host, const char **user, const char **domain,
128               struct net_data *net_data ) {
129         struct irs_ng *ng;
130
131         if (!net_data || !(ng = net_data->ng))
132                 return (0);
133         return ((*ng->next)(ng, host, user, domain));
134 }
135
136 /* Private */
137
138 static struct net_data *
139 init(void) {
140         struct net_data *net_data;
141
142         if (!(net_data = net_data_init(NULL)))
143                 goto error;
144         if (!net_data->ng) {
145                 net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
146                 if (!net_data->ng) {
147   error:
148                         errno = EIO;
149                         return (NULL);
150                 }
151         }
152         
153         return (net_data);
154 }
155
156 #endif /*__BIND_NOSTATIC*/