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