2 Unix SMB/CIFS implementation.
4 dcerpc over SMB transport
6 Copyright (C) Tim Potter 2003
7 Copyright (C) Andrew Tridgell 2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "librpc/rpc/dcerpc.h"
28 /* transport private information used by SMB pipe transport */
31 struct smbcli_tree *tree;
32 const char *server_name;
38 tell the dcerpc layer that the transport is dead
40 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
42 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
50 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
51 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
54 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
55 status = NT_STATUS_END_OF_FILE;
58 if (c->transport.recv_data) {
59 c->transport.recv_data(c, NULL, status);
65 this holds the state of an in-flight call
67 struct smb_read_state {
68 struct dcerpc_connection *c;
69 struct smbcli_request *req;
76 called when a read request has completed
78 static void smb_read_callback(struct smbcli_request *req)
80 struct smb_private *smb;
81 struct smb_read_state *state;
86 state = talloc_get_type(req->async.private, struct smb_read_state);
87 smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
90 status = smb_raw_read_recv(state->req, io);
91 if (NT_STATUS_IS_ERR(status)) {
92 pipe_dead(state->c, status);
97 state->received += io->readx.out.nread;
99 if (state->received < 16) {
100 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
101 (int)state->received));
102 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
107 frag_length = dcerpc_get_frag_length(&state->data);
109 if (frag_length <= state->received) {
110 DATA_BLOB data = state->data;
111 struct dcerpc_connection *c = state->c;
112 data.length = state->received;
113 talloc_steal(state->c, data.data);
115 c->transport.recv_data(c, &data, NT_STATUS_OK);
119 /* initiate another read request, as we only got part of a fragment */
120 state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
122 io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag,
123 frag_length - state->received);
124 io->readx.in.maxcnt = io->readx.in.mincnt;
125 io->readx.out.data = state->data.data + state->received;
127 state->req = smb_raw_read_send(smb->tree, io);
128 if (state->req == NULL) {
129 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
134 state->req->async.fn = smb_read_callback;
135 state->req->async.private = state;
139 trigger a read request from the server, possibly with some initial
140 data in the read buffer
142 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
144 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
146 struct smb_read_state *state;
147 struct smbcli_request *req;
149 state = talloc(smb, struct smb_read_state);
151 return NT_STATUS_NO_MEMORY;
157 state->data = data_blob_talloc(state, NULL, 0x2000);
159 uint32_t frag_length = blob->length>=16?
160 dcerpc_get_frag_length(blob):0x2000;
161 state->received = blob->length;
162 state->data = data_blob_talloc(state, NULL, frag_length);
163 if (!state->data.data) {
165 return NT_STATUS_NO_MEMORY;
167 memcpy(state->data.data, blob->data, blob->length);
170 state->io = talloc(state, union smb_read);
173 io->generic.level = RAW_READ_READX;
174 io->readx.in.file.fnum = smb->fnum;
175 io->readx.in.mincnt = state->data.length - state->received;
176 io->readx.in.maxcnt = io->readx.in.mincnt;
177 io->readx.in.offset = 0;
178 io->readx.in.remaining = 0;
179 io->readx.in.read_for_execute = false;
180 io->readx.out.data = state->data.data + state->received;
181 req = smb_raw_read_send(smb->tree, io);
183 return NT_STATUS_NO_MEMORY;
186 req->async.fn = smb_read_callback;
187 req->async.private = state;
196 trigger a read request from the server
198 static NTSTATUS send_read_request(struct dcerpc_connection *c)
200 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
203 return NT_STATUS_CONNECTION_DISCONNECTED;
206 return send_read_request_continue(c, NULL);
210 this holds the state of an in-flight trans call
212 struct smb_trans_state {
213 struct dcerpc_connection *c;
214 struct smbcli_request *req;
215 struct smb_trans2 *trans;
219 called when a trans request has completed
221 static void smb_trans_callback(struct smbcli_request *req)
223 struct smb_trans_state *state = (struct smb_trans_state *)req->async.private;
224 struct dcerpc_connection *c = state->c;
227 status = smb_raw_trans_recv(req, state, state->trans);
229 if (NT_STATUS_IS_ERR(status)) {
230 pipe_dead(c, status);
234 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
235 DATA_BLOB data = state->trans->out.data;
236 talloc_steal(c, data.data);
238 c->transport.recv_data(c, &data, NT_STATUS_OK);
242 /* there is more to receive - setup a readx */
243 send_read_request_continue(c, &state->trans->out.data);
248 send a SMBtrans style request
250 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
252 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
253 struct smb_trans2 *trans;
255 struct smb_trans_state *state;
257 state = talloc(smb, struct smb_trans_state);
259 return NT_STATUS_NO_MEMORY;
263 state->trans = talloc(state, struct smb_trans2);
264 trans = state->trans;
266 trans->in.data = *blob;
267 trans->in.params = data_blob(NULL, 0);
269 setup[0] = TRANSACT_DCERPCCMD;
270 setup[1] = smb->fnum;
272 trans->in.max_param = 0;
273 trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
274 trans->in.max_setup = 0;
275 trans->in.setup_count = 2;
277 trans->in.timeout = 0;
278 trans->in.setup = setup;
279 trans->in.trans_name = "\\PIPE\\";
281 state->req = smb_raw_trans_send(smb->tree, trans);
282 if (state->req == NULL) {
284 return NT_STATUS_NO_MEMORY;
287 state->req->async.fn = smb_trans_callback;
288 state->req->async.private = state;
290 talloc_steal(state, state->req);
296 called when a write request has completed
298 static void smb_write_callback(struct smbcli_request *req)
300 struct dcerpc_connection *c = (struct dcerpc_connection *)req->async.private;
302 if (!NT_STATUS_IS_OK(req->status)) {
303 DEBUG(0,("dcerpc_smb: write callback error\n"));
304 pipe_dead(c, req->status);
307 smbcli_request_destroy(req);
311 send a packet to the server
313 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob,
316 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
318 struct smbcli_request *req;
321 return NT_STATUS_CONNECTION_DISCONNECTED;
325 return smb_send_trans_request(c, blob);
328 io.generic.level = RAW_WRITE_WRITEX;
329 io.writex.in.file.fnum = smb->fnum;
330 io.writex.in.offset = 0;
331 io.writex.in.wmode = PIPE_START_MESSAGE;
332 io.writex.in.remaining = blob->length;
333 io.writex.in.count = blob->length;
334 io.writex.in.data = blob->data;
336 /* we must not timeout at the smb level for rpc requests, as otherwise
337 signing/sealing can be messed up */
338 smb->tree->session->transport->options.request_timeout = 0;
340 req = smb_raw_write_send(smb->tree, &io);
342 return NT_STATUS_NO_MEMORY;
345 req->async.fn = smb_write_callback;
346 req->async.private = c;
349 send_read_request(c);
356 shutdown SMB pipe connection
358 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
360 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
362 struct smbcli_request *req;
364 /* maybe we're still starting up */
365 if (!smb) return status;
367 io.close.level = RAW_CLOSE_CLOSE;
368 io.close.in.file.fnum = smb->fnum;
369 io.close.in.write_time = 0;
370 req = smb_raw_close_send(smb->tree, &io);
372 /* we don't care if this fails, so just free it if it succeeds */
373 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
382 return SMB server name (called name)
384 static const char *smb_peer_name(struct dcerpc_connection *c)
386 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
387 return smb->server_name;
391 return remote name we make the actual connection (good for kerberos)
393 static const char *smb_target_hostname(struct dcerpc_connection *c)
395 struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
396 return smb->tree->session->transport->socket->hostname;
400 fetch the user session key
402 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
404 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
406 if (smb->tree->session->user_session_key.data) {
407 *session_key = smb->tree->session->user_session_key;
410 return NT_STATUS_NO_USER_SESSION_KEY;
413 struct pipe_open_smb_state {
414 union smb_open *open;
415 struct dcerpc_connection *c;
416 struct smbcli_tree *tree;
417 struct composite_context *ctx;
420 static void pipe_open_recv(struct smbcli_request *req);
422 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p,
423 struct smbcli_tree *tree,
424 const char *pipe_name)
426 struct composite_context *ctx;
427 struct pipe_open_smb_state *state;
428 struct smbcli_request *req;
429 struct dcerpc_connection *c = p->conn;
431 /* if we don't have a binding on this pipe yet, then create one */
432 if (p->binding == NULL) {
435 SMB_ASSERT(tree->session->transport->socket->hostname != NULL);
436 s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
437 if (s == NULL) return NULL;
438 status = dcerpc_parse_binding(p, s, &p->binding);
440 if (!NT_STATUS_IS_OK(status)) {
445 ctx = composite_create(c, c->event_ctx);
446 if (ctx == NULL) return NULL;
448 state = talloc(ctx, struct pipe_open_smb_state);
449 if (composite_nomem(state, ctx)) return ctx;
450 ctx->private_data = state;
456 state->open = talloc(state, union smb_open);
457 if (composite_nomem(state->open, ctx)) return ctx;
459 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
460 state->open->ntcreatex.in.flags = 0;
461 state->open->ntcreatex.in.root_fid = 0;
462 state->open->ntcreatex.in.access_mask =
463 SEC_STD_READ_CONTROL |
464 SEC_FILE_WRITE_ATTRIBUTE |
468 state->open->ntcreatex.in.file_attr = 0;
469 state->open->ntcreatex.in.alloc_size = 0;
470 state->open->ntcreatex.in.share_access =
471 NTCREATEX_SHARE_ACCESS_READ |
472 NTCREATEX_SHARE_ACCESS_WRITE;
473 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
474 state->open->ntcreatex.in.create_options = 0;
475 state->open->ntcreatex.in.impersonation =
476 NTCREATEX_IMPERSONATION_IMPERSONATION;
477 state->open->ntcreatex.in.security_flags = 0;
479 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
480 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
483 state->open->ntcreatex.in.fname =
484 (pipe_name[0] == '\\') ?
485 talloc_strdup(state->open, pipe_name) :
486 talloc_asprintf(state->open, "\\%s", pipe_name);
487 if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
489 req = smb_raw_open_send(tree, state->open);
490 composite_continue_smb(ctx, req, pipe_open_recv, state);
494 static void pipe_open_recv(struct smbcli_request *req)
496 struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
497 struct pipe_open_smb_state);
498 struct composite_context *ctx = state->ctx;
499 struct dcerpc_connection *c = state->c;
500 struct smb_private *smb;
502 ctx->status = smb_raw_open_recv(req, state, state->open);
503 if (!composite_is_ok(ctx)) return;
506 fill in the transport methods
508 c->transport.transport = NCACN_NP;
509 c->transport.private_data = NULL;
510 c->transport.shutdown_pipe = smb_shutdown_pipe;
511 c->transport.peer_name = smb_peer_name;
512 c->transport.target_hostname = smb_target_hostname;
514 c->transport.send_request = smb_send_request;
515 c->transport.send_read = send_read_request;
516 c->transport.recv_data = NULL;
518 /* Over-ride the default session key with the SMB session key */
519 c->security_state.session_key = smb_session_key;
521 smb = talloc(c, struct smb_private);
522 if (composite_nomem(smb, ctx)) return;
524 smb->fnum = state->open->ntcreatex.out.file.fnum;
525 smb->tree = talloc_reference(smb, state->tree);
526 smb->server_name= strupper_talloc(smb,
527 state->tree->session->transport->called.name);
528 if (composite_nomem(smb->server_name, ctx)) return;
531 c->transport.private_data = smb;
536 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
538 NTSTATUS status = composite_wait(c);
543 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
544 struct smbcli_tree *tree,
545 const char *pipe_name)
547 struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
549 return dcerpc_pipe_open_smb_recv(ctx);
553 return the SMB tree used for a dcerpc over SMB pipe
555 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
557 struct smb_private *smb;
559 if (c->transport.transport != NCACN_NP) return NULL;
561 smb = talloc_get_type(c->transport.private_data, struct smb_private);
562 if (!smb) return NULL;
568 return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
570 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
572 struct smb_private *smb;
574 if (c->transport.transport != NCACN_NP) return 0;
576 smb = talloc_get_type(c->transport.private_data, struct smb_private);