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, BOOL trigger_read)
315 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
317 struct smbcli_request *req;
320 return NT_STATUS_CONNECTION_DISCONNECTED;
324 return smb_send_trans_request(c, blob);
327 io.generic.level = RAW_WRITE_WRITEX;
328 io.writex.in.file.fnum = smb->fnum;
329 io.writex.in.offset = 0;
330 io.writex.in.wmode = PIPE_START_MESSAGE;
331 io.writex.in.remaining = blob->length;
332 io.writex.in.count = blob->length;
333 io.writex.in.data = blob->data;
335 /* we must not timeout at the smb level for rpc requests, as otherwise
336 signing/sealing can be messed up */
337 smb->tree->session->transport->options.request_timeout = 0;
339 req = smb_raw_write_send(smb->tree, &io);
341 return NT_STATUS_NO_MEMORY;
344 req->async.fn = smb_write_callback;
345 req->async.private = c;
348 send_read_request(c);
355 shutdown SMB pipe connection
357 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
359 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
361 struct smbcli_request *req;
363 /* maybe we're still starting up */
364 if (!smb) return status;
366 io.close.level = RAW_CLOSE_CLOSE;
367 io.close.in.file.fnum = smb->fnum;
368 io.close.in.write_time = 0;
369 req = smb_raw_close_send(smb->tree, &io);
371 /* we don't care if this fails, so just free it if it succeeds */
372 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
381 return SMB server name (called name)
383 static const char *smb_peer_name(struct dcerpc_connection *c)
385 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
386 return smb->server_name;
390 return remote name we make the actual connection (good for kerberos)
392 static const char *smb_target_hostname(struct dcerpc_connection *c)
394 struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
395 return smb->tree->session->transport->socket->hostname;
399 fetch the user session key
401 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
403 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
405 if (smb->tree->session->user_session_key.data) {
406 *session_key = smb->tree->session->user_session_key;
409 return NT_STATUS_NO_USER_SESSION_KEY;
412 struct pipe_open_smb_state {
413 union smb_open *open;
414 struct dcerpc_connection *c;
415 struct smbcli_tree *tree;
416 struct composite_context *ctx;
419 static void pipe_open_recv(struct smbcli_request *req);
421 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p,
422 struct smbcli_tree *tree,
423 const char *pipe_name)
425 struct composite_context *ctx;
426 struct pipe_open_smb_state *state;
427 struct smbcli_request *req;
428 struct dcerpc_connection *c = p->conn;
430 /* if we don't have a binding on this pipe yet, then create one */
431 if (p->binding == NULL) {
434 SMB_ASSERT(tree->session->transport->socket->hostname != NULL);
435 s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
436 if (s == NULL) return NULL;
437 status = dcerpc_parse_binding(p, s, &p->binding);
439 if (!NT_STATUS_IS_OK(status)) {
444 ctx = composite_create(c, c->event_ctx);
445 if (ctx == NULL) return NULL;
447 state = talloc(ctx, struct pipe_open_smb_state);
448 if (composite_nomem(state, ctx)) return ctx;
449 ctx->private_data = state;
455 state->open = talloc(state, union smb_open);
456 if (composite_nomem(state->open, ctx)) return ctx;
458 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
459 state->open->ntcreatex.in.flags = 0;
460 state->open->ntcreatex.in.root_fid = 0;
461 state->open->ntcreatex.in.access_mask =
462 SEC_STD_READ_CONTROL |
463 SEC_FILE_WRITE_ATTRIBUTE |
467 state->open->ntcreatex.in.file_attr = 0;
468 state->open->ntcreatex.in.alloc_size = 0;
469 state->open->ntcreatex.in.share_access =
470 NTCREATEX_SHARE_ACCESS_READ |
471 NTCREATEX_SHARE_ACCESS_WRITE;
472 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
473 state->open->ntcreatex.in.create_options = 0;
474 state->open->ntcreatex.in.impersonation =
475 NTCREATEX_IMPERSONATION_IMPERSONATION;
476 state->open->ntcreatex.in.security_flags = 0;
478 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
479 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
482 state->open->ntcreatex.in.fname =
483 (pipe_name[0] == '\\') ?
484 talloc_strdup(state->open, pipe_name) :
485 talloc_asprintf(state->open, "\\%s", pipe_name);
486 if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
488 req = smb_raw_open_send(tree, state->open);
489 composite_continue_smb(ctx, req, pipe_open_recv, state);
493 static void pipe_open_recv(struct smbcli_request *req)
495 struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
496 struct pipe_open_smb_state);
497 struct composite_context *ctx = state->ctx;
498 struct dcerpc_connection *c = state->c;
499 struct smb_private *smb;
501 ctx->status = smb_raw_open_recv(req, state, state->open);
502 if (!composite_is_ok(ctx)) return;
505 fill in the transport methods
507 c->transport.transport = NCACN_NP;
508 c->transport.private_data = NULL;
509 c->transport.shutdown_pipe = smb_shutdown_pipe;
510 c->transport.peer_name = smb_peer_name;
511 c->transport.target_hostname = smb_target_hostname;
513 c->transport.send_request = smb_send_request;
514 c->transport.send_read = send_read_request;
515 c->transport.recv_data = NULL;
517 /* Over-ride the default session key with the SMB session key */
518 c->security_state.session_key = smb_session_key;
520 smb = talloc(c, struct smb_private);
521 if (composite_nomem(smb, ctx)) return;
523 smb->fnum = state->open->ntcreatex.out.file.fnum;
524 smb->tree = talloc_reference(smb, state->tree);
525 smb->server_name= strupper_talloc(smb,
526 state->tree->session->transport->called.name);
527 if (composite_nomem(smb->server_name, ctx)) return;
530 c->transport.private_data = smb;
535 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
537 NTSTATUS status = composite_wait(c);
542 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
543 struct smbcli_tree *tree,
544 const char *pipe_name)
546 struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
548 return dcerpc_pipe_open_smb_recv(ctx);
552 return the SMB tree used for a dcerpc over SMB pipe
554 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
556 struct smb_private *smb;
558 if (c->transport.transport != NCACN_NP) return NULL;
560 smb = talloc_get_type(c->transport.private_data, struct smb_private);
561 if (!smb) return NULL;
567 return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
569 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
571 struct smb_private *smb;
573 if (c->transport.transport != NCACN_NP) return 0;
575 smb = talloc_get_type(c->transport.private_data, struct smb_private);