qa.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. void qa_abort(const char *reason)
  15. {
  16. //ERR_print_errors_fp(stderr);
  17. exit(EXIT_FAILURE);
  18. }
  19. X509* get_local_cert(const char *src)
  20. {
  21. X509 *crt;
  22. FILE *fp;
  23. if (!strcmp(src, "-")) fp = stdin;
  24. else if (!(fp = fopen(src, "r")))
  25. return NULL;
  26. crt = PEM_read_X509(fp, NULL, 0, NULL);
  27. return crt;
  28. }
  29. /**
  30. * \brief Given an initial configuration, stuctures the program flow.
  31. *
  32. * \param[in] args Initial configuration given from a frontend.
  33. */
  34. int qa_init(const struct qa_conf* conf)
  35. {
  36. X509 *crt;
  37. struct qa_question *q;
  38. /* bind stdout/stderr to a BIO shit to be used externally */
  39. bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
  40. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  41. /* Initialize SSL Library by registering algorithms. */
  42. SSL_library_init();
  43. if (conf->src_type == REMOTE)
  44. crt = get_remote_cert(conf->src);
  45. else if (conf->src_type == LOCAL)
  46. crt = get_local_cert(conf->src);
  47. else
  48. error(EXIT_FAILURE, 0, "iternal error: unable to determine source type.");
  49. if (!crt)
  50. error(EXIT_FAILURE, errno, "oops");
  51. register_all_questions();
  52. for (q=questions.lh_first; q; q = q->qs.le_next) {
  53. if (q->setup) q->setup();
  54. if (q->test) q->test(crt);
  55. q->ask(crt);
  56. if (q->teardown) q->teardown();
  57. }
  58. X509_free(crt);
  59. return 0;
  60. }