session: Add configuration plugin
[platform/upstream/connman.git] / plugins / pptp.c
1 /*
2  *
3  *  Connection Manager
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 <glib.h>
34
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/provider.h>
38 #include <connman/log.h>
39 #include <connman/task.h>
40 #include <connman/dbus.h>
41 #include <connman/inet.h>
42
43 #include "vpn.h"
44
45 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
46
47 enum {
48         OPT_STRING = 1,
49         OPT_BOOL = 2,
50 };
51
52 struct {
53         const char *cm_opt;
54         const char *pptp_opt;
55         const char *vpnc_default;
56         int type;
57 } pptp_options[] = {
58         { "PPTP.User", "user", NULL, OPT_STRING },
59         { "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
60         { "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
61         { "PPTP.Debug", "debug", NULL, OPT_STRING },
62         { "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
63         { "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
64         { "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
65         { "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
66         { "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
67         { "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
68         { "PPTP.NoDeflate", "nodeflate", NULL, OPT_BOOL },
69         { "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
70         { "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
71         { "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
72         { "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
73         { "PPTP.NoVJ", "no-vj-comp", NULL, OPT_BOOL },
74 };
75
76 static DBusConnection *connection;
77
78 static DBusMessage *pptp_get_sec(struct connman_task *task,
79                                 DBusMessage *msg, void *user_data)
80 {
81         const char *user, *passwd;
82         struct connman_provider *provider = user_data;
83         DBusMessage *reply;
84
85         if (dbus_message_get_no_reply(msg) == TRUE)
86                 return NULL;
87
88         user = connman_provider_get_string(provider, "PPTP.User");
89         passwd = connman_provider_get_string(provider, "PPTP.Password");
90         if (user == NULL || strlen(user) == 0 ||
91                                 passwd == NULL || strlen(passwd) == 0)
92                 return NULL;
93
94         reply = dbus_message_new_method_return(msg);
95         if (reply == NULL)
96                 return NULL;
97
98         dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
99                                 DBUS_TYPE_STRING, &passwd,
100                                 DBUS_TYPE_INVALID);
101         return reply;
102 }
103
104 static int pptp_notify(DBusMessage *msg, struct connman_provider *provider)
105 {
106         DBusMessageIter iter, dict;
107         const char *reason, *key, *value;
108         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
109         char *ifname = NULL, *nameservers = NULL;
110         struct connman_ipaddress *ipaddress = NULL;
111
112         dbus_message_iter_init(msg, &iter);
113
114         dbus_message_iter_get_basic(&iter, &reason);
115         dbus_message_iter_next(&iter);
116
117         if (provider == NULL) {
118                 connman_error("No provider found");
119                 return VPN_STATE_FAILURE;
120         }
121
122         if (strcmp(reason, "auth failed") == 0)
123                 return VPN_STATE_AUTH_FAILURE;
124
125         if (strcmp(reason, "connect"))
126                 return VPN_STATE_DISCONNECT;
127
128         dbus_message_iter_recurse(&iter, &dict);
129
130         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
131                 DBusMessageIter entry;
132
133                 dbus_message_iter_recurse(&dict, &entry);
134                 dbus_message_iter_get_basic(&entry, &key);
135                 dbus_message_iter_next(&entry);
136                 dbus_message_iter_get_basic(&entry, &value);
137
138                 DBG("%s = %s", key, value);
139
140                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
141                         connman_provider_set_string(provider, "Address", value);
142                         addressv4 = g_strdup(value);
143                 }
144
145                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
146                         connman_provider_set_string(provider, "Netmask", value);
147                         netmask = g_strdup(value);
148                 }
149
150                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
151                         connman_provider_set_string(provider, "DNS", value);
152                         nameservers = g_strdup(value);
153                 }
154
155                 if (!strcmp(key, "INTERNAL_IFNAME"))
156                         ifname = g_strdup(value);
157
158                 dbus_message_iter_next(&dict);
159         }
160
161         if (vpn_set_ifname(provider, ifname) < 0) {
162                 g_free(ifname);
163                 g_free(addressv4);
164                 g_free(netmask);
165                 g_free(nameservers);
166                 return VPN_STATE_FAILURE;
167         }
168
169         if (addressv4 != NULL)
170                 ipaddress = connman_ipaddress_alloc(AF_INET);
171
172         g_free(ifname);
173
174         if (ipaddress == NULL) {
175                 connman_error("No IP address for provider");
176                 g_free(addressv4);
177                 g_free(netmask);
178                 g_free(nameservers);
179                 return VPN_STATE_FAILURE;
180         }
181
182         value = connman_provider_get_string(provider, "HostIP");
183         if (value != NULL) {
184                 connman_provider_set_string(provider, "Gateway", value);
185                 gateway = g_strdup(value);
186         }
187
188         if (addressv4 != NULL)
189                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
190                                         gateway);
191
192         connman_provider_set_ipaddress(provider, ipaddress);
193         connman_provider_set_nameservers(provider, nameservers);
194
195         g_free(addressv4);
196         g_free(netmask);
197         g_free(gateway);
198         g_free(nameservers);
199         connman_ipaddress_free(ipaddress);
200
201         return VPN_STATE_CONNECT;
202 }
203
204 static int pptp_save(struct connman_provider *provider, GKeyFile *keyfile)
205 {
206         const char *option;
207         int i;
208
209         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
210                 if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0) {
211                         option = connman_provider_get_string(provider,
212                                                         pptp_options[i].cm_opt);
213                         if (option == NULL)
214                                 continue;
215
216                         g_key_file_set_string(keyfile,
217                                         connman_provider_get_save_group(provider),
218                                         pptp_options[i].cm_opt, option);
219                 }
220         }
221         return 0;
222 }
223
224 static void pptp_write_bool_option(struct connman_task *task,
225                                 const char *key, const char *value)
226 {
227         if (key != NULL && value != NULL) {
228                 if (strcasecmp(value, "yes") == 0 ||
229                                 strcasecmp(value, "true") == 0 ||
230                                 strcmp(value, "1") == 0)
231                         connman_task_add_argument(task, key, NULL);
232         }
233 }
234
235 static int pptp_connect(struct connman_provider *provider,
236                 struct connman_task *task, const char *if_name)
237 {
238         const char *opt_s, *host;
239         char *str;
240         int err, i;
241
242         if (connman_task_set_notify(task, "getsec",
243                                         pptp_get_sec, provider))
244                 return -ENOMEM;
245
246         host = connman_provider_get_string(provider, "Host");
247         if (host == NULL) {
248                 connman_error("Host not set; cannot enable VPN");
249                 return -EINVAL;
250         }
251
252         str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
253                                 PPTP, host);
254         if (str == NULL) {
255                 connman_error("can not allocate memory");
256                 return -ENOMEM;
257         }
258
259         connman_task_add_argument(task, "pty", str);
260         g_free(str);
261
262         connman_task_add_argument(task, "nodetach", NULL);
263         connman_task_add_argument(task, "lock", NULL);
264         connman_task_add_argument(task, "usepeerdns", NULL);
265         connman_task_add_argument(task, "noipdefault", NULL);
266         connman_task_add_argument(task, "noauth", NULL);
267         connman_task_add_argument(task, "nodefaultroute", NULL);
268         connman_task_add_argument(task, "ipparam", "pptp_plugin");
269
270         for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
271                 opt_s = connman_provider_get_string(provider,
272                                         pptp_options[i].cm_opt);
273                 if (opt_s == NULL)
274                         opt_s = pptp_options[i].vpnc_default;
275
276                 if (opt_s == NULL)
277                         continue;
278
279                 if (pptp_options[i].type == OPT_STRING)
280                         connman_task_add_argument(task,
281                                         pptp_options[i].pptp_opt, opt_s);
282                 else if (pptp_options[i].type == OPT_BOOL)
283                         pptp_write_bool_option(task,
284                                         pptp_options[i].pptp_opt, opt_s);
285         }
286
287         connman_task_add_argument(task, "plugin",
288                                 SCRIPTDIR "/libppp-plugin.so");
289
290         err = connman_task_run(task, vpn_died, provider,
291                                 NULL, NULL, NULL);
292         if (err < 0) {
293                 connman_error("pptp failed to start");
294                 return -EIO;
295         }
296
297         return 0;
298 }
299
300 static int pptp_error_code(int exit_code)
301 {
302
303         switch (exit_code) {
304         case 1:
305                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
306         case 2:
307                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
308         case 16:
309                 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
310         default:
311                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
312         }
313 }
314
315 static struct vpn_driver vpn_driver = {
316         .flags          = VPN_FLAG_NO_TUN,
317         .notify         = pptp_notify,
318         .connect        = pptp_connect,
319         .error_code     = pptp_error_code,
320         .save           = pptp_save,
321 };
322
323 static int pptp_init(void)
324 {
325         connection = connman_dbus_get_connection();
326
327         return vpn_register("pptp", &vpn_driver, PPPD);
328 }
329
330 static void pptp_exit(void)
331 {
332         vpn_unregister("pptp");
333
334         dbus_connection_unref(connection);
335 }
336
337 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
338         CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)