cmdline.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <bsd/sys/queue.h>
  16. #include "qa/qa.h"
  17. #include "qa/questions/questions.h"
  18. /**
  19. * \brief Prints the usage message, then exit.
  20. *
  21. * Prints in POSIX format the various options for using qa.
  22. *
  23. */
  24. void usage(void)
  25. {
  26. qa_question_t *q;
  27. static const char* help_message = "%s usage: %s"
  28. " [-r HOST:port | -f X509 | -R RSA]"
  29. " [-a ATTACK]"
  30. " \n\n"
  31. "If no argument is supplied, by default a public RSA key is expected "
  32. "to be read from the standard input.\n\n"
  33. "Available attacks: \n";
  34. fprintf(stderr, help_message,
  35. program_invocation_short_name,
  36. program_invocation_name);
  37. LIST_FOREACH(q, &questions, qs)
  38. fprintf(stderr, "%-10s\t\t%s\n", q->name, q->pretty_name);
  39. fprintf(stderr, "\n");
  40. }
  41. void conflicting_args(void)
  42. {
  43. fprintf(stderr, "Conflicting arguments.\n");
  44. usage();
  45. exit(EXIT_FAILURE);
  46. }
  47. int main(int argc, char** argv)
  48. {
  49. char opt;
  50. int option_index;
  51. struct option long_options[] = {
  52. {"help", no_argument, NULL, 'h'},
  53. {"remote", required_argument, NULL, 'r'},
  54. {"file", required_argument, NULL, 'f'},
  55. {"rsa", required_argument, NULL, 'R'},
  56. {0, 0, 0, 0}
  57. };
  58. static const char* short_options = "hr:f:a:R:";
  59. struct qa_conf conf = {
  60. .src_type = NONE,
  61. .attacks = NULL,
  62. };
  63. QA_library_init();
  64. while ((opt=getopt_long(argc, argv,
  65. short_options, long_options,
  66. &option_index)) != -1)
  67. switch (opt) {
  68. case 'h':
  69. usage();
  70. exit(EXIT_SUCCESS);
  71. break;
  72. case 'f':
  73. if (conf.src_type != NONE) conflicting_args();
  74. conf.src_type = LOCAL_X509;
  75. conf.src = optarg;
  76. break;
  77. case 'r':
  78. if (conf.src_type != NONE) conflicting_args();
  79. conf.src_type = REMOTE;
  80. conf.src = optarg;
  81. break;
  82. case 'R':
  83. if (conf.src_type != NONE) conflicting_args();
  84. conf.src_type = LOCAL_RSA;
  85. conf.src = optarg;
  86. break;
  87. case 'a':
  88. conf.attacks = optarg;
  89. break;
  90. case '?':
  91. default:
  92. usage();
  93. exit(EXIT_FAILURE);
  94. }
  95. if (conf.src_type == NONE) {
  96. conf.src_type = LOCAL_RSA;
  97. if (optind == argc)
  98. conf.src = "-";
  99. else if (optind == argc-1)
  100. conf.src = argv[optind];
  101. else if (optind == argc-2 && !strcmp(argv[optind], "--"))
  102. conf.src = argv[optind+1];
  103. else {
  104. usage();
  105. exit(EXIT_FAILURE);
  106. }
  107. }
  108. return qa_init(&conf);
  109. }