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