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