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