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