Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba.git] / source / librpc / rpc / dcerpc_smb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc over SMB transport
5
6    Copyright (C) Tim Potter 2003
7    Copyright (C) Andrew Tridgell 2003
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/composite/composite.h"
26 #include "librpc/rpc/dcerpc.h"
27
28 /* transport private information used by SMB pipe transport */
29 struct smb_private {
30         uint16_t fnum;
31         struct smbcli_tree *tree;
32         const char *server_name;
33         bool dead;
34 };
35
36
37 /*
38   tell the dcerpc layer that the transport is dead
39 */
40 static void pipe_dead(struct dcerpc_connection *c, NTSTATUS status)
41 {
42         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
43
44         if (smb->dead) {
45                 return;
46         }
47
48         smb->dead = true;
49
50         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
51                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
52         }
53
54         if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
55                 status = NT_STATUS_END_OF_FILE;
56         }
57
58         if (c->transport.recv_data) {
59                 c->transport.recv_data(c, NULL, status);
60         }
61 }
62
63
64 /* 
65    this holds the state of an in-flight call
66 */
67 struct smb_read_state {
68         struct dcerpc_connection *c;
69         struct smbcli_request *req;
70         size_t received;
71         DATA_BLOB data;
72         union smb_read *io;
73 };
74
75 /*
76   called when a read request has completed
77 */
78 static void smb_read_callback(struct smbcli_request *req)
79 {
80         struct smb_private *smb;
81         struct smb_read_state *state;
82         union smb_read *io;
83         uint16_t frag_length;
84         NTSTATUS status;
85
86         state = talloc_get_type(req->async.private, struct smb_read_state);
87         smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
88         io = state->io;
89
90         status = smb_raw_read_recv(state->req, io);
91         if (NT_STATUS_IS_ERR(status)) {
92                 pipe_dead(state->c, status);
93                 talloc_free(state);
94                 return;
95         }
96
97         state->received += io->readx.out.nread;
98
99         if (state->received < 16) {
100                 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
101                          (int)state->received));
102                 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
103                 talloc_free(state);
104                 return;
105         }
106
107         frag_length = dcerpc_get_frag_length(&state->data);
108
109         if (frag_length <= state->received) {
110                 DATA_BLOB data = state->data;
111                 struct dcerpc_connection *c = state->c;
112                 data.length = state->received;
113                 talloc_steal(state->c, data.data);
114                 talloc_free(state);
115                 c->transport.recv_data(c, &data, NT_STATUS_OK);
116                 return;
117         }
118
119         /* initiate another read request, as we only got part of a fragment */
120         state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
121
122         io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag, 
123                                   frag_length - state->received);
124         io->readx.in.maxcnt = io->readx.in.mincnt;
125         io->readx.out.data = state->data.data + state->received;
126
127         state->req = smb_raw_read_send(smb->tree, io);
128         if (state->req == NULL) {
129                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
130                 talloc_free(state);
131                 return;
132         }
133
134         state->req->async.fn = smb_read_callback;
135         state->req->async.private = state;
136 }
137
138 /*
139   trigger a read request from the server, possibly with some initial
140   data in the read buffer
141 */
142 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
143 {
144         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
145         union smb_read *io;
146         struct smb_read_state *state;
147         struct smbcli_request *req;
148
149         state = talloc(smb, struct smb_read_state);
150         if (state == NULL) {
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         state->c = c;
155         if (blob == NULL) {
156                 state->received = 0;
157                 state->data = data_blob_talloc(state, NULL, 0x2000);
158         } else {
159                 uint32_t frag_length = blob->length>=16?
160                         dcerpc_get_frag_length(blob):0x2000;
161                 state->received = blob->length;
162                 state->data = data_blob_talloc(state, NULL, frag_length);
163                 if (!state->data.data) {
164                         talloc_free(state);
165                         return NT_STATUS_NO_MEMORY;
166                 }
167                 memcpy(state->data.data, blob->data, blob->length);
168         }
169
170         state->io = talloc(state, union smb_read);
171
172         io = state->io;
173         io->generic.level = RAW_READ_READX;
174         io->readx.in.file.fnum = smb->fnum;
175         io->readx.in.mincnt = state->data.length - state->received;
176         io->readx.in.maxcnt = io->readx.in.mincnt;
177         io->readx.in.offset = 0;
178         io->readx.in.remaining = 0;
179         io->readx.in.read_for_execute = false;
180         io->readx.out.data = state->data.data + state->received;
181         req = smb_raw_read_send(smb->tree, io);
182         if (req == NULL) {
183                 return NT_STATUS_NO_MEMORY;
184         }
185
186         req->async.fn = smb_read_callback;
187         req->async.private = state;
188
189         state->req = req;
190
191         return NT_STATUS_OK;
192 }
193
194
195 /*
196   trigger a read request from the server
197 */
198 static NTSTATUS send_read_request(struct dcerpc_connection *c)
199 {
200         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
201
202         if (smb->dead) {
203                 return NT_STATUS_CONNECTION_DISCONNECTED;
204         }
205
206         return send_read_request_continue(c, NULL);
207 }
208
209 /* 
210    this holds the state of an in-flight trans call
211 */
212 struct smb_trans_state {
213         struct dcerpc_connection *c;
214         struct smbcli_request *req;
215         struct smb_trans2 *trans;
216 };
217
218 /*
219   called when a trans request has completed
220 */
221 static void smb_trans_callback(struct smbcli_request *req)
222 {
223         struct smb_trans_state *state = (struct smb_trans_state *)req->async.private;
224         struct dcerpc_connection *c = state->c;
225         NTSTATUS status;
226
227         status = smb_raw_trans_recv(req, state, state->trans);
228
229         if (NT_STATUS_IS_ERR(status)) {
230                 pipe_dead(c, status);
231                 return;
232         }
233
234         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
235                 DATA_BLOB data = state->trans->out.data;
236                 talloc_steal(c, data.data);
237                 talloc_free(state);
238                 c->transport.recv_data(c, &data, NT_STATUS_OK);
239                 return;
240         }
241
242         /* there is more to receive - setup a readx */
243         send_read_request_continue(c, &state->trans->out.data);
244         talloc_free(state);
245 }
246
247 /*
248   send a SMBtrans style request
249 */
250 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
251 {
252         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
253         struct smb_trans2 *trans;
254         uint16_t setup[2];
255         struct smb_trans_state *state;
256
257         state = talloc(smb, struct smb_trans_state);
258         if (state == NULL) {
259                 return NT_STATUS_NO_MEMORY;
260         }
261
262         state->c = c;
263         state->trans = talloc(state, struct smb_trans2);
264         trans = state->trans;
265
266         trans->in.data = *blob;
267         trans->in.params = data_blob(NULL, 0);
268         
269         setup[0] = TRANSACT_DCERPCCMD;
270         setup[1] = smb->fnum;
271
272         trans->in.max_param = 0;
273         trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
274         trans->in.max_setup = 0;
275         trans->in.setup_count = 2;
276         trans->in.flags = 0;
277         trans->in.timeout = 0;
278         trans->in.setup = setup;
279         trans->in.trans_name = "\\PIPE\\";
280
281         state->req = smb_raw_trans_send(smb->tree, trans);
282         if (state->req == NULL) {
283                 talloc_free(state);
284                 return NT_STATUS_NO_MEMORY;
285         }
286
287         state->req->async.fn = smb_trans_callback;
288         state->req->async.private = state;
289
290         talloc_steal(state, state->req);
291
292         return NT_STATUS_OK;
293 }
294
295 /*
296   called when a write request has completed
297 */
298 static void smb_write_callback(struct smbcli_request *req)
299 {
300         struct dcerpc_connection *c = (struct dcerpc_connection *)req->async.private;
301
302         if (!NT_STATUS_IS_OK(req->status)) {
303                 DEBUG(0,("dcerpc_smb: write callback error\n"));
304                 pipe_dead(c, req->status);
305         }
306
307         smbcli_request_destroy(req);
308 }
309
310 /* 
311    send a packet to the server
312 */
313 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, 
314                                  bool trigger_read)
315 {
316         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
317         union smb_write io;
318         struct smbcli_request *req;
319
320         if (smb->dead) {
321                 return NT_STATUS_CONNECTION_DISCONNECTED;
322         }
323
324         if (trigger_read) {
325                 return smb_send_trans_request(c, blob);
326         }
327
328         io.generic.level = RAW_WRITE_WRITEX;
329         io.writex.in.file.fnum = smb->fnum;
330         io.writex.in.offset = 0;
331         io.writex.in.wmode = PIPE_START_MESSAGE;
332         io.writex.in.remaining = blob->length;
333         io.writex.in.count = blob->length;
334         io.writex.in.data = blob->data;
335
336         /* we must not timeout at the smb level for rpc requests, as otherwise
337            signing/sealing can be messed up */
338         smb->tree->session->transport->options.request_timeout = 0;
339
340         req = smb_raw_write_send(smb->tree, &io);
341         if (req == NULL) {
342                 return NT_STATUS_NO_MEMORY;
343         }
344
345         req->async.fn = smb_write_callback;
346         req->async.private = c;
347
348         if (trigger_read) {
349                 send_read_request(c);
350         }
351
352         return NT_STATUS_OK;
353 }
354
355 /* 
356    shutdown SMB pipe connection
357 */
358 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
359 {
360         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
361         union smb_close io;
362         struct smbcli_request *req;
363
364         /* maybe we're still starting up */
365         if (!smb) return status;
366
367         io.close.level = RAW_CLOSE_CLOSE;
368         io.close.in.file.fnum = smb->fnum;
369         io.close.in.write_time = 0;
370         req = smb_raw_close_send(smb->tree, &io);
371         if (req != NULL) {
372                 /* we don't care if this fails, so just free it if it succeeds */
373                 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
374         }
375
376         talloc_free(smb);
377
378         return status;
379 }
380
381 /*
382   return SMB server name (called name)
383 */
384 static const char *smb_peer_name(struct dcerpc_connection *c)
385 {
386         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
387         return smb->server_name;
388 }
389
390 /*
391   return remote name we make the actual connection (good for kerberos) 
392 */
393 static const char *smb_target_hostname(struct dcerpc_connection *c)
394 {
395         struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
396         return smb->tree->session->transport->socket->hostname;
397 }
398
399 /*
400   fetch the user session key 
401 */
402 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
403 {
404         struct smb_private *smb = (struct smb_private *)c->transport.private_data;
405
406         if (smb->tree->session->user_session_key.data) {
407                 *session_key = smb->tree->session->user_session_key;
408                 return NT_STATUS_OK;
409         }
410         return NT_STATUS_NO_USER_SESSION_KEY;
411 }
412
413 struct pipe_open_smb_state {
414         union smb_open *open;
415         struct dcerpc_connection *c;
416         struct smbcli_tree *tree;
417         struct composite_context *ctx;
418 };
419
420 static void pipe_open_recv(struct smbcli_request *req);
421
422 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p, 
423                                                     struct smbcli_tree *tree,
424                                                     const char *pipe_name)
425 {
426         struct composite_context *ctx;
427         struct pipe_open_smb_state *state;
428         struct smbcli_request *req;
429         struct dcerpc_connection *c = p->conn;
430
431         /* if we don't have a binding on this pipe yet, then create one */
432         if (p->binding == NULL) {
433                 NTSTATUS status;
434                 char *s;
435                 SMB_ASSERT(tree->session->transport->socket->hostname != NULL);
436                 s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
437                 if (s == NULL) return NULL;
438                 status = dcerpc_parse_binding(p, s, &p->binding);
439                 talloc_free(s);
440                 if (!NT_STATUS_IS_OK(status)) {
441                         return NULL;
442                 }
443         }
444
445         ctx = composite_create(c, c->event_ctx);
446         if (ctx == NULL) return NULL;
447
448         state = talloc(ctx, struct pipe_open_smb_state);
449         if (composite_nomem(state, ctx)) return ctx;
450         ctx->private_data = state;
451
452         state->c = c;
453         state->tree = tree;
454         state->ctx = ctx;
455
456         state->open = talloc(state, union smb_open);
457         if (composite_nomem(state->open, ctx)) return ctx;
458
459         state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
460         state->open->ntcreatex.in.flags = 0;
461         state->open->ntcreatex.in.root_fid = 0;
462         state->open->ntcreatex.in.access_mask = 
463                 SEC_STD_READ_CONTROL |
464                 SEC_FILE_WRITE_ATTRIBUTE |
465                 SEC_FILE_WRITE_EA |
466                 SEC_FILE_READ_DATA |
467                 SEC_FILE_WRITE_DATA;
468         state->open->ntcreatex.in.file_attr = 0;
469         state->open->ntcreatex.in.alloc_size = 0;
470         state->open->ntcreatex.in.share_access = 
471                 NTCREATEX_SHARE_ACCESS_READ |
472                 NTCREATEX_SHARE_ACCESS_WRITE;
473         state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
474         state->open->ntcreatex.in.create_options = 0;
475         state->open->ntcreatex.in.impersonation =
476                 NTCREATEX_IMPERSONATION_IMPERSONATION;
477         state->open->ntcreatex.in.security_flags = 0;
478
479         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
480             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
481                 pipe_name += 6;
482         }
483         state->open->ntcreatex.in.fname =
484                 (pipe_name[0] == '\\') ?
485                 talloc_strdup(state->open, pipe_name) :
486                 talloc_asprintf(state->open, "\\%s", pipe_name);
487         if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
488
489         req = smb_raw_open_send(tree, state->open);
490         composite_continue_smb(ctx, req, pipe_open_recv, state);
491         return ctx;
492 }
493
494 static void pipe_open_recv(struct smbcli_request *req)
495 {
496         struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
497                                             struct pipe_open_smb_state);
498         struct composite_context *ctx = state->ctx;
499         struct dcerpc_connection *c = state->c;
500         struct smb_private *smb;
501         
502         ctx->status = smb_raw_open_recv(req, state, state->open);
503         if (!composite_is_ok(ctx)) return;
504
505         /*
506           fill in the transport methods
507         */
508         c->transport.transport       = NCACN_NP;
509         c->transport.private_data    = NULL;
510         c->transport.shutdown_pipe   = smb_shutdown_pipe;
511         c->transport.peer_name       = smb_peer_name;
512         c->transport.target_hostname = smb_target_hostname;
513
514         c->transport.send_request    = smb_send_request;
515         c->transport.send_read       = send_read_request;
516         c->transport.recv_data       = NULL;
517         
518         /* Over-ride the default session key with the SMB session key */
519         c->security_state.session_key = smb_session_key;
520
521         smb = talloc(c, struct smb_private);
522         if (composite_nomem(smb, ctx)) return;
523
524         smb->fnum       = state->open->ntcreatex.out.file.fnum;
525         smb->tree       = talloc_reference(smb, state->tree);
526         smb->server_name= strupper_talloc(smb,
527                           state->tree->session->transport->called.name);
528         if (composite_nomem(smb->server_name, ctx)) return;
529         smb->dead       = false;
530
531         c->transport.private_data = smb;
532
533         composite_done(ctx);
534 }
535
536 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
537 {
538         NTSTATUS status = composite_wait(c);
539         talloc_free(c);
540         return status;
541 }
542
543 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
544                               struct smbcli_tree *tree,
545                               const char *pipe_name)
546 {
547         struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
548                                                                   pipe_name);
549         return dcerpc_pipe_open_smb_recv(ctx);
550 }
551
552 /*
553   return the SMB tree used for a dcerpc over SMB pipe
554 */
555 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
556 {
557         struct smb_private *smb;
558
559         if (c->transport.transport != NCACN_NP) return NULL;
560
561         smb = talloc_get_type(c->transport.private_data, struct smb_private);
562         if (!smb) return NULL;
563
564         return smb->tree;
565 }
566
567 /*
568   return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
569 */
570 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
571 {
572         struct smb_private *smb;
573
574         if (c->transport.transport != NCACN_NP) return 0;
575
576         smb = talloc_get_type(c->transport.private_data, struct smb_private);
577         if (!smb) return 0;
578
579         return smb->fnum;
580 }