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 #ifndef ACCESSTOKEN_MANAGER_SERVICE_H
17 #define ACCESSTOKEN_MANAGER_SERVICE_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "accesstoken_manager_stub.h"
23 #ifdef EVENTHANDLER_ENABLE
24 #include "access_event_handler.h"
25 #endif
26 #include "access_token.h"
27 #include "hap_token_info.h"
28 #include "iremote_object.h"
29 #include "nocopyable.h"
30 #include "singleton.h"
31 #include "system_ability.h"
32 #include "thread_pool.h"
33 
34 namespace OHOS {
35 namespace Security {
36 namespace AccessToken {
37 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
38 class AccessTokenManagerService final : public SystemAbility, public AccessTokenManagerStub {
39     DECLARE_DELAYED_SINGLETON(AccessTokenManagerService);
40     DECLEAR_SYSTEM_ABILITY(AccessTokenManagerService);
41 
42 public:
43     void OnStart() override;
44     void OnStop() override;
45     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
46     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
47 
48     AccessTokenIDEx AllocHapToken(const HapInfoParcel& info, const HapPolicyParcel& policy) override;
49     PermUsedTypeEnum GetUserGrantedPermissionUsedType(
50         AccessTokenID tokenID, const std::string& permissionName) override;
51     int32_t InitHapToken(const HapInfoParcel& info, HapPolicyParcel& policy,
52         AccessTokenIDEx& fullTokenId) override;
53     int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) override;
54     int GetDefPermission(const std::string& permissionName, PermissionDefParcel& permissionDefResult) override;
55     int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDefParcel>& permList) override;
56     int GetReqPermissions(
57         AccessTokenID tokenID, std::vector<PermissionStateFullParcel>& reqPermList, bool isSystemGrant) override;
58     PermissionOper GetSelfPermissionsState(std::vector<PermissionListStateParcel>& reqPermList,
59         PermissionGrantInfoParcel& infoParcel) override;
60     int32_t GetPermissionsStatus(AccessTokenID tokenID, std::vector<PermissionListStateParcel>& reqPermList) override;
61     int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag) override;
62     int32_t SetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t status,
63         int32_t userID) override;
64     int32_t GetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t& status,
65         int32_t userID) override;
66     int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag) override;
67     int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag) override;
68     int GrantPermissionForSpecifiedTime(
69         AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime) override;
70     int ClearUserGrantedPermissionState(AccessTokenID tokenID) override;
71     int DeleteToken(AccessTokenID tokenID) override;
72     int GetTokenType(AccessTokenID tokenID) override;
73     int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap) override;
74     AccessTokenIDEx GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex) override;
75     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID) override;
76     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfoParcel& infoParcel) override;
77     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& infoParcel) override;
78     int32_t UpdateHapToken(AccessTokenIDEx& tokenIdEx,
79         const UpdateHapInfoParams& info, const HapPolicyParcel& policyParcel) override;
80     int32_t RegisterPermStateChangeCallback(
81         const PermStateChangeScopeParcel& scope, const sptr<IRemoteObject>& callback) override;
82     int32_t UnRegisterPermStateChangeCallback(const sptr<IRemoteObject>& callback) override;
83 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
84     int32_t ReloadNativeTokenInfo() override;
85 #endif
86     AccessTokenID GetNativeTokenId(const std::string& processName) override;
87 
88 #ifdef TOKEN_SYNC_ENABLE
89     int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSyncParcel& hapSyncParcel) override;
90     int SetRemoteHapTokenInfo(const std::string& deviceID, HapTokenInfoForSyncParcel& hapSyncParcel) override;
91     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) override;
92     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) override;
93     int DeleteRemoteDeviceTokens(const std::string& deviceID) override;
94     int32_t RegisterTokenSyncCallback(const sptr<IRemoteObject>& callback) override;
95     int32_t UnRegisterTokenSyncCallback() override;
96 #endif
97 
98     int SetPermDialogCap(const HapBaseInfoParcel& hapBaseInfoParcel, bool enable) override;
99     void GetPermissionManagerInfo(PermissionGrantInfoParcel& infoParcel) override;
100     void DumpTokenInfo(const AtmToolsParamInfoParcel& infoParcel, std::string& dumpInfo) override;
101     int32_t GetVersion(uint32_t& version) override;
102     int Dump(int fd, const std::vector<std::u16string>& args) override;
103 
104 private:
105     void GetValidConfigFilePathList(std::vector<std::string>& pathList);
106     bool GetConfigGrantValueFromFile(std::string& fileContent);
107     void GetConfigValue();
108     bool Initialize();
109     void AccessTokenServiceParamSet() const;
110     PermissionOper GetPermissionsState(AccessTokenID tokenID, std::vector<PermissionListStateParcel>& reqPermList);
111 #ifdef EVENTHANDLER_ENABLE
112     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
113     std::shared_ptr<AccessEventHandler> eventHandler_;
114     std::shared_ptr<AppExecFwk::EventRunner> shortGrantEventRunner_;
115     std::shared_ptr<AccessEventHandler> shortGrantEventHandler_;
116 #endif
117     ServiceRunningState state_;
118     std::string grantBundleName_;
119     std::string grantAbilityName_;
120     std::string grantServiceAbilityName_;
121     std::string permStateAbilityName_;
122     std::string globalSwitchAbilityName_;
123 };
124 } // namespace AccessToken
125 } // namespace Security
126 } // namespace OHOS
127 #endif // ACCESSTOKEN_MANAGER_SERVICE_H
128