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 */
24 #include "port_before.h"
27 static int __bind_irs_gr_unneeded
;
31 #include <sys/param.h>
32 #include <sys/types.h>
45 #include <isc/memcluster.h>
46 #include <isc/irpmarshall.h>
52 #include "port_after.h"
58 * Module for the getnetgrent(3) family to use when connected to a
61 * See irpd.c for justification of caching done here.
66 struct irp_p
*girpdata
; /* global IRP data */
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
*);
81 static void free_group(struct group
*gr
);
91 * struct irs_gr * irs_irp_gr(struct irs_acc *this)
95 * Initialize the group sub-module.
104 irs_irp_gr(struct irs_acc
*this) {
108 if (!(gr
= memget(sizeof *gr
))) {
112 memset(gr
, 0x0, sizeof *gr
);
114 if (!(pvt
= memget(sizeof *pvt
))) {
115 memput(gr
, sizeof *gr
);
119 memset(pvt
, 0x0, sizeof *pvt
);
120 pvt
->girpdata
= this->private;
123 gr
->close
= gr_close
;
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
;
138 * void gr_close(struct irs_gr *this)
142 * Close the sub-module.
147 gr_close(struct irs_gr
*this) {
148 struct pvt
*pvt
= (struct pvt
*)this->private;
152 memput(pvt
, sizeof *pvt
);
153 memput(this, sizeof *this);
160 * struct group * gr_next(struct irs_gr *this)
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
;
177 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
181 if (irs_irp_send_command(pvt
->girpdata
, "getgrent") != 0) {
185 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
187 &body
, &bodylen
) != 0) {
188 if (irp_log_errors
) {
189 syslog(LOG_WARNING
, "getgrent failed: %s", text
);
194 if (code
== IRPD_GETGROUP_OK
) {
196 if (irp_unmarshall_gr(gr
, body
) != 0) {
204 memput(body
, bodylen
);
215 * struct group * gr_byname(struct irs_gr *this, const char *name)
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
;
233 if (gr
->gr_name
!= NULL
&& strcmp(name
, gr
->gr_name
) == 0) {
237 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
241 if (irs_irp_send_command(pvt
->girpdata
, "getgrnam %s", name
) != 0)
244 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
246 &body
, &bodylen
) != 0) {
250 if (code
== IRPD_GETGROUP_OK
) {
252 if (irp_unmarshall_gr(gr
, body
) != 0) {
260 memput(body
, bodylen
);
271 * struct group * gr_bygid(struct irs_gr *this, gid_t gid)
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
;
288 if (gr
->gr_name
!= NULL
&& (gid_t
)gr
->gr_gid
== gid
) {
292 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
296 if (irs_irp_send_command(pvt
->girpdata
, "getgrgid %d", gid
) != 0)
299 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
301 &body
, &bodylen
) != 0) {
305 if (code
== IRPD_GETGROUP_OK
) {
307 if (irp_unmarshall_gr(gr
, body
) != 0) {
315 memput(body
, bodylen
);
325 * void gr_rewind(struct irs_gr *this)
330 gr_rewind(struct irs_gr
*this) {
331 struct pvt
*pvt
= (struct pvt
*)this->private;
335 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
339 if (irs_irp_send_command(pvt
->girpdata
, "setgrent") != 0) {
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
);
357 * void gr_minimize(struct irs_gr *this)
361 * Frees up cached data and disconnects(if necessary) from the remote.
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
);
378 * static void free_group(struct group *gr);
380 * Deallocate all the memory irp_unmarshall_gr allocated.
385 free_group(struct group
*gr
) {
391 if (gr
->gr_name
!= NULL
)
394 if (gr
->gr_passwd
!= NULL
)
397 for (p
= gr
->gr_mem
; p
!= NULL
&& *p
!= NULL
; p
++)
408 #endif /* WANT_IRS_GR */