Redo askuser-notification 39/135939/4
authorZofia Abramowska <z.abramowska@samsung.com>
Fri, 23 Jun 2017 18:09:07 +0000 (20:09 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Fri, 30 Jun 2017 16:44:27 +0000 (18:44 +0200)
* Remove redundant event/element ids
* Move PolicyUpdater to askuser-notification
* Implement new logic of askuser-notification

Change-Id: I32f104f20bcb640f00e05c6edfc140786e73c2de

29 files changed:
packaging/askuser-notification.spec
src/agent/main/Policy.cpp [deleted file]
src/agent/main/Policy.h [deleted file]
src/agent/main/PolicyUpdater.cpp [deleted file]
src/agent/main/PolicyUpdater.h [deleted file]
src/common/policy/PkgInfo.h [deleted file]
src/common/policy/PrivilegeInfo.cpp [deleted file]
src/common/policy/PrivilegeInfo.h [deleted file]
src/common/protocol/askuser-notification/ask-user-service.h
src/notification-daemon/CMakeLists.txt
src/notification-daemon/Logic.cpp
src/notification-daemon/Logic.h
src/notification-daemon/PolicyUpdater.cpp [new file with mode: 0644]
src/notification-daemon/PolicyUpdater.h [new file with mode: 0644]
src/notification-daemon/ServerCallbacks.cpp
src/notification-daemon/event/Event.h
src/notification-daemon/policy/PkgInfo.h [new file with mode: 0644]
src/notification-daemon/policy/Policy.cpp [new file with mode: 0644]
src/notification-daemon/policy/Policy.h [new file with mode: 0644]
src/notification-daemon/policy/PrivilegeInfo.cpp [new file with mode: 0644]
src/notification-daemon/policy/PrivilegeInfo.h [new file with mode: 0644]
src/notification-daemon/ui/Answerable.h
src/notification-daemon/ui/Po.cpp
src/notification-daemon/ui/Po.h
src/notification-daemon/ui/PopupCheckMobile.h
src/notification-daemon/ui/PopupCheckWearable.h
src/notification-daemon/ui/Popupper.cpp
src/notification-daemon/ui/Popupper.h
src/notification-daemon/ui/UIElement.h

index e767bb257d5936695a4957097262189f39f95933..6c0e6be4b1bb0d0d9bb188914cad84facae0572b 100644 (file)
@@ -21,6 +21,7 @@ BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(cynara-plugin)
 BuildRequires: pkgconfig(pkgmgr-info)
 BuildRequires: pkgconfig(security-privilege-manager)
+BuildRequires: pkgconfig(security-manager)
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(vconf)
 BuildRequires: pkgconfig(capi-ui-efl-util)
diff --git a/src/agent/main/Policy.cpp b/src/agent/main/Policy.cpp
deleted file mode 100644 (file)
index e63faa7..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- *  Copyright (c) 2016-2017 Samsung Electronics Co.
- *
- *  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/agent/main/Policy.cpp
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Implementation of Policy wrappers
- */
-
-#include <memory>
-
-#include <security-manager.h>
-
-#include <exception/Exception.h>
-#include <log/alog.h>
-
-#include <policy/PkgInfo.h>
-#include "Policy.h"
-
-namespace {
-inline void throwOnSMError(std::string err, int ret)
-{
-    if (ret != SECURITY_MANAGER_SUCCESS)
-        throw AskUser::Exception(err + " : " + std::to_string(ret));
-}
-}
-
-namespace AskUser {
-
-void identifyApp(const std::string &client, const std::string &user,
-                 std::string &appId, std::string &pkgLabel)
-{
-    char *pkgName = nullptr;
-    char *appName = nullptr;
-
-    int ret = security_manager_identify_app_from_cynara_client(client.c_str(), &pkgName, &appName);
-    std::unique_ptr<char, decltype(free)*> pkg_name_p(pkgName, free);
-    std::unique_ptr<char, decltype(free)*> app_name_p(appName, free);
-    throwOnSMError("security_manager_identify_app_from_cynara_client", ret);
-
-    if (!pkgName) {
-        ALOGE("Couldn't identify clients package id");
-        return;
-    }
-
-    uid_t uid = std::strtoul(user.c_str(), nullptr, 10);
-    PkgInfo pkgInfo(pkgName, uid);
-    if (!appName)
-        appId = pkgInfo.mainAppId();
-
-    pkgLabel = pkgInfo.pkgLabel();
-}
-
-PolicyEntry::PolicyEntry() {
-    throwOnSMError("security_manager_policy_entry_new",
-                                  security_manager_policy_entry_new(&m_entry));
-}
-
-PolicyEntry::PolicyEntry(PolicyEntry &&other) : m_entry(std::move(other.m_entry)) {
-    other.m_entry = nullptr;
-}
-
-PolicyEntry::~PolicyEntry() {
-    security_manager_policy_entry_free(m_entry);
-}
-
-void PolicyEntry::setApp(const std::string &appId) {
-    throwOnSMError("security_manager_policy_entry_set_application",
-                   security_manager_policy_entry_set_application(m_entry, appId.c_str()));
-}
-
-void PolicyEntry::setUser(const std::string &user) {
-    throwOnSMError("security_manager_policy_entry_set_user",
-                   security_manager_policy_entry_set_user(m_entry, user.c_str()));
-}
-
-void PolicyEntry::setPrivilege(const std::string &privilege) {
-    throwOnSMError("security_manager_policy_entry_set_privilege",
-                   security_manager_policy_entry_set_privilege(m_entry, privilege.c_str()));
-}
-
-void PolicyEntry::setLevel(const std::string &level) {
-    throwOnSMError("security_manager_policy_entry_admin_set_level",
-                   security_manager_policy_entry_set_level(m_entry, level.c_str()));
-}
-
-PolicyRequest::PolicyRequest() {
-    throwOnSMError("security_manager_policy_update_req_new",
-                   security_manager_policy_update_req_new(&m_req));
-}
-
-PolicyRequest::~PolicyRequest() {
-    m_entries.clear();
-    security_manager_policy_update_req_free(m_req);
-}
-
-void PolicyRequest::addEntry(PolicyEntry &&entry) {
-    throwOnSMError("security_manager_policy_update_req_add_entry",
-                   security_manager_policy_update_req_add_entry(m_req, entry.get()));
-    m_entries.emplace_back(std::move(entry));
-}
-
-void PolicyRequest::updatePolicy() {
-    throwOnSMError("security_manager_policy_update_send",
-                   security_manager_policy_update_send(m_req));
-}
-
-} /* namespace AskUser */
diff --git a/src/agent/main/Policy.h b/src/agent/main/Policy.h
deleted file mode 100644 (file)
index 66bf8d9..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- *  Copyright (c) 2016-2017 Samsung Electronics Co.
- *
- *  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/agent/main/Policy.h
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Definition of Policy wrappers
- */
-
-#pragma once
-
-#include <string>
-#include <vector>
-
-struct policy_entry;
-struct policy_update_req;
-
-namespace AskUser {
-
-void identifyApp(const std::string &client, const std::string &user,
-                 std::string &appId, std::string &pkgLabel);
-
-class PolicyEntry {
-public:
-    PolicyEntry();
-    PolicyEntry(PolicyEntry &&other);
-    ~PolicyEntry();
-    void setApp(const std::string &appId);
-    void setUser(const std::string &user);
-    void setPrivilege(const std::string &privilege);
-    void setLevel(const std::string &level);
-
-    policy_entry *get() const { return m_entry; }
-
-private:
-    policy_entry *m_entry;
-};
-
-class PolicyRequest {
-public:
-    PolicyRequest();
-    ~PolicyRequest();
-    void addEntry(PolicyEntry &&entry);
-    void updatePolicy();
-
-private:
-    policy_update_req *m_req;
-    std::vector<PolicyEntry> m_entries;
-};
-
-} /* namespace AskUser */
diff --git a/src/agent/main/PolicyUpdater.cpp b/src/agent/main/PolicyUpdater.cpp
deleted file mode 100644 (file)
index a9fbd91..0000000
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright (c) 2016-2017 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        PolicyUpdater.cpp
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       This file implements class of policy updater
- */
-
-#include <csignal>
-#include <unistd.h>
-
-#include <exception/Exception.h>
-#include <log/alog.h>
-
-#include <policy/PrivilegeInfo.h>
-
-#include "Policy.h"
-#include "PolicyUpdater.h"
-
-namespace AskUser {
-
-namespace Agent {
-
-PolicyUpdater::PolicyUpdater() : m_running(false)
-{}
-
-void PolicyUpdater::start() {
-    m_thread = std::thread(&PolicyUpdater::run, this);
-    m_running = true;
-}
-
-void PolicyUpdater::stop() {
-    if (!m_running || !m_thread.joinable())
-        return;
-    m_running = false;
-
-    m_thread.join();
-    ALOGD("PolicyUpdater thread finished.");
-}
-
-void PolicyUpdater::run() {
-    int ret;
-    sigset_t mask;
-    sigemptyset(&mask);
-    sigaddset(&mask, SIGTERM);
-    if ((ret = pthread_sigmask(SIG_BLOCK, &mask, nullptr)) < 0) {
-        ALOGE("sigprocmask failed [<<" << ret << "]");
-    }
-
-    while (m_running) {
-        try {
-            std::unique_lock<std::mutex> lock(m_mutex);
-            m_event.wait(lock, [&](){ return !m_events.empty() || !m_running;});
-
-            if (m_events.empty()) {
-                continue;
-            }
-
-            ALOGD("Got " << m_events.size() << " policies to update");
-            m_eventsInternal = std::move(m_events);
-            lock.unlock();
-
-            PolicyRequest req;
-            ALOGD("Updating " << m_eventsInternal.size() << " policies");
-            for (auto &policyInfo : m_eventsInternal) {
-                std::string privacyName = PrivilegeInfo::getPrivacyName(policyInfo.priv);
-                if (privacyName.empty()) {
-                    ALOGE("Unable to get privacy name for privilege " << policyInfo.priv);
-                    throw Exception("Unable to get privacy name for " + policyInfo.priv);
-                }
-                auto privacyPrivs = PrivilegeInfo::getPrivacyPrivileges(privacyName);
-                if (privacyPrivs.empty()) {
-                    ALOGE("Unable to get privacy privileges for privacy " << privacyName);
-                    throw Exception("Unable to get privacy privileges for privacy " + privacyName);
-                }
-                ALOGD("Adding policy entry for : app: " << policyInfo.appId << ", privilege: "
-                      << policyInfo.priv  << ", user:" << policyInfo.user << ", level: "
-                      << policyInfo.level);
-                for (auto &priv : privacyPrivs) {
-                    PolicyEntry entry;
-                    entry.setApp(policyInfo.appId);
-                    entry.setUser(policyInfo.user);
-                    entry.setPrivilege(priv);
-                    entry.setLevel(policyInfo.level);
-                    req.addEntry(std::move(entry));
-                }
-            }
-            req.updatePolicy();
-            ALOGD("Policy update sent");
-
-            m_eventsInternal.clear();
-        } catch (const std::exception &e) {
-            ALOGC("Unexpected exception: <" << e.what() << ">");
-        } catch (...) {
-            ALOGE("Unexpected unknown exception caught!");
-        }
-    }
-}
-
-void PolicyUpdater::update(const std::string &appId, const std::string &user,
-                           const std::string &privilege, const std::string &level)
-{
-    ALOGD("Queueing policy update for: app: " << appId << ", privilege: " << privilege
-          << ", user:" << user << ", level: " << level);
-    if (!m_running) {
-        ALOGE("PolicyUpdater not running, cannot update policy for " << appId << " " << user << " "
-              << privilege << " " << level);
-        return;
-    }
-    {
-        std::lock_guard<std::mutex> lock(m_mutex);
-        m_events.push_back({appId, user, privilege, level});
-    }
-    m_event.notify_one();
-}
-
-} // namespace Agent
-
-} // namespace AskUser
diff --git a/src/agent/main/PolicyUpdater.h b/src/agent/main/PolicyUpdater.h
deleted file mode 100644 (file)
index 1cfff28..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2016 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        PolicyUpdater.h
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       This file declares class of policy updater
- */
-
-#pragma once
-
-#include <atomic>
-#include <mutex>
-#include <string>
-#include <vector>
-#include <thread>
-#include <condition_variable>
-
-namespace AskUser {
-
-namespace Agent {
-
-class PolicyUpdater {
-public:
-    PolicyUpdater();
-    ~PolicyUpdater() { stop(); }
-
-    void start();
-    void stop();
-
-    void update(const std::string &appId, const std::string &user, const std::string &privilege,
-                const std::string &level);
-private:
-    struct PolicyInfo {
-        std::string appId;
-        std::string user;
-        std::string priv;
-        std::string level;
-    };
-    std::atomic<bool> m_running;
-    std::thread m_thread;
-
-    std::mutex m_mutex;
-    std::condition_variable m_event;
-    std::vector<PolicyInfo> m_events;
-    std::vector<PolicyInfo> m_eventsInternal;
-
-    void run();
-};
-
-} // namespace Agent
-
-} // namespace AskUser
diff --git a/src/common/policy/PkgInfo.h b/src/common/policy/PkgInfo.h
deleted file mode 100644 (file)
index e2f9e59..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- *  Copyright (c) 2017 Samsung Electronics Co.
- *
- *  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/common/policy/PkgInfo.h
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Definition of pkgmgr-info wrappers
- */
-
-#pragma once
-
-#include <string>
-#include <sys/types.h>
-#include <pkgmgr-info.h>
-
-#include <log/alog.h>
-
-struct PkgInfo {
-    PkgInfo(const std::string &pkgId, uid_t uid) : m_handle(nullptr) {
-        int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &m_handle);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_usr_pkginfo failed for " << pkgId  << " with " << ret);
-            m_handle = nullptr;
-        }
-    }
-    ~PkgInfo() {
-        if (m_handle)
-            pkgmgrinfo_pkginfo_destroy_pkginfo(m_handle);
-    }
-    const std::string mainAppId(){
-        if (!m_handle) {
-            return "";
-        }
-        char *mainAppId;
-        int ret = pkgmgrinfo_pkginfo_get_mainappid(m_handle, &mainAppId);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_mainappid failed  with " << ret);
-            return "";
-        }
-        return mainAppId ? mainAppId : "";
-    }
-    const std::string pkgLabel() {
-        if (!m_handle) {
-            return "";
-        }
-        char *pkgLabel;
-        int ret = pkgmgrinfo_pkginfo_get_label(m_handle, &pkgLabel);
-        if (ret != PMINFO_R_OK) {
-            ALOGE("pkgmgrinfo_pkginfo_get_label failed  with " << ret);
-            return "";
-        }
-        return pkgLabel ? pkgLabel : "";
-    }
-    pkgmgrinfo_pkginfo_h m_handle;
-};
diff --git a/src/common/policy/PrivilegeInfo.cpp b/src/common/policy/PrivilegeInfo.cpp
deleted file mode 100644 (file)
index 06d65b7..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- *  Copyright (c) 2016 Samsung Electronics Co.
- *
- *  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/agent/notification-daemon/Privilege.cpp
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Implementation of Privilege Info wrappers
- */
-
-#include <cstdlib>
-#include <memory>
-
-#include "PrivilegeInfo.h"
-
-#include <exception/Exception.h>
-#include <log/alog.h>
-
-#include <privilegemgr/privilege_info.h>
-#include <glib.h>
-
-namespace AskUser {
-
-struct GListWrap {
-    GListWrap(GList *_list) : list(_list) {}
-    ~GListWrap() { g_list_free_full(list, free);}
-    GList * get() { return list; }
-    GList *list;
-};
-
-namespace PrivilegeInfo {
-
-std::string getPrivacyDisplayName(const std::string &privilege) {
-    char *displayName = nullptr;
-    int ret = privilege_info_get_privacy_display(getPrivacyName(privilege).c_str(), &displayName);
-    if (ret != PRVMGR_ERR_NONE || !displayName) {
-        ALOGE("Unable to get privacy display name for <" << privilege << ">, err: <" << ret << ">");
-        return privilege;
-    }
-    std::unique_ptr<char, decltype(free)*> displaNamePtr(displayName, free);
-    return std::string(displayName);
-}
-
-std::string getPrivacyName(const std::string &privilege) {
-    char* privacyName = nullptr;
-    int ret = privilege_info_get_privacy_by_privilege(privilege.c_str(), &privacyName);
-    if (ret != PRVMGR_ERR_NONE || !privacyName) {
-        ALOGE("Unable to get privacy group for privilege: <" << privilege << ">, err: <" << ret << ">");
-        return privilege;
-    }
-
-    std::unique_ptr<char, decltype(free) *> privacyNamePtr(privacyName, free);
-    return std::string(privacyName);
-}
-
-std::vector<std::string> getPrivacyPrivileges(const std::string &privacy) {
-    GList *privilegeList = nullptr;
-
-    int ret = privilege_info_get_privilege_list_by_privacy(privacy.c_str(), &privilegeList);
-    if (ret != PRVMGR_ERR_NONE || !privilegeList) {
-        ALOGE("Unable to get privacy group list of privileges; err: <" << ret <<  ">" );
-        return {privacy};
-    }
-
-    GListWrap privList(privilegeList);
-    std::vector<std::string> privVector;
-    for (GList *l = privList.get(); l != NULL; l = l->next) {
-        privVector.push_back(static_cast<char*>(l->data));
-    }
-    return privVector;
-}
-
-}
-} /* namespace AskUser */
diff --git a/src/common/policy/PrivilegeInfo.h b/src/common/policy/PrivilegeInfo.h
deleted file mode 100644 (file)
index 0249b4f..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- *  Copyright (c) 2016 Samsung Electronics Co.
- *
- *  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/agent/notification-daemon/Privilege.h
- * @author      Zofia Abramowska <z.abramowska@samsung.com>
- * @brief       Definition of Privilege Info wrappers
- */
-
-#pragma once
-
-#include <string>
-#include <vector>
-
-namespace AskUser {
-
-namespace PrivilegeInfo {
-    std::string getPrivacyDisplayName(const std::string &privilege);
-    std::string getPrivacyName(const std::string &privilege);
-    std::vector<std::string> getPrivacyPrivileges(const std::string &privacy);
-};
-
-} /* namespace AskUser */
-
-
index 8fb5b93a40aabc3eacf0d2d471efebd5f0ac2ebb..ca55528819072fed2d126dd9052b6e5cdd9c4d10 100644 (file)
@@ -34,6 +34,9 @@ namespace Protocol {
 typedef int ConnectionFd;
 typedef int RequestId;
 
+const ConnectionFd INVALID_FD = -1;
+const RequestId INVALID_ID = -1;
+
 enum FdMask {
     READ = 1,
     WRITE = 2,
index 5986288128059bf07fb1edf8cc0c60ca61b9fa30..80e724cb6de294ac29a696af7766dbf250b315c5 100644 (file)
@@ -15,6 +15,7 @@ PKG_CHECK_MODULES(ASKUSER_NOTIFICATION_DEP
     capi-ui-efl-util
     capi-system-info
     security-privilege-manager
+    security-manager
 )
 
 INCLUDE_DIRECTORIES(SYSTEM
@@ -30,10 +31,12 @@ INCLUDE_DIRECTORIES(
 SET(ASKUSER_NOTIFICATION_SOURCES
     ${NOTIF_PATH}/main.cpp
     ${NOTIF_PATH}/Logic.cpp
+    ${NOTIF_PATH}/PolicyUpdater.cpp
     ${NOTIF_PATH}/ServerCallbacks.cpp
+    ${NOTIF_PATH}/policy/Policy.cpp
+    ${NOTIF_PATH}/policy/PrivilegeInfo.cpp
     ${NOTIF_PATH}/ui/Po.cpp
     ${ASKUSER_PATH}/common/log/alog.cpp
-    ${ASKUSER_PATH}/common/policy/PrivilegeInfo.cpp
     ${NOTIF_PATH}/ui/Popupper.cpp
    )
 
index 7997bdb821765b64b20d3974a64921ef02912c9d..c4e4d9de9d9fa2f4b69dd91d0af0349bbc307b2e 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <exception/Exception.h>
 #include <exception/ErrnoException.h>
+#include <policy/Policy.h>
+#include "PolicyUpdater.h"
 #include "ServerCallbacks.h"
 
 namespace AskUser {
@@ -37,16 +39,16 @@ namespace AskUser {
 namespace Notification {
 
 void Logic::addChannelFd(Protocol::ConnectionFd fd, const Protocol::Credentials &creds) {
-    // TODO check if doesn't already exist
-    // TODO translate to appId, pkgId and appIds from package
     auto it = m_connToInfo.find(fd);
     if (it != m_connToInfo.end()) {
         ALOGE("Connection with fd : " << fd << " already exists. Closing connection");
         m_clientChannel->process(fd, 0);
         return;
     }
-    (void)creds;
-    ConnectionInfo connInfo;
+
+    std::string appId, pkgId;
+    identifyApp(creds.label, appId, pkgId);
+    ConnectionInfo connInfo{appId, pkgId};
     m_connToInfo.insert(it, std::make_pair(fd, connInfo));
 }
 
@@ -55,19 +57,22 @@ void Logic::updateChannelFd(Protocol::ConnectionFd fd, Ecore_Fd_Handler_Flags fl
     if (it != m_fdInfo.end()) {
         m_fdInfo[fd].status = FdChange::CHANGE;
         ecore_main_fd_handler_del(m_fdInfo[fd].handler);
+    } else {
+        m_fdInfo[fd].status = FdChange::NONE;
     }
 
     auto handler = ecore_main_fd_handler_add(fd, flags, &Logic::clientChHandler, this, nullptr, nullptr);
     if (handler == nullptr) {
         ALOGE("Couldn't set fd handler");
+        m_fdInfo[fd].status = FdChange::DEL;
         return;
     }
     m_fdInfo[fd].handler = handler;
-    m_fdInfo[fd].status = FdChange::NONE;
 }
 
 void Logic::removeChannelFd(Protocol::ConnectionFd fd) {
     m_connToInfo.erase(fd);
+
     auto it = m_fdInfo.find(fd);
     if (it == m_fdInfo.end()) {
         return;
@@ -93,7 +98,6 @@ Eina_Bool Logic::clientChHandler(void *data, Ecore_Fd_Handler *handler) {
 }
 
 Eina_Bool Logic::processChannel(int fd, int mask) {
-    m_fdInfo[fd].status = FdChange::NONE;
     updateChannel(fd, mask);
 
     switch (m_fdInfo[fd].status) {
@@ -101,15 +105,17 @@ Eina_Bool Logic::processChannel(int fd, int mask) {
         return ECORE_CALLBACK_RENEW;
     case FdChange::DEL: {
         // Remove all pending events from this fd
-        auto queueIt = std::remove_if(m_pendingEvents.begin(), m_pendingEvents.end(), [fd](const FdEvent &fdEvent) {return fdEvent.fd == fd;});
+        auto queueIt = std::remove_if(m_pendingEvents.begin(), m_pendingEvents.end(),
+                [fd](const FdEvent &fdEvent) {return fdEvent.id.fd == fd;});
         m_pendingEvents.erase(queueIt, m_pendingEvents.end());
 
         // Handle next event if removed active fd
-        if (fd == m_currentFd) {
+        if (fd == m_currentEvent.fd) {
             finishCurrentEvent();
             processEvents();
         }
         m_fdInfo.erase(fd);
+        m_clientChannel->process(fd, 0);
         return ECORE_CALLBACK_CANCEL;
     }
     case FdChange::CHANGE:
@@ -120,26 +126,31 @@ Eina_Bool Logic::processChannel(int fd, int mask) {
     }
 }
 
-void Logic::addEvent(IUIEvent *event) {
-    FdEvent fdEvent{m_currentFd, std::unique_ptr<IUIEvent>(event)};
+void Logic::addEvent(EventId id, IUIEvent *event) {
+    FdEvent fdEvent{id, std::unique_ptr<IUIEvent>(event)};
     m_pendingEvents.emplace_back(std::move(fdEvent));
     processEvents();
 }
 
-void Logic::popup(Protocol::ConnectionFd fd, Protocol::RequestId id, std::string &&privilege) {
-    (void)fd;
-    (void)id;
-    (void)privilege;
+void Logic::popup(Protocol::ConnectionFd fd, Protocol::RequestId id, const std::string &privilege) {
+    auto it = m_connToInfo.find(fd);
+    if (it == m_connToInfo.end()) {
+        ALOGE("Got request to non existing fd " << fd);
+        return;
+    }
+
+    auto &pkgId = it->second.pkgId;
+    addEvent({fd, id}, new EventPopupCheck(&m_popupper, pkgId, privilege));
 }
 
 bool Logic::isEventProcessed() {
-    return m_currentEventId != -1;
+    return m_currentEvent.fd != Protocol::INVALID_FD;
 }
 
 void Logic::finishCurrentEvent() {
-    m_popupper.popupClose(m_currentEventId);
-    m_currentEventId = -1;
-    m_currentFd = -1;
+    m_popupper.popupClose();
+    m_currentEvent = EventId();
+    m_pendingEvents.pop_front();
 }
 
 void Logic::processEvents() {
@@ -152,10 +163,6 @@ void Logic::processEvents() {
         return;
 
     FdEvent fdEvent = std::move(m_pendingEvents.front());
-    m_currentFd = fdEvent.fd;
-    m_currentEventId = fdEvent.event->getId();
-    m_pendingEvents.pop_front();
-
     fdEvent.event->process();
 }
 
@@ -205,7 +212,7 @@ void Logic::init() {
     init_agent_log();
     m_popupper.setLocale();
     m_popupper.initialize();
-    m_popupper.registerPopupResponseHandler([&](int popupId, NResponseType response) { popupResponse(popupId, response);});
+    m_popupper.registerPopupResponseHandler([&](NResponseType response) { popupResponse(response);});
 
     registerSignalFd();
 
@@ -218,12 +225,17 @@ void Logic::run() {
     m_popupper.shutdown();
 }
 
-void Logic::popupResponse(int popupId, NResponseType response) {
-    if (popupId != m_currentEventId) {
-        ALOGD("Got different popup id than is being processed");
+void Logic::popupResponse(NResponseType response) {
+    auto it = m_connToInfo.find(m_currentEvent.fd);
+    if (it == m_connToInfo.end()) {
+        ALOGE("Got response from inactive fd " << m_currentEvent.fd);
         return;
     }
+
+    EventPopupCheck *event = dynamic_cast<EventPopupCheck*>(m_pendingEvents.front().event.get());
+
     int clientResponse;
+    std::string level;
     // TODO translate ui response to policy result
     switch (response) {
     case NResponseType::Deny:
@@ -231,12 +243,12 @@ void Logic::popupResponse(int popupId, NResponseType response) {
         break;
     case NResponseType::DenyAlways:
         clientResponse = ASKUSER_DENY_FOREVER;
+        level = "Deny";
         break;
     case NResponseType::Allow:
-        clientResponse = ASKUSER_ALLOW_ONCE;
-        break;
     case NResponseType::AllowAlways:
         clientResponse = ASKUSER_ALLOW_FOREVER;
+        level = "Allow";
         break;
     case NResponseType::None:
         clientResponse = ASKUSER_NONE;
@@ -247,8 +259,15 @@ void Logic::popupResponse(int popupId, NResponseType response) {
     default:
         clientResponse = -255; // error
     }
+    if (!level.empty()) {
+        if (!PolicyUpdater::update(it->second.appId, event->getPrivilege(), level)) {
+            ALOGE("Failed to update policy, returning DENY_ONCE in this case");
+            clientResponse = ASKUSER_DENY_ONCE;
+        }
+    }
+
     // TODO create popup ids containing fd and request id
-    m_clientChannel->popupResponse(-1, popupId, clientResponse);
+    m_clientChannel->popupResponse(m_currentEvent.fd, m_currentEvent.id, clientResponse);
     finishCurrentEvent();
     processEvents();
 }
index e1bec91a3e90a0801fced901872f554db84c8d0f..4a50c000444749b9827bf53180cae2f696ffc83a 100644 (file)
@@ -41,7 +41,7 @@ public:
     private:
         std::string m_msg;
     };
-    Logic() : m_currentFd(-1), m_currentEventId(-1), m_isFdDeleted(false) {}
+    Logic() : m_currentEvent{-1, -1} {}
     void init();
     void run();
     void stop();
@@ -50,11 +50,17 @@ public:
     void updateChannelFd(Protocol::ConnectionFd fd, Ecore_Fd_Handler_Flags flags);
     void removeChannelFd(Protocol::ConnectionFd fd);
 
-    void addEvent(IUIEvent *event);
-    void popup(Protocol::ConnectionFd fd, Protocol::RequestId id, std::string &&privilege);
+    void popup(Protocol::ConnectionFd fd, Protocol::RequestId id, const std::string &privilege);
 
     ~Logic() {}
 private:
+    struct EventId {
+        EventId() : fd(Protocol::INVALID_FD), id(Protocol::INVALID_ID) {}
+        EventId(Protocol::ConnectionFd _fd, Protocol::RequestId _id) : fd(_fd), id(_id) {}
+        Protocol::ConnectionFd fd;
+        Protocol::RequestId id;
+    };
+
     //Initialization
     void registerSignalFd();
     void prepareChannel();
@@ -65,43 +71,42 @@ private:
     static Eina_Bool signalHandler(void *data, Ecore_Fd_Handler *handler);
     //static Eina_Bool signalHandler(void *data, int type, void *event);
 
+    void addEvent(EventId id, IUIEvent *event);
     Eina_Bool processChannel(int fd, int mask);
+    bool identifyClient(const std::string &label, std::string &appId, std::string &pkgId);
     void processEvents();
     bool isEventProcessed();
     void finishCurrentEvent();
-    void popupResponse(int popupId, NResponseType response);
+    void popupResponse(NResponseType response);
 
     AskUser::Protocol::ChannelPtr m_clientChannel;
     Popupper m_popupper;
 
     struct FdEvent {
-        int fd;
+        EventId id;
         std::unique_ptr<IUIEvent> event;
     };
 
-    // Workaround for no information about to which fd event belongs to
-    int m_currentFd;
-    int m_currentEventId;
-    int m_isFdDeleted;
+    EventId m_currentEvent;
     std::deque<FdEvent> m_pendingEvents;
 
     enum class FdChange {
         NONE,
         ADD,
         CHANGE,
-        DEL
+        DEL,
     };
     struct FdInfo {
+        FdInfo() : handler(nullptr), status(FdChange::NONE) {}
         Ecore_Fd_Handler* handler;
         FdChange status;
     };
 
-    std::map<int, FdInfo> m_fdInfo;
+    std::map<Protocol::ConnectionFd, FdInfo> m_fdInfo;
 
     struct ConnectionInfo {
         std::string appId;
         std::string pkgId;
-        std::vector<std::string> apps;
     };
 
     std::map<Protocol::ConnectionFd, ConnectionInfo> m_connToInfo;
diff --git a/src/notification-daemon/PolicyUpdater.cpp b/src/notification-daemon/PolicyUpdater.cpp
new file mode 100644 (file)
index 0000000..2319dd3
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2016-2017 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        PolicyUpdater.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       This file implements class of policy updater
+ */
+
+#include <csignal>
+#include <unistd.h>
+
+#include <exception/Exception.h>
+#include <log/alog.h>
+
+#include "PolicyUpdater.h"
+#include <policy/Policy.h>
+#include <policy/PrivilegeInfo.h>
+
+namespace AskUser {
+
+namespace Notification {
+
+bool PolicyUpdater::update(const std::string &appId,
+                           const std::string &privilege, const std::string &level)
+{
+    try {
+        ALOGD("Policy update for: app: " << appId << ", privilege: " << privilege
+              << ", user:" << geteuid() << ", level: " << level);
+
+        static const std::string user = std::to_string(geteuid());
+        PolicyRequest req;
+
+        std::string privacyName = PrivilegeInfo::getPrivacyName(privilege);
+        if (privacyName.empty()) {
+            ALOGE("Unable to get privacy name for privilege " << privilege);
+            throw Exception("Unable to get privacy name for " + privilege);
+        }
+        auto privacyPrivs = PrivilegeInfo::getPrivacyPrivileges(privacyName);
+        if (privacyPrivs.empty()) {
+            ALOGE("Unable to get privacy privileges for privacy " << privacyName);
+            throw Exception("Unable to get privacy privileges for privacy " + privacyName);
+        }
+        ALOGD("Adding policy entry for : app: " << appId << ", privilege: "
+              << privilege  << ", user:" << user << ", level: "
+              << level);
+        for (auto &priv : privacyPrivs) {
+            PolicyEntry entry;
+            entry.setApp(appId);
+            entry.setUser(user);
+            entry.setPrivilege(priv);
+            entry.setLevel(level);
+            req.addEntry(std::move(entry));
+        }
+
+        req.updatePolicy();
+        ALOGD("Policy update sent");
+        return true;
+    } catch (AskUser::Exception &e) {
+        ALOGE("Failed to update policy in security-manager : " << e.what());
+    } catch (std::exception &e) {
+        ALOGE("Failed to update policy : " << e.what());
+    } catch (...) {
+        ALOGE("Failed to update policy with unknown exception");
+    }
+    return false;
+}
+
+} // namespace Agent
+
+} // namespace AskUser
diff --git a/src/notification-daemon/PolicyUpdater.h b/src/notification-daemon/PolicyUpdater.h
new file mode 100644 (file)
index 0000000..f91e88f
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2016 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        PolicyUpdater.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       This file declares class of policy updater
+ */
+
+#pragma once
+
+#include <atomic>
+#include <mutex>
+#include <string>
+#include <vector>
+#include <thread>
+#include <condition_variable>
+
+namespace AskUser {
+
+namespace Notification {
+
+namespace PolicyUpdater {
+    bool update(const std::string &appId, const std::string &privilege,
+                const std::string &level);
+};
+
+} // namespace Agent
+
+} // namespace AskUser
index b8c7825a0cd9ea4f9dd7d6d910f66f9bd2cb1112..5f438ba49f5e40c0aed795b506964fc4da4cff77 100644 (file)
@@ -49,7 +49,7 @@ void ServerCallbacks::updateConnection(ConnectionFd fd, int mask) {
 }
 
 void ServerCallbacks::popup(ConnectionFd fd, RequestId id, std::string &&privilege) {
-    m_service->popup(fd, id, std::move(privilege));
+    m_service->popup(fd, id, privilege);
 }
 
 } /* namespace Notification */
index f00cba490620c9597f905abbb8ac06af5f438a32..232756994406cb88c30cfb35704ce4cb660ad646 100644 (file)
@@ -31,32 +31,27 @@ namespace Notification {
 
 class IUIEvent {
 public:
-    IUIEvent(Popupper *popupper) : m_popupper(popupper), m_eventId(-1) {}
+    IUIEvent(Popupper *popupper) : m_popupper(popupper) {}
     virtual void process() = 0;
-    void setId(int id) { m_eventId = id; }
-    int getId() { return m_eventId; }
     virtual ~IUIEvent() {}
 protected:
     Popupper *m_popupper;
-    int m_eventId;
 };
 
 class EventPopupCheck : public IUIEvent {
 public:
-    EventPopupCheck(Popupper *popupper, int popupId, const std::string &appName,
-               const std::string &pkgName, const std::string &privilege)
-        : IUIEvent(popupper), m_popupId(popupId), m_appName(appName),
-          m_pkgName(pkgName), m_privilege(privilege)
-    {
-        setId(popupId);
+    EventPopupCheck(Popupper *popupper, const std::string &pkgName, const std::string &privilege)
+        : IUIEvent(popupper), m_pkgName(pkgName), m_privilege(privilege)
+    {}
+
+    std::string getPrivilege() {
+        return m_privilege;
     }
 
     virtual void process() {
-        m_popupper->popupCheck(m_popupId, m_appName, m_pkgName, m_privilege);
+        m_popupper->popupCheck(m_pkgName, m_privilege);
     }
 private:
-    int m_popupId;
-    std::string m_appName;
     std::string m_pkgName;
     std::string m_privilege;
 };
diff --git a/src/notification-daemon/policy/PkgInfo.h b/src/notification-daemon/policy/PkgInfo.h
new file mode 100644 (file)
index 0000000..e2f9e59
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co.
+ *
+ *  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/common/policy/PkgInfo.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of pkgmgr-info wrappers
+ */
+
+#pragma once
+
+#include <string>
+#include <sys/types.h>
+#include <pkgmgr-info.h>
+
+#include <log/alog.h>
+
+struct PkgInfo {
+    PkgInfo(const std::string &pkgId, uid_t uid) : m_handle(nullptr) {
+        int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &m_handle);
+        if (ret != PMINFO_R_OK) {
+            ALOGE("pkgmgrinfo_pkginfo_get_usr_pkginfo failed for " << pkgId  << " with " << ret);
+            m_handle = nullptr;
+        }
+    }
+    ~PkgInfo() {
+        if (m_handle)
+            pkgmgrinfo_pkginfo_destroy_pkginfo(m_handle);
+    }
+    const std::string mainAppId(){
+        if (!m_handle) {
+            return "";
+        }
+        char *mainAppId;
+        int ret = pkgmgrinfo_pkginfo_get_mainappid(m_handle, &mainAppId);
+        if (ret != PMINFO_R_OK) {
+            ALOGE("pkgmgrinfo_pkginfo_get_mainappid failed  with " << ret);
+            return "";
+        }
+        return mainAppId ? mainAppId : "";
+    }
+    const std::string pkgLabel() {
+        if (!m_handle) {
+            return "";
+        }
+        char *pkgLabel;
+        int ret = pkgmgrinfo_pkginfo_get_label(m_handle, &pkgLabel);
+        if (ret != PMINFO_R_OK) {
+            ALOGE("pkgmgrinfo_pkginfo_get_label failed  with " << ret);
+            return "";
+        }
+        return pkgLabel ? pkgLabel : "";
+    }
+    pkgmgrinfo_pkginfo_h m_handle;
+};
diff --git a/src/notification-daemon/policy/Policy.cpp b/src/notification-daemon/policy/Policy.cpp
new file mode 100644 (file)
index 0000000..5fefb59
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ *  Copyright (c) 2016-2017 Samsung Electronics Co.
+ *
+ *  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/agent/main/Policy.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Implementation of Policy wrappers
+ */
+
+#include <memory>
+
+#include <security-manager.h>
+
+#include <exception/Exception.h>
+#include <log/alog.h>
+
+#include "PkgInfo.h"
+#include "Policy.h"
+
+namespace {
+inline void throwOnSMError(std::string err, int ret)
+{
+    if (ret != SECURITY_MANAGER_SUCCESS)
+        throw AskUser::Exception(err + " : " + std::to_string(ret));
+}
+}
+
+namespace AskUser {
+
+void identifyApp(const std::string &client, std::string &appId, std::string &pkgLabel)
+{
+    char *pkgName = nullptr;
+    char *appName = nullptr;
+
+    int ret = security_manager_identify_app_from_cynara_client(client.c_str(), &pkgName, &appName);
+    std::unique_ptr<char, decltype(free)*> pkg_name_p(pkgName, free);
+    std::unique_ptr<char, decltype(free)*> app_name_p(appName, free);
+    throwOnSMError("security_manager_identify_app_from_cynara_client", ret);
+
+    if (!pkgName) {
+        ALOGE("Couldn't identify clients package id");
+        return;
+    }
+
+    PkgInfo pkgInfo(pkgName, geteuid());
+    if (!appName)
+        appId = pkgInfo.mainAppId();
+
+    pkgLabel = pkgInfo.pkgLabel();
+}
+
+PolicyEntry::PolicyEntry() {
+    throwOnSMError("security_manager_policy_entry_new",
+                                  security_manager_policy_entry_new(&m_entry));
+}
+
+PolicyEntry::PolicyEntry(PolicyEntry &&other) : m_entry(std::move(other.m_entry)) {
+    other.m_entry = nullptr;
+}
+
+PolicyEntry::~PolicyEntry() {
+    security_manager_policy_entry_free(m_entry);
+}
+
+void PolicyEntry::setApp(const std::string &appId) {
+    throwOnSMError("security_manager_policy_entry_set_application",
+                   security_manager_policy_entry_set_application(m_entry, appId.c_str()));
+}
+
+void PolicyEntry::setUser(const std::string &user) {
+    throwOnSMError("security_manager_policy_entry_set_user",
+                   security_manager_policy_entry_set_user(m_entry, user.c_str()));
+}
+
+void PolicyEntry::setPrivilege(const std::string &privilege) {
+    throwOnSMError("security_manager_policy_entry_set_privilege",
+                   security_manager_policy_entry_set_privilege(m_entry, privilege.c_str()));
+}
+
+void PolicyEntry::setLevel(const std::string &level) {
+    throwOnSMError("security_manager_policy_entry_admin_set_level",
+                   security_manager_policy_entry_set_level(m_entry, level.c_str()));
+}
+
+PolicyRequest::PolicyRequest() {
+    throwOnSMError("security_manager_policy_update_req_new",
+                   security_manager_policy_update_req_new(&m_req));
+}
+
+PolicyRequest::~PolicyRequest() {
+    m_entries.clear();
+    security_manager_policy_update_req_free(m_req);
+}
+
+void PolicyRequest::addEntry(PolicyEntry &&entry) {
+    throwOnSMError("security_manager_policy_update_req_add_entry",
+                   security_manager_policy_update_req_add_entry(m_req, entry.get()));
+    m_entries.emplace_back(std::move(entry));
+}
+
+void PolicyRequest::updatePolicy() {
+    throwOnSMError("security_manager_policy_update_send",
+                   security_manager_policy_update_send(m_req));
+}
+
+} /* namespace AskUser */
diff --git a/src/notification-daemon/policy/Policy.h b/src/notification-daemon/policy/Policy.h
new file mode 100644 (file)
index 0000000..3c9828b
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ *  Copyright (c) 2016-2017 Samsung Electronics Co.
+ *
+ *  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/agent/main/Policy.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of Policy wrappers
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+struct policy_entry;
+struct policy_update_req;
+
+namespace AskUser {
+
+void identifyApp(const std::string &client, std::string &appId, std::string &pkgLabel);
+
+class PolicyEntry {
+public:
+    PolicyEntry();
+    PolicyEntry(PolicyEntry &&other);
+    ~PolicyEntry();
+    void setApp(const std::string &appId);
+    void setUser(const std::string &user);
+    void setPrivilege(const std::string &privilege);
+    void setLevel(const std::string &level);
+
+    policy_entry *get() const { return m_entry; }
+
+private:
+    policy_entry *m_entry;
+};
+
+class PolicyRequest {
+public:
+    PolicyRequest();
+    ~PolicyRequest();
+    void addEntry(PolicyEntry &&entry);
+    void updatePolicy();
+
+private:
+    policy_update_req *m_req;
+    std::vector<PolicyEntry> m_entries;
+};
+
+} /* namespace AskUser */
diff --git a/src/notification-daemon/policy/PrivilegeInfo.cpp b/src/notification-daemon/policy/PrivilegeInfo.cpp
new file mode 100644 (file)
index 0000000..06d65b7
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co.
+ *
+ *  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/agent/notification-daemon/Privilege.cpp
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Implementation of Privilege Info wrappers
+ */
+
+#include <cstdlib>
+#include <memory>
+
+#include "PrivilegeInfo.h"
+
+#include <exception/Exception.h>
+#include <log/alog.h>
+
+#include <privilegemgr/privilege_info.h>
+#include <glib.h>
+
+namespace AskUser {
+
+struct GListWrap {
+    GListWrap(GList *_list) : list(_list) {}
+    ~GListWrap() { g_list_free_full(list, free);}
+    GList * get() { return list; }
+    GList *list;
+};
+
+namespace PrivilegeInfo {
+
+std::string getPrivacyDisplayName(const std::string &privilege) {
+    char *displayName = nullptr;
+    int ret = privilege_info_get_privacy_display(getPrivacyName(privilege).c_str(), &displayName);
+    if (ret != PRVMGR_ERR_NONE || !displayName) {
+        ALOGE("Unable to get privacy display name for <" << privilege << ">, err: <" << ret << ">");
+        return privilege;
+    }
+    std::unique_ptr<char, decltype(free)*> displaNamePtr(displayName, free);
+    return std::string(displayName);
+}
+
+std::string getPrivacyName(const std::string &privilege) {
+    char* privacyName = nullptr;
+    int ret = privilege_info_get_privacy_by_privilege(privilege.c_str(), &privacyName);
+    if (ret != PRVMGR_ERR_NONE || !privacyName) {
+        ALOGE("Unable to get privacy group for privilege: <" << privilege << ">, err: <" << ret << ">");
+        return privilege;
+    }
+
+    std::unique_ptr<char, decltype(free) *> privacyNamePtr(privacyName, free);
+    return std::string(privacyName);
+}
+
+std::vector<std::string> getPrivacyPrivileges(const std::string &privacy) {
+    GList *privilegeList = nullptr;
+
+    int ret = privilege_info_get_privilege_list_by_privacy(privacy.c_str(), &privilegeList);
+    if (ret != PRVMGR_ERR_NONE || !privilegeList) {
+        ALOGE("Unable to get privacy group list of privileges; err: <" << ret <<  ">" );
+        return {privacy};
+    }
+
+    GListWrap privList(privilegeList);
+    std::vector<std::string> privVector;
+    for (GList *l = privList.get(); l != NULL; l = l->next) {
+        privVector.push_back(static_cast<char*>(l->data));
+    }
+    return privVector;
+}
+
+}
+} /* namespace AskUser */
diff --git a/src/notification-daemon/policy/PrivilegeInfo.h b/src/notification-daemon/policy/PrivilegeInfo.h
new file mode 100644 (file)
index 0000000..0249b4f
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co.
+ *
+ *  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/agent/notification-daemon/Privilege.h
+ * @author      Zofia Abramowska <z.abramowska@samsung.com>
+ * @brief       Definition of Privilege Info wrappers
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace AskUser {
+
+namespace PrivilegeInfo {
+    std::string getPrivacyDisplayName(const std::string &privilege);
+    std::string getPrivacyName(const std::string &privilege);
+    std::vector<std::string> getPrivacyPrivileges(const std::string &privacy);
+};
+
+} /* namespace AskUser */
+
+
index 3a18ed212068c9700580599f3b8de075b25d9545..114fa2c3a49efb72cb34adcf4eb46ab39575b995 100644 (file)
@@ -34,7 +34,7 @@ public:
         ALLOW,
         DENY
     };
-    typedef std::function<void(int, NResponseType)> AnswerHandler;
+    typedef std::function<void(NResponseType)> AnswerHandler;
 
     IAnswerable(AnswerHandler handler) : m_handler(handler) {}
     virtual void process(enum Button) const = 0;
@@ -65,7 +65,7 @@ public:
         default:
             answer = NResponseType::Error;
         }
-        m_handler(m_popup->getId(), answer);
+        m_handler( answer);
     }
 private:
     PopupCheck *m_popup;
index b984fd54e12fb66d65f4e1a771cac4e76a3d0152..7ee82183967e5eb3b2ee194ea81767cf9ef71fd5 100644 (file)
@@ -99,7 +99,7 @@ std::string getPkgLabel(const std::string &pkgName) {
 
 namespace Notification {
 namespace Po {
-std::string createPopupCheckMsg(const std::string &, const std::string &pkgName, const std::string &priv) {
+std::string createPopupCheckMsg(const std::string &pkgName, const std::string &priv) {
     return makeFromFormat(getFormat(MsgType::MSG_POPUP_TEXT), getPkgLabel(pkgName).c_str(),
                           PrivilegeInfo::getPrivacyDisplayName(priv).c_str());
 }
index 0f7fab4287b7093450576daddfe86266538e8240..b702c938676551dc0dfde72590b27114d076cbc1 100644 (file)
@@ -26,7 +26,7 @@ namespace AskUser {
 namespace Notification {
 namespace Po {
 
-std::string createPopupCheckMsg(const std::string &appName, const std::string &pkgName, const std::string &priv);
+std::string createPopupCheckMsg(const std::string &pkgName, const std::string &priv);
 std::string createPrivilegeDescr(const std::string &priv);
 
 std::string getPopupTitleMsg();
index 5ba73805c0385bd3e1b46f64be8bd1d36890ded0..3882343addd670940185467252910f95eda501f8 100644 (file)
@@ -32,11 +32,9 @@ namespace Notification {
 
 class PopupCheckMobile : public PopupCheck {
 public:
-    PopupCheckMobile(Evas_Object *parent, int popupId, const std::string &msg)
+    PopupCheckMobile(Evas_Object *parent, const std::string &msg)
         : PopupCheck(parent, msg)
-    {
-        setId(popupId);
-    }
+    {}
     virtual void create() {
         // popup
         elm_popup_align_set(m_popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
index 74fe9ef5e9208d002d0534674f18be67cdef8acc..a58692c1067fd582de7e5ea748e0771a3d55fbae 100644 (file)
@@ -32,11 +32,9 @@ namespace Notification {
 
 class PopupCheckWearable : public PopupCheck {
 public:
-    PopupCheckWearable(Evas_Object *parent, int popupId, const std::string msg)
+    PopupCheckWearable(Evas_Object *parent, const std::string msg)
         : PopupCheck(parent, msg)
-    {
-        setId(popupId);
-    }
+    {}
     ~PopupCheckWearable() {}
 
     virtual void create() {
index ee31ec60c2384de76f9ea28f5e0102b5e5ff0afb..e8194bb824188bab93377f8cffe8abf3fe5cafe8 100644 (file)
@@ -68,12 +68,12 @@ void Popupper::unfocusedCb(void *data, Evas_Object *, void *)
     }
 }
 
-void Popupper::popupClose(int popupId)
+void Popupper::popupClose()
 {
     ALOGD("Window close");
     m_shouldRaise = false;
 
-    if (!m_elementPtr || popupId == -1 || popupId != m_elementPtr->getId())
+    if (!m_elementPtr)
         ALOGD("Closing non-existing popup");
 
     m_elementPtr.reset(nullptr);
@@ -101,7 +101,7 @@ Eina_Bool Popupper::hwKeyClickedCb(void *data, int type, void *event)
     ALOGD("HW button pressed. type <" << type << "> pressed key is <" << ev->key << ">");
     if ((!strcmp("XF86Home", ev->key) || !strcmp("XF86Back", ev->key)) && runner->m_elementPtr) {
         ALOGD("Respond as deny once.");
-        runner->m_popupResponseHandler(runner->m_elementPtr->getId(), NResponseType::None);
+        runner->m_popupResponseHandler(NResponseType::None);
     }
     return EINA_TRUE;
 }
@@ -154,21 +154,21 @@ void Popupper::show() {
     evas_object_show(m_win);
 }
 
-void Popupper::popupCheck(int popupId, const std::string &appId, const std::string &pkgId, const std::string &priv) {
+void Popupper::popupCheck(const std::string &pkgId, const std::string &priv) {
     std::string profileName = getProfileName();
     PopupCheck *popup;
     try {
         if (profileName[0] != 'w' && profileName[0] != 'W') {
             // Not wearable
-            popup = new PopupCheckMobile(m_win, popupId, Po::createPopupCheckMsg(appId, pkgId, priv));
+            popup = new PopupCheckMobile(m_win, Po::createPopupCheckMsg(pkgId, priv));
         } else {
             // Wearable
-            popup = new PopupCheckWearable(m_win, popupId, Po::createPopupCheckMsg(appId, pkgId, priv));
+            popup = new PopupCheckWearable(m_win, Po::createPopupCheckMsg(pkgId, priv));
         }
         popup->create();
     } catch (const std::exception &e) {
         ALOGE("Failed to create popup check : " << e.what());
-        m_popupResponseHandler(popupId, NResponseType::Error);
+        m_popupResponseHandler(NResponseType::Error);
         return;
     }
 
index 478f6829d15c4d41f62227f29721030a75ea8a6b..5855d53271959b1d69d7efdad98729e6fe06a526 100644 (file)
@@ -39,7 +39,7 @@ namespace Notification {
 
 class Popupper {
 public:
-    typedef std::function<void(int, NResponseType)> PopupHandler;
+    typedef std::function<void(NResponseType)> PopupHandler;
 
     Popupper() = default;
     void initialize();
@@ -47,9 +47,9 @@ public:
     void registerPopupResponseHandler(PopupHandler handler);
     void start();
 
-    void popupCheck(int popupId, const std::string &appId, const std::string &pkgId, const std::string &priv);
+    void popupCheck(const std::string &pkgId, const std::string &priv);
 
-    void popupClose(int popupId);
+    void popupClose();
     void stop();
     void shutdown();
 
index 998b8e6b4113be736b0acf04d9d7b5167474d2d5..c59ad01df8591dc4b73a1e708cf049f1ae209504 100644 (file)
@@ -28,14 +28,11 @@ namespace Notification {
 
 class UIElement {
 public:
-    explicit UIElement(Evas_Object *parent) : m_parent(parent), m_elementId(-1) {}
-    void setId(int id) { m_elementId = id;}
-    int getId() { return m_elementId; }
+    explicit UIElement(Evas_Object *parent) : m_parent(parent) {}
     virtual void create() = 0;
     virtual ~UIElement() {}
 protected:
     Evas_Object *m_parent;
-    int m_elementId;
 };
 
 } /* namespace Notification */