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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*
*
* ConnMan VPN daemon
*
* Copyright (C) 2010-2014 BMW Car IT GmbH.
*
* 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 <linux/if_tun.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"
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
static DBusConnection *connection;
struct {
const char *cm_opt;
const char *ov_opt;
char has_value;
} ov_options[] = {
{ "Host", "--remote", 1 },
{ "OpenVPN.CACert", "--ca", 1 },
{ "OpenVPN.Cert", "--cert", 1 },
{ "OpenVPN.Key", "--key", 1 },
{ "OpenVPN.MTU", "--mtu", 1 },
{ "OpenVPN.NSCertType", "--ns-cert-type", 1 },
{ "OpenVPN.Proto", "--proto", 1 },
{ "OpenVPN.Port", "--port", 1 },
{ "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
{ "OpenVPN.AskPass", "--askpass", 1 },
{ "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
{ "OpenVPN.TLSRemote", "--tls-remote", 1 },
{ "OpenVPN.TLSAuth", NULL, 1 },
{ "OpenVPN.TLSAuthDir", NULL, 1 },
{ "OpenVPN.Cipher", "--cipher", 1 },
{ "OpenVPN.Auth", "--auth", 1 },
{ "OpenVPN.CompLZO", "--comp-lzo", 0 },
{ "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
{ "OpenVPN.ConfigFile", "--config", 1 },
{ "OpenVPN.DeviceType", NULL, 1 },
{ "OpenVPN.Verb", "--verb", 1 },
};
struct nameserver_entry {
int id;
char *nameserver;
};
static struct nameserver_entry *ov_append_dns_entries(const char *key,
const char *value)
{
struct nameserver_entry *entry = NULL;
gchar **options;
if (!g_str_has_prefix(key, "foreign_option_"))
return NULL;
options = g_strsplit(value, " ", 3);
if (options[0] &&
!strcmp(options[0], "dhcp-option") &&
options[1] &&
!strcmp(options[1], "DNS") &&
options[2]) {
entry = g_try_new(struct nameserver_entry, 1);
if (!entry)
return NULL;
entry->nameserver = g_strdup(options[2]);
entry->id = atoi(key + 15); /* foreign_option_XXX */
}
g_strfreev(options);
return entry;
}
static char *ov_get_domain_name(const char *key, const char *value)
{
gchar **options;
char *domain = NULL;
if (!g_str_has_prefix(key, "foreign_option_"))
return NULL;
options = g_strsplit(value, " ", 3);
if (options[0] &&
!strcmp(options[0], "dhcp-option") &&
options[1] &&
!strcmp(options[1], "DOMAIN") &&
options[2]) {
domain = g_strdup(options[2]);
}
g_strfreev(options);
return domain;
}
static gint cmp_ns(gconstpointer a, gconstpointer b)
{
struct nameserver_entry *entry_a = (struct nameserver_entry *)a;
struct nameserver_entry *entry_b = (struct nameserver_entry *)b;
if (entry_a->id < entry_b->id)
return -1;
if (entry_a->id > entry_b->id)
return 1;
return 0;
}
static void free_ns_entry(gpointer data)
{
struct nameserver_entry *entry = data;
g_free(entry->nameserver);
g_free(entry);
}
static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
{
DBusMessageIter iter, dict;
const char *reason, *key, *value;
char *address = NULL, *gateway = NULL, *peer = NULL, *netmask = NULL;
struct connman_ipaddress *ipaddress;
GSList *nameserver_list = NULL;
dbus_message_iter_init(msg, &iter);
dbus_message_iter_get_basic(&iter, &reason);
dbus_message_iter_next(&iter);
if (!provider) {
connman_error("No provider found");
return VPN_STATE_FAILURE;
}
if (strcmp(reason, "up"))
return VPN_STATE_DISCONNECT;
dbus_message_iter_recurse(&iter, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
struct nameserver_entry *ns_entry = NULL;
DBusMessageIter entry;
dbus_message_iter_recurse(&dict, &entry);
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
dbus_message_iter_get_basic(&entry, &value);
DBG("%s = %s", key, value);
if (!strcmp(key, "trusted_ip"))
gateway = g_strdup(value);
if (!strcmp(key, "ifconfig_local"))
address = g_strdup(value);
if (!strcmp(key, "ifconfig_netmask"))
netmask = g_strdup(value);
if (!strcmp(key, "ifconfig_remote"))
peer = g_strdup(value);
if (g_str_has_prefix(key, "route_"))
vpn_provider_append_route(provider, key, value);
if ((ns_entry = ov_append_dns_entries(key, value)))
nameserver_list = g_slist_prepend(nameserver_list,
ns_entry);
else {
char *domain = ov_get_domain_name(key, value);
if (domain) {
vpn_provider_set_domain(provider, domain);
g_free(domain);
}
}
dbus_message_iter_next(&dict);
}
ipaddress = connman_ipaddress_alloc(AF_INET);
if (!ipaddress) {
g_slist_free_full(nameserver_list, free_ns_entry);
g_free(address);
g_free(gateway);
g_free(peer);
g_free(netmask);
return VPN_STATE_FAILURE;
}
connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
connman_ipaddress_set_peer(ipaddress, peer);
vpn_provider_set_ipaddress(provider, ipaddress);
if (nameserver_list) {
char *nameservers = NULL;
GSList *tmp;
nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
for (tmp = nameserver_list; tmp;
tmp = g_slist_next(tmp)) {
struct nameserver_entry *ns = tmp->data;
if (!nameservers) {
nameservers = g_strdup(ns->nameserver);
} else {
char *str;
str = g_strjoin(" ", nameservers,
ns->nameserver, NULL);
g_free(nameservers);
nameservers = str;
}
}
g_slist_free_full(nameserver_list, free_ns_entry);
vpn_provider_set_nameservers(provider, nameservers);
g_free(nameservers);
}
g_free(address);
g_free(gateway);
g_free(peer);
g_free(netmask);
connman_ipaddress_free(ipaddress);
return VPN_STATE_CONNECT;
}
static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
{
const char *option;
int i;
for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
option = vpn_provider_get_string(provider,
ov_options[i].cm_opt);
if (!option)
continue;
g_key_file_set_string(keyfile,
vpn_provider_get_save_group(provider),
ov_options[i].cm_opt, option);
}
}
return 0;
}
static int task_append_config_data(struct vpn_provider *provider,
struct connman_task *task)
{
const char *option;
int i;
for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
if (!ov_options[i].ov_opt)
continue;
option = vpn_provider_get_string(provider,
ov_options[i].cm_opt);
if (!option)
continue;
if (connman_task_add_argument(task,
ov_options[i].ov_opt,
ov_options[i].has_value ? option : NULL) < 0) {
return -EIO;
}
}
return 0;
}
static gboolean can_read_data(GIOChannel *chan,
GIOCondition cond, gpointer data)
{
void (*cbf)(const char *format, ...) = data;
gchar *str;
gsize size;
if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
return FALSE;
g_io_channel_read_line(chan, &str, &size, NULL, NULL);
cbf(str);
g_free(str);
return TRUE;
}
static int setup_log_read(int stdout_fd, int stderr_fd)
{
GIOChannel *chan;
int watch;
chan = g_io_channel_unix_new(stdout_fd);
g_io_channel_set_close_on_unref(chan, TRUE);
watch = g_io_add_watch(chan, G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
can_read_data, connman_debug);
g_io_channel_unref(chan);
if (watch == 0)
return -EIO;
chan = g_io_channel_unix_new(stderr_fd);
g_io_channel_set_close_on_unref(chan, TRUE);
watch = g_io_add_watch(chan, G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
can_read_data, connman_error);
g_io_channel_unref(chan);
return watch == 0? -EIO : 0;
}
static int ov_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)
{
const char *option;
int stdout_fd, stderr_fd;
int err = 0;
option = vpn_provider_get_string(provider, "Host");
if (!option) {
connman_error("Host not set; cannot enable VPN");
return -EINVAL;
}
task_append_config_data(provider, task);
option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
if (!option) {
/*
* Set some default options if user has no config file.
*/
option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
if (option) {
connman_task_add_argument(task, "--tls-auth", option);
option = vpn_provider_get_string(provider,
"OpenVPN.TLSAuthDir");
if (option)
connman_task_add_argument(task, option, NULL);
}
connman_task_add_argument(task, "--nobind", NULL);
connman_task_add_argument(task, "--persist-key", NULL);
connman_task_add_argument(task, "--client", NULL);
}
connman_task_add_argument(task, "--script-security", "2");
connman_task_add_argument(task, "--up",
SCRIPTDIR "/openvpn-script");
connman_task_add_argument(task, "--up-restart", NULL);
connman_task_add_argument(task, "--setenv", NULL);
connman_task_add_argument(task, "CONNMAN_BUSNAME",
dbus_bus_get_unique_name(connection));
connman_task_add_argument(task, "--setenv", NULL);
connman_task_add_argument(task, "CONNMAN_INTERFACE",
CONNMAN_TASK_INTERFACE);
connman_task_add_argument(task, "--setenv", NULL);
connman_task_add_argument(task, "CONNMAN_PATH",
connman_task_get_path(task));
connman_task_add_argument(task, "--dev", if_name);
option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
if (option) {
connman_task_add_argument(task, "--dev-type", option);
} else {
/*
* Default to tun for backwards compatibility.
*/
connman_task_add_argument(task, "--dev-type", "tun");
}
connman_task_add_argument(task, "--persist-tun", NULL);
connman_task_add_argument(task, "--route-noexec", NULL);
connman_task_add_argument(task, "--ifconfig-noexec", NULL);
/*
* Disable client restarts because we can't handle this at the
* moment. The problem is that when OpenVPN decides to switch
* from CONNECTED state to RECONNECTING and then to RESOLVE,
* it is not possible to do a DNS lookup. The DNS server is
* not accessable through the tunnel anymore and so we end up
* trying to resolve the OpenVPN servers address.
*/
connman_task_add_argument(task, "--ping-restart", "0");
err = connman_task_run(task, vpn_died, provider,
NULL, &stdout_fd, &stderr_fd);
if (err < 0) {
connman_error("openvpn failed to start");
err = -EIO;
goto done;
}
err = setup_log_read(stdout_fd, stderr_fd);
done:
if (cb)
cb(provider, user_data, err);
return err;
}
static int ov_device_flags(struct vpn_provider *provider)
{
const char *option;
option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
if (!option) {
return IFF_TUN;
}
if (g_str_equal(option, "tap")) {
return IFF_TAP;
}
if (!g_str_equal(option, "tun")) {
connman_warn("bad OpenVPN.DeviceType value, falling back to tun");
}
return IFF_TUN;
}
static int ov_route_env_parse(struct vpn_provider *provider, const char *key,
int *family, unsigned long *idx, enum vpn_provider_route_type *type)
{
char *end;
const char *start;
if (g_str_has_prefix(key, "route_network_")) {
start = key + strlen("route_network_");
*type = VPN_PROVIDER_ROUTE_TYPE_ADDR;
} else if (g_str_has_prefix(key, "route_netmask_")) {
start = key + strlen("route_netmask_");
*type = VPN_PROVIDER_ROUTE_TYPE_MASK;
} else if (g_str_has_prefix(key, "route_gateway_")) {
start = key + strlen("route_gateway_");
*type = VPN_PROVIDER_ROUTE_TYPE_GW;
} else
return -EINVAL;
*family = AF_INET;
*idx = g_ascii_strtoull(start, &end, 10);
return 0;
}
static struct vpn_driver vpn_driver = {
.notify = ov_notify,
.connect = ov_connect,
.save = ov_save,
.device_flags = ov_device_flags,
.route_env_parse = ov_route_env_parse,
};
static int openvpn_init(void)
{
connection = connman_dbus_get_connection();
return vpn_register("openvpn", &vpn_driver, OPENVPN);
}
static void openvpn_exit(void)
{
vpn_unregister("openvpn");
dbus_connection_unref(connection);
}
CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)
|