openvpn: Use parser table instead of open coding
[platform/upstream/connman.git] / plugins / openvpn.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. 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 #include <stdio.h>
30 #include <net/if.h>
31
32 #include <glib.h>
33
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/dbus.h>
40
41 #include "vpn.h"
42
43 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
44
45 static DBusConnection *connection;
46
47 struct {
48         const char *cm_opt;
49         const char *ov_opt;
50 } ov_options[] = {
51         { "Host", "--remote" },
52         { "OpenVPN.CACert", "--ca" },
53         { "OpenVPN.Cert", "--cert" },
54         { "OpenVPN.Key", "--key" },
55         { "OpenVPN.MTU", "--mtu" },
56         { "OpenVPN.Proto", "--proto" },
57         { "OpenVPN.Port", "--port" },
58         { "OpenVPN.AuthUserPass", "--auth-user-pass" },
59         { "OpenVPN.TLSRemote", "--tls-remote" },
60         { "OpenVPN.Cipher", "--cipher" },
61         { "OpenVPN.Auth", "--auth" },
62         { "OpenVPN.CompLZO", "--comp-lzo" },
63 };
64
65 static void ov_append_dns_entries(const char *key, const char *value,
66                                         char **dns_entries)
67 {
68         gchar **options;
69
70         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
71                 return;
72
73         options = g_strsplit(value, " ", 3);
74         if (options[0] != NULL &&
75                 !strcmp(options[0], "dhcp-option") &&
76                         options[1] != NULL &&
77                         !strcmp(options[1], "DNS") &&
78                                 options[2] != NULL) {
79
80                 if (*dns_entries != NULL) {
81                         char *tmp;
82
83                         tmp = g_strjoin(" ", *dns_entries,
84                                                 options[2], NULL);
85                         g_free(*dns_entries);
86                         *dns_entries = tmp;
87                 } else {
88                         *dns_entries = g_strdup(options[2]);
89                 }
90         }
91
92         g_strfreev(options);
93 }
94
95 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
96 {
97         DBusMessageIter iter, dict;
98         const char *reason, *key, *value;
99         const char *domain = NULL;
100         char *dns_entries = NULL;
101
102         dbus_message_iter_init(msg, &iter);
103
104         dbus_message_iter_get_basic(&iter, &reason);
105         dbus_message_iter_next(&iter);
106
107         if (!provider) {
108                 connman_error("No provider found");
109                 return VPN_STATE_FAILURE;
110         }
111
112         if (strcmp(reason, "up"))
113                 return VPN_STATE_DISCONNECT;
114
115         domain = connman_provider_get_string(provider, "VPN.Domain");
116
117         dbus_message_iter_recurse(&iter, &dict);
118
119         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
120                 DBusMessageIter entry;
121
122                 dbus_message_iter_recurse(&dict, &entry);
123                 dbus_message_iter_get_basic(&entry, &key);
124                 dbus_message_iter_next(&entry);
125                 dbus_message_iter_get_basic(&entry, &value);
126
127                 DBG("%s = %s", key, value);
128
129                 if (!strcmp(key, "trusted_ip"))
130                         connman_provider_set_string(provider, "Gateway", value);
131
132                 if (!strcmp(key, "ifconfig_local"))
133                         connman_provider_set_string(provider, "Address", value);
134
135                 if (!strcmp(key, "ifconfig_remote"))
136                         connman_provider_set_string(provider, "Peer", value);
137
138                 if (g_str_has_prefix(key, "route_") == TRUE)
139                         connman_provider_append_route(provider, key, value);
140
141                 ov_append_dns_entries(key, value, &dns_entries);
142
143                 dbus_message_iter_next(&dict);
144         }
145
146         if (dns_entries != NULL) {
147                 connman_provider_set_string(provider, "DNS", dns_entries);
148                 g_free(dns_entries);
149         }
150
151         return VPN_STATE_CONNECT;
152 }
153
154 static int task_append_config_data(struct connman_provider *provider,
155                                         struct connman_task *task)
156 {
157         const char *option;
158         int i;
159
160         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
161                 option = connman_provider_get_string(provider,
162                                         ov_options[i].cm_opt);
163                 if (option == NULL)
164                         continue;
165
166                 if (connman_task_add_argument(task,
167                                         ov_options[i].ov_opt, option) < 0) {
168                         return -EIO;
169                 }
170         }
171
172         return 0;
173 }
174
175 static int ov_connect(struct connman_provider *provider,
176                 struct connman_task *task, const char *if_name)
177 {
178         const char *option;
179         int err, fd;
180
181         option = connman_provider_get_string(provider, "Host");
182         if (option == NULL) {
183                 connman_error("Host not set; cannot enable VPN");
184                 return -EINVAL;
185         }
186
187         task_append_config_data(provider, task);
188
189         connman_task_add_argument(task, "--syslog", NULL);
190
191         connman_task_add_argument(task, "--script-security", "2");
192
193         connman_task_add_argument(task, "--up",
194                                         SCRIPTDIR "/openvpn-script");
195         connman_task_add_argument(task, "--up-restart", NULL);
196
197         connman_task_add_argument(task, "--setenv", NULL);
198         connman_task_add_argument(task, "CONNMAN_BUSNAME",
199                                         dbus_bus_get_unique_name(connection));
200
201         connman_task_add_argument(task, "--setenv", NULL);
202         connman_task_add_argument(task, "CONNMAN_INTERFACE",
203                                         CONNMAN_TASK_INTERFACE);
204
205         connman_task_add_argument(task, "--setenv", NULL);
206         connman_task_add_argument(task, "CONNMAN_PATH",
207                                         connman_task_get_path(task));
208
209         connman_task_add_argument(task, "--dev", if_name);
210         connman_task_add_argument(task, "--dev-type", "tun");
211
212         connman_task_add_argument(task, "--tls-client", NULL);
213         connman_task_add_argument(task, "--nobind", NULL);
214         connman_task_add_argument(task, "--persist-key", NULL);
215         connman_task_add_argument(task, "--persist-tun", NULL);
216
217         connman_task_add_argument(task, "--route-noexec", NULL);
218         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
219
220         /*
221          * Disable client restarts because we can't handle this at the
222          * moment. The problem is that when OpenVPN decides to switch
223          * from CONNECTED state to RECONNECTING and then to RESOLVE,
224          * it is not possible to do a DNS lookup. The DNS server is
225          * not accessable through the tunnel anymore and so we end up
226          * trying to resolve the OpenVPN servers address.
227          */
228         connman_task_add_argument(task, "--ping-restart", "0");
229
230         connman_task_add_argument(task, "--client", NULL);
231
232         fd = fileno(stderr);
233         err = connman_task_run(task, vpn_died, provider,
234                         NULL, &fd, &fd);
235         if (err < 0) {
236                 connman_error("openvpn failed to start");
237                 return -EIO;
238         }
239
240         return 0;
241 }
242
243 static struct vpn_driver vpn_driver = {
244         .notify = ov_notify,
245         .connect        = ov_connect,
246 };
247
248 static int openvpn_init(void)
249 {
250         connection = connman_dbus_get_connection();
251
252         return vpn_register("openvpn", &vpn_driver, OPENVPN);
253 }
254
255 static void openvpn_exit(void)
256 {
257         vpn_unregister("openvpn");
258
259         dbus_connection_unref(connection);
260 }
261
262 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
263         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)