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