File: addgrps.c

package info (click to toggle)
shadow 1%3A4.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 66,920 kB
  • sloc: sh: 44,121; ansic: 34,155; xml: 12,285; exp: 3,691; makefile: 1,650; python: 1,135; perl: 120; sed: 16
file content (81 lines) | stat: -rw-r--r-- 1,742 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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 "search/l/lsearch.h"
#include "shadow/grp/agetgroups.h"
#include "shadowlog.h"
#include "string/strchr/strchrscnt.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    *g, *p, *dup;
	FILE *shadow_logfd = log_get_logfd();
	gid_t   *gids;
	size_t  n;

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

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

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

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

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

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

	if (setgroups(n, gids) == -1) {
		fprintf(shadow_logfd, "setgroups: %s\n", strerror(errno));
		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 */