• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "bundle/bundle_manager_internal.h"
17 #include "bundle/bundle_manager_callback_stub.h"
18 #include "distributed_sched_adapter.h"
19 #include "dtbschedmgr_log.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "os_account_manager.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
26 namespace DistributedSchedule {
27 using namespace AccountSA;
28 namespace {
29 const std::string TAG = "BundleManagerInternal";
30 }
31 IMPLEMENT_SINGLE_INSTANCE(BundleManagerInternal);
GetCallerAppIdFromBms(int32_t callingUid,std::string & appId)32 bool BundleManagerInternal::GetCallerAppIdFromBms(int32_t callingUid, std::string& appId)
33 {
34     std::vector<std::string> bundleNameList;
35     if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
36         HILOGE("GetBundleNameListFromBms failed");
37         return false;
38     }
39     if (bundleNameList.empty()) {
40         HILOGE("bundleNameList empty");
41         return false;
42     }
43     // getting an arbitrary bundlename for they sharing a same appId, here we get the first one
44     return GetCallerAppIdFromBms(bundleNameList.front(), appId);
45 }
46 
GetCallerAppIdFromBms(const std::string & bundleName,std::string & appId)47 bool BundleManagerInternal::GetCallerAppIdFromBms(const std::string& bundleName, std::string& appId)
48 {
49     auto bundleMgr = GetBundleManager();
50     if (bundleMgr == nullptr) {
51         HILOGE("failed to get bms");
52         return false;
53     }
54     std::vector<int32_t> ids;
55     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
56     if (result != ERR_OK || ids.empty()) {
57         return false;
58     }
59     int32_t activeAccountId = ids[0];
60     appId = bundleMgr->GetAppIdByBundleName(bundleName, activeAccountId);
61     HILOGD("appId:%s", appId.c_str());
62     return true;
63 }
64 
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::string> & bundleNameList)65 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid, std::vector<std::string>& bundleNameList)
66 {
67     auto bundleMgr = GetBundleManager();
68     if (bundleMgr == nullptr) {
69         HILOGE("failed to get bms");
70         return false;
71     }
72     bool result = bundleMgr->GetBundlesForUid(callingUid, bundleNameList);
73     if (!result) {
74         HILOGE("GetBundlesForUid failed, result: %{public}d", result);
75         return false;
76     }
77     return result;
78 }
79 
GetBundleNameListFromBms(int32_t callingUid,std::vector<std::u16string> & u16BundleNameList)80 bool BundleManagerInternal::GetBundleNameListFromBms(int32_t callingUid,
81     std::vector<std::u16string>& u16BundleNameList)
82 {
83     std::vector<std::string> bundleNameList;
84     if (!GetBundleNameListFromBms(callingUid, bundleNameList)) {
85         HILOGE("GetBundleNameListFromBms failed");
86         return false;
87     }
88     for (const std::string& bundleName : bundleNameList) {
89         u16BundleNameList.emplace_back(Str8ToStr16(bundleName));
90     }
91     return true;
92 }
93 
QueryAbilityInfo(const AAFwk::Want & want,AppExecFwk::AbilityInfo & abilityInfo)94 bool BundleManagerInternal::QueryAbilityInfo(const AAFwk::Want& want, AppExecFwk::AbilityInfo& abilityInfo)
95 {
96     std::vector<int32_t> ids;
97     int32_t ret = OsAccountManager::QueryActiveOsAccountIds(ids);
98     if (ret != ERR_OK || ids.empty()) {
99         return false;
100     }
101     int32_t activeAccountId = ids[0];
102     auto bundleMgr = GetBundleManager();
103     if (bundleMgr == nullptr) {
104         HILOGE("failed to get bms");
105         return false;
106     }
107     bool result = bundleMgr->QueryAbilityInfo(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
108         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, abilityInfo);
109     if (!result) {
110         HILOGE("QueryAbilityInfo failed");
111         return false;
112     }
113     return true;
114 }
115 
QueryExtensionAbilityInfo(const AAFwk::Want & want,AppExecFwk::ExtensionAbilityInfo & extensionInfo)116 bool BundleManagerInternal::QueryExtensionAbilityInfo(const AAFwk::Want& want,
117     AppExecFwk::ExtensionAbilityInfo& extensionInfo)
118 {
119     std::vector<int32_t> ids;
120     int32_t ret = OsAccountManager::QueryActiveOsAccountIds(ids);
121     if (ret != ERR_OK || ids.empty()) {
122         return false;
123     }
124     int32_t activeAccountId = ids[0];
125     auto bundleMgr = GetBundleManager();
126     if (bundleMgr == nullptr) {
127         HILOGE("failed to get bms");
128         return false;
129     }
130     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
131     bundleMgr->QueryExtensionAbilityInfos(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
132         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION, activeAccountId, extensionInfos);
133     if (extensionInfos.empty()) {
134         HILOGE("QueryExtensionAbilityInfo failed.");
135         return false;
136     }
137     extensionInfo = extensionInfos.front();
138     if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
139         HILOGE("ExtensionAbilityInfo is empty.");
140         return false;
141     }
142     HILOGD("ExtensionAbilityInfo found, name=%{public}s.", extensionInfo.name.c_str());
143     return true;
144 }
145 
InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo & extensionAbilityInfo,AppExecFwk::AbilityInfo & abilityInfo)146 void BundleManagerInternal::InitAbilityInfoFromExtension(const AppExecFwk::ExtensionAbilityInfo &extensionAbilityInfo,
147     AppExecFwk::AbilityInfo &abilityInfo)
148 {
149     abilityInfo.bundleName = extensionAbilityInfo.bundleName;
150     abilityInfo.name = extensionAbilityInfo.name;
151     abilityInfo.permissions = extensionAbilityInfo.permissions;
152     abilityInfo.visible = extensionAbilityInfo.visible;
153 }
154 
IsSameAppId(const std::string & callerAppId,const std::string & targetBundleName)155 bool BundleManagerInternal::IsSameAppId(const std::string& callerAppId, const std::string& targetBundleName)
156 {
157     if (targetBundleName.empty() || callerAppId.empty()) {
158         HILOGE("targetBundleName:%{public}s or callerAppId:%s is empty",
159             targetBundleName.c_str(), callerAppId.c_str());
160         return false;
161     }
162     HILOGD("callerAppId:%s", callerAppId.c_str());
163     std::string calleeAppId;
164     if (!GetCallerAppIdFromBms(targetBundleName, calleeAppId)) {
165         HILOGE("GetCallerAppIdFromBms failed");
166         return false;
167     }
168     HILOGD("calleeAppId:%s", calleeAppId.c_str());
169     return callerAppId == calleeAppId;
170 }
171 
GetLocalBundleInfo(const std::string & bundleName,AppExecFwk::BundleInfo & localBundleInfo)172 int32_t BundleManagerInternal::GetLocalBundleInfo(const std::string& bundleName,
173     AppExecFwk::BundleInfo &localBundleInfo)
174 {
175     auto bms = GetBundleManager();
176     if (bms == nullptr) {
177         HILOGE("get bundle manager failed");
178         return INVALID_PARAMETERS_ERR;
179     }
180 
181     std::vector<int32_t> ids;
182     ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids);
183     if (ret != ERR_OK || ids.empty()) {
184         HILOGE("QueryActiveOsAccountIds failed");
185         return INVALID_PARAMETERS_ERR;
186     }
187     int32_t activeAccountId = ids[0];
188     if (!bms->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
189         localBundleInfo, activeAccountId)) {
190         HILOGE("get local bundle info failed");
191         return INVALID_PARAMETERS_ERR;
192     }
193     return ERR_OK;
194 }
195 
GetLocalBundleInfoV9(const std::string & bundleName,AppExecFwk::BundleInfo & bundleInfo)196 int32_t BundleManagerInternal::GetLocalBundleInfoV9(const std::string& bundleName,
197     AppExecFwk::BundleInfo &bundleInfo)
198 {
199     auto bms = GetBundleManager();
200     if (bms == nullptr) {
201         HILOGE("get bundle manager failed");
202         return INVALID_PARAMETERS_ERR;
203     }
204 
205     std::vector<int32_t> ids;
206     ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids);
207     if (ret != ERR_OK || ids.empty()) {
208         HILOGE("QueryActiveOsAccountIds failed");
209         return INVALID_PARAMETERS_ERR;
210     }
211     int32_t activeAccountId = ids[0];
212     ret = bms->GetBundleInfoV9(bundleName,
213         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION),
214         bundleInfo, activeAccountId);
215     if (ret != ERR_OK) {
216         HILOGE("get local bundle info failed, ret: %{public}d", ret);
217     }
218     return ret;
219 }
220 
CheckRemoteBundleInfoForContinuation(const std::string & dstDeviceId,const std::string & bundleName,AppExecFwk::DistributedBundleInfo & remoteBundleInfo)221 int32_t BundleManagerInternal::CheckRemoteBundleInfoForContinuation(const std::string& dstDeviceId,
222     const std::string& bundleName, AppExecFwk::DistributedBundleInfo& remoteBundleInfo)
223 {
224     if (bundleName.empty()) {
225         HILOGE("bundle name empty");
226         return INVALID_PARAMETERS_ERR;
227     }
228     HILOGI("bundleName: %{public}s", bundleName.c_str());
229 
230     auto bms = GetBundleManager();
231     if (bms == nullptr) {
232         HILOGE("get bundle manager failed");
233         return INVALID_PARAMETERS_ERR;
234     }
235 
236     bool isInstalled = bms->GetDistributedBundleInfo(dstDeviceId, bundleName, remoteBundleInfo);
237     if (isInstalled) {
238         return ERR_OK;
239     }
240 
241     AppExecFwk::BundleInfo localBundleInfo;
242     if (GetLocalBundleInfo(bundleName, localBundleInfo) != ERR_OK) {
243         HILOGE("get local bundle info failed");
244         return INVALID_PARAMETERS_ERR;
245     }
246     if (localBundleInfo.entryInstallationFree) {
247         return CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL;
248     }
249     return CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL;
250 }
251 
CheckIfRemoteCanInstall(const AAFwk::Want & want,int32_t missionId)252 bool BundleManagerInternal::CheckIfRemoteCanInstall(const AAFwk::Want& want, int32_t missionId)
253 {
254     std::string bundleName = want.GetElement().GetBundleName();
255     std::string moduleName = want.GetElement().GetModuleName();
256     std::string abilityName = want.GetElement().GetAbilityName();
257     std::string deviceId = want.GetElement().GetDeviceID();
258     HILOGD("bundleName = %{public}s, moduleName = %{public}s, abilityName = %{public}s, deviceId = %{public}s",
259         bundleName.c_str(), moduleName.c_str(), abilityName.c_str(), deviceId.c_str());
260 
261     if (bundleName.empty() || moduleName.empty() || abilityName.empty() || deviceId.empty()) {
262         HILOGE("deviceId or bundle or module or ability name is empty");
263         return false;
264     }
265     auto bms = GetBundleManager();
266     if (bms == nullptr) {
267         HILOGE("get bundle manager failed");
268         return false;
269     }
270 
271     AAFwk::Want newWant;
272     newWant.SetElementName(deviceId, bundleName, abilityName, moduleName);
273     std::vector<int32_t> ids;
274     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
275     if (result != ERR_OK || ids.empty()) {
276         return false;
277     }
278     int32_t activeAccountId = ids[0];
279     bool ret = bms->CheckAbilityEnableInstall(newWant, missionId, activeAccountId, new DmsBundleManagerCallbackStub());
280     if (ret != true) {
281         HILOGE("CheckAbilityEnableInstall from bms failed");
282     }
283     return ret;
284 }
285 
GetBundleManager()286 sptr<AppExecFwk::IBundleMgr> BundleManagerInternal::GetBundleManager()
287 {
288     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
289     if (samgrProxy == nullptr) {
290         return nullptr;
291     }
292     sptr<IRemoteObject> bmsProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
293     if (bmsProxy == nullptr) {
294         HILOGE("failed to get bms from samgr");
295         return nullptr;
296     }
297     return iface_cast<AppExecFwk::IBundleMgr>(bmsProxy);
298 }
299 
GetDistributedBundleManager()300 sptr<AppExecFwk::IDistributedBms> BundleManagerInternal::GetDistributedBundleManager()
301 {
302     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
303     if (samgrProxy == nullptr) {
304         HILOGE("failed to get samgrProxy from dbms");
305         return nullptr;
306     }
307     sptr<IRemoteObject> dbmsProxy = samgrProxy->GetSystemAbility(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
308     if (dbmsProxy == nullptr) {
309         HILOGE("failed to get dbmsProxy from dbms");
310         return nullptr;
311     }
312     return iface_cast<AppExecFwk::IDistributedBms>(dbmsProxy);
313 }
314 
GetUidFromBms(const std::string & bundleName)315 int32_t BundleManagerInternal::GetUidFromBms(const std::string& bundleName)
316 {
317     auto bundleMgr = GetBundleManager();
318     if (bundleMgr == nullptr) {
319         HILOGE("failed to get bms");
320         return -1;
321     }
322     std::vector<int32_t> ids;
323     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
324     if (result != ERR_OK || ids.empty()) {
325         return -1;
326     }
327     int32_t activeAccountId = ids[0];
328     return bundleMgr->GetUidByBundleName(bundleName, activeAccountId);
329 }
330 
GetBundleIdFromBms(const std::string & bundleName,uint32_t & accessTokenId)331 int32_t BundleManagerInternal::GetBundleIdFromBms(const std::string& bundleName, uint32_t& accessTokenId)
332 {
333     auto bundleMgr = GetBundleManager();
334     if (bundleMgr == nullptr) {
335         HILOGE("failed to get bms");
336         return INVALID_PARAMETERS_ERR;
337     }
338     std::vector<int32_t> ids;
339     ErrCode result = OsAccountManager::QueryActiveOsAccountIds(ids);
340     if (result != ERR_OK || ids.empty()) {
341         HILOGE("fild to get userId");
342         return INVALID_PARAMETERS_ERR;
343     }
344     int32_t activeAccountId = ids[0];
345     AppExecFwk::ApplicationInfo appInfo;
346     int32_t flag = static_cast<int32_t>(AppExecFwk::GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT);
347     result = bundleMgr->GetApplicationInfoV9(bundleName, flag, activeAccountId, appInfo);
348     if (result != ERR_OK) {
349         HILOGE("failed to get appInfo from bms");
350         return CAN_NOT_FOUND_ABILITY_ERR;
351     }
352     accessTokenId = appInfo.accessTokenId;
353     return ERR_OK;
354 }
355 
GetBundleNameFromDbms(const std::string & networkId,const uint32_t accessTokenId,std::string & bundleName)356 int32_t BundleManagerInternal::GetBundleNameFromDbms(const std::string& networkId,
357     const uint32_t accessTokenId, std::string& bundleName)
358 {
359     auto bundleMgr = GetDistributedBundleManager();
360     if (bundleMgr == nullptr) {
361         HILOGE("failed to get dbms");
362         return INVALID_PARAMETERS_ERR;
363     }
364     int32_t result = bundleMgr->GetDistributedBundleName(networkId, accessTokenId, bundleName);
365     if (result != ERR_OK) {
366         HILOGE("failed to get bundleName from dbms");
367         return CAN_NOT_FOUND_ABILITY_ERR;
368     }
369     return ERR_OK;
370 }
371 
GetApplicationInfoFromBms(const std::string & bundleName,const AppExecFwk::BundleFlag flag,const int32_t userId,AppExecFwk::ApplicationInfo & appInfo)372 int32_t BundleManagerInternal::GetApplicationInfoFromBms(const std::string& bundleName,
373     const AppExecFwk::BundleFlag flag, const int32_t userId, AppExecFwk::ApplicationInfo &appInfo)
374 {
375     auto bundleMgr = GetBundleManager();
376     if (bundleMgr == nullptr) {
377         HILOGE("failed to get bms");
378         return INVALID_PARAMETERS_ERR;
379     }
380     std::string identity = IPCSkeleton::ResetCallingIdentity();
381     bundleMgr->GetApplicationInfo(bundleName, flag, userId, appInfo);
382     IPCSkeleton::SetCallingIdentity(identity);
383     return ERR_OK;
384 }
385 } // namespace DistributedSchedule
386 } // namespace OHOS
387