1 /*
2  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1996-1999, 2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or 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 WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: getnetgrent.c,v 1.6 2008/11/14 02:36:51 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(NGR_R_CONST char **host, NGR_R_CONST char **user,
80 	    NGR_R_CONST char **domain)
81 {
82 	struct net_data *net_data = init();
83 	const char *ch, *cu, *cd;
84 	int ret;
85 
86 	ret = getnetgrent_p(&ch, &cu, &cd, net_data);
87 	if (ret != 1)
88 		return (ret);
89 
90 	DE_CONST(ch, *host);
91 	DE_CONST(cu, *user);
92 	DE_CONST(cd, *domain);
93 	return (ret);
94 }
95 
96 /* Shared private. */
97 
98 void
99 setnetgrent_p(const char *netgroup, struct net_data *net_data) {
100 	struct irs_ng *ng;
101 
102 	if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
103 		(*ng->rewind)(ng, netgroup);
104 }
105 
106 void
107 endnetgrent_p(struct net_data *net_data) {
108 	struct irs_ng *ng;
109 
110 	if (!net_data)
111 		return;
112 	if ((ng = net_data->ng) != NULL)
113 		(*ng->close)(ng);
114 	net_data->ng = NULL;
115 }
116 
117 int
118 innetgr_p(const char *netgroup, const char *host,
119 	  const char *user, const char *domain,
120 	  struct net_data *net_data) {
121 	struct irs_ng *ng;
122 
123 	if (!net_data || !(ng = net_data->ng))
124 		return (0);
125 	return ((*ng->test)(ng, netgroup, host, user, domain));
126 }
127 
128 int
129 getnetgrent_p(const char **host, const char **user, const char **domain,
130 	      struct net_data *net_data ) {
131 	struct irs_ng *ng;
132 
133 	if (!net_data || !(ng = net_data->ng))
134 		return (0);
135 	return ((*ng->next)(ng, host, user, domain));
136 }
137 
138 /* Private */
139 
140 static struct net_data *
141 init(void) {
142 	struct net_data *net_data;
143 
144 	if (!(net_data = net_data_init(NULL)))
145 		goto error;
146 	if (!net_data->ng) {
147 		net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
148 		if (!net_data->ng) {
149   error:
150 			errno = EIO;
151 			return (NULL);
152 		}
153 	}
154 
155 	return (net_data);
156 }
157 
158 #endif /*__BIND_NOSTATIC*/
159 
160 /*! \file */
161