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