Merge tag 'upstream/1.40' into tizen.
[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         connman_ipaddress_set_p2p(ipaddress, true);
250
251         vpn_provider_set_ipaddress(provider, ipaddress);
252         vpn_provider_set_nameservers(provider, nameservers);
253
254         g_free(addressv4);
255         g_free(netmask);
256         g_free(gateway);
257         g_free(nameservers);
258         connman_ipaddress_free(ipaddress);
259
260         return VPN_STATE_CONNECT;
261 }
262
263 static int l2tp_save(struct vpn_provider *provider, GKeyFile *keyfile)
264 {
265         const char *option;
266         bool l2tp_option, pppd_option;
267         int i;
268
269         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
270                 l2tp_option = pppd_option = false;
271
272                 if (strncmp(pppd_options[i].cm_opt, "L2TP.", 5) == 0)
273                         l2tp_option = true;
274
275                 if (strncmp(pppd_options[i].cm_opt, "PPPD.", 5) == 0)
276                         pppd_option = true;
277
278                 if (l2tp_option || pppd_option) {
279                         option = vpn_provider_get_string(provider,
280                                                 pppd_options[i].cm_opt);
281                         if (!option) {
282                                 /*
283                                  * Check if the option prefix is L2TP as the
284                                  * PPPD options were using L2TP prefix earlier.
285                                  */
286                                 char *l2tp_str;
287
288                                 if (!pppd_option)
289                                         continue;
290
291                                 l2tp_str = g_strdup_printf("L2TP.%s",
292                                                 &pppd_options[i].cm_opt[5]);
293                                 option = vpn_provider_get_string(provider,
294                                                                 l2tp_str);
295                                 g_free(l2tp_str);
296
297                                 if (!option)
298                                         continue;
299                         }
300
301                         g_key_file_set_string(keyfile,
302                                         vpn_provider_get_save_group(provider),
303                                         pppd_options[i].cm_opt, option);
304                 }
305         }
306
307         return 0;
308 }
309
310 static ssize_t full_write(int fd, const void *buf, size_t len)
311 {
312         ssize_t byte_write;
313
314         while (len) {
315                 byte_write = write(fd, buf, len);
316                 if (byte_write < 0) {
317                         connman_error("failed to write config to l2tp: %s\n",
318                                         strerror(errno));
319                         return byte_write;
320                 }
321                 len -= byte_write;
322                 buf += byte_write;
323         }
324
325         return 0;
326 }
327
328 static ssize_t l2tp_write_bool_option(int fd,
329                                         const char *key, const char *value)
330 {
331         gchar *buf;
332         ssize_t ret = 0;
333
334         if (key && value) {
335                 if (strcasecmp(value, "yes") == 0 ||
336                                 strcasecmp(value, "true") == 0 ||
337                                 strcmp(value, "1") == 0) {
338                         buf = g_strdup_printf("%s\n", key);
339                         ret = full_write(fd, buf, strlen(buf));
340
341                         g_free(buf);
342                 }
343         }
344
345         return ret;
346 }
347
348 static int l2tp_write_option(int fd, const char *key, const char *value)
349 {
350         gchar *buf;
351         ssize_t ret = 0;
352
353         if (key) {
354                 if (value)
355                         buf = g_strdup_printf("%s %s\n", key, value);
356                 else
357                         buf = g_strdup_printf("%s\n", key);
358
359                 ret = full_write(fd, buf, strlen(buf));
360
361                 g_free(buf);
362         }
363
364         return ret;
365 }
366
367 static int l2tp_write_section(int fd, const char *key, const char *value)
368 {
369         gchar *buf;
370         ssize_t ret = 0;
371
372         if (key && value) {
373                 buf = g_strdup_printf("%s = %s\n", key, value);
374                 ret = full_write(fd, buf, strlen(buf));
375
376                 g_free(buf);
377         }
378
379         return ret;
380 }
381
382 static int write_pppd_option(struct vpn_provider *provider, int fd)
383 {
384         int i;
385         const char *opt_s;
386
387         l2tp_write_option(fd, "nodetach", NULL);
388         l2tp_write_option(fd, "lock", NULL);
389         l2tp_write_option(fd, "logfd", "2");
390         l2tp_write_option(fd, "usepeerdns", NULL);
391         l2tp_write_option(fd, "noipdefault", NULL);
392         l2tp_write_option(fd, "noauth", NULL);
393         l2tp_write_option(fd, "nodefaultroute", NULL);
394         l2tp_write_option(fd, "ipparam", "l2tp_plugin");
395
396         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
397                 if (pppd_options[i].sub != OPT_ALL &&
398                         pppd_options[i].sub != OPT_PPPD)
399                         continue;
400
401                 opt_s = vpn_provider_get_string(provider,
402                                         pppd_options[i].cm_opt);
403                 if (!opt_s)
404                         opt_s = pppd_options[i].vpn_default;
405
406                 if (!opt_s)
407                         continue;
408
409                 if (pppd_options[i].type == OPT_STRING)
410                         l2tp_write_option(fd,
411                                 pppd_options[i].pppd_opt, opt_s);
412                 else if (pppd_options[i].type == OPT_BOOL)
413                         l2tp_write_bool_option(fd,
414                                 pppd_options[i].pppd_opt, opt_s);
415         }
416
417         l2tp_write_option(fd, "plugin",
418                                 SCRIPTDIR "/libppp-plugin.so");
419
420         return 0;
421 }
422
423
424 static int l2tp_write_fields(struct vpn_provider *provider,
425                                                 int fd, int sub)
426 {
427         int i;
428         const char *opt_s;
429
430         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
431                 if (pppd_options[i].sub != sub)
432                         continue;
433
434                 opt_s = vpn_provider_get_string(provider,
435                                         pppd_options[i].cm_opt);
436                 if (!opt_s)
437                         opt_s = pppd_options[i].vpn_default;
438
439                 if (!opt_s)
440                         continue;
441
442                 if (pppd_options[i].type == OPT_STRING)
443                         l2tp_write_section(fd,
444                                 pppd_options[i].pppd_opt, opt_s);
445                 else if (pppd_options[i].type == OPT_BOOL)
446                         l2tp_write_bool_option(fd,
447                                 pppd_options[i].pppd_opt, opt_s);
448         }
449
450         return 0;
451 }
452
453 static int l2tp_write_config(struct vpn_provider *provider,
454                                         const char *pppd_name, int fd)
455 {
456         const char *option;
457
458         l2tp_write_option(fd, "[global]", NULL);
459         l2tp_write_fields(provider, fd, OPT_L2G);
460
461         l2tp_write_option(fd, "[lns default]", NULL);
462         l2tp_write_fields(provider, fd, OPT_L2LNS);
463
464         l2tp_write_option(fd, "[lac l2tp]", NULL);
465
466         option = vpn_provider_get_string(provider, "Host");
467         l2tp_write_option(fd, "lns =", option);
468
469         l2tp_write_fields(provider, fd, OPT_ALL);
470         l2tp_write_fields(provider, fd, OPT_L2);
471
472         l2tp_write_option(fd, "pppoptfile =", pppd_name);
473
474         return 0;
475 }
476
477 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
478 {
479         char *conf_file;
480
481         vpn_died(task, exit_code, user_data);
482
483         conf_file = g_strdup_printf(VPN_STATEDIR "/connman-xl2tpd.conf");
484         unlink(conf_file);
485         g_free(conf_file);
486
487         conf_file = g_strdup_printf(VPN_STATEDIR "/connman-ppp-option.conf");
488         unlink(conf_file);
489         g_free(conf_file);
490 }
491
492 struct request_input_reply {
493         struct vpn_provider *provider;
494         vpn_provider_password_cb_t callback;
495         void *user_data;
496 };
497
498 static void request_input_reply(DBusMessage *reply, void *user_data)
499 {
500         struct request_input_reply *l2tp_reply = user_data;
501         struct l2tp_private_data *data;
502         const char *error = NULL;
503         char *username = NULL, *password = NULL;
504         char *key;
505         DBusMessageIter iter, dict;
506         int err;
507
508         DBG("provider %p", l2tp_reply->provider);
509
510         if (!reply)
511                 goto done;
512
513         data = l2tp_reply->user_data;
514
515         err = vpn_agent_check_and_process_reply_error(reply,
516                                 l2tp_reply->provider, data->task, data->cb,
517                                 data->user_data);
518         if (err) {
519                 /* Ensure cb is called only once */
520                 data->cb = NULL;
521                 data->user_data = NULL;
522                 error = dbus_message_get_error_name(reply);
523                 goto done;
524         }
525
526         if (!vpn_agent_check_reply_has_dict(reply))
527                 goto done;
528
529         dbus_message_iter_init(reply, &iter);
530         dbus_message_iter_recurse(&iter, &dict);
531         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
532                 DBusMessageIter entry, value;
533                 const char *str;
534
535                 dbus_message_iter_recurse(&dict, &entry);
536                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
537                         break;
538
539                 dbus_message_iter_get_basic(&entry, &key);
540
541                 if (g_str_equal(key, "Username")) {
542                         dbus_message_iter_next(&entry);
543                         if (dbus_message_iter_get_arg_type(&entry)
544                                                         != DBUS_TYPE_VARIANT)
545                                 break;
546                         dbus_message_iter_recurse(&entry, &value);
547                         if (dbus_message_iter_get_arg_type(&value)
548                                                         != DBUS_TYPE_STRING)
549                                 break;
550                         dbus_message_iter_get_basic(&value, &str);
551                         username = g_strdup(str);
552                 }
553
554                 if (g_str_equal(key, "Password")) {
555                         dbus_message_iter_next(&entry);
556                         if (dbus_message_iter_get_arg_type(&entry)
557                                                         != DBUS_TYPE_VARIANT)
558                                 break;
559                         dbus_message_iter_recurse(&entry, &value);
560                         if (dbus_message_iter_get_arg_type(&value)
561                                                         != DBUS_TYPE_STRING)
562                                 break;
563                         dbus_message_iter_get_basic(&value, &str);
564                         password = g_strdup(str);
565                 }
566
567                 dbus_message_iter_next(&dict);
568         }
569
570 done:
571         l2tp_reply->callback(l2tp_reply->provider, username, password, error,
572                                 l2tp_reply->user_data);
573
574         g_free(username);
575         g_free(password);
576
577         g_free(l2tp_reply);
578 }
579
580 typedef void (* request_cb_t)(struct vpn_provider *provider,
581                                 const char *username, const char *password,
582                                 const char *error, void *user_data);
583
584 static int request_input(struct vpn_provider *provider,
585                         request_cb_t callback, const char *dbus_sender,
586                         void *user_data)
587 {
588         DBusMessage *message;
589         const char *path, *agent_sender, *agent_path;
590         DBusMessageIter iter;
591         DBusMessageIter dict;
592         struct request_input_reply *l2tp_reply;
593         int err;
594         void *agent;
595
596         agent = connman_agent_get_info(dbus_sender, &agent_sender,
597                                                         &agent_path);
598         if (!provider || !agent || !agent_path || !callback)
599                 return -ESRCH;
600
601         message = dbus_message_new_method_call(agent_sender, agent_path,
602                                         VPN_AGENT_INTERFACE,
603                                         "RequestInput");
604         if (!message)
605                 return -ENOMEM;
606
607         dbus_message_iter_init_append(message, &iter);
608
609         path = vpn_provider_get_path(provider);
610         dbus_message_iter_append_basic(&iter,
611                                 DBUS_TYPE_OBJECT_PATH, &path);
612
613         connman_dbus_dict_open(&iter, &dict);
614
615         if (vpn_provider_get_authentication_errors(provider))
616                 vpn_agent_append_auth_failure(&dict, provider, NULL);
617
618         vpn_agent_append_user_info(&dict, provider, "L2TP.User");
619
620         vpn_agent_append_host_and_name(&dict, provider);
621
622         connman_dbus_dict_close(&iter, &dict);
623
624         l2tp_reply = g_try_new0(struct request_input_reply, 1);
625         if (!l2tp_reply) {
626                 dbus_message_unref(message);
627                 return -ENOMEM;
628         }
629
630         l2tp_reply->provider = provider;
631         l2tp_reply->callback = callback;
632         l2tp_reply->user_data = user_data;
633
634         err = connman_agent_queue_message(provider, message,
635                         connman_timeout_input_request(),
636                         request_input_reply, l2tp_reply, agent);
637         if (err < 0 && err != -EBUSY) {
638                 DBG("error %d sending agent request", err);
639                 dbus_message_unref(message);
640                 g_free(l2tp_reply);
641                 return err;
642         }
643
644         dbus_message_unref(message);
645
646         return -EINPROGRESS;
647 }
648
649 static int run_connect(struct vpn_provider *provider,
650                         struct connman_task *task, const char *if_name,
651                         vpn_provider_connect_cb_t cb, void *user_data,
652                         const char *username, const char *password)
653 {
654         char *l2tp_name, *pppd_name;
655         int l2tp_fd, pppd_fd;
656         int err;
657
658         if (!username || !*username || !password || !*password) {
659                 DBG("Cannot connect username %s password %p",
660                                                 username, password);
661                 err = -EINVAL;
662                 goto done;
663         }
664
665         DBG("username %s password %p", username, password);
666
667         l2tp_name = g_strdup_printf(VPN_STATEDIR "/connman-xl2tpd.conf");
668
669         l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
670         if (l2tp_fd < 0) {
671                 g_free(l2tp_name);
672                 connman_error("Error writing l2tp config");
673                 err = -EIO;
674                 goto done;
675         }
676
677         pppd_name = g_strdup_printf(VPN_STATEDIR "/connman-ppp-option.conf");
678
679         pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
680         if (pppd_fd < 0) {
681                 connman_error("Error writing pppd config");
682                 g_free(l2tp_name);
683                 g_free(pppd_name);
684                 close(l2tp_fd);
685                 err = -EIO;
686                 goto done;
687         }
688
689         l2tp_write_config(provider, pppd_name, l2tp_fd);
690
691         write_pppd_option(provider, pppd_fd);
692
693         connman_task_add_argument(task, "-D", NULL);
694         connman_task_add_argument(task, "-c", l2tp_name);
695
696         g_free(l2tp_name);
697         g_free(pppd_name);
698         close(l2tp_fd);
699         close(pppd_fd);
700
701         err = connman_task_run(task, l2tp_died, provider,
702                                 NULL, NULL, NULL);
703         if (err < 0) {
704                 connman_error("l2tp failed to start");
705                 err = -EIO;
706                 goto done;
707         }
708
709 done:
710         if (cb)
711                 cb(provider, user_data, err);
712
713         return err;
714 }
715
716 static void free_private_data(struct l2tp_private_data *data)
717 {
718         g_free(data->if_name);
719         g_free(data);
720 }
721
722 static void request_input_cb(struct vpn_provider *provider,
723                         const char *username,
724                         const char *password,
725                         const char *error, void *user_data)
726 {
727         struct l2tp_private_data *data = user_data;
728
729         if (!username || !*username || !password || !*password)
730                 DBG("Requesting username %s or password failed, error %s",
731                         username, error);
732         else if (error)
733                 DBG("error %s", error);
734
735         vpn_provider_set_string(provider, "L2TP.User", username);
736         vpn_provider_set_string_hide_value(provider, "L2TP.Password",
737                                                                 password);
738
739         run_connect(provider, data->task, data->if_name, data->cb,
740                 data->user_data, username, password);
741
742         free_private_data(data);
743 }
744
745 static int l2tp_connect(struct vpn_provider *provider,
746                         struct connman_task *task, const char *if_name,
747                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
748                         void *user_data)
749 {
750         const char *username, *password;
751         int err;
752
753         if (connman_task_set_notify(task, "getsec",
754                                         l2tp_get_sec, provider) != 0) {
755                 err = -ENOMEM;
756                 goto error;
757         }
758
759         username = vpn_provider_get_string(provider, "L2TP.User");
760         password = vpn_provider_get_string(provider, "L2TP.Password");
761
762         DBG("user %s password %p", username, password);
763
764         if (!username || !*username || !password || !*password) {
765                 struct l2tp_private_data *data;
766
767                 data = g_try_new0(struct l2tp_private_data, 1);
768                 if (!data)
769                         return -ENOMEM;
770
771                 data->task = task;
772                 data->if_name = g_strdup(if_name);
773                 data->cb = cb;
774                 data->user_data = user_data;
775
776                 err = request_input(provider, request_input_cb, dbus_sender,
777                                                                         data);
778                 if (err != -EINPROGRESS) {
779                         free_private_data(data);
780                         goto done;
781                 }
782                 return err;
783         }
784
785 done:
786         return run_connect(provider, task, if_name, cb, user_data,
787                                                         username, password);
788
789 error:
790         if (cb)
791                 cb(provider, user_data, err);
792
793         return err;
794 }
795
796 static int l2tp_error_code(struct vpn_provider *provider, int exit_code)
797 {
798         switch (exit_code) {
799         case 1:
800                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
801         default:
802                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
803         }
804 }
805
806 static void l2tp_disconnect(struct vpn_provider *provider)
807 {
808         if (!provider)
809                 return;
810
811         vpn_provider_set_string_hide_value(provider, "L2TP.Password", NULL);
812
813         connman_agent_cancel(provider);
814 }
815
816 static struct vpn_driver vpn_driver = {
817         .flags          = VPN_FLAG_NO_TUN,
818         .notify         = l2tp_notify,
819         .connect        = l2tp_connect,
820         .error_code     = l2tp_error_code,
821         .save           = l2tp_save,
822         .disconnect     = l2tp_disconnect,
823 };
824
825 static int l2tp_init(void)
826 {
827         connection = connman_dbus_get_connection();
828
829         return vpn_register("l2tp", &vpn_driver, L2TP);
830 }
831
832 static void l2tp_exit(void)
833 {
834         vpn_unregister("l2tp");
835
836         dbus_connection_unref(connection);
837 }
838
839 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
840         CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)