From: Zofia Abramowska Date: Mon, 1 Dec 2014 15:58:10 +0000 (+0100) Subject: Add translation methods between agent<->cynara<->plugin X-Git-Tag: accepted/tizen/common/20150128.145804~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=75889548626ad2fe24f591895e738d0edbbd8b50;p=platform%2Fcore%2Fsecurity%2Faskuser.git Add translation methods between agent<->cynara<->plugin Change-Id: Ifd24baca36d9afed2b5201b44054d03584216f22 --- diff --git a/packaging/askuser.spec b/packaging/askuser.spec index 27b9edf..632cd78 100644 --- a/packaging/askuser.spec +++ b/packaging/askuser.spec @@ -11,6 +11,9 @@ BuildRequires: cmake BuildRequires: zip BuildRequires: pkgconfig(libsystemd-daemon) BuildRequires: pkgconfig(libsystemd-journal) +BuildRequires: pkgconfig(cynara-plugin) +BuildRequires: pkgconfig(cynara-agent) + %{?systemd_requires} %if !%{defined build_type} diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index c85ecae..2512cc5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -15,6 +15,11 @@ # @file CMakeLists.txt # @author Adam Malinowski # +PKG_CHECK_MODULES(COMMON_DEP + REQUIRED + cynara-plugin + cynara-agent + ) SET(ASKUSER_COMMON_VERSION_MAJOR 0) SET(ASKUSER_COMMON_VERSION ${ASKUSER_COMMON_VERSION_MAJOR}.0.1) @@ -34,8 +39,15 @@ INCLUDE_DIRECTORIES(SYSTEM SET(COMMON_PATH ${ASKUSER_PATH}/common) +INCLUDE_DIRECTORIES( + ${COMMON_DEP_INCLUDE_DIRS} + ${COMMON_PATH} + ) + SET(COMMON_SOURCES ${COMMON_PATH}/log/log.cpp + ${COMMON_PATH}/translator/Translator.cpp + ${COMMON_PATH}/types/AgentErrorMsg.cpp ) ADD_DEFINITIONS("-fvisibility=default") diff --git a/src/common/translator/Translator.cpp b/src/common/translator/Translator.cpp new file mode 100644 index 0000000..9dc59dc --- /dev/null +++ b/src/common/translator/Translator.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file Translator.cpp + * @author Zofia Abramowska + * @brief Implementation of Translator methods + */ + +#include "Translator.h" + +#include + +#include +#include +#include + +namespace AskUser { +namespace Translator { +namespace Agent { + +RequestData dataToRequest(const Cynara::PluginData &data) { + std::stringstream stream(data); + std::size_t strSize; + std::string members[3]; + + for (auto &member : members) { + stream >> strSize; + std::vector buffer(strSize, '\0'); + char separator; + //Consume separator + stream.read(&separator, 1); + stream.read(buffer.data(), strSize); + //read doesn't append null + member.assign(buffer.begin(), buffer.end()); + } + return RequestData{members[0], members[1], members[2]}; +} + +Cynara::PluginData answerToData(Cynara::PolicyType answer, const std::string &errMsg) { + if (errMsg.empty()) + return std::to_string(answer); + else + return errMsg; +} + +} //namespace Agent + +namespace Plugin { + +Cynara::PolicyType dataToAnswer(const Cynara::PluginData &data) { + // data is an error string + if (data == AgentErrorMsg::Error || data == AgentErrorMsg::Timeout) + return Cynara::PredefinedPolicyType::DENY; + // data is policy type + long long policyType; + try { + policyType = std::stoll(data); + } catch (const std::exception &e) { + throw TranslateErrorException("Could not convert response to PolicyType : " + + data); + } + auto maxPolicyType = std::numeric_limits::max(); + if (policyType > maxPolicyType) { + throw TranslateErrorException("Value of response exceeds max value of PolicyType : " + + std::to_string(policyType)); + } + return static_cast(policyType); +} + +Cynara::PluginData requestToData(const std::string &client, + const std::string &user, + const std::string &privilege) +{ + const char separator = ' '; + return std::to_string(client.length()) + separator + client + separator + + std::to_string(user.length()) + separator + user + separator + + std::to_string(privilege.length()) + separator + privilege + separator; +} + +} //namespace Plugin +} //namespace Translator +} //namespace AskUser diff --git a/src/common/translator/Translator.h b/src/common/translator/Translator.h new file mode 100644 index 0000000..89140fe --- /dev/null +++ b/src/common/translator/Translator.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file Translator.h + * @author Zofia Abramowska + * @brief Definition of Translator methods and TranslateErrorException class + */ + +#pragma once + +#include +#include +#include + +#include +#include + +namespace AskUser { +namespace Translator { + +class TranslateErrorException : std::exception { +public: + TranslateErrorException(const std::string &msg) : m_what(msg) {}; + virtual const char* what() const noexcept { + return m_what.c_str(); + } +private: + std::string m_what; +}; + +namespace Agent { + RequestData dataToRequest(const Cynara::PluginData &data); + Cynara::PluginData answerToData(Cynara::PolicyType answer, const std::string &errMsg); +} // namespace Agent + +namespace Plugin { + Cynara::PolicyType dataToAnswer(const Cynara::PluginData &data); + Cynara::PluginData requestToData(const std::string &client, + const std::string &user, + const std::string &privilege); +} // namespace Plugin + +} // namespace Translator +} // namespace AskUser + diff --git a/src/common/types/AgentErrorMsg.cpp b/src/common/types/AgentErrorMsg.cpp new file mode 100644 index 0000000..983a84f --- /dev/null +++ b/src/common/types/AgentErrorMsg.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file AgentErrorMsg.cpp + * @author Zofia Abramowska + * @brief Definition of agent error messages passed to cynara plugin + */ + +#include "AgentErrorMsg.h" + +namespace AskUser { +namespace AgentErrorMsg { + +const std::string Error = "ERROR"; +const std::string Timeout = "TIMEOUT"; + +} // namespace AgentErrorMsg +} // namespace AskUser diff --git a/src/common/types/AgentErrorMsg.h b/src/common/types/AgentErrorMsg.h new file mode 100644 index 0000000..6b7dcaa --- /dev/null +++ b/src/common/types/AgentErrorMsg.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file AgentErrorMsg.h + * @author Zofia Abramowska + * @brief Agent error messages passed to cynara plugin + */ + +#pragma once + +#include + +namespace AskUser { +namespace AgentErrorMsg { + +extern const std::string Error; +extern const std::string Timeout; + +} // namespace AgentErrorMsg +} // namespace AskUser diff --git a/src/common/types/RequestData.h b/src/common/types/RequestData.h new file mode 100644 index 0000000..db93ac5 --- /dev/null +++ b/src/common/types/RequestData.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file RequestData.h + * @author Zofia Abramowska + * @brief Definition of RequestData structure + */ + +#pragma once + +#include + +namespace AskUser { + +struct RequestData { + std::string client; + std::string user; + std::string privilege; +}; + +} // namespace AskUser diff --git a/src/common/types/SupportedTypes.h b/src/common/types/SupportedTypes.h new file mode 100644 index 0000000..8baf196 --- /dev/null +++ b/src/common/types/SupportedTypes.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +/** + * @file SupportedTypes.h + * @author Zofia Abramowska + * @brief Definition of plugin supported types and agent type + */ + +#pragma once + +#include + +namespace AskUser { +namespace SupportedTypes { + +namespace Agent { +const char* const AgentType = "AskUser"; +} //namespace Agent + +namespace Service { +const Cynara::PolicyType ASK_USER = 10; +} //namespace Service + +namespace Client { +const Cynara::PolicyType ALLOW_ONCE = 11; +const Cynara::PolicyType ALLOW_PER_SESSION = 12; +const Cynara::PolicyType ALLOW_PER_LIFE = 13; +} //namespace Client + +} //namespace SupportedTypes +} //namespace AskUser