1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
37  *
38  * Permission to use, copy, modify, and distribute this software for any
39  * purpose with or without fee is hereby granted, provided that the above
40  * copyright notice and this permission notice appear in all copies.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49  */
50 
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: nis_gr.c,v 1.4 2005/04/27 04:56:32 sra Exp $";
53 /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
54 /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $	*/
55 #endif /* LIBC_SCCS and not lint */
56 
57 /* Imports */
58 
59 #include "port_before.h"
60 
61 #if !defined(WANT_IRS_GR) || !defined(WANT_IRS_NIS)
62 static int __bind_irs_gr_unneeded;
63 #else
64 
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
69 #include <resolv.h>
70 #include <isc/memcluster.h>
71 
72 #include <rpc/rpc.h>
73 #include <rpc/xdr.h>
74 #include <rpcsvc/yp_prot.h>
75 #include <rpcsvc/ypclnt.h>
76 
77 #include <errno.h>
78 #include <grp.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 
84 #include <isc/memcluster.h>
85 
86 #include <irs.h>
87 
88 #include "port_after.h"
89 
90 #include "irs_p.h"
91 #include "nis_p.h"
92 
93 /* Definitions */
94 
95 struct pvt {
96 	int		needrewind;
97 	char *		nis_domain;
98 	char *		curkey_data;
99 	int		curkey_len;
100 	char *		curval_data;
101 	int		curval_len;
102 	/*%<
103 	 * Need space to store the entries read from the group file.
104 	 * The members list also needs space per member, and the
105 	 * strings making up the user names must be allocated
106 	 * somewhere.  Rather than doing lots of small allocations,
107 	 * we keep one buffer and resize it as needed.
108 	 */
109 	struct group	group;
110 	size_t		nmemb;		/*%< Malloc'd max index of gr_mem[]. */
111 	char *		membuf;
112 	size_t		membufsize;
113 };
114 
115 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
116 
117 static /*const*/ char group_bygid[] =	"group.bygid";
118 static /*const*/ char group_byname[] =	"group.byname";
119 
120 /* Forward */
121 
122 static void		gr_close(struct irs_gr *);
123 static struct group *	gr_next(struct irs_gr *);
124 static struct group *	gr_byname(struct irs_gr *, const char *);
125 static struct group *	gr_bygid(struct irs_gr *, gid_t);
126 static void		gr_rewind(struct irs_gr *);
127 static void		gr_minimize(struct irs_gr *);
128 
129 static struct group *	makegroupent(struct irs_gr *);
130 static void		nisfree(struct pvt *, enum do_what);
131 
132 /* Public */
133 
134 struct irs_gr *
irs_nis_gr(struct irs_acc * this)135 irs_nis_gr(struct irs_acc *this) {
136 	struct irs_gr *gr;
137 	struct pvt *pvt;
138 
139 	if (!(gr = memget(sizeof *gr))) {
140 		errno = ENOMEM;
141 		return (NULL);
142 	}
143 	memset(gr, 0x5e, sizeof *gr);
144 	if (!(pvt = memget(sizeof *pvt))) {
145 		memput(gr, sizeof *gr);
146 		errno = ENOMEM;
147 		return (NULL);
148 	}
149 	memset(pvt, 0, sizeof *pvt);
150 	pvt->needrewind = 1;
151 	pvt->nis_domain = ((struct nis_p *)this->private)->domain;
152 	gr->private = pvt;
153 	gr->close = gr_close;
154 	gr->next = gr_next;
155 	gr->byname = gr_byname;
156 	gr->bygid = gr_bygid;
157 	gr->rewind = gr_rewind;
158 	gr->list = make_group_list;
159 	gr->minimize = gr_minimize;
160 	gr->res_get = NULL;
161 	gr->res_set = NULL;
162 	return (gr);
163 }
164 
165 /* Methods */
166 
167 static void
gr_close(struct irs_gr * this)168 gr_close(struct irs_gr *this) {
169 	struct pvt *pvt = (struct pvt *)this->private;
170 
171 	if (pvt->group.gr_mem)
172 		free(pvt->group.gr_mem);
173 	if (pvt->membuf)
174 		free(pvt->membuf);
175 	memput(pvt, sizeof *pvt);
176 	memput(this, sizeof *this);
177 }
178 
179 static struct group *
gr_next(struct irs_gr * this)180 gr_next(struct irs_gr *this) {
181 	struct pvt *pvt = (struct pvt *)this->private;
182 	struct group *rval;
183 	int r;
184 
185 	do {
186 		if (pvt->needrewind) {
187 			nisfree(pvt, do_all);
188 			r = yp_first(pvt->nis_domain, group_byname,
189 				     &pvt->curkey_data, &pvt->curkey_len,
190 				     &pvt->curval_data, &pvt->curval_len);
191 			pvt->needrewind = 0;
192 		} else {
193 			char *newkey_data;
194 			int newkey_len;
195 
196 			nisfree(pvt, do_val);
197 			r = yp_next(pvt->nis_domain, group_byname,
198 				    pvt->curkey_data, pvt->curkey_len,
199 				    &newkey_data, &newkey_len,
200 				    &pvt->curval_data, &pvt->curval_len);
201 			nisfree(pvt, do_key);
202 			pvt->curkey_data = newkey_data;
203 			pvt->curkey_len = newkey_len;
204 		}
205 		if (r != 0) {
206 			errno = ENOENT;
207 			return (NULL);
208 		}
209 		rval = makegroupent(this);
210 	} while (rval == NULL);
211 	return (rval);
212 }
213 
214 static struct group *
gr_byname(struct irs_gr * this,const char * name)215 gr_byname(struct irs_gr *this, const char *name) {
216 	struct pvt *pvt = (struct pvt *)this->private;
217 	int r;
218 
219 	nisfree(pvt, do_val);
220 	r = yp_match(pvt->nis_domain, group_byname, name, strlen(name),
221 		     &pvt->curval_data, &pvt->curval_len);
222 	if (r != 0) {
223 		errno = ENOENT;
224 		return (NULL);
225 	}
226 	return (makegroupent(this));
227 }
228 
229 static struct group *
gr_bygid(struct irs_gr * this,gid_t gid)230 gr_bygid(struct irs_gr *this, gid_t gid) {
231 	struct pvt *pvt = (struct pvt *)this->private;
232 	char tmp[sizeof "4294967295"];
233 	int r;
234 
235 	nisfree(pvt, do_val);
236 	(void) sprintf(tmp, "%u", (unsigned int)gid);
237 	r = yp_match(pvt->nis_domain, group_bygid, tmp, strlen(tmp),
238 		     &pvt->curval_data, &pvt->curval_len);
239 	if (r != 0) {
240 		errno = ENOENT;
241 		return (NULL);
242 	}
243 	return (makegroupent(this));
244 }
245 
246 static void
gr_rewind(struct irs_gr * this)247 gr_rewind(struct irs_gr *this) {
248 	struct pvt *pvt = (struct pvt *)this->private;
249 
250 	pvt->needrewind = 1;
251 }
252 
253 static void
gr_minimize(struct irs_gr * this)254 gr_minimize(struct irs_gr *this) {
255 	UNUSED(this);
256 	/* NOOP */
257 }
258 
259 /* Private */
260 
261 static struct group *
makegroupent(struct irs_gr * this)262 makegroupent(struct irs_gr *this) {
263 	struct pvt *pvt = (struct pvt *)this->private;
264 	unsigned int num_members = 0;
265 	char *cp, **new;
266 	u_long t;
267 
268 	if (pvt->group.gr_mem) {
269 		free(pvt->group.gr_mem);
270 		pvt->group.gr_mem = NULL;
271 		pvt->nmemb = 0;
272 	}
273 	if (pvt->membuf)
274 		free(pvt->membuf);
275 	pvt->membuf = pvt->curval_data;
276 	pvt->curval_data = NULL;
277 
278 	cp = pvt->membuf;
279 	pvt->group.gr_name = cp;
280 	if (!(cp = strchr(cp, ':')))
281 		goto cleanup;
282 	*cp++ = '\0';
283 
284 	pvt->group.gr_passwd = cp;
285 	if (!(cp = strchr(cp, ':')))
286 		goto cleanup;
287 	*cp++ = '\0';
288 
289 	errno = 0;
290 	t = strtoul(cp, NULL, 10);
291 	if (errno == ERANGE)
292 		goto cleanup;
293 	pvt->group.gr_gid = (gid_t) t;
294 	if (!(cp = strchr(cp, ':')))
295 		goto cleanup;
296 	cp++;
297 
298         if (*cp && cp[strlen(cp)-1] == '\n')
299           cp[strlen(cp)-1] = '\0';
300 
301 	/*
302 	 * Parse the members out.
303 	 */
304 	while (*cp) {
305 		if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
306 			pvt->nmemb += 10;
307 			new = realloc(pvt->group.gr_mem,
308 				      pvt->nmemb * sizeof(char *));
309 			if (new == NULL)
310 				goto cleanup;
311 			pvt->group.gr_mem = new;
312 		}
313 		pvt->group.gr_mem[num_members++] = cp;
314 		if (!(cp = strchr(cp, ',')))
315 			break;
316 		*cp++ = '\0';
317 	}
318 	if (pvt->group.gr_mem == NULL) {
319 		pvt->group.gr_mem = malloc(sizeof(char*));
320 		if (!pvt->group.gr_mem)
321 			goto cleanup;
322 		pvt->nmemb = 1;
323 	}
324 	pvt->group.gr_mem[num_members] = NULL;
325 
326 	return (&pvt->group);
327 
328  cleanup:
329 	if (pvt->group.gr_mem) {
330 		free(pvt->group.gr_mem);
331 		pvt->group.gr_mem = NULL;
332 		pvt->nmemb = 0;
333 	}
334 	if (pvt->membuf) {
335 		free(pvt->membuf);
336 		pvt->membuf = NULL;
337 	}
338 	return (NULL);
339 }
340 
341 static void
nisfree(struct pvt * pvt,enum do_what do_what)342 nisfree(struct pvt *pvt, enum do_what do_what) {
343 	if ((do_what & do_key) && pvt->curkey_data) {
344 		free(pvt->curkey_data);
345 		pvt->curkey_data = NULL;
346 	}
347 	if ((do_what & do_val) && pvt->curval_data) {
348 		free(pvt->curval_data);
349 		pvt->curval_data = NULL;
350 	}
351 }
352 
353 #endif /* WANT_IRS_GR && WANT_IRS_NIS */
354 /*! \file */
355