// SPDX-FileCopyrightText: 1989-1994, Julianne Frances Haugh
// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
// SPDX-FileCopyrightText: 2001-2006, Tomasz Kłoczko
// SPDX-FileCopyrightText: 2007-2009, Nicolas François
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#if !defined(USE_PAM)

#include "prototypes.h"
#include "defines.h"

#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "alloc/reallocf.h"
#include "io/fprintf.h"
#include "search/l/lsearch.h"
#include "shadow/grp/agetgroups.h"
#include "shadowlog.h"
#include "string/strchr/strchrscnt.h"
#include "string/strcmp/streq.h"


/*
 * Add groups with names from LIST (separated by commas or colons)
 * to the supplementary group set.  Silently ignore groups which are
 * already there.
 */
int
add_groups(const char *list)
{
	char    *dup;
	gid_t   *gids;
	size_t  n;

	gids = agetgroups(&n);
	if (gids == NULL)
		return -1;

	gids = reallocf_T(gids, n + strchrscnt(list, ",:") + 1, gid_t);
	if (gids == NULL)
		return -1;

	dup = strdup(list);
	if (dup == NULL)
		goto free_gids;

	if (!streq(dup, "")) {
		char  *g, *p;

		p = dup;
		while (NULL != (g = strsep(&p, ",:"))) {
			struct group  *grp;

			grp = getgrnam(g); /* local, no need for xgetgrnam */
			if (NULL == grp) {
				fprintf(log_get_logfd(), _("Warning: unknown group %s\n"), g);
				continue;
			}

			LSEARCH(gid_t, &grp->gr_gid, gids, &n);
		}
	}
	free(dup);

	if (setgroups(n, gids) == -1) {
		fprinte(log_get_logfd(), "setgroups");
		goto free_gids;
	}

	free(gids);
	return 0;

free_gids:
	free(gids);
	return -1;
}
#else				/* !USE_PAM */
extern int ISO_C_forbids_an_empty_translation_unit;
#endif				/* !USE_PAM */
