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