Imported Upstream version 1.40
[platform/upstream/connman.git] / vpn / plugins / pptp.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010,2013-2014  BMW Car IT GmbH.
6  *  Copyright (C) 2012-2013  Intel Corporation. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <net/if.h>
32
33 #include <dbus/dbus.h>
34 #include <glib.h>
35
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/provider.h>
39 #include <connman/log.h>
40 #include <connman/task.h>
41 #include <connman/dbus.h>
42 #include <connman/inet.h>
43 #include <connman/agent.h>
44 #include <connman/setting.h>
45 #include <connman/vpn-dbus.h>
46
47 #include "../vpn-provider.h"
48 #include "../vpn-agent.h"
49
50 #include "vpn.h"
51
52 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
53
54 enum {
55         OPT_STRING = 1,
56         OPT_BOOL = 2,
57         OPT_PPTP_ONLY = 3,
58 };
59
60 struct {
61         const char *cm_opt;
62         const char *pptp_opt;
63         const char *pptp_default;
64         int type;
65 } pptp_options[] = {
66         { "PPTP.User", "user", NULL, OPT_STRING },
67         { "PPTP.IdleWait", "--idle-wait", NULL, OPT_PPTP_ONLY},
68         { "PPTP.MaxEchoWait", "--max-echo-wait", NULL, OPT_PPTP_ONLY},
69         { "PPPD.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
70         { "PPPD.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
71         { "PPPD.Debug", "debug", NULL, OPT_STRING },
72         { "PPPD.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
73         { "PPPD.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
74         { "PPPD.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
75         { "PPPD.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
76         { "PPPD.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
77         { "PPPD.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
78         { "PPPD.NoDeflate", "nodeflate", NULL, OPT_BOOL },
79         { "PPPD.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
80         { "PPPD.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
81         { "PPPD.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
82         { "PPPD.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
83         { "PPPD.NoVJ", "novj", NULL, OPT_BOOL },
84 };
85
86 static DBusConnection *connection;
87
88 struct pptp_private_data {
89         struct connman_task *task;
90         char *if_name;
91         vpn_provider_connect_cb_t cb;
92         void *user_data;
93 };
94
95 static DBusMessage *pptp_get_sec(struct connman_task *task,
96                                 DBusMessage *msg, void *user_data)
97 {
98         const char *user, *passwd;
99         struct vpn_provider *provider = user_data;
100         DBusMessage *reply;
101
102         if (dbus_message_get_no_reply(msg))
103                 return NULL;
104
105         user = vpn_provider_get_string(provider, "PPTP.User");
106         passwd = vpn_provider_get_string(provider, "PPTP.Password");
107         if (!user || strlen(user) == 0 ||
108                                 !passwd || strlen(passwd) == 0)
109                 return NULL;
110
111         reply = dbus_message_new_method_return(msg);
112         if (!reply)
113                 return NULL;
114
115         dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
116                                 DBUS_TYPE_STRING, &passwd,
117                                 DBUS_TYPE_INVALID);
118         return reply;
119 }
120
121 static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
122 {
123         DBusMessageIter iter, dict;
124         const char *reason, *key, *value;
125         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
126         char *ifname = NULL, *nameservers = NULL;
127         struct connman_ipaddress *ipaddress = NULL;
128
129         dbus_message_iter_init(msg, &iter);
130
131         dbus_message_iter_get_basic(&iter, &reason);
132         dbus_message_iter_next(&iter);
133
134         if (!provider) {
135                 connman_error("No provider found");
136                 return VPN_STATE_FAILURE;
137         }
138
139         if (strcmp(reason, "auth failed") == 0) {
140                 DBG("authentication failure");
141
142                 vpn_provider_set_string(provider, "PPTP.User", NULL);
143                 vpn_provider_set_string_hide_value(provider, "PPTP.Password",
144                                         NULL);
145
146                 return VPN_STATE_AUTH_FAILURE;
147         }
148
149         if (strcmp(reason, "connect"))
150                 return VPN_STATE_DISCONNECT;
151
152         dbus_message_iter_recurse(&iter, &dict);
153
154         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
155                 DBusMessageIter entry;
156
157                 dbus_message_iter_recurse(&dict, &entry);
158                 dbus_message_iter_get_basic(&entry, &key);
159                 dbus_message_iter_next(&entry);
160                 dbus_message_iter_get_basic(&entry, &value);
161
162                 DBG("%s = %s", key, value);
163
164                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
165                         addressv4 = g_strdup(value);
166
167                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
168                         netmask = g_strdup(value);
169
170                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
171                         nameservers = g_strdup(value);
172
173                 if (!strcmp(key, "INTERNAL_IFNAME"))
174                         ifname = g_strdup(value);
175
176                 dbus_message_iter_next(&dict);
177         }
178
179         if (vpn_set_ifname(provider, ifname) < 0) {
180                 g_free(ifname);
181                 g_free(addressv4);
182                 g_free(netmask);
183                 g_free(nameservers);
184                 return VPN_STATE_FAILURE;
185         }
186
187         if (addressv4)
188                 ipaddress = connman_ipaddress_alloc(AF_INET);
189
190         g_free(ifname);
191
192         if (!ipaddress) {
193                 connman_error("No IP address for provider");
194                 g_free(addressv4);
195                 g_free(netmask);
196                 g_free(nameservers);
197                 return VPN_STATE_FAILURE;
198         }
199
200         value = vpn_provider_get_string(provider, "HostIP");
201         if (value) {
202                 vpn_provider_set_string(provider, "Gateway", value);
203                 gateway = g_strdup(value);
204         }
205
206         if (addressv4)
207                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
208                                         gateway);
209
210         connman_ipaddress_set_p2p(ipaddress, true);
211         vpn_provider_set_ipaddress(provider, ipaddress);
212         vpn_provider_set_nameservers(provider, nameservers);
213
214         g_free(addressv4);
215         g_free(netmask);
216         g_free(gateway);
217         g_free(nameservers);
218         connman_ipaddress_free(ipaddress);
219
220         return VPN_STATE_CONNECT;
221 }
222
223 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
224 {
225         const char *option;
226         bool pptp_option, pppd_option;
227         int i;
228
229         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
230                 pptp_option = pppd_option = false;
231
232                 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0)
233                         pptp_option = true;
234
235                 if (strncmp(pptp_options[i].cm_opt, "PPPD.", 5) == 0)
236                         pppd_option = true;
237
238                 if (pptp_option || pppd_option) {
239                         option = vpn_provider_get_string(provider,
240                                                         pptp_options[i].cm_opt);
241                         if (!option) {
242                                 /*
243                                  * Check if the option prefix is PPTP as the
244                                  * PPPD options were using PPTP prefix earlier.
245                                  */
246                                 char *pptp_str;
247
248                                 if (!pppd_option)
249                                         continue;
250
251                                 pptp_str = g_strdup_printf("PPTP.%s",
252                                                 &pptp_options[i].cm_opt[5]);
253                                 option = vpn_provider_get_string(provider,
254                                                                 pptp_str);
255                                 g_free(pptp_str);
256
257                                 if (!option)
258                                         continue;
259                         }
260
261                         g_key_file_set_string(keyfile,
262                                         vpn_provider_get_save_group(provider),
263                                         pptp_options[i].cm_opt, option);
264                 }
265         }
266
267         return 0;
268 }
269
270 static void pptp_write_bool_option(struct connman_task *task,
271                                 const char *key, const char *value)
272 {
273         if (key && value) {
274                 if (strcasecmp(value, "yes") == 0 ||
275                                 strcasecmp(value, "true") == 0 ||
276                                 strcmp(value, "1") == 0)
277                         connman_task_add_argument(task, key, NULL);
278         }
279 }
280
281 struct request_input_reply {
282         struct vpn_provider *provider;
283         vpn_provider_password_cb_t callback;
284         void *user_data;
285 };
286
287 static void request_input_reply(DBusMessage *reply, void *user_data)
288 {
289         struct request_input_reply *pptp_reply = user_data;
290         struct pptp_private_data *data;
291         const char *error = NULL;
292         char *username = NULL, *password = NULL;
293         char *key;
294         DBusMessageIter iter, dict;
295         int err;
296
297         DBG("provider %p", pptp_reply->provider);
298
299         if (!reply)
300                 goto done;
301
302         data = pptp_reply->user_data;
303
304         err = vpn_agent_check_and_process_reply_error(reply,
305                                 pptp_reply->provider, data->task, data->cb,
306                                 data->user_data);
307         if (err) {
308                 /* Ensure cb is called only once */
309                 data->cb = NULL;
310                 data->user_data = NULL;
311                 error = dbus_message_get_error_name(reply);
312                 goto done;
313         }
314
315         if (!vpn_agent_check_reply_has_dict(reply))
316                 goto done;
317
318         dbus_message_iter_init(reply, &iter);
319         dbus_message_iter_recurse(&iter, &dict);
320         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
321                 DBusMessageIter entry, value;
322                 const char *str;
323
324                 dbus_message_iter_recurse(&dict, &entry);
325                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
326                         break;
327
328                 dbus_message_iter_get_basic(&entry, &key);
329
330                 if (g_str_equal(key, "Username")) {
331                         dbus_message_iter_next(&entry);
332                         if (dbus_message_iter_get_arg_type(&entry)
333                                                         != DBUS_TYPE_VARIANT)
334                                 break;
335                         dbus_message_iter_recurse(&entry, &value);
336                         if (dbus_message_iter_get_arg_type(&value)
337                                                         != DBUS_TYPE_STRING)
338                                 break;
339                         dbus_message_iter_get_basic(&value, &str);
340                         username = g_strdup(str);
341                 }
342
343                 if (g_str_equal(key, "Password")) {
344                         dbus_message_iter_next(&entry);
345                         if (dbus_message_iter_get_arg_type(&entry)
346                                                         != DBUS_TYPE_VARIANT)
347                                 break;
348                         dbus_message_iter_recurse(&entry, &value);
349                         if (dbus_message_iter_get_arg_type(&value)
350                                                         != DBUS_TYPE_STRING)
351                                 break;
352                         dbus_message_iter_get_basic(&value, &str);
353                         password = g_strdup(str);
354                 }
355
356                 dbus_message_iter_next(&dict);
357         }
358
359 done:
360         pptp_reply->callback(pptp_reply->provider, username, password, error,
361                                 pptp_reply->user_data);
362
363         g_free(username);
364         g_free(password);
365
366         g_free(pptp_reply);
367 }
368
369 typedef void (* request_cb_t)(struct vpn_provider *provider,
370                                 const char *username, const char *password,
371                                 const char *error, void *user_data);
372
373 static int request_input(struct vpn_provider *provider,
374                         request_cb_t callback, const char *dbus_sender,
375                         void *user_data)
376 {
377         DBusMessage *message;
378         const char *path, *agent_sender, *agent_path;
379         DBusMessageIter iter;
380         DBusMessageIter dict;
381         struct request_input_reply *pptp_reply;
382         int err;
383         void *agent;
384
385         agent = connman_agent_get_info(dbus_sender, &agent_sender,
386                                                         &agent_path);
387         if (!provider || !agent || !agent_path || !callback)
388                 return -ESRCH;
389
390         message = dbus_message_new_method_call(agent_sender, agent_path,
391                                         VPN_AGENT_INTERFACE,
392                                         "RequestInput");
393         if (!message)
394                 return -ENOMEM;
395
396         dbus_message_iter_init_append(message, &iter);
397
398         path = vpn_provider_get_path(provider);
399         dbus_message_iter_append_basic(&iter,
400                                 DBUS_TYPE_OBJECT_PATH, &path);
401
402         connman_dbus_dict_open(&iter, &dict);
403
404         if (vpn_provider_get_authentication_errors(provider))
405                 vpn_agent_append_auth_failure(&dict, provider, NULL);
406
407         vpn_agent_append_user_info(&dict, provider, "PPTP.User");
408
409         vpn_agent_append_host_and_name(&dict, provider);
410
411         connman_dbus_dict_close(&iter, &dict);
412
413         pptp_reply = g_try_new0(struct request_input_reply, 1);
414         if (!pptp_reply) {
415                 dbus_message_unref(message);
416                 return -ENOMEM;
417         }
418
419         pptp_reply->provider = provider;
420         pptp_reply->callback = callback;
421         pptp_reply->user_data = user_data;
422
423         err = connman_agent_queue_message(provider, message,
424                         connman_timeout_input_request(),
425                         request_input_reply, pptp_reply, agent);
426         if (err < 0 && err != -EBUSY) {
427                 DBG("error %d sending agent request", err);
428                 dbus_message_unref(message);
429                 g_free(pptp_reply);
430                 return err;
431         }
432
433         dbus_message_unref(message);
434
435         return -EINPROGRESS;
436 }
437
438 static int run_connect(struct vpn_provider *provider,
439                         struct connman_task *task, const char *if_name,
440                         vpn_provider_connect_cb_t cb, void *user_data,
441                         const char *username, const char *password)
442 {
443         GString *pptp_opt_s;
444         const char *opt_s;
445         const char *host;
446         char *str;
447         int err, i;
448
449         if (!username || !password) {
450                 DBG("Cannot connect username %s password %p",
451                                                 username, password);
452                 err = -EINVAL;
453                 goto done;
454         }
455
456         DBG("username %s password %p", username, password);
457
458         host = vpn_provider_get_string(provider, "Host");
459
460         /* Create PPTP options for pppd "pty" */
461         pptp_opt_s = g_string_new(NULL);
462         g_string_append_printf(pptp_opt_s, "%s %s --nolaunchpppd --loglevel 2",
463                                 PPTP, host);
464
465         connman_task_add_argument(task, "nodetach", NULL);
466         connman_task_add_argument(task, "lock", NULL);
467         connman_task_add_argument(task, "logfd", "2");
468         connman_task_add_argument(task, "usepeerdns", NULL);
469         connman_task_add_argument(task, "noipdefault", NULL);
470         connman_task_add_argument(task, "noauth", NULL);
471         connman_task_add_argument(task, "nodefaultroute", NULL);
472         connman_task_add_argument(task, "ipparam", "pptp_plugin");
473
474         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
475                 opt_s = vpn_provider_get_string(provider,
476                                         pptp_options[i].cm_opt);
477                 if (!opt_s)
478                         opt_s = pptp_options[i].pptp_default;
479
480                 if (!opt_s)
481                         continue;
482
483                 if (pptp_options[i].type == OPT_STRING)
484                         connman_task_add_argument(task,
485                                         pptp_options[i].pptp_opt, opt_s);
486                 else if (pptp_options[i].type == OPT_BOOL)
487                         pptp_write_bool_option(task,
488                                         pptp_options[i].pptp_opt, opt_s);
489                 else if (pptp_options[i].type == OPT_PPTP_ONLY)
490                         g_string_append_printf(pptp_opt_s, " %s %s",
491                                         pptp_options[i].pptp_opt, opt_s);
492         }
493
494         str = g_string_free(pptp_opt_s, FALSE);
495         connman_task_add_argument(task, "pty", str);
496         g_free(str);
497
498         connman_task_add_argument(task, "plugin",
499                                 SCRIPTDIR "/libppp-plugin.so");
500
501         err = connman_task_run(task, vpn_died, provider,
502                                 NULL, NULL, NULL);
503         if (err < 0) {
504                 connman_error("pptp failed to start");
505                 err = -EIO;
506                 goto done;
507         }
508
509 done:
510         if (cb)
511                 cb(provider, user_data, err);
512
513         return err;
514 }
515
516 static void free_private_data(struct pptp_private_data *data)
517 {
518         g_free(data->if_name);
519         g_free(data);
520 }
521
522 static void request_input_cb(struct vpn_provider *provider,
523                         const char *username,
524                         const char *password,
525                         const char *error, void *user_data)
526 {
527         struct pptp_private_data *data = user_data;
528
529         if (!username || !password)
530                 DBG("Requesting username %s or password failed, error %s",
531                         username, error);
532         else if (error)
533                 DBG("error %s", error);
534
535         vpn_provider_set_string(provider, "PPTP.User", username);
536         vpn_provider_set_string_hide_value(provider, "PPTP.Password",
537                                                                 password);
538
539         run_connect(provider, data->task, data->if_name, data->cb,
540                 data->user_data, username, password);
541
542         free_private_data(data);
543 }
544
545 static int pptp_connect(struct vpn_provider *provider,
546                         struct connman_task *task, const char *if_name,
547                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
548                         void *user_data)
549 {
550         const char *username, *password;
551         int err;
552
553         DBG("iface %s provider %p user %p", if_name, provider, user_data);
554
555         if (connman_task_set_notify(task, "getsec",
556                                         pptp_get_sec, provider)) {
557                 err = -ENOMEM;
558                 goto error;
559         }
560
561         username = vpn_provider_get_string(provider, "PPTP.User");
562         password = vpn_provider_get_string(provider, "PPTP.Password");
563
564         DBG("user %s password %p", username, password);
565
566         if (!username || !password) {
567                 struct pptp_private_data *data;
568
569                 data = g_try_new0(struct pptp_private_data, 1);
570                 if (!data)
571                         return -ENOMEM;
572
573                 data->task = task;
574                 data->if_name = g_strdup(if_name);
575                 data->cb = cb;
576                 data->user_data = user_data;
577
578                 err = request_input(provider, request_input_cb, dbus_sender,
579                                                                         data);
580                 if (err != -EINPROGRESS) {
581                         free_private_data(data);
582                         goto done;
583                 }
584                 return err;
585         }
586
587 done:
588         return run_connect(provider, task, if_name, cb, user_data,
589                                                         username, password);
590
591 error:
592         if (cb)
593                 cb(provider, user_data, err);
594
595         return err;
596 }
597
598 static int pptp_error_code(struct vpn_provider *provider, int exit_code)
599 {
600
601         switch (exit_code) {
602         case 1:
603                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
604         case 2:
605                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
606         case 16:
607                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
608         default:
609                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
610         }
611 }
612
613 static void pptp_disconnect(struct vpn_provider *provider)
614 {
615         if (!provider)
616                 return;
617
618         vpn_provider_set_string_hide_value(provider, "PPTP.Password", NULL);
619
620         connman_agent_cancel(provider);
621 }
622
623 static struct vpn_driver vpn_driver = {
624         .flags          = VPN_FLAG_NO_TUN,
625         .notify         = pptp_notify,
626         .connect        = pptp_connect,
627         .error_code     = pptp_error_code,
628         .save           = pptp_save,
629         .disconnect     = pptp_disconnect,
630 };
631
632 static int pptp_init(void)
633 {
634         connection = connman_dbus_get_connection();
635
636         return vpn_register("pptp", &vpn_driver, PPPD);
637 }
638
639 static void pptp_exit(void)
640 {
641         vpn_unregister("pptp");
642
643         dbus_connection_unref(connection);
644 }
645
646 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
647         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)