s3: Pass down smb_filename to smbacl4_fill_ace4
[Samba/gebeck_regimport.git] / source4 / librpc / rpc / dcerpc_smb2.c
blob473ca78ad1d9d93f09a74730cfb1f85446345276
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc over SMB2 transport
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
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/smb/smb_constants.h"
28 #include "librpc/rpc/dcerpc.h"
29 #include "librpc/rpc/dcerpc_proto.h"
30 #include "librpc/rpc/rpc_common.h"
31 #include "../libcli/smb/smbXcli_base.h"
33 /* transport private information used by SMB2 pipe transport */
34 struct smb2_private {
35 struct smb2_handle handle;
36 struct smb2_tree *tree;
37 DATA_BLOB session_key;
38 const char *server_name;
39 bool dead;
44 tell the dcerpc layer that the transport is dead
46 static void pipe_dead(struct dcecli_connection *c, NTSTATUS status)
48 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
50 if (smb->dead) {
51 return;
54 smb->dead = true;
56 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
57 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
60 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
61 status = NT_STATUS_END_OF_FILE;
64 if (c->transport.recv_data) {
65 c->transport.recv_data(c, NULL, status);
70 /*
71 this holds the state of an in-flight call
73 struct smb2_read_state {
74 struct dcecli_connection *c;
75 DATA_BLOB data;
79 called when a read request has completed
81 static void smb2_read_callback(struct smb2_request *req)
83 struct dcecli_connection *c;
84 struct smb2_private *smb;
85 struct smb2_read_state *state;
86 struct smb2_read io;
87 uint16_t frag_length;
88 NTSTATUS status;
90 state = talloc_get_type(req->async.private_data, struct smb2_read_state);
91 smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
92 c = state->c;
94 status = smb2_read_recv(req, state, &io);
95 if (NT_STATUS_IS_ERR(status)) {
96 talloc_free(state);
97 pipe_dead(c, status);
98 return;
101 if (!data_blob_append(state, &state->data,
102 io.out.data.data, io.out.data.length)) {
103 talloc_free(state);
104 pipe_dead(c, NT_STATUS_NO_MEMORY);
105 return;
108 if (state->data.length < 16) {
109 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
110 (int)state->data.length));
111 talloc_free(state);
112 pipe_dead(c, NT_STATUS_INFO_LENGTH_MISMATCH);
113 return;
116 frag_length = dcerpc_get_frag_length(&state->data);
118 if (frag_length <= state->data.length) {
119 DATA_BLOB data = state->data;
120 talloc_steal(c, data.data);
121 talloc_free(state);
122 c->transport.recv_data(c, &data, NT_STATUS_OK);
123 return;
126 /* initiate another read request, as we only got part of a fragment */
127 ZERO_STRUCT(io);
128 io.in.file.handle = smb->handle;
129 io.in.length = MIN(state->c->srv_max_xmit_frag,
130 frag_length - state->data.length);
131 if (io.in.length < 16) {
132 io.in.length = 16;
135 req = smb2_read_send(smb->tree, &io);
136 if (req == NULL) {
137 talloc_free(state);
138 pipe_dead(c, NT_STATUS_NO_MEMORY);
139 return;
142 req->async.fn = smb2_read_callback;
143 req->async.private_data = state;
148 trigger a read request from the server, possibly with some initial
149 data in the read buffer
151 static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLOB *blob)
153 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
154 struct smb2_read io;
155 struct smb2_read_state *state;
156 struct smb2_request *req;
158 state = talloc(c, struct smb2_read_state);
159 if (state == NULL) {
160 return NT_STATUS_NO_MEMORY;
163 state->c = c;
164 if (blob == NULL) {
165 state->data = data_blob(NULL, 0);
166 } else {
167 state->data = *blob;
168 talloc_steal(state, state->data.data);
171 ZERO_STRUCT(io);
172 io.in.file.handle = smb->handle;
174 if (state->data.length >= 16) {
175 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
176 io.in.length = frag_length - state->data.length;
177 } else {
178 io.in.length = 0x2000;
181 req = smb2_read_send(smb->tree, &io);
182 if (req == NULL) {
183 return NT_STATUS_NO_MEMORY;
186 req->async.fn = smb2_read_callback;
187 req->async.private_data = state;
189 return NT_STATUS_OK;
194 trigger a read request from the server
196 static NTSTATUS send_read_request(struct dcecli_connection *c)
198 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
200 if (smb->dead) {
201 return NT_STATUS_CONNECTION_DISCONNECTED;
204 return send_read_request_continue(c, NULL);
208 this holds the state of an in-flight trans call
210 struct smb2_trans_state {
211 struct dcecli_connection *c;
215 called when a trans request has completed
217 static void smb2_trans_callback(struct smb2_request *req)
219 struct smb2_trans_state *state = talloc_get_type(req->async.private_data,
220 struct smb2_trans_state);
221 struct dcecli_connection *c = state->c;
222 NTSTATUS status;
223 struct smb2_ioctl io;
225 status = smb2_ioctl_recv(req, state, &io);
226 if (NT_STATUS_IS_ERR(status)) {
227 pipe_dead(c, status);
228 return;
231 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
232 DATA_BLOB data = io.out.out;
233 talloc_steal(c, data.data);
234 talloc_free(state);
235 c->transport.recv_data(c, &data, NT_STATUS_OK);
236 return;
239 /* there is more to receive - setup a read */
240 send_read_request_continue(c, &io.out.out);
241 talloc_free(state);
245 send a SMBtrans style request, using a named pipe read_write fsctl
247 static NTSTATUS smb2_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
249 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
250 struct smb2_private);
251 struct smb2_ioctl io;
252 struct smb2_trans_state *state;
253 struct smb2_request *req;
255 state = talloc(smb, struct smb2_trans_state);
256 if (state == NULL) {
257 return NT_STATUS_NO_MEMORY;
260 state->c = c;
262 ZERO_STRUCT(io);
263 io.in.file.handle = smb->handle;
264 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
265 io.in.max_response_size = 0x2000;
266 io.in.flags = 1;
267 io.in.out = *blob;
269 req = smb2_ioctl_send(smb->tree, &io);
270 if (req == NULL) {
271 talloc_free(state);
272 return NT_STATUS_NO_MEMORY;
275 req->async.fn = smb2_trans_callback;
276 req->async.private_data = state;
278 talloc_steal(state, req);
280 return NT_STATUS_OK;
284 called when a write request has completed
286 static void smb2_write_callback(struct smb2_request *req)
288 struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
289 struct smb2_write io;
290 NTSTATUS status;
292 ZERO_STRUCT(io);
294 status = smb2_write_recv(req, &io);
295 if (!NT_STATUS_IS_OK(status)) {
296 DEBUG(0,("dcerpc_smb2: write callback error: %s\n",
297 nt_errstr(status)));
298 pipe_dead(c, status);
303 send a packet to the server
305 static NTSTATUS smb2_send_request(struct dcecli_connection *c, DATA_BLOB *blob,
306 bool trigger_read)
308 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
309 struct smb2_write io;
310 struct smb2_request *req;
312 if (smb->dead) {
313 return NT_STATUS_CONNECTION_DISCONNECTED;
316 if (trigger_read) {
317 return smb2_send_trans_request(c, blob);
320 ZERO_STRUCT(io);
321 io.in.file.handle = smb->handle;
322 io.in.data = *blob;
324 req = smb2_write_send(smb->tree, &io);
325 if (req == NULL) {
326 return NT_STATUS_NO_MEMORY;
329 req->async.fn = smb2_write_callback;
330 req->async.private_data = c;
332 return NT_STATUS_OK;
335 static void free_request(struct smb2_request *req)
337 talloc_free(req);
341 shutdown SMB pipe connection
343 static NTSTATUS smb2_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
345 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
346 struct smb2_close io;
347 struct smb2_request *req;
349 /* maybe we're still starting up */
350 if (!smb) return status;
352 ZERO_STRUCT(io);
353 io.in.file.handle = smb->handle;
354 req = smb2_close_send(smb->tree, &io);
355 if (req != NULL) {
356 /* we don't care if this fails, so just free it if it succeeds */
357 req->async.fn = free_request;
360 talloc_free(smb);
362 return status;
366 return SMB server name
368 static const char *smb2_peer_name(struct dcecli_connection *c)
370 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
371 struct smb2_private);
372 return smb->server_name;
376 return remote name we make the actual connection (good for kerberos)
378 static const char *smb2_target_hostname(struct dcecli_connection *c)
380 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
381 struct smb2_private);
382 return smbXcli_conn_remote_name(smb->tree->session->transport->conn);
386 fetch the user session key
388 static NTSTATUS smb2_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
390 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
391 struct smb2_private);
393 if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
395 if (smb->session_key.length == 0) {
396 return NT_STATUS_NO_USER_SESSION_KEY;
399 *session_key = smb->session_key;
400 return NT_STATUS_OK;
403 struct pipe_open_smb2_state {
404 struct dcecli_connection *c;
405 struct composite_context *ctx;
408 static void pipe_open_recv(struct smb2_request *req);
410 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p,
411 struct smb2_tree *tree,
412 const char *pipe_name)
414 struct composite_context *ctx;
415 struct pipe_open_smb2_state *state;
416 struct smb2_create io;
417 struct smb2_request *req;
418 struct dcecli_connection *c = p->conn;
420 /* if we don't have a binding on this pipe yet, then create one */
421 if (p->binding == NULL) {
422 NTSTATUS status;
423 const char *r = smbXcli_conn_remote_name(tree->session->transport->conn);
424 char *s;
425 SMB_ASSERT(r != NULL);
426 s = talloc_asprintf(p, "ncacn_np:%s", r);
427 if (s == NULL) return NULL;
428 status = dcerpc_parse_binding(p, s, &p->binding);
429 talloc_free(s);
430 if (!NT_STATUS_IS_OK(status)) {
431 return NULL;
435 ctx = composite_create(c, c->event_ctx);
436 if (ctx == NULL) return NULL;
438 state = talloc(ctx, struct pipe_open_smb2_state);
439 if (composite_nomem(state, ctx)) return ctx;
440 ctx->private_data = state;
442 state->c = c;
443 state->ctx = ctx;
445 ZERO_STRUCT(io);
446 io.in.desired_access =
447 SEC_STD_READ_CONTROL |
448 SEC_FILE_READ_ATTRIBUTE |
449 SEC_FILE_WRITE_ATTRIBUTE |
450 SEC_STD_SYNCHRONIZE |
451 SEC_FILE_READ_EA |
452 SEC_FILE_WRITE_EA |
453 SEC_FILE_READ_DATA |
454 SEC_FILE_WRITE_DATA |
455 SEC_FILE_APPEND_DATA;
456 io.in.share_access =
457 NTCREATEX_SHARE_ACCESS_READ |
458 NTCREATEX_SHARE_ACCESS_WRITE;
459 io.in.create_disposition = NTCREATEX_DISP_OPEN;
460 io.in.create_options =
461 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
462 NTCREATEX_OPTIONS_NO_RECALL;
463 io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
465 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
466 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
467 pipe_name += 6;
469 io.in.fname = pipe_name;
471 req = smb2_create_send(tree, &io);
472 composite_continue_smb2(ctx, req, pipe_open_recv, state);
473 return ctx;
476 static void pipe_open_recv(struct smb2_request *req)
478 struct pipe_open_smb2_state *state =
479 talloc_get_type(req->async.private_data,
480 struct pipe_open_smb2_state);
481 struct composite_context *ctx = state->ctx;
482 struct dcecli_connection *c = state->c;
483 struct smb2_tree *tree = req->tree;
484 struct smb2_private *smb;
485 struct smb2_create io;
487 ctx->status = smb2_create_recv(req, state, &io);
488 if (!composite_is_ok(ctx)) return;
491 fill in the transport methods
493 c->transport.transport = NCACN_NP;
494 c->transport.private_data = NULL;
495 c->transport.shutdown_pipe = smb2_shutdown_pipe;
496 c->transport.peer_name = smb2_peer_name;
497 c->transport.target_hostname = smb2_target_hostname;
499 c->transport.send_request = smb2_send_request;
500 c->transport.send_read = send_read_request;
501 c->transport.recv_data = NULL;
503 /* Over-ride the default session key with the SMB session key */
504 c->security_state.session_key = smb2_session_key;
506 smb = talloc(c, struct smb2_private);
507 if (composite_nomem(smb, ctx)) return;
509 smb->handle = io.out.file.handle;
510 smb->tree = talloc_reference(smb, tree);
511 smb->server_name= strupper_talloc(smb,
512 smbXcli_conn_remote_name(tree->session->transport->conn));
513 if (composite_nomem(smb->server_name, ctx)) return;
514 smb->dead = false;
516 ctx->status = smbXcli_session_application_key(tree->session->smbXcli,
517 smb, &smb->session_key);
518 if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_NO_USER_SESSION_KEY)) {
519 smb->session_key = data_blob_null;
520 ctx->status = NT_STATUS_OK;
522 if (!composite_is_ok(ctx)) return;
524 c->transport.private_data = smb;
526 composite_done(ctx);
529 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
531 NTSTATUS status = composite_wait(c);
532 talloc_free(c);
533 return status;
536 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
537 struct smb2_tree *tree,
538 const char *pipe_name)
540 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
541 return dcerpc_pipe_open_smb2_recv(ctx);
545 return the SMB2 tree used for a dcerpc over SMB2 pipe
547 struct smb2_tree *dcerpc_smb2_tree(struct dcecli_connection *c)
549 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
550 struct smb2_private);
551 return smb->tree;