smbclient: query disk usage relative to current directory
[samba.git] / source3 / libsmb / clifile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jeremy Allison 2001-2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
31
32 /***********************************************************
33  Common function for pushing stings, used by smb_bytes_push_str()
34  and trans_bytes_push_str(). Only difference is the align_odd
35  parameter setting.
36 ***********************************************************/
37
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39                                 const char *str, size_t str_len,
40                                 bool align_odd,
41                                 size_t *pconverted_size)
42 {
43         size_t buflen;
44         char *converted;
45         size_t converted_size;
46
47         if (buf == NULL) {
48                 return NULL;
49         }
50
51         buflen = talloc_get_size(buf);
52
53         if (ucs2 &&
54             ((align_odd && (buflen % 2 == 0)) ||
55              (!align_odd && (buflen % 2 == 1)))) {
56                 /*
57                  * We're pushing into an SMB buffer, align odd
58                  */
59                 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60                 if (buf == NULL) {
61                         return NULL;
62                 }
63                 buf[buflen] = '\0';
64                 buflen += 1;
65         }
66
67         if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68                                    ucs2 ? CH_UTF16LE : CH_DOS,
69                                    str, str_len, &converted,
70                                    &converted_size)) {
71                 return NULL;
72         }
73
74         buf = talloc_realloc(NULL, buf, uint8_t,
75                                    buflen + converted_size);
76         if (buf == NULL) {
77                 TALLOC_FREE(converted);
78                 return NULL;
79         }
80
81         memcpy(buf + buflen, converted, converted_size);
82
83         TALLOC_FREE(converted);
84
85         if (pconverted_size) {
86                 *pconverted_size = converted_size;
87         }
88
89         return buf;
90 }
91
92 /***********************************************************
93  Push a string into an SMB buffer, with odd byte alignment
94  if it's a UCS2 string.
95 ***********************************************************/
96
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98                             const char *str, size_t str_len,
99                             size_t *pconverted_size)
100 {
101         return internal_bytes_push_str(buf, ucs2, str, str_len,
102                         true, pconverted_size);
103 }
104
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106                               const uint8_t *bytes, size_t num_bytes)
107 {
108         size_t buflen;
109
110         if (buf == NULL) {
111                 return NULL;
112         }
113         buflen = talloc_get_size(buf);
114
115         buf = talloc_realloc(NULL, buf, uint8_t,
116                                    buflen + 1 + num_bytes);
117         if (buf == NULL) {
118                 return NULL;
119         }
120         buf[buflen] = prefix;
121         memcpy(&buf[buflen+1], bytes, num_bytes);
122         return buf;
123 }
124
125 /***********************************************************
126  Same as smb_bytes_push_str(), but without the odd byte
127  align for ucs2 (we're pushing into a param or data block).
128  static for now, although this will probably change when
129  other modules use async trans calls.
130 ***********************************************************/
131
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133                                const char *str, size_t str_len,
134                                size_t *pconverted_size)
135 {
136         return internal_bytes_push_str(buf, ucs2, str, str_len,
137                         false, pconverted_size);
138 }
139
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141                                  const uint8_t *bytes, size_t num_bytes)
142 {
143         size_t buflen;
144
145         if (buf == NULL) {
146                 return NULL;
147         }
148         buflen = talloc_get_size(buf);
149
150         buf = talloc_realloc(NULL, buf, uint8_t,
151                              buflen + num_bytes);
152         if (buf == NULL) {
153                 return NULL;
154         }
155         memcpy(&buf[buflen], bytes, num_bytes);
156         return buf;
157 }
158
159 struct cli_setpathinfo_state {
160         uint16_t setup;
161         uint8_t *param;
162 };
163
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
165
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167                                         struct tevent_context *ev,
168                                         struct cli_state *cli,
169                                         uint16_t level,
170                                         const char *path,
171                                         uint8_t *data,
172                                         size_t data_len)
173 {
174         struct tevent_req *req, *subreq;
175         struct cli_setpathinfo_state *state;
176
177         req = tevent_req_create(mem_ctx, &state,
178                                 struct cli_setpathinfo_state);
179         if (req == NULL) {
180                 return NULL;
181         }
182
183         /* Setup setup word. */
184         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
185
186         /* Setup param array. */
187         state->param = talloc_zero_array(state, uint8_t, 6);
188         if (tevent_req_nomem(state->param, req)) {
189                 return tevent_req_post(req, ev);
190         }
191         SSVAL(state->param, 0, level);
192
193         state->param = trans2_bytes_push_str(
194                 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195         if (tevent_req_nomem(state->param, req)) {
196                 return tevent_req_post(req, ev);
197         }
198
199         subreq = cli_trans_send(
200                 state,                  /* mem ctx. */
201                 ev,                     /* event ctx. */
202                 cli,                    /* cli_state. */
203                 SMBtrans2,              /* cmd. */
204                 NULL,                   /* pipe name. */
205                 -1,                     /* fid. */
206                 0,                      /* function. */
207                 0,                      /* flags. */
208                 &state->setup,          /* setup. */
209                 1,                      /* num setup uint16_t words. */
210                 0,                      /* max returned setup. */
211                 state->param,           /* param. */
212                 talloc_get_size(state->param),  /* num param. */
213                 2,                      /* max returned param. */
214                 data,                   /* data. */
215                 data_len,               /* num data. */
216                 0);                     /* max returned data. */
217
218         if (tevent_req_nomem(subreq, req)) {
219                 return tevent_req_post(req, ev);
220         }
221         tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222         return req;
223 }
224
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
226 {
227         NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228                                          NULL, 0, NULL, NULL, 0, NULL);
229         tevent_req_simple_finish_ntstatus(subreq, status);
230 }
231
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
233 {
234         return tevent_req_simple_recv_ntstatus(req);
235 }
236
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238                          uint16_t level,
239                          const char *path,
240                          uint8_t *data,
241                          size_t data_len)
242 {
243         TALLOC_CTX *frame = talloc_stackframe();
244         struct tevent_context *ev;
245         struct tevent_req *req;
246         NTSTATUS status = NT_STATUS_NO_MEMORY;
247
248         if (smbXcli_conn_has_async_calls(cli->conn)) {
249                 /*
250                  * Can't use sync call while an async call is in flight
251                  */
252                 status = NT_STATUS_INVALID_PARAMETER;
253                 goto fail;
254         }
255         ev = samba_tevent_context_init(frame);
256         if (ev == NULL) {
257                 goto fail;
258         }
259         req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260         if (req == NULL) {
261                 goto fail;
262         }
263         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264                 goto fail;
265         }
266         status = cli_setpathinfo_recv(req);
267  fail:
268         TALLOC_FREE(frame);
269         return status;
270 }
271
272 /****************************************************************************
273  Hard/Symlink a file (UNIX extensions).
274  Creates new name (sym)linked to oldname.
275 ****************************************************************************/
276
277 struct cli_posix_link_internal_state {
278         uint8_t *data;
279 };
280
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
282
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284                                         struct tevent_context *ev,
285                                         struct cli_state *cli,
286                                         uint16_t level,
287                                         const char *oldname,
288                                         const char *newname)
289 {
290         struct tevent_req *req = NULL, *subreq = NULL;
291         struct cli_posix_link_internal_state *state = NULL;
292
293         req = tevent_req_create(mem_ctx, &state,
294                                 struct cli_posix_link_internal_state);
295         if (req == NULL) {
296                 return NULL;
297         }
298
299         /* Setup data array. */
300         state->data = talloc_array(state, uint8_t, 0);
301         if (tevent_req_nomem(state->data, req)) {
302                 return tevent_req_post(req, ev);
303         }
304         state->data = trans2_bytes_push_str(
305                 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
306
307         subreq = cli_setpathinfo_send(
308                 state, ev, cli, level, newname,
309                 state->data, talloc_get_size(state->data));
310         if (tevent_req_nomem(subreq, req)) {
311                 return tevent_req_post(req, ev);
312         }
313         tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314         return req;
315 }
316
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
318 {
319         NTSTATUS status = cli_setpathinfo_recv(subreq);
320         tevent_req_simple_finish_ntstatus(subreq, status);
321 }
322
323 /****************************************************************************
324  Symlink a file (UNIX extensions).
325 ****************************************************************************/
326
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328                                         struct tevent_context *ev,
329                                         struct cli_state *cli,
330                                         const char *oldname,
331                                         const char *newname)
332 {
333         return cli_posix_link_internal_send(
334                 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
335 }
336
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
338 {
339         return tevent_req_simple_recv_ntstatus(req);
340 }
341
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343                         const char *oldname,
344                         const char *newname)
345 {
346         TALLOC_CTX *frame = talloc_stackframe();
347         struct tevent_context *ev = NULL;
348         struct tevent_req *req = NULL;
349         NTSTATUS status = NT_STATUS_OK;
350
351         if (smbXcli_conn_has_async_calls(cli->conn)) {
352                 /*
353                  * Can't use sync call while an async call is in flight
354                  */
355                 status = NT_STATUS_INVALID_PARAMETER;
356                 goto fail;
357         }
358
359         ev = samba_tevent_context_init(frame);
360         if (ev == NULL) {
361                 status = NT_STATUS_NO_MEMORY;
362                 goto fail;
363         }
364
365         req = cli_posix_symlink_send(frame,
366                                 ev,
367                                 cli,
368                                 oldname,
369                                 newname);
370         if (req == NULL) {
371                 status = NT_STATUS_NO_MEMORY;
372                 goto fail;
373         }
374
375         if (!tevent_req_poll_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, const char *path, uint64_t *bsize,
4203                        uint64_t *total, uint64_t *avail)
4204 {
4205         uint64_t sectors_per_block;
4206         uint64_t bytes_per_sector;
4207         int old_bsize, old_total, old_avail;
4208         NTSTATUS status;
4209
4210         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4211                 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4212         }
4213
4214         /*
4215          * Try the trans2 disk full size info call first.
4216          * We already use this in SMBC_fstatvfs_ctx().
4217          * Ignore 'actual_available_units' as we only
4218          * care about the quota for the caller.
4219          */
4220
4221         status = cli_get_fs_full_size_info(cli,
4222                         total,
4223                         avail,
4224                         NULL,
4225                         &sectors_per_block,
4226                         &bytes_per_sector);
4227
4228         /* Try and cope will all varients of "we don't do this call"
4229            and fall back to cli_dskattr. */
4230
4231         if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4232                         NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4233                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4234                         NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4235                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4236                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4237                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4238                         NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4239                         NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4240                         NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4241                 goto try_dskattr;
4242         }
4243
4244         if (!NT_STATUS_IS_OK(status)) {
4245                 return status;
4246         }
4247
4248         if (bsize) {
4249                 *bsize = sectors_per_block *
4250                          bytes_per_sector;
4251         }
4252
4253         return NT_STATUS_OK;
4254
4255   try_dskattr:
4256
4257         /* Old SMB1 core protocol fallback. */
4258         status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4259         if (!NT_STATUS_IS_OK(status)) {
4260                 return status;
4261         }
4262         if (bsize) {
4263                 *bsize = (uint64_t)old_bsize;
4264         }
4265         if (total) {
4266                 *total = (uint64_t)old_total;
4267         }
4268         if (avail) {
4269                 *avail = (uint64_t)old_avail;
4270         }
4271         return NT_STATUS_OK;
4272 }
4273
4274 /****************************************************************************
4275  Create and open a temporary file.
4276 ****************************************************************************/
4277
4278 static void cli_ctemp_done(struct tevent_req *subreq);
4279
4280 struct ctemp_state {
4281         uint16_t vwv[3];
4282         char *ret_path;
4283         uint16_t fnum;
4284 };
4285
4286 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4287                                 struct tevent_context *ev,
4288                                 struct cli_state *cli,
4289                                 const char *path)
4290 {
4291         struct tevent_req *req = NULL, *subreq = NULL;
4292         struct ctemp_state *state = NULL;
4293         uint8_t additional_flags = 0;
4294         uint8_t *bytes = NULL;
4295
4296         req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4297         if (req == NULL) {
4298                 return NULL;
4299         }
4300
4301         SSVAL(state->vwv,0,0);
4302         SIVALS(state->vwv+1,0,-1);
4303
4304         bytes = talloc_array(state, uint8_t, 1);
4305         if (tevent_req_nomem(bytes, req)) {
4306                 return tevent_req_post(req, ev);
4307         }
4308         bytes[0] = 4;
4309         bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4310                                    strlen(path)+1, NULL);
4311         if (tevent_req_nomem(bytes, req)) {
4312                 return tevent_req_post(req, ev);
4313         }
4314
4315         subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4316                               3, state->vwv, talloc_get_size(bytes), bytes);
4317         if (tevent_req_nomem(subreq, req)) {
4318                 return tevent_req_post(req, ev);
4319         }
4320         tevent_req_set_callback(subreq, cli_ctemp_done, req);
4321         return req;
4322 }
4323
4324 static void cli_ctemp_done(struct tevent_req *subreq)
4325 {
4326         struct tevent_req *req = tevent_req_callback_data(
4327                                 subreq, struct tevent_req);
4328         struct ctemp_state *state = tevent_req_data(
4329                                 req, struct ctemp_state);
4330         NTSTATUS status;
4331         uint8_t wcnt;
4332         uint16_t *vwv;
4333         uint32_t num_bytes = 0;
4334         uint8_t *bytes = NULL;
4335
4336         status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4337                               &num_bytes, &bytes);
4338         TALLOC_FREE(subreq);
4339         if (tevent_req_nterror(req, status)) {
4340                 return;
4341         }
4342
4343         state->fnum = SVAL(vwv+0, 0);
4344
4345         /* From W2K3, the result is just the ASCII name */
4346         if (num_bytes < 2) {
4347                 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4348                 return;
4349         }
4350
4351         if (pull_string_talloc(state,
4352                         NULL,
4353                         0,
4354                         &state->ret_path,
4355                         bytes,
4356                         num_bytes,
4357                         STR_ASCII) == 0) {
4358                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4359                 return;
4360         }
4361         tevent_req_done(req);
4362 }
4363
4364 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4365                         TALLOC_CTX *ctx,
4366                         uint16_t *pfnum,
4367                         char **outfile)
4368 {
4369         struct ctemp_state *state = tevent_req_data(req,
4370                         struct ctemp_state);
4371         NTSTATUS status;
4372
4373         if (tevent_req_is_nterror(req, &status)) {
4374                 return status;
4375         }
4376         *pfnum = state->fnum;
4377         *outfile = talloc_strdup(ctx, state->ret_path);
4378         if (!*outfile) {
4379                 return NT_STATUS_NO_MEMORY;
4380         }
4381         return NT_STATUS_OK;
4382 }
4383
4384 NTSTATUS cli_ctemp(struct cli_state *cli,
4385                         TALLOC_CTX *ctx,
4386                         const char *path,
4387                         uint16_t *pfnum,
4388                         char **out_path)
4389 {
4390         TALLOC_CTX *frame = talloc_stackframe();
4391         struct tevent_context *ev;
4392         struct tevent_req *req;
4393         NTSTATUS status = NT_STATUS_OK;
4394
4395         if (smbXcli_conn_has_async_calls(cli->conn)) {
4396                 /*
4397                  * Can't use sync call while an async call is in flight
4398                  */
4399                 status = NT_STATUS_INVALID_PARAMETER;
4400                 goto fail;
4401         }
4402
4403         ev = samba_tevent_context_init(frame);
4404         if (ev == NULL) {
4405                 status = NT_STATUS_NO_MEMORY;
4406                 goto fail;
4407         }
4408
4409         req = cli_ctemp_send(frame, ev, cli, path);
4410         if (req == NULL) {
4411                 status = NT_STATUS_NO_MEMORY;
4412                 goto fail;
4413         }
4414
4415         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4416                 goto fail;
4417         }
4418
4419         status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4420
4421  fail:
4422         TALLOC_FREE(frame);
4423         return status;
4424 }
4425
4426 /*
4427    send a raw ioctl - used by the torture code
4428 */
4429 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4430 {
4431         uint16_t vwv[3];
4432         NTSTATUS status;
4433
4434         SSVAL(vwv+0, 0, fnum);
4435         SSVAL(vwv+1, 0, code>>16);
4436         SSVAL(vwv+2, 0, (code&0xFFFF));
4437
4438         status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4439                          NULL, 0, NULL, NULL, NULL, NULL);
4440         if (!NT_STATUS_IS_OK(status)) {
4441                 return status;
4442         }
4443         *blob = data_blob_null;
4444         return NT_STATUS_OK;
4445 }
4446
4447 /*********************************************************
4448  Set an extended attribute utility fn.
4449 *********************************************************/
4450
4451 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4452                            uint8_t *param, unsigned int param_len,
4453                            const char *ea_name,
4454                            const char *ea_val, size_t ea_len)
4455 {
4456         uint16_t setup[1];
4457         unsigned int data_len = 0;
4458         uint8_t *data = NULL;
4459         char *p;
4460         size_t ea_namelen = strlen(ea_name);
4461         NTSTATUS status;
4462
4463         SSVAL(setup, 0, setup_val);
4464
4465         if (ea_namelen == 0 && ea_len == 0) {
4466                 data_len = 4;
4467                 data = talloc_array(talloc_tos(),
4468                                 uint8_t,
4469                                 data_len);
4470                 if (!data) {
4471                         return NT_STATUS_NO_MEMORY;
4472                 }
4473                 p = (char *)data;
4474                 SIVAL(p,0,data_len);
4475         } else {
4476                 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4477                 data = talloc_array(talloc_tos(),
4478                                 uint8_t,
4479                                 data_len);
4480                 if (!data) {
4481                         return NT_STATUS_NO_MEMORY;
4482                 }
4483                 p = (char *)data;
4484                 SIVAL(p,0,data_len);
4485                 p += 4;
4486                 SCVAL(p, 0, 0); /* EA flags. */
4487                 SCVAL(p, 1, ea_namelen);
4488                 SSVAL(p, 2, ea_len);
4489                 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4490                 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4491         }
4492
4493         status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4494                            setup, 1, 0,
4495                            param, param_len, 2,
4496                            data,  data_len, CLI_BUFFER_SIZE,
4497                            NULL,
4498                            NULL, 0, NULL, /* rsetup */
4499                            NULL, 0, NULL, /* rparam */
4500                            NULL, 0, NULL); /* rdata */
4501         talloc_free(data);
4502         return status;
4503 }
4504
4505 /*********************************************************
4506  Set an extended attribute on a pathname.
4507 *********************************************************/
4508
4509 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4510                          const char *ea_name, const char *ea_val,
4511                          size_t ea_len)
4512 {
4513         unsigned int param_len = 0;
4514         uint8_t *param;
4515         NTSTATUS status;
4516         TALLOC_CTX *frame = NULL;
4517
4518         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4519                 return cli_smb2_set_ea_path(cli,
4520                                         path,
4521                                         ea_name,
4522                                         ea_val,
4523                                         ea_len);
4524         }
4525
4526         frame = talloc_stackframe();
4527
4528         param = talloc_array(frame, uint8_t, 6);
4529         if (!param) {
4530                 status = NT_STATUS_NO_MEMORY;
4531                 goto fail;
4532         }
4533         SSVAL(param,0,SMB_INFO_SET_EA);
4534         SSVAL(param,2,0);
4535         SSVAL(param,4,0);
4536
4537         param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4538                                       path, strlen(path)+1,
4539                                       NULL);
4540         param_len = talloc_get_size(param);
4541
4542         status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4543                             ea_name, ea_val, ea_len);
4544
4545   fail:
4546
4547         TALLOC_FREE(frame);
4548         return status;
4549 }
4550
4551 /*********************************************************
4552  Set an extended attribute on an fnum.
4553 *********************************************************/
4554
4555 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4556                          const char *ea_name, const char *ea_val,
4557                          size_t ea_len)
4558 {
4559         uint8_t param[6];
4560
4561         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4562                 return cli_smb2_set_ea_fnum(cli,
4563                                         fnum,
4564                                         ea_name,
4565                                         ea_val,
4566                                         ea_len);
4567         }
4568
4569         memset(param, 0, 6);
4570         SSVAL(param,0,fnum);
4571         SSVAL(param,2,SMB_INFO_SET_EA);
4572
4573         return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4574                           ea_name, ea_val, ea_len);
4575 }
4576
4577 /*********************************************************
4578  Get an extended attribute list utility fn.
4579 *********************************************************/
4580
4581 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4582                           size_t rdata_len,
4583                           size_t *pnum_eas, struct ea_struct **pea_list)
4584 {
4585         struct ea_struct *ea_list = NULL;
4586         size_t num_eas;
4587         size_t ea_size;
4588         const uint8_t *p;
4589
4590         if (rdata_len < 4) {
4591                 return false;
4592         }
4593
4594         ea_size = (size_t)IVAL(rdata,0);
4595         if (ea_size > rdata_len) {
4596                 return false;
4597         }
4598
4599         if (ea_size == 0) {
4600                 /* No EA's present. */
4601                 *pnum_eas = 0;
4602                 *pea_list = NULL;
4603                 return true;
4604         }
4605
4606         p = rdata + 4;
4607         ea_size -= 4;
4608
4609         /* Validate the EA list and count it. */
4610         for (num_eas = 0; ea_size >= 4; num_eas++) {
4611                 unsigned int ea_namelen = CVAL(p,1);
4612                 unsigned int ea_valuelen = SVAL(p,2);
4613                 if (ea_namelen == 0) {
4614                         return false;
4615                 }
4616                 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4617                         return false;
4618                 }
4619                 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4620                 p += 4 + ea_namelen + 1 + ea_valuelen;
4621         }
4622
4623         if (num_eas == 0) {
4624                 *pnum_eas = 0;
4625                 *pea_list = NULL;
4626                 return true;
4627         }
4628
4629         *pnum_eas = num_eas;
4630         if (!pea_list) {
4631                 /* Caller only wants number of EA's. */
4632                 return true;
4633         }
4634
4635         ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4636         if (!ea_list) {
4637                 return false;
4638         }
4639
4640         ea_size = (size_t)IVAL(rdata,0);
4641         p = rdata + 4;
4642
4643         for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4644                 struct ea_struct *ea = &ea_list[num_eas];
4645                 fstring unix_ea_name;
4646                 unsigned int ea_namelen = CVAL(p,1);
4647                 unsigned int ea_valuelen = SVAL(p,2);
4648
4649                 ea->flags = CVAL(p,0);
4650                 unix_ea_name[0] = '\0';
4651                 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4652                 ea->name = talloc_strdup(ea_list, unix_ea_name);
4653                 if (!ea->name) {
4654                         goto fail;
4655                 }
4656                 /* Ensure the value is null terminated (in case it's a string). */
4657                 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4658                 if (!ea->value.data) {
4659                         goto fail;
4660                 }
4661                 if (ea_valuelen) {
4662                         memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4663                 }
4664                 ea->value.data[ea_valuelen] = 0;
4665                 ea->value.length--;
4666                 p += 4 + ea_namelen + 1 + ea_valuelen;
4667         }
4668
4669         *pea_list = ea_list;
4670         return true;
4671
4672 fail:
4673         TALLOC_FREE(ea_list);
4674         return false;
4675 }
4676
4677 /*********************************************************
4678  Get an extended attribute list from a pathname.
4679 *********************************************************/
4680
4681 struct cli_get_ea_list_path_state {
4682         uint32_t num_data;
4683         uint8_t *data;
4684 };
4685
4686 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4687
4688 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4689                                              struct tevent_context *ev,
4690                                              struct cli_state *cli,
4691                                              const char *fname)
4692 {
4693         struct tevent_req *req, *subreq;
4694         struct cli_get_ea_list_path_state *state;
4695
4696         req = tevent_req_create(mem_ctx, &state,
4697                                 struct cli_get_ea_list_path_state);
4698         if (req == NULL) {
4699                 return NULL;
4700         }
4701         subreq = cli_qpathinfo_send(state, ev, cli, fname,
4702                                     SMB_INFO_QUERY_ALL_EAS, 4,
4703                                     CLI_BUFFER_SIZE);
4704         if (tevent_req_nomem(subreq, req)) {
4705                 return tevent_req_post(req, ev);
4706         }
4707         tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4708         return req;
4709 }
4710
4711 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4712 {
4713         struct tevent_req *req = tevent_req_callback_data(
4714                                 subreq, struct tevent_req);
4715         struct cli_get_ea_list_path_state *state = tevent_req_data(
4716                 req, struct cli_get_ea_list_path_state);
4717         NTSTATUS status;
4718
4719         status = cli_qpathinfo_recv(subreq, state, &state->data,
4720                                     &state->num_data);
4721         TALLOC_FREE(subreq);
4722         if (tevent_req_nterror(req, status)) {
4723                 return;
4724         }
4725         tevent_req_done(req);
4726 }
4727
4728 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4729                                    size_t *pnum_eas, struct ea_struct **peas)
4730 {
4731         struct cli_get_ea_list_path_state *state = tevent_req_data(
4732                 req, struct cli_get_ea_list_path_state);
4733         NTSTATUS status;
4734
4735         if (tevent_req_is_nterror(req, &status)) {
4736                 return status;
4737         }
4738         if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4739                            pnum_eas, peas)) {
4740                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4741         }
4742         return NT_STATUS_OK;
4743 }
4744
4745 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4746                 TALLOC_CTX *ctx,
4747                 size_t *pnum_eas,
4748                 struct ea_struct **pea_list)
4749 {
4750         TALLOC_CTX *frame = NULL;
4751         struct tevent_context *ev = NULL;
4752         struct tevent_req *req = NULL;
4753         NTSTATUS status = NT_STATUS_NO_MEMORY;
4754
4755         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4756                 return cli_smb2_get_ea_list_path(cli,
4757                                         path,
4758                                         ctx,
4759                                         pnum_eas,
4760                                         pea_list);
4761         }
4762
4763         frame = talloc_stackframe();
4764
4765         if (smbXcli_conn_has_async_calls(cli->conn)) {
4766                 /*
4767                  * Can't use sync call while an async call is in flight
4768                  */
4769                 status = NT_STATUS_INVALID_PARAMETER;
4770                 goto fail;
4771         }
4772         ev = samba_tevent_context_init(frame);
4773         if (ev == NULL) {
4774                 goto fail;
4775         }
4776         req = cli_get_ea_list_path_send(frame, ev, cli, path);
4777         if (req == NULL) {
4778                 goto fail;
4779         }
4780         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4781                 goto fail;
4782         }
4783         status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4784  fail:
4785         TALLOC_FREE(frame);
4786         return status;
4787 }
4788
4789 /****************************************************************************
4790  Convert open "flags" arg to uint32_t on wire.
4791 ****************************************************************************/
4792
4793 static uint32_t open_flags_to_wire(int flags)
4794 {
4795         int open_mode = flags & O_ACCMODE;
4796         uint32_t ret = 0;
4797
4798         switch (open_mode) {
4799                 case O_WRONLY:
4800                         ret |= SMB_O_WRONLY;
4801                         break;
4802                 case O_RDWR:
4803                         ret |= SMB_O_RDWR;
4804                         break;
4805                 default:
4806                 case O_RDONLY:
4807                         ret |= SMB_O_RDONLY;
4808                         break;
4809         }
4810
4811         if (flags & O_CREAT) {
4812                 ret |= SMB_O_CREAT;
4813         }
4814         if (flags & O_EXCL) {
4815                 ret |= SMB_O_EXCL;
4816         }
4817         if (flags & O_TRUNC) {
4818                 ret |= SMB_O_TRUNC;
4819         }
4820 #if defined(O_SYNC)
4821         if (flags & O_SYNC) {
4822                 ret |= SMB_O_SYNC;
4823         }
4824 #endif /* O_SYNC */
4825         if (flags & O_APPEND) {
4826                 ret |= SMB_O_APPEND;
4827         }
4828 #if defined(O_DIRECT)
4829         if (flags & O_DIRECT) {
4830                 ret |= SMB_O_DIRECT;
4831         }
4832 #endif
4833 #if defined(O_DIRECTORY)
4834         if (flags & O_DIRECTORY) {
4835                 ret |= SMB_O_DIRECTORY;
4836         }
4837 #endif
4838         return ret;
4839 }
4840
4841 /****************************************************************************
4842  Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4843 ****************************************************************************/
4844
4845 struct posix_open_state {
4846         uint16_t setup;
4847         uint8_t *param;
4848         uint8_t data[18];
4849         uint16_t fnum; /* Out */
4850 };
4851
4852 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4853 {
4854         struct tevent_req *req = tevent_req_callback_data(
4855                                 subreq, struct tevent_req);
4856         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4857         NTSTATUS status;
4858         uint8_t *data;
4859         uint32_t num_data;
4860
4861         status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4862                                 NULL, 0, NULL, &data, 12, &num_data);
4863         TALLOC_FREE(subreq);
4864         if (tevent_req_nterror(req, status)) {
4865                 return;
4866         }
4867         state->fnum = SVAL(data,2);
4868         tevent_req_done(req);
4869 }
4870
4871 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4872                                         struct tevent_context *ev,
4873                                         struct cli_state *cli,
4874                                         const char *fname,
4875                                         int flags,
4876                                         mode_t mode,
4877                                         bool is_dir)
4878 {
4879         struct tevent_req *req = NULL, *subreq = NULL;
4880         struct posix_open_state *state = NULL;
4881         uint32_t wire_flags = open_flags_to_wire(flags);
4882
4883         req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4884         if (req == NULL) {
4885                 return NULL;
4886         }
4887
4888         /* Setup setup word. */
4889         SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4890
4891         /* Setup param array. */
4892         state->param = talloc_array(state, uint8_t, 6);
4893         if (tevent_req_nomem(state->param, req)) {
4894                 return tevent_req_post(req, ev);
4895         }
4896         memset(state->param, '\0', 6);
4897         SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4898
4899         state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4900                                    strlen(fname)+1, NULL);
4901
4902         if (tevent_req_nomem(state->param, req)) {
4903                 return tevent_req_post(req, ev);
4904         }
4905
4906         /* Setup data words. */
4907         if (is_dir) {
4908                 wire_flags |= SMB_O_DIRECTORY;
4909         }
4910
4911         SIVAL(state->data,0,0); /* No oplock. */
4912         SIVAL(state->data,4,wire_flags);
4913         SIVAL(state->data,8,unix_perms_to_wire(mode));
4914         SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4915         SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4916
4917         subreq = cli_trans_send(state,                  /* mem ctx. */
4918                                 ev,                     /* event ctx. */
4919                                 cli,                    /* cli_state. */
4920                                 SMBtrans2,              /* cmd. */
4921                                 NULL,                   /* pipe name. */
4922                                 -1,                     /* fid. */
4923                                 0,                      /* function. */
4924                                 0,                      /* flags. */
4925                                 &state->setup,          /* setup. */
4926                                 1,                      /* num setup uint16_t words. */
4927                                 0,                      /* max returned setup. */
4928                                 state->param,           /* param. */
4929                                 talloc_get_size(state->param),/* num param. */
4930                                 2,                      /* max returned param. */
4931                                 state->data,            /* data. */
4932                                 18,                     /* num data. */
4933                                 12);                    /* max returned data. */
4934
4935         if (tevent_req_nomem(subreq, req)) {
4936                 return tevent_req_post(req, ev);
4937         }
4938         tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4939         return req;
4940 }
4941
4942 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4943                                         struct tevent_context *ev,
4944                                         struct cli_state *cli,
4945                                         const char *fname,
4946                                         int flags,
4947                                         mode_t mode)
4948 {
4949         return cli_posix_open_internal_send(mem_ctx, ev,
4950                                 cli, fname, flags, mode, false);
4951 }
4952
4953 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4954 {
4955         struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4956         NTSTATUS status;
4957
4958         if (tevent_req_is_nterror(req, &status)) {
4959                 return status;
4960         }
4961         *pfnum = state->fnum;
4962         return NT_STATUS_OK;
4963 }
4964
4965 /****************************************************************************
4966  Open - POSIX semantics. Doesn't request oplock.
4967 ****************************************************************************/
4968
4969 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4970                         int flags, mode_t mode, uint16_t *pfnum)
4971 {
4972
4973         TALLOC_CTX *frame = talloc_stackframe();
4974         struct tevent_context *ev = NULL;
4975         struct tevent_req *req = NULL;
4976         NTSTATUS status = NT_STATUS_OK;
4977
4978         if (smbXcli_conn_has_async_calls(cli->conn)) {
4979                 /*
4980                  * Can't use sync call while an async call is in flight
4981                  */
4982                 status = NT_STATUS_INVALID_PARAMETER;
4983                 goto fail;
4984         }
4985
4986         ev = samba_tevent_context_init(frame);
4987         if (ev == NULL) {
4988                 status = NT_STATUS_NO_MEMORY;
4989                 goto fail;
4990         }
4991
4992         req = cli_posix_open_send(frame,
4993                                 ev,
4994                                 cli,
4995                                 fname,
4996                                 flags,
4997                                 mode);
4998         if (req == NULL) {
4999                 status = NT_STATUS_NO_MEMORY;
5000                 goto fail;
5001         }
5002
5003         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5004                 goto fail;
5005         }
5006
5007         status = cli_posix_open_recv(req, pfnum);
5008
5009  fail:
5010         TALLOC_FREE(frame);
5011         return status;
5012 }
5013
5014 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5015                                         struct tevent_context *ev,
5016                                         struct cli_state *cli,
5017                                         const char *fname,
5018                                         mode_t mode)
5019 {
5020         return cli_posix_open_internal_send(mem_ctx, ev,
5021                                 cli, fname, O_CREAT, mode, true);
5022 }
5023
5024 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5025 {
5026         return tevent_req_simple_recv_ntstatus(req);
5027 }
5028
5029 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5030 {
5031         TALLOC_CTX *frame = talloc_stackframe();
5032         struct tevent_context *ev = NULL;
5033         struct tevent_req *req = NULL;
5034         NTSTATUS status = NT_STATUS_OK;
5035
5036         if (smbXcli_conn_has_async_calls(cli->conn)) {
5037                 /*
5038                  * Can't use sync call while an async call is in flight
5039                  */
5040                 status = NT_STATUS_INVALID_PARAMETER;
5041                 goto fail;
5042         }
5043
5044         ev = samba_tevent_context_init(frame);
5045         if (ev == NULL) {
5046                 status = NT_STATUS_NO_MEMORY;
5047                 goto fail;
5048         }
5049
5050         req = cli_posix_mkdir_send(frame,
5051                                 ev,
5052                                 cli,
5053                                 fname,
5054                                 mode);
5055         if (req == NULL) {
5056                 status = NT_STATUS_NO_MEMORY;
5057                 goto fail;
5058         }
5059
5060         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5061                 goto fail;
5062         }
5063
5064         status = cli_posix_mkdir_recv(req);
5065
5066  fail:
5067         TALLOC_FREE(frame);
5068         return status;
5069 }
5070
5071 /****************************************************************************
5072  unlink or rmdir - POSIX semantics.
5073 ****************************************************************************/
5074
5075 struct cli_posix_unlink_internal_state {
5076         uint8_t data[2];
5077 };
5078
5079 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5080
5081 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5082                                         struct tevent_context *ev,
5083                                         struct cli_state *cli,
5084                                         const char *fname,
5085                                         uint16_t level)
5086 {
5087         struct tevent_req *req = NULL, *subreq = NULL;
5088         struct cli_posix_unlink_internal_state *state = NULL;
5089
5090         req = tevent_req_create(mem_ctx, &state,
5091                                 struct cli_posix_unlink_internal_state);
5092         if (req == NULL) {
5093                 return NULL;
5094         }
5095
5096         /* Setup data word. */
5097         SSVAL(state->data, 0, level);
5098
5099         subreq = cli_setpathinfo_send(state, ev, cli,
5100                                       SMB_POSIX_PATH_UNLINK,
5101                                       fname,
5102                                       state->data, sizeof(state->data));
5103         if (tevent_req_nomem(subreq, req)) {
5104                 return tevent_req_post(req, ev);
5105         }
5106         tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5107         return req;
5108 }
5109
5110 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5111 {
5112         NTSTATUS status = cli_setpathinfo_recv(subreq);
5113         tevent_req_simple_finish_ntstatus(subreq, status);
5114 }
5115
5116 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5117                                         struct tevent_context *ev,
5118                                         struct cli_state *cli,
5119                                         const char *fname)
5120 {
5121         return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5122                                               SMB_POSIX_UNLINK_FILE_TARGET);
5123 }
5124
5125 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5126 {
5127         return tevent_req_simple_recv_ntstatus(req);
5128 }
5129
5130 /****************************************************************************
5131  unlink - POSIX semantics.
5132 ****************************************************************************/
5133
5134 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5135 {
5136         TALLOC_CTX *frame = talloc_stackframe();
5137         struct tevent_context *ev = NULL;
5138         struct tevent_req *req = NULL;
5139         NTSTATUS status = NT_STATUS_OK;
5140
5141         if (smbXcli_conn_has_async_calls(cli->conn)) {
5142                 /*
5143                  * Can't use sync call while an async call is in flight
5144                  */
5145                 status = NT_STATUS_INVALID_PARAMETER;
5146                 goto fail;
5147         }
5148
5149         ev = samba_tevent_context_init(frame);
5150         if (ev == NULL) {
5151                 status = NT_STATUS_NO_MEMORY;
5152                 goto fail;
5153         }
5154
5155         req = cli_posix_unlink_send(frame,
5156                                 ev,
5157                                 cli,
5158                                 fname);
5159         if (req == NULL) {
5160                 status = NT_STATUS_NO_MEMORY;
5161                 goto fail;
5162         }
5163
5164         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5165                 goto fail;
5166         }
5167
5168         status = cli_posix_unlink_recv(req);
5169
5170  fail:
5171         TALLOC_FREE(frame);
5172         return status;
5173 }
5174
5175 /****************************************************************************
5176  rmdir - POSIX semantics.
5177 ****************************************************************************/
5178
5179 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5180                                         struct tevent_context *ev,
5181                                         struct cli_state *cli,
5182                                         const char *fname)
5183 {
5184         return cli_posix_unlink_internal_send(
5185                 mem_ctx, ev, cli, fname,
5186                 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5187 }
5188
5189 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5190 {
5191         return tevent_req_simple_recv_ntstatus(req);
5192 }
5193
5194 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5195 {
5196         TALLOC_CTX *frame = talloc_stackframe();
5197         struct tevent_context *ev = NULL;
5198         struct tevent_req *req = NULL;
5199         NTSTATUS status = NT_STATUS_OK;
5200
5201         if (smbXcli_conn_has_async_calls(cli->conn)) {
5202                 /*
5203                  * Can't use sync call while an async call is in flight
5204                  */
5205                 status = NT_STATUS_INVALID_PARAMETER;
5206                 goto fail;
5207         }
5208
5209         ev = samba_tevent_context_init(frame);
5210         if (ev == NULL) {
5211                 status = NT_STATUS_NO_MEMORY;
5212                 goto fail;
5213         }
5214
5215         req = cli_posix_rmdir_send(frame,
5216                                 ev,
5217                                 cli,
5218                                 fname);
5219         if (req == NULL) {
5220                 status = NT_STATUS_NO_MEMORY;
5221                 goto fail;
5222         }
5223
5224         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5225                 goto fail;
5226         }
5227
5228         status = cli_posix_rmdir_recv(req, frame);
5229
5230  fail:
5231         TALLOC_FREE(frame);
5232         return status;
5233 }
5234
5235 /****************************************************************************
5236  filechangenotify
5237 ****************************************************************************/
5238
5239 struct cli_notify_state {
5240         uint8_t setup[8];
5241         uint32_t num_changes;
5242         struct notify_change *changes;
5243 };
5244
5245 static void cli_notify_done(struct tevent_req *subreq);
5246
5247 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5248                                    struct tevent_context *ev,
5249                                    struct cli_state *cli, uint16_t fnum,
5250                                    uint32_t buffer_size,
5251                                    uint32_t completion_filter, bool recursive)
5252 {
5253         struct tevent_req *req, *subreq;
5254         struct cli_notify_state *state;
5255         unsigned old_timeout;
5256
5257         req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5258         if (req == NULL) {
5259                 return NULL;
5260         }
5261
5262         SIVAL(state->setup, 0, completion_filter);
5263         SSVAL(state->setup, 4, fnum);
5264         SSVAL(state->setup, 6, recursive);
5265
5266         /*
5267          * Notifies should not time out
5268          */
5269         old_timeout = cli_set_timeout(cli, 0);
5270
5271         subreq = cli_trans_send(
5272                 state,                  /* mem ctx. */
5273                 ev,                     /* event ctx. */
5274                 cli,                    /* cli_state. */
5275                 SMBnttrans,             /* cmd. */
5276                 NULL,                   /* pipe name. */
5277                 -1,                     /* fid. */
5278                 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5279                 0,                      /* flags. */
5280                 (uint16_t *)state->setup, /* setup. */
5281                 4,                      /* num setup uint16_t words. */
5282                 0,                      /* max returned setup. */
5283                 NULL,                   /* param. */
5284                 0,                      /* num param. */
5285                 buffer_size,            /* max returned param. */
5286                 NULL,                   /* data. */
5287                 0,                      /* num data. */
5288                 0);                     /* max returned data. */
5289
5290         cli_set_timeout(cli, old_timeout);
5291
5292         if (tevent_req_nomem(subreq, req)) {
5293                 return tevent_req_post(req, ev);
5294         }
5295         tevent_req_set_callback(subreq, cli_notify_done, req);
5296         return req;
5297 }
5298
5299 static void cli_notify_done(struct tevent_req *subreq)
5300 {
5301         struct tevent_req *req = tevent_req_callback_data(
5302                 subreq, struct tevent_req);
5303         struct cli_notify_state *state = tevent_req_data(
5304                 req, struct cli_notify_state);
5305         NTSTATUS status;
5306         uint8_t *params;
5307         uint32_t i, ofs, num_params;
5308         uint16_t flags2;
5309
5310         status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5311                                 &params, 0, &num_params, NULL, 0, NULL);
5312         TALLOC_FREE(subreq);
5313         if (tevent_req_nterror(req, status)) {
5314                 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5315                 return;
5316         }
5317
5318         state->num_changes = 0;
5319         ofs = 0;
5320
5321         while (num_params - ofs > 12) {
5322                 uint32_t next = IVAL(params, ofs);
5323                 state->num_changes += 1;
5324
5325                 if ((next == 0) || (ofs+next >= num_params)) {
5326                         break;
5327                 }
5328                 ofs += next;
5329         }
5330
5331         state->changes = talloc_array(state, struct notify_change,
5332                                       state->num_changes);
5333         if (tevent_req_nomem(state->changes, req)) {
5334                 TALLOC_FREE(params);
5335                 return;
5336         }
5337
5338         ofs = 0;
5339
5340         for (i=0; i<state->num_changes; i++) {
5341                 uint32_t next = IVAL(params, ofs);
5342                 uint32_t len = IVAL(params, ofs+8);
5343                 ssize_t ret;
5344                 char *name;
5345
5346                 if (trans_oob(num_params, ofs + 12, len)) {
5347                         TALLOC_FREE(params);
5348                         tevent_req_nterror(
5349                                 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5350                         return;
5351                 }
5352
5353                 state->changes[i].action = IVAL(params, ofs+4);
5354                 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5355                                          &name, params+ofs+12, len,
5356                                          STR_TERMINATE|STR_UNICODE);
5357                 if (ret == -1) {
5358                         TALLOC_FREE(params);
5359                         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5360                         return;
5361                 }
5362                 state->changes[i].name = name;
5363                 ofs += next;
5364         }
5365
5366         TALLOC_FREE(params);
5367         tevent_req_done(req);
5368 }
5369
5370 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5371                          uint32_t *pnum_changes,
5372                          struct notify_change **pchanges)
5373 {
5374         struct cli_notify_state *state = tevent_req_data(
5375                 req, struct cli_notify_state);
5376         NTSTATUS status;
5377
5378         if (tevent_req_is_nterror(req, &status)) {
5379                 return status;
5380         }
5381
5382         *pnum_changes = state->num_changes;
5383         *pchanges = talloc_move(mem_ctx, &state->changes);
5384         return NT_STATUS_OK;
5385 }
5386
5387 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5388                     uint32_t completion_filter, bool recursive,
5389                     TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5390                     struct notify_change **pchanges)
5391 {
5392         TALLOC_CTX *frame = talloc_stackframe();
5393         struct tevent_context *ev;
5394         struct tevent_req *req;
5395         NTSTATUS status = NT_STATUS_NO_MEMORY;
5396
5397         if (smbXcli_conn_has_async_calls(cli->conn)) {
5398                 /*
5399                  * Can't use sync call while an async call is in flight
5400                  */
5401                 status = NT_STATUS_INVALID_PARAMETER;
5402                 goto fail;
5403         }
5404         ev = samba_tevent_context_init(frame);
5405         if (ev == NULL) {
5406                 goto fail;
5407         }
5408         req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5409                               completion_filter, recursive);
5410         if (req == NULL) {
5411                 goto fail;
5412         }
5413         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5414                 goto fail;
5415         }
5416         status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5417  fail:
5418         TALLOC_FREE(frame);
5419         return status;
5420 }
5421
5422 struct cli_qpathinfo_state {
5423         uint8_t *param;
5424         uint8_t *data;
5425         uint16_t setup[1];
5426         uint32_t min_rdata;
5427         uint8_t *rdata;
5428         uint32_t num_rdata;
5429 };
5430
5431 static void cli_qpathinfo_done(struct tevent_req *subreq);
5432
5433 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5434                                       struct tevent_context *ev,
5435                                       struct cli_state *cli, const char *fname,
5436                                       uint16_t level, uint32_t min_rdata,
5437                                       uint32_t max_rdata)
5438 {
5439         struct tevent_req *req, *subreq;
5440         struct cli_qpathinfo_state *state;
5441
5442         req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5443         if (req == NULL) {
5444                 return NULL;
5445         }
5446         state->min_rdata = min_rdata;
5447         SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5448
5449         state->param = talloc_zero_array(state, uint8_t, 6);
5450         if (tevent_req_nomem(state->param, req)) {
5451                 return tevent_req_post(req, ev);
5452         }
5453         SSVAL(state->param, 0, level);
5454         state->param = trans2_bytes_push_str(
5455                 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5456         if (tevent_req_nomem(state->param, req)) {
5457                 return tevent_req_post(req, ev);
5458         }
5459
5460         subreq = cli_trans_send(
5461                 state,                  /* mem ctx. */
5462                 ev,                     /* event ctx. */
5463                 cli,                    /* cli_state. */
5464                 SMBtrans2,              /* cmd. */
5465                 NULL,                   /* pipe name. */
5466                 -1,                     /* fid. */
5467                 0,                      /* function. */
5468                 0,                      /* flags. */
5469                 state->setup,           /* setup. */
5470                 1,                      /* num setup uint16_t words. */
5471                 0,                      /* max returned setup. */
5472                 state->param,           /* param. */
5473                 talloc_get_size(state->param),  /* num param. */
5474                 2,                      /* max returned param. */
5475                 NULL,                   /* data. */
5476                 0,                      /* num data. */
5477                 max_rdata);             /* max returned data. */
5478
5479         if (tevent_req_nomem(subreq, req)) {
5480                 return tevent_req_post(req, ev);
5481         }
5482         tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5483         return req;
5484 }
5485
5486 static void cli_qpathinfo_done(struct tevent_req *subreq)
5487 {
5488         struct tevent_req *req = tevent_req_callback_data(
5489                 subreq, struct tevent_req);
5490         struct cli_qpathinfo_state *state = tevent_req_data(
5491                 req, struct cli_qpathinfo_state);
5492         NTSTATUS status;
5493
5494         status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5495                                 NULL, 0, NULL,
5496                                 &state->rdata, state->min_rdata,
5497                                 &state->num_rdata);
5498         if (tevent_req_nterror(req, status)) {
5499                 return;
5500         }
5501         tevent_req_done(req);
5502 }
5503
5504 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5505                             uint8_t **rdata, uint32_t *num_rdata)
5506 {
5507         struct cli_qpathinfo_state *state = tevent_req_data(
5508                 req, struct cli_qpathinfo_state);
5509         NTSTATUS status;
5510
5511         if (tevent_req_is_nterror(req, &status)) {
5512                 return status;
5513         }
5514         if (rdata != NULL) {
5515                 *rdata = talloc_move(mem_ctx, &state->rdata);
5516         } else {
5517                 TALLOC_FREE(state->rdata);
5518         }
5519         if (num_rdata != NULL) {
5520                 *num_rdata = state->num_rdata;
5521         }
5522         return NT_STATUS_OK;
5523 }
5524
5525 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5526                        const char *fname, uint16_t level, uint32_t min_rdata,
5527                        uint32_t max_rdata,
5528                        uint8_t **rdata, uint32_t *num_rdata)
5529 {
5530         TALLOC_CTX *frame = talloc_stackframe();
5531         struct tevent_context *ev;
5532         struct tevent_req *req;
5533         NTSTATUS status = NT_STATUS_NO_MEMORY;
5534
5535         if (smbXcli_conn_has_async_calls(cli->conn)) {
5536                 /*
5537                  * Can't use sync call while an async call is in flight
5538                  */
5539                 status = NT_STATUS_INVALID_PARAMETER;
5540                 goto fail;
5541         }
5542         ev = samba_tevent_context_init(frame);
5543         if (ev == NULL) {
5544                 goto fail;
5545         }
5546         req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5547                                  max_rdata);
5548         if (req == NULL) {
5549                 goto fail;
5550         }
5551         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5552                 goto fail;
5553         }
5554         status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5555  fail:
5556         TALLOC_FREE(frame);
5557         return status;
5558 }
5559
5560 struct cli_qfileinfo_state {
5561         uint16_t setup[1];
5562         uint8_t param[4];
5563         uint8_t *data;
5564         uint16_t recv_flags2;
5565         uint32_t min_rdata;
5566         uint8_t *rdata;
5567         uint32_t num_rdata;
5568 };
5569
5570 static void cli_qfileinfo_done(struct tevent_req *subreq);
5571
5572 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5573                                       struct tevent_context *ev,
5574                                       struct cli_state *cli, uint16_t fnum,
5575                                       uint16_t level, uint32_t min_rdata,
5576                                       uint32_t max_rdata)
5577 {
5578         struct tevent_req *req, *subreq;
5579         struct cli_qfileinfo_state *state;
5580
5581         req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5582         if (req == NULL) {
5583                 return NULL;
5584         }
5585         state->min_rdata = min_rdata;
5586         SSVAL(state->param, 0, fnum);
5587         SSVAL(state->param, 2, level);
5588         SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5589
5590         subreq = cli_trans_send(
5591                 state,                  /* mem ctx. */
5592                 ev,                     /* event ctx. */
5593                 cli,                    /* cli_state. */
5594                 SMBtrans2,              /* cmd. */
5595                 NULL,                   /* pipe name. */
5596                 -1,                     /* fid. */
5597                 0,                      /* function. */
5598                 0,                      /* flags. */
5599                 state->setup,           /* setup. */
5600                 1,                      /* num setup uint16_t words. */
5601                 0,                      /* max returned setup. */
5602                 state->param,           /* param. */
5603                 sizeof(state->param),   /* num param. */
5604                 2,                      /* max returned param. */
5605                 NULL,                   /* data. */
5606                 0,                      /* num data. */
5607                 max_rdata);             /* max returned data. */
5608
5609         if (tevent_req_nomem(subreq, req)) {
5610                 return tevent_req_post(req, ev);
5611         }
5612         tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5613         return req;
5614 }
5615
5616 static void cli_qfileinfo_done(struct tevent_req *subreq)
5617 {
5618         struct tevent_req *req = tevent_req_callback_data(
5619                 subreq, struct tevent_req);
5620         struct cli_qfileinfo_state *state = tevent_req_data(
5621                 req, struct cli_qfileinfo_state);
5622         NTSTATUS status;
5623
5624         status = cli_trans_recv(subreq, state,
5625                                 &state->recv_flags2,
5626                                 NULL, 0, NULL,
5627                                 NULL, 0, NULL,
5628                                 &state->rdata, state->min_rdata,
5629                                 &state->num_rdata);
5630         if (tevent_req_nterror(req, status)) {
5631                 return;
5632         }
5633         tevent_req_done(req);
5634 }
5635
5636 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5637                             uint16_t *recv_flags2,
5638                             uint8_t **rdata, uint32_t *num_rdata)
5639 {
5640         struct cli_qfileinfo_state *state = tevent_req_data(
5641                 req, struct cli_qfileinfo_state);
5642         NTSTATUS status;
5643
5644         if (tevent_req_is_nterror(req, &status)) {
5645                 return status;
5646         }
5647
5648         if (recv_flags2 != NULL) {
5649                 *recv_flags2 = state->recv_flags2;
5650         }
5651         if (rdata != NULL) {
5652                 *rdata = talloc_move(mem_ctx, &state->rdata);
5653         } else {
5654                 TALLOC_FREE(state->rdata);
5655         }
5656         if (num_rdata != NULL) {
5657                 *num_rdata = state->num_rdata;
5658         }
5659         return NT_STATUS_OK;
5660 }
5661
5662 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5663                        uint16_t fnum, uint16_t level, uint32_t min_rdata,
5664                        uint32_t max_rdata, uint16_t *recv_flags2,
5665                        uint8_t **rdata, uint32_t *num_rdata)
5666 {
5667         TALLOC_CTX *frame = talloc_stackframe();
5668         struct tevent_context *ev;
5669         struct tevent_req *req;
5670         NTSTATUS status = NT_STATUS_NO_MEMORY;
5671
5672         if (smbXcli_conn_has_async_calls(cli->conn)) {
5673                 /*
5674                  * Can't use sync call while an async call is in flight
5675                  */
5676                 status = NT_STATUS_INVALID_PARAMETER;
5677                 goto fail;
5678         }
5679         ev = samba_tevent_context_init(frame);
5680         if (ev == NULL) {
5681                 goto fail;
5682         }
5683         req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5684                                  max_rdata);
5685         if (req == NULL) {
5686                 goto fail;
5687         }
5688         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5689                 goto fail;
5690         }
5691         status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5692  fail:
5693         TALLOC_FREE(frame);
5694         return status;
5695 }
5696
5697 struct cli_flush_state {
5698         uint16_t vwv[1];
5699 };
5700
5701 static void cli_flush_done(struct tevent_req *subreq);
5702
5703 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5704                                   struct tevent_context *ev,
5705                                   struct cli_state *cli,
5706                                   uint16_t fnum)
5707 {
5708         struct tevent_req *req, *subreq;
5709         struct cli_flush_state *state;
5710
5711         req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5712         if (req == NULL) {
5713                 return NULL;
5714         }
5715         SSVAL(state->vwv + 0, 0, fnum);
5716
5717         subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5718                               0, NULL);
5719         if (tevent_req_nomem(subreq, req)) {
5720                 return tevent_req_post(req, ev);
5721         }
5722         tevent_req_set_callback(subreq, cli_flush_done, req);
5723         return req;
5724 }
5725
5726 static void cli_flush_done(struct tevent_req *subreq)
5727 {
5728         struct tevent_req *req = tevent_req_callback_data(
5729                 subreq, struct tevent_req);
5730         NTSTATUS status;
5731
5732         status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5733         TALLOC_FREE(subreq);
5734         if (tevent_req_nterror(req, status)) {
5735                 return;
5736         }
5737         tevent_req_done(req);
5738 }
5739
5740 NTSTATUS cli_flush_recv(struct tevent_req *req)
5741 {
5742         return tevent_req_simple_recv_ntstatus(req);
5743 }
5744
5745 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5746 {
5747         TALLOC_CTX *frame = talloc_stackframe();
5748         struct tevent_context *ev;
5749         struct tevent_req *req;
5750         NTSTATUS status = NT_STATUS_NO_MEMORY;
5751
5752         if (smbXcli_conn_has_async_calls(cli->conn)) {
5753                 /*
5754                  * Can't use sync call while an async call is in flight
5755                  */
5756                 status = NT_STATUS_INVALID_PARAMETER;
5757                 goto fail;
5758         }
5759         ev = samba_tevent_context_init(frame);
5760         if (ev == NULL) {
5761                 goto fail;
5762         }
5763         req = cli_flush_send(frame, ev, cli, fnum);
5764         if (req == NULL) {
5765                 goto fail;
5766         }
5767         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5768                 goto fail;
5769         }
5770         status = cli_flush_recv(req);
5771  fail:
5772         TALLOC_FREE(frame);
5773         return status;
5774 }
5775
5776 struct cli_shadow_copy_data_state {
5777         uint16_t setup[4];
5778         uint8_t *data;
5779         uint32_t num_data;
5780         bool get_names;
5781 };
5782
5783 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5784
5785 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5786                                              struct tevent_context *ev,
5787                                              struct cli_state *cli,
5788                                              uint16_t fnum,
5789                                              bool get_names)
5790 {
5791         struct tevent_req *req, *subreq;
5792         struct cli_shadow_copy_data_state *state;
5793         uint32_t ret_size;
5794
5795         req = tevent_req_create(mem_ctx, &state,
5796                                 struct cli_shadow_copy_data_state);
5797         if (req == NULL) {
5798                 return NULL;
5799         }
5800         state->get_names = get_names;
5801         ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5802
5803         SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5804         SSVAL(state->setup + 2, 0, fnum);
5805         SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5806         SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5807
5808         subreq = cli_trans_send(
5809                 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5810                 state->setup, ARRAY_SIZE(state->setup), 0,
5811                 NULL, 0, 0,
5812                 NULL, 0, ret_size);
5813         if (tevent_req_nomem(subreq, req)) {
5814                 return tevent_req_post(req, ev);
5815         }
5816         tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5817         return req;
5818 }
5819
5820 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5821 {
5822         struct tevent_req *req = tevent_req_callback_data(
5823                 subreq, struct tevent_req);
5824         struct cli_shadow_copy_data_state *state = tevent_req_data(
5825                 req, struct cli_shadow_copy_data_state);
5826         NTSTATUS status;
5827
5828         status = cli_trans_recv(subreq, state, NULL,
5829                                 NULL, 0, NULL, /* setup */
5830                                 NULL, 0, NULL, /* param */
5831                                 &state->data, 12, &state->num_data);
5832         TALLOC_FREE(subreq);
5833         if (tevent_req_nterror(req, status)) {
5834                 return;
5835         }
5836         tevent_req_done(req);
5837 }
5838
5839 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5840                                    char ***pnames, int *pnum_names)
5841 {
5842         struct cli_shadow_copy_data_state *state = tevent_req_data(
5843                 req, struct cli_shadow_copy_data_state);
5844         char **names;
5845         int i, num_names;
5846         uint32_t dlength;
5847         NTSTATUS status;
5848
5849         if (tevent_req_is_nterror(req, &status)) {
5850                 return status;
5851         }
5852         num_names = IVAL(state->data, 4);
5853         dlength = IVAL(state->data, 8);
5854
5855         if (!state->get_names) {
5856                 *pnum_names = num_names;
5857                 return NT_STATUS_OK;
5858         }
5859
5860         if (dlength+12 > state->num_data) {
5861                 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5862         }
5863         names = talloc_array(mem_ctx, char *, num_names);
5864         if (names == NULL) {
5865                 return NT_STATUS_NO_MEMORY;
5866         }
5867
5868         for (i=0; i<num_names; i++) {
5869                 bool ret;
5870                 uint8_t *src;
5871                 size_t converted_size;
5872
5873                 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5874                 ret = convert_string_talloc(
5875                         names, CH_UTF16LE, CH_UNIX,
5876                         src, 2 * sizeof(SHADOW_COPY_LABEL),
5877                         &names[i], &converted_size);
5878                 if (!ret) {
5879                         TALLOC_FREE(names);
5880                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
5881                 }
5882         }
5883         *pnum_names = num_names;
5884         *pnames = names;
5885         return NT_STATUS_OK;
5886 }
5887
5888 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5889                               uint16_t fnum, bool get_names,
5890                               char ***pnames, int *pnum_names)
5891 {
5892         TALLOC_CTX *frame = talloc_stackframe();
5893         struct tevent_context *ev;
5894         struct tevent_req *req;
5895         NTSTATUS status = NT_STATUS_NO_MEMORY;
5896
5897         if (smbXcli_conn_has_async_calls(cli->conn)) {
5898                 /*
5899                  * Can't use sync call while an async call is in flight
5900                  */
5901                 status = NT_STATUS_INVALID_PARAMETER;
5902                 goto fail;
5903         }
5904         ev = samba_tevent_context_init(frame);
5905         if (ev == NULL) {
5906                 goto fail;
5907         }
5908         req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5909         if (req == NULL) {
5910                 goto fail;
5911         }
5912         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5913                 goto fail;
5914         }
5915         status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5916  fail:
5917         TALLOC_FREE(frame);
5918         return status;
5919 }