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