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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1070 - (show annotations) (download) (as text)
Tue Nov 10 09:34:50 2009 UTC (12 years, 2 months ago) by aramv
File MIME type: text/x-chdr
File size: 8017 byte(s)
Added logic to clean up old versions of flex, added some comments to the evaluation manager callbacks
1 #include "pdl.h"
2
3 /* Lex/Yacc stuff */
4 extern FILE * yyin;
5 extern int yylex_destroy(void);
6 extern int yyparse(void);
7 extern int yylex(void);
8 extern int yy_delete_buffer(YY_BUFFER_STATE);
9 extern void delete_lex_buffer(void);
10
11 /* internal data */
12 static char* _pdl_path;
13 FILE* config_file;
14 static var_t* plugin_list;
15 static var_t* plugin_list_last;
16 static var_t* current_plugin;
17 EES_RC config_file_ok;
18
19 /*! initializes the parsing of the configuration file */
20 EES_RC pdl_init(const char* name){
21 if((config_file = fopen(name,"r")) != NULL){
22 yyin = config_file;
23 yyparse();
24 if(config_file_ok){
25 initialize_plugins();
26 }
27 return EES_SUCCESS;
28 } else {
29 eef_log(LOG_ERR, "Failed to open policy file");
30 return EES_FAILURE;
31 }
32 }
33
34 /*! concatenates two strings */
35 record_t* concat_strings(record_t* r1, record_t* r2){
36 record_t* new_record;
37 /*eef_log(LOG_DEBUG, "Concating: %s with %s\n", r1->string, r2->string);*/
38 if((new_record = malloc(sizeof(record_t)))){
39 if((new_record->string =
40 calloc(1, sizeof(char)*(strlen(r1->string) + strlen(r2->string)+1)))){
41 strncat(new_record->string, r1->string, strlen(r1->string));
42 strncat(new_record->string, r2->string, strlen(r2->string));
43 free(r1->string);
44 free(r1);
45 free(r2->string);
46 free(r2);
47 return new_record;
48 }
49 }
50 return NULL;
51 }
52
53 /*! concatenates two strings with a space in between */
54 record_t* concat_strings_with_space(record_t *r1, record_t* r2){
55 record_t *r;
56 /*eef_log(LOG_DEBUG, "Concating: %s with %s with spaces\n", r1->string, r2->string);*/
57 if((r = malloc(sizeof(record_t)))){
58 r->string = calloc(1,(sizeof(char)*(strlen(r1->string)+strlen(r2->string)+2)));
59 strncat(r->string, r1->string, strlen(r1->string));
60 strncat(r->string, " ", 1);
61 strncat(r->string, r2->string, strlen(r2->string));
62 free(r1->string);
63 free(r1);
64 free(r2->string);
65 free(r2);
66 return r;
67 }
68 return NULL;
69 }
70
71 /*! logs errors encountered during parsing */
72 int yyerror(char* string){
73 eef_log(LOG_ERR, string);
74 return 0;
75 }
76
77 /*! sets the path to modules directory */
78 void set_modules_path(record_t* path){
79 size_t path_size = 0;
80 size_t string_size = (sizeof(char) * (strlen(path->string)+2)); /* size of string + extra slash + null byte */
81 _pdl_path = "";
82
83 /*struct stat sb;*/
84 /*if stat(MODUL*/
85
86 if(string_size < FILENAME_MAX){
87 path_size = string_size;
88 } else {
89 path_size = FILENAME_MAX;
90 }
91
92 if((_pdl_path = calloc(1, path_size))){
93 strncpy(_pdl_path, path->string, path_size);
94 strncat(_pdl_path, "/", 1);
95 }
96
97 eef_log(LOG_DEBUG, "Found a new modules path: %s\n", _pdl_path);
98
99 free(path->string);
100 free(path);
101 }
102
103 void add_policy(record_t* policy, rule_t* rule){
104 eef_log(LOG_DEBUG, "Found a new policy: %s\n", policy->string);
105 free(policy->string);
106 free(policy);
107
108 /*free(rule->true_branch);*/
109 /*free(rule->false_branch);*/
110 /*free(rule);*/
111 }
112
113 const char* get_pdl_path(){
114 if(_pdl_path == NULL){
115 return "";
116 }
117 return _pdl_path;
118 }
119
120 /*! Adds a rule */
121 /*TODO describe function better*/
122 rule_t* add_rule(record_t* rule, record_t* true_branch, record_t* false_branch){
123 rule_t* new_rule = malloc(sizeof(rule_t));
124 if(rule != NULL){
125 eef_log(LOG_DEBUG, "Added a new rule: %s\n", rule->string);
126 }
127 if(true_branch != NULL){
128 eef_log(LOG_DEBUG, "True branch: %s\n", true_branch->string);
129 }
130 if(false_branch != NULL){
131 eef_log(LOG_DEBUG, "False branch: %s\n", false_branch->string);
132 }
133
134 /* TODO check for recursion */
135
136 if(rule != NULL){
137 free(rule->string);
138 }
139 free(rule);
140 if(true_branch != NULL){
141 free(true_branch->string);
142 }
143 free(true_branch);
144 if(false_branch != NULL){
145 free(false_branch->string);
146 }
147 free(false_branch);
148 free(new_rule);
149 return new_rule;
150 }
151
152 /*TODO*/
153 /*! Removes a policy from the list of policies */
154 void remove_policy(record_t* policy){
155 eef_log(LOG_DEBUG, "Deleted policy: %s\n", policy->string);
156 free(policy->string);
157 free(policy);
158 }
159
160 /*! converts a string to an array of strings by splitting it at each \t delimiter
161 - overwrites the second argument with a pointer to the number of elements in the array */
162 char** _var_to_argv(char* value, int *argc){
163 char *start_of_arg = NULL, *copy_of_value = NULL, **argv = NULL;
164 char *delimiters = " \t";
165 size_t size_of_arg = 0, size_of_array, i = 0;
166 char *str_ptr;
167 copy_of_value = strdup(value);
168 size_of_array = (sizeof(char)*(strlen(copy_of_value)+1));
169 if((argv = calloc(1, size_of_array)) != NULL){
170 start_of_arg = strtok_r(copy_of_value, delimiters, &str_ptr);
171 while(start_of_arg != NULL){
172 size_of_arg = (sizeof(char)*(strlen(start_of_arg)+1));
173 if((argv[i] = calloc(1, size_of_arg))){
174 memcpy(argv[i], start_of_arg, size_of_arg);
175 start_of_arg = strtok_r(NULL, delimiters, &str_ptr);
176 i++;
177 }
178 }
179 }
180 free(copy_of_value);
181 if(i < ARG_MAX){
182 *argc = i;
183 } else {
184 *argc = ARG_MAX;
185 }
186 return argv;
187 }
188
189 /*! Handles a variable representing a plug-in; creates a structure containing its values and adds it to the list of plug-in structs */
190 void add_variable(record_t* name, record_t* value){
191 eef_log(LOG_DEBUG, "Added variable name: %s\n", name->string);
192 eef_log(LOG_DEBUG, "Added variable value: %s\n", value->string);
193
194 /* allocate and fill fields */
195
196 if((current_plugin = malloc(sizeof(var_t)))){
197 current_plugin->lineno = name->lineno;
198 current_plugin->name = name->string;
199 current_plugin->value = value->string;
200 current_plugin->next = NULL;
201 } else {
202 eef_log(LOG_ERR, "Out of memory!");
203 }
204
205 /* append to the end of the list */
206
207 if(plugin_list){
208 plugin_list_last->next = current_plugin;
209 } else {
210 plugin_list = current_plugin;
211 }
212 plugin_list_last = current_plugin;
213
214 /* clean up */
215
216 free(name->string);
217 free(name);
218 free(value->string);
219 free(value);
220 }
221
222 /*! Iterates the list of plug-in structures and tries to initialize the specified plug-ins */
223 EES_RC initialize_plugins(){
224 char** argv;
225 int argc;
226
227 var_t* last_plugin = NULL;
228 current_plugin = plugin_list;
229 while(current_plugin && config_file_ok){
230 /* prepare arguments and add to plug-in manager */
231 argv = _var_to_argv(current_plugin->value, &argc);
232 /* TODO rename addPlugin to something more uniform */
233 config_file_ok = addPlugin(argc, argv);
234 last_plugin = current_plugin;
235 current_plugin = current_plugin->next;
236 }
237 /*return EES_SUCCESS;*/
238 return config_file_ok;
239 }
240
241 /*! Iterates the list of plug-in structures and tries to free them */
242 EES_RC clean_variables(){
243 var_t* last_plugin = NULL;
244 current_plugin = plugin_list;
245 while(current_plugin){
246 last_plugin = current_plugin;
247 current_plugin = current_plugin->next;
248 free(last_plugin);
249 }
250 return EES_SUCCESS;
251 }
252
253 /*! Terminates the parser and tries to free all used memory */
254 EES_RC pdl_term(){
255 free(_pdl_path);
256 clean_variables();
257 if(config_file != NULL){
258 /* 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 */
259 #if HAVE_YYLEX_DESTROY
260 yylex_destroy();
261 #else
262 eef_log(LOG_WARNING, "Lex function yylex_destroy() not available - possibly unable to free allocated memory for evaluation manager\n");
263 #if HAVE_FLEX
264 delete_lex_buffer();
265 /* eef_log(LOG_INFO, "Managed to free allocated memory for evaluation manager\n"); */
266 #endif
267 #endif
268 if((fclose(config_file)==0) ){
269 return EES_SUCCESS;
270 }
271 }
272 return EES_FAILURE;
273 }
274
275 /*! TODO */
276 EES_RC allow_rules(int val){
277 return EES_SUCCESS;
278 }
279
280 /*! TODO */
281 EES_RC allowed_policy_rule(const char* label){
282 return EES_SUCCESS;
283 }

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