2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 struct cli_setpathinfo_state {
37 static void cli_setpathinfo_done(struct tevent_req *subreq);
39 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
40 struct tevent_context *ev,
41 struct cli_state *cli,
47 struct tevent_req *req, *subreq;
48 struct cli_setpathinfo_state *state;
49 uint16_t additional_flags2 = 0;
51 req = tevent_req_create(mem_ctx, &state,
52 struct cli_setpathinfo_state);
57 /* Setup setup word. */
58 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
60 /* Setup param array. */
61 state->param = talloc_zero_array(state, uint8_t, 6);
62 if (tevent_req_nomem(state->param, req)) {
63 return tevent_req_post(req, ev);
65 SSVAL(state->param, 0, level);
67 state->param = trans2_bytes_push_str(
68 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
69 if (tevent_req_nomem(state->param, req)) {
70 return tevent_req_post(req, ev);
73 if (clistr_is_previous_version_path(path, NULL, NULL, NULL) &&
74 !INFO_LEVEL_IS_UNIX(level)) {
75 additional_flags2 = FLAGS2_REPARSE_PATH;
78 subreq = cli_trans_send(
82 additional_flags2, /* additional_flags2 */
84 NULL, /* pipe name. */
88 &state->setup, /* setup. */
89 1, /* num setup uint16_t words. */
90 0, /* max returned setup. */
91 state->param, /* param. */
92 talloc_get_size(state->param), /* num param. */
93 2, /* max returned param. */
95 data_len, /* num data. */
96 0); /* max returned data. */
98 if (tevent_req_nomem(subreq, req)) {
99 return tevent_req_post(req, ev);
101 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
105 static void cli_setpathinfo_done(struct tevent_req *subreq)
107 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
108 NULL, 0, NULL, NULL, 0, NULL);
109 tevent_req_simple_finish_ntstatus(subreq, status);
112 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
114 return tevent_req_simple_recv_ntstatus(req);
117 NTSTATUS cli_setpathinfo(struct cli_state *cli,
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct tevent_context *ev;
125 struct tevent_req *req;
126 NTSTATUS status = NT_STATUS_NO_MEMORY;
128 if (smbXcli_conn_has_async_calls(cli->conn)) {
130 * Can't use sync call while an async call is in flight
132 status = NT_STATUS_INVALID_PARAMETER;
135 ev = samba_tevent_context_init(frame);
139 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
143 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
146 status = cli_setpathinfo_recv(req);
152 /****************************************************************************
153 Hard/Symlink a file (UNIX extensions).
154 Creates new name (sym)linked to 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 /****************************************************************************
205 Symlink a file (UNIX extensions).
206 ****************************************************************************/
208 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
209 struct tevent_context *ev,
210 struct cli_state *cli,
211 const char *link_target,
214 return cli_posix_link_internal_send(
215 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, link_target, newname);
218 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
220 return tevent_req_simple_recv_ntstatus(req);
223 NTSTATUS cli_posix_symlink(struct cli_state *cli,
224 const char *link_target,
227 TALLOC_CTX *frame = talloc_stackframe();
228 struct tevent_context *ev = NULL;
229 struct tevent_req *req = NULL;
230 NTSTATUS status = NT_STATUS_OK;
232 if (smbXcli_conn_has_async_calls(cli->conn)) {
234 * Can't use sync call while an async call is in flight
236 status = NT_STATUS_INVALID_PARAMETER;
240 ev = samba_tevent_context_init(frame);
242 status = NT_STATUS_NO_MEMORY;
246 req = cli_posix_symlink_send(frame,
252 status = NT_STATUS_NO_MEMORY;
256 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
260 status = cli_posix_symlink_recv(req);
267 /****************************************************************************
268 Read a POSIX symlink.
269 ****************************************************************************/
271 struct readlink_state {
276 static void cli_posix_readlink_done(struct tevent_req *subreq);
278 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
279 struct tevent_context *ev,
280 struct cli_state *cli,
284 struct tevent_req *req = NULL, *subreq = NULL;
285 struct readlink_state *state = NULL;
286 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
288 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
294 * Len is in bytes, we need it in UCS2 units.
296 if ((2*len < len) || (maxbytelen < len)) {
297 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
298 return tevent_req_post(req, ev);
301 subreq = cli_qpathinfo_send(state, ev, cli, fname,
302 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
303 if (tevent_req_nomem(subreq, req)) {
304 return tevent_req_post(req, ev);
306 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
310 static void cli_posix_readlink_done(struct tevent_req *subreq)
312 struct tevent_req *req = tevent_req_callback_data(
313 subreq, struct tevent_req);
314 struct readlink_state *state = tevent_req_data(
315 req, struct readlink_state);
318 status = cli_qpathinfo_recv(subreq, state, &state->data,
321 if (tevent_req_nterror(req, status)) {
325 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
327 if (state->data[state->num_data-1] != '\0') {
328 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
331 tevent_req_done(req);
334 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
335 char *retpath, size_t len)
338 char *converted = NULL;
339 size_t converted_size = 0;
340 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
342 if (tevent_req_is_nterror(req, &status)) {
345 /* The returned data is a pushed string, not raw data. */
346 if (!convert_string_talloc(state,
347 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
353 return NT_STATUS_NO_MEMORY;
356 len = MIN(len,converted_size);
358 return NT_STATUS_DATA_ERROR;
360 memcpy(retpath, converted, len);
364 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
365 char *linkpath, size_t len)
367 TALLOC_CTX *frame = talloc_stackframe();
368 struct tevent_context *ev = NULL;
369 struct tevent_req *req = NULL;
370 NTSTATUS status = NT_STATUS_OK;
372 if (smbXcli_conn_has_async_calls(cli->conn)) {
374 * Can't use sync call while an async call is in flight
376 status = NT_STATUS_INVALID_PARAMETER;
380 ev = samba_tevent_context_init(frame);
382 status = NT_STATUS_NO_MEMORY;
386 req = cli_posix_readlink_send(frame,
392 status = NT_STATUS_NO_MEMORY;
396 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
400 status = cli_posix_readlink_recv(req, cli, linkpath, len);
407 /****************************************************************************
408 Hard link a file (UNIX extensions).
409 ****************************************************************************/
411 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
412 struct tevent_context *ev,
413 struct cli_state *cli,
417 return cli_posix_link_internal_send(
418 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
421 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
423 return tevent_req_simple_recv_ntstatus(req);
426 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
430 TALLOC_CTX *frame = talloc_stackframe();
431 struct tevent_context *ev = NULL;
432 struct tevent_req *req = NULL;
433 NTSTATUS status = NT_STATUS_OK;
435 if (smbXcli_conn_has_async_calls(cli->conn)) {
437 * Can't use sync call while an async call is in flight
439 status = NT_STATUS_INVALID_PARAMETER;
443 ev = samba_tevent_context_init(frame);
445 status = NT_STATUS_NO_MEMORY;
449 req = cli_posix_hardlink_send(frame,
455 status = NT_STATUS_NO_MEMORY;
459 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
463 status = cli_posix_hardlink_recv(req);
470 /****************************************************************************
471 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
472 ****************************************************************************/
474 struct getacl_state {
479 static void cli_posix_getacl_done(struct tevent_req *subreq);
481 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
482 struct tevent_context *ev,
483 struct cli_state *cli,
486 struct tevent_req *req = NULL, *subreq = NULL;
487 struct getacl_state *state = NULL;
489 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
493 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
495 if (tevent_req_nomem(subreq, req)) {
496 return tevent_req_post(req, ev);
498 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
502 static void cli_posix_getacl_done(struct tevent_req *subreq)
504 struct tevent_req *req = tevent_req_callback_data(
505 subreq, struct tevent_req);
506 struct getacl_state *state = tevent_req_data(
507 req, struct getacl_state);
510 status = cli_qpathinfo_recv(subreq, state, &state->data,
513 if (tevent_req_nterror(req, status)) {
516 tevent_req_done(req);
519 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
524 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
527 if (tevent_req_is_nterror(req, &status)) {
530 *prb_size = (size_t)state->num_data;
531 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
535 NTSTATUS cli_posix_getacl(struct cli_state *cli,
541 TALLOC_CTX *frame = talloc_stackframe();
542 struct tevent_context *ev = NULL;
543 struct tevent_req *req = NULL;
544 NTSTATUS status = NT_STATUS_OK;
546 if (smbXcli_conn_has_async_calls(cli->conn)) {
548 * Can't use sync call while an async call is in flight
550 status = NT_STATUS_INVALID_PARAMETER;
554 ev = samba_tevent_context_init(frame);
556 status = NT_STATUS_NO_MEMORY;
560 req = cli_posix_getacl_send(frame,
565 status = NT_STATUS_NO_MEMORY;
569 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
573 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
580 /****************************************************************************
581 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
582 ****************************************************************************/
584 struct setacl_state {
588 static void cli_posix_setacl_done(struct tevent_req *subreq);
590 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
591 struct tevent_context *ev,
592 struct cli_state *cli,
597 struct tevent_req *req = NULL, *subreq = NULL;
598 struct setacl_state *state = NULL;
600 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
604 state->data = talloc_memdup(state, data, num_data);
605 if (tevent_req_nomem(state->data, req)) {
606 return tevent_req_post(req, ev);
609 subreq = cli_setpathinfo_send(state,
616 if (tevent_req_nomem(subreq, req)) {
617 return tevent_req_post(req, ev);
619 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
623 static void cli_posix_setacl_done(struct tevent_req *subreq)
625 NTSTATUS status = cli_setpathinfo_recv(subreq);
626 tevent_req_simple_finish_ntstatus(subreq, status);
629 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
631 return tevent_req_simple_recv_ntstatus(req);
634 NTSTATUS cli_posix_setacl(struct cli_state *cli,
639 TALLOC_CTX *frame = talloc_stackframe();
640 struct tevent_context *ev = NULL;
641 struct tevent_req *req = NULL;
642 NTSTATUS status = NT_STATUS_OK;
644 if (smbXcli_conn_has_async_calls(cli->conn)) {
646 * Can't use sync call while an async call is in flight
648 status = NT_STATUS_INVALID_PARAMETER;
652 ev = samba_tevent_context_init(frame);
654 status = NT_STATUS_NO_MEMORY;
658 req = cli_posix_setacl_send(frame,
665 status = NT_STATUS_NO_MEMORY;
669 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
673 status = cli_posix_setacl_recv(req);
680 /****************************************************************************
681 Stat a file (UNIX extensions).
682 ****************************************************************************/
689 static void cli_posix_stat_done(struct tevent_req *subreq);
691 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
692 struct tevent_context *ev,
693 struct cli_state *cli,
696 struct tevent_req *req = NULL, *subreq = NULL;
697 struct stat_state *state = NULL;
699 req = tevent_req_create(mem_ctx, &state, struct stat_state);
703 subreq = cli_qpathinfo_send(state, ev, cli, fname,
704 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
705 if (tevent_req_nomem(subreq, req)) {
706 return tevent_req_post(req, ev);
708 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
712 static void cli_posix_stat_done(struct tevent_req *subreq)
714 struct tevent_req *req = tevent_req_callback_data(
715 subreq, struct tevent_req);
716 struct stat_state *state = tevent_req_data(req, struct stat_state);
719 status = cli_qpathinfo_recv(subreq, state, &state->data,
722 if (tevent_req_nterror(req, status)) {
725 tevent_req_done(req);
728 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
729 SMB_STRUCT_STAT *sbuf)
731 struct stat_state *state = tevent_req_data(req, struct stat_state);
734 if (tevent_req_is_nterror(req, &status)) {
738 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
739 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
740 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
741 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
743 /* assume 512 byte blocks */
744 sbuf->st_ex_blocks /= 512;
746 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
747 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
748 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
750 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
751 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
752 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
753 #if defined(HAVE_MAKEDEV)
755 uint32_t dev_major = IVAL(state->data,60);
756 uint32_t dev_minor = IVAL(state->data,68);
757 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
760 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
761 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
762 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
767 NTSTATUS cli_posix_stat(struct cli_state *cli,
769 SMB_STRUCT_STAT *sbuf)
771 TALLOC_CTX *frame = talloc_stackframe();
772 struct tevent_context *ev = NULL;
773 struct tevent_req *req = NULL;
774 NTSTATUS status = NT_STATUS_OK;
776 if (smbXcli_conn_has_async_calls(cli->conn)) {
778 * Can't use sync call while an async call is in flight
780 status = NT_STATUS_INVALID_PARAMETER;
784 ev = samba_tevent_context_init(frame);
786 status = NT_STATUS_NO_MEMORY;
790 req = cli_posix_stat_send(frame,
795 status = NT_STATUS_NO_MEMORY;
799 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
803 status = cli_posix_stat_recv(req, sbuf);
810 /****************************************************************************
811 Chmod or chown a file internal (UNIX extensions).
812 ****************************************************************************/
814 struct cli_posix_chown_chmod_internal_state {
818 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
820 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
821 struct tevent_context *ev,
822 struct cli_state *cli,
828 struct tevent_req *req = NULL, *subreq = NULL;
829 struct cli_posix_chown_chmod_internal_state *state = NULL;
831 req = tevent_req_create(mem_ctx, &state,
832 struct cli_posix_chown_chmod_internal_state);
837 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
838 memset(&state->data[40], '\0', 60);
839 SIVAL(state->data,40,uid);
840 SIVAL(state->data,48,gid);
841 SIVAL(state->data,84,mode);
843 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
844 fname, state->data, sizeof(state->data));
845 if (tevent_req_nomem(subreq, req)) {
846 return tevent_req_post(req, ev);
848 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
853 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
855 NTSTATUS status = cli_setpathinfo_recv(subreq);
856 tevent_req_simple_finish_ntstatus(subreq, status);
859 /****************************************************************************
860 chmod a file (UNIX extensions).
861 ****************************************************************************/
863 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
864 struct tevent_context *ev,
865 struct cli_state *cli,
869 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
871 unix_perms_to_wire(mode),
876 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
878 return tevent_req_simple_recv_ntstatus(req);
881 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
883 TALLOC_CTX *frame = talloc_stackframe();
884 struct tevent_context *ev = NULL;
885 struct tevent_req *req = NULL;
886 NTSTATUS status = NT_STATUS_OK;
888 if (smbXcli_conn_has_async_calls(cli->conn)) {
890 * Can't use sync call while an async call is in flight
892 status = NT_STATUS_INVALID_PARAMETER;
896 ev = samba_tevent_context_init(frame);
898 status = NT_STATUS_NO_MEMORY;
902 req = cli_posix_chmod_send(frame,
908 status = NT_STATUS_NO_MEMORY;
912 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
916 status = cli_posix_chmod_recv(req);
923 /****************************************************************************
924 chown a file (UNIX extensions).
925 ****************************************************************************/
927 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
928 struct tevent_context *ev,
929 struct cli_state *cli,
934 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
941 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
943 return tevent_req_simple_recv_ntstatus(req);
946 NTSTATUS cli_posix_chown(struct cli_state *cli,
951 TALLOC_CTX *frame = talloc_stackframe();
952 struct tevent_context *ev = NULL;
953 struct tevent_req *req = NULL;
954 NTSTATUS status = NT_STATUS_OK;
956 if (smbXcli_conn_has_async_calls(cli->conn)) {
958 * Can't use sync call while an async call is in flight
960 status = NT_STATUS_INVALID_PARAMETER;
964 ev = samba_tevent_context_init(frame);
966 status = NT_STATUS_NO_MEMORY;
970 req = cli_posix_chown_send(frame,
977 status = NT_STATUS_NO_MEMORY;
981 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
985 status = cli_posix_chown_recv(req);
992 /****************************************************************************
994 ****************************************************************************/
996 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
997 struct tevent_context *ev,
998 struct cli_state *cli,
999 const char *fname_src,
1000 const char *fname_dst,
1003 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1004 struct tevent_context *ev,
1005 struct cli_state *cli,
1006 const char *fname_src,
1007 const char *fname_dst,
1010 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1011 struct tevent_context *ev,
1012 struct cli_state *cli,
1013 const char *fname_src,
1014 const char *fname_dst,
1017 if (replace && smbXcli_conn_support_passthrough(cli->conn)) {
1018 return cli_smb1_rename_send(mem_ctx, ev, cli, fname_src,
1019 fname_dst, replace);
1021 return cli_cifs_rename_send(mem_ctx, ev, cli, fname_src,
1022 fname_dst, replace);
1026 struct cli_smb1_rename_state {
1030 static void cli_smb1_rename_done(struct tevent_req *subreq);
1032 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1033 struct tevent_context *ev,
1034 struct cli_state *cli,
1035 const char *fname_src,
1036 const char *fname_dst,
1040 struct tevent_req *req = NULL, *subreq = NULL;
1041 struct cli_smb1_rename_state *state = NULL;
1042 smb_ucs2_t *converted_str = NULL;
1043 size_t converted_size_bytes = 0;
1045 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_rename_state);
1050 if (!push_ucs2_talloc(talloc_tos(), &converted_str, fname_dst,
1051 &converted_size_bytes)) {
1052 status = NT_STATUS_INVALID_PARAMETER;
1056 /* W2K8 insists the dest name is not null
1057 terminated. Remove the last 2 zero bytes
1058 and reduce the name length. */
1060 if (converted_size_bytes < 2) {
1061 status = NT_STATUS_INVALID_PARAMETER;
1064 converted_size_bytes -= 2;
1067 talloc_zero_array(state, uint8_t, 12 + converted_size_bytes);
1068 if (state->data == NULL) {
1069 status = NT_STATUS_NO_MEMORY;
1074 SCVAL(state->data, 0, 1);
1077 SIVAL(state->data, 8, converted_size_bytes);
1078 memcpy(state->data + 12, converted_str, converted_size_bytes);
1080 TALLOC_FREE(converted_str);
1082 subreq = cli_setpathinfo_send(
1083 state, ev, cli, SMB_FILE_RENAME_INFORMATION, fname_src, state->data,
1084 talloc_get_size(state->data));
1085 if (tevent_req_nomem(subreq, req)) {
1086 status = NT_STATUS_NO_MEMORY;
1089 tevent_req_set_callback(subreq, cli_smb1_rename_done, req);
1093 TALLOC_FREE(converted_str);
1094 tevent_req_nterror(req, status);
1095 return tevent_req_post(req, ev);
1098 static void cli_smb1_rename_done(struct tevent_req *subreq)
1100 NTSTATUS status = cli_setpathinfo_recv(subreq);
1101 tevent_req_simple_finish_ntstatus(subreq, status);
1104 static void cli_cifs_rename_done(struct tevent_req *subreq);
1106 struct cli_cifs_rename_state {
1110 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1111 struct tevent_context *ev,
1112 struct cli_state *cli,
1113 const char *fname_src,
1114 const char *fname_dst,
1117 struct tevent_req *req = NULL, *subreq = NULL;
1118 struct cli_cifs_rename_state *state = NULL;
1119 uint8_t additional_flags = 0;
1120 uint16_t additional_flags2 = 0;
1121 uint8_t *bytes = NULL;
1123 req = tevent_req_create(mem_ctx, &state, struct cli_cifs_rename_state);
1130 * CIFS doesn't support replace
1132 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1133 return tevent_req_post(req, ev);
1136 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1138 bytes = talloc_array(state, uint8_t, 1);
1139 if (tevent_req_nomem(bytes, req)) {
1140 return tevent_req_post(req, ev);
1143 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1144 strlen(fname_src)+1, NULL);
1145 if (tevent_req_nomem(bytes, req)) {
1146 return tevent_req_post(req, ev);
1149 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1150 additional_flags2 = FLAGS2_REPARSE_PATH;
1153 bytes = talloc_realloc(state, bytes, uint8_t,
1154 talloc_get_size(bytes)+1);
1155 if (tevent_req_nomem(bytes, req)) {
1156 return tevent_req_post(req, ev);
1159 bytes[talloc_get_size(bytes)-1] = 4;
1160 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1161 strlen(fname_dst)+1, NULL);
1162 if (tevent_req_nomem(bytes, req)) {
1163 return tevent_req_post(req, ev);
1166 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1168 1, state->vwv, talloc_get_size(bytes), bytes);
1169 if (tevent_req_nomem(subreq, req)) {
1170 return tevent_req_post(req, ev);
1172 tevent_req_set_callback(subreq, cli_cifs_rename_done, req);
1176 static void cli_cifs_rename_done(struct tevent_req *subreq)
1178 struct tevent_req *req = tevent_req_callback_data(
1179 subreq, struct tevent_req);
1182 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1183 TALLOC_FREE(subreq);
1184 if (tevent_req_nterror(req, status)) {
1187 tevent_req_done(req);
1190 NTSTATUS cli_rename_recv(struct tevent_req *req)
1192 return tevent_req_simple_recv_ntstatus(req);
1195 NTSTATUS cli_rename(struct cli_state *cli,
1196 const char *fname_src,
1197 const char *fname_dst,
1200 TALLOC_CTX *frame = NULL;
1201 struct tevent_context *ev;
1202 struct tevent_req *req;
1203 NTSTATUS status = NT_STATUS_OK;
1205 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1206 return cli_smb2_rename(cli, fname_src, fname_dst, replace);
1209 frame = talloc_stackframe();
1211 if (smbXcli_conn_has_async_calls(cli->conn)) {
1213 * Can't use sync call while an async call is in flight
1215 status = NT_STATUS_INVALID_PARAMETER;
1219 ev = samba_tevent_context_init(frame);
1221 status = NT_STATUS_NO_MEMORY;
1225 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst, replace);
1227 status = NT_STATUS_NO_MEMORY;
1231 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1235 status = cli_rename_recv(req);
1242 /****************************************************************************
1244 ****************************************************************************/
1246 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1248 struct cli_ntrename_internal_state {
1252 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1253 struct tevent_context *ev,
1254 struct cli_state *cli,
1255 const char *fname_src,
1256 const char *fname_dst,
1257 uint16_t rename_flag)
1259 struct tevent_req *req = NULL, *subreq = NULL;
1260 struct cli_ntrename_internal_state *state = NULL;
1261 uint8_t additional_flags = 0;
1262 uint16_t additional_flags2 = 0;
1263 uint8_t *bytes = NULL;
1265 req = tevent_req_create(mem_ctx, &state,
1266 struct cli_ntrename_internal_state);
1271 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1272 SSVAL(state->vwv+1, 0, rename_flag);
1274 bytes = talloc_array(state, uint8_t, 1);
1275 if (tevent_req_nomem(bytes, req)) {
1276 return tevent_req_post(req, ev);
1279 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1280 strlen(fname_src)+1, NULL);
1281 if (tevent_req_nomem(bytes, req)) {
1282 return tevent_req_post(req, ev);
1285 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1286 additional_flags2 = FLAGS2_REPARSE_PATH;
1289 bytes = talloc_realloc(state, bytes, uint8_t,
1290 talloc_get_size(bytes)+1);
1291 if (tevent_req_nomem(bytes, req)) {
1292 return tevent_req_post(req, ev);
1295 bytes[talloc_get_size(bytes)-1] = 4;
1296 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1297 strlen(fname_dst)+1, NULL);
1298 if (tevent_req_nomem(bytes, req)) {
1299 return tevent_req_post(req, ev);
1302 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1304 4, state->vwv, talloc_get_size(bytes), bytes);
1305 if (tevent_req_nomem(subreq, req)) {
1306 return tevent_req_post(req, ev);
1308 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1312 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1314 struct tevent_req *req = tevent_req_callback_data(
1315 subreq, struct tevent_req);
1318 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1319 TALLOC_FREE(subreq);
1320 if (tevent_req_nterror(req, status)) {
1323 tevent_req_done(req);
1326 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1328 return tevent_req_simple_recv_ntstatus(req);
1331 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1332 struct tevent_context *ev,
1333 struct cli_state *cli,
1334 const char *fname_src,
1335 const char *fname_dst)
1337 return cli_ntrename_internal_send(mem_ctx,
1342 RENAME_FLAG_RENAME);
1345 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1347 return cli_ntrename_internal_recv(req);
1350 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1352 TALLOC_CTX *frame = talloc_stackframe();
1353 struct tevent_context *ev;
1354 struct tevent_req *req;
1355 NTSTATUS status = NT_STATUS_OK;
1357 if (smbXcli_conn_has_async_calls(cli->conn)) {
1359 * Can't use sync call while an async call is in flight
1361 status = NT_STATUS_INVALID_PARAMETER;
1365 ev = samba_tevent_context_init(frame);
1367 status = NT_STATUS_NO_MEMORY;
1371 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1373 status = NT_STATUS_NO_MEMORY;
1377 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1381 status = cli_ntrename_recv(req);
1388 /****************************************************************************
1390 ****************************************************************************/
1392 struct tevent_req *cli_nt_hardlink_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)
1398 return cli_ntrename_internal_send(mem_ctx,
1403 RENAME_FLAG_HARD_LINK);
1406 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1408 return cli_ntrename_internal_recv(req);
1411 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1413 TALLOC_CTX *frame = talloc_stackframe();
1414 struct tevent_context *ev;
1415 struct tevent_req *req;
1416 NTSTATUS status = NT_STATUS_OK;
1418 if (smbXcli_conn_has_async_calls(cli->conn)) {
1420 * Can't use sync call while an async call is in flight
1422 status = NT_STATUS_INVALID_PARAMETER;
1426 ev = samba_tevent_context_init(frame);
1428 status = NT_STATUS_NO_MEMORY;
1432 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1434 status = NT_STATUS_NO_MEMORY;
1438 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1442 status = cli_nt_hardlink_recv(req);
1449 /****************************************************************************
1451 ****************************************************************************/
1453 static void cli_unlink_done(struct tevent_req *subreq);
1455 struct cli_unlink_state {
1459 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1460 struct tevent_context *ev,
1461 struct cli_state *cli,
1463 uint16_t mayhave_attrs)
1465 struct tevent_req *req = NULL, *subreq = NULL;
1466 struct cli_unlink_state *state = NULL;
1467 uint8_t additional_flags = 0;
1468 uint16_t additional_flags2 = 0;
1469 uint8_t *bytes = NULL;
1471 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1476 SSVAL(state->vwv+0, 0, mayhave_attrs);
1478 bytes = talloc_array(state, uint8_t, 1);
1479 if (tevent_req_nomem(bytes, req)) {
1480 return tevent_req_post(req, ev);
1483 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1484 strlen(fname)+1, NULL);
1486 if (tevent_req_nomem(bytes, req)) {
1487 return tevent_req_post(req, ev);
1490 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1491 additional_flags2 = FLAGS2_REPARSE_PATH;
1494 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1496 1, state->vwv, talloc_get_size(bytes), bytes);
1497 if (tevent_req_nomem(subreq, req)) {
1498 return tevent_req_post(req, ev);
1500 tevent_req_set_callback(subreq, cli_unlink_done, req);
1504 static void cli_unlink_done(struct tevent_req *subreq)
1506 struct tevent_req *req = tevent_req_callback_data(
1507 subreq, struct tevent_req);
1510 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1511 TALLOC_FREE(subreq);
1512 if (tevent_req_nterror(req, status)) {
1515 tevent_req_done(req);
1518 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1520 return tevent_req_simple_recv_ntstatus(req);
1523 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1525 TALLOC_CTX *frame = NULL;
1526 struct tevent_context *ev;
1527 struct tevent_req *req;
1528 NTSTATUS status = NT_STATUS_OK;
1530 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1531 return cli_smb2_unlink(cli, fname);
1534 frame = talloc_stackframe();
1536 if (smbXcli_conn_has_async_calls(cli->conn)) {
1538 * Can't use sync call while an async call is in flight
1540 status = NT_STATUS_INVALID_PARAMETER;
1544 ev = samba_tevent_context_init(frame);
1546 status = NT_STATUS_NO_MEMORY;
1550 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1552 status = NT_STATUS_NO_MEMORY;
1556 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1560 status = cli_unlink_recv(req);
1567 /****************************************************************************
1569 ****************************************************************************/
1571 static void cli_mkdir_done(struct tevent_req *subreq);
1573 struct cli_mkdir_state {
1577 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1578 struct tevent_context *ev,
1579 struct cli_state *cli,
1582 struct tevent_req *req = NULL, *subreq = NULL;
1583 struct cli_mkdir_state *state = NULL;
1584 uint8_t additional_flags = 0;
1585 uint16_t additional_flags2 = 0;
1586 uint8_t *bytes = NULL;
1588 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1593 bytes = talloc_array(state, uint8_t, 1);
1594 if (tevent_req_nomem(bytes, req)) {
1595 return tevent_req_post(req, ev);
1598 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1599 strlen(dname)+1, NULL);
1601 if (tevent_req_nomem(bytes, req)) {
1602 return tevent_req_post(req, ev);
1605 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1606 additional_flags2 = FLAGS2_REPARSE_PATH;
1609 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1611 0, NULL, talloc_get_size(bytes), bytes);
1612 if (tevent_req_nomem(subreq, req)) {
1613 return tevent_req_post(req, ev);
1615 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1619 static void cli_mkdir_done(struct tevent_req *subreq)
1621 struct tevent_req *req = tevent_req_callback_data(
1622 subreq, struct tevent_req);
1625 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1626 TALLOC_FREE(subreq);
1627 if (tevent_req_nterror(req, status)) {
1630 tevent_req_done(req);
1633 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1635 return tevent_req_simple_recv_ntstatus(req);
1638 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1640 TALLOC_CTX *frame = NULL;
1641 struct tevent_context *ev;
1642 struct tevent_req *req;
1643 NTSTATUS status = NT_STATUS_OK;
1645 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1646 return cli_smb2_mkdir(cli, dname);
1649 frame = talloc_stackframe();
1651 if (smbXcli_conn_has_async_calls(cli->conn)) {
1653 * Can't use sync call while an async call is in flight
1655 status = NT_STATUS_INVALID_PARAMETER;
1659 ev = samba_tevent_context_init(frame);
1661 status = NT_STATUS_NO_MEMORY;
1665 req = cli_mkdir_send(frame, ev, cli, dname);
1667 status = NT_STATUS_NO_MEMORY;
1671 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1675 status = cli_mkdir_recv(req);
1682 /****************************************************************************
1684 ****************************************************************************/
1686 static void cli_rmdir_done(struct tevent_req *subreq);
1688 struct cli_rmdir_state {
1692 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1693 struct tevent_context *ev,
1694 struct cli_state *cli,
1697 struct tevent_req *req = NULL, *subreq = NULL;
1698 struct cli_rmdir_state *state = NULL;
1699 uint8_t additional_flags = 0;
1700 uint16_t additional_flags2 = 0;
1701 uint8_t *bytes = NULL;
1703 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1708 bytes = talloc_array(state, uint8_t, 1);
1709 if (tevent_req_nomem(bytes, req)) {
1710 return tevent_req_post(req, ev);
1713 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1714 strlen(dname)+1, NULL);
1716 if (tevent_req_nomem(bytes, req)) {
1717 return tevent_req_post(req, ev);
1720 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1721 additional_flags2 = FLAGS2_REPARSE_PATH;
1724 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1726 0, NULL, talloc_get_size(bytes), bytes);
1727 if (tevent_req_nomem(subreq, req)) {
1728 return tevent_req_post(req, ev);
1730 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1734 static void cli_rmdir_done(struct tevent_req *subreq)
1736 struct tevent_req *req = tevent_req_callback_data(
1737 subreq, struct tevent_req);
1740 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1741 TALLOC_FREE(subreq);
1742 if (tevent_req_nterror(req, status)) {
1745 tevent_req_done(req);
1748 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1750 return tevent_req_simple_recv_ntstatus(req);
1753 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1755 TALLOC_CTX *frame = NULL;
1756 struct tevent_context *ev;
1757 struct tevent_req *req;
1758 NTSTATUS status = NT_STATUS_OK;
1760 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1761 return cli_smb2_rmdir(cli, dname);
1764 frame = talloc_stackframe();
1766 if (smbXcli_conn_has_async_calls(cli->conn)) {
1768 * Can't use sync call while an async call is in flight
1770 status = NT_STATUS_INVALID_PARAMETER;
1774 ev = samba_tevent_context_init(frame);
1776 status = NT_STATUS_NO_MEMORY;
1780 req = cli_rmdir_send(frame, ev, cli, dname);
1782 status = NT_STATUS_NO_MEMORY;
1786 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1790 status = cli_rmdir_recv(req);
1797 /****************************************************************************
1798 Set or clear the delete on close flag.
1799 ****************************************************************************/
1807 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq);
1808 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq);
1810 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1811 struct tevent_context *ev,
1812 struct cli_state *cli,
1816 struct tevent_req *req = NULL, *subreq = NULL;
1817 struct doc_state *state = NULL;
1819 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1824 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1825 subreq = cli_smb2_delete_on_close_send(state, ev, cli,
1827 if (tevent_req_nomem(subreq, req)) {
1828 return tevent_req_post(req, ev);
1830 tevent_req_set_callback(subreq,
1831 cli_nt_delete_on_close_smb2_done,
1836 /* Setup setup word. */
1837 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1839 /* Setup param array. */
1840 SSVAL(state->param,0,fnum);
1841 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1843 /* Setup data array. */
1844 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1846 subreq = cli_trans_send(state, /* mem ctx. */
1847 ev, /* event ctx. */
1848 cli, /* cli_state. */
1849 0, /* additional_flags2 */
1850 SMBtrans2, /* cmd. */
1851 NULL, /* pipe name. */
1855 &state->setup, /* setup. */
1856 1, /* num setup uint16_t words. */
1857 0, /* max returned setup. */
1858 state->param, /* param. */
1860 2, /* max returned param. */
1861 state->data, /* data. */
1863 0); /* max returned data. */
1865 if (tevent_req_nomem(subreq, req)) {
1866 return tevent_req_post(req, ev);
1868 tevent_req_set_callback(subreq,
1869 cli_nt_delete_on_close_smb1_done,
1874 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq)
1876 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1877 NULL, 0, NULL, NULL, 0, NULL);
1878 tevent_req_simple_finish_ntstatus(subreq, status);
1881 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq)
1883 NTSTATUS status = cli_smb2_delete_on_close_recv(subreq);
1884 tevent_req_simple_finish_ntstatus(subreq, status);
1887 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1889 return tevent_req_simple_recv_ntstatus(req);
1892 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1894 TALLOC_CTX *frame = talloc_stackframe();
1895 struct tevent_context *ev = NULL;
1896 struct tevent_req *req = NULL;
1897 NTSTATUS status = NT_STATUS_OK;
1899 if (smbXcli_conn_has_async_calls(cli->conn)) {
1901 * Can't use sync call while an async call is in flight
1903 status = NT_STATUS_INVALID_PARAMETER;
1907 ev = samba_tevent_context_init(frame);
1909 status = NT_STATUS_NO_MEMORY;
1913 req = cli_nt_delete_on_close_send(frame,
1919 status = NT_STATUS_NO_MEMORY;
1923 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1927 status = cli_nt_delete_on_close_recv(req);
1934 struct cli_ntcreate1_state {
1937 struct smb_create_returns cr;
1938 struct tevent_req *subreq;
1941 static void cli_ntcreate1_done(struct tevent_req *subreq);
1942 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1944 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1945 struct tevent_context *ev,
1946 struct cli_state *cli,
1948 uint32_t CreatFlags,
1949 uint32_t DesiredAccess,
1950 uint32_t FileAttributes,
1951 uint32_t ShareAccess,
1952 uint32_t CreateDisposition,
1953 uint32_t CreateOptions,
1954 uint8_t SecurityFlags)
1956 struct tevent_req *req, *subreq;
1957 struct cli_ntcreate1_state *state;
1960 size_t converted_len;
1961 uint16_t additional_flags2 = 0;
1963 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1970 SCVAL(vwv+0, 0, 0xFF);
1975 if (cli->use_oplocks) {
1976 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1978 SIVAL(vwv+3, 1, CreatFlags);
1979 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1980 SIVAL(vwv+7, 1, DesiredAccess);
1981 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1982 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1983 SIVAL(vwv+13, 1, FileAttributes);
1984 SIVAL(vwv+15, 1, ShareAccess);
1985 SIVAL(vwv+17, 1, CreateDisposition);
1986 SIVAL(vwv+19, 1, CreateOptions |
1987 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1988 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1989 SCVAL(vwv+23, 1, SecurityFlags);
1991 bytes = talloc_array(state, uint8_t, 0);
1992 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1993 fname, strlen(fname)+1,
1996 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1997 additional_flags2 = FLAGS2_REPARSE_PATH;
2000 /* sigh. this copes with broken netapp filer behaviour */
2001 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
2003 if (tevent_req_nomem(bytes, req)) {
2004 return tevent_req_post(req, ev);
2007 SSVAL(vwv+2, 1, converted_len);
2009 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
2010 additional_flags2, 24, vwv,
2011 talloc_get_size(bytes), bytes);
2012 if (tevent_req_nomem(subreq, req)) {
2013 return tevent_req_post(req, ev);
2015 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
2017 state->subreq = subreq;
2018 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
2023 static void cli_ntcreate1_done(struct tevent_req *subreq)
2025 struct tevent_req *req = tevent_req_callback_data(
2026 subreq, struct tevent_req);
2027 struct cli_ntcreate1_state *state = tevent_req_data(
2028 req, struct cli_ntcreate1_state);
2035 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2036 &num_bytes, &bytes);
2037 TALLOC_FREE(subreq);
2038 if (tevent_req_nterror(req, status)) {
2041 state->cr.oplock_level = CVAL(vwv+2, 0);
2042 state->fnum = SVAL(vwv+2, 1);
2043 state->cr.create_action = IVAL(vwv+3, 1);
2044 state->cr.creation_time = BVAL(vwv+5, 1);
2045 state->cr.last_access_time = BVAL(vwv+9, 1);
2046 state->cr.last_write_time = BVAL(vwv+13, 1);
2047 state->cr.change_time = BVAL(vwv+17, 1);
2048 state->cr.file_attributes = IVAL(vwv+21, 1);
2049 state->cr.allocation_size = BVAL(vwv+23, 1);
2050 state->cr.end_of_file = BVAL(vwv+27, 1);
2052 tevent_req_done(req);
2055 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2057 struct cli_ntcreate1_state *state = tevent_req_data(
2058 req, struct cli_ntcreate1_state);
2059 return tevent_req_cancel(state->subreq);
2062 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2064 struct smb_create_returns *cr)
2066 struct cli_ntcreate1_state *state = tevent_req_data(
2067 req, struct cli_ntcreate1_state);
2070 if (tevent_req_is_nterror(req, &status)) {
2073 *pfnum = state->fnum;
2077 return NT_STATUS_OK;
2080 struct cli_ntcreate_state {
2081 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2082 struct smb_create_returns *cr);
2083 struct smb_create_returns cr;
2085 struct tevent_req *subreq;
2088 static void cli_ntcreate_done(struct tevent_req *subreq);
2089 static bool cli_ntcreate_cancel(struct tevent_req *req);
2091 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2092 struct tevent_context *ev,
2093 struct cli_state *cli,
2095 uint32_t create_flags,
2096 uint32_t desired_access,
2097 uint32_t file_attributes,
2098 uint32_t share_access,
2099 uint32_t create_disposition,
2100 uint32_t create_options,
2101 uint8_t security_flags)
2103 struct tevent_req *req, *subreq;
2104 struct cli_ntcreate_state *state;
2105 uint32_t impersonation_level = SMB2_IMPERSONATION_IMPERSONATION;
2107 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2112 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2113 state->recv = cli_smb2_create_fnum_recv;
2115 if (cli->use_oplocks) {
2116 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2119 subreq = cli_smb2_create_fnum_send(
2120 state, ev, cli, fname, create_flags,
2121 impersonation_level, desired_access,
2122 file_attributes, share_access, create_disposition,
2125 state->recv = cli_ntcreate1_recv;
2126 subreq = cli_ntcreate1_send(
2127 state, ev, cli, fname, create_flags, desired_access,
2128 file_attributes, share_access, create_disposition,
2129 create_options, security_flags);
2131 if (tevent_req_nomem(subreq, req)) {
2132 return tevent_req_post(req, ev);
2134 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2136 state->subreq = subreq;
2137 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2142 static void cli_ntcreate_done(struct tevent_req *subreq)
2144 struct tevent_req *req = tevent_req_callback_data(
2145 subreq, struct tevent_req);
2146 struct cli_ntcreate_state *state = tevent_req_data(
2147 req, struct cli_ntcreate_state);
2150 status = state->recv(subreq, &state->fnum, &state->cr);
2151 TALLOC_FREE(subreq);
2152 if (tevent_req_nterror(req, status)) {
2155 tevent_req_done(req);
2158 static bool cli_ntcreate_cancel(struct tevent_req *req)
2160 struct cli_ntcreate_state *state = tevent_req_data(
2161 req, struct cli_ntcreate_state);
2162 return tevent_req_cancel(state->subreq);
2165 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2166 struct smb_create_returns *cr)
2168 struct cli_ntcreate_state *state = tevent_req_data(
2169 req, struct cli_ntcreate_state);
2172 if (tevent_req_is_nterror(req, &status)) {
2176 *fnum = state->fnum;
2181 return NT_STATUS_OK;
2184 NTSTATUS cli_ntcreate(struct cli_state *cli,
2186 uint32_t CreatFlags,
2187 uint32_t DesiredAccess,
2188 uint32_t FileAttributes,
2189 uint32_t ShareAccess,
2190 uint32_t CreateDisposition,
2191 uint32_t CreateOptions,
2192 uint8_t SecurityFlags,
2194 struct smb_create_returns *cr)
2196 TALLOC_CTX *frame = talloc_stackframe();
2197 struct tevent_context *ev;
2198 struct tevent_req *req;
2199 NTSTATUS status = NT_STATUS_NO_MEMORY;
2201 if (smbXcli_conn_has_async_calls(cli->conn)) {
2203 * Can't use sync call while an async call is in flight
2205 status = NT_STATUS_INVALID_PARAMETER;
2209 ev = samba_tevent_context_init(frame);
2214 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2215 DesiredAccess, FileAttributes, ShareAccess,
2216 CreateDisposition, CreateOptions,
2222 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2226 status = cli_ntcreate_recv(req, pfid, cr);
2232 struct cli_nttrans_create_state {
2234 struct smb_create_returns cr;
2237 static void cli_nttrans_create_done(struct tevent_req *subreq);
2239 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2240 struct tevent_context *ev,
2241 struct cli_state *cli,
2243 uint32_t CreatFlags,
2244 uint32_t DesiredAccess,
2245 uint32_t FileAttributes,
2246 uint32_t ShareAccess,
2247 uint32_t CreateDisposition,
2248 uint32_t CreateOptions,
2249 uint8_t SecurityFlags,
2250 struct security_descriptor *secdesc,
2251 struct ea_struct *eas,
2254 struct tevent_req *req, *subreq;
2255 struct cli_nttrans_create_state *state;
2257 uint8_t *secdesc_buf;
2260 size_t converted_len;
2261 uint16_t additional_flags2 = 0;
2263 req = tevent_req_create(mem_ctx,
2264 &state, struct cli_nttrans_create_state);
2269 if (secdesc != NULL) {
2270 status = marshall_sec_desc(talloc_tos(), secdesc,
2271 &secdesc_buf, &secdesc_len);
2272 if (tevent_req_nterror(req, status)) {
2273 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2274 nt_errstr(status)));
2275 return tevent_req_post(req, ev);
2286 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2287 return tevent_req_post(req, ev);
2290 param = talloc_array(state, uint8_t, 53);
2291 if (tevent_req_nomem(param, req)) {
2292 return tevent_req_post(req, ev);
2295 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2296 fname, strlen(fname),
2298 if (tevent_req_nomem(param, req)) {
2299 return tevent_req_post(req, ev);
2302 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2303 additional_flags2 = FLAGS2_REPARSE_PATH;
2306 SIVAL(param, 0, CreatFlags);
2307 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2308 SIVAL(param, 8, DesiredAccess);
2309 SIVAL(param, 12, 0x0); /* AllocationSize */
2310 SIVAL(param, 16, 0x0); /* AllocationSize */
2311 SIVAL(param, 20, FileAttributes);
2312 SIVAL(param, 24, ShareAccess);
2313 SIVAL(param, 28, CreateDisposition);
2314 SIVAL(param, 32, CreateOptions |
2315 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2316 SIVAL(param, 36, secdesc_len);
2317 SIVAL(param, 40, 0); /* EA length*/
2318 SIVAL(param, 44, converted_len);
2319 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2320 SCVAL(param, 52, SecurityFlags);
2322 subreq = cli_trans_send(state, ev, cli,
2323 additional_flags2, /* additional_flags2 */
2325 NULL, -1, /* name, fid */
2326 NT_TRANSACT_CREATE, 0,
2327 NULL, 0, 0, /* setup */
2328 param, talloc_get_size(param), 128, /* param */
2329 secdesc_buf, secdesc_len, 0); /* data */
2330 if (tevent_req_nomem(subreq, req)) {
2331 return tevent_req_post(req, ev);
2333 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2337 static void cli_nttrans_create_done(struct tevent_req *subreq)
2339 struct tevent_req *req = tevent_req_callback_data(
2340 subreq, struct tevent_req);
2341 struct cli_nttrans_create_state *state = tevent_req_data(
2342 req, struct cli_nttrans_create_state);
2347 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2348 NULL, 0, NULL, /* rsetup */
2349 ¶m, 69, &num_param,
2351 if (tevent_req_nterror(req, status)) {
2354 state->cr.oplock_level = CVAL(param, 0);
2355 state->fnum = SVAL(param, 2);
2356 state->cr.create_action = IVAL(param, 4);
2357 state->cr.creation_time = BVAL(param, 12);
2358 state->cr.last_access_time = BVAL(param, 20);
2359 state->cr.last_write_time = BVAL(param, 28);
2360 state->cr.change_time = BVAL(param, 36);
2361 state->cr.file_attributes = IVAL(param, 44);
2362 state->cr.allocation_size = BVAL(param, 48);
2363 state->cr.end_of_file = BVAL(param, 56);
2366 tevent_req_done(req);
2369 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2371 struct smb_create_returns *cr)
2373 struct cli_nttrans_create_state *state = tevent_req_data(
2374 req, struct cli_nttrans_create_state);
2377 if (tevent_req_is_nterror(req, &status)) {
2380 *fnum = state->fnum;
2384 return NT_STATUS_OK;
2387 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2389 uint32_t CreatFlags,
2390 uint32_t DesiredAccess,
2391 uint32_t FileAttributes,
2392 uint32_t ShareAccess,
2393 uint32_t CreateDisposition,
2394 uint32_t CreateOptions,
2395 uint8_t SecurityFlags,
2396 struct security_descriptor *secdesc,
2397 struct ea_struct *eas,
2400 struct smb_create_returns *cr)
2402 TALLOC_CTX *frame = talloc_stackframe();
2403 struct tevent_context *ev;
2404 struct tevent_req *req;
2405 NTSTATUS status = NT_STATUS_NO_MEMORY;
2407 if (smbXcli_conn_has_async_calls(cli->conn)) {
2409 * Can't use sync call while an async call is in flight
2411 status = NT_STATUS_INVALID_PARAMETER;
2414 ev = samba_tevent_context_init(frame);
2418 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2419 DesiredAccess, FileAttributes,
2420 ShareAccess, CreateDisposition,
2421 CreateOptions, SecurityFlags,
2422 secdesc, eas, num_eas);
2426 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2429 status = cli_nttrans_create_recv(req, pfid, cr);
2435 /****************************************************************************
2437 WARNING: if you open with O_WRONLY then getattrE won't work!
2438 ****************************************************************************/
2440 struct cli_openx_state {
2447 static void cli_openx_done(struct tevent_req *subreq);
2449 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2450 struct tevent_context *ev,
2451 struct cli_state *cli, const char *fname,
2452 int flags, int share_mode,
2453 struct tevent_req **psmbreq)
2455 struct tevent_req *req, *subreq;
2456 struct cli_openx_state *state;
2458 unsigned accessmode;
2459 uint8_t additional_flags;
2460 uint16_t additional_flags2 = 0;
2463 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2469 if (flags & O_CREAT) {
2472 if (!(flags & O_EXCL)) {
2473 if (flags & O_TRUNC)
2479 accessmode = (share_mode<<4);
2481 if ((flags & O_ACCMODE) == O_RDWR) {
2483 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2488 if ((flags & O_SYNC) == O_SYNC) {
2489 accessmode |= (1<<14);
2493 if (share_mode == DENY_FCB) {
2497 SCVAL(state->vwv + 0, 0, 0xFF);
2498 SCVAL(state->vwv + 0, 1, 0);
2499 SSVAL(state->vwv + 1, 0, 0);
2500 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2501 SSVAL(state->vwv + 3, 0, accessmode);
2502 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2503 SSVAL(state->vwv + 5, 0, 0);
2504 SIVAL(state->vwv + 6, 0, 0);
2505 SSVAL(state->vwv + 8, 0, openfn);
2506 SIVAL(state->vwv + 9, 0, 0);
2507 SIVAL(state->vwv + 11, 0, 0);
2508 SIVAL(state->vwv + 13, 0, 0);
2510 additional_flags = 0;
2512 if (cli->use_oplocks) {
2513 /* if using oplocks then ask for a batch oplock via
2514 core and extended methods */
2516 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2517 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2520 bytes = talloc_array(state, uint8_t, 0);
2521 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2522 strlen(fname)+1, NULL);
2524 if (tevent_req_nomem(bytes, req)) {
2525 return tevent_req_post(req, ev);
2528 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2529 additional_flags2 = FLAGS2_REPARSE_PATH;
2532 state->bytes.iov_base = (void *)bytes;
2533 state->bytes.iov_len = talloc_get_size(bytes);
2535 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2536 additional_flags2, 15, state->vwv, 1, &state->bytes);
2537 if (subreq == NULL) {
2541 tevent_req_set_callback(subreq, cli_openx_done, req);
2546 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2547 struct cli_state *cli, const char *fname,
2548 int flags, int share_mode)
2550 struct tevent_req *req, *subreq;
2553 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2559 status = smb1cli_req_chain_submit(&subreq, 1);
2560 if (tevent_req_nterror(req, status)) {
2561 return tevent_req_post(req, ev);
2566 static void cli_openx_done(struct tevent_req *subreq)
2568 struct tevent_req *req = tevent_req_callback_data(
2569 subreq, struct tevent_req);
2570 struct cli_openx_state *state = tevent_req_data(
2571 req, struct cli_openx_state);
2576 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2578 TALLOC_FREE(subreq);
2579 if (tevent_req_nterror(req, status)) {
2582 state->fnum = SVAL(vwv+2, 0);
2583 tevent_req_done(req);
2586 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2588 struct cli_openx_state *state = tevent_req_data(
2589 req, struct cli_openx_state);
2592 if (tevent_req_is_nterror(req, &status)) {
2595 *pfnum = state->fnum;
2596 return NT_STATUS_OK;
2599 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2600 int share_mode, uint16_t *pfnum)
2602 TALLOC_CTX *frame = talloc_stackframe();
2603 struct tevent_context *ev;
2604 struct tevent_req *req;
2605 NTSTATUS status = NT_STATUS_NO_MEMORY;
2607 if (smbXcli_conn_has_async_calls(cli->conn)) {
2609 * Can't use sync call while an async call is in flight
2611 status = NT_STATUS_INVALID_PARAMETER;
2615 ev = samba_tevent_context_init(frame);
2620 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2625 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2629 status = cli_openx_recv(req, pfnum);
2634 /****************************************************************************
2635 Synchronous wrapper function that does an NtCreateX open by preference
2636 and falls back to openX if this fails.
2637 ****************************************************************************/
2639 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2640 int share_mode_in, uint16_t *pfnum)
2643 unsigned int openfn = 0;
2644 unsigned int dos_deny = 0;
2645 uint32_t access_mask, share_mode, create_disposition, create_options;
2646 struct smb_create_returns cr;
2648 /* Do the initial mapping into OpenX parameters. */
2649 if (flags & O_CREAT) {
2652 if (!(flags & O_EXCL)) {
2653 if (flags & O_TRUNC)
2659 dos_deny = (share_mode_in<<4);
2661 if ((flags & O_ACCMODE) == O_RDWR) {
2663 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2668 if ((flags & O_SYNC) == O_SYNC) {
2669 dos_deny |= (1<<14);
2673 if (share_mode_in == DENY_FCB) {
2678 /* Hmmm. This is what I think the above code
2679 should look like if it's using the constants
2682 if (flags & O_CREAT) {
2683 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2685 if (!(flags & O_EXCL)) {
2686 if (flags & O_TRUNC)
2687 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2689 openfn |= OPENX_FILE_EXISTS_OPEN;
2692 dos_deny = SET_DENY_MODE(share_mode_in);
2694 if ((flags & O_ACCMODE) == O_RDWR) {
2695 dos_deny |= DOS_OPEN_RDWR;
2696 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2697 dos_deny |= DOS_OPEN_WRONLY;
2701 if ((flags & O_SYNC) == O_SYNC) {
2702 dos_deny |= FILE_SYNC_OPENMODE;
2706 if (share_mode_in == DENY_FCB) {
2711 if (!map_open_params_to_ntcreate(fname, dos_deny,
2712 openfn, &access_mask,
2713 &share_mode, &create_disposition,
2714 &create_options, NULL)) {
2718 status = cli_ntcreate(cli,
2730 /* Try and cope will all varients of "we don't do this call"
2731 and fall back to openX. */
2733 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2734 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2735 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2736 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2737 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2738 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2739 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2740 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2741 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2745 if (NT_STATUS_IS_OK(status) &&
2746 (create_options & FILE_NON_DIRECTORY_FILE) &&
2747 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2750 * Some (broken) servers return a valid handle
2751 * for directories even if FILE_NON_DIRECTORY_FILE
2752 * is set. Just close the handle and set the
2753 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2755 status = cli_close(cli, *pfnum);
2756 if (!NT_STATUS_IS_OK(status)) {
2759 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2760 /* Set this so libsmbclient can retrieve it. */
2761 cli->raw_status = status;
2768 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2771 /****************************************************************************
2773 ****************************************************************************/
2775 struct cli_smb1_close_state {
2779 static void cli_smb1_close_done(struct tevent_req *subreq);
2781 struct tevent_req *cli_smb1_close_create(TALLOC_CTX *mem_ctx,
2782 struct tevent_context *ev,
2783 struct cli_state *cli,
2785 struct tevent_req **psubreq)
2787 struct tevent_req *req, *subreq;
2788 struct cli_smb1_close_state *state;
2790 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_close_state);
2795 SSVAL(state->vwv+0, 0, fnum);
2796 SIVALS(state->vwv+1, 0, -1);
2798 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
2799 3, state->vwv, 0, NULL);
2800 if (subreq == NULL) {
2804 tevent_req_set_callback(subreq, cli_smb1_close_done, req);
2809 static void cli_smb1_close_done(struct tevent_req *subreq)
2811 struct tevent_req *req = tevent_req_callback_data(
2812 subreq, struct tevent_req);
2815 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2816 TALLOC_FREE(subreq);
2817 if (tevent_req_nterror(req, status)) {
2820 tevent_req_done(req);
2823 struct cli_close_state {
2827 static void cli_close_done(struct tevent_req *subreq);
2829 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2830 struct tevent_context *ev,
2831 struct cli_state *cli,
2834 struct tevent_req *req, *subreq;
2835 struct cli_close_state *state;
2838 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2843 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2844 subreq = cli_smb2_close_fnum_send(state,
2848 if (tevent_req_nomem(subreq, req)) {
2849 return tevent_req_post(req, ev);
2852 struct tevent_req *ch_req = NULL;
2853 subreq = cli_smb1_close_create(state, ev, cli, fnum, &ch_req);
2854 if (tevent_req_nomem(subreq, req)) {
2855 return tevent_req_post(req, ev);
2857 status = smb1cli_req_chain_submit(&ch_req, 1);
2858 if (tevent_req_nterror(req, status)) {
2859 return tevent_req_post(req, ev);
2863 tevent_req_set_callback(subreq, cli_close_done, req);
2867 static void cli_close_done(struct tevent_req *subreq)
2869 struct tevent_req *req = tevent_req_callback_data(
2870 subreq, struct tevent_req);
2871 NTSTATUS status = NT_STATUS_OK;
2872 bool err = tevent_req_is_nterror(subreq, &status);
2874 TALLOC_FREE(subreq);
2876 tevent_req_nterror(req, status);
2879 tevent_req_done(req);
2882 NTSTATUS cli_close_recv(struct tevent_req *req)
2884 return tevent_req_simple_recv_ntstatus(req);
2887 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2889 TALLOC_CTX *frame = NULL;
2890 struct tevent_context *ev;
2891 struct tevent_req *req;
2892 NTSTATUS status = NT_STATUS_OK;
2894 frame = talloc_stackframe();
2896 if (smbXcli_conn_has_async_calls(cli->conn)) {
2898 * Can't use sync call while an async call is in flight
2900 status = NT_STATUS_INVALID_PARAMETER;
2904 ev = samba_tevent_context_init(frame);
2906 status = NT_STATUS_NO_MEMORY;
2910 req = cli_close_send(frame, ev, cli, fnum);
2912 status = NT_STATUS_NO_MEMORY;
2916 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2920 status = cli_close_recv(req);
2926 /****************************************************************************
2927 Truncate a file to a specified size
2928 ****************************************************************************/
2930 struct ftrunc_state {
2936 static void cli_ftruncate_done(struct tevent_req *subreq)
2938 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2939 NULL, 0, NULL, NULL, 0, NULL);
2940 tevent_req_simple_finish_ntstatus(subreq, status);
2943 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2944 struct tevent_context *ev,
2945 struct cli_state *cli,
2949 struct tevent_req *req = NULL, *subreq = NULL;
2950 struct ftrunc_state *state = NULL;
2952 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2957 /* Setup setup word. */
2958 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2960 /* Setup param array. */
2961 SSVAL(state->param,0,fnum);
2962 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2963 SSVAL(state->param,4,0);
2965 /* Setup data array. */
2966 SBVAL(state->data, 0, size);
2968 subreq = cli_trans_send(state, /* mem ctx. */
2969 ev, /* event ctx. */
2970 cli, /* cli_state. */
2971 0, /* additional_flags2 */
2972 SMBtrans2, /* cmd. */
2973 NULL, /* pipe name. */
2977 &state->setup, /* setup. */
2978 1, /* num setup uint16_t words. */
2979 0, /* max returned setup. */
2980 state->param, /* param. */
2982 2, /* max returned param. */
2983 state->data, /* data. */
2985 0); /* max returned data. */
2987 if (tevent_req_nomem(subreq, req)) {
2988 return tevent_req_post(req, ev);
2990 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2994 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2996 return tevent_req_simple_recv_ntstatus(req);
2999 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
3001 TALLOC_CTX *frame = NULL;
3002 struct tevent_context *ev = NULL;
3003 struct tevent_req *req = NULL;
3004 NTSTATUS status = NT_STATUS_OK;
3006 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3007 return cli_smb2_ftruncate(cli, fnum, size);
3010 frame = talloc_stackframe();
3012 if (smbXcli_conn_has_async_calls(cli->conn)) {
3014 * Can't use sync call while an async call is in flight
3016 status = NT_STATUS_INVALID_PARAMETER;
3020 ev = samba_tevent_context_init(frame);
3022 status = NT_STATUS_NO_MEMORY;
3026 req = cli_ftruncate_send(frame,
3032 status = NT_STATUS_NO_MEMORY;
3036 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3040 status = cli_ftruncate_recv(req);
3047 /****************************************************************************
3048 send a lock with a specified locktype
3049 this is used for testing LOCKING_ANDX_CANCEL_LOCK
3050 ****************************************************************************/
3052 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
3053 uint32_t offset, uint32_t len,
3054 int timeout, unsigned char locktype)
3059 unsigned int set_timeout = 0;
3060 unsigned int saved_timeout = 0;
3062 SCVAL(vwv + 0, 0, 0xff);
3063 SCVAL(vwv + 0, 1, 0);
3064 SSVAL(vwv + 1, 0, 0);
3065 SSVAL(vwv + 2, 0, fnum);
3066 SCVAL(vwv + 3, 0, locktype);
3067 SCVAL(vwv + 3, 1, 0);
3068 SIVALS(vwv + 4, 0, timeout);
3069 SSVAL(vwv + 6, 0, 0);
3070 SSVAL(vwv + 7, 0, 1);
3072 SSVAL(bytes, 0, cli_getpid(cli));
3073 SIVAL(bytes, 2, offset);
3074 SIVAL(bytes, 6, len);
3077 if (timeout == -1) {
3078 set_timeout = 0x7FFFFFFF;
3080 set_timeout = timeout + 2*1000;
3082 saved_timeout = cli_set_timeout(cli, set_timeout);
3085 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3086 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3088 if (saved_timeout != 0) {
3089 cli_set_timeout(cli, saved_timeout);
3095 /****************************************************************************
3097 note that timeout is in units of 2 milliseconds
3098 ****************************************************************************/
3100 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3101 uint32_t offset, uint32_t len, int timeout,
3102 enum brl_type lock_type)
3106 status = cli_locktype(cli, fnum, offset, len, timeout,
3107 (lock_type == READ_LOCK? 1 : 0));
3111 /****************************************************************************
3113 ****************************************************************************/
3115 struct cli_unlock_state {
3120 static void cli_unlock_done(struct tevent_req *subreq);
3122 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3123 struct tevent_context *ev,
3124 struct cli_state *cli,
3130 struct tevent_req *req = NULL, *subreq = NULL;
3131 struct cli_unlock_state *state = NULL;
3132 uint8_t additional_flags = 0;
3134 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3139 SCVAL(state->vwv+0, 0, 0xFF);
3140 SSVAL(state->vwv+2, 0, fnum);
3141 SCVAL(state->vwv+3, 0, 0);
3142 SIVALS(state->vwv+4, 0, 0);
3143 SSVAL(state->vwv+6, 0, 1);
3144 SSVAL(state->vwv+7, 0, 0);
3146 SSVAL(state->data, 0, cli_getpid(cli));
3147 SIVAL(state->data, 2, offset);
3148 SIVAL(state->data, 6, len);
3150 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3151 8, state->vwv, 10, state->data);
3152 if (tevent_req_nomem(subreq, req)) {
3153 return tevent_req_post(req, ev);
3155 tevent_req_set_callback(subreq, cli_unlock_done, req);
3159 static void cli_unlock_done(struct tevent_req *subreq)
3161 struct tevent_req *req = tevent_req_callback_data(
3162 subreq, struct tevent_req);
3165 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3166 TALLOC_FREE(subreq);
3167 if (tevent_req_nterror(req, status)) {
3170 tevent_req_done(req);
3173 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3175 return tevent_req_simple_recv_ntstatus(req);
3178 NTSTATUS cli_unlock(struct cli_state *cli,
3183 TALLOC_CTX *frame = talloc_stackframe();
3184 struct tevent_context *ev;
3185 struct tevent_req *req;
3186 NTSTATUS status = NT_STATUS_OK;
3188 if (smbXcli_conn_has_async_calls(cli->conn)) {
3190 * Can't use sync call while an async call is in flight
3192 status = NT_STATUS_INVALID_PARAMETER;
3196 ev = samba_tevent_context_init(frame);
3198 status = NT_STATUS_NO_MEMORY;
3202 req = cli_unlock_send(frame, ev, cli,
3205 status = NT_STATUS_NO_MEMORY;
3209 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3213 status = cli_unlock_recv(req);
3220 /****************************************************************************
3221 Lock a file with 64 bit offsets.
3222 ****************************************************************************/
3224 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3225 uint64_t offset, uint64_t len, int timeout,
3226 enum brl_type lock_type)
3230 unsigned int set_timeout = 0;
3231 unsigned int saved_timeout = 0;
3235 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3236 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3239 ltype = (lock_type == READ_LOCK? 1 : 0);
3240 ltype |= LOCKING_ANDX_LARGE_FILES;
3242 SCVAL(vwv + 0, 0, 0xff);
3243 SCVAL(vwv + 0, 1, 0);
3244 SSVAL(vwv + 1, 0, 0);
3245 SSVAL(vwv + 2, 0, fnum);
3246 SCVAL(vwv + 3, 0, ltype);
3247 SCVAL(vwv + 3, 1, 0);
3248 SIVALS(vwv + 4, 0, timeout);
3249 SSVAL(vwv + 6, 0, 0);
3250 SSVAL(vwv + 7, 0, 1);
3252 SIVAL(bytes, 0, cli_getpid(cli));
3253 SOFF_T_R(bytes, 4, offset);
3254 SOFF_T_R(bytes, 12, len);
3257 if (timeout == -1) {
3258 set_timeout = 0x7FFFFFFF;
3260 set_timeout = timeout + 2*1000;
3262 saved_timeout = cli_set_timeout(cli, set_timeout);
3265 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3266 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3268 if (saved_timeout != 0) {
3269 cli_set_timeout(cli, saved_timeout);
3275 /****************************************************************************
3276 Unlock a file with 64 bit offsets.
3277 ****************************************************************************/
3279 struct cli_unlock64_state {
3284 static void cli_unlock64_done(struct tevent_req *subreq);
3286 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3287 struct tevent_context *ev,
3288 struct cli_state *cli,
3294 struct tevent_req *req = NULL, *subreq = NULL;
3295 struct cli_unlock64_state *state = NULL;
3296 uint8_t additional_flags = 0;
3298 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3303 SCVAL(state->vwv+0, 0, 0xff);
3304 SSVAL(state->vwv+2, 0, fnum);
3305 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3306 SIVALS(state->vwv+4, 0, 0);
3307 SSVAL(state->vwv+6, 0, 1);
3308 SSVAL(state->vwv+7, 0, 0);
3310 SIVAL(state->data, 0, cli_getpid(cli));
3311 SOFF_T_R(state->data, 4, offset);
3312 SOFF_T_R(state->data, 12, len);
3314 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3315 8, state->vwv, 20, state->data);
3316 if (tevent_req_nomem(subreq, req)) {
3317 return tevent_req_post(req, ev);
3319 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3323 static void cli_unlock64_done(struct tevent_req *subreq)
3325 struct tevent_req *req = tevent_req_callback_data(
3326 subreq, struct tevent_req);
3329 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3330 TALLOC_FREE(subreq);
3331 if (tevent_req_nterror(req, status)) {
3334 tevent_req_done(req);
3337 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3339 return tevent_req_simple_recv_ntstatus(req);
3342 NTSTATUS cli_unlock64(struct cli_state *cli,
3347 TALLOC_CTX *frame = talloc_stackframe();
3348 struct tevent_context *ev;
3349 struct tevent_req *req;
3350 NTSTATUS status = NT_STATUS_OK;
3352 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3353 return cli_unlock(cli, fnum, offset, len);
3356 if (smbXcli_conn_has_async_calls(cli->conn)) {
3358 * Can't use sync call while an async call is in flight
3360 status = NT_STATUS_INVALID_PARAMETER;
3364 ev = samba_tevent_context_init(frame);
3366 status = NT_STATUS_NO_MEMORY;
3370 req = cli_unlock64_send(frame, ev, cli,
3373 status = NT_STATUS_NO_MEMORY;
3377 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3381 status = cli_unlock64_recv(req);
3388 /****************************************************************************
3389 Get/unlock a POSIX lock on a file - internal function.
3390 ****************************************************************************/
3392 struct posix_lock_state {
3395 uint8_t data[POSIX_LOCK_DATA_SIZE];
3398 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3400 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3401 NULL, 0, NULL, NULL, 0, NULL);
3402 tevent_req_simple_finish_ntstatus(subreq, status);
3405 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3406 struct tevent_context *ev,
3407 struct cli_state *cli,
3412 enum brl_type lock_type)
3414 struct tevent_req *req = NULL, *subreq = NULL;
3415 struct posix_lock_state *state = NULL;
3417 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3422 /* Setup setup word. */
3423 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3425 /* Setup param array. */
3426 SSVAL(&state->param, 0, fnum);
3427 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3429 /* Setup data array. */
3430 switch (lock_type) {
3432 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3433 POSIX_LOCK_TYPE_READ);
3436 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3437 POSIX_LOCK_TYPE_WRITE);
3440 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3441 POSIX_LOCK_TYPE_UNLOCK);
3448 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3449 POSIX_LOCK_FLAG_WAIT);
3451 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3452 POSIX_LOCK_FLAG_NOWAIT);
3455 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3456 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3457 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3459 subreq = cli_trans_send(state, /* mem ctx. */
3460 ev, /* event ctx. */
3461 cli, /* cli_state. */
3462 0, /* additional_flags2 */
3463 SMBtrans2, /* cmd. */
3464 NULL, /* pipe name. */
3468 &state->setup, /* setup. */
3469 1, /* num setup uint16_t words. */
3470 0, /* max returned setup. */
3471 state->param, /* param. */
3473 2, /* max returned param. */
3474 state->data, /* data. */
3475 POSIX_LOCK_DATA_SIZE, /* num data. */
3476 0); /* max returned data. */
3478 if (tevent_req_nomem(subreq, req)) {
3479 return tevent_req_post(req, ev);
3481 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3485 /****************************************************************************
3487 ****************************************************************************/
3489 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3490 struct tevent_context *ev,
3491 struct cli_state *cli,
3496 enum brl_type lock_type)
3498 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3499 wait_lock, lock_type);
3502 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3504 return tevent_req_simple_recv_ntstatus(req);
3507 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3508 uint64_t offset, uint64_t len,
3509 bool wait_lock, enum brl_type lock_type)
3511 TALLOC_CTX *frame = talloc_stackframe();
3512 struct tevent_context *ev = NULL;
3513 struct tevent_req *req = NULL;
3514 NTSTATUS status = NT_STATUS_OK;
3516 if (smbXcli_conn_has_async_calls(cli->conn)) {
3518 * Can't use sync call while an async call is in flight
3520 status = NT_STATUS_INVALID_PARAMETER;
3524 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3525 status = NT_STATUS_INVALID_PARAMETER;
3529 ev = samba_tevent_context_init(frame);
3531 status = NT_STATUS_NO_MEMORY;
3535 req = cli_posix_lock_send(frame,
3544 status = NT_STATUS_NO_MEMORY;
3548 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3552 status = cli_posix_lock_recv(req);
3559 /****************************************************************************
3560 POSIX Unlock a file.
3561 ****************************************************************************/
3563 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3564 struct tevent_context *ev,
3565 struct cli_state *cli,
3570 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3571 false, UNLOCK_LOCK);
3574 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3576 return tevent_req_simple_recv_ntstatus(req);
3579 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3581 TALLOC_CTX *frame = talloc_stackframe();
3582 struct tevent_context *ev = NULL;
3583 struct tevent_req *req = NULL;
3584 NTSTATUS status = NT_STATUS_OK;
3586 if (smbXcli_conn_has_async_calls(cli->conn)) {
3588 * Can't use sync call while an async call is in flight
3590 status = NT_STATUS_INVALID_PARAMETER;
3594 ev = samba_tevent_context_init(frame);
3596 status = NT_STATUS_NO_MEMORY;
3600 req = cli_posix_unlock_send(frame,
3607 status = NT_STATUS_NO_MEMORY;
3611 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3615 status = cli_posix_unlock_recv(req);
3622 /****************************************************************************
3623 Do a SMBgetattrE call.
3624 ****************************************************************************/
3626 static void cli_getattrE_done(struct tevent_req *subreq);
3628 struct cli_getattrE_state {
3638 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3639 struct tevent_context *ev,
3640 struct cli_state *cli,
3643 struct tevent_req *req = NULL, *subreq = NULL;
3644 struct cli_getattrE_state *state = NULL;
3645 uint8_t additional_flags = 0;
3647 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3652 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3653 SSVAL(state->vwv+0,0,fnum);
3655 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3656 1, state->vwv, 0, NULL);
3657 if (tevent_req_nomem(subreq, req)) {
3658 return tevent_req_post(req, ev);
3660 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3664 static void cli_getattrE_done(struct tevent_req *subreq)
3666 struct tevent_req *req = tevent_req_callback_data(
3667 subreq, struct tevent_req);
3668 struct cli_getattrE_state *state = tevent_req_data(
3669 req, struct cli_getattrE_state);
3671 uint16_t *vwv = NULL;
3674 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3676 TALLOC_FREE(subreq);
3677 if (tevent_req_nterror(req, status)) {
3681 state->size = (off_t)IVAL(vwv+6,0);
3682 state->attr = SVAL(vwv+10,0);
3683 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3684 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3685 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3687 tevent_req_done(req);
3690 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3693 time_t *change_time,
3694 time_t *access_time,
3697 struct cli_getattrE_state *state = tevent_req_data(
3698 req, struct cli_getattrE_state);
3701 if (tevent_req_is_nterror(req, &status)) {
3705 *attr = state->attr;
3708 *size = state->size;
3711 *change_time = state->change_time;
3714 *access_time = state->access_time;
3717 *write_time = state->write_time;
3719 return NT_STATUS_OK;
3722 NTSTATUS cli_getattrE(struct cli_state *cli,
3726 time_t *change_time,
3727 time_t *access_time,
3730 TALLOC_CTX *frame = NULL;
3731 struct tevent_context *ev = NULL;
3732 struct tevent_req *req = NULL;
3733 NTSTATUS status = NT_STATUS_OK;
3735 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3736 return cli_smb2_getattrE(cli,
3745 frame = talloc_stackframe();
3747 if (smbXcli_conn_has_async_calls(cli->conn)) {
3749 * Can't use sync call while an async call is in flight
3751 status = NT_STATUS_INVALID_PARAMETER;
3755 ev = samba_tevent_context_init(frame);
3757 status = NT_STATUS_NO_MEMORY;
3761 req = cli_getattrE_send(frame, ev, cli, fnum);
3763 status = NT_STATUS_NO_MEMORY;
3767 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3771 status = cli_getattrE_recv(req,
3783 /****************************************************************************
3785 ****************************************************************************/
3787 static void cli_getatr_done(struct tevent_req *subreq);
3789 struct cli_getatr_state {
3796 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3797 struct tevent_context *ev,
3798 struct cli_state *cli,
3801 struct tevent_req *req = NULL, *subreq = NULL;
3802 struct cli_getatr_state *state = NULL;
3803 uint8_t additional_flags = 0;
3804 uint16_t additional_flags2 = 0;
3805 uint8_t *bytes = NULL;
3807 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3812 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3814 bytes = talloc_array(state, uint8_t, 1);
3815 if (tevent_req_nomem(bytes, req)) {
3816 return tevent_req_post(req, ev);
3819 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3820 strlen(fname)+1, NULL);
3822 if (tevent_req_nomem(bytes, req)) {
3823 return tevent_req_post(req, ev);
3826 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
3827 additional_flags2 = FLAGS2_REPARSE_PATH;
3830 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3832 0, NULL, talloc_get_size(bytes), bytes);
3833 if (tevent_req_nomem(subreq, req)) {
3834 return tevent_req_post(req, ev);
3836 tevent_req_set_callback(subreq, cli_getatr_done, req);
3840 static void cli_getatr_done(struct tevent_req *subreq)
3842 struct tevent_req *req = tevent_req_callback_data(
3843 subreq, struct tevent_req);
3844 struct cli_getatr_state *state = tevent_req_data(
3845 req, struct cli_getatr_state);
3847 uint16_t *vwv = NULL;
3850 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3852 TALLOC_FREE(subreq);
3853 if (tevent_req_nterror(req, status)) {
3857 state->attr = SVAL(vwv+0,0);
3858 state->size = (off_t)IVAL(vwv+3,0);
3859 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3861 tevent_req_done(req);
3864 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3869 struct cli_getatr_state *state = tevent_req_data(
3870 req, struct cli_getatr_state);
3873 if (tevent_req_is_nterror(req, &status)) {
3877 *attr = state->attr;
3880 *size = state->size;
3883 *write_time = state->write_time;
3885 return NT_STATUS_OK;
3888 NTSTATUS cli_getatr(struct cli_state *cli,
3894 TALLOC_CTX *frame = NULL;
3895 struct tevent_context *ev = NULL;
3896 struct tevent_req *req = NULL;
3897 NTSTATUS status = NT_STATUS_OK;
3899 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3900 return cli_smb2_getatr(cli,
3907 frame = talloc_stackframe();
3909 if (smbXcli_conn_has_async_calls(cli->conn)) {
3911 * Can't use sync call while an async call is in flight
3913 status = NT_STATUS_INVALID_PARAMETER;
3917 ev = samba_tevent_context_init(frame);
3919 status = NT_STATUS_NO_MEMORY;
3923 req = cli_getatr_send(frame, ev, cli, fname);
3925 status = NT_STATUS_NO_MEMORY;
3929 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3933 status = cli_getatr_recv(req,
3943 /****************************************************************************
3944 Do a SMBsetattrE call.
3945 ****************************************************************************/
3947 static void cli_setattrE_done(struct tevent_req *subreq);
3949 struct cli_setattrE_state {
3953 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3954 struct tevent_context *ev,
3955 struct cli_state *cli,
3961 struct tevent_req *req = NULL, *subreq = NULL;
3962 struct cli_setattrE_state *state = NULL;
3963 uint8_t additional_flags = 0;
3965 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3970 SSVAL(state->vwv+0, 0, fnum);
3971 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3972 smb1cli_conn_server_time_zone(cli->conn));
3973 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3974 smb1cli_conn_server_time_zone(cli->conn));
3975 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3976 smb1cli_conn_server_time_zone(cli->conn));
3978 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3979 7, state->vwv, 0, NULL);
3980 if (tevent_req_nomem(subreq, req)) {
3981 return tevent_req_post(req, ev);
3983 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3987 static void cli_setattrE_done(struct tevent_req *subreq)
3989 struct tevent_req *req = tevent_req_callback_data(
3990 subreq, struct tevent_req);
3993 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3994 TALLOC_FREE(subreq);
3995 if (tevent_req_nterror(req, status)) {
3998 tevent_req_done(req);
4001 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
4003 return tevent_req_simple_recv_ntstatus(req);
4006 NTSTATUS cli_setattrE(struct cli_state *cli,
4012 TALLOC_CTX *frame = NULL;
4013 struct tevent_context *ev = NULL;
4014 struct tevent_req *req = NULL;
4015 NTSTATUS status = NT_STATUS_OK;
4017 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4018 return cli_smb2_setattrE(cli,
4025 frame = talloc_stackframe();
4027 if (smbXcli_conn_has_async_calls(cli->conn)) {
4029 * Can't use sync call while an async call is in flight
4031 status = NT_STATUS_INVALID_PARAMETER;
4035 ev = samba_tevent_context_init(frame);
4037 status = NT_STATUS_NO_MEMORY;
4041 req = cli_setattrE_send(frame, ev,
4049 status = NT_STATUS_NO_MEMORY;
4053 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4057 status = cli_setattrE_recv(req);
4064 /****************************************************************************
4065 Do a SMBsetatr call.
4066 ****************************************************************************/
4068 static void cli_setatr_done(struct tevent_req *subreq);
4070 struct cli_setatr_state {
4074 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4075 struct tevent_context *ev,
4076 struct cli_state *cli,
4081 struct tevent_req *req = NULL, *subreq = NULL;
4082 struct cli_setatr_state *state = NULL;
4083 uint8_t additional_flags = 0;
4084 uint16_t additional_flags2 = 0;
4085 uint8_t *bytes = NULL;
4087 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4092 SSVAL(state->vwv+0, 0, attr);
4093 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4095 bytes = talloc_array(state, uint8_t, 1);
4096 if (tevent_req_nomem(bytes, req)) {
4097 return tevent_req_post(req, ev);
4100 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4101 strlen(fname)+1, NULL);
4102 if (tevent_req_nomem(bytes, req)) {
4103 return tevent_req_post(req, ev);
4105 bytes = talloc_realloc(state, bytes, uint8_t,
4106 talloc_get_size(bytes)+1);
4107 if (tevent_req_nomem(bytes, req)) {
4108 return tevent_req_post(req, ev);
4111 bytes[talloc_get_size(bytes)-1] = 4;
4112 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4114 if (tevent_req_nomem(bytes, req)) {
4115 return tevent_req_post(req, ev);
4118 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4119 additional_flags2 = FLAGS2_REPARSE_PATH;
4122 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
4124 8, state->vwv, talloc_get_size(bytes), bytes);
4125 if (tevent_req_nomem(subreq, req)) {
4126 return tevent_req_post(req, ev);
4128 tevent_req_set_callback(subreq, cli_setatr_done, req);
4132 static void cli_setatr_done(struct tevent_req *subreq)
4134 struct tevent_req *req = tevent_req_callback_data(
4135 subreq, struct tevent_req);
4138 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4139 TALLOC_FREE(subreq);
4140 if (tevent_req_nterror(req, status)) {
4143 tevent_req_done(req);
4146 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4148 return tevent_req_simple_recv_ntstatus(req);
4151 NTSTATUS cli_setatr(struct cli_state *cli,
4156 TALLOC_CTX *frame = NULL;
4157 struct tevent_context *ev = NULL;
4158 struct tevent_req *req = NULL;
4159 NTSTATUS status = NT_STATUS_OK;
4161 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4162 return cli_smb2_setatr(cli,
4168 frame = talloc_stackframe();
4170 if (smbXcli_conn_has_async_calls(cli->conn)) {
4172 * Can't use sync call while an async call is in flight
4174 status = NT_STATUS_INVALID_PARAMETER;
4178 ev = samba_tevent_context_init(frame);
4180 status = NT_STATUS_NO_MEMORY;
4184 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4186 status = NT_STATUS_NO_MEMORY;
4190 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4194 status = cli_setatr_recv(req);
4201 /****************************************************************************
4202 Check for existence of a dir.
4203 ****************************************************************************/
4205 static void cli_chkpath_done(struct tevent_req *subreq);
4207 struct cli_chkpath_state {
4211 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4212 struct tevent_context *ev,
4213 struct cli_state *cli,
4216 struct tevent_req *req = NULL, *subreq = NULL;
4217 struct cli_chkpath_state *state = NULL;
4218 uint8_t additional_flags = 0;
4219 uint16_t additional_flags2 = 0;
4220 uint8_t *bytes = NULL;
4222 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4227 bytes = talloc_array(state, uint8_t, 1);
4228 if (tevent_req_nomem(bytes, req)) {
4229 return tevent_req_post(req, ev);
4232 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4233 strlen(fname)+1, NULL);
4235 if (tevent_req_nomem(bytes, req)) {
4236 return tevent_req_post(req, ev);
4239 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4240 additional_flags2 = FLAGS2_REPARSE_PATH;
4243 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4245 0, NULL, talloc_get_size(bytes), bytes);
4246 if (tevent_req_nomem(subreq, req)) {
4247 return tevent_req_post(req, ev);
4249 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4253 static void cli_chkpath_done(struct tevent_req *subreq)
4255 struct tevent_req *req = tevent_req_callback_data(
4256 subreq, struct tevent_req);
4259 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4260 TALLOC_FREE(subreq);
4261 if (tevent_req_nterror(req, status)) {
4264 tevent_req_done(req);
4267 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4269 return tevent_req_simple_recv_ntstatus(req);
4272 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4274 TALLOC_CTX *frame = NULL;
4275 struct tevent_context *ev = NULL;
4276 struct tevent_req *req = NULL;
4278 NTSTATUS status = NT_STATUS_OK;
4280 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4281 return cli_smb2_chkpath(cli, path);
4284 frame = talloc_stackframe();
4286 if (smbXcli_conn_has_async_calls(cli->conn)) {
4288 * Can't use sync call while an async call is in flight
4290 status = NT_STATUS_INVALID_PARAMETER;
4294 path2 = talloc_strdup(frame, path);
4296 status = NT_STATUS_NO_MEMORY;
4299 trim_char(path2,'\0','\\');
4301 path2 = talloc_strdup(frame, "\\");
4303 status = NT_STATUS_NO_MEMORY;
4308 ev = samba_tevent_context_init(frame);
4310 status = NT_STATUS_NO_MEMORY;
4314 req = cli_chkpath_send(frame, ev, cli, path2);
4316 status = NT_STATUS_NO_MEMORY;
4320 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4324 status = cli_chkpath_recv(req);
4331 /****************************************************************************
4333 ****************************************************************************/
4335 static void cli_dskattr_done(struct tevent_req *subreq);
4337 struct cli_dskattr_state {
4343 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4344 struct tevent_context *ev,
4345 struct cli_state *cli)
4347 struct tevent_req *req = NULL, *subreq = NULL;
4348 struct cli_dskattr_state *state = NULL;
4349 uint8_t additional_flags = 0;
4351 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4356 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4358 if (tevent_req_nomem(subreq, req)) {
4359 return tevent_req_post(req, ev);
4361 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4365 static void cli_dskattr_done(struct tevent_req *subreq)
4367 struct tevent_req *req = tevent_req_callback_data(
4368 subreq, struct tevent_req);
4369 struct cli_dskattr_state *state = tevent_req_data(
4370 req, struct cli_dskattr_state);
4372 uint16_t *vwv = NULL;
4375 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4377 TALLOC_FREE(subreq);
4378 if (tevent_req_nterror(req, status)) {
4381 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4382 state->total = SVAL(vwv+0, 0);
4383 state->avail = SVAL(vwv+3, 0);
4384 tevent_req_done(req);
4387 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4389 struct cli_dskattr_state *state = tevent_req_data(
4390 req, struct cli_dskattr_state);
4393 if (tevent_req_is_nterror(req, &status)) {
4396 *bsize = state->bsize;
4397 *total = state->total;
4398 *avail = state->avail;
4399 return NT_STATUS_OK;
4402 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4404 TALLOC_CTX *frame = NULL;
4405 struct tevent_context *ev = NULL;
4406 struct tevent_req *req = NULL;
4407 NTSTATUS status = NT_STATUS_OK;
4409 frame = talloc_stackframe();
4411 if (smbXcli_conn_has_async_calls(cli->conn)) {
4413 * Can't use sync call while an async call is in flight
4415 status = NT_STATUS_INVALID_PARAMETER;
4419 ev = samba_tevent_context_init(frame);
4421 status = NT_STATUS_NO_MEMORY;
4425 req = cli_dskattr_send(frame, ev, cli);
4427 status = NT_STATUS_NO_MEMORY;
4431 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4435 status = cli_dskattr_recv(req, bsize, total, avail);
4442 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4443 uint64_t *total, uint64_t *avail)
4445 uint64_t sectors_per_block;
4446 uint64_t bytes_per_sector;
4447 int old_bsize, old_total, old_avail;
4450 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4451 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4455 * Try the trans2 disk full size info call first.
4456 * We already use this in SMBC_fstatvfs_ctx().
4457 * Ignore 'actual_available_units' as we only
4458 * care about the quota for the caller.
4461 status = cli_get_fs_full_size_info(cli,
4468 /* Try and cope will all varients of "we don't do this call"
4469 and fall back to cli_dskattr. */
4471 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4472 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4473 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4474 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4475 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4476 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4477 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4478 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4479 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4480 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4484 if (!NT_STATUS_IS_OK(status)) {
4489 *bsize = sectors_per_block *
4493 return NT_STATUS_OK;
4497 /* Old SMB1 core protocol fallback. */
4498 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4499 if (!NT_STATUS_IS_OK(status)) {
4503 *bsize = (uint64_t)old_bsize;
4506 *total = (uint64_t)old_total;
4509 *avail = (uint64_t)old_avail;
4511 return NT_STATUS_OK;
4514 /****************************************************************************
4515 Create and open a temporary file.
4516 ****************************************************************************/
4518 static void cli_ctemp_done(struct tevent_req *subreq);
4520 struct ctemp_state {
4526 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4527 struct tevent_context *ev,
4528 struct cli_state *cli,
4531 struct tevent_req *req = NULL, *subreq = NULL;
4532 struct ctemp_state *state = NULL;
4533 uint8_t additional_flags = 0;
4534 uint16_t additional_flags2 = 0;
4535 uint8_t *bytes = NULL;
4537 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4542 SSVAL(state->vwv,0,0);
4543 SIVALS(state->vwv+1,0,-1);
4545 bytes = talloc_array(state, uint8_t, 1);
4546 if (tevent_req_nomem(bytes, req)) {
4547 return tevent_req_post(req, ev);
4550 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4551 strlen(path)+1, NULL);
4552 if (tevent_req_nomem(bytes, req)) {
4553 return tevent_req_post(req, ev);
4556 if (clistr_is_previous_version_path(path, NULL, NULL, NULL)) {
4557 additional_flags2 = FLAGS2_REPARSE_PATH;
4560 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4562 3, state->vwv, talloc_get_size(bytes), bytes);
4563 if (tevent_req_nomem(subreq, req)) {
4564 return tevent_req_post(req, ev);
4566 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4570 static void cli_ctemp_done(struct tevent_req *subreq)
4572 struct tevent_req *req = tevent_req_callback_data(
4573 subreq, struct tevent_req);
4574 struct ctemp_state *state = tevent_req_data(
4575 req, struct ctemp_state);
4579 uint32_t num_bytes = 0;
4580 uint8_t *bytes = NULL;
4582 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4583 &num_bytes, &bytes);
4584 TALLOC_FREE(subreq);
4585 if (tevent_req_nterror(req, status)) {
4589 state->fnum = SVAL(vwv+0, 0);
4591 /* From W2K3, the result is just the ASCII name */
4592 if (num_bytes < 2) {
4593 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4597 if (pull_string_talloc(state,
4604 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4607 tevent_req_done(req);
4610 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4615 struct ctemp_state *state = tevent_req_data(req,
4616 struct ctemp_state);
4619 if (tevent_req_is_nterror(req, &status)) {
4622 *pfnum = state->fnum;
4623 *outfile = talloc_strdup(ctx, state->ret_path);
4625 return NT_STATUS_NO_MEMORY;
4627 return NT_STATUS_OK;
4630 NTSTATUS cli_ctemp(struct cli_state *cli,
4636 TALLOC_CTX *frame = talloc_stackframe();
4637 struct tevent_context *ev;
4638 struct tevent_req *req;
4639 NTSTATUS status = NT_STATUS_OK;
4641 if (smbXcli_conn_has_async_calls(cli->conn)) {
4643 * Can't use sync call while an async call is in flight
4645 status = NT_STATUS_INVALID_PARAMETER;
4649 ev = samba_tevent_context_init(frame);
4651 status = NT_STATUS_NO_MEMORY;
4655 req = cli_ctemp_send(frame, ev, cli, path);
4657 status = NT_STATUS_NO_MEMORY;
4661 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4665 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4673 send a raw ioctl - used by the torture code
4675 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4680 SSVAL(vwv+0, 0, fnum);
4681 SSVAL(vwv+1, 0, code>>16);
4682 SSVAL(vwv+2, 0, (code&0xFFFF));
4684 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4685 NULL, 0, NULL, NULL, NULL, NULL);
4686 if (!NT_STATUS_IS_OK(status)) {
4689 *blob = data_blob_null;
4690 return NT_STATUS_OK;
4693 /*********************************************************
4694 Set an extended attribute utility fn.
4695 *********************************************************/
4697 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4698 uint8_t *param, unsigned int param_len,
4699 const char *ea_name,
4700 const char *ea_val, size_t ea_len)
4703 unsigned int data_len = 0;
4704 uint8_t *data = NULL;
4706 size_t ea_namelen = strlen(ea_name);
4709 SSVAL(setup, 0, setup_val);
4711 if (ea_namelen == 0 && ea_len == 0) {
4713 data = talloc_array(talloc_tos(),
4717 return NT_STATUS_NO_MEMORY;
4720 SIVAL(p,0,data_len);
4722 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4723 data = talloc_array(talloc_tos(),
4727 return NT_STATUS_NO_MEMORY;
4730 SIVAL(p,0,data_len);
4732 SCVAL(p, 0, 0); /* EA flags. */
4733 SCVAL(p, 1, ea_namelen);
4734 SSVAL(p, 2, ea_len);
4735 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4736 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4740 * FIXME - if we want to do previous version path
4741 * processing on an EA set call we need to turn this
4742 * into calls to cli_trans_send()/cli_trans_recv()
4743 * with a temporary event context, as cli_trans_send()
4744 * have access to the additional_flags2 needed to
4745 * send @GMT- paths. JRA.
4748 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4750 param, param_len, 2,
4753 NULL, 0, NULL, /* rsetup */
4754 NULL, 0, NULL, /* rparam */
4755 NULL, 0, NULL); /* rdata */
4760 /*********************************************************
4761 Set an extended attribute on a pathname.
4762 *********************************************************/
4764 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4765 const char *ea_name, const char *ea_val,
4768 unsigned int param_len = 0;
4771 TALLOC_CTX *frame = NULL;
4773 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4774 return cli_smb2_set_ea_path(cli,
4781 frame = talloc_stackframe();
4783 param = talloc_array(frame, uint8_t, 6);
4785 status = NT_STATUS_NO_MEMORY;
4788 SSVAL(param,0,SMB_INFO_SET_EA);
4792 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4793 path, strlen(path)+1,
4795 param_len = talloc_get_size(param);
4797 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4798 ea_name, ea_val, ea_len);
4806 /*********************************************************
4807 Set an extended attribute on an fnum.
4808 *********************************************************/
4810 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4811 const char *ea_name, const char *ea_val,
4816 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4817 return cli_smb2_set_ea_fnum(cli,
4824 memset(param, 0, 6);
4825 SSVAL(param,0,fnum);
4826 SSVAL(param,2,SMB_INFO_SET_EA);
4828 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4829 ea_name, ea_val, ea_len);
4832 /*********************************************************
4833 Get an extended attribute list utility fn.
4834 *********************************************************/
4836 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4838 size_t *pnum_eas, struct ea_struct **pea_list)
4840 struct ea_struct *ea_list = NULL;
4845 if (rdata_len < 4) {
4849 ea_size = (size_t)IVAL(rdata,0);
4850 if (ea_size > rdata_len) {
4855 /* No EA's present. */
4864 /* Validate the EA list and count it. */
4865 for (num_eas = 0; ea_size >= 4; num_eas++) {
4866 unsigned int ea_namelen = CVAL(p,1);
4867 unsigned int ea_valuelen = SVAL(p,2);
4868 if (ea_namelen == 0) {
4871 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4874 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4875 p += 4 + ea_namelen + 1 + ea_valuelen;
4884 *pnum_eas = num_eas;
4886 /* Caller only wants number of EA's. */
4890 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4895 ea_size = (size_t)IVAL(rdata,0);
4898 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4899 struct ea_struct *ea = &ea_list[num_eas];
4900 fstring unix_ea_name;
4901 unsigned int ea_namelen = CVAL(p,1);
4902 unsigned int ea_valuelen = SVAL(p,2);
4904 ea->flags = CVAL(p,0);
4905 unix_ea_name[0] = '\0';
4906 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4907 ea->name = talloc_strdup(ea_list, unix_ea_name);
4911 /* Ensure the value is null terminated (in case it's a string). */
4912 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4913 if (!ea->value.data) {
4917 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4919 ea->value.data[ea_valuelen] = 0;
4921 p += 4 + ea_namelen + 1 + ea_valuelen;
4924 *pea_list = ea_list;
4928 TALLOC_FREE(ea_list);
4932 /*********************************************************
4933 Get an extended attribute list from a pathname.
4934 *********************************************************/
4936 struct cli_get_ea_list_path_state {
4941 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4943 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4944 struct tevent_context *ev,
4945 struct cli_state *cli,
4948 struct tevent_req *req, *subreq;
4949 struct cli_get_ea_list_path_state *state;
4951 req = tevent_req_create(mem_ctx, &state,
4952 struct cli_get_ea_list_path_state);
4956 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4957 SMB_INFO_QUERY_ALL_EAS, 4,
4959 if (tevent_req_nomem(subreq, req)) {
4960 return tevent_req_post(req, ev);
4962 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4966 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4968 struct tevent_req *req = tevent_req_callback_data(
4969 subreq, struct tevent_req);
4970 struct cli_get_ea_list_path_state *state = tevent_req_data(
4971 req, struct cli_get_ea_list_path_state);
4974 status = cli_qpathinfo_recv(subreq, state, &state->data,
4976 TALLOC_FREE(subreq);
4977 if (tevent_req_nterror(req, status)) {
4980 tevent_req_done(req);
4983 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4984 size_t *pnum_eas, struct ea_struct **peas)
4986 struct cli_get_ea_list_path_state *state = tevent_req_data(
4987 req, struct cli_get_ea_list_path_state);
4990 if (tevent_req_is_nterror(req, &status)) {
4993 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4995 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4997 return NT_STATUS_OK;
5000 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
5003 struct ea_struct **pea_list)
5005 TALLOC_CTX *frame = NULL;
5006 struct tevent_context *ev = NULL;
5007 struct tevent_req *req = NULL;
5008 NTSTATUS status = NT_STATUS_NO_MEMORY;
5010 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5011 return cli_smb2_get_ea_list_path(cli,
5018 frame = talloc_stackframe();
5020 if (smbXcli_conn_has_async_calls(cli->conn)) {
5022 * Can't use sync call while an async call is in flight
5024 status = NT_STATUS_INVALID_PARAMETER;
5027 ev = samba_tevent_context_init(frame);
5031 req = cli_get_ea_list_path_send(frame, ev, cli, path);
5035 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5038 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
5044 /****************************************************************************
5045 Convert open "flags" arg to uint32_t on wire.
5046 ****************************************************************************/
5048 static uint32_t open_flags_to_wire(int flags)
5050 int open_mode = flags & O_ACCMODE;
5053 switch (open_mode) {
5055 ret |= SMB_O_WRONLY;
5062 ret |= SMB_O_RDONLY;
5066 if (flags & O_CREAT) {
5069 if (flags & O_EXCL) {
5072 if (flags & O_TRUNC) {
5076 if (flags & O_SYNC) {
5080 if (flags & O_APPEND) {
5081 ret |= SMB_O_APPEND;
5083 #if defined(O_DIRECT)
5084 if (flags & O_DIRECT) {
5085 ret |= SMB_O_DIRECT;
5088 #if defined(O_DIRECTORY)
5089 if (flags & O_DIRECTORY) {
5090 ret |= SMB_O_DIRECTORY;
5096 /****************************************************************************
5097 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5098 ****************************************************************************/
5100 struct posix_open_state {
5104 uint16_t fnum; /* Out */
5107 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5109 struct tevent_req *req = tevent_req_callback_data(
5110 subreq, struct tevent_req);
5111 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5116 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5117 NULL, 0, NULL, &data, 12, &num_data);
5118 TALLOC_FREE(subreq);
5119 if (tevent_req_nterror(req, status)) {
5122 state->fnum = SVAL(data,2);
5123 tevent_req_done(req);
5126 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5127 struct tevent_context *ev,
5128 struct cli_state *cli,
5134 struct tevent_req *req = NULL, *subreq = NULL;
5135 struct posix_open_state *state = NULL;
5136 uint32_t wire_flags = open_flags_to_wire(flags);
5138 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5143 /* Setup setup word. */
5144 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5146 /* Setup param array. */
5147 state->param = talloc_array(state, uint8_t, 6);
5148 if (tevent_req_nomem(state->param, req)) {
5149 return tevent_req_post(req, ev);
5151 memset(state->param, '\0', 6);
5152 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5154 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5155 strlen(fname)+1, NULL);
5157 if (tevent_req_nomem(state->param, req)) {
5158 return tevent_req_post(req, ev);
5161 /* Setup data words. */
5163 wire_flags |= SMB_O_DIRECTORY;
5166 SIVAL(state->data,0,0); /* No oplock. */
5167 SIVAL(state->data,4,wire_flags);
5168 SIVAL(state->data,8,unix_perms_to_wire(mode));
5169 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5170 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5172 subreq = cli_trans_send(state, /* mem ctx. */
5173 ev, /* event ctx. */
5174 cli, /* cli_state. */
5175 0, /* additional_flags2 */
5176 SMBtrans2, /* cmd. */
5177 NULL, /* pipe name. */
5181 &state->setup, /* setup. */
5182 1, /* num setup uint16_t words. */
5183 0, /* max returned setup. */
5184 state->param, /* param. */
5185 talloc_get_size(state->param),/* num param. */
5186 2, /* max returned param. */
5187 state->data, /* data. */
5189 12); /* max returned data. */
5191 if (tevent_req_nomem(subreq, req)) {
5192 return tevent_req_post(req, ev);
5194 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5198 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5199 struct tevent_context *ev,
5200 struct cli_state *cli,
5205 return cli_posix_open_internal_send(mem_ctx, ev,
5206 cli, fname, flags, mode, false);
5209 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5211 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5214 if (tevent_req_is_nterror(req, &status)) {
5217 *pfnum = state->fnum;
5218 return NT_STATUS_OK;
5221 /****************************************************************************
5222 Open - POSIX semantics. Doesn't request oplock.
5223 ****************************************************************************/
5225 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5226 int flags, mode_t mode, uint16_t *pfnum)
5229 TALLOC_CTX *frame = talloc_stackframe();
5230 struct tevent_context *ev = NULL;
5231 struct tevent_req *req = NULL;
5232 NTSTATUS status = NT_STATUS_OK;
5234 if (smbXcli_conn_has_async_calls(cli->conn)) {
5236 * Can't use sync call while an async call is in flight
5238 status = NT_STATUS_INVALID_PARAMETER;
5242 ev = samba_tevent_context_init(frame);
5244 status = NT_STATUS_NO_MEMORY;
5248 req = cli_posix_open_send(frame,
5255 status = NT_STATUS_NO_MEMORY;
5259 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5263 status = cli_posix_open_recv(req, pfnum);
5270 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5271 struct tevent_context *ev,
5272 struct cli_state *cli,
5276 return cli_posix_open_internal_send(mem_ctx, ev,
5277 cli, fname, O_CREAT, mode, true);
5280 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5282 return tevent_req_simple_recv_ntstatus(req);
5285 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5287 TALLOC_CTX *frame = talloc_stackframe();
5288 struct tevent_context *ev = NULL;
5289 struct tevent_req *req = NULL;
5290 NTSTATUS status = NT_STATUS_OK;
5292 if (smbXcli_conn_has_async_calls(cli->conn)) {
5294 * Can't use sync call while an async call is in flight
5296 status = NT_STATUS_INVALID_PARAMETER;
5300 ev = samba_tevent_context_init(frame);
5302 status = NT_STATUS_NO_MEMORY;
5306 req = cli_posix_mkdir_send(frame,
5312 status = NT_STATUS_NO_MEMORY;
5316 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5320 status = cli_posix_mkdir_recv(req);
5327 /****************************************************************************
5328 unlink or rmdir - POSIX semantics.
5329 ****************************************************************************/
5331 struct cli_posix_unlink_internal_state {
5335 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5337 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5338 struct tevent_context *ev,
5339 struct cli_state *cli,
5343 struct tevent_req *req = NULL, *subreq = NULL;
5344 struct cli_posix_unlink_internal_state *state = NULL;
5346 req = tevent_req_create(mem_ctx, &state,
5347 struct cli_posix_unlink_internal_state);
5352 /* Setup data word. */
5353 SSVAL(state->data, 0, level);
5355 subreq = cli_setpathinfo_send(state, ev, cli,
5356 SMB_POSIX_PATH_UNLINK,
5358 state->data, sizeof(state->data));
5359 if (tevent_req_nomem(subreq, req)) {
5360 return tevent_req_post(req, ev);
5362 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5366 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5368 NTSTATUS status = cli_setpathinfo_recv(subreq);
5369 tevent_req_simple_finish_ntstatus(subreq, status);
5372 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5373 struct tevent_context *ev,
5374 struct cli_state *cli,
5377 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5378 SMB_POSIX_UNLINK_FILE_TARGET);
5381 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5383 return tevent_req_simple_recv_ntstatus(req);
5386 /****************************************************************************
5387 unlink - POSIX semantics.
5388 ****************************************************************************/
5390 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5392 TALLOC_CTX *frame = talloc_stackframe();
5393 struct tevent_context *ev = NULL;
5394 struct tevent_req *req = NULL;
5395 NTSTATUS status = NT_STATUS_OK;
5397 if (smbXcli_conn_has_async_calls(cli->conn)) {
5399 * Can't use sync call while an async call is in flight
5401 status = NT_STATUS_INVALID_PARAMETER;
5405 ev = samba_tevent_context_init(frame);
5407 status = NT_STATUS_NO_MEMORY;
5411 req = cli_posix_unlink_send(frame,
5416 status = NT_STATUS_NO_MEMORY;
5420 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5424 status = cli_posix_unlink_recv(req);
5431 /****************************************************************************
5432 rmdir - POSIX semantics.
5433 ****************************************************************************/
5435 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5436 struct tevent_context *ev,
5437 struct cli_state *cli,
5440 return cli_posix_unlink_internal_send(
5441 mem_ctx, ev, cli, fname,
5442 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5445 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5447 return tevent_req_simple_recv_ntstatus(req);
5450 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5452 TALLOC_CTX *frame = talloc_stackframe();
5453 struct tevent_context *ev = NULL;
5454 struct tevent_req *req = NULL;
5455 NTSTATUS status = NT_STATUS_OK;
5457 if (smbXcli_conn_has_async_calls(cli->conn)) {
5459 * Can't use sync call while an async call is in flight
5461 status = NT_STATUS_INVALID_PARAMETER;
5465 ev = samba_tevent_context_init(frame);
5467 status = NT_STATUS_NO_MEMORY;
5471 req = cli_posix_rmdir_send(frame,
5476 status = NT_STATUS_NO_MEMORY;
5480 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5484 status = cli_posix_rmdir_recv(req, frame);
5491 /****************************************************************************
5493 ****************************************************************************/
5495 struct cli_notify_state {
5496 struct tevent_req *subreq;
5498 uint32_t num_changes;
5499 struct notify_change *changes;
5502 static void cli_notify_done(struct tevent_req *subreq);
5503 static void cli_notify_done_smb2(struct tevent_req *subreq);
5504 static bool cli_notify_cancel(struct tevent_req *req);
5506 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5507 struct tevent_context *ev,
5508 struct cli_state *cli, uint16_t fnum,
5509 uint32_t buffer_size,
5510 uint32_t completion_filter, bool recursive)
5512 struct tevent_req *req;
5513 struct cli_notify_state *state;
5514 unsigned old_timeout;
5516 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5521 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5523 * Notifies should not time out
5525 old_timeout = cli_set_timeout(cli, 0);
5527 state->subreq = cli_smb2_notify_send(
5536 cli_set_timeout(cli, old_timeout);
5538 if (tevent_req_nomem(state->subreq, req)) {
5539 return tevent_req_post(req, ev);
5541 tevent_req_set_callback(
5542 state->subreq, cli_notify_done_smb2, req);
5546 SIVAL(state->setup, 0, completion_filter);
5547 SSVAL(state->setup, 4, fnum);
5548 SSVAL(state->setup, 6, recursive);
5551 * Notifies should not time out
5553 old_timeout = cli_set_timeout(cli, 0);
5555 state->subreq = cli_trans_send(
5556 state, /* mem ctx. */
5557 ev, /* event ctx. */
5558 cli, /* cli_state. */
5559 0, /* additional_flags2 */
5560 SMBnttrans, /* cmd. */
5561 NULL, /* pipe name. */
5563 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5565 (uint16_t *)state->setup, /* setup. */
5566 4, /* num setup uint16_t words. */
5567 0, /* max returned setup. */
5570 buffer_size, /* max returned param. */
5573 0); /* max returned data. */
5575 cli_set_timeout(cli, old_timeout);
5577 if (tevent_req_nomem(state->subreq, req)) {
5578 return tevent_req_post(req, ev);
5580 tevent_req_set_callback(state->subreq, cli_notify_done, req);
5582 tevent_req_set_cancel_fn(req, cli_notify_cancel);
5586 static bool cli_notify_cancel(struct tevent_req *req)
5588 struct cli_notify_state *state = tevent_req_data(
5589 req, struct cli_notify_state);
5592 ok = tevent_req_cancel(state->subreq);
5596 static void cli_notify_done(struct tevent_req *subreq)
5598 struct tevent_req *req = tevent_req_callback_data(
5599 subreq, struct tevent_req);
5600 struct cli_notify_state *state = tevent_req_data(
5601 req, struct cli_notify_state);
5604 uint32_t i, ofs, num_params;
5607 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5608 ¶ms, 0, &num_params, NULL, 0, NULL);
5609 TALLOC_FREE(subreq);
5610 state->subreq = NULL;
5611 if (tevent_req_nterror(req, status)) {
5612 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5616 state->num_changes = 0;
5619 while (num_params - ofs > 12) {
5620 uint32_t next = IVAL(params, ofs);
5621 state->num_changes += 1;
5623 if ((next == 0) || (ofs+next >= num_params)) {
5629 state->changes = talloc_array(state, struct notify_change,
5630 state->num_changes);
5631 if (tevent_req_nomem(state->changes, req)) {
5632 TALLOC_FREE(params);
5638 for (i=0; i<state->num_changes; i++) {
5639 uint32_t next = IVAL(params, ofs);
5640 uint32_t len = IVAL(params, ofs+8);
5644 if (trans_oob(num_params, ofs + 12, len)) {
5645 TALLOC_FREE(params);
5647 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5651 state->changes[i].action = IVAL(params, ofs+4);
5652 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5653 &name, params+ofs+12, len,
5654 STR_TERMINATE|STR_UNICODE);
5656 TALLOC_FREE(params);
5657 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5660 state->changes[i].name = name;
5664 TALLOC_FREE(params);
5665 tevent_req_done(req);
5668 static void cli_notify_done_smb2(struct tevent_req *subreq)
5670 struct tevent_req *req = tevent_req_callback_data(
5671 subreq, struct tevent_req);
5672 struct cli_notify_state *state = tevent_req_data(
5673 req, struct cli_notify_state);
5676 status = cli_smb2_notify_recv(
5680 &state->num_changes);
5681 TALLOC_FREE(subreq);
5682 if (tevent_req_nterror(req, status)) {
5685 tevent_req_done(req);
5688 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5689 uint32_t *pnum_changes,
5690 struct notify_change **pchanges)
5692 struct cli_notify_state *state = tevent_req_data(
5693 req, struct cli_notify_state);
5696 if (tevent_req_is_nterror(req, &status)) {
5700 *pnum_changes = state->num_changes;
5701 *pchanges = talloc_move(mem_ctx, &state->changes);
5702 return NT_STATUS_OK;
5705 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5706 uint32_t completion_filter, bool recursive,
5707 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5708 struct notify_change **pchanges)
5711 struct tevent_context *ev;
5712 struct tevent_req *req;
5713 NTSTATUS status = NT_STATUS_NO_MEMORY;
5715 frame = talloc_stackframe();
5717 if (smbXcli_conn_has_async_calls(cli->conn)) {
5719 * Can't use sync call while an async call is in flight
5721 status = NT_STATUS_INVALID_PARAMETER;
5724 ev = samba_tevent_context_init(frame);
5728 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5729 completion_filter, recursive);
5733 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5736 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5742 struct cli_qpathinfo_state {
5751 static void cli_qpathinfo_done(struct tevent_req *subreq);
5753 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5754 struct tevent_context *ev,
5755 struct cli_state *cli, const char *fname,
5756 uint16_t level, uint32_t min_rdata,
5759 struct tevent_req *req, *subreq;
5760 struct cli_qpathinfo_state *state;
5761 uint16_t additional_flags2 = 0;
5763 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5767 state->min_rdata = min_rdata;
5768 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5770 state->param = talloc_zero_array(state, uint8_t, 6);
5771 if (tevent_req_nomem(state->param, req)) {
5772 return tevent_req_post(req, ev);
5774 SSVAL(state->param, 0, level);
5775 state->param = trans2_bytes_push_str(
5776 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5777 if (tevent_req_nomem(state->param, req)) {
5778 return tevent_req_post(req, ev);
5781 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL) &&
5782 !INFO_LEVEL_IS_UNIX(level)) {
5783 additional_flags2 = FLAGS2_REPARSE_PATH;
5786 subreq = cli_trans_send(
5787 state, /* mem ctx. */
5788 ev, /* event ctx. */
5789 cli, /* cli_state. */
5790 additional_flags2, /* additional_flags2 */
5791 SMBtrans2, /* cmd. */
5792 NULL, /* pipe name. */
5796 state->setup, /* setup. */
5797 1, /* num setup uint16_t words. */
5798 0, /* max returned setup. */
5799 state->param, /* param. */
5800 talloc_get_size(state->param), /* num param. */
5801 2, /* max returned param. */
5804 max_rdata); /* max returned data. */
5806 if (tevent_req_nomem(subreq, req)) {
5807 return tevent_req_post(req, ev);
5809 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5813 static void cli_qpathinfo_done(struct tevent_req *subreq)
5815 struct tevent_req *req = tevent_req_callback_data(
5816 subreq, struct tevent_req);
5817 struct cli_qpathinfo_state *state = tevent_req_data(
5818 req, struct cli_qpathinfo_state);
5821 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5823 &state->rdata, state->min_rdata,
5825 if (tevent_req_nterror(req, status)) {
5828 tevent_req_done(req);
5831 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5832 uint8_t **rdata, uint32_t *num_rdata)
5834 struct cli_qpathinfo_state *state = tevent_req_data(
5835 req, struct cli_qpathinfo_state);
5838 if (tevent_req_is_nterror(req, &status)) {
5841 if (rdata != NULL) {
5842 *rdata = talloc_move(mem_ctx, &state->rdata);
5844 TALLOC_FREE(state->rdata);
5846 if (num_rdata != NULL) {
5847 *num_rdata = state->num_rdata;
5849 return NT_STATUS_OK;
5852 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5853 const char *fname, uint16_t level, uint32_t min_rdata,
5855 uint8_t **rdata, uint32_t *num_rdata)
5857 TALLOC_CTX *frame = talloc_stackframe();
5858 struct tevent_context *ev;
5859 struct tevent_req *req;
5860 NTSTATUS status = NT_STATUS_NO_MEMORY;
5862 if (smbXcli_conn_has_async_calls(cli->conn)) {
5864 * Can't use sync call while an async call is in flight
5866 status = NT_STATUS_INVALID_PARAMETER;
5869 ev = samba_tevent_context_init(frame);
5873 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5878 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5881 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5887 struct cli_qfileinfo_state {
5891 uint16_t recv_flags2;
5897 static void cli_qfileinfo_done(struct tevent_req *subreq);
5899 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5900 struct tevent_context *ev,
5901 struct cli_state *cli, uint16_t fnum,
5902 uint16_t level, uint32_t min_rdata,
5905 struct tevent_req *req, *subreq;
5906 struct cli_qfileinfo_state *state;
5908 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5912 state->min_rdata = min_rdata;
5913 SSVAL(state->param, 0, fnum);
5914 SSVAL(state->param, 2, level);
5915 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5917 subreq = cli_trans_send(
5918 state, /* mem ctx. */
5919 ev, /* event ctx. */
5920 cli, /* cli_state. */
5921 0, /* additional_flags2 */
5922 SMBtrans2, /* cmd. */
5923 NULL, /* pipe name. */
5927 state->setup, /* setup. */
5928 1, /* num setup uint16_t words. */
5929 0, /* max returned setup. */
5930 state->param, /* param. */
5931 sizeof(state->param), /* num param. */
5932 2, /* max returned param. */
5935 max_rdata); /* max returned data. */
5937 if (tevent_req_nomem(subreq, req)) {
5938 return tevent_req_post(req, ev);
5940 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5944 static void cli_qfileinfo_done(struct tevent_req *subreq)
5946 struct tevent_req *req = tevent_req_callback_data(
5947 subreq, struct tevent_req);
5948 struct cli_qfileinfo_state *state = tevent_req_data(
5949 req, struct cli_qfileinfo_state);
5952 status = cli_trans_recv(subreq, state,
5953 &state->recv_flags2,
5956 &state->rdata, state->min_rdata,
5958 if (tevent_req_nterror(req, status)) {
5961 tevent_req_done(req);
5964 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5965 uint16_t *recv_flags2,
5966 uint8_t **rdata, uint32_t *num_rdata)
5968 struct cli_qfileinfo_state *state = tevent_req_data(
5969 req, struct cli_qfileinfo_state);
5972 if (tevent_req_is_nterror(req, &status)) {
5976 if (recv_flags2 != NULL) {
5977 *recv_flags2 = state->recv_flags2;
5979 if (rdata != NULL) {
5980 *rdata = talloc_move(mem_ctx, &state->rdata);
5982 TALLOC_FREE(state->rdata);
5984 if (num_rdata != NULL) {
5985 *num_rdata = state->num_rdata;
5987 return NT_STATUS_OK;
5990 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5991 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5992 uint32_t max_rdata, uint16_t *recv_flags2,
5993 uint8_t **rdata, uint32_t *num_rdata)
5995 TALLOC_CTX *frame = talloc_stackframe();
5996 struct tevent_context *ev;
5997 struct tevent_req *req;
5998 NTSTATUS status = NT_STATUS_NO_MEMORY;
6000 if (smbXcli_conn_has_async_calls(cli->conn)) {
6002 * Can't use sync call while an async call is in flight
6004 status = NT_STATUS_INVALID_PARAMETER;
6007 ev = samba_tevent_context_init(frame);
6011 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
6016 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6019 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
6025 struct cli_flush_state {
6029 static void cli_flush_done(struct tevent_req *subreq);
6031 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
6032 struct tevent_context *ev,
6033 struct cli_state *cli,
6036 struct tevent_req *req, *subreq;
6037 struct cli_flush_state *state;
6039 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
6043 SSVAL(state->vwv + 0, 0, fnum);
6045 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
6047 if (tevent_req_nomem(subreq, req)) {
6048 return tevent_req_post(req, ev);
6050 tevent_req_set_callback(subreq, cli_flush_done, req);
6054 static void cli_flush_done(struct tevent_req *subreq)
6056 struct tevent_req *req = tevent_req_callback_data(
6057 subreq, struct tevent_req);
6060 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
6061 TALLOC_FREE(subreq);
6062 if (tevent_req_nterror(req, status)) {
6065 tevent_req_done(req);
6068 NTSTATUS cli_flush_recv(struct tevent_req *req)
6070 return tevent_req_simple_recv_ntstatus(req);
6073 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
6075 TALLOC_CTX *frame = talloc_stackframe();
6076 struct tevent_context *ev;
6077 struct tevent_req *req;
6078 NTSTATUS status = NT_STATUS_NO_MEMORY;
6080 if (smbXcli_conn_has_async_calls(cli->conn)) {
6082 * Can't use sync call while an async call is in flight
6084 status = NT_STATUS_INVALID_PARAMETER;
6087 ev = samba_tevent_context_init(frame);
6091 req = cli_flush_send(frame, ev, cli, fnum);
6095 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6098 status = cli_flush_recv(req);
6104 struct cli_shadow_copy_data_state {
6111 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
6113 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
6114 struct tevent_context *ev,
6115 struct cli_state *cli,
6119 struct tevent_req *req, *subreq;
6120 struct cli_shadow_copy_data_state *state;
6123 req = tevent_req_create(mem_ctx, &state,
6124 struct cli_shadow_copy_data_state);
6128 state->get_names = get_names;
6129 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
6131 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
6132 SSVAL(state->setup + 2, 0, fnum);
6133 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
6134 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
6136 subreq = cli_trans_send(
6137 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
6138 state->setup, ARRAY_SIZE(state->setup),
6139 ARRAY_SIZE(state->setup),
6142 if (tevent_req_nomem(subreq, req)) {
6143 return tevent_req_post(req, ev);
6145 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
6149 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
6151 struct tevent_req *req = tevent_req_callback_data(
6152 subreq, struct tevent_req);
6153 struct cli_shadow_copy_data_state *state = tevent_req_data(
6154 req, struct cli_shadow_copy_data_state);
6157 status = cli_trans_recv(subreq, state, NULL,
6158 NULL, 0, NULL, /* setup */
6159 NULL, 0, NULL, /* param */
6160 &state->data, 12, &state->num_data);
6161 TALLOC_FREE(subreq);
6162 if (tevent_req_nterror(req, status)) {
6165 tevent_req_done(req);
6168 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6169 char ***pnames, int *pnum_names)
6171 struct cli_shadow_copy_data_state *state = tevent_req_data(
6172 req, struct cli_shadow_copy_data_state);
6173 char **names = NULL;
6174 uint32_t i, num_names;
6176 uint8_t *endp = NULL;
6179 if (tevent_req_is_nterror(req, &status)) {
6183 if (state->num_data < 16) {
6184 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6187 num_names = IVAL(state->data, 4);
6188 dlength = IVAL(state->data, 8);
6190 if (num_names > 0x7FFFFFFF) {
6191 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6194 if (!state->get_names) {
6195 *pnum_names = (int)num_names;
6196 return NT_STATUS_OK;
6199 if (dlength + 12 < 12) {
6200 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6202 if (dlength + 12 > state->num_data) {
6203 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6205 if (state->num_data + (2 * sizeof(SHADOW_COPY_LABEL)) <
6207 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6210 names = talloc_array(mem_ctx, char *, num_names);
6211 if (names == NULL) {
6212 return NT_STATUS_NO_MEMORY;
6215 endp = state->data + state->num_data;
6217 for (i=0; i<num_names; i++) {
6220 size_t converted_size;
6222 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6224 if (src + (2 * sizeof(SHADOW_COPY_LABEL)) > endp) {
6225 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6228 ret = convert_string_talloc(
6229 names, CH_UTF16LE, CH_UNIX,
6230 src, 2 * sizeof(SHADOW_COPY_LABEL),
6231 &names[i], &converted_size);
6234 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6237 *pnum_names = (int)num_names;
6239 return NT_STATUS_OK;
6242 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6243 uint16_t fnum, bool get_names,
6244 char ***pnames, int *pnum_names)
6246 TALLOC_CTX *frame = NULL;
6247 struct tevent_context *ev;
6248 struct tevent_req *req;
6249 NTSTATUS status = NT_STATUS_NO_MEMORY;
6251 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6252 return cli_smb2_shadow_copy_data(mem_ctx,
6260 frame = talloc_stackframe();
6262 if (smbXcli_conn_has_async_calls(cli->conn)) {
6264 * Can't use sync call while an async call is in flight
6266 status = NT_STATUS_INVALID_PARAMETER;
6269 ev = samba_tevent_context_init(frame);
6273 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6277 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6280 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);