Merge "Modified logic to process each VSIE of all vendors." into tizen
[platform/upstream/connman.git] / vpn / plugins / openvpn.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010-2014  BMW Car IT GmbH.
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_"))
88                 return NULL;
89
90         options = g_strsplit(value, " ", 3);
91         if (options[0] &&
92                 !strcmp(options[0], "dhcp-option") &&
93                         options[1] &&
94                         !strcmp(options[1], "DNS") &&
95                                 options[2]) {
96
97                 entry = g_try_new(struct nameserver_entry, 1);
98                 if (!entry)
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_"))
116                 return NULL;
117
118         options = g_strsplit(value, " ", 3);
119         if (options[0] &&
120                 !strcmp(options[0], "dhcp-option") &&
121                         options[1] &&
122                         !strcmp(options[1], "DOMAIN") &&
123                                 options[2]) {
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                         gateway = g_strdup(value);
191
192                 if (!strcmp(key, "ifconfig_local"))
193                         address = g_strdup(value);
194
195                 if (!strcmp(key, "ifconfig_remote"))
196                         peer = g_strdup(value);
197
198                 if (g_str_has_prefix(key, "route_"))
199                         vpn_provider_append_route(provider, key, value);
200
201                 if ((ns_entry = ov_append_dns_entries(key, value)))
202                         nameserver_list = g_slist_prepend(nameserver_list,
203                                                         ns_entry);
204                 else {
205                         char *domain = ov_get_domain_name(key, value);
206                         if (domain) {
207                                 vpn_provider_set_domain(provider, domain);
208                                 g_free(domain);
209                         }
210                 }
211
212                 dbus_message_iter_next(&dict);
213         }
214
215         ipaddress = connman_ipaddress_alloc(AF_INET);
216         if (!ipaddress) {
217                 g_slist_free_full(nameserver_list, free_ns_entry);
218                 g_free(address);
219                 g_free(gateway);
220                 g_free(peer);
221
222                 return VPN_STATE_FAILURE;
223         }
224
225         connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
226         connman_ipaddress_set_peer(ipaddress, peer);
227         vpn_provider_set_ipaddress(provider, ipaddress);
228
229         if (nameserver_list) {
230                 char *nameservers = NULL;
231                 GSList *tmp;
232
233                 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
234                 for (tmp = nameserver_list; tmp;
235                                                 tmp = g_slist_next(tmp)) {
236                         struct nameserver_entry *ns = tmp->data;
237
238                         if (!nameservers) {
239                                 nameservers = g_strdup(ns->nameserver);
240                         } else {
241                                 char *str;
242                                 str = g_strjoin(" ", nameservers,
243                                                 ns->nameserver, NULL);
244                                 g_free(nameservers);
245                                 nameservers = str;
246                         }
247                 }
248
249                 g_slist_free_full(nameserver_list, free_ns_entry);
250
251                 vpn_provider_set_nameservers(provider, nameservers);
252
253                 g_free(nameservers);
254         }
255
256         g_free(address);
257         g_free(gateway);
258         g_free(peer);
259         connman_ipaddress_free(ipaddress);
260
261         return VPN_STATE_CONNECT;
262 }
263
264 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
265 {
266         const char *option;
267         int i;
268
269         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
270                 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
271                         option = vpn_provider_get_string(provider,
272                                                         ov_options[i].cm_opt);
273                         if (!option)
274                                 continue;
275
276                         g_key_file_set_string(keyfile,
277                                         vpn_provider_get_save_group(provider),
278                                         ov_options[i].cm_opt, option);
279                 }
280         }
281         return 0;
282 }
283
284 static int task_append_config_data(struct vpn_provider *provider,
285                                         struct connman_task *task)
286 {
287         const char *option;
288         int i;
289
290         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
291                 if (!ov_options[i].ov_opt)
292                         continue;
293
294                 option = vpn_provider_get_string(provider,
295                                         ov_options[i].cm_opt);
296                 if (!option)
297                         continue;
298
299                 if (connman_task_add_argument(task,
300                                 ov_options[i].ov_opt,
301                                 ov_options[i].has_value ? option : NULL) < 0) {
302                         return -EIO;
303                 }
304         }
305
306         return 0;
307 }
308
309 static int ov_connect(struct vpn_provider *provider,
310                         struct connman_task *task, const char *if_name,
311                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
312                         void *user_data)
313 {
314         const char *option;
315         int err = 0, fd;
316
317         option = vpn_provider_get_string(provider, "Host");
318         if (!option) {
319                 connman_error("Host not set; cannot enable VPN");
320                 return -EINVAL;
321         }
322
323         task_append_config_data(provider, task);
324
325         option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
326         if (!option) {
327                 /*
328                  * Set some default options if user has no config file.
329                  */
330                 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
331                 if (option) {
332                         connman_task_add_argument(task, "--tls-auth", option);
333                         option = vpn_provider_get_string(provider,
334                                                         "OpenVPN.TLSAuthDir");
335                         if (option)
336                                 connman_task_add_argument(task, option, NULL);
337                 }
338
339                 connman_task_add_argument(task, "--nobind", NULL);
340                 connman_task_add_argument(task, "--persist-key", NULL);
341                 connman_task_add_argument(task, "--client", NULL);
342         }
343
344         connman_task_add_argument(task, "--syslog", NULL);
345
346         connman_task_add_argument(task, "--script-security", "2");
347
348         connman_task_add_argument(task, "--up",
349                                         SCRIPTDIR "/openvpn-script");
350         connman_task_add_argument(task, "--up-restart", NULL);
351
352         connman_task_add_argument(task, "--setenv", NULL);
353         connman_task_add_argument(task, "CONNMAN_BUSNAME",
354                                         dbus_bus_get_unique_name(connection));
355
356         connman_task_add_argument(task, "--setenv", NULL);
357         connman_task_add_argument(task, "CONNMAN_INTERFACE",
358                                         CONNMAN_TASK_INTERFACE);
359
360         connman_task_add_argument(task, "--setenv", NULL);
361         connman_task_add_argument(task, "CONNMAN_PATH",
362                                         connman_task_get_path(task));
363
364         connman_task_add_argument(task, "--dev", if_name);
365         connman_task_add_argument(task, "--dev-type", "tun");
366
367         connman_task_add_argument(task, "--persist-tun", NULL);
368
369 #if !defined TIZEN_EXT
370         connman_task_add_argument(task, "--route-noexec", NULL);
371         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
372 #endif
373
374         /*
375          * Disable client restarts because we can't handle this at the
376          * moment. The problem is that when OpenVPN decides to switch
377          * from CONNECTED state to RECONNECTING and then to RESOLVE,
378          * it is not possible to do a DNS lookup. The DNS server is
379          * not accessable through the tunnel anymore and so we end up
380          * trying to resolve the OpenVPN servers address.
381          */
382         connman_task_add_argument(task, "--ping-restart", "0");
383
384         fd = fileno(stderr);
385         err = connman_task_run(task, vpn_died, provider,
386                         NULL, &fd, &fd);
387         if (err < 0) {
388                 connman_error("openvpn failed to start");
389                 err = -EIO;
390                 goto done;
391         }
392
393 done:
394         if (cb)
395                 cb(provider, user_data, err);
396
397         return err;
398 }
399
400 static struct vpn_driver vpn_driver = {
401         .notify = ov_notify,
402         .connect        = ov_connect,
403         .save           = ov_save,
404 };
405
406 static int openvpn_init(void)
407 {
408         connection = connman_dbus_get_connection();
409
410         return vpn_register("openvpn", &vpn_driver, OPENVPN);
411 }
412
413 static void openvpn_exit(void)
414 {
415         vpn_unregister("openvpn");
416
417         dbus_connection_unref(connection);
418 }
419
420 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
421         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)