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 { "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 },
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 return VPN_STATE_AUTH_FAILURE;
139 if (strcmp(reason, "connect"))
140 return VPN_STATE_DISCONNECT;
142 dbus_message_iter_recurse(&iter, &dict);
144 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
145 DBusMessageIter entry;
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);
152 DBG("%s = %s", key, value);
154 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
155 vpn_provider_set_string(provider, "Address", value);
156 addressv4 = g_strdup(value);
159 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
160 vpn_provider_set_string(provider, "Netmask", value);
161 netmask = g_strdup(value);
164 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
165 vpn_provider_set_string(provider, "DNS", value);
166 nameservers = g_strdup(value);
169 if (!strcmp(key, "INTERNAL_IFNAME"))
170 ifname = g_strdup(value);
172 dbus_message_iter_next(&dict);
175 if (vpn_set_ifname(provider, ifname) < 0) {
180 return VPN_STATE_FAILURE;
183 if (addressv4 != NULL)
184 ipaddress = connman_ipaddress_alloc(AF_INET);
188 if (ipaddress == NULL) {
189 connman_error("No IP address for provider");
193 return VPN_STATE_FAILURE;
196 value = vpn_provider_get_string(provider, "HostIP");
198 vpn_provider_set_string(provider, "Gateway", value);
199 gateway = g_strdup(value);
202 if (addressv4 != NULL)
203 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
206 vpn_provider_set_ipaddress(provider, ipaddress);
207 vpn_provider_set_nameservers(provider, nameservers);
213 connman_ipaddress_free(ipaddress);
215 return VPN_STATE_CONNECT;
218 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
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);
230 g_key_file_set_string(keyfile,
231 vpn_provider_get_save_group(provider),
232 pptp_options[i].cm_opt, option);
239 static void pptp_write_bool_option(struct connman_task *task,
240 const char *key, const char *value)
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);
250 struct request_input_reply {
251 struct vpn_provider *provider;
252 vpn_provider_password_cb_t callback;
256 static void request_input_reply(DBusMessage *reply, void *user_data)
258 struct request_input_reply *pptp_reply = user_data;
259 const char *error = NULL;
260 char *username = NULL, *password = NULL;
262 DBusMessageIter iter, dict;
264 DBG("provider %p", pptp_reply->provider);
266 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
267 error = dbus_message_get_error_name(reply);
271 if (vpn_agent_check_reply_has_dict(reply) == FALSE)
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;
280 dbus_message_iter_recurse(&dict, &entry);
281 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
284 dbus_message_iter_get_basic(&entry, &key);
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)
291 dbus_message_iter_recurse(&entry, &value);
292 if (dbus_message_iter_get_arg_type(&value)
295 dbus_message_iter_get_basic(&value, &str);
296 username = g_strdup(str);
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)
304 dbus_message_iter_recurse(&entry, &value);
305 if (dbus_message_iter_get_arg_type(&value)
308 dbus_message_iter_get_basic(&value, &str);
309 password = g_strdup(str);
312 dbus_message_iter_next(&dict);
316 pptp_reply->callback(pptp_reply->provider, username, password, error,
317 pptp_reply->user_data);
325 typedef void (* request_cb_t)(struct vpn_provider *provider,
326 const char *username, const char *password,
327 const char *error, void *user_data);
329 static int request_input(struct vpn_provider *provider,
330 request_cb_t callback, void *user_data)
332 DBusMessage *message;
333 const char *path, *agent_sender, *agent_path;
334 DBusMessageIter iter;
335 DBusMessageIter dict;
336 struct request_input_reply *pptp_reply;
339 connman_agent_get_info(&agent_sender, &agent_path);
341 if (provider == NULL || agent_path == NULL || callback == NULL)
344 message = dbus_message_new_method_call(agent_sender, agent_path,
350 dbus_message_iter_init_append(message, &iter);
352 path = vpn_provider_get_path(provider);
353 dbus_message_iter_append_basic(&iter,
354 DBUS_TYPE_OBJECT_PATH, &path);
356 connman_dbus_dict_open(&iter, &dict);
358 vpn_agent_append_user_info(&dict, provider, "PPTP.User");
360 vpn_agent_append_host_and_name(&dict, provider);
362 connman_dbus_dict_close(&iter, &dict);
364 pptp_reply = g_try_new0(struct request_input_reply, 1);
365 if (pptp_reply == NULL) {
366 dbus_message_unref(message);
370 pptp_reply->provider = provider;
371 pptp_reply->callback = callback;
372 pptp_reply->user_data = user_data;
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);
384 dbus_message_unref(message);
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)
394 const char *opt_s, *host;
398 host = vpn_provider_get_string(provider, "Host");
400 connman_error("Host not set; cannot enable VPN");
405 if (username == NULL || password == NULL) {
406 DBG("Cannot connect username %s password %p",
412 vpn_provider_set_string(provider, "PPTP.User", username);
413 vpn_provider_set_string(provider, "PPTP.Password", password);
415 DBG("username %s password %p", username, password);
417 str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
420 connman_error("can not allocate memory");
425 connman_task_add_argument(task, "pty", str);
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");
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);
440 opt_s = pptp_options[i].vpnc_default;
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);
453 connman_task_add_argument(task, "plugin",
454 SCRIPTDIR "/libppp-plugin.so");
456 err = connman_task_run(task, vpn_died, provider,
459 connman_error("pptp failed to start");
466 cb(provider, user_data, err);
471 static void free_private_data(struct pptp_private_data *data)
473 g_free(data->if_name);
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)
482 struct pptp_private_data *data = user_data;
484 if (username == NULL || password == NULL)
485 DBG("Requesting username %s or password failed, error %s",
487 else if (error != NULL)
488 DBG("error %s", error);
490 run_connect(provider, data->task, data->if_name, data->cb,
491 data->user_data, username, password);
493 free_private_data(data);
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)
500 const char *username, *password;
503 DBG("iface %s provider %p user %p", if_name, provider, user_data);
505 if (connman_task_set_notify(task, "getsec",
506 pptp_get_sec, provider)) {
511 username = vpn_provider_get_string(provider, "PPTP.User");
512 password = vpn_provider_get_string(provider, "PPTP.Password");
514 DBG("user %s password %p", username, password);
516 if (username == NULL || password == NULL) {
517 struct pptp_private_data *data;
519 data = g_try_new0(struct pptp_private_data, 1);
524 data->if_name = g_strdup(if_name);
526 data->user_data = user_data;
528 err = request_input(provider, request_input_cb, data);
529 if (err != -EINPROGRESS) {
530 free_private_data(data);
537 return run_connect(provider, task, if_name, cb, user_data,
542 cb(provider, user_data, err);
547 static int pptp_error_code(int exit_code)
552 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
554 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
556 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
558 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
562 static void pptp_disconnect(struct vpn_provider *provider)
564 vpn_provider_set_string(provider, "PPTP.Password", NULL);
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,
573 .disconnect = pptp_disconnect,
576 static int pptp_init(void)
578 connection = connman_dbus_get_connection();
580 return vpn_register("pptp", &vpn_driver, PPPD);
583 static void pptp_exit(void)
585 vpn_unregister("pptp");
587 dbus_connection_unref(connection);
590 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
591 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)