2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file client-ima-evm-server-set-state.cpp
18 * @author Janusz Kozerski (j.kozerski@samsung.com)
20 * @brief This file contains implementation of
21 * ima/evm-set-state functions
25 #include <dpl/log/log.h>
26 #include <dpl/exception.h>
28 #include <socket-buffer.h>
29 #include <client-common.h>
30 #include <protocols.h>
32 #include <ima-evm-server.h>
34 int libimaevmToIMAEVMServiceError (int 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;
49 int ima_evm_server_set_ima_state(int state)
51 using namespace IMAEVMServer;
52 SocketBuffer send, recv;
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);
61 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
62 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
66 //receive response from server
67 Deserialization::Deserialize(recv, apiResult);
70 } catch (SocketBuffer::Exception::Base &e) {
71 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
72 } catch (std::exception &e) {
73 LogError("STD exception " << e.what());
75 LogError("Unknown exception occured");
78 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
82 int ima_evm_server_set_evm_state(int state)
84 using namespace IMAEVMServer;
85 SocketBuffer send, recv;
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);
94 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
95 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
99 //receive response from server
100 Deserialization::Deserialize(recv, apiResult);
103 } catch (SocketBuffer::Exception::Base &e) {
104 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
105 } catch (std::exception &e) {
106 LogError("STD exception " << e.what());
108 LogError("Unknown exception occured");
111 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
115 int ima_evm_server_get_ima_state(int *state)
117 using namespace IMAEVMServer;
118 SocketBuffer send, recv;
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));
126 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
127 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
131 //receive response from server
132 Deserialization::Deserialize(recv, apiResult);
133 if (apiResult == IMA_EVM_SERVER_API_SUCCESS) {
134 Deserialization::Deserialize(recv, *state);
138 } catch (SocketBuffer::Exception::Base &e) {
139 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
140 } catch (std::exception &e) {
141 LogError("STD exception " << e.what());
143 LogError("Unknown exception occured");
146 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
150 int ima_evm_server_get_evm_state(int *state)
152 using namespace IMAEVMServer;
153 SocketBuffer send, recv;
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));
161 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
162 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
166 //receive response from server
167 Deserialization::Deserialize(recv, apiResult);
168 if (apiResult == IMA_EVM_SERVER_API_SUCCESS) {
169 Deserialization::Deserialize(recv, *state);
173 } catch (SocketBuffer::Exception::Base &e) {
174 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
175 } catch (std::exception &e) {
176 LogError("STD exception " << e.what());
178 LogError("Unknown exception occured");
181 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
185 int ima_evm_server_set_policy(const char **policy, const char *policy_sig)
187 using namespace IMAEVMServer;
189 SocketBuffer send, recv;
192 //put data into buffer
193 Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_POLICY));
195 while(policy[rules_count])
197 Serialization::Serialize(send, static_cast<int>(rules_count));
199 for (int i=0; i<rules_count; ++i)
200 Serialization::Serialize(send, std::string(policy[i]));
202 Serialization::Serialize(send, std::string(policy_sig));
205 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
206 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
210 //receive response from server
211 Deserialization::Deserialize(recv, apiResult);
214 } catch (SocketBuffer::Exception::Base &e) {
215 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
216 } catch (std::exception &e) {
217 LogError("STD exception " << e.what());
219 LogError("Unknown exception occured");
222 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
226 int ima_evm_server_set_policy_file(const char *policy_path)
228 using namespace IMAEVMServer;
229 SocketBuffer send, recv;
232 //put data into buffer
233 Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::SET_POLICY_FILE));
235 Serialization::Serialize(send, std::string(policy_path));
238 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
239 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
243 //receive response from server
244 Deserialization::Deserialize(recv, apiResult);
247 } catch (SocketBuffer::Exception::Base &e) {
248 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
249 } catch (std::exception &e) {
250 LogError("STD exception " << e.what());
252 LogError("Unknown exception occured");
255 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
259 int ima_evm_server_get_policy(char ***policy)
261 using namespace IMAEVMServer;
264 SocketBuffer send, recv;
266 printf("ima_evm_server_get_policy\n");
268 //put data into buffer
269 Serialization::Serialize(send, static_cast<int>(IMAEVMFuncHdrs::GET_POLICY));
272 int apiResult = sendToServer(SERVICE_SOCKET_IMA_EVM, send.Pop(), recv);
273 if (apiResult != IMA_EVM_SERVER_API_SUCCESS) {
277 //receive response from server
278 Deserialization::Deserialize(recv, apiResult);
279 if (apiResult != IMA_EVM_SERVER_API_SUCCESS)
282 Deserialization::Deserialize(recv, rules_count);
283 *policy = (char**) malloc((rules_count + 1) * sizeof(char*));
285 return IMA_EVM_SERVER_API_ERROR_OUT_OF_MEMORY;
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';
294 (*policy)[rules_count] = NULL;
297 } catch (SocketBuffer::Exception::Base &e) {
298 LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString());
299 } catch (std::exception &e) {
300 LogError("STD exception " << e.what());
302 LogError("Unknown exception occured");
305 return IMA_EVM_SERVER_API_ERROR_UNKNOWN;
309 int ima_evm_server_free_policy(char **policy)
311 return libimaevmToIMAEVMServiceError(ima_free_policy(policy));