2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 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: nis_ng.c,v 1.2.206.1 2004/03/09 08:33:38 marka Exp $";
24 #include "port_before.h"
27 static int __bind_irs_nis_unneeded;
30 #include <sys/types.h>
31 #include <netinet/in.h>
34 #include <rpcsvc/yp_prot.h>
35 #include <rpcsvc/ypclnt.h>
37 #include <isc/assertions.h>
45 #include <netinet/in.h>
47 #undef T_NULL /* Silence re-definition warning of T_NULL. */
49 #include <arpa/nameser.h>
52 #include <isc/memcluster.h>
55 #include "port_after.h"
77 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
79 static /*const*/ char netgroup_map[] = "netgroup";
83 static void ng_close(struct irs_ng *);
84 static int ng_next(struct irs_ng *, const char **,
85 const char **, const char **);
86 static int ng_test(struct irs_ng *,
87 const char *, const char *,
88 const char *, const char *);
89 static void ng_rewind(struct irs_ng *, const char *);
90 static void ng_minimize(struct irs_ng *);
92 static void add_group_to_list(struct pvt *, const char *, int);
93 static void add_tuple_to_list(struct pvt *, const char *, char *);
94 static void tmpfree(struct pvt *);
99 irs_nis_ng(struct irs_acc *this) {
103 if (!(ng = memget(sizeof *ng))) {
107 memset(ng, 0x5e, sizeof *ng);
108 if (!(pvt = memget(sizeof *pvt))) {
109 memput(ng, sizeof *ng);
113 memset(pvt, 0, sizeof *pvt);
114 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
116 ng->close = ng_close;
119 ng->rewind = ng_rewind;
120 ng->minimize = ng_minimize;
127 ng_close(struct irs_ng *this) {
128 struct pvt *pvt = (struct pvt *)this->private;
131 memput(pvt, sizeof *pvt);
132 memput(this, sizeof *this);
136 ng_next(struct irs_ng *this, const char **host, const char **user, const char **domain) {
137 struct pvt *pvt = (struct pvt *)this->private;
141 *host = pvt->cur->host;
142 *user = pvt->cur->user;
143 *domain = pvt->cur->domain;
144 pvt->cur = pvt->cur->next;
149 ng_test(struct irs_ng *this, const char *name,
150 const char *host, const char *user, const char *domain)
152 struct pvt *pvt = (struct pvt *)this->private;
156 add_group_to_list(pvt, name, strlen(name));
157 for (cur = pvt->tmp; cur; cur = cur->next) {
158 if ((!host || !cur->host || !strcmp(host, cur->host)) &&
159 (!user || !cur->user || !strcmp(user, cur->user)) &&
160 (!domain || !cur->domain || !strcmp(domain, cur->domain)))
164 return ((cur == NULL) ? 0 : 1);
168 ng_rewind(struct irs_ng *this, const char *name) {
169 struct pvt *pvt = (struct pvt *)this->private;
171 /* Either hand back or free the existing list. */
173 if (pvt->tmp && !strcmp(pvt->tmpgroup, name))
177 pvt->tmpgroup = strdup(name);
178 add_group_to_list(pvt, name, strlen(name));
184 ng_minimize(struct irs_ng *this) {
192 add_group_to_list(struct pvt *pvt, const char *name, int len) {
193 char *vdata, *cp, *np;
198 /* Don't add the same group to the list more than once. */
199 for (tmp = pvt->tmp; tmp; tmp = tmp->next)
200 if (!strcmp(tmp->name, name))
203 DE_CONST(name, nametmp);
204 r = yp_match(pvt->nis_domain, netgroup_map, nametmp, len,
208 if (*cp && cp[strlen(cp)-1] == '\n')
209 cp[strlen(cp)-1] = '\0';
210 for ( ; cp; cp = np) {
211 np = strchr(cp, ' ');
215 add_tuple_to_list(pvt, name, cp);
217 add_group_to_list(pvt, cp, strlen(cp));
224 add_tuple_to_list(struct pvt *pvt, const char *name, char *cp) {
228 INSIST(*cp++ == '(');
230 tmp = malloc(sizeof *tmp + strlen(name) + sizeof '\0' +
231 strlen(cp) - sizeof ')');
234 memset(tmp, 0, sizeof *tmp);
235 tp = ((char *)tmp) + sizeof *tmp;
240 tp += strlen(tp) + 1;
243 if (!(np = strchr(cp, ',')))
248 tp += strlen(tp) + 1;
252 if (!(np = strchr(cp, ',')))
257 tp += strlen(tp) + 1;
261 if (!(np = strchr(cp, ')')))
268 * Empty string in file means wildcard, but
269 * NULL string in return value means wildcard.
278 /* Add to list (LIFO). */
279 tmp->next = pvt->tmp;
288 tmpfree(struct pvt *pvt) {
289 struct tmpgrp *cur, *next;
293 pvt->tmpgroup = NULL;
295 for (cur = pvt->tmp; cur; cur = next) {
302 #endif /*WANT_IRS_NIS*/