summaryrefslogtreecommitdiff
path: root/plugins/pptp.c
blob: 9db1b896f7471ac257841c342b5995246357244b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <net/if.h>

#include <glib.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/provider.h>
#include <connman/log.h>
#include <connman/task.h>
#include <connman/dbus.h>
#include <connman/inet.h>

#include "vpn.h"

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

enum {
	OPT_STRING = 1,
	OPT_BOOL = 2,
};

struct {
	const char *cm_opt;
	const char *pptp_opt;
	const char *vpnc_default;
	int type;
} pptp_options[] = {
	{ "PPTP.User", "user", NULL, OPT_STRING },
	{ "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
	{ "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
	{ "PPTP.Debug", "debug", NULL, OPT_STRING },
	{ "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
	{ "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
	{ "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
	{ "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
	{ "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
	{ "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
	{ "PPTP.NoDeflate", "nodeflatey", NULL, OPT_BOOL },
	{ "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
	{ "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
	{ "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
	{ "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
	{ "PPTP.NoVJ", "no-vj-comp", NULL, OPT_BOOL },
};

static DBusConnection *connection;

static DBusMessage *pptp_get_sec(struct connman_task *task,
				DBusMessage *msg, void *user_data)
{
	const char *user, *passwd;
	struct connman_provider *provider = user_data;
	DBusMessage *reply;

	if (dbus_message_get_no_reply(msg) == TRUE)
		return NULL;

	user = connman_provider_get_string(provider, "PPTP.User");
	passwd = connman_provider_get_string(provider, "PPTP.Password");
	if (user == NULL || strlen(user) == 0 ||
				passwd == NULL || strlen(passwd) == 0)
		return NULL;

	reply = dbus_message_new_method_return(msg);
	if (reply == NULL)
		return NULL;

	dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
				DBUS_TYPE_STRING, &passwd,
				DBUS_TYPE_INVALID);
	return reply;
}

static int pptp_notify(DBusMessage *msg, struct connman_provider *provider)
{
	DBusMessageIter iter, dict;
	const char *reason, *key, *value;
	char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
	char *ifname = NULL, *nameservers = NULL;
	struct connman_ipaddress *ipaddress = NULL;

	dbus_message_iter_init(msg, &iter);

	dbus_message_iter_get_basic(&iter, &reason);
	dbus_message_iter_next(&iter);

	if (provider == NULL) {
		connman_error("No provider found");
		return VPN_STATE_FAILURE;
	}

	if (strcmp(reason, "auth failed") == 0)
		return VPN_STATE_AUTH_FAILURE;

	if (strcmp(reason, "connect"))
		return VPN_STATE_DISCONNECT;

	dbus_message_iter_recurse(&iter, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry;

		dbus_message_iter_recurse(&dict, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
		dbus_message_iter_get_basic(&entry, &value);

		DBG("%s = %s", key, value);

		if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
			connman_provider_set_string(provider, "Address", value);
			addressv4 = g_strdup(value);
		}

		if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
			connman_provider_set_string(provider, "Netmask", value);
			netmask = g_strdup(value);
		}

		if (!strcmp(key, "INTERNAL_IP4_DNS")) {
			connman_provider_set_string(provider, "DNS", value);
			nameservers = g_strdup(value);
		}

		if (!strcmp(key, "INTERNAL_IFNAME"))
			ifname = g_strdup(value);

		dbus_message_iter_next(&dict);
	}

	if (vpn_set_ifname(provider, ifname) < 0) {
		g_free(ifname);
		g_free(addressv4);
		g_free(netmask);
		g_free(nameservers);
		return VPN_STATE_FAILURE;
	}

	if (addressv4 != NULL)
		ipaddress = connman_ipaddress_alloc(AF_INET);

	g_free(ifname);

	if (ipaddress == NULL) {
		connman_error("No IP address for provider");
		g_free(addressv4);
		g_free(netmask);
		g_free(nameservers);
		return VPN_STATE_FAILURE;
	}

	value = connman_provider_get_string(provider, "Host");
	if (value != NULL) {
		connman_provider_set_string(provider, "Gateway", value);
		gateway = g_strdup(value);
	}

	if (addressv4 != NULL)
		connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
					gateway);

	connman_provider_set_ipaddress(provider, ipaddress);
	connman_provider_set_nameservers(provider, nameservers);

	g_free(addressv4);
	g_free(netmask);
	g_free(gateway);
	g_free(nameservers);
	connman_ipaddress_free(ipaddress);

	return VPN_STATE_CONNECT;
}

static int pptp_save(struct connman_provider *provider, GKeyFile *keyfile)
{
	const char *option;
	int i;

	for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
		if (strncmp(pptp_options[i].cm_opt, "PPTP.", 5) == 0) {
			option = connman_provider_get_string(provider,
							pptp_options[i].cm_opt);
			if (option == NULL)
				continue;

			g_key_file_set_string(keyfile,
					connman_provider_get_save_group(provider),
					pptp_options[i].cm_opt, option);
		}
	}
	return 0;
}

static void pptp_write_bool_option(struct connman_task *task,
				const char *key, const char *value)
{
	if (key != NULL && value != NULL) {
		if (strcmp(value, "yes") == 0)
			connman_task_add_argument(task, key, NULL);
	}
}

static int pptp_connect(struct connman_provider *provider,
		struct connman_task *task, const char *if_name)
{
	const char *opt_s, *host;
	char *str;
	int err, i;

	if (connman_task_set_notify(task, "getsec",
					pptp_get_sec, provider))
		return -ENOMEM;

	host = connman_provider_get_string(provider, "Host");
	if (host == NULL) {
		connman_error("Host not set; cannot enable VPN");
		return -EINVAL;
	}

	str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
				PPTP, host);
	if (str == NULL) {
		connman_error("can not allocate memory");
		return -ENOMEM;
	}

	connman_task_add_argument(task, "pty", str);
	g_free(str);

	connman_task_add_argument(task, "nodetach", NULL);
	connman_task_add_argument(task, "lock", NULL);
	connman_task_add_argument(task, "usepeerdns", NULL);
	connman_task_add_argument(task, "noipdefault", NULL);
	connman_task_add_argument(task, "noauth", NULL);
	connman_task_add_argument(task, "nodefaultroute", NULL);
	connman_task_add_argument(task, "ipparam", "pptp_plugin");

	for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
		opt_s = connman_provider_get_string(provider,
					pptp_options[i].cm_opt);
		if (opt_s == NULL)
			opt_s = pptp_options[i].vpnc_default;

		if (opt_s == NULL)
			continue;

		if (pptp_options[i].type == OPT_STRING)
			connman_task_add_argument(task,
					pptp_options[i].pptp_opt, opt_s);
		else if (pptp_options[i].type == OPT_BOOL)
			pptp_write_bool_option(task,
					pptp_options[i].pptp_opt, opt_s);
	}

	connman_task_add_argument(task, "plugin",
				SCRIPTDIR "/libppp-plugin.so");

	err = connman_task_run(task, vpn_died, provider,
				NULL, NULL, NULL);
	if (err < 0) {
		connman_error("pptp failed to start");
		return -EIO;
	}

	return 0;
}

static int pptp_error_code(int exit_code)
{

	switch (exit_code) {
	case 1:
		return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
	case 2:
		return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
	case 16:
		return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
	default:
		return CONNMAN_PROVIDER_ERROR_UNKNOWN;
	}
}

static struct vpn_driver vpn_driver = {
	.flags		= VPN_FLAG_NO_TUN,
	.notify		= pptp_notify,
	.connect	= pptp_connect,
	.error_code     = pptp_error_code,
	.save		= pptp_save,
};

static int pptp_init(void)
{
	connection = connman_dbus_get_connection();

	return vpn_register("pptp", &vpn_driver, PPPD);
}

static void pptp_exit(void)
{
	vpn_unregister("pptp");

	dbus_connection_unref(connection);
}

CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
	CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)