aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mozjs.c
blob: feaabd9c2372b4107d7f14bc944bddd130b8c768 (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
/*
 *
 *  PACrunner - Proxy configuration daemon
 *
 *  Copyright (C) 2010-2011  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 <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <pthread.h>

#include <netdb.h>
#include <arpa/inet.h>
#include <linux/if_arp.h>

#pragma GCC diagnostic ignored "-Wredundant-decls"
#include <jsapi.h>
#pragma GCC diagnostic error "-Wredundant-decls"

#include "javascript.h"

#include "pacrunner.h"
#include "js.h"

static pthread_mutex_t mozjs_mutex = PTHREAD_MUTEX_INITIALIZER;

struct pacrunner_mozjs {
	struct pacrunner_proxy *proxy;
	JSContext *jsctx;
	JSObject *jsobj;
};

static int getaddr(const char *node, char *host, size_t hostlen)
{
	struct sockaddr_in addr;
	struct ifreq ifr;
	int sk, err;

	sk = socket(PF_INET, SOCK_DGRAM, 0);
	if (sk < 0)
		return -EIO;

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, node, sizeof(ifr.ifr_name));

	err = ioctl(sk, SIOCGIFADDR, &ifr);

	close(sk);

	if (err < 0)
		return -EIO;

	memcpy(&addr, &ifr.ifr_addr, sizeof(addr));
	snprintf(host, hostlen, "%s", inet_ntoa(addr.sin_addr));

	return 0;
}

static int resolve(const char *node, char *host, size_t hostlen)
{
	struct addrinfo *info;
	int err;

	if (getaddrinfo(node, NULL, NULL, &info) < 0)
		return -EIO;

	err = getnameinfo(info->ai_addr, info->ai_addrlen,
				host, hostlen, NULL, 0, NI_NUMERICHOST);

	freeaddrinfo(info);

	if (err < 0)
		return -EIO;

	return 0;
}

static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp)
{
	struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx);
	const char *interface;
	char address[NI_MAXHOST];

	DBG("");

	JS_SET_RVAL(jsctx, vp, JSVAL_NULL);

	if (!ctx)
		return JS_TRUE;

	interface = pacrunner_proxy_get_interface(ctx->proxy);
	if (!interface)
		return JS_TRUE;

	if (getaddr(interface, address, sizeof(address)) < 0)
		return JS_TRUE;

	DBG("address %s", address);

	JS_SET_RVAL(jsctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx,
							       address)));

	return JS_TRUE;
}

static JSBool dnsresolve(JSContext *ctx, uintN argc, jsval *vp)
{
	char address[NI_MAXHOST];
	jsval *argv = JS_ARGV(ctx, vp);
	char *host = JS_EncodeString(ctx, JS_ValueToString(ctx, argv[0]));
	char **split_res;

	DBG("host %s", host);

	JS_SET_RVAL(ctx, vp, JSVAL_NULL);

	/* Q&D test on host to know if it is a proper hostname */
	split_res = g_strsplit_set(host, ":%?!,;@\\'*|<>{}[]()+=$&~# \"", -1);
	if (split_res) {
		int length = g_strv_length(split_res);
		g_strfreev(split_res);

		if (length > 1)
			goto out;
	}

	if (resolve(host, address, sizeof(address)) < 0)
		goto out;

	DBG("address %s", address);

	JS_SET_RVAL(ctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, address)));

 out:
	JS_free(ctx, host);
	return JS_TRUE;

}

static JSClass jscls = {
	"global", JSCLASS_GLOBAL_FLAGS,
	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
	JS_StrictPropertyStub,
	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};

static JSRuntime *jsrun;

static int create_object(struct pacrunner_proxy *proxy)
{
	struct pacrunner_mozjs *ctx;
	const char *script;
	jsval rval;

	script = pacrunner_proxy_get_script(proxy);
	if (!script)
		return 0;

	ctx = g_malloc0(sizeof(struct pacrunner_mozjs));

	ctx->proxy = proxy;
	ctx->jsctx = JS_NewContext(jsrun, 8 * 1024);
	if (!ctx->jsctx) {
		g_free(ctx);
		return -ENOMEM;
	}
	JS_SetContextPrivate(ctx->jsctx, ctx);
	__pacrunner_proxy_set_jsctx(proxy, ctx);

#if JS_VERSION >= 185
	ctx->jsobj = JS_NewCompartmentAndGlobalObject(ctx->jsctx, &jscls,
						      NULL);
#else
	ctx->jsobj = JS_NewObject(ctx->jsctx, &jscls, NULL, NULL);
#endif

	if (!JS_InitStandardClasses(ctx->jsctx, ctx->jsobj))
		pacrunner_error("Failed to init JS standard classes");

	JS_DefineFunction(ctx->jsctx, ctx->jsobj, "myIpAddress",
			  myipaddress, 0, 0);
	JS_DefineFunction(ctx->jsctx, ctx->jsobj,
			  "dnsResolve", dnsresolve, 1, 0);

	JS_EvaluateScript(ctx->jsctx, ctx->jsobj, JAVASCRIPT_ROUTINES,
			  strlen(JAVASCRIPT_ROUTINES), NULL, 0, &rval);

	JS_EvaluateScript(ctx->jsctx, ctx->jsobj, script, strlen(script),
			  "wpad.dat", 0, &rval);

	return 0;
}

static int mozjs_clear_proxy(struct pacrunner_proxy *proxy)
{
	struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);

	DBG("proxy %p ctx %p", proxy, ctx);

	if (!ctx)
		return -EINVAL;

	JS_DestroyContext(ctx->jsctx);
	__pacrunner_proxy_set_jsctx(proxy, NULL);

	return 0;
}

static int mozjs_set_proxy(struct pacrunner_proxy *proxy)
{
	DBG("proxy %p", proxy);

	if (!proxy)
		return 0;

	mozjs_clear_proxy(proxy);

	return create_object(proxy);
}

static char * mozjs_execute(struct pacrunner_proxy *proxy, const char *url,
			    const char *host)
{
	struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);
	JSBool result;
	jsval rval, args[2];
	char *answer, *g_answer;

	DBG("proxy %p ctx %p url %s host %s", proxy, ctx, url, host);

	if (!ctx)
		return NULL;

	pthread_mutex_lock(&mozjs_mutex);

	JS_BeginRequest(ctx->jsctx);

	args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx->jsctx, url));
	args[1] = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx->jsctx, host));

	result = JS_CallFunctionName(ctx->jsctx, ctx->jsobj,
				     "FindProxyForURL", 2, args, &rval);

	JS_EndRequest(ctx->jsctx);

	JS_MaybeGC(ctx->jsctx);

	pthread_mutex_unlock(&mozjs_mutex);

	if (result) {
		answer = JS_EncodeString(ctx->jsctx,
					 JS_ValueToString(ctx->jsctx, rval));
		g_answer = g_strdup(answer);
		JS_free(ctx->jsctx, answer);
		return g_answer;
	}

	return NULL;
}

static struct pacrunner_js_driver mozjs_driver = {
	.name		= "mozjs",
	.priority	= PACRUNNER_JS_PRIORITY_DEFAULT,
	.set_proxy	= mozjs_set_proxy,
	.clear_proxy	= mozjs_clear_proxy,
	.execute	= mozjs_execute,
};

static int mozjs_init(void)
{
	DBG("");

	jsrun = JS_NewRuntime(8 * 1024 * 1024);

	return pacrunner_js_driver_register(&mozjs_driver);
}

static void mozjs_exit(void)
{
	DBG("");

	pacrunner_js_driver_unregister(&mozjs_driver);

	JS_DestroyRuntime(jsrun);
}

PACRUNNER_PLUGIN_DEFINE(mozjs, mozjs_init, mozjs_exit)