7227ef99cf0f4486382e01652fb3b151e5a7b6fd
[platform/core/security/cynara.git] / src / service / logic / Logic.cpp
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        src/service/logic/Logic.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @version     1.0
20  * @brief       This file implements main class of logic layer in cynara service
21  */
22
23 #include <csignal>
24 #include <memory>
25
26 #include <common.h>
27 #include <log/log.h>
28 #include <exceptions/BucketNotExistsException.h>
29 #include <exceptions/DatabaseException.h>
30 #include <exceptions/DefaultBucketDeletionException.h>
31 #include <exceptions/DefaultBucketSetNoneException.h>
32 #include <exceptions/InvalidBucketIdException.h>
33 #include <exceptions/PluginErrorException.h>
34 #include <exceptions/PluginNotFoundException.h>
35 #include <exceptions/UnexpectedErrorException.h>
36 #include <request/AdminCheckRequest.h>
37 #include <request/AgentRegisterRequest.h>
38 #include <request/CancelRequest.h>
39 #include <request/CheckRequest.h>
40 #include <request/InsertOrUpdateBucketRequest.h>
41 #include <request/RemoveBucketRequest.h>
42 #include <request/RequestContext.h>
43 #include <request/SetPoliciesRequest.h>
44 #include <request/SignalRequest.h>
45 #include <response/AgentRegisterResponse.h>
46 #include <response/CancelResponse.h>
47 #include <response/CheckResponse.h>
48 #include <response/CodeResponse.h>
49
50 #include <main/Cynara.h>
51 #include <agent/AgentManager.h>
52 #include <sockets/SocketManager.h>
53 #include <storage/Storage.h>
54
55 #include <cynara-plugin.h>
56
57 #include "Logic.h"
58
59 namespace Cynara {
60 Logic::Logic() {
61 }
62
63 Logic::~Logic() {
64 }
65
66 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
67     LOGD("Processing signal: [%d]", request->signalNumber());
68
69     switch (request->signalNumber()) {
70     case SIGTERM:
71         LOGI("SIGTERM received!");
72         m_socketManager->mainLoopStop();
73         break;
74     }
75 }
76
77 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
78     PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
79                                                  request->recursive());
80
81     context->returnResponse(context, std::make_shared<CheckResponse>(result,
82                             request->sequenceNumber()));
83 }
84
85 void Logic::execute(RequestContextPtr context, AgentRegisterRequestPtr request) {
86     auto result = m_agentManager->registerAgent(request->agentType(), context->responseQueue());
87     context->returnResponse(context, std::make_shared<AgentRegisterResponse>(
88                             result, request->sequenceNumber()));
89 }
90
91 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
92     // MOCKUP
93     context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
94 }
95
96 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
97     PolicyResult result(PredefinedPolicyType::DENY);
98     if (check(context, request->key(), result)) {
99         context->returnResponse(context, std::make_shared<CheckResponse>(result,
100                                 request->sequenceNumber()));
101     }
102 }
103
104 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
105                   PolicyResult& result) {
106     result = m_storage->checkPolicy(key);
107
108     switch (result.policyType()) {
109         case PredefinedPolicyType::ALLOW :
110             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
111             return true;
112         case PredefinedPolicyType::DENY :
113             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
114             return true;
115     }
116
117     ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
118     if (!plugin) {
119         throw PluginNotFoundException(result);
120     }
121
122     ServicePluginInterfacePtr servicePlugin =
123             std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
124     if (!plugin) {
125         throw PluginNotFoundException(result);
126     }
127
128     AgentType requiredAgent;
129     PluginData pluginData;
130
131     auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
132                                     key.privilege().toString(), result, requiredAgent, pluginData);
133
134     switch (ret) {
135         case ServicePluginInterface::PluginStatus::ANSWER_READY:
136             return true;
137         case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY:
138             //todo send request to agent
139             //context should be saved in plugin in order to return answer when ready
140             return false;
141         default:
142             throw PluginErrorException(key);
143     }
144 }
145
146 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
147     auto code = CodeResponse::Code::OK;
148
149     try {
150         m_storage->addOrUpdateBucket(request->bucketId(), request->result());
151         onPoliciesChanged();
152     } catch (const DatabaseException &ex) {
153         code = CodeResponse::Code::FAILED;
154     } catch (const DefaultBucketSetNoneException &ex) {
155         code = CodeResponse::Code::NOT_ALLOWED;
156     } catch (const InvalidBucketIdException &ex) {
157         code = CodeResponse::Code::NOT_ALLOWED;
158     }
159
160     context->returnResponse(context, std::make_shared<CodeResponse>(code,
161                             request->sequenceNumber()));
162 }
163
164 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
165     auto code = CodeResponse::Code::OK;
166     try {
167         m_storage->deleteBucket(request->bucketId());
168         onPoliciesChanged();
169     } catch (const DatabaseException &ex) {
170         code = CodeResponse::Code::FAILED;
171     } catch (const BucketNotExistsException &ex) {
172         code = CodeResponse::Code::NO_BUCKET;
173     } catch (const DefaultBucketDeletionException &ex) {
174         code = CodeResponse::Code::NOT_ALLOWED;
175     }
176     context->returnResponse(context, std::make_shared<CodeResponse>(code,
177                             request->sequenceNumber()));
178 }
179
180 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
181     auto code = CodeResponse::Code::OK;
182     try {
183         m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
184         m_storage->deletePolicies(request->policiesToBeRemoved());
185         onPoliciesChanged();
186     } catch (const DatabaseException &ex) {
187         code = CodeResponse::Code::FAILED;
188     } catch (const BucketNotExistsException &ex) {
189         code = CodeResponse::Code::NO_BUCKET;
190     }
191     context->returnResponse(context, std::make_shared<CodeResponse>(code,
192                             request->sequenceNumber()));
193 }
194
195 void Logic::contextClosed(RequestContextPtr context UNUSED) {
196     //We don't care now, but we will
197 }
198
199 void Logic::onPoliciesChanged(void) {
200     m_storage->save();
201     m_socketManager->disconnectAllClients();
202     //todo remove all saved contexts (if there will be any saved contexts)
203 }
204
205 } // namespace Cynara