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 "contacts_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 ContactsFfi {
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 auto ts = static_cast<long long>(time(NULL));
57 HILOG_INFO("contactsCheckPermission,get callerPid = %{public}d,permission = %{public}s,ts = %{public}lld",
58 callerPid, permissionName.c_str(), ts);
59 auto tokenType = AccessTokenKit::GetTokenTypeFlag(callerToken);
60 int result = PermissionState::PERMISSION_DENIED;
61 if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) {
62 result = PermissionState::PERMISSION_GRANTED;
63 } else if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
64 result = AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
65 } else {
66 HILOG_ERROR("ContactsTelephonyPermission check failed");
67 }
68
69 if (permissionName == Permission::READ_CALL_LOG
70 || permissionName == Permission::READ_CONTACTS || permissionName == Permission::WRITE_CONTACTS
71 || permissionName == Permission::OHOS_PERMISSION_MANAGE_VOICEMAIL) {
72 if (tokenType == ATokenTypeEnum::TOKEN_HAP) {
73 bool status = result == PermissionState::PERMISSION_GRANTED;
74 int32_t successCount = status ? 1 : 0;
75 int32_t failCount = status ? 0 : 1;
76 int32_t ret = PrivacyKit::AddPermissionUsedRecord(callerToken, permissionName, successCount, failCount);
77 if (ret != 0) {
78 HILOG_INFO("AddPermissionUsedRecord failed, permissionName = %{public}s, callerPid = %{public}d,"
79 "successCount = %{public}d,failCount = %{public}d,ret = %{public}d", permissionName.c_str(),
80 callerPid, successCount, failCount, ret);
81 }
82 }
83 }
84
85 if (result != PermissionState::PERMISSION_GRANTED) {
86 HILOG_ERROR("ContactsTelephonyPermission check failed");
87 return false;
88 }
89 return true;
90 }
91 } // namespace Telephony
92 } // namespace OHOS
93