2008-06-17 14:17:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Thomas Kallenberg
|
|
|
|
* Copyright (C) 2008 Tobias Brunner
|
|
|
|
* Copyright (C) 2008 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
#include "uci_config.h"
|
|
|
|
#include "uci_parser.h"
|
|
|
|
|
|
|
|
#include <daemon.h>
|
|
|
|
|
|
|
|
typedef struct private_uci_config_t private_uci_config_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private data of an uci_config_t object
|
|
|
|
*/
|
|
|
|
struct private_uci_config_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public part
|
|
|
|
*/
|
|
|
|
uci_config_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
/**
|
|
|
|
* UCI parser context
|
|
|
|
*/
|
|
|
|
uci_parser_t *parser;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enumerator implementation for create_peer_cfg_enumerator
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** implements enumerator */
|
|
|
|
enumerator_t public;
|
|
|
|
/** currently enumerated peer config */
|
|
|
|
peer_cfg_t *peer_cfg;
|
|
|
|
/** inner uci_parser section enumerator */
|
|
|
|
enumerator_t *inner;
|
|
|
|
} peer_enumerator_t;
|
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
/**
|
|
|
|
* create a proposal from a string, with fallback to default
|
|
|
|
*/
|
|
|
|
static proposal_t *create_proposal(char *string, protocol_id_t proto)
|
|
|
|
{
|
|
|
|
proposal_t *proposal = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
proposal = proposal_create_from_string(proto, string);
|
|
|
|
}
|
|
|
|
if (!proposal)
|
2008-06-18 13:32:23 +00:00
|
|
|
{ /* UCI default is aes/sha1 only */
|
|
|
|
if (proto == PROTO_IKE)
|
|
|
|
{
|
2009-09-04 11:46:09 +00:00
|
|
|
proposal = proposal_create_from_string(proto,
|
2008-06-18 13:32:23 +00:00
|
|
|
"aes128-aes192-aes256-sha1-modp1536-modp2048");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-04 11:46:09 +00:00
|
|
|
proposal = proposal_create_from_string(proto,
|
2008-06-18 13:32:23 +00:00
|
|
|
"aes128-aes192-aes256-sha1");
|
|
|
|
}
|
2008-06-18 08:50:32 +00:00
|
|
|
}
|
|
|
|
return proposal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create an traffic selector, fallback to dynamic
|
|
|
|
*/
|
|
|
|
static traffic_selector_t *create_ts(char *string)
|
|
|
|
{
|
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
int netbits = 32;
|
|
|
|
host_t *net;
|
|
|
|
char *pos;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
string = strdupa(string);
|
|
|
|
pos = strchr(string, '/');
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
*pos++ = '\0';
|
|
|
|
netbits = atoi(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (strchr(string, ':'))
|
|
|
|
{
|
|
|
|
netbits = 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
net = host_create_from_string(string, 0);
|
|
|
|
if (net)
|
|
|
|
{
|
|
|
|
return traffic_selector_create_from_subnet(net, netbits, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 13:09:52 +00:00
|
|
|
return traffic_selector_create_dynamic(0, 0, 65535);
|
2008-06-18 08:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a rekey time from a string with hours, with fallback
|
|
|
|
*/
|
|
|
|
static u_int create_rekey(char *string)
|
|
|
|
{
|
|
|
|
u_int rekey = 0;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
rekey = atoi(string);
|
|
|
|
if (rekey)
|
|
|
|
{
|
|
|
|
return rekey * 3600;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* every 12 hours */
|
|
|
|
return 12 * 3600;
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(enumerator_t, peer_enumerator_enumerate, bool,
|
|
|
|
peer_enumerator_t *this, peer_cfg_t **cfg)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
2008-06-18 08:50:32 +00:00
|
|
|
char *name, *ike_proposal, *esp_proposal, *ike_rekey, *esp_rekey;
|
|
|
|
char *local_id, *local_addr, *local_net;
|
|
|
|
char *remote_id, *remote_addr, *remote_net;
|
2008-06-17 14:17:51 +00:00
|
|
|
child_cfg_t *child_cfg;
|
|
|
|
ike_cfg_t *ike_cfg;
|
2009-04-14 10:34:24 +00:00
|
|
|
auth_cfg_t *auth;
|
2009-08-28 15:04:35 +00:00
|
|
|
lifetime_cfg_t lifetime = {
|
|
|
|
.time = {
|
|
|
|
.life = create_rekey(esp_rekey) + 300,
|
2009-09-02 14:12:52 +00:00
|
|
|
.rekey = create_rekey(esp_rekey),
|
2009-08-28 15:04:35 +00:00
|
|
|
.jitter = 300
|
|
|
|
}
|
|
|
|
};
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
/* defaults */
|
|
|
|
name = "unnamed";
|
2008-06-18 08:50:32 +00:00
|
|
|
local_id = NULL;
|
|
|
|
remote_id = NULL;
|
|
|
|
local_addr = "0.0.0.0";
|
|
|
|
remote_addr = "0.0.0.0";
|
|
|
|
local_net = NULL;
|
|
|
|
remote_net = NULL;
|
|
|
|
ike_proposal = NULL;
|
|
|
|
esp_proposal = NULL;
|
|
|
|
ike_rekey = NULL;
|
|
|
|
esp_rekey = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
if (this->inner->enumerate(this->inner, &name, &local_id, &remote_id,
|
|
|
|
&local_addr, &remote_addr, &local_net, &remote_net,
|
|
|
|
&ike_proposal, &esp_proposal, &ike_rekey, &esp_rekey))
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
DESTROY_IF(this->peer_cfg);
|
2010-02-22 17:11:42 +00:00
|
|
|
ike_cfg = ike_cfg_create(FALSE, FALSE,
|
2012-04-20 08:59:14 +00:00
|
|
|
local_addr, FALSE, charon->socket->get_port(charon->socket, FALSE),
|
2012-06-08 11:01:08 +00:00
|
|
|
remote_addr, FALSE, IKEV2_UDP_PORT);
|
2008-06-18 08:50:32 +00:00
|
|
|
ike_cfg->add_proposal(ike_cfg, create_proposal(ike_proposal, PROTO_IKE));
|
2008-06-17 14:17:51 +00:00
|
|
|
this->peer_cfg = peer_cfg_create(
|
2011-11-16 16:28:06 +00:00
|
|
|
name, IKEV2, ike_cfg, CERT_SEND_IF_ASKED, UNIQUE_NO,
|
2008-06-18 08:50:32 +00:00
|
|
|
1, create_rekey(ike_rekey), 0, /* keytries, rekey, reauth */
|
|
|
|
1800, 900, /* jitter, overtime */
|
2012-05-17 17:49:22 +00:00
|
|
|
TRUE, FALSE, /* mobike, aggressive */
|
|
|
|
60, 0, /* DPD delay, timeout */
|
2011-10-04 12:27:06 +00:00
|
|
|
FALSE, NULL, NULL); /* mediation, med by, peer id */
|
2009-04-14 10:34:24 +00:00
|
|
|
auth = auth_cfg_create();
|
|
|
|
auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
|
|
|
|
auth->add(auth, AUTH_RULE_IDENTITY,
|
|
|
|
identification_create_from_string(local_id));
|
|
|
|
this->peer_cfg->add_auth_cfg(this->peer_cfg, auth, TRUE);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-14 10:34:24 +00:00
|
|
|
auth = auth_cfg_create();
|
|
|
|
auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
|
|
|
|
if (remote_id)
|
|
|
|
{
|
|
|
|
auth->add(auth, AUTH_RULE_IDENTITY,
|
|
|
|
identification_create_from_string(remote_id));
|
|
|
|
}
|
|
|
|
this->peer_cfg->add_auth_cfg(this->peer_cfg, auth, FALSE);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-08-28 15:04:35 +00:00
|
|
|
child_cfg = child_cfg_create(name, &lifetime, NULL, TRUE, MODE_TUNNEL,
|
2010-11-28 10:57:49 +00:00
|
|
|
ACTION_NONE, ACTION_NONE, ACTION_NONE,
|
2010-12-08 12:41:04 +00:00
|
|
|
FALSE, 0, 0, NULL, NULL, 0);
|
2008-06-18 13:32:23 +00:00
|
|
|
child_cfg->add_proposal(child_cfg, create_proposal(esp_proposal, PROTO_ESP));
|
2008-06-18 08:50:32 +00:00
|
|
|
child_cfg->add_traffic_selector(child_cfg, TRUE, create_ts(local_net));
|
|
|
|
child_cfg->add_traffic_selector(child_cfg, FALSE, create_ts(remote_net));
|
2008-06-17 14:17:51 +00:00
|
|
|
this->peer_cfg->add_child_cfg(this->peer_cfg, child_cfg);
|
|
|
|
*cfg = this->peer_cfg;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
|
|
|
|
METHOD(enumerator_t, peer_enumerator_destroy, void,
|
|
|
|
peer_enumerator_t *this)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
DESTROY_IF(this->peer_cfg);
|
|
|
|
this->inner->destroy(this->inner);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
|
|
|
|
private_uci_config_t *this, identification_t *me, identification_t *other)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
2011-10-04 12:27:06 +00:00
|
|
|
peer_enumerator_t *e;
|
|
|
|
|
|
|
|
INIT(e,
|
|
|
|
.public = {
|
|
|
|
.enumerate = (void*)_peer_enumerator_enumerate,
|
|
|
|
.destroy = _peer_enumerator_destroy,
|
|
|
|
},
|
|
|
|
.inner = this->parser->create_section_enumerator(this->parser,
|
2008-06-18 08:50:32 +00:00
|
|
|
"local_id", "remote_id", "local_addr", "remote_addr",
|
|
|
|
"local_net", "remote_net", "ike_proposal", "esp_proposal",
|
2011-10-04 12:27:06 +00:00
|
|
|
"ike_rekey", "esp_rekey", NULL),
|
|
|
|
);
|
2008-06-17 14:17:51 +00:00
|
|
|
if (!e->inner)
|
|
|
|
{
|
|
|
|
free(e);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &e->public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enumerator implementation for create_ike_cfg_enumerator
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** implements enumerator */
|
|
|
|
enumerator_t public;
|
|
|
|
/** currently enumerated ike config */
|
|
|
|
ike_cfg_t *ike_cfg;
|
|
|
|
/** inner uci_parser section enumerator */
|
|
|
|
enumerator_t *inner;
|
|
|
|
} ike_enumerator_t;
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(enumerator_t, ike_enumerator_enumerate, bool,
|
|
|
|
ike_enumerator_t *this, ike_cfg_t **cfg)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
2008-06-18 08:50:32 +00:00
|
|
|
char *local_addr, *remote_addr, *ike_proposal;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
/* defaults */
|
2008-06-18 08:50:32 +00:00
|
|
|
local_addr = "0.0.0.0";
|
|
|
|
remote_addr = "0.0.0.0";
|
|
|
|
ike_proposal = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-18 08:50:32 +00:00
|
|
|
if (this->inner->enumerate(this->inner, NULL,
|
|
|
|
&local_addr, &remote_addr, &ike_proposal))
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
DESTROY_IF(this->ike_cfg);
|
2012-06-08 11:01:08 +00:00
|
|
|
this->ike_cfg = ike_cfg_create(FALSE, FALSE,
|
2012-04-20 08:59:14 +00:00
|
|
|
local_addr, FALSE, charon->socket->get_port(charon->socket, FALSE),
|
2012-06-08 11:01:08 +00:00
|
|
|
remote_addr, FALSE, IKEV2_UDP_PORT);
|
2008-06-17 14:17:51 +00:00
|
|
|
this->ike_cfg->add_proposal(this->ike_cfg,
|
2008-06-18 08:50:32 +00:00
|
|
|
create_proposal(ike_proposal, PROTO_IKE));
|
2008-06-17 14:17:51 +00:00
|
|
|
|
|
|
|
*cfg = this->ike_cfg;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(enumerator_t, ike_enumerator_destroy, void,
|
|
|
|
ike_enumerator_t *this)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
DESTROY_IF(this->ike_cfg);
|
|
|
|
this->inner->destroy(this->inner);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
|
|
|
|
private_uci_config_t *this, host_t *me, host_t *other)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
2011-10-04 12:27:06 +00:00
|
|
|
ike_enumerator_t *e;
|
|
|
|
|
|
|
|
INIT(e,
|
|
|
|
.public = {
|
|
|
|
.enumerate = (void*)_ike_enumerator_enumerate,
|
|
|
|
.destroy = _ike_enumerator_destroy,
|
|
|
|
},
|
|
|
|
.inner = this->parser->create_section_enumerator(this->parser,
|
|
|
|
"local_addr", "remote_addr", "ike_proposal", NULL),
|
|
|
|
);
|
2008-06-17 14:17:51 +00:00
|
|
|
if (!e->inner)
|
|
|
|
{
|
|
|
|
free(e);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &e->public;
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
|
|
|
|
private_uci_config_t *this, char *name)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
peer_cfg_t *current, *found = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
enumerator = create_peer_cfg_enumerator(this, NULL, NULL);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
|
|
|
if (streq(name, current->get_name(current)))
|
|
|
|
{
|
|
|
|
found = current->get_ref(current);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2011-10-04 12:27:06 +00:00
|
|
|
METHOD(uci_config_t, destroy, void,
|
|
|
|
private_uci_config_t *this)
|
2008-06-17 14:17:51 +00:00
|
|
|
{
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
uci_config_t *uci_config_create(uci_parser_t *parser)
|
|
|
|
{
|
2011-10-04 12:27:06 +00:00
|
|
|
private_uci_config_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.backend = {
|
|
|
|
.create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
|
|
|
|
.create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
|
|
|
|
.get_peer_cfg_by_name = _get_peer_cfg_by_name,
|
|
|
|
},
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.parser = parser,
|
|
|
|
);
|
2008-07-18 10:34:44 +00:00
|
|
|
|
2008-06-17 14:17:51 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|