3dc93b03f06dc6edc2c8a3a74b62cd152627d4d0
[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(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 (!reply || dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
293                 if (reply)
294                         error = dbus_message_get_error_name(reply);
295                 goto done;
296         }
297
298         if (!vpn_agent_check_reply_has_dict(reply))
299                 goto done;
300
301         dbus_message_iter_init(reply, &iter);
302         dbus_message_iter_recurse(&iter, &dict);
303         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
304                 DBusMessageIter entry, value;
305                 const char *str;
306
307                 dbus_message_iter_recurse(&dict, &entry);
308                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
309                         break;
310
311                 dbus_message_iter_get_basic(&entry, &key);
312
313                 if (g_str_equal(key, "Username")) {
314                         dbus_message_iter_next(&entry);
315                         if (dbus_message_iter_get_arg_type(&entry)
316                                                         != DBUS_TYPE_VARIANT)
317                                 break;
318                         dbus_message_iter_recurse(&entry, &value);
319                         if (dbus_message_iter_get_arg_type(&value)
320                                                         != DBUS_TYPE_STRING)
321                                 break;
322                         dbus_message_iter_get_basic(&value, &str);
323                         username = g_strdup(str);
324                 }
325
326                 if (g_str_equal(key, "Password")) {
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                         password = g_strdup(str);
337                 }
338
339                 dbus_message_iter_next(&dict);
340         }
341
342 done:
343         pptp_reply->callback(pptp_reply->provider, username, password, error,
344                                 pptp_reply->user_data);
345
346         g_free(username);
347         g_free(password);
348
349         g_free(pptp_reply);
350 }
351
352 typedef void (* request_cb_t)(struct vpn_provider *provider,
353                                 const char *username, const char *password,
354                                 const char *error, void *user_data);
355
356 static int request_input(struct vpn_provider *provider,
357                         request_cb_t callback, const char *dbus_sender,
358                         void *user_data)
359 {
360         DBusMessage *message;
361         const char *path, *agent_sender, *agent_path;
362         DBusMessageIter iter;
363         DBusMessageIter dict;
364         struct request_input_reply *pptp_reply;
365         int err;
366         void *agent;
367
368         agent = connman_agent_get_info(dbus_sender, &agent_sender,
369                                                         &agent_path);
370         if (!provider || !agent || !agent_path || !callback)
371                 return -ESRCH;
372
373         message = dbus_message_new_method_call(agent_sender, agent_path,
374                                         VPN_AGENT_INTERFACE,
375                                         "RequestInput");
376         if (!message)
377                 return -ENOMEM;
378
379         dbus_message_iter_init_append(message, &iter);
380
381         path = vpn_provider_get_path(provider);
382         dbus_message_iter_append_basic(&iter,
383                                 DBUS_TYPE_OBJECT_PATH, &path);
384
385         connman_dbus_dict_open(&iter, &dict);
386
387         vpn_agent_append_user_info(&dict, provider, "PPTP.User");
388
389         vpn_agent_append_host_and_name(&dict, provider);
390
391         connman_dbus_dict_close(&iter, &dict);
392
393         pptp_reply = g_try_new0(struct request_input_reply, 1);
394         if (!pptp_reply) {
395                 dbus_message_unref(message);
396                 return -ENOMEM;
397         }
398
399         pptp_reply->provider = provider;
400         pptp_reply->callback = callback;
401         pptp_reply->user_data = user_data;
402
403         err = connman_agent_queue_message(provider, message,
404                         connman_timeout_input_request(),
405                         request_input_reply, pptp_reply, agent);
406         if (err < 0 && err != -EBUSY) {
407                 DBG("error %d sending agent request", err);
408                 dbus_message_unref(message);
409                 g_free(pptp_reply);
410                 return err;
411         }
412
413         dbus_message_unref(message);
414
415         return -EINPROGRESS;
416 }
417
418 static int run_connect(struct vpn_provider *provider,
419                         struct connman_task *task, const char *if_name,
420                         vpn_provider_connect_cb_t cb, void *user_data,
421                         const char *username, const char *password)
422 {
423         const char *opt_s, *host;
424         char *str;
425         int err, i;
426
427         host = vpn_provider_get_string(provider, "Host");
428         if (!host) {
429                 connman_error("Host not set; cannot enable VPN");
430                 err = -EINVAL;
431                 goto done;
432         }
433
434         if (!username || !password) {
435                 DBG("Cannot connect username %s password %p",
436                                                 username, password);
437                 err = -EINVAL;
438                 goto done;
439         }
440
441         DBG("username %s password %p", username, password);
442
443         str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
444                                 PPTP, host);
445         if (!str) {
446                 connman_error("can not allocate memory");
447                 err = -ENOMEM;
448                 goto done;
449         }
450
451         connman_task_add_argument(task, "pty", str);
452         g_free(str);
453
454         connman_task_add_argument(task, "nodetach", NULL);
455         connman_task_add_argument(task, "lock", NULL);
456         connman_task_add_argument(task, "logfd", "2");
457         connman_task_add_argument(task, "usepeerdns", NULL);
458         connman_task_add_argument(task, "noipdefault", NULL);
459         connman_task_add_argument(task, "noauth", NULL);
460         connman_task_add_argument(task, "nodefaultroute", NULL);
461         connman_task_add_argument(task, "ipparam", "pptp_plugin");
462
463         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
464                 opt_s = vpn_provider_get_string(provider,
465                                         pptp_options[i].cm_opt);
466                 if (!opt_s)
467                         opt_s = pptp_options[i].vpnc_default;
468
469                 if (!opt_s)
470                         continue;
471
472                 if (pptp_options[i].type == OPT_STRING)
473                         connman_task_add_argument(task,
474                                         pptp_options[i].pptp_opt, opt_s);
475                 else if (pptp_options[i].type == OPT_BOOL)
476                         pptp_write_bool_option(task,
477                                         pptp_options[i].pptp_opt, opt_s);
478         }
479
480         connman_task_add_argument(task, "plugin",
481                                 SCRIPTDIR "/libppp-plugin.so");
482
483         err = connman_task_run(task, vpn_died, provider,
484                                 NULL, NULL, NULL);
485         if (err < 0) {
486                 connman_error("pptp failed to start");
487                 err = -EIO;
488                 goto done;
489         }
490
491 done:
492         if (cb)
493                 cb(provider, user_data, err);
494
495         return err;
496 }
497
498 static void free_private_data(struct pptp_private_data *data)
499 {
500         g_free(data->if_name);
501         g_free(data);
502 }
503
504 static void request_input_cb(struct vpn_provider *provider,
505                         const char *username,
506                         const char *password,
507                         const char *error, void *user_data)
508 {
509         struct pptp_private_data *data = user_data;
510
511         if (!username || !password)
512                 DBG("Requesting username %s or password failed, error %s",
513                         username, error);
514         else if (error)
515                 DBG("error %s", error);
516
517         vpn_provider_set_string(provider, "PPTP.User", username);
518         vpn_provider_set_string_hide_value(provider, "PPTP.Password",
519                                                                 password);
520
521         run_connect(provider, data->task, data->if_name, data->cb,
522                 data->user_data, username, password);
523
524         free_private_data(data);
525 }
526
527 static int pptp_connect(struct vpn_provider *provider,
528                         struct connman_task *task, const char *if_name,
529                         vpn_provider_connect_cb_t cb, const char *dbus_sender,
530                         void *user_data)
531 {
532         const char *username, *password;
533         int err;
534
535         DBG("iface %s provider %p user %p", if_name, provider, user_data);
536
537         if (connman_task_set_notify(task, "getsec",
538                                         pptp_get_sec, provider)) {
539                 err = -ENOMEM;
540                 goto error;
541         }
542
543         username = vpn_provider_get_string(provider, "PPTP.User");
544         password = vpn_provider_get_string(provider, "PPTP.Password");
545
546         DBG("user %s password %p", username, password);
547
548         if (!username || !password) {
549                 struct pptp_private_data *data;
550
551                 data = g_try_new0(struct pptp_private_data, 1);
552                 if (!data)
553                         return -ENOMEM;
554
555                 data->task = task;
556                 data->if_name = g_strdup(if_name);
557                 data->cb = cb;
558                 data->user_data = user_data;
559
560                 err = request_input(provider, request_input_cb, dbus_sender,
561                                                                         data);
562                 if (err != -EINPROGRESS) {
563                         free_private_data(data);
564                         goto done;
565                 }
566                 return err;
567         }
568
569 done:
570         return run_connect(provider, task, if_name, cb, user_data,
571                                                         username, password);
572
573 error:
574         if (cb)
575                 cb(provider, user_data, err);
576
577         return err;
578 }
579
580 static int pptp_error_code(struct vpn_provider *provider, int exit_code)
581 {
582
583         switch (exit_code) {
584         case 1:
585                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
586         case 2:
587                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
588         case 16:
589                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
590         default:
591                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
592         }
593 }
594
595 static void pptp_disconnect(struct vpn_provider *provider)
596 {
597         vpn_provider_set_string(provider, "PPTP.Password", NULL);
598 }
599
600 static struct vpn_driver vpn_driver = {
601         .flags          = VPN_FLAG_NO_TUN,
602         .notify         = pptp_notify,
603         .connect        = pptp_connect,
604         .error_code     = pptp_error_code,
605         .save           = pptp_save,
606         .disconnect     = pptp_disconnect,
607 };
608
609 static int pptp_init(void)
610 {
611         connection = connman_dbus_get_connection();
612
613         return vpn_register("pptp", &vpn_driver, PPPD);
614 }
615
616 static void pptp_exit(void)
617 {
618         vpn_unregister("pptp");
619
620         dbus_connection_unref(connection);
621 }
622
623 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
624         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)