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 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC 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 3 of the License, or (at your option) any later version.
//
// BOINC 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 BOINC. If not, see <http://www.gnu.org/licenses/>.
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
#include "boinc_win.h"
#endif
#ifdef _WIN32
#include "win_util.h"
#ifndef InternetGetCookie
#if defined(__cplusplus)
extern "C" {
#endif
BOOL WINAPI InternetGetCookieA( LPCSTR lpszUrl, LPCSTR lpszCookieName, LPSTR lpszCookieData, LPDWORD lpdwSize );
#if defined(__cplusplus)
}
#endif
#endif
#else
#include <string>
#include <vector>
#include <time.h>
#endif
#include <sqlite3.h>
#include "error_numbers.h"
#include "str_replace.h"
#include "mfile.h"
#include "miofile.h"
#include "str_util.h"
#include "filesys.h"
#include "browser.h"
//
// Utility Functions
//
// retrieve the user's application data directory.
// Win : C:\Documents and Settings\<username>\Application Data
// Unix: ~/
//
// Google Chrome decided to put its application data in the 'local' application data
// store on Windows. 'local' only has meaning for Windows.
//
void get_home_dir_path( std::string& path, bool local = false ) {
#ifdef _WIN32
CHAR szBuffer[MAX_PATH];
int iCSIDLFlags = CSIDL_FLAG_CREATE;
if (local) {
iCSIDLFlags |= CSIDL_LOCAL_APPDATA;
} else {
iCSIDLFlags |= CSIDL_APPDATA;
}
if (SUCCEEDED(SHGetFolderPathA(NULL, iCSIDLFlags, NULL, SHGFP_TYPE_CURRENT, szBuffer))) {
path = std::string(szBuffer);
path += std::string("\\");
}
#elif defined(__APPLE__)
path = std::string(getenv("HOME")) + std::string("/");
#else
path = std::string("~/");
#endif
}
// retrieve the user's cookie directory.
// WinXP
// C:\Documents and Settings\<username>\Cookies
// WinVista/Win7
// C:\Documents and Settings\<username>\Application Data\Roaming\Microsoft\Windows\Cookies
// C:\Documents and Settings\<username>\Application Data\Roaming\Microsoft\Windows\Cookies\Low
//
void get_internet_explorer_cookie_path( bool low_rights, std::string& path ) {
#ifdef _WIN32
CHAR szBuffer[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_COOKIES|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, szBuffer))) {
path = std::string(szBuffer);
path += std::string("\\");
if (low_rights) {
path += std::string("Low\\");
}
}
#endif
}
// retrieve the user's name.
//
void get_user_name( std::string& name ) {
#ifdef _WIN32
CHAR szBuffer[256];
DWORD dwSize = sizeof(szBuffer);
if (GetUserNameA((LPSTR)&szBuffer, &dwSize)) {
name = szBuffer;
}
#endif
}
// is the authenticator valid?
//
bool is_authenticator_valid(const std::string authenticator) {
std::string tmp = authenticator;
// Perform some basic validation of the suspect authenticator
//
// If the string is null then it is invalid.
if (0 == tmp.length()) {
return false;
}
// If the string contains non alpha numeric characters it is invalid.
std::string::iterator it = tmp.begin();
while (it != tmp.end()) {
if (!isalpha(*it) && !isdigit(*it)) {
return false;
}
++it;
}
return true;
}
// parse name value pairs based on INI file rules.
//
bool parse_name_value_pair(char* buf, std::string& name, std::string& value) {
std::basic_string<char>::size_type i;
std::string s;
s = std::string(buf);
i = s.find("=", 0);
if ( i < s.npos ) {
name = s.substr(0, i);
value = s.substr(i + 1);
strip_whitespace(name);
strip_whitespace(value);
return true;
}
return false;
}
// parse host name from url for Mozilla compatible browsers.
//
bool parse_hostname_mozilla_compatible(std::string& project_url, std::string& hostname) {
std::basic_string<char>::size_type start;
std::basic_string<char>::size_type end;
start = project_url.find("//", 0) + 2;
end = project_url.find("/", start);
hostname = project_url.substr(start, end - start);
if (starts_with(hostname.c_str(), "www"))
hostname.erase(0, 3);
if (!hostname.empty())
return true;
return false;
}
// parse host name from url for Chrome compatible browsers.
//
bool parse_hostname_chrome_compatible(std::string& project_url, std::string& hostname) {
std::basic_string<char>::size_type start;
std::basic_string<char>::size_type end;
start = project_url.find("//", 0) + 2;
end = project_url.find("/", start);
hostname = project_url.substr(start, end - start);
if (starts_with(hostname.c_str(), "www"))
hostname.erase(0, 3);
if (!hostname.empty())
return true;
return false;
}
// parse host name from url for Internet Explorer compatible browsers.
//
bool parse_hostname_ie_compatible(
std::string& project_url, std::string& hostname, std::string& domainname
) {
std::basic_string<char>::size_type start;
std::basic_string<char>::size_type end;
// Remove the protocol identifier
start = project_url.find("//", 0) + 2;
end = project_url.find("/", start);
hostname = project_url.substr(start, end - start);
// Remove the hostname to extract the domain name
start = 0;
end = hostname.find(".") + 1;
domainname = hostname;
domainname.erase(start, end);
if (!hostname.empty() && !domainname.empty())
return true;
return false;
}
//
// Generic Browser Support
//
class COOKIE_SQL {
public:
std::string host;
std::string name;
std::string value;
COOKIE_SQL();
void clear();
};
COOKIE_SQL::COOKIE_SQL() {
clear();
}
void COOKIE_SQL::clear() {
host.clear();
name.clear();
value.clear();
}
//
// Mozilla-Based Browser Support
//
class MOZILLA_PROFILE {
public:
std::string name;
std::string path;
bool is_relative;
bool is_default;
MOZILLA_PROFILE();
void clear();
int parse(MIOFILE& in);
};
class MOZILLA_PROFILES {
public:
std::vector<MOZILLA_PROFILE*> profiles;
MOZILLA_PROFILES();
void clear();
int parse(MIOFILE& in);
};
MOZILLA_PROFILE::MOZILLA_PROFILE() {
clear();
}
void MOZILLA_PROFILE::clear() {
name.clear();
path.clear();
is_relative = false;
is_default = false;
}
int MOZILLA_PROFILE::parse(MIOFILE& in) {
char buf[512];
std::string sn;
std::string sv;
while (in.fgets(buf, 512)) {
if (starts_with(buf, "\n")) return 0;
if (starts_with(buf, "\r\n")) return 0;
if (!parse_name_value_pair(buf, sn, sv)) continue;
if ("Name" == sn) name = sv;
if ("Path" == sn) path = sv;
if (("IsRelative" == sn) && ("1" == sv)) is_relative = true;
if (("Default" == sn) && ("1" == sv)) is_default = true;
}
return ERR_FREAD;
}
MOZILLA_PROFILES::MOZILLA_PROFILES() {
clear();
}
void MOZILLA_PROFILES::clear() {
unsigned int i;
for (i=0; i<profiles.size(); i++) {
delete profiles[i];
}
profiles.clear();
}
int MOZILLA_PROFILES::parse(MIOFILE& in) {
char buf[512];
MOZILLA_PROFILE* p = NULL;
while (in.fgets(buf, 512)) {
if (starts_with(buf, "[Profile")) {
p = new MOZILLA_PROFILE;
if (!p->parse( in )) {
profiles.push_back( p );
} else {
delete p;
}
}
}
return 0;
}
// search for the project specific cookie for mozilla based browsers.
// SELECT host, name, value, expiry from moz_cookies WHERE name = '%s' AND host LIKE '%s'
//
static int find_site_cookie_mozilla_v3(
void* cookie, int /* argc */, char **argv, char ** /* szColumnName */
) {
COOKIE_SQL* _cookie = (COOKIE_SQL*)cookie;
char host[256], cookie_name[256], cookie_value[4096];
long long expires;
safe_strcpy(host, "");
safe_strcpy(cookie_name, "");
safe_strcpy(cookie_value, "");
expires = 0;
sscanf( argv[0], "%255s", host );
sscanf( argv[1], "%255s", cookie_name );
sscanf( argv[2], "%4095s", cookie_value );
sscanf( argv[3],
#ifdef _WIN32
"%I64d",
#elif defined(__APPLE__)
"%lld",
#else
"%Ld",
#endif
&expires
);
// is this a real cookie?
// temporary cookie? these cookies do not trickle back up
// to the jscript interface, so ignore them.
if (starts_with(host, "#HttpOnly")) return 0;
// is this the right host?
if (!strstr(host, _cookie->host.c_str())) return 0;
// has the cookie expired?
if (time(0) > expires) return 0;
// is this the right cookie?
if (starts_with(cookie_name, _cookie->name)) {
_cookie->value = cookie_value;
}
return 0;
}
// traverse the profiles and determine which profile to use.
// this should be compatible with firefox2, firefox3, firefox4, seamonkey, and netscape.
//
bool get_firefox_profile_root( std::string& profile_root ) {
bool retval = false;
FILE* pf = NULL;
MIOFILE pmf;
MOZILLA_PROFILES mps;
MOZILLA_PROFILE* mp = NULL;
std::string cookies_root;
std::string tmp;
unsigned int i = 0;
unsigned int default_index = 0;
get_home_dir_path( profile_root );
#ifdef _WIN32
profile_root += std::string("Mozilla\\Firefox\\");
#elif defined(__APPLE__)
profile_root += std::string("Library/Application Support/Firefox/");
#else
profile_root += std::string(".mozilla/firefox/");
#endif
// lets see if we can open the profiles configuration file
tmp = profile_root + "profiles.ini";
pf = fopen(tmp.c_str(), "r");
// if profiles configuration file exists, parse it.
if (pf) {
pmf.init_file(pf);
mps.parse(pmf);
}
// we need to know which cookie file to look at, so if only
// one profile exists, use it.
//
// if more than one profile exists, look through all the
// profiles until we find the default profile. even when the
// user selects a different profile at startup the default
// profile flag is changed at startup to the new profile.
if (mps.profiles.size() == 0) {
if (pf) fclose(pf);
return retval; // something is very wrong, don't
// risk a crash
}
if (mps.profiles.size() == 1) {
default_index = 0;
} else {
for (i=0; i < mps.profiles.size(); i++) {
if (mps.profiles[i]->is_default) {
default_index = i;
break;
}
}
}
// should the path be treated as an absolute path or a relative
// path?
mp = mps.profiles[default_index];
if (mp->is_relative) {
cookies_root = profile_root + mp->path + "/";
} else {
cookies_root = mp->path + "/";
}
profile_root = cookies_root;
retval = true;
// cleanup
if (pf) fclose(pf);
return retval;
}
bool detect_cookie_mozilla_v3(
std::string profile_root, std::string& project_url, std::string& name, std::string& value
) {
bool retval = false;
std::string tmp;
std::string hostname;
char query[1024];
sqlite3* db;
char* lpszSQLErrorMessage = NULL;
int rc;
COOKIE_SQL cookie;
// determine the project hostname using the project url
parse_hostname_mozilla_compatible(project_url, hostname);
// now we should open up the cookie database.
tmp = profile_root + "cookies.sqlite";
rc = sqlite3_open(tmp.c_str(), &db);
if ( rc ) {
sqlite3_close(db);
return false;
}
// construct SQL query to extract the desired cookie
// SELECT host, name, value, expiry from moz_cookies WHERE name = '%s' AND host LIKE '%%%s'
sqlite3_snprintf(sizeof(query), query,
"SELECT host, name, value, expiry from moz_cookies WHERE name = '%q' AND host LIKE '%%%q'",
name.c_str(),
hostname.c_str()
);
// execute query
rc = sqlite3_exec(db, query, find_site_cookie_mozilla_v3, &cookie, &lpszSQLErrorMessage);
if ( rc != SQLITE_OK ){
sqlite3_free(lpszSQLErrorMessage);
}
// cleanup
sqlite3_close(db);
if ( !cookie.value.empty() ) {
value = cookie.value;
retval = true;
}
return retval;
}
//
// Firefox Browser Support
//
bool detect_cookie_firefox_3(
std::string& project_url, std::string& name, std::string& value
) {
std::string profile_root;
get_firefox_profile_root(profile_root);
return detect_cookie_mozilla_v3(
profile_root,
project_url,
name,
value
);
}
//
// Chrome-Based Browser Support
//
// search for the project specific cookie for chrome based browsers.
// SELECT host_key, name, value, expires_utc, httponly from cookies WHERE name = '%s' AND host_key LIKE '%s'
//
static int find_site_cookie_chrome(
void* cookie, int /* argc */, char **argv, char ** /* szColumnName */
) {
COOKIE_SQL* _cookie = (COOKIE_SQL*)cookie;
char host[256], cookie_name[256], cookie_value[4096];
long long expires;
long httponly;
safe_strcpy(host, "");
safe_strcpy(cookie_name, "");
safe_strcpy(cookie_value, "");
expires = 0;
sscanf( argv[0], "%255s", host );
sscanf( argv[1], "%255s", cookie_name );
sscanf( argv[2], "%4095s", cookie_value );
sscanf( argv[3],
#ifdef _WIN32
"%I64d",
#elif defined(__APPLE__)
"%lld",
#else
"%Ld",
#endif
&expires
);
sscanf( argv[4],
"%ld",
&httponly
);
// Convert Google Chrome time (microseconds since January 1, 1601)
// to UNIX time (seconds since January 1, 1970)
expires = (expires / 1000000) - 11644473600LL;
// is this a real cookie?
// temporary cookie? these cookies do not trickle back up
// to the jscript interface, so ignore them.
if (httponly) return 0;
// is this the right host?
if (!strstr(host, _cookie->host.c_str())) return 0;
// has the cookie expired?
if (time(0) > expires) return 0;
// is this the right cookie?
if (starts_with(cookie_name, _cookie->name)) {
_cookie->value = cookie_value;
}
return 0;
}
// traverse the profiles and determine which profile to use.
// this should be compatible with chrome.
//
bool get_chrome_profile_root( std::string& profile_root ) {
get_home_dir_path( profile_root, true );
#ifdef _WIN32
profile_root += std::string("Google\\Chrome\\User Data\\Default\\");
#elif defined(__APPLE__)
profile_root += std::string("Library/Application Support/Google/Chrome/Default/");
#else
profile_root += std::string(".google/chrome/");
#endif
return true;
}
bool detect_cookie_chrome(
std::string profile_root, std::string& project_url, std::string& name, std::string& value
) {
bool retval = false;
std::string tmp;
std::string hostname;
char query[1024];
sqlite3* db;
char* lpszSQLErrorMessage = NULL;
int rc;
COOKIE_SQL cookie;
// determine the project hostname using the project url
parse_hostname_chrome_compatible(project_url, hostname);
// now we should open up the cookie database.
tmp = profile_root + "Cookies";
rc = sqlite3_open(tmp.c_str(), &db);
if ( rc ) {
sqlite3_close(db);
tmp = profile_root + "Safe Browsing Cookies";
rc = sqlite3_open(tmp.c_str(), &db);
if ( rc ) {
sqlite3_close(db);
return false;
}
}
// construct SQL query to extract the desired cookie
// SELECT host_key, name, value, expires_utc, httponly from cookies WHERE name = '%s' AND host_key LIKE '%%%s'
sqlite3_snprintf(sizeof(query), query,
"SELECT host_key, name, value, expires_utc, httponly from cookies WHERE name = '%q' AND host_key LIKE '%%%q'",
name.c_str(),
hostname.c_str()
);
// execute query
rc = sqlite3_exec(db, query, find_site_cookie_chrome, &cookie, &lpszSQLErrorMessage);
if ( rc != SQLITE_OK ){
sqlite3_free(lpszSQLErrorMessage);
}
// cleanup
sqlite3_close(db);
if ( !cookie.value.empty() ) {
value = cookie.value;
retval = true;
}
return retval;
}
bool detect_cookie_chrome(
std::string& project_url, std::string& name, std::string& value
) {
std::string profile_root;
get_chrome_profile_root(profile_root);
return detect_cookie_chrome(
profile_root,
project_url,
name,
value
);
}
#ifdef _WIN32
//
// Internet Explorer Browser Support
//
//
// Detect a cookie in Internet Explorer by using the InternetGetCookie API.
// Supports: 3.x, 4.x, 5.x, 6.x, 7.x (UAC Turned Off), 8.x (UAC Turned Off), 9.x (UAC Turned Off), and
// 10.x (UAC Turned Off).
//
bool detect_cookie_ie_supported(std::string& project_url, std::string& name, std::string& value)
{
bool bReturnValue = false;
bool bCheckDomainName = false;
char szCookieBuffer[4096];
char* pszCookieFragment = NULL;
DWORD dwSize = sizeof(szCookieBuffer)/sizeof(char);
std::string strCookieFragment;
std::string strCookieName;
std::string strCookieValue;
std::string hostname;
std::string domainname;
size_t uiDelimeterLocation;
// if we don't find the cookie at the exact project dns name, check one higher
// (i.e. www.worldcommunitygrid.org becomes worldcommunitygrid.org
parse_hostname_ie_compatible(project_url, hostname, domainname);
// InternetGetCookie expects them in URL format
hostname = std::string("http://") + hostname + std::string("/");
domainname = std::string("http://") + domainname + std::string("/");
// First check to see if the desired cookie is assigned to the hostname.
bReturnValue = InternetGetCookieA(hostname.c_str(), NULL, szCookieBuffer, &dwSize) == TRUE;
if (!bReturnValue || (!strstr(szCookieBuffer, name.c_str()))) {
bCheckDomainName = true;
}
// Next check if it was assigned to the domainname.
if (bCheckDomainName) {
bReturnValue = InternetGetCookieA(domainname.c_str(), NULL, szCookieBuffer, &dwSize) == TRUE;
if (!bReturnValue || (!strstr(szCookieBuffer, name.c_str()))) {
return false;
}
}
// Format of cookie buffer:
// 'cookie1=value1; cookie2=value2; cookie3=value3;
//
pszCookieFragment = strtok(szCookieBuffer, "; ");
while(pszCookieFragment) {
// Convert to a std::string so we can go to town
strCookieFragment = pszCookieFragment;
// Extract the name & value
uiDelimeterLocation = strCookieFragment.find("=", 0);
strCookieName = strCookieFragment.substr(0, uiDelimeterLocation);
strCookieValue = strCookieFragment.substr(uiDelimeterLocation + 1);
if (0 == strcmp(name.c_str(), strCookieName.c_str())) {
// Now we found it! Yea - auto attach!
value = strCookieValue;
bReturnValue = true;
}
pszCookieFragment = strtok(NULL, "; ");
}
return bReturnValue;
}
//
// Detect a cookie in Internet Explorer by using the InternetGetCookie API.
// Supports: 8.x (UAC Turned On), 9.x (UAC Turned On), 10.x (UAC Turned On).
//
typedef HRESULT (__stdcall *tIEGPMC)( IN LPCWSTR, IN LPCWSTR, OUT LPWSTR, OUT DWORD*, IN DWORD );
bool detect_cookie_ie_supported_uac(std::string& project_url, std::string& name, std::string& value)
{
static HMODULE ieframelib = NULL;
static tIEGPMC pIEGPMC = NULL;
bool bReturnValue = false;
bool bCheckDomainName = false;
HRESULT rc;
WCHAR szCookieBuffer[4096];
WCHAR* pszCookieFragment = NULL;
DWORD dwSize = sizeof(szCookieBuffer)/sizeof(WCHAR);
std::wstring strCookieFragment;
std::wstring strCookieName;
std::wstring strCookieValue;
std::string hostname;
std::wstring hostname_w;
std::string domainname;
std::wstring domainname_w;
std::wstring name_w;
size_t uiDelimeterLocation;
if (!ieframelib) {
ieframelib = LoadLibraryA("ieframe.dll");
if (ieframelib) {
pIEGPMC = (tIEGPMC)GetProcAddress(ieframelib, "IEGetProtectedModeCookie");
}
}
if (!pIEGPMC) {
return false;
}
// Convert name into wide character string
name_w = boinc_ascii_to_wide(name);
// if we don't find the cookie at the exact project dns name, check one higher
// (i.e. www.worldcommunitygrid.org becomes worldcommunitygrid.org
parse_hostname_ie_compatible(project_url, hostname, domainname);
// InternetGetCookie expects them in URL format
hostname_w = std::wstring(_T("http://")) + boinc_ascii_to_wide(hostname) + std::wstring(_T("/"));
domainname_w = std::wstring(_T("http://")) + boinc_ascii_to_wide(domainname) + std::wstring(_T("/"));
// First check to see if the desired cookie is assigned to the hostname.
rc = pIEGPMC(hostname_w.c_str(), NULL, szCookieBuffer, &dwSize, NULL) == TRUE;
if (!SUCCEEDED(rc) || (!wcsstr(szCookieBuffer, name_w.c_str()))) {
bCheckDomainName = true;
}
// Next check if it was assigned to the domainname.
if (bCheckDomainName) {
rc = pIEGPMC(domainname_w.c_str(), NULL, szCookieBuffer, &dwSize, NULL) == TRUE;
if (!SUCCEEDED(rc) || (!wcsstr(szCookieBuffer, name_w.c_str()))) {
return false;
}
}
// Format of cookie buffer:
// 'cookie1=value1; cookie2=value2; cookie3=value3;
//
pszCookieFragment = wcstok(szCookieBuffer, _T("; "));
while(pszCookieFragment) {
// Convert to a std::string so we can go to town
strCookieFragment = pszCookieFragment;
// Extract the name & value
uiDelimeterLocation = strCookieFragment.find(_T("="), 0);
strCookieName = strCookieFragment.substr(0, uiDelimeterLocation);
strCookieValue = strCookieFragment.substr(uiDelimeterLocation + 1);
if (0 == wcscmp(name_w.c_str(), strCookieName.c_str())) {
// Now we found it! Yea - auto attach!
value = boinc_wide_to_ascii(strCookieValue);
bReturnValue = true;
}
pszCookieFragment = wcstok(NULL, _T("; "));
}
return bReturnValue;
}
//
// Detect a cookie in Internet Explorer.
//
bool detect_cookie_ie(std::string& project_url, std::string& name, std::string& value)
{
// Check using the supported methods first
if (detect_cookie_ie_supported( project_url, name, value )) return true;
if (detect_cookie_ie_supported_uac( project_url, name, value )) return true;
return false;
}
#endif
//
// walk through the various browsers looking up the
// project cookies until the projects 'Setup' cookie is found.
//
// give preference to the default platform specific browers first before going
// to the platform independant browsers since most people don't switch from
// the default.
//
bool detect_setup_authenticator(
std::string& project_url, std::string& authenticator
) {
bool retval = false;
std::string strCookieSetup("Setup");
#ifdef _WIN32
if (detect_cookie_ie(project_url, strCookieSetup, authenticator)) goto END;
#endif
#ifdef __APPLE__
if (detect_cookie_safari(project_url, strCookieSetup, authenticator)) goto END;
#endif
if (detect_cookie_chrome(project_url, strCookieSetup, authenticator)) goto END;
if (detect_cookie_firefox_3(project_url, strCookieSetup, authenticator)) goto END;
END:
if (is_authenticator_valid(authenticator)) {
retval = true;
}
return retval;
}
//
// walk through the various browsers looking up the
// various cookies that make up the simple account creation scheme.
//
// give preference to the default platform specific browers first before going
// to the platform independant browsers since most people don't switch from
// the default.
//
bool detect_simple_account_credentials(
std::string& project_name, std::string& project_url, std::string& authenticator,
std::string& project_institution, std::string& project_description, std::string& known
) {
bool retval = false;
std::string strCookieServer("http://boinc.berkeley.edu");
std::string strCookieProjectName("attach_project_name");
std::string strCookieProjectURL("attach_master_url");
std::string strCookieAuthenticator("attach_auth");
std::string strCookieProjectInstitution("attach_project_inst");
std::string strCookieProjectDescription("attach_project_desc");
std::string strCookieKnown("attach_known");
#ifdef _WIN32
if ( detect_cookie_ie(strCookieServer, strCookieProjectName, project_name) &&
detect_cookie_ie(strCookieServer, strCookieProjectURL, project_url)
){
detect_cookie_ie(strCookieServer, strCookieAuthenticator, authenticator);
detect_cookie_ie(strCookieServer, strCookieProjectInstitution, project_institution);
detect_cookie_ie(strCookieServer, strCookieProjectDescription, project_description);
detect_cookie_ie(strCookieServer, strCookieKnown, known);
goto END;
}
#endif
#ifdef __APPLE__
if ( detect_cookie_safari(strCookieServer, strCookieProjectName, project_name) &&
detect_cookie_safari(strCookieServer, strCookieProjectURL, project_url)
){
detect_cookie_safari(strCookieServer, strCookieAuthenticator, authenticator);
detect_cookie_safari(strCookieServer, strCookieProjectInstitution, project_institution);
detect_cookie_safari(strCookieServer, strCookieProjectDescription, project_description);
detect_cookie_safari(strCookieServer, strCookieKnown, known);
goto END;
}
#endif
if ( detect_cookie_chrome(strCookieServer, strCookieProjectName, project_name) &&
detect_cookie_chrome(strCookieServer, strCookieProjectURL, project_url)
){
detect_cookie_chrome(strCookieServer, strCookieAuthenticator, authenticator);
detect_cookie_chrome(strCookieServer, strCookieProjectInstitution, project_institution);
detect_cookie_chrome(strCookieServer, strCookieProjectDescription, project_description);
detect_cookie_chrome(strCookieServer, strCookieKnown, known);
goto END;
}
if ( detect_cookie_firefox_3(strCookieServer, strCookieProjectName, project_name) &&
detect_cookie_firefox_3(strCookieServer, strCookieProjectURL, project_url)
){
detect_cookie_firefox_3(strCookieServer, strCookieAuthenticator, authenticator);
detect_cookie_firefox_3(strCookieServer, strCookieProjectInstitution, project_institution);
detect_cookie_firefox_3(strCookieServer, strCookieProjectDescription, project_description);
detect_cookie_firefox_3(strCookieServer, strCookieKnown, known);
goto END;
}
END:
if (!project_name.empty() && !project_url.empty()) {
retval = true;
}
return retval;
}
//
// walk through the various browsers looking up the
// account manager cookies until the account manager's 'Login' and 'Password_Hash'
// cookies are found.
//
// give preference to the default platform specific browers first before going
// to the platform independant browsers since most people don't switch from
// the default.
//
bool detect_account_manager_credentials(
std::string& project_url, std::string& login, std::string& password_hash, std::string& return_url
) {
bool retval = false;
std::string strCookieLogon("Logon");
std::string strCookiePasswordHash("PasswordHash");
std::string strCookieReturnURL("ReturnURL");
#ifdef _WIN32
if ( detect_cookie_ie(project_url, strCookieLogon, login) &&
detect_cookie_ie(project_url, strCookiePasswordHash, password_hash)
){
detect_cookie_ie(project_url, strCookieReturnURL, return_url);
goto END;
}
#endif
#ifdef __APPLE__
if ( detect_cookie_safari(project_url, strCookieLogon, login) &&
detect_cookie_safari(project_url, strCookiePasswordHash, password_hash)
){
detect_cookie_safari(project_url, strCookieReturnURL, return_url);
goto END;
}
#endif
if ( detect_cookie_chrome(project_url, strCookieLogon, login) &&
detect_cookie_chrome(project_url, strCookiePasswordHash, password_hash)
){
detect_cookie_chrome(project_url, strCookieReturnURL, return_url);
goto END;
}
if ( detect_cookie_firefox_3(project_url, strCookieLogon, login) &&
detect_cookie_firefox_3(project_url, strCookiePasswordHash, password_hash)
){
detect_cookie_firefox_3(project_url, strCookieReturnURL, return_url);
goto END;
}
END:
if (!login.empty() && !password_hash.empty()) {
retval = true;
}
return retval;
}
|