1 /* 2 * Copyright (c) 2022-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 ACCESSTOKENMANAGER_COMMAND_H 17 #define ACCESSTOKENMANAGER_COMMAND_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <getopt.h> 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 #include "access_token.h" 27 #include "atm_tools_param_info.h" 28 29 namespace OHOS { 30 namespace Security { 31 namespace AccessToken { 32 constexpr const uint32_t INVALID_ATM_SET_STATUS = 2; 33 /** 34 * @brief Atm tools operate type 35 */ 36 typedef enum TypeOptType { 37 /** default */ 38 DEFAULT_OPER = 0, 39 /** dump hap or native token info */ 40 DUMP_TOKEN, 41 /** dump permission used records */ 42 DUMP_RECORD, 43 /** dump permission used types */ 44 DUMP_TYPE, 45 /** dump permission definition info */ 46 DUMP_PERM, 47 /** grant permission */ 48 PERM_GRANT, 49 /** revoke permission */ 50 PERM_REVOKE, 51 } OptType; 52 53 /** 54 * @brief Atm toggle mode type 55 */ 56 typedef enum TypeToggleModeType { 57 /** toggle mode is request */ 58 TOGGLE_REQUEST = 0, 59 /** toggle mode is record */ 60 TOGGLE_RECORD, 61 } ToggleModeType; 62 63 typedef enum TypeToggleOperateType { 64 /** set toggle request/record status */ 65 TOGGLE_SET, 66 /** get toggle request/record status */ 67 TOGGLE_GET, 68 } ToggleOperateType; 69 70 class AtmToggleParamInfo final { 71 public: 72 ToggleModeType toggleMode; 73 ToggleOperateType type; 74 int32_t userID; 75 std::string permissionName; 76 uint32_t status = INVALID_ATM_SET_STATUS; 77 }; 78 79 class AtmCommand final { 80 public: 81 AtmCommand(int32_t argc, char* argv[]); 82 virtual ~AtmCommand() = default; 83 84 std::string ExecCommand(); 85 86 private: 87 std::string GetCommandErrorMsg() const; 88 int32_t RunAsCommandError(void); 89 std::string GetUnknownOptionMsg() const; 90 int32_t RunAsCommandMissingOptionArgument(const std::vector<char>& requiredOptions); 91 void RunAsCommandExistentOptionForDump( 92 const int32_t& option, AtmToolsParamInfo& info, OptType& type, std::string& permissionName); 93 void RunAsCommandExistentOptionForPerm( 94 const int32_t& option, bool& isGranted, AccessTokenID& tokenID, std::string& permission); 95 void RunAsCommandExistentOptionForToggle(const int32_t& option, AtmToggleParamInfo& info); 96 std::string DumpRecordInfo(uint32_t tokenId, const std::string& permissionName); 97 std::string DumpUsedTypeInfo(uint32_t tokenId, const std::string& permissionName); 98 int32_t ModifyPermission(bool isGranted, AccessTokenID tokenId, const std::string& permissionName); 99 int32_t RunCommandByOperationType(const AtmToolsParamInfo& info, OptType type, std::string& permissionName); 100 101 int32_t SetToggleStatus(int32_t userID, const std::string& permissionName, const uint32_t& status); 102 int32_t GetToggleStatus(int32_t userID, const std::string& permissionName, std::string& statusInfo); 103 104 int32_t RunToggleCommandByOperationType(const AtmToggleParamInfo& info); 105 int32_t HandleToggleRequest(const AtmToggleParamInfo& info, std::string& dumpInfo); 106 int32_t HandleToggleRecord(const AtmToggleParamInfo& info, std::string& dumpInfo); 107 int32_t SetRecordToggleStatus(int32_t userID, const uint32_t& recordStatus, std::string& statusInfo); 108 int32_t GetRecordToggleStatus(int32_t userID, std::string& statusInfo); 109 bool IsNumericString(const char* string); 110 111 int32_t RunAsHelpCommand(); 112 int32_t RunAsCommonCommandForDump(); 113 int32_t RunAsCommonCommandForPerm(); 114 int32_t RunAsCommonCommandForToggle(); 115 116 int32_t argc_; 117 char** argv_; 118 119 std::string cmd_; 120 std::vector<std::string> argList_; 121 122 std::string name_; 123 std::map<std::string, std::function<int32_t()>> commandMap_; 124 125 std::string resultReceiver_; 126 }; 127 } // namespace AccessToken 128 } // namespace Security 129 } // namespace OHOS 130 131 #endif // ACCESSTOKENMANAGER_COMMAND_H 132