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
->session_key
.length
== 0) {
394 return NT_STATUS_NO_USER_SESSION_KEY
;
397 *session_key
= smb
->session_key
;
401 struct pipe_open_smb2_state
{
402 struct dcecli_connection
*c
;
403 struct composite_context
*ctx
;
406 static void pipe_open_recv(struct smb2_request
*req
);
408 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
409 struct smb2_tree
*tree
,
410 const char *pipe_name
)
412 struct composite_context
*ctx
;
413 struct pipe_open_smb2_state
*state
;
414 struct smb2_create io
;
415 struct smb2_request
*req
;
416 struct dcecli_connection
*c
= p
->conn
;
418 ctx
= composite_create(c
, c
->event_ctx
);
419 if (ctx
== NULL
) return NULL
;
421 state
= talloc(ctx
, struct pipe_open_smb2_state
);
422 if (composite_nomem(state
, ctx
)) return ctx
;
423 ctx
->private_data
= state
;
429 io
.in
.desired_access
=
430 SEC_STD_READ_CONTROL
|
431 SEC_FILE_READ_ATTRIBUTE
|
432 SEC_FILE_WRITE_ATTRIBUTE
|
433 SEC_STD_SYNCHRONIZE
|
437 SEC_FILE_WRITE_DATA
|
438 SEC_FILE_APPEND_DATA
;
440 NTCREATEX_SHARE_ACCESS_READ
|
441 NTCREATEX_SHARE_ACCESS_WRITE
;
442 io
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
443 io
.in
.create_options
=
444 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
445 NTCREATEX_OPTIONS_NO_RECALL
;
446 io
.in
.impersonation_level
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
448 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
449 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
452 io
.in
.fname
= pipe_name
;
454 req
= smb2_create_send(tree
, &io
);
455 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
459 static void pipe_open_recv(struct smb2_request
*req
)
461 struct pipe_open_smb2_state
*state
=
462 talloc_get_type(req
->async
.private_data
,
463 struct pipe_open_smb2_state
);
464 struct composite_context
*ctx
= state
->ctx
;
465 struct dcecli_connection
*c
= state
->c
;
466 struct smb2_tree
*tree
= req
->tree
;
467 struct smb2_private
*smb
;
468 struct smb2_create io
;
470 ctx
->status
= smb2_create_recv(req
, state
, &io
);
471 if (!composite_is_ok(ctx
)) return;
474 fill in the transport methods
476 c
->transport
.transport
= NCACN_NP
;
477 c
->transport
.private_data
= NULL
;
478 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
479 c
->transport
.peer_name
= smb2_peer_name
;
480 c
->transport
.target_hostname
= smb2_target_hostname
;
482 c
->transport
.send_request
= smb2_send_request
;
483 c
->transport
.send_read
= send_read_request
;
484 c
->transport
.recv_data
= NULL
;
486 /* Over-ride the default session key with the SMB session key */
487 c
->security_state
.session_key
= smb2_session_key
;
489 smb
= talloc(c
, struct smb2_private
);
490 if (composite_nomem(smb
, ctx
)) return;
492 smb
->handle
= io
.out
.file
.handle
;
493 smb
->tree
= talloc_reference(smb
, tree
);
494 smb
->server_name
= strupper_talloc(smb
,
495 smbXcli_conn_remote_name(tree
->session
->transport
->conn
));
496 if (composite_nomem(smb
->server_name
, ctx
)) return;
499 ctx
->status
= smb2cli_session_application_key(tree
->session
->smbXcli
,
500 smb
, &smb
->session_key
);
501 if (NT_STATUS_EQUAL(ctx
->status
, NT_STATUS_NO_USER_SESSION_KEY
)) {
502 smb
->session_key
= data_blob_null
;
503 ctx
->status
= NT_STATUS_OK
;
505 if (!composite_is_ok(ctx
)) return;
507 c
->transport
.private_data
= smb
;
512 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
514 NTSTATUS status
= composite_wait(c
);
519 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
520 struct smb2_tree
*tree
,
521 const char *pipe_name
)
523 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
524 return dcerpc_pipe_open_smb2_recv(ctx
);
528 return the SMB2 tree used for a dcerpc over SMB2 pipe
530 struct smb2_tree
*dcerpc_smb2_tree(struct dcecli_connection
*c
)
532 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
533 struct smb2_private
);