void CPathID::SetContentPrivate(const char* content) { if (content == NULL || content[0] == '\0') { m_value = InvalidID; #if STRINGID_USESTRINGCONTENT m_content = NULL; #endif } else { m_value = CRC32::CalcCRCNoCase(content); } #if STRINGID_USESTRINGCONTENT behaviac::Mutex s_cs; behaviac::ScopedLock lock(s_cs); StringIdDictionary& dictionary = GetDictionary(); StringIdDictionary::iterator it = dictionary.find(m_value); if (it != dictionary.end()) { if (string_icmp(content, (*it).second)) { BEHAVIAC_ASSERT(0, "There is a conflict between two PathID for the CRC 0x%08X.\n%s\n%s\n\nThe result of that is unpredictable but will probably crash.", m_value, content, (*it).second); } m_content = (*it).second; } else { uint32_t len = strlen(content); char* str = (char*)BEHAVIAC_MALLOC_WITHTAG(len + 1, "PathID"); string_cpy(str, content); dictionary[m_value] = str; m_content = str; } #endif }
void CFileSystem::MakeSureDirectoryExist(const char* filename) { const int kMAX_PATH = 260; char directory[kMAX_PATH]; string_cpy(directory, filename); char* iter = directory; while (*iter != 0) { if (*iter == '\\' || *iter == '/') { char c = *iter; *iter = 0; mkdir(directory, S_IWUSR); *iter = c; } iter++; } }
TMApplication* get_application_mysql(int application_id) { int retc; if(con==NULL) if((retc=init_database())==0) return retc; char query_buffer[QUERY_BUFFER_SIZE]; sprintf(query_buffer, " \ SELECT Applications.id, Applications.name, Applications.certificate_id, Applications.analyzer_id, \ Analyzers.name, Analyzers.description, Analyzers.src, \ Certificates.name, Certificates.root_cert_path, Analyzers.args \ FROM Applications \ LEFT JOIN Certificates \ ON Certificates.id = Applications.certificate_id \ LEFT JOIN Analyzers \ ON Analyzers.id = Applications.analyzer_id \ WHERE Applications.id='%d' \ LIMIT 1;", application_id); mysql_query(con, query_buffer); MYSQL_RES* result = mysql_store_result(con); //printf("num-rows: %d\n", mysql_num_rows(result)); if(mysql_num_rows(result)==0) return NULL; TMApplication* application = (TMApplication*)malloc(sizeof(TMApplication)); if(application==NULL) return NULL; MYSQL_ROW* row = mysql_fetch_row(result); if(row[0]!=NULL) application->id = atoi(row[0]); string_cpy(&(application->name), row[1]); application->certificate = (TMCertificate*)malloc(sizeof(TMCertificate)); if(application->certificate==NULL) return NULL; if(row[2]!=NULL) application->certificate->id = atoi(row[2]); string_cpy(&(application->certificate->name), row[7]); string_cpy(&(application->certificate->root_cert_path), row[8]); application->analyzer = (TMAnalyzer*)malloc(sizeof(TMAnalyzer)); if(application->analyzer==NULL) return NULL; if(row[3]!=NULL) { application->analyzer->id = atoi(row[3]); string_cpy(&(application->analyzer->name), row[4]); string_cpy(&(application->analyzer->description), row[5]); string_cpy(&(application->analyzer->src), row[6]); string_cpy(&(application->analyzer->args), row[9]); } mysql_free_result(result); sprintf(query_buffer, " \ SELECT application_config.id, application_config.config_name, application_config.config_value \ FROM application_config \ WHERE application_config.application_id = '%d'", application_id); mysql_query(con, query_buffer); result = mysql_store_result(con); application->config = NULL; if(mysql_num_rows(result)>0) { MYSQL_ROW row; TMApplication_config* config = NULL; while((row = mysql_fetch_row(result))) { //TMApplication_config* config = TMApplication_config* last_config = config; config = (TMApplication_config*)malloc(sizeof(TMApplication_config)); if(config==NULL) exit(1); config->next=NULL; if(last_config==NULL) application->config = config; else last_config->next = config; if(row[0]!=NULL) { config->id = atoi(row[0]); string_cpy(&(config->config_name), row[1]); string_cpy(&(config->config_value), row[2]); } } mysql_free_result(result); } return (TMApplication*) application; //return NULL; }
TMAccess_list* get_filter_access_lists(int filter_id) { int retc; TMAccess_list* return_acl = NULL; if(con==NULL) if((retc=init_database())==0) return retc; char query_buffer[QUERY_BUFFER_SIZE]; sprintf(query_buffer, " \ SELECT AccessLists.id, AccessLists.action, AccessLists.protocol, \ SRC.address as src_address, SRC.mask as src_mask,\ DST.address as dst_address, DST.mask as dst_mask, \ PNS.greater_or_equal, PNS.less_or_equal, \ PND.greater_or_equal, PND.less_or_equal \ FROM AccessLists \ LEFT JOIN IpNetworks SRC \ ON AccessLists.ip_source = SRC.id \ LEFT JOIN IpNetworks DST \ ON AccessLists.ip_destination = DST.id \ LEFT JOIN Ports PNS \ ON AccessLists.pn_source = PNS.id \ LEFT JOIN Ports PND \ ON AccessLists.pn_destination = PND.id \ WHERE AccessLists.filter_id='%d'; \ ", filter_id); mysql_query(con, query_buffer); //printf("%s\n", mysql_error(con)); MYSQL_RES* result = mysql_store_result(con); //printf("FACnum-rows: %d\n", mysql_num_rows(result)); if(mysql_num_rows(result)==0) return NULL; MYSQL_ROW row; TMAccess_list* acl; while((row = mysql_fetch_row(result))) { TMAccess_list* last_acl = acl; acl = (TMAccess_list*)malloc(sizeof(TMAccess_list)); if(acl==NULL) return NULL; acl->next=NULL; if(return_acl==NULL) return_acl = acl; else last_acl->next = acl; //filter->id = atoi(row[0]); if(row[0]!=NULL) acl->id = atoi(row[0]); if(strcmp(row[1], "permit")==0) acl->action = PERMIT; else acl->action = DENY; string_cpy(&(acl->protocol), row[2]); acl->ip_source = (TMIp_network*)malloc(sizeof(TMIp_network)); acl->ip_destination = (TMIp_network*)malloc(sizeof(TMIp_network)); if(acl->ip_source==NULL || acl->ip_destination==NULL) return NULL; string_cpy(&(acl->ip_source->address), row[3]); if(row[4]!=NULL) acl->ip_source->mask = atoi(row[4]); string_cpy(&(acl->ip_destination->address), row[5]); if(row[6]!=NULL) acl->ip_destination->mask = atoi(row[6]); // Source ports if(row[7]!=NULL && row[8]!=NULL) { acl->pn_source = (TMPorts*)malloc(sizeof(TMPorts)); if(acl->pn_source==NULL) return NULL; acl->pn_source->greater_or_equal = atoi(row[7]); acl->pn_source->less_or_equal = atoi(row[8]); //printf(">%d %d\n", acl->pn_source->greater_or_equal, acl->pn_source->less_or_equal); } else { acl->pn_source = NULL; } // Destination ports if(row[9]!=NULL && row[10]!=NULL) { acl->pn_destination = (TMPorts*)malloc(sizeof(TMPorts)); if(acl->pn_destination==NULL) return NULL; acl->pn_destination->greater_or_equal = atoi(row[9]); acl->pn_destination->less_or_equal = atoi(row[10]); //printf(">%d %d\n", acl->pn_destination->greater_or_equal, acl->pn_destination->less_or_equal); } else { acl->pn_destination = NULL; } } mysql_free_result(result); return (TMAccess_list*)return_acl; }
void CStringID::SetContent(const char* content, bool noCase, bool resolve) { BEHAVIAC_UNUSED_VAR(resolve); #ifdef BEHAVIAC_STRINGID_DEBUG_CASE m_noCase = noCase; #endif #if BEHAVIAC_STRINGID_USESTRINGCONTENT static behaviac::Mutex ms_critialsection; #ifdef BEHAVIAC_STRINGID_RESOLVE_CONTENT if (resolve) { ms_critialsection.Lock(); StringIdDictionary::iterator it = GetContentDictionary().find(m_value); if (it != GetContentDictionary().end()) { m_content = (*it).second; } ms_critialsection.Unlock(); return; } #endif #endif if (content == NULL || content[0] == '\0') { m_value = InvalidID; #if BEHAVIAC_STRINGID_USESTRINGCONTENT m_content = NULL; #endif } else { if (noCase) { BEHAVIAC_ASSERT(false); m_value = CRC32::CalcCRCNoCase(content); } else { m_value = CRC32::CalcCRC(content); } #if BEHAVIAC_STRINGID_USESTRINGCONTENT ms_critialsection.Lock(); StringIdDictionary::iterator it = GetContentDictionary().find(m_value); if (it != GetContentDictionary().end()) { if (noCase) { if (string_icmp(content, (*it).second)) { BEHAVIAC_LOG3(BEHAVIAC_LOG_INFO, "There is a conflict between two StringID for the CRC 0x%08X.\n%s\n%s\n\nThe result of that is unpredictable but will probably crash.", m_value, content, (*it).second); } } else { if (strcmp(content, (*it).second)) { BEHAVIAC_LOG3(BEHAVIAC_LOG_INFO, "There is a conflict between two StringID for the CRC 0x%08X.\n%s\n%s\n\nThe result of that is unpredictable but will probably crash.", m_value, content, (*it).second); } } m_content = (*it).second; } else { uint32_t len = strlen(content); char* str = (char*)BEHAVIAC_MALLOC(sizeof(char) * (len + 1)); string_cpy(str, content); if (noCase) { for (uint32_t i = 0; i < len; ++i) { str[i] = (char)tolower(str[i]); } } GetContentDictionary()[m_value] = str; m_content = str; } ms_critialsection.Unlock(); #endif } }
TConfig* parse_config(char* config_filename) { FILE* config_fd = fopen(config_filename, "r"); if(config_fd==NULL) exit(EXIT_FAILURE); size_t cflen; ssize_t cfread; char* line = NULL; unsigned int state = 0; config = malloc(sizeof(TConfig)); if(config==NULL) exit(EXIT_FAILURE); while((cfread = getline(&line, &cflen, config_fd)) != -1) { // skip empty lines if(strlen(line)<3) continue; // remove last new-line character if(line[strlen(line)-1]=='\r' || line[strlen(line)-1]=='\n') line[strlen(line)-1] = 0; if(line[0]=='[') state = 0; if(strcmp(line, "[DATABASE]")==0) { state = 1; // change state to database section config->database = malloc(sizeof(TConfigDbInfo)); if(config->database==NULL) exit(EXIT_FAILURE); } else if(strcmp(line, "[APPLICATION]")==0) { state = 2; // change state to application section } /*else if(strcmp(line, "[DATAPATH]")==0) { state = 3; // change state to datapath section config->datapath = malloc(sizeof(TConfigDpInfo)); if(config->datapath==NULL) exit(EXIT_FAILURE); }*/ // [DATABASE] else if(state==1) { char* left = strtok(line, "="); char* right = strtok(NULL, "="); if(strcmp(left,"type")==0) { if(strcmp(right,"mysql")==0) { config->database->type = DB_MYSQL; } else { fprintf(stderr, "Unknown type of database \"%s\"\n.", right); exit(EXIT_FAILURE); } } else if(strcmp(left,"hostname")==0) { string_cpy(&(config->database->hostname), right); } else if(strcmp(left,"username")==0) { string_cpy(&(config->database->username), right); } else if(strcmp(left,"password")==0) { string_cpy(&(config->database->password), right); } else if(strcmp(left,"schema")==0) { string_cpy(&(config->database->schema), right); } else if(strcmp(left,"results")==0) { string_cpy(&(config->database->results), right); } } // [APPLICATION] else if(state==2) { char* left = strtok(line, "="); char* right = strtok(NULL, "="); if(strcmp(left,"id")==0) { config->application_id = atoi(right); } } /* // [DATAPATH] else if(state==3) { char* left = strtok(line, "="); char* right = strtok(NULL, "="); if(strcmp(left,"username")==0) { string_cpy(&(config->datapath->username), right); } else if(strcmp(left,"password")==0) { string_cpy(&(config->datapath->password), right); } else if(strcmp(left,"ip")==0) { string_cpy(&(config->datapath->ip), right); } else if(strcmp(left,"interface")==0) { string_cpy(&(config->datapath->interface), right); } else if(strcmp(left,"local_cert")==0) { string_cpy(&(config->datapath->local_cert), right); } else if(strcmp(left,"remote_cert")==0) { string_cpy(&(config->datapath->remote_cert), right); } }*/ else { } } return NULL; }