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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
28 /* transport private information used by SMB pipe transport */
31 struct smbcli_tree *tree;
32 const char *server_name;
37 tell the dcerpc layer that the transport is dead
39 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
41 c->transport.recv_data(c, NULL, status);
46 this holds the state of an in-flight call
48 struct smb_read_state {
49 struct dcerpc_connection *c;
50 struct smbcli_request *req;
57 called when a read request has completed
59 static void smb_read_callback(struct smbcli_request *req)
61 struct smb_private *smb;
62 struct smb_read_state *state;
67 state = talloc_get_type(req->async.private, struct smb_read_state);
68 smb = talloc_get_type(state->c->transport.private, struct smb_private);
71 status = smb_raw_read_recv(state->req, io);
72 if (NT_STATUS_IS_ERR(status)) {
73 pipe_dead(state->c, status);
78 state->received += io->readx.out.nread;
80 if (state->received < 16) {
81 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
82 (int)state->received));
83 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
88 frag_length = dcerpc_get_frag_length(&state->data);
90 if (frag_length <= state->received) {
91 DATA_BLOB data = state->data;
92 struct dcerpc_connection *c = state->c;
93 data.length = state->received;
94 talloc_steal(state->c, data.data);
96 c->transport.recv_data(c, &data, NT_STATUS_OK);
100 /* initiate another read request, as we only got part of a fragment */
101 state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
103 io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag,
104 frag_length - state->received);
105 io->readx.in.maxcnt = io->readx.in.mincnt;
106 io->readx.out.data = state->data.data + state->received;
108 state->req = smb_raw_read_send(smb->tree, io);
109 if (state->req == NULL) {
110 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
115 state->req->async.fn = smb_read_callback;
116 state->req->async.private = state;
120 trigger a read request from the server, possibly with some initial
121 data in the read buffer
123 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
125 struct smb_private *smb = c->transport.private;
127 struct smb_read_state *state;
128 struct smbcli_request *req;
130 state = talloc(smb, struct smb_read_state);
132 return NT_STATUS_NO_MEMORY;
138 state->data = data_blob_talloc(state, NULL, 0x2000);
140 uint32_t frag_length = blob->length>=16?
141 dcerpc_get_frag_length(blob):0x2000;
142 state->received = blob->length;
143 state->data = data_blob_talloc(state, NULL, frag_length);
144 if (!state->data.data) {
146 return NT_STATUS_NO_MEMORY;
148 memcpy(state->data.data, blob->data, blob->length);
151 state->io = talloc(state, union smb_read);
154 io->generic.level = RAW_READ_READX;
155 io->readx.in.fnum = smb->fnum;
156 io->readx.in.mincnt = state->data.length - state->received;
157 io->readx.in.maxcnt = io->readx.in.mincnt;
158 io->readx.in.offset = 0;
159 io->readx.in.remaining = 0;
160 io->readx.out.data = state->data.data + state->received;
161 req = smb_raw_read_send(smb->tree, io);
163 return NT_STATUS_NO_MEMORY;
166 req->async.fn = smb_read_callback;
167 req->async.private = state;
176 trigger a read request from the server
178 static NTSTATUS send_read_request(struct dcerpc_connection *c)
180 return send_read_request_continue(c, NULL);
184 this holds the state of an in-flight trans call
186 struct smb_trans_state {
187 struct dcerpc_connection *c;
188 struct smbcli_request *req;
189 struct smb_trans2 *trans;
193 called when a trans request has completed
195 static void smb_trans_callback(struct smbcli_request *req)
197 struct smb_trans_state *state = req->async.private;
198 struct dcerpc_connection *c = state->c;
201 status = smb_raw_trans_recv(req, state, state->trans);
203 if (NT_STATUS_IS_ERR(status)) {
204 pipe_dead(c, status);
208 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
209 DATA_BLOB data = state->trans->out.data;
210 talloc_steal(c, data.data);
212 c->transport.recv_data(c, &data, NT_STATUS_OK);
216 /* there is more to receive - setup a readx */
217 send_read_request_continue(c, &state->trans->out.data);
222 send a SMBtrans style request
224 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
226 struct smb_private *smb = c->transport.private;
227 struct smb_trans2 *trans;
229 struct smb_trans_state *state;
231 state = talloc(smb, struct smb_trans_state);
233 return NT_STATUS_NO_MEMORY;
237 state->trans = talloc(state, struct smb_trans2);
238 trans = state->trans;
240 trans->in.data = *blob;
241 trans->in.params = data_blob(NULL, 0);
243 setup[0] = TRANSACT_DCERPCCMD;
244 setup[1] = smb->fnum;
246 trans->in.max_param = 0;
247 trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
248 trans->in.max_setup = 0;
249 trans->in.setup_count = 2;
251 trans->in.timeout = 0;
252 trans->in.setup = setup;
253 trans->in.trans_name = "\\PIPE\\";
255 state->req = smb_raw_trans_send(smb->tree, trans);
256 if (state->req == NULL) {
258 return NT_STATUS_NO_MEMORY;
261 state->req->async.fn = smb_trans_callback;
262 state->req->async.private = state;
264 talloc_steal(state, state->req);
270 called when a write request has completed
272 static void smb_write_callback(struct smbcli_request *req)
274 struct dcerpc_connection *c = req->async.private;
276 if (!NT_STATUS_IS_OK(req->status)) {
277 DEBUG(0,("dcerpc_smb: write callback error\n"));
278 pipe_dead(c, req->status);
281 smbcli_request_destroy(req);
285 send a packet to the server
287 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
289 struct smb_private *smb = c->transport.private;
291 struct smbcli_request *req;
294 return smb_send_trans_request(c, blob);
297 io.generic.level = RAW_WRITE_WRITEX;
298 io.writex.in.fnum = smb->fnum;
299 io.writex.in.offset = 0;
300 io.writex.in.wmode = PIPE_START_MESSAGE;
301 io.writex.in.remaining = blob->length;
302 io.writex.in.count = blob->length;
303 io.writex.in.data = blob->data;
305 /* we must not timeout at the smb level for rpc requests, as otherwise
306 signing/sealing can be messed up */
307 smb->tree->session->transport->options.request_timeout = 0;
309 req = smb_raw_write_send(smb->tree, &io);
311 return NT_STATUS_NO_MEMORY;
314 req->async.fn = smb_write_callback;
315 req->async.private = c;
318 send_read_request(c);
325 shutdown SMB pipe connection
327 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
329 struct smb_private *smb = c->transport.private;
331 struct smbcli_request *req;
333 /* maybe we're still starting up */
334 if (!smb) return NT_STATUS_OK;
336 io.close.level = RAW_CLOSE_CLOSE;
337 io.close.in.fnum = smb->fnum;
338 io.close.in.write_time = 0;
339 req = smb_raw_close_send(smb->tree, &io);
341 /* we don't care if this fails, so just free it if it succeeds */
342 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
351 return SMB server name
353 static const char *smb_peer_name(struct dcerpc_connection *c)
355 struct smb_private *smb = c->transport.private;
356 return smb->server_name;
360 fetch the user session key
362 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
364 struct smb_private *smb = c->transport.private;
366 if (smb->tree->session->user_session_key.data) {
367 *session_key = smb->tree->session->user_session_key;
370 return NT_STATUS_NO_USER_SESSION_KEY;
373 struct pipe_open_smb_state {
374 union smb_open *open;
375 struct dcerpc_connection *c;
376 struct smbcli_request *req;
377 struct smbcli_tree *tree;
378 struct composite_context *ctx;
381 static void pipe_open_recv(struct smbcli_request *req);
383 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c,
384 struct smbcli_tree *tree,
385 const char *pipe_name)
387 struct composite_context *ctx;
388 struct pipe_open_smb_state *state;
390 ctx = talloc_zero(NULL, struct composite_context);
391 if (ctx == NULL) goto failed;
392 ctx->state = COMPOSITE_STATE_IN_PROGRESS;
393 ctx->event_ctx = talloc_reference(c, c->event_ctx);
395 state = talloc(ctx, struct pipe_open_smb_state);
396 if (state == NULL) goto failed;
402 state->open = talloc(state, union smb_open);
403 if (state->open == NULL) goto failed;
405 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
406 state->open->ntcreatex.in.flags = 0;
407 state->open->ntcreatex.in.root_fid = 0;
408 state->open->ntcreatex.in.access_mask =
409 SEC_STD_READ_CONTROL |
410 SEC_FILE_WRITE_ATTRIBUTE |
414 state->open->ntcreatex.in.file_attr = 0;
415 state->open->ntcreatex.in.alloc_size = 0;
416 state->open->ntcreatex.in.share_access =
417 NTCREATEX_SHARE_ACCESS_READ |
418 NTCREATEX_SHARE_ACCESS_WRITE;
419 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
420 state->open->ntcreatex.in.create_options = 0;
421 state->open->ntcreatex.in.impersonation =
422 NTCREATEX_IMPERSONATION_IMPERSONATION;
423 state->open->ntcreatex.in.security_flags = 0;
425 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
426 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
429 state->open->ntcreatex.in.fname =
430 (pipe_name[0] == '\\') ?
431 talloc_strdup(state->open, pipe_name) :
432 talloc_asprintf(state->open, "\\%s", pipe_name);
433 if (state->open->ntcreatex.in.fname == NULL) goto failed;
435 state->req = smb_raw_open_send(tree, state->open);
436 if (state->req == NULL) goto failed;
438 state->req->async.fn = pipe_open_recv;
439 state->req->async.private = state;
448 static void pipe_open_recv(struct smbcli_request *req)
450 struct pipe_open_smb_state *state =
451 talloc_get_type(req->async.private,
452 struct pipe_open_smb_state);
453 struct composite_context *ctx = state->ctx;
454 struct dcerpc_connection *c = state->c;
455 struct smb_private *smb;
457 ctx->status = smb_raw_open_recv(req, state, state->open);
458 if (!NT_STATUS_IS_OK(ctx->status)) goto done;
461 fill in the transport methods
463 c->transport.transport = NCACN_NP;
464 c->transport.private = NULL;
465 c->transport.shutdown_pipe = smb_shutdown_pipe;
466 c->transport.peer_name = smb_peer_name;
468 c->transport.send_request = smb_send_request;
469 c->transport.send_read = send_read_request;
470 c->transport.recv_data = NULL;
472 /* Over-ride the default session key with the SMB session key */
473 c->security_state.session_key = smb_session_key;
475 smb = talloc(c, struct smb_private);
477 ctx->status = NT_STATUS_NO_MEMORY;
481 smb->fnum = state->open->ntcreatex.out.fnum;
482 smb->tree = talloc_reference(smb, state->tree);
483 smb->server_name= strupper_talloc(
484 smb, state->tree->session->transport->called.name);
485 if (smb->server_name == NULL) {
486 ctx->status = NT_STATUS_NO_MEMORY;
489 c->transport.private = smb;
491 ctx->status = NT_STATUS_OK;
492 ctx->state = COMPOSITE_STATE_DONE;
495 if (!NT_STATUS_IS_OK(ctx->status)) {
496 ctx->state = COMPOSITE_STATE_ERROR;
498 if ((ctx->state >= COMPOSITE_STATE_DONE) &&
499 (ctx->async.fn != NULL)) {
504 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
506 NTSTATUS status = composite_wait(c);
511 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
512 struct smbcli_tree *tree,
513 const char *pipe_name)
515 struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, tree,
517 return dcerpc_pipe_open_smb_recv(ctx);
521 return the SMB tree used for a dcerpc over SMB pipe
523 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
525 struct smb_private *smb = c->transport.private;
527 if (c->transport.transport != NCACN_NP) {