diff --git a/services/distributeddataservice/app/distributed_data.cfg b/services/distributeddataservice/app/distributed_data.cfg index 0ee10ab520561d76aa2dd3bdacb0a686bbb2295a..fa59771cb2686bab311b678a126b771ecaf00a43 100644 --- a/services/distributeddataservice/app/distributed_data.cfg +++ b/services/distributeddataservice/app/distributed_data.cfg @@ -2,6 +2,9 @@ "jobs":[{ "name" : "boot", "cmds" : [ + "mkdir /data/service/el1/public/database 02770 ddms ddms", + "mkdir /data/service/el1/public/database/distributeddata 02770 ddms ddms", + "mkdir /data/service/el1/public/database/distributeddata/meta 02770 ddms ddms", "start distributeddata" ] } diff --git a/services/distributeddataservice/app/distributed_data.rc b/services/distributeddataservice/app/distributed_data.rc deleted file mode 100644 index 5f9dc66e49ab43be660bac0d643dc5eb147432c1..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/app/distributed_data.rc +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# 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. - -on boot - start distributeddata - -service distributeddata /system/bin/sa_main /system/profile/distributeddata.xml - class z_core - capabilities DAC_READ_SEARCH - user system - group system shell readproc ddms - seclabel u:r:distributeddata:s0 - writepid /dev/cpuset/foreground/tasks /dev/stune/foreground/tasks /dev/blkio/foreground/tasks diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 262d86e07a7ba059a2c73b4ad0a0820704a59611..b59f3ea407600d10dab1add57cf7a21feeef0438 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -133,7 +133,7 @@ Status KvStoreDataService::GetKvStore(const Options &options, const AppId &appId const int32_t uid = IPCSkeleton::GetCallingUid(); param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); if (param.trueAppId.empty()) { - ZLOGW("appId empty(permission issues?)"); + ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), uid); return Status::PERMISSION_DENIED; } @@ -195,7 +195,7 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId const int32_t uid = IPCSkeleton::GetCallingUid(); param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); if (param.trueAppId.empty()) { - ZLOGW("appId empty(permission issues?)"); + ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), uid); return Status::PERMISSION_DENIED; } @@ -333,7 +333,7 @@ Status KvStoreDataService::UpdateMetaData(const Options &options, const KvStoreP metaData.storeId = kvParas.storeId; metaData.userId = AccountDelegate::GetInstance()->GetCurrentAccountId(kvParas.bundleName); metaData.uid = IPCSkeleton::GetCallingUid(); - metaData.version = KVSTORE_META_VERSION; + metaData.version = STORE_VERSION; metaData.securityLevel = options.securityLevel; metaData.dataDir = kvStoreUserManager.GetDbDir(kvParas.bundleName, options); diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 26ba9cfc8b705a65f8f07e7b1e593842ebffc10a..0ec6dcdaf03fc99147af03f91b6c67529a256cd3 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -43,7 +43,7 @@ class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { public: // record kvstore meta version for compatible, should update when modify kvstore meta structure. - static constexpr int KVSTORE_META_VERSION = 1; + static constexpr uint32_t STORE_VERSION = 0x03000001; explicit KvStoreDataService(bool runOnCreate = false); explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 9a41816b80726551f84bf9b806df53a5818e30ac..046808d08cf05cc1ecded12f02fbc78634682e05 100755 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -60,8 +60,7 @@ KvStoreMetaManager::KvStoreMetaManager() ZLOGE("CloseMetaKvstore return error status: %d", static_cast(result)); } }), - metaDBDirectory_(Constant::Concatenate({ - Constant::ROOT_PATH_DE, "/", Constant::SERVICE_NAME, "/", Constant::META_DIR_NAME })), + metaDBDirectory_("/data/service/el1/public/database/distributeddata/meta"), kvStoreDelegateManager_(META_DB_APP_ID, Constant::GetDefaultHarmonyAccountName()) { ZLOGI("begin."); @@ -98,14 +97,15 @@ void KvStoreMetaManager::InitMetaData() ZLOGI("get meta failed."); return; } + auto uid = getuid(); const std::string accountId = AccountDelegate::GetInstance()->GetCurrentAccountId(); - const std::string userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(getuid()); + const std::string userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid); auto metaKey = GetMetaKey(userId, "default", META_DB_APP_ID, Constant::SERVICE_META_DB_NAME); struct KvStoreMetaData metaData { .appId = META_DB_APP_ID, .appType = "default", .bundleName = META_DB_APP_ID, - .dataDir = "default", + .dataDir = metaDBDirectory_, .deviceAccountId = userId, .deviceId = DeviceKvStoreImpl::GetLocalDeviceId(), .isAutoSync = false, @@ -115,9 +115,9 @@ void KvStoreMetaManager::InitMetaData() .schema = "", .storeId = Constant::SERVICE_META_DB_NAME, .userId = accountId, - .uid = -1, - .version = KvStoreDataService::KVSTORE_META_VERSION, - .securityLevel = SecurityLevel::NO_LABEL, + .uid = int32_t(uid), + .version = META_STORE_VERSION, + .securityLevel = SecurityLevel::S1, }; std::string jsonStr = metaData.Marshal(); std::vector value(jsonStr.begin(), jsonStr.end()); @@ -137,9 +137,7 @@ void KvStoreMetaManager::InitMetaParameter() ZLOGE("create directories failed"); return; } - // change mode for directories to 0755, and for files to 0600. - DirectoryUtils::ChangeModeDirOnly(metaDBDirectory_, Constant::DEFAULT_MODE_DIR); - DirectoryUtils::ChangeModeFileOnly(metaDBDirectory_, Constant::DEFAULT_MODE_FILE); + ret = ForceCreateDirectory(metaDBDirectory_ + "/backup"); DistributedDB::KvStoreConfig kvStoreConfig {metaDBDirectory_}; kvStoreDelegateManager_.SetKvStoreConfig(kvStoreConfig); @@ -257,16 +255,13 @@ Status KvStoreMetaManager::CheckUpdateServiceMeta(const std::vector &me return Status::DB_ERROR; } - KvStoreAppManager::PathType pathType = KvStoreAppManager::PATH_DE; DistributedDB::Key dbKey = metaKey; DistributedDB::Value dbValue = val; DistributedDB::DBStatus dbStatus; DistributedDB::CipherPassword dbPassword; - const std::string userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(getuid()); - const std::string accountId = AccountDelegate::GetInstance()->GetCurrentAccountId(); - std::initializer_list backList = {accountId, "_", META_DB_APP_ID, "_", Constant::SERVICE_META_DB_NAME}; + std::initializer_list backList = {META_DB_APP_ID, "_", Constant::SERVICE_META_DB_NAME}; std::string backupName = Constant::Concatenate(backList); - std::initializer_list backFullList = {BackupHandler::GetBackupPath(userId, pathType), "/", + std::initializer_list backFullList = {metaDBDirectory_, "/backup/", BackupHandler::GetHashedBackupName(backupName)}; auto backupFullName = Constant::Concatenate(backFullList); @@ -1005,16 +1000,6 @@ AppDistributedKv::ChangeLevelType KvStoreMetaManager::MetaDeviceChangeListenerIm return AppDistributedKv::ChangeLevelType::HIGH; } -void KvStoreMetaManager::ToJson(json &j, const KvStoreMetaData &k) -{ - j = json(k.Marshal()); -} - -void KvStoreMetaManager::FromJson(const json &j, KvStoreMetaData &k) -{ - k.Unmarshal(j); -} - Status KvStoreMetaManager::QueryKvStoreMetaDataByDeviceIdAndAppId(const std::string &devId, const std::string &appId, KvStoreMetaData &val) { @@ -1141,26 +1126,6 @@ void KvStoreMetaData::Unmarshal(const nlohmann::json &jObject) isDirty = Serializable::GetVal(jObject, DIRTY_KEY, json::value_t::boolean, isDirty); } -bool KvStoreMetaData::CheckChiefValues(const nlohmann::json &jObject) -{ - return Serializable::CheckJsonValue(jObject, KVSTORE_TYPE, json::value_t::number_unsigned) && - Serializable::CheckJsonValue(jObject, BACKUP, json::value_t::boolean) && - Serializable::CheckJsonValue(jObject, ENCRYPT, json::value_t::boolean) && - Serializable::CheckJsonValue(jObject, AUTO_SYNC, json::value_t::boolean) && - Serializable::CheckJsonValue(jObject, APP_ID, json::value_t::string) && - Serializable::CheckJsonValue(jObject, USER_ID, json::value_t::string) && - Serializable::CheckJsonValue(jObject, STORE_ID, json::value_t::string) && - Serializable::CheckJsonValue(jObject, BUNDLE_NAME, json::value_t::string) && - Serializable::CheckJsonValue(jObject, DEVICE_ACCOUNT_ID, json::value_t::string) && - Serializable::CheckJsonValue(jObject, DATA_DIR, json::value_t::string); -} - -bool Serializable::CheckJsonValue(const nlohmann::json &j, const std::string &name, json::value_t type) -{ - auto it = j.find(name); - return it != j.end() && it->type() == type; -} - template T Serializable::GetVal(const json &j, const std::string &name, json::value_t type, const T &val) { @@ -1242,17 +1207,6 @@ bool KvStoreMetaManager::GetFullMetaData(std::map &entrie return true; } -int KvStoreMetaManager::GetSecurityLevelByBundleName(const std::string &bundleName) -{ - KvStoreMetaData kvStoreMetaData; - auto getKvStoreMetaBMeta = GetKvStoreMetaByType(KvStoreMetaData::BUNDLE_NAME, bundleName, kvStoreMetaData); - if (!getKvStoreMetaBMeta) { - ZLOGE("getkvstore meta by type failed"); - return SecurityLevel::NO_LABEL; - } - return kvStoreMetaData.securityLevel; -} - bool KvStoreMetaManager::GetKvStoreMetaByType(const std::string &name, const std::string &val, KvStoreMetaData &metaData) { diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.h b/services/distributeddataservice/app/src/kvstore_meta_manager.h index a3fc0307fc11e5570bb282d5f4eedcccdf05ac2d..413910b6cdb13cfc97808863b577eb25fb6d1ddb 100755 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.h +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.h @@ -41,7 +41,6 @@ struct Serializable { using json = nlohmann::json; template static T GetVal(const json &j, const std::string &name, json::value_t type, const T &def); - static bool CheckJsonValue(const json &j, const std::string &name, json::value_t type); static json ToJson(const std::string &jsonStr); }; @@ -103,17 +102,11 @@ struct KvStoreMetaData { std::string Marshal() const; void Unmarshal(const json &jObject); - static bool CheckChiefValues(const json &jObject); - static inline std::string GetAppId(const json &jObject) { return Serializable::GetVal(jObject, APP_ID, json::value_t::string, ""); } - static inline std::string GetBundleName(const json &jObject) - { - return Serializable::GetVal(jObject, BUNDLE_NAME, json::value_t::string, ""); - } static inline std::string GetStoreId(const json &jObject) { return Serializable::GetVal(jObject, STORE_ID, json::value_t::string, ""); @@ -150,24 +143,9 @@ private: static constexpr const char *KVSTORE_TYPE = "kvStoreType"; }; -struct DelegateGuard { - using Fn = std::function; - Fn action_; - DelegateGuard(Fn action) : action_(std::forward(action)) {} - - ~DelegateGuard() - { - if (action_) { - action_(); - } - } - DelegateGuard() = delete; - DelegateGuard(const DelegateGuard &) = delete; - DelegateGuard &operator=(const DelegateGuard &) = delete; -}; - class KvStoreMetaManager { public: + static constexpr uint32_t META_STORE_VERSION = 0x03000001; using NbDelegate = std::unique_ptr>; @@ -243,10 +221,6 @@ public: Status QueryKvStoreMetaDataByDeviceIdAndAppId(const std::string &devId, const std::string &appId, KvStoreMetaData &val); - // json rule - void ToJson(nlohmann::json &j, const KvStoreMetaData &k); - - void FromJson(const nlohmann::json &j, KvStoreMetaData &k); Status GetKvStoreMeta(const std::vector &metaKey, KvStoreMetaData &kvStoreMetaData); @@ -270,8 +244,6 @@ private: Status GetStategyMeta(const std::string &key, std::map> &strategies); - int GetSecurityLevelByBundleName(const std::string &bundleName); - bool GetKvStoreMetaByType(const std::string &name, const std::string &val, KvStoreMetaData &metaData); class KvStoreMetaObserver : public DistributedDB::KvStoreObserver { diff --git a/services/distributeddataservice/framework/include/checker/checker_manager.h b/services/distributeddataservice/framework/include/checker/checker_manager.h index 6b8f9aa105f65b04e9214e94cbbfe82affc8966f..e703a46ed72732f010148608bf1f3f606d547d16 100644 --- a/services/distributeddataservice/framework/include/checker/checker_manager.h +++ b/services/distributeddataservice/framework/include/checker/checker_manager.h @@ -21,7 +21,7 @@ namespace OHOS { namespace DistributedData { class CheckerManager { public: - static constexpr pid_t INVALID_UID = 0; + static constexpr pid_t INVALID_UID = -1; struct Trust { std::string bundleName; std::string appId;