new applets: selinux utils by KaiGai Kohei <kaigai@kaigai.gr.jp>
[oweals/busybox.git] / selinux / matchpathcon.c
1 /* matchpathcon  -  get the default security context for the specified
2  *                  path from the file contexts configuration.
3  *                  based on libselinux-1.32
4  * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
5  *
6  */
7 #include "busybox.h"
8
9 static int print_matchpathcon(char *path, int noprint)
10 {
11         char *buf;
12         int rc = matchpathcon(path, 0, &buf);
13         if (rc < 0) {
14                 bb_perror_msg("matchpathcon(%s) failed", path);
15                 return 1;
16         }
17         if (!noprint)
18                 printf("%s\t%s\n", path, buf);
19         else
20                 printf("%s\n", buf);
21
22         freecon(buf);
23         return 0;
24 }
25
26 #define OPT_NOT_PRINT   (1<<0)  /* -n */
27 #define OPT_NOT_TRANS   (1<<1)  /* -N */
28 #define OPT_FCONTEXT    (1<<2)  /* -f */
29 #define OPT_PREFIX      (1<<3)  /* -p */
30 #define OPT_VERIFY      (1<<4)  /* -V */
31
32 int matchpathcon_main(int argc, char **argv)
33 {
34         int error = 0;
35         unsigned opts;
36         char *fcontext, *prefix, *path;
37
38         opt_complementary = "-1:" /* at least one param reqd */
39                 "f--p:p--f"; /* mutually exclusive */
40         opts = getopt32(argc, argv, "nNf:p:V", &fcontext, &prefix);
41         argv += optind;
42
43         if (opts & OPT_NOT_TRANS) {
44                 set_matchpathcon_flags(NOTRANS);
45         }
46         if (opts & OPT_FCONTEXT) {
47                 if (matchpathcon_init(fcontext))
48                         bb_perror_msg_and_die("error while processing %s", fcontext);
49         }
50         if (opts & OPT_PREFIX) {
51                 if (matchpathcon_init_prefix(NULL, prefix))
52                         bb_perror_msg_and_die("error while processing %s", prefix);
53         }
54
55         while((path = *argv++) != NULL) {
56                 security_context_t con;
57                 int rc;
58
59                 if (!(opts & OPT_VERIFY)) {
60                         error += print_matchpathcon(path, opt & OPT_NOT_PRINT);
61                         continue;
62                 }
63
64                 if (selinux_file_context_verify(path, 0)) {
65                         printf("%s verified\n", path);
66                         continue;
67                 }
68
69                 if (opts & OPT_NOT_TRANS)
70                         rc = lgetfilecon_raw(path, &con);
71                 else
72                         rc = lgetfilecon(path, &con);
73
74                 if (rc >= 0) {
75                         printf("%s has context %s, should be ", path, con);
76                         error += print_matchpathcon(path, 1);
77                         freecon(con);
78                         continue;
79                 }
80                 printf("actual context unknown: %s, should be ", strerror(errno));
81                 error += print_matchpathcon(path, 1);
82         }
83         matchpathcon_fini();
84         return error;
85 }