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"
56 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
58 static DBusConnection *connection;
65 { "Host", "--remote", 1 },
66 { "OpenVPN.CACert", "--ca", 1 },
67 { "OpenVPN.Cert", "--cert", 1 },
68 { "OpenVPN.Key", "--key", 1 },
69 { "OpenVPN.MTU", "--tun-mtu", 1 },
70 { "OpenVPN.NSCertType", "--ns-cert-type", 1 },
71 { "OpenVPN.Proto", "--proto", 1 },
72 { "OpenVPN.Port", "--port", 1 },
73 { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
74 { "OpenVPN.AskPass", "--askpass", 1 },
75 { "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
76 { "OpenVPN.TLSRemote", "--tls-remote", 1 },
77 { "OpenVPN.TLSAuth", NULL, 1 },
78 { "OpenVPN.TLSAuthDir", NULL, 1 },
79 { "OpenVPN.TLSCipher", "--tls-cipher", 1},
80 { "OpenVPN.Cipher", "--cipher", 1 },
81 { "OpenVPN.Auth", "--auth", 1 },
82 { "OpenVPN.CompLZO", "--comp-lzo", 0 },
83 { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
84 { "OpenVPN.ConfigFile", "--config", 1 },
85 { "OpenVPN.DeviceType", NULL, 1 },
86 { "OpenVPN.Verb", "--verb", 1 },
89 struct ov_private_data {
90 struct vpn_provider *provider;
91 struct connman_task *task;
94 vpn_provider_connect_cb_t cb;
99 GIOChannel *mgmt_channel;
100 int connect_attempts;
101 int failed_attempts_privatekey;
104 static void ov_connect_done(struct ov_private_data *data, int err)
106 if (data && data->cb) {
107 vpn_provider_connect_cb_t cb = data->cb;
108 void *user_data = data->user_data;
110 /* Make sure we don't invoke this callback twice */
112 data->user_data = NULL;
113 cb(data->provider, user_data, err);
117 data->failed_attempts_privatekey = 0;
120 static void free_private_data(struct ov_private_data *data)
122 if (vpn_provider_get_plugin_data(data->provider) == data)
123 vpn_provider_set_plugin_data(data->provider, NULL);
125 ov_connect_done(data, EIO);
126 vpn_provider_unref(data->provider);
127 g_free(data->dbus_sender);
128 g_free(data->if_name);
129 g_free(data->mgmt_path);
133 struct nameserver_entry {
138 static struct nameserver_entry *ov_append_dns_entries(const char *key,
141 struct nameserver_entry *entry = NULL;
144 if (!g_str_has_prefix(key, "foreign_option_"))
147 options = g_strsplit(value, " ", 3);
149 !strcmp(options[0], "dhcp-option") &&
151 !strcmp(options[1], "DNS") &&
154 entry = g_try_new(struct nameserver_entry, 1);
158 entry->nameserver = g_strdup(options[2]);
159 entry->id = atoi(key + 15); /* foreign_option_XXX */
167 static char *ov_get_domain_name(const char *key, const char *value)
172 if (!g_str_has_prefix(key, "foreign_option_"))
175 options = g_strsplit(value, " ", 3);
177 !strcmp(options[0], "dhcp-option") &&
179 !strcmp(options[1], "DOMAIN") &&
182 domain = g_strdup(options[2]);
190 static gint cmp_ns(gconstpointer a, gconstpointer b)
192 struct nameserver_entry *entry_a = (struct nameserver_entry *)a;
193 struct nameserver_entry *entry_b = (struct nameserver_entry *)b;
195 if (entry_a->id < entry_b->id)
198 if (entry_a->id > entry_b->id)
204 static void free_ns_entry(gpointer data)
206 struct nameserver_entry *entry = data;
208 g_free(entry->nameserver);
212 static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
214 DBusMessageIter iter, dict;
215 const char *reason, *key, *value;
216 char *address = NULL, *gateway = NULL, *peer = NULL, *netmask = NULL;
217 struct connman_ipaddress *ipaddress;
218 GSList *nameserver_list = NULL;
219 struct ov_private_data *data = vpn_provider_get_plugin_data(provider);
221 dbus_message_iter_init(msg, &iter);
223 dbus_message_iter_get_basic(&iter, &reason);
224 dbus_message_iter_next(&iter);
227 connman_error("No provider found");
228 return VPN_STATE_FAILURE;
231 DBG("%p %s", vpn_provider_get_name(provider), reason);
233 if (strcmp(reason, "up")) {
234 ov_connect_done(data, EIO);
235 return VPN_STATE_DISCONNECT;
238 dbus_message_iter_recurse(&iter, &dict);
240 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
241 struct nameserver_entry *ns_entry = NULL;
242 DBusMessageIter entry;
244 dbus_message_iter_recurse(&dict, &entry);
245 dbus_message_iter_get_basic(&entry, &key);
246 dbus_message_iter_next(&entry);
247 dbus_message_iter_get_basic(&entry, &value);
249 DBG("%s = %s", key, value);
251 if (!strcmp(key, "trusted_ip"))
252 gateway = g_strdup(value);
254 if (!strcmp(key, "ifconfig_local"))
255 address = g_strdup(value);
257 if (!strcmp(key, "ifconfig_netmask"))
258 netmask = g_strdup(value);
260 if (!strcmp(key, "ifconfig_remote"))
261 peer = g_strdup(value);
263 if (g_str_has_prefix(key, "route_"))
264 vpn_provider_append_route(provider, key, value);
266 if ((ns_entry = ov_append_dns_entries(key, value)))
267 nameserver_list = g_slist_prepend(nameserver_list,
270 char *domain = ov_get_domain_name(key, value);
272 vpn_provider_set_domain(provider, domain);
277 dbus_message_iter_next(&dict);
280 ipaddress = connman_ipaddress_alloc(AF_INET);
282 g_slist_free_full(nameserver_list, free_ns_entry);
288 return VPN_STATE_FAILURE;
291 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
292 connman_ipaddress_set_peer(ipaddress, peer);
293 vpn_provider_set_ipaddress(provider, ipaddress);
295 if (nameserver_list) {
296 char *nameservers = NULL;
299 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
300 for (tmp = nameserver_list; tmp;
301 tmp = g_slist_next(tmp)) {
302 struct nameserver_entry *ns = tmp->data;
305 nameservers = g_strdup(ns->nameserver);
308 str = g_strjoin(" ", nameservers,
309 ns->nameserver, NULL);
315 g_slist_free_full(nameserver_list, free_ns_entry);
317 vpn_provider_set_nameservers(provider, nameservers);
326 connman_ipaddress_free(ipaddress);
328 ov_connect_done(data, 0);
329 return VPN_STATE_CONNECT;
332 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
337 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
338 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
339 option = vpn_provider_get_string(provider,
340 ov_options[i].cm_opt);
344 g_key_file_set_string(keyfile,
345 vpn_provider_get_save_group(provider),
346 ov_options[i].cm_opt, option);
352 static int task_append_config_data(struct vpn_provider *provider,
353 struct connman_task *task)
358 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
359 if (!ov_options[i].ov_opt)
362 option = vpn_provider_get_string(provider,
363 ov_options[i].cm_opt);
368 * If the AuthUserPass option is "-", provide the input
369 * via management interface
371 if (!strcmp(ov_options[i].cm_opt, "OpenVPN.AuthUserPass") &&
372 !strcmp(option, "-"))
375 if (connman_task_add_argument(task,
376 ov_options[i].ov_opt,
377 ov_options[i].has_value ? option : NULL) < 0)
385 static void close_management_interface(struct ov_private_data *data)
387 if (data->mgmt_path) {
388 if (unlink(data->mgmt_path) && errno != ENOENT)
389 connman_warn("Unable to unlink management socket %s: "
390 "%d", data->mgmt_path, errno);
392 g_free(data->mgmt_path);
393 data->mgmt_path = NULL;
396 if (data->mgmt_timer_id != 0) {
397 g_source_remove(data->mgmt_timer_id);
398 data->mgmt_timer_id = 0;
401 if (data->mgmt_event_id) {
402 g_source_remove(data->mgmt_event_id);
403 data->mgmt_event_id = 0;
406 if (data->mgmt_channel) {
407 g_io_channel_shutdown(data->mgmt_channel, FALSE, NULL);
408 g_io_channel_unref(data->mgmt_channel);
409 data->mgmt_channel = NULL;
413 static void ov_died(struct connman_task *task, int exit_code, void *user_data)
415 struct ov_private_data *data = user_data;
417 /* Cancel any pending agent requests */
418 connman_agent_cancel(data->provider);
420 close_management_interface(data);
422 vpn_died(task, exit_code, data->provider);
424 free_private_data(data);
427 static int run_connect(struct ov_private_data *data,
428 vpn_provider_connect_cb_t cb, void *user_data)
430 struct vpn_provider *provider = data->provider;
431 struct connman_task *task = data->task;
435 option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
438 * Set some default options if user has no config file.
440 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
442 connman_task_add_argument(task, "--tls-auth", option);
443 option = vpn_provider_get_string(provider,
444 "OpenVPN.TLSAuthDir");
446 connman_task_add_argument(task, option, NULL);
449 connman_task_add_argument(task, "--nobind", NULL);
450 connman_task_add_argument(task, "--persist-key", NULL);
451 connman_task_add_argument(task, "--client", NULL);
454 if (data->mgmt_path) {
455 connman_task_add_argument(task, "--management", NULL);
456 connman_task_add_argument(task, data->mgmt_path, NULL);
457 connman_task_add_argument(task, "unix", NULL);
458 connman_task_add_argument(task, "--management-query-passwords",
460 connman_task_add_argument(task, "--auth-retry", "interact");
463 connman_task_add_argument(task, "--syslog", NULL);
465 connman_task_add_argument(task, "--script-security", "2");
467 connman_task_add_argument(task, "--up",
468 SCRIPTDIR "/openvpn-script");
469 connman_task_add_argument(task, "--up-restart", NULL);
471 connman_task_add_argument(task, "--setenv", NULL);
472 connman_task_add_argument(task, "CONNMAN_BUSNAME",
473 dbus_bus_get_unique_name(connection));
475 connman_task_add_argument(task, "--setenv", NULL);
476 connman_task_add_argument(task, "CONNMAN_INTERFACE",
477 CONNMAN_TASK_INTERFACE);
479 connman_task_add_argument(task, "--setenv", NULL);
480 connman_task_add_argument(task, "CONNMAN_PATH",
481 connman_task_get_path(task));
483 connman_task_add_argument(task, "--dev", data->if_name);
484 option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
486 connman_task_add_argument(task, "--dev-type", option);
489 * Default to tun for backwards compatibility.
491 connman_task_add_argument(task, "--dev-type", "tun");
494 connman_task_add_argument(task, "--persist-tun", NULL);
496 #if !defined TIZEN_EXT
497 connman_task_add_argument(task, "--route-noexec", NULL);
498 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
502 * Disable client restarts because we can't handle this at the
503 * moment. The problem is that when OpenVPN decides to switch
504 * from CONNECTED state to RECONNECTING and then to RESOLVE,
505 * it is not possible to do a DNS lookup. The DNS server is
506 * not accessible through the tunnel anymore and so we end up
507 * trying to resolve the OpenVPN servers address.
509 connman_task_add_argument(task, "--ping-restart", "0");
512 * Disable connetion retrying when OpenVPN is connected over TCP.
513 * With TCP OpenVPN attempts to handle reconnection silently without
514 * reporting the error back when establishing a connection or
515 * reconnecting as succesful one. The latter causes trouble if the
516 * retries are not limited to 1 (no retry) as the interface is up and
517 * connman regards it as the default route and network ceases to work,
520 option = vpn_provider_get_string(provider, "OpenVPN.Proto");
521 if (option && g_str_has_prefix(option, "tcp"))
522 connman_task_add_argument(task, "--connect-retry-max", "1");
524 err = connman_task_run(task, ov_died, data, NULL, NULL, NULL);
527 data->user_data = NULL;
528 connman_error("openvpn failed to start");
531 /* This lets the caller know that the actual result of
532 * the operation will be reported to the callback */
537 static void ov_quote_credential(GString *line, const char *cred)
542 g_string_append_c(line, '"');
544 while (*cred != '\0') {
550 g_string_append_c(line, '\\');
556 g_string_append_c(line, *cred++);
559 g_string_append_c(line, '"');
562 static void ov_return_credentials(struct ov_private_data *data,
563 const char *username, const char *password)
565 GString *reply_string;
569 reply_string = g_string_new(NULL);
571 g_string_append(reply_string, "username \"Auth\" ");
572 ov_quote_credential(reply_string, username);
573 g_string_append_c(reply_string, '\n');
575 g_string_append(reply_string, "password \"Auth\" ");
576 ov_quote_credential(reply_string, password);
577 g_string_append_c(reply_string, '\n');
579 len = reply_string->len;
580 reply = g_string_free(reply_string, FALSE);
582 g_io_channel_write_chars(data->mgmt_channel, reply, len, NULL, NULL);
583 g_io_channel_flush(data->mgmt_channel, NULL);
585 memset(reply, 0, len);
589 static void ov_return_private_key_password(struct ov_private_data *data,
590 const char *privatekeypass)
592 GString *reply_string;
596 reply_string = g_string_new(NULL);
598 g_string_append(reply_string, "password \"Private Key\" ");
599 ov_quote_credential(reply_string, privatekeypass);
600 g_string_append_c(reply_string, '\n');
602 len = reply_string->len;
603 reply = g_string_free(reply_string, FALSE);
605 g_io_channel_write_chars(data->mgmt_channel, reply, len, NULL, NULL);
606 g_io_channel_flush(data->mgmt_channel, NULL);
608 memset(reply, 0, len);
612 static void request_input_append_informational(DBusMessageIter *iter,
615 char *str = "string";
617 connman_dbus_dict_append_basic(iter, "Type",
618 DBUS_TYPE_STRING, &str);
619 str = "informational";
620 connman_dbus_dict_append_basic(iter, "Requirement",
621 DBUS_TYPE_STRING, &str);
624 static void request_input_append_mandatory(DBusMessageIter *iter,
627 char *str = "string";
629 connman_dbus_dict_append_basic(iter, "Type",
630 DBUS_TYPE_STRING, &str);
632 connman_dbus_dict_append_basic(iter, "Requirement",
633 DBUS_TYPE_STRING, &str);
636 static void request_input_append_password(DBusMessageIter *iter,
639 char *str = "password";
641 connman_dbus_dict_append_basic(iter, "Type",
642 DBUS_TYPE_STRING, &str);
644 connman_dbus_dict_append_basic(iter, "Requirement",
645 DBUS_TYPE_STRING, &str);
648 static void request_input_credentials_reply(DBusMessage *reply,
651 struct ov_private_data *data = user_data;
652 char *username = NULL;
653 char *password = NULL;
655 DBusMessageIter iter, dict;
659 connman_info("provider %p", data->provider);
662 * When connmand calls disconnect because of connection timeout no
670 dbus_error_init(&error);
672 err = vpn_agent_check_and_process_reply_error(reply, data->provider,
673 data->task, data->cb, data->user_data);
675 /* Ensure cb is called only once */
677 data->user_data = NULL;
681 if (!vpn_agent_check_reply_has_dict(reply)) {
686 dbus_message_iter_init(reply, &iter);
687 dbus_message_iter_recurse(&iter, &dict);
688 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
689 DBusMessageIter entry, value;
691 dbus_message_iter_recurse(&dict, &entry);
692 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
695 dbus_message_iter_get_basic(&entry, &key);
697 if (g_str_equal(key, "OpenVPN.Password")) {
698 dbus_message_iter_next(&entry);
699 if (dbus_message_iter_get_arg_type(&entry)
700 != DBUS_TYPE_VARIANT)
702 dbus_message_iter_recurse(&entry, &value);
703 if (dbus_message_iter_get_arg_type(&value)
706 dbus_message_iter_get_basic(&value, &password);
707 vpn_provider_set_string_hide_value(data->provider,
710 } else if (g_str_equal(key, "OpenVPN.Username")) {
711 dbus_message_iter_next(&entry);
712 if (dbus_message_iter_get_arg_type(&entry)
713 != DBUS_TYPE_VARIANT)
715 dbus_message_iter_recurse(&entry, &value);
716 if (dbus_message_iter_get_arg_type(&value)
719 dbus_message_iter_get_basic(&value, &username);
720 vpn_provider_set_string_hide_value(data->provider,
724 dbus_message_iter_next(&dict);
727 if (!password || !username) {
728 vpn_provider_indicate_error(data->provider,
729 VPN_PROVIDER_ERROR_AUTH_FAILED);
734 ov_return_credentials(data, username, password);
739 ov_connect_done(data, err);
742 static int request_credentials_input(struct ov_private_data *data)
744 DBusMessage *message;
745 const char *path, *agent_sender, *agent_path;
746 DBusMessageIter iter;
747 DBusMessageIter dict;
751 agent = connman_agent_get_info(data->dbus_sender, &agent_sender,
753 if (!agent || !agent_path)
756 message = dbus_message_new_method_call(agent_sender, agent_path,
762 dbus_message_iter_init_append(message, &iter);
764 path = vpn_provider_get_path(data->provider);
765 dbus_message_iter_append_basic(&iter,
766 DBUS_TYPE_OBJECT_PATH, &path);
768 connman_dbus_dict_open(&iter, &dict);
770 if (vpn_provider_get_authentication_errors(data->provider))
771 vpn_agent_append_auth_failure(&dict, data->provider, NULL);
773 /* Request temporary properties to pass on to openvpn */
774 connman_dbus_dict_append_dict(&dict, "OpenVPN.Username",
775 request_input_append_mandatory, NULL);
777 connman_dbus_dict_append_dict(&dict, "OpenVPN.Password",
778 request_input_append_password, NULL);
780 vpn_agent_append_host_and_name(&dict, data->provider);
782 connman_dbus_dict_close(&iter, &dict);
784 err = connman_agent_queue_message(data->provider, message,
785 connman_timeout_input_request(),
786 request_input_credentials_reply, data, agent);
788 if (err < 0 && err != -EBUSY) {
789 connman_error("error %d sending agent request", err);
790 dbus_message_unref(message);
795 dbus_message_unref(message);
800 static void request_input_private_key_reply(DBusMessage *reply,
803 struct ov_private_data *data = user_data;
804 const char *privatekeypass = NULL;
806 DBusMessageIter iter, dict;
810 connman_info("provider %p", data->provider);
813 * When connmand calls disconnect because of connection timeout no
821 dbus_error_init(&error);
823 err = vpn_agent_check_and_process_reply_error(reply, data->provider,
824 data->task, data->cb, data->user_data);
826 /* Ensure cb is called only once */
828 data->user_data = NULL;
832 if (!vpn_agent_check_reply_has_dict(reply)) {
837 dbus_message_iter_init(reply, &iter);
838 dbus_message_iter_recurse(&iter, &dict);
839 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
840 DBusMessageIter entry, value;
842 dbus_message_iter_recurse(&dict, &entry);
843 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
846 dbus_message_iter_get_basic(&entry, &key);
848 if (g_str_equal(key, "OpenVPN.PrivateKeyPassword")) {
849 dbus_message_iter_next(&entry);
850 if (dbus_message_iter_get_arg_type(&entry)
851 != DBUS_TYPE_VARIANT)
853 dbus_message_iter_recurse(&entry, &value);
854 if (dbus_message_iter_get_arg_type(&value)
857 dbus_message_iter_get_basic(&value, &privatekeypass);
858 vpn_provider_set_string_hide_value(data->provider,
859 key, privatekeypass);
863 dbus_message_iter_next(&dict);
866 if (!privatekeypass) {
867 vpn_provider_indicate_error(data->provider,
868 VPN_PROVIDER_ERROR_AUTH_FAILED);
874 ov_return_private_key_password(data, privatekeypass);
879 ov_connect_done(data, err);
882 static int request_private_key_input(struct ov_private_data *data)
884 DBusMessage *message;
885 const char *path, *agent_sender, *agent_path;
886 const char *privatekeypass;
887 DBusMessageIter iter;
888 DBusMessageIter dict;
893 * First check if this is the second attempt to get the key within
894 * this connection. In such case there has been invalid Private Key
895 * Password and it must be reset, and queried from user.
897 if (data->failed_attempts_privatekey) {
898 vpn_provider_set_string_hide_value(data->provider,
899 "OpenVPN.PrivateKeyPassword", NULL);
901 /* If the encrypted Private key password is kept in memory and
902 * use it first. If authentication fails this is cleared,
903 * likewise it is when connman-vpnd is restarted.
905 privatekeypass = vpn_provider_get_string(data->provider,
906 "OpenVPN.PrivateKeyPassword");
907 if (privatekeypass) {
908 ov_return_private_key_password(data, privatekeypass);
913 agent = connman_agent_get_info(data->dbus_sender, &agent_sender,
915 if (!agent || !agent_path)
918 message = dbus_message_new_method_call(agent_sender, agent_path,
924 dbus_message_iter_init_append(message, &iter);
926 path = vpn_provider_get_path(data->provider);
927 dbus_message_iter_append_basic(&iter,
928 DBUS_TYPE_OBJECT_PATH, &path);
930 connman_dbus_dict_open(&iter, &dict);
932 connman_dbus_dict_append_dict(&dict, "OpenVPN.PrivateKeyPassword",
933 request_input_append_password, NULL);
935 vpn_agent_append_host_and_name(&dict, data->provider);
937 /* Do not allow to store or retrieve the encrypted Private Key pass */
938 vpn_agent_append_allow_credential_storage(&dict, false);
939 vpn_agent_append_allow_credential_retrieval(&dict, false);
942 * Indicate to keep credentials, the enc Private Key password should
943 * not affect the credential storing.
945 vpn_agent_append_keep_credentials(&dict, true);
947 connman_dbus_dict_append_dict(&dict, "Enter Private Key password",
948 request_input_append_informational, NULL);
950 connman_dbus_dict_close(&iter, &dict);
952 err = connman_agent_queue_message(data->provider, message,
953 connman_timeout_input_request(),
954 request_input_private_key_reply, data, agent);
956 if (err < 0 && err != -EBUSY) {
957 connman_error("error %d sending agent request", err);
958 dbus_message_unref(message);
963 dbus_message_unref(message);
969 static gboolean ov_management_handle_input(GIOChannel *source,
970 GIOCondition condition, gpointer user_data)
972 struct ov_private_data *data = user_data;
977 if (condition & G_IO_IN) {
979 * Just return if line is not read and str is not allocated.
980 * Condition check handles closing of the channel later.
982 if (g_io_channel_read_line(source, &str, NULL, NULL, NULL) !=
986 str[strlen(str) - 1] = '\0';
987 connman_warn("openvpn request %s", str);
989 if (g_str_has_prefix(str, ">PASSWORD:Need 'Auth'")) {
991 * Request credentials from the user
993 err = request_credentials_input(data);
994 if (err != -EINPROGRESS)
996 } else if (g_str_has_prefix(str,
997 ">PASSWORD:Need 'Private Key'")) {
998 err = request_private_key_input(data);
999 if (err != -EINPROGRESS)
1001 } else if (g_str_has_prefix(str,
1002 ">PASSWORD:Verification Failed: 'Auth'")) {
1004 * Add error only, state change indication causes
1005 * signal to be sent, which is not desired when
1006 * OpenVPN is in interactive mode.
1008 vpn_provider_add_error(data->provider,
1009 VPN_PROVIDER_ERROR_AUTH_FAILED);
1011 * According to the OpenVPN manual about management interface
1012 * https://openvpn.net/community-resources/management-interface/
1013 * this should be received but it does not seem to be reported
1014 * when decrypting private key fails. This requires following
1015 * patch for OpenVPN (at least <= 2.4.5) in order to work:
1016 * https://git.sailfishos.org/mer-core/openvpn/blob/
1017 * 4f4b4af116292a207416c8a990392e35a6fc41af/rpm/privatekey-
1018 * passphrase-handling.diff
1020 } else if (g_str_has_prefix(str, ">PASSWORD:Verification "
1021 "Failed: 'Private Key'")) {
1022 data->failed_attempts_privatekey++;
1026 } else if (condition & (G_IO_ERR | G_IO_HUP)) {
1027 connman_warn("Management channel termination");
1032 close_management_interface(data);
1037 static int ov_management_connect_timer_cb(gpointer user_data)
1039 struct ov_private_data *data = user_data;
1041 if (!data->mgmt_channel) {
1042 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
1044 struct sockaddr_un remote;
1047 memset(&remote, 0, sizeof(remote));
1048 remote.sun_family = AF_UNIX;
1049 g_strlcpy(remote.sun_path, data->mgmt_path,
1050 sizeof(remote.sun_path));
1052 err = connect(fd, (struct sockaddr *)&remote,
1055 data->mgmt_channel = g_io_channel_unix_new(fd);
1056 data->mgmt_event_id =
1057 g_io_add_watch(data->mgmt_channel,
1058 G_IO_IN | G_IO_ERR | G_IO_HUP,
1059 ov_management_handle_input,
1062 connman_warn("Connected management socket");
1063 data->mgmt_timer_id = 0;
1064 return G_SOURCE_REMOVE;
1070 data->connect_attempts++;
1071 if (data->connect_attempts > 30) {
1072 connman_warn("Unable to connect management socket");
1073 data->mgmt_timer_id = 0;
1074 return G_SOURCE_REMOVE;
1077 return G_SOURCE_CONTINUE;
1080 static int ov_connect(struct vpn_provider *provider,
1081 struct connman_task *task, const char *if_name,
1082 vpn_provider_connect_cb_t cb, const char *dbus_sender,
1086 struct ov_private_data *data;
1088 data = g_try_new0(struct ov_private_data, 1);
1092 vpn_provider_set_plugin_data(provider, data);
1093 data->provider = vpn_provider_ref(provider);
1095 data->dbus_sender = g_strdup(dbus_sender);
1096 data->if_name = g_strdup(if_name);
1098 data->user_data = user_data;
1101 * We need to use the management interface to provide
1102 * the user credentials and password for decrypting private key.
1105 /* Use env TMPDIR for creating management socket, fall back to /tmp */
1106 tmpdir = getenv("TMPDIR");
1107 if (!tmpdir || !*tmpdir)
1110 /* Set up the path for the management interface */
1111 data->mgmt_path = g_strconcat(tmpdir, "/connman-vpn-management-",
1112 vpn_provider_get_ident(provider), NULL);
1113 if (unlink(data->mgmt_path) != 0 && errno != ENOENT) {
1114 connman_warn("Unable to unlink management socket %s: %d",
1115 data->mgmt_path, errno);
1118 data->mgmt_timer_id = g_timeout_add(200,
1119 ov_management_connect_timer_cb, data);
1121 task_append_config_data(provider, task);
1123 return run_connect(data, cb, user_data);
1126 static void ov_disconnect(struct vpn_provider *provider)
1131 connman_agent_cancel(provider);
1133 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_DISCONNECT);
1136 static int ov_device_flags(struct vpn_provider *provider)
1140 option = vpn_provider_get_string(provider, "OpenVPN.DeviceType");
1145 if (g_str_equal(option, "tap")) {
1149 if (!g_str_equal(option, "tun")) {
1150 connman_warn("bad OpenVPN.DeviceType value "
1151 "falling back to tun");
1157 static int ov_route_env_parse(struct vpn_provider *provider, const char *key,
1158 int *family, unsigned long *idx,
1159 enum vpn_provider_route_type *type)
1164 if (g_str_has_prefix(key, "route_network_")) {
1165 start = key + strlen("route_network_");
1166 *type = VPN_PROVIDER_ROUTE_TYPE_ADDR;
1167 } else if (g_str_has_prefix(key, "route_netmask_")) {
1168 start = key + strlen("route_netmask_");
1169 *type = VPN_PROVIDER_ROUTE_TYPE_MASK;
1170 } else if (g_str_has_prefix(key, "route_gateway_")) {
1171 start = key + strlen("route_gateway_");
1172 *type = VPN_PROVIDER_ROUTE_TYPE_GW;
1177 *idx = g_ascii_strtoull(start, &end, 10);
1182 static struct vpn_driver vpn_driver = {
1183 .notify = ov_notify,
1184 .connect = ov_connect,
1185 .disconnect = ov_disconnect,
1187 .device_flags = ov_device_flags,
1188 .route_env_parse = ov_route_env_parse,
1191 static int openvpn_init(void)
1193 connection = connman_dbus_get_connection();
1195 return vpn_register("openvpn", &vpn_driver, OPENVPN);
1198 static void openvpn_exit(void)
1200 vpn_unregister("openvpn");
1202 dbus_connection_unref(connection);
1205 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
1206 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)