• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "app_control_manager.h"
17 
18 #include "ability_manager_helper.h"
19 #include "account_helper.h"
20 #include "app_control_constants.h"
21 #include "app_control_manager_rdb.h"
22 #include "app_jump_interceptor_manager_rdb.h"
23 #include "app_log_wrapper.h"
24 #include "appexecfwk_errors.h"
25 #include "application_info.h"
26 #include "bundle_constants.h"
27 #include "bundle_info.h"
28 #include "bundle_mgr_service.h"
29 #include "parameters.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34     const std::string APP_MARKET_CALLING = "app market";
35     const std::string INVALID_MESSAGE = "INVALID_MESSAGE";
36 }
37 
AppControlManager()38 AppControlManager::AppControlManager()
39 {
40     appControlManagerDb_ = std::make_shared<AppControlManagerRdb>();
41     bool isAppJumpEnabled = OHOS::system::GetBoolParameter(
42         OHOS::AppExecFwk::PARAMETER_APP_JUMP_INTERCEPTOR_ENABLE, false);
43     APP_LOGE("GetBoolParameter -> isAppJumpEnabled:%{public}s", (isAppJumpEnabled ? "true" : "false"));
44     if (isAppJumpEnabled) {
45         APP_LOGI("App jump intercetor enabled, start init to AppJumpInterceptorManagerRdb");
46         appJumpInterceptorManagerDb_ = std::make_shared<AppJumpInterceptorManagerRdb>();
47         appJumpInterceptorManagerDb_->SubscribeCommonEvent();
48     } else {
49         APP_LOGI("App jump intercetor disabled");
50     }
51 }
52 
~AppControlManager()53 AppControlManager::~AppControlManager()
54 {
55 }
56 
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)57 ErrCode AppControlManager::AddAppInstallControlRule(const std::string &callingName,
58     const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
59 {
60     APP_LOGD("AddAppInstallControlRule");
61     auto ret = appControlManagerDb_->AddAppInstallControlRule(callingName, appIds, controlRuleType, userId);
62     if ((ret == ERR_OK) && !isAppInstallControlEnabled_) {
63         isAppInstallControlEnabled_ = true;
64     }
65     return ret;
66 }
67 
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)68 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
69     const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
70 {
71     APP_LOGD("DeleteAppInstallControlRule");
72     auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, appIds, userId);
73     if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
74         SetAppInstallControlStatus();
75     }
76     return ret;
77 }
78 
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)79 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
80     const std::string &controlRuleType, int32_t userId)
81 {
82     APP_LOGD("CleanInstallControlRule");
83     auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, userId);
84     if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
85         SetAppInstallControlStatus();
86     }
87     return ret;
88 }
89 
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)90 ErrCode AppControlManager::GetAppInstallControlRule(const std::string &callingName,
91     const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
92 {
93     APP_LOGD("GetAppInstallControlRule");
94     return appControlManagerDb_->GetAppInstallControlRule(callingName, controlRuleType, userId, appIds);
95 }
96 
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)97 ErrCode AppControlManager::AddAppRunningControlRule(const std::string &callingName,
98     const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
99 {
100     APP_LOGD("AddAppRunningControlRule");
101     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
102     ErrCode ret = appControlManagerDb_->AddAppRunningControlRule(callingName, controlRules, userId);
103     if (ret == ERR_OK) {
104         for (const auto &rule : controlRules) {
105             std::string key = rule.appId + std::string("_") + std::to_string(userId);
106             auto iter = appRunningControlRuleResult_.find(key);
107             if (iter != appRunningControlRuleResult_.end()) {
108                 appRunningControlRuleResult_.erase(iter);
109             }
110         }
111         KillRunningApp(controlRules, userId);
112     }
113     return ret;
114 }
115 
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)116 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName,
117     const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
118 {
119     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
120     auto ret = appControlManagerDb_->DeleteAppRunningControlRule(callingName, controlRules, userId);
121     if (ret == ERR_OK) {
122         for (const auto &rule : controlRules) {
123             std::string key = rule.appId + std::string("_") + std::to_string(userId);
124             auto iter = appRunningControlRuleResult_.find(key);
125             if (iter != appRunningControlRuleResult_.end()) {
126                 appRunningControlRuleResult_.erase(iter);
127             }
128         }
129     }
130     return ret;
131 }
132 
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)133 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
134 {
135     return appControlManagerDb_->DeleteAppRunningControlRule(callingName, userId);
136 }
137 
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)138 ErrCode AppControlManager::GetAppRunningControlRule(
139     const std::string &callingName, int32_t userId, std::vector<std::string> &appIds)
140 {
141     return appControlManagerDb_->GetAppRunningControlRule(callingName, userId, appIds);
142 }
143 
ConfirmAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId)144 ErrCode AppControlManager::ConfirmAppJumpControlRule(const std::string &callerBundleName,
145     const std::string &targetBundleName, int32_t userId)
146 {
147     if (appJumpInterceptorManagerDb_ == nullptr) {
148         APP_LOGE("DataMgr rdb is not init");
149         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
150     }
151     return appJumpInterceptorManagerDb_->ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
152 }
153 
AddAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)154 ErrCode AppControlManager::AddAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
155 {
156     if (appJumpInterceptorManagerDb_ == nullptr) {
157         APP_LOGE("DataMgr rdb is not init");
158         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
159     }
160     return appJumpInterceptorManagerDb_->AddAppJumpControlRule(controlRules, userId);
161 }
162 
DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)163 ErrCode AppControlManager::DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
164 {
165     if (appJumpInterceptorManagerDb_ == nullptr) {
166         APP_LOGE("DataMgr rdb is not init");
167         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
168     }
169     return appJumpInterceptorManagerDb_->DeleteAppJumpControlRule(controlRules, userId);
170 }
171 
DeleteRuleByCallerBundleName(const std::string & callerBundleName,int32_t userId)172 ErrCode AppControlManager::DeleteRuleByCallerBundleName(const std::string &callerBundleName, int32_t userId)
173 {
174     if (appJumpInterceptorManagerDb_ == nullptr) {
175         APP_LOGE("DataMgr rdb is not init");
176         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
177     }
178     return appJumpInterceptorManagerDb_->DeleteRuleByCallerBundleName(callerBundleName, userId);
179 }
180 
DeleteRuleByTargetBundleName(const std::string & targetBundleName,int32_t userId)181 ErrCode AppControlManager::DeleteRuleByTargetBundleName(const std::string &targetBundleName, int32_t userId)
182 {
183     if (appJumpInterceptorManagerDb_ == nullptr) {
184         APP_LOGE("DataMgr rdb is not init");
185         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
186     }
187     return appJumpInterceptorManagerDb_->DeleteRuleByTargetBundleName(targetBundleName, userId);
188 }
189 
GetAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId,AppJumpControlRule & controlRule)190 ErrCode AppControlManager::GetAppJumpControlRule(const std::string &callerBundleName,
191     const std::string &targetBundleName, int32_t userId, AppJumpControlRule &controlRule)
192 {
193     if (appJumpInterceptorManagerDb_ == nullptr) {
194         APP_LOGE("DataMgr rdb is not init");
195         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
196     }
197     auto ret = appJumpInterceptorManagerDb_->GetAppJumpControlRule(callerBundleName, targetBundleName,
198         userId, controlRule);
199     return ret;
200 }
201 
SetDisposedStatus(const std::string & appId,const Want & want,int32_t userId)202 ErrCode AppControlManager::SetDisposedStatus(const std::string &appId, const Want& want, int32_t userId)
203 {
204     return appControlManagerDb_->SetDisposedStatus(
205         APP_MARKET_CALLING, appId, want, userId);
206 }
207 
DeleteDisposedStatus(const std::string & appId,int32_t userId)208 ErrCode AppControlManager::DeleteDisposedStatus(const std::string &appId, int32_t userId)
209 {
210     return appControlManagerDb_->DeleteDisposedStatus(
211         APP_MARKET_CALLING, appId, userId);
212 }
213 
GetDisposedStatus(const std::string & appId,Want & want,int32_t userId)214 ErrCode AppControlManager::GetDisposedStatus(const std::string &appId, Want& want, int32_t userId)
215 {
216     return appControlManagerDb_->GetDisposedStatus(
217         APP_MARKET_CALLING, appId, want, userId);
218 }
219 
GetAppRunningControlRule(const std::string & bundleName,int32_t userId,AppRunningControlRuleResult & controlRuleResult)220 ErrCode AppControlManager::GetAppRunningControlRule(
221     const std::string &bundleName, int32_t userId, AppRunningControlRuleResult &controlRuleResult)
222 {
223     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
224     if (dataMgr == nullptr) {
225         APP_LOGE("DataMgr is nullptr");
226         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
227     }
228     BundleInfo bundleInfo;
229     ErrCode ret = dataMgr->GetBundleInfoV9(bundleName,
230         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
231     if (ret != ERR_OK) {
232         APP_LOGE("DataMgr GetBundleInfoV9 failed");
233         return ret;
234     }
235     std::string key = bundleInfo.appId + std::string("_") + std::to_string(userId);
236     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
237     if (appRunningControlRuleResult_.find(key) != appRunningControlRuleResult_.end()) {
238         controlRuleResult = appRunningControlRuleResult_[key];
239         if (controlRuleResult.controlMessage == INVALID_MESSAGE) {
240             controlRuleResult.controlMessage = std::string();
241             return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
242         }
243         return ERR_OK;
244     }
245     ret = appControlManagerDb_->GetAppRunningControlRule(bundleInfo.appId, userId, controlRuleResult);
246     if (ret != ERR_OK) {
247         controlRuleResult.controlMessage = INVALID_MESSAGE;
248     }
249     appRunningControlRuleResult_.emplace(key, controlRuleResult);
250     return ret;
251 }
252 
KillRunningApp(const std::vector<AppRunningControlRule> & rules,int32_t userId) const253 void AppControlManager::KillRunningApp(const std::vector<AppRunningControlRule> &rules, int32_t userId) const
254 {
255     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
256     if (dataMgr == nullptr) {
257         APP_LOGE("dataMgr is null");
258         return;
259     }
260     std::for_each(rules.cbegin(), rules.cend(), [&dataMgr, userId](const auto &rule) {
261         std::string bundleName = dataMgr->GetBundleNameByAppId(rule.appId);
262         if (bundleName.empty()) {
263             APP_LOGW("GetBundleNameByAppId failed");
264             return;
265         }
266         BundleInfo bundleInfo;
267         ErrCode ret = dataMgr->GetBundleInfoV9(
268             bundleName, static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
269         if (ret != ERR_OK) {
270             APP_LOGW("GetBundleInfoV9 failed");
271             return;
272         }
273         AbilityManagerHelper::UninstallApplicationProcesses(bundleName, bundleInfo.uid);
274     });
275 }
276 
IsAppInstallControlEnabled() const277 bool AppControlManager::IsAppInstallControlEnabled() const
278 {
279     return isAppInstallControlEnabled_;
280 }
281 
SetAppInstallControlStatus()282 void AppControlManager::SetAppInstallControlStatus()
283 {
284     isAppInstallControlEnabled_ = false;
285     std::vector<std::string> appIds;
286     int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
287     if (currentUserId == Constants::INVALID_USERID) {
288         currentUserId = Constants::START_USERID;
289     }
290     ErrCode ret = appControlManagerDb_->GetAppInstallControlRule(AppControlConstants::EDM_CALLING,
291         AppControlConstants::APP_DISALLOWED_UNINSTALL, currentUserId, appIds);
292     if ((ret == ERR_OK) && !appIds.empty()) {
293         isAppInstallControlEnabled_ = true;
294     }
295 }
296 }
297 }