2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
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.
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.
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/>.
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"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
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
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
41 size_t *pconverted_size)
45 size_t converted_size;
51 buflen = talloc_get_size(buf);
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
77 TALLOC_FREE(converted);
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
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)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
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 ***********************************************************/
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)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
155 memcpy(&buf[buflen], bytes, num_bytes);
159 struct cli_setpathinfo_state {
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
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);
191 SSVAL(state->param, 0, level);
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);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
202 cli, /* cli_state. */
203 SMBtrans2, /* cmd. */
204 NULL, /* pipe name. */
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. */
215 data_len, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq, req)) {
219 return tevent_req_post(req, ev);
221 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
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);
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
234 return tevent_req_simple_recv_ntstatus(req);
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct tevent_context *ev;
245 struct tevent_req *req;
246 NTSTATUS status = NT_STATUS_NO_MEMORY;
248 if (smbXcli_conn_has_async_calls(cli->conn)) {
250 * Can't use sync call while an async call is in flight
252 status = NT_STATUS_INVALID_PARAMETER;
255 ev = samba_tevent_context_init(frame);
259 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
263 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
266 status = cli_setpathinfo_recv(req);
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state {
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct cli_state *cli,
290 struct tevent_req *req = NULL, *subreq = NULL;
291 struct cli_posix_link_internal_state *state = NULL;
293 req = tevent_req_create(mem_ctx, &state,
294 struct cli_posix_link_internal_state);
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);
304 state->data = trans2_bytes_push_str(
305 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
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);
313 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
319 NTSTATUS status = cli_setpathinfo_recv(subreq);
320 tevent_req_simple_finish_ntstatus(subreq, status);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328 struct tevent_context *ev,
329 struct cli_state *cli,
333 return cli_posix_link_internal_send(
334 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
339 return tevent_req_simple_recv_ntstatus(req);
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
346 TALLOC_CTX *frame = talloc_stackframe();
347 struct tevent_context *ev = NULL;
348 struct tevent_req *req = NULL;
349 NTSTATUS status = NT_STATUS_OK;
351 if (smbXcli_conn_has_async_calls(cli->conn)) {
353 * Can't use sync call while an async call is in flight
355 status = NT_STATUS_INVALID_PARAMETER;
359 ev = samba_tevent_context_init(frame);
361 status = NT_STATUS_NO_MEMORY;
365 req = cli_posix_symlink_send(frame,
371 status = NT_STATUS_NO_MEMORY;
375 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
379 status = cli_posix_symlink_recv(req);
386 /****************************************************************************
387 Read a POSIX symlink.
388 ****************************************************************************/
390 struct readlink_state {
395 static void cli_posix_readlink_done(struct tevent_req *subreq);
397 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
398 struct tevent_context *ev,
399 struct cli_state *cli,
403 struct tevent_req *req = NULL, *subreq = NULL;
404 struct readlink_state *state = NULL;
405 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
407 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
413 * Len is in bytes, we need it in UCS2 units.
415 if ((2*len < len) || (maxbytelen < len)) {
416 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
417 return tevent_req_post(req, ev);
420 subreq = cli_qpathinfo_send(state, ev, cli, fname,
421 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
422 if (tevent_req_nomem(subreq, req)) {
423 return tevent_req_post(req, ev);
425 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
429 static void cli_posix_readlink_done(struct tevent_req *subreq)
431 struct tevent_req *req = tevent_req_callback_data(
432 subreq, struct tevent_req);
433 struct readlink_state *state = tevent_req_data(
434 req, struct readlink_state);
437 status = cli_qpathinfo_recv(subreq, state, &state->data,
440 if (tevent_req_nterror(req, status)) {
444 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446 if (state->data[state->num_data-1] != '\0') {
447 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
450 tevent_req_done(req);
453 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
454 char *retpath, size_t len)
457 char *converted = NULL;
458 size_t converted_size = 0;
459 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
461 if (tevent_req_is_nterror(req, &status)) {
464 /* The returned data is a pushed string, not raw data. */
465 if (!convert_string_talloc(state,
466 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
472 return NT_STATUS_NO_MEMORY;
475 len = MIN(len,converted_size);
477 return NT_STATUS_DATA_ERROR;
479 memcpy(retpath, converted, len);
483 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
484 char *linkpath, size_t len)
486 TALLOC_CTX *frame = talloc_stackframe();
487 struct tevent_context *ev = NULL;
488 struct tevent_req *req = NULL;
489 NTSTATUS status = NT_STATUS_OK;
491 if (smbXcli_conn_has_async_calls(cli->conn)) {
493 * Can't use sync call while an async call is in flight
495 status = NT_STATUS_INVALID_PARAMETER;
499 ev = samba_tevent_context_init(frame);
501 status = NT_STATUS_NO_MEMORY;
505 req = cli_posix_readlink_send(frame,
511 status = NT_STATUS_NO_MEMORY;
515 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
519 status = cli_posix_readlink_recv(req, cli, linkpath, len);
526 /****************************************************************************
527 Hard link a file (UNIX extensions).
528 ****************************************************************************/
530 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
531 struct tevent_context *ev,
532 struct cli_state *cli,
536 return cli_posix_link_internal_send(
537 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
540 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
542 return tevent_req_simple_recv_ntstatus(req);
545 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
549 TALLOC_CTX *frame = talloc_stackframe();
550 struct tevent_context *ev = NULL;
551 struct tevent_req *req = NULL;
552 NTSTATUS status = NT_STATUS_OK;
554 if (smbXcli_conn_has_async_calls(cli->conn)) {
556 * Can't use sync call while an async call is in flight
558 status = NT_STATUS_INVALID_PARAMETER;
562 ev = samba_tevent_context_init(frame);
564 status = NT_STATUS_NO_MEMORY;
568 req = cli_posix_hardlink_send(frame,
574 status = NT_STATUS_NO_MEMORY;
578 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
582 status = cli_posix_hardlink_recv(req);
589 /****************************************************************************
590 Do a POSIX getfacl (UNIX extensions).
591 ****************************************************************************/
593 struct getfacl_state {
598 static void cli_posix_getfacl_done(struct tevent_req *subreq);
600 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
601 struct tevent_context *ev,
602 struct cli_state *cli,
605 struct tevent_req *req = NULL, *subreq = NULL;
606 struct getfacl_state *state = NULL;
608 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
614 if (tevent_req_nomem(subreq, req)) {
615 return tevent_req_post(req, ev);
617 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621 static void cli_posix_getfacl_done(struct tevent_req *subreq)
623 struct tevent_req *req = tevent_req_callback_data(
624 subreq, struct tevent_req);
625 struct getfacl_state *state = tevent_req_data(
626 req, struct getfacl_state);
629 status = cli_qpathinfo_recv(subreq, state, &state->data,
632 if (tevent_req_nterror(req, status)) {
635 tevent_req_done(req);
638 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
643 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
646 if (tevent_req_is_nterror(req, &status)) {
649 *prb_size = (size_t)state->num_data;
650 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
660 TALLOC_CTX *frame = talloc_stackframe();
661 struct tevent_context *ev = NULL;
662 struct tevent_req *req = NULL;
663 NTSTATUS status = NT_STATUS_OK;
665 if (smbXcli_conn_has_async_calls(cli->conn)) {
667 * Can't use sync call while an async call is in flight
669 status = NT_STATUS_INVALID_PARAMETER;
673 ev = samba_tevent_context_init(frame);
675 status = NT_STATUS_NO_MEMORY;
679 req = cli_posix_getfacl_send(frame,
684 status = NT_STATUS_NO_MEMORY;
688 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
692 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
699 /****************************************************************************
700 Stat a file (UNIX extensions).
701 ****************************************************************************/
708 static void cli_posix_stat_done(struct tevent_req *subreq);
710 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
711 struct tevent_context *ev,
712 struct cli_state *cli,
715 struct tevent_req *req = NULL, *subreq = NULL;
716 struct stat_state *state = NULL;
718 req = tevent_req_create(mem_ctx, &state, struct stat_state);
722 subreq = cli_qpathinfo_send(state, ev, cli, fname,
723 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
724 if (tevent_req_nomem(subreq, req)) {
725 return tevent_req_post(req, ev);
727 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
731 static void cli_posix_stat_done(struct tevent_req *subreq)
733 struct tevent_req *req = tevent_req_callback_data(
734 subreq, struct tevent_req);
735 struct stat_state *state = tevent_req_data(req, struct stat_state);
738 status = cli_qpathinfo_recv(subreq, state, &state->data,
741 if (tevent_req_nterror(req, status)) {
744 tevent_req_done(req);
747 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
748 SMB_STRUCT_STAT *sbuf)
750 struct stat_state *state = tevent_req_data(req, struct stat_state);
753 if (tevent_req_is_nterror(req, &status)) {
757 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
758 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
759 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
760 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
762 /* assume 512 byte blocks */
763 sbuf->st_ex_blocks /= 512;
765 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
766 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
767 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
769 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
770 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
771 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
772 #if defined(HAVE_MAKEDEV)
774 uint32_t dev_major = IVAL(state->data,60);
775 uint32_t dev_minor = IVAL(state->data,68);
776 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
779 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
780 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
781 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
786 NTSTATUS cli_posix_stat(struct cli_state *cli,
788 SMB_STRUCT_STAT *sbuf)
790 TALLOC_CTX *frame = talloc_stackframe();
791 struct tevent_context *ev = NULL;
792 struct tevent_req *req = NULL;
793 NTSTATUS status = NT_STATUS_OK;
795 if (smbXcli_conn_has_async_calls(cli->conn)) {
797 * Can't use sync call while an async call is in flight
799 status = NT_STATUS_INVALID_PARAMETER;
803 ev = samba_tevent_context_init(frame);
805 status = NT_STATUS_NO_MEMORY;
809 req = cli_posix_stat_send(frame,
814 status = NT_STATUS_NO_MEMORY;
818 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
822 status = cli_posix_stat_recv(req, sbuf);
829 /****************************************************************************
830 Chmod or chown a file internal (UNIX extensions).
831 ****************************************************************************/
833 struct cli_posix_chown_chmod_internal_state {
837 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
839 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
840 struct tevent_context *ev,
841 struct cli_state *cli,
847 struct tevent_req *req = NULL, *subreq = NULL;
848 struct cli_posix_chown_chmod_internal_state *state = NULL;
850 req = tevent_req_create(mem_ctx, &state,
851 struct cli_posix_chown_chmod_internal_state);
856 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
857 memset(&state->data[40], '\0', 60);
858 SIVAL(state->data,40,uid);
859 SIVAL(state->data,48,gid);
860 SIVAL(state->data,84,mode);
862 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
863 fname, state->data, sizeof(state->data));
864 if (tevent_req_nomem(subreq, req)) {
865 return tevent_req_post(req, ev);
867 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
872 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
874 NTSTATUS status = cli_setpathinfo_recv(subreq);
875 tevent_req_simple_finish_ntstatus(subreq, status);
878 /****************************************************************************
879 chmod a file (UNIX extensions).
880 ****************************************************************************/
882 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
883 struct tevent_context *ev,
884 struct cli_state *cli,
888 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
890 unix_perms_to_wire(mode),
895 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
897 return tevent_req_simple_recv_ntstatus(req);
900 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
902 TALLOC_CTX *frame = talloc_stackframe();
903 struct tevent_context *ev = NULL;
904 struct tevent_req *req = NULL;
905 NTSTATUS status = NT_STATUS_OK;
907 if (smbXcli_conn_has_async_calls(cli->conn)) {
909 * Can't use sync call while an async call is in flight
911 status = NT_STATUS_INVALID_PARAMETER;
915 ev = samba_tevent_context_init(frame);
917 status = NT_STATUS_NO_MEMORY;
921 req = cli_posix_chmod_send(frame,
927 status = NT_STATUS_NO_MEMORY;
931 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
935 status = cli_posix_chmod_recv(req);
942 /****************************************************************************
943 chown a file (UNIX extensions).
944 ****************************************************************************/
946 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct cli_state *cli,
953 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
962 return tevent_req_simple_recv_ntstatus(req);
965 NTSTATUS cli_posix_chown(struct cli_state *cli,
970 TALLOC_CTX *frame = talloc_stackframe();
971 struct tevent_context *ev = NULL;
972 struct tevent_req *req = NULL;
973 NTSTATUS status = NT_STATUS_OK;
975 if (smbXcli_conn_has_async_calls(cli->conn)) {
977 * Can't use sync call while an async call is in flight
979 status = NT_STATUS_INVALID_PARAMETER;
983 ev = samba_tevent_context_init(frame);
985 status = NT_STATUS_NO_MEMORY;
989 req = cli_posix_chown_send(frame,
996 status = NT_STATUS_NO_MEMORY;
1000 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1004 status = cli_posix_chown_recv(req);
1011 /****************************************************************************
1013 ****************************************************************************/
1015 static void cli_rename_done(struct tevent_req *subreq);
1017 struct cli_rename_state {
1021 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1022 struct tevent_context *ev,
1023 struct cli_state *cli,
1024 const char *fname_src,
1025 const char *fname_dst)
1027 struct tevent_req *req = NULL, *subreq = NULL;
1028 struct cli_rename_state *state = NULL;
1029 uint8_t additional_flags = 0;
1030 uint8_t *bytes = NULL;
1032 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1037 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1039 bytes = talloc_array(state, uint8_t, 1);
1040 if (tevent_req_nomem(bytes, req)) {
1041 return tevent_req_post(req, ev);
1044 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1045 strlen(fname_src)+1, NULL);
1046 if (tevent_req_nomem(bytes, req)) {
1047 return tevent_req_post(req, ev);
1050 bytes = talloc_realloc(state, bytes, uint8_t,
1051 talloc_get_size(bytes)+1);
1052 if (tevent_req_nomem(bytes, req)) {
1053 return tevent_req_post(req, ev);
1056 bytes[talloc_get_size(bytes)-1] = 4;
1057 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1058 strlen(fname_dst)+1, NULL);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1064 1, state->vwv, talloc_get_size(bytes), bytes);
1065 if (tevent_req_nomem(subreq, req)) {
1066 return tevent_req_post(req, ev);
1068 tevent_req_set_callback(subreq, cli_rename_done, req);
1072 static void cli_rename_done(struct tevent_req *subreq)
1074 struct tevent_req *req = tevent_req_callback_data(
1075 subreq, struct tevent_req);
1078 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1079 TALLOC_FREE(subreq);
1080 if (tevent_req_nterror(req, status)) {
1083 tevent_req_done(req);
1086 NTSTATUS cli_rename_recv(struct tevent_req *req)
1088 return tevent_req_simple_recv_ntstatus(req);
1091 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1093 TALLOC_CTX *frame = NULL;
1094 struct tevent_context *ev;
1095 struct tevent_req *req;
1096 NTSTATUS status = NT_STATUS_OK;
1098 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1099 return cli_smb2_rename(cli,
1104 frame = talloc_stackframe();
1106 if (smbXcli_conn_has_async_calls(cli->conn)) {
1108 * Can't use sync call while an async call is in flight
1110 status = NT_STATUS_INVALID_PARAMETER;
1114 ev = samba_tevent_context_init(frame);
1116 status = NT_STATUS_NO_MEMORY;
1120 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1122 status = NT_STATUS_NO_MEMORY;
1126 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1130 status = cli_rename_recv(req);
1137 /****************************************************************************
1139 ****************************************************************************/
1141 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1143 struct cli_ntrename_internal_state {
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)
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;
1159 req = tevent_req_create(mem_ctx, &state,
1160 struct cli_ntrename_internal_state);
1165 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1166 SSVAL(state->vwv+1, 0, rename_flag);
1168 bytes = talloc_array(state, uint8_t, 1);
1169 if (tevent_req_nomem(bytes, req)) {
1170 return tevent_req_post(req, ev);
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);
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);
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);
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);
1197 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1201 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1203 struct tevent_req *req = tevent_req_callback_data(
1204 subreq, struct tevent_req);
1207 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1208 TALLOC_FREE(subreq);
1209 if (tevent_req_nterror(req, status)) {
1212 tevent_req_done(req);
1215 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1217 return tevent_req_simple_recv_ntstatus(req);
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)
1226 return cli_ntrename_internal_send(mem_ctx,
1231 RENAME_FLAG_RENAME);
1234 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1236 return cli_ntrename_internal_recv(req);
1239 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1241 TALLOC_CTX *frame = talloc_stackframe();
1242 struct tevent_context *ev;
1243 struct tevent_req *req;
1244 NTSTATUS status = NT_STATUS_OK;
1246 if (smbXcli_conn_has_async_calls(cli->conn)) {
1248 * Can't use sync call while an async call is in flight
1250 status = NT_STATUS_INVALID_PARAMETER;
1254 ev = samba_tevent_context_init(frame);
1256 status = NT_STATUS_NO_MEMORY;
1260 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1262 status = NT_STATUS_NO_MEMORY;
1266 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1270 status = cli_ntrename_recv(req);
1277 /****************************************************************************
1279 ****************************************************************************/
1281 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1282 struct tevent_context *ev,
1283 struct cli_state *cli,
1284 const char *fname_src,
1285 const char *fname_dst)
1287 return cli_ntrename_internal_send(mem_ctx,
1292 RENAME_FLAG_HARD_LINK);
1295 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1297 return cli_ntrename_internal_recv(req);
1300 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1302 TALLOC_CTX *frame = talloc_stackframe();
1303 struct tevent_context *ev;
1304 struct tevent_req *req;
1305 NTSTATUS status = NT_STATUS_OK;
1307 if (smbXcli_conn_has_async_calls(cli->conn)) {
1309 * Can't use sync call while an async call is in flight
1311 status = NT_STATUS_INVALID_PARAMETER;
1315 ev = samba_tevent_context_init(frame);
1317 status = NT_STATUS_NO_MEMORY;
1321 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1323 status = NT_STATUS_NO_MEMORY;
1327 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1331 status = cli_nt_hardlink_recv(req);
1338 /****************************************************************************
1340 ****************************************************************************/
1342 static void cli_unlink_done(struct tevent_req *subreq);
1344 struct cli_unlink_state {
1348 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1349 struct tevent_context *ev,
1350 struct cli_state *cli,
1352 uint16_t mayhave_attrs)
1354 struct tevent_req *req = NULL, *subreq = NULL;
1355 struct cli_unlink_state *state = NULL;
1356 uint8_t additional_flags = 0;
1357 uint8_t *bytes = NULL;
1359 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1364 SSVAL(state->vwv+0, 0, mayhave_attrs);
1366 bytes = talloc_array(state, uint8_t, 1);
1367 if (tevent_req_nomem(bytes, req)) {
1368 return tevent_req_post(req, ev);
1371 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1372 strlen(fname)+1, NULL);
1374 if (tevent_req_nomem(bytes, req)) {
1375 return tevent_req_post(req, ev);
1378 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1379 1, state->vwv, talloc_get_size(bytes), bytes);
1380 if (tevent_req_nomem(subreq, req)) {
1381 return tevent_req_post(req, ev);
1383 tevent_req_set_callback(subreq, cli_unlink_done, req);
1387 static void cli_unlink_done(struct tevent_req *subreq)
1389 struct tevent_req *req = tevent_req_callback_data(
1390 subreq, struct tevent_req);
1393 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1394 TALLOC_FREE(subreq);
1395 if (tevent_req_nterror(req, status)) {
1398 tevent_req_done(req);
1401 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1403 return tevent_req_simple_recv_ntstatus(req);
1406 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1408 TALLOC_CTX *frame = NULL;
1409 struct tevent_context *ev;
1410 struct tevent_req *req;
1411 NTSTATUS status = NT_STATUS_OK;
1413 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1414 return cli_smb2_unlink(cli, fname);
1417 frame = talloc_stackframe();
1419 if (smbXcli_conn_has_async_calls(cli->conn)) {
1421 * Can't use sync call while an async call is in flight
1423 status = NT_STATUS_INVALID_PARAMETER;
1427 ev = samba_tevent_context_init(frame);
1429 status = NT_STATUS_NO_MEMORY;
1433 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1435 status = NT_STATUS_NO_MEMORY;
1439 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1443 status = cli_unlink_recv(req);
1450 /****************************************************************************
1452 ****************************************************************************/
1454 static void cli_mkdir_done(struct tevent_req *subreq);
1456 struct cli_mkdir_state {
1460 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1461 struct tevent_context *ev,
1462 struct cli_state *cli,
1465 struct tevent_req *req = NULL, *subreq = NULL;
1466 struct cli_mkdir_state *state = NULL;
1467 uint8_t additional_flags = 0;
1468 uint8_t *bytes = NULL;
1470 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1475 bytes = talloc_array(state, uint8_t, 1);
1476 if (tevent_req_nomem(bytes, req)) {
1477 return tevent_req_post(req, ev);
1480 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1481 strlen(dname)+1, NULL);
1483 if (tevent_req_nomem(bytes, req)) {
1484 return tevent_req_post(req, ev);
1487 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1488 0, NULL, talloc_get_size(bytes), bytes);
1489 if (tevent_req_nomem(subreq, req)) {
1490 return tevent_req_post(req, ev);
1492 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1496 static void cli_mkdir_done(struct tevent_req *subreq)
1498 struct tevent_req *req = tevent_req_callback_data(
1499 subreq, struct tevent_req);
1502 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1503 TALLOC_FREE(subreq);
1504 if (tevent_req_nterror(req, status)) {
1507 tevent_req_done(req);
1510 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1512 return tevent_req_simple_recv_ntstatus(req);
1515 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1517 TALLOC_CTX *frame = NULL;
1518 struct tevent_context *ev;
1519 struct tevent_req *req;
1520 NTSTATUS status = NT_STATUS_OK;
1522 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1523 return cli_smb2_mkdir(cli, dname);
1526 frame = talloc_stackframe();
1528 if (smbXcli_conn_has_async_calls(cli->conn)) {
1530 * Can't use sync call while an async call is in flight
1532 status = NT_STATUS_INVALID_PARAMETER;
1536 ev = samba_tevent_context_init(frame);
1538 status = NT_STATUS_NO_MEMORY;
1542 req = cli_mkdir_send(frame, ev, cli, dname);
1544 status = NT_STATUS_NO_MEMORY;
1548 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1552 status = cli_mkdir_recv(req);
1559 /****************************************************************************
1561 ****************************************************************************/
1563 static void cli_rmdir_done(struct tevent_req *subreq);
1565 struct cli_rmdir_state {
1569 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1570 struct tevent_context *ev,
1571 struct cli_state *cli,
1574 struct tevent_req *req = NULL, *subreq = NULL;
1575 struct cli_rmdir_state *state = NULL;
1576 uint8_t additional_flags = 0;
1577 uint8_t *bytes = NULL;
1579 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1584 bytes = talloc_array(state, uint8_t, 1);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1589 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1590 strlen(dname)+1, NULL);
1592 if (tevent_req_nomem(bytes, req)) {
1593 return tevent_req_post(req, ev);
1596 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1597 0, NULL, talloc_get_size(bytes), bytes);
1598 if (tevent_req_nomem(subreq, req)) {
1599 return tevent_req_post(req, ev);
1601 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1605 static void cli_rmdir_done(struct tevent_req *subreq)
1607 struct tevent_req *req = tevent_req_callback_data(
1608 subreq, struct tevent_req);
1611 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1612 TALLOC_FREE(subreq);
1613 if (tevent_req_nterror(req, status)) {
1616 tevent_req_done(req);
1619 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1621 return tevent_req_simple_recv_ntstatus(req);
1624 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1626 TALLOC_CTX *frame = NULL;
1627 struct tevent_context *ev;
1628 struct tevent_req *req;
1629 NTSTATUS status = NT_STATUS_OK;
1631 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1632 return cli_smb2_rmdir(cli, dname);
1635 frame = talloc_stackframe();
1637 if (smbXcli_conn_has_async_calls(cli->conn)) {
1639 * Can't use sync call while an async call is in flight
1641 status = NT_STATUS_INVALID_PARAMETER;
1645 ev = samba_tevent_context_init(frame);
1647 status = NT_STATUS_NO_MEMORY;
1651 req = cli_rmdir_send(frame, ev, cli, dname);
1653 status = NT_STATUS_NO_MEMORY;
1657 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1661 status = cli_rmdir_recv(req);
1668 /****************************************************************************
1669 Set or clear the delete on close flag.
1670 ****************************************************************************/
1678 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1680 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1681 NULL, 0, NULL, NULL, 0, NULL);
1682 tevent_req_simple_finish_ntstatus(subreq, status);
1685 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1686 struct tevent_context *ev,
1687 struct cli_state *cli,
1691 struct tevent_req *req = NULL, *subreq = NULL;
1692 struct doc_state *state = NULL;
1694 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1699 /* Setup setup word. */
1700 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1702 /* Setup param array. */
1703 SSVAL(state->param,0,fnum);
1704 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1706 /* Setup data array. */
1707 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1709 subreq = cli_trans_send(state, /* mem ctx. */
1710 ev, /* event ctx. */
1711 cli, /* cli_state. */
1712 SMBtrans2, /* cmd. */
1713 NULL, /* pipe name. */
1717 &state->setup, /* setup. */
1718 1, /* num setup uint16_t words. */
1719 0, /* max returned setup. */
1720 state->param, /* param. */
1722 2, /* max returned param. */
1723 state->data, /* data. */
1725 0); /* max returned data. */
1727 if (tevent_req_nomem(subreq, req)) {
1728 return tevent_req_post(req, ev);
1730 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1734 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1736 return tevent_req_simple_recv_ntstatus(req);
1739 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1741 TALLOC_CTX *frame = talloc_stackframe();
1742 struct tevent_context *ev = NULL;
1743 struct tevent_req *req = NULL;
1744 NTSTATUS status = NT_STATUS_OK;
1746 if (smbXcli_conn_has_async_calls(cli->conn)) {
1748 * Can't use sync call while an async call is in flight
1750 status = NT_STATUS_INVALID_PARAMETER;
1754 ev = samba_tevent_context_init(frame);
1756 status = NT_STATUS_NO_MEMORY;
1760 req = cli_nt_delete_on_close_send(frame,
1766 status = NT_STATUS_NO_MEMORY;
1770 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1774 status = cli_nt_delete_on_close_recv(req);
1781 struct cli_ntcreate1_state {
1784 struct smb_create_returns cr;
1785 struct tevent_req *subreq;
1788 static void cli_ntcreate1_done(struct tevent_req *subreq);
1789 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1791 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1792 struct tevent_context *ev,
1793 struct cli_state *cli,
1795 uint32_t CreatFlags,
1796 uint32_t DesiredAccess,
1797 uint32_t FileAttributes,
1798 uint32_t ShareAccess,
1799 uint32_t CreateDisposition,
1800 uint32_t CreateOptions,
1801 uint8_t SecurityFlags)
1803 struct tevent_req *req, *subreq;
1804 struct cli_ntcreate1_state *state;
1807 size_t converted_len;
1809 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1816 SCVAL(vwv+0, 0, 0xFF);
1821 if (cli->use_oplocks) {
1822 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1824 SIVAL(vwv+3, 1, CreatFlags);
1825 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1826 SIVAL(vwv+7, 1, DesiredAccess);
1827 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1828 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1829 SIVAL(vwv+13, 1, FileAttributes);
1830 SIVAL(vwv+15, 1, ShareAccess);
1831 SIVAL(vwv+17, 1, CreateDisposition);
1832 SIVAL(vwv+19, 1, CreateOptions |
1833 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1834 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1835 SCVAL(vwv+23, 1, SecurityFlags);
1837 bytes = talloc_array(state, uint8_t, 0);
1838 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1839 fname, strlen(fname)+1,
1842 /* sigh. this copes with broken netapp filer behaviour */
1843 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1845 if (tevent_req_nomem(bytes, req)) {
1846 return tevent_req_post(req, ev);
1849 SSVAL(vwv+2, 1, converted_len);
1851 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1852 talloc_get_size(bytes), bytes);
1853 if (tevent_req_nomem(subreq, req)) {
1854 return tevent_req_post(req, ev);
1856 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1858 state->subreq = subreq;
1859 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
1864 static void cli_ntcreate1_done(struct tevent_req *subreq)
1866 struct tevent_req *req = tevent_req_callback_data(
1867 subreq, struct tevent_req);
1868 struct cli_ntcreate1_state *state = tevent_req_data(
1869 req, struct cli_ntcreate1_state);
1876 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1877 &num_bytes, &bytes);
1878 TALLOC_FREE(subreq);
1879 if (tevent_req_nterror(req, status)) {
1882 state->cr.oplock_level = CVAL(vwv+2, 0);
1883 state->fnum = SVAL(vwv+2, 1);
1884 state->cr.create_action = IVAL(vwv+3, 1);
1885 state->cr.creation_time = BVAL(vwv+5, 1);
1886 state->cr.last_access_time = BVAL(vwv+9, 1);
1887 state->cr.last_write_time = BVAL(vwv+13, 1);
1888 state->cr.change_time = BVAL(vwv+17, 1);
1889 state->cr.file_attributes = IVAL(vwv+21, 1);
1890 state->cr.allocation_size = BVAL(vwv+23, 1);
1891 state->cr.end_of_file = BVAL(vwv+27, 1);
1893 tevent_req_done(req);
1896 static bool cli_ntcreate1_cancel(struct tevent_req *req)
1898 struct cli_ntcreate1_state *state = tevent_req_data(
1899 req, struct cli_ntcreate1_state);
1900 return tevent_req_cancel(state->subreq);
1903 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
1905 struct smb_create_returns *cr)
1907 struct cli_ntcreate1_state *state = tevent_req_data(
1908 req, struct cli_ntcreate1_state);
1911 if (tevent_req_is_nterror(req, &status)) {
1914 *pfnum = state->fnum;
1918 return NT_STATUS_OK;
1921 struct cli_ntcreate_state {
1922 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
1923 struct smb_create_returns *cr);
1924 struct smb_create_returns cr;
1926 struct tevent_req *subreq;
1929 static void cli_ntcreate_done(struct tevent_req *subreq);
1930 static bool cli_ntcreate_cancel(struct tevent_req *req);
1932 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1933 struct tevent_context *ev,
1934 struct cli_state *cli,
1936 uint32_t create_flags,
1937 uint32_t desired_access,
1938 uint32_t file_attributes,
1939 uint32_t share_access,
1940 uint32_t create_disposition,
1941 uint32_t create_options,
1942 uint8_t security_flags)
1944 struct tevent_req *req, *subreq;
1945 struct cli_ntcreate_state *state;
1947 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1952 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1953 state->recv = cli_smb2_create_fnum_recv;
1955 if (cli->use_oplocks) {
1956 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
1959 subreq = cli_smb2_create_fnum_send(
1960 state, ev, cli, fname, create_flags, desired_access,
1961 file_attributes, share_access, create_disposition,
1964 state->recv = cli_ntcreate1_recv;
1965 subreq = cli_ntcreate1_send(
1966 state, ev, cli, fname, create_flags, desired_access,
1967 file_attributes, share_access, create_disposition,
1968 create_options, security_flags);
1970 if (tevent_req_nomem(subreq, req)) {
1971 return tevent_req_post(req, ev);
1973 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1975 state->subreq = subreq;
1976 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
1981 static void cli_ntcreate_done(struct tevent_req *subreq)
1983 struct tevent_req *req = tevent_req_callback_data(
1984 subreq, struct tevent_req);
1985 struct cli_ntcreate_state *state = tevent_req_data(
1986 req, struct cli_ntcreate_state);
1989 status = state->recv(subreq, &state->fnum, &state->cr);
1990 TALLOC_FREE(subreq);
1991 if (tevent_req_nterror(req, status)) {
1994 tevent_req_done(req);
1997 static bool cli_ntcreate_cancel(struct tevent_req *req)
1999 struct cli_ntcreate_state *state = tevent_req_data(
2000 req, struct cli_ntcreate_state);
2001 return tevent_req_cancel(state->subreq);
2004 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2005 struct smb_create_returns *cr)
2007 struct cli_ntcreate_state *state = tevent_req_data(
2008 req, struct cli_ntcreate_state);
2011 if (tevent_req_is_nterror(req, &status)) {
2015 *fnum = state->fnum;
2020 return NT_STATUS_OK;
2023 NTSTATUS cli_ntcreate(struct cli_state *cli,
2025 uint32_t CreatFlags,
2026 uint32_t DesiredAccess,
2027 uint32_t FileAttributes,
2028 uint32_t ShareAccess,
2029 uint32_t CreateDisposition,
2030 uint32_t CreateOptions,
2031 uint8_t SecurityFlags,
2033 struct smb_create_returns *cr)
2035 TALLOC_CTX *frame = talloc_stackframe();
2036 struct tevent_context *ev;
2037 struct tevent_req *req;
2038 NTSTATUS status = NT_STATUS_NO_MEMORY;
2040 if (smbXcli_conn_has_async_calls(cli->conn)) {
2042 * Can't use sync call while an async call is in flight
2044 status = NT_STATUS_INVALID_PARAMETER;
2048 ev = samba_tevent_context_init(frame);
2053 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2054 DesiredAccess, FileAttributes, ShareAccess,
2055 CreateDisposition, CreateOptions,
2061 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2065 status = cli_ntcreate_recv(req, pfid, cr);
2071 struct cli_nttrans_create_state {
2073 struct smb_create_returns cr;
2076 static void cli_nttrans_create_done(struct tevent_req *subreq);
2078 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2079 struct tevent_context *ev,
2080 struct cli_state *cli,
2082 uint32_t CreatFlags,
2083 uint32_t DesiredAccess,
2084 uint32_t FileAttributes,
2085 uint32_t ShareAccess,
2086 uint32_t CreateDisposition,
2087 uint32_t CreateOptions,
2088 uint8_t SecurityFlags,
2089 struct security_descriptor *secdesc,
2090 struct ea_struct *eas,
2093 struct tevent_req *req, *subreq;
2094 struct cli_nttrans_create_state *state;
2096 uint8_t *secdesc_buf;
2099 size_t converted_len;
2101 req = tevent_req_create(mem_ctx,
2102 &state, struct cli_nttrans_create_state);
2107 if (secdesc != NULL) {
2108 status = marshall_sec_desc(talloc_tos(), secdesc,
2109 &secdesc_buf, &secdesc_len);
2110 if (tevent_req_nterror(req, status)) {
2111 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2112 nt_errstr(status)));
2113 return tevent_req_post(req, ev);
2124 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2125 return tevent_req_post(req, ev);
2128 param = talloc_array(state, uint8_t, 53);
2129 if (tevent_req_nomem(param, req)) {
2130 return tevent_req_post(req, ev);
2133 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2134 fname, strlen(fname),
2136 if (tevent_req_nomem(param, req)) {
2137 return tevent_req_post(req, ev);
2140 SIVAL(param, 0, CreatFlags);
2141 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2142 SIVAL(param, 8, DesiredAccess);
2143 SIVAL(param, 12, 0x0); /* AllocationSize */
2144 SIVAL(param, 16, 0x0); /* AllocationSize */
2145 SIVAL(param, 20, FileAttributes);
2146 SIVAL(param, 24, ShareAccess);
2147 SIVAL(param, 28, CreateDisposition);
2148 SIVAL(param, 32, CreateOptions |
2149 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2150 SIVAL(param, 36, secdesc_len);
2151 SIVAL(param, 40, 0); /* EA length*/
2152 SIVAL(param, 44, converted_len);
2153 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2154 SCVAL(param, 52, SecurityFlags);
2156 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2157 NULL, -1, /* name, fid */
2158 NT_TRANSACT_CREATE, 0,
2159 NULL, 0, 0, /* setup */
2160 param, talloc_get_size(param), 128, /* param */
2161 secdesc_buf, secdesc_len, 0); /* data */
2162 if (tevent_req_nomem(subreq, req)) {
2163 return tevent_req_post(req, ev);
2165 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2169 static void cli_nttrans_create_done(struct tevent_req *subreq)
2171 struct tevent_req *req = tevent_req_callback_data(
2172 subreq, struct tevent_req);
2173 struct cli_nttrans_create_state *state = tevent_req_data(
2174 req, struct cli_nttrans_create_state);
2179 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2180 NULL, 0, NULL, /* rsetup */
2181 ¶m, 69, &num_param,
2183 if (tevent_req_nterror(req, status)) {
2186 state->cr.oplock_level = CVAL(param, 0);
2187 state->fnum = SVAL(param, 2);
2188 state->cr.create_action = IVAL(param, 4);
2189 state->cr.creation_time = BVAL(param, 12);
2190 state->cr.last_access_time = BVAL(param, 20);
2191 state->cr.last_write_time = BVAL(param, 28);
2192 state->cr.change_time = BVAL(param, 36);
2193 state->cr.file_attributes = IVAL(param, 44);
2194 state->cr.allocation_size = BVAL(param, 48);
2195 state->cr.end_of_file = BVAL(param, 56);
2198 tevent_req_done(req);
2201 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2203 struct smb_create_returns *cr)
2205 struct cli_nttrans_create_state *state = tevent_req_data(
2206 req, struct cli_nttrans_create_state);
2209 if (tevent_req_is_nterror(req, &status)) {
2212 *fnum = state->fnum;
2216 return NT_STATUS_OK;
2219 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2221 uint32_t CreatFlags,
2222 uint32_t DesiredAccess,
2223 uint32_t FileAttributes,
2224 uint32_t ShareAccess,
2225 uint32_t CreateDisposition,
2226 uint32_t CreateOptions,
2227 uint8_t SecurityFlags,
2228 struct security_descriptor *secdesc,
2229 struct ea_struct *eas,
2232 struct smb_create_returns *cr)
2234 TALLOC_CTX *frame = talloc_stackframe();
2235 struct tevent_context *ev;
2236 struct tevent_req *req;
2237 NTSTATUS status = NT_STATUS_NO_MEMORY;
2239 if (smbXcli_conn_has_async_calls(cli->conn)) {
2241 * Can't use sync call while an async call is in flight
2243 status = NT_STATUS_INVALID_PARAMETER;
2246 ev = samba_tevent_context_init(frame);
2250 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2251 DesiredAccess, FileAttributes,
2252 ShareAccess, CreateDisposition,
2253 CreateOptions, SecurityFlags,
2254 secdesc, eas, num_eas);
2258 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2261 status = cli_nttrans_create_recv(req, pfid, cr);
2267 /****************************************************************************
2269 WARNING: if you open with O_WRONLY then getattrE won't work!
2270 ****************************************************************************/
2272 struct cli_openx_state {
2279 static void cli_openx_done(struct tevent_req *subreq);
2281 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2282 struct tevent_context *ev,
2283 struct cli_state *cli, const char *fname,
2284 int flags, int share_mode,
2285 struct tevent_req **psmbreq)
2287 struct tevent_req *req, *subreq;
2288 struct cli_openx_state *state;
2290 unsigned accessmode;
2291 uint8_t additional_flags;
2294 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2300 if (flags & O_CREAT) {
2303 if (!(flags & O_EXCL)) {
2304 if (flags & O_TRUNC)
2310 accessmode = (share_mode<<4);
2312 if ((flags & O_ACCMODE) == O_RDWR) {
2314 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2319 if ((flags & O_SYNC) == O_SYNC) {
2320 accessmode |= (1<<14);
2324 if (share_mode == DENY_FCB) {
2328 SCVAL(state->vwv + 0, 0, 0xFF);
2329 SCVAL(state->vwv + 0, 1, 0);
2330 SSVAL(state->vwv + 1, 0, 0);
2331 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2332 SSVAL(state->vwv + 3, 0, accessmode);
2333 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2334 SSVAL(state->vwv + 5, 0, 0);
2335 SIVAL(state->vwv + 6, 0, 0);
2336 SSVAL(state->vwv + 8, 0, openfn);
2337 SIVAL(state->vwv + 9, 0, 0);
2338 SIVAL(state->vwv + 11, 0, 0);
2339 SIVAL(state->vwv + 13, 0, 0);
2341 additional_flags = 0;
2343 if (cli->use_oplocks) {
2344 /* if using oplocks then ask for a batch oplock via
2345 core and extended methods */
2347 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2348 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2351 bytes = talloc_array(state, uint8_t, 0);
2352 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2353 strlen(fname)+1, NULL);
2355 if (tevent_req_nomem(bytes, req)) {
2356 return tevent_req_post(req, ev);
2359 state->bytes.iov_base = (void *)bytes;
2360 state->bytes.iov_len = talloc_get_size(bytes);
2362 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2363 15, state->vwv, 1, &state->bytes);
2364 if (subreq == NULL) {
2368 tevent_req_set_callback(subreq, cli_openx_done, req);
2373 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2374 struct cli_state *cli, const char *fname,
2375 int flags, int share_mode)
2377 struct tevent_req *req, *subreq;
2380 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2386 status = smb1cli_req_chain_submit(&subreq, 1);
2387 if (tevent_req_nterror(req, status)) {
2388 return tevent_req_post(req, ev);
2393 static void cli_openx_done(struct tevent_req *subreq)
2395 struct tevent_req *req = tevent_req_callback_data(
2396 subreq, struct tevent_req);
2397 struct cli_openx_state *state = tevent_req_data(
2398 req, struct cli_openx_state);
2403 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2405 TALLOC_FREE(subreq);
2406 if (tevent_req_nterror(req, status)) {
2409 state->fnum = SVAL(vwv+2, 0);
2410 tevent_req_done(req);
2413 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2415 struct cli_openx_state *state = tevent_req_data(
2416 req, struct cli_openx_state);
2419 if (tevent_req_is_nterror(req, &status)) {
2422 *pfnum = state->fnum;
2423 return NT_STATUS_OK;
2426 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2427 int share_mode, uint16_t *pfnum)
2429 TALLOC_CTX *frame = talloc_stackframe();
2430 struct tevent_context *ev;
2431 struct tevent_req *req;
2432 NTSTATUS status = NT_STATUS_NO_MEMORY;
2434 if (smbXcli_conn_has_async_calls(cli->conn)) {
2436 * Can't use sync call while an async call is in flight
2438 status = NT_STATUS_INVALID_PARAMETER;
2442 ev = samba_tevent_context_init(frame);
2447 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2452 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2456 status = cli_openx_recv(req, pfnum);
2461 /****************************************************************************
2462 Synchronous wrapper function that does an NtCreateX open by preference
2463 and falls back to openX if this fails.
2464 ****************************************************************************/
2466 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2467 int share_mode_in, uint16_t *pfnum)
2470 unsigned int openfn = 0;
2471 unsigned int dos_deny = 0;
2472 uint32_t access_mask, share_mode, create_disposition, create_options;
2473 struct smb_create_returns cr;
2475 /* Do the initial mapping into OpenX parameters. */
2476 if (flags & O_CREAT) {
2479 if (!(flags & O_EXCL)) {
2480 if (flags & O_TRUNC)
2486 dos_deny = (share_mode_in<<4);
2488 if ((flags & O_ACCMODE) == O_RDWR) {
2490 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2495 if ((flags & O_SYNC) == O_SYNC) {
2496 dos_deny |= (1<<14);
2500 if (share_mode_in == DENY_FCB) {
2505 /* Hmmm. This is what I think the above code
2506 should look like if it's using the constants
2509 if (flags & O_CREAT) {
2510 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2512 if (!(flags & O_EXCL)) {
2513 if (flags & O_TRUNC)
2514 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2516 openfn |= OPENX_FILE_EXISTS_OPEN;
2519 dos_deny = SET_DENY_MODE(share_mode_in);
2521 if ((flags & O_ACCMODE) == O_RDWR) {
2522 dos_deny |= DOS_OPEN_RDWR;
2523 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2524 dos_deny |= DOS_OPEN_WRONLY;
2528 if ((flags & O_SYNC) == O_SYNC) {
2529 dos_deny |= FILE_SYNC_OPENMODE;
2533 if (share_mode_in == DENY_FCB) {
2538 if (!map_open_params_to_ntcreate(fname, dos_deny,
2539 openfn, &access_mask,
2540 &share_mode, &create_disposition,
2541 &create_options, NULL)) {
2545 status = cli_ntcreate(cli,
2557 /* Try and cope will all varients of "we don't do this call"
2558 and fall back to openX. */
2560 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2561 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2562 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2563 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2564 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2565 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2566 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2567 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2568 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2572 if (NT_STATUS_IS_OK(status) &&
2573 (create_options & FILE_NON_DIRECTORY_FILE) &&
2574 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2577 * Some (broken) servers return a valid handle
2578 * for directories even if FILE_NON_DIRECTORY_FILE
2579 * is set. Just close the handle and set the
2580 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2582 status = cli_close(cli, *pfnum);
2583 if (!NT_STATUS_IS_OK(status)) {
2586 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2587 /* Set this so libsmbclient can retrieve it. */
2588 cli->raw_status = status;
2595 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2598 /****************************************************************************
2600 ****************************************************************************/
2602 struct cli_close_state {
2606 static void cli_close_done(struct tevent_req *subreq);
2608 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2609 struct tevent_context *ev,
2610 struct cli_state *cli,
2612 struct tevent_req **psubreq)
2614 struct tevent_req *req, *subreq;
2615 struct cli_close_state *state;
2617 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2622 SSVAL(state->vwv+0, 0, fnum);
2623 SIVALS(state->vwv+1, 0, -1);
2625 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2627 if (subreq == NULL) {
2631 tevent_req_set_callback(subreq, cli_close_done, req);
2636 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2637 struct tevent_context *ev,
2638 struct cli_state *cli,
2641 struct tevent_req *req, *subreq;
2644 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2649 status = smb1cli_req_chain_submit(&subreq, 1);
2650 if (tevent_req_nterror(req, status)) {
2651 return tevent_req_post(req, ev);
2656 static void cli_close_done(struct tevent_req *subreq)
2658 struct tevent_req *req = tevent_req_callback_data(
2659 subreq, struct tevent_req);
2662 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2663 TALLOC_FREE(subreq);
2664 if (tevent_req_nterror(req, status)) {
2667 tevent_req_done(req);
2670 NTSTATUS cli_close_recv(struct tevent_req *req)
2672 return tevent_req_simple_recv_ntstatus(req);
2675 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2677 TALLOC_CTX *frame = NULL;
2678 struct tevent_context *ev;
2679 struct tevent_req *req;
2680 NTSTATUS status = NT_STATUS_OK;
2682 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2683 return cli_smb2_close_fnum(cli, fnum);
2686 frame = talloc_stackframe();
2688 if (smbXcli_conn_has_async_calls(cli->conn)) {
2690 * Can't use sync call while an async call is in flight
2692 status = NT_STATUS_INVALID_PARAMETER;
2696 ev = samba_tevent_context_init(frame);
2698 status = NT_STATUS_NO_MEMORY;
2702 req = cli_close_send(frame, ev, cli, fnum);
2704 status = NT_STATUS_NO_MEMORY;
2708 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2712 status = cli_close_recv(req);
2718 /****************************************************************************
2719 Truncate a file to a specified size
2720 ****************************************************************************/
2722 struct ftrunc_state {
2728 static void cli_ftruncate_done(struct tevent_req *subreq)
2730 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2731 NULL, 0, NULL, NULL, 0, NULL);
2732 tevent_req_simple_finish_ntstatus(subreq, status);
2735 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2736 struct tevent_context *ev,
2737 struct cli_state *cli,
2741 struct tevent_req *req = NULL, *subreq = NULL;
2742 struct ftrunc_state *state = NULL;
2744 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2749 /* Setup setup word. */
2750 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2752 /* Setup param array. */
2753 SSVAL(state->param,0,fnum);
2754 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2755 SSVAL(state->param,4,0);
2757 /* Setup data array. */
2758 SBVAL(state->data, 0, size);
2760 subreq = cli_trans_send(state, /* mem ctx. */
2761 ev, /* event ctx. */
2762 cli, /* cli_state. */
2763 SMBtrans2, /* cmd. */
2764 NULL, /* pipe name. */
2768 &state->setup, /* setup. */
2769 1, /* num setup uint16_t words. */
2770 0, /* max returned setup. */
2771 state->param, /* param. */
2773 2, /* max returned param. */
2774 state->data, /* data. */
2776 0); /* max returned data. */
2778 if (tevent_req_nomem(subreq, req)) {
2779 return tevent_req_post(req, ev);
2781 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2785 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2787 return tevent_req_simple_recv_ntstatus(req);
2790 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2792 TALLOC_CTX *frame = talloc_stackframe();
2793 struct tevent_context *ev = NULL;
2794 struct tevent_req *req = NULL;
2795 NTSTATUS status = NT_STATUS_OK;
2797 if (smbXcli_conn_has_async_calls(cli->conn)) {
2799 * Can't use sync call while an async call is in flight
2801 status = NT_STATUS_INVALID_PARAMETER;
2805 ev = samba_tevent_context_init(frame);
2807 status = NT_STATUS_NO_MEMORY;
2811 req = cli_ftruncate_send(frame,
2817 status = NT_STATUS_NO_MEMORY;
2821 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2825 status = cli_ftruncate_recv(req);
2832 /****************************************************************************
2833 send a lock with a specified locktype
2834 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2835 ****************************************************************************/
2837 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2838 uint32_t offset, uint32_t len,
2839 int timeout, unsigned char locktype)
2844 unsigned int set_timeout = 0;
2845 unsigned int saved_timeout = 0;
2847 SCVAL(vwv + 0, 0, 0xff);
2848 SCVAL(vwv + 0, 1, 0);
2849 SSVAL(vwv + 1, 0, 0);
2850 SSVAL(vwv + 2, 0, fnum);
2851 SCVAL(vwv + 3, 0, locktype);
2852 SCVAL(vwv + 3, 1, 0);
2853 SIVALS(vwv + 4, 0, timeout);
2854 SSVAL(vwv + 6, 0, 0);
2855 SSVAL(vwv + 7, 0, 1);
2857 SSVAL(bytes, 0, cli_getpid(cli));
2858 SIVAL(bytes, 2, offset);
2859 SIVAL(bytes, 6, len);
2862 if (timeout == -1) {
2863 set_timeout = 0x7FFFFFFF;
2865 set_timeout = timeout + 2*1000;
2867 saved_timeout = cli_set_timeout(cli, set_timeout);
2870 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2871 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2873 if (saved_timeout != 0) {
2874 cli_set_timeout(cli, saved_timeout);
2880 /****************************************************************************
2882 note that timeout is in units of 2 milliseconds
2883 ****************************************************************************/
2885 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2886 uint32_t offset, uint32_t len, int timeout,
2887 enum brl_type lock_type)
2891 status = cli_locktype(cli, fnum, offset, len, timeout,
2892 (lock_type == READ_LOCK? 1 : 0));
2896 /****************************************************************************
2898 ****************************************************************************/
2900 struct cli_unlock_state {
2905 static void cli_unlock_done(struct tevent_req *subreq);
2907 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2908 struct tevent_context *ev,
2909 struct cli_state *cli,
2915 struct tevent_req *req = NULL, *subreq = NULL;
2916 struct cli_unlock_state *state = NULL;
2917 uint8_t additional_flags = 0;
2919 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2924 SCVAL(state->vwv+0, 0, 0xFF);
2925 SSVAL(state->vwv+2, 0, fnum);
2926 SCVAL(state->vwv+3, 0, 0);
2927 SIVALS(state->vwv+4, 0, 0);
2928 SSVAL(state->vwv+6, 0, 1);
2929 SSVAL(state->vwv+7, 0, 0);
2931 SSVAL(state->data, 0, cli_getpid(cli));
2932 SIVAL(state->data, 2, offset);
2933 SIVAL(state->data, 6, len);
2935 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2936 8, state->vwv, 10, state->data);
2937 if (tevent_req_nomem(subreq, req)) {
2938 return tevent_req_post(req, ev);
2940 tevent_req_set_callback(subreq, cli_unlock_done, req);
2944 static void cli_unlock_done(struct tevent_req *subreq)
2946 struct tevent_req *req = tevent_req_callback_data(
2947 subreq, struct tevent_req);
2950 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2951 TALLOC_FREE(subreq);
2952 if (tevent_req_nterror(req, status)) {
2955 tevent_req_done(req);
2958 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2960 return tevent_req_simple_recv_ntstatus(req);
2963 NTSTATUS cli_unlock(struct cli_state *cli,
2968 TALLOC_CTX *frame = talloc_stackframe();
2969 struct tevent_context *ev;
2970 struct tevent_req *req;
2971 NTSTATUS status = NT_STATUS_OK;
2973 if (smbXcli_conn_has_async_calls(cli->conn)) {
2975 * Can't use sync call while an async call is in flight
2977 status = NT_STATUS_INVALID_PARAMETER;
2981 ev = samba_tevent_context_init(frame);
2983 status = NT_STATUS_NO_MEMORY;
2987 req = cli_unlock_send(frame, ev, cli,
2990 status = NT_STATUS_NO_MEMORY;
2994 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2998 status = cli_unlock_recv(req);
3005 /****************************************************************************
3006 Lock a file with 64 bit offsets.
3007 ****************************************************************************/
3009 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3010 uint64_t offset, uint64_t len, int timeout,
3011 enum brl_type lock_type)
3015 unsigned int set_timeout = 0;
3016 unsigned int saved_timeout = 0;
3020 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3021 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3024 ltype = (lock_type == READ_LOCK? 1 : 0);
3025 ltype |= LOCKING_ANDX_LARGE_FILES;
3027 SCVAL(vwv + 0, 0, 0xff);
3028 SCVAL(vwv + 0, 1, 0);
3029 SSVAL(vwv + 1, 0, 0);
3030 SSVAL(vwv + 2, 0, fnum);
3031 SCVAL(vwv + 3, 0, ltype);
3032 SCVAL(vwv + 3, 1, 0);
3033 SIVALS(vwv + 4, 0, timeout);
3034 SSVAL(vwv + 6, 0, 0);
3035 SSVAL(vwv + 7, 0, 1);
3037 SIVAL(bytes, 0, cli_getpid(cli));
3038 SOFF_T_R(bytes, 4, offset);
3039 SOFF_T_R(bytes, 12, len);
3042 if (timeout == -1) {
3043 set_timeout = 0x7FFFFFFF;
3045 set_timeout = timeout + 2*1000;
3047 saved_timeout = cli_set_timeout(cli, set_timeout);
3050 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3051 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3053 if (saved_timeout != 0) {
3054 cli_set_timeout(cli, saved_timeout);
3060 /****************************************************************************
3061 Unlock a file with 64 bit offsets.
3062 ****************************************************************************/
3064 struct cli_unlock64_state {
3069 static void cli_unlock64_done(struct tevent_req *subreq);
3071 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3072 struct tevent_context *ev,
3073 struct cli_state *cli,
3079 struct tevent_req *req = NULL, *subreq = NULL;
3080 struct cli_unlock64_state *state = NULL;
3081 uint8_t additional_flags = 0;
3083 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3088 SCVAL(state->vwv+0, 0, 0xff);
3089 SSVAL(state->vwv+2, 0, fnum);
3090 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3091 SIVALS(state->vwv+4, 0, 0);
3092 SSVAL(state->vwv+6, 0, 1);
3093 SSVAL(state->vwv+7, 0, 0);
3095 SIVAL(state->data, 0, cli_getpid(cli));
3096 SOFF_T_R(state->data, 4, offset);
3097 SOFF_T_R(state->data, 12, len);
3099 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
3100 8, state->vwv, 20, state->data);
3101 if (tevent_req_nomem(subreq, req)) {
3102 return tevent_req_post(req, ev);
3104 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3108 static void cli_unlock64_done(struct tevent_req *subreq)
3110 struct tevent_req *req = tevent_req_callback_data(
3111 subreq, struct tevent_req);
3114 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3115 TALLOC_FREE(subreq);
3116 if (tevent_req_nterror(req, status)) {
3119 tevent_req_done(req);
3122 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3124 return tevent_req_simple_recv_ntstatus(req);
3127 NTSTATUS cli_unlock64(struct cli_state *cli,
3132 TALLOC_CTX *frame = talloc_stackframe();
3133 struct tevent_context *ev;
3134 struct tevent_req *req;
3135 NTSTATUS status = NT_STATUS_OK;
3137 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3138 return cli_unlock(cli, fnum, offset, len);
3141 if (smbXcli_conn_has_async_calls(cli->conn)) {
3143 * Can't use sync call while an async call is in flight
3145 status = NT_STATUS_INVALID_PARAMETER;
3149 ev = samba_tevent_context_init(frame);
3151 status = NT_STATUS_NO_MEMORY;
3155 req = cli_unlock64_send(frame, ev, cli,
3158 status = NT_STATUS_NO_MEMORY;
3162 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3166 status = cli_unlock64_recv(req);
3173 /****************************************************************************
3174 Get/unlock a POSIX lock on a file - internal function.
3175 ****************************************************************************/
3177 struct posix_lock_state {
3180 uint8_t data[POSIX_LOCK_DATA_SIZE];
3183 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3185 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3186 NULL, 0, NULL, NULL, 0, NULL);
3187 tevent_req_simple_finish_ntstatus(subreq, status);
3190 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3191 struct tevent_context *ev,
3192 struct cli_state *cli,
3197 enum brl_type lock_type)
3199 struct tevent_req *req = NULL, *subreq = NULL;
3200 struct posix_lock_state *state = NULL;
3202 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3207 /* Setup setup word. */
3208 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3210 /* Setup param array. */
3211 SSVAL(&state->param, 0, fnum);
3212 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3214 /* Setup data array. */
3215 switch (lock_type) {
3217 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3218 POSIX_LOCK_TYPE_READ);
3221 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3222 POSIX_LOCK_TYPE_WRITE);
3225 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3226 POSIX_LOCK_TYPE_UNLOCK);
3233 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3234 POSIX_LOCK_FLAG_WAIT);
3236 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3237 POSIX_LOCK_FLAG_NOWAIT);
3240 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3241 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3242 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3244 subreq = cli_trans_send(state, /* mem ctx. */
3245 ev, /* event ctx. */
3246 cli, /* cli_state. */
3247 SMBtrans2, /* cmd. */
3248 NULL, /* pipe name. */
3252 &state->setup, /* setup. */
3253 1, /* num setup uint16_t words. */
3254 0, /* max returned setup. */
3255 state->param, /* param. */
3257 2, /* max returned param. */
3258 state->data, /* data. */
3259 POSIX_LOCK_DATA_SIZE, /* num data. */
3260 0); /* max returned data. */
3262 if (tevent_req_nomem(subreq, req)) {
3263 return tevent_req_post(req, ev);
3265 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3269 /****************************************************************************
3271 ****************************************************************************/
3273 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3274 struct tevent_context *ev,
3275 struct cli_state *cli,
3280 enum brl_type lock_type)
3282 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3283 wait_lock, lock_type);
3286 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3288 return tevent_req_simple_recv_ntstatus(req);
3291 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3292 uint64_t offset, uint64_t len,
3293 bool wait_lock, enum brl_type lock_type)
3295 TALLOC_CTX *frame = talloc_stackframe();
3296 struct tevent_context *ev = NULL;
3297 struct tevent_req *req = NULL;
3298 NTSTATUS status = NT_STATUS_OK;
3300 if (smbXcli_conn_has_async_calls(cli->conn)) {
3302 * Can't use sync call while an async call is in flight
3304 status = NT_STATUS_INVALID_PARAMETER;
3308 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3309 status = NT_STATUS_INVALID_PARAMETER;
3313 ev = samba_tevent_context_init(frame);
3315 status = NT_STATUS_NO_MEMORY;
3319 req = cli_posix_lock_send(frame,
3328 status = NT_STATUS_NO_MEMORY;
3332 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3336 status = cli_posix_lock_recv(req);
3343 /****************************************************************************
3344 POSIX Unlock a file.
3345 ****************************************************************************/
3347 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3348 struct tevent_context *ev,
3349 struct cli_state *cli,
3354 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3355 false, UNLOCK_LOCK);
3358 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3360 return tevent_req_simple_recv_ntstatus(req);
3363 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3365 TALLOC_CTX *frame = talloc_stackframe();
3366 struct tevent_context *ev = NULL;
3367 struct tevent_req *req = NULL;
3368 NTSTATUS status = NT_STATUS_OK;
3370 if (smbXcli_conn_has_async_calls(cli->conn)) {
3372 * Can't use sync call while an async call is in flight
3374 status = NT_STATUS_INVALID_PARAMETER;
3378 ev = samba_tevent_context_init(frame);
3380 status = NT_STATUS_NO_MEMORY;
3384 req = cli_posix_unlock_send(frame,
3391 status = NT_STATUS_NO_MEMORY;
3395 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3399 status = cli_posix_unlock_recv(req);
3406 /****************************************************************************
3407 Do a SMBgetattrE call.
3408 ****************************************************************************/
3410 static void cli_getattrE_done(struct tevent_req *subreq);
3412 struct cli_getattrE_state {
3422 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3423 struct tevent_context *ev,
3424 struct cli_state *cli,
3427 struct tevent_req *req = NULL, *subreq = NULL;
3428 struct cli_getattrE_state *state = NULL;
3429 uint8_t additional_flags = 0;
3431 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3436 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3437 SSVAL(state->vwv+0,0,fnum);
3439 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3440 1, state->vwv, 0, NULL);
3441 if (tevent_req_nomem(subreq, req)) {
3442 return tevent_req_post(req, ev);
3444 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3448 static void cli_getattrE_done(struct tevent_req *subreq)
3450 struct tevent_req *req = tevent_req_callback_data(
3451 subreq, struct tevent_req);
3452 struct cli_getattrE_state *state = tevent_req_data(
3453 req, struct cli_getattrE_state);
3455 uint16_t *vwv = NULL;
3458 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3460 TALLOC_FREE(subreq);
3461 if (tevent_req_nterror(req, status)) {
3465 state->size = (off_t)IVAL(vwv+6,0);
3466 state->attr = SVAL(vwv+10,0);
3467 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3468 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3469 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3471 tevent_req_done(req);
3474 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3477 time_t *change_time,
3478 time_t *access_time,
3481 struct cli_getattrE_state *state = tevent_req_data(
3482 req, struct cli_getattrE_state);
3485 if (tevent_req_is_nterror(req, &status)) {
3489 *attr = state->attr;
3492 *size = state->size;
3495 *change_time = state->change_time;
3498 *access_time = state->access_time;
3501 *write_time = state->write_time;
3503 return NT_STATUS_OK;
3506 NTSTATUS cli_getattrE(struct cli_state *cli,
3510 time_t *change_time,
3511 time_t *access_time,
3514 TALLOC_CTX *frame = NULL;
3515 struct tevent_context *ev = NULL;
3516 struct tevent_req *req = NULL;
3517 NTSTATUS status = NT_STATUS_OK;
3519 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3520 return cli_smb2_getattrE(cli,
3529 frame = talloc_stackframe();
3531 if (smbXcli_conn_has_async_calls(cli->conn)) {
3533 * Can't use sync call while an async call is in flight
3535 status = NT_STATUS_INVALID_PARAMETER;
3539 ev = samba_tevent_context_init(frame);
3541 status = NT_STATUS_NO_MEMORY;
3545 req = cli_getattrE_send(frame, ev, cli, fnum);
3547 status = NT_STATUS_NO_MEMORY;
3551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3555 status = cli_getattrE_recv(req,
3567 /****************************************************************************
3569 ****************************************************************************/
3571 static void cli_getatr_done(struct tevent_req *subreq);
3573 struct cli_getatr_state {
3580 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3581 struct tevent_context *ev,
3582 struct cli_state *cli,
3585 struct tevent_req *req = NULL, *subreq = NULL;
3586 struct cli_getatr_state *state = NULL;
3587 uint8_t additional_flags = 0;
3588 uint8_t *bytes = NULL;
3590 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3595 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3597 bytes = talloc_array(state, uint8_t, 1);
3598 if (tevent_req_nomem(bytes, req)) {
3599 return tevent_req_post(req, ev);
3602 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3603 strlen(fname)+1, NULL);
3605 if (tevent_req_nomem(bytes, req)) {
3606 return tevent_req_post(req, ev);
3609 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3610 0, NULL, talloc_get_size(bytes), bytes);
3611 if (tevent_req_nomem(subreq, req)) {
3612 return tevent_req_post(req, ev);
3614 tevent_req_set_callback(subreq, cli_getatr_done, req);
3618 static void cli_getatr_done(struct tevent_req *subreq)
3620 struct tevent_req *req = tevent_req_callback_data(
3621 subreq, struct tevent_req);
3622 struct cli_getatr_state *state = tevent_req_data(
3623 req, struct cli_getatr_state);
3625 uint16_t *vwv = NULL;
3628 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3630 TALLOC_FREE(subreq);
3631 if (tevent_req_nterror(req, status)) {
3635 state->attr = SVAL(vwv+0,0);
3636 state->size = (off_t)IVAL(vwv+3,0);
3637 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3639 tevent_req_done(req);
3642 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3647 struct cli_getatr_state *state = tevent_req_data(
3648 req, struct cli_getatr_state);
3651 if (tevent_req_is_nterror(req, &status)) {
3655 *attr = state->attr;
3658 *size = state->size;
3661 *write_time = state->write_time;
3663 return NT_STATUS_OK;
3666 NTSTATUS cli_getatr(struct cli_state *cli,
3672 TALLOC_CTX *frame = NULL;
3673 struct tevent_context *ev = NULL;
3674 struct tevent_req *req = NULL;
3675 NTSTATUS status = NT_STATUS_OK;
3677 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3678 return cli_smb2_getatr(cli,
3685 frame = talloc_stackframe();
3687 if (smbXcli_conn_has_async_calls(cli->conn)) {
3689 * Can't use sync call while an async call is in flight
3691 status = NT_STATUS_INVALID_PARAMETER;
3695 ev = samba_tevent_context_init(frame);
3697 status = NT_STATUS_NO_MEMORY;
3701 req = cli_getatr_send(frame, ev, cli, fname);
3703 status = NT_STATUS_NO_MEMORY;
3707 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3711 status = cli_getatr_recv(req,
3721 /****************************************************************************
3722 Do a SMBsetattrE call.
3723 ****************************************************************************/
3725 static void cli_setattrE_done(struct tevent_req *subreq);
3727 struct cli_setattrE_state {
3731 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3732 struct tevent_context *ev,
3733 struct cli_state *cli,
3739 struct tevent_req *req = NULL, *subreq = NULL;
3740 struct cli_setattrE_state *state = NULL;
3741 uint8_t additional_flags = 0;
3743 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3748 SSVAL(state->vwv+0, 0, fnum);
3749 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3750 smb1cli_conn_server_time_zone(cli->conn));
3751 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3752 smb1cli_conn_server_time_zone(cli->conn));
3753 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3754 smb1cli_conn_server_time_zone(cli->conn));
3756 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3757 7, state->vwv, 0, NULL);
3758 if (tevent_req_nomem(subreq, req)) {
3759 return tevent_req_post(req, ev);
3761 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3765 static void cli_setattrE_done(struct tevent_req *subreq)
3767 struct tevent_req *req = tevent_req_callback_data(
3768 subreq, struct tevent_req);
3771 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3772 TALLOC_FREE(subreq);
3773 if (tevent_req_nterror(req, status)) {
3776 tevent_req_done(req);
3779 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3781 return tevent_req_simple_recv_ntstatus(req);
3784 NTSTATUS cli_setattrE(struct cli_state *cli,
3790 TALLOC_CTX *frame = NULL;
3791 struct tevent_context *ev = NULL;
3792 struct tevent_req *req = NULL;
3793 NTSTATUS status = NT_STATUS_OK;
3795 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3796 return cli_smb2_setattrE(cli,
3803 frame = talloc_stackframe();
3805 if (smbXcli_conn_has_async_calls(cli->conn)) {
3807 * Can't use sync call while an async call is in flight
3809 status = NT_STATUS_INVALID_PARAMETER;
3813 ev = samba_tevent_context_init(frame);
3815 status = NT_STATUS_NO_MEMORY;
3819 req = cli_setattrE_send(frame, ev,
3827 status = NT_STATUS_NO_MEMORY;
3831 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3835 status = cli_setattrE_recv(req);
3842 /****************************************************************************
3843 Do a SMBsetatr call.
3844 ****************************************************************************/
3846 static void cli_setatr_done(struct tevent_req *subreq);
3848 struct cli_setatr_state {
3852 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3853 struct tevent_context *ev,
3854 struct cli_state *cli,
3859 struct tevent_req *req = NULL, *subreq = NULL;
3860 struct cli_setatr_state *state = NULL;
3861 uint8_t additional_flags = 0;
3862 uint8_t *bytes = NULL;
3864 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3869 SSVAL(state->vwv+0, 0, attr);
3870 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3872 bytes = talloc_array(state, uint8_t, 1);
3873 if (tevent_req_nomem(bytes, req)) {
3874 return tevent_req_post(req, ev);
3877 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3878 strlen(fname)+1, NULL);
3879 if (tevent_req_nomem(bytes, req)) {
3880 return tevent_req_post(req, ev);
3882 bytes = talloc_realloc(state, bytes, uint8_t,
3883 talloc_get_size(bytes)+1);
3884 if (tevent_req_nomem(bytes, req)) {
3885 return tevent_req_post(req, ev);
3888 bytes[talloc_get_size(bytes)-1] = 4;
3889 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3891 if (tevent_req_nomem(bytes, req)) {
3892 return tevent_req_post(req, ev);
3895 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3896 8, state->vwv, talloc_get_size(bytes), bytes);
3897 if (tevent_req_nomem(subreq, req)) {
3898 return tevent_req_post(req, ev);
3900 tevent_req_set_callback(subreq, cli_setatr_done, req);
3904 static void cli_setatr_done(struct tevent_req *subreq)
3906 struct tevent_req *req = tevent_req_callback_data(
3907 subreq, struct tevent_req);
3910 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3911 TALLOC_FREE(subreq);
3912 if (tevent_req_nterror(req, status)) {
3915 tevent_req_done(req);
3918 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3920 return tevent_req_simple_recv_ntstatus(req);
3923 NTSTATUS cli_setatr(struct cli_state *cli,
3928 TALLOC_CTX *frame = NULL;
3929 struct tevent_context *ev = NULL;
3930 struct tevent_req *req = NULL;
3931 NTSTATUS status = NT_STATUS_OK;
3933 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3934 return cli_smb2_setatr(cli,
3940 frame = talloc_stackframe();
3942 if (smbXcli_conn_has_async_calls(cli->conn)) {
3944 * Can't use sync call while an async call is in flight
3946 status = NT_STATUS_INVALID_PARAMETER;
3950 ev = samba_tevent_context_init(frame);
3952 status = NT_STATUS_NO_MEMORY;
3956 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3958 status = NT_STATUS_NO_MEMORY;
3962 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3966 status = cli_setatr_recv(req);
3973 /****************************************************************************
3974 Check for existance of a dir.
3975 ****************************************************************************/
3977 static void cli_chkpath_done(struct tevent_req *subreq);
3979 struct cli_chkpath_state {
3983 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3984 struct tevent_context *ev,
3985 struct cli_state *cli,
3988 struct tevent_req *req = NULL, *subreq = NULL;
3989 struct cli_chkpath_state *state = NULL;
3990 uint8_t additional_flags = 0;
3991 uint8_t *bytes = NULL;
3993 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3998 bytes = talloc_array(state, uint8_t, 1);
3999 if (tevent_req_nomem(bytes, req)) {
4000 return tevent_req_post(req, ev);
4003 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4004 strlen(fname)+1, NULL);
4006 if (tevent_req_nomem(bytes, req)) {
4007 return tevent_req_post(req, ev);
4010 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4011 0, NULL, talloc_get_size(bytes), bytes);
4012 if (tevent_req_nomem(subreq, req)) {
4013 return tevent_req_post(req, ev);
4015 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4019 static void cli_chkpath_done(struct tevent_req *subreq)
4021 struct tevent_req *req = tevent_req_callback_data(
4022 subreq, struct tevent_req);
4025 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4026 TALLOC_FREE(subreq);
4027 if (tevent_req_nterror(req, status)) {
4030 tevent_req_done(req);
4033 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4035 return tevent_req_simple_recv_ntstatus(req);
4038 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4040 TALLOC_CTX *frame = talloc_stackframe();
4041 struct tevent_context *ev = NULL;
4042 struct tevent_req *req = NULL;
4044 NTSTATUS status = NT_STATUS_OK;
4046 if (smbXcli_conn_has_async_calls(cli->conn)) {
4048 * Can't use sync call while an async call is in flight
4050 status = NT_STATUS_INVALID_PARAMETER;
4054 path2 = talloc_strdup(frame, path);
4056 status = NT_STATUS_NO_MEMORY;
4059 trim_char(path2,'\0','\\');
4061 path2 = talloc_strdup(frame, "\\");
4063 status = NT_STATUS_NO_MEMORY;
4068 ev = samba_tevent_context_init(frame);
4070 status = NT_STATUS_NO_MEMORY;
4074 req = cli_chkpath_send(frame, ev, cli, path2);
4076 status = NT_STATUS_NO_MEMORY;
4080 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4084 status = cli_chkpath_recv(req);
4091 /****************************************************************************
4093 ****************************************************************************/
4095 static void cli_dskattr_done(struct tevent_req *subreq);
4097 struct cli_dskattr_state {
4103 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4104 struct tevent_context *ev,
4105 struct cli_state *cli)
4107 struct tevent_req *req = NULL, *subreq = NULL;
4108 struct cli_dskattr_state *state = NULL;
4109 uint8_t additional_flags = 0;
4111 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4116 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
4118 if (tevent_req_nomem(subreq, req)) {
4119 return tevent_req_post(req, ev);
4121 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4125 static void cli_dskattr_done(struct tevent_req *subreq)
4127 struct tevent_req *req = tevent_req_callback_data(
4128 subreq, struct tevent_req);
4129 struct cli_dskattr_state *state = tevent_req_data(
4130 req, struct cli_dskattr_state);
4132 uint16_t *vwv = NULL;
4135 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4137 TALLOC_FREE(subreq);
4138 if (tevent_req_nterror(req, status)) {
4141 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4142 state->total = SVAL(vwv+0, 0);
4143 state->avail = SVAL(vwv+3, 0);
4144 tevent_req_done(req);
4147 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4149 struct cli_dskattr_state *state = tevent_req_data(
4150 req, struct cli_dskattr_state);
4153 if (tevent_req_is_nterror(req, &status)) {
4156 *bsize = state->bsize;
4157 *total = state->total;
4158 *avail = state->avail;
4159 return NT_STATUS_OK;
4162 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4164 TALLOC_CTX *frame = NULL;
4165 struct tevent_context *ev = NULL;
4166 struct tevent_req *req = NULL;
4167 NTSTATUS status = NT_STATUS_OK;
4169 frame = talloc_stackframe();
4171 if (smbXcli_conn_has_async_calls(cli->conn)) {
4173 * Can't use sync call while an async call is in flight
4175 status = NT_STATUS_INVALID_PARAMETER;
4179 ev = samba_tevent_context_init(frame);
4181 status = NT_STATUS_NO_MEMORY;
4185 req = cli_dskattr_send(frame, ev, cli);
4187 status = NT_STATUS_NO_MEMORY;
4191 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4195 status = cli_dskattr_recv(req, bsize, total, avail);
4202 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4203 uint64_t *total, uint64_t *avail)
4205 uint64_t sectors_per_block;
4206 uint64_t bytes_per_sector;
4207 int old_bsize, old_total, old_avail;
4210 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4211 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4215 * Try the trans2 disk full size info call first.
4216 * We already use this in SMBC_fstatvfs_ctx().
4217 * Ignore 'actual_available_units' as we only
4218 * care about the quota for the caller.
4221 status = cli_get_fs_full_size_info(cli,
4228 /* Try and cope will all varients of "we don't do this call"
4229 and fall back to cli_dskattr. */
4231 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4232 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4233 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4234 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4235 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4236 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4237 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4238 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4239 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4240 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4244 if (!NT_STATUS_IS_OK(status)) {
4249 *bsize = sectors_per_block *
4253 return NT_STATUS_OK;
4257 /* Old SMB1 core protocol fallback. */
4258 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4259 if (!NT_STATUS_IS_OK(status)) {
4263 *bsize = (uint64_t)old_bsize;
4266 *total = (uint64_t)old_total;
4269 *avail = (uint64_t)old_avail;
4271 return NT_STATUS_OK;
4274 /****************************************************************************
4275 Create and open a temporary file.
4276 ****************************************************************************/
4278 static void cli_ctemp_done(struct tevent_req *subreq);
4280 struct ctemp_state {
4286 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4287 struct tevent_context *ev,
4288 struct cli_state *cli,
4291 struct tevent_req *req = NULL, *subreq = NULL;
4292 struct ctemp_state *state = NULL;
4293 uint8_t additional_flags = 0;
4294 uint8_t *bytes = NULL;
4296 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4301 SSVAL(state->vwv,0,0);
4302 SIVALS(state->vwv+1,0,-1);
4304 bytes = talloc_array(state, uint8_t, 1);
4305 if (tevent_req_nomem(bytes, req)) {
4306 return tevent_req_post(req, ev);
4309 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4310 strlen(path)+1, NULL);
4311 if (tevent_req_nomem(bytes, req)) {
4312 return tevent_req_post(req, ev);
4315 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4316 3, state->vwv, talloc_get_size(bytes), bytes);
4317 if (tevent_req_nomem(subreq, req)) {
4318 return tevent_req_post(req, ev);
4320 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4324 static void cli_ctemp_done(struct tevent_req *subreq)
4326 struct tevent_req *req = tevent_req_callback_data(
4327 subreq, struct tevent_req);
4328 struct ctemp_state *state = tevent_req_data(
4329 req, struct ctemp_state);
4333 uint32_t num_bytes = 0;
4334 uint8_t *bytes = NULL;
4336 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4337 &num_bytes, &bytes);
4338 TALLOC_FREE(subreq);
4339 if (tevent_req_nterror(req, status)) {
4343 state->fnum = SVAL(vwv+0, 0);
4345 /* From W2K3, the result is just the ASCII name */
4346 if (num_bytes < 2) {
4347 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4351 if (pull_string_talloc(state,
4358 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4361 tevent_req_done(req);
4364 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4369 struct ctemp_state *state = tevent_req_data(req,
4370 struct ctemp_state);
4373 if (tevent_req_is_nterror(req, &status)) {
4376 *pfnum = state->fnum;
4377 *outfile = talloc_strdup(ctx, state->ret_path);
4379 return NT_STATUS_NO_MEMORY;
4381 return NT_STATUS_OK;
4384 NTSTATUS cli_ctemp(struct cli_state *cli,
4390 TALLOC_CTX *frame = talloc_stackframe();
4391 struct tevent_context *ev;
4392 struct tevent_req *req;
4393 NTSTATUS status = NT_STATUS_OK;
4395 if (smbXcli_conn_has_async_calls(cli->conn)) {
4397 * Can't use sync call while an async call is in flight
4399 status = NT_STATUS_INVALID_PARAMETER;
4403 ev = samba_tevent_context_init(frame);
4405 status = NT_STATUS_NO_MEMORY;
4409 req = cli_ctemp_send(frame, ev, cli, path);
4411 status = NT_STATUS_NO_MEMORY;
4415 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4419 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4427 send a raw ioctl - used by the torture code
4429 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4434 SSVAL(vwv+0, 0, fnum);
4435 SSVAL(vwv+1, 0, code>>16);
4436 SSVAL(vwv+2, 0, (code&0xFFFF));
4438 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4439 NULL, 0, NULL, NULL, NULL, NULL);
4440 if (!NT_STATUS_IS_OK(status)) {
4443 *blob = data_blob_null;
4444 return NT_STATUS_OK;
4447 /*********************************************************
4448 Set an extended attribute utility fn.
4449 *********************************************************/
4451 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4452 uint8_t *param, unsigned int param_len,
4453 const char *ea_name,
4454 const char *ea_val, size_t ea_len)
4457 unsigned int data_len = 0;
4458 uint8_t *data = NULL;
4460 size_t ea_namelen = strlen(ea_name);
4463 SSVAL(setup, 0, setup_val);
4465 if (ea_namelen == 0 && ea_len == 0) {
4467 data = talloc_array(talloc_tos(),
4471 return NT_STATUS_NO_MEMORY;
4474 SIVAL(p,0,data_len);
4476 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4477 data = talloc_array(talloc_tos(),
4481 return NT_STATUS_NO_MEMORY;
4484 SIVAL(p,0,data_len);
4486 SCVAL(p, 0, 0); /* EA flags. */
4487 SCVAL(p, 1, ea_namelen);
4488 SSVAL(p, 2, ea_len);
4489 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4490 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4493 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4495 param, param_len, 2,
4496 data, data_len, CLI_BUFFER_SIZE,
4498 NULL, 0, NULL, /* rsetup */
4499 NULL, 0, NULL, /* rparam */
4500 NULL, 0, NULL); /* rdata */
4505 /*********************************************************
4506 Set an extended attribute on a pathname.
4507 *********************************************************/
4509 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4510 const char *ea_name, const char *ea_val,
4513 unsigned int param_len = 0;
4516 TALLOC_CTX *frame = NULL;
4518 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4519 return cli_smb2_set_ea_path(cli,
4526 frame = talloc_stackframe();
4528 param = talloc_array(frame, uint8_t, 6);
4530 status = NT_STATUS_NO_MEMORY;
4533 SSVAL(param,0,SMB_INFO_SET_EA);
4537 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4538 path, strlen(path)+1,
4540 param_len = talloc_get_size(param);
4542 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4543 ea_name, ea_val, ea_len);
4551 /*********************************************************
4552 Set an extended attribute on an fnum.
4553 *********************************************************/
4555 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4556 const char *ea_name, const char *ea_val,
4561 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4562 return cli_smb2_set_ea_fnum(cli,
4569 memset(param, 0, 6);
4570 SSVAL(param,0,fnum);
4571 SSVAL(param,2,SMB_INFO_SET_EA);
4573 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4574 ea_name, ea_val, ea_len);
4577 /*********************************************************
4578 Get an extended attribute list utility fn.
4579 *********************************************************/
4581 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4583 size_t *pnum_eas, struct ea_struct **pea_list)
4585 struct ea_struct *ea_list = NULL;
4590 if (rdata_len < 4) {
4594 ea_size = (size_t)IVAL(rdata,0);
4595 if (ea_size > rdata_len) {
4600 /* No EA's present. */
4609 /* Validate the EA list and count it. */
4610 for (num_eas = 0; ea_size >= 4; num_eas++) {
4611 unsigned int ea_namelen = CVAL(p,1);
4612 unsigned int ea_valuelen = SVAL(p,2);
4613 if (ea_namelen == 0) {
4616 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4619 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4620 p += 4 + ea_namelen + 1 + ea_valuelen;
4629 *pnum_eas = num_eas;
4631 /* Caller only wants number of EA's. */
4635 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4640 ea_size = (size_t)IVAL(rdata,0);
4643 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4644 struct ea_struct *ea = &ea_list[num_eas];
4645 fstring unix_ea_name;
4646 unsigned int ea_namelen = CVAL(p,1);
4647 unsigned int ea_valuelen = SVAL(p,2);
4649 ea->flags = CVAL(p,0);
4650 unix_ea_name[0] = '\0';
4651 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4652 ea->name = talloc_strdup(ea_list, unix_ea_name);
4656 /* Ensure the value is null terminated (in case it's a string). */
4657 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4658 if (!ea->value.data) {
4662 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4664 ea->value.data[ea_valuelen] = 0;
4666 p += 4 + ea_namelen + 1 + ea_valuelen;
4669 *pea_list = ea_list;
4673 TALLOC_FREE(ea_list);
4677 /*********************************************************
4678 Get an extended attribute list from a pathname.
4679 *********************************************************/
4681 struct cli_get_ea_list_path_state {
4686 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4688 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4689 struct tevent_context *ev,
4690 struct cli_state *cli,
4693 struct tevent_req *req, *subreq;
4694 struct cli_get_ea_list_path_state *state;
4696 req = tevent_req_create(mem_ctx, &state,
4697 struct cli_get_ea_list_path_state);
4701 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4702 SMB_INFO_QUERY_ALL_EAS, 4,
4704 if (tevent_req_nomem(subreq, req)) {
4705 return tevent_req_post(req, ev);
4707 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4711 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4713 struct tevent_req *req = tevent_req_callback_data(
4714 subreq, struct tevent_req);
4715 struct cli_get_ea_list_path_state *state = tevent_req_data(
4716 req, struct cli_get_ea_list_path_state);
4719 status = cli_qpathinfo_recv(subreq, state, &state->data,
4721 TALLOC_FREE(subreq);
4722 if (tevent_req_nterror(req, status)) {
4725 tevent_req_done(req);
4728 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4729 size_t *pnum_eas, struct ea_struct **peas)
4731 struct cli_get_ea_list_path_state *state = tevent_req_data(
4732 req, struct cli_get_ea_list_path_state);
4735 if (tevent_req_is_nterror(req, &status)) {
4738 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4740 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4742 return NT_STATUS_OK;
4745 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4748 struct ea_struct **pea_list)
4750 TALLOC_CTX *frame = NULL;
4751 struct tevent_context *ev = NULL;
4752 struct tevent_req *req = NULL;
4753 NTSTATUS status = NT_STATUS_NO_MEMORY;
4755 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4756 return cli_smb2_get_ea_list_path(cli,
4763 frame = talloc_stackframe();
4765 if (smbXcli_conn_has_async_calls(cli->conn)) {
4767 * Can't use sync call while an async call is in flight
4769 status = NT_STATUS_INVALID_PARAMETER;
4772 ev = samba_tevent_context_init(frame);
4776 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4780 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4783 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4789 /****************************************************************************
4790 Convert open "flags" arg to uint32_t on wire.
4791 ****************************************************************************/
4793 static uint32_t open_flags_to_wire(int flags)
4795 int open_mode = flags & O_ACCMODE;
4798 switch (open_mode) {
4800 ret |= SMB_O_WRONLY;
4807 ret |= SMB_O_RDONLY;
4811 if (flags & O_CREAT) {
4814 if (flags & O_EXCL) {
4817 if (flags & O_TRUNC) {
4821 if (flags & O_SYNC) {
4825 if (flags & O_APPEND) {
4826 ret |= SMB_O_APPEND;
4828 #if defined(O_DIRECT)
4829 if (flags & O_DIRECT) {
4830 ret |= SMB_O_DIRECT;
4833 #if defined(O_DIRECTORY)
4834 if (flags & O_DIRECTORY) {
4835 ret |= SMB_O_DIRECTORY;
4841 /****************************************************************************
4842 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4843 ****************************************************************************/
4845 struct posix_open_state {
4849 uint16_t fnum; /* Out */
4852 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4854 struct tevent_req *req = tevent_req_callback_data(
4855 subreq, struct tevent_req);
4856 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4861 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4862 NULL, 0, NULL, &data, 12, &num_data);
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4867 state->fnum = SVAL(data,2);
4868 tevent_req_done(req);
4871 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4872 struct tevent_context *ev,
4873 struct cli_state *cli,
4879 struct tevent_req *req = NULL, *subreq = NULL;
4880 struct posix_open_state *state = NULL;
4881 uint32_t wire_flags = open_flags_to_wire(flags);
4883 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4888 /* Setup setup word. */
4889 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4891 /* Setup param array. */
4892 state->param = talloc_array(state, uint8_t, 6);
4893 if (tevent_req_nomem(state->param, req)) {
4894 return tevent_req_post(req, ev);
4896 memset(state->param, '\0', 6);
4897 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4899 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4900 strlen(fname)+1, NULL);
4902 if (tevent_req_nomem(state->param, req)) {
4903 return tevent_req_post(req, ev);
4906 /* Setup data words. */
4908 wire_flags |= SMB_O_DIRECTORY;
4911 SIVAL(state->data,0,0); /* No oplock. */
4912 SIVAL(state->data,4,wire_flags);
4913 SIVAL(state->data,8,unix_perms_to_wire(mode));
4914 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4915 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4917 subreq = cli_trans_send(state, /* mem ctx. */
4918 ev, /* event ctx. */
4919 cli, /* cli_state. */
4920 SMBtrans2, /* cmd. */
4921 NULL, /* pipe name. */
4925 &state->setup, /* setup. */
4926 1, /* num setup uint16_t words. */
4927 0, /* max returned setup. */
4928 state->param, /* param. */
4929 talloc_get_size(state->param),/* num param. */
4930 2, /* max returned param. */
4931 state->data, /* data. */
4933 12); /* max returned data. */
4935 if (tevent_req_nomem(subreq, req)) {
4936 return tevent_req_post(req, ev);
4938 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4942 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4943 struct tevent_context *ev,
4944 struct cli_state *cli,
4949 return cli_posix_open_internal_send(mem_ctx, ev,
4950 cli, fname, flags, mode, false);
4953 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4955 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4958 if (tevent_req_is_nterror(req, &status)) {
4961 *pfnum = state->fnum;
4962 return NT_STATUS_OK;
4965 /****************************************************************************
4966 Open - POSIX semantics. Doesn't request oplock.
4967 ****************************************************************************/
4969 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4970 int flags, mode_t mode, uint16_t *pfnum)
4973 TALLOC_CTX *frame = talloc_stackframe();
4974 struct tevent_context *ev = NULL;
4975 struct tevent_req *req = NULL;
4976 NTSTATUS status = NT_STATUS_OK;
4978 if (smbXcli_conn_has_async_calls(cli->conn)) {
4980 * Can't use sync call while an async call is in flight
4982 status = NT_STATUS_INVALID_PARAMETER;
4986 ev = samba_tevent_context_init(frame);
4988 status = NT_STATUS_NO_MEMORY;
4992 req = cli_posix_open_send(frame,
4999 status = NT_STATUS_NO_MEMORY;
5003 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5007 status = cli_posix_open_recv(req, pfnum);
5014 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5015 struct tevent_context *ev,
5016 struct cli_state *cli,
5020 return cli_posix_open_internal_send(mem_ctx, ev,
5021 cli, fname, O_CREAT, mode, true);
5024 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5026 return tevent_req_simple_recv_ntstatus(req);
5029 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5031 TALLOC_CTX *frame = talloc_stackframe();
5032 struct tevent_context *ev = NULL;
5033 struct tevent_req *req = NULL;
5034 NTSTATUS status = NT_STATUS_OK;
5036 if (smbXcli_conn_has_async_calls(cli->conn)) {
5038 * Can't use sync call while an async call is in flight
5040 status = NT_STATUS_INVALID_PARAMETER;
5044 ev = samba_tevent_context_init(frame);
5046 status = NT_STATUS_NO_MEMORY;
5050 req = cli_posix_mkdir_send(frame,
5056 status = NT_STATUS_NO_MEMORY;
5060 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5064 status = cli_posix_mkdir_recv(req);
5071 /****************************************************************************
5072 unlink or rmdir - POSIX semantics.
5073 ****************************************************************************/
5075 struct cli_posix_unlink_internal_state {
5079 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5081 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5082 struct tevent_context *ev,
5083 struct cli_state *cli,
5087 struct tevent_req *req = NULL, *subreq = NULL;
5088 struct cli_posix_unlink_internal_state *state = NULL;
5090 req = tevent_req_create(mem_ctx, &state,
5091 struct cli_posix_unlink_internal_state);
5096 /* Setup data word. */
5097 SSVAL(state->data, 0, level);
5099 subreq = cli_setpathinfo_send(state, ev, cli,
5100 SMB_POSIX_PATH_UNLINK,
5102 state->data, sizeof(state->data));
5103 if (tevent_req_nomem(subreq, req)) {
5104 return tevent_req_post(req, ev);
5106 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5110 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5112 NTSTATUS status = cli_setpathinfo_recv(subreq);
5113 tevent_req_simple_finish_ntstatus(subreq, status);
5116 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5117 struct tevent_context *ev,
5118 struct cli_state *cli,
5121 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5122 SMB_POSIX_UNLINK_FILE_TARGET);
5125 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5127 return tevent_req_simple_recv_ntstatus(req);
5130 /****************************************************************************
5131 unlink - POSIX semantics.
5132 ****************************************************************************/
5134 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5136 TALLOC_CTX *frame = talloc_stackframe();
5137 struct tevent_context *ev = NULL;
5138 struct tevent_req *req = NULL;
5139 NTSTATUS status = NT_STATUS_OK;
5141 if (smbXcli_conn_has_async_calls(cli->conn)) {
5143 * Can't use sync call while an async call is in flight
5145 status = NT_STATUS_INVALID_PARAMETER;
5149 ev = samba_tevent_context_init(frame);
5151 status = NT_STATUS_NO_MEMORY;
5155 req = cli_posix_unlink_send(frame,
5160 status = NT_STATUS_NO_MEMORY;
5164 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5168 status = cli_posix_unlink_recv(req);
5175 /****************************************************************************
5176 rmdir - POSIX semantics.
5177 ****************************************************************************/
5179 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5180 struct tevent_context *ev,
5181 struct cli_state *cli,
5184 return cli_posix_unlink_internal_send(
5185 mem_ctx, ev, cli, fname,
5186 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5189 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5191 return tevent_req_simple_recv_ntstatus(req);
5194 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5196 TALLOC_CTX *frame = talloc_stackframe();
5197 struct tevent_context *ev = NULL;
5198 struct tevent_req *req = NULL;
5199 NTSTATUS status = NT_STATUS_OK;
5201 if (smbXcli_conn_has_async_calls(cli->conn)) {
5203 * Can't use sync call while an async call is in flight
5205 status = NT_STATUS_INVALID_PARAMETER;
5209 ev = samba_tevent_context_init(frame);
5211 status = NT_STATUS_NO_MEMORY;
5215 req = cli_posix_rmdir_send(frame,
5220 status = NT_STATUS_NO_MEMORY;
5224 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5228 status = cli_posix_rmdir_recv(req, frame);
5235 /****************************************************************************
5237 ****************************************************************************/
5239 struct cli_notify_state {
5241 uint32_t num_changes;
5242 struct notify_change *changes;
5245 static void cli_notify_done(struct tevent_req *subreq);
5247 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5248 struct tevent_context *ev,
5249 struct cli_state *cli, uint16_t fnum,
5250 uint32_t buffer_size,
5251 uint32_t completion_filter, bool recursive)
5253 struct tevent_req *req, *subreq;
5254 struct cli_notify_state *state;
5255 unsigned old_timeout;
5257 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5262 SIVAL(state->setup, 0, completion_filter);
5263 SSVAL(state->setup, 4, fnum);
5264 SSVAL(state->setup, 6, recursive);
5267 * Notifies should not time out
5269 old_timeout = cli_set_timeout(cli, 0);
5271 subreq = cli_trans_send(
5272 state, /* mem ctx. */
5273 ev, /* event ctx. */
5274 cli, /* cli_state. */
5275 SMBnttrans, /* cmd. */
5276 NULL, /* pipe name. */
5278 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5280 (uint16_t *)state->setup, /* setup. */
5281 4, /* num setup uint16_t words. */
5282 0, /* max returned setup. */
5285 buffer_size, /* max returned param. */
5288 0); /* max returned data. */
5290 cli_set_timeout(cli, old_timeout);
5292 if (tevent_req_nomem(subreq, req)) {
5293 return tevent_req_post(req, ev);
5295 tevent_req_set_callback(subreq, cli_notify_done, req);
5299 static void cli_notify_done(struct tevent_req *subreq)
5301 struct tevent_req *req = tevent_req_callback_data(
5302 subreq, struct tevent_req);
5303 struct cli_notify_state *state = tevent_req_data(
5304 req, struct cli_notify_state);
5307 uint32_t i, ofs, num_params;
5310 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5311 ¶ms, 0, &num_params, NULL, 0, NULL);
5312 TALLOC_FREE(subreq);
5313 if (tevent_req_nterror(req, status)) {
5314 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5318 state->num_changes = 0;
5321 while (num_params - ofs > 12) {
5322 uint32_t next = IVAL(params, ofs);
5323 state->num_changes += 1;
5325 if ((next == 0) || (ofs+next >= num_params)) {
5331 state->changes = talloc_array(state, struct notify_change,
5332 state->num_changes);
5333 if (tevent_req_nomem(state->changes, req)) {
5334 TALLOC_FREE(params);
5340 for (i=0; i<state->num_changes; i++) {
5341 uint32_t next = IVAL(params, ofs);
5342 uint32_t len = IVAL(params, ofs+8);
5346 if (trans_oob(num_params, ofs + 12, len)) {
5347 TALLOC_FREE(params);
5349 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5353 state->changes[i].action = IVAL(params, ofs+4);
5354 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5355 &name, params+ofs+12, len,
5356 STR_TERMINATE|STR_UNICODE);
5358 TALLOC_FREE(params);
5359 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5362 state->changes[i].name = name;
5366 TALLOC_FREE(params);
5367 tevent_req_done(req);
5370 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5371 uint32_t *pnum_changes,
5372 struct notify_change **pchanges)
5374 struct cli_notify_state *state = tevent_req_data(
5375 req, struct cli_notify_state);
5378 if (tevent_req_is_nterror(req, &status)) {
5382 *pnum_changes = state->num_changes;
5383 *pchanges = talloc_move(mem_ctx, &state->changes);
5384 return NT_STATUS_OK;
5387 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5388 uint32_t completion_filter, bool recursive,
5389 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5390 struct notify_change **pchanges)
5392 TALLOC_CTX *frame = talloc_stackframe();
5393 struct tevent_context *ev;
5394 struct tevent_req *req;
5395 NTSTATUS status = NT_STATUS_NO_MEMORY;
5397 if (smbXcli_conn_has_async_calls(cli->conn)) {
5399 * Can't use sync call while an async call is in flight
5401 status = NT_STATUS_INVALID_PARAMETER;
5404 ev = samba_tevent_context_init(frame);
5408 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5409 completion_filter, recursive);
5413 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5416 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5422 struct cli_qpathinfo_state {
5431 static void cli_qpathinfo_done(struct tevent_req *subreq);
5433 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5434 struct tevent_context *ev,
5435 struct cli_state *cli, const char *fname,
5436 uint16_t level, uint32_t min_rdata,
5439 struct tevent_req *req, *subreq;
5440 struct cli_qpathinfo_state *state;
5442 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5446 state->min_rdata = min_rdata;
5447 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5449 state->param = talloc_zero_array(state, uint8_t, 6);
5450 if (tevent_req_nomem(state->param, req)) {
5451 return tevent_req_post(req, ev);
5453 SSVAL(state->param, 0, level);
5454 state->param = trans2_bytes_push_str(
5455 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5456 if (tevent_req_nomem(state->param, req)) {
5457 return tevent_req_post(req, ev);
5460 subreq = cli_trans_send(
5461 state, /* mem ctx. */
5462 ev, /* event ctx. */
5463 cli, /* cli_state. */
5464 SMBtrans2, /* cmd. */
5465 NULL, /* pipe name. */
5469 state->setup, /* setup. */
5470 1, /* num setup uint16_t words. */
5471 0, /* max returned setup. */
5472 state->param, /* param. */
5473 talloc_get_size(state->param), /* num param. */
5474 2, /* max returned param. */
5477 max_rdata); /* max returned data. */
5479 if (tevent_req_nomem(subreq, req)) {
5480 return tevent_req_post(req, ev);
5482 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5486 static void cli_qpathinfo_done(struct tevent_req *subreq)
5488 struct tevent_req *req = tevent_req_callback_data(
5489 subreq, struct tevent_req);
5490 struct cli_qpathinfo_state *state = tevent_req_data(
5491 req, struct cli_qpathinfo_state);
5494 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5496 &state->rdata, state->min_rdata,
5498 if (tevent_req_nterror(req, status)) {
5501 tevent_req_done(req);
5504 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5505 uint8_t **rdata, uint32_t *num_rdata)
5507 struct cli_qpathinfo_state *state = tevent_req_data(
5508 req, struct cli_qpathinfo_state);
5511 if (tevent_req_is_nterror(req, &status)) {
5514 if (rdata != NULL) {
5515 *rdata = talloc_move(mem_ctx, &state->rdata);
5517 TALLOC_FREE(state->rdata);
5519 if (num_rdata != NULL) {
5520 *num_rdata = state->num_rdata;
5522 return NT_STATUS_OK;
5525 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5526 const char *fname, uint16_t level, uint32_t min_rdata,
5528 uint8_t **rdata, uint32_t *num_rdata)
5530 TALLOC_CTX *frame = talloc_stackframe();
5531 struct tevent_context *ev;
5532 struct tevent_req *req;
5533 NTSTATUS status = NT_STATUS_NO_MEMORY;
5535 if (smbXcli_conn_has_async_calls(cli->conn)) {
5537 * Can't use sync call while an async call is in flight
5539 status = NT_STATUS_INVALID_PARAMETER;
5542 ev = samba_tevent_context_init(frame);
5546 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5554 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5560 struct cli_qfileinfo_state {
5564 uint16_t recv_flags2;
5570 static void cli_qfileinfo_done(struct tevent_req *subreq);
5572 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5573 struct tevent_context *ev,
5574 struct cli_state *cli, uint16_t fnum,
5575 uint16_t level, uint32_t min_rdata,
5578 struct tevent_req *req, *subreq;
5579 struct cli_qfileinfo_state *state;
5581 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5585 state->min_rdata = min_rdata;
5586 SSVAL(state->param, 0, fnum);
5587 SSVAL(state->param, 2, level);
5588 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5590 subreq = cli_trans_send(
5591 state, /* mem ctx. */
5592 ev, /* event ctx. */
5593 cli, /* cli_state. */
5594 SMBtrans2, /* cmd. */
5595 NULL, /* pipe name. */
5599 state->setup, /* setup. */
5600 1, /* num setup uint16_t words. */
5601 0, /* max returned setup. */
5602 state->param, /* param. */
5603 sizeof(state->param), /* num param. */
5604 2, /* max returned param. */
5607 max_rdata); /* max returned data. */
5609 if (tevent_req_nomem(subreq, req)) {
5610 return tevent_req_post(req, ev);
5612 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5616 static void cli_qfileinfo_done(struct tevent_req *subreq)
5618 struct tevent_req *req = tevent_req_callback_data(
5619 subreq, struct tevent_req);
5620 struct cli_qfileinfo_state *state = tevent_req_data(
5621 req, struct cli_qfileinfo_state);
5624 status = cli_trans_recv(subreq, state,
5625 &state->recv_flags2,
5628 &state->rdata, state->min_rdata,
5630 if (tevent_req_nterror(req, status)) {
5633 tevent_req_done(req);
5636 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5637 uint16_t *recv_flags2,
5638 uint8_t **rdata, uint32_t *num_rdata)
5640 struct cli_qfileinfo_state *state = tevent_req_data(
5641 req, struct cli_qfileinfo_state);
5644 if (tevent_req_is_nterror(req, &status)) {
5648 if (recv_flags2 != NULL) {
5649 *recv_flags2 = state->recv_flags2;
5651 if (rdata != NULL) {
5652 *rdata = talloc_move(mem_ctx, &state->rdata);
5654 TALLOC_FREE(state->rdata);
5656 if (num_rdata != NULL) {
5657 *num_rdata = state->num_rdata;
5659 return NT_STATUS_OK;
5662 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5663 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5664 uint32_t max_rdata, uint16_t *recv_flags2,
5665 uint8_t **rdata, uint32_t *num_rdata)
5667 TALLOC_CTX *frame = talloc_stackframe();
5668 struct tevent_context *ev;
5669 struct tevent_req *req;
5670 NTSTATUS status = NT_STATUS_NO_MEMORY;
5672 if (smbXcli_conn_has_async_calls(cli->conn)) {
5674 * Can't use sync call while an async call is in flight
5676 status = NT_STATUS_INVALID_PARAMETER;
5679 ev = samba_tevent_context_init(frame);
5683 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5688 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5691 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5697 struct cli_flush_state {
5701 static void cli_flush_done(struct tevent_req *subreq);
5703 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5704 struct tevent_context *ev,
5705 struct cli_state *cli,
5708 struct tevent_req *req, *subreq;
5709 struct cli_flush_state *state;
5711 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5715 SSVAL(state->vwv + 0, 0, fnum);
5717 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5719 if (tevent_req_nomem(subreq, req)) {
5720 return tevent_req_post(req, ev);
5722 tevent_req_set_callback(subreq, cli_flush_done, req);
5726 static void cli_flush_done(struct tevent_req *subreq)
5728 struct tevent_req *req = tevent_req_callback_data(
5729 subreq, struct tevent_req);
5732 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5733 TALLOC_FREE(subreq);
5734 if (tevent_req_nterror(req, status)) {
5737 tevent_req_done(req);
5740 NTSTATUS cli_flush_recv(struct tevent_req *req)
5742 return tevent_req_simple_recv_ntstatus(req);
5745 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5747 TALLOC_CTX *frame = talloc_stackframe();
5748 struct tevent_context *ev;
5749 struct tevent_req *req;
5750 NTSTATUS status = NT_STATUS_NO_MEMORY;
5752 if (smbXcli_conn_has_async_calls(cli->conn)) {
5754 * Can't use sync call while an async call is in flight
5756 status = NT_STATUS_INVALID_PARAMETER;
5759 ev = samba_tevent_context_init(frame);
5763 req = cli_flush_send(frame, ev, cli, fnum);
5767 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5770 status = cli_flush_recv(req);
5776 struct cli_shadow_copy_data_state {
5783 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5785 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5786 struct tevent_context *ev,
5787 struct cli_state *cli,
5791 struct tevent_req *req, *subreq;
5792 struct cli_shadow_copy_data_state *state;
5795 req = tevent_req_create(mem_ctx, &state,
5796 struct cli_shadow_copy_data_state);
5800 state->get_names = get_names;
5801 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5803 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5804 SSVAL(state->setup + 2, 0, fnum);
5805 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5806 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5808 subreq = cli_trans_send(
5809 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5810 state->setup, ARRAY_SIZE(state->setup), 0,
5813 if (tevent_req_nomem(subreq, req)) {
5814 return tevent_req_post(req, ev);
5816 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5820 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5822 struct tevent_req *req = tevent_req_callback_data(
5823 subreq, struct tevent_req);
5824 struct cli_shadow_copy_data_state *state = tevent_req_data(
5825 req, struct cli_shadow_copy_data_state);
5828 status = cli_trans_recv(subreq, state, NULL,
5829 NULL, 0, NULL, /* setup */
5830 NULL, 0, NULL, /* param */
5831 &state->data, 12, &state->num_data);
5832 TALLOC_FREE(subreq);
5833 if (tevent_req_nterror(req, status)) {
5836 tevent_req_done(req);
5839 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5840 char ***pnames, int *pnum_names)
5842 struct cli_shadow_copy_data_state *state = tevent_req_data(
5843 req, struct cli_shadow_copy_data_state);
5849 if (tevent_req_is_nterror(req, &status)) {
5852 num_names = IVAL(state->data, 4);
5853 dlength = IVAL(state->data, 8);
5855 if (!state->get_names) {
5856 *pnum_names = num_names;
5857 return NT_STATUS_OK;
5860 if (dlength+12 > state->num_data) {
5861 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5863 names = talloc_array(mem_ctx, char *, num_names);
5864 if (names == NULL) {
5865 return NT_STATUS_NO_MEMORY;
5868 for (i=0; i<num_names; i++) {
5871 size_t converted_size;
5873 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5874 ret = convert_string_talloc(
5875 names, CH_UTF16LE, CH_UNIX,
5876 src, 2 * sizeof(SHADOW_COPY_LABEL),
5877 &names[i], &converted_size);
5880 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5883 *pnum_names = num_names;
5885 return NT_STATUS_OK;
5888 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5889 uint16_t fnum, bool get_names,
5890 char ***pnames, int *pnum_names)
5892 TALLOC_CTX *frame = talloc_stackframe();
5893 struct tevent_context *ev;
5894 struct tevent_req *req;
5895 NTSTATUS status = NT_STATUS_NO_MEMORY;
5897 if (smbXcli_conn_has_async_calls(cli->conn)) {
5899 * Can't use sync call while an async call is in flight
5901 status = NT_STATUS_INVALID_PARAMETER;
5904 ev = samba_tevent_context_init(frame);
5908 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5912 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5915 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);