CONF_modules_load_file.pod 6.1 KB

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