📄 snort_ftptelnet.c
字号:
}/* * Function: ProcessDateFormat(FTP_DATE_FMT *dateFmt, * FTP_DATE_FMT *LastNonOptFmt, * char **format) * * Purpose: Sets the value for nodes in the FTP Date validation tree. * * Arguments: dateFmt => pointer to an FTP date validation node * LastNonOptFmt => pointer to previous FTP date validation node * format => pointer to next part of date validation string * Updated on function exit. * * Returns: int => an error code integer (0 = success, * >0 = non-fatal error, <0 = fatal error) * */static int ProcessDateFormat(FTP_DATE_FMT *dateFmt, FTP_DATE_FMT *LastNonOptFmt, char **format){ char *curr_format; int iRet = FTPP_SUCCESS; int curr_len = 0; char *curr_ch; char *start_ch; FTP_DATE_FMT *CurrFmt = dateFmt; if (!dateFmt) return FTPP_INVALID_ARG; if (!format || !*format) return FTPP_INVALID_ARG; start_ch = curr_ch = *format; while (*curr_ch != '\0') { switch (*curr_ch) { case 'n': case 'C': case '+': case '-': case '.': curr_len++; curr_ch++; break; case '[': curr_ch++; if (curr_len > 0) { FTP_DATE_FMT *OptFmt; OptFmt = (FTP_DATE_FMT *)malloc(sizeof(FTP_DATE_FMT)); memset(OptFmt, 0, sizeof(FTP_DATE_FMT)); curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; curr_len = 0; CurrFmt->optional = OptFmt; OptFmt->prev = CurrFmt; iRet = ProcessDateFormat(OptFmt, CurrFmt, &curr_ch); if (iRet != FTPP_SUCCESS) { return iRet; } } start_ch = curr_ch; break; case ']': curr_ch++; if (curr_len > 0) { curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; curr_len = 0; } *format = curr_ch; return FTPP_SUCCESS; break; case '{': curr_ch++; { FTP_DATE_FMT *NewFmt; NewFmt = (FTP_DATE_FMT *)malloc(sizeof(FTP_DATE_FMT)); memset(NewFmt, 0, sizeof(FTP_DATE_FMT)); if (curr_len > 0) { curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; curr_len = 0; } else { CurrFmt->empty = 1; } NewFmt->prev = LastNonOptFmt; CurrFmt->next_a = NewFmt; iRet = ProcessDateFormat(NewFmt, CurrFmt, &curr_ch); if (iRet != FTPP_SUCCESS) { return iRet; } NewFmt = (FTP_DATE_FMT *)malloc(sizeof(FTP_DATE_FMT)); memset(NewFmt, 0, sizeof(FTP_DATE_FMT)); NewFmt->prev = LastNonOptFmt; CurrFmt->next_b = NewFmt; iRet = ProcessDateFormat(NewFmt, CurrFmt, &curr_ch); if (iRet != FTPP_SUCCESS) { return iRet; } if (curr_ch != NULL) { NewFmt = (FTP_DATE_FMT *)malloc(sizeof(FTP_DATE_FMT)); memset(NewFmt, 0, sizeof(FTP_DATE_FMT)); NewFmt->prev = CurrFmt; CurrFmt->next = NewFmt; iRet = ProcessDateFormat(NewFmt, CurrFmt, &curr_ch); if (iRet != FTPP_SUCCESS) { return iRet; } } } break; case '}': curr_ch++; if (curr_len > 0) { curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; curr_len = 0; *format = curr_ch; return FTPP_SUCCESS; } else { CurrFmt->empty = 1; *format = curr_ch; return FTPP_SUCCESS; } break; case '|': curr_ch++; if (curr_len > 0) { curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; curr_len = 0; *format = curr_ch; return FTPP_SUCCESS; } else { CurrFmt->empty = 1; *format = curr_ch; return FTPP_SUCCESS; } break; default: /* Uh, shouldn't get this. */ return FTPP_INVALID_ARG; break; } } if (curr_len > 0) { curr_format = (char *)malloc((curr_len+1) * sizeof(char)); memset(curr_format, 0, curr_len+1); strncpy(curr_format, start_ch, curr_len); CurrFmt->format_string = curr_format; start_ch = curr_ch; curr_len = 0; } /* Should've closed all options & ORs */ *format = curr_ch; return FTPP_SUCCESS;}/* * Function: DoNextFormat(FTP_PARAM_FMT *ThisFmt, int allocated, * char *ErrorString, int ErrStrLen) * * Purpose: Processes the next FTP parameter validation node. * * Arguments: ThisFmt => pointer to an FTP parameter validation node * allocated => indicator whether the next node is allocated * ErrorString => error string buffer * ErrStrLen => the length of the error string buffer * * Returns: int => an error code integer (0 = success, * >0 = non-fatal error, <0 = fatal error) * */int DoNextFormat(FTP_PARAM_FMT *ThisFmt, int allocated, char *ErrorString, int ErrStrLen){ FTP_PARAM_FMT *NextFmt; int iRet = FTPP_SUCCESS; char *fmt = NextToken( CONF_SEPARATORS); if (!fmt) return FTPP_INVALID_ARG; if(!strcmp(END_CMD_FORMAT, fmt)) { return FTPP_SUCCESS; } if (!strcmp(fmt, OR_FMT)) { return FTPP_OR_FOUND; } if (!strcmp(fmt, END_OPT_FMT)) { return FTPP_OPT_END_FOUND; } if (!strcmp(fmt, END_CHOICE_FMT)) { return FTPP_CHOICE_END_FOUND; } if (!strcmp(fmt, START_OPT_FMT)) { NextFmt = (FTP_PARAM_FMT *)malloc(sizeof(FTP_PARAM_FMT)); memset(NextFmt, 0, sizeof(FTP_PARAM_FMT)); ThisFmt->optional_fmt = NextFmt; NextFmt->optional = 1; NextFmt->prev_param_fmt = ThisFmt; if (ThisFmt->optional) NextFmt->prev_optional = 1; iRet = DoNextFormat(NextFmt, 1, ErrorString, ErrStrLen); if (iRet != FTPP_OPT_END_FOUND) { return FTPP_INVALID_ARG; } return DoNextFormat(ThisFmt, 0, ErrorString, ErrStrLen); } if (!strcmp(fmt, START_CHOICE_FMT)) { int numChoices = 1; do { FTP_PARAM_FMT **tmpChoices = (FTP_PARAM_FMT **)malloc( sizeof(FTP_PARAM_FMT*) * numChoices); if (ThisFmt->numChoices) { memcpy(tmpChoices, ThisFmt->choices, sizeof(FTP_PARAM_FMT*) * ThisFmt->numChoices); } NextFmt = (FTP_PARAM_FMT *)malloc(sizeof(FTP_PARAM_FMT)); memset(NextFmt, 0, sizeof(FTP_PARAM_FMT)); ThisFmt->numChoices = numChoices; tmpChoices[numChoices-1] = NextFmt; if (ThisFmt->choices) free(ThisFmt->choices); ThisFmt->choices = tmpChoices; NextFmt->prev_param_fmt = ThisFmt; iRet = DoNextFormat(NextFmt, 1, ErrorString, ErrStrLen); numChoices++; } while (iRet == FTPP_OR_FOUND); if (iRet != FTPP_CHOICE_END_FOUND) { return FTPP_INVALID_ARG; } return DoNextFormat(ThisFmt, 0, ErrorString, ErrStrLen); } if (!allocated) { NextFmt = (FTP_PARAM_FMT *)malloc(sizeof(FTP_PARAM_FMT)); memset(NextFmt, 0, sizeof(FTP_PARAM_FMT)); NextFmt->prev_param_fmt = ThisFmt; ThisFmt->next_param_fmt = NextFmt; if (ThisFmt->optional) NextFmt->prev_optional = 1; } else { NextFmt = ThisFmt; } /* If its not an end cmd, OR, START/END Opt... * it must be a parameter specification. */ /* Setup the type & format specs */ if (!strcmp(fmt, F_INT)) { NextFmt->type = e_int; } else if (!strcmp(fmt, F_NUMBER)) { NextFmt->type = e_number; } else if (!strcmp(fmt, F_CHAR)) { char *chars_allowed = NextToken( CONF_SEPARATORS); NextFmt->type = e_char; NextFmt->format.chars_allowed = 0; while (*chars_allowed != 0) { int bitNum = (*chars_allowed & 0x1f); NextFmt->format.chars_allowed |= (1 << (bitNum-1)); chars_allowed++; } } else if (!strcmp(fmt, F_DATE)) { FTP_DATE_FMT *DateFmt; char *format = NextToken( CONF_SEPARATORS); NextFmt->type = e_date; DateFmt = (FTP_DATE_FMT *)malloc(sizeof(FTP_DATE_FMT)); memset(DateFmt, 0, sizeof(FTP_DATE_FMT)); NextFmt->format.date_fmt = DateFmt; if ((iRet = ProcessDateFormat(DateFmt, NULL, &format))) { snprintf(ErrorString, ErrStrLen, "Illegal format %s for token '%s'.", format, CMD_VALIDITY); return FTPP_INVALID_ARG; } } else if (!strcmp(fmt, F_STRING)) { NextFmt->type = e_unrestricted; } else if (!strcmp(fmt, F_HOST_PORT)) { NextFmt->type = e_host_port; } else { snprintf(ErrorString, ErrStrLen, "Illegal format type %s for token '%s'.", fmt, CMD_VALIDITY); return FTPP_INVALID_ARG; } return DoNextFormat(NextFmt, 0, ErrorString, ErrStrLen);}/* * Function: ProcessFTPCmdValidity(FTP_SERVER_PROTO_CONF *ServerConf, * char *ErrorString, int ErrStrLen) * * Purpose: Process the ftp cmd validity configuration. * This sets the FTP command parameter validation tree. * * Arguments: ServerConf => pointer to the FTP server configuration * confOption => pointer to the name of the option * ErrorString => error string buffer * ErrStrLen => the length of the error string buffer * * Returns: int => an error code integer (0 = success, * >0 = non-fatal error, <0 = fatal error) * */static int ProcessFTPCmdValidity(FTP_SERVER_PROTO_CONF *ServerConf, char *ErrorString, int ErrStrLen){ FTP_CMD_CONF *FTPCmd = NULL; FTP_PARAM_FMT *HeadFmt = NULL; char *cmd; char *fmt; int iRet; fmt = NextToken( CONF_SEPARATORS); if(fmt == NULL) { snprintf(ErrorString, ErrStrLen, "No argument to token '%s'.", CMD_VALIDITY); return FTPP_FATAL_ERR; } cmd = fmt; if (strlen(cmd) > 4) { snprintf(ErrorString, ErrStrLen, "FTP Commands are no longer than 4 characters: '%s'.", cmd); return FTPP_FATAL_ERR; } fmt = NextToken( CONF_SEPARATORS); if(!fmt) { snprintf(ErrorString, ErrStrLen, "Invalid cmd validity format."); return FTPP_FATAL_ERR; } if(strcmp(START_CMD_FORMAT, fmt)) { snprintf(ErrorString, ErrStrLen, "Must start a cmd validity with the '%s' token.", START_CMD_FORMAT); return FTPP_FATAL_ERR; } HeadFmt = (FTP_PARAM_FMT *)malloc(sizeof(FTP_PARAM_FMT)); memset(HeadFmt, 0, sizeof(FTP_PARAM_FMT)); HeadFmt->type = e_head; iRet = DoNextFormat(HeadFmt, 0, ErrorString, ErrStrLen); /* Need to check to be sure we got a complete command */ if (iRet) { return FTPP_FATAL_ERR; } SetOptionalsNext(HeadFmt, NULL, NULL, 0); FTPCmd = ftp_cmd_lookup_find(ServerConf->cmd_lookup, cmd, strlen(cmd), &iRet); if (FTPCmd == NULL) { /* Add it to the list */ FTPCmd = (FTP_CMD_CONF *)malloc(sizeof(FTP_CMD_CONF)); memset(FTPCmd, 0, sizeof(FTP_CMD_CONF)); strcpy(FTPCmd->cmd_name, cmd); FTPCmd->max_param_len = ServerConf->def_max_param_len; ftp_cmd_lookup_add(ServerConf->cmd_lookup, cmd, strlen(cmd), FTPCmd);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -