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