Fix translate EVM state from IMAEVMServer to libimaevm
[platform/core/security/ima-evm-reference-utils.git] / src / service / client / client-ima-evm-server-set-state.cpp
1 /*
2  * Copyright (c) 2000 - 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        client-ima-evm-server-set-state.cpp
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  * @brief       This file contains implementation of
21  *              ima/evm-set-state functions
22  */
23 #include <imaevm.h>
24
25 #include <dpl/log/log.h>
26 #include <dpl/exception.h>
27
28 #include <socket-buffer.h>
29 #include <client-common.h>
30 #include <protocols.h>
31
32 #include <ima-evm-server.h>
33
34 int libimaevmToIMAEVMServiceError (int error)
35 {
36     switch (error) {
37         case LIB_SUCCESS:           return IMA_EVM_SERVER_API_SUCCESS;
38         case LIB_ERROR_UNKNOWN:     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
39         case LIB_ERROR_INPUT_PARAM: return IMA_EVM_SERVER_API_ERROR_INPUT_PARAM;
40         case LIB_ERROR_MEMORY:      return IMA_EVM_SERVER_API_ERROR_OUT_OF_MEMORY;
41         case LIB_ERROR_SYSCALL:     return IMA_EVM_SERVER_API_ERROR_SERVER_ERROR;
42         case LIB_ERROR_ATTRIBUTE:   return IMA_EVM_SERVER_API_ERROR_NO_SUCH_OBJECT;
43         default: return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
44     }   
45 }
46
47
48 IMA_EVM_SERVER_API
49 int ima_evm_server_set_ima_state(int state)
50 {
51     using namespace IMAEVMServer;
52     SocketBuffer send, recv;
53
54     try {
55         //put data into buffer
56         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_STATE));
57         Serialization::Serialize(send, static_cast<int>(IMAEVMSwitch::IMA_SWITCH));
58         Serialization::Serialize(send, state);
59
60         //send buffer
61         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
62         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
63             return apiResult;
64         }
65
66         //receive response from server
67         Deserialization::Deserialize(recv, apiResult);
68         return apiResult;
69
70     } catch (SocketBuffer::Exception::Base &e) {
71         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
72     } catch (std::exception &e) {
73         LogError("STD exception " << e.what());
74     } catch (...) {
75         LogError("Unknown exception occured");
76     }
77
78     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
79 }
80
81 IMA_EVM_SERVER_API
82 int ima_evm_server_set_evm_state(int state)
83 {
84     using namespace IMAEVMServer;
85     SocketBuffer send, recv;
86
87     try {
88         //put data into buffer
89         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_STATE));
90         Serialization::Serialize(send, static_cast<int>(IMAEVMSwitch::EVM_SWITCH));
91         Serialization::Serialize(send, state);
92
93         //send buffer
94         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
95         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
96             return apiResult;
97         }   
98
99         //receive response from server
100         Deserialization::Deserialize(recv, apiResult);
101         return apiResult;
102
103     } catch (SocketBuffer::Exception::Base &e) {
104         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
105     } catch (std::exception &e) {
106         LogError("STD exception " << e.what());
107     } catch (...) {
108         LogError("Unknown exception occured");
109     }   
110
111     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
112 }
113
114 IMA_EVM_SERVER_API
115 int ima_evm_server_get_ima_state(int *state)
116 {
117     using namespace IMAEVMServer;
118     SocketBuffer send, recv;
119
120     try {
121         //put data into buffer
122         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::GET_STATE));
123         Serialization::Serialize(send, static_cast<int>(IMAEVMSwitch::IMA_SWITCH));
124
125         //send buffer
126         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
127         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
128             return apiResult;
129         }   
130
131         //receive response from server
132         Deserialization::Deserialize(recv, apiResult);
133         if (apiResult == IMA_EVM_SERVER_API_SUCCESS) {
134             Deserialization::Deserialize(recv, *state);
135         }   
136         return apiResult;
137
138     } catch (SocketBuffer::Exception::Base &e) {
139         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
140     } catch (std::exception &e) {
141         LogError("STD exception " << e.what());
142     } catch (...) {
143         LogError("Unknown exception occured");
144     }   
145
146     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
147 }
148
149 IMA_EVM_SERVER_API
150 int ima_evm_server_get_evm_state(int *state)
151 {
152     using namespace IMAEVMServer;
153     SocketBuffer send, recv;
154
155     try {
156         //put data into buffer
157         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::GET_STATE));
158         Serialization::Serialize(send, static_cast<int>(IMAEVMSwitch::EVM_SWITCH));
159
160         //send buffer
161         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
162         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
163             return apiResult;
164         }
165
166         //receive response from server
167         Deserialization::Deserialize(recv, apiResult);
168         if (apiResult == IMA_EVM_SERVER_API_SUCCESS) {
169             Deserialization::Deserialize(recv, *state);
170         }
171         return apiResult;
172
173     } catch (SocketBuffer::Exception::Base &e) {
174         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
175     } catch (std::exception &e) {
176         LogError("STD exception " << e.what());
177     } catch (...) {
178         LogError("Unknown exception occured");
179     }
180
181     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
182 }
183
184 IMA_EVM_SERVER_API
185 int ima_evm_server_set_policy(const char **policy, const char *policy_sig)
186 {
187     using namespace IMAEVMServer;
188     int rules_count = 0;
189     SocketBuffer send, recv;
190
191     try {
192         //put data into buffer
193         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_POLICY));
194
195         while(policy[rules_count])
196             ++rules_count;
197         Serialization::Serialize(send, static_cast<int>(rules_count));
198
199         for (int i=0; i<rules_count; ++i)
200             Serialization::Serialize(send, std::string(policy[i]));
201
202         Serialization::Serialize(send, std::string(policy_sig));
203
204         //send buffer
205         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
206         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
207             return apiResult;
208         }   
209
210         //receive response from server
211         Deserialization::Deserialize(recv, apiResult);
212         return apiResult;
213
214     } catch (SocketBuffer::Exception::Base &e) {
215         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
216     } catch (std::exception &e) {
217         LogError("STD exception " << e.what());
218     } catch (...) {
219         LogError("Unknown exception occured");
220     }   
221
222     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
223 }
224
225 IMA_EVM_SERVER_API
226 int ima_evm_server_set_policy_file(const char *policy_path)
227 {
228     using namespace IMAEVMServer;
229     SocketBuffer send, recv;
230
231     try {
232         //put data into buffer
233         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_POLICY_FILE));
234
235         Serialization::Serialize(send, std::string(policy_path));
236
237         //send buffer
238         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
239         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
240             return apiResult;
241         }   
242
243         //receive response from server
244         Deserialization::Deserialize(recv, apiResult);
245         return apiResult;
246
247     } catch (SocketBuffer::Exception::Base &e) {
248         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
249     } catch (std::exception &e) {
250         LogError("STD exception " << e.what());
251     } catch (...) {
252         LogError("Unknown exception occured");
253     }   
254
255     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
256 }
257
258 IMA_EVM_SERVER_API
259 int ima_evm_server_get_policy(char ***policy)
260 {
261     using namespace IMAEVMServer;
262     int rules_count = 0;
263     std::string rule;
264     SocketBuffer send, recv;
265
266     printf("ima_evm_server_get_policy\n");
267     try {
268         //put data into buffer
269         Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::GET_POLICY));
270
271         //send buffer
272         int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
273         if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
274             return apiResult;
275         }   
276
277         //receive response from server
278         Deserialization::Deserialize(recv, apiResult);
279         if (apiResult != IMA_EVM_SERVER_API_SUCCESS)
280             return apiResult;
281
282         Deserialization::Deserialize(recv, rules_count);
283         *policy = (char**) malloc((rules_count + 1) * sizeof(char*));
284         if (!policy)
285             return IMA_EVM_SERVER_API_ERROR_OUT_OF_MEMORY;
286
287         for (int i=0; i<rules_count; ++i) {
288             Deserialization::Deserialize(recv, rule);
289             (*policy)[i] = (char *) malloc((rule.size() + 1 ) * sizeof(char));
290             // FIXME: add malloc error handling
291             std::copy(rule.begin(), rule.end(), (*policy)[i]);
292             (*policy)[i][rule.size()] = '\0';
293         }
294         (*policy)[rules_count] = NULL;
295         return apiResult;
296
297     } catch (SocketBuffer::Exception::Base &e) {
298         LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
299     } catch (std::exception &e) {
300         LogError("STD exception " << e.what());
301     } catch (...) {
302         LogError("Unknown exception occured");
303     }   
304
305     return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
306 }
307
308 IMA_EVM_SERVER_API
309 int ima_evm_server_free_policy(char **policy)
310 {
311     return libimaevmToIMAEVMServiceError(ima_free_policy(policy));
312 }
313