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"
51 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
61 const char *vpnc_default;
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 },
82 static DBusConnection *connection;
84 struct pptp_private_data {
85 struct connman_task *task;
87 vpn_provider_connect_cb_t cb;
91 static DBusMessage *pptp_get_sec(struct connman_task *task,
92 DBusMessage *msg, void *user_data)
94 const char *user, *passwd;
95 struct vpn_provider *provider = user_data;
98 if (dbus_message_get_no_reply(msg) == TRUE)
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)
107 reply = dbus_message_new_method_return(msg);
111 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
112 DBUS_TYPE_STRING, &passwd,
117 static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
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;
125 dbus_message_iter_init(msg, &iter);
127 dbus_message_iter_get_basic(&iter, &reason);
128 dbus_message_iter_next(&iter);
130 if (provider == NULL) {
131 connman_error("No provider found");
132 return VPN_STATE_FAILURE;
135 if (strcmp(reason, "auth failed") == 0)
136 return VPN_STATE_AUTH_FAILURE;
138 if (strcmp(reason, "connect"))
139 return VPN_STATE_DISCONNECT;
141 dbus_message_iter_recurse(&iter, &dict);
143 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
144 DBusMessageIter entry;
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);
151 DBG("%s = %s", key, value);
153 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
154 vpn_provider_set_string(provider, "Address", value);
155 addressv4 = g_strdup(value);
158 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
159 vpn_provider_set_string(provider, "Netmask", value);
160 netmask = g_strdup(value);
163 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
164 vpn_provider_set_string(provider, "DNS", value);
165 nameservers = g_strdup(value);
168 if (!strcmp(key, "INTERNAL_IFNAME"))
169 ifname = g_strdup(value);
171 dbus_message_iter_next(&dict);
174 if (vpn_set_ifname(provider, ifname) < 0) {
179 return VPN_STATE_FAILURE;
182 if (addressv4 != NULL)
183 ipaddress = connman_ipaddress_alloc(AF_INET);
187 if (ipaddress == NULL) {
188 connman_error("No IP address for provider");
192 return VPN_STATE_FAILURE;
195 value = vpn_provider_get_string(provider, "HostIP");
197 vpn_provider_set_string(provider, "Gateway", value);
198 gateway = g_strdup(value);
201 if (addressv4 != NULL)
202 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
205 vpn_provider_set_ipaddress(provider, ipaddress);
206 vpn_provider_set_nameservers(provider, nameservers);
212 connman_ipaddress_free(ipaddress);
214 return VPN_STATE_CONNECT;
217 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
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);
229 g_key_file_set_string(keyfile,
230 vpn_provider_get_save_group(provider),
231 pptp_options[i].cm_opt, option);
238 static void pptp_write_bool_option(struct connman_task *task,
239 const char *key, const char *value)
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);
249 struct request_input_reply {
250 struct vpn_provider *provider;
251 vpn_provider_password_cb_t callback;
255 static void request_input_reply(DBusMessage *reply, void *user_data)
257 struct request_input_reply *pptp_reply = user_data;
258 const char *error = NULL;
259 char *username = NULL, *password = NULL;
261 DBusMessageIter iter, dict;
263 DBG("provider %p", pptp_reply->provider);
265 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
266 error = dbus_message_get_error_name(reply);
270 if (vpn_agent_check_reply_has_dict(reply) == FALSE)
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;
279 dbus_message_iter_recurse(&dict, &entry);
280 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
283 dbus_message_iter_get_basic(&entry, &key);
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)
290 dbus_message_iter_recurse(&entry, &value);
291 if (dbus_message_iter_get_arg_type(&value)
294 dbus_message_iter_get_basic(&value, &str);
295 username = g_strdup(str);
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)
303 dbus_message_iter_recurse(&entry, &value);
304 if (dbus_message_iter_get_arg_type(&value)
307 dbus_message_iter_get_basic(&value, &str);
308 password = g_strdup(str);
311 dbus_message_iter_next(&dict);
315 pptp_reply->callback(pptp_reply->provider, username, password, error,
316 pptp_reply->user_data);
324 typedef void (* request_cb_t)(struct vpn_provider *provider,
325 const char *username, const char *password,
326 const char *error, void *user_data);
328 static int request_input(struct vpn_provider *provider,
329 request_cb_t callback, void *user_data)
331 DBusMessage *message;
332 const char *path, *agent_sender, *agent_path;
333 DBusMessageIter iter;
334 DBusMessageIter dict;
335 struct request_input_reply *pptp_reply;
338 connman_agent_get_info(&agent_sender, &agent_path);
340 if (provider == NULL || agent_path == NULL || callback == NULL)
343 message = dbus_message_new_method_call(agent_sender, agent_path,
349 dbus_message_iter_init_append(message, &iter);
351 path = vpn_provider_get_path(provider);
352 dbus_message_iter_append_basic(&iter,
353 DBUS_TYPE_OBJECT_PATH, &path);
355 connman_dbus_dict_open(&iter, &dict);
357 vpn_agent_append_user_info(&dict, provider, "PPTP.User");
359 vpn_agent_append_host_and_name(&dict, provider);
361 connman_dbus_dict_close(&iter, &dict);
363 pptp_reply = g_try_new0(struct request_input_reply, 1);
364 if (pptp_reply == NULL) {
365 dbus_message_unref(message);
369 pptp_reply->provider = provider;
370 pptp_reply->callback = callback;
371 pptp_reply->user_data = user_data;
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);
383 dbus_message_unref(message);
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)
393 const char *opt_s, *host;
397 host = vpn_provider_get_string(provider, "Host");
399 connman_error("Host not set; cannot enable VPN");
404 if (username == NULL || password == NULL) {
405 DBG("Cannot connect username %s password %p",
411 vpn_provider_set_string(provider, "PPTP.User", username);
412 vpn_provider_set_string(provider, "PPTP.Password", password);
414 DBG("username %s password %p", username, password);
416 str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
419 connman_error("can not allocate memory");
424 connman_task_add_argument(task, "pty", str);
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");
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);
439 opt_s = pptp_options[i].vpnc_default;
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);
452 connman_task_add_argument(task, "plugin",
453 SCRIPTDIR "/libppp-plugin.so");
455 err = connman_task_run(task, vpn_died, provider,
458 connman_error("pptp failed to start");
465 cb(provider, user_data, err);
470 static void free_private_data(struct pptp_private_data *data)
472 g_free(data->if_name);
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)
481 struct pptp_private_data *data = user_data;
483 if (username == NULL || password == NULL)
484 DBG("Requesting username %s or password failed, error %s",
486 else if (error != NULL)
487 DBG("error %s", error);
489 run_connect(provider, data->task, data->if_name, data->cb,
490 data->user_data, username, password);
492 free_private_data(data);
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)
499 const char *username, *password;
502 DBG("iface %s provider %p user %p", if_name, provider, user_data);
504 if (connman_task_set_notify(task, "getsec",
505 pptp_get_sec, provider)) {
510 username = vpn_provider_get_string(provider, "PPTP.User");
511 password = vpn_provider_get_string(provider, "PPTP.Password");
513 DBG("user %s password %p", username, password);
515 if (username == NULL || password == NULL) {
516 struct pptp_private_data *data;
518 data = g_try_new0(struct pptp_private_data, 1);
523 data->if_name = g_strdup(if_name);
525 data->user_data = user_data;
527 err = request_input(provider, request_input_cb, data);
528 if (err != -EINPROGRESS) {
529 free_private_data(data);
536 return run_connect(provider, task, if_name, cb, user_data,
541 cb(provider, user_data, err);
546 static int pptp_error_code(int exit_code)
551 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
553 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
555 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
557 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
561 static void pptp_disconnect(struct vpn_provider *provider)
563 vpn_provider_set_string(provider, "PPTP.Password", NULL);
566 static struct vpn_driver vpn_driver = {
567 .flags = VPN_FLAG_NO_TUN,
568 .notify = pptp_notify,
569 .connect = pptp_connect,
570 .error_code = pptp_error_code,
572 .disconnect = pptp_disconnect,
575 static int pptp_init(void)
577 connection = connman_dbus_get_connection();
579 return vpn_register("pptp", &vpn_driver, PPPD);
582 static void pptp_exit(void)
584 vpn_unregister("pptp");
586 dbus_connection_unref(connection);
589 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
590 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)