486944d524eaadbd2d8e3e833779608882d63c95
[platform/upstream/connman.git] / vpn / plugins / openvpn.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <net/if.h>
32
33 #include <glib.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/dbus.h>
40 #include <connman/ipconfig.h>
41
42 #include "../vpn-provider.h"
43
44 #include "vpn.h"
45
46 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
47
48 static DBusConnection *connection;
49
50 struct {
51         const char *cm_opt;
52         const char *ov_opt;
53         char       has_value;
54 } ov_options[] = {
55         { "Host", "--remote", 1 },
56         { "OpenVPN.CACert", "--ca", 1 },
57         { "OpenVPN.Cert", "--cert", 1 },
58         { "OpenVPN.Key", "--key", 1 },
59         { "OpenVPN.MTU", "--mtu", 1 },
60         { "OpenVPN.NSCertType", "--ns-cert-type", 1 },
61         { "OpenVPN.Proto", "--proto", 1 },
62         { "OpenVPN.Port", "--port", 1 },
63         { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
64         { "OpenVPN.AskPass", "--askpass", 1 },
65         { "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
66         { "OpenVPN.TLSRemote", "--tls-remote", 1 },
67         { "OpenVPN.TLSAuth", NULL, 1 },
68         { "OpenVPN.TLSAuthDir", NULL, 1 },
69         { "OpenVPN.Cipher", "--cipher", 1 },
70         { "OpenVPN.Auth", "--auth", 1 },
71         { "OpenVPN.CompLZO", "--comp-lzo", 0 },
72         { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
73         { "OpenVPN.ConfigFile", "--config", 1 },
74 };
75
76 struct nameserver_entry {
77         int id;
78         char *nameserver;
79 };
80
81 static struct nameserver_entry *ov_append_dns_entries(const char *key,
82                                                 const char *value)
83 {
84         struct nameserver_entry *entry = NULL;
85         gchar **options;
86
87         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
88                 return NULL;
89
90         options = g_strsplit(value, " ", 3);
91         if (options[0] != NULL &&
92                 !strcmp(options[0], "dhcp-option") &&
93                         options[1] != NULL &&
94                         !strcmp(options[1], "DNS") &&
95                                 options[2] != NULL) {
96
97                 entry = g_try_new(struct nameserver_entry, 1);
98                 if (entry == NULL)
99                         return NULL;
100
101                 entry->nameserver = g_strdup(options[2]);
102                 entry->id = atoi(key + 15); /* foreign_option_XXX */
103         }
104
105         g_strfreev(options);
106
107         return entry;
108 }
109
110 static char *ov_get_domain_name(const char *key, const char *value)
111 {
112         gchar **options;
113         char *domain = NULL;
114
115         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
116                 return NULL;
117
118         options = g_strsplit(value, " ", 3);
119         if (options[0] != NULL &&
120                 !strcmp(options[0], "dhcp-option") &&
121                         options[1] != NULL &&
122                         !strcmp(options[1], "DOMAIN") &&
123                                 options[2] != NULL) {
124
125                 domain = g_strdup(options[2]);
126         }
127
128         g_strfreev(options);
129
130         return domain;
131 }
132
133 static gint cmp_ns(gconstpointer a, gconstpointer b)
134 {
135         struct nameserver_entry *entry_a = (struct nameserver_entry *)a;
136         struct nameserver_entry *entry_b = (struct nameserver_entry *)b;
137
138         if (entry_a->id < entry_b->id)
139                 return -1;
140
141         if (entry_a->id > entry_b->id)
142                 return 1;
143
144         return 0;
145 }
146
147 static void free_ns_entry(gpointer data)
148 {
149         struct nameserver_entry *entry = data;
150
151         g_free(entry->nameserver);
152         g_free(entry);
153 }
154
155 static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
156 {
157         DBusMessageIter iter, dict;
158         const char *reason, *key, *value;
159         char *address = NULL, *gateway = NULL, *peer = NULL;
160         struct connman_ipaddress *ipaddress;
161         GSList *nameserver_list = NULL;
162
163         dbus_message_iter_init(msg, &iter);
164
165         dbus_message_iter_get_basic(&iter, &reason);
166         dbus_message_iter_next(&iter);
167
168         if (!provider) {
169                 connman_error("No provider found");
170                 return VPN_STATE_FAILURE;
171         }
172
173         if (strcmp(reason, "up"))
174                 return VPN_STATE_DISCONNECT;
175
176         dbus_message_iter_recurse(&iter, &dict);
177
178         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
179                 struct nameserver_entry *ns_entry = NULL;
180                 DBusMessageIter entry;
181
182                 dbus_message_iter_recurse(&dict, &entry);
183                 dbus_message_iter_get_basic(&entry, &key);
184                 dbus_message_iter_next(&entry);
185                 dbus_message_iter_get_basic(&entry, &value);
186
187                 DBG("%s = %s", key, value);
188
189                 if (!strcmp(key, "trusted_ip")) {
190                         vpn_provider_set_string(provider, "Gateway", value);
191                         gateway = g_strdup(value);
192                 }
193
194                 if (!strcmp(key, "ifconfig_local")) {
195                         vpn_provider_set_string(provider, "Address", value);
196                         address = g_strdup(value);
197                 }
198
199                 if (!strcmp(key, "ifconfig_remote")) {
200                         vpn_provider_set_string(provider, "Peer", value);
201                         peer = g_strdup(value);
202                 }
203
204                 if (g_str_has_prefix(key, "route_") == TRUE)
205                         vpn_provider_append_route(provider, key, value);
206
207                 if ((ns_entry = ov_append_dns_entries(key, value)) != NULL)
208                         nameserver_list = g_slist_prepend(nameserver_list,
209                                                         ns_entry);
210                 else {
211                         char *domain = ov_get_domain_name(key, value);
212                         if (domain != NULL) {
213                                 vpn_provider_set_domain(provider, domain);
214                                 g_free(domain);
215                         }
216                 }
217
218                 dbus_message_iter_next(&dict);
219         }
220
221         ipaddress = connman_ipaddress_alloc(AF_INET);
222         if (ipaddress == NULL) {
223                 g_slist_free_full(nameserver_list, free_ns_entry);
224                 g_free(address);
225                 g_free(gateway);
226                 g_free(peer);
227
228                 return VPN_STATE_FAILURE;
229         }
230
231         connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
232         connman_ipaddress_set_peer(ipaddress, peer);
233         vpn_provider_set_ipaddress(provider, ipaddress);
234
235         if (nameserver_list != NULL) {
236                 char *nameservers = NULL;
237                 GSList *tmp;
238
239                 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
240                 for (tmp = nameserver_list; tmp != NULL;
241                                                 tmp = g_slist_next(tmp)) {
242                         struct nameserver_entry *ns = tmp->data;
243
244                         if (nameservers == NULL) {
245                                 nameservers = g_strdup(ns->nameserver);
246                         } else {
247                                 char *str;
248                                 str = g_strjoin(" ", nameservers,
249                                                 ns->nameserver, NULL);
250                                 g_free(nameservers);
251                                 nameservers = str;
252                         }
253                 }
254
255                 g_slist_free_full(nameserver_list, free_ns_entry);
256
257                 vpn_provider_set_nameservers(provider, nameservers);
258
259                 g_free(nameservers);
260         }
261
262         g_free(address);
263         g_free(gateway);
264         g_free(peer);
265         connman_ipaddress_free(ipaddress);
266
267         return VPN_STATE_CONNECT;
268 }
269
270 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
271 {
272         const char *option;
273         int i;
274
275         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
276                 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
277                         option = vpn_provider_get_string(provider,
278                                                         ov_options[i].cm_opt);
279                         if (option == NULL)
280                                 continue;
281
282                         g_key_file_set_string(keyfile,
283                                         vpn_provider_get_save_group(provider),
284                                         ov_options[i].cm_opt, option);
285                 }
286         }
287         return 0;
288 }
289
290 static int task_append_config_data(struct vpn_provider *provider,
291                                         struct connman_task *task)
292 {
293         const char *option;
294         int i;
295
296         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
297                 if (ov_options[i].ov_opt == NULL)
298                         continue;
299
300                 option = vpn_provider_get_string(provider,
301                                         ov_options[i].cm_opt);
302                 if (option == NULL)
303                         continue;
304
305                 if (connman_task_add_argument(task,
306                                 ov_options[i].ov_opt,
307                                 ov_options[i].has_value ? option : NULL) < 0) {
308                         return -EIO;
309                 }
310         }
311
312         return 0;
313 }
314
315 static int ov_connect(struct vpn_provider *provider,
316                         struct connman_task *task, const char *if_name,
317                         vpn_provider_connect_cb_t cb, void *user_data)
318 {
319         const char *option;
320         int err = 0, fd;
321
322         option = vpn_provider_get_string(provider, "Host");
323         if (option == NULL) {
324                 connman_error("Host not set; cannot enable VPN");
325                 return -EINVAL;
326         }
327
328         task_append_config_data(provider, task);
329
330         option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
331         if (option == NULL) {
332                 /*
333                  * Set some default options if user has no config file.
334                  */
335                 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
336                 if (option != NULL) {
337                         connman_task_add_argument(task, "--tls-auth", option);
338                         option = vpn_provider_get_string(provider,
339                                                         "OpenVPN.TLSAuthDir");
340                         if (option != NULL)
341                                 connman_task_add_argument(task, option, NULL);
342                 }
343
344                 connman_task_add_argument(task, "--nobind", NULL);
345                 connman_task_add_argument(task, "--persist-key", NULL);
346                 connman_task_add_argument(task, "--client", NULL);
347         }
348
349         connman_task_add_argument(task, "--syslog", NULL);
350
351         connman_task_add_argument(task, "--script-security", "2");
352
353         connman_task_add_argument(task, "--up",
354                                         SCRIPTDIR "/openvpn-script");
355         connman_task_add_argument(task, "--up-restart", NULL);
356
357         connman_task_add_argument(task, "--setenv", NULL);
358         connman_task_add_argument(task, "CONNMAN_BUSNAME",
359                                         dbus_bus_get_unique_name(connection));
360
361         connman_task_add_argument(task, "--setenv", NULL);
362         connman_task_add_argument(task, "CONNMAN_INTERFACE",
363                                         CONNMAN_TASK_INTERFACE);
364
365         connman_task_add_argument(task, "--setenv", NULL);
366         connman_task_add_argument(task, "CONNMAN_PATH",
367                                         connman_task_get_path(task));
368
369         connman_task_add_argument(task, "--dev", if_name);
370         connman_task_add_argument(task, "--dev-type", "tun");
371
372         connman_task_add_argument(task, "--persist-tun", NULL);
373
374         connman_task_add_argument(task, "--route-noexec", NULL);
375         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
376
377         /*
378          * Disable client restarts because we can't handle this at the
379          * moment. The problem is that when OpenVPN decides to switch
380          * from CONNECTED state to RECONNECTING and then to RESOLVE,
381          * it is not possible to do a DNS lookup. The DNS server is
382          * not accessable through the tunnel anymore and so we end up
383          * trying to resolve the OpenVPN servers address.
384          */
385         connman_task_add_argument(task, "--ping-restart", "0");
386
387         fd = fileno(stderr);
388         err = connman_task_run(task, vpn_died, provider,
389                         NULL, &fd, &fd);
390         if (err < 0) {
391                 connman_error("openvpn failed to start");
392                 err = -EIO;
393                 goto done;
394         }
395
396 done:
397         if (cb != NULL)
398                 cb(provider, user_data, err);
399
400         return err;
401 }
402
403 static struct vpn_driver vpn_driver = {
404         .notify = ov_notify,
405         .connect        = ov_connect,
406         .save           = ov_save,
407 };
408
409 static int openvpn_init(void)
410 {
411         connection = connman_dbus_get_connection();
412
413         return vpn_register("openvpn", &vpn_driver, OPENVPN);
414 }
415
416 static void openvpn_exit(void)
417 {
418         vpn_unregister("openvpn");
419
420         dbus_connection_unref(connection);
421 }
422
423 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
424         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)