xref: /freebsd/lib/libc/gen/getgrent.c (revision d74e86d9e30043893d6b308468008b65640ddcae)
1 /*
2  * Copyright (c) 1989, 1993
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 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)getgrent.c	8.2 (Berkeley) 3/21/94";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <sys/types.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <grp.h>
43 
44 static FILE *_gr_fp;
45 static struct group _gr_group;
46 static int _gr_stayopen;
47 static int grscan(), start_gr();
48 #ifdef YP
49 #include <rpc/rpc.h>
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
52 static int _gr_stepping_yp;
53 static int _gr_yp_enabled;
54 static int _getypgroup(struct group *, const char *, char *);
55 static int _nextypgroup(struct group *);
56 #endif
57 
58 /* initial size for malloc and increase steps for realloc */
59 #define	MAXGRP		64
60 #define	MAXLINELENGTH	256
61 
62 static char **members; 		/* list of group members */
63 static int maxgrp;              /* current length of **mebers */
64 static char *line;		/* temp buffer for group line */
65 static int maxlinelength;       /* current length of *line */
66 
67 /*
68  * Lines longer than MAXLINELENGTHLIMIT will be count as an error.
69  * <= 0 disable check for maximum line length
70  * 256K is enough for 64,000 uids
71  */
72 #define MAXLINELENGTHLIMIT	(256 * 1024)
73 #define GROUP_IGNORE_COMMENTS	1	/* allow comments in /etc/group */
74 
75 struct group *
76 getgrent()
77 {
78 	if (!_gr_fp && !start_gr()) {
79 		return NULL;
80 	}
81 
82 #ifdef YP
83 	if (_gr_stepping_yp) {
84 		if (_nextypgroup(&_gr_group))
85 			return(&_gr_group);
86 	}
87 tryagain:
88 #endif
89 
90 	if (!grscan(0, 0, NULL))
91 		return(NULL);
92 #ifdef YP
93 	if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) {
94 		_getypgroup(&_gr_group, &_gr_group.gr_name[1],
95 			    "group.byname");
96 	} else if(_gr_group.gr_name[0] == '+') {
97 		if (!_nextypgroup(&_gr_group))
98 			goto tryagain;
99 		else
100 			return(&_gr_group);
101 	}
102 #endif
103 	return(&_gr_group);
104 }
105 
106 struct group *
107 getgrnam(name)
108 	const char *name;
109 {
110 	int rval;
111 
112 	if (!start_gr())
113 		return(NULL);
114 #ifdef YP
115 	tryagain:
116 #endif
117 	rval = grscan(1, 0, name);
118 #ifdef YP
119 	if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled &&
120 					_gr_group.gr_name[0] == '+'))) {
121 		if (!(rval = _getypgroup(&_gr_group, name, "group.byname")))
122 			goto tryagain;
123 	}
124 #endif
125 	if (!_gr_stayopen)
126 		endgrent();
127 	return(rval ? &_gr_group : NULL);
128 }
129 
130 struct group *
131 #ifdef __STDC__
132 getgrgid(gid_t gid)
133 #else
134 getgrgid(gid)
135 	gid_t gid;
136 #endif
137 {
138 	int rval;
139 
140 	if (!start_gr())
141 		return(NULL);
142 #ifdef YP
143 	tryagain:
144 #endif
145 	rval = grscan(1, gid, NULL);
146 #ifdef YP
147 	if(rval == -1 && _gr_yp_enabled) {
148 		char buf[16];
149 		snprintf(buf, sizeof buf, "%d", (unsigned)gid);
150 		if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid")))
151 			goto tryagain;
152 	}
153 #endif
154 	if (!_gr_stayopen)
155 		endgrent();
156 	return(rval ? &_gr_group : NULL);
157 }
158 
159 static int
160 start_gr()
161 {
162 	if (_gr_fp) {
163 		rewind(_gr_fp);
164 		return(1);
165 	}
166 	_gr_fp = fopen(_PATH_GROUP, "r");
167 	if(!_gr_fp) return 0;
168 #ifdef YP
169 	/*
170 	 * This is a disgusting hack, used to determine when YP is enabled.
171 	 * This would be easier if we had a group database to go along with
172 	 * the password database.
173 	 */
174 	{
175 		char *line;
176 		size_t linelen;
177 		_gr_yp_enabled = 0;
178 		while((line = fgetln(_gr_fp, &linelen)) != NULL) {
179 			if(line[0] == '+') {
180 				if(line[1] && line[1] != ':' && !_gr_yp_enabled) {
181 					_gr_yp_enabled = 1;
182 				} else {
183 					_gr_yp_enabled = -1;
184 					break;
185 				}
186 			}
187 		}
188 		rewind(_gr_fp);
189 	}
190 #endif
191 
192 	if (maxlinelength == 0) {
193 		if ((line = (char *)malloc(sizeof(char) *
194 					   MAXLINELENGTH)) == NULL)
195 			return(0);
196 		maxlinelength += MAXLINELENGTH;
197 	}
198 
199 	if (maxgrp == 0) {
200 		if ((members = (char **)malloc(sizeof(char **) *
201 					       MAXGRP)) == NULL)
202 			return(0);
203 		maxgrp += MAXGRP;
204 	}
205 
206 	return 1;
207 }
208 
209 int
210 setgrent()
211 {
212 	return(setgroupent(0));
213 }
214 
215 int
216 setgroupent(stayopen)
217 	int stayopen;
218 {
219 	if (!start_gr())
220 		return(0);
221 	_gr_stayopen = stayopen;
222 #ifdef YP
223 	_gr_stepping_yp = 0;
224 #endif
225 	return(1);
226 }
227 
228 void
229 endgrent()
230 {
231 #ifdef YP
232 	_gr_stepping_yp = 0;
233 #endif
234 	if (_gr_fp) {
235 		(void)fclose(_gr_fp);
236 		_gr_fp = NULL;
237 	}
238 }
239 
240 static int
241 grscan(search, gid, name)
242 	register int search, gid;
243 	register char *name;
244 {
245 	register char *cp, **m;
246 	char *bp;
247 
248 
249 #ifdef YP
250 	int _ypfound;
251 #endif
252 	for (;;) {
253 #ifdef YP
254 		_ypfound = 0;
255 #endif
256 		if (fgets(line, maxlinelength, _gr_fp) == NULL)
257 			return(0);
258 
259 		if (!index(line, '\n')) {
260 			do {
261 				if (feof(_gr_fp))
262 					return(0);
263 
264 				/* don't allocate infinite memory */
265 				if (MAXLINELENGTHLIMIT > 0 &&
266 				    maxlinelength >= MAXLINELENGTHLIMIT)
267 					return(0);
268 
269 				if ((line = (char *)reallocf(line,
270 				     sizeof(char) *
271 				     (maxlinelength + MAXLINELENGTH))) == NULL)
272 					return(0);
273 
274 				if (fgets(line + maxlinelength - 1,
275 					  MAXLINELENGTH + 1, _gr_fp) == NULL)
276 					return(0);
277 
278 				maxlinelength += MAXLINELENGTH;
279 			} while (!index(line + maxlinelength -
280 				       MAXLINELENGTH - 1, '\n'));
281 		}
282 
283 #ifdef GROUP_IGNORE_COMMENTS
284 		/*
285 		 * Ignore comments: ^[ \t]*#
286 		 */
287 		for (cp = line; *cp != '\0'; cp++)
288 			if (*cp != ' ' && *cp != '\t')
289 				break;
290 		if (*cp == '#' || *cp == '\0')
291 			continue;
292 #endif
293 
294 		bp = line;
295 
296 		if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
297 			break;
298 #ifdef YP
299 		/*
300 		 * XXX   We need to be careful to avoid proceeding
301 		 * past this point under certain circumstances or
302 		 * we risk dereferencing null pointers down below.
303 		 */
304 		if (_gr_group.gr_name[0] == '+') {
305 			if (strlen(_gr_group.gr_name) == 1) {
306 				switch(search) {
307 				case 0:
308 					return(1);
309 				case 1:
310 					return(-1);
311 				default:
312 					return(0);
313 				}
314 			} else {
315 				cp = &_gr_group.gr_name[1];
316 				if (search && name != NULL)
317 					if (strcmp(cp, name))
318 						continue;
319 				if (!_getypgroup(&_gr_group, cp,
320 						"group.byname"))
321 					continue;
322 				if (search && name == NULL)
323 					if (gid != _gr_group.gr_gid)
324 						continue;
325 			/* We're going to override -- tell the world. */
326 				_ypfound++;
327 			}
328 		}
329 #else
330 		if (_gr_group.gr_name[0] == '+')
331 			continue;
332 #endif /* YP */
333 		if (search && name) {
334 			if(strcmp(_gr_group.gr_name, name)) {
335 				continue;
336 			}
337 		}
338 #ifdef YP
339 		if ((cp = strsep(&bp, ":\n")) == NULL)
340 			if (_ypfound)
341 				return(1);
342 			else
343 				break;
344 		if (strlen(cp) || !_ypfound)
345 			_gr_group.gr_passwd = cp;
346 #else
347 		if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
348 			break;
349 #endif
350 		if (!(cp = strsep(&bp, ":\n")))
351 #ifdef YP
352 			if (_ypfound)
353 				return(1);
354 			else
355 #endif
356 				continue;
357 #ifdef YP
358 		/*
359 		 * Hurm. Should we be doing this? We allow UIDs to
360 		 * be overridden -- what about GIDs?
361 		 */
362 		if (!_ypfound)
363 #endif
364 		_gr_group.gr_gid = atoi(cp);
365 		if (search && name == NULL && _gr_group.gr_gid != gid)
366 			continue;
367 		cp = NULL;
368 		if (bp == NULL) /* !!! Must check for this! */
369 			break;
370 #ifdef YP
371 		if ((cp = strsep(&bp, ":\n")) == NULL)
372 			break;
373 
374 		if (!strlen(cp) && _ypfound)
375 			return(1);
376 		else
377 			members[0] = NULL;
378 		bp = cp;
379 		cp = NULL;
380 #endif
381 		for (m = members; ; bp++) {
382 			if (m == (members + maxgrp - 1)) {
383 				if ((members = (char **)
384 				     reallocf(members,
385 					     sizeof(char **) *
386 					     (maxgrp + MAXGRP))) == NULL)
387 					return(0);
388 				m = members + maxgrp - 1;
389 				maxgrp += MAXGRP;
390 			}
391 			if (*bp == ',') {
392 				if (cp) {
393 					*bp = '\0';
394 					*m++ = cp;
395 					cp = NULL;
396 				}
397 			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
398 				if (cp) {
399 					*bp = '\0';
400 					*m++ = cp;
401 				}
402 				break;
403 			} else if (cp == NULL)
404 				cp = bp;
405 
406 		}
407 		_gr_group.gr_mem = members;
408 		*m = NULL;
409 		return(1);
410 	}
411 	/* NOTREACHED */
412 	return (0);
413 }
414 
415 #ifdef YP
416 
417 static int
418 _gr_breakout_yp(struct group *gr, char *result)
419 {
420 	char *s, *cp;
421 	char **m;
422 
423 	/*
424 	 * XXX If 's' ends up being a NULL pointer, punt on this group.
425 	 * It means the NIS group entry is badly formatted and should
426 	 * be skipped.
427 	 */
428 	if ((s = strsep(&result, ":")) == NULL) return 0; /* name */
429 	gr->gr_name = s;
430 
431 	if ((s = strsep(&result, ":")) == NULL) return 0; /* password */
432 	gr->gr_passwd = s;
433 
434 	if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */
435 	gr->gr_gid = atoi(s);
436 
437 	if ((s = result) == NULL) return 0;
438 	cp = 0;
439 
440 	for (m = members; ; s++) {
441 		if (m == members + maxgrp - 1) {
442 			if ((members = (char **)reallocf(members,
443 			     sizeof(char **) * (maxgrp + MAXGRP))) == NULL)
444 				return(0);
445 			m = members + maxgrp - 1;
446 			maxgrp += MAXGRP;
447 		}
448 		if (*s == ',') {
449 			if (cp) {
450 				*s = '\0';
451 				*m++ = cp;
452 				cp = NULL;
453 			}
454 		} else if (*s == '\0' || *s == '\n' || *s == ' ') {
455 			if (cp) {
456 				*s = '\0';
457 				*m++ = cp;
458 			}
459 			break;
460 		} else if (cp == NULL) {
461 			cp = s;
462 		}
463 	}
464 	_gr_group.gr_mem = members;
465 	*m = NULL;
466 
467 	return 1;
468 }
469 
470 static char *_gr_yp_domain;
471 
472 static int
473 _getypgroup(struct group *gr, const char *name, char *map)
474 {
475 	char *result, *s;
476 	static char resultbuf[YPMAXRECORD + 2];
477 	int resultlen;
478 
479 	if(!_gr_yp_domain) {
480 		if(yp_get_default_domain(&_gr_yp_domain))
481 		  return 0;
482 	}
483 
484 	if(yp_match(_gr_yp_domain, map, name, strlen(name),
485 		    &result, &resultlen))
486 		return 0;
487 
488 	s = strchr(result, '\n');
489 	if(s) *s = '\0';
490 
491 	if(resultlen >= sizeof resultbuf) return 0;
492 	strncpy(resultbuf, result, resultlen);
493 	resultbuf[resultlen] = '\0';
494 	free(result);
495 	return(_gr_breakout_yp(gr, resultbuf));
496 
497 }
498 
499 
500 static int
501 _nextypgroup(struct group *gr)
502 {
503 	static char *key;
504 	static int keylen;
505 	char *lastkey, *result;
506 	static char resultbuf[YPMAXRECORD + 2];
507 	int resultlen;
508 	int rv;
509 
510 	if(!_gr_yp_domain) {
511 		if(yp_get_default_domain(&_gr_yp_domain))
512 		  return 0;
513 	}
514 
515 	if(!_gr_stepping_yp) {
516 		if(key) free(key);
517 		rv = yp_first(_gr_yp_domain, "group.byname",
518 			      &key, &keylen, &result, &resultlen);
519 		if(rv) {
520 			return 0;
521 		}
522 		_gr_stepping_yp = 1;
523 		goto unpack;
524 	} else {
525 tryagain:
526 		lastkey = key;
527 		rv = yp_next(_gr_yp_domain, "group.byname", key, keylen,
528 			     &key, &keylen, &result, &resultlen);
529 		free(lastkey);
530 unpack:
531 		if(rv) {
532 			_gr_stepping_yp = 0;
533 			return 0;
534 		}
535 
536 		if(resultlen > sizeof(resultbuf)) {
537 			free(result);
538 			goto tryagain;
539 		}
540 
541 		strncpy(resultbuf, result, resultlen);
542 		resultbuf[resultlen] = '\0';
543 		free(result);
544 		if((result = strchr(resultbuf, '\n')) != NULL)
545 			*result = '\0';
546 		if (_gr_breakout_yp(gr, resultbuf))
547 			return(1);
548 		else
549 			goto tryagain;
550 	}
551 }
552 
553 #endif /* YP */
554