Home | History | Annotate | Line # | Download | only in gen
getgrouplist.c revision 1.19
      1 /*	$NetBSD: getgrouplist.c,v 1.19 2004/09/25 12:27:35 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn.
      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 NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1991, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 #if defined(LIBC_SCCS) && !defined(lint)
     70 #if 0
     71 static char sccsid[] = "@(#)getgrouplist.c	8.2 (Berkeley) 12/8/94";
     72 #else
     73 __RCSID("$NetBSD: getgrouplist.c,v 1.19 2004/09/25 12:27:35 lukem Exp $");
     74 #endif
     75 #endif /* LIBC_SCCS and not lint */
     76 
     77 /*
     78  * calculate group access list
     79  */
     80 
     81 #include "namespace.h"
     82 #include <sys/param.h>
     83 
     84 #include <assert.h>
     85 #include <errno.h>
     86 #include <grp.h>
     87 #include <nsswitch.h>
     88 #include <stdarg.h>
     89 #include <stdlib.h>
     90 #include <string.h>
     91 #include <unistd.h>
     92 
     93 #ifdef HESIOD
     94 #include <hesiod.h>
     95 #endif
     96 
     97 #ifdef __weak_alias
     98 __weak_alias(getgrouplist,_getgrouplist)
     99 #endif
    100 
    101 #ifdef HESIOD
    102 
    103 /*ARGSUSED*/
    104 static int
    105 _nss_dns_getgrouplist(void *retval, void *cb_data, va_list ap)
    106 {
    107 	const char 	*uname	= va_arg(ap, const char *);
    108 	gid_t		 agroup	= va_arg(ap, gid_t);
    109 	gid_t		*groups	= va_arg(ap, gid_t *);
    110 	int		*grpcnt	= va_arg(ap, int *);
    111 
    112 	unsigned long	id;
    113 	void		*context;
    114 	char		**hp, *cp, *ep;
    115 	int		rv, ret, ngroups, maxgroups;
    116 
    117 	hp = NULL;
    118 	rv = NS_NOTFOUND;
    119 	ret = 0;
    120 
    121 	if (hesiod_init(&context) == -1)		/* setup hesiod */
    122 		return NS_UNAVAIL;
    123 
    124 	hp = hesiod_resolve(context, uname, "grplist");	/* find grplist */
    125 	if (hp == NULL) {
    126 		if (errno != ENOENT)
    127 			rv = NS_NOTFOUND;
    128 		goto dnsgrouplist_out;
    129 	}
    130 
    131 	if ((ep = strchr(hp[0], '\n')) != NULL)
    132 		*ep = '\0';				/* clear trailing \n */
    133 
    134 	ret = 0;
    135 	ngroups = 0;
    136 	maxgroups = *grpcnt;
    137 
    138 	if (ngroups < maxgroups)			/* add primary gid */
    139 		groups[ngroups] = agroup;
    140 	else
    141 		ret = -1;
    142 	ngroups++;
    143 
    144 	for (cp = hp[0]; *cp != '\0'; ) {		/* parse grplist */
    145 		if ((cp = strchr(cp, ':')) == NULL)	/* skip grpname */
    146 			break;
    147 		cp++;
    148 		id = strtoul(cp, &ep, 10);		/* parse gid */
    149 		if (id > GID_MAX || (*ep != ':' && *ep != '\0')) {
    150 			rv = NS_UNAVAIL;
    151 			goto dnsgrouplist_out;
    152 		}
    153 		cp = ep;
    154 		if (*cp == ':')
    155 			cp++;
    156 		if (ngroups < maxgroups)		/* add this gid */
    157 			groups[ngroups] = (gid_t)id;
    158 		else
    159 			ret = -1;
    160 		ngroups++;
    161 	}
    162 
    163 	*(int *)retval = ret;
    164 	*grpcnt = ngroups;
    165 	rv = NS_SUCCESS;
    166 
    167  dnsgrouplist_out:
    168 	if (hp)
    169 		hesiod_free_list(context, hp);
    170 	hesiod_end(context);
    171 	return rv;
    172 }
    173 
    174 #endif /* HESIOD */
    175 
    176 int
    177 getgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt)
    178 {
    179 	struct group *grp;
    180 	int i, ngroups, maxgroups, ret, glretval;
    181 
    182 	static const ns_dtab dtab[] = {
    183 		NS_DNS_CB(_nss_dns_getgrouplist, NULL)
    184 		{ 0 }
    185 	};
    186 
    187 	_DIAGASSERT(uname != NULL);
    188 	_DIAGASSERT(groups != NULL);
    189 	_DIAGASSERT(grpcnt != NULL);
    190 
    191 			/* first, try source-specific optimized getgrouplist */
    192 	ret = nsdispatch(&glretval, dtab, NSDB_GROUP, "getgrouplist",
    193 	    __nsdefaultsrc,
    194 	    uname, agroup, groups, grpcnt);
    195 	if (ret == NS_SUCCESS)
    196 		return glretval;
    197 
    198 			/* fallback to scan the group(5) database */
    199 	ret = 0;
    200 	ngroups = 0;
    201 	maxgroups = *grpcnt;
    202 
    203 	/*
    204 	 * install primary group
    205 	 */
    206 	if (ngroups < maxgroups)
    207 		groups[ngroups] = agroup;
    208 	else
    209 		ret = -1;
    210 	ngroups++;
    211 
    212 	/*
    213 	 * Scan the group file to find additional groups.
    214 	 */
    215 	setgrent();
    216  nextgroup:
    217 	while ((grp = getgrent()) != NULL) {
    218 		if (grp->gr_gid == agroup)
    219 			continue;
    220 		for (i = 0; grp->gr_mem[i]; i++) {
    221 			if (strcmp(grp->gr_mem[i], uname) != 0)
    222 				continue;
    223 			for (i = 0; i < MIN(ngroups, maxgroups); i++) {
    224 				if (grp->gr_gid == groups[i])
    225 					goto nextgroup;
    226 			}
    227 			if (ngroups < maxgroups)
    228 				groups[ngroups] = grp->gr_gid;
    229 			else
    230 				ret = -1;
    231 			ngroups++;
    232 			break;
    233 		}
    234 	}
    235 	endgrent();
    236 	*grpcnt = ngroups;
    237 	return ret;
    238 }
    239