/[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 1212 - (show annotations) (download) (as text)
Wed Dec 2 14:13:38 2009 UTC (12 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 8310 byte(s)
Working on stripping policies. Still some memleaks. :(
1 #include "plugin_manager.h"
2 #include "aos.h"
3 #include "_aos.h"
4
5 /* internal methods */
6
7 int aos_is_initialized(){
8 return _is_initialized;
9 }
10
11 size_t aos_buffer_size(eef_arg_type argtype){
12 switch (argtype){
13 case TYPE_NONE :
14 return 0;
15 case TYPE_SCHAR :
16 return sizeof(int);
17 case TYPE_UCHAR :
18 return sizeof(unsigned int);
19 case TYPE_SHORT :
20 return sizeof(int);
21 case TYPE_USHORT :
22 return sizeof(unsigned int);
23 case TYPE_INT :
24 return sizeof(int);
25 case TYPE_UINT :
26 return sizeof(unsigned int);
27 case TYPE_LONGINT :
28 return sizeof(long int);
29 case TYPE_ULONGINT :
30 return sizeof(unsigned long int);
31 #ifdef HAVE_LONG_LONG_INT
32 case TYPE_LONGLONGINT :
33 return sizeof(long long int);
34 case TYPE_ULONGLONGINT :
35 return sizeof(unsigned long long int);
36 #endif
37 case TYPE_FLOAT :
38 return sizeof(double);
39 case TYPE_DOUBLE :
40 return sizeof(double);
41 #ifdef HAVE_LONG_DOUBLE
42 case TYPE_LONGDOUBLE :
43 return sizeof(long double);
44 #endif
45 case TYPE_CHAR :
46 return sizeof(int);
47 #ifdef HAVE_WINT_T
48 case TYPE_WIDE_CHAR :
49 return sizeof(wint_t);
50 #endif
51 case TYPE_STRING :
52 return sizeof(const char*);
53 #ifdef HAVE_WCHAR_T
54 case TYPE_WIDE_STRING :
55 return sizeof(const wchar_t*);
56 #endif
57 case TYPE_POINTER :
58 return sizeof(void*);
59 case TYPE_COUNT_SCHAR_POINTER :
60 return sizeof(signed char*);
61 case TYPE_COUNT_SHORT_POINTER :
62 return sizeof(short *);
63 case TYPE_COUNT_INT_POINTER :
64 return sizeof(int*);
65 case TYPE_COUNT_LONGINT_POINTER :
66 return sizeof(long int*);
67 #ifdef HAVE_LONG_LONG_INT
68 case TYPE_COUNT_LONGLONGINT_POINTER :
69 return sizeof(long long int*);
70 #endif
71 default:
72 return 0;
73 }
74 }
75
76 /* base functions */
77
78 EES_RC aos_set(const char *label, void *value, eef_arg_type type, size_t arg_bufsize, aos_argument_t * parent, aos_argument_t * child, aos_argument_t **head_node, unsigned int *args_size){
79
80 aos_argument_t * args_new = NULL;
81 aos_argument_t * last_node = NULL;
82 size_t buf_size;
83 buf_size = arg_bufsize;
84
85 if(!aos_is_initialized()){
86 eef_log(LOG_ERR, "AOS is not yet initialized. Aborting.");
87 return EES_FAILURE;
88 }
89
90 if(!(aos_get_value_by_label(label, head_node, args_size) == NULL)){
91 eef_log(LOG_ERR, "Block with label %s already exists.", label);
92 return EES_FAILURE;
93 }
94
95 /* allocate an extra argument in the list */
96 if((args_new = calloc(1, (sizeof(aos_argument_t)))) == NULL){
97 eef_log(LOG_ERR, "Failed to realloc a new argument on the list. %s", strerror(errno));
98 return EES_FAILURE;
99 } else {
100 /*eef_log(LOG_DEBUG, "Malloc succeeded. Argument lists is now %lu elements.\n", (unsigned long) (*args_size));*/
101 }
102
103 if(buf_size == 0) {
104 args_new->data = value;
105 args_new->needs_free = 0;
106 /*eef_log(LOG_DEBUG, "1 pointer allocated at %p. No need to free!\n", args_new->data);*/
107 } else {
108 args_new->data = calloc(1, buf_size);
109 /*eef_log(LOG_DEBUG, "Storing bytes: %i\n", buf_size);*/
110 memcpy(args_new->data, value, buf_size);
111 args_new->needs_free = 1;
112 /*eef_log(LOG_DEBUG, "%i bytes allocated at %p. (index %lu) Remember to free it!\n", buf_size, args_new->data, (unsigned long) (*args_size));*/
113 }
114
115 args_new->type = type;
116 args_new->label = label;
117 args_new->parent = parent;
118 args_new->child = child;
119 args_new->next = NULL;
120 args_new->setting_plugin = get_running_plugin();
121
122 last_node = aos_get_last_node(head_node, args_size);
123 if(last_node != NULL){
124 (last_node)->next = args_new;
125 } else {
126 *head_node = args_new;
127 }
128
129 (*args_size)++;
130 return EES_SUCCESS;
131 }
132
133 aos_argument_t * aos_get_last_node(aos_argument_t **head_node, unsigned int *args_size){
134 aos_argument_t * current_arg;
135 unsigned int i;
136 current_arg = *head_node;
137 for(i = 0; i < (*args_size); i++){
138 if(current_arg->next != NULL){
139 current_arg = current_arg->next;
140 }
141 }
142 return current_arg;
143 }
144
145 void* aos_get_value_by_label(const char *label, aos_argument_t **head_node, unsigned int *args_size){
146 aos_argument_t* arg;
147 arg = aos_get_argument_by_label(label, head_node, args_size);
148 if(arg == NULL){
149 return NULL;
150 } else {
151 return arg->data;
152 }
153 }
154
155 aos_argument_t * aos_get_argument_by_label(const char *label, aos_argument_t **head_node, unsigned int *args_size){
156 unsigned int i;
157 aos_argument_t *current_arg;
158 current_arg = *head_node;
159 for(i = 0; i < (*args_size); i++){
160 if(strncmp(current_arg->label, label, strlen(label))==0){
161 return current_arg;
162 }
163 if(current_arg->next != NULL){
164 current_arg = current_arg->next;
165 }
166 }
167 return NULL;
168 }
169
170 /* frees every copied byte */
171 void aos_free_argslist(aos_argument_t **head_node, unsigned int *args_size) {
172 unsigned int i;
173 aos_argument_t * current_arg;
174 aos_argument_t * next_arg;
175 current_arg = *head_node;
176 for(i = 0; i < (*args_size); i++){
177 /*eef_log(LOG_DEBUG, "Element %i points to %p\n", i, current_arg->data);*/
178 next_arg = current_arg->next;
179 if(current_arg->needs_free){
180 /*eef_log(LOG_DEBUG, "Freeing arg %i\n",i);*/
181 free(current_arg->data);
182 free(current_arg);
183 }
184 current_arg = next_arg;
185 }
186 (*args_size) = 0;
187 }
188
189 EES_RC aos_delete_argument_by_label(const char* label, aos_argument_t **head_node, unsigned int *args_size){
190 aos_argument_t * current_arg = NULL;
191 aos_argument_t * previous_arg = NULL;
192
193 if(label == NULL){
194 return EES_FAILURE;
195 }
196
197 for(current_arg = *head_node; current_arg != NULL; previous_arg = current_arg, current_arg = current_arg->next){
198 if(strncmp(current_arg->label, label, strlen(label)) == 0){
199 if(current_arg->setting_plugin != get_running_plugin()){
200 eef_log(LOG_ERR, "Argument %s is not owned by running plugin %s", current_arg->label, get_running_plugin()->name);
201 return EES_FAILURE;
202 }
203 if(previous_arg == NULL){
204 /* head node deleted. linking start of list to next node */
205 *head_node = current_arg->next;
206 } else {
207 /* link previous to deletee's next */
208 previous_arg->next = current_arg->next;
209 }
210 break;
211 }
212 }
213
214 eef_log(LOG_ERR, "Deleting arg: %s", current_arg->label);
215 free(current_arg->data);
216 free(current_arg);
217 (*args_size)--;
218
219 return EES_SUCCESS;
220 }
221
222 /* initializer & terminator */
223
224 EES_RC AOS_Init (void){
225 size_assertions = calloc(1, sizeof(int));
226 *size_assertions = 0;
227 list_assertions = NULL;
228
229 size_obligations = calloc(1, sizeof(int));
230 *size_obligations = 0;
231 list_obligations = NULL;
232
233 _is_initialized = 1;
234
235 return EES_SUCCESS;
236 }
237
238
239 EES_RC AOS_Term (void){
240 aos_free_argslist(&list_assertions, size_assertions);
241 aos_free_argslist(&list_obligations, size_obligations);
242 free(size_assertions);
243 free(size_obligations);
244 _is_initialized = 0;
245
246 return EES_SUCCESS;
247 }
248
249 /* general high-level wrapper functions */
250
251 EES_RC aos_set_int(const char* label, int value){
252 int* stack_value = &value;
253 return aos_set(label, stack_value, TYPE_INT, 0, NULL, NULL, &list_assertions, size_assertions);
254 }
255
256 EES_RC aos_set_string(const char* label, char* value){
257 return aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
258 }
259
260 EES_RC aos_set_pem(const char* label, char* value){
261 return aos_set(label, value, TYPE_PEM, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
262 }
263
264 /* AOS high-level wrapper functions */
265
266 EES_RC setAssertion(const char* label, char* value){
267 return aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_assertions, size_assertions);
268 }
269
270 char* getAssertion(const char* label){
271 return aos_get_value_by_label(label, &list_assertions, size_assertions);
272 }
273
274 EES_RC setObligation(const char* label, char* value){
275 if(aos_set(label, value, TYPE_STRING, strlen(value)+1, NULL, NULL, &list_obligations, size_obligations)){
276 return EES_SUCCESS;
277 } else {
278 return EES_FAILURE;
279 }
280 }
281
282 char* getObligation(const char* label){
283 return aos_get_value_by_label(label, &list_obligations, size_obligations);
284 }
285
286 EES_RC destroyObligation(const char* label){
287 return aos_delete_argument_by_label(label, &list_obligations, size_obligations);
288 }
289

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