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