We're getting a clash with C++ because it has a type called 'list'.
[openssl.git] / crypto / conf / conf_mod.c
index 234eb7a06e52552594de7c056755f23848b45527..8ceab6a21f8fcf27100822e1930d88872459ad06 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <stdio.h>
+#include <ctype.h>
 #include <openssl/crypto.h>
 #include "cryptlib.h"
 #include <openssl/conf.h>
@@ -129,9 +130,11 @@ int CONF_modules_load(const CONF *cnf, const char *appname,
 
        int ret, i;
 
-       if (!cnf || !appname)
+       if (!cnf)
                return 1;
 
+       if (appname == NULL)
+               appname = "openssl_conf";
 
        vsection = NCONF_get_string(cnf, NULL, appname); 
 
@@ -162,18 +165,38 @@ int CONF_modules_load(const CONF *cnf, const char *appname,
 int CONF_modules_load_file(const char *filename, const char *appname,
                           unsigned long flags)
        {
+       char *file = NULL;
        CONF *conf = NULL;
        int ret = 0;
        conf = NCONF_new(NULL);
        if (!conf)
                goto err;
 
-       if (NCONF_load(conf, filename, NULL) <= 0)
+       if (filename == NULL)
+               {
+               file = CONF_get1_default_config_file();
+               if (!file)
+                       goto err;
+               }
+       else
+               file = (char *)filename;
+
+       if (NCONF_load(conf, file, NULL) <= 0)
+               {
+               if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
+                 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
+                       {
+                       ERR_clear_error();
+                       ret = 1;
+                       }
                goto err;
+               }
 
        ret = CONF_modules_load(conf, appname, flags);
 
        err:
+       if (filename == NULL)
+               OPENSSL_free(file);
        NCONF_free(conf);
 
        return ret;
@@ -188,7 +211,7 @@ static int module_run(const CONF *cnf, char *name, char *value,
        md = module_find(name);
 
        /* Module not found: try to load DSO */
-       if (!md)
+       if (!md && !(flags & CONF_MFLAGS_NO_DSO))
                md = module_load_dso(cnf, name, value, flags);
 
        if (!md)
@@ -207,7 +230,7 @@ static int module_run(const CONF *cnf, char *name, char *value,
                {
                if (!(flags & CONF_MFLAGS_SILENT))
                        {
-                       char rcode[10];
+                       char rcode[DECIMAL_SIZE(ret)+1];
                        CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
                        sprintf(rcode, "%-8d", ret);
                        ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
@@ -247,11 +270,6 @@ static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
                goto err;
                }
         ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
-       if (!ffunc)
-               {
-               errcode = CONF_R_MISSING_FINISH_FUNCTION;
-               goto err;
-               }
        /* All OK, add module */
        md = module_add(dso, name, ifunc, ffunc);
 
@@ -404,6 +422,7 @@ void CONF_modules_unload(int all)
        {
        int i;
        CONF_MODULE *md;
+       CONF_modules_finish();
        /* unload modules in reverse order */
        for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
                {
@@ -449,7 +468,8 @@ void CONF_modules_finish(void)
 
 static void module_finish(CONF_IMODULE *imod)
        {
-       imod->pmod->finish(imod);
+       if (imod->pmod->finish)
+               imod->pmod->finish(imod);
        imod->pmod->links--;
        OPENSSL_free(imod->name);
        OPENSSL_free(imod->value);
@@ -549,3 +569,48 @@ char *CONF_get1_default_config_file(void)
 
        return file;
        }
+
+/* This function takes a list separated by 'sep' and calls the
+ * callback function giving the start and length of each member
+ * optionally stripping leading and trailing whitespace. This can
+ * be used to parse comma separated lists for example.
+ */
+
+int CONF_parse_list(const char *list_, int sep, int nospc,
+       int (*list_cb)(const char *elem, int len, void *usr), void *arg)
+       {
+       int ret;
+       const char *lstart, *tmpend, *p;
+       lstart = list_;
+
+       for(;;)
+               {
+               if (nospc)
+                       {
+                       while(*lstart && isspace((unsigned char)*lstart))
+                               lstart++;
+                       }
+               p = strchr(lstart, sep);
+               if (p == lstart || !*lstart)
+                       ret = list_cb(NULL, 0, arg);
+               else
+                       {
+                       if (p)
+                               tmpend = p - 1;
+                       else 
+                               tmpend = lstart + strlen(lstart) - 1;
+                       if (nospc)
+                               {
+                               while(isspace((unsigned char)*tmpend))
+                                       tmpend--;
+                               }
+                       ret = list_cb(lstart, tmpend - lstart + 1, arg);
+                       }
+               if (ret <= 0)
+                       return ret;
+               if (p == NULL)
+                       return 1;
+               lstart = p + 1;
+               }
+       }
+