summaryrefslogtreecommitdiff
path: root/curlfilter.c
blob: 5984edca62c8d40ceb2eba1ccbd3452f8c5e20e4 (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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <curl/curl.h>
#include <pcre.h>
/* the function is actually a macro, which prevents us from redefining it
 * properly */
#undef curl_easy_setopt
 
typedef CURLcode (*orig_curl_easy_setopt) (CURL *handle, CURLoption option, ...); 

static pcre *re = NULL;

CURL_EXTERN CURLcode curl_easy_setopt (CURL *handle, CURLoption option, ...) {
	va_list args;
	va_start (args, option);
	void *parameter = va_arg (args, void *);

	if (re != NULL && option == CURLOPT_URL) {
		int matches[3];
		if (pcre_exec (re, NULL, parameter, strlen (parameter), 0, 0, matches, 3) >= 0) {
			/* not allowed */
			return CURLE_URL_MALFORMAT;
		}
	}

    orig_curl_easy_setopt orig_setopt;
	orig_setopt = (orig_curl_easy_setopt) dlsym (RTLD_NEXT, "curl_easy_setopt");
    return orig_setopt (handle, option, parameter);
}

void init () {
	const char * const pattern = getenv ("CURLFILTER_PATTERN");
	const char *errmsg = NULL;
	int errpos;
	if (pattern == NULL) {
		fprintf (stderr, "Environment variable CURLFILTER_PATTERN is not set\n");
		return;
	}
	if ((re = pcre_compile (pattern, 0, &errmsg, &errpos, NULL)) == NULL) {
		fprintf (stderr, "Cannot compile regex %s: %s\n", pattern, errmsg);
		return;
	}
}