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