• 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 PERMISSION_MANAGER_H
17 #define PERMISSION_MANAGER_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <string>
22 
23 #include "access_token.h"
24 #include "hap_token_info_inner.h"
25 #include "iremote_broker.h"
26 #include "permission_def.h"
27 #include "permission_grant_event.h"
28 #include "permission_list_state.h"
29 #include "permission_list_state_parcel.h"
30 #include "permission_state_change_info.h"
31 #include "permission_state_full.h"
32 #include "temp_permission_observer.h"
33 
34 #include "rwlock.h"
35 #include "nocopyable.h"
36 
37 namespace OHOS {
38 namespace Security {
39 namespace AccessToken {
40 constexpr const char* VAGUE_LOCATION_PERMISSION_NAME = "ohos.permission.APPROXIMATELY_LOCATION";
41 constexpr const char* ACCURATE_LOCATION_PERMISSION_NAME = "ohos.permission.LOCATION";
42 constexpr const char* BACKGROUND_LOCATION_PERMISSION_NAME = "ohos.permission.LOCATION_IN_BACKGROUND";
43 const int32_t ACCURATE_LOCATION_API_VERSION = 9;
44 const int32_t BACKGROUND_LOCATION_API_VERSION = 11;
45 const uint32_t PERMISSION_NOT_REQUSET = -1;
46 struct LocationIndex {
47     uint32_t vagueIndex = PERMISSION_NOT_REQUSET;
48     uint32_t accurateIndex = PERMISSION_NOT_REQUSET;
49     uint32_t backIndex = PERMISSION_NOT_REQUSET;
50 };
51 
52 class PermissionManager {
53 public:
54     static PermissionManager& GetInstance();
55     PermissionManager();
56     virtual ~PermissionManager();
57 
58     void RegisterApplicationCallback();
59     void RegisterAppManagerDeathCallback();
60     void AddDefPermissions(const std::vector<PermissionDef>& permList, AccessTokenID tokenId,
61         bool updateFlag);
62     void RemoveDefPermissions(AccessTokenID tokenID);
63     int VerifyNativeAccessToken(AccessTokenID tokenID, const std::string& permissionName);
64     int VerifyHapAccessToken(AccessTokenID tokenID, const std::string& permissionName);
65     virtual int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName);
66     int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult);
67     int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList);
68     int GetReqPermissions(
69         AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant);
70     int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag);
71     int32_t CheckAndUpdatePermission(AccessTokenID tokenID, const std::string& permissionName,
72         bool isGranted, uint32_t flag);
73     int32_t GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag);
74     int32_t RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag);
75     void ClearUserGrantedPermissionState(AccessTokenID tokenID);
76     void GetSelfPermissionState(const std::vector<PermissionStateFull>& permsList,
77         PermissionListState& permState, int32_t apiVersion);
78     int32_t AddPermStateChangeCallback(
79         const PermStateChangeScope& scope, const sptr<IRemoteObject>& callback);
80     int32_t RemovePermStateChangeCallback(const sptr<IRemoteObject>& callback);
81     bool GetApiVersionByTokenId(AccessTokenID tokenID, int32_t& apiVersion);
82     bool LocationPermissionSpecialHandle(std::vector<PermissionListStateParcel>& reqPermList,
83         std::vector<PermissionStateFull>& permsList, int32_t apiVersion);
84     void NotifyPermGrantStoreResult(bool result, uint64_t timestamp);
85     void ClearAllSecCompGrantedPerm(const std::vector<AccessTokenID>& tokenIdList);
86     void ParamUpdate(const std::string& permissionName, uint32_t flag, bool filtered);
87     void NotifyWhenPermissionStateUpdated(AccessTokenID tokenID, const std::string& permissionName,
88         bool isGranted, uint32_t flag, const std::shared_ptr<HapTokenInfoInner>& infoPtr);
89     int32_t ClearUserGrantedPermission(AccessTokenID tokenID);
90     bool IsAllowGrantTempPermission(AccessTokenID tokenID, const std::string& permissionName);
91     void AddPermToKernel(AccessTokenID tokenID, const std::shared_ptr<PermissionPolicySet>& policy);
92     void RemovePermFromKernel(AccessTokenID tokenID);
93     void SetPermToKernel(AccessTokenID tokenID, const std::string& permissionName, bool isGranted);
94 
95 protected:
96     static void RegisterImpl(PermissionManager* implInstance);
97 private:
98     void ScopeToString(
99         const std::vector<AccessTokenID>& tokenIDs, const std::vector<std::string>& permList);
100     int32_t ScopeFilter(const PermStateChangeScope& scopeSrc, PermStateChangeScope& scopeRes);
101     int32_t UpdateTokenPermissionState(
102         AccessTokenID tokenID, const std::string& permissionName, bool isGranted, uint32_t flag);
103     std::string TransferPermissionDefToString(const PermissionDef& inPermissionDef);
104     bool IsPermissionVaild(const std::string& permissionName);
105     bool GetLocationPermissionIndex(std::vector<PermissionListStateParcel>& reqPermList, LocationIndex& locationIndex);
106     bool GetLocationPermissionState(std::vector<PermissionListStateParcel>& reqPermList,
107         std::vector<PermissionStateFull>& permsList, int32_t apiVersion);
108     bool GetStateWithVaguePermission(std::vector<PermissionListStateParcel>& reqPermList,
109         std::vector<PermissionStateFull>& permsList, int32_t apiVersion, const LocationIndex& locationIndex);
110     bool GetLocationPermissionStateBackGroundVersion(std::vector<PermissionListStateParcel>& reqPermList,
111         std::vector<PermissionStateFull>& permsList, int32_t apiVersion);
112     bool GetStateWithVaguePermissionBackGroundVersion(std::vector<PermissionListStateParcel>& reqPermList,
113         std::vector<PermissionStateFull>& permsList, int32_t apiVersion, const LocationIndex& locationIndex);
114     void NotifyUpdatedPermList(const std::vector<std::string>& grantedPermListBefore,
115         const std::vector<std::string>& grantedPermListAfter, AccessTokenID tokenID);
116 
117     PermissionGrantEvent grantEvent_;
118     static std::recursive_mutex mutex_;
119     static PermissionManager* implInstance_;
120 
121     OHOS::Utils::RWLock permParamSetLock_;
122     uint64_t paramValue_ = 0;
123 
124     DISALLOW_COPY_AND_MOVE(PermissionManager);
125 };
126 } // namespace AccessToken
127 } // namespace Security
128 } // namespace OHOS
129 #endif // PERMISSION_MANAGER_H
130