xref: /freebsd/lib/libc/gen/initgroups.c (revision 88b8b7f0c4e9948667a2279e78e975a784049cba)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2025 The FreeBSD Foundation
7  *
8  * Portions of this software were developed by Olivier Certner
9  * <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD
10  * Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /* For __sym_compat(). */
38 #include <sys/cdefs.h>
39 
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 /* For freebsd14_setgroups(). */
45 #include "gen-compat.h"
46 
47 static int
48 initgroups_impl(const char *uname, gid_t agroup,
49     int (*setgroups)(int, const gid_t *))
50 {
51 	gid_t *groups;
52 	long ngroups_max;
53 	int ngroups, ret;
54 
55 	/*
56 	 * Provide space for one group more than possible to allow setgroups()
57 	 * to fail and set 'errno' in case we get back more than {NGROUPS_MAX} +
58 	 * 1 groups.
59 	 */
60 	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2;
61 	groups = malloc(sizeof(*groups) * ngroups_max);
62 	if (groups == NULL)
63 		return (-1); /* malloc() set 'errno'. */
64 
65 	ngroups = (int)ngroups_max;
66 	(void)getgrouplist(uname, agroup, groups, &ngroups);
67 	ret = (*setgroups)(ngroups, groups);
68 
69 	free(groups);
70 	return (ret); /* setgroups() set 'errno'. */
71 }
72 
73 int
74 initgroups(const char *uname, gid_t agroup)
75 {
76 	return (initgroups_impl(uname, agroup, setgroups));
77 }
78 
79 int
80 freebsd14_initgroups(const char *uname, gid_t agroup)
81 {
82 	return (initgroups_impl(uname, agroup, freebsd14_setgroups));
83 }
84 
85 __sym_compat(initgroups, freebsd14_initgroups, FBSD_1.0);
86