diff --git a/BUILD.gn b/BUILD.gn index aaef20f8ff2fc1413ae26c3a753dd27470691eea..1f04f79e0308c6f81a10127d842bae7c5b7a1707 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -154,7 +154,12 @@ template("hdcd_source_set") { } if (image_name == "system") { defines += [ "HDC_TRACE" ] - external_deps += [ "hitrace:hitrace_meter" ] + external_deps += [ + "hitrace:hitrace_meter", + "os_account:account_iam_innerkits", + "os_account:os_account_innerkits", + ] + defines += [ "HDC_SUPPORT_GETUID" ] } } diff --git a/src/common/base.cpp b/src/common/base.cpp index 95fd3ea220ee6ae6be99d924583594008baa694c..3404a1b1b89d01de552557221d6ccc540fe818f4 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -42,6 +42,9 @@ #endif #include #include "server_cmd_log.h" +#ifdef HDC_SUPPORT_GETUID +#include "os_account_manager.h" +#endif // HDC_SUPPORT_GETUID using namespace std::chrono; namespace Hdc { @@ -3094,5 +3097,33 @@ void CloseOpenFd(void) GetTimeString(timeStr); return StringFormat("[%s] %u %s\n", timeStr.c_str(), targetSessionId, cmdStr.c_str()); } +#ifndef HDC_HOST + std::string GetBundlePath() + { +#ifdef HDC_SUPPORT_GETUID + int32_t id = GetUserId(); + if (id < 0) { + return ""; + } + std::string idString = std::to_string(static_cast(id)); + return DEBUG_BUNDLE_PATH_HALF + idString + DEBUG_BUNDLE_PATH_END; +#else + return DEBUG_BUNDLE_PATH; +#endif // HDC_SUPPORT_GETUID + } + +#ifdef HDC_SUPPORT_GETUID + int32_t GetUserId() + { + std::vector ids; + OHOS::ErrCode err = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids); + if (err != 0) { + WRITE_LOG(LOG_FATAL, "Get OS account local id failed, errno is %d", err); + return -1; + } + return ids.empty() ? -1 : ids[0]; + } +#endif // HDC_SUPPORT_GETUID +#endif // HDC_HOST } // namespace Base } // namespace Hdc diff --git a/src/common/base.h b/src/common/base.h index f593960c6fd97490bf5c576a5e59f387e9b9afc3..502e9025c62c34653aab25f25551f80dc89654b8 100644 --- a/src/common/base.h +++ b/src/common/base.h @@ -306,6 +306,12 @@ namespace Base { void ProcessCmdLogs(); void UpdateEncrpytTCPCache(); bool GetEncrpytTCPSwitch(); +#ifndef HDC_HOST + std::string GetBundlePath(); +#ifdef HDC_SUPPORT_GETUID + int32_t GetUserId(); +#endif +#endif } // namespace base } // namespace Hdc diff --git a/src/common/define.h b/src/common/define.h index 1210ef1045f187d81a1027bd1846db433ac612ed..ffafc0928dde03f2289296f3a479006159fba1e1 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -138,6 +138,8 @@ constexpr int32_t RSA_KEY_BITS = 3072; const string WHITE_SPACES = " \t\n\r"; const string UT_TMP_PATH = "/tmp/hdc-ut"; const string DEBUG_BUNDLE_PATH = "/mnt/debug/100/debug_hap/"; +const string DEBUG_BUNDLE_PATH_HALF = "/mnt/debug/"; +const string DEBUG_BUNDLE_PATH_END = "/debug_hap/"; const string LOG_FILE_NAME = "hdc.log"; const string LOG_FILE_NAME_PREFIX = "hdc-"; const string LOG_FILE_SUFFIX = ".log"; diff --git a/src/common/file.cpp b/src/common/file.cpp index 591219de9b0358c764de2ad10a5dbeea4c9e7264..789ec882560341e03c03c35814a5cacfad8f3e17 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -183,7 +183,8 @@ bool HdcFile::ValidateAndSetPaths(CtxFile *context, int argc, char **argv, int & bool HdcFile::CheckSandboxSubPath(CtxFile *context, string &resolvedPath) { - string fullPath = SANDBOX_ROOT_DIR + context->bundleName; +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + context->bundleName; string appDir(fullPath); appDir = Base::CanonicalizeSpecPath(appDir); fullPath = fullPath + Base::GetPathSep() + context->inputLocalPath; @@ -207,6 +208,10 @@ bool HdcFile::CheckSandboxSubPath(CtxFile *context, string &resolvedPath) return false; } return true; +#else + WRITE_LOG(LOG_DEBUG, "CheckSandboxSubPath is not supported on host"); + return false; +#endif } bool HdcFile::IsPathInsideSandbox(const string &path, const string &appDir) @@ -451,15 +456,16 @@ bool HdcFile::ParseAndValidateOptions(uint8_t *payload, const int payloadSize) bool HdcFile::CheckBundleAndPath() { if (!taskInfo->serverOrDaemon && IsValidBundlePath(ctxNow.bundleName)) { - string fullPath = SANDBOX_ROOT_DIR + ctxNow.bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + ctxNow.bundleName + Base::GetPathSep(); fullPath.append(ctxNow.inputLocalPath); ctxNow.localPath = fullPath; - string resolvedPath; if (!CheckSandboxSubPath(&ctxNow, resolvedPath)) { WRITE_LOG(LOG_DEBUG, "SlaveCheck CheckSandboxSubPath false."); return false; } +#endif } else if (!taskInfo->serverOrDaemon && ctxNow.bundleName.size() > 0) { LogMsg(MSG_FAIL, "[E005101] Invalid bundle name: %s", ctxNow.bundleName.c_str()); diff --git a/src/common/transfer.cpp b/src/common/transfer.cpp index 96e0bf4e1e2c57472612afc8057b6f20df2e6bf6..8d315ae75ea3ea1add09deb74daf2a806bfb3c03 100644 --- a/src/common/transfer.cpp +++ b/src/common/transfer.cpp @@ -445,8 +445,12 @@ void HdcTransferBase::OnFileOpenFailed(CtxFile *context) bool HdcTransferBase::IsValidBundlePath(const string &bundleName) { - string fullPath = SANDBOX_ROOT_DIR + bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + bundleName + Base::GetPathSep(); return Base::CheckBundleName(bundleName) && access(fullPath.c_str(), F_OK) == 0; +#else + return true; +#endif } void HdcTransferBase::RemoveSandboxRootPath(std::string &srcStr, const std::string &bundleName) @@ -454,13 +458,15 @@ void HdcTransferBase::RemoveSandboxRootPath(std::string &srcStr, const std::stri if (taskInfo->serverOrDaemon || !IsValidBundlePath(bundleName)) { return; } - string fullPath = SANDBOX_ROOT_DIR + bundleName + Base::GetPathSep(); +#ifndef HDC_HOST + string fullPath = Base::GetBundlePath() + bundleName + Base::GetPathSep(); size_t pos = 0; if ((pos = srcStr.find(fullPath)) != std::string::npos) { srcStr = srcStr.replace(pos, fullPath.length(), ""); } else { WRITE_LOG(LOG_DEBUG, "fullPath:%s, srcStr:%s", fullPath.c_str(), srcStr.c_str()); } +#endif } void HdcTransferBase::OnFileOpen(uv_fs_t *req) diff --git a/src/daemon/daemon_unity.cpp b/src/daemon/daemon_unity.cpp index 69161d12663f2264e83bdc63730c65dedbec9557..6d635e20d03f41497d40bc8bcf4aaee2f2105768 100644 --- a/src/daemon/daemon_unity.cpp +++ b/src/daemon/daemon_unity.cpp @@ -75,13 +75,14 @@ bool HdcDaemonUnity::AsyncCmdOut(bool finish, int64_t exitStatus, const string r bool HdcDaemonUnity::CheckbundlePath(const string &bundleName, string &mountPath) { - if (access(DEBUG_BUNDLE_PATH.c_str(), F_OK) != 0 || !Base::CheckBundleName(bundleName)) { - WRITE_LOG(LOG_FATAL, "debug path %s not found", DEBUG_BUNDLE_PATH.c_str()); + string debugBasePath = Base::GetBundlePath(); + if (access(debugBasePath.c_str(), F_OK) != 0 || !Base::CheckBundleName(bundleName)) { + WRITE_LOG(LOG_FATAL, "debug path %s not found", debugBasePath.c_str()); LogMsg(MSG_FAIL, "[E003001] Invalid bundle name: %s", bundleName.c_str()); return false; } string targetPath = ""; - targetPath += DEBUG_BUNDLE_PATH; + targetPath += debugBasePath; targetPath += bundleName; if ((access(targetPath.c_str(), F_OK) != 0)) { WRITE_LOG(LOG_FATAL, "bundle mount path %s not found", targetPath.c_str());