static void LogInit(NS_tchar *path) { if (gLogFP) return; NS_tchar logFile[MAXPATHLEN]; NS_tsnprintf(logFile, MAXPATHLEN, NS_T("%s/update.log"), path); gLogFP = NS_tfopen(logFile, NS_T("w")); }
static void WriteMsg(const NS_tchar *path, const char *status) { FILE* outFP = NS_tfopen(path, NS_T("wb")); if (!outFP) return; fprintf(outFP, "%s\n", status); fclose(outFP); outFP = nullptr; }
void UpdateLog::Init(NS_tchar* sourcePath, const NS_tchar* fileName) { if (logFP) return; this->sourcePath = sourcePath; NS_tchar logFile[MAXPATHLEN]; NS_tsnprintf(logFile, sizeof(logFile)/sizeof(logFile[0]), NS_T("%s/%s"), sourcePath, fileName); logFP = NS_tfopen(logFile, NS_T("w")); }
int CVE_2014_1496_thunderbird3_1_20_PatchFile::Prepare() { LOG(("PREPARE PATCH %s\n", mFile)); // extract the patch to a temporary file mPatchIndex = sPatchIndex++; NS_tsnprintf(spath, sizeof(spath)/sizeof(spath[0]), NS_T("%s/%d.patch"), gSourcePath, mPatchIndex); NS_tremove(spath); FILE *fp = NS_tfopen(spath, NS_T("wb")); if (!fp) return WRITE_ERROR; int rv = gArchiveReader.ExtractFileToStream(mPatchFile, fp); fclose(fp); return rv; }
static bool CheckMsg(const NS_tchar *path, const char *expected) { if (NS_taccess(path, F_OK)) { return false; } FILE *inFP = NS_tfopen(path, NS_T("rb")); if (!inFP) { return false; } struct stat ms; if (fstat(fileno(inFP), &ms)) { return false; } char *mbuf = (char *) malloc(ms.st_size + 1); if (!mbuf) { return false; } size_t r = ms.st_size; char *rb = mbuf; size_t c = fread(rb, sizeof(char), 50, inFP); r -= c; rb += c; if (c == 0 && r) { return false; } mbuf[ms.st_size] = '\0'; rb = mbuf; fclose(inFP); inFP = nullptr; return strcmp(rb, expected) == 0; }
int NS_main(int argc, NS_tchar **argv) { if (argc < 3) { fprintf(stderr, \ "\n" \ "Application Update Service Test Helper\n" \ "\n" \ "Usage: WORKINGDIR INFILE OUTFILE -s SECONDS [FILETOLOCK]\n" \ " or: WORKINGDIR LOGFILE [ARG2 ARG3...]\n" \ " or: signature-check filepath\n" \ " or: setup-symlink dir1 dir2 file symlink\n" \ " or: remove-symlink dir1 dir2 file symlink\n" \ " or: check-symlink symlink\n" \ "\n" \ " WORKINGDIR \tThe relative path to the working directory to use.\n" \ " INFILE \tThe relative path from the working directory for the file to\n" \ " \tread actions to perform such as finish.\n" \ " OUTFILE \tThe relative path from the working directory for the file to\n" \ " \twrite status information.\n" \ " SECONDS \tThe number of seconds to sleep.\n" \ " FILETOLOCK \tThe relative path from the working directory to an existing\n" \ " \tfile to open exlusively.\n" \ " \tOnly available on Windows platforms and silently ignored on\n" \ " \tother platforms.\n" \ " LOGFILE \tThe relative path from the working directory to log the\n" \ " \tcommand line arguments.\n" \ " ARG2 ARG3...\tArguments to write to the LOGFILE after the preceding command\n" \ " \tline arguments.\n" \ "\n" \ "Note: All paths must be relative.\n" \ "\n"); return 1; } if (!NS_tstrcmp(argv[1], NS_T("check-signature"))) { #ifdef XP_WIN if (ERROR_SUCCESS == VerifyCertificateTrustForFile(argv[2])) { return 0; } else { return 1; } #else // Not implemented on non-Windows platforms return 1; #endif } if (!NS_tstrcmp(argv[1], NS_T("setup-symlink"))) { #ifdef XP_UNIX NS_tchar path[MAXPATHLEN]; NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s"), NS_T("/tmp"), argv[2]); mkdir(path, 0755); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3]); mkdir(path, 0755); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3], argv[4]); FILE * file = NS_tfopen(path, NS_T("w")); if (file) { NS_tfputs(NS_T("test"), file); fclose(file); } symlink(path, argv[5]); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s"), NS_T("/tmp"), argv[2]); if (argc > 6 && !NS_tstrcmp(argv[6], NS_T("change-perm"))) { chmod(path, 0644); } return 0; #else // Not implemented on non-Unix platforms return 1; #endif } if (!NS_tstrcmp(argv[1], NS_T("remove-symlink"))) { #ifdef XP_UNIX NS_tchar path[MAXPATHLEN]; NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s"), NS_T("/tmp"), argv[2]); chmod(path, 0755); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3], argv[4]); unlink(path); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s/%s"), NS_T("/tmp"), argv[2], argv[3]); rmdir(path); NS_tsnprintf(path, sizeof(path)/sizeof(path[0]), NS_T("%s/%s"), NS_T("/tmp"), argv[2]); rmdir(path); return 0; #else // Not implemented on non-Unix platforms return 1; #endif } if (!NS_tstrcmp(argv[1], NS_T("check-symlink"))) { #ifdef XP_UNIX struct stat ss; lstat(argv[2], &ss); return S_ISLNK(ss.st_mode) ? 0 : 1; #else // Not implemented on non-Unix platforms return 1; #endif } if (!NS_tstrcmp(argv[1], NS_T("wait-for-service-stop"))) { #ifdef XP_WIN const int maxWaitSeconds = NS_ttoi(argv[3]); LPCWSTR serviceName = argv[2]; DWORD serviceState = WaitForServiceStop(serviceName, maxWaitSeconds); if (SERVICE_STOPPED == serviceState) { return 0; } else { return serviceState; } #else // Not implemented on non-Windows platforms return 1; #endif } if (!NS_tstrcmp(argv[1], NS_T("wait-for-application-exit"))) { #ifdef XP_WIN const int maxWaitSeconds = NS_ttoi(argv[3]); LPCWSTR application = argv[2]; DWORD ret = WaitForProcessExit(application, maxWaitSeconds); if (ERROR_SUCCESS == ret) { return 0; } else if (WAIT_TIMEOUT == ret) { return 1; } else { return 2; } #else // Not implemented on non-Windows platforms return 1; #endif } int i = 0; if (NS_tchdir(argv[1]) != 0) { return 1; } // File in use test helper section if (!NS_tstrcmp(argv[4], NS_T("-s"))) { NS_tchar *cwd = NS_tgetcwd(nullptr, 0); NS_tchar inFilePath[MAXPATHLEN]; NS_tsnprintf(inFilePath, sizeof(inFilePath)/sizeof(inFilePath[0]), NS_T("%s/%s"), cwd, argv[2]); NS_tchar outFilePath[MAXPATHLEN]; NS_tsnprintf(outFilePath, sizeof(outFilePath)/sizeof(outFilePath[0]), NS_T("%s/%s"), cwd, argv[3]); int seconds = NS_ttoi(argv[5]); #ifdef XP_WIN HANDLE hFile = INVALID_HANDLE_VALUE; if (argc == 7) { hFile = CreateFileW(argv[6], DELETE | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (hFile == INVALID_HANDLE_VALUE) { WriteMsg(outFilePath, "error_locking"); return 1; } } WriteMsg(outFilePath, "sleeping"); while (!CheckMsg(inFilePath, "finish\n") && i++ <= seconds) { Sleep(1000); } if (argc == 7) { CloseHandle(hFile); } #else WriteMsg(outFilePath, "sleeping"); while (!CheckMsg(inFilePath, "finish\n") && i++ <= seconds) { sleep(1); } #endif WriteMsg(outFilePath, "finished"); return 0; } // Command line argument test helper section NS_tchar logFilePath[MAXPATHLEN]; NS_tsnprintf(logFilePath, sizeof(logFilePath)/sizeof(logFilePath[0]), NS_T("%s"), argv[2]); FILE* logFP = NS_tfopen(logFilePath, NS_T("wb")); for (i = 1; i < argc; ++i) { fprintf(logFP, LOG_S "\n", argv[i]); } fclose(logFP); logFP = nullptr; return 0; }
/** * A very basic parser for updater.ini taken mostly from nsINIParser.cpp * that can be used by standalone apps. * * @param path Path to the .ini file to read * @param keyList List of zero-delimited keys ending with two zero characters * @param numStrings Number of strings to read into results buffer - must be equal to the number of keys * @param results Two-dimensional array of strings to be filled in the same order as the keys provided * @param section Optional name of the section to read; defaults to "Strings" */ int ReadStrings(const NS_tchar *path, const char *keyList, unsigned int numStrings, char results[][MAX_TEXT_LEN], const char *section) { AutoFILE fp(NS_tfopen(path, OPEN_MODE)); if (!fp) return READ_ERROR; /* get file size */ if (fseek(fp, 0, SEEK_END) != 0) return READ_ERROR; long len = ftell(fp); if (len <= 0) return READ_ERROR; size_t flen = size_t(len); AutoCharArray fileContents(flen + 1); if (!fileContents) return READ_STRINGS_MEM_ERROR; /* read the file in one swoop */ if (fseek(fp, 0, SEEK_SET) != 0) return READ_ERROR; size_t rd = fread(fileContents, sizeof(char), flen, fp); if (rd != flen) return READ_ERROR; fileContents[flen] = '\0'; char *buffer = fileContents; bool inStringsSection = false; unsigned int read = 0; while (char *token = NS_strtok(kNL, &buffer)) { if (token[0] == '#' || token[0] == ';') // it's a comment continue; token = (char*) NS_strspnp(kWhitespace, token); if (!*token) // empty line continue; if (token[0] == '[') { // section header! ++token; char const * currSection = token; char *rb = NS_strtok(kRBracket, &token); if (!rb || NS_strtok(kWhitespace, &token)) { // there's either an unclosed [Section or a [Section]Moretext! // we could frankly decide that this INI file is malformed right // here and stop, but we won't... keep going, looking for // a well-formed [section] to continue working with inStringsSection = false; } else { if (section) inStringsSection = strcmp(currSection, section) == 0; else inStringsSection = strcmp(currSection, "Strings") == 0; } continue; } if (!inStringsSection) { // If we haven't found a section header (or we found a malformed // section header), or this isn't the [Strings] section don't bother // parsing this line. continue; } char *key = token; char *e = NS_strtok(kEquals, &token); if (!e) continue; int keyIndex = find_key(keyList, key); if (keyIndex >= 0 && (unsigned int)keyIndex < numStrings) { strncpy(results[keyIndex], token, MAX_TEXT_LEN - 1); results[keyIndex][MAX_TEXT_LEN - 1] = '\0'; read++; } } return (read == numStrings) ? OK : PARSE_ERROR; }