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_installer_host.h"
17
18 #ifdef ABILITY_RUNTIME_ENABLE
19 #include "ability_manager_client.h"
20 #endif
21 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
22 #include "app_control_manager.h"
23 #endif
24 #include "app_log_tag_wrapper.h"
25 #include "bundle_clone_installer.h"
26 #include "bundle_framework_core_ipc_interface_code.h"
27 #include "bundle_memory_guard.h"
28 #include "bundle_mgr_service.h"
29 #include "bundle_multiuser_installer.h"
30 #include "bundle_permission_mgr.h"
31 #include "ipc_skeleton.h"
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 namespace {
36 const std::string GET_MANAGER_FAIL = "fail to get bundle installer manager";
37 constexpr const char* BMS_PARA_BUNDLE_NAME = "ohos.bms.param.bundleName";
38 constexpr const char* BMS_PARA_IS_KEEP_DATA = "ohos.bms.param.isKeepData";
39 constexpr const char* BMS_PARA_USER_ID = "ohos.bms.param.userId";
40 constexpr const char* BMS_PARA_APP_INDEX = "ohos.bms.param.appIndex";
41 constexpr bool IS_KEEP_DATA = false;
42 int32_t INVALID_APP_INDEX = 0;
43 int32_t LOWER_DLP_TYPE_BOUND = 0;
44 int32_t UPPER_DLP_TYPE_BOUND = 3;
45 } // namespace
46
BundleInstallerHost()47 BundleInstallerHost::BundleInstallerHost()
48 {
49 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "create bundle installer host instance");
50 }
51
~BundleInstallerHost()52 BundleInstallerHost::~BundleInstallerHost()
53 {
54 LOG_NOFUNC_I(BMS_TAG_INSTALLER, "destroy bundle installer host instance");
55 }
56
Init()57 void BundleInstallerHost::Init()
58 {
59 LOG_D(BMS_TAG_INSTALLER, "begin to init");
60 manager_ = std::make_shared<BundleInstallerManager>();
61 LOG_D(BMS_TAG_INSTALLER, "init successfully");
62 }
63
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int BundleInstallerHost::OnRemoteRequest(
65 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
66 {
67 BundleMemoryGuard memoryGuard;
68 LOG_D(BMS_TAG_INSTALLER, "bundle installer host onReceived message, the message code is %{public}u", code);
69 std::u16string descripter = GetDescriptor();
70 std::u16string remoteDescripter = data.ReadInterfaceToken();
71 if (descripter != remoteDescripter) {
72 LOG_E(BMS_TAG_INSTALLER, "fail to write reply message in bundle mgr host due to the reply is nullptr");
73 return OBJECT_NULL;
74 }
75 switch (code) {
76 case static_cast<uint32_t>(BundleInstallerInterfaceCode::INSTALL):
77 HandleInstallMessage(data);
78 break;
79 case static_cast<uint32_t>(BundleInstallerInterfaceCode::INSTALL_MULTIPLE_HAPS):
80 HandleInstallMultipleHapsMessage(data);
81 break;
82 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL):
83 HandleUninstallMessage(data);
84 break;
85 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL_MODULE):
86 HandleUninstallModuleMessage(data);
87 break;
88 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL_BY_UNINSTALL_PARAM):
89 HandleUninstallByUninstallParam(data);
90 break;
91 case static_cast<uint32_t>(BundleInstallerInterfaceCode::RECOVER):
92 HandleRecoverMessage(data);
93 break;
94 case static_cast<uint32_t>(BundleInstallerInterfaceCode::INSTALL_SANDBOX_APP):
95 HandleInstallSandboxApp(data, reply);
96 break;
97 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL_SANDBOX_APP):
98 HandleUninstallSandboxApp(data, reply);
99 break;
100 case static_cast<uint32_t>(BundleInstallerInterfaceCode::CREATE_STREAM_INSTALLER):
101 HandleCreateStreamInstaller(data, reply);
102 break;
103 case static_cast<uint32_t>(BundleInstallerInterfaceCode::DESTORY_STREAM_INSTALLER):
104 HandleDestoryBundleStreamInstaller(data, reply);
105 break;
106 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL_AND_RECOVER):
107 HandleUninstallAndRecoverMessage(data);
108 break;
109 case static_cast<uint32_t>(BundleInstallerInterfaceCode::INSTALL_CLONE_APP):
110 HandleInstallCloneApp(data, reply);
111 break;
112 case static_cast<uint32_t>(BundleInstallerInterfaceCode::UNINSTALL_CLONE_APP):
113 HandleUninstallCloneApp(data, reply);
114 break;
115 case static_cast<uint32_t>(BundleInstallerInterfaceCode::INSTALL_EXISTED):
116 HandleInstallExisted(data, reply);
117 break;
118 default:
119 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
120 }
121 return NO_ERROR;
122 }
123
HandleInstallMessage(MessageParcel & data)124 void BundleInstallerHost::HandleInstallMessage(MessageParcel &data)
125 {
126 LOG_D(BMS_TAG_INSTALLER, "handle install message");
127 std::string bundlePath = Str16ToStr8(data.ReadString16());
128 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
129 if (installParam == nullptr) {
130 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
131 return;
132 }
133 sptr<IRemoteObject> object = data.ReadRemoteObject();
134 if (object == nullptr) {
135 LOG_E(BMS_TAG_INSTALLER, "read failed");
136 return;
137 }
138 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
139 installParam->withCopyHaps = true;
140 Install(bundlePath, *installParam, statusReceiver);
141 LOG_D(BMS_TAG_INSTALLER, "handle install message finished");
142 }
143
HandleRecoverMessage(MessageParcel & data)144 void BundleInstallerHost::HandleRecoverMessage(MessageParcel &data)
145 {
146 LOG_D(BMS_TAG_INSTALLER, "handle install message by bundleName");
147 std::string bundleName = Str16ToStr8(data.ReadString16());
148 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
149 if (installParam == nullptr) {
150 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
151 return;
152 }
153 sptr<IRemoteObject> object = data.ReadRemoteObject();
154 if (object == nullptr) {
155 LOG_E(BMS_TAG_INSTALLER, "read failed");
156 return;
157 }
158 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
159
160 installParam->preinstallSourceFlag = ApplicationInfoFlag::FLAG_RECOVER_INSTALLED;
161 Recover(bundleName, *installParam, statusReceiver);
162 LOG_D(BMS_TAG_INSTALLER, "handle install message by bundleName finished");
163 }
164
HandleInstallMultipleHapsMessage(MessageParcel & data)165 void BundleInstallerHost::HandleInstallMultipleHapsMessage(MessageParcel &data)
166 {
167 LOG_D(BMS_TAG_INSTALLER, "handle install multiple haps message");
168 int32_t size = data.ReadInt32();
169 if (size > ServiceConstants::MAX_HAP_NUMBER) {
170 LOG_E(BMS_TAG_INSTALLER, "bundle path size is greater than the max hap number 128");
171 return;
172 }
173 std::vector<std::string> pathVec;
174 for (int i = 0; i < size; ++i) {
175 pathVec.emplace_back(Str16ToStr8(data.ReadString16()));
176 }
177 if (size == 0 || pathVec.empty()) {
178 LOG_E(BMS_TAG_INSTALLER, "inputted bundlepath vector is empty");
179 return;
180 }
181 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
182 if (installParam == nullptr) {
183 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
184 return;
185 }
186 sptr<IRemoteObject> object = data.ReadRemoteObject();
187 if (object == nullptr) {
188 LOG_E(BMS_TAG_INSTALLER, "read failed");
189 return;
190 }
191 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
192 installParam->withCopyHaps = true;
193 Install(pathVec, *installParam, statusReceiver);
194 LOG_D(BMS_TAG_INSTALLER, "handle install multiple haps finished");
195 }
196
HandleUninstallMessage(MessageParcel & data)197 void BundleInstallerHost::HandleUninstallMessage(MessageParcel &data)
198 {
199 LOG_D(BMS_TAG_INSTALLER, "handle uninstall message");
200 std::string bundleName = Str16ToStr8(data.ReadString16());
201 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
202 if (installParam == nullptr) {
203 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
204 return;
205 }
206 sptr<IRemoteObject> object = data.ReadRemoteObject();
207 if (object == nullptr) {
208 LOG_E(BMS_TAG_INSTALLER, "read failed");
209 return;
210 }
211 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
212
213 Uninstall(bundleName, *installParam, statusReceiver);
214 LOG_D(BMS_TAG_INSTALLER, "handle uninstall message finished");
215 }
216
HandleUninstallModuleMessage(MessageParcel & data)217 void BundleInstallerHost::HandleUninstallModuleMessage(MessageParcel &data)
218 {
219 LOG_D(BMS_TAG_INSTALLER, "handle uninstall module message");
220 std::string bundleName = Str16ToStr8(data.ReadString16());
221 std::string modulePackage = Str16ToStr8(data.ReadString16());
222 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
223 if (installParam == nullptr) {
224 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
225 return;
226 }
227 sptr<IRemoteObject> object = data.ReadRemoteObject();
228 if (object == nullptr) {
229 LOG_E(BMS_TAG_INSTALLER, "read failed");
230 return;
231 }
232 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
233 Uninstall(bundleName, modulePackage, *installParam, statusReceiver);
234 LOG_D(BMS_TAG_INSTALLER, "handle uninstall message finished");
235 }
236
HandleUninstallByUninstallParam(MessageParcel & data)237 void BundleInstallerHost::HandleUninstallByUninstallParam(MessageParcel &data)
238 {
239 std::unique_ptr<UninstallParam> uninstallParam(data.ReadParcelable<UninstallParam>());
240 if (uninstallParam == nullptr) {
241 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<UninstallParam failed");
242 return;
243 }
244 sptr<IRemoteObject> object = data.ReadRemoteObject();
245 if (object == nullptr) {
246 LOG_E(BMS_TAG_INSTALLER, "read failed");
247 return;
248 }
249 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
250 Uninstall(*uninstallParam, statusReceiver);
251 }
252
HandleInstallSandboxApp(MessageParcel & data,MessageParcel & reply)253 void BundleInstallerHost::HandleInstallSandboxApp(MessageParcel &data, MessageParcel &reply)
254 {
255 LOG_D(BMS_TAG_INSTALLER, "handle install sandbox app message");
256 std::string bundleName = Str16ToStr8(data.ReadString16());
257 int32_t dplType = data.ReadInt32();
258 int32_t userId = data.ReadInt32();
259 int32_t appIndex = Constants::INITIAL_SANDBOX_APP_INDEX;
260 auto ret = InstallSandboxApp(bundleName, dplType, userId, appIndex);
261 if (!reply.WriteInt32(ret)) {
262 LOG_E(BMS_TAG_INSTALLER, "write failed");
263 }
264 if (ret == ERR_OK && !reply.WriteInt32(appIndex)) {
265 LOG_E(BMS_TAG_INSTALLER, "write failed");
266 }
267 LOG_D(BMS_TAG_INSTALLER, "handle install sandbox app message finished");
268 }
269
HandleUninstallSandboxApp(MessageParcel & data,MessageParcel & reply)270 void BundleInstallerHost::HandleUninstallSandboxApp(MessageParcel &data, MessageParcel &reply)
271 {
272 LOG_D(BMS_TAG_INSTALLER, "handle install sandbox app message");
273 std::string bundleName = Str16ToStr8(data.ReadString16());
274 int32_t appIndex = data.ReadInt32();
275 int32_t userId = data.ReadInt32();
276 auto ret = UninstallSandboxApp(bundleName, appIndex, userId);
277 if (!reply.WriteInt32(ret)) {
278 LOG_E(BMS_TAG_INSTALLER, "write failed");
279 }
280 LOG_D(BMS_TAG_INSTALLER, "handle install sandbox app message finished");
281 }
282
HandleCreateStreamInstaller(MessageParcel & data,MessageParcel & reply)283 void BundleInstallerHost::HandleCreateStreamInstaller(MessageParcel &data, MessageParcel &reply)
284 {
285 LOG_D(BMS_TAG_INSTALLER, "handle create stream installer message begin");
286 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
287 if (installParam == nullptr) {
288 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
289 return;
290 }
291 sptr<IRemoteObject> object = data.ReadRemoteObject();
292 if (object == nullptr) {
293 reply.WriteBool(false);
294 LOG_E(BMS_TAG_INSTALLER, "read receiver failed");
295 return;
296 }
297 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
298 if (statusReceiver == nullptr) {
299 reply.WriteBool(false);
300 LOG_E(BMS_TAG_INSTALLER, "cast remote object to status receiver error");
301 return;
302 }
303 std::vector<std::string> originHapPaths;
304 if (!data.ReadStringVector(&originHapPaths)) {
305 reply.WriteBool(false);
306 LOG_E(BMS_TAG_INSTALLER, "read originPaths failed");
307 return;
308 }
309
310 sptr<IBundleStreamInstaller> streamInstaller = CreateStreamInstaller(*installParam, statusReceiver, originHapPaths);
311 if (streamInstaller == nullptr) {
312 if (!reply.WriteBool(false)) {
313 LOG_E(BMS_TAG_INSTALLER, "write result failed");
314 }
315 return;
316 }
317 if (!reply.WriteBool(true)) {
318 LOG_E(BMS_TAG_INSTALLER, "write result failed");
319 return;
320 }
321 if (!reply.WriteUint32(streamInstaller->GetInstallerId())) {
322 LOG_E(BMS_TAG_INSTALLER, "write stream installe id failed");
323 return;
324 }
325 if (!reply.WriteRemoteObject(streamInstaller->AsObject())) {
326 LOG_E(BMS_TAG_INSTALLER, "write stream installer remote object failed");
327 return;
328 }
329
330 std::lock_guard<std::mutex> lock(streamInstallMutex_);
331 streamInstallers_.emplace_back(streamInstaller);
332 LOG_D(BMS_TAG_INSTALLER, "handle create stream installer message finish");
333 }
334
HandleDestoryBundleStreamInstaller(MessageParcel & data,MessageParcel & reply)335 void BundleInstallerHost::HandleDestoryBundleStreamInstaller(MessageParcel &data, MessageParcel &reply)
336 {
337 LOG_D(BMS_TAG_INSTALLER, "handle destory stream installer message begin");
338 uint32_t installeId = data.ReadUint32();
339 DestoryBundleStreamInstaller(installeId);
340 LOG_D(BMS_TAG_INSTALLER, "handle destoy stream installer message finish");
341 }
342
HandleUninstallAndRecoverMessage(MessageParcel & data)343 void BundleInstallerHost::HandleUninstallAndRecoverMessage(MessageParcel &data)
344 {
345 LOG_D(BMS_TAG_INSTALLER, "handle UninstallAndRecover message");
346 std::string bundleName = Str16ToStr8(data.ReadString16());
347 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
348 if (installParam == nullptr) {
349 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<InstallParam> failed");
350 return;
351 }
352 sptr<IRemoteObject> object = data.ReadRemoteObject();
353 if (object == nullptr) {
354 LOG_E(BMS_TAG_INSTALLER, "read failed");
355 return;
356 }
357 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
358 installParam->preinstallSourceFlag = ApplicationInfoFlag::FLAG_RECOVER_INSTALLED;
359 UninstallAndRecover(bundleName, *installParam, statusReceiver);
360 LOG_D(BMS_TAG_INSTALLER, "handle UninstallAndRecover message finished");
361 }
362
Install(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)363 bool BundleInstallerHost::Install(
364 const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
365 {
366 if (!CheckBundleInstallerManager(statusReceiver)) {
367 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
368 return false;
369 }
370 if (!BundlePermissionMgr::IsSystemApp() &&
371 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
372 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
373 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
374 return false;
375 }
376 if (!BundlePermissionMgr::IsSelfCalling() &&
377 !BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
378 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_BUNDLE) &&
379 !BundlePermissionMgr::VerifyCallingPermissionForAll(
380 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_NORMAL_BUNDLE) &&
381 !BundlePermissionMgr::VerifyCallingPermissionForAll(
382 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_MDM_BUNDLE) &&
383 !BundlePermissionMgr::VerifyCallingPermissionForAll(
384 ServiceConstants::PERMISSION_INSTALL_INTERNALTESTING_BUNDLE)) {
385 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
386 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
387 return false;
388 }
389
390 manager_->CreateInstallTask(bundleFilePath, installParam, statusReceiver);
391 return true;
392 }
393
Install(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)394 bool BundleInstallerHost::Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
395 const sptr<IStatusReceiver> &statusReceiver)
396 {
397 if (!CheckBundleInstallerManager(statusReceiver)) {
398 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
399 return false;
400 }
401 if (!BundlePermissionMgr::IsSystemApp() &&
402 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
403 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
404 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
405 return false;
406 }
407 if (!BundlePermissionMgr::IsSelfCalling() &&
408 !BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
409 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_BUNDLE) &&
410 !BundlePermissionMgr::VerifyCallingPermissionForAll(
411 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_NORMAL_BUNDLE) &&
412 !BundlePermissionMgr::VerifyCallingPermissionForAll(
413 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_MDM_BUNDLE) &&
414 !BundlePermissionMgr::VerifyCallingPermissionForAll(
415 ServiceConstants::PERMISSION_INSTALL_INTERNALTESTING_BUNDLE)) {
416 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
417 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
418 return false;
419 }
420
421 manager_->CreateInstallTask(bundleFilePaths, installParam, statusReceiver);
422 return true;
423 }
424
Recover(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)425 bool BundleInstallerHost::Recover(
426 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
427 {
428 if (!CheckBundleInstallerManager(statusReceiver)) {
429 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
430 return false;
431 }
432 if (!BundlePermissionMgr::IsSystemApp() &&
433 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
434 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
435 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
436 return false;
437 }
438 if (!BundlePermissionMgr::VerifyRecoverPermission()) {
439 LOG_E(BMS_TAG_INSTALLER, "Recover permission denied");
440 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
441 return false;
442 }
443 manager_->CreateRecoverTask(bundleName, CheckInstallParam(installParam), statusReceiver);
444 return true;
445 }
446
Uninstall(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)447 bool BundleInstallerHost::Uninstall(
448 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
449 {
450 if (!CheckBundleInstallerManager(statusReceiver)) {
451 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
452 return false;
453 }
454 if (!BundlePermissionMgr::IsSystemApp() &&
455 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
456 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
457 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
458 return false;
459 }
460 if (!BundlePermissionMgr::VerifyUninstallPermission()) {
461 LOG_E(BMS_TAG_INSTALLER, "uninstall permission denied");
462 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_PERMISSION_DENIED, "");
463 return false;
464 }
465 if (installParam.IsVerifyUninstallRule() &&
466 CheckUninstallDisposedRule(bundleName, installParam.userId, Constants::MAIN_APP_INDEX,
467 installParam.isKeepData)) {
468 LOG_W(BMS_TAG_INSTALLER, "CheckUninstallDisposedRule failed");
469 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_DISPOSED_RULE_FAILED, "");
470 return false;
471 }
472 manager_->CreateUninstallTask(bundleName, CheckInstallParam(installParam), statusReceiver);
473 return true;
474 }
475
Uninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)476 bool BundleInstallerHost::Uninstall(const std::string &bundleName, const std::string &modulePackage,
477 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
478 {
479 if (!CheckBundleInstallerManager(statusReceiver)) {
480 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
481 return false;
482 }
483 if (!BundlePermissionMgr::IsSystemApp() &&
484 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
485 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
486 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
487 return false;
488 }
489 if (!BundlePermissionMgr::VerifyUninstallPermission()) {
490 LOG_E(BMS_TAG_INSTALLER, "uninstall permission denied");
491 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_PERMISSION_DENIED, "");
492 return false;
493 }
494 if (installParam.IsVerifyUninstallRule() &&
495 CheckUninstallDisposedRule(
496 bundleName, installParam.userId, Constants::MAIN_APP_INDEX, installParam.isKeepData, modulePackage)) {
497 LOG_W(BMS_TAG_INSTALLER, "CheckUninstallDisposedRule failed");
498 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_DISPOSED_RULE_FAILED, "");
499 return false;
500 }
501 manager_->CreateUninstallTask(
502 bundleName, modulePackage, CheckInstallParam(installParam), statusReceiver);
503 return true;
504 }
505
Uninstall(const UninstallParam & uninstallParam,const sptr<IStatusReceiver> & statusReceiver)506 bool BundleInstallerHost::Uninstall(const UninstallParam &uninstallParam,
507 const sptr<IStatusReceiver> &statusReceiver)
508 {
509 if (!CheckBundleInstallerManager(statusReceiver)) {
510 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
511 return false;
512 }
513 if (!BundlePermissionMgr::IsSystemApp()) {
514 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
515 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
516 return false;
517 }
518 if (!BundlePermissionMgr::VerifyUninstallPermission()) {
519 LOG_E(BMS_TAG_INSTALLER, "uninstall permission denied");
520 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_PERMISSION_DENIED, "");
521 return false;
522 }
523 manager_->CreateUninstallTask(uninstallParam, statusReceiver);
524 return true;
525 }
526
InstallByBundleName(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)527 bool BundleInstallerHost::InstallByBundleName(const std::string &bundleName,
528 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
529 {
530 if (!CheckBundleInstallerManager(statusReceiver)) {
531 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
532 return false;
533 }
534 if (!BundlePermissionMgr::IsSystemApp() &&
535 !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) {
536 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
537 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
538 return false;
539 }
540 if (!BundlePermissionMgr::IsSelfCalling() &&
541 !BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
542 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_BUNDLE) &&
543 !BundlePermissionMgr::VerifyCallingPermissionForAll(
544 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_NORMAL_BUNDLE) &&
545 !BundlePermissionMgr::VerifyCallingPermissionForAll(
546 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_MDM_BUNDLE) &&
547 !BundlePermissionMgr::VerifyCallingPermissionForAll(
548 ServiceConstants::PERMISSION_INSTALL_INTERNALTESTING_BUNDLE)) {
549 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
550 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
551 return false;
552 }
553
554 manager_->CreateInstallByBundleNameTask(bundleName, CheckInstallParam(installParam), statusReceiver);
555 return true;
556 }
557
InstallSandboxApp(const std::string & bundleName,int32_t dplType,int32_t userId,int32_t & appIndex)558 ErrCode BundleInstallerHost::InstallSandboxApp(const std::string &bundleName, int32_t dplType, int32_t userId,
559 int32_t &appIndex)
560 {
561 if (bundleName.empty() || dplType <= LOWER_DLP_TYPE_BOUND || dplType >= UPPER_DLP_TYPE_BOUND) {
562 LOG_E(BMS_TAG_INSTALLER, "install sandbox failed due to error parameters");
563 return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
564 }
565 if (!BundlePermissionMgr::IsSystemApp()) {
566 LOG_E(BMS_TAG_INSTALLER, "vnon-system app calling system api");
567 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
568 }
569 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
570 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_SANDBOX_BUNDLE)) {
571 LOG_E(BMS_TAG_INSTALLER, "InstallSandboxApp permission denied");
572 return ERR_APPEXECFWK_PERMISSION_DENIED;
573 }
574 auto helper = DelayedSingleton<BundleSandboxAppHelper>::GetInstance();
575 if (helper == nullptr) {
576 return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
577 }
578 auto res = helper->InstallSandboxApp(bundleName, dplType, userId, appIndex);
579 if (res != ERR_OK) {
580 LOG_E(BMS_TAG_INSTALLER, "install sandbox failed due to error code : %{public}d", res);
581 }
582 return res;
583 }
584
UninstallSandboxApp(const std::string & bundleName,int32_t appIndex,int32_t userId)585 ErrCode BundleInstallerHost::UninstallSandboxApp(const std::string &bundleName, int32_t appIndex, int32_t userId)
586 {
587 // check bundle name
588 if (bundleName.empty()) {
589 LOG_E(BMS_TAG_INSTALLER, "uninstall sandbox failed due to empty bundleName");
590 return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
591 }
592 // check appIndex
593 if (appIndex <= INVALID_APP_INDEX || appIndex > Constants::MAX_SANDBOX_APP_INDEX) {
594 LOG_E(BMS_TAG_INSTALLER, "the appIndex %{public}d is invalid", appIndex);
595 return ERR_APPEXECFWK_SANDBOX_INSTALL_PARAM_ERROR;
596 }
597 if (!BundlePermissionMgr::IsSystemApp()) {
598 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
599 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
600 }
601 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
602 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_UNINSTALL_SANDBOX_BUNDLE)) {
603 LOG_E(BMS_TAG_INSTALLER, "UninstallSandboxApp permission denied");
604 return ERR_APPEXECFWK_PERMISSION_DENIED;
605 }
606 auto helper = DelayedSingleton<BundleSandboxAppHelper>::GetInstance();
607 if (helper == nullptr) {
608 return ERR_APPEXECFWK_SANDBOX_INSTALL_INTERNAL_ERROR;
609 }
610 auto res = helper->UninstallSandboxApp(bundleName, appIndex, userId);
611 if (res != ERR_OK) {
612 LOG_E(BMS_TAG_INSTALLER, "uninstall sandbox failed due to error code : %{public}d", res);
613 }
614 return res;
615 }
616
StreamInstall(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)617 ErrCode BundleInstallerHost::StreamInstall(const std::vector<std::string> &bundleFilePaths,
618 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
619 {
620 return ERR_OK;
621 }
622
CreateStreamInstaller(const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver,const std::vector<std::string> & originHapPaths)623 sptr<IBundleStreamInstaller> BundleInstallerHost::CreateStreamInstaller(const InstallParam &installParam,
624 const sptr<IStatusReceiver> &statusReceiver, const std::vector<std::string> &originHapPaths)
625 {
626 if (!CheckBundleInstallerManager(statusReceiver)) {
627 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
628 return nullptr;
629 }
630 if (!BundlePermissionMgr::IsSystemApp()) {
631 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
632 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
633 return nullptr;
634 }
635 InstallParam verifiedInstallParam = installParam;
636 if (!IsPermissionVaild(installParam, verifiedInstallParam)) {
637 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
638 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
639 return nullptr;
640 }
641 auto uid = IPCSkeleton::GetCallingUid();
642 sptr<BundleStreamInstallerHostImpl> streamInstaller(new (std::nothrow) BundleStreamInstallerHostImpl(
643 ++streamInstallerIds_, uid));
644 if (streamInstaller == nullptr) {
645 LOG_E(BMS_TAG_INSTALLER, "streamInstaller is nullptr, uid : %{public}d", uid);
646 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR, "");
647 return nullptr;
648 }
649 bool res = streamInstaller->Init(verifiedInstallParam, statusReceiver, originHapPaths);
650 if (!res) {
651 LOG_E(BMS_TAG_INSTALLER, "stream installer init failed");
652 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR, "");
653 return nullptr;
654 }
655 return streamInstaller;
656 }
657
IsPermissionVaild(const InstallParam & installParam,InstallParam & verifiedInstallParam)658 bool BundleInstallerHost::IsPermissionVaild(const InstallParam &installParam, InstallParam &verifiedInstallParam)
659 {
660 verifiedInstallParam.isCallByShell = BundlePermissionMgr::IsShellTokenType();
661 verifiedInstallParam.installBundlePermissionStatus =
662 BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) ?
663 PermissionStatus::HAVE_PERMISSION_STATUS : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
664 verifiedInstallParam.installEnterpriseBundlePermissionStatus =
665 BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_BUNDLE) ?
666 PermissionStatus::HAVE_PERMISSION_STATUS : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
667 verifiedInstallParam.installEtpNormalBundlePermissionStatus =
668 BundlePermissionMgr::VerifyCallingPermissionForAll(
669 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_NORMAL_BUNDLE) ?
670 PermissionStatus::HAVE_PERMISSION_STATUS : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
671 verifiedInstallParam.installEtpMdmBundlePermissionStatus =
672 BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_MDM_BUNDLE) ?
673 PermissionStatus::HAVE_PERMISSION_STATUS : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
674 verifiedInstallParam.installInternaltestingBundlePermissionStatus =
675 BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_INTERNALTESTING_BUNDLE)
676 ? PermissionStatus::HAVE_PERMISSION_STATUS
677 : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
678 verifiedInstallParam.installUpdateSelfBundlePermissionStatus =
679 BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_SELF_BUNDLE) ?
680 PermissionStatus::HAVE_PERMISSION_STATUS : PermissionStatus::NON_HAVE_PERMISSION_STATUS;
681 return (verifiedInstallParam.installBundlePermissionStatus == PermissionStatus::HAVE_PERMISSION_STATUS ||
682 verifiedInstallParam.installEnterpriseBundlePermissionStatus == PermissionStatus::HAVE_PERMISSION_STATUS ||
683 verifiedInstallParam.installEtpNormalBundlePermissionStatus == PermissionStatus::HAVE_PERMISSION_STATUS ||
684 verifiedInstallParam.installEtpMdmBundlePermissionStatus == PermissionStatus::HAVE_PERMISSION_STATUS ||
685 verifiedInstallParam.installUpdateSelfBundlePermissionStatus == PermissionStatus::HAVE_PERMISSION_STATUS ||
686 BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_QUICK_FIX_BUNDLE));
687 }
688
DestoryBundleStreamInstaller(uint32_t streamInstallerId)689 bool BundleInstallerHost::DestoryBundleStreamInstaller(uint32_t streamInstallerId)
690 {
691 if (!BundlePermissionMgr::IsSystemApp()) {
692 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
693 return false;
694 }
695 if (!BundlePermissionMgr::IsSelfCalling() &&
696 !BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE) &&
697 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_BUNDLE) &&
698 !BundlePermissionMgr::VerifyCallingPermissionForAll(
699 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_NORMAL_BUNDLE) &&
700 !BundlePermissionMgr::VerifyCallingPermissionForAll(
701 ServiceConstants::PERMISSION_INSTALL_ENTERPRISE_MDM_BUNDLE) &&
702 !BundlePermissionMgr::VerifyCallingPermissionForAll(
703 ServiceConstants::PERMISSION_INSTALL_INTERNALTESTING_BUNDLE) &&
704 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_QUICK_FIX_BUNDLE)) {
705 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
706 return false;
707 }
708 std::lock_guard<std::mutex> lock(streamInstallMutex_);
709 for (auto it = streamInstallers_.begin(); it != streamInstallers_.end();) {
710 if ((*it)->GetInstallerId() == streamInstallerId) {
711 (*it)->UnInit();
712 it = streamInstallers_.erase(it);
713 } else {
714 it++;
715 }
716 }
717 return true;
718 }
719
CheckBundleInstallerManager(const sptr<IStatusReceiver> & statusReceiver) const720 bool BundleInstallerHost::CheckBundleInstallerManager(const sptr<IStatusReceiver> &statusReceiver) const
721 {
722 if (statusReceiver == nullptr) {
723 LOG_E(BMS_TAG_INSTALLER, "the receiver is nullptr");
724 return false;
725 }
726 if (manager_ == nullptr) {
727 LOG_E(BMS_TAG_INSTALLER, "the bundle installer manager is nullptr");
728 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR, GET_MANAGER_FAIL);
729 return false;
730 }
731 return true;
732 }
733
CheckInstallParam(const InstallParam & installParam)734 InstallParam BundleInstallerHost::CheckInstallParam(const InstallParam &installParam)
735 {
736 if (installParam.userId == Constants::UNSPECIFIED_USERID) {
737 LOG_I(BMS_TAG_INSTALLER, "installParam userId is unspecified and get calling userId by callingUid");
738 InstallParam callInstallParam = installParam;
739 callInstallParam.userId = BundleUtil::GetUserIdByCallingUid();
740 return callInstallParam;
741 }
742
743 return installParam;
744 }
745
UpdateBundleForSelf(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)746 bool BundleInstallerHost::UpdateBundleForSelf(const std::vector<std::string> &bundleFilePaths,
747 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
748 {
749 if (!CheckBundleInstallerManager(statusReceiver)) {
750 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
751 return false;
752 }
753 if (!BundlePermissionMgr::IsSystemApp()) {
754 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
755 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
756 return false;
757 }
758 if (!BundlePermissionMgr::IsSelfCalling() &&
759 !BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_INSTALL_SELF_BUNDLE)) {
760 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
761 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
762 return false;
763 }
764 manager_->CreateInstallTask(bundleFilePaths, installParam, statusReceiver);
765 return true;
766 }
767
UninstallAndRecover(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)768 bool BundleInstallerHost::UninstallAndRecover(const std::string &bundleName, const InstallParam &installParam,
769 const sptr<IStatusReceiver> &statusReceiver)
770 {
771 if (!CheckBundleInstallerManager(statusReceiver)) {
772 LOG_E(BMS_TAG_INSTALLER, "statusReceiver invalid");
773 return false;
774 }
775 if (!BundlePermissionMgr::IsSystemApp()) {
776 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api");
777 statusReceiver->OnFinished(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, "");
778 return false;
779 }
780 if (!BundlePermissionMgr::IsSelfCalling() &&
781 !BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_INSTALL_BUNDLE,
782 ServiceConstants::PERMISSION_UNINSTALL_BUNDLE})) {
783 LOG_E(BMS_TAG_INSTALLER, "install permission denied");
784 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
785 return false;
786 }
787 manager_->CreateUninstallAndRecoverTask(bundleName, CheckInstallParam(installParam), statusReceiver);
788 return true;
789 }
790
AddTask(const ThreadPoolTask & task,const std::string & taskName)791 void BundleInstallerHost::AddTask(const ThreadPoolTask &task, const std::string &taskName)
792 {
793 manager_->AddTask(task, taskName);
794 }
795
GetThreadsNum()796 int32_t BundleInstallerHost::GetThreadsNum()
797 {
798 return manager_->GetThreadsNum();
799 }
800
GetCurTaskNum()801 size_t BundleInstallerHost::GetCurTaskNum()
802 {
803 return manager_->GetCurTaskNum();
804 }
805
InstallCloneApp(const std::string & bundleName,int32_t userId,int32_t & appIndex)806 ErrCode BundleInstallerHost::InstallCloneApp(const std::string &bundleName, int32_t userId, int32_t& appIndex)
807 {
808 LOG_D(BMS_TAG_INSTALLER, "params[bundleName: %{public}s, user_id: %{public}d, appIndex: %{public}d]",
809 bundleName.c_str(), userId, appIndex);
810 if (bundleName.empty()) {
811 LOG_E(BMS_TAG_INSTALLER, "install clone app failed due to error parameters");
812 return ERR_APPEXECFWK_CLONE_INSTALL_PARAM_ERROR;
813 }
814 if (!BundlePermissionMgr::IsSystemApp()) {
815 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api bundleName: %{public}s", bundleName.c_str());
816 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
817 }
818 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_CLONE_BUNDLE)) {
819 LOG_E(BMS_TAG_INSTALLER, "InstallCloneApp permission denied");
820 return ERR_APPEXECFWK_PERMISSION_DENIED;
821 }
822 std::shared_ptr<BundleCloneInstaller> installer = std::make_shared<BundleCloneInstaller>();
823 return installer->InstallCloneApp(bundleName, userId, appIndex);
824 }
825
HandleInstallCloneApp(MessageParcel & data,MessageParcel & reply)826 void BundleInstallerHost::HandleInstallCloneApp(MessageParcel &data, MessageParcel &reply)
827 {
828 LOG_D(BMS_TAG_INSTALLER, "handle install clone app message");
829 std::string bundleName = Str16ToStr8(data.ReadString16());
830 int32_t userId = data.ReadInt32();
831 int32_t appIndex = data.ReadInt32();
832
833 LOG_I(BMS_TAG_INSTALLER, "receive Install CLone App Request");
834
835 auto ret = InstallCloneApp(bundleName, userId, appIndex);
836 if (!reply.WriteInt32(ret)) {
837 LOG_E(BMS_TAG_INSTALLER, "write failed");
838 }
839
840 if (ret == ERR_OK && !reply.WriteInt32(appIndex)) {
841 LOG_E(BMS_TAG_INSTALLER, "write failed");
842 }
843 LOG_D(BMS_TAG_INSTALLER, "handle install clone app message finished");
844 }
845
UninstallCloneApp(const std::string & bundleName,int32_t userId,int32_t appIndex,const DestroyAppCloneParam & destroyAppCloneParam)846 ErrCode BundleInstallerHost::UninstallCloneApp(const std::string &bundleName, int32_t userId, int32_t appIndex,
847 const DestroyAppCloneParam &destroyAppCloneParam)
848 {
849 LOG_D(BMS_TAG_INSTALLER, "params[bundleName: %{public}s, user_id: %{public}d, appIndex: %{public}d]",
850 bundleName.c_str(), userId, appIndex);
851 if (bundleName.empty()) {
852 LOG_E(BMS_TAG_INSTALLER, "install clone app failed due to empty bundleName");
853 return ERR_APPEXECFWK_CLONE_UNINSTALL_INVALID_BUNDLE_NAME;
854 }
855 if (!BundlePermissionMgr::IsSystemApp()) {
856 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api, bundleName: %{public}s", bundleName.c_str());
857 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
858 }
859 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_UNINSTALL_CLONE_BUNDLE)) {
860 LOG_E(BMS_TAG_INSTALLER, "UninstallCloneApp permission denied");
861 return ERR_APPEXECFWK_PERMISSION_DENIED;
862 }
863 if (appIndex < ServiceConstants::CLONE_APP_INDEX_MIN || appIndex > ServiceConstants::CLONE_APP_INDEX_MAX) {
864 APP_LOGE("Add Clone Bundle Fail, appIndex: %{public}d not in valid range", appIndex);
865 return ERR_APPEXECFWK_CLONE_UNINSTALL_INVALID_APP_INDEX;
866 }
867 if (destroyAppCloneParam.IsVerifyUninstallRule() &&
868 CheckUninstallDisposedRule(bundleName, userId, appIndex, IS_KEEP_DATA)) {
869 LOG_W(BMS_TAG_INSTALLER, "CheckUninstallDisposedRule failed");
870 return ERR_APPEXECFWK_UNINSTALL_DISPOSED_RULE_FAILED;
871 }
872 std::shared_ptr<BundleCloneInstaller> installer = std::make_shared<BundleCloneInstaller>();
873 return installer->UninstallCloneApp(bundleName, userId, appIndex);
874 }
875
HandleUninstallCloneApp(MessageParcel & data,MessageParcel & reply)876 void BundleInstallerHost::HandleUninstallCloneApp(MessageParcel &data, MessageParcel &reply)
877 {
878 LOG_D(BMS_TAG_INSTALLER, "handle uninstall clone app message");
879 std::string bundleName = Str16ToStr8(data.ReadString16());
880 int32_t userId = data.ReadInt32();
881 int32_t appIndex = data.ReadInt32();
882 std::unique_ptr<DestroyAppCloneParam> destroyAppCloneParam(data.ReadParcelable<DestroyAppCloneParam>());
883 if (destroyAppCloneParam == nullptr) {
884 LOG_E(BMS_TAG_INSTALLER, "ReadParcelable<DestroyAppCloneParam> failed");
885 return;
886 }
887
888 LOG_I(BMS_TAG_INSTALLER, "receive Uninstall CLone App Request");
889
890 auto ret = UninstallCloneApp(bundleName, userId, appIndex, *destroyAppCloneParam);
891 if (!reply.WriteInt32(ret)) {
892 LOG_E(BMS_TAG_INSTALLER, "write failed");
893 }
894 LOG_D(BMS_TAG_INSTALLER, "handle uninstall clone app message finished");
895 }
896
InstallExisted(const std::string & bundleName,int32_t userId)897 ErrCode BundleInstallerHost::InstallExisted(const std::string &bundleName, int32_t userId)
898 {
899 LOG_D(BMS_TAG_INSTALLER, "params[bundleName: %{public}s, user_id: %{public}d]",
900 bundleName.c_str(), userId);
901 if (bundleName.empty()) {
902 LOG_E(BMS_TAG_INSTALLER, "install existed app failed due to error parameters");
903 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
904 }
905 if (!BundlePermissionMgr::IsSystemApp()) {
906 LOG_E(BMS_TAG_INSTALLER, "non-system app calling system api bundleName: %{public}s", bundleName.c_str());
907 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
908 }
909 if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_INSTALL_BUNDLE)) {
910 LOG_E(BMS_TAG_INSTALLER, "InstallExisted permission denied");
911 return ERR_APPEXECFWK_PERMISSION_DENIED;
912 }
913 std::shared_ptr<BundleMultiUserInstaller> installer = std::make_shared<BundleMultiUserInstaller>();
914 return installer->InstallExistedApp(bundleName, userId);
915 }
916
HandleInstallExisted(MessageParcel & data,MessageParcel & reply)917 void BundleInstallerHost::HandleInstallExisted(MessageParcel &data, MessageParcel &reply)
918 {
919 LOG_D(BMS_TAG_INSTALLER, "handle install existed app message");
920 std::string bundleName = Str16ToStr8(data.ReadString16());
921 int32_t userId = data.ReadInt32();
922
923 LOG_I(BMS_TAG_INSTALLER, "receive InstallExisted Request -n %{public}s -u %{public}d",
924 bundleName.c_str(), userId);
925
926 auto ret = InstallExisted(bundleName, userId);
927 if (!reply.WriteInt32(ret)) {
928 LOG_E(BMS_TAG_INSTALLER, "write failed");
929 }
930 LOG_D(BMS_TAG_INSTALLER, "handle installExisted message finished");
931 }
932
CheckUninstallDisposedRule(const std::string & bundleName,int32_t userId,int32_t appIndex,bool isKeepData,const std::string & modulePackage)933 bool BundleInstallerHost::CheckUninstallDisposedRule(
934 const std::string &bundleName, int32_t userId, int32_t appIndex, bool isKeepData, const std::string &modulePackage)
935 {
936 #if defined (BUNDLE_FRAMEWORK_APP_CONTROL) && defined (ABILITY_RUNTIME_ENABLE)
937 std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
938 if (dataMgr == nullptr) {
939 LOG_E(BMS_TAG_INSTALLER, "null dataMgr");
940 return false;
941 }
942
943 InnerBundleInfo bundleInfo;
944 bool isBundleExist = dataMgr->FetchInnerBundleInfo(bundleName, bundleInfo);
945 if (!isBundleExist) {
946 LOG_E(BMS_TAG_INSTALLER, "the bundle: %{public}s is not install", bundleName.c_str());
947 return false;
948 }
949 if (!modulePackage.empty() && !bundleInfo.IsOnlyModule(modulePackage)) {
950 return false;
951 }
952 std::string appId = bundleInfo.GetAppIdentifier();
953 if (appId.empty()) {
954 appId = bundleInfo.GetAppId();
955 }
956
957 if (userId == Constants::UNSPECIFIED_USERID) {
958 LOG_I(BMS_TAG_INSTALLER, "installParam userId is unspecified and get calling userId by callingUid");
959 userId = BundleUtil::GetUserIdByCallingUid();
960 }
961
962 UninstallDisposedRule rule;
963 auto ret = DelayedSingleton<AppControlManager>::GetInstance()
964 ->GetUninstallDisposedRule(appId, appIndex, userId, rule);
965 if (ret != ERR_OK) {
966 LOG_E(BMS_TAG_INSTALLER, "GetUninstallDisposedRule failed code:%{public}d", ret);
967 return false;
968 }
969
970 if (rule.want == nullptr) {
971 LOG_E(BMS_TAG_INSTALLER, "null rule.want");
972 return false;
973 }
974 rule.want->SetParam(BMS_PARA_BUNDLE_NAME, bundleName);
975 rule.want->SetParam(BMS_PARA_USER_ID, userId);
976 rule.want->SetParam(BMS_PARA_APP_INDEX, appIndex);
977 rule.want->SetParam(BMS_PARA_IS_KEEP_DATA, isKeepData);
978
979 if (rule.uninstallComponentType == UninstallComponentType::EXTENSION) {
980 std::string identity = IPCSkeleton::ResetCallingIdentity();
981 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartExtensionAbility(
982 *rule.want, nullptr, userId, AppExecFwk::ExtensionAbilityType::SERVICE);
983 IPCSkeleton::SetCallingIdentity(identity);
984 if (err != ERR_OK) {
985 LOG_E(BMS_TAG_INSTALLER, "start extension ability failed code:%{public}d", err);
986 }
987 } else {
988 LOG_E(BMS_TAG_INSTALLER, "uninstallComponentType wrong type:%{public}d", rule.uninstallComponentType);
989 }
990
991 return true;
992 #else
993 LOG_I(BMS_TAG_INSTALLER, "BUNDLE_FRAMEWORK_APP_CONTROL or ABILITY_RUNTIME_ENABLE is false");
994 return false;
995 #endif
996 }
997 } // namespace AppExecFwk
998 } // namespace OHOS