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