7f562a8061a2d462ef605632fc2306c523efcd10
[mat/samba.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 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
31
32 /***********************************************************
33  Common function for pushing stings, used by smb_bytes_push_str()
34  and trans_bytes_push_str(). Only difference is the align_odd
35  parameter setting.
36 ***********************************************************/
37
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39                                 const char *str, size_t str_len,
40                                 bool align_odd,
41                                 size_t *pconverted_size)
42 {
43         size_t buflen;
44         char *converted;
45         size_t converted_size;
46
47         if (buf == NULL) {
48                 return NULL;
49         }
50
51         buflen = talloc_get_size(buf);
52
53         if (ucs2 &&
54             ((align_odd && (buflen % 2 == 0)) ||
55              (!align_odd && (buflen % 2 == 1)))) {
56                 /*
57                  * We're pushing into an SMB buffer, align odd
58                  */
59                 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60                 if (buf == NULL) {
61                         return NULL;
62                 }
63                 buf[buflen] = '\0';
64                 buflen += 1;
65         }
66
67         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68                                    ucs2 ? CH_UTF16LE : CH_DOS,
69                                    str, str_len, &converted,
70                                    &converted_size)) {
71                 return NULL;
72         }
73
74         buf = talloc_realloc(NULL, buf, uint8_t,
75                                    buflen + converted_size);
76         if (buf == NULL) {
77                 TALLOC_FREE(converted);
78                 return NULL;
79         }
80
81         memcpy(buf + buflen, converted, converted_size);
82
83         TALLOC_FREE(converted);
84
85         if (pconverted_size) {
86                 *pconverted_size = converted_size;
87         }
88
89         return buf;
90 }
91
92 /***********************************************************
93  Push a string into an SMB buffer, with odd byte alignment
94  if it's a UCS2 string.
95 ***********************************************************/
96
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98                             const char *str, size_t str_len,
99                             size_t *pconverted_size)
100 {
101         return internal_bytes_push_str(buf, ucs2, str, str_len,
102                         true, pconverted_size);
103 }
104
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106                               const uint8_t *bytes, size_t num_bytes)
107 {
108         size_t buflen;
109
110         if (buf == NULL) {
111                 return NULL;
112         }
113         buflen = talloc_get_size(buf);
114
115         buf = talloc_realloc(NULL, buf, uint8_t,
116                                    buflen + 1 + num_bytes);
117         if (buf == NULL) {
118                 return NULL;
119         }
120         buf[buflen] = prefix;
121         memcpy(&buf[buflen+1], bytes, num_bytes);
122         return buf;
123 }
124
125 /***********************************************************
126  Same as smb_bytes_push_str(), but without the odd byte
127  align for ucs2 (we're pushing into a param or data block).
128  static for now, although this will probably change when
129  other modules use async trans calls.
130 ***********************************************************/
131
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133                                const char *str, size_t str_len,
134                                size_t *pconverted_size)
135 {
136         return internal_bytes_push_str(buf, ucs2, str, str_len,
137                         false, pconverted_size);
138 }
139
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141                                  const uint8_t *bytes, size_t num_bytes)
142 {
143         size_t buflen;
144
145         if (buf == NULL) {
146                 return NULL;
147         }
148         buflen = talloc_get_size(buf);
149
150         buf = talloc_realloc(NULL, buf, uint8_t,
151                              buflen + num_bytes);
152         if (buf == NULL) {
153                 return NULL;
154         }
155         memcpy(&buf[buflen], bytes, num_bytes);
156         return buf;
157 }
158
159 struct cli_setpathinfo_state {
160         uint16_t setup;
161         uint8_t *param;
162 };
163
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
165
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167                                         struct tevent_context *ev,
168                                         struct cli_state *cli,
169                                         uint16_t level,
170                                         const char *path,
171                                         uint8_t *data,
172                                         size_t data_len)
173 {
174         struct tevent_req *req, *subreq;
175         struct cli_setpathinfo_state *state;
176
177         req = tevent_req_create(mem_ctx, &state,
178                                 struct cli_setpathinfo_state);
179         if (req == NULL) {
180                 return NULL;
181         }
182
183         /* Setup setup word. */
184         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
185
186         /* Setup param array. */
187         state->param = talloc_zero_array(state, uint8_t, 6);
188         if (tevent_req_nomem(state->param, req)) {
189                 return tevent_req_post(req, ev);
190         }
191         SSVAL(state->param, 0, level);
192
193         state->param = trans2_bytes_push_str(
194                 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195         if (tevent_req_nomem(state->param, req)) {
196                 return tevent_req_post(req, ev);
197         }
198
199         subreq = cli_trans_send(
200                 state,                  /* mem ctx. */
201                 ev,                     /* event ctx. */
202                 cli,                    /* cli_state. */
203                 SMBtrans2,              /* cmd. */
204                 NULL,                   /* pipe name. */
205                 -1,                     /* fid. */
206                 0,                      /* function. */
207                 0,                      /* flags. */
208                 &state->setup,          /* setup. */
209                 1,                      /* num setup uint16_t words. */
210                 0,                      /* max returned setup. */
211                 state->param,           /* param. */
212                 talloc_get_size(state->param),  /* num param. */
213                 2,                      /* max returned param. */
214                 data,                   /* data. */
215                 data_len,               /* num data. */
216                 0);                     /* max returned data. */
217
218         if (tevent_req_nomem(subreq, req)) {
219                 return tevent_req_post(req, ev);
220         }
221         tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222         return req;
223 }
224
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
226 {
227         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228                                          NULL, 0, NULL, NULL, 0, NULL);
229         tevent_req_simple_finish_ntstatus(subreq, status);
230 }
231
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
233 {
234         return tevent_req_simple_recv_ntstatus(req);
235 }
236
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238                          uint16_t level,
239                          const char *path,
240                          uint8_t *data,
241                          size_t data_len)
242 {
243         TALLOC_CTX *frame = talloc_stackframe();
244         struct tevent_context *ev;
245         struct tevent_req *req;
246         NTSTATUS status = NT_STATUS_NO_MEMORY;
247
248         if (smbXcli_conn_has_async_calls(cli->conn)) {
249                 /*
250                  * Can't use sync call while an async call is in flight
251                  */
252                 status = NT_STATUS_INVALID_PARAMETER;
253                 goto fail;
254         }
255         ev = samba_tevent_context_init(frame);
256         if (ev == NULL) {
257                 goto fail;
258         }
259         req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260         if (req == NULL) {
261                 goto fail;
262         }
263         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264                 goto fail;
265         }
266         status = cli_setpathinfo_recv(req);
267  fail:
268         TALLOC_FREE(frame);
269         return status;
270 }
271
272 /****************************************************************************
273  Hard/Symlink a file (UNIX extensions).
274  Creates new name (sym)linked to oldname.
275 ****************************************************************************/
276
277 struct cli_posix_link_internal_state {
278         uint8_t *data;
279 };
280
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
282
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284                                         struct tevent_context *ev,
285                                         struct cli_state *cli,
286                                         uint16_t level,
287                                         const char *oldname,
288                                         const char *newname)
289 {
290         struct tevent_req *req = NULL, *subreq = NULL;
291         struct cli_posix_link_internal_state *state = NULL;
292
293         req = tevent_req_create(mem_ctx, &state,
294                                 struct cli_posix_link_internal_state);
295         if (req == NULL) {
296                 return NULL;
297         }
298
299         /* Setup data array. */
300         state->data = talloc_array(state, uint8_t, 0);
301         if (tevent_req_nomem(state->data, req)) {
302                 return tevent_req_post(req, ev);
303         }
304         state->data = trans2_bytes_push_str(
305                 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
306
307         subreq = cli_setpathinfo_send(
308                 state, ev, cli, level, newname,
309                 state->data, talloc_get_size(state->data));
310         if (tevent_req_nomem(subreq, req)) {
311                 return tevent_req_post(req, ev);
312         }
313         tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314         return req;
315 }
316
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
318 {
319         NTSTATUS status = cli_setpathinfo_recv(subreq);
320         tevent_req_simple_finish_ntstatus(subreq, status);
321 }
322
323 /****************************************************************************
324  Symlink a file (UNIX extensions).
325 ****************************************************************************/
326
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328                                         struct tevent_context *ev,
329                                         struct cli_state *cli,
330                                         const char *oldname,
331                                         const char *newname)
332 {
333         return cli_posix_link_internal_send(
334                 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
335 }
336
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
338 {
339         return tevent_req_simple_recv_ntstatus(req);
340 }
341
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343                         const char *oldname,
344                         const char *newname)
345 {
346         TALLOC_CTX *frame = talloc_stackframe();
347         struct tevent_context *ev = NULL;
348         struct tevent_req *req = NULL;
349         NTSTATUS status = NT_STATUS_OK;
350
351         if (smbXcli_conn_has_async_calls(cli->conn)) {
352                 /*
353                  * Can't use sync call while an async call is in flight
354                  */
355                 status = NT_STATUS_INVALID_PARAMETER;
356                 goto fail;
357         }
358
359         ev = samba_tevent_context_init(frame);
360         if (ev == NULL) {
361                 status = NT_STATUS_NO_MEMORY;
362                 goto fail;
363         }
364
365         req = cli_posix_symlink_send(frame,
366                                 ev,
367                                 cli,
368                                 oldname,
369                                 newname);
370         if (req == NULL) {
371                 status = NT_STATUS_NO_MEMORY;
372                 goto fail;
373         }
374
375         if (!tevent_req_poll(req, ev)) {
376                 status = map_nt_error_from_unix(errno);
377                 goto fail;
378         }
379
380         status = cli_posix_symlink_recv(req);
381
382  fail:
383         TALLOC_FREE(frame);
384         return status;
385 }
386
387 /****************************************************************************
388  Read a POSIX symlink.
389 ****************************************************************************/
390
391 struct readlink_state {
392         uint8_t *data;
393         uint32_t num_data;
394 };
395
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
397
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399                                         struct tevent_context *ev,
400                                         struct cli_state *cli,
401                                         const char *fname,
402                                         size_t len)
403 {
404         struct tevent_req *req = NULL, *subreq = NULL;
405         struct readlink_state *state = NULL;
406         uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
407
408         req = tevent_req_create(mem_ctx, &state, struct readlink_state);
409         if (req == NULL) {
410                 return NULL;
411         }
412
413         /*
414          * Len is in bytes, we need it in UCS2 units.
415          */
416         if ((2*len < len) || (maxbytelen < len)) {
417                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418                 return tevent_req_post(req, ev);
419         }
420
421         subreq = cli_qpathinfo_send(state, ev, cli, fname,
422                                     SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423         if (tevent_req_nomem(subreq, req)) {
424                 return tevent_req_post(req, ev);
425         }
426         tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
427         return req;
428 }
429
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
431 {
432         struct tevent_req *req = tevent_req_callback_data(
433                 subreq, struct tevent_req);
434         struct readlink_state *state = tevent_req_data(
435                 req, struct readlink_state);
436         NTSTATUS status;
437
438         status = cli_qpathinfo_recv(subreq, state, &state->data,
439                                     &state->num_data);
440         TALLOC_FREE(subreq);
441         if (tevent_req_nterror(req, status)) {
442                 return;
443         }
444         /*
445          * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446          */
447         if (state->data[state->num_data-1] != '\0') {
448                 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
449                 return;
450         }
451         tevent_req_done(req);
452 }
453
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455                                 char *retpath, size_t len)
456 {
457         NTSTATUS status;
458         char *converted = NULL;
459         size_t converted_size = 0;
460         struct readlink_state *state = tevent_req_data(req, struct readlink_state);
461
462         if (tevent_req_is_nterror(req, &status)) {
463                 return status;
464         }
465         /* The returned data is a pushed string, not raw data. */
466         if (!convert_string_talloc(state,
467                                 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS, 
468                                 CH_UNIX,
469                                 state->data,
470                                 state->num_data,
471                                 &converted,
472                                 &converted_size)) {
473                 return NT_STATUS_NO_MEMORY;
474         }
475
476         len = MIN(len,converted_size);
477         if (len == 0) {
478                 return NT_STATUS_DATA_ERROR;
479         }
480         memcpy(retpath, converted, len);
481         return NT_STATUS_OK;
482 }
483
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485                                 char *linkpath, size_t len)
486 {
487         TALLOC_CTX *frame = talloc_stackframe();
488         struct tevent_context *ev = NULL;
489         struct tevent_req *req = NULL;
490         NTSTATUS status = NT_STATUS_OK;
491
492         if (smbXcli_conn_has_async_calls(cli->conn)) {
493                 /*
494                  * Can't use sync call while an async call is in flight
495                  */
496                 status = NT_STATUS_INVALID_PARAMETER;
497                 goto fail;
498         }
499
500         ev = samba_tevent_context_init(frame);
501         if (ev == NULL) {
502                 status = NT_STATUS_NO_MEMORY;
503                 goto fail;
504         }
505
506         req = cli_posix_readlink_send(frame,
507                                 ev,
508                                 cli,
509                                 fname,
510                                 len);
511         if (req == NULL) {
512                 status = NT_STATUS_NO_MEMORY;
513                 goto fail;
514         }
515
516         if (!tevent_req_poll(req, ev)) {
517                 status = map_nt_error_from_unix(errno);
518                 goto fail;
519         }
520
521         status = cli_posix_readlink_recv(req, cli, linkpath, len);
522
523  fail:
524         TALLOC_FREE(frame);
525         return status;
526 }
527
528 /****************************************************************************
529  Hard link a file (UNIX extensions).
530 ****************************************************************************/
531
532 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
533                                         struct tevent_context *ev,
534                                         struct cli_state *cli,
535                                         const char *oldname,
536                                         const char *newname)
537 {
538         return cli_posix_link_internal_send(
539                 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
540 }
541
542 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
543 {
544         return tevent_req_simple_recv_ntstatus(req);
545 }
546
547 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
548                         const char *oldname,
549                         const char *newname)
550 {
551         TALLOC_CTX *frame = talloc_stackframe();
552         struct tevent_context *ev = NULL;
553         struct tevent_req *req = NULL;
554         NTSTATUS status = NT_STATUS_OK;
555
556         if (smbXcli_conn_has_async_calls(cli->conn)) {
557                 /*
558                  * Can't use sync call while an async call is in flight
559                  */
560                 status = NT_STATUS_INVALID_PARAMETER;
561                 goto fail;
562         }
563
564         ev = samba_tevent_context_init(frame);
565         if (ev == NULL) {
566                 status = NT_STATUS_NO_MEMORY;
567                 goto fail;
568         }
569
570         req = cli_posix_hardlink_send(frame,
571                                 ev,
572                                 cli,
573                                 oldname,
574                                 newname);
575         if (req == NULL) {
576                 status = NT_STATUS_NO_MEMORY;
577                 goto fail;
578         }
579
580         if (!tevent_req_poll(req, ev)) {
581                 status = map_nt_error_from_unix(errno);
582                 goto fail;
583         }
584
585         status = cli_posix_hardlink_recv(req);
586
587  fail:
588         TALLOC_FREE(frame);
589         return status;
590 }
591
592 /****************************************************************************
593  Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
595
596 struct getfacl_state {
597         uint32_t num_data;
598         uint8_t *data;
599 };
600
601 static void cli_posix_getfacl_done(struct tevent_req *subreq);
602
603 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
604                                         struct tevent_context *ev,
605                                         struct cli_state *cli,
606                                         const char *fname)
607 {
608         struct tevent_req *req = NULL, *subreq = NULL;
609         struct getfacl_state *state = NULL;
610
611         req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612         if (req == NULL) {
613                 return NULL;
614         }
615         subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616                                     0, CLI_BUFFER_SIZE);
617         if (tevent_req_nomem(subreq, req)) {
618                 return tevent_req_post(req, ev);
619         }
620         tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621         return req;
622 }
623
624 static void cli_posix_getfacl_done(struct tevent_req *subreq)
625 {
626         struct tevent_req *req = tevent_req_callback_data(
627                 subreq, struct tevent_req);
628         struct getfacl_state *state = tevent_req_data(
629                 req, struct getfacl_state);
630         NTSTATUS status;
631
632         status = cli_qpathinfo_recv(subreq, state, &state->data,
633                                     &state->num_data);
634         TALLOC_FREE(subreq);
635         if (tevent_req_nterror(req, status)) {
636                 return;
637         }
638         tevent_req_done(req);
639 }
640
641 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
642                                 TALLOC_CTX *mem_ctx,
643                                 size_t *prb_size,
644                                 char **retbuf)
645 {
646         struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
647         NTSTATUS status;
648
649         if (tevent_req_is_nterror(req, &status)) {
650                 return status;
651         }
652         *prb_size = (size_t)state->num_data;
653         *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654         return NT_STATUS_OK;
655 }
656
657 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
658                         const char *fname,
659                         TALLOC_CTX *mem_ctx,
660                         size_t *prb_size,
661                         char **retbuf)
662 {
663         TALLOC_CTX *frame = talloc_stackframe();
664         struct tevent_context *ev = NULL;
665         struct tevent_req *req = NULL;
666         NTSTATUS status = NT_STATUS_OK;
667
668         if (smbXcli_conn_has_async_calls(cli->conn)) {
669                 /*
670                  * Can't use sync call while an async call is in flight
671                  */
672                 status = NT_STATUS_INVALID_PARAMETER;
673                 goto fail;
674         }
675
676         ev = samba_tevent_context_init(frame);
677         if (ev == NULL) {
678                 status = NT_STATUS_NO_MEMORY;
679                 goto fail;
680         }
681
682         req = cli_posix_getfacl_send(frame,
683                                 ev,
684                                 cli,
685                                 fname);
686         if (req == NULL) {
687                 status = NT_STATUS_NO_MEMORY;
688                 goto fail;
689         }
690
691         if (!tevent_req_poll(req, ev)) {
692                 status = map_nt_error_from_unix(errno);
693                 goto fail;
694         }
695
696         status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
697
698  fail:
699         TALLOC_FREE(frame);
700         return status;
701 }
702
703 /****************************************************************************
704  Stat a file (UNIX extensions).
705 ****************************************************************************/
706
707 struct stat_state {
708         uint32_t num_data;
709         uint8_t *data;
710 };
711
712 static void cli_posix_stat_done(struct tevent_req *subreq);
713
714 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
715                                         struct tevent_context *ev,
716                                         struct cli_state *cli,
717                                         const char *fname)
718 {
719         struct tevent_req *req = NULL, *subreq = NULL;
720         struct stat_state *state = NULL;
721
722         req = tevent_req_create(mem_ctx, &state, struct stat_state);
723         if (req == NULL) {
724                 return NULL;
725         }
726         subreq = cli_qpathinfo_send(state, ev, cli, fname,
727                                     SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
728         if (tevent_req_nomem(subreq, req)) {
729                 return tevent_req_post(req, ev);
730         }
731         tevent_req_set_callback(subreq, cli_posix_stat_done, req);
732         return req;
733 }
734
735 static void cli_posix_stat_done(struct tevent_req *subreq)
736 {
737         struct tevent_req *req = tevent_req_callback_data(
738                                 subreq, struct tevent_req);
739         struct stat_state *state = tevent_req_data(req, struct stat_state);
740         NTSTATUS status;
741
742         status = cli_qpathinfo_recv(subreq, state, &state->data,
743                                     &state->num_data);
744         TALLOC_FREE(subreq);
745         if (tevent_req_nterror(req, status)) {
746                 return;
747         }
748         tevent_req_done(req);
749 }
750
751 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
752                                 SMB_STRUCT_STAT *sbuf)
753 {
754         struct stat_state *state = tevent_req_data(req, struct stat_state);
755         NTSTATUS status;
756
757         if (tevent_req_is_nterror(req, &status)) {
758                 return status;
759         }
760
761         sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0);     /* total size, in bytes */
762         sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8);   /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764         sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 #else
766         /* assume 512 byte blocks */
767         sbuf->st_ex_blocks /= 512;
768 #endif
769         sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16));    /* time of last change */
770         sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24));    /* time of last access */
771         sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32));    /* time of last modification */
772
773         sbuf->st_ex_uid = (uid_t) IVAL(state->data,40);      /* user ID of owner */
774         sbuf->st_ex_gid = (gid_t) IVAL(state->data,48);      /* group ID of owner */
775         sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
776 #if defined(HAVE_MAKEDEV)
777         {
778                 uint32_t dev_major = IVAL(state->data,60);
779                 uint32_t dev_minor = IVAL(state->data,68);
780                 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
781         }
782 #endif
783         sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76);      /* inode */
784         sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84));     /* protection */
785         sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
786
787         return NT_STATUS_OK;
788 }
789
790 NTSTATUS cli_posix_stat(struct cli_state *cli,
791                         const char *fname,
792                         SMB_STRUCT_STAT *sbuf)
793 {
794         TALLOC_CTX *frame = talloc_stackframe();
795         struct tevent_context *ev = NULL;
796         struct tevent_req *req = NULL;
797         NTSTATUS status = NT_STATUS_OK;
798
799         if (smbXcli_conn_has_async_calls(cli->conn)) {
800                 /*
801                  * Can't use sync call while an async call is in flight
802                  */
803                 status = NT_STATUS_INVALID_PARAMETER;
804                 goto fail;
805         }
806
807         ev = samba_tevent_context_init(frame);
808         if (ev == NULL) {
809                 status = NT_STATUS_NO_MEMORY;
810                 goto fail;
811         }
812
813         req = cli_posix_stat_send(frame,
814                                 ev,
815                                 cli,
816                                 fname);
817         if (req == NULL) {
818                 status = NT_STATUS_NO_MEMORY;
819                 goto fail;
820         }
821
822         if (!tevent_req_poll(req, ev)) {
823                 status = map_nt_error_from_unix(errno);
824                 goto fail;
825         }
826
827         status = cli_posix_stat_recv(req, sbuf);
828
829  fail:
830         TALLOC_FREE(frame);
831         return status;
832 }
833
834 /****************************************************************************
835  Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
837
838 struct cli_posix_chown_chmod_internal_state {
839         uint8_t data[100];
840 };
841
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
843
844 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
845                                         struct tevent_context *ev,
846                                         struct cli_state *cli,
847                                         const char *fname,
848                                         uint32_t mode,
849                                         uint32_t uid,
850                                         uint32_t gid)
851 {
852         struct tevent_req *req = NULL, *subreq = NULL;
853         struct cli_posix_chown_chmod_internal_state *state = NULL;
854
855         req = tevent_req_create(mem_ctx, &state,
856                                 struct cli_posix_chown_chmod_internal_state);
857         if (req == NULL) {
858                 return NULL;
859         }
860
861         memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
862         memset(&state->data[40], '\0', 60);
863         SIVAL(state->data,40,uid);
864         SIVAL(state->data,48,gid);
865         SIVAL(state->data,84,mode);
866
867         subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
868                                       fname, state->data, sizeof(state->data));
869         if (tevent_req_nomem(subreq, req)) {
870                 return tevent_req_post(req, ev);
871         }
872         tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
873                                 req);
874         return req;
875 }
876
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
878 {
879         NTSTATUS status = cli_setpathinfo_recv(subreq);
880         tevent_req_simple_finish_ntstatus(subreq, status);
881 }
882
883 /****************************************************************************
884  chmod a file (UNIX extensions).
885 ****************************************************************************/
886
887 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
888                                         struct tevent_context *ev,
889                                         struct cli_state *cli,
890                                         const char *fname,
891                                         mode_t mode)
892 {
893         return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894                         fname,
895                         unix_perms_to_wire(mode),
896                         SMB_UID_NO_CHANGE,
897                         SMB_GID_NO_CHANGE);
898 }
899
900 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
901 {
902         return tevent_req_simple_recv_ntstatus(req);
903 }
904
905 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
906 {
907         TALLOC_CTX *frame = talloc_stackframe();
908         struct tevent_context *ev = NULL;
909         struct tevent_req *req = NULL;
910         NTSTATUS status = NT_STATUS_OK;
911
912         if (smbXcli_conn_has_async_calls(cli->conn)) {
913                 /*
914                  * Can't use sync call while an async call is in flight
915                  */
916                 status = NT_STATUS_INVALID_PARAMETER;
917                 goto fail;
918         }
919
920         ev = samba_tevent_context_init(frame);
921         if (ev == NULL) {
922                 status = NT_STATUS_NO_MEMORY;
923                 goto fail;
924         }
925
926         req = cli_posix_chmod_send(frame,
927                                 ev,
928                                 cli,
929                                 fname,
930                                 mode);
931         if (req == NULL) {
932                 status = NT_STATUS_NO_MEMORY;
933                 goto fail;
934         }
935
936         if (!tevent_req_poll(req, ev)) {
937                 status = map_nt_error_from_unix(errno);
938                 goto fail;
939         }
940
941         status = cli_posix_chmod_recv(req);
942
943  fail:
944         TALLOC_FREE(frame);
945         return status;
946 }
947
948 /****************************************************************************
949  chown a file (UNIX extensions).
950 ****************************************************************************/
951
952 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
953                                         struct tevent_context *ev,
954                                         struct cli_state *cli,
955                                         const char *fname,
956                                         uid_t uid,
957                                         gid_t gid)
958 {
959         return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960                         fname,
961                         SMB_MODE_NO_CHANGE,
962                         (uint32_t)uid,
963                         (uint32_t)gid);
964 }
965
966 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
967 {
968         return tevent_req_simple_recv_ntstatus(req);
969 }
970
971 NTSTATUS cli_posix_chown(struct cli_state *cli,
972                         const char *fname,
973                         uid_t uid,
974                         gid_t gid)
975 {
976         TALLOC_CTX *frame = talloc_stackframe();
977         struct tevent_context *ev = NULL;
978         struct tevent_req *req = NULL;
979         NTSTATUS status = NT_STATUS_OK;
980
981         if (smbXcli_conn_has_async_calls(cli->conn)) {
982                 /*
983                  * Can't use sync call while an async call is in flight
984                  */
985                 status = NT_STATUS_INVALID_PARAMETER;
986                 goto fail;
987         }
988
989         ev = samba_tevent_context_init(frame);
990         if (ev == NULL) {
991                 status = NT_STATUS_NO_MEMORY;
992                 goto fail;
993         }
994
995         req = cli_posix_chown_send(frame,
996                                 ev,
997                                 cli,
998                                 fname,
999                                 uid,
1000                                 gid);
1001         if (req == NULL) {
1002                 status = NT_STATUS_NO_MEMORY;
1003                 goto fail;
1004         }
1005
1006         if (!tevent_req_poll(req, ev)) {
1007                 status = map_nt_error_from_unix(errno);
1008                 goto fail;
1009         }
1010
1011         status = cli_posix_chown_recv(req);
1012
1013  fail:
1014         TALLOC_FREE(frame);
1015         return status;
1016 }
1017
1018 /****************************************************************************
1019  Rename a file.
1020 ****************************************************************************/
1021
1022 static void cli_rename_done(struct tevent_req *subreq);
1023
1024 struct cli_rename_state {
1025         uint16_t vwv[1];
1026 };
1027
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029                                 struct tevent_context *ev,
1030                                 struct cli_state *cli,
1031                                 const char *fname_src,
1032                                 const char *fname_dst)
1033 {
1034         struct tevent_req *req = NULL, *subreq = NULL;
1035         struct cli_rename_state *state = NULL;
1036         uint8_t additional_flags = 0;
1037         uint8_t *bytes = NULL;
1038
1039         req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1040         if (req == NULL) {
1041                 return NULL;
1042         }
1043
1044         SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1045
1046         bytes = talloc_array(state, uint8_t, 1);
1047         if (tevent_req_nomem(bytes, req)) {
1048                 return tevent_req_post(req, ev);
1049         }
1050         bytes[0] = 4;
1051         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1052                                    strlen(fname_src)+1, NULL);
1053         if (tevent_req_nomem(bytes, req)) {
1054                 return tevent_req_post(req, ev);
1055         }
1056
1057         bytes = talloc_realloc(state, bytes, uint8_t,
1058                         talloc_get_size(bytes)+1);
1059         if (tevent_req_nomem(bytes, req)) {
1060                 return tevent_req_post(req, ev);
1061         }
1062
1063         bytes[talloc_get_size(bytes)-1] = 4;
1064         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1065                                    strlen(fname_dst)+1, NULL);
1066         if (tevent_req_nomem(bytes, req)) {
1067                 return tevent_req_post(req, ev);
1068         }
1069
1070         subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071                               1, state->vwv, talloc_get_size(bytes), bytes);
1072         if (tevent_req_nomem(subreq, req)) {
1073                 return tevent_req_post(req, ev);
1074         }
1075         tevent_req_set_callback(subreq, cli_rename_done, req);
1076         return req;
1077 }
1078
1079 static void cli_rename_done(struct tevent_req *subreq)
1080 {
1081         struct tevent_req *req = tevent_req_callback_data(
1082                                 subreq, struct tevent_req);
1083         NTSTATUS status;
1084
1085         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086         TALLOC_FREE(subreq);
1087         if (tevent_req_nterror(req, status)) {
1088                 return;
1089         }
1090         tevent_req_done(req);
1091 }
1092
1093 NTSTATUS cli_rename_recv(struct tevent_req *req)
1094 {
1095         return tevent_req_simple_recv_ntstatus(req);
1096 }
1097
1098 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1099 {
1100         TALLOC_CTX *frame = NULL;
1101         struct tevent_context *ev;
1102         struct tevent_req *req;
1103         NTSTATUS status = NT_STATUS_OK;
1104
1105         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1106                 return cli_smb2_rename(cli,
1107                                         fname_src,
1108                                         fname_dst);
1109         }
1110
1111         frame = talloc_stackframe();
1112
1113         if (smbXcli_conn_has_async_calls(cli->conn)) {
1114                 /*
1115                  * Can't use sync call while an async call is in flight
1116                  */
1117                 status = NT_STATUS_INVALID_PARAMETER;
1118                 goto fail;
1119         }
1120
1121         ev = samba_tevent_context_init(frame);
1122         if (ev == NULL) {
1123                 status = NT_STATUS_NO_MEMORY;
1124                 goto fail;
1125         }
1126
1127         req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1128         if (req == NULL) {
1129                 status = NT_STATUS_NO_MEMORY;
1130                 goto fail;
1131         }
1132
1133         if (!tevent_req_poll(req, ev)) {
1134                 status = map_nt_error_from_unix(errno);
1135                 goto fail;
1136         }
1137
1138         status = cli_rename_recv(req);
1139
1140  fail:
1141         TALLOC_FREE(frame);
1142         return status;
1143 }
1144
1145 /****************************************************************************
1146  NT Rename a file.
1147 ****************************************************************************/
1148
1149 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1150
1151 struct cli_ntrename_internal_state {
1152         uint16_t vwv[4];
1153 };
1154
1155 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1156                                 struct tevent_context *ev,
1157                                 struct cli_state *cli,
1158                                 const char *fname_src,
1159                                 const char *fname_dst,
1160                                 uint16_t rename_flag)
1161 {
1162         struct tevent_req *req = NULL, *subreq = NULL;
1163         struct cli_ntrename_internal_state *state = NULL;
1164         uint8_t additional_flags = 0;
1165         uint8_t *bytes = NULL;
1166
1167         req = tevent_req_create(mem_ctx, &state,
1168                                 struct cli_ntrename_internal_state);
1169         if (req == NULL) {
1170                 return NULL;
1171         }
1172
1173         SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1174         SSVAL(state->vwv+1, 0, rename_flag);
1175
1176         bytes = talloc_array(state, uint8_t, 1);
1177         if (tevent_req_nomem(bytes, req)) {
1178                 return tevent_req_post(req, ev);
1179         }
1180         bytes[0] = 4;
1181         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1182                                    strlen(fname_src)+1, NULL);
1183         if (tevent_req_nomem(bytes, req)) {
1184                 return tevent_req_post(req, ev);
1185         }
1186
1187         bytes = talloc_realloc(state, bytes, uint8_t,
1188                         talloc_get_size(bytes)+1);
1189         if (tevent_req_nomem(bytes, req)) {
1190                 return tevent_req_post(req, ev);
1191         }
1192
1193         bytes[talloc_get_size(bytes)-1] = 4;
1194         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1195                                    strlen(fname_dst)+1, NULL);
1196         if (tevent_req_nomem(bytes, req)) {
1197                 return tevent_req_post(req, ev);
1198         }
1199
1200         subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1201                               4, state->vwv, talloc_get_size(bytes), bytes);
1202         if (tevent_req_nomem(subreq, req)) {
1203                 return tevent_req_post(req, ev);
1204         }
1205         tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1206         return req;
1207 }
1208
1209 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1210 {
1211         struct tevent_req *req = tevent_req_callback_data(
1212                                 subreq, struct tevent_req);
1213         NTSTATUS status;
1214
1215         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1216         TALLOC_FREE(subreq);
1217         if (tevent_req_nterror(req, status)) {
1218                 return;
1219         }
1220         tevent_req_done(req);
1221 }
1222
1223 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1224 {
1225         return tevent_req_simple_recv_ntstatus(req);
1226 }
1227
1228 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1229                                 struct tevent_context *ev,
1230                                 struct cli_state *cli,
1231                                 const char *fname_src,
1232                                 const char *fname_dst)
1233 {
1234         return cli_ntrename_internal_send(mem_ctx,
1235                                           ev,
1236                                           cli,
1237                                           fname_src,
1238                                           fname_dst,
1239                                           RENAME_FLAG_RENAME);
1240 }
1241
1242 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1243 {
1244         return cli_ntrename_internal_recv(req);
1245 }
1246
1247 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1248 {
1249         TALLOC_CTX *frame = talloc_stackframe();
1250         struct tevent_context *ev;
1251         struct tevent_req *req;
1252         NTSTATUS status = NT_STATUS_OK;
1253
1254         if (smbXcli_conn_has_async_calls(cli->conn)) {
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 = samba_tevent_context_init(frame);
1263         if (ev == NULL) {
1264                 status = NT_STATUS_NO_MEMORY;
1265                 goto fail;
1266         }
1267
1268         req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1269         if (req == NULL) {
1270                 status = NT_STATUS_NO_MEMORY;
1271                 goto fail;
1272         }
1273
1274         if (!tevent_req_poll(req, ev)) {
1275                 status = map_nt_error_from_unix(errno);
1276                 goto fail;
1277         }
1278
1279         status = cli_ntrename_recv(req);
1280
1281  fail:
1282         TALLOC_FREE(frame);
1283         return status;
1284 }
1285
1286 /****************************************************************************
1287  NT hardlink a file.
1288 ****************************************************************************/
1289
1290 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1291                                 struct tevent_context *ev,
1292                                 struct cli_state *cli,
1293                                 const char *fname_src,
1294                                 const char *fname_dst)
1295 {
1296         return cli_ntrename_internal_send(mem_ctx,
1297                                           ev,
1298                                           cli,
1299                                           fname_src,
1300                                           fname_dst,
1301                                           RENAME_FLAG_HARD_LINK);
1302 }
1303
1304 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1305 {
1306         return cli_ntrename_internal_recv(req);
1307 }
1308
1309 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1310 {
1311         TALLOC_CTX *frame = talloc_stackframe();
1312         struct tevent_context *ev;
1313         struct tevent_req *req;
1314         NTSTATUS status = NT_STATUS_OK;
1315
1316         if (smbXcli_conn_has_async_calls(cli->conn)) {
1317                 /*
1318                  * Can't use sync call while an async call is in flight
1319                  */
1320                 status = NT_STATUS_INVALID_PARAMETER;
1321                 goto fail;
1322         }
1323
1324         ev = samba_tevent_context_init(frame);
1325         if (ev == NULL) {
1326                 status = NT_STATUS_NO_MEMORY;
1327                 goto fail;
1328         }
1329
1330         req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1331         if (req == NULL) {
1332                 status = NT_STATUS_NO_MEMORY;
1333                 goto fail;
1334         }
1335
1336         if (!tevent_req_poll(req, ev)) {
1337                 status = map_nt_error_from_unix(errno);
1338                 goto fail;
1339         }
1340
1341         status = cli_nt_hardlink_recv(req);
1342
1343  fail:
1344         TALLOC_FREE(frame);
1345         return status;
1346 }
1347
1348 /****************************************************************************
1349  Delete a file.
1350 ****************************************************************************/
1351
1352 static void cli_unlink_done(struct tevent_req *subreq);
1353
1354 struct cli_unlink_state {
1355         uint16_t vwv[1];
1356 };
1357
1358 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1359                                 struct tevent_context *ev,
1360                                 struct cli_state *cli,
1361                                 const char *fname,
1362                                 uint16_t mayhave_attrs)
1363 {
1364         struct tevent_req *req = NULL, *subreq = NULL;
1365         struct cli_unlink_state *state = NULL;
1366         uint8_t additional_flags = 0;
1367         uint8_t *bytes = NULL;
1368
1369         req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1370         if (req == NULL) {
1371                 return NULL;
1372         }
1373
1374         SSVAL(state->vwv+0, 0, mayhave_attrs);
1375
1376         bytes = talloc_array(state, uint8_t, 1);
1377         if (tevent_req_nomem(bytes, req)) {
1378                 return tevent_req_post(req, ev);
1379         }
1380         bytes[0] = 4;
1381         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1382                                    strlen(fname)+1, NULL);
1383
1384         if (tevent_req_nomem(bytes, req)) {
1385                 return tevent_req_post(req, ev);
1386         }
1387
1388         subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1389                                 1, state->vwv, talloc_get_size(bytes), bytes);
1390         if (tevent_req_nomem(subreq, req)) {
1391                 return tevent_req_post(req, ev);
1392         }
1393         tevent_req_set_callback(subreq, cli_unlink_done, req);
1394         return req;
1395 }
1396
1397 static void cli_unlink_done(struct tevent_req *subreq)
1398 {
1399         struct tevent_req *req = tevent_req_callback_data(
1400                 subreq, struct tevent_req);
1401         NTSTATUS status;
1402
1403         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1404         TALLOC_FREE(subreq);
1405         if (tevent_req_nterror(req, status)) {
1406                 return;
1407         }
1408         tevent_req_done(req);
1409 }
1410
1411 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1412 {
1413         return tevent_req_simple_recv_ntstatus(req);
1414 }
1415
1416 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1417 {
1418         TALLOC_CTX *frame = NULL;
1419         struct tevent_context *ev;
1420         struct tevent_req *req;
1421         NTSTATUS status = NT_STATUS_OK;
1422
1423         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1424                 return cli_smb2_unlink(cli, fname);
1425         }
1426
1427         frame = talloc_stackframe();
1428
1429         if (smbXcli_conn_has_async_calls(cli->conn)) {
1430                 /*
1431                  * Can't use sync call while an async call is in flight
1432                  */
1433                 status = NT_STATUS_INVALID_PARAMETER;
1434                 goto fail;
1435         }
1436
1437         ev = samba_tevent_context_init(frame);
1438         if (ev == NULL) {
1439                 status = NT_STATUS_NO_MEMORY;
1440                 goto fail;
1441         }
1442
1443         req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1444         if (req == NULL) {
1445                 status = NT_STATUS_NO_MEMORY;
1446                 goto fail;
1447         }
1448
1449         if (!tevent_req_poll(req, ev)) {
1450                 status = map_nt_error_from_unix(errno);
1451                 goto fail;
1452         }
1453
1454         status = cli_unlink_recv(req);
1455
1456  fail:
1457         TALLOC_FREE(frame);
1458         return status;
1459 }
1460
1461 /****************************************************************************
1462  Create a directory.
1463 ****************************************************************************/
1464
1465 static void cli_mkdir_done(struct tevent_req *subreq);
1466
1467 struct cli_mkdir_state {
1468         int dummy;
1469 };
1470
1471 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1472                                   struct tevent_context *ev,
1473                                   struct cli_state *cli,
1474                                   const char *dname)
1475 {
1476         struct tevent_req *req = NULL, *subreq = NULL;
1477         struct cli_mkdir_state *state = NULL;
1478         uint8_t additional_flags = 0;
1479         uint8_t *bytes = NULL;
1480
1481         req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1482         if (req == NULL) {
1483                 return NULL;
1484         }
1485
1486         bytes = talloc_array(state, uint8_t, 1);
1487         if (tevent_req_nomem(bytes, req)) {
1488                 return tevent_req_post(req, ev);
1489         }
1490         bytes[0] = 4;
1491         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1492                                    strlen(dname)+1, NULL);
1493
1494         if (tevent_req_nomem(bytes, req)) {
1495                 return tevent_req_post(req, ev);
1496         }
1497
1498         subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1499                               0, NULL, talloc_get_size(bytes), bytes);
1500         if (tevent_req_nomem(subreq, req)) {
1501                 return tevent_req_post(req, ev);
1502         }
1503         tevent_req_set_callback(subreq, cli_mkdir_done, req);
1504         return req;
1505 }
1506
1507 static void cli_mkdir_done(struct tevent_req *subreq)
1508 {
1509         struct tevent_req *req = tevent_req_callback_data(
1510                 subreq, struct tevent_req);
1511         NTSTATUS status;
1512
1513         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1514         TALLOC_FREE(subreq);
1515         if (tevent_req_nterror(req, status)) {
1516                 return;
1517         }
1518         tevent_req_done(req);
1519 }
1520
1521 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1522 {
1523         return tevent_req_simple_recv_ntstatus(req);
1524 }
1525
1526 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1527 {
1528         TALLOC_CTX *frame = NULL;
1529         struct tevent_context *ev;
1530         struct tevent_req *req;
1531         NTSTATUS status = NT_STATUS_OK;
1532
1533         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1534                 return cli_smb2_mkdir(cli, dname);
1535         }
1536
1537         frame = talloc_stackframe();
1538
1539         if (smbXcli_conn_has_async_calls(cli->conn)) {
1540                 /*
1541                  * Can't use sync call while an async call is in flight
1542                  */
1543                 status = NT_STATUS_INVALID_PARAMETER;
1544                 goto fail;
1545         }
1546
1547         ev = samba_tevent_context_init(frame);
1548         if (ev == NULL) {
1549                 status = NT_STATUS_NO_MEMORY;
1550                 goto fail;
1551         }
1552
1553         req = cli_mkdir_send(frame, ev, cli, dname);
1554         if (req == NULL) {
1555                 status = NT_STATUS_NO_MEMORY;
1556                 goto fail;
1557         }
1558
1559         if (!tevent_req_poll(req, ev)) {
1560                 status = map_nt_error_from_unix(errno);
1561                 goto fail;
1562         }
1563
1564         status = cli_mkdir_recv(req);
1565
1566  fail:
1567         TALLOC_FREE(frame);
1568         return status;
1569 }
1570
1571 /****************************************************************************
1572  Remove a directory.
1573 ****************************************************************************/
1574
1575 static void cli_rmdir_done(struct tevent_req *subreq);
1576
1577 struct cli_rmdir_state {
1578         int dummy;
1579 };
1580
1581 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1582                                   struct tevent_context *ev,
1583                                   struct cli_state *cli,
1584                                   const char *dname)
1585 {
1586         struct tevent_req *req = NULL, *subreq = NULL;
1587         struct cli_rmdir_state *state = NULL;
1588         uint8_t additional_flags = 0;
1589         uint8_t *bytes = NULL;
1590
1591         req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1592         if (req == NULL) {
1593                 return NULL;
1594         }
1595
1596         bytes = talloc_array(state, uint8_t, 1);
1597         if (tevent_req_nomem(bytes, req)) {
1598                 return tevent_req_post(req, ev);
1599         }
1600         bytes[0] = 4;
1601         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1602                                    strlen(dname)+1, NULL);
1603
1604         if (tevent_req_nomem(bytes, req)) {
1605                 return tevent_req_post(req, ev);
1606         }
1607
1608         subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1609                               0, NULL, talloc_get_size(bytes), bytes);
1610         if (tevent_req_nomem(subreq, req)) {
1611                 return tevent_req_post(req, ev);
1612         }
1613         tevent_req_set_callback(subreq, cli_rmdir_done, req);
1614         return req;
1615 }
1616
1617 static void cli_rmdir_done(struct tevent_req *subreq)
1618 {
1619         struct tevent_req *req = tevent_req_callback_data(
1620                 subreq, struct tevent_req);
1621         NTSTATUS status;
1622
1623         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1624         TALLOC_FREE(subreq);
1625         if (tevent_req_nterror(req, status)) {
1626                 return;
1627         }
1628         tevent_req_done(req);
1629 }
1630
1631 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1632 {
1633         return tevent_req_simple_recv_ntstatus(req);
1634 }
1635
1636 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1637 {
1638         TALLOC_CTX *frame = NULL;
1639         struct tevent_context *ev;
1640         struct tevent_req *req;
1641         NTSTATUS status = NT_STATUS_OK;
1642
1643         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1644                 return cli_smb2_rmdir(cli, dname);
1645         }
1646
1647         frame = talloc_stackframe();
1648
1649         if (smbXcli_conn_has_async_calls(cli->conn)) {
1650                 /*
1651                  * Can't use sync call while an async call is in flight
1652                  */
1653                 status = NT_STATUS_INVALID_PARAMETER;
1654                 goto fail;
1655         }
1656
1657         ev = samba_tevent_context_init(frame);
1658         if (ev == NULL) {
1659                 status = NT_STATUS_NO_MEMORY;
1660                 goto fail;
1661         }
1662
1663         req = cli_rmdir_send(frame, ev, cli, dname);
1664         if (req == NULL) {
1665                 status = NT_STATUS_NO_MEMORY;
1666                 goto fail;
1667         }
1668
1669         if (!tevent_req_poll(req, ev)) {
1670                 status = map_nt_error_from_unix(errno);
1671                 goto fail;
1672         }
1673
1674         status = cli_rmdir_recv(req);
1675
1676  fail:
1677         TALLOC_FREE(frame);
1678         return status;
1679 }
1680
1681 /****************************************************************************
1682  Set or clear the delete on close flag.
1683 ****************************************************************************/
1684
1685 struct doc_state {
1686         uint16_t setup;
1687         uint8_t param[6];
1688         uint8_t data[1];
1689 };
1690
1691 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1692 {
1693         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1694                                          NULL, 0, NULL, NULL, 0, NULL);
1695         tevent_req_simple_finish_ntstatus(subreq, status);
1696 }
1697
1698 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1699                                         struct tevent_context *ev,
1700                                         struct cli_state *cli,
1701                                         uint16_t fnum,
1702                                         bool flag)
1703 {
1704         struct tevent_req *req = NULL, *subreq = NULL;
1705         struct doc_state *state = NULL;
1706
1707         req = tevent_req_create(mem_ctx, &state, struct doc_state);
1708         if (req == NULL) {
1709                 return NULL;
1710         }
1711
1712         /* Setup setup word. */
1713         SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1714
1715         /* Setup param array. */
1716         SSVAL(state->param,0,fnum);
1717         SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1718
1719         /* Setup data array. */
1720         SCVAL(&state->data[0], 0, flag ? 1 : 0);
1721
1722         subreq = cli_trans_send(state,                  /* mem ctx. */
1723                                 ev,                     /* event ctx. */
1724                                 cli,                    /* cli_state. */
1725                                 SMBtrans2,              /* cmd. */
1726                                 NULL,                   /* pipe name. */
1727                                 -1,                     /* fid. */
1728                                 0,                      /* function. */
1729                                 0,                      /* flags. */
1730                                 &state->setup,          /* setup. */
1731                                 1,                      /* num setup uint16_t words. */
1732                                 0,                      /* max returned setup. */
1733                                 state->param,           /* param. */
1734                                 6,                      /* num param. */
1735                                 2,                      /* max returned param. */
1736                                 state->data,            /* data. */
1737                                 1,                      /* num data. */
1738                                 0);                     /* max returned data. */
1739
1740         if (tevent_req_nomem(subreq, req)) {
1741                 return tevent_req_post(req, ev);
1742         }
1743         tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1744         return req;
1745 }
1746
1747 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1748 {
1749         return tevent_req_simple_recv_ntstatus(req);
1750 }
1751
1752 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1753 {
1754         TALLOC_CTX *frame = talloc_stackframe();
1755         struct tevent_context *ev = NULL;
1756         struct tevent_req *req = NULL;
1757         NTSTATUS status = NT_STATUS_OK;
1758
1759         if (smbXcli_conn_has_async_calls(cli->conn)) {
1760                 /*
1761                  * Can't use sync call while an async call is in flight
1762                  */
1763                 status = NT_STATUS_INVALID_PARAMETER;
1764                 goto fail;
1765         }
1766
1767         ev = samba_tevent_context_init(frame);
1768         if (ev == NULL) {
1769                 status = NT_STATUS_NO_MEMORY;
1770                 goto fail;
1771         }
1772
1773         req = cli_nt_delete_on_close_send(frame,
1774                                 ev,
1775                                 cli,
1776                                 fnum,
1777                                 flag);
1778         if (req == NULL) {
1779                 status = NT_STATUS_NO_MEMORY;
1780                 goto fail;
1781         }
1782
1783         if (!tevent_req_poll(req, ev)) {
1784                 status = map_nt_error_from_unix(errno);
1785                 goto fail;
1786         }
1787
1788         status = cli_nt_delete_on_close_recv(req);
1789
1790  fail:
1791         TALLOC_FREE(frame);
1792         return status;
1793 }
1794
1795 struct cli_ntcreate_state {
1796         uint16_t vwv[24];
1797         uint16_t fnum;
1798         struct smb_create_returns cr;
1799 };
1800
1801 static void cli_ntcreate_done(struct tevent_req *subreq);
1802
1803 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1804                                      struct tevent_context *ev,
1805                                      struct cli_state *cli,
1806                                      const char *fname,
1807                                      uint32_t CreatFlags,
1808                                      uint32_t DesiredAccess,
1809                                      uint32_t FileAttributes,
1810                                      uint32_t ShareAccess,
1811                                      uint32_t CreateDisposition,
1812                                      uint32_t CreateOptions,
1813                                      uint8_t SecurityFlags)
1814 {
1815         struct tevent_req *req, *subreq;
1816         struct cli_ntcreate_state *state;
1817         uint16_t *vwv;
1818         uint8_t *bytes;
1819         size_t converted_len;
1820
1821         req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1822         if (req == NULL) {
1823                 return NULL;
1824         }
1825
1826         vwv = state->vwv;
1827
1828         SCVAL(vwv+0, 0, 0xFF);
1829         SCVAL(vwv+0, 1, 0);
1830         SSVAL(vwv+1, 0, 0);
1831         SCVAL(vwv+2, 0, 0);
1832
1833         if (cli->use_oplocks) {
1834                 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1835         }
1836         SIVAL(vwv+3, 1, CreatFlags);
1837         SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
1838         SIVAL(vwv+7, 1, DesiredAccess);
1839         SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
1840         SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
1841         SIVAL(vwv+13, 1, FileAttributes);
1842         SIVAL(vwv+15, 1, ShareAccess);
1843         SIVAL(vwv+17, 1, CreateDisposition);
1844         SIVAL(vwv+19, 1, CreateOptions |
1845                 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1846         SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1847         SCVAL(vwv+23, 1, SecurityFlags);
1848
1849         bytes = talloc_array(state, uint8_t, 0);
1850         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1851                                    fname, strlen(fname)+1,
1852                                    &converted_len);
1853
1854         /* sigh. this copes with broken netapp filer behaviour */
1855         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1856
1857         if (tevent_req_nomem(bytes, req)) {
1858                 return tevent_req_post(req, ev);
1859         }
1860
1861         SSVAL(vwv+2, 1, converted_len);
1862
1863         subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1864                               talloc_get_size(bytes), bytes);
1865         if (tevent_req_nomem(subreq, req)) {
1866                 return tevent_req_post(req, ev);
1867         }
1868         tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1869         return req;
1870 }
1871
1872 static void cli_ntcreate_done(struct tevent_req *subreq)
1873 {
1874         struct tevent_req *req = tevent_req_callback_data(
1875                 subreq, struct tevent_req);
1876         struct cli_ntcreate_state *state = tevent_req_data(
1877                 req, struct cli_ntcreate_state);
1878         uint8_t wct;
1879         uint16_t *vwv;
1880         uint32_t num_bytes;
1881         uint8_t *bytes;
1882         NTSTATUS status;
1883
1884         status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1885                               &num_bytes, &bytes);
1886         TALLOC_FREE(subreq);
1887         if (tevent_req_nterror(req, status)) {
1888                 return;
1889         }
1890         state->cr.oplock_level = CVAL(vwv+2, 0);
1891         state->fnum = SVAL(vwv+2, 1);
1892         state->cr.create_action = IVAL(vwv+3, 1);
1893         state->cr.creation_time = BVAL(vwv+5, 1);
1894         state->cr.last_access_time = BVAL(vwv+9, 1);
1895         state->cr.last_write_time = BVAL(vwv+13, 1);
1896         state->cr.change_time   = BVAL(vwv+17, 1);
1897         state->cr.file_attributes = IVAL(vwv+21, 1);
1898         state->cr.allocation_size = BVAL(vwv+23, 1);
1899         state->cr.end_of_file   = BVAL(vwv+27, 1);
1900
1901         tevent_req_done(req);
1902 }
1903
1904 NTSTATUS cli_ntcreate_recv(struct tevent_req *req,
1905                 uint16_t *pfnum,
1906                 struct smb_create_returns *cr)
1907 {
1908         struct cli_ntcreate_state *state = tevent_req_data(
1909                 req, struct cli_ntcreate_state);
1910         NTSTATUS status;
1911
1912         if (tevent_req_is_nterror(req, &status)) {
1913                 return status;
1914         }
1915         *pfnum = state->fnum;
1916         if (cr != NULL) {
1917                 *cr = state->cr;
1918         }
1919         return NT_STATUS_OK;
1920 }
1921
1922 NTSTATUS cli_ntcreate(struct cli_state *cli,
1923                       const char *fname,
1924                       uint32_t CreatFlags,
1925                       uint32_t DesiredAccess,
1926                       uint32_t FileAttributes,
1927                       uint32_t ShareAccess,
1928                       uint32_t CreateDisposition,
1929                       uint32_t CreateOptions,
1930                       uint8_t SecurityFlags,
1931                       uint16_t *pfid,
1932                       struct smb_create_returns *cr)
1933 {
1934         TALLOC_CTX *frame = NULL;
1935         struct tevent_context *ev;
1936         struct tevent_req *req;
1937         NTSTATUS status = NT_STATUS_OK;
1938
1939         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1940                 return cli_smb2_create_fnum(cli,
1941                                         fname,
1942                                         CreatFlags,
1943                                         DesiredAccess,
1944                                         FileAttributes,
1945                                         ShareAccess,
1946                                         CreateDisposition,
1947                                         CreateOptions,
1948                                         pfid,
1949                                         cr);
1950         }
1951
1952         frame = talloc_stackframe();
1953
1954         if (smbXcli_conn_has_async_calls(cli->conn)) {
1955                 /*
1956                  * Can't use sync call while an async call is in flight
1957                  */
1958                 status = NT_STATUS_INVALID_PARAMETER;
1959                 goto fail;
1960         }
1961
1962         ev = samba_tevent_context_init(frame);
1963         if (ev == NULL) {
1964                 status = NT_STATUS_NO_MEMORY;
1965                 goto fail;
1966         }
1967
1968         req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1969                                 DesiredAccess, FileAttributes, ShareAccess,
1970                                 CreateDisposition, CreateOptions,
1971                                 SecurityFlags);
1972         if (req == NULL) {
1973                 status = NT_STATUS_NO_MEMORY;
1974                 goto fail;
1975         }
1976
1977         if (!tevent_req_poll(req, ev)) {
1978                 status = map_nt_error_from_unix(errno);
1979                 goto fail;
1980         }
1981
1982         status = cli_ntcreate_recv(req, pfid, cr);
1983  fail:
1984         TALLOC_FREE(frame);
1985         return status;
1986 }
1987
1988 struct cli_nttrans_create_state {
1989         uint16_t fnum;
1990         struct smb_create_returns cr;
1991 };
1992
1993 static void cli_nttrans_create_done(struct tevent_req *subreq);
1994
1995 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1996                                            struct tevent_context *ev,
1997                                            struct cli_state *cli,
1998                                            const char *fname,
1999                                            uint32_t CreatFlags,
2000                                            uint32_t DesiredAccess,
2001                                            uint32_t FileAttributes,
2002                                            uint32_t ShareAccess,
2003                                            uint32_t CreateDisposition,
2004                                            uint32_t CreateOptions,
2005                                            uint8_t SecurityFlags,
2006                                            struct security_descriptor *secdesc,
2007                                            struct ea_struct *eas,
2008                                            int num_eas)
2009 {
2010         struct tevent_req *req, *subreq;
2011         struct cli_nttrans_create_state *state;
2012         uint8_t *param;
2013         uint8_t *secdesc_buf;
2014         size_t secdesc_len;
2015         NTSTATUS status;
2016         size_t converted_len;
2017
2018         req = tevent_req_create(mem_ctx,
2019                                 &state, struct cli_nttrans_create_state);
2020         if (req == NULL) {
2021                 return NULL;
2022         }
2023
2024         if (secdesc != NULL) {
2025                 status = marshall_sec_desc(talloc_tos(), secdesc,
2026                                            &secdesc_buf, &secdesc_len);
2027                 if (tevent_req_nterror(req, status)) {
2028                         DEBUG(10, ("marshall_sec_desc failed: %s\n",
2029                                    nt_errstr(status)));
2030                         return tevent_req_post(req, ev);
2031                 }
2032         } else {
2033                 secdesc_buf = NULL;
2034                 secdesc_len = 0;
2035         }
2036
2037         if (num_eas != 0) {
2038                 /*
2039                  * TODO ;-)
2040                  */
2041                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2042                 return tevent_req_post(req, ev);
2043         }
2044
2045         param = talloc_array(state, uint8_t, 53);
2046         if (tevent_req_nomem(param, req)) {
2047                 return tevent_req_post(req, ev);
2048         }
2049
2050         param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2051                                       fname, strlen(fname),
2052                                       &converted_len);
2053         if (tevent_req_nomem(param, req)) {
2054                 return tevent_req_post(req, ev);
2055         }
2056
2057         SIVAL(param, 0, CreatFlags);
2058         SIVAL(param, 4, 0x0);   /* RootDirectoryFid */
2059         SIVAL(param, 8, DesiredAccess);
2060         SIVAL(param, 12, 0x0);  /* AllocationSize */
2061         SIVAL(param, 16, 0x0);  /* AllocationSize */
2062         SIVAL(param, 20, FileAttributes);
2063         SIVAL(param, 24, ShareAccess);
2064         SIVAL(param, 28, CreateDisposition);
2065         SIVAL(param, 32, CreateOptions |
2066                 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2067         SIVAL(param, 36, secdesc_len);
2068         SIVAL(param, 40, 0);     /* EA length*/
2069         SIVAL(param, 44, converted_len);
2070         SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2071         SCVAL(param, 52, SecurityFlags);
2072
2073         subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2074                                 NULL, -1, /* name, fid */
2075                                 NT_TRANSACT_CREATE, 0,
2076                                 NULL, 0, 0, /* setup */
2077                                 param, talloc_get_size(param), 128, /* param */
2078                                 secdesc_buf, secdesc_len, 0); /* data */
2079         if (tevent_req_nomem(subreq, req)) {
2080                 return tevent_req_post(req, ev);
2081         }
2082         tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2083         return req;
2084 }
2085
2086 static void cli_nttrans_create_done(struct tevent_req *subreq)
2087 {
2088         struct tevent_req *req = tevent_req_callback_data(
2089                 subreq, struct tevent_req);
2090         struct cli_nttrans_create_state *state = tevent_req_data(
2091                 req, struct cli_nttrans_create_state);
2092         uint8_t *param;
2093         uint32_t num_param;
2094         NTSTATUS status;
2095
2096         status = cli_trans_recv(subreq, talloc_tos(), NULL,
2097                                 NULL, 0, NULL, /* rsetup */
2098                                 &param, 69, &num_param,
2099                                 NULL, 0, NULL);
2100         if (tevent_req_nterror(req, status)) {
2101                 return;
2102         }
2103         state->cr.oplock_level = CVAL(param, 0);
2104         state->fnum = SVAL(param, 2);
2105         state->cr.create_action = IVAL(param, 4);
2106         state->cr.creation_time = BVAL(param, 12);
2107         state->cr.last_access_time = BVAL(param, 20);
2108         state->cr.last_write_time = BVAL(param, 28);
2109         state->cr.change_time   = BVAL(param, 36);
2110         state->cr.file_attributes = IVAL(param, 44);
2111         state->cr.allocation_size = BVAL(param, 48);
2112         state->cr.end_of_file   = BVAL(param, 56);
2113
2114         TALLOC_FREE(param);
2115         tevent_req_done(req);
2116 }
2117
2118 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2119                         uint16_t *fnum,
2120                         struct smb_create_returns *cr)
2121 {
2122         struct cli_nttrans_create_state *state = tevent_req_data(
2123                 req, struct cli_nttrans_create_state);
2124         NTSTATUS status;
2125
2126         if (tevent_req_is_nterror(req, &status)) {
2127                 return status;
2128         }
2129         *fnum = state->fnum;
2130         if (cr != NULL) {
2131                 *cr = state->cr;
2132         }
2133         return NT_STATUS_OK;
2134 }
2135
2136 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2137                             const char *fname,
2138                             uint32_t CreatFlags,
2139                             uint32_t DesiredAccess,
2140                             uint32_t FileAttributes,
2141                             uint32_t ShareAccess,
2142                             uint32_t CreateDisposition,
2143                             uint32_t CreateOptions,
2144                             uint8_t SecurityFlags,
2145                             struct security_descriptor *secdesc,
2146                             struct ea_struct *eas,
2147                             int num_eas,
2148                             uint16_t *pfid,
2149                             struct smb_create_returns *cr)
2150 {
2151         TALLOC_CTX *frame = talloc_stackframe();
2152         struct tevent_context *ev;
2153         struct tevent_req *req;
2154         NTSTATUS status = NT_STATUS_NO_MEMORY;
2155
2156         if (smbXcli_conn_has_async_calls(cli->conn)) {
2157                 /*
2158                  * Can't use sync call while an async call is in flight
2159                  */
2160                 status = NT_STATUS_INVALID_PARAMETER;
2161                 goto fail;
2162         }
2163         ev = samba_tevent_context_init(frame);
2164         if (ev == NULL) {
2165                 goto fail;
2166         }
2167         req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2168                                       DesiredAccess, FileAttributes,
2169                                       ShareAccess, CreateDisposition,
2170                                       CreateOptions, SecurityFlags,
2171                                       secdesc, eas, num_eas);
2172         if (req == NULL) {
2173                 goto fail;
2174         }
2175         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2176                 goto fail;
2177         }
2178         status = cli_nttrans_create_recv(req, pfid, cr);
2179  fail:
2180         TALLOC_FREE(frame);
2181         return status;
2182 }
2183
2184 /****************************************************************************
2185  Open a file
2186  WARNING: if you open with O_WRONLY then getattrE won't work!
2187 ****************************************************************************/
2188
2189 struct cli_openx_state {
2190         const char *fname;
2191         uint16_t vwv[15];
2192         uint16_t fnum;
2193         struct iovec bytes;
2194 };
2195
2196 static void cli_openx_done(struct tevent_req *subreq);
2197
2198 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2199                                    struct tevent_context *ev,
2200                                    struct cli_state *cli, const char *fname,
2201                                    int flags, int share_mode,
2202                                    struct tevent_req **psmbreq)
2203 {
2204         struct tevent_req *req, *subreq;
2205         struct cli_openx_state *state;
2206         unsigned openfn;
2207         unsigned accessmode;
2208         uint8_t additional_flags;
2209         uint8_t *bytes;
2210
2211         req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2212         if (req == NULL) {
2213                 return NULL;
2214         }
2215
2216         openfn = 0;
2217         if (flags & O_CREAT) {
2218                 openfn |= (1<<4);
2219         }
2220         if (!(flags & O_EXCL)) {
2221                 if (flags & O_TRUNC)
2222                         openfn |= (1<<1);
2223                 else
2224                         openfn |= (1<<0);
2225         }
2226
2227         accessmode = (share_mode<<4);
2228
2229         if ((flags & O_ACCMODE) == O_RDWR) {
2230                 accessmode |= 2;
2231         } else if ((flags & O_ACCMODE) == O_WRONLY) {
2232                 accessmode |= 1;
2233         }
2234
2235 #if defined(O_SYNC)
2236         if ((flags & O_SYNC) == O_SYNC) {
2237                 accessmode |= (1<<14);
2238         }
2239 #endif /* O_SYNC */
2240
2241         if (share_mode == DENY_FCB) {
2242                 accessmode = 0xFF;
2243         }
2244
2245         SCVAL(state->vwv + 0, 0, 0xFF);
2246         SCVAL(state->vwv + 0, 1, 0);
2247         SSVAL(state->vwv + 1, 0, 0);
2248         SSVAL(state->vwv + 2, 0, 0);  /* no additional info */
2249         SSVAL(state->vwv + 3, 0, accessmode);
2250         SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2251         SSVAL(state->vwv + 5, 0, 0);
2252         SIVAL(state->vwv + 6, 0, 0);
2253         SSVAL(state->vwv + 8, 0, openfn);
2254         SIVAL(state->vwv + 9, 0, 0);
2255         SIVAL(state->vwv + 11, 0, 0);
2256         SIVAL(state->vwv + 13, 0, 0);
2257
2258         additional_flags = 0;
2259
2260         if (cli->use_oplocks) {
2261                 /* if using oplocks then ask for a batch oplock via
2262                    core and extended methods */
2263                 additional_flags =
2264                         FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2265                 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2266         }
2267
2268         bytes = talloc_array(state, uint8_t, 0);
2269         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2270                                    strlen(fname)+1, NULL);
2271
2272         if (tevent_req_nomem(bytes, req)) {
2273                 return tevent_req_post(req, ev);
2274         }
2275
2276         state->bytes.iov_base = (void *)bytes;
2277         state->bytes.iov_len = talloc_get_size(bytes);
2278
2279         subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2280                                     15, state->vwv, 1, &state->bytes);
2281         if (subreq == NULL) {
2282                 TALLOC_FREE(req);
2283                 return NULL;
2284         }
2285         tevent_req_set_callback(subreq, cli_openx_done, req);
2286         *psmbreq = subreq;
2287         return req;
2288 }
2289
2290 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2291                                  struct cli_state *cli, const char *fname,
2292                                  int flags, int share_mode)
2293 {
2294         struct tevent_req *req, *subreq;
2295         NTSTATUS status;
2296
2297         req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2298                               &subreq);
2299         if (req == NULL) {
2300                 return NULL;
2301         }
2302
2303         status = smb1cli_req_chain_submit(&subreq, 1);
2304         if (tevent_req_nterror(req, status)) {
2305                 return tevent_req_post(req, ev);
2306         }
2307         return req;
2308 }
2309
2310 static void cli_openx_done(struct tevent_req *subreq)
2311 {
2312         struct tevent_req *req = tevent_req_callback_data(
2313                 subreq, struct tevent_req);
2314         struct cli_openx_state *state = tevent_req_data(
2315                 req, struct cli_openx_state);
2316         uint8_t wct;
2317         uint16_t *vwv;
2318         NTSTATUS status;
2319
2320         status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2321                               NULL);
2322         TALLOC_FREE(subreq);
2323         if (tevent_req_nterror(req, status)) {
2324                 return;
2325         }
2326         state->fnum = SVAL(vwv+2, 0);
2327         tevent_req_done(req);
2328 }
2329
2330 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2331 {
2332         struct cli_openx_state *state = tevent_req_data(
2333                 req, struct cli_openx_state);
2334         NTSTATUS status;
2335
2336         if (tevent_req_is_nterror(req, &status)) {
2337                 return status;
2338         }
2339         *pfnum = state->fnum;
2340         return NT_STATUS_OK;
2341 }
2342
2343 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2344              int share_mode, uint16_t *pfnum)
2345 {
2346         TALLOC_CTX *frame = talloc_stackframe();
2347         struct tevent_context *ev;
2348         struct tevent_req *req;
2349         NTSTATUS status = NT_STATUS_NO_MEMORY;
2350
2351         if (smbXcli_conn_has_async_calls(cli->conn)) {
2352                 /*
2353                  * Can't use sync call while an async call is in flight
2354                  */
2355                 status = NT_STATUS_INVALID_PARAMETER;
2356                 goto fail;
2357         }
2358
2359         ev = samba_tevent_context_init(frame);
2360         if (ev == NULL) {
2361                 goto fail;
2362         }
2363
2364         req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2365         if (req == NULL) {
2366                 goto fail;
2367         }
2368
2369         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2370                 goto fail;
2371         }
2372
2373         status = cli_openx_recv(req, pfnum);
2374  fail:
2375         TALLOC_FREE(frame);
2376         return status;
2377 }
2378 /****************************************************************************
2379  Synchronous wrapper function that does an NtCreateX open by preference
2380  and falls back to openX if this fails.
2381 ****************************************************************************/
2382
2383 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2384                         int share_mode_in, uint16_t *pfnum)
2385 {
2386         NTSTATUS status;
2387         unsigned int openfn = 0;
2388         unsigned int dos_deny = 0;
2389         uint32_t access_mask, share_mode, create_disposition, create_options;
2390
2391         /* Do the initial mapping into OpenX parameters. */
2392         if (flags & O_CREAT) {
2393                 openfn |= (1<<4);
2394         }
2395         if (!(flags & O_EXCL)) {
2396                 if (flags & O_TRUNC)
2397                         openfn |= (1<<1);
2398                 else
2399                         openfn |= (1<<0);
2400         }
2401
2402         dos_deny = (share_mode_in<<4);
2403
2404         if ((flags & O_ACCMODE) == O_RDWR) {
2405                 dos_deny |= 2;
2406         } else if ((flags & O_ACCMODE) == O_WRONLY) {
2407                 dos_deny |= 1;
2408         }
2409
2410 #if defined(O_SYNC)
2411         if ((flags & O_SYNC) == O_SYNC) {
2412                 dos_deny |= (1<<14);
2413         }
2414 #endif /* O_SYNC */
2415
2416         if (share_mode_in == DENY_FCB) {
2417                 dos_deny = 0xFF;
2418         }
2419
2420 #if 0
2421         /* Hmmm. This is what I think the above code
2422            should look like if it's using the constants
2423            we #define. JRA. */
2424
2425         if (flags & O_CREAT) {
2426                 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2427         }
2428         if (!(flags & O_EXCL)) {
2429                 if (flags & O_TRUNC)
2430                         openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2431                 else
2432                         openfn |= OPENX_FILE_EXISTS_OPEN;
2433         }
2434
2435         dos_deny = SET_DENY_MODE(share_mode_in);
2436
2437         if ((flags & O_ACCMODE) == O_RDWR) {
2438                 dos_deny |= DOS_OPEN_RDWR;
2439         } else if ((flags & O_ACCMODE) == O_WRONLY) {
2440                 dos_deny |= DOS_OPEN_WRONLY;
2441         }
2442
2443 #if defined(O_SYNC)
2444         if ((flags & O_SYNC) == O_SYNC) {
2445                 dos_deny |= FILE_SYNC_OPENMODE;
2446         }
2447 #endif /* O_SYNC */
2448
2449         if (share_mode_in == DENY_FCB) {
2450                 dos_deny = 0xFF;
2451         }
2452 #endif
2453
2454         if (!map_open_params_to_ntcreate(fname, dos_deny,
2455                                         openfn, &access_mask,
2456                                         &share_mode, &create_disposition,
2457                                         &create_options, NULL)) {
2458                 goto try_openx;
2459         }
2460
2461         status = cli_ntcreate(cli,
2462                                 fname,
2463                                 0,
2464                                 access_mask,
2465                                 0,
2466                                 share_mode,
2467                                 create_disposition,
2468                                 create_options,
2469                                 0,
2470                                 pfnum,
2471                                 NULL);
2472
2473         /* Try and cope will all varients of "we don't do this call"
2474            and fall back to openX. */
2475
2476         if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2477                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2478                         NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2479                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2480                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2481                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2482                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2483                         NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2484                         NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2485                 goto try_openx;
2486         }
2487
2488         return status;
2489
2490   try_openx:
2491
2492         return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2493 }
2494
2495 /****************************************************************************
2496  Close a file.
2497 ****************************************************************************/
2498
2499 struct cli_close_state {
2500         uint16_t vwv[3];
2501 };
2502
2503 static void cli_close_done(struct tevent_req *subreq);
2504
2505 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2506                                 struct tevent_context *ev,
2507                                 struct cli_state *cli,
2508                                 uint16_t fnum,
2509                                 struct tevent_req **psubreq)
2510 {
2511         struct tevent_req *req, *subreq;
2512         struct cli_close_state *state;
2513
2514         req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2515         if (req == NULL) {
2516                 return NULL;
2517         }
2518
2519         SSVAL(state->vwv+0, 0, fnum);
2520         SIVALS(state->vwv+1, 0, -1);
2521
2522         subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2523                                     0, NULL);
2524         if (subreq == NULL) {
2525                 TALLOC_FREE(req);
2526                 return NULL;
2527         }
2528         tevent_req_set_callback(subreq, cli_close_done, req);
2529         *psubreq = subreq;
2530         return req;
2531 }
2532
2533 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2534                                 struct tevent_context *ev,
2535                                 struct cli_state *cli,
2536                                 uint16_t fnum)
2537 {
2538         struct tevent_req *req, *subreq;
2539         NTSTATUS status;
2540
2541         req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2542         if (req == NULL) {
2543                 return NULL;
2544         }
2545
2546         status = smb1cli_req_chain_submit(&subreq, 1);
2547         if (tevent_req_nterror(req, status)) {
2548                 return tevent_req_post(req, ev);
2549         }
2550         return req;
2551 }
2552
2553 static void cli_close_done(struct tevent_req *subreq)
2554 {
2555         struct tevent_req *req = tevent_req_callback_data(
2556                 subreq, struct tevent_req);
2557         NTSTATUS status;
2558
2559         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2560         TALLOC_FREE(subreq);
2561         if (tevent_req_nterror(req, status)) {
2562                 return;
2563         }
2564         tevent_req_done(req);
2565 }
2566
2567 NTSTATUS cli_close_recv(struct tevent_req *req)
2568 {
2569         return tevent_req_simple_recv_ntstatus(req);
2570 }
2571
2572 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2573 {
2574         TALLOC_CTX *frame = NULL;
2575         struct tevent_context *ev;
2576         struct tevent_req *req;
2577         NTSTATUS status = NT_STATUS_OK;
2578
2579         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2580                 return cli_smb2_close_fnum(cli, fnum);
2581         }
2582
2583         frame = talloc_stackframe();
2584
2585         if (smbXcli_conn_has_async_calls(cli->conn)) {
2586                 /*
2587                  * Can't use sync call while an async call is in flight
2588                  */
2589                 status = NT_STATUS_INVALID_PARAMETER;
2590                 goto fail;
2591         }
2592
2593         ev = samba_tevent_context_init(frame);
2594         if (ev == NULL) {
2595                 status = NT_STATUS_NO_MEMORY;
2596                 goto fail;
2597         }
2598
2599         req = cli_close_send(frame, ev, cli, fnum);
2600         if (req == NULL) {
2601                 status = NT_STATUS_NO_MEMORY;
2602                 goto fail;
2603         }
2604
2605         if (!tevent_req_poll(req, ev)) {
2606                 status = map_nt_error_from_unix(errno);
2607                 goto fail;
2608         }
2609
2610         status = cli_close_recv(req);
2611  fail:
2612         TALLOC_FREE(frame);
2613         return status;
2614 }
2615
2616 /****************************************************************************
2617  Truncate a file to a specified size
2618 ****************************************************************************/
2619
2620 struct ftrunc_state {
2621         uint16_t setup;
2622         uint8_t param[6];
2623         uint8_t data[8];
2624 };
2625
2626 static void cli_ftruncate_done(struct tevent_req *subreq)
2627 {
2628         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2629                                          NULL, 0, NULL, NULL, 0, NULL);
2630         tevent_req_simple_finish_ntstatus(subreq, status);
2631 }
2632
2633 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2634                                         struct tevent_context *ev,
2635                                         struct cli_state *cli,
2636                                         uint16_t fnum,
2637                                         uint64_t size)
2638 {
2639         struct tevent_req *req = NULL, *subreq = NULL;
2640         struct ftrunc_state *state = NULL;
2641
2642         req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2643         if (req == NULL) {
2644                 return NULL;
2645         }
2646
2647         /* Setup setup word. */
2648         SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2649
2650         /* Setup param array. */
2651         SSVAL(state->param,0,fnum);
2652         SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2653         SSVAL(state->param,4,0);
2654
2655         /* Setup data array. */
2656         SBVAL(state->data, 0, size);
2657
2658         subreq = cli_trans_send(state,                  /* mem ctx. */
2659                                 ev,                     /* event ctx. */
2660                                 cli,                    /* cli_state. */
2661                                 SMBtrans2,              /* cmd. */
2662                                 NULL,                   /* pipe name. */
2663                                 -1,                     /* fid. */
2664                                 0,                      /* function. */
2665                                 0,                      /* flags. */
2666                                 &state->setup,          /* setup. */
2667                                 1,                      /* num setup uint16_t words. */
2668                                 0,                      /* max returned setup. */
2669                                 state->param,           /* param. */
2670                                 6,                      /* num param. */
2671                                 2,                      /* max returned param. */
2672                                 state->data,            /* data. */
2673                                 8,                      /* num data. */
2674                                 0);                     /* max returned data. */
2675
2676         if (tevent_req_nomem(subreq, req)) {
2677                 return tevent_req_post(req, ev);
2678         }
2679         tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2680         return req;
2681 }
2682
2683 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2684 {
2685         return tevent_req_simple_recv_ntstatus(req);
2686 }
2687
2688 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2689 {
2690         TALLOC_CTX *frame = talloc_stackframe();
2691         struct tevent_context *ev = NULL;
2692         struct tevent_req *req = NULL;
2693         NTSTATUS status = NT_STATUS_OK;
2694
2695         if (smbXcli_conn_has_async_calls(cli->conn)) {
2696                 /*
2697                  * Can't use sync call while an async call is in flight
2698                  */
2699                 status = NT_STATUS_INVALID_PARAMETER;
2700                 goto fail;
2701         }
2702
2703         ev = samba_tevent_context_init(frame);
2704         if (ev == NULL) {
2705                 status = NT_STATUS_NO_MEMORY;
2706                 goto fail;
2707         }
2708
2709         req = cli_ftruncate_send(frame,
2710                                 ev,
2711                                 cli,
2712                                 fnum,
2713                                 size);
2714         if (req == NULL) {
2715                 status = NT_STATUS_NO_MEMORY;
2716                 goto fail;
2717         }
2718
2719         if (!tevent_req_poll(req, ev)) {
2720                 status = map_nt_error_from_unix(errno);
2721                 goto fail;
2722         }
2723
2724         status = cli_ftruncate_recv(req);
2725
2726  fail:
2727         TALLOC_FREE(frame);
2728         return status;
2729 }
2730
2731 /****************************************************************************
2732  send a lock with a specified locktype
2733  this is used for testing LOCKING_ANDX_CANCEL_LOCK
2734 ****************************************************************************/
2735
2736 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2737                       uint32_t offset, uint32_t len,
2738                       int timeout, unsigned char locktype)
2739 {
2740         uint16_t vwv[8];
2741         uint8_t bytes[10];
2742         NTSTATUS status;
2743         unsigned int set_timeout = 0;
2744         unsigned int saved_timeout = 0;
2745
2746         SCVAL(vwv + 0, 0, 0xff);
2747         SCVAL(vwv + 0, 1, 0);
2748         SSVAL(vwv + 1, 0, 0);
2749         SSVAL(vwv + 2, 0, fnum);
2750         SCVAL(vwv + 3, 0, locktype);
2751         SCVAL(vwv + 3, 1, 0);
2752         SIVALS(vwv + 4, 0, timeout);
2753         SSVAL(vwv + 6, 0, 0);
2754         SSVAL(vwv + 7, 0, 1);
2755
2756         SSVAL(bytes, 0, cli_getpid(cli));
2757         SIVAL(bytes, 2, offset);
2758         SIVAL(bytes, 6, len);
2759
2760         if (timeout != 0) {
2761                 if (timeout == -1) {
2762                         set_timeout = 0x7FFFFFFF;
2763                 } else {
2764                         set_timeout = timeout + 2*1000;
2765                 }
2766                 saved_timeout = cli_set_timeout(cli, set_timeout);
2767         }
2768
2769         status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2770                          10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2771
2772         if (saved_timeout != 0) {
2773                 cli_set_timeout(cli, saved_timeout);
2774         }
2775
2776         return status;
2777 }
2778
2779 /****************************************************************************
2780  Lock a file.
2781  note that timeout is in units of 2 milliseconds
2782 ****************************************************************************/
2783
2784 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2785                   uint32_t offset, uint32_t len, int timeout,
2786                   enum brl_type lock_type)
2787 {
2788         NTSTATUS status;
2789
2790         status = cli_locktype(cli, fnum, offset, len, timeout,
2791                               (lock_type == READ_LOCK? 1 : 0));
2792         return status;
2793 }
2794
2795 /****************************************************************************
2796  Unlock a file.
2797 ****************************************************************************/
2798
2799 struct cli_unlock_state {
2800         uint16_t vwv[8];
2801         uint8_t data[10];
2802 };
2803
2804 static void cli_unlock_done(struct tevent_req *subreq);
2805
2806 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2807                                 struct tevent_context *ev,
2808                                 struct cli_state *cli,
2809                                 uint16_t fnum,
2810                                 uint64_t offset,
2811                                 uint64_t len)
2812
2813 {
2814         struct tevent_req *req = NULL, *subreq = NULL;
2815         struct cli_unlock_state *state = NULL;
2816         uint8_t additional_flags = 0;
2817
2818         req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2819         if (req == NULL) {
2820                 return NULL;
2821         }
2822
2823         SCVAL(state->vwv+0, 0, 0xFF);
2824         SSVAL(state->vwv+2, 0, fnum);
2825         SCVAL(state->vwv+3, 0, 0);
2826         SIVALS(state->vwv+4, 0, 0);
2827         SSVAL(state->vwv+6, 0, 1);
2828         SSVAL(state->vwv+7, 0, 0);
2829
2830         SSVAL(state->data, 0, cli_getpid(cli));
2831         SIVAL(state->data, 2, offset);
2832         SIVAL(state->data, 6, len);
2833
2834         subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2835                                 8, state->vwv, 10, state->data);
2836         if (tevent_req_nomem(subreq, req)) {
2837                 return tevent_req_post(req, ev);
2838         }
2839         tevent_req_set_callback(subreq, cli_unlock_done, req);
2840         return req;
2841 }
2842
2843 static void cli_unlock_done(struct tevent_req *subreq)
2844 {
2845         struct tevent_req *req = tevent_req_callback_data(
2846                                 subreq, struct tevent_req);
2847         NTSTATUS status;
2848
2849         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2850         TALLOC_FREE(subreq);
2851         if (tevent_req_nterror(req, status)) {
2852                 return;
2853         }
2854         tevent_req_done(req);
2855 }
2856
2857 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2858 {
2859         return tevent_req_simple_recv_ntstatus(req);
2860 }
2861
2862 NTSTATUS cli_unlock(struct cli_state *cli,
2863                         uint16_t fnum,
2864                         uint32_t offset,
2865                         uint32_t len)
2866 {
2867         TALLOC_CTX *frame = talloc_stackframe();
2868         struct tevent_context *ev;
2869         struct tevent_req *req;
2870         NTSTATUS status = NT_STATUS_OK;
2871
2872         if (smbXcli_conn_has_async_calls(cli->conn)) {
2873                 /*
2874                  * Can't use sync call while an async call is in flight
2875                  */
2876                 status = NT_STATUS_INVALID_PARAMETER;
2877                 goto fail;
2878         }
2879
2880         ev = samba_tevent_context_init(frame);
2881         if (ev == NULL) {
2882                 status = NT_STATUS_NO_MEMORY;
2883                 goto fail;
2884         }
2885
2886         req = cli_unlock_send(frame, ev, cli,
2887                         fnum, offset, len);
2888         if (req == NULL) {
2889                 status = NT_STATUS_NO_MEMORY;
2890                 goto fail;
2891         }
2892
2893         if (!tevent_req_poll(req, ev)) {
2894                 status = map_nt_error_from_unix(errno);
2895                 goto fail;
2896         }
2897
2898         status = cli_unlock_recv(req);
2899
2900  fail:
2901         TALLOC_FREE(frame);
2902         return status;
2903 }
2904
2905 /****************************************************************************
2906  Lock a file with 64 bit offsets.
2907 ****************************************************************************/
2908
2909 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2910                     uint64_t offset, uint64_t len, int timeout,
2911                     enum brl_type lock_type)
2912 {
2913         uint16_t vwv[8];
2914         uint8_t bytes[20];
2915         unsigned int set_timeout = 0;
2916         unsigned int saved_timeout = 0;
2917         int ltype;
2918         NTSTATUS status;
2919
2920         if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2921                 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2922         }
2923
2924         ltype = (lock_type == READ_LOCK? 1 : 0);
2925         ltype |= LOCKING_ANDX_LARGE_FILES;
2926
2927         SCVAL(vwv + 0, 0, 0xff);
2928         SCVAL(vwv + 0, 1, 0);
2929         SSVAL(vwv + 1, 0, 0);
2930         SSVAL(vwv + 2, 0, fnum);
2931         SCVAL(vwv + 3, 0, ltype);
2932         SCVAL(vwv + 3, 1, 0);
2933         SIVALS(vwv + 4, 0, timeout);
2934         SSVAL(vwv + 6, 0, 0);
2935         SSVAL(vwv + 7, 0, 1);
2936
2937         SIVAL(bytes, 0, cli_getpid(cli));
2938         SOFF_T_R(bytes, 4, offset);
2939         SOFF_T_R(bytes, 12, len);
2940
2941         if (timeout != 0) {
2942                 if (timeout == -1) {
2943                         set_timeout = 0x7FFFFFFF;
2944                 } else {
2945                         set_timeout = timeout + 2*1000;
2946                 }
2947                 saved_timeout = cli_set_timeout(cli, set_timeout);
2948         }
2949
2950         status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2951                          20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2952
2953         if (saved_timeout != 0) {
2954                 cli_set_timeout(cli, saved_timeout);
2955         }
2956
2957         return status;
2958 }
2959
2960 /****************************************************************************
2961  Unlock a file with 64 bit offsets.
2962 ****************************************************************************/
2963
2964 struct cli_unlock64_state {
2965         uint16_t vwv[8];
2966         uint8_t data[20];
2967 };
2968
2969 static void cli_unlock64_done(struct tevent_req *subreq);
2970
2971 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2972                                 struct tevent_context *ev,
2973                                 struct cli_state *cli,
2974                                 uint16_t fnum,
2975                                 uint64_t offset,
2976                                 uint64_t len)
2977
2978 {
2979         struct tevent_req *req = NULL, *subreq = NULL;
2980         struct cli_unlock64_state *state = NULL;
2981         uint8_t additional_flags = 0;
2982
2983         req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2984         if (req == NULL) {
2985                 return NULL;
2986         }
2987
2988         SCVAL(state->vwv+0, 0, 0xff);
2989         SSVAL(state->vwv+2, 0, fnum);
2990         SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2991         SIVALS(state->vwv+4, 0, 0);
2992         SSVAL(state->vwv+6, 0, 1);
2993         SSVAL(state->vwv+7, 0, 0);
2994
2995         SIVAL(state->data, 0, cli_getpid(cli));
2996         SOFF_T_R(state->data, 4, offset);
2997         SOFF_T_R(state->data, 12, len);
2998
2999         subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
3000                                 8, state->vwv, 20, state->data);
3001         if (tevent_req_nomem(subreq, req)) {
3002                 return tevent_req_post(req, ev);
3003         }
3004         tevent_req_set_callback(subreq, cli_unlock64_done, req);
3005         return req;
3006 }
3007
3008 static void cli_unlock64_done(struct tevent_req *subreq)
3009 {
3010         struct tevent_req *req = tevent_req_callback_data(
3011                                 subreq, struct tevent_req);
3012         NTSTATUS status;
3013
3014         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3015         TALLOC_FREE(subreq);
3016         if (tevent_req_nterror(req, status)) {
3017                 return;
3018         }
3019         tevent_req_done(req);
3020 }
3021
3022 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3023 {
3024         return tevent_req_simple_recv_ntstatus(req);
3025 }
3026
3027 NTSTATUS cli_unlock64(struct cli_state *cli,
3028                                 uint16_t fnum,
3029                                 uint64_t offset,
3030                                 uint64_t len)
3031 {
3032         TALLOC_CTX *frame = talloc_stackframe();
3033         struct tevent_context *ev;
3034         struct tevent_req *req;
3035         NTSTATUS status = NT_STATUS_OK;
3036
3037         if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3038                 return cli_unlock(cli, fnum, offset, len);
3039         }
3040
3041         if (smbXcli_conn_has_async_calls(cli->conn)) {
3042                 /*
3043                  * Can't use sync call while an async call is in flight
3044                  */
3045                 status = NT_STATUS_INVALID_PARAMETER;
3046                 goto fail;
3047         }
3048
3049         ev = samba_tevent_context_init(frame);
3050         if (ev == NULL) {
3051                 status = NT_STATUS_NO_MEMORY;
3052                 goto fail;
3053         }
3054
3055         req = cli_unlock64_send(frame, ev, cli,
3056                         fnum, offset, len);
3057         if (req == NULL) {
3058                 status = NT_STATUS_NO_MEMORY;
3059                 goto fail;
3060         }
3061
3062         if (!tevent_req_poll(req, ev)) {
3063                 status = map_nt_error_from_unix(errno);
3064                 goto fail;
3065         }
3066
3067         status = cli_unlock64_recv(req);
3068
3069  fail:
3070         TALLOC_FREE(frame);
3071         return status;
3072 }
3073
3074 /****************************************************************************
3075  Get/unlock a POSIX lock on a file - internal function.
3076 ****************************************************************************/
3077
3078 struct posix_lock_state {
3079         uint16_t setup;
3080         uint8_t param[4];
3081         uint8_t data[POSIX_LOCK_DATA_SIZE];
3082 };
3083
3084 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3085 {
3086         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3087                                          NULL, 0, NULL, NULL, 0, NULL);
3088         tevent_req_simple_finish_ntstatus(subreq, status);
3089 }
3090
3091 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3092                                         struct tevent_context *ev,
3093                                         struct cli_state *cli,
3094                                         uint16_t fnum,
3095                                         uint64_t offset,
3096                                         uint64_t len,
3097                                         bool wait_lock,
3098                                         enum brl_type lock_type)
3099 {
3100         struct tevent_req *req = NULL, *subreq = NULL;
3101         struct posix_lock_state *state = NULL;
3102
3103         req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3104         if (req == NULL) {
3105                 return NULL;
3106         }
3107
3108         /* Setup setup word. */
3109         SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3110
3111         /* Setup param array. */
3112         SSVAL(&state->param, 0, fnum);
3113         SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3114
3115         /* Setup data array. */
3116         switch (lock_type) {
3117                 case READ_LOCK:
3118                         SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3119                                 POSIX_LOCK_TYPE_READ);
3120                         break;
3121                 case WRITE_LOCK:
3122                         SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3123                                 POSIX_LOCK_TYPE_WRITE);
3124                         break;
3125                 case UNLOCK_LOCK:
3126                         SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3127                                 POSIX_LOCK_TYPE_UNLOCK);
3128                         break;
3129                 default:
3130                         return NULL;
3131         }
3132
3133         if (wait_lock) {
3134                 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3135                                 POSIX_LOCK_FLAG_WAIT);
3136         } else {
3137                 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3138                                 POSIX_LOCK_FLAG_NOWAIT);
3139         }
3140
3141         SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3142         SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3143         SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3144
3145         subreq = cli_trans_send(state,                  /* mem ctx. */
3146                                 ev,                     /* event ctx. */
3147                                 cli,                    /* cli_state. */
3148                                 SMBtrans2,              /* cmd. */
3149                                 NULL,                   /* pipe name. */
3150                                 -1,                     /* fid. */
3151                                 0,                      /* function. */
3152                                 0,                      /* flags. */
3153                                 &state->setup,          /* setup. */
3154                                 1,                      /* num setup uint16_t words. */
3155                                 0,                      /* max returned setup. */
3156                                 state->param,           /* param. */
3157                                 4,                      /* num param. */
3158                                 2,                      /* max returned param. */
3159                                 state->data,            /* data. */
3160                                 POSIX_LOCK_DATA_SIZE,   /* num data. */
3161                                 0);                     /* max returned data. */
3162
3163         if (tevent_req_nomem(subreq, req)) {
3164                 return tevent_req_post(req, ev);
3165         }
3166         tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3167         return req;
3168 }
3169
3170 /****************************************************************************
3171  POSIX Lock a file.
3172 ****************************************************************************/
3173
3174 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3175                                         struct tevent_context *ev,
3176                                         struct cli_state *cli,
3177                                         uint16_t fnum,
3178                                         uint64_t offset,
3179                                         uint64_t len,
3180                                         bool wait_lock,
3181                                         enum brl_type lock_type)
3182 {
3183         return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3184                                         wait_lock, lock_type);
3185 }
3186
3187 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3188 {
3189         return tevent_req_simple_recv_ntstatus(req);
3190 }
3191
3192 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3193                         uint64_t offset, uint64_t len,
3194                         bool wait_lock, enum brl_type lock_type)
3195 {
3196         TALLOC_CTX *frame = talloc_stackframe();
3197         struct tevent_context *ev = NULL;
3198         struct tevent_req *req = NULL;
3199         NTSTATUS status = NT_STATUS_OK;
3200
3201         if (smbXcli_conn_has_async_calls(cli->conn)) {
3202                 /*
3203                  * Can't use sync call while an async call is in flight
3204                  */
3205                 status = NT_STATUS_INVALID_PARAMETER;
3206                 goto fail;
3207         }
3208
3209         if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3210                 status = NT_STATUS_INVALID_PARAMETER;
3211                 goto fail;
3212         }
3213
3214         ev = samba_tevent_context_init(frame);
3215         if (ev == NULL) {
3216                 status = NT_STATUS_NO_MEMORY;
3217                 goto fail;
3218         }
3219
3220         req = cli_posix_lock_send(frame,
3221                                 ev,
3222                                 cli,
3223                                 fnum,
3224                                 offset,
3225                                 len,
3226                                 wait_lock,
3227                                 lock_type);
3228         if (req == NULL) {
3229                 status = NT_STATUS_NO_MEMORY;
3230                 goto fail;
3231         }
3232
3233         if (!tevent_req_poll(req, ev)) {
3234                 status = map_nt_error_from_unix(errno);
3235                 goto fail;
3236         }
3237
3238         status = cli_posix_lock_recv(req);
3239
3240  fail:
3241         TALLOC_FREE(frame);
3242         return status;
3243 }
3244
3245 /****************************************************************************
3246  POSIX Unlock a file.
3247 ****************************************************************************/
3248
3249 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3250                                         struct tevent_context *ev,
3251                                         struct cli_state *cli,
3252                                         uint16_t fnum,
3253                                         uint64_t offset,
3254                                         uint64_t len)
3255 {
3256         return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3257                                         false, UNLOCK_LOCK);
3258 }
3259
3260 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3261 {
3262         return tevent_req_simple_recv_ntstatus(req);
3263 }
3264
3265 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3266 {
3267         TALLOC_CTX *frame = talloc_stackframe();
3268         struct tevent_context *ev = NULL;
3269         struct tevent_req *req = NULL;
3270         NTSTATUS status = NT_STATUS_OK;
3271
3272         if (smbXcli_conn_has_async_calls(cli->conn)) {
3273                 /*
3274                  * Can't use sync call while an async call is in flight
3275                  */
3276                 status = NT_STATUS_INVALID_PARAMETER;
3277                 goto fail;
3278         }
3279
3280         ev = samba_tevent_context_init(frame);
3281         if (ev == NULL) {
3282                 status = NT_STATUS_NO_MEMORY;
3283                 goto fail;
3284         }
3285
3286         req = cli_posix_unlock_send(frame,
3287                                 ev,
3288                                 cli,
3289                                 fnum,
3290                                 offset,
3291                                 len);
3292         if (req == NULL) {
3293                 status = NT_STATUS_NO_MEMORY;
3294                 goto fail;
3295         }
3296
3297         if (!tevent_req_poll(req, ev)) {
3298                 status = map_nt_error_from_unix(errno);
3299                 goto fail;
3300         }
3301
3302         status = cli_posix_unlock_recv(req);
3303
3304  fail:
3305         TALLOC_FREE(frame);
3306         return status;
3307 }
3308
3309 /****************************************************************************
3310  Do a SMBgetattrE call.
3311 ****************************************************************************/
3312
3313 static void cli_getattrE_done(struct tevent_req *subreq);
3314
3315 struct cli_getattrE_state {
3316         uint16_t vwv[1];
3317         int zone_offset;
3318         uint16_t attr;
3319         off_t size;
3320         time_t change_time;
3321         time_t access_time;
3322         time_t write_time;
3323 };
3324
3325 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3326                                 struct tevent_context *ev,
3327                                 struct cli_state *cli,
3328                                 uint16_t fnum)
3329 {
3330         struct tevent_req *req = NULL, *subreq = NULL;
3331         struct cli_getattrE_state *state = NULL;
3332         uint8_t additional_flags = 0;
3333
3334         req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3335         if (req == NULL) {
3336                 return NULL;
3337         }
3338
3339         state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3340         SSVAL(state->vwv+0,0,fnum);
3341
3342         subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3343                               1, state->vwv, 0, NULL);
3344         if (tevent_req_nomem(subreq, req)) {
3345                 return tevent_req_post(req, ev);
3346         }
3347         tevent_req_set_callback(subreq, cli_getattrE_done, req);
3348         return req;
3349 }
3350
3351 static void cli_getattrE_done(struct tevent_req *subreq)
3352 {
3353         struct tevent_req *req = tevent_req_callback_data(
3354                 subreq, struct tevent_req);
3355         struct cli_getattrE_state *state = tevent_req_data(
3356                 req, struct cli_getattrE_state);
3357         uint8_t wct;
3358         uint16_t *vwv = NULL;
3359         NTSTATUS status;
3360
3361         status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3362                               NULL, NULL);
3363         TALLOC_FREE(subreq);
3364         if (tevent_req_nterror(req, status)) {
3365                 return;
3366         }
3367
3368         state->size = (off_t)IVAL(vwv+6,0);
3369         state->attr = SVAL(vwv+10,0);
3370         state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3371         state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3372         state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3373
3374         tevent_req_done(req);
3375 }
3376
3377 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3378                         uint16_t *attr,
3379                         off_t *size,
3380                         time_t *change_time,
3381                         time_t *access_time,
3382                         time_t *write_time)
3383 {
3384         struct cli_getattrE_state *state = tevent_req_data(
3385                                 req, struct cli_getattrE_state);
3386         NTSTATUS status;
3387
3388         if (tevent_req_is_nterror(req, &status)) {
3389                 return status;
3390         }
3391         if (attr) {
3392                 *attr = state->attr;
3393         }
3394         if (size) {
3395                 *size = state->size;
3396         }
3397         if (change_time) {
3398                 *change_time = state->change_time;
3399         }
3400         if (access_time) {
3401                 *access_time = state->access_time;
3402         }
3403         if (write_time) {
3404                 *write_time = state->write_time;
3405         }
3406         return NT_STATUS_OK;
3407 }
3408
3409 NTSTATUS cli_getattrE(struct cli_state *cli,
3410                         uint16_t fnum,
3411                         uint16_t *attr,
3412                         off_t *size,
3413                         time_t *change_time,
3414                         time_t *access_time,
3415                         time_t *write_time)
3416 {
3417         TALLOC_CTX *frame = NULL;
3418         struct tevent_context *ev = NULL;
3419         struct tevent_req *req = NULL;
3420         NTSTATUS status = NT_STATUS_OK;
3421
3422         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3423                 return cli_smb2_getattrE(cli,
3424                                         fnum,
3425                                         attr,
3426                                         size,
3427                                         change_time,
3428                                         access_time,
3429                                         write_time);
3430         }
3431
3432         frame = talloc_stackframe();
3433
3434         if (smbXcli_conn_has_async_calls(cli->conn)) {
3435                 /*
3436                  * Can't use sync call while an async call is in flight
3437                  */
3438                 status = NT_STATUS_INVALID_PARAMETER;
3439                 goto fail;
3440         }
3441
3442         ev = samba_tevent_context_init(frame);
3443         if (ev == NULL) {
3444                 status = NT_STATUS_NO_MEMORY;
3445                 goto fail;
3446         }
3447
3448         req = cli_getattrE_send(frame, ev, cli, fnum);
3449         if (req == NULL) {
3450                 status = NT_STATUS_NO_MEMORY;
3451                 goto fail;
3452         }
3453
3454         if (!tevent_req_poll(req, ev)) {
3455                 status = map_nt_error_from_unix(errno);
3456                 goto fail;
3457         }
3458
3459         status = cli_getattrE_recv(req,
3460                                         attr,
3461                                         size,
3462                                         change_time,
3463                                         access_time,
3464                                         write_time);
3465
3466  fail:
3467         TALLOC_FREE(frame);
3468         return status;
3469 }
3470
3471 /****************************************************************************
3472  Do a SMBgetatr call
3473 ****************************************************************************/
3474
3475 static void cli_getatr_done(struct tevent_req *subreq);
3476
3477 struct cli_getatr_state {
3478         int zone_offset;
3479         uint16_t attr;
3480         off_t size;
3481         time_t write_time;
3482 };
3483
3484 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3485                                 struct tevent_context *ev,
3486                                 struct cli_state *cli,
3487                                 const char *fname)
3488 {
3489         struct tevent_req *req = NULL, *subreq = NULL;
3490         struct cli_getatr_state *state = NULL;
3491         uint8_t additional_flags = 0;
3492         uint8_t *bytes = NULL;
3493
3494         req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3495         if (req == NULL) {
3496                 return NULL;
3497         }
3498
3499         state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3500
3501         bytes = talloc_array(state, uint8_t, 1);
3502         if (tevent_req_nomem(bytes, req)) {
3503                 return tevent_req_post(req, ev);
3504         }
3505         bytes[0] = 4;
3506         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3507                                    strlen(fname)+1, NULL);
3508
3509         if (tevent_req_nomem(bytes, req)) {
3510                 return tevent_req_post(req, ev);
3511         }
3512
3513         subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3514                               0, NULL, talloc_get_size(bytes), bytes);
3515         if (tevent_req_nomem(subreq, req)) {
3516                 return tevent_req_post(req, ev);
3517         }
3518         tevent_req_set_callback(subreq, cli_getatr_done, req);
3519         return req;
3520 }
3521
3522 static void cli_getatr_done(struct tevent_req *subreq)
3523 {
3524         struct tevent_req *req = tevent_req_callback_data(
3525                 subreq, struct tevent_req);
3526         struct cli_getatr_state *state = tevent_req_data(
3527                 req, struct cli_getatr_state);
3528         uint8_t wct;
3529         uint16_t *vwv = NULL;
3530         NTSTATUS status;
3531
3532         status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3533                               NULL);
3534         TALLOC_FREE(subreq);
3535         if (tevent_req_nterror(req, status)) {
3536                 return;
3537         }
3538
3539         state->attr = SVAL(vwv+0,0);
3540         state->size = (off_t)IVAL(vwv+3,0);
3541         state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3542
3543         tevent_req_done(req);
3544 }
3545
3546 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3547                         uint16_t *attr,
3548                         off_t *size,
3549                         time_t *write_time)
3550 {
3551         struct cli_getatr_state *state = tevent_req_data(
3552                                 req, struct cli_getatr_state);
3553         NTSTATUS status;
3554
3555         if (tevent_req_is_nterror(req, &status)) {
3556                 return status;
3557         }
3558         if (attr) {
3559                 *attr = state->attr;
3560         }
3561         if (size) {
3562                 *size = state->size;
3563         }
3564         if (write_time) {
3565                 *write_time = state->write_time;
3566         }
3567         return NT_STATUS_OK;
3568 }
3569
3570 NTSTATUS cli_getatr(struct cli_state *cli,
3571                         const char *fname,
3572                         uint16_t *attr,
3573                         off_t *size,
3574                         time_t *write_time)
3575 {
3576         TALLOC_CTX *frame = NULL;
3577         struct tevent_context *ev = NULL;
3578         struct tevent_req *req = NULL;
3579         NTSTATUS status = NT_STATUS_OK;
3580
3581         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3582                 return cli_smb2_getatr(cli,
3583                                         fname,
3584                                         attr,
3585                                         size,
3586                                         write_time);
3587         }
3588
3589         frame = talloc_stackframe();
3590
3591         if (smbXcli_conn_has_async_calls(cli->conn)) {
3592                 /*
3593                  * Can't use sync call while an async call is in flight
3594                  */
3595                 status = NT_STATUS_INVALID_PARAMETER;
3596                 goto fail;
3597         }
3598
3599         ev = samba_tevent_context_init(frame);
3600         if (ev == NULL) {
3601                 status = NT_STATUS_NO_MEMORY;
3602                 goto fail;
3603         }
3604
3605         req = cli_getatr_send(frame, ev, cli, fname);
3606         if (req == NULL) {
3607                 status = NT_STATUS_NO_MEMORY;
3608                 goto fail;
3609         }
3610
3611         if (!tevent_req_poll(req, ev)) {
3612                 status = map_nt_error_from_unix(errno);
3613                 goto fail;
3614         }
3615
3616         status = cli_getatr_recv(req,
3617                                 attr,
3618                                 size,
3619                                 write_time);
3620
3621  fail:
3622         TALLOC_FREE(frame);
3623         return status;
3624 }
3625
3626 /****************************************************************************
3627  Do a SMBsetattrE call.
3628 ****************************************************************************/
3629
3630 static void cli_setattrE_done(struct tevent_req *subreq);
3631
3632 struct cli_setattrE_state {
3633         uint16_t vwv[7];
3634 };
3635
3636 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3637                                 struct tevent_context *ev,
3638                                 struct cli_state *cli,
3639                                 uint16_t fnum,
3640                                 time_t change_time,
3641                                 time_t access_time,
3642                                 time_t write_time)
3643 {
3644         struct tevent_req *req = NULL, *subreq = NULL;
3645         struct cli_setattrE_state *state = NULL;
3646         uint8_t additional_flags = 0;
3647
3648         req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3649         if (req == NULL) {
3650                 return NULL;
3651         }
3652
3653         SSVAL(state->vwv+0, 0, fnum);
3654         push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3655                        smb1cli_conn_server_time_zone(cli->conn));
3656         push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3657                        smb1cli_conn_server_time_zone(cli->conn));
3658         push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3659                        smb1cli_conn_server_time_zone(cli->conn));
3660
3661         subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3662                               7, state->vwv, 0, NULL);
3663         if (tevent_req_nomem(subreq, req)) {
3664                 return tevent_req_post(req, ev);
3665         }
3666         tevent_req_set_callback(subreq, cli_setattrE_done, req);
3667         return req;
3668 }
3669
3670 static void cli_setattrE_done(struct tevent_req *subreq)
3671 {
3672         struct tevent_req *req = tevent_req_callback_data(
3673                 subreq, struct tevent_req);
3674         NTSTATUS status;
3675
3676         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3677         TALLOC_FREE(subreq);
3678         if (tevent_req_nterror(req, status)) {
3679                 return;
3680         }
3681         tevent_req_done(req);
3682 }
3683
3684 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3685 {
3686         return tevent_req_simple_recv_ntstatus(req);
3687 }
3688
3689 NTSTATUS cli_setattrE(struct cli_state *cli,
3690                         uint16_t fnum,
3691                         time_t change_time,
3692                         time_t access_time,
3693                         time_t write_time)
3694 {
3695         TALLOC_CTX *frame = NULL;
3696         struct tevent_context *ev = NULL;
3697         struct tevent_req *req = NULL;
3698         NTSTATUS status = NT_STATUS_OK;
3699
3700         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3701                 return cli_smb2_setattrE(cli,
3702                                         fnum,
3703                                         change_time,
3704                                         access_time,
3705                                         write_time);
3706         }
3707
3708         frame = talloc_stackframe();
3709
3710         if (smbXcli_conn_has_async_calls(cli->conn)) {
3711                 /*
3712                  * Can't use sync call while an async call is in flight
3713                  */
3714                 status = NT_STATUS_INVALID_PARAMETER;
3715                 goto fail;
3716         }
3717
3718         ev = samba_tevent_context_init(frame);
3719         if (ev == NULL) {
3720                 status = NT_STATUS_NO_MEMORY;
3721                 goto fail;
3722         }
3723
3724         req = cli_setattrE_send(frame, ev,
3725                         cli,
3726                         fnum,
3727                         change_time,
3728                         access_time,
3729                         write_time);
3730
3731         if (req == NULL) {
3732                 status = NT_STATUS_NO_MEMORY;
3733                 goto fail;
3734         }
3735
3736         if (!tevent_req_poll(req, ev)) {
3737                 status = map_nt_error_from_unix(errno);
3738                 goto fail;
3739         }
3740
3741         status = cli_setattrE_recv(req);
3742
3743  fail:
3744         TALLOC_FREE(frame);
3745         return status;
3746 }
3747
3748 /****************************************************************************
3749  Do a SMBsetatr call.
3750 ****************************************************************************/
3751
3752 static void cli_setatr_done(struct tevent_req *subreq);
3753
3754 struct cli_setatr_state {
3755         uint16_t vwv[8];
3756 };
3757
3758 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3759                                 struct tevent_context *ev,
3760                                 struct cli_state *cli,
3761                                 const char *fname,
3762                                 uint16_t attr,
3763                                 time_t mtime)
3764 {
3765         struct tevent_req *req = NULL, *subreq = NULL;
3766         struct cli_setatr_state *state = NULL;
3767         uint8_t additional_flags = 0;
3768         uint8_t *bytes = NULL;
3769
3770         req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3771         if (req == NULL) {
3772                 return NULL;
3773         }
3774
3775         SSVAL(state->vwv+0, 0, attr);
3776         push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3777
3778         bytes = talloc_array(state, uint8_t, 1);
3779         if (tevent_req_nomem(bytes, req)) {
3780                 return tevent_req_post(req, ev);
3781         }
3782         bytes[0] = 4;
3783         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3784                                    strlen(fname)+1, NULL);
3785         if (tevent_req_nomem(bytes, req)) {
3786                 return tevent_req_post(req, ev);
3787         }
3788         bytes = talloc_realloc(state, bytes, uint8_t,
3789                         talloc_get_size(bytes)+1);
3790         if (tevent_req_nomem(bytes, req)) {
3791                 return tevent_req_post(req, ev);
3792         }
3793
3794         bytes[talloc_get_size(bytes)-1] = 4;
3795         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3796                                    1, NULL);
3797         if (tevent_req_nomem(bytes, req)) {
3798                 return tevent_req_post(req, ev);
3799         }
3800
3801         subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3802                               8, state->vwv, talloc_get_size(bytes), bytes);
3803         if (tevent_req_nomem(subreq, req)) {
3804                 return tevent_req_post(req, ev);
3805         }
3806         tevent_req_set_callback(subreq, cli_setatr_done, req);
3807         return req;
3808 }
3809
3810 static void cli_setatr_done(struct tevent_req *subreq)
3811 {
3812         struct tevent_req *req = tevent_req_callback_data(
3813                 subreq, struct tevent_req);
3814         NTSTATUS status;
3815
3816         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3817         TALLOC_FREE(subreq);
3818         if (tevent_req_nterror(req, status)) {
3819                 return;
3820         }
3821         tevent_req_done(req);
3822 }
3823
3824 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3825 {
3826         return tevent_req_simple_recv_ntstatus(req);
3827 }
3828
3829 NTSTATUS cli_setatr(struct cli_state *cli,
3830                 const char *fname,
3831                 uint16_t attr,
3832                 time_t mtime)
3833 {
3834         TALLOC_CTX *frame = NULL;
3835         struct tevent_context *ev = NULL;
3836         struct tevent_req *req = NULL;
3837         NTSTATUS status = NT_STATUS_OK;
3838
3839         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3840                 return cli_smb2_setatr(cli,
3841                                         fname,
3842                                         attr,
3843                                         mtime);
3844         }
3845
3846         frame = talloc_stackframe();
3847
3848         if (smbXcli_conn_has_async_calls(cli->conn)) {
3849                 /*
3850                  * Can't use sync call while an async call is in flight
3851                  */
3852                 status = NT_STATUS_INVALID_PARAMETER;
3853                 goto fail;
3854         }
3855
3856         ev = samba_tevent_context_init(frame);
3857         if (ev == NULL) {
3858                 status = NT_STATUS_NO_MEMORY;
3859                 goto fail;
3860         }
3861
3862         req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3863         if (req == NULL) {
3864                 status = NT_STATUS_NO_MEMORY;
3865                 goto fail;
3866         }
3867
3868         if (!tevent_req_poll(req, ev)) {
3869                 status = map_nt_error_from_unix(errno);
3870                 goto fail;
3871         }
3872
3873         status = cli_setatr_recv(req);
3874
3875  fail:
3876         TALLOC_FREE(frame);
3877         return status;
3878 }
3879
3880 /****************************************************************************
3881  Check for existance of a dir.
3882 ****************************************************************************/
3883
3884 static void cli_chkpath_done(struct tevent_req *subreq);
3885
3886 struct cli_chkpath_state {
3887         int dummy;
3888 };
3889
3890 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3891                                   struct tevent_context *ev,
3892                                   struct cli_state *cli,
3893                                   const char *fname)
3894 {
3895         struct tevent_req *req = NULL, *subreq = NULL;
3896         struct cli_chkpath_state *state = NULL;
3897         uint8_t additional_flags = 0;
3898         uint8_t *bytes = NULL;
3899
3900         req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3901         if (req == NULL) {
3902                 return NULL;
3903         }
3904
3905         bytes = talloc_array(state, uint8_t, 1);
3906         if (tevent_req_nomem(bytes, req)) {
3907                 return tevent_req_post(req, ev);
3908         }
3909         bytes[0] = 4;
3910         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3911                                    strlen(fname)+1, NULL);
3912
3913         if (tevent_req_nomem(bytes, req)) {
3914                 return tevent_req_post(req, ev);
3915         }
3916
3917         subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3918                               0, NULL, talloc_get_size(bytes), bytes);
3919         if (tevent_req_nomem(subreq, req)) {
3920                 return tevent_req_post(req, ev);
3921         }
3922         tevent_req_set_callback(subreq, cli_chkpath_done, req);
3923         return req;
3924 }
3925
3926 static void cli_chkpath_done(struct tevent_req *subreq)
3927 {
3928         struct tevent_req *req = tevent_req_callback_data(
3929                 subreq, struct tevent_req);
3930         NTSTATUS status;
3931
3932         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3933         TALLOC_FREE(subreq);
3934         if (tevent_req_nterror(req, status)) {
3935                 return;
3936         }
3937         tevent_req_done(req);
3938 }
3939
3940 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3941 {
3942         return tevent_req_simple_recv_ntstatus(req);
3943 }
3944
3945 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3946 {
3947         TALLOC_CTX *frame = talloc_stackframe();
3948         struct tevent_context *ev = NULL;
3949         struct tevent_req *req = NULL;
3950         char *path2 = NULL;
3951         NTSTATUS status = NT_STATUS_OK;
3952
3953         if (smbXcli_conn_has_async_calls(cli->conn)) {
3954                 /*
3955                  * Can't use sync call while an async call is in flight
3956                  */
3957                 status = NT_STATUS_INVALID_PARAMETER;
3958                 goto fail;
3959         }
3960
3961         path2 = talloc_strdup(frame, path);
3962         if (!path2) {
3963                 status = NT_STATUS_NO_MEMORY;
3964                 goto fail;
3965         }
3966         trim_char(path2,'\0','\\');
3967         if (!*path2) {
3968                 path2 = talloc_strdup(frame, "\\");
3969                 if (!path2) {
3970                         status = NT_STATUS_NO_MEMORY;
3971                         goto fail;
3972                 }
3973         }
3974
3975         ev = samba_tevent_context_init(frame);
3976         if (ev == NULL) {
3977                 status = NT_STATUS_NO_MEMORY;
3978                 goto fail;
3979         }
3980
3981         req = cli_chkpath_send(frame, ev, cli, path2);
3982         if (req == NULL) {
3983                 status = NT_STATUS_NO_MEMORY;
3984                 goto fail;
3985         }
3986
3987         if (!tevent_req_poll(req, ev)) {
3988                 status = map_nt_error_from_unix(errno);
3989                 goto fail;
3990         }
3991
3992         status = cli_chkpath_recv(req);
3993
3994  fail:
3995         TALLOC_FREE(frame);
3996         return status;
3997 }
3998
3999 /****************************************************************************
4000  Query disk space.
4001 ****************************************************************************/
4002
4003 static void cli_dskattr_done(struct tevent_req *subreq);
4004
4005 struct cli_dskattr_state {
4006         int bsize;
4007         int total;
4008         int avail;
4009 };
4010
4011 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4012                                   struct tevent_context *ev,
4013                                   struct cli_state *cli)
4014 {
4015         struct tevent_req *req = NULL, *subreq = NULL;
4016         struct cli_dskattr_state *state = NULL;
4017         uint8_t additional_flags = 0;
4018
4019         req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4020         if (req == NULL) {
4021                 return NULL;
4022         }
4023
4024         subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
4025                               0, NULL, 0, NULL);
4026         if (tevent_req_nomem(subreq, req)) {
4027                 return tevent_req_post(req, ev);
4028         }
4029         tevent_req_set_callback(subreq, cli_dskattr_done, req);
4030         return req;
4031 }
4032
4033 static void cli_dskattr_done(struct tevent_req *subreq)
4034 {
4035         struct tevent_req *req = tevent_req_callback_data(
4036                 subreq, struct tevent_req);
4037         struct cli_dskattr_state *state = tevent_req_data(
4038                 req, struct cli_dskattr_state);
4039         uint8_t wct;
4040         uint16_t *vwv = NULL;
4041         NTSTATUS status;
4042
4043         status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4044                               NULL);
4045         TALLOC_FREE(subreq);
4046         if (tevent_req_nterror(req, status)) {
4047                 return;
4048         }
4049         state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4050         state->total = SVAL(vwv+0, 0);
4051         state->avail = SVAL(vwv+3, 0);
4052         tevent_req_done(req);
4053 }
4054
4055 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4056 {
4057         struct cli_dskattr_state *state = tevent_req_data(
4058                                 req, struct cli_dskattr_state);
4059         NTSTATUS status;
4060
4061         if (tevent_req_is_nterror(req, &status)) {
4062                 return status;
4063         }
4064         *bsize = state->bsize;
4065         *total = state->total;
4066         *avail = state->avail;
4067         return NT_STATUS_OK;
4068 }
4069
4070 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4071 {
4072         TALLOC_CTX *frame = NULL;
4073         struct tevent_context *ev = NULL;
4074         struct tevent_req *req = NULL;
4075         NTSTATUS status = NT_STATUS_OK;
4076
4077         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4078                 return cli_smb2_dskattr(cli, bsize, total, avail);
4079         }
4080
4081         frame = talloc_stackframe();
4082
4083         if (smbXcli_conn_has_async_calls(cli->conn)) {
4084                 /*
4085                  * Can't use sync call while an async call is in flight
4086                  */
4087                 status = NT_STATUS_INVALID_PARAMETER;
4088                 goto fail;
4089         }
4090
4091         ev = samba_tevent_context_init(frame);
4092         if (ev == NULL) {
4093                 status = NT_STATUS_NO_MEMORY;
4094                 goto fail;
4095         }
4096
4097         req = cli_dskattr_send(frame, ev, cli);
4098         if (req == NULL) {
4099                 status = NT_STATUS_NO_MEMORY;
4100                 goto fail;
4101         }
4102
4103         if (!tevent_req_poll(req, ev)) {
4104                 status = map_nt_error_from_unix(errno);
4105                 goto fail;
4106         }
4107
4108         status = cli_dskattr_recv(req, bsize, total, avail);
4109
4110  fail:
4111         TALLOC_FREE(frame);
4112         return status;
4113 }
4114
4115 /****************************************************************************
4116  Create and open a temporary file.
4117 ****************************************************************************/
4118
4119 static void cli_ctemp_done(struct tevent_req *subreq);
4120
4121 struct ctemp_state {
4122         uint16_t vwv[3];
4123         char *ret_path;
4124         uint16_t fnum;
4125 };
4126
4127 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4128                                 struct tevent_context *ev,
4129                                 struct cli_state *cli,
4130                                 const char *path)
4131 {
4132         struct tevent_req *req = NULL, *subreq = NULL;
4133         struct ctemp_state *state = NULL;
4134         uint8_t additional_flags = 0;
4135         uint8_t *bytes = NULL;
4136
4137         req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4138         if (req == NULL) {
4139                 return NULL;
4140         }
4141
4142         SSVAL(state->vwv,0,0);
4143         SIVALS(state->vwv+1,0,-1);
4144
4145         bytes = talloc_array(state, uint8_t, 1);
4146         if (tevent_req_nomem(bytes, req)) {
4147                 return tevent_req_post(req, ev);
4148         }
4149         bytes[0] = 4;
4150         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4151                                    strlen(path)+1, NULL);
4152         if (tevent_req_nomem(bytes, req)) {
4153                 return tevent_req_post(req, ev);
4154         }
4155
4156         subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4157                               3, state->vwv, talloc_get_size(bytes), bytes);
4158         if (tevent_req_nomem(subreq, req)) {
4159                 return tevent_req_post(req, ev);
4160         }
4161         tevent_req_set_callback(subreq, cli_ctemp_done, req);
4162         return req;
4163 }
4164
4165 static void cli_ctemp_done(struct tevent_req *subreq)
4166 {
4167         struct tevent_req *req = tevent_req_callback_data(
4168                                 subreq, struct tevent_req);
4169         struct ctemp_state *state = tevent_req_data(
4170                                 req, struct ctemp_state);
4171         NTSTATUS status;
4172         uint8_t wcnt;
4173         uint16_t *vwv;
4174         uint32_t num_bytes = 0;
4175         uint8_t *bytes = NULL;
4176
4177         status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4178                               &num_bytes, &bytes);
4179         TALLOC_FREE(subreq);
4180         if (tevent_req_nterror(req, status)) {
4181                 return;
4182         }
4183
4184         state->fnum = SVAL(vwv+0, 0);
4185
4186         /* From W2K3, the result is just the ASCII name */
4187         if (num_bytes < 2) {
4188                 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4189                 return;
4190         }
4191
4192         if (pull_string_talloc(state,
4193                         NULL,
4194                         0,
4195                         &state->ret_path,
4196                         bytes,
4197                         num_bytes,
4198                         STR_ASCII) == 0) {
4199                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4200                 return;
4201         }
4202         tevent_req_done(req);
4203 }
4204
4205 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4206                         TALLOC_CTX *ctx,
4207                         uint16_t *pfnum,
4208                         char **outfile)
4209 {
4210         struct ctemp_state *state = tevent_req_data(req,
4211                         struct ctemp_state);
4212         NTSTATUS status;
4213
4214         if (tevent_req_is_nterror(req, &status)) {
4215                 return status;
4216         }
4217         *pfnum = state->fnum;
4218         *outfile = talloc_strdup(ctx, state->ret_path);
4219         if (!*outfile) {
4220                 return NT_STATUS_NO_MEMORY;
4221         }
4222         return NT_STATUS_OK;
4223 }
4224
4225 NTSTATUS cli_ctemp(struct cli_state *cli,
4226                         TALLOC_CTX *ctx,
4227                         const char *path,
4228                         uint16_t *pfnum,
4229                         char **out_path)
4230 {
4231         TALLOC_CTX *frame = talloc_stackframe();
4232         struct tevent_context *ev;
4233         struct tevent_req *req;
4234         NTSTATUS status = NT_STATUS_OK;
4235
4236         if (smbXcli_conn_has_async_calls(cli->conn)) {
4237                 /*
4238                  * Can't use sync call while an async call is in flight
4239                  */
4240                 status = NT_STATUS_INVALID_PARAMETER;
4241                 goto fail;
4242         }
4243
4244         ev = samba_tevent_context_init(frame);
4245         if (ev == NULL) {
4246                 status = NT_STATUS_NO_MEMORY;
4247                 goto fail;
4248         }
4249
4250         req = cli_ctemp_send(frame, ev, cli, path);
4251         if (req == NULL) {
4252                 status = NT_STATUS_NO_MEMORY;
4253                 goto fail;
4254         }
4255
4256         if (!tevent_req_poll(req, ev)) {
4257                 status = map_nt_error_from_unix(errno);
4258                 goto fail;
4259         }
4260
4261         status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4262
4263  fail:
4264         TALLOC_FREE(frame);
4265         return status;
4266 }
4267
4268 /*
4269    send a raw ioctl - used by the torture code
4270 */
4271 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4272 {
4273         uint16_t vwv[3];
4274         NTSTATUS status;
4275
4276         SSVAL(vwv+0, 0, fnum);
4277         SSVAL(vwv+1, 0, code>>16);
4278         SSVAL(vwv+2, 0, (code&0xFFFF));
4279
4280         status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4281                          NULL, 0, NULL, NULL, NULL, NULL);
4282         if (!NT_STATUS_IS_OK(status)) {
4283                 return status;
4284         }
4285         *blob = data_blob_null;
4286         return NT_STATUS_OK;
4287 }
4288
4289 /*********************************************************
4290  Set an extended attribute utility fn.
4291 *********************************************************/
4292
4293 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4294                            uint8_t *param, unsigned int param_len,
4295                            const char *ea_name,
4296                            const char *ea_val, size_t ea_len)
4297 {
4298         uint16_t setup[1];
4299         unsigned int data_len = 0;
4300         uint8_t *data = NULL;
4301         char *p;
4302         size_t ea_namelen = strlen(ea_name);
4303         NTSTATUS status;
4304
4305         SSVAL(setup, 0, setup_val);
4306
4307         if (ea_namelen == 0 && ea_len == 0) {
4308                 data_len = 4;
4309                 data = talloc_array(talloc_tos(),
4310                                 uint8_t,
4311                                 data_len);
4312                 if (!data) {
4313                         return NT_STATUS_NO_MEMORY;
4314                 }
4315                 p = (char *)data;
4316                 SIVAL(p,0,data_len);
4317         } else {
4318                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4319                 data = talloc_array(talloc_tos(),
4320                                 uint8_t,
4321                                 data_len);
4322                 if (!data) {
4323                         return NT_STATUS_NO_MEMORY;
4324                 }
4325                 p = (char *)data;
4326                 SIVAL(p,0,data_len);
4327                 p += 4;
4328                 SCVAL(p, 0, 0); /* EA flags. */
4329                 SCVAL(p, 1, ea_namelen);
4330                 SSVAL(p, 2, ea_len);
4331                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4332                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4333         }
4334
4335         status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4336                            setup, 1, 0,
4337                            param, param_len, 2,
4338                            data,  data_len, CLI_BUFFER_SIZE,
4339                            NULL,
4340                            NULL, 0, NULL, /* rsetup */
4341                            NULL, 0, NULL, /* rparam */
4342                            NULL, 0, NULL); /* rdata */
4343         talloc_free(data);
4344         return status;
4345 }
4346
4347 /*********************************************************
4348  Set an extended attribute on a pathname.
4349 *********************************************************/
4350
4351 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4352                          const char *ea_name, const char *ea_val,
4353                          size_t ea_len)
4354 {
4355         unsigned int param_len = 0;
4356         uint8_t *param;
4357         NTSTATUS status;
4358         TALLOC_CTX *frame = NULL;
4359
4360         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4361                 return cli_smb2_set_ea_path(cli,
4362                                         path,
4363                                         ea_name,
4364                                         ea_val,
4365                                         ea_len);
4366         }
4367
4368         frame = talloc_stackframe();
4369
4370         param = talloc_array(frame, uint8_t, 6);
4371         if (!param) {
4372                 status = NT_STATUS_NO_MEMORY;
4373                 goto fail;
4374         }
4375         SSVAL(param,0,SMB_INFO_SET_EA);
4376         SSVAL(param,2,0);
4377         SSVAL(param,4,0);
4378
4379         param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4380                                       path, strlen(path)+1,
4381                                       NULL);
4382         param_len = talloc_get_size(param);
4383
4384         status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4385                             ea_name, ea_val, ea_len);
4386
4387   fail:
4388
4389         TALLOC_FREE(frame);
4390         return status;
4391 }
4392
4393 /*********************************************************
4394  Set an extended attribute on an fnum.
4395 *********************************************************/
4396
4397 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4398                          const char *ea_name, const char *ea_val,
4399                          size_t ea_len)
4400 {
4401         uint8_t param[6];
4402
4403         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4404                 return cli_smb2_set_ea_fnum(cli,
4405                                         fnum,
4406                                         ea_name,
4407                                         ea_val,
4408                                         ea_len);
4409         }
4410
4411         memset(param, 0, 6);
4412         SSVAL(param,0,fnum);
4413         SSVAL(param,2,SMB_INFO_SET_EA);
4414
4415         return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4416                           ea_name, ea_val, ea_len);
4417 }
4418
4419 /*********************************************************
4420  Get an extended attribute list utility fn.
4421 *********************************************************/
4422
4423 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4424                           size_t rdata_len,
4425                           size_t *pnum_eas, struct ea_struct **pea_list)
4426 {
4427         struct ea_struct *ea_list = NULL;
4428         size_t num_eas;
4429         size_t ea_size;
4430         const uint8_t *p;
4431
4432         if (rdata_len < 4) {
4433                 return false;
4434         }
4435
4436         ea_size = (size_t)IVAL(rdata,0);
4437         if (ea_size > rdata_len) {
4438                 return false;
4439         }
4440
4441         if (ea_size == 0) {
4442                 /* No EA's present. */
4443                 *pnum_eas = 0;
4444                 *pea_list = NULL;
4445                 return true;
4446         }
4447
4448         p = rdata + 4;
4449         ea_size -= 4;
4450
4451         /* Validate the EA list and count it. */
4452         for (num_eas = 0; ea_size >= 4; num_eas++) {
4453                 unsigned int ea_namelen = CVAL(p,1);
4454                 unsigned int ea_valuelen = SVAL(p,2);
4455                 if (ea_namelen == 0) {
4456                         return false;
4457                 }
4458                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4459                         return false;
4460                 }
4461                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4462                 p += 4 + ea_namelen + 1 + ea_valuelen;
4463         }
4464
4465         if (num_eas == 0) {
4466                 *pnum_eas = 0;
4467                 *pea_list = NULL;
4468                 return true;
4469         }
4470
4471         *pnum_eas = num_eas;
4472         if (!pea_list) {
4473                 /* Caller only wants number of EA's. */
4474                 return true;
4475         }
4476
4477         ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4478         if (!ea_list) {
4479                 return false;
4480         }
4481
4482         ea_size = (size_t)IVAL(rdata,0);
4483         p = rdata + 4;
4484
4485         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4486                 struct ea_struct *ea = &ea_list[num_eas];
4487                 fstring unix_ea_name;
4488                 unsigned int ea_namelen = CVAL(p,1);
4489                 unsigned int ea_valuelen = SVAL(p,2);
4490
4491                 ea->flags = CVAL(p,0);
4492                 unix_ea_name[0] = '\0';
4493                 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4494                 ea->name = talloc_strdup(ea_list, unix_ea_name);
4495                 if (!ea->name) {
4496                         goto fail;
4497                 }
4498                 /* Ensure the value is null terminated (in case it's a string). */
4499                 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4500                 if (!ea->value.data) {
4501                         goto fail;
4502                 }
4503                 if (ea_valuelen) {
4504                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4505                 }
4506                 ea->value.data[ea_valuelen] = 0;
4507                 ea->value.length--;
4508                 p += 4 + ea_namelen + 1 + ea_valuelen;
4509         }
4510
4511         *pea_list = ea_list;
4512         return true;
4513
4514 fail:
4515         TALLOC_FREE(ea_list);
4516         return false;
4517 }
4518
4519 /*********************************************************
4520  Get an extended attribute list from a pathname.
4521 *********************************************************/
4522
4523 struct cli_get_ea_list_path_state {
4524         uint32_t num_data;
4525         uint8_t *data;
4526 };
4527
4528 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4529
4530 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4531                                              struct tevent_context *ev,
4532                                              struct cli_state *cli,
4533                                              const char *fname)
4534 {
4535         struct tevent_req *req, *subreq;
4536         struct cli_get_ea_list_path_state *state;
4537
4538         req = tevent_req_create(mem_ctx, &state,
4539                                 struct cli_get_ea_list_path_state);
4540         if (req == NULL) {
4541                 return NULL;
4542         }
4543         subreq = cli_qpathinfo_send(state, ev, cli, fname,
4544                                     SMB_INFO_QUERY_ALL_EAS, 4,
4545                                     CLI_BUFFER_SIZE);
4546         if (tevent_req_nomem(subreq, req)) {
4547                 return tevent_req_post(req, ev);
4548         }
4549         tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4550         return req;
4551 }
4552
4553 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4554 {
4555         struct tevent_req *req = tevent_req_callback_data(
4556                                 subreq, struct tevent_req);
4557         struct cli_get_ea_list_path_state *state = tevent_req_data(
4558                 req, struct cli_get_ea_list_path_state);
4559         NTSTATUS status;
4560
4561         status = cli_qpathinfo_recv(subreq, state, &state->data,
4562                                     &state->num_data);
4563         TALLOC_FREE(subreq);
4564         if (tevent_req_nterror(req, status)) {
4565                 return;
4566         }
4567         tevent_req_done(req);
4568 }
4569
4570 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4571                                    size_t *pnum_eas, struct ea_struct **peas)
4572 {
4573         struct cli_get_ea_list_path_state *state = tevent_req_data(
4574                 req, struct cli_get_ea_list_path_state);
4575         NTSTATUS status;
4576
4577         if (tevent_req_is_nterror(req, &status)) {
4578                 return status;
4579         }
4580         if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4581                            pnum_eas, peas)) {
4582                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4583         }
4584         return NT_STATUS_OK;
4585 }
4586
4587 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4588                 TALLOC_CTX *ctx,
4589                 size_t *pnum_eas,
4590                 struct ea_struct **pea_list)
4591 {
4592         TALLOC_CTX *frame = NULL;
4593         struct tevent_context *ev = NULL;
4594         struct tevent_req *req = NULL;
4595         NTSTATUS status = NT_STATUS_NO_MEMORY;
4596
4597         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4598                 return cli_smb2_get_ea_list_path(cli,
4599                                         path,
4600                                         ctx,
4601                                         pnum_eas,
4602                                         pea_list);
4603         }
4604
4605         frame = talloc_stackframe();
4606
4607         if (smbXcli_conn_has_async_calls(cli->conn)) {
4608                 /*
4609                  * Can't use sync call while an async call is in flight
4610                  */
4611                 status = NT_STATUS_INVALID_PARAMETER;
4612                 goto fail;
4613         }
4614         ev = samba_tevent_context_init(frame);
4615         if (ev == NULL) {
4616                 goto fail;
4617         }
4618         req = cli_get_ea_list_path_send(frame, ev, cli, path);
4619         if (req == NULL) {
4620                 goto fail;
4621         }
4622         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4623                 goto fail;
4624         }
4625         status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4626  fail:
4627         TALLOC_FREE(frame);
4628         return status;
4629 }
4630
4631 /****************************************************************************
4632  Convert open "flags" arg to uint32_t on wire.
4633 ****************************************************************************/
4634
4635 static uint32_t open_flags_to_wire(int flags)
4636 {
4637         int open_mode = flags & O_ACCMODE;
4638         uint32_t ret = 0;
4639
4640         switch (open_mode) {
4641                 case O_WRONLY:
4642                         ret |= SMB_O_WRONLY;
4643                         break;
4644                 case O_RDWR:
4645                         ret |= SMB_O_RDWR;
4646                         break;
4647                 default:
4648                 case O_RDONLY:
4649                         ret |= SMB_O_RDONLY;
4650                         break;
4651         }
4652
4653         if (flags & O_CREAT) {
4654                 ret |= SMB_O_CREAT;
4655         }
4656         if (flags & O_EXCL) {
4657                 ret |= SMB_O_EXCL;
4658         }
4659         if (flags & O_TRUNC) {
4660                 ret |= SMB_O_TRUNC;
4661         }
4662 #if defined(O_SYNC)
4663         if (flags & O_SYNC) {
4664                 ret |= SMB_O_SYNC;
4665         }
4666 #endif /* O_SYNC */
4667         if (flags & O_APPEND) {
4668                 ret |= SMB_O_APPEND;
4669         }
4670 #if defined(O_DIRECT)
4671         if (flags & O_DIRECT) {
4672                 ret |= SMB_O_DIRECT;
4673         }
4674 #endif
4675 #if defined(O_DIRECTORY)
4676         if (flags & O_DIRECTORY) {
4677                 ret |= SMB_O_DIRECTORY;
4678         }
4679 #endif
4680         return ret;
4681 }
4682
4683 /****************************************************************************
4684  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4685 ****************************************************************************/
4686
4687 struct posix_open_state {
4688         uint16_t setup;
4689         uint8_t *param;
4690         uint8_t data[18];
4691         uint16_t fnum; /* Out */
4692 };
4693
4694 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4695 {
4696         struct tevent_req *req = tevent_req_callback_data(
4697                                 subreq, struct tevent_req);
4698         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4699         NTSTATUS status;
4700         uint8_t *data;
4701         uint32_t num_data;
4702
4703         status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4704                                 NULL, 0, NULL, &data, 12, &num_data);
4705         TALLOC_FREE(subreq);
4706         if (tevent_req_nterror(req, status)) {
4707                 return;
4708         }
4709         state->fnum = SVAL(data,2);
4710         tevent_req_done(req);
4711 }
4712
4713 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4714                                         struct tevent_context *ev,
4715                                         struct cli_state *cli,
4716                                         const char *fname,
4717                                         int flags,
4718                                         mode_t mode,
4719                                         bool is_dir)
4720 {
4721         struct tevent_req *req = NULL, *subreq = NULL;
4722         struct posix_open_state *state = NULL;
4723         uint32_t wire_flags = open_flags_to_wire(flags);
4724
4725         req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4726         if (req == NULL) {
4727                 return NULL;
4728         }
4729
4730         /* Setup setup word. */
4731         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4732
4733         /* Setup param array. */
4734         state->param = talloc_array(state, uint8_t, 6);
4735         if (tevent_req_nomem(state->param, req)) {
4736                 return tevent_req_post(req, ev);
4737         }
4738         memset(state->param, '\0', 6);
4739         SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4740
4741         state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4742                                    strlen(fname)+1, NULL);
4743
4744         if (tevent_req_nomem(state->param, req)) {
4745                 return tevent_req_post(req, ev);
4746         }
4747
4748         /* Setup data words. */
4749         if (is_dir) {
4750                 wire_flags |= SMB_O_DIRECTORY;
4751         }
4752
4753         SIVAL(state->data,0,0); /* No oplock. */
4754         SIVAL(state->data,4,wire_flags);
4755         SIVAL(state->data,8,unix_perms_to_wire(mode));
4756         SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4757         SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4758
4759         subreq = cli_trans_send(state,                  /* mem ctx. */
4760                                 ev,                     /* event ctx. */
4761                                 cli,                    /* cli_state. */
4762                                 SMBtrans2,              /* cmd. */
4763                                 NULL,                   /* pipe name. */
4764                                 -1,                     /* fid. */
4765                                 0,                      /* function. */
4766                                 0,                      /* flags. */
4767                                 &state->setup,          /* setup. */
4768                                 1,                      /* num setup uint16_t words. */
4769                                 0,                      /* max returned setup. */
4770                                 state->param,           /* param. */
4771                                 talloc_get_size(state->param),/* num param. */
4772                                 2,                      /* max returned param. */
4773                                 state->data,            /* data. */
4774                                 18,                     /* num data. */
4775                                 12);                    /* max returned data. */
4776
4777         if (tevent_req_nomem(subreq, req)) {
4778                 return tevent_req_post(req, ev);
4779         }
4780         tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4781         return req;
4782 }
4783
4784 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4785                                         struct tevent_context *ev,
4786                                         struct cli_state *cli,
4787                                         const char *fname,
4788                                         int flags,
4789                                         mode_t mode)
4790 {
4791         return cli_posix_open_internal_send(mem_ctx, ev,
4792                                 cli, fname, flags, mode, false);
4793 }
4794
4795 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4796 {
4797         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4798         NTSTATUS status;
4799
4800         if (tevent_req_is_nterror(req, &status)) {
4801                 return status;
4802         }
4803         *pfnum = state->fnum;
4804         return NT_STATUS_OK;
4805 }
4806
4807 /****************************************************************************
4808  Open - POSIX semantics. Doesn't request oplock.
4809 ****************************************************************************/
4810
4811 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4812                         int flags, mode_t mode, uint16_t *pfnum)
4813 {
4814
4815         TALLOC_CTX *frame = talloc_stackframe();
4816         struct tevent_context *ev = NULL;
4817         struct tevent_req *req = NULL;
4818         NTSTATUS status = NT_STATUS_OK;
4819
4820         if (smbXcli_conn_has_async_calls(cli->conn)) {
4821                 /*
4822                  * Can't use sync call while an async call is in flight
4823                  */
4824                 status = NT_STATUS_INVALID_PARAMETER;
4825                 goto fail;
4826         }
4827
4828         ev = samba_tevent_context_init(frame);
4829         if (ev == NULL) {
4830                 status = NT_STATUS_NO_MEMORY;
4831                 goto fail;
4832         }
4833
4834         req = cli_posix_open_send(frame,
4835                                 ev,
4836                                 cli,
4837                                 fname,
4838                                 flags,
4839                                 mode);
4840         if (req == NULL) {
4841                 status = NT_STATUS_NO_MEMORY;
4842                 goto fail;
4843         }
4844
4845         if (!tevent_req_poll(req, ev)) {
4846                 status = map_nt_error_from_unix(errno);
4847                 goto fail;
4848         }
4849
4850         status = cli_posix_open_recv(req, pfnum);
4851
4852  fail:
4853         TALLOC_FREE(frame);
4854         return status;
4855 }
4856
4857 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4858                                         struct tevent_context *ev,
4859                                         struct cli_state *cli,
4860                                         const char *fname,
4861                                         mode_t mode)
4862 {
4863         return cli_posix_open_internal_send(mem_ctx, ev,
4864                                 cli, fname, O_CREAT, mode, true);
4865 }
4866
4867 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4868 {
4869         return tevent_req_simple_recv_ntstatus(req);
4870 }
4871
4872 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4873 {
4874         TALLOC_CTX *frame = talloc_stackframe();
4875         struct tevent_context *ev = NULL;
4876         struct tevent_req *req = NULL;
4877         NTSTATUS status = NT_STATUS_OK;
4878
4879         if (smbXcli_conn_has_async_calls(cli->conn)) {
4880                 /*
4881                  * Can't use sync call while an async call is in flight
4882                  */
4883                 status = NT_STATUS_INVALID_PARAMETER;
4884                 goto fail;
4885         }
4886
4887         ev = samba_tevent_context_init(frame);
4888         if (ev == NULL) {
4889                 status = NT_STATUS_NO_MEMORY;
4890                 goto fail;
4891         }
4892
4893         req = cli_posix_mkdir_send(frame,
4894                                 ev,
4895                                 cli,
4896                                 fname,
4897                                 mode);
4898         if (req == NULL) {
4899                 status = NT_STATUS_NO_MEMORY;
4900                 goto fail;
4901         }
4902
4903         if (!tevent_req_poll(req, ev)) {
4904                 status = map_nt_error_from_unix(errno);
4905                 goto fail;
4906         }
4907
4908         status = cli_posix_mkdir_recv(req);
4909
4910  fail:
4911         TALLOC_FREE(frame);
4912         return status;
4913 }
4914
4915 /****************************************************************************
4916  unlink or rmdir - POSIX semantics.
4917 ****************************************************************************/
4918
4919 struct cli_posix_unlink_internal_state {
4920         uint8_t data[2];
4921 };
4922
4923 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4924
4925 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4926                                         struct tevent_context *ev,
4927                                         struct cli_state *cli,
4928                                         const char *fname,
4929                                         uint16_t level)
4930 {
4931         struct tevent_req *req = NULL, *subreq = NULL;
4932         struct cli_posix_unlink_internal_state *state = NULL;
4933
4934         req = tevent_req_create(mem_ctx, &state,
4935                                 struct cli_posix_unlink_internal_state);
4936         if (req == NULL) {
4937                 return NULL;
4938         }
4939
4940         /* Setup data word. */
4941         SSVAL(state->data, 0, level);
4942
4943         subreq = cli_setpathinfo_send(state, ev, cli,
4944                                       SMB_POSIX_PATH_UNLINK,
4945                                       fname,
4946                                       state->data, sizeof(state->data));
4947         if (tevent_req_nomem(subreq, req)) {
4948                 return tevent_req_post(req, ev);
4949         }
4950         tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4951         return req;
4952 }
4953
4954 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4955 {
4956         NTSTATUS status = cli_setpathinfo_recv(subreq);
4957         tevent_req_simple_finish_ntstatus(subreq, status);
4958 }
4959
4960 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4961                                         struct tevent_context *ev,
4962                                         struct cli_state *cli,
4963                                         const char *fname)
4964 {
4965         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4966                                               SMB_POSIX_UNLINK_FILE_TARGET);
4967 }
4968
4969 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4970 {
4971         return tevent_req_simple_recv_ntstatus(req);
4972 }
4973
4974 /****************************************************************************
4975  unlink - POSIX semantics.
4976 ****************************************************************************/
4977
4978 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4979 {
4980         TALLOC_CTX *frame = talloc_stackframe();
4981         struct tevent_context *ev = NULL;
4982         struct tevent_req *req = NULL;
4983         NTSTATUS status = NT_STATUS_OK;
4984
4985         if (smbXcli_conn_has_async_calls(cli->conn)) {
4986                 /*
4987                  * Can't use sync call while an async call is in flight
4988                  */
4989                 status = NT_STATUS_INVALID_PARAMETER;
4990                 goto fail;
4991         }
4992
4993         ev = samba_tevent_context_init(frame);
4994         if (ev == NULL) {
4995                 status = NT_STATUS_NO_MEMORY;
4996                 goto fail;
4997         }
4998
4999         req = cli_posix_unlink_send(frame,
5000                                 ev,
5001                                 cli,
5002                                 fname);
5003         if (req == NULL) {
5004                 status = NT_STATUS_NO_MEMORY;
5005                 goto fail;
5006         }
5007
5008         if (!tevent_req_poll(req, ev)) {
5009                 status = map_nt_error_from_unix(errno);
5010                 goto fail;
5011         }
5012
5013         status = cli_posix_unlink_recv(req);
5014
5015  fail:
5016         TALLOC_FREE(frame);
5017         return status;
5018 }
5019
5020 /****************************************************************************
5021  rmdir - POSIX semantics.
5022 ****************************************************************************/
5023
5024 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5025                                         struct tevent_context *ev,
5026                                         struct cli_state *cli,
5027                                         const char *fname)
5028 {
5029         return cli_posix_unlink_internal_send(
5030                 mem_ctx, ev, cli, fname,
5031                 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5032 }
5033
5034 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5035 {
5036         return tevent_req_simple_recv_ntstatus(req);
5037 }
5038
5039 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5040 {
5041         TALLOC_CTX *frame = talloc_stackframe();
5042         struct tevent_context *ev = NULL;
5043         struct tevent_req *req = NULL;
5044         NTSTATUS status = NT_STATUS_OK;
5045
5046         if (smbXcli_conn_has_async_calls(cli->conn)) {
5047                 /*
5048                  * Can't use sync call while an async call is in flight
5049                  */
5050                 status = NT_STATUS_INVALID_PARAMETER;
5051                 goto fail;
5052         }
5053
5054         ev = samba_tevent_context_init(frame);
5055         if (ev == NULL) {
5056                 status = NT_STATUS_NO_MEMORY;
5057                 goto fail;
5058         }
5059
5060         req = cli_posix_rmdir_send(frame,
5061                                 ev,
5062                                 cli,
5063                                 fname);
5064         if (req == NULL) {
5065                 status = NT_STATUS_NO_MEMORY;
5066                 goto fail;
5067         }
5068
5069         if (!tevent_req_poll(req, ev)) {
5070                 status = map_nt_error_from_unix(errno);
5071                 goto fail;
5072         }
5073
5074         status = cli_posix_rmdir_recv(req, frame);
5075
5076  fail:
5077         TALLOC_FREE(frame);
5078         return status;
5079 }
5080
5081 /****************************************************************************
5082  filechangenotify
5083 ****************************************************************************/
5084
5085 struct cli_notify_state {
5086         uint8_t setup[8];
5087         uint32_t num_changes;
5088         struct notify_change *changes;
5089 };
5090
5091 static void cli_notify_done(struct tevent_req *subreq);
5092
5093 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5094                                    struct tevent_context *ev,
5095                                    struct cli_state *cli, uint16_t fnum,
5096                                    uint32_t buffer_size,
5097                                    uint32_t completion_filter, bool recursive)
5098 {
5099         struct tevent_req *req, *subreq;
5100         struct cli_notify_state *state;
5101         unsigned old_timeout;
5102
5103         req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5104         if (req == NULL) {
5105                 return NULL;
5106         }
5107
5108         SIVAL(state->setup, 0, completion_filter);
5109         SSVAL(state->setup, 4, fnum);
5110         SSVAL(state->setup, 6, recursive);
5111
5112         /*
5113          * Notifies should not time out
5114          */
5115         old_timeout = cli_set_timeout(cli, 0);
5116
5117         subreq = cli_trans_send(
5118                 state,                  /* mem ctx. */
5119                 ev,                     /* event ctx. */
5120                 cli,                    /* cli_state. */
5121                 SMBnttrans,             /* cmd. */
5122                 NULL,                   /* pipe name. */
5123                 -1,                     /* fid. */
5124                 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5125                 0,                      /* flags. */
5126                 (uint16_t *)state->setup, /* setup. */
5127                 4,                      /* num setup uint16_t words. */
5128                 0,                      /* max returned setup. */
5129                 NULL,                   /* param. */
5130                 0,                      /* num param. */
5131                 buffer_size,            /* max returned param. */
5132                 NULL,                   /* data. */
5133                 0,                      /* num data. */
5134                 0);                     /* max returned data. */
5135
5136         cli_set_timeout(cli, old_timeout);
5137
5138         if (tevent_req_nomem(subreq, req)) {
5139                 return tevent_req_post(req, ev);
5140         }
5141         tevent_req_set_callback(subreq, cli_notify_done, req);
5142         return req;
5143 }
5144
5145 static void cli_notify_done(struct tevent_req *subreq)
5146 {
5147         struct tevent_req *req = tevent_req_callback_data(
5148                 subreq, struct tevent_req);
5149         struct cli_notify_state *state = tevent_req_data(
5150                 req, struct cli_notify_state);
5151         NTSTATUS status;
5152         uint8_t *params;
5153         uint32_t i, ofs, num_params;
5154         uint16_t flags2;
5155
5156         status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5157                                 &params, 0, &num_params, NULL, 0, NULL);
5158         TALLOC_FREE(subreq);
5159         if (tevent_req_nterror(req, status)) {
5160                 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5161                 return;
5162         }
5163
5164         state->num_changes = 0;
5165         ofs = 0;
5166
5167         while (num_params - ofs > 12) {
5168                 uint32_t next = IVAL(params, ofs);
5169                 state->num_changes += 1;
5170
5171                 if ((next == 0) || (ofs+next >= num_params)) {
5172                         break;
5173                 }
5174                 ofs += next;
5175         }
5176
5177         state->changes = talloc_array(state, struct notify_change,
5178                                       state->num_changes);
5179         if (tevent_req_nomem(state->changes, req)) {
5180                 TALLOC_FREE(params);
5181                 return;
5182         }
5183
5184         ofs = 0;
5185
5186         for (i=0; i<state->num_changes; i++) {
5187                 uint32_t next = IVAL(params, ofs);
5188                 uint32_t len = IVAL(params, ofs+8);
5189                 ssize_t ret;
5190                 char *name;
5191
5192                 if (trans_oob(num_params, ofs + 12, len)) {
5193                         TALLOC_FREE(params);
5194                         tevent_req_nterror(
5195                                 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5196                         return;
5197                 }
5198
5199                 state->changes[i].action = IVAL(params, ofs+4);
5200                 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5201                                          &name, params+ofs+12, len,
5202                                          STR_TERMINATE|STR_UNICODE);
5203                 if (ret == -1) {
5204                         TALLOC_FREE(params);
5205                         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5206                         return;
5207                 }
5208                 state->changes[i].name = name;
5209                 ofs += next;
5210         }
5211
5212         TALLOC_FREE(params);
5213         tevent_req_done(req);
5214 }
5215
5216 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5217                          uint32_t *pnum_changes,
5218                          struct notify_change **pchanges)
5219 {
5220         struct cli_notify_state *state = tevent_req_data(
5221                 req, struct cli_notify_state);
5222         NTSTATUS status;
5223
5224         if (tevent_req_is_nterror(req, &status)) {
5225                 return status;
5226         }
5227
5228         *pnum_changes = state->num_changes;
5229         *pchanges = talloc_move(mem_ctx, &state->changes);
5230         return NT_STATUS_OK;
5231 }
5232
5233 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5234                     uint32_t completion_filter, bool recursive,
5235                     TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5236                     struct notify_change **pchanges)
5237 {
5238         TALLOC_CTX *frame = talloc_stackframe();
5239         struct tevent_context *ev;
5240         struct tevent_req *req;
5241         NTSTATUS status = NT_STATUS_NO_MEMORY;
5242
5243         if (smbXcli_conn_has_async_calls(cli->conn)) {
5244                 /*
5245                  * Can't use sync call while an async call is in flight
5246                  */
5247                 status = NT_STATUS_INVALID_PARAMETER;
5248                 goto fail;
5249         }
5250         ev = samba_tevent_context_init(frame);
5251         if (ev == NULL) {
5252                 goto fail;
5253         }
5254         req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5255                               completion_filter, recursive);
5256         if (req == NULL) {
5257                 goto fail;
5258         }
5259         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5260                 goto fail;
5261         }
5262         status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5263  fail:
5264         TALLOC_FREE(frame);
5265         return status;
5266 }
5267
5268 struct cli_qpathinfo_state {
5269         uint8_t *param;
5270         uint8_t *data;
5271         uint16_t setup[1];
5272         uint32_t min_rdata;
5273         uint8_t *rdata;
5274         uint32_t num_rdata;
5275 };
5276
5277 static void cli_qpathinfo_done(struct tevent_req *subreq);
5278
5279 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5280                                       struct tevent_context *ev,
5281                                       struct cli_state *cli, const char *fname,
5282                                       uint16_t level, uint32_t min_rdata,
5283                                       uint32_t max_rdata)
5284 {
5285         struct tevent_req *req, *subreq;
5286         struct cli_qpathinfo_state *state;
5287
5288         req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5289         if (req == NULL) {
5290                 return NULL;
5291         }
5292         state->min_rdata = min_rdata;
5293         SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5294
5295         state->param = talloc_zero_array(state, uint8_t, 6);
5296         if (tevent_req_nomem(state->param, req)) {
5297                 return tevent_req_post(req, ev);
5298         }
5299         SSVAL(state->param, 0, level);
5300         state->param = trans2_bytes_push_str(
5301                 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5302         if (tevent_req_nomem(state->param, req)) {
5303                 return tevent_req_post(req, ev);
5304         }
5305
5306         subreq = cli_trans_send(
5307                 state,                  /* mem ctx. */
5308                 ev,                     /* event ctx. */
5309                 cli,                    /* cli_state. */
5310                 SMBtrans2,              /* cmd. */
5311                 NULL,                   /* pipe name. */
5312                 -1,                     /* fid. */
5313                 0,                      /* function. */
5314                 0,                      /* flags. */
5315                 state->setup,           /* setup. */
5316                 1,                      /* num setup uint16_t words. */
5317                 0,                      /* max returned setup. */
5318                 state->param,           /* param. */
5319                 talloc_get_size(state->param),  /* num param. */
5320                 2,                      /* max returned param. */
5321                 NULL,                   /* data. */
5322                 0,                      /* num data. */
5323                 max_rdata);             /* max returned data. */
5324
5325         if (tevent_req_nomem(subreq, req)) {
5326                 return tevent_req_post(req, ev);
5327         }
5328         tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5329         return req;
5330 }
5331
5332 static void cli_qpathinfo_done(struct tevent_req *subreq)
5333 {
5334         struct tevent_req *req = tevent_req_callback_data(
5335                 subreq, struct tevent_req);
5336         struct cli_qpathinfo_state *state = tevent_req_data(
5337                 req, struct cli_qpathinfo_state);
5338         NTSTATUS status;
5339
5340         status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5341                                 NULL, 0, NULL,
5342                                 &state->rdata, state->min_rdata,
5343                                 &state->num_rdata);
5344         if (tevent_req_nterror(req, status)) {
5345                 return;
5346         }
5347         tevent_req_done(req);
5348 }
5349
5350 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5351                             uint8_t **rdata, uint32_t *num_rdata)
5352 {
5353         struct cli_qpathinfo_state *state = tevent_req_data(
5354                 req, struct cli_qpathinfo_state);
5355         NTSTATUS status;
5356
5357         if (tevent_req_is_nterror(req, &status)) {
5358                 return status;
5359         }
5360         if (rdata != NULL) {
5361                 *rdata = talloc_move(mem_ctx, &state->rdata);
5362         } else {
5363                 TALLOC_FREE(state->rdata);
5364         }
5365         if (num_rdata != NULL) {
5366                 *num_rdata = state->num_rdata;
5367         }
5368         return NT_STATUS_OK;
5369 }
5370
5371 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5372                        const char *fname, uint16_t level, uint32_t min_rdata,
5373                        uint32_t max_rdata,
5374                        uint8_t **rdata, uint32_t *num_rdata)
5375 {
5376         TALLOC_CTX *frame = talloc_stackframe();
5377         struct tevent_context *ev;
5378         struct tevent_req *req;
5379         NTSTATUS status = NT_STATUS_NO_MEMORY;
5380
5381         if (smbXcli_conn_has_async_calls(cli->conn)) {
5382                 /*
5383                  * Can't use sync call while an async call is in flight
5384                  */
5385                 status = NT_STATUS_INVALID_PARAMETER;
5386                 goto fail;
5387         }
5388         ev = samba_tevent_context_init(frame);
5389         if (ev == NULL) {
5390                 goto fail;
5391         }
5392         req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5393                                  max_rdata);
5394         if (req == NULL) {
5395                 goto fail;
5396         }
5397         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5398                 goto fail;
5399         }
5400         status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5401  fail:
5402         TALLOC_FREE(frame);
5403         return status;
5404 }
5405
5406 struct cli_qfileinfo_state {
5407         uint16_t setup[1];
5408         uint8_t param[4];
5409         uint8_t *data;
5410         uint16_t recv_flags2;
5411         uint32_t min_rdata;
5412         uint8_t *rdata;
5413         uint32_t num_rdata;
5414 };
5415
5416 static void cli_qfileinfo_done(struct tevent_req *subreq);
5417
5418 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5419                                       struct tevent_context *ev,
5420                                       struct cli_state *cli, uint16_t fnum,
5421                                       uint16_t level, uint32_t min_rdata,
5422                                       uint32_t max_rdata)
5423 {
5424         struct tevent_req *req, *subreq;
5425         struct cli_qfileinfo_state *state;
5426
5427         req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5428         if (req == NULL) {
5429                 return NULL;
5430         }
5431         state->min_rdata = min_rdata;
5432         SSVAL(state->param, 0, fnum);
5433         SSVAL(state->param, 2, level);
5434         SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5435
5436         subreq = cli_trans_send(
5437                 state,                  /* mem ctx. */
5438                 ev,                     /* event ctx. */
5439                 cli,                    /* cli_state. */
5440                 SMBtrans2,              /* cmd. */
5441                 NULL,                   /* pipe name. */
5442                 -1,                     /* fid. */
5443                 0,                      /* function. */
5444                 0,                      /* flags. */
5445                 state->setup,           /* setup. */
5446                 1,                      /* num setup uint16_t words. */
5447                 0,                      /* max returned setup. */
5448                 state->param,           /* param. */
5449                 sizeof(state->param),   /* num param. */
5450                 2,                      /* max returned param. */
5451                 NULL,                   /* data. */
5452                 0,                      /* num data. */
5453                 max_rdata);             /* max returned data. */
5454
5455         if (tevent_req_nomem(subreq, req)) {
5456                 return tevent_req_post(req, ev);
5457         }
5458         tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5459         return req;
5460 }
5461
5462 static void cli_qfileinfo_done(struct tevent_req *subreq)
5463 {
5464         struct tevent_req *req = tevent_req_callback_data(
5465                 subreq, struct tevent_req);
5466         struct cli_qfileinfo_state *state = tevent_req_data(
5467                 req, struct cli_qfileinfo_state);
5468         NTSTATUS status;
5469
5470         status = cli_trans_recv(subreq, state,
5471                                 &state->recv_flags2,
5472                                 NULL, 0, NULL,
5473                                 NULL, 0, NULL,
5474                                 &state->rdata, state->min_rdata,
5475                                 &state->num_rdata);
5476         if (tevent_req_nterror(req, status)) {
5477                 return;
5478         }
5479         tevent_req_done(req);
5480 }
5481
5482 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5483                             uint16_t *recv_flags2,
5484                             uint8_t **rdata, uint32_t *num_rdata)
5485 {
5486         struct cli_qfileinfo_state *state = tevent_req_data(
5487                 req, struct cli_qfileinfo_state);
5488         NTSTATUS status;
5489
5490         if (tevent_req_is_nterror(req, &status)) {
5491                 return status;
5492         }
5493
5494         if (recv_flags2 != NULL) {
5495                 *recv_flags2 = state->recv_flags2;
5496         }
5497         if (rdata != NULL) {
5498                 *rdata = talloc_move(mem_ctx, &state->rdata);
5499         } else {
5500                 TALLOC_FREE(state->rdata);
5501         }
5502         if (num_rdata != NULL) {
5503                 *num_rdata = state->num_rdata;
5504         }
5505         return NT_STATUS_OK;
5506 }
5507
5508 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5509                        uint16_t fnum, uint16_t level, uint32_t min_rdata,
5510                        uint32_t max_rdata, uint16_t *recv_flags2,
5511                        uint8_t **rdata, uint32_t *num_rdata)
5512 {
5513         TALLOC_CTX *frame = talloc_stackframe();
5514         struct tevent_context *ev;
5515         struct tevent_req *req;
5516         NTSTATUS status = NT_STATUS_NO_MEMORY;
5517
5518         if (smbXcli_conn_has_async_calls(cli->conn)) {
5519                 /*
5520                  * Can't use sync call while an async call is in flight
5521                  */
5522                 status = NT_STATUS_INVALID_PARAMETER;
5523                 goto fail;
5524         }
5525         ev = samba_tevent_context_init(frame);
5526         if (ev == NULL) {
5527                 goto fail;
5528         }
5529         req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5530                                  max_rdata);
5531         if (req == NULL) {
5532                 goto fail;
5533         }
5534         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5535                 goto fail;
5536         }
5537         status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5538  fail:
5539         TALLOC_FREE(frame);
5540         return status;
5541 }
5542
5543 struct cli_flush_state {
5544         uint16_t vwv[1];
5545 };
5546
5547 static void cli_flush_done(struct tevent_req *subreq);
5548
5549 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5550                                   struct tevent_context *ev,
5551                                   struct cli_state *cli,
5552                                   uint16_t fnum)
5553 {
5554         struct tevent_req *req, *subreq;
5555         struct cli_flush_state *state;
5556
5557         req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5558         if (req == NULL) {
5559                 return NULL;
5560         }
5561         SSVAL(state->vwv + 0, 0, fnum);
5562
5563         subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5564                               0, NULL);
5565         if (tevent_req_nomem(subreq, req)) {
5566                 return tevent_req_post(req, ev);
5567         }
5568         tevent_req_set_callback(subreq, cli_flush_done, req);
5569         return req;
5570 }
5571
5572 static void cli_flush_done(struct tevent_req *subreq)
5573 {
5574         struct tevent_req *req = tevent_req_callback_data(
5575                 subreq, struct tevent_req);
5576         NTSTATUS status;
5577
5578         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5579         TALLOC_FREE(subreq);
5580         if (tevent_req_nterror(req, status)) {
5581                 return;
5582         }
5583         tevent_req_done(req);
5584 }
5585
5586 NTSTATUS cli_flush_recv(struct tevent_req *req)
5587 {
5588         return tevent_req_simple_recv_ntstatus(req);
5589 }
5590
5591 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5592 {
5593         TALLOC_CTX *frame = talloc_stackframe();
5594         struct tevent_context *ev;
5595         struct tevent_req *req;
5596         NTSTATUS status = NT_STATUS_NO_MEMORY;
5597
5598         if (smbXcli_conn_has_async_calls(cli->conn)) {
5599                 /*
5600                  * Can't use sync call while an async call is in flight
5601                  */
5602                 status = NT_STATUS_INVALID_PARAMETER;
5603                 goto fail;
5604         }
5605         ev = samba_tevent_context_init(frame);
5606         if (ev == NULL) {
5607                 goto fail;
5608         }
5609         req = cli_flush_send(frame, ev, cli, fnum);
5610         if (req == NULL) {
5611                 goto fail;
5612         }
5613         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5614                 goto fail;
5615         }
5616         status = cli_flush_recv(req);
5617  fail:
5618         TALLOC_FREE(frame);
5619         return status;
5620 }
5621
5622 struct cli_shadow_copy_data_state {
5623         uint16_t setup[4];
5624         uint8_t *data;
5625         uint32_t num_data;
5626         bool get_names;
5627 };
5628
5629 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5630
5631 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5632                                              struct tevent_context *ev,
5633                                              struct cli_state *cli,
5634                                              uint16_t fnum,
5635                                              bool get_names)
5636 {
5637         struct tevent_req *req, *subreq;
5638         struct cli_shadow_copy_data_state *state;
5639         uint32_t ret_size;
5640
5641         req = tevent_req_create(mem_ctx, &state,
5642                                 struct cli_shadow_copy_data_state);
5643         if (req == NULL) {
5644                 return NULL;
5645         }
5646         state->get_names = get_names;
5647         ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5648
5649         SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5650         SSVAL(state->setup + 2, 0, fnum);
5651         SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5652         SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5653
5654         subreq = cli_trans_send(
5655                 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5656                 state->setup, ARRAY_SIZE(state->setup), 0,
5657                 NULL, 0, 0,
5658                 NULL, 0, ret_size);
5659         if (tevent_req_nomem(subreq, req)) {
5660                 return tevent_req_post(req, ev);
5661         }
5662         tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5663         return req;
5664 }
5665
5666 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5667 {
5668         struct tevent_req *req = tevent_req_callback_data(
5669                 subreq, struct tevent_req);
5670         struct cli_shadow_copy_data_state *state = tevent_req_data(
5671                 req, struct cli_shadow_copy_data_state);
5672         NTSTATUS status;
5673
5674         status = cli_trans_recv(subreq, state, NULL,
5675                                 NULL, 0, NULL, /* setup */
5676                                 NULL, 0, NULL, /* param */
5677                                 &state->data, 12, &state->num_data);
5678         TALLOC_FREE(subreq);
5679         if (tevent_req_nterror(req, status)) {
5680                 return;
5681         }
5682         tevent_req_done(req);
5683 }
5684
5685 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5686                                    char ***pnames, int *pnum_names)
5687 {
5688         struct cli_shadow_copy_data_state *state = tevent_req_data(
5689                 req, struct cli_shadow_copy_data_state);
5690         char **names;
5691         int i, num_names;
5692         uint32_t dlength;
5693         NTSTATUS status;
5694
5695         if (tevent_req_is_nterror(req, &status)) {
5696                 return status;
5697         }
5698         num_names = IVAL(state->data, 4);
5699         dlength = IVAL(state->data, 8);
5700
5701         if (!state->get_names) {
5702                 *pnum_names = num_names;
5703                 return NT_STATUS_OK;
5704         }
5705
5706         if (dlength+12 > state->num_data) {
5707                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5708         }
5709         names = talloc_array(mem_ctx, char *, num_names);
5710         if (names == NULL) {
5711                 return NT_STATUS_NO_MEMORY;
5712         }
5713
5714         for (i=0; i<num_names; i++) {
5715                 bool ret;
5716                 uint8_t *src;
5717                 size_t converted_size;
5718
5719                 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5720                 ret = convert_string_talloc(
5721                         names, CH_UTF16LE, CH_UNIX,
5722                         src, 2 * sizeof(SHADOW_COPY_LABEL),
5723                         &names[i], &converted_size);
5724                 if (!ret) {
5725                         TALLOC_FREE(names);
5726                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
5727                 }
5728         }
5729         *pnum_names = num_names;
5730         *pnames = names;
5731         return NT_STATUS_OK;
5732 }
5733
5734 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5735                               uint16_t fnum, bool get_names,
5736                               char ***pnames, int *pnum_names)
5737 {
5738         TALLOC_CTX *frame = talloc_stackframe();
5739         struct tevent_context *ev;
5740         struct tevent_req *req;
5741         NTSTATUS status = NT_STATUS_NO_MEMORY;
5742
5743         if (smbXcli_conn_has_async_calls(cli->conn)) {
5744                 /*
5745                  * Can't use sync call while an async call is in flight
5746                  */
5747                 status = NT_STATUS_INVALID_PARAMETER;
5748                 goto fail;
5749         }
5750         ev = samba_tevent_context_init(frame);
5751         if (ev == NULL) {
5752                 goto fail;
5753         }
5754         req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5755         if (req == NULL) {
5756                 goto fail;
5757         }
5758         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5759                 goto fail;
5760         }
5761         status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5762  fail:
5763         TALLOC_FREE(frame);
5764         return status;
5765 }