confdump.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. static STACK_OF(OPENSSL_CSTRING) *section_names = NULL;
  16. static void collect_section_name(CONF_VALUE *v)
  17. {
  18. /* A section is a CONF_VALUE with name == NULL */
  19. if (v->name == NULL)
  20. sk_OPENSSL_CSTRING_push(section_names, v->section);
  21. }
  22. static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
  23. {
  24. return strcmp(*a, *b);
  25. }
  26. static void collect_all_sections(const CONF *cnf)
  27. {
  28. section_names = sk_OPENSSL_CSTRING_new(section_name_cmp);
  29. lh_CONF_VALUE_doall(cnf->data, collect_section_name);
  30. sk_OPENSSL_CSTRING_sort(section_names);
  31. }
  32. static void dump_section(const char *name, const CONF *cnf)
  33. {
  34. STACK_OF(CONF_VALUE) *sect = NCONF_get_section(cnf, name);
  35. int i;
  36. printf("[ %s ]\n", name);
  37. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  38. CONF_VALUE *cv = sk_CONF_VALUE_value(sect, i);
  39. printf("%s = %s\n", cv->name, cv->value);
  40. }
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. long eline;
  45. CONF *conf = NCONF_new(NCONF_default());
  46. int ret = 1;
  47. if (conf != NULL && NCONF_load(conf, argv[1], &eline)) {
  48. int i;
  49. collect_all_sections(conf);
  50. for (i = 0; i < sk_OPENSSL_CSTRING_num(section_names); i++) {
  51. dump_section(sk_OPENSSL_CSTRING_value(section_names, i), conf);
  52. }
  53. sk_OPENSSL_CSTRING_free(section_names);
  54. ret = 0;
  55. } else {
  56. ERR_print_errors_fp(stderr);
  57. }
  58. NCONF_free(conf);
  59. return ret;
  60. }