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