2008-08-19 19:30:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2011-06-29 04:11:17 +00:00
|
|
|
#include <limits.h>
|
2008-08-19 19:30:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <selinux/selinux.h>
|
2011-06-29 04:11:17 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-08-19 19:30:36 +00:00
|
|
|
|
|
|
|
void usage(const char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-N] [-n] [-f file_contexts] [-p prefix] [-Vq] path...\n",
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int printmatchpathcon(char *path, int header, int mode)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int rc = matchpathcon(path, mode, &buf);
|
|
|
|
if (rc < 0) {
|
2009-09-24 19:01:53 +00:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
buf=strdup("<<none>>");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "matchpathcon(%s) failed: %s\n", path,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i, init = 0;
|
|
|
|
int header = 1, opt;
|
|
|
|
int verify = 0;
|
|
|
|
int notrans = 0;
|
|
|
|
int error = 0;
|
|
|
|
int quiet = 0;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "Nnf:p:Vq")) > 0) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'n':
|
|
|
|
header = 0;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
verify = 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
notrans = 1;
|
|
|
|
set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (init) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: -f and -p are exclusive\n",
|
|
|
|
argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
init = 1;
|
|
|
|
if (matchpathcon_init(optarg)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error while processing %s: %s\n",
|
|
|
|
optarg,
|
|
|
|
errno ? strerror(errno) : "invalid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if (init) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: -f and -p are exclusive\n",
|
|
|
|
argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
init = 1;
|
|
|
|
if (matchpathcon_init_prefix(NULL, optarg)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error while processing %s: %s\n",
|
|
|
|
optarg,
|
|
|
|
errno ? strerror(errno) : "invalid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-06-29 04:11:17 +00:00
|
|
|
if (rc)
|
2008-08-19 19:30:36 +00:00
|
|
|
continue;
|
|
|
|
else
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-06-29 04:11:17 +00:00
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
printf("%s verified.\n", path);
|
2008-08-19 19:30:36 +00:00
|
|
|
} else {
|
|
|
|
security_context_t con;
|
|
|
|
int rc;
|
|
|
|
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);
|
|
|
|
printmatchpathcon(path, 0, mode);
|
2008-08-19 19:30:36 +00:00
|
|
|
freecon(con);
|
|
|
|
} else {
|
|
|
|
printf
|
|
|
|
("actual context unknown: %s, should be ",
|
|
|
|
strerror(errno));
|
2011-06-29 04:11:17 +00:00
|
|
|
printmatchpathcon(path, 0, mode);
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2011-06-29 04:11:17 +00:00
|
|
|
error |= printmatchpathcon(path, header, mode);
|
2008-08-19 19:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
matchpathcon_fini();
|
|
|
|
return error;
|
|
|
|
}
|