r10681: Convert dcerpc_open_smb to a composite function.
[ira/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 "librpc/gen_ndr/ndr_security.h"
27 #include "libcli/composite/composite.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 };
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         c->transport.recv_data(c, NULL, status);
43 }
44
45
46 /* 
47    this holds the state of an in-flight call
48 */
49 struct smb_read_state {
50         struct dcerpc_connection *c;
51         struct smbcli_request *req;
52         size_t received;
53         DATA_BLOB data;
54         union smb_read *io;
55 };
56
57 /*
58   called when a read request has completed
59 */
60 static void smb_read_callback(struct smbcli_request *req)
61 {
62         struct smb_private *smb;
63         struct smb_read_state *state;
64         union smb_read *io;
65         uint16_t frag_length;
66         NTSTATUS status;
67
68         state = req->async.private;
69         smb = state->c->transport.private;
70         io = state->io;
71
72         status = smb_raw_read_recv(state->req, io);
73         if (NT_STATUS_IS_ERR(status)) {
74                 pipe_dead(state->c, status);
75                 talloc_free(state);
76                 return;
77         }
78
79         state->received += io->readx.out.nread;
80
81         if (state->received < 16) {
82                 DEBUG(0,("dcerpc_smb: short packet (length %d) in read callback!\n",
83                          (int)state->received));
84                 pipe_dead(state->c, NT_STATUS_INFO_LENGTH_MISMATCH);
85                 talloc_free(state);
86                 return;
87         }
88
89         frag_length = dcerpc_get_frag_length(&state->data);
90
91         if (frag_length <= state->received) {
92                 state->data.length = state->received;
93                 state->c->transport.recv_data(state->c, &state->data, NT_STATUS_OK);
94                 talloc_free(state);
95                 return;
96         }
97
98         /* initiate another read request, as we only got part of a fragment */
99         state->data.data = talloc_realloc(state, state->data.data, uint8_t, frag_length);
100
101         io->readx.in.mincnt = MIN(state->c->srv_max_xmit_frag, 
102                                   frag_length - state->received);
103         io->readx.in.maxcnt = io->readx.in.mincnt;
104         io->readx.out.data = state->data.data + state->received;
105
106         state->req = smb_raw_read_send(smb->tree, io);
107         if (state->req == NULL) {
108                 pipe_dead(state->c, NT_STATUS_NO_MEMORY);
109                 talloc_free(state);
110                 return;
111         }
112
113         state->req->async.fn = smb_read_callback;
114         state->req->async.private = state;
115 }
116
117 /*
118   trigger a read request from the server, possibly with some initial
119   data in the read buffer
120 */
121 static NTSTATUS send_read_request_continue(struct dcerpc_connection *c, DATA_BLOB *blob)
122 {
123         struct smb_private *smb = c->transport.private;
124         union smb_read *io;
125         struct smb_read_state *state;
126         struct smbcli_request *req;
127
128         state = talloc(smb, struct smb_read_state);
129         if (state == NULL) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         state->c = c;
134         if (blob == NULL) {
135                 state->received = 0;
136                 state->data = data_blob_talloc(state, NULL, 0x2000);
137         } else {
138                 uint32_t frag_length = blob->length>=16?
139                         dcerpc_get_frag_length(blob):0x2000;
140                 state->received = blob->length;
141                 state->data = data_blob_talloc(state, NULL, frag_length);
142                 if (!state->data.data) {
143                         talloc_free(state);
144                         return NT_STATUS_NO_MEMORY;
145                 }
146                 memcpy(state->data.data, blob->data, blob->length);
147         }
148
149         state->io = talloc(state, union smb_read);
150
151         io = state->io;
152         io->generic.level = RAW_READ_READX;
153         io->readx.in.fnum = smb->fnum;
154         io->readx.in.mincnt = state->data.length - state->received;
155         io->readx.in.maxcnt = io->readx.in.mincnt;
156         io->readx.in.offset = 0;
157         io->readx.in.remaining = 0;
158         io->readx.out.data = state->data.data + state->received;
159         req = smb_raw_read_send(smb->tree, io);
160         if (req == NULL) {
161                 return NT_STATUS_NO_MEMORY;
162         }
163
164         req->async.fn = smb_read_callback;
165         req->async.private = state;
166
167         state->req = req;
168
169         return NT_STATUS_OK;
170 }
171
172
173 /*
174   trigger a read request from the server
175 */
176 static NTSTATUS send_read_request(struct dcerpc_connection *c)
177 {
178         return send_read_request_continue(c, NULL);
179 }
180
181 /* 
182    this holds the state of an in-flight trans call
183 */
184 struct smb_trans_state {
185         struct dcerpc_connection *c;
186         struct smbcli_request *req;
187         struct smb_trans2 *trans;
188 };
189
190 /*
191   called when a trans request has completed
192 */
193 static void smb_trans_callback(struct smbcli_request *req)
194 {
195         struct smb_trans_state *state = req->async.private;
196         struct dcerpc_connection *c = state->c;
197         NTSTATUS status;
198
199         status = smb_raw_trans_recv(req, state, state->trans);
200
201         if (NT_STATUS_IS_ERR(status)) {
202                 pipe_dead(c, status);
203                 return;
204         }
205
206         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
207                 c->transport.recv_data(c, &state->trans->out.data, NT_STATUS_OK);
208                 talloc_free(state);
209                 return;
210         }
211
212         /* there is more to receive - setup a readx */
213         send_read_request_continue(c, &state->trans->out.data);
214         talloc_free(state);
215 }
216
217 /*
218   send a SMBtrans style request
219 */
220 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
221 {
222         struct smb_private *smb = c->transport.private;
223         struct smb_trans2 *trans;
224         uint16_t setup[2];
225         struct smb_trans_state *state;
226
227         state = talloc(smb, struct smb_trans_state);
228         if (state == NULL) {
229                 return NT_STATUS_NO_MEMORY;
230         }
231
232         state->c = c;
233         state->trans = talloc(state, struct smb_trans2);
234         trans = state->trans;
235
236         trans->in.data = *blob;
237         trans->in.params = data_blob(NULL, 0);
238         
239         setup[0] = TRANSACT_DCERPCCMD;
240         setup[1] = smb->fnum;
241
242         trans->in.max_param = 0;
243         trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
244         trans->in.max_setup = 0;
245         trans->in.setup_count = 2;
246         trans->in.flags = 0;
247         trans->in.timeout = 0;
248         trans->in.setup = setup;
249         trans->in.trans_name = "\\PIPE\\";
250
251         state->req = smb_raw_trans_send(smb->tree, trans);
252         if (state->req == NULL) {
253                 talloc_free(state);
254                 return NT_STATUS_NO_MEMORY;
255         }
256
257         state->req->async.fn = smb_trans_callback;
258         state->req->async.private = state;
259
260         return NT_STATUS_OK;
261 }
262
263 /*
264   called when a write request has completed
265 */
266 static void smb_write_callback(struct smbcli_request *req)
267 {
268         struct dcerpc_connection *c = req->async.private;
269
270         if (!NT_STATUS_IS_OK(req->status)) {
271                 DEBUG(0,("dcerpc_smb: write callback error\n"));
272                 pipe_dead(c, req->status);
273         }
274
275         smbcli_request_destroy(req);
276 }
277
278 /* 
279    send a packet to the server
280 */
281 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
282 {
283         struct smb_private *smb = c->transport.private;
284         union smb_write io;
285         struct smbcli_request *req;
286
287         if (trigger_read) {
288                 return smb_send_trans_request(c, blob);
289         }
290
291         io.generic.level = RAW_WRITE_WRITEX;
292         io.writex.in.fnum = smb->fnum;
293         io.writex.in.offset = 0;
294         io.writex.in.wmode = PIPE_START_MESSAGE;
295         io.writex.in.remaining = blob->length;
296         io.writex.in.count = blob->length;
297         io.writex.in.data = blob->data;
298
299         /* we must not timeout at the smb level for rpc requests, as otherwise
300            signing/sealing can be messed up */
301         smb->tree->session->transport->options.request_timeout = 0;
302
303         req = smb_raw_write_send(smb->tree, &io);
304         if (req == NULL) {
305                 return NT_STATUS_NO_MEMORY;
306         }
307
308         req->async.fn = smb_write_callback;
309         req->async.private = c;
310
311         if (trigger_read) {
312                 send_read_request(c);
313         }
314
315         return NT_STATUS_OK;
316 }
317
318 /* 
319    shutdown SMB pipe connection
320 */
321 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
322 {
323         struct smb_private *smb = c->transport.private;
324         union smb_close io;
325
326         /* maybe we're still starting up */
327         if (!smb) return NT_STATUS_OK;
328
329         io.close.level = RAW_CLOSE_CLOSE;
330         io.close.in.fnum = smb->fnum;
331         io.close.in.write_time = 0;
332         smb_raw_close(smb->tree, &io);
333
334         talloc_free(smb);
335
336         return NT_STATUS_OK;
337 }
338
339 /*
340   return SMB server name
341 */
342 static const char *smb_peer_name(struct dcerpc_connection *c)
343 {
344         struct smb_private *smb = c->transport.private;
345         return smb->server_name;
346 }
347
348 /*
349   fetch the user session key 
350 */
351 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
352 {
353         struct smb_private *smb = c->transport.private;
354
355         if (smb->tree->session->user_session_key.data) {
356                 *session_key = smb->tree->session->user_session_key;
357                 return NT_STATUS_OK;
358         }
359         return NT_STATUS_NO_USER_SESSION_KEY;
360 }
361
362 struct pipe_open_smb_state {
363         union smb_open *open;
364         struct dcerpc_connection *c;
365         struct smbcli_request *req;
366         struct smbcli_tree *tree;
367         struct composite_context *ctx;
368 };
369
370 static void pipe_open_recv(struct smbcli_request *req);
371
372 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c, 
373                                                     struct smbcli_tree *tree,
374                                                     const char *pipe_name)
375 {
376         struct composite_context *ctx;
377         struct pipe_open_smb_state *state;
378
379         ctx = talloc_zero(NULL, struct composite_context);
380         if (ctx == NULL) goto failed;
381         ctx->state = COMPOSITE_STATE_IN_PROGRESS;
382         ctx->event_ctx = talloc_reference(c, c->event_ctx);
383
384         state = talloc(ctx, struct pipe_open_smb_state);
385         if (state == NULL) goto failed;
386
387         state->c = c;
388         state->tree = tree;
389         state->ctx = ctx;
390
391         state->open = talloc(state, union smb_open);
392         if (state->open == NULL) goto failed;
393
394         state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
395         state->open->ntcreatex.in.flags = 0;
396         state->open->ntcreatex.in.root_fid = 0;
397         state->open->ntcreatex.in.access_mask = 
398                 SEC_STD_READ_CONTROL |
399                 SEC_FILE_WRITE_ATTRIBUTE |
400                 SEC_FILE_WRITE_EA |
401                 SEC_FILE_READ_DATA |
402                 SEC_FILE_WRITE_DATA;
403         state->open->ntcreatex.in.file_attr = 0;
404         state->open->ntcreatex.in.alloc_size = 0;
405         state->open->ntcreatex.in.share_access = 
406                 NTCREATEX_SHARE_ACCESS_READ |
407                 NTCREATEX_SHARE_ACCESS_WRITE;
408         state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
409         state->open->ntcreatex.in.create_options = 0;
410         state->open->ntcreatex.in.impersonation =
411                 NTCREATEX_IMPERSONATION_IMPERSONATION;
412         state->open->ntcreatex.in.security_flags = 0;
413
414         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
415             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
416                 pipe_name += 6;
417         }
418         state->open->ntcreatex.in.fname =
419                 (pipe_name[0] == '\\') ?
420                 talloc_strdup(state->open, pipe_name) :
421                 talloc_asprintf(state->open, "\\%s", pipe_name);
422         if (state->open->ntcreatex.in.fname == NULL) goto failed;
423
424         state->req = smb_raw_open_send(tree, state->open);
425         if (state->req == NULL) goto failed;
426
427         state->req->async.fn = pipe_open_recv;
428         state->req->async.private = state;
429
430         return ctx;
431
432  failed:
433         talloc_free(ctx);
434         return NULL;
435 }
436
437 static void pipe_open_recv(struct smbcli_request *req)
438 {
439         struct pipe_open_smb_state *state =
440                 talloc_get_type(req->async.private,
441                                 struct pipe_open_smb_state);
442         struct composite_context *ctx = state->ctx;
443         struct dcerpc_connection *c = state->c;
444         struct smb_private *smb;
445         
446         ctx->status = smb_raw_open_recv(req, state, state->open);
447         if (!NT_STATUS_IS_OK(ctx->status)) goto done;
448
449         /*
450           fill in the transport methods
451         */
452         c->transport.transport = NCACN_NP;
453         c->transport.private = NULL;
454         c->transport.shutdown_pipe = smb_shutdown_pipe;
455         c->transport.peer_name = smb_peer_name;
456
457         c->transport.send_request = smb_send_request;
458         c->transport.send_read = send_read_request;
459         c->transport.recv_data = NULL;
460         
461         /* Over-ride the default session key with the SMB session key */
462         c->security_state.session_key = smb_session_key;
463
464         smb = talloc(c, struct smb_private);
465         if (smb == NULL) {
466                 ctx->status = NT_STATUS_NO_MEMORY;
467                 goto done;
468         }
469
470         smb->fnum       = state->open->ntcreatex.out.fnum;
471         smb->tree       = talloc_reference(smb, state->tree);
472         smb->server_name= strupper_talloc(
473                 smb, state->tree->session->transport->socket->hostname);
474         if (smb->server_name == NULL) {
475                 ctx->status = NT_STATUS_NO_MEMORY;
476                 goto done;
477         }
478         c->transport.private = smb;
479
480         ctx->status = NT_STATUS_OK;
481         ctx->state = COMPOSITE_STATE_DONE;
482
483  done:
484         if (!NT_STATUS_IS_OK(ctx->status)) {
485                 ctx->state = COMPOSITE_STATE_ERROR;
486         }
487         if ((ctx->state >= COMPOSITE_STATE_DONE) &&
488             (ctx->async.fn != NULL)) {
489                 ctx->async.fn(ctx);
490         }
491 }
492
493 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
494 {
495         NTSTATUS status = composite_wait(c);
496         talloc_free(c);
497         return status;
498 }
499
500 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
501                               struct smbcli_tree *tree,
502                               const char *pipe_name)
503 {
504         struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, tree,
505                                                                   pipe_name);
506         return dcerpc_pipe_open_smb_recv(ctx);
507 }
508
509 /*
510   return the SMB tree used for a dcerpc over SMB pipe
511 */
512 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
513 {
514         struct smb_private *smb = c->transport.private;
515
516         if (c->transport.transport != NCACN_NP) {
517                 return NULL;
518         }
519
520         return smb->tree;
521 }