2 Unix SMB/CIFS implementation.
4 dcerpc over SMB2 transport
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "../libcli/smb/smb_constants.h"
28 #include "librpc/rpc/dcerpc.h"
29 #include "librpc/rpc/dcerpc_proto.h"
30 #include "librpc/rpc/rpc_common.h"
31 #include "../libcli/smb/smbXcli_base.h"
33 /* transport private information used by SMB2 pipe transport */
35 struct smb2_handle handle
;
36 struct smb2_tree
*tree
;
37 DATA_BLOB session_key
;
38 const char *server_name
;
44 tell the dcerpc layer that the transport is dead
46 static void pipe_dead(struct dcecli_connection
*c
, NTSTATUS status
)
48 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
56 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
57 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
60 if (NT_STATUS_EQUAL(NT_STATUS_OK
, status
)) {
61 status
= NT_STATUS_END_OF_FILE
;
64 if (c
->transport
.recv_data
) {
65 c
->transport
.recv_data(c
, NULL
, status
);
71 this holds the state of an in-flight call
73 struct smb2_read_state
{
74 struct dcecli_connection
*c
;
79 called when a read request has completed
81 static void smb2_read_callback(struct smb2_request
*req
)
83 struct dcecli_connection
*c
;
84 struct smb2_private
*smb
;
85 struct smb2_read_state
*state
;
90 state
= talloc_get_type(req
->async
.private_data
, struct smb2_read_state
);
91 smb
= talloc_get_type(state
->c
->transport
.private_data
, struct smb2_private
);
94 status
= smb2_read_recv(req
, state
, &io
);
95 if (NT_STATUS_IS_ERR(status
)) {
101 if (!data_blob_append(state
, &state
->data
,
102 io
.out
.data
.data
, io
.out
.data
.length
)) {
104 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
108 if (state
->data
.length
< 16) {
109 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
110 (int)state
->data
.length
));
112 pipe_dead(c
, NT_STATUS_INFO_LENGTH_MISMATCH
);
116 frag_length
= dcerpc_get_frag_length(&state
->data
);
118 if (frag_length
<= state
->data
.length
) {
119 DATA_BLOB data
= state
->data
;
120 talloc_steal(c
, data
.data
);
122 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
126 /* initiate another read request, as we only got part of a fragment */
128 io
.in
.file
.handle
= smb
->handle
;
129 io
.in
.length
= MIN(state
->c
->srv_max_xmit_frag
,
130 frag_length
- state
->data
.length
);
131 if (io
.in
.length
< 16) {
135 req
= smb2_read_send(smb
->tree
, &io
);
138 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
142 req
->async
.fn
= smb2_read_callback
;
143 req
->async
.private_data
= state
;
148 trigger a read request from the server, possibly with some initial
149 data in the read buffer
151 static NTSTATUS
send_read_request_continue(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
153 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
155 struct smb2_read_state
*state
;
156 struct smb2_request
*req
;
158 state
= talloc(c
, struct smb2_read_state
);
160 return NT_STATUS_NO_MEMORY
;
165 state
->data
= data_blob(NULL
, 0);
168 talloc_steal(state
, state
->data
.data
);
172 io
.in
.file
.handle
= smb
->handle
;
174 if (state
->data
.length
>= 16) {
175 uint16_t frag_length
= dcerpc_get_frag_length(&state
->data
);
176 io
.in
.length
= frag_length
- state
->data
.length
;
178 io
.in
.length
= 0x2000;
181 req
= smb2_read_send(smb
->tree
, &io
);
183 return NT_STATUS_NO_MEMORY
;
186 req
->async
.fn
= smb2_read_callback
;
187 req
->async
.private_data
= state
;
194 trigger a read request from the server
196 static NTSTATUS
send_read_request(struct dcecli_connection
*c
)
198 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
201 return NT_STATUS_CONNECTION_DISCONNECTED
;
204 return send_read_request_continue(c
, NULL
);
208 this holds the state of an in-flight trans call
210 struct smb2_trans_state
{
211 struct dcecli_connection
*c
;
215 called when a trans request has completed
217 static void smb2_trans_callback(struct smb2_request
*req
)
219 struct smb2_trans_state
*state
= talloc_get_type(req
->async
.private_data
,
220 struct smb2_trans_state
);
221 struct dcecli_connection
*c
= state
->c
;
223 struct smb2_ioctl io
;
225 status
= smb2_ioctl_recv(req
, state
, &io
);
226 if (NT_STATUS_IS_ERR(status
)) {
227 pipe_dead(c
, status
);
231 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
232 DATA_BLOB data
= io
.out
.out
;
233 talloc_steal(c
, data
.data
);
235 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
239 /* there is more to receive - setup a read */
240 send_read_request_continue(c
, &io
.out
.out
);
245 send a SMBtrans style request, using a named pipe read_write fsctl
247 static NTSTATUS
smb2_send_trans_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
249 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
250 struct smb2_private
);
251 struct smb2_ioctl io
;
252 struct smb2_trans_state
*state
;
253 struct smb2_request
*req
;
255 state
= talloc(smb
, struct smb2_trans_state
);
257 return NT_STATUS_NO_MEMORY
;
263 io
.in
.file
.handle
= smb
->handle
;
264 io
.in
.function
= FSCTL_NAMED_PIPE_READ_WRITE
;
265 io
.in
.max_response_size
= 0x2000;
269 req
= smb2_ioctl_send(smb
->tree
, &io
);
272 return NT_STATUS_NO_MEMORY
;
275 req
->async
.fn
= smb2_trans_callback
;
276 req
->async
.private_data
= state
;
278 talloc_steal(state
, req
);
284 called when a write request has completed
286 static void smb2_write_callback(struct smb2_request
*req
)
288 struct dcecli_connection
*c
= (struct dcecli_connection
*)req
->async
.private_data
;
289 struct smb2_write io
;
294 status
= smb2_write_recv(req
, &io
);
295 if (!NT_STATUS_IS_OK(status
)) {
296 DEBUG(0,("dcerpc_smb2: write callback error: %s\n",
298 pipe_dead(c
, status
);
303 send a packet to the server
305 static NTSTATUS
smb2_send_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
,
308 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
309 struct smb2_write io
;
310 struct smb2_request
*req
;
313 return NT_STATUS_CONNECTION_DISCONNECTED
;
317 return smb2_send_trans_request(c
, blob
);
321 io
.in
.file
.handle
= smb
->handle
;
324 req
= smb2_write_send(smb
->tree
, &io
);
326 return NT_STATUS_NO_MEMORY
;
329 req
->async
.fn
= smb2_write_callback
;
330 req
->async
.private_data
= c
;
335 static void free_request(struct smb2_request
*req
)
341 shutdown SMB pipe connection
343 static NTSTATUS
smb2_shutdown_pipe(struct dcecli_connection
*c
, NTSTATUS status
)
345 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
346 struct smb2_close io
;
347 struct smb2_request
*req
;
349 /* maybe we're still starting up */
350 if (!smb
) return status
;
353 io
.in
.file
.handle
= smb
->handle
;
354 req
= smb2_close_send(smb
->tree
, &io
);
356 /* we don't care if this fails, so just free it if it succeeds */
357 req
->async
.fn
= free_request
;
366 return SMB server name
368 static const char *smb2_peer_name(struct dcecli_connection
*c
)
370 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
371 struct smb2_private
);
372 return smb
->server_name
;
376 return remote name we make the actual connection (good for kerberos)
378 static const char *smb2_target_hostname(struct dcecli_connection
*c
)
380 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
381 struct smb2_private
);
382 return smbXcli_conn_remote_name(smb
->tree
->session
->transport
->conn
);
386 fetch the user session key
388 static NTSTATUS
smb2_session_key(struct dcecli_connection
*c
, DATA_BLOB
*session_key
)
390 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
391 struct smb2_private
);
393 if (smb
== NULL
) return NT_STATUS_CONNECTION_DISCONNECTED
;
395 if (smb
->session_key
.length
== 0) {
396 return NT_STATUS_NO_USER_SESSION_KEY
;
399 *session_key
= smb
->session_key
;
403 struct pipe_open_smb2_state
{
404 struct dcecli_connection
*c
;
405 struct composite_context
*ctx
;
408 static void pipe_open_recv(struct smb2_request
*req
);
410 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
411 struct smb2_tree
*tree
,
412 const char *pipe_name
)
414 struct composite_context
*ctx
;
415 struct pipe_open_smb2_state
*state
;
416 struct smb2_create io
;
417 struct smb2_request
*req
;
418 struct dcecli_connection
*c
= p
->conn
;
420 /* if we don't have a binding on this pipe yet, then create one */
421 if (p
->binding
== NULL
) {
423 const char *r
= smbXcli_conn_remote_name(tree
->session
->transport
->conn
);
425 SMB_ASSERT(r
!= NULL
);
426 s
= talloc_asprintf(p
, "ncacn_np:%s", r
);
427 if (s
== NULL
) return NULL
;
428 status
= dcerpc_parse_binding(p
, s
, &p
->binding
);
430 if (!NT_STATUS_IS_OK(status
)) {
435 ctx
= composite_create(c
, c
->event_ctx
);
436 if (ctx
== NULL
) return NULL
;
438 state
= talloc(ctx
, struct pipe_open_smb2_state
);
439 if (composite_nomem(state
, ctx
)) return ctx
;
440 ctx
->private_data
= state
;
446 io
.in
.desired_access
=
447 SEC_STD_READ_CONTROL
|
448 SEC_FILE_READ_ATTRIBUTE
|
449 SEC_FILE_WRITE_ATTRIBUTE
|
450 SEC_STD_SYNCHRONIZE
|
454 SEC_FILE_WRITE_DATA
|
455 SEC_FILE_APPEND_DATA
;
457 NTCREATEX_SHARE_ACCESS_READ
|
458 NTCREATEX_SHARE_ACCESS_WRITE
;
459 io
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
460 io
.in
.create_options
=
461 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
462 NTCREATEX_OPTIONS_NO_RECALL
;
463 io
.in
.impersonation_level
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
465 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
466 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
469 io
.in
.fname
= pipe_name
;
471 req
= smb2_create_send(tree
, &io
);
472 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
476 static void pipe_open_recv(struct smb2_request
*req
)
478 struct pipe_open_smb2_state
*state
=
479 talloc_get_type(req
->async
.private_data
,
480 struct pipe_open_smb2_state
);
481 struct composite_context
*ctx
= state
->ctx
;
482 struct dcecli_connection
*c
= state
->c
;
483 struct smb2_tree
*tree
= req
->tree
;
484 struct smb2_private
*smb
;
485 struct smb2_create io
;
487 ctx
->status
= smb2_create_recv(req
, state
, &io
);
488 if (!composite_is_ok(ctx
)) return;
491 fill in the transport methods
493 c
->transport
.transport
= NCACN_NP
;
494 c
->transport
.private_data
= NULL
;
495 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
496 c
->transport
.peer_name
= smb2_peer_name
;
497 c
->transport
.target_hostname
= smb2_target_hostname
;
499 c
->transport
.send_request
= smb2_send_request
;
500 c
->transport
.send_read
= send_read_request
;
501 c
->transport
.recv_data
= NULL
;
503 /* Over-ride the default session key with the SMB session key */
504 c
->security_state
.session_key
= smb2_session_key
;
506 smb
= talloc(c
, struct smb2_private
);
507 if (composite_nomem(smb
, ctx
)) return;
509 smb
->handle
= io
.out
.file
.handle
;
510 smb
->tree
= talloc_reference(smb
, tree
);
511 smb
->server_name
= strupper_talloc(smb
,
512 smbXcli_conn_remote_name(tree
->session
->transport
->conn
));
513 if (composite_nomem(smb
->server_name
, ctx
)) return;
516 ctx
->status
= smbXcli_session_application_key(tree
->session
->smbXcli
,
517 smb
, &smb
->session_key
);
518 if (NT_STATUS_EQUAL(ctx
->status
, NT_STATUS_NO_USER_SESSION_KEY
)) {
519 smb
->session_key
= data_blob_null
;
520 ctx
->status
= NT_STATUS_OK
;
522 if (!composite_is_ok(ctx
)) return;
524 c
->transport
.private_data
= smb
;
529 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
531 NTSTATUS status
= composite_wait(c
);
536 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
537 struct smb2_tree
*tree
,
538 const char *pipe_name
)
540 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
541 return dcerpc_pipe_open_smb2_recv(ctx
);
545 return the SMB2 tree used for a dcerpc over SMB2 pipe
547 struct smb2_tree
*dcerpc_smb2_tree(struct dcecli_connection
*c
)
549 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
550 struct smb2_private
);