shadow/src/get_subid_owners.c
Serge Hallyn 2b22a6909d libsubid: don't print error messages on stderr by default
Closes #325

Add a new subid_init() function which can be used to specify the
stream on which error messages should be printed.  (If you want to
get fancy you can redirect that to memory :)  If subid_init() is
not called, use stderr.  If NULL is passed, then /dev/null will
be used.

This patch also fixes up the 'Prog', which previously had to be
defined by any program linking against libsubid.  Now, by default
in libsubid it will show (subid).  Once subid_init() is called,
it will use the first variable passed to subid_init().

Signed-off-by: Serge Hallyn <serge@hallyn.com>
2021-05-15 12:38:55 -05:00

43 lines
841 B
C

#include <stdio.h>
#include "subid.h"
#include "stdlib.h"
#include "prototypes.h"
const char *Prog;
FILE *shadow_logfd = NULL;
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;
Prog = Basename (argv[0]);
shadow_logfd = stderr;
if (argc < 2) {
usage();
}
if (argc == 3 && strcmp(argv[1], "-g") == 0)
n = get_subgid_owners(atoi(argv[2]), &uids);
else if (argc == 2 && strcmp(argv[1], "-h") == 0)
usage();
else
n = get_subuid_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;
}