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 "librpc/gen_ndr/ndr_security.h"
27 #include "libcli/composite/composite.h"
29 /* transport private information used by SMB pipe transport */
32 struct smbcli_tree *tree;
33 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 c->transport.recv_data(c, NULL, status);
47 this holds the state of an in-flight call
49 struct smb_read_state {
50 struct dcerpc_connection *c;
51 struct smbcli_request *req;
58 called when a read request has completed
60 static void smb_read_callback(struct smbcli_request *req)
62 struct smb_private *smb;
63 struct smb_read_state *state;
68 state = req->async.private;
69 smb = state->c->transport.private;
72 status = smb_raw_read_recv(state->req, io);
73 if (NT_STATUS_IS_ERR(status)) {
74 pipe_dead(state->c, status);
79 state->received += io->readx.out.nread;
81 if (state->received < 16) {
82 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
83 (int)state->received));
84 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
89 frag_length = dcerpc_get_frag_length(&state->data);
91 if (frag_length <= state->received) {
92 state->data.length = state->received;
93 state->c->transport.recv_data(state->c, &state->data, NT_STATUS_OK);
98 /* initiate another read request, as we only got part of a fragment */
99 state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
101 io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag,
102 frag_length - state->received);
103 io->readx.in.maxcnt = io->readx.in.mincnt;
104 io->readx.out.data = state->data.data + state->received;
106 state->req = smb_raw_read_send(smb->tree, io);
107 if (state->req == NULL) {
108 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
113 state->req->async.fn = smb_read_callback;
114 state->req->async.private = state;
118 trigger a read request from the server, possibly with some initial
119 data in the read buffer
121 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
123 struct smb_private *smb = c->transport.private;
125 struct smb_read_state *state;
126 struct smbcli_request *req;
128 state = talloc(smb, struct smb_read_state);
130 return NT_STATUS_NO_MEMORY;
136 state->data = data_blob_talloc(state, NULL, 0x2000);
138 uint32_t frag_length = blob->length>=16?
139 dcerpc_get_frag_length(blob):0x2000;
140 state->received = blob->length;
141 state->data = data_blob_talloc(state, NULL, frag_length);
142 if (!state->data.data) {
144 return NT_STATUS_NO_MEMORY;
146 memcpy(state->data.data, blob->data, blob->length);
149 state->io = talloc(state, union smb_read);
152 io->generic.level = RAW_READ_READX;
153 io->readx.in.fnum = smb->fnum;
154 io->readx.in.mincnt = state->data.length - state->received;
155 io->readx.in.maxcnt = io->readx.in.mincnt;
156 io->readx.in.offset = 0;
157 io->readx.in.remaining = 0;
158 io->readx.out.data = state->data.data + state->received;
159 req = smb_raw_read_send(smb->tree, io);
161 return NT_STATUS_NO_MEMORY;
164 req->async.fn = smb_read_callback;
165 req->async.private = state;
174 trigger a read request from the server
176 static NTSTATUS send_read_request(struct dcerpc_connection *c)
178 return send_read_request_continue(c, NULL);
182 this holds the state of an in-flight trans call
184 struct smb_trans_state {
185 struct dcerpc_connection *c;
186 struct smbcli_request *req;
187 struct smb_trans2 *trans;
191 called when a trans request has completed
193 static void smb_trans_callback(struct smbcli_request *req)
195 struct smb_trans_state *state = req->async.private;
196 struct dcerpc_connection *c = state->c;
199 status = smb_raw_trans_recv(req, state, state->trans);
201 if (NT_STATUS_IS_ERR(status)) {
202 pipe_dead(c, status);
206 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
207 c->transport.recv_data(c, &state->trans->out.data, NT_STATUS_OK);
212 /* there is more to receive - setup a readx */
213 send_read_request_continue(c, &state->trans->out.data);
218 send a SMBtrans style request
220 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
222 struct smb_private *smb = c->transport.private;
223 struct smb_trans2 *trans;
225 struct smb_trans_state *state;
227 state = talloc(smb, struct smb_trans_state);
229 return NT_STATUS_NO_MEMORY;
233 state->trans = talloc(state, struct smb_trans2);
234 trans = state->trans;
236 trans->in.data = *blob;
237 trans->in.params = data_blob(NULL, 0);
239 setup[0] = TRANSACT_DCERPCCMD;
240 setup[1] = smb->fnum;
242 trans->in.max_param = 0;
243 trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
244 trans->in.max_setup = 0;
245 trans->in.setup_count = 2;
247 trans->in.timeout = 0;
248 trans->in.setup = setup;
249 trans->in.trans_name = "\\PIPE\\";
251 state->req = smb_raw_trans_send(smb->tree, trans);
252 if (state->req == NULL) {
254 return NT_STATUS_NO_MEMORY;
257 state->req->async.fn = smb_trans_callback;
258 state->req->async.private = state;
264 called when a write request has completed
266 static void smb_write_callback(struct smbcli_request *req)
268 struct dcerpc_connection *c = req->async.private;
270 if (!NT_STATUS_IS_OK(req->status)) {
271 DEBUG(0,("dcerpc_smb: write callback error\n"));
272 pipe_dead(c, req->status);
275 smbcli_request_destroy(req);
279 send a packet to the server
281 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
283 struct smb_private *smb = c->transport.private;
285 struct smbcli_request *req;
288 return smb_send_trans_request(c, blob);
291 io.generic.level = RAW_WRITE_WRITEX;
292 io.writex.in.fnum = smb->fnum;
293 io.writex.in.offset = 0;
294 io.writex.in.wmode = PIPE_START_MESSAGE;
295 io.writex.in.remaining = blob->length;
296 io.writex.in.count = blob->length;
297 io.writex.in.data = blob->data;
299 /* we must not timeout at the smb level for rpc requests, as otherwise
300 signing/sealing can be messed up */
301 smb->tree->session->transport->options.request_timeout = 0;
303 req = smb_raw_write_send(smb->tree, &io);
305 return NT_STATUS_NO_MEMORY;
308 req->async.fn = smb_write_callback;
309 req->async.private = c;
312 send_read_request(c);
319 shutdown SMB pipe connection
321 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
323 struct smb_private *smb = c->transport.private;
326 /* maybe we're still starting up */
327 if (!smb) return NT_STATUS_OK;
329 io.close.level = RAW_CLOSE_CLOSE;
330 io.close.in.fnum = smb->fnum;
331 io.close.in.write_time = 0;
332 smb_raw_close(smb->tree, &io);
340 return SMB server name
342 static const char *smb_peer_name(struct dcerpc_connection *c)
344 struct smb_private *smb = c->transport.private;
345 return smb->server_name;
349 fetch the user session key
351 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
353 struct smb_private *smb = c->transport.private;
355 if (smb->tree->session->user_session_key.data) {
356 *session_key = smb->tree->session->user_session_key;
359 return NT_STATUS_NO_USER_SESSION_KEY;
362 struct pipe_open_smb_state {
363 union smb_open *open;
364 struct dcerpc_connection *c;
365 struct smbcli_request *req;
366 struct smbcli_tree *tree;
367 struct composite_context *ctx;
370 static void pipe_open_recv(struct smbcli_request *req);
372 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c,
373 struct smbcli_tree *tree,
374 const char *pipe_name)
376 struct composite_context *ctx;
377 struct pipe_open_smb_state *state;
379 ctx = talloc_zero(NULL, struct composite_context);
380 if (ctx == NULL) goto failed;
381 ctx->state = COMPOSITE_STATE_IN_PROGRESS;
382 ctx->event_ctx = talloc_reference(c, c->event_ctx);
384 state = talloc(ctx, struct pipe_open_smb_state);
385 if (state == NULL) goto failed;
391 state->open = talloc(state, union smb_open);
392 if (state->open == NULL) goto failed;
394 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
395 state->open->ntcreatex.in.flags = 0;
396 state->open->ntcreatex.in.root_fid = 0;
397 state->open->ntcreatex.in.access_mask =
398 SEC_STD_READ_CONTROL |
399 SEC_FILE_WRITE_ATTRIBUTE |
403 state->open->ntcreatex.in.file_attr = 0;
404 state->open->ntcreatex.in.alloc_size = 0;
405 state->open->ntcreatex.in.share_access =
406 NTCREATEX_SHARE_ACCESS_READ |
407 NTCREATEX_SHARE_ACCESS_WRITE;
408 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
409 state->open->ntcreatex.in.create_options = 0;
410 state->open->ntcreatex.in.impersonation =
411 NTCREATEX_IMPERSONATION_IMPERSONATION;
412 state->open->ntcreatex.in.security_flags = 0;
414 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
415 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
418 state->open->ntcreatex.in.fname =
419 (pipe_name[0] == '\\') ?
420 talloc_strdup(state->open, pipe_name) :
421 talloc_asprintf(state->open, "\\%s", pipe_name);
422 if (state->open->ntcreatex.in.fname == NULL) goto failed;
424 state->req = smb_raw_open_send(tree, state->open);
425 if (state->req == NULL) goto failed;
427 state->req->async.fn = pipe_open_recv;
428 state->req->async.private = state;
437 static void pipe_open_recv(struct smbcli_request *req)
439 struct pipe_open_smb_state *state =
440 talloc_get_type(req->async.private,
441 struct pipe_open_smb_state);
442 struct composite_context *ctx = state->ctx;
443 struct dcerpc_connection *c = state->c;
444 struct smb_private *smb;
446 ctx->status = smb_raw_open_recv(req, state, state->open);
447 if (!NT_STATUS_IS_OK(ctx->status)) goto done;
450 fill in the transport methods
452 c->transport.transport = NCACN_NP;
453 c->transport.private = NULL;
454 c->transport.shutdown_pipe = smb_shutdown_pipe;
455 c->transport.peer_name = smb_peer_name;
457 c->transport.send_request = smb_send_request;
458 c->transport.send_read = send_read_request;
459 c->transport.recv_data = NULL;
461 /* Over-ride the default session key with the SMB session key */
462 c->security_state.session_key = smb_session_key;
464 smb = talloc(c, struct smb_private);
466 ctx->status = NT_STATUS_NO_MEMORY;
470 smb->fnum = state->open->ntcreatex.out.fnum;
471 smb->tree = talloc_reference(smb, state->tree);
472 smb->server_name= strupper_talloc(
473 smb, state->tree->session->transport->called.name);
474 if (smb->server_name == NULL) {
475 ctx->status = NT_STATUS_NO_MEMORY;
478 c->transport.private = smb;
480 ctx->status = NT_STATUS_OK;
481 ctx->state = COMPOSITE_STATE_DONE;
484 if (!NT_STATUS_IS_OK(ctx->status)) {
485 ctx->state = COMPOSITE_STATE_ERROR;
487 if ((ctx->state >= COMPOSITE_STATE_DONE) &&
488 (ctx->async.fn != NULL)) {
493 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
495 NTSTATUS status = composite_wait(c);
500 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
501 struct smbcli_tree *tree,
502 const char *pipe_name)
504 struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, tree,
506 return dcerpc_pipe_open_smb_recv(ctx);
510 return the SMB tree used for a dcerpc over SMB pipe
512 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
514 struct smb_private *smb = c->transport.private;
516 if (c->transport.transport != NCACN_NP) {