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