5 * Copyright (C) 2010,2013-2014 BMW Car IT GmbH.
6 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
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.
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.
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
33 #include <dbus/dbus.h>
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>
47 #include "../vpn-provider.h"
48 #include "../vpn-agent.h"
52 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
62 const char *vpnc_default;
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", "novj", NULL, OPT_BOOL },
83 static DBusConnection *connection;
85 struct pptp_private_data {
86 struct connman_task *task;
88 vpn_provider_connect_cb_t cb;
92 static DBusMessage *pptp_get_sec(struct connman_task *task,
93 DBusMessage *msg, void *user_data)
95 const char *user, *passwd;
96 struct vpn_provider *provider = user_data;
99 if (dbus_message_get_no_reply(msg))
102 user = vpn_provider_get_string(provider, "PPTP.User");
103 passwd = vpn_provider_get_string(provider, "PPTP.Password");
104 if (!user || strlen(user) == 0 ||
105 !passwd || strlen(passwd) == 0)
108 reply = dbus_message_new_method_return(msg);
112 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
113 DBUS_TYPE_STRING, &passwd,
118 static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
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;
126 dbus_message_iter_init(msg, &iter);
128 dbus_message_iter_get_basic(&iter, &reason);
129 dbus_message_iter_next(&iter);
132 connman_error("No provider found");
133 return VPN_STATE_FAILURE;
136 if (strcmp(reason, "auth failed") == 0) {
137 DBG("authentication failure");
139 vpn_provider_set_string(provider, "PPTP.User", NULL);
140 vpn_provider_set_string_hide_value(provider, "PPTP.Password",
143 return VPN_STATE_AUTH_FAILURE;
146 if (strcmp(reason, "connect"))
147 return VPN_STATE_DISCONNECT;
149 dbus_message_iter_recurse(&iter, &dict);
151 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
152 DBusMessageIter entry;
154 dbus_message_iter_recurse(&dict, &entry);
155 dbus_message_iter_get_basic(&entry, &key);
156 dbus_message_iter_next(&entry);
157 dbus_message_iter_get_basic(&entry, &value);
159 DBG("%s = %s", key, value);
161 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
162 addressv4 = g_strdup(value);
164 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
165 netmask = g_strdup(value);
167 if (!strcmp(key, "INTERNAL_IP4_DNS"))
168 nameservers = g_strdup(value);
170 if (!strcmp(key, "INTERNAL_IFNAME"))
171 ifname = g_strdup(value);
173 dbus_message_iter_next(&dict);
176 if (vpn_set_ifname(provider, ifname) < 0) {
181 return VPN_STATE_FAILURE;
185 ipaddress = connman_ipaddress_alloc(AF_INET);
190 connman_error("No IP address for provider");
194 return VPN_STATE_FAILURE;
197 value = vpn_provider_get_string(provider, "HostIP");
199 vpn_provider_set_string(provider, "Gateway", value);
200 gateway = g_strdup(value);
204 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
207 vpn_provider_set_ipaddress(provider, ipaddress);
208 vpn_provider_set_nameservers(provider, nameservers);
214 connman_ipaddress_free(ipaddress);
216 return VPN_STATE_CONNECT;
219 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
222 bool pptp_option, pppd_option;
225 for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
226 pptp_option = pppd_option = false;
228 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0)
231 if (strncmp(pptp_options[i].cm_opt, "PPPD.", 5) == 0)
234 if (pptp_option || pppd_option) {
235 option = vpn_provider_get_string(provider,
236 pptp_options[i].cm_opt);
239 * Check if the option prefix is PPTP as the
240 * PPPD options were using PPTP prefix earlier.
247 pptp_str = g_strdup_printf("PPTP.%s",
248 &pptp_options[i].cm_opt[5]);
249 option = vpn_provider_get_string(provider,
257 g_key_file_set_string(keyfile,
258 vpn_provider_get_save_group(provider),
259 pptp_options[i].cm_opt, option);
266 static void pptp_write_bool_option(struct connman_task *task,
267 const char *key, const char *value)
270 if (strcasecmp(value, "yes") == 0 ||
271 strcasecmp(value, "true") == 0 ||
272 strcmp(value, "1") == 0)
273 connman_task_add_argument(task, key, NULL);
277 struct request_input_reply {
278 struct vpn_provider *provider;
279 vpn_provider_password_cb_t callback;
283 static void request_input_reply(DBusMessage *reply, void *user_data)
285 struct request_input_reply *pptp_reply = user_data;
286 struct pptp_private_data *data;
287 const char *error = NULL;
288 char *username = NULL, *password = NULL;
290 DBusMessageIter iter, dict;
293 DBG("provider %p", pptp_reply->provider);
298 data = pptp_reply->user_data;
300 err = vpn_agent_check_and_process_reply_error(reply,
301 pptp_reply->provider, data->task, data->cb,
304 /* Ensure cb is called only once */
306 data->user_data = NULL;
307 error = dbus_message_get_error_name(reply);
311 if (!vpn_agent_check_reply_has_dict(reply))
314 dbus_message_iter_init(reply, &iter);
315 dbus_message_iter_recurse(&iter, &dict);
316 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
317 DBusMessageIter entry, value;
320 dbus_message_iter_recurse(&dict, &entry);
321 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
324 dbus_message_iter_get_basic(&entry, &key);
326 if (g_str_equal(key, "Username")) {
327 dbus_message_iter_next(&entry);
328 if (dbus_message_iter_get_arg_type(&entry)
329 != DBUS_TYPE_VARIANT)
331 dbus_message_iter_recurse(&entry, &value);
332 if (dbus_message_iter_get_arg_type(&value)
335 dbus_message_iter_get_basic(&value, &str);
336 username = g_strdup(str);
339 if (g_str_equal(key, "Password")) {
340 dbus_message_iter_next(&entry);
341 if (dbus_message_iter_get_arg_type(&entry)
342 != DBUS_TYPE_VARIANT)
344 dbus_message_iter_recurse(&entry, &value);
345 if (dbus_message_iter_get_arg_type(&value)
348 dbus_message_iter_get_basic(&value, &str);
349 password = g_strdup(str);
352 dbus_message_iter_next(&dict);
356 pptp_reply->callback(pptp_reply->provider, username, password, error,
357 pptp_reply->user_data);
365 typedef void (* request_cb_t)(struct vpn_provider *provider,
366 const char *username, const char *password,
367 const char *error, void *user_data);
369 static int request_input(struct vpn_provider *provider,
370 request_cb_t callback, const char *dbus_sender,
373 DBusMessage *message;
374 const char *path, *agent_sender, *agent_path;
375 DBusMessageIter iter;
376 DBusMessageIter dict;
377 struct request_input_reply *pptp_reply;
381 agent = connman_agent_get_info(dbus_sender, &agent_sender,
383 if (!provider || !agent || !agent_path || !callback)
386 message = dbus_message_new_method_call(agent_sender, agent_path,
392 dbus_message_iter_init_append(message, &iter);
394 path = vpn_provider_get_path(provider);
395 dbus_message_iter_append_basic(&iter,
396 DBUS_TYPE_OBJECT_PATH, &path);
398 connman_dbus_dict_open(&iter, &dict);
400 if (vpn_provider_get_authentication_errors(provider))
401 vpn_agent_append_auth_failure(&dict, provider, NULL);
403 vpn_agent_append_user_info(&dict, provider, "PPTP.User");
405 vpn_agent_append_host_and_name(&dict, provider);
407 connman_dbus_dict_close(&iter, &dict);
409 pptp_reply = g_try_new0(struct request_input_reply, 1);
411 dbus_message_unref(message);
415 pptp_reply->provider = provider;
416 pptp_reply->callback = callback;
417 pptp_reply->user_data = user_data;
419 err = connman_agent_queue_message(provider, message,
420 connman_timeout_input_request(),
421 request_input_reply, pptp_reply, agent);
422 if (err < 0 && err != -EBUSY) {
423 DBG("error %d sending agent request", err);
424 dbus_message_unref(message);
429 dbus_message_unref(message);
434 static int run_connect(struct vpn_provider *provider,
435 struct connman_task *task, const char *if_name,
436 vpn_provider_connect_cb_t cb, void *user_data,
437 const char *username, const char *password)
439 const char *opt_s, *host;
443 if (!username || !password) {
444 DBG("Cannot connect username %s password %p",
450 DBG("username %s password %p", username, password);
452 host = vpn_provider_get_string(provider, "Host");
453 str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
456 connman_error("can not allocate memory");
461 connman_task_add_argument(task, "pty", str);
464 connman_task_add_argument(task, "nodetach", NULL);
465 connman_task_add_argument(task, "lock", NULL);
466 connman_task_add_argument(task, "logfd", "2");
467 connman_task_add_argument(task, "usepeerdns", NULL);
468 connman_task_add_argument(task, "noipdefault", NULL);
469 connman_task_add_argument(task, "noauth", NULL);
470 connman_task_add_argument(task, "nodefaultroute", NULL);
471 connman_task_add_argument(task, "ipparam", "pptp_plugin");
473 for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
474 opt_s = vpn_provider_get_string(provider,
475 pptp_options[i].cm_opt);
477 opt_s = pptp_options[i].vpnc_default;
482 if (pptp_options[i].type == OPT_STRING)
483 connman_task_add_argument(task,
484 pptp_options[i].pptp_opt, opt_s);
485 else if (pptp_options[i].type == OPT_BOOL)
486 pptp_write_bool_option(task,
487 pptp_options[i].pptp_opt, opt_s);
490 connman_task_add_argument(task, "plugin",
491 SCRIPTDIR "/libppp-plugin.so");
493 err = connman_task_run(task, vpn_died, provider,
496 connman_error("pptp failed to start");
503 cb(provider, user_data, err);
508 static void free_private_data(struct pptp_private_data *data)
510 g_free(data->if_name);
514 static void request_input_cb(struct vpn_provider *provider,
515 const char *username,
516 const char *password,
517 const char *error, void *user_data)
519 struct pptp_private_data *data = user_data;
521 if (!username || !password)
522 DBG("Requesting username %s or password failed, error %s",
525 DBG("error %s", error);
527 vpn_provider_set_string(provider, "PPTP.User", username);
528 vpn_provider_set_string_hide_value(provider, "PPTP.Password",
531 run_connect(provider, data->task, data->if_name, data->cb,
532 data->user_data, username, password);
534 free_private_data(data);
537 static int pptp_connect(struct vpn_provider *provider,
538 struct connman_task *task, const char *if_name,
539 vpn_provider_connect_cb_t cb, const char *dbus_sender,
542 const char *username, *password;
545 DBG("iface %s provider %p user %p", if_name, provider, user_data);
547 if (connman_task_set_notify(task, "getsec",
548 pptp_get_sec, provider)) {
553 username = vpn_provider_get_string(provider, "PPTP.User");
554 password = vpn_provider_get_string(provider, "PPTP.Password");
556 DBG("user %s password %p", username, password);
558 if (!username || !password) {
559 struct pptp_private_data *data;
561 data = g_try_new0(struct pptp_private_data, 1);
566 data->if_name = g_strdup(if_name);
568 data->user_data = user_data;
570 err = request_input(provider, request_input_cb, dbus_sender,
572 if (err != -EINPROGRESS) {
573 free_private_data(data);
580 return run_connect(provider, task, if_name, cb, user_data,
585 cb(provider, user_data, err);
590 static int pptp_error_code(struct vpn_provider *provider, int exit_code)
595 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
597 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
599 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
601 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
605 static void pptp_disconnect(struct vpn_provider *provider)
610 vpn_provider_set_string_hide_value(provider, "PPTP.Password", NULL);
612 connman_agent_cancel(provider);
615 static struct vpn_driver vpn_driver = {
616 .flags = VPN_FLAG_NO_TUN,
617 .notify = pptp_notify,
618 .connect = pptp_connect,
619 .error_code = pptp_error_code,
621 .disconnect = pptp_disconnect,
624 static int pptp_init(void)
626 connection = connman_dbus_get_connection();
628 return vpn_register("pptp", &vpn_driver, PPPD);
631 static void pptp_exit(void)
633 vpn_unregister("pptp");
635 dbus_connection_unref(connection);
638 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
639 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)