1323134Sdes/*	$OpenBSD: getgrouplist.c,v 1.12 2005/08/08 08:05:34 espie Exp */
298937Sdes/*
398937Sdes * Copyright (c) 1991, 1993
498937Sdes *	The Regents of the University of California.  All rights reserved.
598937Sdes *
698937Sdes * Redistribution and use in source and binary forms, with or without
798937Sdes * modification, are permitted provided that the following conditions
898937Sdes * are met:
998937Sdes * 1. Redistributions of source code must retain the above copyright
1098937Sdes *    notice, this list of conditions and the following disclaimer.
1198937Sdes * 2. Redistributions in binary form must reproduce the above copyright
1298937Sdes *    notice, this list of conditions and the following disclaimer in the
1398937Sdes *    documentation and/or other materials provided with the distribution.
14124208Sdes * 3. Neither the name of the University nor the names of its contributors
1598937Sdes *    may be used to endorse or promote products derived from this software
1698937Sdes *    without specific prior written permission.
1798937Sdes *
1898937Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1998937Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2098937Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2198937Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2298937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2398937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2498937Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2598937Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2698937Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2798937Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2898937Sdes * SUCH DAMAGE.
2998937Sdes */
3098937Sdes
31157016Sdes/* OPENBSD ORIGINAL: lib/libc/gen/getgrouplist.c */
32157016Sdes
3398937Sdes#include "includes.h"
3498937Sdes
3598937Sdes#ifndef HAVE_GETGROUPLIST
3698937Sdes
3798937Sdes/*
3898937Sdes * get credential
3998937Sdes */
4098937Sdes#include <sys/types.h>
4198937Sdes#include <string.h>
42124208Sdes#include <unistd.h>
4398937Sdes#include <grp.h>
4498937Sdes
4598937Sdesint
46157016Sdesgetgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt)
4798937Sdes{
48157016Sdes	struct group *grp;
49157016Sdes	int i, ngroups;
5098937Sdes	int ret, maxgroups;
5198937Sdes	int bail;
5298937Sdes
5398937Sdes	ret = 0;
5498937Sdes	ngroups = 0;
5598937Sdes	maxgroups = *grpcnt;
5698937Sdes
5798937Sdes	/*
5898937Sdes	 * install primary group
5998937Sdes	 */
6098937Sdes	if (ngroups >= maxgroups) {
6198937Sdes		*grpcnt = ngroups;
6298937Sdes		return (-1);
6398937Sdes	}
6498937Sdes	groups[ngroups++] = agroup;
6598937Sdes
6698937Sdes	/*
6798937Sdes	 * Scan the group file to find additional groups.
6898937Sdes	 */
6998937Sdes	setgrent();
7098937Sdes	while ((grp = getgrent())) {
7198937Sdes		if (grp->gr_gid == agroup)
7298937Sdes			continue;
7398937Sdes		for (bail = 0, i = 0; bail == 0 && i < ngroups; i++)
7498937Sdes			if (groups[i] == grp->gr_gid)
7598937Sdes				bail = 1;
7698937Sdes		if (bail)
7798937Sdes			continue;
7898937Sdes		for (i = 0; grp->gr_mem[i]; i++) {
7998937Sdes			if (!strcmp(grp->gr_mem[i], uname)) {
8098937Sdes				if (ngroups >= maxgroups) {
8198937Sdes					ret = -1;
8298937Sdes					goto out;
8398937Sdes				}
8498937Sdes				groups[ngroups++] = grp->gr_gid;
8598937Sdes				break;
8698937Sdes			}
8798937Sdes		}
8898937Sdes	}
8998937Sdesout:
9098937Sdes	endgrent();
9198937Sdes	*grpcnt = ngroups;
9298937Sdes	return (ret);
9398937Sdes}
9498937Sdes
9598937Sdes#endif /* HAVE_GETGROUPLIST */
96