static VOID AddMutex( CS_SEMAPHORE *CsSem, i4 thread_type ) { u_i4 hashnum, hashstart; char *hashname; MUTEXES *hashtable = CsSamplerBlkPtr->Mutex; ++CsSamplerBlkPtr->nummutexsamples; /* count the total number */ if (CsSem->cs_sem_name[0] != '\0') { /* A "named" semaphore */ hashnum = ElfHash(CsSem->cs_sem_name) % MAXMUTEXES; hashname = CsSem->cs_sem_name; } else { /* All "unnamed" semaphores use the last slot */ hashnum = MAXMUTEXES; hashname = "<unnamed>"; } hashstart = hashnum; do { if (hashtable[hashnum].name[0] == '\0') { /* Slot available: add semaphore entry to the table */ STcopy(hashname, hashtable[hashnum].name); hashtable[hashnum].id = (char *) CsSem; hashtable[hashnum].count[thread_type] = 1; break; } if (STcompare(hashname, hashtable[hashnum].name) == 0) { /* Found the correct semaphore entry...increment the count. */ ++hashtable[hashnum].count[thread_type]; hashtable[hashnum].id = (char *) CsSem; break; } /* This is a hash collision...see if the next slot is available. */ /* If past the end of the table, wrap around to beginning. */ ++hashnum; if (hashnum >= MAXMUTEXES) hashnum = 0; } while (hashnum != hashstart); /* ** If we fall out of the 'do' loop without finding a slot (the table is ** full), just ignore this semaphore. */ } /* AddMutex() */
void CryptoInitialize() { static bool crypto_initialized = false; if (!crypto_initialized) { OpenSSL_add_all_algorithms(); OpenSSL_add_all_digests(); ERR_load_crypto_strings(); RandomSeed(); unsigned char s[16] = { 0 }; int seed = 0; RAND_bytes(s, 16); s[15] = '\0'; seed = ElfHash(s, CF_HASHTABLESIZE); srand48((long) seed); crypto_initialized = true; } }
void CheckOptsAndInit(int argc,char **argv) { extern char *optarg; int optindex = 0; char ld_library_path[CF_BUFSIZE]; int c,seed; unsigned char s[16]; ld_library_path[0] = '\0'; Banner("Check options"); NOSPLAY = false; sprintf(VPREFIX, "cfexecd"); openlog(VPREFIX,LOG_PID|LOG_NOWAIT|LOG_ODELAY,LOG_DAEMON); while ((c=getopt_long(argc,argv,"L:d:vhpqFV1g",CFDOPTIONS,&optindex)) != EOF) { switch ((char) c) { case 'd': switch ((optarg==NULL)?3:*optarg) { case '1': D1 = true; break; case '2': D2 = true; break; default: DEBUG = true; break; } NO_FORK = true; VERBOSE = true; printf("cfexecd Debug mode: running in foreground\n"); break; case 'v': VERBOSE = true; break; case 'V': printf("GNU %s-%s daemon\n%s\n",PACKAGE,VERSION,COPYRIGHT); printf("This program is covered by the GNU Public License and may be\n"); printf("copied free of charge. No warrenty is implied.\n\n"); exit(0); break; case 'q': NOSPLAY = true; break; case 'p': PARSEONLY = true; break; case 'g': NO_FORK = true; break; case 'L': snprintf(ld_library_path,CF_BUFSIZE-1,"LD_LIBRARY_PATH=%s",optarg); if (putenv(strdup(ld_library_path)) != 0) { } break; case 'F': case '1': ONCE = true; NO_FORK = true; break; case 'h': Syntax(); exit(1); break; /* never reached.... */ default: Syntax(); exit(1); } } LOGGING = true; /* Do output to syslog */ /* XXX Initialize workdir for non privileged users */ strcpy(CFWORKDIR,WORKDIR); #ifndef NT if (getuid() > 0) { char *homedir; if ((homedir = getenv("HOME")) != NULL) { strcpy(CFWORKDIR,homedir); strcat(CFWORKDIR,"/.cfagent"); } } #endif snprintf(VBUFF,CF_BUFSIZE,"%s/inputs/update.conf",CFWORKDIR); MakeDirectoriesFor(VBUFF,'y'); snprintf(VBUFF,CF_BUFSIZE,"%s/bin/cfagent -D from_cfexecd",CFWORKDIR); MakeDirectoriesFor(VBUFF,'y'); snprintf(VBUFF,CF_BUFSIZE,"%s/outputs/spooled_reports",CFWORKDIR); MakeDirectoriesFor(VBUFF,'y'); snprintf(VBUFF,CF_BUFSIZE,"%s/inputs",CFWORKDIR); chmod(VBUFF,0700); snprintf(VBUFF,CF_BUFSIZE,"%s/outputs",CFWORKDIR); chmod(VBUFF,0700); strncpy(VLOCKDIR,CFWORKDIR,CF_BUFSIZE-1); strncpy(VLOGDIR,CFWORKDIR,CF_BUFSIZE-1); VCANONICALFILE = strdup(CanonifyName(VINPUTFILE)); GetCfStuff(); MAILTO[0] = '\0'; MAILFROM[0] = '\0'; VIPADDRESS[0] = '\0'; VMAILSERVER[0] = '\0'; OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); CheckWorkDirectories(); RandomSeed(); RAND_bytes(s,16); s[15] = '\0'; seed = ElfHash(s); srand48((long)seed); }
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); } } }
static void TestHashEntropy(char *names, char *title) { char word[32], *sp; int i, j, slot, eslot, sslot, hashtable[CF_HASHTABLESIZE], ehashtable[CF_HASHTABLESIZE], shashtable[CF_HASHTABLESIZE]; int freq[10], efreq[10], sfreq[10]; double tot = 0, etot = 0, stot = 0; for (i = 0; i < CF_HASHTABLESIZE; i++) { hashtable[i] = 0; ehashtable[i] = 0; shashtable[i] = 0; } printf(" -> Trial of \"%s\":\n", title); for (i = 0, sp = names; *sp != '\0'; sp += strlen(word) + 1, i++) { struct timespec start, stop; word[0] = '\0'; sscanf(sp, "%s", word); if (word[0] == '\0') { break; } clock_gettime(CLOCK_REALTIME, &start); slot = RefHash(word); clock_gettime(CLOCK_REALTIME, &stop); tot += (double) (stop.tv_sec - start.tv_sec) + (double) (stop.tv_nsec - start.tv_nsec); clock_gettime(CLOCK_REALTIME, &start); eslot = ElfHash(word); clock_gettime(CLOCK_REALTIME, &stop); etot += (double) (stop.tv_sec - start.tv_sec) + (double) (stop.tv_nsec - start.tv_nsec); clock_gettime(CLOCK_REALTIME, &start); sslot = OatHash(word); clock_gettime(CLOCK_REALTIME, &stop); stot += (double) (stop.tv_sec - start.tv_sec) + (double) (stop.tv_nsec - start.tv_nsec); hashtable[slot]++; ehashtable[eslot]++; shashtable[sslot]++; printf("SLOTS: %d,%d,%d\n", slot, eslot, sslot); } printf("reference time %lf\n", tot / (double) CF_BILLION); printf("elf time %lf\n", etot / (double) CF_BILLION); printf("fast time %lf\n", stot / (double) CF_BILLION); printf(" -> Hashed %d %s words into %d slots with the following spectra:\n", i, title, CF_HASHTABLESIZE); for (j = 0; j < 10; j++) { freq[j] = efreq[j] = sfreq[j] = 0; } for (i = 0; i < CF_HASHTABLESIZE; i++) { for (j = 0; j < 10; j++) { if (hashtable[i] == j) { freq[j]++; } if (ehashtable[i] == j) { efreq[j]++; } if (shashtable[i] == j) { sfreq[j]++; } } } printf("\n"); for (j = 1; j < 10; j++) { if (freq[j] > 0) { printf(" -> F[%d] = %d\n", j, freq[j]); } } printf("\n"); for (j = 1; j < 10; j++) { if (efreq[j] > 0) { printf(" -> eF[%d] = %d\n", j, efreq[j]); } } printf("\n"); for (j = 1; j < 10; j++) { if (sfreq[j] > 0) { printf(" -> sF[%d] = %d\n", j, sfreq[j]); } } }