cmdline.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * \file cmdline.c
  3. *
  4. * \brief Commandline utilities.
  5. *
  6. * Frontend to QA, proving an easy command line inteface according with the
  7. * POSIX standard.
  8. */
  9. #define _GNU_SOURCE
  10. #include <getopt.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include "qa/qa.h"
  16. /**
  17. * \brief Prints the usage message, then exit.
  18. *
  19. * Prints in POSIX format the various options for using qa.
  20. *
  21. */
  22. void usage(void)
  23. {
  24. static const char* help_message = "%s usage: %s"
  25. " [-r HOST:port | -f X509 | -R RSA]"
  26. " [-a ATTACK]"
  27. " \n"
  28. "If no argument is supplied, by default a public RSA key is expected "
  29. "to be read from the standard input.\n";
  30. fprintf(stderr, help_message,
  31. program_invocation_short_name,
  32. program_invocation_name);
  33. }
  34. void conflicting_args(void)
  35. {
  36. fprintf(stderr, "Conflicting arguments.\n");
  37. usage();
  38. exit(EXIT_FAILURE);
  39. }
  40. int main(int argc, char** argv)
  41. {
  42. char opt;
  43. int option_index;
  44. struct option long_options[] = {
  45. {"help", no_argument, NULL, 'h'},
  46. {"remote", required_argument, NULL, 'r'},
  47. {"file", required_argument, NULL, 'f'},
  48. {"rsa", required_argument, NULL, 'R'},
  49. {0, 0, 0, 0}
  50. };
  51. static const char* short_options = "hr:f:a:R:";
  52. struct qa_conf conf = {
  53. .src_type = NONE,
  54. .attacks = NULL,
  55. };
  56. while ((opt=getopt_long(argc, argv,
  57. short_options, long_options,
  58. &option_index)) != -1)
  59. switch (opt) {
  60. case 'h':
  61. usage();
  62. exit(EXIT_SUCCESS);
  63. break;
  64. case 'f':
  65. if (conf.src_type != NONE) conflicting_args();
  66. conf.src_type = LOCAL_X509;
  67. conf.src = optarg;
  68. break;
  69. case 'r':
  70. if (conf.src_type != NONE) conflicting_args();
  71. conf.src_type = REMOTE;
  72. conf.src = optarg;
  73. break;
  74. case 'R':
  75. if (conf.src_type != NONE) conflicting_args();
  76. conf.src_type = LOCAL_RSA;
  77. conf.src = optarg;
  78. break;
  79. case 'a':
  80. conf.attacks = optarg;
  81. break;
  82. case '?':
  83. default:
  84. usage();
  85. exit(EXIT_FAILURE);
  86. }
  87. if (conf.src_type == NONE) {
  88. conf.src_type = LOCAL_RSA;
  89. if (optind == argc)
  90. conf.src = "-";
  91. else if (optind == argc-1)
  92. conf.src = argv[optind];
  93. else if (optind == argc-2 && !strcmp(argv[optind], "--"))
  94. conf.src = argv[optind+1];
  95. else {
  96. usage();
  97. exit(EXIT_FAILURE);
  98. }
  99. }
  100. return qa_init(&conf);
  101. }