5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/provider.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/ipconfig.h>
43 static int oc_notify(DBusMessage *msg, struct connman_provider *provider)
45 DBusMessageIter iter, dict;
46 const char *reason, *key, *value;
47 const char *domain = NULL;
48 char *addressv4 = NULL, *addressv6 = NULL;
49 char *netmask = NULL, *gateway = NULL;
50 unsigned char prefix_len = 0;
51 struct connman_ipaddress *ipaddress;
53 dbus_message_iter_init(msg, &iter);
55 dbus_message_iter_get_basic(&iter, &reason);
56 dbus_message_iter_next(&iter);
59 connman_error("No provider found");
60 return VPN_STATE_FAILURE;
63 if (strcmp(reason, "connect"))
64 return VPN_STATE_DISCONNECT;
66 domain = connman_provider_get_string(provider, "VPN.Domain");
68 dbus_message_iter_recurse(&iter, &dict);
70 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
71 DBusMessageIter entry;
73 dbus_message_iter_recurse(&dict, &entry);
74 dbus_message_iter_get_basic(&entry, &key);
75 dbus_message_iter_next(&entry);
76 dbus_message_iter_get_basic(&entry, &value);
78 if (strcmp(key, "CISCO_CSTP_OPTIONS"))
79 DBG("%s = %s", key, value);
81 if (!strcmp(key, "VPNGATEWAY"))
82 gateway = g_strdup(value);
84 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
85 addressv4 = g_strdup(value);
87 if (!strcmp(key, "INTERNAL_IP6_ADDRESS")) {
88 addressv6 = g_strdup(value);
92 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
93 netmask = g_strdup(value);
95 if (!strcmp(key, "INTERNAL_IP6_NETMASK")) {
98 /* The netmask contains the address and the prefix */
99 sep = strchr(value, '/');
101 unsigned char ip_len = sep - value;
103 addressv6 = g_strndup(value, ip_len);
104 prefix_len = (unsigned char)
105 strtol(sep + 1, NULL, 10);
109 if (!strcmp(key, "INTERNAL_IP4_DNS") ||
110 !strcmp(key, "INTERNAL_IP6_DNS"))
111 connman_provider_set_nameservers(provider, value);
113 if (!strcmp(key, "CISCO_PROXY_PAC"))
114 connman_provider_set_pac(provider, value);
116 if (domain == NULL && !strcmp(key, "CISCO_DEF_DOMAIN"))
119 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE ||
120 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
121 connman_provider_append_route(provider, key, value);
123 dbus_message_iter_next(&dict);
126 DBG("%p %p", addressv4, addressv6);
128 if (addressv4 != NULL)
129 ipaddress = connman_ipaddress_alloc(AF_INET);
130 else if (addressv6 != NULL)
131 ipaddress = connman_ipaddress_alloc(AF_INET6);
135 if (ipaddress == NULL) {
141 return VPN_STATE_FAILURE;
144 if (addressv4 != NULL)
145 connman_ipaddress_set_ipv4(ipaddress, addressv4,
148 connman_ipaddress_set_ipv6(ipaddress, addressv6,
149 prefix_len, gateway);
150 connman_provider_set_ipaddress(provider, ipaddress);
151 connman_provider_set_domain(provider, domain);
157 connman_ipaddress_free(ipaddress);
159 return VPN_STATE_CONNECT;
162 static int oc_connect(struct connman_provider *provider,
163 struct connman_task *task, const char *if_name)
165 const char *vpnhost, *vpncookie, *cafile, *certsha1, *mtu;
168 vpnhost = connman_provider_get_string(provider, "Host");
170 connman_error("Host not set; cannot enable VPN");
174 vpncookie = connman_provider_get_string(provider, "OpenConnect.Cookie");
176 connman_error("OpenConnect.Cookie not set; cannot enable VPN");
180 certsha1 = connman_provider_get_string(provider,
181 "OpenConnect.ServerCert");
183 connman_task_add_argument(task, "--servercert",
186 cafile = connman_provider_get_string(provider, "OpenConnect.CACert");
187 mtu = connman_provider_get_string(provider, "VPN.MTU");
190 connman_task_add_argument(task, "--cafile",
193 connman_task_add_argument(task, "--mtu", (char *)mtu);
195 connman_task_add_argument(task, "--syslog", NULL);
196 connman_task_add_argument(task, "--cookie-on-stdin", NULL);
198 connman_task_add_argument(task, "--script",
199 SCRIPTDIR "/openconnect-script");
201 connman_task_add_argument(task, "--interface", if_name);
203 connman_task_add_argument(task, (char *)vpnhost, NULL);
205 err = connman_task_run(task, vpn_died, provider,
208 connman_error("openconnect failed to start");
212 if (write(fd, vpncookie, strlen(vpncookie)) !=
213 (ssize_t)strlen(vpncookie) ||
214 write(fd, "\n", 1) != 1) {
215 connman_error("openconnect failed to take cookie on stdin");
222 static int oc_save (struct connman_provider *provider, GKeyFile *keyfile)
226 setting = connman_provider_get_string(provider,
227 "OpenConnect.ServerCert");
229 g_key_file_set_string(keyfile,
230 connman_provider_get_save_group(provider),
231 "OpenConnect.ServerCert", setting);
233 setting = connman_provider_get_string(provider,
234 "OpenConnect.CACert");
236 g_key_file_set_string(keyfile,
237 connman_provider_get_save_group(provider),
238 "OpenConnect.CACert", setting);
240 setting = connman_provider_get_string(provider,
243 g_key_file_set_string(keyfile,
244 connman_provider_get_save_group(provider),
250 static int oc_error_code(int exit_code)
255 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
257 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
259 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
263 static struct vpn_driver vpn_driver = {
265 .connect = oc_connect,
266 .error_code = oc_error_code,
270 static int openconnect_init(void)
272 return vpn_register("openconnect", &vpn_driver, OPENCONNECT);
275 static void openconnect_exit(void)
277 vpn_unregister("openconnect");
280 CONNMAN_PLUGIN_DEFINE(openconnect, "OpenConnect VPN plugin", VERSION,
281 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openconnect_init, openconnect_exit)