diff --git a/datamgr_service/datamgr_service.gni b/datamgr_service/datamgr_service.gni index 6ec402a19d78db68d0e97b6f7d131cf7793fb8c7..c3fa0d49c708f6b63b21ca268c4fc142ca183954 100644 --- a/datamgr_service/datamgr_service.gni +++ b/datamgr_service/datamgr_service.gni @@ -26,6 +26,7 @@ datashare_path = "//foundation/distributeddatamgr/data_share" ipc_core_path = "//foundation/communication/ipc/interfaces/innerkits/ipc_core" device_manager_path = "//foundation/distributedhardware/device_manager" + declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || diff --git a/datamgr_service/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/datamgr_service/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index 152b38ad9e45d741a0d96ebccaff898ae384ec75..5dd42f3fdf063d2eeac6ee443eee7cc0fc05c458 100644 --- a/datamgr_service/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/datamgr_service/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -32,7 +32,7 @@ using namespace OHOS::EventFwk; using namespace OHOS::AAFwk; using namespace OHOS::DistributedData; using namespace Security::AccessToken; - AccountDelegate::BaseInstance AccountDelegate::getInstance_ = AccountDelegateNormalImpl::GetBaseInstance; +AccountDelegate::BaseInstance AccountDelegate::getInstance_ = AccountDelegateNormalImpl::GetBaseInstance; AccountDelegate *AccountDelegateNormalImpl::GetBaseInstance() { static AccountDelegateNormalImpl accountDelegate; diff --git a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 65ff559f27c4d4da25a64445808c7c3ff5511914..77488f11d8d3448abcfaffbd18bd74862046b322 100644 --- a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -50,7 +50,7 @@ void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) std::string bundleName = want.GetElement().GetBundleName(); int32_t userId = want.GetIntParam(USER_ID, -1); int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); - ZLOGI("bundleName:%s, user:%d, appIndex:%d", bundleName.c_str(), userId, appIndex); + ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex); callback(bundleName, userId, appIndex); return true; }); @@ -87,7 +87,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); CommonEventSubscribeInfo info(matchingSkills); auto subscriber = std::make_shared(info); @@ -102,7 +102,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared } for (auto &meta : storeMetaData) { if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("uninstalled bundleName:%s, stordId:%s", bundleName.c_str(), meta.storeId.c_str()); + ZLOGI("uninstalled bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); MetaDataManager::GetInstance().DelMeta(meta.GetKey()); MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true); MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); @@ -114,7 +114,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared } }; - auto replacedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + auto updatedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); std::string prefix = StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); @@ -125,7 +125,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared } for (auto &meta : storeMetaData) { if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { - ZLOGI("updated bundleName:%s, stordId:%s", bundleName.c_str(), meta.storeId.c_str()); + ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(), meta.storeId.c_str()); MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); } } @@ -133,7 +133,7 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, removedCallback); subscriber->RegisterCallback(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, removedCallback); - subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED, replacedCallback); + subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, updatedCallback); subscriber_ = subscriber; executors_ = executors; @@ -148,7 +148,7 @@ ExecutorPool::Task UninstallerImpl::GetTask() ZLOGI("subscribe uninstall event success"); return; } - ZLOGE("subscribe uninstall event fail, try times:%d", retryTime_); + ZLOGE("subscribe uninstall event fail, try times:%{public}d", retryTime_); if (retryTime_++ >= RETRY_TIME) { return; } diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index a50f4d69d03d060d220235e63cfed5dd50bbc043..557127498f9cfdd31043f1c928f736a4a52f0258 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -98,6 +98,7 @@ bool CloudInfo::IsExist(const std::string &bundleName) const } } return false; +// return apps.find(bundleName) != apps.end(); } std::string CloudInfo::GetPrefix(const std::initializer_list &fields) diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index e6cff13aa5498253250c83019e292b1d6924296f..5e3056e40c56ff327e9e09b7a944fde6b2cc0fb5 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -25,14 +25,13 @@ public: enum : int32_t { FEATURE_INIT = EVT_CLOUD, GET_SCHEMA, - NEED_CREATE, CLOUD_BUTT }; struct StoreInfo { uint32_t tokenId = 0; - std::string bundleName; - std::string storeName; + std::string bundleName = ""; + std::string storeName = ""; int32_t instanceId = 0; }; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index dc73202775809e0f82e98ecacabfc0f8900f43f8..fab877050dc3a08f752ad2d4d359e5213bcc01c2 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -35,6 +35,7 @@ public: uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; +// std::map apps; std::vector apps; std::string GetKey() const; diff --git a/datamgr_service/services/distributeddataservice/service/BUILD.gn b/datamgr_service/services/distributeddataservice/service/BUILD.gn index 81b76328ee39be1bc223703998e15d94ba5591ba..35707d652c5354fbbb07d7be1c4ecb67d6a67c22 100644 --- a/datamgr_service/services/distributeddataservice/service/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/BUILD.gn @@ -118,10 +118,10 @@ ohos_shared_library("distributeddatasvc") { deps = [ "${kv_store_distributeddb_path}:distributeddb", + "${relational_store_path}/interfaces/inner_api/cloud_data:cloud_data_inner", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data_inner", ] external_deps = [ @@ -130,6 +130,7 @@ ohos_shared_library("distributeddatasvc") { "ability_runtime:ability_manager", "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 54ec42a9d45c04380fb16b9c2641300889330e22..4863cdba2639fb4d2b138568b4e9b536636f1d43 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -30,7 +30,6 @@ #include "metadata/store_meta_data.h" #include "utils/anonymous.h" #include "store/auto_cache.h" -#include "account_delegate.h" namespace OHOS::CloudData { using namespace DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; @@ -54,47 +53,12 @@ CloudServiceImpl::Factory::~Factory() {} CloudServiceImpl::CloudServiceImpl() { EventCenter::GetInstance().Subscribe(CloudEvent::FEATURE_INIT, [this](const Event &event) { - auto &rdbEvent = static_cast(event); - CloudInfo cloudInfo; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", cloudInfo.user); - return; - } - UpdateCloudInfo(cloudInfo); - AddSchema(cloudInfo); + FeatureInit(event); + return; }); EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { - auto &rdbEvent = static_cast(event); - auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); - auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); - auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, - rdbEvent.GetStoreInfo().instanceId); - - AutoCache::Watchers watchers; - auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); - if (store == nullptr) { - ZLOGE("store is nullptr"); - return; - } - store->SetSchema(schemaMeta); - auto instance = CloudServer::GetInstance(); - if (instance == nullptr) { - ZLOGE("instance is nullptr"); - return; - } - for (auto &database : schemaMeta.databases) { - if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { - continue; - } - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); - if (cloudDB != nullptr) { - store->Bind(cloudDB); - } - executor_->Execute(GetCloudTask(RETRY_TIMES)); - // do sync - } + GetSchema(event); return; }); } @@ -102,7 +66,7 @@ CloudServiceImpl::CloudServiceImpl() int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { CloudInfo cloudInfo; - if (GetCloudInfo(id, cloudInfo) != SUCCESS) { + if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { return INVALID_ARGUMENT; } cloudInfo.enableCloud = true; @@ -114,7 +78,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapcloudSwitch = item.second; } if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { return ERROR; @@ -125,7 +89,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &actions) { CloudInfo cloudInfo; - if (GetCloudInfo(id, cloudInfo) != SUCCESS) { + if (GetCloudInfo(IPCSkeleton::GetCallingTokenID(), id, cloudInfo) != SUCCESS) { return INVALID_ARGUMENT; } auto keys = cloudInfo.GetSchemaKey(); @@ -194,18 +158,17 @@ int32_t CloudServiceImpl::OnExecutor(std::shared_ptr executor) return E_OK; } -int32_t CloudServiceImpl::GetCloudInfo(const std::string &id, CloudInfo &cloudInfo) +int32_t CloudServiceImpl::GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo) { - auto tokenId = IPCSkeleton::GetCallingTokenID(); cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(tokenId); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true) && - GetServerInfo(cloudInfo ) != SUCCESS) { + GetServerInfo(cloudInfo) != SUCCESS) { ZLOGE("invalid args, user:%{public}d", cloudInfo.user); return INVALID_ARGUMENT; } if (cloudInfo.id != id) { - ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", - Anonymous::Change(id).c_str(), Anonymous::Change(cloudInfo.id).c_str()); + ZLOGE("invalid args, [input] id:%{public}s, [exist] id:%{public}s", Anonymous::Change(id).c_str(), + Anonymous::Change(cloudInfo.id).c_str()); return INVALID_ARGUMENT; } return SUCCESS; @@ -281,25 +244,25 @@ int32_t CloudServiceImpl::GetAppSchema(int32_t user, const std::string &bundleNa return SUCCESS; } -ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry) -{ - return [this, retry]() -> void { - std::vector users; - auto success = Account::GetInstance()->QueryUsers(users); - CloudServer *cloud = CloudServer::GetInstance(); - for (auto user : users) { - // cloud->GetServerInfo(); - } - - if (cloud == nullptr) { - - } - if (executor_ == nullptr) { - return ; - } - executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1)); - }; -} +//ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry) +//{ +// return [this, retry]() -> void { +// std::vector users; +// auto success = Account::GetInstance()->QueryUsers(users); +// CloudServer *cloud = CloudServer::GetInstance(); +// for (auto user : users) { +// cloud->GetServerInfo(user); +// } +// +// if (cloud == nullptr) { +// +// } +// if (executor_ == nullptr) { +// return ; +// } +// executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1)); +// }; +//} SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) { @@ -334,4 +297,59 @@ StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string & MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); return storeMetaData; } + +void CloudServiceImpl::FeatureInit(const Event &event) +{ + CloudInfo cloudInfo; + std::vector users; + if (!DistributedKv::AccountDelegate::GetInstance()->QueryUsers(users) || users.empty()) { + return; + } + cloudInfo.user = *users.begin(); + if (GetServerInfo(cloudInfo) != SUCCESS) { + ZLOGE("failed, user:%{public}d", cloudInfo.user); + return; + } + UpdateCloudInfo(cloudInfo); + AddSchema(cloudInfo); +} + +void CloudServiceImpl::GetSchema(const Event &event) +{ + auto &rdbEvent = static_cast(event); + ZLOGD("Start GetSchema, bundleName:%{public}s, storeName:%{public}s, instanceId:%{public}d", + rdbEvent.GetStoreInfo().bundleName.c_str(), rdbEvent.GetStoreInfo().storeName.c_str(), + rdbEvent.GetStoreInfo().instanceId); + auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); + auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + rdbEvent.GetStoreInfo().instanceId); + + AutoCache::Watchers watchers; + auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + if (store == nullptr) { + ZLOGE("store is nullptr"); + return; + } + store->SetSchema(schemaMeta); + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return; + } + for (auto &database : schemaMeta.databases) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { + continue; + } + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); + if (cloudDB != nullptr) { + store->Bind(cloudDB); + } + for (auto &table : database.tables) { + ZLOGI("table: %{public}s sync start", table.name.c_str()); + } + // do sync + } + return; +} } // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 607cb0960114fc95919de9d21473312012820206..276603e2c6de72212ebf5cfd46edb3a170164f5f 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -20,6 +20,7 @@ #include "cloud_service_stub.h" #include "cloud/cloud_info.h" #include "cloud/schema_meta.h" +#include "cloud/cloud_event.h" namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { @@ -47,6 +48,7 @@ private: using CloudInfo = DistributedData::CloudInfo; using SchemaMeta = DistributedData::SchemaMeta; + using Event = DistributedData::Event; static constexpr int32_t RETRY_TIMES = 10; static constexpr int32_t RETRY_INTERVAL = 30; @@ -56,11 +58,13 @@ private: SchemaMeta GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, int32_t instanceId); - int32_t GetCloudInfo(const std::string &id, CloudInfo &cloudInfo); + int32_t GetCloudInfo(uint32_t tokenId, const std::string &id, CloudInfo &cloudInfo); int32_t GetServerInfo(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); std::string GetAppId(const std::string &bundleName); - ExecutorPool::Task GetCloudTask(int32_t retry); + void FeatureInit(const Event &event); + void GetSchema(const Event &event); +// ExecutorPool::Task GetCloudTask(int32_t retry); bool CheckAccess(const std::string &bundleName); std::shared_ptr executor_; }; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp index 2ccf869e5aabae40423eec2672653278ebe36783..61e9dc63f66d40ad14b86ec79ddef95982497ce1 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_stub.cpp @@ -19,8 +19,10 @@ #include "itypes_util.h" #include "log_print.h" #include "utils/anonymous.h" +#include "tokenid_kit.h" namespace OHOS::CloudData { using namespace DistributedData; +using namespace OHOS::Security::AccessToken; const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = { &CloudServiceStub::OnEnableCloud, &CloudServiceStub::OnDisableCloud, @@ -43,6 +45,12 @@ int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); return -1; } + + if (!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) { + ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT); + return -1; + } + std::string id; if (!ITypesUtil::Unmarshal(data, id)) { ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str()); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 71a73d9702a4eb6131da8c28ab0e4402f8d98603..16fd16ba36e3449db4030d83519fe96e85e88981 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -50,11 +50,14 @@ namespace OHOS::DistributedRdb { __attribute__((used)) RdbServiceImpl::Factory RdbServiceImpl::factory_; RdbServiceImpl::Factory::Factory() { - FeatureSystem::GetInstance().RegisterCreator(RdbServiceImpl::SERVICE_NAME, []() { - return std::make_shared(); + FeatureSystem::GetInstance().RegisterCreator(RdbServiceImpl::SERVICE_NAME, [this]() { + if (product_ == nullptr) { + product_ = std::make_shared(); + } + return product_; }); AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore* { - return new RdbGeneralStore(metaData); + return new (std::nothrow) RdbGeneralStore(metaData); }); } @@ -463,8 +466,7 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - CloudEvent::StoreInfo storeInfo = { IPCSkeleton::GetCallingTokenID() }; - auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, storeInfo); + auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, CloudEvent::StoreInfo()); EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } @@ -475,24 +477,28 @@ int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) ZLOGE("permission error"); return RDB_ERROR; } - - auto storeMeta = GetStoreMetaData(param); - StoreMetaData oldMeta; - bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta); - if (isCreated && (oldMeta.storeType != storeMeta.storeType || - Constant::NotEqual(oldMeta.isEncrypt, storeMeta.isEncrypt) || - oldMeta.area != storeMeta.area)) { - ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " - "area:%{public}d->%{public}d", - storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), oldMeta.storeType, storeMeta.storeType, - oldMeta.isEncrypt, storeMeta.isEncrypt, oldMeta.area, storeMeta.area); - return RDB_ERROR; - } - if (!MetaDataManager::GetInstance().SaveMeta(storeMeta.GetKey(), storeMeta)) { + auto syncer = GetRdbSyncer(param); + if (syncer == nullptr) { return RDB_ERROR; } - CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, - storeMeta.instanceId }; +// auto storeMeta = GetStoreMetaData(param); +// StoreMetaData oldMeta; +// bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta); +// if (isCreated && (oldMeta.storeType != storeMeta.storeType || +// Constant::NotEqual(oldMeta.isEncrypt, storeMeta.isEncrypt) || +// oldMeta.area != storeMeta.area)) { +// ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " +// "area:%{public}d->%{public}d", +// storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), oldMeta.storeType, storeMeta.storeType, +// oldMeta.isEncrypt, storeMeta.isEncrypt, oldMeta.area, storeMeta.area); +// return RDB_ERROR; +// } +// if (!MetaDataManager::GetInstance().SaveMeta(storeMeta.GetKey(), storeMeta)) { +// return RDB_ERROR; +// } + CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, + RdbSyncer::RemoveSuffix(param.storeName_), + RdbSyncer::GetInstIndex(IPCSkeleton::GetCallingTokenID(), param.bundleName_) }; auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); EventCenter::GetInstance().PostEvent(move(event)); return RDB_OK; @@ -503,10 +509,10 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) StoreMetaData metaData; metaData.uid = IPCSkeleton::GetCallingUid(); metaData.tokenId = IPCSkeleton::GetCallingTokenID(); - metaData.instanceId = GetInstIndex(metaData.tokenId, param.bundleName_, param.storeName_); + metaData.instanceId = RdbSyncer::GetInstIndex(metaData.tokenId, param.bundleName_); metaData.bundleName = param.bundleName_; metaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; - metaData.storeId = param.storeName_; + metaData.storeId = RdbSyncer::RemoveSuffix(param.storeName_); metaData.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData.tokenId)); metaData.storeType = param.type_; metaData.securityLevel = param.level_; @@ -520,23 +526,6 @@ StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) return metaData; } -int32_t RdbServiceImpl::GetInstIndex(uint32_t tokenId, const std::string &bundleName, const std::string &storeName) -{ - if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { - return 0; - } - - HapTokenInfo tokenInfo; - tokenInfo.instIndex = -1; - int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); - if (errCode != RET_SUCCESS) { - ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x bundleName:%{public}s storeName:%{public}s", - errCode, tokenId, bundleName.c_str(), storeName.c_str()); - return -1; - } - return tokenInfo.instIndex; -} - int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) { executors_ = std::move(executors); diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h index 89fcac3c94a4e13a617e831cbf972385cdae0e23..ca13cfba1bb14776b3f43f5d48925e3682db0a6c 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -85,8 +85,6 @@ private: StoreMetaData GetStoreMetaData(const RdbSyncerParam& param); - int32_t GetInstIndex(uint32_t tokenId, const std::string &appId, const std::string &storeName); - class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; @@ -100,6 +98,8 @@ private: public: Factory(); ~Factory(); + private: + std::shared_ptr product_; }; using StoreSyncersType = std::map>; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h index 888919c067aa0b35399ae4904b48d61882245368..b103c7ad123c71147761dda0349bd2e8c749c898 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_syncer.h @@ -57,6 +57,7 @@ public: const std::vector& selectionArgs, sptr& resultSet); int32_t DestroyMetaData(StoreMetaData &meta); + static std::string RemoveSuffix(const std::string& name); static int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName); diff --git a/datamgr_service/services/distributeddataservice/service/test/BUILD.gn b/datamgr_service/services/distributeddataservice/service/test/BUILD.gn index cb5622157a8e6a7a02f22a059166669c151ae659..5a7ff143f256cf1a0563baa789cab1c99e9cbbca 100644 --- a/datamgr_service/services/distributeddataservice/service/test/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/test/BUILD.gn @@ -43,7 +43,9 @@ ohos_unittest("CloudDataTest") { "mock/db_change_data_mock.cpp", "mock/db_store_mock.cpp", ] - include_dirs = [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] + + include_dirs = + [ "../../../../../relational_store/interfaces/inner_api/rdb/include" ] configs = [ ":module_private_config" ] diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h index 383cd6c732aaadafccb36c7572fd295e7db95d04..a74d79a9b54d193bc384879aa4b44351879c085b 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/access_token.h @@ -21,6 +21,7 @@ namespace Security { namespace AccessToken { typedef unsigned int AccessTokenID; typedef unsigned int AccessTokenAttr; +typedef uint64_t FullTokenID; static const int DEFAULT_TOKEN_VERSION = 1; static const int FIRSTCALLER_TOKENID_DEFAULT = 0; static const int MAX_PERMISSION_SIZE = 1000; diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h index 677a26e24e8b66f171a7acbd2fb4fc2f2ad3db0f..9064c4272ed33517cd6ed1f7cdf68fcbcb78c233 100644 --- a/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/accesstoken_kit.h @@ -1,24 +1,47 @@ /* - * Copyright (c) 2021-2022 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. - */ +* Copyright (c) 2021-2023 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. +*/ + +/** +* @addtogroup AccessToken +* @{ +* +* @brief Provides permission management. +* +* Provides tokenID-based application permission verification mechanism. +* When an application accesses sensitive data or APIs, this module can check +* whether the application has the corresponding permission. Allows applications +* to query their access token information or APL levcels based on token IDs. +* +* @since 7.0 +* @version 7.0 +*/ + +/** +* @file accesstoken_kit.h +* +* @brief Declares access token interfaces. +* +* @since 7.0 +* @version 7.0 +*/ #ifndef INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H #define INTERFACES_INNER_KITS_ACCESSTOKEN_KIT_H #include #include -#include #include "access_token.h" #include "hap_token_info.h" @@ -32,49 +55,273 @@ namespace OHOS { namespace Security { namespace AccessToken { +/** +* @brief Declares AccessTokenKit class +*/ class AccessTokenKit { public: - static AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); - static AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); - static int UpdateHapToken(AccessTokenID tokenID, const std::string& appIDDesc, const HapPolicyParams& policy); - static int DeleteToken(AccessTokenID tokenID); - /* Get token type by ATM service */ - static ATokenTypeEnum GetTokenType(AccessTokenID tokenID); - /* Get token type from flag in tokenId, which doesn't depend on ATM service */ - static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID); - static int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); - static AccessTokenID GetHapTokenID(int userID, const std::string& bundleName, int instIndex); - static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); - static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); - static int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); - static int VerifyNativeToken(AccessTokenID tokenID, const std::string& permissionName); - static int VerifyAccessToken( - AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName); - static int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); - static int GetDefPermissions(AccessTokenID tokenID, std::vector& permList); - static int GetReqPermissions( - AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant); - static int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName); - static PermissionOper GetSelfPermissionsState(std::vector& permList); - static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); - static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); - static int ClearUserGrantedPermissionState(AccessTokenID tokenID); - static int32_t RegisterPermStateChangeCallback( - const std::shared_ptr& callback); - static int32_t UnRegisterPermStateChangeCallback(const std::shared_ptr& callback); - static int32_t GetHapDlpFlag(AccessTokenID tokenID); + /** + * @brief Create a unique hap token by input values. + * @param info struct HapInfoParams quote, see hap_token_info.h + * @param policy struct HapPolicyParams quote, see hap_token_info.h + * @return union AccessTokenIDEx, see access_token.h + */ + static AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy); + /** + * @brief Create a unique mapping token binding remote tokenID and DeviceID. + * @param remoteDeviceID remote device deviceID + * @param remoteTokenID remote device tokenID + * @return local tokenID which mapped by local token + */ + static AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID); + /** + * @brief Update hap token info. + * @param tokenIdEx union AccessTokenIDEx quote, see access_token.h + * @param isSystemApp is system app or not + * @param appIDDesc app id description quote + * @param apiVersion app api version + * @param policy struct HapPolicyParams quote, see hap_token_info.h + * @return error code, see access_token_error.h + */ + static int UpdateHapToken(AccessTokenIDEx& tokenIdEx, + bool isSystemApp, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy); + /** + * @brief Delete token info. + * @param tokenID token id + * @return error code, see access_token_error.h + */ + static int DeleteToken(AccessTokenID tokenID); + /** + * @brief Get token type by ATM service. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenType(AccessTokenID tokenID); + /** + * @brief Get token type from flag in tokenId, which doesn't depend on ATM service. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID); + /** + * @brief Get token type by ATM service with uint_64 parameters. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenType(FullTokenID tokenID); + /** + * @brief Get token type from flag in tokenId, which doesn't depend + * on ATM service, with uint_64 parameters. + * @param tokenID token id + * @return token type enum, see access_token.h + */ + static ATokenTypeEnum GetTokenTypeFlag(FullTokenID tokenID); + /** + * @brief Check native token dcap by token id. + * @param tokenID token id + * @param dcap dcap to be checked + * @return error code, see access_token_error.h + */ + static int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap); + /** + * @brief Query hap tokenID by input prarms. + * @param userID user id + * @param bundleName bundle name + * @param instIndex inst index + * @return token id if exsit or 0 if not exsit + */ + static AccessTokenID GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex); + /** + * @brief Query hap token attribute by input prarms. + * @param userID user id + * @param bundleName bundle name + * @param instIndex inst index + * @return union AccessTokenIDEx, see access_token.h + */ + static AccessTokenIDEx GetHapTokenIDEx(int32_t userID, const std::string& bundleName, int32_t instIndex); + /** + * @brief Get hap token info by token id. + * @param tokenID token id + * @param hapTokenInfoRes HapTokenInfo quote, as query result + * @return error code, see access_token_error.h + */ + static int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes); + /** + * @brief Get native token info by token id. + * @param tokenID token id + * @param nativeTokenInfoRes NativeTokenInfo quote, as query result + * @return error code, see access_token_error.h + */ + static int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes); + /** + * @brief Check if the input tokenID has been granted the input permission. + * @param tokenID token id + * @param permissionName permission to be checked + * @return enum PermissionState, see access_token.h + */ + static int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName); + /** + * @brief Check if the input tokenID has been granted the input permission. + * @param callerTokenID the tokenID of caller native process or hap process + * @param firstTokenID the tokenID of first caller native process or hap process + * @param permissionName permission to be checked + * @return enum PermissionState, see access_token.h + */ + static int VerifyAccessToken( + AccessTokenID callerTokenID, AccessTokenID firstTokenID, const std::string& permissionName); + /** + * @brief Get permission definition by permission name. + * @param permissionName permission name quote + * @param permissionDefResult PermissionDef quote, as query result + * @return error code, see access_token_error.h + */ + static int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult); + /** + * @brief Get all permission definitions by token id. + * @param tokenID token id + * @param permList PermissionDef list quote, as query result + * @return error code, see access_token_error.h + */ + static int GetDefPermissions(AccessTokenID tokenID, std::vector& permList); + /** + * @brief Get all requested permission full state by token id and grant mode. + * @param tokenID token id + * @param reqPermList PermissionStateFull list quote, as query result + * @param isSystemGrant grant mode + * @return error code, see access_token_error.h + */ + static int GetReqPermissions( + AccessTokenID tokenID, std::vector& reqPermList, bool isSystemGrant); + /** + * @brief Get permission grant flag + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag, as query result + * @return error code, see access_token_error.h + */ + static int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, int& flag); + /** + * @brief Get requsted permission grant result. + * @param permList PermissionListState list quote, as input and query result + * @return enum PermissionOper, see access_token.h + */ + static PermissionOper GetSelfPermissionsState(std::vector& permList); + /** + * @brief Grant input permission to input tokenID with input flag. + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag + * @return error code, see access_token_error.h + */ + static int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag); + /** + * @brief Revoke input permission to input tokenID with input flag. + * @param tokenID token id + * @param permissionName permission name quote + * @param flag the permission grant flag + * @return error code, see access_token_error.h + */ + static int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag); + /** + * @brief Clear all user granted permissions state in input tokenID. + * @param tokenID token id + * @return error code, see access_token_error.h + */ + static int ClearUserGrantedPermissionState(AccessTokenID tokenID); + /** + * @brief Register permission state change callback. + * @param callback smart point of class PermStateChangeCallbackCustomize quote + * @return error code, see access_token_error.h + */ + static int32_t RegisterPermStateChangeCallback( + const std::shared_ptr& callback); + /** + * @brief Unregister permission state change callback. + * @param callback smart point of class PermStateChangeCallbackCustomize quote + * @return error code, see access_token_error.h + */ + static int32_t UnRegisterPermStateChangeCallback(const std::shared_ptr& callback); + /** + * @brief Get current version. + * @return error code, see access_token_error.h + */ + static int32_t GetVersion(void); + /** + * @brief Get hap dlp flag by input tokenID. + * @param tokenID token id + * @return dlp flag in tokenID bitmap, or default -1 + */ + static int32_t GetHapDlpFlag(AccessTokenID tokenID); + /** + * @brief Reload native token info. + * @return error code, see access_token_error.h + */ + static int32_t ReloadNativeTokenInfo(); + /** + * @brief Get tokenID by native process name. + * @param processName native process name + * @return token id of native process + */ + static AccessTokenID GetNativeTokenId(const std::string& processName); #ifdef TOKEN_SYNC_ENABLE - static int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); - static int GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes); - static int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync); - static int SetRemoteNativeTokenInfo(const std::string& deviceID, - std::vector& nativeTokenInfoList); - static int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); - static AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID); - static int DeleteRemoteDeviceTokens(const std::string& deviceID); + /** + * @brief Get remote hap token info by remote token id. + * @param tokenID remote token id + * @param hapSync HapTokenInfoForSync quote, as query result + * @return error code, see access_token_error.h + */ + static int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync); + /** + * @brief Get all native token infos. + * @param nativeTokenInfosRes NativeTokenInfoForSync list quote + * as input and query result + * @return error code, see access_token_error.h + */ + static int GetAllNativeTokenInfo(std::vector& nativeTokenInfosRes); + /** + * @brief Set remote hap token info with remote deviceID. + * @param deviceID remote deviceID + * @param hapSync hap token info to set + * @return error code, see access_token_error.h + */ + static int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync); + /** + * @brief Set remote native token info list with remote deviceID. + * @param deviceID remote deviceID + * @param nativeTokenInfoList native token info list to set + * @return error code, see access_token_error.h + */ + static int SetRemoteNativeTokenInfo(const std::string& deviceID, + const std::vector& nativeTokenInfoList); + /** + * @brief Delete remote token by remote deviceID and remote tokenID. + * @param deviceID remote deviceID + * @param tokenID remote tokenID + * @return error code, see access_token_error.h + */ + static int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID); + /** + * @brief Get local mapping native tokenID by remote deviceID + * and remote tokenID. + * @param deviceID remote deviceID + * @param tokenID remote tokenID + * @return token id of mapping native tokenID + */ + static AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID); + /** + * @brief Delete remote tokens by remote deviceID. + * @param deviceID remote deviceID + * @return error code, see access_token_error.h + */ + static int DeleteRemoteDeviceTokens(const std::string& deviceID); #endif - static void DumpTokenInfo(std::string& dumpInfo); + /** + * @brief Dump all token infos in the cache. + * @param tokenID token id, if tokenID is valid, only dump this token info + * @param dumpInfo all token info + */ + static void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo); }; } // namespace AccessToken } // namespace Security diff --git a/mock/innerkits/access_token/libaccesstoken_sdk/include/tokenid_kit.h b/mock/innerkits/access_token/libaccesstoken_sdk/include/tokenid_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..1560692d16eae47916c79d80bc93d1e100057359 --- /dev/null +++ b/mock/innerkits/access_token/libaccesstoken_sdk/include/tokenid_kit.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022-2023 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. + */ + +#ifndef TOKENID_KIT_H +#define TOKENID_KIT_H + +#include + +namespace OHOS { +namespace Security { +namespace AccessToken { +class TokenIdKit { +public: + static bool IsSystemAppByFullTokenID(uint64_t tokenId) + { + return true; + } +}; +} // namespace AccessToken +} // namespace Security +} // namespace OHOS +#endif // TOKENID_KIT_H diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp index 7b8e2f828b993be89de3f4647e8a5bde2200ef55..814226b86b320f3a582cb569d558289ff2c80ddd 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp @@ -250,6 +250,7 @@ struct HelperRdbContext : public BaseContext { HelperRdbContext() : config(""), version(0), iscontext(false), openCallback(), proxy(nullptr) { + config.SetTokenType(0); } virtual ~HelperRdbContext(){}; }; diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_helper.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_helper.cpp index 19d831759e0f66491a2d2e1ad6cadbbcd8c58b71..4800ddcb6052814b334e983fe840d7e8db4f683a 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_helper.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_store_helper.cpp @@ -43,6 +43,7 @@ struct HelperRdbContext : public Context { HelperRdbContext() : config(""), proxy(nullptr), isSystemAppCalled(false) { + config.SetTokenType(0); } virtual ~HelperRdbContext(){}; }; diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp index 5238b304b91b965976176ad8ec0067a4218bb69f..4dcd25ba6d8412241f8db813e29dd89d0da02150 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_config.cpp @@ -388,4 +388,14 @@ std::map RdbStoreConfig::GetScalarFunctions() c { return customScalarFunctions; } + +void RdbStoreConfig::SetTokenType(uint32_t tokenType) +{ + tokenType_ = tokenType; +} + +uint32_t RdbStoreConfig::GetTokenType() const +{ + return tokenType_; +} } // namespace OHOS::NativeRdb diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 01273a655b379e3cac5c1d8c9d4c10dcf32158a2..ec086c35f022f1f31a8c3a1c4dcd5b1e3ec732e0 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -21,6 +21,8 @@ #include +#include "accesstoken_kit.h" +#include "ipc_skeleton.h" #include "logger.h" #include "rdb_errno.h" #include "rdb_trace.h" @@ -52,6 +54,7 @@ #endif namespace OHOS::NativeRdb { +using namespace Security::AccessToken; std::shared_ptr RdbStoreImpl::Open(const RdbStoreConfig &config, int &errCode) { std::shared_ptr rdbStore = std::make_shared(config); @@ -89,25 +92,30 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) syncerParam_.isEncrypt_ = config.IsEncrypt(); syncerParam_.password_ = {}; + //config.GetTokenType() == 1 + uint32_t id = IPCSkeleton::GetSelfTokenID() & 0xffffffff; + if (AccessTokenKit::GetTokenTypeFlag(id) != TOKEN_HAP) { + LOG_DEBUG("%{public}s is not a hap.", config.GetBundleName().c_str()); + return E_OK; + } std::shared_ptr service = nullptr; errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { - LOG_ERROR("RdbStoreImpl::InnerOpen get service failed, err is %{public}d.", errCode); + LOG_ERROR("GetRdbService failed, err is %{public}d.", errCode); return E_OK; } + errCode = service->GetSchema(syncerParam_); + if (errCode != E_OK) { + LOG_ERROR("GetSchema failed, err is %{public}d.", errCode); + } if (!config.GetUri().empty()) { errCode = service->CreateRDBTable(syncerParam_, config.GetWritePermission(), config.GetReadPermission()); if (errCode != E_OK) { - LOG_ERROR("RdbStoreImpl::InnerOpen service CreateRDBTable failed"); + LOG_ERROR("CreateRDBTable failed"); } else { isShared_ = true; } } - - errCode = service->GetSchema(syncerParam_); - if (errCode != E_OK) { - LOG_ERROR("RdbStoreImpl::InnerOpen GetSchema failed, err is %{public}d.", errCode); - } #endif return E_OK; } diff --git a/relational_store/interfaces/inner_api/rdb/BUILD.gn b/relational_store/interfaces/inner_api/rdb/BUILD.gn index 5c0dfca83ebb18a1bfe11c13e6d4a4b5dbbc0074..cbd3f6fee6eb062bc333ed184b535872d5c55f6b 100644 --- a/relational_store/interfaces/inner_api/rdb/BUILD.gn +++ b/relational_store/interfaces/inner_api/rdb/BUILD.gn @@ -45,14 +45,8 @@ if (is_ohos) { include_dirs = [ "include", - "//base/security/huks/frameworks/huks_standard/main/common/include", "${relational_store_native_path}/rdb/include", "${relational_store_native_path}/rdb_device_manager_adapter/include", - "//utils/system/safwk/native/include", - "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb/interfaces/include/relational", ] defines = [ @@ -71,8 +65,8 @@ if (is_ohos) { defines += [ "SQLITE_DISTRIBUTE_RELATIONAL" ] include_dirs += [ - "//foundation/distributeddatamgr/kv_store/frameworks/common", - "//foundation/distributeddatamgr/distributedfile/interfaces/kits/js/src/mod_securitylabel", + "${kvstore_path}/common", + "${distributedfile_path}/mod_securitylabel", ] } @@ -99,8 +93,7 @@ if (is_ohos) { deps = base_deps deps += [ - "//base/security/huks/frameworks/huks_standard/main/os_dependency:libhuks_os_dependency_standard_static", - "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", + "${kvstore_path}/libs/distributeddb:distributeddb", "//third_party/sqlite:sqlite", ] ldflags = [ "-Wl,--exclude-libs,ALL" ] @@ -125,6 +118,7 @@ if (is_ohos) { [ "${relational_store_innerapi_path}/appdatafwk:native_appdatafwk" ] external_deps = [ + "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", "hilog_native:libhilog", @@ -172,6 +166,7 @@ if (is_ohos) { [ "${relational_store_innerapi_path}/appdatafwk:native_appdatafwk" ] external_deps = [ + "access_token:libaccesstoken_sdk", "c_utils:utils", "device_manager:devicemanagersdk", "hilog_native:libhilog", diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h index 0e9294e4d7de9b9c2907ab64398605b7867010c1..0a5e036a19069ba8861bf710d76cc15e943c4ad3 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h @@ -432,6 +432,16 @@ public: */ std::vector GetEncryptKey() const; + /** + * @brief Sets the tokenType for the object. + */ + void SetTokenType(uint32_t tokenType); + + /** + * @brief Obtains the tokenType in this {@code StoreConfig} object. + */ + uint32_t GetTokenType() const; + /** * @brief Sets the scalar function for the object. */ @@ -511,8 +521,7 @@ private: std::map customScalarFunctions; //cloud rdb - std::string cloudId_; -// int32_t schemaVerion_ = -1; + int32_t tokenType_ = 1; }; } // namespace OHOS::NativeRdb