Introduce "struct stat_ex" as a replacement for SMB_STRUCT_STAT
[abartlet/samba.git/.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2009
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Hard/Symlink a file (UNIX extensions).
25  Creates new name (sym)linked to oldname.
26 ****************************************************************************/
27
28 static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
29 {
30         unsigned int data_len = 0;
31         unsigned int param_len = 0;
32         uint16_t setup = TRANSACT2_SETPATHINFO;
33         char *param;
34         char *data;
35         char *rparam=NULL, *rdata=NULL;
36         char *p;
37         size_t oldlen = 2*(strlen(oldname)+1);
38         size_t newlen = 2*(strlen(newname)+1);
39
40         param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
41
42         if (!param) {
43                 return false;
44         }
45
46         data = SMB_MALLOC_ARRAY(char, oldlen+2);
47
48         if (!data) {
49                 SAFE_FREE(param);
50                 return false;
51         }
52
53         SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
54         SIVAL(param,2,0);
55         p = &param[6];
56
57         p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
58         param_len = PTR_DIFF(p, param);
59
60         p = data;
61         p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
62         data_len = PTR_DIFF(p, data);
63
64         if (!cli_send_trans(cli, SMBtrans2,
65                         NULL,                        /* name */
66                         -1, 0,                          /* fid, flags */
67                         &setup, 1, 0,                   /* setup, length, max */
68                         param, param_len, 2,            /* param, length, max */
69                         data,  data_len, cli->max_xmit /* data, length, max */
70                         )) {
71                 SAFE_FREE(data);
72                 SAFE_FREE(param);
73                 return false;
74         }
75
76         SAFE_FREE(data);
77         SAFE_FREE(param);
78
79         if (!cli_receive_trans(cli, SMBtrans2,
80                         &rparam, &param_len,
81                         &rdata, &data_len)) {
82                         return false;
83         }
84
85         SAFE_FREE(data);
86         SAFE_FREE(param);
87         SAFE_FREE(rdata);
88         SAFE_FREE(rparam);
89
90         return true;
91 }
92
93 /****************************************************************************
94  Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
96
97 uint32_t unix_perms_to_wire(mode_t perms)
98 {
99         unsigned int ret = 0;
100
101         ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
102         ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
103         ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
104         ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
105         ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
106         ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
107         ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
108         ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
109         ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
110 #ifdef S_ISVTX
111         ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
112 #endif
113 #ifdef S_ISGID
114         ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
115 #endif
116 #ifdef S_ISUID
117         ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
118 #endif
119         return ret;
120 }
121
122 /****************************************************************************
123  Map wire permissions to standard UNIX.
124 ****************************************************************************/
125
126 mode_t wire_perms_to_unix(uint32_t perms)
127 {
128         mode_t ret = (mode_t)0;
129
130         ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
131         ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
132         ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
133         ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
134         ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
135         ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
136         ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
137         ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
138         ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
139 #ifdef S_ISVTX
140         ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
141 #endif
142 #ifdef S_ISGID
143         ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
144 #endif
145 #ifdef S_ISUID
146         ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
147 #endif
148         return ret;
149 }
150
151 /****************************************************************************
152  Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
154
155 static mode_t unix_filetype_from_wire(uint32_t wire_type)
156 {
157         switch (wire_type) {
158                 case UNIX_TYPE_FILE:
159                         return S_IFREG;
160                 case UNIX_TYPE_DIR:
161                         return S_IFDIR;
162 #ifdef S_IFLNK
163                 case UNIX_TYPE_SYMLINK:
164                         return S_IFLNK;
165 #endif
166 #ifdef S_IFCHR
167                 case UNIX_TYPE_CHARDEV:
168                         return S_IFCHR;
169 #endif
170 #ifdef S_IFBLK
171                 case UNIX_TYPE_BLKDEV:
172                         return S_IFBLK;
173 #endif
174 #ifdef S_IFIFO
175                 case UNIX_TYPE_FIFO:
176                         return S_IFIFO;
177 #endif
178 #ifdef S_IFSOCK
179                 case UNIX_TYPE_SOCKET:
180                         return S_IFSOCK;
181 #endif
182                 default:
183                         return (mode_t)0;
184         }
185 }
186
187 /****************************************************************************
188  Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
190
191 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
192 {
193         unsigned int param_len = 0;
194         unsigned int data_len = 0;
195         uint16_t setup = TRANSACT2_QPATHINFO;
196         char *param;
197         size_t nlen = 2*(strlen(name)+1);
198         char *rparam=NULL, *rdata=NULL;
199         char *p;
200
201         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
202         if (!param) {
203                 return false;
204         }
205
206         p = param;
207         memset(p, '\0', 6);
208         SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
209         p += 6;
210         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
211         param_len = PTR_DIFF(p, param);
212
213         if (!cli_send_trans(cli, SMBtrans2,
214                 NULL,                        /* name */
215                 -1, 0,                       /* fid, flags */
216                 &setup, 1, 0,                /* setup, length, max */
217                 param, param_len, 2,         /* param, length, max */
218                 NULL,  0, cli->max_xmit      /* data, length, max */
219                 )) {
220                 SAFE_FREE(param);
221                 return false;
222         }
223
224         SAFE_FREE(param);
225
226         if (!cli_receive_trans(cli, SMBtrans2,
227                         &rparam, &param_len,
228                         &rdata, &data_len)) {
229                 return false;
230         }
231
232         if (data_len < 6) {
233                 SAFE_FREE(rdata);
234                 SAFE_FREE(rparam);
235                 return false;
236         }
237
238         SAFE_FREE(rparam);
239         *retbuf = rdata;
240         *prb_size = (size_t)data_len;
241
242         return true;
243 }
244
245 /****************************************************************************
246  Stat a file (UNIX extensions).
247 ****************************************************************************/
248
249 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
250 {
251         unsigned int param_len = 0;
252         unsigned int data_len = 0;
253         uint16_t setup = TRANSACT2_QPATHINFO;
254         char *param;
255         size_t nlen = 2*(strlen(name)+1);
256         char *rparam=NULL, *rdata=NULL;
257         char *p;
258
259         ZERO_STRUCTP(sbuf);
260
261         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
262         if (!param) {
263                 return false;
264         }
265         p = param;
266         memset(p, '\0', 6);
267         SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
268         p += 6;
269         p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
270         param_len = PTR_DIFF(p, param);
271
272         if (!cli_send_trans(cli, SMBtrans2,
273                         NULL,                        /* name */
274                         -1, 0,                       /* fid, flags */
275                         &setup, 1, 0,                /* setup, length, max */
276                         param, param_len, 2,         /* param, length, max */
277                         NULL,  0, cli->max_xmit      /* data, length, max */
278                         )) {
279                 SAFE_FREE(param);
280                 return false;
281         }
282
283         SAFE_FREE(param);
284
285         if (!cli_receive_trans(cli, SMBtrans2,
286                         &rparam, &param_len,
287                         &rdata, &data_len)) {
288                 return false;
289         }
290
291         if (data_len < 96) {
292                 SAFE_FREE(rdata);
293                 SAFE_FREE(rparam);
294                 return false;
295         }
296
297         sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
298         sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300         sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
301 #else
302         /* assume 512 byte blocks */
303         sbuf->st_ex_blocks /= 512;
304 #endif
305         sbuf->st_ex_ctime = interpret_long_date(rdata + 16);    /* time of last change */
306         sbuf->st_ex_atime = interpret_long_date(rdata + 24);    /* time of last access */
307         sbuf->st_ex_mtime = interpret_long_date(rdata + 32);    /* time of last modification */
308
309         sbuf->st_ex_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
310         sbuf->st_ex_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
311         sbuf->st_ex_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
312 #if defined(HAVE_MAKEDEV)
313         {
314                 uint32_t dev_major = IVAL(rdata,60);
315                 uint32_t dev_minor = IVAL(rdata,68);
316                 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
317         }
318 #endif
319         sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
320         sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
321         sbuf->st_ex_nlink = IVAL(rdata,92);    /* number of hard links */
322
323         SAFE_FREE(rdata);
324         SAFE_FREE(rparam);
325
326         return true;
327 }
328
329 /****************************************************************************
330  Symlink a file (UNIX extensions).
331 ****************************************************************************/
332
333 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
334 {
335         return cli_link_internal(cli, oldname, newname, False);
336 }
337
338 /****************************************************************************
339  Hard a file (UNIX extensions).
340 ****************************************************************************/
341
342 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
343 {
344         return cli_link_internal(cli, oldname, newname, True);
345 }
346
347 /****************************************************************************
348  Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
350
351 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
352 {
353         unsigned int data_len = 0;
354         unsigned int param_len = 0;
355         uint16_t setup = TRANSACT2_SETPATHINFO;
356         size_t nlen = 2*(strlen(fname)+1);
357         char *param;
358         char data[100];
359         char *rparam=NULL, *rdata=NULL;
360         char *p;
361
362         param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
363         if (!param) {
364                 return false;
365         }
366         memset(param, '\0', 6);
367         memset(data, 0, sizeof(data));
368
369         SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
370         p = &param[6];
371
372         p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
373         param_len = PTR_DIFF(p, param);
374
375         memset(data, 0xff, 40); /* Set all sizes/times to no change. */
376
377         SIVAL(data,40,uid);
378         SIVAL(data,48,gid);
379         SIVAL(data,84,mode);
380
381         data_len = 100;
382
383         if (!cli_send_trans(cli, SMBtrans2,
384                         NULL,                        /* name */
385                         -1, 0,                          /* fid, flags */
386                         &setup, 1, 0,                   /* setup, length, max */
387                         param, param_len, 2,            /* param, length, max */
388                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
389                         )) {
390                 SAFE_FREE(param);
391                 return False;
392         }
393
394         SAFE_FREE(param);
395
396         if (!cli_receive_trans(cli, SMBtrans2,
397                         &rparam, &param_len,
398                         &rdata, &data_len)) {
399                 return false;
400         }
401
402         SAFE_FREE(rdata);
403         SAFE_FREE(rparam);
404
405         return true;
406 }
407
408 /****************************************************************************
409  chmod a file (UNIX extensions).
410 ****************************************************************************/
411
412 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
413 {
414         return cli_unix_chmod_chown_internal(cli, fname,
415                 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
416 }
417
418 /****************************************************************************
419  chown a file (UNIX extensions).
420 ****************************************************************************/
421
422 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
423 {
424         return cli_unix_chmod_chown_internal(cli, fname,
425                         SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
426 }
427
428 /****************************************************************************
429  Rename a file.
430 ****************************************************************************/
431
432 static void cli_rename_done(struct tevent_req *subreq);
433
434 struct cli_rename_state {
435         uint16_t vwv[1];
436 };
437
438 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
439                                 struct event_context *ev,
440                                 struct cli_state *cli,
441                                 const char *fname_src,
442                                 const char *fname_dst)
443 {
444         struct tevent_req *req = NULL, *subreq = NULL;
445         struct cli_rename_state *state = NULL;
446         uint8_t additional_flags = 0;
447         uint8_t *bytes = NULL;
448
449         req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
450         if (req == NULL) {
451                 return NULL;
452         }
453
454         SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
455
456         bytes = talloc_array(state, uint8_t, 1);
457         if (tevent_req_nomem(bytes, req)) {
458                 return tevent_req_post(req, ev);
459         }
460         bytes[0] = 4;
461         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
462                                    strlen(fname_src)+1, NULL);
463         if (tevent_req_nomem(bytes, req)) {
464                 return tevent_req_post(req, ev);
465         }
466
467         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
468                         talloc_get_size(bytes)+1);
469         if (tevent_req_nomem(bytes, req)) {
470                 return tevent_req_post(req, ev);
471         }
472
473         bytes[talloc_get_size(bytes)-1] = 4;
474         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
475                                    strlen(fname_dst)+1, NULL);
476         if (tevent_req_nomem(bytes, req)) {
477                 return tevent_req_post(req, ev);
478         }
479
480         subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
481                               1, state->vwv, talloc_get_size(bytes), bytes);
482         if (tevent_req_nomem(subreq, req)) {
483                 return tevent_req_post(req, ev);
484         }
485         tevent_req_set_callback(subreq, cli_rename_done, req);
486         return req;
487 }
488
489 static void cli_rename_done(struct tevent_req *subreq)
490 {
491         struct tevent_req *req = tevent_req_callback_data(
492                                 subreq, struct tevent_req);
493         NTSTATUS status;
494
495         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
496         TALLOC_FREE(subreq);
497         if (!NT_STATUS_IS_OK(status)) {
498                 tevent_req_nterror(req, status);
499                 return;
500         }
501         tevent_req_done(req);
502 }
503
504 NTSTATUS cli_rename_recv(struct tevent_req *req)
505 {
506         return tevent_req_simple_recv_ntstatus(req);
507 }
508
509 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
510 {
511         TALLOC_CTX *frame = talloc_stackframe();
512         struct event_context *ev;
513         struct tevent_req *req;
514         NTSTATUS status = NT_STATUS_OK;
515
516         if (cli_has_async_calls(cli)) {
517                 /*
518                  * Can't use sync call while an async call is in flight
519                  */
520                 status = NT_STATUS_INVALID_PARAMETER;
521                 goto fail;
522         }
523
524         ev = event_context_init(frame);
525         if (ev == NULL) {
526                 status = NT_STATUS_NO_MEMORY;
527                 goto fail;
528         }
529
530         req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
531         if (req == NULL) {
532                 status = NT_STATUS_NO_MEMORY;
533                 goto fail;
534         }
535
536         if (!tevent_req_poll(req, ev)) {
537                 status = map_nt_error_from_unix(errno);
538                 goto fail;
539         }
540
541         status = cli_rename_recv(req);
542
543  fail:
544         TALLOC_FREE(frame);
545         if (!NT_STATUS_IS_OK(status)) {
546                 cli_set_error(cli, status);
547         }
548         return status;
549 }
550
551 /****************************************************************************
552  NT Rename a file.
553 ****************************************************************************/
554
555 static void cli_ntrename_internal_done(struct tevent_req *subreq);
556
557 struct cli_ntrename_internal_state {
558         uint16_t vwv[4];
559 };
560
561 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
562                                 struct event_context *ev,
563                                 struct cli_state *cli,
564                                 const char *fname_src,
565                                 const char *fname_dst,
566                                 uint16_t rename_flag)
567 {
568         struct tevent_req *req = NULL, *subreq = NULL;
569         struct cli_ntrename_internal_state *state = NULL;
570         uint8_t additional_flags = 0;
571         uint8_t *bytes = NULL;
572
573         req = tevent_req_create(mem_ctx, &state,
574                                 struct cli_ntrename_internal_state);
575         if (req == NULL) {
576                 return NULL;
577         }
578
579         SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
580         SSVAL(state->vwv+1, 0, rename_flag);
581
582         bytes = talloc_array(state, uint8_t, 1);
583         if (tevent_req_nomem(bytes, req)) {
584                 return tevent_req_post(req, ev);
585         }
586         bytes[0] = 4;
587         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
588                                    strlen(fname_src)+1, NULL);
589         if (tevent_req_nomem(bytes, req)) {
590                 return tevent_req_post(req, ev);
591         }
592
593         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
594                         talloc_get_size(bytes)+1);
595         if (tevent_req_nomem(bytes, req)) {
596                 return tevent_req_post(req, ev);
597         }
598
599         bytes[talloc_get_size(bytes)-1] = 4;
600         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
601                                    strlen(fname_dst)+1, NULL);
602         if (tevent_req_nomem(bytes, req)) {
603                 return tevent_req_post(req, ev);
604         }
605
606         subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
607                               4, state->vwv, talloc_get_size(bytes), bytes);
608         if (tevent_req_nomem(subreq, req)) {
609                 return tevent_req_post(req, ev);
610         }
611         tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
612         return req;
613 }
614
615 static void cli_ntrename_internal_done(struct tevent_req *subreq)
616 {
617         struct tevent_req *req = tevent_req_callback_data(
618                                 subreq, struct tevent_req);
619         NTSTATUS status;
620
621         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
622         TALLOC_FREE(subreq);
623         if (!NT_STATUS_IS_OK(status)) {
624                 tevent_req_nterror(req, status);
625                 return;
626         }
627         tevent_req_done(req);
628 }
629
630 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
631 {
632         return tevent_req_simple_recv_ntstatus(req);
633 }
634
635 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
636                                 struct event_context *ev,
637                                 struct cli_state *cli,
638                                 const char *fname_src,
639                                 const char *fname_dst)
640 {
641         return cli_ntrename_internal_send(mem_ctx,
642                                           ev,
643                                           cli,
644                                           fname_src,
645                                           fname_dst,
646                                           RENAME_FLAG_RENAME);
647 }
648
649 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
650 {
651         return cli_ntrename_internal_recv(req);
652 }
653
654 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
655 {
656         TALLOC_CTX *frame = talloc_stackframe();
657         struct event_context *ev;
658         struct tevent_req *req;
659         NTSTATUS status = NT_STATUS_OK;
660
661         if (cli_has_async_calls(cli)) {
662                 /*
663                  * Can't use sync call while an async call is in flight
664                  */
665                 status = NT_STATUS_INVALID_PARAMETER;
666                 goto fail;
667         }
668
669         ev = event_context_init(frame);
670         if (ev == NULL) {
671                 status = NT_STATUS_NO_MEMORY;
672                 goto fail;
673         }
674
675         req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
676         if (req == NULL) {
677                 status = NT_STATUS_NO_MEMORY;
678                 goto fail;
679         }
680
681         if (!tevent_req_poll(req, ev)) {
682                 status = map_nt_error_from_unix(errno);
683                 goto fail;
684         }
685
686         status = cli_ntrename_recv(req);
687
688  fail:
689         TALLOC_FREE(frame);
690         if (!NT_STATUS_IS_OK(status)) {
691                 cli_set_error(cli, status);
692         }
693         return status;
694 }
695
696 /****************************************************************************
697  NT hardlink a file.
698 ****************************************************************************/
699
700 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
701                                 struct event_context *ev,
702                                 struct cli_state *cli,
703                                 const char *fname_src,
704                                 const char *fname_dst)
705 {
706         return cli_ntrename_internal_send(mem_ctx,
707                                           ev,
708                                           cli,
709                                           fname_src,
710                                           fname_dst,
711                                           RENAME_FLAG_HARD_LINK);
712 }
713
714 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
715 {
716         return cli_ntrename_internal_recv(req);
717 }
718
719 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
720 {
721         TALLOC_CTX *frame = talloc_stackframe();
722         struct event_context *ev;
723         struct tevent_req *req;
724         NTSTATUS status = NT_STATUS_OK;
725
726         if (cli_has_async_calls(cli)) {
727                 /*
728                  * Can't use sync call while an async call is in flight
729                  */
730                 status = NT_STATUS_INVALID_PARAMETER;
731                 goto fail;
732         }
733
734         ev = event_context_init(frame);
735         if (ev == NULL) {
736                 status = NT_STATUS_NO_MEMORY;
737                 goto fail;
738         }
739
740         req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
741         if (req == NULL) {
742                 status = NT_STATUS_NO_MEMORY;
743                 goto fail;
744         }
745
746         if (!tevent_req_poll(req, ev)) {
747                 status = map_nt_error_from_unix(errno);
748                 goto fail;
749         }
750
751         status = cli_nt_hardlink_recv(req);
752
753  fail:
754         TALLOC_FREE(frame);
755         if (!NT_STATUS_IS_OK(status)) {
756                 cli_set_error(cli, status);
757         }
758         return status;
759 }
760
761 /****************************************************************************
762  Delete a file.
763 ****************************************************************************/
764
765 static void cli_unlink_done(struct tevent_req *subreq);
766
767 struct cli_unlink_state {
768         uint16_t vwv[1];
769 };
770
771 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
772                                 struct event_context *ev,
773                                 struct cli_state *cli,
774                                 const char *fname,
775                                 uint16_t mayhave_attrs)
776 {
777         struct tevent_req *req = NULL, *subreq = NULL;
778         struct cli_unlink_state *state = NULL;
779         uint8_t additional_flags = 0;
780         uint8_t *bytes = NULL;
781
782         req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
783         if (req == NULL) {
784                 return NULL;
785         }
786
787         SSVAL(state->vwv+0, 0, mayhave_attrs);
788
789         bytes = talloc_array(state, uint8_t, 1);
790         if (tevent_req_nomem(bytes, req)) {
791                 return tevent_req_post(req, ev);
792         }
793         bytes[0] = 4;
794         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
795                                    strlen(fname)+1, NULL);
796
797         if (tevent_req_nomem(bytes, req)) {
798                 return tevent_req_post(req, ev);
799         }
800
801         subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
802                                 1, state->vwv, talloc_get_size(bytes), bytes);
803         if (tevent_req_nomem(subreq, req)) {
804                 return tevent_req_post(req, ev);
805         }
806         tevent_req_set_callback(subreq, cli_unlink_done, req);
807         return req;
808 }
809
810 static void cli_unlink_done(struct tevent_req *subreq)
811 {
812         struct tevent_req *req = tevent_req_callback_data(
813                 subreq, struct tevent_req);
814         NTSTATUS status;
815
816         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
817         TALLOC_FREE(subreq);
818         if (!NT_STATUS_IS_OK(status)) {
819                 tevent_req_nterror(req, status);
820                 return;
821         }
822         tevent_req_done(req);
823 }
824
825 NTSTATUS cli_unlink_recv(struct tevent_req *req)
826 {
827         return tevent_req_simple_recv_ntstatus(req);
828 }
829
830 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
831 {
832         TALLOC_CTX *frame = talloc_stackframe();
833         struct event_context *ev;
834         struct tevent_req *req;
835         NTSTATUS status = NT_STATUS_OK;
836
837         if (cli_has_async_calls(cli)) {
838                 /*
839                  * Can't use sync call while an async call is in flight
840                  */
841                 status = NT_STATUS_INVALID_PARAMETER;
842                 goto fail;
843         }
844
845         ev = event_context_init(frame);
846         if (ev == NULL) {
847                 status = NT_STATUS_NO_MEMORY;
848                 goto fail;
849         }
850
851         req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
852         if (req == NULL) {
853                 status = NT_STATUS_NO_MEMORY;
854                 goto fail;
855         }
856
857         if (!tevent_req_poll(req, ev)) {
858                 status = map_nt_error_from_unix(errno);
859                 goto fail;
860         }
861
862         status = cli_unlink_recv(req);
863
864  fail:
865         TALLOC_FREE(frame);
866         if (!NT_STATUS_IS_OK(status)) {
867                 cli_set_error(cli, status);
868         }
869         return status;
870 }
871
872 /****************************************************************************
873  Create a directory.
874 ****************************************************************************/
875
876 static void cli_mkdir_done(struct tevent_req *subreq);
877
878 struct cli_mkdir_state {
879         int dummy;
880 };
881
882 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
883                                   struct event_context *ev,
884                                   struct cli_state *cli,
885                                   const char *dname)
886 {
887         struct tevent_req *req = NULL, *subreq = NULL;
888         struct cli_mkdir_state *state = NULL;
889         uint8_t additional_flags = 0;
890         uint8_t *bytes = NULL;
891
892         req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
893         if (req == NULL) {
894                 return NULL;
895         }
896
897         bytes = talloc_array(state, uint8_t, 1);
898         if (tevent_req_nomem(bytes, req)) {
899                 return tevent_req_post(req, ev);
900         }
901         bytes[0] = 4;
902         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
903                                    strlen(dname)+1, NULL);
904
905         if (tevent_req_nomem(bytes, req)) {
906                 return tevent_req_post(req, ev);
907         }
908
909         subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
910                               0, NULL, talloc_get_size(bytes), bytes);
911         if (tevent_req_nomem(subreq, req)) {
912                 return tevent_req_post(req, ev);
913         }
914         tevent_req_set_callback(subreq, cli_mkdir_done, req);
915         return req;
916 }
917
918 static void cli_mkdir_done(struct tevent_req *subreq)
919 {
920         struct tevent_req *req = tevent_req_callback_data(
921                 subreq, struct tevent_req);
922         NTSTATUS status;
923
924         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
925         TALLOC_FREE(subreq);
926         if (!NT_STATUS_IS_OK(status)) {
927                 tevent_req_nterror(req, status);
928                 return;
929         }
930         tevent_req_done(req);
931 }
932
933 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
934 {
935         return tevent_req_simple_recv_ntstatus(req);
936 }
937
938 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
939 {
940         TALLOC_CTX *frame = talloc_stackframe();
941         struct event_context *ev;
942         struct tevent_req *req;
943         NTSTATUS status = NT_STATUS_OK;
944
945         if (cli_has_async_calls(cli)) {
946                 /*
947                  * Can't use sync call while an async call is in flight
948                  */
949                 status = NT_STATUS_INVALID_PARAMETER;
950                 goto fail;
951         }
952
953         ev = event_context_init(frame);
954         if (ev == NULL) {
955                 status = NT_STATUS_NO_MEMORY;
956                 goto fail;
957         }
958
959         req = cli_mkdir_send(frame, ev, cli, dname);
960         if (req == NULL) {
961                 status = NT_STATUS_NO_MEMORY;
962                 goto fail;
963         }
964
965         if (!tevent_req_poll(req, ev)) {
966                 status = map_nt_error_from_unix(errno);
967                 goto fail;
968         }
969
970         status = cli_mkdir_recv(req);
971
972  fail:
973         TALLOC_FREE(frame);
974         if (!NT_STATUS_IS_OK(status)) {
975                 cli_set_error(cli, status);
976         }
977         return status;
978 }
979
980 /****************************************************************************
981  Remove a directory.
982 ****************************************************************************/
983
984 static void cli_rmdir_done(struct tevent_req *subreq);
985
986 struct cli_rmdir_state {
987         int dummy;
988 };
989
990 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
991                                   struct event_context *ev,
992                                   struct cli_state *cli,
993                                   const char *dname)
994 {
995         struct tevent_req *req = NULL, *subreq = NULL;
996         struct cli_rmdir_state *state = NULL;
997         uint8_t additional_flags = 0;
998         uint8_t *bytes = NULL;
999
1000         req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1001         if (req == NULL) {
1002                 return NULL;
1003         }
1004
1005         bytes = talloc_array(state, uint8_t, 1);
1006         if (tevent_req_nomem(bytes, req)) {
1007                 return tevent_req_post(req, ev);
1008         }
1009         bytes[0] = 4;
1010         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1011                                    strlen(dname)+1, NULL);
1012
1013         if (tevent_req_nomem(bytes, req)) {
1014                 return tevent_req_post(req, ev);
1015         }
1016
1017         subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1018                               0, NULL, talloc_get_size(bytes), bytes);
1019         if (tevent_req_nomem(subreq, req)) {
1020                 return tevent_req_post(req, ev);
1021         }
1022         tevent_req_set_callback(subreq, cli_rmdir_done, req);
1023         return req;
1024 }
1025
1026 static void cli_rmdir_done(struct tevent_req *subreq)
1027 {
1028         struct tevent_req *req = tevent_req_callback_data(
1029                 subreq, struct tevent_req);
1030         NTSTATUS status;
1031
1032         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1033         TALLOC_FREE(subreq);
1034         if (!NT_STATUS_IS_OK(status)) {
1035                 tevent_req_nterror(req, status);
1036                 return;
1037         }
1038         tevent_req_done(req);
1039 }
1040
1041 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1042 {
1043         return tevent_req_simple_recv_ntstatus(req);
1044 }
1045
1046 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1047 {
1048         TALLOC_CTX *frame = talloc_stackframe();
1049         struct event_context *ev;
1050         struct tevent_req *req;
1051         NTSTATUS status = NT_STATUS_OK;
1052
1053         if (cli_has_async_calls(cli)) {
1054                 /*
1055                  * Can't use sync call while an async call is in flight
1056                  */
1057                 status = NT_STATUS_INVALID_PARAMETER;
1058                 goto fail;
1059         }
1060
1061         ev = event_context_init(frame);
1062         if (ev == NULL) {
1063                 status = NT_STATUS_NO_MEMORY;
1064                 goto fail;
1065         }
1066
1067         req = cli_rmdir_send(frame, ev, cli, dname);
1068         if (req == NULL) {
1069                 status = NT_STATUS_NO_MEMORY;
1070                 goto fail;
1071         }
1072
1073         if (!tevent_req_poll(req, ev)) {
1074                 status = map_nt_error_from_unix(errno);
1075                 goto fail;
1076         }
1077
1078         status = cli_rmdir_recv(req);
1079
1080  fail:
1081         TALLOC_FREE(frame);
1082         if (!NT_STATUS_IS_OK(status)) {
1083                 cli_set_error(cli, status);
1084         }
1085         return status;
1086 }
1087
1088 /****************************************************************************
1089  Set or clear the delete on close flag.
1090 ****************************************************************************/
1091
1092 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1093 {
1094         unsigned int data_len = 1;
1095         unsigned int param_len = 6;
1096         uint16_t setup = TRANSACT2_SETFILEINFO;
1097         char param[6];
1098         unsigned char data;
1099         char *rparam=NULL, *rdata=NULL;
1100
1101         memset(param, 0, param_len);
1102         SSVAL(param,0,fnum);
1103         SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1104
1105         data = flag ? 1 : 0;
1106
1107         if (!cli_send_trans(cli, SMBtrans2,
1108                         NULL,                        /* name */
1109                         -1, 0,                          /* fid, flags */
1110                         &setup, 1, 0,                   /* setup, length, max */
1111                         param, param_len, 2,            /* param, length, max */
1112                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
1113                         )) {
1114                 return false;
1115         }
1116
1117         if (!cli_receive_trans(cli, SMBtrans2,
1118                         &rparam, &param_len,
1119                         &rdata, &data_len)) {
1120                 return false;
1121         }
1122
1123         SAFE_FREE(rdata);
1124         SAFE_FREE(rparam);
1125
1126         return true;
1127 }
1128
1129 struct cli_ntcreate_state {
1130         uint16_t vwv[24];
1131         uint16_t fnum;
1132 };
1133
1134 static void cli_ntcreate_done(struct tevent_req *subreq);
1135
1136 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1137                                      struct event_context *ev,
1138                                      struct cli_state *cli,
1139                                      const char *fname,
1140                                      uint32_t CreatFlags,
1141                                      uint32_t DesiredAccess,
1142                                      uint32_t FileAttributes,
1143                                      uint32_t ShareAccess,
1144                                      uint32_t CreateDisposition,
1145                                      uint32_t CreateOptions,
1146                                      uint8_t SecurityFlags)
1147 {
1148         struct tevent_req *req, *subreq;
1149         struct cli_ntcreate_state *state;
1150         uint16_t *vwv;
1151         uint8_t *bytes;
1152         size_t converted_len;
1153
1154         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1155         if (req == NULL) {
1156                 return NULL;
1157         }
1158         vwv = state->vwv;
1159
1160         SCVAL(vwv+0, 0, 0xFF);
1161         SCVAL(vwv+0, 1, 0);
1162         SSVAL(vwv+1, 0, 0);
1163         SCVAL(vwv+2, 0, 0);
1164
1165         if (cli->use_oplocks) {
1166                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1167         }
1168         SIVAL(vwv+3, 1, CreatFlags);
1169         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
1170         SIVAL(vwv+7, 1, DesiredAccess);
1171         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
1172         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
1173         SIVAL(vwv+13, 1, FileAttributes);
1174         SIVAL(vwv+15, 1, ShareAccess);
1175         SIVAL(vwv+17, 1, CreateDisposition);
1176         SIVAL(vwv+19, 1, CreateOptions);
1177         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1178         SCVAL(vwv+23, 1, SecurityFlags);
1179
1180         bytes = talloc_array(state, uint8_t, 0);
1181         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1182                                    fname, strlen(fname)+1,
1183                                    &converted_len);
1184
1185         /* sigh. this copes with broken netapp filer behaviour */
1186         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1187
1188         if (tevent_req_nomem(bytes, req)) {
1189                 return tevent_req_post(req, ev);
1190         }
1191
1192         SIVAL(vwv+2, 1, converted_len);
1193
1194         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1195                               talloc_get_size(bytes), bytes);
1196         if (tevent_req_nomem(subreq, req)) {
1197                 return tevent_req_post(req, ev);
1198         }
1199         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1200         return req;
1201 }
1202
1203 static void cli_ntcreate_done(struct tevent_req *subreq)
1204 {
1205         struct tevent_req *req = tevent_req_callback_data(
1206                 subreq, struct tevent_req);
1207         struct cli_ntcreate_state *state = tevent_req_data(
1208                 req, struct cli_ntcreate_state);
1209         uint8_t wct;
1210         uint16_t *vwv;
1211         uint32_t num_bytes;
1212         uint8_t *bytes;
1213         NTSTATUS status;
1214
1215         status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1216         if (!NT_STATUS_IS_OK(status)) {
1217                 TALLOC_FREE(subreq);
1218                 tevent_req_nterror(req, status);
1219                 return;
1220         }
1221         state->fnum = SVAL(vwv+2, 1);
1222         tevent_req_done(req);
1223 }
1224
1225 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1226 {
1227         struct cli_ntcreate_state *state = tevent_req_data(
1228                 req, struct cli_ntcreate_state);
1229         NTSTATUS status;
1230
1231         if (tevent_req_is_nterror(req, &status)) {
1232                 return status;
1233         }
1234         *pfnum = state->fnum;
1235         return NT_STATUS_OK;
1236 }
1237
1238 NTSTATUS cli_ntcreate(struct cli_state *cli,
1239                       const char *fname,
1240                       uint32_t CreatFlags,
1241                       uint32_t DesiredAccess,
1242                       uint32_t FileAttributes,
1243                       uint32_t ShareAccess,
1244                       uint32_t CreateDisposition,
1245                       uint32_t CreateOptions,
1246                       uint8_t SecurityFlags,
1247                       uint16_t *pfid)
1248 {
1249         TALLOC_CTX *frame = talloc_stackframe();
1250         struct event_context *ev;
1251         struct tevent_req *req;
1252         NTSTATUS status = NT_STATUS_OK;
1253
1254         if (cli_has_async_calls(cli)) {
1255                 /*
1256                  * Can't use sync call while an async call is in flight
1257                  */
1258                 status = NT_STATUS_INVALID_PARAMETER;
1259                 goto fail;
1260         }
1261
1262         ev = event_context_init(frame);
1263         if (ev == NULL) {
1264                 status = NT_STATUS_NO_MEMORY;
1265                 goto fail;
1266         }
1267
1268         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1269                                 DesiredAccess, FileAttributes, ShareAccess,
1270                                 CreateDisposition, CreateOptions,
1271                                 SecurityFlags);
1272         if (req == NULL) {
1273                 status = NT_STATUS_NO_MEMORY;
1274                 goto fail;
1275         }
1276
1277         if (!tevent_req_poll(req, ev)) {
1278                 status = map_nt_error_from_unix(errno);
1279                 goto fail;
1280         }
1281
1282         status = cli_ntcreate_recv(req, pfid);
1283  fail:
1284         TALLOC_FREE(frame);
1285         if (!NT_STATUS_IS_OK(status)) {
1286                 cli_set_error(cli, status);
1287         }
1288         return status;
1289 }
1290
1291 /***********************************************************
1292  Common function for pushing stings, used by smb_bytes_push_str()
1293  and trans_bytes_push_str(). Only difference is the align_odd
1294  parameter setting.
1295 ***********************************************************/
1296
1297 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
1298                                 const char *str, size_t str_len,
1299                                 bool align_odd,
1300                                 size_t *pconverted_size)
1301 {
1302         size_t buflen;
1303         char *converted;
1304         size_t converted_size;
1305
1306         if (buf == NULL) {
1307                 return NULL;
1308         }
1309
1310         buflen = talloc_get_size(buf);
1311
1312         if (align_odd && ucs2 && (buflen % 2 == 0)) {
1313                 /*
1314                  * We're pushing into an SMB buffer, align odd
1315                  */
1316                 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1317                 if (buf == NULL) {
1318                         return NULL;
1319                 }
1320                 buf[buflen] = '\0';
1321                 buflen += 1;
1322         }
1323
1324         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1325                                    ucs2 ? CH_UTF16LE : CH_DOS,
1326                                    str, str_len, &converted,
1327                                    &converted_size, true)) {
1328                 return NULL;
1329         }
1330
1331         buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1332                                    buflen + converted_size);
1333         if (buf == NULL) {
1334                 TALLOC_FREE(converted);
1335                 return NULL;
1336         }
1337
1338         memcpy(buf + buflen, converted, converted_size);
1339
1340         TALLOC_FREE(converted);
1341
1342         if (pconverted_size) {
1343                 *pconverted_size = converted_size;
1344         }
1345
1346         return buf;
1347 }
1348
1349 /***********************************************************
1350  Push a string into an SMB buffer, with odd byte alignment
1351  if it's a UCS2 string.
1352 ***********************************************************/
1353
1354 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1355                             const char *str, size_t str_len,
1356                             size_t *pconverted_size)
1357 {
1358         return internal_bytes_push_str(buf, ucs2, str, str_len,
1359                         true, pconverted_size);
1360 }
1361
1362 /***********************************************************
1363  Same as smb_bytes_push_str(), but without the odd byte
1364  align for ucs2 (we're pushing into a param or data block).
1365  static for now, although this will probably change when
1366  other modules use async trans calls.
1367 ***********************************************************/
1368
1369 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
1370                             const char *str, size_t str_len,
1371                             size_t *pconverted_size)
1372 {
1373         return internal_bytes_push_str(buf, ucs2, str, str_len,
1374                         false, pconverted_size);
1375 }
1376
1377 /****************************************************************************
1378  Open a file
1379  WARNING: if you open with O_WRONLY then getattrE won't work!
1380 ****************************************************************************/
1381
1382 struct cli_open_state {
1383         uint16_t vwv[15];
1384         uint16_t fnum;
1385         struct iovec bytes;
1386 };
1387
1388 static void cli_open_done(struct tevent_req *subreq);
1389
1390 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1391                                    struct event_context *ev,
1392                                    struct cli_state *cli, const char *fname,
1393                                    int flags, int share_mode,
1394                                    struct tevent_req **psmbreq)
1395 {
1396         struct tevent_req *req, *subreq;
1397         struct cli_open_state *state;
1398         unsigned openfn;
1399         unsigned accessmode;
1400         uint8_t additional_flags;
1401         uint8_t *bytes;
1402
1403         req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1404         if (req == NULL) {
1405                 return NULL;
1406         }
1407
1408         openfn = 0;
1409         if (flags & O_CREAT) {
1410                 openfn |= (1<<4);
1411         }
1412         if (!(flags & O_EXCL)) {
1413                 if (flags & O_TRUNC)
1414                         openfn |= (1<<1);
1415                 else
1416                         openfn |= (1<<0);
1417         }
1418
1419         accessmode = (share_mode<<4);
1420
1421         if ((flags & O_ACCMODE) == O_RDWR) {
1422                 accessmode |= 2;
1423         } else if ((flags & O_ACCMODE) == O_WRONLY) {
1424                 accessmode |= 1;
1425         }
1426
1427 #if defined(O_SYNC)
1428         if ((flags & O_SYNC) == O_SYNC) {
1429                 accessmode |= (1<<14);
1430         }
1431 #endif /* O_SYNC */
1432
1433         if (share_mode == DENY_FCB) {
1434                 accessmode = 0xFF;
1435         }
1436
1437         SCVAL(state->vwv + 0, 0, 0xFF);
1438         SCVAL(state->vwv + 0, 1, 0);
1439         SSVAL(state->vwv + 1, 0, 0);
1440         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
1441         SSVAL(state->vwv + 3, 0, accessmode);
1442         SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1443         SSVAL(state->vwv + 5, 0, 0);
1444         SIVAL(state->vwv + 6, 0, 0);
1445         SSVAL(state->vwv + 8, 0, openfn);
1446         SIVAL(state->vwv + 9, 0, 0);
1447         SIVAL(state->vwv + 11, 0, 0);
1448         SIVAL(state->vwv + 13, 0, 0);
1449
1450         additional_flags = 0;
1451
1452         if (cli->use_oplocks) {
1453                 /* if using oplocks then ask for a batch oplock via
1454                    core and extended methods */
1455                 additional_flags =
1456                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1457                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1458         }
1459
1460         bytes = talloc_array(state, uint8_t, 0);
1461         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1462                                    strlen(fname)+1, NULL);
1463
1464         if (tevent_req_nomem(bytes, req)) {
1465                 return tevent_req_post(req, ev);
1466         }
1467
1468         state->bytes.iov_base = (void *)bytes;
1469         state->bytes.iov_len = talloc_get_size(bytes);
1470
1471         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1472                                     15, state->vwv, 1, &state->bytes);
1473         if (subreq == NULL) {
1474                 TALLOC_FREE(req);
1475                 return NULL;
1476         }
1477         tevent_req_set_callback(subreq, cli_open_done, req);
1478         *psmbreq = subreq;
1479         return req;
1480 }
1481
1482 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1483                                  struct cli_state *cli, const char *fname,
1484                                  int flags, int share_mode)
1485 {
1486         struct tevent_req *req, *subreq;
1487         NTSTATUS status;
1488
1489         req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1490                               &subreq);
1491         if (req == NULL) {
1492                 return NULL;
1493         }
1494
1495         status = cli_smb_req_send(subreq);
1496         if (!NT_STATUS_IS_OK(status)) {
1497                 tevent_req_nterror(req, status);
1498                 return tevent_req_post(req, ev);
1499         }
1500         return req;
1501 }
1502
1503 static void cli_open_done(struct tevent_req *subreq)
1504 {
1505         struct tevent_req *req = tevent_req_callback_data(
1506                 subreq, struct tevent_req);
1507         struct cli_open_state *state = tevent_req_data(
1508                 req, struct cli_open_state);
1509         uint8_t wct;
1510         uint16_t *vwv;
1511         NTSTATUS status;
1512
1513         status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1514         if (!NT_STATUS_IS_OK(status)) {
1515                 TALLOC_FREE(subreq);
1516                 tevent_req_nterror(req, status);
1517                 return;
1518         }
1519         state->fnum = SVAL(vwv+2, 0);
1520         tevent_req_done(req);
1521 }
1522
1523 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1524 {
1525         struct cli_open_state *state = tevent_req_data(
1526                 req, struct cli_open_state);
1527         NTSTATUS status;
1528
1529         if (tevent_req_is_nterror(req, &status)) {
1530                 return status;
1531         }
1532         *pfnum = state->fnum;
1533         return NT_STATUS_OK;
1534 }
1535
1536 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1537              int share_mode, uint16_t *pfnum)
1538 {
1539         TALLOC_CTX *frame = talloc_stackframe();
1540         struct event_context *ev;
1541         struct tevent_req *req;
1542         NTSTATUS status = NT_STATUS_OK;
1543
1544         if (cli_has_async_calls(cli)) {
1545                 /*
1546                  * Can't use sync call while an async call is in flight
1547                  */
1548                 status = NT_STATUS_INVALID_PARAMETER;
1549                 goto fail;
1550         }
1551
1552         ev = event_context_init(frame);
1553         if (ev == NULL) {
1554                 status = NT_STATUS_NO_MEMORY;
1555                 goto fail;
1556         }
1557
1558         req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1559         if (req == NULL) {
1560                 status = NT_STATUS_NO_MEMORY;
1561                 goto fail;
1562         }
1563
1564         if (!tevent_req_poll(req, ev)) {
1565                 status = map_nt_error_from_unix(errno);
1566                 goto fail;
1567         }
1568
1569         status = cli_open_recv(req, pfnum);
1570  fail:
1571         TALLOC_FREE(frame);
1572         if (!NT_STATUS_IS_OK(status)) {
1573                 cli_set_error(cli, status);
1574         }
1575         return status;
1576 }
1577
1578 /****************************************************************************
1579  Close a file.
1580 ****************************************************************************/
1581
1582 struct cli_close_state {
1583         uint16_t vwv[3];
1584 };
1585
1586 static void cli_close_done(struct tevent_req *subreq);
1587
1588 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1589                                 struct event_context *ev,
1590                                 struct cli_state *cli,
1591                                 uint16_t fnum,
1592                                 struct tevent_req **psubreq)
1593 {
1594         struct tevent_req *req, *subreq;
1595         struct cli_close_state *state;
1596
1597         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1598         if (req == NULL) {
1599                 return NULL;
1600         }
1601         SSVAL(state->vwv+0, 0, fnum);
1602         SIVALS(state->vwv+1, 0, -1);
1603
1604         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1605                                     0, NULL);
1606         if (subreq == NULL) {
1607                 TALLOC_FREE(req);
1608                 return NULL;
1609         }
1610         tevent_req_set_callback(subreq, cli_close_done, req);
1611         *psubreq = subreq;
1612         return req;
1613 }
1614
1615 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1616                                 struct event_context *ev,
1617                                 struct cli_state *cli,
1618                                 uint16_t fnum)
1619 {
1620         struct tevent_req *req, *subreq;
1621         NTSTATUS status;
1622
1623         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1624         if (req == NULL) {
1625                 return NULL;
1626         }
1627
1628         status = cli_smb_req_send(subreq);
1629         if (!NT_STATUS_IS_OK(status)) {
1630                 tevent_req_nterror(req, status);
1631                 return tevent_req_post(req, ev);
1632         }
1633         return req;
1634 }
1635
1636 static void cli_close_done(struct tevent_req *subreq)
1637 {
1638         struct tevent_req *req = tevent_req_callback_data(
1639                 subreq, struct tevent_req);
1640         NTSTATUS status;
1641
1642         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1643         TALLOC_FREE(subreq);
1644         if (!NT_STATUS_IS_OK(status)) {
1645                 tevent_req_nterror(req, status);
1646                 return;
1647         }
1648         tevent_req_done(req);
1649 }
1650
1651 NTSTATUS cli_close_recv(struct tevent_req *req)
1652 {
1653         return tevent_req_simple_recv_ntstatus(req);
1654 }
1655
1656 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
1657 {
1658         TALLOC_CTX *frame = talloc_stackframe();
1659         struct event_context *ev;
1660         struct tevent_req *req;
1661         NTSTATUS status = NT_STATUS_OK;
1662
1663         if (cli_has_async_calls(cli)) {
1664                 /*
1665                  * Can't use sync call while an async call is in flight
1666                  */
1667                 status = NT_STATUS_INVALID_PARAMETER;
1668                 goto fail;
1669         }
1670
1671         ev = event_context_init(frame);
1672         if (ev == NULL) {
1673                 status = NT_STATUS_NO_MEMORY;
1674                 goto fail;
1675         }
1676
1677         req = cli_close_send(frame, ev, cli, fnum);
1678         if (req == NULL) {
1679                 status = NT_STATUS_NO_MEMORY;
1680                 goto fail;
1681         }
1682
1683         if (!tevent_req_poll(req, ev)) {
1684                 status = map_nt_error_from_unix(errno);
1685                 goto fail;
1686         }
1687
1688         status = cli_close_recv(req);
1689  fail:
1690         TALLOC_FREE(frame);
1691         if (!NT_STATUS_IS_OK(status)) {
1692                 cli_set_error(cli, status);
1693         }
1694         return status;
1695 }
1696
1697 /****************************************************************************
1698  Truncate a file to a specified size
1699 ****************************************************************************/
1700
1701 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
1702 {
1703         unsigned int param_len = 6;
1704         unsigned int data_len = 8;
1705         uint16_t setup = TRANSACT2_SETFILEINFO;
1706         char param[6];
1707         unsigned char data[8];
1708         char *rparam=NULL, *rdata=NULL;
1709         int saved_timeout = cli->timeout;
1710
1711         SSVAL(param,0,fnum);
1712         SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1713         SSVAL(param,4,0);
1714
1715         SBVAL(data, 0, size);
1716
1717         if (!cli_send_trans(cli, SMBtrans2,
1718                             NULL,                    /* name */
1719                             -1, 0,                   /* fid, flags */
1720                             &setup, 1, 0,            /* setup, length, max */
1721                             param, param_len, 2,     /* param, length, max */
1722                             (char *)&data,  data_len,/* data, length, ... */
1723                             cli->max_xmit)) {        /* ... max */
1724                 cli->timeout = saved_timeout;
1725                 return False;
1726         }
1727
1728         if (!cli_receive_trans(cli, SMBtrans2,
1729                                 &rparam, &param_len,
1730                                 &rdata, &data_len)) {
1731                 cli->timeout = saved_timeout;
1732                 SAFE_FREE(rdata);
1733                 SAFE_FREE(rparam);
1734                 return False;
1735         }
1736
1737         cli->timeout = saved_timeout;
1738
1739         SAFE_FREE(rdata);
1740         SAFE_FREE(rparam);
1741
1742         return True;
1743 }
1744
1745
1746 /****************************************************************************
1747  send a lock with a specified locktype
1748  this is used for testing LOCKING_ANDX_CANCEL_LOCK
1749 ****************************************************************************/
1750
1751 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
1752                       uint32_t offset, uint32_t len,
1753                       int timeout, unsigned char locktype)
1754 {
1755         char *p;
1756         int saved_timeout = cli->timeout;
1757
1758         memset(cli->outbuf,'\0',smb_size);
1759         memset(cli->inbuf,'\0', smb_size);
1760
1761         cli_set_message(cli->outbuf,8,0,True);
1762
1763         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1764         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1765         cli_setup_packet(cli);
1766
1767         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1768         SSVAL(cli->outbuf,smb_vwv2,fnum);
1769         SCVAL(cli->outbuf,smb_vwv3,locktype);
1770         SIVALS(cli->outbuf, smb_vwv4, timeout);
1771         SSVAL(cli->outbuf,smb_vwv6,0);
1772         SSVAL(cli->outbuf,smb_vwv7,1);
1773
1774         p = smb_buf(cli->outbuf);
1775         SSVAL(p, 0, cli->pid);
1776         SIVAL(p, 2, offset);
1777         SIVAL(p, 6, len);
1778
1779         p += 10;
1780
1781         cli_setup_bcc(cli, p);
1782
1783         cli_send_smb(cli);
1784
1785         if (timeout != 0) {
1786                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1787         }
1788
1789         if (!cli_receive_smb(cli)) {
1790                 cli->timeout = saved_timeout;
1791                 return NT_STATUS_UNSUCCESSFUL;
1792         }
1793
1794         cli->timeout = saved_timeout;
1795
1796         return cli_nt_error(cli);
1797 }
1798
1799 /****************************************************************************
1800  Lock a file.
1801  note that timeout is in units of 2 milliseconds
1802 ****************************************************************************/
1803
1804 bool cli_lock(struct cli_state *cli, uint16_t fnum,
1805               uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1806 {
1807         char *p;
1808         int saved_timeout = cli->timeout;
1809
1810         memset(cli->outbuf,'\0',smb_size);
1811         memset(cli->inbuf,'\0', smb_size);
1812
1813         cli_set_message(cli->outbuf,8,0,True);
1814
1815         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1816         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1817         cli_setup_packet(cli);
1818
1819         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1820         SSVAL(cli->outbuf,smb_vwv2,fnum);
1821         SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1822         SIVALS(cli->outbuf, smb_vwv4, timeout);
1823         SSVAL(cli->outbuf,smb_vwv6,0);
1824         SSVAL(cli->outbuf,smb_vwv7,1);
1825
1826         p = smb_buf(cli->outbuf);
1827         SSVAL(p, 0, cli->pid);
1828         SIVAL(p, 2, offset);
1829         SIVAL(p, 6, len);
1830
1831         p += 10;
1832
1833         cli_setup_bcc(cli, p);
1834
1835         cli_send_smb(cli);
1836
1837         if (timeout != 0) {
1838                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1839         }
1840
1841         if (!cli_receive_smb(cli)) {
1842                 cli->timeout = saved_timeout;
1843                 return False;
1844         }
1845
1846         cli->timeout = saved_timeout;
1847
1848         if (cli_is_error(cli)) {
1849                 return False;
1850         }
1851
1852         return True;
1853 }
1854
1855 /****************************************************************************
1856  Unlock a file.
1857 ****************************************************************************/
1858
1859 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
1860 {
1861         char *p;
1862
1863         memset(cli->outbuf,'\0',smb_size);
1864         memset(cli->inbuf,'\0',smb_size);
1865
1866         cli_set_message(cli->outbuf,8,0,True);
1867
1868         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1869         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1870         cli_setup_packet(cli);
1871
1872         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1873         SSVAL(cli->outbuf,smb_vwv2,fnum);
1874         SCVAL(cli->outbuf,smb_vwv3,0);
1875         SIVALS(cli->outbuf, smb_vwv4, 0);
1876         SSVAL(cli->outbuf,smb_vwv6,1);
1877         SSVAL(cli->outbuf,smb_vwv7,0);
1878
1879         p = smb_buf(cli->outbuf);
1880         SSVAL(p, 0, cli->pid);
1881         SIVAL(p, 2, offset);
1882         SIVAL(p, 6, len);
1883         p += 10;
1884         cli_setup_bcc(cli, p);
1885         cli_send_smb(cli);
1886         if (!cli_receive_smb(cli)) {
1887                 return False;
1888         }
1889
1890         if (cli_is_error(cli)) {
1891                 return False;
1892         }
1893
1894         return True;
1895 }
1896
1897 /****************************************************************************
1898  Lock a file with 64 bit offsets.
1899 ****************************************************************************/
1900
1901 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
1902                 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1903 {
1904         char *p;
1905         int saved_timeout = cli->timeout;
1906         int ltype;
1907
1908         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1909                 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1910         }
1911
1912         ltype = (lock_type == READ_LOCK? 1 : 0);
1913         ltype |= LOCKING_ANDX_LARGE_FILES;
1914
1915         memset(cli->outbuf,'\0',smb_size);
1916         memset(cli->inbuf,'\0', smb_size);
1917
1918         cli_set_message(cli->outbuf,8,0,True);
1919
1920         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1921         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1922         cli_setup_packet(cli);
1923
1924         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1925         SSVAL(cli->outbuf,smb_vwv2,fnum);
1926         SCVAL(cli->outbuf,smb_vwv3,ltype);
1927         SIVALS(cli->outbuf, smb_vwv4, timeout);
1928         SSVAL(cli->outbuf,smb_vwv6,0);
1929         SSVAL(cli->outbuf,smb_vwv7,1);
1930
1931         p = smb_buf(cli->outbuf);
1932         SIVAL(p, 0, cli->pid);
1933         SOFF_T_R(p, 4, offset);
1934         SOFF_T_R(p, 12, len);
1935         p += 20;
1936
1937         cli_setup_bcc(cli, p);
1938         cli_send_smb(cli);
1939
1940         if (timeout != 0) {
1941                 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1942         }
1943
1944         if (!cli_receive_smb(cli)) {
1945                 cli->timeout = saved_timeout;
1946                 return False;
1947         }
1948
1949         cli->timeout = saved_timeout;
1950
1951         if (cli_is_error(cli)) {
1952                 return False;
1953         }
1954
1955         return True;
1956 }
1957
1958 /****************************************************************************
1959  Unlock a file with 64 bit offsets.
1960 ****************************************************************************/
1961
1962 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
1963 {
1964         char *p;
1965
1966         if (! (cli->capabilities & CAP_LARGE_FILES)) {
1967                 return cli_unlock(cli, fnum, offset, len);
1968         }
1969
1970         memset(cli->outbuf,'\0',smb_size);
1971         memset(cli->inbuf,'\0',smb_size);
1972
1973         cli_set_message(cli->outbuf,8,0,True);
1974
1975         SCVAL(cli->outbuf,smb_com,SMBlockingX);
1976         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1977         cli_setup_packet(cli);
1978
1979         SCVAL(cli->outbuf,smb_vwv0,0xFF);
1980         SSVAL(cli->outbuf,smb_vwv2,fnum);
1981         SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1982         SIVALS(cli->outbuf, smb_vwv4, 0);
1983         SSVAL(cli->outbuf,smb_vwv6,1);
1984         SSVAL(cli->outbuf,smb_vwv7,0);
1985
1986         p = smb_buf(cli->outbuf);
1987         SIVAL(p, 0, cli->pid);
1988         SOFF_T_R(p, 4, offset);
1989         SOFF_T_R(p, 12, len);
1990         p += 20;
1991         cli_setup_bcc(cli, p);
1992         cli_send_smb(cli);
1993         if (!cli_receive_smb(cli)) {
1994                 return False;
1995         }
1996
1997         if (cli_is_error(cli)) {
1998                 return False;
1999         }
2000
2001         return True;
2002 }
2003
2004 /****************************************************************************
2005  Get/unlock a POSIX lock on a file - internal function.
2006 ****************************************************************************/
2007
2008 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2009                 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2010 {
2011         unsigned int param_len = 4;
2012         unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2013         uint16_t setup = TRANSACT2_SETFILEINFO;
2014         char param[4];
2015         unsigned char data[POSIX_LOCK_DATA_SIZE];
2016         char *rparam=NULL, *rdata=NULL;
2017         int saved_timeout = cli->timeout;
2018
2019         SSVAL(param,0,fnum);
2020         SSVAL(param,2,SMB_SET_POSIX_LOCK);
2021
2022         switch (lock_type) {
2023                 case READ_LOCK:
2024                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2025                         break;
2026                 case WRITE_LOCK:
2027                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2028                         break;
2029                 case UNLOCK_LOCK:
2030                         SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2031                         break;
2032                 default:
2033                         return False;
2034         }
2035
2036         if (wait_lock) {
2037                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2038                 cli->timeout = 0x7FFFFFFF;
2039         } else {
2040                 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2041         }
2042
2043         SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2044         SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2045         SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2046
2047         if (!cli_send_trans(cli, SMBtrans2,
2048                         NULL,                        /* name */
2049                         -1, 0,                          /* fid, flags */
2050                         &setup, 1, 0,                   /* setup, length, max */
2051                         param, param_len, 2,            /* param, length, max */
2052                         (char *)&data,  data_len, cli->max_xmit /* data, length, max */
2053                         )) {
2054                 cli->timeout = saved_timeout;
2055                 return False;
2056         }
2057
2058         if (!cli_receive_trans(cli, SMBtrans2,
2059                                 &rparam, &param_len,
2060                                 &rdata, &data_len)) {
2061                 cli->timeout = saved_timeout;
2062                 SAFE_FREE(rdata);
2063                 SAFE_FREE(rparam);
2064                 return False;
2065         }
2066
2067         cli->timeout = saved_timeout;
2068
2069         SAFE_FREE(rdata);
2070         SAFE_FREE(rparam);
2071
2072         return True;
2073 }
2074
2075 /****************************************************************************
2076  POSIX Lock a file.
2077 ****************************************************************************/
2078
2079 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2080                         uint64_t offset, uint64_t len,
2081                         bool wait_lock, enum brl_type lock_type)
2082 {
2083         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2084                 return False;
2085         }
2086         return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2087 }
2088
2089 /****************************************************************************
2090  POSIX Unlock a file.
2091 ****************************************************************************/
2092
2093 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2094 {
2095         return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2096 }
2097
2098 /****************************************************************************
2099  POSIX Get any lock covering a file.
2100 ****************************************************************************/
2101
2102 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2103 {
2104         return True;
2105 }
2106
2107 /****************************************************************************
2108  Do a SMBgetattrE call.
2109 ****************************************************************************/
2110
2111 static void cli_getattrE_done(struct tevent_req *subreq);
2112
2113 struct cli_getattrE_state {
2114         uint16_t vwv[1];
2115         int zone_offset;
2116         uint16_t attr;
2117         SMB_OFF_T size;
2118         time_t change_time;
2119         time_t access_time;
2120         time_t write_time;
2121 };
2122
2123 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2124                                 struct event_context *ev,
2125                                 struct cli_state *cli,
2126                                 uint16_t fnum)
2127 {
2128         struct tevent_req *req = NULL, *subreq = NULL;
2129         struct cli_getattrE_state *state = NULL;
2130         uint8_t additional_flags = 0;
2131
2132         req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2133         if (req == NULL) {
2134                 return NULL;
2135         }
2136
2137         state->zone_offset = cli->serverzone;
2138         SSVAL(state->vwv+0,0,fnum);
2139
2140         subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2141                               1, state->vwv, 0, NULL);
2142         if (tevent_req_nomem(subreq, req)) {
2143                 return tevent_req_post(req, ev);
2144         }
2145         tevent_req_set_callback(subreq, cli_getattrE_done, req);
2146         return req;
2147 }
2148
2149 static void cli_getattrE_done(struct tevent_req *subreq)
2150 {
2151         struct tevent_req *req = tevent_req_callback_data(
2152                 subreq, struct tevent_req);
2153         struct cli_getattrE_state *state = tevent_req_data(
2154                 req, struct cli_getattrE_state);
2155         uint8_t wct;
2156         uint16_t *vwv = NULL;
2157         NTSTATUS status;
2158
2159         status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2160         if (!NT_STATUS_IS_OK(status)) {
2161                 tevent_req_nterror(req, status);
2162                 return;
2163         }
2164
2165         state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2166         state->attr = SVAL(vwv+10,0);
2167         state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2168         state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2169         state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2170
2171         TALLOC_FREE(subreq);
2172         tevent_req_done(req);
2173 }
2174
2175 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2176                         uint16_t *attr,
2177                         SMB_OFF_T *size,
2178                         time_t *change_time,
2179                         time_t *access_time,
2180                         time_t *write_time)
2181 {
2182         struct cli_getattrE_state *state = tevent_req_data(
2183                                 req, struct cli_getattrE_state);
2184         NTSTATUS status;
2185
2186         if (tevent_req_is_nterror(req, &status)) {
2187                 return status;
2188         }
2189         if (attr) {
2190                 *attr = state->attr;
2191         }
2192         if (size) {
2193                 *size = state->size;
2194         }
2195         if (change_time) {
2196                 *change_time = state->change_time;
2197         }
2198         if (access_time) {
2199                 *access_time = state->access_time;
2200         }
2201         if (write_time) {
2202                 *write_time = state->write_time;
2203         }
2204         return NT_STATUS_OK;
2205 }
2206
2207 NTSTATUS cli_getattrE(struct cli_state *cli,
2208                         uint16_t fnum,
2209                         uint16_t *attr,
2210                         SMB_OFF_T *size,
2211                         time_t *change_time,
2212                         time_t *access_time,
2213                         time_t *write_time)
2214 {
2215         TALLOC_CTX *frame = talloc_stackframe();
2216         struct event_context *ev = NULL;
2217         struct tevent_req *req = NULL;
2218         NTSTATUS status = NT_STATUS_OK;
2219
2220         if (cli_has_async_calls(cli)) {
2221                 /*
2222                  * Can't use sync call while an async call is in flight
2223                  */
2224                 status = NT_STATUS_INVALID_PARAMETER;
2225                 goto fail;
2226         }
2227
2228         ev = event_context_init(frame);
2229         if (ev == NULL) {
2230                 status = NT_STATUS_NO_MEMORY;
2231                 goto fail;
2232         }
2233
2234         req = cli_getattrE_send(frame, ev, cli, fnum);
2235         if (req == NULL) {
2236                 status = NT_STATUS_NO_MEMORY;
2237                 goto fail;
2238         }
2239
2240         if (!tevent_req_poll(req, ev)) {
2241                 status = map_nt_error_from_unix(errno);
2242                 goto fail;
2243         }
2244
2245         status = cli_getattrE_recv(req,
2246                                         attr,
2247                                         size,
2248                                         change_time,
2249                                         access_time,
2250                                         write_time);
2251
2252  fail:
2253         TALLOC_FREE(frame);
2254         if (!NT_STATUS_IS_OK(status)) {
2255                 cli_set_error(cli, status);
2256         }
2257         return status;
2258 }
2259
2260 /****************************************************************************
2261  Do a SMBgetatr call
2262 ****************************************************************************/
2263
2264 static void cli_getatr_done(struct tevent_req *subreq);
2265
2266 struct cli_getatr_state {
2267         int zone_offset;
2268         uint16_t attr;
2269         SMB_OFF_T size;
2270         time_t write_time;
2271 };
2272
2273 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2274                                 struct event_context *ev,
2275                                 struct cli_state *cli,
2276                                 const char *fname)
2277 {
2278         struct tevent_req *req = NULL, *subreq = NULL;
2279         struct cli_getatr_state *state = NULL;
2280         uint8_t additional_flags = 0;
2281         uint8_t *bytes = NULL;
2282
2283         req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2284         if (req == NULL) {
2285                 return NULL;
2286         }
2287
2288         state->zone_offset = cli->serverzone;
2289
2290         bytes = talloc_array(state, uint8_t, 1);
2291         if (tevent_req_nomem(bytes, req)) {
2292                 return tevent_req_post(req, ev);
2293         }
2294         bytes[0] = 4;
2295         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2296                                    strlen(fname)+1, NULL);
2297
2298         if (tevent_req_nomem(bytes, req)) {
2299                 return tevent_req_post(req, ev);
2300         }
2301
2302         subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2303                               0, NULL, talloc_get_size(bytes), bytes);
2304         if (tevent_req_nomem(subreq, req)) {
2305                 return tevent_req_post(req, ev);
2306         }
2307         tevent_req_set_callback(subreq, cli_getatr_done, req);
2308         return req;
2309 }
2310
2311 static void cli_getatr_done(struct tevent_req *subreq)
2312 {
2313         struct tevent_req *req = tevent_req_callback_data(
2314                 subreq, struct tevent_req);
2315         struct cli_getatr_state *state = tevent_req_data(
2316                 req, struct cli_getatr_state);
2317         uint8_t wct;
2318         uint16_t *vwv = NULL;
2319         NTSTATUS status;
2320
2321         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2322         if (!NT_STATUS_IS_OK(status)) {
2323                 tevent_req_nterror(req, status);
2324                 return;
2325         }
2326
2327         state->attr = SVAL(vwv+0,0);
2328         state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2329         state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2330
2331         TALLOC_FREE(subreq);
2332         tevent_req_done(req);
2333 }
2334
2335 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2336                         uint16_t *attr,
2337                         SMB_OFF_T *size,
2338                         time_t *write_time)
2339 {
2340         struct cli_getatr_state *state = tevent_req_data(
2341                                 req, struct cli_getatr_state);
2342         NTSTATUS status;
2343
2344         if (tevent_req_is_nterror(req, &status)) {
2345                 return status;
2346         }
2347         if (attr) {
2348                 *attr = state->attr;
2349         }
2350         if (size) {
2351                 *size = state->size;
2352         }
2353         if (write_time) {
2354                 *write_time = state->write_time;
2355         }
2356         return NT_STATUS_OK;
2357 }
2358
2359 NTSTATUS cli_getatr(struct cli_state *cli,
2360                         const char *fname,
2361                         uint16_t *attr,
2362                         SMB_OFF_T *size,
2363                         time_t *write_time)
2364 {
2365         TALLOC_CTX *frame = talloc_stackframe();
2366         struct event_context *ev = NULL;
2367         struct tevent_req *req = NULL;
2368         NTSTATUS status = NT_STATUS_OK;
2369
2370         if (cli_has_async_calls(cli)) {
2371                 /*
2372                  * Can't use sync call while an async call is in flight
2373                  */
2374                 status = NT_STATUS_INVALID_PARAMETER;
2375                 goto fail;
2376         }
2377
2378         ev = event_context_init(frame);
2379         if (ev == NULL) {
2380                 status = NT_STATUS_NO_MEMORY;
2381                 goto fail;
2382         }
2383
2384         req = cli_getatr_send(frame, ev, cli, fname);
2385         if (req == NULL) {
2386                 status = NT_STATUS_NO_MEMORY;
2387                 goto fail;
2388         }
2389
2390         if (!tevent_req_poll(req, ev)) {
2391                 status = map_nt_error_from_unix(errno);
2392                 goto fail;
2393         }
2394
2395         status = cli_getatr_recv(req,
2396                                 attr,
2397                                 size,
2398                                 write_time);
2399
2400  fail:
2401         TALLOC_FREE(frame);
2402         if (!NT_STATUS_IS_OK(status)) {
2403                 cli_set_error(cli, status);
2404         }
2405         return status;
2406 }
2407
2408 /****************************************************************************
2409  Do a SMBsetattrE call.
2410 ****************************************************************************/
2411
2412 static void cli_setattrE_done(struct tevent_req *subreq);
2413
2414 struct cli_setattrE_state {
2415         int dummy;
2416 };
2417
2418 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2419                                 struct event_context *ev,
2420                                 struct cli_state *cli,
2421                                 uint16_t fnum,
2422                                 time_t change_time,
2423                                 time_t access_time,
2424                                 time_t write_time)
2425 {
2426         struct tevent_req *req = NULL, *subreq = NULL;
2427         struct cli_setattrE_state *state = NULL;
2428         uint8_t additional_flags = 0;
2429         uint16_t vwv[7];
2430
2431         req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2432         if (req == NULL) {
2433                 return NULL;
2434         }
2435
2436         memset(vwv, '\0', sizeof(vwv));
2437         SSVAL(vwv+0, 0, fnum);
2438         cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2439         cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2440         cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2441
2442         subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2443                               7, vwv, 0, NULL);
2444         if (tevent_req_nomem(subreq, req)) {
2445                 return tevent_req_post(req, ev);
2446         }
2447         tevent_req_set_callback(subreq, cli_setattrE_done, req);
2448         return req;
2449 }
2450
2451 static void cli_setattrE_done(struct tevent_req *subreq)
2452 {
2453         struct tevent_req *req = tevent_req_callback_data(
2454                 subreq, struct tevent_req);
2455         NTSTATUS status;
2456
2457         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2458         TALLOC_FREE(subreq);
2459         if (!NT_STATUS_IS_OK(status)) {
2460                 tevent_req_nterror(req, status);
2461                 return;
2462         }
2463         tevent_req_done(req);
2464 }
2465
2466 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2467 {
2468         return tevent_req_simple_recv_ntstatus(req);
2469 }
2470
2471 NTSTATUS cli_setattrE(struct cli_state *cli,
2472                         uint16_t fnum,
2473                         time_t change_time,
2474                         time_t access_time,
2475                         time_t write_time)
2476 {
2477         TALLOC_CTX *frame = talloc_stackframe();
2478         struct event_context *ev = NULL;
2479         struct tevent_req *req = NULL;
2480         NTSTATUS status = NT_STATUS_OK;
2481
2482         if (cli_has_async_calls(cli)) {
2483                 /*
2484                  * Can't use sync call while an async call is in flight
2485                  */
2486                 status = NT_STATUS_INVALID_PARAMETER;
2487                 goto fail;
2488         }
2489
2490         ev = event_context_init(frame);
2491         if (ev == NULL) {
2492                 status = NT_STATUS_NO_MEMORY;
2493                 goto fail;
2494         }
2495
2496         req = cli_setattrE_send(frame, ev,
2497                         cli,
2498                         fnum,
2499                         change_time,
2500                         access_time,
2501                         write_time);
2502
2503         if (req == NULL) {
2504                 status = NT_STATUS_NO_MEMORY;
2505                 goto fail;
2506         }
2507
2508         if (!tevent_req_poll(req, ev)) {
2509                 status = map_nt_error_from_unix(errno);
2510                 goto fail;
2511         }
2512
2513         status = cli_setattrE_recv(req);
2514
2515  fail:
2516         TALLOC_FREE(frame);
2517         if (!NT_STATUS_IS_OK(status)) {
2518                 cli_set_error(cli, status);
2519         }
2520         return status;
2521 }
2522
2523 /****************************************************************************
2524  Do a SMBsetatr call.
2525 ****************************************************************************/
2526
2527 static void cli_setatr_done(struct tevent_req *subreq);
2528
2529 struct cli_setatr_state {
2530         uint16_t vwv[8];
2531 };
2532
2533 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
2534                                 struct event_context *ev,
2535                                 struct cli_state *cli,
2536                                 const char *fname,
2537                                 uint16_t attr,
2538                                 time_t mtime)
2539 {
2540         struct tevent_req *req = NULL, *subreq = NULL;
2541         struct cli_setatr_state *state = NULL;
2542         uint8_t additional_flags = 0;
2543         uint8_t *bytes = NULL;
2544
2545         req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
2546         if (req == NULL) {
2547                 return NULL;
2548         }
2549
2550         memset(state->vwv, '\0', sizeof(state->vwv));
2551         SSVAL(state->vwv+0, 0, attr);
2552         cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
2553
2554         bytes = talloc_array(state, uint8_t, 1);
2555         if (tevent_req_nomem(bytes, req)) {
2556                 return tevent_req_post(req, ev);
2557         }
2558         bytes[0] = 4;
2559         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2560                                    strlen(fname)+1, NULL);
2561         if (tevent_req_nomem(bytes, req)) {
2562                 return tevent_req_post(req, ev);
2563         }
2564         bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
2565                         talloc_get_size(bytes)+1);
2566         if (tevent_req_nomem(bytes, req)) {
2567                 return tevent_req_post(req, ev);
2568         }
2569
2570         bytes[talloc_get_size(bytes)-1] = 4;
2571         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
2572                                    1, NULL);
2573         if (tevent_req_nomem(bytes, req)) {
2574                 return tevent_req_post(req, ev);
2575         }
2576
2577         subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
2578                               8, state->vwv, talloc_get_size(bytes), bytes);
2579         if (tevent_req_nomem(subreq, req)) {
2580                 return tevent_req_post(req, ev);
2581         }
2582         tevent_req_set_callback(subreq, cli_setatr_done, req);
2583         return req;
2584 }
2585
2586 static void cli_setatr_done(struct tevent_req *subreq)
2587 {
2588         struct tevent_req *req = tevent_req_callback_data(
2589                 subreq, struct tevent_req);
2590         NTSTATUS status;
2591
2592         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2593         TALLOC_FREE(subreq);
2594         if (!NT_STATUS_IS_OK(status)) {
2595                 tevent_req_nterror(req, status);
2596                 return;
2597         }
2598         tevent_req_done(req);
2599 }
2600
2601 NTSTATUS cli_setatr_recv(struct tevent_req *req)
2602 {
2603         return tevent_req_simple_recv_ntstatus(req);
2604 }
2605
2606 NTSTATUS cli_setatr(struct cli_state *cli,
2607                 const char *fname,
2608                 uint16_t attr,
2609                 time_t mtime)
2610 {
2611         TALLOC_CTX *frame = talloc_stackframe();
2612         struct event_context *ev = NULL;
2613         struct tevent_req *req = NULL;
2614         NTSTATUS status = NT_STATUS_OK;
2615
2616         if (cli_has_async_calls(cli)) {
2617                 /*
2618                  * Can't use sync call while an async call is in flight
2619                  */
2620                 status = NT_STATUS_INVALID_PARAMETER;
2621                 goto fail;
2622         }
2623
2624         ev = event_context_init(frame);
2625         if (ev == NULL) {
2626                 status = NT_STATUS_NO_MEMORY;
2627                 goto fail;
2628         }
2629
2630         req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
2631         if (req == NULL) {
2632                 status = NT_STATUS_NO_MEMORY;
2633                 goto fail;
2634         }
2635
2636         if (!tevent_req_poll(req, ev)) {
2637                 status = map_nt_error_from_unix(errno);
2638                 goto fail;
2639         }
2640
2641         status = cli_setatr_recv(req);
2642
2643  fail:
2644         TALLOC_FREE(frame);
2645         if (!NT_STATUS_IS_OK(status)) {
2646                 cli_set_error(cli, status);
2647         }
2648         return status;
2649 }
2650
2651 /****************************************************************************
2652  Check for existance of a dir.
2653 ****************************************************************************/
2654
2655 static void cli_chkpath_done(struct tevent_req *subreq);
2656
2657 struct cli_chkpath_state {
2658         int dummy;
2659 };
2660
2661 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2662                                   struct event_context *ev,
2663                                   struct cli_state *cli,
2664                                   const char *fname)
2665 {
2666         struct tevent_req *req = NULL, *subreq = NULL;
2667         struct cli_chkpath_state *state = NULL;
2668         uint8_t additional_flags = 0;
2669         uint8_t *bytes = NULL;
2670
2671         req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2672         if (req == NULL) {
2673                 return NULL;
2674         }
2675
2676         bytes = talloc_array(state, uint8_t, 1);
2677         if (tevent_req_nomem(bytes, req)) {
2678                 return tevent_req_post(req, ev);
2679         }
2680         bytes[0] = 4;
2681         bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2682                                    strlen(fname)+1, NULL);
2683
2684         if (tevent_req_nomem(bytes, req)) {
2685                 return tevent_req_post(req, ev);
2686         }
2687
2688         subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2689                               0, NULL, talloc_get_size(bytes), bytes);
2690         if (tevent_req_nomem(subreq, req)) {
2691                 return tevent_req_post(req, ev);
2692         }
2693         tevent_req_set_callback(subreq, cli_chkpath_done, req);
2694         return req;
2695 }
2696
2697 static void cli_chkpath_done(struct tevent_req *subreq)
2698 {
2699         struct tevent_req *req = tevent_req_callback_data(
2700                 subreq, struct tevent_req);
2701         NTSTATUS status;
2702
2703         status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2704         TALLOC_FREE(subreq);
2705         if (!NT_STATUS_IS_OK(status)) {
2706                 tevent_req_nterror(req, status);
2707                 return;
2708         }
2709         tevent_req_done(req);
2710 }
2711
2712 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2713 {
2714         return tevent_req_simple_recv_ntstatus(req);
2715 }
2716
2717 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2718 {
2719         TALLOC_CTX *frame = talloc_stackframe();
2720         struct event_context *ev = NULL;
2721         struct tevent_req *req = NULL;
2722         char *path2 = NULL;
2723         NTSTATUS status = NT_STATUS_OK;
2724
2725         if (cli_has_async_calls(cli)) {
2726                 /*
2727                  * Can't use sync call while an async call is in flight
2728                  */
2729                 status = NT_STATUS_INVALID_PARAMETER;
2730                 goto fail;
2731         }
2732
2733         path2 = talloc_strdup(frame, path);
2734         if (!path2) {
2735                 status = NT_STATUS_NO_MEMORY;
2736                 goto fail;
2737         }
2738         trim_char(path2,'\0','\\');
2739         if (!*path2) {
2740                 path2 = talloc_strdup(frame, "\\");
2741                 if (!path2) {
2742                         status = NT_STATUS_NO_MEMORY;
2743                         goto fail;
2744                 }
2745         }
2746
2747         ev = event_context_init(frame);
2748         if (ev == NULL) {
2749                 status = NT_STATUS_NO_MEMORY;
2750                 goto fail;
2751         }
2752
2753         req = cli_chkpath_send(frame, ev, cli, path2);
2754         if (req == NULL) {
2755                 status = NT_STATUS_NO_MEMORY;
2756                 goto fail;
2757         }
2758
2759         if (!tevent_req_poll(req, ev)) {
2760                 status = map_nt_error_from_unix(errno);
2761                 goto fail;
2762         }
2763
2764         status = cli_chkpath_recv(req);
2765
2766  fail:
2767         TALLOC_FREE(frame);
2768         if (!NT_STATUS_IS_OK(status)) {
2769                 cli_set_error(cli, status);
2770         }
2771         return status;
2772 }
2773
2774 /****************************************************************************
2775  Query disk space.
2776 ****************************************************************************/
2777
2778 static void cli_dskattr_done(struct tevent_req *subreq);
2779
2780 struct cli_dskattr_state {
2781         int bsize;
2782         int total;
2783         int avail;
2784 };
2785
2786 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2787                                   struct event_context *ev,
2788                                   struct cli_state *cli)
2789 {
2790         struct tevent_req *req = NULL, *subreq = NULL;
2791         struct cli_dskattr_state *state = NULL;
2792         uint8_t additional_flags = 0;
2793
2794         req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2795         if (req == NULL) {
2796                 return NULL;
2797         }
2798
2799         subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2800                               0, NULL, 0, NULL);
2801         if (tevent_req_nomem(subreq, req)) {
2802                 return tevent_req_post(req, ev);
2803         }
2804         tevent_req_set_callback(subreq, cli_dskattr_done, req);
2805         return req;
2806 }
2807
2808 static void cli_dskattr_done(struct tevent_req *subreq)
2809 {
2810         struct tevent_req *req = tevent_req_callback_data(
2811                 subreq, struct tevent_req);
2812         struct cli_dskattr_state *state = tevent_req_data(
2813                 req, struct cli_dskattr_state);
2814         uint8_t wct;
2815         uint16_t *vwv = NULL;
2816         NTSTATUS status;
2817
2818         status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2819         if (!NT_STATUS_IS_OK(status)) {
2820                 tevent_req_nterror(req, status);
2821                 return;
2822         }
2823         state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2824         state->total = SVAL(vwv+0, 0);
2825         state->avail = SVAL(vwv+3, 0);
2826         TALLOC_FREE(subreq);
2827         tevent_req_done(req);
2828 }
2829
2830 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2831 {
2832         struct cli_dskattr_state *state = tevent_req_data(
2833                                 req, struct cli_dskattr_state);
2834         NTSTATUS status;
2835
2836         if (tevent_req_is_nterror(req, &status)) {
2837                 return status;
2838         }
2839         *bsize = state->bsize;
2840         *total = state->total;
2841         *avail = state->avail;
2842         return NT_STATUS_OK;
2843 }
2844
2845 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2846 {
2847         TALLOC_CTX *frame = talloc_stackframe();
2848         struct event_context *ev = NULL;
2849         struct tevent_req *req = NULL;
2850         NTSTATUS status = NT_STATUS_OK;
2851
2852         if (cli_has_async_calls(cli)) {
2853                 /*
2854                  * Can't use sync call while an async call is in flight
2855                  */
2856                 status = NT_STATUS_INVALID_PARAMETER;
2857                 goto fail;
2858         }
2859
2860         ev = event_context_init(frame);
2861         if (ev == NULL) {
2862                 status = NT_STATUS_NO_MEMORY;
2863                 goto fail;
2864         }
2865
2866         req = cli_dskattr_send(frame, ev, cli);
2867         if (req == NULL) {
2868                 status = NT_STATUS_NO_MEMORY;
2869                 goto fail;
2870         }
2871
2872         if (!tevent_req_poll(req, ev)) {
2873                 status = map_nt_error_from_unix(errno);
2874                 goto fail;
2875         }
2876
2877         status = cli_dskattr_recv(req, bsize, total, avail);
2878
2879  fail:
2880         TALLOC_FREE(frame);
2881         if (!NT_STATUS_IS_OK(status)) {
2882                 cli_set_error(cli, status);
2883         }
2884         return status;
2885 }
2886
2887 /****************************************************************************
2888  Create and open a temporary file.
2889 ****************************************************************************/
2890
2891 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2892 {
2893         int len;
2894         char *p;
2895
2896         memset(cli->outbuf,'\0',smb_size);
2897         memset(cli->inbuf,'\0',smb_size);
2898
2899         cli_set_message(cli->outbuf,3,0,True);
2900
2901         SCVAL(cli->outbuf,smb_com,SMBctemp);
2902         SSVAL(cli->outbuf,smb_tid,cli->cnum);
2903         cli_setup_packet(cli);
2904
2905         SSVAL(cli->outbuf,smb_vwv0,0);
2906         SIVALS(cli->outbuf,smb_vwv1,-1);
2907
2908         p = smb_buf(cli->outbuf);
2909         *p++ = 4;
2910         p += clistr_push(cli, p, path,
2911                         cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2912
2913         cli_setup_bcc(cli, p);
2914
2915         cli_send_smb(cli);
2916         if (!cli_receive_smb(cli)) {
2917                 return -1;
2918         }
2919
2920         if (cli_is_error(cli)) {
2921                 return -1;
2922         }
2923
2924         /* despite the spec, the result has a -1, followed by
2925            length, followed by name */
2926         p = smb_buf(cli->inbuf);
2927         p += 4;
2928         len = smb_buflen(cli->inbuf) - 4;
2929         if (len <= 0 || len > PATH_MAX) return -1;
2930
2931         if (tmp_path) {
2932                 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2933                 if (!path2) {
2934                         return -1;
2935                 }
2936                 clistr_pull(cli->inbuf, path2, p,
2937                             len+1, len, STR_ASCII);
2938                 *tmp_path = path2;
2939         }
2940
2941         return SVAL(cli->inbuf,smb_vwv0);
2942 }
2943
2944 /*
2945    send a raw ioctl - used by the torture code
2946 */
2947 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
2948 {
2949         memset(cli->outbuf,'\0',smb_size);
2950         memset(cli->inbuf,'\0',smb_size);
2951
2952         cli_set_message(cli->outbuf, 3, 0, True);
2953         SCVAL(cli->outbuf,smb_com,SMBioctl);
2954         cli_setup_packet(cli);
2955
2956         SSVAL(cli->outbuf, smb_vwv0, fnum);
2957         SSVAL(cli->outbuf, smb_vwv1, code>>16);
2958         SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2959
2960         cli_send_smb(cli);
2961         if (!cli_receive_smb(cli)) {
2962                 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2963         }
2964
2965         if (cli_is_error(cli)) {
2966                 return cli_nt_error(cli);
2967         }
2968
2969         *blob = data_blob_null;
2970
2971         return NT_STATUS_OK;
2972 }
2973
2974 /*********************************************************
2975  Set an extended attribute utility fn.
2976 *********************************************************/
2977
2978 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2979                         const char *ea_name, const char *ea_val, size_t ea_len)
2980 {
2981         unsigned int data_len = 0;
2982         char *data = NULL;
2983         char *rparam=NULL, *rdata=NULL;
2984         char *p;
2985         size_t ea_namelen = strlen(ea_name);
2986
2987         if (ea_namelen == 0 && ea_len == 0) {
2988                 data_len = 4;
2989                 data = (char *)SMB_MALLOC(data_len);
2990                 if (!data) {
2991                         return False;
2992                 }
2993                 p = data;
2994                 SIVAL(p,0,data_len);
2995         } else {
2996                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2997                 data = (char *)SMB_MALLOC(data_len);
2998                 if (!data) {
2999                         return False;
3000                 }
3001                 p = data;
3002                 SIVAL(p,0,data_len);
3003                 p += 4;
3004                 SCVAL(p, 0, 0); /* EA flags. */
3005                 SCVAL(p, 1, ea_namelen);
3006                 SSVAL(p, 2, ea_len);
3007                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3008                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3009         }
3010
3011         if (!cli_send_trans(cli, SMBtrans2,
3012                         NULL,                        /* name */
3013                         -1, 0,                          /* fid, flags */
3014                         &setup, 1, 0,                   /* setup, length, max */
3015                         param, param_len, 2,            /* param, length, max */
3016                         data,  data_len, cli->max_xmit /* data, length, max */
3017                         )) {
3018                 SAFE_FREE(data);
3019                 return False;
3020         }
3021
3022         if (!cli_receive_trans(cli, SMBtrans2,
3023                         &rparam, &param_len,
3024                         &rdata, &data_len)) {
3025                         SAFE_FREE(data);
3026                 return false;
3027         }
3028
3029         SAFE_FREE(data);
3030         SAFE_FREE(rdata);
3031         SAFE_FREE(rparam);
3032
3033         return True;
3034 }
3035
3036 /*********************************************************
3037  Set an extended attribute on a pathname.
3038 *********************************************************/
3039
3040 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3041 {
3042         uint16_t setup = TRANSACT2_SETPATHINFO;
3043         unsigned int param_len = 0;
3044         char *param;
3045         size_t srclen = 2*(strlen(path)+1);
3046         char *p;
3047         bool ret;
3048
3049         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3050         if (!param) {
3051                 return false;
3052         }
3053         memset(param, '\0', 6);
3054         SSVAL(param,0,SMB_INFO_SET_EA);
3055         p = &param[6];
3056
3057         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3058         param_len = PTR_DIFF(p, param);
3059
3060         ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3061         SAFE_FREE(param);
3062         return ret;
3063 }
3064
3065 /*********************************************************
3066  Set an extended attribute on an fnum.
3067 *********************************************************/
3068
3069 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
3070 {
3071         char param[6];
3072         uint16_t setup = TRANSACT2_SETFILEINFO;
3073
3074         memset(param, 0, 6);
3075         SSVAL(param,0,fnum);
3076         SSVAL(param,2,SMB_INFO_SET_EA);
3077
3078         return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
3079 }
3080
3081 /*********************************************************
3082  Get an extended attribute list utility fn.
3083 *********************************************************/
3084
3085 static bool cli_get_ea_list(struct cli_state *cli,
3086                 uint16_t setup, char *param, unsigned int param_len,
3087                 TALLOC_CTX *ctx,
3088                 size_t *pnum_eas,
3089                 struct ea_struct **pea_list)
3090 {
3091         unsigned int data_len = 0;
3092         unsigned int rparam_len, rdata_len;
3093         char *rparam=NULL, *rdata=NULL;
3094         char *p;
3095         size_t ea_size;
3096         size_t num_eas;
3097         bool ret = False;
3098         struct ea_struct *ea_list;
3099
3100         *pnum_eas = 0;
3101         if (pea_list) {
3102                 *pea_list = NULL;
3103         }
3104
3105         if (!cli_send_trans(cli, SMBtrans2,
3106                         NULL,           /* Name */
3107                         -1, 0,          /* fid, flags */
3108                         &setup, 1, 0,   /* setup, length, max */
3109                         param, param_len, 10, /* param, length, max */
3110                         NULL, data_len, cli->max_xmit /* data, length, max */
3111                                 )) {
3112                 return False;
3113         }
3114
3115         if (!cli_receive_trans(cli, SMBtrans2,
3116                         &rparam, &rparam_len,
3117                         &rdata, &rdata_len)) {
3118                 return False;
3119         }
3120
3121         if (!rdata || rdata_len < 4) {
3122                 goto out;
3123         }
3124
3125         ea_size = (size_t)IVAL(rdata,0);
3126         if (ea_size > rdata_len) {
3127                 goto out;
3128         }
3129
3130         if (ea_size == 0) {
3131                 /* No EA's present. */
3132                 ret = True;
3133                 goto out;
3134         }
3135
3136         p = rdata + 4;
3137         ea_size -= 4;
3138
3139         /* Validate the EA list and count it. */
3140         for (num_eas = 0; ea_size >= 4; num_eas++) {
3141                 unsigned int ea_namelen = CVAL(p,1);
3142                 unsigned int ea_valuelen = SVAL(p,2);
3143                 if (ea_namelen == 0) {
3144                         goto out;
3145                 }
3146                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3147                         goto out;
3148                 }
3149                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3150                 p += 4 + ea_namelen + 1 + ea_valuelen;
3151         }
3152
3153         if (num_eas == 0) {
3154                 ret = True;
3155                 goto out;
3156         }
3157
3158         *pnum_eas = num_eas;
3159         if (!pea_list) {
3160                 /* Caller only wants number of EA's. */
3161                 ret = True;
3162                 goto out;
3163         }
3164
3165         ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
3166         if (!ea_list) {
3167                 goto out;
3168         }
3169
3170         ea_size = (size_t)IVAL(rdata,0);
3171         p = rdata + 4;
3172
3173         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
3174                 struct ea_struct *ea = &ea_list[num_eas];
3175                 fstring unix_ea_name;
3176                 unsigned int ea_namelen = CVAL(p,1);
3177                 unsigned int ea_valuelen = SVAL(p,2);
3178
3179                 ea->flags = CVAL(p,0);
3180                 unix_ea_name[0] = '\0';
3181                 pull_ascii_fstring(unix_ea_name, p + 4);
3182                 ea->name = talloc_strdup(ctx, unix_ea_name);
3183                 /* Ensure the value is null terminated (in case it's a string). */
3184                 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
3185                 if (!ea->value.data) {
3186                         goto out;
3187                 }
3188                 if (ea_valuelen) {
3189                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
3190                 }
3191                 ea->value.data[ea_valuelen] = 0;
3192                 ea->value.length--;
3193                 p += 4 + ea_namelen + 1 + ea_valuelen;
3194         }
3195
3196         *pea_list = ea_list;
3197         ret = True;
3198
3199  out :
3200
3201         SAFE_FREE(rdata);
3202         SAFE_FREE(rparam);
3203         return ret;
3204 }
3205
3206 /*********************************************************
3207  Get an extended attribute list from a pathname.
3208 *********************************************************/
3209
3210 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
3211                 TALLOC_CTX *ctx,
3212                 size_t *pnum_eas,
3213                 struct ea_struct **pea_list)
3214 {
3215         uint16_t setup = TRANSACT2_QPATHINFO;
3216         unsigned int param_len = 0;
3217         char *param;
3218         char *p;
3219         size_t srclen = 2*(strlen(path)+1);
3220         bool ret;
3221
3222         param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3223         if (!param) {
3224                 return false;
3225         }
3226         p = param;
3227         memset(p, 0, 6);
3228         SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
3229         p += 6;
3230         p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3231         param_len = PTR_DIFF(p, param);
3232
3233         ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
3234         SAFE_FREE(param);
3235         return ret;
3236 }
3237
3238 /*********************************************************
3239  Get an extended attribute list from an fnum.
3240 *********************************************************/
3241
3242 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
3243                 TALLOC_CTX *ctx,
3244                 size_t *pnum_eas,
3245                 struct ea_struct **pea_list)
3246 {
3247         uint16_t setup = TRANSACT2_QFILEINFO;
3248         char param[6];
3249
3250         memset(param, 0, 6);
3251         SSVAL(param,0,fnum);
3252         SSVAL(param,2,SMB_INFO_SET_EA);
3253
3254         return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
3255 }
3256
3257 /****************************************************************************
3258  Convert open "flags" arg to uint32_t on wire.
3259 ****************************************************************************/
3260
3261 static uint32_t open_flags_to_wire(int flags)
3262 {
3263         int open_mode = flags & O_ACCMODE;
3264         uint32_t ret = 0;
3265
3266         switch (open_mode) {
3267                 case O_WRONLY:
3268                         ret |= SMB_O_WRONLY;
3269                         break;
3270                 case O_RDWR:
3271                         ret |= SMB_O_RDWR;
3272                         break;
3273                 default:
3274                 case O_RDONLY:
3275                         ret |= SMB_O_RDONLY;
3276                         break;
3277         }
3278
3279         if (flags & O_CREAT) {
3280                 ret |= SMB_O_CREAT;
3281         }
3282         if (flags & O_EXCL) {
3283                 ret |= SMB_O_EXCL;
3284         }
3285         if (flags & O_TRUNC) {
3286                 ret |= SMB_O_TRUNC;
3287         }
3288 #if defined(O_SYNC)
3289         if (flags & O_SYNC) {
3290                 ret |= SMB_O_SYNC;
3291         }
3292 #endif /* O_SYNC */
3293         if (flags & O_APPEND) {
3294                 ret |= SMB_O_APPEND;
3295         }
3296 #if defined(O_DIRECT)
3297         if (flags & O_DIRECT) {
3298                 ret |= SMB_O_DIRECT;
3299         }
3300 #endif
3301 #if defined(O_DIRECTORY)
3302         if (flags & O_DIRECTORY) {
3303                 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3304                 ret |= SMB_O_DIRECTORY;
3305         }
3306 #endif
3307         return ret;
3308 }
3309
3310 /****************************************************************************
3311  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
3312 ****************************************************************************/
3313
3314 struct posix_open_state {
3315         uint16_t setup;
3316         uint8_t *param;
3317         uint8_t data[18];
3318         uint16_t fnum; /* Out */
3319 };
3320
3321 static void cli_posix_open_internal_done(struct tevent_req *subreq)
3322 {
3323         struct tevent_req *req = tevent_req_callback_data(
3324                                 subreq, struct tevent_req);
3325         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
3326         NTSTATUS status;
3327         uint8_t *data;
3328         uint32_t num_data;
3329
3330         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, &data, &num_data);
3331         TALLOC_FREE(subreq);
3332         if (!NT_STATUS_IS_OK(status)) {
3333                 tevent_req_nterror(req, status);
3334                 return;
3335         }
3336         if (num_data < 12) {
3337                 tevent_req_nterror(req, status);
3338                 return;
3339         }
3340         state->fnum = SVAL(data,2);
3341         tevent_req_done(req);
3342 }
3343
3344 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
3345                                         struct event_context *ev,
3346                                         struct cli_state *cli,
3347                                         const char *fname,
3348                                         int flags,
3349                                         mode_t mode,
3350                                         bool is_dir)
3351 {
3352         struct tevent_req *req = NULL, *subreq = NULL;
3353         struct posix_open_state *state = NULL;
3354         uint32_t wire_flags = open_flags_to_wire(flags);
3355
3356         req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
3357         if (req == NULL) {
3358                 return NULL;
3359         }
3360
3361         /* Setup setup word. */
3362         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
3363
3364         /* Setup param array. */
3365         state->param = talloc_array(state, uint8_t, 6);
3366         if (tevent_req_nomem(state->param, req)) {
3367                 return tevent_req_post(req, ev);
3368         }
3369         memset(state->param, '\0', 6);
3370         SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
3371
3372         state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
3373                                    strlen(fname)+1, NULL);
3374
3375         if (tevent_req_nomem(state->param, req)) {
3376                 return tevent_req_post(req, ev);
3377         }
3378
3379         /* Setup data words. */
3380         if (is_dir) {
3381                 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3382                 wire_flags |= SMB_O_DIRECTORY;
3383         }
3384
3385         SIVAL(state->data,0,0); /* No oplock. */
3386         SIVAL(state->data,4,wire_flags);
3387         SIVAL(state->data,8,unix_perms_to_wire(mode));
3388         SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
3389         SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
3390
3391         subreq = cli_trans_send(state,                  /* mem ctx. */
3392                                 ev,                     /* event ctx. */
3393                                 cli,                    /* cli_state. */
3394                                 SMBtrans2,              /* cmd. */
3395                                 NULL,                   /* pipe name. */
3396                                 -1,                     /* fid. */
3397                                 0,                      /* function. */
3398                                 0,                      /* flags. */
3399                                 &state->setup,          /* setup. */
3400                                 1,                      /* num setup uint16_t words. */
3401                                 0,                      /* max returned setup. */
3402                                 state->param,           /* param. */
3403                                 talloc_get_size(state->param),/* num param. */
3404                                 2,                      /* max returned param. */
3405                                 state->data,            /* data. */
3406                                 18,                     /* num data. */
3407                                 12);                    /* max returned data. */
3408
3409         if (tevent_req_nomem(subreq, req)) {
3410                 return tevent_req_post(req, ev);
3411         }
3412         tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
3413         return req;
3414 }
3415
3416 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
3417                                         struct event_context *ev,
3418                                         struct cli_state *cli,
3419                                         const char *fname,
3420                                         int flags,
3421                                         mode_t mode)
3422 {
3423         return cli_posix_open_internal_send(mem_ctx, ev,
3424                                 cli, fname, flags, mode, false);
3425 }
3426
3427 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
3428 {
3429         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
3430         NTSTATUS status;
3431
3432         if (tevent_req_is_nterror(req, &status)) {
3433                 return status;
3434         }
3435         *pfnum = state->fnum;
3436         return NT_STATUS_OK;
3437 }
3438
3439 /****************************************************************************
3440  Open - POSIX semantics. Doesn't request oplock.
3441 ****************************************************************************/
3442
3443 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
3444                         int flags, mode_t mode, uint16_t *pfnum)
3445 {
3446
3447         TALLOC_CTX *frame = talloc_stackframe();
3448         struct event_context *ev = NULL;
3449         struct tevent_req *req = NULL;
3450         NTSTATUS status = NT_STATUS_OK;
3451
3452         if (cli_has_async_calls(cli)) {
3453                 /*
3454                  * Can't use sync call while an async call is in flight
3455                  */
3456                 status = NT_STATUS_INVALID_PARAMETER;
3457                 goto fail;
3458         }
3459
3460         ev = event_context_init(frame);
3461         if (ev == NULL) {
3462                 status = NT_STATUS_NO_MEMORY;
3463                 goto fail;
3464         }
3465
3466         req = cli_posix_open_send(frame,
3467                                 ev,
3468                                 cli,
3469                                 fname,
3470                                 flags,
3471                                 mode);
3472         if (req == NULL) {
3473                 status = NT_STATUS_NO_MEMORY;
3474                 goto fail;
3475         }
3476
3477         if (!tevent_req_poll(req, ev)) {
3478                 status = map_nt_error_from_unix(errno);
3479                 goto fail;
3480         }
3481
3482         status = cli_posix_open_recv(req, pfnum);
3483
3484  fail:
3485         TALLOC_FREE(frame);
3486         if (!NT_STATUS_IS_OK(status)) {
3487                 cli_set_error(cli, status);
3488         }
3489         return status;
3490 }
3491
3492 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
3493                                         struct event_context *ev,
3494                                         struct cli_state *cli,
3495                                         const char *fname,
3496                                         mode_t mode)
3497 {
3498         return cli_posix_open_internal_send(mem_ctx, ev,
3499                                 cli, fname, O_CREAT, mode, true);
3500 }
3501
3502 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
3503 {
3504         NTSTATUS status;
3505
3506         if (tevent_req_is_nterror(req, &status)) {
3507                 return status;
3508         }
3509         return NT_STATUS_OK;
3510 }
3511
3512 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
3513 {
3514         TALLOC_CTX *frame = talloc_stackframe();
3515         struct event_context *ev = NULL;
3516         struct tevent_req *req = NULL;
3517         NTSTATUS status = NT_STATUS_OK;
3518
3519         if (cli_has_async_calls(cli)) {
3520                 /*
3521                  * Can't use sync call while an async call is in flight
3522                  */
3523                 status = NT_STATUS_INVALID_PARAMETER;
3524                 goto fail;
3525         }
3526
3527         ev = event_context_init(frame);
3528         if (ev == NULL) {
3529                 status = NT_STATUS_NO_MEMORY;
3530                 goto fail;
3531         }
3532
3533         req = cli_posix_mkdir_send(frame,
3534                                 ev,
3535                                 cli,
3536                                 fname,
3537                                 mode);
3538         if (req == NULL) {
3539                 status = NT_STATUS_NO_MEMORY;
3540                 goto fail;
3541         }
3542
3543         if (!tevent_req_poll(req, ev)) {
3544                 status = map_nt_error_from_unix(errno);
3545                 goto fail;
3546         }
3547
3548         status = cli_posix_mkdir_recv(req);
3549
3550  fail:
3551         TALLOC_FREE(frame);
3552         if (!NT_STATUS_IS_OK(status)) {
3553                 cli_set_error(cli, status);
3554         }
3555         return status;
3556 }
3557
3558 /****************************************************************************
3559  unlink or rmdir - POSIX semantics.
3560 ****************************************************************************/
3561
3562 struct unlink_state {
3563         uint16_t setup;
3564         uint8_t data[2];
3565 };
3566
3567 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3568 {
3569         struct tevent_req *req = tevent_req_callback_data(
3570                                 subreq, struct tevent_req);
3571         struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3572         NTSTATUS status;
3573
3574         status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3575         TALLOC_FREE(subreq);
3576         if (!NT_STATUS_IS_OK(status)) {
3577                 tevent_req_nterror(req, status);
3578                 return;
3579         }
3580         tevent_req_done(req);
3581 }
3582
3583 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3584                                         struct event_context *ev,
3585                                         struct cli_state *cli,
3586                                         const char *fname,
3587                                         bool is_dir)
3588 {
3589         struct tevent_req *req = NULL, *subreq = NULL;
3590         struct unlink_state *state = NULL;
3591         uint8_t *param = NULL;
3592
3593         req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3594         if (req == NULL) {
3595                 return NULL;
3596         }
3597
3598         /* Setup setup word. */
3599         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
3600
3601         /* Setup param array. */
3602         param = talloc_array(state, uint8_t, 6);
3603         if (tevent_req_nomem(param, req)) {
3604                 return tevent_req_post(req, ev);
3605         }
3606         memset(param, '\0', 6);
3607         SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3608
3609         param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
3610                                    strlen(fname)+1, NULL);
3611
3612         if (tevent_req_nomem(param, req)) {
3613                 return tevent_req_post(req, ev);
3614         }
3615
3616         /* Setup data word. */
3617         SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3618                         SMB_POSIX_UNLINK_FILE_TARGET);
3619
3620         subreq = cli_trans_send(state,                  /* mem ctx. */
3621                                 ev,                     /* event ctx. */
3622                                 cli,                    /* cli_state. */
3623                                 SMBtrans2,              /* cmd. */
3624                                 NULL,                   /* pipe name. */
3625                                 -1,                     /* fid. */
3626                                 0,                      /* function. */
3627                                 0,                      /* flags. */
3628                                 &state->setup,          /* setup. */
3629                                 1,                      /* num setup uint16_t words. */
3630                                 0,                      /* max returned setup. */
3631                                 param,                  /* param. */
3632                                 talloc_get_size(param), /* num param. */
3633                                 2,                      /* max returned param. */
3634                                 state->data,            /* data. */
3635                                 2,                      /* num data. */
3636                                 0);                     /* max returned data. */
3637
3638         if (tevent_req_nomem(subreq, req)) {
3639                 return tevent_req_post(req, ev);
3640         }
3641         tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3642         return req;
3643 }
3644
3645 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3646                                         struct event_context *ev,
3647                                         struct cli_state *cli,
3648                                         const char *fname)
3649 {
3650         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3651 }
3652
3653 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
3654 {
3655         NTSTATUS status;
3656
3657         if (tevent_req_is_nterror(req, &status)) {
3658                 return status;
3659         }
3660         return NT_STATUS_OK;
3661 }
3662
3663 /****************************************************************************
3664  unlink - POSIX semantics.
3665 ****************************************************************************/
3666
3667 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3668 {
3669         TALLOC_CTX *frame = talloc_stackframe();
3670         struct event_context *ev = NULL;
3671         struct tevent_req *req = NULL;
3672         NTSTATUS status = NT_STATUS_OK;
3673
3674         if (cli_has_async_calls(cli)) {
3675                 /*
3676                  * Can't use sync call while an async call is in flight
3677                  */
3678                 status = NT_STATUS_INVALID_PARAMETER;
3679                 goto fail;
3680         }
3681
3682         ev = event_context_init(frame);
3683         if (ev == NULL) {
3684                 status = NT_STATUS_NO_MEMORY;
3685                 goto fail;
3686         }
3687
3688         req = cli_posix_unlink_send(frame,
3689                                 ev,
3690                                 cli,
3691                                 fname);
3692         if (req == NULL) {
3693                 status = NT_STATUS_NO_MEMORY;
3694                 goto fail;
3695         }
3696
3697         if (!tevent_req_poll(req, ev)) {
3698                 status = map_nt_error_from_unix(errno);
3699                 goto fail;
3700         }
3701
3702         status = cli_posix_unlink_recv(req);
3703
3704  fail:
3705         TALLOC_FREE(frame);
3706         if (!NT_STATUS_IS_OK(status)) {
3707                 cli_set_error(cli, status);
3708         }
3709         return status;
3710 }
3711
3712 /****************************************************************************
3713  rmdir - POSIX semantics.
3714 ****************************************************************************/
3715
3716 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3717                                         struct event_context *ev,
3718                                         struct cli_state *cli,
3719                                         const char *fname)
3720 {
3721         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3722 }
3723
3724 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3725 {
3726         NTSTATUS status;
3727
3728         if (tevent_req_is_nterror(req, &status)) {
3729                 return status;
3730         }
3731         return NT_STATUS_OK;
3732 }
3733
3734 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3735 {
3736         TALLOC_CTX *frame = talloc_stackframe();
3737         struct event_context *ev = NULL;
3738         struct tevent_req *req = NULL;
3739         NTSTATUS status = NT_STATUS_OK;
3740
3741         if (cli_has_async_calls(cli)) {
3742                 /*
3743                  * Can't use sync call while an async call is in flight
3744                  */
3745                 status = NT_STATUS_INVALID_PARAMETER;
3746                 goto fail;
3747         }
3748
3749         ev = event_context_init(frame);
3750         if (ev == NULL) {
3751                 status = NT_STATUS_NO_MEMORY;
3752                 goto fail;
3753         }
3754
3755         req = cli_posix_rmdir_send(frame,
3756                                 ev,
3757                                 cli,
3758                                 fname);
3759         if (req == NULL) {
3760                 status = NT_STATUS_NO_MEMORY;
3761                 goto fail;
3762         }
3763
3764         if (!tevent_req_poll(req, ev)) {
3765                 status = map_nt_error_from_unix(errno);
3766                 goto fail;
3767         }
3768
3769         status = cli_posix_rmdir_recv(req, frame);
3770
3771  fail:
3772         TALLOC_FREE(frame);
3773         if (!NT_STATUS_IS_OK(status)) {
3774                 cli_set_error(cli, status);
3775         }
3776         return status;
3777 }