1 /*
2 * Copyright (c) 2022 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 "privacy_manager_service.h"
17
18 #include <cstring>
19
20 #include "accesstoken_log.h"
21 #include "constant_common.h"
22 #include "constant.h"
23 #include "ipc_skeleton.h"
24 #include "permission_record_manager.h"
25 #include "string_ex.h"
26
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 namespace {
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
32 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacyManagerService"
33 };
34 }
35
36 const bool REGISTER_RESULT =
37 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<PrivacyManagerService>::GetInstance().get());
38
PrivacyManagerService()39 PrivacyManagerService::PrivacyManagerService()
40 : SystemAbility(SA_ID_PRIVACY_MANAGER_SERVICE, true), state_(ServiceRunningState::STATE_NOT_START)
41 {
42 ACCESSTOKEN_LOG_INFO(LABEL, "PrivacyManagerService()");
43 }
44
~PrivacyManagerService()45 PrivacyManagerService::~PrivacyManagerService()
46 {
47 ACCESSTOKEN_LOG_INFO(LABEL, "~PrivacyManagerService()");
48 }
49
OnStart()50 void PrivacyManagerService::OnStart()
51 {
52 if (state_ == ServiceRunningState::STATE_RUNNING) {
53 ACCESSTOKEN_LOG_INFO(LABEL, "PrivacyManagerService has already started!");
54 return;
55 }
56 if (!Initialize()) {
57 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to initialize");
58 return;
59 }
60 state_ = ServiceRunningState::STATE_RUNNING;
61 bool ret = Publish(DelayedSingleton<PrivacyManagerService>::GetInstance().get());
62 if (!ret) {
63 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to publish service!");
64 return;
65 }
66 ACCESSTOKEN_LOG_INFO(LABEL, "Congratulations, PrivacyManagerService start successfully!");
67 }
68
OnStop()69 void PrivacyManagerService::OnStop()
70 {
71 ACCESSTOKEN_LOG_INFO(LABEL, "stop service");
72 state_ = ServiceRunningState::STATE_NOT_START;
73 }
74
AddPermissionUsedRecord(AccessTokenID tokenId,const std::string & permissionName,int32_t successCount,int32_t failCount)75 int32_t PrivacyManagerService::AddPermissionUsedRecord(
76 AccessTokenID tokenId, const std::string& permissionName, int32_t successCount, int32_t failCount)
77 {
78 return PermissionRecordManager::GetInstance().AddPermissionUsedRecord(
79 tokenId, permissionName, successCount, failCount);
80 }
81
StartUsingPermission(AccessTokenID tokenId,const std::string & permissionName)82 int32_t PrivacyManagerService::StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName)
83 {
84 return PermissionRecordManager::GetInstance().StartUsingPermission(tokenId, permissionName);
85 }
86
StartUsingPermission(AccessTokenID tokenId,const std::string & permissionName,const sptr<IRemoteObject> & callback)87 int32_t PrivacyManagerService::StartUsingPermission(AccessTokenID tokenId, const std::string& permissionName,
88 const sptr<IRemoteObject>& callback)
89 {
90 return PermissionRecordManager::GetInstance().StartUsingPermission(tokenId, permissionName, callback);
91 }
92
StopUsingPermission(AccessTokenID tokenId,const std::string & permissionName)93 int32_t PrivacyManagerService::StopUsingPermission(AccessTokenID tokenId, const std::string& permissionName)
94 {
95 return PermissionRecordManager::GetInstance().StopUsingPermission(tokenId, permissionName);
96 }
97
RemovePermissionUsedRecords(AccessTokenID tokenId,const std::string & deviceID)98 int32_t PrivacyManagerService::RemovePermissionUsedRecords(AccessTokenID tokenId, const std::string& deviceID)
99 {
100 PermissionRecordManager::GetInstance().RemovePermissionUsedRecords(tokenId, deviceID);
101 return Constant::SUCCESS;
102 }
103
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,PermissionUsedResultParcel & result)104 int32_t PrivacyManagerService::GetPermissionUsedRecords(
105 const PermissionUsedRequestParcel& request, PermissionUsedResultParcel& result)
106 {
107 PermissionUsedResult permissionRecord;
108 int32_t ret = PermissionRecordManager::GetInstance().GetPermissionUsedRecords(request.request, permissionRecord);
109 result.result = permissionRecord;
110 return ret;
111 }
112
GetPermissionUsedRecords(const PermissionUsedRequestParcel & request,const sptr<OnPermissionUsedRecordCallback> & callback)113 int32_t PrivacyManagerService::GetPermissionUsedRecords(
114 const PermissionUsedRequestParcel& request, const sptr<OnPermissionUsedRecordCallback>& callback)
115 {
116 return PermissionRecordManager::GetInstance().GetPermissionUsedRecordsAsync(request.request, callback);
117 }
118
RegisterPermActiveStatusCallback(std::vector<std::string> & permList,const sptr<IRemoteObject> & callback)119 int32_t PrivacyManagerService::RegisterPermActiveStatusCallback(
120 std::vector<std::string>& permList, const sptr<IRemoteObject>& callback)
121 {
122 return PermissionRecordManager::GetInstance().RegisterPermActiveStatusCallback(permList, callback);
123 }
124
ResponseDumpCommand(int32_t fd,const std::vector<std::u16string> & args)125 int32_t PrivacyManagerService::ResponseDumpCommand(int32_t fd, const std::vector<std::u16string>& args)
126 {
127 if (args.size() < 2) { // 2 :need two args 0:command 1:tokenId
128 return ERR_INVALID_VALUE;
129 }
130 long long tokenId = atoll(static_cast<const char*>(Str16ToStr8(args.at(1)).c_str()));
131 PermissionUsedRequest request;
132 if (tokenId <= 0) {
133 return ERR_INVALID_VALUE;
134 }
135 request.tokenId = static_cast<uint32_t>(tokenId);
136 request.flag = FLAG_PERMISSION_USAGE_SUMMARY;
137 PermissionUsedResult result;
138 if (PermissionRecordManager::GetInstance().GetPermissionUsedRecords(request, result) != 0) {
139 return ERR_INVALID_VALUE;
140 }
141 std::string infos;
142 if (result.bundleRecords.empty() || result.bundleRecords[0].permissionRecords.empty()) {
143 dprintf(fd, "No Record \n");
144 return ERR_OK;
145 }
146 for (size_t index = 0; index < result.bundleRecords[0].permissionRecords.size(); index++) {
147 infos.append(R"( "permissionRecord": [)");
148 infos.append("\n");
149 infos.append(R"( "bundleName": )" + result.bundleRecords[0].bundleName + ",\n");
150 infos.append(R"( "isRemote": )" + std::to_string(result.bundleRecords[0].isRemote) + ",\n");
151 infos.append(R"( "permissionName": ")" + result.bundleRecords[0].permissionRecords[index].permissionName +
152 R"(")" + ",\n");
153 time_t lastAccessTime = static_cast<time_t>(result.bundleRecords[0].permissionRecords[index].lastAccessTime);
154 infos.append(R"( "lastAccessTime": )" + std::to_string(lastAccessTime) + ",\n");
155 infos.append(R"( "lastAccessDuration": )" +
156 std::to_string(result.bundleRecords[0].permissionRecords[index].lastAccessDuration) + ",\n");
157 infos.append(R"( "accessCount": ")" +
158 std::to_string(result.bundleRecords[0].permissionRecords[index].accessCount) + R"(")" + ",\n");
159 infos.append(" ]");
160 infos.append("\n");
161 }
162 dprintf(fd, "%s\n", infos.c_str());
163 return ERR_OK;
164 }
165
Dump(int32_t fd,const std::vector<std::u16string> & args)166 int32_t PrivacyManagerService::Dump(int32_t fd, const std::vector<std::u16string>& args)
167 {
168 if (fd < 0) {
169 ACCESSTOKEN_LOG_ERROR(LABEL, "Dump fd invalid value");
170 return ERR_INVALID_VALUE;
171 }
172 int32_t ret = ERR_OK;
173 dprintf(fd, "Privacy Dump:\n");
174 if (args.size() == 0) {
175 dprintf(fd, "please use hidumper -s said -a '-h' command help\n");
176 return ret;
177 }
178 std::string arg0 = Str16ToStr8(args.at(0));
179 if (arg0.compare("-h") == 0) {
180 dprintf(fd, "Usage:\n");
181 dprintf(fd, " -h: command help\n");
182 dprintf(fd, " -t <TOKEN_ID>: according to specific token id dump permission used records\n");
183 } else if (arg0.compare("-t") == 0) {
184 ret = PrivacyManagerService::ResponseDumpCommand(fd, args);
185 } else {
186 dprintf(fd, "please use hidumper -s said -a '-h' command help\n");
187 ret = ERR_INVALID_VALUE;
188 }
189 return ret;
190 }
191
UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject> & callback)192 int32_t PrivacyManagerService::UnRegisterPermActiveStatusCallback(const sptr<IRemoteObject>& callback)
193 {
194 return PermissionRecordManager::GetInstance().UnRegisterPermActiveStatusCallback(callback);
195 }
196
IsAllowedUsingPermission(AccessTokenID tokenId,const std::string & permissionName)197 bool PrivacyManagerService::IsAllowedUsingPermission(AccessTokenID tokenId, const std::string& permissionName)
198 {
199 return PermissionRecordManager::GetInstance().IsAllowedUsingPermission(tokenId, permissionName);
200 }
201
Initialize() const202 bool PrivacyManagerService::Initialize() const
203 {
204 PermissionRecordManager::GetInstance().Init();
205 return true;
206 }
207 } // namespace AccessToken
208 } // namespace Security
209 } // namespace OHOS
210