1*0Sstevel@tonic-gate /* conf_mod.c */ 2*0Sstevel@tonic-gate /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL 3*0Sstevel@tonic-gate * project 2001. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate /* ==================================================================== 6*0Sstevel@tonic-gate * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 9*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 10*0Sstevel@tonic-gate * are met: 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 14*0Sstevel@tonic-gate * 15*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 16*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 17*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 18*0Sstevel@tonic-gate * distribution. 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 21*0Sstevel@tonic-gate * software must display the following acknowledgment: 22*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 23*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24*0Sstevel@tonic-gate * 25*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26*0Sstevel@tonic-gate * endorse or promote products derived from this software without 27*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 28*0Sstevel@tonic-gate * licensing@OpenSSL.org. 29*0Sstevel@tonic-gate * 30*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 31*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 32*0Sstevel@tonic-gate * permission of the OpenSSL Project. 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 35*0Sstevel@tonic-gate * acknowledgment: 36*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 37*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38*0Sstevel@tonic-gate * 39*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 51*0Sstevel@tonic-gate * ==================================================================== 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 54*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 55*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #include <stdio.h> 60*0Sstevel@tonic-gate #include <ctype.h> 61*0Sstevel@tonic-gate #include <openssl/crypto.h> 62*0Sstevel@tonic-gate #include "cryptlib.h" 63*0Sstevel@tonic-gate #include <openssl/conf.h> 64*0Sstevel@tonic-gate #include <openssl/dso.h> 65*0Sstevel@tonic-gate #include <openssl/x509.h> 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate #define DSO_mod_init_name "OPENSSL_init" 69*0Sstevel@tonic-gate #define DSO_mod_finish_name "OPENSSL_finish" 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* This structure contains a data about supported modules. 73*0Sstevel@tonic-gate * entries in this table correspond to either dynamic or 74*0Sstevel@tonic-gate * static modules. 75*0Sstevel@tonic-gate */ 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate struct conf_module_st 78*0Sstevel@tonic-gate { 79*0Sstevel@tonic-gate /* DSO of this module or NULL if static */ 80*0Sstevel@tonic-gate DSO *dso; 81*0Sstevel@tonic-gate /* Name of the module */ 82*0Sstevel@tonic-gate char *name; 83*0Sstevel@tonic-gate /* Init function */ 84*0Sstevel@tonic-gate conf_init_func *init; 85*0Sstevel@tonic-gate /* Finish function */ 86*0Sstevel@tonic-gate conf_finish_func *finish; 87*0Sstevel@tonic-gate /* Number of successfully initialized modules */ 88*0Sstevel@tonic-gate int links; 89*0Sstevel@tonic-gate void *usr_data; 90*0Sstevel@tonic-gate }; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* This structure contains information about modules that have been 94*0Sstevel@tonic-gate * successfully initialized. There may be more than one entry for a 95*0Sstevel@tonic-gate * given module. 96*0Sstevel@tonic-gate */ 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate struct conf_imodule_st 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate CONF_MODULE *pmod; 101*0Sstevel@tonic-gate char *name; 102*0Sstevel@tonic-gate char *value; 103*0Sstevel@tonic-gate unsigned long flags; 104*0Sstevel@tonic-gate void *usr_data; 105*0Sstevel@tonic-gate }; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate static STACK_OF(CONF_MODULE) *supported_modules = NULL; 108*0Sstevel@tonic-gate static STACK_OF(CONF_IMODULE) *initialized_modules = NULL; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate static void module_free(CONF_MODULE *md); 111*0Sstevel@tonic-gate static void module_finish(CONF_IMODULE *imod); 112*0Sstevel@tonic-gate static int module_run(const CONF *cnf, char *name, char *value, 113*0Sstevel@tonic-gate unsigned long flags); 114*0Sstevel@tonic-gate static CONF_MODULE *module_add(DSO *dso, const char *name, 115*0Sstevel@tonic-gate conf_init_func *ifunc, conf_finish_func *ffunc); 116*0Sstevel@tonic-gate static CONF_MODULE *module_find(char *name); 117*0Sstevel@tonic-gate static int module_init(CONF_MODULE *pmod, char *name, char *value, 118*0Sstevel@tonic-gate const CONF *cnf); 119*0Sstevel@tonic-gate static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value, 120*0Sstevel@tonic-gate unsigned long flags); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* Main function: load modules from a CONF structure */ 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate int CONF_modules_load(const CONF *cnf, const char *appname, 125*0Sstevel@tonic-gate unsigned long flags) 126*0Sstevel@tonic-gate { 127*0Sstevel@tonic-gate STACK_OF(CONF_VALUE) *values; 128*0Sstevel@tonic-gate CONF_VALUE *vl; 129*0Sstevel@tonic-gate char *vsection; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate int ret, i; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate if (!cnf) 134*0Sstevel@tonic-gate return 1; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate if (appname == NULL) 137*0Sstevel@tonic-gate appname = "openssl_conf"; 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate vsection = NCONF_get_string(cnf, NULL, appname); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate if (!vsection) 142*0Sstevel@tonic-gate { 143*0Sstevel@tonic-gate ERR_clear_error(); 144*0Sstevel@tonic-gate return 1; 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate values = NCONF_get_section(cnf, vsection); 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if (!values) 150*0Sstevel@tonic-gate return 0; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate for (i = 0; i < sk_CONF_VALUE_num(values); i++) 153*0Sstevel@tonic-gate { 154*0Sstevel@tonic-gate vl = sk_CONF_VALUE_value(values, i); 155*0Sstevel@tonic-gate ret = module_run(cnf, vl->name, vl->value, flags); 156*0Sstevel@tonic-gate if (ret <= 0) 157*0Sstevel@tonic-gate if(!(flags & CONF_MFLAGS_IGNORE_ERRORS)) 158*0Sstevel@tonic-gate return ret; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate return 1; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate int CONF_modules_load_file(const char *filename, const char *appname, 166*0Sstevel@tonic-gate unsigned long flags) 167*0Sstevel@tonic-gate { 168*0Sstevel@tonic-gate char *file = NULL; 169*0Sstevel@tonic-gate CONF *conf = NULL; 170*0Sstevel@tonic-gate int ret = 0; 171*0Sstevel@tonic-gate conf = NCONF_new(NULL); 172*0Sstevel@tonic-gate if (!conf) 173*0Sstevel@tonic-gate goto err; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate if (filename == NULL) 176*0Sstevel@tonic-gate { 177*0Sstevel@tonic-gate file = CONF_get1_default_config_file(); 178*0Sstevel@tonic-gate if (!file) 179*0Sstevel@tonic-gate goto err; 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate else 182*0Sstevel@tonic-gate file = (char *)filename; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate if (NCONF_load(conf, file, NULL) <= 0) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) && 187*0Sstevel@tonic-gate (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) 188*0Sstevel@tonic-gate { 189*0Sstevel@tonic-gate ERR_clear_error(); 190*0Sstevel@tonic-gate ret = 1; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate goto err; 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate ret = CONF_modules_load(conf, appname, flags); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate err: 198*0Sstevel@tonic-gate if (filename == NULL) 199*0Sstevel@tonic-gate OPENSSL_free(file); 200*0Sstevel@tonic-gate NCONF_free(conf); 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate return ret; 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate static int module_run(const CONF *cnf, char *name, char *value, 206*0Sstevel@tonic-gate unsigned long flags) 207*0Sstevel@tonic-gate { 208*0Sstevel@tonic-gate CONF_MODULE *md; 209*0Sstevel@tonic-gate int ret; 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate md = module_find(name); 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* Module not found: try to load DSO */ 214*0Sstevel@tonic-gate if (!md && !(flags & CONF_MFLAGS_NO_DSO)) 215*0Sstevel@tonic-gate md = module_load_dso(cnf, name, value, flags); 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate if (!md) 218*0Sstevel@tonic-gate { 219*0Sstevel@tonic-gate if (!(flags & CONF_MFLAGS_SILENT)) 220*0Sstevel@tonic-gate { 221*0Sstevel@tonic-gate CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME); 222*0Sstevel@tonic-gate ERR_add_error_data(2, "module=", name); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate return -1; 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate ret = module_init(md, name, value, cnf); 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate if (ret <= 0) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate if (!(flags & CONF_MFLAGS_SILENT)) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate char rcode[DECIMAL_SIZE(ret)+1]; 234*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR); 235*0Sstevel@tonic-gate BIO_snprintf(rcode, sizeof rcode, "%-8d", ret); 236*0Sstevel@tonic-gate ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode); 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate return ret; 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate /* Load a module from a DSO */ 244*0Sstevel@tonic-gate static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value, 245*0Sstevel@tonic-gate unsigned long flags) 246*0Sstevel@tonic-gate { 247*0Sstevel@tonic-gate DSO *dso = NULL; 248*0Sstevel@tonic-gate conf_init_func *ifunc; 249*0Sstevel@tonic-gate conf_finish_func *ffunc; 250*0Sstevel@tonic-gate char *path = NULL; 251*0Sstevel@tonic-gate int errcode = 0; 252*0Sstevel@tonic-gate CONF_MODULE *md; 253*0Sstevel@tonic-gate /* Look for alternative path in module section */ 254*0Sstevel@tonic-gate path = NCONF_get_string(cnf, value, "path"); 255*0Sstevel@tonic-gate if (!path) 256*0Sstevel@tonic-gate { 257*0Sstevel@tonic-gate ERR_get_error(); 258*0Sstevel@tonic-gate path = name; 259*0Sstevel@tonic-gate } 260*0Sstevel@tonic-gate dso = DSO_load(NULL, path, NULL, 0); 261*0Sstevel@tonic-gate if (!dso) 262*0Sstevel@tonic-gate { 263*0Sstevel@tonic-gate errcode = CONF_R_ERROR_LOADING_DSO; 264*0Sstevel@tonic-gate goto err; 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name); 267*0Sstevel@tonic-gate if (!ifunc) 268*0Sstevel@tonic-gate { 269*0Sstevel@tonic-gate errcode = CONF_R_MISSING_INIT_FUNCTION; 270*0Sstevel@tonic-gate goto err; 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name); 273*0Sstevel@tonic-gate /* All OK, add module */ 274*0Sstevel@tonic-gate md = module_add(dso, name, ifunc, ffunc); 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate if (!md) 277*0Sstevel@tonic-gate goto err; 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate return md; 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate err: 282*0Sstevel@tonic-gate if (dso) 283*0Sstevel@tonic-gate DSO_free(dso); 284*0Sstevel@tonic-gate CONFerr(CONF_F_MODULE_LOAD_DSO, errcode); 285*0Sstevel@tonic-gate ERR_add_error_data(4, "module=", name, ", path=", path); 286*0Sstevel@tonic-gate return NULL; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate /* add module to list */ 290*0Sstevel@tonic-gate static CONF_MODULE *module_add(DSO *dso, const char *name, 291*0Sstevel@tonic-gate conf_init_func *ifunc, conf_finish_func *ffunc) 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate CONF_MODULE *tmod = NULL; 294*0Sstevel@tonic-gate if (supported_modules == NULL) 295*0Sstevel@tonic-gate supported_modules = sk_CONF_MODULE_new_null(); 296*0Sstevel@tonic-gate if (supported_modules == NULL) 297*0Sstevel@tonic-gate return NULL; 298*0Sstevel@tonic-gate tmod = OPENSSL_malloc(sizeof(CONF_MODULE)); 299*0Sstevel@tonic-gate if (tmod == NULL) 300*0Sstevel@tonic-gate return NULL; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate tmod->dso = dso; 303*0Sstevel@tonic-gate tmod->name = BUF_strdup(name); 304*0Sstevel@tonic-gate tmod->init = ifunc; 305*0Sstevel@tonic-gate tmod->finish = ffunc; 306*0Sstevel@tonic-gate tmod->links = 0; 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate if (!sk_CONF_MODULE_push(supported_modules, tmod)) 309*0Sstevel@tonic-gate { 310*0Sstevel@tonic-gate OPENSSL_free(tmod); 311*0Sstevel@tonic-gate return NULL; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate return tmod; 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate /* Find a module from the list. We allow module names of the 318*0Sstevel@tonic-gate * form modname.XXXX to just search for modname to allow the 319*0Sstevel@tonic-gate * same module to be initialized more than once. 320*0Sstevel@tonic-gate */ 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate static CONF_MODULE *module_find(char *name) 323*0Sstevel@tonic-gate { 324*0Sstevel@tonic-gate CONF_MODULE *tmod; 325*0Sstevel@tonic-gate int i, nchar; 326*0Sstevel@tonic-gate char *p; 327*0Sstevel@tonic-gate p = strrchr(name, '.'); 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate if (p) 330*0Sstevel@tonic-gate nchar = p - name; 331*0Sstevel@tonic-gate else 332*0Sstevel@tonic-gate nchar = strlen(name); 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) 335*0Sstevel@tonic-gate { 336*0Sstevel@tonic-gate tmod = sk_CONF_MODULE_value(supported_modules, i); 337*0Sstevel@tonic-gate if (!strncmp(tmod->name, name, nchar)) 338*0Sstevel@tonic-gate return tmod; 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate return NULL; 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate /* initialize a module */ 346*0Sstevel@tonic-gate static int module_init(CONF_MODULE *pmod, char *name, char *value, 347*0Sstevel@tonic-gate const CONF *cnf) 348*0Sstevel@tonic-gate { 349*0Sstevel@tonic-gate int ret = 1; 350*0Sstevel@tonic-gate int init_called = 0; 351*0Sstevel@tonic-gate CONF_IMODULE *imod = NULL; 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate /* Otherwise add initialized module to list */ 354*0Sstevel@tonic-gate imod = OPENSSL_malloc(sizeof(CONF_IMODULE)); 355*0Sstevel@tonic-gate if (!imod) 356*0Sstevel@tonic-gate goto err; 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate imod->pmod = pmod; 359*0Sstevel@tonic-gate imod->name = BUF_strdup(name); 360*0Sstevel@tonic-gate imod->value = BUF_strdup(value); 361*0Sstevel@tonic-gate imod->usr_data = NULL; 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate if (!imod->name || !imod->value) 364*0Sstevel@tonic-gate goto memerr; 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate /* Try to initialize module */ 367*0Sstevel@tonic-gate if(pmod->init) 368*0Sstevel@tonic-gate { 369*0Sstevel@tonic-gate ret = pmod->init(imod, cnf); 370*0Sstevel@tonic-gate init_called = 1; 371*0Sstevel@tonic-gate /* Error occurred, exit */ 372*0Sstevel@tonic-gate if (ret <= 0) 373*0Sstevel@tonic-gate goto err; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate if (initialized_modules == NULL) 377*0Sstevel@tonic-gate { 378*0Sstevel@tonic-gate initialized_modules = sk_CONF_IMODULE_new_null(); 379*0Sstevel@tonic-gate if (!initialized_modules) 380*0Sstevel@tonic-gate { 381*0Sstevel@tonic-gate CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE); 382*0Sstevel@tonic-gate goto err; 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate if (!sk_CONF_IMODULE_push(initialized_modules, imod)) 387*0Sstevel@tonic-gate { 388*0Sstevel@tonic-gate CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE); 389*0Sstevel@tonic-gate goto err; 390*0Sstevel@tonic-gate } 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate pmod->links++; 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate return ret; 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate err: 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate /* We've started the module so we'd better finish it */ 399*0Sstevel@tonic-gate if (pmod->finish && init_called) 400*0Sstevel@tonic-gate pmod->finish(imod); 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate memerr: 403*0Sstevel@tonic-gate if (imod) 404*0Sstevel@tonic-gate { 405*0Sstevel@tonic-gate if (imod->name) 406*0Sstevel@tonic-gate OPENSSL_free(imod->name); 407*0Sstevel@tonic-gate if (imod->value) 408*0Sstevel@tonic-gate OPENSSL_free(imod->value); 409*0Sstevel@tonic-gate OPENSSL_free(imod); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate return -1; 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate } 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gate /* Unload any dynamic modules that have a link count of zero: 417*0Sstevel@tonic-gate * i.e. have no active initialized modules. If 'all' is set 418*0Sstevel@tonic-gate * then all modules are unloaded including static ones. 419*0Sstevel@tonic-gate */ 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate void CONF_modules_unload(int all) 422*0Sstevel@tonic-gate { 423*0Sstevel@tonic-gate int i; 424*0Sstevel@tonic-gate CONF_MODULE *md; 425*0Sstevel@tonic-gate CONF_modules_finish(); 426*0Sstevel@tonic-gate /* unload modules in reverse order */ 427*0Sstevel@tonic-gate for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) 428*0Sstevel@tonic-gate { 429*0Sstevel@tonic-gate md = sk_CONF_MODULE_value(supported_modules, i); 430*0Sstevel@tonic-gate /* If static or in use and 'all' not set ignore it */ 431*0Sstevel@tonic-gate if (((md->links > 0) || !md->dso) && !all) 432*0Sstevel@tonic-gate continue; 433*0Sstevel@tonic-gate /* Since we're working in reverse this is OK */ 434*0Sstevel@tonic-gate sk_CONF_MODULE_delete(supported_modules, i); 435*0Sstevel@tonic-gate module_free(md); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate if (sk_CONF_MODULE_num(supported_modules) == 0) 438*0Sstevel@tonic-gate { 439*0Sstevel@tonic-gate sk_CONF_MODULE_free(supported_modules); 440*0Sstevel@tonic-gate supported_modules = NULL; 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate /* unload a single module */ 445*0Sstevel@tonic-gate static void module_free(CONF_MODULE *md) 446*0Sstevel@tonic-gate { 447*0Sstevel@tonic-gate if (md->dso) 448*0Sstevel@tonic-gate DSO_free(md->dso); 449*0Sstevel@tonic-gate OPENSSL_free(md->name); 450*0Sstevel@tonic-gate OPENSSL_free(md); 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate /* finish and free up all modules instances */ 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gate void CONF_modules_finish(void) 456*0Sstevel@tonic-gate { 457*0Sstevel@tonic-gate CONF_IMODULE *imod; 458*0Sstevel@tonic-gate while (sk_CONF_IMODULE_num(initialized_modules) > 0) 459*0Sstevel@tonic-gate { 460*0Sstevel@tonic-gate imod = sk_CONF_IMODULE_pop(initialized_modules); 461*0Sstevel@tonic-gate module_finish(imod); 462*0Sstevel@tonic-gate } 463*0Sstevel@tonic-gate sk_CONF_IMODULE_free(initialized_modules); 464*0Sstevel@tonic-gate initialized_modules = NULL; 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate /* finish a module instance */ 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate static void module_finish(CONF_IMODULE *imod) 470*0Sstevel@tonic-gate { 471*0Sstevel@tonic-gate if (imod->pmod->finish) 472*0Sstevel@tonic-gate imod->pmod->finish(imod); 473*0Sstevel@tonic-gate imod->pmod->links--; 474*0Sstevel@tonic-gate OPENSSL_free(imod->name); 475*0Sstevel@tonic-gate OPENSSL_free(imod->value); 476*0Sstevel@tonic-gate OPENSSL_free(imod); 477*0Sstevel@tonic-gate } 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate /* Add a static module to OpenSSL */ 480*0Sstevel@tonic-gate 481*0Sstevel@tonic-gate int CONF_module_add(const char *name, conf_init_func *ifunc, 482*0Sstevel@tonic-gate conf_finish_func *ffunc) 483*0Sstevel@tonic-gate { 484*0Sstevel@tonic-gate if (module_add(NULL, name, ifunc, ffunc)) 485*0Sstevel@tonic-gate return 1; 486*0Sstevel@tonic-gate else 487*0Sstevel@tonic-gate return 0; 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate void CONF_modules_free(void) 491*0Sstevel@tonic-gate { 492*0Sstevel@tonic-gate CONF_modules_finish(); 493*0Sstevel@tonic-gate CONF_modules_unload(1); 494*0Sstevel@tonic-gate } 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate /* Utility functions */ 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate const char *CONF_imodule_get_name(const CONF_IMODULE *md) 499*0Sstevel@tonic-gate { 500*0Sstevel@tonic-gate return md->name; 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate const char *CONF_imodule_get_value(const CONF_IMODULE *md) 504*0Sstevel@tonic-gate { 505*0Sstevel@tonic-gate return md->value; 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate void *CONF_imodule_get_usr_data(const CONF_IMODULE *md) 509*0Sstevel@tonic-gate { 510*0Sstevel@tonic-gate return md->usr_data; 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data) 514*0Sstevel@tonic-gate { 515*0Sstevel@tonic-gate md->usr_data = usr_data; 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md) 519*0Sstevel@tonic-gate { 520*0Sstevel@tonic-gate return md->pmod; 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md) 524*0Sstevel@tonic-gate { 525*0Sstevel@tonic-gate return md->flags; 526*0Sstevel@tonic-gate } 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gate void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags) 529*0Sstevel@tonic-gate { 530*0Sstevel@tonic-gate md->flags = flags; 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate void *CONF_module_get_usr_data(CONF_MODULE *pmod) 534*0Sstevel@tonic-gate { 535*0Sstevel@tonic-gate return pmod->usr_data; 536*0Sstevel@tonic-gate } 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data) 539*0Sstevel@tonic-gate { 540*0Sstevel@tonic-gate pmod->usr_data = usr_data; 541*0Sstevel@tonic-gate } 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate /* Return default config file name */ 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate char *CONF_get1_default_config_file(void) 546*0Sstevel@tonic-gate { 547*0Sstevel@tonic-gate char *file; 548*0Sstevel@tonic-gate int len; 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate file = getenv("OPENSSL_CONF"); 551*0Sstevel@tonic-gate if (file) 552*0Sstevel@tonic-gate return BUF_strdup(file); 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate len = strlen(X509_get_default_cert_area()); 555*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS 556*0Sstevel@tonic-gate len++; 557*0Sstevel@tonic-gate #endif 558*0Sstevel@tonic-gate len += strlen(OPENSSL_CONF); 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate file = OPENSSL_malloc(len + 1); 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate if (!file) 563*0Sstevel@tonic-gate return NULL; 564*0Sstevel@tonic-gate BUF_strlcpy(file,X509_get_default_cert_area(),len + 1); 565*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS 566*0Sstevel@tonic-gate BUF_strlcat(file,"/",len + 1); 567*0Sstevel@tonic-gate #endif 568*0Sstevel@tonic-gate BUF_strlcat(file,OPENSSL_CONF,len + 1); 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate return file; 571*0Sstevel@tonic-gate } 572*0Sstevel@tonic-gate 573*0Sstevel@tonic-gate /* This function takes a list separated by 'sep' and calls the 574*0Sstevel@tonic-gate * callback function giving the start and length of each member 575*0Sstevel@tonic-gate * optionally stripping leading and trailing whitespace. This can 576*0Sstevel@tonic-gate * be used to parse comma separated lists for example. 577*0Sstevel@tonic-gate */ 578*0Sstevel@tonic-gate 579*0Sstevel@tonic-gate int CONF_parse_list(const char *list_, int sep, int nospc, 580*0Sstevel@tonic-gate int (*list_cb)(const char *elem, int len, void *usr), void *arg) 581*0Sstevel@tonic-gate { 582*0Sstevel@tonic-gate int ret; 583*0Sstevel@tonic-gate const char *lstart, *tmpend, *p; 584*0Sstevel@tonic-gate lstart = list_; 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate for(;;) 587*0Sstevel@tonic-gate { 588*0Sstevel@tonic-gate if (nospc) 589*0Sstevel@tonic-gate { 590*0Sstevel@tonic-gate while(*lstart && isspace((unsigned char)*lstart)) 591*0Sstevel@tonic-gate lstart++; 592*0Sstevel@tonic-gate } 593*0Sstevel@tonic-gate p = strchr(lstart, sep); 594*0Sstevel@tonic-gate if (p == lstart || !*lstart) 595*0Sstevel@tonic-gate ret = list_cb(NULL, 0, arg); 596*0Sstevel@tonic-gate else 597*0Sstevel@tonic-gate { 598*0Sstevel@tonic-gate if (p) 599*0Sstevel@tonic-gate tmpend = p - 1; 600*0Sstevel@tonic-gate else 601*0Sstevel@tonic-gate tmpend = lstart + strlen(lstart) - 1; 602*0Sstevel@tonic-gate if (nospc) 603*0Sstevel@tonic-gate { 604*0Sstevel@tonic-gate while(isspace((unsigned char)*tmpend)) 605*0Sstevel@tonic-gate tmpend--; 606*0Sstevel@tonic-gate } 607*0Sstevel@tonic-gate ret = list_cb(lstart, tmpend - lstart + 1, arg); 608*0Sstevel@tonic-gate } 609*0Sstevel@tonic-gate if (ret <= 0) 610*0Sstevel@tonic-gate return ret; 611*0Sstevel@tonic-gate if (p == NULL) 612*0Sstevel@tonic-gate return 1; 613*0Sstevel@tonic-gate lstart = p + 1; 614*0Sstevel@tonic-gate } 615*0Sstevel@tonic-gate } 616*0Sstevel@tonic-gate 617