Don't load eap-sim-file plugin if no valid triplet file found
authorMartin Willi <martin@revosec.ch>
Thu, 13 Oct 2011 14:43:03 +0000 (16:43 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 14 Oct 2011 08:05:49 +0000 (10:05 +0200)
src/libcharon/plugins/eap_sim_file/eap_sim_file_plugin.c
src/libcharon/plugins/eap_sim_file/eap_sim_file_triplets.c

index 2aadfcc..eae7672 100644 (file)
@@ -57,6 +57,32 @@ METHOD(plugin_t, get_name, char*,
 }
 
 /**
+ * Load triplet file
+ */
+static bool load_triplets(private_eap_sim_file_t *this,
+                                                 plugin_feature_t *feature, bool reg, void *data)
+{
+       if (reg)
+       {
+               this->triplets = eap_sim_file_triplets_create(TRIPLET_FILE);
+               if (!this->triplets)
+               {
+                       return FALSE;
+               }
+               this->provider = eap_sim_file_provider_create(this->triplets);
+               this->card = eap_sim_file_card_create(this->triplets);
+               return TRUE;
+       }
+       this->card->destroy(this->card);
+       this->provider->destroy(this->provider);
+       this->triplets->destroy(this->triplets);
+       this->card = NULL;
+       this->provider = NULL;
+       this->triplets = NULL;
+       return TRUE;
+}
+
+/**
  * Callback providing our card to register
  */
 static simaka_card_t* get_card(private_eap_sim_file_t *this)
@@ -76,12 +102,16 @@ METHOD(plugin_t, get_features, int,
        private_eap_sim_file_t *this, plugin_feature_t *features[])
 {
        static plugin_feature_t f[] = {
+               PLUGIN_CALLBACK((void*)load_triplets, NULL),
+                       PLUGIN_PROVIDE(CUSTOM, "eap-sim-file-triplets"),
                PLUGIN_CALLBACK(simaka_manager_register, get_card),
                        PLUGIN_PROVIDE(CUSTOM, "sim-card"),
                                PLUGIN_DEPENDS(CUSTOM, "sim-manager"),
+                               PLUGIN_DEPENDS(CUSTOM, "eap-sim-file-triplets"),
                PLUGIN_CALLBACK(simaka_manager_register, get_provider),
                        PLUGIN_PROVIDE(CUSTOM, "sim-provider"),
                                PLUGIN_DEPENDS(CUSTOM, "sim-manager"),
+                               PLUGIN_DEPENDS(CUSTOM, "eap-sim-file-triplets"),
        };
        *features = f;
        return countof(f);
@@ -90,9 +120,6 @@ METHOD(plugin_t, get_features, int,
 METHOD(plugin_t, destroy, void,
        private_eap_sim_file_t *this)
 {
-       this->card->destroy(this->card);
-       this->provider->destroy(this->provider);
-       this->triplets->destroy(this->triplets);
        free(this);
 }
 
@@ -111,10 +138,7 @@ plugin_t *eap_sim_file_plugin_create()
                                .destroy = _destroy,
                        },
                },
-               .triplets = eap_sim_file_triplets_create(TRIPLET_FILE),
        );
-       this->provider = eap_sim_file_provider_create(this->triplets);
-       this->card = eap_sim_file_card_create(this->triplets);
 
        return &this->public.plugin;
 }
index 618ae9e..de3b693 100644 (file)
@@ -150,7 +150,7 @@ static void parse_token(char *to, char *from, size_t len)
 /**
  * Read the triplets from the file
  */
-static void read_triplets(private_eap_sim_file_triplets_t *this, char *path)
+static bool read_triplets(private_eap_sim_file_triplets_t *this, char *path)
 {
        char line[512];
        FILE *file;
@@ -161,7 +161,7 @@ static void read_triplets(private_eap_sim_file_triplets_t *this, char *path)
        {
                DBG1(DBG_CFG, "opening triplet file %s failed: %s",
                         path, strerror(errno));
-               return;
+               return FALSE;
        }
 
        /* read line by line */
@@ -227,6 +227,7 @@ static void read_triplets(private_eap_sim_file_triplets_t *this, char *path)
 
        DBG1(DBG_CFG, "read %d triplets from %s",
                 this->triplets->get_count(this->triplets), path);
+       return TRUE;
 }
 
 METHOD(eap_sim_file_triplets_t, destroy, void,
@@ -252,8 +253,12 @@ eap_sim_file_triplets_t *eap_sim_file_triplets_create(char *file)
                .triplets = linked_list_create(),
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
        );
-       read_triplets(this, file);
 
+       if (!read_triplets(this, file))
+       {
+               destroy(this);
+               return NULL;
+       }
        return &this->public;
 }