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