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 const char *server_name
;
43 tell the dcerpc layer that the transport is dead
45 static void pipe_dead(struct dcecli_connection
*c
, NTSTATUS status
)
47 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
55 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
56 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
59 if (NT_STATUS_EQUAL(NT_STATUS_OK
, status
)) {
60 status
= NT_STATUS_END_OF_FILE
;
63 if (c
->transport
.recv_data
) {
64 c
->transport
.recv_data(c
, NULL
, status
);
70 this holds the state of an in-flight call
72 struct smb2_read_state
{
73 struct dcecli_connection
*c
;
78 called when a read request has completed
80 static void smb2_read_callback(struct smb2_request
*req
)
82 struct dcecli_connection
*c
;
83 struct smb2_private
*smb
;
84 struct smb2_read_state
*state
;
89 state
= talloc_get_type(req
->async
.private_data
, struct smb2_read_state
);
90 smb
= talloc_get_type(state
->c
->transport
.private_data
, struct smb2_private
);
93 status
= smb2_read_recv(req
, state
, &io
);
94 if (NT_STATUS_IS_ERR(status
)) {
100 if (!data_blob_append(state
, &state
->data
,
101 io
.out
.data
.data
, io
.out
.data
.length
)) {
103 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
107 if (state
->data
.length
< 16) {
108 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
109 (int)state
->data
.length
));
111 pipe_dead(c
, NT_STATUS_INFO_LENGTH_MISMATCH
);
115 frag_length
= dcerpc_get_frag_length(&state
->data
);
117 if (frag_length
<= state
->data
.length
) {
118 DATA_BLOB data
= state
->data
;
119 talloc_steal(c
, data
.data
);
121 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
125 /* initiate another read request, as we only got part of a fragment */
127 io
.in
.file
.handle
= smb
->handle
;
128 io
.in
.length
= MIN(state
->c
->srv_max_xmit_frag
,
129 frag_length
- state
->data
.length
);
130 if (io
.in
.length
< 16) {
134 req
= smb2_read_send(smb
->tree
, &io
);
137 pipe_dead(c
, NT_STATUS_NO_MEMORY
);
141 req
->async
.fn
= smb2_read_callback
;
142 req
->async
.private_data
= state
;
147 trigger a read request from the server, possibly with some initial
148 data in the read buffer
150 static NTSTATUS
send_read_request_continue(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
152 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
154 struct smb2_read_state
*state
;
155 struct smb2_request
*req
;
157 state
= talloc(c
, struct smb2_read_state
);
159 return NT_STATUS_NO_MEMORY
;
164 state
->data
= data_blob(NULL
, 0);
167 talloc_steal(state
, state
->data
.data
);
171 io
.in
.file
.handle
= smb
->handle
;
173 if (state
->data
.length
>= 16) {
174 uint16_t frag_length
= dcerpc_get_frag_length(&state
->data
);
175 io
.in
.length
= frag_length
- state
->data
.length
;
177 io
.in
.length
= 0x2000;
180 req
= smb2_read_send(smb
->tree
, &io
);
182 return NT_STATUS_NO_MEMORY
;
185 req
->async
.fn
= smb2_read_callback
;
186 req
->async
.private_data
= state
;
193 trigger a read request from the server
195 static NTSTATUS
send_read_request(struct dcecli_connection
*c
)
197 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
200 return NT_STATUS_CONNECTION_DISCONNECTED
;
203 return send_read_request_continue(c
, NULL
);
207 this holds the state of an in-flight trans call
209 struct smb2_trans_state
{
210 struct dcecli_connection
*c
;
214 called when a trans request has completed
216 static void smb2_trans_callback(struct smb2_request
*req
)
218 struct smb2_trans_state
*state
= talloc_get_type(req
->async
.private_data
,
219 struct smb2_trans_state
);
220 struct dcecli_connection
*c
= state
->c
;
222 struct smb2_ioctl io
;
224 status
= smb2_ioctl_recv(req
, state
, &io
);
225 if (NT_STATUS_IS_ERR(status
)) {
226 pipe_dead(c
, status
);
230 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
231 DATA_BLOB data
= io
.out
.out
;
232 talloc_steal(c
, data
.data
);
234 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
238 /* there is more to receive - setup a read */
239 send_read_request_continue(c
, &io
.out
.out
);
244 send a SMBtrans style request, using a named pipe read_write fsctl
246 static NTSTATUS
smb2_send_trans_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
)
248 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
249 struct smb2_private
);
250 struct smb2_ioctl io
;
251 struct smb2_trans_state
*state
;
252 struct smb2_request
*req
;
254 state
= talloc(smb
, struct smb2_trans_state
);
256 return NT_STATUS_NO_MEMORY
;
262 io
.in
.file
.handle
= smb
->handle
;
263 io
.in
.function
= FSCTL_NAMED_PIPE_READ_WRITE
;
264 io
.in
.max_response_size
= 0x2000;
268 req
= smb2_ioctl_send(smb
->tree
, &io
);
271 return NT_STATUS_NO_MEMORY
;
274 req
->async
.fn
= smb2_trans_callback
;
275 req
->async
.private_data
= state
;
277 talloc_steal(state
, req
);
283 called when a write request has completed
285 static void smb2_write_callback(struct smb2_request
*req
)
287 struct dcecli_connection
*c
= (struct dcecli_connection
*)req
->async
.private_data
;
288 struct smb2_write io
;
293 status
= smb2_write_recv(req
, &io
);
294 if (!NT_STATUS_IS_OK(status
)) {
295 DEBUG(0,("dcerpc_smb2: write callback error: %s\n",
297 pipe_dead(c
, status
);
302 send a packet to the server
304 static NTSTATUS
smb2_send_request(struct dcecli_connection
*c
, DATA_BLOB
*blob
,
307 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
308 struct smb2_write io
;
309 struct smb2_request
*req
;
312 return NT_STATUS_CONNECTION_DISCONNECTED
;
316 return smb2_send_trans_request(c
, blob
);
320 io
.in
.file
.handle
= smb
->handle
;
323 req
= smb2_write_send(smb
->tree
, &io
);
325 return NT_STATUS_NO_MEMORY
;
328 req
->async
.fn
= smb2_write_callback
;
329 req
->async
.private_data
= c
;
334 static void free_request(struct smb2_request
*req
)
340 shutdown SMB pipe connection
342 static NTSTATUS
smb2_shutdown_pipe(struct dcecli_connection
*c
, NTSTATUS status
)
344 struct smb2_private
*smb
= (struct smb2_private
*)c
->transport
.private_data
;
345 struct smb2_close io
;
346 struct smb2_request
*req
;
348 /* maybe we're still starting up */
349 if (!smb
) return status
;
352 io
.in
.file
.handle
= smb
->handle
;
353 req
= smb2_close_send(smb
->tree
, &io
);
355 /* we don't care if this fails, so just free it if it succeeds */
356 req
->async
.fn
= free_request
;
365 return SMB server name
367 static const char *smb2_peer_name(struct dcecli_connection
*c
)
369 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
370 struct smb2_private
);
371 return smb
->server_name
;
375 return remote name we make the actual connection (good for kerberos)
377 static const char *smb2_target_hostname(struct dcecli_connection
*c
)
379 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
380 struct smb2_private
);
381 return smbXcli_conn_remote_name(smb
->tree
->session
->transport
->conn
);
385 fetch the user session key
387 static NTSTATUS
smb2_session_key(struct dcecli_connection
*c
, DATA_BLOB
*session_key
)
389 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
390 struct smb2_private
);
391 *session_key
= smb
->tree
->session
->session_key
;
392 if (session_key
->data
== NULL
) {
393 return NT_STATUS_NO_USER_SESSION_KEY
;
398 struct pipe_open_smb2_state
{
399 struct dcecli_connection
*c
;
400 struct composite_context
*ctx
;
403 static void pipe_open_recv(struct smb2_request
*req
);
405 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
406 struct smb2_tree
*tree
,
407 const char *pipe_name
)
409 struct composite_context
*ctx
;
410 struct pipe_open_smb2_state
*state
;
411 struct smb2_create io
;
412 struct smb2_request
*req
;
413 struct dcecli_connection
*c
= p
->conn
;
415 ctx
= composite_create(c
, c
->event_ctx
);
416 if (ctx
== NULL
) return NULL
;
418 state
= talloc(ctx
, struct pipe_open_smb2_state
);
419 if (composite_nomem(state
, ctx
)) return ctx
;
420 ctx
->private_data
= state
;
426 io
.in
.desired_access
=
427 SEC_STD_READ_CONTROL
|
428 SEC_FILE_READ_ATTRIBUTE
|
429 SEC_FILE_WRITE_ATTRIBUTE
|
430 SEC_STD_SYNCHRONIZE
|
434 SEC_FILE_WRITE_DATA
|
435 SEC_FILE_APPEND_DATA
;
437 NTCREATEX_SHARE_ACCESS_READ
|
438 NTCREATEX_SHARE_ACCESS_WRITE
;
439 io
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
440 io
.in
.create_options
=
441 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
442 NTCREATEX_OPTIONS_NO_RECALL
;
443 io
.in
.impersonation_level
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
445 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
446 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
449 io
.in
.fname
= pipe_name
;
451 req
= smb2_create_send(tree
, &io
);
452 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
456 static void pipe_open_recv(struct smb2_request
*req
)
458 struct pipe_open_smb2_state
*state
=
459 talloc_get_type(req
->async
.private_data
,
460 struct pipe_open_smb2_state
);
461 struct composite_context
*ctx
= state
->ctx
;
462 struct dcecli_connection
*c
= state
->c
;
463 struct smb2_tree
*tree
= req
->tree
;
464 struct smb2_private
*smb
;
465 struct smb2_create io
;
467 ctx
->status
= smb2_create_recv(req
, state
, &io
);
468 if (!composite_is_ok(ctx
)) return;
471 fill in the transport methods
473 c
->transport
.transport
= NCACN_NP
;
474 c
->transport
.private_data
= NULL
;
475 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
476 c
->transport
.peer_name
= smb2_peer_name
;
477 c
->transport
.target_hostname
= smb2_target_hostname
;
479 c
->transport
.send_request
= smb2_send_request
;
480 c
->transport
.send_read
= send_read_request
;
481 c
->transport
.recv_data
= NULL
;
483 /* Over-ride the default session key with the SMB session key */
484 c
->security_state
.session_key
= smb2_session_key
;
486 smb
= talloc(c
, struct smb2_private
);
487 if (composite_nomem(smb
, ctx
)) return;
489 smb
->handle
= io
.out
.file
.handle
;
490 smb
->tree
= talloc_reference(smb
, tree
);
491 smb
->server_name
= strupper_talloc(smb
,
492 smbXcli_conn_remote_name(tree
->session
->transport
->conn
));
493 if (composite_nomem(smb
->server_name
, ctx
)) return;
496 c
->transport
.private_data
= smb
;
501 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
503 NTSTATUS status
= composite_wait(c
);
508 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
509 struct smb2_tree
*tree
,
510 const char *pipe_name
)
512 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
513 return dcerpc_pipe_open_smb2_recv(ctx
);
517 return the SMB2 tree used for a dcerpc over SMB2 pipe
519 struct smb2_tree
*dcerpc_smb2_tree(struct dcecli_connection
*c
)
521 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private_data
,
522 struct smb2_private
);