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"
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 dcecli_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 dcecli_connection
*c
;
77 called when a read request has completed
79 static void smb2_read_callback(struct smb2_request
*req
)
81 struct dcecli_connection
*c
;
82 struct smb2_private
*smb
;
83 struct smb2_read_state
*state
;
88 state
= talloc_get_type(req
->async
.private_data
, struct smb2_read_state
);
89 smb
= talloc_get_type(state
->c
->transport
.private_data
, struct smb2_private
);
92 status
= smb2_read_recv(req
, state
, &io
);
93 if (NT_STATUS_IS_ERR(status
)) {
99 if (!data_blob_append(state
, &state
->data
,
100 io
.out
.data
.data
, io
.out
.data
.length
)) {
102 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
106 if (state
->data
.length
< 16) {
107 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
108 (int)state
->data
.length
));
110 pipe_dead(c
, NT_STATUS_INFO_LENGTH_MISMATCH
);
114 frag_length
= dcerpc_get_frag_length(&state
->data
);
116 if (frag_length
<= state
->data
.length
) {
117 DATA_BLOB data
= state
->data
;
118 talloc_steal(c
, data
.data
);
120 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
124 /* initiate another read request, as we only got part of a fragment */
126 io
.in
.file
.handle
= smb
->handle
;
127 io
.in
.length
= MIN(state
->c
->srv_max_xmit_frag
,
128 frag_length
- state
->data
.length
);
129 if (io
.in
.length
< 16) {
133 req
= smb2_read_send(smb
->tree
, &io
);
136 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
140 req
->async
.fn
= smb2_read_callback
;
141 req
->async
.private_data
= state
;
146 trigger a read request from the server, possibly with some initial
147 data in the read buffer
149 static NTSTATUS
send_read_request_continue(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
151 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
153 struct smb2_read_state
*state
;
154 struct smb2_request
*req
;
156 state
= talloc(c
, struct smb2_read_state
);
158 return NT_STATUS_NO_MEMORY
;
163 state
->data
= data_blob(NULL
, 0);
166 talloc_steal(state
, state
->data
.data
);
170 io
.in
.file
.handle
= smb
->handle
;
172 if (state
->data
.length
>= 16) {
173 uint16_t frag_length
= dcerpc_get_frag_length(&state
->data
);
174 io
.in
.length
= frag_length
- state
->data
.length
;
176 io
.in
.length
= 0x2000;
179 req
= smb2_read_send(smb
->tree
, &io
);
181 return NT_STATUS_NO_MEMORY
;
184 req
->async
.fn
= smb2_read_callback
;
185 req
->async
.private_data
= state
;
192 trigger a read request from the server
194 static NTSTATUS
send_read_request(struct dcecli_connection
*c
)
196 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
199 return NT_STATUS_CONNECTION_DISCONNECTED
;
202 return send_read_request_continue(c
, NULL
);
206 this holds the state of an in-flight trans call
208 struct smb2_trans_state
{
209 struct dcecli_connection
*c
;
213 called when a trans request has completed
215 static void smb2_trans_callback(struct smb2_request
*req
)
217 struct smb2_trans_state
*state
= talloc_get_type(req
->async
.private_data
,
218 struct smb2_trans_state
);
219 struct dcecli_connection
*c
= state
->c
;
221 struct smb2_ioctl io
;
223 status
= smb2_ioctl_recv(req
, state
, &io
);
224 if (NT_STATUS_IS_ERR(status
)) {
225 pipe_dead(c
, status
);
229 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
230 DATA_BLOB data
= io
.out
.out
;
231 talloc_steal(c
, data
.data
);
233 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
237 /* there is more to receive - setup a read */
238 send_read_request_continue(c
, &io
.out
.out
);
243 send a SMBtrans style request, using a named pipe read_write fsctl
245 static NTSTATUS
smb2_send_trans_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
247 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
248 struct smb2_private
);
249 struct smb2_ioctl io
;
250 struct smb2_trans_state
*state
;
251 struct smb2_request
*req
;
253 state
= talloc(smb
, struct smb2_trans_state
);
255 return NT_STATUS_NO_MEMORY
;
261 io
.in
.file
.handle
= smb
->handle
;
262 io
.in
.function
= FSCTL_NAMED_PIPE_READ_WRITE
;
263 io
.in
.max_response_size
= 0x2000;
267 req
= smb2_ioctl_send(smb
->tree
, &io
);
270 return NT_STATUS_NO_MEMORY
;
273 req
->async
.fn
= smb2_trans_callback
;
274 req
->async
.private_data
= state
;
276 talloc_steal(state
, req
);
282 called when a write request has completed
284 static void smb2_write_callback(struct smb2_request
*req
)
286 struct dcecli_connection
*c
= (struct dcecli_connection
*)req
->async
.private_data
;
287 struct smb2_write io
;
292 status
= smb2_write_recv(req
, &io
);
293 if (!NT_STATUS_IS_OK(status
)) {
294 DEBUG(0,("dcerpc_smb2: write callback error: %s\n",
296 pipe_dead(c
, status
);
301 send a packet to the server
303 static NTSTATUS
smb2_send_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
,
306 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
307 struct smb2_write io
;
308 struct smb2_request
*req
;
311 return NT_STATUS_CONNECTION_DISCONNECTED
;
315 return smb2_send_trans_request(c
, blob
);
319 io
.in
.file
.handle
= smb
->handle
;
322 req
= smb2_write_send(smb
->tree
, &io
);
324 return NT_STATUS_NO_MEMORY
;
327 req
->async
.fn
= smb2_write_callback
;
328 req
->async
.private_data
= c
;
333 static void free_request(struct smb2_request
*req
)
339 shutdown SMB pipe connection
341 static NTSTATUS
smb2_shutdown_pipe(struct dcecli_connection
*c
, NTSTATUS status
)
343 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
344 struct smb2_close io
;
345 struct smb2_request
*req
;
347 /* maybe we're still starting up */
348 if (!smb
) return status
;
351 io
.in
.file
.handle
= smb
->handle
;
352 req
= smb2_close_send(smb
->tree
, &io
);
354 /* we don't care if this fails, so just free it if it succeeds */
355 req
->async
.fn
= free_request
;
364 return SMB server name
366 static const char *smb2_peer_name(struct dcecli_connection
*c
)
368 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
369 struct smb2_private
);
370 return smb
->server_name
;
374 return remote name we make the actual connection (good for kerberos)
376 static const char *smb2_target_hostname(struct dcecli_connection
*c
)
378 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
379 struct smb2_private
);
380 return smb
->tree
->session
->transport
->socket
->hostname
;
384 fetch the user session key
386 static NTSTATUS
smb2_session_key(struct dcecli_connection
*c
, DATA_BLOB
*session_key
)
388 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
389 struct smb2_private
);
390 *session_key
= smb
->tree
->session
->session_key
;
391 if (session_key
->data
== NULL
) {
392 return NT_STATUS_NO_USER_SESSION_KEY
;
397 struct pipe_open_smb2_state
{
398 struct dcecli_connection
*c
;
399 struct composite_context
*ctx
;
402 static void pipe_open_recv(struct smb2_request
*req
);
404 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
405 struct smb2_tree
*tree
,
406 const char *pipe_name
)
408 struct composite_context
*ctx
;
409 struct pipe_open_smb2_state
*state
;
410 struct smb2_create io
;
411 struct smb2_request
*req
;
412 struct dcecli_connection
*c
= p
->conn
;
414 ctx
= composite_create(c
, c
->event_ctx
);
415 if (ctx
== NULL
) return NULL
;
417 state
= talloc(ctx
, struct pipe_open_smb2_state
);
418 if (composite_nomem(state
, ctx
)) return ctx
;
419 ctx
->private_data
= state
;
425 io
.in
.desired_access
=
426 SEC_STD_READ_CONTROL
|
427 SEC_FILE_READ_ATTRIBUTE
|
428 SEC_FILE_WRITE_ATTRIBUTE
|
429 SEC_STD_SYNCHRONIZE
|
433 SEC_FILE_WRITE_DATA
|
434 SEC_FILE_APPEND_DATA
;
436 NTCREATEX_SHARE_ACCESS_READ
|
437 NTCREATEX_SHARE_ACCESS_WRITE
;
438 io
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
439 io
.in
.create_options
=
440 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
441 NTCREATEX_OPTIONS_NO_RECALL
;
442 io
.in
.impersonation_level
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
444 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
445 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
448 io
.in
.fname
= pipe_name
;
450 req
= smb2_create_send(tree
, &io
);
451 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
455 static void pipe_open_recv(struct smb2_request
*req
)
457 struct pipe_open_smb2_state
*state
=
458 talloc_get_type(req
->async
.private_data
,
459 struct pipe_open_smb2_state
);
460 struct composite_context
*ctx
= state
->ctx
;
461 struct dcecli_connection
*c
= state
->c
;
462 struct smb2_tree
*tree
= req
->tree
;
463 struct smb2_private
*smb
;
464 struct smb2_create io
;
466 ctx
->status
= smb2_create_recv(req
, state
, &io
);
467 if (!composite_is_ok(ctx
)) return;
470 fill in the transport methods
472 c
->transport
.transport
= NCACN_NP
;
473 c
->transport
.private_data
= NULL
;
474 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
475 c
->transport
.peer_name
= smb2_peer_name
;
476 c
->transport
.target_hostname
= smb2_target_hostname
;
478 c
->transport
.send_request
= smb2_send_request
;
479 c
->transport
.send_read
= send_read_request
;
480 c
->transport
.recv_data
= NULL
;
482 /* Over-ride the default session key with the SMB session key */
483 c
->security_state
.session_key
= smb2_session_key
;
485 smb
= talloc(c
, struct smb2_private
);
486 if (composite_nomem(smb
, ctx
)) return;
488 smb
->handle
= io
.out
.file
.handle
;
489 smb
->tree
= talloc_reference(smb
, tree
);
490 smb
->server_name
= strupper_talloc(smb
,
491 tree
->session
->transport
->socket
->hostname
);
492 if (composite_nomem(smb
->server_name
, ctx
)) return;
495 c
->transport
.private_data
= smb
;
500 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
502 NTSTATUS status
= composite_wait(c
);
507 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
508 struct smb2_tree
*tree
,
509 const char *pipe_name
)
511 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
512 return dcerpc_pipe_open_smb2_recv(ctx
);
516 return the SMB2 tree used for a dcerpc over SMB2 pipe
518 struct smb2_tree
*dcerpc_smb2_tree(struct dcecli_connection
*c
)
520 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
521 struct smb2_private
);