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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "libcli/raw/ioctl.h"
29 #include "librpc/rpc/dcerpc.h"
31 /* transport private information used by SMB2 pipe transport */
33 struct smb2_handle handle
;
34 struct smb2_tree
*tree
;
35 const char *server_name
;
40 tell the dcerpc layer that the transport is dead
42 static void pipe_dead(struct dcerpc_connection
*c
, NTSTATUS status
)
44 c
->transport
.recv_data(c
, NULL
, status
);
49 this holds the state of an in-flight call
51 struct smb2_read_state
{
52 struct dcerpc_connection
*c
;
57 called when a read request has completed
59 static void smb2_read_callback(struct smb2_request
*req
)
61 struct smb2_private
*smb
;
62 struct smb2_read_state
*state
;
67 state
= talloc_get_type(req
->async
.private, struct smb2_read_state
);
68 smb
= talloc_get_type(state
->c
->transport
.private, struct smb2_private
);
70 status
= smb2_read_recv(req
, state
, &io
);
71 if (NT_STATUS_IS_ERR(status
)) {
72 pipe_dead(state
->c
, status
);
77 status
= data_blob_append(state
, &state
->data
,
78 io
.out
.data
.data
, io
.out
.data
.length
);
79 if (NT_STATUS_IS_ERR(status
)) {
80 pipe_dead(state
->c
, status
);
85 if (state
->data
.length
< 16) {
86 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
87 (int)state
->data
.length
));
88 pipe_dead(state
->c
, NT_STATUS_INFO_LENGTH_MISMATCH
);
93 frag_length
= dcerpc_get_frag_length(&state
->data
);
95 if (frag_length
<= state
->data
.length
) {
96 DATA_BLOB data
= state
->data
;
97 struct dcerpc_connection
*c
= state
->c
;
98 talloc_steal(c
, data
.data
);
100 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
104 /* initiate another read request, as we only got part of a fragment */
106 io
.in
.file
.handle
= smb
->handle
;
107 io
.in
.length
= MIN(state
->c
->srv_max_xmit_frag
,
108 frag_length
- state
->data
.length
);
109 if (io
.in
.length
< 16) {
113 req
= smb2_read_send(smb
->tree
, &io
);
115 pipe_dead(state
->c
, NT_STATUS_NO_MEMORY
);
120 req
->async
.fn
= smb2_read_callback
;
121 req
->async
.private = state
;
126 trigger a read request from the server, possibly with some initial
127 data in the read buffer
129 static NTSTATUS
send_read_request_continue(struct dcerpc_connection
*c
, DATA_BLOB
*blob
)
131 struct smb2_private
*smb
= c
->transport
.private;
133 struct smb2_read_state
*state
;
134 struct smb2_request
*req
;
136 state
= talloc(smb
, struct smb2_read_state
);
138 return NT_STATUS_NO_MEMORY
;
143 state
->data
= data_blob(NULL
, 0);
146 talloc_steal(state
, state
->data
.data
);
150 io
.in
.file
.handle
= smb
->handle
;
152 if (state
->data
.length
>= 16) {
153 uint16_t frag_length
= dcerpc_get_frag_length(&state
->data
);
154 io
.in
.length
= frag_length
- state
->data
.length
;
156 io
.in
.length
= 0x2000;
159 req
= smb2_read_send(smb
->tree
, &io
);
161 return NT_STATUS_NO_MEMORY
;
164 req
->async
.fn
= smb2_read_callback
;
165 req
->async
.private = state
;
172 trigger a read request from the server
174 static NTSTATUS
send_read_request(struct dcerpc_connection
*c
)
176 return send_read_request_continue(c
, NULL
);
180 this holds the state of an in-flight trans call
182 struct smb2_trans_state
{
183 struct dcerpc_connection
*c
;
187 called when a trans request has completed
189 static void smb2_trans_callback(struct smb2_request
*req
)
191 struct smb2_trans_state
*state
= talloc_get_type(req
->async
.private,
192 struct smb2_trans_state
);
193 struct dcerpc_connection
*c
= state
->c
;
195 struct smb2_ioctl io
;
197 status
= smb2_ioctl_recv(req
, state
, &io
);
198 if (NT_STATUS_IS_ERR(status
)) {
199 pipe_dead(c
, status
);
203 if (!NT_STATUS_EQUAL(status
, STATUS_BUFFER_OVERFLOW
)) {
204 DATA_BLOB data
= io
.out
.out
;
205 talloc_steal(c
, data
.data
);
207 c
->transport
.recv_data(c
, &data
, NT_STATUS_OK
);
211 /* there is more to receive - setup a read */
212 send_read_request_continue(c
, &io
.out
.out
);
217 send a SMBtrans style request, using a named pipe read_write fsctl
219 static NTSTATUS
smb2_send_trans_request(struct dcerpc_connection
*c
, DATA_BLOB
*blob
)
221 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private,
222 struct smb2_private
);
223 struct smb2_ioctl io
;
224 struct smb2_trans_state
*state
;
225 struct smb2_request
*req
;
227 state
= talloc(smb
, struct smb2_trans_state
);
229 return NT_STATUS_NO_MEMORY
;
235 io
.in
.file
.handle
= smb
->handle
;
236 io
.in
.function
= FSCTL_NAMED_PIPE_READ_WRITE
;
237 io
.in
.max_response_size
= 0x1000;
241 req
= smb2_ioctl_send(smb
->tree
, &io
);
244 return NT_STATUS_NO_MEMORY
;
247 req
->async
.fn
= smb2_trans_callback
;
248 req
->async
.private = state
;
250 talloc_steal(state
, req
);
256 called when a write request has completed
258 static void smb2_write_callback(struct smb2_request
*req
)
260 struct dcerpc_connection
*c
= req
->async
.private;
262 if (!NT_STATUS_IS_OK(req
->status
)) {
263 DEBUG(0,("dcerpc_smb2: write callback error\n"));
264 pipe_dead(c
, req
->status
);
267 smb2_request_destroy(req
);
271 send a packet to the server
273 static NTSTATUS
smb2_send_request(struct dcerpc_connection
*c
, DATA_BLOB
*blob
,
276 struct smb2_private
*smb
= c
->transport
.private;
277 struct smb2_write io
;
278 struct smb2_request
*req
;
281 return smb2_send_trans_request(c
, blob
);
285 io
.in
.file
.handle
= smb
->handle
;
288 req
= smb2_write_send(smb
->tree
, &io
);
290 return NT_STATUS_NO_MEMORY
;
293 req
->async
.fn
= smb2_write_callback
;
294 req
->async
.private = c
;
300 shutdown SMB pipe connection
302 static NTSTATUS
smb2_shutdown_pipe(struct dcerpc_connection
*c
)
304 struct smb2_private
*smb
= c
->transport
.private;
305 struct smb2_close io
;
306 struct smb2_request
*req
;
308 /* maybe we're still starting up */
309 if (!smb
) return NT_STATUS_OK
;
312 io
.in
.file
.handle
= smb
->handle
;
313 req
= smb2_close_send(smb
->tree
, &io
);
315 /* we don't care if this fails, so just free it if it succeeds */
316 req
->async
.fn
= (void (*)(struct smb2_request
*))talloc_free
;
325 return SMB server name
327 static const char *smb2_peer_name(struct dcerpc_connection
*c
)
329 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private,
330 struct smb2_private
);
331 return smb
->server_name
;
335 return remote name we make the actual connection (good for kerberos)
337 static const char *smb2_target_hostname(struct dcerpc_connection
*c
)
339 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private,
340 struct smb2_private
);
341 return smb
->tree
->session
->transport
->socket
->hostname
;
345 fetch the user session key
347 static NTSTATUS
smb2_session_key(struct dcerpc_connection
*c
, DATA_BLOB
*session_key
)
349 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private,
350 struct smb2_private
);
351 *session_key
= smb
->tree
->session
->session_key
;
352 if (session_key
->data
== NULL
) {
353 return NT_STATUS_NO_USER_SESSION_KEY
;
358 struct pipe_open_smb2_state
{
359 struct dcerpc_connection
*c
;
360 struct composite_context
*ctx
;
363 static void pipe_open_recv(struct smb2_request
*req
);
365 struct composite_context
*dcerpc_pipe_open_smb2_send(struct dcerpc_pipe
*p
,
366 struct smb2_tree
*tree
,
367 const char *pipe_name
)
369 struct composite_context
*ctx
;
370 struct pipe_open_smb2_state
*state
;
371 struct smb2_create io
;
372 struct smb2_request
*req
;
373 struct dcerpc_connection
*c
= p
->conn
;
375 ctx
= composite_create(c
, c
->event_ctx
);
376 if (ctx
== NULL
) return NULL
;
378 state
= talloc(ctx
, struct pipe_open_smb2_state
);
379 if (composite_nomem(state
, ctx
)) return ctx
;
380 ctx
->private_data
= state
;
387 SEC_STD_READ_CONTROL
|
388 SEC_FILE_READ_ATTRIBUTE
|
389 SEC_FILE_WRITE_ATTRIBUTE
|
390 SEC_STD_SYNCHRONIZE
|
394 SEC_FILE_WRITE_DATA
|
395 SEC_FILE_APPEND_DATA
;
397 NTCREATEX_SHARE_ACCESS_READ
|
398 NTCREATEX_SHARE_ACCESS_WRITE
;
399 io
.in
.open_disposition
= NTCREATEX_DISP_OPEN
;
400 io
.in
.create_options
=
401 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE
|
402 NTCREATEX_OPTIONS_UNKNOWN_400000
;
403 io
.in
.impersonation
= NTCREATEX_IMPERSONATION_IMPERSONATION
;
405 if ((strncasecmp(pipe_name
, "/pipe/", 6) == 0) ||
406 (strncasecmp(pipe_name
, "\\pipe\\", 6) == 0)) {
409 io
.in
.fname
= pipe_name
;
411 req
= smb2_create_send(tree
, &io
);
412 composite_continue_smb2(ctx
, req
, pipe_open_recv
, state
);
416 static void pipe_open_recv(struct smb2_request
*req
)
418 struct pipe_open_smb2_state
*state
=
419 talloc_get_type(req
->async
.private,
420 struct pipe_open_smb2_state
);
421 struct composite_context
*ctx
= state
->ctx
;
422 struct dcerpc_connection
*c
= state
->c
;
423 struct smb2_tree
*tree
= req
->tree
;
424 struct smb2_private
*smb
;
425 struct smb2_create io
;
427 ctx
->status
= smb2_create_recv(req
, state
, &io
);
428 if (!composite_is_ok(ctx
)) return;
431 fill in the transport methods
433 c
->transport
.transport
= NCACN_NP
;
434 c
->transport
.private = NULL
;
435 c
->transport
.shutdown_pipe
= smb2_shutdown_pipe
;
436 c
->transport
.peer_name
= smb2_peer_name
;
437 c
->transport
.target_hostname
= smb2_target_hostname
;
439 c
->transport
.send_request
= smb2_send_request
;
440 c
->transport
.send_read
= send_read_request
;
441 c
->transport
.recv_data
= NULL
;
443 /* Over-ride the default session key with the SMB session key */
444 c
->security_state
.session_key
= smb2_session_key
;
446 smb
= talloc(c
, struct smb2_private
);
447 if (composite_nomem(smb
, ctx
)) return;
449 smb
->handle
= io
.out
.file
.handle
;
450 smb
->tree
= talloc_reference(smb
, tree
);
451 smb
->server_name
= strupper_talloc(smb
,
452 tree
->session
->transport
->socket
->hostname
);
453 if (composite_nomem(smb
->server_name
, ctx
)) return;
455 c
->transport
.private = smb
;
460 NTSTATUS
dcerpc_pipe_open_smb2_recv(struct composite_context
*c
)
462 NTSTATUS status
= composite_wait(c
);
467 NTSTATUS
dcerpc_pipe_open_smb2(struct dcerpc_pipe
*p
,
468 struct smb2_tree
*tree
,
469 const char *pipe_name
)
471 struct composite_context
*ctx
= dcerpc_pipe_open_smb2_send(p
, tree
, pipe_name
);
472 return dcerpc_pipe_open_smb2_recv(ctx
);
476 return the SMB2 tree used for a dcerpc over SMB2 pipe
478 struct smb2_tree
*dcerpc_smb2_tree(struct dcerpc_connection
*c
)
480 struct smb2_private
*smb
= talloc_get_type(c
->transport
.private,
481 struct smb2_private
);