Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 3 | */ |
michael@0 | 4 | |
michael@0 | 5 | #ifdef XP_WIN |
michael@0 | 6 | # include <windows.h> |
michael@0 | 7 | # include <wintrust.h> |
michael@0 | 8 | # include <tlhelp32.h> |
michael@0 | 9 | # include <softpub.h> |
michael@0 | 10 | # include <direct.h> |
michael@0 | 11 | # include <io.h> |
michael@0 | 12 | typedef WCHAR NS_tchar; |
michael@0 | 13 | # define NS_main wmain |
michael@0 | 14 | # define F_OK 00 |
michael@0 | 15 | # define W_OK 02 |
michael@0 | 16 | # define R_OK 04 |
michael@0 | 17 | # define stat _stat |
michael@0 | 18 | # define NS_T(str) L ## str |
michael@0 | 19 | # define NS_tsnprintf(dest, count, fmt, ...) \ |
michael@0 | 20 | { \ |
michael@0 | 21 | int _count = count - 1; \ |
michael@0 | 22 | _snwprintf(dest, _count, fmt, ##__VA_ARGS__); \ |
michael@0 | 23 | dest[_count] = L'\0'; \ |
michael@0 | 24 | } |
michael@0 | 25 | # define NS_taccess _waccess |
michael@0 | 26 | # define NS_tchdir _wchdir |
michael@0 | 27 | # define NS_tfopen _wfopen |
michael@0 | 28 | # define NS_tstrcmp wcscmp |
michael@0 | 29 | # define NS_ttoi _wtoi |
michael@0 | 30 | # define NS_tstat _wstat |
michael@0 | 31 | # define NS_tgetcwd _wgetcwd |
michael@0 | 32 | # define LOG_S "%S" |
michael@0 | 33 | |
michael@0 | 34 | #include "../common/updatehelper.h" |
michael@0 | 35 | |
michael@0 | 36 | #else |
michael@0 | 37 | # include <unistd.h> |
michael@0 | 38 | # define NS_main main |
michael@0 | 39 | typedef char NS_tchar; |
michael@0 | 40 | # define NS_T(str) str |
michael@0 | 41 | # define NS_tsnprintf snprintf |
michael@0 | 42 | # define NS_taccess access |
michael@0 | 43 | # define NS_tchdir chdir |
michael@0 | 44 | # define NS_tfopen fopen |
michael@0 | 45 | # define NS_tstrcmp strcmp |
michael@0 | 46 | # define NS_ttoi atoi |
michael@0 | 47 | # define NS_tstat stat |
michael@0 | 48 | # define NS_tgetcwd getcwd |
michael@0 | 49 | # define NS_tfputs fputs |
michael@0 | 50 | # define LOG_S "%s" |
michael@0 | 51 | #endif |
michael@0 | 52 | |
michael@0 | 53 | #include "mozilla/NullPtr.h" |
michael@0 | 54 | #include <stdlib.h> |
michael@0 | 55 | #include <stdio.h> |
michael@0 | 56 | #include <string.h> |
michael@0 | 57 | #include <sys/types.h> |
michael@0 | 58 | #include <sys/stat.h> |
michael@0 | 59 | |
michael@0 | 60 | #ifndef MAXPATHLEN |
michael@0 | 61 | # ifdef PATH_MAX |
michael@0 | 62 | # define MAXPATHLEN PATH_MAX |
michael@0 | 63 | # elif defined(MAX_PATH) |
michael@0 | 64 | # define MAXPATHLEN MAX_PATH |
michael@0 | 65 | # elif defined(_MAX_PATH) |
michael@0 | 66 | # define MAXPATHLEN _MAX_PATH |
michael@0 | 67 | # elif defined(CCHMAXPATH) |
michael@0 | 68 | # define MAXPATHLEN CCHMAXPATH |
michael@0 | 69 | # else |
michael@0 | 70 | # define MAXPATHLEN 1024 |
michael@0 | 71 | # endif |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | static void |
michael@0 | 75 | WriteMsg(const NS_tchar *path, const char *status) |
michael@0 | 76 | { |
michael@0 | 77 | FILE* outFP = NS_tfopen(path, NS_T("wb")); |
michael@0 | 78 | if (!outFP) |
michael@0 | 79 | return; |
michael@0 | 80 | |
michael@0 | 81 | fprintf(outFP, "%s\n", status); |
michael@0 | 82 | fclose(outFP); |
michael@0 | 83 | outFP = nullptr; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | static bool |
michael@0 | 87 | CheckMsg(const NS_tchar *path, const char *expected) |
michael@0 | 88 | { |
michael@0 | 89 | if (NS_taccess(path, F_OK)) { |
michael@0 | 90 | return false; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | FILE *inFP = NS_tfopen(path, NS_T("rb")); |
michael@0 | 94 | if (!inFP) { |
michael@0 | 95 | return false; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | struct stat ms; |
michael@0 | 99 | if (fstat(fileno(inFP), &ms)) { |
michael@0 | 100 | return false; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | char *mbuf = (char *) malloc(ms.st_size + 1); |
michael@0 | 104 | if (!mbuf) { |
michael@0 | 105 | return false; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | size_t r = ms.st_size; |
michael@0 | 109 | char *rb = mbuf; |
michael@0 | 110 | size_t c = fread(rb, sizeof(char), 50, inFP); |
michael@0 | 111 | r -= c; |
michael@0 | 112 | rb += c; |
michael@0 | 113 | if (c == 0 && r) { |
michael@0 | 114 | return false; |
michael@0 | 115 | } |
michael@0 | 116 | mbuf[ms.st_size] = '\0'; |
michael@0 | 117 | rb = mbuf; |
michael@0 | 118 | |
michael@0 | 119 | fclose(inFP); |
michael@0 | 120 | inFP = nullptr; |
michael@0 | 121 | return strcmp(rb, expected) == 0; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | #ifdef XP_WIN |
michael@0 | 125 | /** |
michael@0 | 126 | * Verifies the trust of the specified file path. |
michael@0 | 127 | * |
michael@0 | 128 | * @param filePath The file path to check. |
michael@0 | 129 | * @return ERROR_SUCCESS if successful, or the last error code otherwise. |
michael@0 | 130 | */ |
michael@0 | 131 | DWORD |
michael@0 | 132 | VerifyCertificateTrustForFile(LPCWSTR filePath) |
michael@0 | 133 | { |
michael@0 | 134 | // Setup the file to check. |
michael@0 | 135 | WINTRUST_FILE_INFO fileToCheck; |
michael@0 | 136 | ZeroMemory(&fileToCheck, sizeof(fileToCheck)); |
michael@0 | 137 | fileToCheck.cbStruct = sizeof(WINTRUST_FILE_INFO); |
michael@0 | 138 | fileToCheck.pcwszFilePath = filePath; |
michael@0 | 139 | |
michael@0 | 140 | // Setup what to check, we want to check it is signed and trusted. |
michael@0 | 141 | WINTRUST_DATA trustData; |
michael@0 | 142 | ZeroMemory(&trustData, sizeof(trustData)); |
michael@0 | 143 | trustData.cbStruct = sizeof(trustData); |
michael@0 | 144 | trustData.pPolicyCallbackData = nullptr; |
michael@0 | 145 | trustData.pSIPClientData = nullptr; |
michael@0 | 146 | trustData.dwUIChoice = WTD_UI_NONE; |
michael@0 | 147 | trustData.fdwRevocationChecks = WTD_REVOKE_NONE; |
michael@0 | 148 | trustData.dwUnionChoice = WTD_CHOICE_FILE; |
michael@0 | 149 | trustData.dwStateAction = 0; |
michael@0 | 150 | trustData.hWVTStateData = nullptr; |
michael@0 | 151 | trustData.pwszURLReference = nullptr; |
michael@0 | 152 | // no UI |
michael@0 | 153 | trustData.dwUIContext = 0; |
michael@0 | 154 | trustData.pFile = &fileToCheck; |
michael@0 | 155 | |
michael@0 | 156 | GUID policyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2; |
michael@0 | 157 | // Check if the file is signed by something that is trusted. |
michael@0 | 158 | return WinVerifyTrust(nullptr, &policyGUID, &trustData); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | #endif |
michael@0 | 162 | |
michael@0 | 163 | int NS_main(int argc, NS_tchar **argv) |
michael@0 | 164 | { |
michael@0 | 165 | |
michael@0 | 166 | if (argc < 3) { |
michael@0 | 167 | fprintf(stderr, \ |
michael@0 | 168 | "\n" \ |
michael@0 | 169 | "Application Update Service Test Helper\n" \ |
michael@0 | 170 | "\n" \ |
michael@0 | 171 | "Usage: WORKINGDIR INFILE OUTFILE -s SECONDS [FILETOLOCK]\n" \ |
michael@0 | 172 | " or: WORKINGDIR LOGFILE [ARG2 ARG3...]\n" \ |
michael@0 | 173 | " or: signature-check filepath\n" \ |
michael@0 | 174 | " or: setup-symlink dir1 dir2 file symlink\n" \ |
michael@0 | 175 | " or: remove-symlink dir1 dir2 file symlink\n" \ |
michael@0 | 176 | " or: check-symlink symlink\n" \ |
michael@0 | 177 | "\n" \ |
michael@0 | 178 | " WORKINGDIR \tThe relative path to the working directory to use.\n" \ |
michael@0 | 179 | " INFILE \tThe relative path from the working directory for the file to\n" \ |
michael@0 | 180 | " \tread actions to perform such as finish.\n" \ |
michael@0 | 181 | " OUTFILE \tThe relative path from the working directory for the file to\n" \ |
michael@0 | 182 | " \twrite status information.\n" \ |
michael@0 | 183 | " SECONDS \tThe number of seconds to sleep.\n" \ |
michael@0 | 184 | " FILETOLOCK \tThe relative path from the working directory to an existing\n" \ |
michael@0 | 185 | " \tfile to open exlusively.\n" \ |
michael@0 | 186 | " \tOnly available on Windows platforms and silently ignored on\n" \ |
michael@0 | 187 | " \tother platforms.\n" \ |
michael@0 | 188 | " LOGFILE \tThe relative path from the working directory to log the\n" \ |
michael@0 | 189 | " \tcommand line arguments.\n" \ |
michael@0 | 190 | " ARG2 ARG3...\tArguments to write to the LOGFILE after the preceding command\n" \ |
michael@0 | 191 | " \tline arguments.\n" \ |
michael@0 | 192 | "\n" \ |
michael@0 | 193 | "Note: All paths must be relative.\n" \ |
michael@0 | 194 | "\n"); |
michael@0 | 195 | return 1; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | if (!NS_tstrcmp(argv[1], NS_T("check-signature"))) { |
michael@0 | 199 | #ifdef XP_WIN |
michael@0 | 200 | if (ERROR_SUCCESS == VerifyCertificateTrustForFile(argv[2])) { |
michael@0 | 201 | return 0; |
michael@0 | 202 | } else { |
michael@0 | 203 | return 1; |
michael@0 | 204 | } |
michael@0 | 205 | #else |
michael@0 | 206 | // Not implemented on non-Windows platforms |
michael@0 | 207 | return 1; |
michael@0 | 208 | #endif |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | if (!NS_tstrcmp(argv[1], NS_T("setup-symlink"))) { |
michael@0 | 212 | #ifdef XP_UNIX |
michael@0 | 213 | NS_tchar path[MAXPATHLEN]; |
michael@0 | 214 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 215 | NS_T("%s/%s"), NS_T("/tmp"), argv[2]); |
michael@0 | 216 | mkdir(path, 0755); |
michael@0 | 217 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 218 | NS_T("%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3]); |
michael@0 | 219 | mkdir(path, 0755); |
michael@0 | 220 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 221 | NS_T("%s/%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3], argv[4]); |
michael@0 | 222 | FILE * file = NS_tfopen(path, NS_T("w")); |
michael@0 | 223 | if (file) { |
michael@0 | 224 | NS_tfputs(NS_T("test"), file); |
michael@0 | 225 | fclose(file); |
michael@0 | 226 | } |
michael@0 | 227 | symlink(path, argv[5]); |
michael@0 | 228 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 229 | NS_T("%s/%s"), NS_T("/tmp"), argv[2]); |
michael@0 | 230 | if (argc > 6 && !NS_tstrcmp(argv[6], NS_T("change-perm"))) { |
michael@0 | 231 | chmod(path, 0644); |
michael@0 | 232 | } |
michael@0 | 233 | return 0; |
michael@0 | 234 | #else |
michael@0 | 235 | // Not implemented on non-Unix platforms |
michael@0 | 236 | return 1; |
michael@0 | 237 | #endif |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | if (!NS_tstrcmp(argv[1], NS_T("remove-symlink"))) { |
michael@0 | 241 | #ifdef XP_UNIX |
michael@0 | 242 | NS_tchar path[MAXPATHLEN]; |
michael@0 | 243 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 244 | NS_T("%s/%s"), NS_T("/tmp"), argv[2]); |
michael@0 | 245 | chmod(path, 0755); |
michael@0 | 246 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 247 | NS_T("%s/%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3], argv[4]); |
michael@0 | 248 | unlink(path); |
michael@0 | 249 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 250 | NS_T("%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3]); |
michael@0 | 251 | rmdir(path); |
michael@0 | 252 | NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), |
michael@0 | 253 | NS_T("%s/%s"), NS_T("/tmp"), argv[2]); |
michael@0 | 254 | rmdir(path); |
michael@0 | 255 | return 0; |
michael@0 | 256 | #else |
michael@0 | 257 | // Not implemented on non-Unix platforms |
michael@0 | 258 | return 1; |
michael@0 | 259 | #endif |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | if (!NS_tstrcmp(argv[1], NS_T("check-symlink"))) { |
michael@0 | 263 | #ifdef XP_UNIX |
michael@0 | 264 | struct stat ss; |
michael@0 | 265 | lstat(argv[2], &ss); |
michael@0 | 266 | return S_ISLNK(ss.st_mode) ? 0 : 1; |
michael@0 | 267 | #else |
michael@0 | 268 | // Not implemented on non-Unix platforms |
michael@0 | 269 | return 1; |
michael@0 | 270 | #endif |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | if (!NS_tstrcmp(argv[1], NS_T("wait-for-service-stop"))) { |
michael@0 | 274 | #ifdef XP_WIN |
michael@0 | 275 | const int maxWaitSeconds = NS_ttoi(argv[3]); |
michael@0 | 276 | LPCWSTR serviceName = argv[2]; |
michael@0 | 277 | DWORD serviceState = WaitForServiceStop(serviceName, maxWaitSeconds); |
michael@0 | 278 | if (SERVICE_STOPPED == serviceState) { |
michael@0 | 279 | return 0; |
michael@0 | 280 | } else { |
michael@0 | 281 | return serviceState; |
michael@0 | 282 | } |
michael@0 | 283 | #else |
michael@0 | 284 | // Not implemented on non-Windows platforms |
michael@0 | 285 | return 1; |
michael@0 | 286 | #endif |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | if (!NS_tstrcmp(argv[1], NS_T("wait-for-application-exit"))) { |
michael@0 | 290 | #ifdef XP_WIN |
michael@0 | 291 | const int maxWaitSeconds = NS_ttoi(argv[3]); |
michael@0 | 292 | LPCWSTR application = argv[2]; |
michael@0 | 293 | DWORD ret = WaitForProcessExit(application, maxWaitSeconds); |
michael@0 | 294 | if (ERROR_SUCCESS == ret) { |
michael@0 | 295 | return 0; |
michael@0 | 296 | } else if (WAIT_TIMEOUT == ret) { |
michael@0 | 297 | return 1; |
michael@0 | 298 | } else { |
michael@0 | 299 | return 2; |
michael@0 | 300 | } |
michael@0 | 301 | #else |
michael@0 | 302 | // Not implemented on non-Windows platforms |
michael@0 | 303 | return 1; |
michael@0 | 304 | #endif |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | int i = 0; |
michael@0 | 308 | |
michael@0 | 309 | if (NS_tchdir(argv[1]) != 0) { |
michael@0 | 310 | return 1; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // File in use test helper section |
michael@0 | 314 | if (!NS_tstrcmp(argv[4], NS_T("-s"))) { |
michael@0 | 315 | NS_tchar *cwd = NS_tgetcwd(nullptr, 0); |
michael@0 | 316 | NS_tchar inFilePath[MAXPATHLEN]; |
michael@0 | 317 | NS_tsnprintf(inFilePath, sizeof(inFilePath)/sizeof(inFilePath[0]), |
michael@0 | 318 | NS_T("%s/%s"), cwd, argv[2]); |
michael@0 | 319 | NS_tchar outFilePath[MAXPATHLEN]; |
michael@0 | 320 | NS_tsnprintf(outFilePath, sizeof(outFilePath)/sizeof(outFilePath[0]), |
michael@0 | 321 | NS_T("%s/%s"), cwd, argv[3]); |
michael@0 | 322 | |
michael@0 | 323 | int seconds = NS_ttoi(argv[5]); |
michael@0 | 324 | #ifdef XP_WIN |
michael@0 | 325 | HANDLE hFile = INVALID_HANDLE_VALUE; |
michael@0 | 326 | if (argc == 7) { |
michael@0 | 327 | hFile = CreateFileW(argv[6], |
michael@0 | 328 | DELETE | GENERIC_WRITE, 0, |
michael@0 | 329 | nullptr, OPEN_EXISTING, 0, nullptr); |
michael@0 | 330 | if (hFile == INVALID_HANDLE_VALUE) { |
michael@0 | 331 | WriteMsg(outFilePath, "error_locking"); |
michael@0 | 332 | return 1; |
michael@0 | 333 | } |
michael@0 | 334 | } |
michael@0 | 335 | |
michael@0 | 336 | WriteMsg(outFilePath, "sleeping"); |
michael@0 | 337 | while (!CheckMsg(inFilePath, "finish\n") && i++ <= seconds) { |
michael@0 | 338 | Sleep(1000); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | if (argc == 7) { |
michael@0 | 342 | CloseHandle(hFile); |
michael@0 | 343 | } |
michael@0 | 344 | #else |
michael@0 | 345 | WriteMsg(outFilePath, "sleeping"); |
michael@0 | 346 | while (!CheckMsg(inFilePath, "finish\n") && i++ <= seconds) { |
michael@0 | 347 | sleep(1); |
michael@0 | 348 | } |
michael@0 | 349 | #endif |
michael@0 | 350 | WriteMsg(outFilePath, "finished"); |
michael@0 | 351 | return 0; |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | // Command line argument test helper section |
michael@0 | 355 | NS_tchar logFilePath[MAXPATHLEN]; |
michael@0 | 356 | NS_tsnprintf(logFilePath, sizeof(logFilePath)/sizeof(logFilePath[0]), |
michael@0 | 357 | NS_T("%s"), argv[2]); |
michael@0 | 358 | |
michael@0 | 359 | FILE* logFP = NS_tfopen(logFilePath, NS_T("wb")); |
michael@0 | 360 | for (i = 1; i < argc; ++i) { |
michael@0 | 361 | fprintf(logFP, LOG_S "\n", argv[i]); |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | fclose(logFP); |
michael@0 | 365 | logFP = nullptr; |
michael@0 | 366 | |
michael@0 | 367 | return 0; |
michael@0 | 368 | } |