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