diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/CMakeLists.txt | 93 | ||||
-rw-r--r-- | server/inc/NotificationServer.h | 43 | ||||
-rw-r--r-- | server/inc/PrivacyInfoService.h | 64 | ||||
-rw-r--r-- | server/inc/PrivacyManagerDaemon.h | 43 | ||||
-rw-r--r-- | server/inc/PrivacyManagerServer.h | 67 | ||||
-rw-r--r-- | server/inc/SocketService.h | 84 | ||||
-rw-r--r-- | server/inc/privacy_manager_daemon.h | 37 | ||||
-rw-r--r-- | server/src/CMakeLists.txt | 73 | ||||
-rw-r--r-- | server/src/NotificationServer.cpp | 103 | ||||
-rw-r--r-- | server/src/PrivacyManagerDaemon.cpp | 80 | ||||
-rw-r--r-- | server/src/PrivacyManagerServer.cpp | 370 | ||||
-rw-r--r-- | server/src/SocketService.cpp | 386 | ||||
-rw-r--r-- | server/src/main.cpp | 37 | ||||
-rw-r--r-- | server/src/privacy_manager_daemon.cpp | 46 | ||||
-rw-r--r-- | server/src/service/PrivacyInfoService.cpp | 133 |
15 files changed, 1659 insertions, 0 deletions
diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt new file mode 100644 index 0000000..e2ad49a --- /dev/null +++ b/server/CMakeLists.txt @@ -0,0 +1,93 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(privacy-manager) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR "\${prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include") + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED dlog sqlite3 dbus-1 dbus-glib-1 db-util pkgmgr-info capi-system-info security-server libsmack vconf) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS_PROFILING " -g -pg") +SET(CMAKE_CXX_FLAGS_PROFILING " -std=c++0x -g -pg") +SET(CMAKE_C_FLAGS_DEBUG " -g") +SET(CMAKE_CXX_FLAGS_DEBUG " -std=c++0x -g") +SET(CMAKE_C_FLAGS_RELEASE " -g") +SET(CMAKE_CXX_FLAGS_RELEASE " -std=c++0x -g") +SET(CMAKE_C_FLAGS_CCOV " -g --coverage") +SET(CMAKE_CXX_FLAGS_CCOV " -std=c++0x -g --coverage") + +SET(server_src_dir "${CMAKE_SOURCE_DIR}/server/src") +SET(server_include_dir "${CMAKE_SOURCE_DIR}/server/inc/") +SET(common_src_dir "${CMAKE_SOURCE_DIR}/common/src/") +SET(common_include_dir "${CMAKE_SOURCE_DIR}/common/inc/") +SET(dbus_include_dir "/usr/include/dbus-1.0") + +## Additional flag +ADD_DEFINITIONS("-fvisibility=hidden") +ADD_DEFINITIONS("-Wall -Werror") +ADD_DEFINITIONS("-DDLOG_ERROR_ENABLED") +OPTION (FILTER_LISTED_PKG "FILTER PKG BY LIST" ON) +IF(FILTER_LISTED_PKG) + MESSAGE("FILTER PKGs BY FILTERING LIST") + ADD_DEFINITIONS("-D__FILTER_LISTED_PKG") +ENDIF(FILTER_LISTED_PKG) + +################################################################################################### +## for privacy-manager-server (executable) +INCLUDE_DIRECTORIES( + ${pkgs_INCLUDE_DIRS} + ${server_include_dir} + ${common_include_dir} + ${dbus_include_dir} + /usr/include/security-server/ + ) + +SET(PRIVACY_MANAGER_SERVER_SOURCES + ${common_src_dir}/SocketConnection.cpp + ${common_src_dir}/SocketStream.cpp + ${common_src_dir}/PrivacyDb.cpp + ${common_src_dir}/PrivacyIdInfo.cpp + # ${server_src_dir}/main.cpp + ${server_src_dir}/SocketService.cpp + ${server_src_dir}/PrivacyManagerDaemon.cpp + ${server_src_dir}/service/PrivacyInfoService.cpp + ${server_src_dir}/PrivacyManagerServer.cpp + ${server_src_dir}/NotificationServer.cpp + ${server_src_dir}/privacy_manager_daemon.cpp + ) +SET(PRIVACY_MANAGER_SERVER_HEADERS + ${server_include_dir}/privacy_manager_daemon.h + # ${server_include_dir}/SocketService.h + # ${server_include_dir}/PrivacyManagerDaemon.h + # ${common_include_dir}/SocketConnection.h + # ${common_include_dir}/SocketConnection.h +) +SET(PRIVACY_MANAGER_SERVER_LDFLAGS " -module -avoid-version ") +SET(PRIVACY_MANAGER_SERVER_CFLAGS " ${CFLAGS} -fPIC ") +#SET(PRIVACY_MANAGER_SERVER_LIBADD " ") + +ADD_DEFINITIONS("-DLOG_TAG=\"PRIVACY-MANAGER-SERVER\"") +ADD_LIBRARY(privacy-manager-server SHARED ${PRIVACY_MANAGER_SERVER_SOURCES}) +TARGET_LINK_LIBRARIES(privacy-manager-server ${pkgs_LDFLAGS} ${pkgs_LIBRARIES}) +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES COMPILE_FLAGS "${PRIVACY_MANAGER_SERVER_CFLAGS}") +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES SOVERSION ${API_VERSION}) +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES VERSION ${VERSION}) +################################################################################################### + +SET(PC_NAME privacy-manager-server) +SET(PC_DESCRIPTION "Privacy Manager Server API") +SET(PC_LDFLAGS -lprivacy-manager-server) +SET(PC_CFLAGS -I\${includedir}/privacy_manager) + +CONFIGURE_FILE(../privacy-manager-server.pc.in privacy-manager-server.pc @ONLY) + +INSTALL(TARGETS privacy-manager-server DESTINATION ../lib COMPONENT RuntimeLibraries) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/privacy-manager-server.pc DESTINATION ../lib/pkgconfig) +INSTALL(FILES ${PRIVACY_MANAGER_SERVER_HEADERS} DESTINATION ../include/privacy_manager) diff --git a/server/inc/NotificationServer.h b/server/inc/NotificationServer.h new file mode 100644 index 0000000..bd91953 --- /dev/null +++ b/server/inc/NotificationServer.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013 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 _NOTIFICATION_SERVER_H_ +#define _NOTIFICATION_SERVER_H_ + +#include <string> +#include <memory> +#include <list> +#include <sqlite3.h> +#include <mutex> +#include <dbus/dbus.h> +#include <gio/gio.h> + +class NotificationServer +{ +private: + bool m_initialized; + GDBusConnection* m_pDBusConnection; +public: + + NotificationServer(void); + ~NotificationServer(void); + int initialize(void); + int notifySettingChanged(const std::string pkgId, const std::string privacyId); + int notifyPkgRemoved(const std::string pkgId); +}; + + +#endif // _NOTIFICATION_SERVER_H_ diff --git a/server/inc/PrivacyInfoService.h b/server/inc/PrivacyInfoService.h new file mode 100644 index 0000000..3fcbde9 --- /dev/null +++ b/server/inc/PrivacyInfoService.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2013 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 _PRIVACY_INFO_SERVICE_H_ +#define _PRIVACY_INFO_SERVICE_H_ + +#include <SocketConnection.h> +#include <SocketService.h> + +class PrivacyInfoService { +private: + inline static std::string getInterfaceName(void) + { + return "PrivacyInfoService"; + } + +public: + static void registerCallbacks(SocketService* pSocketService) + { + pSocketService->registerServiceCallback(getInterfaceName(), std::string("addPrivacyInfo"), addPrivacyInfo); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("removePrivacyInfo"), removePrivacyInfo); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("setPrivacySetting"), setPrivacySetting); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("getPrivacyAppPackages"), getPrivacyAppPackages); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("getAppPackagePrivacyInfo"), getAppPackagePrivacyInfo); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("isUserPrompted"), isUserPrompted); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("setUserPrompted"), setUserPrompted); + pSocketService->registerServiceCallback(getInterfaceName(), std::string("notifyUserNotConsented"), notifyUserNotConsented); + } + + static void addPrivacyInfo(SocketConnection* pConnector); + + static void removePrivacyInfo(SocketConnection* pConnector); + + static void setPrivacySetting(SocketConnection* pConnector); + + // input message format + // interface_name(str) | method_name(str) + // output message format + // result (int) | list size (int) | serialized package list (using delimeter) + static void getPrivacyAppPackages(SocketConnection* pConnector); + + static void getAppPackagePrivacyInfo(SocketConnection* pConnector); + + static void isUserPrompted(SocketConnection* pConnector); + + static void setUserPrompted(SocketConnection* pConnector); + + static void notifyUserNotConsented(SocketConnection* pConnector); + +}; +#endif // _PRIVACY_INFO_SERVICE_H_ diff --git a/server/inc/PrivacyManagerDaemon.h b/server/inc/PrivacyManagerDaemon.h new file mode 100644 index 0000000..a646c8b --- /dev/null +++ b/server/inc/PrivacyManagerDaemon.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013 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 _PRIVACY_MANAGER_DAEMON_H_ +#define _PRIVACY_MANAGER_DAEMON_H_ + +#include <privacy_manager_client_types.h> + +class SocketService; + +class EXTERN_API PrivacyManagerDaemon +{ +private: + static PrivacyManagerDaemon* pInstance; + SocketService* pSocketService; + +private: + PrivacyManagerDaemon(void); + ~PrivacyManagerDaemon(void); + + +public: + static PrivacyManagerDaemon* getInstance(void); + int initialize(void); + int start(void); + int stop(void); + int shutdown(void); +}; + +#endif // _PRIVACY_MANAGER_DAEMON_H_ diff --git a/server/inc/PrivacyManagerServer.h b/server/inc/PrivacyManagerServer.h new file mode 100644 index 0000000..a6534ac --- /dev/null +++ b/server/inc/PrivacyManagerServer.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2013 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 _PRIVACY_MANAGER_SERVER_H_ +#define _PRIVACY_MANAGER_SERVER_H_ + +#include <string> +#include <memory> +#include <list> +#include <mutex> +#include <NotificationServer.h> + +class NotificationServer; + +class PrivacyManagerServer +{ +private: + static std::mutex m_singletonMutex; + static PrivacyManagerServer* m_pInstance; + NotificationServer m_notificationServer; + +private: + void createDB(void); + +public: + + explicit PrivacyManagerServer(void); + + virtual ~PrivacyManagerServer(void); + + static PrivacyManagerServer* getInstance(void); + + int getPrivacyAppPackages(std::list <std::string>& list); + + int getAppPackagePrivacyInfo(const std::string pkgId, std::list < std::pair < std::string, bool > > & list); + + int setPrivacySetting(const std::string pkgId, const std::string privacyId, bool enabled); + + int addAppPackagePrivacyInfo(const std::string pkgcId, const std::list < std::string > privilegeList, bool privacyPopupRequired); + + int removeAppPackagePrivacyInfo(const std::string pkgId); + + int isUserPrompted(const std::string pkgId, bool& isPrompted); + + int setUserPrompted(const std::string pkgId, bool prompted); + + int setPermissions(const std::string pkgId, const std::string privacyId, bool enabled); + + int notifyUserNotConsented(const std::string pkgId, const std::string privacyId); + +}; + + +#endif // _PRIVACY_MANAGER_SERVER_H_ diff --git a/server/inc/SocketService.h b/server/inc/SocketService.h new file mode 100644 index 0000000..50fc0e6 --- /dev/null +++ b/server/inc/SocketService.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2013 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 _SOCKET_SERVICE_H_ +#define _SOCKET_SERVICE_H_ + +#include <string> +#include <mutex> +#include <list> +#include <map> +#include <memory> +#include <pthread.h> +#include <SocketConnection.h> + +typedef void(*socketServiceCallback)(SocketConnection* pConnector); + +class SocketService +{ + struct ConnectionInfo{ + ConnectionInfo(int fd, void* pData) : connFd(fd), pData(pData) {} + int connFd; + void* pData; + }; + class ServiceCallback + { + public: + ServiceCallback(socketServiceCallback callback) + : serviceCallback(callback) + {} + socketServiceCallback serviceCallback; + }; + +private: + static const int MAX_LISTEN; + static const int TIMEOUT_SEC; + static const int TIMEOUT_NSEC; + int m_listenFd; + int m_signalToClose; + pthread_t m_mainThread; + + typedef std::shared_ptr<ServiceCallback> ServiceCallbackPtr; + //Map for callback methods, key is a method name and value is a callback to method + typedef std::map<std::string, ServiceCallbackPtr> ServiceMethodCallbackMap; + //Map for interface methods, key is an interface name and value is a map of available methods with callbacks + std::map <std::string, ServiceMethodCallbackMap > m_callbackMap; + + std::list < int > m_clientSocketList; + std::mutex m_clientSocketListMutex; + +private: + static void* serverThread(void* ); + static void* connectionThread(void* pData); + int connectionService(int fd); + int mainloop(void); + void closeConnections(void); + + void addClientSocket(int clientSocket); + void removeClientSocket(int clientSocket); + bool popClientSocket(int* pClientSocket); + +public: + SocketService(void); + ~SocketService(void); + int initialize(void); + int registerServiceCallback(const std::string &interfaceName, const std::string &methodName, socketServiceCallback callbackMethod); + int start(void); + int stop(void); + int shutdown(void); +}; + +#endif //_SOCKET_SERVICE_H_
\ No newline at end of file diff --git a/server/inc/privacy_manager_daemon.h b/server/inc/privacy_manager_daemon.h new file mode 100644 index 0000000..70e39da --- /dev/null +++ b/server/inc/privacy_manager_daemon.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2013 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 __PRIVACY_MANAGER_DAEMON_H__ +#define __PRIVACY_MANAGER_DAEMON_H__ + +#include <privacy_manager_client_types.h> + +#ifdef __cplusplus +extern "C" { +#endif + +int EXTERN_API privacy_manager_daemon_initialize(void); +int EXTERN_API privacy_manager_daemon_start(void); +int EXTERN_API privacy_manager_daemon_stop(void); +int EXTERN_API privacy_manager_daemon_shutdown(void); + +#ifdef __cplusplus +} +#endif + + +#endif //__PRIVACY_MANAGER_DAEMON_H__ + diff --git a/server/src/CMakeLists.txt b/server/src/CMakeLists.txt new file mode 100644 index 0000000..9aecc40 --- /dev/null +++ b/server/src/CMakeLists.txt @@ -0,0 +1,73 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +#PROJECT(privacy-manager-server) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR "\${prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include") +SET(VERSION 0.0) + +SET(VERSION_MAJOR 0) +SET(VERSION "${VERSION_MAJOR}.0.1") + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED dlog pkgmgr-info) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS_PROFILING " -g -pg")
+SET(CMAKE_CXX_FLAGS_PROFILING " -std=c++0x -g -pg")
+SET(CMAKE_C_FLAGS_DEBUG " -g")
+SET(CMAKE_CXX_FLAGS_DEBUG " -std=c++0x -g")
+SET(CMAKE_C_FLAGS_RELEASE " -g")
+SET(CMAKE_CXX_FLAGS_RELEASE " -std=c++0x -g")
+SET(CMAKE_C_FLAGS_CCOV " -g --coverage")
+SET(CMAKE_CXX_FLAGS_CCOV " -std=c++0x -g --coverage")
+ +SET(src_dir "./") +SET(include_dir "./../inc/") +SET(common_src_dir "./../../common/src/") +SET(common_include_dir "./../../common/inc/") + +## Additional flag +ADD_DEFINITIONS("-fvisibility=hidden") +ADD_DEFINITIONS("-Wall -Werror") +ADD_DEFINITIONS("-DDLOG_ERROR_ENABLED") + +################################################################################################### +## for libprivacy-manager-server.so (library) +INCLUDE_DIRECTORIES(${pkgs_INCLUDE_DIRS}) +SET(PRIVACY_MANAGER_SERVER_SOURCES + ${src_dir}/main.cpp + ${src_dir}/SocketService.cpp + ${src_dir}/PrivacyManagerDaemon.cpp + ${common_src_dir}/SocketConnection.cpp + ${common_src_dir}/SocketStream.cpp + ) +SET(PRIVACY_MANAGER_SERVER_HEADERS + ${include_dir}/SocketService.h + ${include_dir}/PrivacyManagerDaemon.h + ${common_include_dir}/SocketConnection.h + ${common_include_dir}/SocketConnection.h +) +SET(PRIVACY_MANAGER_SERVER_LDFLAGS " -module -avoid-version ") +SET(PRIVACY_MANAGER_SERVER_CFLAGS " ${CFLAGS} -fPIC -I${include_dir}" -I${common_include_dir}) +#SET(PRIVACY_MANAGER_SERVER_LIBADD " ") + +ADD_EXECUTABLE(privacy-manager-server ${PRIVACY_MANAGER_SERVER_SOURCES}) +TARGET_LINK_LIBRARIES(privacy-manager-server ${pkgs_LDFLAGS} ${pkgs_LIBRARIES}) +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES COMPILE_FLAGS "${PRIVACY_MANAGER_SERVER_CFLAGS}") +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES SOVERSION ${VERSION_MAJOR}) +SET_TARGET_PROPERTIES(privacy-manager-server PROPERTIES VERSION ${VERSION}) +################################################################################################### + +CONFIGURE_FILE(../../privacy-manager-server.pc.in privacy-manager-server.pc @ONLY) + +INSTALL(TARGETS privacy-manager-server DESTINATION ../lib COMPONENT RuntimeLibraries) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/privacy-manager-server.pc DESTINATION ../lib/pkgconfig) +INSTALL(FILES ${PRIVACY_MANAGER_SERVER_HEADERS} DESTINATION ../include) diff --git a/server/src/NotificationServer.cpp b/server/src/NotificationServer.cpp new file mode 100644 index 0000000..780901c --- /dev/null +++ b/server/src/NotificationServer.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2012 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 <NotificationServer.h> +#include <dbus/dbus.h> +#include <dbus/dbus-glib-lowlevel.h> +#include <PrivacyManagerTypes.h> +#include <Utils.h> +#include <gio/gio.h> + +auto DBusConnectionDeleter = [&](DBusConnection* pPtr) { dbus_connection_close(pPtr); pPtr = NULL;}; +const int MAX_LOCAL_BUF_SIZE = 128; + +NotificationServer::NotificationServer(void) + : m_initialized(false) + , m_pDBusConnection(NULL) +{ + +} + +NotificationServer::~NotificationServer(void) +{ + if (m_pDBusConnection) + { + g_object_unref(m_pDBusConnection); + m_pDBusConnection = NULL; + } +} + +int +NotificationServer::initialize(void) +{ + if (m_initialized) + return PRIV_MGR_ERROR_SUCCESS; + + GError* pGerror = NULL; + g_type_init(); + + m_pDBusConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &pGerror); + TryReturn(pGerror == NULL, PRIV_MGR_ERROR_SYSTEM_ERROR, g_error_free(pGerror), "g_dbus_get_sync : %s", pGerror->message); + + m_initialized = true; + return PRIV_MGR_ERROR_SUCCESS; +} + +int +NotificationServer::notifySettingChanged(const std::string pkgId, const std::string privacyId) +{ + if (!m_initialized) + return PRIV_MGR_ERROR_INVALID_STATE; + + GError* pGerror = NULL; + char* pPkgId = const_cast <char*> (pkgId.c_str()); + char* pPrivacyId = const_cast <char*> (privacyId.c_str()); + + g_dbus_connection_emit_signal(m_pDBusConnection, + NULL, + DBUS_PATH.c_str(), + DBUS_SIGNAL_INTERFACE.c_str(), + DBUS_SIGNAL_SETTING_CHANGED.c_str(), + g_variant_new("(ss)", pPkgId, pPrivacyId), + &pGerror + ); + TryReturn(pGerror == NULL, PRIV_MGR_ERROR_SYSTEM_ERROR, g_error_free(pGerror), "g_dbus_connection_emit_signal : %s", pGerror->message); + + return PRIV_MGR_ERROR_SUCCESS; +} + +int +NotificationServer::notifyPkgRemoved(const std::string pkgId) +{ + if (!m_initialized) + return PRIV_MGR_ERROR_INVALID_STATE; + + GError* pGerror = NULL; + char* pPkgId = const_cast <char*> (pkgId.c_str()); + + g_dbus_connection_emit_signal(m_pDBusConnection, + NULL, + DBUS_PATH.c_str(), + DBUS_SIGNAL_INTERFACE.c_str(), + DBUS_SIGNAL_PKG_REMOVED.c_str(), + g_variant_new("(s)", pPkgId), + &pGerror + ); + TryReturn(pGerror == NULL, PRIV_MGR_ERROR_SYSTEM_ERROR, g_error_free(pGerror), "g_dbus_connection_emit_signal : %s", pGerror->message); + + return PRIV_MGR_ERROR_SUCCESS; +} diff --git a/server/src/PrivacyManagerDaemon.cpp b/server/src/PrivacyManagerDaemon.cpp new file mode 100644 index 0000000..bf84448 --- /dev/null +++ b/server/src/PrivacyManagerDaemon.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2013 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 <string> +#include <PrivacyManagerDaemon.h> +#include <PrivacyInfoService.h> +#include <PrivacyManagerServer.h> +#include <SocketService.h> + +PrivacyManagerDaemon* PrivacyManagerDaemon::pInstance = NULL; + +PrivacyManagerDaemon::PrivacyManagerDaemon(void) + : pSocketService(NULL) +{ + +} + +PrivacyManagerDaemon::~PrivacyManagerDaemon(void) +{ + +} + +PrivacyManagerDaemon* +PrivacyManagerDaemon::getInstance(void) +{ + if (pInstance == NULL) + pInstance = new PrivacyManagerDaemon(); + PrivacyManagerServer::getInstance(); + return pInstance; +} + +int +PrivacyManagerDaemon::initialize(void) +{ + if (pSocketService == NULL) + pSocketService = new SocketService(); + + pSocketService->initialize(); + + PrivacyInfoService::registerCallbacks(pSocketService); + + return 0; +} + +int +PrivacyManagerDaemon::start(void) +{ + if (pSocketService == NULL) + return -1; + return pSocketService->start(); + + return 0; +} + +int +PrivacyManagerDaemon::stop(void) +{ + pSocketService->stop(); + return 0; +} + +int +PrivacyManagerDaemon::shutdown(void) +{ + pSocketService->shutdown(); + return 0; +} diff --git a/server/src/PrivacyManagerServer.cpp b/server/src/PrivacyManagerServer.cpp new file mode 100644 index 0000000..1a21007 --- /dev/null +++ b/server/src/PrivacyManagerServer.cpp @@ -0,0 +1,370 @@ +/* + * Copyright (c) 2013 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 <sstream> +#include <dlog.h> +#include <Utils.h> +#include <PrivacyIdInfo.h> +#include <PrivacyManagerServer.h> +#include <PrivacyManagerTypes.h> +#include <PrivacyDb.h> +#include <privilege-control.h> +#include <security-server.h> +#include <pkgmgr-info.h> +#include <libintl.h> +#include <dlfcn.h> +#include <fcntl.h> +#include <vconf.h> + + +std::mutex PrivacyManagerServer::m_singletonMutex; +PrivacyManagerServer* PrivacyManagerServer::m_pInstance = NULL; +const char *pLocationPrivacy = "http://tizen.org/privacy/location"; +const char *pLocationPrivilege = "http://tizen.org/privilege/location"; +pkgmgrinfo_client *__pc = NULL; +static const char *VCONF_KEY = "db/menu_widget/language"; + +static int pkgmgrInfoCallBack(int req_id, const char *pkg_type, + const char *pkgid, const char *key, const char *val, + const void *pmsg, void *user_data) +{ + if(strncmp(pkg_type, "wgt", 3) == 0 && strncmp(key, "end", 3) == 0 ) + { + LOGD("pkg_id : %s, pkg_type : %s will remove from DB", pkgid, pkg_type); + int res = PrivacyDb::getInstance()->removeAppPackagePrivacyInfo(pkgid); + if(res != PRIV_MGR_ERROR_SUCCESS) + LOGE("failed to remove data : %d", res); + } + return 0; +} + +static void languageCallBack(keynode_t *node, void* user_data) +{ + char *language_set = vconf_get_str(VCONF_KEY); + if(!language_set) + { + LOGE("Failed to get laguage set"); + } + char* result = setlocale(LC_ALL, language_set); + if(!result) + { + LOGE("Failed to set locale"); + } + + if (language_set) + free(language_set); +} + +void +PrivacyManagerServer::createDB(void) +{ + +} + +int +PrivacyManagerServer::setPrivacySetting(const std::string pkgId, const std::string privacyId, bool enabled) +{ + int res = PrivacyDb::getInstance()->setPrivacySetting(pkgId, privacyId, enabled); + TryReturn( res == PRIV_MGR_ERROR_SUCCESS, res, , "privacyDb::setPrivacySetting : %d", res); + + res = m_notificationServer.notifySettingChanged(pkgId, privacyId); + TryReturn( res == PRIV_MGR_ERROR_SUCCESS, res, , "NotificationServer::notifySettingChanged : %d", res); + + res = setPermissions(pkgId, privacyId, enabled); + TryReturn( res == PRIV_MGR_ERROR_SUCCESS, res, , "PrivacyManagerServer::setPermissions : %d", res); + + return res; +} + +int +PrivacyManagerServer::getPrivacyAppPackages(std::list <std::string>& list) +{ + return PrivacyDb::getInstance()->getPrivacyAppPackages(list); +} + +int +PrivacyManagerServer::getAppPackagePrivacyInfo(const std::string pkgId, std::list < std::pair < std::string, bool > >& privacyInfoList) +{ + return PrivacyDb::getInstance()->getAppPackagePrivacyInfo(pkgId, privacyInfoList); +} + + +int +PrivacyManagerServer::addAppPackagePrivacyInfo(const std::string pkgId, const std::list < std::string > privilegeList, bool privacyPopupRequired) +{ + return PrivacyDb::getInstance()->addAppPackagePrivacyInfo(pkgId, privilegeList, privacyPopupRequired); +} + +int +PrivacyManagerServer::removeAppPackagePrivacyInfo(const std::string pkgId) +{ + int res = PrivacyDb::getInstance()->removeAppPackagePrivacyInfo(pkgId); + TryReturn( res == PRIV_MGR_ERROR_SUCCESS, res, , "privacyDb::removeAppPackagePrivacyInfo : %d", res); + + res = m_notificationServer.notifyPkgRemoved(pkgId); + TryReturn( res == PRIV_MGR_ERROR_SUCCESS, res, , "NotificationServer::notifyPkgRemoved : %d", res); + + return res; +} + +int +PrivacyManagerServer::isUserPrompted(const std::string pkgId, bool& isPrompted) +{ + return PrivacyDb::getInstance()->isUserPrompted(pkgId, isPrompted); +} + +int +PrivacyManagerServer::setUserPrompted(const std::string pkgId, bool prompted) +{ + return PrivacyDb::getInstance()->setUserPrompted(pkgId, prompted); +} + +PrivacyManagerServer::PrivacyManagerServer(void) +{ + // for non privileged W3C location apps + int event_type = PMINFO_CLIENT_STATUS_UNINSTALL; + __pc = pkgmgrinfo_client_new(PMINFO_LISTENING); + pkgmgrinfo_client_set_status_type(__pc, event_type); + pkgmgrinfo_client_listen_status(__pc, pkgmgrInfoCallBack, NULL); + + // set locale for notification + if(vconf_notify_key_changed(VCONF_KEY, languageCallBack, NULL) < 0) + LOGE("Failed to register vconf callback"); + else + LOGD("Succeed to register vconf vconf callback"); + + char *language_set = vconf_get_str(VCONF_KEY); + setlocale(LC_ALL, language_set); + if (language_set) + free(language_set); +} + +PrivacyManagerServer::~PrivacyManagerServer(void) +{ + if(__pc) + { + pkgmgrinfo_client_free(__pc); + } +} + +PrivacyManagerServer* +PrivacyManagerServer::getInstance(void) +{ + std::lock_guard < std::mutex > guard(m_singletonMutex); + + if (m_pInstance == NULL) + { + m_pInstance = new PrivacyManagerServer(); + + m_pInstance->m_notificationServer.initialize(); + } + + return m_pInstance; +} + +void destroy_char_list(char** ppList, int size) +{ + int i; + for (i = 0; i < size; ++i) + { + if (ppList[i]) + free(ppList[i]); + } + free(ppList); +} + +int privilegeListCallback(const char *privilege_name, void *user_data) +{ + if (user_data == NULL) + return PRIV_MGR_ERROR_SYSTEM_ERROR; + + std::list <std::string>* pPrivilegList = (std::list <std::string>*)user_data; + pPrivilegList->push_back(std::string(privilege_name)); + return 0; +} + +int +PrivacyManagerServer::setPermissions(const std::string pkgId, const std::string privacyId, bool enabled) +{ + SECURE_LOGD("pkgId = %s / privacyId = %s, enabled = %d", pkgId.c_str(), privacyId.c_str(), enabled); + + int ret = PMINFO_R_OK; + int res = PRIV_MGR_ERROR_SUCCESS; + + char *pType = NULL; + app_type_t appType = APP_TYPE_OSP; + + pkgmgrinfo_pkginfo_h handle; + ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle); + TryReturn(ret == PMINFO_R_OK, PRIV_MGR_ERROR_SYSTEM_ERROR,, "pkgmgrinfo_pkginfo_get_pkginfo was failed : %d", ret); + + ret = pkgmgrinfo_pkginfo_get_type(handle, &pType); + TryReturn(ret == PMINFO_R_OK, PRIV_MGR_ERROR_SYSTEM_ERROR, pkgmgrinfo_pkginfo_destroy_pkginfo(handle), "pkgmgrinfo_pkginfo_get_type was failed :%d", ret); + + int typeSize = sizeof(pType); + if (strncmp(pType, "wgt", typeSize) == 0) + { + appType = APP_TYPE_WGT; + } + else if (strncmp(pType, "tpk", typeSize) == 0) + { + appType = APP_TYPE_OSP; + } + else if (strncmp(pType, "rpm", typeSize) == 0) + { + appType = APP_TYPE_EFL; + } + else + { + LOGE("Type of package is incorrect. [TYPE: %s]", pType); + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + return PRIV_MGR_ERROR_SYSTEM_ERROR; + } + + std::list <std::string> pkgPrivilegeList; + res = pkgmgrinfo_pkginfo_foreach_privilege(handle, privilegeListCallback, &pkgPrivilegeList); + TryReturn(ret == PMINFO_R_OK, PRIV_MGR_ERROR_SYSTEM_ERROR, pkgmgrinfo_pkginfo_destroy_pkginfo(handle), "pkgmgrinfo_pkginfo_foreach_privilege was failed :%d", ret); + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + + std::list <std::string> privacyPrivilegeList; + res = PrivacyIdInfo::getPrivilegeListFromPrivacyId(privacyId, privacyPrivilegeList); + TryReturn(res == PRIV_MGR_ERROR_SUCCESS, PRIV_MGR_ERROR_SYSTEM_ERROR, pkgPrivilegeList.clear(), "getPrivilegeListFromPrivacyId was failed."); + + std::list <std::string> privilegeList; + for (std::list <std::string>::iterator privacyPrivilegeListIter = privacyPrivilegeList.begin(); privacyPrivilegeListIter != privacyPrivilegeList.end(); ++privacyPrivilegeListIter) + { + for (std::list <std::string>::iterator pkgPrivilegeListIter = pkgPrivilegeList.begin(); pkgPrivilegeListIter != pkgPrivilegeList.end(); ++pkgPrivilegeListIter) + { + if ((*privacyPrivilegeListIter).compare(*pkgPrivilegeListIter) == 0) + { + privilegeList.push_back(std::string(*privacyPrivilegeListIter)); + SECURE_LOGD("smack rule control [%s]", (*privacyPrivilegeListIter).c_str()); + break; + } + } + } + + // for W3C Web Application using location API + if(appType == APP_TYPE_WGT && strncmp(privacyId.c_str(), pLocationPrivacy, strlen(pLocationPrivacy)) == 0) + { + LOGD("WGT location privacy request for w3c location api"); + std::list<std::string>::iterator iter = find(privilegeList.begin(), privilegeList.end(), std::string(pLocationPrivilege)); + + if(iter == privilegeList.end()) + privilegeList.push_back(std::string(pLocationPrivilege)); + } + + privacyPrivilegeList.clear(); + pkgPrivilegeList.clear(); + + unsigned int listSize = privilegeList.size(); + char** ppPrivilegeList = (char**) calloc(listSize + 1, sizeof(char*)); + TryReturn(ppPrivilegeList != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, privilegeList.clear(),"calloc was failed."); + + std::list <std::string>::iterator iter = privilegeList.begin(); + for (unsigned int i = 0; i < listSize; ++i) + { + ppPrivilegeList[i] = (char*)calloc (strlen(iter->c_str()) + 1, sizeof(char)); + if (ppPrivilegeList[i] == NULL) + { + destroy_char_list(ppPrivilegeList, listSize + 1); + privilegeList.clear(); + LOGE("calloc was failed."); + return PRIV_MGR_ERROR_OUT_OF_MEMORY; + } + memcpy(ppPrivilegeList[i], iter->c_str(), strlen(iter->c_str())); + ++iter; + } + privilegeList.clear(); + + ppPrivilegeList[listSize] = NULL; + if (enabled == true) + { + LOGD("call: security_server_app_enable_permissions()"); + res = security_server_app_enable_permissions(pkgId.c_str(), appType, (const char**) ppPrivilegeList, 1); + LOGD("leave: security_server_app_enable_permissions()"); + TryReturn(res == 0/*SECURITY_SERVER_SUCCESS*/, PRIV_MGR_ERROR_SYSTEM_ERROR, destroy_char_list(ppPrivilegeList, listSize + 1), "security_server_app_enable_permissions was failed : %d", res); + } + else + { + LOGD("call: security_server_app_disable_permissions()"); + res = security_server_app_disable_permissions(pkgId.c_str(), appType, (const char**) ppPrivilegeList); + LOGD("leave: security_server_app_disable_permissions()"); + TryReturn(res == 0/*SECURITY_SERVER_SUCCESS*/, PRIV_MGR_ERROR_SYSTEM_ERROR, destroy_char_list(ppPrivilegeList, listSize + 1), "security_server_app_disable_permissions was failed : %d", res); + } + destroy_char_list(ppPrivilegeList, listSize + 1); + return PRIV_MGR_ERROR_SUCCESS; +} + +int +PrivacyManagerServer::notifyUserNotConsented(const std::string pkgId, const std::string privacyId) +{ + int res = PRIV_MGR_ERROR_SUCCESS; + + const char* notifyStringFormat = dgettext("privilege", "IDS_ST_TPOP_P1SS_DISABLED_IN_P2SS_ABB"); + char notifyString[1024] = {0,}; + + bool packageNameFlag = true; + char* packageName = NULL; + pkgmgrinfo_pkginfo_h handle; + res = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle); + if (res != PMINFO_R_OK) + { + SECURE_LOGE("pkgmgrinfo_pkginfo_get_pkginfo() was failed: result[%d]", res); + packageNameFlag = false; + } + else + { + res = pkgmgrinfo_pkginfo_get_label(handle, &packageName); + if (res != PMINFO_R_OK) + { + SECURE_LOGE("pkgmgrinfo_pkginfo_get_label() was failed: result[%d]", res); + packageNameFlag = false; + } + } + + std::string privacyDisplayName; + res = PrivacyIdInfo::getPrivaycDisplayName(privacyId, privacyDisplayName); + if (res != PRIV_MGR_ERROR_SUCCESS) + { + privacyDisplayName = privacyId; + } + + if (packageNameFlag) + { + snprintf(notifyString, 1024, notifyStringFormat, privacyDisplayName.c_str(), packageName); + } + else + { + snprintf(notifyString, 1024, notifyStringFormat, privacyDisplayName.c_str(), pkgId.c_str()); + } + + void* so_handle = dlopen("libnotification.so", RTLD_LAZY | RTLD_GLOBAL); + TryReturn(so_handle != NULL, PRIV_MGR_ERROR_SYSTEM_ERROR, , "Failed to open notification binary"); + + int(*DoNotificationFunc)(const char*) = NULL; + + char* errormsg = NULL; + DoNotificationFunc = reinterpret_cast<int(*)(const char*)>(dlsym(so_handle, "notification_status_message_post")); + errormsg = dlerror(); + TryReturn(errormsg == NULL, PRIV_MGR_ERROR_SYSTEM_ERROR, dlclose(so_handle), "Failed to find symbol"); + + DoNotificationFunc(notifyString); + dlclose(so_handle); + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + return PRIV_MGR_ERROR_SUCCESS; +} + diff --git a/server/src/SocketService.cpp b/server/src/SocketService.cpp new file mode 100644 index 0000000..9d3f892 --- /dev/null +++ b/server/src/SocketService.cpp @@ -0,0 +1,386 @@ +/* + * Copyright (c) 2013 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 <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/signalfd.h> +#include <sys/select.h> +#include <sys/stat.h> +#include <signal.h> +#include <unistd.h> +#include <fcntl.h> +#include <memory> +#include <dlog.h> +#include <PrivacyManagerTypes.h> +#include <Utils.h> +#include "SocketService.h" +#include "SocketConnection.h" +#include <sys/smack.h> + +const int SocketService::MAX_LISTEN = 5; + +SocketService::SocketService(void) + : m_listenFd(-1) + , m_signalToClose(-1) + , m_mainThread(-1) +{ + +} + +SocketService::~SocketService(void) +{ + +} + +int +SocketService::initialize(void) +{ + LOGI("SocketService initializing"); + + m_listenFd = socket(AF_UNIX, SOCK_STREAM, 0); + TryReturn( m_listenFd != -1, PRIV_MGR_ERROR_SYSTEM_ERROR, , "socket : %s", strerror(errno)); + + TryReturn( smack_fsetlabel(m_listenFd, "*", SMACK_LABEL_IPIN) == 0, PRIV_MGR_ERROR_SYSTEM_ERROR, , "Failed to set permission to socket : %s", strerror(errno)); + + int flags = -1; + int res; + if ( (flags = fcntl(m_listenFd, F_GETFL, 0)) == -1) + flags = 0; + res = fcntl(m_listenFd, F_SETFL, flags | O_NONBLOCK); + TryReturn( res != -1, PRIV_MGR_ERROR_SYSTEM_ERROR, , "fcntl : %s", strerror(errno)); + + sockaddr_un server_address; + bzero(&server_address, sizeof(server_address)); + server_address.sun_family = AF_UNIX; + if(strlen(SERVER_ADDRESS.c_str()) <= strlen(server_address.sun_path)) + strcpy(server_address.sun_path, SERVER_ADDRESS.c_str()); + else + return PRIV_MGR_ERROR_SYSTEM_ERROR; + unlink(server_address.sun_path); + + mode_t socket_umask, original_umask; + socket_umask = 0; + original_umask = umask(socket_umask); + + res = bind(m_listenFd, (struct sockaddr*)&server_address, SUN_LEN(&server_address)); + TryReturn( res != -1, PRIV_MGR_ERROR_SYSTEM_ERROR, , "bind : %s", strerror(errno)); + + umask(original_umask); + + LOGI("SocketService initialized"); + + return PRIV_MGR_ERROR_SUCCESS; +} + +int +SocketService::start(void) +{ + LOGI("SocketService starting"); + + sigset_t sigset; + sigemptyset(&sigset); +// if ( sigaddset(&sigset, m_signalToClose) == -1 ) +// { +// LOGE("Failed to sigaddset : %s", strerror(errno)); +// return -1; +// } + + int res = 0; + res = pthread_sigmask(SIG_BLOCK, &sigset, NULL); + TryReturn( res >= 0, PRIV_MGR_ERROR_SYSTEM_ERROR, , "pthread_sigmask : %s", strerror(errno)); + + pthread_t mainThread; + res = pthread_create(&mainThread, NULL, &serverThread, this); + TryReturn( res >= 0, PRIV_MGR_ERROR_SYSTEM_ERROR, errno = res, "pthread_create : %s", strerror(res)); + + m_mainThread = mainThread; + + LOGI("SocketService started"); + + return PRIV_MGR_ERROR_SUCCESS; +} + +void* +SocketService::serverThread(void* pData) +{ + if(pthread_detach(pthread_self())!=0) + { + LOGE("Failed detach thread!"); + } + SocketService &t = *static_cast< SocketService* > (pData); + LOGI("Running main thread"); + int ret = t.mainloop(); + if (ret < 0) + { + return (void*) 1; + } + return (void*) 0; +} + +int +SocketService::mainloop(void) +{ + if( listen(m_listenFd, MAX_LISTEN) == -1 ){ + LOGE("listen : %s", strerror(errno)); + return PRIV_MGR_ERROR_IPC_ERROR; + } + + //Settings to catch closing signal in select + int signal_fd; + sigset_t sigset; + int res; + res = sigemptyset(&sigset); + TryReturn( res != -1, PRIV_MGR_ERROR_SYSTEM_ERROR, , "sigemptyset : %s", strerror(errno)); + +// if( sigaddset(&sigset, m_signalToClose) == -1) { +// LOGE("sigaddset : %s", strerror(errno)); +// return -1; +// } + signal_fd = signalfd(-1, &sigset, 0); + TryReturn( signal_fd >= 0, PRIV_MGR_ERROR_SYSTEM_ERROR, , "signalfd : %s", strerror(errno)); + + //Setting descriptors for pselect + fd_set allset, rset; + int maxfd; + FD_ZERO(&allset); + FD_SET(m_listenFd, &allset); + FD_SET(signal_fd, &allset); + maxfd = (m_listenFd > signal_fd) ? (m_listenFd) : (signal_fd); + ++maxfd; + //this will block SIGPIPE for this thread and every thread created in it + //reason : from here on we don't won't to receive SIGPIPE on writing to closed socket + //instead of signal we want to receive error from write - hence blocking SIGPIPE + sigset_t set; + sigemptyset(&set); + sigaddset(&set, SIGPIPE); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + while(1) + { + rset = allset; + if(pselect(maxfd, &rset, NULL, NULL, NULL, NULL) == -1) + { + closeConnections(); + LOGE("pselect()"); + return PRIV_MGR_ERROR_SYSTEM_ERROR; + } + + if(FD_ISSET(signal_fd, &rset)) + { + LOGI("Got signal to close"); + signalfd_siginfo siginfo; + ssize_t res; + res = read(signal_fd, &siginfo, sizeof(siginfo)); + TryReturn( res > 0, PRIV_MGR_ERROR_IPC_ERROR, closeConnections();, "read : %s", strerror(errno)); + TryReturn( (size_t)res == sizeof(siginfo), PRIV_MGR_ERROR_IPC_ERROR, closeConnections();, "couldn't read whole siginfo"); + + if((int)siginfo.ssi_signo == m_signalToClose) + { + LOGI("Server thread got signal to close"); + closeConnections(); + return PRIV_MGR_ERROR_SUCCESS; + } + else + { + LOGI("Got not handled signal"); + } + } + if(FD_ISSET(m_listenFd, &rset)) + { + int clientFd; + clientFd = accept(m_listenFd, NULL, NULL); + TryReturn( clientFd != -1, PRIV_MGR_ERROR_IPC_ERROR, closeConnections();, "accept : %s", strerror(errno)); + + LOGI("Got incoming connection"); + ConnectionInfo * connection = new ConnectionInfo(clientFd, (void *)this); + int res; + pthread_t client_thread; + if((res = pthread_create(&client_thread, NULL, &connectionThread, connection)) < 0) + { + delete connection; + errno = res; + closeConnections(); + LOGE("pthread_create()"); + return PRIV_MGR_ERROR_SYSTEM_ERROR; + } + addClientSocket(clientFd); + } + } +} + +void* +SocketService::connectionThread(void* pData) +{ + if(pthread_detach(pthread_self())!=0) + { + LOGE("Failed to detach thread"); + } + std::unique_ptr<ConnectionInfo> connectionInfo (static_cast<ConnectionInfo *>(pData)); + SocketService &t = *static_cast<SocketService *>(connectionInfo->pData); + //LOGI("Starting connection thread"); + int ret = t.connectionService(connectionInfo->connFd); + if (ret < 0) + { + LOGE("Connection thread error"); + t.removeClientSocket(connectionInfo->connFd); + close(connectionInfo->connFd); + return (void*)1; + } + //LOGI("Client serviced"); + return (void*)0; +} + +int +SocketService::connectionService(int fd) +{ + + SocketConnection connector = SocketConnection(fd); + std::string interfaceName, methodName; + + int res = connector.read(&interfaceName, &methodName); + if (res != PRIV_MGR_ERROR_SUCCESS) + { + LOGE("read : %d", res); + return res; + } + + //LOGD("Got interface : %s", interfaceName.c_str()); + //LOGD("Got method : %s", methodName.c_str()); + + if( m_callbackMap.find(interfaceName) == m_callbackMap.end()) + { + LOGE("Unknown interface : %s", interfaceName.c_str()); + return PRIV_MGR_ERROR_NO_DATA; + } + + if(m_callbackMap[interfaceName].find(methodName) == m_callbackMap[interfaceName].end()) + { + LOGE("Unknown method : %s", methodName.c_str()); + return PRIV_MGR_ERROR_NO_DATA; + } + +// if(m_callbackMap[interfaceName][methodName]->securityCallback != NULL){ +// if(!m_callbackMap[interfaceName][methodName]->securityCallback(fd)){ +// LOGE("Security check returned false"); +// return -1; +// } +// } + + //LOGI("Calling service"); + m_callbackMap[interfaceName][methodName]->serviceCallback(&connector); + + //LOGI("Removing client"); + removeClientSocket(fd); + close(fd); + + //LOGI("Call served"); + + return PRIV_MGR_ERROR_SUCCESS; +} + +int +SocketService::stop(void) +{ + LOGI("Stopping"); + if(close(m_listenFd) == -1) + if(errno != ENOTCONN) + { + LOGE("close() : %s", strerror(errno)); + return PRIV_MGR_ERROR_IPC_ERROR; + } + + int returned_value; + if((returned_value = pthread_kill(m_mainThread, m_signalToClose)) < 0) + { + errno = returned_value; + LOGE("pthread_kill() : %s", strerror(errno)); + return PRIV_MGR_ERROR_IPC_ERROR; + } + pthread_join(m_mainThread, NULL); + + LOGI("Stopped"); + return PRIV_MGR_ERROR_SUCCESS; +} + +int +SocketService::shutdown(void) +{ + return PRIV_MGR_ERROR_SUCCESS; +} + +int +SocketService::registerServiceCallback(const std::string &interfaceName, const std::string &methodName, socketServiceCallback callbackMethod) +{ + if(NULL == callbackMethod) + { + LOGE("Null callback"); + return PRIV_MGR_ERROR_INVALID_PARAMETER; + } + if(interfaceName.empty() || methodName.empty()) + { + LOGE("Interface and method name cannot be empty"); + return PRIV_MGR_ERROR_INVALID_PARAMETER; + } + + auto serviceCallbackPtr = std::make_shared<ServiceCallback>(ServiceCallback(callbackMethod)); + m_callbackMap[interfaceName][methodName] = serviceCallbackPtr; + + return PRIV_MGR_ERROR_SUCCESS; +} + +void +SocketService::addClientSocket(int clientSocket) +{ + std::lock_guard<std::mutex> guard(m_clientSocketListMutex); + m_clientSocketList.push_back(clientSocket); +} + +void +SocketService::removeClientSocket(int clientSocket) +{ + std::lock_guard<std::mutex> guard(m_clientSocketListMutex); + m_clientSocketList.remove(clientSocket); +} + +bool +SocketService::popClientSocket(int * pClientSocket) +{ + std::lock_guard<std::mutex> guard(m_clientSocketListMutex); + if(m_clientSocketList.empty()) + return false; + *pClientSocket = m_clientSocketList.front(); + m_clientSocketList.pop_front(); + return true; +} + + +void +SocketService::closeConnections(void) +{ + int clientSocket; + LOGI("Closing client sockets"); + while(popClientSocket(&clientSocket)) + { + if(close(clientSocket) == -1) + { + LOGE("close() : %s", strerror(errno)); + } + } + + LOGI("Connections closed"); +} diff --git a/server/src/main.cpp b/server/src/main.cpp new file mode 100644 index 0000000..2ae8169 --- /dev/null +++ b/server/src/main.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2013 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 <PrivacyManagerDaemon.h> +#include <glib.h> + +int +main(int argc, char* argv[]) +{ + PrivacyManagerDaemon* pDaemon = PrivacyManagerDaemon::getInstance(); + + pDaemon->inialize(); + pDaemon->start(); + + GMainLoop* pLoop; + pLoop = g_main_new(TRUE); + + g_main_loop_run(pLoop); + + pDaemon->stop(); + pDaemon->shutdown(); + + return 0; +}
\ No newline at end of file diff --git a/server/src/privacy_manager_daemon.cpp b/server/src/privacy_manager_daemon.cpp new file mode 100644 index 0000000..098f414 --- /dev/null +++ b/server/src/privacy_manager_daemon.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2013 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 <privacy_manager_daemon.h> +#include <PrivacyManagerDaemon.h> + +int privacy_manager_daemon_initialize(void) +{ + PrivacyManagerDaemon* pPrivacyDaemon = PrivacyManagerDaemon::getInstance(); + + return pPrivacyDaemon->initialize(); +} + +int privacy_manager_daemon_start(void) +{ + PrivacyManagerDaemon* pInstance = PrivacyManagerDaemon::getInstance(); + + return pInstance->start(); +} + +int privacy_manager_daemon_stop(void) +{ + PrivacyManagerDaemon* pInstance = PrivacyManagerDaemon::getInstance(); + + return pInstance->stop(); +} + +int privacy_manager_daemon_shutdown(void) +{ + PrivacyManagerDaemon* pInstance = PrivacyManagerDaemon::getInstance(); + + return pInstance->shutdown(); +} diff --git a/server/src/service/PrivacyInfoService.cpp b/server/src/service/PrivacyInfoService.cpp new file mode 100644 index 0000000..e3f87e2 --- /dev/null +++ b/server/src/service/PrivacyInfoService.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2012 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 <PrivacyInfoService.h> +#include <PrivacyManagerServer.h> +#include <dlog.h> +#include <Utils.h> + +void +PrivacyInfoService::addPrivacyInfo(SocketConnection* pConnector) +{ + std::string pkgId; + std::list < std::string > list; + bool privacyPopupRequired = true; + pConnector->read(&pkgId, &list, &privacyPopupRequired); + + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + int result = pPrivacyManagerServer->addAppPackagePrivacyInfo(pkgId, list, privacyPopupRequired); + + pConnector->write(result); +} + +void +PrivacyInfoService::removePrivacyInfo(SocketConnection* pConnector) +{ + std::string pkgId; + pConnector->read(&pkgId); + + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + int res = pPrivacyManagerServer->removeAppPackagePrivacyInfo(pkgId); + + pConnector->write(res); +} + +void +PrivacyInfoService::setPrivacySetting(SocketConnection* pConnector) +{ + std::string pkgId; + std::string privacyId; + bool enabled = false; + pConnector->read(&pkgId, &privacyId, &enabled); + + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + int result = pPrivacyManagerServer->setPrivacySetting(pkgId, privacyId, enabled); + + pConnector->write(result); +} + +void +PrivacyInfoService::getPrivacyAppPackages(SocketConnection* pConnector) +{ + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + std::list <std::string> list; + int result = pPrivacyManagerServer->getPrivacyAppPackages(list); + + pConnector->write( (unsigned int) result); + pConnector->write( (unsigned int) list.size()); + pConnector->write(list); +} + +void +PrivacyInfoService::getAppPackagePrivacyInfo(SocketConnection* pConnector) +{ + std::string pkgId; + + pConnector->read(&pkgId); + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + + + std::list < std::pair < std::string, bool > > infoList; + + int res = pPrivacyManagerServer->getAppPackagePrivacyInfo(pkgId, infoList); + + pConnector->write( res ); + pConnector->write( infoList ); +} + +void +PrivacyInfoService::isUserPrompted(SocketConnection* pConnector) +{ + std::string pkgId; + + pConnector->read(&pkgId); + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + + bool isPrompted; + int res = pPrivacyManagerServer->isUserPrompted(pkgId, isPrompted); + + pConnector->write( res ); + pConnector->write( isPrompted ); + +} + +void +PrivacyInfoService::setUserPrompted(SocketConnection* pConnector) +{ + std::string pkgId; + bool prompted = false; + + pConnector->read(&pkgId, &prompted); + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + + int res = pPrivacyManagerServer->setUserPrompted(pkgId, prompted); + LOGI("write"); + pConnector->write( res ); + +} + +void +PrivacyInfoService::notifyUserNotConsented(SocketConnection* pConnector) +{ + std::string pkgId, privacyId; + + pConnector->read(&pkgId, &privacyId); + PrivacyManagerServer* pPrivacyManagerServer = PrivacyManagerServer::getInstance(); + + int res = pPrivacyManagerServer->notifyUserNotConsented(pkgId, privacyId); + pConnector->write( res ); + +} |