/[pdpsoft]/trunk/grid-mw-security/ees/src/eval_man/pdl.c
ViewVC logotype

Diff of /trunk/grid-mw-security/ees/src/eval_man/pdl.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1415 by aramv, Fri Jan 22 16:57:35 2010 UTC revision 1416 by aramv, Fri Jan 22 18:05:24 2010 UTC
# Line 12  extern void                 delete_lex_b Line 12  extern void                 delete_lex_b
12  extern int                  yylex_destroy(void);  extern int                  yylex_destroy(void);
13  #endif  #endif
14    
15  pdl_data_t*                config;  pdl_data_t                  config;
16    
17  /* This function does everything parser related. After running yacc through wrap_yacc(), the created list is reduced to those in the array of named policies, of number_of_policies is greater than 0. The policies are checked for unreachable rules. After the parser has created a list of policies containing lists of rules, these rules are transformed to a tree by the link_dead_end_rules_in_policies() function. Finally, when a list of policies containing rules trees was created, the plug-ins are linked to each rule in the node. */  /* This function does everything parser related. After running yacc through wrap_yacc(), the created list is reduced to those in the array of named policies, of number_of_policies is greater than 0. The policies are checked for unreachable rules. After the parser has created a list of policies containing lists of rules, these rules are transformed to a tree by the link_dead_end_rules_in_policies() function. Finally, when a list of policies containing rules trees was created, the plug-ins are linked to each rule in the node. */
18  EES_RC start_pdl_parser(char *config_file, int number_of_policies, char* names_of_policies[]){  EES_RC start_pdl_parser(char *config_file, int number_of_policies, char* names_of_policies[]){
19    config->recursion_was_created           = 0;    config.recursion_was_created           = 0;
20    config->unknown_variable_was_referenced = 0;    config.unknown_variable_was_referenced = 0;
21    config->starting_state_was_reused       = 0;    config.starting_state_was_reused       = 0;
22    
23    eef_log(LOG_NOTICE, "Loading config file: %s\n", config_file);    eef_log(LOG_NOTICE, "Loading config file: %s\n", config_file);
24    if(wrap_yacc(config_file) == EES_SUCCESS){    if(wrap_yacc(config_file) == EES_SUCCESS){
# Line 30  EES_RC start_pdl_parser(char *config_fil Line 30  EES_RC start_pdl_parser(char *config_fil
30    
31      /* strip policies that are not explicitly passed in the names_of_policies array */      /* strip policies that are not explicitly passed in the names_of_policies array */
32      if(number_of_policies){      if(number_of_policies){
33        config->policies_list = reduce_policies(config->policies_list, number_of_policies, names_of_policies);        config.policies_list = reduce_policies(config.policies_list, number_of_policies, names_of_policies);
34      }      }
35    
36      if(remove_unreachable_rules_in_policies(config->policies_list) == EES_SUCCESS){      if(remove_unreachable_rules_in_policies(config.policies_list) == EES_SUCCESS){
37        if(config->policies_list){        if(config.policies_list){
38          link_dead_end_rules_in_policies(config->policies_list);          link_dead_end_rules_in_policies(config.policies_list);
39          link_rules_to_plugins(config->policies_list);          link_rules_to_plugins(config.policies_list);
40        }        }
41        eef_log(LOG_INFO, "Your configuration file seems sane.\n");        eef_log(LOG_INFO, "Your configuration file seems sane.\n");
42        print_policies(config->policies_list);        print_policies(config.policies_list);
43    
44        /* this is a callout to the plugin manager, which dlopen()'s the plugin, dlsym()'s the plugin's functions and calls the plugin's initialize function */        /* this is a callout to the plugin manager, which dlopen()'s the plugin, dlsym()'s the plugin's functions and calls the plugin's initialize function */
45        if(initialize_plugins() == EES_SUCCESS){        if(initialize_plugins() == EES_SUCCESS){
# Line 51  EES_RC start_pdl_parser(char *config_fil Line 51  EES_RC start_pdl_parser(char *config_fil
51        eef_log(LOG_ERR, "The loaded configuration file defines unreachable rules. Please check your configuration file.");        eef_log(LOG_ERR, "The loaded configuration file defines unreachable rules. Please check your configuration file.");
52      }      }
53    } else {    } else {
54      if(config->recursion_was_created){      if(config.recursion_was_created){
55        eef_log(LOG_ERR, "The loaded configuration file defines recursive rules. Please check your configuration file.");        eef_log(LOG_ERR, "The loaded configuration file defines recursive rules. Please check your configuration file.");
56      }      }
57      if(config->unknown_variable_was_referenced){      if(config.unknown_variable_was_referenced){
58        eef_log(LOG_ERR, "The loaded configuration file references undefined variables. Please check your configuration file.");        eef_log(LOG_ERR, "The loaded configuration file references undefined variables. Please check your configuration file.");
59      }      }
60      if(config->starting_state_was_reused){      if(config.starting_state_was_reused){
61        eef_log(LOG_ERR, "The loaded configuration file reuses a starting state. Please check your configuration file.");        eef_log(LOG_ERR, "The loaded configuration file reuses a starting state. Please check your configuration file.");
62      }      }
63    }    }
# Line 67  EES_RC start_pdl_parser(char *config_fil Line 67  EES_RC start_pdl_parser(char *config_fil
67    
68  /*! Initializes the parsing of the configuration file. When parsing has completed successfully, the policy_list is available. This can be transformed to a tree using the link_dead_end_rules_in_policies() function. */  /*! Initializes the parsing of the configuration file. When parsing has completed successfully, the policy_list is available. This can be transformed to a tree using the link_dead_end_rules_in_policies() function. */
69  EES_RC wrap_yacc(const char* config_file){  EES_RC wrap_yacc(const char* config_file){
70    config->config_file_s = config_file;    config.config_file_s = config_file;
71    if((config->config_file_fp = fopen(config->config_file_s,"r")) != NULL){    if((config.config_file_fp = fopen(config.config_file_s,"r")) != NULL){
72      yyin = config->config_file_fp;      yyin = config.config_file_fp;
73      yyparse();      yyparse();
74      if(!config->recursion_was_created && !config->unknown_variable_was_referenced && !config->starting_state_was_reused){      if(!config.recursion_was_created && !config.unknown_variable_was_referenced && !config.starting_state_was_reused){
75        /* callout to the plugin manager */        /* callout to the plugin manager */
76        if(add_plugin_structs() == EES_SUCCESS){        if(add_plugin_structs() == EES_SUCCESS){
77          return EES_SUCCESS;          return EES_SUCCESS;
78        } else {        } else {
79          eef_log(LOG_ERR, "Failed to load plug-ins from policy config file %s\n", config->config_file_s);          eef_log(LOG_ERR, "Failed to load plug-ins from policy config file %s\n", config.config_file_s);
80        }        }
81      }      }
82    } else {    } else {
83      eef_log(LOG_ERR, "Failed to open policy config file %s", config->config_file_s);      eef_log(LOG_ERR, "Failed to open policy config file %s", config.config_file_s);
84    }    }
85    return EES_FAILURE;    return EES_FAILURE;
86  }  }
87    
88  policy_t* get_policies(){  policy_t* get_policies(){
89    return config->policies_list;    return config.policies_list;
90  }  }
91    
92  /*! sets the path to modules directory */  /*! sets the path to modules directory */
93  void set_pdl_path(record_t* path){  void set_pdl_path(record_t* path){
94    size_t path_size = 0;    size_t path_size = 0;
95    size_t string_size = (sizeof(char) * (strlen(path->string)+2)); /* size of string + extra slash + null byte */    size_t string_size = (sizeof(char) * (strlen(path->string)+2)); /* size of string + extra slash + null byte */
96    config->pdl_path = "";    config.pdl_path = "";
97    
98    /*struct stat sb;*/    /*struct stat sb;*/
99    /*if stat(MODUL*/    /*if stat(MODUL*/
# Line 104  void set_pdl_path(record_t* path){ Line 104  void set_pdl_path(record_t* path){
104      path_size = FILENAME_MAX;      path_size = FILENAME_MAX;
105    }    }
106    
107    if((config->pdl_path = calloc(1, path_size))){    if((config.pdl_path = calloc(1, path_size))){
108      strncpy(config->pdl_path, path->string, path_size);      strncpy(config.pdl_path, path->string, path_size);
109                  strncat(config->pdl_path, "/", 1);                  strncat(config.pdl_path, "/", 1);
110    }    }
111    
112    eef_log(LOG_DEBUG, "Found a new modules path: %s\n", config->pdl_path);    eef_log(LOG_DEBUG, "Found a new modules path: %s\n", config.pdl_path);
113    
114    free(path->string);    free(path->string);
115    path->string = NULL;    path->string = NULL;
# Line 123  void add_variable(record_t* name, record Line 123  void add_variable(record_t* name, record
123    eef_log(LOG_DEBUG, "Added variable value: %s\n", value->string);    eef_log(LOG_DEBUG, "Added variable value: %s\n", value->string);
124    
125    /* allocate struct and populate fields */    /* allocate struct and populate fields */
126    if((config->current_variable = calloc(1,sizeof(var_t)))){    if((config.current_variable = calloc(1,sizeof(var_t)))){
127      config->current_variable->name = strdup(name->string);      config.current_variable->name = strdup(name->string);
128      config->current_variable->value = strdup(value->string);      config.current_variable->value = strdup(value->string);
129      config->current_variable->lineno = name->lineno;      config.current_variable->lineno = name->lineno;
130      config->current_variable->next = NULL;      config.current_variable->next = NULL;
131    } else {    } else {
132      eef_log(LOG_ERR, "Out of memory!");      eef_log(LOG_ERR, "Out of memory!");
133    }    }
134    
135    /* append to the end of the list */    /* append to the end of the list */
136    if(config->variables_list){    if(config.variables_list){
137      config->variables_list_last->next = config->current_variable;      config.variables_list_last->next = config.current_variable;
138    } else {    } else {
139      config->variables_list = config->current_variable;      config.variables_list = config.current_variable;
140    }    }
141    config->variables_list_last = config->current_variable;    config.variables_list_last = config.current_variable;
142    
143    /* clean up */    /* clean up */
144    free(name->string);    free(name->string);
# Line 162  rule_t* add_rule(record_t* state, record Line 162  rule_t* add_rule(record_t* state, record
162    var_t  *temp_var = NULL;    var_t  *temp_var = NULL;
163    char *unknown_var_format_string = "Unknown variable %s at line %i in config file %s";    char *unknown_var_format_string = "Unknown variable %s at line %i in config file %s";
164    
165    if((new_rule = get_left_hand_rule(config->rules_list, state->string))){    if((new_rule = get_left_hand_rule(config.rules_list, state->string))){
166      eef_log(LOG_WARNING, "State %s at line %i is already in use at line %i.\n", state->string, state->lineno, new_rule->lineno);      eef_log(LOG_WARNING, "State %s at line %i is already in use at line %i.\n", state->string, state->lineno, new_rule->lineno);
167      config->starting_state_was_reused = 1;      config.starting_state_was_reused = 1;
168    } else {    } else {
169      /*find variables for rule */      /*find variables for rule */
170      temp_var = get_variable_by_name(state->string);      temp_var = get_variable_by_name(state->string);
171      if(temp_var == NULL){      if(temp_var == NULL){
172        /* Errorous state - variable referenced in rule not previously defined */        /* Errorous state - variable referenced in rule not previously defined */
173        eef_log(LOG_ERR, unknown_var_format_string, state->string, state->lineno, config->config_file_s);        eef_log(LOG_ERR, unknown_var_format_string, state->string, state->lineno, config.config_file_s);
174        config->unknown_variable_was_referenced = 1;        config.unknown_variable_was_referenced = 1;
175        new_rule = clean_rules_tree(new_rule);        new_rule = clean_rules_tree(new_rule);
176        goto cleanup;        goto cleanup;
177      }      }
# Line 186  rule_t* add_rule(record_t* state, record Line 186  rule_t* add_rule(record_t* state, record
186            temp_var = get_variable_by_name(false_branch->string);            temp_var = get_variable_by_name(false_branch->string);
187            if(temp_var == NULL){            if(temp_var == NULL){
188              /* Errorous state - variable referenced in rule not previously defined */              /* Errorous state - variable referenced in rule not previously defined */
189              eef_log(LOG_ERR, unknown_var_format_string, false_branch->string, false_branch->lineno, config->config_file_s);              eef_log(LOG_ERR, unknown_var_format_string, false_branch->string, false_branch->lineno, config.config_file_s);
190              config->unknown_variable_was_referenced = 1;              config.unknown_variable_was_referenced = 1;
191              new_rule = clean_rules_tree(new_rule);              new_rule = clean_rules_tree(new_rule);
192              goto cleanup;              goto cleanup;
193            } else {            } else {
# Line 203  rule_t* add_rule(record_t* state, record Line 203  rule_t* add_rule(record_t* state, record
203            temp_var = get_variable_by_name(true_branch->string);            temp_var = get_variable_by_name(true_branch->string);
204            if(temp_var == NULL){            if(temp_var == NULL){
205              /* Errorous state - variable referenced in rule not previously defined */              /* Errorous state - variable referenced in rule not previously defined */
206              eef_log(LOG_ERR, unknown_var_format_string, true_branch->string, true_branch->lineno, config->config_file_s);              eef_log(LOG_ERR, unknown_var_format_string, true_branch->string, true_branch->lineno, config.config_file_s);
207              config->unknown_variable_was_referenced = 1;              config.unknown_variable_was_referenced = 1;
208              new_rule = clean_rules_tree(new_rule);              new_rule = clean_rules_tree(new_rule);
209              goto cleanup;              goto cleanup;
210            } else {            } else {
# Line 217  rule_t* add_rule(record_t* state, record Line 217  rule_t* add_rule(record_t* state, record
217          }          }
218    
219          /* check for recursion */          /* check for recursion */
220          if((recursive_rule = check_for_recursion(config->rules_list, new_rule))){          if((recursive_rule = check_for_recursion(config.rules_list, new_rule))){
221            eef_log(LOG_WARNING, "Rule %s at line %i leads to recursion into state %s", new_rule->state, new_rule->lineno, recursive_rule->state);            eef_log(LOG_WARNING, "Rule %s at line %i leads to recursion into state %s", new_rule->state, new_rule->lineno, recursive_rule->state);
222            new_rule = clean_rules_tree(new_rule);            new_rule = clean_rules_tree(new_rule);
223            config->recursion_was_created = 1;            config.recursion_was_created = 1;
224            goto cleanup;            goto cleanup;
225          } else {          } else {
226            /* add new rule at the end of the rules list */            /* add new rule at the end of the rules list */
227            if(config->rules_list){            if(config.rules_list){
228              config->rules_list_last->next = new_rule;              config.rules_list_last->next = new_rule;
229            } else {            } else {
230              config->rules_list = new_rule;              config.rules_list = new_rule;
231            }            }
232            config->rules_list_last = new_rule;            config.rules_list_last = new_rule;
233    
234            eef_log(LOG_DEBUG, "Added a new rule: %s\n", new_rule->state);            eef_log(LOG_DEBUG, "Added a new rule: %s\n", new_rule->state);
235          }          }
# Line 421  void add_policy(record_t* policy, rule_t Line 421  void add_policy(record_t* policy, rule_t
421    }    }
422    
423    /* append to the end of the list */    /* append to the end of the list */
424    if(config->policies_list){    if(config.policies_list){
425      config->policies_list_last->next = new_policy;      config.policies_list_last->next = new_policy;
426    } else {    } else {
427      config->policies_list = new_policy;      config.policies_list = new_policy;
428    }    }
429    config->policies_list_last = new_policy;    config.policies_list_last = new_policy;
430    
431    /* start a new rules list */    /* start a new rules list */
432    config->rules_list = NULL;    config.rules_list = NULL;
433    
434    free(policy->string);    free(policy->string);
435    policy->string = NULL;    policy->string = NULL;
# Line 572  record_t* concat_strings_with_space(reco Line 572  record_t* concat_strings_with_space(reco
572    
573  /*! logs errors encountered during parsing */  /*! logs errors encountered during parsing */
574  int yyerror(char* string){  int yyerror(char* string){
575    eef_log(LOG_ERR, "Parse error: %s at line %i in config file %s", string, lineno, config->config_file_s);    eef_log(LOG_ERR, "Parse error: %s at line %i in config file %s", string, lineno, config.config_file_s);
576    return 0;    return 0;
577  }  }
578    
579  /*! get variable from list */  /*! get variable from list */
580  var_t* get_variable_by_name(char* name){  var_t* get_variable_by_name(char* name){
581    for(config->current_variable = config->variables_list; config->current_variable != NULL; config->current_variable = config->current_variable->next){    for(config.current_variable = config.variables_list; config.current_variable != NULL; config.current_variable = config.current_variable->next){
582      if(!strncmp(name, config->current_variable->name, strlen(config->current_variable->name))){      if(!strncmp(name, config.current_variable->name, strlen(config.current_variable->name))){
583        return config->current_variable;        return config.current_variable;
584      }      }
585    }    }
586    return NULL;    return NULL;
# Line 588  var_t* get_variable_by_name(char* name){ Line 588  var_t* get_variable_by_name(char* name){
588    
589  /*! Removes a policy from the list of policies */  /*! Removes a policy from the list of policies */
590  void remove_policy(record_t* policy){  void remove_policy(record_t* policy){
591    policy_t *temp_policy = config->policies_list, *next_policy = NULL;    policy_t *temp_policy = config.policies_list, *next_policy = NULL;
592    eef_log(LOG_DEBUG, "Deleted policy: %s\n", policy->string);    eef_log(LOG_DEBUG, "Deleted policy: %s\n", policy->string);
593    
594    while(temp_policy){    while(temp_policy){
# Line 690  char** _var_to_argv(char* value, int *ar Line 690  char** _var_to_argv(char* value, int *ar
690    
691  /*! returns pdl path found in parsed config file */  /*! returns pdl path found in parsed config file */
692  const char* get_pdl_path(){  const char* get_pdl_path(){
693    return config->pdl_path;    return config.pdl_path;
694  }  }
695    
696  /*! Iterates list of policies and the rules they contain and tries to let the plugin manager prepare plugin structs*/  /*! Iterates list of policies and the rules they contain and tries to let the plugin manager prepare plugin structs*/
# Line 698  EES_RC add_plugin_structs(){ Line 698  EES_RC add_plugin_structs(){
698    char**    argv;    char**    argv;
699    int       argc;    int       argc;
700    EES_RC    plugin_struct_added_ok = EES_SUCCESS;    EES_RC    plugin_struct_added_ok = EES_SUCCESS;
701    var_t    *temp_var = config->variables_list;    var_t    *temp_var = config.variables_list;
702    
703    while((temp_var != NULL) && (plugin_struct_added_ok == EES_SUCCESS)){    while((temp_var != NULL) && (plugin_struct_added_ok == EES_SUCCESS)){
704      argv = _var_to_argv(temp_var->value, &argc);      argv = _var_to_argv(temp_var->value, &argc);
# Line 715  EES_RC add_plugin_structs(){ Line 715  EES_RC add_plugin_structs(){
715    
716  /*! Iterates the list of var_t structures and tries to free them */  /*! Iterates the list of var_t structures and tries to free them */
717  EES_RC clean_variables_list(){  EES_RC clean_variables_list(){
718    config->current_variable = config->variables_list;    config.current_variable = config.variables_list;
719    while(config->current_variable){    while(config.current_variable){
720      free(config->current_variable->name);      free(config.current_variable->name);
721      config->current_variable->name = NULL;      config.current_variable->name = NULL;
722      free(config->current_variable->value);      free(config.current_variable->value);
723      config->current_variable->value = NULL;      config.current_variable->value = NULL;
724    
725      /* Move to next variable */      /* Move to next variable */
726      config->variables_list_last = config->current_variable;      config.variables_list_last = config.current_variable;
727      config->current_variable = config->current_variable->next;      config.current_variable = config.current_variable->next;
728    
729      /* Clean last variable struct */      /* Clean last variable struct */
730      free(config->variables_list_last);      free(config.variables_list_last);
731      config->variables_list_last = NULL;      config.variables_list_last = NULL;
732    }    }
733    return EES_SUCCESS;    return EES_SUCCESS;
734  }  }
# Line 793  EES_RC clean_policies_list(policy_t* pol Line 793  EES_RC clean_policies_list(policy_t* pol
793  /*! Terminates the parser and tries to free all used memory */  /*! Terminates the parser and tries to free all used memory */
794  EES_RC pdl_term(){  EES_RC pdl_term(){
795    clean_variables_list();    clean_variables_list();
796    clean_policies_list(config->policies_list);    clean_policies_list(config.policies_list);
797    free(config->pdl_path);    free(config.pdl_path);
798    config->pdl_path = NULL;    config.pdl_path = NULL;
799    
800    if(config->config_file_fp != NULL){    if(config.config_file_fp != NULL){
801      /* This is the preferred way of cleaning up various flex versions - See: http://flex.sourceforge.net/manual/Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e.html#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e */      /* This is the preferred way of cleaning up various flex versions - See: http://flex.sourceforge.net/manual/Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e.html#Memory-leak-_002d-16386-bytes-allocated-by-malloc_002e */
802      #if HAVE_YYLEX_DESTROY /* macro set by configure script */      #if HAVE_YYLEX_DESTROY /* macro set by configure script */
803        yylex_destroy();        yylex_destroy();
# Line 811  EES_RC pdl_term(){ Line 811  EES_RC pdl_term(){
811        #endif        #endif
812      #endif      #endif
813    
814      if((fclose(config->config_file_fp)==0) ){      if((fclose(config.config_file_fp)==0) ){
815        return EES_SUCCESS;        return EES_SUCCESS;
816      }      }
817    }    }

Legend:
Removed from v.1415  
changed lines
  Added in v.1416

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28