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