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