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 */
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"
59 * Module for the getnetgrent(3) family to use when connected to a
62 * See irpd.c for justification of caching done here.
67 struct irp_p
*girpdata
; /*%< global IRP data */
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
*);
82 static void free_group(struct group
*gr
);
88 * Initialize the group sub-module.
93 irs_irp_gr(struct irs_acc
*this) {
97 if (!(gr
= memget(sizeof *gr
))) {
101 memset(gr
, 0x0, sizeof *gr
);
103 if (!(pvt
= memget(sizeof *pvt
))) {
104 memput(gr
, sizeof *gr
);
108 memset(pvt
, 0x0, sizeof *pvt
);
109 pvt
->girpdata
= this->private;
112 gr
->close
= gr_close
;
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
;
125 * Close the sub-module.
130 gr_close(struct irs_gr
*this) {
131 struct pvt
*pvt
= (struct pvt
*)this->private;
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
;
153 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
157 if (irs_irp_send_command(pvt
->girpdata
, "getgrent") != 0) {
161 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
163 &body
, &bodylen
) != 0) {
164 if (irp_log_errors
) {
165 syslog(LOG_WARNING
, "getgrent failed: %s", text
);
170 if (code
== IRPD_GETGROUP_OK
) {
172 if (irp_unmarshall_gr(gr
, body
) != 0) {
180 memput(body
, bodylen
);
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
;
201 if (gr
->gr_name
!= NULL
&& strcmp(name
, gr
->gr_name
) == 0) {
205 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
209 if (irs_irp_send_command(pvt
->girpdata
, "getgrnam %s", name
) != 0)
212 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
214 &body
, &bodylen
) != 0) {
218 if (code
== IRPD_GETGROUP_OK
) {
220 if (irp_unmarshall_gr(gr
, body
) != 0) {
228 memput(body
, bodylen
);
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
;
248 if (gr
->gr_name
!= NULL
&& (gid_t
)gr
->gr_gid
== gid
) {
252 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
256 if (irs_irp_send_command(pvt
->girpdata
, "getgrgid %d", gid
) != 0)
259 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
261 &body
, &bodylen
) != 0) {
265 if (code
== IRPD_GETGROUP_OK
) {
267 if (irp_unmarshall_gr(gr
, body
) != 0) {
275 memput(body
, bodylen
);
282 * void gr_rewind(struct irs_gr *this)
287 gr_rewind(struct irs_gr
*this) {
288 struct pvt
*pvt
= (struct pvt
*)this->private;
292 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
296 if (irs_irp_send_command(pvt
->girpdata
, "setgrent") != 0) {
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
);
311 * Frees up cached data and disconnects(if necessary) from the remote.
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
);
326 * static void free_group(struct group *gr);
328 * Deallocate all the memory irp_unmarshall_gr allocated.
333 free_group(struct group
*gr
) {
339 if (gr
->gr_name
!= NULL
)
342 if (gr
->gr_passwd
!= NULL
)
345 for (p
= gr
->gr_mem
; p
!= NULL
&& *p
!= NULL
; p
++)
356 #endif /* WANT_IRS_GR */