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