Imported Upstream version 1.24
[platform/upstream/connman.git] / vpn / plugins / vpnc.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010,2013  BMW Car IT GmbH. All rights reserved.
6  *  Copyright (C) 2010,2012-2013  Intel Corporation. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <net/if.h>
32
33 #include <glib.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/ipconfig.h>
40 #include <connman/dbus.h>
41
42 #include "../vpn-provider.h"
43
44 #include "vpn.h"
45
46 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
47
48 static DBusConnection *connection;
49
50 enum {
51         OPT_STRING = 1,
52         OPT_BOOLEAN = 2,
53 };
54
55 struct {
56         const char *cm_opt;
57         const char *vpnc_opt;
58         const char *vpnc_default;
59         int type;
60         bool cm_save;
61 } vpnc_options[] = {
62         { "Host", "IPSec gateway", NULL, OPT_STRING, true },
63         { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING, true },
64         { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING, false },
65         { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING, false },
66         { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING, false },
67         { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING, true },
68         { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING, true },
69         { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING, true },
70         { "VPNC.Domain", "Domain", NULL, OPT_STRING, true },
71         { "VPNC.Vendor", "Vendor", NULL, OPT_STRING, true },
72         { "VPNC.LocalPort", "Local Port", "0", OPT_STRING, true, },
73         { "VPNC.CiscoPort", "Cisco UDP Encapsulation Port", "0", OPT_STRING,
74                                                                         true },
75         { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING, true },
76         { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING,
77                                                                         true },
78         { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING,
79                                                                         true },
80         { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN, true },
81         { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN,
82                                                                         true },
83 };
84
85 static int vc_notify(DBusMessage *msg, struct vpn_provider *provider)
86 {
87         DBusMessageIter iter, dict;
88         char *address = NULL, *netmask = NULL, *gateway = NULL;
89         struct connman_ipaddress *ipaddress;
90         const char *reason, *key, *value;
91
92         dbus_message_iter_init(msg, &iter);
93
94         dbus_message_iter_get_basic(&iter, &reason);
95         dbus_message_iter_next(&iter);
96
97         if (!provider) {
98                 connman_error("No provider found");
99                 return VPN_STATE_FAILURE;
100         }
101
102         if (strcmp(reason, "connect"))
103                 return VPN_STATE_DISCONNECT;
104
105         dbus_message_iter_recurse(&iter, &dict);
106
107         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
108                 DBusMessageIter entry;
109
110                 dbus_message_iter_recurse(&dict, &entry);
111                 dbus_message_iter_get_basic(&entry, &key);
112                 dbus_message_iter_next(&entry);
113                 dbus_message_iter_get_basic(&entry, &value);
114
115                 DBG("%s = %s", key, value);
116
117                 if (!strcmp(key, "VPNGATEWAY"))
118                         gateway = g_strdup(value);
119
120                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
121                         address = g_strdup(value);
122
123                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
124                         netmask = g_strdup(value);
125
126                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
127                         vpn_provider_set_nameservers(provider, value);
128
129                 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
130                         vpn_provider_set_domain(provider, value);
131
132                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") ||
133                         g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC"))
134                         vpn_provider_append_route(provider, key, value);
135
136                 dbus_message_iter_next(&dict);
137         }
138
139
140         ipaddress = connman_ipaddress_alloc(AF_INET);
141         if (!ipaddress) {
142                 g_free(address);
143                 g_free(netmask);
144                 g_free(gateway);
145
146                 return VPN_STATE_FAILURE;
147         }
148
149         connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
150         vpn_provider_set_ipaddress(provider, ipaddress);
151
152         g_free(address);
153         g_free(netmask);
154         g_free(gateway);
155         connman_ipaddress_free(ipaddress);
156
157         return VPN_STATE_CONNECT;
158 }
159
160 static ssize_t full_write(int fd, const void *buf, size_t len)
161 {
162         ssize_t byte_write;
163
164         while (len) {
165                 byte_write = write(fd, buf, len);
166                 if (byte_write < 0) {
167                         connman_error("failed to write config to vpnc: %s\n",
168                                         strerror(errno));
169                         return byte_write;
170                 }
171                 len -= byte_write;
172                 buf += byte_write;
173         }
174
175         return 0;
176 }
177
178 static ssize_t write_option(int fd, const char *key, const char *value)
179 {
180         gchar *buf;
181         ssize_t ret = 0;
182
183         if (key && value) {
184                 buf = g_strdup_printf("%s %s\n", key, value);
185                 ret = full_write(fd, buf, strlen(buf));
186
187                 g_free(buf);
188         }
189
190         return ret;
191 }
192
193 static ssize_t write_bool_option(int fd, const char *key, const char *value)
194 {
195         gchar *buf;
196         ssize_t ret = 0;
197
198         if (key && value) {
199                 if (strcasecmp(value, "yes") == 0 ||
200                                 strcasecmp(value, "true") == 0 ||
201                                 strcmp(value, "1") == 0) {
202                         buf = g_strdup_printf("%s\n", key);
203                         ret = full_write(fd, buf, strlen(buf));
204
205                         g_free(buf);
206                 }
207         }
208
209         return ret;
210 }
211
212 static int vc_write_config_data(struct vpn_provider *provider, int fd)
213 {
214         const char *opt_s;
215         int i;
216
217         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
218                 opt_s = vpn_provider_get_string(provider,
219                                         vpnc_options[i].cm_opt);
220                 if (!opt_s)
221                         opt_s = vpnc_options[i].vpnc_default;
222
223                 if (!opt_s)
224                         continue;
225
226                 if (vpnc_options[i].type == OPT_STRING) {
227                         if (write_option(fd,
228                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
229                                 return -EIO;
230                 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
231                         if (write_bool_option(fd,
232                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
233                                 return -EIO;
234                 }
235
236         }
237
238         return 0;
239 }
240
241 static int vc_save(struct vpn_provider *provider, GKeyFile *keyfile)
242 {
243         const char *option;
244         int i;
245
246         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
247                 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
248
249                         if (!vpnc_options[i].cm_save)
250                                 continue;
251
252                         option = vpn_provider_get_string(provider,
253                                                         vpnc_options[i].cm_opt);
254                         if (!option)
255                                 continue;
256
257                         g_key_file_set_string(keyfile,
258                                         vpn_provider_get_save_group(provider),
259                                         vpnc_options[i].cm_opt, option);
260                 }
261         }
262         return 0;
263 }
264
265 static int vc_connect(struct vpn_provider *provider,
266                         struct connman_task *task, const char *if_name,
267                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
268                         void *user_data)
269 {
270         const char *option;
271         int err = 0, fd;
272
273         option = vpn_provider_get_string(provider, "Host");
274         if (!option) {
275                 connman_error("Host not set; cannot enable VPN");
276                 err = -EINVAL;
277                 goto done;
278         }
279         option = vpn_provider_get_string(provider, "VPNC.IPSec.ID");
280         if (!option) {
281                 connman_error("Group not set; cannot enable VPN");
282                 err = -EINVAL;
283                 goto done;
284         }
285
286         connman_task_add_argument(task, "--non-inter", NULL);
287         connman_task_add_argument(task, "--no-detach", NULL);
288
289         connman_task_add_argument(task, "--ifname", if_name);
290         connman_task_add_argument(task, "--ifmode", "tun");
291
292         connman_task_add_argument(task, "--script",
293                                 SCRIPTDIR "/openconnect-script");
294
295         option = vpn_provider_get_string(provider, "VPNC.Debug");
296         if (option)
297                 connman_task_add_argument(task, "--debug", option);
298
299         connman_task_add_argument(task, "-", NULL);
300
301         err = connman_task_run(task, vpn_died, provider,
302                                 &fd, NULL, NULL);
303         if (err < 0) {
304                 connman_error("vpnc failed to start");
305                 err = -EIO;
306                 goto done;
307         }
308
309         err = vc_write_config_data(provider, fd);
310
311         close(fd);
312
313 done:
314         if (cb)
315                 cb(provider, user_data, err);
316
317         return err;
318 }
319
320 static int vc_error_code(struct vpn_provider *provider, int exit_code)
321 {
322         switch (exit_code) {
323         case 1:
324                 return VPN_PROVIDER_ERROR_CONNECT_FAILED;
325         case 2:
326                 return VPN_PROVIDER_ERROR_LOGIN_FAILED;
327         default:
328                 return VPN_PROVIDER_ERROR_UNKNOWN;
329         }
330 }
331
332 static struct vpn_driver vpn_driver = {
333         .notify         = vc_notify,
334         .connect        = vc_connect,
335         .error_code     = vc_error_code,
336         .save           = vc_save,
337 };
338
339 static int vpnc_init(void)
340 {
341         connection = connman_dbus_get_connection();
342
343         return vpn_register("vpnc", &vpn_driver, VPNC);
344 }
345
346 static void vpnc_exit(void)
347 {
348         vpn_unregister("vpnc");
349
350         dbus_connection_unref(connection);
351 }
352
353 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
354         CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)