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