Debian Codegraph @

src/backend/plugins/config-windows/config-windows.c

raw
1 /* config-windows.c
2 *
3 * Copyright 2022-2023 The Libproxy Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <windows.h>
23 #include <winreg.h>
24
25 #include <gio/gio.h>
26
27 #include "config-windows.h"
28
29 #include "px-plugin-config.h"
30 #include "px-manager.h"
31
32 #define W32REG_OFFSET_PAC (1 << 2)
33 #define W32REG_OFFSET_WPAD (1 << 3)
34 #define W32REG_BASEKEY "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
35 #define W32REG_BUFFLEN 1024
36
37 struct _PxConfigWindows {
38 GObject parent_instance;
39 };
40
41 static void px_config_iface_init (PxConfigInterface *iface);
42
43 G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigWindows,
44 px_config_windows,
45 G_TYPE_OBJECT,
46 G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init))
47
48 enum {
49 PROP_0,
50 PROP_CONFIG_OPTION
51 };
52
53 static void
54 px_config_windows_set_property (GObject *object,
55 guint prop_id,
56 const GValue *value,
57 GParamSpec *pspec)
58 {
59 switch (prop_id) {
60 case PROP_CONFIG_OPTION:
61 break;
62 default:
63 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
64 break;
65 }
66 }
67
68 static void
69 px_config_windows_get_property (GObject *object,
70 guint prop_id,
71 GValue *value,
72 GParamSpec *pspec)
73 {
74 switch (prop_id) {
75 default:
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 break;
78 }
79 }
80
81 static void
82 px_config_windows_init (PxConfigWindows *self)
83 {
84 }
85
86 static void
87 px_config_windows_class_init (PxConfigWindowsClass *klass)
88 {
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
91 object_class->set_property = px_config_windows_set_property;
92 object_class->get_property = px_config_windows_get_property;
93
94 g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option");
95 }
96
97 static gboolean
98 get_registry (const char *key,
99 const char *name,
100 char **sval,
101 guint32 *slen,
102 guint32 *ival)
103 {
104 HKEY hkey;
105 LONG result;
106 DWORD type;
107 DWORD buflen = W32REG_BUFFLEN;
108 BYTE buffer[W32REG_BUFFLEN];
109
110 if (sval && ival)
111 return FALSE;
112
113 if (RegOpenKeyExA (HKEY_CURRENT_USER, key, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
114 return FALSE;
115
116 result = RegQueryValueExA (hkey, name, NULL, &type, buffer, &buflen);
117 RegCloseKey (hkey);
118
119 if (result != ERROR_SUCCESS)
120 return FALSE;
121
122 switch (type) {
123 case REG_BINARY:
124 case REG_EXPAND_SZ:
125 case REG_SZ:
126 if (!sval)
127 return FALSE;
128 if (slen)
129 *slen = buflen;
130
131 *sval = g_malloc0 (buflen);
132 return memcpy (*sval, buffer, buflen) != NULL;
133 case REG_DWORD:
134 if (ival)
135 return memcpy (ival, buffer, buflen < sizeof (guint32) ? buflen : sizeof (guint32)) != NULL;
136 default:
137 break;
138 }
139
140 return FALSE;
141 }
142
143 static char *
144 build_proxy_uri (const char *uri)
145 {
146 char *proxy_server = NULL;
147 if (!g_strstr_len (uri, -1, "://")) {
148 proxy_server = g_strdup_printf ("http://%s", uri);
149 } else {
150 proxy_server = g_strdup (uri);
151 }
152
153 return proxy_server;
154 }
155
156 static GHashTable *
157 parse_manual (char *manual)
158 {
159 g_auto (GStrv) split = NULL;
160 GHashTable *ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
161
162 /* We have to check for two formats:
163 * - [scheme://]1.2.3.4:8080
164 * - ftp=[scheme://]1.2.4.5:8080;https=[scheme://]1.2.3.4:8080
165 * where each entry may or may not contain a scheme to use for the proxy host.
166 * No scheme implies http://
167 */
168
169 split = g_strsplit (manual, ";", -1);
170 for (int idx = 0; idx < g_strv_length (split); idx++) {
171 if (!strchr (split[idx], '=')) {
172 char *proxy_uri = build_proxy_uri (split[idx]);
173 /* When a proxy server is provided without any scheme specifier, */
174 /* it should be used for all schemes that do not have an explicit entry */
175 g_hash_table_insert (ret, g_strdup ("default"), proxy_uri);
176 } else {
177 g_auto (GStrv) split_kv = g_strsplit (split[idx], "=", -1);
178
179 if (g_strv_length (split_kv) == 2) {
180 char *proxy_uri = build_proxy_uri (split_kv[1]);
181 g_hash_table_insert (ret, g_strdup (split_kv[0]), proxy_uri);
182 }
183 }
184 }
185
186 return ret;
187 }
188
189 static gboolean
190 is_enabled (char type)
191 {
192 g_autofree char *data = NULL;
193 guint32 dlen = 0;
194 gboolean result = FALSE;
195
196 if (!get_registry (W32REG_BASEKEY "\\Connections", "DefaultConnectionSettings", &data, &dlen, NULL))
197 return FALSE;
198
199 if (dlen >= 9)
200 result = (data[8] & type) == type;
201
202 return result;
203 }
204
205 static void
206 px_config_windows_get_config (PxConfig *self,
207 GUri *uri,
208 GStrvBuilder *builder)
209 {
210 g_autofree char *tmp1 = NULL;
211 g_autofree char *tmp2 = NULL;
212 g_autofree char *tmp3 = NULL;
213 guint32 enabled = 0;
214
215 if (get_registry (W32REG_BASEKEY, "ProxyOverride", &tmp1, NULL, NULL)) {
216 g_auto (GStrv) no_proxy = g_strsplit (tmp1, ";", -1);
217
218 if (px_manager_is_ignore (uri, no_proxy))
219 return;
220 }
221
222 /* WPAD */
223 if (is_enabled (W32REG_OFFSET_WPAD)) {
224 px_strv_builder_add_proxy (builder, "wpad://");
225 }
226
227 /* PAC */
228 if (is_enabled (W32REG_OFFSET_PAC) && get_registry (W32REG_BASEKEY, "AutoConfigURL", &tmp2, NULL, NULL)) {
229 g_autofree char *pac_uri = g_strconcat ("pac+", tmp2, NULL);
230 GUri *ac_uri = g_uri_parse (tmp2, G_URI_FLAGS_NONE, NULL);
231
232 if (ac_uri) {
233 px_strv_builder_add_proxy (builder, pac_uri);
234 }
235 }
236
237 /* Manual proxy */
238 if (get_registry (W32REG_BASEKEY, "ProxyEnable", NULL, NULL, &enabled) && enabled && get_registry (W32REG_BASEKEY, "ProxyServer", &tmp3, NULL, NULL)) {
239 g_autoptr (GHashTable) table = parse_manual (tmp3);
240 const char *scheme = g_uri_get_scheme (uri);
241
242 if (table) {
243 char *ret = g_hash_table_lookup (table, scheme);
244 if (ret) {
245 px_strv_builder_add_proxy (builder, ret);
246 return;
247 }
248
249 ret = g_hash_table_lookup (table, "socks");
250 if (ret) {
251 px_strv_builder_add_proxy (builder, ret);
252 return;
253 }
254
255 ret = g_hash_table_lookup (table, "default");
256 if (ret) {
257 px_strv_builder_add_proxy (builder, ret);
258 return;
259 }
260 }
261 }
262 }
263
264 static void
265 px_config_iface_init (PxConfigInterface *iface)
266 {
267 iface->name = "config-windows";
268 iface->priority = PX_CONFIG_PRIORITY_DEFAULT;
269 iface->get_config = px_config_windows_get_config;
270 }