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