provider: Factorize VPN routing environment variables parsing
[framework/connectivity/connman.git] / plugins / openconnect.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/plugin.h>
34 #include <connman/provider.h>
35 #include <connman/log.h>
36 #include <connman/task.h>
37
38 #include "vpn.h"
39
40 static int oc_notify(DBusMessage *msg, struct connman_provider *provider)
41 {
42         DBusMessageIter iter, dict;
43         const char *reason, *key, *value;
44         const char *domain = NULL;
45
46         dbus_message_iter_init(msg, &iter);
47
48         dbus_message_iter_get_basic(&iter, &reason);
49         dbus_message_iter_next(&iter);
50
51         if (!provider) {
52                 connman_error("No provider found");
53                 return VPN_STATE_FAILURE;
54         }
55
56         if (strcmp(reason, "connect"))
57                 return VPN_STATE_DISCONNECT;
58
59         domain = connman_provider_get_string(provider, "VPN.Domain");
60
61         dbus_message_iter_recurse(&iter, &dict);
62
63         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
64                 DBusMessageIter entry;
65
66                 dbus_message_iter_recurse(&dict, &entry);
67                 dbus_message_iter_get_basic(&entry, &key);
68                 dbus_message_iter_next(&entry);
69                 dbus_message_iter_get_basic(&entry, &value);
70
71                 if (strcmp(key, "CISCO_CSTP_OPTIONS"))
72                         DBG("%s = %s", key, value);
73
74                 if (!strcmp(key, "VPNGATEWAY"))
75                         connman_provider_set_string(provider, "Gateway", value);
76
77                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
78                         connman_provider_set_string(provider, "Address", value);
79
80                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
81                         connman_provider_set_string(provider, "Netmask", value);
82
83                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
84                         connman_provider_set_string(provider, "DNS", value);
85
86                 if (!strcmp(key, "CISCO_PROXY_PAC"))
87                         connman_provider_set_string(provider, "PAC", value);
88
89                 if (domain == NULL && !strcmp(key, "CISCO_DEF_DOMAIN"))
90                         domain = value;
91
92                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE ||
93                         g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
94                         connman_provider_append_route(provider, key, value);
95
96                 dbus_message_iter_next(&dict);
97         }
98
99         connman_provider_set_string(provider, "Domain", domain);
100
101         return VPN_STATE_CONNECT;
102 }
103
104 static int oc_connect(struct connman_provider *provider,
105                         struct connman_task *task, const char *if_name)
106 {
107         const char *vpnhost, *vpncookie, *cafile, *certsha1, *mtu;
108         int fd, err;
109
110         vpnhost = connman_provider_get_string(provider, "Host");
111         if (!vpnhost) {
112                 connman_error("Host not set; cannot enable VPN");
113                 return -EINVAL;
114         }
115
116         vpncookie = connman_provider_get_string(provider, "OpenConnect.Cookie");
117         if (!vpncookie) {
118                 connman_error("OpenConnect.Cookie not set; cannot enable VPN");
119                 return -EINVAL;
120         }
121
122         certsha1 = connman_provider_get_string(provider,
123                                                 "OpenConnect.ServerCert");
124         if (certsha1)
125                 connman_task_add_argument(task, "--servercert",
126                                                         (char *)certsha1);
127
128         cafile = connman_provider_get_string(provider, "OpenConnect.CACert");
129         mtu = connman_provider_get_string(provider, "VPN.MTU");
130
131         if (cafile)
132                 connman_task_add_argument(task, "--cafile",
133                                                         (char *)cafile);
134         if (mtu)
135                 connman_task_add_argument(task, "--mtu", (char *)mtu);
136
137         connman_task_add_argument(task, "--syslog", NULL);
138         connman_task_add_argument(task, "--cookie-on-stdin", NULL);
139
140         connman_task_add_argument(task, "--script",
141                                   SCRIPTDIR "/openconnect-script");
142
143         connman_task_add_argument(task, "--interface", if_name);
144
145         connman_task_add_argument(task, (char *)vpnhost, NULL);
146
147         err = connman_task_run(task, vpn_died, provider,
148                                &fd, NULL, NULL);
149         if (err < 0) {
150                 connman_error("openconnect failed to start");
151                 return -EIO;
152         }
153
154         if (write(fd, vpncookie, strlen(vpncookie)) !=
155                         (ssize_t)strlen(vpncookie) ||
156                         write(fd, "\n", 1) != 1) {
157                 connman_error("openconnect failed to take cookie on stdin");
158                 return -EIO;
159         }
160
161         return 0;
162 }
163
164 static struct vpn_driver vpn_driver = {
165         .notify         = oc_notify,
166         .connect        = oc_connect,
167 };
168
169 static int openconnect_init(void)
170 {
171         return vpn_register("openconnect", &vpn_driver, OPENCONNECT);
172 }
173
174 static void openconnect_exit(void)
175 {
176         vpn_unregister("openconnect");
177 }
178
179 CONNMAN_PLUGIN_DEFINE(openconnect, "OpenConnect VPN plugin", VERSION,
180         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openconnect_init, openconnect_exit)