Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / irp_ng.c
blob49a7346d717916dd0b9c953cacbbd7094ad9caaa
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996, 1998 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid[] = "Id: irp_ng.c,v 1.4 2006/12/07 04:46:27 marka Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <syslog.h>
35 #include <irs.h>
36 #include <irp.h>
37 #include <isc/memcluster.h>
38 #include <isc/irpmarshall.h>
40 #include "irs_p.h"
41 #include "irp_p.h"
43 #include "port_after.h"
45 /* Definitions */
47 struct pvt {
48 struct irp_p *girpdata;
49 int warned;
53 /* Forward */
55 static void ng_rewind(struct irs_ng *, const char*);
56 static void ng_close(struct irs_ng *);
57 static int ng_next(struct irs_ng *, const char **, const char **,
58 const char **);
59 static int ng_test(struct irs_ng *, const char *,
60 const char *, const char *,
61 const char *);
62 static void ng_minimize(struct irs_ng *);
65 /* Public */
67 /*%
68 * Intialize the irp netgroup module.
72 struct irs_ng *
73 irs_irp_ng(struct irs_acc *this) {
74 struct irs_ng *ng;
75 struct pvt *pvt;
77 if (!(ng = memget(sizeof *ng))) {
78 errno = ENOMEM;
79 return (NULL);
81 memset(ng, 0x5e, sizeof *ng);
83 if (!(pvt = memget(sizeof *pvt))) {
84 memput(ng, sizeof *ng);
85 errno = ENOMEM;
86 return (NULL);
88 memset(pvt, 0, sizeof *pvt);
89 pvt->girpdata = this->private;
91 ng->private = pvt;
92 ng->close = ng_close;
93 ng->next = ng_next;
94 ng->test = ng_test;
95 ng->rewind = ng_rewind;
96 ng->minimize = ng_minimize;
97 return (ng);
100 /* Methods */
105 * void ng_close(struct irs_ng *this)
109 static void
110 ng_close(struct irs_ng *this) {
111 struct pvt *pvt = (struct pvt *)this->private;
113 ng_minimize(this);
115 memput(pvt, sizeof *pvt);
116 memput(this, sizeof *this);
123 * void ng_rewind(struct irs_ng *this, const char *group)
128 static void
129 ng_rewind(struct irs_ng *this, const char *group) {
130 struct pvt *pvt = (struct pvt *)this->private;
131 char text[256];
132 int code;
134 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
135 return;
138 if (irs_irp_send_command(pvt->girpdata,
139 "setnetgrent %s", group) != 0) {
140 return;
143 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
144 if (code != IRPD_GETNETGR_SETOK) {
145 if (irp_log_errors) {
146 syslog(LOG_WARNING, "setnetgrent(%s) failed: %s",
147 group, text);
151 return;
155 * Get the next netgroup item from the cache.
159 static int
160 ng_next(struct irs_ng *this, const char **host, const char **user,
161 const char **domain)
163 struct pvt *pvt = (struct pvt *)this->private;
164 int code;
165 char *body = NULL;
166 size_t bodylen;
167 int rval = 0;
168 char text[256];
170 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
171 return (0);
174 if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0)
175 return (0);
177 if (irs_irp_get_full_response(pvt->girpdata, &code,
178 text, sizeof text,
179 &body, &bodylen) != 0) {
180 return (0);
183 if (code == IRPD_GETNETGR_OK) {
184 if (irp_unmarshall_ng(host, user, domain, body) == 0) {
185 rval = 1;
189 if (body != NULL) {
190 memput(body, bodylen);
193 return (rval);
197 * Search for a match in a netgroup.
201 static int
202 ng_test(struct irs_ng *this, const char *name,
203 const char *host, const char *user, const char *domain)
205 struct pvt *pvt = (struct pvt *)this->private;
206 char *body = NULL;
207 size_t bodylen = 0;
208 int code;
209 char text[256];
210 int rval = 0;
212 UNUSED(name);
214 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
215 return (0);
218 if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) {
219 return (0);
222 if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) {
223 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
224 if (code == IRPD_GETNETGR_MATCHES) {
225 rval = 1;
229 memput(body, bodylen);
231 return (rval);
238 * void ng_minimize(struct irs_ng *this)
242 static void
243 ng_minimize(struct irs_ng *this) {
244 struct pvt *pvt = (struct pvt *)this->private;
246 irs_irp_disconnect(pvt->girpdata);
252 /* Private */
255 /*! \file */