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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
/* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mobile Application Link.
*
* The Initial Developer of the Original Code is AvantGo, Inc.
* Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
* AvantGo, Inc. All Rights Reserved.
*
* Contributor(s):
*/
/* owner: miket <miket@avantgo.com> */
#ifdef _WIN32
#include <AGProxySearch.h>
#include <AGLocationConfig.h>
#include <windows.h>
#include <stdio.h>
#define int32 netscapeint32
#define uint32 netscapeuint32
#include <reg.h> /* mozilla */
#include <nsreg.h> /* mozilla */
#undef int32
#undef uint32
#include <wininet.h>
#include <AGBase64.h>
#include <AGUtil.h>
static const LPTSTR kIESettingsKey =
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
/* ----------------------------------------------------------------------------
*/
int findWindowsInetInternetInformation(AGLocationConfig * lc)
{
typedef HINTERNET (CALLBACK* INETOPEN)(LPCSTR lpszAgent,
DWORD dwAccessType,
LPCSTR lpszProxyName,
LPCSTR lpszProxyBypass,
DWORD dwFlags);
typedef BOOL (CALLBACK* INETQUERYOPTION)(HINTERNET hInternet,
DWORD dwOption,
LPVOID lpBuffer,
LPDWORD lpdwBufferLength);
typedef BOOL (CALLBACK* INETCLOSEHANDLE)(HINTERNET hInet);
int ret = AG_PROXY_NONE_FOUND;
HMODULE hInetLib = NULL;
HINTERNET hInet = NULL;
INETOPEN InetOpen = NULL;
INETQUERYOPTION InetQuery = NULL;
INETCLOSEHANDLE InetClose = NULL;
AGBool netLibraryOpen = FALSE;
__try {
hInetLib = LoadLibrary(TEXT("WININET.DLL"));
if (NULL == hInetLib)
__leave;
/* Load up the functions we need to find out info from Wininet. */
InetOpen = (INETOPEN) GetProcAddress(
hInetLib, TEXT("InternetOpenA"));
#ifdef UNICODE
InetQuery = (INETQUERYOPTION) GetProcAddress(
hInetLib, L"InternetQueryOptionW");
#else
InetQuery = (INETQUERYOPTION) GetProcAddress(
hInetLib, "InternetQueryOptionA");
#endif /* !UNICODE */
InetClose = (INETCLOSEHANDLE) GetProcAddress(
hInetLib, TEXT("InternetCloseHandle"));
if (NULL == InetOpen || NULL == InetQuery || NULL == InetClose)
__leave;
/* Query Wininet for information. */
hInet = InetOpen("MALCOMMON",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
if (NULL != hInet) {
const int bufSize = 1024;
char buffer[bufSize];
DWORD bufLen;
HKEY hkey = NULL;
netLibraryOpen = TRUE;
/* Find out proxy information. */
bufLen = bufSize;
if (!InetQuery(NULL, INTERNET_OPTION_PROXY, &buffer, &bufLen))
__leave;
LPINTERNET_PROXY_INFO inetProxyInfo =
(LPINTERNET_PROXY_INFO)buffer;
lc->HTTPUseProxy = FALSE;
lc->SOCKSUseProxy = FALSE;
/* Is there an autoproxy URL? */
RegOpenKeyEx(HKEY_CURRENT_USER,
kIESettingsKey,
0,
KEY_READ,
&hkey);
if (NULL != hkey) {
DWORD dwType;
LONG hr;
bufLen = bufSize;
hr = RegQueryValueEx(hkey,
"AutoConfigURL",
0,
&dwType,
(LPBYTE)buffer,
&bufLen);
if (ERROR_SUCCESS == hr) {
if (NULL != lc->autoConfigProxyURL) {
free(lc->autoConfigProxyURL);
lc->autoConfigProxyURL = NULL;
}
if (strlen(buffer) > 0)
lc->autoConfigProxyURL = strdup(buffer);
}
RegCloseKey(hkey);
}
AGArrayRemoveAll(lc->exclusionServers);
if (INTERNET_OPEN_TYPE_PROXY == inetProxyInfo->dwAccessType) {
char * cptr, * dptr;
char sptr[128];
char buf[MAX_PATH];
char proxylist[4096];
/* If there's no qualifier to tell us which kind of
proxy server this is, then we need to insert one to
make it an http proxy server. pending(miket): this
may not be right! It may be that Windows simply tries
HTTP and SOCKS if one or the other isn't specified. */
if (NULL == strstr(inetProxyInfo->lpszProxy, "="))
strcpy(proxylist, "http=");
else
strcpy(proxylist, "");
strcat(proxylist, inetProxyInfo->lpszProxy);
/* Look for an HTTP proxy */
strcpy(sptr, "http=");
cptr = strstr(proxylist, sptr);
if (NULL != cptr) {
lc->HTTPUseProxy = TRUE;
cptr += strlen(sptr);
dptr = buf;
while (*cptr != '\0' && *cptr != ' ' && *cptr != ':')
*dptr++ = *cptr++;
*dptr = '\0';
/* Get port number. */
if (*cptr == ':') {
char number[16];
cptr++;
dptr = number;
while (*cptr != '\0' && *cptr != ' ')
*dptr++ = *cptr++;
*dptr = '\0';
lc->HTTPPort = atoi(number);
}
if (lc->HTTPUseProxy && lc->HTTPPort <= 0)
lc->HTTPPort = 8080;
if (NULL != lc->HTTPName)
free(lc->HTTPName);
lc->HTTPName = strdup(buf);
}
/* Look for a SOCKS proxy */
strcpy(sptr, "socks=");
cptr = strstr(proxylist, sptr);
if (NULL != cptr) {
lc->SOCKSUseProxy = TRUE;
cptr += strlen(sptr);
dptr = buf;
while (*cptr != '\0' && *cptr != ' ' && *cptr != ':')
*dptr++ = *cptr++;
*dptr = '\0';
/* Get port number. */
if (*cptr == ':') {
char number[16];
cptr++;
dptr = number;
while (*cptr != '\0' && *cptr != ' ')
*dptr++ = *cptr++;
*dptr = '\0';
lc->SOCKSPort = atoi(number);
}
if (lc->SOCKSUseProxy && lc->SOCKSPort <= 0)
lc->SOCKSPort = 1080;
if (NULL != lc->SOCKSName)
free(lc->SOCKSName);
lc->SOCKSName = strdup(buf);
}
if (lc->SOCKSUseProxy || lc->HTTPUseProxy) {
lc->exclusionServers = AGFillExclusionArray((char*)
inetProxyInfo->lpszProxyBypass);
/* pending(miket): handle <local> */
}
}
ret = AG_PROXY_NO_ERROR;
}
}
__finally {
if (NULL != hInet && NULL != InetClose && netLibraryOpen) {
InetClose(hInet);
netLibraryOpen = FALSE;
}
if (NULL != hInetLib)
::FreeLibrary(hInetLib);
}
return ret;
}
/* ----------------------------------------------------------------------------
*/
void getTokenWithoutQuotes(FILE * f, char * buf)
{
fscanf(f, "%[^\"]", buf);
fgetc(f);
fscanf(f, "%[^\"]", buf);
}
/* ----------------------------------------------------------------------------
*/
static void findNetscapeUserPrefFilename(char * buffer, uint32 buflen)
{
AGBool success = FALSE;
HREG hReg = NULL;
__try {
if (REGERR_OK != NR_RegOpen(NULL, &hReg))
__leave;
RKEY rKey;
char buf[MAX_PATH];
if (REGERR_OK != NR_RegGetKey(hReg, ROOTKEY_COMMON,
"Netscape", &rKey))
__leave;
if (REGERR_OK != NR_RegGetKey(hReg, rKey,
"ProfileManager", &rKey))
__leave;
/* Get name of last user who used Netscape. */
if (REGERR_OK != NR_RegGetEntryString(hReg, rKey,
"LastNetscapeUser", buf, MAX_PATH))
__leave;
/* Get profile location for this user. */
if (REGERR_OK != NR_RegGetKey(hReg,
ROOTKEY_USERS,
buf,
&rKey))
__leave;
if (REGERR_OK != NR_RegGetEntryString(hReg, rKey,
"ProfileLocation", buffer, buflen))
__leave;
success = TRUE;
}
__finally {
if (NULL != hReg)
NR_RegClose(hReg);
}
/* Reading 4.5 registry failed. Try looking in known registry location
for older browsers. */
if (!success) {
HKEY hKey = NULL;
HKEY hSubKey = NULL;
__try {
char buf[MAX_PATH];
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Netscape\\Netscape Navigator\\Users",
0,
KEY_READ,
&hKey))
__leave;
DWORD size = MAX_PATH;
if (ERROR_SUCCESS != RegQueryValueEx(hKey,
"CurrentUser",
0, NULL, (LPBYTE)buf, &size))
__leave;
if (ERROR_SUCCESS != RegOpenKeyEx(hKey,
buf,
0,
KEY_READ,
&hSubKey))
__leave;
size = buflen;
if (ERROR_SUCCESS != RegQueryValueEx(hSubKey,
"DirRoot",
0, NULL, (LPBYTE)buffer, &size))
__leave;
success = TRUE;
}
__finally {
if (NULL != hKey)
RegCloseKey(hKey);
if (NULL != hSubKey)
RegCloseKey(hSubKey);
}
}
if (!success)
strcpy(buffer, "");
}
/* ----------------------------------------------------------------------------
*/
static int findNetscapeInternetInformation(AGLocationConfig * lc)
{
int ret = AG_PROXY_NONE_FOUND;
char buf[MAX_PATH];
/* Open up Netscape's proprietary registry and query it. */
findNetscapeUserPrefFilename(buf, MAX_PATH);
if (strlen(buf) == 0)
return ret;
/* At this point, we know the location of the prefs file for the user
who last used Netscape. That's a reasonable guess for picking the
right prefs file to search for proxy information. */
strcat(buf, "\\prefs.js");
FILE * f = fopen(buf, "r");
if (NULL == f)
return ret;
int c = 0;
int httpPort = -1;
int socksPort = -1;
int proxyType = 0;
AGArrayRemoveAll(lc->exclusionServers);
do {
c = fscanf(f, "%s", &buf);
if (!strcmp(buf,
"user_pref(\"network.proxy.type\",")) {
fscanf(f, "%d", &proxyType);
continue;
}
if (!strcmp(buf, "user_pref(\"network.hosts.socks_server\",")) {
getTokenWithoutQuotes(f, buf);
if (NULL != lc->SOCKSName)
free(lc->SOCKSName);
lc->SOCKSName = strdup(buf);
if (socksPort < 0)
socksPort = 1080;
lc->SOCKSUseProxy = TRUE;
continue;
}
if (!strcmp(buf, "user_pref(\"network.proxy.http\",")) {
getTokenWithoutQuotes(f, buf);
if (NULL != lc->HTTPName)
free(lc->HTTPName);
lc->HTTPName = strdup(buf);
if (httpPort < 0)
httpPort = 80;
lc->HTTPUseProxy = TRUE;
continue;
}
if (!strcmp(buf, "user_pref(\"network.proxy.autoconfig_url\",")) {
getTokenWithoutQuotes(f, buf);
if (NULL != lc->autoConfigProxyURL)
free(lc->autoConfigProxyURL);
lc->autoConfigProxyURL = strdup(buf);
continue;
}
if (!strcmp(buf, "user_pref(\"network.proxy.no_proxies_on\",")) {
getTokenWithoutQuotes(f, buf);
lc->exclusionServers = AGFillExclusionArray(buf);
continue;
}
if (!strcmp(buf, "user_pref(\"network.hosts.socks_serverport\",")) {
fscanf(f, "%d", &socksPort);
continue;
}
if (!strcmp(buf, "user_pref(\"network.proxy.http_port\",")) {
fscanf(f, "%d", &httpPort);
continue;
}
} while (EOF != c);
fclose(f);
/* At this point, we have successfully parsed the Netscape
user preferences. */
ret = AG_PROXY_NO_ERROR;
if (socksPort >= 0)
lc->SOCKSPort = socksPort;
if (httpPort >= 0)
lc->HTTPPort = httpPort;
/* Note: Netscape apparently doesn't have a way to specify that
all local addresses should bypass the proxy, so we just set it false
in all cases here. */
lc->bypassLocal = FALSE;
/* Proxy type zero (which isn't ever put in the source file as far
as I can tell) means don't use proxy servers.
Proxy type 1 is either socks or HTTP. */
if (proxyType != 1) {
lc->HTTPUseProxy = FALSE;
lc->SOCKSUseProxy = FALSE;
}
/* Proxy type 2 means Netscape's automatic proxy configuration. */
if (proxyType != 2)
CHECKANDFREE(lc->autoConfigProxyURL);
if (!(lc->HTTPUseProxy || lc->SOCKSUseProxy))
AGArrayRemoveAll(lc->exclusionServers);
return ret;
}
/* ----------------------------------------------------------------------------
*/
int32 AGSearchBrowserSettings(AGLocationConfig * lc, AGBool searchAlternate)
{
int ret = AG_PROXY_NONE_FOUND;
HKEY hKey = NULL;
__try {
if (NULL == lc) {
ret = AG_PROXY_NULL_POINTER;
__leave;
}
char buf[MAX_PATH];
/* This registry key looks for whoever's registered to handle http
documents. In English, this is the default browser. */
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT,
"http\\shell\\open\\ddeexec\\Application", 0, KEY_READ, &hKey))
__leave;
DWORD size = MAX_PATH;
if (ERROR_SUCCESS != RegQueryValueEx(hKey,
NULL, 0, NULL, (LPBYTE)buf, &size))
__leave;
int preferredBrowser = -1;
/* See which browser is the default. */
if (!strcmp(buf, "NSShell"))
preferredBrowser = AG_PROXY_Netscape;
else
preferredBrowser = AG_PROXY_MSIE;
if (searchAlternate)
preferredBrowser = (AG_PROXY_MSIE == preferredBrowser)
? AG_PROXY_Netscape
: AG_PROXY_MSIE;
enum {
AG_TRY_PREFERRED,
AG_TRY_OTHER,
AG_FOUND,
AG_NONE
};
/* In the following state machine, we cycle until we've (a) found
the proxy information, or (b) failed with every browser we know
about. We try in order of preference. */
int state = AG_TRY_PREFERRED;
while (1) {
switch (state) {
case AG_TRY_PREFERRED: {
if (AG_PROXY_MSIE == preferredBrowser) {
lc->source = AG_PROXY_MSIE;
ret = findWindowsInetInternetInformation(lc);
}
else {
lc->source = AG_PROXY_Netscape;
ret = findNetscapeInternetInformation(lc);
}
if (AG_PROXY_NO_ERROR == ret)
{
lc->source =
(AG_PROXY_MSIE == preferredBrowser) ?
AG_PROXY_MSIE : AG_PROXY_Netscape;
state = AG_FOUND;
}
else
state = AG_TRY_OTHER;
}
break;
case AG_TRY_OTHER: {
if (AG_PROXY_MSIE == preferredBrowser) {
lc->source = AG_PROXY_Netscape;
ret = findNetscapeInternetInformation(lc);
}
else {
lc->source = AG_PROXY_MSIE;
ret = findWindowsInetInternetInformation(lc);
}
if (AG_PROXY_NO_ERROR == ret)
state = AG_FOUND;
else
state = AG_NONE;
}
break;
case AG_FOUND:
__leave;
break;
case AG_NONE:
lc->source = AG_PROXY_FAILED;
__leave;
break;
}
}
}
__finally {
if (NULL != hKey)
RegCloseKey(hKey);
return ret;
}
}
#endif
|