edeb898d5faca96b57cf7c28894313ea5facb03d
[metze/samba/wip.git] / source4 / 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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "librpc/rpc/dcerpc.h"
28
29 /* transport private information used by SMB pipe transport */
30 struct smb_private {
31         uint16_t fnum;
32         struct smbcli_tree *tree;
33         const char *server_name;
34         bool dead;
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         struct smb_private *smb = c->transport.private_data;
44
45         if (smb->dead) {
46                 return;
47         }
48
49         smb->dead = true;
50
51         if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
52                 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
53         }
54
55         if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
56                 status = NT_STATUS_END_OF_FILE;
57         }
58
59         if (c->transport.recv_data) {
60                 c->transport.recv_data(c, NULL, status);
61         }
62 }
63
64
65 /* 
66    this holds the state of an in-flight call
67 */
68 struct smb_read_state {
69         struct dcerpc_connection *c;
70         struct smbcli_request *req;
71         size_t received;
72         DATA_BLOB data;
73         union smb_read *io;
74 };
75
76 /*
77   called when a read request has completed
78 */
79 static void smb_read_callback(struct smbcli_request *req)
80 {
81         struct smb_private *smb;
82         struct smb_read_state *state;
83         union smb_read *io;
84         uint16_t frag_length;
85         NTSTATUS status;
86
87         state = talloc_get_type(req->async.private, struct smb_read_state);
88         smb = talloc_get_type(state->c->transport.private_data, struct smb_private);
89         io = state->io;
90
91         status = smb_raw_read_recv(state->req, io);
92         if (NT_STATUS_IS_ERR(status)) {
93                 pipe_dead(state->c, status);
94                 talloc_free(state);
95                 return;
96         }
97
98         state->received += io->readx.out.nread;
99
100         if (state->received < 16) {
101                 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
102                          (int)state->received));
103                 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
104                 talloc_free(state);
105                 return;
106         }
107
108         frag_length = dcerpc_get_frag_length(&state->data);
109
110         if (frag_length <= state->received) {
111                 DATA_BLOB data = state->data;
112                 struct dcerpc_connection *c = state->c;
113                 data.length = state->received;
114                 talloc_steal(state->c, data.data);
115                 talloc_free(state);
116                 c->transport.recv_data(c, &data, NT_STATUS_OK);
117                 return;
118         }
119
120         /* initiate another read request, as we only got part of a fragment */
121         state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
122
123         io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag, 
124                                   frag_length - state->received);
125         io->readx.in.maxcnt = io->readx.in.mincnt;
126         io->readx.out.data = state->data.data + state->received;
127
128         state->req = smb_raw_read_send(smb->tree, io);
129         if (state->req == NULL) {
130                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
131                 talloc_free(state);
132                 return;
133         }
134
135         state->req->async.fn = smb_read_callback;
136         state->req->async.private = state;
137 }
138
139 /*
140   trigger a read request from the server, possibly with some initial
141   data in the read buffer
142 */
143 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
144 {
145         struct smb_private *smb = c->transport.private_data;
146         union smb_read *io;
147         struct smb_read_state *state;
148         struct smbcli_request *req;
149
150         state = talloc(smb, struct smb_read_state);
151         if (state == NULL) {
152                 return NT_STATUS_NO_MEMORY;
153         }
154
155         state->c = c;
156         if (blob == NULL) {
157                 state->received = 0;
158                 state->data = data_blob_talloc(state, NULL, 0x2000);
159         } else {
160                 uint32_t frag_length = blob->length>=16?
161                         dcerpc_get_frag_length(blob):0x2000;
162                 state->received = blob->length;
163                 state->data = data_blob_talloc(state, NULL, frag_length);
164                 if (!state->data.data) {
165                         talloc_free(state);
166                         return NT_STATUS_NO_MEMORY;
167                 }
168                 memcpy(state->data.data, blob->data, blob->length);
169         }
170
171         state->io = talloc(state, union smb_read);
172
173         io = state->io;
174         io->generic.level = RAW_READ_READX;
175         io->readx.in.file.fnum = smb->fnum;
176         io->readx.in.mincnt = state->data.length - state->received;
177         io->readx.in.maxcnt = io->readx.in.mincnt;
178         io->readx.in.offset = 0;
179         io->readx.in.remaining = 0;
180         io->readx.in.read_for_execute = False;
181         io->readx.out.data = state->data.data + state->received;
182         req = smb_raw_read_send(smb->tree, io);
183         if (req == NULL) {
184                 return NT_STATUS_NO_MEMORY;
185         }
186
187         req->async.fn = smb_read_callback;
188         req->async.private = state;
189
190         state->req = req;
191
192         return NT_STATUS_OK;
193 }
194
195
196 /*
197   trigger a read request from the server
198 */
199 static NTSTATUS send_read_request(struct dcerpc_connection *c)
200 {
201         struct smb_private *smb = c->transport.private_data;
202
203         if (smb->dead) {
204                 return NT_STATUS_CONNECTION_DISCONNECTED;
205         }
206
207         return send_read_request_continue(c, NULL);
208 }
209
210 /* 
211    this holds the state of an in-flight trans call
212 */
213 struct smb_trans_state {
214         struct dcerpc_connection *c;
215         struct smbcli_request *req;
216         struct smb_trans2 *trans;
217 };
218
219 /*
220   called when a trans request has completed
221 */
222 static void smb_trans_callback(struct smbcli_request *req)
223 {
224         struct smb_trans_state *state = req->async.private;
225         struct dcerpc_connection *c = state->c;
226         NTSTATUS status;
227
228         status = smb_raw_trans_recv(req, state, state->trans);
229
230         if (NT_STATUS_IS_ERR(status)) {
231                 pipe_dead(c, status);
232                 return;
233         }
234
235         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
236                 DATA_BLOB data = state->trans->out.data;
237                 talloc_steal(c, data.data);
238                 talloc_free(state);
239                 c->transport.recv_data(c, &data, NT_STATUS_OK);
240                 return;
241         }
242
243         /* there is more to receive - setup a readx */
244         send_read_request_continue(c, &state->trans->out.data);
245         talloc_free(state);
246 }
247
248 /*
249   send a SMBtrans style request
250 */
251 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
252 {
253         struct smb_private *smb = c->transport.private_data;
254         struct smb_trans2 *trans;
255         uint16_t setup[2];
256         struct smb_trans_state *state;
257
258         state = talloc(smb, struct smb_trans_state);
259         if (state == NULL) {
260                 return NT_STATUS_NO_MEMORY;
261         }
262
263         state->c = c;
264         state->trans = talloc(state, struct smb_trans2);
265         trans = state->trans;
266
267         trans->in.data = *blob;
268         trans->in.params = data_blob(NULL, 0);
269         
270         setup[0] = TRANSACT_DCERPCCMD;
271         setup[1] = smb->fnum;
272
273         trans->in.max_param = 0;
274         trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
275         trans->in.max_setup = 0;
276         trans->in.setup_count = 2;
277         trans->in.flags = 0;
278         trans->in.timeout = 0;
279         trans->in.setup = setup;
280         trans->in.trans_name = "\\PIPE\\";
281
282         state->req = smb_raw_trans_send(smb->tree, trans);
283         if (state->req == NULL) {
284                 talloc_free(state);
285                 return NT_STATUS_NO_MEMORY;
286         }
287
288         state->req->async.fn = smb_trans_callback;
289         state->req->async.private = state;
290
291         talloc_steal(state, state->req);
292
293         return NT_STATUS_OK;
294 }
295
296 /*
297   called when a write request has completed
298 */
299 static void smb_write_callback(struct smbcli_request *req)
300 {
301         struct dcerpc_connection *c = req->async.private;
302
303         if (!NT_STATUS_IS_OK(req->status)) {
304                 DEBUG(0,("dcerpc_smb: write callback error\n"));
305                 pipe_dead(c, req->status);
306         }
307
308         smbcli_request_destroy(req);
309 }
310
311 /* 
312    send a packet to the server
313 */
314 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
315 {
316         struct smb_private *smb = 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 = 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 = 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 = 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 = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
435                 if (s == NULL) return NULL;
436                 status = dcerpc_parse_binding(p, s, &p->binding);
437                 talloc_free(s);
438                 if (!NT_STATUS_IS_OK(status)) {
439                         return NULL;
440                 }
441         }
442
443         ctx = composite_create(c, c->event_ctx);
444         if (ctx == NULL) return NULL;
445
446         state = talloc(ctx, struct pipe_open_smb_state);
447         if (composite_nomem(state, ctx)) return ctx;
448         ctx->private_data = state;
449
450         state->c = c;
451         state->tree = tree;
452         state->ctx = ctx;
453
454         state->open = talloc(state, union smb_open);
455         if (composite_nomem(state->open, ctx)) return ctx;
456
457         state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
458         state->open->ntcreatex.in.flags = 0;
459         state->open->ntcreatex.in.root_fid = 0;
460         state->open->ntcreatex.in.access_mask = 
461                 SEC_STD_READ_CONTROL |
462                 SEC_FILE_WRITE_ATTRIBUTE |
463                 SEC_FILE_WRITE_EA |
464                 SEC_FILE_READ_DATA |
465                 SEC_FILE_WRITE_DATA;
466         state->open->ntcreatex.in.file_attr = 0;
467         state->open->ntcreatex.in.alloc_size = 0;
468         state->open->ntcreatex.in.share_access = 
469                 NTCREATEX_SHARE_ACCESS_READ |
470                 NTCREATEX_SHARE_ACCESS_WRITE;
471         state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
472         state->open->ntcreatex.in.create_options = 0;
473         state->open->ntcreatex.in.impersonation =
474                 NTCREATEX_IMPERSONATION_IMPERSONATION;
475         state->open->ntcreatex.in.security_flags = 0;
476
477         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
478             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
479                 pipe_name += 6;
480         }
481         state->open->ntcreatex.in.fname =
482                 (pipe_name[0] == '\\') ?
483                 talloc_strdup(state->open, pipe_name) :
484                 talloc_asprintf(state->open, "\\%s", pipe_name);
485         if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
486
487         req = smb_raw_open_send(tree, state->open);
488         composite_continue_smb(ctx, req, pipe_open_recv, state);
489         return ctx;
490 }
491
492 static void pipe_open_recv(struct smbcli_request *req)
493 {
494         struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
495                                             struct pipe_open_smb_state);
496         struct composite_context *ctx = state->ctx;
497         struct dcerpc_connection *c = state->c;
498         struct smb_private *smb;
499         
500         ctx->status = smb_raw_open_recv(req, state, state->open);
501         if (!composite_is_ok(ctx)) return;
502
503         /*
504           fill in the transport methods
505         */
506         c->transport.transport       = NCACN_NP;
507         c->transport.private_data    = NULL;
508         c->transport.shutdown_pipe   = smb_shutdown_pipe;
509         c->transport.peer_name       = smb_peer_name;
510         c->transport.target_hostname = smb_target_hostname;
511
512         c->transport.send_request    = smb_send_request;
513         c->transport.send_read       = send_read_request;
514         c->transport.recv_data       = NULL;
515         
516         /* Over-ride the default session key with the SMB session key */
517         c->security_state.session_key = smb_session_key;
518
519         smb = talloc(c, struct smb_private);
520         if (composite_nomem(smb, ctx)) return;
521
522         smb->fnum       = state->open->ntcreatex.out.file.fnum;
523         smb->tree       = talloc_reference(smb, state->tree);
524         smb->server_name= strupper_talloc(smb,
525                           state->tree->session->transport->called.name);
526         if (composite_nomem(smb->server_name, ctx)) return;
527         smb->dead       = false;
528
529         c->transport.private_data = smb;
530
531         composite_done(ctx);
532 }
533
534 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
535 {
536         NTSTATUS status = composite_wait(c);
537         talloc_free(c);
538         return status;
539 }
540
541 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
542                               struct smbcli_tree *tree,
543                               const char *pipe_name)
544 {
545         struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
546                                                                   pipe_name);
547         return dcerpc_pipe_open_smb_recv(ctx);
548 }
549
550 /*
551   return the SMB tree used for a dcerpc over SMB pipe
552 */
553 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
554 {
555         struct smb_private *smb;
556
557         if (c->transport.transport != NCACN_NP) return NULL;
558
559         smb = talloc_get_type(c->transport.private_data, struct smb_private);
560         if (!smb) return NULL;
561
562         return smb->tree;
563 }
564
565 /*
566   return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
567 */
568 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
569 {
570         struct smb_private *smb;
571
572         if (c->transport.transport != NCACN_NP) return 0;
573
574         smb = talloc_get_type(c->transport.private_data, struct smb_private);
575         if (!smb) return 0;
576
577         return smb->fnum;
578 }