nss_tool.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <algorithm>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include <prinit.h>
  11. #include "argparse.h"
  12. #include "db/dbtool.h"
  13. #include "digest/digesttool.h"
  14. #include "enc/enctool.h"
  15. #include "tool.h"
  16. static void Usage() {
  17. std::cerr << "Usage: nss <command> <subcommand> [options]" << std::endl;
  18. std::cerr << " nss db [--path <directory>] <commands>" << std::endl;
  19. std::cerr << " nss encrypt <options>" << std::endl;
  20. std::cerr << " nss decrypt <options>" << std::endl;
  21. std::cerr << " nss digest <options>" << std::endl;
  22. }
  23. static const std::string kDbCommand = "db";
  24. static const std::string kEncryptCommand = "encrypt";
  25. static const std::string kDecryptCommand = "decrypt";
  26. static const std::string kDigestCommand = "digest";
  27. int main(int argc, char **argv) {
  28. if (argc < 2) {
  29. Usage();
  30. return 1;
  31. }
  32. std::vector<std::string> arguments(argv + 2, argv + argc);
  33. std::unique_ptr<Tool> tool = nullptr;
  34. if (argv[1] == kDbCommand) {
  35. tool = std::unique_ptr<Tool>(new DBTool());
  36. }
  37. if (argv[1] == kEncryptCommand) {
  38. tool = std::unique_ptr<Tool>(new EncTool());
  39. arguments.push_back("--encrypt");
  40. }
  41. if (argv[1] == kDecryptCommand) {
  42. tool = std::unique_ptr<Tool>(new EncTool());
  43. arguments.push_back("--decrypt");
  44. }
  45. if (argv[1] == kDigestCommand) {
  46. tool = std::unique_ptr<Tool>(new DigestTool());
  47. }
  48. if (!tool) {
  49. Usage();
  50. return 1;
  51. }
  52. int exit_code = 0;
  53. PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
  54. if (!tool->Run(arguments)) {
  55. exit_code = 1;
  56. }
  57. PR_Cleanup();
  58. return exit_code;
  59. }