From a41af0d1e2a95773389dcf6a67c2213b1e6debc1 Mon Sep 17 00:00:00 2001 From: Radoslaw Bartosiak Date: Wed, 3 Sep 2014 21:48:25 +0200 Subject: [PATCH] Prepare libcynara-creds-dbus for implementation Change-Id: Ibdecf8790b7d296e35c063c52202a4a672ad9e9a --- src/helpers/creds-dbus/CMakeLists.txt | 1 + src/helpers/creds-dbus/creds-dbus-inner.cpp | 58 +++++++++++++ src/helpers/creds-dbus/creds-dbus-inner.h | 39 +++++++++ src/helpers/creds-dbus/creds-dbus.cpp | 51 +++++++++++- src/include/cynara-creds-dbus.h | 125 +++++++++++++++++++++++++++- 5 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 src/helpers/creds-dbus/creds-dbus-inner.cpp create mode 100644 src/helpers/creds-dbus/creds-dbus-inner.h diff --git a/src/helpers/creds-dbus/CMakeLists.txt b/src/helpers/creds-dbus/CMakeLists.txt index 4e19a8b..c80d301 100644 --- a/src/helpers/creds-dbus/CMakeLists.txt +++ b/src/helpers/creds-dbus/CMakeLists.txt @@ -25,6 +25,7 @@ SET(LIB_CREDS_DBUS_PATH ${CYNARA_PATH}/helpers/creds-dbus) SET(LIB_CREDS_DBUS_SOURCES ${LIB_CREDS_DBUS_PATH}/creds-dbus.cpp + ${LIB_CREDS_DBUS_PATH}/creds-dbus-inner.cpp ) PKG_CHECK_MODULES(LIB_CREDS_DBUS_DEP diff --git a/src/helpers/creds-dbus/creds-dbus-inner.cpp b/src/helpers/creds-dbus/creds-dbus-inner.cpp new file mode 100644 index 0000000..f72f073 --- /dev/null +++ b/src/helpers/creds-dbus/creds-dbus-inner.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2014 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 creds-dbus-inner.cpp + * @author Radoslaw Bartosiak + * @author Aleksander Zdyb + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief Implementation of internal libcynara-creds-dbus functions + */ + +#include + +#include + +#include "creds-dbus-inner.h" + +int getClientSmackLabel(DBusConnection *connection UNUSED, const char *uniqueName UNUSED, + char **client UNUSED) { + //todo + return CYNARA_API_METHOD_NOT_SUPPORTED; +} + +int getClientPid(DBusConnection *connection UNUSED, const char *uniqueName UNUSED, + char **client UNUSED) { + //todo + return CYNARA_API_METHOD_NOT_SUPPORTED; +} + +int getUserId(DBusConnection *connection UNUSED, const char *uniqueName UNUSED, + char **user UNUSED) { + //todo + return CYNARA_API_METHOD_NOT_SUPPORTED; +} + +int getUserGid(DBusConnection *connection UNUSED, const char *uniqueName UNUSED, + char **user UNUSED) { + //todo + return CYNARA_API_METHOD_NOT_SUPPORTED; +} + +int getPid(DBusConnection *connection UNUSED, const char *uniqueName UNUSED, pid_t *pid UNUSED) { + //todo + return CYNARA_API_METHOD_NOT_SUPPORTED; +} diff --git a/src/helpers/creds-dbus/creds-dbus-inner.h b/src/helpers/creds-dbus/creds-dbus-inner.h new file mode 100644 index 0000000..4c51119 --- /dev/null +++ b/src/helpers/creds-dbus/creds-dbus-inner.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 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 creds-dbus-inner.h + * @author Radoslaw Bartosiak + * @author Aleksander Zdyb + * @author Lukasz Wojciechowski + * @version 1.0 + * @brief Definition of internal external libcynara-creds-dbus functions + */ + +#ifndef SRC_HELPERS_CREDSDBUS_CREDSDBUSINNER_H_ +#define SRC_HELPERS_CREDSDBUS_CREDSDBUSINNER_H_ + +#include +#include + +int getClientSmackLabel(DBusConnection *connection, const char *uniqueName, char **client); +int getClientPid(DBusConnection *connection, const char *uniqueName, char **client); + +int getUserId(DBusConnection *connection, const char *uniqueName, char **user); +int getUserGid(DBusConnection *connection, const char *uniqueName, char **user); + +int getPid(DBusConnection *connection, const char *uniqueName, pid_t *pid); + +#endif /* SRC_HELPERS_CREDSDBUS_CREDSDBUSINNER_H_ */ diff --git a/src/helpers/creds-dbus/creds-dbus.cpp b/src/helpers/creds-dbus/creds-dbus.cpp index 535ca84..344cd02 100644 --- a/src/helpers/creds-dbus/creds-dbus.cpp +++ b/src/helpers/creds-dbus/creds-dbus.cpp @@ -15,9 +15,58 @@ */ /* * @file creds-dbus.cpp + * @author Radoslaw Bartosiak + * @author Aleksander Zdyb * @author Lukasz Wojciechowski * @version 1.0 * @brief Implementation of external libcynara-creds-dbus API */ -// Empty initial file + +#include + +#include + +#include +#include +#include + +CYNARA_API +int cynara_creds_dbus_get_client(DBusConnection *connection, const char *uniqueName, + enum cynara_client_creds method, char **client) { + if (connection == nullptr || uniqueName == nullptr || client == nullptr) + return CYNARA_API_INVALID_PARAM; + + switch (method) { + case cynara_client_creds::CLIENT_METHOD_SMACK: + return getClientSmackLabel(connection, uniqueName, client); + case cynara_client_creds::CLIENT_METHOD_PID: + return getClientPid(connection, uniqueName, client); + default: + return CYNARA_API_METHOD_NOT_SUPPORTED; + } +} + +CYNARA_API +int cynara_creds_dbus_get_user(DBusConnection *connection, const char *uniqueName, + enum cynara_user_creds method, char **user) { + if (connection == nullptr || uniqueName == nullptr || user == nullptr) + return CYNARA_API_INVALID_PARAM; + + switch (method) { + case cynara_user_creds::USER_METHOD_UID: + return getUserId(connection, uniqueName, user); + case cynara_user_creds::USER_METHOD_GID: + return getUserGid(connection, uniqueName, user); + default: + return CYNARA_API_METHOD_NOT_SUPPORTED; + } +} + +CYNARA_API +int cynara_creds_dbus_get_pid(DBusConnection *connection, const char *uniqueName, pid_t *pid) { + if (connection == nullptr || uniqueName == nullptr) + return CYNARA_API_INVALID_PARAM; + + return getPid(connection, uniqueName, pid); +} diff --git a/src/include/cynara-creds-dbus.h b/src/include/cynara-creds-dbus.h index 925a829..904e4dd 100644 --- a/src/include/cynara-creds-dbus.h +++ b/src/include/cynara-creds-dbus.h @@ -16,6 +16,7 @@ /* * @file cynara-creds-dbus.h * @author Lukasz Wojciechowski + * @author Radoslaw Bartosiak * @version 1.0 * @brief This file contains Cynara credentials helper APIs for dbus clients. */ @@ -24,11 +25,133 @@ #ifndef CYNARA_CREDS_DBUS_H #define CYNARA_CREDS_DBUS_H +#include +#include + +#include "cynara-creds-commons.h" + #ifdef __cplusplus extern "C" { #endif -/* empty initial file */ +/** + * \par Description: + * Creates a client identification string with given method. Client is a process identified by the + * unique name at the other side of the dbus connection. + * + * \par Purpose: + * Client identification string is required for cynara_check() and cynara_async_check() functions. + * + * \par Typical use case: + * The function is called before the call of one of ...check() functions. + * Returned string is used as client parameter in ...check() 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 a method from DBus Interface + * ("org.freedesktop.DBus") which is placed on system bus ("org.freedesktop.DBus"). + * + * \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 client string is obtained with malloc(), and should be freed with free(). + * Allocated string is returned only, when function succeeds. + * + * \param[in] connection DBus connection to a bus. It manages incomming and outgoing messages + * \param[in] uniqueName DBus 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 + * CYNARA_API_INVALID_PARAM when client is NULL or uniqueName or client has wrong + * value (i.e NULL or non-existing) + * CYNARA_API_METHOD_NOT_SUPPORTED when requested method is not supported + * CYNARA_API_OUT_OF_MEMORY when there was error allocating memory + */ +int cynara_creds_dbus_get_client(DBusConnection *connection, const char *uniqueName, + enum cynara_client_creds method, char **client); + +/** + * \par Description: + * Creates a user identification string with given method. User is an executor of process + * at the other side of socket. + * + * \par Purpose: + * User identification string is required for cynara_check() and cynara_async_check() functions. + * + * \par Typical use case: + * The function is called before the call of one of ...check() functions. + * Returned string is used as user parameter in ...check() 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 a method from DBus Interface + * ("org.freedesktop.DBus") which is placed on system bus ("org.freedesktop.DBus"). + * + * \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. + * + * \param[in] connection DBus connection to a bus. It manages incomming and outgoing messages + * \param[in] uniqueName DBus 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 + * CYNARA_API_INVALID_PARAM when user is NULL or connection is not valid DBus connection or + * uniqueName does not represent a process conected to the DBus + * CYNARA_API_METHOD_NOT_SUPPORTED when requested method is not supported + * CYNARA_API_OUT_OF_MEMORY when there was error allocating memory + */ +int cynara_creds_dbus_get_user(DBusConnection *connection, const char *uniqueName, + enum cynara_user_creds method, char **user); + +/** + * \par Description: + * Return PID of a proces identified by the unique name at the other side of the dbus connection. + * + * \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_check() 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 peer by calling a method from DBus Interface + * ("org.freedesktop.DBus") which is placed on system bus ("org.freedesktop.DBus") + * with "GetConnectionUnixProcessID" argument. + * + * \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] connection DBus connection to a bus. It manages incomming and outgoing messages + * \param[in] uniqueName DBus identifier of the client invoked by the user + * \param[out] pid Placeholder for PID returned by function + * + * \return CYNARA_API_SUCCESS on success + * CYNARA_API_INVALID_PARAM when socket_fd is not valid connected socket descriptor + * CYNARA_API_UNKNOWN_ERROR when system function fails in incredible situation + * CYNARA_API_OUT_OF_MEMORY when there was error allocating memory + */ +int cynara_creds_dbus_get_pid(DBusConnection *connection, const char *uniqueName, pid_t *pid); #ifdef __cplusplus } -- 2.7.4