2008-08-19 19:30:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/get_context_list.h>
|
|
|
|
|
2016-11-01 21:23:10 +00:00
|
|
|
static __attribute__ ((__noreturn__)) void usage(const char *name, const char *detail, int rc)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
2012-06-21 19:44:45 +00:00
|
|
|
fprintf(stderr, "usage: %s [-l level] [-s service] user [fromcon]\n", name);
|
2008-08-19 19:30:36 +00:00
|
|
|
if (detail)
|
|
|
|
fprintf(stderr, "%s: %s\n", name, detail);
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-02-19 14:16:17 +00:00
|
|
|
char * usercon = NULL, *cur_context = NULL;
|
2008-08-19 19:30:36 +00:00
|
|
|
char *user = NULL, *level = NULL, *role=NULL, *seuser=NULL, *dlevel=NULL;
|
2012-06-21 19:44:45 +00:00
|
|
|
char *service = NULL;
|
2008-08-19 19:30:36 +00:00
|
|
|
int ret, opt;
|
2009-05-14 19:43:18 +00:00
|
|
|
int verbose = 0;
|
2008-08-19 19:30:36 +00:00
|
|
|
|
2012-06-21 19:44:45 +00:00
|
|
|
while ((opt = getopt(argc, argv, "l:r:s:v")) > 0) {
|
2008-08-19 19:30:36 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
2021-05-03 15:11:07 +00:00
|
|
|
free(level);
|
2008-08-19 19:30:36 +00:00
|
|
|
level = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'r':
|
2021-05-03 15:11:07 +00:00
|
|
|
free(role);
|
2008-08-19 19:30:36 +00:00
|
|
|
role = strdup(optarg);
|
|
|
|
break;
|
2012-06-21 19:44:45 +00:00
|
|
|
case 's':
|
2021-05-03 15:11:07 +00:00
|
|
|
free(service);
|
2012-06-21 19:44:45 +00:00
|
|
|
service = strdup(optarg);
|
|
|
|
break;
|
2009-05-14 19:43:18 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2008-08-19 19:30:36 +00:00
|
|
|
default:
|
|
|
|
usage(argv[0], "invalid option", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((argc - optind) < 1) || ((argc - optind) > 2))
|
|
|
|
usage(argv[0], "invalid number of arguments", 2);
|
|
|
|
|
|
|
|
/* If selinux isn't available, bail out. */
|
|
|
|
if (!is_selinux_enabled()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s may be used only on a SELinux kernel.\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
user = argv[optind];
|
|
|
|
|
|
|
|
/* If a context wasn't passed, use the current context. */
|
|
|
|
if (((argc - optind) < 2)) {
|
|
|
|
if (getcon(&cur_context) < 0) {
|
2022-05-05 17:44:01 +00:00
|
|
|
fprintf(stderr, "Couldn't get current context: %s\n", strerror(errno));
|
2008-08-19 19:30:36 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
cur_context = argv[optind + 1];
|
|
|
|
|
2022-04-05 13:26:30 +00:00
|
|
|
if (security_check_context(cur_context)) {
|
|
|
|
fprintf(stderr, "%s: invalid from context '%s'\n", argv[0], cur_context);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2012-06-21 19:44:45 +00:00
|
|
|
if ((ret = getseuser(user, service, &seuser, &dlevel)) == 0) {
|
2008-08-19 19:30:36 +00:00
|
|
|
if (! level) level=dlevel;
|
|
|
|
if (role != NULL && role[0])
|
|
|
|
ret=get_default_context_with_rolelevel(seuser, role, level,cur_context,&usercon);
|
|
|
|
else
|
|
|
|
ret=get_default_context_with_level(seuser, level, cur_context,&usercon);
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
perror(argv[0]);
|
2009-05-14 19:43:18 +00:00
|
|
|
else {
|
|
|
|
if (verbose) {
|
|
|
|
printf("%s: %s from %s %s %s %s -> %s\n", argv[0], user, cur_context, seuser, role, level, usercon);
|
|
|
|
} else {
|
2012-06-21 19:44:45 +00:00
|
|
|
printf("%s\n", usercon);
|
2009-05-14 19:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
free(role);
|
|
|
|
free(seuser);
|
|
|
|
if (level != dlevel) free(level);
|
|
|
|
free(dlevel);
|
|
|
|
free(usercon);
|
|
|
|
|
2009-05-14 19:43:18 +00:00
|
|
|
return ret >= 0;
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|