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