• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef ACCESSTOKEN_TOKEN_INFO_MANAGER_H
17 #define ACCESSTOKEN_TOKEN_INFO_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <vector>
22 
23 #include "access_token.h"
24 #include "hap_token_info.h"
25 #include "hap_token_info_inner.h"
26 #include "native_token_info.h"
27 #include "native_token_info_inner.h"
28 #include "nocopyable.h"
29 #include "rwlock.h"
30 #include "thread_pool.h"
31 
32 namespace OHOS {
33 namespace Security {
34 namespace AccessToken {
35 static const int UDID_MAX_LENGTH = 128; // udid/uuid max length
36 
37 class AccessTokenInfoManager final {
38 public:
39     static AccessTokenInfoManager& GetInstance();
40     ~AccessTokenInfoManager();
41     void Init();
42     std::shared_ptr<HapTokenInfoInner> GetHapTokenInfoInner(AccessTokenID id);
43     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& infoParcel);
44     std::shared_ptr<NativeTokenInfoInner> GetNativeTokenInfoInner(AccessTokenID id);
45     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& infoParcel);
46     int AllocAccessTokenIDEx(const HapInfoParams& info, AccessTokenID tokenId, AccessTokenIDEx& tokenIdEx);
47     std::shared_ptr<PermissionPolicySet> GetNativePermissionPolicySet(AccessTokenID id);
48     std::shared_ptr<PermissionPolicySet> GetHapPermissionPolicySet(AccessTokenID id);
49     int RemoveHapTokenInfo(AccessTokenID id);
50     int RemoveNativeTokenInfo(AccessTokenID id);
51     int CreateHapTokenInfo(const HapInfoParams& info, const HapPolicyParams& policy, AccessTokenIDEx& tokenIdEx);
52     int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap);
53     AccessTokenIDEx GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex);
54     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID);
55     void ProcessNativeTokenInfos(const std::vector<std::shared_ptr<NativeTokenInfoInner>>& tokenInfos);
56     int UpdateHapToken(AccessTokenIDEx& tokenIdEx,
57         bool isSystemApp, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy);
58     void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo);
59     void RefreshTokenInfoIfNeeded();
60     bool IsTokenIdExist(AccessTokenID id);
61     AccessTokenID GetNativeTokenId(const std::string& processName);
62 
63 #ifdef TOKEN_SYNC_ENABLE
64     /* tokensync needed */
65     int GetHapTokenSync(AccessTokenID tokenID, HapTokenInfoForSync& hapSync);
66     int GetHapTokenInfoFromRemote(AccessTokenID tokenID,
67         HapTokenInfoForSync& hapSync);
68     void GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSync>& nativeTokenInfosRes);
69     int SetRemoteHapTokenInfo(const std::string& deviceID, HapTokenInfoForSync& hapSync);
70     int SetRemoteNativeTokenInfo(const std::string& deviceID,
71         std::vector<NativeTokenInfoForSync>& nativeTokenInfoList);
72     bool IsRemoteHapTokenValid(const std::string& deviceID, const HapTokenInfoForSync& hapSync);
73     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID);
74     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID);
75     int DeleteRemoteDeviceTokens(const std::string& deviceID);
76     std::string GetUdidByNodeId(const std::string &nodeId);
77 #endif
78 
79 private:
80     AccessTokenInfoManager();
81     DISALLOW_COPY_AND_MOVE(AccessTokenInfoManager);
82 
83     void InitHapTokenInfos();
84     void InitNativeTokenInfos();
85     int AddHapTokenInfo(const std::shared_ptr<HapTokenInfoInner>& info);
86     int AddNativeTokenInfo(const std::shared_ptr<NativeTokenInfoInner>& info);
87     std::string GetHapUniqueStr(const std::shared_ptr<HapTokenInfoInner>& info) const;
88     std::string GetHapUniqueStr(const int& userID, const std::string& bundleName, const int& instIndex) const;
89     bool TryUpdateExistNativeToken(const std::shared_ptr<NativeTokenInfoInner>& infoPtr);
90     int AllocNativeToken(const std::shared_ptr<NativeTokenInfoInner>& infoPtr);
91     void StoreAllTokenInfo();
92     int CreateRemoteHapTokenInfo(AccessTokenID mapID, HapTokenInfoForSync& hapSync);
93     int UpdateRemoteHapTokenInfo(AccessTokenID mapID, HapTokenInfoForSync& hapSync);
94     void PermissionStateNotify(const std::shared_ptr<HapTokenInfoInner>& info, AccessTokenID id);
95 
96     OHOS::ThreadPool tokenDataWorker_;
97     bool hasInited_;
98 
99     OHOS::Utils::RWLock hapTokenInfoLock_;
100     OHOS::Utils::RWLock nativeTokenInfoLock_;
101     OHOS::Utils::RWLock managerLock_;
102 
103     std::map<int, std::shared_ptr<HapTokenInfoInner>> hapTokenInfoMap_;
104     std::map<std::string, AccessTokenID> hapTokenIdMap_;
105     std::map<int, std::shared_ptr<NativeTokenInfoInner>> nativeTokenInfoMap_;
106     std::map<std::string, AccessTokenID> nativeTokenIdMap_;
107 };
108 } // namespace AccessToken
109 } // namespace Security
110 } // namespace OHOS
111 #endif // ACCESSTOKEN_TOKEN_INFO_MANAGER_H
112