Add signal processing
[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 <signal.h>
27
28 #include <main/Cynara.h>
29 #include <request/CheckRequest.h>
30 #include <request/RequestContext.h>
31 #include <request/SignalRequest.h>
32 #include <response/CheckResponse.h>
33 #include <storage/Storage.h>
34
35 #include <sockets/SocketManager.h>
36
37 #include "Logic.h"
38
39 namespace Cynara {
40 Logic::Logic() {
41 }
42
43 Logic::~Logic() {
44 }
45
46 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
47     LOGD("Processing signal: [%d]", request->signalNumber());
48
49     switch (request->signalNumber()) {
50     case SIGTERM:
51         LOGI("SIGTERM received!");
52         m_socketManager->mainLoopStop();
53         break;
54     }
55 }
56
57 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
58     PolicyResult result(PredefinedPolicyType::DENY);
59     if (check(context, request->key(), result)) {
60         context->returnResponse(context, std::make_shared<CheckResponse>(result, request->sequenceNumber()));
61     }
62 }
63
64 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
65                   PolicyResult& result) {
66     result = m_storage->checkPolicy(key);
67
68     switch (result.policyType()) {
69         case PredefinedPolicyType::ALLOW :
70             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
71             return true;
72         case PredefinedPolicyType::DENY :
73             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
74             return true;
75     }
76     //todo pass question to proper plugin that:
77     //  1) returns false when answer has to be waited for (UI)
78     //  2) returns true if Response is to be generated
79     // In case 1) context should be saved in plugin in order to return answer when ready.
80
81     //in case no proper plugin is found
82     throw PluginNotFoundException(result);
83     return false;
84 }
85
86 } // namespace Cynara