Skip to content

Commit

Permalink
mysql: fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
QianKaiLin committed Oct 26, 2024
1 parent d822dee commit 46feb19
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 65 deletions.
2 changes: 1 addition & 1 deletion doc/userguide/output/eve/eve-json-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3059,7 +3059,7 @@ Example of ARP logging: request and response
}

Event type: MySQL
---------------
-----------------

Fields
~~~~~~
Expand Down
119 changes: 83 additions & 36 deletions rust/src/mysql/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,13 @@ impl MysqlState {
}

fn new_tx(&mut self, command: String) -> MysqlTransaction {
let mut tx = MysqlTransaction::new(self.version.clone().unwrap());
let mut tx = MysqlTransaction::new(self.version.clone().unwrap_or_default());
self.tx_id += 1;
tx.tx_id = self.tx_id;
tx.tls = self.tls;
if tx.tls {
tx.complete = true;
}
tx.command = Some(command);
SCLogDebug!("Creating new transaction.tx_id: {}", tx.tx_id);
if self.transactions.len() > unsafe { MYSQL_MAX_TX } + self.tx_index_completed {
Expand All @@ -326,9 +329,6 @@ impl MysqlState {
}
self.tx_index_completed = index;
}
if tx.tls {
tx.complete = true;
}
tx
}

Expand Down Expand Up @@ -528,7 +528,7 @@ impl MysqlState {

// If there was gap, check we can sync up again.
if self.request_gap {
if !probe(i).is_ok() {
if probe(i).is_err() {
SCLogDebug!("Suricata interprets there's a gap in the request");
return AppLayerResult::ok();
}
Expand Down Expand Up @@ -612,14 +612,22 @@ impl MysqlState {
Some(MysqlStateProgress::LocalFileRequestReceived)
}
MysqlResponsePacket::FieldsList { columns: _ } => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Some(MysqlStateProgress::FieldListResponseReceived)
}
MysqlResponsePacket::Statistics => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Expand All @@ -630,22 +638,34 @@ impl MysqlState {
MysqlResponsePacket::Err { .. } => match self.state_progress {
MysqlStateProgress::CommandReceived
| MysqlStateProgress::TextResulsetContinue => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Some(MysqlStateProgress::CommandResponseReceived)
}
MysqlStateProgress::FieldListReceived => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Some(MysqlStateProgress::FieldListResponseReceived)
}
MysqlStateProgress::StmtExecReceived
| MysqlStateProgress::StmtExecResponseContinue => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Expand All @@ -655,15 +675,23 @@ impl MysqlState {
Some(MysqlStateProgress::StmtResetResponseReceived)
}
MysqlStateProgress::ChangeUserReceived => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Some(MysqlStateProgress::Finished)
}
MysqlStateProgress::StmtFetchReceived
| MysqlStateProgress::StmtFetchResponseContinue => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}
Expand All @@ -678,16 +706,23 @@ impl MysqlState {
} => match self.state_progress {
MysqlStateProgress::Auth => Some(MysqlStateProgress::AuthFinished),
MysqlStateProgress::CommandReceived => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.affected_rows = Some(rows);
tx.complete = true;
}

Some(MysqlStateProgress::CommandResponseReceived)
}
MysqlStateProgress::StmtExecReceived => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.affected_rows = Some(rows);
tx.complete = true;
Expand All @@ -701,34 +736,38 @@ impl MysqlState {
Some(MysqlStateProgress::StmtResetResponseReceived)
}
MysqlStateProgress::TextResulsetContinue => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.complete = true;
}

Some(MysqlStateProgress::CommandResponseReceived)
}
MysqlStateProgress::StmtExecResponseContinue => {
let prepare_stmt = self.prepare_stmt.take();
let tx = self.get_tx_mut(self.tx_id - 1);
if let Some(tx) = tx {
if let Some(mut prepare_stmt) = prepare_stmt {
let rows = prepare_stmt.rows.take();
if let Some(rows) = rows {
tx.rows = Some(
rows.into_iter()
.map(|row| match row {
MysqlResultBinarySetRow::Err => String::new(),
MysqlResultBinarySetRow::Text(text) => text,
})
.collect::<Vec<String>>(),
);
if self.tx_id > 0 {
let tx = self.get_tx_mut(self.tx_id - 1);
if let Some(tx) = tx {
if let Some(mut prepare_stmt) = prepare_stmt {
let rows = prepare_stmt.rows.take();
if let Some(rows) = rows {
tx.rows = Some(
rows.into_iter()
.map(|row| match row {
MysqlResultBinarySetRow::Err => String::new(),
MysqlResultBinarySetRow::Text(text) => text,
})
.collect::<Vec<String>>(),
);
}

tx.complete = true;
}

tx.complete = true;
}
}

Some(MysqlStateProgress::StmtExecResponseReceived)
}
MysqlStateProgress::StmtFetchResponseContinue => {
Expand All @@ -742,7 +781,11 @@ impl MysqlState {
eof,
rows,
} => {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if !rows.is_empty() {
let mut rows = rows.into_iter().map(|row| row.texts.join(",")).collect();
if let Some(tx) = tx {
Expand Down Expand Up @@ -798,7 +841,11 @@ impl MysqlState {

if !rows.is_empty() {
if eof.status_flags != 0x0A {
let tx = self.get_tx_mut(self.tx_id - 1);
let tx = if self.tx_id > 0 {
self.get_tx_mut(self.tx_id - 1)
} else {
None
};
if let Some(tx) = tx {
tx.rows = Some(
rows.into_iter()
Expand Down Expand Up @@ -894,7 +941,7 @@ impl MysqlState {
}

if self.response_gap {
if !probe(i).is_ok() {
if probe(i).is_err() {
SCLogDebug!("Suricata interprets there's a gap in the response");
return AppLayerResult::ok();
}
Expand Down
39 changes: 13 additions & 26 deletions rust/src/mysql/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ pub enum MysqlCommand {
},
}

impl MysqlCommand {
pub fn to_string(self) -> String {
impl std::fmt::Display for MysqlCommand {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
MysqlCommand::Quit => "quit".to_string(),
MysqlCommand::Query { query } => query,
MysqlCommand::Ping => "ping".to_string(),
_ => String::new(),
MysqlCommand::Quit => write!(f, "quit"),
MysqlCommand::Query { query } => write!(f, "{}", query),
MysqlCommand::Ping => write!(f, "ping"),
_ => write!(f, ""),
}
}
}
Expand Down Expand Up @@ -744,15 +744,12 @@ fn parse_stmt_execute_cmd(
let param_types = if let Some(new_param_types) = new_param_types {
Some(new_param_types)
} else {
match param_types {
Some(param_types) => Some(
param_types
.iter()
.map(|param_type| (param_type.field_type, param_type.flags != 0))
.collect(),
),
None => None,
}
param_types.map(|param_types| {
param_types
.iter()
.map(|param_type| (param_type.field_type, param_type.flags != 0))
.collect()
})
};

let consumed = old.len() - i.len();
Expand Down Expand Up @@ -1096,7 +1093,7 @@ fn parse_resultset_row_texts(i: &[u8]) -> IResult<&[u8], Vec<String>> {
length -= consumed;
}

Ok((rem, texts))
Ok((&[], texts))
}

fn parse_resultset_row(i: &[u8]) -> IResult<&[u8], MysqlResultSetRow> {
Expand Down Expand Up @@ -2125,16 +2122,6 @@ mod test {

use super::*;

#[test]
fn test_parse_packet_header() {
let pkt: &[u8] = &[0x07, 0x00, 0x00, 0x03];
let (rem, packet_header) = parse_packet_header(pkt).unwrap();

assert!(rem.is_empty());
assert_eq!(packet_header.pkt_len, 7);
assert_eq!(packet_header.pkt_num, 3);
}

#[test]
fn test_parse_handshake_request() {
let pkt: &[u8] = &[
Expand Down
3 changes: 1 addition & 2 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,7 @@ void OutputRegisterRootLoggers(void)
// underscore instead of dash for bittorrent_dht
RegisterSimpleJsonApplayerLogger(
ALPROTO_BITTORRENT_DHT, rs_bittorrent_dht_logger_log, "bittorrent_dht");
RegisterSimpleJsonApplayerLogger(
ALPROTO_MYSQL, SCMysqlLogger, "mysql");
RegisterSimpleJsonApplayerLogger(ALPROTO_MYSQL, SCMysqlLogger, "mysql");

OutputPacketLoggerRegister();
OutputFiledataLoggerRegister();
Expand Down

0 comments on commit 46feb19

Please sign in to comment.