source code open - smack
[framework/security/smack.git] / utils / smackctl.c
1 /*
2  * This file is part of libsmack.
3  *
4  * Copyright (C) 2011 Intel Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * version 2.1 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  * Authors:
21  * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
22  * Brian McGillion <brian.mcgillion@intel.com>
23  */
24
25 #include "common.h"
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29
30 static int apply_all(void)
31 {
32         if (is_smackfs_mounted() != 1) {
33                 fprintf(stderr, "ERROR: SmackFS is not mounted.\n");
34                 return -1;
35         }
36
37         if (clear())
38                 return -1;
39
40         if (apply_rules(ACCESSES_D_PATH, 0))
41                 perror("apply_rules");
42
43         if (apply_cipso(CIPSO_D_PATH))
44                 perror("apply_cipso");
45
46         return 0;
47 }
48
49 static int status(void)
50 {
51         int ret = is_smackfs_mounted();
52
53         switch (ret) {
54         case 1:
55                 printf("SmackFS is mounted.\n");
56                 return 0;
57         case 0:
58                 printf("SmackFS is not mounted.\n");
59                 return 0;
60         default:
61                 return -1;
62         }
63 }
64
65 int main(int argc, char **argv)
66 {
67         if (argc < 2) {
68                 fprintf(stderr, "Usage: %s <action>\n", argv[0]);
69                 return 1;
70         }
71
72         if (!strcmp(argv[1], "apply")) {
73                 if (apply_all())
74                         return 1;
75         } else if (!strcmp(argv[1], "clear")) {
76                 if (clear())
77                         return 1;
78         } else if (!strcmp(argv[1], "status")) {
79                 if (status())
80                         return 1;
81         } else {
82                 fprintf(stderr, "Uknown action: %s\n", argv[1]);
83                 return 1;
84         }
85
86         return 0;
87 }