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 "librpc/gen_ndr/ndr_security.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb2/smb2.h"
28 #include "libcli/smb2/smb2_calls.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.length = MIN(state->c->srv_max_xmit_frag,
107 frag_length - state->data.length);
108 if (io.in.length < 16) {
111 io.in.handle = smb->handle;
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.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.function = FSCTL_NAMED_PIPE_READ_WRITE;
236 io.in.handle = smb->handle;
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.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.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 fetch the user session key
337 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
339 struct smb2_private *smb = talloc_get_type(c->transport.private,
340 struct smb2_private);
341 *session_key = smb->tree->session->session_key;
342 if (session_key->data == NULL) {
343 return NT_STATUS_NO_USER_SESSION_KEY;
348 struct pipe_open_smb2_state {
349 struct dcerpc_connection *c;
350 struct composite_context *ctx;
353 static void pipe_open_recv(struct smb2_request *req);
355 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c,
356 struct smb2_tree *tree,
357 const char *pipe_name)
359 struct composite_context *ctx;
360 struct pipe_open_smb2_state *state;
361 struct smb2_create io;
362 struct smb2_request *req;
364 ctx = talloc_zero(NULL, struct composite_context);
365 if (ctx == NULL) goto failed;
366 ctx->state = COMPOSITE_STATE_IN_PROGRESS;
367 ctx->event_ctx = talloc_reference(c, c->event_ctx);
369 state = talloc(ctx, struct pipe_open_smb2_state);
370 if (state == NULL) goto failed;
377 SEC_STD_READ_CONTROL |
378 SEC_FILE_READ_ATTRIBUTE |
379 SEC_FILE_WRITE_ATTRIBUTE |
380 SEC_STD_SYNCHRONIZE |
384 SEC_FILE_WRITE_DATA |
385 SEC_FILE_APPEND_DATA;
387 NTCREATEX_SHARE_ACCESS_READ |
388 NTCREATEX_SHARE_ACCESS_WRITE;
389 io.in.open_disposition = NTCREATEX_DISP_OPEN;
390 io.in.create_options =
391 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
392 NTCREATEX_OPTIONS_UNKNOWN_400000;
393 io.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
395 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
396 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
399 io.in.fname = pipe_name;
401 req = smb2_create_send(tree, &io);
402 if (req == NULL) goto failed;
404 req->async.fn = pipe_open_recv;
405 req->async.private = state;
414 static void pipe_open_recv(struct smb2_request *req)
416 struct pipe_open_smb2_state *state =
417 talloc_get_type(req->async.private,
418 struct pipe_open_smb2_state);
419 struct composite_context *ctx = state->ctx;
420 struct dcerpc_connection *c = state->c;
421 struct smb2_tree *tree = req->tree;
422 struct smb2_private *smb;
423 struct smb2_create io;
425 ctx->status = smb2_create_recv(req, state, &io);
426 if (!composite_is_ok(ctx)) return;
429 fill in the transport methods
431 c->transport.transport = NCACN_NP;
432 c->transport.private = NULL;
433 c->transport.shutdown_pipe = smb2_shutdown_pipe;
434 c->transport.peer_name = smb2_peer_name;
436 c->transport.send_request = smb2_send_request;
437 c->transport.send_read = send_read_request;
438 c->transport.recv_data = NULL;
440 /* Over-ride the default session key with the SMB session key */
441 c->security_state.session_key = smb2_session_key;
443 smb = talloc(c, struct smb2_private);
444 if (composite_nomem(smb, ctx)) return;
446 smb->handle = io.out.handle;
447 smb->tree = talloc_reference(smb, tree);
448 smb->server_name= strupper_talloc(smb,
449 tree->session->transport->socket->hostname);
450 if (composite_nomem(smb->server_name, ctx)) return;
452 c->transport.private = smb;
457 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
459 NTSTATUS status = composite_wait(c);
464 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
465 struct smb2_tree *tree,
466 const char *pipe_name)
468 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(c, tree, pipe_name);
469 return dcerpc_pipe_open_smb2_recv(ctx);
473 return the SMB2 tree used for a dcerpc over SMB2 pipe
475 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
477 struct smb2_private *smb = talloc_get_type(c->transport.private,
478 struct smb2_private);