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