xref: /netbsd-src/external/bsd/libbind/dist/irs/irp_ng.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: irp_ng.c,v 1.1.1.2 2012/09/09 16:07:59 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1996, 1998 by Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and 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
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid[] = "Id: irp_ng.c,v 1.4 2006/12/07 04:46:27 marka Exp ";
22 #endif
23 
24 /* Imports */
25 
26 #include "port_before.h"
27 
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <syslog.h>
34 
35 #include <irs.h>
36 #include <irp.h>
37 #include <isc/memcluster.h>
38 #include <isc/irpmarshall.h>
39 
40 #include "irs_p.h"
41 #include "irp_p.h"
42 
43 #include "port_after.h"
44 
45 /* Definitions */
46 
47 struct pvt {
48 	struct irp_p	       *girpdata;
49 	int			warned;
50 };
51 
52 
53 /* Forward */
54 
55 static void		ng_rewind(struct irs_ng *, const char*);
56 static void		ng_close(struct irs_ng *);
57 static int		ng_next(struct irs_ng *, const char **, const char **,
58 				const char **);
59 static int		ng_test(struct irs_ng *, const char *,
60 				const char *, const char *,
61 				const char *);
62 static void		ng_minimize(struct irs_ng *);
63 
64 
65 /* Public */
66 
67 /*%
68  *	Intialize the irp netgroup module.
69  *
70  */
71 
72 struct irs_ng *
irs_irp_ng(struct irs_acc * this)73 irs_irp_ng(struct irs_acc *this) {
74 	struct irs_ng *ng;
75 	struct pvt *pvt;
76 
77 	if (!(ng = memget(sizeof *ng))) {
78 		errno = ENOMEM;
79 		return (NULL);
80 	}
81 	memset(ng, 0x5e, sizeof *ng);
82 
83 	if (!(pvt = memget(sizeof *pvt))) {
84 		memput(ng, sizeof *ng);
85 		errno = ENOMEM;
86 		return (NULL);
87 	}
88 	memset(pvt, 0, sizeof *pvt);
89 	pvt->girpdata = this->private;
90 
91 	ng->private = pvt;
92 	ng->close = ng_close;
93 	ng->next = ng_next;
94 	ng->test = ng_test;
95 	ng->rewind = ng_rewind;
96 	ng->minimize = ng_minimize;
97 	return (ng);
98 }
99 
100 /* Methods */
101 
102 
103 
104 /*
105  * void ng_close(struct irs_ng *this)
106  *
107  */
108 
109 static void
ng_close(struct irs_ng * this)110 ng_close(struct irs_ng *this) {
111 	struct pvt *pvt = (struct pvt *)this->private;
112 
113 	ng_minimize(this);
114 
115 	memput(pvt, sizeof *pvt);
116 	memput(this, sizeof *this);
117 }
118 
119 
120 
121 
122 /*
123  * void ng_rewind(struct irs_ng *this, const char *group)
124  *
125  *
126  */
127 
128 static void
ng_rewind(struct irs_ng * this,const char * group)129 ng_rewind(struct irs_ng *this, const char *group) {
130 	struct pvt *pvt = (struct pvt *)this->private;
131 	char text[256];
132 	int code;
133 
134 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
135 		return;
136 	}
137 
138 	if (irs_irp_send_command(pvt->girpdata,
139 				 "setnetgrent %s", group) != 0) {
140 		return;
141 	}
142 
143 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
144 	if (code != IRPD_GETNETGR_SETOK) {
145 		if (irp_log_errors) {
146 			syslog(LOG_WARNING, "setnetgrent(%s) failed: %s",
147 			       group, text);
148 		}
149 	}
150 
151 	return;
152 }
153 
154 /*
155  *	Get the next netgroup item from the cache.
156  *
157  */
158 
159 static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)160 ng_next(struct irs_ng *this, const char **host, const char **user,
161         const char **domain)
162 {
163 	struct pvt *pvt = (struct pvt *)this->private;
164 	int code;
165 	char *body = NULL;
166 	size_t bodylen;
167 	int rval = 0;
168 	char text[256];
169 
170 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
171 		return (0);
172 	}
173 
174 	if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0)
175 		return (0);
176 
177 	if (irs_irp_get_full_response(pvt->girpdata, &code,
178 				      text, sizeof text,
179 				      &body, &bodylen) != 0) {
180 		return (0);
181 	}
182 
183 	if (code == IRPD_GETNETGR_OK) {
184 		if (irp_unmarshall_ng(host, user, domain, body) == 0) {
185 			rval = 1;
186 		}
187 	}
188 
189 	if (body != NULL) {
190 		memput(body, bodylen);
191 	}
192 
193 	return (rval);
194 }
195 
196 /*
197  *	Search for a match in a netgroup.
198  *
199  */
200 
201 static int
ng_test(struct irs_ng * this,const char * name,const char * host,const char * user,const char * domain)202 ng_test(struct irs_ng *this, const char *name,
203 	const char *host, const char *user, const char *domain)
204 {
205 	struct pvt *pvt = (struct pvt *)this->private;
206 	char *body = NULL;
207 	size_t bodylen = 0;
208 	int code;
209 	char text[256];
210 	int rval = 0;
211 
212 	UNUSED(name);
213 
214 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
215 		return (0);
216 	}
217 
218 	if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) {
219 		return (0);
220 	}
221 
222 	if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) {
223 		code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
224 		if (code == IRPD_GETNETGR_MATCHES) {
225 			rval = 1;
226 		}
227 	}
228 
229 	memput(body, bodylen);
230 
231 	return (rval);
232 }
233 
234 
235 
236 
237 /*
238  * void ng_minimize(struct irs_ng *this)
239  *
240  */
241 
242 static void
ng_minimize(struct irs_ng * this)243 ng_minimize(struct irs_ng *this) {
244 	struct pvt *pvt = (struct pvt *)this->private;
245 
246 	irs_irp_disconnect(pvt->girpdata);
247 }
248 
249 
250 
251 
252 /* Private */
253 
254 
255 /*! \file */
256