| 1 |
/* config-osx.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 <SystemConfiguration/SystemConfiguration.h> |
| 23 |
|
| 24 |
#include <gio/gio.h> |
| 25 |
|
| 26 |
#include "config-osx.h" |
| 27 |
|
| 28 |
#include "px-plugin-config.h" |
| 29 |
#include "px-manager.h" |
| 30 |
|
| 31 |
static void px_config_iface_init (PxConfigInterface *iface); |
| 32 |
|
| 33 |
struct _PxConfigOsX { |
| 34 |
GObject parent_instance; |
| 35 |
}; |
| 36 |
|
| 37 |
G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigOsX, |
| 38 |
px_config_osx, |
| 39 |
G_TYPE_OBJECT, |
| 40 |
G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init)) |
| 41 |
|
| 42 |
enum { |
| 43 |
PROP_0, |
| 44 |
PROP_CONFIG_OPTION |
| 45 |
}; |
| 46 |
|
| 47 |
static void |
| 48 |
px_config_osx_set_property (GObject *object, |
| 49 |
guint prop_id, |
| 50 |
const GValue *value, |
| 51 |
GParamSpec *pspec) |
| 52 |
{ |
| 53 |
switch (prop_id) { |
| 54 |
case PROP_CONFIG_OPTION: |
| 55 |
break; |
| 56 |
default: |
| 57 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
px_config_osx_get_property (GObject *object, |
| 64 |
guint prop_id, |
| 65 |
GValue *value, |
| 66 |
GParamSpec *pspec) |
| 67 |
{ |
| 68 |
switch (prop_id) { |
| 69 |
default: |
| 70 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
px_config_osx_init (PxConfigOsX *self) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
px_config_osx_class_init (PxConfigOsXClass *klass) |
| 82 |
{ |
| 83 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 84 |
|
| 85 |
object_class->set_property = px_config_osx_set_property; |
| 86 |
object_class->get_property = px_config_osx_get_property; |
| 87 |
|
| 88 |
g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option"); |
| 89 |
} |
| 90 |
|
| 91 |
static CFNumberRef |
| 92 |
getobj (CFDictionaryRef settings, |
| 93 |
char *key) |
| 94 |
{ |
| 95 |
CFStringRef k; |
| 96 |
CFNumberRef retval; |
| 97 |
|
| 98 |
if (!settings) |
| 99 |
return NULL; |
| 100 |
|
| 101 |
k = CFStringCreateWithCString (NULL, key, kCFStringEncodingMacRoman); |
| 102 |
if (!k) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
retval = (CFNumberRef)CFDictionaryGetValue (settings, k); |
| 106 |
|
| 107 |
CFRelease (k); |
| 108 |
return retval; |
| 109 |
} |
| 110 |
|
| 111 |
static CFStringRef |
| 112 |
getobj_str (CFDictionaryRef settings, |
| 113 |
char *key) |
| 114 |
{ |
| 115 |
CFStringRef k; |
| 116 |
CFStringRef retval; |
| 117 |
|
| 118 |
if (!settings) |
| 119 |
return NULL; |
| 120 |
|
| 121 |
k = CFStringCreateWithCString (NULL, key, kCFStringEncodingMacRoman); |
| 122 |
if (!k) |
| 123 |
return NULL; |
| 124 |
|
| 125 |
retval = (CFStringRef)CFDictionaryGetValue (settings, k); |
| 126 |
|
| 127 |
CFRelease (k); |
| 128 |
return retval; |
| 129 |
} |
| 130 |
|
| 131 |
static CFArrayRef |
| 132 |
getobj_array (CFDictionaryRef settings, |
| 133 |
char *key) |
| 134 |
{ |
| 135 |
CFStringRef k; |
| 136 |
CFArrayRef retval; |
| 137 |
|
| 138 |
if (!settings) |
| 139 |
return NULL; |
| 140 |
|
| 141 |
k = CFStringCreateWithCString (NULL, key, kCFStringEncodingMacRoman); |
| 142 |
if (!k) |
| 143 |
return NULL; |
| 144 |
|
| 145 |
retval = (CFArrayRef)CFDictionaryGetValue (settings, k); |
| 146 |
|
| 147 |
CFRelease (k); |
| 148 |
return retval; |
| 149 |
} |
| 150 |
|
| 151 |
static char * |
| 152 |
str (CFStringRef ref) |
| 153 |
{ |
| 154 |
CFIndex size = CFStringGetLength (ref) + 1; |
| 155 |
char *ret = g_malloc0 (size); |
| 156 |
|
| 157 |
CFStringGetCString (ref, ret, size, kCFStringEncodingUTF8); |
| 158 |
|
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
static gboolean |
| 163 |
getint (CFDictionaryRef settings, |
| 164 |
char *key, |
| 165 |
int64_t *answer) |
| 166 |
{ |
| 167 |
CFNumberRef n = getobj (settings, key); |
| 168 |
|
| 169 |
if (!n) |
| 170 |
return FALSE; |
| 171 |
|
| 172 |
if (!CFNumberGetValue (n, kCFNumberSInt64Type, answer)) |
| 173 |
return FALSE; |
| 174 |
|
| 175 |
return TRUE; |
| 176 |
} |
| 177 |
|
| 178 |
static gboolean |
| 179 |
getbool (CFDictionaryRef settings, |
| 180 |
char *key) |
| 181 |
{ |
| 182 |
int64_t i = 0; |
| 183 |
|
| 184 |
if (!getint (settings, key, &i)) |
| 185 |
return FALSE; |
| 186 |
|
| 187 |
return i != 0; |
| 188 |
} |
| 189 |
|
| 190 |
static char * |
| 191 |
str_to_upper (const char *str) |
| 192 |
{ |
| 193 |
char *ret = NULL; |
| 194 |
int idx; |
| 195 |
|
| 196 |
if (!str) |
| 197 |
return NULL; |
| 198 |
|
| 199 |
ret = g_malloc0 (strlen (str) + 1); |
| 200 |
|
| 201 |
for (idx = 0; idx < strlen (str); idx++) |
| 202 |
ret[idx] = g_ascii_toupper (str[idx]); |
| 203 |
|
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
static char * |
| 208 |
protocol_url (CFDictionaryRef settings, |
| 209 |
char *protocol) |
| 210 |
{ |
| 211 |
g_autofree char *tmp = NULL; |
| 212 |
g_autoptr (GString) ret = NULL; |
| 213 |
g_autofree char *host = NULL; |
| 214 |
int64_t port; |
| 215 |
CFStringRef ref; |
| 216 |
|
| 217 |
/* Check ProtocolEnabled */ |
| 218 |
tmp = g_strconcat (protocol, "Enable", NULL); |
| 219 |
if (!getbool (settings, tmp)) { |
| 220 |
g_debug ("%s: %s not set", __FUNCTION__, tmp); |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
g_clear_pointer (&tmp, g_free); |
| 224 |
|
| 225 |
/* Get ProtocolPort */ |
| 226 |
tmp = g_strconcat (protocol, "Port", NULL); |
| 227 |
getint (settings, tmp, &port); |
| 228 |
if (!port) { |
| 229 |
g_debug ("%s: %s not set", __FUNCTION__, tmp); |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
g_clear_pointer (&tmp, g_free); |
| 233 |
|
| 234 |
/* Get ProtocolProxy */ |
| 235 |
tmp = g_strconcat (protocol, "Proxy", NULL); |
| 236 |
ref = getobj_str (settings, tmp); |
| 237 |
g_clear_pointer (&tmp, g_free); |
| 238 |
|
| 239 |
host = str (ref); |
| 240 |
if (!host || strlen (host) == 0) |
| 241 |
return NULL; |
| 242 |
|
| 243 |
if (strcmp (protocol, "HTTP") == 0 || strcmp (protocol, "HTTPS") == 0 || strcmp (protocol, "FTP") == 0 || strcmp (protocol, "Gopher") == 0) |
| 244 |
ret = g_string_new ("http://"); |
| 245 |
else if (strcmp (protocol, "RTSP") == 0) |
| 246 |
ret = g_string_new ("rtsp://"); |
| 247 |
else if (strcmp (protocol, "SOCKS") == 0) |
| 248 |
ret = g_string_new ("socks://"); |
| 249 |
else |
| 250 |
return NULL; |
| 251 |
|
| 252 |
g_string_append_printf (ret, "%s:%lld", host, port); |
| 253 |
|
| 254 |
return g_strdup (ret->str); |
| 255 |
} |
| 256 |
|
| 257 |
static GStrv |
| 258 |
get_ignore_list (CFDictionaryRef proxies) |
| 259 |
{ |
| 260 |
CFArrayRef ref = getobj_array (proxies, "ExceptionsList"); |
| 261 |
g_autoptr (GStrvBuilder) ret = g_strv_builder_new (); |
| 262 |
|
| 263 |
if (!ref) |
| 264 |
return g_strv_builder_end (ret); |
| 265 |
|
| 266 |
for (int idx = 0; idx < CFArrayGetCount (ref); idx++) { |
| 267 |
CFStringRef s = (CFStringRef)CFArrayGetValueAtIndex (ref, idx); |
| 268 |
|
| 269 |
px_strv_builder_add_proxy (ret, str (s)); |
| 270 |
} |
| 271 |
|
| 272 |
if (getbool (proxies, "ExcludeSimpleHostnames")) |
| 273 |
px_strv_builder_add_proxy (ret, "127.0.0.1"); |
| 274 |
|
| 275 |
return g_strv_builder_end (ret); |
| 276 |
} |
| 277 |
|
| 278 |
static void |
| 279 |
px_config_osx_get_config (PxConfig *self, |
| 280 |
GUri *uri, |
| 281 |
GStrvBuilder *builder) |
| 282 |
{ |
| 283 |
const char *proxy = NULL; |
| 284 |
CFDictionaryRef proxies = SCDynamicStoreCopyProxies (NULL); |
| 285 |
g_auto (GStrv) ignore_list = NULL; |
| 286 |
|
| 287 |
if (!proxies) { |
| 288 |
g_warning ("Unable to fetch proxy configuration"); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
ignore_list = get_ignore_list (proxies); |
| 293 |
|
| 294 |
if (px_manager_is_ignore (uri, ignore_list)) |
| 295 |
return; |
| 296 |
|
| 297 |
if (getbool (proxies, "ProxyAutoDiscoveryEnable")) { |
| 298 |
CFRelease (proxies); |
| 299 |
px_strv_builder_add_proxy (builder, "wpad://"); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
if (getbool (proxies, "ProxyAutoConfigEnable")) { |
| 304 |
CFStringRef ref = getobj_str (proxies, "ProxyAutoConfigURLString"); |
| 305 |
g_autofree char *tmp = str (ref); |
| 306 |
GUri *tmp_uri = g_uri_parse (tmp, G_URI_FLAGS_NONE, NULL); |
| 307 |
|
| 308 |
if (tmp_uri) { |
| 309 |
g_autofree char *ret = g_strdup_printf ("pac+%s", g_uri_to_string (tmp_uri)); |
| 310 |
CFRelease (proxies); |
| 311 |
px_strv_builder_add_proxy (builder, ret); |
| 312 |
return; |
| 313 |
} |
| 314 |
} else { |
| 315 |
const char *scheme = g_uri_get_scheme (uri); |
| 316 |
g_autofree char *capital_scheme = str_to_upper (scheme); |
| 317 |
|
| 318 |
proxy = protocol_url (proxies, capital_scheme); |
| 319 |
|
| 320 |
if (!proxy) |
| 321 |
proxy = protocol_url (proxies, "SOCKS"); |
| 322 |
} |
| 323 |
|
| 324 |
if (proxy) |
| 325 |
px_strv_builder_add_proxy (builder, proxy); |
| 326 |
} |
| 327 |
|
| 328 |
static void |
| 329 |
px_config_iface_init (PxConfigInterface *iface) |
| 330 |
{ |
| 331 |
iface->name = "config-osx"; |
| 332 |
iface->priority = PX_CONFIG_PRIORITY_DEFAULT; |
| 333 |
iface->get_config = px_config_osx_get_config; |
| 334 |
} |