1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "adapter/dnetwork_adapter.h"
17 #include "bundle/bundle_manager_internal.h"
18 #include "bundle/bundle_manager_callback_stub.h"
19 #include "distributed_sched_adapter.h"
20 #include "distributed_sched_utils.h"
21 #include "dtbschedmgr_log.h"
22 #include "mission/dsched_sync_e2e.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "os_account_manager.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 using namespace AccountSA;
31 namespace {
32 const std::string TAG = "BundleManagerInternal";
33 }
34 IMPLEMENT_SINGLE_INSTANCE(BundleManagerInternal);
GetCallerAppIdFromBms(int32_t callingUid,std::string & appId)35 bool BundleManagerInternal::GetCallerAppIdFromBms(int32_t callingUid, std::string& appId)
36 {
37     std::vector<std::string> bundleNameList;
38     if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
39         HILOGE("GetBundleNameListFromBms failed");
40         return false;
41     }
42     if (bundleNameList.empty()) {
43         HILOGE("bundleNameList empty");
44         return false;
45     }
46     // getting an arbitrary bundlename for they sharing a same appId, here we get the first one
47     return GetCallerAppIdFromBms(bundleNameList.front(), appId);
48 }
49 
GetCallerAppIdFromBms(const std::string & bundleName,std::string & appId)50 bool BundleManagerInternal::GetCallerAppIdFromBms(const std::string& bundleName, std::string& appId)
51 {
52     auto bundleMgr = GetBundleManager();
53     if (bundleMgr == nullptr) {
54         HILOGE("failed to get bms");
55         return false;
56     }
57     int32_t activeAccountId = 0;
58     ErrCode err = QueryOsAccount(activeAccountId);
59     if (err != ERR_OK) {
60         return false;
61     }
62     appId = bundleMgr->GetAppIdByBundleName(bundleName, activeAccountId);
63     HILOGD("appId:%s", GetAnonymStr(appId).c_str());
64     return true;
65 }
66 
GetSpecifyBundleNameFromBms(int32_t callingUid,std::string & bundleName)67 bool BundleManagerInternal::GetSpecifyBundleNameFromBms(int32_t callingUid, std::string& bundleName)
68 {
69     auto bundleMgr = GetBundleManager();
70     if (bundleMgr == nullptr) {
71         HILOGE("failed to get bms");
72         return false;
73     }
74     bool result = bundleMgr->GetBundleNameForUid(callingUid, bundleName);
75     if (!result) {
76         HILOGE("Get specify bundle name for uid failed, result: %{public}d", result);
77         return false;
78     }
79     return result;
80 }
81 
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::string> & bundleNameList)82 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid, std::vector<std::string>& bundleNameList)
83 {
84     auto bundleMgr = GetBundleManager();
85     if (bundleMgr == nullptr) {
86         HILOGE("failed to get bms");
87         return false;
88     }
89     bool result = bundleMgr->GetBundlesForUid(callingUid, bundleNameList);
90     if (!result) {
91         HILOGE("Get bundle name list for userId which the uid belongs failed, result: %{public}d", result);
92         return false;
93     }
94     return result;
95 }
96 
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::u16string> & u16BundleNameList)97 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid,
98     std::vector<std::u16string>& u16BundleNameList)
99 {
100     std::vector<std::string> bundleNameList;
101     if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
102         HILOGE("Get bundle name list for userId which the uid belongs failed.");
103         return false;
104     }
105     for (const std::string& bundleName : bundleNameList) {
106         u16BundleNameList.emplace_back(Str8ToStr16(bundleName));
107     }
108     return true;
109 }
110 
QueryAbilityInfo(const AAFwk::Want & want,AppExecFwk::AbilityInfo & abilityInfo)111 bool BundleManagerInternal::QueryAbilityInfo(const AAFwk::Want& want, AppExecFwk::AbilityInfo& abilityInfo)
112 {
113     int32_t activeAccountId = 0;
114     ErrCode err = QueryOsAccount(activeAccountId);
115     if (err != ERR_OK) {
116         return false;
117     }
118     auto bundleMgr = GetBundleManager();
119     if (bundleMgr == nullptr) {
120         HILOGE("failed to get bms");
121         return false;
122     }
123     bool result = bundleMgr->QueryAbilityInfo(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
124         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, abilityInfo);
125     if (!result) {
126         HILOGW("QueryAbilityInfo failed");
127         return false;
128     }
129     return true;
130 }
131 
QueryExtensionAbilityInfo(const AAFwk::Want & want,AppExecFwk::ExtensionAbilityInfo & extensionInfo)132 bool BundleManagerInternal::QueryExtensionAbilityInfo(const AAFwk::Want& want,
133     AppExecFwk::ExtensionAbilityInfo& extensionInfo)
134 {
135     int32_t activeAccountId = 0;
136     ErrCode err = QueryOsAccount(activeAccountId);
137     if (err != ERR_OK) {
138         return false;
139     }
140     auto bundleMgr = GetBundleManager();
141     if (bundleMgr == nullptr) {
142         HILOGE("failed to get bms");
143         return false;
144     }
145     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
146     bundleMgr->QueryExtensionAbilityInfos(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
147         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, extensionInfos);
148     if (extensionInfos.empty()) {
149         HILOGE("QueryExtensionAbilityInfo failed.");
150         return false;
151     }
152     extensionInfo = extensionInfos.front();
153     if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
154         HILOGE("ExtensionAbilityInfo is empty.");
155         return false;
156     }
157     HILOGD("ExtensionAbilityInfo found, name=%{public}s.", extensionInfo.name.c_str());
158     return true;
159 }
160 
InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo & extensionAbilityInfo,AppExecFwk::AbilityInfo & abilityInfo)161 void BundleManagerInternal::InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo &extensionAbilityInfo,
162     AppExecFwk::AbilityInfo &abilityInfo)
163 {
164     abilityInfo.bundleName = extensionAbilityInfo.bundleName;
165     abilityInfo.name = extensionAbilityInfo.name;
166     abilityInfo.permissions = extensionAbilityInfo.permissions;
167     abilityInfo.visible = extensionAbilityInfo.visible;
168 }
169 
IsSameAppId(const std::string & callerAppId,const std::string & targetBundleName)170 bool BundleManagerInternal::IsSameAppId(const std::string& callerAppId, const std::string& targetBundleName)
171 {
172     if (targetBundleName.empty() || callerAppId.empty()) {
173         HILOGE("targetBundleName:%{public}s or callerAppId:%s is empty",
174             targetBundleName.c_str(), GetAnonymStr(callerAppId).c_str());
175         return false;
176     }
177     HILOGD("callerAppId:%s", GetAnonymStr(callerAppId).c_str());
178     std::string calleeAppId;
179     if (!GetCallerAppIdFromBms(targetBundleName, calleeAppId)) {
180         HILOGE("GetCallerAppIdFromBms failed");
181         return false;
182     }
183     HILOGD("calleeAppId:%s", GetAnonymStr(calleeAppId).c_str());
184     return callerAppId == calleeAppId;
185 }
186 
IsSameDeveloperId(const std::string & bundleNameInCurrentSide,const std::string & developerId4OtherSide)187 bool BundleManagerInternal::IsSameDeveloperId(const std::string &bundleNameInCurrentSide,
188     const std::string &developerId4OtherSide)
189 {
190     if (bundleNameInCurrentSide.empty() || developerId4OtherSide.empty()) {
191         HILOGE("bundleNameInCurrentSide: %{public}s or developerId4OtherSide: %{public}s is empty",
192             bundleNameInCurrentSide.c_str(), developerId4OtherSide.c_str());
193         return false;
194     }
195 
196     auto bundleMgr = GetBundleManager();
197     if (bundleMgr == nullptr) {
198         HILOGE("get bundle manager failed");
199         return false;
200     }
201     AppExecFwk::AppProvisionInfo targetAppProvisionInfo;
202     std::vector<int32_t> ids;
203     ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
204     if (ret != ERR_OK || ids.empty()) {
205         HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", ret);
206         return false;
207     }
208     bundleMgr->GetAppProvisionInfo(bundleNameInCurrentSide, ids[0], targetAppProvisionInfo);
209     return developerId4OtherSide == targetAppProvisionInfo.developerId;
210 }
211 
GetLocalBundleInfo(const std::string & bundleName,AppExecFwk::BundleInfo & localBundleInfo)212 int32_t BundleManagerInternal::GetLocalBundleInfo(const std::string& bundleName,
213     AppExecFwk::BundleInfo &localBundleInfo)
214 {
215     auto bms = GetBundleManager();
216     if (bms == nullptr) {
217         HILOGE("get bundle manager failed");
218         return INVALID_PARAMETERS_ERR;
219     }
220 
221     int32_t activeAccountId = 0;
222     ErrCode err = QueryOsAccount(activeAccountId);
223     if (err != ERR_OK) {
224         return err;
225     }
226     if (!bms->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES,
227         localBundleInfo, activeAccountId)) {
228         HILOGE("get local bundle info failed");
229         return INVALID_PARAMETERS_ERR;
230     }
231     return ERR_OK;
232 }
233 
GetLocalBundleInfoV9(const std::string & bundleName,AppExecFwk::BundleInfo & bundleInfo)234 int32_t BundleManagerInternal::GetLocalBundleInfoV9(const std::string& bundleName,
235     AppExecFwk::BundleInfo &bundleInfo)
236 {
237     auto bms = GetBundleManager();
238     if (bms == nullptr) {
239         HILOGE("get bundle manager failed");
240         return INVALID_PARAMETERS_ERR;
241     }
242 
243     int32_t activeAccountId = 0;
244     ErrCode ret = QueryOsAccount(activeAccountId);
245     if (ret != ERR_OK) {
246         return ret;
247     }
248     ret = bms->GetBundleInfoV9(bundleName,
249         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
250         bundleInfo, activeAccountId);
251     if (ret != ERR_OK) {
252         HILOGE("get local bundle info failed, ret: %{public}d", ret);
253     }
254     return ret;
255 }
256 
GetContinueBundle4Src(const std::string & srcBundleName,std::vector<std::string> & bundleNameList)257 bool BundleManagerInternal::GetContinueBundle4Src(const std::string &srcBundleName,
258     std::vector<std::string> &bundleNameList)
259 {
260     auto bundleMgr = GetBundleManager();
261     if (bundleMgr == nullptr) {
262         HILOGE("get bundle manager failed");
263         return false;
264     }
265 
266     int32_t activeAccountId = 0;
267     ErrCode ret = QueryOsAccount(activeAccountId);
268     if (ret != ERR_OK) {
269         HILOGE("get os account id failed");
270         return false;
271     }
272     ret = bundleMgr->GetContinueBundleNames(srcBundleName, bundleNameList, activeAccountId);
273     if (ret != ERR_OK || bundleNameList.empty()) {
274         HILOGW("No APP with specified bundle name(%{public}s) configured in continue Bundle; ret: %{public}d",
275             srcBundleName.c_str(), ret);
276         return false;
277     }
278     return true;
279 }
280 
GetAppProvisionInfo4CurrentUser(const std::string & bundleName,AppExecFwk::AppProvisionInfo & appProvisionInfo)281 bool BundleManagerInternal::GetAppProvisionInfo4CurrentUser(const std::string &bundleName,
282     AppExecFwk::AppProvisionInfo &appProvisionInfo)
283 {
284     sptr<AppExecFwk::IBundleMgr> bundleMgr = GetBundleManager();
285     if (bundleMgr == nullptr) {
286         HILOGE("get bundle manager failed");
287         return false;
288     }
289     std::vector<int32_t> ids;
290     ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
291     if (ret != ERR_OK || ids.empty()) {
292         HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", ret);
293         return false;
294     }
295     ErrCode result = bundleMgr->GetAppProvisionInfo(bundleName, ids[0], appProvisionInfo);
296     HILOGI("find dst bundle name for diff bundle continue. dst developer id: %{public}s; ",
297         GetAnonymStr(appProvisionInfo.developerId).c_str());
298     return result == ERR_OK;
299 }
300 
CheckRemoteBundleInfoForContinuation(const std::string & dstDeviceId,const std::string & bundleName,AppExecFwk::DistributedBundleInfo & remoteBundleInfo)301 int32_t BundleManagerInternal::CheckRemoteBundleInfoForContinuation(const std::string& dstDeviceId,
302     const std::string& bundleName, AppExecFwk::DistributedBundleInfo& remoteBundleInfo)
303 {
304     if (bundleName.empty()) {
305         HILOGE("bundle name empty");
306         return INVALID_PARAMETERS_ERR;
307     }
308     HILOGI("bundleName: %{public}s", bundleName.c_str());
309 
310     auto bms = GetBundleManager();
311     if (bms == nullptr) {
312         HILOGE("get bundle manager failed");
313         return INVALID_PARAMETERS_ERR;
314     }
315 
316     bool isInstalled = bms->GetDistributedBundleInfo(dstDeviceId, bundleName, remoteBundleInfo);
317     if (isInstalled) {
318         return ERR_OK;
319     }
320 
321     AppExecFwk::BundleInfo localBundleInfo;
322     if (GetLocalBundleInfo(bundleName, localBundleInfo) != ERR_OK) {
323         HILOGE("get local bundle info failed");
324         return INVALID_PARAMETERS_ERR;
325     }
326     if (localBundleInfo.entryInstallationFree) {
327         return CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL;
328     }
329     return CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL;
330 }
331 
CheckIfRemoteCanInstall(const AAFwk::Want & want,int32_t missionId)332 bool BundleManagerInternal::CheckIfRemoteCanInstall(const AAFwk::Want& want, int32_t missionId)
333 {
334     std::string bundleName = want.GetElement().GetBundleName();
335     std::string moduleName = want.GetElement().GetModuleName();
336     std::string abilityName = want.GetElement().GetAbilityName();
337     std::string deviceId = want.GetElement().GetDeviceID();
338     std::string udid = DnetworkAdapter::GetInstance()->GetUdidByNetworkId(deviceId);
339     HILOGD("bundleName = %{public}s, moduleName = %{public}s, abilityName = %{public}s, udid = %{public}s",
340         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), GetAnonymStr(udid).c_str());
341 
342     if (bundleName.empty() || moduleName.empty() || abilityName.empty() || udid.empty()) {
343         HILOGE("udid or bundle or module or ability name is empty");
344         return false;
345     }
346     auto bms = GetBundleManager();
347     if (bms == nullptr) {
348         HILOGE("get bundle manager failed");
349         return false;
350     }
351 
352     AAFwk::Want newWant;
353     newWant.SetElementName(udid, bundleName, abilityName, moduleName);
354     int32_t activeAccountId = 0;
355     ErrCode err = QueryOsAccount(activeAccountId);
356     if (err != ERR_OK) {
357         return false;
358     }
359     bool ret = bms->CheckAbilityEnableInstall(newWant, missionId, activeAccountId, new DmsBundleManagerCallbackStub());
360     if (ret != true) {
361         HILOGE("CheckAbilityEnableInstall from bms failed");
362     }
363     return ret;
364 }
365 
GetBundleManager()366 sptr<AppExecFwk::IBundleMgr> BundleManagerInternal::GetBundleManager()
367 {
368     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
369     if (samgrProxy == nullptr) {
370         return nullptr;
371     }
372     sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
373     if (bmsProxy == nullptr) {
374         HILOGE("failed to get bms from samgr");
375         return nullptr;
376     }
377     return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
378 }
379 
GetDistributedBundleManager()380 sptr<AppExecFwk::IDistributedBms> BundleManagerInternal::GetDistributedBundleManager()
381 {
382     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
383     if (samgrProxy == nullptr) {
384         HILOGE("failed to get samgrProxy from dbms");
385         return nullptr;
386     }
387     sptr<IRemoteObject> dbmsProxy = samgrProxy->GetSystemAbility(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
388     if (dbmsProxy == nullptr) {
389         HILOGE("failed to get dbmsProxy from dbms");
390         return nullptr;
391     }
392     return iface_cast<AppExecFwk::IDistributedBms>(dbmsProxy);
393 }
394 
GetUidFromBms(const std::string & bundleName)395 int32_t BundleManagerInternal::GetUidFromBms(const std::string& bundleName)
396 {
397     auto bundleMgr = GetBundleManager();
398     if (bundleMgr == nullptr) {
399         HILOGE("failed to get bms");
400         return -1;
401     }
402     int32_t activeAccountId = 0;
403     ErrCode err = QueryOsAccount(activeAccountId);
404     if (err != ERR_OK) {
405         return err;
406     }
407     return bundleMgr->GetUidByBundleName(bundleName, activeAccountId);
408 }
409 
GetBundleNameId(const std::string & bundleName,uint16_t & bundleNameId)410 int32_t BundleManagerInternal::GetBundleNameId(const std::string& bundleName, uint16_t& bundleNameId)
411 {
412     HILOGD("called.");
413     bool ret = DmsBmStorage::GetInstance()->GetBundleNameId(bundleName, bundleNameId);
414     HILOGI("bundleNameId: %{public}d end.", bundleNameId);
415     if (!ret) {
416         HILOGE("can not get bundleNameId by bundleName");
417         return CAN_NOT_FOUND_ABILITY_ERR;
418     }
419     HILOGD("end.");
420     return ERR_OK;
421 }
422 
GetContinueType(const std::string & networkId,std::string & bundleName,uint8_t continueTypeId)423 std::string BundleManagerInternal::GetContinueType(const std::string &networkId,
424     std::string &bundleName, uint8_t continueTypeId)
425 {
426     HILOGD("called.");
427     std::string continueType = DmsBmStorage::GetInstance()->GetContinueType(networkId, bundleName, continueTypeId);
428     HILOGI("continueType: %{public}s end.", continueType.c_str());
429     if (continueType == "") {
430         HILOGE("can not get continueType!");
431     }
432     HILOGD("end.");
433     return continueType;
434 }
435 
GetContinueTypeId(const std::string & bundleName,const std::string & abilityName,uint8_t & continueTypeId)436 int32_t BundleManagerInternal::GetContinueTypeId(const std::string &bundleName,
437     const std::string &abilityName, uint8_t &continueTypeId)
438 {
439     HILOGD("called.");
440     bool ret = DmsBmStorage::GetInstance()->GetContinueTypeId(bundleName, abilityName, continueTypeId);
441     HILOGI("ContinueTypeId: %{public}d ", continueTypeId);
442     if (!ret) {
443         HILOGE("can not get ContinueTypeId!");
444         return CAN_NOT_FOUND_ABILITY_ERR;
445     }
446     HILOGD("end.");
447     return ERR_OK;
448 }
449 
GetAbilityName(const std::string & networkId,std::string & bundleName,std::string & continueType)450 std::string BundleManagerInternal::GetAbilityName(const std::string &networkId,
451     std::string &bundleName, std::string &continueType)
452 {
453     HILOGI("called. continueType: %{public}s ", continueType.c_str());
454     std::string abilityName = DmsBmStorage::GetInstance()->GetAbilityName(networkId, bundleName, continueType);
455     if (abilityName == "") {
456         HILOGE("can not get abilityName!");
457     }
458     HILOGD("end.");
459     return abilityName;
460 }
461 
GetBundleNameById(const std::string & networkId,const uint16_t & bundleNameId,std::string & bundleName)462 int32_t BundleManagerInternal::GetBundleNameById(const std::string& networkId,
463     const uint16_t& bundleNameId, std::string& bundleName)
464 {
465     HILOGD("called.");
466     bool result = DmsBmStorage::GetInstance()->GetDistributedBundleName(networkId, bundleNameId, bundleName);
467     if (!result) {
468         HILOGE("failed to get bundleName by bundleNameId, try syncing");
469         DmsKvSyncE2E::GetInstance()->PushAndPullData(networkId);
470         return CAN_NOT_FOUND_ABILITY_ERR;
471     }
472     HILOGD("end.");
473     return ERR_OK;
474 }
475 
GetApplicationInfoFromBms(const std::string & bundleName,const AppExecFwk::BundleFlag flag,const int32_t userId,AppExecFwk::ApplicationInfo & appInfo)476 int32_t BundleManagerInternal::GetApplicationInfoFromBms(const std::string& bundleName,
477     const AppExecFwk::BundleFlag flag, const int32_t userId, AppExecFwk::ApplicationInfo &appInfo)
478 {
479     auto bundleMgr = GetBundleManager();
480     if (bundleMgr == nullptr) {
481         HILOGE("failed to get bms");
482         return INVALID_PARAMETERS_ERR;
483     }
484     std::string identity = IPCSkeleton::ResetCallingIdentity();
485     bundleMgr->GetApplicationInfo(bundleName, flag, userId, appInfo);
486     IPCSkeleton::SetCallingIdentity(identity);
487     return ERR_OK;
488 }
489 
QueryOsAccount(int32_t & activeAccountId)490 ErrCode BundleManagerInternal::QueryOsAccount(int32_t& activeAccountId)
491 {
492 #ifdef OS_ACCOUNT_PART
493     std::vector<int32_t> ids;
494     ErrCode err = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
495     if (err != ERR_OK || ids.empty()) {
496         HILOGE("QueryActiveOsAccountIds passing param invalid or return error!, err : %{public}d", err);
497         return INVALID_PARAMETERS_ERR;
498     }
499     activeAccountId = ids[0];
500 #endif
501     return ERR_OK;
502 }
503 } // namespace DistributedSchedule
504 } // namespace OHOS
505