confdump.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/safestack.h>
  14. #include <openssl/err.h>
  15. DEFINE_STACK_OF(CONF_VALUE)
  16. DEFINE_STACK_OF_CSTRING()
  17. static STACK_OF(OPENSSL_CSTRING) *section_names = NULL;
  18. static void collect_section_name(CONF_VALUE *v)
  19. {
  20. /* A section is a CONF_VALUE with name == NULL */
  21. if (v->name == NULL)
  22. sk_OPENSSL_CSTRING_push(section_names, v->section);
  23. }
  24. static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
  25. {
  26. return strcmp(*a, *b);
  27. }
  28. static void collect_all_sections(const CONF *cnf)
  29. {
  30. section_names = sk_OPENSSL_CSTRING_new(section_name_cmp);
  31. lh_CONF_VALUE_doall(cnf->data, collect_section_name);
  32. sk_OPENSSL_CSTRING_sort(section_names);
  33. }
  34. static void dump_section(const char *name, const CONF *cnf)
  35. {
  36. STACK_OF(CONF_VALUE) *sect = NCONF_get_section(cnf, name);
  37. int i;
  38. printf("[ %s ]\n", name);
  39. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  40. CONF_VALUE *cv = sk_CONF_VALUE_value(sect, i);
  41. printf("%s = %s\n", cv->name, cv->value);
  42. }
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. long eline;
  47. CONF *conf = NCONF_new(NCONF_default());
  48. int ret = 1;
  49. if (conf != NULL && NCONF_load(conf, argv[1], &eline)) {
  50. int i;
  51. collect_all_sections(conf);
  52. for (i = 0; i < sk_OPENSSL_CSTRING_num(section_names); i++) {
  53. dump_section(sk_OPENSSL_CSTRING_value(section_names, i), conf);
  54. }
  55. sk_OPENSSL_CSTRING_free(section_names);
  56. ret = 0;
  57. } else {
  58. ERR_print_errors_fp(stderr);
  59. }
  60. NCONF_free(conf);
  61. return ret;
  62. }