Filter creation fails if the rule file is not valid.
This commit is contained in:
Markus Makela
2015-03-28 13:11:57 +02:00
parent 28ee7f18e0
commit e0319067c9

View File

@ -294,24 +294,27 @@ void* rlistdup(void* fval)
static void* hrulefree(void* fval) static void* hrulefree(void* fval)
{ {
USER* user = (USER*)fval; RULELIST *ptr = (RULELIST*)fval;
RULELIST *ptr = user->rules_or,*tmp;
while(ptr){ while(ptr){
tmp = ptr; RULELIST *tmp = ptr;
ptr = ptr->next; ptr = ptr->next;
free(tmp); free(tmp);
} }
ptr = user->rules_and;
while(ptr){
tmp = ptr;
ptr = ptr->next;
free(tmp);
}
free(user->name);
free(user);
return NULL; return NULL;
} }
static void* huserfree(void* fval)
{
USER* value = (USER*)fval;
hrulefree(value->rules_and);
hrulefree(value->rules_or);
hrulefree(value->rules_strict_and);
free(value->qs_limit);
free(value->name);
free(value);
return NULL;
}
/** /**
* Strips the single or double quotes from a string. * Strips the single or double quotes from a string.
@ -632,13 +635,14 @@ void add_users(char* rule, FW_INSTANCE* instance)
* @param rule Rule string to parse * @param rule Rule string to parse
* @param instance The FW_FILTER instance * @param instance The FW_FILTER instance
*/ */
void link_rules(char* rule, FW_INSTANCE* instance) bool link_rules(char* orig, FW_INSTANCE* instance)
{ {
assert(rule != NULL && instance != NULL);
/**Apply rules to users*/ /**Apply rules to users*/
bool match_any = true; bool match_any = true;
bool rval = true;
char *rule = strdup(orig);
char *tok, *ruleptr, *userptr, *modeptr; char *tok, *ruleptr, *userptr, *modeptr;
char *saveptr = NULL; char *saveptr = NULL;
RULELIST* rulelist = NULL; RULELIST* rulelist = NULL;
@ -649,8 +653,9 @@ void link_rules(char* rule, FW_INSTANCE* instance)
if((userptr == NULL || ruleptr == NULL || modeptr == NULL)|| if((userptr == NULL || ruleptr == NULL || modeptr == NULL)||
(userptr > modeptr || userptr > ruleptr || modeptr > ruleptr)) { (userptr > modeptr || userptr > ruleptr || modeptr > ruleptr)) {
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, right keywords not found in the correct order: %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, right keywords not found in the correct order: %s",orig);
return; rval = false;
goto parse_err;
} }
*modeptr++ = '\0'; *modeptr++ = '\0';
@ -660,15 +665,17 @@ void link_rules(char* rule, FW_INSTANCE* instance)
if(tok == NULL) if(tok == NULL)
{ {
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, right keywords not found in the correct order: %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, right keywords not found in the correct order: %s",orig);
return; rval = false;
goto parse_err;
} }
if(strcmp(tok,"match") == 0){ if(strcmp(tok,"match") == 0){
tok = strtok_r(NULL," ",&saveptr); tok = strtok_r(NULL," ",&saveptr);
if(tok == NULL) if(tok == NULL)
{ {
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, missing keyword after 'match': %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, missing keyword after 'match': %s",orig);
return; rval = false;
goto parse_err;
} }
if(strcmp(tok,"any") == 0){ if(strcmp(tok,"any") == 0){
match_any = true; match_any = true;
@ -678,8 +685,9 @@ void link_rules(char* rule, FW_INSTANCE* instance)
match_any = false; match_any = false;
strict = true; strict = true;
}else{ }else{
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, 'match' was not followed by correct keyword: %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, 'match' was not followed by correct keyword: %s",orig);
return; rval = false;
goto parse_err;
} }
} }
@ -688,8 +696,9 @@ void link_rules(char* rule, FW_INSTANCE* instance)
if(tok == NULL) if(tok == NULL)
{ {
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, no rules given: %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, no rules given: %s",orig);
return; rval = false;
goto parse_err;
} }
while(tok) while(tok)
@ -721,7 +730,15 @@ void link_rules(char* rule, FW_INSTANCE* instance)
if(userptr == NULL) if(userptr == NULL)
{ {
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, no users given: %s",rule); skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, no users given: %s",orig);
rval = false;
goto parse_err;
}
if(rulelist == NULL)
{
skygw_log_write(LOGFILE_ERROR, "dbfwfilter: Rule syntax incorrect, no rules found: %s",orig);
rval = false;
goto parse_err; goto parse_err;
} }
@ -737,6 +754,7 @@ void link_rules(char* rule, FW_INSTANCE* instance)
if(user == NULL){ if(user == NULL){
skygw_log_write(LOGFILE_ERROR,"Error: dbfwfilter: failed to allocate memory when parsing rules."); skygw_log_write(LOGFILE_ERROR,"Error: dbfwfilter: failed to allocate memory when parsing rules.");
rval = false;
goto parse_err; goto parse_err;
} }
@ -773,12 +791,15 @@ void link_rules(char* rule, FW_INSTANCE* instance)
} }
parse_err: parse_err:
free(rule);
while(rulelist) while(rulelist)
{ {
RULELIST *tmp = rulelist; RULELIST *tmp = rulelist;
rulelist = rulelist->next; rulelist = rulelist->next;
free(tmp); free(tmp);
} }
return rval;
} }
@ -1042,7 +1063,7 @@ createInstance(char **options, FILTER_PARAMETER **params)
return NULL; return NULL;
} }
hashtable_memory_fns(ht,(HASHMEMORYFN)strdup,NULL,(HASHMEMORYFN)free,hrulefree); hashtable_memory_fns(ht,(HASHMEMORYFN)strdup,NULL,(HASHMEMORYFN)free,huserfree);
my_instance->htable = ht; my_instance->htable = ht;
my_instance->def_op = true; my_instance->def_op = true;
@ -1104,13 +1125,28 @@ createInstance(char **options, FILTER_PARAMETER **params)
fclose(file); fclose(file);
/**Apply the rules to users*/ /**Apply the rules to users*/
bool err = false;
ptr = my_instance->userstrings; ptr = my_instance->userstrings;
while(ptr){ while(ptr){
link_rules(ptr->value,my_instance);
tmp = ptr; if(!link_rules(ptr->value,my_instance))
ptr = ptr->next; {
free(tmp->value); skygw_log_write(LOGFILE_ERROR,"dbfwfilter: Failed to parse rule: %s",ptr->value);
free(tmp); err = true;
}
tmp = ptr;
ptr = ptr->next;
free(tmp->value);
free(tmp);
}
if(err)
{
hrulefree(my_instance->rules);
hashtable_free(my_instance->htable);
free(my_instance);
my_instance = NULL;
} }
return (FILTER *)my_instance; return (FILTER *)my_instance;