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 "permission/permission.h"
23 #include "permission/permission_kit.h"
24 #include "system_ability_definition.h"
25 #include "accesstoken_kit.h"
26
27 #include "telephony_log_wrapper.h"
28
29 namespace OHOS {
30 namespace Telephony {
31 /**
32 * @brief Get bundleName by callingUid.
33 * @param callingUid.
34 * @param bundleName.
35 * @return Returns true on success, false on failure.
36 */
GetBundleNameByUid(int32_t uid,std::string & bundleName)37 bool TelephonyPermission::GetBundleNameByUid(int32_t uid, std::string &bundleName)
38 {
39 OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
40 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 OHOS::sptr<OHOS::IRemoteObject> remoteObject =
42 systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
43
44 sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
45 if (iBundleMgr == nullptr) {
46 TELEPHONY_LOGE(" permission check failed, cannot get IBundleMgr.");
47 return false;
48 }
49 return iBundleMgr->GetBundleNameForUid(uid, bundleName);
50 }
51
52 /**
53 * @brief Permission check by callingUid.
54 * @param permissionName permission name.
55 * @return Returns true on success, false on failure.
56 */
CheckPermission(const std::string & permissionName)57 bool TelephonyPermission::CheckPermission(const std::string &permissionName)
58 {
59 if (permissionName.empty()) {
60 TELEPHONY_LOGE("permission check failed,permission name is empty.");
61 return false;
62 }
63
64 auto callerToken = IPCSkeleton::GetCallingTokenID();
65 auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
66 int result = Security::AccessToken::PERMISSION_DENIED;
67
68 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
69 result = Security::AccessToken::AccessTokenKit::VerifyNativeToken(callerToken, permissionName);
70 } else if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
71 result = Security::AccessToken::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 (result != Security::AccessToken::PERMISSION_GRANTED) {
78 TELEPHONY_LOGE("permission check failed, permission:%{public}s, callerToken:%{public}u, tokenType:%{public}d",
79 permissionName.c_str(), callerToken, tokenType);
80 return false;
81 }
82 return true;
83 }
84 } // namespace Telephony
85 } // namespace OHOS