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