af9dbe7678b39b9e75f5e223fc67493ecbaf3131
[platform/upstream/connman.git] / vpn / plugins / vpnc.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010,2013  BMW Car IT GmbH.
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 #include <linux/if_tun.h>
33
34 #include <glib.h>
35
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/log.h>
39 #include <connman/task.h>
40 #include <connman/ipconfig.h>
41 #include <connman/dbus.h>
42
43 #include "../vpn-provider.h"
44
45 #include "vpn.h"
46
47 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
48
49 static DBusConnection *connection;
50
51 enum {
52         OPT_STRING = 1,
53         OPT_BOOLEAN = 2,
54 };
55
56 struct {
57         const char *cm_opt;
58         const char *vpnc_opt;
59         const char *vpnc_default;
60         int type;
61         bool cm_save;
62 } vpnc_options[] = {
63         { "Host", "IPSec gateway", NULL, OPT_STRING, true },
64         { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING, true },
65         { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING, false },
66         { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING, false },
67         { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING, false },
68         { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING, true },
69         { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING, true },
70         { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING, true },
71         { "VPNC.Domain", "Domain", NULL, OPT_STRING, true },
72         { "VPNC.Vendor", "Vendor", NULL, OPT_STRING, true },
73         { "VPNC.LocalPort", "Local Port", "0", OPT_STRING, true, },
74         { "VPNC.CiscoPort", "Cisco UDP Encapsulation Port", "0", OPT_STRING,
75                                                                         true },
76         { "VPNC.AppVersion", "Application version", NULL, OPT_STRING, true },
77         { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING,
78                                                                         true },
79         { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING,
80                                                                         true },
81         { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN, true },
82         { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN,
83                                                                         true },
84 };
85
86 static int vc_notify(DBusMessage *msg, struct vpn_provider *provider)
87 {
88         DBusMessageIter iter, dict;
89         char *address = NULL, *netmask = NULL, *gateway = NULL;
90         struct connman_ipaddress *ipaddress;
91         const char *reason, *key, *value;
92
93         dbus_message_iter_init(msg, &iter);
94
95         dbus_message_iter_get_basic(&iter, &reason);
96         dbus_message_iter_next(&iter);
97
98         if (!provider) {
99                 connman_error("No provider found");
100                 return VPN_STATE_FAILURE;
101         }
102
103         if (strcmp(reason, "connect"))
104                 return VPN_STATE_DISCONNECT;
105
106         dbus_message_iter_recurse(&iter, &dict);
107
108         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
109                 DBusMessageIter entry;
110
111                 dbus_message_iter_recurse(&dict, &entry);
112                 dbus_message_iter_get_basic(&entry, &key);
113                 dbus_message_iter_next(&entry);
114                 dbus_message_iter_get_basic(&entry, &value);
115
116                 DBG("%s = %s", key, value);
117
118                 if (!strcmp(key, "VPNGATEWAY"))
119                         gateway = g_strdup(value);
120
121                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
122                         address = g_strdup(value);
123
124                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
125                         netmask = g_strdup(value);
126
127                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
128                         vpn_provider_set_nameservers(provider, value);
129
130                 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
131                         vpn_provider_set_domain(provider, value);
132
133                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") ||
134                         g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC"))
135                         vpn_provider_append_route(provider, key, value);
136
137                 dbus_message_iter_next(&dict);
138         }
139
140
141         ipaddress = connman_ipaddress_alloc(AF_INET);
142         if (!ipaddress) {
143                 g_free(address);
144                 g_free(netmask);
145                 g_free(gateway);
146
147                 return VPN_STATE_FAILURE;
148         }
149
150         connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
151         vpn_provider_set_ipaddress(provider, ipaddress);
152
153         g_free(address);
154         g_free(netmask);
155         g_free(gateway);
156         connman_ipaddress_free(ipaddress);
157
158         return VPN_STATE_CONNECT;
159 }
160
161 static ssize_t full_write(int fd, const void *buf, size_t len)
162 {
163         ssize_t byte_write;
164
165         while (len) {
166                 byte_write = write(fd, buf, len);
167                 if (byte_write < 0) {
168                         connman_error("failed to write config to vpnc: %s\n",
169                                         strerror(errno));
170                         return byte_write;
171                 }
172                 len -= byte_write;
173                 buf += byte_write;
174         }
175
176         return 0;
177 }
178
179 static ssize_t write_option(int fd, const char *key, const char *value)
180 {
181         gchar *buf;
182         ssize_t ret = 0;
183
184         if (key && value) {
185                 buf = g_strdup_printf("%s %s\n", key, value);
186                 ret = full_write(fd, buf, strlen(buf));
187
188                 g_free(buf);
189         }
190
191         return ret;
192 }
193
194 static ssize_t write_bool_option(int fd, const char *key, const char *value)
195 {
196         gchar *buf;
197         ssize_t ret = 0;
198
199         if (key && value) {
200                 if (strcasecmp(value, "yes") == 0 ||
201                                 strcasecmp(value, "true") == 0 ||
202                                 strcmp(value, "1") == 0) {
203                         buf = g_strdup_printf("%s\n", key);
204                         ret = full_write(fd, buf, strlen(buf));
205
206                         g_free(buf);
207                 }
208         }
209
210         return ret;
211 }
212
213 static int vc_write_config_data(struct vpn_provider *provider, int fd)
214 {
215         const char *opt_s;
216         int i;
217
218         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
219                 opt_s = vpn_provider_get_string(provider,
220                                         vpnc_options[i].cm_opt);
221                 if (!opt_s)
222                         opt_s = vpnc_options[i].vpnc_default;
223
224                 if (!opt_s)
225                         continue;
226
227                 if (vpnc_options[i].type == OPT_STRING) {
228                         if (write_option(fd,
229                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
230                                 return -EIO;
231                 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
232                         if (write_bool_option(fd,
233                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
234                                 return -EIO;
235                 }
236
237         }
238
239         return 0;
240 }
241
242 static int vc_save(struct vpn_provider *provider, GKeyFile *keyfile)
243 {
244         const char *option;
245         int i;
246
247         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
248                 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
249
250                         if (!vpnc_options[i].cm_save)
251                                 continue;
252
253                         option = vpn_provider_get_string(provider,
254                                                         vpnc_options[i].cm_opt);
255                         if (!option)
256                                 continue;
257
258                         g_key_file_set_string(keyfile,
259                                         vpn_provider_get_save_group(provider),
260                                         vpnc_options[i].cm_opt, option);
261                 }
262         }
263         return 0;
264 }
265
266 static int vc_connect(struct vpn_provider *provider,
267                         struct connman_task *task, const char *if_name,
268                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
269                         void *user_data)
270 {
271         const char *option;
272         int err = 0, fd;
273
274         option = vpn_provider_get_string(provider, "Host");
275         if (!option) {
276                 connman_error("Host not set; cannot enable VPN");
277                 err = -EINVAL;
278                 goto done;
279         }
280         option = vpn_provider_get_string(provider, "VPNC.IPSec.ID");
281         if (!option) {
282                 connman_error("Group not set; cannot enable VPN");
283                 err = -EINVAL;
284                 goto done;
285         }
286
287         connman_task_add_argument(task, "--non-inter", NULL);
288         connman_task_add_argument(task, "--no-detach", NULL);
289
290         connman_task_add_argument(task, "--ifname", if_name);
291         option = vpn_provider_get_string(provider, "VPNC.DeviceType");
292         if (option) {
293                 connman_task_add_argument(task, "--ifmode", option);
294         } else {
295                 /*
296                  * Default to tun for backwards compatibility.
297                  */
298                 connman_task_add_argument(task, "--ifmode", "tun");
299         }
300
301         connman_task_add_argument(task, "--script",
302                                 SCRIPTDIR "/openconnect-script");
303
304         option = vpn_provider_get_string(provider, "VPNC.Debug");
305         if (option)
306                 connman_task_add_argument(task, "--debug", option);
307
308         connman_task_add_argument(task, "-", NULL);
309
310         err = connman_task_run(task, vpn_died, provider,
311                                 &fd, NULL, NULL);
312         if (err < 0) {
313                 connman_error("vpnc failed to start");
314                 err = -EIO;
315                 goto done;
316         }
317
318         err = vc_write_config_data(provider, fd);
319
320         close(fd);
321
322 done:
323         if (cb)
324                 cb(provider, user_data, err);
325
326         return err;
327 }
328
329 static int vc_error_code(struct vpn_provider *provider, int exit_code)
330 {
331         switch (exit_code) {
332         case 1:
333                 return VPN_PROVIDER_ERROR_CONNECT_FAILED;
334         case 2:
335                 return VPN_PROVIDER_ERROR_LOGIN_FAILED;
336         default:
337                 return VPN_PROVIDER_ERROR_UNKNOWN;
338         }
339 }
340
341 static int vc_device_flags(struct vpn_provider *provider)
342 {
343         const char *option;
344
345         option = vpn_provider_get_string(provider, "VPNC.DeviceType");
346         if (!option) {
347                 return IFF_TUN;
348         }
349
350         if (g_str_equal(option, "tap")) {
351                 return IFF_TAP;
352         }
353
354         if (!g_str_equal(option, "tun")) {
355                 connman_warn("bad VPNC.DeviceType value, falling back to tun");
356         }
357
358         return IFF_TUN;
359 }
360
361 static struct vpn_driver vpn_driver = {
362         .notify         = vc_notify,
363         .connect        = vc_connect,
364         .error_code     = vc_error_code,
365         .save           = vc_save,
366         .device_flags   = vc_device_flags,
367 };
368
369 static int vpnc_init(void)
370 {
371         connection = connman_dbus_get_connection();
372
373         return vpn_register("vpnc", &vpn_driver, VPNC);
374 }
375
376 static void vpnc_exit(void)
377 {
378         vpn_unregister("vpnc");
379
380         dbus_connection_unref(connection);
381 }
382
383 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
384         CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)