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/raw/ioctl.h"
28 #include "librpc/rpc/dcerpc.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;
40 tell the dcerpc layer that the transport is dead
42 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
44 struct smb2_private *smb = c->transport.private_data;
52 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
53 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
56 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
57 status = NT_STATUS_END_OF_FILE;
60 if (c->transport.recv_data) {
61 c->transport.recv_data(c, NULL, status);
67 this holds the state of an in-flight call
69 struct smb2_read_state {
70 struct dcerpc_connection *c;
75 called when a read request has completed
77 static void smb2_read_callback(struct smb2_request *req)
79 struct smb2_private *smb;
80 struct smb2_read_state *state;
85 state = talloc_get_type(req->async.private, struct smb2_read_state);
86 smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
88 status = smb2_read_recv(req, state, &io);
89 if (NT_STATUS_IS_ERR(status)) {
90 pipe_dead(state->c, status);
95 if (!data_blob_append(state, &state->data,
96 io.out.data.data, io.out.data.length)) {
97 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
102 if (state->data.length < 16) {
103 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
104 (int)state->data.length));
105 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
110 frag_length = dcerpc_get_frag_length(&state->data);
112 if (frag_length <= state->data.length) {
113 DATA_BLOB data = state->data;
114 struct dcerpc_connection *c = state->c;
115 talloc_steal(c, data.data);
117 c->transport.recv_data(c, &data, NT_STATUS_OK);
121 /* initiate another read request, as we only got part of a fragment */
123 io.in.file.handle = smb->handle;
124 io.in.length = MIN(state->c->srv_max_xmit_frag,
125 frag_length - state->data.length);
126 if (io.in.length < 16) {
130 req = smb2_read_send(smb->tree, &io);
132 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
137 req->async.fn = smb2_read_callback;
138 req->async.private = state;
143 trigger a read request from the server, possibly with some initial
144 data in the read buffer
146 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
148 struct smb2_private *smb = c->transport.private_data;
150 struct smb2_read_state *state;
151 struct smb2_request *req;
153 state = talloc(smb, struct smb2_read_state);
155 return NT_STATUS_NO_MEMORY;
160 state->data = data_blob(NULL, 0);
163 talloc_steal(state, state->data.data);
167 io.in.file.handle = smb->handle;
169 if (state->data.length >= 16) {
170 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
171 io.in.length = frag_length - state->data.length;
173 io.in.length = 0x2000;
176 req = smb2_read_send(smb->tree, &io);
178 return NT_STATUS_NO_MEMORY;
181 req->async.fn = smb2_read_callback;
182 req->async.private = state;
189 trigger a read request from the server
191 static NTSTATUS send_read_request(struct dcerpc_connection *c)
193 struct smb2_private *smb = c->transport.private_data;
196 return NT_STATUS_CONNECTION_DISCONNECTED;
199 return send_read_request_continue(c, NULL);
203 this holds the state of an in-flight trans call
205 struct smb2_trans_state {
206 struct dcerpc_connection *c;
210 called when a trans request has completed
212 static void smb2_trans_callback(struct smb2_request *req)
214 struct smb2_trans_state *state = talloc_get_type(req->async.private,
215 struct smb2_trans_state);
216 struct dcerpc_connection *c = state->c;
218 struct smb2_ioctl io;
220 status = smb2_ioctl_recv(req, state, &io);
221 if (NT_STATUS_IS_ERR(status)) {
222 pipe_dead(c, status);
226 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
227 DATA_BLOB data = io.out.out;
228 talloc_steal(c, data.data);
230 c->transport.recv_data(c, &data, NT_STATUS_OK);
234 /* there is more to receive - setup a read */
235 send_read_request_continue(c, &io.out.out);
240 send a SMBtrans style request, using a named pipe read_write fsctl
242 static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
244 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
245 struct smb2_private);
246 struct smb2_ioctl io;
247 struct smb2_trans_state *state;
248 struct smb2_request *req;
250 state = talloc(smb, struct smb2_trans_state);
252 return NT_STATUS_NO_MEMORY;
258 io.in.file.handle = smb->handle;
259 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
260 io.in.max_response_size = 0x1000;
264 req = smb2_ioctl_send(smb->tree, &io);
267 return NT_STATUS_NO_MEMORY;
270 req->async.fn = smb2_trans_callback;
271 req->async.private = state;
273 talloc_steal(state, req);
279 called when a write request has completed
281 static void smb2_write_callback(struct smb2_request *req)
283 struct dcerpc_connection *c = req->async.private;
285 if (!NT_STATUS_IS_OK(req->status)) {
286 DEBUG(0,("dcerpc_smb2: write callback error\n"));
287 pipe_dead(c, req->status);
290 smb2_request_destroy(req);
294 send a packet to the server
296 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
299 struct smb2_private *smb = c->transport.private_data;
300 struct smb2_write io;
301 struct smb2_request *req;
304 return NT_STATUS_CONNECTION_DISCONNECTED;
308 return smb2_send_trans_request(c, blob);
312 io.in.file.handle = smb->handle;
315 req = smb2_write_send(smb->tree, &io);
317 return NT_STATUS_NO_MEMORY;
320 req->async.fn = smb2_write_callback;
321 req->async.private = c;
327 shutdown SMB pipe connection
329 static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
331 struct smb2_private *smb = c->transport.private_data;
332 struct smb2_close io;
333 struct smb2_request *req;
335 /* maybe we're still starting up */
336 if (!smb) return status;
339 io.in.file.handle = smb->handle;
340 req = smb2_close_send(smb->tree, &io);
342 /* we don't care if this fails, so just free it if it succeeds */
343 req->async.fn = (void (*)(struct smb2_request *))talloc_free;
352 return SMB server name
354 static const char *smb2_peer_name(struct dcerpc_connection *c)
356 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
357 struct smb2_private);
358 return smb->server_name;
362 return remote name we make the actual connection (good for kerberos)
364 static const char *smb2_target_hostname(struct dcerpc_connection *c)
366 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
367 struct smb2_private);
368 return smb->tree->session->transport->socket->hostname;
372 fetch the user session key
374 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
376 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
377 struct smb2_private);
378 *session_key = smb->tree->session->session_key;
379 if (session_key->data == NULL) {
380 return NT_STATUS_NO_USER_SESSION_KEY;
385 struct pipe_open_smb2_state {
386 struct dcerpc_connection *c;
387 struct composite_context *ctx;
390 static void pipe_open_recv(struct smb2_request *req);
392 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p,
393 struct smb2_tree *tree,
394 const char *pipe_name)
396 struct composite_context *ctx;
397 struct pipe_open_smb2_state *state;
398 struct smb2_create io;
399 struct smb2_request *req;
400 struct dcerpc_connection *c = p->conn;
402 ctx = composite_create(c, c->event_ctx);
403 if (ctx == NULL) return NULL;
405 state = talloc(ctx, struct pipe_open_smb2_state);
406 if (composite_nomem(state, ctx)) return ctx;
407 ctx->private_data = state;
414 SEC_STD_READ_CONTROL |
415 SEC_FILE_READ_ATTRIBUTE |
416 SEC_FILE_WRITE_ATTRIBUTE |
417 SEC_STD_SYNCHRONIZE |
421 SEC_FILE_WRITE_DATA |
422 SEC_FILE_APPEND_DATA;
424 NTCREATEX_SHARE_ACCESS_READ |
425 NTCREATEX_SHARE_ACCESS_WRITE;
426 io.in.open_disposition = NTCREATEX_DISP_OPEN;
427 io.in.create_options =
428 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
429 NTCREATEX_OPTIONS_UNKNOWN_400000;
430 io.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
432 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
433 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
436 io.in.fname = pipe_name;
438 req = smb2_create_send(tree, &io);
439 composite_continue_smb2(ctx, req, pipe_open_recv, state);
443 static void pipe_open_recv(struct smb2_request *req)
445 struct pipe_open_smb2_state *state =
446 talloc_get_type(req->async.private,
447 struct pipe_open_smb2_state);
448 struct composite_context *ctx = state->ctx;
449 struct dcerpc_connection *c = state->c;
450 struct smb2_tree *tree = req->tree;
451 struct smb2_private *smb;
452 struct smb2_create io;
454 ctx->status = smb2_create_recv(req, state, &io);
455 if (!composite_is_ok(ctx)) return;
458 fill in the transport methods
460 c->transport.transport = NCACN_NP;
461 c->transport.private_data = NULL;
462 c->transport.shutdown_pipe = smb2_shutdown_pipe;
463 c->transport.peer_name = smb2_peer_name;
464 c->transport.target_hostname = smb2_target_hostname;
466 c->transport.send_request = smb2_send_request;
467 c->transport.send_read = send_read_request;
468 c->transport.recv_data = NULL;
470 /* Over-ride the default session key with the SMB session key */
471 c->security_state.session_key = smb2_session_key;
473 smb = talloc(c, struct smb2_private);
474 if (composite_nomem(smb, ctx)) return;
476 smb->handle = io.out.file.handle;
477 smb->tree = talloc_reference(smb, tree);
478 smb->server_name= strupper_talloc(smb,
479 tree->session->transport->socket->hostname);
480 if (composite_nomem(smb->server_name, ctx)) return;
483 c->transport.private_data = smb;
488 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
490 NTSTATUS status = composite_wait(c);
495 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
496 struct smb2_tree *tree,
497 const char *pipe_name)
499 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
500 return dcerpc_pipe_open_smb2_recv(ctx);
504 return the SMB2 tree used for a dcerpc over SMB2 pipe
506 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
508 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
509 struct smb2_private);