vpn: New vpn daemon that handles vpn connections and clients
[platform/upstream/connman.git] / vpn / plugins / pptp.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <net/if.h>
32
33 #include <dbus/dbus.h>
34 #include <glib.h>
35
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/provider.h>
39 #include <connman/log.h>
40 #include <connman/task.h>
41 #include <connman/dbus.h>
42 #include <connman/inet.h>
43
44 #include "vpn.h"
45
46 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
47
48 enum {
49         OPT_STRING = 1,
50         OPT_BOOL = 2,
51 };
52
53 struct {
54         const char *cm_opt;
55         const char *pptp_opt;
56         const char *vpnc_default;
57         int type;
58 } pptp_options[] = {
59         { "PPTP.User", "user", NULL, OPT_STRING },
60         { "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
61         { "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
62         { "PPTP.Debug", "debug", NULL, OPT_STRING },
63         { "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
64         { "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
65         { "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
66         { "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
67         { "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
68         { "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
69         { "PPTP.NoDeflate", "nodeflate", NULL, OPT_BOOL },
70         { "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
71         { "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
72         { "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
73         { "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
74         { "PPTP.NoVJ", "no-vj-comp", NULL, OPT_BOOL },
75 };
76
77 static DBusConnection *connection;
78
79 static DBusMessage *pptp_get_sec(struct connman_task *task,
80                                 DBusMessage *msg, void *user_data)
81 {
82         const char *user, *passwd;
83         struct vpn_provider *provider = user_data;
84         DBusMessage *reply;
85
86         if (dbus_message_get_no_reply(msg) == TRUE)
87                 return NULL;
88
89         user = vpn_provider_get_string(provider, "PPTP.User");
90         passwd = vpn_provider_get_string(provider, "PPTP.Password");
91         if (user == NULL || strlen(user) == 0 ||
92                                 passwd == NULL || strlen(passwd) == 0)
93                 return NULL;
94
95         reply = dbus_message_new_method_return(msg);
96         if (reply == NULL)
97                 return NULL;
98
99         dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
100                                 DBUS_TYPE_STRING, &passwd,
101                                 DBUS_TYPE_INVALID);
102         return reply;
103 }
104
105 static int pptp_notify(DBusMessage *msg, struct vpn_provider *provider)
106 {
107         DBusMessageIter iter, dict;
108         const char *reason, *key, *value;
109         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
110         char *ifname = NULL, *nameservers = NULL;
111         struct connman_ipaddress *ipaddress = NULL;
112
113         dbus_message_iter_init(msg, &iter);
114
115         dbus_message_iter_get_basic(&iter, &reason);
116         dbus_message_iter_next(&iter);
117
118         if (provider == NULL) {
119                 connman_error("No provider found");
120                 return VPN_STATE_FAILURE;
121         }
122
123         if (strcmp(reason, "auth failed") == 0)
124                 return VPN_STATE_AUTH_FAILURE;
125
126         if (strcmp(reason, "connect"))
127                 return VPN_STATE_DISCONNECT;
128
129         dbus_message_iter_recurse(&iter, &dict);
130
131         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
132                 DBusMessageIter entry;
133
134                 dbus_message_iter_recurse(&dict, &entry);
135                 dbus_message_iter_get_basic(&entry, &key);
136                 dbus_message_iter_next(&entry);
137                 dbus_message_iter_get_basic(&entry, &value);
138
139                 DBG("%s = %s", key, value);
140
141                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
142                         vpn_provider_set_string(provider, "Address", value);
143                         addressv4 = g_strdup(value);
144                 }
145
146                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
147                         vpn_provider_set_string(provider, "Netmask", value);
148                         netmask = g_strdup(value);
149                 }
150
151                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
152                         vpn_provider_set_string(provider, "DNS", value);
153                         nameservers = g_strdup(value);
154                 }
155
156                 if (!strcmp(key, "INTERNAL_IFNAME"))
157                         ifname = g_strdup(value);
158
159                 dbus_message_iter_next(&dict);
160         }
161
162         if (vpn_set_ifname(provider, ifname) < 0) {
163                 g_free(ifname);
164                 g_free(addressv4);
165                 g_free(netmask);
166                 g_free(nameservers);
167                 return VPN_STATE_FAILURE;
168         }
169
170         if (addressv4 != NULL)
171                 ipaddress = connman_ipaddress_alloc(AF_INET);
172
173         g_free(ifname);
174
175         if (ipaddress == NULL) {
176                 connman_error("No IP address for provider");
177                 g_free(addressv4);
178                 g_free(netmask);
179                 g_free(nameservers);
180                 return VPN_STATE_FAILURE;
181         }
182
183         value = vpn_provider_get_string(provider, "HostIP");
184         if (value != NULL) {
185                 vpn_provider_set_string(provider, "Gateway", value);
186                 gateway = g_strdup(value);
187         }
188
189         if (addressv4 != NULL)
190                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
191                                         gateway);
192
193         vpn_provider_set_ipaddress(provider, ipaddress);
194         vpn_provider_set_nameservers(provider, nameservers);
195
196         g_free(addressv4);
197         g_free(netmask);
198         g_free(gateway);
199         g_free(nameservers);
200         connman_ipaddress_free(ipaddress);
201
202         return VPN_STATE_CONNECT;
203 }
204
205 static int pptp_save(struct vpn_provider *provider, GKeyFile *keyfile)
206 {
207         const char *option;
208         int i;
209
210         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
211                 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0) {
212                         option = vpn_provider_get_string(provider,
213                                                         pptp_options[i].cm_opt);
214                         if (option == NULL)
215                                 continue;
216
217                         g_key_file_set_string(keyfile,
218                                         vpn_provider_get_save_group(provider),
219                                         pptp_options[i].cm_opt, option);
220                 }
221         }
222         return 0;
223 }
224
225 static void pptp_write_bool_option(struct connman_task *task,
226                                 const char *key, const char *value)
227 {
228         if (key != NULL && value != NULL) {
229                 if (strcasecmp(value, "yes") == 0 ||
230                                 strcasecmp(value, "true") == 0 ||
231                                 strcmp(value, "1") == 0)
232                         connman_task_add_argument(task, key, NULL);
233         }
234 }
235
236 static int pptp_connect(struct vpn_provider *provider,
237                 struct connman_task *task, const char *if_name)
238 {
239         const char *opt_s, *host;
240         char *str;
241         int err, i;
242
243         if (connman_task_set_notify(task, "getsec",
244                                         pptp_get_sec, provider))
245                 return -ENOMEM;
246
247         host = vpn_provider_get_string(provider, "Host");
248         if (host == NULL) {
249                 connman_error("Host not set; cannot enable VPN");
250                 return -EINVAL;
251         }
252
253         str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
254                                 PPTP, host);
255         if (str == NULL) {
256                 connman_error("can not allocate memory");
257                 return -ENOMEM;
258         }
259
260         connman_task_add_argument(task, "pty", str);
261         g_free(str);
262
263         connman_task_add_argument(task, "nodetach", NULL);
264         connman_task_add_argument(task, "lock", NULL);
265         connman_task_add_argument(task, "usepeerdns", NULL);
266         connman_task_add_argument(task, "noipdefault", NULL);
267         connman_task_add_argument(task, "noauth", NULL);
268         connman_task_add_argument(task, "nodefaultroute", NULL);
269         connman_task_add_argument(task, "ipparam", "pptp_plugin");
270
271         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
272                 opt_s = vpn_provider_get_string(provider,
273                                         pptp_options[i].cm_opt);
274                 if (opt_s == NULL)
275                         opt_s = pptp_options[i].vpnc_default;
276
277                 if (opt_s == NULL)
278                         continue;
279
280                 if (pptp_options[i].type == OPT_STRING)
281                         connman_task_add_argument(task,
282                                         pptp_options[i].pptp_opt, opt_s);
283                 else if (pptp_options[i].type == OPT_BOOL)
284                         pptp_write_bool_option(task,
285                                         pptp_options[i].pptp_opt, opt_s);
286         }
287
288         connman_task_add_argument(task, "plugin",
289                                 SCRIPTDIR "/libppp-plugin.so");
290
291         err = connman_task_run(task, vpn_died, provider,
292                                 NULL, NULL, NULL);
293         if (err < 0) {
294                 connman_error("pptp failed to start");
295                 return -EIO;
296         }
297
298         return 0;
299 }
300
301 static int pptp_error_code(int exit_code)
302 {
303
304         switch (exit_code) {
305         case 1:
306                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
307         case 2:
308                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
309         case 16:
310                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
311         default:
312                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
313         }
314 }
315
316 static struct vpn_driver vpn_driver = {
317         .flags          = VPN_FLAG_NO_TUN,
318         .notify         = pptp_notify,
319         .connect        = pptp_connect,
320         .error_code     = pptp_error_code,
321         .save           = pptp_save,
322 };
323
324 static int pptp_init(void)
325 {
326         connection = connman_dbus_get_connection();
327
328         return vpn_register("pptp", &vpn_driver, PPPD);
329 }
330
331 static void pptp_exit(void)
332 {
333         vpn_unregister("pptp");
334
335         dbus_connection_unref(connection);
336 }
337
338 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
339         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)