Disconnect all clients from cynara when policy rules change
[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        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 <signal.h>
29
30 #include <main/Cynara.h>
31 #include <request/CheckRequest.h>
32 #include <request/InsertOrUpdateBucketRequest.h>
33 #include <request/RemoveBucketRequest.h>
34 #include <request/RequestContext.h>
35 #include <request/SetPoliciesRequest.h>
36 #include <request/SignalRequest.h>
37 #include <response/CheckResponse.h>
38 #include <response/CodeResponse.h>
39 #include <storage/Storage.h>
40
41 #include <sockets/SocketManager.h>
42
43 #include "Logic.h"
44
45 namespace Cynara {
46 Logic::Logic() {
47 }
48
49 Logic::~Logic() {
50 }
51
52 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
53     LOGD("Processing signal: [%d]", request->signalNumber());
54
55     switch (request->signalNumber()) {
56     case SIGTERM:
57         LOGI("SIGTERM received!");
58         m_socketManager->mainLoopStop();
59         break;
60     }
61 }
62
63 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
64     PolicyResult result(PredefinedPolicyType::DENY);
65     if (check(context, request->key(), result)) {
66         context->returnResponse(context, std::make_shared<CheckResponse>(result,
67                                 request->sequenceNumber()));
68     }
69 }
70
71 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
72                   PolicyResult& result) {
73     result = m_storage->checkPolicy(key);
74
75     switch (result.policyType()) {
76         case PredefinedPolicyType::ALLOW :
77             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
78             return true;
79         case PredefinedPolicyType::DENY :
80             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
81             return true;
82     }
83     //todo pass question to proper plugin that:
84     //  1) returns false when answer has to be waited for (UI)
85     //  2) returns true if Response is to be generated
86     // In case 1) context should be saved in plugin in order to return answer when ready.
87
88     //in case no proper plugin is found
89     throw PluginNotFoundException(result);
90 }
91
92 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
93     m_storage->addOrUpdateBucket(request->bucketId(), request->result());
94     onPoliciesChanged();
95
96     context->returnResponse(context, std::make_shared<CodeResponse>(CodeResponse::Code::OK,
97                             request->sequenceNumber()));
98 }
99
100 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
101     auto code = CodeResponse::Code::OK;
102     try {
103         m_storage->deleteBucket(request->bucketId());
104         onPoliciesChanged();
105     } catch (const BucketNotExistsException &ex) {
106         code = CodeResponse::Code::NO_BUCKET;
107     } catch (const DefaultBucketDeletionException &ex) {
108         code = CodeResponse::Code::NOT_ALLOWED;
109     }
110     context->returnResponse(context, std::make_shared<CodeResponse>(code,
111                             request->sequenceNumber()));
112 }
113
114 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
115     auto code = CodeResponse::Code::OK;
116     try {
117         m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
118         m_storage->deletePolicies(request->policiesToBeRemoved());
119         onPoliciesChanged();
120     } catch (const BucketNotExistsException &ex) {
121         code = CodeResponse::Code::NO_BUCKET;
122     }
123     context->returnResponse(context, std::make_shared<CodeResponse>(code,
124                             request->sequenceNumber()));
125 }
126
127 void Logic::onPoliciesChanged(void) {
128     m_storage->save();
129     m_socketManager->disconnectAllClients();
130     //todo remove all saved contexts (if there will be any saved contexts)
131 }
132
133 } // namespace Cynara