Add sd_bus helper API to fetch all credentials in one call
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 31 Jan 2023 18:50:33 +0000 (19:50 +0100)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 8 Feb 2023 16:02:21 +0000 (17:02 +0100)
Change-Id: I352a0387f237559447871ec79bb0b96322ceaa0e

src/helpers/creds-commons/Credentials.h
src/helpers/creds-commons/creds-inner.cpp
src/helpers/creds-dbus/creds-dbus.cpp
src/helpers/creds-gdbus/creds-gdbus.cpp
src/helpers/creds-sd-bus/CMakeLists.txt
src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp [deleted file]
src/helpers/creds-sd-bus/creds-sd-bus-inner.h [deleted file]
src/helpers/creds-sd-bus/creds-sd-bus.cpp
src/include/cynara-creds-sd-bus.h

index ad34718b096b8d2d8ded13a6a0ee0447f8bf8505..16fc2b71d7789829a37b0939ec4a36b9cafdef05 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2022-2023 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
@@ -35,17 +35,20 @@ public:
     virtual bool isPidSet(void) const { return m_isPidSet; }
     virtual bool isUidSet(void) const { return m_isUidSet; }
     virtual bool isSecurityLabelSet(void) const { return m_isSecuritySet; }
+    virtual bool isGidSet(void) const {return m_isGidSet; }
 
     virtual pid_t getPid(void) const = 0;
     virtual char* getPidStr(void) const = 0;
     virtual char* getUidStr(void) const = 0;
     virtual char* getSecurityLabel(void) const = 0;
+    virtual char* getGidStr(void) const = 0;
 
     virtual ~Credentials() {}
 protected:
     bool m_isPidSet = false;
     bool m_isUidSet = false;
     bool m_isSecuritySet = false;
+    bool m_isGidSet = false;
 };
 
 #endif /* SRC_HELPERS_CREDS_COMMONS_CREDENTIALS_H_ */
index a7266840764fb7d5541c11076f1bea29c44ca968..bcdc802b3844b05ff105a154b903a68656e3ce02 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2022-2023 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
@@ -78,20 +78,20 @@ int cynara_creds_inner_get(const Credentials &credentials,
         if (clientMethod == cynara_client_creds::CLIENT_METHOD_DEFAULT) {
             int ret = cynara_creds_get_default_client_method(&clientMethod);
             if (ret != CYNARA_API_SUCCESS) {
-                LOGD("Failed to get default client method: %d", ret);
+                LOGE("Failed to get default client method: %d", ret);
                 return ret;
             }
         }
         switch (clientMethod) {
         case cynara_client_creds::CLIENT_METHOD_SMACK:
             if (!credentials.isSecurityLabelSet()) {
-                return CYNARA_API_UNKNOWN_ERROR;
+                return CYNARA_API_METHOD_NOT_SUPPORTED;
             }
             *client = credentials.getSecurityLabel();
             break;
         case cynara_client_creds::CLIENT_METHOD_PID:
             if (!credentials.isPidSet()) {
-                return CYNARA_API_UNKNOWN_ERROR;
+                return CYNARA_API_METHOD_NOT_SUPPORTED;
             }
             *client = credentials.getPidStr();
             break;
@@ -105,16 +105,23 @@ int cynara_creds_inner_get(const Credentials &credentials,
         if (userMethod == cynara_user_creds::USER_METHOD_DEFAULT) {
             int ret = cynara_creds_get_default_user_method(&userMethod);
             if (ret != CYNARA_API_SUCCESS) {
+                LOGE("Failed to get default user method: %d", ret);
                 return ret;
             }
         }
         switch (userMethod) {
         case cynara_user_creds::USER_METHOD_UID:
             if (!credentials.isUidSet()) {
-                return CYNARA_API_UNKNOWN_ERROR;
+                return CYNARA_API_METHOD_NOT_SUPPORTED;
             }
             *user = credentials.getUidStr();
             break;
+        case cynara_user_creds::USER_METHOD_GID:
+            if (!credentials.isGidSet()) {
+                return CYNARA_API_METHOD_NOT_SUPPORTED;
+            }
+            *user = credentials.getGidStr();
+            break;
         default:
             return CYNARA_API_METHOD_NOT_SUPPORTED;
         }
@@ -126,5 +133,6 @@ int cynara_creds_inner_get(const Credentials &credentials,
         }
         *pid = credentials.getPid();
     }
+
     return CYNARA_API_SUCCESS;
 }
index 81c1d3476b69a179d233fd192595ccbc157d5e88..676a74ad274798806c850892ada91604852f360d 100644 (file)
@@ -145,6 +145,9 @@ public:
     char *getSecurityLabel(void) const {
         return strdup(m_securityLabel);
     }
+    char *getGidStr(void) const {
+        return nullptr;
+    }
 
 private:
     dbus_uint32_t m_pid;
index e2b7711e0837c24aae2d86ee50e7eda14770d0e6..cf5f99b89f87b965925899be586676a8c1726b10 100644 (file)
@@ -106,6 +106,10 @@ public:
     virtual char *getSecurityLabel(void) const {
         return g_strdup(m_securityLabel);
     }
+    virtual char *getGidStr(void) const {
+        return nullptr;
+    }
+
 private:
     guint32 m_pid;
     guint32 m_uid;
index d8b615c17464de985d81b6b159062621b82b8eae..5a7f129f41d1ff769ded5dd2ba39c1d840f5368e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2018-2020 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2018-2023 Samsung Electronics Co., Ltd All Rights Reserved
 #
 # This file is licensed under the terms of MIT License or the Apache License
 # Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
@@ -25,10 +25,11 @@ SET(LIB_CREDS_SD_BUS_VERSION_MAJOR 0)
 SET(LIB_CREDS_SD_BUS_VERSION ${LIB_CREDS_SD_BUS_VERSION_MAJOR}.18.0)
 
 SET(LIB_CREDS_SD_BUS_PATH ${CYNARA_PATH}/helpers/creds-sd-bus)
+SET(LIB_CREDS_COMMONS_PATH ${CYNARA_PATH}/helpers/creds-commons)
 
 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
+    ${LIB_CREDS_COMMONS_PATH}/creds-inner.cpp
     )
 
 INCLUDE_DIRECTORIES(
@@ -38,6 +39,7 @@ INCLUDE_DIRECTORIES(
 INCLUDE_DIRECTORIES(
     ${CYNARA_PATH}/include
     ${LIB_CREDS_SD_BUS_PATH}
+    ${LIB_CREDS_COMMONS_PATH}
     )
 
 ADD_LIBRARY(${TARGET_LIB_CREDS_SD_BUS} SHARED ${LIB_CREDS_SD_BUS_SOURCES})
diff --git a/src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp b/src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
deleted file mode 100644 (file)
index 2420463..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * This file is licensed under the terms of MIT License or the Apache License
- * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
- * See the LICENSE file or the notice below for Apache License Version 2.0
- * details.
- *
- * 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 <systemd/sd-bus.h>
-
-#include <cynara-error.h>
-#include <exceptions/TryCatch.h>
-#include <log/log.h>
-
-#include <error/SafeStrError.h>
-
-#include <creds-sd-bus-inner.h>
-
-namespace {
-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;
-}
-} // namespace
-
-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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: " << Cynara::safeStrError(-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
deleted file mode 100644 (file)
index 34fc36c..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * This file is licensed under the terms of MIT License or the Apache License
- * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
- * See the LICENSE file or the notice below for Apache License Version 2.0
- * details.
- *
- * 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_ */
index 8927cdf5e137f61e978746b2edfd313a4b75fe1e..ea08e33c9cb15beda13ad4ab7c4e99cb2693e3f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2018-2023 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
  * @brief       Implementation of external libcynara-creds-sd-bus API
  */
 
+#include <errno.h>
 #include <attributes/attributes.h>
+#include <memory>
+#include <systemd/sd-bus.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>
+#include <Credentials.h>
+#include <creds-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;
-    }
+namespace {
 
-    if (name == nullptr) {
-        LOGE("Invalid name parameter");
-        return CYNARA_API_INVALID_PARAM;
-    }
+class SDBusCredentials : public Credentials {
+public:
+    SDBusCredentials() : m_pid(0), m_uid(0), m_securityLabel(nullptr) {}
 
-    if (client == nullptr) {
-        LOGE("Invalid client parameter");
-        return CYNARA_API_INVALID_PARAM;
+    int init(sd_bus *bus, const char *name) {
+        sd_bus_creds *creds;
+
+
+        uint64_t mask = SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_PID |
+            SD_BUS_CREDS_SELINUX_CONTEXT | SD_BUS_CREDS_UID |
+            SD_BUS_CREDS_GID;
+        int ret = sd_bus_get_name_creds(bus, name, mask, &creds);
+        std::unique_ptr<sd_bus_creds, decltype(sd_bus_creds_unref)*>
+            credsPtr(creds, sd_bus_creds_unref);
+        if (ret < 0) {
+            LOGE("sd_bus_get_name_creds failed with: %d", ret);
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        m_isPidSet = (ret = sd_bus_creds_get_pid(creds, &m_pid)) >= 0;
+        if (ret < 0) {
+            LOGE("sd_bus_creds_get_pid failed with: %d", ret);
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        m_isUidSet = (ret = sd_bus_creds_get_uid(creds, &m_uid)) >= 0;
+        if (ret < 0) {
+            LOGE("sd_bus_creds_get_uid failed with: %d", ret);
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        m_isGidSet = (ret = sd_bus_creds_get_gid(creds, &m_gid)) >= 0;
+        if (ret < 0) {
+            LOGE("sd_bus_creds_get_gid failed with: %d", ret);
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        const char *label;
+        m_isSecuritySet = (ret = sd_bus_creds_get_selinux_context(creds, &label)) >= 0;
+        if (ret < 0) {
+            LOGE("sd_bus_creds_get_selinux_content failed with: %d", ret);
+            return CYNARA_API_UNKNOWN_ERROR;
+        }
+
+        m_securityLabel = strdup(label);
+
+        return CYNARA_API_SUCCESS;
     }
 
-    if (method == cynara_client_creds::CLIENT_METHOD_DEFAULT) {
-        int ret = cynara_creds_get_default_client_method(&method);
-        if (ret != CYNARA_API_SUCCESS)
-            return ret;
+    virtual ~SDBusCredentials() {
+        free(m_securityLabel);
     }
 
-    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;
+    pid_t getPid(void) const { return m_pid; }
+    char *getPidStr(void) const {
+        return strdup(std::to_string(m_pid).c_str());
+    }
+    char *getUidStr(void) const {
+        return strdup(std::to_string(m_uid).c_str());
     }
+    char *getSecurityLabel(void) const {
+        return strdup(m_securityLabel);
+    }
+    char *getGidStr(void) const {
+        return strdup(std::to_string(m_gid).c_str());
+    }
+
+    pid_t m_pid;
+    uid_t m_uid;
+    gid_t m_gid;
+    char *m_securityLabel;
+};
+
 }
 
 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");
+int cynara_creds_sd_bus_get(sd_bus *bus, const char *name,
+                            const enum cynara_client_creds *pClientMethod, char **client,
+                            const enum cynara_user_creds *pUserMethod, char **user,
+                            pid_t *pid) {
+    if (bus == nullptr || name == nullptr) {
         return CYNARA_API_INVALID_PARAM;
     }
 
-    if (name == nullptr) {
-        LOGE("Invalid name parameter");
-        return CYNARA_API_INVALID_PARAM;
+    SDBusCredentials credentials;
+    int ret = credentials.init(bus, name);
+    if (ret != CYNARA_API_SUCCESS) {
+        return ret;
     }
 
-    if (user == nullptr) {
-        LOGE("Invalid user parameter");
+    ret = cynara_creds_inner_get(credentials, pClientMethod, client,
+                                 pUserMethod, user, pid);
+
+    LOGW("cynara_creds_sd_bus_get returns %d", ret);
+    return ret;
+}
+
+CYNARA_API
+int cynara_creds_sd_bus_get_default(sd_bus *bus, const char *name,
+                                    char **client, char **user, pid_t *pid) {
+    if (bus == nullptr || name == nullptr) {
         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;
+    SDBusCredentials credentials;
+    int ret = credentials.init(bus, name);
+    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;
-    }
+    ret = cynara_creds_inner_get_default(credentials, client, user, pid);
+    LOGD("cynara_creds_sd_bus_get_default returns %d", ret);
+    return ret;
 }
 
 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;
-    }
+int cynara_creds_sd_bus_get_client(sd_bus *bus, const char *name,
+                                   enum cynara_client_creds method, char **client) {
+    int ret = cynara_creds_sd_bus_get(bus, name, &method, client, nullptr,
+                                    nullptr, nullptr);
+    LOGD("cynara_creds_sd_bus_get_client returns %d", ret);
+    return ret;
+}
 
-    if (name == nullptr) {
-        LOGE("Invalid name parameter");
-        return CYNARA_API_INVALID_PARAM;
-    }
+CYNARA_API
+int cynara_creds_sd_bus_get_user(sd_bus *bus, const char *name,
+                                 enum cynara_user_creds method, char **user) {
+    int ret = cynara_creds_sd_bus_get(bus, name, nullptr, nullptr, &method,
+                                     user, nullptr);
+    LOGD("cynara_creds_sd_bus_get_user returns %d", ret);
+    return ret;
+}
 
-    if (pid == nullptr) {
-        LOGE("Invalid pid parameter");
-        return CYNARA_API_INVALID_PARAM;
-    }
-    return getPid(bus, name, pid);
+CYNARA_API
+int cynara_creds_sd_bus_get_pid(sd_bus *bus, const char *name, pid_t *pid) {
+    int ret = cynara_creds_sd_bus_get(bus, name, nullptr, nullptr, nullptr,
+                                     nullptr, pid);
+    LOGD("cynara_creds_sd_bus_get_pid returns %d", ret);
+    return ret;
 }
index 26591d1c4c8f956bb732cab8a1873afdc3d43b8d..7975e2a0d13d73f64311a721b1651ece5ffeb6b4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2018-2023 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * This file is licensed under the terms of MIT License or the Apache License
  * Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
 extern "C" {
 #endif
 
+/**
+ * \par Description:
+ * Creates a client identification string, user identification string with default methods
+ * from Cynara configuration file, or PID identification. Client is a process identified
+ * by the unique name at the other side of the dbus connection. User is an executor of
+ * process at the other side of the socket. User can fetch any of the three, function will fail
+*  only when none of the client, user or pid are requested.
+ *
+ * \par Purpose:
+ * Client and user identification strings are required for cynara_check() and
+ * cynara_async_create_request() functions. 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_check() or cynara_async_create_request()
+ * function. Returned PID may be used to create client_session using function
+ * cynara_helper_session_from_pid(). Returned strings are used as client, user and pid parameters
+ * in cynara_check() or cynara_async_create_request() function.
+ * Strings are released with free() function when they are no longer needed.
+ *
+ * \par Method of function operation:
+ * The function generates client string, user string and PID by calling a method from
+ * sd_bus_get_name_creds API.
+ *
+ * \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 all strings should be freed with free().
+ * Allocated strings are returned only, when function succeeds.
+ *
+ * \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] client Placeholder for allocated string containing client id
+ * \param[out] user Placeholder for allocated string containing client id
+ * \param[out] pid Placeholder for PID returned by function
+ *
+ * \return CYNARA_API_SUCCESS on success
+ * \return CYNARA_API_INVALID_PARAM when all client, user and pid are NULL or uniqueName
+ *                                  and connection have wrong value (i.e NULL or non-existing)
+ *                                  or given method is NULL for identifier thas is not.
+ * \return CYNARA_API_CONFIGURATION_ERROR if the configuration file can not be opened or
+ *                                        there are errors in configuration file
+ * \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_default(sd_bus *bus, const char *name,
+                                    char **client, char **user, pid_t *pid);
+
+/**
+ * \par Description:
+ * Creates a client identification string with given method, user identification string with
+ * given method, or PID identification. Client is a process identified by the unique name
+ * at the other side of the sd-bus connection. User is an executor of process at the other side
+ * of the sd-bus connection. User can fetch any of the three, function will fail only when none
+ * of the client, user or pid are requested.
+ *
+ * \par Purpose:
+ * Client and user identification strings are required for cynara_check() and
+ * cynara_async_create_request() functions. 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_check() or cynara_async_create_request()
+ * function. Returned PID may be used to create client_session using function
+ * cynara_helper_session_from_pid(). Returned strings are used as client, user and pid
+ * parameters in cynara_check() or cynara_async_create_request() function.
+ * Strings are released with free() function when they are no longer needed.
+ *
+ * \par Method of function operation:
+ * The function generates client string, user string and PID by calling a method from
+ * sd_bus_get_name_creds API.
+ *
+ * \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 all strings should be freed with free().
+ * Allocated strings are returned only, when function succeeds.
+ * If methods are CLIENT_METHOD_DEFAULT or 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] pClientMethod Pointer to method of client identifier creation
+ * \param[out] client Placeholder for allocated string containing client id
+ * \param[in] pUserMethod Pointer to method of user identifier creation
+ * \param[out] user Placeholder for allocated string containing client id
+ * \param[out] pid Placeholder for PID returned by function
+ *
+ * \return CYNARA_API_SUCCESS on success
+ * \return CYNARA_API_INVALID_PARAM when all client, user and pid are NULL or uniqueName
+ *                                  and connection have wrong value (i.e NULL or non-existing)
+ *                                  or given method is NULL for identifier thas is not.
+ * \return CYNARA_API_CONFIGURATION_ERROR if the configuration file can not be opened or
+ *                                        there are errors in configuration file
+ * \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(sd_bus *bus, const char *uniqueName,
+                            const cynara_client_creds *pClientMethod, char **client,
+                            const cynara_user_creds *pUserMethod, char **user,
+                            pid_t *pid);
+
 /**
  * \par Description:
  * Creates a client identification string with given method. Client is a process connected to