getgrouplist.c revision 181110
1160814Ssimon/* $OpenBSD: getgrouplist.c,v 1.12 2005/08/08 08:05:34 espie Exp $ */ 2160814Ssimon/* 3238405Sjkim * Copyright (c) 1991, 1993 4160814Ssimon * The Regents of the University of California. All rights reserved. 5160814Ssimon * 6160814Ssimon * Redistribution and use in source and binary forms, with or without 7160814Ssimon * modification, are permitted provided that the following conditions 8160814Ssimon * are met: 9160814Ssimon * 1. Redistributions of source code must retain the above copyright 10280304Sjkim * notice, this list of conditions and the following disclaimer. 11160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 12160814Ssimon * notice, this list of conditions and the following disclaimer in the 13160814Ssimon * documentation and/or other materials provided with the distribution. 14160814Ssimon * 3. Neither the name of the University nor the names of its contributors 15160814Ssimon * may be used to endorse or promote products derived from this software 16160814Ssimon * without specific prior written permission. 17160814Ssimon * 18160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21160814Ssimon * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28160814Ssimon * SUCH DAMAGE. 29160814Ssimon */ 30160814Ssimon 31160814Ssimon/* OPENBSD ORIGINAL: lib/libc/gen/getgrouplist.c */ 32160814Ssimon 33160814Ssimon#include "includes.h" 34160814Ssimon 35160814Ssimon#ifndef HAVE_GETGROUPLIST 36160814Ssimon 37160814Ssimon/* 38160814Ssimon * get credential 39160814Ssimon */ 40160814Ssimon#include <sys/types.h> 41160814Ssimon#include <string.h> 42160814Ssimon#include <unistd.h> 43160814Ssimon#include <grp.h> 44160814Ssimon 45160814Ssimonint 46160814Ssimongetgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt) 47160814Ssimon{ 48160814Ssimon struct group *grp; 49160814Ssimon int i, ngroups; 50160814Ssimon int ret, maxgroups; 51160814Ssimon int bail; 52160814Ssimon 53160814Ssimon ret = 0; 54160814Ssimon ngroups = 0; 55160814Ssimon maxgroups = *grpcnt; 56280304Sjkim 57280304Sjkim /* 58160814Ssimon * install primary group 59160814Ssimon */ 60160814Ssimon if (ngroups >= maxgroups) { 61160814Ssimon *grpcnt = ngroups; 62160814Ssimon return (-1); 63160814Ssimon } 64160814Ssimon groups[ngroups++] = agroup; 65160814Ssimon 66160814Ssimon /* 67160814Ssimon * Scan the group file to find additional groups. 68160814Ssimon */ 69280304Sjkim setgrent(); 70280304Sjkim while ((grp = getgrent())) { 71160814Ssimon if (grp->gr_gid == agroup) 72280304Sjkim continue; 73280304Sjkim for (bail = 0, i = 0; bail == 0 && i < ngroups; i++) 74280304Sjkim if (groups[i] == grp->gr_gid) 75280304Sjkim bail = 1; 76280304Sjkim if (bail) 77280304Sjkim continue; 78280304Sjkim for (i = 0; grp->gr_mem[i]; i++) { 79280304Sjkim if (!strcmp(grp->gr_mem[i], uname)) { 80160814Ssimon if (ngroups >= maxgroups) { 81280304Sjkim ret = -1; 82280304Sjkim goto out; 83280304Sjkim } 84280304Sjkim groups[ngroups++] = grp->gr_gid; 85280304Sjkim break; 86280304Sjkim } 87280304Sjkim } 88280304Sjkim } 89280304Sjkimout: 90280304Sjkim endgrent(); 91280304Sjkim *grpcnt = ngroups; 92280304Sjkim return (ret); 93280304Sjkim} 94160814Ssimon 95160814Ssimon#endif /* HAVE_GETGROUPLIST */ 96160814Ssimon