5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
6 * Copyright (C) 2012 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", "no-vj-comp", 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) == TRUE)
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)
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);
131 if (provider == NULL) {
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(provider, "PPTP.Password", NULL);
142 return VPN_STATE_AUTH_FAILURE;
145 if (strcmp(reason, "connect"))
146 return VPN_STATE_DISCONNECT;
148 dbus_message_iter_recurse(&iter, &dict);
150 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
151 DBusMessageIter entry;
153 dbus_message_iter_recurse(&dict, &entry);
154 dbus_message_iter_get_basic(&entry, &key);
155 dbus_message_iter_next(&entry);
156 dbus_message_iter_get_basic(&entry, &value);
158 DBG("%s = %s", key, value);
160 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
161 vpn_provider_set_string(provider, "Address", value);
162 addressv4 = g_strdup(value);
165 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
166 vpn_provider_set_string(provider, "Netmask", value);
167 netmask = g_strdup(value);
170 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
171 vpn_provider_set_string(provider, "DNS", value);
172 nameservers = g_strdup(value);
175 if (!strcmp(key, "INTERNAL_IFNAME"))
176 ifname = g_strdup(value);
178 dbus_message_iter_next(&dict);
181 if (vpn_set_ifname(provider, ifname) < 0) {
186 return VPN_STATE_FAILURE;
189 if (addressv4 != NULL)
190 ipaddress = connman_ipaddress_alloc(AF_INET);
194 if (ipaddress == NULL) {
195 connman_error("No IP address for provider");
199 return VPN_STATE_FAILURE;
202 value = vpn_provider_get_string(provider, "HostIP");
204 vpn_provider_set_string(provider, "Gateway", value);
205 gateway = g_strdup(value);
208 if (addressv4 != NULL)
209 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
212 vpn_provider_set_ipaddress(provider, ipaddress);
213 vpn_provider_set_nameservers(provider, nameservers);
219 connman_ipaddress_free(ipaddress);
221 return VPN_STATE_CONNECT;
224 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
227 connman_bool_t pptp_option, pppd_option;
230 for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
231 pptp_option = pppd_option = FALSE;
233 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0)
236 if (strncmp(pptp_options[i].cm_opt, "PPPD.", 5) == 0)
239 if (pptp_option == TRUE || pppd_option == TRUE) {
240 option = vpn_provider_get_string(provider,
241 pptp_options[i].cm_opt);
242 if (option == NULL) {
244 * Check if the option prefix is PPTP as the
245 * PPPD options were using PPTP prefix earlier.
249 if (pppd_option == FALSE)
252 pptp_str = g_strdup_printf("PPTP.%s",
253 &pptp_options[i].cm_opt[5]);
254 option = vpn_provider_get_string(provider,
262 g_key_file_set_string(keyfile,
263 vpn_provider_get_save_group(provider),
264 pptp_options[i].cm_opt, option);
271 static void pptp_write_bool_option(struct connman_task *task,
272 const char *key, const char *value)
274 if (key != NULL && value != NULL) {
275 if (strcasecmp(value, "yes") == 0 ||
276 strcasecmp(value, "true") == 0 ||
277 strcmp(value, "1") == 0)
278 connman_task_add_argument(task, key, NULL);
282 struct request_input_reply {
283 struct vpn_provider *provider;
284 vpn_provider_password_cb_t callback;
288 static void request_input_reply(DBusMessage *reply, void *user_data)
290 struct request_input_reply *pptp_reply = user_data;
291 const char *error = NULL;
292 char *username = NULL, *password = NULL;
294 DBusMessageIter iter, dict;
296 DBG("provider %p", pptp_reply->provider);
298 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
299 error = dbus_message_get_error_name(reply);
303 if (vpn_agent_check_reply_has_dict(reply) == FALSE)
306 dbus_message_iter_init(reply, &iter);
307 dbus_message_iter_recurse(&iter, &dict);
308 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
309 DBusMessageIter entry, value;
312 dbus_message_iter_recurse(&dict, &entry);
313 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
316 dbus_message_iter_get_basic(&entry, &key);
318 if (g_str_equal(key, "Username")) {
319 dbus_message_iter_next(&entry);
320 if (dbus_message_iter_get_arg_type(&entry)
321 != DBUS_TYPE_VARIANT)
323 dbus_message_iter_recurse(&entry, &value);
324 if (dbus_message_iter_get_arg_type(&value)
327 dbus_message_iter_get_basic(&value, &str);
328 username = g_strdup(str);
331 if (g_str_equal(key, "Password")) {
332 dbus_message_iter_next(&entry);
333 if (dbus_message_iter_get_arg_type(&entry)
334 != DBUS_TYPE_VARIANT)
336 dbus_message_iter_recurse(&entry, &value);
337 if (dbus_message_iter_get_arg_type(&value)
340 dbus_message_iter_get_basic(&value, &str);
341 password = g_strdup(str);
344 dbus_message_iter_next(&dict);
348 pptp_reply->callback(pptp_reply->provider, username, password, error,
349 pptp_reply->user_data);
357 typedef void (* request_cb_t)(struct vpn_provider *provider,
358 const char *username, const char *password,
359 const char *error, void *user_data);
361 static int request_input(struct vpn_provider *provider,
362 request_cb_t callback, void *user_data)
364 DBusMessage *message;
365 const char *path, *agent_sender, *agent_path;
366 DBusMessageIter iter;
367 DBusMessageIter dict;
368 struct request_input_reply *pptp_reply;
371 connman_agent_get_info(&agent_sender, &agent_path);
373 if (provider == NULL || agent_path == NULL || callback == NULL)
376 message = dbus_message_new_method_call(agent_sender, agent_path,
382 dbus_message_iter_init_append(message, &iter);
384 path = vpn_provider_get_path(provider);
385 dbus_message_iter_append_basic(&iter,
386 DBUS_TYPE_OBJECT_PATH, &path);
388 connman_dbus_dict_open(&iter, &dict);
390 vpn_agent_append_user_info(&dict, provider, "PPTP.User");
392 vpn_agent_append_host_and_name(&dict, provider);
394 connman_dbus_dict_close(&iter, &dict);
396 pptp_reply = g_try_new0(struct request_input_reply, 1);
397 if (pptp_reply == NULL) {
398 dbus_message_unref(message);
402 pptp_reply->provider = provider;
403 pptp_reply->callback = callback;
404 pptp_reply->user_data = user_data;
406 err = connman_agent_queue_message(provider, message,
407 connman_timeout_input_request(),
408 request_input_reply, pptp_reply);
409 if (err < 0 && err != -EBUSY) {
410 DBG("error %d sending agent request", err);
411 dbus_message_unref(message);
416 dbus_message_unref(message);
421 static int run_connect(struct vpn_provider *provider,
422 struct connman_task *task, const char *if_name,
423 vpn_provider_connect_cb_t cb, void *user_data,
424 const char *username, const char *password)
426 const char *opt_s, *host;
430 host = vpn_provider_get_string(provider, "Host");
432 connman_error("Host not set; cannot enable VPN");
437 if (username == NULL || password == NULL) {
438 DBG("Cannot connect username %s password %p",
444 DBG("username %s password %p", username, password);
446 str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
449 connman_error("can not allocate memory");
454 connman_task_add_argument(task, "pty", str);
457 connman_task_add_argument(task, "nodetach", NULL);
458 connman_task_add_argument(task, "lock", NULL);
459 connman_task_add_argument(task, "usepeerdns", NULL);
460 connman_task_add_argument(task, "noipdefault", NULL);
461 connman_task_add_argument(task, "noauth", NULL);
462 connman_task_add_argument(task, "nodefaultroute", NULL);
463 connman_task_add_argument(task, "ipparam", "pptp_plugin");
465 for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
466 opt_s = vpn_provider_get_string(provider,
467 pptp_options[i].cm_opt);
469 opt_s = pptp_options[i].vpnc_default;
474 if (pptp_options[i].type == OPT_STRING)
475 connman_task_add_argument(task,
476 pptp_options[i].pptp_opt, opt_s);
477 else if (pptp_options[i].type == OPT_BOOL)
478 pptp_write_bool_option(task,
479 pptp_options[i].pptp_opt, opt_s);
482 connman_task_add_argument(task, "plugin",
483 SCRIPTDIR "/libppp-plugin.so");
485 err = connman_task_run(task, vpn_died, provider,
488 connman_error("pptp failed to start");
495 cb(provider, user_data, err);
500 static void free_private_data(struct pptp_private_data *data)
502 g_free(data->if_name);
506 static void request_input_cb(struct vpn_provider *provider,
507 const char *username,
508 const char *password,
509 const char *error, void *user_data)
511 struct pptp_private_data *data = user_data;
513 if (username == NULL || password == NULL)
514 DBG("Requesting username %s or password failed, error %s",
516 else if (error != NULL)
517 DBG("error %s", error);
519 vpn_provider_set_string(provider, "PPTP.User", username);
520 vpn_provider_set_string_hide_value(provider, "PPTP.Password",
523 run_connect(provider, data->task, data->if_name, data->cb,
524 data->user_data, username, password);
526 free_private_data(data);
529 static int pptp_connect(struct vpn_provider *provider,
530 struct connman_task *task, const char *if_name,
531 vpn_provider_connect_cb_t cb, void *user_data)
533 const char *username, *password;
536 DBG("iface %s provider %p user %p", if_name, provider, user_data);
538 if (connman_task_set_notify(task, "getsec",
539 pptp_get_sec, provider)) {
544 username = vpn_provider_get_string(provider, "PPTP.User");
545 password = vpn_provider_get_string(provider, "PPTP.Password");
547 DBG("user %s password %p", username, password);
549 if (username == NULL || password == NULL) {
550 struct pptp_private_data *data;
552 data = g_try_new0(struct pptp_private_data, 1);
557 data->if_name = g_strdup(if_name);
559 data->user_data = user_data;
561 err = request_input(provider, request_input_cb, data);
562 if (err != -EINPROGRESS) {
563 free_private_data(data);
570 return run_connect(provider, task, if_name, cb, user_data,
575 cb(provider, user_data, err);
580 static int pptp_error_code(int exit_code)
585 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
587 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
589 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
591 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
595 static void pptp_disconnect(struct vpn_provider *provider)
597 vpn_provider_set_string(provider, "PPTP.Password", NULL);
600 static struct vpn_driver vpn_driver = {
601 .flags = VPN_FLAG_NO_TUN,
602 .notify = pptp_notify,
603 .connect = pptp_connect,
604 .error_code = pptp_error_code,
606 .disconnect = pptp_disconnect,
609 static int pptp_init(void)
611 connection = connman_dbus_get_connection();
613 return vpn_register("pptp", &vpn_driver, PPPD);
616 static void pptp_exit(void)
618 vpn_unregister("pptp");
620 dbus_connection_unref(connection);
623 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
624 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)