Split openconnect into generic vpn and openconnect part
[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 #define CONNMAN_API_SUBJECT_TO_CHANGE
31 #include <connman/plugin.h>
32 #include <connman/provider.h>
33 #include <connman/log.h>
34 #include <connman/task.h>
35
36 #include "vpn.h"
37
38 static int oc_notify(DBusMessage *msg, struct connman_provider *provider)
39 {
40         DBusMessageIter iter, dict;
41         struct oc_data *data;
42         const char *reason, *key, *value;
43         const char *domain = NULL;
44
45         dbus_message_iter_init(msg, &iter);
46
47         dbus_message_iter_get_basic(&iter, &reason);
48         dbus_message_iter_next(&iter);
49
50         if (!provider) {
51                 connman_error("No provider found");
52                 return VPN_STATE_FAILURE;
53         }
54
55         data = connman_provider_get_data(provider);
56         if (!data) {
57                 DBG("provider %p no data", provider);
58                 return VPN_STATE_FAILURE;
59         }
60
61         if (strcmp(reason, "connect"))
62                 return VPN_STATE_DISCONNECT;
63
64         domain = connman_provider_get_string(provider, "VPN.Domain");
65
66         dbus_message_iter_recurse(&iter, &dict);
67
68         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
69                 DBusMessageIter entry;
70
71                 dbus_message_iter_recurse(&dict, &entry);
72                 dbus_message_iter_get_basic(&entry, &key);
73                 dbus_message_iter_next(&entry);
74                 dbus_message_iter_get_basic(&entry, &value);
75
76                 if (strcmp(key, "CISCO_CSTP_OPTIONS"))
77                         DBG("%s = %s", key, value);
78
79                 if (!strcmp(key, "VPNGATEWAY"))
80                         connman_provider_set_string(provider, "Gateway", value);
81
82                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
83                         connman_provider_set_string(provider, "Address", value);
84
85                 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
86                         connman_provider_set_string(provider, "Netmask", value);
87
88                 if (!strcmp(key, "INTERNAL_IP4_DNS"))
89                         connman_provider_set_string(provider, "DNS", value);
90
91                 if (!strcmp(key, "CISCO_PROXY_PAC"))
92                         connman_provider_set_string(provider, "PAC", value);
93
94                 if (domain == NULL && !strcmp(key, "CISCO_DEF_DOMAIN"))
95                         domain = value;
96
97                 dbus_message_iter_next(&dict);
98         }
99
100         connman_provider_set_string(provider, "Domain", domain);
101
102         return VPN_STATE_CONNECT;
103 }
104
105 static int oc_connect(struct connman_provider *provider,
106                         struct connman_task *task, const char *if_name)
107 {
108         const char *vpnhost, *vpncookie, *cafile, *mtu;
109         int fd, err;
110
111         vpnhost = connman_provider_get_string(provider, "Host");
112         if (!vpnhost) {
113                 connman_error("Host not set; cannot enable VPN");
114                 return -EINVAL;
115         }
116
117         vpncookie = connman_provider_get_string(provider, "OpenConnect.Cookie");
118         if (!vpncookie) {
119                 connman_error("OpenConnect.Cookie not set; cannot enable VPN");
120                 return -EINVAL;
121         }
122
123         cafile = connman_provider_get_string(provider, "OpenConnect.CACert");
124         mtu = connman_provider_get_string(provider, "VPN.MTU");
125
126         if (cafile)
127                 connman_task_add_argument(task, "--cafile",
128                                                         (char *)cafile);
129         if (mtu)
130                 connman_task_add_argument(task, "--mtu", (char *)mtu);
131
132         connman_task_add_argument(task, "--syslog", NULL);
133         connman_task_add_argument(task, "--cookie-on-stdin", NULL);
134
135         connman_task_add_argument(task, "--script",
136                                   SCRIPTDIR "/openconnect-script");
137
138         connman_task_add_argument(task, "--interface", if_name);
139
140         connman_task_add_argument(task, (char *)vpnhost, NULL);
141
142         err = connman_task_run(task, vpn_died, provider,
143                                &fd, NULL, NULL);
144         if (err < 0) {
145                 connman_error("openconnect failed to start");
146                 return -EIO;
147         }
148
149         if (write(fd, vpncookie, strlen(vpncookie)) !=
150                         (ssize_t)strlen(vpncookie) ||
151                         write(fd, "\n", 1) != 1) {
152                 connman_error("openconnect failed to take cookie on stdin");
153                 return -EIO;
154         }
155
156         return 0;
157 }
158
159 static struct vpn_driver vpn_driver = {
160         .notify         = oc_notify,
161         .connect        = oc_connect,
162 };
163
164 static int openconnect_init(void)
165 {
166         return vpn_register("openconnect", &vpn_driver, OPENCONNECT);
167 }
168
169 static void openconnect_exit(void)
170 {
171         vpn_unregister("openconnect");
172 }
173
174 CONNMAN_PLUGIN_DEFINE(openconnect, "OpenConnect VPN plugin", VERSION,
175         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openconnect_init, openconnect_exit)