Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / bind / irs / irp_gr.c
blob1f40e76022d37daccf371dbbd73134406e45bebe
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.4 2005/04/27 04:56:27 sra 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. */
57 /*! \file
58 * \brief
59 * Module for the getnetgrent(3) family to use when connected to a
60 * remote irp daemon.
61 * \brief
62 * See irpd.c for justification of caching done here.
66 struct pvt {
67 struct irp_p *girpdata; /*%< global IRP data */
68 int warned;
69 struct group group;
72 /* Forward. */
74 static void gr_close(struct irs_gr *);
75 static struct group * gr_next(struct irs_gr *);
76 static struct group * gr_byname(struct irs_gr *, const char *);
77 static struct group * gr_bygid(struct irs_gr *, gid_t);
78 static void gr_rewind(struct irs_gr *);
79 static void gr_minimize(struct irs_gr *);
81 /* Private */
82 static void free_group(struct group *gr);
85 /* Public. */
87 /*%
88 * Initialize the group sub-module.
92 struct irs_gr *
93 irs_irp_gr(struct irs_acc *this) {
94 struct irs_gr *gr;
95 struct pvt *pvt;
97 if (!(gr = memget(sizeof *gr))) {
98 errno = ENOMEM;
99 return (NULL);
101 memset(gr, 0x0, sizeof *gr);
103 if (!(pvt = memget(sizeof *pvt))) {
104 memput(gr, sizeof *gr);
105 errno = ENOMEM;
106 return (NULL);
108 memset(pvt, 0x0, sizeof *pvt);
109 pvt->girpdata = this->private;
111 gr->private = pvt;
112 gr->close = gr_close;
113 gr->next = gr_next;
114 gr->byname = gr_byname;
115 gr->bygid = gr_bygid;
116 gr->rewind = gr_rewind;
117 gr->list = make_group_list;
118 gr->minimize = gr_minimize;
119 return (gr);
122 /* Methods. */
125 * Close the sub-module.
129 static void
130 gr_close(struct irs_gr *this) {
131 struct pvt *pvt = (struct pvt *)this->private;
133 gr_minimize(this);
135 memput(pvt, sizeof *pvt);
136 memput(this, sizeof *this);
140 * Gets the next group out of the cached data and returns it.
144 static struct group *
145 gr_next(struct irs_gr *this) {
146 struct pvt *pvt = (struct pvt *)this->private;
147 struct group *gr = &pvt->group;
148 char *body;
149 size_t bodylen;
150 int code;
151 char text[256];
153 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
154 return (NULL);
157 if (irs_irp_send_command(pvt->girpdata, "getgrent") != 0) {
158 return (NULL);
161 if (irs_irp_get_full_response(pvt->girpdata, &code,
162 text, sizeof text,
163 &body, &bodylen) != 0) {
164 if (irp_log_errors) {
165 syslog(LOG_WARNING, "getgrent failed: %s", text);
167 return (NULL);
170 if (code == IRPD_GETGROUP_OK) {
171 free_group(gr);
172 if (irp_unmarshall_gr(gr, body) != 0) {
173 gr = NULL;
175 } else {
176 gr = NULL;
179 if (body != NULL) {
180 memput(body, bodylen);
183 return (gr);
187 * Gets a group by name from irpd and returns it.
191 static struct group *
192 gr_byname(struct irs_gr *this, const char *name) {
193 struct pvt *pvt = (struct pvt *)this->private;
194 struct group *gr = &pvt->group;
195 char *body;
196 size_t bodylen;
197 int code;
198 char text[256];
201 if (gr->gr_name != NULL && strcmp(name, gr->gr_name) == 0) {
202 return (gr);
205 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
206 return (NULL);
209 if (irs_irp_send_command(pvt->girpdata, "getgrnam %s", name) != 0)
210 return (NULL);
212 if (irs_irp_get_full_response(pvt->girpdata, &code,
213 text, sizeof text,
214 &body, &bodylen) != 0) {
215 return (NULL);
218 if (code == IRPD_GETGROUP_OK) {
219 free_group(gr);
220 if (irp_unmarshall_gr(gr, body) != 0) {
221 gr = NULL;
223 } else {
224 gr = NULL;
227 if (body != NULL) {
228 memput(body, bodylen);
231 return (gr);
235 * Gets a group by gid from irpd and returns it.
239 static struct group *
240 gr_bygid(struct irs_gr *this, gid_t gid) {
241 struct pvt *pvt = (struct pvt *)this->private;
242 struct group *gr = &pvt->group;
243 char *body;
244 size_t bodylen;
245 int code;
246 char text[256];
248 if (gr->gr_name != NULL && (gid_t)gr->gr_gid == gid) {
249 return (gr);
252 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
253 return (NULL);
256 if (irs_irp_send_command(pvt->girpdata, "getgrgid %d", gid) != 0)
257 return (NULL);
259 if (irs_irp_get_full_response(pvt->girpdata, &code,
260 text, sizeof text,
261 &body, &bodylen) != 0) {
262 return (NULL);
265 if (code == IRPD_GETGROUP_OK) {
266 free_group(gr);
267 if (irp_unmarshall_gr(gr, body) != 0) {
268 gr = NULL;
270 } else {
271 gr = NULL;
274 if (body != NULL) {
275 memput(body, bodylen);
278 return (gr);
282 * void gr_rewind(struct irs_gr *this)
286 static void
287 gr_rewind(struct irs_gr *this) {
288 struct pvt *pvt = (struct pvt *)this->private;
289 char text[256];
290 int code;
292 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
293 return;
296 if (irs_irp_send_command(pvt->girpdata, "setgrent") != 0) {
297 return;
300 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
301 if (code != IRPD_GETGROUP_SETOK) {
302 if (irp_log_errors) {
303 syslog(LOG_WARNING, "setgrent failed: %s", text);
307 return;
311 * Frees up cached data and disconnects(if necessary) from the remote.
315 static void
316 gr_minimize(struct irs_gr *this) {
317 struct pvt *pvt = (struct pvt *)this->private;
319 free_group(&pvt->group);
320 irs_irp_disconnect(pvt->girpdata);
323 /* Private. */
326 * static void free_group(struct group *gr);
328 * Deallocate all the memory irp_unmarshall_gr allocated.
332 static void
333 free_group(struct group *gr) {
334 char **p;
336 if (gr == NULL)
337 return;
339 if (gr->gr_name != NULL)
340 free(gr->gr_name);
342 if (gr->gr_passwd != NULL)
343 free(gr->gr_passwd);
345 for (p = gr->gr_mem ; p != NULL && *p != NULL ; p++)
346 free(*p);
348 if (gr->gr_mem)
349 free(gr->gr_mem);
351 if (p != NULL)
352 free(p);
356 #endif /* WANT_IRS_GR */
357 /*! \file */