Skip to content

Commit

Permalink
Improve process_irp_create (#12830)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer authored Jun 28, 2022
1 parent 8a3153b commit de934c8
Showing 1 changed file with 82 additions and 70 deletions.
152 changes: 82 additions & 70 deletions lib/srv/desktop/rdp/rdpclient/src/rdpdr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,69 +328,78 @@ impl Client {
Box::new(
|cli: &mut Self, res: SharedDirectoryInfoResponse| -> RdpResult<Vec<Vec<u8>>> {
let rdp_req = rdp_req;
if res.err_code == TdpErrCode::Nil {
// The file exists
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L214
if res.fso.file_type == FileType::Directory {
if rdp_req.create_disposition == flags::CreateDisposition::FILE_CREATE {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L221
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_OBJECT_NAME_COLLISION,
0,
);
}

if rdp_req
match res.err_code {
TdpErrCode::Failed | TdpErrCode::AlreadyExists => {
return Err(try_error(&format!(
"received unexpected TDP error code in SharedDirectoryInfoResponse: {:?}",
res.err_code,
)));
}
TdpErrCode::Nil => {
// The file exists
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L214
if res.fso.file_type == FileType::Directory {
if rdp_req.create_disposition
== flags::CreateDisposition::FILE_CREATE
{
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L221
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_OBJECT_NAME_COLLISION,
0,
);
}

if rdp_req
.create_options
.contains(flags::CreateOptions::FILE_NON_DIRECTORY_FILE)
{
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L227
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_ACCESS_DENIED,
0,
);
}
} else if rdp_req
.create_options
.contains(flags::CreateOptions::FILE_NON_DIRECTORY_FILE)
.contains(flags::CreateOptions::FILE_DIRECTORY_FILE)
{
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L227
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L237
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_ACCESS_DENIED,
NTSTATUS::STATUS_NOT_A_DIRECTORY,
0,
);
}
} else if rdp_req
.create_options
.contains(flags::CreateOptions::FILE_DIRECTORY_FILE)
{
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L237
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_NOT_A_DIRECTORY,
0,
);
}
} else if res.err_code == TdpErrCode::DoesNotExist {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L242
if rdp_req
.create_options
.contains(flags::CreateOptions::FILE_DIRECTORY_FILE)
{
if rdp_req.create_disposition.intersects(
flags::CreateDisposition::FILE_OPEN_IF
| flags::CreateDisposition::FILE_CREATE,
) {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L252
return cli.tdp_sd_create(rdp_req, FileType::Directory, res.fso);
} else {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L258
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_NO_SUCH_FILE,
0,
);
TdpErrCode::DoesNotExist => {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L242
if rdp_req
.create_options
.contains(flags::CreateOptions::FILE_DIRECTORY_FILE)
{
if rdp_req.create_disposition.intersects(
flags::CreateDisposition::FILE_OPEN_IF
| flags::CreateDisposition::FILE_CREATE,
) {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L252
return cli.tdp_sd_create(
rdp_req,
FileType::Directory,
res.fso,
);
} else {
// https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L258
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_NO_SUCH_FILE,
0,
);
}
}
}
} else {
// generic RDP error
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_UNSUCCESSFUL,
0,
);
}

// The actual creation of files and error mapping in FreeRDP happens here, for reference:
Expand All @@ -399,9 +408,9 @@ impl Client {
flags::CreateDisposition::FILE_SUPERSEDE => {
// If the file already exists, replace it with the given file. If it does not, create the given file.
if res.err_code == TdpErrCode::Nil {
cli.tdp_sd_overwrite(rdp_req, res.fso)
} else {
cli.tdp_sd_create(rdp_req, FileType::File, res.fso)
return cli.tdp_sd_overwrite(rdp_req, res.fso);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.tdp_sd_create(rdp_req, FileType::File, res.fso);
}
}
flags::CreateDisposition::FILE_OPEN => {
Expand All @@ -416,9 +425,9 @@ impl Client {
&rdp_req,
NTSTATUS::STATUS_SUCCESS,
file_id,
)
} else {
cli.prep_device_create_response(
);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_NO_SUCH_FILE,
0,
Expand All @@ -432,9 +441,9 @@ impl Client {
&rdp_req,
NTSTATUS::STATUS_OBJECT_NAME_COLLISION,
0,
)
} else {
cli.tdp_sd_create(rdp_req, FileType::File, res.fso)
);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.tdp_sd_create(rdp_req, FileType::File, res.fso);
}
}
flags::CreateDisposition::FILE_OPEN_IF => {
Expand All @@ -449,17 +458,17 @@ impl Client {
&rdp_req,
NTSTATUS::STATUS_SUCCESS,
file_id,
)
} else {
cli.tdp_sd_create(rdp_req, FileType::File, res.fso)
);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.tdp_sd_create(rdp_req, FileType::File, res.fso);
}
}
flags::CreateDisposition::FILE_OVERWRITE => {
// If the file already exists, open it and overwrite it. If it does not, fail the request.
if res.err_code == TdpErrCode::Nil {
cli.tdp_sd_overwrite(rdp_req, res.fso)
} else {
cli.prep_device_create_response(
return cli.tdp_sd_overwrite(rdp_req, res.fso);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.prep_device_create_response(
&rdp_req,
NTSTATUS::STATUS_NO_SUCH_FILE,
0,
Expand All @@ -469,9 +478,9 @@ impl Client {
flags::CreateDisposition::FILE_OVERWRITE_IF => {
// If the file already exists, open it and overwrite it. If it does not, create the given file.
if res.err_code == TdpErrCode::Nil {
cli.tdp_sd_overwrite(rdp_req, res.fso)
} else {
cli.tdp_sd_create(rdp_req, FileType::File, res.fso)
return cli.tdp_sd_overwrite(rdp_req, res.fso);
} else if res.err_code == TdpErrCode::DoesNotExist {
return cli.tdp_sd_create(rdp_req, FileType::File, res.fso);
}
}
_ => {
Expand All @@ -481,9 +490,12 @@ impl Client {
)));
}
}

Err(try_error("Programmer error, this line should never be reached"))
},
),
);

Ok(vec![])
}

Expand Down

0 comments on commit de934c8

Please sign in to comment.