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