1/*	$NetBSD: nis_ng.c,v 1.1.1.2 2012/09/09 16:07:58 christos Exp $	*/
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21static const char rcsid[] = "Id: nis_ng.c,v 1.4 2005/04/27 04:56:32 sra Exp ";
22#endif
23
24/* Imports */
25
26#include "port_before.h"
27
28#ifndef WANT_IRS_NIS
29static int __bind_irs_nis_unneeded;
30#else
31
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>
38
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>
46
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>
53
54#include <isc/memcluster.h>
55#include <irs.h>
56
57#include "port_after.h"
58
59#include "irs_p.h"
60#include "nis_p.h"
61
62/* Definitions */
63
64struct tmpgrp {
65	const char *	name;
66	const char *	host;
67	const char *	user;
68	const char *	domain;
69	struct tmpgrp *	next;
70};
71
72struct pvt {
73	char *		nis_domain;
74	struct tmpgrp *	tmp;
75	struct tmpgrp *	cur;
76	char *		tmpgroup;
77};
78
79enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
80
81static /*const*/ char netgroup_map[]	= "netgroup";
82
83/* Forward */
84
85static void 		ng_close(struct irs_ng *);
86static int		ng_next(struct irs_ng *, const char **,
87				const char **, const char **);
88static int		ng_test(struct irs_ng *,
89 				const char *, const char *,
90				const char *, const char *);
91static void		ng_rewind(struct irs_ng *, const char *);
92static void		ng_minimize(struct irs_ng *);
93
94static void		add_group_to_list(struct pvt *, const char *, int);
95static void		add_tuple_to_list(struct pvt *, const char *, char *);
96static void		tmpfree(struct pvt *);
97
98/* Public */
99
100struct irs_ng *
101irs_nis_ng(struct irs_acc *this) {
102	struct irs_ng *ng;
103	struct pvt *pvt;
104
105	if (!(ng = memget(sizeof *ng))) {
106		errno = ENOMEM;
107		return (NULL);
108	}
109	memset(ng, 0x5e, sizeof *ng);
110	if (!(pvt = memget(sizeof *pvt))) {
111		memput(ng, sizeof *ng);
112		errno = ENOMEM;
113		return (NULL);
114	}
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);
124}
125
126/* Methods */
127
128static void
129ng_close(struct irs_ng *this) {
130	struct pvt *pvt = (struct pvt *)this->private;
131
132	tmpfree(pvt);
133	memput(pvt, sizeof *pvt);
134	memput(this, sizeof *this);
135}
136
137static int
138ng_next(struct irs_ng *this, const char **host, const char **user, const char **domain) {
139	struct pvt *pvt = (struct pvt *)this->private;
140
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);
148}
149
150static int
151ng_test(struct irs_ng *this, const char *name,
152	const char *host, const char *user, const char *domain)
153{
154	struct pvt *pvt = (struct pvt *)this->private;
155	struct tmpgrp *cur;
156
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;
164	}
165	tmpfree(pvt);
166	return ((cur == NULL) ? 0 : 1);
167}
168
169static void
170ng_rewind(struct irs_ng *this, const char *name) {
171	struct pvt *pvt = (struct pvt *)this->private;
172
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);
178	}
179	pvt->tmpgroup = strdup(name);
180	add_group_to_list(pvt, name, strlen(name));
181 reset:
182	pvt->cur = pvt->tmp;
183}
184
185static void
186ng_minimize(struct irs_ng *this) {
187	UNUSED(this);
188	/* NOOP */
189}
190
191/* Private */
192
193static void
194add_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;
199
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;
204
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));
220		}
221		free(vdata);
222	}
223}
224
225static void
226add_tuple_to_list(struct pvt *pvt, const char *name, char *cp) {
227	struct tmpgrp *tmp;
228	char *tp, *np;
229
230	INSIST(*cp++ == '(');
231
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;
238
239	/* Name */
240	strcpy(tp, name);
241	tmp->name = tp;
242	tp += strlen(tp) + 1;
243
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;
252
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;
261
262	/* Domain */
263	if (!(np = strchr(cp, ')')))
264		goto cleanup;
265	*np++ = '\0';
266	strcpy(tp, cp);
267	tmp->domain = tp;
268
269	/*
270	 * Empty string in file means wildcard, but
271	 * NULL string in return value means wildcard.
272	 */
273	if (!*tmp->host)
274		tmp->host = NULL;
275	if (!*tmp->user)
276		tmp->user = NULL;
277	if (!*tmp->domain)
278		tmp->domain = NULL;
279
280	/* Add to list (LIFO). */
281	tmp->next = pvt->tmp;
282	pvt->tmp = tmp;
283	return;
284
285 cleanup:
286	free(tmp);
287}
288
289static void
290tmpfree(struct pvt *pvt) {
291	struct tmpgrp *cur, *next;
292
293	if (pvt->tmpgroup) {
294		free(pvt->tmpgroup);
295		pvt->tmpgroup = NULL;
296	}
297	for (cur = pvt->tmp; cur; cur = next) {
298		next = cur->next;
299		free(cur);
300	}
301	pvt->tmp = NULL;
302}
303
304#endif /*WANT_IRS_NIS*/
305
306/*! \file */
307