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