1 /*
2 * Copyright (c) 2021-2024 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 "accesstoken_manager_client.h"
17
18 #include "accesstoken_log.h"
19 #include "access_token_error.h"
20 #include "accesstoken_manager_proxy.h"
21 #include "atm_tools_param_info_parcel.h"
22 #include "hap_token_info.h"
23 #include "hap_token_info_for_sync_parcel.h"
24 #include "iservice_registry.h"
25 #include "parameter.h"
26 #include "permission_grant_info_parcel.h"
27 #include "accesstoken_callbacks.h"
28
29 namespace OHOS {
30 namespace Security {
31 namespace AccessToken {
32 namespace {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
34 LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "AccessTokenManagerClient"
35 };
36 static constexpr int32_t VALUE_MAX_LEN = 32;
37 static const char* ACCESS_TOKEN_SERVICE_INIT_KEY = "accesstoken.permission.init";
38 std::recursive_mutex g_instanceMutex;
39 } // namespace
40 static const uint32_t MAX_CALLBACK_MAP_SIZE = 200;
41
GetInstance()42 AccessTokenManagerClient& AccessTokenManagerClient::GetInstance()
43 {
44 static AccessTokenManagerClient* instance = nullptr;
45 if (instance == nullptr) {
46 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
47 if (instance == nullptr) {
48 AccessTokenManagerClient* tmp = new AccessTokenManagerClient();
49 instance = std::move(tmp);
50 }
51 }
52 return *instance;
53 }
54
AccessTokenManagerClient()55 AccessTokenManagerClient::AccessTokenManagerClient()
56 {}
57
~AccessTokenManagerClient()58 AccessTokenManagerClient::~AccessTokenManagerClient()
59 {
60 ACCESSTOKEN_LOG_ERROR(LABEL, "~AccessTokenManagerClient");
61 std::lock_guard<std::mutex> lock(proxyMutex_);
62 ReleaseProxy();
63 }
64
GetPermissionUsedType( AccessTokenID tokenID, const std::string &permissionName)65 PermUsedTypeEnum AccessTokenManagerClient::GetPermissionUsedType(
66 AccessTokenID tokenID, const std::string &permissionName)
67 {
68 auto proxy = GetProxy();
69 if (proxy == nullptr) {
70 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
71 return PermUsedTypeEnum::INVALID_USED_TYPE;
72 }
73 return proxy->GetPermissionUsedType(tokenID, permissionName);
74 }
75
VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)76 int AccessTokenManagerClient::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)
77 {
78 auto proxy = GetProxy();
79 if (proxy != nullptr) {
80 return proxy->VerifyAccessToken(tokenID, permissionName);
81 }
82 char value[VALUE_MAX_LEN] = {0};
83 int32_t ret = GetParameter(ACCESS_TOKEN_SERVICE_INIT_KEY, "", value, VALUE_MAX_LEN - 1);
84 if ((ret < 0) || (static_cast<uint64_t>(std::atoll(value)) != 0)) {
85 ACCESSTOKEN_LOG_ERROR(LABEL, "At service has been started, ret=%{public}d.", ret);
86 return PERMISSION_DENIED;
87 }
88 AccessTokenIDInner *idInner = reinterpret_cast<AccessTokenIDInner *>(&tokenID);
89 if (static_cast<ATokenTypeEnum>(idInner->type) == TOKEN_NATIVE) {
90 ACCESSTOKEN_LOG_INFO(LABEL, "At service has not been started.");
91 return PERMISSION_GRANTED;
92 }
93 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
94 return PERMISSION_DENIED;
95 }
96
GetDefPermission( const std::string& permissionName, PermissionDef& permissionDefResult)97 int AccessTokenManagerClient::GetDefPermission(
98 const std::string& permissionName, PermissionDef& permissionDefResult)
99 {
100 auto proxy = GetProxy();
101 if (proxy == nullptr) {
102 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
103 return AccessTokenError::ERR_SERVICE_ABNORMAL;
104 }
105 PermissionDefParcel permissionDefParcel;
106 int result = proxy->GetDefPermission(permissionName, permissionDefParcel);
107 permissionDefResult = permissionDefParcel.permissionDef;
108 return result;
109 }
110
GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList)111 int AccessTokenManagerClient::GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList)
112 {
113 auto proxy = GetProxy();
114 if (proxy == nullptr) {
115 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
116 return AccessTokenError::ERR_SERVICE_ABNORMAL;
117 }
118 std::vector<PermissionDefParcel> parcelList;
119 int result = proxy->GetDefPermissions(tokenID, parcelList);
120 for (const auto& permParcel : parcelList) {
121 PermissionDef perm = permParcel.permissionDef;
122 permList.emplace_back(perm);
123 }
124 return result;
125 }
126
GetReqPermissions( AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant)127 int AccessTokenManagerClient::GetReqPermissions(
128 AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant)
129 {
130 auto proxy = GetProxy();
131 if (proxy == nullptr) {
132 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
133 return AccessTokenError::ERR_SERVICE_ABNORMAL;
134 }
135 std::vector<PermissionStateFullParcel> parcelList;
136 int result = proxy->GetReqPermissions(tokenID, parcelList, isSystemGrant);
137 for (const auto& permParcel : parcelList) {
138 PermissionStateFull perm = permParcel.permStatFull;
139 reqPermList.emplace_back(perm);
140 }
141 return result;
142 }
143
GetPermissionFlag( AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag)144 int AccessTokenManagerClient::GetPermissionFlag(
145 AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag)
146 {
147 auto proxy = GetProxy();
148 if (proxy == nullptr) {
149 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
150 return AccessTokenError::ERR_SERVICE_ABNORMAL;
151 }
152 return proxy->GetPermissionFlag(tokenID, permissionName, flag);
153 }
154
GetSelfPermissionsState(std::vector<PermissionListState>& permList, PermissionGrantInfo& info)155 PermissionOper AccessTokenManagerClient::GetSelfPermissionsState(std::vector<PermissionListState>& permList,
156 PermissionGrantInfo& info)
157 {
158 auto proxy = GetProxy();
159 if (proxy == nullptr) {
160 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
161 return INVALID_OPER;
162 }
163
164 size_t len = permList.size();
165 if (len == 0) {
166 ACCESSTOKEN_LOG_DEBUG(LABEL, "Len is zero.");
167 return PASS_OPER;
168 }
169
170 std::vector<PermissionListStateParcel> parcelList;
171
172 for (const auto& perm : permList) {
173 PermissionListStateParcel permParcel;
174 permParcel.permsState = perm;
175 parcelList.emplace_back(permParcel);
176 }
177 PermissionGrantInfoParcel infoParcel;
178 PermissionOper result = proxy->GetSelfPermissionsState(parcelList, infoParcel);
179
180 for (uint32_t i = 0; i < len; i++) {
181 PermissionListState perm = parcelList[i].permsState;
182 permList[i].state = perm.state;
183 }
184
185 info = infoParcel.info;
186 return result;
187 }
188
GetPermissionsStatus( AccessTokenID tokenID, std::vector<PermissionListState>& permList)189 int32_t AccessTokenManagerClient::GetPermissionsStatus(
190 AccessTokenID tokenID, std::vector<PermissionListState>& permList)
191 {
192 auto proxy = GetProxy();
193 if (proxy == nullptr) {
194 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
195 return AccessTokenError::ERR_SERVICE_ABNORMAL;
196 }
197
198 size_t len = permList.size();
199 if (len == 0) {
200 ACCESSTOKEN_LOG_ERROR(LABEL, "Len is zero.");
201 return AccessTokenError::ERR_PARAM_INVALID;
202 }
203
204 std::vector<PermissionListStateParcel> parcelList;
205
206 for (const auto& perm : permList) {
207 PermissionListStateParcel permParcel;
208 permParcel.permsState = perm;
209 parcelList.emplace_back(permParcel);
210 }
211 int32_t result = proxy->GetPermissionsStatus(tokenID, parcelList);
212 if (result != RET_SUCCESS) {
213 return result;
214 }
215 for (uint32_t i = 0; i < len; i++) {
216 PermissionListState perm = parcelList[i].permsState;
217 permList[i].state = perm.state;
218 }
219
220 return result;
221 }
222
GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag)223 int AccessTokenManagerClient::GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag)
224 {
225 auto proxy = GetProxy();
226 if (proxy == nullptr) {
227 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
228 return AccessTokenError::ERR_SERVICE_ABNORMAL;
229 }
230 return proxy->GrantPermission(tokenID, permissionName, flag);
231 }
232
RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag)233 int AccessTokenManagerClient::RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag)
234 {
235 auto proxy = GetProxy();
236 if (proxy == nullptr) {
237 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
238 return AccessTokenError::ERR_SERVICE_ABNORMAL;
239 }
240 return proxy->RevokePermission(tokenID, permissionName, flag);
241 }
242
GrantPermissionForSpecifiedTime( AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime)243 int AccessTokenManagerClient::GrantPermissionForSpecifiedTime(
244 AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime)
245 {
246 auto proxy = GetProxy();
247 if (proxy == nullptr) {
248 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
249 return AccessTokenError::ERR_SERVICE_ABNORMAL;
250 }
251 return proxy->GrantPermissionForSpecifiedTime(tokenID, permissionName, onceTime);
252 }
253
ClearUserGrantedPermissionState(AccessTokenID tokenID)254 int AccessTokenManagerClient::ClearUserGrantedPermissionState(AccessTokenID tokenID)
255 {
256 auto proxy = GetProxy();
257 if (proxy == nullptr) {
258 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
259 return AccessTokenError::ERR_SERVICE_ABNORMAL;
260 }
261 return proxy->ClearUserGrantedPermissionState(tokenID);
262 }
263
SetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t status, int32_t userID = 0)264 int32_t AccessTokenManagerClient::SetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t status,
265 int32_t userID = 0)
266 {
267 auto proxy = GetProxy();
268 if (proxy == nullptr) {
269 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
270 return AccessTokenError::ERR_SERVICE_ABNORMAL;
271 }
272 return proxy->SetPermissionRequestToggleStatus(permissionName, status, userID);
273 }
274
GetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t& status, int32_t userID = 0)275 int32_t AccessTokenManagerClient::GetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t& status,
276 int32_t userID = 0)
277 {
278 auto proxy = GetProxy();
279 if (proxy == nullptr) {
280 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
281 return AccessTokenError::ERR_SERVICE_ABNORMAL;
282 }
283 return proxy->GetPermissionRequestToggleStatus(permissionName, status, userID);
284 }
285
CreatePermStateChangeCallback( const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb, sptr<PermissionStateChangeCallback>& callback)286 int32_t AccessTokenManagerClient::CreatePermStateChangeCallback(
287 const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb,
288 sptr<PermissionStateChangeCallback>& callback)
289 {
290 std::lock_guard<std::mutex> lock(callbackMutex_);
291 if (callbackMap_.size() == MAX_CALLBACK_MAP_SIZE) {
292 ACCESSTOKEN_LOG_ERROR(LABEL, "The maximum number of callback has been reached");
293 return AccessTokenError::ERR_CALLBACKS_EXCEED_LIMITATION;
294 }
295
296 auto goalCallback = callbackMap_.find(customizedCb);
297 if (goalCallback != callbackMap_.end()) {
298 ACCESSTOKEN_LOG_ERROR(LABEL, "Already has the same callback");
299 return AccessTokenError::ERR_CALLBACK_ALREADY_EXIST;
300 } else {
301 callback = new (std::nothrow) PermissionStateChangeCallback(customizedCb);
302 if (!callback) {
303 ACCESSTOKEN_LOG_ERROR(LABEL, "Memory allocation for callback failed!");
304 return AccessTokenError::ERR_SERVICE_ABNORMAL;
305 }
306 }
307 return RET_SUCCESS;
308 }
309
RegisterPermStateChangeCallback( const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)310 int32_t AccessTokenManagerClient::RegisterPermStateChangeCallback(
311 const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)
312 {
313 if (customizedCb == nullptr) {
314 ACCESSTOKEN_LOG_ERROR(LABEL, "CustomizedCb is nullptr");
315 return AccessTokenError::ERR_PARAM_INVALID;
316 }
317
318 sptr<PermissionStateChangeCallback> callback = nullptr;
319 int32_t result = CreatePermStateChangeCallback(customizedCb, callback);
320 if (result != RET_SUCCESS) {
321 return result;
322 }
323 auto proxy = GetProxy();
324 if (proxy == nullptr) {
325 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
326 return AccessTokenError::ERR_SERVICE_ABNORMAL;
327 }
328
329 PermStateChangeScopeParcel scopeParcel;
330 customizedCb->GetScope(scopeParcel.scope);
331
332 if (scopeParcel.scope.permList.size() > PERMS_LIST_SIZE_MAX ||
333 scopeParcel.scope.tokenIDs.size() > TOKENIDS_LIST_SIZE_MAX) {
334 ACCESSTOKEN_LOG_ERROR(LABEL, "Scope oversize");
335 return AccessTokenError::ERR_PARAM_INVALID;
336 }
337 result = proxy->RegisterPermStateChangeCallback(scopeParcel, callback->AsObject());
338 if (result == RET_SUCCESS) {
339 std::lock_guard<std::mutex> lock(callbackMutex_);
340 callbackMap_[customizedCb] = callback;
341 }
342 return result;
343 }
344
UnRegisterPermStateChangeCallback( const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)345 int32_t AccessTokenManagerClient::UnRegisterPermStateChangeCallback(
346 const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb)
347 {
348 auto proxy = GetProxy();
349 if (proxy == nullptr) {
350 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
351 return AccessTokenError::ERR_SERVICE_ABNORMAL;
352 }
353
354 std::lock_guard<std::mutex> lock(callbackMutex_);
355 auto goalCallback = callbackMap_.find(customizedCb);
356 if (goalCallback == callbackMap_.end()) {
357 ACCESSTOKEN_LOG_ERROR(LABEL, "GoalCallback already is not exist");
358 return AccessTokenError::ERR_INTERFACE_NOT_USED_TOGETHER;
359 }
360
361 int32_t result = proxy->UnRegisterPermStateChangeCallback(goalCallback->second->AsObject());
362 if (result == RET_SUCCESS) {
363 callbackMap_.erase(goalCallback);
364 }
365 return result;
366 }
367
AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy)368 AccessTokenIDEx AccessTokenManagerClient::AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy)
369 {
370 AccessTokenIDEx tokenIdEx = { 0 };
371 auto proxy = GetProxy();
372 if (proxy == nullptr) {
373 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
374 return tokenIdEx;
375 }
376 HapInfoParcel hapInfoParcel;
377 HapPolicyParcel hapPolicyParcel;
378 hapInfoParcel.hapInfoParameter = info;
379 hapPolicyParcel.hapPolicyParameter = policy;
380
381 return proxy->AllocHapToken(hapInfoParcel, hapPolicyParcel);
382 }
383
InitHapToken(const HapInfoParams& info, HapPolicyParams& policy, AccessTokenIDEx& fullTokenId)384 int32_t AccessTokenManagerClient::InitHapToken(const HapInfoParams& info, HapPolicyParams& policy,
385 AccessTokenIDEx& fullTokenId)
386 {
387 auto proxy = GetProxy();
388 if (proxy == nullptr) {
389 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
390 return AccessTokenError::ERR_SERVICE_ABNORMAL;
391 }
392 HapInfoParcel hapInfoParcel;
393 HapPolicyParcel hapPolicyParcel;
394 hapInfoParcel.hapInfoParameter = info;
395 hapPolicyParcel.hapPolicyParameter = policy;
396
397 return proxy->InitHapToken(hapInfoParcel, hapPolicyParcel, fullTokenId);
398 }
399
DeleteToken(AccessTokenID tokenID)400 int AccessTokenManagerClient::DeleteToken(AccessTokenID tokenID)
401 {
402 auto proxy = GetProxy();
403 if (proxy == nullptr) {
404 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
405 return AccessTokenError::ERR_SERVICE_ABNORMAL;
406 }
407 return proxy->DeleteToken(tokenID);
408 }
409
GetTokenType(AccessTokenID tokenID)410 ATokenTypeEnum AccessTokenManagerClient::GetTokenType(AccessTokenID tokenID)
411 {
412 auto proxy = GetProxy();
413 if (proxy == nullptr) {
414 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
415 return TOKEN_INVALID;
416 }
417 return static_cast<ATokenTypeEnum>(proxy->GetTokenType(tokenID));
418 }
419
GetHapTokenID( int32_t userID, const std::string& bundleName, int32_t instIndex)420 AccessTokenIDEx AccessTokenManagerClient::GetHapTokenID(
421 int32_t userID, const std::string& bundleName, int32_t instIndex)
422 {
423 AccessTokenIDEx result = {0};
424 auto proxy = GetProxy();
425 if (proxy == nullptr) {
426 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
427 return result;
428 }
429 return proxy->GetHapTokenID(userID, bundleName, instIndex);
430 }
431
AllocLocalTokenID( const std::string& remoteDeviceID, AccessTokenID remoteTokenID)432 AccessTokenID AccessTokenManagerClient::AllocLocalTokenID(
433 const std::string& remoteDeviceID, AccessTokenID remoteTokenID)
434 {
435 auto proxy = GetProxy();
436 if (proxy == nullptr) {
437 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
438 return INVALID_TOKENID;
439 }
440 return proxy->AllocLocalTokenID(remoteDeviceID, remoteTokenID);
441 }
442
UpdateHapToken( AccessTokenIDEx& tokenIdEx, const UpdateHapInfoParams& info, const HapPolicyParams& policy)443 int32_t AccessTokenManagerClient::UpdateHapToken(
444 AccessTokenIDEx& tokenIdEx, const UpdateHapInfoParams& info, const HapPolicyParams& policy)
445 {
446 auto proxy = GetProxy();
447 if (proxy == nullptr) {
448 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
449 return AccessTokenError::ERR_SERVICE_ABNORMAL;
450 }
451 HapPolicyParcel hapPolicyParcel;
452 hapPolicyParcel.hapPolicyParameter = policy;
453 return proxy->UpdateHapToken(tokenIdEx, info, hapPolicyParcel);
454 }
455
GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes)456 int AccessTokenManagerClient::GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes)
457 {
458 auto proxy = GetProxy();
459 if (proxy == nullptr) {
460 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
461 return AccessTokenError::ERR_SERVICE_ABNORMAL;
462 }
463 HapTokenInfoParcel hapTokenInfoParcel;
464 int res = proxy->GetHapTokenInfo(tokenID, hapTokenInfoParcel);
465
466 hapTokenInfoRes = hapTokenInfoParcel.hapTokenInfoParams;
467 return res;
468 }
469
GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes)470 int AccessTokenManagerClient::GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes)
471 {
472 auto proxy = GetProxy();
473 if (proxy == nullptr) {
474 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
475 return AccessTokenError::ERR_SERVICE_ABNORMAL;
476 }
477 NativeTokenInfoParcel nativeTokenInfoParcel;
478 int res = proxy->GetNativeTokenInfo(tokenID, nativeTokenInfoParcel);
479 nativeTokenInfoRes = nativeTokenInfoParcel.nativeTokenInfoParams;
480 return res;
481 }
482
483 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
ReloadNativeTokenInfo()484 int32_t AccessTokenManagerClient::ReloadNativeTokenInfo()
485 {
486 auto proxy = GetProxy();
487 if (proxy == nullptr) {
488 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
489 return AccessTokenError::ERR_SERVICE_ABNORMAL;
490 }
491 return proxy->ReloadNativeTokenInfo();
492 }
493 #endif
494
GetNativeTokenId(const std::string& processName)495 AccessTokenID AccessTokenManagerClient::GetNativeTokenId(const std::string& processName)
496 {
497 auto proxy = GetProxy();
498 if (proxy == nullptr) {
499 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
500 return INVALID_TOKENID;
501 }
502 return proxy->GetNativeTokenId(processName);
503 }
504
505 #ifdef TOKEN_SYNC_ENABLE
GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync)506 int AccessTokenManagerClient::GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync)
507 {
508 auto proxy = GetProxy();
509 if (proxy == nullptr) {
510 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
511 return AccessTokenError::ERR_SERVICE_ABNORMAL;
512 }
513
514 HapTokenInfoForSyncParcel hapSyncParcel;
515 int res = proxy->GetHapTokenInfoFromRemote(tokenID, hapSyncParcel);
516 hapSync = hapSyncParcel.hapTokenInfoForSyncParams;
517 return res;
518 }
519
SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync)520 int AccessTokenManagerClient::SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync)
521 {
522 auto proxy = GetProxy();
523 if (proxy == nullptr) {
524 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
525 return AccessTokenError::ERR_SERVICE_ABNORMAL;
526 }
527
528 HapTokenInfoForSyncParcel hapSyncParcel;
529 hapSyncParcel.hapTokenInfoForSyncParams = hapSync;
530
531 int res = proxy->SetRemoteHapTokenInfo(deviceID, hapSyncParcel);
532 return res;
533 }
534
DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID)535 int AccessTokenManagerClient::DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID)
536 {
537 auto proxy = GetProxy();
538 if (proxy == nullptr) {
539 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
540 return AccessTokenError::ERR_SERVICE_ABNORMAL;
541 }
542
543 int res = proxy->DeleteRemoteToken(deviceID, tokenID);
544 return res;
545 }
546
GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID)547 AccessTokenID AccessTokenManagerClient::GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID)
548 {
549 auto proxy = GetProxy();
550 if (proxy == nullptr) {
551 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
552 return INVALID_TOKENID;
553 }
554
555 AccessTokenID res = proxy->GetRemoteNativeTokenID(deviceID, tokenID);
556 return res;
557 }
558
DeleteRemoteDeviceTokens(const std::string& deviceID)559 int AccessTokenManagerClient::DeleteRemoteDeviceTokens(const std::string& deviceID)
560 {
561 auto proxy = GetProxy();
562 if (proxy == nullptr) {
563 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
564 return AccessTokenError::ERR_SERVICE_ABNORMAL;
565 }
566
567 int res = proxy->DeleteRemoteDeviceTokens(deviceID);
568 return res;
569 }
570
RegisterTokenSyncCallback( const std::shared_ptr<TokenSyncKitInterface>& syncCallback)571 int32_t AccessTokenManagerClient::RegisterTokenSyncCallback(
572 const std::shared_ptr<TokenSyncKitInterface>& syncCallback)
573 {
574 auto proxy = GetProxy();
575 if (proxy == nullptr) {
576 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
577 return AccessTokenError::ERR_SERVICE_ABNORMAL;
578 }
579
580 if (syncCallback == nullptr) {
581 ACCESSTOKEN_LOG_ERROR(LABEL, "Input callback is null.");
582 return AccessTokenError::ERR_PARAM_INVALID;
583 }
584
585 sptr<TokenSyncCallback> callback = sptr<TokenSyncCallback>(new TokenSyncCallback(syncCallback));
586
587 std::lock_guard<std::mutex> lock(tokenSyncCallbackMutex_);
588 int32_t res = proxy->RegisterTokenSyncCallback(callback->AsObject());
589 if (res == RET_SUCCESS) {
590 tokenSyncCallback_ = callback;
591 syncCallbackImpl_ = syncCallback;
592 }
593 return res;
594 }
595
UnRegisterTokenSyncCallback()596 int32_t AccessTokenManagerClient::UnRegisterTokenSyncCallback()
597 {
598 auto proxy = GetProxy();
599 if (proxy == nullptr) {
600 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
601 return AccessTokenError::ERR_SERVICE_ABNORMAL;
602 }
603 std::lock_guard<std::mutex> lock(tokenSyncCallbackMutex_);
604 int32_t res = proxy->UnRegisterTokenSyncCallback();
605 if (res == RET_SUCCESS) {
606 tokenSyncCallback_ = nullptr;
607 syncCallbackImpl_ = nullptr;
608 }
609 return res;
610 }
611 #endif
612
DumpTokenInfo(const AtmToolsParamInfo& info, std::string& dumpInfo)613 void AccessTokenManagerClient::DumpTokenInfo(const AtmToolsParamInfo& info, std::string& dumpInfo)
614 {
615 auto proxy = GetProxy();
616 if (proxy == nullptr) {
617 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
618 return;
619 }
620
621 AtmToolsParamInfoParcel infoParcel;
622 infoParcel.info = info;
623 proxy->DumpTokenInfo(infoParcel, dumpInfo);
624 }
625
GetVersion(uint32_t& version)626 int32_t AccessTokenManagerClient::GetVersion(uint32_t& version)
627 {
628 auto proxy = GetProxy();
629 if (proxy == nullptr) {
630 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null.");
631 return AccessTokenError::ERR_SERVICE_ABNORMAL;
632 }
633
634 return proxy->GetVersion(version);
635 }
636
InitProxy()637 void AccessTokenManagerClient::InitProxy()
638 {
639 if (proxy_ == nullptr) {
640 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
641 if (sam == nullptr) {
642 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
643 return;
644 }
645 sptr<IRemoteObject> accesstokenSa =
646 sam->GetSystemAbility(IAccessTokenManager::SA_ID_ACCESSTOKEN_MANAGER_SERVICE);
647 if (accesstokenSa == nullptr) {
648 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
649 IAccessTokenManager::SA_ID_ACCESSTOKEN_MANAGER_SERVICE);
650 return;
651 }
652
653 serviceDeathObserver_ = sptr<AccessTokenDeathRecipient>::MakeSptr();
654 if (serviceDeathObserver_ != nullptr) {
655 accesstokenSa->AddDeathRecipient(serviceDeathObserver_);
656 }
657 proxy_ = new AccessTokenManagerProxy(accesstokenSa);
658 if (proxy_ == nullptr) {
659 ACCESSTOKEN_LOG_ERROR(LABEL, "Iface_cast get null");
660 }
661 }
662 }
663
OnRemoteDiedHandle()664 void AccessTokenManagerClient::OnRemoteDiedHandle()
665 {
666 {
667 std::lock_guard<std::mutex> lock(proxyMutex_);
668 ReleaseProxy();
669 InitProxy();
670 }
671
672 #ifdef TOKEN_SYNC_ENABLE
673 if (syncCallbackImpl_ != nullptr) {
674 RegisterTokenSyncCallback(syncCallbackImpl_); // re-register callback when AT crashes
675 }
676 #endif // TOKEN_SYNC_ENABLE
677 }
678
GetProxy()679 sptr<IAccessTokenManager> AccessTokenManagerClient::GetProxy()
680 {
681 std::lock_guard<std::mutex> lock(proxyMutex_);
682 if (proxy_ == nullptr) {
683 InitProxy();
684 }
685 return proxy_;
686 }
687
SetPermDialogCap(const HapBaseInfo& hapBaseInfo, bool enable)688 int32_t AccessTokenManagerClient::SetPermDialogCap(const HapBaseInfo& hapBaseInfo, bool enable)
689 {
690 auto proxy = GetProxy();
691 if (proxy == nullptr) {
692 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
693 return AccessTokenError::ERR_SERVICE_ABNORMAL;
694 }
695 HapBaseInfoParcel hapBaseInfoParcel;
696 hapBaseInfoParcel.hapBaseInfo = hapBaseInfo;
697 return proxy->SetPermDialogCap(hapBaseInfoParcel, enable);
698 }
699
GetPermissionManagerInfo(PermissionGrantInfo& info)700 void AccessTokenManagerClient::GetPermissionManagerInfo(PermissionGrantInfo& info)
701 {
702 auto proxy = GetProxy();
703 if (proxy == nullptr) {
704 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
705 return;
706 }
707 PermissionGrantInfoParcel infoParcel;
708 proxy->GetPermissionManagerInfo(infoParcel);
709 info = infoParcel.info;
710 }
711
InitUserPolicy( const std::vector<UserState>& userList, const std::vector<std::string>& permList)712 int32_t AccessTokenManagerClient::InitUserPolicy(
713 const std::vector<UserState>& userList, const std::vector<std::string>& permList)
714 {
715 auto proxy = GetProxy();
716 if (proxy == nullptr) {
717 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
718 return AccessTokenError::ERR_SERVICE_ABNORMAL;
719 }
720 return proxy->InitUserPolicy(userList, permList);
721 }
722
ClearUserPolicy()723 int32_t AccessTokenManagerClient::ClearUserPolicy()
724 {
725 auto proxy = GetProxy();
726 if (proxy == nullptr) {
727 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
728 return AccessTokenError::ERR_SERVICE_ABNORMAL;
729 }
730 return proxy->ClearUserPolicy();
731 }
732
UpdateUserPolicy(const std::vector<UserState>& userList)733 int32_t AccessTokenManagerClient::UpdateUserPolicy(const std::vector<UserState>& userList)
734 {
735 auto proxy = GetProxy();
736 if (proxy == nullptr) {
737 ACCESSTOKEN_LOG_ERROR(LABEL, "Proxy is null");
738 return AccessTokenError::ERR_SERVICE_ABNORMAL;
739 }
740 return proxy->UpdateUserPolicy(userList);
741 }
742
ReleaseProxy()743 void AccessTokenManagerClient::ReleaseProxy()
744 {
745 if (proxy_ != nullptr && serviceDeathObserver_ != nullptr) {
746 proxy_->AsObject()->RemoveDeathRecipient(serviceDeathObserver_);
747 }
748 proxy_ = nullptr;
749 serviceDeathObserver_ = nullptr;
750 }
751 } // namespace AccessToken
752 } // namespace Security
753 } // namespace OHOS
754