Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

pdl_policy.c

Go to the documentation of this file.
00001 /*                                                                                                            
00002  * Copyright (c) Members of the EGEE Collaboration. 2004.
00003  * See http://eu-egee.org/partners/ for details on the copyright holders.
00004  * For license conditions see the license file or
00005  * http://eu-egee.org/license.html
00006  */
00007 
00008 /*
00009  *   Copyright (c) 2003 EU DataGrid        http://www.eu-datagrid.org/
00010  *
00011  *   $Id: pdl_policy.c,v 1.21 2005/05/27 16:22:20 msteenba Exp $
00012  *
00013  *   Copyright (c) 2003 by
00014  *      G.M. Venekamp <venekamp@nikhef.nl>
00015  *      NIKHEF Amsterdam, the Netherlands
00016  *
00017  *   This software is distributed under a BSD-style open source
00018  *   licence. For a complete description of the licence take a look
00019  *   at: http://eu-datagrid.web.cern.ch/eu-datagrid/license.html
00020  *
00021  */
00022 
00023 
00037 #include <stdarg.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 
00042 #include "lcmaps_log.h"
00043 #include "pdl_policy.h"
00044 
00045 static BOOL policies_reduced = FALSE;       
00046 
00047 static policy_t *top_policy=0, *last_policy=0;
00048 
00049 static int num_of_policies_to_evaluate = 0;
00050 static char** policies_to_evaluate = NULL;
00051 
00052 BOOL _add_policy(const record_t* name, const rule_t* rules);
00053 
00054 
00065 void allow_rules(BOOL allow)
00066 {
00067   allow_new_rules(allow);
00068 }
00069 
00070 
00083 void add_policy(record_t* policy, rule_t* rules)
00084 {
00085   /*
00086    *  Do not switch the order of the and operator, because the
00087    *  _add_policy needs to be done always.
00088    */
00089   if (!_add_policy(policy, rules)) {
00090     free_rules(rules);     rules          = NULL;
00091     free(policy->string);  policy->string = NULL;
00092     free(policy);          policy         = NULL;
00093 
00094     set_yylval(0);
00095   }
00096 
00097   free(policy);
00098   policy = NULL;
00099 
00100   start_new_rules();
00101 };
00102 
00103 
00118 BOOL _add_policy(const record_t* name, const rule_t* rules)
00119 {
00120   policy_t *policy;
00121 
00122   if ((policy = find_policy(name->string))) {
00123     warning(PDL_ERROR, "policy '%s' already defined at line %d.", name->string, policy->lineno);
00124     allow_rules(FALSE);
00125     return FALSE;
00126   }
00127 
00128   if ((policy = (policy_t *)malloc(sizeof(policy_t)))) {
00129     policy->name   = name->string;
00130     policy->rule   = (rule_t*)rules;
00131     policy->lineno = name->lineno;
00132     policy->next   = 0;
00133     policy->prev   = last_policy;
00134 
00135     if (top_policy)
00136       last_policy->next = policy;
00137     else
00138       top_policy = policy;
00139 
00140     last_policy = policy;
00141   } else {
00142     warning(PDL_ERROR, "Out of memory; cannot add policy '%s'.\n", name);
00143     return FALSE;
00144   }
00145 
00146   return TRUE;
00147 }
00148 
00149 
00157 void remove_policy(record_t* policy)
00158 {
00159   free(policy->string);  policy->string = NULL;
00160   free(policy);          policy         = NULL;
00161 }
00162 
00163 
00171 policy_t* find_policy(const char* name)
00172 {
00173   policy_t* policy=top_policy;
00174 
00175   while (policy && strcmp(name, policy->name)!=0) {
00176     policy = policy->next;
00177   }
00178 
00179   return policy;  
00180 }
00181 
00182 
00190 BOOL check_policies_for_recursion(void)
00191 {
00192   BOOL recursion = FALSE;
00193   policy_t* policy = get_policies();
00194 
00195   while (policy) {
00196     lcmaps_log_debug(1, "Checking policy '%s' for recursions.\n", policy->name);
00197 
00198     if (check_rule_for_recursion(policy->rule)) {
00199       recursion = TRUE;
00200       lcmaps_log_debug(1, "Recursions were found.\n");
00201     }
00202     else
00203       lcmaps_log_debug(1, "No recursions were found.\n");
00204 
00205     policy = policy->next;
00206   }
00207 
00208   return recursion;
00209 }
00210 
00211 
00217 void reduce_policies(void)
00218 {
00219   policy_t* policy = get_policies();
00220 
00221   while (policy) {
00222     rule_t* rule = policy->rule;
00223     set_top_rule(rule);
00224 
00225     while (rule) {
00226       reduce_rule(rule);
00227       rule = rule->next;
00228     }
00229 
00230     policy = policy->next;
00231   }
00232 
00233   policies_reduced = TRUE;
00234 }
00235 
00236 
00237 
00244 policy_t* get_policies(void)
00245 {
00246   return top_policy;
00247 }
00248 
00249 
00254 void show_policies(void)
00255 {
00256   policy_t* policy = top_policy;
00257 
00258   while (policy) {
00259     lcmaps_log_debug(1, "policy: %s\n", policy->name);
00260     show_rules(policy->rule);
00261 
00262     policy = policy->next;
00263   }
00264 }
00265 
00266 
00275 void cleanup_policies(void)
00276 {
00277   policy_t* policy = top_policy;
00278 
00279   while (policy) {
00280     if (!policy->rule) {
00281       if (policy->prev) {
00282         policy->prev->next = policy->next;
00283       }
00284       else
00285       {
00286         /* move the top_policy to the new head of the list */
00287         top_policy = policy->next;
00288       }
00289       if (policy->next) {
00290         policy->next->prev = policy->prev;
00291       }
00292       policy_t* tmp = policy;
00293       policy = policy->next;
00294       free (tmp);
00295     } else {
00296       policy = policy->next;
00297     }
00298   }
00299 }
00300 
00301 
00306 void free_policies(void)
00307 {
00308   policy_t* next_pol, *policy = top_policy;
00309 
00310   while (policy) {
00311     next_pol = policy->next;
00312     free((char *)policy->name);  policy->name = NULL;
00313     free_rules(policy->rule);    policy->rule = NULL;
00314     free(policy);
00315     policy = next_pol;
00316   }
00317 
00318   top_policy = 0;
00319   //  When there is no policy defined, it does not make sense to have a
00320   //  top rule either.
00321   set_top_rule(0);
00322 }
00323 
00324 
00332 BOOL allowed_policy_rule(const char* label)
00333 {
00334   BOOL allowed = TRUE;
00335   int i=0;
00336 
00337   if (num_of_policies_to_evaluate > 0)
00338   {
00339     allowed = FALSE;
00340     while (i<num_of_policies_to_evaluate) {
00341       if (strcmp(label, policies_to_evaluate[i]) == 0) {
00342         allowed = TRUE;
00343         break;
00344       }
00345       i++;
00346     }
00347   }
00348 
00349   return allowed;
00350 }
00351 
00352 
00356 void SetSetOfRules(int argc, char* argv[])
00357 {
00358   num_of_policies_to_evaluate = argc;
00359   policies_to_evaluate = argv;
00360 }
00361 
00368 BOOL policies_have_been_reduced(void)
00369 {
00370   return policies_reduced;
00371 }

Generated on Sun May 29 21:22:11 2005 for lcmaps by doxygen 1.3.5