Add console tool (UI substitute)
[platform/core/security/ima-evm-reference-utils.git] / src / console / im-console.c
1 /**
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        im-console.c
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22
23 #include <ima-evm-server.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 void inval()
30 {
31     printf("Invalid param.\n");
32 }
33
34 void few_args()
35 {
36     printf("Too few arguments\n");
37 }
38
39 void error(int i)
40 {
41     printf("Error occurced %d\n", i);
42 }
43
44 void print_policy(const char** policy)
45 {
46     int i = 0;
47     while (policy[i]) {
48         printf("%s\n", policy[i]);
49         ++i;
50     }
51 }
52
53 void help ()
54 {
55     printf("Usage:\n");
56     printf("  im-console option\n");
57     printf("Options:\n");
58     printf("  -h\t\t\tPrint this message\n");
59     printf("  -s ima|evm state\tSet state IMA or EVM\n");
60     printf("  \t\t\tThe state of IMA can be one of: dis|enf|ign|fix\n");
61     printf("  \t\t\tThe state of EVM can be one of: dis|ena|fix\n");
62     printf("  -g ima|evm\t\tGet state of IMA or EVM\n");
63     printf("  -p\t\t\tGet policy from the kernel\n");
64     printf("  -l file\t\tLoad policy from the file into the kernel\n");
65     printf("  \t\t\tThe signature of the policy should be present in location file.sig\n");
66 }
67
68 int ima_state_to_server(const char* state)
69 {
70     if (strcmp(state, "dis") == 0)
71         return IMA_SERVER_API_STATE_DISABLED;
72     else if (strcmp(state, "enf") == 0)
73         return IMA_SERVER_API_STATE_ENFORCE;
74     else if (strcmp(state, "ign") == 0)
75         return IMA_SERVER_API_STATE_IGNORE;
76     else if (strcmp(state, "fix") == 0)
77         return IMA_SERVER_API_STATE_FIX;
78     else {
79         inval();
80         exit(0);
81     }
82 }
83
84 int evm_state_to_server(const char* state)
85 {
86     if (strcmp(state, "dis") == 0)
87         return EVM_SERVER_API_STATE_DISABLED;
88     else if (strcmp(state, "ena") == 0)
89         return EVM_SERVER_API_STATE_ENABLED;
90     else if (strcmp(state, "fix") == 0)
91         return EVM_SERVER_API_STATE_FIX;
92     else {
93         inval();
94         exit(0);
95     }
96 }
97
98 const char* ima_state_from_server(int state)
99 {
100     switch (state) {
101     case IMA_SERVER_API_STATE_DISABLED: return "DISABLED";
102     case IMA_SERVER_API_STATE_IGNORE:   return "IGNORE";
103     case IMA_SERVER_API_STATE_ENFORCE:  return "ENFORCE";
104     case IMA_SERVER_API_STATE_FIX:      return "FIX";
105     default:                            return "UNKNOWN";
106     }
107 }
108
109 const char* evm_state_from_server(int state)
110 {
111     switch (state) {
112     case EVM_SERVER_API_STATE_DISABLED: return "DISABLED";
113     case EVM_SERVER_API_STATE_ENABLED:  return "ENABLED";
114     case EVM_SERVER_API_STATE_FIX:      return "FIX";
115     default:                            return "UNKNOWN";
116     }
117 }
118
119 int is_ima_module(const char* arg)
120 {
121     if (strcmp("ima", arg) == 0 || strcmp("IMA", arg) == 0)
122         return 1;
123     return 0;
124 }
125
126 int main (int argc, const char* argv[])
127 {
128     int i = 1;
129     int ret;
130     int state;
131     char** policy;
132
133     while (i<argc) {
134         if (argv[i][0] != '-'){
135             inval();
136             return 0;
137         }
138         switch (argv[i][1]) {
139         case 'h': help(); return 0;
140         case 's':
141             ++i;
142             if (i+1>=argc) {
143                 few_args();
144                 return 0;
145             }
146             if (is_ima_module(argv[i]))
147                 ret = ima_evm_server_set_ima_state(ima_state_to_server(argv[i+1]));
148             else
149                 ret = ima_evm_server_set_evm_state(evm_state_to_server(argv[i+1]));
150             i++;
151             if (ret == IMA_EVM_SERVER_API_SUCCESS)
152                 printf("State changed\n");
153             else
154                 error(ret);
155             return 0;
156         case 'g': 
157             i++;
158             if (i>=argc) {
159                 few_args();
160                 return 0;
161             }
162             if (is_ima_module(argv[i])) {
163                 ret = ima_evm_server_get_ima_state(&state);
164                 if (ret == IMA_EVM_SERVER_API_SUCCESS)
165                     printf("%s\n", ima_state_from_server(state));
166             } else {
167                 ret = ima_evm_server_get_evm_state(&state);
168                 if (ret == IMA_EVM_SERVER_API_SUCCESS)
169                     printf("%s\n", evm_state_from_server(state));
170             }
171             if (ret != IMA_EVM_SERVER_API_SUCCESS)
172                 error(ret);
173             return 0;              
174         case 'p':
175             ret = ima_evm_server_get_policy(&policy);
176             if (ret != IMA_EVM_SERVER_API_SUCCESS) {
177                 error(ret);
178                 return 0;
179             }
180             print_policy(policy);
181         case 'l': break;
182         default: inval(); return 0;
183         }
184     ++i;
185     }
186 out:
187     return 0;
188 }
189