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", FALSE
, charon
->socket
->get_port(charon
->socket
, FALSE
),
123 address
, FALSE
, IKEV2_UDP_PORT
);
124 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(PROTO_IKE
));
125 med_cfg
= peer_cfg_create(
126 "mediation", IKEV2
, ike_cfg
,
127 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
128 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
129 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
130 TRUE
, FALSE
, /* mobike, aggressive */
131 this->dpd
, 0, /* DPD delay, timeout */
132 NULL
, NULL
, /* vip, pool */
133 TRUE
, NULL
, NULL
); /* mediation, med by, peer id */
136 auth
= auth_cfg_create();
137 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
138 auth
->add(auth
, AUTH_RULE_IDENTITY
,
139 identification_create_from_encoding(ID_KEY_ID
, me
));
140 med_cfg
->add_auth_cfg(med_cfg
, auth
, TRUE
);
141 auth
= auth_cfg_create();
142 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
143 auth
->add(auth
, AUTH_RULE_IDENTITY
,
144 identification_create_from_encoding(ID_KEY_ID
, other
));
145 med_cfg
->add_auth_cfg(med_cfg
, auth
, FALSE
);
147 /* query mediated config:
148 * - use any-any ike_cfg
149 * - build peer_cfg on-the-fly using med_cfg
152 e
= this->db
->query(this->db
,
153 "SELECT ClientConfig.KeyId, Connection.KeyId, "
154 "Connection.LocalSubnet, Connection.RemoteSubnet "
155 "FROM ClientConfig JOIN Connection "
156 "WHERE Active AND Alias = ?", DB_TEXT
, name
,
157 DB_BLOB
, DB_BLOB
, DB_TEXT
, DB_TEXT
);
158 if (!e
|| !e
->enumerate(e
, &me
, &other
, &local_net
, &remote_net
))
163 peer_cfg
= peer_cfg_create(
164 name
, IKEV2
, this->ike
->get_ref(this->ike
),
165 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
166 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
167 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
168 TRUE
, FALSE
, /* mobike, aggressive */
169 this->dpd
, 0, /* DPD delay, timeout */
170 NULL
, NULL
, /* vip, pool */
171 FALSE
, med_cfg
, /* mediation, med by */
172 identification_create_from_encoding(ID_KEY_ID
, other
));
174 auth
= auth_cfg_create();
175 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
176 auth
->add(auth
, AUTH_RULE_IDENTITY
,
177 identification_create_from_encoding(ID_KEY_ID
, me
));
178 peer_cfg
->add_auth_cfg(peer_cfg
, auth
, TRUE
);
179 auth
= auth_cfg_create();
180 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
181 auth
->add(auth
, AUTH_RULE_IDENTITY
,
182 identification_create_from_encoding(ID_KEY_ID
, other
));
183 peer_cfg
->add_auth_cfg(peer_cfg
, auth
, FALSE
);
185 child_cfg
= child_cfg_create(name
, &lifetime
, NULL
, TRUE
, MODE_TUNNEL
,
186 ACTION_NONE
, ACTION_NONE
, ACTION_NONE
, FALSE
,
187 0, 0, NULL
, NULL
, 0);
188 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
189 child_cfg
->add_traffic_selector(child_cfg
, TRUE
, ts_from_string(local_net
));
190 child_cfg
->add_traffic_selector(child_cfg
, FALSE
, ts_from_string(remote_net
));
191 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
196 METHOD(backend_t
, create_ike_cfg_enumerator
, enumerator_t
*,
197 private_medcli_config_t
*this, host_t
*me
, host_t
*other
)
199 return enumerator_create_single(this->ike
, NULL
);
203 /** implements enumerator */
205 /** inner SQL enumerator */
207 /** currently enumerated peer config */
209 /** ike cfg to use in peer cfg */
217 METHOD(enumerator_t
, peer_enumerator_enumerate
, bool,
218 peer_enumerator_t
*this, peer_cfg_t
**cfg
)
220 char *name
, *local_net
, *remote_net
;
222 child_cfg_t
*child_cfg
;
224 lifetime_cfg_t lifetime
= {
226 .life
= this->rekey
* 60 + this->rekey
,
227 .rekey
= this->rekey
,
228 .jitter
= this->rekey
232 DESTROY_IF(this->current
);
233 if (!this->inner
->enumerate(this->inner
, &name
, &me
, &other
,
234 &local_net
, &remote_net
))
236 this->current
= NULL
;
239 this->current
= peer_cfg_create(
240 name
, IKEV2
, this->ike
->get_ref(this->ike
),
241 CERT_NEVER_SEND
, UNIQUE_REPLACE
,
242 1, this->rekey
*60, 0, /* keytries, rekey, reauth */
243 this->rekey
*5, this->rekey
*3, /* jitter, overtime */
244 TRUE
, FALSE
, /* mobike, aggressive */
245 this->dpd
, 0, /* DPD delay, timeout */
246 NULL
, NULL
, /* vip, pool */
247 FALSE
, NULL
, NULL
); /* mediation, med by, peer id */
249 auth
= auth_cfg_create();
250 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
251 auth
->add(auth
, AUTH_RULE_IDENTITY
,
252 identification_create_from_encoding(ID_KEY_ID
, me
));
253 this->current
->add_auth_cfg(this->current
, auth
, TRUE
);
254 auth
= auth_cfg_create();
255 auth
->add(auth
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
256 auth
->add(auth
, AUTH_RULE_IDENTITY
,
257 identification_create_from_encoding(ID_KEY_ID
, other
));
258 this->current
->add_auth_cfg(this->current
, auth
, FALSE
);
260 child_cfg
= child_cfg_create(name
, &lifetime
, NULL
, TRUE
, MODE_TUNNEL
,
261 ACTION_NONE
, ACTION_NONE
, ACTION_NONE
, FALSE
,
262 0, 0, NULL
, NULL
, 0);
263 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
264 child_cfg
->add_traffic_selector(child_cfg
, TRUE
, ts_from_string(local_net
));
265 child_cfg
->add_traffic_selector(child_cfg
, FALSE
, ts_from_string(remote_net
));
266 this->current
->add_child_cfg(this->current
, child_cfg
);
267 *cfg
= this->current
;
271 METHOD(enumerator_t
, peer_enumerator_destroy
, void,
272 peer_enumerator_t
*this)
274 DESTROY_IF(this->current
);
275 this->inner
->destroy(this->inner
);
279 METHOD(backend_t
, create_peer_cfg_enumerator
, enumerator_t
*,
280 private_medcli_config_t
*this, identification_t
*me
,
281 identification_t
*other
)
283 peer_enumerator_t
*e
;
287 .enumerate
= (void*)_peer_enumerator_enumerate
,
288 .destroy
= _peer_enumerator_destroy
,
291 .rekey
= this->rekey
,
295 /* filter on IDs: NULL or ANY or matching KEY_ID */
296 e
->inner
= this->db
->query(this->db
,
297 "SELECT Alias, ClientConfig.KeyId, Connection.KeyId, "
298 "Connection.LocalSubnet, Connection.RemoteSubnet "
299 "FROM ClientConfig JOIN Connection "
301 "(? OR ClientConfig.KeyId = ?) AND (? OR Connection.KeyId = ?)",
302 DB_INT
, me
== NULL
|| me
->get_type(me
) == ID_ANY
,
303 DB_BLOB
, me
&& me
->get_type(me
) == ID_KEY_ID
?
304 me
->get_encoding(me
) : chunk_empty
,
305 DB_INT
, other
== NULL
|| other
->get_type(other
) == ID_ANY
,
306 DB_BLOB
, other
&& other
->get_type(other
) == ID_KEY_ID
?
307 other
->get_encoding(other
) : chunk_empty
,
308 DB_TEXT
, DB_BLOB
, DB_BLOB
, DB_TEXT
, DB_TEXT
);
318 * initiate a peer config
320 static job_requeue_t
initiate_config(peer_cfg_t
*peer_cfg
)
322 enumerator_t
*enumerator
;
323 child_cfg_t
*child_cfg
= NULL
;;
325 enumerator
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
326 enumerator
->enumerate(enumerator
, &child_cfg
);
329 child_cfg
->get_ref(child_cfg
);
330 peer_cfg
->get_ref(peer_cfg
);
331 enumerator
->destroy(enumerator
);
332 charon
->controller
->initiate(charon
->controller
,
333 peer_cfg
, child_cfg
, NULL
, NULL
, 0);
337 enumerator
->destroy(enumerator
);
339 return JOB_REQUEUE_NONE
;
343 * schedule initiation of all "active" connections
345 static void schedule_autoinit(private_medcli_config_t
*this)
350 e
= this->db
->query(this->db
, "SELECT Alias FROM Connection WHERE Active",
354 while (e
->enumerate(e
, &name
))
356 peer_cfg_t
*peer_cfg
;
358 peer_cfg
= get_peer_cfg_by_name(this, name
);
361 /* schedule asynchronous initiation job */
362 lib
->processor
->queue_job(lib
->processor
,
363 (job_t
*)callback_job_create(
364 (callback_job_cb_t
)initiate_config
,
365 peer_cfg
, (void*)peer_cfg
->destroy
, NULL
));
372 METHOD(medcli_config_t
, destroy
, void,
373 private_medcli_config_t
*this)
375 this->ike
->destroy(this->ike
);
380 * Described in header.
382 medcli_config_t
*medcli_config_create(database_t
*db
)
384 private_medcli_config_t
*this;
389 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
390 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
391 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
396 .rekey
= lib
->settings
->get_time(lib
->settings
, "medcli.rekey", 1200),
397 .dpd
= lib
->settings
->get_time(lib
->settings
, "medcli.dpd", 300),
398 .ike
= ike_cfg_create(FALSE
, FALSE
,
399 "0.0.0.0", FALSE
, charon
->socket
->get_port(charon
->socket
, FALSE
),
400 "0.0.0.0", FALSE
, IKEV2_UDP_PORT
),
402 this->ike
->add_proposal(this->ike
, proposal_create_default(PROTO_IKE
));
404 schedule_autoinit(this);
406 return &this->public;