pptp: Use PPPD prefix for pppd specific options
[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                 return VPN_STATE_AUTH_FAILURE;
138
139         if (strcmp(reason, "connect"))
140                 return VPN_STATE_DISCONNECT;
141
142         dbus_message_iter_recurse(&iter, &dict);
143
144         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
145                 DBusMessageIter entry;
146
147                 dbus_message_iter_recurse(&dict, &entry);
148                 dbus_message_iter_get_basic(&entry, &key);
149                 dbus_message_iter_next(&entry);
150                 dbus_message_iter_get_basic(&entry, &value);
151
152                 DBG("%s = %s", key, value);
153
154                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
155                         vpn_provider_set_string(provider, "Address", value);
156                         addressv4 = g_strdup(value);
157                 }
158
159                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
160                         vpn_provider_set_string(provider, "Netmask", value);
161                         netmask = g_strdup(value);
162                 }
163
164                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
165                         vpn_provider_set_string(provider, "DNS", value);
166                         nameservers = g_strdup(value);
167                 }
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 != NULL)
184                 ipaddress = connman_ipaddress_alloc(AF_INET);
185
186         g_free(ifname);
187
188         if (ipaddress == NULL) {
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 != NULL) {
198                 vpn_provider_set_string(provider, "Gateway", value);
199                 gateway = g_strdup(value);
200         }
201
202         if (addressv4 != NULL)
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         connman_bool_t 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 == TRUE || pppd_option == TRUE) {
234                         option = vpn_provider_get_string(provider,
235                                                         pptp_options[i].cm_opt);
236                         if (option == NULL) {
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 == FALSE)
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 == NULL)
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 != NULL && value != NULL) {
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) == FALSE)
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, void *user_data)
357 {
358         DBusMessage *message;
359         const char *path, *agent_sender, *agent_path;
360         DBusMessageIter iter;
361         DBusMessageIter dict;
362         struct request_input_reply *pptp_reply;
363         int err;
364
365         connman_agent_get_info(&agent_sender, &agent_path);
366
367         if (provider == NULL || agent_path == NULL || callback == NULL)
368                 return -ESRCH;
369
370         message = dbus_message_new_method_call(agent_sender, agent_path,
371                                         VPN_AGENT_INTERFACE,
372                                         "RequestInput");
373         if (message == NULL)
374                 return -ENOMEM;
375
376         dbus_message_iter_init_append(message, &iter);
377
378         path = vpn_provider_get_path(provider);
379         dbus_message_iter_append_basic(&iter,
380                                 DBUS_TYPE_OBJECT_PATH, &path);
381
382         connman_dbus_dict_open(&iter, &dict);
383
384         vpn_agent_append_user_info(&dict, provider, "PPTP.User");
385
386         vpn_agent_append_host_and_name(&dict, provider);
387
388         connman_dbus_dict_close(&iter, &dict);
389
390         pptp_reply = g_try_new0(struct request_input_reply, 1);
391         if (pptp_reply == NULL) {
392                 dbus_message_unref(message);
393                 return -ENOMEM;
394         }
395
396         pptp_reply->provider = provider;
397         pptp_reply->callback = callback;
398         pptp_reply->user_data = user_data;
399
400         err = connman_agent_queue_message(provider, message,
401                         connman_timeout_input_request(),
402                         request_input_reply, pptp_reply);
403         if (err < 0 && err != -EBUSY) {
404                 DBG("error %d sending agent request", err);
405                 dbus_message_unref(message);
406                 g_free(pptp_reply);
407                 return err;
408         }
409
410         dbus_message_unref(message);
411
412         return -EINPROGRESS;
413 }
414
415 static int run_connect(struct vpn_provider *provider,
416                         struct connman_task *task, const char *if_name,
417                         vpn_provider_connect_cb_t cb, void *user_data,
418                         const char *username, const char *password)
419 {
420         const char *opt_s, *host;
421         char *str;
422         int err, i;
423
424         host = vpn_provider_get_string(provider, "Host");
425         if (host == NULL) {
426                 connman_error("Host not set; cannot enable VPN");
427                 err = -EINVAL;
428                 goto done;
429         }
430
431         if (username == NULL || password == NULL) {
432                 DBG("Cannot connect username %s password %p",
433                                                 username, password);
434                 err = -EINVAL;
435                 goto done;
436         }
437
438         vpn_provider_set_string(provider, "PPTP.User", username);
439         vpn_provider_set_string(provider, "PPTP.Password", password);
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 == NULL) {
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, "usepeerdns", NULL);
457         connman_task_add_argument(task, "noipdefault", NULL);
458         connman_task_add_argument(task, "noauth", NULL);
459         connman_task_add_argument(task, "nodefaultroute", NULL);
460         connman_task_add_argument(task, "ipparam", "pptp_plugin");
461
462         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
463                 opt_s = vpn_provider_get_string(provider,
464                                         pptp_options[i].cm_opt);
465                 if (opt_s == NULL)
466                         opt_s = pptp_options[i].vpnc_default;
467
468                 if (opt_s == NULL)
469                         continue;
470
471                 if (pptp_options[i].type == OPT_STRING)
472                         connman_task_add_argument(task,
473                                         pptp_options[i].pptp_opt, opt_s);
474                 else if (pptp_options[i].type == OPT_BOOL)
475                         pptp_write_bool_option(task,
476                                         pptp_options[i].pptp_opt, opt_s);
477         }
478
479         connman_task_add_argument(task, "plugin",
480                                 SCRIPTDIR "/libppp-plugin.so");
481
482         err = connman_task_run(task, vpn_died, provider,
483                                 NULL, NULL, NULL);
484         if (err < 0) {
485                 connman_error("pptp failed to start");
486                 err = -EIO;
487                 goto done;
488         }
489
490 done:
491         if (cb != NULL)
492                 cb(provider, user_data, err);
493
494         return err;
495 }
496
497 static void free_private_data(struct pptp_private_data *data)
498 {
499         g_free(data->if_name);
500         g_free(data);
501 }
502
503 static void request_input_cb(struct vpn_provider *provider,
504                         const char *username,
505                         const char *password,
506                         const char *error, void *user_data)
507 {
508         struct pptp_private_data *data = user_data;
509
510         if (username == NULL || password == NULL)
511                 DBG("Requesting username %s or password failed, error %s",
512                         username, error);
513         else if (error != NULL)
514                 DBG("error %s", error);
515
516         run_connect(provider, data->task, data->if_name, data->cb,
517                 data->user_data, username, password);
518
519         free_private_data(data);
520 }
521
522 static int pptp_connect(struct vpn_provider *provider,
523                         struct connman_task *task, const char *if_name,
524                         vpn_provider_connect_cb_t cb, void *user_data)
525 {
526         const char *username, *password;
527         int err;
528
529         DBG("iface %s provider %p user %p", if_name, provider, user_data);
530
531         if (connman_task_set_notify(task, "getsec",
532                                         pptp_get_sec, provider)) {
533                 err = -ENOMEM;
534                 goto error;
535         }
536
537         username = vpn_provider_get_string(provider, "PPTP.User");
538         password = vpn_provider_get_string(provider, "PPTP.Password");
539
540         DBG("user %s password %p", username, password);
541
542         if (username == NULL || password == NULL) {
543                 struct pptp_private_data *data;
544
545                 data = g_try_new0(struct pptp_private_data, 1);
546                 if (data == NULL)
547                         return -ENOMEM;
548
549                 data->task = task;
550                 data->if_name = g_strdup(if_name);
551                 data->cb = cb;
552                 data->user_data = user_data;
553
554                 err = request_input(provider, request_input_cb, data);
555                 if (err != -EINPROGRESS) {
556                         free_private_data(data);
557                         goto done;
558                 }
559                 return err;
560         }
561
562 done:
563         return run_connect(provider, task, if_name, cb, user_data,
564                                                         username, password);
565
566 error:
567         if (cb != NULL)
568                 cb(provider, user_data, err);
569
570         return err;
571 }
572
573 static int pptp_error_code(int exit_code)
574 {
575
576         switch (exit_code) {
577         case 1:
578                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
579         case 2:
580                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
581         case 16:
582                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
583         default:
584                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
585         }
586 }
587
588 static void pptp_disconnect(struct vpn_provider *provider)
589 {
590         vpn_provider_set_string(provider, "PPTP.Password", NULL);
591 }
592
593 static struct vpn_driver vpn_driver = {
594         .flags          = VPN_FLAG_NO_TUN,
595         .notify         = pptp_notify,
596         .connect        = pptp_connect,
597         .error_code     = pptp_error_code,
598         .save           = pptp_save,
599         .disconnect     = pptp_disconnect,
600 };
601
602 static int pptp_init(void)
603 {
604         connection = connman_dbus_get_connection();
605
606         return vpn_register("pptp", &vpn_driver, PPPD);
607 }
608
609 static void pptp_exit(void)
610 {
611         vpn_unregister("pptp");
612
613         dbus_connection_unref(connection);
614 }
615
616 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
617         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)