c34cb9c52f3b673180845ae3ba73c994a0a20616
[samba.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smb.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_security.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.out.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
267         nt.in.max_setup = 0;
268         nt.in.max_param = 101;
269         nt.in.max_data  = 0;
270         nt.in.setup_count = 0;
271         nt.in.function = NT_TRANSACT_CREATE;
272         nt.in.setup = NULL;
273
274         sd_blob = data_blob(NULL, 0);
275         ea_blob = data_blob(NULL, 0);
276
277         if (parms->ntcreatex.in.sec_desc) {
278                 enum ndr_err_code ndr_err;
279                 ndr_err = 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 (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
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->transport;
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.out.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         case RAW_OPEN_SMB2:
548                 return NULL;
549         }
550
551         if (!smbcli_request_send(req)) {
552                 smbcli_request_destroy(req);
553                 return NULL;
554         }
555
556         return req;
557 }
558
559 /****************************************************************************
560  Open a file - async recv
561 ****************************************************************************/
562 NTSTATUS smb_raw_open_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, union smb_open *parms)
563 {
564         NTSTATUS status;
565
566         if (!smbcli_request_receive(req) ||
567             smbcli_request_is_error(req)) {
568                 goto failed;
569         }
570
571         switch (parms->openold.level) {
572         case RAW_OPEN_T2OPEN:
573                 return smb_raw_t2open_recv(req, mem_ctx, parms);
574
575         case RAW_OPEN_OPEN:
576                 SMBCLI_CHECK_WCT(req, 7);
577                 parms->openold.out.file.fnum = SVAL(req->in.vwv, VWV(0));
578                 parms->openold.out.attrib = SVAL(req->in.vwv, VWV(1));
579                 parms->openold.out.write_time = raw_pull_dos_date3(req->transport,
580                                                                 req->in.vwv + VWV(2));
581                 parms->openold.out.size = IVAL(req->in.vwv, VWV(4));
582                 parms->openold.out.rmode = SVAL(req->in.vwv, VWV(6));
583                 break;
584
585         case RAW_OPEN_OPENX:
586                 SMBCLI_CHECK_MIN_WCT(req, 15);
587                 parms->openx.out.file.fnum = SVAL(req->in.vwv, VWV(2));
588                 parms->openx.out.attrib = SVAL(req->in.vwv, VWV(3));
589                 parms->openx.out.write_time = raw_pull_dos_date3(req->transport,
590                                                                  req->in.vwv + VWV(4));
591                 parms->openx.out.size = IVAL(req->in.vwv, VWV(6));
592                 parms->openx.out.access = SVAL(req->in.vwv, VWV(8));
593                 parms->openx.out.ftype = SVAL(req->in.vwv, VWV(9));
594                 parms->openx.out.devstate = SVAL(req->in.vwv, VWV(10));
595                 parms->openx.out.action = SVAL(req->in.vwv, VWV(11));
596                 parms->openx.out.unique_fid = IVAL(req->in.vwv, VWV(12));
597                 if (req->in.wct >= 19) {
598                         parms->openx.out.access_mask = IVAL(req->in.vwv, VWV(15));
599                         parms->openx.out.unknown =     IVAL(req->in.vwv, VWV(17));
600                 } else {
601                         parms->openx.out.access_mask = 0;
602                         parms->openx.out.unknown = 0;
603                 }
604                 break;
605
606         case RAW_OPEN_MKNEW:
607                 SMBCLI_CHECK_WCT(req, 1);
608                 parms->mknew.out.file.fnum = SVAL(req->in.vwv, VWV(0));
609                 break;
610
611         case RAW_OPEN_CREATE:
612                 SMBCLI_CHECK_WCT(req, 1);
613                 parms->create.out.file.fnum = SVAL(req->in.vwv, VWV(0));
614                 break;
615
616         case RAW_OPEN_CTEMP:
617                 SMBCLI_CHECK_WCT(req, 1);
618                 parms->ctemp.out.file.fnum = SVAL(req->in.vwv, VWV(0));
619                 smbcli_req_pull_string(req, mem_ctx, &parms->ctemp.out.name, req->in.data, -1, STR_TERMINATE | STR_ASCII);
620                 break;
621
622         case RAW_OPEN_SPLOPEN:
623                 SMBCLI_CHECK_WCT(req, 1);
624                 parms->splopen.out.file.fnum = SVAL(req->in.vwv, VWV(0));
625                 break;
626
627         case RAW_OPEN_NTCREATEX:
628                 SMBCLI_CHECK_MIN_WCT(req, 34);
629                 parms->ntcreatex.out.oplock_level =              CVAL(req->in.vwv, 4);
630                 parms->ntcreatex.out.file.fnum =                 SVAL(req->in.vwv, 5);
631                 parms->ntcreatex.out.create_action =             IVAL(req->in.vwv, 7);
632                 parms->ntcreatex.out.create_time =   smbcli_pull_nttime(req->in.vwv, 11);
633                 parms->ntcreatex.out.access_time =   smbcli_pull_nttime(req->in.vwv, 19);
634                 parms->ntcreatex.out.write_time =    smbcli_pull_nttime(req->in.vwv, 27);
635                 parms->ntcreatex.out.change_time =   smbcli_pull_nttime(req->in.vwv, 35);
636                 parms->ntcreatex.out.attrib =                   IVAL(req->in.vwv, 43);
637                 parms->ntcreatex.out.alloc_size =               BVAL(req->in.vwv, 47);
638                 parms->ntcreatex.out.size =                     BVAL(req->in.vwv, 55);
639                 parms->ntcreatex.out.file_type =                SVAL(req->in.vwv, 63);
640                 parms->ntcreatex.out.ipc_state =                SVAL(req->in.vwv, 65);
641                 parms->ntcreatex.out.is_directory =             CVAL(req->in.vwv, 67);
642                 break;
643
644         case RAW_OPEN_NTTRANS_CREATE:
645                 return smb_raw_nttrans_create_recv(req, mem_ctx, parms);
646
647         case RAW_OPEN_OPENX_READX:
648                 SMBCLI_CHECK_MIN_WCT(req, 15);
649                 parms->openxreadx.out.file.fnum = SVAL(req->in.vwv, VWV(2));
650                 parms->openxreadx.out.attrib = SVAL(req->in.vwv, VWV(3));
651                 parms->openxreadx.out.write_time = raw_pull_dos_date3(req->transport,
652                                                                  req->in.vwv + VWV(4));
653                 parms->openxreadx.out.size = IVAL(req->in.vwv, VWV(6));
654                 parms->openxreadx.out.access = SVAL(req->in.vwv, VWV(8));
655                 parms->openxreadx.out.ftype = SVAL(req->in.vwv, VWV(9));
656                 parms->openxreadx.out.devstate = SVAL(req->in.vwv, VWV(10));
657                 parms->openxreadx.out.action = SVAL(req->in.vwv, VWV(11));
658                 parms->openxreadx.out.unique_fid = IVAL(req->in.vwv, VWV(12));
659                 if (req->in.wct >= 19) {
660                         parms->openxreadx.out.access_mask = IVAL(req->in.vwv, VWV(15));
661                         parms->openxreadx.out.unknown =     IVAL(req->in.vwv, VWV(17));
662                 } else {
663                         parms->openxreadx.out.access_mask = 0;
664                         parms->openxreadx.out.unknown = 0;
665                 }
666
667                 status = smbcli_chained_advance(req);
668                 if (!NT_STATUS_IS_OK(status)) {
669                         return status;
670                 }
671
672                 SMBCLI_CHECK_WCT(req, 12);
673                 parms->openxreadx.out.remaining       = SVAL(req->in.vwv, VWV(2));
674                 parms->openxreadx.out.compaction_mode = SVAL(req->in.vwv, VWV(3));
675                 parms->openxreadx.out.nread = SVAL(req->in.vwv, VWV(5));
676                 if (parms->openxreadx.out.nread > 
677                     MAX(parms->openxreadx.in.mincnt, parms->openxreadx.in.maxcnt) ||
678                     !smbcli_raw_pull_data(req, req->in.hdr + SVAL(req->in.vwv, VWV(6)), 
679                                           parms->openxreadx.out.nread, 
680                                           parms->openxreadx.out.data)) {
681                         req->status = NT_STATUS_BUFFER_TOO_SMALL;
682                 }
683                 break;
684         case RAW_OPEN_SMB2:
685                 req->status = NT_STATUS_INTERNAL_ERROR;
686                 break;
687         }
688
689 failed:
690         return smbcli_request_destroy(req);
691 }
692
693
694 /****************************************************************************
695  Open a file - sync interface
696 ****************************************************************************/
697 NTSTATUS smb_raw_open(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, union smb_open *parms)
698 {
699         struct smbcli_request *req = smb_raw_open_send(tree, parms);
700         return smb_raw_open_recv(req, mem_ctx, parms);
701 }
702
703
704 /****************************************************************************
705  Close a file - async send
706 ****************************************************************************/
707 struct smbcli_request *smb_raw_close_send(struct smbcli_tree *tree, union smb_close *parms)
708 {
709         struct smbcli_request *req = NULL; 
710
711         switch (parms->generic.level) {
712         case RAW_CLOSE_CLOSE:
713                 SETUP_REQUEST(SMBclose, 3, 0);
714                 SSVAL(req->out.vwv, VWV(0), parms->close.in.file.fnum);
715                 raw_push_dos_date3(tree->session->transport, 
716                                   req->out.vwv, VWV(1), parms->close.in.write_time);
717                 break;
718
719         case RAW_CLOSE_SPLCLOSE:
720                 SETUP_REQUEST(SMBsplclose, 3, 0);
721                 SSVAL(req->out.vwv, VWV(0), parms->splclose.in.file.fnum);
722                 SIVAL(req->out.vwv, VWV(1), 0); /* reserved */
723                 break;
724
725         case RAW_CLOSE_SMB2:
726                 return NULL;
727         }
728
729         if (!req) return NULL;
730
731         if (!smbcli_request_send(req)) {
732                 smbcli_request_destroy(req);
733                 return NULL;
734         }
735
736         return req;
737 }
738
739
740 /****************************************************************************
741  Close a file - sync interface
742 ****************************************************************************/
743 NTSTATUS smb_raw_close(struct smbcli_tree *tree, union smb_close *parms)
744 {
745         struct smbcli_request *req = smb_raw_close_send(tree, parms);
746         return smbcli_request_simple_recv(req);
747 }
748
749
750 /****************************************************************************
751  Locking calls - async interface
752 ****************************************************************************/
753 struct smbcli_request *smb_raw_lock_send(struct smbcli_tree *tree, union smb_lock *parms)
754 {
755         struct smbcli_request *req = NULL; 
756
757         switch (parms->generic.level) {
758         case RAW_LOCK_LOCK:
759                 SETUP_REQUEST(SMBlock, 5, 0);
760                 SSVAL(req->out.vwv, VWV(0), parms->lock.in.file.fnum);
761                 SIVAL(req->out.vwv, VWV(1), parms->lock.in.count);
762                 SIVAL(req->out.vwv, VWV(3), parms->lock.in.offset);
763                 break;
764                 
765         case RAW_LOCK_UNLOCK:
766                 SETUP_REQUEST(SMBunlock, 5, 0);
767                 SSVAL(req->out.vwv, VWV(0), parms->unlock.in.file.fnum);
768                 SIVAL(req->out.vwv, VWV(1), parms->unlock.in.count);
769                 SIVAL(req->out.vwv, VWV(3), parms->unlock.in.offset);
770                 break;
771                 
772         case RAW_LOCK_LOCKX: {
773                 struct smb_lock_entry *lockp;
774                 uint_t lck_size = (parms->lockx.in.mode & LOCKING_ANDX_LARGE_FILES)? 20 : 10;
775                 uint_t lock_count = parms->lockx.in.ulock_cnt + parms->lockx.in.lock_cnt;
776                 int i;
777
778                 SETUP_REQUEST(SMBlockingX, 8, lck_size * lock_count);
779                 SSVAL(req->out.vwv, VWV(0), SMB_CHAIN_NONE);
780                 SSVAL(req->out.vwv, VWV(1), 0);
781                 SSVAL(req->out.vwv, VWV(2), parms->lockx.in.file.fnum);
782                 SSVAL(req->out.vwv, VWV(3), parms->lockx.in.mode);
783                 SIVAL(req->out.vwv, VWV(4), parms->lockx.in.timeout);
784                 SSVAL(req->out.vwv, VWV(6), parms->lockx.in.ulock_cnt);
785                 SSVAL(req->out.vwv, VWV(7), parms->lockx.in.lock_cnt);
786                 
787                 /* copy in all the locks */
788                 lockp = &parms->lockx.in.locks[0];
789                 for (i = 0; i < lock_count; i++) {
790                         uint8_t *p = req->out.data + lck_size * i;
791                         SSVAL(p, 0, lockp[i].pid);
792                         if (parms->lockx.in.mode & LOCKING_ANDX_LARGE_FILES) {
793                                 SSVAL(p,  2, 0); /* reserved */
794                                 SIVAL(p,  4, lockp[i].offset>>32);
795                                 SIVAL(p,  8, lockp[i].offset);
796                                 SIVAL(p, 12, lockp[i].count>>32);
797                                 SIVAL(p, 16, lockp[i].count);
798                         } else {
799                                 SIVAL(p, 2, lockp[i].offset);
800                                 SIVAL(p, 6, lockp[i].count);
801                         }
802                 }       
803                 break;
804         }
805         case RAW_LOCK_SMB2:
806                 return NULL;
807         }
808
809         if (!smbcli_request_send(req)) {
810                 smbcli_request_destroy(req);
811                 return NULL;
812         }
813
814         return req;
815 }
816
817 /****************************************************************************
818  Locking calls - sync interface
819 ****************************************************************************/
820 NTSTATUS smb_raw_lock(struct smbcli_tree *tree, union smb_lock *parms)
821 {
822         struct smbcli_request *req = smb_raw_lock_send(tree, parms);
823         return smbcli_request_simple_recv(req);
824 }
825         
826
827 /****************************************************************************
828  Check for existence of a dir - async send
829 ****************************************************************************/
830 struct smbcli_request *smb_raw_chkpath_send(struct smbcli_tree *tree, union smb_chkpath *parms)
831 {
832         struct smbcli_request *req; 
833
834         SETUP_REQUEST(SMBchkpth, 0, 0);
835
836         smbcli_req_append_ascii4(req, parms->chkpath.in.path, STR_TERMINATE);
837
838         if (!smbcli_request_send(req)) {
839                 smbcli_request_destroy(req);
840                 return NULL;
841         }
842
843         return req;
844 }
845
846 /****************************************************************************
847  Check for existence of a dir - sync interface
848 ****************************************************************************/
849 NTSTATUS smb_raw_chkpath(struct smbcli_tree *tree, union smb_chkpath *parms)
850 {
851         struct smbcli_request *req = smb_raw_chkpath_send(tree, parms);
852         return smbcli_request_simple_recv(req);
853 }
854
855 /****************************************************************************
856  flush a file - async send
857  a flush with RAW_FLUSH_ALL will flush all files
858 ****************************************************************************/
859 struct smbcli_request *smb_raw_flush_send(struct smbcli_tree *tree, union smb_flush *parms)
860 {
861         struct smbcli_request *req; 
862         uint16_t fnum=0;
863
864         switch (parms->generic.level) {
865         case RAW_FLUSH_FLUSH:
866                 fnum = parms->flush.in.file.fnum;
867                 break;
868         case RAW_FLUSH_ALL:
869                 fnum = 0xFFFF;
870                 break;
871         case RAW_FLUSH_SMB2:
872                 return NULL;
873         }
874
875         SETUP_REQUEST(SMBflush, 1, 0);
876         SSVAL(req->out.vwv, VWV(0), fnum);
877
878         if (!smbcli_request_send(req)) {
879                 smbcli_request_destroy(req);
880                 return NULL;
881         }
882
883         return req;
884 }
885
886
887 /****************************************************************************
888  flush a file - sync interface
889 ****************************************************************************/
890 NTSTATUS smb_raw_flush(struct smbcli_tree *tree, union smb_flush *parms)
891 {
892         struct smbcli_request *req = smb_raw_flush_send(tree, parms);
893         return smbcli_request_simple_recv(req);
894 }
895
896
897 /****************************************************************************
898  seek a file - async send
899 ****************************************************************************/
900 struct smbcli_request *smb_raw_seek_send(struct smbcli_tree *tree,
901                                          union smb_seek *parms)
902 {
903         struct smbcli_request *req; 
904
905         SETUP_REQUEST(SMBlseek, 4, 0);
906
907         SSVAL(req->out.vwv, VWV(0), parms->lseek.in.file.fnum);
908         SSVAL(req->out.vwv, VWV(1), parms->lseek.in.mode);
909         SIVALS(req->out.vwv, VWV(2), parms->lseek.in.offset);
910
911         if (!smbcli_request_send(req)) {
912                 smbcli_request_destroy(req);
913                 return NULL;
914         }
915         return req;
916 }
917
918 /****************************************************************************
919  seek a file - async receive
920 ****************************************************************************/
921 NTSTATUS smb_raw_seek_recv(struct smbcli_request *req,
922                            union smb_seek *parms)
923 {
924         if (!smbcli_request_receive(req) ||
925             smbcli_request_is_error(req)) {
926                 return smbcli_request_destroy(req);
927         }
928
929         SMBCLI_CHECK_WCT(req, 2);       
930         parms->lseek.out.offset = IVAL(req->in.vwv, VWV(0));
931
932 failed: 
933         return smbcli_request_destroy(req);
934 }
935
936 /*
937   seek a file - sync interface
938 */
939 NTSTATUS smb_raw_seek(struct smbcli_tree *tree,
940                       union smb_seek *parms)
941 {
942         struct smbcli_request *req = smb_raw_seek_send(tree, parms);
943         return smb_raw_seek_recv(req, parms);
944 }