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) {
96 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
99 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
100 PolicyResult result(PredefinedPolicyType::DENY);
101 if (check(context, request->key(), request->sequenceNumber(), result)) {
102 context->returnResponse(context, std::make_shared<CheckResponse>(result,
103 request->sequenceNumber()));
107 bool Logic::check(const RequestContextPtr &context, const PolicyKey &key,
108 ProtocolFrameSequenceNumber checkId, PolicyResult &result) {
110 if (m_checkRequestManager.getContext(context->responseQueue(), checkId)) {
111 LOGE("Check request for checkId: [%" PRIu16 "] is already processing", checkId);
115 result = m_storage->checkPolicy(key);
117 switch (result.policyType()) {
118 case PredefinedPolicyType::ALLOW :
119 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
121 case PredefinedPolicyType::DENY :
122 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
126 return pluginCheck(context, key, checkId, result);
129 bool Logic::pluginCheck(const RequestContextPtr &context, const PolicyKey &key,
130 ProtocolFrameSequenceNumber checkId, PolicyResult &result) {
132 LOGD("Trying to check policy: <%s> in plugin.", key.toString().c_str());
134 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
136 LOGE("Plugin not found for policy: [0x%x]", result.policyType());
137 result = PolicyResult(PredefinedPolicyType::DENY);
141 ServicePluginInterfacePtr servicePlugin =
142 std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
144 throw PluginNotFoundException(result);
147 AgentType requiredAgent;
148 PluginData pluginData;
150 auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
151 key.privilege().toString(), result, requiredAgent, pluginData);
154 case ServicePluginInterface::PluginStatus::ANSWER_READY:
156 case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY: {
157 result = PolicyResult(PredefinedPolicyType::DENY);
158 AgentTalkerPtr agentTalker = m_agentManager->createTalker(requiredAgent);
160 LOGE("Required agent talker for: <%s> could not be created.",
161 requiredAgent.c_str());
165 if (!m_checkRequestManager.createContext(key, context, checkId, servicePlugin,
167 LOGE("Check context for checkId: [%" PRIu16 "] could not be created.",
169 m_agentManager->removeTalker(agentTalker);
172 agentTalker->send(pluginData);
176 throw PluginErrorException(key); // This 'throw' should be removed or handled properly.
180 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
181 auto code = CodeResponse::Code::OK;
184 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
186 } catch (const DatabaseException &ex) {
187 code = CodeResponse::Code::FAILED;
188 } catch (const DefaultBucketSetNoneException &ex) {
189 code = CodeResponse::Code::NOT_ALLOWED;
190 } catch (const InvalidBucketIdException &ex) {
191 code = CodeResponse::Code::NOT_ALLOWED;
194 context->returnResponse(context, std::make_shared<CodeResponse>(code,
195 request->sequenceNumber()));
198 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
199 auto code = CodeResponse::Code::OK;
201 m_storage->deleteBucket(request->bucketId());
203 } catch (const DatabaseException &ex) {
204 code = CodeResponse::Code::FAILED;
205 } catch (const BucketNotExistsException &ex) {
206 code = CodeResponse::Code::NO_BUCKET;
207 } catch (const DefaultBucketDeletionException &ex) {
208 code = CodeResponse::Code::NOT_ALLOWED;
210 context->returnResponse(context, std::make_shared<CodeResponse>(code,
211 request->sequenceNumber()));
214 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
215 auto code = CodeResponse::Code::OK;
217 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
218 m_storage->deletePolicies(request->policiesToBeRemoved());
220 } catch (const DatabaseException &ex) {
221 code = CodeResponse::Code::FAILED;
222 } catch (const BucketNotExistsException &ex) {
223 code = CodeResponse::Code::NO_BUCKET;
225 context->returnResponse(context, std::make_shared<CodeResponse>(code,
226 request->sequenceNumber()));
229 void Logic::contextClosed(RequestContextPtr context UNUSED) {
230 //We don't care now, but we will
233 void Logic::onPoliciesChanged(void) {
235 m_socketManager->disconnectAllClients();
236 //todo remove all saved contexts (if there will be any saved contexts)
239 } // namespace Cynara