r14173: change smb interface structures to always use
[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.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.out.data = state->data.data + state->received;
161         req = smb_raw_read_send(smb->tree, io);
162         if (req == NULL) {
163                 return NT_STATUS_NO_MEMORY;
164         }
165
166         req->async.fn = smb_read_callback;
167         req->async.private = state;
168
169         state->req = req;
170
171         return NT_STATUS_OK;
172 }
173
174
175 /*
176   trigger a read request from the server
177 */
178 static NTSTATUS send_read_request(struct dcerpc_connection *c)
179 {
180         return send_read_request_continue(c, NULL);
181 }
182
183 /* 
184    this holds the state of an in-flight trans call
185 */
186 struct smb_trans_state {
187         struct dcerpc_connection *c;
188         struct smbcli_request *req;
189         struct smb_trans2 *trans;
190 };
191
192 /*
193   called when a trans request has completed
194 */
195 static void smb_trans_callback(struct smbcli_request *req)
196 {
197         struct smb_trans_state *state = req->async.private;
198         struct dcerpc_connection *c = state->c;
199         NTSTATUS status;
200
201         status = smb_raw_trans_recv(req, state, state->trans);
202
203         if (NT_STATUS_IS_ERR(status)) {
204                 pipe_dead(c, status);
205                 return;
206         }
207
208         if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
209                 DATA_BLOB data = state->trans->out.data;
210                 talloc_steal(c, data.data);
211                 talloc_free(state);
212                 c->transport.recv_data(c, &data, NT_STATUS_OK);
213                 return;
214         }
215
216         /* there is more to receive - setup a readx */
217         send_read_request_continue(c, &state->trans->out.data);
218         talloc_free(state);
219 }
220
221 /*
222   send a SMBtrans style request
223 */
224 static NTSTATUS smb_send_trans_request(struct dcerpc_connection *c, DATA_BLOB *blob)
225 {
226         struct smb_private *smb = c->transport.private;
227         struct smb_trans2 *trans;
228         uint16_t setup[2];
229         struct smb_trans_state *state;
230
231         state = talloc(smb, struct smb_trans_state);
232         if (state == NULL) {
233                 return NT_STATUS_NO_MEMORY;
234         }
235
236         state->c = c;
237         state->trans = talloc(state, struct smb_trans2);
238         trans = state->trans;
239
240         trans->in.data = *blob;
241         trans->in.params = data_blob(NULL, 0);
242         
243         setup[0] = TRANSACT_DCERPCCMD;
244         setup[1] = smb->fnum;
245
246         trans->in.max_param = 0;
247         trans->in.max_data = smb_raw_max_trans_data(smb->tree, 0);
248         trans->in.max_setup = 0;
249         trans->in.setup_count = 2;
250         trans->in.flags = 0;
251         trans->in.timeout = 0;
252         trans->in.setup = setup;
253         trans->in.trans_name = "\\PIPE\\";
254
255         state->req = smb_raw_trans_send(smb->tree, trans);
256         if (state->req == NULL) {
257                 talloc_free(state);
258                 return NT_STATUS_NO_MEMORY;
259         }
260
261         state->req->async.fn = smb_trans_callback;
262         state->req->async.private = state;
263
264         talloc_steal(state, state->req);
265
266         return NT_STATUS_OK;
267 }
268
269 /*
270   called when a write request has completed
271 */
272 static void smb_write_callback(struct smbcli_request *req)
273 {
274         struct dcerpc_connection *c = req->async.private;
275
276         if (!NT_STATUS_IS_OK(req->status)) {
277                 DEBUG(0,("dcerpc_smb: write callback error\n"));
278                 pipe_dead(c, req->status);
279         }
280
281         smbcli_request_destroy(req);
282 }
283
284 /* 
285    send a packet to the server
286 */
287 static NTSTATUS smb_send_request(struct dcerpc_connection *c, DATA_BLOB *blob, BOOL trigger_read)
288 {
289         struct smb_private *smb = c->transport.private;
290         union smb_write io;
291         struct smbcli_request *req;
292
293         if (trigger_read) {
294                 return smb_send_trans_request(c, blob);
295         }
296
297         io.generic.level = RAW_WRITE_WRITEX;
298         io.writex.file.fnum = smb->fnum;
299         io.writex.in.offset = 0;
300         io.writex.in.wmode = PIPE_START_MESSAGE;
301         io.writex.in.remaining = blob->length;
302         io.writex.in.count = blob->length;
303         io.writex.in.data = blob->data;
304
305         /* we must not timeout at the smb level for rpc requests, as otherwise
306            signing/sealing can be messed up */
307         smb->tree->session->transport->options.request_timeout = 0;
308
309         req = smb_raw_write_send(smb->tree, &io);
310         if (req == NULL) {
311                 return NT_STATUS_NO_MEMORY;
312         }
313
314         req->async.fn = smb_write_callback;
315         req->async.private = c;
316
317         if (trigger_read) {
318                 send_read_request(c);
319         }
320
321         return NT_STATUS_OK;
322 }
323
324 /* 
325    shutdown SMB pipe connection
326 */
327 static NTSTATUS smb_shutdown_pipe(struct dcerpc_connection *c)
328 {
329         struct smb_private *smb = c->transport.private;
330         union smb_close io;
331         struct smbcli_request *req;
332
333         /* maybe we're still starting up */
334         if (!smb) return NT_STATUS_OK;
335
336         io.close.level = RAW_CLOSE_CLOSE;
337         io.close.file.fnum = smb->fnum;
338         io.close.in.write_time = 0;
339         req = smb_raw_close_send(smb->tree, &io);
340         if (req != NULL) {
341                 /* we don't care if this fails, so just free it if it succeeds */
342                 req->async.fn = (void (*)(struct smbcli_request *))talloc_free;
343         }
344
345         talloc_free(smb);
346
347         return NT_STATUS_OK;
348 }
349
350 /*
351   return SMB server name
352 */
353 static const char *smb_peer_name(struct dcerpc_connection *c)
354 {
355         struct smb_private *smb = c->transport.private;
356         return smb->server_name;
357 }
358
359 /*
360   fetch the user session key 
361 */
362 static NTSTATUS smb_session_key(struct dcerpc_connection *c, DATA_BLOB *session_key)
363 {
364         struct smb_private *smb = c->transport.private;
365
366         if (smb->tree->session->user_session_key.data) {
367                 *session_key = smb->tree->session->user_session_key;
368                 return NT_STATUS_OK;
369         }
370         return NT_STATUS_NO_USER_SESSION_KEY;
371 }
372
373 struct pipe_open_smb_state {
374         union smb_open *open;
375         struct dcerpc_connection *c;
376         struct smbcli_request *req;
377         struct smbcli_tree *tree;
378         struct composite_context *ctx;
379 };
380
381 static void pipe_open_recv(struct smbcli_request *req);
382
383 struct composite_context *dcerpc_pipe_open_smb_send(struct dcerpc_connection *c, 
384                                                     struct smbcli_tree *tree,
385                                                     const char *pipe_name)
386 {
387         struct composite_context *ctx;
388         struct pipe_open_smb_state *state;
389
390         ctx = talloc_zero(NULL, struct composite_context);
391         if (ctx == NULL) goto failed;
392         ctx->state = COMPOSITE_STATE_IN_PROGRESS;
393         ctx->event_ctx = talloc_reference(c, c->event_ctx);
394
395         state = talloc(ctx, struct pipe_open_smb_state);
396         if (state == NULL) goto failed;
397
398         state->c = c;
399         state->tree = tree;
400         state->ctx = ctx;
401
402         state->open = talloc(state, union smb_open);
403         if (state->open == NULL) goto failed;
404
405         state->open->ntcreatex.level = RAW_OPEN_NTCREATEX;
406         state->open->ntcreatex.in.flags = 0;
407         state->open->ntcreatex.in.root_fid = 0;
408         state->open->ntcreatex.in.access_mask = 
409                 SEC_STD_READ_CONTROL |
410                 SEC_FILE_WRITE_ATTRIBUTE |
411                 SEC_FILE_WRITE_EA |
412                 SEC_FILE_READ_DATA |
413                 SEC_FILE_WRITE_DATA;
414         state->open->ntcreatex.in.file_attr = 0;
415         state->open->ntcreatex.in.alloc_size = 0;
416         state->open->ntcreatex.in.share_access = 
417                 NTCREATEX_SHARE_ACCESS_READ |
418                 NTCREATEX_SHARE_ACCESS_WRITE;
419         state->open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
420         state->open->ntcreatex.in.create_options = 0;
421         state->open->ntcreatex.in.impersonation =
422                 NTCREATEX_IMPERSONATION_IMPERSONATION;
423         state->open->ntcreatex.in.security_flags = 0;
424
425         if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) || 
426             (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
427                 pipe_name += 6;
428         }
429         state->open->ntcreatex.in.fname =
430                 (pipe_name[0] == '\\') ?
431                 talloc_strdup(state->open, pipe_name) :
432                 talloc_asprintf(state->open, "\\%s", pipe_name);
433         if (state->open->ntcreatex.in.fname == NULL) goto failed;
434
435         state->req = smb_raw_open_send(tree, state->open);
436         if (state->req == NULL) goto failed;
437
438         state->req->async.fn = pipe_open_recv;
439         state->req->async.private = state;
440
441         return ctx;
442
443  failed:
444         talloc_free(ctx);
445         return NULL;
446 }
447
448 static void pipe_open_recv(struct smbcli_request *req)
449 {
450         struct pipe_open_smb_state *state =
451                 talloc_get_type(req->async.private,
452                                 struct pipe_open_smb_state);
453         struct composite_context *ctx = state->ctx;
454         struct dcerpc_connection *c = state->c;
455         struct smb_private *smb;
456         
457         ctx->status = smb_raw_open_recv(req, state, state->open);
458         if (!NT_STATUS_IS_OK(ctx->status)) goto done;
459
460         /*
461           fill in the transport methods
462         */
463         c->transport.transport = NCACN_NP;
464         c->transport.private = NULL;
465         c->transport.shutdown_pipe = smb_shutdown_pipe;
466         c->transport.peer_name = smb_peer_name;
467
468         c->transport.send_request = smb_send_request;
469         c->transport.send_read = send_read_request;
470         c->transport.recv_data = NULL;
471         
472         /* Over-ride the default session key with the SMB session key */
473         c->security_state.session_key = smb_session_key;
474
475         smb = talloc(c, struct smb_private);
476         if (smb == NULL) {
477                 ctx->status = NT_STATUS_NO_MEMORY;
478                 goto done;
479         }
480
481         smb->fnum       = state->open->ntcreatex.file.fnum;
482         smb->tree       = talloc_reference(smb, state->tree);
483         smb->server_name= strupper_talloc(
484                 smb, state->tree->session->transport->called.name);
485         if (smb->server_name == NULL) {
486                 ctx->status = NT_STATUS_NO_MEMORY;
487                 goto done;
488         }
489         c->transport.private = smb;
490
491         ctx->status = NT_STATUS_OK;
492         ctx->state = COMPOSITE_STATE_DONE;
493
494  done:
495         if (!NT_STATUS_IS_OK(ctx->status)) {
496                 ctx->state = COMPOSITE_STATE_ERROR;
497         }
498         if ((ctx->state >= COMPOSITE_STATE_DONE) &&
499             (ctx->async.fn != NULL)) {
500                 ctx->async.fn(ctx);
501         }
502 }
503
504 NTSTATUS dcerpc_pipe_open_smb_recv(struct composite_context *c)
505 {
506         NTSTATUS status = composite_wait(c);
507         talloc_free(c);
508         return status;
509 }
510
511 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_connection *c,
512                               struct smbcli_tree *tree,
513                               const char *pipe_name)
514 {
515         struct composite_context *ctx = dcerpc_pipe_open_smb_send(c, tree,
516                                                                   pipe_name);
517         return dcerpc_pipe_open_smb_recv(ctx);
518 }
519
520 /*
521   return the SMB tree used for a dcerpc over SMB pipe
522 */
523 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c)
524 {
525         struct smb_private *smb = c->transport.private;
526
527         if (c->transport.transport != NCACN_NP) {
528                 return NULL;
529         }
530
531         return smb->tree;
532 }