9f69850fbfb434504cf2cbe3ba17a37a677397b0
[platform/upstream/connman.git] / vpn / plugins / vpnc.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *  Copyright (C) 2010  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         connman_bool_t 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") == TRUE ||
133                         g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
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 == NULL) {
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 != NULL && value != NULL) {
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 != NULL && value != NULL) {
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 == FALSE)
221                         opt_s = vpnc_options[i].vpnc_default;
222
223                 if (opt_s == FALSE)
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 == FALSE)
250                                 continue;
251
252                         option = vpn_provider_get_string(provider,
253                                                         vpnc_options[i].cm_opt);
254                         if (option == NULL)
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, void *user_data)
268 {
269         const char *option;
270         int err = 0, fd;
271
272         option = vpn_provider_get_string(provider, "Host");
273         if (option == NULL) {
274                 connman_error("Host not set; cannot enable VPN");
275                 err = -EINVAL;
276                 goto done;
277         }
278         option = vpn_provider_get_string(provider, "VPNC.IPSec.ID");
279         if (option == NULL) {
280                 connman_error("Group not set; cannot enable VPN");
281                 err = -EINVAL;
282                 goto done;
283         }
284
285         connman_task_add_argument(task, "--non-inter", NULL);
286         connman_task_add_argument(task, "--no-detach", NULL);
287
288         connman_task_add_argument(task, "--ifname", if_name);
289         connman_task_add_argument(task, "--ifmode", "tun");
290
291         connman_task_add_argument(task, "--script",
292                                 SCRIPTDIR "/openconnect-script");
293
294         option = vpn_provider_get_string(provider, "VPNC.Debug");
295         if (option != NULL)
296                 connman_task_add_argument(task, "--debug", option);
297
298         connman_task_add_argument(task, "-", NULL);
299
300         err = connman_task_run(task, vpn_died, provider,
301                                 &fd, NULL, NULL);
302         if (err < 0) {
303                 connman_error("vpnc failed to start");
304                 err = -EIO;
305                 goto done;
306         }
307
308         err = vc_write_config_data(provider, fd);
309
310         close(fd);
311
312 done:
313         if (cb != NULL)
314                 cb(provider, user_data, err);
315
316         return err;
317 }
318
319 static int vc_error_code(int exit_code)
320 {
321         switch (exit_code) {
322         case 1:
323                 return VPN_PROVIDER_ERROR_CONNECT_FAILED;
324         case 2:
325                 return VPN_PROVIDER_ERROR_LOGIN_FAILED;
326         default:
327                 return VPN_PROVIDER_ERROR_UNKNOWN;
328         }
329 }
330
331 static struct vpn_driver vpn_driver = {
332         .notify         = vc_notify,
333         .connect        = vc_connect,
334         .error_code     = vc_error_code,
335         .save           = vc_save,
336 };
337
338 static int vpnc_init(void)
339 {
340         connection = connman_dbus_get_connection();
341
342         return vpn_register("vpnc", &vpn_driver, VPNC);
343 }
344
345 static void vpnc_exit(void)
346 {
347         vpn_unregister("vpnc");
348
349         dbus_connection_unref(connection);
350 }
351
352 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
353         CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)