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