use std::sync::Arc;
use crate::proto::header::Smb2Header;
use crate::proto::messages::{CreateContext, CreateRequest, CreateResponse};
use tracing::{debug, warn};
use crate::access_mask::{
DELETE, FILE_APPEND_DATA, FILE_READ_DATA, FILE_WRITE_ATTRIBUTES, FILE_WRITE_DATA,
granted_access, maximal_access,
};
use crate::backend::{FileInfo, OpenIntent, OpenOptions};
use crate::conn::state::{Connection, Open};
use crate::dispatch::HandlerResponse;
use crate::handlers::shared::lookup_session_tree;
use crate::info_class::VOLUME_SERIAL_NUMBER;
use crate::ntstatus;
use crate::path::SmbPath;
use crate::server::ServerState;
use crate::utils::utf16le_to_units;
const FILE_DIRECTORY_FILE: u32 = 0x0000_0001;
const FILE_NON_DIRECTORY_FILE: u32 = 0x0000_0040;
const FILE_DELETE_ON_CLOSE: u32 = 0x0000_1000;
const FILE_SUPERSEDE: u32 = 0x0000_0000;
const FILE_OPEN: u32 = 0x0000_0001;
const FILE_CREATE: u32 = 0x0000_0002;
const FILE_OPEN_IF: u32 = 0x0000_0003;
const FILE_OVERWRITE: u32 = 0x0000_0004;
const FILE_OVERWRITE_IF: u32 = 0x0000_0005;
const FILE_OPENED: u32 = 0x0000_0001;
const FILE_CREATED: u32 = 0x0000_0002;
const RESPONSE_CONTEXTS_OFFSET: u32 = 0x98;
struct ContextQueries {
maximal_access: Option<Option<u64>>,
query_on_disk_id: bool,
}
impl ContextQueries {
fn parse(chain: &[u8]) -> Option<Self> {
let contexts = CreateContext::parse_chain(chain).ok()?;
let find = |name: &[u8; 4]| contexts.iter().find(|c| c.name == name);
let maximal_access = match find(CreateContext::NAME_MXAC) {
None => None,
Some(c) if c.data.is_empty() => Some(None),
Some(c) => Some(Some(u64::from_le_bytes(c.data.as_slice().try_into().ok()?))),
};
let query_on_disk_id = find(CreateContext::NAME_QFID).is_some()
&& find(CreateContext::NAME_DHNC).is_none()
&& find(CreateContext::NAME_DH2C).is_none();
Some(Self {
maximal_access,
query_on_disk_id,
})
}
fn answer(&self, info: &FileInfo, maximal: u32) -> Vec<CreateContext> {
let mut out = Vec::new();
if let Some(timestamp) = self.maximal_access {
let (status, access) = if timestamp == Some(info.change_time) {
(ntstatus::STATUS_NONE_MAPPED, 0)
} else {
(ntstatus::STATUS_SUCCESS, maximal)
};
let mut data = status.to_le_bytes().to_vec();
data.extend_from_slice(&access.to_le_bytes());
out.push(CreateContext {
name: CreateContext::NAME_MXAC.to_vec(),
data,
});
}
if self.query_on_disk_id {
let mut data = vec![0; 32];
data[..8].copy_from_slice(&info.file_index.to_le_bytes());
data[8..16].copy_from_slice(&u64::from(VOLUME_SERIAL_NUMBER).to_le_bytes());
out.push(CreateContext {
name: CreateContext::NAME_QFID.to_vec(),
data,
});
}
out
}
}
pub async fn handle(
_server: &Arc<ServerState>,
conn: &Arc<Connection>,
hdr: &Smb2Header,
body: &[u8],
) -> HandlerResponse {
let req = match CreateRequest::parse(body) {
Ok(r) => r,
Err(_) => return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER),
};
let tree_arc = match lookup_session_tree(conn, hdr).await {
Ok(t) => t,
Err(s) => return HandlerResponse::err(s),
};
let tree = tree_arc.read().await;
let tree_access = tree.granted_access;
let backend = tree.share.backend.clone();
drop(tree);
let units = match utf16le_to_units(&req.name) {
Some(u) => u,
None => return HandlerResponse::err(ntstatus::STATUS_OBJECT_NAME_INVALID),
};
let path = match SmbPath::from_utf16(&units) {
Ok(p) => p,
Err(_) => return HandlerResponse::err(ntstatus::STATUS_OBJECT_NAME_INVALID),
};
let intent = match req.create_disposition {
FILE_SUPERSEDE | FILE_OVERWRITE_IF => OpenIntent::OverwriteOrCreate,
FILE_OPEN => OpenIntent::Open,
FILE_CREATE => OpenIntent::Create,
FILE_OPEN_IF => OpenIntent::OpenOrCreate,
FILE_OVERWRITE => OpenIntent::Truncate,
_ => return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER),
};
let Some(queries) = ContextQueries::parse(&req.create_contexts) else {
return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
};
let maximal = maximal_access(tree_access);
let Some(open_access) = granted_access(req.desired_access, maximal) else {
warn!(path = %path, "requested access exceeds tree access");
return HandlerResponse::err(ntstatus::STATUS_ACCESS_DENIED);
};
if !tree_access.allows_write()
&& matches!(
intent,
OpenIntent::Create
| OpenIntent::OpenOrCreate
| OpenIntent::OverwriteOrCreate
| OpenIntent::Truncate
)
{
return HandlerResponse::err(ntstatus::STATUS_ACCESS_DENIED);
}
let directory = req.create_options & FILE_DIRECTORY_FILE != 0;
let non_directory = req.create_options & FILE_NON_DIRECTORY_FILE != 0;
if directory && non_directory {
return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
}
let delete_on_close = req.create_options & FILE_DELETE_ON_CLOSE != 0;
if delete_on_close {
if maximal & DELETE == 0 {
warn!(path = %path, "delete-on-close open on read-only tree");
return HandlerResponse::err(ntstatus::STATUS_ACCESS_DENIED);
}
if open_access & DELETE == 0 {
return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
}
}
let write = open_access & (FILE_WRITE_DATA | FILE_APPEND_DATA) != 0;
let opts = OpenOptions {
read: open_access & FILE_READ_DATA != 0 || !write,
write,
write_attributes: open_access & FILE_WRITE_ATTRIBUTES != 0,
intent,
directory,
non_directory,
delete_on_close,
};
let handle = match backend.open(&path, opts).await {
Ok(h) => h,
Err(e) => {
debug!(error = %e, path = %path, "backend open failed");
return HandlerResponse::err(e.to_nt_status());
}
};
let info = match handle.stat().await {
Ok(i) => i,
Err(e) => {
let _ = handle.close().await;
return HandlerResponse::err(e.to_nt_status());
}
};
if delete_on_close && info.read_only {
let _ = handle.close().await;
return HandlerResponse::err(ntstatus::STATUS_CANNOT_DELETE);
}
let tree = tree_arc.write().await;
let file_id = tree.alloc_file_id();
let open = Open::new(
file_id,
handle,
open_access,
path,
info.is_directory,
delete_on_close,
);
let open_arc = Arc::new(tokio::sync::RwLock::new(open));
tree.opens.write().await.insert(file_id, open_arc);
drop(tree);
let create_action = match intent {
OpenIntent::Create => FILE_CREATED,
OpenIntent::OpenOrCreate | OpenIntent::OverwriteOrCreate => FILE_OPENED,
OpenIntent::Open | OpenIntent::Truncate => FILE_OPENED,
};
let mut contexts = Vec::new();
CreateContext::encode_chain(&queries.answer(&info, maximal), &mut contexts).expect("encode");
let resp = CreateResponse {
structure_size: 89,
oplock_level: 0,
flags: 0,
create_action,
creation_time: info.creation_time,
last_access_time: info.last_access_time,
last_write_time: info.last_write_time,
change_time: info.change_time,
allocation_size: info.allocation_size,
end_of_file: info.end_of_file,
file_attributes: info.attributes(),
reserved2: 0,
file_id,
create_contexts_offset: if contexts.is_empty() {
0
} else {
RESPONSE_CONTEXTS_OFFSET
},
create_contexts_length: contexts.len() as u32,
create_contexts: contexts,
};
let mut buf = Vec::new();
resp.write_to(&mut buf).expect("encode");
HandlerResponse::ok(buf)
}