Skip to content
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

Adds support for GetStatusChangeCall/GetStatusChangeReturn #33056

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions lib/srv/desktop/rdp/rdpclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ parking_lot = "0.12.1"

bytes = "1"
futures-util = "0.3"
tokio = { version = "1", features = ["full"]}
tokio = { version = "1.32", features = ["full"]}
tokio-util = { version = "0.7.4", features = ["compat"] }
tokio-rustls = { version = "0.24", features = ["dangerous_configuration"] }
x509-parser = "0.14"
sspi = { version = "0.10.1", features = ["network_client", "dns_resolver"] }
static_init = "1.0.3"

ironrdp-connector = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-tls = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-session = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-pdu = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-tokio = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-rdpsnd = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-rdpdr = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-svc = { git = "https://github.com/Devolutions/IronRDP", rev = "774a8634da6e2eb3d934cf0c55f1b4d948d216fe" }
ironrdp-connector = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-tls = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-session = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-pdu = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-tokio = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-rdpsnd = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-rdpdr = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }
ironrdp-svc = { git = "https://github.com/Devolutions/IronRDP", rev = "bf05ef8749149ed31129ed0db0561db13e78d0d7" }

# Uncomment the following lines to use local crates instead of the ones from github
# ironrdp-connector = { path = "/Users/ibeckermayer/Devolutions/IronRDP/crates/ironrdp-connector" }
Expand Down
117 changes: 114 additions & 3 deletions lib/srv/desktop/rdp/rdpclient/src/rdpdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use consts::{
};
use ironrdp_pdu::{custom_err, other_err, PduResult};
use ironrdp_rdpdr::pdu::esc::{
rpce, EstablishContextCall, EstablishContextReturn, ListReadersCall, ListReadersReturn,
ScardCall,
rpce, CardStateFlags, EstablishContextCall, EstablishContextReturn, GetStatusChangeCall,
GetStatusChangeReturn, ListReadersCall, ListReadersReturn, ReaderStateCommonCall, ScardCall,
};
use ironrdp_rdpdr::pdu::RdpdrPdu;
use ironrdp_rdpdr::{
Expand Down Expand Up @@ -145,11 +145,121 @@ impl TeleportRdpdrBackend {
req,
Box::new(ListReadersReturn::new(
ReturnCode::Success,
vec!["Teleport".to_string()],
vec![scard::TELEPORT_READER_NAME.to_string()],
)),
)
}

fn handle_get_status_change(
&mut self,
req: DeviceControlRequest<ScardIoCtlCode>,
call: GetStatusChangeCall,
) -> PduResult<()> {
let timeout = call.timeout;
let context_id = call.context.value;

if timeout != scard::TIMEOUT_INFINITE && timeout != scard::TIMEOUT_IMMEDIATE {
// We've never seen one of these but we log a warning here in case we ever come
// across one and need to debug a related issue.
warn!(
"logic for a non-infinite/non-immediate timeout [{}] is not implemented",
timeout
);
}

let get_status_change_ret = Self::create_get_status_change_return(call);

// We have no status change to report, cache a response
// for later in case we get an SCARD_IOCTL_CANCEL.
if Self::has_no_change(&get_status_change_ret) {
if timeout != scard::TIMEOUT_INFINITE {
return Err(other_err!(
"TeleportRdpdrBackend::handle_list_readers",
"got no change for non-infinite timeout",
));
}

// Received a GetStatusChangeCall with an infinite timeout, so we're adding
// a corresponding DeviceControlResponse holding a GetStatusChangeReturn
// with its return code set to SCARD_E_CANCELLED to this Context. This value will
// be returned when we get an SCARD_IOCTL_CANCEL call for this Context.
self.contexts.set_scard_cancel_response(
context_id,
DeviceControlResponse::new(
req,
NtStatus::Success,
Box::new(GetStatusChangeReturn::new(
ReturnCode::Cancelled,
get_status_change_ret.into_inner().reader_states,
)),
),
)?;

debug!("blocking GetStatusChange call indefinitely (since our status never changes) until we receive an SCARD_IOCTL_CANCEL");

return Ok(());
}

// We have some status change to report, send it to the server.
self.write_rdpdr_dev_ctl_resp(req, Box::new(get_status_change_ret))
}

fn create_get_status_change_return(
call: GetStatusChangeCall,
) -> rpce::Pdu<GetStatusChangeReturn> {
let mut reader_states = vec![];
for state in call.states {
match state.reader.as_str() {
// PnP is Plug-and-Play. This special reader "name" is used to monitor for
// new readers being plugged in.
"\\\\?PnP?\\Notification" => {
reader_states.push(ReaderStateCommonCall {
current_state: state.common.current_state,
event_state: state.common.current_state,
atr_length: state.common.atr_length,
atr: state.common.atr,
});
}
// This is our actual emulated smartcard reader. We always advertise its state as
// "present".
scard::TELEPORT_READER_NAME => {
let (atr_length, atr) = scard::padded_atr::<36>();
reader_states.push(ReaderStateCommonCall {
current_state: state.common.current_state,
event_state: CardStateFlags::SCARD_STATE_CHANGED
| CardStateFlags::SCARD_STATE_PRESENT,
atr_length,
atr,
});
}
// All other reader names are unknown and unexpected.
_ => {
warn!(
"got unexpected reader name [{}], ignoring",
state.reader.as_str()
);
reader_states.push(ReaderStateCommonCall {
current_state: state.common.current_state,
event_state: CardStateFlags::SCARD_STATE_CHANGED
| CardStateFlags::SCARD_STATE_UNKNOWN
| CardStateFlags::SCARD_STATE_IGNORE,
atr_length: state.common.atr_length,
atr: state.common.atr,
});
}
}
}

GetStatusChangeReturn::new(ReturnCode::Success, reader_states)
}

fn has_no_change(pdu: &rpce::Pdu<GetStatusChangeReturn>) -> bool {
pdu.into_inner_ref()
.reader_states
.iter()
.all(|state| state.current_state == state.event_state)
}

fn write_rdpdr_dev_ctl_resp(
&mut self,
req: DeviceControlRequest<ScardIoCtlCode>,
Expand Down Expand Up @@ -204,6 +314,7 @@ impl RdpdrBackend for TeleportRdpdrBackend {
ScardCall::AccessStartedEventCall(call) => self.handle_access_started(req, call),
ScardCall::EstablishContextCall(call) => self.handle_establish_context(req, call),
ScardCall::ListReadersCall(call) => self.handle_list_readers(req, call),
ScardCall::GetStatusChangeCall(call) => self.handle_get_status_change(req, call),
ScardCall::Unsupported => Ok(()),
}
}
Expand Down
Loading