From 083a200d9019bada8a9e3d4e72cd99e79bebdefc Mon Sep 17 00:00:00 2001 From: Sungbae Yoo Date: Wed, 16 Jan 2019 13:27:08 +0900 Subject: [PATCH] Remove krate-specific classes It removes several classes such as PAM, Cgroup, Namespace, xml. Change-Id: I359f00587a0df7e1c6499cd61d522b96c38bbbc9 Signed-off-by: Sungbae Yoo --- include/klay/cgroup.h | 51 -------- include/klay/namespace.h | 38 ------ include/klay/pam.h | 68 ----------- include/klay/xml/document.h | 53 --------- include/klay/xml/keepblanks.h | 44 ------- include/klay/xml/node.h | 66 ---------- include/klay/xml/parser.h | 44 ------- packaging/klay.spec | 2 - src/CMakeLists.txt | 7 -- src/cgroup.cpp | 218 ---------------------------------- src/namespace.cpp | 80 ------------- src/pam.cpp | 164 ------------------------- src/xml/document.cpp | 136 --------------------- src/xml/keepblanks.cpp | 34 ------ src/xml/node.cpp | 117 ------------------ src/xml/parser.cpp | 84 ------------- test/CMakeLists.txt | 2 - test/data/sample-policy.xml | 147 ----------------------- 18 files changed, 1355 deletions(-) delete mode 100644 include/klay/cgroup.h delete mode 100644 include/klay/namespace.h delete mode 100644 include/klay/pam.h delete mode 100644 include/klay/xml/document.h delete mode 100644 include/klay/xml/keepblanks.h delete mode 100644 include/klay/xml/node.h delete mode 100644 include/klay/xml/parser.h delete mode 100644 src/cgroup.cpp delete mode 100644 src/namespace.cpp delete mode 100644 src/pam.cpp delete mode 100644 src/xml/document.cpp delete mode 100644 src/xml/keepblanks.cpp delete mode 100644 src/xml/node.cpp delete mode 100644 src/xml/parser.cpp delete mode 100644 test/data/sample-policy.xml diff --git a/include/klay/cgroup.h b/include/klay/cgroup.h deleted file mode 100644 index 4900ef4..0000000 --- a/include/klay/cgroup.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __RUNTIME_CGROUP_H__ -#define __RUNTIME_CGROUP_H__ - -#include -#include - -#include - -namespace klay { - -class KLAY_EXPORT Cgroup final { -public: - Cgroup() = delete; - - static bool existSubsystem(const std::string& name); - static void createSubsystem(const std::string& name); - static void destroySubsystem(const std::string& name); - - static bool exist(const std::string& subsystem, const std::string& path); - static void create(const std::string& subsystem, const std::string& path); - static void destroy(const std::string& subsystem, const std::string& path); - - static void addProcess(const std::string& subsystem, - const std::string& path, const pid_t pid); - static std::vector getProcessList(const std::string& subsystem, - const std::string& path); - - static const std::string getPath(const std::string& subsystem, const pid_t pid); -}; - -} // namespace klay - -namespace runtime = klay; - -#endif //!__RUNTIME_CGROUP_H__ diff --git a/include/klay/namespace.h b/include/klay/namespace.h deleted file mode 100644 index b42f12c..0000000 --- a/include/klay/namespace.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __RUNTIME_NAMESPACE_H__ -#define __RUNTIME_NAMESPACE_H__ - -#include - -#include - -namespace klay { - -class KLAY_EXPORT Namespace final { -public: - Namespace() = delete; - - static void unshare(int flags); - static void attach(const pid_t pid); -}; - -} // namespace klay - -namespace runtime = klay; - -#endif //!__RUNTIME_NAMESPACE_H__ diff --git a/include/klay/pam.h b/include/klay/pam.h deleted file mode 100644 index 0ce7145..0000000 --- a/include/klay/pam.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __RUNTIME_PAM_H__ -#define __RUNTIME_PAM_H__ - -#include -#include -#include - -#include -#include - -namespace klay { - -class PAM final { -public: - PAM(PAM&&) = delete; - PAM(const PAM&) = delete; - PAM(const std::string& service, const std::string& user); - ~PAM(); - - PAM& operator=(const PAM&) = delete; - PAM& operator=(PAM &&) = delete; - - void setData(const std::string &name, void* data, void (*cleanup)(pam_handle_t* pamh, void* data, int error)); - const void* getData(const std::string &name) const; - - void setItem(int item, void* data); - const void* getItem(int item) const; - - const std::string getUser(const std::string &prompt = "") const; - - void putEnv(const std::string &name_value); - const std::string getEnv(const std::string &name) const; - const std::vector getEnvList() const; - - void syslog(const std::string &log, int priority = LOG_ERR); - - int authenticate(int flags); - int setCredential(int flags); - int accountManagement(int flags); - int changeAuthenticationToken(int flags); - void openSession(int flags); - void closeSession(int flags); - -private: - pam_handle_t* pamh; -}; - -} // namespace klay - -namespace runtime = klay; - -#endif // __RUNTIME_PAM_H__ diff --git a/include/klay/xml/document.h b/include/klay/xml/document.h deleted file mode 100644 index 24ff52b..0000000 --- a/include/klay/xml/document.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __XML_DOCUMENT_H__ -#define __XML_DOCUMENT_H__ - -#include -#include - -#include - -#include -#include - -namespace klay { -namespace xml { - -class KLAY_EXPORT Document { -public: - Document(const std::string& root, const std::string& version = XML_DEFAULT_VERSION); - Document(xmlDoc* doc); - - ~Document(); - - Node& getRootNode(); - - Node::NodeList evaluate(const std::string& xpath); - void write(const std::string& filename, const std::string& encoding, bool formatted); - -private: - Node* rootNode; - xmlDoc* implementation; -}; - -} // namespace xml -} // namespace klay - -namespace xml = klay::xml; - -#endif //__XML_DOCUMENT_H__ diff --git a/include/klay/xml/keepblanks.h b/include/klay/xml/keepblanks.h deleted file mode 100644 index 76d5ac4..0000000 --- a/include/klay/xml/keepblanks.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __XML_KEEPBLANKS_H__ -#define __XML_KEEPBLANKS_H__ - -#include - -#include - -namespace klay { -namespace xml { - -class KLAY_EXPORT KeepBlanks { -public: - KeepBlanks(bool value); - ~KeepBlanks(); - - static const bool Default = true; - -private: - int preservedKeepBlanksDefault; - int preservedIndentTreeOutput; -}; - -} // namespace xml -} // namespace klay - -namespace xml = klay::xml; - -#endif //__XML_KEEPBLANKS_H__ diff --git a/include/klay/xml/node.h b/include/klay/xml/node.h deleted file mode 100644 index 7693446..0000000 --- a/include/klay/xml/node.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __XML_NODE_H__ -#define __XML_NODE_H__ - -#include -#include - -#include -#include - -#include - -namespace klay { -namespace xml { - -class KLAY_EXPORT Node { -public: - typedef std::vector NodeList; - - explicit Node(xmlNode* node); - Node(Node&&); - Node(const Node&) = delete; - - ~Node(); - - Node& operator=(const Node&) = delete; - - NodeList getChildren() const; - Node addNewChild(const std::string& name); - - std::string getName() const; - void setName(const std::string& name); - - std::string getContent() const; - void setContent(const std::string& content); - - std::string getProp(const std::string& name) const; - void setProp(const std::string& name, const std::string& val); - - bool isBlank() const; - -private: - xmlNode* implementation; -}; - -} // namespace xml -} // namespace klay - -namespace xml = klay::xml; - -#endif //__XML_NODE_H__ diff --git a/include/klay/xml/parser.h b/include/klay/xml/parser.h deleted file mode 100644 index f5d7857..0000000 --- a/include/klay/xml/parser.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#ifndef __XML_DOMPARSER_H__ -#define __XML_DOMPARSER_H__ - -#include - -#include - -#include -#include - -namespace klay { -namespace xml { - -class KLAY_EXPORT Parser { -public: - static Document* parseFile(const std::string& filename, bool validate = false); - static Document* parseString(const std::string& xml, bool validate = false); - -private: - static Document* parseContext(xmlParserCtxt* context, bool validate = false); -}; - -} // namespace xml -} // namespace klay - -namespace xml = klay::xml; - -#endif //__XML_DOMPARSER_H__ diff --git a/packaging/klay.spec b/packaging/klay.spec index 4945f73..3d103d1 100755 --- a/packaging/klay.spec +++ b/packaging/klay.spec @@ -12,7 +12,6 @@ BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(dlog) -BuildRequires: pkgconfig(libxml-2.0) BuildRequires: pkgconfig(libsystemd-daemon) BuildRequires: pkgconfig(libtzplatform-config) Requires(post): /sbin/ldconfig @@ -88,4 +87,3 @@ Testcases for klay library. %defattr(644,root,root,755) %attr(755,root,root) %{_bindir}/klay-test %attr(755,root,root) %{TZ_SYS_DATA}/klay-test/test-proc.sh -%{TZ_SYS_DATA}/klay-test/sample-policy.xml diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab1cbe2..265303c 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,11 +18,9 @@ SET(LIB_SOVERSION "0") SET (KLAY_SOURCES ${KLAY_SRC}/error.cpp ${KLAY_SRC}/exception.cpp - ${KLAY_SRC}/cgroup.cpp ${KLAY_SRC}/process.cpp ${KLAY_SRC}/eventfd.cpp ${KLAY_SRC}/mainloop.cpp - ${KLAY_SRC}/namespace.cpp ${KLAY_SRC}/testbench.cpp ${KLAY_SRC}/file-user.cpp ${KLAY_SRC}/filesystem.cpp @@ -31,10 +29,6 @@ SET (KLAY_SOURCES ${KLAY_SRC}/error.cpp ${KLAY_SRC}/db/column.cpp ${KLAY_SRC}/db/statement.cpp ${KLAY_SRC}/db/connection.cpp - ${KLAY_SRC}/xml/node.cpp - ${KLAY_SRC}/xml/parser.cpp - ${KLAY_SRC}/xml/document.cpp - ${KLAY_SRC}/xml/keepblanks.cpp ${KLAY_SRC}/rmi/socket.cpp ${KLAY_SRC}/rmi/client.cpp ${KLAY_SRC}/rmi/service.cpp @@ -69,7 +63,6 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${LIB_SOVERSION}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${LIB_VERSION}) PKG_CHECK_MODULES(KLAY_DEPS REQUIRED gio-2.0 - libxml-2.0 sqlite3 dlog libsystemd-daemon diff --git a/src/cgroup.cpp b/src/cgroup.cpp deleted file mode 100644 index ef04aa3..0000000 --- a/src/cgroup.cpp +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#define NAME_PATTERN "^[A-Za-z_][A-Za-z0-9_-]*" -#define PATH_PATTERN "(/*[A-Za-z_][A-Za-z0-9_-]*)*" - -namespace klay { - -bool Cgroup::existSubsystem(const std::string& name) -{ - try { - if (!std::regex_match(name, std::regex(NAME_PATTERN))) { - return false; - } - } catch (std::runtime_error &e) { - throw klay::Exception("Unexpected regex error"); - } - - klay::File dir("/sys/fs/cgroup/" + name); - if (dir.exists()) { - if (dir.isDirectory()) { - return true; - } - throw klay::Exception("Invalid subsystem name"); - } - - return false; -} - -void Cgroup::createSubsystem(const std::string& name) -{ - try { - if (!std::regex_match(name, std::regex(NAME_PATTERN))) { - throw klay::Exception("Invalid subsystem name"); - } - } catch (std::runtime_error &e) { - throw klay::Exception("Unexpected regex error"); - } - - if (existSubsystem(name)) { - return; - } - - klay::File subsystem("/sys/fs/cgroup/" + name); - if (::mount(NULL, "/sys/fs/cgroup/", NULL, MS_REMOUNT | - MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_STRICTATIME, - "mode=755")) { - throw klay::Exception("Failed to remount cgroupfs as the writable"); - } - - if (!subsystem.exists()) { - subsystem.makeDirectory(true); - } - - if (::mount(name.c_str(), subsystem.getPath().c_str(), - "cgroup", MS_NODEV | MS_NOSUID | MS_NOEXEC, - ("none,name=" + name).c_str())) { - subsystem.remove(false); - throw klay::Exception("Failed to mount cgroup subsystem"); - } - - if (::mount(NULL, "/sys/fs/cgroup/", NULL, MS_REMOUNT | MS_RDONLY | - MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_STRICTATIME, - "mode=755")) { - throw klay::Exception("Failed to remount cgroupfs as the read-only"); - } -} - -void Cgroup::destroySubsystem(const std::string& name) -{ - if (!existSubsystem(name)) { - throw klay::Exception("No such subsystem"); - } - - if (::mount(NULL, "/sys/fs/cgroup/", NULL, MS_REMOUNT | - MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_STRICTATIME, - "mode=755")) { - throw klay::Exception("Failed to remount cgroupfs as the writable"); - } - - klay::File subsystem("/sys/fs/cgroup/" + name); - ::umount2(subsystem.getPath().c_str(), MNT_EXPIRE); - - subsystem.remove(false); - - if (::mount(NULL, "/sys/fs/cgroup/", NULL, MS_REMOUNT | MS_RDONLY | - MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_STRICTATIME, - "mode=755")) { - throw klay::Exception("Failed to remount cgroupfs as the read-only"); - } -} - -bool Cgroup::exist(const std::string& subsystem, const std::string& path) -{ - try { - if (!std::regex_match(path, std::regex(PATH_PATTERN))) { - return false; - } - } catch (std::runtime_error &e) { - throw klay::Exception("Unexpected regex error"); - } - - klay::File dir("/sys/fs/cgroup/" + subsystem + "/" + path); - if (dir.exists()) { - if (dir.isDirectory()) { - return true; - } - throw klay::Exception("Invalid path"); - } - - return false; -} - -void Cgroup::create(const std::string& subsystem, const std::string& path) -{ - try { - if (!std::regex_match(path, std::regex(PATH_PATTERN))) { - throw klay::Exception("Invalid path"); - } - } catch (std::runtime_error &e) { - throw klay::Exception("Unexpected regex error"); - } - - if (exist(subsystem, path)) { - return; - } - - klay::File dir("/sys/fs/cgroup/" + subsystem + "/" + path); - dir.makeDirectory(true); -} - -void Cgroup::destroy(const std::string& subsystem, const std::string& path) -{ - if (!exist(subsystem, path)) { - throw klay::Exception("No such path in subsystem"); - } - - klay::File dir("/sys/fs/cgroup/" + subsystem + "/" + path); - dir.remove(false); -} - -void Cgroup::addProcess(const std::string& subsystem, const std::string& path, const pid_t pid) -{ - if (!exist(subsystem, path)) { - throw klay::Exception("No such path in subsystem"); - } - - std::ofstream ofs("/sys/fs/cgroup/" + subsystem + "/" + path + - "/tasks"); - - ofs << pid << std::endl; -} - -std::vector Cgroup::getProcessList(const std::string& subsystem, const std::string& path) -{ - std::vector ret; - std::ifstream ifs("/sys/fs/cgroup/" + subsystem + "/" + path + - "/tasks"); - - pid_t pid; - - ifs >> pid; - - while (ifs.good()) { - ret.push_back(pid); - ifs >> pid; - } - - return ret; -} - -const std::string Cgroup::getPath(const std::string& subsystem, const pid_t pid) -{ - std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup"); - std::string ret = "/", line; - - while (std::getline(ifs, line)) { - std::stringstream lineStream(line); - std::string name; - - //the first getline is for removing the first argument - std::getline(lineStream, name, ':'); - std::getline(lineStream, name, ':'); - - if (name == subsystem || name == "name=" + subsystem) { - ret = line.substr(line.find('/')); - } - } - return ret; -} - -} // namespace klay diff --git a/src/namespace.cpp b/src/namespace.cpp deleted file mode 100644 index 2890c6d..0000000 --- a/src/namespace.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ -#include - -#include -#include -#include - -#include -#include - -namespace klay { - -namespace { - -typedef std::pair NamespacePair; -std::vector namespaces = { - {"mnt", CLONE_NEWNS}, - {"net", CLONE_NEWNET}, - {"ipc", CLONE_NEWIPC}, - {"pid", CLONE_NEWPID}, - {"uts", CLONE_NEWUTS}, - {"user", CLONE_NEWUSER}, -#ifdef CLONE_NEWCGROUP - {"cgroup", CLONE_NEWCGROUP}, -#endif -}; - -} // namespace - -void Namespace::attach(const pid_t pid) -{ - for (const NamespacePair& ns : namespaces) { - std::string nspath = "/proc/" + std::to_string(pid) + "/ns/" + ns.first; - - int fd; - do { - fd = ::open(nspath.c_str(), O_RDONLY); - } while (fd == -1 && errno == EINTR); - - if (fd == -1) { - if (errno != ENOENT) { - throw klay::Exception("Failed to open namesapce: " + nspath); - } - } else { - if (::setns(fd, ns.second)) { - ::close(fd); - throw klay::Exception("Failed to set namespace: " + nspath); - } - ::close(fd); - } - } -} - -void Namespace::unshare(int flags) -{ - if (::unshare(flags)) { - throw klay::Exception("Failed to unshare namespace"); - } - - if (flags & CLONE_NEWNS && - ::mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) { - throw klay::Exception("Failed to mount root filesystem"); - } -} - -} // namespace klay diff --git a/src/pam.cpp b/src/pam.cpp deleted file mode 100644 index 0ac705c..0000000 --- a/src/pam.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#include -#include - -#include -#include - -#include -#include - -namespace klay { - -PAM::PAM(const std::string& service, const std::string& user) -{ - struct pam_conv pamc; - - int error = ::pam_start(service.c_str(), user.c_str(), &pamc, &pamh); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -PAM::~PAM() -{ - int error = ::pam_end(pamh, PAM_SUCCESS); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -void PAM::setData(const std::string &name, void* data, void (*cleanup)(pam_handle_t* pamh, void* data, int error)) -{ - int error = ::pam_set_data(pamh, name.c_str(), data, cleanup); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -const void* PAM::getData(const std::string &name) const -{ - const void* ret; - int error = ::pam_get_data(pamh, name.c_str(), &ret); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } - return ret; -} - -void PAM::setItem(int item, void* data) -{ - int error = ::pam_set_item(pamh, item, data); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -const void* PAM::getItem(int item) const -{ - const void* ret; - int error = ::pam_get_item(pamh, item, &ret); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } - return ret; -} - -const std::string PAM::getUser(const std::string &prompt) const -{ - const char* user; - int error = ::pam_get_user(pamh, &user, prompt.c_str()); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } - return std::string(user); -} - -void PAM::putEnv(const std::string &name_value) -{ - int error = ::pam_putenv(pamh, name_value.c_str()); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -const std::string PAM::getEnv(const std::string &name) const -{ - const char* value = ::pam_getenv(pamh, name.c_str()); - if (value == NULL) { - throw klay::Exception("PAM Error"); - } - return value; -} - -const std::vector PAM::getEnvList() const -{ - std::vector ret; - char** array = ::pam_getenvlist(pamh); - if (array == NULL) { - throw klay::Exception("PAM Error"); - } - for (int i = 0; array[i] != NULL; i++) { - ret.push_back(array[i]); - } - return ret; -} - -void PAM::syslog(const std::string &log, int priority) -{ - ::pam_syslog(pamh, priority, "%s", log.c_str()); -} - - -int PAM::authenticate(int flags) -{ - return ::pam_authenticate(pamh, flags); -} - -int PAM::setCredential(int flags) -{ - return ::pam_setcred(pamh, flags); -} - -int PAM::accountManagement(int flags) -{ - return ::pam_acct_mgmt(pamh, flags); -} - -int PAM::changeAuthenticationToken(int flags) -{ - return ::pam_chauthtok(pamh, flags); -} - -void PAM::openSession(int flags) -{ - int error = ::pam_open_session(pamh, flags); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -void PAM::closeSession(int flags) -{ - int error = ::pam_close_session(pamh, flags); - if (error != PAM_SUCCESS) { - throw klay::Exception("PAM Error"); - } -} - -} // namespace klay diff --git a/src/xml/document.cpp b/src/xml/document.cpp deleted file mode 100644 index 4d9ab57..0000000 --- a/src/xml/document.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ - -#include - -#include - -#include -#include -#include - -namespace klay { -namespace xml { - -Document::Document(const std::string& root, const std::string& version) : - implementation(xmlNewDoc((const xmlChar*)version.c_str())) -{ - if (implementation == nullptr) { - throw klay::Exception("Failed to create document"); - } - - implementation->_private = this; - - xmlNode* rootPtr = xmlNewNode(NULL, xmlStrdup((const xmlChar*)root.c_str())); - xmlDocSetRootElement(implementation, rootPtr); - - rootNode = new Node(rootPtr); -} - -Document::Document(xmlDoc* doc) - : implementation(doc) -{ - implementation->_private = this; - - rootNode = new Node(xmlDocGetRootElement(implementation)); -} - -Document::~Document() -{ - if (rootNode != nullptr) { - delete rootNode; - } - - xmlFreeDoc(implementation); -} - -Node& Document::getRootNode() -{ - if (rootNode == nullptr) { - throw klay::Exception("Empty document"); - } - - return *rootNode; -} - -Node::NodeList Document::evaluate(const std::string& xpath) -{ - auto ctxt = xmlXPathNewContext(implementation); - if (ctxt == nullptr) { - throw klay::Exception("Failed to create XPath context for " + xpath); - } - - auto result = xmlXPathEval((const xmlChar*)xpath.c_str(), ctxt); - if (result == nullptr) { - xmlXPathFreeContext(ctxt); - throw klay::Exception("Invalid XPath: " + xpath); - } - - if (result ->type != XPATH_NODESET) { - xmlXPathFreeObject(result); - xmlXPathFreeContext(ctxt); - - throw klay::Exception("Only nodeset result types are supported"); - } - - auto nodeset = result->nodesetval; - - Node::NodeList nodes; - if ((nodeset == nullptr) || (xmlXPathNodeSetIsEmpty(nodeset))) { - xmlXPathFreeContext(ctxt); - return nodes; - } - - const int count = xmlXPathNodeSetGetLength(nodeset); - - nodes.reserve(count); - for (int i = 0; i != count; i++) { - auto cnode = xmlXPathNodeSetItem(nodeset, i); - if (!cnode) { - continue; - } - - if (cnode->type == XML_NAMESPACE_DECL) { - continue; - } - - nodes.emplace_back(cnode); - } - - xmlXPathFreeObject(result); - xmlXPathFreeContext(ctxt); - - return nodes; -} - -void Document::write(const std::string& filename, const std::string& encoding, bool formatted) -{ - KeepBlanks keepBlanks(KeepBlanks::Default); - xmlIndentTreeOutput = formatted; - - xmlResetLastError(); - - const int result = xmlSaveFormatFileEnc(filename.c_str(), - implementation, - encoding.c_str(), - formatted); - if (result == 0) { - throw klay::Exception("Failed to write XML document"); - } -} - -} // namespace xml -} // namespace klay diff --git a/src/xml/keepblanks.cpp b/src/xml/keepblanks.cpp deleted file mode 100644 index 32c909f..0000000 --- a/src/xml/keepblanks.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ -#include - -namespace klay { -namespace xml { - -KeepBlanks::KeepBlanks(bool value) -{ - preservedIndentTreeOutput = xmlIndentTreeOutput; - preservedKeepBlanksDefault = xmlKeepBlanksDefault(value ? 1 : 0); -} - -KeepBlanks::~KeepBlanks() -{ - xmlKeepBlanksDefault(preservedKeepBlanksDefault); - xmlIndentTreeOutput = preservedIndentTreeOutput; -} - -} // namespace xml -} // namespace klay diff --git a/src/xml/node.cpp b/src/xml/node.cpp deleted file mode 100644 index 83b8742..0000000 --- a/src/xml/node.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ -#include -#include - -namespace klay { -namespace xml { - -Node::Node(xmlNode* node) : - implementation(node) -{ -} - -Node::Node(Node&& node) : - implementation(node.implementation) -{ -} - -Node::~Node() -{ -} - -Node::NodeList Node::getChildren() const -{ - NodeList nodeList; - - auto child = implementation->xmlChildrenNode; - while (child != nullptr) { - nodeList.emplace_back(child); - child = child->next; - } - - return nodeList; -} - -Node Node::addNewChild(const std::string& name) -{ - xmlNode* nodePtr = xmlNewNode(NULL, xmlStrdup((const xmlChar*)name.c_str())); - if (nodePtr == nullptr) { - throw klay::Exception("Can not create a new node"); - } - xmlAddChild(implementation, nodePtr); - - return Node(nodePtr); -} - -std::string Node::getName() const -{ - return implementation->name ? (const char*)implementation->name : ""; -} - -void Node::setName(const std::string& name) -{ - xmlNodeSetName(implementation, (const xmlChar*)name.c_str()); -} - -std::string Node::getContent() const -{ - xmlChar* content = xmlNodeGetContent(implementation); - if (content == NULL) { - return ""; - } - std::string ret((const char*)content); - xmlFree(content); - return ret; -} - -void Node::setContent(const std::string& content) -{ - xmlNodeSetContent(implementation, (xmlChar*)content.c_str()); -} - -std::string Node::getProp(const std::string& name) const -{ - if (implementation->type != XML_ELEMENT_NODE) { - throw klay::Exception("This node type does not have properties"); - } - - xmlChar* prop = xmlGetProp(implementation, (xmlChar*)name.c_str()); - if (prop) { - std::string ret((const char*)prop); - xmlFree(prop); - return ret; - } - - return ""; -} - -void Node::setProp(const std::string& name, const std::string& val) -{ - if (implementation->type != XML_ELEMENT_NODE) { - throw klay::Exception("Can not set properties for this node type"); - } - - xmlSetProp(implementation, (xmlChar*)name.c_str(), (xmlChar*)val.c_str()); -} - -bool Node::isBlank() const -{ - return xmlIsBlankNode(const_cast(implementation)); -} - -} // namespace xml -} // namespace klay diff --git a/src/xml/parser.cpp b/src/xml/parser.cpp deleted file mode 100644 index 86ba1db..0000000 --- a/src/xml/parser.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 - */ -#include - -#include -#include -#include - -namespace klay { -namespace xml { - -Document* Parser::parseContext(xmlParserCtxt* context, bool validate) -{ - if (context == nullptr) { - throw klay::Exception("Could not create parser context"); - } - - KeepBlanks(false); - - int options = 0; - - if (validate) { - options |= XML_PARSE_DTDVALID; - } else { - options &= ~XML_PARSE_DTDVALID; - } - - xmlCtxtUseOptions(context, options); - - if (xmlParseDocument(context) < 0) { - xmlFreeParserCtxt(context); - throw klay::Exception("Parsing failed"); - } - - xmlDoc* document = context->myDoc; - - // We took the ownership on the doc - context->myDoc = nullptr; - - xmlFreeParserCtxt(context); - - return new Document(document); -} - -Document* Parser::parseFile(const std::string& filename, bool validate) -{ - xmlParserCtxt* context = xmlCreateFileParserCtxt(filename.c_str()); - if (context == nullptr) { - throw klay::Exception("Could not create parser context"); - } - - if (context->directory == nullptr) { - context->directory = xmlParserGetDirectory(filename.c_str()); - } - - return parseContext(context, validate); -} - -Document* Parser::parseString(const std::string& xml, bool validate) -{ - xmlParserCtxt* context = xmlCreateMemoryParserCtxt(xml.c_str(), xml.size() + 1); - - if (context == nullptr) { - throw klay::Exception("Could not create parser context"); - } - - return parseContext(context, validate); -} - -} // namespace xml -} // namespace klay diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8bf1544..5be15de 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,7 +17,6 @@ PROJECT(klay-test) SET(TEST_SRC main.cpp rmi.cpp - xml.cpp auth.cpp dbus.cpp proc.cpp @@ -45,5 +44,4 @@ INCLUDE_DIRECTORIES(SYSTEM ${TEST_DEPS_INCLUDE_DIRS} ${KLAY_INCLUDE}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${TEST_DEPS_LIBRARIES} ${LFS_LDFLAGS} ${LFS_LIBS} klay pthread) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin) -INSTALL(FILES data/sample-policy.xml DESTINATION ${TEST_DATA_INSTALL_DIR}) INSTALL(FILES data/test-proc.sh DESTINATION ${TEST_DATA_INSTALL_DIR}) diff --git a/test/data/sample-policy.xml b/test/data/sample-policy.xml deleted file mode 100644 index 6768eac..0000000 --- a/test/data/sample-policy.xml +++ /dev/null @@ -1,147 +0,0 @@ - - - 0.1.0 - - allowed - allowed - - - - allowed - allowed - allowed - - - - 0 - 0 - 0 - 0 - 0 - empty - 0 - 0 - 0 - 0 - 0 - 0 - 0 - empty - - - - - - allowed - allowed - - - - - - allowed - none - none - disabled - allowed - enabled - disabled - - - - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - disabled - disabled - - - - allowed - allowed - allowed - allowed - allowed - allowed - allowed - - - - disabled - disabled - enabled - - - - 0 - 0 - 0 - 0 - 0 - 0 - 0,0,0 - 0,0,0 - 0,0,0 - 0,0,0 - 0,0,0 - 0 - 0 - - - - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - allowed - - - - allowed - allowed - allowed - allowed - allowed - - - - empty - empty - allowed - allowed - empty - empty - - -- 2.34.1