source: trunk/server/source4/librpc/rpc/dcerpc_smb.c

Last change on this file was 862, checked in by Silvan Scherrer, 11 years ago

Samba Server: update trunk to 3.6.23

File size: 15.5 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 dcerpc over SMB transport
5
6 Copyright (C) Tim Potter 2003
7 Copyright (C) Andrew Tridgell 2003
8
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.
13
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.
18
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/>.
21*/
22
23#include "includes.h"
24#include "libcli/raw/libcliraw.h"
25#include "libcli/composite/composite.h"
26#include "librpc/rpc/dcerpc.h"
27#include "librpc/rpc/dcerpc_proto.h"
28#include "librpc/rpc/rpc_common.h"
29
30/* transport private information used by SMB pipe transport */
31struct smb_private {
32 uint16_t fnum;
33 struct smbcli_tree *tree;
34 const char *server_name;
35 bool dead;
36};
37
38
39/*
40 tell the dcerpc layer that the transport is dead
41*/
42static void pipe_dead(struct dcecli_connection *c, NTSTATUS status)
43{
44 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
45
46 if (smb->dead) {
47 return;
48 }
49
50 smb->dead = true;
51
52 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
53 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
54 }
55
56 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
57 status = NT_STATUS_END_OF_FILE;
58 }
59
60 if (c->transport.recv_data) {
61 c->transport.recv_data(c, NULL, status);
62 }
63}
64
65
66/*
67 this holds the state of an in-flight call
68*/
69struct smb_read_state {
70 struct dcecli_connection *c;
71 struct smbcli_request *req;
72 size_t received;
73 DATA_BLOB data;
74 union smb_read *io;
75};
76
77/*
78 called when a read request has completed
79*/
80static void smb_read_callback(struct smbcli_request *req)
81{
82 struct smb_private *smb;
83 struct smb_read_state *state;
84 union smb_read *io;
85 uint16_t frag_length;
86 NTSTATUS status;
87
88 state = talloc_get_type(req->async.private_data, struct smb_read_state);
89 smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
90 io = state->io;
91
92 status = smb_raw_read_recv(state->req, io);
93 if (NT_STATUS_IS_ERR(status)) {
94 pipe_dead(state->c, status);
95 talloc_free(state);
96 return;
97 }
98
99 state->received += io->readx.out.nread;
100
101 if (state->received < 16) {
102 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
103 (int)state->received));
104 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
105 talloc_free(state);
106 return;
107 }
108
109 frag_length = dcerpc_get_frag_length(&state->data);
110
111 if (frag_length <= state->received) {
112 DATA_BLOB data = state->data;
113 struct dcecli_connection *c = state->c;
114 data.length = state->received;
115 talloc_steal(state->c, data.data);
116 talloc_free(state);
117 c->transport.recv_data(c, &data, NT_STATUS_OK);
118 return;
119 }
120
121 /* initiate another read request, as we only got part of a fragment */
122 state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
123
124 io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag,
125 frag_length - state->received);
126 io->readx.in.maxcnt = io->readx.in.mincnt;
127 io->readx.out.data = state->data.data + state->received;
128
129 state->req = smb_raw_read_send(smb->tree, io);
130 if (state->req == NULL) {
131 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
132 talloc_free(state);
133 return;
134 }
135
136 state->req->async.fn = smb_read_callback;
137 state->req->async.private_data = state;
138}
139
140/*
141 trigger a read request from the server, possibly with some initial
142 data in the read buffer
143*/
144static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLOB *blob)
145{
146 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
147 union smb_read *io;
148 struct smb_read_state *state;
149 struct smbcli_request *req;
150
151 state = talloc(smb, struct smb_read_state);
152 if (state == NULL) {
153 return NT_STATUS_NO_MEMORY;
154 }
155
156 state->c = c;
157 if (blob == NULL) {
158 state->received = 0;
159 state->data = data_blob_talloc(state, NULL, 0x2000);
160 } else {
161 uint32_t frag_length = blob->length>=16?
162 dcerpc_get_frag_length(blob):0x2000;
163
164 if (frag_length < state->data.length) {
165 talloc_free(state);
166 return NT_STATUS_RPC_PROTOCOL_ERROR;
167 }
168
169 state->received = blob->length;
170 state->data = data_blob_talloc(state, NULL, frag_length);
171 if (!state->data.data) {
172 talloc_free(state);
173 return NT_STATUS_NO_MEMORY;
174 }
175 memcpy(state->data.data, blob->data, blob->length);
176 }
177
178 state->io = talloc(state, union smb_read);
179
180 io = state->io;
181 io->generic.level = RAW_READ_READX;
182 io->readx.in.file.fnum = smb->fnum;
183 io->readx.in.mincnt = state->data.length - state->received;
184 io->readx.in.maxcnt = io->readx.in.mincnt;
185 io->readx.in.offset = 0;
186 io->readx.in.remaining = 0;
187 io->readx.in.read_for_execute = false;
188 io->readx.out.data = state->data.data + state->received;
189 req = smb_raw_read_send(smb->tree, io);
190 if (req == NULL) {
191 return NT_STATUS_NO_MEMORY;
192 }
193
194 req->async.fn = smb_read_callback;
195 req->async.private_data = state;
196
197 state->req = req;
198
199 return NT_STATUS_OK;
200}
201
202
203/*
204 trigger a read request from the server
205*/
206static NTSTATUS send_read_request(struct dcecli_connection *c)
207{
208 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
209
210 if (smb->dead) {
211 return NT_STATUS_CONNECTION_DISCONNECTED;
212 }
213
214 return send_read_request_continue(c, NULL);
215}
216
217/*
218 this holds the state of an in-flight trans call
219*/
220struct smb_trans_state {
221 struct dcecli_connection *c;
222 struct smbcli_request *req;
223 struct smb_trans2 *trans;
224};
225
226/*
227 called when a trans request has completed
228*/
229static void smb_trans_callback(struct smbcli_request *req)
230{
231 struct smb_trans_state *state = (struct smb_trans_state *)req->async.private_data;
232 struct dcecli_connection *c = state->c;
233 NTSTATUS status;
234
235 status = smb_raw_trans_recv(req, state, state->trans);
236
237 if (NT_STATUS_IS_ERR(status)) {
238 pipe_dead(c, status);
239 return;
240 }
241
242 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
243 DATA_BLOB data = state->trans->out.data;
244 talloc_steal(c, data.data);
245 talloc_free(state);
246 c->transport.recv_data(c, &data, NT_STATUS_OK);
247 return;
248 }
249
250 /* there is more to receive - setup a readx */
251 send_read_request_continue(c, &state->trans->out.data);
252 talloc_free(state);
253}
254
255/*
256 send a SMBtrans style request
257*/
258static NTSTATUS smb_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
259{
260 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
261 struct smb_trans2 *trans;
262 uint16_t setup[2];
263 struct smb_trans_state *state;
264 uint16_t max_data;
265
266 state = talloc(smb, struct smb_trans_state);
267 if (state == NULL) {
268 return NT_STATUS_NO_MEMORY;
269 }
270
271 state->c = c;
272 state->trans = talloc(state, struct smb_trans2);
273 trans = state->trans;
274
275 trans->in.data = *blob;
276 trans->in.params = data_blob(NULL, 0);
277
278 setup[0] = TRANSACT_DCERPCCMD;
279 setup[1] = smb->fnum;
280
281 if (c->srv_max_xmit_frag > 0) {
282 max_data = MIN(UINT16_MAX, c->srv_max_xmit_frag);
283 } else {
284 max_data = UINT16_MAX;
285 }
286
287 trans->in.max_param = 0;
288 trans->in.max_data = max_data;
289 trans->in.max_setup = 0;
290 trans->in.setup_count = 2;
291 trans->in.flags = 0;
292 trans->in.timeout = 0;
293 trans->in.setup = setup;
294 trans->in.trans_name = "\\PIPE\\";
295
296 state->req = smb_raw_trans_send(smb->tree, trans);
297 if (state->req == NULL) {
298 talloc_free(state);
299 return NT_STATUS_NO_MEMORY;
300 }
301
302 state->req->async.fn = smb_trans_callback;
303 state->req->async.private_data = state;
304
305 talloc_steal(state, state->req);
306
307 return NT_STATUS_OK;
308}
309
310/*
311 called when a write request has completed
312*/
313static void smb_write_callback(struct smbcli_request *req)
314{
315 struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
316
317 if (!NT_STATUS_IS_OK(req->status)) {
318 DEBUG(0,("dcerpc_smb: write callback error\n"));
319 pipe_dead(c, req->status);
320 }
321
322 smbcli_request_destroy(req);
323}
324
325/*
326 send a packet to the server
327*/
328static NTSTATUS smb_send_request(struct dcecli_connection *c, DATA_BLOB *blob,
329 bool trigger_read)
330{
331 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
332 union smb_write io;
333 struct smbcli_request *req;
334
335 if (!smb || smb->dead) {
336 return NT_STATUS_CONNECTION_DISCONNECTED;
337 }
338
339 if (trigger_read) {
340 return smb_send_trans_request(c, blob);
341 }
342
343 io.generic.level = RAW_WRITE_WRITEX;
344 io.writex.in.file.fnum = smb->fnum;
345 io.writex.in.offset = 0;
346 io.writex.in.wmode = PIPE_START_MESSAGE;
347 io.writex.in.remaining = blob->length;
348 io.writex.in.count = blob->length;
349 io.writex.in.data = blob->data;
350
351 /* we must not timeout at the smb level for rpc requests, as otherwise
352 signing/sealing can be messed up */
353 smb->tree->session->transport->options.request_timeout = 0;
354
355 req = smb_raw_write_send(smb->tree, &io);
356 if (req == NULL) {
357 return NT_STATUS_NO_MEMORY;
358 }
359
360 req->async.fn = smb_write_callback;
361 req->async.private_data = c;
362
363 if (trigger_read) {
364 send_read_request(c);
365 }
366
367 return NT_STATUS_OK;
368}
369
370
371static void free_request(struct smbcli_request *req)
372{
373 talloc_free(req);
374}
375
376/*
377 shutdown SMB pipe connection
378*/
379static NTSTATUS smb_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
380{
381 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
382 union smb_close io;
383 struct smbcli_request *req;
384
385 /* maybe we're still starting up */
386 if (!smb) return status;
387
388 io.close.level = RAW_CLOSE_CLOSE;
389 io.close.in.file.fnum = smb->fnum;
390 io.close.in.write_time = 0;
391 req = smb_raw_close_send(smb->tree, &io);
392 if (req != NULL) {
393 /* we don't care if this fails, so just free it if it succeeds */
394 req->async.fn = free_request;
395 }
396
397 talloc_free(smb);
398 c->transport.private_data = NULL;
399
400 return status;
401}
402
403/*
404 return SMB server name (called name)
405*/
406static const char *smb_peer_name(struct dcecli_connection *c)
407{
408 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
409 if (smb == NULL) return "";
410 return smb->server_name;
411}
412
413/*
414 return remote name we make the actual connection (good for kerberos)
415*/
416static const char *smb_target_hostname(struct dcecli_connection *c)
417{
418 struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
419 if (smb == NULL) return "";
420 return smb->tree->session->transport->socket->hostname;
421}
422
423/*
424 fetch the user session key
425*/
426static NTSTATUS smb_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
427{
428 struct smb_private *smb = (struct smb_private *)c->transport.private_data;
429
430 if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
431 if (smb->tree->session->user_session_key.data) {
432 *session_key = smb->tree->session->user_session_key;
433 return NT_STATUS_OK;
434 }
435 return NT_STATUS_NO_USER_SESSION_KEY;
436}
437
438struct pipe_open_smb_state {
439 union smb_open *open;
440 struct dcecli_connection *c;
441 struct smbcli_tree *tree;
442 struct composite_context *ctx;
443};
444
445static void pipe_open_recv(struct smbcli_request *req);
446
447struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p,
448 struct smbcli_tree *tree,
449 const char *pipe_name)
450{
451 struct composite_context *ctx;
452 struct pipe_open_smb_state *state;
453 struct smbcli_request *req;
454 struct dcecli_connection *c = p->conn;
455
456 /* if we don't have a binding on this pipe yet, then create one */
457 if (p->binding == NULL) {
458 NTSTATUS status;
459 char *s;
460 SMB_ASSERT(tree->session->transport->socket->hostname != NULL);
461 s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
462 if (s == NULL) return NULL;
463 status = dcerpc_parse_binding(p, s, &p->binding);
464 talloc_free(s);
465 if (!NT_STATUS_IS_OK(status)) {
466 return NULL;
467 }
468 }
469
470 ctx = composite_create(c, c->event_ctx);
471 if (ctx == NULL) return NULL;
472
473 state = talloc(ctx, struct pipe_open_smb_state);
474 if (composite_nomem(state, ctx)) return ctx;
475 ctx->private_data = state;
476
477 state->c = c;
478 state->tree = tree;
479 state->ctx = ctx;
480
481 state->open = talloc(state, union smb_open);
482 if (composite_nomem(state->open, ctx)) return ctx;
483
484 state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
485 state->open->ntcreatex.in.flags = 0;
486 state->open->ntcreatex.in.root_fid.fnum = 0;
487 state->open->ntcreatex.in.access_mask =
488 SEC_STD_READ_CONTROL |
489 SEC_FILE_WRITE_ATTRIBUTE |
490 SEC_FILE_WRITE_EA |
491 SEC_FILE_READ_DATA |
492 SEC_FILE_WRITE_DATA;
493 state->open->ntcreatex.in.file_attr = 0;
494 state->open->ntcreatex.in.alloc_size = 0;
495 state->open->ntcreatex.in.share_access =
496 NTCREATEX_SHARE_ACCESS_READ |
497 NTCREATEX_SHARE_ACCESS_WRITE;
498 state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
499 state->open->ntcreatex.in.create_options = 0;
500 state->open->ntcreatex.in.impersonation =
501 NTCREATEX_IMPERSONATION_IMPERSONATION;
502 state->open->ntcreatex.in.security_flags = 0;
503
504 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
505 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
506 pipe_name += 6;
507 }
508 state->open->ntcreatex.in.fname =
509 (pipe_name[0] == '\\') ?
510 talloc_strdup(state->open, pipe_name) :
511 talloc_asprintf(state->open, "\\%s", pipe_name);
512 if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
513
514 req = smb_raw_open_send(tree, state->open);
515 composite_continue_smb(ctx, req, pipe_open_recv, state);
516 return ctx;
517}
518
519static void pipe_open_recv(struct smbcli_request *req)
520{
521 struct pipe_open_smb_state *state = talloc_get_type(req->async.private_data,
522 struct pipe_open_smb_state);
523 struct composite_context *ctx = state->ctx;
524 struct dcecli_connection *c = state->c;
525 struct smb_private *smb;
526
527 ctx->status = smb_raw_open_recv(req, state, state->open);
528 if (!composite_is_ok(ctx)) return;
529
530 /*
531 fill in the transport methods
532 */
533 c->transport.transport = NCACN_NP;
534 c->transport.private_data = NULL;
535 c->transport.shutdown_pipe = smb_shutdown_pipe;
536 c->transport.peer_name = smb_peer_name;
537 c->transport.target_hostname = smb_target_hostname;
538
539 c->transport.send_request = smb_send_request;
540 c->transport.send_read = send_read_request;
541 c->transport.recv_data = NULL;
542
543 /* Over-ride the default session key with the SMB session key */
544 c->security_state.session_key = smb_session_key;
545
546 smb = talloc(c, struct smb_private);
547 if (composite_nomem(smb, ctx)) return;
548
549 smb->fnum = state->open->ntcreatex.out.file.fnum;
550 smb->tree = talloc_reference(smb, state->tree);
551 smb->server_name= strupper_talloc(smb,
552 state->tree->session->transport->called.name);
553 if (composite_nomem(smb->server_name, ctx)) return;
554 smb->dead = false;
555
556 c->transport.private_data = smb;
557
558 composite_done(ctx);
559}
560
561NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
562{
563 NTSTATUS status = composite_wait(c);
564 talloc_free(c);
565 return status;
566}
567
568_PUBLIC_ NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
569 struct smbcli_tree *tree,
570 const char *pipe_name)
571{
572 struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
573 pipe_name);
574 return dcerpc_pipe_open_smb_recv(ctx);
575}
576
577/*
578 return the SMB tree used for a dcerpc over SMB pipe
579*/
580_PUBLIC_ struct smbcli_tree *dcerpc_smb_tree(struct dcecli_connection *c)
581{
582 struct smb_private *smb;
583
584 if (c->transport.transport != NCACN_NP) return NULL;
585
586 smb = talloc_get_type(c->transport.private_data, struct smb_private);
587 if (!smb) return NULL;
588
589 return smb->tree;
590}
591
592/*
593 return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
594*/
595_PUBLIC_ uint16_t dcerpc_smb_fnum(struct dcecli_connection *c)
596{
597 struct smb_private *smb;
598
599 if (c->transport.transport != NCACN_NP) return 0;
600
601 smb = talloc_get_type(c->transport.private_data, struct smb_private);
602 if (!smb) return 0;
603
604 return smb->fnum;
605}
Note: See TracBrowser for help on using the repository browser.