Fix smack labeling for lib rpk
[platform/core/appfw/app-installers.git] / src / common / shared_dirs.cc
index 8e83748..88fd0ba 100644 (file)
@@ -83,7 +83,7 @@ const char kSkelAppDir[] = "skel/apps_rw";
 const char kExternalStoragePrivilege[] =
     "http://tizen.org/privilege/externalstorage.appdata";
 const char kSystemShareGroupName[] = "system_share";
-
+const char kSubssesionDir[] = "subsession";
 // the input path should be root directory of package.
 // for example: "../apps_rw/pkgid" or "../.shared/pkgid"
 bool SetPackageDirectoryOwnerAndPermissions(const bf::path& path, uid_t uid) {
@@ -150,7 +150,7 @@ bool DeleteSharedDataDirectories(const bf::path& path,
     return false;
 
   std::vector<std::string> shared_dirs(kSharedDataEntries);
-  for (auto entry : shared_dirs) {
+  for (const auto& entry : shared_dirs) {
     if (!ci::RemoveAll(path / entry / pkgid))
       return false;
   }
@@ -161,7 +161,8 @@ bool DeleteSharedDataDirectories(const bf::path& path,
 bool CreateSharedDataDirectories(const bf::path& path,
                                  const std::string& pkgid) {
   if (!ci::CreateDir(path / kSharedDir / pkgid / kData) ||
-      !ci::CreateDir(path / kSharedTmpDir / pkgid))
+      !ci::CreateDir(path / kSharedTmpDir / pkgid) ||
+      !ci::CreateDir(path / pkgid / kShared))
     return false;
 
   bf::current_path(path / pkgid / kShared);
@@ -411,6 +412,24 @@ bool PerformExternalDirectoryCreationForUser(uid_t user,
   if (CreateExternalUserDirectories(user, pkgid, storage_apps_path.string()))
     return false;
 
+  for (auto& lw_user : GetLightUserList(user)) {
+    bf::path storage_apps_lw_path = storage_apps_path
+        / kSubssesionDir / lw_user / "apps";
+    if (!bf::exists(storage_apps_lw_path)) {
+      bs::error_code error;
+      bf::create_directories(storage_apps_lw_path, error);
+      if (error) {
+        LOG(ERROR) << "Failed to create directory: "
+                   << storage_apps_lw_path.c_str();
+        return false;
+      }
+    }
+
+    if (CreateExternalUserDirectories(user, pkgid,
+        storage_apps_lw_path.string()))
+      return false;
+  }
+
   return true;
 }
 
@@ -424,13 +443,25 @@ bool PerformExternalDirectoryDeletionForUser(uid_t user,
   }
 
   bf::path storage_apps_path = bf::path(storage_path) / "apps";
-  return DeleteDirectories(
-      GetDirectoryPathForStorage(user, storage_apps_path.string()), pkgid);
+  if (!DeleteDirectories(
+      GetDirectoryPathForStorage(user, storage_apps_path.string()), pkgid))
+    return false;
+
+  for (auto& lw_user : GetLightUserList(user)) {
+    bf::path storage_apps_lw_path =
+        storage_apps_path / kSubssesionDir / lw_user / "apps";
+    if (!DeleteDirectories(
+            GetDirectoryPathForStorage(user, storage_apps_lw_path.string()),
+            pkgid))
+      return false;
+  }
+
+  return true;
 }
 
 bool PerformExternalDirectoryCreationForAllUsers(const std::string& pkgid) {
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     if (!PerformExternalDirectoryCreationForUser(std::get<0>(l),
                                                  pkgid))
       LOG(WARNING) << "Could not create external storage directories for user: "
@@ -441,7 +472,7 @@ bool PerformExternalDirectoryCreationForAllUsers(const std::string& pkgid) {
 
 bool PerformExternalDirectoryCreationForAllPkgs() {
   UserList list = ci::GetUserList();
-  for (auto l  : list) {
+  for (const auto& l  : list) {
     uid_t uid = std::get<0>(l);
     pkgmgrinfo_pkginfo_filter_h filter_handle = nullptr;
     int ret = pkgmgrinfo_pkginfo_filter_create(&filter_handle);
@@ -482,7 +513,7 @@ bool PerformExternalDirectoryCreationForAllPkgs() {
 
 bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid) {
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
     ci::PkgQueryInterface pkg_query(pkgid, uid);
     LOG(DEBUG) << "Deleting directories for user: " << uid;
@@ -512,8 +543,8 @@ bool CreatePerUserStorageDirectories(const std::string& pkgid, bool trusted,
   }
 
   std::string error_message;
-  if (!RegisterSecurityContextForPath(pkgid, skel_apps_rw / pkgid,
-          kGlobalUserUid, is_readonly, &error_message)) {
+  if (!RegisterSecurityContextForPath(pkgid, {}, skel_apps_rw / pkgid,
+      kGlobalUserUid, is_readonly, &error_message)) {
     LOG(ERROR) << "Failed to register security context for path: "
                << skel_apps_rw / pkgid << ", error_message: " << error_message;
     return false;
@@ -527,33 +558,42 @@ bool CreatePerUserStorageDirectories(const std::string& pkgid, bool trusted,
 
   // create per user dir
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    bf::path apps_rw = std::get<2>(l) / "apps_rw";
+    bf::path owner_apps_rw = std::get<2>(l) / "apps_rw";
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l)
+          / kSubssesionDir / lw_user / "apps_rw");
+
     LOG(DEBUG) << "Creating directories for user: " << uid;
-    if (!::CreateStorageDirectories(apps_rw, pkgid, trusted, shareddata,
-         additional_dirs)) {
-      LOG(ERROR) << "Failed to create storage directory for user: " << uid;
-      return false;
-    }
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!::CreateStorageDirectories(apps_rw, pkgid, trusted, shareddata,
+          additional_dirs)) {
+        LOG(ERROR) << "Failed to create storage directory for user: " << uid;
+        return false;
+      }
 
-    if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
-      return false;
+      if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
+        return false;
 
-    if (shareddata) {
-      std::vector<std::string> shared_dirs(kSharedDataEntries);
-      for (auto entry : shared_dirs) {
-        bf::path shared_dst = apps_rw / entry / pkgid;
-        if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
-          return false;
+      if (shareddata) {
+        std::vector<std::string> shared_dirs(kSharedDataEntries);
+        for (const auto& entry : shared_dirs) {
+          bf::path shared_dst = apps_rw / entry / pkgid;
+          if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+            return false;
+        }
       }
-    }
 
-    if (!RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid,
-            false, &error_message)) {
-      LOG(ERROR) << "Failed to register security context for path: "
-                 << apps_rw / pkgid << ", error_message: " << error_message;
-      return false;
+      if (!RegisterSecurityContextForPath(pkgid, {}, apps_rw / pkgid, uid,
+          false, &error_message)) {
+        LOG(ERROR) << "Failed to register security context for path: "
+                   << apps_rw / pkgid << ", error_message: " << error_message;
+        return false;
+      }
     }
   }
 
@@ -575,19 +615,28 @@ bool DeletePerUserStorageDirectories(const std::string& pkgid,
 
   // delete per user dir
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    bf::path apps_rw = std::get<2>(l) / "apps_rw";
+    bf::path owner_apps_rw = std::get<2>(l) / "apps_rw";
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
     LOG(DEBUG) << "Deleting directories for user: " << uid;
-    if (!ci::RemoveAll(apps_rw / pkgid)) {
-      LOG(ERROR) << "Failed to delete storage directory for user: " << uid;
-      return false;
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!ci::RemoveAll(apps_rw / pkgid)) {
+        LOG(ERROR) << "Failed to delete storage directory for user: " << uid;
+        return false;
+      }
     }
+  }
 
-    if (!ci::DeletePerUserSharedDataDir(pkgid)) {
-      LOG(ERROR) << "Failed to delete per user shared data dir";
-      return false;
-    }
+  if (!ci::DeletePerUserSharedDataDir(pkgid)) {
+    LOG(ERROR) << "Failed to delete per user shared data dir";
+    return false;
   }
 
   return true;
@@ -607,7 +656,7 @@ bool CreateStorageDirectories(const boost::filesystem::path& path,
 
   if (shareddata) {
     std::vector<std::string> shared_dirs(kSharedDataEntries);
-    for (auto entry : shared_dirs) {
+    for (const auto& entry : shared_dirs) {
       bf::path shared_dst = path / entry / pkgid;
       if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
         return false;
@@ -615,8 +664,8 @@ bool CreateStorageDirectories(const boost::filesystem::path& path,
   }
 
   std::string error_message;
-  if (!RegisterSecurityContextForPath(pkgid, path / pkgid, uid, false,
-                                      &error_message)) {
+  if (!RegisterSecurityContextForPath(pkgid, {}, path / pkgid, uid, false,
+      &error_message)) {
     LOG(ERROR) << "Failed to register security context for path: " << path
                << ", error_message: " << error_message;
     return false;
@@ -645,7 +694,7 @@ bool DeleteSharedDirectories(const bf::path& path,
 
 bool DeleteUserExternalDirectories(const std::string& pkgid) {
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
     ci::PkgQueryInterface pkg_query(pkgid, uid);
     if (pkg_query.IsPackageInstalled()) {
@@ -659,7 +708,14 @@ bool DeleteUserExternalDirectories(const std::string& pkgid) {
     if (!DeleteDirectories(apps_rw, pkgid)) {
       return false;
     }
+
+    for (auto& lw_user : GetLightUserList(uid)) {
+      bf::path apps_rw_lw(std::get<2>(l) / kSubssesionDir / lw_user / "apps_rw");
+      if (!DeleteDirectories(apps_rw_lw, pkgid))
+        return false;
+    }
   }
+
   return true;
 }
 
@@ -670,24 +726,36 @@ bool CreateGlobalAppSymlinksForAllUsers(const std::string& pkgid) {
     return false;
   }
 
-  bool result = true;
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
     LOG(DEBUG) << "Creating symlinks for uid: " << uid;
     // check installed user private app.
     ci::PkgQueryInterface pkg_query(pkgid, uid);
     if (pkg_query.IsPackageInstalled())
       continue;
-    bf::path apps_rw(std::get<2>(l) / "apps_rw");
-    bf::path dst_dir = apps_rw / pkgid;
-    if (!bf::exists(dst_dir)) {
-      LOG(WARNING) << "dst_dir not exists";
-      continue;
+
+    bf::path owner_apps_rw = std::get<2>(l) / "apps_rw";
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      bf::path dst_dir = apps_rw / pkgid;
+      if (!bf::exists(dst_dir)) {
+        LOG(WARNING) << "dst_dir not exists";
+        continue;
+      }
+
+      if (!CreateSymlinkFiles(src_dir, dst_dir))
+        return false;
     }
-    result = CreateSymlinkFiles(src_dir, dst_dir);
   }
-  return result;
+
+  return true;
 }
 
 bool CreateGlobalAppSymlinksForUser(const std::string& pkgid, uid_t uid) {
@@ -716,24 +784,36 @@ bool DeleteGlobalAppSymlinksForAllUsers(const std::string& pkgid) {
     return true;
   }
 
-  bool result = true;
   UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
     LOG(DEBUG) << "Deleting symlinks for uid: " << uid;
+
     // check installed user private app.
     ci::PkgQueryInterface pkg_query(pkgid, uid);
     if (pkg_query.IsPackageInstalled())
       continue;
-    bf::path apps_rw(std::get<2>(l) / "apps_rw");
-    bf::path dst_dir = apps_rw / pkgid;
-    if (!bf::exists(dst_dir)) {
-      LOG(WARNING) << "dst_dir not exists";
-      continue;
+
+    bf::path owner_apps_rw = std::get<2>(l) / "apps_rw";
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      bf::path dst_dir = apps_rw / pkgid;
+      if (!bf::exists(dst_dir)) {
+        LOG(WARNING) << "dst_dir not exists";
+        continue;
+      }
+
+      if (!DeleteSymlinkFiles(src_dir, dst_dir))
+        return false;
     }
-    result = DeleteSymlinkFiles(src_dir, dst_dir);
   }
-  return result;
+
+  return true;
 }
 
 bool DeleteGlobalAppSymlinksForUser(const std::string& pkgid, uid_t uid) {
@@ -775,8 +855,8 @@ bool CreateSharedDataDir(const std::string& pkgid, uid_t uid) {
 
   bf::path path = apps_rw / pkgid;
   std::string error_message;
-  if (!ci::RegisterSecurityContextForPath(pkgid, path, uid, false,
-          &error_message)) {
+  if (!ci::RegisterSecurityContextForPath(pkgid, {}, path, uid, false,
+      &error_message)) {
     LOG(ERROR) << "Failed to register security context for path: " << path
                << ", error_message: " << error_message;
     return false;
@@ -794,8 +874,8 @@ bool CreatePerUserSharedDataDir(const std::string& pkgid) {
     return false;
 
   std::string error_message;
-  if (!ci::RegisterSecurityContextForPath(pkgid, skel_apps_rw / pkgid,
-          kGlobalUserUid, false, &error_message)) {
+  if (!ci::RegisterSecurityContextForPath(pkgid, {}, skel_apps_rw / pkgid,
+      kGlobalUserUid, false, &error_message)) {
     LOG(ERROR) << "Failed to register security context for path: "
                << skel_apps_rw / pkgid << ", error_message: " << error_message;
     return false;
@@ -803,26 +883,34 @@ bool CreatePerUserSharedDataDir(const std::string& pkgid) {
 
   // create per user dir
   ci::UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
     LOG(DEBUG) << "Adding shareddata directory for uid: " << uid;
 
-    bf::path apps_rw = ci::GetRootAppPath(false, uid);
-    if (!CreateSharedDataDirectories(apps_rw, pkgid))
-      return false;
+    bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
 
-    std::vector<std::string> shared_dirs(kSharedDataEntries);
-    for (auto entry : shared_dirs) {
-      bf::path shared_dst = apps_rw / entry / pkgid;
-      if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!CreateSharedDataDirectories(apps_rw, pkgid))
         return false;
-    }
 
-    if (!ci::RegisterSecurityContextForPath(pkgid, apps_rw / pkgid, uid,
-        false, &error_message)) {
-      LOG(ERROR) << "Failed to register security context for path: "
-                 << apps_rw / pkgid << ", error_message: " << error_message;
-      return false;
+      std::vector<std::string> shared_dirs(kSharedDataEntries);
+      for (const auto& entry : shared_dirs) {
+        bf::path shared_dst = apps_rw / entry / pkgid;
+        if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+          return false;
+      }
+
+      if (!ci::RegisterSecurityContextForPath(pkgid, {}, apps_rw / pkgid, uid,
+                                              false, &error_message)) {
+        LOG(ERROR) << "Failed to register security context for path: "
+                   << apps_rw / pkgid << ", error_message: " << error_message;
+        return false;
+      }
     }
   }
 
@@ -841,13 +929,21 @@ bool DeletePerUserSharedDataDir(const std::string& pkgid) {
     return false;
 
   ci::UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    bf::path apps_rw = ci::GetRootAppPath(false, uid);
-    if (!DeleteSharedDataDirectories(apps_rw, pkgid))
-      return false;
-  }
 
+    bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!DeleteSharedDataDirectories(apps_rw, pkgid))
+        return false;
+    }
+  }
   return true;
 }
 
@@ -863,10 +959,19 @@ bool BackupPerUserSharedDataDir(const std::string& pkgid) {
     return false;
 
   ci::UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    if (!BackupSharedDataDir(pkgid, uid))
-      return false;
+    bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!BackupSharedDataDirectories(apps_rw, pkgid))
+        return false;
+    }
   }
 
   return true;
@@ -874,7 +979,28 @@ bool BackupPerUserSharedDataDir(const std::string& pkgid) {
 
 bool RestoreSharedDataDir(const std::string& pkgid, uid_t uid) {
   bf::path apps_rw = ci::GetRootAppPath(false, uid);
-  return RestoreSharedDataDirectories(apps_rw, pkgid);
+  if (!RestoreSharedDataDirectories(apps_rw, pkgid))
+    return false;
+
+  if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
+    return false;
+
+  std::vector<std::string> shared_dirs(kSharedDataEntries);
+  for (const auto& entry : shared_dirs) {
+    bf::path shared_dst = apps_rw / entry / pkgid;
+    if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+      return false;
+  }
+
+  std::string error_message;
+  if (!RegisterSecurityContextForPath(pkgid, {}, apps_rw / pkgid, uid, false,
+                                      &error_message)) {
+    LOG(ERROR) << "Failed to register security context for path: " << apps_rw
+               << ", error_message: " << error_message;
+    return false;
+  }
+
+  return true;
 }
 
 bool RestorePerUserSharedDataDir(const std::string& pkgid) {
@@ -883,11 +1009,38 @@ bool RestorePerUserSharedDataDir(const std::string& pkgid) {
   if (!RestoreSharedDataDirectories(skel_apps_rw, pkgid))
     return false;
 
+  std::string error_message;
   ci::UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    if (!RestoreSharedDataDir(pkgid, uid))
-      return false;
+    bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!RestoreSharedDataDirectories(apps_rw, pkgid))
+        return false;
+
+      if (!::SetPackageDirectoryOwnerAndPermissions(apps_rw / pkgid, uid))
+        return false;
+
+      std::vector<std::string> shared_dirs(kSharedDataEntries);
+      for (const auto& entry : shared_dirs) {
+        bf::path shared_dst = apps_rw / entry / pkgid;
+        if (!::SetPackageDirectoryOwnerAndPermissions(shared_dst, uid))
+          return false;
+      }
+
+      if (!RegisterSecurityContextForPath(pkgid, {}, apps_rw / pkgid, uid,
+          false, &error_message)) {
+        LOG(ERROR) << "Failed to register security context for path: "
+                   << apps_rw / pkgid << ", error_message: " << error_message;
+        return false;
+      }
+    }
   }
 
   return true;
@@ -905,10 +1058,19 @@ bool RemoveBackupPerUserSharedDataDir(const std::string& pkgid) {
     return false;
 
   ci::UserList list = ci::GetUserList();
-  for (auto l : list) {
+  for (const auto& l : list) {
     uid_t uid = std::get<0>(l);
-    if (!RemoveBackupSharedDataDir(pkgid, uid))
-      return false;
+    bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
+    std::vector<bf::path> apps_rw_paths;
+    apps_rw_paths.push_back(std::move(owner_apps_rw));
+    for (auto& lw_user : GetLightUserList(uid))
+      apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
+                              "apps_rw");
+
+    for (auto& apps_rw : apps_rw_paths) {
+      if (!RemoveBackupSharedDataDirectories(apps_rw, pkgid))
+        return false;
+    }
   }
 
   return true;