void TFile::InternalCreateEmpty() { if(IsOpen()) INITIATE_FAILURE; if(!m_FilePos.m_bPlainFile) INITIATE_FAILURE; if(!CreateEmptyFile(m_FilePos.m_FileName)) INITIATE_FAILURE; }
void MonitorInitialize(void) { int i, j, k; char vbuff[CF_BUFSIZE]; sprintf(vbuff, "%s/state/cf_users", CFWORKDIR); MapName(vbuff); CreateEmptyFile(vbuff); snprintf(ENVFILE_NEW, CF_BUFSIZE, "%s/state/%s", CFWORKDIR, CF_ENVNEW_FILE); MapName(ENVFILE_NEW); snprintf(ENVFILE, CF_BUFSIZE, "%s/state/%s", CFWORKDIR, CF_ENV_FILE); MapName(ENVFILE); MonEntropyClassesInit(); GetDatabaseAge(); for (i = 0; i < CF_OBSERVABLES; i++) { LOCALAV.Q[i] = QDefinite(0.0); } for (i = 0; i < 7; i++) { for (j = 0; j < CF_OBSERVABLES; j++) { for (k = 0; k < CF_GRAINS; k++) { HISTOGRAM[i][j][k] = 0; } } } for (i = 0; i < CF_OBSERVABLES; i++) { CHI[i] = 0; CHI_LIMIT[i] = 0.1; LDT_AVG[i] = 0; LDT_SUM[i] = 0; } srand((unsigned int) time(NULL)); LoadHistogram(); /* Look for local sensors - this is unfortunately linux-centric */ MonNetworkInit(); MonTempInit(); MonOtherInit(); Log(LOG_LEVEL_DEBUG, "Finished with monitor initialization"); }
void MonNetworkInit(void) { DeleteItemList(MON_TCP4); DeleteItemList(MON_TCP6); MON_TCP4 = MON_TCP6 = NULL; for (int i = 0; i < ATTR; i++) { char vbuff[CF_BUFSIZE]; sprintf(vbuff, "%s/state/cf_incoming.%s", CFWORKDIR, ECGSOCKS[i].name); MapName(vbuff); CreateEmptyFile(vbuff); sprintf(vbuff, "%s/state/cf_outgoing.%s", CFWORKDIR, ECGSOCKS[i].name); MapName(vbuff); CreateEmptyFile(vbuff); } }
void MonitorInitialize(void) { int i, j, k; char vbuff[CF_BUFSIZE]; sprintf(vbuff, "%s/state/cf_users", CFWORKDIR); MapName(vbuff); CreateEmptyFile(vbuff); MonEntropyClassesInit(); GetDatabaseAge(); for (i = 0; i < CF_OBSERVABLES; i++) { LOCALAV.Q[i].expect = 0.0; LOCALAV.Q[i].var = 0.0; LOCALAV.Q[i].q = 0.0; LOCALAV.Q[i].dq = 0.0; } for (i = 0; i < 7; i++) { for (j = 0; j < CF_OBSERVABLES; j++) { for (k = 0; k < CF_GRAINS; k++) { HISTOGRAM[i][j][k] = 0; } } } for (i = 0; i < CF_OBSERVABLES; i++) { CHI[i] = 0; CHI_LIMIT[i] = 0.1; LDT_AVG[i] = 0; LDT_SUM[i] = 0; } srand((unsigned int) time(NULL)); LoadHistogram(); /* Look for local sensors - this is unfortunately linux-centric */ MonNetworkInit(); MonTempInit(); MonOtherInit(); CfDebug("Finished with initialization.\n"); }
int TestFileIO() { wchar_t* TestPath = L"C:\\tmp\\"; wchar_t* TestFile = L"text.txt"; wchar_t* TestFilename = L"C:\\tmp\\text.txt"; wchar_t* TestData = L"Hello World!"; int result = 0; wprintf(L"[*] Checking if file %s exists...", TestFile); if (FileExists(TestFilename)) { wprintf(L"[FOUND]\n"); wprintf(L"[!] Deleting %s.\n", TestFilename); DeleteFile(TestFilename); } else { wprintf(L"[NOT FOUND]\n"); } wprintf(L"[*] Creating file %s...\n", TestFile); result = CreateEmptyFile(TestFilename, wcslen(TestFilename)); if (result == 0) { wprintf(L"[+] %s%s created.\n", TestPath, TestFile); } else { wprintf(L"[-] Fail to create %s%s...\n", TestPath, TestFile); return -1; } wprintf(L"[*] Writing %d byte(s) to %s...\n", 2*wcslen(TestData), TestFile); result = WriteDataToFile(TestFilename, wcslen(TestFilename), (unsigned char*)TestData, wcslen(TestData)*2); if (result == 0) { wprintf(L"[+] Wrote %d byte(s) to %s...\n", 2*wcslen(TestData), TestFile); } else { DWORD LastError = GetLastError(); wprintf(L"[-] Failed to write to %s.(ERR_CODE: %02x)...\n", TestFile, LastError); return -1; } return 0; }
ULONG CreateImage(const char *source_path, const char *res_path, UCHAR n, UCHAR k) { ULONG BytesInBlock = k * RAW_LEN; ULONG SourceFileBytesNum; ULONG SourceFileBlocksNum, Remainder; ULONG j; UCHAR i; PUCHAR ReadBuffer, WriteBuffer; FILE *source; FILE *res; FORMATTING formatting; CreateFormatting(&formatting, n, k); printf("Creating empty file..."); CreateEmptyFile(res_path, formatting.ZonesNum * formatting.SectorsInZone); printf(" done.\n"); source = fopen(source_path, "rb"); res = fopen(res_path, "r+b"); ReadBuffer = malloc(BytesInBlock); WriteBuffer = malloc(RAW_LEN); fseek(source, 0, SEEK_END); SourceFileBytesNum = ftell(source); fseek(source, 0, SEEK_SET); SourceFileBlocksNum = (SourceFileBytesNum / BytesInBlock) + 1; Remainder = SourceFileBytesNum % BytesInBlock; for(j=0; j<SourceFileBlocksNum; j++) { if(j % 100 == 0) printf("Block %d/%d\n", j, SourceFileBlocksNum); if(j == SourceFileBlocksNum - 1) { fread(ReadBuffer, 1, BytesInBlock - Remainder, source); memset(ReadBuffer + BytesInBlock - Remainder, 0, Remainder); } else fread(ReadBuffer, 1, BytesInBlock, source); fseek(res, GetBaseOffset(&formatting, j)*RAW_LEN, SEEK_SET); for(i=1; i<=k; i++) { GFCreateSlice(WriteBuffer, ReadBuffer, RAW_LEN, k, i); fwrite(WriteBuffer, 1, RAW_LEN, res); } for(i=0; i<n-k; i++) { GFCreateSlice(WriteBuffer, ReadBuffer, RAW_LEN, k, k + i + 1); fseek(res, GetBackupOffset(&formatting, j, i)*RAW_LEN, SEEK_SET); fwrite(WriteBuffer, 1, RAW_LEN, res); } } fclose(source); fclose(res); free(ReadBuffer); free(WriteBuffer); return 0; }
void GenericAgentInitialize(EvalContext *ctx, GenericAgentConfig *config) { int force = false; struct stat statbuf, sb; char vbuff[CF_BUFSIZE]; char ebuff[CF_EXPANDSIZE]; #ifdef __MINGW32__ InitializeWindows(); #endif DetermineCfenginePort(); EvalContextClassPutHard(ctx, "any", "source=agent"); GenericAgentAddEditionClasses(ctx); strcpy(VPREFIX, GetConsolePrefix()); /* Define trusted directories */ { const char *workdir = GetWorkDir(); if (!workdir) { FatalError(ctx, "Error determining working directory"); } strcpy(CFWORKDIR, workdir); MapName(CFWORKDIR); } OpenLog(LOG_USER); SetSyslogFacility(LOG_USER); Log(LOG_LEVEL_VERBOSE, "Work directory is %s", CFWORKDIR); snprintf(vbuff, CF_BUFSIZE, "%s%cupdate.conf", GetInputDir(), FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%cbin%ccf-agent -D from_cfexecd", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%coutputs%cspooled_reports", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%clastseen%cintermittencies", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%creports%cvarious", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s", GetInputDir()); if (stat(vbuff, &sb) == -1) { FatalError(ctx, " No access to WORKSPACE/inputs dir"); } else { chmod(vbuff, sb.st_mode | 0700); } snprintf(vbuff, CF_BUFSIZE, "%s%coutputs", CFWORKDIR, FILE_SEPARATOR); if (stat(vbuff, &sb) == -1) { FatalError(ctx, " No access to WORKSPACE/outputs dir"); } else { chmod(vbuff, sb.st_mode | 0700); } snprintf(ebuff, sizeof(ebuff), "%s%cstate%ccf_procs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(ebuff, force); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } snprintf(ebuff, sizeof(ebuff), "%s%cstate%ccf_rootprocs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } snprintf(ebuff, sizeof(ebuff), "%s%cstate%ccf_otherprocs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } snprintf(ebuff, sizeof(ebuff), "%s%cstate%cprevious_state%c", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(ebuff, force); snprintf(ebuff, sizeof(ebuff), "%s%cstate%cdiff%c", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(ebuff, force); snprintf(ebuff, sizeof(ebuff), "%s%cstate%cuntracked%c", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(ebuff, force); OpenNetwork(); CryptoInitialize(); CheckWorkingDirectories(ctx); /* Initialize keys and networking. cf-key, doesn't need keys. In fact it must function properly even without them, so that it generates them! */ if (config->agent_type != AGENT_TYPE_KEYGEN) { LoadSecretKeys(); char *bootstrapped_policy_server = ReadPolicyServerFile(CFWORKDIR); PolicyHubUpdateKeys(bootstrapped_policy_server); free(bootstrapped_policy_server); cfnet_init(); } size_t cwd_size = PATH_MAX; while (true) { char cwd[cwd_size]; if (!getcwd(cwd, cwd_size)) { if (errno == ERANGE) { cwd_size *= 2; continue; } Log(LOG_LEVEL_WARNING, "Could not determine current directory. (getcwd: '%s')", GetErrorStr()); break; } EvalContextSetLaunchDirectory(ctx, cwd); break; } if (!MINUSF) { GenericAgentConfigSetInputFile(config, GetInputDir(), "promises.cf"); } VIFELAPSED = 1; VEXPIREAFTER = 1; setlinebuf(stdout); if (config->agent_specific.agent.bootstrap_policy_server) { snprintf(vbuff, CF_BUFSIZE, "%s%cfailsafe.cf", GetInputDir(), FILE_SEPARATOR); if (stat(vbuff, &statbuf) == -1) { GenericAgentConfigSetInputFile(config, GetInputDir(), "failsafe.cf"); } else { GenericAgentConfigSetInputFile(config, GetInputDir(), vbuff); } } }
void GenericAgentInitialize(EvalContext *ctx, GenericAgentConfig *config) { int force = false; struct stat statbuf, sb; char vbuff[CF_BUFSIZE]; char ebuff[CF_EXPANDSIZE]; SHORT_CFENGINEPORT = htons((unsigned short) 5308); snprintf(STR_CFENGINEPORT, 15, "5308"); EvalContextHeapAddHard(ctx, "any"); strcpy(VPREFIX, GetConsolePrefix()); /* Define trusted directories */ { const char *workdir = GetWorkDir(); if (!workdir) { FatalError(ctx, "Error determining working directory"); } strcpy(CFWORKDIR, workdir); MapName(CFWORKDIR); } /* On windows, use 'binary mode' as default for files */ #ifdef __MINGW32__ _fmode = _O_BINARY; #endif OpenLog(LOG_USER); SetSyslogFacility(LOG_USER); if (!LOOKUP) /* cf-know should not do this in lookup mode */ { Log(LOG_LEVEL_VERBOSE, "Work directory is %s", CFWORKDIR); snprintf(vbuff, CF_BUFSIZE, "%s%cinputs%cupdate.conf", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%cbin%ccf-agent -D from_cfexecd", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%coutputs%cspooled_reports", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%clastseen%cintermittencies", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%creports%cvarious", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(vbuff, force); snprintf(vbuff, CF_BUFSIZE, "%s%cinputs", CFWORKDIR, FILE_SEPARATOR); if (stat(vbuff, &sb) == -1) { FatalError(ctx, " No access to WORKSPACE/inputs dir"); } else { chmod(vbuff, sb.st_mode | 0700); } snprintf(vbuff, CF_BUFSIZE, "%s%coutputs", CFWORKDIR, FILE_SEPARATOR); if (stat(vbuff, &sb) == -1) { FatalError(ctx, " No access to WORKSPACE/outputs dir"); } else { chmod(vbuff, sb.st_mode | 0700); } sprintf(ebuff, "%s%cstate%ccf_procs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); MakeParentDirectory(ebuff, force); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } sprintf(ebuff, "%s%cstate%ccf_rootprocs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } sprintf(ebuff, "%s%cstate%ccf_otherprocs", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); if (stat(ebuff, &statbuf) == -1) { CreateEmptyFile(ebuff); } } OpenNetwork(); CryptoInitialize(); if (!LOOKUP) { CheckWorkingDirectories(ctx); } const char *bootstrapped_policy_server = ReadPolicyServerFile(CFWORKDIR); /* Initialize keys and networking. cf-key, doesn't need keys. In fact it must function properly even without them, so that it generates them! */ if (config->agent_type != AGENT_TYPE_KEYGEN) { LoadSecretKeys(bootstrapped_policy_server); cfnet_init(); } if (!MINUSF) { GenericAgentConfigSetInputFile(config, GetWorkDir(), "promises.cf"); } DetermineCfenginePort(); VIFELAPSED = 1; VEXPIREAFTER = 1; setlinebuf(stdout); if (config->agent_specific.agent.bootstrap_policy_server) { snprintf(vbuff, CF_BUFSIZE, "%s%cinputs%cfailsafe.cf", CFWORKDIR, FILE_SEPARATOR, FILE_SEPARATOR); #ifndef HAVE_ENTERPRISE if (stat(vbuff, &statbuf) == -1) { GenericAgentConfigSetInputFile(config, GetWorkDir(), "failsafe.cf"); } else #endif { GenericAgentConfigSetInputFile(config, GetWorkDir(), vbuff); } } }
void InitializeGA(int argc,char *argv[]) { int seed,force = false; struct stat statbuf,sb; unsigned char s[16]; char vbuff[CF_BUFSIZE]; char ebuff[CF_EXPANDSIZE]; SHORT_CFENGINEPORT = htons((unsigned short)5308); snprintf(STR_CFENGINEPORT,15,"5308"); NewClass("any"); #if defined HAVE_CONSTELLATION NewClass("constellation_edition"); #elif defined HAVE_NOVA NewClass("nova_edition"); #else NewClass("community_edition"); #endif strcpy(VPREFIX,GetConsolePrefix()); if (VERBOSE) { NewClass("verbose_mode"); } if (INFORM) { NewClass("inform_mode"); } if (DEBUG) { NewClass("debug_mode"); } CfOut(cf_verbose,"","Cfengine - autonomous configuration engine - commence self-diagnostic prelude\n"); CfOut(cf_verbose,"","------------------------------------------------------------------------\n"); /* Define trusted directories */ #ifdef MINGW if(NovaWin_GetProgDir(CFWORKDIR, CF_BUFSIZE - sizeof("Cfengine"))) { strcat(CFWORKDIR, "\\Cfengine"); } else { CfOut(cf_error, "", "!! Could not get CFWORKDIR from Windows environment variable, falling back to compile time dir (%s)", WORKDIR); strcpy(CFWORKDIR,WORKDIR); } Debug("Setting CFWORKDIR=%s\n", CFWORKDIR); #elif defined(CFCYG) strcpy(CFWORKDIR,WORKDIR); MapName(CFWORKDIR); #else if (getuid() > 0) { strncpy(CFWORKDIR,GetHome(getuid()),CF_BUFSIZE-10); strcat(CFWORKDIR,"/.cfagent"); if (strlen(CFWORKDIR) > CF_BUFSIZE/2) { FatalError("Suspicious looking home directory. The path is too long and will lead to problems."); } } else { strcpy(CFWORKDIR,WORKDIR); } #endif /* On windows, use 'binary mode' as default for files */ #ifdef MINGW _fmode = _O_BINARY; #endif strcpy(SYSLOGHOST,"localhost"); SYSLOGPORT = htons(514); Cf3OpenLog(LOG_USER); if (!LOOKUP) /* cf-know should not do this in lookup mode */ { CfOut(cf_verbose,"","Work directory is %s\n",CFWORKDIR); snprintf(HASHDB,CF_BUFSIZE-1,"%s%c%s",CFWORKDIR,FILE_SEPARATOR,CF_CHKDB); snprintf(vbuff,CF_BUFSIZE,"%s%cinputs%cupdate.conf",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(vbuff,force); snprintf(vbuff,CF_BUFSIZE,"%s%cbin%ccf-agent -D from_cfexecd",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(vbuff,force); snprintf(vbuff,CF_BUFSIZE,"%s%coutputs%cspooled_reports",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(vbuff,force); snprintf(vbuff,CF_BUFSIZE,"%s%clastseen%cintermittencies",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(vbuff,force); snprintf(vbuff,CF_BUFSIZE,"%s%creports%cvarious",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(vbuff,force); snprintf(vbuff,CF_BUFSIZE,"%s%cinputs",CFWORKDIR,FILE_SEPARATOR); if (cfstat(vbuff,&sb) == -1) { FatalError(" !!! No access to WORKSPACE/inputs dir"); } else { cf_chmod(vbuff,sb.st_mode | 0700); } snprintf(vbuff,CF_BUFSIZE,"%s%coutputs",CFWORKDIR,FILE_SEPARATOR); if (cfstat(vbuff,&sb) == -1) { FatalError(" !!! No access to WORKSPACE/outputs dir"); } else { cf_chmod(vbuff,sb.st_mode | 0700); } sprintf(ebuff,"%s%cstate%ccf_procs",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); MakeParentDirectory(ebuff,force); if (cfstat(ebuff,&statbuf) == -1) { CreateEmptyFile(ebuff); } sprintf(ebuff,"%s%cstate%ccf_rootprocs",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); if (cfstat(ebuff,&statbuf) == -1) { CreateEmptyFile(ebuff); } sprintf(ebuff,"%s%cstate%ccf_otherprocs",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); if (cfstat(ebuff,&statbuf) == -1) { CreateEmptyFile(ebuff); } } OpenNetwork(); /* Init crypto stuff */ OpenSSL_add_all_algorithms(); OpenSSL_add_all_digests(); ERR_load_crypto_strings(); if(!LOOKUP) { CheckWorkingDirectories(); } RandomSeed(); RAND_bytes(s,16); s[15] = '\0'; seed = ElfHash(s); srand48((long)seed); LoadSecretKeys(); /* CheckOpts(argc,argv); - MacOS can't handle this back reference */ if (!MINUSF) { snprintf(VINPUTFILE,CF_BUFSIZE-1,"promises.cf"); } AUDITDBP = NULL; DetermineCfenginePort(); VIFELAPSED = 1; VEXPIREAFTER = 1; setlinebuf(stdout); if (BOOTSTRAP) { snprintf(vbuff,CF_BUFSIZE,"%s%cinputs%cfailsafe.cf",CFWORKDIR,FILE_SEPARATOR,FILE_SEPARATOR); if (!IsEnterprise() && cfstat(vbuff,&statbuf) == -1) { snprintf(VINPUTFILE,CF_BUFSIZE-1,".%cfailsafe.cf",FILE_SEPARATOR); } else { strncpy(VINPUTFILE,vbuff,CF_BUFSIZE-1); } } }