summaryrefslogtreecommitdiffstats
path: root/src/get_subid_owners.c
blob: e1c1e795eae9086181b39190e7f3e0d895ea030a (plain)
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
/* SPDX-License-Identifier: BSD-3-Clause */

#include <stdio.h>
#include "subid.h"
#include "stdlib.h"
#include "prototypes.h"
#include "shadowlog.h"

static const char Prog[] = "get_subid_owners";

static void usage(void)
{
	fprintf(stderr, "Usage: [-g] %s subuid\n", Prog);
	fprintf(stderr, "    list uids who own the given subuid\n");
	fprintf(stderr, "    pass -g to query a subgid\n");
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	int i, n;
	uid_t *uids;

	log_set_progname(Prog);
	log_set_logfd(stderr);
	if (argc < 2) {
		usage();
	}
	if (argc == 3 && strcmp(argv[1], "-g") == 0)
		n = subid_get_gid_owners(atoi(argv[2]), &uids);
	else if (argc == 2 && strcmp(argv[1], "-h") == 0)
		usage();
	else
		n = subid_get_uid_owners(atoi(argv[1]), &uids);
	if (n < 0) {
		fprintf(stderr, "No owners found\n");
		exit(1);
	}
	for (i = 0; i < n; i++) {
		printf("%d\n", uids[i]);
	}
	free(uids);
	return 0;
}