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