Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / irp_ng.c
blob6906f1d2210ba4850a7674fb0d9b6ac8b5b2078e
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996, 1998 by Internet Software Consortium.
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.
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.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: irp_ng.c,v 1.1.2.1 2004/03/09 09:17:31 marka Exp $";
20 #endif
22 /* Imports */
24 #include "port_before.h"
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <syslog.h>
33 #include <irs.h>
34 #include <irp.h>
35 #include <isc/memcluster.h>
36 #include <isc/irpmarshall.h>
38 #include "irs_p.h"
39 #include "irp_p.h"
41 #include "port_after.h"
43 /* Definitions */
45 struct pvt {
46 struct irp_p *girpdata;
47 int warned;
51 /* Forward */
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 *);
63 /* Public */
68 * struct irs_ng * irs_irp_ng(struct irs_acc *this)
70 * Notes:
72 * Intialize the irp netgroup module.
76 struct irs_ng *
77 irs_irp_ng(struct irs_acc *this) {
78 struct irs_ng *ng;
79 struct pvt *pvt;
81 if (!(ng = memget(sizeof *ng))) {
82 errno = ENOMEM;
83 return (NULL);
85 memset(ng, 0x5e, sizeof *ng);
87 if (!(pvt = memget(sizeof *pvt))) {
88 memput(ng, sizeof *ng);
89 errno = ENOMEM;
90 return (NULL);
92 memset(pvt, 0, sizeof *pvt);
93 pvt->girpdata = this->private;
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);
104 /* Methods */
109 * void ng_close(struct irs_ng *this)
113 static void
114 ng_close(struct irs_ng *this) {
115 struct pvt *pvt = (struct pvt *)this->private;
117 ng_minimize(this);
119 memput(pvt, sizeof *pvt);
120 memput(this, sizeof *this);
127 * void ng_rewind(struct irs_ng *this, const char *group)
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;
138 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
139 return;
142 if (irs_irp_send_command(pvt->girpdata,
143 "setnetgrent %s", group) != 0) {
144 return;
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);
155 return;
162 * int ng_next(struct irs_ng *this, const char **host, const char **user,
163 * const char **domain)
165 * Notes:
167 * Get the next netgroup item from the cache.
171 static int
172 ng_next(struct irs_ng *this, const char **host, const char **user,
173 const char **domain)
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];
182 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
183 return (0);
186 if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0)
187 return (0);
189 if (irs_irp_get_full_response(pvt->girpdata, &code,
190 text, sizeof text,
191 &body, &bodylen) != 0) {
192 return (0);
195 if (code == IRPD_GETNETGR_OK) {
196 if (irp_unmarshall_ng(host, user, domain, body) == 0) {
197 rval = 1;
201 if (body != NULL) {
202 memput(body, bodylen);
205 return (rval);
211 * int ng_test(struct irs_ng *this, const char *name, const char *host,
212 * const char *user, const char *domain)
214 * Notes:
216 * Search for a match in a netgroup.
220 static int
221 ng_test(struct irs_ng *this, const char *name,
222 const char *host, const char *user, const char *domain)
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;
231 UNUSED(name);
233 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
234 return (0);
237 if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) {
238 return (0);
241 if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) {
242 memput(body, bodylen);
244 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
245 if (code == IRPD_GETNETGR_MATCHES) {
246 rval = 1;
250 return (rval);
257 * void ng_minimize(struct irs_ng *this)
261 static void
262 ng_minimize(struct irs_ng *this) {
263 struct pvt *pvt = (struct pvt *)this->private;
265 irs_irp_disconnect(pvt->girpdata);
271 /* Private */