session: Add configuration plugin
[platform/upstream/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 (strcasecmp(value, "yes") == 0 ||
198                                 strcasecmp(value, "true") == 0 ||
199                                 strcmp(value, "1") == 0) {
200                         buf = g_strdup_printf("%s\n", key);
201                         ret = full_write(fd, buf, strlen(buf));
202
203                         g_free(buf);
204                 }
205         }
206
207         return ret;
208 }
209
210 static int vc_write_config_data(struct connman_provider *provider, int fd)
211 {
212         const char *opt_s;
213         int i;
214
215         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
216                 opt_s = connman_provider_get_string(provider,
217                                         vpnc_options[i].cm_opt);
218                 if (!opt_s)
219                         opt_s= vpnc_options[i].vpnc_default;
220
221                 if(!opt_s)
222                         continue;
223
224                 if (vpnc_options[i].type == OPT_STRING) {
225                         if (write_option(fd,
226                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
227                                 return -EIO;
228                 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
229                         if (write_bool_option(fd,
230                                         vpnc_options[i].vpnc_opt, opt_s) < 0)
231                                 return -EIO;
232                 }
233
234         }
235
236         return 0;
237 }
238
239 static int vc_save(struct connman_provider *provider, GKeyFile *keyfile)
240 {
241         const char *option;
242         int i;
243
244         for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
245                 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
246
247                         if (vpnc_options[i].cm_save == FALSE)
248                                 continue;
249
250                         option = connman_provider_get_string(provider,
251                                                         vpnc_options[i].cm_opt);
252                         if (option == NULL)
253                                 continue;
254
255                         g_key_file_set_string(keyfile,
256                                         connman_provider_get_save_group(provider),
257                                         vpnc_options[i].cm_opt, option);
258                 }
259         }
260         return 0;
261 }
262
263 static int vc_connect(struct connman_provider *provider,
264                 struct connman_task *task, const char *if_name)
265 {
266         const char *option;
267         int err, fd;
268
269         option = connman_provider_get_string(provider, "Host");
270         if (option == NULL) {
271                 connman_error("Host not set; cannot enable VPN");
272                 return -EINVAL;
273         }
274         option = connman_provider_get_string(provider, "VPNC.IPSec.ID");
275         if (option == NULL) {
276                 connman_error("Group not set; cannot enable VPN");
277                 return -EINVAL;
278         }
279
280         connman_task_add_argument(task, "--non-inter", NULL);
281         connman_task_add_argument(task, "--no-detach", NULL);
282
283         connman_task_add_argument(task, "--ifname", if_name);
284         connman_task_add_argument(task, "--ifmode", "tun");
285
286         connman_task_add_argument(task, "--script",
287                                 SCRIPTDIR "/openconnect-script");
288
289         option = connman_provider_get_string(provider, "VPNC.Debug");
290         if (option != NULL)
291                 connman_task_add_argument(task, "--debug", option);
292
293         connman_task_add_argument(task, "-", NULL);
294
295         err = connman_task_run(task, vpn_died, provider,
296                                 &fd, NULL, NULL);
297         if (err < 0) {
298                 connman_error("vpnc failed to start");
299                 return -EIO;
300         }
301
302         err = vc_write_config_data(provider, fd);
303
304         close(fd);
305
306         return err;
307 }
308
309 static int vc_error_code(int exit_code)
310 {
311         switch (exit_code) {
312         case 1:
313                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
314         case 2:
315                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
316         default:
317                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
318         }
319 }
320
321 static struct vpn_driver vpn_driver = {
322         .notify         = vc_notify,
323         .connect        = vc_connect,
324         .error_code     = vc_error_code,
325         .save           = vc_save,
326 };
327
328 static int vpnc_init(void)
329 {
330         connection = connman_dbus_get_connection();
331
332         return vpn_register("vpnc", &vpn_driver, VPNC);
333 }
334
335 static void vpnc_exit(void)
336 {
337         vpn_unregister("vpnc");
338
339         dbus_connection_unref(connection);
340 }
341
342 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
343         CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)