r14173: change smb interface structures to always use
[jelmer/samba4-debian.git] / source / libcli / raw / rawfile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2002
6    Copyright (C) James Myers 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "smb.h"
25 #include "libcli/raw/libcliraw.h"
26
27 #define SETUP_REQUEST(cmd, wct, buflen) do { \
28         req = smbcli_request_setup(tree, cmd, wct, buflen); \
29         if (!req) return NULL; \
30 } while (0)
31
32 /****************************************************************************
33  Rename a file - async interface
34 ****************************************************************************/
35 struct smbcli_request *smb_raw_rename_send(struct smbcli_tree *tree,
36                                         union smb_rename *parms)
37 {
38         struct smbcli_request *req = NULL; 
39
40         switch (parms->generic.level) {
41         case RAW_RENAME_RENAME:
42                 SETUP_REQUEST(SMBmv, 1, 0);
43                 SSVAL(req->out.vwv, VWV(0), parms->rename.in.attrib);
44                 smbcli_req_append_ascii4(req, parms->rename.in.pattern1, STR_TERMINATE);
45                 smbcli_req_append_ascii4(req, parms->rename.in.pattern2, STR_TERMINATE);
46                 break;
47
48         case RAW_RENAME_NTRENAME:
49                 SETUP_REQUEST(SMBntrename, 4, 0);
50                 SSVAL(req->out.vwv, VWV(0), parms->ntrename.in.attrib);
51                 SSVAL(req->out.vwv, VWV(1), parms->ntrename.in.flags);
52                 SIVAL(req->out.vwv, VWV(2), parms->ntrename.in.cluster_size);
53                 smbcli_req_append_ascii4(req, parms->ntrename.in.old_name, STR_TERMINATE);
54                 smbcli_req_append_ascii4(req, parms->ntrename.in.new_name, STR_TERMINATE);
55                 break;
56         }
57
58         if (!smbcli_request_send(req)) {
59                 smbcli_request_destroy(req);
60                 return NULL;
61         }
62
63         return req;
64 }
65
66 /****************************************************************************
67  Rename a file - sync interface
68 ****************************************************************************/
69 NTSTATUS smb_raw_rename(struct smbcli_tree *tree,
70                         union smb_rename *parms)
71 {
72         struct smbcli_request *req = smb_raw_rename_send(tree, parms);
73         return smbcli_request_simple_recv(req);
74 }
75
76
77 /****************************************************************************
78  Delete a file - async interface
79 ****************************************************************************/
80 struct smbcli_request *smb_raw_unlink_send(struct smbcli_tree *tree,
81                                            union smb_unlink *parms)
82 {
83         struct smbcli_request *req; 
84
85         SETUP_REQUEST(SMBunlink, 1, 0);
86
87         SSVAL(req->out.vwv, VWV(0), parms->unlink.in.attrib);
88         smbcli_req_append_ascii4(req, parms->unlink.in.pattern, STR_TERMINATE);
89
90         if (!smbcli_request_send(req)) {
91                 smbcli_request_destroy(req);
92                 return NULL;
93         }
94         return req;
95 }
96
97 /*
98   delete a file - sync interface
99 */
100 NTSTATUS smb_raw_unlink(struct smbcli_tree *tree,
101                         union smb_unlink *parms)
102 {
103         struct smbcli_request *req = smb_raw_unlink_send(tree, parms);
104         return smbcli_request_simple_recv(req);
105 }
106
107
108 /****************************************************************************
109  create a directory  using TRANSACT2_MKDIR - async interface
110 ****************************************************************************/
111 static struct smbcli_request *smb_raw_t2mkdir_send(struct smbcli_tree *tree, 
112                                                 union smb_mkdir *parms)
113 {
114         struct smb_trans2 t2;
115         uint16_t setup = TRANSACT2_MKDIR;
116         TALLOC_CTX *mem_ctx;
117         struct smbcli_request *req;
118         uint16_t data_total;
119
120         mem_ctx = talloc_init("t2mkdir");
121
122         data_total = ea_list_size(parms->t2mkdir.in.num_eas, parms->t2mkdir.in.eas);
123
124         t2.in.max_param = 2;
125         t2.in.max_data = 0;
126         t2.in.max_setup = 0;
127         t2.in.flags = 0;
128         t2.in.timeout = 0;
129         t2.in.setup_count = 1;
130         t2.in.setup = &setup;
131         t2.in.params = data_blob_talloc(mem_ctx, NULL, 4);
132         t2.in.data = data_blob_talloc(mem_ctx, NULL, data_total);
133
134         SIVAL(t2.in.params.data, VWV(0), 0); /* reserved */
135
136         smbcli_blob_append_string(tree->session, mem_ctx, 
137                                   &t2.in.params, parms->t2mkdir.in.path, STR_TERMINATE);
138
139         ea_put_list(t2.in.data.data, parms->t2mkdir.in.num_eas, parms->t2mkdir.in.eas);
140
141         req = smb_raw_trans2_send(tree, &t2);
142
143         talloc_free(mem_ctx);
144
145         return req;
146 }
147
148 /****************************************************************************
149  Create a directory - async interface
150 ****************************************************************************/
151 struct smbcli_request *smb_raw_mkdir_send(struct smbcli_tree *tree,
152                                        union smb_mkdir *parms)
153 {
154         struct smbcli_request *req; 
155
156         if (parms->generic.level == RAW_MKDIR_T2MKDIR) {
157                 return smb_raw_t2mkdir_send(tree, parms);
158         }
159
160         if (parms->generic.level != RAW_MKDIR_MKDIR) {
161                 return NULL;
162         }
163
164         SETUP_REQUEST(SMBmkdir, 0, 0);
165         
166         smbcli_req_append_ascii4(req, parms->mkdir.in.path, STR_TERMINATE);
167
168         if (!smbcli_request_send(req)) {
169                 return NULL;
170         }
171
172         return req;
173 }
174
175 /****************************************************************************
176  Create a directory - sync interface
177 ****************************************************************************/
178 NTSTATUS smb_raw_mkdir(struct smbcli_tree *tree,
179                        union smb_mkdir *parms)
180 {
181         struct smbcli_request *req = smb_raw_mkdir_send(tree, parms);
182         return smbcli_request_simple_recv(req);
183 }
184
185 /****************************************************************************
186  Remove a directory - async interface
187 ****************************************************************************/
188 struct smbcli_request *smb_raw_rmdir_send(struct smbcli_tree *tree,
189                                        struct smb_rmdir *parms)
190 {
191         struct smbcli_request *req; 
192
193         SETUP_REQUEST(SMBrmdir, 0, 0);
194         
195         smbcli_req_append_ascii4(req, parms->in.path, STR_TERMINATE);
196
197         if (!smbcli_request_send(req)) {
198                 smbcli_request_destroy(req);
199                 return NULL;
200         }
201
202         return req;
203 }
204
205 /****************************************************************************
206  Remove a directory - sync interface
207 ****************************************************************************/
208 NTSTATUS smb_raw_rmdir(struct smbcli_tree *tree,
209                        struct smb_rmdir *parms)
210 {
211         struct smbcli_request *req = smb_raw_rmdir_send(tree, parms);
212         return smbcli_request_simple_recv(req);
213 }
214
215
216 /*
217  Open a file using TRANSACT2_OPEN - async recv
218 */
219 static NTSTATUS smb_raw_nttrans_create_recv(struct smbcli_request *req, 
220                                             TALLOC_CTX *mem_ctx, 
221                                             union smb_open *parms)
222 {
223         NTSTATUS status;
224         struct smb_nttrans nt;
225         uint8_t *params;
226
227         status = smb_raw_nttrans_recv(req, mem_ctx, &nt);
228         if (!NT_STATUS_IS_OK(status)) return status;
229
230         if (nt.out.params.length < 69) {
231                 return NT_STATUS_INVALID_PARAMETER;
232         }
233
234         params = nt.out.params.data;
235
236         parms->ntcreatex.out.oplock_level =                 CVAL(params, 0);
237         parms->ntcreatex.file.fnum =                        SVAL(params, 2);
238         parms->ntcreatex.out.create_action =                IVAL(params, 4);
239         parms->ntcreatex.out.create_time =   smbcli_pull_nttime(params, 12);
240         parms->ntcreatex.out.access_time =   smbcli_pull_nttime(params, 20);
241         parms->ntcreatex.out.write_time =    smbcli_pull_nttime(params, 28);
242         parms->ntcreatex.out.change_time =   smbcli_pull_nttime(params, 36);
243         parms->ntcreatex.out.attrib =                      IVAL(params, 44);
244         parms->ntcreatex.out.alloc_size =                  BVAL(params, 48);
245         parms->ntcreatex.out.size =                        BVAL(params, 56);
246         parms->ntcreatex.out.file_type =                   SVAL(params, 64);
247         parms->ntcreatex.out.ipc_state =                   SVAL(params, 66);
248         parms->ntcreatex.out.is_directory =                CVAL(params, 68);
249         
250         return NT_STATUS_OK;
251 }
252
253
254 /*
255  Open a file using NTTRANS CREATE - async send 
256 */
257 static struct smbcli_request *smb_raw_nttrans_create_send(struct smbcli_tree *tree, 
258                                                           union smb_open *parms)
259 {
260         struct smb_nttrans nt;
261         uint8_t *params;
262         TALLOC_CTX *mem_ctx = talloc_new(tree);
263         uint16_t fname_len;
264         DATA_BLOB sd_blob, ea_blob;
265         struct smbcli_request *req;
266         NTSTATUS status;
267
268         nt.in.max_setup = 0;
269         nt.in.max_param = 101;
270         nt.in.max_data  = 0;
271         nt.in.setup_count = 0;
272         nt.in.function = NT_TRANSACT_CREATE;
273         nt.in.setup = NULL;
274
275         sd_blob = data_blob(NULL, 0);
276         ea_blob = data_blob(NULL, 0);
277
278         if (parms->ntcreatex.in.sec_desc) {
279                 status = ndr_push_struct_blob(&sd_blob, mem_ctx, 
280                                               parms->ntcreatex.in.sec_desc, 
281                                               (ndr_push_flags_fn_t)ndr_push_security_descriptor);
282                 if (!NT_STATUS_IS_OK(status)) {
283                         talloc_free(mem_ctx);
284                         return NULL;
285                 }
286         }
287
288         if (parms->ntcreatex.in.ea_list) {
289                 uint32_t ea_size = ea_list_size_chained(parms->ntcreatex.in.ea_list->num_eas,
290                                                         parms->ntcreatex.in.ea_list->eas);
291                 ea_blob = data_blob_talloc(mem_ctx, NULL, ea_size);
292                 if (ea_blob.data == NULL) {
293                         return NULL;
294                 }
295                 ea_put_list_chained(ea_blob.data, 
296                                     parms->ntcreatex.in.ea_list->num_eas,
297                                     parms->ntcreatex.in.ea_list->eas);
298         }
299
300         nt.in.params = data_blob_talloc(mem_ctx, NULL, 53);
301         if (nt.in.params.data == NULL) {
302                 talloc_free(mem_ctx);
303                 return NULL;
304         }
305
306         /* build the parameter section */
307         params = nt.in.params.data;
308
309         SIVAL(params,  0, parms->ntcreatex.in.flags);
310         SIVAL(params,  4, parms->ntcreatex.in.root_fid);
311         SIVAL(params,  8, parms->ntcreatex.in.access_mask);
312         SBVAL(params, 12, parms->ntcreatex.in.alloc_size);
313         SIVAL(params, 20, parms->ntcreatex.in.file_attr);
314         SIVAL(params, 24, parms->ntcreatex.in.share_access);
315         SIVAL(params, 28, parms->ntcreatex.in.open_disposition);
316         SIVAL(params, 32, parms->ntcreatex.in.create_options);
317         SIVAL(params, 36, sd_blob.length);
318         SIVAL(params, 40, ea_blob.length);
319         SIVAL(params, 48, parms->ntcreatex.in.impersonation);
320         SCVAL(params, 52, parms->ntcreatex.in.security_flags);
321
322         /* the empty string first forces the correct alignment */
323         smbcli_blob_append_string(tree->session, mem_ctx, &nt.in.params,"", 0);
324         fname_len = smbcli_blob_append_string(tree->session, mem_ctx, &nt.in.params,
325                                               parms->ntcreatex.in.fname, STR_TERMINATE);
326
327         SIVAL(nt.in.params.data, 44, fname_len);
328
329         /* build the data section */
330         nt.in.data = data_blob_talloc(mem_ctx, NULL, sd_blob.length + ea_blob.length);
331         memcpy(nt.in.data.data, sd_blob.data, sd_blob.length);
332         memcpy(nt.in.data.data+sd_blob.length, ea_blob.data, ea_blob.length);
333
334         /* send the request on its way */
335         req = smb_raw_nttrans_send(tree, &nt);
336
337         talloc_free(mem_ctx);
338         
339         return req;
340 }
341
342
343 /****************************************************************************
344  Open a file using TRANSACT2_OPEN - async send 
345 ****************************************************************************/
346 static struct smbcli_request *smb_raw_t2open_send(struct smbcli_tree *tree, 
347                                                union smb_open *parms)
348 {
349         struct smb_trans2 t2;
350         uint16_t setup = TRANSACT2_OPEN;
351         TALLOC_CTX *mem_ctx = talloc_init("smb_raw_t2open");
352         struct smbcli_request *req;
353         uint16_t list_size;
354
355         list_size = ea_list_size(parms->t2open.in.num_eas, parms->t2open.in.eas);
356
357         t2.in.max_param = 30;
358         t2.in.max_data = 0;
359         t2.in.max_setup = 0;
360         t2.in.flags = 0;
361         t2.in.timeout = 0;
362         t2.in.setup_count = 1;
363         t2.in.setup = &setup;
364         t2.in.params = data_blob_talloc(mem_ctx, NULL, 28);
365         t2.in.data = data_blob_talloc(mem_ctx, NULL, list_size);
366
367         SSVAL(t2.in.params.data, VWV(0), parms->t2open.in.flags);
368         SSVAL(t2.in.params.data, VWV(1), parms->t2open.in.open_mode);
369         SSVAL(t2.in.params.data, VWV(2), parms->t2open.in.search_attrs);
370         SSVAL(t2.in.params.data, VWV(3), parms->t2open.in.file_attrs);
371         raw_push_dos_date(tree->session->transport, 
372                           t2.in.params.data, VWV(4), parms->t2open.in.write_time);
373         SSVAL(t2.in.params.data, VWV(6), parms->t2open.in.open_func);
374         SIVAL(t2.in.params.data, VWV(7), parms->t2open.in.size);
375         SIVAL(t2.in.params.data, VWV(9), parms->t2open.in.timeout);
376         SIVAL(t2.in.params.data, VWV(11), 0);
377         SSVAL(t2.in.params.data, VWV(13), 0);
378
379         smbcli_blob_append_string(tree->session, mem_ctx, 
380                                   &t2.in.params, parms->t2open.in.fname, 
381                                   STR_TERMINATE);
382
383         ea_put_list(t2.in.data.data, parms->t2open.in.num_eas, parms->t2open.in.eas);
384
385         req = smb_raw_trans2_send(tree, &t2);
386
387         talloc_free(mem_ctx);
388
389         return req;
390 }
391
392
393 /****************************************************************************
394  Open a file using TRANSACT2_OPEN - async recv
395 ****************************************************************************/
396 static NTSTATUS smb_raw_t2open_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_open *parms)
397 {
398         struct smbcli_transport *transport = req?req->transport:NULL;
399         struct smb_trans2 t2;
400         NTSTATUS status;
401
402         status = smb_raw_trans2_recv(req, mem_ctx, &t2);
403         if (!NT_STATUS_IS_OK(status)) return status;
404
405         if (t2.out.params.length < 30) {
406                 return NT_STATUS_INFO_LENGTH_MISMATCH;
407         }
408
409         parms->t2open.file.fnum =       SVAL(t2.out.params.data, VWV(0));
410         parms->t2open.out.attrib =      SVAL(t2.out.params.data, VWV(1));
411         parms->t2open.out.write_time =  raw_pull_dos_date3(transport, t2.out.params.data + VWV(2));
412         parms->t2open.out.size =        IVAL(t2.out.params.data, VWV(4));
413         parms->t2open.out.access =      SVAL(t2.out.params.data, VWV(6));
414         parms->t2open.out.ftype =       SVAL(t2.out.params.data, VWV(7));
415         parms->t2open.out.devstate =    SVAL(t2.out.params.data, VWV(8));
416         parms->t2open.out.action =      SVAL(t2.out.params.data, VWV(9));
417         parms->t2open.out.file_id =     SVAL(t2.out.params.data, VWV(10));
418
419         return NT_STATUS_OK;
420 }
421
422 /****************************************************************************
423  Open a file - async send
424 ****************************************************************************/
425 struct smbcli_request *smb_raw_open_send(struct smbcli_tree *tree, union smb_open *parms)
426 {
427         int len;
428         struct smbcli_request *req = NULL; 
429         BOOL bigoffset = False;
430
431         switch (parms->generic.level) {
432         case RAW_OPEN_T2OPEN:
433                 return smb_raw_t2open_send(tree, parms);
434
435         case RAW_OPEN_OPEN:
436                 SETUP_REQUEST(SMBopen, 2, 0);
437                 SSVAL(req->out.vwv, VWV(0), parms->openold.in.open_mode);
438                 SSVAL(req->out.vwv, VWV(1), parms->openold.in.search_attrs);
439                 smbcli_req_append_ascii4(req, parms->openold.in.fname, STR_TERMINATE);
440                 break;
441                 
442         case RAW_OPEN_OPENX:
443                 SETUP_REQUEST(SMBopenX, 15, 0);
444                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
445                 SSVAL(req->out.vwv, VWV(1), 0);
446                 SSVAL(req->out.vwv, VWV(2), parms->openx.in.flags);
447                 SSVAL(req->out.vwv, VWV(3), parms->openx.in.open_mode);
448                 SSVAL(req->out.vwv, VWV(4), parms->openx.in.search_attrs);
449                 SSVAL(req->out.vwv, VWV(5), parms->openx.in.file_attrs);
450                 raw_push_dos_date3(tree->session->transport, 
451                                   req->out.vwv, VWV(6), parms->openx.in.write_time);
452                 SSVAL(req->out.vwv, VWV(8), parms->openx.in.open_func);
453                 SIVAL(req->out.vwv, VWV(9), parms->openx.in.size);
454                 SIVAL(req->out.vwv, VWV(11),parms->openx.in.timeout);
455                 SIVAL(req->out.vwv, VWV(13),0); /* reserved */
456                 smbcli_req_append_string(req, parms->openx.in.fname, STR_TERMINATE);
457                 break;
458                 
459         case RAW_OPEN_MKNEW:
460                 SETUP_REQUEST(SMBmknew, 3, 0);
461                 SSVAL(req->out.vwv, VWV(0), parms->mknew.in.attrib);
462                 raw_push_dos_date3(tree->session->transport, 
463                                   req->out.vwv, VWV(1), parms->mknew.in.write_time);
464                 smbcli_req_append_ascii4(req, parms->mknew.in.fname, STR_TERMINATE);
465                 break;
466
467         case RAW_OPEN_CREATE:
468                 SETUP_REQUEST(SMBcreate, 3, 0);
469                 SSVAL(req->out.vwv, VWV(0), parms->create.in.attrib);
470                 raw_push_dos_date3(tree->session->transport, 
471                                   req->out.vwv, VWV(1), parms->create.in.write_time);
472                 smbcli_req_append_ascii4(req, parms->create.in.fname, STR_TERMINATE);
473                 break;
474                 
475         case RAW_OPEN_CTEMP:
476                 SETUP_REQUEST(SMBctemp, 3, 0);
477                 SSVAL(req->out.vwv, VWV(0), parms->ctemp.in.attrib);
478                 raw_push_dos_date3(tree->session->transport, 
479                                   req->out.vwv, VWV(1), parms->ctemp.in.write_time);
480                 smbcli_req_append_ascii4(req, parms->ctemp.in.directory, STR_TERMINATE);
481                 break;
482                 
483         case RAW_OPEN_SPLOPEN:
484                 SETUP_REQUEST(SMBsplopen, 2, 0);
485                 SSVAL(req->out.vwv, VWV(0), parms->splopen.in.setup_length);
486                 SSVAL(req->out.vwv, VWV(1), parms->splopen.in.mode);
487                 break;
488                 
489         case RAW_OPEN_NTCREATEX:
490                 SETUP_REQUEST(SMBntcreateX, 24, 0);
491                 SSVAL(req->out.vwv, VWV(0),SMB_CHAIN_NONE);
492                 SSVAL(req->out.vwv, VWV(1),0);
493                 SCVAL(req->out.vwv, VWV(2),0); /* padding */
494                 SIVAL(req->out.vwv,  7, parms->ntcreatex.in.flags);
495                 SIVAL(req->out.vwv, 11, parms->ntcreatex.in.root_fid);
496                 SIVAL(req->out.vwv, 15, parms->ntcreatex.in.access_mask);
497                 SBVAL(req->out.vwv, 19, parms->ntcreatex.in.alloc_size);
498                 SIVAL(req->out.vwv, 27, parms->ntcreatex.in.file_attr);
499                 SIVAL(req->out.vwv, 31, parms->ntcreatex.in.share_access);
500                 SIVAL(req->out.vwv, 35, parms->ntcreatex.in.open_disposition);
501                 SIVAL(req->out.vwv, 39, parms->ntcreatex.in.create_options);
502                 SIVAL(req->out.vwv, 43, parms->ntcreatex.in.impersonation);
503                 SCVAL(req->out.vwv, 47, parms->ntcreatex.in.security_flags);
504                 
505                 smbcli_req_append_string_len(req, parms->ntcreatex.in.fname, STR_TERMINATE, &len);
506                 SSVAL(req->out.vwv, 5, len);
507                 break;
508
509         case RAW_OPEN_NTTRANS_CREATE:
510                 return smb_raw_nttrans_create_send(tree, parms);
511
512
513         case RAW_OPEN_OPENX_READX:
514                 SETUP_REQUEST(SMBopenX, 15, 0);
515                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
516                 SSVAL(req->out.vwv, VWV(1), 0);
517                 SSVAL(req->out.vwv, VWV(2), parms->openxreadx.in.flags);
518                 SSVAL(req->out.vwv, VWV(3), parms->openxreadx.in.open_mode);
519                 SSVAL(req->out.vwv, VWV(4), parms->openxreadx.in.search_attrs);
520                 SSVAL(req->out.vwv, VWV(5), parms->openxreadx.in.file_attrs);
521                 raw_push_dos_date3(tree->session->transport, 
522                                   req->out.vwv, VWV(6), parms->openxreadx.in.write_time);
523                 SSVAL(req->out.vwv, VWV(8), parms->openxreadx.in.open_func);
524                 SIVAL(req->out.vwv, VWV(9), parms->openxreadx.in.size);
525                 SIVAL(req->out.vwv, VWV(11),parms->openxreadx.in.timeout);
526                 SIVAL(req->out.vwv, VWV(13),0);
527                 smbcli_req_append_string(req, parms->openxreadx.in.fname, STR_TERMINATE);
528
529                 if (tree->session->transport->negotiate.capabilities & CAP_LARGE_FILES) {
530                         bigoffset = True;
531                 }
532
533                 smbcli_chained_request_setup(req, SMBreadX, bigoffset ? 12 : 10, 0);
534
535                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
536                 SSVAL(req->out.vwv, VWV(1), 0);
537                 SSVAL(req->out.vwv, VWV(2), 0);
538                 SIVAL(req->out.vwv, VWV(3), parms->openxreadx.in.offset);
539                 SSVAL(req->out.vwv, VWV(5), parms->openxreadx.in.maxcnt & 0xFFFF);
540                 SSVAL(req->out.vwv, VWV(6), parms->openxreadx.in.mincnt);
541                 SIVAL(req->out.vwv, VWV(7), parms->openxreadx.in.maxcnt >> 16);
542                 SSVAL(req->out.vwv, VWV(9), parms->openxreadx.in.remaining);
543                 if (bigoffset) {
544                         SIVAL(req->out.vwv, VWV(10),parms->openxreadx.in.offset>>32);
545                 }
546                 break;
547         }
548
549         if (!smbcli_request_send(req)) {
550                 smbcli_request_destroy(req);
551                 return NULL;
552         }
553
554         return req;
555 }
556
557 /****************************************************************************
558  Open a file - async recv
559 ****************************************************************************/
560 NTSTATUS smb_raw_open_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_open *parms)
561 {
562         NTSTATUS status;
563
564         if (!smbcli_request_receive(req) ||
565             smbcli_request_is_error(req)) {
566                 goto failed;
567         }
568
569         switch (parms->openold.level) {
570         case RAW_OPEN_T2OPEN:
571                 return smb_raw_t2open_recv(req, mem_ctx, parms);
572
573         case RAW_OPEN_OPEN:
574                 SMBCLI_CHECK_WCT(req, 7);
575                 parms->openold.file.fnum  = SVAL(req->in.vwv, VWV(0));
576                 parms->openold.out.attrib = SVAL(req->in.vwv, VWV(1));
577                 parms->openold.out.write_time = raw_pull_dos_date3(req->transport,
578                                                                 req->in.vwv + VWV(2));
579                 parms->openold.out.size = IVAL(req->in.vwv, VWV(4));
580                 parms->openold.out.rmode = SVAL(req->in.vwv, VWV(6));
581                 break;
582
583         case RAW_OPEN_OPENX:
584                 SMBCLI_CHECK_MIN_WCT(req, 15);
585                 parms->openx.file.fnum  = SVAL(req->in.vwv, VWV(2));
586                 parms->openx.out.attrib = SVAL(req->in.vwv, VWV(3));
587                 parms->openx.out.write_time = raw_pull_dos_date3(req->transport,
588                                                                  req->in.vwv + VWV(4));
589                 parms->openx.out.size = IVAL(req->in.vwv, VWV(6));
590                 parms->openx.out.access = SVAL(req->in.vwv, VWV(8));
591                 parms->openx.out.ftype = SVAL(req->in.vwv, VWV(9));
592                 parms->openx.out.devstate = SVAL(req->in.vwv, VWV(10));
593                 parms->openx.out.action = SVAL(req->in.vwv, VWV(11));
594                 parms->openx.out.unique_fid = IVAL(req->in.vwv, VWV(12));
595                 if (req->in.wct >= 19) {
596                         parms->openx.out.access_mask = IVAL(req->in.vwv, VWV(15));
597                         parms->openx.out.unknown =     IVAL(req->in.vwv, VWV(17));
598                 } else {
599                         parms->openx.out.access_mask = 0;
600                         parms->openx.out.unknown = 0;
601                 }
602                 break;
603
604         case RAW_OPEN_MKNEW:
605                 SMBCLI_CHECK_WCT(req, 1);
606                 parms->mknew.file.fnum = SVAL(req->in.vwv, VWV(0));
607                 break;
608
609         case RAW_OPEN_CREATE:
610                 SMBCLI_CHECK_WCT(req, 1);
611                 parms->create.file.fnum = SVAL(req->in.vwv, VWV(0));
612                 break;
613
614         case RAW_OPEN_CTEMP:
615                 SMBCLI_CHECK_WCT(req, 1);
616                 parms->ctemp.file.fnum = SVAL(req->in.vwv, VWV(0));
617                 smbcli_req_pull_string(req, mem_ctx, &parms->ctemp.out.name, req->in.data, -1, STR_TERMINATE | STR_ASCII);
618                 break;
619
620         case RAW_OPEN_SPLOPEN:
621                 SMBCLI_CHECK_WCT(req, 1);
622                 parms->splopen.file.fnum = SVAL(req->in.vwv, VWV(0));
623                 break;
624
625         case RAW_OPEN_NTCREATEX:
626                 SMBCLI_CHECK_MIN_WCT(req, 34);
627                 parms->ntcreatex.out.oplock_level =              CVAL(req->in.vwv, 4);
628                 parms->ntcreatex.file.fnum =                     SVAL(req->in.vwv, 5);
629                 parms->ntcreatex.out.create_action =             IVAL(req->in.vwv, 7);
630                 parms->ntcreatex.out.create_time =   smbcli_pull_nttime(req->in.vwv, 11);
631                 parms->ntcreatex.out.access_time =   smbcli_pull_nttime(req->in.vwv, 19);
632                 parms->ntcreatex.out.write_time =    smbcli_pull_nttime(req->in.vwv, 27);
633                 parms->ntcreatex.out.change_time =   smbcli_pull_nttime(req->in.vwv, 35);
634                 parms->ntcreatex.out.attrib =                   IVAL(req->in.vwv, 43);
635                 parms->ntcreatex.out.alloc_size =               BVAL(req->in.vwv, 47);
636                 parms->ntcreatex.out.size =                     BVAL(req->in.vwv, 55);
637                 parms->ntcreatex.out.file_type =                SVAL(req->in.vwv, 63);
638                 parms->ntcreatex.out.ipc_state =                SVAL(req->in.vwv, 65);
639                 parms->ntcreatex.out.is_directory =             CVAL(req->in.vwv, 67);
640                 break;
641
642         case RAW_OPEN_NTTRANS_CREATE:
643                 return smb_raw_nttrans_create_recv(req, mem_ctx, parms);
644
645         case RAW_OPEN_OPENX_READX:
646                 SMBCLI_CHECK_MIN_WCT(req, 15);
647                 parms->openxreadx.file.fnum = SVAL(req->in.vwv, VWV(2));
648                 parms->openxreadx.out.attrib = SVAL(req->in.vwv, VWV(3));
649                 parms->openxreadx.out.write_time = raw_pull_dos_date3(req->transport,
650                                                                  req->in.vwv + VWV(4));
651                 parms->openxreadx.out.size = IVAL(req->in.vwv, VWV(6));
652                 parms->openxreadx.out.access = SVAL(req->in.vwv, VWV(8));
653                 parms->openxreadx.out.ftype = SVAL(req->in.vwv, VWV(9));
654                 parms->openxreadx.out.devstate = SVAL(req->in.vwv, VWV(10));
655                 parms->openxreadx.out.action = SVAL(req->in.vwv, VWV(11));
656                 parms->openxreadx.out.unique_fid = IVAL(req->in.vwv, VWV(12));
657                 if (req->in.wct >= 19) {
658                         parms->openxreadx.out.access_mask = IVAL(req->in.vwv, VWV(15));
659                         parms->openxreadx.out.unknown =     IVAL(req->in.vwv, VWV(17));
660                 } else {
661                         parms->openxreadx.out.access_mask = 0;
662                         parms->openxreadx.out.unknown = 0;
663                 }
664
665                 status = smbcli_chained_advance(req);
666                 if (!NT_STATUS_IS_OK(status)) {
667                         return status;
668                 }
669
670                 SMBCLI_CHECK_WCT(req, 12);
671                 parms->openxreadx.out.remaining       = SVAL(req->in.vwv, VWV(2));
672                 parms->openxreadx.out.compaction_mode = SVAL(req->in.vwv, VWV(3));
673                 parms->openxreadx.out.nread = SVAL(req->in.vwv, VWV(5));
674                 if (parms->openxreadx.out.nread > 
675                     MAX(parms->openxreadx.in.mincnt, parms->openxreadx.in.maxcnt) ||
676                     !smbcli_raw_pull_data(req, req->in.hdr + SVAL(req->in.vwv, VWV(6)), 
677                                           parms->openxreadx.out.nread, 
678                                           parms->openxreadx.out.data)) {
679                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
680                 }
681                 break;
682         }
683
684 failed:
685         return smbcli_request_destroy(req);
686 }
687
688
689 /****************************************************************************
690  Open a file - sync interface
691 ****************************************************************************/
692 NTSTATUS smb_raw_open(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_open *parms)
693 {
694         struct smbcli_request *req = smb_raw_open_send(tree, parms);
695         return smb_raw_open_recv(req, mem_ctx, parms);
696 }
697
698
699 /****************************************************************************
700  Close a file - async send
701 ****************************************************************************/
702 struct smbcli_request *smb_raw_close_send(struct smbcli_tree *tree, union smb_close *parms)
703 {
704         struct smbcli_request *req = NULL; 
705
706         switch (parms->generic.level) {
707         case RAW_CLOSE_CLOSE:
708                 SETUP_REQUEST(SMBclose, 3, 0);
709                 SSVAL(req->out.vwv, VWV(0), parms->close.file.fnum);
710                 raw_push_dos_date3(tree->session->transport, 
711                                   req->out.vwv, VWV(1), parms->close.in.write_time);
712                 break;
713
714         case RAW_CLOSE_SPLCLOSE:
715                 SETUP_REQUEST(SMBsplclose, 3, 0);
716                 SSVAL(req->out.vwv, VWV(0), parms->splclose.file.fnum);
717                 SIVAL(req->out.vwv, VWV(1), 0); /* reserved */
718                 break;
719         }
720
721         if (!req) return NULL;
722
723         if (!smbcli_request_send(req)) {
724                 smbcli_request_destroy(req);
725                 return NULL;
726         }
727
728         return req;
729 }
730
731
732 /****************************************************************************
733  Close a file - sync interface
734 ****************************************************************************/
735 NTSTATUS smb_raw_close(struct smbcli_tree *tree, union smb_close *parms)
736 {
737         struct smbcli_request *req = smb_raw_close_send(tree, parms);
738         return smbcli_request_simple_recv(req);
739 }
740
741
742 /****************************************************************************
743  Locking calls - async interface
744 ****************************************************************************/
745 struct smbcli_request *smb_raw_lock_send(struct smbcli_tree *tree, union smb_lock *parms)
746 {
747         struct smbcli_request *req = NULL; 
748
749         switch (parms->generic.level) {
750         case RAW_LOCK_LOCK:
751                 SETUP_REQUEST(SMBlock, 5, 0);
752                 SSVAL(req->out.vwv, VWV(0), parms->lock.file.fnum);
753                 SIVAL(req->out.vwv, VWV(1), parms->lock.in.count);
754                 SIVAL(req->out.vwv, VWV(3), parms->lock.in.offset);
755                 break;
756                 
757         case RAW_LOCK_UNLOCK:
758                 SETUP_REQUEST(SMBunlock, 5, 0);
759                 SSVAL(req->out.vwv, VWV(0), parms->unlock.file.fnum);
760                 SIVAL(req->out.vwv, VWV(1), parms->unlock.in.count);
761                 SIVAL(req->out.vwv, VWV(3), parms->unlock.in.offset);
762                 break;
763                 
764         case RAW_LOCK_LOCKX: {
765                 struct smb_lock_entry *lockp;
766                 uint_t lck_size = (parms->lockx.in.mode & LOCKING_ANDX_LARGE_FILES)? 20 : 10;
767                 uint_t lock_count = parms->lockx.in.ulock_cnt + parms->lockx.in.lock_cnt;
768                 int i;
769
770                 SETUP_REQUEST(SMBlockingX, 8, lck_size * lock_count);
771                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
772                 SSVAL(req->out.vwv, VWV(1), 0);
773                 SSVAL(req->out.vwv, VWV(2), parms->lockx.file.fnum);
774                 SSVAL(req->out.vwv, VWV(3), parms->lockx.in.mode);
775                 SIVAL(req->out.vwv, VWV(4), parms->lockx.in.timeout);
776                 SSVAL(req->out.vwv, VWV(6), parms->lockx.in.ulock_cnt);
777                 SSVAL(req->out.vwv, VWV(7), parms->lockx.in.lock_cnt);
778                 
779                 /* copy in all the locks */
780                 lockp = &parms->lockx.in.locks[0];
781                 for (i = 0; i < lock_count; i++) {
782                         uint8_t *p = req->out.data + lck_size * i;
783                         SSVAL(p, 0, lockp[i].pid);
784                         if (parms->lockx.in.mode & LOCKING_ANDX_LARGE_FILES) {
785                                 SSVAL(p,  2, 0); /* reserved */
786                                 SIVAL(p,  4, lockp[i].offset>>32);
787                                 SIVAL(p,  8, lockp[i].offset);
788                                 SIVAL(p, 12, lockp[i].count>>32);
789                                 SIVAL(p, 16, lockp[i].count);
790                         } else {
791                                 SIVAL(p, 2, lockp[i].offset);
792                                 SIVAL(p, 6, lockp[i].count);
793                         }
794                 }       
795         }
796         }
797
798         if (!smbcli_request_send(req)) {
799                 smbcli_request_destroy(req);
800                 return NULL;
801         }
802
803         return req;
804 }
805
806 /****************************************************************************
807  Locking calls - sync interface
808 ****************************************************************************/
809 NTSTATUS smb_raw_lock(struct smbcli_tree *tree, union smb_lock *parms)
810 {
811         struct smbcli_request *req = smb_raw_lock_send(tree, parms);
812         return smbcli_request_simple_recv(req);
813 }
814         
815
816 /****************************************************************************
817  Check for existence of a dir - async send
818 ****************************************************************************/
819 struct smbcli_request *smb_raw_chkpath_send(struct smbcli_tree *tree, union smb_chkpath *parms)
820 {
821         struct smbcli_request *req; 
822
823         SETUP_REQUEST(SMBchkpth, 0, 0);
824
825         smbcli_req_append_ascii4(req, parms->chkpath.in.path, STR_TERMINATE);
826
827         if (!smbcli_request_send(req)) {
828                 smbcli_request_destroy(req);
829                 return NULL;
830         }
831
832         return req;
833 }
834
835 /****************************************************************************
836  Check for existence of a dir - sync interface
837 ****************************************************************************/
838 NTSTATUS smb_raw_chkpath(struct smbcli_tree *tree, union smb_chkpath *parms)
839 {
840         struct smbcli_request *req = smb_raw_chkpath_send(tree, parms);
841         return smbcli_request_simple_recv(req);
842 }
843
844 /****************************************************************************
845  flush a file - async send
846  a flush to fnum 0xFFFF will flush all files
847 ****************************************************************************/
848 struct smbcli_request *smb_raw_flush_send(struct smbcli_tree *tree, union smb_flush *parms)
849 {
850         struct smbcli_request *req; 
851
852         SETUP_REQUEST(SMBflush, 1, 0);
853         SSVAL(req->out.vwv, VWV(0), parms->flush.file.fnum);
854
855         if (!smbcli_request_send(req)) {
856                 smbcli_request_destroy(req);
857                 return NULL;
858         }
859
860         return req;
861 }
862
863
864 /****************************************************************************
865  flush a file - sync interface
866 ****************************************************************************/
867 NTSTATUS smb_raw_flush(struct smbcli_tree *tree, union smb_flush *parms)
868 {
869         struct smbcli_request *req = smb_raw_flush_send(tree, parms);
870         return smbcli_request_simple_recv(req);
871 }
872
873
874 /****************************************************************************
875  seek a file - async send
876 ****************************************************************************/
877 struct smbcli_request *smb_raw_seek_send(struct smbcli_tree *tree,
878                                          union smb_seek *parms)
879 {
880         struct smbcli_request *req; 
881
882         SETUP_REQUEST(SMBlseek, 4, 0);
883
884         SSVAL(req->out.vwv, VWV(0), parms->lseek.file.fnum);
885         SSVAL(req->out.vwv, VWV(1), parms->lseek.in.mode);
886         SIVALS(req->out.vwv, VWV(2), parms->lseek.in.offset);
887
888         if (!smbcli_request_send(req)) {
889                 smbcli_request_destroy(req);
890                 return NULL;
891         }
892         return req;
893 }
894
895 /****************************************************************************
896  seek a file - async receive
897 ****************************************************************************/
898 NTSTATUS smb_raw_seek_recv(struct smbcli_request *req,
899                            union smb_seek *parms)
900 {
901         if (!smbcli_request_receive(req) ||
902             smbcli_request_is_error(req)) {
903                 return smbcli_request_destroy(req);
904         }
905
906         SMBCLI_CHECK_WCT(req, 2);       
907         parms->lseek.out.offset = IVAL(req->in.vwv, VWV(0));
908
909 failed: 
910         return smbcli_request_destroy(req);
911 }
912
913 /*
914   seek a file - sync interface
915 */
916 NTSTATUS smb_raw_seek(struct smbcli_tree *tree,
917                       union smb_seek *parms)
918 {
919         struct smbcli_request *req = smb_raw_seek_send(tree, parms);
920         return smb_raw_seek_recv(req, parms);
921 }