l2tp: Remember the connection 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
48 #include "vpn.h"
49
50 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
51
52 enum {
53         OPT_STRING = 1,
54         OPT_BOOL = 2,
55 };
56
57 enum {
58         OPT_ALL = 1,
59         OPT_L2G = 2,
60         OPT_L2  = 3,
61         OPT_PPPD = 4,
62 };
63
64 struct {
65         const char *cm_opt;
66         const char *pppd_opt;
67         int sub;
68         const char *vpn_default;
69         int type;
70 } pppd_options[] = {
71         { "L2TP.User", "name", OPT_ALL, NULL, OPT_STRING },
72         { "L2TP.BPS", "bps", OPT_L2, NULL, OPT_STRING },
73         { "L2TP.LengthBit", "length bit", OPT_L2, NULL, OPT_STRING },
74         { "L2TP.Challenge", "challenge", OPT_L2, NULL, OPT_STRING },
75         { "L2TP.DefaultRoute", "defaultroute", OPT_L2, NULL, OPT_STRING },
76         { "L2TP.FlowBit", "flow bit", OPT_L2, NULL, OPT_STRING },
77         { "L2TP.TunnelRWS", "tunnel rws", OPT_L2, NULL, OPT_STRING },
78         { "L2TP.Exclusive", "exclusive", OPT_L2, NULL, OPT_STRING },
79         { "L2TP.Autodial", "autodial", OPT_L2, "yes", OPT_STRING },
80         { "L2TP.Redial", "redial", OPT_L2, "yes", OPT_STRING },
81         { "L2TP.RedialTimeout", "redial timeout", OPT_L2, "10", OPT_STRING },
82         { "L2TP.MaxRedials", "max redials", OPT_L2, NULL, OPT_STRING },
83         { "L2TP.RequirePAP", "require pap", OPT_L2, "no", OPT_STRING },
84         { "L2TP.RequireCHAP", "require chap", OPT_L2, "yes", OPT_STRING },
85         { "L2TP.ReqAuth", "require authentication", OPT_L2, "no", OPT_STRING },
86         { "L2TP.AccessControl", "access control", OPT_L2G, "yes", OPT_STRING },
87         { "L2TP.AuthFile", "auth file", OPT_L2G, NULL, OPT_STRING },
88         { "L2TP.ForceUserSpace", "force userspace", OPT_L2G, NULL, OPT_STRING },
89         { "L2TP.ListenAddr", "listen-addr", OPT_L2G, NULL, OPT_STRING },
90         { "L2TP.Rand Source", "rand source", OPT_L2G, NULL, OPT_STRING },
91         { "L2TP.IPsecSaref", "ipsec saref", OPT_L2G, NULL, OPT_STRING },
92         { "L2TP.Port", "port", OPT_L2G, NULL, OPT_STRING },
93         { "L2TP.EchoFailure", "lcp-echo-failure", OPT_PPPD, "0", OPT_STRING },
94         { "L2TP.EchoInterval", "lcp-echo-interval", OPT_PPPD, "0", OPT_STRING },
95         { "L2TP.Debug", "debug", OPT_PPPD, NULL, OPT_STRING },
96         { "L2TP.RefuseEAP", "refuse-eap", OPT_PPPD, NULL, OPT_BOOL },
97         { "L2TP.RefusePAP", "refuse-pap", OPT_PPPD, NULL, OPT_BOOL },
98         { "L2TP.RefuseCHAP", "refuse-chap", OPT_PPPD, NULL, OPT_BOOL },
99         { "L2TP.RefuseMSCHAP", "refuse-mschap", OPT_PPPD, NULL, OPT_BOOL },
100         { "L2TP.RefuseMSCHAP2", "refuse-mschapv2", OPT_PPPD, NULL, OPT_BOOL },
101         { "L2TP.NoBSDComp", "nobsdcomp", OPT_PPPD, NULL, OPT_BOOL },
102         { "L2TP.NoPcomp", "nopcomp", OPT_PPPD, NULL, OPT_BOOL },
103         { "L2TP.UseAccomp", "accomp", OPT_PPPD, NULL, OPT_BOOL },
104         { "L2TP.NoDeflate", "nodeflatey", OPT_PPPD, NULL, OPT_BOOL },
105         { "L2TP.ReqMPPE", "require-mppe", OPT_PPPD, NULL, OPT_BOOL },
106         { "L2TP.ReqMPPE40", "require-mppe-40", OPT_PPPD, NULL, OPT_BOOL },
107         { "L2TP.ReqMPPE128", "require-mppe-128", OPT_PPPD, NULL, OPT_BOOL },
108         { "L2TP.ReqMPPEStateful", "mppe-stateful", OPT_PPPD, NULL, OPT_BOOL },
109         { "L2TP.NoVJ", "no-vj-comp", OPT_PPPD, NULL, OPT_BOOL },
110 };
111
112 static DBusConnection *connection;
113
114 static DBusMessage *l2tp_get_sec(struct connman_task *task,
115                         DBusMessage *msg, void *user_data)
116 {
117         const char *user, *passwd;
118         struct vpn_provider *provider = user_data;
119
120         if (dbus_message_get_no_reply(msg) == FALSE) {
121                 DBusMessage *reply;
122
123                 user = vpn_provider_get_string(provider, "L2TP.User");
124                 passwd = vpn_provider_get_string(provider, "L2TP.Password");
125
126                 if (user == NULL || strlen(user) == 0 ||
127                                 passwd == NULL || strlen(passwd) == 0)
128                         return NULL;
129
130                 reply = dbus_message_new_method_return(msg);
131                 if (reply == NULL)
132                         return NULL;
133
134                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
135                                                 DBUS_TYPE_STRING, &passwd,
136                                                 DBUS_TYPE_INVALID);
137
138                 return reply;
139         }
140
141         return NULL;
142 }
143
144 static int l2tp_notify(DBusMessage *msg, struct vpn_provider *provider)
145 {
146         DBusMessageIter iter, dict;
147         const char *reason, *key, *value;
148         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
149         char *ifname = NULL, *nameservers = NULL;
150         struct connman_ipaddress *ipaddress = NULL;
151
152         dbus_message_iter_init(msg, &iter);
153
154         dbus_message_iter_get_basic(&iter, &reason);
155         dbus_message_iter_next(&iter);
156
157         if (!provider) {
158                 connman_error("No provider found");
159                 return VPN_STATE_FAILURE;
160         }
161
162         if (strcmp(reason, "auth failed") == 0)
163                 return VPN_STATE_AUTH_FAILURE;
164
165         if (strcmp(reason, "connect"))
166                 return VPN_STATE_DISCONNECT;
167
168         dbus_message_iter_recurse(&iter, &dict);
169
170         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
171                 DBusMessageIter entry;
172
173                 dbus_message_iter_recurse(&dict, &entry);
174                 dbus_message_iter_get_basic(&entry, &key);
175                 dbus_message_iter_next(&entry);
176                 dbus_message_iter_get_basic(&entry, &value);
177
178                 DBG("%s = %s", key, value);
179
180                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
181                         vpn_provider_set_string(provider, "Address", value);
182                         addressv4 = g_strdup(value);
183                 }
184
185                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
186                         vpn_provider_set_string(provider, "Netmask", value);
187                         netmask = g_strdup(value);
188                 }
189
190                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
191                         vpn_provider_set_string(provider, "DNS", value);
192                         nameservers = g_strdup(value);
193                 }
194
195                 if (!strcmp(key, "INTERNAL_IFNAME"))
196                         ifname = g_strdup(value);
197
198                 dbus_message_iter_next(&dict);
199         }
200
201         if (vpn_set_ifname(provider, ifname) < 0) {
202                 g_free(ifname);
203                 g_free(addressv4);
204                 g_free(netmask);
205                 g_free(nameservers);
206                 return VPN_STATE_FAILURE;
207         }
208
209         if (addressv4 != NULL)
210                 ipaddress = connman_ipaddress_alloc(AF_INET);
211
212         g_free(ifname);
213
214         if (ipaddress == NULL) {
215                 connman_error("No IP address for provider");
216                 g_free(addressv4);
217                 g_free(netmask);
218                 g_free(nameservers);
219                 return VPN_STATE_FAILURE;
220         }
221
222         value = vpn_provider_get_string(provider, "HostIP");
223         if (value != NULL) {
224                 vpn_provider_set_string(provider, "Gateway", value);
225                 gateway = g_strdup(value);
226         }
227
228         if (addressv4 != NULL)
229                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
230                                         gateway);
231
232         vpn_provider_set_ipaddress(provider, ipaddress);
233         vpn_provider_set_nameservers(provider, nameservers);
234
235         g_free(addressv4);
236         g_free(netmask);
237         g_free(gateway);
238         g_free(nameservers);
239         connman_ipaddress_free(ipaddress);
240
241         return VPN_STATE_CONNECT;
242 }
243
244 static int l2tp_save(struct vpn_provider *provider, GKeyFile *keyfile)
245 {
246         const char *option;
247         int i;
248
249         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
250                 if (strncmp(pppd_options[i].cm_opt, "L2TP.", 5) == 0) {
251                         option = vpn_provider_get_string(provider,
252                                                         pppd_options[i].cm_opt);
253                         if (option == NULL)
254                                 continue;
255
256                         g_key_file_set_string(keyfile,
257                                         vpn_provider_get_save_group(provider),
258                                         pppd_options[i].cm_opt, option);
259                 }
260         }
261
262         option = vpn_provider_get_string(provider, "L2TP.Password");
263         if (option != NULL)
264                 g_key_file_set_string(keyfile,
265                                         vpn_provider_get_save_group(provider),
266                                         "L2TP.Password", option);
267
268         return 0;
269 }
270
271 static ssize_t full_write(int fd, const void *buf, size_t len)
272 {
273         ssize_t byte_write;
274
275         while (len) {
276                 byte_write = write(fd, buf, len);
277                 if (byte_write < 0) {
278                         connman_error("failed to write config to l2tp: %s\n",
279                                         strerror(errno));
280                         return byte_write;
281                 }
282                 len -= byte_write;
283                 buf += byte_write;
284         }
285
286         return 0;
287 }
288
289 static ssize_t l2tp_write_bool_option(int fd,
290                                         const char *key, const char *value)
291 {
292         gchar *buf;
293         ssize_t ret = 0;
294
295         if (key != NULL && value != NULL) {
296                 if (strcasecmp(value, "yes") == 0 ||
297                                 strcasecmp(value, "true") == 0 ||
298                                 strcmp(value, "1") == 0) {
299                         buf = g_strdup_printf("%s\n", key);
300                         ret = full_write(fd, buf, strlen(buf));
301
302                         g_free(buf);
303                 }
304         }
305
306         return ret;
307 }
308
309 static int l2tp_write_option(int fd, const char *key, const char *value)
310 {
311         gchar *buf;
312         ssize_t ret = 0;
313
314         if (key != NULL) {
315                 if (value != NULL)
316                         buf = g_strdup_printf("%s %s\n", key, value);
317                 else
318                         buf = g_strdup_printf("%s\n", key);
319
320                 ret = full_write(fd, buf, strlen(buf));
321
322                 g_free(buf);
323         }
324
325         return ret;
326 }
327
328 static int l2tp_write_section(int fd, const char *key, const char *value)
329 {
330         gchar *buf;
331         ssize_t ret = 0;
332
333         if (key != NULL && value != NULL) {
334                 buf = g_strdup_printf("%s = %s\n", key, value);
335                 ret = full_write(fd, buf, strlen(buf));
336
337                 g_free(buf);
338         }
339
340         return ret;
341 }
342
343 static int write_pppd_option(struct vpn_provider *provider, int fd)
344 {
345         int i;
346         const char *opt_s;
347
348         l2tp_write_option(fd, "nodetach", NULL);
349         l2tp_write_option(fd, "lock", NULL);
350         l2tp_write_option(fd, "usepeerdns", NULL);
351         l2tp_write_option(fd, "noipdefault", NULL);
352         l2tp_write_option(fd, "noauth", NULL);
353         l2tp_write_option(fd, "nodefaultroute", NULL);
354         l2tp_write_option(fd, "ipparam", "l2tp_plugin");
355
356         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
357                 if (pppd_options[i].sub != OPT_ALL &&
358                         pppd_options[i].sub != OPT_PPPD)
359                         continue;
360
361                 opt_s = vpn_provider_get_string(provider,
362                                         pppd_options[i].cm_opt);
363                 if (!opt_s)
364                         opt_s = pppd_options[i].vpn_default;
365
366                 if (!opt_s)
367                         continue;
368
369                 if (pppd_options[i].type == OPT_STRING)
370                         l2tp_write_option(fd,
371                                 pppd_options[i].pppd_opt, opt_s);
372                 else if (pppd_options[i].type == OPT_BOOL)
373                         l2tp_write_bool_option(fd,
374                                 pppd_options[i].pppd_opt, opt_s);
375         }
376
377         l2tp_write_option(fd, "plugin",
378                                 SCRIPTDIR "/libppp-plugin.so");
379
380         return 0;
381 }
382
383
384 static int l2tp_write_fields(struct vpn_provider *provider,
385                                                 int fd, int sub)
386 {
387         int i;
388         const char *opt_s;
389
390         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
391                 if (pppd_options[i].sub != sub)
392                         continue;
393
394                 opt_s = vpn_provider_get_string(provider,
395                                         pppd_options[i].cm_opt);
396                 if (!opt_s)
397                         opt_s = pppd_options[i].vpn_default;
398
399                 if (!opt_s)
400                         continue;
401
402                 if (pppd_options[i].type == OPT_STRING)
403                         l2tp_write_section(fd,
404                                 pppd_options[i].pppd_opt, opt_s);
405                 else if (pppd_options[i].type == OPT_BOOL)
406                         l2tp_write_bool_option(fd,
407                                 pppd_options[i].pppd_opt, opt_s);
408         }
409
410         return 0;
411 }
412
413 static int l2tp_write_config(struct vpn_provider *provider,
414                                         const char *pppd_name, int fd)
415 {
416         const char *option;
417
418         l2tp_write_option(fd, "[global]", NULL);
419         l2tp_write_fields(provider, fd, OPT_L2G);
420
421         l2tp_write_option(fd, "[lac l2tp]", NULL);
422
423         option = vpn_provider_get_string(provider, "Host");
424         l2tp_write_option(fd, "lns =", option);
425
426         l2tp_write_fields(provider, fd, OPT_ALL);
427         l2tp_write_fields(provider, fd, OPT_L2);
428
429         l2tp_write_option(fd, "pppoptfile =", pppd_name);
430
431         return 0;
432 }
433
434 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
435 {
436         char *conf_file;
437
438         vpn_died(task, exit_code, user_data);
439
440         conf_file = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
441         unlink(conf_file);
442         g_free(conf_file);
443
444         conf_file = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
445         unlink(conf_file);
446         g_free(conf_file);
447 }
448
449 static int l2tp_connect(struct vpn_provider *provider,
450                 struct connman_task *task, const char *if_name)
451 {
452         const char *host;
453         char *l2tp_name, *pppd_name;
454         int l2tp_fd, pppd_fd;
455         int err;
456
457         if (connman_task_set_notify(task, "getsec",
458                                         l2tp_get_sec, provider))
459                 return -ENOMEM;
460
461         host = vpn_provider_get_string(provider, "Host");
462         if (host == NULL) {
463                 connman_error("Host not set; cannot enable VPN");
464                 return -EINVAL;
465         }
466
467         l2tp_name = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
468
469         l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
470         if (l2tp_fd < 0) {
471                 g_free(l2tp_name);
472                 connman_error("Error writing l2tp config");
473                 return -EIO;
474         }
475
476         pppd_name = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
477
478         pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
479         if (pppd_fd < 0) {
480                 connman_error("Error writing pppd config");
481                 g_free(l2tp_name);
482                 g_free(pppd_name);
483                 close(l2tp_fd);
484                 return -EIO;
485         }
486
487         l2tp_write_config(provider, pppd_name, l2tp_fd);
488
489         write_pppd_option(provider, pppd_fd);
490
491         connman_task_add_argument(task, "-D", NULL);
492         connman_task_add_argument(task, "-c", l2tp_name);
493
494         g_free(l2tp_name);
495         g_free(pppd_name);
496
497         err = connman_task_run(task, l2tp_died, provider,
498                                 NULL, NULL, NULL);
499         if (err < 0) {
500                 connman_error("l2tp failed to start");
501                 return -EIO;
502         }
503
504         return 0;
505 }
506
507 static int l2tp_error_code(int exit_code)
508 {
509         switch (exit_code) {
510         case 1:
511                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
512         default:
513                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
514         }
515 }
516
517 static struct vpn_driver vpn_driver = {
518         .flags          = VPN_FLAG_NO_TUN,
519         .notify         = l2tp_notify,
520         .connect        = l2tp_connect,
521         .error_code     = l2tp_error_code,
522         .save           = l2tp_save,
523 };
524
525 static int l2tp_init(void)
526 {
527         connection = connman_dbus_get_connection();
528
529         return vpn_register("l2tp", &vpn_driver, L2TP);
530 }
531
532 static void l2tp_exit(void)
533 {
534         vpn_unregister("l2tp");
535
536         dbus_connection_unref(connection);
537 }
538
539 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
540         CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)