source: vendor/current/source4/librpc/rpc/dcerpc_smb2.c@ 740

Last change on this file since 740 was 740, checked in by Silvan Scherrer, 12 years ago

Samba Server: update vendor to 3.6.0

File size: 13.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 dcerpc over SMB2 transport
5
6 Copyright (C) Andrew Tridgell 2005
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "libcli/raw/libcliraw.h"
24#include "libcli/composite/composite.h"
25#include "libcli/smb2/smb2.h"
26#include "libcli/smb2/smb2_calls.h"
27#include "libcli/raw/ioctl.h"
28#include "librpc/rpc/dcerpc.h"
29#include "librpc/rpc/dcerpc_proto.h"
30#include "librpc/rpc/rpc_common.h"
31
32/* transport private information used by SMB2 pipe transport */
33struct smb2_private {
34 struct smb2_handle handle;
35 struct smb2_tree *tree;
36 const char *server_name;
37 bool dead;
38};
39
40
41/*
42 tell the dcerpc layer that the transport is dead
43*/
44static void pipe_dead(struct dcecli_connection *c, NTSTATUS status)
45{
46 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
47
48 if (smb->dead) {
49 return;
50 }
51
52 smb->dead = true;
53
54 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
55 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
56 }
57
58 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
59 status = NT_STATUS_END_OF_FILE;
60 }
61
62 if (c->transport.recv_data) {
63 c->transport.recv_data(c, NULL, status);
64 }
65}
66
67
68/*
69 this holds the state of an in-flight call
70*/
71struct smb2_read_state {
72 struct dcecli_connection *c;
73 DATA_BLOB data;
74};
75
76/*
77 called when a read request has completed
78*/
79static void smb2_read_callback(struct smb2_request *req)
80{
81 struct smb2_private *smb;
82 struct smb2_read_state *state;
83 struct smb2_read io;
84 uint16_t frag_length;
85 NTSTATUS status;
86
87 state = talloc_get_type(req->async.private_data, struct smb2_read_state);
88 smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
89
90 status = smb2_read_recv(req, state, &io);
91 if (NT_STATUS_IS_ERR(status)) {
92 pipe_dead(state->c, status);
93 talloc_free(state);
94 return;
95 }
96
97 if (!data_blob_append(state, &state->data,
98 io.out.data.data, io.out.data.length)) {
99 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
100 talloc_free(state);
101 return;
102 }
103
104 if (state->data.length < 16) {
105 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
106 (int)state->data.length));
107 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
108 talloc_free(state);
109 return;
110 }
111
112 frag_length = dcerpc_get_frag_length(&state->data);
113
114 if (frag_length <= state->data.length) {
115 DATA_BLOB data = state->data;
116 struct dcecli_connection *c = state->c;
117 talloc_steal(c, data.data);
118 talloc_free(state);
119 c->transport.recv_data(c, &data, NT_STATUS_OK);
120 return;
121 }
122
123 /* initiate another read request, as we only got part of a fragment */
124 ZERO_STRUCT(io);
125 io.in.file.handle = smb->handle;
126 io.in.length = MIN(state->c->srv_max_xmit_frag,
127 frag_length - state->data.length);
128 if (io.in.length < 16) {
129 io.in.length = 16;
130 }
131
132 req = smb2_read_send(smb->tree, &io);
133 if (req == NULL) {
134 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
135 talloc_free(state);
136 return;
137 }
138
139 req->async.fn = smb2_read_callback;
140 req->async.private_data = state;
141}
142
143
144/*
145 trigger a read request from the server, possibly with some initial
146 data in the read buffer
147*/
148static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLOB *blob)
149{
150 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
151 struct smb2_read io;
152 struct smb2_read_state *state;
153 struct smb2_request *req;
154
155 state = talloc(smb, struct smb2_read_state);
156 if (state == NULL) {
157 return NT_STATUS_NO_MEMORY;
158 }
159
160 state->c = c;
161 if (blob == NULL) {
162 state->data = data_blob(NULL, 0);
163 } else {
164 state->data = *blob;
165 talloc_steal(state, state->data.data);
166 }
167
168 ZERO_STRUCT(io);
169 io.in.file.handle = smb->handle;
170
171 if (state->data.length >= 16) {
172 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
173 io.in.length = frag_length - state->data.length;
174 } else {
175 io.in.length = 0x2000;
176 }
177
178 req = smb2_read_send(smb->tree, &io);
179 if (req == NULL) {
180 return NT_STATUS_NO_MEMORY;
181 }
182
183 req->async.fn = smb2_read_callback;
184 req->async.private_data = state;
185
186 return NT_STATUS_OK;
187}
188
189
190/*
191 trigger a read request from the server
192*/
193static NTSTATUS send_read_request(struct dcecli_connection *c)
194{
195 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
196
197 if (smb->dead) {
198 return NT_STATUS_CONNECTION_DISCONNECTED;
199 }
200
201 return send_read_request_continue(c, NULL);
202}
203
204/*
205 this holds the state of an in-flight trans call
206*/
207struct smb2_trans_state {
208 struct dcecli_connection *c;
209};
210
211/*
212 called when a trans request has completed
213*/
214static void smb2_trans_callback(struct smb2_request *req)
215{
216 struct smb2_trans_state *state = talloc_get_type(req->async.private_data,
217 struct smb2_trans_state);
218 struct dcecli_connection *c = state->c;
219 NTSTATUS status;
220 struct smb2_ioctl io;
221
222 status = smb2_ioctl_recv(req, state, &io);
223 if (NT_STATUS_IS_ERR(status)) {
224 pipe_dead(c, status);
225 return;
226 }
227
228 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
229 DATA_BLOB data = io.out.out;
230 talloc_steal(c, data.data);
231 talloc_free(state);
232 c->transport.recv_data(c, &data, NT_STATUS_OK);
233 return;
234 }
235
236 /* there is more to receive - setup a read */
237 send_read_request_continue(c, &io.out.out);
238 talloc_free(state);
239}
240
241/*
242 send a SMBtrans style request, using a named pipe read_write fsctl
243*/
244static NTSTATUS smb2_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
245{
246 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
247 struct smb2_private);
248 struct smb2_ioctl io;
249 struct smb2_trans_state *state;
250 struct smb2_request *req;
251
252 state = talloc(smb, struct smb2_trans_state);
253 if (state == NULL) {
254 return NT_STATUS_NO_MEMORY;
255 }
256
257 state->c = c;
258
259 ZERO_STRUCT(io);
260 io.in.file.handle = smb->handle;
261 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
262 io.in.max_response_size = 0x2000;
263 io.in.flags = 1;
264 io.in.out = *blob;
265
266 req = smb2_ioctl_send(smb->tree, &io);
267 if (req == NULL) {
268 talloc_free(state);
269 return NT_STATUS_NO_MEMORY;
270 }
271
272 req->async.fn = smb2_trans_callback;
273 req->async.private_data = state;
274
275 talloc_steal(state, req);
276
277 return NT_STATUS_OK;
278}
279
280/*
281 called when a write request has completed
282*/
283static void smb2_write_callback(struct smb2_request *req)
284{
285 struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
286
287 if (!NT_STATUS_IS_OK(req->status)) {
288 DEBUG(0,("dcerpc_smb2: write callback error\n"));
289 pipe_dead(c, req->status);
290 }
291
292 smb2_request_destroy(req);
293}
294
295/*
296 send a packet to the server
297*/
298static NTSTATUS smb2_send_request(struct dcecli_connection *c, DATA_BLOB *blob,
299 bool trigger_read)
300{
301 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
302 struct smb2_write io;
303 struct smb2_request *req;
304
305 if (smb->dead) {
306 return NT_STATUS_CONNECTION_DISCONNECTED;
307 }
308
309 if (trigger_read) {
310 return smb2_send_trans_request(c, blob);
311 }
312
313 ZERO_STRUCT(io);
314 io.in.file.handle = smb->handle;
315 io.in.data = *blob;
316
317 req = smb2_write_send(smb->tree, &io);
318 if (req == NULL) {
319 return NT_STATUS_NO_MEMORY;
320 }
321
322 req->async.fn = smb2_write_callback;
323 req->async.private_data = c;
324
325 return NT_STATUS_OK;
326}
327
328static void free_request(struct smb2_request *req)
329{
330 talloc_free(req);
331}
332
333/*
334 shutdown SMB pipe connection
335*/
336static NTSTATUS smb2_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
337{
338 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
339 struct smb2_close io;
340 struct smb2_request *req;
341
342 /* maybe we're still starting up */
343 if (!smb) return status;
344
345 ZERO_STRUCT(io);
346 io.in.file.handle = smb->handle;
347 req = smb2_close_send(smb->tree, &io);
348 if (req != NULL) {
349 /* we don't care if this fails, so just free it if it succeeds */
350 req->async.fn = free_request;
351 }
352
353 talloc_free(smb);
354
355 return status;
356}
357
358/*
359 return SMB server name
360*/
361static const char *smb2_peer_name(struct dcecli_connection *c)
362{
363 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
364 struct smb2_private);
365 return smb->server_name;
366}
367
368/*
369 return remote name we make the actual connection (good for kerberos)
370*/
371static const char *smb2_target_hostname(struct dcecli_connection *c)
372{
373 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
374 struct smb2_private);
375 return smb->tree->session->transport->socket->hostname;
376}
377
378/*
379 fetch the user session key
380*/
381static NTSTATUS smb2_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
382{
383 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
384 struct smb2_private);
385 *session_key = smb->tree->session->session_key;
386 if (session_key->data == NULL) {
387 return NT_STATUS_NO_USER_SESSION_KEY;
388 }
389 return NT_STATUS_OK;
390}
391
392struct pipe_open_smb2_state {
393 struct dcecli_connection *c;
394 struct composite_context *ctx;
395};
396
397static void pipe_open_recv(struct smb2_request *req);
398
399struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p,
400 struct smb2_tree *tree,
401 const char *pipe_name)
402{
403 struct composite_context *ctx;
404 struct pipe_open_smb2_state *state;
405 struct smb2_create io;
406 struct smb2_request *req;
407 struct dcecli_connection *c = p->conn;
408
409 ctx = composite_create(c, c->event_ctx);
410 if (ctx == NULL) return NULL;
411
412 state = talloc(ctx, struct pipe_open_smb2_state);
413 if (composite_nomem(state, ctx)) return ctx;
414 ctx->private_data = state;
415
416 state->c = c;
417 state->ctx = ctx;
418
419 ZERO_STRUCT(io);
420 io.in.desired_access =
421 SEC_STD_READ_CONTROL |
422 SEC_FILE_READ_ATTRIBUTE |
423 SEC_FILE_WRITE_ATTRIBUTE |
424 SEC_STD_SYNCHRONIZE |
425 SEC_FILE_READ_EA |
426 SEC_FILE_WRITE_EA |
427 SEC_FILE_READ_DATA |
428 SEC_FILE_WRITE_DATA |
429 SEC_FILE_APPEND_DATA;
430 io.in.share_access =
431 NTCREATEX_SHARE_ACCESS_READ |
432 NTCREATEX_SHARE_ACCESS_WRITE;
433 io.in.create_disposition = NTCREATEX_DISP_OPEN;
434 io.in.create_options =
435 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
436 NTCREATEX_OPTIONS_NO_RECALL;
437 io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
438
439 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
440 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
441 pipe_name += 6;
442 }
443 io.in.fname = pipe_name;
444
445 req = smb2_create_send(tree, &io);
446 composite_continue_smb2(ctx, req, pipe_open_recv, state);
447 return ctx;
448}
449
450static void pipe_open_recv(struct smb2_request *req)
451{
452 struct pipe_open_smb2_state *state =
453 talloc_get_type(req->async.private_data,
454 struct pipe_open_smb2_state);
455 struct composite_context *ctx = state->ctx;
456 struct dcecli_connection *c = state->c;
457 struct smb2_tree *tree = req->tree;
458 struct smb2_private *smb;
459 struct smb2_create io;
460
461 ctx->status = smb2_create_recv(req, state, &io);
462 if (!composite_is_ok(ctx)) return;
463
464 /*
465 fill in the transport methods
466 */
467 c->transport.transport = NCACN_NP;
468 c->transport.private_data = NULL;
469 c->transport.shutdown_pipe = smb2_shutdown_pipe;
470 c->transport.peer_name = smb2_peer_name;
471 c->transport.target_hostname = smb2_target_hostname;
472
473 c->transport.send_request = smb2_send_request;
474 c->transport.send_read = send_read_request;
475 c->transport.recv_data = NULL;
476
477 /* Over-ride the default session key with the SMB session key */
478 c->security_state.session_key = smb2_session_key;
479
480 smb = talloc(c, struct smb2_private);
481 if (composite_nomem(smb, ctx)) return;
482
483 smb->handle = io.out.file.handle;
484 smb->tree = talloc_reference(smb, tree);
485 smb->server_name= strupper_talloc(smb,
486 tree->session->transport->socket->hostname);
487 if (composite_nomem(smb->server_name, ctx)) return;
488 smb->dead = false;
489
490 c->transport.private_data = smb;
491
492 composite_done(ctx);
493}
494
495NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
496{
497 NTSTATUS status = composite_wait(c);
498 talloc_free(c);
499 return status;
500}
501
502NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
503 struct smb2_tree *tree,
504 const char *pipe_name)
505{
506 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
507 return dcerpc_pipe_open_smb2_recv(ctx);
508}
509
510/*
511 return the SMB2 tree used for a dcerpc over SMB2 pipe
512*/
513struct smb2_tree *dcerpc_smb2_tree(struct dcecli_connection *c)
514{
515 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
516 struct smb2_private);
517 return smb->tree;
518}
Note: See TracBrowser for help on using the repository browser.