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