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"
30 /* transport private information used by SMB2 pipe transport */
32 struct smb2_handle handle;
33 struct smb2_tree *tree;
34 const char *server_name;
39 tell the dcerpc layer that the transport is dead
41 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
43 c->transport.recv_data(c, NULL, status);
48 this holds the state of an in-flight call
50 struct smb2_read_state {
51 struct dcerpc_connection *c;
56 called when a read request has completed
58 static void smb2_read_callback(struct smb2_request *req)
60 struct smb2_private *smb;
61 struct smb2_read_state *state;
66 state = talloc_get_type(req->async.private, struct smb2_read_state);
67 smb = talloc_get_type(state->c->transport.private, struct smb2_private);
69 status = smb2_read_recv(req, state, &io);
70 if (NT_STATUS_IS_ERR(status)) {
71 pipe_dead(state->c, status);
76 status = data_blob_append(state, &state->data,
77 io.out.data.data, io.out.data.length);
78 if (NT_STATUS_IS_ERR(status)) {
79 pipe_dead(state->c, status);
84 if (state->data.length < 16) {
85 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
86 (int)state->data.length));
87 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
92 frag_length = dcerpc_get_frag_length(&state->data);
94 if (frag_length <= state->data.length) {
95 DATA_BLOB data = state->data;
96 struct dcerpc_connection *c = state->c;
97 talloc_steal(c, data.data);
99 c->transport.recv_data(c, &data, NT_STATUS_OK);
103 /* initiate another read request, as we only got part of a fragment */
105 io.in.length = MIN(state->c->srv_max_xmit_frag,
106 frag_length - state->data.length);
107 if (io.in.length < 16) {
110 io.in.handle = smb->handle;
112 req = smb2_read_send(smb->tree, &io);
114 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
119 req->async.fn = smb2_read_callback;
120 req->async.private = state;
125 trigger a read request from the server, possibly with some initial
126 data in the read buffer
128 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
130 struct smb2_private *smb = c->transport.private;
132 struct smb2_read_state *state;
133 struct smb2_request *req;
135 state = talloc(smb, struct smb2_read_state);
137 return NT_STATUS_NO_MEMORY;
142 state->data = data_blob(NULL, 0);
145 talloc_steal(state, state->data.data);
149 io.in.handle = smb->handle;
151 if (state->data.length >= 16) {
152 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
153 io.in.length = frag_length - state->data.length;
155 io.in.length = 0x2000;
158 req = smb2_read_send(smb->tree, &io);
160 return NT_STATUS_NO_MEMORY;
163 req->async.fn = smb2_read_callback;
164 req->async.private = state;
171 trigger a read request from the server
173 static NTSTATUS send_read_request(struct dcerpc_connection *c)
175 return send_read_request_continue(c, NULL);
179 this holds the state of an in-flight trans call
181 struct smb2_trans_state {
182 struct dcerpc_connection *c;
186 called when a trans request has completed
188 static void smb2_trans_callback(struct smb2_request *req)
190 struct smb2_trans_state *state = talloc_get_type(req->async.private,
191 struct smb2_trans_state);
192 struct dcerpc_connection *c = state->c;
194 struct smb2_ioctl io;
196 status = smb2_ioctl_recv(req, state, &io);
197 if (NT_STATUS_IS_ERR(status)) {
198 pipe_dead(c, status);
202 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
203 DATA_BLOB data = io.out.out;
204 talloc_steal(c, data.data);
206 c->transport.recv_data(c, &data, NT_STATUS_OK);
210 /* there is more to receive - setup a read */
211 send_read_request_continue(c, &io.out.out);
216 send a SMBtrans style request, using a named pipe read_write fsctl
218 static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
220 struct smb2_private *smb = talloc_get_type(c->transport.private,
221 struct smb2_private);
222 struct smb2_ioctl io;
223 struct smb2_trans_state *state;
224 struct smb2_request *req;
226 state = talloc(smb, struct smb2_trans_state);
228 return NT_STATUS_NO_MEMORY;
234 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
235 io.in.handle = smb->handle;
236 io.in.max_response_size = 0x1000;
240 req = smb2_ioctl_send(smb->tree, &io);
243 return NT_STATUS_NO_MEMORY;
246 req->async.fn = smb2_trans_callback;
247 req->async.private = state;
249 talloc_steal(state, req);
255 called when a write request has completed
257 static void smb2_write_callback(struct smb2_request *req)
259 struct dcerpc_connection *c = req->async.private;
261 if (!NT_STATUS_IS_OK(req->status)) {
262 DEBUG(0,("dcerpc_smb2: write callback error\n"));
263 pipe_dead(c, req->status);
266 smb2_request_destroy(req);
270 send a packet to the server
272 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
275 struct smb2_private *smb = c->transport.private;
276 struct smb2_write io;
277 struct smb2_request *req;
280 return smb2_send_trans_request(c, blob);
284 io.in.handle = smb->handle;
287 req = smb2_write_send(smb->tree, &io);
289 return NT_STATUS_NO_MEMORY;
292 req->async.fn = smb2_write_callback;
293 req->async.private = c;
299 shutdown SMB pipe connection
301 static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
303 struct smb2_private *smb = c->transport.private;
304 struct smb2_close io;
305 struct smb2_request *req;
307 /* maybe we're still starting up */
308 if (!smb) return NT_STATUS_OK;
311 io.in.handle = smb->handle;
312 req = smb2_close_send(smb->tree, &io);
314 /* we don't care if this fails, so just free it if it succeeds */
315 req->async.fn = (void (*)(struct smb2_request *))talloc_free;
324 return SMB server name
326 static const char *smb2_peer_name(struct dcerpc_connection *c)
328 struct smb2_private *smb = talloc_get_type(c->transport.private,
329 struct smb2_private);
330 return smb->server_name;
334 fetch the user session key
336 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
338 struct smb2_private *smb = talloc_get_type(c->transport.private,
339 struct smb2_private);
340 *session_key = smb->tree->session->session_key;
341 if (session_key->data == NULL) {
342 return NT_STATUS_NO_USER_SESSION_KEY;
347 struct pipe_open_smb2_state {
348 struct dcerpc_connection *c;
349 struct composite_context *ctx;
352 static void pipe_open_recv(struct smb2_request *req);
354 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c,
355 struct smb2_tree *tree,
356 const char *pipe_name)
358 struct composite_context *ctx;
359 struct pipe_open_smb2_state *state;
360 struct smb2_create io;
361 struct smb2_request *req;
363 ctx = talloc_zero(NULL, struct composite_context);
364 if (ctx == NULL) goto failed;
365 ctx->state = COMPOSITE_STATE_IN_PROGRESS;
366 ctx->event_ctx = talloc_reference(c, c->event_ctx);
368 state = talloc(ctx, struct pipe_open_smb2_state);
369 if (state == NULL) goto failed;
376 SEC_STD_READ_CONTROL |
377 SEC_FILE_READ_ATTRIBUTE |
378 SEC_FILE_WRITE_ATTRIBUTE |
379 SEC_STD_SYNCHRONIZE |
383 SEC_FILE_WRITE_DATA |
384 SEC_FILE_APPEND_DATA;
386 NTCREATEX_SHARE_ACCESS_READ |
387 NTCREATEX_SHARE_ACCESS_WRITE;
388 io.in.open_disposition = NTCREATEX_DISP_OPEN;
389 io.in.create_options =
390 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
391 NTCREATEX_OPTIONS_UNKNOWN_400000;
392 io.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
394 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
395 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
398 io.in.fname = pipe_name;
400 req = smb2_create_send(tree, &io);
401 if (req == NULL) goto failed;
403 req->async.fn = pipe_open_recv;
404 req->async.private = state;
413 static void pipe_open_recv(struct smb2_request *req)
415 struct pipe_open_smb2_state *state =
416 talloc_get_type(req->async.private,
417 struct pipe_open_smb2_state);
418 struct composite_context *ctx = state->ctx;
419 struct dcerpc_connection *c = state->c;
420 struct smb2_tree *tree = req->tree;
421 struct smb2_private *smb;
422 struct smb2_create io;
424 ctx->status = smb2_create_recv(req, state, &io);
425 if (!composite_is_ok(ctx)) return;
428 fill in the transport methods
430 c->transport.transport = NCACN_NP;
431 c->transport.private = NULL;
432 c->transport.shutdown_pipe = smb2_shutdown_pipe;
433 c->transport.peer_name = smb2_peer_name;
435 c->transport.send_request = smb2_send_request;
436 c->transport.send_read = send_read_request;
437 c->transport.recv_data = NULL;
439 /* Over-ride the default session key with the SMB session key */
440 c->security_state.session_key = smb2_session_key;
442 smb = talloc(c, struct smb2_private);
443 if (composite_nomem(smb, ctx)) return;
445 smb->handle = io.out.handle;
446 smb->tree = talloc_reference(smb, tree);
447 smb->server_name= strupper_talloc(smb,
448 tree->session->transport->socket->hostname);
449 if (composite_nomem(smb->server_name, ctx)) return;
451 c->transport.private = smb;
456 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
458 NTSTATUS status = composite_wait(c);
463 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
464 struct smb2_tree *tree,
465 const char *pipe_name)
467 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(c, tree, pipe_name);
468 return dcerpc_pipe_open_smb2_recv(ctx);
472 return the SMB2 tree used for a dcerpc over SMB2 pipe
474 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
476 struct smb2_private *smb = talloc_get_type(c->transport.private,
477 struct smb2_private);