Moving charon to libcharon.
[strongswan.git] / src / libcharon / plugins / medsrv / medsrv_config.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include <string.h>
17
18 #include "medsrv_config.h"
19
20 #include <daemon.h>
21
22 typedef struct private_medsrv_config_t private_medsrv_config_t;
23
24 /**
25 * Private data of an medsrv_config_t object
26 */
27 struct private_medsrv_config_t {
28
29 /**
30 * Public part
31 */
32 medsrv_config_t public;
33
34 /**
35 * database connection
36 */
37 database_t *db;
38
39 /**
40 * rekey time
41 */
42 int rekey;
43
44 /**
45 * dpd delay
46 */
47 int dpd;
48
49 /**
50 * default ike config
51 */
52 ike_cfg_t *ike;
53 };
54
55 /**
56 * implements backend_t.get_peer_cfg_by_name.
57 */
58 static peer_cfg_t *get_peer_cfg_by_name(private_medsrv_config_t *this, char *name)
59 {
60 return NULL;
61 }
62
63 /**
64 * Implementation of backend_t.create_ike_cfg_enumerator.
65 */
66 static enumerator_t* create_ike_cfg_enumerator(private_medsrv_config_t *this,
67 host_t *me, host_t *other)
68 {
69 return enumerator_create_single(this->ike, NULL);
70 }
71
72 /**
73 * Implementation of backend_t.create_peer_cfg_enumerator.
74 */
75 static enumerator_t* create_peer_cfg_enumerator(private_medsrv_config_t *this,
76 identification_t *me,
77 identification_t *other)
78 {
79 enumerator_t *e;
80
81 if (!me || !other || other->get_type(other) != ID_KEY_ID)
82 {
83 return NULL;
84 }
85 e = this->db->query(this->db,
86 "SELECT CONCAT(peer.alias, CONCAT('@', user.login)) FROM "
87 "peer JOIN user ON peer.user = user.id "
88 "WHERE peer.keyid = ?", DB_BLOB, other->get_encoding(other),
89 DB_TEXT);
90 if (e)
91 {
92 peer_cfg_t *peer_cfg;
93 auth_cfg_t *auth;
94 char *name;
95
96 if (e->enumerate(e, &name))
97 {
98 peer_cfg = peer_cfg_create(
99 name, 2, this->ike->get_ref(this->ike),
100 CERT_NEVER_SEND, UNIQUE_REPLACE,
101 1, this->rekey*60, 0, /* keytries, rekey, reauth */
102 this->rekey*5, this->rekey*3, /* jitter, overtime */
103 TRUE, this->dpd, /* mobike, dpddelay */
104 NULL, NULL, /* vip, pool */
105 TRUE, NULL, NULL); /* mediation, med by, peer id */
106 e->destroy(e);
107
108 auth = auth_cfg_create();
109 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
110 auth->add(auth, AUTH_RULE_IDENTITY, me->clone(me));
111 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
112 auth = auth_cfg_create();
113 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
114 auth->add(auth, AUTH_RULE_IDENTITY, other->clone(other));
115 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
116
117 return enumerator_create_single(peer_cfg, (void*)peer_cfg->destroy);
118 }
119 e->destroy(e);
120 }
121 return NULL;
122 }
123
124 /**
125 * Implementation of medsrv_config_t.destroy.
126 */
127 static void destroy(private_medsrv_config_t *this)
128 {
129 this->ike->destroy(this->ike);
130 free(this);
131 }
132
133 /**
134 * Described in header.
135 */
136 medsrv_config_t *medsrv_config_create(database_t *db)
137 {
138 private_medsrv_config_t *this = malloc_thing(private_medsrv_config_t);
139
140 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
141 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
142 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
143 this->public.destroy = (void(*)(medsrv_config_t*))destroy;
144
145 this->db = db;
146 this->rekey = lib->settings->get_time(lib->settings, "medsrv.rekey", 1200);
147 this->dpd = lib->settings->get_time(lib->settings, "medsrv.dpd", 300);
148 this->ike = ike_cfg_create(FALSE, FALSE,
149 "0.0.0.0", IKEV2_UDP_PORT, "0.0.0.0", IKEV2_UDP_PORT);
150 this->ike->add_proposal(this->ike, proposal_create_default(PROTO_IKE));
151
152 return &this->public;
153 }
154