From fd025b68f92b468689343da4e96c5caa989cb0b4 Mon Sep 17 00:00:00 2001 From: cmac4603 Date: Sun, 22 Sep 2024 22:23:02 +0100 Subject: [PATCH 1/2] modify-psql-auth Description: - We now copy to another secondary internal buffer where we can change things - User can't be changed yet, need to change length of message etc. as well but this is working as expected recreating the current string - Really bloody happy, we're getting there, let's goooooo!!!!!!!!!! --- dam-shared/src/buffers/colinsbuffer.rs | 30 +++++++++--- dam-shared/src/messages/postgres.rs | 65 +++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/dam-shared/src/buffers/colinsbuffer.rs b/dam-shared/src/buffers/colinsbuffer.rs index 63697d0..96fd1c8 100644 --- a/dam-shared/src/buffers/colinsbuffer.rs +++ b/dam-shared/src/buffers/colinsbuffer.rs @@ -10,7 +10,9 @@ use tokio::{ net::TcpStream, }; -use crate::messages::PostgresMsg; +use crate::messages::{postgres::PostgresMessageType, PostgresMsg}; + +use super::DEFAULT_BUF_SIZE; #[derive(Debug)] pub(crate) struct ColinsBuffer { @@ -48,16 +50,32 @@ impl ColinsBuffer { let mut buf = ReadBuf::new(&mut me.buf); buf.set_filled(me.capacity); + let mut new_buf = vec![0; DEFAULT_BUF_SIZE]; + let mut int_buf = ReadBuf::new(&mut new_buf); + // read from the tcp stream into the buf - let res = reader.poll_read(cx, &mut buf); + let res = reader.poll_read(cx, &mut int_buf); if let Poll::Ready(Ok(())) = res { - let filled_len = buf.filled().len(); - // debug!("{} [{} bytes] - {:?}", me.name, filled_len, &buf.filled()); - debug!("{} [{} bytes]", me.name, filled_len); - let postgres_msg = PostgresMsg::try_from(buf.filled()); + // debug!("{} [{} bytes]", me.name, filled_len); + let postgres_msg = PostgresMsg::try_from(int_buf.filled()); if let Ok(m) = postgres_msg { debug!("{:?}", m.msg_type); + match m.msg_type { + PostgresMessageType::StartupMessage { + user: _, + database: _, + application_name: _, + client_encoding: _, + } => { + buf.put_slice(&m.changed_bytes.unwrap()); + } + _ => buf.put_slice(&m.bytes), + } + } else { + buf.put_slice(int_buf.filled()); }; + let filled_len = buf.filled().len(); + debug!("{} [{} bytes] - {:?}", me.name, filled_len, &buf.filled()); me.read_done = me.capacity == filled_len; me.capacity = filled_len; } diff --git a/dam-shared/src/messages/postgres.rs b/dam-shared/src/messages/postgres.rs index b04cea6..9e2e13c 100644 --- a/dam-shared/src/messages/postgres.rs +++ b/dam-shared/src/messages/postgres.rs @@ -5,11 +5,38 @@ use log::debug; pub struct PostgresMsg<'pm> { pub msg_type: PostgresMessageType, pub bytes: &'pm [u8], + pub changed_bytes: Option>, +} + +impl<'def> Default for PostgresMsg<'def> { + fn default() -> Self { + PostgresMsg { + msg_type: PostgresMessageType::NotRecognized, + bytes: &[], + changed_bytes: None, + } + } } impl<'m> PostgresMsg<'m> { fn new(msg_type: PostgresMessageType, bytes: &'m [u8]) -> PostgresMsg<'m> { - PostgresMsg { msg_type, bytes } + PostgresMsg { + msg_type, + bytes, + ..Default::default() + } + } + + fn new_transformed( + msg_type: PostgresMessageType, + bytes: &'m [u8], + changed_bytes: Vec, + ) -> PostgresMsg<'m> { + PostgresMsg { + msg_type, + bytes, + changed_bytes: Some(changed_bytes), + } } fn not_recognised<'nr>(bytes: &'nr [u8]) -> PostgresMsg<'nr> { @@ -19,11 +46,31 @@ impl<'m> PostgresMsg<'m> { /// First message from client to the server with user name / database fn startup_message(bytes: &'m [u8]) -> Result { let length_of_message = as_u32_be(&bytes[0..4]) as usize; - debug!( - "StartupMessage == {:?}", - String::from_utf8(bytes[8..length_of_message].to_vec()) + // user\0cmac4603\0database\0cmac4603\0application_name\0psql\0client_encoding\0UTF8\0\0 + let startup_message = String::from_utf8(bytes[8..length_of_message].to_vec()).unwrap(); // TODO: handle + let startup_elements: Vec<&str> = startup_message.split("\0").collect(); + let user = startup_elements[1].to_string(); + let database = startup_elements[3].to_string(); + let application_name = startup_elements[5].to_string(); + let client_encoding = startup_elements[7].to_string(); + let changed_bytes = format!( + "user\0{}\0database\0{}\0application_name\0{}\0client_encoding\0{}\0\0", + user, database, application_name, client_encoding ); - Ok(PostgresMsg::new(PostgresMessageType::StartupMessage, bytes)) + + let bytes_sent = changed_bytes.as_bytes().to_owned(); + let merged = [&bytes[0..8], &bytes_sent[..]].concat(); + + Ok(PostgresMsg::new_transformed( + PostgresMessageType::StartupMessage { + user: startup_elements[1].to_string(), + database: startup_elements[3].to_string(), + application_name: startup_elements[5].to_string(), + client_encoding: startup_elements[7].to_string(), + }, + bytes, + merged, + )) } // TODO: Only supports SAML currently @@ -112,6 +159,7 @@ impl<'tf> TryFrom<&'tf [u8]> for PostgresMsg<'tf> { return Ok(PostgresMsg { msg_type: PostgresMessageType::Empty, bytes, + ..Default::default() }); } if as_u32_be(&bytes[4..8]) == 196608 { @@ -149,7 +197,12 @@ pub(crate) enum PostgresMessageType { Empty, SASLInitialResponse, SASLResponse, - StartupMessage, + StartupMessage { + user: String, + database: String, + application_name: String, + client_encoding: String, + }, // SSLRequest, NotRecognized, } -- knotmirror 0.0.1 From 25cf6bddd8cc834ec819a317ca2120b27f569ec5 Mon Sep 17 00:00:00 2001 From: cmac4603 Date: Wed, 25 Sep 2024 21:58:20 +0100 Subject: [PATCH 2/2] v0.3.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e2cfc6d..66b17ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["dam-daemon", "dam-relay"] resolver = "2" [workspace.package] -version = "0.2.0" +version = "0.3.0" edition = "2021" [workspace.dependencies] -- knotmirror 0.0.1