summaryrefslogtreecommitdiff
path: root/vpn/plugins/ipsec.c
blob: 7cf0a5ce4a0b3daa1d2532a968a9d6c7049ec0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 *
 *  ConnMan VPN daemon
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <net/if.h>

#include <glib.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/log.h>
#include <connman/task.h>
#include <connman/dbus.h>
#include <connman/ipconfig.h>

#include "../vpn-provider.h"

#include "vpn.h"
#include "ipsec.h"
#include "vici-client.h"

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

static DBusConnection *connection;

struct {
	const char *cm_opt;
	const char *vici_key;
	const char *section;
} ipsec_conn_options[] = {
	{"IPsec.Version", "version", NULL},
	{"IPsec.LocalAddrs", "local_addrs", NULL},
	{"IPsec.RemoteAddrs", "remote_addrs", NULL},
	{"IPsec.LocalAuth", "auth", "local"},
	{"IPsec.RemoteAuth", "auth", "remote"},
};

/*
 * IPsec.LocalID
 * IPsec.RemoteTS
 */
struct {
	const char *cm_opt;
	const char *vici_type;
} ipsec_shared_options[] = {
	{"IPsec.LocalXauthID", NULL},
	{"IPsec.XauthSecret", "XAUTH"},
	{"IPsec.IKESecret", "IKE"},
};

struct {
	const char *cm_opt;
	const char *vici_type;
	const char *vici_flag;
} ipsec_cert_options[] = {
	{"IPsec.LocalCert", "X509", NULL},
	{"IPsec.RemoteCert", "X509", NULL},
	{"IPsec.CACert", "X509", "CA"},
};


static int ipsec_notify(DBusMessage *msg, struct vpn_provider *provider)
{
	return 0;
}

static void vici_destroy_section(struct section* sect)
{
	g_hash_table_destroy(sect->elm);
	g_hash_table_destroy(sect->subsection);
	g_free(sect);
}

static void free_section(gpointer data)
{
	struct section* sect = (struct section*)data;
	vici_destroy_section(sect);
}

static struct section* vici_create_section(const char* name)
{
	struct section* sect;

	sect = g_try_new0(struct section, 1);
	if (!sect) {
		connman_error("Failed to create section");
		return NULL;
	}

	sect->name = g_strdup(name);
	sect->elm = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
	sect->subsection = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_section);
	return sect;
}

static int vici_section_add_kv(struct section* sect, const char* key, const char* value)
{
	if (sect == NULL || key == NULL || value == NULL) {
		connman_error("invalid parameter");
		return -1;
	}

	g_hash_table_insert(sect->elm, g_strdup(key), g_strdup(value));
	return 0;
}

static int vici_section_add_subsection(struct section* sect, const char* name, struct section* child)
{
	if (sect == NULL || name == NULL || child == NULL) {
		connman_error("invalid parameter");
		return -1;
	}

	g_hash_table_insert(sect->subsection, g_strdup(name), child);
	return 0;
}


static struct section* vici_section_get_subsection(struct section* sect, const char* name)
{
	struct section* sub = g_hash_table_lookup(sect->subsection, name);
	if (sub == NULL) {
		sub = vici_create_section(name);
		vici_section_add_subsection(sect, name, sub);
	}
	return sub;
}

static int vici_section_add_element(struct section* sect, const char* key,
		const char* value, const char* subsection)
{
	struct section* target = sect;

	if (sect == NULL || key == NULL) {
		connman_error("invalid parameter");
		return -1;
	}

	if (subsection)
		target = vici_section_get_subsection(sect, subsection);

	vici_section_add_kv(target, key, value);
	return 0;
}

static int ipsec_is_same_auth(const char* req, const char* target)
{
	if (req == NULL || target == NULL)
		return 0;
	return (g_strcmp0(req, target) == 0);
}

static int vici_load_cert(const char* type, const char* flag, const char* data)
{
	struct section *sect;
	sect = vici_create_section("");
	vici_section_add_element(sect, "type", type, NULL);
	vici_section_add_element(sect, "flag", flag, NULL);
	vici_section_add_element(sect, "data", data, NULL);

	vici_client_send_request(VICI_REQUEST_LOAD_CERT, sect);

	vici_destroy_section(sect);

	return 0;
}

static int ipsec_load_conn(struct vpn_provider *provider)
{
	const char *key;
	const char *value;
	const char *subsection;
	struct section *sect;
	int i;

	value = vpn_provider_get_string(provider, "Name");
	sect = vici_create_section(value);

	for (i = 0; i < (int)ARRAY_SIZE(ipsec_conn_options); i++) {
		key = ipsec_conn_options[i].vici_key;
		value = vpn_provider_get_string(provider, ipsec_conn_options[i].cm_opt);
		subsection = ipsec_conn_options[i].section;
		vici_section_add_element(sect, key, value, subsection);
	}

	vici_client_send_request(VICI_REQUEST_LOAD_CONN, sect);

	vici_destroy_section(sect);

	return 0;
}

static int ipsec_load_shared(struct vpn_provider *provider)
{
	const char *type;
	const char *data;
	const char *owner;
	const char *auth_type;
	struct section *sect;

	sect = vici_create_section("");

	auth_type = vpn_provider_get_string(provider, "IPsec.LocalAuth");
	if (ipsec_is_same_auth(auth_type, IPSEC_AUTH_PSK)) {
		type = VICI_SHARED_TYPE_PSK;
		data = vpn_provider_get_string(provider, "IPsec.IKESecret");
	} else if (ipsec_is_same_auth(auth_type, IPSEC_AUTH_XAUTH)) {
		type = VICI_SHARED_TYPE_XAUTH;
		data = vpn_provider_get_string(provider, "IPsec.XauthSecret");
	} else {
		connman_error("invalid auth type: %s", auth_type);
		return -1;
	}

	owner = vpn_provider_get_string(provider, "IPsec.LocalXauthID");

	vici_section_add_element(sect, "type", type, NULL);
	vici_section_add_element(sect, "data", data, NULL);
	vici_section_add_element(sect, "owner", owner, NULL);

	vici_client_send_request(VICI_REQUEST_LOAD_SHARED, sect);

	vici_destroy_section(sect);

	return 0;
}

static int ipsec_load_cert(struct vpn_provider *provider)
{
	const char *type;
	const char *flag;
	const char *data;
	const char *auth_type;
	int i;

	auth_type = vpn_provider_get_string(provider, "IPsec.LocalAuth");
	if (!ipsec_is_same_auth(auth_type, IPSEC_AUTH_RSA)) {
		connman_error("invalid auth type: %s", auth_type);
		return -1;
	}

	for (i = 0; i < (int)ARRAY_SIZE(ipsec_cert_options); i++) {
		type = ipsec_cert_options[i].vici_type;;
		flag = ipsec_cert_options[i].vici_flag;
		data = vpn_provider_get_string(provider, ipsec_cert_options[i].cm_opt);
		vici_load_cert(type, flag, data);
	}

	return 0;
}

static int ipsec_connect(struct vpn_provider *provider,
			struct connman_task *task, const char *if_name,
			vpn_provider_connect_cb_t cb, const char *dbus_sender,
			void *user_data)
{
	int err = 0;

	/*
	 * Start charon daemon using ipsec script of strongSwan.
	 */
	connman_task_add_argument(task, "start", NULL);
	err = connman_task_run(task, vpn_died, provider, NULL, NULL, NULL);
	IPSEC_ERROR_CHECK_GOTO(err, done, "ipsec start failed");

	/*
	 * Initialize vici client
	 */
	err = vici_client_initialize();
	IPSEC_ERROR_CHECK_GOTO(err, done, "failed to initialize vici_client");

	/*
	 * Send the load-conn command
	 */
	err = ipsec_load_conn(provider);
	IPSEC_ERROR_CHECK_GOTO(err, done, "load-conn failed");

	/*
	 * Send the load-shared command for PSK or XAUTH
	 */
	err = ipsec_load_shared(provider);
	IPSEC_ERROR_CHECK_GOTO(err, done, "load-shared failed");

	/*
	 * Send the load-cert command
	 */
	err = ipsec_load_cert(provider);
	IPSEC_ERROR_CHECK_GOTO(err, done, "load-cert failed");

done:
	if (cb)
		cb(provider, user_data, err);

	return err;
}

static int ipsec_error_code(struct vpn_provider *provider, int exit_code)
{
	return 0;
}

static int ipsec_save(struct vpn_provider *provider, GKeyFile *keyfile)
{
	return 0;
}

static void ipsec_disconnect(struct vpn_provider *provider)
{
	int err = 0;

	err = vici_client_deinitialize();
	IPSEC_ERROR_CHECK_RETURN(err, "failed to deinitialize vici_client");
}

static struct vpn_driver vpn_driver = {
	.flags = VPN_FLAG_NO_TUN,
	.notify = ipsec_notify,
	.connect = ipsec_connect,
	.error_code = ipsec_error_code,
	.save = ipsec_save,
	.disconnect = ipsec_disconnect,
};

static int ipsec_init(void)
{
	connection = connman_dbus_get_connection();

	return vpn_register("ipsec", &vpn_driver, IPSEC);
}

static void ipsec_exit(void)
{
	vpn_unregister("ipsec");

	dbus_connection_unref(connection);
}

CONNMAN_PLUGIN_DEFINE(ipsec, "IPSec plugin", VERSION,
	CONNMAN_PLUGIN_PRIORITY_DEFAULT, ipsec_init, ipsec_exit)