Commit cc01d217 authored by Rich Salz's avatar Rich Salz Committed by Rich Salz
Browse files

RT3876: Only load config when needed



Create app_load_config(), a routine to load config file.  Remove the
"always load config" from the main app.  Change the places that used to
load config to call the new common routine.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent f097f81c
Loading
Loading
Loading
Loading
+31 −16
Original line number Diff line number Diff line
@@ -496,6 +496,33 @@ static char *app_get_pass(char *arg, int keepbio)
    return BUF_strdup(tpass);
}

CONF *app_load_config(const char *filename)
{
    long errorline = -1;
    CONF *conf;
    int i;
    BIO *in;

    in = bio_open_default(filename, "r");
    if (in == NULL)
        return NULL;

    conf = NCONF_new(NULL);
    i = NCONF_load_bio(conf, in, &errorline);
    BIO_free(in);
    if (i > 0)
        return conf;

    if (errorline <= 0)
        BIO_printf(bio_err, "%s: Can't load config file \"%s\"\n",
                   opt_getprog(), filename);
    else
        BIO_printf(bio_err, "%s: Error on line %ld of config file \"%s\"\n",
                   opt_getprog(), errorline, filename);
    NCONF_free(conf);
    return NULL;
}

int add_oid_section(CONF *conf)
{
    char *p;
@@ -1559,8 +1586,7 @@ CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
    TXT_DB *tmpdb = NULL;
    BIO *in;
    CONF *dbattr_conf = NULL;
    char buf[1][BSIZE];
    long errorline = -1;
    char buf[BSIZE];

    in = BIO_new_file(dbfile, "r");
    if (in == NULL) {
@@ -1571,22 +1597,11 @@ CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
        goto err;

#ifndef OPENSSL_SYS_VMS
    BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
    BIO_snprintf(buf, sizeof buf, "%s.attr", dbfile);
#else
    BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
    BIO_snprintf(buf, sizeof buf, "%s-attr", dbfile);
#endif
    dbattr_conf = NCONF_new(NULL);
    if (NCONF_load(dbattr_conf, buf[0], &errorline) <= 0) {
        if (errorline > 0) {
            BIO_printf(bio_err,
                       "error on line %ld of db attribute file '%s'\n",
                       errorline, buf[0]);
            goto err;
        } else {
            NCONF_free(dbattr_conf);
            dbattr_conf = NULL;
        }
    }
    dbattr_conf = app_load_config(buf);

    retdb = app_malloc(sizeof(*retdb), "new DB");
    retdb->db = tmpdb;
+1 −1
Original line number Diff line number Diff line
@@ -147,7 +147,6 @@ long app_RAND_load_files(char *file); /* `file' is a list of files to read,
                                       * (see e_os.h).  The string is
                                       * destroyed! */

extern CONF *config;
extern char *default_config_file;
extern BIO *bio_in;
extern BIO *bio_out;
@@ -155,6 +154,7 @@ extern BIO *bio_err;
BIO *dup_bio_in(void);
BIO *dup_bio_out(void);
BIO *bio_open_default(const char *filename, const char *mode);
CONF *app_load_config(const char* filename);
void unbuffer(FILE *fp);

/* Often used in calls to bio_open_default. */
+2 −14
Original line number Diff line number Diff line
@@ -334,14 +334,12 @@ static int do_generate(char *genstr, char *genconf, BUF_MEM *buf)
{
    CONF *cnf = NULL;
    int len;
    long errline = 0;
    unsigned char *p;
    ASN1_TYPE *atyp = NULL;

    if (genconf) {
        cnf = NCONF_new(NULL);
        if (!NCONF_load(cnf, genconf, &errline))
            goto conferr;
        if ((cnf = app_load_config(genconf)) == NULL)
            goto err;
        if (!genstr)
            genstr = NCONF_get_string(cnf, "default", "asn1");
        if (!genstr) {
@@ -372,18 +370,8 @@ static int do_generate(char *genstr, char *genconf, BUF_MEM *buf)
    ASN1_TYPE_free(atyp);
    return len;

 conferr:

    if (errline > 0)
        BIO_printf(bio_err, "Error on line %ld of config file '%s'\n",
                   errline, genconf);
    else
        BIO_printf(bio_err, "Error loading config file '%s'\n", genconf);

 err:
    NCONF_free(cnf);
    ASN1_TYPE_free(atyp);

    return -1;

}
+6 −45
Original line number Diff line number Diff line
@@ -99,7 +99,6 @@
#define BSIZE 256

#define BASE_SECTION    "ca"
#define CONFIG_FILE "openssl.cnf"

#define ENV_DEFAULT_CA          "default_ca"

@@ -285,7 +284,8 @@ int ca_main(int argc, char **argv)
    STACK_OF(X509) *cert_sk = NULL;
    X509_CRL *crl = NULL;
    const EVP_MD *dgst = NULL;
    char *configfile = NULL, *md = NULL, *policy = NULL, *keyfile = NULL;
    char *configfile = default_config_file;
    char *md = NULL, *policy = NULL, *keyfile = NULL;
    char *certfile = NULL, *crl_ext = NULL, *crlnumberfile = NULL;
    char *infile = NULL, *spkac_file = NULL, *ss_cert_file = NULL;
    char *extensions = NULL, *extfile = NULL, *key = NULL, *passinarg = NULL;
@@ -301,7 +301,7 @@ int ca_main(int argc, char **argv)
    int keyformat = FORMAT_PEM, multirdn = 0, notext = 0, output_der = 0;
    int ret = 1, email_dn = 1, req = 0, verbose = 0, gencrl = 0, dorevoke = 0;
    int i, j, rev_type = REV_NONE, selfsign = 0;
    long crldays = 0, crlhours = 0, crlsec = 0, errorline = -1, days = 0;
    long crldays = 0, crlhours = 0, crlsec = 0, days = 0;
    unsigned long chtype = MBSTRING_ASC, nameopt = 0, certopt = 0;
    X509 *x509 = NULL, *x509p = NULL, *x = NULL;
    X509_REVOKED *r = NULL;
@@ -482,40 +482,9 @@ end_of_options:
    argc = opt_num_rest();
    argv = opt_rest();

    tofree = NULL;
    if (configfile == NULL)
        configfile = getenv("OPENSSL_CONF");
    if (configfile == NULL)
        configfile = getenv("SSLEAY_CONF");
    if (configfile == NULL) {
        const char *s = X509_get_default_cert_area();
        size_t len;

        len = strlen(s) + 1 + sizeof(CONFIG_FILE);
        tofree = app_malloc(len, "config filename");
#ifdef OPENSSL_SYS_VMS
        strcpy(tofree, s);
#else
        BUF_strlcpy(tofree, s, len);
        BUF_strlcat(tofree, "/", len);
#endif
        BUF_strlcat(tofree, CONFIG_FILE, len);
        configfile = tofree;
    }

    BIO_printf(bio_err, "Using configuration from %s\n", configfile);
    conf = NCONF_new(NULL);
    if (NCONF_load(conf, configfile, &errorline) <= 0) {
        if (errorline <= 0)
            BIO_printf(bio_err, "error loading the config file '%s'\n",
                       configfile);
        else
            BIO_printf(bio_err, "error on line %ld of config file '%s'\n",
                       errorline, configfile);
    if ((conf = app_load_config(configfile)) == NULL)
        goto end;
    }
    OPENSSL_free(tofree);
    tofree = NULL;

    /* Lets get the config section we are using */
    if (section == NULL) {
@@ -803,15 +772,7 @@ end_of_options:
    /*****************************************************************/
    /* Read extensions config file                                   */
    if (extfile) {
        extconf = NCONF_new(NULL);
        if (NCONF_load(extconf, extfile, &errorline) <= 0) {
            if (errorline <= 0)
                BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
                           extfile);
            else
                BIO_printf(bio_err,
                           "ERROR: on line %ld of config file '%s'\n",
                           errorline, extfile);
        if ((extconf = app_load_config(extfile)) == NULL) {
            ret = 1;
            goto end;
        }
+14 −56
Original line number Diff line number Diff line
@@ -172,6 +172,7 @@ static void apps_startup()
    ERR_load_SSL_strings();
    OpenSSL_add_all_algorithms();
    OpenSSL_add_ssl_algorithms();
    OPENSSL_load_builtin_modules();
    setup_ui_method();
    /*SSL_library_init();*/
#ifndef OPENSSL_NO_ENGINE
@@ -199,43 +200,26 @@ static void apps_shutdown()

static char *make_config_name()
{
    const char *t = X509_get_default_cert_area();
    const char *t;
    size_t len;
    char *p;

    len = strlen(t) + strlen(OPENSSL_CONF) + 2;
    if ((t = getenv("OPENSSL_CONF")) != NULL
        || (t = getenv("SSLEAY_CONF")) != NULL)
        return BUF_strdup(t);

    t = X509_get_default_cert_area();
    len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
    p = app_malloc(len, "config filename buffer");
    BUF_strlcpy(p, t, len);
    strcpy(p, t);
#ifndef OPENSSL_SYS_VMS
    BUF_strlcat(p, "/", len);
    strcat(p, "/");
#endif
    BUF_strlcat(p, OPENSSL_CONF, len);
    strcat(p, OPENSSL_CONF);

    return p;
}

static int load_config(CONF *cnf)
{
    static int load_config_called = 0;

    if (load_config_called)
        return 1;
    load_config_called = 1;
    if (!cnf)
        cnf = config;
    if (!cnf)
        return 1;

    OPENSSL_load_builtin_modules();

    if (CONF_modules_load(cnf, NULL, 0) <= 0) {
        BIO_printf(bio_err, "Error configuring OpenSSL\n");
        ERR_print_errors(bio_err);
        return 0;
    }
    return 1;
}

static void lock_dbg_cb(int mode, int type, const char *file, int line)
{
    static int modes[CRYPTO_NUM_LOCKS];
@@ -338,12 +322,11 @@ int main(int argc, char *argv[])
    FUNCTION f, *fp;
    LHASH_OF(FUNCTION) *prog = NULL;
    char **copied_argv = NULL;
    char *p, *pname, *to_free = NULL;
    char *p, *pname;
    char buf[1024];
    const char *prompt;
    ARGS arg;
    int first, n, i, ret = 0;
    long errline;

    arg.argv = NULL;
    arg.size = 0;
@@ -394,36 +377,11 @@ int main(int argc, char *argv[])
    pname = opt_progname(argv[0]);

    /* Lets load up our environment a little */
    default_config_file = make_config_name();
    bio_in = dup_bio_in();
    bio_out = dup_bio_out();
    bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);

    /* Determine and load the config file. */
    default_config_file = getenv("OPENSSL_CONF");
    if (default_config_file == NULL)
        default_config_file = getenv("SSLEAY_CONF");
    if (default_config_file == NULL)
        default_config_file = to_free = make_config_name();
    if (!load_config(NULL))
        goto end;
    config = NCONF_new(NULL);
    i = NCONF_load(config, default_config_file, &errline);
    if (i == 0) {
        if (ERR_GET_REASON(ERR_peek_last_error())
            == CONF_R_NO_SUCH_FILE) {
            BIO_printf(bio_err,
                       "%s: WARNING: can't open config file: %s\n",
                       pname, default_config_file);
            ERR_clear_error();
            NCONF_free(config);
            config = NULL;
        } else {
            ERR_print_errors(bio_err);
            NCONF_free(config);
            exit(1);
        }
    }

    /* first check the program name */
    f.name = pname;
    fp = lh_FUNCTION_retrieve(prog, &f);
@@ -510,7 +468,7 @@ int main(int argc, char *argv[])
    ret = 1;
 end:
    OPENSSL_free(copied_argv);
    OPENSSL_free(to_free);
    OPENSSL_free(default_config_file);
    NCONF_free(config);
    config = NULL;
    lh_FUNCTION_free(prog);
Loading