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