Handle wildcard pattern to match with allowed_package 05/260205/4
authorIlho Kim <ilho159.kim@samsung.com>
Tue, 22 Jun 2021 03:01:34 +0000 (12:01 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Tue, 22 Jun 2021 09:44:15 +0000 (18:44 +0900)
'fnmatch' provides shell style wildcard pattern matching

Change-Id: I08fd657b12e8af7d8126249c8bebe94a434b680a
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/lib/res_info/res_pkg_info.cc
src/lib/res_info/res_pkg_info.hh

index cdee50e8ebe6f12fadcfa6eec7e8b811efc13179..4f44bfbb33a7de11c07fc1d12c974247acf6073d 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <fnmatch.h>
+
 #include "lib/res_info/res_app_info.hh"
 #include "lib/common/log_private.hh"
 
@@ -41,15 +43,22 @@ bool ResPkgInfo::IsBlocked() {
 
 bool ResPkgInfo::IsAllowedPkg(const std::string& pkgid,
     const std::set<std::string>& app_priv) {
-  if (!allowed_pkg_priv_map_.count(pkgid))
-    return false;
-
-  for (const std::string& required_priv : allowed_pkg_priv_map_[pkgid]) {
-    if (!app_priv.count(required_priv))
-      return false;
+  for (const auto& it : allowed_pkg_priv_list_) {
+    if (fnmatch(it.first.c_str(), pkgid.c_str(), FNM_NOESCAPE))
+      continue;
+
+    bool has_required_priv = true;
+    for (const std::string& required_priv : it.second) {
+      if (!app_priv.count(required_priv)) {
+        has_required_priv = false;
+        break;
+      }
+    }
+    if (has_required_priv)
+      return true;
   }
 
-  return true;
+  return false;
 }
 
 void ResPkgInfo::SetBlocking() {
@@ -66,7 +75,7 @@ std::shared_ptr<ResPkgInfo> ResPkgInfo::CreateResPkgInfo(
   char* res_type;
   char* res_version;
   char* root_path;
-  AllowedPkgPrivMap allowed_pkg_priv_map;
+  AllowedPkgPrivList allowed_pkg_priv_list;
 
   if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) != PMINFO_R_OK)
     return nullptr;
@@ -86,8 +95,8 @@ std::shared_ptr<ResPkgInfo> ResPkgInfo::CreateResPkgInfo(
         if (!allowed_package)
           return 0;
 
-        AllowedPkgPrivMap* allowed_pkg_priv_map =
-            reinterpret_cast<AllowedPkgPrivMap*>(user_data);
+        AllowedPkgPrivList* allowed_pkg_priv_list =
+            reinterpret_cast<AllowedPkgPrivList*>(user_data);
 
         std::set<std::string> priv_set;
 
@@ -104,13 +113,14 @@ std::shared_ptr<ResPkgInfo> ResPkgInfo::CreateResPkgInfo(
             }, &priv_set) != PMINFO_R_OK)
           return 0;
 
-        (*allowed_pkg_priv_map)[allowed_package] = std::move(priv_set);
+        allowed_pkg_priv_list->emplace_back(
+            std::make_pair(allowed_package, std::move(priv_set)));
         return 0;
-      }, &allowed_pkg_priv_map) != PMINFO_R_OK)
+      }, &allowed_pkg_priv_list) != PMINFO_R_OK)
     return nullptr;
 
   return std::shared_ptr<ResPkgInfo>(new ResPkgInfo(pkgid, res_type,
-      res_version, root_path, std::move(allowed_pkg_priv_map)));
+      res_version, root_path, std::move(allowed_pkg_priv_list)));
 }
 
 std::shared_ptr<ResPkgInfo> ResPkgInfo::CreateResPkgInfo(
index b1d79be173d90b2233948f3aeac472a2f0a45c04..da1cc2101583f9fc6d6ff7ff941b0d5f37286217 100644 (file)
@@ -23,7 +23,8 @@
 #include <memory>
 #include <string>
 
-using AllowedPkgPrivMap = std::map<std::string, std::set<std::string>>;
+using AllowedPkgPrivList =
+    std::vector<std::pair<std::string, std::set<std::string>>>;
 
 namespace amd {
 
@@ -46,19 +47,19 @@ class ResPkgInfo {
 
  private:
   ResPkgInfo(std::string pkgid, std::string res_type, std::string res_version,
-      std::string root_path, AllowedPkgPrivMap priv_map)
+      std::string root_path, AllowedPkgPrivList priv_list)
           : pkgid_(std::move(pkgid)),
             res_type_(std::move(res_type)),
             res_version_(std::move(res_version)),
             root_path_(std::move(root_path)),
-            allowed_pkg_priv_map_(std::move(priv_map)),
+            allowed_pkg_priv_list_(std::move(priv_list)),
             is_blocked_(false) {}
 
   std::string pkgid_;
   std::string res_type_;
   std::string res_version_;
   std::string root_path_;
-  AllowedPkgPrivMap allowed_pkg_priv_map_;
+  AllowedPkgPrivList allowed_pkg_priv_list_;
   bool is_blocked_;
 };