use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::Access;
use crate::conn::state::{Connection, Session, TreeConnect};
use crate::conn::writer::{ResponseReceiver, response_queue};
use crate::dispatch::{Outgoing, dispatch_frame, read_u32};
use crate::proto::auth::ntlm::Identity;
use crate::proto::crypto::{SigningAlgo, sign};
use crate::proto::header::{Command, HeaderTail, SMB2_FLAGS_SIGNED, Smb2Header};
use crate::proto::messages::Dialect;
use crate::server::{ServerState, ShareBindings};
pub(crate) const KEY: [u8; 16] = [0x5A; 16];
pub(crate) const START_CREDITS: u16 = 64;
pub(crate) struct Peer {
pub(crate) conn: Arc<Connection>,
pub(crate) rx: ResponseReceiver,
pub(crate) credit_request: u16,
next_mid: u64,
signed: bool,
}
impl Peer {
pub(crate) async fn new(state: &Arc<ServerState>, signed: bool) -> Self {
Self::from_client(state, signed, Uuid::nil()).await
}
pub(crate) async fn from_client(state: &Arc<ServerState>, signed: bool, client: Uuid) -> Self {
let share = state.find_share("home").await.expect("share");
Self::on_share(state, signed, client, share, Access::ReadWrite).await
}
pub(crate) async fn on_ipc(state: &Arc<ServerState>, signed: bool) -> Self {
let ipc = ShareBindings::ipc();
Self::on_share(state, signed, Uuid::nil(), ipc, Access::Read).await
}
async fn on_share(
state: &Arc<ServerState>,
signed: bool,
client: Uuid,
share: Arc<ShareBindings>,
access: Access,
) -> Self {
let conn = Arc::new(Connection::new(state.config.server_guid, 1 << 20, 1 << 20));
*conn.dialect.write().await = Some(Dialect::Smb300);
*conn.signing_algo.write().await = SigningAlgo::AesCmac;
*conn.client_guid.write().await = client;
{
let mut credits = conn.credits.lock().expect("credits");
assert!(credits.consume(0, 1));
credits.grant(START_CREDITS, false);
}
let identity = if signed {
Identity::User {
user: "alice".to_string(),
domain: String::new(),
}
} else {
Identity::Anonymous
};
let session = Session::new(identity, [0; 16], KEY, false, None);
let tree = TreeConnect::new(1, share, access);
session
.trees
.write()
.await
.insert(1, Arc::new(RwLock::new(tree)));
conn.sessions
.write()
.await
.insert(1, Arc::new(RwLock::new(session)));
let (tx, rx) = response_queue();
conn.outbound.attach(tx);
Self {
conn,
rx,
credit_request: 1,
next_mid: 1,
signed,
}
}
pub(crate) fn message(&mut self, command: Command, body: &[u8]) -> (u64, Vec<u8>) {
let mid = self.next_mid;
self.next_mid += 1;
(mid, self.message_with(command, mid, 1, body))
}
pub(crate) fn message_with(
&self,
command: Command,
mid: u64,
session_id: u64,
body: &[u8],
) -> Vec<u8> {
let hdr = Smb2Header {
credit_charge: 1,
command,
credit_request_response: self.credit_request,
flags: if self.signed { SMB2_FLAGS_SIGNED } else { 0 },
message_id: mid,
tail: HeaderTail::sync(1),
session_id,
..Smb2Header::default()
};
let mut msg = Vec::new();
hdr.write(&mut msg).expect("header");
msg.extend_from_slice(body);
if self.signed {
sign(&mut msg, &KEY, SigningAlgo::AesCmac).expect("sign");
}
msg
}
pub(crate) async fn dispatch(&self, state: &Arc<ServerState>, frame: &[u8]) -> Option<Vec<u8>> {
let Outgoing {
bytes,
after_send,
continuation,
} = dispatch_frame(state, &self.conn, frame).await?;
drop(after_send);
if let Some(continuation) = continuation {
self.conn
.pending
.spawn(continuation.run(Arc::clone(state), Arc::clone(&self.conn)));
}
Some(bytes)
}
pub(crate) async fn request(
&mut self,
state: &Arc<ServerState>,
command: Command,
body: &[u8],
) -> (u64, Vec<u8>) {
let (mid, msg) = self.message(command, body);
let reply = self.dispatch(state, &msg).await.expect("a response");
(mid, reply)
}
pub(crate) async fn next(&mut self) -> Vec<u8> {
self.rx.recv().await.expect("a queued message")
}
pub(crate) async fn drop_connection(&self, state: &Arc<ServerState>) {
self.conn.mark_closing();
self.conn
.pending
.shutdown(std::time::Duration::from_secs(1))
.await;
self.conn.outbound.close();
self.conn.close_all(Some(&state.durable)).await;
}
}
pub(crate) fn status(frame: &[u8]) -> u32 {
read_u32(frame, 0x08)
}
pub(crate) fn header(frame: &[u8]) -> Smb2Header {
Smb2Header::parse(frame).expect("header").0
}