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

[v9] backports for 13630 14267 14959 15289 15364 15743 (directory sharing) #15770

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
353 changes: 257 additions & 96 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/srv/desktop/rdp/rdpclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ byteorder = "1.4.3"
env_logger = "0.9.0"
iso7816 = "0.1.0"
iso7816-tlv = "0.4.2"
libc = "0.2.121"
log = "0.4.16"
libc = "0.2.126"
log = "0.4.17"
num-derive = "0.3.3"
num-traits = "0.2.14"
num-traits = "0.2.15"
rand = { version = "0.8.5", features = ["getrandom"] }
rand_chacha = "0.3.1"
rsa = "0.6.1"
rdp-rs = { git = "https://github.com/gravitational/rdp-rs", rev = "6075679e7c9bd8e3c2136a87f097d05c7db3235f" }
rdp-rs = { git = "https://github.com/gravitational/rdp-rs", rev = "888b15477d9aaceb83b2afcc73de040a07781d28" }
uuid = { version = "1.1.2", features = ["v4"] }
utf16string = "0.2.0"

Expand Down
282 changes: 268 additions & 14 deletions lib/srv/desktop/rdp/rdpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type Client struct {
}

// New creates and connects a new Client based on cfg.
func New(ctx context.Context, cfg Config) (*Client, error) {
func New(cfg Config) (*Client, error) {
if err := cfg.checkAndSetDefaults(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -412,18 +412,121 @@ func (c *Client) start() {
defer C.free(unsafe.Pointer(path))
if errCode := C.handle_tdp_sd_info_response(c.rustClient, C.CGOSharedDirectoryInfoResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: C.uint32_t(m.ErrCode),
err_code: m.ErrCode,
fso: C.CGOFileSystemObject{
last_modified: C.uint64_t(m.Fso.LastModified),
size: C.uint64_t(m.Fso.Size),
file_type: C.uint32_t(m.Fso.FileType),
file_type: m.Fso.FileType,
path: path,
},
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryInfoResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryCreateResponse:
if c.cfg.AllowDirectorySharing {
path := C.CString(m.Fso.Path)
defer C.free(unsafe.Pointer(path))
if errCode := C.handle_tdp_sd_create_response(c.rustClient, C.CGOSharedDirectoryCreateResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
fso: C.CGOFileSystemObject{
last_modified: C.uint64_t(m.Fso.LastModified),
size: C.uint64_t(m.Fso.Size),
file_type: m.Fso.FileType,
path: path,
},
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryCreateResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryDeleteResponse:
if c.cfg.AllowDirectorySharing {
if errCode := C.handle_tdp_sd_delete_response(c.rustClient, C.CGOSharedDirectoryDeleteResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryDeleteResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryListResponse:
if c.cfg.AllowDirectorySharing {
fsoList := make([]C.CGOFileSystemObject, 0, len(m.FsoList))

for _, fso := range m.FsoList {
path := C.CString(fso.Path)
defer C.free(unsafe.Pointer(path))

fsoList = append(fsoList, C.CGOFileSystemObject{
last_modified: C.uint64_t(fso.LastModified),
size: C.uint64_t(fso.Size),
file_type: fso.FileType,
path: path,
})
}

fsoListLen := len(fsoList)
var cgoFsoList *C.CGOFileSystemObject

if fsoListLen > 0 {
cgoFsoList = (*C.CGOFileSystemObject)(unsafe.Pointer(&fsoList[0]))
} else {
cgoFsoList = (*C.CGOFileSystemObject)(unsafe.Pointer(&fsoList))
}

if errCode := C.handle_tdp_sd_list_response(c.rustClient, C.CGOSharedDirectoryListResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
fso_list_length: C.uint32_t(fsoListLen),
fso_list: cgoFsoList,
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryListResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryReadResponse:
if c.cfg.AllowDirectorySharing {
var readData *C.uint8_t
if m.ReadDataLength > 0 {
readData = (*C.uint8_t)(unsafe.Pointer(&m.ReadData[0]))
} else {
readData = (*C.uint8_t)(unsafe.Pointer(&m.ReadData))
}

if errCode := C.handle_tdp_sd_read_response(c.rustClient, C.CGOSharedDirectoryReadResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
read_data_length: C.uint32_t(m.ReadDataLength),
read_data: readData,
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryReadResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryWriteResponse:
if c.cfg.AllowDirectorySharing {
if errCode := C.handle_tdp_sd_write_response(c.rustClient, C.CGOSharedDirectoryWriteResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
bytes_written: C.uint32_t(m.BytesWritten),
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryWriteResponse failed: %v", errCode)
return
}
}
case tdp.SharedDirectoryMoveResponse:
if c.cfg.AllowDirectorySharing {
if errCode := C.handle_tdp_sd_move_response(c.rustClient, C.CGOSharedDirectoryMoveResponse{
completion_id: C.uint32_t(m.CompletionID),
err_code: m.ErrCode,
}); errCode != C.ErrCodeSuccess {
c.cfg.Log.Errorf("SharedDirectoryMoveResponse failed: %v", errCode)
return
}
}
default:
c.cfg.Log.Warningf("Skipping unimplemented TDP message type %T", msg)
}
Expand Down Expand Up @@ -497,13 +600,16 @@ func tdp_sd_acknowledge(handle C.uintptr_t, ack *C.CGOSharedDirectoryAcknowledge
})
}

// sharedDirectoryAcknowledge acknowledges that a `Shared Directory Announce` TDP message was processed.
// sharedDirectoryAcknowledge is sent by the TDP server to the client
// to acknowledge that a SharedDirectoryAnnounce was received.
func (c *Client) sharedDirectoryAcknowledge(ack tdp.SharedDirectoryAcknowledge) C.CGOErrCode {
if c.cfg.AllowDirectorySharing {
if err := c.cfg.Conn.OutputMessage(ack); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryAcknowledge: %v", err)
return C.ErrCodeFailure
}
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(ack); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryAcknowledge: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}
Expand All @@ -517,16 +623,164 @@ func tdp_sd_info_request(handle C.uintptr_t, req *C.CGOSharedDirectoryInfoReques
})
}

// sharedDirectoryInfoRequest is sent from the TDP server to the client
// to request information about a file or directory at a given path.
func (c *Client) sharedDirectoryInfoRequest(req tdp.SharedDirectoryInfoRequest) C.CGOErrCode {
if c.cfg.AllowDirectorySharing {
if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryAcknowledge: %v", err)
return C.ErrCodeFailure
}
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryAcknowledge: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_create_request
func tdp_sd_create_request(handle C.uintptr_t, req *C.CGOSharedDirectoryCreateRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryCreateRequest(tdp.SharedDirectoryCreateRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
FileType: uint32(req.file_type),
Path: C.GoString(req.path),
})
}

// sharedDirectoryCreateRequest is sent by the TDP server to
// the client to request the creation of a new file or directory.
func (c *Client) sharedDirectoryCreateRequest(req tdp.SharedDirectoryCreateRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryCreateRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_delete_request
func tdp_sd_delete_request(handle C.uintptr_t, req *C.CGOSharedDirectoryDeleteRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryDeleteRequest(tdp.SharedDirectoryDeleteRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
Path: C.GoString(req.path),
})
}

// sharedDirectoryDeleteRequest is sent by the TDP server to the client
// to request the deletion of a file or directory at path.
func (c *Client) sharedDirectoryDeleteRequest(req tdp.SharedDirectoryDeleteRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryDeleteRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_list_request
func tdp_sd_list_request(handle C.uintptr_t, req *C.CGOSharedDirectoryListRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryListRequest(tdp.SharedDirectoryListRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
Path: C.GoString(req.path),
})
}

// sharedDirectoryListRequest is sent by the TDP server to the client
// to request the contents of a directory.
func (c *Client) sharedDirectoryListRequest(req tdp.SharedDirectoryListRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryListRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_read_request
func tdp_sd_read_request(handle C.uintptr_t, req *C.CGOSharedDirectoryReadRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryReadRequest(tdp.SharedDirectoryReadRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
Path: C.GoString(req.path),
Offset: uint64(req.offset),
Length: uint32(req.length),
})
}

// SharedDirectoryReadRequest is sent by the TDP server to the client
// to request the contents of a file.
func (c *Client) sharedDirectoryReadRequest(req tdp.SharedDirectoryReadRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryReadRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_write_request
func tdp_sd_write_request(handle C.uintptr_t, req *C.CGOSharedDirectoryWriteRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryWriteRequest(tdp.SharedDirectoryWriteRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
Offset: uint64(req.offset),
Path: C.GoString(req.path),
WriteDataLength: uint32(req.write_data_length),
WriteData: C.GoBytes(unsafe.Pointer(req.write_data), C.int(req.write_data_length)),
})
}

// SharedDirectoryWriteRequest is sent by the TDP server to the client
// to write to a file.
func (c *Client) sharedDirectoryWriteRequest(req tdp.SharedDirectoryWriteRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryWriteRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess
}

//export tdp_sd_move_request
func tdp_sd_move_request(handle C.uintptr_t, req *C.CGOSharedDirectoryMoveRequest) C.CGOErrCode {
return cgo.Handle(handle).Value().(*Client).sharedDirectoryMoveRequest(tdp.SharedDirectoryMoveRequest{
CompletionID: uint32(req.completion_id),
DirectoryID: uint32(req.directory_id),
OriginalPath: C.GoString(req.original_path),
NewPath: C.GoString(req.new_path),
})
}

func (c *Client) sharedDirectoryMoveRequest(req tdp.SharedDirectoryMoveRequest) C.CGOErrCode {
if !c.cfg.AllowDirectorySharing {
return C.ErrCodeFailure
}

if err := c.cfg.Conn.OutputMessage(req); err != nil {
c.cfg.Log.Errorf("failed to send SharedDirectoryMoveRequest: %v", err)
return C.ErrCodeFailure
}
return C.ErrCodeSuccess

}

// close closes the RDP client connection and
// the TDP connection to the browser.
func (c *Client) close() {
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/desktop/rdp/rdpclient/client_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Client struct {
}

// New creates and connects a new Client based on opts.
func New(ctx context.Context, cfg Config) (*Client, error) {
func New(cfg Config) (*Client, error) {
return &Client{}, errors.New("the real rdpclient.Client implementation was not included in this build")
}

Expand Down
Loading