Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / irp_gr.c
blobfa17c4113996137fc156a2de60ab672d17c1e07d
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions 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(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: irp_gr.c,v 1.2.2.1 2004/03/09 09:17:31 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 /* extern */
24 #include "port_before.h"
26 #ifndef WANT_IRS_PW
27 static int __bind_irs_gr_unneeded;
28 #else
30 #include <syslog.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <grp.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <syslog.h>
43 #include <irs.h>
44 #include <irp.h>
45 #include <isc/memcluster.h>
46 #include <isc/irpmarshall.h>
48 #include "irs_p.h"
49 #include "lcl_p.h"
50 #include "irp_p.h"
52 #include "port_after.h"
55 /* Types. */
58 * Module for the getnetgrent(3) family to use when connected to a
59 * remote irp daemon.
61 * See irpd.c for justification of caching done here.
65 struct pvt {
66 struct irp_p *girpdata; /* global IRP data */
67 int warned;
68 struct group group;
71 /* Forward. */
73 static void gr_close(struct irs_gr *);
74 static struct group * gr_next(struct irs_gr *);
75 static struct group * gr_byname(struct irs_gr *, const char *);
76 static struct group * gr_bygid(struct irs_gr *, gid_t);
77 static void gr_rewind(struct irs_gr *);
78 static void gr_minimize(struct irs_gr *);
80 /* Private */
81 static void free_group(struct group *gr);
84 /* Public. */
91 * struct irs_gr * irs_irp_gr(struct irs_acc *this)
93 * Notes:
95 * Initialize the group sub-module.
97 * Notes:
99 * Module data.
103 struct irs_gr *
104 irs_irp_gr(struct irs_acc *this) {
105 struct irs_gr *gr;
106 struct pvt *pvt;
108 if (!(gr = memget(sizeof *gr))) {
109 errno = ENOMEM;
110 return (NULL);
112 memset(gr, 0x0, sizeof *gr);
114 if (!(pvt = memget(sizeof *pvt))) {
115 memput(gr, sizeof *gr);
116 errno = ENOMEM;
117 return (NULL);
119 memset(pvt, 0x0, sizeof *pvt);
120 pvt->girpdata = this->private;
122 gr->private = pvt;
123 gr->close = gr_close;
124 gr->next = gr_next;
125 gr->byname = gr_byname;
126 gr->bygid = gr_bygid;
127 gr->rewind = gr_rewind;
128 gr->list = make_group_list;
129 gr->minimize = gr_minimize;
130 return (gr);
133 /* Methods. */
138 * void gr_close(struct irs_gr *this)
140 * Notes:
142 * Close the sub-module.
146 static void
147 gr_close(struct irs_gr *this) {
148 struct pvt *pvt = (struct pvt *)this->private;
150 gr_minimize(this);
152 memput(pvt, sizeof *pvt);
153 memput(this, sizeof *this);
160 * struct group * gr_next(struct irs_gr *this)
162 * Notes:
164 * Gets the next group out of the cached data and returns it.
168 static struct group *
169 gr_next(struct irs_gr *this) {
170 struct pvt *pvt = (struct pvt *)this->private;
171 struct group *gr = &pvt->group;
172 char *body;
173 size_t bodylen;
174 int code;
175 char text[256];
177 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
178 return (NULL);
181 if (irs_irp_send_command(pvt->girpdata, "getgrent") != 0) {
182 return (NULL);
185 if (irs_irp_get_full_response(pvt->girpdata, &code,
186 text, sizeof text,
187 &body, &bodylen) != 0) {
188 if (irp_log_errors) {
189 syslog(LOG_WARNING, "getgrent failed: %s", text);
191 return (NULL);
194 if (code == IRPD_GETGROUP_OK) {
195 free_group(gr);
196 if (irp_unmarshall_gr(gr, body) != 0) {
197 gr = NULL;
199 } else {
200 gr = NULL;
203 if (body != NULL) {
204 memput(body, bodylen);
207 return (gr);
215 * struct group * gr_byname(struct irs_gr *this, const char *name)
217 * Notes:
219 * Gets a group by name from irpd and returns it.
223 static struct group *
224 gr_byname(struct irs_gr *this, const char *name) {
225 struct pvt *pvt = (struct pvt *)this->private;
226 struct group *gr = &pvt->group;
227 char *body;
228 size_t bodylen;
229 int code;
230 char text[256];
233 if (gr->gr_name != NULL && strcmp(name, gr->gr_name) == 0) {
234 return (gr);
237 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
238 return (NULL);
241 if (irs_irp_send_command(pvt->girpdata, "getgrnam %s", name) != 0)
242 return (NULL);
244 if (irs_irp_get_full_response(pvt->girpdata, &code,
245 text, sizeof text,
246 &body, &bodylen) != 0) {
247 return (NULL);
250 if (code == IRPD_GETGROUP_OK) {
251 free_group(gr);
252 if (irp_unmarshall_gr(gr, body) != 0) {
253 gr = NULL;
255 } else {
256 gr = NULL;
259 if (body != NULL) {
260 memput(body, bodylen);
263 return (gr);
271 * struct group * gr_bygid(struct irs_gr *this, gid_t gid)
273 * Notes:
275 * Gets a group by gid from irpd and returns it.
279 static struct group *
280 gr_bygid(struct irs_gr *this, gid_t gid) {
281 struct pvt *pvt = (struct pvt *)this->private;
282 struct group *gr = &pvt->group;
283 char *body;
284 size_t bodylen;
285 int code;
286 char text[256];
288 if (gr->gr_name != NULL && (gid_t)gr->gr_gid == gid) {
289 return (gr);
292 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
293 return (NULL);
296 if (irs_irp_send_command(pvt->girpdata, "getgrgid %d", gid) != 0)
297 return (NULL);
299 if (irs_irp_get_full_response(pvt->girpdata, &code,
300 text, sizeof text,
301 &body, &bodylen) != 0) {
302 return (NULL);
305 if (code == IRPD_GETGROUP_OK) {
306 free_group(gr);
307 if (irp_unmarshall_gr(gr, body) != 0) {
308 gr = NULL;
310 } else {
311 gr = NULL;
314 if (body != NULL) {
315 memput(body, bodylen);
318 return (gr);
325 * void gr_rewind(struct irs_gr *this)
329 static void
330 gr_rewind(struct irs_gr *this) {
331 struct pvt *pvt = (struct pvt *)this->private;
332 char text[256];
333 int code;
335 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
336 return;
339 if (irs_irp_send_command(pvt->girpdata, "setgrent") != 0) {
340 return;
343 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
344 if (code != IRPD_GETGROUP_SETOK) {
345 if (irp_log_errors) {
346 syslog(LOG_WARNING, "setgrent failed: %s", text);
350 return;
357 * void gr_minimize(struct irs_gr *this)
359 * Notes:
361 * Frees up cached data and disconnects(if necessary) from the remote.
365 static void
366 gr_minimize(struct irs_gr *this) {
367 struct pvt *pvt = (struct pvt *)this->private;
369 free_group(&pvt->group);
370 irs_irp_disconnect(pvt->girpdata);
373 /* Private. */
378 * static void free_group(struct group *gr);
380 * Deallocate all the memory irp_unmarshall_gr allocated.
384 static void
385 free_group(struct group *gr) {
386 char **p;
388 if (gr == NULL)
389 return;
391 if (gr->gr_name != NULL)
392 free(gr->gr_name);
394 if (gr->gr_passwd != NULL)
395 free(gr->gr_passwd);
397 for (p = gr->gr_mem ; p != NULL && *p != NULL ; p++)
398 free(*p);
400 if (gr->gr_mem)
401 free(gr->gr_mem);
403 if (p != NULL)
404 free(p);
408 #endif /* WANT_IRS_GR */