Add adminCheck() method prototype in admin Logic
[platform/core/security/cynara.git] / src / admin / 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 contains implementation of Logic class - main libcynara-admin class
21  */
22
23 #include <memory>
24
25 #include <cynara-admin-error.h>
26 #include <common.h>
27 #include <exceptions/ServerConnectionErrorException.h>
28 #include <log/log.h>
29 #include <protocol/Protocol.h>
30 #include <protocol/ProtocolAdmin.h>
31 #include <request/InsertOrUpdateBucketRequest.h>
32 #include <request/pointers.h>
33 #include <request/RemoveBucketRequest.h>
34 #include <request/SetPoliciesRequest.h>
35 #include <response/CodeResponse.h>
36 #include <response/pointers.h>
37 #include <sockets/SocketClient.h>
38 #include <types/ProtocolFields.h>
39
40 #include "Logic.h"
41
42 namespace Cynara {
43
44 const std::string adminSocketPath("/run/cynara/cynara-admin.socket");
45
46 Logic::Logic() {
47     m_socketClient = std::make_shared<SocketClient>(adminSocketPath,
48                                                     std::make_shared<ProtocolAdmin>());
49 }
50
51 ProtocolFrameSequenceNumber generateSequenceNumber(void) {
52     static ProtocolFrameSequenceNumber sequenceNumber = 0;
53     return ++sequenceNumber;
54 }
55
56 template<typename T, typename... Args>
57 int Logic::askCynaraAndInterpreteCodeResponse(Args... args) {
58     ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber();
59
60     //Ask cynara service
61     CodeResponsePtr codeResponse;
62     try {
63         RequestPtr request = std::make_shared<T>(args..., sequenceNumber);
64         ResponsePtr response = m_socketClient->askCynaraServer(request);
65         if (!response) {
66             LOGW("Disconnected by cynara server.");
67             return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE;
68         }
69         codeResponse = std::dynamic_pointer_cast<CodeResponse>(response);
70         if (!codeResponse) {
71             LOGC("Critical error. Casting Response to CodeResponse failed.");
72             return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
73         }
74
75         LOGD("codeResponse: code = %d", static_cast<int>(codeResponse->m_code));
76         switch (codeResponse->m_code) {
77             case CodeResponse::Code::OK:
78                 LOGI("Policies set successfully.");
79                 return CYNARA_ADMIN_API_SUCCESS;
80             case CodeResponse::Code::NOT_ALLOWED:
81                 LOGE("Cynara service answered: Operation not allowed.");
82                 return CYNARA_ADMIN_API_OPERATION_NOT_ALLOWED;
83             case CodeResponse::Code::NO_BUCKET:
84                 LOGE("Trying to use unexisting bucket.");
85                 return CYNARA_ADMIN_API_BUCKET_NOT_FOUND;
86             default:
87                 LOGE("Unexpected response code from server: %d",
88                      static_cast<int>(codeResponse->m_code));
89                 return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
90         }
91     } catch (const ServerConnectionErrorException &ex) {
92         LOGE("Cynara service not available.");
93         return CYNARA_ADMIN_API_SERVICE_NOT_AVAILABLE;
94     } catch (const std::bad_alloc &ex) {
95         LOGE("Cynara admin client out of memory.");
96         return CYNARA_ADMIN_API_OUT_OF_MEMORY;
97     } catch (const std::exception &ex) {
98         LOGE("Unexpected client error: %s", ex.what());
99         return CYNARA_ADMIN_API_UNEXPECTED_CLIENT_ERROR;
100     }
101 }
102
103 int Logic::setPolicies(const std::map<PolicyBucketId, std::vector<Policy>> &insertOrUpdate,
104                 const std::map<PolicyBucketId, std::vector<PolicyKey>> &remove) noexcept {
105     return askCynaraAndInterpreteCodeResponse<SetPoliciesRequest>(insertOrUpdate, remove);
106 }
107
108 int Logic::insertOrUpdateBucket(const PolicyBucketId &bucket,
109                                 const PolicyResult &policyResult) noexcept {
110     return askCynaraAndInterpreteCodeResponse<InsertOrUpdateBucketRequest>(bucket, policyResult);
111 }
112
113 int Logic::removeBucket(const PolicyBucketId &bucket) noexcept {
114     return askCynaraAndInterpreteCodeResponse<RemoveBucketRequest>(bucket);
115 }
116
117 int Logic::adminCheck(const PolicyBucketId &startBucket UNUSED, bool recursive UNUSED,
118                       const PolicyKey &key UNUSED, PolicyResult &result UNUSED) noexcept {
119     //just mock-up
120     return CYNARA_ADMIN_API_SUCCESS;
121 }
122
123 } // namespace Cynara