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/security.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 link_target.
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,
167 const char *link_target,
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),
186 link_target, strlen(link_target)+1, NULL);
188 subreq = cli_setpathinfo_send(
189 state, ev, cli, level, newname,
190 state->data, talloc_get_size(state->data));
191 if (tevent_req_nomem(subreq, req)) {
192 return tevent_req_post(req, ev);
194 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
198 static void cli_posix_link_internal_done(struct tevent_req *subreq)
200 NTSTATUS status = cli_setpathinfo_recv(subreq);
201 tevent_req_simple_finish_ntstatus(subreq, status);
204 static NTSTATUS cli_posix_link_internal_recv(struct tevent_req *req)
206 return tevent_req_simple_recv_ntstatus(req);
209 /****************************************************************************
210 Symlink a file (UNIX extensions).
211 ****************************************************************************/
213 struct cli_posix_symlink_state {
217 static void cli_posix_symlink_done(struct tevent_req *subreq);
219 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
220 struct tevent_context *ev,
221 struct cli_state *cli,
222 const char *link_target,
225 struct tevent_req *req = NULL, *subreq = NULL;
226 struct cli_posix_symlink_state *state = NULL;
228 req = tevent_req_create(
229 mem_ctx, &state, struct cli_posix_symlink_state);
234 subreq = cli_posix_link_internal_send(
235 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, link_target, newname);
236 if (tevent_req_nomem(subreq, req)) {
237 return tevent_req_post(req, ev);
239 tevent_req_set_callback(subreq, cli_posix_symlink_done, req);
243 static void cli_posix_symlink_done(struct tevent_req *subreq)
245 NTSTATUS status = cli_posix_link_internal_recv(subreq);
246 tevent_req_simple_finish_ntstatus(subreq, status);
249 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
251 return tevent_req_simple_recv_ntstatus(req);
254 NTSTATUS cli_posix_symlink(struct cli_state *cli,
255 const char *link_target,
258 TALLOC_CTX *frame = talloc_stackframe();
259 struct tevent_context *ev = NULL;
260 struct tevent_req *req = NULL;
261 NTSTATUS status = NT_STATUS_OK;
263 if (smbXcli_conn_has_async_calls(cli->conn)) {
265 * Can't use sync call while an async call is in flight
267 status = NT_STATUS_INVALID_PARAMETER;
271 ev = samba_tevent_context_init(frame);
273 status = NT_STATUS_NO_MEMORY;
277 req = cli_posix_symlink_send(frame,
283 status = NT_STATUS_NO_MEMORY;
287 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
291 status = cli_posix_symlink_recv(req);
298 /****************************************************************************
299 Read a POSIX symlink.
300 ****************************************************************************/
302 struct cli_posix_readlink_state {
303 struct cli_state *cli;
307 static void cli_posix_readlink_done(struct tevent_req *subreq);
309 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
310 struct tevent_context *ev,
311 struct cli_state *cli,
314 struct tevent_req *req = NULL, *subreq = NULL;
315 struct cli_posix_readlink_state *state = NULL;
317 req = tevent_req_create(
318 mem_ctx, &state, struct cli_posix_readlink_state);
324 subreq = cli_qpathinfo_send(
329 SMB_QUERY_FILE_UNIX_LINK,
332 if (tevent_req_nomem(subreq, req)) {
333 return tevent_req_post(req, ev);
335 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
339 static void cli_posix_readlink_done(struct tevent_req *subreq)
341 struct tevent_req *req = tevent_req_callback_data(
342 subreq, struct tevent_req);
343 struct cli_posix_readlink_state *state = tevent_req_data(
344 req, struct cli_posix_readlink_state);
346 uint8_t *data = NULL;
349 size_t converted_size;
352 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
354 if (tevent_req_nterror(req, status)) {
358 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
360 if (data[num_data-1] != '\0') {
361 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
365 charset = smbXcli_conn_use_unicode(state->cli->conn) ?
368 /* The returned data is a pushed string, not raw data. */
369 ok = convert_string_talloc(
381 tevent_req_done(req);
384 NTSTATUS cli_posix_readlink_recv(
385 struct tevent_req *req, TALLOC_CTX *mem_ctx, char **target)
387 struct cli_posix_readlink_state *state = tevent_req_data(
388 req, struct cli_posix_readlink_state);
391 if (tevent_req_is_nterror(req, &status)) {
394 *target = talloc_move(mem_ctx, &state->converted);
398 NTSTATUS cli_posix_readlink(
399 struct cli_state *cli,
404 TALLOC_CTX *frame = talloc_stackframe();
405 struct tevent_context *ev = NULL;
406 struct tevent_req *req = NULL;
407 NTSTATUS status = NT_STATUS_OK;
409 if (smbXcli_conn_has_async_calls(cli->conn)) {
411 * Can't use sync call while an async call is in flight
413 status = NT_STATUS_INVALID_PARAMETER;
417 ev = samba_tevent_context_init(frame);
419 status = NT_STATUS_NO_MEMORY;
423 req = cli_posix_readlink_send(frame, ev, cli, fname);
425 status = NT_STATUS_NO_MEMORY;
429 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
433 status = cli_posix_readlink_recv(req, mem_ctx, target);
440 /****************************************************************************
441 Hard link a file (UNIX extensions).
442 ****************************************************************************/
444 struct cli_posix_hardlink_state {
448 static void cli_posix_hardlink_done(struct tevent_req *subreq);
450 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
451 struct tevent_context *ev,
452 struct cli_state *cli,
456 struct tevent_req *req = NULL, *subreq = NULL;
457 struct cli_posix_hardlink_state *state = NULL;
459 req = tevent_req_create(
460 mem_ctx, &state, struct cli_posix_hardlink_state);
465 subreq = cli_posix_link_internal_send(
466 state, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
467 if (tevent_req_nomem(subreq, req)) {
468 return tevent_req_post(req, ev);
470 tevent_req_set_callback(subreq, cli_posix_hardlink_done, req);
474 static void cli_posix_hardlink_done(struct tevent_req *subreq)
476 NTSTATUS status = cli_posix_link_internal_recv(subreq);
477 tevent_req_simple_finish_ntstatus(subreq, status);
480 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
482 return tevent_req_simple_recv_ntstatus(req);
485 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
489 TALLOC_CTX *frame = talloc_stackframe();
490 struct tevent_context *ev = NULL;
491 struct tevent_req *req = NULL;
492 NTSTATUS status = NT_STATUS_OK;
494 if (smbXcli_conn_has_async_calls(cli->conn)) {
496 * Can't use sync call while an async call is in flight
498 status = NT_STATUS_INVALID_PARAMETER;
502 ev = samba_tevent_context_init(frame);
504 status = NT_STATUS_NO_MEMORY;
508 req = cli_posix_hardlink_send(frame,
514 status = NT_STATUS_NO_MEMORY;
518 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
522 status = cli_posix_hardlink_recv(req);
529 /****************************************************************************
530 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
531 ****************************************************************************/
533 struct getacl_state {
538 static void cli_posix_getacl_done(struct tevent_req *subreq);
540 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
541 struct tevent_context *ev,
542 struct cli_state *cli,
545 struct tevent_req *req = NULL, *subreq = NULL;
546 struct getacl_state *state = NULL;
548 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
552 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
554 if (tevent_req_nomem(subreq, req)) {
555 return tevent_req_post(req, ev);
557 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
561 static void cli_posix_getacl_done(struct tevent_req *subreq)
563 struct tevent_req *req = tevent_req_callback_data(
564 subreq, struct tevent_req);
565 struct getacl_state *state = tevent_req_data(
566 req, struct getacl_state);
569 status = cli_qpathinfo_recv(subreq, state, &state->data,
572 if (tevent_req_nterror(req, status)) {
575 tevent_req_done(req);
578 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
583 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
586 if (tevent_req_is_nterror(req, &status)) {
589 *prb_size = (size_t)state->num_data;
590 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
594 NTSTATUS cli_posix_getacl(struct cli_state *cli,
600 TALLOC_CTX *frame = talloc_stackframe();
601 struct tevent_context *ev = NULL;
602 struct tevent_req *req = NULL;
603 NTSTATUS status = NT_STATUS_OK;
605 if (smbXcli_conn_has_async_calls(cli->conn)) {
607 * Can't use sync call while an async call is in flight
609 status = NT_STATUS_INVALID_PARAMETER;
613 ev = samba_tevent_context_init(frame);
615 status = NT_STATUS_NO_MEMORY;
619 req = cli_posix_getacl_send(frame,
624 status = NT_STATUS_NO_MEMORY;
628 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
632 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
639 /****************************************************************************
640 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
641 ****************************************************************************/
643 struct setacl_state {
647 static void cli_posix_setacl_done(struct tevent_req *subreq);
649 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
650 struct tevent_context *ev,
651 struct cli_state *cli,
656 struct tevent_req *req = NULL, *subreq = NULL;
657 struct setacl_state *state = NULL;
659 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
663 state->data = talloc_memdup(state, data, num_data);
664 if (tevent_req_nomem(state->data, req)) {
665 return tevent_req_post(req, ev);
668 subreq = cli_setpathinfo_send(state,
675 if (tevent_req_nomem(subreq, req)) {
676 return tevent_req_post(req, ev);
678 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
682 static void cli_posix_setacl_done(struct tevent_req *subreq)
684 NTSTATUS status = cli_setpathinfo_recv(subreq);
685 tevent_req_simple_finish_ntstatus(subreq, status);
688 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
690 return tevent_req_simple_recv_ntstatus(req);
693 NTSTATUS cli_posix_setacl(struct cli_state *cli,
698 TALLOC_CTX *frame = talloc_stackframe();
699 struct tevent_context *ev = NULL;
700 struct tevent_req *req = NULL;
701 NTSTATUS status = NT_STATUS_OK;
703 if (smbXcli_conn_has_async_calls(cli->conn)) {
705 * Can't use sync call while an async call is in flight
707 status = NT_STATUS_INVALID_PARAMETER;
711 ev = samba_tevent_context_init(frame);
713 status = NT_STATUS_NO_MEMORY;
717 req = cli_posix_setacl_send(frame,
724 status = NT_STATUS_NO_MEMORY;
728 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
732 status = cli_posix_setacl_recv(req);
739 /****************************************************************************
740 Stat a file (UNIX extensions).
741 ****************************************************************************/
744 SMB_STRUCT_STAT *sbuf;
747 static void cli_posix_stat_done(struct tevent_req *subreq);
749 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
750 struct tevent_context *ev,
751 struct cli_state *cli,
753 SMB_STRUCT_STAT *sbuf)
755 struct tevent_req *req = NULL, *subreq = NULL;
756 struct stat_state *state = NULL;
758 req = tevent_req_create(mem_ctx, &state, struct stat_state);
764 subreq = cli_qpathinfo_send(state, ev, cli, fname,
765 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
766 if (tevent_req_nomem(subreq, req)) {
767 return tevent_req_post(req, ev);
769 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
773 static void cli_posix_stat_done(struct tevent_req *subreq)
775 struct tevent_req *req = tevent_req_callback_data(
776 subreq, struct tevent_req);
777 struct stat_state *state = tevent_req_data(req, struct stat_state);
778 SMB_STRUCT_STAT *sbuf = state->sbuf;
780 uint32_t num_data = 0;
783 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
785 if (tevent_req_nterror(req, status)) {
789 if (num_data != 100) {
791 * Paranoia, cli_qpathinfo should have guaranteed
792 * this, but you never know...
794 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
798 *sbuf = (SMB_STRUCT_STAT) { 0 };
800 /* total size, in bytes */
801 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(data, 0);
803 /* number of blocks allocated */
804 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(data,8);
805 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
806 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
808 /* assume 512 byte blocks */
809 sbuf->st_ex_blocks /= 512;
811 /* time of last change */
812 sbuf->st_ex_ctime = interpret_long_date((char *)(data + 16));
814 /* time of last access */
815 sbuf->st_ex_atime = interpret_long_date((char *)(data + 24));
817 /* time of last modification */
818 sbuf->st_ex_mtime = interpret_long_date((char *)(data + 32));
820 sbuf->st_ex_uid = (uid_t) IVAL(data, 40); /* user ID of owner */
821 sbuf->st_ex_gid = (gid_t) IVAL(data, 48); /* group ID of owner */
822 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(data, 56));
824 #if defined(HAVE_MAKEDEV)
826 uint32_t dev_major = IVAL(data,60);
827 uint32_t dev_minor = IVAL(data,68);
828 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
832 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(data, 76);
835 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(data, 84));
837 /* number of hard links */
838 sbuf->st_ex_nlink = BIG_UINT(data, 92);
840 tevent_req_done(req);
843 NTSTATUS cli_posix_stat_recv(struct tevent_req *req)
845 return tevent_req_simple_recv_ntstatus(req);
848 NTSTATUS cli_posix_stat(struct cli_state *cli,
850 SMB_STRUCT_STAT *sbuf)
852 TALLOC_CTX *frame = talloc_stackframe();
853 struct tevent_context *ev = NULL;
854 struct tevent_req *req = NULL;
855 NTSTATUS status = NT_STATUS_OK;
857 if (smbXcli_conn_has_async_calls(cli->conn)) {
859 * Can't use sync call while an async call is in flight
861 status = NT_STATUS_INVALID_PARAMETER;
865 ev = samba_tevent_context_init(frame);
867 status = NT_STATUS_NO_MEMORY;
871 req = cli_posix_stat_send(frame, ev, cli, fname, sbuf);
873 status = NT_STATUS_NO_MEMORY;
877 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
881 status = cli_posix_stat_recv(req);
888 /****************************************************************************
889 Chmod or chown a file internal (UNIX extensions).
890 ****************************************************************************/
892 struct cli_posix_chown_chmod_internal_state {
896 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
898 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
899 struct tevent_context *ev,
900 struct cli_state *cli,
906 struct tevent_req *req = NULL, *subreq = NULL;
907 struct cli_posix_chown_chmod_internal_state *state = NULL;
909 req = tevent_req_create(mem_ctx, &state,
910 struct cli_posix_chown_chmod_internal_state);
915 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
916 memset(&state->data[40], '\0', 60);
917 SIVAL(state->data,40,uid);
918 SIVAL(state->data,48,gid);
919 SIVAL(state->data,84,mode);
921 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
922 fname, state->data, sizeof(state->data));
923 if (tevent_req_nomem(subreq, req)) {
924 return tevent_req_post(req, ev);
926 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
931 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
933 NTSTATUS status = cli_setpathinfo_recv(subreq);
934 tevent_req_simple_finish_ntstatus(subreq, status);
937 static NTSTATUS cli_posix_chown_chmod_internal_recv(struct tevent_req *req)
939 return tevent_req_simple_recv_ntstatus(req);
942 /****************************************************************************
943 chmod a file (UNIX extensions).
944 ****************************************************************************/
946 struct cli_posix_chmod_state {
950 static void cli_posix_chmod_done(struct tevent_req *subreq);
952 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
953 struct tevent_context *ev,
954 struct cli_state *cli,
958 struct tevent_req *req = NULL, *subreq = NULL;
959 struct cli_posix_chmod_state *state = NULL;
961 req = tevent_req_create(mem_ctx, &state, struct cli_posix_chmod_state);
966 subreq = cli_posix_chown_chmod_internal_send(
971 unix_perms_to_wire(mode),
974 if (tevent_req_nomem(subreq, req)) {
975 return tevent_req_post(req, ev);
977 tevent_req_set_callback(subreq, cli_posix_chmod_done, req);
981 static void cli_posix_chmod_done(struct tevent_req *subreq)
983 NTSTATUS status = cli_posix_chown_chmod_internal_recv(subreq);
984 tevent_req_simple_finish_ntstatus(subreq, status);
987 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
989 return tevent_req_simple_recv_ntstatus(req);
992 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
994 TALLOC_CTX *frame = talloc_stackframe();
995 struct tevent_context *ev = NULL;
996 struct tevent_req *req = NULL;
997 NTSTATUS status = NT_STATUS_OK;
999 if (smbXcli_conn_has_async_calls(cli->conn)) {
1001 * Can't use sync call while an async call is in flight
1003 status = NT_STATUS_INVALID_PARAMETER;
1007 ev = samba_tevent_context_init(frame);
1009 status = NT_STATUS_NO_MEMORY;
1013 req = cli_posix_chmod_send(frame,
1019 status = NT_STATUS_NO_MEMORY;
1023 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1027 status = cli_posix_chmod_recv(req);
1034 /****************************************************************************
1035 chown a file (UNIX extensions).
1036 ****************************************************************************/
1038 struct cli_posix_chown_state {
1042 static void cli_posix_chown_done(struct tevent_req *subreq);
1044 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1045 struct tevent_context *ev,
1046 struct cli_state *cli,
1051 struct tevent_req *req = NULL, *subreq = NULL;
1052 struct cli_posix_chown_state *state = NULL;
1054 req = tevent_req_create(
1055 mem_ctx, &state, struct cli_posix_chown_state);
1060 subreq = cli_posix_chown_chmod_internal_send(
1068 if (tevent_req_nomem(subreq, req)) {
1069 return tevent_req_post(req, ev);
1071 tevent_req_set_callback(subreq, cli_posix_chown_done, req);
1075 static void cli_posix_chown_done(struct tevent_req *subreq)
1077 NTSTATUS status = cli_posix_chown_chmod_internal_recv(subreq);
1078 tevent_req_simple_finish_ntstatus(subreq, status);
1081 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1083 return tevent_req_simple_recv_ntstatus(req);
1086 NTSTATUS cli_posix_chown(struct cli_state *cli,
1091 TALLOC_CTX *frame = talloc_stackframe();
1092 struct tevent_context *ev = NULL;
1093 struct tevent_req *req = NULL;
1094 NTSTATUS status = NT_STATUS_OK;
1096 if (smbXcli_conn_has_async_calls(cli->conn)) {
1098 * Can't use sync call while an async call is in flight
1100 status = NT_STATUS_INVALID_PARAMETER;
1104 ev = samba_tevent_context_init(frame);
1106 status = NT_STATUS_NO_MEMORY;
1110 req = cli_posix_chown_send(frame,
1117 status = NT_STATUS_NO_MEMORY;
1121 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1125 status = cli_posix_chown_recv(req);
1132 /****************************************************************************
1134 ****************************************************************************/
1136 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1137 struct tevent_context *ev,
1138 struct cli_state *cli,
1139 const char *fname_src,
1140 const char *fname_dst,
1143 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1144 struct tevent_context *ev,
1145 struct cli_state *cli,
1146 const char *fname_src,
1147 const char *fname_dst,
1150 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1151 struct tevent_context *ev,
1152 struct cli_state *cli,
1153 const char *fname_src,
1154 const char *fname_dst,
1157 if (replace && smbXcli_conn_support_passthrough(cli->conn)) {
1158 return cli_smb1_rename_send(mem_ctx, ev, cli, fname_src,
1159 fname_dst, replace);
1161 return cli_cifs_rename_send(mem_ctx, ev, cli, fname_src,
1162 fname_dst, replace);
1166 struct cli_smb1_rename_state {
1170 static void cli_smb1_rename_done(struct tevent_req *subreq);
1172 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1173 struct tevent_context *ev,
1174 struct cli_state *cli,
1175 const char *fname_src,
1176 const char *fname_dst,
1180 struct tevent_req *req = NULL, *subreq = NULL;
1181 struct cli_smb1_rename_state *state = NULL;
1182 smb_ucs2_t *converted_str = NULL;
1183 size_t converted_size_bytes = 0;
1185 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_rename_state);
1190 if (!push_ucs2_talloc(talloc_tos(), &converted_str, fname_dst,
1191 &converted_size_bytes)) {
1192 status = NT_STATUS_INVALID_PARAMETER;
1196 /* W2K8 insists the dest name is not null
1197 terminated. Remove the last 2 zero bytes
1198 and reduce the name length. */
1200 if (converted_size_bytes < 2) {
1201 status = NT_STATUS_INVALID_PARAMETER;
1204 converted_size_bytes -= 2;
1207 talloc_zero_array(state, uint8_t, 12 + converted_size_bytes);
1208 if (state->data == NULL) {
1209 status = NT_STATUS_NO_MEMORY;
1214 SCVAL(state->data, 0, 1);
1217 SIVAL(state->data, 8, converted_size_bytes);
1218 memcpy(state->data + 12, converted_str, converted_size_bytes);
1220 TALLOC_FREE(converted_str);
1222 subreq = cli_setpathinfo_send(
1223 state, ev, cli, SMB_FILE_RENAME_INFORMATION, fname_src, state->data,
1224 talloc_get_size(state->data));
1225 if (tevent_req_nomem(subreq, req)) {
1226 status = NT_STATUS_NO_MEMORY;
1229 tevent_req_set_callback(subreq, cli_smb1_rename_done, req);
1233 TALLOC_FREE(converted_str);
1234 tevent_req_nterror(req, status);
1235 return tevent_req_post(req, ev);
1238 static void cli_smb1_rename_done(struct tevent_req *subreq)
1240 NTSTATUS status = cli_setpathinfo_recv(subreq);
1241 tevent_req_simple_finish_ntstatus(subreq, status);
1244 static void cli_cifs_rename_done(struct tevent_req *subreq);
1246 struct cli_cifs_rename_state {
1250 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1251 struct tevent_context *ev,
1252 struct cli_state *cli,
1253 const char *fname_src,
1254 const char *fname_dst,
1257 struct tevent_req *req = NULL, *subreq = NULL;
1258 struct cli_cifs_rename_state *state = NULL;
1259 uint8_t additional_flags = 0;
1260 uint16_t additional_flags2 = 0;
1261 uint8_t *bytes = NULL;
1263 req = tevent_req_create(mem_ctx, &state, struct cli_cifs_rename_state);
1270 * CIFS doesn't support replace
1272 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1273 return tevent_req_post(req, ev);
1276 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1278 bytes = talloc_array(state, uint8_t, 1);
1279 if (tevent_req_nomem(bytes, req)) {
1280 return tevent_req_post(req, ev);
1283 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1284 strlen(fname_src)+1, NULL);
1285 if (tevent_req_nomem(bytes, req)) {
1286 return tevent_req_post(req, ev);
1289 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1290 additional_flags2 = FLAGS2_REPARSE_PATH;
1293 bytes = talloc_realloc(state, bytes, uint8_t,
1294 talloc_get_size(bytes)+1);
1295 if (tevent_req_nomem(bytes, req)) {
1296 return tevent_req_post(req, ev);
1299 bytes[talloc_get_size(bytes)-1] = 4;
1300 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1301 strlen(fname_dst)+1, NULL);
1302 if (tevent_req_nomem(bytes, req)) {
1303 return tevent_req_post(req, ev);
1306 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1308 1, state->vwv, talloc_get_size(bytes), bytes);
1309 if (tevent_req_nomem(subreq, req)) {
1310 return tevent_req_post(req, ev);
1312 tevent_req_set_callback(subreq, cli_cifs_rename_done, req);
1316 static void cli_cifs_rename_done(struct tevent_req *subreq)
1318 struct tevent_req *req = tevent_req_callback_data(
1319 subreq, struct tevent_req);
1322 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1323 TALLOC_FREE(subreq);
1324 if (tevent_req_nterror(req, status)) {
1327 tevent_req_done(req);
1330 NTSTATUS cli_rename_recv(struct tevent_req *req)
1332 return tevent_req_simple_recv_ntstatus(req);
1335 NTSTATUS cli_rename(struct cli_state *cli,
1336 const char *fname_src,
1337 const char *fname_dst,
1340 TALLOC_CTX *frame = NULL;
1341 struct tevent_context *ev;
1342 struct tevent_req *req;
1343 NTSTATUS status = NT_STATUS_OK;
1345 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1346 return cli_smb2_rename(cli, fname_src, fname_dst, replace);
1349 frame = talloc_stackframe();
1351 if (smbXcli_conn_has_async_calls(cli->conn)) {
1353 * Can't use sync call while an async call is in flight
1355 status = NT_STATUS_INVALID_PARAMETER;
1359 ev = samba_tevent_context_init(frame);
1361 status = NT_STATUS_NO_MEMORY;
1365 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst, replace);
1367 status = NT_STATUS_NO_MEMORY;
1371 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1375 status = cli_rename_recv(req);
1382 /****************************************************************************
1384 ****************************************************************************/
1386 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1388 struct cli_ntrename_internal_state {
1392 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1393 struct tevent_context *ev,
1394 struct cli_state *cli,
1395 const char *fname_src,
1396 const char *fname_dst,
1397 uint16_t rename_flag)
1399 struct tevent_req *req = NULL, *subreq = NULL;
1400 struct cli_ntrename_internal_state *state = NULL;
1401 uint8_t additional_flags = 0;
1402 uint16_t additional_flags2 = 0;
1403 uint8_t *bytes = NULL;
1405 req = tevent_req_create(mem_ctx, &state,
1406 struct cli_ntrename_internal_state);
1411 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1412 SSVAL(state->vwv+1, 0, rename_flag);
1414 bytes = talloc_array(state, uint8_t, 1);
1415 if (tevent_req_nomem(bytes, req)) {
1416 return tevent_req_post(req, ev);
1419 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1420 strlen(fname_src)+1, NULL);
1421 if (tevent_req_nomem(bytes, req)) {
1422 return tevent_req_post(req, ev);
1425 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1426 additional_flags2 = FLAGS2_REPARSE_PATH;
1429 bytes = talloc_realloc(state, bytes, uint8_t,
1430 talloc_get_size(bytes)+1);
1431 if (tevent_req_nomem(bytes, req)) {
1432 return tevent_req_post(req, ev);
1435 bytes[talloc_get_size(bytes)-1] = 4;
1436 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1437 strlen(fname_dst)+1, NULL);
1438 if (tevent_req_nomem(bytes, req)) {
1439 return tevent_req_post(req, ev);
1442 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1444 4, state->vwv, talloc_get_size(bytes), bytes);
1445 if (tevent_req_nomem(subreq, req)) {
1446 return tevent_req_post(req, ev);
1448 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1452 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1454 NTSTATUS status = cli_smb_recv(
1455 subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1456 tevent_req_simple_finish_ntstatus(subreq, status);
1459 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1461 return tevent_req_simple_recv_ntstatus(req);
1464 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1465 struct tevent_context *ev,
1466 struct cli_state *cli,
1467 const char *fname_src,
1468 const char *fname_dst)
1470 return cli_ntrename_internal_send(mem_ctx,
1475 RENAME_FLAG_RENAME);
1478 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1480 return cli_ntrename_internal_recv(req);
1483 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1485 TALLOC_CTX *frame = talloc_stackframe();
1486 struct tevent_context *ev;
1487 struct tevent_req *req;
1488 NTSTATUS status = NT_STATUS_OK;
1490 if (smbXcli_conn_has_async_calls(cli->conn)) {
1492 * Can't use sync call while an async call is in flight
1494 status = NT_STATUS_INVALID_PARAMETER;
1498 ev = samba_tevent_context_init(frame);
1500 status = NT_STATUS_NO_MEMORY;
1504 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1506 status = NT_STATUS_NO_MEMORY;
1510 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1514 status = cli_ntrename_recv(req);
1521 /****************************************************************************
1523 ****************************************************************************/
1525 static struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1526 struct tevent_context *ev,
1527 struct cli_state *cli,
1528 const char *fname_src,
1529 const char *fname_dst)
1531 return cli_ntrename_internal_send(mem_ctx,
1536 RENAME_FLAG_HARD_LINK);
1539 static NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1541 return cli_ntrename_internal_recv(req);
1544 struct cli_smb2_hardlink_state {
1545 struct tevent_context *ev;
1546 struct cli_state *cli;
1548 const char *fname_dst;
1553 static void cli_smb2_hardlink_opened(struct tevent_req *subreq);
1554 static void cli_smb2_hardlink_info_set(struct tevent_req *subreq);
1555 static void cli_smb2_hardlink_closed(struct tevent_req *subreq);
1557 static struct tevent_req *cli_smb2_hardlink_send(
1558 TALLOC_CTX *mem_ctx,
1559 struct tevent_context *ev,
1560 struct cli_state *cli,
1561 const char *fname_src,
1562 const char *fname_dst,
1564 struct smb2_create_blobs *in_cblobs)
1566 struct tevent_req *req = NULL, *subreq = NULL;
1567 struct cli_smb2_hardlink_state *state = NULL;
1569 req = tevent_req_create(
1570 mem_ctx, &state, struct cli_smb2_hardlink_state);
1576 state->fname_dst = fname_dst;
1577 state->overwrite = overwrite;
1579 subreq = cli_smb2_create_fnum_send(
1584 0, /* create_flags */
1585 SMB2_IMPERSONATION_IMPERSONATION,
1586 FILE_WRITE_ATTRIBUTES,
1587 0, /* file attributes */
1590 FILE_SHARE_DELETE, /* share_access */
1591 FILE_OPEN, /* create_disposition */
1592 FILE_NON_DIRECTORY_FILE, /* no hardlinks on directories */
1594 if (tevent_req_nomem(subreq, req)) {
1595 return tevent_req_post(req, ev);
1597 tevent_req_set_callback(subreq, cli_smb2_hardlink_opened, req);
1601 static void cli_smb2_hardlink_opened(struct tevent_req *subreq)
1603 struct tevent_req *req = tevent_req_callback_data(
1604 subreq, struct tevent_req);
1605 struct cli_smb2_hardlink_state *state = tevent_req_data(
1606 req, struct cli_smb2_hardlink_state);
1608 smb_ucs2_t *ucs2_dst;
1613 status = cli_smb2_create_fnum_recv(
1614 subreq, &state->fnum_src, NULL, NULL, NULL);
1615 TALLOC_FREE(subreq);
1616 if (tevent_req_nterror(req, status)) {
1620 ok = push_ucs2_talloc(state, &ucs2_dst, state->fname_dst, &ucs2_len);
1621 if (!ok || (ucs2_len < 2)) {
1622 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1625 /* Don't 0-terminate the name */
1628 inbuf = data_blob_talloc_zero(state, ucs2_len + 20);
1629 if (tevent_req_nomem(inbuf.data, req)) {
1633 if (state->overwrite) {
1634 SCVAL(inbuf.data, 0, 1);
1636 SIVAL(inbuf.data, 16, ucs2_len);
1637 memcpy(inbuf.data + 20, ucs2_dst, ucs2_len);
1638 TALLOC_FREE(ucs2_dst);
1640 subreq = cli_smb2_set_info_fnum_send(
1645 1, /* in_info_type */
1646 SMB_FILE_LINK_INFORMATION - 1000, /* in_file_info_class */
1648 0); /* in_additional_info */
1649 if (tevent_req_nomem(subreq, req)) {
1652 tevent_req_set_callback(subreq, cli_smb2_hardlink_info_set, req);
1655 static void cli_smb2_hardlink_info_set(struct tevent_req *subreq)
1657 struct tevent_req *req = tevent_req_callback_data(
1658 subreq, struct tevent_req);
1659 struct cli_smb2_hardlink_state *state = tevent_req_data(
1660 req, struct cli_smb2_hardlink_state);
1662 state->status = cli_smb2_set_info_fnum_recv(subreq);
1663 TALLOC_FREE(subreq);
1665 /* ignore error here, we need to close the file */
1667 subreq = cli_smb2_close_fnum_send(
1668 state, state->ev, state->cli, state->fnum_src);
1669 if (tevent_req_nomem(subreq, req)) {
1672 tevent_req_set_callback(subreq, cli_smb2_hardlink_closed, req);
1675 static void cli_smb2_hardlink_closed(struct tevent_req *subreq)
1677 NTSTATUS status = cli_smb2_close_fnum_recv(subreq);
1678 tevent_req_simple_finish_ntstatus(subreq, status);
1681 static NTSTATUS cli_smb2_hardlink_recv(struct tevent_req *req)
1683 struct cli_smb2_hardlink_state *state = tevent_req_data(
1684 req, struct cli_smb2_hardlink_state);
1687 if (tevent_req_is_nterror(req, &status)) {
1690 return state->status;
1693 struct cli_hardlink_state {
1697 static void cli_hardlink_done(struct tevent_req *subreq);
1698 static void cli_hardlink_done2(struct tevent_req *subreq);
1700 struct tevent_req *cli_hardlink_send(
1701 TALLOC_CTX *mem_ctx,
1702 struct tevent_context *ev,
1703 struct cli_state *cli,
1704 const char *fname_src,
1705 const char *fname_dst)
1707 struct tevent_req *req = NULL, *subreq = NULL;
1708 struct cli_hardlink_state *state;
1710 req = tevent_req_create(mem_ctx, &state, struct cli_hardlink_state);
1715 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1716 subreq = cli_smb2_hardlink_send(
1717 state, ev, cli, fname_src, fname_dst, false, NULL);
1718 if (tevent_req_nomem(subreq, req)) {
1719 return tevent_req_post(req, ev);
1721 tevent_req_set_callback(subreq, cli_hardlink_done2, req);
1725 subreq = cli_nt_hardlink_send(state, ev, cli, fname_src, fname_dst);
1726 if (tevent_req_nomem(subreq, req)) {
1727 return tevent_req_post(req, ev);
1729 tevent_req_set_callback(subreq, cli_hardlink_done, req);
1733 static void cli_hardlink_done(struct tevent_req *subreq)
1735 NTSTATUS status = cli_nt_hardlink_recv(subreq);
1736 tevent_req_simple_finish_ntstatus(subreq, status);
1739 static void cli_hardlink_done2(struct tevent_req *subreq)
1741 NTSTATUS status = cli_smb2_hardlink_recv(subreq);
1742 tevent_req_simple_finish_ntstatus(subreq, status);
1745 NTSTATUS cli_hardlink_recv(struct tevent_req *req)
1747 return tevent_req_simple_recv_ntstatus(req);
1750 NTSTATUS cli_hardlink(
1751 struct cli_state *cli, const char *fname_src, const char *fname_dst)
1753 TALLOC_CTX *frame = talloc_stackframe();
1754 struct tevent_context *ev = NULL;
1755 struct tevent_req *req = NULL;
1756 NTSTATUS status = NT_STATUS_NO_MEMORY;
1758 if (smbXcli_conn_has_async_calls(cli->conn)) {
1759 status = NT_STATUS_INVALID_PARAMETER;
1762 ev = samba_tevent_context_init(frame);
1766 req = cli_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1770 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1773 status = cli_hardlink_recv(req);
1779 /****************************************************************************
1781 ****************************************************************************/
1783 static void cli_unlink_done(struct tevent_req *subreq);
1785 struct cli_unlink_state {
1789 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1790 struct tevent_context *ev,
1791 struct cli_state *cli,
1793 uint16_t mayhave_attrs)
1795 struct tevent_req *req = NULL, *subreq = NULL;
1796 struct cli_unlink_state *state = NULL;
1797 uint8_t additional_flags = 0;
1798 uint16_t additional_flags2 = 0;
1799 uint8_t *bytes = NULL;
1801 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1806 SSVAL(state->vwv+0, 0, mayhave_attrs);
1808 bytes = talloc_array(state, uint8_t, 1);
1809 if (tevent_req_nomem(bytes, req)) {
1810 return tevent_req_post(req, ev);
1813 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1814 strlen(fname)+1, NULL);
1816 if (tevent_req_nomem(bytes, req)) {
1817 return tevent_req_post(req, ev);
1820 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1821 additional_flags2 = FLAGS2_REPARSE_PATH;
1824 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1826 1, state->vwv, talloc_get_size(bytes), bytes);
1827 if (tevent_req_nomem(subreq, req)) {
1828 return tevent_req_post(req, ev);
1830 tevent_req_set_callback(subreq, cli_unlink_done, req);
1834 static void cli_unlink_done(struct tevent_req *subreq)
1836 struct tevent_req *req = tevent_req_callback_data(
1837 subreq, struct tevent_req);
1840 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1841 TALLOC_FREE(subreq);
1842 if (tevent_req_nterror(req, status)) {
1845 tevent_req_done(req);
1848 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1850 return tevent_req_simple_recv_ntstatus(req);
1853 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1855 TALLOC_CTX *frame = NULL;
1856 struct tevent_context *ev;
1857 struct tevent_req *req;
1858 NTSTATUS status = NT_STATUS_OK;
1860 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1861 return cli_smb2_unlink(cli, fname, NULL);
1864 frame = talloc_stackframe();
1866 if (smbXcli_conn_has_async_calls(cli->conn)) {
1868 * Can't use sync call while an async call is in flight
1870 status = NT_STATUS_INVALID_PARAMETER;
1874 ev = samba_tevent_context_init(frame);
1876 status = NT_STATUS_NO_MEMORY;
1880 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1882 status = NT_STATUS_NO_MEMORY;
1886 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1890 status = cli_unlink_recv(req);
1897 /****************************************************************************
1899 ****************************************************************************/
1901 static void cli_mkdir_done(struct tevent_req *subreq);
1903 struct cli_mkdir_state {
1907 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1908 struct tevent_context *ev,
1909 struct cli_state *cli,
1912 struct tevent_req *req = NULL, *subreq = NULL;
1913 struct cli_mkdir_state *state = NULL;
1914 uint8_t additional_flags = 0;
1915 uint16_t additional_flags2 = 0;
1916 uint8_t *bytes = NULL;
1918 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1923 bytes = talloc_array(state, uint8_t, 1);
1924 if (tevent_req_nomem(bytes, req)) {
1925 return tevent_req_post(req, ev);
1928 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1929 strlen(dname)+1, NULL);
1931 if (tevent_req_nomem(bytes, req)) {
1932 return tevent_req_post(req, ev);
1935 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1936 additional_flags2 = FLAGS2_REPARSE_PATH;
1939 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1941 0, NULL, talloc_get_size(bytes), bytes);
1942 if (tevent_req_nomem(subreq, req)) {
1943 return tevent_req_post(req, ev);
1945 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1949 static void cli_mkdir_done(struct tevent_req *subreq)
1951 struct tevent_req *req = tevent_req_callback_data(
1952 subreq, struct tevent_req);
1955 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1956 TALLOC_FREE(subreq);
1957 if (tevent_req_nterror(req, status)) {
1960 tevent_req_done(req);
1963 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1965 return tevent_req_simple_recv_ntstatus(req);
1968 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1970 TALLOC_CTX *frame = NULL;
1971 struct tevent_context *ev;
1972 struct tevent_req *req;
1973 NTSTATUS status = NT_STATUS_OK;
1975 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1976 return cli_smb2_mkdir(cli, dname);
1979 frame = talloc_stackframe();
1981 if (smbXcli_conn_has_async_calls(cli->conn)) {
1983 * Can't use sync call while an async call is in flight
1985 status = NT_STATUS_INVALID_PARAMETER;
1989 ev = samba_tevent_context_init(frame);
1991 status = NT_STATUS_NO_MEMORY;
1995 req = cli_mkdir_send(frame, ev, cli, dname);
1997 status = NT_STATUS_NO_MEMORY;
2001 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2005 status = cli_mkdir_recv(req);
2012 /****************************************************************************
2014 ****************************************************************************/
2016 static void cli_rmdir_done(struct tevent_req *subreq);
2018 struct cli_rmdir_state {
2022 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
2023 struct tevent_context *ev,
2024 struct cli_state *cli,
2027 struct tevent_req *req = NULL, *subreq = NULL;
2028 struct cli_rmdir_state *state = NULL;
2029 uint8_t additional_flags = 0;
2030 uint16_t additional_flags2 = 0;
2031 uint8_t *bytes = NULL;
2033 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
2038 bytes = talloc_array(state, uint8_t, 1);
2039 if (tevent_req_nomem(bytes, req)) {
2040 return tevent_req_post(req, ev);
2043 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
2044 strlen(dname)+1, NULL);
2046 if (tevent_req_nomem(bytes, req)) {
2047 return tevent_req_post(req, ev);
2050 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
2051 additional_flags2 = FLAGS2_REPARSE_PATH;
2054 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
2056 0, NULL, talloc_get_size(bytes), bytes);
2057 if (tevent_req_nomem(subreq, req)) {
2058 return tevent_req_post(req, ev);
2060 tevent_req_set_callback(subreq, cli_rmdir_done, req);
2064 static void cli_rmdir_done(struct tevent_req *subreq)
2066 struct tevent_req *req = tevent_req_callback_data(
2067 subreq, struct tevent_req);
2070 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2071 TALLOC_FREE(subreq);
2072 if (tevent_req_nterror(req, status)) {
2075 tevent_req_done(req);
2078 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
2080 return tevent_req_simple_recv_ntstatus(req);
2083 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
2085 TALLOC_CTX *frame = NULL;
2086 struct tevent_context *ev;
2087 struct tevent_req *req;
2088 NTSTATUS status = NT_STATUS_OK;
2090 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2091 return cli_smb2_rmdir(cli, dname, NULL);
2094 frame = talloc_stackframe();
2096 if (smbXcli_conn_has_async_calls(cli->conn)) {
2098 * Can't use sync call while an async call is in flight
2100 status = NT_STATUS_INVALID_PARAMETER;
2104 ev = samba_tevent_context_init(frame);
2106 status = NT_STATUS_NO_MEMORY;
2110 req = cli_rmdir_send(frame, ev, cli, dname);
2112 status = NT_STATUS_NO_MEMORY;
2116 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2120 status = cli_rmdir_recv(req);
2127 /****************************************************************************
2128 Set or clear the delete on close flag.
2129 ****************************************************************************/
2137 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq);
2138 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq);
2140 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
2141 struct tevent_context *ev,
2142 struct cli_state *cli,
2146 struct tevent_req *req = NULL, *subreq = NULL;
2147 struct doc_state *state = NULL;
2149 req = tevent_req_create(mem_ctx, &state, struct doc_state);
2154 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2155 subreq = cli_smb2_delete_on_close_send(state, ev, cli,
2157 if (tevent_req_nomem(subreq, req)) {
2158 return tevent_req_post(req, ev);
2160 tevent_req_set_callback(subreq,
2161 cli_nt_delete_on_close_smb2_done,
2166 /* Setup setup word. */
2167 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2169 /* Setup param array. */
2170 SSVAL(state->param,0,fnum);
2171 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
2173 /* Setup data array. */
2174 SCVAL(&state->data[0], 0, flag ? 1 : 0);
2176 subreq = cli_trans_send(state, /* mem ctx. */
2177 ev, /* event ctx. */
2178 cli, /* cli_state. */
2179 0, /* additional_flags2 */
2180 SMBtrans2, /* cmd. */
2181 NULL, /* pipe name. */
2185 &state->setup, /* setup. */
2186 1, /* num setup uint16_t words. */
2187 0, /* max returned setup. */
2188 state->param, /* param. */
2190 2, /* max returned param. */
2191 state->data, /* data. */
2193 0); /* max returned data. */
2195 if (tevent_req_nomem(subreq, req)) {
2196 return tevent_req_post(req, ev);
2198 tevent_req_set_callback(subreq,
2199 cli_nt_delete_on_close_smb1_done,
2204 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq)
2206 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2207 NULL, 0, NULL, NULL, 0, NULL);
2208 tevent_req_simple_finish_ntstatus(subreq, status);
2211 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq)
2213 NTSTATUS status = cli_smb2_delete_on_close_recv(subreq);
2214 tevent_req_simple_finish_ntstatus(subreq, status);
2217 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
2219 return tevent_req_simple_recv_ntstatus(req);
2222 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
2224 TALLOC_CTX *frame = talloc_stackframe();
2225 struct tevent_context *ev = NULL;
2226 struct tevent_req *req = NULL;
2227 NTSTATUS status = NT_STATUS_OK;
2229 if (smbXcli_conn_has_async_calls(cli->conn)) {
2231 * Can't use sync call while an async call is in flight
2233 status = NT_STATUS_INVALID_PARAMETER;
2237 ev = samba_tevent_context_init(frame);
2239 status = NT_STATUS_NO_MEMORY;
2243 req = cli_nt_delete_on_close_send(frame,
2249 status = NT_STATUS_NO_MEMORY;
2253 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2257 status = cli_nt_delete_on_close_recv(req);
2264 struct cli_ntcreate1_state {
2267 struct smb_create_returns cr;
2268 struct tevent_req *subreq;
2271 static void cli_ntcreate1_done(struct tevent_req *subreq);
2272 static bool cli_ntcreate1_cancel(struct tevent_req *req);
2274 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
2275 struct tevent_context *ev,
2276 struct cli_state *cli,
2278 uint32_t CreatFlags,
2279 uint32_t DesiredAccess,
2280 uint32_t FileAttributes,
2281 uint32_t ShareAccess,
2282 uint32_t CreateDisposition,
2283 uint32_t CreateOptions,
2284 uint32_t ImpersonationLevel,
2285 uint8_t SecurityFlags)
2287 struct tevent_req *req, *subreq;
2288 struct cli_ntcreate1_state *state;
2291 size_t converted_len;
2292 uint16_t additional_flags2 = 0;
2294 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
2301 SCVAL(vwv+0, 0, 0xFF);
2306 if (cli->use_oplocks) {
2307 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
2309 SIVAL(vwv+3, 1, CreatFlags);
2310 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
2311 SIVAL(vwv+7, 1, DesiredAccess);
2312 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
2313 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
2314 SIVAL(vwv+13, 1, FileAttributes);
2315 SIVAL(vwv+15, 1, ShareAccess);
2316 SIVAL(vwv+17, 1, CreateDisposition);
2317 SIVAL(vwv+19, 1, CreateOptions |
2318 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2319 SIVAL(vwv+21, 1, ImpersonationLevel);
2320 SCVAL(vwv+23, 1, SecurityFlags);
2322 bytes = talloc_array(state, uint8_t, 0);
2323 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
2324 fname, strlen(fname)+1,
2327 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2328 additional_flags2 = FLAGS2_REPARSE_PATH;
2331 /* sigh. this copes with broken netapp filer behaviour */
2332 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
2334 if (tevent_req_nomem(bytes, req)) {
2335 return tevent_req_post(req, ev);
2338 SSVAL(vwv+2, 1, converted_len);
2340 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
2341 additional_flags2, 24, vwv,
2342 talloc_get_size(bytes), bytes);
2343 if (tevent_req_nomem(subreq, req)) {
2344 return tevent_req_post(req, ev);
2346 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
2348 state->subreq = subreq;
2349 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
2354 static void cli_ntcreate1_done(struct tevent_req *subreq)
2356 struct tevent_req *req = tevent_req_callback_data(
2357 subreq, struct tevent_req);
2358 struct cli_ntcreate1_state *state = tevent_req_data(
2359 req, struct cli_ntcreate1_state);
2366 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2367 &num_bytes, &bytes);
2368 TALLOC_FREE(subreq);
2369 if (tevent_req_nterror(req, status)) {
2372 state->cr.oplock_level = CVAL(vwv+2, 0);
2373 state->fnum = SVAL(vwv+2, 1);
2374 state->cr.create_action = IVAL(vwv+3, 1);
2375 state->cr.creation_time = BVAL(vwv+5, 1);
2376 state->cr.last_access_time = BVAL(vwv+9, 1);
2377 state->cr.last_write_time = BVAL(vwv+13, 1);
2378 state->cr.change_time = BVAL(vwv+17, 1);
2379 state->cr.file_attributes = IVAL(vwv+21, 1);
2380 state->cr.allocation_size = BVAL(vwv+23, 1);
2381 state->cr.end_of_file = BVAL(vwv+27, 1);
2383 tevent_req_done(req);
2386 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2388 struct cli_ntcreate1_state *state = tevent_req_data(
2389 req, struct cli_ntcreate1_state);
2390 return tevent_req_cancel(state->subreq);
2393 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2395 struct smb_create_returns *cr)
2397 struct cli_ntcreate1_state *state = tevent_req_data(
2398 req, struct cli_ntcreate1_state);
2401 if (tevent_req_is_nterror(req, &status)) {
2404 *pfnum = state->fnum;
2408 return NT_STATUS_OK;
2411 struct cli_ntcreate_state {
2412 struct smb_create_returns cr;
2414 struct tevent_req *subreq;
2417 static void cli_ntcreate_done_nt1(struct tevent_req *subreq);
2418 static void cli_ntcreate_done_smb2(struct tevent_req *subreq);
2419 static bool cli_ntcreate_cancel(struct tevent_req *req);
2421 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2422 struct tevent_context *ev,
2423 struct cli_state *cli,
2425 uint32_t create_flags,
2426 uint32_t desired_access,
2427 uint32_t file_attributes,
2428 uint32_t share_access,
2429 uint32_t create_disposition,
2430 uint32_t create_options,
2431 uint32_t impersonation_level,
2432 uint8_t security_flags)
2434 struct tevent_req *req, *subreq;
2435 struct cli_ntcreate_state *state;
2437 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2442 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2443 if (cli->use_oplocks) {
2444 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2447 subreq = cli_smb2_create_fnum_send(
2453 impersonation_level,
2460 if (tevent_req_nomem(subreq, req)) {
2461 return tevent_req_post(req, ev);
2463 tevent_req_set_callback(subreq, cli_ntcreate_done_smb2, req);
2465 subreq = cli_ntcreate1_send(
2466 state, ev, cli, fname, create_flags, desired_access,
2467 file_attributes, share_access, create_disposition,
2468 create_options, impersonation_level, security_flags);
2469 if (tevent_req_nomem(subreq, req)) {
2470 return tevent_req_post(req, ev);
2472 tevent_req_set_callback(subreq, cli_ntcreate_done_nt1, req);
2475 state->subreq = subreq;
2476 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2481 static void cli_ntcreate_done_nt1(struct tevent_req *subreq)
2483 struct tevent_req *req = tevent_req_callback_data(
2484 subreq, struct tevent_req);
2485 struct cli_ntcreate_state *state = tevent_req_data(
2486 req, struct cli_ntcreate_state);
2489 status = cli_ntcreate1_recv(subreq, &state->fnum, &state->cr);
2490 TALLOC_FREE(subreq);
2491 if (tevent_req_nterror(req, status)) {
2494 tevent_req_done(req);
2497 static void cli_ntcreate_done_smb2(struct tevent_req *subreq)
2499 struct tevent_req *req = tevent_req_callback_data(
2500 subreq, struct tevent_req);
2501 struct cli_ntcreate_state *state = tevent_req_data(
2502 req, struct cli_ntcreate_state);
2505 status = cli_smb2_create_fnum_recv(
2511 TALLOC_FREE(subreq);
2512 if (tevent_req_nterror(req, status)) {
2515 tevent_req_done(req);
2518 static bool cli_ntcreate_cancel(struct tevent_req *req)
2520 struct cli_ntcreate_state *state = tevent_req_data(
2521 req, struct cli_ntcreate_state);
2522 return tevent_req_cancel(state->subreq);
2525 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2526 struct smb_create_returns *cr)
2528 struct cli_ntcreate_state *state = tevent_req_data(
2529 req, struct cli_ntcreate_state);
2532 if (tevent_req_is_nterror(req, &status)) {
2536 *fnum = state->fnum;
2541 return NT_STATUS_OK;
2544 NTSTATUS cli_ntcreate(struct cli_state *cli,
2546 uint32_t CreatFlags,
2547 uint32_t DesiredAccess,
2548 uint32_t FileAttributes,
2549 uint32_t ShareAccess,
2550 uint32_t CreateDisposition,
2551 uint32_t CreateOptions,
2552 uint8_t SecurityFlags,
2554 struct smb_create_returns *cr)
2556 TALLOC_CTX *frame = talloc_stackframe();
2557 struct tevent_context *ev;
2558 struct tevent_req *req;
2559 uint32_t ImpersonationLevel = SMB2_IMPERSONATION_IMPERSONATION;
2560 NTSTATUS status = NT_STATUS_NO_MEMORY;
2562 if (smbXcli_conn_has_async_calls(cli->conn)) {
2564 * Can't use sync call while an async call is in flight
2566 status = NT_STATUS_INVALID_PARAMETER;
2570 ev = samba_tevent_context_init(frame);
2575 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2576 DesiredAccess, FileAttributes, ShareAccess,
2577 CreateDisposition, CreateOptions,
2578 ImpersonationLevel, SecurityFlags);
2583 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2587 status = cli_ntcreate_recv(req, pfid, cr);
2593 struct cli_nttrans_create_state {
2595 struct smb_create_returns cr;
2598 static void cli_nttrans_create_done(struct tevent_req *subreq);
2600 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2601 struct tevent_context *ev,
2602 struct cli_state *cli,
2604 uint32_t CreatFlags,
2605 uint32_t DesiredAccess,
2606 uint32_t FileAttributes,
2607 uint32_t ShareAccess,
2608 uint32_t CreateDisposition,
2609 uint32_t CreateOptions,
2610 uint8_t SecurityFlags,
2611 struct security_descriptor *secdesc,
2612 struct ea_struct *eas,
2615 struct tevent_req *req, *subreq;
2616 struct cli_nttrans_create_state *state;
2618 uint8_t *secdesc_buf;
2621 size_t converted_len;
2622 uint16_t additional_flags2 = 0;
2624 req = tevent_req_create(mem_ctx,
2625 &state, struct cli_nttrans_create_state);
2630 if (secdesc != NULL) {
2631 status = marshall_sec_desc(talloc_tos(), secdesc,
2632 &secdesc_buf, &secdesc_len);
2633 if (tevent_req_nterror(req, status)) {
2634 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2635 nt_errstr(status)));
2636 return tevent_req_post(req, ev);
2647 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2648 return tevent_req_post(req, ev);
2651 param = talloc_array(state, uint8_t, 53);
2652 if (tevent_req_nomem(param, req)) {
2653 return tevent_req_post(req, ev);
2656 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2657 fname, strlen(fname),
2659 if (tevent_req_nomem(param, req)) {
2660 return tevent_req_post(req, ev);
2663 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2664 additional_flags2 = FLAGS2_REPARSE_PATH;
2667 SIVAL(param, 0, CreatFlags);
2668 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2669 SIVAL(param, 8, DesiredAccess);
2670 SIVAL(param, 12, 0x0); /* AllocationSize */
2671 SIVAL(param, 16, 0x0); /* AllocationSize */
2672 SIVAL(param, 20, FileAttributes);
2673 SIVAL(param, 24, ShareAccess);
2674 SIVAL(param, 28, CreateDisposition);
2675 SIVAL(param, 32, CreateOptions |
2676 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2677 SIVAL(param, 36, secdesc_len);
2678 SIVAL(param, 40, 0); /* EA length*/
2679 SIVAL(param, 44, converted_len);
2680 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2681 SCVAL(param, 52, SecurityFlags);
2683 subreq = cli_trans_send(state, ev, cli,
2684 additional_flags2, /* additional_flags2 */
2686 NULL, -1, /* name, fid */
2687 NT_TRANSACT_CREATE, 0,
2688 NULL, 0, 0, /* setup */
2689 param, talloc_get_size(param), 128, /* param */
2690 secdesc_buf, secdesc_len, 0); /* data */
2691 if (tevent_req_nomem(subreq, req)) {
2692 return tevent_req_post(req, ev);
2694 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2698 static void cli_nttrans_create_done(struct tevent_req *subreq)
2700 struct tevent_req *req = tevent_req_callback_data(
2701 subreq, struct tevent_req);
2702 struct cli_nttrans_create_state *state = tevent_req_data(
2703 req, struct cli_nttrans_create_state);
2708 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2709 NULL, 0, NULL, /* rsetup */
2710 ¶m, 69, &num_param,
2712 if (tevent_req_nterror(req, status)) {
2715 state->cr.oplock_level = CVAL(param, 0);
2716 state->fnum = SVAL(param, 2);
2717 state->cr.create_action = IVAL(param, 4);
2718 state->cr.creation_time = BVAL(param, 12);
2719 state->cr.last_access_time = BVAL(param, 20);
2720 state->cr.last_write_time = BVAL(param, 28);
2721 state->cr.change_time = BVAL(param, 36);
2722 state->cr.file_attributes = IVAL(param, 44);
2723 state->cr.allocation_size = BVAL(param, 48);
2724 state->cr.end_of_file = BVAL(param, 56);
2727 tevent_req_done(req);
2730 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2732 struct smb_create_returns *cr)
2734 struct cli_nttrans_create_state *state = tevent_req_data(
2735 req, struct cli_nttrans_create_state);
2738 if (tevent_req_is_nterror(req, &status)) {
2741 *fnum = state->fnum;
2745 return NT_STATUS_OK;
2748 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2750 uint32_t CreatFlags,
2751 uint32_t DesiredAccess,
2752 uint32_t FileAttributes,
2753 uint32_t ShareAccess,
2754 uint32_t CreateDisposition,
2755 uint32_t CreateOptions,
2756 uint8_t SecurityFlags,
2757 struct security_descriptor *secdesc,
2758 struct ea_struct *eas,
2761 struct smb_create_returns *cr)
2763 TALLOC_CTX *frame = talloc_stackframe();
2764 struct tevent_context *ev;
2765 struct tevent_req *req;
2766 NTSTATUS status = NT_STATUS_NO_MEMORY;
2768 if (smbXcli_conn_has_async_calls(cli->conn)) {
2770 * Can't use sync call while an async call is in flight
2772 status = NT_STATUS_INVALID_PARAMETER;
2775 ev = samba_tevent_context_init(frame);
2779 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2780 DesiredAccess, FileAttributes,
2781 ShareAccess, CreateDisposition,
2782 CreateOptions, SecurityFlags,
2783 secdesc, eas, num_eas);
2787 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2790 status = cli_nttrans_create_recv(req, pfid, cr);
2796 /****************************************************************************
2798 WARNING: if you open with O_WRONLY then getattrE won't work!
2799 ****************************************************************************/
2801 struct cli_openx_state {
2808 static void cli_openx_done(struct tevent_req *subreq);
2810 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2811 struct tevent_context *ev,
2812 struct cli_state *cli, const char *fname,
2813 int flags, int share_mode,
2814 struct tevent_req **psmbreq)
2816 struct tevent_req *req, *subreq;
2817 struct cli_openx_state *state;
2819 unsigned accessmode;
2820 uint8_t additional_flags;
2821 uint16_t additional_flags2 = 0;
2824 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2830 if (flags & O_CREAT) {
2833 if (!(flags & O_EXCL)) {
2834 if (flags & O_TRUNC)
2840 accessmode = (share_mode<<4);
2842 if ((flags & O_ACCMODE) == O_RDWR) {
2844 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2849 if ((flags & O_SYNC) == O_SYNC) {
2850 accessmode |= (1<<14);
2854 if (share_mode == DENY_FCB) {
2858 SCVAL(state->vwv + 0, 0, 0xFF);
2859 SCVAL(state->vwv + 0, 1, 0);
2860 SSVAL(state->vwv + 1, 0, 0);
2861 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2862 SSVAL(state->vwv + 3, 0, accessmode);
2863 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2864 SSVAL(state->vwv + 5, 0, 0);
2865 SIVAL(state->vwv + 6, 0, 0);
2866 SSVAL(state->vwv + 8, 0, openfn);
2867 SIVAL(state->vwv + 9, 0, 0);
2868 SIVAL(state->vwv + 11, 0, 0);
2869 SIVAL(state->vwv + 13, 0, 0);
2871 additional_flags = 0;
2873 if (cli->use_oplocks) {
2874 /* if using oplocks then ask for a batch oplock via
2875 core and extended methods */
2877 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2878 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2881 bytes = talloc_array(state, uint8_t, 0);
2882 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2883 strlen(fname)+1, NULL);
2885 if (tevent_req_nomem(bytes, req)) {
2886 return tevent_req_post(req, ev);
2889 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2890 additional_flags2 = FLAGS2_REPARSE_PATH;
2893 state->bytes.iov_base = (void *)bytes;
2894 state->bytes.iov_len = talloc_get_size(bytes);
2896 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2897 additional_flags2, 15, state->vwv, 1, &state->bytes);
2898 if (subreq == NULL) {
2902 tevent_req_set_callback(subreq, cli_openx_done, req);
2907 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2908 struct cli_state *cli, const char *fname,
2909 int flags, int share_mode)
2911 struct tevent_req *req, *subreq;
2914 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2920 status = smb1cli_req_chain_submit(&subreq, 1);
2921 if (tevent_req_nterror(req, status)) {
2922 return tevent_req_post(req, ev);
2927 static void cli_openx_done(struct tevent_req *subreq)
2929 struct tevent_req *req = tevent_req_callback_data(
2930 subreq, struct tevent_req);
2931 struct cli_openx_state *state = tevent_req_data(
2932 req, struct cli_openx_state);
2937 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2939 TALLOC_FREE(subreq);
2940 if (tevent_req_nterror(req, status)) {
2943 state->fnum = SVAL(vwv+2, 0);
2944 tevent_req_done(req);
2947 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2949 struct cli_openx_state *state = tevent_req_data(
2950 req, struct cli_openx_state);
2953 if (tevent_req_is_nterror(req, &status)) {
2956 *pfnum = state->fnum;
2957 return NT_STATUS_OK;
2960 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2961 int share_mode, uint16_t *pfnum)
2963 TALLOC_CTX *frame = talloc_stackframe();
2964 struct tevent_context *ev;
2965 struct tevent_req *req;
2966 NTSTATUS status = NT_STATUS_NO_MEMORY;
2968 if (smbXcli_conn_has_async_calls(cli->conn)) {
2970 * Can't use sync call while an async call is in flight
2972 status = NT_STATUS_INVALID_PARAMETER;
2976 ev = samba_tevent_context_init(frame);
2981 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2986 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2990 status = cli_openx_recv(req, pfnum);
2995 /****************************************************************************
2996 Synchronous wrapper function that does an NtCreateX open by preference
2997 and falls back to openX if this fails.
2998 ****************************************************************************/
3000 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
3001 int share_mode_in, uint16_t *pfnum)
3004 unsigned int openfn = 0;
3005 unsigned int dos_deny = 0;
3006 uint32_t access_mask, share_mode, create_disposition, create_options;
3007 struct smb_create_returns cr = {0};
3009 /* Do the initial mapping into OpenX parameters. */
3010 if (flags & O_CREAT) {
3013 if (!(flags & O_EXCL)) {
3014 if (flags & O_TRUNC)
3020 dos_deny = (share_mode_in<<4);
3022 if ((flags & O_ACCMODE) == O_RDWR) {
3024 } else if ((flags & O_ACCMODE) == O_WRONLY) {
3029 if ((flags & O_SYNC) == O_SYNC) {
3030 dos_deny |= (1<<14);
3034 if (share_mode_in == DENY_FCB) {
3039 /* Hmmm. This is what I think the above code
3040 should look like if it's using the constants
3043 if (flags & O_CREAT) {
3044 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
3046 if (!(flags & O_EXCL)) {
3047 if (flags & O_TRUNC)
3048 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
3050 openfn |= OPENX_FILE_EXISTS_OPEN;
3053 dos_deny = SET_DENY_MODE(share_mode_in);
3055 if ((flags & O_ACCMODE) == O_RDWR) {
3056 dos_deny |= DOS_OPEN_RDWR;
3057 } else if ((flags & O_ACCMODE) == O_WRONLY) {
3058 dos_deny |= DOS_OPEN_WRONLY;
3062 if ((flags & O_SYNC) == O_SYNC) {
3063 dos_deny |= FILE_SYNC_OPENMODE;
3067 if (share_mode_in == DENY_FCB) {
3072 if (!map_open_params_to_ntcreate(fname, dos_deny,
3073 openfn, &access_mask,
3074 &share_mode, &create_disposition,
3075 &create_options, NULL)) {
3079 status = cli_ntcreate(cli,
3091 /* Try and cope will all varients of "we don't do this call"
3092 and fall back to openX. */
3094 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
3095 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
3096 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
3097 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
3098 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
3099 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
3100 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
3101 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
3102 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
3106 if (NT_STATUS_IS_OK(status) &&
3107 (create_options & FILE_NON_DIRECTORY_FILE) &&
3108 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
3111 * Some (broken) servers return a valid handle
3112 * for directories even if FILE_NON_DIRECTORY_FILE
3113 * is set. Just close the handle and set the
3114 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
3116 status = cli_close(cli, *pfnum);
3117 if (!NT_STATUS_IS_OK(status)) {
3120 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3121 /* Set this so libsmbclient can retrieve it. */
3122 cli->raw_status = status;
3129 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
3132 /****************************************************************************
3134 ****************************************************************************/
3136 struct cli_smb1_close_state {
3140 static void cli_smb1_close_done(struct tevent_req *subreq);
3142 struct tevent_req *cli_smb1_close_create(TALLOC_CTX *mem_ctx,
3143 struct tevent_context *ev,
3144 struct cli_state *cli,
3146 struct tevent_req **psubreq)
3148 struct tevent_req *req, *subreq;
3149 struct cli_smb1_close_state *state;
3151 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_close_state);
3156 SSVAL(state->vwv+0, 0, fnum);
3157 SIVALS(state->vwv+1, 0, -1);
3159 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
3160 3, state->vwv, 0, NULL);
3161 if (subreq == NULL) {
3165 tevent_req_set_callback(subreq, cli_smb1_close_done, req);
3170 static void cli_smb1_close_done(struct tevent_req *subreq)
3172 struct tevent_req *req = tevent_req_callback_data(
3173 subreq, struct tevent_req);
3176 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3177 TALLOC_FREE(subreq);
3178 if (tevent_req_nterror(req, status)) {
3181 tevent_req_done(req);
3184 struct cli_close_state {
3188 static void cli_close_done(struct tevent_req *subreq);
3190 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
3191 struct tevent_context *ev,
3192 struct cli_state *cli,
3195 struct tevent_req *req, *subreq;
3196 struct cli_close_state *state;
3199 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
3204 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3205 subreq = cli_smb2_close_fnum_send(state,
3209 if (tevent_req_nomem(subreq, req)) {
3210 return tevent_req_post(req, ev);
3213 struct tevent_req *ch_req = NULL;
3214 subreq = cli_smb1_close_create(state, ev, cli, fnum, &ch_req);
3215 if (tevent_req_nomem(subreq, req)) {
3216 return tevent_req_post(req, ev);
3218 status = smb1cli_req_chain_submit(&ch_req, 1);
3219 if (tevent_req_nterror(req, status)) {
3220 return tevent_req_post(req, ev);
3224 tevent_req_set_callback(subreq, cli_close_done, req);
3228 static void cli_close_done(struct tevent_req *subreq)
3230 struct tevent_req *req = tevent_req_callback_data(
3231 subreq, struct tevent_req);
3232 NTSTATUS status = NT_STATUS_OK;
3233 bool err = tevent_req_is_nterror(subreq, &status);
3235 TALLOC_FREE(subreq);
3237 tevent_req_nterror(req, status);
3240 tevent_req_done(req);
3243 NTSTATUS cli_close_recv(struct tevent_req *req)
3245 return tevent_req_simple_recv_ntstatus(req);
3248 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
3250 TALLOC_CTX *frame = NULL;
3251 struct tevent_context *ev;
3252 struct tevent_req *req;
3253 NTSTATUS status = NT_STATUS_OK;
3255 frame = talloc_stackframe();
3257 if (smbXcli_conn_has_async_calls(cli->conn)) {
3259 * Can't use sync call while an async call is in flight
3261 status = NT_STATUS_INVALID_PARAMETER;
3265 ev = samba_tevent_context_init(frame);
3267 status = NT_STATUS_NO_MEMORY;
3271 req = cli_close_send(frame, ev, cli, fnum);
3273 status = NT_STATUS_NO_MEMORY;
3277 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3281 status = cli_close_recv(req);
3287 /****************************************************************************
3288 Truncate a file to a specified size
3289 ****************************************************************************/
3291 struct ftrunc_state {
3297 static void cli_ftruncate_done(struct tevent_req *subreq)
3299 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3300 NULL, 0, NULL, NULL, 0, NULL);
3301 tevent_req_simple_finish_ntstatus(subreq, status);
3304 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
3305 struct tevent_context *ev,
3306 struct cli_state *cli,
3310 struct tevent_req *req = NULL, *subreq = NULL;
3311 struct ftrunc_state *state = NULL;
3313 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
3318 /* Setup setup word. */
3319 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3321 /* Setup param array. */
3322 SSVAL(state->param,0,fnum);
3323 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
3324 SSVAL(state->param,4,0);
3326 /* Setup data array. */
3327 SBVAL(state->data, 0, size);
3329 subreq = cli_trans_send(state, /* mem ctx. */
3330 ev, /* event ctx. */
3331 cli, /* cli_state. */
3332 0, /* additional_flags2 */
3333 SMBtrans2, /* cmd. */
3334 NULL, /* pipe name. */
3338 &state->setup, /* setup. */
3339 1, /* num setup uint16_t words. */
3340 0, /* max returned setup. */
3341 state->param, /* param. */
3343 2, /* max returned param. */
3344 state->data, /* data. */
3346 0); /* max returned data. */
3348 if (tevent_req_nomem(subreq, req)) {
3349 return tevent_req_post(req, ev);
3351 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
3355 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
3357 return tevent_req_simple_recv_ntstatus(req);
3360 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
3362 TALLOC_CTX *frame = NULL;
3363 struct tevent_context *ev = NULL;
3364 struct tevent_req *req = NULL;
3365 NTSTATUS status = NT_STATUS_OK;
3367 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3368 return cli_smb2_ftruncate(cli, fnum, size);
3371 frame = talloc_stackframe();
3373 if (smbXcli_conn_has_async_calls(cli->conn)) {
3375 * Can't use sync call while an async call is in flight
3377 status = NT_STATUS_INVALID_PARAMETER;
3381 ev = samba_tevent_context_init(frame);
3383 status = NT_STATUS_NO_MEMORY;
3387 req = cli_ftruncate_send(frame,
3393 status = NT_STATUS_NO_MEMORY;
3397 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3401 status = cli_ftruncate_recv(req);
3408 static uint8_t *cli_lockingx_put_locks(
3412 const struct smb1_lock_element *locks)
3416 for (i=0; i<num_locks; i++) {
3417 const struct smb1_lock_element *e = &locks[i];
3419 SSVAL(buf, 0, e->pid);
3421 SOFF_T_R(buf, 4, e->offset);
3422 SOFF_T_R(buf, 12, e->length);
3425 SSVAL(buf, 0, e->pid);
3426 SIVAL(buf, 2, e->offset);
3427 SIVAL(buf, 6, e->length);
3434 struct cli_lockingx_state {
3437 struct tevent_req *subreq;
3440 static void cli_lockingx_done(struct tevent_req *subreq);
3441 static bool cli_lockingx_cancel(struct tevent_req *req);
3443 struct tevent_req *cli_lockingx_create(
3444 TALLOC_CTX *mem_ctx,
3445 struct tevent_context *ev,
3446 struct cli_state *cli,
3449 uint8_t newoplocklevel,
3451 uint16_t num_unlocks,
3452 const struct smb1_lock_element *unlocks,
3454 const struct smb1_lock_element *locks,
3455 struct tevent_req **psmbreq)
3457 struct tevent_req *req = NULL, *subreq = NULL;
3458 struct cli_lockingx_state *state = NULL;
3461 const bool large = (typeoflock & LOCKING_ANDX_LARGE_FILES);
3462 const size_t element_len = large ? 20 : 10;
3464 /* uint16->size_t, no overflow */
3465 const size_t num_elements = (size_t)num_locks + (size_t)num_unlocks;
3467 /* at most 20*2*65535 = 2621400, no overflow */
3468 const size_t num_bytes = num_elements * element_len;
3470 req = tevent_req_create(mem_ctx, &state, struct cli_lockingx_state);
3476 SCVAL(vwv + 0, 0, 0xFF);
3477 SCVAL(vwv + 0, 1, 0);
3478 SSVAL(vwv + 1, 0, 0);
3479 SSVAL(vwv + 2, 0, fnum);
3480 SCVAL(vwv + 3, 0, typeoflock);
3481 SCVAL(vwv + 3, 1, newoplocklevel);
3482 SIVALS(vwv + 4, 0, timeout);
3483 SSVAL(vwv + 6, 0, num_unlocks);
3484 SSVAL(vwv + 7, 0, num_locks);
3486 state->bytes.iov_len = num_bytes;
3487 state->bytes.iov_base = talloc_array(state, uint8_t, num_bytes);
3488 if (tevent_req_nomem(state->bytes.iov_base, req)) {
3489 return tevent_req_post(req, ev);
3492 p = cli_lockingx_put_locks(
3493 state->bytes.iov_base, large, num_unlocks, unlocks);
3494 cli_lockingx_put_locks(p, large, num_locks, locks);
3496 subreq = cli_smb_req_create(
3497 state, ev, cli, SMBlockingX, 0, 0, 8, vwv, 1, &state->bytes);
3498 if (tevent_req_nomem(subreq, req)) {
3499 return tevent_req_post(req, ev);
3501 tevent_req_set_callback(subreq, cli_lockingx_done, req);
3506 struct tevent_req *cli_lockingx_send(
3507 TALLOC_CTX *mem_ctx,
3508 struct tevent_context *ev,
3509 struct cli_state *cli,
3512 uint8_t newoplocklevel,
3514 uint16_t num_unlocks,
3515 const struct smb1_lock_element *unlocks,
3517 const struct smb1_lock_element *locks)
3519 struct tevent_req *req = NULL, *subreq = NULL;
3520 struct cli_lockingx_state *state = NULL;
3523 req = cli_lockingx_create(
3539 state = tevent_req_data(req, struct cli_lockingx_state);
3540 state->subreq = subreq;
3542 status = smb1cli_req_chain_submit(&subreq, 1);
3543 if (tevent_req_nterror(req, status)) {
3544 return tevent_req_post(req, ev);
3546 tevent_req_set_cancel_fn(req, cli_lockingx_cancel);
3550 static void cli_lockingx_done(struct tevent_req *subreq)
3552 NTSTATUS status = cli_smb_recv(
3553 subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3554 tevent_req_simple_finish_ntstatus(subreq, status);
3557 static bool cli_lockingx_cancel(struct tevent_req *req)
3559 struct cli_lockingx_state *state = tevent_req_data(
3560 req, struct cli_lockingx_state);
3561 if (state->subreq == NULL) {
3564 return tevent_req_cancel(state->subreq);
3567 NTSTATUS cli_lockingx_recv(struct tevent_req *req)
3569 return tevent_req_simple_recv_ntstatus(req);
3572 NTSTATUS cli_lockingx(
3573 struct cli_state *cli,
3576 uint8_t newoplocklevel,
3578 uint16_t num_unlocks,
3579 const struct smb1_lock_element *unlocks,
3581 const struct smb1_lock_element *locks)
3583 TALLOC_CTX *frame = talloc_stackframe();
3584 struct tevent_context *ev = NULL;
3585 struct tevent_req *req = NULL;
3586 NTSTATUS status = NT_STATUS_NO_MEMORY;
3587 unsigned int set_timeout = 0;
3588 unsigned int saved_timeout = 0;
3590 if (smbXcli_conn_has_async_calls(cli->conn)) {
3591 return NT_STATUS_INVALID_PARAMETER;
3593 ev = samba_tevent_context_init(frame);
3599 if (timeout == -1) {
3600 set_timeout = 0x7FFFFFFF;
3602 set_timeout = timeout + 2*1000;
3604 saved_timeout = cli_set_timeout(cli, set_timeout);
3607 req = cli_lockingx_send(
3622 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3625 status = cli_lockingx_recv(req);
3627 if (saved_timeout != 0) {
3628 cli_set_timeout(cli, saved_timeout);
3635 /****************************************************************************
3636 send a lock with a specified locktype
3637 this is used for testing LOCKING_ANDX_CANCEL_LOCK
3638 ****************************************************************************/
3640 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
3641 uint32_t offset, uint32_t len,
3642 int timeout, unsigned char locktype)
3644 struct smb1_lock_element lck = {
3645 .pid = cli_getpid(cli),
3651 status = cli_lockingx(
3654 locktype, /* typeoflock */
3655 0, /* newoplocklevel */
3656 timeout, /* timeout */
3657 0, /* num_unlocks */
3664 /****************************************************************************
3666 note that timeout is in units of 2 milliseconds
3667 ****************************************************************************/
3669 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3670 uint32_t offset, uint32_t len, int timeout,
3671 enum brl_type lock_type)
3675 status = cli_locktype(cli, fnum, offset, len, timeout,
3676 (lock_type == READ_LOCK? 1 : 0));
3680 /****************************************************************************
3682 ****************************************************************************/
3684 struct cli_unlock_state {
3685 struct smb1_lock_element lck;
3688 static void cli_unlock_done(struct tevent_req *subreq);
3690 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3691 struct tevent_context *ev,
3692 struct cli_state *cli,
3698 struct tevent_req *req = NULL, *subreq = NULL;
3699 struct cli_unlock_state *state = NULL;
3701 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3705 state->lck = (struct smb1_lock_element) {
3706 .pid = cli_getpid(cli),
3711 subreq = cli_lockingx_send(
3712 state, /* mem_ctx */
3713 ev, /* tevent_context */
3717 0, /* newoplocklevel */
3719 1, /* num_unlocks */
3720 &state->lck, /* unlocks */
3723 if (tevent_req_nomem(subreq, req)) {
3724 return tevent_req_post(req, ev);
3726 tevent_req_set_callback(subreq, cli_unlock_done, req);
3730 static void cli_unlock_done(struct tevent_req *subreq)
3732 NTSTATUS status = cli_lockingx_recv(subreq);
3733 tevent_req_simple_finish_ntstatus(subreq, status);
3736 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3738 return tevent_req_simple_recv_ntstatus(req);
3741 NTSTATUS cli_unlock(struct cli_state *cli,
3746 TALLOC_CTX *frame = talloc_stackframe();
3747 struct tevent_context *ev;
3748 struct tevent_req *req;
3749 NTSTATUS status = NT_STATUS_OK;
3751 if (smbXcli_conn_has_async_calls(cli->conn)) {
3753 * Can't use sync call while an async call is in flight
3755 status = NT_STATUS_INVALID_PARAMETER;
3759 ev = samba_tevent_context_init(frame);
3761 status = NT_STATUS_NO_MEMORY;
3765 req = cli_unlock_send(frame, ev, cli,
3768 status = NT_STATUS_NO_MEMORY;
3772 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3776 status = cli_unlock_recv(req);
3783 /****************************************************************************
3784 Get/unlock a POSIX lock on a file - internal function.
3785 ****************************************************************************/
3787 struct posix_lock_state {
3790 uint8_t data[POSIX_LOCK_DATA_SIZE];
3793 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3795 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3796 NULL, 0, NULL, NULL, 0, NULL);
3797 tevent_req_simple_finish_ntstatus(subreq, status);
3800 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3801 struct tevent_context *ev,
3802 struct cli_state *cli,
3807 enum brl_type lock_type)
3809 struct tevent_req *req = NULL, *subreq = NULL;
3810 struct posix_lock_state *state = NULL;
3812 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3817 /* Setup setup word. */
3818 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3820 /* Setup param array. */
3821 SSVAL(&state->param, 0, fnum);
3822 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3824 /* Setup data array. */
3825 switch (lock_type) {
3827 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3828 POSIX_LOCK_TYPE_READ);
3831 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3832 POSIX_LOCK_TYPE_WRITE);
3835 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3836 POSIX_LOCK_TYPE_UNLOCK);
3843 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3844 POSIX_LOCK_FLAG_WAIT);
3846 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3847 POSIX_LOCK_FLAG_NOWAIT);
3850 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3851 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3852 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3854 subreq = cli_trans_send(state, /* mem ctx. */
3855 ev, /* event ctx. */
3856 cli, /* cli_state. */
3857 0, /* additional_flags2 */
3858 SMBtrans2, /* cmd. */
3859 NULL, /* pipe name. */
3863 &state->setup, /* setup. */
3864 1, /* num setup uint16_t words. */
3865 0, /* max returned setup. */
3866 state->param, /* param. */
3868 2, /* max returned param. */
3869 state->data, /* data. */
3870 POSIX_LOCK_DATA_SIZE, /* num data. */
3871 0); /* max returned data. */
3873 if (tevent_req_nomem(subreq, req)) {
3874 return tevent_req_post(req, ev);
3876 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3880 /****************************************************************************
3882 ****************************************************************************/
3884 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3885 struct tevent_context *ev,
3886 struct cli_state *cli,
3891 enum brl_type lock_type)
3893 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3894 wait_lock, lock_type);
3897 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3899 return tevent_req_simple_recv_ntstatus(req);
3902 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3903 uint64_t offset, uint64_t len,
3904 bool wait_lock, enum brl_type lock_type)
3906 TALLOC_CTX *frame = talloc_stackframe();
3907 struct tevent_context *ev = NULL;
3908 struct tevent_req *req = NULL;
3909 NTSTATUS status = NT_STATUS_OK;
3911 if (smbXcli_conn_has_async_calls(cli->conn)) {
3913 * Can't use sync call while an async call is in flight
3915 status = NT_STATUS_INVALID_PARAMETER;
3919 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3920 status = NT_STATUS_INVALID_PARAMETER;
3924 ev = samba_tevent_context_init(frame);
3926 status = NT_STATUS_NO_MEMORY;
3930 req = cli_posix_lock_send(frame,
3939 status = NT_STATUS_NO_MEMORY;
3943 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3947 status = cli_posix_lock_recv(req);
3954 /****************************************************************************
3955 POSIX Unlock a file.
3956 ****************************************************************************/
3958 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3959 struct tevent_context *ev,
3960 struct cli_state *cli,
3965 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3966 false, UNLOCK_LOCK);
3969 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3971 return tevent_req_simple_recv_ntstatus(req);
3974 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3976 TALLOC_CTX *frame = talloc_stackframe();
3977 struct tevent_context *ev = NULL;
3978 struct tevent_req *req = NULL;
3979 NTSTATUS status = NT_STATUS_OK;
3981 if (smbXcli_conn_has_async_calls(cli->conn)) {
3983 * Can't use sync call while an async call is in flight
3985 status = NT_STATUS_INVALID_PARAMETER;
3989 ev = samba_tevent_context_init(frame);
3991 status = NT_STATUS_NO_MEMORY;
3995 req = cli_posix_unlock_send(frame,
4002 status = NT_STATUS_NO_MEMORY;
4006 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4010 status = cli_posix_unlock_recv(req);
4017 /****************************************************************************
4018 Do a SMBgetattrE call.
4019 ****************************************************************************/
4021 static void cli_getattrE_done(struct tevent_req *subreq);
4023 struct cli_getattrE_state {
4033 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
4034 struct tevent_context *ev,
4035 struct cli_state *cli,
4038 struct tevent_req *req = NULL, *subreq = NULL;
4039 struct cli_getattrE_state *state = NULL;
4040 uint8_t additional_flags = 0;
4042 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
4047 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
4048 SSVAL(state->vwv+0,0,fnum);
4050 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
4051 1, state->vwv, 0, NULL);
4052 if (tevent_req_nomem(subreq, req)) {
4053 return tevent_req_post(req, ev);
4055 tevent_req_set_callback(subreq, cli_getattrE_done, req);
4059 static void cli_getattrE_done(struct tevent_req *subreq)
4061 struct tevent_req *req = tevent_req_callback_data(
4062 subreq, struct tevent_req);
4063 struct cli_getattrE_state *state = tevent_req_data(
4064 req, struct cli_getattrE_state);
4066 uint16_t *vwv = NULL;
4069 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
4071 TALLOC_FREE(subreq);
4072 if (tevent_req_nterror(req, status)) {
4076 state->size = (off_t)IVAL(vwv+6,0);
4077 state->attr = SVAL(vwv+10,0);
4078 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
4079 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
4080 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
4082 tevent_req_done(req);
4085 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
4088 time_t *change_time,
4089 time_t *access_time,
4092 struct cli_getattrE_state *state = tevent_req_data(
4093 req, struct cli_getattrE_state);
4096 if (tevent_req_is_nterror(req, &status)) {
4100 *attr = state->attr;
4103 *size = state->size;
4106 *change_time = state->change_time;
4109 *access_time = state->access_time;
4112 *write_time = state->write_time;
4114 return NT_STATUS_OK;
4117 NTSTATUS cli_getattrE(struct cli_state *cli,
4121 time_t *change_time,
4122 time_t *access_time,
4125 TALLOC_CTX *frame = NULL;
4126 struct tevent_context *ev = NULL;
4127 struct tevent_req *req = NULL;
4128 NTSTATUS status = NT_STATUS_OK;
4130 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4131 return cli_smb2_getattrE(cli,
4140 frame = talloc_stackframe();
4142 if (smbXcli_conn_has_async_calls(cli->conn)) {
4144 * Can't use sync call while an async call is in flight
4146 status = NT_STATUS_INVALID_PARAMETER;
4150 ev = samba_tevent_context_init(frame);
4152 status = NT_STATUS_NO_MEMORY;
4156 req = cli_getattrE_send(frame, ev, cli, fnum);
4158 status = NT_STATUS_NO_MEMORY;
4162 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4166 status = cli_getattrE_recv(req,
4178 /****************************************************************************
4180 ****************************************************************************/
4182 static void cli_getatr_done(struct tevent_req *subreq);
4184 struct cli_getatr_state {
4191 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
4192 struct tevent_context *ev,
4193 struct cli_state *cli,
4196 struct tevent_req *req = NULL, *subreq = NULL;
4197 struct cli_getatr_state *state = NULL;
4198 uint8_t additional_flags = 0;
4199 uint16_t additional_flags2 = 0;
4200 uint8_t *bytes = NULL;
4202 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
4207 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
4209 bytes = talloc_array(state, uint8_t, 1);
4210 if (tevent_req_nomem(bytes, req)) {
4211 return tevent_req_post(req, ev);
4214 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4215 strlen(fname)+1, NULL);
4217 if (tevent_req_nomem(bytes, req)) {
4218 return tevent_req_post(req, ev);
4221 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4222 additional_flags2 = FLAGS2_REPARSE_PATH;
4225 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
4227 0, NULL, talloc_get_size(bytes), bytes);
4228 if (tevent_req_nomem(subreq, req)) {
4229 return tevent_req_post(req, ev);
4231 tevent_req_set_callback(subreq, cli_getatr_done, req);
4235 static void cli_getatr_done(struct tevent_req *subreq)
4237 struct tevent_req *req = tevent_req_callback_data(
4238 subreq, struct tevent_req);
4239 struct cli_getatr_state *state = tevent_req_data(
4240 req, struct cli_getatr_state);
4242 uint16_t *vwv = NULL;
4245 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4247 TALLOC_FREE(subreq);
4248 if (tevent_req_nterror(req, status)) {
4252 state->attr = SVAL(vwv+0,0);
4253 state->size = (off_t)IVAL(vwv+3,0);
4254 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
4256 tevent_req_done(req);
4259 NTSTATUS cli_getatr_recv(struct tevent_req *req,
4264 struct cli_getatr_state *state = tevent_req_data(
4265 req, struct cli_getatr_state);
4268 if (tevent_req_is_nterror(req, &status)) {
4272 *attr = state->attr;
4275 *size = state->size;
4278 *write_time = state->write_time;
4280 return NT_STATUS_OK;
4283 NTSTATUS cli_getatr(struct cli_state *cli,
4289 TALLOC_CTX *frame = NULL;
4290 struct tevent_context *ev = NULL;
4291 struct tevent_req *req = NULL;
4292 NTSTATUS status = NT_STATUS_OK;
4294 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4295 return cli_smb2_getatr(cli,
4302 frame = talloc_stackframe();
4304 if (smbXcli_conn_has_async_calls(cli->conn)) {
4306 * Can't use sync call while an async call is in flight
4308 status = NT_STATUS_INVALID_PARAMETER;
4312 ev = samba_tevent_context_init(frame);
4314 status = NT_STATUS_NO_MEMORY;
4318 req = cli_getatr_send(frame, ev, cli, fname);
4320 status = NT_STATUS_NO_MEMORY;
4324 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4328 status = cli_getatr_recv(req,
4338 /****************************************************************************
4339 Do a SMBsetattrE call.
4340 ****************************************************************************/
4342 static void cli_setattrE_done(struct tevent_req *subreq);
4344 struct cli_setattrE_state {
4348 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
4349 struct tevent_context *ev,
4350 struct cli_state *cli,
4356 struct tevent_req *req = NULL, *subreq = NULL;
4357 struct cli_setattrE_state *state = NULL;
4358 uint8_t additional_flags = 0;
4360 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
4365 SSVAL(state->vwv+0, 0, fnum);
4366 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
4367 smb1cli_conn_server_time_zone(cli->conn));
4368 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
4369 smb1cli_conn_server_time_zone(cli->conn));
4370 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
4371 smb1cli_conn_server_time_zone(cli->conn));
4373 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
4374 7, state->vwv, 0, NULL);
4375 if (tevent_req_nomem(subreq, req)) {
4376 return tevent_req_post(req, ev);
4378 tevent_req_set_callback(subreq, cli_setattrE_done, req);
4382 static void cli_setattrE_done(struct tevent_req *subreq)
4384 struct tevent_req *req = tevent_req_callback_data(
4385 subreq, struct tevent_req);
4388 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4389 TALLOC_FREE(subreq);
4390 if (tevent_req_nterror(req, status)) {
4393 tevent_req_done(req);
4396 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
4398 return tevent_req_simple_recv_ntstatus(req);
4401 NTSTATUS cli_setattrE(struct cli_state *cli,
4407 TALLOC_CTX *frame = NULL;
4408 struct tevent_context *ev = NULL;
4409 struct tevent_req *req = NULL;
4410 NTSTATUS status = NT_STATUS_OK;
4412 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4413 return cli_smb2_setattrE(cli,
4420 frame = talloc_stackframe();
4422 if (smbXcli_conn_has_async_calls(cli->conn)) {
4424 * Can't use sync call while an async call is in flight
4426 status = NT_STATUS_INVALID_PARAMETER;
4430 ev = samba_tevent_context_init(frame);
4432 status = NT_STATUS_NO_MEMORY;
4436 req = cli_setattrE_send(frame, ev,
4444 status = NT_STATUS_NO_MEMORY;
4448 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4452 status = cli_setattrE_recv(req);
4459 /****************************************************************************
4460 Do a SMBsetatr call.
4461 ****************************************************************************/
4463 static void cli_setatr_done(struct tevent_req *subreq);
4465 struct cli_setatr_state {
4469 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4470 struct tevent_context *ev,
4471 struct cli_state *cli,
4476 struct tevent_req *req = NULL, *subreq = NULL;
4477 struct cli_setatr_state *state = NULL;
4478 uint8_t additional_flags = 0;
4479 uint16_t additional_flags2 = 0;
4480 uint8_t *bytes = NULL;
4482 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4487 SSVAL(state->vwv+0, 0, attr);
4488 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4490 bytes = talloc_array(state, uint8_t, 1);
4491 if (tevent_req_nomem(bytes, req)) {
4492 return tevent_req_post(req, ev);
4495 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4496 strlen(fname)+1, NULL);
4497 if (tevent_req_nomem(bytes, req)) {
4498 return tevent_req_post(req, ev);
4500 bytes = talloc_realloc(state, bytes, uint8_t,
4501 talloc_get_size(bytes)+1);
4502 if (tevent_req_nomem(bytes, req)) {
4503 return tevent_req_post(req, ev);
4506 bytes[talloc_get_size(bytes)-1] = 4;
4507 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4509 if (tevent_req_nomem(bytes, req)) {
4510 return tevent_req_post(req, ev);
4513 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4514 additional_flags2 = FLAGS2_REPARSE_PATH;
4517 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
4519 8, state->vwv, talloc_get_size(bytes), bytes);
4520 if (tevent_req_nomem(subreq, req)) {
4521 return tevent_req_post(req, ev);
4523 tevent_req_set_callback(subreq, cli_setatr_done, req);
4527 static void cli_setatr_done(struct tevent_req *subreq)
4529 struct tevent_req *req = tevent_req_callback_data(
4530 subreq, struct tevent_req);
4533 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4534 TALLOC_FREE(subreq);
4535 if (tevent_req_nterror(req, status)) {
4538 tevent_req_done(req);
4541 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4543 return tevent_req_simple_recv_ntstatus(req);
4546 NTSTATUS cli_setatr(struct cli_state *cli,
4551 TALLOC_CTX *frame = NULL;
4552 struct tevent_context *ev = NULL;
4553 struct tevent_req *req = NULL;
4554 NTSTATUS status = NT_STATUS_OK;
4556 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4557 return cli_smb2_setatr(cli,
4563 frame = talloc_stackframe();
4565 if (smbXcli_conn_has_async_calls(cli->conn)) {
4567 * Can't use sync call while an async call is in flight
4569 status = NT_STATUS_INVALID_PARAMETER;
4573 ev = samba_tevent_context_init(frame);
4575 status = NT_STATUS_NO_MEMORY;
4579 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4581 status = NT_STATUS_NO_MEMORY;
4585 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4589 status = cli_setatr_recv(req);
4596 /****************************************************************************
4597 Check for existence of a dir.
4598 ****************************************************************************/
4600 static void cli_chkpath_done(struct tevent_req *subreq);
4602 struct cli_chkpath_state {
4606 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4607 struct tevent_context *ev,
4608 struct cli_state *cli,
4611 struct tevent_req *req = NULL, *subreq = NULL;
4612 struct cli_chkpath_state *state = NULL;
4613 uint8_t additional_flags = 0;
4614 uint16_t additional_flags2 = 0;
4615 uint8_t *bytes = NULL;
4617 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4622 bytes = talloc_array(state, uint8_t, 1);
4623 if (tevent_req_nomem(bytes, req)) {
4624 return tevent_req_post(req, ev);
4627 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4628 strlen(fname)+1, NULL);
4630 if (tevent_req_nomem(bytes, req)) {
4631 return tevent_req_post(req, ev);
4634 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4635 additional_flags2 = FLAGS2_REPARSE_PATH;
4638 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4640 0, NULL, talloc_get_size(bytes), bytes);
4641 if (tevent_req_nomem(subreq, req)) {
4642 return tevent_req_post(req, ev);
4644 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4648 static void cli_chkpath_done(struct tevent_req *subreq)
4650 struct tevent_req *req = tevent_req_callback_data(
4651 subreq, struct tevent_req);
4654 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4655 TALLOC_FREE(subreq);
4656 if (tevent_req_nterror(req, status)) {
4659 tevent_req_done(req);
4662 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4664 return tevent_req_simple_recv_ntstatus(req);
4667 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4669 TALLOC_CTX *frame = NULL;
4670 struct tevent_context *ev = NULL;
4671 struct tevent_req *req = NULL;
4673 NTSTATUS status = NT_STATUS_OK;
4675 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4676 return cli_smb2_chkpath(cli, path);
4679 frame = talloc_stackframe();
4681 if (smbXcli_conn_has_async_calls(cli->conn)) {
4683 * Can't use sync call while an async call is in flight
4685 status = NT_STATUS_INVALID_PARAMETER;
4689 path2 = talloc_strdup(frame, path);
4691 status = NT_STATUS_NO_MEMORY;
4694 trim_char(path2,'\0','\\');
4696 path2 = talloc_strdup(frame, "\\");
4698 status = NT_STATUS_NO_MEMORY;
4703 ev = samba_tevent_context_init(frame);
4705 status = NT_STATUS_NO_MEMORY;
4709 req = cli_chkpath_send(frame, ev, cli, path2);
4711 status = NT_STATUS_NO_MEMORY;
4715 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4719 status = cli_chkpath_recv(req);
4726 /****************************************************************************
4728 ****************************************************************************/
4730 static void cli_dskattr_done(struct tevent_req *subreq);
4732 struct cli_dskattr_state {
4738 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4739 struct tevent_context *ev,
4740 struct cli_state *cli)
4742 struct tevent_req *req = NULL, *subreq = NULL;
4743 struct cli_dskattr_state *state = NULL;
4744 uint8_t additional_flags = 0;
4746 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4751 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4753 if (tevent_req_nomem(subreq, req)) {
4754 return tevent_req_post(req, ev);
4756 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4760 static void cli_dskattr_done(struct tevent_req *subreq)
4762 struct tevent_req *req = tevent_req_callback_data(
4763 subreq, struct tevent_req);
4764 struct cli_dskattr_state *state = tevent_req_data(
4765 req, struct cli_dskattr_state);
4767 uint16_t *vwv = NULL;
4770 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4772 TALLOC_FREE(subreq);
4773 if (tevent_req_nterror(req, status)) {
4776 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4777 state->total = SVAL(vwv+0, 0);
4778 state->avail = SVAL(vwv+3, 0);
4779 tevent_req_done(req);
4782 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4784 struct cli_dskattr_state *state = tevent_req_data(
4785 req, struct cli_dskattr_state);
4788 if (tevent_req_is_nterror(req, &status)) {
4791 *bsize = state->bsize;
4792 *total = state->total;
4793 *avail = state->avail;
4794 return NT_STATUS_OK;
4797 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4799 TALLOC_CTX *frame = NULL;
4800 struct tevent_context *ev = NULL;
4801 struct tevent_req *req = NULL;
4802 NTSTATUS status = NT_STATUS_OK;
4804 frame = talloc_stackframe();
4806 if (smbXcli_conn_has_async_calls(cli->conn)) {
4808 * Can't use sync call while an async call is in flight
4810 status = NT_STATUS_INVALID_PARAMETER;
4814 ev = samba_tevent_context_init(frame);
4816 status = NT_STATUS_NO_MEMORY;
4820 req = cli_dskattr_send(frame, ev, cli);
4822 status = NT_STATUS_NO_MEMORY;
4826 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4830 status = cli_dskattr_recv(req, bsize, total, avail);
4837 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4838 uint64_t *total, uint64_t *avail)
4840 uint64_t sectors_per_block;
4841 uint64_t bytes_per_sector;
4842 int old_bsize = 0, old_total = 0, old_avail = 0;
4845 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4846 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4850 * Try the trans2 disk full size info call first.
4851 * We already use this in SMBC_fstatvfs_ctx().
4852 * Ignore 'actual_available_units' as we only
4853 * care about the quota for the caller.
4856 status = cli_get_fs_full_size_info(cli,
4863 /* Try and cope will all varients of "we don't do this call"
4864 and fall back to cli_dskattr. */
4866 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4867 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4868 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4869 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4870 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4871 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4872 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4873 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4874 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4875 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4879 if (!NT_STATUS_IS_OK(status)) {
4884 *bsize = sectors_per_block *
4888 return NT_STATUS_OK;
4892 /* Old SMB1 core protocol fallback. */
4893 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4894 if (!NT_STATUS_IS_OK(status)) {
4898 *bsize = (uint64_t)old_bsize;
4901 *total = (uint64_t)old_total;
4904 *avail = (uint64_t)old_avail;
4906 return NT_STATUS_OK;
4909 /****************************************************************************
4910 Create and open a temporary file.
4911 ****************************************************************************/
4913 static void cli_ctemp_done(struct tevent_req *subreq);
4915 struct ctemp_state {
4921 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4922 struct tevent_context *ev,
4923 struct cli_state *cli,
4926 struct tevent_req *req = NULL, *subreq = NULL;
4927 struct ctemp_state *state = NULL;
4928 uint8_t additional_flags = 0;
4929 uint16_t additional_flags2 = 0;
4930 uint8_t *bytes = NULL;
4932 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4937 SSVAL(state->vwv,0,0);
4938 SIVALS(state->vwv+1,0,-1);
4940 bytes = talloc_array(state, uint8_t, 1);
4941 if (tevent_req_nomem(bytes, req)) {
4942 return tevent_req_post(req, ev);
4945 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4946 strlen(path)+1, NULL);
4947 if (tevent_req_nomem(bytes, req)) {
4948 return tevent_req_post(req, ev);
4951 if (clistr_is_previous_version_path(path, NULL, NULL, NULL)) {
4952 additional_flags2 = FLAGS2_REPARSE_PATH;
4955 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4957 3, state->vwv, talloc_get_size(bytes), bytes);
4958 if (tevent_req_nomem(subreq, req)) {
4959 return tevent_req_post(req, ev);
4961 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4965 static void cli_ctemp_done(struct tevent_req *subreq)
4967 struct tevent_req *req = tevent_req_callback_data(
4968 subreq, struct tevent_req);
4969 struct ctemp_state *state = tevent_req_data(
4970 req, struct ctemp_state);
4974 uint32_t num_bytes = 0;
4975 uint8_t *bytes = NULL;
4977 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4978 &num_bytes, &bytes);
4979 TALLOC_FREE(subreq);
4980 if (tevent_req_nterror(req, status)) {
4984 state->fnum = SVAL(vwv+0, 0);
4986 /* From W2K3, the result is just the ASCII name */
4987 if (num_bytes < 2) {
4988 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4992 if (pull_string_talloc(state,
4999 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
5002 tevent_req_done(req);
5005 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
5010 struct ctemp_state *state = tevent_req_data(req,
5011 struct ctemp_state);
5014 if (tevent_req_is_nterror(req, &status)) {
5017 *pfnum = state->fnum;
5018 *outfile = talloc_strdup(ctx, state->ret_path);
5020 return NT_STATUS_NO_MEMORY;
5022 return NT_STATUS_OK;
5025 NTSTATUS cli_ctemp(struct cli_state *cli,
5031 TALLOC_CTX *frame = talloc_stackframe();
5032 struct tevent_context *ev;
5033 struct tevent_req *req;
5034 NTSTATUS status = NT_STATUS_OK;
5036 if (smbXcli_conn_has_async_calls(cli->conn)) {
5038 * Can't use sync call while an async call is in flight
5040 status = NT_STATUS_INVALID_PARAMETER;
5044 ev = samba_tevent_context_init(frame);
5046 status = NT_STATUS_NO_MEMORY;
5050 req = cli_ctemp_send(frame, ev, cli, path);
5052 status = NT_STATUS_NO_MEMORY;
5056 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5060 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
5068 send a raw ioctl - used by the torture code
5070 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
5075 SSVAL(vwv+0, 0, fnum);
5076 SSVAL(vwv+1, 0, code>>16);
5077 SSVAL(vwv+2, 0, (code&0xFFFF));
5079 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
5080 NULL, 0, NULL, NULL, NULL, NULL);
5081 if (!NT_STATUS_IS_OK(status)) {
5084 *blob = data_blob_null;
5085 return NT_STATUS_OK;
5088 /*********************************************************
5089 Set an extended attribute utility fn.
5090 *********************************************************/
5092 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
5093 uint8_t *param, unsigned int param_len,
5094 const char *ea_name,
5095 const char *ea_val, size_t ea_len)
5098 unsigned int data_len = 0;
5099 uint8_t *data = NULL;
5101 size_t ea_namelen = strlen(ea_name);
5104 SSVAL(setup, 0, setup_val);
5106 if (ea_namelen == 0 && ea_len == 0) {
5108 data = talloc_array(talloc_tos(),
5112 return NT_STATUS_NO_MEMORY;
5115 SIVAL(p,0,data_len);
5117 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
5118 data = talloc_array(talloc_tos(),
5122 return NT_STATUS_NO_MEMORY;
5125 SIVAL(p,0,data_len);
5127 SCVAL(p, 0, 0); /* EA flags. */
5128 SCVAL(p, 1, ea_namelen);
5129 SSVAL(p, 2, ea_len);
5130 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
5131 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
5135 * FIXME - if we want to do previous version path
5136 * processing on an EA set call we need to turn this
5137 * into calls to cli_trans_send()/cli_trans_recv()
5138 * with a temporary event context, as cli_trans_send()
5139 * have access to the additional_flags2 needed to
5140 * send @GMT- paths. JRA.
5143 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
5145 param, param_len, 2,
5148 NULL, 0, NULL, /* rsetup */
5149 NULL, 0, NULL, /* rparam */
5150 NULL, 0, NULL); /* rdata */
5155 /*********************************************************
5156 Set an extended attribute on a pathname.
5157 *********************************************************/
5159 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
5160 const char *ea_name, const char *ea_val,
5163 unsigned int param_len = 0;
5166 TALLOC_CTX *frame = NULL;
5168 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5169 return cli_smb2_set_ea_path(cli,
5176 frame = talloc_stackframe();
5178 param = talloc_array(frame, uint8_t, 6);
5180 status = NT_STATUS_NO_MEMORY;
5183 SSVAL(param,0,SMB_INFO_SET_EA);
5187 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
5188 path, strlen(path)+1,
5190 param_len = talloc_get_size(param);
5192 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
5193 ea_name, ea_val, ea_len);
5201 /*********************************************************
5202 Set an extended attribute on an fnum.
5203 *********************************************************/
5205 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
5206 const char *ea_name, const char *ea_val,
5211 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5212 return cli_smb2_set_ea_fnum(cli,
5219 memset(param, 0, 6);
5220 SSVAL(param,0,fnum);
5221 SSVAL(param,2,SMB_INFO_SET_EA);
5223 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
5224 ea_name, ea_val, ea_len);
5227 /*********************************************************
5228 Get an extended attribute list utility fn.
5229 *********************************************************/
5231 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
5233 size_t *pnum_eas, struct ea_struct **pea_list)
5235 struct ea_struct *ea_list = NULL;
5240 if (rdata_len < 4) {
5244 ea_size = (size_t)IVAL(rdata,0);
5245 if (ea_size > rdata_len) {
5250 /* No EA's present. */
5259 /* Validate the EA list and count it. */
5260 for (num_eas = 0; ea_size >= 4; num_eas++) {
5261 unsigned int ea_namelen = CVAL(p,1);
5262 unsigned int ea_valuelen = SVAL(p,2);
5263 if (ea_namelen == 0) {
5266 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
5269 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
5270 p += 4 + ea_namelen + 1 + ea_valuelen;
5279 *pnum_eas = num_eas;
5281 /* Caller only wants number of EA's. */
5285 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
5292 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
5293 struct ea_struct *ea = &ea_list[num_eas];
5294 fstring unix_ea_name;
5295 unsigned int ea_namelen = CVAL(p,1);
5296 unsigned int ea_valuelen = SVAL(p,2);
5298 ea->flags = CVAL(p,0);
5299 unix_ea_name[0] = '\0';
5300 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
5301 ea->name = talloc_strdup(ea_list, unix_ea_name);
5305 /* Ensure the value is null terminated (in case it's a string). */
5306 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
5307 if (!ea->value.data) {
5311 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
5313 ea->value.data[ea_valuelen] = 0;
5315 p += 4 + ea_namelen + 1 + ea_valuelen;
5318 *pea_list = ea_list;
5322 TALLOC_FREE(ea_list);
5326 /*********************************************************
5327 Get an extended attribute list from a pathname.
5328 *********************************************************/
5330 struct cli_get_ea_list_path_state {
5335 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
5337 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
5338 struct tevent_context *ev,
5339 struct cli_state *cli,
5342 struct tevent_req *req, *subreq;
5343 struct cli_get_ea_list_path_state *state;
5345 req = tevent_req_create(mem_ctx, &state,
5346 struct cli_get_ea_list_path_state);
5350 subreq = cli_qpathinfo_send(state, ev, cli, fname,
5351 SMB_INFO_QUERY_ALL_EAS, 4,
5353 if (tevent_req_nomem(subreq, req)) {
5354 return tevent_req_post(req, ev);
5356 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
5360 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
5362 struct tevent_req *req = tevent_req_callback_data(
5363 subreq, struct tevent_req);
5364 struct cli_get_ea_list_path_state *state = tevent_req_data(
5365 req, struct cli_get_ea_list_path_state);
5368 status = cli_qpathinfo_recv(subreq, state, &state->data,
5370 TALLOC_FREE(subreq);
5371 if (tevent_req_nterror(req, status)) {
5374 tevent_req_done(req);
5377 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5378 size_t *pnum_eas, struct ea_struct **peas)
5380 struct cli_get_ea_list_path_state *state = tevent_req_data(
5381 req, struct cli_get_ea_list_path_state);
5384 if (tevent_req_is_nterror(req, &status)) {
5387 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
5389 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5391 return NT_STATUS_OK;
5394 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
5397 struct ea_struct **pea_list)
5399 TALLOC_CTX *frame = NULL;
5400 struct tevent_context *ev = NULL;
5401 struct tevent_req *req = NULL;
5402 NTSTATUS status = NT_STATUS_NO_MEMORY;
5404 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5405 return cli_smb2_get_ea_list_path(cli,
5412 frame = talloc_stackframe();
5414 if (smbXcli_conn_has_async_calls(cli->conn)) {
5416 * Can't use sync call while an async call is in flight
5418 status = NT_STATUS_INVALID_PARAMETER;
5421 ev = samba_tevent_context_init(frame);
5425 req = cli_get_ea_list_path_send(frame, ev, cli, path);
5429 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5432 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
5438 /****************************************************************************
5439 Convert open "flags" arg to uint32_t on wire.
5440 ****************************************************************************/
5442 static uint32_t open_flags_to_wire(int flags)
5444 int open_mode = flags & O_ACCMODE;
5447 switch (open_mode) {
5449 ret |= SMB_O_WRONLY;
5456 ret |= SMB_O_RDONLY;
5460 if (flags & O_CREAT) {
5463 if (flags & O_EXCL) {
5466 if (flags & O_TRUNC) {
5470 if (flags & O_SYNC) {
5474 if (flags & O_APPEND) {
5475 ret |= SMB_O_APPEND;
5477 #if defined(O_DIRECT)
5478 if (flags & O_DIRECT) {
5479 ret |= SMB_O_DIRECT;
5482 #if defined(O_DIRECTORY)
5483 if (flags & O_DIRECTORY) {
5484 ret |= SMB_O_DIRECTORY;
5490 /****************************************************************************
5491 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5492 ****************************************************************************/
5494 struct cli_posix_open_internal_state {
5498 uint16_t fnum; /* Out */
5501 static void cli_posix_open_internal_done(struct tevent_req *subreq);
5503 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5504 struct tevent_context *ev,
5505 struct cli_state *cli,
5507 uint32_t wire_flags,
5510 struct tevent_req *req = NULL, *subreq = NULL;
5511 struct cli_posix_open_internal_state *state = NULL;
5513 req = tevent_req_create(
5514 mem_ctx, &state, struct cli_posix_open_internal_state);
5519 /* Setup setup word. */
5520 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5522 /* Setup param array. */
5523 state->param = talloc_zero_array(state, uint8_t, 6);
5524 if (tevent_req_nomem(state->param, req)) {
5525 return tevent_req_post(req, ev);
5527 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5529 state->param = trans2_bytes_push_str(
5531 smbXcli_conn_use_unicode(cli->conn),
5536 if (tevent_req_nomem(state->param, req)) {
5537 return tevent_req_post(req, ev);
5540 SIVAL(state->data,0,0); /* No oplock. */
5541 SIVAL(state->data,4,wire_flags);
5542 SIVAL(state->data,8,unix_perms_to_wire(mode));
5543 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5544 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5546 subreq = cli_trans_send(state, /* mem ctx. */
5547 ev, /* event ctx. */
5548 cli, /* cli_state. */
5549 0, /* additional_flags2 */
5550 SMBtrans2, /* cmd. */
5551 NULL, /* pipe name. */
5555 &state->setup, /* setup. */
5556 1, /* num setup uint16_t words. */
5557 0, /* max returned setup. */
5558 state->param, /* param. */
5559 talloc_get_size(state->param),/* num param. */
5560 2, /* max returned param. */
5561 state->data, /* data. */
5563 12); /* max returned data. */
5565 if (tevent_req_nomem(subreq, req)) {
5566 return tevent_req_post(req, ev);
5568 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5572 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5574 struct tevent_req *req = tevent_req_callback_data(
5575 subreq, struct tevent_req);
5576 struct cli_posix_open_internal_state *state = tevent_req_data(
5577 req, struct cli_posix_open_internal_state);
5582 status = cli_trans_recv(
5595 TALLOC_FREE(subreq);
5596 if (tevent_req_nterror(req, status)) {
5599 state->fnum = SVAL(data,2);
5600 tevent_req_done(req);
5603 static NTSTATUS cli_posix_open_internal_recv(struct tevent_req *req,
5606 struct cli_posix_open_internal_state *state = tevent_req_data(
5607 req, struct cli_posix_open_internal_state);
5610 if (tevent_req_is_nterror(req, &status)) {
5613 *pfnum = state->fnum;
5614 return NT_STATUS_OK;
5617 struct cli_posix_open_state {
5621 static void cli_posix_open_done(struct tevent_req *subreq);
5623 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5624 struct tevent_context *ev,
5625 struct cli_state *cli,
5630 struct tevent_req *req = NULL, *subreq = NULL;
5631 struct cli_posix_open_state *state = NULL;
5632 uint32_t wire_flags;
5634 req = tevent_req_create(mem_ctx, &state,
5635 struct cli_posix_open_state);
5640 wire_flags = open_flags_to_wire(flags);
5642 subreq = cli_posix_open_internal_send(
5643 mem_ctx, ev, cli, fname, wire_flags, mode);
5644 if (tevent_req_nomem(subreq, req)) {
5645 return tevent_req_post(req, ev);
5647 tevent_req_set_callback(subreq, cli_posix_open_done, req);
5651 static void cli_posix_open_done(struct tevent_req *subreq)
5653 struct tevent_req *req = tevent_req_callback_data(
5654 subreq, struct tevent_req);
5655 struct cli_posix_open_state *state = tevent_req_data(
5656 req, struct cli_posix_open_state);
5659 status = cli_posix_open_internal_recv(subreq, &state->fnum);
5660 tevent_req_simple_finish_ntstatus(subreq, status);
5663 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5665 struct cli_posix_open_state *state = tevent_req_data(
5666 req, struct cli_posix_open_state);
5669 if (tevent_req_is_nterror(req, &status)) {
5672 *pfnum = state->fnum;
5673 return NT_STATUS_OK;
5676 /****************************************************************************
5677 Open - POSIX semantics. Doesn't request oplock.
5678 ****************************************************************************/
5680 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5681 int flags, mode_t mode, uint16_t *pfnum)
5684 TALLOC_CTX *frame = talloc_stackframe();
5685 struct tevent_context *ev = NULL;
5686 struct tevent_req *req = NULL;
5687 NTSTATUS status = NT_STATUS_NO_MEMORY;
5689 if (smbXcli_conn_has_async_calls(cli->conn)) {
5691 * Can't use sync call while an async call is in flight
5693 status = NT_STATUS_INVALID_PARAMETER;
5696 ev = samba_tevent_context_init(frame);
5700 req = cli_posix_open_send(
5701 frame, ev, cli, fname, flags, mode);
5705 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5708 status = cli_posix_open_recv(req, pfnum);
5714 struct cli_posix_mkdir_state {
5715 struct tevent_context *ev;
5716 struct cli_state *cli;
5719 static void cli_posix_mkdir_done(struct tevent_req *subreq);
5721 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5722 struct tevent_context *ev,
5723 struct cli_state *cli,
5727 struct tevent_req *req = NULL, *subreq = NULL;
5728 struct cli_posix_mkdir_state *state = NULL;
5729 uint32_t wire_flags;
5731 req = tevent_req_create(
5732 mem_ctx, &state, struct cli_posix_mkdir_state);
5739 wire_flags = SMB_O_CREAT | SMB_O_DIRECTORY;
5741 subreq = cli_posix_open_internal_send(
5742 mem_ctx, ev, cli, fname, wire_flags, mode);
5743 if (tevent_req_nomem(subreq, req)) {
5744 return tevent_req_post(req, ev);
5746 tevent_req_set_callback(subreq, cli_posix_mkdir_done, req);
5750 static void cli_posix_mkdir_done(struct tevent_req *subreq)
5752 struct tevent_req *req = tevent_req_callback_data(
5753 subreq, struct tevent_req);
5757 status = cli_posix_open_internal_recv(subreq, &fnum);
5758 TALLOC_FREE(subreq);
5759 if (tevent_req_nterror(req, status)) {
5762 tevent_req_done(req);
5765 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5767 return tevent_req_simple_recv_ntstatus(req);
5770 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5772 TALLOC_CTX *frame = talloc_stackframe();
5773 struct tevent_context *ev = NULL;
5774 struct tevent_req *req = NULL;
5775 NTSTATUS status = NT_STATUS_NO_MEMORY;
5777 if (smbXcli_conn_has_async_calls(cli->conn)) {
5779 * Can't use sync call while an async call is in flight
5781 status = NT_STATUS_INVALID_PARAMETER;
5785 ev = samba_tevent_context_init(frame);
5789 req = cli_posix_mkdir_send(
5790 frame, ev, cli, fname, mode);
5794 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5797 status = cli_posix_mkdir_recv(req);
5803 /****************************************************************************
5804 unlink or rmdir - POSIX semantics.
5805 ****************************************************************************/
5807 struct cli_posix_unlink_internal_state {
5811 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5813 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5814 struct tevent_context *ev,
5815 struct cli_state *cli,
5819 struct tevent_req *req = NULL, *subreq = NULL;
5820 struct cli_posix_unlink_internal_state *state = NULL;
5822 req = tevent_req_create(mem_ctx, &state,
5823 struct cli_posix_unlink_internal_state);
5828 /* Setup data word. */
5829 SSVAL(state->data, 0, level);
5831 subreq = cli_setpathinfo_send(state, ev, cli,
5832 SMB_POSIX_PATH_UNLINK,
5834 state->data, sizeof(state->data));
5835 if (tevent_req_nomem(subreq, req)) {
5836 return tevent_req_post(req, ev);
5838 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5842 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5844 NTSTATUS status = cli_setpathinfo_recv(subreq);
5845 tevent_req_simple_finish_ntstatus(subreq, status);
5848 static NTSTATUS cli_posix_unlink_internal_recv(struct tevent_req *req)
5850 return tevent_req_simple_recv_ntstatus(req);
5853 struct cli_posix_unlink_state {
5857 static void cli_posix_unlink_done(struct tevent_req *subreq);
5859 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5860 struct tevent_context *ev,
5861 struct cli_state *cli,
5864 struct tevent_req *req = NULL, *subreq = NULL;
5865 struct cli_posix_unlink_state *state;
5867 req = tevent_req_create(
5868 mem_ctx, &state, struct cli_posix_unlink_state);
5872 subreq = cli_posix_unlink_internal_send(
5873 mem_ctx, ev, cli, fname, SMB_POSIX_UNLINK_FILE_TARGET);
5874 if (tevent_req_nomem(subreq, req)) {
5875 return tevent_req_post(req, ev);
5877 tevent_req_set_callback(subreq, cli_posix_unlink_done, req);
5881 static void cli_posix_unlink_done(struct tevent_req *subreq)
5883 NTSTATUS status = cli_posix_unlink_internal_recv(subreq);
5884 tevent_req_simple_finish_ntstatus(subreq, status);
5887 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5889 return tevent_req_simple_recv_ntstatus(req);
5892 /****************************************************************************
5893 unlink - POSIX semantics.
5894 ****************************************************************************/
5896 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5898 TALLOC_CTX *frame = talloc_stackframe();
5899 struct tevent_context *ev = NULL;
5900 struct tevent_req *req = NULL;
5901 NTSTATUS status = NT_STATUS_OK;
5903 if (smbXcli_conn_has_async_calls(cli->conn)) {
5905 * Can't use sync call while an async call is in flight
5907 status = NT_STATUS_INVALID_PARAMETER;
5911 ev = samba_tevent_context_init(frame);
5913 status = NT_STATUS_NO_MEMORY;
5917 req = cli_posix_unlink_send(frame,
5922 status = NT_STATUS_NO_MEMORY;
5926 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5930 status = cli_posix_unlink_recv(req);
5937 /****************************************************************************
5938 rmdir - POSIX semantics.
5939 ****************************************************************************/
5941 struct cli_posix_rmdir_state {
5945 static void cli_posix_rmdir_done(struct tevent_req *subreq);
5947 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5948 struct tevent_context *ev,
5949 struct cli_state *cli,
5952 struct tevent_req *req = NULL, *subreq = NULL;
5953 struct cli_posix_rmdir_state *state;
5955 req = tevent_req_create(mem_ctx, &state, struct cli_posix_rmdir_state);
5959 subreq = cli_posix_unlink_internal_send(
5960 mem_ctx, ev, cli, fname, SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5961 if (tevent_req_nomem(subreq, req)) {
5962 return tevent_req_post(req, ev);
5964 tevent_req_set_callback(subreq, cli_posix_rmdir_done, req);
5968 static void cli_posix_rmdir_done(struct tevent_req *subreq)
5970 NTSTATUS status = cli_posix_unlink_internal_recv(subreq);
5971 tevent_req_simple_finish_ntstatus(subreq, status);
5974 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5976 return tevent_req_simple_recv_ntstatus(req);
5979 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5981 TALLOC_CTX *frame = talloc_stackframe();
5982 struct tevent_context *ev = NULL;
5983 struct tevent_req *req = NULL;
5984 NTSTATUS status = NT_STATUS_OK;
5986 if (smbXcli_conn_has_async_calls(cli->conn)) {
5988 * Can't use sync call while an async call is in flight
5990 status = NT_STATUS_INVALID_PARAMETER;
5994 ev = samba_tevent_context_init(frame);
5996 status = NT_STATUS_NO_MEMORY;
6000 req = cli_posix_rmdir_send(frame,
6005 status = NT_STATUS_NO_MEMORY;
6009 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6013 status = cli_posix_rmdir_recv(req, frame);
6020 /****************************************************************************
6022 ****************************************************************************/
6024 struct cli_notify_state {
6025 struct tevent_req *subreq;
6027 uint32_t num_changes;
6028 struct notify_change *changes;
6031 static void cli_notify_done(struct tevent_req *subreq);
6032 static void cli_notify_done_smb2(struct tevent_req *subreq);
6033 static bool cli_notify_cancel(struct tevent_req *req);
6035 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
6036 struct tevent_context *ev,
6037 struct cli_state *cli, uint16_t fnum,
6038 uint32_t buffer_size,
6039 uint32_t completion_filter, bool recursive)
6041 struct tevent_req *req;
6042 struct cli_notify_state *state;
6043 unsigned old_timeout;
6045 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
6050 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6052 * Notifies should not time out
6054 old_timeout = cli_set_timeout(cli, 0);
6056 state->subreq = cli_smb2_notify_send(
6065 cli_set_timeout(cli, old_timeout);
6067 if (tevent_req_nomem(state->subreq, req)) {
6068 return tevent_req_post(req, ev);
6070 tevent_req_set_callback(
6071 state->subreq, cli_notify_done_smb2, req);
6075 SIVAL(state->setup, 0, completion_filter);
6076 SSVAL(state->setup, 4, fnum);
6077 SSVAL(state->setup, 6, recursive);
6080 * Notifies should not time out
6082 old_timeout = cli_set_timeout(cli, 0);
6084 state->subreq = cli_trans_send(
6085 state, /* mem ctx. */
6086 ev, /* event ctx. */
6087 cli, /* cli_state. */
6088 0, /* additional_flags2 */
6089 SMBnttrans, /* cmd. */
6090 NULL, /* pipe name. */
6092 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
6094 (uint16_t *)state->setup, /* setup. */
6095 4, /* num setup uint16_t words. */
6096 0, /* max returned setup. */
6099 buffer_size, /* max returned param. */
6102 0); /* max returned data. */
6104 cli_set_timeout(cli, old_timeout);
6106 if (tevent_req_nomem(state->subreq, req)) {
6107 return tevent_req_post(req, ev);
6109 tevent_req_set_callback(state->subreq, cli_notify_done, req);
6111 tevent_req_set_cancel_fn(req, cli_notify_cancel);
6115 static bool cli_notify_cancel(struct tevent_req *req)
6117 struct cli_notify_state *state = tevent_req_data(
6118 req, struct cli_notify_state);
6121 ok = tevent_req_cancel(state->subreq);
6125 static void cli_notify_done(struct tevent_req *subreq)
6127 struct tevent_req *req = tevent_req_callback_data(
6128 subreq, struct tevent_req);
6129 struct cli_notify_state *state = tevent_req_data(
6130 req, struct cli_notify_state);
6133 uint32_t i, ofs, num_params;
6136 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
6137 ¶ms, 0, &num_params, NULL, 0, NULL);
6138 TALLOC_FREE(subreq);
6139 state->subreq = NULL;
6140 if (tevent_req_nterror(req, status)) {
6141 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
6145 state->num_changes = 0;
6148 while (num_params - ofs > 12) {
6149 uint32_t next = IVAL(params, ofs);
6150 state->num_changes += 1;
6152 if ((next == 0) || (ofs+next >= num_params)) {
6158 state->changes = talloc_array(state, struct notify_change,
6159 state->num_changes);
6160 if (tevent_req_nomem(state->changes, req)) {
6161 TALLOC_FREE(params);
6167 for (i=0; i<state->num_changes; i++) {
6168 uint32_t next = IVAL(params, ofs);
6169 uint32_t len = IVAL(params, ofs+8);
6173 if (trans_oob(num_params, ofs + 12, len)) {
6174 TALLOC_FREE(params);
6176 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
6180 state->changes[i].action = IVAL(params, ofs+4);
6181 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
6182 &name, params+ofs+12, len,
6183 STR_TERMINATE|STR_UNICODE);
6185 TALLOC_FREE(params);
6186 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6189 state->changes[i].name = name;
6193 TALLOC_FREE(params);
6194 tevent_req_done(req);
6197 static void cli_notify_done_smb2(struct tevent_req *subreq)
6199 struct tevent_req *req = tevent_req_callback_data(
6200 subreq, struct tevent_req);
6201 struct cli_notify_state *state = tevent_req_data(
6202 req, struct cli_notify_state);
6205 status = cli_smb2_notify_recv(
6209 &state->num_changes);
6210 TALLOC_FREE(subreq);
6211 if (tevent_req_nterror(req, status)) {
6214 tevent_req_done(req);
6217 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6218 uint32_t *pnum_changes,
6219 struct notify_change **pchanges)
6221 struct cli_notify_state *state = tevent_req_data(
6222 req, struct cli_notify_state);
6225 if (tevent_req_is_nterror(req, &status)) {
6229 *pnum_changes = state->num_changes;
6230 *pchanges = talloc_move(mem_ctx, &state->changes);
6231 return NT_STATUS_OK;
6234 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
6235 uint32_t completion_filter, bool recursive,
6236 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
6237 struct notify_change **pchanges)
6240 struct tevent_context *ev;
6241 struct tevent_req *req;
6242 NTSTATUS status = NT_STATUS_NO_MEMORY;
6244 frame = talloc_stackframe();
6246 if (smbXcli_conn_has_async_calls(cli->conn)) {
6248 * Can't use sync call while an async call is in flight
6250 status = NT_STATUS_INVALID_PARAMETER;
6253 ev = samba_tevent_context_init(frame);
6257 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
6258 completion_filter, recursive);
6262 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6265 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
6271 struct cli_qpathinfo_state {
6280 static void cli_qpathinfo_done(struct tevent_req *subreq);
6282 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
6283 struct tevent_context *ev,
6284 struct cli_state *cli, const char *fname,
6285 uint16_t level, uint32_t min_rdata,
6288 struct tevent_req *req, *subreq;
6289 struct cli_qpathinfo_state *state;
6290 uint16_t additional_flags2 = 0;
6292 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
6296 state->min_rdata = min_rdata;
6297 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
6299 state->param = talloc_zero_array(state, uint8_t, 6);
6300 if (tevent_req_nomem(state->param, req)) {
6301 return tevent_req_post(req, ev);
6303 SSVAL(state->param, 0, level);
6304 state->param = trans2_bytes_push_str(
6305 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
6306 if (tevent_req_nomem(state->param, req)) {
6307 return tevent_req_post(req, ev);
6310 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL) &&
6311 !INFO_LEVEL_IS_UNIX(level)) {
6312 additional_flags2 = FLAGS2_REPARSE_PATH;
6315 subreq = cli_trans_send(
6316 state, /* mem ctx. */
6317 ev, /* event ctx. */
6318 cli, /* cli_state. */
6319 additional_flags2, /* additional_flags2 */
6320 SMBtrans2, /* cmd. */
6321 NULL, /* pipe name. */
6325 state->setup, /* setup. */
6326 1, /* num setup uint16_t words. */
6327 0, /* max returned setup. */
6328 state->param, /* param. */
6329 talloc_get_size(state->param), /* num param. */
6330 2, /* max returned param. */
6333 max_rdata); /* max returned data. */
6335 if (tevent_req_nomem(subreq, req)) {
6336 return tevent_req_post(req, ev);
6338 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
6342 static void cli_qpathinfo_done(struct tevent_req *subreq)
6344 struct tevent_req *req = tevent_req_callback_data(
6345 subreq, struct tevent_req);
6346 struct cli_qpathinfo_state *state = tevent_req_data(
6347 req, struct cli_qpathinfo_state);
6350 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
6352 &state->rdata, state->min_rdata,
6354 if (tevent_req_nterror(req, status)) {
6357 tevent_req_done(req);
6360 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6361 uint8_t **rdata, uint32_t *num_rdata)
6363 struct cli_qpathinfo_state *state = tevent_req_data(
6364 req, struct cli_qpathinfo_state);
6367 if (tevent_req_is_nterror(req, &status)) {
6370 if (rdata != NULL) {
6371 *rdata = talloc_move(mem_ctx, &state->rdata);
6373 TALLOC_FREE(state->rdata);
6375 if (num_rdata != NULL) {
6376 *num_rdata = state->num_rdata;
6378 return NT_STATUS_OK;
6381 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6382 const char *fname, uint16_t level, uint32_t min_rdata,
6384 uint8_t **rdata, uint32_t *num_rdata)
6386 TALLOC_CTX *frame = talloc_stackframe();
6387 struct tevent_context *ev;
6388 struct tevent_req *req;
6389 NTSTATUS status = NT_STATUS_NO_MEMORY;
6391 if (smbXcli_conn_has_async_calls(cli->conn)) {
6393 * Can't use sync call while an async call is in flight
6395 status = NT_STATUS_INVALID_PARAMETER;
6398 ev = samba_tevent_context_init(frame);
6402 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
6407 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6410 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
6416 struct cli_qfileinfo_state {
6420 uint16_t recv_flags2;
6426 static void cli_qfileinfo_done(struct tevent_req *subreq);
6428 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
6429 struct tevent_context *ev,
6430 struct cli_state *cli, uint16_t fnum,
6431 uint16_t level, uint32_t min_rdata,
6434 struct tevent_req *req, *subreq;
6435 struct cli_qfileinfo_state *state;
6437 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
6441 state->min_rdata = min_rdata;
6442 SSVAL(state->param, 0, fnum);
6443 SSVAL(state->param, 2, level);
6444 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
6446 subreq = cli_trans_send(
6447 state, /* mem ctx. */
6448 ev, /* event ctx. */
6449 cli, /* cli_state. */
6450 0, /* additional_flags2 */
6451 SMBtrans2, /* cmd. */
6452 NULL, /* pipe name. */
6456 state->setup, /* setup. */
6457 1, /* num setup uint16_t words. */
6458 0, /* max returned setup. */
6459 state->param, /* param. */
6460 sizeof(state->param), /* num param. */
6461 2, /* max returned param. */
6464 max_rdata); /* max returned data. */
6466 if (tevent_req_nomem(subreq, req)) {
6467 return tevent_req_post(req, ev);
6469 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
6473 static void cli_qfileinfo_done(struct tevent_req *subreq)
6475 struct tevent_req *req = tevent_req_callback_data(
6476 subreq, struct tevent_req);
6477 struct cli_qfileinfo_state *state = tevent_req_data(
6478 req, struct cli_qfileinfo_state);
6481 status = cli_trans_recv(subreq, state,
6482 &state->recv_flags2,
6485 &state->rdata, state->min_rdata,
6487 if (tevent_req_nterror(req, status)) {
6490 tevent_req_done(req);
6493 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6494 uint16_t *recv_flags2,
6495 uint8_t **rdata, uint32_t *num_rdata)
6497 struct cli_qfileinfo_state *state = tevent_req_data(
6498 req, struct cli_qfileinfo_state);
6501 if (tevent_req_is_nterror(req, &status)) {
6505 if (recv_flags2 != NULL) {
6506 *recv_flags2 = state->recv_flags2;
6508 if (rdata != NULL) {
6509 *rdata = talloc_move(mem_ctx, &state->rdata);
6511 TALLOC_FREE(state->rdata);
6513 if (num_rdata != NULL) {
6514 *num_rdata = state->num_rdata;
6516 return NT_STATUS_OK;
6519 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6520 uint16_t fnum, uint16_t level, uint32_t min_rdata,
6521 uint32_t max_rdata, uint16_t *recv_flags2,
6522 uint8_t **rdata, uint32_t *num_rdata)
6524 TALLOC_CTX *frame = talloc_stackframe();
6525 struct tevent_context *ev;
6526 struct tevent_req *req;
6527 NTSTATUS status = NT_STATUS_NO_MEMORY;
6529 if (smbXcli_conn_has_async_calls(cli->conn)) {
6531 * Can't use sync call while an async call is in flight
6533 status = NT_STATUS_INVALID_PARAMETER;
6536 ev = samba_tevent_context_init(frame);
6540 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
6545 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6548 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
6554 struct cli_flush_state {
6558 static void cli_flush_done(struct tevent_req *subreq);
6560 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
6561 struct tevent_context *ev,
6562 struct cli_state *cli,
6565 struct tevent_req *req, *subreq;
6566 struct cli_flush_state *state;
6568 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
6572 SSVAL(state->vwv + 0, 0, fnum);
6574 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
6576 if (tevent_req_nomem(subreq, req)) {
6577 return tevent_req_post(req, ev);
6579 tevent_req_set_callback(subreq, cli_flush_done, req);
6583 static void cli_flush_done(struct tevent_req *subreq)
6585 struct tevent_req *req = tevent_req_callback_data(
6586 subreq, struct tevent_req);
6589 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
6590 TALLOC_FREE(subreq);
6591 if (tevent_req_nterror(req, status)) {
6594 tevent_req_done(req);
6597 NTSTATUS cli_flush_recv(struct tevent_req *req)
6599 return tevent_req_simple_recv_ntstatus(req);
6602 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
6604 TALLOC_CTX *frame = talloc_stackframe();
6605 struct tevent_context *ev;
6606 struct tevent_req *req;
6607 NTSTATUS status = NT_STATUS_NO_MEMORY;
6609 if (smbXcli_conn_has_async_calls(cli->conn)) {
6611 * Can't use sync call while an async call is in flight
6613 status = NT_STATUS_INVALID_PARAMETER;
6616 ev = samba_tevent_context_init(frame);
6620 req = cli_flush_send(frame, ev, cli, fnum);
6624 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6627 status = cli_flush_recv(req);
6633 struct cli_shadow_copy_data_state {
6640 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
6642 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
6643 struct tevent_context *ev,
6644 struct cli_state *cli,
6648 struct tevent_req *req, *subreq;
6649 struct cli_shadow_copy_data_state *state;
6652 req = tevent_req_create(mem_ctx, &state,
6653 struct cli_shadow_copy_data_state);
6657 state->get_names = get_names;
6658 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
6660 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
6661 SSVAL(state->setup + 2, 0, fnum);
6662 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
6663 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
6665 subreq = cli_trans_send(
6666 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
6667 state->setup, ARRAY_SIZE(state->setup),
6668 ARRAY_SIZE(state->setup),
6671 if (tevent_req_nomem(subreq, req)) {
6672 return tevent_req_post(req, ev);
6674 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
6678 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
6680 struct tevent_req *req = tevent_req_callback_data(
6681 subreq, struct tevent_req);
6682 struct cli_shadow_copy_data_state *state = tevent_req_data(
6683 req, struct cli_shadow_copy_data_state);
6686 status = cli_trans_recv(subreq, state, NULL,
6687 NULL, 0, NULL, /* setup */
6688 NULL, 0, NULL, /* param */
6689 &state->data, 12, &state->num_data);
6690 TALLOC_FREE(subreq);
6691 if (tevent_req_nterror(req, status)) {
6694 tevent_req_done(req);
6697 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6698 char ***pnames, int *pnum_names)
6700 struct cli_shadow_copy_data_state *state = tevent_req_data(
6701 req, struct cli_shadow_copy_data_state);
6702 char **names = NULL;
6703 uint32_t i, num_names;
6705 uint8_t *endp = NULL;
6708 if (tevent_req_is_nterror(req, &status)) {
6712 if (state->num_data < 16) {
6713 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6716 num_names = IVAL(state->data, 4);
6717 dlength = IVAL(state->data, 8);
6719 if (num_names > 0x7FFFFFFF) {
6720 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6723 if (!state->get_names) {
6724 *pnum_names = (int)num_names;
6725 return NT_STATUS_OK;
6728 if (dlength + 12 < 12) {
6729 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6731 if (dlength + 12 > state->num_data) {
6732 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6734 if (state->num_data + (2 * sizeof(SHADOW_COPY_LABEL)) <
6736 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6739 names = talloc_array(mem_ctx, char *, num_names);
6740 if (names == NULL) {
6741 return NT_STATUS_NO_MEMORY;
6744 endp = state->data + state->num_data;
6746 for (i=0; i<num_names; i++) {
6749 size_t converted_size;
6751 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6753 if (src + (2 * sizeof(SHADOW_COPY_LABEL)) > endp) {
6754 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6757 ret = convert_string_talloc(
6758 names, CH_UTF16LE, CH_UNIX,
6759 src, 2 * sizeof(SHADOW_COPY_LABEL),
6760 &names[i], &converted_size);
6763 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6766 *pnum_names = (int)num_names;
6768 return NT_STATUS_OK;
6771 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6772 uint16_t fnum, bool get_names,
6773 char ***pnames, int *pnum_names)
6775 TALLOC_CTX *frame = NULL;
6776 struct tevent_context *ev;
6777 struct tevent_req *req;
6778 NTSTATUS status = NT_STATUS_NO_MEMORY;
6780 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6781 return cli_smb2_shadow_copy_data(mem_ctx,
6789 frame = talloc_stackframe();
6791 if (smbXcli_conn_has_async_calls(cli->conn)) {
6793 * Can't use sync call while an async call is in flight
6795 status = NT_STATUS_INVALID_PARAMETER;
6798 ev = samba_tevent_context_init(frame);
6802 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6806 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6809 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);