1 /*
2 * Copyright (c) 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 "user_unlocked_event_subscriber.h"
17
18 #include <thread>
19
20 #include "app_control_manager.h"
21 #include "app_log_wrapper.h"
22 #include "bundle_mgr_service.h"
23 #include "common_event_manager.h"
24 #include "common_event_support.h"
25 #if defined (BUNDLE_FRAMEWORK_SANDBOX_APP) && defined (DLP_PERMISSION_ENABLE)
26 #include "dlp_permission_kit.h"
27 #endif
28 #include "installd_client.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
UserUnlockedEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)32 UserUnlockedEventSubscriber::UserUnlockedEventSubscriber(
33 const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo)
34 {}
35
~UserUnlockedEventSubscriber()36 UserUnlockedEventSubscriber::~UserUnlockedEventSubscriber()
37 {}
38
OnReceiveEvent(const EventFwk::CommonEventData & data)39 void UserUnlockedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
40 {
41 std::string action = data.GetWant().GetAction();
42 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
43 int32_t userId = data.GetCode();
44 APP_LOGI("UserUnlockedEventSubscriber userId %{public}d is unlocked", userId);
45 std::thread updateDataDirThread(UpdateAppDataDirSelinuxLabel, userId);
46 updateDataDirThread.detach();
47 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
48 DelayedSingleton<AppControlManager>::GetInstance()->SetAppInstallControlStatus();
49 #endif
50 }
51 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
52 #if defined (BUNDLE_FRAMEWORK_SANDBOX_APP) && defined (DLP_PERMISSION_ENABLE)
53 APP_LOGI("RemoveUnreservedSandbox call ClearUnreservedSandbox");
54 Security::DlpPermission::DlpPermissionKit::ClearUnreservedSandbox();
55 #endif
56 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
57 DelayedSingleton<AppControlManager>::GetInstance()->SetAppInstallControlStatus();
58 #endif
59 }
60 }
61
CreateBundleDataDir(const BundleInfo & bundleInfo,int32_t userId)62 bool UserUnlockedEventSubscriber::CreateBundleDataDir(const BundleInfo &bundleInfo, int32_t userId)
63 {
64 std::string baseBundleDataDir = Constants::BUNDLE_APP_DATA_BASE_DIR + Constants::BUNDLE_EL[1] +
65 Constants::PATH_SEPARATOR + std::to_string(userId) + Constants::BASE + bundleInfo.name;
66 bool isExist = false;
67 if (InstalldClient::GetInstance()->IsExistDir(baseBundleDataDir, isExist) != ERR_OK) {
68 APP_LOGE("path: %{private}s IsExistDir failed", baseBundleDataDir.c_str());
69 return false;
70 }
71 if (!isExist) {
72 APP_LOGD("path: %{private}s is not exist, need to create it", baseBundleDataDir.c_str());
73 CreateDirParam createDirParam;
74 createDirParam.userId = userId;
75 createDirParam.bundleName = bundleInfo.name;
76 createDirParam.uid = bundleInfo.uid;
77 createDirParam.gid = bundleInfo.gid;
78 createDirParam.apl = bundleInfo.applicationInfo.appPrivilegeLevel;
79 createDirParam.isPreInstallApp = bundleInfo.isPreInstallApp;
80 createDirParam.debug = bundleInfo.applicationInfo.debug;
81 createDirParam.createDirFlag = CreateDirFlag::CREATE_DIR_UNLOCKED;
82 if (InstalldClient::GetInstance()->CreateBundleDataDir(createDirParam) != ERR_OK) {
83 APP_LOGE("failed to CreateBundleDataDir");
84 return false;
85 }
86 }
87 return true;
88 }
89
UpdateAppDataDirSelinuxLabel(int32_t userId)90 void UserUnlockedEventSubscriber::UpdateAppDataDirSelinuxLabel(int32_t userId)
91 {
92 APP_LOGD("UpdateAppDataDirSelinuxLabel userId:%{public}d", userId);
93 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
94 if (dataMgr == nullptr) {
95 APP_LOGE("UpdateAppDataDirSelinuxLabel DataMgr is nullptr");
96 return;
97 }
98 std::vector<BundleInfo> bundleInfos;
99 if (!dataMgr->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, userId)) {
100 APP_LOGE("UpdateAppDataDirSelinuxLabel GetAllBundleInfos failed");
101 return;
102 }
103 std::string baseBundleDataDir = Constants::BUNDLE_APP_DATA_BASE_DIR + Constants::BUNDLE_EL[1] +
104 Constants::PATH_SEPARATOR + std::to_string(userId);
105
106 for (const auto &bundleInfo : bundleInfos) {
107 if (bundleInfo.singleton || !CreateBundleDataDir(bundleInfo, userId)) {
108 continue;
109 }
110 std::string baseDir = baseBundleDataDir + Constants::BASE + bundleInfo.name;
111 if (InstalldClient::GetInstance()->SetDirApl(baseDir, bundleInfo.name,
112 bundleInfo.applicationInfo.appPrivilegeLevel, bundleInfo.isPreInstallApp,
113 bundleInfo.applicationInfo.debug) != ERR_OK) {
114 APP_LOGW("failed to SetDirApl baseDir dir");
115 continue;
116 }
117 std::string baseDataDir = baseBundleDataDir + Constants::DATABASE + bundleInfo.name;
118 if (InstalldClient::GetInstance()->SetDirApl(baseDataDir, bundleInfo.name,
119 bundleInfo.applicationInfo.appPrivilegeLevel, bundleInfo.isPreInstallApp,
120 bundleInfo.applicationInfo.debug) != ERR_OK) {
121 APP_LOGW("failed to SetDirApl baseDataDir dir");
122 }
123 }
124 }
125 } // namespace AppExecFwk
126 } // namespace OHOS