task: use g_hash_table_replace instead of insert function
[framework/connectivity/connman.git] / plugins / openvpn.c
1 /*
2  *
3  *  Connection Manager
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 <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <net/if.h>
31
32 #include <glib.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/provider.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.h"
43
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
45
46 static DBusConnection *connection;
47
48 struct {
49         const char *cm_opt;
50         const char *ov_opt;
51 } ov_options[] = {
52         { "Host", "--remote" },
53         { "OpenVPN.CACert", "--ca" },
54         { "OpenVPN.Cert", "--cert" },
55         { "OpenVPN.Key", "--key" },
56         { "OpenVPN.MTU", "--mtu" },
57         { "OpenVPN.Proto", "--proto" },
58         { "OpenVPN.Port", "--port" },
59         { "OpenVPN.AuthUserPass", "--auth-user-pass" },
60         { "OpenVPN.TLSRemote", "--tls-remote" },
61         { "OpenVPN.Cipher", "--cipher" },
62         { "OpenVPN.Auth", "--auth" },
63         { "OpenVPN.CompLZO", "--comp-lzo" },
64         { "OpenVPN.RemoteCertTls", "--remote-cert-tls" },
65 };
66
67 static void ov_append_dns_entries(const char *key, const char *value,
68                                         char **dns_entries)
69 {
70         gchar **options;
71
72         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
73                 return;
74
75         options = g_strsplit(value, " ", 3);
76         if (options[0] != NULL &&
77                 !strcmp(options[0], "dhcp-option") &&
78                         options[1] != NULL &&
79                         !strcmp(options[1], "DNS") &&
80                                 options[2] != NULL) {
81
82                 if (*dns_entries != NULL) {
83                         char *tmp;
84
85                         tmp = g_strjoin(" ", *dns_entries,
86                                                 options[2], NULL);
87                         g_free(*dns_entries);
88                         *dns_entries = tmp;
89                 } else {
90                         *dns_entries = g_strdup(options[2]);
91                 }
92         }
93
94         g_strfreev(options);
95 }
96
97 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
98 {
99         DBusMessageIter iter, dict;
100         const char *reason, *key, *value;
101         char *nameservers = NULL;
102         char *address = NULL, *gateway = NULL, *peer = NULL;
103         struct connman_ipaddress *ipaddress;
104
105         dbus_message_iter_init(msg, &iter);
106
107         dbus_message_iter_get_basic(&iter, &reason);
108         dbus_message_iter_next(&iter);
109
110         if (!provider) {
111                 connman_error("No provider found");
112                 return VPN_STATE_FAILURE;
113         }
114
115         if (strcmp(reason, "up"))
116                 return VPN_STATE_DISCONNECT;
117
118         dbus_message_iter_recurse(&iter, &dict);
119
120         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
121                 DBusMessageIter entry;
122
123                 dbus_message_iter_recurse(&dict, &entry);
124                 dbus_message_iter_get_basic(&entry, &key);
125                 dbus_message_iter_next(&entry);
126                 dbus_message_iter_get_basic(&entry, &value);
127
128                 DBG("%s = %s", key, value);
129
130                 if (!strcmp(key, "trusted_ip")) {
131                         connman_provider_set_string(provider, "Gateway", value);
132                         gateway = g_strdup(value);
133                 }
134
135                 if (!strcmp(key, "ifconfig_local")) {
136                         connman_provider_set_string(provider, "Address", value);
137                         address = g_strdup(value);
138                 }
139
140                 if (!strcmp(key, "ifconfig_remote")) {
141                         connman_provider_set_string(provider, "Peer", value);
142                         peer = g_strdup(value);
143                 }
144
145                 if (g_str_has_prefix(key, "route_") == TRUE)
146                         connman_provider_append_route(provider, key, value);
147
148                 ov_append_dns_entries(key, value, &nameservers);
149
150                 dbus_message_iter_next(&dict);
151         }
152
153         ipaddress = connman_ipaddress_alloc(AF_INET);
154         if (ipaddress == NULL) {
155                 g_free(nameservers);
156                 g_free(address);
157                 g_free(gateway);
158                 g_free(peer);
159
160                 return VPN_STATE_FAILURE;
161         }
162
163         connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
164         connman_ipaddress_set_peer(ipaddress, peer);
165         connman_provider_set_ipaddress(provider, ipaddress);
166
167         connman_provider_set_nameservers(provider, nameservers);
168
169         g_free(nameservers);
170         g_free(address);
171         g_free(gateway);
172         g_free(peer);
173         connman_ipaddress_free(ipaddress);
174
175         return VPN_STATE_CONNECT;
176 }
177
178 static int ov_save(struct connman_provider *provider, GKeyFile *keyfile)
179 {
180         const char *option;
181         int i;
182
183         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
184                 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
185                         option = connman_provider_get_string(provider,
186                                                         ov_options[i].cm_opt);
187                         if (option == NULL)
188                                 continue;
189
190                         g_key_file_set_string(keyfile,
191                                         connman_provider_get_save_group(provider),
192                                         ov_options[i].cm_opt, option);
193                 }
194         }
195         return 0;
196 }
197
198 static int task_append_config_data(struct connman_provider *provider,
199                                         struct connman_task *task)
200 {
201         const char *option;
202         int i;
203
204         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
205                 option = connman_provider_get_string(provider,
206                                         ov_options[i].cm_opt);
207                 if (option == NULL)
208                         continue;
209
210                 if (connman_task_add_argument(task,
211                                         ov_options[i].ov_opt, option) < 0) {
212                         return -EIO;
213                 }
214         }
215
216         return 0;
217 }
218
219 static int ov_connect(struct connman_provider *provider,
220                 struct connman_task *task, const char *if_name)
221 {
222         const char *option;
223         int err, fd;
224
225         option = connman_provider_get_string(provider, "Host");
226         if (option == NULL) {
227                 connman_error("Host not set; cannot enable VPN");
228                 return -EINVAL;
229         }
230
231         task_append_config_data(provider, task);
232
233         connman_task_add_argument(task, "--syslog", NULL);
234
235         connman_task_add_argument(task, "--script-security", "2");
236
237         connman_task_add_argument(task, "--up",
238                                         SCRIPTDIR "/openvpn-script");
239         connman_task_add_argument(task, "--up-restart", NULL);
240
241         connman_task_add_argument(task, "--setenv", NULL);
242         connman_task_add_argument(task, "CONNMAN_BUSNAME",
243                                         dbus_bus_get_unique_name(connection));
244
245         connman_task_add_argument(task, "--setenv", NULL);
246         connman_task_add_argument(task, "CONNMAN_INTERFACE",
247                                         CONNMAN_TASK_INTERFACE);
248
249         connman_task_add_argument(task, "--setenv", NULL);
250         connman_task_add_argument(task, "CONNMAN_PATH",
251                                         connman_task_get_path(task));
252
253         connman_task_add_argument(task, "--dev", if_name);
254         connman_task_add_argument(task, "--dev-type", "tun");
255
256         connman_task_add_argument(task, "--tls-client", NULL);
257         connman_task_add_argument(task, "--nobind", NULL);
258         connman_task_add_argument(task, "--persist-key", NULL);
259         connman_task_add_argument(task, "--persist-tun", NULL);
260
261         connman_task_add_argument(task, "--route-noexec", NULL);
262         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
263
264         /*
265          * Disable client restarts because we can't handle this at the
266          * moment. The problem is that when OpenVPN decides to switch
267          * from CONNECTED state to RECONNECTING and then to RESOLVE,
268          * it is not possible to do a DNS lookup. The DNS server is
269          * not accessable through the tunnel anymore and so we end up
270          * trying to resolve the OpenVPN servers address.
271          */
272         connman_task_add_argument(task, "--ping-restart", "0");
273
274         connman_task_add_argument(task, "--client", NULL);
275
276         fd = fileno(stderr);
277         err = connman_task_run(task, vpn_died, provider,
278                         NULL, &fd, &fd);
279         if (err < 0) {
280                 connman_error("openvpn failed to start");
281                 return -EIO;
282         }
283
284         return 0;
285 }
286
287 static struct vpn_driver vpn_driver = {
288         .notify = ov_notify,
289         .connect        = ov_connect,
290         .save           = ov_save,
291 };
292
293 static int openvpn_init(void)
294 {
295         connection = connman_dbus_get_connection();
296
297         return vpn_register("openvpn", &vpn_driver, OPENVPN);
298 }
299
300 static void openvpn_exit(void)
301 {
302         vpn_unregister("openvpn");
303
304         dbus_connection_unref(connection);
305 }
306
307 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
308         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)