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