1 /* 2 * Copyright (c) 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 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 28 namespace OHOS { 29 namespace Security { 30 namespace AccessToken { 31 typedef enum TypeOptType { 32 DEFAULT = 0, 33 DUMP_TOKEN, 34 DUMP_RECORD, 35 PERM_GRANT, 36 PERM_REVOKE, 37 } OptType; 38 39 class AtmCommand final { 40 public: 41 AtmCommand(int32_t argc, char *argv[]); 42 virtual ~AtmCommand() = default; 43 44 std::string ExecCommand(); 45 46 private: 47 std::string GetCommandErrorMsg() const; 48 int32_t RunAsCommandError(void); 49 std::string GetUnknownOptionMsg() const; 50 int32_t RunAsCommandMissingOptionArgument(void); 51 int32_t RunAsCommandExistentOptionArgument(const int32_t& option, 52 OptType& type, AccessTokenID& tokenId, std::string& permissionName); 53 std::string DumpRecordInfo(uint32_t tokenId, const std::string& permissionName); 54 int32_t ModifyPermission(const OptType& type, AccessTokenID tokenId, const std::string& permissionName); 55 int32_t RunCommandByOperationType(const OptType& type, 56 AccessTokenID tokenId, const std::string& permissionName); 57 int32_t HandleComplexCommand(const std::string& shortOption, const struct option longOption[], 58 const std::string& helpMsg); 59 60 int32_t RunAsHelpCommand(); 61 int32_t RunAsCommonCommand(); 62 63 int32_t argc_; 64 char** argv_; 65 66 std::string cmd_; 67 std::vector<std::string> argList_; 68 69 std::string name_; 70 std::map<std::string, std::function<int32_t()>> commandMap_; 71 72 std::string resultReceiver_; 73 }; 74 } // namespace AccessToken 75 } // namespace Security 76 } // namespace OHOS 77 78 #endif // ACCESSTOKENMANAGER_COMMAND_H 79