Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.28
      1 /*	$NetBSD: getgrent.c,v 1.28 1999/01/16 14:44:33 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 #if 0
     40 static char sccsid[] = "@(#)getgrent.c	8.2 (Berkeley) 3/21/94";
     41 #else
     42 __RCSID("$NetBSD: getgrent.c,v 1.28 1999/01/16 14:44:33 lukem Exp $");
     43 #endif
     44 #endif /* LIBC_SCCS and not lint */
     45 
     46 #include "namespace.h"
     47 #include <sys/types.h>
     48 #include <grp.h>
     49 #include <limits.h>
     50 #include <nsswitch.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <syslog.h>
     55 #ifdef HESIOD
     56 #include <hesiod.h>
     57 #endif
     58 #ifdef YP
     59 #include <rpc/rpc.h>
     60 #include <rpcsvc/yp_prot.h>
     61 #include <rpcsvc/ypclnt.h>
     62 #endif
     63 
     64 #ifdef __weak_alias
     65 __weak_alias(endgrent,_endgrent);
     66 __weak_alias(getgrent,_getgrent);
     67 __weak_alias(getgrgid,_getgrgid);
     68 __weak_alias(getgrnam,_getgrnam);
     69 __weak_alias(setgrent,_setgrent);
     70 __weak_alias(setgroupent,_setgroupent);
     71 #endif
     72 
     73 static FILE		*_gr_fp;
     74 static struct group	_gr_group;
     75 static int		_gr_stayopen;
     76 static int		_gr_nomore;
     77 
     78 static int grscan	__P((int, gid_t, const char *));
     79 static int matchline	__P((int, gid_t, const char *));
     80 static int start_gr	__P((void));
     81 
     82 #define	MAXGRP		200
     83 #define	MAXLINELENGTH	1024
     84 
     85 static __aconst char	*members[MAXGRP];
     86 static char		line[MAXLINELENGTH];
     87 
     88 #ifdef YP
     89 enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
     90 static enum _grmode	 __grmode;
     91 static char		*__ypcurrent, *__ypdomain;
     92 static int		 __ypcurrentlen;
     93 #endif
     94 
     95 #ifdef HESIOD
     96 static int	__gr_hesnum;
     97 #endif
     98 
     99 struct group *
    100 getgrent()
    101 {
    102 	_gr_nomore = 0;
    103 	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL) || _gr_nomore)
    104  		return(NULL);
    105 	return &_gr_group;
    106 }
    107 
    108 struct group *
    109 getgrnam(name)
    110 	const char *name;
    111 {
    112 	int rval;
    113 
    114 	if (!start_gr())
    115 		return NULL;
    116 	rval = grscan(1, 0, name);
    117 	if (!_gr_stayopen)
    118 		endgrent();
    119 	return (rval) ? &_gr_group : NULL;
    120 }
    121 
    122 struct group *
    123 getgrgid(gid)
    124 	gid_t gid;
    125 {
    126 	int rval;
    127 
    128 	if (!start_gr())
    129 		return NULL;
    130 	rval = grscan(1, gid, NULL);
    131 	if (!_gr_stayopen)
    132 		endgrent();
    133 	return (rval) ? &_gr_group : NULL;
    134 }
    135 
    136 static int
    137 start_gr()
    138 {
    139 #ifdef YP
    140 	__grmode = GRMODE_NONE;
    141 	if (__ypcurrent)
    142 		free(__ypcurrent);
    143 	__ypcurrent = NULL;
    144 #endif
    145 #ifdef HESIOD
    146 	__gr_hesnum = 0;
    147 #endif
    148 	if (_gr_fp) {
    149 		rewind(_gr_fp);
    150 		return 1;
    151 	}
    152 	return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
    153 }
    154 
    155 void
    156 setgrent()
    157 {
    158 	(void) setgroupent(0);
    159 }
    160 
    161 int
    162 setgroupent(stayopen)
    163 	int stayopen;
    164 {
    165 	if (!start_gr())
    166 		return 0;
    167 	_gr_stayopen = stayopen;
    168 	return 1;
    169 }
    170 
    171 void
    172 endgrent()
    173 {
    174 #ifdef YP
    175 	__grmode = GRMODE_NONE;
    176 	if (__ypcurrent)
    177 		free(__ypcurrent);
    178 	__ypcurrent = NULL;
    179 #endif
    180 #ifdef HESIOD
    181 	__gr_hesnum = 0;
    182 #endif
    183 	if (_gr_fp) {
    184 		(void)fclose(_gr_fp);
    185 		_gr_fp = NULL;
    186 	}
    187 }
    188 
    189 
    190 static int _local_grscan __P((void *, void *, va_list));
    191 
    192 static int
    193 _local_grscan(rv, cb_data, ap)
    194 	void	*rv;
    195 	void	*cb_data;
    196 	va_list	 ap;
    197 {
    198 	int		 search = va_arg(ap, int);
    199 	gid_t		 gid = va_arg(ap, gid_t);
    200 	const char	*name = va_arg(ap, const char *);
    201 
    202 	for (;;) {
    203 		if (!fgets(line, sizeof(line), _gr_fp)) {
    204 			if (!search) {
    205 				_gr_nomore = 1;
    206 				return NS_SUCCESS;
    207 			}
    208 			return NS_NOTFOUND;
    209 		}
    210 		/* skip lines that are too big */
    211 		if (!strchr(line, '\n')) {
    212 			int ch;
    213 
    214 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    215 				;
    216 			continue;
    217 		}
    218 		if (matchline(search, gid, name))
    219 			return NS_SUCCESS;
    220 	}
    221 	/* NOTREACHED */
    222 }
    223 
    224 #ifdef HESIOD
    225 static int _dns_grscan __P((void *, void *, va_list));
    226 
    227 static int
    228 _dns_grscan(rv, cb_data, ap)
    229 	void	*rv;
    230 	void	*cb_data;
    231 	va_list	 ap;
    232 {
    233 	int		 search = va_arg(ap, int);
    234 	gid_t		 gid = va_arg(ap, gid_t);
    235 	const char	*name = va_arg(ap, const char *);
    236 
    237 	char		**hp;
    238 
    239 	for (;;) {
    240 		if (search) {
    241 			if (name)
    242 				strncpy(line, name, sizeof(line));
    243 			else
    244 				snprintf(line, sizeof(line), "%u",
    245 				    (unsigned int)gid);
    246 		} else {
    247 			snprintf(line, sizeof(line), "group-%u", __gr_hesnum);
    248 			__gr_hesnum++;
    249 		}
    250 
    251 		line[sizeof(line) - 1] = '\0';
    252 		hp = hes_resolve(line, "group");
    253 		if (hp == NULL) {
    254 			switch (hes_error()) {
    255 			case HES_ER_NOTFOUND:
    256 				if (!search) {
    257 					__gr_hesnum = 0;
    258 					_gr_nomore = 1;
    259 					return NS_SUCCESS;
    260 				}
    261 				return NS_NOTFOUND;
    262 			case HES_ER_OK:
    263 				abort();
    264 			default:
    265 				return NS_UNAVAIL;
    266 			}
    267 		}
    268 
    269 						/* only check first elem */
    270 		strncpy(line, hp[0], sizeof(line));
    271 		line[sizeof(line) - 1] = '\0';
    272 		hes_free(hp);
    273 		if (matchline(search, gid, name))
    274 			return NS_SUCCESS;
    275 		else if (search)
    276 			return NS_NOTFOUND;
    277 	}
    278 }
    279 #endif
    280 
    281 #ifdef YP
    282 static int _nis_grscan __P((void *, void *, va_list));
    283 
    284 static int
    285 _nis_grscan(rv, cb_data, ap)
    286 	void	*rv;
    287 	void	*cb_data;
    288 	va_list	 ap;
    289 {
    290 	int		 search = va_arg(ap, int);
    291 	gid_t		 gid = va_arg(ap, gid_t);
    292 	const char	*name = va_arg(ap, const char *);
    293 
    294 	char	*key, *data;
    295 	int	 keylen, datalen;
    296 	int	 r;
    297 
    298 	if(__ypdomain == NULL) {
    299 		switch (yp_get_default_domain(&__ypdomain)) {
    300 		case 0:
    301 			break;
    302 		case YPERR_RESRC:
    303 			return NS_TRYAGAIN;
    304 		default:
    305 			return NS_UNAVAIL;
    306 		}
    307 	}
    308 
    309 	if (search) {			/* specific group or gid */
    310 		if (name)
    311 			strncpy(line, name, sizeof(line));
    312 		else
    313 			snprintf(line, sizeof(line), "%u", (unsigned int)gid);
    314 		line[sizeof(line) - 1] = '\0';
    315 		data = NULL;
    316 		r = yp_match(__ypdomain,
    317 				(name) ? "group.byname" : "group.bygid",
    318 				line, (int)strlen(line), &data, &datalen);
    319 		switch (r) {
    320 		case 0:
    321 			break;
    322 		case YPERR_KEY:
    323 			if (data)
    324 				free(data);
    325 			return NS_NOTFOUND;
    326 		default:
    327 			if (data)
    328 				free(data);
    329 			return NS_UNAVAIL;
    330 		}
    331 		data[datalen] = '\0';			/* clear trailing \n */
    332 		strncpy(line, data, sizeof(line));
    333 		line[sizeof(line) - 1] = '\0';
    334 		free(data);
    335 		if (matchline(search, gid, name))
    336 			return NS_SUCCESS;
    337 		else
    338 			return NS_NOTFOUND;
    339 	}
    340 
    341 	for (;;) {			/* ! search */
    342 		data = NULL;
    343 		if(__ypcurrent) {
    344 			key = NULL;
    345 			r = yp_next(__ypdomain, "group.byname",
    346 				__ypcurrent, __ypcurrentlen,
    347 				&key, &keylen, &data, &datalen);
    348 			free(__ypcurrent);
    349 			switch (r) {
    350 			case 0:
    351 				break;
    352 			case YPERR_NOMORE:
    353 				__ypcurrent = NULL;
    354 				if (key)
    355 					free(key);
    356 				if (data)
    357 					free(data);
    358 				_gr_nomore = 1;
    359 				return NS_SUCCESS;
    360 			default:
    361 				if (key)
    362 					free(key);
    363 				if (data)
    364 					free(data);
    365 				return NS_UNAVAIL;
    366 			}
    367 			__ypcurrent = key;
    368 			__ypcurrentlen = keylen;
    369 		} else {
    370 			if (yp_first(__ypdomain, "group.byname",
    371 					&__ypcurrent, &__ypcurrentlen,
    372 					&data, &datalen)) {
    373 				if (data);
    374 					free(data);
    375 				return NS_UNAVAIL;
    376 			}
    377 		}
    378 		data[datalen] = '\0';			/* clear trailing \n */
    379 		strncpy(line, data, sizeof(line));
    380 		line[sizeof(line) - 1] = '\0';
    381 		free(data);
    382 		if (matchline(search, gid, name))
    383 			return NS_SUCCESS;
    384 	}
    385 	/* NOTREACHED */
    386 }
    387 #endif
    388 
    389 #if defined(YP) || defined(HESIOD)
    390 /*
    391  * log an error if "files" or "compat" is specified in group_compat database
    392  */
    393 static int _bad_grscan __P((void *, void *, va_list));
    394 
    395 static int
    396 _bad_grscan(rv, cb_data, ap)
    397 	void	*rv;
    398 	void	*cb_data;
    399 	va_list	 ap;
    400 {
    401 	static int warned;
    402 
    403 	if (!warned) {
    404 		syslog(LOG_ERR,
    405 			"nsswitch.conf group_compat database can't use '%s'",
    406 			(char *)cb_data);
    407 	}
    408 	warned = 1;
    409 	return NS_UNAVAIL;
    410 }
    411 
    412 /*
    413  * when a name lookup in compat mode is required, look it up in group_compat
    414  * nsswitch database. only Hesiod and NIS is supported - it doesn't make
    415  * sense to lookup compat names from 'files' or 'compat'
    416  */
    417 
    418 static int __grscancompat __P((int, gid_t, const char *));
    419 
    420 static int
    421 __grscancompat(search, gid, name)
    422 	int		 search;
    423 	gid_t		 gid;
    424 	const char	*name;
    425 {
    426 	static ns_dtab	dtab[] = {
    427 		NS_FILES_CB(_bad_grscan, "files"),
    428 		NS_DNS_CB(_dns_grscan, NULL),
    429 		NS_NIS_CB(_nis_grscan, NULL),
    430 		NS_COMPAT_CB(_bad_grscan, "compat"),
    431 		{ 0 }
    432 	};
    433 
    434 	return nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, search, gid, name);
    435 }
    436 
    437 
    438 static int _compat_grscan __P((void *, void *, va_list));
    439 
    440 static int
    441 _compat_grscan(rv, cb_data, ap)
    442 	void	*rv;
    443 	void	*cb_data;
    444 	va_list	 ap;
    445 {
    446 	int		 search = va_arg(ap, int);
    447 	gid_t		 gid = va_arg(ap, gid_t);
    448 	const char	*name = va_arg(ap, const char *);
    449 
    450 	static char	*grname = NULL;
    451 
    452 	for (;;) {
    453 		if(__grmode != GRMODE_NONE) {
    454 			int	 r;
    455 
    456 			switch(__grmode) {
    457 			case GRMODE_FULL:
    458 				r = __grscancompat(search, gid, name);
    459 				if (r == NS_SUCCESS)
    460 					return r;
    461 				__grmode = GRMODE_NONE;
    462 				break;
    463 			case GRMODE_NAME:
    464 				if(grname == (char *)NULL) {
    465 					__grmode = GRMODE_NONE;
    466 					break;
    467 				}
    468 				r = __grscancompat(1, 0, grname);
    469 				free(grname);
    470 				grname = (char *)NULL;
    471 				if (r != NS_SUCCESS)
    472 					break;
    473 				if (!search)
    474 					return NS_SUCCESS;
    475 				if (name) {
    476 					if (! strcmp(_gr_group.gr_name, name))
    477 						return NS_SUCCESS;
    478 				} else {
    479 					if (_gr_group.gr_gid == gid)
    480 						return NS_SUCCESS;
    481 				}
    482 				break;
    483 			case GRMODE_NONE:
    484 				abort();
    485 			}
    486 			continue;
    487 		}
    488 
    489 		if (!fgets(line, sizeof(line), _gr_fp))
    490 			return NS_NOTFOUND;
    491 		/* skip lines that are too big */
    492 		if (!strchr(line, '\n')) {
    493 			int ch;
    494 
    495 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    496 				;
    497 			continue;
    498 		}
    499 
    500 		if (line[0] == '+') {
    501 			char	*tptr, *bp;
    502 
    503 			switch(line[1]) {
    504 			case ':':
    505 			case '\0':
    506 			case '\n':
    507 				__grmode = GRMODE_FULL;
    508 				break;
    509 			default:
    510 				__grmode = GRMODE_NAME;
    511 				bp = line;
    512 				tptr = strsep(&bp, ":\n");
    513 				grname = strdup(tptr + 1);
    514 				break;
    515 			}
    516 			continue;
    517 		}
    518 		if (matchline(search, gid, name))
    519 			return NS_SUCCESS;
    520 	}
    521 	/* NOTREACHED */
    522 }
    523 #endif /* YP || HESIOD */
    524 
    525 static int
    526 grscan(search, gid, name)
    527 	int		 search;
    528 	gid_t		 gid;
    529 	const char	*name;
    530 {
    531 	int		r;
    532 	static ns_dtab	dtab[] = {
    533 		NS_FILES_CB(_local_grscan, NULL),
    534 		NS_DNS_CB(_dns_grscan, NULL),
    535 		NS_NIS_CB(_nis_grscan, NULL),
    536 		NS_COMPAT_CB(_compat_grscan, NULL),
    537 		{ 0 }
    538 	};
    539 
    540 	r = nsdispatch(NULL, dtab, NSDB_GROUP, search, gid, name);
    541 	return (r == NS_SUCCESS) ? 1 : 0;
    542 }
    543 
    544 static int
    545 matchline(search, gid, name)
    546 	int		 search;
    547 	gid_t		 gid;
    548 	const char	*name;
    549 {
    550 	unsigned long	id;
    551 	__aconst char	**m;
    552 	char		*cp, *bp, *ep;
    553 
    554 	if (line[0] == '+')
    555 		return 0;	/* sanity check to prevent recursion */
    556 	bp = line;
    557 	_gr_group.gr_name = strsep(&bp, ":\n");
    558 	if (search && name && strcmp(_gr_group.gr_name, name))
    559 		return 0;
    560 	_gr_group.gr_passwd = strsep(&bp, ":\n");
    561 	if (!(cp = strsep(&bp, ":\n")))
    562 		return 0;
    563 	id = strtoul(cp, &ep, 10);
    564 	if (id > GID_MAX || *ep != '\0')
    565 		return 0;
    566 	_gr_group.gr_gid = (gid_t)id;
    567 	if (search && name == NULL && _gr_group.gr_gid != gid)
    568 		return 0;
    569 	cp = NULL;
    570 	if (bp == NULL)
    571 		return 0;
    572 	for (_gr_group.gr_mem = m = members;; bp++) {
    573 		if (m == &members[MAXGRP - 1])
    574 			break;
    575 		if (*bp == ',') {
    576 			if (cp) {
    577 				*bp = '\0';
    578 				*m++ = cp;
    579 				cp = NULL;
    580 			}
    581 		} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    582 			if (cp) {
    583 				*bp = '\0';
    584 				*m++ = cp;
    585 			}
    586 			break;
    587 		} else if (cp == NULL)
    588 			cp = bp;
    589 	}
    590 	*m = NULL;
    591 	return 1;
    592 }
    593