Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[samba.git] / source4 / librpc / rpc / dcerpc_smb2.c
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
30 /* transport private information used by SMB2 pipe transport */
31 struct smb2_private {
32         struct smb2_handle handle;
33         struct smb2_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 */
42 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
43 {
44         struct smb2_private *smb = (struct smb2_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 */
69 struct smb2_read_state {
70         struct dcerpc_connection *c;
71         DATA_BLOB data;
72 };
73
74 /*
75   called when a read request has completed
76 */
77 static void smb2_read_callback(struct smb2_request *req)
78 {
79         struct smb2_private *smb;
80         struct smb2_read_state *state;
81         struct smb2_read io;
82         uint16_t frag_length;
83         NTSTATUS status;
84
85         state = talloc_get_type(req->async.private, struct smb2_read_state);
86         smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
87
88         status = smb2_read_recv(req, state, &io);
89         if (NT_STATUS_IS_ERR(status)) {
90                 pipe_dead(state->c, status);
91                 talloc_free(state);
92                 return;
93         }
94
95         if (!data_blob_append(state, &state->data, 
96                                   io.out.data.data, io.out.data.length)) {
97                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
98                 talloc_free(state);
99                 return;
100         }
101
102         if (state->data.length < 16) {
103                 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
104                          (int)state->data.length));
105                 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
106                 talloc_free(state);
107                 return;
108         }
109
110         frag_length = dcerpc_get_frag_length(&state->data);
111
112         if (frag_length <= state->data.length) {
113                 DATA_BLOB data = state->data;
114                 struct dcerpc_connection *c = state->c;
115                 talloc_steal(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         ZERO_STRUCT(io);
123         io.in.file.handle = smb->handle;
124         io.in.length = MIN(state->c->srv_max_xmit_frag, 
125                            frag_length - state->data.length);
126         if (io.in.length < 16) {
127                 io.in.length = 16;
128         }
129         
130         req = smb2_read_send(smb->tree, &io);
131         if (req == NULL) {
132                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
133                 talloc_free(state);
134                 return;
135         }
136
137         req->async.fn = smb2_read_callback;
138         req->async.private = state;
139 }
140
141
142 /*
143   trigger a read request from the server, possibly with some initial
144   data in the read buffer
145 */
146 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
147 {
148         struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
149         struct smb2_read io;
150         struct smb2_read_state *state;
151         struct smb2_request *req;
152
153         state = talloc(smb, struct smb2_read_state);
154         if (state == NULL) {
155                 return NT_STATUS_NO_MEMORY;
156         }
157
158         state->c = c;
159         if (blob == NULL) {
160                 state->data = data_blob(NULL, 0);
161         } else {
162                 state->data = *blob;
163                 talloc_steal(state, state->data.data);
164         }
165
166         ZERO_STRUCT(io);
167         io.in.file.handle = smb->handle;
168
169         if (state->data.length >= 16) {
170                 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
171                 io.in.length = frag_length - state->data.length;
172         } else {
173                 io.in.length = 0x2000;
174         }
175
176         req = smb2_read_send(smb->tree, &io);
177         if (req == NULL) {
178                 return NT_STATUS_NO_MEMORY;
179         }
180
181         req->async.fn = smb2_read_callback;
182         req->async.private = state;
183
184         return NT_STATUS_OK;
185 }
186
187
188 /*
189   trigger a read request from the server
190 */
191 static NTSTATUS send_read_request(struct dcerpc_connection *c)
192 {
193         struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
194
195         if (smb->dead) {
196                 return NT_STATUS_CONNECTION_DISCONNECTED;
197         }
198
199         return send_read_request_continue(c, NULL);
200 }
201
202 /* 
203    this holds the state of an in-flight trans call
204 */
205 struct smb2_trans_state {
206         struct dcerpc_connection *c;
207 };
208
209 /*
210   called when a trans request has completed
211 */
212 static void smb2_trans_callback(struct smb2_request *req)
213 {
214         struct smb2_trans_state *state = talloc_get_type(req->async.private,
215                                                         struct smb2_trans_state);
216         struct dcerpc_connection *c = state->c;
217         NTSTATUS status;
218         struct smb2_ioctl io;
219
220         status = smb2_ioctl_recv(req, state, &io);
221         if (NT_STATUS_IS_ERR(status)) {
222                 pipe_dead(c, status);
223                 return;
224         }
225
226         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
227                 DATA_BLOB data = io.out.out;
228                 talloc_steal(c, data.data);
229                 talloc_free(state);
230                 c->transport.recv_data(c, &data, NT_STATUS_OK);
231                 return;
232         }
233
234         /* there is more to receive - setup a read */
235         send_read_request_continue(c, &io.out.out);
236         talloc_free(state);
237 }
238
239 /*
240   send a SMBtrans style request, using a named pipe read_write fsctl
241 */
242 static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
243 {
244         struct smb2_private *smb = talloc_get_type(c->transport.private_data,
245                                                    struct smb2_private);
246         struct smb2_ioctl io;
247         struct smb2_trans_state *state;
248         struct smb2_request *req;
249
250         state = talloc(smb, struct smb2_trans_state);
251         if (state == NULL) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         state->c = c;
256         
257         ZERO_STRUCT(io);
258         io.in.file.handle       = smb->handle;
259         io.in.function          = FSCTL_NAMED_PIPE_READ_WRITE;
260         io.in.max_response_size = 0x1000;
261         io.in.flags             = 1;
262         io.in.out               = *blob;
263
264         req = smb2_ioctl_send(smb->tree, &io);
265         if (req == NULL) {
266                 talloc_free(state);
267                 return NT_STATUS_NO_MEMORY;
268         }
269
270         req->async.fn = smb2_trans_callback;
271         req->async.private = state;
272
273         talloc_steal(state, req);
274
275         return NT_STATUS_OK;
276 }
277
278 /*
279   called when a write request has completed
280 */
281 static void smb2_write_callback(struct smb2_request *req)
282 {
283         struct dcerpc_connection *c = (struct dcerpc_connection *)req->async.private;
284
285         if (!NT_STATUS_IS_OK(req->status)) {
286                 DEBUG(0,("dcerpc_smb2: write callback error\n"));
287                 pipe_dead(c, req->status);
288         }
289
290         smb2_request_destroy(req);
291 }
292
293 /* 
294    send a packet to the server
295 */
296 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, 
297                                   bool trigger_read)
298 {
299         struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
300         struct smb2_write io;
301         struct smb2_request *req;
302
303         if (smb->dead) {
304                 return NT_STATUS_CONNECTION_DISCONNECTED;
305         }
306
307         if (trigger_read) {
308                 return smb2_send_trans_request(c, blob);
309         }
310
311         ZERO_STRUCT(io);
312         io.in.file.handle       = smb->handle;
313         io.in.data              = *blob;
314
315         req = smb2_write_send(smb->tree, &io);
316         if (req == NULL) {
317                 return NT_STATUS_NO_MEMORY;
318         }
319
320         req->async.fn = smb2_write_callback;
321         req->async.private = c;
322
323         return NT_STATUS_OK;
324 }
325
326 /* 
327    shutdown SMB pipe connection
328 */
329 static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
330 {
331         struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
332         struct smb2_close io;
333         struct smb2_request *req;
334
335         /* maybe we're still starting up */
336         if (!smb) return status;
337
338         ZERO_STRUCT(io);
339         io.in.file.handle = smb->handle;
340         req = smb2_close_send(smb->tree, &io);
341         if (req != NULL) {
342                 /* we don't care if this fails, so just free it if it succeeds */
343                 req->async.fn = (void (*)(struct smb2_request *))talloc_free;
344         }
345
346         talloc_free(smb);
347
348         return status;
349 }
350
351 /*
352   return SMB server name
353 */
354 static const char *smb2_peer_name(struct dcerpc_connection *c)
355 {
356         struct smb2_private *smb = talloc_get_type(c->transport.private_data,
357                                                    struct smb2_private);
358         return smb->server_name;
359 }
360
361 /*
362   return remote name we make the actual connection (good for kerberos) 
363 */
364 static const char *smb2_target_hostname(struct dcerpc_connection *c)
365 {
366         struct smb2_private *smb = talloc_get_type(c->transport.private_data, 
367                                                    struct smb2_private);
368         return smb->tree->session->transport->socket->hostname;
369 }
370
371 /*
372   fetch the user session key 
373 */
374 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
375 {
376         struct smb2_private *smb = talloc_get_type(c->transport.private_data,
377                                                    struct smb2_private);
378         *session_key = smb->tree->session->session_key;
379         if (session_key->data == NULL) {
380                 return NT_STATUS_NO_USER_SESSION_KEY;
381         }
382         return NT_STATUS_OK;
383 }
384
385 struct pipe_open_smb2_state {
386         struct dcerpc_connection *c;
387         struct composite_context *ctx;
388 };
389
390 static void pipe_open_recv(struct smb2_request *req);
391
392 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p, 
393                                                      struct smb2_tree *tree,
394                                                      const char *pipe_name)
395 {
396         struct composite_context *ctx;
397         struct pipe_open_smb2_state *state;
398         struct smb2_create io;
399         struct smb2_request *req;
400         struct dcerpc_connection *c = p->conn;
401
402         ctx = composite_create(c, c->event_ctx);
403         if (ctx == NULL) return NULL;
404
405         state = talloc(ctx, struct pipe_open_smb2_state);
406         if (composite_nomem(state, ctx)) return ctx;
407         ctx->private_data = state;
408
409         state->c = c;
410         state->ctx = ctx;
411
412         ZERO_STRUCT(io);
413         io.in.desired_access = 
414                 SEC_STD_READ_CONTROL |
415                 SEC_FILE_READ_ATTRIBUTE |
416                 SEC_FILE_WRITE_ATTRIBUTE |
417                 SEC_STD_SYNCHRONIZE |
418                 SEC_FILE_READ_EA |
419                 SEC_FILE_WRITE_EA |
420                 SEC_FILE_READ_DATA |
421                 SEC_FILE_WRITE_DATA |
422                 SEC_FILE_APPEND_DATA;
423         io.in.share_access = 
424                 NTCREATEX_SHARE_ACCESS_READ |
425                 NTCREATEX_SHARE_ACCESS_WRITE;
426         io.in.create_disposition = NTCREATEX_DISP_OPEN;
427         io.in.create_options   = 
428                 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE | 
429                 NTCREATEX_OPTIONS_UNKNOWN_400000;
430         io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
431
432         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
433             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
434                 pipe_name += 6;
435         }
436         io.in.fname = pipe_name;
437
438         req = smb2_create_send(tree, &io);
439         composite_continue_smb2(ctx, req, pipe_open_recv, state);
440         return ctx;
441 }
442
443 static void pipe_open_recv(struct smb2_request *req)
444 {
445         struct pipe_open_smb2_state *state =
446                 talloc_get_type(req->async.private,
447                                 struct pipe_open_smb2_state);
448         struct composite_context *ctx = state->ctx;
449         struct dcerpc_connection *c = state->c;
450         struct smb2_tree *tree = req->tree;
451         struct smb2_private *smb;
452         struct smb2_create io;
453
454         ctx->status = smb2_create_recv(req, state, &io);
455         if (!composite_is_ok(ctx)) return;
456
457         /*
458           fill in the transport methods
459         */
460         c->transport.transport = NCACN_NP;
461         c->transport.private_data = NULL;
462         c->transport.shutdown_pipe = smb2_shutdown_pipe;
463         c->transport.peer_name = smb2_peer_name;
464         c->transport.target_hostname = smb2_target_hostname;
465
466         c->transport.send_request = smb2_send_request;
467         c->transport.send_read = send_read_request;
468         c->transport.recv_data = NULL;
469         
470         /* Over-ride the default session key with the SMB session key */
471         c->security_state.session_key = smb2_session_key;
472
473         smb = talloc(c, struct smb2_private);
474         if (composite_nomem(smb, ctx)) return;
475
476         smb->handle     = io.out.file.handle;
477         smb->tree       = talloc_reference(smb, tree);
478         smb->server_name= strupper_talloc(smb, 
479                                           tree->session->transport->socket->hostname);
480         if (composite_nomem(smb->server_name, ctx)) return;
481         smb->dead       = false;
482
483         c->transport.private_data = smb;
484
485         composite_done(ctx);
486 }
487
488 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
489 {
490         NTSTATUS status = composite_wait(c);
491         talloc_free(c);
492         return status;
493 }
494
495 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
496                                struct smb2_tree *tree,
497                                const char *pipe_name)
498 {
499         struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
500         return dcerpc_pipe_open_smb2_recv(ctx);
501 }
502
503 /*
504   return the SMB2 tree used for a dcerpc over SMB2 pipe
505 */
506 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
507 {
508         struct smb2_private *smb = talloc_get_type(c->transport.private_data,
509                                                    struct smb2_private);
510         return smb->tree;
511 }