00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00030 #include <stdarg.h>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 #include "lcmaps_log.h"
00036 #include "pdl_policy.h"
00037
00038 static BOOL policies_reduced = FALSE;
00039
00040 static policy_t *top_policy=0, *last_policy=0;
00041
00042 BOOL _add_policy(const record_t* name, const rule_t* rules);
00043
00044
00055 void allow_rules(BOOL allow)
00056 {
00057 allow_new_rules(allow);
00058 }
00059
00060
00073 void add_policy(record_t* policy, rule_t* rules)
00074 {
00075
00076
00077
00078
00079 if (!_add_policy(policy, rules)) {
00080 free_rules(rules);
00081 free(policy->string);
00082 free(policy);
00083
00084 set_yylval(0);
00085 }
00086
00087 free(policy);
00088
00089 start_new_rules();
00090 };
00091
00092
00107 BOOL _add_policy(const record_t* name, const rule_t* rules)
00108 {
00109 policy_t *policy;
00110
00111 if ((policy = find_policy(name->string))) {
00112 warning(PDL_ERROR, "policy '%s' already defined at line %d.", name->string, policy->lineno);
00113 allow_rules(FALSE);
00114 return FALSE;
00115 }
00116
00117 if ((policy = (policy_t *)malloc(sizeof(policy_t)))) {
00118 policy->name = name->string;
00119 policy->rule = (rule_t*)rules;
00120 policy->lineno = name->lineno;
00121 policy->next = 0;
00122 policy->prev = last_policy;
00123
00124 if (top_policy)
00125 last_policy->next = policy;
00126 else
00127 top_policy = policy;
00128
00129 last_policy = policy;
00130 } else {
00131 warning(PDL_ERROR, "Out of memory; cannot add policy '%s'.\n", name);
00132 return FALSE;
00133 }
00134
00135 return TRUE;
00136 }
00137
00138
00146 void remove_policy(record_t* policy)
00147 {
00148 free(policy->string);
00149 free(policy);
00150 }
00151
00152
00160 policy_t* find_policy(const char* name)
00161 {
00162 policy_t* policy=top_policy;
00163
00164 while (policy && strcmp(name, policy->name)!=0) {
00165 policy = policy->next;
00166 }
00167
00168 return policy;
00169 }
00170
00171
00179 BOOL check_policies_for_recursion(void)
00180 {
00181 BOOL recursion = FALSE;
00182 policy_t* policy = get_policies();
00183
00184 while (policy) {
00185 lcmaps_log_debug(1, "Checking policy '%s' for recursions.\n", policy->name);
00186
00187 if (check_rule_for_recursion(policy->rule)) {
00188 recursion = TRUE;
00189 lcmaps_log_debug(1, "Recursions were found.\n");
00190 }
00191 else
00192 lcmaps_log_debug(1, "No recursions were found.\n");
00193
00194 policy = policy->next;
00195 }
00196
00197 return recursion;
00198 }
00199
00200
00206 void reduce_policies(void)
00207 {
00208 policy_t* policy = get_policies();
00209
00210 while (policy) {
00211 rule_t* rule = policy->rule;
00212 set_top_rule(rule);
00213
00214 while (rule) {
00215 reduce_rule(rule);
00216 rule = rule->next;
00217 }
00218
00219 policy = policy->next;
00220 }
00221
00222 policies_reduced = TRUE;
00223 }
00224
00225
00226
00233 policy_t* get_policies(void)
00234 {
00235 return top_policy;
00236 }
00237
00238
00243 void show_policies(void)
00244 {
00245 policy_t* policy = top_policy;
00246
00247 while (policy) {
00248 lcmaps_log_debug(1, "policy: %s\n", policy->name);
00249 show_rules(policy->rule);
00250
00251 policy = policy->next;
00252 }
00253 }
00254
00255
00260 void free_policies(void)
00261 {
00262 policy_t* next_pol, *policy = top_policy;
00263
00264 while (policy) {
00265 next_pol = policy->next;
00266 free((char *)policy->name);
00267 free_rules(policy->rule);
00268 free(policy);
00269 policy = next_pol;
00270 }
00271
00272 top_policy = 0;
00273 }
00274
00275
00282 BOOL policies_have_been_reduced(void)
00283 {
00284 return policies_reduced;
00285 }