qa.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <assert.h>
  2. #include <error.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <openssl/err.h>
  8. #include <openssl/pem.h>
  9. #include <openssl/ssl.h>
  10. #include <openssl/x509.h>
  11. #include "qa/qa.h"
  12. #include "qa/questions/questions.h"
  13. #include "qa/qa_sock.h"
  14. /** BIO wrapper around stdout */
  15. BIO* bio_out;
  16. /** BIO wrapper around srderr */
  17. BIO* bio_err;
  18. void qa_abort(const char *reason)
  19. {
  20. //ERR_print_errors_fp(stderr);
  21. exit(EXIT_FAILURE);
  22. }
  23. X509* get_local_cert(const char *src)
  24. {
  25. X509 *crt;
  26. FILE *fp;
  27. if (!strcmp(src, "-")) fp = stdin;
  28. else if (!(fp = fopen(src, "r")))
  29. return NULL;
  30. crt = PEM_read_X509(fp, NULL, 0, NULL);
  31. return crt;
  32. }
  33. /**
  34. * \brief Given an initial configuration, stuctures the program flow.
  35. *
  36. * \param[in] args Initial configuration given from a frontend.
  37. */
  38. int qa_init(const struct qa_conf* conf)
  39. {
  40. X509 *crt;
  41. struct qa_question *q;
  42. /* bind stdout/stderr to a BIO shit to be used externally */
  43. bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  44. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  45. /* Initialize SSL Library by registering algorithms. */
  46. SSL_library_init();
  47. if (conf->src_type == REMOTE)
  48. crt = get_remote_cert(conf->src);
  49. else if (conf->src_type == LOCAL)
  50. crt = get_local_cert(conf->src);
  51. else
  52. error(EXIT_FAILURE, 0, "iternal error: unable to determine source type.");
  53. if (!crt)
  54. error(EXIT_FAILURE, errno, "oops");
  55. register_all_questions();
  56. for (q=questions.lh_first; q; q = q->qs.le_next) {
  57. if (q->setup) q->setup();
  58. if (q->test) q->test(crt);
  59. q->ask(crt);
  60. if (q->teardown) q->teardown();
  61. }
  62. X509_free(crt);
  63. return 0;
  64. }