📄 smtp_config.c
字号:
**** @retval 0 successs** @retval -1 generic fatal error** @retval 1 generic non-fatal error*/static int ProcessPorts(char *ErrorString, int ErrStrLen){ char *pcToken; char *pcEnd; int iPort; int iEndPorts = 0; pcToken = strtok(NULL, CONF_SEPARATORS); if(!pcToken) { snprintf(ErrorString, ErrStrLen, "Invalid port list format."); return -1; } if(strcmp(CONF_START_LIST, pcToken)) { snprintf(ErrorString, ErrStrLen, "Must start a port list with the '%s' token.", CONF_START_LIST); return -1; } /* Since ports are specified, clear default ports */ _smtp_config.ports[SMTP_DEFAULT_SERVER_PORT / 8] &= ~(1 << (SMTP_DEFAULT_SERVER_PORT % 8)); _smtp_config.ports[XLINK2STATE_DEFAULT_PORT / 8] &= ~(1 << (XLINK2STATE_DEFAULT_PORT % 8)); _smtp_config.ports[SMTP_DEFAULT_SUBMISSION_PORT / 8] &= ~(1 << (SMTP_DEFAULT_SUBMISSION_PORT % 8)); while ((pcToken = strtok(NULL, CONF_SEPARATORS)) != NULL) { if(!strcmp(CONF_END_LIST, pcToken)) { iEndPorts = 1; break; } iPort = strtol(pcToken, &pcEnd, 10); /* ** Validity check for port */ if(*pcEnd) { snprintf(ErrorString, ErrStrLen, "Invalid port number."); return -1; } if(iPort < 0 || iPort > 65535) { snprintf(ErrorString, ErrStrLen, "Invalid port number. Must be between 0 and 65535."); return -1; } _smtp_config.ports[iPort / 8] |= (1 << (iPort % 8)); } if(!iEndPorts) { snprintf(ErrorString, ErrStrLen, "Must end '%s' configuration with '%s'.", CONF_PORTS, CONF_END_LIST); return -1; } return 0;}/*** NAME** ProcessCmds::*//**** Process the command list.**** This configuration is a list of valid ports and is ended by a ** delimiter.**** @param ErrorString error string buffer** @param ErrStrLen the length of the error string buffer**** @return an error code integer ** (0 = success, >0 = non-fatal error, <0 = fatal error)**** @retval 0 successs** @retval -1 generic fatal error*/static int ProcessCmds(char *ErrorString, int ErrStrLen, int action){ char *pcToken; int iEndCmds = 0; int id; pcToken = strtok(NULL, CONF_SEPARATORS); if (!pcToken) { snprintf(ErrorString, ErrStrLen, "Invalid command list format."); return -1; } if (strcmp(CONF_START_LIST, pcToken)) { snprintf(ErrorString, ErrStrLen, "Must start a command list with the '%s' token.", CONF_START_LIST); return -1; } while ((pcToken = strtok(NULL, CONF_SEPARATORS)) != NULL) { if (strcmp(CONF_END_LIST, pcToken) == 0) { iEndCmds = 1; break; } id = GetCmdId(pcToken); if (action == ACTION_ALERT) { _smtp_cmd_config[id].alert = 1; } else if (action == ACTION_NO_ALERT) { _smtp_cmd_config[id].alert = 0; } else if (action == ACTION_NORMALIZE) { _smtp_cmd_config[id].normalize = 1; } } if (!iEndCmds) { snprintf(ErrorString, ErrStrLen, "Must end '%s' configuration with '%s'.", action == ACTION_ALERT ? CONF_INVALID_CMDS : (action == ACTION_NO_ALERT ? CONF_VALID_CMDS : (action == ACTION_NORMALIZE ? CONF_NORMALIZE_CMDS : "")), CONF_END_LIST); return -1; } return 0;}/* Return id associated with a given command string */static int GetCmdId(char *name){ const SMTPToken *cmd; for (cmd = _smtp_cmds; cmd->name != NULL; cmd++) { if (strcasecmp(cmd->name, name) == 0) { return cmd->search_id; } } return AddCmd(name);}static int AddCmd(char *name){ static int num_cmds = CMD_LAST + 1; static int id = CMD_LAST; SMTPToken *cmds, *tmp_cmds; SMTPSearch *cmd_search; SMTPCmdConfig *cmd_config; int ret; /* allocate enough memory for new commmand - alloc one extra for NULL entry */ cmds = (SMTPToken *)calloc(num_cmds + 1, sizeof(SMTPToken)); if (cmds == NULL) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for SMTP " "command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } cmd_search = (SMTPSearch *)calloc(num_cmds, sizeof(SMTPSearch)); if (cmd_search == NULL) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for SMTP " "command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } cmd_config = (SMTPCmdConfig *)calloc(num_cmds, sizeof(SMTPCmdConfig)); if (cmd_config == NULL) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for SMTP " "command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } /* copy existing commands into newly allocated memory * don't need to copy anything from cmd_search since this hasn't been initialized yet */ ret = SafeMemcpy(cmds, _smtp_cmds, id * sizeof(SMTPToken), cmds, cmds + num_cmds); if (ret != SAFEMEM_SUCCESS) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to memory copy SMTP command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } ret = SafeMemcpy(cmd_config, _smtp_cmd_config, id * sizeof(SMTPCmdConfig), cmd_config, cmd_config + num_cmds); if (ret != SAFEMEM_SUCCESS) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to memory copy SMTP command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } /* add new command to cmds * cmd_config doesn't need anything added - this will probably be done by a calling function * cmd_search will be initialized when the searches are initialized */ tmp_cmds = &cmds[id]; tmp_cmds->name = strdup(name); tmp_cmds->name_len = strlen(name); tmp_cmds->search_id = id; if (tmp_cmds->name == NULL) { DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for SMTP " "command structure\n", *(_dpd.config_file), *(_dpd.config_line)); } /* free global memory structures */ if (_smtp_cmds != NULL) free(_smtp_cmds); if (_smtp_cmd_search != NULL) free(_smtp_cmd_search); if (_smtp_cmd_config != NULL) free(_smtp_cmd_config); /* set globals to new memory */ _smtp_cmds = cmds; _smtp_cmd_search = cmd_search; _smtp_cmd_config = cmd_config; ret = id; id++; num_cmds++; return ret;}/*** NAME** ProcessAltMaxCmdLen::*//****** alt_max_command_line_len <int> { <cmd> [<cmd>] }**** @param ErrorString error string buffer** @param ErrStrLen the length of the error string buffer**** @return an error code integer ** (0 = success, >0 = non-fatal error, <0 = fatal error)**** @retval 0 successs** @retval -1 generic fatal error*/static int ProcessAltMaxCmdLen(char *ErrorString, int ErrStrLen){ char *pcToken; char *pcLen; char *pcLenEnd; int iEndCmds = 0; int id; int cmd_len; /* Find number */ pcLen = strtok(NULL, CONF_SEPARATORS); if (!pcLen) { snprintf(ErrorString, ErrStrLen, "Invalid format for alt_max_command_line_len."); return -1; } pcToken = strtok(NULL, CONF_SEPARATORS); if (!pcToken) { snprintf(ErrorString, ErrStrLen, "Invalid format for alt_max_command_line_len."); return -1; } cmd_len = strtoul(pcLen, &pcLenEnd, 10); if (pcLenEnd == pcLen) { snprintf(ErrorString, ErrStrLen, "Invalid format for alt_max_command_line_len (non-numeric)."); return -1; } if (strcmp(CONF_START_LIST, pcToken)) { snprintf(ErrorString, ErrStrLen, "Must start alt_max_command_line_len list with the '%s' token.", CONF_START_LIST); return -1; } while ((pcToken = strtok(NULL, CONF_SEPARATORS)) != NULL) { if (strcmp(CONF_END_LIST, pcToken) == 0) { iEndCmds = 1; break; } id = GetCmdId(pcToken); _smtp_cmd_config[id].max_line_len = cmd_len; } if (!iEndCmds) { snprintf(ErrorString, ErrStrLen, "Must end alt_max_command_line_len configuration with '%s'.", CONF_END_LIST); return -1; } return 0;}/*** NAME** ProcessXlink2State::*//****** xlink2state { <enable/disable> <drop> }**** @param ErrorString error string buffer** @param ErrStrLen the length of the error string buffer**** @return an error code integer ** (0 = success, >0 = non-fatal error, <0 = fatal error)**** @retval 0 successs** @retval -1 generic fatal error*/static int ProcessXlink2State(char *ErrorString, int ErrStrLen){ char *pcToken; int iEnd = 0; pcToken = strtok(NULL, CONF_SEPARATORS); if(!pcToken) { snprintf(ErrorString, ErrStrLen, "Invalid xlink2state argument format."); return -1; } if(strcmp(CONF_START_LIST, pcToken)) { snprintf(ErrorString, ErrStrLen, "Must start xlink2state arguments with the '%s' token.", CONF_START_LIST); return -1; } while ((pcToken = strtok(NULL, CONF_SEPARATORS)) != NULL) { if(!strcmp(CONF_END_LIST, pcToken)) { iEnd = 1; break; } if ( !strcasecmp(CONF_DISABLE, pcToken) ) { _smtp_config.alert_xlink2state = 0; _smtp_config.ports[XLINK2STATE_DEFAULT_PORT / 8] &= ~(1 << (XLINK2STATE_DEFAULT_PORT % 8)); } else if ( !strcasecmp(CONF_ENABLE, pcToken) ) { _smtp_config.alert_xlink2state = 1; _smtp_config.ports[XLINK2STATE_DEFAULT_PORT / 8] |= 1 << (XLINK2STATE_DEFAULT_PORT % 8); } else if ( !strcasecmp(CONF_INLINE_DROP, pcToken) ) { if (!_smtp_config.alert_xlink2state) { snprintf(ErrorString, ErrStrLen, "Alerting on X-LINK2STATE must be enabled to drop."); return -1; } if (_dpd.inlineMode()) { _smtp_config.drop_xlink2state = 1; } else { snprintf(ErrorString, ErrStrLen, "Cannot use 'drop' keyword in X-LINK2STATE config " "if Snort is not in inline mode."); return -1; } } } if(!iEnd) { snprintf(ErrorString, ErrStrLen, "Must end '%s' configuration with '%s'.", CONF_XLINK2STATE, CONF_END_LIST); return -1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -