2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file src/client/logic/Logic.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @author Zofia Abramowska <z.abramowska@samsung.com>
21 * @brief This file contains implementation of Logic class - main libcynara-client class
26 #include <cache/CapacityCache.h>
28 #include <config/PathConfig.h>
29 #include <cynara-error.h>
30 #include <exceptions/Exception.h>
31 #include <exceptions/UnexpectedErrorException.h>
33 #include <plugins/NaiveInterpreter.h>
34 #include <protocol/Protocol.h>
35 #include <protocol/ProtocolClient.h>
36 #include <request/CheckRequest.h>
37 #include <request/pointers.h>
38 #include <request/SimpleCheckRequest.h>
39 #include <response/CheckResponse.h>
40 #include <response/pointers.h>
41 #include <response/SimpleCheckResponse.h>
42 #include <sockets/SocketClient.h>
44 #include <logic/Logic.h>
48 static ProtocolFrameSequenceNumber generateSequenceNumber(void) {
49 static ProtocolFrameSequenceNumber sequenceNumber = 0;
50 return ++sequenceNumber;
53 Logic::Logic(const Configuration &conf) :
54 m_socketClient(PathConfig::SocketPath::client, std::make_shared<ProtocolClient>()),
55 m_cache(conf.getCacheSize()) {
56 auto naiveInterpreter = std::make_shared<NaiveInterpreter>();
57 for (auto &descr : naiveInterpreter->getSupportedPolicyDescr()) {
58 m_cache.registerPlugin(descr, naiveInterpreter);
62 int Logic::check(const std::string &client, const ClientSession &session, const std::string &user,
63 const std::string &privilege) {
64 if (!ensureConnection())
65 return CYNARA_API_SERVICE_NOT_AVAILABLE;
67 PolicyKey key(client, user, privilege);
68 int ret = m_cache.get(session, key);
69 if (ret != CYNARA_API_CACHE_MISS) {
74 ret = requestResult(key, result);
75 if (ret != CYNARA_API_SUCCESS) {
76 LOGE("Error fetching new entry.");
80 return m_cache.update(session, key, result);
83 int Logic::simpleCheck(const std::string &client, const ClientSession &session,
84 const std::string &user, const std::string &privilege) {
85 if (!ensureConnection())
86 return CYNARA_API_SERVICE_NOT_AVAILABLE;
88 PolicyKey key(client, user, privilege);
89 int ret = m_cache.get(session, key);
90 if (ret != CYNARA_API_CACHE_MISS) {
95 ret = requestSimpleResult(key, result);
96 if (ret != CYNARA_API_SUCCESS) {
97 if (ret != CYNARA_API_ACCESS_NOT_RESOLVED)
98 LOGE("Error fetching response for simpleCheck.");
102 return m_cache.update(session, key, result);
105 bool Logic::ensureConnection(void) {
106 if (m_socketClient.isConnected())
109 if (m_socketClient.connect())
111 LOGW("Cannot connect to cynara. Service not available.");
115 template <typename Req, typename Res>
116 std::shared_ptr<Res> Logic::requestResponse(const PolicyKey &key) {
117 ProtocolFrameSequenceNumber sequenceNumber = generateSequenceNumber();
120 std::shared_ptr<Res> reqResponse;
121 Req request(key, sequenceNumber);
122 ResponsePtr response;
123 while (!(response = m_socketClient.askCynaraServer(request))) {
125 if (!m_socketClient.connect())
129 reqResponse = std::dynamic_pointer_cast<Res>(response);
133 int Logic::requestResult(const PolicyKey &key, PolicyResult &result) {
134 auto checkResponse = requestResponse<CheckRequest, CheckResponse>(key);
135 if (!checkResponse) {
136 LOGC("Critical error. Requesting CheckResponse failed.");
137 return CYNARA_API_SERVICE_NOT_AVAILABLE;
139 LOGD("checkResponse: policyType = %" PRIu16 ", metadata = %s",
140 checkResponse->m_resultRef.policyType(),
141 checkResponse->m_resultRef.metadata().c_str());
142 result = checkResponse->m_resultRef;
143 return CYNARA_API_SUCCESS;
146 int Logic::requestSimpleResult(const PolicyKey &key, PolicyResult &result) {
147 auto simpleCheckResponse = requestResponse<SimpleCheckRequest, SimpleCheckResponse>(key);
148 if (!simpleCheckResponse) {
149 LOGC("Critical error. Requesting SimpleCheckResponse failed.");
150 return CYNARA_API_SERVICE_NOT_AVAILABLE;
153 if (simpleCheckResponse->getReturnValue() != CYNARA_API_SUCCESS)
154 return simpleCheckResponse->getReturnValue();
156 LOGD("SimpleCheckResponse: policyType = %" PRIu16 ", metadata = %s",
157 simpleCheckResponse->getResult().policyType(),
158 simpleCheckResponse->getResult().metadata().c_str());
160 result = simpleCheckResponse->getResult();
161 return CYNARA_API_SUCCESS;
164 void Logic::onDisconnected(void) {
168 } // namespace Cynara