pptp: Ask username and password from agent
[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
49 #include "vpn.h"
50
51 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
52
53 enum {
54         OPT_STRING = 1,
55         OPT_BOOL = 2,
56 };
57
58 struct {
59         const char *cm_opt;
60         const char *pptp_opt;
61         const char *vpnc_default;
62         int type;
63 } pptp_options[] = {
64         { "PPTP.User", "user", NULL, OPT_STRING },
65         { "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
66         { "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
67         { "PPTP.Debug", "debug", NULL, OPT_STRING },
68         { "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
69         { "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
70         { "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
71         { "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
72         { "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
73         { "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
74         { "PPTP.NoDeflate", "nodeflate", NULL, OPT_BOOL },
75         { "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
76         { "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
77         { "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
78         { "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
79         { "PPTP.NoVJ", "no-vj-comp", NULL, OPT_BOOL },
80 };
81
82 static DBusConnection *connection;
83
84 struct pptp_private_data {
85         struct connman_task *task;
86         char *if_name;
87         vpn_provider_connect_cb_t cb;
88         void *user_data;
89 };
90
91 static DBusMessage *pptp_get_sec(struct connman_task *task,
92                                 DBusMessage *msg, void *user_data)
93 {
94         const char *user, *passwd;
95         struct vpn_provider *provider = user_data;
96         DBusMessage *reply;
97
98         if (dbus_message_get_no_reply(msg) == TRUE)
99                 return NULL;
100
101         user = vpn_provider_get_string(provider, "PPTP.User");
102         passwd = vpn_provider_get_string(provider, "PPTP.Password");
103         if (user == NULL || strlen(user) == 0 ||
104                                 passwd == NULL || strlen(passwd) == 0)
105                 return NULL;
106
107         reply = dbus_message_new_method_return(msg);
108         if (reply == NULL)
109                 return NULL;
110
111         dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
112                                 DBUS_TYPE_STRING, &passwd,
113                                 DBUS_TYPE_INVALID);
114         return reply;
115 }
116
117 static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
118 {
119         DBusMessageIter iter, dict;
120         const char *reason, *key, *value;
121         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
122         char *ifname = NULL, *nameservers = NULL;
123         struct connman_ipaddress *ipaddress = NULL;
124
125         dbus_message_iter_init(msg, &iter);
126
127         dbus_message_iter_get_basic(&iter, &reason);
128         dbus_message_iter_next(&iter);
129
130         if (provider == NULL) {
131                 connman_error("No provider found");
132                 return VPN_STATE_FAILURE;
133         }
134
135         if (strcmp(reason, "auth failed") == 0)
136                 return VPN_STATE_AUTH_FAILURE;
137
138         if (strcmp(reason, "connect"))
139                 return VPN_STATE_DISCONNECT;
140
141         dbus_message_iter_recurse(&iter, &dict);
142
143         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
144                 DBusMessageIter entry;
145
146                 dbus_message_iter_recurse(&dict, &entry);
147                 dbus_message_iter_get_basic(&entry, &key);
148                 dbus_message_iter_next(&entry);
149                 dbus_message_iter_get_basic(&entry, &value);
150
151                 DBG("%s = %s", key, value);
152
153                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
154                         vpn_provider_set_string(provider, "Address", value);
155                         addressv4 = g_strdup(value);
156                 }
157
158                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
159                         vpn_provider_set_string(provider, "Netmask", value);
160                         netmask = g_strdup(value);
161                 }
162
163                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
164                         vpn_provider_set_string(provider, "DNS", value);
165                         nameservers = g_strdup(value);
166                 }
167
168                 if (!strcmp(key, "INTERNAL_IFNAME"))
169                         ifname = g_strdup(value);
170
171                 dbus_message_iter_next(&dict);
172         }
173
174         if (vpn_set_ifname(provider, ifname) < 0) {
175                 g_free(ifname);
176                 g_free(addressv4);
177                 g_free(netmask);
178                 g_free(nameservers);
179                 return VPN_STATE_FAILURE;
180         }
181
182         if (addressv4 != NULL)
183                 ipaddress = connman_ipaddress_alloc(AF_INET);
184
185         g_free(ifname);
186
187         if (ipaddress == NULL) {
188                 connman_error("No IP address for provider");
189                 g_free(addressv4);
190                 g_free(netmask);
191                 g_free(nameservers);
192                 return VPN_STATE_FAILURE;
193         }
194
195         value = vpn_provider_get_string(provider, "HostIP");
196         if (value != NULL) {
197                 vpn_provider_set_string(provider, "Gateway", value);
198                 gateway = g_strdup(value);
199         }
200
201         if (addressv4 != NULL)
202                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
203                                         gateway);
204
205         vpn_provider_set_ipaddress(provider, ipaddress);
206         vpn_provider_set_nameservers(provider, nameservers);
207
208         g_free(addressv4);
209         g_free(netmask);
210         g_free(gateway);
211         g_free(nameservers);
212         connman_ipaddress_free(ipaddress);
213
214         return VPN_STATE_CONNECT;
215 }
216
217 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
218 {
219         const char *option;
220         int i;
221
222         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
223                 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0) {
224                         option = vpn_provider_get_string(provider,
225                                                         pptp_options[i].cm_opt);
226                         if (option == NULL)
227                                 continue;
228
229                         g_key_file_set_string(keyfile,
230                                         vpn_provider_get_save_group(provider),
231                                         pptp_options[i].cm_opt, option);
232                 }
233         }
234
235         option = vpn_provider_get_string(provider, "PPTP.Password");
236         if (option != NULL)
237                 g_key_file_set_string(keyfile,
238                                         vpn_provider_get_save_group(provider),
239                                         "PPTP.Password", option);
240
241         return 0;
242 }
243
244 static void pptp_write_bool_option(struct connman_task *task,
245                                 const char *key, const char *value)
246 {
247         if (key != NULL && value != NULL) {
248                 if (strcasecmp(value, "yes") == 0 ||
249                                 strcasecmp(value, "true") == 0 ||
250                                 strcmp(value, "1") == 0)
251                         connman_task_add_argument(task, key, NULL);
252         }
253 }
254
255 struct request_input_reply {
256         struct vpn_provider *provider;
257         vpn_provider_password_cb_t callback;
258         void *user_data;
259 };
260
261 static void request_input_reply(DBusMessage *reply, void *user_data)
262 {
263         struct request_input_reply *pptp_reply = user_data;
264         const char *error = NULL;
265         char *username = NULL, *password = NULL;
266         char *key;
267         DBusMessageIter iter, dict;
268
269         DBG("provider %p", pptp_reply->provider);
270
271         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
272                 error = dbus_message_get_error_name(reply);
273                 goto done;
274         }
275
276         if (vpn_agent_check_reply_has_dict(reply) == FALSE)
277                 goto done;
278
279         dbus_message_iter_init(reply, &iter);
280         dbus_message_iter_recurse(&iter, &dict);
281         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
282                 DBusMessageIter entry, value;
283                 const char *str;
284
285                 dbus_message_iter_recurse(&dict, &entry);
286                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
287                         break;
288
289                 dbus_message_iter_get_basic(&entry, &key);
290
291                 if (g_str_equal(key, "Username")) {
292                         dbus_message_iter_next(&entry);
293                         if (dbus_message_iter_get_arg_type(&entry)
294                                                         != DBUS_TYPE_VARIANT)
295                                 break;
296                         dbus_message_iter_recurse(&entry, &value);
297                         if (dbus_message_iter_get_arg_type(&value)
298                                                         != DBUS_TYPE_STRING)
299                                 break;
300                         dbus_message_iter_get_basic(&value, &str);
301                         username = g_strdup(str);
302                 }
303
304                 if (g_str_equal(key, "Password")) {
305                         dbus_message_iter_next(&entry);
306                         if (dbus_message_iter_get_arg_type(&entry)
307                                                         != DBUS_TYPE_VARIANT)
308                                 break;
309                         dbus_message_iter_recurse(&entry, &value);
310                         if (dbus_message_iter_get_arg_type(&value)
311                                                         != DBUS_TYPE_STRING)
312                                 break;
313                         dbus_message_iter_get_basic(&value, &str);
314                         password = g_strdup(str);
315                 }
316
317                 dbus_message_iter_next(&dict);
318         }
319
320 done:
321         pptp_reply->callback(pptp_reply->provider, username, password, error,
322                                 pptp_reply->user_data);
323
324         g_free(username);
325         g_free(password);
326
327         g_free(pptp_reply);
328 }
329
330 typedef void (* request_cb_t)(struct vpn_provider *provider,
331                                 const char *username, const char *password,
332                                 const char *error, void *user_data);
333
334 static int request_input(struct vpn_provider *provider,
335                                 request_cb_t callback, void *user_data)
336 {
337         DBusMessage *message;
338         const char *path, *agent_sender, *agent_path;
339         DBusMessageIter iter;
340         DBusMessageIter dict;
341         struct request_input_reply *pptp_reply;
342         int err;
343
344         connman_agent_get_info(&agent_sender, &agent_path);
345
346         if (provider == NULL || agent_path == NULL || callback == NULL)
347                 return -ESRCH;
348
349         message = dbus_message_new_method_call(agent_sender, agent_path,
350                                         VPN_AGENT_INTERFACE,
351                                         "RequestInput");
352         if (message == NULL)
353                 return -ENOMEM;
354
355         dbus_message_iter_init_append(message, &iter);
356
357         path = vpn_provider_get_path(provider);
358         dbus_message_iter_append_basic(&iter,
359                                 DBUS_TYPE_OBJECT_PATH, &path);
360
361         connman_dbus_dict_open(&iter, &dict);
362
363         vpn_agent_append_user_info(&dict, provider, "PPTP.User");
364
365         vpn_agent_append_host_and_name(&dict, provider);
366
367         connman_dbus_dict_close(&iter, &dict);
368
369         pptp_reply = g_try_new0(struct request_input_reply, 1);
370         if (pptp_reply == NULL) {
371                 dbus_message_unref(message);
372                 return -ENOMEM;
373         }
374
375         pptp_reply->provider = provider;
376         pptp_reply->callback = callback;
377         pptp_reply->user_data = user_data;
378
379         err = connman_agent_queue_message(provider, message,
380                         connman_timeout_input_request(),
381                         request_input_reply, pptp_reply);
382         if (err < 0 && err != -EBUSY) {
383                 DBG("error %d sending agent request", err);
384                 dbus_message_unref(message);
385                 g_free(pptp_reply);
386                 return err;
387         }
388
389         dbus_message_unref(message);
390
391         return -EINPROGRESS;
392 }
393
394 static int run_connect(struct vpn_provider *provider,
395                         struct connman_task *task, const char *if_name,
396                         vpn_provider_connect_cb_t cb, void *user_data,
397                         const char *username, const char *password)
398 {
399         const char *opt_s, *host;
400         char *str;
401         int err, i;
402
403         host = vpn_provider_get_string(provider, "Host");
404         if (host == NULL) {
405                 connman_error("Host not set; cannot enable VPN");
406                 err = -EINVAL;
407                 goto done;
408         }
409
410         if (username == NULL || password == NULL) {
411                 DBG("Cannot connect username %s password %p",
412                                                 username, password);
413                 err = -EINVAL;
414                 goto done;
415         }
416
417         vpn_provider_set_string(provider, "PPTP.User", username);
418         vpn_provider_set_string(provider, "PPTP.Password", password);
419
420         DBG("username %s password %p", username, password);
421
422         str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
423                                 PPTP, host);
424         if (str == NULL) {
425                 connman_error("can not allocate memory");
426                 err = -ENOMEM;
427                 goto done;
428         }
429
430         connman_task_add_argument(task, "pty", str);
431         g_free(str);
432
433         connman_task_add_argument(task, "nodetach", NULL);
434         connman_task_add_argument(task, "lock", NULL);
435         connman_task_add_argument(task, "usepeerdns", NULL);
436         connman_task_add_argument(task, "noipdefault", NULL);
437         connman_task_add_argument(task, "noauth", NULL);
438         connman_task_add_argument(task, "nodefaultroute", NULL);
439         connman_task_add_argument(task, "ipparam", "pptp_plugin");
440
441         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
442                 opt_s = vpn_provider_get_string(provider,
443                                         pptp_options[i].cm_opt);
444                 if (opt_s == NULL)
445                         opt_s = pptp_options[i].vpnc_default;
446
447                 if (opt_s == NULL)
448                         continue;
449
450                 if (pptp_options[i].type == OPT_STRING)
451                         connman_task_add_argument(task,
452                                         pptp_options[i].pptp_opt, opt_s);
453                 else if (pptp_options[i].type == OPT_BOOL)
454                         pptp_write_bool_option(task,
455                                         pptp_options[i].pptp_opt, opt_s);
456         }
457
458         connman_task_add_argument(task, "plugin",
459                                 SCRIPTDIR "/libppp-plugin.so");
460
461         err = connman_task_run(task, vpn_died, provider,
462                                 NULL, NULL, NULL);
463         if (err < 0) {
464                 connman_error("pptp failed to start");
465                 err = -EIO;
466                 goto done;
467         }
468
469 done:
470         if (cb != NULL)
471                 cb(provider, user_data, err);
472
473         return err;
474 }
475
476 static void free_private_data(struct pptp_private_data *data)
477 {
478         g_free(data->if_name);
479         g_free(data);
480 }
481
482 static void request_input_cb(struct vpn_provider *provider,
483                         const char *username,
484                         const char *password,
485                         const char *error, void *user_data)
486 {
487         struct pptp_private_data *data = user_data;
488
489         if (username == NULL || password == NULL)
490                 DBG("Requesting username %s or password failed, error %s",
491                         username, error);
492         else if (error != NULL)
493                 DBG("error %s", error);
494
495         run_connect(provider, data->task, data->if_name, data->cb,
496                 data->user_data, username, password);
497
498         free_private_data(data);
499 }
500
501 static int pptp_connect(struct vpn_provider *provider,
502                         struct connman_task *task, const char *if_name,
503                         vpn_provider_connect_cb_t cb, void *user_data)
504 {
505         const char *username, *password;
506         int err;
507
508         DBG("iface %s provider %p user %p", if_name, provider, user_data);
509
510         if (connman_task_set_notify(task, "getsec",
511                                         pptp_get_sec, provider)) {
512                 err = -ENOMEM;
513                 goto error;
514         }
515
516         username = vpn_provider_get_string(provider, "PPTP.User");
517         password = vpn_provider_get_string(provider, "PPTP.Password");
518
519         DBG("user %s password %p", username, password);
520
521         if (username == NULL || password == NULL) {
522                 struct pptp_private_data *data;
523
524                 data = g_try_new0(struct pptp_private_data, 1);
525                 if (data == NULL)
526                         return -ENOMEM;
527
528                 data->task = task;
529                 data->if_name = g_strdup(if_name);
530                 data->cb = cb;
531                 data->user_data = user_data;
532
533                 err = request_input(provider, request_input_cb, data);
534                 if (err != -EINPROGRESS) {
535                         free_private_data(data);
536                         goto done;
537                 }
538                 return err;
539         }
540
541 done:
542         return run_connect(provider, task, if_name, cb, user_data,
543                                                         username, password);
544
545 error:
546         if (cb != NULL)
547                 cb(provider, user_data, err);
548
549         return err;
550 }
551
552 static int pptp_error_code(int exit_code)
553 {
554
555         switch (exit_code) {
556         case 1:
557                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
558         case 2:
559                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
560         case 16:
561                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
562         default:
563                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
564         }
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 };
574
575 static int pptp_init(void)
576 {
577         connection = connman_dbus_get_connection();
578
579         return vpn_register("pptp", &vpn_driver, PPPD);
580 }
581
582 static void pptp_exit(void)
583 {
584         vpn_unregister("pptp");
585
586         dbus_connection_unref(connection);
587 }
588
589 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
590         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)