5 * Copyright (C) 2010-2014 BMW Car IT GmbH.
6 * Copyright (C) 2016-2019 Jolla Ltd.
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 <linux/if_tun.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 #define CONNMAN_API_SUBJECT_TO_CHANGE
41 #include <connman/plugin.h>
42 #include <connman/log.h>
43 #include <connman/task.h>
44 #include <connman/dbus.h>
45 #include <connman/ipconfig.h>
46 #include <connman/agent.h>
47 #include <connman/setting.h>
48 #include <connman/vpn-dbus.h>
50 #include "../vpn-provider.h"
51 #include "../vpn-agent.h"
55 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
57 static DBusConnection *connection;
64 { "Host", "--remote", 1 },
65 { "OpenVPN.CACert", "--ca", 1 },
66 { "OpenVPN.Cert", "--cert", 1 },
67 { "OpenVPN.Key", "--key", 1 },
68 { "OpenVPN.MTU", "--tun-mtu", 1 },
69 { "OpenVPN.NSCertType", "--ns-cert-type", 1 },
70 { "OpenVPN.Proto", "--proto", 1 },
71 { "OpenVPN.Port", "--port", 1 },
72 { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
73 { "OpenVPN.AskPass", "--askpass", 1 },
74 { "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
75 { "OpenVPN.TLSRemote", "--tls-remote", 1 },
76 { "OpenVPN.TLSAuth", NULL, 1 },
77 { "OpenVPN.TLSAuthDir", NULL, 1 },
78 { "OpenVPN.TLSCipher", "--tls-cipher", 1},
79 { "OpenVPN.Cipher", "--cipher", 1 },
80 { "OpenVPN.Auth", "--auth", 1 },
81 { "OpenVPN.CompLZO", "--comp-lzo", 0 },
82 { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
83 { "OpenVPN.ConfigFile", "--config", 1 },
84 { "OpenVPN.DeviceType", NULL, 1 },
85 { "OpenVPN.Verb", "--verb", 1 },
86 { "OpenVPN.Ping", "--ping", 1},
87 { "OpenVPN.PingExit", "--ping-exit", 1},
88 { "OpenVPN.RemapUsr1", "--remap-usr1", 1},
91 struct ov_private_data {
92 struct vpn_provider *provider;
93 struct connman_task *task;
96 vpn_provider_connect_cb_t cb;
101 GIOChannel *mgmt_channel;
102 int connect_attempts;
103 int failed_attempts_privatekey;
106 static void ov_connect_done(struct ov_private_data *data, int err)
108 if (data && data->cb) {
109 vpn_provider_connect_cb_t cb = data->cb;
110 void *user_data = data->user_data;
112 /* Make sure we don't invoke this callback twice */
114 data->user_data = NULL;
115 cb(data->provider, user_data, err);
119 data->failed_attempts_privatekey = 0;
122 static void free_private_data(struct ov_private_data *data)
124 if (vpn_provider_get_plugin_data(data->provider) == data)
125 vpn_provider_set_plugin_data(data->provider, NULL);
127 ov_connect_done(data, EIO);
128 vpn_provider_unref(data->provider);
129 g_free(data->dbus_sender);
130 g_free(data->if_name);
131 g_free(data->mgmt_path);
135 struct nameserver_entry {
140 static struct nameserver_entry *ov_append_dns_entries(const char *key,
143 struct nameserver_entry *entry = NULL;
146 if (!g_str_has_prefix(key, "foreign_option_"))
149 options = g_strsplit(value, " ", 3);
151 !strcmp(options[0], "dhcp-option") &&
153 !strcmp(options[1], "DNS") &&
156 entry = g_try_new(struct nameserver_entry, 1);
160 entry->nameserver = g_strdup(options[2]);
161 entry->id = atoi(key + 15); /* foreign_option_XXX */
169 static char *ov_get_domain_name(const char *key, const char *value)
174 if (!g_str_has_prefix(key, "foreign_option_"))
177 options = g_strsplit(value, " ", 3);
179 !strcmp(options[0], "dhcp-option") &&
181 !strcmp(options[1], "DOMAIN") &&
184 domain = g_strdup(options[2]);
192 static gint cmp_ns(gconstpointer a, gconstpointer b)
194 struct nameserver_entry *entry_a = (struct nameserver_entry *)a;
195 struct nameserver_entry *entry_b = (struct nameserver_entry *)b;
197 if (entry_a->id < entry_b->id)
200 if (entry_a->id > entry_b->id)
206 static void free_ns_entry(gpointer data)
208 struct nameserver_entry *entry = data;
210 g_free(entry->nameserver);
214 static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
216 DBusMessageIter iter, dict;
217 const char *reason, *key, *value;
218 char *address = NULL, *gateway = NULL, *peer = NULL, *netmask = NULL;
219 struct connman_ipaddress *ipaddress;
220 GSList *nameserver_list = NULL;
221 struct ov_private_data *data = vpn_provider_get_plugin_data(provider);
223 dbus_message_iter_init(msg, &iter);
225 dbus_message_iter_get_basic(&iter, &reason);
226 dbus_message_iter_next(&iter);
229 connman_error("No provider found");
230 return VPN_STATE_FAILURE;
233 DBG("%p %s", vpn_provider_get_name(provider), reason);
235 if (strcmp(reason, "up")) {
236 ov_connect_done(data, EIO);
237 return VPN_STATE_DISCONNECT;
240 dbus_message_iter_recurse(&iter, &dict);
242 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
243 struct nameserver_entry *ns_entry = NULL;
244 DBusMessageIter entry;
246 dbus_message_iter_recurse(&dict, &entry);
247 dbus_message_iter_get_basic(&entry, &key);
248 dbus_message_iter_next(&entry);
249 dbus_message_iter_get_basic(&entry, &value);
251 DBG("%s = %s", key, value);
253 if (!strcmp(key, "trusted_ip"))
254 gateway = g_strdup(value);
256 if (!strcmp(key, "ifconfig_local"))
257 address = g_strdup(value);
259 if (!strcmp(key, "ifconfig_netmask"))
260 netmask = g_strdup(value);
262 if (!strcmp(key, "ifconfig_remote"))
263 peer = g_strdup(value);
265 if (g_str_has_prefix(key, "route_"))
266 vpn_provider_append_route(provider, key, value);
268 if ((ns_entry = ov_append_dns_entries(key, value)))
269 nameserver_list = g_slist_prepend(nameserver_list,
272 char *domain = ov_get_domain_name(key, value);
274 vpn_provider_set_domain(provider, domain);
279 dbus_message_iter_next(&dict);
282 ipaddress = connman_ipaddress_alloc(AF_INET);
284 g_slist_free_full(nameserver_list, free_ns_entry);
290 return VPN_STATE_FAILURE;
293 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
294 connman_ipaddress_set_peer(ipaddress, peer);
295 connman_ipaddress_set_p2p(ipaddress, true);
296 vpn_provider_set_ipaddress(provider, ipaddress);
298 if (nameserver_list) {
299 char *nameservers = NULL;
302 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
303 for (tmp = nameserver_list; tmp;
304 tmp = g_slist_next(tmp)) {
305 struct nameserver_entry *ns = tmp->data;
308 nameservers = g_strdup(ns->nameserver);
311 str = g_strjoin(" ", nameservers,
312 ns->nameserver, NULL);
318 g_slist_free_full(nameserver_list, free_ns_entry);
320 vpn_provider_set_nameservers(provider, nameservers);
329 connman_ipaddress_free(ipaddress);
331 ov_connect_done(data, 0);
332 return VPN_STATE_CONNECT;
335 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
340 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
341 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
342 option = vpn_provider_get_string(provider,
343 ov_options[i].cm_opt);
347 g_key_file_set_string(keyfile,
348 vpn_provider_get_save_group(provider),
349 ov_options[i].cm_opt, option);
355 static int task_append_config_data(struct vpn_provider *provider,
356 struct connman_task *task)
361 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
362 if (!ov_options[i].ov_opt)
365 option = vpn_provider_get_string(provider,
366 ov_options[i].cm_opt);
371 * If the AuthUserPass option is "-", provide the input
372 * via management interface
374 if (!strcmp(ov_options[i].cm_opt, "OpenVPN.AuthUserPass") &&
375 !strcmp(option, "-"))
378 if (connman_task_add_argument(task,
379 ov_options[i].ov_opt,
380 ov_options[i].has_value ? option : NULL) < 0)
388 static void close_management_interface(struct ov_private_data *data)
390 if (data->mgmt_path) {
391 if (unlink(data->mgmt_path) && errno != ENOENT)
392 connman_warn("Unable to unlink management socket %s: "
393 "%d", data->mgmt_path, errno);
395 g_free(data->mgmt_path);
396 data->mgmt_path = NULL;
399 if (data->mgmt_timer_id != 0) {
400 g_source_remove(data->mgmt_timer_id);
401 data->mgmt_timer_id = 0;
404 if (data->mgmt_event_id) {
405 g_source_remove(data->mgmt_event_id);
406 data->mgmt_event_id = 0;
409 if (data->mgmt_channel) {
410 g_io_channel_shutdown(data->mgmt_channel, FALSE, NULL);
411 g_io_channel_unref(data->mgmt_channel);
412 data->mgmt_channel = NULL;
416 static void ov_died(struct connman_task *task, int exit_code, void *user_data)
418 struct ov_private_data *data = user_data;
420 /* Cancel any pending agent requests */
421 connman_agent_cancel(data->provider);
423 close_management_interface(data);
425 vpn_died(task, exit_code, data->provider);
427 free_private_data(data);
430 static int run_connect(struct ov_private_data *data,
431 vpn_provider_connect_cb_t cb, void *user_data)
433 struct vpn_provider *provider = data->provider;
434 struct connman_task *task = data->task;
438 option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
441 * Set some default options if user has no config file.
443 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
445 connman_task_add_argument(task, "--tls-auth", option);
446 option = vpn_provider_get_string(provider,
447 "OpenVPN.TLSAuthDir");
449 connman_task_add_argument(task, option, NULL);
452 connman_task_add_argument(task, "--nobind", NULL);
453 connman_task_add_argument(task, "--persist-key", NULL);
454 connman_task_add_argument(task, "--client", NULL);
457 if (data->mgmt_path) {
458 connman_task_add_argument(task, "--management", NULL);
459 connman_task_add_argument(task, data->mgmt_path, NULL);
460 connman_task_add_argument(task, "unix", NULL);
461 connman_task_add_argument(task, "--management-query-passwords",
463 connman_task_add_argument(task, "--auth-retry", "interact");
466 connman_task_add_argument(task, "--syslog", NULL);
468 connman_task_add_argument(task, "--script-security", "2");
470 connman_task_add_argument(task, "--up",
471 SCRIPTDIR "/openvpn-script");
472 connman_task_add_argument(task, "--up-restart", NULL);
474 connman_task_add_argument(task, "--setenv", NULL);
475 connman_task_add_argument(task, "CONNMAN_BUSNAME",
476 dbus_bus_get_unique_name(connection));
478 connman_task_add_argument(task, "--setenv", NULL);
479 connman_task_add_argument(task, "CONNMAN_INTERFACE",
480 CONNMAN_TASK_INTERFACE);
482 connman_task_add_argument(task, "--setenv", NULL);
483 connman_task_add_argument(task, "CONNMAN_PATH",
484 connman_task_get_path(task));
486 connman_task_add_argument(task, "--dev", data->if_name);
487 option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
489 connman_task_add_argument(task, "--dev-type", option);
492 * Default to tun for backwards compatibility.
494 connman_task_add_argument(task, "--dev-type", "tun");
497 connman_task_add_argument(task, "--persist-tun", NULL);
499 connman_task_add_argument(task, "--route-noexec", NULL);
500 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
503 * Disable client restarts with TCP because we can't handle this at
504 * the moment. The problem is that when OpenVPN decides to switch
505 * from CONNECTED state to RECONNECTING and then to RESOLVE,
506 * it is not possible to do a DNS lookup. The DNS server is
507 * not accessible through the tunnel anymore and so we end up
508 * trying to resolve the OpenVPN servers address.
510 * Disable connetion retrying when OpenVPN is connected over TCP.
511 * With TCP OpenVPN attempts to handle reconnection silently without
512 * reporting the error back when establishing a connection or
513 * reconnecting as succesful one. The latter causes trouble if the
514 * retries are not limited to 1 (no retry) as the interface is up and
515 * connman regards it as the default route and network ceases to work,
518 option = vpn_provider_get_string(provider, "OpenVPN.Proto");
519 if (option && g_str_has_prefix(option, "tcp")) {
520 option = vpn_provider_get_string(provider, "OpenVPN.PingExit");
522 connman_task_add_argument(task, "--ping-restart", "0");
524 connman_task_add_argument(task, "--connect-retry-max", "1");
525 /* Apply defaults for --ping and --ping-exit only with UDP protocol. */
527 /* Apply default of 10 second interval for ping if omitted. */
528 option = vpn_provider_get_string(provider, "OpenVPN.Ping");
530 connman_task_add_argument(task, "--ping", "10");
532 /* Apply default of 60 seconds for ping exit if omitted. */
533 option = vpn_provider_get_string(provider, "OpenVPN.PingExit");
535 connman_task_add_argument(task, "--ping-exit", "60");
538 err = connman_task_run(task, ov_died, data, NULL, NULL, NULL);
541 data->user_data = NULL;
542 connman_error("openvpn failed to start");
545 /* This lets the caller know that the actual result of
546 * the operation will be reported to the callback */
551 static void ov_quote_credential(GString *line, const char *cred)
556 g_string_append_c(line, '"');
558 while (*cred != '\0') {
564 g_string_append_c(line, '\\');
570 g_string_append_c(line, *cred++);
573 g_string_append_c(line, '"');
576 static void ov_return_credentials(struct ov_private_data *data,
577 const char *username, const char *password)
579 GString *reply_string;
583 reply_string = g_string_new(NULL);
585 g_string_append(reply_string, "username \"Auth\" ");
586 ov_quote_credential(reply_string, username);
587 g_string_append_c(reply_string, '\n');
589 g_string_append(reply_string, "password \"Auth\" ");
590 ov_quote_credential(reply_string, password);
591 g_string_append_c(reply_string, '\n');
593 len = reply_string->len;
594 reply = g_string_free(reply_string, FALSE);
596 g_io_channel_write_chars(data->mgmt_channel, reply, len, NULL, NULL);
597 g_io_channel_flush(data->mgmt_channel, NULL);
599 memset(reply, 0, len);
603 static void ov_return_private_key_password(struct ov_private_data *data,
604 const char *privatekeypass)
606 GString *reply_string;
610 reply_string = g_string_new(NULL);
612 g_string_append(reply_string, "password \"Private Key\" ");
613 ov_quote_credential(reply_string, privatekeypass);
614 g_string_append_c(reply_string, '\n');
616 len = reply_string->len;
617 reply = g_string_free(reply_string, FALSE);
619 g_io_channel_write_chars(data->mgmt_channel, reply, len, NULL, NULL);
620 g_io_channel_flush(data->mgmt_channel, NULL);
622 memset(reply, 0, len);
626 static void request_input_append_informational(DBusMessageIter *iter,
629 char *str = "string";
631 connman_dbus_dict_append_basic(iter, "Type",
632 DBUS_TYPE_STRING, &str);
633 str = "informational";
634 connman_dbus_dict_append_basic(iter, "Requirement",
635 DBUS_TYPE_STRING, &str);
638 static void request_input_append_mandatory(DBusMessageIter *iter,
641 char *str = "string";
643 connman_dbus_dict_append_basic(iter, "Type",
644 DBUS_TYPE_STRING, &str);
646 connman_dbus_dict_append_basic(iter, "Requirement",
647 DBUS_TYPE_STRING, &str);
650 static void request_input_append_password(DBusMessageIter *iter,
653 char *str = "password";
655 connman_dbus_dict_append_basic(iter, "Type",
656 DBUS_TYPE_STRING, &str);
658 connman_dbus_dict_append_basic(iter, "Requirement",
659 DBUS_TYPE_STRING, &str);
662 static void request_input_credentials_reply(DBusMessage *reply,
665 struct ov_private_data *data = user_data;
666 char *username = NULL;
667 char *password = NULL;
669 DBusMessageIter iter, dict;
673 connman_info("provider %p", data->provider);
676 * When connmand calls disconnect because of connection timeout no
684 dbus_error_init(&error);
686 err = vpn_agent_check_and_process_reply_error(reply, data->provider,
687 data->task, data->cb, data->user_data);
689 /* Ensure cb is called only once */
691 data->user_data = NULL;
695 if (!vpn_agent_check_reply_has_dict(reply)) {
700 dbus_message_iter_init(reply, &iter);
701 dbus_message_iter_recurse(&iter, &dict);
702 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
703 DBusMessageIter entry, value;
705 dbus_message_iter_recurse(&dict, &entry);
706 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
709 dbus_message_iter_get_basic(&entry, &key);
711 if (g_str_equal(key, "OpenVPN.Password")) {
712 dbus_message_iter_next(&entry);
713 if (dbus_message_iter_get_arg_type(&entry)
714 != DBUS_TYPE_VARIANT)
716 dbus_message_iter_recurse(&entry, &value);
717 if (dbus_message_iter_get_arg_type(&value)
720 dbus_message_iter_get_basic(&value, &password);
721 vpn_provider_set_string_hide_value(data->provider,
724 } else if (g_str_equal(key, "OpenVPN.Username")) {
725 dbus_message_iter_next(&entry);
726 if (dbus_message_iter_get_arg_type(&entry)
727 != DBUS_TYPE_VARIANT)
729 dbus_message_iter_recurse(&entry, &value);
730 if (dbus_message_iter_get_arg_type(&value)
733 dbus_message_iter_get_basic(&value, &username);
734 vpn_provider_set_string_hide_value(data->provider,
738 dbus_message_iter_next(&dict);
741 if (!password || !username) {
742 vpn_provider_indicate_error(data->provider,
743 VPN_PROVIDER_ERROR_AUTH_FAILED);
748 ov_return_credentials(data, username, password);
753 ov_connect_done(data, err);
756 static int request_credentials_input(struct ov_private_data *data)
758 DBusMessage *message;
759 const char *path, *agent_sender, *agent_path;
760 DBusMessageIter iter;
761 DBusMessageIter dict;
765 agent = connman_agent_get_info(data->dbus_sender, &agent_sender,
767 if (!agent || !agent_path)
770 message = dbus_message_new_method_call(agent_sender, agent_path,
776 dbus_message_iter_init_append(message, &iter);
778 path = vpn_provider_get_path(data->provider);
779 dbus_message_iter_append_basic(&iter,
780 DBUS_TYPE_OBJECT_PATH, &path);
782 connman_dbus_dict_open(&iter, &dict);
784 if (vpn_provider_get_authentication_errors(data->provider))
785 vpn_agent_append_auth_failure(&dict, data->provider, NULL);
787 /* Request temporary properties to pass on to openvpn */
788 connman_dbus_dict_append_dict(&dict, "OpenVPN.Username",
789 request_input_append_mandatory, NULL);
791 connman_dbus_dict_append_dict(&dict, "OpenVPN.Password",
792 request_input_append_password, NULL);
794 vpn_agent_append_host_and_name(&dict, data->provider);
796 connman_dbus_dict_close(&iter, &dict);
798 err = connman_agent_queue_message(data->provider, message,
799 connman_timeout_input_request(),
800 request_input_credentials_reply, data, agent);
802 if (err < 0 && err != -EBUSY) {
803 connman_error("error %d sending agent request", err);
804 dbus_message_unref(message);
809 dbus_message_unref(message);
814 static void request_input_private_key_reply(DBusMessage *reply,
817 struct ov_private_data *data = user_data;
818 const char *privatekeypass = NULL;
820 DBusMessageIter iter, dict;
824 connman_info("provider %p", data->provider);
827 * When connmand calls disconnect because of connection timeout no
835 dbus_error_init(&error);
837 err = vpn_agent_check_and_process_reply_error(reply, data->provider,
838 data->task, data->cb, data->user_data);
840 /* Ensure cb is called only once */
842 data->user_data = NULL;
846 if (!vpn_agent_check_reply_has_dict(reply)) {
851 dbus_message_iter_init(reply, &iter);
852 dbus_message_iter_recurse(&iter, &dict);
853 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
854 DBusMessageIter entry, value;
856 dbus_message_iter_recurse(&dict, &entry);
857 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
860 dbus_message_iter_get_basic(&entry, &key);
862 if (g_str_equal(key, "OpenVPN.PrivateKeyPassword")) {
863 dbus_message_iter_next(&entry);
864 if (dbus_message_iter_get_arg_type(&entry)
865 != DBUS_TYPE_VARIANT)
867 dbus_message_iter_recurse(&entry, &value);
868 if (dbus_message_iter_get_arg_type(&value)
871 dbus_message_iter_get_basic(&value, &privatekeypass);
872 vpn_provider_set_string_hide_value(data->provider,
873 key, privatekeypass);
877 dbus_message_iter_next(&dict);
880 if (!privatekeypass) {
881 vpn_provider_indicate_error(data->provider,
882 VPN_PROVIDER_ERROR_AUTH_FAILED);
888 ov_return_private_key_password(data, privatekeypass);
893 ov_connect_done(data, err);
896 static int request_private_key_input(struct ov_private_data *data)
898 DBusMessage *message;
899 const char *path, *agent_sender, *agent_path;
900 const char *privatekeypass;
901 DBusMessageIter iter;
902 DBusMessageIter dict;
907 * First check if this is the second attempt to get the key within
908 * this connection. In such case there has been invalid Private Key
909 * Password and it must be reset, and queried from user.
911 if (data->failed_attempts_privatekey) {
912 vpn_provider_set_string_hide_value(data->provider,
913 "OpenVPN.PrivateKeyPassword", NULL);
915 /* If the encrypted Private key password is kept in memory and
916 * use it first. If authentication fails this is cleared,
917 * likewise it is when connman-vpnd is restarted.
919 privatekeypass = vpn_provider_get_string(data->provider,
920 "OpenVPN.PrivateKeyPassword");
921 if (privatekeypass) {
922 ov_return_private_key_password(data, privatekeypass);
927 agent = connman_agent_get_info(data->dbus_sender, &agent_sender,
929 if (!agent || !agent_path)
932 message = dbus_message_new_method_call(agent_sender, agent_path,
938 dbus_message_iter_init_append(message, &iter);
940 path = vpn_provider_get_path(data->provider);
941 dbus_message_iter_append_basic(&iter,
942 DBUS_TYPE_OBJECT_PATH, &path);
944 connman_dbus_dict_open(&iter, &dict);
946 connman_dbus_dict_append_dict(&dict, "OpenVPN.PrivateKeyPassword",
947 request_input_append_password, NULL);
949 vpn_agent_append_host_and_name(&dict, data->provider);
951 /* Do not allow to store or retrieve the encrypted Private Key pass */
952 vpn_agent_append_allow_credential_storage(&dict, false);
953 vpn_agent_append_allow_credential_retrieval(&dict, false);
956 * Indicate to keep credentials, the enc Private Key password should
957 * not affect the credential storing.
959 vpn_agent_append_keep_credentials(&dict, true);
961 connman_dbus_dict_append_dict(&dict, "Enter Private Key password",
962 request_input_append_informational, NULL);
964 connman_dbus_dict_close(&iter, &dict);
966 err = connman_agent_queue_message(data->provider, message,
967 connman_timeout_input_request(),
968 request_input_private_key_reply, data, agent);
970 if (err < 0 && err != -EBUSY) {
971 connman_error("error %d sending agent request", err);
972 dbus_message_unref(message);
977 dbus_message_unref(message);
983 static gboolean ov_management_handle_input(GIOChannel *source,
984 GIOCondition condition, gpointer user_data)
986 struct ov_private_data *data = user_data;
991 if (condition & G_IO_IN) {
993 * Just return if line is not read and str is not allocated.
994 * Condition check handles closing of the channel later.
996 if (g_io_channel_read_line(source, &str, NULL, NULL, NULL) !=
1000 str[strlen(str) - 1] = '\0';
1001 connman_warn("openvpn request %s", str);
1003 if (g_str_has_prefix(str, ">PASSWORD:Need 'Auth'")) {
1005 * Request credentials from the user
1007 err = request_credentials_input(data);
1008 if (err != -EINPROGRESS)
1010 } else if (g_str_has_prefix(str,
1011 ">PASSWORD:Need 'Private Key'")) {
1012 err = request_private_key_input(data);
1013 if (err != -EINPROGRESS)
1015 } else if (g_str_has_prefix(str,
1016 ">PASSWORD:Verification Failed: 'Auth'")) {
1018 * Add error only, state change indication causes
1019 * signal to be sent, which is not desired when
1020 * OpenVPN is in interactive mode.
1022 vpn_provider_add_error(data->provider,
1023 VPN_PROVIDER_ERROR_AUTH_FAILED);
1025 * According to the OpenVPN manual about management interface
1026 * https://openvpn.net/community-resources/management-interface/
1027 * this should be received but it does not seem to be reported
1028 * when decrypting private key fails. This requires following
1029 * patch for OpenVPN (at least <= 2.4.5) in order to work:
1030 * https://git.sailfishos.org/mer-core/openvpn/blob/
1031 * 4f4b4af116292a207416c8a990392e35a6fc41af/rpm/privatekey-
1032 * passphrase-handling.diff
1034 } else if (g_str_has_prefix(str, ">PASSWORD:Verification "
1035 "Failed: 'Private Key'")) {
1036 data->failed_attempts_privatekey++;
1040 } else if (condition & (G_IO_ERR | G_IO_HUP)) {
1041 connman_warn("Management channel termination");
1046 close_management_interface(data);
1051 static int ov_management_connect_timer_cb(gpointer user_data)
1053 struct ov_private_data *data = user_data;
1055 if (!data->mgmt_channel) {
1056 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
1058 struct sockaddr_un remote;
1061 memset(&remote, 0, sizeof(remote));
1062 remote.sun_family = AF_UNIX;
1063 g_strlcpy(remote.sun_path, data->mgmt_path,
1064 sizeof(remote.sun_path));
1066 err = connect(fd, (struct sockaddr *)&remote,
1069 data->mgmt_channel = g_io_channel_unix_new(fd);
1070 data->mgmt_event_id =
1071 g_io_add_watch(data->mgmt_channel,
1072 G_IO_IN | G_IO_ERR | G_IO_HUP,
1073 ov_management_handle_input,
1076 connman_warn("Connected management socket");
1077 data->mgmt_timer_id = 0;
1078 return G_SOURCE_REMOVE;
1084 data->connect_attempts++;
1085 if (data->connect_attempts > 30) {
1086 connman_warn("Unable to connect management socket");
1087 data->mgmt_timer_id = 0;
1088 return G_SOURCE_REMOVE;
1091 return G_SOURCE_CONTINUE;
1094 static int ov_connect(struct vpn_provider *provider,
1095 struct connman_task *task, const char *if_name,
1096 vpn_provider_connect_cb_t cb, const char *dbus_sender,
1100 struct ov_private_data *data;
1102 data = g_try_new0(struct ov_private_data, 1);
1106 vpn_provider_set_plugin_data(provider, data);
1107 data->provider = vpn_provider_ref(provider);
1109 data->dbus_sender = g_strdup(dbus_sender);
1110 data->if_name = g_strdup(if_name);
1112 data->user_data = user_data;
1115 * We need to use the management interface to provide
1116 * the user credentials and password for decrypting private key.
1119 /* Use env TMPDIR for creating management socket, fall back to /tmp */
1120 tmpdir = getenv("TMPDIR");
1121 if (!tmpdir || !*tmpdir)
1124 /* Set up the path for the management interface */
1125 data->mgmt_path = g_strconcat(tmpdir, "/connman-vpn-management-",
1126 vpn_provider_get_ident(provider), NULL);
1127 if (unlink(data->mgmt_path) != 0 && errno != ENOENT) {
1128 connman_warn("Unable to unlink management socket %s: %d",
1129 data->mgmt_path, errno);
1132 data->mgmt_timer_id = g_timeout_add(200,
1133 ov_management_connect_timer_cb, data);
1135 task_append_config_data(provider, task);
1137 return run_connect(data, cb, user_data);
1140 static void ov_disconnect(struct vpn_provider *provider)
1145 connman_agent_cancel(provider);
1147 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_DISCONNECT);
1150 static int ov_device_flags(struct vpn_provider *provider)
1154 option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
1159 if (g_str_equal(option, "tap")) {
1163 if (!g_str_equal(option, "tun")) {
1164 connman_warn("bad OpenVPN.DeviceType value "
1165 "falling back to tun");
1171 static int ov_route_env_parse(struct vpn_provider *provider, const char *key,
1172 int *family, unsigned long *idx,
1173 enum vpn_provider_route_type *type)
1178 if (g_str_has_prefix(key, "route_network_")) {
1179 start = key + strlen("route_network_");
1180 *type = VPN_PROVIDER_ROUTE_TYPE_ADDR;
1181 } else if (g_str_has_prefix(key, "route_netmask_")) {
1182 start = key + strlen("route_netmask_");
1183 *type = VPN_PROVIDER_ROUTE_TYPE_MASK;
1184 } else if (g_str_has_prefix(key, "route_gateway_")) {
1185 start = key + strlen("route_gateway_");
1186 *type = VPN_PROVIDER_ROUTE_TYPE_GW;
1191 *idx = g_ascii_strtoull(start, &end, 10);
1196 static struct vpn_driver vpn_driver = {
1197 .notify = ov_notify,
1198 .connect = ov_connect,
1199 .disconnect = ov_disconnect,
1201 .device_flags = ov_device_flags,
1202 .route_env_parse = ov_route_env_parse,
1205 static int openvpn_init(void)
1207 connection = connman_dbus_get_connection();
1209 return vpn_register("openvpn", &vpn_driver, OPENVPN);
1212 static void openvpn_exit(void)
1214 vpn_unregister("openvpn");
1216 dbus_connection_unref(connection);
1219 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
1220 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)