source: trunk/server/source4/librpc/rpc/dcerpc_smb2.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: 12.6 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
174 if (frag_length < state->data.length) {
175 talloc_free(state);
176 return NT_STATUS_RPC_PROTOCOL_ERROR;
177 }
178
179 io.in.length = frag_length - state->data.length;
180 } else {
181 io.in.length = 0x2000;
182 }
183
184 req = smb2_read_send(smb->tree, &io);
185 if (req == NULL) {
186 return NT_STATUS_NO_MEMORY;
187 }
188
189 req->async.fn = smb2_read_callback;
190 req->async.private_data = state;
191
192 return NT_STATUS_OK;
193}
194
195
196/*
197 trigger a read request from the server
198*/
199static NTSTATUS send_read_request(struct dcecli_connection *c)
200{
201 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
202
203 if (smb->dead) {
204 return NT_STATUS_CONNECTION_DISCONNECTED;
205 }
206
207 return send_read_request_continue(c, NULL);
208}
209
210/*
211 this holds the state of an in-flight trans call
212*/
213struct smb2_trans_state {
214 struct dcecli_connection *c;
215};
216
217/*
218 called when a trans request has completed
219*/
220static void smb2_trans_callback(struct smb2_request *req)
221{
222 struct smb2_trans_state *state = talloc_get_type(req->async.private_data,
223 struct smb2_trans_state);
224 struct dcecli_connection *c = state->c;
225 NTSTATUS status;
226 struct smb2_ioctl io;
227
228 status = smb2_ioctl_recv(req, state, &io);
229 if (NT_STATUS_IS_ERR(status)) {
230 pipe_dead(c, status);
231 return;
232 }
233
234 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
235 DATA_BLOB data = io.out.out;
236 talloc_steal(c, data.data);
237 talloc_free(state);
238 c->transport.recv_data(c, &data, NT_STATUS_OK);
239 return;
240 }
241
242 /* there is more to receive - setup a read */
243 send_read_request_continue(c, &io.out.out);
244 talloc_free(state);
245}
246
247/*
248 send a SMBtrans style request, using a named pipe read_write fsctl
249*/
250static NTSTATUS smb2_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
251{
252 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
253 struct smb2_private);
254 struct smb2_ioctl io;
255 struct smb2_trans_state *state;
256 struct smb2_request *req;
257
258 state = talloc(smb, struct smb2_trans_state);
259 if (state == NULL) {
260 return NT_STATUS_NO_MEMORY;
261 }
262
263 state->c = c;
264
265 ZERO_STRUCT(io);
266 io.in.file.handle = smb->handle;
267 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
268 io.in.max_response_size = 0x2000;
269 io.in.flags = 1;
270 io.in.out = *blob;
271
272 req = smb2_ioctl_send(smb->tree, &io);
273 if (req == NULL) {
274 talloc_free(state);
275 return NT_STATUS_NO_MEMORY;
276 }
277
278 req->async.fn = smb2_trans_callback;
279 req->async.private_data = state;
280
281 talloc_steal(state, req);
282
283 return NT_STATUS_OK;
284}
285
286/*
287 called when a write request has completed
288*/
289static void smb2_write_callback(struct smb2_request *req)
290{
291 struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
292
293 if (!NT_STATUS_IS_OK(req->status)) {
294 DEBUG(0,("dcerpc_smb2: write callback error\n"));
295 pipe_dead(c, req->status);
296 }
297
298 smb2_request_destroy(req);
299}
300
301/*
302 send a packet to the server
303*/
304static NTSTATUS smb2_send_request(struct dcecli_connection *c, DATA_BLOB *blob,
305 bool trigger_read)
306{
307 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
308 struct smb2_write io;
309 struct smb2_request *req;
310
311 if (smb->dead) {
312 return NT_STATUS_CONNECTION_DISCONNECTED;
313 }
314
315 if (trigger_read) {
316 return smb2_send_trans_request(c, blob);
317 }
318
319 ZERO_STRUCT(io);
320 io.in.file.handle = smb->handle;
321 io.in.data = *blob;
322
323 req = smb2_write_send(smb->tree, &io);
324 if (req == NULL) {
325 return NT_STATUS_NO_MEMORY;
326 }
327
328 req->async.fn = smb2_write_callback;
329 req->async.private_data = c;
330
331 return NT_STATUS_OK;
332}
333
334static void free_request(struct smb2_request *req)
335{
336 talloc_free(req);
337}
338
339/*
340 shutdown SMB pipe connection
341*/
342static NTSTATUS smb2_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
343{
344 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
345 struct smb2_close io;
346 struct smb2_request *req;
347
348 /* maybe we're still starting up */
349 if (!smb) return status;
350
351 ZERO_STRUCT(io);
352 io.in.file.handle = smb->handle;
353 req = smb2_close_send(smb->tree, &io);
354 if (req != NULL) {
355 /* we don't care if this fails, so just free it if it succeeds */
356 req->async.fn = free_request;
357 }
358
359 talloc_free(smb);
360
361 return status;
362}
363
364/*
365 return SMB server name
366*/
367static const char *smb2_peer_name(struct dcecli_connection *c)
368{
369 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
370 struct smb2_private);
371 return smb->server_name;
372}
373
374/*
375 return remote name we make the actual connection (good for kerberos)
376*/
377static const char *smb2_target_hostname(struct dcecli_connection *c)
378{
379 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
380 struct smb2_private);
381 return smb->tree->session->transport->socket->hostname;
382}
383
384/*
385 fetch the user session key
386*/
387static NTSTATUS smb2_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
388{
389 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
390 struct smb2_private);
391 *session_key = smb->tree->session->session_key;
392 if (session_key->data == NULL) {
393 return NT_STATUS_NO_USER_SESSION_KEY;
394 }
395 return NT_STATUS_OK;
396}
397
398struct pipe_open_smb2_state {
399 struct dcecli_connection *c;
400 struct composite_context *ctx;
401};
402
403static void pipe_open_recv(struct smb2_request *req);
404
405struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p,
406 struct smb2_tree *tree,
407 const char *pipe_name)
408{
409 struct composite_context *ctx;
410 struct pipe_open_smb2_state *state;
411 struct smb2_create io;
412 struct smb2_request *req;
413 struct dcecli_connection *c = p->conn;
414
415 ctx = composite_create(c, c->event_ctx);
416 if (ctx == NULL) return NULL;
417
418 state = talloc(ctx, struct pipe_open_smb2_state);
419 if (composite_nomem(state, ctx)) return ctx;
420 ctx->private_data = state;
421
422 state->c = c;
423 state->ctx = ctx;
424
425 ZERO_STRUCT(io);
426 io.in.desired_access =
427 SEC_STD_READ_CONTROL |
428 SEC_FILE_READ_ATTRIBUTE |
429 SEC_FILE_WRITE_ATTRIBUTE |
430 SEC_STD_SYNCHRONIZE |
431 SEC_FILE_READ_EA |
432 SEC_FILE_WRITE_EA |
433 SEC_FILE_READ_DATA |
434 SEC_FILE_WRITE_DATA |
435 SEC_FILE_APPEND_DATA;
436 io.in.share_access =
437 NTCREATEX_SHARE_ACCESS_READ |
438 NTCREATEX_SHARE_ACCESS_WRITE;
439 io.in.create_disposition = NTCREATEX_DISP_OPEN;
440 io.in.create_options =
441 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
442 NTCREATEX_OPTIONS_NO_RECALL;
443 io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
444
445 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
446 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
447 pipe_name += 6;
448 }
449 io.in.fname = pipe_name;
450
451 req = smb2_create_send(tree, &io);
452 composite_continue_smb2(ctx, req, pipe_open_recv, state);
453 return ctx;
454}
455
456static void pipe_open_recv(struct smb2_request *req)
457{
458 struct pipe_open_smb2_state *state =
459 talloc_get_type(req->async.private_data,
460 struct pipe_open_smb2_state);
461 struct composite_context *ctx = state->ctx;
462 struct dcecli_connection *c = state->c;
463 struct smb2_tree *tree = req->tree;
464 struct smb2_private *smb;
465 struct smb2_create io;
466
467 ctx->status = smb2_create_recv(req, state, &io);
468 if (!composite_is_ok(ctx)) return;
469
470 /*
471 fill in the transport methods
472 */
473 c->transport.transport = NCACN_NP;
474 c->transport.private_data = NULL;
475 c->transport.shutdown_pipe = smb2_shutdown_pipe;
476 c->transport.peer_name = smb2_peer_name;
477 c->transport.target_hostname = smb2_target_hostname;
478
479 c->transport.send_request = smb2_send_request;
480 c->transport.send_read = send_read_request;
481 c->transport.recv_data = NULL;
482
483 /* Over-ride the default session key with the SMB session key */
484 c->security_state.session_key = smb2_session_key;
485
486 smb = talloc(c, struct smb2_private);
487 if (composite_nomem(smb, ctx)) return;
488
489 smb->handle = io.out.file.handle;
490 smb->tree = talloc_reference(smb, tree);
491 smb->server_name= strupper_talloc(smb,
492 tree->session->transport->socket->hostname);
493 if (composite_nomem(smb->server_name, ctx)) return;
494 smb->dead = false;
495
496 c->transport.private_data = smb;
497
498 composite_done(ctx);
499}
500
501NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
502{
503 NTSTATUS status = composite_wait(c);
504 talloc_free(c);
505 return status;
506}
507
508NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
509 struct smb2_tree *tree,
510 const char *pipe_name)
511{
512 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
513 return dcerpc_pipe_open_smb2_recv(ctx);
514}
515
516/*
517 return the SMB2 tree used for a dcerpc over SMB2 pipe
518*/
519struct smb2_tree *dcerpc_smb2_tree(struct dcecli_connection *c)
520{
521 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
522 struct smb2_private);
523 return smb->tree;
524}
Note: See TracBrowser for help on using the repository browser.