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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1569 - (show annotations) (download) (as text)
Tue Mar 16 17:35:17 2010 UTC (11 years, 10 months ago) by aramv
File MIME type: text/x-chdr
File size: 13075 byte(s)
Re-added AOS cleaning, so reduced memory leaks by quite a bit
1 #include "plugin_manager.h"
2 #include "_aos.h"
3 #include "eef_aos.h"
4
5 /* internal methods */
6
7 /**
8 * returns _is_initialized
9 */
10 int aos_is_initialized(){
11 return _is_initialized;
12 }
13
14 /**
15 * sets threading flag
16 */
17 void aos_start_threading(){
18 _is_threading = 1;
19 }
20
21 /**
22 * returns a pointer to an aos_storage_t struct that corresponds to the current thread context. If the current context is not threaded, a pointer to a global storage struct is returned.
23 */
24 aos_storage_t * aos_get_storage(){
25 pid_t tid;
26 aos_storage_t * storage = NULL;
27
28 tid = syscall(SYS_gettid);
29 /*tid = gettid();*/
30
31 if(!_is_threading){
32 return _global_storage;
33 }
34
35 /* Try to get TLS */
36 storage = pthread_getspecific(_aos_key);
37 if(storage == 0) {
38 /* Create new TLS */
39 if((storage = calloc(1, sizeof(aos_storage_t)))){
40 storage->last_context = NULL;
41 pthread_setspecific(_aos_key, storage);
42 return storage;
43 }
44 } else {
45 return storage;
46 }
47 return NULL;
48 }
49
50 /**
51 * Frees storage struct pointed to by storage pointer
52 */
53 EES_RC aos_free_storage(aos_storage_t* storage){
54 aos_context_t *context = NULL, *next_context = NULL;
55 aos_attribute_t *attribute = NULL, *next_attribute = NULL;
56 int attribute_count = 0;
57
58 if(!storage){
59 return EES_FAILURE;
60 }
61
62 eef_log(LOG_DEBUG, "Cleaning aos storage at %p\n", storage);
63
64 context = storage->list_contexts;
65
66 while(context){
67 next_context = context->next;
68 attribute = context->list_attributes;
69 while(attribute){
70 next_attribute = attribute->next;
71 /*free(attribute->id);*/
72 free(attribute->issuer);
73 /*free(attribute->type);*/
74 if(attribute->needs_free){
75 eef_log(LOG_DEBUG, "Freeing arg data %i at %p\n",attribute_count, attribute->data);
76 free(attribute->data);
77 }
78 free(attribute);
79 attribute = next_attribute;
80 attribute_count++;
81 }
82 /* free here */
83 eef_log(LOG_DEBUG, "Freeing context at %p\n", context);
84 free(context);
85 context = next_context;
86 }
87 free(storage);
88 return EES_SUCCESS;
89 }
90
91 /* base functions */
92
93 /**
94 * Returns a pointer to a freshly allocated aos_context_t struct
95 */
96 aos_context_t * createContext(aos_context_class_t context_class){
97 aos_context_t * context;
98 if((context = calloc(1, sizeof(aos_context_t)))){
99 context->context_class = context_class;
100 context->list_attributes = NULL;
101 context->last_attribute = NULL;
102 context->next = NULL;
103 }
104 return context;
105 }
106
107 /**
108 * Adds supplied context to storage attained through aos_get_storage()
109 */
110 EES_RC addContext(aos_context_t* context){
111 aos_storage_t* storage = aos_get_storage();
112 /*eef_log(LOG_DEBUG, "Adding context at %p to storage at %p\n", context, storage);*/
113 if(storage->last_context){
114 /*eef_log(LOG_DEBUG, "Inserted at %p\n", storage->last_context);*/
115 storage->last_context->next = context;
116 } else {
117 /*eef_log(LOG_DEBUG, "Created at %p\n", storage->list_contexts);*/
118 storage->list_contexts = context;
119 storage->last_context = NULL;
120 }
121 /*eef_log(LOG_DEBUG, "Copied context to %p\n", storage->last_context);*/
122 storage->last_context = context;
123 return EES_SUCCESS;
124 }
125
126 EES_RC rewindContexts(aos_storage_t* storage){
127 if(storage == NULL){
128 storage = aos_get_storage();
129 }
130 storage->last_context = NULL;
131 return EES_SUCCESS;
132 }
133
134 /**
135 * returns the next aos_context_t pointer with the supplied context_class from the list of contexts in the supplied storage
136 */
137 aos_context_t * getNextContext(aos_context_class_t context_class, aos_storage_t* storage){
138 aos_context_t* current_context = NULL;
139 aos_storage_t* current_storage = NULL;
140
141 if(storage != NULL){
142 /* Using passed storage */
143 current_storage = storage;
144 } else {
145 /* Using TLS getter */
146 current_storage = aos_get_storage();
147 }
148
149 if(current_storage != NULL){
150 printf("Got storage\n");
151 if(current_storage->last_context != NULL){
152 /* Pick up from last context */
153 current_context = current_storage->last_context->next;
154 } else {
155 /* Reset current_context to start of list */
156 current_context = current_storage->list_contexts;
157 }
158 }
159
160 if(current_context != NULL){
161 printf("Got context\n");
162 if(context_class != ANY){
163 printf("Looking for context with class %i\n", context_class);
164 while(current_context != NULL){
165 printf("Checking context at %p\n", current_context);
166 printf("This one has %i\n", current_context->context_class);
167 if((current_context->context_class == context_class)){
168 /* Found context */
169 printf("Found context\n");
170 break;
171 }
172 current_context = current_context->next;
173 }
174 }
175 }
176
177 current_storage->last_context = current_context;
178
179 if(current_context != NULL){
180 /* Found something on the first go */
181 return current_context;
182 } else {
183 if(current_storage != _global_storage){
184 /* Retry with global storage */
185 return getNextContext(context_class, _global_storage);
186 }
187 }
188 return NULL;
189 }
190
191 /**
192 * Sets the obligation name in the struct pointed to by the supplied pointer context to supplied obligation
193 */
194 void setContextObligationId(aos_context_t* context, char * obligation){
195 if(context->context_class == OBLIGATION){
196 context->obligation_name = obligation;
197 }
198 }
199
200 /**
201 * Returns the obligation name from the supplied context
202 */
203 char* getContextObligationId(aos_context_t* context){
204 return context->obligation_name;
205 }
206
207 /**
208 * Returns a pointer to a freshly allocated aos_attribute_t struct
209 */
210 aos_attribute_t* createAttribute(void){
211 aos_attribute_t* attribute = NULL;
212 if((attribute = calloc(1, sizeof(aos_attribute_t)))){
213 attribute->next = NULL;
214 attribute->setting_plugin = get_running_plugin();
215 }
216 return attribute;
217 }
218
219 /**
220 * Adds supplied attribute to supplied context
221 */
222 EES_RC addAttribute(aos_context_t * context, aos_attribute_t* attribute){
223 if(context->last_attribute){
224 context->last_attribute->next = attribute;
225 } else {
226 context->list_attributes = attribute;
227 context->last_attribute = NULL;
228 }
229 context->last_attribute = attribute;
230 return EES_SUCCESS;
231 }
232
233 EES_RC rewindAttributes(aos_context_t* context){
234 context->last_attribute = NULL;
235 return EES_SUCCESS;
236 }
237
238 /**
239 * returns the next aos_attribute_t pointer from list of attributes in the supplied context
240 */
241 aos_attribute_t* getNextAttribute(aos_context_t* context){
242 aos_attribute_t* attribute = NULL;
243 if(context->last_attribute != NULL){
244 attribute = context->last_attribute->next;
245 } else {
246 attribute = context->list_attributes;
247 }
248 context->last_attribute = attribute;
249 return attribute;
250 }
251
252 /**
253 * removes a supplied attribute from the supplied context
254 */
255 EES_RC destroyAttribute(aos_context_t* context, aos_attribute_t* attribute){
256 aos_attribute_t *last_attribute = NULL, *current_attribute = NULL;
257 for(current_attribute = context->list_attributes; current_attribute != NULL; last_attribute = current_attribute, current_attribute = current_attribute->next){
258 if(current_attribute == attribute){
259 if(current_attribute->setting_plugin != get_running_plugin()){
260 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_attribute->id, get_running_plugin()->name);
261 return EES_FAILURE;
262 }
263 free(attribute->data);
264 free(attribute);
265 attribute = NULL;
266 if(last_attribute == NULL){
267 context->list_attributes = current_attribute->next;
268 } else {
269 last_attribute->next = current_attribute->next;
270 }
271 break;
272 }
273 }
274
275 if(current_attribute){
276 free(current_attribute->data);
277 free(current_attribute);
278 current_attribute = NULL;
279 return EES_SUCCESS;
280 }
281 return EES_FAILURE;
282 }
283
284 /**
285 * sets attribute id in supplied attribute
286 */
287 EES_RC setAttributeId(aos_attribute_t* attribute, char* id){
288 attribute->id = id;
289 return EES_SUCCESS;
290 }
291
292 /**
293 * sets attribute issuer in supplied attribute
294 */
295 EES_RC setAttributeIssuer(aos_attribute_t* attribute, char* issuer){
296 attribute->issuer = issuer;
297 return EES_SUCCESS;
298 }
299
300 /**
301 * sets attribute value in supplied attribute
302 */
303 EES_RC setAttributeValue(aos_attribute_t* attribute, void* value, size_t size){
304 if(size == 0) {
305 attribute->data = value;
306 attribute->needs_free = 0;
307 } else {
308 attribute->data = calloc(1, size);
309 strncpy(attribute->data, value, size);
310 attribute->needs_free = 1;
311 }
312 return EES_SUCCESS;
313 }
314
315 /**
316 * sets attribute type in supplied attribute
317 */
318 EES_RC setAttributeType(aos_attribute_t * attribute, char* type){
319 attribute->type = type;
320 return EES_SUCCESS;
321 }
322
323 /**
324 * returns id from supplied attribute
325 */
326 char* getAttributeId(aos_attribute_t* attribute){
327 return attribute->id;
328 }
329
330 /**
331 * returns issuer from supplied attribute
332 */
333 char* getAttributeIssuer(aos_attribute_t* attribute){
334 return attribute->issuer;
335 }
336
337 /**
338 * returns data from supplied attribute casted to a char*
339 */
340 char* getAttributeValueAsString(aos_attribute_t* attribute){
341 return (char*) attribute->data;
342 }
343
344 /**
345 * returns data from supplied attribute casted to a int
346 */
347 int getAttributeValueAsInt(aos_attribute_t* attribute){
348 return (int) (attribute->data);
349 }
350
351 void* aos_get_value_by_label(const char *label){
352 aos_attribute_t* attr = aos_get_attribute_by_label(label);
353 if(attr == NULL){
354 return NULL;
355 } else {
356 return attr->data;
357 }
358 }
359
360 aos_attribute_t * aos_get_attribute_by_label(const char *label){
361 aos_context_t * context;
362 aos_attribute_t * attribute;
363 aos_storage_t * storage = aos_get_storage();
364
365 while((context = getNextContext(ANY, storage))){
366 while((attribute = getNextAttribute(context))){
367 if(strncmp(attribute->id, label, strlen(label)) ==0){
368 return attribute;
369 }
370 }
371 }
372
373 storage = _global_storage;
374
375 while((context = getNextContext(ANY, storage))){
376 while((attribute = getNextAttribute(context))){
377 if(strncmp(attribute->id, label, strlen(label)) ==0){
378 return attribute;
379 }
380 }
381 }
382
383 return NULL;
384 }
385
386 /* AOS control functions */
387
388 /**
389 * Initializes the AOS and creates thread-local storage key
390 */
391 EES_RC AOS_Init (void){
392 _is_threading = 0;
393 _aos_key_once = PTHREAD_ONCE_INIT;
394 pthread_once(&_aos_key_once, aos_make_key);
395
396 if(!(_global_storage = calloc(1, sizeof(aos_storage_t)))){
397 return EES_FAILURE;
398 }
399
400 _is_initialized = 1;
401 return EES_SUCCESS;
402 }
403
404 /**
405 * Creates thread-local storage key
406 */
407 void aos_make_key(void){
408 pthread_key_create(&_aos_key, aos_free_key);
409 }
410
411 /**
412 * Frees thread-local storage key
413 */
414 void aos_free_key(void* storage){
415 pid_t tid;
416 /*tid = gettid();*/
417 tid = syscall(SYS_gettid);
418 eef_log(LOG_DEBUG, "Killing thread %i\n", tid);
419 aos_free_storage(storage);
420 }
421
422 /**
423 * Frees thread-local storage for this thread
424 */
425 EES_RC AOS_Clean(void){
426 pid_t tid;
427 /*tid = gettid();*/
428 tid = syscall(SYS_gettid);
429 eef_log(LOG_DEBUG, "Killing thread %i\n", tid);
430 if(!_is_threading){
431 return EES_FAILURE;
432 }
433 aos_free_storage(aos_get_storage());
434 return EES_SUCCESS;
435 }
436
437 /**
438 * Terminates AOS, deleting the thread-local storage key
439 */
440 EES_RC AOS_Term (void){
441 _is_threading = 0;
442 aos_free_storage(_global_storage);
443 pthread_setspecific(_aos_key, NULL);
444 pthread_key_delete(_aos_key);
445 _is_initialized = 0;
446 return EES_SUCCESS;
447 }
448
449 /**
450 * Dumps out all the data present in the AOS, as seen from the current thread's perspective
451 */
452 EES_RC aos_dump_argslist (void) {
453 aos_context_t * context;
454 aos_attribute_t* attribute;
455 char *log_str = "aos_dump";
456 char *attribute_name = "", *attribute_value = "";
457 eef_log(LOG_DEBUG, "Dumping aos");
458 rewindContexts(aos_get_storage());
459 while((context = getNextContext(ANY, NULL)) != NULL){
460 if(context->context_class == OBLIGATION){
461 eef_log(LOG_DEBUG, "Context obligation: %s\n", context->obligation_name);
462 }
463 rewindAttributes(context);
464 while((attribute = getNextAttribute(context))){
465 attribute_name = getAttributeId(attribute);
466 attribute_value = getAttributeValueAsString(attribute);
467 eef_log(LOG_DEBUG, "Attribute: %s with value: %s\n", attribute_name, attribute_value);
468 }
469 }
470 return EES_SUCCESS;
471 }
472
473 /*EES_PL_RC plugin_run(){*/
474 /*printf("Examining AOS\n");*/
475 /*while((_context = getNextContext(OBLIGATION, NULL))){*/
476 /*printf("Context: %s\n", getContextObligationId(_context));*/
477 /*if(strncmp(getContextObligationId(_context), "uidgid", strlen("uidgid")) == 0){*/
478 /*while((_attribute = getNextAttribute(_context))){*/
479 /*if(strncmp(getAttributeId(_attribute), "posix-uid", strlen("posix-uid")) == 0){*/
480 /*printf("Got UID: %s\n", getAttributeId(_attribute));*/
481 /*_real_uid = getAttributeValueAsInt(_attribute);*/
482 /*} else if(strncmp(getAttributeId(_attribute), "posix-gid", strlen("posix-gid")) == 0){*/
483 /*printf("Got primary GID: %s\n", getAttributeId(_attribute));*/
484 /*}*/
485 /*}*/
486 /*}*/
487 /*}*/
488
489 /*if(_do_uid_check){*/
490 /**//*if(getpwuid()){*/
491
492 /**//*}*/
493 /*}*/
494 /*downgradeEffectiveToRealUid(_real_uid, _saved_uid);*/
495 /*endpwent();*/
496
497 /*return 0;*/
498 /*}*/
499

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