Skip to content

Commit

Permalink
libselinux: update getseuser
Browse files Browse the repository at this point in the history
- Bail out if not running on a SELinux enabled system
- Check whether the passed context is valid
- Do not report a get_ordered_context_list_with_level failure on zero
  found contexts

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
  • Loading branch information
cgzones authored and fishilico committed Jan 20, 2021
1 parent e2dca5d commit 156dd0d
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions libselinux/utils/getseuser.c
Expand Up @@ -9,32 +9,51 @@ int main(int argc, char **argv)
{
char *seuser = NULL, *level = NULL;
char **contextlist;
int rc, n, i;
int rc, n;

if (argc != 3) {
fprintf(stderr, "usage: %s linuxuser fromcon\n", argv[0]);
exit(1);
return 1;
}

if (!is_selinux_enabled()) {
fprintf(stderr, "%s may be used only on a SELinux enabled kernel.\n", argv[0]);
return 4;
}

rc = getseuserbyname(argv[1], &seuser, &level);
if (rc) {
fprintf(stderr, "getseuserbyname failed: %s\n",
strerror(errno));
exit(2);
fprintf(stderr, "getseuserbyname failed: %s\n", strerror(errno));
return 2;
}
printf("seuser: %s, level %s\n", seuser, level);
n = get_ordered_context_list_with_level(seuser, level, argv[2],
&contextlist);
if (n <= 0) {
fprintf(stderr,
"get_ordered_context_list_with_level failed: %s\n",
strerror(errno));
exit(3);

rc = security_check_context(argv[2]);
if (rc) {
fprintf(stderr, "context '%s' is invalid\n", argv[2]);
free(seuser);
free(level);
return 5;
}

n = get_ordered_context_list_with_level(seuser, level, argv[2], &contextlist);
if (n < 0) {
fprintf(stderr, "get_ordered_context_list_with_level failed: %s\n", strerror(errno));
free(seuser);
free(level);
return 3;
}

free(seuser);
free(level);
for (i = 0; i < n; i++)

if (n == 0)
printf("no valid context found\n");

for (int i = 0; i < n; i++)
printf("Context %d\t%s\n", i, contextlist[i]);

freeconary(contextlist);
exit(EXIT_SUCCESS);

return EXIT_SUCCESS;
}

0 comments on commit 156dd0d

Please sign in to comment.