2008-08-19 19:30:36 +00:00
|
|
|
#include <errno.h>
|
2020-10-04 14:03:39 +00:00
|
|
|
#include <getopt.h>
|
2011-06-29 04:11:17 +00:00
|
|
|
#include <limits.h>
|
2020-10-04 14:03:39 +00:00
|
|
|
#include <selinux/label.h>
|
2008-08-19 19:30:36 +00:00
|
|
|
#include <selinux/selinux.h>
|
2020-10-04 14:03:39 +00:00
|
|
|
#include <stdio.h>
|
2011-06-29 04:11:17 +00:00
|
|
|
#include <stdlib.h>
|
2020-10-04 14:03:39 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2011-06-29 04:11:17 +00:00
|
|
|
|
2016-11-01 21:23:10 +00:00
|
|
|
static __attribute__ ((__noreturn__)) void usage(const char *progname)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2019-01-31 19:48:24 +00:00
|
|
|
"usage: %s [-V] [-N] [-n] [-m type] [-f file_contexts_file] [-p prefix] [-P policy_root_path] filepath...\n",
|
2008-08-19 19:30:36 +00:00
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-10-04 14:03:39 +00:00
|
|
|
static int printmatchpathcon(struct selabel_handle *hnd, const char *path, int header, int mode, int notrans)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
2020-10-04 14:03:39 +00:00
|
|
|
char *buf = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (notrans) {
|
|
|
|
rc = selabel_lookup_raw(hnd, &buf, path, mode);
|
|
|
|
} else {
|
|
|
|
rc = selabel_lookup(hnd, &buf, path, mode);
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
if (rc < 0) {
|
2009-09-24 19:01:53 +00:00
|
|
|
if (errno == ENOENT) {
|
2019-05-10 16:47:07 +00:00
|
|
|
buf = strdup("<<none>>");
|
2009-09-24 19:01:53 +00:00
|
|
|
} else {
|
2020-10-04 14:03:39 +00:00
|
|
|
fprintf(stderr, "selabel_lookup(%s) failed: %s\n", path,
|
2009-09-24 19:01:53 +00:00
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|
|
|
|
if (header)
|
|
|
|
printf("%s\t%s\n", path, buf);
|
|
|
|
else
|
|
|
|
printf("%s\n", buf);
|
|
|
|
|
|
|
|
freecon(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-12 18:18:50 +00:00
|
|
|
static mode_t string_to_mode(char *s)
|
|
|
|
{
|
|
|
|
switch (s[0]) {
|
|
|
|
case 'b':
|
|
|
|
return S_IFBLK;
|
|
|
|
case 'c':
|
|
|
|
return S_IFCHR;
|
|
|
|
case 'd':
|
|
|
|
return S_IFDIR;
|
|
|
|
case 'p':
|
|
|
|
return S_IFIFO;
|
|
|
|
case 'l':
|
|
|
|
return S_IFLNK;
|
|
|
|
case 's':
|
|
|
|
return S_IFSOCK;
|
|
|
|
case 'f':
|
|
|
|
return S_IFREG;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-10-04 14:03:39 +00:00
|
|
|
int i, force_mode = 0;
|
2008-08-19 19:30:36 +00:00
|
|
|
int header = 1, opt;
|
|
|
|
int verify = 0;
|
|
|
|
int notrans = 0;
|
|
|
|
int error = 0;
|
|
|
|
int quiet = 0;
|
2020-10-04 14:03:39 +00:00
|
|
|
struct selabel_handle *hnd;
|
|
|
|
struct selinux_opt options[SELABEL_NOPT] = {};
|
2020-04-15 21:14:26 +00:00
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage(argv[0]);
|
|
|
|
|
2013-10-09 18:46:05 +00:00
|
|
|
while ((opt = getopt(argc, argv, "m:Nnf:P:p:Vq")) > 0) {
|
2008-08-19 19:30:36 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'n':
|
|
|
|
header = 0;
|
|
|
|
break;
|
2012-09-12 18:18:50 +00:00
|
|
|
case 'm':
|
|
|
|
force_mode = string_to_mode(optarg);
|
|
|
|
if (force_mode < 0) {
|
|
|
|
fprintf(stderr, "%s: mode %s is invalid\n", argv[0], optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2008-08-19 19:30:36 +00:00
|
|
|
case 'V':
|
|
|
|
verify = 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
notrans = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2020-10-04 14:03:39 +00:00
|
|
|
options[SELABEL_OPT_PATH].type = SELABEL_OPT_PATH;
|
|
|
|
options[SELABEL_OPT_PATH].value = optarg;
|
2008-08-19 19:30:36 +00:00
|
|
|
break;
|
2013-10-09 18:46:05 +00:00
|
|
|
case 'P':
|
|
|
|
if (selinux_set_policy_root(optarg) < 0 ) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error setting policy root %s: %s\n",
|
|
|
|
optarg,
|
|
|
|
errno ? strerror(errno) : "invalid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2008-08-19 19:30:36 +00:00
|
|
|
case 'p':
|
2020-10-04 14:03:39 +00:00
|
|
|
// This option has been deprecated since libselinux 2.5 (2016):
|
|
|
|
// https://github.com/SELinuxProject/selinux/commit/26e05da0fc2d0a4bd274320968a88f8acbb3b6a6
|
|
|
|
fprintf(stderr, "Warning: using %s -p is deprecated\n", argv[0]);
|
|
|
|
options[SELABEL_OPT_SUBSET].type = SELABEL_OPT_SUBSET;
|
|
|
|
options[SELABEL_OPT_SUBSET].value = optarg;
|
2008-08-19 19:30:36 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 14:03:39 +00:00
|
|
|
hnd = selabel_open(SELABEL_CTX_FILE, options, SELABEL_NOPT);
|
|
|
|
if (!hnd) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error while opening file contexts database: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-19 19:30:36 +00:00
|
|
|
for (i = optind; i < argc; i++) {
|
2011-06-29 04:11:17 +00:00
|
|
|
int rc, mode = 0;
|
2008-08-19 19:30:36 +00:00
|
|
|
struct stat buf;
|
2011-08-17 15:24:25 +00:00
|
|
|
char *path = argv[i];
|
2011-06-29 04:11:17 +00:00
|
|
|
int len = strlen(path);
|
|
|
|
if (len > 1 && path[len - 1 ] == '/')
|
|
|
|
path[len - 1 ] = '\0';
|
2009-04-10 15:33:23 +00:00
|
|
|
|
2011-06-29 04:11:17 +00:00
|
|
|
if (lstat(path, &buf) == 0)
|
2008-08-19 19:30:36 +00:00
|
|
|
mode = buf.st_mode;
|
2012-09-12 18:18:50 +00:00
|
|
|
if (force_mode)
|
|
|
|
mode = force_mode;
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
if (verify) {
|
2011-06-29 04:11:17 +00:00
|
|
|
rc = selinux_file_context_verify(path, mode);
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
if (quiet) {
|
2011-03-09 16:34:08 +00:00
|
|
|
if (rc == 1)
|
2008-08-19 19:30:36 +00:00
|
|
|
continue;
|
|
|
|
else
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-06-29 04:11:17 +00:00
|
|
|
|
2011-03-09 16:34:08 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
printf("%s error: %s\n", path, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
} else if (rc == 1) {
|
2011-06-29 04:11:17 +00:00
|
|
|
printf("%s verified.\n", path);
|
2008-08-19 19:30:36 +00:00
|
|
|
} else {
|
2014-02-19 14:16:17 +00:00
|
|
|
char * con;
|
2008-08-19 19:30:36 +00:00
|
|
|
error = 1;
|
|
|
|
if (notrans)
|
2011-06-29 04:11:17 +00:00
|
|
|
rc = lgetfilecon_raw(path, &con);
|
2008-08-19 19:30:36 +00:00
|
|
|
else
|
2011-06-29 04:11:17 +00:00
|
|
|
rc = lgetfilecon(path, &con);
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
if (rc >= 0) {
|
|
|
|
printf("%s has context %s, should be ",
|
2011-06-29 04:11:17 +00:00
|
|
|
path, con);
|
2020-10-04 14:03:39 +00:00
|
|
|
printmatchpathcon(hnd, path, 0, mode, notrans);
|
2008-08-19 19:30:36 +00:00
|
|
|
freecon(con);
|
|
|
|
} else {
|
|
|
|
printf
|
|
|
|
("actual context unknown: %s, should be ",
|
|
|
|
strerror(errno));
|
2020-10-04 14:03:39 +00:00
|
|
|
printmatchpathcon(hnd, path, 0, mode, notrans);
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-10-04 14:03:39 +00:00
|
|
|
error |= printmatchpathcon(hnd, path, header, mode, notrans);
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 14:03:39 +00:00
|
|
|
selabel_close(hnd);
|
2008-08-19 19:30:36 +00:00
|
|
|
return error;
|
|
|
|
}
|