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