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