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