/[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 1234 by aramv, Fri Dec 4 10:07:20 2009 UTC revision 1235 by aramv, Fri Dec 4 11:35:24 2009 UTC
# Line 17  static const char*          config_file_ Line 17  static const char*          config_file_
17  FILE*                       config_file_fp;  FILE*                       config_file_fp;
18  static char*                _pdl_path;  static char*                _pdl_path;
19    
20    int                         recursion_was_created;
21    int                         unknown_variable_was_referenced;
22    
23  var_t*                      variables_list;  var_t*                      variables_list;
24  var_t*                      variables_list_last;  var_t*                      variables_list_last;
25  var_t*                      current_variable;  var_t*                      current_variable;
# Line 29  policy_t*                   policies_lis Line 32  policy_t*                   policies_lis
32  policy_t*                   policies_list_last;  policy_t*                   policies_list_last;
33  policy_t*                   current_policy;  policy_t*                   current_policy;
34    
 /*! initializes the parsing of the configuration file */  
 EES_RC pdl_init(const char* config_file_name){  
 config_file_s = config_file_name;  
35    
36    /* 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. */
37    EES_RC start_pdl_parser(char *config_file, int number_of_policies, char* names_of_policies[]){
38      recursion_was_created = 0;
39    
40      eef_log(LOG_NOTICE, "Loading config file: %s\n", config_file);
41      if(wrap_yacc(config_file) == EES_SUCCESS){
42        /* after the config file was successfully parsed, the policies and path to plugins are accessible. We can try to set the global (EEF-wide) modules path if one was provided */
43        eef_log(LOG_NOTICE, "Loaded config file: %s\n", config_file);
44    
45        /* set the EEF modules path to the path found by the evaluation manager */
46        set_modules_path(get_pdl_path());
47    
48        /* retrieve policy list from the evaluation mananger */
49        parsed_policy_list = get_policies();
50    
51        /* strip policies that are not explicitly passed in the names_of_policies array */
52        if(number_of_policies){
53          parsed_policy_list = reduce_policies(parsed_policy_list, number_of_policies, names_of_policies);
54        }
55    
56        if(remove_unreachable_rules_in_policies(parsed_policy_list) == EES_SUCCESS){
57          if(parsed_policy_list){
58            link_dead_end_rules_in_policies(parsed_policy_list);
59            link_rules_to_plugins(parsed_policy_list);
60          }
61          eef_log(LOG_INFO, "Your configuration file seems sane.\n");
62          print_policies(parsed_policy_list);
63    
64          /* 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 */
65          if(initialize_plugins() == EES_SUCCESS){
66            return EES_SUCCESS;
67          } else {
68            eef_log(LOG_ERR, "Failed to initialize plug-ins from policy config file %s\n", config_file);
69          }
70        } else {
71          eef_log(LOG_ERR, "The loaded configuration file defines unreachable rules. Please check your configuration file.");
72        }
73      } else {
74        if(recursion_was_created){
75          eef_log(LOG_ERR, "The loaded configuration file defines recursive rules. Please check your configuration file.");
76        }
77        if(unknown_variable_was_referenced){
78          eef_log(LOG_ERR, "The loaded configuration file references undefined variables. Please check your configuration file.");
79        }
80      }
81      return EES_FAILURE;
82    }
83    
84    
85    /*! 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. */
86    EES_RC wrap_yacc(const char* config_file){
87      config_file_s = config_file;
88    if((config_file_fp = fopen(config_file_s,"r")) != NULL){    if((config_file_fp = fopen(config_file_s,"r")) != NULL){
89      yyin = config_file_fp;      yyin = config_file_fp;
90      yyparse();      yyparse();
91      if(add_prepared_plugins() == EES_SUCCESS){      if(!recursion_was_created && !unknown_variable_was_referenced){
92        /* strip policies that are not explicitly passed in the names_of_policies array */        /* callout to the plugin manager */
93        if(remove_unreachable_rules_in_policies(policies_list)){        if(add_plugin_structs() == EES_SUCCESS){
94          if(policies_list){          return EES_SUCCESS;
95            link_dead_end_rules_in_policies(policies_list);        } else {
96            link_rules_to_plugins(policies_list);          eef_log(LOG_ERR, "Failed to load plug-ins from policy config file %s\n", config_file_s);
         }  
97        }        }
       print_policies(policies_list);  
       return EES_SUCCESS;  
     } else {  
       eef_log(LOG_ERR, "Failed to load plug-ins from policy config file %s\n", config_file_s);  
98      }      }
99    } else {    } else {
100      eef_log(LOG_ERR, "Failed to open policy config file %s", config_file_s);      eef_log(LOG_ERR, "Failed to open policy config file %s", config_file_s);
# Line 123  void add_variable(record_t* name, record Line 170  void add_variable(record_t* name, record
170    
171  /* TODO I think this function is a bit of a kludge and could probably be rewritten to be more efficient and more legible - I think it might do too much, so extracting some functions might help */  /* TODO I think this function is a bit of a kludge and could probably be rewritten to be more efficient and more legible - I think it might do too much, so extracting some functions might help */
172  /*! Appends a rule to the global rules_list, which is to be added to a policy when add_policy is called  /*! Appends a rule to the global rules_list, which is to be added to a policy when add_policy is called
173   Will return an error when:   Will return NULL and log an error when:
174   - adding the rule to the list will create a recursion in the tree of rules   - adding the rule to the list will create a recursion in the tree of rules
175   - an unknown variable is referenced   - an unknown variable is referenced
176  */  */
# Line 140  rule_t* add_rule(record_t* state, record Line 187  rule_t* add_rule(record_t* state, record
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, state->string, state->lineno, config_file_s);        eef_log(LOG_ERR, unknown_var_format_string, state->string, state->lineno, config_file_s);
190          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      }      }
# Line 155  rule_t* add_rule(record_t* state, record Line 203  rule_t* add_rule(record_t* state, record
203            if(temp_var == NULL){            if(temp_var == NULL){
204              /* Errorous state - variable referenced in rule not previously defined */              /* Errorous state - variable referenced in rule not previously defined */
205              eef_log(LOG_ERR, unknown_var_format_string, false_branch->string, false_branch->lineno, config_file_s);              eef_log(LOG_ERR, unknown_var_format_string, false_branch->string, false_branch->lineno, config_file_s);
206                unknown_variable_was_referenced = 1;
207              new_rule = clean_rules_tree(new_rule);              new_rule = clean_rules_tree(new_rule);
208              goto cleanup;              goto cleanup;
209            } else {            } else {
# Line 171  rule_t* add_rule(record_t* state, record Line 220  rule_t* add_rule(record_t* state, record
220            if(temp_var == NULL){            if(temp_var == NULL){
221              /* Errorous state - variable referenced in rule not previously defined */              /* Errorous state - variable referenced in rule not previously defined */
222              eef_log(LOG_ERR, unknown_var_format_string, true_branch->string, true_branch->lineno, config_file_s);              eef_log(LOG_ERR, unknown_var_format_string, true_branch->string, true_branch->lineno, config_file_s);
223                unknown_variable_was_referenced = 1;
224              new_rule = clean_rules_tree(new_rule);              new_rule = clean_rules_tree(new_rule);
225              goto cleanup;              goto cleanup;
226            } else {            } else {
# Line 186  rule_t* add_rule(record_t* state, record Line 236  rule_t* add_rule(record_t* state, record
236          if((recursive_rule = check_for_recursion(rules_list, new_rule))){          if((recursive_rule = check_for_recursion(rules_list, new_rule))){
237            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);
238            new_rule = clean_rules_tree(new_rule);            new_rule = clean_rules_tree(new_rule);
239              recursion_was_created = 1;
240          } else {          } else {
241            /* add new rule at the end of the rules list */            /* add new rule at the end of the rules list */
242            if(rules_list){            if(rules_list){
# Line 272  rule_t* check_for_recursion(rule_t* rule Line 323  rule_t* check_for_recursion(rule_t* rule
323    return NULL;    return NULL;
324  }  }
325    
326    /* This function iterates the passed list of policies and removes unreachable rules from each policy */
327  EES_RC remove_unreachable_rules_in_policies(policy_t* policies){  EES_RC remove_unreachable_rules_in_policies(policy_t* policies){
328    policy_t *temp_policy = policies;    policy_t *temp_policy = policies;
329    EES_RC retval = EES_SUCCESS;    EES_RC retval = EES_SUCCESS;
330    while(temp_policy){    while(temp_policy){
331      temp_policy->rules = remove_unreachable_rules(temp_policy->rules);      temp_policy->rules = remove_unreachable_rules(temp_policy->rules, &retval);
332      temp_policy = temp_policy->next;      temp_policy = temp_policy->next;
333    }    }
334    return retval;    return retval;
335  }  }
336    
337  /* This function iterates the passed list of rules and removes those rules that, while they exist on the left-hand side, are never referenced on the right-hand side of a rule */  /* This function iterates the passed list of rules and removes those rules that, while they exist on the left-hand side, are never referenced on the right-hand side of a rule in the list */
338  rule_t* remove_unreachable_rules(rule_t* rules){  rule_t* remove_unreachable_rules(rule_t* rules, EES_RC *retval){
339    rule_t *temp_rule = NULL, *next_rule = NULL, *previous_rule = rules;    rule_t *temp_rule = NULL, *next_rule = NULL, *previous_rule = rules;
340    if(rules){    if(rules){
341      temp_rule = rules->next;      temp_rule = rules->next;
# Line 295  rule_t* remove_unreachable_rules(rule_t* Line 347  rule_t* remove_unreachable_rules(rule_t*
347          clean_rules_tree(temp_rule);          clean_rules_tree(temp_rule);
348          previous_rule->next = next_rule;          previous_rule->next = next_rule;
349          temp_rule = previous_rule;          temp_rule = previous_rule;
350            *retval = EES_FAILURE;
351        }        }
352        previous_rule = temp_rule;        previous_rule = temp_rule;
353        temp_rule = next_rule;        temp_rule = next_rule;
# Line 654  const char* get_pdl_path(){ Line 707  const char* get_pdl_path(){
707  }  }
708    
709  /*! 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*/
710  EES_RC add_prepared_plugins(){  EES_RC add_plugin_structs(){
711    char**    argv;    char**    argv;
712    int       argc;    int       argc;
713    EES_RC    plugin_initialized_ok = EES_SUCCESS;    EES_RC    plugin_struct_added_ok = EES_SUCCESS;
714    var_t    *temp_var = variables_list;    var_t    *temp_var = variables_list;
715    
716    while((temp_var != NULL) && (plugin_initialized_ok == EES_SUCCESS)){    while((temp_var != NULL) && (plugin_struct_added_ok == EES_SUCCESS)){
717      argv = _var_to_argv(temp_var->value, &argc);      argv = _var_to_argv(temp_var->value, &argc);
718    
719      /* this is a callout to the plug-in manager, which adds a struct describing a single plug-in to its list */      /* this is a callout to the plug-in manager, which adds a struct describing a single plug-in to its list */
720      plugin_initialized_ok = add_prepared_plugin(argc, argv);      plugin_struct_added_ok = add_plugin_struct(argc, argv);
721    
722      /* Move to next variable */      /* Move to next variable */
723      temp_var = temp_var->next;      temp_var = temp_var->next;
724    }    }
725    
726    return plugin_initialized_ok;    return plugin_struct_added_ok;
727  }  }
728    
729  /*! Iterates the list of var_t structures and tries to free them */  /*! Iterates the list of var_t structures and tries to free them */

Legend:
Removed from v.1234  
changed lines
  Added in v.1235

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