• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "telephony_permission.h"
17 
18 #include "bundle_mgr_interface.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "accesstoken_kit.h"
24 #include "privacy_kit.h"
25 
26 #include "telephony_log_wrapper.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 using namespace Security::AccessToken;
31 
32 /**
33  * @brief Get bundleName by callingUid.
34  * @param callingUid.
35  * @param bundleName.
36  * @return Returns true on success, false on failure.
37  */
GetBundleNameByUid(int32_t uid,std::string & bundleName)38 bool TelephonyPermission::GetBundleNameByUid(int32_t uid, std::string &bundleName)
39 {
40     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
41         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
43         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
44 
45     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
46     if (iBundleMgr == nullptr) {
47         TELEPHONY_LOGE(" permission check failed, cannot get IBundleMgr.");
48         return false;
49     }
50     return iBundleMgr->GetBundleNameForUid(uid, bundleName);
51 }
52 
53 /**
54  * @brief Permission check by callingUid.
55  * @param permissionName permission name.
56  * @return Returns true on success, false on failure.
57  */
CheckPermission(const std::string & permissionName)58 bool TelephonyPermission::CheckPermission(const std::string &permissionName)
59 {
60     if (permissionName.empty()) {
61         TELEPHONY_LOGE("permission check failed,permission name is empty.");
62         return false;
63     }
64 
65     auto callerToken = IPCSkeleton::GetCallingTokenID();
66     auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
67     int result = PermissionState::PERMISSION_DENIED;
68     if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) {
69         result = PermissionState::PERMISSION_GRANTED;
70     } else if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
71         result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
72     } else {
73         TELEPHONY_LOGE("permission check failed, callerToken:%{public}u, tokenType:%{public}d",
74             callerToken, tokenType);
75     }
76 
77     if (permissionName == Permission::ANSWER_CALL || permissionName == Permission::READ_CALL_LOG
78         || permissionName == Permission::READ_CONTACTS || permissionName == Permission::WRITE_CONTACTS
79         || permissionName == Permission::SEND_MESSAGES) {
80         if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
81             bool status = result == PermissionState::PERMISSION_GRANTED;
82             int32_t successCount = status ? 1 : 0;
83             int32_t failCount = status ? 0 : 1;
84             int32_t ret = PrivacyKit::AddPermissionUsedRecord(callerToken, permissionName, successCount, failCount);
85             if (ret != 0) {
86                 TELEPHONY_LOGE("AddPermissionUsedRecord failed, permission:%{public}s, "
87                     "successCount:%{public}d, failCount:%{public}d", permissionName.c_str(), successCount, failCount);
88             }
89         }
90     }
91 
92     if (result != PermissionState::PERMISSION_GRANTED) {
93         TELEPHONY_LOGE("permission check failed, permission:%{public}s, callerToken:%{public}u, tokenType:%{public}d",
94             permissionName.c_str(), callerToken, tokenType);
95         return false;
96     }
97     return true;
98 }
99 } // namespace Telephony
100 } // namespace OHOS