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 struct cli_setpathinfo_state {
37 static void cli_setpathinfo_done(struct tevent_req *subreq);
39 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
40 struct tevent_context *ev,
41 struct cli_state *cli,
47 struct tevent_req *req, *subreq;
48 struct cli_setpathinfo_state *state;
49 uint16_t additional_flags2 = 0;
51 req = tevent_req_create(mem_ctx, &state,
52 struct cli_setpathinfo_state);
57 /* Setup setup word. */
58 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
60 /* Setup param array. */
61 state->param = talloc_zero_array(state, uint8_t, 6);
62 if (tevent_req_nomem(state->param, req)) {
63 return tevent_req_post(req, ev);
65 SSVAL(state->param, 0, level);
67 state->param = trans2_bytes_push_str(
68 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
69 if (tevent_req_nomem(state->param, req)) {
70 return tevent_req_post(req, ev);
73 if (clistr_is_previous_version_path(path, NULL, NULL, NULL) &&
74 !INFO_LEVEL_IS_UNIX(level)) {
75 additional_flags2 = FLAGS2_REPARSE_PATH;
78 subreq = cli_trans_send(
82 additional_flags2, /* additional_flags2 */
84 NULL, /* pipe name. */
88 &state->setup, /* setup. */
89 1, /* num setup uint16_t words. */
90 0, /* max returned setup. */
91 state->param, /* param. */
92 talloc_get_size(state->param), /* num param. */
93 2, /* max returned param. */
95 data_len, /* num data. */
96 0); /* max returned data. */
98 if (tevent_req_nomem(subreq, req)) {
99 return tevent_req_post(req, ev);
101 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
105 static void cli_setpathinfo_done(struct tevent_req *subreq)
107 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
108 NULL, 0, NULL, NULL, 0, NULL);
109 tevent_req_simple_finish_ntstatus(subreq, status);
112 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
114 return tevent_req_simple_recv_ntstatus(req);
117 NTSTATUS cli_setpathinfo(struct cli_state *cli,
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct tevent_context *ev;
125 struct tevent_req *req;
126 NTSTATUS status = NT_STATUS_NO_MEMORY;
128 if (smbXcli_conn_has_async_calls(cli->conn)) {
130 * Can't use sync call while an async call is in flight
132 status = NT_STATUS_INVALID_PARAMETER;
135 ev = samba_tevent_context_init(frame);
139 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
143 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
146 status = cli_setpathinfo_recv(req);
152 /****************************************************************************
153 Hard/Symlink a file (UNIX extensions).
154 Creates new name (sym)linked to oldname.
155 ****************************************************************************/
157 struct cli_posix_link_internal_state {
161 static void cli_posix_link_internal_done(struct tevent_req *subreq);
163 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
164 struct tevent_context *ev,
165 struct cli_state *cli,
170 struct tevent_req *req = NULL, *subreq = NULL;
171 struct cli_posix_link_internal_state *state = NULL;
173 req = tevent_req_create(mem_ctx, &state,
174 struct cli_posix_link_internal_state);
179 /* Setup data array. */
180 state->data = talloc_array(state, uint8_t, 0);
181 if (tevent_req_nomem(state->data, req)) {
182 return tevent_req_post(req, ev);
184 state->data = trans2_bytes_push_str(
185 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
187 subreq = cli_setpathinfo_send(
188 state, ev, cli, level, newname,
189 state->data, talloc_get_size(state->data));
190 if (tevent_req_nomem(subreq, req)) {
191 return tevent_req_post(req, ev);
193 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
197 static void cli_posix_link_internal_done(struct tevent_req *subreq)
199 NTSTATUS status = cli_setpathinfo_recv(subreq);
200 tevent_req_simple_finish_ntstatus(subreq, status);
203 /****************************************************************************
204 Symlink a file (UNIX extensions).
205 ****************************************************************************/
207 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
208 struct tevent_context *ev,
209 struct cli_state *cli,
213 return cli_posix_link_internal_send(
214 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
217 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
219 return tevent_req_simple_recv_ntstatus(req);
222 NTSTATUS cli_posix_symlink(struct cli_state *cli,
226 TALLOC_CTX *frame = talloc_stackframe();
227 struct tevent_context *ev = NULL;
228 struct tevent_req *req = NULL;
229 NTSTATUS status = NT_STATUS_OK;
231 if (smbXcli_conn_has_async_calls(cli->conn)) {
233 * Can't use sync call while an async call is in flight
235 status = NT_STATUS_INVALID_PARAMETER;
239 ev = samba_tevent_context_init(frame);
241 status = NT_STATUS_NO_MEMORY;
245 req = cli_posix_symlink_send(frame,
251 status = NT_STATUS_NO_MEMORY;
255 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
259 status = cli_posix_symlink_recv(req);
266 /****************************************************************************
267 Read a POSIX symlink.
268 ****************************************************************************/
270 struct readlink_state {
275 static void cli_posix_readlink_done(struct tevent_req *subreq);
277 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
278 struct tevent_context *ev,
279 struct cli_state *cli,
283 struct tevent_req *req = NULL, *subreq = NULL;
284 struct readlink_state *state = NULL;
285 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
287 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
293 * Len is in bytes, we need it in UCS2 units.
295 if ((2*len < len) || (maxbytelen < len)) {
296 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
297 return tevent_req_post(req, ev);
300 subreq = cli_qpathinfo_send(state, ev, cli, fname,
301 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
302 if (tevent_req_nomem(subreq, req)) {
303 return tevent_req_post(req, ev);
305 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
309 static void cli_posix_readlink_done(struct tevent_req *subreq)
311 struct tevent_req *req = tevent_req_callback_data(
312 subreq, struct tevent_req);
313 struct readlink_state *state = tevent_req_data(
314 req, struct readlink_state);
317 status = cli_qpathinfo_recv(subreq, state, &state->data,
320 if (tevent_req_nterror(req, status)) {
324 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
326 if (state->data[state->num_data-1] != '\0') {
327 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
330 tevent_req_done(req);
333 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
334 char *retpath, size_t len)
337 char *converted = NULL;
338 size_t converted_size = 0;
339 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
341 if (tevent_req_is_nterror(req, &status)) {
344 /* The returned data is a pushed string, not raw data. */
345 if (!convert_string_talloc(state,
346 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
352 return NT_STATUS_NO_MEMORY;
355 len = MIN(len,converted_size);
357 return NT_STATUS_DATA_ERROR;
359 memcpy(retpath, converted, len);
363 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
364 char *linkpath, size_t len)
366 TALLOC_CTX *frame = talloc_stackframe();
367 struct tevent_context *ev = NULL;
368 struct tevent_req *req = NULL;
369 NTSTATUS status = NT_STATUS_OK;
371 if (smbXcli_conn_has_async_calls(cli->conn)) {
373 * Can't use sync call while an async call is in flight
375 status = NT_STATUS_INVALID_PARAMETER;
379 ev = samba_tevent_context_init(frame);
381 status = NT_STATUS_NO_MEMORY;
385 req = cli_posix_readlink_send(frame,
391 status = NT_STATUS_NO_MEMORY;
395 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
399 status = cli_posix_readlink_recv(req, cli, linkpath, len);
406 /****************************************************************************
407 Hard link a file (UNIX extensions).
408 ****************************************************************************/
410 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
411 struct tevent_context *ev,
412 struct cli_state *cli,
416 return cli_posix_link_internal_send(
417 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
420 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
422 return tevent_req_simple_recv_ntstatus(req);
425 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
429 TALLOC_CTX *frame = talloc_stackframe();
430 struct tevent_context *ev = NULL;
431 struct tevent_req *req = NULL;
432 NTSTATUS status = NT_STATUS_OK;
434 if (smbXcli_conn_has_async_calls(cli->conn)) {
436 * Can't use sync call while an async call is in flight
438 status = NT_STATUS_INVALID_PARAMETER;
442 ev = samba_tevent_context_init(frame);
444 status = NT_STATUS_NO_MEMORY;
448 req = cli_posix_hardlink_send(frame,
454 status = NT_STATUS_NO_MEMORY;
458 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
462 status = cli_posix_hardlink_recv(req);
469 /****************************************************************************
470 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
471 ****************************************************************************/
473 struct getacl_state {
478 static void cli_posix_getacl_done(struct tevent_req *subreq);
480 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
481 struct tevent_context *ev,
482 struct cli_state *cli,
485 struct tevent_req *req = NULL, *subreq = NULL;
486 struct getacl_state *state = NULL;
488 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
492 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
494 if (tevent_req_nomem(subreq, req)) {
495 return tevent_req_post(req, ev);
497 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
501 static void cli_posix_getacl_done(struct tevent_req *subreq)
503 struct tevent_req *req = tevent_req_callback_data(
504 subreq, struct tevent_req);
505 struct getacl_state *state = tevent_req_data(
506 req, struct getacl_state);
509 status = cli_qpathinfo_recv(subreq, state, &state->data,
512 if (tevent_req_nterror(req, status)) {
515 tevent_req_done(req);
518 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
523 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
526 if (tevent_req_is_nterror(req, &status)) {
529 *prb_size = (size_t)state->num_data;
530 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
534 NTSTATUS cli_posix_getacl(struct cli_state *cli,
540 TALLOC_CTX *frame = talloc_stackframe();
541 struct tevent_context *ev = NULL;
542 struct tevent_req *req = NULL;
543 NTSTATUS status = NT_STATUS_OK;
545 if (smbXcli_conn_has_async_calls(cli->conn)) {
547 * Can't use sync call while an async call is in flight
549 status = NT_STATUS_INVALID_PARAMETER;
553 ev = samba_tevent_context_init(frame);
555 status = NT_STATUS_NO_MEMORY;
559 req = cli_posix_getacl_send(frame,
564 status = NT_STATUS_NO_MEMORY;
568 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
572 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
579 /****************************************************************************
580 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
581 ****************************************************************************/
583 struct setacl_state {
587 static void cli_posix_setacl_done(struct tevent_req *subreq);
589 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
590 struct tevent_context *ev,
591 struct cli_state *cli,
596 struct tevent_req *req = NULL, *subreq = NULL;
597 struct setacl_state *state = NULL;
599 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
603 state->data = talloc_memdup(state, data, num_data);
604 if (tevent_req_nomem(state->data, req)) {
605 return tevent_req_post(req, ev);
608 subreq = cli_setpathinfo_send(state,
615 if (tevent_req_nomem(subreq, req)) {
616 return tevent_req_post(req, ev);
618 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
622 static void cli_posix_setacl_done(struct tevent_req *subreq)
624 NTSTATUS status = cli_setpathinfo_recv(subreq);
625 tevent_req_simple_finish_ntstatus(subreq, status);
628 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
630 return tevent_req_simple_recv_ntstatus(req);
633 NTSTATUS cli_posix_setacl(struct cli_state *cli,
638 TALLOC_CTX *frame = talloc_stackframe();
639 struct tevent_context *ev = NULL;
640 struct tevent_req *req = NULL;
641 NTSTATUS status = NT_STATUS_OK;
643 if (smbXcli_conn_has_async_calls(cli->conn)) {
645 * Can't use sync call while an async call is in flight
647 status = NT_STATUS_INVALID_PARAMETER;
651 ev = samba_tevent_context_init(frame);
653 status = NT_STATUS_NO_MEMORY;
657 req = cli_posix_setacl_send(frame,
664 status = NT_STATUS_NO_MEMORY;
668 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
672 status = cli_posix_setacl_recv(req);
679 /****************************************************************************
680 Stat a file (UNIX extensions).
681 ****************************************************************************/
688 static void cli_posix_stat_done(struct tevent_req *subreq);
690 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
691 struct tevent_context *ev,
692 struct cli_state *cli,
695 struct tevent_req *req = NULL, *subreq = NULL;
696 struct stat_state *state = NULL;
698 req = tevent_req_create(mem_ctx, &state, struct stat_state);
702 subreq = cli_qpathinfo_send(state, ev, cli, fname,
703 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
704 if (tevent_req_nomem(subreq, req)) {
705 return tevent_req_post(req, ev);
707 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
711 static void cli_posix_stat_done(struct tevent_req *subreq)
713 struct tevent_req *req = tevent_req_callback_data(
714 subreq, struct tevent_req);
715 struct stat_state *state = tevent_req_data(req, struct stat_state);
718 status = cli_qpathinfo_recv(subreq, state, &state->data,
721 if (tevent_req_nterror(req, status)) {
724 tevent_req_done(req);
727 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
728 SMB_STRUCT_STAT *sbuf)
730 struct stat_state *state = tevent_req_data(req, struct stat_state);
733 if (tevent_req_is_nterror(req, &status)) {
737 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
738 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
739 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
740 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
742 /* assume 512 byte blocks */
743 sbuf->st_ex_blocks /= 512;
745 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
746 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
747 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
749 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
750 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
751 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
752 #if defined(HAVE_MAKEDEV)
754 uint32_t dev_major = IVAL(state->data,60);
755 uint32_t dev_minor = IVAL(state->data,68);
756 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
759 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
760 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
761 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
766 NTSTATUS cli_posix_stat(struct cli_state *cli,
768 SMB_STRUCT_STAT *sbuf)
770 TALLOC_CTX *frame = talloc_stackframe();
771 struct tevent_context *ev = NULL;
772 struct tevent_req *req = NULL;
773 NTSTATUS status = NT_STATUS_OK;
775 if (smbXcli_conn_has_async_calls(cli->conn)) {
777 * Can't use sync call while an async call is in flight
779 status = NT_STATUS_INVALID_PARAMETER;
783 ev = samba_tevent_context_init(frame);
785 status = NT_STATUS_NO_MEMORY;
789 req = cli_posix_stat_send(frame,
794 status = NT_STATUS_NO_MEMORY;
798 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
802 status = cli_posix_stat_recv(req, sbuf);
809 /****************************************************************************
810 Chmod or chown a file internal (UNIX extensions).
811 ****************************************************************************/
813 struct cli_posix_chown_chmod_internal_state {
817 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
819 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
820 struct tevent_context *ev,
821 struct cli_state *cli,
827 struct tevent_req *req = NULL, *subreq = NULL;
828 struct cli_posix_chown_chmod_internal_state *state = NULL;
830 req = tevent_req_create(mem_ctx, &state,
831 struct cli_posix_chown_chmod_internal_state);
836 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
837 memset(&state->data[40], '\0', 60);
838 SIVAL(state->data,40,uid);
839 SIVAL(state->data,48,gid);
840 SIVAL(state->data,84,mode);
842 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
843 fname, state->data, sizeof(state->data));
844 if (tevent_req_nomem(subreq, req)) {
845 return tevent_req_post(req, ev);
847 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
852 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
854 NTSTATUS status = cli_setpathinfo_recv(subreq);
855 tevent_req_simple_finish_ntstatus(subreq, status);
858 /****************************************************************************
859 chmod a file (UNIX extensions).
860 ****************************************************************************/
862 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
863 struct tevent_context *ev,
864 struct cli_state *cli,
868 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
870 unix_perms_to_wire(mode),
875 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
877 return tevent_req_simple_recv_ntstatus(req);
880 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
882 TALLOC_CTX *frame = talloc_stackframe();
883 struct tevent_context *ev = NULL;
884 struct tevent_req *req = NULL;
885 NTSTATUS status = NT_STATUS_OK;
887 if (smbXcli_conn_has_async_calls(cli->conn)) {
889 * Can't use sync call while an async call is in flight
891 status = NT_STATUS_INVALID_PARAMETER;
895 ev = samba_tevent_context_init(frame);
897 status = NT_STATUS_NO_MEMORY;
901 req = cli_posix_chmod_send(frame,
907 status = NT_STATUS_NO_MEMORY;
911 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
915 status = cli_posix_chmod_recv(req);
922 /****************************************************************************
923 chown a file (UNIX extensions).
924 ****************************************************************************/
926 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
927 struct tevent_context *ev,
928 struct cli_state *cli,
933 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
940 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
942 return tevent_req_simple_recv_ntstatus(req);
945 NTSTATUS cli_posix_chown(struct cli_state *cli,
950 TALLOC_CTX *frame = talloc_stackframe();
951 struct tevent_context *ev = NULL;
952 struct tevent_req *req = NULL;
953 NTSTATUS status = NT_STATUS_OK;
955 if (smbXcli_conn_has_async_calls(cli->conn)) {
957 * Can't use sync call while an async call is in flight
959 status = NT_STATUS_INVALID_PARAMETER;
963 ev = samba_tevent_context_init(frame);
965 status = NT_STATUS_NO_MEMORY;
969 req = cli_posix_chown_send(frame,
976 status = NT_STATUS_NO_MEMORY;
980 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
984 status = cli_posix_chown_recv(req);
991 /****************************************************************************
993 ****************************************************************************/
995 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
996 struct tevent_context *ev,
997 struct cli_state *cli,
998 const char *fname_src,
999 const char *fname_dst,
1002 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1003 struct tevent_context *ev,
1004 struct cli_state *cli,
1005 const char *fname_src,
1006 const char *fname_dst,
1009 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1010 struct tevent_context *ev,
1011 struct cli_state *cli,
1012 const char *fname_src,
1013 const char *fname_dst,
1016 if (replace && smbXcli_conn_support_passthrough(cli->conn)) {
1017 return cli_smb1_rename_send(mem_ctx, ev, cli, fname_src,
1018 fname_dst, replace);
1020 return cli_cifs_rename_send(mem_ctx, ev, cli, fname_src,
1021 fname_dst, replace);
1025 struct cli_smb1_rename_state {
1029 static void cli_smb1_rename_done(struct tevent_req *subreq);
1031 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1032 struct tevent_context *ev,
1033 struct cli_state *cli,
1034 const char *fname_src,
1035 const char *fname_dst,
1039 struct tevent_req *req = NULL, *subreq = NULL;
1040 struct cli_smb1_rename_state *state = NULL;
1041 smb_ucs2_t *converted_str = NULL;
1042 size_t converted_size_bytes = 0;
1044 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_rename_state);
1049 if (!push_ucs2_talloc(talloc_tos(), &converted_str, fname_dst,
1050 &converted_size_bytes)) {
1051 status = NT_STATUS_INVALID_PARAMETER;
1055 /* W2K8 insists the dest name is not null
1056 terminated. Remove the last 2 zero bytes
1057 and reduce the name length. */
1059 if (converted_size_bytes < 2) {
1060 status = NT_STATUS_INVALID_PARAMETER;
1063 converted_size_bytes -= 2;
1066 talloc_zero_array(state, uint8_t, 12 + converted_size_bytes);
1067 if (state->data == NULL) {
1068 status = NT_STATUS_NO_MEMORY;
1073 SCVAL(state->data, 0, 1);
1076 SIVAL(state->data, 8, converted_size_bytes);
1077 memcpy(state->data + 12, converted_str, converted_size_bytes);
1079 TALLOC_FREE(converted_str);
1081 subreq = cli_setpathinfo_send(
1082 state, ev, cli, SMB_FILE_RENAME_INFORMATION, fname_src, state->data,
1083 talloc_get_size(state->data));
1084 if (tevent_req_nomem(subreq, req)) {
1085 status = NT_STATUS_NO_MEMORY;
1088 tevent_req_set_callback(subreq, cli_smb1_rename_done, req);
1092 TALLOC_FREE(converted_str);
1093 tevent_req_nterror(req, status);
1094 return tevent_req_post(req, ev);
1097 static void cli_smb1_rename_done(struct tevent_req *subreq)
1099 NTSTATUS status = cli_setpathinfo_recv(subreq);
1100 tevent_req_simple_finish_ntstatus(subreq, status);
1103 static void cli_cifs_rename_done(struct tevent_req *subreq);
1105 struct cli_cifs_rename_state {
1109 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1110 struct tevent_context *ev,
1111 struct cli_state *cli,
1112 const char *fname_src,
1113 const char *fname_dst,
1116 struct tevent_req *req = NULL, *subreq = NULL;
1117 struct cli_cifs_rename_state *state = NULL;
1118 uint8_t additional_flags = 0;
1119 uint16_t additional_flags2 = 0;
1120 uint8_t *bytes = NULL;
1122 req = tevent_req_create(mem_ctx, &state, struct cli_cifs_rename_state);
1129 * CIFS doesn't support replace
1131 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1132 return tevent_req_post(req, ev);
1135 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1137 bytes = talloc_array(state, uint8_t, 1);
1138 if (tevent_req_nomem(bytes, req)) {
1139 return tevent_req_post(req, ev);
1142 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1143 strlen(fname_src)+1, NULL);
1144 if (tevent_req_nomem(bytes, req)) {
1145 return tevent_req_post(req, ev);
1148 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1149 additional_flags2 = FLAGS2_REPARSE_PATH;
1152 bytes = talloc_realloc(state, bytes, uint8_t,
1153 talloc_get_size(bytes)+1);
1154 if (tevent_req_nomem(bytes, req)) {
1155 return tevent_req_post(req, ev);
1158 bytes[talloc_get_size(bytes)-1] = 4;
1159 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1160 strlen(fname_dst)+1, NULL);
1161 if (tevent_req_nomem(bytes, req)) {
1162 return tevent_req_post(req, ev);
1165 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1167 1, state->vwv, talloc_get_size(bytes), bytes);
1168 if (tevent_req_nomem(subreq, req)) {
1169 return tevent_req_post(req, ev);
1171 tevent_req_set_callback(subreq, cli_cifs_rename_done, req);
1175 static void cli_cifs_rename_done(struct tevent_req *subreq)
1177 struct tevent_req *req = tevent_req_callback_data(
1178 subreq, struct tevent_req);
1181 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1182 TALLOC_FREE(subreq);
1183 if (tevent_req_nterror(req, status)) {
1186 tevent_req_done(req);
1189 NTSTATUS cli_rename_recv(struct tevent_req *req)
1191 return tevent_req_simple_recv_ntstatus(req);
1194 NTSTATUS cli_rename(struct cli_state *cli,
1195 const char *fname_src,
1196 const char *fname_dst,
1199 TALLOC_CTX *frame = NULL;
1200 struct tevent_context *ev;
1201 struct tevent_req *req;
1202 NTSTATUS status = NT_STATUS_OK;
1204 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1205 return cli_smb2_rename(cli, fname_src, fname_dst, replace);
1208 frame = talloc_stackframe();
1210 if (smbXcli_conn_has_async_calls(cli->conn)) {
1212 * Can't use sync call while an async call is in flight
1214 status = NT_STATUS_INVALID_PARAMETER;
1218 ev = samba_tevent_context_init(frame);
1220 status = NT_STATUS_NO_MEMORY;
1224 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst, replace);
1226 status = NT_STATUS_NO_MEMORY;
1230 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1234 status = cli_rename_recv(req);
1241 /****************************************************************************
1243 ****************************************************************************/
1245 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1247 struct cli_ntrename_internal_state {
1251 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1252 struct tevent_context *ev,
1253 struct cli_state *cli,
1254 const char *fname_src,
1255 const char *fname_dst,
1256 uint16_t rename_flag)
1258 struct tevent_req *req = NULL, *subreq = NULL;
1259 struct cli_ntrename_internal_state *state = NULL;
1260 uint8_t additional_flags = 0;
1261 uint16_t additional_flags2 = 0;
1262 uint8_t *bytes = NULL;
1264 req = tevent_req_create(mem_ctx, &state,
1265 struct cli_ntrename_internal_state);
1270 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1271 SSVAL(state->vwv+1, 0, rename_flag);
1273 bytes = talloc_array(state, uint8_t, 1);
1274 if (tevent_req_nomem(bytes, req)) {
1275 return tevent_req_post(req, ev);
1278 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1279 strlen(fname_src)+1, NULL);
1280 if (tevent_req_nomem(bytes, req)) {
1281 return tevent_req_post(req, ev);
1284 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1285 additional_flags2 = FLAGS2_REPARSE_PATH;
1288 bytes = talloc_realloc(state, bytes, uint8_t,
1289 talloc_get_size(bytes)+1);
1290 if (tevent_req_nomem(bytes, req)) {
1291 return tevent_req_post(req, ev);
1294 bytes[talloc_get_size(bytes)-1] = 4;
1295 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1296 strlen(fname_dst)+1, NULL);
1297 if (tevent_req_nomem(bytes, req)) {
1298 return tevent_req_post(req, ev);
1301 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1303 4, state->vwv, talloc_get_size(bytes), bytes);
1304 if (tevent_req_nomem(subreq, req)) {
1305 return tevent_req_post(req, ev);
1307 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1311 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1313 struct tevent_req *req = tevent_req_callback_data(
1314 subreq, struct tevent_req);
1317 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1318 TALLOC_FREE(subreq);
1319 if (tevent_req_nterror(req, status)) {
1322 tevent_req_done(req);
1325 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1327 return tevent_req_simple_recv_ntstatus(req);
1330 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1331 struct tevent_context *ev,
1332 struct cli_state *cli,
1333 const char *fname_src,
1334 const char *fname_dst)
1336 return cli_ntrename_internal_send(mem_ctx,
1341 RENAME_FLAG_RENAME);
1344 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1346 return cli_ntrename_internal_recv(req);
1349 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1351 TALLOC_CTX *frame = talloc_stackframe();
1352 struct tevent_context *ev;
1353 struct tevent_req *req;
1354 NTSTATUS status = NT_STATUS_OK;
1356 if (smbXcli_conn_has_async_calls(cli->conn)) {
1358 * Can't use sync call while an async call is in flight
1360 status = NT_STATUS_INVALID_PARAMETER;
1364 ev = samba_tevent_context_init(frame);
1366 status = NT_STATUS_NO_MEMORY;
1370 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1372 status = NT_STATUS_NO_MEMORY;
1376 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1380 status = cli_ntrename_recv(req);
1387 /****************************************************************************
1389 ****************************************************************************/
1391 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1392 struct tevent_context *ev,
1393 struct cli_state *cli,
1394 const char *fname_src,
1395 const char *fname_dst)
1397 return cli_ntrename_internal_send(mem_ctx,
1402 RENAME_FLAG_HARD_LINK);
1405 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1407 return cli_ntrename_internal_recv(req);
1410 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1412 TALLOC_CTX *frame = talloc_stackframe();
1413 struct tevent_context *ev;
1414 struct tevent_req *req;
1415 NTSTATUS status = NT_STATUS_OK;
1417 if (smbXcli_conn_has_async_calls(cli->conn)) {
1419 * Can't use sync call while an async call is in flight
1421 status = NT_STATUS_INVALID_PARAMETER;
1425 ev = samba_tevent_context_init(frame);
1427 status = NT_STATUS_NO_MEMORY;
1431 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1433 status = NT_STATUS_NO_MEMORY;
1437 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1441 status = cli_nt_hardlink_recv(req);
1448 /****************************************************************************
1450 ****************************************************************************/
1452 static void cli_unlink_done(struct tevent_req *subreq);
1454 struct cli_unlink_state {
1458 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1459 struct tevent_context *ev,
1460 struct cli_state *cli,
1462 uint16_t mayhave_attrs)
1464 struct tevent_req *req = NULL, *subreq = NULL;
1465 struct cli_unlink_state *state = NULL;
1466 uint8_t additional_flags = 0;
1467 uint16_t additional_flags2 = 0;
1468 uint8_t *bytes = NULL;
1470 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1475 SSVAL(state->vwv+0, 0, mayhave_attrs);
1477 bytes = talloc_array(state, uint8_t, 1);
1478 if (tevent_req_nomem(bytes, req)) {
1479 return tevent_req_post(req, ev);
1482 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1483 strlen(fname)+1, NULL);
1485 if (tevent_req_nomem(bytes, req)) {
1486 return tevent_req_post(req, ev);
1489 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1490 additional_flags2 = FLAGS2_REPARSE_PATH;
1493 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1495 1, state->vwv, talloc_get_size(bytes), bytes);
1496 if (tevent_req_nomem(subreq, req)) {
1497 return tevent_req_post(req, ev);
1499 tevent_req_set_callback(subreq, cli_unlink_done, req);
1503 static void cli_unlink_done(struct tevent_req *subreq)
1505 struct tevent_req *req = tevent_req_callback_data(
1506 subreq, struct tevent_req);
1509 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1510 TALLOC_FREE(subreq);
1511 if (tevent_req_nterror(req, status)) {
1514 tevent_req_done(req);
1517 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1519 return tevent_req_simple_recv_ntstatus(req);
1522 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1524 TALLOC_CTX *frame = NULL;
1525 struct tevent_context *ev;
1526 struct tevent_req *req;
1527 NTSTATUS status = NT_STATUS_OK;
1529 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1530 return cli_smb2_unlink(cli, fname);
1533 frame = talloc_stackframe();
1535 if (smbXcli_conn_has_async_calls(cli->conn)) {
1537 * Can't use sync call while an async call is in flight
1539 status = NT_STATUS_INVALID_PARAMETER;
1543 ev = samba_tevent_context_init(frame);
1545 status = NT_STATUS_NO_MEMORY;
1549 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1551 status = NT_STATUS_NO_MEMORY;
1555 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1559 status = cli_unlink_recv(req);
1566 /****************************************************************************
1568 ****************************************************************************/
1570 static void cli_mkdir_done(struct tevent_req *subreq);
1572 struct cli_mkdir_state {
1576 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1577 struct tevent_context *ev,
1578 struct cli_state *cli,
1581 struct tevent_req *req = NULL, *subreq = NULL;
1582 struct cli_mkdir_state *state = NULL;
1583 uint8_t additional_flags = 0;
1584 uint16_t additional_flags2 = 0;
1585 uint8_t *bytes = NULL;
1587 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1592 bytes = talloc_array(state, uint8_t, 1);
1593 if (tevent_req_nomem(bytes, req)) {
1594 return tevent_req_post(req, ev);
1597 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1598 strlen(dname)+1, NULL);
1600 if (tevent_req_nomem(bytes, req)) {
1601 return tevent_req_post(req, ev);
1604 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1605 additional_flags2 = FLAGS2_REPARSE_PATH;
1608 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1610 0, NULL, talloc_get_size(bytes), bytes);
1611 if (tevent_req_nomem(subreq, req)) {
1612 return tevent_req_post(req, ev);
1614 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1618 static void cli_mkdir_done(struct tevent_req *subreq)
1620 struct tevent_req *req = tevent_req_callback_data(
1621 subreq, struct tevent_req);
1624 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1625 TALLOC_FREE(subreq);
1626 if (tevent_req_nterror(req, status)) {
1629 tevent_req_done(req);
1632 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1634 return tevent_req_simple_recv_ntstatus(req);
1637 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1639 TALLOC_CTX *frame = NULL;
1640 struct tevent_context *ev;
1641 struct tevent_req *req;
1642 NTSTATUS status = NT_STATUS_OK;
1644 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1645 return cli_smb2_mkdir(cli, dname);
1648 frame = talloc_stackframe();
1650 if (smbXcli_conn_has_async_calls(cli->conn)) {
1652 * Can't use sync call while an async call is in flight
1654 status = NT_STATUS_INVALID_PARAMETER;
1658 ev = samba_tevent_context_init(frame);
1660 status = NT_STATUS_NO_MEMORY;
1664 req = cli_mkdir_send(frame, ev, cli, dname);
1666 status = NT_STATUS_NO_MEMORY;
1670 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1674 status = cli_mkdir_recv(req);
1681 /****************************************************************************
1683 ****************************************************************************/
1685 static void cli_rmdir_done(struct tevent_req *subreq);
1687 struct cli_rmdir_state {
1691 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1692 struct tevent_context *ev,
1693 struct cli_state *cli,
1696 struct tevent_req *req = NULL, *subreq = NULL;
1697 struct cli_rmdir_state *state = NULL;
1698 uint8_t additional_flags = 0;
1699 uint16_t additional_flags2 = 0;
1700 uint8_t *bytes = NULL;
1702 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1707 bytes = talloc_array(state, uint8_t, 1);
1708 if (tevent_req_nomem(bytes, req)) {
1709 return tevent_req_post(req, ev);
1712 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1713 strlen(dname)+1, NULL);
1715 if (tevent_req_nomem(bytes, req)) {
1716 return tevent_req_post(req, ev);
1719 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1720 additional_flags2 = FLAGS2_REPARSE_PATH;
1723 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1725 0, NULL, talloc_get_size(bytes), bytes);
1726 if (tevent_req_nomem(subreq, req)) {
1727 return tevent_req_post(req, ev);
1729 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1733 static void cli_rmdir_done(struct tevent_req *subreq)
1735 struct tevent_req *req = tevent_req_callback_data(
1736 subreq, struct tevent_req);
1739 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1740 TALLOC_FREE(subreq);
1741 if (tevent_req_nterror(req, status)) {
1744 tevent_req_done(req);
1747 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1749 return tevent_req_simple_recv_ntstatus(req);
1752 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1754 TALLOC_CTX *frame = NULL;
1755 struct tevent_context *ev;
1756 struct tevent_req *req;
1757 NTSTATUS status = NT_STATUS_OK;
1759 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1760 return cli_smb2_rmdir(cli, dname);
1763 frame = talloc_stackframe();
1765 if (smbXcli_conn_has_async_calls(cli->conn)) {
1767 * Can't use sync call while an async call is in flight
1769 status = NT_STATUS_INVALID_PARAMETER;
1773 ev = samba_tevent_context_init(frame);
1775 status = NT_STATUS_NO_MEMORY;
1779 req = cli_rmdir_send(frame, ev, cli, dname);
1781 status = NT_STATUS_NO_MEMORY;
1785 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1789 status = cli_rmdir_recv(req);
1796 /****************************************************************************
1797 Set or clear the delete on close flag.
1798 ****************************************************************************/
1806 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1808 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1809 NULL, 0, NULL, NULL, 0, NULL);
1810 tevent_req_simple_finish_ntstatus(subreq, status);
1813 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1814 struct tevent_context *ev,
1815 struct cli_state *cli,
1819 struct tevent_req *req = NULL, *subreq = NULL;
1820 struct doc_state *state = NULL;
1822 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1827 /* Setup setup word. */
1828 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1830 /* Setup param array. */
1831 SSVAL(state->param,0,fnum);
1832 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1834 /* Setup data array. */
1835 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1837 subreq = cli_trans_send(state, /* mem ctx. */
1838 ev, /* event ctx. */
1839 cli, /* cli_state. */
1840 0, /* additional_flags2 */
1841 SMBtrans2, /* cmd. */
1842 NULL, /* pipe name. */
1846 &state->setup, /* setup. */
1847 1, /* num setup uint16_t words. */
1848 0, /* max returned setup. */
1849 state->param, /* param. */
1851 2, /* max returned param. */
1852 state->data, /* data. */
1854 0); /* max returned data. */
1856 if (tevent_req_nomem(subreq, req)) {
1857 return tevent_req_post(req, ev);
1859 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1863 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1865 return tevent_req_simple_recv_ntstatus(req);
1868 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1870 TALLOC_CTX *frame = talloc_stackframe();
1871 struct tevent_context *ev = NULL;
1872 struct tevent_req *req = NULL;
1873 NTSTATUS status = NT_STATUS_OK;
1875 if (smbXcli_conn_has_async_calls(cli->conn)) {
1877 * Can't use sync call while an async call is in flight
1879 status = NT_STATUS_INVALID_PARAMETER;
1883 ev = samba_tevent_context_init(frame);
1885 status = NT_STATUS_NO_MEMORY;
1889 req = cli_nt_delete_on_close_send(frame,
1895 status = NT_STATUS_NO_MEMORY;
1899 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1903 status = cli_nt_delete_on_close_recv(req);
1910 struct cli_ntcreate1_state {
1913 struct smb_create_returns cr;
1914 struct tevent_req *subreq;
1917 static void cli_ntcreate1_done(struct tevent_req *subreq);
1918 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1920 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1921 struct tevent_context *ev,
1922 struct cli_state *cli,
1924 uint32_t CreatFlags,
1925 uint32_t DesiredAccess,
1926 uint32_t FileAttributes,
1927 uint32_t ShareAccess,
1928 uint32_t CreateDisposition,
1929 uint32_t CreateOptions,
1930 uint8_t SecurityFlags)
1932 struct tevent_req *req, *subreq;
1933 struct cli_ntcreate1_state *state;
1936 size_t converted_len;
1937 uint16_t additional_flags2 = 0;
1939 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1946 SCVAL(vwv+0, 0, 0xFF);
1951 if (cli->use_oplocks) {
1952 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1954 SIVAL(vwv+3, 1, CreatFlags);
1955 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1956 SIVAL(vwv+7, 1, DesiredAccess);
1957 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1958 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1959 SIVAL(vwv+13, 1, FileAttributes);
1960 SIVAL(vwv+15, 1, ShareAccess);
1961 SIVAL(vwv+17, 1, CreateDisposition);
1962 SIVAL(vwv+19, 1, CreateOptions |
1963 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1964 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1965 SCVAL(vwv+23, 1, SecurityFlags);
1967 bytes = talloc_array(state, uint8_t, 0);
1968 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1969 fname, strlen(fname)+1,
1972 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1973 additional_flags2 = FLAGS2_REPARSE_PATH;
1976 /* sigh. this copes with broken netapp filer behaviour */
1977 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1979 if (tevent_req_nomem(bytes, req)) {
1980 return tevent_req_post(req, ev);
1983 SSVAL(vwv+2, 1, converted_len);
1985 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
1986 additional_flags2, 24, vwv,
1987 talloc_get_size(bytes), bytes);
1988 if (tevent_req_nomem(subreq, req)) {
1989 return tevent_req_post(req, ev);
1991 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1993 state->subreq = subreq;
1994 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
1999 static void cli_ntcreate1_done(struct tevent_req *subreq)
2001 struct tevent_req *req = tevent_req_callback_data(
2002 subreq, struct tevent_req);
2003 struct cli_ntcreate1_state *state = tevent_req_data(
2004 req, struct cli_ntcreate1_state);
2011 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2012 &num_bytes, &bytes);
2013 TALLOC_FREE(subreq);
2014 if (tevent_req_nterror(req, status)) {
2017 state->cr.oplock_level = CVAL(vwv+2, 0);
2018 state->fnum = SVAL(vwv+2, 1);
2019 state->cr.create_action = IVAL(vwv+3, 1);
2020 state->cr.creation_time = BVAL(vwv+5, 1);
2021 state->cr.last_access_time = BVAL(vwv+9, 1);
2022 state->cr.last_write_time = BVAL(vwv+13, 1);
2023 state->cr.change_time = BVAL(vwv+17, 1);
2024 state->cr.file_attributes = IVAL(vwv+21, 1);
2025 state->cr.allocation_size = BVAL(vwv+23, 1);
2026 state->cr.end_of_file = BVAL(vwv+27, 1);
2028 tevent_req_done(req);
2031 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2033 struct cli_ntcreate1_state *state = tevent_req_data(
2034 req, struct cli_ntcreate1_state);
2035 return tevent_req_cancel(state->subreq);
2038 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2040 struct smb_create_returns *cr)
2042 struct cli_ntcreate1_state *state = tevent_req_data(
2043 req, struct cli_ntcreate1_state);
2046 if (tevent_req_is_nterror(req, &status)) {
2049 *pfnum = state->fnum;
2053 return NT_STATUS_OK;
2056 struct cli_ntcreate_state {
2057 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2058 struct smb_create_returns *cr);
2059 struct smb_create_returns cr;
2061 struct tevent_req *subreq;
2064 static void cli_ntcreate_done(struct tevent_req *subreq);
2065 static bool cli_ntcreate_cancel(struct tevent_req *req);
2067 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2068 struct tevent_context *ev,
2069 struct cli_state *cli,
2071 uint32_t create_flags,
2072 uint32_t desired_access,
2073 uint32_t file_attributes,
2074 uint32_t share_access,
2075 uint32_t create_disposition,
2076 uint32_t create_options,
2077 uint8_t security_flags)
2079 struct tevent_req *req, *subreq;
2080 struct cli_ntcreate_state *state;
2082 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2087 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2088 state->recv = cli_smb2_create_fnum_recv;
2090 if (cli->use_oplocks) {
2091 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2094 subreq = cli_smb2_create_fnum_send(
2095 state, ev, cli, fname, create_flags, desired_access,
2096 file_attributes, share_access, create_disposition,
2099 state->recv = cli_ntcreate1_recv;
2100 subreq = cli_ntcreate1_send(
2101 state, ev, cli, fname, create_flags, desired_access,
2102 file_attributes, share_access, create_disposition,
2103 create_options, security_flags);
2105 if (tevent_req_nomem(subreq, req)) {
2106 return tevent_req_post(req, ev);
2108 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2110 state->subreq = subreq;
2111 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2116 static void cli_ntcreate_done(struct tevent_req *subreq)
2118 struct tevent_req *req = tevent_req_callback_data(
2119 subreq, struct tevent_req);
2120 struct cli_ntcreate_state *state = tevent_req_data(
2121 req, struct cli_ntcreate_state);
2124 status = state->recv(subreq, &state->fnum, &state->cr);
2125 TALLOC_FREE(subreq);
2126 if (tevent_req_nterror(req, status)) {
2129 tevent_req_done(req);
2132 static bool cli_ntcreate_cancel(struct tevent_req *req)
2134 struct cli_ntcreate_state *state = tevent_req_data(
2135 req, struct cli_ntcreate_state);
2136 return tevent_req_cancel(state->subreq);
2139 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2140 struct smb_create_returns *cr)
2142 struct cli_ntcreate_state *state = tevent_req_data(
2143 req, struct cli_ntcreate_state);
2146 if (tevent_req_is_nterror(req, &status)) {
2150 *fnum = state->fnum;
2155 return NT_STATUS_OK;
2158 NTSTATUS cli_ntcreate(struct cli_state *cli,
2160 uint32_t CreatFlags,
2161 uint32_t DesiredAccess,
2162 uint32_t FileAttributes,
2163 uint32_t ShareAccess,
2164 uint32_t CreateDisposition,
2165 uint32_t CreateOptions,
2166 uint8_t SecurityFlags,
2168 struct smb_create_returns *cr)
2170 TALLOC_CTX *frame = talloc_stackframe();
2171 struct tevent_context *ev;
2172 struct tevent_req *req;
2173 NTSTATUS status = NT_STATUS_NO_MEMORY;
2175 if (smbXcli_conn_has_async_calls(cli->conn)) {
2177 * Can't use sync call while an async call is in flight
2179 status = NT_STATUS_INVALID_PARAMETER;
2183 ev = samba_tevent_context_init(frame);
2188 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2189 DesiredAccess, FileAttributes, ShareAccess,
2190 CreateDisposition, CreateOptions,
2196 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2200 status = cli_ntcreate_recv(req, pfid, cr);
2206 struct cli_nttrans_create_state {
2208 struct smb_create_returns cr;
2211 static void cli_nttrans_create_done(struct tevent_req *subreq);
2213 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2214 struct tevent_context *ev,
2215 struct cli_state *cli,
2217 uint32_t CreatFlags,
2218 uint32_t DesiredAccess,
2219 uint32_t FileAttributes,
2220 uint32_t ShareAccess,
2221 uint32_t CreateDisposition,
2222 uint32_t CreateOptions,
2223 uint8_t SecurityFlags,
2224 struct security_descriptor *secdesc,
2225 struct ea_struct *eas,
2228 struct tevent_req *req, *subreq;
2229 struct cli_nttrans_create_state *state;
2231 uint8_t *secdesc_buf;
2234 size_t converted_len;
2235 uint16_t additional_flags2 = 0;
2237 req = tevent_req_create(mem_ctx,
2238 &state, struct cli_nttrans_create_state);
2243 if (secdesc != NULL) {
2244 status = marshall_sec_desc(talloc_tos(), secdesc,
2245 &secdesc_buf, &secdesc_len);
2246 if (tevent_req_nterror(req, status)) {
2247 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2248 nt_errstr(status)));
2249 return tevent_req_post(req, ev);
2260 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2261 return tevent_req_post(req, ev);
2264 param = talloc_array(state, uint8_t, 53);
2265 if (tevent_req_nomem(param, req)) {
2266 return tevent_req_post(req, ev);
2269 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2270 fname, strlen(fname),
2272 if (tevent_req_nomem(param, req)) {
2273 return tevent_req_post(req, ev);
2276 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2277 additional_flags2 = FLAGS2_REPARSE_PATH;
2280 SIVAL(param, 0, CreatFlags);
2281 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2282 SIVAL(param, 8, DesiredAccess);
2283 SIVAL(param, 12, 0x0); /* AllocationSize */
2284 SIVAL(param, 16, 0x0); /* AllocationSize */
2285 SIVAL(param, 20, FileAttributes);
2286 SIVAL(param, 24, ShareAccess);
2287 SIVAL(param, 28, CreateDisposition);
2288 SIVAL(param, 32, CreateOptions |
2289 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2290 SIVAL(param, 36, secdesc_len);
2291 SIVAL(param, 40, 0); /* EA length*/
2292 SIVAL(param, 44, converted_len);
2293 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2294 SCVAL(param, 52, SecurityFlags);
2296 subreq = cli_trans_send(state, ev, cli,
2297 additional_flags2, /* additional_flags2 */
2299 NULL, -1, /* name, fid */
2300 NT_TRANSACT_CREATE, 0,
2301 NULL, 0, 0, /* setup */
2302 param, talloc_get_size(param), 128, /* param */
2303 secdesc_buf, secdesc_len, 0); /* data */
2304 if (tevent_req_nomem(subreq, req)) {
2305 return tevent_req_post(req, ev);
2307 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2311 static void cli_nttrans_create_done(struct tevent_req *subreq)
2313 struct tevent_req *req = tevent_req_callback_data(
2314 subreq, struct tevent_req);
2315 struct cli_nttrans_create_state *state = tevent_req_data(
2316 req, struct cli_nttrans_create_state);
2321 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2322 NULL, 0, NULL, /* rsetup */
2323 ¶m, 69, &num_param,
2325 if (tevent_req_nterror(req, status)) {
2328 state->cr.oplock_level = CVAL(param, 0);
2329 state->fnum = SVAL(param, 2);
2330 state->cr.create_action = IVAL(param, 4);
2331 state->cr.creation_time = BVAL(param, 12);
2332 state->cr.last_access_time = BVAL(param, 20);
2333 state->cr.last_write_time = BVAL(param, 28);
2334 state->cr.change_time = BVAL(param, 36);
2335 state->cr.file_attributes = IVAL(param, 44);
2336 state->cr.allocation_size = BVAL(param, 48);
2337 state->cr.end_of_file = BVAL(param, 56);
2340 tevent_req_done(req);
2343 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2345 struct smb_create_returns *cr)
2347 struct cli_nttrans_create_state *state = tevent_req_data(
2348 req, struct cli_nttrans_create_state);
2351 if (tevent_req_is_nterror(req, &status)) {
2354 *fnum = state->fnum;
2358 return NT_STATUS_OK;
2361 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2363 uint32_t CreatFlags,
2364 uint32_t DesiredAccess,
2365 uint32_t FileAttributes,
2366 uint32_t ShareAccess,
2367 uint32_t CreateDisposition,
2368 uint32_t CreateOptions,
2369 uint8_t SecurityFlags,
2370 struct security_descriptor *secdesc,
2371 struct ea_struct *eas,
2374 struct smb_create_returns *cr)
2376 TALLOC_CTX *frame = talloc_stackframe();
2377 struct tevent_context *ev;
2378 struct tevent_req *req;
2379 NTSTATUS status = NT_STATUS_NO_MEMORY;
2381 if (smbXcli_conn_has_async_calls(cli->conn)) {
2383 * Can't use sync call while an async call is in flight
2385 status = NT_STATUS_INVALID_PARAMETER;
2388 ev = samba_tevent_context_init(frame);
2392 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2393 DesiredAccess, FileAttributes,
2394 ShareAccess, CreateDisposition,
2395 CreateOptions, SecurityFlags,
2396 secdesc, eas, num_eas);
2400 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2403 status = cli_nttrans_create_recv(req, pfid, cr);
2409 /****************************************************************************
2411 WARNING: if you open with O_WRONLY then getattrE won't work!
2412 ****************************************************************************/
2414 struct cli_openx_state {
2421 static void cli_openx_done(struct tevent_req *subreq);
2423 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2424 struct tevent_context *ev,
2425 struct cli_state *cli, const char *fname,
2426 int flags, int share_mode,
2427 struct tevent_req **psmbreq)
2429 struct tevent_req *req, *subreq;
2430 struct cli_openx_state *state;
2432 unsigned accessmode;
2433 uint8_t additional_flags;
2434 uint16_t additional_flags2 = 0;
2437 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2443 if (flags & O_CREAT) {
2446 if (!(flags & O_EXCL)) {
2447 if (flags & O_TRUNC)
2453 accessmode = (share_mode<<4);
2455 if ((flags & O_ACCMODE) == O_RDWR) {
2457 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2462 if ((flags & O_SYNC) == O_SYNC) {
2463 accessmode |= (1<<14);
2467 if (share_mode == DENY_FCB) {
2471 SCVAL(state->vwv + 0, 0, 0xFF);
2472 SCVAL(state->vwv + 0, 1, 0);
2473 SSVAL(state->vwv + 1, 0, 0);
2474 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2475 SSVAL(state->vwv + 3, 0, accessmode);
2476 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2477 SSVAL(state->vwv + 5, 0, 0);
2478 SIVAL(state->vwv + 6, 0, 0);
2479 SSVAL(state->vwv + 8, 0, openfn);
2480 SIVAL(state->vwv + 9, 0, 0);
2481 SIVAL(state->vwv + 11, 0, 0);
2482 SIVAL(state->vwv + 13, 0, 0);
2484 additional_flags = 0;
2486 if (cli->use_oplocks) {
2487 /* if using oplocks then ask for a batch oplock via
2488 core and extended methods */
2490 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2491 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2494 bytes = talloc_array(state, uint8_t, 0);
2495 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2496 strlen(fname)+1, NULL);
2498 if (tevent_req_nomem(bytes, req)) {
2499 return tevent_req_post(req, ev);
2502 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2503 additional_flags2 = FLAGS2_REPARSE_PATH;
2506 state->bytes.iov_base = (void *)bytes;
2507 state->bytes.iov_len = talloc_get_size(bytes);
2509 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2510 additional_flags2, 15, state->vwv, 1, &state->bytes);
2511 if (subreq == NULL) {
2515 tevent_req_set_callback(subreq, cli_openx_done, req);
2520 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2521 struct cli_state *cli, const char *fname,
2522 int flags, int share_mode)
2524 struct tevent_req *req, *subreq;
2527 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2533 status = smb1cli_req_chain_submit(&subreq, 1);
2534 if (tevent_req_nterror(req, status)) {
2535 return tevent_req_post(req, ev);
2540 static void cli_openx_done(struct tevent_req *subreq)
2542 struct tevent_req *req = tevent_req_callback_data(
2543 subreq, struct tevent_req);
2544 struct cli_openx_state *state = tevent_req_data(
2545 req, struct cli_openx_state);
2550 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2552 TALLOC_FREE(subreq);
2553 if (tevent_req_nterror(req, status)) {
2556 state->fnum = SVAL(vwv+2, 0);
2557 tevent_req_done(req);
2560 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2562 struct cli_openx_state *state = tevent_req_data(
2563 req, struct cli_openx_state);
2566 if (tevent_req_is_nterror(req, &status)) {
2569 *pfnum = state->fnum;
2570 return NT_STATUS_OK;
2573 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2574 int share_mode, uint16_t *pfnum)
2576 TALLOC_CTX *frame = talloc_stackframe();
2577 struct tevent_context *ev;
2578 struct tevent_req *req;
2579 NTSTATUS status = NT_STATUS_NO_MEMORY;
2581 if (smbXcli_conn_has_async_calls(cli->conn)) {
2583 * Can't use sync call while an async call is in flight
2585 status = NT_STATUS_INVALID_PARAMETER;
2589 ev = samba_tevent_context_init(frame);
2594 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2599 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2603 status = cli_openx_recv(req, pfnum);
2608 /****************************************************************************
2609 Synchronous wrapper function that does an NtCreateX open by preference
2610 and falls back to openX if this fails.
2611 ****************************************************************************/
2613 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2614 int share_mode_in, uint16_t *pfnum)
2617 unsigned int openfn = 0;
2618 unsigned int dos_deny = 0;
2619 uint32_t access_mask, share_mode, create_disposition, create_options;
2620 struct smb_create_returns cr;
2622 /* Do the initial mapping into OpenX parameters. */
2623 if (flags & O_CREAT) {
2626 if (!(flags & O_EXCL)) {
2627 if (flags & O_TRUNC)
2633 dos_deny = (share_mode_in<<4);
2635 if ((flags & O_ACCMODE) == O_RDWR) {
2637 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2642 if ((flags & O_SYNC) == O_SYNC) {
2643 dos_deny |= (1<<14);
2647 if (share_mode_in == DENY_FCB) {
2652 /* Hmmm. This is what I think the above code
2653 should look like if it's using the constants
2656 if (flags & O_CREAT) {
2657 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2659 if (!(flags & O_EXCL)) {
2660 if (flags & O_TRUNC)
2661 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2663 openfn |= OPENX_FILE_EXISTS_OPEN;
2666 dos_deny = SET_DENY_MODE(share_mode_in);
2668 if ((flags & O_ACCMODE) == O_RDWR) {
2669 dos_deny |= DOS_OPEN_RDWR;
2670 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2671 dos_deny |= DOS_OPEN_WRONLY;
2675 if ((flags & O_SYNC) == O_SYNC) {
2676 dos_deny |= FILE_SYNC_OPENMODE;
2680 if (share_mode_in == DENY_FCB) {
2685 if (!map_open_params_to_ntcreate(fname, dos_deny,
2686 openfn, &access_mask,
2687 &share_mode, &create_disposition,
2688 &create_options, NULL)) {
2692 status = cli_ntcreate(cli,
2704 /* Try and cope will all varients of "we don't do this call"
2705 and fall back to openX. */
2707 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2708 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2709 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2710 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2711 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2712 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2713 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2714 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2715 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2719 if (NT_STATUS_IS_OK(status) &&
2720 (create_options & FILE_NON_DIRECTORY_FILE) &&
2721 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2724 * Some (broken) servers return a valid handle
2725 * for directories even if FILE_NON_DIRECTORY_FILE
2726 * is set. Just close the handle and set the
2727 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2729 status = cli_close(cli, *pfnum);
2730 if (!NT_STATUS_IS_OK(status)) {
2733 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2734 /* Set this so libsmbclient can retrieve it. */
2735 cli->raw_status = status;
2742 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2745 /****************************************************************************
2747 ****************************************************************************/
2749 struct cli_close_state {
2753 static void cli_close_done(struct tevent_req *subreq);
2755 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2756 struct tevent_context *ev,
2757 struct cli_state *cli,
2759 struct tevent_req **psubreq)
2761 struct tevent_req *req, *subreq;
2762 struct cli_close_state *state;
2764 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2769 SSVAL(state->vwv+0, 0, fnum);
2770 SIVALS(state->vwv+1, 0, -1);
2772 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
2773 3, state->vwv, 0, NULL);
2774 if (subreq == NULL) {
2778 tevent_req_set_callback(subreq, cli_close_done, req);
2783 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2784 struct tevent_context *ev,
2785 struct cli_state *cli,
2788 struct tevent_req *req, *subreq;
2791 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2796 status = smb1cli_req_chain_submit(&subreq, 1);
2797 if (tevent_req_nterror(req, status)) {
2798 return tevent_req_post(req, ev);
2803 static void cli_close_done(struct tevent_req *subreq)
2805 struct tevent_req *req = tevent_req_callback_data(
2806 subreq, struct tevent_req);
2809 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2810 TALLOC_FREE(subreq);
2811 if (tevent_req_nterror(req, status)) {
2814 tevent_req_done(req);
2817 NTSTATUS cli_close_recv(struct tevent_req *req)
2819 return tevent_req_simple_recv_ntstatus(req);
2822 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2824 TALLOC_CTX *frame = NULL;
2825 struct tevent_context *ev;
2826 struct tevent_req *req;
2827 NTSTATUS status = NT_STATUS_OK;
2829 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2830 return cli_smb2_close_fnum(cli, fnum);
2833 frame = talloc_stackframe();
2835 if (smbXcli_conn_has_async_calls(cli->conn)) {
2837 * Can't use sync call while an async call is in flight
2839 status = NT_STATUS_INVALID_PARAMETER;
2843 ev = samba_tevent_context_init(frame);
2845 status = NT_STATUS_NO_MEMORY;
2849 req = cli_close_send(frame, ev, cli, fnum);
2851 status = NT_STATUS_NO_MEMORY;
2855 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2859 status = cli_close_recv(req);
2865 /****************************************************************************
2866 Truncate a file to a specified size
2867 ****************************************************************************/
2869 struct ftrunc_state {
2875 static void cli_ftruncate_done(struct tevent_req *subreq)
2877 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2878 NULL, 0, NULL, NULL, 0, NULL);
2879 tevent_req_simple_finish_ntstatus(subreq, status);
2882 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2883 struct tevent_context *ev,
2884 struct cli_state *cli,
2888 struct tevent_req *req = NULL, *subreq = NULL;
2889 struct ftrunc_state *state = NULL;
2891 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2896 /* Setup setup word. */
2897 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2899 /* Setup param array. */
2900 SSVAL(state->param,0,fnum);
2901 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2902 SSVAL(state->param,4,0);
2904 /* Setup data array. */
2905 SBVAL(state->data, 0, size);
2907 subreq = cli_trans_send(state, /* mem ctx. */
2908 ev, /* event ctx. */
2909 cli, /* cli_state. */
2910 0, /* additional_flags2 */
2911 SMBtrans2, /* cmd. */
2912 NULL, /* pipe name. */
2916 &state->setup, /* setup. */
2917 1, /* num setup uint16_t words. */
2918 0, /* max returned setup. */
2919 state->param, /* param. */
2921 2, /* max returned param. */
2922 state->data, /* data. */
2924 0); /* max returned data. */
2926 if (tevent_req_nomem(subreq, req)) {
2927 return tevent_req_post(req, ev);
2929 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2933 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2935 return tevent_req_simple_recv_ntstatus(req);
2938 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2940 TALLOC_CTX *frame = NULL;
2941 struct tevent_context *ev = NULL;
2942 struct tevent_req *req = NULL;
2943 NTSTATUS status = NT_STATUS_OK;
2945 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2946 return cli_smb2_ftruncate(cli, fnum, size);
2949 frame = talloc_stackframe();
2951 if (smbXcli_conn_has_async_calls(cli->conn)) {
2953 * Can't use sync call while an async call is in flight
2955 status = NT_STATUS_INVALID_PARAMETER;
2959 ev = samba_tevent_context_init(frame);
2961 status = NT_STATUS_NO_MEMORY;
2965 req = cli_ftruncate_send(frame,
2971 status = NT_STATUS_NO_MEMORY;
2975 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2979 status = cli_ftruncate_recv(req);
2986 /****************************************************************************
2987 send a lock with a specified locktype
2988 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2989 ****************************************************************************/
2991 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2992 uint32_t offset, uint32_t len,
2993 int timeout, unsigned char locktype)
2998 unsigned int set_timeout = 0;
2999 unsigned int saved_timeout = 0;
3001 SCVAL(vwv + 0, 0, 0xff);
3002 SCVAL(vwv + 0, 1, 0);
3003 SSVAL(vwv + 1, 0, 0);
3004 SSVAL(vwv + 2, 0, fnum);
3005 SCVAL(vwv + 3, 0, locktype);
3006 SCVAL(vwv + 3, 1, 0);
3007 SIVALS(vwv + 4, 0, timeout);
3008 SSVAL(vwv + 6, 0, 0);
3009 SSVAL(vwv + 7, 0, 1);
3011 SSVAL(bytes, 0, cli_getpid(cli));
3012 SIVAL(bytes, 2, offset);
3013 SIVAL(bytes, 6, len);
3016 if (timeout == -1) {
3017 set_timeout = 0x7FFFFFFF;
3019 set_timeout = timeout + 2*1000;
3021 saved_timeout = cli_set_timeout(cli, set_timeout);
3024 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3025 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3027 if (saved_timeout != 0) {
3028 cli_set_timeout(cli, saved_timeout);
3034 /****************************************************************************
3036 note that timeout is in units of 2 milliseconds
3037 ****************************************************************************/
3039 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3040 uint32_t offset, uint32_t len, int timeout,
3041 enum brl_type lock_type)
3045 status = cli_locktype(cli, fnum, offset, len, timeout,
3046 (lock_type == READ_LOCK? 1 : 0));
3050 /****************************************************************************
3052 ****************************************************************************/
3054 struct cli_unlock_state {
3059 static void cli_unlock_done(struct tevent_req *subreq);
3061 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3062 struct tevent_context *ev,
3063 struct cli_state *cli,
3069 struct tevent_req *req = NULL, *subreq = NULL;
3070 struct cli_unlock_state *state = NULL;
3071 uint8_t additional_flags = 0;
3073 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3078 SCVAL(state->vwv+0, 0, 0xFF);
3079 SSVAL(state->vwv+2, 0, fnum);
3080 SCVAL(state->vwv+3, 0, 0);
3081 SIVALS(state->vwv+4, 0, 0);
3082 SSVAL(state->vwv+6, 0, 1);
3083 SSVAL(state->vwv+7, 0, 0);
3085 SSVAL(state->data, 0, cli_getpid(cli));
3086 SIVAL(state->data, 2, offset);
3087 SIVAL(state->data, 6, len);
3089 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3090 8, state->vwv, 10, state->data);
3091 if (tevent_req_nomem(subreq, req)) {
3092 return tevent_req_post(req, ev);
3094 tevent_req_set_callback(subreq, cli_unlock_done, req);
3098 static void cli_unlock_done(struct tevent_req *subreq)
3100 struct tevent_req *req = tevent_req_callback_data(
3101 subreq, struct tevent_req);
3104 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3105 TALLOC_FREE(subreq);
3106 if (tevent_req_nterror(req, status)) {
3109 tevent_req_done(req);
3112 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3114 return tevent_req_simple_recv_ntstatus(req);
3117 NTSTATUS cli_unlock(struct cli_state *cli,
3122 TALLOC_CTX *frame = talloc_stackframe();
3123 struct tevent_context *ev;
3124 struct tevent_req *req;
3125 NTSTATUS status = NT_STATUS_OK;
3127 if (smbXcli_conn_has_async_calls(cli->conn)) {
3129 * Can't use sync call while an async call is in flight
3131 status = NT_STATUS_INVALID_PARAMETER;
3135 ev = samba_tevent_context_init(frame);
3137 status = NT_STATUS_NO_MEMORY;
3141 req = cli_unlock_send(frame, ev, cli,
3144 status = NT_STATUS_NO_MEMORY;
3148 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3152 status = cli_unlock_recv(req);
3159 /****************************************************************************
3160 Lock a file with 64 bit offsets.
3161 ****************************************************************************/
3163 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3164 uint64_t offset, uint64_t len, int timeout,
3165 enum brl_type lock_type)
3169 unsigned int set_timeout = 0;
3170 unsigned int saved_timeout = 0;
3174 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3175 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3178 ltype = (lock_type == READ_LOCK? 1 : 0);
3179 ltype |= LOCKING_ANDX_LARGE_FILES;
3181 SCVAL(vwv + 0, 0, 0xff);
3182 SCVAL(vwv + 0, 1, 0);
3183 SSVAL(vwv + 1, 0, 0);
3184 SSVAL(vwv + 2, 0, fnum);
3185 SCVAL(vwv + 3, 0, ltype);
3186 SCVAL(vwv + 3, 1, 0);
3187 SIVALS(vwv + 4, 0, timeout);
3188 SSVAL(vwv + 6, 0, 0);
3189 SSVAL(vwv + 7, 0, 1);
3191 SIVAL(bytes, 0, cli_getpid(cli));
3192 SOFF_T_R(bytes, 4, offset);
3193 SOFF_T_R(bytes, 12, len);
3196 if (timeout == -1) {
3197 set_timeout = 0x7FFFFFFF;
3199 set_timeout = timeout + 2*1000;
3201 saved_timeout = cli_set_timeout(cli, set_timeout);
3204 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3205 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3207 if (saved_timeout != 0) {
3208 cli_set_timeout(cli, saved_timeout);
3214 /****************************************************************************
3215 Unlock a file with 64 bit offsets.
3216 ****************************************************************************/
3218 struct cli_unlock64_state {
3223 static void cli_unlock64_done(struct tevent_req *subreq);
3225 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3226 struct tevent_context *ev,
3227 struct cli_state *cli,
3233 struct tevent_req *req = NULL, *subreq = NULL;
3234 struct cli_unlock64_state *state = NULL;
3235 uint8_t additional_flags = 0;
3237 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3242 SCVAL(state->vwv+0, 0, 0xff);
3243 SSVAL(state->vwv+2, 0, fnum);
3244 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3245 SIVALS(state->vwv+4, 0, 0);
3246 SSVAL(state->vwv+6, 0, 1);
3247 SSVAL(state->vwv+7, 0, 0);
3249 SIVAL(state->data, 0, cli_getpid(cli));
3250 SOFF_T_R(state->data, 4, offset);
3251 SOFF_T_R(state->data, 12, len);
3253 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3254 8, state->vwv, 20, state->data);
3255 if (tevent_req_nomem(subreq, req)) {
3256 return tevent_req_post(req, ev);
3258 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3262 static void cli_unlock64_done(struct tevent_req *subreq)
3264 struct tevent_req *req = tevent_req_callback_data(
3265 subreq, struct tevent_req);
3268 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3269 TALLOC_FREE(subreq);
3270 if (tevent_req_nterror(req, status)) {
3273 tevent_req_done(req);
3276 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3278 return tevent_req_simple_recv_ntstatus(req);
3281 NTSTATUS cli_unlock64(struct cli_state *cli,
3286 TALLOC_CTX *frame = talloc_stackframe();
3287 struct tevent_context *ev;
3288 struct tevent_req *req;
3289 NTSTATUS status = NT_STATUS_OK;
3291 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3292 return cli_unlock(cli, fnum, offset, len);
3295 if (smbXcli_conn_has_async_calls(cli->conn)) {
3297 * Can't use sync call while an async call is in flight
3299 status = NT_STATUS_INVALID_PARAMETER;
3303 ev = samba_tevent_context_init(frame);
3305 status = NT_STATUS_NO_MEMORY;
3309 req = cli_unlock64_send(frame, ev, cli,
3312 status = NT_STATUS_NO_MEMORY;
3316 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3320 status = cli_unlock64_recv(req);
3327 /****************************************************************************
3328 Get/unlock a POSIX lock on a file - internal function.
3329 ****************************************************************************/
3331 struct posix_lock_state {
3334 uint8_t data[POSIX_LOCK_DATA_SIZE];
3337 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3339 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3340 NULL, 0, NULL, NULL, 0, NULL);
3341 tevent_req_simple_finish_ntstatus(subreq, status);
3344 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3345 struct tevent_context *ev,
3346 struct cli_state *cli,
3351 enum brl_type lock_type)
3353 struct tevent_req *req = NULL, *subreq = NULL;
3354 struct posix_lock_state *state = NULL;
3356 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3361 /* Setup setup word. */
3362 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3364 /* Setup param array. */
3365 SSVAL(&state->param, 0, fnum);
3366 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3368 /* Setup data array. */
3369 switch (lock_type) {
3371 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3372 POSIX_LOCK_TYPE_READ);
3375 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3376 POSIX_LOCK_TYPE_WRITE);
3379 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3380 POSIX_LOCK_TYPE_UNLOCK);
3387 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3388 POSIX_LOCK_FLAG_WAIT);
3390 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3391 POSIX_LOCK_FLAG_NOWAIT);
3394 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3395 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3396 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3398 subreq = cli_trans_send(state, /* mem ctx. */
3399 ev, /* event ctx. */
3400 cli, /* cli_state. */
3401 0, /* additional_flags2 */
3402 SMBtrans2, /* cmd. */
3403 NULL, /* pipe name. */
3407 &state->setup, /* setup. */
3408 1, /* num setup uint16_t words. */
3409 0, /* max returned setup. */
3410 state->param, /* param. */
3412 2, /* max returned param. */
3413 state->data, /* data. */
3414 POSIX_LOCK_DATA_SIZE, /* num data. */
3415 0); /* max returned data. */
3417 if (tevent_req_nomem(subreq, req)) {
3418 return tevent_req_post(req, ev);
3420 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3424 /****************************************************************************
3426 ****************************************************************************/
3428 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3429 struct tevent_context *ev,
3430 struct cli_state *cli,
3435 enum brl_type lock_type)
3437 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3438 wait_lock, lock_type);
3441 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3443 return tevent_req_simple_recv_ntstatus(req);
3446 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3447 uint64_t offset, uint64_t len,
3448 bool wait_lock, enum brl_type lock_type)
3450 TALLOC_CTX *frame = talloc_stackframe();
3451 struct tevent_context *ev = NULL;
3452 struct tevent_req *req = NULL;
3453 NTSTATUS status = NT_STATUS_OK;
3455 if (smbXcli_conn_has_async_calls(cli->conn)) {
3457 * Can't use sync call while an async call is in flight
3459 status = NT_STATUS_INVALID_PARAMETER;
3463 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3464 status = NT_STATUS_INVALID_PARAMETER;
3468 ev = samba_tevent_context_init(frame);
3470 status = NT_STATUS_NO_MEMORY;
3474 req = cli_posix_lock_send(frame,
3483 status = NT_STATUS_NO_MEMORY;
3487 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3491 status = cli_posix_lock_recv(req);
3498 /****************************************************************************
3499 POSIX Unlock a file.
3500 ****************************************************************************/
3502 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3503 struct tevent_context *ev,
3504 struct cli_state *cli,
3509 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3510 false, UNLOCK_LOCK);
3513 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3515 return tevent_req_simple_recv_ntstatus(req);
3518 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3520 TALLOC_CTX *frame = talloc_stackframe();
3521 struct tevent_context *ev = NULL;
3522 struct tevent_req *req = NULL;
3523 NTSTATUS status = NT_STATUS_OK;
3525 if (smbXcli_conn_has_async_calls(cli->conn)) {
3527 * Can't use sync call while an async call is in flight
3529 status = NT_STATUS_INVALID_PARAMETER;
3533 ev = samba_tevent_context_init(frame);
3535 status = NT_STATUS_NO_MEMORY;
3539 req = cli_posix_unlock_send(frame,
3546 status = NT_STATUS_NO_MEMORY;
3550 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3554 status = cli_posix_unlock_recv(req);
3561 /****************************************************************************
3562 Do a SMBgetattrE call.
3563 ****************************************************************************/
3565 static void cli_getattrE_done(struct tevent_req *subreq);
3567 struct cli_getattrE_state {
3577 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3578 struct tevent_context *ev,
3579 struct cli_state *cli,
3582 struct tevent_req *req = NULL, *subreq = NULL;
3583 struct cli_getattrE_state *state = NULL;
3584 uint8_t additional_flags = 0;
3586 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3591 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3592 SSVAL(state->vwv+0,0,fnum);
3594 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3595 1, state->vwv, 0, NULL);
3596 if (tevent_req_nomem(subreq, req)) {
3597 return tevent_req_post(req, ev);
3599 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3603 static void cli_getattrE_done(struct tevent_req *subreq)
3605 struct tevent_req *req = tevent_req_callback_data(
3606 subreq, struct tevent_req);
3607 struct cli_getattrE_state *state = tevent_req_data(
3608 req, struct cli_getattrE_state);
3610 uint16_t *vwv = NULL;
3613 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3615 TALLOC_FREE(subreq);
3616 if (tevent_req_nterror(req, status)) {
3620 state->size = (off_t)IVAL(vwv+6,0);
3621 state->attr = SVAL(vwv+10,0);
3622 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3623 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3624 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3626 tevent_req_done(req);
3629 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3632 time_t *change_time,
3633 time_t *access_time,
3636 struct cli_getattrE_state *state = tevent_req_data(
3637 req, struct cli_getattrE_state);
3640 if (tevent_req_is_nterror(req, &status)) {
3644 *attr = state->attr;
3647 *size = state->size;
3650 *change_time = state->change_time;
3653 *access_time = state->access_time;
3656 *write_time = state->write_time;
3658 return NT_STATUS_OK;
3661 NTSTATUS cli_getattrE(struct cli_state *cli,
3665 time_t *change_time,
3666 time_t *access_time,
3669 TALLOC_CTX *frame = NULL;
3670 struct tevent_context *ev = NULL;
3671 struct tevent_req *req = NULL;
3672 NTSTATUS status = NT_STATUS_OK;
3674 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3675 return cli_smb2_getattrE(cli,
3684 frame = talloc_stackframe();
3686 if (smbXcli_conn_has_async_calls(cli->conn)) {
3688 * Can't use sync call while an async call is in flight
3690 status = NT_STATUS_INVALID_PARAMETER;
3694 ev = samba_tevent_context_init(frame);
3696 status = NT_STATUS_NO_MEMORY;
3700 req = cli_getattrE_send(frame, ev, cli, fnum);
3702 status = NT_STATUS_NO_MEMORY;
3706 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3710 status = cli_getattrE_recv(req,
3722 /****************************************************************************
3724 ****************************************************************************/
3726 static void cli_getatr_done(struct tevent_req *subreq);
3728 struct cli_getatr_state {
3735 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3736 struct tevent_context *ev,
3737 struct cli_state *cli,
3740 struct tevent_req *req = NULL, *subreq = NULL;
3741 struct cli_getatr_state *state = NULL;
3742 uint8_t additional_flags = 0;
3743 uint16_t additional_flags2 = 0;
3744 uint8_t *bytes = NULL;
3746 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3751 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3753 bytes = talloc_array(state, uint8_t, 1);
3754 if (tevent_req_nomem(bytes, req)) {
3755 return tevent_req_post(req, ev);
3758 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3759 strlen(fname)+1, NULL);
3761 if (tevent_req_nomem(bytes, req)) {
3762 return tevent_req_post(req, ev);
3765 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
3766 additional_flags2 = FLAGS2_REPARSE_PATH;
3769 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3771 0, NULL, talloc_get_size(bytes), bytes);
3772 if (tevent_req_nomem(subreq, req)) {
3773 return tevent_req_post(req, ev);
3775 tevent_req_set_callback(subreq, cli_getatr_done, req);
3779 static void cli_getatr_done(struct tevent_req *subreq)
3781 struct tevent_req *req = tevent_req_callback_data(
3782 subreq, struct tevent_req);
3783 struct cli_getatr_state *state = tevent_req_data(
3784 req, struct cli_getatr_state);
3786 uint16_t *vwv = NULL;
3789 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3791 TALLOC_FREE(subreq);
3792 if (tevent_req_nterror(req, status)) {
3796 state->attr = SVAL(vwv+0,0);
3797 state->size = (off_t)IVAL(vwv+3,0);
3798 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3800 tevent_req_done(req);
3803 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3808 struct cli_getatr_state *state = tevent_req_data(
3809 req, struct cli_getatr_state);
3812 if (tevent_req_is_nterror(req, &status)) {
3816 *attr = state->attr;
3819 *size = state->size;
3822 *write_time = state->write_time;
3824 return NT_STATUS_OK;
3827 NTSTATUS cli_getatr(struct cli_state *cli,
3833 TALLOC_CTX *frame = NULL;
3834 struct tevent_context *ev = NULL;
3835 struct tevent_req *req = NULL;
3836 NTSTATUS status = NT_STATUS_OK;
3838 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3839 return cli_smb2_getatr(cli,
3846 frame = talloc_stackframe();
3848 if (smbXcli_conn_has_async_calls(cli->conn)) {
3850 * Can't use sync call while an async call is in flight
3852 status = NT_STATUS_INVALID_PARAMETER;
3856 ev = samba_tevent_context_init(frame);
3858 status = NT_STATUS_NO_MEMORY;
3862 req = cli_getatr_send(frame, ev, cli, fname);
3864 status = NT_STATUS_NO_MEMORY;
3868 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3872 status = cli_getatr_recv(req,
3882 /****************************************************************************
3883 Do a SMBsetattrE call.
3884 ****************************************************************************/
3886 static void cli_setattrE_done(struct tevent_req *subreq);
3888 struct cli_setattrE_state {
3892 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3893 struct tevent_context *ev,
3894 struct cli_state *cli,
3900 struct tevent_req *req = NULL, *subreq = NULL;
3901 struct cli_setattrE_state *state = NULL;
3902 uint8_t additional_flags = 0;
3904 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3909 SSVAL(state->vwv+0, 0, fnum);
3910 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3911 smb1cli_conn_server_time_zone(cli->conn));
3912 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3913 smb1cli_conn_server_time_zone(cli->conn));
3914 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3915 smb1cli_conn_server_time_zone(cli->conn));
3917 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3918 7, state->vwv, 0, NULL);
3919 if (tevent_req_nomem(subreq, req)) {
3920 return tevent_req_post(req, ev);
3922 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3926 static void cli_setattrE_done(struct tevent_req *subreq)
3928 struct tevent_req *req = tevent_req_callback_data(
3929 subreq, struct tevent_req);
3932 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3933 TALLOC_FREE(subreq);
3934 if (tevent_req_nterror(req, status)) {
3937 tevent_req_done(req);
3940 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3942 return tevent_req_simple_recv_ntstatus(req);
3945 NTSTATUS cli_setattrE(struct cli_state *cli,
3951 TALLOC_CTX *frame = NULL;
3952 struct tevent_context *ev = NULL;
3953 struct tevent_req *req = NULL;
3954 NTSTATUS status = NT_STATUS_OK;
3956 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3957 return cli_smb2_setattrE(cli,
3964 frame = talloc_stackframe();
3966 if (smbXcli_conn_has_async_calls(cli->conn)) {
3968 * Can't use sync call while an async call is in flight
3970 status = NT_STATUS_INVALID_PARAMETER;
3974 ev = samba_tevent_context_init(frame);
3976 status = NT_STATUS_NO_MEMORY;
3980 req = cli_setattrE_send(frame, ev,
3988 status = NT_STATUS_NO_MEMORY;
3992 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3996 status = cli_setattrE_recv(req);
4003 /****************************************************************************
4004 Do a SMBsetatr call.
4005 ****************************************************************************/
4007 static void cli_setatr_done(struct tevent_req *subreq);
4009 struct cli_setatr_state {
4013 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4014 struct tevent_context *ev,
4015 struct cli_state *cli,
4020 struct tevent_req *req = NULL, *subreq = NULL;
4021 struct cli_setatr_state *state = NULL;
4022 uint8_t additional_flags = 0;
4023 uint16_t additional_flags2 = 0;
4024 uint8_t *bytes = NULL;
4026 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4031 SSVAL(state->vwv+0, 0, attr);
4032 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4034 bytes = talloc_array(state, uint8_t, 1);
4035 if (tevent_req_nomem(bytes, req)) {
4036 return tevent_req_post(req, ev);
4039 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4040 strlen(fname)+1, NULL);
4041 if (tevent_req_nomem(bytes, req)) {
4042 return tevent_req_post(req, ev);
4044 bytes = talloc_realloc(state, bytes, uint8_t,
4045 talloc_get_size(bytes)+1);
4046 if (tevent_req_nomem(bytes, req)) {
4047 return tevent_req_post(req, ev);
4050 bytes[talloc_get_size(bytes)-1] = 4;
4051 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4053 if (tevent_req_nomem(bytes, req)) {
4054 return tevent_req_post(req, ev);
4057 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4058 additional_flags2 = FLAGS2_REPARSE_PATH;
4061 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
4063 8, state->vwv, talloc_get_size(bytes), bytes);
4064 if (tevent_req_nomem(subreq, req)) {
4065 return tevent_req_post(req, ev);
4067 tevent_req_set_callback(subreq, cli_setatr_done, req);
4071 static void cli_setatr_done(struct tevent_req *subreq)
4073 struct tevent_req *req = tevent_req_callback_data(
4074 subreq, struct tevent_req);
4077 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4078 TALLOC_FREE(subreq);
4079 if (tevent_req_nterror(req, status)) {
4082 tevent_req_done(req);
4085 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4087 return tevent_req_simple_recv_ntstatus(req);
4090 NTSTATUS cli_setatr(struct cli_state *cli,
4095 TALLOC_CTX *frame = NULL;
4096 struct tevent_context *ev = NULL;
4097 struct tevent_req *req = NULL;
4098 NTSTATUS status = NT_STATUS_OK;
4100 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4101 return cli_smb2_setatr(cli,
4107 frame = talloc_stackframe();
4109 if (smbXcli_conn_has_async_calls(cli->conn)) {
4111 * Can't use sync call while an async call is in flight
4113 status = NT_STATUS_INVALID_PARAMETER;
4117 ev = samba_tevent_context_init(frame);
4119 status = NT_STATUS_NO_MEMORY;
4123 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4125 status = NT_STATUS_NO_MEMORY;
4129 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4133 status = cli_setatr_recv(req);
4140 /****************************************************************************
4141 Check for existence of a dir.
4142 ****************************************************************************/
4144 static void cli_chkpath_done(struct tevent_req *subreq);
4146 struct cli_chkpath_state {
4150 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4151 struct tevent_context *ev,
4152 struct cli_state *cli,
4155 struct tevent_req *req = NULL, *subreq = NULL;
4156 struct cli_chkpath_state *state = NULL;
4157 uint8_t additional_flags = 0;
4158 uint16_t additional_flags2 = 0;
4159 uint8_t *bytes = NULL;
4161 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4166 bytes = talloc_array(state, uint8_t, 1);
4167 if (tevent_req_nomem(bytes, req)) {
4168 return tevent_req_post(req, ev);
4171 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4172 strlen(fname)+1, NULL);
4174 if (tevent_req_nomem(bytes, req)) {
4175 return tevent_req_post(req, ev);
4178 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4179 additional_flags2 = FLAGS2_REPARSE_PATH;
4182 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4184 0, NULL, talloc_get_size(bytes), bytes);
4185 if (tevent_req_nomem(subreq, req)) {
4186 return tevent_req_post(req, ev);
4188 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4192 static void cli_chkpath_done(struct tevent_req *subreq)
4194 struct tevent_req *req = tevent_req_callback_data(
4195 subreq, struct tevent_req);
4198 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4199 TALLOC_FREE(subreq);
4200 if (tevent_req_nterror(req, status)) {
4203 tevent_req_done(req);
4206 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4208 return tevent_req_simple_recv_ntstatus(req);
4211 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4213 TALLOC_CTX *frame = talloc_stackframe();
4214 struct tevent_context *ev = NULL;
4215 struct tevent_req *req = NULL;
4217 NTSTATUS status = NT_STATUS_OK;
4219 if (smbXcli_conn_has_async_calls(cli->conn)) {
4221 * Can't use sync call while an async call is in flight
4223 status = NT_STATUS_INVALID_PARAMETER;
4227 path2 = talloc_strdup(frame, path);
4229 status = NT_STATUS_NO_MEMORY;
4232 trim_char(path2,'\0','\\');
4234 path2 = talloc_strdup(frame, "\\");
4236 status = NT_STATUS_NO_MEMORY;
4241 ev = samba_tevent_context_init(frame);
4243 status = NT_STATUS_NO_MEMORY;
4247 req = cli_chkpath_send(frame, ev, cli, path2);
4249 status = NT_STATUS_NO_MEMORY;
4253 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4257 status = cli_chkpath_recv(req);
4264 /****************************************************************************
4266 ****************************************************************************/
4268 static void cli_dskattr_done(struct tevent_req *subreq);
4270 struct cli_dskattr_state {
4276 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4277 struct tevent_context *ev,
4278 struct cli_state *cli)
4280 struct tevent_req *req = NULL, *subreq = NULL;
4281 struct cli_dskattr_state *state = NULL;
4282 uint8_t additional_flags = 0;
4284 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4289 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4291 if (tevent_req_nomem(subreq, req)) {
4292 return tevent_req_post(req, ev);
4294 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4298 static void cli_dskattr_done(struct tevent_req *subreq)
4300 struct tevent_req *req = tevent_req_callback_data(
4301 subreq, struct tevent_req);
4302 struct cli_dskattr_state *state = tevent_req_data(
4303 req, struct cli_dskattr_state);
4305 uint16_t *vwv = NULL;
4308 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4310 TALLOC_FREE(subreq);
4311 if (tevent_req_nterror(req, status)) {
4314 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4315 state->total = SVAL(vwv+0, 0);
4316 state->avail = SVAL(vwv+3, 0);
4317 tevent_req_done(req);
4320 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4322 struct cli_dskattr_state *state = tevent_req_data(
4323 req, struct cli_dskattr_state);
4326 if (tevent_req_is_nterror(req, &status)) {
4329 *bsize = state->bsize;
4330 *total = state->total;
4331 *avail = state->avail;
4332 return NT_STATUS_OK;
4335 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4337 TALLOC_CTX *frame = NULL;
4338 struct tevent_context *ev = NULL;
4339 struct tevent_req *req = NULL;
4340 NTSTATUS status = NT_STATUS_OK;
4342 frame = talloc_stackframe();
4344 if (smbXcli_conn_has_async_calls(cli->conn)) {
4346 * Can't use sync call while an async call is in flight
4348 status = NT_STATUS_INVALID_PARAMETER;
4352 ev = samba_tevent_context_init(frame);
4354 status = NT_STATUS_NO_MEMORY;
4358 req = cli_dskattr_send(frame, ev, cli);
4360 status = NT_STATUS_NO_MEMORY;
4364 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4368 status = cli_dskattr_recv(req, bsize, total, avail);
4375 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4376 uint64_t *total, uint64_t *avail)
4378 uint64_t sectors_per_block;
4379 uint64_t bytes_per_sector;
4380 int old_bsize, old_total, old_avail;
4383 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4384 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4388 * Try the trans2 disk full size info call first.
4389 * We already use this in SMBC_fstatvfs_ctx().
4390 * Ignore 'actual_available_units' as we only
4391 * care about the quota for the caller.
4394 status = cli_get_fs_full_size_info(cli,
4401 /* Try and cope will all varients of "we don't do this call"
4402 and fall back to cli_dskattr. */
4404 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4405 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4406 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4407 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4408 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4409 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4410 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4411 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4412 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4413 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4417 if (!NT_STATUS_IS_OK(status)) {
4422 *bsize = sectors_per_block *
4426 return NT_STATUS_OK;
4430 /* Old SMB1 core protocol fallback. */
4431 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4432 if (!NT_STATUS_IS_OK(status)) {
4436 *bsize = (uint64_t)old_bsize;
4439 *total = (uint64_t)old_total;
4442 *avail = (uint64_t)old_avail;
4444 return NT_STATUS_OK;
4447 /****************************************************************************
4448 Create and open a temporary file.
4449 ****************************************************************************/
4451 static void cli_ctemp_done(struct tevent_req *subreq);
4453 struct ctemp_state {
4459 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4460 struct tevent_context *ev,
4461 struct cli_state *cli,
4464 struct tevent_req *req = NULL, *subreq = NULL;
4465 struct ctemp_state *state = NULL;
4466 uint8_t additional_flags = 0;
4467 uint16_t additional_flags2 = 0;
4468 uint8_t *bytes = NULL;
4470 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4475 SSVAL(state->vwv,0,0);
4476 SIVALS(state->vwv+1,0,-1);
4478 bytes = talloc_array(state, uint8_t, 1);
4479 if (tevent_req_nomem(bytes, req)) {
4480 return tevent_req_post(req, ev);
4483 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4484 strlen(path)+1, NULL);
4485 if (tevent_req_nomem(bytes, req)) {
4486 return tevent_req_post(req, ev);
4489 if (clistr_is_previous_version_path(path, NULL, NULL, NULL)) {
4490 additional_flags2 = FLAGS2_REPARSE_PATH;
4493 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4495 3, state->vwv, talloc_get_size(bytes), bytes);
4496 if (tevent_req_nomem(subreq, req)) {
4497 return tevent_req_post(req, ev);
4499 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4503 static void cli_ctemp_done(struct tevent_req *subreq)
4505 struct tevent_req *req = tevent_req_callback_data(
4506 subreq, struct tevent_req);
4507 struct ctemp_state *state = tevent_req_data(
4508 req, struct ctemp_state);
4512 uint32_t num_bytes = 0;
4513 uint8_t *bytes = NULL;
4515 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4516 &num_bytes, &bytes);
4517 TALLOC_FREE(subreq);
4518 if (tevent_req_nterror(req, status)) {
4522 state->fnum = SVAL(vwv+0, 0);
4524 /* From W2K3, the result is just the ASCII name */
4525 if (num_bytes < 2) {
4526 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4530 if (pull_string_talloc(state,
4537 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4540 tevent_req_done(req);
4543 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4548 struct ctemp_state *state = tevent_req_data(req,
4549 struct ctemp_state);
4552 if (tevent_req_is_nterror(req, &status)) {
4555 *pfnum = state->fnum;
4556 *outfile = talloc_strdup(ctx, state->ret_path);
4558 return NT_STATUS_NO_MEMORY;
4560 return NT_STATUS_OK;
4563 NTSTATUS cli_ctemp(struct cli_state *cli,
4569 TALLOC_CTX *frame = talloc_stackframe();
4570 struct tevent_context *ev;
4571 struct tevent_req *req;
4572 NTSTATUS status = NT_STATUS_OK;
4574 if (smbXcli_conn_has_async_calls(cli->conn)) {
4576 * Can't use sync call while an async call is in flight
4578 status = NT_STATUS_INVALID_PARAMETER;
4582 ev = samba_tevent_context_init(frame);
4584 status = NT_STATUS_NO_MEMORY;
4588 req = cli_ctemp_send(frame, ev, cli, path);
4590 status = NT_STATUS_NO_MEMORY;
4594 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4598 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4606 send a raw ioctl - used by the torture code
4608 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4613 SSVAL(vwv+0, 0, fnum);
4614 SSVAL(vwv+1, 0, code>>16);
4615 SSVAL(vwv+2, 0, (code&0xFFFF));
4617 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4618 NULL, 0, NULL, NULL, NULL, NULL);
4619 if (!NT_STATUS_IS_OK(status)) {
4622 *blob = data_blob_null;
4623 return NT_STATUS_OK;
4626 /*********************************************************
4627 Set an extended attribute utility fn.
4628 *********************************************************/
4630 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4631 uint8_t *param, unsigned int param_len,
4632 const char *ea_name,
4633 const char *ea_val, size_t ea_len)
4636 unsigned int data_len = 0;
4637 uint8_t *data = NULL;
4639 size_t ea_namelen = strlen(ea_name);
4642 SSVAL(setup, 0, setup_val);
4644 if (ea_namelen == 0 && ea_len == 0) {
4646 data = talloc_array(talloc_tos(),
4650 return NT_STATUS_NO_MEMORY;
4653 SIVAL(p,0,data_len);
4655 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4656 data = talloc_array(talloc_tos(),
4660 return NT_STATUS_NO_MEMORY;
4663 SIVAL(p,0,data_len);
4665 SCVAL(p, 0, 0); /* EA flags. */
4666 SCVAL(p, 1, ea_namelen);
4667 SSVAL(p, 2, ea_len);
4668 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4669 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4673 * FIXME - if we want to do previous version path
4674 * processing on an EA set call we need to turn this
4675 * into calls to cli_trans_send()/cli_trans_recv()
4676 * with a temporary event context, as cli_trans_send()
4677 * have access to the additional_flags2 needed to
4678 * send @GMT- paths. JRA.
4681 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4683 param, param_len, 2,
4686 NULL, 0, NULL, /* rsetup */
4687 NULL, 0, NULL, /* rparam */
4688 NULL, 0, NULL); /* rdata */
4693 /*********************************************************
4694 Set an extended attribute on a pathname.
4695 *********************************************************/
4697 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4698 const char *ea_name, const char *ea_val,
4701 unsigned int param_len = 0;
4704 TALLOC_CTX *frame = NULL;
4706 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4707 return cli_smb2_set_ea_path(cli,
4714 frame = talloc_stackframe();
4716 param = talloc_array(frame, uint8_t, 6);
4718 status = NT_STATUS_NO_MEMORY;
4721 SSVAL(param,0,SMB_INFO_SET_EA);
4725 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4726 path, strlen(path)+1,
4728 param_len = talloc_get_size(param);
4730 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4731 ea_name, ea_val, ea_len);
4739 /*********************************************************
4740 Set an extended attribute on an fnum.
4741 *********************************************************/
4743 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4744 const char *ea_name, const char *ea_val,
4749 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4750 return cli_smb2_set_ea_fnum(cli,
4757 memset(param, 0, 6);
4758 SSVAL(param,0,fnum);
4759 SSVAL(param,2,SMB_INFO_SET_EA);
4761 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4762 ea_name, ea_val, ea_len);
4765 /*********************************************************
4766 Get an extended attribute list utility fn.
4767 *********************************************************/
4769 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4771 size_t *pnum_eas, struct ea_struct **pea_list)
4773 struct ea_struct *ea_list = NULL;
4778 if (rdata_len < 4) {
4782 ea_size = (size_t)IVAL(rdata,0);
4783 if (ea_size > rdata_len) {
4788 /* No EA's present. */
4797 /* Validate the EA list and count it. */
4798 for (num_eas = 0; ea_size >= 4; num_eas++) {
4799 unsigned int ea_namelen = CVAL(p,1);
4800 unsigned int ea_valuelen = SVAL(p,2);
4801 if (ea_namelen == 0) {
4804 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4807 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4808 p += 4 + ea_namelen + 1 + ea_valuelen;
4817 *pnum_eas = num_eas;
4819 /* Caller only wants number of EA's. */
4823 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4828 ea_size = (size_t)IVAL(rdata,0);
4831 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4832 struct ea_struct *ea = &ea_list[num_eas];
4833 fstring unix_ea_name;
4834 unsigned int ea_namelen = CVAL(p,1);
4835 unsigned int ea_valuelen = SVAL(p,2);
4837 ea->flags = CVAL(p,0);
4838 unix_ea_name[0] = '\0';
4839 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4840 ea->name = talloc_strdup(ea_list, unix_ea_name);
4844 /* Ensure the value is null terminated (in case it's a string). */
4845 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4846 if (!ea->value.data) {
4850 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4852 ea->value.data[ea_valuelen] = 0;
4854 p += 4 + ea_namelen + 1 + ea_valuelen;
4857 *pea_list = ea_list;
4861 TALLOC_FREE(ea_list);
4865 /*********************************************************
4866 Get an extended attribute list from a pathname.
4867 *********************************************************/
4869 struct cli_get_ea_list_path_state {
4874 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4876 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4877 struct tevent_context *ev,
4878 struct cli_state *cli,
4881 struct tevent_req *req, *subreq;
4882 struct cli_get_ea_list_path_state *state;
4884 req = tevent_req_create(mem_ctx, &state,
4885 struct cli_get_ea_list_path_state);
4889 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4890 SMB_INFO_QUERY_ALL_EAS, 4,
4892 if (tevent_req_nomem(subreq, req)) {
4893 return tevent_req_post(req, ev);
4895 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4899 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4901 struct tevent_req *req = tevent_req_callback_data(
4902 subreq, struct tevent_req);
4903 struct cli_get_ea_list_path_state *state = tevent_req_data(
4904 req, struct cli_get_ea_list_path_state);
4907 status = cli_qpathinfo_recv(subreq, state, &state->data,
4909 TALLOC_FREE(subreq);
4910 if (tevent_req_nterror(req, status)) {
4913 tevent_req_done(req);
4916 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4917 size_t *pnum_eas, struct ea_struct **peas)
4919 struct cli_get_ea_list_path_state *state = tevent_req_data(
4920 req, struct cli_get_ea_list_path_state);
4923 if (tevent_req_is_nterror(req, &status)) {
4926 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4928 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4930 return NT_STATUS_OK;
4933 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4936 struct ea_struct **pea_list)
4938 TALLOC_CTX *frame = NULL;
4939 struct tevent_context *ev = NULL;
4940 struct tevent_req *req = NULL;
4941 NTSTATUS status = NT_STATUS_NO_MEMORY;
4943 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4944 return cli_smb2_get_ea_list_path(cli,
4951 frame = talloc_stackframe();
4953 if (smbXcli_conn_has_async_calls(cli->conn)) {
4955 * Can't use sync call while an async call is in flight
4957 status = NT_STATUS_INVALID_PARAMETER;
4960 ev = samba_tevent_context_init(frame);
4964 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4968 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4971 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4977 /****************************************************************************
4978 Convert open "flags" arg to uint32_t on wire.
4979 ****************************************************************************/
4981 static uint32_t open_flags_to_wire(int flags)
4983 int open_mode = flags & O_ACCMODE;
4986 switch (open_mode) {
4988 ret |= SMB_O_WRONLY;
4995 ret |= SMB_O_RDONLY;
4999 if (flags & O_CREAT) {
5002 if (flags & O_EXCL) {
5005 if (flags & O_TRUNC) {
5009 if (flags & O_SYNC) {
5013 if (flags & O_APPEND) {
5014 ret |= SMB_O_APPEND;
5016 #if defined(O_DIRECT)
5017 if (flags & O_DIRECT) {
5018 ret |= SMB_O_DIRECT;
5021 #if defined(O_DIRECTORY)
5022 if (flags & O_DIRECTORY) {
5023 ret |= SMB_O_DIRECTORY;
5029 /****************************************************************************
5030 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5031 ****************************************************************************/
5033 struct posix_open_state {
5037 uint16_t fnum; /* Out */
5040 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5042 struct tevent_req *req = tevent_req_callback_data(
5043 subreq, struct tevent_req);
5044 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5049 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5050 NULL, 0, NULL, &data, 12, &num_data);
5051 TALLOC_FREE(subreq);
5052 if (tevent_req_nterror(req, status)) {
5055 state->fnum = SVAL(data,2);
5056 tevent_req_done(req);
5059 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5060 struct tevent_context *ev,
5061 struct cli_state *cli,
5067 struct tevent_req *req = NULL, *subreq = NULL;
5068 struct posix_open_state *state = NULL;
5069 uint32_t wire_flags = open_flags_to_wire(flags);
5071 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5076 /* Setup setup word. */
5077 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5079 /* Setup param array. */
5080 state->param = talloc_array(state, uint8_t, 6);
5081 if (tevent_req_nomem(state->param, req)) {
5082 return tevent_req_post(req, ev);
5084 memset(state->param, '\0', 6);
5085 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5087 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5088 strlen(fname)+1, NULL);
5090 if (tevent_req_nomem(state->param, req)) {
5091 return tevent_req_post(req, ev);
5094 /* Setup data words. */
5096 wire_flags |= SMB_O_DIRECTORY;
5099 SIVAL(state->data,0,0); /* No oplock. */
5100 SIVAL(state->data,4,wire_flags);
5101 SIVAL(state->data,8,unix_perms_to_wire(mode));
5102 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5103 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5105 subreq = cli_trans_send(state, /* mem ctx. */
5106 ev, /* event ctx. */
5107 cli, /* cli_state. */
5108 0, /* additional_flags2 */
5109 SMBtrans2, /* cmd. */
5110 NULL, /* pipe name. */
5114 &state->setup, /* setup. */
5115 1, /* num setup uint16_t words. */
5116 0, /* max returned setup. */
5117 state->param, /* param. */
5118 talloc_get_size(state->param),/* num param. */
5119 2, /* max returned param. */
5120 state->data, /* data. */
5122 12); /* max returned data. */
5124 if (tevent_req_nomem(subreq, req)) {
5125 return tevent_req_post(req, ev);
5127 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5131 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5132 struct tevent_context *ev,
5133 struct cli_state *cli,
5138 return cli_posix_open_internal_send(mem_ctx, ev,
5139 cli, fname, flags, mode, false);
5142 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5144 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5147 if (tevent_req_is_nterror(req, &status)) {
5150 *pfnum = state->fnum;
5151 return NT_STATUS_OK;
5154 /****************************************************************************
5155 Open - POSIX semantics. Doesn't request oplock.
5156 ****************************************************************************/
5158 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5159 int flags, mode_t mode, uint16_t *pfnum)
5162 TALLOC_CTX *frame = talloc_stackframe();
5163 struct tevent_context *ev = NULL;
5164 struct tevent_req *req = NULL;
5165 NTSTATUS status = NT_STATUS_OK;
5167 if (smbXcli_conn_has_async_calls(cli->conn)) {
5169 * Can't use sync call while an async call is in flight
5171 status = NT_STATUS_INVALID_PARAMETER;
5175 ev = samba_tevent_context_init(frame);
5177 status = NT_STATUS_NO_MEMORY;
5181 req = cli_posix_open_send(frame,
5188 status = NT_STATUS_NO_MEMORY;
5192 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5196 status = cli_posix_open_recv(req, pfnum);
5203 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5204 struct tevent_context *ev,
5205 struct cli_state *cli,
5209 return cli_posix_open_internal_send(mem_ctx, ev,
5210 cli, fname, O_CREAT, mode, true);
5213 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5215 return tevent_req_simple_recv_ntstatus(req);
5218 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5220 TALLOC_CTX *frame = talloc_stackframe();
5221 struct tevent_context *ev = NULL;
5222 struct tevent_req *req = NULL;
5223 NTSTATUS status = NT_STATUS_OK;
5225 if (smbXcli_conn_has_async_calls(cli->conn)) {
5227 * Can't use sync call while an async call is in flight
5229 status = NT_STATUS_INVALID_PARAMETER;
5233 ev = samba_tevent_context_init(frame);
5235 status = NT_STATUS_NO_MEMORY;
5239 req = cli_posix_mkdir_send(frame,
5245 status = NT_STATUS_NO_MEMORY;
5249 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5253 status = cli_posix_mkdir_recv(req);
5260 /****************************************************************************
5261 unlink or rmdir - POSIX semantics.
5262 ****************************************************************************/
5264 struct cli_posix_unlink_internal_state {
5268 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5270 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5271 struct tevent_context *ev,
5272 struct cli_state *cli,
5276 struct tevent_req *req = NULL, *subreq = NULL;
5277 struct cli_posix_unlink_internal_state *state = NULL;
5279 req = tevent_req_create(mem_ctx, &state,
5280 struct cli_posix_unlink_internal_state);
5285 /* Setup data word. */
5286 SSVAL(state->data, 0, level);
5288 subreq = cli_setpathinfo_send(state, ev, cli,
5289 SMB_POSIX_PATH_UNLINK,
5291 state->data, sizeof(state->data));
5292 if (tevent_req_nomem(subreq, req)) {
5293 return tevent_req_post(req, ev);
5295 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5299 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5301 NTSTATUS status = cli_setpathinfo_recv(subreq);
5302 tevent_req_simple_finish_ntstatus(subreq, status);
5305 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5306 struct tevent_context *ev,
5307 struct cli_state *cli,
5310 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5311 SMB_POSIX_UNLINK_FILE_TARGET);
5314 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5316 return tevent_req_simple_recv_ntstatus(req);
5319 /****************************************************************************
5320 unlink - POSIX semantics.
5321 ****************************************************************************/
5323 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5325 TALLOC_CTX *frame = talloc_stackframe();
5326 struct tevent_context *ev = NULL;
5327 struct tevent_req *req = NULL;
5328 NTSTATUS status = NT_STATUS_OK;
5330 if (smbXcli_conn_has_async_calls(cli->conn)) {
5332 * Can't use sync call while an async call is in flight
5334 status = NT_STATUS_INVALID_PARAMETER;
5338 ev = samba_tevent_context_init(frame);
5340 status = NT_STATUS_NO_MEMORY;
5344 req = cli_posix_unlink_send(frame,
5349 status = NT_STATUS_NO_MEMORY;
5353 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5357 status = cli_posix_unlink_recv(req);
5364 /****************************************************************************
5365 rmdir - POSIX semantics.
5366 ****************************************************************************/
5368 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5369 struct tevent_context *ev,
5370 struct cli_state *cli,
5373 return cli_posix_unlink_internal_send(
5374 mem_ctx, ev, cli, fname,
5375 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5378 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5380 return tevent_req_simple_recv_ntstatus(req);
5383 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5385 TALLOC_CTX *frame = talloc_stackframe();
5386 struct tevent_context *ev = NULL;
5387 struct tevent_req *req = NULL;
5388 NTSTATUS status = NT_STATUS_OK;
5390 if (smbXcli_conn_has_async_calls(cli->conn)) {
5392 * Can't use sync call while an async call is in flight
5394 status = NT_STATUS_INVALID_PARAMETER;
5398 ev = samba_tevent_context_init(frame);
5400 status = NT_STATUS_NO_MEMORY;
5404 req = cli_posix_rmdir_send(frame,
5409 status = NT_STATUS_NO_MEMORY;
5413 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5417 status = cli_posix_rmdir_recv(req, frame);
5424 /****************************************************************************
5426 ****************************************************************************/
5428 struct cli_notify_state {
5430 uint32_t num_changes;
5431 struct notify_change *changes;
5434 static void cli_notify_done(struct tevent_req *subreq);
5436 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5437 struct tevent_context *ev,
5438 struct cli_state *cli, uint16_t fnum,
5439 uint32_t buffer_size,
5440 uint32_t completion_filter, bool recursive)
5442 struct tevent_req *req, *subreq;
5443 struct cli_notify_state *state;
5444 unsigned old_timeout;
5446 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5451 SIVAL(state->setup, 0, completion_filter);
5452 SSVAL(state->setup, 4, fnum);
5453 SSVAL(state->setup, 6, recursive);
5456 * Notifies should not time out
5458 old_timeout = cli_set_timeout(cli, 0);
5460 subreq = cli_trans_send(
5461 state, /* mem ctx. */
5462 ev, /* event ctx. */
5463 cli, /* cli_state. */
5464 0, /* additional_flags2 */
5465 SMBnttrans, /* cmd. */
5466 NULL, /* pipe name. */
5468 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5470 (uint16_t *)state->setup, /* setup. */
5471 4, /* num setup uint16_t words. */
5472 0, /* max returned setup. */
5475 buffer_size, /* max returned param. */
5478 0); /* max returned data. */
5480 cli_set_timeout(cli, old_timeout);
5482 if (tevent_req_nomem(subreq, req)) {
5483 return tevent_req_post(req, ev);
5485 tevent_req_set_callback(subreq, cli_notify_done, req);
5489 static void cli_notify_done(struct tevent_req *subreq)
5491 struct tevent_req *req = tevent_req_callback_data(
5492 subreq, struct tevent_req);
5493 struct cli_notify_state *state = tevent_req_data(
5494 req, struct cli_notify_state);
5497 uint32_t i, ofs, num_params;
5500 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5501 ¶ms, 0, &num_params, NULL, 0, NULL);
5502 TALLOC_FREE(subreq);
5503 if (tevent_req_nterror(req, status)) {
5504 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5508 state->num_changes = 0;
5511 while (num_params - ofs > 12) {
5512 uint32_t next = IVAL(params, ofs);
5513 state->num_changes += 1;
5515 if ((next == 0) || (ofs+next >= num_params)) {
5521 state->changes = talloc_array(state, struct notify_change,
5522 state->num_changes);
5523 if (tevent_req_nomem(state->changes, req)) {
5524 TALLOC_FREE(params);
5530 for (i=0; i<state->num_changes; i++) {
5531 uint32_t next = IVAL(params, ofs);
5532 uint32_t len = IVAL(params, ofs+8);
5536 if (trans_oob(num_params, ofs + 12, len)) {
5537 TALLOC_FREE(params);
5539 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5543 state->changes[i].action = IVAL(params, ofs+4);
5544 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5545 &name, params+ofs+12, len,
5546 STR_TERMINATE|STR_UNICODE);
5548 TALLOC_FREE(params);
5549 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5552 state->changes[i].name = name;
5556 TALLOC_FREE(params);
5557 tevent_req_done(req);
5560 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5561 uint32_t *pnum_changes,
5562 struct notify_change **pchanges)
5564 struct cli_notify_state *state = tevent_req_data(
5565 req, struct cli_notify_state);
5568 if (tevent_req_is_nterror(req, &status)) {
5572 *pnum_changes = state->num_changes;
5573 *pchanges = talloc_move(mem_ctx, &state->changes);
5574 return NT_STATUS_OK;
5577 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5578 uint32_t completion_filter, bool recursive,
5579 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5580 struct notify_change **pchanges)
5582 TALLOC_CTX *frame = talloc_stackframe();
5583 struct tevent_context *ev;
5584 struct tevent_req *req;
5585 NTSTATUS status = NT_STATUS_NO_MEMORY;
5587 if (smbXcli_conn_has_async_calls(cli->conn)) {
5589 * Can't use sync call while an async call is in flight
5591 status = NT_STATUS_INVALID_PARAMETER;
5594 ev = samba_tevent_context_init(frame);
5598 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5599 completion_filter, recursive);
5603 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5606 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5612 struct cli_qpathinfo_state {
5621 static void cli_qpathinfo_done(struct tevent_req *subreq);
5623 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5624 struct tevent_context *ev,
5625 struct cli_state *cli, const char *fname,
5626 uint16_t level, uint32_t min_rdata,
5629 struct tevent_req *req, *subreq;
5630 struct cli_qpathinfo_state *state;
5631 uint16_t additional_flags2 = 0;
5633 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5637 state->min_rdata = min_rdata;
5638 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5640 state->param = talloc_zero_array(state, uint8_t, 6);
5641 if (tevent_req_nomem(state->param, req)) {
5642 return tevent_req_post(req, ev);
5644 SSVAL(state->param, 0, level);
5645 state->param = trans2_bytes_push_str(
5646 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5647 if (tevent_req_nomem(state->param, req)) {
5648 return tevent_req_post(req, ev);
5651 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL) &&
5652 !INFO_LEVEL_IS_UNIX(level)) {
5653 additional_flags2 = FLAGS2_REPARSE_PATH;
5656 subreq = cli_trans_send(
5657 state, /* mem ctx. */
5658 ev, /* event ctx. */
5659 cli, /* cli_state. */
5660 additional_flags2, /* additional_flags2 */
5661 SMBtrans2, /* cmd. */
5662 NULL, /* pipe name. */
5666 state->setup, /* setup. */
5667 1, /* num setup uint16_t words. */
5668 0, /* max returned setup. */
5669 state->param, /* param. */
5670 talloc_get_size(state->param), /* num param. */
5671 2, /* max returned param. */
5674 max_rdata); /* max returned data. */
5676 if (tevent_req_nomem(subreq, req)) {
5677 return tevent_req_post(req, ev);
5679 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5683 static void cli_qpathinfo_done(struct tevent_req *subreq)
5685 struct tevent_req *req = tevent_req_callback_data(
5686 subreq, struct tevent_req);
5687 struct cli_qpathinfo_state *state = tevent_req_data(
5688 req, struct cli_qpathinfo_state);
5691 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5693 &state->rdata, state->min_rdata,
5695 if (tevent_req_nterror(req, status)) {
5698 tevent_req_done(req);
5701 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5702 uint8_t **rdata, uint32_t *num_rdata)
5704 struct cli_qpathinfo_state *state = tevent_req_data(
5705 req, struct cli_qpathinfo_state);
5708 if (tevent_req_is_nterror(req, &status)) {
5711 if (rdata != NULL) {
5712 *rdata = talloc_move(mem_ctx, &state->rdata);
5714 TALLOC_FREE(state->rdata);
5716 if (num_rdata != NULL) {
5717 *num_rdata = state->num_rdata;
5719 return NT_STATUS_OK;
5722 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5723 const char *fname, uint16_t level, uint32_t min_rdata,
5725 uint8_t **rdata, uint32_t *num_rdata)
5727 TALLOC_CTX *frame = talloc_stackframe();
5728 struct tevent_context *ev;
5729 struct tevent_req *req;
5730 NTSTATUS status = NT_STATUS_NO_MEMORY;
5732 if (smbXcli_conn_has_async_calls(cli->conn)) {
5734 * Can't use sync call while an async call is in flight
5736 status = NT_STATUS_INVALID_PARAMETER;
5739 ev = samba_tevent_context_init(frame);
5743 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5748 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5751 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5757 struct cli_qfileinfo_state {
5761 uint16_t recv_flags2;
5767 static void cli_qfileinfo_done(struct tevent_req *subreq);
5769 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5770 struct tevent_context *ev,
5771 struct cli_state *cli, uint16_t fnum,
5772 uint16_t level, uint32_t min_rdata,
5775 struct tevent_req *req, *subreq;
5776 struct cli_qfileinfo_state *state;
5778 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5782 state->min_rdata = min_rdata;
5783 SSVAL(state->param, 0, fnum);
5784 SSVAL(state->param, 2, level);
5785 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5787 subreq = cli_trans_send(
5788 state, /* mem ctx. */
5789 ev, /* event ctx. */
5790 cli, /* cli_state. */
5791 0, /* additional_flags2 */
5792 SMBtrans2, /* cmd. */
5793 NULL, /* pipe name. */
5797 state->setup, /* setup. */
5798 1, /* num setup uint16_t words. */
5799 0, /* max returned setup. */
5800 state->param, /* param. */
5801 sizeof(state->param), /* num param. */
5802 2, /* max returned param. */
5805 max_rdata); /* max returned data. */
5807 if (tevent_req_nomem(subreq, req)) {
5808 return tevent_req_post(req, ev);
5810 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5814 static void cli_qfileinfo_done(struct tevent_req *subreq)
5816 struct tevent_req *req = tevent_req_callback_data(
5817 subreq, struct tevent_req);
5818 struct cli_qfileinfo_state *state = tevent_req_data(
5819 req, struct cli_qfileinfo_state);
5822 status = cli_trans_recv(subreq, state,
5823 &state->recv_flags2,
5826 &state->rdata, state->min_rdata,
5828 if (tevent_req_nterror(req, status)) {
5831 tevent_req_done(req);
5834 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5835 uint16_t *recv_flags2,
5836 uint8_t **rdata, uint32_t *num_rdata)
5838 struct cli_qfileinfo_state *state = tevent_req_data(
5839 req, struct cli_qfileinfo_state);
5842 if (tevent_req_is_nterror(req, &status)) {
5846 if (recv_flags2 != NULL) {
5847 *recv_flags2 = state->recv_flags2;
5849 if (rdata != NULL) {
5850 *rdata = talloc_move(mem_ctx, &state->rdata);
5852 TALLOC_FREE(state->rdata);
5854 if (num_rdata != NULL) {
5855 *num_rdata = state->num_rdata;
5857 return NT_STATUS_OK;
5860 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5861 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5862 uint32_t max_rdata, uint16_t *recv_flags2,
5863 uint8_t **rdata, uint32_t *num_rdata)
5865 TALLOC_CTX *frame = talloc_stackframe();
5866 struct tevent_context *ev;
5867 struct tevent_req *req;
5868 NTSTATUS status = NT_STATUS_NO_MEMORY;
5870 if (smbXcli_conn_has_async_calls(cli->conn)) {
5872 * Can't use sync call while an async call is in flight
5874 status = NT_STATUS_INVALID_PARAMETER;
5877 ev = samba_tevent_context_init(frame);
5881 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5886 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5889 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5895 struct cli_flush_state {
5899 static void cli_flush_done(struct tevent_req *subreq);
5901 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5902 struct tevent_context *ev,
5903 struct cli_state *cli,
5906 struct tevent_req *req, *subreq;
5907 struct cli_flush_state *state;
5909 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5913 SSVAL(state->vwv + 0, 0, fnum);
5915 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5917 if (tevent_req_nomem(subreq, req)) {
5918 return tevent_req_post(req, ev);
5920 tevent_req_set_callback(subreq, cli_flush_done, req);
5924 static void cli_flush_done(struct tevent_req *subreq)
5926 struct tevent_req *req = tevent_req_callback_data(
5927 subreq, struct tevent_req);
5930 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5931 TALLOC_FREE(subreq);
5932 if (tevent_req_nterror(req, status)) {
5935 tevent_req_done(req);
5938 NTSTATUS cli_flush_recv(struct tevent_req *req)
5940 return tevent_req_simple_recv_ntstatus(req);
5943 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5945 TALLOC_CTX *frame = talloc_stackframe();
5946 struct tevent_context *ev;
5947 struct tevent_req *req;
5948 NTSTATUS status = NT_STATUS_NO_MEMORY;
5950 if (smbXcli_conn_has_async_calls(cli->conn)) {
5952 * Can't use sync call while an async call is in flight
5954 status = NT_STATUS_INVALID_PARAMETER;
5957 ev = samba_tevent_context_init(frame);
5961 req = cli_flush_send(frame, ev, cli, fnum);
5965 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5968 status = cli_flush_recv(req);
5974 struct cli_shadow_copy_data_state {
5981 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5983 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5984 struct tevent_context *ev,
5985 struct cli_state *cli,
5989 struct tevent_req *req, *subreq;
5990 struct cli_shadow_copy_data_state *state;
5993 req = tevent_req_create(mem_ctx, &state,
5994 struct cli_shadow_copy_data_state);
5998 state->get_names = get_names;
5999 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
6001 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
6002 SSVAL(state->setup + 2, 0, fnum);
6003 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
6004 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
6006 subreq = cli_trans_send(
6007 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
6008 state->setup, ARRAY_SIZE(state->setup),
6009 ARRAY_SIZE(state->setup),
6012 if (tevent_req_nomem(subreq, req)) {
6013 return tevent_req_post(req, ev);
6015 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
6019 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
6021 struct tevent_req *req = tevent_req_callback_data(
6022 subreq, struct tevent_req);
6023 struct cli_shadow_copy_data_state *state = tevent_req_data(
6024 req, struct cli_shadow_copy_data_state);
6027 status = cli_trans_recv(subreq, state, NULL,
6028 NULL, 0, NULL, /* setup */
6029 NULL, 0, NULL, /* param */
6030 &state->data, 12, &state->num_data);
6031 TALLOC_FREE(subreq);
6032 if (tevent_req_nterror(req, status)) {
6035 tevent_req_done(req);
6038 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6039 char ***pnames, int *pnum_names)
6041 struct cli_shadow_copy_data_state *state = tevent_req_data(
6042 req, struct cli_shadow_copy_data_state);
6043 char **names = NULL;
6044 uint32_t i, num_names;
6046 uint8_t *endp = NULL;
6049 if (tevent_req_is_nterror(req, &status)) {
6053 if (state->num_data < 16) {
6054 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6057 num_names = IVAL(state->data, 4);
6058 dlength = IVAL(state->data, 8);
6060 if (num_names > 0x7FFFFFFF) {
6061 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6064 if (!state->get_names) {
6065 *pnum_names = (int)num_names;
6066 return NT_STATUS_OK;
6069 if (dlength + 12 < 12) {
6070 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6072 if (dlength + 12 > state->num_data) {
6073 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6075 if (state->num_data + (2 * sizeof(SHADOW_COPY_LABEL)) <
6077 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6080 names = talloc_array(mem_ctx, char *, num_names);
6081 if (names == NULL) {
6082 return NT_STATUS_NO_MEMORY;
6085 endp = state->data + state->num_data;
6087 for (i=0; i<num_names; i++) {
6090 size_t converted_size;
6092 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6094 if (src + (2 * sizeof(SHADOW_COPY_LABEL)) > endp) {
6095 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6098 ret = convert_string_talloc(
6099 names, CH_UTF16LE, CH_UNIX,
6100 src, 2 * sizeof(SHADOW_COPY_LABEL),
6101 &names[i], &converted_size);
6104 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6107 *pnum_names = (int)num_names;
6109 return NT_STATUS_OK;
6112 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6113 uint16_t fnum, bool get_names,
6114 char ***pnames, int *pnum_names)
6116 TALLOC_CTX *frame = NULL;
6117 struct tevent_context *ev;
6118 struct tevent_req *req;
6119 NTSTATUS status = NT_STATUS_NO_MEMORY;
6121 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6122 return cli_smb2_shadow_copy_data(mem_ctx,
6130 frame = talloc_stackframe();
6132 if (smbXcli_conn_has_async_calls(cli->conn)) {
6134 * Can't use sync call while an async call is in flight
6136 status = NT_STATUS_INVALID_PARAMETER;
6139 ev = samba_tevent_context_init(frame);
6143 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6147 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6150 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);