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