nss_tool.cc (1851B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include <prinit.h> 13 14 #include "argparse.h" 15 #include "db/dbtool.h" 16 #include "digest/digesttool.h" 17 #include "enc/enctool.h" 18 #include "tool.h" 19 20 static void Usage() { 21 std::cerr << "Usage: nss <command> <subcommand> [options]" << std::endl; 22 std::cerr << " nss db [--path <directory>] <commands>" << std::endl; 23 std::cerr << " nss encrypt <options>" << std::endl; 24 std::cerr << " nss decrypt <options>" << std::endl; 25 std::cerr << " nss digest <options>" << std::endl; 26 } 27 28 static const std::string kDbCommand = "db"; 29 static const std::string kEncryptCommand = "encrypt"; 30 static const std::string kDecryptCommand = "decrypt"; 31 static const std::string kDigestCommand = "digest"; 32 33 int main(int argc, char **argv) { 34 if (argc < 2) { 35 Usage(); 36 return 1; 37 } 38 std::vector<std::string> arguments(argv + 2, argv + argc); 39 40 std::unique_ptr<Tool> tool = nullptr; 41 if (argv[1] == kDbCommand) { 42 tool = std::unique_ptr<Tool>(new DBTool()); 43 } 44 if (argv[1] == kEncryptCommand) { 45 tool = std::unique_ptr<Tool>(new EncTool()); 46 arguments.push_back("--encrypt"); 47 } 48 if (argv[1] == kDecryptCommand) { 49 tool = std::unique_ptr<Tool>(new EncTool()); 50 arguments.push_back("--decrypt"); 51 } 52 if (argv[1] == kDigestCommand) { 53 tool = std::unique_ptr<Tool>(new DigestTool()); 54 } 55 if (!tool) { 56 Usage(); 57 return 1; 58 } 59 60 int exit_code = 0; 61 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); 62 63 if (!tool->Run(arguments)) { 64 exit_code = 1; 65 } 66 67 PR_Cleanup(); 68 69 return exit_code; 70 }