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";
26 #include "port_before.h"
29 static int __bind_irs_nis_unneeded
;
32 #include <sys/types.h>
33 #include <netinet/in.h>
36 #include <rpcsvc/yp_prot.h>
37 #include <rpcsvc/ypclnt.h>
39 #include <isc/assertions.h>
47 #include <netinet/in.h>
49 #undef T_NULL /* Silence re-definition warning of T_NULL. */
51 #include <arpa/nameser.h>
54 #include <isc/memcluster.h>
57 #include "port_after.h"
79 enum do_what
{ do_none
= 0x0, do_key
= 0x1, do_val
= 0x2, do_all
= 0x3 };
81 static /*const*/ char netgroup_map
[] = "netgroup";
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
*);
101 irs_nis_ng(struct irs_acc
*this) {
105 if (!(ng
= memget(sizeof *ng
))) {
109 memset(ng
, 0x5e, sizeof *ng
);
110 if (!(pvt
= memget(sizeof *pvt
))) {
111 memput(ng
, sizeof *ng
);
115 memset(pvt
, 0, sizeof *pvt
);
116 pvt
->nis_domain
= ((struct nis_p
*)this->private)->domain
;
118 ng
->close
= ng_close
;
121 ng
->rewind
= ng_rewind
;
122 ng
->minimize
= ng_minimize
;
129 ng_close(struct irs_ng
*this) {
130 struct pvt
*pvt
= (struct pvt
*)this->private;
133 memput(pvt
, sizeof *pvt
);
134 memput(this, sizeof *this);
138 ng_next(struct irs_ng
*this, const char **host
, const char **user
, const char **domain
) {
139 struct pvt
*pvt
= (struct pvt
*)this->private;
143 *host
= pvt
->cur
->host
;
144 *user
= pvt
->cur
->user
;
145 *domain
= pvt
->cur
->domain
;
146 pvt
->cur
= pvt
->cur
->next
;
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;
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
)))
166 return ((cur
== NULL
) ? 0 : 1);
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. */
175 if (pvt
->tmp
&& !strcmp(pvt
->tmpgroup
, name
))
179 pvt
->tmpgroup
= strdup(name
);
180 add_group_to_list(pvt
, name
, strlen(name
));
186 ng_minimize(struct irs_ng
*this) {
194 add_group_to_list(struct pvt
*pvt
, const char *name
, int len
) {
195 char *vdata
, *cp
, *np
;
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
))
205 DE_CONST(name
, nametmp
);
206 r
= yp_match(pvt
->nis_domain
, netgroup_map
, nametmp
, len
,
210 if (*cp
&& cp
[strlen(cp
)-1] == '\n')
211 cp
[strlen(cp
)-1] = '\0';
212 for ( ; cp
; cp
= np
) {
213 np
= strchr(cp
, ' ');
217 add_tuple_to_list(pvt
, name
, cp
);
219 add_group_to_list(pvt
, cp
, strlen(cp
));
226 add_tuple_to_list(struct pvt
*pvt
, const char *name
, char *cp
) {
230 INSIST(*cp
++ == '(');
232 tmp
= malloc(sizeof *tmp
+ strlen(name
) + sizeof '\0' +
233 strlen(cp
) - sizeof ')');
236 memset(tmp
, 0, sizeof *tmp
);
237 tp
= ((char *)tmp
) + sizeof *tmp
;
242 tp
+= strlen(tp
) + 1;
245 if (!(np
= strchr(cp
, ',')))
250 tp
+= strlen(tp
) + 1;
254 if (!(np
= strchr(cp
, ',')))
259 tp
+= strlen(tp
) + 1;
263 if (!(np
= strchr(cp
, ')')))
270 * Empty string in file means wildcard, but
271 * NULL string in return value means wildcard.
280 /* Add to list (LIFO). */
281 tmp
->next
= pvt
->tmp
;
290 tmpfree(struct pvt
*pvt
) {
291 struct tmpgrp
*cur
, *next
;
295 pvt
->tmpgroup
= NULL
;
297 for (cur
= pvt
->tmp
; cur
; cur
= next
) {
304 #endif /*WANT_IRS_NIS*/