Fix CMP -days option range checking and test failing with enable-ubsan
[openssl.git] / test / confdump.c
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
10 #include <stdio.h>
11 #include <string.h>
12 #include <openssl/bio.h>
13 #include <openssl/conf.h>
14 #include <openssl/safestack.h>
15 #include <openssl/err.h>
16
17 DEFINE_STACK_OF(CONF_VALUE)
18 DEFINE_STACK_OF_CSTRING()
19
20 static STACK_OF(OPENSSL_CSTRING) *section_names = NULL;
21
22 static void collect_section_name(CONF_VALUE *v)
23 {
24     /* A section is a CONF_VALUE with name == NULL */
25     if (v->name == NULL)
26         sk_OPENSSL_CSTRING_push(section_names, v->section);
27 }
28
29 static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
30 {
31     return strcmp(*a, *b);
32 }
33
34 static void collect_all_sections(const CONF *cnf)
35 {
36     section_names = sk_OPENSSL_CSTRING_new(section_name_cmp);
37     lh_CONF_VALUE_doall(cnf->data, collect_section_name);
38     sk_OPENSSL_CSTRING_sort(section_names);
39 }
40
41 static void dump_section(const char *name, const CONF *cnf)
42 {
43     STACK_OF(CONF_VALUE) *sect = NCONF_get_section(cnf, name);
44     int i;
45
46     printf("[ %s ]\n", name);
47     for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
48         CONF_VALUE *cv = sk_CONF_VALUE_value(sect, i);
49
50         printf("%s = %s\n", cv->name, cv->value);
51     }
52 }
53
54 int main(int argc, char **argv)
55 {
56     long eline;
57     CONF *conf = NCONF_new(NCONF_default());
58     int ret = 1;
59
60     if (conf != NULL && NCONF_load(conf, argv[1], &eline)) {
61         int i;
62
63         collect_all_sections(conf);
64         for (i = 0; i < sk_OPENSSL_CSTRING_num(section_names); i++) {
65             dump_section(sk_OPENSSL_CSTRING_value(section_names, i), conf);
66         }
67         sk_OPENSSL_CSTRING_free(section_names);
68         ret = 0;
69     } else {
70         ERR_print_errors_fp(stderr);
71     }
72     NCONF_free(conf);
73     return ret;
74 }