Home | History | Annotate | Line # | Download | only in gen
getgrent.c revision 1.34.2.1
      1  1.34.2.1     perry /*	$NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry 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.34.2.1     perry __RCSID("$NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry 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.33     lukem #include <errno.h>
     51      1.27     lukem #include <grp.h>
     52      1.18     lukem #include <limits.h>
     53      1.27     lukem #include <nsswitch.h>
     54       1.1       cgd #include <stdio.h>
     55       1.1       cgd #include <stdlib.h>
     56       1.2   deraadt #include <string.h>
     57      1.27     lukem #include <syslog.h>
     58      1.33     lukem 
     59      1.27     lukem #ifdef HESIOD
     60      1.27     lukem #include <hesiod.h>
     61      1.27     lukem #endif
     62       1.2   deraadt #ifdef YP
     63       1.2   deraadt #include <rpc/rpc.h>
     64       1.2   deraadt #include <rpcsvc/yp_prot.h>
     65       1.2   deraadt #include <rpcsvc/ypclnt.h>
     66      1.21       jtc #endif
     67      1.21       jtc 
     68      1.34     lukem #if defined(YP) || defined(HESIOD)
     69      1.34     lukem #define _GROUP_COMPAT
     70      1.34     lukem #endif
     71      1.34     lukem 
     72      1.21       jtc #ifdef __weak_alias
     73      1.21       jtc __weak_alias(endgrent,_endgrent);
     74      1.21       jtc __weak_alias(getgrent,_getgrent);
     75      1.21       jtc __weak_alias(getgrgid,_getgrgid);
     76      1.21       jtc __weak_alias(getgrnam,_getgrnam);
     77      1.21       jtc __weak_alias(setgrent,_setgrent);
     78      1.21       jtc __weak_alias(setgroupent,_setgroupent);
     79       1.2   deraadt #endif
     80       1.1       cgd 
     81      1.27     lukem static FILE		*_gr_fp;
     82      1.27     lukem static struct group	_gr_group;
     83      1.27     lukem static int		_gr_stayopen;
     84  1.34.2.1     perry static int		_gr_filesdone;
     85      1.27     lukem 
     86  1.34.2.1     perry static void grcleanup	__P((void));
     87      1.27     lukem static int grscan	__P((int, gid_t, const char *));
     88      1.27     lukem static int matchline	__P((int, gid_t, const char *));
     89      1.27     lukem static int start_gr	__P((void));
     90       1.1       cgd 
     91       1.1       cgd #define	MAXGRP		200
     92       1.1       cgd #define	MAXLINELENGTH	1024
     93      1.27     lukem 
     94      1.27     lukem static __aconst char	*members[MAXGRP];
     95      1.27     lukem static char		line[MAXLINELENGTH];
     96       1.1       cgd 
     97       1.2   deraadt #ifdef YP
     98      1.34     lukem static char	*__ypcurrent, *__ypdomain;
     99      1.34     lukem static int	 __ypcurrentlen;
    100  1.34.2.1     perry static int	 _gr_ypdone;
    101      1.27     lukem #endif
    102      1.27     lukem 
    103      1.27     lukem #ifdef HESIOD
    104  1.34.2.1     perry static int	_gr_hesnum;
    105       1.2   deraadt #endif
    106       1.2   deraadt 
    107      1.34     lukem #ifdef _GROUP_COMPAT
    108      1.34     lukem enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
    109      1.34     lukem static enum _grmode	 __grmode;
    110      1.34     lukem #endif
    111      1.34     lukem 
    112       1.1       cgd struct group *
    113       1.1       cgd getgrent()
    114       1.1       cgd {
    115  1.34.2.1     perry 	if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
    116  1.34.2.1     perry  		return (NULL);
    117      1.27     lukem 	return &_gr_group;
    118       1.1       cgd }
    119       1.1       cgd 
    120       1.1       cgd struct group *
    121       1.1       cgd getgrnam(name)
    122       1.1       cgd 	const char *name;
    123       1.1       cgd {
    124       1.1       cgd 	int rval;
    125       1.1       cgd 
    126       1.1       cgd 	if (!start_gr())
    127      1.27     lukem 		return NULL;
    128       1.1       cgd 	rval = grscan(1, 0, name);
    129       1.1       cgd 	if (!_gr_stayopen)
    130       1.1       cgd 		endgrent();
    131      1.27     lukem 	return (rval) ? &_gr_group : NULL;
    132       1.1       cgd }
    133       1.1       cgd 
    134       1.1       cgd struct group *
    135       1.1       cgd getgrgid(gid)
    136       1.1       cgd 	gid_t gid;
    137       1.1       cgd {
    138       1.1       cgd 	int rval;
    139       1.1       cgd 
    140       1.1       cgd 	if (!start_gr())
    141      1.27     lukem 		return NULL;
    142       1.1       cgd 	rval = grscan(1, gid, NULL);
    143       1.1       cgd 	if (!_gr_stayopen)
    144       1.1       cgd 		endgrent();
    145      1.27     lukem 	return (rval) ? &_gr_group : NULL;
    146       1.1       cgd }
    147       1.1       cgd 
    148  1.34.2.1     perry void
    149  1.34.2.1     perry grcleanup()
    150       1.1       cgd {
    151  1.34.2.1     perry 	_gr_filesdone = 0;
    152      1.27     lukem #ifdef YP
    153      1.27     lukem 	if (__ypcurrent)
    154      1.27     lukem 		free(__ypcurrent);
    155      1.27     lukem 	__ypcurrent = NULL;
    156  1.34.2.1     perry 	_gr_ypdone = 0;
    157      1.27     lukem #endif
    158      1.27     lukem #ifdef HESIOD
    159  1.34.2.1     perry 	_gr_hesnum = 0;
    160      1.27     lukem #endif
    161      1.34     lukem #ifdef _GROUP_COMPAT
    162      1.34     lukem 	__grmode = GRMODE_NONE;
    163      1.34     lukem #endif
    164  1.34.2.1     perry }
    165  1.34.2.1     perry 
    166  1.34.2.1     perry static int
    167  1.34.2.1     perry start_gr()
    168  1.34.2.1     perry {
    169  1.34.2.1     perry 	grcleanup();
    170       1.1       cgd 	if (_gr_fp) {
    171       1.1       cgd 		rewind(_gr_fp);
    172      1.27     lukem 		return 1;
    173       1.1       cgd 	}
    174      1.27     lukem 	return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
    175       1.1       cgd }
    176       1.1       cgd 
    177       1.5       jtc void
    178       1.1       cgd setgrent()
    179       1.1       cgd {
    180       1.5       jtc 	(void) setgroupent(0);
    181       1.1       cgd }
    182       1.1       cgd 
    183       1.1       cgd int
    184       1.1       cgd setgroupent(stayopen)
    185       1.1       cgd 	int stayopen;
    186       1.1       cgd {
    187       1.1       cgd 	if (!start_gr())
    188      1.27     lukem 		return 0;
    189       1.1       cgd 	_gr_stayopen = stayopen;
    190      1.27     lukem 	return 1;
    191       1.1       cgd }
    192       1.1       cgd 
    193       1.1       cgd void
    194       1.1       cgd endgrent()
    195       1.1       cgd {
    196  1.34.2.1     perry 	grcleanup();
    197       1.1       cgd 	if (_gr_fp) {
    198       1.1       cgd 		(void)fclose(_gr_fp);
    199       1.1       cgd 		_gr_fp = NULL;
    200       1.1       cgd 	}
    201       1.1       cgd }
    202       1.1       cgd 
    203      1.27     lukem 
    204      1.27     lukem static int _local_grscan __P((void *, void *, va_list));
    205      1.27     lukem 
    206      1.29  christos /*ARGSUSED*/
    207      1.27     lukem static int
    208      1.27     lukem _local_grscan(rv, cb_data, ap)
    209      1.27     lukem 	void	*rv;
    210      1.27     lukem 	void	*cb_data;
    211      1.27     lukem 	va_list	 ap;
    212      1.27     lukem {
    213      1.27     lukem 	int		 search = va_arg(ap, int);
    214      1.27     lukem 	gid_t		 gid = va_arg(ap, gid_t);
    215      1.27     lukem 	const char	*name = va_arg(ap, const char *);
    216      1.27     lukem 
    217  1.34.2.1     perry 	if (_gr_filesdone)
    218  1.34.2.1     perry 		return NS_NOTFOUND;
    219      1.27     lukem 	for (;;) {
    220      1.27     lukem 		if (!fgets(line, sizeof(line), _gr_fp)) {
    221  1.34.2.1     perry 			if (!search)
    222  1.34.2.1     perry 				_gr_filesdone = 1;
    223      1.27     lukem 			return NS_NOTFOUND;
    224      1.27     lukem 		}
    225      1.27     lukem 		/* skip lines that are too big */
    226      1.27     lukem 		if (!strchr(line, '\n')) {
    227      1.27     lukem 			int ch;
    228      1.27     lukem 
    229      1.27     lukem 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    230      1.27     lukem 				;
    231      1.27     lukem 			continue;
    232      1.27     lukem 		}
    233      1.27     lukem 		if (matchline(search, gid, name))
    234      1.27     lukem 			return NS_SUCCESS;
    235      1.27     lukem 	}
    236      1.27     lukem 	/* NOTREACHED */
    237      1.27     lukem }
    238      1.27     lukem 
    239      1.27     lukem #ifdef HESIOD
    240      1.27     lukem static int _dns_grscan __P((void *, void *, va_list));
    241      1.27     lukem 
    242      1.29  christos /*ARGSUSED*/
    243       1.2   deraadt static int
    244      1.27     lukem _dns_grscan(rv, cb_data, ap)
    245      1.27     lukem 	void	*rv;
    246      1.27     lukem 	void	*cb_data;
    247      1.27     lukem 	va_list	 ap;
    248       1.1       cgd {
    249      1.27     lukem 	int		 search = va_arg(ap, int);
    250      1.27     lukem 	gid_t		 gid = va_arg(ap, gid_t);
    251      1.27     lukem 	const char	*name = va_arg(ap, const char *);
    252      1.27     lukem 
    253      1.27     lukem 	char		**hp;
    254      1.33     lukem 	void		 *context;
    255      1.33     lukem 	int		  r;
    256      1.33     lukem 
    257      1.33     lukem 	r = NS_UNAVAIL;
    258  1.34.2.1     perry 	if (!search && _gr_hesnum == -1)
    259  1.34.2.1     perry 		return NS_NOTFOUND;
    260      1.33     lukem 	if (hesiod_init(&context) == -1)
    261      1.33     lukem 		return (r);
    262       1.1       cgd 
    263       1.1       cgd 	for (;;) {
    264      1.27     lukem 		if (search) {
    265      1.27     lukem 			if (name)
    266      1.27     lukem 				strncpy(line, name, sizeof(line));
    267      1.27     lukem 			else
    268      1.28     lukem 				snprintf(line, sizeof(line), "%u",
    269      1.28     lukem 				    (unsigned int)gid);
    270      1.27     lukem 		} else {
    271  1.34.2.1     perry 			snprintf(line, sizeof(line), "group-%u", _gr_hesnum);
    272  1.34.2.1     perry 			_gr_hesnum++;
    273      1.27     lukem 		}
    274       1.2   deraadt 
    275      1.27     lukem 		line[sizeof(line) - 1] = '\0';
    276      1.33     lukem 		hp = hesiod_resolve(context, line, "group");
    277      1.27     lukem 		if (hp == NULL) {
    278      1.33     lukem 			if (errno == ENOENT) {
    279  1.34.2.1     perry 				if (!search)
    280  1.34.2.1     perry 					_gr_hesnum = -1;
    281  1.34.2.1     perry 				r = NS_NOTFOUND;
    282       1.2   deraadt 			}
    283      1.33     lukem 			break;
    284      1.27     lukem 		}
    285      1.27     lukem 
    286      1.27     lukem 						/* only check first elem */
    287      1.27     lukem 		strncpy(line, hp[0], sizeof(line));
    288      1.27     lukem 		line[sizeof(line) - 1] = '\0';
    289      1.33     lukem 		hesiod_free_list(context, hp);
    290      1.33     lukem 		if (matchline(search, gid, name)) {
    291      1.33     lukem 			r = NS_SUCCESS;
    292      1.33     lukem 			break;
    293      1.33     lukem 		} else if (search) {
    294      1.33     lukem 			r = NS_NOTFOUND;
    295      1.33     lukem 			break;
    296      1.33     lukem 		}
    297      1.27     lukem 	}
    298      1.33     lukem 	hesiod_end(context);
    299      1.33     lukem 	return (r);
    300      1.27     lukem }
    301      1.27     lukem #endif
    302      1.27     lukem 
    303      1.27     lukem #ifdef YP
    304      1.27     lukem static int _nis_grscan __P((void *, void *, va_list));
    305      1.27     lukem 
    306      1.29  christos /*ARGSUSED*/
    307      1.27     lukem static int
    308      1.27     lukem _nis_grscan(rv, cb_data, ap)
    309      1.27     lukem 	void	*rv;
    310      1.27     lukem 	void	*cb_data;
    311      1.27     lukem 	va_list	 ap;
    312      1.27     lukem {
    313      1.27     lukem 	int		 search = va_arg(ap, int);
    314      1.27     lukem 	gid_t		 gid = va_arg(ap, gid_t);
    315      1.27     lukem 	const char	*name = va_arg(ap, const char *);
    316      1.27     lukem 
    317      1.27     lukem 	char	*key, *data;
    318      1.27     lukem 	int	 keylen, datalen;
    319      1.27     lukem 	int	 r;
    320      1.27     lukem 
    321      1.27     lukem 	if(__ypdomain == NULL) {
    322      1.27     lukem 		switch (yp_get_default_domain(&__ypdomain)) {
    323      1.27     lukem 		case 0:
    324      1.27     lukem 			break;
    325      1.27     lukem 		case YPERR_RESRC:
    326      1.27     lukem 			return NS_TRYAGAIN;
    327      1.27     lukem 		default:
    328      1.27     lukem 			return NS_UNAVAIL;
    329      1.27     lukem 		}
    330      1.27     lukem 	}
    331      1.27     lukem 
    332      1.27     lukem 	if (search) {			/* specific group or gid */
    333      1.27     lukem 		if (name)
    334      1.27     lukem 			strncpy(line, name, sizeof(line));
    335      1.27     lukem 		else
    336      1.28     lukem 			snprintf(line, sizeof(line), "%u", (unsigned int)gid);
    337      1.27     lukem 		line[sizeof(line) - 1] = '\0';
    338      1.27     lukem 		data = NULL;
    339      1.27     lukem 		r = yp_match(__ypdomain,
    340      1.27     lukem 				(name) ? "group.byname" : "group.bygid",
    341      1.27     lukem 				line, (int)strlen(line), &data, &datalen);
    342      1.27     lukem 		switch (r) {
    343      1.27     lukem 		case 0:
    344      1.27     lukem 			break;
    345      1.27     lukem 		case YPERR_KEY:
    346      1.27     lukem 			if (data)
    347      1.27     lukem 				free(data);
    348      1.27     lukem 			return NS_NOTFOUND;
    349      1.27     lukem 		default:
    350      1.27     lukem 			if (data)
    351      1.16     lukem 				free(data);
    352      1.27     lukem 			return NS_UNAVAIL;
    353      1.27     lukem 		}
    354      1.27     lukem 		data[datalen] = '\0';			/* clear trailing \n */
    355      1.27     lukem 		strncpy(line, data, sizeof(line));
    356      1.27     lukem 		line[sizeof(line) - 1] = '\0';
    357      1.27     lukem 		free(data);
    358      1.27     lukem 		if (matchline(search, gid, name))
    359      1.27     lukem 			return NS_SUCCESS;
    360      1.27     lukem 		else
    361      1.27     lukem 			return NS_NOTFOUND;
    362      1.27     lukem 	}
    363      1.27     lukem 
    364  1.34.2.1     perry 						/* ! search */
    365  1.34.2.1     perry 	if (_gr_ypdone)
    366  1.34.2.1     perry 		return NS_NOTFOUND;
    367  1.34.2.1     perry 	for (;;) {
    368      1.27     lukem 		data = NULL;
    369      1.27     lukem 		if(__ypcurrent) {
    370      1.27     lukem 			key = NULL;
    371      1.27     lukem 			r = yp_next(__ypdomain, "group.byname",
    372      1.27     lukem 				__ypcurrent, __ypcurrentlen,
    373      1.27     lukem 				&key, &keylen, &data, &datalen);
    374      1.27     lukem 			free(__ypcurrent);
    375      1.27     lukem 			switch (r) {
    376      1.27     lukem 			case 0:
    377      1.13      phil 				break;
    378      1.27     lukem 			case YPERR_NOMORE:
    379      1.27     lukem 				__ypcurrent = NULL;
    380      1.27     lukem 				if (key)
    381      1.27     lukem 					free(key);
    382      1.27     lukem 				if (data)
    383       1.2   deraadt 					free(data);
    384  1.34.2.1     perry 				_gr_ypdone = 1;
    385  1.34.2.1     perry 				return NS_NOTFOUND;
    386      1.27     lukem 			default:
    387      1.27     lukem 				if (key)
    388      1.27     lukem 					free(key);
    389      1.27     lukem 				if (data)
    390      1.27     lukem 					free(data);
    391      1.27     lukem 				return NS_UNAVAIL;
    392      1.27     lukem 			}
    393      1.27     lukem 			__ypcurrent = key;
    394      1.27     lukem 			__ypcurrentlen = keylen;
    395      1.27     lukem 		} else {
    396      1.27     lukem 			if (yp_first(__ypdomain, "group.byname",
    397      1.27     lukem 					&__ypcurrent, &__ypcurrentlen,
    398      1.27     lukem 					&data, &datalen)) {
    399      1.27     lukem 				if (data);
    400      1.27     lukem 					free(data);
    401      1.27     lukem 				return NS_UNAVAIL;
    402      1.27     lukem 			}
    403      1.27     lukem 		}
    404      1.27     lukem 		data[datalen] = '\0';			/* clear trailing \n */
    405      1.27     lukem 		strncpy(line, data, sizeof(line));
    406      1.27     lukem 		line[sizeof(line) - 1] = '\0';
    407      1.27     lukem 		free(data);
    408      1.27     lukem 		if (matchline(search, gid, name))
    409      1.27     lukem 			return NS_SUCCESS;
    410      1.27     lukem 	}
    411      1.27     lukem 	/* NOTREACHED */
    412      1.27     lukem }
    413      1.27     lukem #endif
    414      1.27     lukem 
    415      1.34     lukem #ifdef _GROUP_COMPAT
    416      1.27     lukem /*
    417      1.27     lukem  * log an error if "files" or "compat" is specified in group_compat database
    418      1.27     lukem  */
    419      1.27     lukem static int _bad_grscan __P((void *, void *, va_list));
    420      1.27     lukem 
    421      1.29  christos /*ARGSUSED*/
    422      1.27     lukem static int
    423      1.27     lukem _bad_grscan(rv, cb_data, ap)
    424      1.27     lukem 	void	*rv;
    425      1.27     lukem 	void	*cb_data;
    426      1.27     lukem 	va_list	 ap;
    427      1.27     lukem {
    428      1.27     lukem 	static int warned;
    429      1.27     lukem 
    430      1.27     lukem 	if (!warned) {
    431      1.27     lukem 		syslog(LOG_ERR,
    432      1.27     lukem 			"nsswitch.conf group_compat database can't use '%s'",
    433      1.27     lukem 			(char *)cb_data);
    434      1.27     lukem 	}
    435      1.27     lukem 	warned = 1;
    436      1.27     lukem 	return NS_UNAVAIL;
    437      1.27     lukem }
    438      1.27     lukem 
    439      1.27     lukem /*
    440      1.27     lukem  * when a name lookup in compat mode is required, look it up in group_compat
    441      1.27     lukem  * nsswitch database. only Hesiod and NIS is supported - it doesn't make
    442      1.27     lukem  * sense to lookup compat names from 'files' or 'compat'
    443      1.27     lukem  */
    444      1.27     lukem 
    445      1.27     lukem static int __grscancompat __P((int, gid_t, const char *));
    446      1.27     lukem 
    447      1.27     lukem static int
    448      1.27     lukem __grscancompat(search, gid, name)
    449      1.27     lukem 	int		 search;
    450      1.27     lukem 	gid_t		 gid;
    451      1.27     lukem 	const char	*name;
    452      1.27     lukem {
    453      1.31     lukem 	static const ns_dtab dtab[] = {
    454      1.30     lukem 		NS_FILES_CB(_bad_grscan, "files")
    455      1.30     lukem 		NS_DNS_CB(_dns_grscan, NULL)
    456      1.30     lukem 		NS_NIS_CB(_nis_grscan, NULL)
    457      1.30     lukem 		NS_COMPAT_CB(_bad_grscan, "compat")
    458      1.27     lukem 		{ 0 }
    459      1.27     lukem 	};
    460      1.32     lukem 	static const ns_src defaultnis[] = {
    461      1.32     lukem 		{ NSSRC_NIS, 	NS_SUCCESS },
    462      1.32     lukem 		{ 0 }
    463      1.32     lukem 	};
    464      1.27     lukem 
    465      1.30     lukem 	return (nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "grscancompat",
    466      1.32     lukem 	    defaultnis, search, gid, name));
    467      1.27     lukem }
    468      1.34     lukem #endif
    469      1.27     lukem 
    470      1.27     lukem 
    471      1.27     lukem static int _compat_grscan __P((void *, void *, va_list));
    472      1.27     lukem 
    473      1.29  christos /*ARGSUSED*/
    474      1.27     lukem static int
    475      1.27     lukem _compat_grscan(rv, cb_data, ap)
    476      1.27     lukem 	void	*rv;
    477      1.27     lukem 	void	*cb_data;
    478      1.27     lukem 	va_list	 ap;
    479      1.27     lukem {
    480      1.27     lukem 	int		 search = va_arg(ap, int);
    481      1.27     lukem 	gid_t		 gid = va_arg(ap, gid_t);
    482      1.27     lukem 	const char	*name = va_arg(ap, const char *);
    483      1.27     lukem 
    484      1.34     lukem #ifdef _GROUP_COMPAT
    485      1.27     lukem 	static char	*grname = NULL;
    486      1.34     lukem #endif
    487      1.27     lukem 
    488      1.27     lukem 	for (;;) {
    489      1.34     lukem #ifdef _GROUP_COMPAT
    490      1.27     lukem 		if(__grmode != GRMODE_NONE) {
    491      1.27     lukem 			int	 r;
    492      1.27     lukem 
    493      1.27     lukem 			switch(__grmode) {
    494      1.27     lukem 			case GRMODE_FULL:
    495      1.27     lukem 				r = __grscancompat(search, gid, name);
    496      1.27     lukem 				if (r == NS_SUCCESS)
    497      1.27     lukem 					return r;
    498      1.27     lukem 				__grmode = GRMODE_NONE;
    499      1.27     lukem 				break;
    500      1.27     lukem 			case GRMODE_NAME:
    501      1.27     lukem 				if(grname == (char *)NULL) {
    502      1.27     lukem 					__grmode = GRMODE_NONE;
    503      1.27     lukem 					break;
    504      1.27     lukem 				}
    505      1.27     lukem 				r = __grscancompat(1, 0, grname);
    506      1.27     lukem 				free(grname);
    507      1.27     lukem 				grname = (char *)NULL;
    508      1.27     lukem 				if (r != NS_SUCCESS)
    509      1.27     lukem 					break;
    510      1.27     lukem 				if (!search)
    511      1.27     lukem 					return NS_SUCCESS;
    512      1.27     lukem 				if (name) {
    513      1.27     lukem 					if (! strcmp(_gr_group.gr_name, name))
    514      1.27     lukem 						return NS_SUCCESS;
    515      1.13      phil 				} else {
    516      1.27     lukem 					if (_gr_group.gr_gid == gid)
    517      1.27     lukem 						return NS_SUCCESS;
    518       1.2   deraadt 				}
    519      1.20  christos 				break;
    520      1.27     lukem 			case GRMODE_NONE:
    521      1.27     lukem 				abort();
    522       1.2   deraadt 			}
    523      1.27     lukem 			continue;
    524       1.2   deraadt 		}
    525      1.34     lukem #endif /* _GROUP_COMPAT */
    526      1.27     lukem 
    527       1.1       cgd 		if (!fgets(line, sizeof(line), _gr_fp))
    528      1.27     lukem 			return NS_NOTFOUND;
    529       1.1       cgd 		/* skip lines that are too big */
    530       1.6       jtc 		if (!strchr(line, '\n')) {
    531       1.1       cgd 			int ch;
    532       1.1       cgd 
    533       1.1       cgd 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
    534       1.1       cgd 				;
    535       1.1       cgd 			continue;
    536       1.1       cgd 		}
    537      1.27     lukem 
    538      1.34     lukem #ifdef _GROUP_COMPAT
    539      1.13      phil 		if (line[0] == '+') {
    540      1.27     lukem 			char	*tptr, *bp;
    541      1.27     lukem 
    542      1.13      phil 			switch(line[1]) {
    543      1.13      phil 			case ':':
    544      1.13      phil 			case '\0':
    545      1.13      phil 			case '\n':
    546      1.27     lukem 				__grmode = GRMODE_FULL;
    547      1.13      phil 				break;
    548      1.13      phil 			default:
    549      1.27     lukem 				__grmode = GRMODE_NAME;
    550      1.27     lukem 				bp = line;
    551      1.27     lukem 				tptr = strsep(&bp, ":\n");
    552      1.27     lukem 				grname = strdup(tptr + 1);
    553      1.13      phil 				break;
    554       1.2   deraadt 			}
    555       1.9   deraadt 			continue;
    556       1.1       cgd 		}
    557      1.34     lukem #endif /* _GROUP_COMPAT */
    558      1.27     lukem 		if (matchline(search, gid, name))
    559      1.27     lukem 			return NS_SUCCESS;
    560       1.1       cgd 	}
    561       1.1       cgd 	/* NOTREACHED */
    562      1.27     lukem }
    563      1.27     lukem 
    564      1.27     lukem static int
    565      1.27     lukem grscan(search, gid, name)
    566      1.27     lukem 	int		 search;
    567      1.27     lukem 	gid_t		 gid;
    568      1.27     lukem 	const char	*name;
    569      1.27     lukem {
    570      1.27     lukem 	int		r;
    571      1.31     lukem 	static const ns_dtab dtab[] = {
    572      1.30     lukem 		NS_FILES_CB(_local_grscan, NULL)
    573      1.30     lukem 		NS_DNS_CB(_dns_grscan, NULL)
    574      1.30     lukem 		NS_NIS_CB(_nis_grscan, NULL)
    575      1.30     lukem 		NS_COMPAT_CB(_compat_grscan, NULL)
    576      1.27     lukem 		{ 0 }
    577      1.27     lukem 	};
    578      1.32     lukem 	static const ns_src compatsrc[] = {
    579      1.32     lukem 		{ NSSRC_COMPAT, NS_SUCCESS },
    580      1.32     lukem 		{ 0 }
    581      1.32     lukem 	};
    582      1.27     lukem 
    583      1.32     lukem 	r = nsdispatch(NULL, dtab, NSDB_GROUP, "grscan", compatsrc,
    584      1.30     lukem 	    search, gid, name);
    585      1.27     lukem 	return (r == NS_SUCCESS) ? 1 : 0;
    586      1.27     lukem }
    587      1.27     lukem 
    588      1.27     lukem static int
    589      1.27     lukem matchline(search, gid, name)
    590      1.27     lukem 	int		 search;
    591      1.27     lukem 	gid_t		 gid;
    592      1.27     lukem 	const char	*name;
    593      1.27     lukem {
    594      1.27     lukem 	unsigned long	id;
    595      1.27     lukem 	__aconst char	**m;
    596      1.27     lukem 	char		*cp, *bp, *ep;
    597      1.27     lukem 
    598      1.27     lukem 	if (line[0] == '+')
    599      1.27     lukem 		return 0;	/* sanity check to prevent recursion */
    600      1.27     lukem 	bp = line;
    601      1.27     lukem 	_gr_group.gr_name = strsep(&bp, ":\n");
    602      1.27     lukem 	if (search && name && strcmp(_gr_group.gr_name, name))
    603      1.27     lukem 		return 0;
    604      1.27     lukem 	_gr_group.gr_passwd = strsep(&bp, ":\n");
    605      1.27     lukem 	if (!(cp = strsep(&bp, ":\n")))
    606      1.27     lukem 		return 0;
    607      1.27     lukem 	id = strtoul(cp, &ep, 10);
    608      1.27     lukem 	if (id > GID_MAX || *ep != '\0')
    609      1.27     lukem 		return 0;
    610      1.27     lukem 	_gr_group.gr_gid = (gid_t)id;
    611      1.27     lukem 	if (search && name == NULL && _gr_group.gr_gid != gid)
    612      1.27     lukem 		return 0;
    613      1.27     lukem 	cp = NULL;
    614      1.27     lukem 	if (bp == NULL)
    615      1.27     lukem 		return 0;
    616      1.27     lukem 	for (_gr_group.gr_mem = m = members;; bp++) {
    617      1.27     lukem 		if (m == &members[MAXGRP - 1])
    618      1.27     lukem 			break;
    619      1.27     lukem 		if (*bp == ',') {
    620      1.27     lukem 			if (cp) {
    621      1.27     lukem 				*bp = '\0';
    622      1.27     lukem 				*m++ = cp;
    623      1.27     lukem 				cp = NULL;
    624      1.27     lukem 			}
    625      1.27     lukem 		} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
    626      1.27     lukem 			if (cp) {
    627      1.27     lukem 				*bp = '\0';
    628      1.27     lukem 				*m++ = cp;
    629      1.27     lukem 			}
    630      1.27     lukem 			break;
    631      1.27     lukem 		} else if (cp == NULL)
    632      1.27     lukem 			cp = bp;
    633      1.27     lukem 	}
    634      1.27     lukem 	*m = NULL;
    635      1.27     lukem 	return 1;
    636       1.1       cgd }
    637