No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / nis_ng.c
blob0e0319560e9757e82695015384a9c7a0a0cc6e44
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 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(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "Id: nis_ng.c,v 1.4 2005/04/27 04:56:32 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #ifndef WANT_IRS_NIS
29 static int __bind_irs_nis_unneeded;
30 #else
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <rpc/rpc.h>
35 #include <rpc/xdr.h>
36 #include <rpcsvc/yp_prot.h>
37 #include <rpcsvc/ypclnt.h>
39 #include <isc/assertions.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <netinet/in.h>
48 #ifdef T_NULL
49 #undef T_NULL /* Silence re-definition warning of T_NULL. */
50 #endif
51 #include <arpa/nameser.h>
52 #include <resolv.h>
54 #include <isc/memcluster.h>
55 #include <irs.h>
57 #include "port_after.h"
59 #include "irs_p.h"
60 #include "nis_p.h"
62 /* Definitions */
64 struct tmpgrp {
65 const char * name;
66 const char * host;
67 const char * user;
68 const char * domain;
69 struct tmpgrp * next;
72 struct pvt {
73 char * nis_domain;
74 struct tmpgrp * tmp;
75 struct tmpgrp * cur;
76 char * tmpgroup;
79 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
81 static /*const*/ char netgroup_map[] = "netgroup";
83 /* Forward */
85 static void ng_close(struct irs_ng *);
86 static int ng_next(struct irs_ng *, const char **,
87 const char **, const char **);
88 static int ng_test(struct irs_ng *,
89 const char *, const char *,
90 const char *, const char *);
91 static void ng_rewind(struct irs_ng *, const char *);
92 static void ng_minimize(struct irs_ng *);
94 static void add_group_to_list(struct pvt *, const char *, int);
95 static void add_tuple_to_list(struct pvt *, const char *, char *);
96 static void tmpfree(struct pvt *);
98 /* Public */
100 struct irs_ng *
101 irs_nis_ng(struct irs_acc *this) {
102 struct irs_ng *ng;
103 struct pvt *pvt;
105 if (!(ng = memget(sizeof *ng))) {
106 errno = ENOMEM;
107 return (NULL);
109 memset(ng, 0x5e, sizeof *ng);
110 if (!(pvt = memget(sizeof *pvt))) {
111 memput(ng, sizeof *ng);
112 errno = ENOMEM;
113 return (NULL);
115 memset(pvt, 0, sizeof *pvt);
116 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
117 ng->private = pvt;
118 ng->close = ng_close;
119 ng->next = ng_next;
120 ng->test = ng_test;
121 ng->rewind = ng_rewind;
122 ng->minimize = ng_minimize;
123 return (ng);
126 /* Methods */
128 static void
129 ng_close(struct irs_ng *this) {
130 struct pvt *pvt = (struct pvt *)this->private;
132 tmpfree(pvt);
133 memput(pvt, sizeof *pvt);
134 memput(this, sizeof *this);
137 static int
138 ng_next(struct irs_ng *this, const char **host, const char **user, const char **domain) {
139 struct pvt *pvt = (struct pvt *)this->private;
141 if (!pvt->cur)
142 return (0);
143 *host = pvt->cur->host;
144 *user = pvt->cur->user;
145 *domain = pvt->cur->domain;
146 pvt->cur = pvt->cur->next;
147 return (1);
150 static int
151 ng_test(struct irs_ng *this, const char *name,
152 const char *host, const char *user, const char *domain)
154 struct pvt *pvt = (struct pvt *)this->private;
155 struct tmpgrp *cur;
157 tmpfree(pvt);
158 add_group_to_list(pvt, name, strlen(name));
159 for (cur = pvt->tmp; cur; cur = cur->next) {
160 if ((!host || !cur->host || !strcmp(host, cur->host)) &&
161 (!user || !cur->user || !strcmp(user, cur->user)) &&
162 (!domain || !cur->domain || !strcmp(domain, cur->domain)))
163 break;
165 tmpfree(pvt);
166 return ((cur == NULL) ? 0 : 1);
169 static void
170 ng_rewind(struct irs_ng *this, const char *name) {
171 struct pvt *pvt = (struct pvt *)this->private;
173 /* Either hand back or free the existing list. */
174 if (pvt->tmpgroup) {
175 if (pvt->tmp && !strcmp(pvt->tmpgroup, name))
176 goto reset;
177 tmpfree(pvt);
179 pvt->tmpgroup = strdup(name);
180 add_group_to_list(pvt, name, strlen(name));
181 reset:
182 pvt->cur = pvt->tmp;
185 static void
186 ng_minimize(struct irs_ng *this) {
187 UNUSED(this);
188 /* NOOP */
191 /* Private */
193 static void
194 add_group_to_list(struct pvt *pvt, const char *name, int len) {
195 char *vdata, *cp, *np;
196 struct tmpgrp *tmp;
197 int vlen, r;
198 char *nametmp;
200 /* Don't add the same group to the list more than once. */
201 for (tmp = pvt->tmp; tmp; tmp = tmp->next)
202 if (!strcmp(tmp->name, name))
203 return;
205 DE_CONST(name, nametmp);
206 r = yp_match(pvt->nis_domain, netgroup_map, nametmp, len,
207 &vdata, &vlen);
208 if (r == 0) {
209 cp = vdata;
210 if (*cp && cp[strlen(cp)-1] == '\n')
211 cp[strlen(cp)-1] = '\0';
212 for ( ; cp; cp = np) {
213 np = strchr(cp, ' ');
214 if (np)
215 *np++ = '\0';
216 if (*cp == '(')
217 add_tuple_to_list(pvt, name, cp);
218 else
219 add_group_to_list(pvt, cp, strlen(cp));
221 free(vdata);
225 static void
226 add_tuple_to_list(struct pvt *pvt, const char *name, char *cp) {
227 struct tmpgrp *tmp;
228 char *tp, *np;
230 INSIST(*cp++ == '(');
232 tmp = malloc(sizeof *tmp + strlen(name) + sizeof '\0' +
233 strlen(cp) - sizeof ')');
234 if (!tmp)
235 return;
236 memset(tmp, 0, sizeof *tmp);
237 tp = ((char *)tmp) + sizeof *tmp;
239 /* Name */
240 strcpy(tp, name);
241 tmp->name = tp;
242 tp += strlen(tp) + 1;
244 /* Host */
245 if (!(np = strchr(cp, ',')))
246 goto cleanup;
247 *np++ = '\0';
248 strcpy(tp, cp);
249 tmp->host = tp;
250 tp += strlen(tp) + 1;
251 cp = np;
253 /* User */
254 if (!(np = strchr(cp, ',')))
255 goto cleanup;
256 *np++ = '\0';
257 strcpy(tp, cp);
258 tmp->user = tp;
259 tp += strlen(tp) + 1;
260 cp = np;
262 /* Domain */
263 if (!(np = strchr(cp, ')')))
264 goto cleanup;
265 *np++ = '\0';
266 strcpy(tp, cp);
267 tmp->domain = tp;
270 * Empty string in file means wildcard, but
271 * NULL string in return value means wildcard.
273 if (!*tmp->host)
274 tmp->host = NULL;
275 if (!*tmp->user)
276 tmp->user = NULL;
277 if (!*tmp->domain)
278 tmp->domain = NULL;
280 /* Add to list (LIFO). */
281 tmp->next = pvt->tmp;
282 pvt->tmp = tmp;
283 return;
285 cleanup:
286 free(tmp);
289 static void
290 tmpfree(struct pvt *pvt) {
291 struct tmpgrp *cur, *next;
293 if (pvt->tmpgroup) {
294 free(pvt->tmpgroup);
295 pvt->tmpgroup = NULL;
297 for (cur = pvt->tmp; cur; cur = next) {
298 next = cur->next;
299 free(cur);
301 pvt->tmp = NULL;
304 #endif /*WANT_IRS_NIS*/
306 /*! \file */