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.file.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.in.read_for_execute = False;
161 io->readx.out.data = state->data.data + state->received;
162 req = smb_raw_read_send(smb->tree, io);
164 return NT_STATUS_NO_MEMORY;
167 req->async.fn = smb_read_callback;
168 req->async.private = state;
177 trigger a read request from the server
179 static NTSTATUS send_read_request(struct dcerpc_connection *c)
181 return send_read_request_continue(c, NULL);
185 this holds the state of an in-flight trans call
187 struct smb_trans_state {
188 struct dcerpc_connection *c;
189 struct smbcli_request *req;
190 struct smb_trans2 *trans;
194 called when a trans request has completed
196 static void smb_trans_callback(struct smbcli_request *req)
198 struct smb_trans_state *state = req->async.private;
199 struct dcerpc_connection *c = state->c;
202 status = smb_raw_trans_recv(req, state, state->trans);
204 if (NT_STATUS_IS_ERR(status)) {
205 pipe_dead(c, status);
209 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
210 DATA_BLOB data = state->trans->out.data;
211 talloc_steal(c, data.data);
213 c->transport.recv_data(c, &data, NT_STATUS_OK);
217 /* there is more to receive - setup a readx */
218 send_read_request_continue(c, &state->trans->out.data);
223 send a SMBtrans style request
225 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
227 struct smb_private *smb = c->transport.private;
228 struct smb_trans2 *trans;
230 struct smb_trans_state *state;
232 state = talloc(smb, struct smb_trans_state);
234 return NT_STATUS_NO_MEMORY;
238 state->trans = talloc(state, struct smb_trans2);
239 trans = state->trans;
241 trans->in.data = *blob;
242 trans->in.params = data_blob(NULL, 0);
244 setup[0] = TRANSACT_DCERPCCMD;
245 setup[1] = smb->fnum;
247 trans->in.max_param = 0;
248 trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
249 trans->in.max_setup = 0;
250 trans->in.setup_count = 2;
252 trans->in.timeout = 0;
253 trans->in.setup = setup;
254 trans->in.trans_name = "\\PIPE\\";
256 state->req = smb_raw_trans_send(smb->tree, trans);
257 if (state->req == NULL) {
259 return NT_STATUS_NO_MEMORY;
262 state->req->async.fn = smb_trans_callback;
263 state->req->async.private = state;
265 talloc_steal(state, state->req);
271 called when a write request has completed
273 static void smb_write_callback(struct smbcli_request *req)
275 struct dcerpc_connection *c = req->async.private;
277 if (!NT_STATUS_IS_OK(req->status)) {
278 DEBUG(0,("dcerpc_smb: write callback error\n"));
279 pipe_dead(c, req->status);
282 smbcli_request_destroy(req);
286 send a packet to the server
288 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
290 struct smb_private *smb = c->transport.private;
292 struct smbcli_request *req;
295 return smb_send_trans_request(c, blob);
298 io.generic.level = RAW_WRITE_WRITEX;
299 io.writex.in.file.fnum = smb->fnum;
300 io.writex.in.offset = 0;
301 io.writex.in.wmode = PIPE_START_MESSAGE;
302 io.writex.in.remaining = blob->length;
303 io.writex.in.count = blob->length;
304 io.writex.in.data = blob->data;
306 /* we must not timeout at the smb level for rpc requests, as otherwise
307 signing/sealing can be messed up */
308 smb->tree->session->transport->options.request_timeout = 0;
310 req = smb_raw_write_send(smb->tree, &io);
312 return NT_STATUS_NO_MEMORY;
315 req->async.fn = smb_write_callback;
316 req->async.private = c;
319 send_read_request(c);
326 shutdown SMB pipe connection
328 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
330 struct smb_private *smb = c->transport.private;
332 struct smbcli_request *req;
334 /* maybe we're still starting up */
335 if (!smb) return NT_STATUS_OK;
337 io.close.level = RAW_CLOSE_CLOSE;
338 io.close.in.file.fnum = smb->fnum;
339 io.close.in.write_time = 0;
340 req = smb_raw_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 smbcli_request *))talloc_free;
352 return SMB server name
354 static const char *smb_peer_name(struct dcerpc_connection *c)
356 struct smb_private *smb = c->transport.private;
357 return smb->server_name;
361 fetch the user session key
363 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
365 struct smb_private *smb = c->transport.private;
367 if (smb->tree->session->user_session_key.data) {
368 *session_key = smb->tree->session->user_session_key;
371 return NT_STATUS_NO_USER_SESSION_KEY;
374 struct pipe_open_smb_state {
375 union smb_open *open;
376 struct dcerpc_connection *c;
377 struct smbcli_request *req;
378 struct smbcli_tree *tree;
379 struct composite_context *ctx;
382 static void pipe_open_recv(struct smbcli_request *req);
384 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c,
385 struct smbcli_tree *tree,
386 const char *pipe_name)
388 struct composite_context *ctx;
389 struct pipe_open_smb_state *state;
391 ctx = talloc_zero(NULL, struct composite_context);
392 if (ctx == NULL) goto failed;
393 ctx->state = COMPOSITE_STATE_IN_PROGRESS;
394 ctx->event_ctx = talloc_reference(c, c->event_ctx);
396 state = talloc(ctx, struct pipe_open_smb_state);
397 if (state == NULL) goto failed;
403 state->open = talloc(state, union smb_open);
404 if (state->open == NULL) goto failed;
406 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
407 state->open->ntcreatex.in.flags = 0;
408 state->open->ntcreatex.in.root_fid = 0;
409 state->open->ntcreatex.in.access_mask =
410 SEC_STD_READ_CONTROL |
411 SEC_FILE_WRITE_ATTRIBUTE |
415 state->open->ntcreatex.in.file_attr = 0;
416 state->open->ntcreatex.in.alloc_size = 0;
417 state->open->ntcreatex.in.share_access =
418 NTCREATEX_SHARE_ACCESS_READ |
419 NTCREATEX_SHARE_ACCESS_WRITE;
420 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
421 state->open->ntcreatex.in.create_options = 0;
422 state->open->ntcreatex.in.impersonation =
423 NTCREATEX_IMPERSONATION_IMPERSONATION;
424 state->open->ntcreatex.in.security_flags = 0;
426 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
427 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
430 state->open->ntcreatex.in.fname =
431 (pipe_name[0] == '\\') ?
432 talloc_strdup(state->open, pipe_name) :
433 talloc_asprintf(state->open, "\\%s", pipe_name);
434 if (state->open->ntcreatex.in.fname == NULL) goto failed;
436 state->req = smb_raw_open_send(tree, state->open);
437 if (state->req == NULL) goto failed;
439 state->req->async.fn = pipe_open_recv;
440 state->req->async.private = state;
449 static void pipe_open_recv(struct smbcli_request *req)
451 struct pipe_open_smb_state *state =
452 talloc_get_type(req->async.private,
453 struct pipe_open_smb_state);
454 struct composite_context *ctx = state->ctx;
455 struct dcerpc_connection *c = state->c;
456 struct smb_private *smb;
458 ctx->status = smb_raw_open_recv(req, state, state->open);
459 if (!NT_STATUS_IS_OK(ctx->status)) goto done;
462 fill in the transport methods
464 c->transport.transport = NCACN_NP;
465 c->transport.private = NULL;
466 c->transport.shutdown_pipe = smb_shutdown_pipe;
467 c->transport.peer_name = smb_peer_name;
469 c->transport.send_request = smb_send_request;
470 c->transport.send_read = send_read_request;
471 c->transport.recv_data = NULL;
473 /* Over-ride the default session key with the SMB session key */
474 c->security_state.session_key = smb_session_key;
476 smb = talloc(c, struct smb_private);
478 ctx->status = NT_STATUS_NO_MEMORY;
482 smb->fnum = state->open->ntcreatex.out.file.fnum;
483 smb->tree = talloc_reference(smb, state->tree);
484 smb->server_name= strupper_talloc(
485 smb, state->tree->session->transport->called.name);
486 if (smb->server_name == NULL) {
487 ctx->status = NT_STATUS_NO_MEMORY;
490 c->transport.private = smb;
492 ctx->status = NT_STATUS_OK;
493 ctx->state = COMPOSITE_STATE_DONE;
496 if (!NT_STATUS_IS_OK(ctx->status)) {
497 ctx->state = COMPOSITE_STATE_ERROR;
499 if ((ctx->state >= COMPOSITE_STATE_DONE) &&
500 (ctx->async.fn != NULL)) {
505 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
507 NTSTATUS status = composite_wait(c);
512 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
513 struct smbcli_tree *tree,
514 const char *pipe_name)
516 struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, tree,
518 return dcerpc_pipe_open_smb_recv(ctx);
522 return the SMB tree used for a dcerpc over SMB pipe
524 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
526 struct smb_private *smb = c->transport.private;
528 if (c->transport.transport != NCACN_NP) {