pptp: Header file was missing
[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         { "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
67         { "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
68         { "PPTP.Debug", "debug", NULL, OPT_STRING },
69         { "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
70         { "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
71         { "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
72         { "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
73         { "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
74         { "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
75         { "PPTP.NoDeflate", "nodeflate", NULL, OPT_BOOL },
76         { "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
77         { "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
78         { "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
79         { "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
80         { "PPTP.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         int i;
222
223         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
224                 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0) {
225                         option = vpn_provider_get_string(provider,
226                                                         pptp_options[i].cm_opt);
227                         if (option == NULL)
228                                 continue;
229
230                         g_key_file_set_string(keyfile,
231                                         vpn_provider_get_save_group(provider),
232                                         pptp_options[i].cm_opt, option);
233                 }
234         }
235
236         return 0;
237 }
238
239 static void pptp_write_bool_option(struct connman_task *task,
240                                 const char *key, const char *value)
241 {
242         if (key != NULL && value != NULL) {
243                 if (strcasecmp(value, "yes") == 0 ||
244                                 strcasecmp(value, "true") == 0 ||
245                                 strcmp(value, "1") == 0)
246                         connman_task_add_argument(task, key, NULL);
247         }
248 }
249
250 struct request_input_reply {
251         struct vpn_provider *provider;
252         vpn_provider_password_cb_t callback;
253         void *user_data;
254 };
255
256 static void request_input_reply(DBusMessage *reply, void *user_data)
257 {
258         struct request_input_reply *pptp_reply = user_data;
259         const char *error = NULL;
260         char *username = NULL, *password = NULL;
261         char *key;
262         DBusMessageIter iter, dict;
263
264         DBG("provider %p", pptp_reply->provider);
265
266         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
267                 error = dbus_message_get_error_name(reply);
268                 goto done;
269         }
270
271         if (vpn_agent_check_reply_has_dict(reply) == FALSE)
272                 goto done;
273
274         dbus_message_iter_init(reply, &iter);
275         dbus_message_iter_recurse(&iter, &dict);
276         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
277                 DBusMessageIter entry, value;
278                 const char *str;
279
280                 dbus_message_iter_recurse(&dict, &entry);
281                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
282                         break;
283
284                 dbus_message_iter_get_basic(&entry, &key);
285
286                 if (g_str_equal(key, "Username")) {
287                         dbus_message_iter_next(&entry);
288                         if (dbus_message_iter_get_arg_type(&entry)
289                                                         != DBUS_TYPE_VARIANT)
290                                 break;
291                         dbus_message_iter_recurse(&entry, &value);
292                         if (dbus_message_iter_get_arg_type(&value)
293                                                         != DBUS_TYPE_STRING)
294                                 break;
295                         dbus_message_iter_get_basic(&value, &str);
296                         username = g_strdup(str);
297                 }
298
299                 if (g_str_equal(key, "Password")) {
300                         dbus_message_iter_next(&entry);
301                         if (dbus_message_iter_get_arg_type(&entry)
302                                                         != DBUS_TYPE_VARIANT)
303                                 break;
304                         dbus_message_iter_recurse(&entry, &value);
305                         if (dbus_message_iter_get_arg_type(&value)
306                                                         != DBUS_TYPE_STRING)
307                                 break;
308                         dbus_message_iter_get_basic(&value, &str);
309                         password = g_strdup(str);
310                 }
311
312                 dbus_message_iter_next(&dict);
313         }
314
315 done:
316         pptp_reply->callback(pptp_reply->provider, username, password, error,
317                                 pptp_reply->user_data);
318
319         g_free(username);
320         g_free(password);
321
322         g_free(pptp_reply);
323 }
324
325 typedef void (* request_cb_t)(struct vpn_provider *provider,
326                                 const char *username, const char *password,
327                                 const char *error, void *user_data);
328
329 static int request_input(struct vpn_provider *provider,
330                                 request_cb_t callback, void *user_data)
331 {
332         DBusMessage *message;
333         const char *path, *agent_sender, *agent_path;
334         DBusMessageIter iter;
335         DBusMessageIter dict;
336         struct request_input_reply *pptp_reply;
337         int err;
338
339         connman_agent_get_info(&agent_sender, &agent_path);
340
341         if (provider == NULL || agent_path == NULL || callback == NULL)
342                 return -ESRCH;
343
344         message = dbus_message_new_method_call(agent_sender, agent_path,
345                                         VPN_AGENT_INTERFACE,
346                                         "RequestInput");
347         if (message == NULL)
348                 return -ENOMEM;
349
350         dbus_message_iter_init_append(message, &iter);
351
352         path = vpn_provider_get_path(provider);
353         dbus_message_iter_append_basic(&iter,
354                                 DBUS_TYPE_OBJECT_PATH, &path);
355
356         connman_dbus_dict_open(&iter, &dict);
357
358         vpn_agent_append_user_info(&dict, provider, "PPTP.User");
359
360         vpn_agent_append_host_and_name(&dict, provider);
361
362         connman_dbus_dict_close(&iter, &dict);
363
364         pptp_reply = g_try_new0(struct request_input_reply, 1);
365         if (pptp_reply == NULL) {
366                 dbus_message_unref(message);
367                 return -ENOMEM;
368         }
369
370         pptp_reply->provider = provider;
371         pptp_reply->callback = callback;
372         pptp_reply->user_data = user_data;
373
374         err = connman_agent_queue_message(provider, message,
375                         connman_timeout_input_request(),
376                         request_input_reply, pptp_reply);
377         if (err < 0 && err != -EBUSY) {
378                 DBG("error %d sending agent request", err);
379                 dbus_message_unref(message);
380                 g_free(pptp_reply);
381                 return err;
382         }
383
384         dbus_message_unref(message);
385
386         return -EINPROGRESS;
387 }
388
389 static int run_connect(struct vpn_provider *provider,
390                         struct connman_task *task, const char *if_name,
391                         vpn_provider_connect_cb_t cb, void *user_data,
392                         const char *username, const char *password)
393 {
394         const char *opt_s, *host;
395         char *str;
396         int err, i;
397
398         host = vpn_provider_get_string(provider, "Host");
399         if (host == NULL) {
400                 connman_error("Host not set; cannot enable VPN");
401                 err = -EINVAL;
402                 goto done;
403         }
404
405         if (username == NULL || password == NULL) {
406                 DBG("Cannot connect username %s password %p",
407                                                 username, password);
408                 err = -EINVAL;
409                 goto done;
410         }
411
412         vpn_provider_set_string(provider, "PPTP.User", username);
413         vpn_provider_set_string(provider, "PPTP.Password", password);
414
415         DBG("username %s password %p", username, password);
416
417         str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
418                                 PPTP, host);
419         if (str == NULL) {
420                 connman_error("can not allocate memory");
421                 err = -ENOMEM;
422                 goto done;
423         }
424
425         connman_task_add_argument(task, "pty", str);
426         g_free(str);
427
428         connman_task_add_argument(task, "nodetach", NULL);
429         connman_task_add_argument(task, "lock", NULL);
430         connman_task_add_argument(task, "usepeerdns", NULL);
431         connman_task_add_argument(task, "noipdefault", NULL);
432         connman_task_add_argument(task, "noauth", NULL);
433         connman_task_add_argument(task, "nodefaultroute", NULL);
434         connman_task_add_argument(task, "ipparam", "pptp_plugin");
435
436         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
437                 opt_s = vpn_provider_get_string(provider,
438                                         pptp_options[i].cm_opt);
439                 if (opt_s == NULL)
440                         opt_s = pptp_options[i].vpnc_default;
441
442                 if (opt_s == NULL)
443                         continue;
444
445                 if (pptp_options[i].type == OPT_STRING)
446                         connman_task_add_argument(task,
447                                         pptp_options[i].pptp_opt, opt_s);
448                 else if (pptp_options[i].type == OPT_BOOL)
449                         pptp_write_bool_option(task,
450                                         pptp_options[i].pptp_opt, opt_s);
451         }
452
453         connman_task_add_argument(task, "plugin",
454                                 SCRIPTDIR "/libppp-plugin.so");
455
456         err = connman_task_run(task, vpn_died, provider,
457                                 NULL, NULL, NULL);
458         if (err < 0) {
459                 connman_error("pptp failed to start");
460                 err = -EIO;
461                 goto done;
462         }
463
464 done:
465         if (cb != NULL)
466                 cb(provider, user_data, err);
467
468         return err;
469 }
470
471 static void free_private_data(struct pptp_private_data *data)
472 {
473         g_free(data->if_name);
474         g_free(data);
475 }
476
477 static void request_input_cb(struct vpn_provider *provider,
478                         const char *username,
479                         const char *password,
480                         const char *error, void *user_data)
481 {
482         struct pptp_private_data *data = user_data;
483
484         if (username == NULL || password == NULL)
485                 DBG("Requesting username %s or password failed, error %s",
486                         username, error);
487         else if (error != NULL)
488                 DBG("error %s", error);
489
490         run_connect(provider, data->task, data->if_name, data->cb,
491                 data->user_data, username, password);
492
493         free_private_data(data);
494 }
495
496 static int pptp_connect(struct vpn_provider *provider,
497                         struct connman_task *task, const char *if_name,
498                         vpn_provider_connect_cb_t cb, void *user_data)
499 {
500         const char *username, *password;
501         int err;
502
503         DBG("iface %s provider %p user %p", if_name, provider, user_data);
504
505         if (connman_task_set_notify(task, "getsec",
506                                         pptp_get_sec, provider)) {
507                 err = -ENOMEM;
508                 goto error;
509         }
510
511         username = vpn_provider_get_string(provider, "PPTP.User");
512         password = vpn_provider_get_string(provider, "PPTP.Password");
513
514         DBG("user %s password %p", username, password);
515
516         if (username == NULL || password == NULL) {
517                 struct pptp_private_data *data;
518
519                 data = g_try_new0(struct pptp_private_data, 1);
520                 if (data == NULL)
521                         return -ENOMEM;
522
523                 data->task = task;
524                 data->if_name = g_strdup(if_name);
525                 data->cb = cb;
526                 data->user_data = user_data;
527
528                 err = request_input(provider, request_input_cb, data);
529                 if (err != -EINPROGRESS) {
530                         free_private_data(data);
531                         goto done;
532                 }
533                 return err;
534         }
535
536 done:
537         return run_connect(provider, task, if_name, cb, user_data,
538                                                         username, password);
539
540 error:
541         if (cb != NULL)
542                 cb(provider, user_data, err);
543
544         return err;
545 }
546
547 static int pptp_error_code(int exit_code)
548 {
549
550         switch (exit_code) {
551         case 1:
552                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
553         case 2:
554                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
555         case 16:
556                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
557         default:
558                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
559         }
560 }
561
562 static void pptp_disconnect(struct vpn_provider *provider)
563 {
564         vpn_provider_set_string(provider, "PPTP.Password", NULL);
565 }
566
567 static struct vpn_driver vpn_driver = {
568         .flags          = VPN_FLAG_NO_TUN,
569         .notify         = pptp_notify,
570         .connect        = pptp_connect,
571         .error_code     = pptp_error_code,
572         .save           = pptp_save,
573         .disconnect     = pptp_disconnect,
574 };
575
576 static int pptp_init(void)
577 {
578         connection = connman_dbus_get_connection();
579
580         return vpn_register("pptp", &vpn_driver, PPPD);
581 }
582
583 static void pptp_exit(void)
584 {
585         vpn_unregister("pptp");
586
587         dbus_connection_unref(connection);
588 }
589
590 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
591         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)