2 * Copyright (c) 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 src/service/logic/Logic.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20 * @brief This file implements main class of logic layer in cynara service
31 #include <exceptions/BucketNotExistsException.h>
32 #include <exceptions/DatabaseException.h>
33 #include <exceptions/DefaultBucketDeletionException.h>
34 #include <exceptions/DefaultBucketSetNoneException.h>
35 #include <exceptions/InvalidBucketIdException.h>
36 #include <exceptions/PluginErrorException.h>
37 #include <exceptions/PluginNotFoundException.h>
38 #include <exceptions/UnexpectedErrorException.h>
39 #include <request/AdminCheckRequest.h>
40 #include <request/AgentRegisterRequest.h>
41 #include <request/CancelRequest.h>
42 #include <request/CheckRequest.h>
43 #include <request/InsertOrUpdateBucketRequest.h>
44 #include <request/RemoveBucketRequest.h>
45 #include <request/RequestContext.h>
46 #include <request/SetPoliciesRequest.h>
47 #include <request/SignalRequest.h>
48 #include <response/AgentRegisterResponse.h>
49 #include <response/CancelResponse.h>
50 #include <response/CheckResponse.h>
51 #include <response/CodeResponse.h>
53 #include <main/Cynara.h>
54 #include <agent/AgentManager.h>
55 #include <sockets/SocketManager.h>
56 #include <storage/Storage.h>
58 #include <cynara-plugin.h>
69 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
70 LOGD("Processing signal: [%d]", request->signalNumber());
72 switch (request->signalNumber()) {
74 LOGI("SIGTERM received!");
75 m_socketManager->mainLoopStop();
80 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
81 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
82 request->recursive());
84 context->returnResponse(context, std::make_shared<CheckResponse>(result,
85 request->sequenceNumber()));
88 void Logic::execute(RequestContextPtr context, AgentRegisterRequestPtr request) {
89 auto result = m_agentManager->registerAgent(request->agentType(), context->responseQueue());
90 context->returnResponse(context, std::make_shared<AgentRegisterResponse>(
91 result, request->sequenceNumber()));
94 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
95 CheckContextPtr checkContextPtr = m_checkRequestManager.getContext(context->responseQueue(),
96 request->sequenceNumber());
97 if (!checkContextPtr) {
98 LOGD("Cancel request id: [%" PRIu16 "] with no matching request in progress.",
99 request->sequenceNumber());
103 if (checkContextPtr->cancelled())
106 checkContextPtr->cancel();
107 checkContextPtr->m_agentTalker->cancel();
109 LOGD("Returning response for cancel request id: [%" PRIu16 "].", request->sequenceNumber());
110 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
113 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
114 PolicyResult result(PredefinedPolicyType::DENY);
115 if (check(context, request->key(), request->sequenceNumber(), result)) {
116 context->returnResponse(context, std::make_shared<CheckResponse>(result,
117 request->sequenceNumber()));
121 bool Logic::check(const RequestContextPtr &context, const PolicyKey &key,
122 ProtocolFrameSequenceNumber checkId, PolicyResult &result) {
124 if (m_checkRequestManager.getContext(context->responseQueue(), checkId)) {
125 LOGE("Check request for checkId: [%" PRIu16 "] is already processing", checkId);
129 result = m_storage->checkPolicy(key);
131 switch (result.policyType()) {
132 case PredefinedPolicyType::ALLOW :
133 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
135 case PredefinedPolicyType::DENY :
136 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
140 return pluginCheck(context, key, checkId, result);
143 bool Logic::pluginCheck(const RequestContextPtr &context, const PolicyKey &key,
144 ProtocolFrameSequenceNumber checkId, PolicyResult &result) {
146 LOGD("Trying to check policy: <%s> in plugin.", key.toString().c_str());
148 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
150 LOGE("Plugin not found for policy: [0x%x]", result.policyType());
151 result = PolicyResult(PredefinedPolicyType::DENY);
155 ServicePluginInterfacePtr servicePlugin =
156 std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
158 throw PluginNotFoundException(result);
161 AgentType requiredAgent;
162 PluginData pluginData;
164 auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
165 key.privilege().toString(), result, requiredAgent, pluginData);
168 case ServicePluginInterface::PluginStatus::ANSWER_READY:
170 case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY: {
171 result = PolicyResult(PredefinedPolicyType::DENY);
172 AgentTalkerPtr agentTalker = m_agentManager->createTalker(requiredAgent);
174 LOGE("Required agent talker for: <%s> could not be created.",
175 requiredAgent.c_str());
179 if (!m_checkRequestManager.createContext(key, context, checkId, servicePlugin,
181 LOGE("Check context for checkId: [%" PRIu16 "] could not be created.",
183 m_agentManager->removeTalker(agentTalker);
186 agentTalker->send(pluginData);
190 throw PluginErrorException(key); // This 'throw' should be removed or handled properly.
194 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
195 auto code = CodeResponse::Code::OK;
198 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
200 } catch (const DatabaseException &ex) {
201 code = CodeResponse::Code::FAILED;
202 } catch (const DefaultBucketSetNoneException &ex) {
203 code = CodeResponse::Code::NOT_ALLOWED;
204 } catch (const InvalidBucketIdException &ex) {
205 code = CodeResponse::Code::NOT_ALLOWED;
208 context->returnResponse(context, std::make_shared<CodeResponse>(code,
209 request->sequenceNumber()));
212 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
213 auto code = CodeResponse::Code::OK;
215 m_storage->deleteBucket(request->bucketId());
217 } catch (const DatabaseException &ex) {
218 code = CodeResponse::Code::FAILED;
219 } catch (const BucketNotExistsException &ex) {
220 code = CodeResponse::Code::NO_BUCKET;
221 } catch (const DefaultBucketDeletionException &ex) {
222 code = CodeResponse::Code::NOT_ALLOWED;
224 context->returnResponse(context, std::make_shared<CodeResponse>(code,
225 request->sequenceNumber()));
228 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
229 auto code = CodeResponse::Code::OK;
231 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
232 m_storage->deletePolicies(request->policiesToBeRemoved());
234 } catch (const DatabaseException &ex) {
235 code = CodeResponse::Code::FAILED;
236 } catch (const BucketNotExistsException &ex) {
237 code = CodeResponse::Code::NO_BUCKET;
239 context->returnResponse(context, std::make_shared<CodeResponse>(code,
240 request->sequenceNumber()));
243 void Logic::contextClosed(RequestContextPtr context UNUSED) {
244 //We don't care now, but we will
247 void Logic::onPoliciesChanged(void) {
249 m_socketManager->disconnectAllClients();
250 //todo remove all saved contexts (if there will be any saved contexts)
253 } // namespace Cynara