initgroups.c revision 256281
1240116Smarcel/*
2240116Smarcel * Copyright (c) 1983, 1993
3240116Smarcel *	The Regents of the University of California.  All rights reserved.
4240116Smarcel *
5240116Smarcel * Redistribution and use in source and binary forms, with or without
6240116Smarcel * modification, are permitted provided that the following conditions
7240116Smarcel * are met:
8240116Smarcel * 1. Redistributions of source code must retain the above copyright
9240116Smarcel *    notice, this list of conditions and the following disclaimer.
10240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer in the
12240116Smarcel *    documentation and/or other materials provided with the distribution.
13240116Smarcel * 4. Neither the name of the University nor the names of its contributors
14240116Smarcel *    may be used to endorse or promote products derived from this software
15240116Smarcel *    without specific prior written permission.
16240116Smarcel *
17240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20240116Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23240116Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24240116Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25240116Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26273929Sjmmv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27273929Sjmmv * SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#if defined(LIBC_SCCS) && !defined(lint)
31240116Smarcelstatic char sccsid[] = "@(#)initgroups.c	8.1 (Berkeley) 6/4/93";
32240116Smarcel#endif
33240116Smarcel#include <sys/cdefs.h>
34240116Smarcel__FBSDID("$FreeBSD: stable/10/lib/libc/gen/initgroups.c 194494 2009-06-19 15:58:24Z brooks $");
35240116Smarcel
36240116Smarcel#include <sys/param.h>
37273929Sjmmv
38240116Smarcel#include "namespace.h"
39273929Sjmmv#include <err.h>
40240116Smarcel#include "un-namespace.h"
41240116Smarcel#include <errno.h>
42240116Smarcel#include <stdio.h>
43240116Smarcel#include <stdlib.h>
44240116Smarcel#include <unistd.h>
45240116Smarcel
46240116Smarcelint
47240116Smarcelinitgroups(uname, agroup)
48240116Smarcel	const char *uname;
49240116Smarcel	gid_t agroup;
50240116Smarcel{
51240116Smarcel	int ngroups, ret;
52240116Smarcel	long ngroups_max;
53240116Smarcel	gid_t *groups;
54240116Smarcel
55240116Smarcel	/*
56240116Smarcel	 * Provide space for one group more than possible to allow
57240116Smarcel	 * setgroups to fail and set errno.
58240116Smarcel	 */
59240116Smarcel	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2;
60240116Smarcel	if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL)
61240116Smarcel		return (ENOMEM);
62240116Smarcel
63240116Smarcel	ngroups = (int)ngroups_max;
64240116Smarcel	getgrouplist(uname, agroup, groups, &ngroups);
65240116Smarcel	ret = setgroups(ngroups, groups);
66240116Smarcel	free(groups);
67240116Smarcel	return (ret);
68240116Smarcel}
69240116Smarcel