Add support for passing the libctx to the config loader
[openssl.git] / doc / man3 / CONF_modules_load_file.pod
1 =pod
2
3 =head1 NAME
4
5 CONF_modules_load_file_with_libctx, CONF_modules_load_file, CONF_modules_load
6 - OpenSSL configuration functions
7
8 =head1 SYNOPSIS
9
10  #include <openssl/conf.h>
11
12  int CONF_modules_load_file_with_libctx(OPENSSL_CTX *libctx,
13                                         const char *filename,
14                                         const char *appname, unsigned long flags);
15  int CONF_modules_load_file(const char *filename, const char *appname,
16                             unsigned long flags);
17  int CONF_modules_load(const CONF *cnf, const char *appname,
18                        unsigned long flags);
19
20 =head1 DESCRIPTION
21
22 The function CONF_modules_load_file_with_libctx() configures OpenSSL using
23 library context B<libctx> file B<filename> and application name B<appname>.
24 If B<filename> is NULL the standard OpenSSL configuration file is used.
25 If B<appname> is NULL the standard OpenSSL application name B<openssl_conf> is
26 used.
27 The behaviour can be customized using B<flags>.
28
29 CONF_modules_load_file() is the same as CONF_modules_load_file_with_libctx() but
30 has a NULL library context.
31
32 CONF_modules_load() is identical to CONF_modules_load_file() except it
33 reads configuration information from B<cnf>.
34
35 =head1 NOTES
36
37 The following B<flags> are currently recognized:
38
39 If B<CONF_MFLAGS_IGNORE_ERRORS> is set errors returned by individual
40 configuration modules are ignored. If not set the first module error is
41 considered fatal and no further modules are loaded.
42
43 Normally any modules errors will add error information to the error queue. If
44 B<CONF_MFLAGS_SILENT> is set no error information is added.
45
46 If B<CONF_MFLAGS_IGNORE_RETURN_CODES> is set the function unconditionally
47 returns success.
48 This is used by default in L<OPENSSL_init_crypto(3)> to ignore any errors in
49 the default system-wide configuration file, as having all OpenSSL applications
50 fail to start when there are potentially minor issues in the file is too risky.
51 Applications calling B<CONF_modules_load_file_with_libctx> explicitly should not
52 generally set this flag.
53
54 If B<CONF_MFLAGS_NO_DSO> is set configuration module loading from DSOs is
55 disabled.
56
57 B<CONF_MFLAGS_IGNORE_MISSING_FILE> if set will make CONF_load_modules_file()
58 ignore missing configuration files. Normally a missing configuration file
59 return an error.
60
61 B<CONF_MFLAGS_DEFAULT_SECTION> if set and B<appname> is not NULL will use the
62 default section pointed to by B<openssl_conf> if B<appname> does not exist.
63
64 By using CONF_modules_load_file_with_libctx() with appropriate flags an
65 application can customise application configuration to best suit its needs.
66 In some cases the use of a configuration file is optional and its absence is not
67 an error: in this case B<CONF_MFLAGS_IGNORE_MISSING_FILE> would be set.
68
69 Errors during configuration may also be handled differently by different
70 applications. For example in some cases an error may simply print out a warning
71 message and the application continue. In other cases an application might
72 consider a configuration file error as fatal and exit immediately.
73
74 Applications can use the CONF_modules_load() function if they wish to load a
75 configuration file themselves and have finer control over how errors are
76 treated.
77
78 =head1 RETURN VALUES
79
80 These functions return 1 for success and a zero or negative value for
81 failure. If module errors are not ignored the return code will reflect the
82 return value of the failing module (this will always be zero or negative).
83
84 =head1 EXAMPLES
85
86 Load a configuration file and print out any errors and exit (missing file
87 considered fatal):
88
89  if (CONF_modules_load_file_with_libctx(libctx, NULL, NULL, 0) <= 0) {
90      fprintf(stderr, "FATAL: error loading configuration file\n");
91      ERR_print_errors_fp(stderr);
92      exit(1);
93  }
94
95 Load default configuration file using the section indicated by "myapp",
96 tolerate missing files, but exit on other errors:
97
98  if (CONF_modules_load_file_with_libctx(NULL, NULL, "myapp",
99                                         CONF_MFLAGS_IGNORE_MISSING_FILE) <= 0) {
100      fprintf(stderr, "FATAL: error loading configuration file\n");
101      ERR_print_errors_fp(stderr);
102      exit(1);
103  }
104
105 Load custom configuration file and section, only print warnings on error,
106 missing configuration file ignored:
107
108  if (CONF_modules_load_file_with_libctx(NULL, "/something/app.cnf", "myapp",
109                                         CONF_MFLAGS_IGNORE_MISSING_FILE) <= 0) {
110      fprintf(stderr, "WARNING: error loading configuration file\n");
111      ERR_print_errors_fp(stderr);
112  }
113
114 Load and parse configuration file manually, custom error handling:
115
116  FILE *fp;
117  CONF *cnf = NULL;
118  long eline;
119
120  fp = fopen("/somepath/app.cnf", "r");
121  if (fp == NULL) {
122      fprintf(stderr, "Error opening configuration file\n");
123      /* Other missing configuration file behaviour */
124  } else {
125      cnf = NCONF_new_with_libctx(libctx, NULL);
126      if (NCONF_load_fp(cnf, fp, &eline) == 0) {
127          fprintf(stderr, "Error on line %ld of configuration file\n", eline);
128          ERR_print_errors_fp(stderr);
129          /* Other malformed configuration file behaviour */
130      } else if (CONF_modules_load(cnf, "appname", 0) <= 0) {
131          fprintf(stderr, "Error configuring application\n");
132          ERR_print_errors_fp(stderr);
133          /* Other configuration error behaviour */
134      }
135      fclose(fp);
136      NCONF_free(cnf);
137  }
138
139 =head1 SEE ALSO
140
141 L<config(5)>,
142 L<OPENSSL_config(3)>,
143 L<NCONF_new_with_libctx(3)>
144
145 =head1 COPYRIGHT
146
147 Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved.
148
149 Licensed under the Apache License 2.0 (the "License").  You may not use
150 this file except in compliance with the License.  You can obtain a copy
151 in the file LICENSE in the source distribution or at
152 L<https://www.openssl.org/source/license.html>.
153
154 =cut