Imported Upstream version 1.38
[platform/upstream/connman.git] / vpn / plugins / l2tp.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010,2013  BMW Car IT GmbH.
6  *  Copyright (C) 2012-2013  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 <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 #include <stdio.h>
35 #include <net/if.h>
36
37 #include <dbus/dbus.h>
38 #include <glib.h>
39
40 #define CONNMAN_API_SUBJECT_TO_CHANGE
41 #include <connman/plugin.h>
42 #include <connman/provider.h>
43 #include <connman/log.h>
44 #include <connman/task.h>
45 #include <connman/dbus.h>
46 #include <connman/inet.h>
47 #include <connman/agent.h>
48 #include <connman/setting.h>
49 #include <connman/vpn-dbus.h>
50
51 #include "../vpn-provider.h"
52 #include "../vpn-agent.h"
53
54 #include "vpn.h"
55
56 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
57
58 enum {
59         OPT_STRING = 1,
60         OPT_BOOL = 2,
61 };
62
63 enum {
64         OPT_ALL = 1,
65         OPT_L2G = 2,
66         OPT_L2  = 3,
67         OPT_PPPD = 4,
68         OPT_L2LNS = 5,
69 };
70
71 struct {
72         const char *cm_opt;
73         const char *pppd_opt;
74         int sub;
75         const char *vpn_default;
76         int type;
77 } pppd_options[] = {
78         { "L2TP.User", "name", OPT_ALL, NULL, OPT_STRING },
79         { "L2TP.BPS", "bps", OPT_L2, NULL, OPT_STRING },
80         { "L2TP.TXBPS", "tx bps", OPT_L2, NULL, OPT_STRING },
81         { "L2TP.RXBPS", "rx bps", OPT_L2, NULL, OPT_STRING },
82         { "L2TP.LengthBit", "length bit", OPT_L2, NULL, OPT_STRING },
83         { "L2TP.Challenge", "challenge", OPT_L2, NULL, OPT_STRING },
84         { "L2TP.DefaultRoute", "defaultroute", OPT_L2, NULL, OPT_STRING },
85         { "L2TP.FlowBit", "flow bit", OPT_L2, NULL, OPT_STRING },
86         { "L2TP.TunnelRWS", "tunnel rws", OPT_L2, NULL, OPT_STRING },
87         { "L2TP.Exclusive", "exclusive", OPT_L2LNS, NULL, OPT_STRING },
88         { "L2TP.Autodial", "autodial", OPT_L2, "yes", OPT_STRING },
89         { "L2TP.Redial", "redial", OPT_L2, "yes", OPT_STRING },
90         { "L2TP.RedialTimeout", "redial timeout", OPT_L2, "10", OPT_STRING },
91         { "L2TP.MaxRedials", "max redials", OPT_L2, NULL, OPT_STRING },
92         { "L2TP.RequirePAP", "require pap", OPT_L2, "no", OPT_STRING },
93         { "L2TP.RequireCHAP", "require chap", OPT_L2, "yes", OPT_STRING },
94         { "L2TP.ReqAuth", "require authentication", OPT_L2, "no", OPT_STRING },
95         { "L2TP.AccessControl", "access control", OPT_L2G, "yes", OPT_STRING },
96         { "L2TP.AuthFile", "auth file", OPT_L2G, NULL, OPT_STRING },
97         { "L2TP.ForceUserSpace", "force userspace", OPT_L2G, NULL, OPT_STRING },
98         { "L2TP.ListenAddr", "listen-addr", OPT_L2G, NULL, OPT_STRING },
99         { "L2TP.Rand Source", "rand source", OPT_L2G, NULL, OPT_STRING },
100         { "L2TP.IPsecSaref", "ipsec saref", OPT_L2G, "no", OPT_STRING },
101         { "L2TP.Port", "port", OPT_L2G, NULL, OPT_STRING },
102         { "PPPD.EchoFailure", "lcp-echo-failure", OPT_PPPD, "0", OPT_STRING },
103         { "PPPD.EchoInterval", "lcp-echo-interval", OPT_PPPD, "0", OPT_STRING },
104         { "PPPD.Debug", "debug", OPT_PPPD, NULL, OPT_STRING },
105         { "PPPD.RefuseEAP", "refuse-eap", OPT_PPPD, NULL, OPT_BOOL },
106         { "PPPD.RefusePAP", "refuse-pap", OPT_PPPD, NULL, OPT_BOOL },
107         { "PPPD.RefuseCHAP", "refuse-chap", OPT_PPPD, NULL, OPT_BOOL },
108         { "PPPD.RefuseMSCHAP", "refuse-mschap", OPT_PPPD, NULL, OPT_BOOL },
109         { "PPPD.RefuseMSCHAP2", "refuse-mschapv2", OPT_PPPD, NULL, OPT_BOOL },
110         { "PPPD.NoBSDComp", "nobsdcomp", OPT_PPPD, NULL, OPT_BOOL },
111         { "PPPD.NoPcomp", "nopcomp", OPT_PPPD, NULL, OPT_BOOL },
112         { "PPPD.UseAccomp", "noaccomp", OPT_PPPD, NULL, OPT_BOOL },
113         { "PPPD.NoDeflate", "nodeflate", OPT_PPPD, NULL, OPT_BOOL },
114         { "PPPD.ReqMPPE", "require-mppe", OPT_PPPD, NULL, OPT_BOOL },
115         { "PPPD.ReqMPPE40", "require-mppe-40", OPT_PPPD, NULL, OPT_BOOL },
116         { "PPPD.ReqMPPE128", "require-mppe-128", OPT_PPPD, NULL, OPT_BOOL },
117         { "PPPD.ReqMPPEStateful", "mppe-stateful", OPT_PPPD, NULL, OPT_BOOL },
118         { "PPPD.NoVJ", "novj", OPT_PPPD, NULL, OPT_BOOL },
119 };
120
121 static DBusConnection *connection;
122
123 struct l2tp_private_data {
124         struct connman_task *task;
125         char *if_name;
126         vpn_provider_connect_cb_t cb;
127         void *user_data;
128 };
129
130 static DBusMessage *l2tp_get_sec(struct connman_task *task,
131                         DBusMessage *msg, void *user_data)
132 {
133         const char *user, *passwd;
134         struct vpn_provider *provider = user_data;
135
136         if (!dbus_message_get_no_reply(msg)) {
137                 DBusMessage *reply;
138
139                 user = vpn_provider_get_string(provider, "L2TP.User");
140                 passwd = vpn_provider_get_string(provider, "L2TP.Password");
141
142                 if (!user || strlen(user) == 0 ||
143                                 !passwd || strlen(passwd) == 0)
144                         return NULL;
145
146                 reply = dbus_message_new_method_return(msg);
147                 if (!reply)
148                         return NULL;
149
150                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
151                                                 DBUS_TYPE_STRING, &passwd,
152                                                 DBUS_TYPE_INVALID);
153
154                 return reply;
155         }
156
157         return NULL;
158 }
159
160 static int l2tp_notify(DBusMessage *msg, struct vpn_provider *provider)
161 {
162         DBusMessageIter iter, dict;
163         const char *reason, *key, *value;
164         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
165         char *ifname = NULL, *nameservers = NULL;
166         struct connman_ipaddress *ipaddress = NULL;
167
168         dbus_message_iter_init(msg, &iter);
169
170         dbus_message_iter_get_basic(&iter, &reason);
171         dbus_message_iter_next(&iter);
172
173         if (!provider) {
174                 connman_error("No provider found");
175                 return VPN_STATE_FAILURE;
176         }
177
178         if (strcmp(reason, "auth failed") == 0) {
179                 DBG("authentication failure");
180
181                 vpn_provider_set_string(provider, "L2TP.User", NULL);
182                 vpn_provider_set_string_hide_value(provider, "L2TP.Password",
183                                         NULL);
184
185                 return VPN_STATE_AUTH_FAILURE;
186         }
187
188         if (strcmp(reason, "connect"))
189                 return VPN_STATE_DISCONNECT;
190
191         dbus_message_iter_recurse(&iter, &dict);
192
193         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
194                 DBusMessageIter entry;
195
196                 dbus_message_iter_recurse(&dict, &entry);
197                 dbus_message_iter_get_basic(&entry, &key);
198                 dbus_message_iter_next(&entry);
199                 dbus_message_iter_get_basic(&entry, &value);
200
201                 DBG("%s = %s", key, value);
202
203                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
204                         addressv4 = g_strdup(value);
205
206                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
207                         netmask = g_strdup(value);
208
209                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
210                         nameservers = g_strdup(value);
211
212                 if (!strcmp(key, "INTERNAL_IFNAME"))
213                         ifname = g_strdup(value);
214
215                 dbus_message_iter_next(&dict);
216         }
217
218         if (vpn_set_ifname(provider, ifname) < 0) {
219                 g_free(ifname);
220                 g_free(addressv4);
221                 g_free(netmask);
222                 g_free(nameservers);
223                 return VPN_STATE_FAILURE;
224         }
225
226         if (addressv4)
227                 ipaddress = connman_ipaddress_alloc(AF_INET);
228
229         g_free(ifname);
230
231         if (!ipaddress) {
232                 connman_error("No IP address for provider");
233                 g_free(addressv4);
234                 g_free(netmask);
235                 g_free(nameservers);
236                 return VPN_STATE_FAILURE;
237         }
238
239         value = vpn_provider_get_string(provider, "HostIP");
240         if (value) {
241                 vpn_provider_set_string(provider, "Gateway", value);
242                 gateway = g_strdup(value);
243         }
244
245         if (addressv4)
246                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
247                                         gateway);
248
249         vpn_provider_set_ipaddress(provider, ipaddress);
250         vpn_provider_set_nameservers(provider, nameservers);
251
252         g_free(addressv4);
253         g_free(netmask);
254         g_free(gateway);
255         g_free(nameservers);
256         connman_ipaddress_free(ipaddress);
257
258         return VPN_STATE_CONNECT;
259 }
260
261 static int l2tp_save(struct vpn_provider *provider, GKeyFile *keyfile)
262 {
263         const char *option;
264         bool l2tp_option, pppd_option;
265         int i;
266
267         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
268                 l2tp_option = pppd_option = false;
269
270                 if (strncmp(pppd_options[i].cm_opt, "L2TP.", 5) == 0)
271                         l2tp_option = true;
272
273                 if (strncmp(pppd_options[i].cm_opt, "PPPD.", 5) == 0)
274                         pppd_option = true;
275
276                 if (l2tp_option || pppd_option) {
277                         option = vpn_provider_get_string(provider,
278                                                 pppd_options[i].cm_opt);
279                         if (!option) {
280                                 /*
281                                  * Check if the option prefix is L2TP as the
282                                  * PPPD options were using L2TP prefix earlier.
283                                  */
284                                 char *l2tp_str;
285
286                                 if (!pppd_option)
287                                         continue;
288
289                                 l2tp_str = g_strdup_printf("L2TP.%s",
290                                                 &pppd_options[i].cm_opt[5]);
291                                 option = vpn_provider_get_string(provider,
292                                                                 l2tp_str);
293                                 g_free(l2tp_str);
294
295                                 if (!option)
296                                         continue;
297                         }
298
299                         g_key_file_set_string(keyfile,
300                                         vpn_provider_get_save_group(provider),
301                                         pppd_options[i].cm_opt, option);
302                 }
303         }
304
305         return 0;
306 }
307
308 static ssize_t full_write(int fd, const void *buf, size_t len)
309 {
310         ssize_t byte_write;
311
312         while (len) {
313                 byte_write = write(fd, buf, len);
314                 if (byte_write < 0) {
315                         connman_error("failed to write config to l2tp: %s\n",
316                                         strerror(errno));
317                         return byte_write;
318                 }
319                 len -= byte_write;
320                 buf += byte_write;
321         }
322
323         return 0;
324 }
325
326 static ssize_t l2tp_write_bool_option(int fd,
327                                         const char *key, const char *value)
328 {
329         gchar *buf;
330         ssize_t ret = 0;
331
332         if (key && value) {
333                 if (strcasecmp(value, "yes") == 0 ||
334                                 strcasecmp(value, "true") == 0 ||
335                                 strcmp(value, "1") == 0) {
336                         buf = g_strdup_printf("%s\n", key);
337                         ret = full_write(fd, buf, strlen(buf));
338
339                         g_free(buf);
340                 }
341         }
342
343         return ret;
344 }
345
346 static int l2tp_write_option(int fd, const char *key, const char *value)
347 {
348         gchar *buf;
349         ssize_t ret = 0;
350
351         if (key) {
352                 if (value)
353                         buf = g_strdup_printf("%s %s\n", key, value);
354                 else
355                         buf = g_strdup_printf("%s\n", key);
356
357                 ret = full_write(fd, buf, strlen(buf));
358
359                 g_free(buf);
360         }
361
362         return ret;
363 }
364
365 static int l2tp_write_section(int fd, const char *key, const char *value)
366 {
367         gchar *buf;
368         ssize_t ret = 0;
369
370         if (key && value) {
371                 buf = g_strdup_printf("%s = %s\n", key, value);
372                 ret = full_write(fd, buf, strlen(buf));
373
374                 g_free(buf);
375         }
376
377         return ret;
378 }
379
380 static int write_pppd_option(struct vpn_provider *provider, int fd)
381 {
382         int i;
383         const char *opt_s;
384
385         l2tp_write_option(fd, "nodetach", NULL);
386         l2tp_write_option(fd, "lock", NULL);
387         l2tp_write_option(fd, "logfd", "2");
388         l2tp_write_option(fd, "usepeerdns", NULL);
389         l2tp_write_option(fd, "noipdefault", NULL);
390         l2tp_write_option(fd, "noauth", NULL);
391         l2tp_write_option(fd, "nodefaultroute", NULL);
392         l2tp_write_option(fd, "ipparam", "l2tp_plugin");
393
394         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
395                 if (pppd_options[i].sub != OPT_ALL &&
396                         pppd_options[i].sub != OPT_PPPD)
397                         continue;
398
399                 opt_s = vpn_provider_get_string(provider,
400                                         pppd_options[i].cm_opt);
401                 if (!opt_s)
402                         opt_s = pppd_options[i].vpn_default;
403
404                 if (!opt_s)
405                         continue;
406
407                 if (pppd_options[i].type == OPT_STRING)
408                         l2tp_write_option(fd,
409                                 pppd_options[i].pppd_opt, opt_s);
410                 else if (pppd_options[i].type == OPT_BOOL)
411                         l2tp_write_bool_option(fd,
412                                 pppd_options[i].pppd_opt, opt_s);
413         }
414
415         l2tp_write_option(fd, "plugin",
416                                 SCRIPTDIR "/libppp-plugin.so");
417
418         return 0;
419 }
420
421
422 static int l2tp_write_fields(struct vpn_provider *provider,
423                                                 int fd, int sub)
424 {
425         int i;
426         const char *opt_s;
427
428         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
429                 if (pppd_options[i].sub != sub)
430                         continue;
431
432                 opt_s = vpn_provider_get_string(provider,
433                                         pppd_options[i].cm_opt);
434                 if (!opt_s)
435                         opt_s = pppd_options[i].vpn_default;
436
437                 if (!opt_s)
438                         continue;
439
440                 if (pppd_options[i].type == OPT_STRING)
441                         l2tp_write_section(fd,
442                                 pppd_options[i].pppd_opt, opt_s);
443                 else if (pppd_options[i].type == OPT_BOOL)
444                         l2tp_write_bool_option(fd,
445                                 pppd_options[i].pppd_opt, opt_s);
446         }
447
448         return 0;
449 }
450
451 static int l2tp_write_config(struct vpn_provider *provider,
452                                         const char *pppd_name, int fd)
453 {
454         const char *option;
455
456         l2tp_write_option(fd, "[global]", NULL);
457         l2tp_write_fields(provider, fd, OPT_L2G);
458
459         l2tp_write_option(fd, "[lns default]", NULL);
460         l2tp_write_fields(provider, fd, OPT_L2LNS);
461
462         l2tp_write_option(fd, "[lac l2tp]", NULL);
463
464         option = vpn_provider_get_string(provider, "Host");
465         l2tp_write_option(fd, "lns =", option);
466
467         l2tp_write_fields(provider, fd, OPT_ALL);
468         l2tp_write_fields(provider, fd, OPT_L2);
469
470         l2tp_write_option(fd, "pppoptfile =", pppd_name);
471
472         return 0;
473 }
474
475 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
476 {
477         char *conf_file;
478
479         vpn_died(task, exit_code, user_data);
480
481         conf_file = g_strdup_printf(VPN_STATEDIR "/connman-xl2tpd.conf");
482         unlink(conf_file);
483         g_free(conf_file);
484
485         conf_file = g_strdup_printf(VPN_STATEDIR "/connman-ppp-option.conf");
486         unlink(conf_file);
487         g_free(conf_file);
488 }
489
490 struct request_input_reply {
491         struct vpn_provider *provider;
492         vpn_provider_password_cb_t callback;
493         void *user_data;
494 };
495
496 static void request_input_reply(DBusMessage *reply, void *user_data)
497 {
498         struct request_input_reply *l2tp_reply = user_data;
499         struct l2tp_private_data *data;
500         const char *error = NULL;
501         char *username = NULL, *password = NULL;
502         char *key;
503         DBusMessageIter iter, dict;
504         int err;
505
506         DBG("provider %p", l2tp_reply->provider);
507
508         if (!reply)
509                 goto done;
510
511         data = l2tp_reply->user_data;
512
513         err = vpn_agent_check_and_process_reply_error(reply,
514                                 l2tp_reply->provider, data->task, data->cb,
515                                 data->user_data);
516         if (err) {
517                 /* Ensure cb is called only once */
518                 data->cb = NULL;
519                 data->user_data = NULL;
520                 error = dbus_message_get_error_name(reply);
521                 goto done;
522         }
523
524         if (!vpn_agent_check_reply_has_dict(reply))
525                 goto done;
526
527         dbus_message_iter_init(reply, &iter);
528         dbus_message_iter_recurse(&iter, &dict);
529         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
530                 DBusMessageIter entry, value;
531                 const char *str;
532
533                 dbus_message_iter_recurse(&dict, &entry);
534                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
535                         break;
536
537                 dbus_message_iter_get_basic(&entry, &key);
538
539                 if (g_str_equal(key, "Username")) {
540                         dbus_message_iter_next(&entry);
541                         if (dbus_message_iter_get_arg_type(&entry)
542                                                         != DBUS_TYPE_VARIANT)
543                                 break;
544                         dbus_message_iter_recurse(&entry, &value);
545                         if (dbus_message_iter_get_arg_type(&value)
546                                                         != DBUS_TYPE_STRING)
547                                 break;
548                         dbus_message_iter_get_basic(&value, &str);
549                         username = g_strdup(str);
550                 }
551
552                 if (g_str_equal(key, "Password")) {
553                         dbus_message_iter_next(&entry);
554                         if (dbus_message_iter_get_arg_type(&entry)
555                                                         != DBUS_TYPE_VARIANT)
556                                 break;
557                         dbus_message_iter_recurse(&entry, &value);
558                         if (dbus_message_iter_get_arg_type(&value)
559                                                         != DBUS_TYPE_STRING)
560                                 break;
561                         dbus_message_iter_get_basic(&value, &str);
562                         password = g_strdup(str);
563                 }
564
565                 dbus_message_iter_next(&dict);
566         }
567
568 done:
569         l2tp_reply->callback(l2tp_reply->provider, username, password, error,
570                                 l2tp_reply->user_data);
571
572         g_free(username);
573         g_free(password);
574
575         g_free(l2tp_reply);
576 }
577
578 typedef void (* request_cb_t)(struct vpn_provider *provider,
579                                 const char *username, const char *password,
580                                 const char *error, void *user_data);
581
582 static int request_input(struct vpn_provider *provider,
583                         request_cb_t callback, const char *dbus_sender,
584                         void *user_data)
585 {
586         DBusMessage *message;
587         const char *path, *agent_sender, *agent_path;
588         DBusMessageIter iter;
589         DBusMessageIter dict;
590         struct request_input_reply *l2tp_reply;
591         int err;
592         void *agent;
593
594         agent = connman_agent_get_info(dbus_sender, &agent_sender,
595                                                         &agent_path);
596         if (!provider || !agent || !agent_path || !callback)
597                 return -ESRCH;
598
599         message = dbus_message_new_method_call(agent_sender, agent_path,
600                                         VPN_AGENT_INTERFACE,
601                                         "RequestInput");
602         if (!message)
603                 return -ENOMEM;
604
605         dbus_message_iter_init_append(message, &iter);
606
607         path = vpn_provider_get_path(provider);
608         dbus_message_iter_append_basic(&iter,
609                                 DBUS_TYPE_OBJECT_PATH, &path);
610
611         connman_dbus_dict_open(&iter, &dict);
612
613         if (vpn_provider_get_authentication_errors(provider))
614                 vpn_agent_append_auth_failure(&dict, provider, NULL);
615
616         vpn_agent_append_user_info(&dict, provider, "L2TP.User");
617
618         vpn_agent_append_host_and_name(&dict, provider);
619
620         connman_dbus_dict_close(&iter, &dict);
621
622         l2tp_reply = g_try_new0(struct request_input_reply, 1);
623         if (!l2tp_reply) {
624                 dbus_message_unref(message);
625                 return -ENOMEM;
626         }
627
628         l2tp_reply->provider = provider;
629         l2tp_reply->callback = callback;
630         l2tp_reply->user_data = user_data;
631
632         err = connman_agent_queue_message(provider, message,
633                         connman_timeout_input_request(),
634                         request_input_reply, l2tp_reply, agent);
635         if (err < 0 && err != -EBUSY) {
636                 DBG("error %d sending agent request", err);
637                 dbus_message_unref(message);
638                 g_free(l2tp_reply);
639                 return err;
640         }
641
642         dbus_message_unref(message);
643
644         return -EINPROGRESS;
645 }
646
647 static int run_connect(struct vpn_provider *provider,
648                         struct connman_task *task, const char *if_name,
649                         vpn_provider_connect_cb_t cb, void *user_data,
650                         const char *username, const char *password)
651 {
652         char *l2tp_name, *pppd_name;
653         int l2tp_fd, pppd_fd;
654         int err;
655
656         if (!username || !*username || !password || !*password) {
657                 DBG("Cannot connect username %s password %p",
658                                                 username, password);
659                 err = -EINVAL;
660                 goto done;
661         }
662
663         DBG("username %s password %p", username, password);
664
665         l2tp_name = g_strdup_printf(VPN_STATEDIR "/connman-xl2tpd.conf");
666
667         l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
668         if (l2tp_fd < 0) {
669                 g_free(l2tp_name);
670                 connman_error("Error writing l2tp config");
671                 err = -EIO;
672                 goto done;
673         }
674
675         pppd_name = g_strdup_printf(VPN_STATEDIR "/connman-ppp-option.conf");
676
677         pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
678         if (pppd_fd < 0) {
679                 connman_error("Error writing pppd config");
680                 g_free(l2tp_name);
681                 g_free(pppd_name);
682                 close(l2tp_fd);
683                 err = -EIO;
684                 goto done;
685         }
686
687         l2tp_write_config(provider, pppd_name, l2tp_fd);
688
689         write_pppd_option(provider, pppd_fd);
690
691         connman_task_add_argument(task, "-D", NULL);
692         connman_task_add_argument(task, "-c", l2tp_name);
693
694         g_free(l2tp_name);
695         g_free(pppd_name);
696         close(l2tp_fd);
697         close(pppd_fd);
698
699         err = connman_task_run(task, l2tp_died, provider,
700                                 NULL, NULL, NULL);
701         if (err < 0) {
702                 connman_error("l2tp failed to start");
703                 err = -EIO;
704                 goto done;
705         }
706
707 done:
708         if (cb)
709                 cb(provider, user_data, err);
710
711         return err;
712 }
713
714 static void free_private_data(struct l2tp_private_data *data)
715 {
716         g_free(data->if_name);
717         g_free(data);
718 }
719
720 static void request_input_cb(struct vpn_provider *provider,
721                         const char *username,
722                         const char *password,
723                         const char *error, void *user_data)
724 {
725         struct l2tp_private_data *data = user_data;
726
727         if (!username || !*username || !password || !*password)
728                 DBG("Requesting username %s or password failed, error %s",
729                         username, error);
730         else if (error)
731                 DBG("error %s", error);
732
733         vpn_provider_set_string(provider, "L2TP.User", username);
734         vpn_provider_set_string_hide_value(provider, "L2TP.Password",
735                                                                 password);
736
737         run_connect(provider, data->task, data->if_name, data->cb,
738                 data->user_data, username, password);
739
740         free_private_data(data);
741 }
742
743 static int l2tp_connect(struct vpn_provider *provider,
744                         struct connman_task *task, const char *if_name,
745                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
746                         void *user_data)
747 {
748         const char *username, *password;
749         int err;
750
751         if (connman_task_set_notify(task, "getsec",
752                                         l2tp_get_sec, provider) != 0) {
753                 err = -ENOMEM;
754                 goto error;
755         }
756
757         username = vpn_provider_get_string(provider, "L2TP.User");
758         password = vpn_provider_get_string(provider, "L2TP.Password");
759
760         DBG("user %s password %p", username, password);
761
762         if (!username || !*username || !password || !*password) {
763                 struct l2tp_private_data *data;
764
765                 data = g_try_new0(struct l2tp_private_data, 1);
766                 if (!data)
767                         return -ENOMEM;
768
769                 data->task = task;
770                 data->if_name = g_strdup(if_name);
771                 data->cb = cb;
772                 data->user_data = user_data;
773
774                 err = request_input(provider, request_input_cb, dbus_sender,
775                                                                         data);
776                 if (err != -EINPROGRESS) {
777                         free_private_data(data);
778                         goto done;
779                 }
780                 return err;
781         }
782
783 done:
784         return run_connect(provider, task, if_name, cb, user_data,
785                                                         username, password);
786
787 error:
788         if (cb)
789                 cb(provider, user_data, err);
790
791         return err;
792 }
793
794 static int l2tp_error_code(struct vpn_provider *provider, int exit_code)
795 {
796         switch (exit_code) {
797         case 1:
798                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
799         default:
800                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
801         }
802 }
803
804 static void l2tp_disconnect(struct vpn_provider *provider)
805 {
806         if (!provider)
807                 return;
808
809         vpn_provider_set_string_hide_value(provider, "L2TP.Password", NULL);
810
811         connman_agent_cancel(provider);
812 }
813
814 static struct vpn_driver vpn_driver = {
815         .flags          = VPN_FLAG_NO_TUN,
816         .notify         = l2tp_notify,
817         .connect        = l2tp_connect,
818         .error_code     = l2tp_error_code,
819         .save           = l2tp_save,
820         .disconnect     = l2tp_disconnect,
821 };
822
823 static int l2tp_init(void)
824 {
825         connection = connman_dbus_get_connection();
826
827         return vpn_register("l2tp", &vpn_driver, L2TP);
828 }
829
830 static void l2tp_exit(void)
831 {
832         vpn_unregister("l2tp");
833
834         dbus_connection_unref(connection);
835 }
836
837 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
838         CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)