82b6291af3e664c6003be9b5341911973c821b2a
[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 Path");
42
43         if (apply_cipso(CIPSO_D_PATH))
44                 perror("apply_cipso Path");
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         int a;
68
69         if (argc < 2) {
70                 fprintf(stderr, "Usage: %s <action>\n", argv[0]);
71                 return 1;
72         }
73
74         if (!strcmp(argv[1], "apply")) {
75                 if (apply_all())
76                         return 1;
77         } else if (!strcmp(argv[1], "clear")) {
78                 if (clear())
79                         return 1;
80         } else if (!strcmp(argv[1], "status")) {
81                 if (status())
82                         return 1;
83         } else {
84                 fprintf(stderr, "Uknown action: %s\n", argv[1]);
85                 return 1;
86         }
87
88         return 0;
89 }