|
@@ -33,34 +33,22 @@
|
|
|
|
|
|
#define MAX_SIZE 10
|
|
|
|
|
|
-struct confentry {
|
|
|
- const char *key;
|
|
|
- const char *value;
|
|
|
-};
|
|
|
-
|
|
|
-struct configuration {
|
|
|
- int size;
|
|
|
- struct confentry **confdetails;
|
|
|
-};
|
|
|
-
|
|
|
-struct configuration config={.size=0, .confdetails=NULL};
|
|
|
-
|
|
|
//clean up method for freeing configuration
|
|
|
-void free_configurations() {
|
|
|
+void free_configurations(struct configuration *cfg) {
|
|
|
int i = 0;
|
|
|
- for (i = 0; i < config.size; i++) {
|
|
|
- if (config.confdetails[i]->key != NULL) {
|
|
|
- free((void *)config.confdetails[i]->key);
|
|
|
+ for (i = 0; i < cfg->size; i++) {
|
|
|
+ if (cfg->confdetails[i]->key != NULL) {
|
|
|
+ free((void *)cfg->confdetails[i]->key);
|
|
|
}
|
|
|
- if (config.confdetails[i]->value != NULL) {
|
|
|
- free((void *)config.confdetails[i]->value);
|
|
|
+ if (cfg->confdetails[i]->value != NULL) {
|
|
|
+ free((void *)cfg->confdetails[i]->value);
|
|
|
}
|
|
|
- free(config.confdetails[i]);
|
|
|
+ free(cfg->confdetails[i]);
|
|
|
}
|
|
|
- if (config.size > 0) {
|
|
|
- free(config.confdetails);
|
|
|
+ if (cfg->size > 0) {
|
|
|
+ free(cfg->confdetails);
|
|
|
}
|
|
|
- config.size = 0;
|
|
|
+ cfg->size = 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -137,8 +125,8 @@ int check_configuration_permissions(const char* file_name) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-//function used to load the configurations present in the secure config
|
|
|
-void read_config(const char* file_name) {
|
|
|
+
|
|
|
+void read_config(const char* file_name, struct configuration *cfg) {
|
|
|
FILE *conf_file;
|
|
|
char *line;
|
|
|
char *equaltok;
|
|
@@ -156,9 +144,9 @@ void read_config(const char* file_name) {
|
|
|
#endif
|
|
|
|
|
|
//allocate space for ten configuration items.
|
|
|
- config.confdetails = (struct confentry **) malloc(sizeof(struct confentry *)
|
|
|
+ cfg->confdetails = (struct confentry **) malloc(sizeof(struct confentry *)
|
|
|
* MAX_SIZE);
|
|
|
- config.size = 0;
|
|
|
+ cfg->size = 0;
|
|
|
conf_file = fopen(file_name, "r");
|
|
|
if (conf_file == NULL) {
|
|
|
fprintf(ERRORFILE, "Invalid conf file provided : %s \n", file_name);
|
|
@@ -200,9 +188,9 @@ void read_config(const char* file_name) {
|
|
|
free(line);
|
|
|
continue;
|
|
|
}
|
|
|
- config.confdetails[config.size] = (struct confentry *) malloc(
|
|
|
+ cfg->confdetails[cfg->size] = (struct confentry *) malloc(
|
|
|
sizeof(struct confentry));
|
|
|
- if(config.confdetails[config.size] == NULL) {
|
|
|
+ if(cfg->confdetails[cfg->size] == NULL) {
|
|
|
fprintf(LOGFILE,
|
|
|
"Failed allocating memory for single configuration item\n");
|
|
|
goto cleanup;
|
|
@@ -212,10 +200,10 @@ void read_config(const char* file_name) {
|
|
|
fprintf(LOGFILE, "read_config : Adding conf key : %s \n", equaltok);
|
|
|
#endif
|
|
|
|
|
|
- memset(config.confdetails[config.size], 0, sizeof(struct confentry));
|
|
|
- config.confdetails[config.size]->key = (char *) malloc(
|
|
|
+ memset(cfg->confdetails[cfg->size], 0, sizeof(struct confentry));
|
|
|
+ cfg->confdetails[cfg->size]->key = (char *) malloc(
|
|
|
sizeof(char) * (strlen(equaltok)+1));
|
|
|
- strcpy((char *)config.confdetails[config.size]->key, equaltok);
|
|
|
+ strcpy((char *)cfg->confdetails[cfg->size]->key, equaltok);
|
|
|
equaltok = strtok_r(NULL, "=", &temp_equaltok);
|
|
|
if (equaltok == NULL) {
|
|
|
fprintf(LOGFILE, "configuration tokenization failed \n");
|
|
@@ -224,8 +212,8 @@ void read_config(const char* file_name) {
|
|
|
//means value is commented so don't store the key
|
|
|
if(equaltok[0] == '#') {
|
|
|
free(line);
|
|
|
- free((void *)config.confdetails[config.size]->key);
|
|
|
- free(config.confdetails[config.size]);
|
|
|
+ free((void *)cfg->confdetails[cfg->size]->key);
|
|
|
+ free(cfg->confdetails[cfg->size]);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
@@ -233,27 +221,29 @@ void read_config(const char* file_name) {
|
|
|
fprintf(LOGFILE, "read_config : Adding conf value : %s \n", equaltok);
|
|
|
#endif
|
|
|
|
|
|
- config.confdetails[config.size]->value = (char *) malloc(
|
|
|
+ cfg->confdetails[cfg->size]->value = (char *) malloc(
|
|
|
sizeof(char) * (strlen(equaltok)+1));
|
|
|
- strcpy((char *)config.confdetails[config.size]->value, equaltok);
|
|
|
- if((config.size + 1) % MAX_SIZE == 0) {
|
|
|
- config.confdetails = (struct confentry **) realloc(config.confdetails,
|
|
|
- sizeof(struct confentry **) * (MAX_SIZE + config.size));
|
|
|
- if (config.confdetails == NULL) {
|
|
|
+ strcpy((char *)cfg->confdetails[cfg->size]->value, equaltok);
|
|
|
+ if((cfg->size + 1) % MAX_SIZE == 0) {
|
|
|
+ cfg->confdetails = (struct confentry **) realloc(cfg->confdetails,
|
|
|
+ sizeof(struct confentry **) * (MAX_SIZE + cfg->size));
|
|
|
+ if (cfg->confdetails == NULL) {
|
|
|
fprintf(LOGFILE,
|
|
|
"Failed re-allocating memory for configuration items\n");
|
|
|
goto cleanup;
|
|
|
}
|
|
|
}
|
|
|
- if(config.confdetails[config.size] )
|
|
|
- config.size++;
|
|
|
+ if(cfg->confdetails[cfg->size]) {
|
|
|
+ cfg->size++;
|
|
|
+ }
|
|
|
+
|
|
|
free(line);
|
|
|
}
|
|
|
|
|
|
//close the file
|
|
|
fclose(conf_file);
|
|
|
|
|
|
- if (config.size == 0) {
|
|
|
+ if (cfg->size == 0) {
|
|
|
fprintf(ERRORFILE, "Invalid configuration provided in %s\n", file_name);
|
|
|
exit(INVALID_CONFIG_FILE);
|
|
|
}
|
|
@@ -266,7 +256,7 @@ void read_config(const char* file_name) {
|
|
|
free(line);
|
|
|
}
|
|
|
fclose(conf_file);
|
|
|
- free_configurations();
|
|
|
+ free_configurations(cfg);
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -276,29 +266,17 @@ void read_config(const char* file_name) {
|
|
|
* array, next time onwards used the populated array.
|
|
|
*
|
|
|
*/
|
|
|
-char * get_value(const char* key) {
|
|
|
+char * get_value(const char* key, struct configuration *cfg) {
|
|
|
int count;
|
|
|
- for (count = 0; count < config.size; count++) {
|
|
|
- if (strcmp(config.confdetails[count]->key, key) == 0) {
|
|
|
- return strdup(config.confdetails[count]->value);
|
|
|
+ for (count = 0; count < cfg->size; count++) {
|
|
|
+ if (strcmp(cfg->confdetails[count]->key, key) == 0) {
|
|
|
+ return strdup(cfg->confdetails[count]->value);
|
|
|
}
|
|
|
}
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * Function to return an array of values for a key.
|
|
|
- * Value delimiter is assumed to be a comma.
|
|
|
- */
|
|
|
-char ** get_values(const char * key) {
|
|
|
- char *value = get_value(key);
|
|
|
- return extract_values(value);
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * Extracts array of values from the comma separated list of values.
|
|
|
- */
|
|
|
-char ** extract_values(char *value) {
|
|
|
+char ** extract_values_delim(char *value, const char *delim) {
|
|
|
char ** toPass = NULL;
|
|
|
char *tempTok = NULL;
|
|
|
char *tempstr = NULL;
|
|
@@ -324,6 +302,32 @@ char ** extract_values(char *value) {
|
|
|
return toPass;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Extracts array of values from the comma separated list of values.
|
|
|
+ */
|
|
|
+char ** extract_values(char *value) {
|
|
|
+ return extract_values_delim(value, ",");
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Function to return an array of values for a key.
|
|
|
+ * Value delimiter is assumed to be a comma.
|
|
|
+ */
|
|
|
+char ** get_values(const char * key, struct configuration *cfg) {
|
|
|
+ char *value = get_value(key, cfg);
|
|
|
+ return extract_values_delim(value, ",");
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Function to return an array of values for a key, using the specified
|
|
|
+ delimiter.
|
|
|
+ */
|
|
|
+char ** get_values_delim(const char * key, struct configuration *cfg,
|
|
|
+ const char *delim) {
|
|
|
+ char *value = get_value(key, cfg);
|
|
|
+ return extract_values_delim(value, delim);
|
|
|
+}
|
|
|
+
|
|
|
// free an entry set of values
|
|
|
void free_values(char** values) {
|
|
|
if (*values != NULL) {
|