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/raw/ioctl.h"
28 #include "librpc/rpc/dcerpc.h"
29 #include "librpc/rpc/dcerpc_proto.h"
30 #include "librpc/rpc/rpc_common.h"
32 /* transport private information used by SMB2 pipe transport */
34 struct smb2_handle handle
;
35 struct smb2_tree
*tree
;
36 const char *server_name
;
42 tell the dcerpc layer that the transport is dead
44 static void pipe_dead(struct dcerpc_connection
*c
, NTSTATUS status
)
46 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
54 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
55 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
58 if (NT_STATUS_EQUAL(NT_STATUS_OK
, status
)) {
59 status
= NT_STATUS_END_OF_FILE
;
62 if (c
->transport
.recv_data
) {
63 c
->transport
.recv_data(c
, NULL
, status
);
69 this holds the state of an in-flight call
71 struct smb2_read_state
{
72 struct dcerpc_connection
*c
;
77 called when a read request has completed
79 static void smb2_read_callback(struct smb2_request
*req
)
81 struct smb2_private
*smb
;
82 struct smb2_read_state
*state
;
87 state
= talloc_get_type(req
->async
.private_data
, struct smb2_read_state
);
88 smb
= talloc_get_type(state
->c
->transport
.private_data
, struct smb2_private
);
90 status
= smb2_read_recv(req
, state
, &io
);
91 if (NT_STATUS_IS_ERR(status
)) {
92 pipe_dead(state
->c
, status
);
97 if (!data_blob_append(state
, &state
->data
,
98 io
.out
.data
.data
, io
.out
.data
.length
)) {
99 pipe_dead(state
->c
, NT_STATUS_NO_MEMORY
);
104 if (state
->data
.length
< 16) {
105 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
106 (int)state
->data
.length
));
107 pipe_dead(state
->c
, NT_STATUS_INFO_LENGTH_MISMATCH
);
112 frag_length
= dcerpc_get_frag_length(&state
->data
);
114 if (frag_length
<= state
->data
.length
) {
115 DATA_BLOB data
= state
->data
;
116 struct dcerpc_connection
*c
= state
->c
;
117 talloc_steal(c
, data
.data
);
119 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
123 /* initiate another read request, as we only got part of a fragment */
125 io
.in
.file
.handle
= smb
->handle
;
126 io
.in
.length
= MIN(state
->c
->srv_max_xmit_frag
,
127 frag_length
- state
->data
.length
);
128 if (io
.in
.length
< 16) {
132 req
= smb2_read_send(smb
->tree
, &io
);
134 pipe_dead(state
->c
, NT_STATUS_NO_MEMORY
);
139 req
->async
.fn
= smb2_read_callback
;
140 req
->async
.private_data
= state
;
145 trigger a read request from the server, possibly with some initial
146 data in the read buffer
148 static NTSTATUS
send_read_request_continue(struct dcerpc_connection
*c
, DATA_BLOB
*blob
)
150 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
152 struct smb2_read_state
*state
;
153 struct smb2_request
*req
;
155 state
= talloc(smb
, struct smb2_read_state
);
157 return NT_STATUS_NO_MEMORY
;
162 state
->data
= data_blob(NULL
, 0);
165 talloc_steal(state
, state
->data
.data
);
169 io
.in
.file
.handle
= smb
->handle
;
171 if (state
->data
.length
>= 16) {
172 uint16_t frag_length
= dcerpc_get_frag_length(&state
->data
);
173 io
.in
.length
= frag_length
- state
->data
.length
;
175 io
.in
.length
= 0x2000;
178 req
= smb2_read_send(smb
->tree
, &io
);
180 return NT_STATUS_NO_MEMORY
;
183 req
->async
.fn
= smb2_read_callback
;
184 req
->async
.private_data
= state
;
191 trigger a read request from the server
193 static NTSTATUS
send_read_request(struct dcerpc_connection
*c
)
195 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
198 return NT_STATUS_CONNECTION_DISCONNECTED
;
201 return send_read_request_continue(c
, NULL
);
205 this holds the state of an in-flight trans call
207 struct smb2_trans_state
{
208 struct dcerpc_connection
*c
;
212 called when a trans request has completed
214 static void smb2_trans_callback(struct smb2_request
*req
)
216 struct smb2_trans_state
*state
= talloc_get_type(req
->async
.private_data
,
217 struct smb2_trans_state
);
218 struct dcerpc_connection
*c
= state
->c
;
220 struct smb2_ioctl io
;
222 status
= smb2_ioctl_recv(req
, state
, &io
);
223 if (NT_STATUS_IS_ERR(status
)) {
224 pipe_dead(c
, status
);
228 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
229 DATA_BLOB data
= io
.out
.out
;
230 talloc_steal(c
, data
.data
);
232 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
236 /* there is more to receive - setup a read */
237 send_read_request_continue(c
, &io
.out
.out
);
242 send a SMBtrans style request, using a named pipe read_write fsctl
244 static NTSTATUS
smb2_send_trans_request(struct dcerpc_connection
*c
, DATA_BLOB
*blob
)
246 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
247 struct smb2_private
);
248 struct smb2_ioctl io
;
249 struct smb2_trans_state
*state
;
250 struct smb2_request
*req
;
252 state
= talloc(smb
, struct smb2_trans_state
);
254 return NT_STATUS_NO_MEMORY
;
260 io
.in
.file
.handle
= smb
->handle
;
261 io
.in
.function
= FSCTL_NAMED_PIPE_READ_WRITE
;
262 io
.in
.max_response_size
= 0x2000;
266 req
= smb2_ioctl_send(smb
->tree
, &io
);
269 return NT_STATUS_NO_MEMORY
;
272 req
->async
.fn
= smb2_trans_callback
;
273 req
->async
.private_data
= state
;
275 talloc_steal(state
, req
);
281 called when a write request has completed
283 static void smb2_write_callback(struct smb2_request
*req
)
285 struct dcerpc_connection
*c
= (struct dcerpc_connection
*)req
->async
.private_data
;
287 if (!NT_STATUS_IS_OK(req
->status
)) {
288 DEBUG(0,("dcerpc_smb2: write callback error\n"));
289 pipe_dead(c
, req
->status
);
292 smb2_request_destroy(req
);
296 send a packet to the server
298 static NTSTATUS
smb2_send_request(struct dcerpc_connection
*c
, DATA_BLOB
*blob
,
301 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
302 struct smb2_write io
;
303 struct smb2_request
*req
;
306 return NT_STATUS_CONNECTION_DISCONNECTED
;
310 return smb2_send_trans_request(c
, blob
);
314 io
.in
.file
.handle
= smb
->handle
;
317 req
= smb2_write_send(smb
->tree
, &io
);
319 return NT_STATUS_NO_MEMORY
;
322 req
->async
.fn
= smb2_write_callback
;
323 req
->async
.private_data
= c
;
328 static void free_request(struct smb2_request
*req
)
334 shutdown SMB pipe connection
336 static NTSTATUS
smb2_shutdown_pipe(struct dcerpc_connection
*c
, NTSTATUS status
)
338 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
339 struct smb2_close io
;
340 struct smb2_request
*req
;
342 /* maybe we're still starting up */
343 if (!smb
) return status
;
346 io
.in
.file
.handle
= smb
->handle
;
347 req
= smb2_close_send(smb
->tree
, &io
);
349 /* we don't care if this fails, so just free it if it succeeds */
350 req
->async
.fn
= free_request
;
359 return SMB server name
361 static const char *smb2_peer_name(struct dcerpc_connection
*c
)
363 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
364 struct smb2_private
);
365 return smb
->server_name
;
369 return remote name we make the actual connection (good for kerberos)
371 static const char *smb2_target_hostname(struct dcerpc_connection
*c
)
373 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
374 struct smb2_private
);
375 return smb
->tree
->session
->transport
->socket
->hostname
;
379 fetch the user session key
381 static NTSTATUS
smb2_session_key(struct dcerpc_connection
*c
, DATA_BLOB
*session_key
)
383 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
384 struct smb2_private
);
385 *session_key
= smb
->tree
->session
->session_key
;
386 if (session_key
->data
== NULL
) {
387 return NT_STATUS_NO_USER_SESSION_KEY
;
392 struct pipe_open_smb2_state
{
393 struct dcerpc_connection
*c
;
394 struct composite_context
*ctx
;
397 static void pipe_open_recv(struct smb2_request
*req
);
399 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
400 struct smb2_tree
*tree
,
401 const char *pipe_name
)
403 struct composite_context
*ctx
;
404 struct pipe_open_smb2_state
*state
;
405 struct smb2_create io
;
406 struct smb2_request
*req
;
407 struct dcerpc_connection
*c
= p
->conn
;
409 ctx
= composite_create(c
, c
->event_ctx
);
410 if (ctx
== NULL
) return NULL
;
412 state
= talloc(ctx
, struct pipe_open_smb2_state
);
413 if (composite_nomem(state
, ctx
)) return ctx
;
414 ctx
->private_data
= state
;
420 io
.in
.desired_access
=
421 SEC_STD_READ_CONTROL
|
422 SEC_FILE_READ_ATTRIBUTE
|
423 SEC_FILE_WRITE_ATTRIBUTE
|
424 SEC_STD_SYNCHRONIZE
|
428 SEC_FILE_WRITE_DATA
|
429 SEC_FILE_APPEND_DATA
;
431 NTCREATEX_SHARE_ACCESS_READ
|
432 NTCREATEX_SHARE_ACCESS_WRITE
;
433 io
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
434 io
.in
.create_options
=
435 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
436 NTCREATEX_OPTIONS_NO_RECALL
;
437 io
.in
.impersonation_level
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
439 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
440 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
443 io
.in
.fname
= pipe_name
;
445 req
= smb2_create_send(tree
, &io
);
446 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
450 static void pipe_open_recv(struct smb2_request
*req
)
452 struct pipe_open_smb2_state
*state
=
453 talloc_get_type(req
->async
.private_data
,
454 struct pipe_open_smb2_state
);
455 struct composite_context
*ctx
= state
->ctx
;
456 struct dcerpc_connection
*c
= state
->c
;
457 struct smb2_tree
*tree
= req
->tree
;
458 struct smb2_private
*smb
;
459 struct smb2_create io
;
461 ctx
->status
= smb2_create_recv(req
, state
, &io
);
462 if (!composite_is_ok(ctx
)) return;
465 fill in the transport methods
467 c
->transport
.transport
= NCACN_NP
;
468 c
->transport
.private_data
= NULL
;
469 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
470 c
->transport
.peer_name
= smb2_peer_name
;
471 c
->transport
.target_hostname
= smb2_target_hostname
;
473 c
->transport
.send_request
= smb2_send_request
;
474 c
->transport
.send_read
= send_read_request
;
475 c
->transport
.recv_data
= NULL
;
477 /* Over-ride the default session key with the SMB session key */
478 c
->security_state
.session_key
= smb2_session_key
;
480 smb
= talloc(c
, struct smb2_private
);
481 if (composite_nomem(smb
, ctx
)) return;
483 smb
->handle
= io
.out
.file
.handle
;
484 smb
->tree
= talloc_reference(smb
, tree
);
485 smb
->server_name
= strupper_talloc(smb
,
486 tree
->session
->transport
->socket
->hostname
);
487 if (composite_nomem(smb
->server_name
, ctx
)) return;
490 c
->transport
.private_data
= smb
;
495 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
497 NTSTATUS status
= composite_wait(c
);
502 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
503 struct smb2_tree
*tree
,
504 const char *pipe_name
)
506 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
507 return dcerpc_pipe_open_smb2_recv(ctx
);
511 return the SMB2 tree used for a dcerpc over SMB2 pipe
513 struct smb2_tree
*dcerpc_smb2_tree(struct dcerpc_connection
*c
)
515 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
516 struct smb2_private
);