summaryrefslogtreecommitdiff
path: root/libproxy/modules/config_w32reg.cpp
blob: 675c75577a0a319c0141f20220066c1ad3f73e65 (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
/*******************************************************************************
 * Copyright (C) 2009 Nathaniel McCallum <nathaniel@natemccallum.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#include <algorithm>
#include "../extension_config.hpp"
using namespace libproxy;

#define W32REG_OFFSET_PAC  (1 << 2)
#define W32REG_OFFSET_WPAD (1 << 3)
#define W32REG_BASEKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
#define W32REG_BUFFLEN 1024

static bool get_registry(const char *key, const char *name, char **sval, uint32_t *slen, uint32_t *ival) {
	HKEY  hkey;
	LONG  result;
	DWORD type;
	DWORD buflen = W32REG_BUFFLEN;
	BYTE  buffer[W32REG_BUFFLEN];

	// Don't allow the caller to specify both sval and ival
	if (sval && ival)
		return false;

	// Open the key
	if (RegOpenKeyExA(HKEY_CURRENT_USER, key, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
		return false;

	// Read the value
	result = RegQueryValueExA(hkey, name, NULL, &type, buffer, &buflen);

	// Close the key
	RegCloseKey(hkey);

	// Evaluate
	if (result != ERROR_SUCCESS)
		return false;
	switch (type)
	{
		case REG_BINARY:
		case REG_EXPAND_SZ:
		case REG_SZ:
			if (!sval) return false;
			if (slen) *slen = buflen;
			*sval = new char[buflen];
			return memcpy(*sval, buffer, buflen) != NULL;
		case REG_DWORD:
			if (ival) return memcpy(ival, buffer, buflen < sizeof(uint32_t) ? buflen : sizeof(uint32_t)) != NULL;
	}
	return false;
}

static bool is_enabled(uint8_t type) {
	char    *data = NULL;
	uint32_t dlen = 0;
	bool   result = false;

	// Get the binary value DefaultConnectionSettings
	if (!get_registry(W32REG_BASEKEY "\\Connections", "DefaultConnectionSettings", &data, &dlen, NULL))
		return false;

	// WPAD and PAC are contained in the 9th value
	if (dlen >= 9)
		result = (data[8] & type) == type; // Check to see if the bit is set

	delete data;
	return result;
}

static map<string, string> parse_manual(string data) {
	// ProxyServer comes in two formats:
	//   1.2.3.4:8080 or ftp=1.2.3.4:8080;https=1.2.3.4:8080...
	map<string, string> rval;

	// If we have the second format, do recursive parsing,
	// then handle just the first entry
	if (data.find(";") != string::npos) {
		rval = parse_manual(data.substr(data.find(";")+1));
		data = data.substr(0, data.find(";"));
	}

	// If we have the first format, just assign HTTP and we're done
	if (data.find("=") == string::npos) {
		rval["http"] = string("http://") + data;
		return rval;
	}

	// Otherwise set the value for this single entry and return
	string protocol = data.substr(0, data.find("="));
	try { rval[protocol] = url(protocol + "://" + data.substr(data.find("=")+1)).to_string(); }
	catch (parse_error&) {}

	return rval;
}

class w32reg_config_extension : public config_extension {
public:
	vector<url> get_config(const url &dst) {
		char        *tmp = NULL;
		uint32_t enabled = 0;
		vector<url> response;

		// WPAD
		if (is_enabled(W32REG_OFFSET_WPAD)) {
			response.push_back(url("wpad://"));
			return response;
		}

		// PAC
		if (is_enabled(W32REG_OFFSET_PAC) &&
			get_registry(W32REG_BASEKEY, "AutoConfigURL", &tmp, NULL, NULL) &&
			url::is_valid(string("pac+") + tmp)) {
			response.push_back(url(string("pac+") + tmp));
			delete tmp;
			return response;
		}

		// Manual proxy
		// Check to see if we are enabled and get the value of ProxyServer
		if (get_registry(W32REG_BASEKEY, "ProxyEnable", NULL, NULL, &enabled) && enabled &&
			get_registry(W32REG_BASEKEY, "ProxyServer", &tmp, NULL, NULL)) {
			map<string, string> manual = parse_manual(tmp);
			delete tmp;

			// First we look for an exact match
			if (manual.find(dst.get_scheme()) != manual.end())
				response.push_back(manual[dst.get_scheme()]);

			// Next we look for http
			else if (manual.find("http") != manual.end())
				response.push_back(manual["http"]);

			// Last we look for socks
			else if (manual.find("socks") != manual.end())
				response.push_back(manual["socks"]);

			return response;
		}

		// Direct
		response.push_back(url("direct://"));
		return response;
	}

	string get_ignore(const url &dst) {
		char *tmp;
		if (get_registry(W32REG_BASEKEY, "ProxyOverride", &tmp, NULL, NULL)) {
			string po = tmp;
			delete tmp;
			const char windowsDelimiter = ';';
			const char libproxyDelimiter = ',';
			replace(po.begin(), po.end(), windowsDelimiter, libproxyDelimiter );
			if (po.length()>0)
				return po;
		}
		return "";
	}
};

MM_MODULE_INIT_EZ(w32reg_config_extension, true, NULL, NULL);