d22ef6a9f24ef45621228df64a23208ceb4f6f5c
[mat/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "libcli/raw/ioctl.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 };
36
37
38 /*
39   tell the dcerpc layer that the transport is dead
40 */
41 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
42 {
43         c->transport.recv_data(c, NULL, status);
44 }
45
46
47 /* 
48    this holds the state of an in-flight call
49 */
50 struct smb2_read_state {
51         struct dcerpc_connection *c;
52         DATA_BLOB data;
53 };
54
55 /*
56   called when a read request has completed
57 */
58 static void smb2_read_callback(struct smb2_request *req)
59 {
60         struct smb2_private *smb;
61         struct smb2_read_state *state;
62         struct smb2_read io;
63         uint16_t frag_length;
64         NTSTATUS status;
65
66         state = talloc_get_type(req->async.private, struct smb2_read_state);
67         smb = talloc_get_type(state->c->transport.private, struct smb2_private);
68
69         status = smb2_read_recv(req, state, &io);
70         if (NT_STATUS_IS_ERR(status)) {
71                 pipe_dead(state->c, status);
72                 talloc_free(state);
73                 return;
74         }
75
76         status = data_blob_append(state, &state->data, 
77                                   io.out.data.data, io.out.data.length);
78         if (NT_STATUS_IS_ERR(status)) {
79                 pipe_dead(state->c, status);
80                 talloc_free(state);
81                 return;
82         }
83
84         if (state->data.length < 16) {
85                 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
86                          (int)state->data.length));
87                 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
88                 talloc_free(state);
89                 return;
90         }
91
92         frag_length = dcerpc_get_frag_length(&state->data);
93
94         if (frag_length <= state->data.length) {
95                 DATA_BLOB data = state->data;
96                 struct dcerpc_connection *c = state->c;
97                 talloc_steal(c, data.data);
98                 talloc_free(state);
99                 c->transport.recv_data(c, &data, NT_STATUS_OK);
100                 return;
101         }
102
103         /* initiate another read request, as we only got part of a fragment */
104         ZERO_STRUCT(io);
105         io.in.length = MIN(state->c->srv_max_xmit_frag, 
106                            frag_length - state->data.length);
107         if (io.in.length < 16) {
108                 io.in.length = 16;
109         }
110         io.in.handle = smb->handle;
111         
112         req = smb2_read_send(smb->tree, &io);
113         if (req == NULL) {
114                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
115                 talloc_free(state);
116                 return;
117         }
118
119         req->async.fn = smb2_read_callback;
120         req->async.private = state;
121 }
122
123
124 /*
125   trigger a read request from the server, possibly with some initial
126   data in the read buffer
127 */
128 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
129 {
130         struct smb2_private *smb = c->transport.private;
131         struct smb2_read io;
132         struct smb2_read_state *state;
133         struct smb2_request *req;
134
135         state = talloc(smb, struct smb2_read_state);
136         if (state == NULL) {
137                 return NT_STATUS_NO_MEMORY;
138         }
139
140         state->c = c;
141         if (blob == NULL) {
142                 state->data = data_blob(NULL, 0);
143         } else {
144                 state->data = *blob;
145                 talloc_steal(state, state->data.data);
146         }
147
148         ZERO_STRUCT(io);
149         io.in.handle = smb->handle;
150
151         if (state->data.length >= 16) {
152                 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
153                 io.in.length = frag_length - state->data.length;
154         } else {
155                 io.in.length = 0x2000;
156         }
157
158         req = smb2_read_send(smb->tree, &io);
159         if (req == NULL) {
160                 return NT_STATUS_NO_MEMORY;
161         }
162
163         req->async.fn = smb2_read_callback;
164         req->async.private = state;
165
166         return NT_STATUS_OK;
167 }
168
169
170 /*
171   trigger a read request from the server
172 */
173 static NTSTATUS send_read_request(struct dcerpc_connection *c)
174 {
175         return send_read_request_continue(c, NULL);
176 }
177
178 /* 
179    this holds the state of an in-flight trans call
180 */
181 struct smb2_trans_state {
182         struct dcerpc_connection *c;
183 };
184
185 /*
186   called when a trans request has completed
187 */
188 static void smb2_trans_callback(struct smb2_request *req)
189 {
190         struct smb2_trans_state *state = talloc_get_type(req->async.private,
191                                                         struct smb2_trans_state);
192         struct dcerpc_connection *c = state->c;
193         NTSTATUS status;
194         struct smb2_ioctl io;
195
196         status = smb2_ioctl_recv(req, state, &io);
197         if (NT_STATUS_IS_ERR(status)) {
198                 pipe_dead(c, status);
199                 return;
200         }
201
202         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
203                 DATA_BLOB data = io.out.out;
204                 talloc_steal(c, data.data);
205                 talloc_free(state);
206                 c->transport.recv_data(c, &data, NT_STATUS_OK);
207                 return;
208         }
209
210         /* there is more to receive - setup a read */
211         send_read_request_continue(c, &io.out.out);
212         talloc_free(state);
213 }
214
215 /*
216   send a SMBtrans style request, using a named pipe read_write fsctl
217 */
218 static NTSTATUS smb2_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
219 {
220         struct smb2_private *smb = talloc_get_type(c->transport.private,
221                                                    struct smb2_private);
222         struct smb2_ioctl io;
223         struct smb2_trans_state *state;
224         struct smb2_request *req;
225
226         state = talloc(smb, struct smb2_trans_state);
227         if (state == NULL) {
228                 return NT_STATUS_NO_MEMORY;
229         }
230
231         state->c = c;
232         
233         ZERO_STRUCT(io);
234         io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
235         io.in.handle = smb->handle;
236         io.in.max_response_size = 0x1000;
237         io.in.flags = 1;
238         io.in.out = *blob;
239
240         req = smb2_ioctl_send(smb->tree, &io);
241         if (req == NULL) {
242                 talloc_free(state);
243                 return NT_STATUS_NO_MEMORY;
244         }
245
246         req->async.fn = smb2_trans_callback;
247         req->async.private = state;
248
249         talloc_steal(state, req);
250
251         return NT_STATUS_OK;
252 }
253
254 /*
255   called when a write request has completed
256 */
257 static void smb2_write_callback(struct smb2_request *req)
258 {
259         struct dcerpc_connection *c = req->async.private;
260
261         if (!NT_STATUS_IS_OK(req->status)) {
262                 DEBUG(0,("dcerpc_smb2: write callback error\n"));
263                 pipe_dead(c, req->status);
264         }
265
266         smb2_request_destroy(req);
267 }
268
269 /* 
270    send a packet to the server
271 */
272 static NTSTATUS smb2_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, 
273                                   BOOL trigger_read)
274 {
275         struct smb2_private *smb = c->transport.private;
276         struct smb2_write io;
277         struct smb2_request *req;
278
279         if (trigger_read) {
280                 return smb2_send_trans_request(c, blob);
281         }
282
283         ZERO_STRUCT(io);
284         io.in.handle = smb->handle;
285         io.in.data = *blob;
286
287         req = smb2_write_send(smb->tree, &io);
288         if (req == NULL) {
289                 return NT_STATUS_NO_MEMORY;
290         }
291
292         req->async.fn = smb2_write_callback;
293         req->async.private = c;
294
295         return NT_STATUS_OK;
296 }
297
298 /* 
299    shutdown SMB pipe connection
300 */
301 static NTSTATUS smb2_shutdown_pipe(struct dcerpc_connection *c)
302 {
303         struct smb2_private *smb = c->transport.private;
304         struct smb2_close io;
305         struct smb2_request *req;
306
307         /* maybe we're still starting up */
308         if (!smb) return NT_STATUS_OK;
309
310         ZERO_STRUCT(io);
311         io.in.handle = smb->handle;
312         req = smb2_close_send(smb->tree, &io);
313         if (req != NULL) {
314                 /* we don't care if this fails, so just free it if it succeeds */
315                 req->async.fn = (void (*)(struct smb2_request *))talloc_free;
316         }
317
318         talloc_free(smb);
319
320         return NT_STATUS_OK;
321 }
322
323 /*
324   return SMB server name
325 */
326 static const char *smb2_peer_name(struct dcerpc_connection *c)
327 {
328         struct smb2_private *smb = talloc_get_type(c->transport.private,
329                                                    struct smb2_private);
330         return smb->server_name;
331 }
332
333 /*
334   fetch the user session key 
335 */
336 static NTSTATUS smb2_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
337 {
338         struct smb2_private *smb = talloc_get_type(c->transport.private,
339                                                    struct smb2_private);
340         *session_key = smb->tree->session->session_key;
341         if (session_key->data == NULL) {
342                 return NT_STATUS_NO_USER_SESSION_KEY;
343         }
344         return NT_STATUS_OK;
345 }
346
347 struct pipe_open_smb2_state {
348         struct dcerpc_connection *c;
349         struct composite_context *ctx;
350 };
351
352 static void pipe_open_recv(struct smb2_request *req);
353
354 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_connection *c, 
355                                                      struct smb2_tree *tree,
356                                                      const char *pipe_name)
357 {
358         struct composite_context *ctx;
359         struct pipe_open_smb2_state *state;
360         struct smb2_create io;
361         struct smb2_request *req;
362
363         ctx = talloc_zero(NULL, struct composite_context);
364         if (ctx == NULL) goto failed;
365         ctx->state = COMPOSITE_STATE_IN_PROGRESS;
366         ctx->event_ctx = talloc_reference(c, c->event_ctx);
367
368         state = talloc(ctx, struct pipe_open_smb2_state);
369         if (state == NULL) goto failed;
370
371         state->c = c;
372         state->ctx = ctx;
373
374         ZERO_STRUCT(io);
375         io.in.access_mask = 
376                 SEC_STD_READ_CONTROL |
377                 SEC_FILE_READ_ATTRIBUTE |
378                 SEC_FILE_WRITE_ATTRIBUTE |
379                 SEC_STD_SYNCHRONIZE |
380                 SEC_FILE_READ_EA |
381                 SEC_FILE_WRITE_EA |
382                 SEC_FILE_READ_DATA |
383                 SEC_FILE_WRITE_DATA |
384                 SEC_FILE_APPEND_DATA;
385         io.in.share_access = 
386                 NTCREATEX_SHARE_ACCESS_READ |
387                 NTCREATEX_SHARE_ACCESS_WRITE;
388         io.in.open_disposition = NTCREATEX_DISP_OPEN;
389         io.in.create_options   = 
390                 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE | 
391                 NTCREATEX_OPTIONS_UNKNOWN_400000;
392         io.in.impersonation    = NTCREATEX_IMPERSONATION_IMPERSONATION;
393
394         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
395             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
396                 pipe_name += 6;
397         }
398         io.in.fname = pipe_name;
399
400         req = smb2_create_send(tree, &io);
401         if (req == NULL) goto failed;
402
403         req->async.fn = pipe_open_recv;
404         req->async.private = state;
405
406         return ctx;
407
408  failed:
409         talloc_free(ctx);
410         return NULL;
411 }
412
413 static void pipe_open_recv(struct smb2_request *req)
414 {
415         struct pipe_open_smb2_state *state =
416                 talloc_get_type(req->async.private,
417                                 struct pipe_open_smb2_state);
418         struct composite_context *ctx = state->ctx;
419         struct dcerpc_connection *c = state->c;
420         struct smb2_tree *tree = req->tree;
421         struct smb2_private *smb;
422         struct smb2_create io;
423
424         ctx->status = smb2_create_recv(req, state, &io);
425         if (!composite_is_ok(ctx)) return;
426
427         /*
428           fill in the transport methods
429         */
430         c->transport.transport = NCACN_NP;
431         c->transport.private = NULL;
432         c->transport.shutdown_pipe = smb2_shutdown_pipe;
433         c->transport.peer_name = smb2_peer_name;
434
435         c->transport.send_request = smb2_send_request;
436         c->transport.send_read = send_read_request;
437         c->transport.recv_data = NULL;
438         
439         /* Over-ride the default session key with the SMB session key */
440         c->security_state.session_key = smb2_session_key;
441
442         smb = talloc(c, struct smb2_private);
443         if (composite_nomem(smb, ctx)) return;
444
445         smb->handle     = io.out.handle;
446         smb->tree       = talloc_reference(smb, tree);
447         smb->server_name= strupper_talloc(smb, 
448                                           tree->session->transport->socket->hostname);
449         if (composite_nomem(smb->server_name, ctx)) return;
450
451         c->transport.private = smb;
452
453         composite_done(ctx);
454 }
455
456 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
457 {
458         NTSTATUS status = composite_wait(c);
459         talloc_free(c);
460         return status;
461 }
462
463 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_connection *c,
464                                struct smb2_tree *tree,
465                                const char *pipe_name)
466 {
467         struct composite_context *ctx = dcerpc_pipe_open_smb2_send(c, tree, pipe_name);
468         return dcerpc_pipe_open_smb2_recv(ctx);
469 }
470
471 /*
472   return the SMB2 tree used for a dcerpc over SMB2 pipe
473 */
474 struct smb2_tree *dcerpc_smb2_tree(struct dcerpc_connection *c)
475 {
476         struct smb2_private *smb = talloc_get_type(c->transport.private,
477                                                    struct smb2_private);
478         return smb->tree;
479 }