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 "accesstoken_kit.h"
19 #include "bundle_mgr_interface.h"
20 #include "hilog_wrapper.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "accesstoken_kit.h"
26 #include "privacy_kit.h"
27 
28 #include "system_ability_definition.h"
29 #include "tokenid_kit.h"
30 
31 namespace OHOS {
32 namespace Telephony {
33 using namespace Security::AccessToken;
34 
35 /**
36  * @brief Permission check by callingUid.
37  * @param permissionName permission name.
38  * @return Returns true on success, false on failure.
39  */
CheckPermission(const std::string &permissionName)40 bool TelephonyPermission::CheckPermission(const std::string &permissionName)
41 {
42     if (permissionName.empty()) {
43         HILOG_ERROR("permission check failed, permission name is empty.");
44         return false;
45     }
46 
47     auto callerToken = IPCSkeleton::GetCallingTokenID();
48     auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
49     int result = PermissionState::PERMISSION_DENIED;
50     if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) {
51         result = PermissionState::PERMISSION_GRANTED;
52     } else if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
53         result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
54     } else {
55         HILOG_ERROR("permission check failed");
56     }
57 
58     if (permissionName == Permission::READ_CALL_LOG
59         || permissionName == Permission::READ_CONTACTS || permissionName == Permission::WRITE_CONTACTS
60         || permissionName == Permission::OHOS_PERMISSION_MANAGE_VOICEMAIL) {
61         if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
62             bool status = result == PermissionState::PERMISSION_GRANTED;
63             int32_t successCount = status ? 1 : 0;
64             int32_t failCount = status ? 0 : 1;
65             int32_t ret = PrivacyKit::AddPermissionUsedRecord(callerToken, permissionName, successCount, failCount);
66             if (ret != 0) {
67                 HILOG_ERROR("AddPermissionUsedRecord failed");
68             }
69         }
70     }
71 
72     if (result != PermissionState::PERMISSION_GRANTED) {
73         HILOG_ERROR("permission check failed");
74         return false;
75     }
76     return true;
77 }
78 } // namespace Telephony
79 } // namespace OHOS