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