1 /* 2 * Copyright (c) 2021-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 #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 OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 46 47 AccessTokenIDEx AllocHapToken(const HapInfoParcel& info, const HapPolicyParcel& policy) override; 48 int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName) override; 49 int GetDefPermission(const std::string& permissionName, PermissionDefParcel& permissionDefResult) override; 50 int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDefParcel>& permList) override; 51 int GetReqPermissions( 52 AccessTokenID tokenID, std::vector<PermissionStateFullParcel>& reqPermList, bool isSystemGrant) override; 53 PermissionOper GetSelfPermissionsState(std::vector<PermissionListStateParcel>& reqPermList, 54 PermissionGrantInfoParcel& infoParcel) override; 55 int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag) override; 56 int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag) override; 57 int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag) override; 58 int ClearUserGrantedPermissionState(AccessTokenID tokenID) override; 59 int DeleteToken(AccessTokenID tokenID) override; 60 int GetTokenType(AccessTokenID tokenID) override; 61 int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap) override; 62 AccessTokenIDEx GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex) override; 63 AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID) override; 64 int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfoParcel& infoParcel) override; 65 int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfoParcel& infoParcel) override; 66 int UpdateHapToken(AccessTokenIDEx& tokenIdEx, bool isSystemApp, 67 const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParcel& policyParcel) override; 68 int32_t RegisterPermStateChangeCallback( 69 const PermStateChangeScopeParcel& scope, const sptr<IRemoteObject>& callback) override; 70 int32_t UnRegisterPermStateChangeCallback(const sptr<IRemoteObject>& callback) override; 71 #ifndef ATM_BUILD_VARIANT_USER_ENABLE 72 int32_t ReloadNativeTokenInfo() override; 73 #endif 74 AccessTokenID GetNativeTokenId(const std::string& processName) override; 75 76 #ifdef TOKEN_SYNC_ENABLE 77 int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSyncParcel& hapSyncParcel) override; 78 int GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSyncParcel>& nativeTokenInfosRes) override; 79 int SetRemoteHapTokenInfo(const std::string& deviceID, HapTokenInfoForSyncParcel& hapSyncParcel) override; 80 int SetRemoteNativeTokenInfo(const std::string& deviceID, 81 std::vector<NativeTokenInfoForSyncParcel>& nativeTokenInfoForSyncParcel) override; 82 int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID) override; 83 AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID) override; 84 int DeleteRemoteDeviceTokens(const std::string& deviceID) override; 85 #endif 86 87 int SetPermDialogCap(const HapBaseInfoParcel& hapBaseInfoParcel, bool enable) override; 88 void DumpTokenInfo(const AtmToolsParamInfoParcel& infoParcel, std::string& dumpInfo) override; 89 int Dump(int fd, const std::vector<std::u16string>& args) override; 90 91 private: 92 void GetValidConfigFilePathList(std::vector<std::string>& pathList); 93 bool GetConfigGrantValueFromFile(std::string& fileContent); 94 void SetDefaultConfigGrantValue(); 95 void GetConfigValue(); 96 bool Initialize(); 97 void DumpToken(); 98 void DumpTokenIfNeeded(); 99 void AccessTokenServiceParamSet() const; 100 101 #ifdef EVENTHANDLER_ENABLE 102 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 103 std::shared_ptr<AccessEventHandler> eventHandler_; 104 #endif 105 ServiceRunningState state_; 106 std::string grantBundleName_; 107 std::string grantAbilityName_; 108 OHOS::ThreadPool tokenDumpWorker_; 109 }; 110 } // namespace AccessToken 111 } // namespace Security 112 } // namespace OHOS 113 #endif // ACCESSTOKEN_MANAGER_SERVICE_H 114