char* readCryptLogMessageWithData(eCRYPT_LOG_MESSAGE message, void* data) { char* logMessage = (char*)calloc(256, sizeof(char)); if( message == eCryptLogMessage_Allow ) { strcpy(logMessage, "allow"); } if( message == eCryptLogMessage_Fail_Handle_Open ) { strcpy(logMessage, "crypt handle open failed"); } if( message == eCryptLogMessage_Fail_Encrypt ) strcpy(logMessage, "encrypt fail"); if( message == eCryptLogMessage_Fail_Decrypt ) strcpy(logMessage, "decrypt fail"); if( message == eCryptLogMessage_Not_Register_Global ) strcpy(logMessage, "not register crypt global"); if( message == eCryptLogMessage_Not_Register_Column ) strcpy(logMessage, "not register crypt column"); if( message == eCryptLogMessage_Not_Register_Key ) strcpy(logMessage, "not register crypt key"); if( message == eCryptLogMessage_Not_Register_Behavior ) strcpy(logMessage, "not register behavior"); if( message == eCryptLogMessage_Not_Register_Policy ) strcpy(logMessage, "not register policy"); if( message == eCryptLogMessage_Not_Possible_Crypt ) strcpy(logMessage, "not possible crypt"); if( message == eCryptLogMessage_Null_Data ) strcpy(logMessage, "data is null"); if( message == eCryptLogMessage_Null_IP ) strcpy(logMessage, "ip is null"); if( message == eCryptLogMessage_Null_User ) strcpy(logMessage, "user is null"); if( message == eCryptLogMessage_Invalid_Option ) strcpy(logMessage, "invalid option algorithm: NONE, mode: NONE"); if( message == eCryptLogMessage_Invalid_Option_None_Algorithm ) strcpy(logMessage, "invalid option algorithm: NONE"); if( message == eCryptLogMessage_Invalid_Option_None_Mode ) strcpy(logMessage, "invalid option mode: NONE"); if( message == eCryptLogMessage_Invalid_Option_Encrypt ) strcpy(logMessage, "invalid option encrypt"); if( message == eCryptLogMessage_Invalid_Option_Decrypt ) strcpy(logMessage, "invalid option decrypt"); if( message == eCryptLogMessage_Over_Normal_To_Data ) strcpy(logMessage, "normal length > data length"); if( message == eCryptLogMessage_Over_Behavior ) { int splitDataCnt = 0; char** splitData = strSplit((char*)data, LOG_DATA_SPLIT_STR, &splitDataCnt); // sprintf(logMessage, "behavior over [ \"%s\" - \"%s\" ] - small time : %ld, small count : ( %d / %d ), big time : %ld, big count : ( %d / %d )" // , splitData[0], splitData[1], splitData[2], splitData[3], splitData[4], splitData[5], splitData[6], splitData[7]); sprintf(logMessage, "behavior over [ \"%s\" - \"%s\" ] - small count : ( %s / %s ), big count : ( %s / %s )" , splitData[0], splitData[1], splitData[2], splitData[3], splitData[4], splitData[5]); freeSplit(splitData, splitDataCnt); } return logMessage; }
int fdfs_load_storage_ids(char *content, const char *pStorageIdsFilename) { char **lines; char *line; char *id; char *group_name; char *pHost; char *pIpAddr; char *pPort; FDFSStorageIdInfo *pStorageIdInfo; FDFSStorageIdInfo **ppStorageIdInfo; FDFSStorageIdInfo **ppStorageIdEnd; int alloc_bytes; int result; int line_count; int i; lines = split(content, '\n', 0, &line_count); if (lines == NULL) { return ENOMEM; } result = 0; do { g_storage_id_count = 0; for (i=0; i<line_count; i++) { trim(lines[i]); if (*lines[i] == '\0' || *lines[i] == '#') { continue; } g_storage_id_count++; } if (g_storage_id_count == 0) { logError("file: "__FILE__", line: %d, " \ "config file: %s, no storage id!", \ __LINE__, pStorageIdsFilename); result = ENOENT; break; } alloc_bytes = sizeof(FDFSStorageIdInfo) * g_storage_id_count; g_storage_ids_by_ip = (FDFSStorageIdInfo *)malloc(alloc_bytes); if (g_storage_ids_by_ip == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", __LINE__, \ alloc_bytes, result, STRERROR(result)); break; } memset(g_storage_ids_by_ip, 0, alloc_bytes); alloc_bytes = sizeof(FDFSStorageIdInfo *) * g_storage_id_count; g_storage_ids_by_id = (FDFSStorageIdInfo **)malloc(alloc_bytes); if (g_storage_ids_by_id == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", __LINE__, \ alloc_bytes, result, STRERROR(result)); free(g_storage_ids_by_ip); break; } memset(g_storage_ids_by_id, 0, alloc_bytes); pStorageIdInfo = g_storage_ids_by_ip; for (i=0; i<line_count; i++) { line = lines[i]; if (*line == '\0' || *line == '#') { continue; } id = line; group_name = line; while (!(*group_name == ' ' || *group_name == '\t' \ || *group_name == '\0')) { group_name++; } if (*group_name == '\0') { logError("file: "__FILE__", line: %d, " \ "config file: %s, line no: %d, " \ "content: %s, invalid format, " \ "expect group name and ip address!", \ __LINE__, pStorageIdsFilename, \ i + 1, line); result = EINVAL; break; } *group_name = '\0'; group_name++; //skip space char while (*group_name == ' ' || *group_name == '\t') { group_name++; } pHost = group_name; while (!(*pHost == ' ' || *pHost == '\t' \ || *pHost == '\0')) { pHost++; } if (*pHost == '\0') { logError("file: "__FILE__", line: %d, " \ "config file: %s, line no: %d, " \ "content: %s, invalid format, " \ "expect ip address!", __LINE__, \ pStorageIdsFilename, i + 1, line); result = EINVAL; break; } *pHost = '\0'; pHost++; //skip space char while (*pHost == ' ' || *pHost == '\t') { pHost++; } pIpAddr = pHost; pPort = strchr(pHost, ':'); if (pPort != NULL) { *pPort = '\0'; pStorageIdInfo->port = atoi(pPort + 1); } else { pStorageIdInfo->port = 0; } if (getIpaddrByName(pIpAddr, pStorageIdInfo->ip_addr, \ sizeof(pStorageIdInfo->ip_addr)) == INADDR_NONE) { logError("file: "__FILE__", line: %d, " \ "invalid host name: %s", __LINE__, pIpAddr); result = EINVAL; break; } if (!fdfs_is_server_id_valid(id)) { logError("file: "__FILE__", line: %d, " \ "invalid server id: \"%s\", " \ "which must be a none zero start " \ "integer, such as 100001", __LINE__, id); result = EINVAL; break; } snprintf(pStorageIdInfo->id, \ sizeof(pStorageIdInfo->id), "%s", id); snprintf(pStorageIdInfo->group_name, \ sizeof(pStorageIdInfo->group_name), \ "%s", group_name); pStorageIdInfo++; } } while (0); freeSplit(lines); if (result != 0) { return result; } logDebug("file: "__FILE__", line: %d, " \ "g_storage_id_count: %d", __LINE__, g_storage_id_count); pStorageIdInfo = g_storage_ids_by_ip; for (i=0; i<g_storage_id_count; i++) { char szPortPart[16]; if (pStorageIdInfo->port > 0) { sprintf(szPortPart, ":%d", pStorageIdInfo->port); } else { *szPortPart = '\0'; } logDebug("%s %s %s%s", pStorageIdInfo->id, pStorageIdInfo->group_name, pStorageIdInfo->ip_addr, szPortPart); pStorageIdInfo++; } ppStorageIdEnd = g_storage_ids_by_id + g_storage_id_count; pStorageIdInfo = g_storage_ids_by_ip; for (ppStorageIdInfo=g_storage_ids_by_id; ppStorageIdInfo < \ ppStorageIdEnd; ppStorageIdInfo++) { *ppStorageIdInfo = pStorageIdInfo++; } qsort(g_storage_ids_by_ip, g_storage_id_count, \ sizeof(FDFSStorageIdInfo), fdfs_cmp_group_name_and_ip); qsort(g_storage_ids_by_id, g_storage_id_count, \ sizeof(FDFSStorageIdInfo *), fdfs_cmp_server_id); return result; }
void writeEncDecLog(int syslogType, eCRYPT_LOG_MESSAGE logMessage, void* data) { if( IS_SUCCESS ) { if( logMessage == eCryptLogMessage_Allow ) *IS_SUCCESS = true; else *IS_SUCCESS = false; } void* logSM = getSharedMemory(LOG_SHARED_MEMORY_KEY, LOG_SHARED_MEMORY_SIZE); if( logSM == NULL ) logSM = createSharedMemory(LOG_SHARED_MEMORY_KEY, LOG_SHARED_MEMORY_SIZE); char dbsLogBuf[256] = {'\0', }; size_t logSMLength = strlen((char*)logSM); bool isFind = false; int dbsLogReplaceIndex = 0; int dbsLogCnt = 0; char** dbsLog = strSplitWithLength(logSM, logSMLength, LINE_SPLIT_STR, &dbsLogCnt); for(int i = 0 ; i < dbsLogCnt ; ++i) { if( i > 0 ) dbsLogReplaceIndex += strlen(LINE_SPLIT_STR); int logDataCnt = 0; int logDataLength = strlen(dbsLog[i]); char** logData = strSplitWithLength(dbsLog[i], logDataLength, LOG_SPLIT_STR, &logDataCnt); char* smLogThreadID = logData[1]; int smLogType = atoi(logData[2]); char* smLogAccessIP = logData[3]; char* smLogAccessUser = logData[4]; char* smLogProgramName = logData[5]; int smLogMessage = atoi(logData[6]); int smLogCnt = atoi(logData[7]); if( memcmp(LOG_THREAD_ID, smLogThreadID, LOG_THREAD_ID_LENGTH) != 0 || LOG_TYPE != smLogType || memcmp(LOG_ACCESS_IP, smLogAccessIP, LOG_ACCESS_IP_LENGTH) != 0 || memcmp(LOG_ACCESS_USER, smLogAccessUser, LOG_ACCESS_USER_LENGTH) != 0 || memcmp(LOG_PROGRAM_NAME, smLogProgramName, LOG_PROGRAM_NAME_LENGTH) != 0 || smLogMessage != logMessage ) { dbsLogReplaceIndex += logDataLength; freeSplit(logData, logDataCnt); continue; } else { if( smLogMessage == eCryptLogMessage_Over_Behavior && strcmp((char*)data, logData[8]) != 0 ) { dbsLogReplaceIndex += logDataLength; freeSplit(logData, logDataCnt); continue; } } isFind = true; sprintf(dbsLogBuf, "%d%s%s%s%d%s%s%s%s%s%s%s%d%s%d" , syslogType, LOG_SPLIT_STR , LOG_THREAD_ID, LOG_SPLIT_STR , LOG_TYPE, LOG_SPLIT_STR , LOG_ACCESS_IP, LOG_SPLIT_STR , LOG_ACCESS_USER, LOG_SPLIT_STR , LOG_PROGRAM_NAME, LOG_SPLIT_STR , logMessage, LOG_SPLIT_STR , smLogCnt + 1); if( logMessage == eCryptLogMessage_Over_Behavior ) { char dataBuf[64] = {'\0', }; sprintf(dataBuf, "%s%s", LOG_SPLIT_STR, (char*)data); strcat(dbsLogBuf, dataBuf); } char* dbsFinalLog = replaceIndexWithLength(logSM, dbsLogReplaceIndex, logSMLength, dbsLog[i], dbsLogBuf); writeSharedMemory(logSM, dbsFinalLog); free(dbsFinalLog); freeSplit(logData, logDataCnt); break; } freeSplit(dbsLog, dbsLogCnt); if( !isFind ) { sprintf(dbsLogBuf, "%s%d%s%s%s%d%s%s%s%s%s%s%s%d%s%d" , ( dbsLogCnt > 0 ) ? LINE_SPLIT_STR : "" , syslogType, LOG_SPLIT_STR , LOG_THREAD_ID, LOG_SPLIT_STR , LOG_TYPE, LOG_SPLIT_STR , LOG_ACCESS_IP, LOG_SPLIT_STR , LOG_ACCESS_USER, LOG_SPLIT_STR , LOG_PROGRAM_NAME, LOG_SPLIT_STR , logMessage, LOG_SPLIT_STR , 1); if( logMessage == eCryptLogMessage_Over_Behavior ) { char dataBuf[64] = {'\0', }; sprintf(dataBuf, "|%s", (char*)data); strcat(dbsLogBuf, dataBuf); } strcat(logSM, dbsLogBuf); } releaseSharedMemory(logSM); }