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