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