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