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 "bundle_overlay_manager_host_impl.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "bundle_permission_mgr.h"
21 #include "bundle_overlay_data_manager.h"
22 #include "bundle_overlay_manager.h"
23 #include "bundle_util.h"
24 #include "ipc_skeleton.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
OverlayManagerHostImpl()28 OverlayManagerHostImpl::OverlayManagerHostImpl()
29 {
30 APP_LOGI("create OverlayManagerHostImpl");
31 }
32
~OverlayManagerHostImpl()33 OverlayManagerHostImpl::~OverlayManagerHostImpl()
34 {
35 APP_LOGI("destory OverlayManagerHostImpl");
36 }
37
GetAllOverlayModuleInfo(const std::string & bundleName,std::vector<OverlayModuleInfo> & overlayModuleInfo,int32_t userId)38 ErrCode OverlayManagerHostImpl::GetAllOverlayModuleInfo(const std::string &bundleName,
39 std::vector<OverlayModuleInfo> &overlayModuleInfo, int32_t userId)
40 {
41 APP_LOGD("start to get all overlay moduleInfo of bundle %{public}s", bundleName.c_str());
42 if (bundleName.empty()) {
43 APP_LOGE("invalid param");
44 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
45 }
46 if (userId == Constants::UNSPECIFIED_USERID) {
47 userId = BundleUtil::GetUserIdByCallingUid();
48 }
49 APP_LOGD("calling userId is %{public}d", userId);
50
51 if (IsNativeTokenType() != ERR_OK) {
52 APP_LOGE("non-native token is not allowed to call this function");
53 return ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_INTERNAL_ERROR;
54 }
55 if (!VerifyQueryPermission(bundleName, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
56 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
57 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
58 }
59 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfo, userId);
60 }
61
GetOverlayModuleInfo(const std::string & bundleName,const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)62 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &bundleName, const std::string &moduleName,
63 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
64 {
65 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
66 moduleName.c_str());
67 if (bundleName.empty() || moduleName.empty()) {
68 APP_LOGE("invalid param");
69 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
70 }
71 if (userId == Constants::UNSPECIFIED_USERID) {
72 userId = BundleUtil::GetUserIdByCallingUid();
73 }
74 APP_LOGD("calling userId is %{public}d", userId);
75
76 if (IsNativeTokenType() != ERR_OK) {
77 APP_LOGE("non-native token is not allowed to call this function");
78 return ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_INTERNAL_ERROR;
79 }
80 if (!VerifyQueryPermission(bundleName, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
81 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", bundleName.c_str());
82 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
83 }
84 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
85 userId);
86 }
87
GetOverlayModuleInfo(const std::string & moduleName,OverlayModuleInfo & overlayModuleInfo,int32_t userId)88 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfo(const std::string &moduleName,
89 OverlayModuleInfo &overlayModuleInfo, int32_t userId)
90 {
91 APP_LOGD("start to get overlay moduleInfo of module %{public}s", moduleName.c_str());
92 if (moduleName.empty()) {
93 APP_LOGE("invalid param");
94 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
95 }
96 if (userId == Constants::UNSPECIFIED_USERID) {
97 userId = BundleUtil::GetUserIdByCallingUid();
98 }
99 APP_LOGD("calling userId is %{public}d", userId);
100
101 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
102 if (callingBundleName.empty()) {
103 APP_LOGE("GetCallingBundleName failed");
104 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
105 }
106 APP_LOGD("get overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
107 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(callingBundleName, moduleName, overlayModuleInfo,
108 userId);
109 }
110
GetTargetOverlayModuleInfo(const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)111 ErrCode OverlayManagerHostImpl::GetTargetOverlayModuleInfo(const std::string &targetModuleName,
112 std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
113 {
114 APP_LOGD("start to get target overlay moduleInfo of target module %{public}s", targetModuleName.c_str());
115 if (targetModuleName.empty()) {
116 APP_LOGE("invalid param");
117 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
118 }
119 if (userId == Constants::UNSPECIFIED_USERID) {
120 userId = BundleUtil::GetUserIdByCallingUid();
121 }
122 APP_LOGD("calling userId is %{public}d", userId);
123
124 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
125 if (callingBundleName.empty()) {
126 APP_LOGE("GetCallingBundleName failed");
127 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
128 }
129 APP_LOGD("get target overlay moduleInfo of bundle %{public}s", callingBundleName.c_str());
130 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(callingBundleName, targetModuleName,
131 overlayModuleInfos, userId);
132 }
133
GetOverlayModuleInfoByBundleName(const std::string & bundleName,const std::string & moduleName,std::vector<OverlayModuleInfo> & overlayModuleInfos,int32_t userId)134 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoByBundleName(const std::string &bundleName,
135 const std::string &moduleName, std::vector<OverlayModuleInfo> &overlayModuleInfos, int32_t userId)
136 {
137 APP_LOGD("start to get overlay moduleInfo of bundle %{public}s and module %{public}s", bundleName.c_str(),
138 moduleName.c_str());
139 if (bundleName.empty()) {
140 APP_LOGE("invalid param");
141 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
142 }
143 if (userId == Constants::UNSPECIFIED_USERID) {
144 userId = BundleUtil::GetUserIdByCallingUid();
145 }
146 APP_LOGD("calling userId is %{public}d", userId);
147
148 if (VerifySystemApi() != ERR_OK) {
149 APP_LOGE("non-system app is not allowed to call this function");
150 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
151 }
152 if (!VerifyQueryPermission(bundleName, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
153 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
154 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
155 }
156 if (moduleName.empty()) {
157 APP_LOGD("moduleName is empty, then to query all overlay module info in specified bundle");
158 return BundleOverlayManager::GetInstance()->GetAllOverlayModuleInfo(bundleName, overlayModuleInfos, userId);
159 }
160 OverlayModuleInfo overlayModuleInfo;
161 ErrCode res = BundleOverlayManager::GetInstance()->GetOverlayModuleInfo(bundleName, moduleName, overlayModuleInfo,
162 userId);
163 if (res != ERR_OK) {
164 APP_LOGE("GetOverlayModuleInfo failed due to errcode %{public}d", res);
165 return res;
166 }
167 overlayModuleInfos.emplace_back(overlayModuleInfo);
168 return ERR_OK;
169 }
170
GetOverlayBundleInfoForTarget(const std::string & targetBundleName,std::vector<OverlayBundleInfo> & overlayBundleInfo,int32_t userId)171 ErrCode OverlayManagerHostImpl::GetOverlayBundleInfoForTarget(const std::string &targetBundleName,
172 std::vector<OverlayBundleInfo> &overlayBundleInfo, int32_t userId)
173 {
174 APP_LOGD("start to get target overlay bundleInfo of bundle %{public}s", targetBundleName.c_str());
175 if (targetBundleName.empty()) {
176 APP_LOGE("invalid param");
177 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
178 }
179 if (userId == Constants::UNSPECIFIED_USERID) {
180 userId = BundleUtil::GetUserIdByCallingUid();
181 }
182 APP_LOGD("calling userId is %{public}d", userId);
183
184 if (IsNativeTokenType() != ERR_OK) {
185 APP_LOGE("non-native token is not allowed to call this function");
186 return ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_INTERNAL_ERROR;
187 }
188 if (!VerifyQueryPermission(targetBundleName, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
189 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
190 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
191 }
192 return BundleOverlayManager::GetInstance()->
193 GetOverlayBundleInfoForTarget(targetBundleName, overlayBundleInfo, userId);
194 }
195
GetOverlayModuleInfoForTarget(const std::string & targetBundleName,const std::string & targetModuleName,std::vector<OverlayModuleInfo> & overlayModuleInfo,int32_t userId)196 ErrCode OverlayManagerHostImpl::GetOverlayModuleInfoForTarget(const std::string &targetBundleName,
197 const std::string &targetModuleName, std::vector<OverlayModuleInfo> &overlayModuleInfo, int32_t userId)
198 {
199 APP_LOGD("start to get target overlay moduleInfo of target bundle %{public}s and target module %{public}s",
200 targetBundleName.c_str(), targetModuleName.c_str());
201 if (targetBundleName.empty()) {
202 APP_LOGE("invalid param");
203 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PARAM_ERROR;
204 }
205 if (userId == Constants::UNSPECIFIED_USERID) {
206 userId = BundleUtil::GetUserIdByCallingUid();
207 }
208 APP_LOGD("calling userId is %{public}d", userId);
209
210 if (VerifySystemApi() != ERR_OK) {
211 APP_LOGE("non-system app is not allowed to call this function");
212 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
213 }
214 if (!VerifyQueryPermission(targetBundleName, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
215 APP_LOGE("no permission to query overlay info of targetBundleName %{public}s", targetBundleName.c_str());
216 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
217 }
218 return BundleOverlayManager::GetInstance()->GetOverlayModuleInfoForTarget(targetBundleName, targetModuleName,
219 overlayModuleInfo, userId);
220 }
221
SetOverlayEnabledForSelf(const std::string & moduleName,bool isEnabled,int32_t userId)222 ErrCode OverlayManagerHostImpl::SetOverlayEnabledForSelf(const std::string &moduleName, bool isEnabled,
223 int32_t userId)
224 {
225 APP_LOGD("start to SetOverlayEnabledForSelf");
226 if (moduleName.empty()) {
227 APP_LOGE("invalid param");
228 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
229 }
230 if (userId == Constants::UNSPECIFIED_USERID) {
231 userId = BundleUtil::GetUserIdByCallingUid();
232 }
233
234 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
235 if (callingBundleName.empty()) {
236 APP_LOGE("GetCallingBundleName failed");
237 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
238 }
239 APP_LOGD("set overlay enable %{public}d for bundle %{public}s", isEnabled, callingBundleName.c_str());
240 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(callingBundleName, moduleName, isEnabled, userId);
241 }
242
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)243 ErrCode OverlayManagerHostImpl::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
244 bool isEnabled, int32_t userId)
245 {
246 APP_LOGD("start to SetOverlayEnabled");
247 if (bundleName.empty() || moduleName.empty()) {
248 APP_LOGE("invalid param");
249 return ERR_BUNDLEMANAGER_OVERLAY_SET_OVERLAY_PARAM_ERROR;
250 }
251 if (userId == Constants::UNSPECIFIED_USERID) {
252 userId = BundleUtil::GetUserIdByCallingUid();
253 }
254 APP_LOGD("calling userId is %{public}d", userId);
255
256 if (VerifySystemApi() != ERR_OK) {
257 APP_LOGE("non-system app is not allowed to call this function");
258 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
259 }
260 if (!VerifyQueryPermission(bundleName, Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE)) {
261 APP_LOGE("no permission to query overlay info of bundleName %{public}s", bundleName.c_str());
262 return ERR_BUNDLEMANAGER_OVERLAY_QUERY_FAILED_PERMISSION_DENIED;
263 }
264 return BundleOverlayManager::GetInstance()->SetOverlayEnabled(bundleName, moduleName, isEnabled, userId);
265 }
266
VerifySystemApi()267 ErrCode OverlayManagerHostImpl::VerifySystemApi()
268 {
269 APP_LOGD("begin to verify system app");
270 if (BundlePermissionMgr::VerifySystemApp()) {
271 return ERR_OK;
272 }
273 return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
274 }
275
IsNativeTokenType()276 ErrCode OverlayManagerHostImpl::IsNativeTokenType()
277 {
278 APP_LOGD("begin to verify whether native token");
279 if (BundlePermissionMgr::IsNativeTokenType()) {
280 return ERR_OK;
281 }
282 return ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_INTERNAL_ERROR;
283 }
284
VerifyQueryPermission(const std::string & queryBundleName,const std::string & permission) const285 bool OverlayManagerHostImpl::VerifyQueryPermission(const std::string &queryBundleName,
286 const std::string &permission) const
287 {
288 std::string callingBundleName = OverlayDataMgr::GetInstance()->GetCallingBundleName();
289 if (queryBundleName == callingBundleName) {
290 APP_LOGD("query own info, verify success");
291 return true;
292 }
293 if (!BundlePermissionMgr::VerifyCallingPermission(permission)) {
294 APP_LOGE("verify query permission failed");
295 return false;
296 }
297 APP_LOGD("verify query permission successfully");
298 return true;
299 }
300 } // AppExecFwk
301 } // OHOS
302