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