xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/getnetgrent.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 /* Imports */
19 
20 #include "port_before.h"
21 
22 #if !defined(__BIND_NOSTATIC)
23 
24 #include <sys/types.h>
25 
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 
29 #include <errno.h>
30 #include <resolv.h>
31 #include <stdio.h>
32 
33 #include <irs.h>
34 
35 #include "port_after.h"
36 
37 #include "irs_data.h"
38 
39 /* Forward */
40 
41 static struct net_data *init(void);
42 
43 
44 /* Public */
45 
46 #ifndef SETNETGRENT_ARGS
47 #define SETNETGRENT_ARGS const char *netgroup
48 #endif
49 void
50 setnetgrent(SETNETGRENT_ARGS) {
51 	struct net_data *net_data = init();
52 
53 	setnetgrent_p(netgroup, net_data);
54 }
55 
56 void
57 endnetgrent(void) {
58 	struct net_data *net_data = init();
59 
60 	endnetgrent_p(net_data);
61 }
62 
63 #ifndef INNETGR_ARGS
64 #define INNETGR_ARGS const char *netgroup, const char *host, \
65 		     const char *user, const char *domain
66 #endif
67 int
68 innetgr(INNETGR_ARGS) {
69 	struct net_data *net_data = init();
70 
71 	return (innetgr_p(netgroup, host, user, domain, net_data));
72 }
73 
74 int
75 getnetgrent(NGR_R_CONST char **host, NGR_R_CONST char **user,
76 	    NGR_R_CONST char **domain)
77 {
78 	struct net_data *net_data = init();
79 	const char *ch, *cu, *cd;
80 	int ret;
81 
82 	ret = getnetgrent_p(&ch, &cu, &cd, net_data);
83 	if (ret != 1)
84 		return (ret);
85 
86 	DE_CONST(ch, *host);
87 	DE_CONST(cu, *user);
88 	DE_CONST(cd, *domain);
89 	return (ret);
90 }
91 
92 /* Shared private. */
93 
94 void
95 setnetgrent_p(const char *netgroup, struct net_data *net_data) {
96 	struct irs_ng *ng;
97 
98 	if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
99 		(*ng->rewind)(ng, netgroup);
100 }
101 
102 void
103 endnetgrent_p(struct net_data *net_data) {
104 	struct irs_ng *ng;
105 
106 	if (!net_data)
107 		return;
108 	if ((ng = net_data->ng) != NULL)
109 		(*ng->close)(ng);
110 	net_data->ng = NULL;
111 }
112 
113 int
114 innetgr_p(const char *netgroup, const char *host,
115 	  const char *user, const char *domain,
116 	  struct net_data *net_data) {
117 	struct irs_ng *ng;
118 
119 	if (!net_data || !(ng = net_data->ng))
120 		return (0);
121 	return ((*ng->test)(ng, netgroup, host, user, domain));
122 }
123 
124 int
125 getnetgrent_p(const char **host, const char **user, const char **domain,
126 	      struct net_data *net_data ) {
127 	struct irs_ng *ng;
128 
129 	if (!net_data || !(ng = net_data->ng))
130 		return (0);
131 	return ((*ng->next)(ng, host, user, domain));
132 }
133 
134 /* Private */
135 
136 static struct net_data *
137 init(void) {
138 	struct net_data *net_data;
139 
140 	if (!(net_data = net_data_init(NULL)))
141 		goto error;
142 	if (!net_data->ng) {
143 		net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
144 		if (!net_data->ng) {
145   error:
146 			errno = EIO;
147 			return (NULL);
148 		}
149 	}
150 
151 	return (net_data);
152 }
153 
154 #endif /*__BIND_NOSTATIC*/
155 
156 /*! \file */
157