2 * @file configuration.c
4 * @brief Configuration class used to store IKE_SA-configurations.
6 * Object of this type represents the configuration for all IKE_SA's and their child_sa's.
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include "configuration_manager.h"
31 #include <utils/allocator.h>
33 typedef struct configuration_entry_t configuration_entry_t
;
35 /* A configuration entry combines a configuration name with a init and sa
36 * configuration represented as init_config_t and sa_config_t objects.
38 struct configuration_entry_t
{
47 * Configuration for IKE_SA_INIT exchange.
49 init_config_t
*init_config
;
52 * Configuration for all phases after IKE_SA_INIT exchange.
54 sa_config_t
*sa_config
;
57 * Destroys a configuration_entry_t
60 * @param this calling object
62 void (*destroy
) (configuration_entry_t
*this);
65 static void configuration_entry_destroy (configuration_entry_t
*this)
67 allocator_free(this->name
);
72 * Creates a configuration_entry_t object
74 * @param name name of the configuration entry (gets copied)
75 * @param init_config object of type init_config_t
76 * @param sa_config object of type sa_config_t
78 configuration_entry_t
* configuration_entry_create(char * name
, init_config_t
* init_config
, sa_config_t
* sa_config
)
80 configuration_entry_t
*entry
= allocator_alloc_thing(configuration_entry_t
);
83 entry
->destroy
= configuration_entry_destroy
;
86 entry
->init_config
= init_config
;
87 entry
->sa_config
= sa_config
;
88 entry
->name
= allocator_alloc(strlen(name
) + 1);
89 strcpy(entry
->name
,name
);
94 typedef struct private_configuration_manager_t private_configuration_manager_t
;
97 * Private data of an configuration_t object
99 struct private_configuration_manager_t
{
102 * Public part of configuration manager.
104 configuration_manager_t
public;
107 * Holding all configurations.
109 linked_list_t
*configurations
;
112 * Holding all init_configs.
114 linked_list_t
*init_configs
;
117 * Holding all init_configs.
119 linked_list_t
*sa_configs
;
123 * Assigned logger object.
128 * Load default configuration
131 * @param this calling object
132 * @param name name for the configuration
133 * @param init_config init_config_t object
134 * @param sa_config sa_config_t object
136 void (*add_new_configuration
) (private_configuration_manager_t
*this, char *name
, init_config_t
*init_config
, sa_config_t
*sa_config
);
139 * Load default configuration
142 * @param this calling object
144 void (*load_default_config
) (private_configuration_manager_t
*this);
148 * Implementation of private_configuration_manager_t.load_default_config.
150 static void load_default_config (private_configuration_manager_t
*this)
152 init_config_t
*init_config1
, *init_config2
, *init_config3
;
153 ike_proposal_t proposals
[2];
154 sa_config_t
*sa_config
;
156 init_config1
= init_config_create("152.96.193.130","152.96.193.131",IKEV2_UDP_PORT
,IKEV2_UDP_PORT
);
157 init_config2
= init_config_create("152.96.193.131","152.96.193.130",IKEV2_UDP_PORT
,IKEV2_UDP_PORT
);
158 init_config3
= init_config_create("0.0.0.0","127.0.0.1",IKEV2_UDP_PORT
,IKEV2_UDP_PORT
);
161 proposals
[0].encryption_algorithm
= ENCR_AES_CBC
;
162 proposals
[0].encryption_algorithm_key_length
= 20;
163 proposals
[0].integrity_algorithm
= AUTH_HMAC_SHA1_96
;
164 proposals
[0].integrity_algorithm_key_length
= 20;
165 proposals
[0].pseudo_random_function
= PRF_HMAC_SHA1
;
166 proposals
[0].pseudo_random_function_key_length
= 20;
167 proposals
[0].diffie_hellman_group
= MODP_768_BIT
;
169 proposals
[1] = proposals
[0];
170 proposals
[1].integrity_algorithm
= AUTH_HMAC_MD5_96
;
171 proposals
[1].integrity_algorithm_key_length
= 16;
172 proposals
[1].pseudo_random_function
= PRF_HMAC_MD5
;
173 proposals
[1].pseudo_random_function_key_length
= 16;
175 init_config1
->add_proposal(init_config1
,1,proposals
[0]);
176 init_config1
->add_proposal(init_config1
,1,proposals
[1]);
177 init_config2
->add_proposal(init_config2
,1,proposals
[0]);
178 init_config2
->add_proposal(init_config2
,1,proposals
[1]);
179 init_config3
->add_proposal(init_config3
,1,proposals
[0]);
180 init_config3
->add_proposal(init_config3
,1,proposals
[1]);
182 this->add_new_configuration(this,"pinflb31",init_config1
,sa_config
);
183 this->add_new_configuration(this,"pinflb30",init_config2
,sa_config
);
184 this->add_new_configuration(this,"localhost",init_config3
,sa_config
);
189 * Implementation of configuration_manager_t.get_init_config_for_host.
191 static status_t
get_init_config_for_host (private_configuration_manager_t
*this, host_t
*my_host
, host_t
*other_host
,init_config_t
**init_config
)
193 iterator_t
*iterator
;
194 status_t status
= NOT_FOUND
;
196 iterator
= this->configurations
->create_iterator(this->configurations
,TRUE
);
198 while (iterator
->has_next(iterator
))
200 configuration_entry_t
*entry
;
201 host_t
*config_my_host
;
202 host_t
*config_other_host
;
204 iterator
->current(iterator
,(void **) &entry
);
206 config_my_host
= entry
->init_config
->get_my_host(entry
->init_config
);
207 config_other_host
= entry
->init_config
->get_other_host(entry
->init_config
);
209 /* first check if ip is equal */
210 if(config_other_host
->ip_is_equal(config_other_host
,other_host
))
212 /* could be right one, check my_host for default route*/
213 if (config_my_host
->is_default_route(config_my_host
))
215 *init_config
= entry
->init_config
;
219 /* check now if host informations are the same */
220 else if (config_my_host
->ip_is_equal(config_my_host
,my_host
))
222 *init_config
= entry
->init_config
;
228 /* Then check for wildcard hosts!
230 * actually its only checked if other host with default route can be found! */
231 else if (config_other_host
->is_default_route(config_other_host
))
233 /* could be right one, check my_host for default route*/
234 if (config_my_host
->is_default_route(config_my_host
))
236 *init_config
= entry
->init_config
;
240 /* check now if host informations are the same */
241 else if (config_my_host
->ip_is_equal(config_my_host
,my_host
))
243 *init_config
= entry
->init_config
;
250 iterator
->destroy(iterator
);
256 * Implementation of configuration_manager_t.get_init_config_for_name.
258 static status_t
get_init_config_for_name (private_configuration_manager_t
*this, char *name
, init_config_t
**init_config
)
260 iterator_t
*iterator
;
261 status_t status
= NOT_FOUND
;
263 iterator
= this->configurations
->create_iterator(this->configurations
,TRUE
);
265 while (iterator
->has_next(iterator
))
267 configuration_entry_t
*entry
;
268 iterator
->current(iterator
,(void **) &entry
);
270 if (strcmp(entry
->name
,name
) == 0)
273 /* found configuration */
274 *init_config
= entry
->init_config
;
280 iterator
->destroy(iterator
);
286 * Implementation of configuration_manager_t.get_sa_config_for_name.
288 static status_t
get_sa_config_for_name (private_configuration_manager_t
*this, char *name
, sa_config_t
**sa_config
)
290 iterator_t
*iterator
;
291 status_t status
= NOT_FOUND
;
293 iterator
= this->configurations
->create_iterator(this->configurations
,TRUE
);
295 while (iterator
->has_next(iterator
))
297 configuration_entry_t
*entry
;
298 iterator
->current(iterator
,(void **) &entry
);
300 if (strcmp(entry
->name
,name
) == 0)
302 /* found configuration */
303 *sa_config
= entry
->sa_config
;
309 iterator
->destroy(iterator
);
315 * Implementation of configuration_manager_t.get_sa_config_for_init_config_and_id.
317 static status_t
get_sa_config_for_init_config_and_id (private_configuration_manager_t
*this, init_config_t
*init_config
, identification_t
*other_id
, identification_t
*my_id
,sa_config_t
**sa_config
)
319 iterator_t
*iterator
;
320 status_t status
= NOT_FOUND
;
322 iterator
= this->configurations
->create_iterator(this->configurations
,TRUE
);
324 while (iterator
->has_next(iterator
))
326 configuration_entry_t
*entry
;
327 iterator
->current(iterator
,(void **) &entry
);
329 if (entry
->init_config
== init_config
)
331 identification_t
*config_my_id
= entry
->sa_config
->get_my_id(entry
->sa_config
);
332 identification_t
*config_other_id
= entry
->sa_config
->get_other_id(entry
->sa_config
);
334 /* host informations seem to be the same */
335 if (config_other_id
->equals(config_other_id
,other_id
))
337 /* other ids seems to match */
341 /* first matching one is selected */
343 /* TODO priorize found entries */
344 *sa_config
= entry
->sa_config
;
349 if (config_my_id
->equals(config_my_id
,my_id
))
351 *sa_config
= entry
->sa_config
;
360 iterator
->destroy(iterator
);
366 * Implementation of private_configuration_manager_t.add_new_configuration.
368 static void add_new_configuration (private_configuration_manager_t
*this, char *name
, init_config_t
*init_config
, sa_config_t
*sa_config
)
370 iterator_t
*iterator
;
373 iterator
= this->init_configs
->create_iterator(this->init_configs
,TRUE
);
375 while (iterator
->has_next(iterator
))
377 init_config_t
*found_init_config
;
378 iterator
->current(iterator
,(void **) &found_init_config
);
379 if (init_config
== found_init_config
)
385 iterator
->destroy(iterator
);
388 this->init_configs
->insert_first(this->init_configs
,init_config
);
391 iterator
= this->sa_configs
->create_iterator(this->sa_configs
,TRUE
);
393 while (iterator
->has_next(iterator
))
395 sa_config_t
*found_sa_config
;
396 iterator
->current(iterator
,(void **) &found_sa_config
);
397 if (sa_config
== found_sa_config
)
403 iterator
->destroy(iterator
);
406 this->sa_configs
->insert_first(this->sa_configs
,sa_config
);
409 this->configurations
->insert_first(this->configurations
,configuration_entry_create(name
,init_config
,sa_config
));
413 * Implementation of configuration_manager_t.destroy.
415 static void destroy(private_configuration_manager_t
*this)
417 this->logger
->log(this->logger
,CONTROL
| MORE
, "Going to destroy configuration manager ");
419 while (this->configurations
->get_count(this->configurations
) > 0)
421 configuration_entry_t
*entry
;
422 this->configurations
->remove_first(this->configurations
,(void **) &entry
);
423 entry
->destroy(entry
);
425 /* todo delete all config objects */
427 this->configurations
->destroy(this->configurations
);
429 while (this->sa_configs
->get_count(this->sa_configs
) > 0)
431 sa_config_t
*sa_config
;
432 this->sa_configs
->remove_first(this->sa_configs
,(void **) &sa_config
);
433 // sa_config->destroy(sa_config);
436 this->sa_configs
->destroy(this->sa_configs
);
438 while (this->init_configs
->get_count(this->init_configs
) > 0)
440 init_config_t
*init_config
;
441 this->init_configs
->remove_first(this->init_configs
,(void **) &init_config
);
442 init_config
->destroy(init_config
);
444 this->init_configs
->destroy(this->init_configs
);
446 this->logger
->log(this->logger
,CONTROL
| MOST
, "Destroy assigned logger");
447 charon
->logger_manager
->destroy_logger(charon
->logger_manager
,this->logger
);
448 allocator_free(this);
452 * Described in header-file
454 configuration_manager_t
*configuration_manager_create()
456 private_configuration_manager_t
*this = allocator_alloc_thing(private_configuration_manager_t
);
458 /* public functions */
459 this->public.destroy
= (void(*)(configuration_manager_t
*))destroy
;
460 this->public.get_init_config_for_name
= (status_t (*) (configuration_manager_t
*, char *, init_config_t
**)) get_init_config_for_name
;
461 this->public.get_init_config_for_host
= (status_t (*) (configuration_manager_t
*, host_t
*, host_t
*,init_config_t
**)) get_init_config_for_host
;
462 this->public.get_sa_config_for_name
=(status_t (*) (configuration_manager_t
*, char *, sa_config_t
**)) get_sa_config_for_name
;
463 this->public.get_sa_config_for_init_config_and_id
=(status_t (*) (configuration_manager_t
*, init_config_t
*, identification_t
*, identification_t
*,sa_config_t
**)) get_sa_config_for_init_config_and_id
;
465 /* private functions */
466 this->load_default_config
= load_default_config
;
467 this->add_new_configuration
= add_new_configuration
;
469 /* private variables */
470 this->logger
= charon
->logger_manager
->create_logger(charon
->logger_manager
,CONFIGURATION_MANAGER
,NULL
);
471 this->configurations
= linked_list_create();
472 this->sa_configs
= linked_list_create();
473 this->init_configs
= linked_list_create();
475 this->load_default_config(this);
477 return (&this->public);