2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include "medcli_config.h"
22 #include <processing/jobs/callback_job.h>
24 typedef struct private_medcli_config_t private_medcli_config_t
;
27 * Private data of an medcli_config_t object
29 struct private_medcli_config_t
{
34 medcli_config_t
public;
58 * create a traffic selector from a CIDR notation string
60 static traffic_selector_t
*ts_from_string(char *str
)
69 pos
= strchr(str
, '/');
82 net
= host_create_from_string(str
, 0);
85 return traffic_selector_create_from_subnet(net
, netbits
, 0, 0);
88 return traffic_selector_create_dynamic(0, 0, 65535);
91 METHOD(backend_t
, get_peer_cfg_by_name
, peer_cfg_t
*,
92 private_medcli_config_t
*this, char *name
)
95 peer_cfg_t
*peer_cfg
, *med_cfg
;
98 child_cfg_t
*child_cfg
;
100 char *address
, *local_net
, *remote_net
;
101 lifetime_cfg_t lifetime
= {
103 .life
= this->rekey
* 60 + this->rekey
,
104 .rekey
= this->rekey
,
105 .jitter
= this->rekey
109 /* query mediation server config:
110 * - build ike_cfg/peer_cfg for mediation connection on-the-fly
112 e
= this->db
->query(this->db
,
113 "SELECT Address, ClientConfig.KeyId, MediationServerConfig.KeyId "
114 "FROM MediationServerConfig JOIN ClientConfig",
115 DB_TEXT
, DB_BLOB
, DB_BLOB
);
116 if (!e
|| !e
->enumerate(e
, &address
, &me
, &other
))
121 ike_cfg
= ike_cfg_create(FALSE
, FALSE
,
122 "0.0.0.0", IKEV2_UDP_PORT
, address
, IKEV2_UDP_PORT
);
123 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(PROTO_IKE
));
124 med_cfg
= peer_cfg_create(
125 "mediation", IKEV2
, ike_cfg
,
126 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
127 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
128 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
129 TRUE
, FALSE
, /* mobike, aggressive */
130 this->dpd
, 0, /* DPD delay, timeout */
131 NULL
, NULL
, /* vip, pool */
132 TRUE
, NULL
, NULL
); /* mediation, med by, peer id */
135 auth
= auth_cfg_create();
136 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
137 auth
->add(auth
, AUTH_RULE_IDENTITY
,
138 identification_create_from_encoding(ID_KEY_ID
, me
));
139 med_cfg
->add_auth_cfg(med_cfg
, auth
, TRUE
);
140 auth
= auth_cfg_create();
141 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
142 auth
->add(auth
, AUTH_RULE_IDENTITY
,
143 identification_create_from_encoding(ID_KEY_ID
, other
));
144 med_cfg
->add_auth_cfg(med_cfg
, auth
, FALSE
);
146 /* query mediated config:
147 * - use any-any ike_cfg
148 * - build peer_cfg on-the-fly using med_cfg
151 e
= this->db
->query(this->db
,
152 "SELECT ClientConfig.KeyId, Connection.KeyId, "
153 "Connection.LocalSubnet, Connection.RemoteSubnet "
154 "FROM ClientConfig JOIN Connection "
155 "WHERE Active AND Alias = ?", DB_TEXT
, name
,
156 DB_BLOB
, DB_BLOB
, DB_TEXT
, DB_TEXT
);
157 if (!e
|| !e
->enumerate(e
, &me
, &other
, &local_net
, &remote_net
))
162 peer_cfg
= peer_cfg_create(
163 name
, IKEV2
, this->ike
->get_ref(this->ike
),
164 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
165 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
166 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
167 TRUE
, FALSE
, /* mobike, aggressive */
168 this->dpd
, 0, /* DPD delay, timeout */
169 NULL
, NULL
, /* vip, pool */
170 FALSE
, med_cfg
, /* mediation, med by */
171 identification_create_from_encoding(ID_KEY_ID
, other
));
173 auth
= auth_cfg_create();
174 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
175 auth
->add(auth
, AUTH_RULE_IDENTITY
,
176 identification_create_from_encoding(ID_KEY_ID
, me
));
177 peer_cfg
->add_auth_cfg(peer_cfg
, auth
, TRUE
);
178 auth
= auth_cfg_create();
179 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
180 auth
->add(auth
, AUTH_RULE_IDENTITY
,
181 identification_create_from_encoding(ID_KEY_ID
, other
));
182 peer_cfg
->add_auth_cfg(peer_cfg
, auth
, FALSE
);
184 child_cfg
= child_cfg_create(name
, &lifetime
, NULL
, TRUE
, MODE_TUNNEL
,
185 ACTION_NONE
, ACTION_NONE
, ACTION_NONE
, FALSE
,
186 0, 0, NULL
, NULL
, 0);
187 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
188 child_cfg
->add_traffic_selector(child_cfg
, TRUE
, ts_from_string(local_net
));
189 child_cfg
->add_traffic_selector(child_cfg
, FALSE
, ts_from_string(remote_net
));
190 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
195 METHOD(backend_t
, create_ike_cfg_enumerator
, enumerator_t
*,
196 private_medcli_config_t
*this, host_t
*me
, host_t
*other
)
198 return enumerator_create_single(this->ike
, NULL
);
202 /** implements enumerator */
204 /** inner SQL enumerator */
206 /** currently enumerated peer config */
208 /** ike cfg to use in peer cfg */
216 METHOD(enumerator_t
, peer_enumerator_enumerate
, bool,
217 peer_enumerator_t
*this, peer_cfg_t
**cfg
)
219 char *name
, *local_net
, *remote_net
;
221 child_cfg_t
*child_cfg
;
223 lifetime_cfg_t lifetime
= {
225 .life
= this->rekey
* 60 + this->rekey
,
226 .rekey
= this->rekey
,
227 .jitter
= this->rekey
231 DESTROY_IF(this->current
);
232 if (!this->inner
->enumerate(this->inner
, &name
, &me
, &other
,
233 &local_net
, &remote_net
))
235 this->current
= NULL
;
238 this->current
= peer_cfg_create(
239 name
, IKEV2
, this->ike
->get_ref(this->ike
),
240 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
241 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
242 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
243 TRUE
, FALSE
, /* mobike, aggressive */
244 this->dpd
, 0, /* DPD delay, timeout */
245 NULL
, NULL
, /* vip, pool */
246 FALSE
, NULL
, NULL
); /* mediation, med by, peer id */
248 auth
= auth_cfg_create();
249 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
250 auth
->add(auth
, AUTH_RULE_IDENTITY
,
251 identification_create_from_encoding(ID_KEY_ID
, me
));
252 this->current
->add_auth_cfg(this->current
, auth
, TRUE
);
253 auth
= auth_cfg_create();
254 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
255 auth
->add(auth
, AUTH_RULE_IDENTITY
,
256 identification_create_from_encoding(ID_KEY_ID
, other
));
257 this->current
->add_auth_cfg(this->current
, auth
, FALSE
);
259 child_cfg
= child_cfg_create(name
, &lifetime
, NULL
, TRUE
, MODE_TUNNEL
,
260 ACTION_NONE
, ACTION_NONE
, ACTION_NONE
, FALSE
,
261 0, 0, NULL
, NULL
, 0);
262 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
263 child_cfg
->add_traffic_selector(child_cfg
, TRUE
, ts_from_string(local_net
));
264 child_cfg
->add_traffic_selector(child_cfg
, FALSE
, ts_from_string(remote_net
));
265 this->current
->add_child_cfg(this->current
, child_cfg
);
266 *cfg
= this->current
;
270 METHOD(enumerator_t
, peer_enumerator_destroy
, void,
271 peer_enumerator_t
*this)
273 DESTROY_IF(this->current
);
274 this->inner
->destroy(this->inner
);
278 METHOD(backend_t
, create_peer_cfg_enumerator
, enumerator_t
*,
279 private_medcli_config_t
*this, identification_t
*me
,
280 identification_t
*other
)
282 peer_enumerator_t
*e
;
286 .enumerate
= (void*)_peer_enumerator_enumerate
,
287 .destroy
= _peer_enumerator_destroy
,
290 .rekey
= this->rekey
,
294 /* filter on IDs: NULL or ANY or matching KEY_ID */
295 e
->inner
= this->db
->query(this->db
,
296 "SELECT Alias, ClientConfig.KeyId, Connection.KeyId, "
297 "Connection.LocalSubnet, Connection.RemoteSubnet "
298 "FROM ClientConfig JOIN Connection "
300 "(? OR ClientConfig.KeyId = ?) AND (? OR Connection.KeyId = ?)",
301 DB_INT
, me
== NULL
|| me
->get_type(me
) == ID_ANY
,
302 DB_BLOB
, me
&& me
->get_type(me
) == ID_KEY_ID ?
303 me
->get_encoding(me
) : chunk_empty
,
304 DB_INT
, other
== NULL
|| other
->get_type(other
) == ID_ANY
,
305 DB_BLOB
, other
&& other
->get_type(other
) == ID_KEY_ID ?
306 other
->get_encoding(other
) : chunk_empty
,
307 DB_TEXT
, DB_BLOB
, DB_BLOB
, DB_TEXT
, DB_TEXT
);
317 * initiate a peer config
319 static job_requeue_t
initiate_config(peer_cfg_t
*peer_cfg
)
321 enumerator_t
*enumerator
;
322 child_cfg_t
*child_cfg
= NULL
;;
324 enumerator
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
325 enumerator
->enumerate(enumerator
, &child_cfg
);
328 child_cfg
->get_ref(child_cfg
);
329 peer_cfg
->get_ref(peer_cfg
);
330 enumerator
->destroy(enumerator
);
331 charon
->controller
->initiate(charon
->controller
,
332 peer_cfg
, child_cfg
, NULL
, NULL
, 0);
336 enumerator
->destroy(enumerator
);
338 return JOB_REQUEUE_NONE
;
342 * schedule initiation of all "active" connections
344 static void schedule_autoinit(private_medcli_config_t
*this)
349 e
= this->db
->query(this->db
, "SELECT Alias FROM Connection WHERE Active",
353 while (e
->enumerate(e
, &name
))
355 peer_cfg_t
*peer_cfg
;
357 peer_cfg
= get_peer_cfg_by_name(this, name
);
360 /* schedule asynchronous initiation job */
361 lib
->processor
->queue_job(lib
->processor
,
362 (job_t
*)callback_job_create(
363 (callback_job_cb_t
)initiate_config
,
364 peer_cfg
, (void*)peer_cfg
->destroy
, NULL
));
371 METHOD(medcli_config_t
, destroy
, void,
372 private_medcli_config_t
*this)
374 this->ike
->destroy(this->ike
);
379 * Described in header.
381 medcli_config_t
*medcli_config_create(database_t
*db
)
383 private_medcli_config_t
*this;
388 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
389 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
390 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
395 .rekey
= lib
->settings
->get_time(lib
->settings
, "medcli.rekey", 1200),
396 .dpd
= lib
->settings
->get_time(lib
->settings
, "medcli.dpd", 300),
397 .ike
= ike_cfg_create(FALSE
, FALSE
, "0.0.0.0", IKEV2_UDP_PORT
,
398 "0.0.0.0", IKEV2_UDP_PORT
),
400 this->ike
->add_proposal(this->ike
, proposal_create_default(PROTO_IKE
));
402 schedule_autoinit(this);
404 return &this->public;