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_manager_adapter.h"
17 #include "accesstoken_kit.h"
18 #include "account_adapt.h"
19 #include "dlp_permission_log.h"
20 #include "dlp_permission.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace Security {
26 namespace DlpPermission {
27 using namespace Security::AccessToken;
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION,
30     "BundleManagerAdapter" };
31 }
GetInstance()32 BundleManagerAdapter& BundleManagerAdapter::GetInstance()
33 {
34     static BundleManagerAdapter instance;
35     return instance;
36 }
37 
BundleManagerAdapter()38 BundleManagerAdapter::BundleManagerAdapter() :proxy_(nullptr)
39 {}
40 
~BundleManagerAdapter()41 BundleManagerAdapter::~BundleManagerAdapter()
42 {}
43 
CheckHapPermission(const std::string & bundleName,const std::string & permission)44 bool BundleManagerAdapter::CheckHapPermission(const std::string & bundleName, const std::string & permission)
45 {
46     int32_t userId = -1;
47     if (!GetUserIdByForegroundAccount(&userId)) {
48         DLP_LOG_ERROR(LABEL, "GetUserIdByForegroundAccount error");
49         return false;
50     }
51     AccessTokenID tokenId = AccessToken::AccessTokenKit::GetHapTokenID(userId, bundleName, 0);
52     if (tokenId == 0) {
53         DLP_LOG_ERROR(LABEL, "Get normal tokenId error.");
54         return false;
55     }
56     int res = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, permission);
57     if (res == AccessToken::PermissionState::PERMISSION_GRANTED) {
58         DLP_LOG_INFO(LABEL, "Check permission %{public}s pass", permission.c_str());
59         return true;
60     }
61     DLP_LOG_ERROR(LABEL, "Check permission %{public}s fail", permission.c_str());
62     return false;
63 }
64 
GetBundleInfo(const std::string & bundleName,int32_t flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)65 bool BundleManagerAdapter::GetBundleInfo(const std::string &bundleName, int32_t flag,
66     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
67 {
68     std::lock_guard<std::mutex> lock(proxyMutex_);
69     int32_t result = Connect();
70     if (result != DLP_OK) {
71         DLP_LOG_ERROR(LABEL, "failed to connect bundle manager service.");
72         return false;
73     }
74     return proxy_->GetBundleInfo(bundleName, flag, bundleInfo, userId);
75 }
76 
GetApplicationInfo(const std::string & appName,const int32_t flag,const int32_t userId,AppExecFwk::ApplicationInfo & applicationInfo)77 bool BundleManagerAdapter::GetApplicationInfo(const std::string &appName, const int32_t flag, const  int32_t userId,
78     AppExecFwk::ApplicationInfo &applicationInfo)
79 {
80     std::lock_guard<std::mutex> lock(proxyMutex_);
81     int32_t result = Connect();
82     if (result != DLP_OK) {
83         DLP_LOG_ERROR(LABEL, "failed to connect bundle manager service.");
84         return false;
85     }
86     return proxy_->GetApplicationInfo(appName, flag, userId, applicationInfo);
87 }
88 
GetBundleInfoV9(const std::string & bundleName,AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)89 int32_t BundleManagerAdapter::GetBundleInfoV9(const std::string &bundleName, AppExecFwk::BundleFlag flag,
90     AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
91 {
92     std::lock_guard<std::mutex> lock(proxyMutex_);
93     int32_t result = Connect();
94     if (result != DLP_OK) {
95         DLP_LOG_ERROR(LABEL, "failed to connect bundle manager service.");
96         return false;
97     }
98     return proxy_->GetBundleInfoV9(bundleName, flag, bundleInfo, userId);
99 }
100 
Connect()101 int32_t BundleManagerAdapter::Connect()
102 {
103     if (proxy_ != nullptr) {
104         return DLP_OK;
105     }
106     sptr<ISystemAbilityManager> systemAbilityManager =
107         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108     if (systemAbilityManager == nullptr) {
109         DLP_LOG_ERROR(LABEL, "failed to get system ability manager");
110         return DLP_SERVICE_ERROR_IPC_REQUEST_FAIL;
111     }
112 
113     sptr<IRemoteObject> remoteObj = systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
114     if (remoteObj == nullptr) {
115         DLP_LOG_ERROR(LABEL, "Fail to connect bundle manager service.");
116         return DLP_SERVICE_ERROR_IPC_REQUEST_FAIL;
117     }
118 
119     proxy_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObj);
120     if (proxy_ == nullptr) {
121         DLP_LOG_ERROR(LABEL, "failed to get bundle mgr service remote object");
122         return DLP_SERVICE_ERROR_IPC_REQUEST_FAIL;
123     }
124     return DLP_OK;
125 }
126 } // namespace DlpPermission
127 } // namespace Security
128 } // namespace OHOS