Add cynara sd-bus helper 10/189910/12
authorMonika Zielinska <m.zielinska3@samsung.com>
Mon, 5 Nov 2018 12:17:38 +0000 (13:17 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Tue, 8 Jan 2019 15:05:47 +0000 (15:05 +0000)
* Added cynara_sd_bus_get_(client,user,pid) credential fetching API
  supporting sd-bus library
* API uses SD_BUS_CREDS_SELINUX_CONTEXT option for fetching Smack label
  as there is currently no other secure way

Change-Id: I36b771be3a5ba764467e36154fd520b3f0b3e2fc

15 files changed:
CMakeLists.txt
doc/examples/cynara-creds-sd-bus.example [new file with mode: 0644]
packaging/cynara.spec
packaging/libcynara-creds-sd-bus.manifest [new file with mode: 0644]
packaging/libcynara-dbus.spec
pkgconfig/CMakeLists.txt
pkgconfig/cynara-creds-sd-bus/CMakeLists.txt [new file with mode: 0644]
pkgconfig/cynara-creds-sd-bus/cynara-creds-sd-bus.pc.in [new file with mode: 0644]
src/CMakeLists.txt
src/helpers/creds-sd-bus/CMakeLists.txt [new file with mode: 0644]
src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp [new file with mode: 0644]
src/helpers/creds-sd-bus/creds-sd-bus-inner.h [new file with mode: 0644]
src/helpers/creds-sd-bus/creds-sd-bus.cpp [new file with mode: 0644]
src/include/CMakeLists.txt
src/include/cynara-creds-sd-bus.h [new file with mode: 0644]

index 13126e29f9bf155ffb33060ec4c845d215b74f2d..fbfdfe2b51291874006d12f0a1bca2e1cc7beabb 100644 (file)
@@ -15,6 +15,7 @@
 # @file        CMakeLists.txt
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
 # @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
 #
 
 ############################# Check minimum CMake version #####################
@@ -197,6 +198,9 @@ ENDIF (BUILD_COMMONS)
 IF (BUILD_DBUS)
     SET(TARGET_LIB_CREDS_DBUS "cynara-creds-dbus")
     SET(TARGET_LIB_CREDS_GDBUS "cynara-creds-gdbus")
+    IF (SYSTEMD_DEP_FOUND)
+        SET(TARGET_LIB_CREDS_SD_BUS "cynara-creds-sd-bus")
+    ENDIF (SYSTEMD_DEP_FOUND)
 ENDIF (BUILD_DBUS)
 
 ADD_SUBDIRECTORY(src)
diff --git a/doc/examples/cynara-creds-sd-bus.example b/doc/examples/cynara-creds-sd-bus.example
new file mode 100644 (file)
index 0000000..db425d4
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *  Copyright (c) 2018 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
+ */
+
+/*
+ * Get client credentials from sd_bus
+ */
+
+void cynara_log(const char *message, int cynara_status) {
+    char buf[BUFSIZ];
+
+    int ret = cynara_strerror(cynara_status, buf, BUFSIZ);
+    if (ret != CYNARA_API_SUCCESS) {
+        strncpy(buf, "cynara_strerror failed", BUFSIZ);
+        buf[BUFSIZ - 1] = '\0';
+    }
+    if (cynara_status < 0)
+        log_error("%s: %s", message, buf);
+    else
+        log_debug("%s: %s", message, buf);
+}
+
+struct Creds {
+    char *client = NULL;
+    char *user = NULL;
+    char *client_session = NULL;
+
+    ~Creds() {
+        free(client);
+        free(user);
+        free(client_session);
+    }
+};
+
+int get_credentials(sd_bus *bus, char *unique_name, Creds &creds)
+{
+    int ret = 0;
+    int pid = 0;
+
+    ret = cynara_creds_sd_bus_get_user(bus, unique_name, USER_METHOD_DEFAULT, &(creds.user));
+    if (ret != CYNARA_API_SUCCESS) {
+        cynara_log("cynara_creds_sd_bus_get_user() failed", ret);
+        return INVALID_OPERATION;
+    }
+
+    ret = cynara_creds_sd_bus_get_client(bus, unique_name, CLIENT_METHOD_DEFAULT, &(creds.client));
+    if (ret != CYNARA_API_SUCCESS) {
+        cynara_log("cynara_creds_sd_bus_get_client() failed", ret);
+        return INVALID_OPERATION;
+    }
+
+    ret = cynara_creds_sd_bus_get_pid(bus, unique_name, &pid);
+    if (ret != CYNARA_API_SUCCESS) {
+        cynara_log("cynara_creds_sd_bus_get_pid() failed", ret);
+        return INVALID_OPERATION;
+    }
+
+    creds.client_session = cynara_session_from_pid(pid);
+    return SUCCESS;
+}
index e51cee45b4f80687a915500430a3d20b4d08b6be..c1aa67970a2dd1fba0f36ac1b0d3e31ee018a34a 100644 (file)
@@ -40,7 +40,7 @@ BuildRequires: pkgconfig(libunwind)
 %description
 service, client libraries (libcynara-client, libcynara-admin),
 agent library, helper libraries (libcynara-session, libcynara-creds-common, libcynara-creds-dbus,
-libcynara-creds-socket, libcynara-creds-self) and tests (cynara-tests)
+libcynara-creds-sd-bus, libcynara-creds-socket, libcynara-creds-self) and tests (cynara-tests)
 
 %prep
 %setup -q -n cynara-%{version}
diff --git a/packaging/libcynara-creds-sd-bus.manifest b/packaging/libcynara-creds-sd-bus.manifest
new file mode 100644 (file)
index 0000000..a76fdba
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+</manifest>
index 37e586f477f8c7d376486bbe40ae0463435ef4ac..14f1d317b57196a1276cfb005697725c3d7bc8c7 100644 (file)
@@ -8,6 +8,7 @@ Source0:    cynara-%{version}.tar.gz
 Source1000:    %{name}-rpmlintrc
 Source1008:    libcynara-creds-dbus.manifest
 Source1009:    libcynara-creds-gdbus.manifest
+Source1010:    libcynara-creds-sd-bus.manifest
 Requires:      default-ac-domains
 Requires:      libcynara-commons = %{version}
 Requires:      dbus
@@ -18,6 +19,7 @@ Requires(postun): cynara-db-migration >= %{version}
 BuildRequires: cmake
 BuildRequires: zip
 BuildRequires: pkgconfig(libsmack)
+BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(dbus-1)
 
 # alternatives: DLOG, JOURNAL, NONE, (otherwise SYSLOG is used)
@@ -71,10 +73,28 @@ Requires:   libcynara-creds-commons = %{version}
 %description -n libcynara-creds-gdbus
 Cynara credentials helpers library for gdbus clients
 
+%package -n libcynara-creds-sd-bus-devel
+Summary:    Development files for sd-bus helpers library
+Requires:   cynara-devel = %{version}
+Requires:   libcynara-creds-sd-bus = %{version}
+Requires:   pkgconfig(libsystemd)
+
+%description -n libcynara-creds-sd-bus-devel
+Development files for sd-bus helpers library
+
+%package -n libcynara-creds-sd-bus
+Summary:    Cynara credentials helpers library for sd-bus client
+BuildRequires: pkgconfig(libsystemd)
+Requires:   libcynara-creds-commons = %{version}
+
+%description -n libcynara-creds-sd-bus
+Cynara credentials helpers library for sd-bus clients
+
 %prep
 %setup -q -n cynara-%{version}
 cp -a %{SOURCE1008} .
 cp -a %{SOURCE1009} .
+cp -a %{SOURCE1010} .
 
 %build
 export CXXFLAGS+=" -Wl,-z,relro,-z,now"
@@ -129,6 +149,11 @@ rm -rf %{buildroot}
 %{_libdir}/pkgconfig/cynara-creds-gdbus.pc
 %{_libdir}/libcynara-creds-gdbus.so
 
+%files -n libcynara-creds-sd-bus-devel
+%{_includedir}/cynara/cynara-creds-sd-bus.h
+%{_libdir}/pkgconfig/cynara-creds-sd-bus.pc
+%{_libdir}/libcynara-creds-sd-bus.so
+
 %files -n libcynara-creds-dbus
 %manifest libcynara-creds-dbus.manifest
 %license LICENSE
@@ -139,3 +164,7 @@ rm -rf %{buildroot}
 %license LICENSE
 %{_libdir}/libcynara-creds-gdbus.so.*
 
+%files -n libcynara-creds-sd-bus
+%manifest libcynara-creds-sd-bus.manifest
+%license LICENSE
+%{_libdir}/libcynara-creds-sd-bus.so.*
index 8f499eb2d578483101d440766defe2c0875e6cb1..578773c2977d39121f2b8a4210c447f99a1d40e8 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2018 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.
@@ -15,6 +15,7 @@
 # @file        CMakeLists.txt
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
 # @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
 #
 
 IF (BUILD_COMMONS)
@@ -34,5 +35,8 @@ ENDIF (BUILD_COMMONS)
 IF (BUILD_DBUS)
     ADD_SUBDIRECTORY(cynara-creds-dbus)
     ADD_SUBDIRECTORY(cynara-creds-gdbus)
+    IF (SYSTEMD_DEP_FOUND)
+        ADD_SUBDIRECTORY(cynara-creds-sd-bus)
+    ENDIF (SYSTEMD_DEP_FOUND)
 ENDIF (BUILD_DBUS)
 
diff --git a/pkgconfig/cynara-creds-sd-bus/CMakeLists.txt b/pkgconfig/cynara-creds-sd-bus/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e163891
--- /dev/null
@@ -0,0 +1,25 @@
+# Copyright (c) 2018 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.
+#
+# @file        CMakeLists.txt
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
+#
+
+CONFIGURE_FILE(cynara-creds-sd-bus.pc.in cynara-creds-sd-bus.pc @ONLY)
+
+INSTALL(FILES
+    ${CMAKE_BINARY_DIR}/pkgconfig/cynara-creds-sd-bus/cynara-creds-sd-bus.pc
+    DESTINATION
+    ${LIB_DIR}/pkgconfig
+    )
diff --git a/pkgconfig/cynara-creds-sd-bus/cynara-creds-sd-bus.pc.in b/pkgconfig/cynara-creds-sd-bus/cynara-creds-sd-bus.pc.in
new file mode 100644 (file)
index 0000000..c727640
--- /dev/null
@@ -0,0 +1,11 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=@LIB_DIR@
+includedir=${prefix}/include
+
+Name: cynara-creds-sd-bus
+Description: cynara-creds package for sd-bus clients
+Version: @CYNARA_VERSION@
+Requires: systemd
+Libs: -L${libdir} -lcynara-creds-sd-bus -lcynara-creds-commons -lcynara-commons
+Cflags: -I${includedir}/cynara
index 424c3bfd15c37152ad646d263a562185aa551dbf..bcb0511654b75ca30292554ecd302ec656df672f 100644 (file)
@@ -14,6 +14,7 @@
 #
 # @file        CMakeLists.txt
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
 #
 
 
@@ -82,5 +83,8 @@ ENDIF (BUILD_SERVICE)
 IF (BUILD_DBUS)
     ADD_SUBDIRECTORY(helpers/creds-dbus)
     ADD_SUBDIRECTORY(helpers/creds-gdbus)
+    IF (SYSTEMD_DEP_FOUND)
+        ADD_SUBDIRECTORY(helpers/creds-sd-bus)
+    ENDIF (SYSTEMD_DEP_FOUND)
 ENDIF (BUILD_DBUS)
 
diff --git a/src/helpers/creds-sd-bus/CMakeLists.txt b/src/helpers/creds-sd-bus/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c5ddb36
--- /dev/null
@@ -0,0 +1,52 @@
+# Copyright (c) 2018 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.
+#
+# @file        CMakeLists.txt
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
+#
+
+SET(LIB_CREDS_SD_BUS_VERSION_MAJOR 0)
+SET(LIB_CREDS_SD_BUS_VERSION ${LIB_CREDS_SD_BUS_VERSION_MAJOR}.14.20)
+
+SET(LIB_CREDS_SD_BUS_PATH ${CYNARA_PATH}/helpers/creds-sd-bus)
+
+SET(LIB_CREDS_SD_BUS_SOURCES
+    ${LIB_CREDS_SD_BUS_PATH}/creds-sd-bus.cpp
+    ${LIB_CREDS_SD_BUS_PATH}/creds-sd-bus-inner.cpp
+    )
+
+INCLUDE_DIRECTORIES(
+    SYSTEM
+    ${SYSTEMD_DEP_INCLUDE_DIRS}
+    )
+INCLUDE_DIRECTORIES(
+    ${CYNARA_PATH}/include
+    ${LIB_CREDS_SD_BUS_PATH}
+    )
+
+ADD_LIBRARY(${TARGET_LIB_CREDS_SD_BUS} SHARED ${LIB_CREDS_SD_BUS_SOURCES})
+
+SET_TARGET_PROPERTIES(
+    ${TARGET_LIB_CREDS_SD_BUS}
+    PROPERTIES
+        SOVERSION ${LIB_CREDS_SD_BUS_VERSION_MAJOR}
+        VERSION ${LIB_CREDS_SD_BUS_VERSION}
+    )
+
+TARGET_LINK_LIBRARIES(${TARGET_LIB_CREDS_SD_BUS}
+    ${SYSTEMD_DEP_LIBRARIES}
+    ${TARGET_LIB_CREDS_COMMONS}
+    )
+
+INSTALL(TARGETS ${TARGET_LIB_CREDS_SD_BUS} DESTINATION ${LIB_DIR})
diff --git a/src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp b/src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
new file mode 100644 (file)
index 0000000..24be99e
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ *  Copyright (c) 2018 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
+ */
+/**
+ * @file        src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
+ * @author      Monika Zielinska <m.zielinska3@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of internal libcynara-creds-sd-bus functions
+ */
+
+#include <cerrno>
+#include <cstring>
+
+#include <systemd/sd-bus.h>
+
+#include <cynara-error.h>
+#include <exceptions/TryCatch.h>
+
+#include <creds-sd-bus-inner.h>
+
+int copyStr(char **destStr, const std::string &str) {
+    if (!destStr) {
+        return CYNARA_API_INVALID_PARAM;
+    }
+    char *destTmp = strdup(str.c_str());
+    if (!destTmp) {
+        LOGE("strdup failed");
+        return CYNARA_API_OUT_OF_MEMORY;
+    }
+    *destStr = destTmp;
+    return CYNARA_API_SUCCESS;
+}
+
+int getPid(sd_bus *bus, const char *name, pid_t *pid) {
+    return Cynara::tryCatch([&bus, &name, &pid]() {
+        sd_bus_creds *creds;
+        int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_PID, &creds);
+        if (ret < 0) {
+            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        ret = sd_bus_creds_get_pid(creds, pid);
+        sd_bus_creds_unref(creds);
+
+        if (ret < 0) {
+            LOGE("Couldn't get pid from creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+        return CYNARA_API_SUCCESS;
+    });
+}
+
+int getClientPid(sd_bus *bus, const char *name, char **client) {
+    return Cynara::tryCatch([&bus, &name, &client]() {
+        pid_t pid;
+        int ret = getPid(bus, name, &pid);
+        if (ret != CYNARA_API_SUCCESS)
+            return ret;
+        return copyStr(client, std::to_string(pid));
+    });
+}
+
+int getClientSmackLabel(sd_bus *bus, const char *name, char **client) {
+    return Cynara::tryCatch([&bus, &name, &client]() {
+        sd_bus_creds *creds;
+        const char *label;
+
+        /* There's no API specially for Smack label, but SELINUX_CONTEXT maps to a standard Linux Security Label */
+        int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_SELINUX_CONTEXT, &creds);
+        if (ret < 0) {
+            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        ret = sd_bus_creds_get_selinux_context(creds, &label);
+        if (ret < 0) {
+            sd_bus_creds_unref(creds);
+            LOGE("Couldn't get smack label from creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        ret = copyStr(client, label);
+        sd_bus_creds_unref(creds);
+        return ret;
+    });
+}
+
+int getUserId(sd_bus *bus, const char *name, char **user) {
+    return Cynara::tryCatch([&bus, &name, &user]() {
+        sd_bus_creds *creds;
+        uid_t uid;
+        int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_UID, &creds);
+        if (ret < 0) {
+            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        ret = sd_bus_creds_get_uid(creds, &uid);
+        sd_bus_creds_unref(creds);
+        if (ret < 0) {
+            LOGE("Couldn't get uid from creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+        return copyStr(user, std::to_string(uid));
+    });
+}
+
+int getUserGid(sd_bus *bus, const char *name, char **user) {
+    return Cynara::tryCatch([&bus, &name, &user]() {
+        sd_bus_creds *creds;
+        gid_t gid;
+        int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_GID, &creds);
+        if (ret < 0) {
+            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        ret = sd_bus_creds_get_gid(creds, &gid);
+        sd_bus_creds_unref(creds);
+        if (ret < 0) {
+            LOGE("Couldn't get gid from creds: %s", std::strerror(-ret));
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+        return copyStr(user, std::to_string(gid));
+    });
+}
diff --git a/src/helpers/creds-sd-bus/creds-sd-bus-inner.h b/src/helpers/creds-sd-bus/creds-sd-bus-inner.h
new file mode 100644 (file)
index 0000000..7b2540f
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ *  Copyright (c) 2018 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
+ */
+/**
+ * @file        src/helpers/creds-sd-bus/creds-sd-bus-inner.h
+ * @author      Monika Zielinska <m.zielinska3@samsung.com>
+ * @version     1.0
+ * @brief       Definition of internal libcynara-creds-sd-bus functions
+ */
+
+#ifndef SRC_HELPERS_CREDSSDBUS_CREDSSDBUSINNER_H_
+#define SRC_HELPERS_CREDSSDBUS_CREDSSDBUSINNER_H_
+
+#include <systemd/sd-bus.h>
+
+int getClientSmackLabel(sd_bus *bus, const char *name, char **client);
+int getClientPid(sd_bus *bus, const char *name, char **client);
+int getUserId(sd_bus *bus, const char *name, char **user);
+int getUserGid(sd_bus *bus, const char *name, char **user);
+
+int getPid(sd_bus *bus, const char *name, pid_t *pid);
+
+#endif /* SRC_HELPERS_CREDSSDBUS_CREDSSDBUSINNER_H_ */
diff --git a/src/helpers/creds-sd-bus/creds-sd-bus.cpp b/src/helpers/creds-sd-bus/creds-sd-bus.cpp
new file mode 100644 (file)
index 0000000..a1791ee
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ *  Copyright (c) 2018 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
+ */
+/**
+ * @file        src/helpers/creds-sd-bus/creds-sd-bus.cpp
+ * @author      Monika Zielinska <m.zielinska3@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of external libcynara-creds-sd-bus API
+ */
+
+#include <attributes/attributes.h>
+#include <cynara-creds-commons.h>
+#include <cynara-creds-sd-bus.h>
+#include <cynara-error.h>
+#include <log/log.h>
+
+#include <creds-sd-bus-inner.h>
+
+CYNARA_API
+int cynara_creds_sd_bus_get_client(sd_bus *bus, const char *name,
+                                   enum cynara_client_creds method, char **client) {
+    if (bus == nullptr) {
+        LOGE("Invalid bus parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (name == nullptr) {
+        LOGE("Invalid name parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (client == nullptr) {
+        LOGE("Invalid client parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (method == cynara_client_creds::CLIENT_METHOD_DEFAULT) {
+        int ret = cynara_creds_get_default_client_method(&method);
+        if (ret != CYNARA_API_SUCCESS)
+            return ret;
+    }
+
+    switch (method) {
+        case cynara_client_creds::CLIENT_METHOD_SMACK:
+            return getClientSmackLabel(bus, name, client);
+        case cynara_client_creds::CLIENT_METHOD_PID:
+            return getClientPid(bus, name, client);
+        default:
+            return CYNARA_API_METHOD_NOT_SUPPORTED;
+    }
+}
+
+CYNARA_API
+int cynara_creds_sd_bus_get_user(sd_bus *bus, const char *name,
+                                 enum cynara_user_creds method, char **user) {
+    if (bus == nullptr) {
+        LOGE("Invalid bus parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (name == nullptr) {
+        LOGE("Invalid name parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (user == nullptr) {
+        LOGE("Invalid user parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (method == cynara_user_creds::USER_METHOD_DEFAULT) {
+        int ret = cynara_creds_get_default_user_method(&method);
+        if (ret != CYNARA_API_SUCCESS)
+            return ret;
+    }
+
+    switch (method) {
+        case cynara_user_creds::USER_METHOD_UID:
+            return getUserId(bus, name, user);
+        case cynara_user_creds::USER_METHOD_GID:
+            return getUserGid(bus, name, user);
+        default:
+            return CYNARA_API_METHOD_NOT_SUPPORTED;
+    }
+}
+
+CYNARA_API
+int cynara_creds_sd_bus_get_pid(sd_bus *bus, const char *name, pid_t *pid) {
+    if (bus == nullptr) {
+        LOGE("Invalid bus parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (name == nullptr) {
+        LOGE("Invalid name parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+
+    if (pid == nullptr) {
+        LOGE("Invalid pid parameter");
+        return CYNARA_API_INVALID_PARAM;
+    }
+    return getPid(bus, name, pid);
+}
index 64ef2f5805b66efe9233b030aa30d669b1226bf3..60b4b63ba678259508181832b3a524e75f3c8bcb 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2018 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.
@@ -14,6 +14,7 @@
 #
 # @file        CMakeLists.txt
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+# @author      Monika Zielinska <m.zielinska3@samsung.com>
 #
 
 IF (BUILD_COMMONS)
@@ -41,6 +42,7 @@ IF (BUILD_DBUS)
     INSTALL(FILES
         ${CYNARA_PATH}/include/cynara-creds-dbus.h
         ${CYNARA_PATH}/include/cynara-creds-gdbus.h
+        ${CYNARA_PATH}/include/cynara-creds-sd-bus.h
         DESTINATION ${INCLUDE_DIR}/cynara
         )
 ENDIF (BUILD_DBUS)
diff --git a/src/include/cynara-creds-sd-bus.h b/src/include/cynara-creds-sd-bus.h
new file mode 100644 (file)
index 0000000..2618aa3
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ *  Copyright (c) 2018 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
+ */
+/**
+ * @file        src/include/cynara-creds-sd-bus.h
+ * @author      Monika Zielinska <m.zielinska3@samsung.com>
+ * @version     1.0
+ * @brief       This file contains Cynara credentials helper APIs for sdbus clients.
+ * @example     cynara-creds-sd-bus.example
+ */
+
+#ifndef CYNARA_CREDS_SD_BUS_H
+#define CYNARA_CREDS_SD_BUS_H
+
+#include <systemd/sd-bus.h>
+
+#include <cynara-creds-commons.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \par Description:
+ * Creates a client identification string with given method. Client is a process connected to
+ * the bus and identified by the unique name.
+ *
+ * \par Purpose:
+ * Client identification string is required for cynara_check() and cynara_async_create_request()
+ * functions.
+ *
+ * \par Typical use case:
+ * The function is called before the call of cynara_check() or cynara_async_create_request()
+ * function. Returned string is used as client parameter in cynara_check() or
+ * cynara_async_create_request() function. String is released with free() function when it is no
+ * longer needed.
+ *
+ * \par Method of function operation:
+ * The function generates client string by calling sd_bus_get_name_creds function or by reading
+ * /proc/(pid)/attr/current file depending on chosen method.
+ *
+ * \par Sync (or) Async:
+ * This is a synchronous API.
+ *
+ * \par Thread safety:
+ * This function is NOT thread-safe. If functions from described API are called by multithreaded
+ * application from different threads, they must be put into mutex protected critical section.
+ *
+ * \par Important notes:
+ * Memory for returned user string is obtained with malloc(), and should be freed with free().
+ * Allocated string is returned only, when function succeeds.
+ * If method is CLIENT_METHOD_DEFAULT then it will be chosen based on Cynara configuration file.
+ *
+ * \param[in] bus Bus connection that manages incoming and outgoing messages
+ * \param[in] name Bus identifier of the client
+ * \param[in] method Method of client identifier creation
+ * \param[out] client Placeholder for allocated string containing client id
+ *
+ * \return CYNARA_API_SUCCESS on success
+ * \return CYNARA_API_INVALID_PARAM when client is NULL or uniqueName or client has wrong
+ *                                  value (i.e NULL or non-existing)
+ * \return CYNARA_API_METHOD_NOT_SUPPORTED when requested method is not supported
+ * \return CYNARA_API_CONFIGURATION_ERROR if the configuration file can not be opened or
+ *                                        there are errors in configuration file
+ * \return CYNARA_API_OUT_OF_MEMORY when there is error allocating memory
+ * \return CYNARA_API_UNKNOWN_ERROR if there is other error
+ */
+int cynara_creds_sd_bus_get_client(sd_bus *bus, const char *name,
+                                   enum cynara_client_creds method, char **client);
+
+/**
+ * \par Description:
+ * Creates a user identification string with given method. User is an executor of a process
+ * connected to the bus and identified by the unique name.
+ *
+ * \par Purpose:
+ * User identification string is required for cynara_check() and cynara_async_create_request()
+ * functions.
+ *
+ * \par Typical use case:
+ * The function is called before the call of cynara_check() or cynara_async_create_request()
+ * function. Returned string is used as user parameter in cynara_check() or
+ * cynara_async_create_request() function. String is released with free() function when it is no
+ * longer needed.
+ *
+ * \par Method of function operation:
+ * The function generates user string by calling sd_bus_get_name_creds function.
+ *
+ * \par Sync (or) Async:
+ * This is a synchronous API.
+ *
+ * \par Thread safety:
+ * This function is NOT thread-safe. If functions from described API are called by multithreaded
+ * application from different threads, they must be put into mutex protected critical section.
+ *
+ * \par Important notes:
+ * Memory for returned user string is obtained with malloc(), and should be freed with free().
+ * Allocated string is returned only, when function succeeds.
+ * If method is USER_METHOD_DEFAULT then it will be chosen based on Cynara configuration file.
+ *
+ * \param[in] bus Bus connection that manages incoming and outgoing messages
+ * \param[in] name Bus identifier of the client invoked by the user
+ * \param[in] method Method of client identifier creation
+ * \param[out] user Placeholder for allocated string containing user id
+ *
+ * \return CYNARA_API_SUCCESS on success
+ * \return CYNARA_API_INVALID_PARAM when user is NULL or uniqueName or client has wrong
+ *                                  value (i.e NULL or non-existing)
+ * \return CYNARA_API_METHOD_NOT_SUPPORTED when requested method is not supported
+ * \return CYNARA_API_CONFIGURATION_ERROR if the configuration file can not be opened or
+ *                                        there are errors in configuration file
+ * \return CYNARA_API_OUT_OF_MEMORY when there is error allocating memory
+ * \return CYNARA_API_UNKNOWN_ERROR if there is other error
+ */
+int cynara_creds_sd_bus_get_user(sd_bus *bus, const char *name,
+                                 enum cynara_user_creds method, char **user);
+
+/**
+ * \par Description:
+ * Return PID of a process connected to the bus and identified by the unique name.
+ *
+ * \par Purpose:
+ * PID may be used for client_session creation with cynara_helper_session_from_pid() function
+ * from libcynara-helper-session library. Client_session is needed for cynara_check()
+ * and cynara_async_create_request() functions.
+ *
+ * \par Typical use case:
+ * The function is called before the call of cynara_helper_session_from_pid() function.
+ *
+ * \par Method of function operation:
+ * The function reads PID of the client by calling sd_bus_creds_get_pid methods.
+ *
+ * \par Sync (or) Async:
+ * This is a synchronous API.
+ *
+ * \par Thread safety:
+ * This function is NOT thread-safe. If functions from described API are called by multithreaded
+ * application from different threads, they must be put into mutex protected critical section.
+ *
+ * \param[in] bus Bus connection that manages incoming and outgoing messages
+ * \param[in] name Bus identifier of the client invoked by the user
+ * \param[out] pid Placeholder for PID returned by function
+ *
+ * \return CYNARA_API_SUCCESS on success
+ * \return CYNARA_API_UNKNOWN_ERROR when function fails because of unknown error
+ */
+int cynara_creds_sd_bus_get_pid(sd_bus *bus, const char *name, pid_t *pid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CYNARA_CREDS_SD_BUS_H */