Implement checkPrivilege() 33/136933/22
authorPiotr Sawicki <p.sawicki2@partner.samsung.com>
Mon, 3 Jul 2017 14:36:54 +0000 (16:36 +0200)
committerPiotr Sawicki <p.sawicki2@partner.samsung.com>
Thu, 6 Jul 2017 18:01:11 +0000 (20:01 +0200)
Change-Id: I91768ce30cced026bc7f15bc5381be5d5df7afc6

src/client/impl/ApiInterfaceImpl.cpp
src/common/policy/Policy.cpp
src/common/policy/Policy.h

index 1a3ec91d8e90a3d25c0ed366d818bbdef4b7aa8b..24dfad6bc990229a8c51360c98a17a95547170b7 100644 (file)
  * @brief       The definition of ApiInterfaceImpl.
  */
 
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <log/alog.h>
+#include <policy/Policy.h>
 #include <askuser-notification/ask-user-client-channel.h>
 #include <askuser-notification/ask-user-types.h>
 
@@ -82,8 +87,34 @@ int ApiInterfaceImpl::process(int fd, int events)
 
 askuser_check_result ApiInterfaceImpl::checkPrivilege(const std::string &privilege)
 {
-    // TODO use PolicyFetchRequest
-    (void) privilege;
+    std::string appId = getOwnAppId();
+
+    PolicyEntry filter;
+    filter.setApp(appId);
+    filter.setUser(std::to_string(geteuid()));
+    filter.setPrivilege(privilege);
+
+    PolicyFetchRequest fetch(std::move(filter));
+    auto policies = fetch.fetchPolicy();
+
+    if (policies.size() != 1) {
+        ALOGE("Unusual situation, there are " << policies.size() << " policies for (" << appId << ", " << geteuid() << ", " << privilege << ")");
+        return ASKUSER_CHECK_RESULT_DENY;
+    }
+
+    auto level = policies.front().getLevel();
+
+    if (level == "Allow") {
+        return ASKUSER_CHECK_RESULT_ALLOW;
+    }
+
+    if (level == "Deny") {
+        return ASKUSER_CHECK_RESULT_DENY;
+    }
+
+    if (level == "Ask user") {
+        return ASKUSER_CHECK_RESULT_ASK;
+    }
 
     return ASKUSER_CHECK_RESULT_DENY;
 }
index 61acd5194be407abbd7e99cc611404227e1644e1..f8d4818bd7da0d6376a40c2606c8feec8b8bae12 100644 (file)
@@ -67,6 +67,23 @@ void identifyApp(const std::string &client, std::string &appId, std::string &pkg
     pkgLabel = pkgInfo.pkgLabel();
 }
 
+std::string getOwnAppId()
+{
+    char *pkgName = nullptr;
+    char *appName = nullptr;
+
+    int ret = security_manager_identify_app_from_pid(getpid(), &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_pid", ret);
+
+    PkgInfo pkgInfo(pkgName, geteuid());
+    if (!appName)
+        return pkgInfo.mainAppId();
+
+    return std::string();
+}
+
 PolicyEntry::PolicyEntry() {
     throwOnSMError("security_manager_policy_entry_new",
                                   security_manager_policy_entry_new(&m_entry));
index f3abb9345452293db44079f9847e7460dde7703d..0f7aca12c9c3961702ec41cecf59b05f0948f02c 100644 (file)
@@ -30,6 +30,7 @@ struct policy_update_req;
 namespace AskUser {
 
 void identifyApp(const std::string &client, std::string &appId, std::string &pkgLabel);
+std::string getOwnAppId();
 
 class PolicyEntry {
 public: