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