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