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