Use thread save settings alloc_str function where appropriate
authorMartin Willi <martin@revosec.ch>
Thu, 21 Apr 2011 08:48:16 +0000 (10:48 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 21 Apr 2011 08:48:16 +0000 (10:48 +0200)
15 files changed:
src/libcharon/plugins/eap_gtc/eap_gtc.c
src/libcharon/plugins/eap_peap/eap_peap_server.c
src/libcharon/plugins/eap_radius/eap_radius.c
src/libcharon/plugins/eap_radius/eap_radius_plugin.c
src/libcharon/plugins/eap_radius/radius_server.c
src/libcharon/plugins/eap_radius/radius_socket.c
src/libcharon/plugins/eap_tnc/eap_tnc.c
src/libcharon/plugins/eap_ttls/eap_ttls_server.c
src/libcharon/plugins/load_tester/load_tester_config.c
src/libcharon/plugins/tnc_imc/tnc_imc_manager.c
src/libhydra/plugins/attr/attr_provider.c
src/libhydra/plugins/resolve/resolve_handler.c
src/libstrongswan/plugins/openssl/openssl_rsa_private_key.c
src/libstrongswan/plugins/pkcs11/pkcs11_manager.c
src/libtls/tls_crypto.c

index c3ab07d..8c5e88c 100644 (file)
@@ -217,13 +217,15 @@ METHOD(eap_method_t, process_server, status_t,
        memcpy(password, data.ptr, data.len);
        password[data.len] = '\0';
 
-       service = lib->settings->get_str(lib->settings,
+       service = lib->settings->alloc_str(lib->settings,
                                                "charon.plugins.eap-gtc.pam_service", GTC_PAM_SERVICE);
 
        if (!authenticate(service, user, password))
        {
+               free(service);
                return FAILED;
        }
+       free(service);
        return SUCCESS;
 }
 
index 3fabc35..e13a6a3 100644 (file)
@@ -62,17 +62,17 @@ struct private_eap_peap_server_t {
        eap_code_t phase2_result;
 
        /**
-     * Outer phase 1 EAP method
+        * Outer phase 1 EAP method
         */
        eap_method_t *ph1_method;
 
        /**
-     * Current phase 2 EAP method
+        * Current phase 2 EAP method
         */
        eap_method_t *ph2_method;
 
        /**
-     * Pending outbound EAP message
+        * Pending outbound EAP message
         */
        eap_payload_t *out;
 
@@ -90,9 +90,10 @@ static status_t start_phase2_auth(private_eap_peap_server_t *this)
        char *eap_type_str;
        eap_type_t type;
 
-       eap_type_str = lib->settings->get_str(lib->settings,
+       eap_type_str = lib->settings->alloc_str(lib->settings,
                                                "charon.plugins.eap-peap.phase2_method", "mschapv2");
        type = eap_type_from_string(eap_type_str);
+       free(eap_type_str);
        if (type == 0)
        {
                DBG1(DBG_IKE, "unrecognized phase2 method \"%s\"", eap_type_str);
@@ -302,8 +303,8 @@ METHOD(tls_application_t, process, status_t,
                        this->ph2_method->destroy(this->ph2_method);
                        this->ph2_method = NULL;
 
-                       /* EAP-PEAP requires the sending of an inner EAP_SUCCESS message */     
-                       this->phase2_result = EAP_SUCCESS;              
+                       /* EAP-PEAP requires the sending of an inner EAP_SUCCESS message */
+                       this->phase2_result = EAP_SUCCESS;
                        this->out = eap_payload_create_code(this->phase2_result, 1 +
                                                        this->ph1_method->get_identifier(this->ph1_method));
                        return NEED_MORE;
@@ -321,7 +322,7 @@ METHOD(tls_application_t, process, status_t,
                                DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
                        }
                        /* EAP-PEAP requires the sending of an inner EAP_FAILURE message */
-                       this->phase2_result = EAP_FAILURE;                      
+                       this->phase2_result = EAP_FAILURE;
                        this->out = eap_payload_create_code(this->phase2_result, 1 +
                                                        this->ph1_method->get_identifier(this->ph1_method));
                        return NEED_MORE;
@@ -360,7 +361,7 @@ METHOD(tls_application_t, build, status_t,
                this->ph2_method->initiate(this->ph2_method, &this->out);
                this->start_phase2 = FALSE;
        }
-       
+
        this->start_phase2_id = TRUE;
 
        if (this->out)
index b93a1f5..08b46c6 100644 (file)
@@ -387,6 +387,7 @@ METHOD(eap_method_t, destroy, void,
        this->peer->destroy(this->peer);
        this->server->destroy(this->server);
        this->client->destroy(this->client);
+       free(this->id_prefix);
        free(this);
 }
 
@@ -414,7 +415,7 @@ eap_radius_t *eap_radius_create(identification_t *server, identification_t *peer
                .type = EAP_RADIUS,
                .eap_start = lib->settings->get_bool(lib->settings,
                                                                "charon.plugins.eap-radius.eap_start", FALSE),
-               .id_prefix = lib->settings->get_str(lib->settings,
+               .id_prefix = lib->settings->alloc_str(lib->settings,
                                                                "charon.plugins.eap-radius.id_prefix", ""),
                .class_group = lib->settings->get_bool(lib->settings,
                                                                "charon.plugins.eap-radius.class_group", FALSE),
@@ -425,6 +426,7 @@ eap_radius_t *eap_radius_create(identification_t *server, identification_t *peer
        this->client = radius_client_create();
        if (!this->client)
        {
+               free(this->id_prefix);
                free(this);
                return NULL;
        }
index 9b15256..8ca5f39 100644 (file)
@@ -65,18 +65,19 @@ static void load_servers(private_eap_radius_plugin_t *this)
        char *nas_identifier, *secret, *address, *section;
        int port, sockets, preference;
 
-       address = lib->settings->get_str(lib->settings,
+       address = lib->settings->alloc_str(lib->settings,
                                        "charon.plugins.eap-radius.server", NULL);
        if (address)
        {       /* legacy configuration */
-               secret = lib->settings->get_str(lib->settings,
+               secret = lib->settings->alloc_str(lib->settings,
                                        "charon.plugins.eap-radius.secret", NULL);
                if (!secret)
                {
                        DBG1(DBG_CFG, "no RADUIS secret defined");
+                       free(address);
                        return;
                }
-               nas_identifier = lib->settings->get_str(lib->settings,
+               nas_identifier = lib->settings->alloc_str(lib->settings,
                                        "charon.plugins.eap-radius.nas_identifier", "strongSwan");
                port = lib->settings->get_int(lib->settings,
                                        "charon.plugins.eap-radius.port", RADIUS_PORT);
@@ -84,6 +85,9 @@ static void load_servers(private_eap_radius_plugin_t *this)
                                        "charon.plugins.eap-radius.sockets", 1);
                server = radius_server_create(address, port, nas_identifier,
                                                                          secret, sockets, 0);
+               free(address);
+               free(nas_identifier);
+               free(secret);
                if (!server)
                {
                        DBG1(DBG_CFG, "no RADUIS server defined");
@@ -97,21 +101,22 @@ static void load_servers(private_eap_radius_plugin_t *this)
                                                                                "charon.plugins.eap-radius.servers");
        while (enumerator->enumerate(enumerator, &section))
        {
-               address = lib->settings->get_str(lib->settings,
+               address = lib->settings->alloc_str(lib->settings,
                        "charon.plugins.eap-radius.servers.%s.address", NULL, section);
                if (!address)
                {
                        DBG1(DBG_CFG, "RADIUS server '%s' misses address, skipped", section);
                        continue;
                }
-               secret = lib->settings->get_str(lib->settings,
+               secret = lib->settings->alloc_str(lib->settings,
                        "charon.plugins.eap-radius.servers.%s.secret", NULL, section);
                if (!secret)
                {
                        DBG1(DBG_CFG, "RADIUS server '%s' misses secret, skipped", section);
+                       free(address);
                        continue;
                }
-               nas_identifier = lib->settings->get_str(lib->settings,
+               nas_identifier = lib->settings->alloc_str(lib->settings,
                        "charon.plugins.eap-radius.servers.%s.nas_identifier",
                        "strongSwan", section);
                port = lib->settings->get_int(lib->settings,
@@ -122,6 +127,9 @@ static void load_servers(private_eap_radius_plugin_t *this)
                        "charon.plugins.eap-radius.servers.%s.preference", 0, section);
                server = radius_server_create(address, port, nas_identifier,
                                                                          secret, sockets, preference);
+               free(address);
+               free(nas_identifier);
+               free(secret);
                if (!server)
                {
                        DBG1(DBG_CFG, "loading RADIUS server '%s' failed, skipped", section);
index d166280..7cd1477 100644 (file)
@@ -176,6 +176,7 @@ METHOD(radius_server_t, destroy, void,
                this->condvar->destroy(this->condvar);
                this->sockets->destroy_offset(this->sockets,
                                                                          offsetof(radius_socket_t, destroy));
+               free(this->nas_identifier.ptr);
                free(this);
        }
 }
@@ -200,7 +201,8 @@ radius_server_t *radius_server_create(char *server, u_int16_t port,
                        .destroy = _destroy,
                },
                .reachable = TRUE,
-               .nas_identifier = chunk_create(nas_identifier, strlen(nas_identifier)),
+               .nas_identifier = chunk_clone(chunk_create(nas_identifier,
+                                                                                                  strlen(nas_identifier))),
                .socket_count = sockets,
                .sockets = linked_list_create(),
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
index f46c27e..df5f5b0 100644 (file)
@@ -257,6 +257,7 @@ METHOD(radius_socket_t, destroy, void,
        DESTROY_IF(this->hasher);
        DESTROY_IF(this->signer);
        DESTROY_IF(this->rng);
+       chunk_clear(&this->secret);
        close(this->fd);
        free(this);
 }
@@ -300,7 +301,7 @@ radius_socket_t *radius_socket_create(host_t *host, chunk_t secret)
                destroy(this);
                return NULL;
        }
-       this->secret = secret;
+       this->secret = chunk_clone(secret);
        this->signer->set_key(this->signer, secret);
        /* we use a random identifier, helps if we restart often */
        this->identifier = random();
index d47fd37..8562834 100644 (file)
@@ -40,7 +40,7 @@ struct private_eap_tnc_t {
 
 
 /** Maximum number of EAP-TNC messages/fragments allowed */
-#define MAX_MESSAGE_COUNT 10 
+#define MAX_MESSAGE_COUNT 10
 /** Default size of a EAP-TNC fragment */
 #define MAX_FRAGMENT_LEN 50000
 
@@ -149,7 +149,7 @@ static eap_tnc_t *eap_tnc_create(identification_t *server,
                                        "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
        max_msg_count = lib->settings->get_int(lib->settings,
                                        "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
-       protocol = lib->settings->get_str(lib->settings,
+       protocol = lib->settings->alloc_str(lib->settings,
                                        "charon.plugins.eap-tnc.protocol", "tnccs-1.1");
        if (strcaseeq(protocol, "tnccs-2.0"))
        {
@@ -166,9 +166,11 @@ static eap_tnc_t *eap_tnc_create(identification_t *server,
        else
        {
                DBG1(DBG_TNC, "TNCCS protocol '%s' not supported", protocol);
+               free(protocol);
                free(this);
                return NULL;
        }
+       free(protocol);
        tnccs = charon->tnccs->create_instance(charon->tnccs, type, is_server);
        this->tls_eap = tls_eap_create(EAP_TNC, (tls_t*)tnccs, frag_size, max_msg_count);
        if (!this->tls_eap)
index 835cd73..8167104 100644 (file)
@@ -54,12 +54,12 @@ struct private_eap_ttls_server_t {
        bool start_phase2_tnc;
 
        /**
-     * Current phase 2 EAP method
+        * Current phase 2 EAP method
         */
        eap_method_t *method;
 
        /**
-     * Pending outbound EAP message
+        * Pending outbound EAP message
         */
        eap_payload_t *out;
 
@@ -77,9 +77,10 @@ static status_t start_phase2_auth(private_eap_ttls_server_t *this)
        char *eap_type_str;
        eap_type_t type;
 
-       eap_type_str = lib->settings->get_str(lib->settings,
+       eap_type_str = lib->settings->alloc_str(lib->settings,
                                                "charon.plugins.eap-ttls.phase2_method", "md5");
        type = eap_type_from_string(eap_type_str);
+       free(eap_type_str);
        if (type == 0)
        {
                DBG1(DBG_IKE, "unrecognized phase2 method \"%s\"", eap_type_str);
index 71391d5..b522aa7 100644 (file)
@@ -279,6 +279,10 @@ static void destroy(private_load_tester_config_t *this)
        this->peer_cfg->destroy(this->peer_cfg);
        DESTROY_IF(this->proposal);
        DESTROY_IF(this->vip);
+       free(this->pool);
+       free(this->remote);
+       free(this->initiator_auth);
+       free(this->responder_auth);
        free(this);
 }
 
@@ -300,9 +304,9 @@ load_tester_config_t *load_tester_config_create()
        {
                this->vip = host_create_from_string("0.0.0.0", 0);
        }
-       this->pool = lib->settings->get_str(lib->settings,
+       this->pool = lib->settings->alloc_str(lib->settings,
                                "charon.plugins.load-tester.pool", NULL);
-       this->remote = lib->settings->get_str(lib->settings,
+       this->remote = lib->settings->alloc_str(lib->settings,
                                "charon.plugins.load-tester.remote", "127.0.0.1");
 
        this->proposal = proposal_create_from_string(PROTO_IKE,
@@ -318,9 +322,9 @@ load_tester_config_t *load_tester_config_create()
        this->child_rekey = lib->settings->get_int(lib->settings,
                                "charon.plugins.load-tester.child_rekey", 600);
 
-       this->initiator_auth = lib->settings->get_str(lib->settings,
+       this->initiator_auth = lib->settings->alloc_str(lib->settings,
                                "charon.plugins.load-tester.initiator_auth", "pubkey");
-       this->responder_auth = lib->settings->get_str(lib->settings,
+       this->responder_auth = lib->settings->alloc_str(lib->settings,
                                "charon.plugins.load-tester.responder_auth", "pubkey");
 
        this->port = lib->settings->get_int(lib->settings,
index aa20534..a04e495 100644 (file)
@@ -43,6 +43,11 @@ struct private_tnc_imc_manager_t {
         * Next IMC ID to be assigned
         */
        TNC_IMCID next_imc_id;
+
+       /**
+        * Preferred language
+        */
+       char *preferred_language;
 };
 
 METHOD(imc_manager_t, add, bool,
@@ -95,8 +100,7 @@ METHOD(imc_manager_t, remove_, imc_t*,
 METHOD(imc_manager_t, get_preferred_language, char*,
        private_tnc_imc_manager_t *this)
 {
-       return lib->settings->get_str(lib->settings,
-                                       "charon.plugins.tnc-imc.preferred_language", "en");
+       return this->preferred_language;
 }
 
 METHOD(imc_manager_t, notify_connection_change, void,
@@ -208,6 +212,7 @@ METHOD(imc_manager_t, destroy, void,
                imc->destroy(imc);
        }
        this->imcs->destroy(this->imcs);
+       free(this->preferred_language);
        free(this);
 }
 
@@ -232,6 +237,8 @@ imc_manager_t* tnc_imc_manager_create(void)
                },
                .imcs = linked_list_create(),
                .next_imc_id = 1,
+               .preferred_language = lib->settings->alloc_str(lib->settings,
+                                       "charon.plugins.tnc-imc.preferred_language", "en");
        );
 
        return &this->public;
index 44242c2..2dd67cb 100644 (file)
@@ -109,8 +109,8 @@ static void add_legacy_entry(private_attr_provider_t *this, char *key, int nr,
        host_t *host;
        char *str;
 
-       str = lib->settings->get_str(lib->settings, "%s.%s%d", NULL, hydra->daemon,
-                                                                key, nr);
+       str = lib->settings->alloc_str(lib->settings, "%s.%s%d", NULL,
+                                                                  hydra->daemon, key, nr);
        if (str)
        {
                host = host_create_from_string(str, 0);
@@ -139,6 +139,7 @@ static void add_legacy_entry(private_attr_provider_t *this, char *key, int nr,
                                 configuration_attribute_type_names, entry->type, &entry->value);
                        this->attributes->insert_last(this->attributes, entry);
                }
+               free(str);
        }
 }
 
index feb2fd0..d16fec9 100644 (file)
@@ -228,6 +228,7 @@ static enumerator_t* create_attribute_enumerator(private_resolve_handler_t *this
 static void destroy(private_resolve_handler_t *this)
 {
        this->mutex->destroy(this->mutex);
+       free(this->file);
        free(this);
 }
 
@@ -244,7 +245,7 @@ resolve_handler_t *resolve_handler_create()
        this->public.destroy = (void(*)(resolve_handler_t*))destroy;
 
        this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
-       this->file = lib->settings->get_str(lib->settings,
+       this->file = lib->settings->alloc_str(lib->settings,
                                                                "%s.plugins.resolve.file", RESOLV_CONF, hydra->daemon);
 
        return &this->public;
index 0b607c3..39c28d3 100644 (file)
@@ -550,36 +550,34 @@ openssl_rsa_private_key_t *openssl_rsa_private_key_connect(key_type_t type,
 
        if (!engine_id)
        {
-               engine_id = lib->settings->get_str(lib->settings,
+               engine_id = lib->settings->alloc_str(lib->settings,
                                                "libstrongswan.plugins.openssl.engine_id", "pkcs11");
        }
        engine = ENGINE_by_id(engine_id);
        if (!engine)
        {
                DBG2(DBG_LIB, "engine '%s' is not available", engine_id);
-               return NULL;
+               goto engine_failed;
        }
        if (!ENGINE_init(engine))
        {
                DBG1(DBG_LIB, "failed to initialize engine '%s'", engine_id);
-               ENGINE_free(engine);
-               return NULL;
+               goto engine_failed;
        }
        if (!login(engine, keyid))
        {
                DBG1(DBG_LIB, "login to engine '%s' failed", engine_id);
-               ENGINE_free(engine);
-               return NULL;
+               goto engine_failed;
        }
        key = ENGINE_load_private_key(engine, keyname, NULL, NULL);
        if (!key)
        {
                DBG1(DBG_LIB, "failed to load private key with ID '%s' from "
                         "engine '%s'", keyname, engine_id);
-               ENGINE_free(engine);
-               return NULL;
+               goto engine_failed;
        }
        ENGINE_free(engine);
+       free(engine_id);
 
        this = create_empty();
        this->rsa = EVP_PKEY_get1_RSA(key);
@@ -594,5 +592,12 @@ openssl_rsa_private_key_t *openssl_rsa_private_key_connect(key_type_t type,
 #else /* OPENSSL_NO_ENGINE */
        return NULL;
 #endif /* OPENSSL_NO_ENGINE */
+engine_failed:
+       if (engine)
+       {
+               ENGINE_free(engine);
+       }
+       free(engine_id);
+       return NULL;
 }
 
index 431cd6a..7579e0e 100644 (file)
@@ -74,7 +74,8 @@ static void lib_entry_destroy(lib_entry_t *entry)
        {
                entry->job->cancel(entry->job);
        }
-       entry->lib->destroy(entry->lib);
+       DESTROY_IF(entry->lib);
+       free(entry->path);
        free(entry);
 }
 
@@ -365,12 +366,12 @@ pkcs11_manager_t *pkcs11_manager_create(pkcs11_manager_token_event_t cb,
                        .this = this,
                );
 
-               entry->path = lib->settings->get_str(lib->settings,
+               entry->path = lib->settings->alloc_str(lib->settings,
                                "libstrongswan.plugins.pkcs11.modules.%s.path", NULL, module);
                if (!entry->path)
                {
                        DBG1(DBG_CFG, "PKCS11 module '%s' lacks library path", module);
-                       free(entry);
+                       lib_entry_destroy(entry);
                        continue;
                }
                entry->lib = pkcs11_library_create(module, entry->path,
@@ -379,7 +380,7 @@ pkcs11_manager_t *pkcs11_manager_create(pkcs11_manager_token_event_t cb,
                                                        FALSE, module));
                if (!entry->lib)
                {
-                       free(entry);
+                       lib_entry_destroy(entry);
                        continue;
                }
                this->libs->insert_last(this->libs, entry);
index b000f9d..fb0373b 100644 (file)
@@ -706,7 +706,7 @@ static void filter_key_exchange_config_suites(private_tls_crypto_t *this,
        int i, remaining = 0;
        char *token, *config;
 
-       config = lib->settings->get_str(lib->settings, "libtls.key_exchange", NULL);
+       config = lib->settings->alloc_str(lib->settings, "libtls.key_exchange", NULL);
        if (config)
        {
                for (i = 0; i < *count; i++)
@@ -747,6 +747,7 @@ static void filter_key_exchange_config_suites(private_tls_crypto_t *this,
                        enumerator->destroy(enumerator);
                }
                *count = remaining;
+               free(config);
        }
 }
 
@@ -760,7 +761,7 @@ static void filter_cipher_config_suites(private_tls_crypto_t *this,
        int i, remaining = 0;
        char *token, *config;
 
-       config = lib->settings->get_str(lib->settings, "libtls.cipher", NULL);
+       config = lib->settings->alloc_str(lib->settings, "libtls.cipher", NULL);
        if (config)
        {
                for (i = 0; i < *count; i++)
@@ -812,6 +813,7 @@ static void filter_cipher_config_suites(private_tls_crypto_t *this,
                        enumerator->destroy(enumerator);
                }
                *count = remaining;
+               free(config);
        }
 }
 
@@ -825,7 +827,7 @@ static void filter_mac_config_suites(private_tls_crypto_t *this,
        int i, remaining = 0;
        char *token, *config;
 
-       config = lib->settings->get_str(lib->settings, "libtls.mac", NULL);
+       config = lib->settings->alloc_str(lib->settings, "libtls.mac", NULL);
        if (config)
        {
                for (i = 0; i < *count; i++)
@@ -861,6 +863,7 @@ static void filter_mac_config_suites(private_tls_crypto_t *this,
                        enumerator->destroy(enumerator);
                }
                *count = remaining;
+               free(config);
        }
 }
 
@@ -874,7 +877,7 @@ static void filter_specific_config_suites(private_tls_crypto_t *this,
        int i, remaining = 0, suite;
        char *token, *config;
 
-       config = lib->settings->get_str(lib->settings, "libtls.suites", NULL);
+       config = lib->settings->alloc_str(lib->settings, "libtls.suites", NULL);
        if (config)
        {
                for (i = 0; i < *count; i++)
@@ -892,6 +895,7 @@ static void filter_specific_config_suites(private_tls_crypto_t *this,
                        enumerator->destroy(enumerator);
                }
                *count = remaining;
+               free(config);
        }
 }