6b43de3358ace0020b7c7bfd0ceec5398bcaa338
[sfrench/samba-autobuild/.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 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 = 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 = 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 = 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 = 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 = 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 = 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, BOOL trigger_read)
314 {
315         struct smb_private *smb = c->transport.private_data;
316         union smb_write io;
317         struct smbcli_request *req;
318
319         if (smb->dead) {
320                 return NT_STATUS_CONNECTION_DISCONNECTED;
321         }
322
323         if (trigger_read) {
324                 return smb_send_trans_request(c, blob);
325         }
326
327         io.generic.level = RAW_WRITE_WRITEX;
328         io.writex.in.file.fnum = smb->fnum;
329         io.writex.in.offset = 0;
330         io.writex.in.wmode = PIPE_START_MESSAGE;
331         io.writex.in.remaining = blob->length;
332         io.writex.in.count = blob->length;
333         io.writex.in.data = blob->data;
334
335         /* we must not timeout at the smb level for rpc requests, as otherwise
336            signing/sealing can be messed up */
337         smb->tree->session->transport->options.request_timeout = 0;
338
339         req = smb_raw_write_send(smb->tree, &io);
340         if (req == NULL) {
341                 return NT_STATUS_NO_MEMORY;
342         }
343
344         req->async.fn = smb_write_callback;
345         req->async.private = c;
346
347         if (trigger_read) {
348                 send_read_request(c);
349         }
350
351         return NT_STATUS_OK;
352 }
353
354 /* 
355    shutdown SMB pipe connection
356 */
357 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c, NTSTATUS status)
358 {
359         struct smb_private *smb = c->transport.private_data;
360         union smb_close io;
361         struct smbcli_request *req;
362
363         /* maybe we're still starting up */
364         if (!smb) return status;
365
366         io.close.level = RAW_CLOSE_CLOSE;
367         io.close.in.file.fnum = smb->fnum;
368         io.close.in.write_time = 0;
369         req = smb_raw_close_send(smb->tree, &io);
370         if (req != NULL) {
371                 /* we don't care if this fails, so just free it if it succeeds */
372                 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
373         }
374
375         talloc_free(smb);
376
377         return status;
378 }
379
380 /*
381   return SMB server name (called name)
382 */
383 static const char *smb_peer_name(struct dcerpc_connection *c)
384 {
385         struct smb_private *smb = c->transport.private_data;
386         return smb->server_name;
387 }
388
389 /*
390   return remote name we make the actual connection (good for kerberos) 
391 */
392 static const char *smb_target_hostname(struct dcerpc_connection *c)
393 {
394         struct smb_private *smb = talloc_get_type(c->transport.private_data, struct smb_private);
395         return smb->tree->session->transport->socket->hostname;
396 }
397
398 /*
399   fetch the user session key 
400 */
401 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
402 {
403         struct smb_private *smb = c->transport.private_data;
404
405         if (smb->tree->session->user_session_key.data) {
406                 *session_key = smb->tree->session->user_session_key;
407                 return NT_STATUS_OK;
408         }
409         return NT_STATUS_NO_USER_SESSION_KEY;
410 }
411
412 struct pipe_open_smb_state {
413         union smb_open *open;
414         struct dcerpc_connection *c;
415         struct smbcli_tree *tree;
416         struct composite_context *ctx;
417 };
418
419 static void pipe_open_recv(struct smbcli_request *req);
420
421 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_pipe *p, 
422                                                     struct smbcli_tree *tree,
423                                                     const char *pipe_name)
424 {
425         struct composite_context *ctx;
426         struct pipe_open_smb_state *state;
427         struct smbcli_request *req;
428         struct dcerpc_connection *c = p->conn;
429
430         /* if we don't have a binding on this pipe yet, then create one */
431         if (p->binding == NULL) {
432                 NTSTATUS status;
433                 char *s = talloc_asprintf(p, "ncacn_np:%s", tree->session->transport->socket->hostname);
434                 if (s == NULL) return NULL;
435                 status = dcerpc_parse_binding(p, s, &p->binding);
436                 talloc_free(s);
437                 if (!NT_STATUS_IS_OK(status)) {
438                         return NULL;
439                 }
440         }
441
442         ctx = composite_create(c, c->event_ctx);
443         if (ctx == NULL) return NULL;
444
445         state = talloc(ctx, struct pipe_open_smb_state);
446         if (composite_nomem(state, ctx)) return ctx;
447         ctx->private_data = state;
448
449         state->c = c;
450         state->tree = tree;
451         state->ctx = ctx;
452
453         state->open = talloc(state, union smb_open);
454         if (composite_nomem(state->open, ctx)) return ctx;
455
456         state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
457         state->open->ntcreatex.in.flags = 0;
458         state->open->ntcreatex.in.root_fid = 0;
459         state->open->ntcreatex.in.access_mask = 
460                 SEC_STD_READ_CONTROL |
461                 SEC_FILE_WRITE_ATTRIBUTE |
462                 SEC_FILE_WRITE_EA |
463                 SEC_FILE_READ_DATA |
464                 SEC_FILE_WRITE_DATA;
465         state->open->ntcreatex.in.file_attr = 0;
466         state->open->ntcreatex.in.alloc_size = 0;
467         state->open->ntcreatex.in.share_access = 
468                 NTCREATEX_SHARE_ACCESS_READ |
469                 NTCREATEX_SHARE_ACCESS_WRITE;
470         state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
471         state->open->ntcreatex.in.create_options = 0;
472         state->open->ntcreatex.in.impersonation =
473                 NTCREATEX_IMPERSONATION_IMPERSONATION;
474         state->open->ntcreatex.in.security_flags = 0;
475
476         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
477             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
478                 pipe_name += 6;
479         }
480         state->open->ntcreatex.in.fname =
481                 (pipe_name[0] == '\\') ?
482                 talloc_strdup(state->open, pipe_name) :
483                 talloc_asprintf(state->open, "\\%s", pipe_name);
484         if (composite_nomem(state->open->ntcreatex.in.fname, ctx)) return ctx;
485
486         req = smb_raw_open_send(tree, state->open);
487         composite_continue_smb(ctx, req, pipe_open_recv, state);
488         return ctx;
489 }
490
491 static void pipe_open_recv(struct smbcli_request *req)
492 {
493         struct pipe_open_smb_state *state = talloc_get_type(req->async.private,
494                                             struct pipe_open_smb_state);
495         struct composite_context *ctx = state->ctx;
496         struct dcerpc_connection *c = state->c;
497         struct smb_private *smb;
498         
499         ctx->status = smb_raw_open_recv(req, state, state->open);
500         if (!composite_is_ok(ctx)) return;
501
502         /*
503           fill in the transport methods
504         */
505         c->transport.transport       = NCACN_NP;
506         c->transport.private_data    = NULL;
507         c->transport.shutdown_pipe   = smb_shutdown_pipe;
508         c->transport.peer_name       = smb_peer_name;
509         c->transport.target_hostname = smb_target_hostname;
510
511         c->transport.send_request    = smb_send_request;
512         c->transport.send_read       = send_read_request;
513         c->transport.recv_data       = NULL;
514         
515         /* Over-ride the default session key with the SMB session key */
516         c->security_state.session_key = smb_session_key;
517
518         smb = talloc(c, struct smb_private);
519         if (composite_nomem(smb, ctx)) return;
520
521         smb->fnum       = state->open->ntcreatex.out.file.fnum;
522         smb->tree       = talloc_reference(smb, state->tree);
523         smb->server_name= strupper_talloc(smb,
524                           state->tree->session->transport->called.name);
525         if (composite_nomem(smb->server_name, ctx)) return;
526         smb->dead       = false;
527
528         c->transport.private_data = smb;
529
530         composite_done(ctx);
531 }
532
533 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
534 {
535         NTSTATUS status = composite_wait(c);
536         talloc_free(c);
537         return status;
538 }
539
540 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
541                               struct smbcli_tree *tree,
542                               const char *pipe_name)
543 {
544         struct composite_context *ctx = dcerpc_pipe_open_smb_send(p, tree,
545                                                                   pipe_name);
546         return dcerpc_pipe_open_smb_recv(ctx);
547 }
548
549 /*
550   return the SMB tree used for a dcerpc over SMB pipe
551 */
552 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
553 {
554         struct smb_private *smb;
555
556         if (c->transport.transport != NCACN_NP) return NULL;
557
558         smb = talloc_get_type(c->transport.private_data, struct smb_private);
559         if (!smb) return NULL;
560
561         return smb->tree;
562 }
563
564 /*
565   return the SMB fnum used for a dcerpc over SMB pipe (hack for torture operations)
566 */
567 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c)
568 {
569         struct smb_private *smb;
570
571         if (c->transport.transport != NCACN_NP) return 0;
572
573         smb = talloc_get_type(c->transport.private_data, struct smb_private);
574         if (!smb) return 0;
575
576         return smb->fnum;
577 }