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