-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ftp: begin conversion process to Rust #12657
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* Copyright (C) 2025 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
// FTP state progress values | ||
#[repr(u8)] | ||
#[allow(non_camel_case_types)] | ||
pub enum FtpStateValues { | ||
FTP_STATE_NONE, | ||
FTP_STATE_IN_PROGRESS, | ||
FTP_STATE_PORT_DONE, | ||
FTP_STATE_FINISHED, | ||
} | ||
// FTP Data progress values | ||
#[repr(u8)] | ||
#[allow(non_camel_case_types)] | ||
pub enum FtpDataStateValues { | ||
FTPDATA_STATE_IN_PROGRESS = 1, | ||
FTPDATA_STATE_FINISHED = 2, | ||
} | ||
|
||
// FTP request command values | ||
#[repr(u8)] | ||
#[allow(non_camel_case_types)] | ||
#[derive(Clone, Copy)] | ||
pub enum FtpRequestCommand { | ||
FTP_COMMAND_UNKNOWN, | ||
FTP_COMMAND_ABOR, | ||
FTP_COMMAND_ACCT, | ||
FTP_COMMAND_ALLO, | ||
FTP_COMMAND_APPE, | ||
FTP_COMMAND_AUTH_TLS, | ||
FTP_COMMAND_CDUP, | ||
FTP_COMMAND_CHMOD, | ||
FTP_COMMAND_CWD, | ||
FTP_COMMAND_DELE, | ||
FTP_COMMAND_EPSV, | ||
FTP_COMMAND_HELP, | ||
FTP_COMMAND_IDLE, | ||
FTP_COMMAND_LIST, | ||
FTP_COMMAND_MAIL, | ||
FTP_COMMAND_MDTM, | ||
FTP_COMMAND_MKD, | ||
FTP_COMMAND_MLFL, | ||
FTP_COMMAND_MODE, | ||
FTP_COMMAND_MRCP, | ||
FTP_COMMAND_MRSQ, | ||
FTP_COMMAND_MSAM, | ||
FTP_COMMAND_MSND, | ||
FTP_COMMAND_MSOM, | ||
FTP_COMMAND_NLST, | ||
FTP_COMMAND_NOOP, | ||
FTP_COMMAND_PASS, | ||
FTP_COMMAND_PASV, | ||
FTP_COMMAND_PORT, | ||
FTP_COMMAND_PWD, | ||
FTP_COMMAND_QUIT, | ||
FTP_COMMAND_REIN, | ||
FTP_COMMAND_REST, | ||
FTP_COMMAND_RETR, | ||
FTP_COMMAND_RMD, | ||
FTP_COMMAND_RNFR, | ||
FTP_COMMAND_RNTO, | ||
FTP_COMMAND_SITE, | ||
FTP_COMMAND_SIZE, | ||
FTP_COMMAND_SMNT, | ||
FTP_COMMAND_STAT, | ||
FTP_COMMAND_STOR, | ||
FTP_COMMAND_STOU, | ||
FTP_COMMAND_STRU, | ||
FTP_COMMAND_SYST, | ||
FTP_COMMAND_TYPE, | ||
FTP_COMMAND_UMASK, | ||
FTP_COMMAND_USER, | ||
FTP_COMMAND_EPRT, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/* Copyright (C) 2025 Open Information Security Foundation | ||
* | ||
* You can copy, redistribute or modify this Program under the terms of | ||
* the GNU General Public License version 2 as published by the Free | ||
* Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* version 2 along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
*/ | ||
|
||
use std; | ||
use std::ffi::CString; | ||
use std::os::raw::{c_char, c_int, c_void}; | ||
|
||
use crate::conf::{conf_get, get_memval}; | ||
use crate::ftp::constant::*; | ||
use lazy_static::lazy_static; | ||
|
||
/// cbindgen:ignore | ||
#[repr(C)] | ||
pub struct FtpCommand { | ||
command_name: CString, | ||
command: FtpRequestCommand, | ||
command_length: u8, | ||
} | ||
|
||
impl FtpCommand { | ||
fn new(command_name: &str, command: FtpRequestCommand) -> FtpCommand { | ||
let cstring = CString::new(command_name).unwrap(); | ||
let length = cstring.as_bytes().len(); | ||
FtpCommand { | ||
command_name: cstring, | ||
command, | ||
command_length: length as u8, | ||
} | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref FTP_COMMANDS: Vec<FtpCommand> = vec![ | ||
FtpCommand::new("PORT", FtpRequestCommand::FTP_COMMAND_PORT), | ||
FtpCommand::new("EPRT", FtpRequestCommand::FTP_COMMAND_EPRT), | ||
FtpCommand::new("AUTH_TLS", FtpRequestCommand::FTP_COMMAND_AUTH_TLS), | ||
FtpCommand::new("PASV", FtpRequestCommand::FTP_COMMAND_PASV), | ||
FtpCommand::new("EPSV", FtpRequestCommand::FTP_COMMAND_EPSV), | ||
FtpCommand::new("RETR", FtpRequestCommand::FTP_COMMAND_RETR), | ||
FtpCommand::new("STOR", FtpRequestCommand::FTP_COMMAND_STOR), | ||
FtpCommand::new("ABOR", FtpRequestCommand::FTP_COMMAND_ABOR), | ||
FtpCommand::new("ACCT", FtpRequestCommand::FTP_COMMAND_ACCT), | ||
FtpCommand::new("ALLO", FtpRequestCommand::FTP_COMMAND_ALLO), | ||
FtpCommand::new("APPE", FtpRequestCommand::FTP_COMMAND_APPE), | ||
FtpCommand::new("CDUP", FtpRequestCommand::FTP_COMMAND_CDUP), | ||
FtpCommand::new("CHMOD", FtpRequestCommand::FTP_COMMAND_CHMOD), | ||
FtpCommand::new("CWD", FtpRequestCommand::FTP_COMMAND_CWD), | ||
FtpCommand::new("DELE", FtpRequestCommand::FTP_COMMAND_DELE), | ||
FtpCommand::new("HELP", FtpRequestCommand::FTP_COMMAND_HELP), | ||
FtpCommand::new("IDLE", FtpRequestCommand::FTP_COMMAND_IDLE), | ||
FtpCommand::new("LIST", FtpRequestCommand::FTP_COMMAND_LIST), | ||
FtpCommand::new("MAIL", FtpRequestCommand::FTP_COMMAND_MAIL), | ||
FtpCommand::new("MDTM", FtpRequestCommand::FTP_COMMAND_MDTM), | ||
FtpCommand::new("MKD", FtpRequestCommand::FTP_COMMAND_MKD), | ||
FtpCommand::new("MLFL", FtpRequestCommand::FTP_COMMAND_MLFL), | ||
FtpCommand::new("MODE", FtpRequestCommand::FTP_COMMAND_MODE), | ||
FtpCommand::new("MRCP", FtpRequestCommand::FTP_COMMAND_MRCP), | ||
FtpCommand::new("MRSQ", FtpRequestCommand::FTP_COMMAND_MRSQ), | ||
FtpCommand::new("MSAM", FtpRequestCommand::FTP_COMMAND_MSAM), | ||
FtpCommand::new("MSND", FtpRequestCommand::FTP_COMMAND_MSND), | ||
FtpCommand::new("MSOM", FtpRequestCommand::FTP_COMMAND_MSOM), | ||
FtpCommand::new("NLST", FtpRequestCommand::FTP_COMMAND_NLST), | ||
FtpCommand::new("NOOP", FtpRequestCommand::FTP_COMMAND_NOOP), | ||
FtpCommand::new("PASS", FtpRequestCommand::FTP_COMMAND_PASS), | ||
FtpCommand::new("PWD", FtpRequestCommand::FTP_COMMAND_PWD), | ||
FtpCommand::new("QUIT", FtpRequestCommand::FTP_COMMAND_QUIT), | ||
FtpCommand::new("REIN", FtpRequestCommand::FTP_COMMAND_REIN), | ||
FtpCommand::new("REST", FtpRequestCommand::FTP_COMMAND_REST), | ||
FtpCommand::new("RMD", FtpRequestCommand::FTP_COMMAND_RMD), | ||
FtpCommand::new("RNFR", FtpRequestCommand::FTP_COMMAND_RNFR), | ||
FtpCommand::new("RNTO", FtpRequestCommand::FTP_COMMAND_RNTO), | ||
FtpCommand::new("SITE", FtpRequestCommand::FTP_COMMAND_SITE), | ||
FtpCommand::new("SIZE", FtpRequestCommand::FTP_COMMAND_SIZE), | ||
FtpCommand::new("SMNT", FtpRequestCommand::FTP_COMMAND_SMNT), | ||
FtpCommand::new("STAT", FtpRequestCommand::FTP_COMMAND_STAT), | ||
FtpCommand::new("STOU", FtpRequestCommand::FTP_COMMAND_STOU), | ||
FtpCommand::new("STRU", FtpRequestCommand::FTP_COMMAND_STRU), | ||
FtpCommand::new("SYST", FtpRequestCommand::FTP_COMMAND_SYST), | ||
FtpCommand::new("TYPE", FtpRequestCommand::FTP_COMMAND_TYPE), | ||
FtpCommand::new("UMASK", FtpRequestCommand::FTP_COMMAND_UMASK), | ||
FtpCommand::new("USER", FtpRequestCommand::FTP_COMMAND_USER), | ||
FtpCommand::new("UNKNOWN", FtpRequestCommand::FTP_COMMAND_UNKNOWN), | ||
]; | ||
} | ||
|
||
/// cbindgen:ignore | ||
extern "C" { | ||
pub fn MpmAddPatternCI( | ||
ctx: *const c_void, pat: *const libc::c_char, pat_len: c_int, _offset: c_int, | ||
_depth: c_int, id: c_int, rule_id: c_int, _flags: c_int, | ||
) -> c_void; | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn SCGetFtpCommandInfo( | ||
index: usize, name_ptr: *mut *const c_char, code_ptr: *mut u8, len_ptr: *mut u8, | ||
) -> bool { | ||
if index <= FTP_COMMANDS.len() { | ||
unsafe { | ||
if !name_ptr.is_null() { | ||
*name_ptr = FTP_COMMANDS[index].command_name.as_ptr(); | ||
} | ||
if !code_ptr.is_null() { | ||
*code_ptr = FTP_COMMANDS[index].command as u8; | ||
} | ||
if !len_ptr.is_null() { | ||
*len_ptr = FTP_COMMANDS[index].command_length; | ||
} | ||
} | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[no_mangle] | ||
pub unsafe extern "C" fn SCFTPSetMpmState(ctx: *const c_void) { | ||
for index in 0..FTP_COMMANDS.len() { | ||
let name_ptr = FTP_COMMANDS[index].command_name.as_ptr(); | ||
let len = FTP_COMMANDS[index].command_length; | ||
if len > 0 { | ||
MpmAddPatternCI( | ||
ctx, | ||
name_ptr, | ||
len as c_int, | ||
0, | ||
0, | ||
index as c_int, | ||
index as c_int, | ||
0, | ||
); | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
#[allow(dead_code)] | ||
pub struct FtpTransferCmd { | ||
// Must be first -- required by app-layer expectation logic | ||
data_free: unsafe extern "C" fn(*mut c_void), | ||
pub flow_id: u64, | ||
pub file_name: *mut u8, | ||
pub file_len: u16, | ||
pub direction: u8, | ||
pub cmd: u8, | ||
} | ||
|
||
impl Default for FtpTransferCmd { | ||
fn default() -> Self { | ||
FtpTransferCmd { | ||
flow_id: 0, | ||
file_name: std::ptr::null_mut(), | ||
file_len: 0, | ||
direction: 0, | ||
cmd: FtpStateValues::FTP_STATE_NONE as u8, | ||
data_free: default_free_fn, | ||
} | ||
} | ||
} | ||
|
||
unsafe extern "C" fn default_free_fn(_ptr: *mut c_void) {} | ||
impl FtpTransferCmd { | ||
pub fn new() -> Self { | ||
FtpTransferCmd { | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn SCFTPGetConfigValues( | ||
memcap: *mut u64, max_tx: *mut u32, max_line_len: *mut u32, | ||
) { | ||
if let Some(val) = conf_get("app-layer.protocols.ftp.memcap") { | ||
if let Ok(v) = get_memval(val) { | ||
*memcap = v; | ||
SCLogConfig!("FTP memcap: {}", v); | ||
} else { | ||
SCLogWarning!( | ||
"Invalid value {} for ftp.memcap; defaulting to {}", | ||
val, | ||
*memcap | ||
); | ||
} | ||
} | ||
if let Some(val) = conf_get("app-layer.protocols.ftp.max-tx") { | ||
if let Ok(v) = val.parse::<u32>() { | ||
*max_tx = v; | ||
SCLogConfig!("FTP max tx: {}", v); | ||
} else { | ||
SCLogWarning!( | ||
"Invalid value {} for ftp.max-tx; defaulting to {}", | ||
val, | ||
*max_tx | ||
); | ||
} | ||
} | ||
// This value is often expressed with a unit suffix, e.g., 5kb, hence get_memval | ||
if let Some(val) = conf_get("app-layer.protocols.ftp.max-line-length") { | ||
if let Ok(v) = get_memval(val) { | ||
*max_line_len = v as u32; | ||
SCLogConfig!("FTP max line length: {}", v); | ||
} else { | ||
SCLogWarning!( | ||
"Invalid value {} for ftp.max-line-length; defaulting to {}", | ||
val, | ||
*max_line_len | ||
); | ||
} | ||
} | ||
} | ||
|
||
/// Returns *mut FtpTransferCmd | ||
#[no_mangle] | ||
pub unsafe extern "C" fn SCFTPTransferCmdNew() -> *mut FtpTransferCmd { | ||
SCLogDebug!("allocating ftp transfer cmd"); | ||
let cmd = FtpTransferCmd::new(); | ||
Box::into_raw(Box::new(cmd)) | ||
} | ||
|
||
/// Params: | ||
/// - transfer command: *mut FTPTransferCmd as void pointer | ||
#[no_mangle] | ||
pub unsafe extern "C" fn SCFTPTransferCmdFree(cmd: *mut FtpTransferCmd) { | ||
SCLogDebug!("freeing ftp transfer cmd"); | ||
if !cmd.is_null() { | ||
let _transfer_cmd = Box::from_raw(cmd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should live in another file like rust/src/core.rs ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jeff can you track this for post-merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes