2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 struct cli_setpathinfo_state
{
37 static void cli_setpathinfo_done(struct tevent_req
*subreq
);
39 struct tevent_req
*cli_setpathinfo_send(TALLOC_CTX
*mem_ctx
,
40 struct tevent_context
*ev
,
41 struct cli_state
*cli
,
47 struct tevent_req
*req
, *subreq
;
48 struct cli_setpathinfo_state
*state
;
49 uint16_t additional_flags2
= 0;
51 req
= tevent_req_create(mem_ctx
, &state
,
52 struct cli_setpathinfo_state
);
57 /* Setup setup word. */
58 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
60 /* Setup param array. */
61 state
->param
= talloc_zero_array(state
, uint8_t, 6);
62 if (tevent_req_nomem(state
->param
, req
)) {
63 return tevent_req_post(req
, ev
);
65 SSVAL(state
->param
, 0, level
);
67 state
->param
= trans2_bytes_push_str(
68 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), path
, strlen(path
)+1, NULL
);
69 if (tevent_req_nomem(state
->param
, req
)) {
70 return tevent_req_post(req
, ev
);
73 if (clistr_is_previous_version_path(path
, NULL
, NULL
, NULL
) &&
74 !INFO_LEVEL_IS_UNIX(level
)) {
75 additional_flags2
= FLAGS2_REPARSE_PATH
;
78 subreq
= cli_trans_send(
82 additional_flags2
, /* additional_flags2 */
84 NULL
, /* pipe name. */
88 &state
->setup
, /* setup. */
89 1, /* num setup uint16_t words. */
90 0, /* max returned setup. */
91 state
->param
, /* param. */
92 talloc_get_size(state
->param
), /* num param. */
93 2, /* max returned param. */
95 data_len
, /* num data. */
96 0); /* max returned data. */
98 if (tevent_req_nomem(subreq
, req
)) {
99 return tevent_req_post(req
, ev
);
101 tevent_req_set_callback(subreq
, cli_setpathinfo_done
, req
);
105 static void cli_setpathinfo_done(struct tevent_req
*subreq
)
107 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
108 NULL
, 0, NULL
, NULL
, 0, NULL
);
109 tevent_req_simple_finish_ntstatus(subreq
, status
);
112 NTSTATUS
cli_setpathinfo_recv(struct tevent_req
*req
)
114 return tevent_req_simple_recv_ntstatus(req
);
117 NTSTATUS
cli_setpathinfo(struct cli_state
*cli
,
123 TALLOC_CTX
*frame
= talloc_stackframe();
124 struct tevent_context
*ev
;
125 struct tevent_req
*req
;
126 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
128 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
130 * Can't use sync call while an async call is in flight
132 status
= NT_STATUS_INVALID_PARAMETER
;
135 ev
= samba_tevent_context_init(frame
);
139 req
= cli_setpathinfo_send(ev
, ev
, cli
, level
, path
, data
, data_len
);
143 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
146 status
= cli_setpathinfo_recv(req
);
152 /****************************************************************************
153 Hard/Symlink a file (UNIX extensions).
154 Creates new name (sym)linked to oldname.
155 ****************************************************************************/
157 struct cli_posix_link_internal_state
{
161 static void cli_posix_link_internal_done(struct tevent_req
*subreq
);
163 static struct tevent_req
*cli_posix_link_internal_send(TALLOC_CTX
*mem_ctx
,
164 struct tevent_context
*ev
,
165 struct cli_state
*cli
,
170 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
171 struct cli_posix_link_internal_state
*state
= NULL
;
173 req
= tevent_req_create(mem_ctx
, &state
,
174 struct cli_posix_link_internal_state
);
179 /* Setup data array. */
180 state
->data
= talloc_array(state
, uint8_t, 0);
181 if (tevent_req_nomem(state
->data
, req
)) {
182 return tevent_req_post(req
, ev
);
184 state
->data
= trans2_bytes_push_str(
185 state
->data
, smbXcli_conn_use_unicode(cli
->conn
), oldname
, strlen(oldname
)+1, NULL
);
187 subreq
= cli_setpathinfo_send(
188 state
, ev
, cli
, level
, newname
,
189 state
->data
, talloc_get_size(state
->data
));
190 if (tevent_req_nomem(subreq
, req
)) {
191 return tevent_req_post(req
, ev
);
193 tevent_req_set_callback(subreq
, cli_posix_link_internal_done
, req
);
197 static void cli_posix_link_internal_done(struct tevent_req
*subreq
)
199 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
200 tevent_req_simple_finish_ntstatus(subreq
, status
);
203 /****************************************************************************
204 Symlink a file (UNIX extensions).
205 ****************************************************************************/
207 struct tevent_req
*cli_posix_symlink_send(TALLOC_CTX
*mem_ctx
,
208 struct tevent_context
*ev
,
209 struct cli_state
*cli
,
213 return cli_posix_link_internal_send(
214 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_LINK
, oldname
, newname
);
217 NTSTATUS
cli_posix_symlink_recv(struct tevent_req
*req
)
219 return tevent_req_simple_recv_ntstatus(req
);
222 NTSTATUS
cli_posix_symlink(struct cli_state
*cli
,
226 TALLOC_CTX
*frame
= talloc_stackframe();
227 struct tevent_context
*ev
= NULL
;
228 struct tevent_req
*req
= NULL
;
229 NTSTATUS status
= NT_STATUS_OK
;
231 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
233 * Can't use sync call while an async call is in flight
235 status
= NT_STATUS_INVALID_PARAMETER
;
239 ev
= samba_tevent_context_init(frame
);
241 status
= NT_STATUS_NO_MEMORY
;
245 req
= cli_posix_symlink_send(frame
,
251 status
= NT_STATUS_NO_MEMORY
;
255 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
259 status
= cli_posix_symlink_recv(req
);
266 /****************************************************************************
267 Read a POSIX symlink.
268 ****************************************************************************/
270 struct readlink_state
{
275 static void cli_posix_readlink_done(struct tevent_req
*subreq
);
277 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
278 struct tevent_context
*ev
,
279 struct cli_state
*cli
,
283 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
284 struct readlink_state
*state
= NULL
;
285 uint32_t maxbytelen
= (uint32_t)(smbXcli_conn_use_unicode(cli
->conn
) ? len
*3 : len
);
287 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
293 * Len is in bytes, we need it in UCS2 units.
295 if ((2*len
< len
) || (maxbytelen
< len
)) {
296 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
297 return tevent_req_post(req
, ev
);
300 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
301 SMB_QUERY_FILE_UNIX_LINK
, 1, maxbytelen
);
302 if (tevent_req_nomem(subreq
, req
)) {
303 return tevent_req_post(req
, ev
);
305 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
309 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
311 struct tevent_req
*req
= tevent_req_callback_data(
312 subreq
, struct tevent_req
);
313 struct readlink_state
*state
= tevent_req_data(
314 req
, struct readlink_state
);
317 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
320 if (tevent_req_nterror(req
, status
)) {
324 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
326 if (state
->data
[state
->num_data
-1] != '\0') {
327 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
330 tevent_req_done(req
);
333 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
334 char *retpath
, size_t len
)
337 char *converted
= NULL
;
338 size_t converted_size
= 0;
339 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
341 if (tevent_req_is_nterror(req
, &status
)) {
344 /* The returned data is a pushed string, not raw data. */
345 if (!convert_string_talloc(state
,
346 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
352 return NT_STATUS_NO_MEMORY
;
355 len
= MIN(len
,converted_size
);
357 return NT_STATUS_DATA_ERROR
;
359 memcpy(retpath
, converted
, len
);
363 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
364 char *linkpath
, size_t len
)
366 TALLOC_CTX
*frame
= talloc_stackframe();
367 struct tevent_context
*ev
= NULL
;
368 struct tevent_req
*req
= NULL
;
369 NTSTATUS status
= NT_STATUS_OK
;
371 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
373 * Can't use sync call while an async call is in flight
375 status
= NT_STATUS_INVALID_PARAMETER
;
379 ev
= samba_tevent_context_init(frame
);
381 status
= NT_STATUS_NO_MEMORY
;
385 req
= cli_posix_readlink_send(frame
,
391 status
= NT_STATUS_NO_MEMORY
;
395 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
399 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
406 /****************************************************************************
407 Hard link a file (UNIX extensions).
408 ****************************************************************************/
410 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
411 struct tevent_context
*ev
,
412 struct cli_state
*cli
,
416 return cli_posix_link_internal_send(
417 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_HLINK
, oldname
, newname
);
420 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
422 return tevent_req_simple_recv_ntstatus(req
);
425 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
429 TALLOC_CTX
*frame
= talloc_stackframe();
430 struct tevent_context
*ev
= NULL
;
431 struct tevent_req
*req
= NULL
;
432 NTSTATUS status
= NT_STATUS_OK
;
434 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
436 * Can't use sync call while an async call is in flight
438 status
= NT_STATUS_INVALID_PARAMETER
;
442 ev
= samba_tevent_context_init(frame
);
444 status
= NT_STATUS_NO_MEMORY
;
448 req
= cli_posix_hardlink_send(frame
,
454 status
= NT_STATUS_NO_MEMORY
;
458 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
462 status
= cli_posix_hardlink_recv(req
);
469 /****************************************************************************
470 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
471 ****************************************************************************/
473 struct getacl_state
{
478 static void cli_posix_getacl_done(struct tevent_req
*subreq
);
480 struct tevent_req
*cli_posix_getacl_send(TALLOC_CTX
*mem_ctx
,
481 struct tevent_context
*ev
,
482 struct cli_state
*cli
,
485 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
486 struct getacl_state
*state
= NULL
;
488 req
= tevent_req_create(mem_ctx
, &state
, struct getacl_state
);
492 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_QUERY_POSIX_ACL
,
494 if (tevent_req_nomem(subreq
, req
)) {
495 return tevent_req_post(req
, ev
);
497 tevent_req_set_callback(subreq
, cli_posix_getacl_done
, req
);
501 static void cli_posix_getacl_done(struct tevent_req
*subreq
)
503 struct tevent_req
*req
= tevent_req_callback_data(
504 subreq
, struct tevent_req
);
505 struct getacl_state
*state
= tevent_req_data(
506 req
, struct getacl_state
);
509 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
512 if (tevent_req_nterror(req
, status
)) {
515 tevent_req_done(req
);
518 NTSTATUS
cli_posix_getacl_recv(struct tevent_req
*req
,
523 struct getacl_state
*state
= tevent_req_data(req
, struct getacl_state
);
526 if (tevent_req_is_nterror(req
, &status
)) {
529 *prb_size
= (size_t)state
->num_data
;
530 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
534 NTSTATUS
cli_posix_getacl(struct cli_state
*cli
,
540 TALLOC_CTX
*frame
= talloc_stackframe();
541 struct tevent_context
*ev
= NULL
;
542 struct tevent_req
*req
= NULL
;
543 NTSTATUS status
= NT_STATUS_OK
;
545 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
547 * Can't use sync call while an async call is in flight
549 status
= NT_STATUS_INVALID_PARAMETER
;
553 ev
= samba_tevent_context_init(frame
);
555 status
= NT_STATUS_NO_MEMORY
;
559 req
= cli_posix_getacl_send(frame
,
564 status
= NT_STATUS_NO_MEMORY
;
568 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
572 status
= cli_posix_getacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
579 /****************************************************************************
580 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
581 ****************************************************************************/
583 struct setacl_state
{
587 static void cli_posix_setacl_done(struct tevent_req
*subreq
);
589 struct tevent_req
*cli_posix_setacl_send(TALLOC_CTX
*mem_ctx
,
590 struct tevent_context
*ev
,
591 struct cli_state
*cli
,
596 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
597 struct setacl_state
*state
= NULL
;
599 req
= tevent_req_create(mem_ctx
, &state
, struct setacl_state
);
603 state
->data
= talloc_memdup(state
, data
, num_data
);
604 if (tevent_req_nomem(state
->data
, req
)) {
605 return tevent_req_post(req
, ev
);
608 subreq
= cli_setpathinfo_send(state
,
615 if (tevent_req_nomem(subreq
, req
)) {
616 return tevent_req_post(req
, ev
);
618 tevent_req_set_callback(subreq
, cli_posix_setacl_done
, req
);
622 static void cli_posix_setacl_done(struct tevent_req
*subreq
)
624 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
625 tevent_req_simple_finish_ntstatus(subreq
, status
);
628 NTSTATUS
cli_posix_setacl_recv(struct tevent_req
*req
)
630 return tevent_req_simple_recv_ntstatus(req
);
633 NTSTATUS
cli_posix_setacl(struct cli_state
*cli
,
638 TALLOC_CTX
*frame
= talloc_stackframe();
639 struct tevent_context
*ev
= NULL
;
640 struct tevent_req
*req
= NULL
;
641 NTSTATUS status
= NT_STATUS_OK
;
643 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
645 * Can't use sync call while an async call is in flight
647 status
= NT_STATUS_INVALID_PARAMETER
;
651 ev
= samba_tevent_context_init(frame
);
653 status
= NT_STATUS_NO_MEMORY
;
657 req
= cli_posix_setacl_send(frame
,
664 status
= NT_STATUS_NO_MEMORY
;
668 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
672 status
= cli_posix_setacl_recv(req
);
679 /****************************************************************************
680 Stat a file (UNIX extensions).
681 ****************************************************************************/
688 static void cli_posix_stat_done(struct tevent_req
*subreq
);
690 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
691 struct tevent_context
*ev
,
692 struct cli_state
*cli
,
695 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
696 struct stat_state
*state
= NULL
;
698 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
702 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
703 SMB_QUERY_FILE_UNIX_BASIC
, 100, 100);
704 if (tevent_req_nomem(subreq
, req
)) {
705 return tevent_req_post(req
, ev
);
707 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
711 static void cli_posix_stat_done(struct tevent_req
*subreq
)
713 struct tevent_req
*req
= tevent_req_callback_data(
714 subreq
, struct tevent_req
);
715 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
718 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
721 if (tevent_req_nterror(req
, status
)) {
724 tevent_req_done(req
);
727 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
728 SMB_STRUCT_STAT
*sbuf
)
730 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
733 if (tevent_req_is_nterror(req
, &status
)) {
737 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
738 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
739 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
740 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
742 /* assume 512 byte blocks */
743 sbuf
->st_ex_blocks
/= 512;
745 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
746 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
747 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
749 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
750 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
751 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
752 #if defined(HAVE_MAKEDEV)
754 uint32_t dev_major
= IVAL(state
->data
,60);
755 uint32_t dev_minor
= IVAL(state
->data
,68);
756 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
759 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
760 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
761 sbuf
->st_ex_nlink
= BIG_UINT(state
->data
,92); /* number of hard links */
766 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
768 SMB_STRUCT_STAT
*sbuf
)
770 TALLOC_CTX
*frame
= talloc_stackframe();
771 struct tevent_context
*ev
= NULL
;
772 struct tevent_req
*req
= NULL
;
773 NTSTATUS status
= NT_STATUS_OK
;
775 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
777 * Can't use sync call while an async call is in flight
779 status
= NT_STATUS_INVALID_PARAMETER
;
783 ev
= samba_tevent_context_init(frame
);
785 status
= NT_STATUS_NO_MEMORY
;
789 req
= cli_posix_stat_send(frame
,
794 status
= NT_STATUS_NO_MEMORY
;
798 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
802 status
= cli_posix_stat_recv(req
, sbuf
);
809 /****************************************************************************
810 Chmod or chown a file internal (UNIX extensions).
811 ****************************************************************************/
813 struct cli_posix_chown_chmod_internal_state
{
817 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
);
819 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
820 struct tevent_context
*ev
,
821 struct cli_state
*cli
,
827 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
828 struct cli_posix_chown_chmod_internal_state
*state
= NULL
;
830 req
= tevent_req_create(mem_ctx
, &state
,
831 struct cli_posix_chown_chmod_internal_state
);
836 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
837 memset(&state
->data
[40], '\0', 60);
838 SIVAL(state
->data
,40,uid
);
839 SIVAL(state
->data
,48,gid
);
840 SIVAL(state
->data
,84,mode
);
842 subreq
= cli_setpathinfo_send(state
, ev
, cli
, SMB_SET_FILE_UNIX_BASIC
,
843 fname
, state
->data
, sizeof(state
->data
));
844 if (tevent_req_nomem(subreq
, req
)) {
845 return tevent_req_post(req
, ev
);
847 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
,
852 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
854 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
855 tevent_req_simple_finish_ntstatus(subreq
, status
);
858 /****************************************************************************
859 chmod a file (UNIX extensions).
860 ****************************************************************************/
862 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
863 struct tevent_context
*ev
,
864 struct cli_state
*cli
,
868 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
870 unix_perms_to_wire(mode
),
875 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
877 return tevent_req_simple_recv_ntstatus(req
);
880 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
882 TALLOC_CTX
*frame
= talloc_stackframe();
883 struct tevent_context
*ev
= NULL
;
884 struct tevent_req
*req
= NULL
;
885 NTSTATUS status
= NT_STATUS_OK
;
887 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
889 * Can't use sync call while an async call is in flight
891 status
= NT_STATUS_INVALID_PARAMETER
;
895 ev
= samba_tevent_context_init(frame
);
897 status
= NT_STATUS_NO_MEMORY
;
901 req
= cli_posix_chmod_send(frame
,
907 status
= NT_STATUS_NO_MEMORY
;
911 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
915 status
= cli_posix_chmod_recv(req
);
922 /****************************************************************************
923 chown a file (UNIX extensions).
924 ****************************************************************************/
926 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
927 struct tevent_context
*ev
,
928 struct cli_state
*cli
,
933 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
940 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
942 return tevent_req_simple_recv_ntstatus(req
);
945 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
950 TALLOC_CTX
*frame
= talloc_stackframe();
951 struct tevent_context
*ev
= NULL
;
952 struct tevent_req
*req
= NULL
;
953 NTSTATUS status
= NT_STATUS_OK
;
955 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
957 * Can't use sync call while an async call is in flight
959 status
= NT_STATUS_INVALID_PARAMETER
;
963 ev
= samba_tevent_context_init(frame
);
965 status
= NT_STATUS_NO_MEMORY
;
969 req
= cli_posix_chown_send(frame
,
976 status
= NT_STATUS_NO_MEMORY
;
980 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
984 status
= cli_posix_chown_recv(req
);
991 /****************************************************************************
993 ****************************************************************************/
995 static struct tevent_req
*cli_cifs_rename_send(TALLOC_CTX
*mem_ctx
,
996 struct tevent_context
*ev
,
997 struct cli_state
*cli
,
998 const char *fname_src
,
999 const char *fname_dst
,
1002 static struct tevent_req
*cli_smb1_rename_send(TALLOC_CTX
*mem_ctx
,
1003 struct tevent_context
*ev
,
1004 struct cli_state
*cli
,
1005 const char *fname_src
,
1006 const char *fname_dst
,
1009 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1010 struct tevent_context
*ev
,
1011 struct cli_state
*cli
,
1012 const char *fname_src
,
1013 const char *fname_dst
,
1016 if (replace
&& smbXcli_conn_support_passthrough(cli
->conn
)) {
1017 return cli_smb1_rename_send(mem_ctx
, ev
, cli
, fname_src
,
1018 fname_dst
, replace
);
1020 return cli_cifs_rename_send(mem_ctx
, ev
, cli
, fname_src
,
1021 fname_dst
, replace
);
1025 struct cli_smb1_rename_state
{
1029 static void cli_smb1_rename_done(struct tevent_req
*subreq
);
1031 static struct tevent_req
*cli_smb1_rename_send(TALLOC_CTX
*mem_ctx
,
1032 struct tevent_context
*ev
,
1033 struct cli_state
*cli
,
1034 const char *fname_src
,
1035 const char *fname_dst
,
1039 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1040 struct cli_smb1_rename_state
*state
= NULL
;
1041 smb_ucs2_t
*converted_str
= NULL
;
1042 size_t converted_size_bytes
= 0;
1044 req
= tevent_req_create(mem_ctx
, &state
, struct cli_smb1_rename_state
);
1049 if (!push_ucs2_talloc(talloc_tos(), &converted_str
, fname_dst
,
1050 &converted_size_bytes
)) {
1051 status
= NT_STATUS_INVALID_PARAMETER
;
1055 /* W2K8 insists the dest name is not null
1056 terminated. Remove the last 2 zero bytes
1057 and reduce the name length. */
1059 if (converted_size_bytes
< 2) {
1060 status
= NT_STATUS_INVALID_PARAMETER
;
1063 converted_size_bytes
-= 2;
1066 talloc_zero_array(state
, uint8_t, 12 + converted_size_bytes
);
1067 if (state
->data
== NULL
) {
1068 status
= NT_STATUS_NO_MEMORY
;
1073 SCVAL(state
->data
, 0, 1);
1076 SIVAL(state
->data
, 8, converted_size_bytes
);
1077 memcpy(state
->data
+ 12, converted_str
, converted_size_bytes
);
1079 TALLOC_FREE(converted_str
);
1081 subreq
= cli_setpathinfo_send(
1082 state
, ev
, cli
, SMB_FILE_RENAME_INFORMATION
, fname_src
, state
->data
,
1083 talloc_get_size(state
->data
));
1084 if (tevent_req_nomem(subreq
, req
)) {
1085 status
= NT_STATUS_NO_MEMORY
;
1088 tevent_req_set_callback(subreq
, cli_smb1_rename_done
, req
);
1092 TALLOC_FREE(converted_str
);
1093 tevent_req_nterror(req
, status
);
1094 return tevent_req_post(req
, ev
);
1097 static void cli_smb1_rename_done(struct tevent_req
*subreq
)
1099 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
1100 tevent_req_simple_finish_ntstatus(subreq
, status
);
1103 static void cli_cifs_rename_done(struct tevent_req
*subreq
);
1105 struct cli_cifs_rename_state
{
1109 static struct tevent_req
*cli_cifs_rename_send(TALLOC_CTX
*mem_ctx
,
1110 struct tevent_context
*ev
,
1111 struct cli_state
*cli
,
1112 const char *fname_src
,
1113 const char *fname_dst
,
1116 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1117 struct cli_cifs_rename_state
*state
= NULL
;
1118 uint8_t additional_flags
= 0;
1119 uint16_t additional_flags2
= 0;
1120 uint8_t *bytes
= NULL
;
1122 req
= tevent_req_create(mem_ctx
, &state
, struct cli_cifs_rename_state
);
1129 * CIFS doesn't support replace
1131 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1132 return tevent_req_post(req
, ev
);
1135 SSVAL(state
->vwv
+0, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1137 bytes
= talloc_array(state
, uint8_t, 1);
1138 if (tevent_req_nomem(bytes
, req
)) {
1139 return tevent_req_post(req
, ev
);
1142 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1143 strlen(fname_src
)+1, NULL
);
1144 if (tevent_req_nomem(bytes
, req
)) {
1145 return tevent_req_post(req
, ev
);
1148 if (clistr_is_previous_version_path(fname_src
, NULL
, NULL
, NULL
)) {
1149 additional_flags2
= FLAGS2_REPARSE_PATH
;
1152 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1153 talloc_get_size(bytes
)+1);
1154 if (tevent_req_nomem(bytes
, req
)) {
1155 return tevent_req_post(req
, ev
);
1158 bytes
[talloc_get_size(bytes
)-1] = 4;
1159 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1160 strlen(fname_dst
)+1, NULL
);
1161 if (tevent_req_nomem(bytes
, req
)) {
1162 return tevent_req_post(req
, ev
);
1165 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1167 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1168 if (tevent_req_nomem(subreq
, req
)) {
1169 return tevent_req_post(req
, ev
);
1171 tevent_req_set_callback(subreq
, cli_cifs_rename_done
, req
);
1175 static void cli_cifs_rename_done(struct tevent_req
*subreq
)
1177 struct tevent_req
*req
= tevent_req_callback_data(
1178 subreq
, struct tevent_req
);
1181 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1182 TALLOC_FREE(subreq
);
1183 if (tevent_req_nterror(req
, status
)) {
1186 tevent_req_done(req
);
1189 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1191 return tevent_req_simple_recv_ntstatus(req
);
1194 NTSTATUS
cli_rename(struct cli_state
*cli
,
1195 const char *fname_src
,
1196 const char *fname_dst
,
1199 TALLOC_CTX
*frame
= NULL
;
1200 struct tevent_context
*ev
;
1201 struct tevent_req
*req
;
1202 NTSTATUS status
= NT_STATUS_OK
;
1204 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1205 return cli_smb2_rename(cli
, fname_src
, fname_dst
, replace
);
1208 frame
= talloc_stackframe();
1210 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1212 * Can't use sync call while an async call is in flight
1214 status
= NT_STATUS_INVALID_PARAMETER
;
1218 ev
= samba_tevent_context_init(frame
);
1220 status
= NT_STATUS_NO_MEMORY
;
1224 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
, replace
);
1226 status
= NT_STATUS_NO_MEMORY
;
1230 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1234 status
= cli_rename_recv(req
);
1241 /****************************************************************************
1243 ****************************************************************************/
1245 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1247 struct cli_ntrename_internal_state
{
1251 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1252 struct tevent_context
*ev
,
1253 struct cli_state
*cli
,
1254 const char *fname_src
,
1255 const char *fname_dst
,
1256 uint16_t rename_flag
)
1258 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1259 struct cli_ntrename_internal_state
*state
= NULL
;
1260 uint8_t additional_flags
= 0;
1261 uint16_t additional_flags2
= 0;
1262 uint8_t *bytes
= NULL
;
1264 req
= tevent_req_create(mem_ctx
, &state
,
1265 struct cli_ntrename_internal_state
);
1270 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1271 SSVAL(state
->vwv
+1, 0, rename_flag
);
1273 bytes
= talloc_array(state
, uint8_t, 1);
1274 if (tevent_req_nomem(bytes
, req
)) {
1275 return tevent_req_post(req
, ev
);
1278 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1279 strlen(fname_src
)+1, NULL
);
1280 if (tevent_req_nomem(bytes
, req
)) {
1281 return tevent_req_post(req
, ev
);
1284 if (clistr_is_previous_version_path(fname_src
, NULL
, NULL
, NULL
)) {
1285 additional_flags2
= FLAGS2_REPARSE_PATH
;
1288 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1289 talloc_get_size(bytes
)+1);
1290 if (tevent_req_nomem(bytes
, req
)) {
1291 return tevent_req_post(req
, ev
);
1294 bytes
[talloc_get_size(bytes
)-1] = 4;
1295 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1296 strlen(fname_dst
)+1, NULL
);
1297 if (tevent_req_nomem(bytes
, req
)) {
1298 return tevent_req_post(req
, ev
);
1301 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1303 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1304 if (tevent_req_nomem(subreq
, req
)) {
1305 return tevent_req_post(req
, ev
);
1307 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1311 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1313 struct tevent_req
*req
= tevent_req_callback_data(
1314 subreq
, struct tevent_req
);
1317 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1318 TALLOC_FREE(subreq
);
1319 if (tevent_req_nterror(req
, status
)) {
1322 tevent_req_done(req
);
1325 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1327 return tevent_req_simple_recv_ntstatus(req
);
1330 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1331 struct tevent_context
*ev
,
1332 struct cli_state
*cli
,
1333 const char *fname_src
,
1334 const char *fname_dst
)
1336 return cli_ntrename_internal_send(mem_ctx
,
1341 RENAME_FLAG_RENAME
);
1344 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1346 return cli_ntrename_internal_recv(req
);
1349 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1351 TALLOC_CTX
*frame
= talloc_stackframe();
1352 struct tevent_context
*ev
;
1353 struct tevent_req
*req
;
1354 NTSTATUS status
= NT_STATUS_OK
;
1356 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1358 * Can't use sync call while an async call is in flight
1360 status
= NT_STATUS_INVALID_PARAMETER
;
1364 ev
= samba_tevent_context_init(frame
);
1366 status
= NT_STATUS_NO_MEMORY
;
1370 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1372 status
= NT_STATUS_NO_MEMORY
;
1376 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1380 status
= cli_ntrename_recv(req
);
1387 /****************************************************************************
1389 ****************************************************************************/
1391 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1392 struct tevent_context
*ev
,
1393 struct cli_state
*cli
,
1394 const char *fname_src
,
1395 const char *fname_dst
)
1397 return cli_ntrename_internal_send(mem_ctx
,
1402 RENAME_FLAG_HARD_LINK
);
1405 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1407 return cli_ntrename_internal_recv(req
);
1410 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1412 TALLOC_CTX
*frame
= talloc_stackframe();
1413 struct tevent_context
*ev
;
1414 struct tevent_req
*req
;
1415 NTSTATUS status
= NT_STATUS_OK
;
1417 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1419 * Can't use sync call while an async call is in flight
1421 status
= NT_STATUS_INVALID_PARAMETER
;
1425 ev
= samba_tevent_context_init(frame
);
1427 status
= NT_STATUS_NO_MEMORY
;
1431 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1433 status
= NT_STATUS_NO_MEMORY
;
1437 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1441 status
= cli_nt_hardlink_recv(req
);
1448 /****************************************************************************
1450 ****************************************************************************/
1452 static void cli_unlink_done(struct tevent_req
*subreq
);
1454 struct cli_unlink_state
{
1458 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1459 struct tevent_context
*ev
,
1460 struct cli_state
*cli
,
1462 uint16_t mayhave_attrs
)
1464 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1465 struct cli_unlink_state
*state
= NULL
;
1466 uint8_t additional_flags
= 0;
1467 uint16_t additional_flags2
= 0;
1468 uint8_t *bytes
= NULL
;
1470 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1475 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1477 bytes
= talloc_array(state
, uint8_t, 1);
1478 if (tevent_req_nomem(bytes
, req
)) {
1479 return tevent_req_post(req
, ev
);
1482 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
1483 strlen(fname
)+1, NULL
);
1485 if (tevent_req_nomem(bytes
, req
)) {
1486 return tevent_req_post(req
, ev
);
1489 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
1490 additional_flags2
= FLAGS2_REPARSE_PATH
;
1493 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1495 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1496 if (tevent_req_nomem(subreq
, req
)) {
1497 return tevent_req_post(req
, ev
);
1499 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1503 static void cli_unlink_done(struct tevent_req
*subreq
)
1505 struct tevent_req
*req
= tevent_req_callback_data(
1506 subreq
, struct tevent_req
);
1509 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1510 TALLOC_FREE(subreq
);
1511 if (tevent_req_nterror(req
, status
)) {
1514 tevent_req_done(req
);
1517 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1519 return tevent_req_simple_recv_ntstatus(req
);
1522 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1524 TALLOC_CTX
*frame
= NULL
;
1525 struct tevent_context
*ev
;
1526 struct tevent_req
*req
;
1527 NTSTATUS status
= NT_STATUS_OK
;
1529 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1530 return cli_smb2_unlink(cli
, fname
);
1533 frame
= talloc_stackframe();
1535 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1537 * Can't use sync call while an async call is in flight
1539 status
= NT_STATUS_INVALID_PARAMETER
;
1543 ev
= samba_tevent_context_init(frame
);
1545 status
= NT_STATUS_NO_MEMORY
;
1549 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1551 status
= NT_STATUS_NO_MEMORY
;
1555 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1559 status
= cli_unlink_recv(req
);
1566 /****************************************************************************
1568 ****************************************************************************/
1570 static void cli_mkdir_done(struct tevent_req
*subreq
);
1572 struct cli_mkdir_state
{
1576 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1577 struct tevent_context
*ev
,
1578 struct cli_state
*cli
,
1581 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1582 struct cli_mkdir_state
*state
= NULL
;
1583 uint8_t additional_flags
= 0;
1584 uint16_t additional_flags2
= 0;
1585 uint8_t *bytes
= NULL
;
1587 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1592 bytes
= talloc_array(state
, uint8_t, 1);
1593 if (tevent_req_nomem(bytes
, req
)) {
1594 return tevent_req_post(req
, ev
);
1597 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1598 strlen(dname
)+1, NULL
);
1600 if (tevent_req_nomem(bytes
, req
)) {
1601 return tevent_req_post(req
, ev
);
1604 if (clistr_is_previous_version_path(dname
, NULL
, NULL
, NULL
)) {
1605 additional_flags2
= FLAGS2_REPARSE_PATH
;
1608 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1610 0, NULL
, talloc_get_size(bytes
), bytes
);
1611 if (tevent_req_nomem(subreq
, req
)) {
1612 return tevent_req_post(req
, ev
);
1614 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1618 static void cli_mkdir_done(struct tevent_req
*subreq
)
1620 struct tevent_req
*req
= tevent_req_callback_data(
1621 subreq
, struct tevent_req
);
1624 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1625 TALLOC_FREE(subreq
);
1626 if (tevent_req_nterror(req
, status
)) {
1629 tevent_req_done(req
);
1632 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1634 return tevent_req_simple_recv_ntstatus(req
);
1637 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1639 TALLOC_CTX
*frame
= NULL
;
1640 struct tevent_context
*ev
;
1641 struct tevent_req
*req
;
1642 NTSTATUS status
= NT_STATUS_OK
;
1644 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1645 return cli_smb2_mkdir(cli
, dname
);
1648 frame
= talloc_stackframe();
1650 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1652 * Can't use sync call while an async call is in flight
1654 status
= NT_STATUS_INVALID_PARAMETER
;
1658 ev
= samba_tevent_context_init(frame
);
1660 status
= NT_STATUS_NO_MEMORY
;
1664 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1666 status
= NT_STATUS_NO_MEMORY
;
1670 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1674 status
= cli_mkdir_recv(req
);
1681 /****************************************************************************
1683 ****************************************************************************/
1685 static void cli_rmdir_done(struct tevent_req
*subreq
);
1687 struct cli_rmdir_state
{
1691 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1692 struct tevent_context
*ev
,
1693 struct cli_state
*cli
,
1696 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1697 struct cli_rmdir_state
*state
= NULL
;
1698 uint8_t additional_flags
= 0;
1699 uint16_t additional_flags2
= 0;
1700 uint8_t *bytes
= NULL
;
1702 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1707 bytes
= talloc_array(state
, uint8_t, 1);
1708 if (tevent_req_nomem(bytes
, req
)) {
1709 return tevent_req_post(req
, ev
);
1712 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1713 strlen(dname
)+1, NULL
);
1715 if (tevent_req_nomem(bytes
, req
)) {
1716 return tevent_req_post(req
, ev
);
1719 if (clistr_is_previous_version_path(dname
, NULL
, NULL
, NULL
)) {
1720 additional_flags2
= FLAGS2_REPARSE_PATH
;
1723 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1725 0, NULL
, talloc_get_size(bytes
), bytes
);
1726 if (tevent_req_nomem(subreq
, req
)) {
1727 return tevent_req_post(req
, ev
);
1729 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1733 static void cli_rmdir_done(struct tevent_req
*subreq
)
1735 struct tevent_req
*req
= tevent_req_callback_data(
1736 subreq
, struct tevent_req
);
1739 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1740 TALLOC_FREE(subreq
);
1741 if (tevent_req_nterror(req
, status
)) {
1744 tevent_req_done(req
);
1747 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1749 return tevent_req_simple_recv_ntstatus(req
);
1752 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1754 TALLOC_CTX
*frame
= NULL
;
1755 struct tevent_context
*ev
;
1756 struct tevent_req
*req
;
1757 NTSTATUS status
= NT_STATUS_OK
;
1759 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1760 return cli_smb2_rmdir(cli
, dname
);
1763 frame
= talloc_stackframe();
1765 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1767 * Can't use sync call while an async call is in flight
1769 status
= NT_STATUS_INVALID_PARAMETER
;
1773 ev
= samba_tevent_context_init(frame
);
1775 status
= NT_STATUS_NO_MEMORY
;
1779 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1781 status
= NT_STATUS_NO_MEMORY
;
1785 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1789 status
= cli_rmdir_recv(req
);
1796 /****************************************************************************
1797 Set or clear the delete on close flag.
1798 ****************************************************************************/
1806 static void cli_nt_delete_on_close_smb1_done(struct tevent_req
*subreq
);
1807 static void cli_nt_delete_on_close_smb2_done(struct tevent_req
*subreq
);
1809 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1810 struct tevent_context
*ev
,
1811 struct cli_state
*cli
,
1815 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1816 struct doc_state
*state
= NULL
;
1818 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1823 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1824 subreq
= cli_smb2_delete_on_close_send(state
, ev
, cli
,
1826 if (tevent_req_nomem(subreq
, req
)) {
1827 return tevent_req_post(req
, ev
);
1829 tevent_req_set_callback(subreq
,
1830 cli_nt_delete_on_close_smb2_done
,
1835 /* Setup setup word. */
1836 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1838 /* Setup param array. */
1839 SSVAL(state
->param
,0,fnum
);
1840 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1842 /* Setup data array. */
1843 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1845 subreq
= cli_trans_send(state
, /* mem ctx. */
1846 ev
, /* event ctx. */
1847 cli
, /* cli_state. */
1848 0, /* additional_flags2 */
1849 SMBtrans2
, /* cmd. */
1850 NULL
, /* pipe name. */
1854 &state
->setup
, /* setup. */
1855 1, /* num setup uint16_t words. */
1856 0, /* max returned setup. */
1857 state
->param
, /* param. */
1859 2, /* max returned param. */
1860 state
->data
, /* data. */
1862 0); /* max returned data. */
1864 if (tevent_req_nomem(subreq
, req
)) {
1865 return tevent_req_post(req
, ev
);
1867 tevent_req_set_callback(subreq
,
1868 cli_nt_delete_on_close_smb1_done
,
1873 static void cli_nt_delete_on_close_smb1_done(struct tevent_req
*subreq
)
1875 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1876 NULL
, 0, NULL
, NULL
, 0, NULL
);
1877 tevent_req_simple_finish_ntstatus(subreq
, status
);
1880 static void cli_nt_delete_on_close_smb2_done(struct tevent_req
*subreq
)
1882 NTSTATUS status
= cli_smb2_delete_on_close_recv(subreq
);
1883 tevent_req_simple_finish_ntstatus(subreq
, status
);
1886 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1888 return tevent_req_simple_recv_ntstatus(req
);
1891 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1893 TALLOC_CTX
*frame
= talloc_stackframe();
1894 struct tevent_context
*ev
= NULL
;
1895 struct tevent_req
*req
= NULL
;
1896 NTSTATUS status
= NT_STATUS_OK
;
1898 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1900 * Can't use sync call while an async call is in flight
1902 status
= NT_STATUS_INVALID_PARAMETER
;
1906 ev
= samba_tevent_context_init(frame
);
1908 status
= NT_STATUS_NO_MEMORY
;
1912 req
= cli_nt_delete_on_close_send(frame
,
1918 status
= NT_STATUS_NO_MEMORY
;
1922 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1926 status
= cli_nt_delete_on_close_recv(req
);
1933 struct cli_ntcreate1_state
{
1936 struct smb_create_returns cr
;
1937 struct tevent_req
*subreq
;
1940 static void cli_ntcreate1_done(struct tevent_req
*subreq
);
1941 static bool cli_ntcreate1_cancel(struct tevent_req
*req
);
1943 static struct tevent_req
*cli_ntcreate1_send(TALLOC_CTX
*mem_ctx
,
1944 struct tevent_context
*ev
,
1945 struct cli_state
*cli
,
1947 uint32_t CreatFlags
,
1948 uint32_t DesiredAccess
,
1949 uint32_t FileAttributes
,
1950 uint32_t ShareAccess
,
1951 uint32_t CreateDisposition
,
1952 uint32_t CreateOptions
,
1953 uint8_t SecurityFlags
)
1955 struct tevent_req
*req
, *subreq
;
1956 struct cli_ntcreate1_state
*state
;
1959 size_t converted_len
;
1960 uint16_t additional_flags2
= 0;
1962 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate1_state
);
1969 SCVAL(vwv
+0, 0, 0xFF);
1974 if (cli
->use_oplocks
) {
1975 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1977 SIVAL(vwv
+3, 1, CreatFlags
);
1978 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1979 SIVAL(vwv
+7, 1, DesiredAccess
);
1980 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1981 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1982 SIVAL(vwv
+13, 1, FileAttributes
);
1983 SIVAL(vwv
+15, 1, ShareAccess
);
1984 SIVAL(vwv
+17, 1, CreateDisposition
);
1985 SIVAL(vwv
+19, 1, CreateOptions
|
1986 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1987 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1988 SCVAL(vwv
+23, 1, SecurityFlags
);
1990 bytes
= talloc_array(state
, uint8_t, 0);
1991 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1992 fname
, strlen(fname
)+1,
1995 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
1996 additional_flags2
= FLAGS2_REPARSE_PATH
;
1999 /* sigh. this copes with broken netapp filer behaviour */
2000 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
2002 if (tevent_req_nomem(bytes
, req
)) {
2003 return tevent_req_post(req
, ev
);
2006 SSVAL(vwv
+2, 1, converted_len
);
2008 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0,
2009 additional_flags2
, 24, vwv
,
2010 talloc_get_size(bytes
), bytes
);
2011 if (tevent_req_nomem(subreq
, req
)) {
2012 return tevent_req_post(req
, ev
);
2014 tevent_req_set_callback(subreq
, cli_ntcreate1_done
, req
);
2016 state
->subreq
= subreq
;
2017 tevent_req_set_cancel_fn(req
, cli_ntcreate1_cancel
);
2022 static void cli_ntcreate1_done(struct tevent_req
*subreq
)
2024 struct tevent_req
*req
= tevent_req_callback_data(
2025 subreq
, struct tevent_req
);
2026 struct cli_ntcreate1_state
*state
= tevent_req_data(
2027 req
, struct cli_ntcreate1_state
);
2034 status
= cli_smb_recv(subreq
, state
, NULL
, 34, &wct
, &vwv
,
2035 &num_bytes
, &bytes
);
2036 TALLOC_FREE(subreq
);
2037 if (tevent_req_nterror(req
, status
)) {
2040 state
->cr
.oplock_level
= CVAL(vwv
+2, 0);
2041 state
->fnum
= SVAL(vwv
+2, 1);
2042 state
->cr
.create_action
= IVAL(vwv
+3, 1);
2043 state
->cr
.creation_time
= BVAL(vwv
+5, 1);
2044 state
->cr
.last_access_time
= BVAL(vwv
+9, 1);
2045 state
->cr
.last_write_time
= BVAL(vwv
+13, 1);
2046 state
->cr
.change_time
= BVAL(vwv
+17, 1);
2047 state
->cr
.file_attributes
= IVAL(vwv
+21, 1);
2048 state
->cr
.allocation_size
= BVAL(vwv
+23, 1);
2049 state
->cr
.end_of_file
= BVAL(vwv
+27, 1);
2051 tevent_req_done(req
);
2054 static bool cli_ntcreate1_cancel(struct tevent_req
*req
)
2056 struct cli_ntcreate1_state
*state
= tevent_req_data(
2057 req
, struct cli_ntcreate1_state
);
2058 return tevent_req_cancel(state
->subreq
);
2061 static NTSTATUS
cli_ntcreate1_recv(struct tevent_req
*req
,
2063 struct smb_create_returns
*cr
)
2065 struct cli_ntcreate1_state
*state
= tevent_req_data(
2066 req
, struct cli_ntcreate1_state
);
2069 if (tevent_req_is_nterror(req
, &status
)) {
2072 *pfnum
= state
->fnum
;
2076 return NT_STATUS_OK
;
2079 struct cli_ntcreate_state
{
2080 NTSTATUS (*recv
)(struct tevent_req
*req
, uint16_t *fnum
,
2081 struct smb_create_returns
*cr
);
2082 struct smb_create_returns cr
;
2084 struct tevent_req
*subreq
;
2087 static void cli_ntcreate_done(struct tevent_req
*subreq
);
2088 static bool cli_ntcreate_cancel(struct tevent_req
*req
);
2090 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
2091 struct tevent_context
*ev
,
2092 struct cli_state
*cli
,
2094 uint32_t create_flags
,
2095 uint32_t desired_access
,
2096 uint32_t file_attributes
,
2097 uint32_t share_access
,
2098 uint32_t create_disposition
,
2099 uint32_t create_options
,
2100 uint8_t security_flags
)
2102 struct tevent_req
*req
, *subreq
;
2103 struct cli_ntcreate_state
*state
;
2105 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
2110 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2111 state
->recv
= cli_smb2_create_fnum_recv
;
2113 if (cli
->use_oplocks
) {
2114 create_flags
|= REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
;
2117 subreq
= cli_smb2_create_fnum_send(
2118 state
, ev
, cli
, fname
, create_flags
, desired_access
,
2119 file_attributes
, share_access
, create_disposition
,
2122 state
->recv
= cli_ntcreate1_recv
;
2123 subreq
= cli_ntcreate1_send(
2124 state
, ev
, cli
, fname
, create_flags
, desired_access
,
2125 file_attributes
, share_access
, create_disposition
,
2126 create_options
, security_flags
);
2128 if (tevent_req_nomem(subreq
, req
)) {
2129 return tevent_req_post(req
, ev
);
2131 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
2133 state
->subreq
= subreq
;
2134 tevent_req_set_cancel_fn(req
, cli_ntcreate_cancel
);
2139 static void cli_ntcreate_done(struct tevent_req
*subreq
)
2141 struct tevent_req
*req
= tevent_req_callback_data(
2142 subreq
, struct tevent_req
);
2143 struct cli_ntcreate_state
*state
= tevent_req_data(
2144 req
, struct cli_ntcreate_state
);
2147 status
= state
->recv(subreq
, &state
->fnum
, &state
->cr
);
2148 TALLOC_FREE(subreq
);
2149 if (tevent_req_nterror(req
, status
)) {
2152 tevent_req_done(req
);
2155 static bool cli_ntcreate_cancel(struct tevent_req
*req
)
2157 struct cli_ntcreate_state
*state
= tevent_req_data(
2158 req
, struct cli_ntcreate_state
);
2159 return tevent_req_cancel(state
->subreq
);
2162 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *fnum
,
2163 struct smb_create_returns
*cr
)
2165 struct cli_ntcreate_state
*state
= tevent_req_data(
2166 req
, struct cli_ntcreate_state
);
2169 if (tevent_req_is_nterror(req
, &status
)) {
2173 *fnum
= state
->fnum
;
2178 return NT_STATUS_OK
;
2181 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2183 uint32_t CreatFlags
,
2184 uint32_t DesiredAccess
,
2185 uint32_t FileAttributes
,
2186 uint32_t ShareAccess
,
2187 uint32_t CreateDisposition
,
2188 uint32_t CreateOptions
,
2189 uint8_t SecurityFlags
,
2191 struct smb_create_returns
*cr
)
2193 TALLOC_CTX
*frame
= talloc_stackframe();
2194 struct tevent_context
*ev
;
2195 struct tevent_req
*req
;
2196 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2198 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2200 * Can't use sync call while an async call is in flight
2202 status
= NT_STATUS_INVALID_PARAMETER
;
2206 ev
= samba_tevent_context_init(frame
);
2211 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2212 DesiredAccess
, FileAttributes
, ShareAccess
,
2213 CreateDisposition
, CreateOptions
,
2219 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2223 status
= cli_ntcreate_recv(req
, pfid
, cr
);
2229 struct cli_nttrans_create_state
{
2231 struct smb_create_returns cr
;
2234 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
2236 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
2237 struct tevent_context
*ev
,
2238 struct cli_state
*cli
,
2240 uint32_t CreatFlags
,
2241 uint32_t DesiredAccess
,
2242 uint32_t FileAttributes
,
2243 uint32_t ShareAccess
,
2244 uint32_t CreateDisposition
,
2245 uint32_t CreateOptions
,
2246 uint8_t SecurityFlags
,
2247 struct security_descriptor
*secdesc
,
2248 struct ea_struct
*eas
,
2251 struct tevent_req
*req
, *subreq
;
2252 struct cli_nttrans_create_state
*state
;
2254 uint8_t *secdesc_buf
;
2257 size_t converted_len
;
2258 uint16_t additional_flags2
= 0;
2260 req
= tevent_req_create(mem_ctx
,
2261 &state
, struct cli_nttrans_create_state
);
2266 if (secdesc
!= NULL
) {
2267 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2268 &secdesc_buf
, &secdesc_len
);
2269 if (tevent_req_nterror(req
, status
)) {
2270 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2271 nt_errstr(status
)));
2272 return tevent_req_post(req
, ev
);
2283 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2284 return tevent_req_post(req
, ev
);
2287 param
= talloc_array(state
, uint8_t, 53);
2288 if (tevent_req_nomem(param
, req
)) {
2289 return tevent_req_post(req
, ev
);
2292 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2293 fname
, strlen(fname
),
2295 if (tevent_req_nomem(param
, req
)) {
2296 return tevent_req_post(req
, ev
);
2299 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
2300 additional_flags2
= FLAGS2_REPARSE_PATH
;
2303 SIVAL(param
, 0, CreatFlags
);
2304 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2305 SIVAL(param
, 8, DesiredAccess
);
2306 SIVAL(param
, 12, 0x0); /* AllocationSize */
2307 SIVAL(param
, 16, 0x0); /* AllocationSize */
2308 SIVAL(param
, 20, FileAttributes
);
2309 SIVAL(param
, 24, ShareAccess
);
2310 SIVAL(param
, 28, CreateDisposition
);
2311 SIVAL(param
, 32, CreateOptions
|
2312 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2313 SIVAL(param
, 36, secdesc_len
);
2314 SIVAL(param
, 40, 0); /* EA length*/
2315 SIVAL(param
, 44, converted_len
);
2316 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2317 SCVAL(param
, 52, SecurityFlags
);
2319 subreq
= cli_trans_send(state
, ev
, cli
,
2320 additional_flags2
, /* additional_flags2 */
2322 NULL
, -1, /* name, fid */
2323 NT_TRANSACT_CREATE
, 0,
2324 NULL
, 0, 0, /* setup */
2325 param
, talloc_get_size(param
), 128, /* param */
2326 secdesc_buf
, secdesc_len
, 0); /* data */
2327 if (tevent_req_nomem(subreq
, req
)) {
2328 return tevent_req_post(req
, ev
);
2330 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2334 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2336 struct tevent_req
*req
= tevent_req_callback_data(
2337 subreq
, struct tevent_req
);
2338 struct cli_nttrans_create_state
*state
= tevent_req_data(
2339 req
, struct cli_nttrans_create_state
);
2344 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2345 NULL
, 0, NULL
, /* rsetup */
2346 ¶m
, 69, &num_param
,
2348 if (tevent_req_nterror(req
, status
)) {
2351 state
->cr
.oplock_level
= CVAL(param
, 0);
2352 state
->fnum
= SVAL(param
, 2);
2353 state
->cr
.create_action
= IVAL(param
, 4);
2354 state
->cr
.creation_time
= BVAL(param
, 12);
2355 state
->cr
.last_access_time
= BVAL(param
, 20);
2356 state
->cr
.last_write_time
= BVAL(param
, 28);
2357 state
->cr
.change_time
= BVAL(param
, 36);
2358 state
->cr
.file_attributes
= IVAL(param
, 44);
2359 state
->cr
.allocation_size
= BVAL(param
, 48);
2360 state
->cr
.end_of_file
= BVAL(param
, 56);
2363 tevent_req_done(req
);
2366 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2368 struct smb_create_returns
*cr
)
2370 struct cli_nttrans_create_state
*state
= tevent_req_data(
2371 req
, struct cli_nttrans_create_state
);
2374 if (tevent_req_is_nterror(req
, &status
)) {
2377 *fnum
= state
->fnum
;
2381 return NT_STATUS_OK
;
2384 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2386 uint32_t CreatFlags
,
2387 uint32_t DesiredAccess
,
2388 uint32_t FileAttributes
,
2389 uint32_t ShareAccess
,
2390 uint32_t CreateDisposition
,
2391 uint32_t CreateOptions
,
2392 uint8_t SecurityFlags
,
2393 struct security_descriptor
*secdesc
,
2394 struct ea_struct
*eas
,
2397 struct smb_create_returns
*cr
)
2399 TALLOC_CTX
*frame
= talloc_stackframe();
2400 struct tevent_context
*ev
;
2401 struct tevent_req
*req
;
2402 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2404 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2406 * Can't use sync call while an async call is in flight
2408 status
= NT_STATUS_INVALID_PARAMETER
;
2411 ev
= samba_tevent_context_init(frame
);
2415 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2416 DesiredAccess
, FileAttributes
,
2417 ShareAccess
, CreateDisposition
,
2418 CreateOptions
, SecurityFlags
,
2419 secdesc
, eas
, num_eas
);
2423 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2426 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2432 /****************************************************************************
2434 WARNING: if you open with O_WRONLY then getattrE won't work!
2435 ****************************************************************************/
2437 struct cli_openx_state
{
2444 static void cli_openx_done(struct tevent_req
*subreq
);
2446 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2447 struct tevent_context
*ev
,
2448 struct cli_state
*cli
, const char *fname
,
2449 int flags
, int share_mode
,
2450 struct tevent_req
**psmbreq
)
2452 struct tevent_req
*req
, *subreq
;
2453 struct cli_openx_state
*state
;
2455 unsigned accessmode
;
2456 uint8_t additional_flags
;
2457 uint16_t additional_flags2
= 0;
2460 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2466 if (flags
& O_CREAT
) {
2469 if (!(flags
& O_EXCL
)) {
2470 if (flags
& O_TRUNC
)
2476 accessmode
= (share_mode
<<4);
2478 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2480 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2485 if ((flags
& O_SYNC
) == O_SYNC
) {
2486 accessmode
|= (1<<14);
2490 if (share_mode
== DENY_FCB
) {
2494 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2495 SCVAL(state
->vwv
+ 0, 1, 0);
2496 SSVAL(state
->vwv
+ 1, 0, 0);
2497 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2498 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2499 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2500 SSVAL(state
->vwv
+ 5, 0, 0);
2501 SIVAL(state
->vwv
+ 6, 0, 0);
2502 SSVAL(state
->vwv
+ 8, 0, openfn
);
2503 SIVAL(state
->vwv
+ 9, 0, 0);
2504 SIVAL(state
->vwv
+ 11, 0, 0);
2505 SIVAL(state
->vwv
+ 13, 0, 0);
2507 additional_flags
= 0;
2509 if (cli
->use_oplocks
) {
2510 /* if using oplocks then ask for a batch oplock via
2511 core and extended methods */
2513 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2514 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2517 bytes
= talloc_array(state
, uint8_t, 0);
2518 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2519 strlen(fname
)+1, NULL
);
2521 if (tevent_req_nomem(bytes
, req
)) {
2522 return tevent_req_post(req
, ev
);
2525 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
2526 additional_flags2
= FLAGS2_REPARSE_PATH
;
2529 state
->bytes
.iov_base
= (void *)bytes
;
2530 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2532 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2533 additional_flags2
, 15, state
->vwv
, 1, &state
->bytes
);
2534 if (subreq
== NULL
) {
2538 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2543 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2544 struct cli_state
*cli
, const char *fname
,
2545 int flags
, int share_mode
)
2547 struct tevent_req
*req
, *subreq
;
2550 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2556 status
= smb1cli_req_chain_submit(&subreq
, 1);
2557 if (tevent_req_nterror(req
, status
)) {
2558 return tevent_req_post(req
, ev
);
2563 static void cli_openx_done(struct tevent_req
*subreq
)
2565 struct tevent_req
*req
= tevent_req_callback_data(
2566 subreq
, struct tevent_req
);
2567 struct cli_openx_state
*state
= tevent_req_data(
2568 req
, struct cli_openx_state
);
2573 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2575 TALLOC_FREE(subreq
);
2576 if (tevent_req_nterror(req
, status
)) {
2579 state
->fnum
= SVAL(vwv
+2, 0);
2580 tevent_req_done(req
);
2583 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2585 struct cli_openx_state
*state
= tevent_req_data(
2586 req
, struct cli_openx_state
);
2589 if (tevent_req_is_nterror(req
, &status
)) {
2592 *pfnum
= state
->fnum
;
2593 return NT_STATUS_OK
;
2596 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2597 int share_mode
, uint16_t *pfnum
)
2599 TALLOC_CTX
*frame
= talloc_stackframe();
2600 struct tevent_context
*ev
;
2601 struct tevent_req
*req
;
2602 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2604 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2606 * Can't use sync call while an async call is in flight
2608 status
= NT_STATUS_INVALID_PARAMETER
;
2612 ev
= samba_tevent_context_init(frame
);
2617 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2622 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2626 status
= cli_openx_recv(req
, pfnum
);
2631 /****************************************************************************
2632 Synchronous wrapper function that does an NtCreateX open by preference
2633 and falls back to openX if this fails.
2634 ****************************************************************************/
2636 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2637 int share_mode_in
, uint16_t *pfnum
)
2640 unsigned int openfn
= 0;
2641 unsigned int dos_deny
= 0;
2642 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2643 struct smb_create_returns cr
;
2645 /* Do the initial mapping into OpenX parameters. */
2646 if (flags
& O_CREAT
) {
2649 if (!(flags
& O_EXCL
)) {
2650 if (flags
& O_TRUNC
)
2656 dos_deny
= (share_mode_in
<<4);
2658 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2660 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2665 if ((flags
& O_SYNC
) == O_SYNC
) {
2666 dos_deny
|= (1<<14);
2670 if (share_mode_in
== DENY_FCB
) {
2675 /* Hmmm. This is what I think the above code
2676 should look like if it's using the constants
2679 if (flags
& O_CREAT
) {
2680 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2682 if (!(flags
& O_EXCL
)) {
2683 if (flags
& O_TRUNC
)
2684 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2686 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2689 dos_deny
= SET_DENY_MODE(share_mode_in
);
2691 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2692 dos_deny
|= DOS_OPEN_RDWR
;
2693 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2694 dos_deny
|= DOS_OPEN_WRONLY
;
2698 if ((flags
& O_SYNC
) == O_SYNC
) {
2699 dos_deny
|= FILE_SYNC_OPENMODE
;
2703 if (share_mode_in
== DENY_FCB
) {
2708 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2709 openfn
, &access_mask
,
2710 &share_mode
, &create_disposition
,
2711 &create_options
, NULL
)) {
2715 status
= cli_ntcreate(cli
,
2727 /* Try and cope will all varients of "we don't do this call"
2728 and fall back to openX. */
2730 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2731 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2732 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2733 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2734 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2735 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2736 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2737 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2738 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2742 if (NT_STATUS_IS_OK(status
) &&
2743 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2744 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2747 * Some (broken) servers return a valid handle
2748 * for directories even if FILE_NON_DIRECTORY_FILE
2749 * is set. Just close the handle and set the
2750 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2752 status
= cli_close(cli
, *pfnum
);
2753 if (!NT_STATUS_IS_OK(status
)) {
2756 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2757 /* Set this so libsmbclient can retrieve it. */
2758 cli
->raw_status
= status
;
2765 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2768 /****************************************************************************
2770 ****************************************************************************/
2772 struct cli_close_state
{
2776 static void cli_close_done(struct tevent_req
*subreq
);
2778 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2779 struct tevent_context
*ev
,
2780 struct cli_state
*cli
,
2782 struct tevent_req
**psubreq
)
2784 struct tevent_req
*req
, *subreq
;
2785 struct cli_close_state
*state
;
2787 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2792 SSVAL(state
->vwv
+0, 0, fnum
);
2793 SIVALS(state
->vwv
+1, 0, -1);
2795 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 0,
2796 3, state
->vwv
, 0, NULL
);
2797 if (subreq
== NULL
) {
2801 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2806 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2807 struct tevent_context
*ev
,
2808 struct cli_state
*cli
,
2811 struct tevent_req
*req
, *subreq
;
2814 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2819 status
= smb1cli_req_chain_submit(&subreq
, 1);
2820 if (tevent_req_nterror(req
, status
)) {
2821 return tevent_req_post(req
, ev
);
2826 static void cli_close_done(struct tevent_req
*subreq
)
2828 struct tevent_req
*req
= tevent_req_callback_data(
2829 subreq
, struct tevent_req
);
2832 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2833 TALLOC_FREE(subreq
);
2834 if (tevent_req_nterror(req
, status
)) {
2837 tevent_req_done(req
);
2840 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2842 return tevent_req_simple_recv_ntstatus(req
);
2845 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2847 TALLOC_CTX
*frame
= NULL
;
2848 struct tevent_context
*ev
;
2849 struct tevent_req
*req
;
2850 NTSTATUS status
= NT_STATUS_OK
;
2852 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2853 return cli_smb2_close_fnum(cli
, fnum
);
2856 frame
= talloc_stackframe();
2858 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2860 * Can't use sync call while an async call is in flight
2862 status
= NT_STATUS_INVALID_PARAMETER
;
2866 ev
= samba_tevent_context_init(frame
);
2868 status
= NT_STATUS_NO_MEMORY
;
2872 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2874 status
= NT_STATUS_NO_MEMORY
;
2878 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2882 status
= cli_close_recv(req
);
2888 /****************************************************************************
2889 Truncate a file to a specified size
2890 ****************************************************************************/
2892 struct ftrunc_state
{
2898 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2900 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2901 NULL
, 0, NULL
, NULL
, 0, NULL
);
2902 tevent_req_simple_finish_ntstatus(subreq
, status
);
2905 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2906 struct tevent_context
*ev
,
2907 struct cli_state
*cli
,
2911 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2912 struct ftrunc_state
*state
= NULL
;
2914 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2919 /* Setup setup word. */
2920 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2922 /* Setup param array. */
2923 SSVAL(state
->param
,0,fnum
);
2924 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2925 SSVAL(state
->param
,4,0);
2927 /* Setup data array. */
2928 SBVAL(state
->data
, 0, size
);
2930 subreq
= cli_trans_send(state
, /* mem ctx. */
2931 ev
, /* event ctx. */
2932 cli
, /* cli_state. */
2933 0, /* additional_flags2 */
2934 SMBtrans2
, /* cmd. */
2935 NULL
, /* pipe name. */
2939 &state
->setup
, /* setup. */
2940 1, /* num setup uint16_t words. */
2941 0, /* max returned setup. */
2942 state
->param
, /* param. */
2944 2, /* max returned param. */
2945 state
->data
, /* data. */
2947 0); /* max returned data. */
2949 if (tevent_req_nomem(subreq
, req
)) {
2950 return tevent_req_post(req
, ev
);
2952 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2956 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2958 return tevent_req_simple_recv_ntstatus(req
);
2961 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2963 TALLOC_CTX
*frame
= NULL
;
2964 struct tevent_context
*ev
= NULL
;
2965 struct tevent_req
*req
= NULL
;
2966 NTSTATUS status
= NT_STATUS_OK
;
2968 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2969 return cli_smb2_ftruncate(cli
, fnum
, size
);
2972 frame
= talloc_stackframe();
2974 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2976 * Can't use sync call while an async call is in flight
2978 status
= NT_STATUS_INVALID_PARAMETER
;
2982 ev
= samba_tevent_context_init(frame
);
2984 status
= NT_STATUS_NO_MEMORY
;
2988 req
= cli_ftruncate_send(frame
,
2994 status
= NT_STATUS_NO_MEMORY
;
2998 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3002 status
= cli_ftruncate_recv(req
);
3009 /****************************************************************************
3010 send a lock with a specified locktype
3011 this is used for testing LOCKING_ANDX_CANCEL_LOCK
3012 ****************************************************************************/
3014 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
3015 uint32_t offset
, uint32_t len
,
3016 int timeout
, unsigned char locktype
)
3021 unsigned int set_timeout
= 0;
3022 unsigned int saved_timeout
= 0;
3024 SCVAL(vwv
+ 0, 0, 0xff);
3025 SCVAL(vwv
+ 0, 1, 0);
3026 SSVAL(vwv
+ 1, 0, 0);
3027 SSVAL(vwv
+ 2, 0, fnum
);
3028 SCVAL(vwv
+ 3, 0, locktype
);
3029 SCVAL(vwv
+ 3, 1, 0);
3030 SIVALS(vwv
+ 4, 0, timeout
);
3031 SSVAL(vwv
+ 6, 0, 0);
3032 SSVAL(vwv
+ 7, 0, 1);
3034 SSVAL(bytes
, 0, cli_getpid(cli
));
3035 SIVAL(bytes
, 2, offset
);
3036 SIVAL(bytes
, 6, len
);
3039 if (timeout
== -1) {
3040 set_timeout
= 0x7FFFFFFF;
3042 set_timeout
= timeout
+ 2*1000;
3044 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3047 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3048 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3050 if (saved_timeout
!= 0) {
3051 cli_set_timeout(cli
, saved_timeout
);
3057 /****************************************************************************
3059 note that timeout is in units of 2 milliseconds
3060 ****************************************************************************/
3062 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
3063 uint32_t offset
, uint32_t len
, int timeout
,
3064 enum brl_type lock_type
)
3068 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
3069 (lock_type
== READ_LOCK
? 1 : 0));
3073 /****************************************************************************
3075 ****************************************************************************/
3077 struct cli_unlock_state
{
3082 static void cli_unlock_done(struct tevent_req
*subreq
);
3084 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
3085 struct tevent_context
*ev
,
3086 struct cli_state
*cli
,
3092 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3093 struct cli_unlock_state
*state
= NULL
;
3094 uint8_t additional_flags
= 0;
3096 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
3101 SCVAL(state
->vwv
+0, 0, 0xFF);
3102 SSVAL(state
->vwv
+2, 0, fnum
);
3103 SCVAL(state
->vwv
+3, 0, 0);
3104 SIVALS(state
->vwv
+4, 0, 0);
3105 SSVAL(state
->vwv
+6, 0, 1);
3106 SSVAL(state
->vwv
+7, 0, 0);
3108 SSVAL(state
->data
, 0, cli_getpid(cli
));
3109 SIVAL(state
->data
, 2, offset
);
3110 SIVAL(state
->data
, 6, len
);
3112 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
, 0,
3113 8, state
->vwv
, 10, state
->data
);
3114 if (tevent_req_nomem(subreq
, req
)) {
3115 return tevent_req_post(req
, ev
);
3117 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
3121 static void cli_unlock_done(struct tevent_req
*subreq
)
3123 struct tevent_req
*req
= tevent_req_callback_data(
3124 subreq
, struct tevent_req
);
3127 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3128 TALLOC_FREE(subreq
);
3129 if (tevent_req_nterror(req
, status
)) {
3132 tevent_req_done(req
);
3135 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
3137 return tevent_req_simple_recv_ntstatus(req
);
3140 NTSTATUS
cli_unlock(struct cli_state
*cli
,
3145 TALLOC_CTX
*frame
= talloc_stackframe();
3146 struct tevent_context
*ev
;
3147 struct tevent_req
*req
;
3148 NTSTATUS status
= NT_STATUS_OK
;
3150 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3152 * Can't use sync call while an async call is in flight
3154 status
= NT_STATUS_INVALID_PARAMETER
;
3158 ev
= samba_tevent_context_init(frame
);
3160 status
= NT_STATUS_NO_MEMORY
;
3164 req
= cli_unlock_send(frame
, ev
, cli
,
3167 status
= NT_STATUS_NO_MEMORY
;
3171 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3175 status
= cli_unlock_recv(req
);
3182 /****************************************************************************
3183 Lock a file with 64 bit offsets.
3184 ****************************************************************************/
3186 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
3187 uint64_t offset
, uint64_t len
, int timeout
,
3188 enum brl_type lock_type
)
3192 unsigned int set_timeout
= 0;
3193 unsigned int saved_timeout
= 0;
3197 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3198 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
3201 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
3202 ltype
|= LOCKING_ANDX_LARGE_FILES
;
3204 SCVAL(vwv
+ 0, 0, 0xff);
3205 SCVAL(vwv
+ 0, 1, 0);
3206 SSVAL(vwv
+ 1, 0, 0);
3207 SSVAL(vwv
+ 2, 0, fnum
);
3208 SCVAL(vwv
+ 3, 0, ltype
);
3209 SCVAL(vwv
+ 3, 1, 0);
3210 SIVALS(vwv
+ 4, 0, timeout
);
3211 SSVAL(vwv
+ 6, 0, 0);
3212 SSVAL(vwv
+ 7, 0, 1);
3214 SIVAL(bytes
, 0, cli_getpid(cli
));
3215 SOFF_T_R(bytes
, 4, offset
);
3216 SOFF_T_R(bytes
, 12, len
);
3219 if (timeout
== -1) {
3220 set_timeout
= 0x7FFFFFFF;
3222 set_timeout
= timeout
+ 2*1000;
3224 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3227 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3228 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3230 if (saved_timeout
!= 0) {
3231 cli_set_timeout(cli
, saved_timeout
);
3237 /****************************************************************************
3238 Unlock a file with 64 bit offsets.
3239 ****************************************************************************/
3241 struct cli_unlock64_state
{
3246 static void cli_unlock64_done(struct tevent_req
*subreq
);
3248 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
3249 struct tevent_context
*ev
,
3250 struct cli_state
*cli
,
3256 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3257 struct cli_unlock64_state
*state
= NULL
;
3258 uint8_t additional_flags
= 0;
3260 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3265 SCVAL(state
->vwv
+0, 0, 0xff);
3266 SSVAL(state
->vwv
+2, 0, fnum
);
3267 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3268 SIVALS(state
->vwv
+4, 0, 0);
3269 SSVAL(state
->vwv
+6, 0, 1);
3270 SSVAL(state
->vwv
+7, 0, 0);
3272 SIVAL(state
->data
, 0, cli_getpid(cli
));
3273 SOFF_T_R(state
->data
, 4, offset
);
3274 SOFF_T_R(state
->data
, 12, len
);
3276 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
, 0,
3277 8, state
->vwv
, 20, state
->data
);
3278 if (tevent_req_nomem(subreq
, req
)) {
3279 return tevent_req_post(req
, ev
);
3281 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3285 static void cli_unlock64_done(struct tevent_req
*subreq
)
3287 struct tevent_req
*req
= tevent_req_callback_data(
3288 subreq
, struct tevent_req
);
3291 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3292 TALLOC_FREE(subreq
);
3293 if (tevent_req_nterror(req
, status
)) {
3296 tevent_req_done(req
);
3299 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3301 return tevent_req_simple_recv_ntstatus(req
);
3304 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3309 TALLOC_CTX
*frame
= talloc_stackframe();
3310 struct tevent_context
*ev
;
3311 struct tevent_req
*req
;
3312 NTSTATUS status
= NT_STATUS_OK
;
3314 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3315 return cli_unlock(cli
, fnum
, offset
, len
);
3318 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3320 * Can't use sync call while an async call is in flight
3322 status
= NT_STATUS_INVALID_PARAMETER
;
3326 ev
= samba_tevent_context_init(frame
);
3328 status
= NT_STATUS_NO_MEMORY
;
3332 req
= cli_unlock64_send(frame
, ev
, cli
,
3335 status
= NT_STATUS_NO_MEMORY
;
3339 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3343 status
= cli_unlock64_recv(req
);
3350 /****************************************************************************
3351 Get/unlock a POSIX lock on a file - internal function.
3352 ****************************************************************************/
3354 struct posix_lock_state
{
3357 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3360 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3362 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3363 NULL
, 0, NULL
, NULL
, 0, NULL
);
3364 tevent_req_simple_finish_ntstatus(subreq
, status
);
3367 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3368 struct tevent_context
*ev
,
3369 struct cli_state
*cli
,
3374 enum brl_type lock_type
)
3376 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3377 struct posix_lock_state
*state
= NULL
;
3379 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3384 /* Setup setup word. */
3385 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3387 /* Setup param array. */
3388 SSVAL(&state
->param
, 0, fnum
);
3389 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3391 /* Setup data array. */
3392 switch (lock_type
) {
3394 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3395 POSIX_LOCK_TYPE_READ
);
3398 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3399 POSIX_LOCK_TYPE_WRITE
);
3402 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3403 POSIX_LOCK_TYPE_UNLOCK
);
3410 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3411 POSIX_LOCK_FLAG_WAIT
);
3413 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3414 POSIX_LOCK_FLAG_NOWAIT
);
3417 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3418 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3419 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3421 subreq
= cli_trans_send(state
, /* mem ctx. */
3422 ev
, /* event ctx. */
3423 cli
, /* cli_state. */
3424 0, /* additional_flags2 */
3425 SMBtrans2
, /* cmd. */
3426 NULL
, /* pipe name. */
3430 &state
->setup
, /* setup. */
3431 1, /* num setup uint16_t words. */
3432 0, /* max returned setup. */
3433 state
->param
, /* param. */
3435 2, /* max returned param. */
3436 state
->data
, /* data. */
3437 POSIX_LOCK_DATA_SIZE
, /* num data. */
3438 0); /* max returned data. */
3440 if (tevent_req_nomem(subreq
, req
)) {
3441 return tevent_req_post(req
, ev
);
3443 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3447 /****************************************************************************
3449 ****************************************************************************/
3451 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3452 struct tevent_context
*ev
,
3453 struct cli_state
*cli
,
3458 enum brl_type lock_type
)
3460 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3461 wait_lock
, lock_type
);
3464 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3466 return tevent_req_simple_recv_ntstatus(req
);
3469 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3470 uint64_t offset
, uint64_t len
,
3471 bool wait_lock
, enum brl_type lock_type
)
3473 TALLOC_CTX
*frame
= talloc_stackframe();
3474 struct tevent_context
*ev
= NULL
;
3475 struct tevent_req
*req
= NULL
;
3476 NTSTATUS status
= NT_STATUS_OK
;
3478 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3480 * Can't use sync call while an async call is in flight
3482 status
= NT_STATUS_INVALID_PARAMETER
;
3486 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3487 status
= NT_STATUS_INVALID_PARAMETER
;
3491 ev
= samba_tevent_context_init(frame
);
3493 status
= NT_STATUS_NO_MEMORY
;
3497 req
= cli_posix_lock_send(frame
,
3506 status
= NT_STATUS_NO_MEMORY
;
3510 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3514 status
= cli_posix_lock_recv(req
);
3521 /****************************************************************************
3522 POSIX Unlock a file.
3523 ****************************************************************************/
3525 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3526 struct tevent_context
*ev
,
3527 struct cli_state
*cli
,
3532 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3533 false, UNLOCK_LOCK
);
3536 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3538 return tevent_req_simple_recv_ntstatus(req
);
3541 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3543 TALLOC_CTX
*frame
= talloc_stackframe();
3544 struct tevent_context
*ev
= NULL
;
3545 struct tevent_req
*req
= NULL
;
3546 NTSTATUS status
= NT_STATUS_OK
;
3548 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3550 * Can't use sync call while an async call is in flight
3552 status
= NT_STATUS_INVALID_PARAMETER
;
3556 ev
= samba_tevent_context_init(frame
);
3558 status
= NT_STATUS_NO_MEMORY
;
3562 req
= cli_posix_unlock_send(frame
,
3569 status
= NT_STATUS_NO_MEMORY
;
3573 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3577 status
= cli_posix_unlock_recv(req
);
3584 /****************************************************************************
3585 Do a SMBgetattrE call.
3586 ****************************************************************************/
3588 static void cli_getattrE_done(struct tevent_req
*subreq
);
3590 struct cli_getattrE_state
{
3600 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3601 struct tevent_context
*ev
,
3602 struct cli_state
*cli
,
3605 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3606 struct cli_getattrE_state
*state
= NULL
;
3607 uint8_t additional_flags
= 0;
3609 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3614 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3615 SSVAL(state
->vwv
+0,0,fnum
);
3617 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
, 0,
3618 1, state
->vwv
, 0, NULL
);
3619 if (tevent_req_nomem(subreq
, req
)) {
3620 return tevent_req_post(req
, ev
);
3622 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3626 static void cli_getattrE_done(struct tevent_req
*subreq
)
3628 struct tevent_req
*req
= tevent_req_callback_data(
3629 subreq
, struct tevent_req
);
3630 struct cli_getattrE_state
*state
= tevent_req_data(
3631 req
, struct cli_getattrE_state
);
3633 uint16_t *vwv
= NULL
;
3636 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3638 TALLOC_FREE(subreq
);
3639 if (tevent_req_nterror(req
, status
)) {
3643 state
->size
= (off_t
)IVAL(vwv
+6,0);
3644 state
->attr
= SVAL(vwv
+10,0);
3645 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3646 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3647 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3649 tevent_req_done(req
);
3652 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3655 time_t *change_time
,
3656 time_t *access_time
,
3659 struct cli_getattrE_state
*state
= tevent_req_data(
3660 req
, struct cli_getattrE_state
);
3663 if (tevent_req_is_nterror(req
, &status
)) {
3667 *attr
= state
->attr
;
3670 *size
= state
->size
;
3673 *change_time
= state
->change_time
;
3676 *access_time
= state
->access_time
;
3679 *write_time
= state
->write_time
;
3681 return NT_STATUS_OK
;
3684 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3688 time_t *change_time
,
3689 time_t *access_time
,
3692 TALLOC_CTX
*frame
= NULL
;
3693 struct tevent_context
*ev
= NULL
;
3694 struct tevent_req
*req
= NULL
;
3695 NTSTATUS status
= NT_STATUS_OK
;
3697 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3698 return cli_smb2_getattrE(cli
,
3707 frame
= talloc_stackframe();
3709 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3711 * Can't use sync call while an async call is in flight
3713 status
= NT_STATUS_INVALID_PARAMETER
;
3717 ev
= samba_tevent_context_init(frame
);
3719 status
= NT_STATUS_NO_MEMORY
;
3723 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3725 status
= NT_STATUS_NO_MEMORY
;
3729 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3733 status
= cli_getattrE_recv(req
,
3745 /****************************************************************************
3747 ****************************************************************************/
3749 static void cli_getatr_done(struct tevent_req
*subreq
);
3751 struct cli_getatr_state
{
3758 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3759 struct tevent_context
*ev
,
3760 struct cli_state
*cli
,
3763 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3764 struct cli_getatr_state
*state
= NULL
;
3765 uint8_t additional_flags
= 0;
3766 uint16_t additional_flags2
= 0;
3767 uint8_t *bytes
= NULL
;
3769 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3774 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3776 bytes
= talloc_array(state
, uint8_t, 1);
3777 if (tevent_req_nomem(bytes
, req
)) {
3778 return tevent_req_post(req
, ev
);
3781 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3782 strlen(fname
)+1, NULL
);
3784 if (tevent_req_nomem(bytes
, req
)) {
3785 return tevent_req_post(req
, ev
);
3788 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
3789 additional_flags2
= FLAGS2_REPARSE_PATH
;
3792 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3794 0, NULL
, talloc_get_size(bytes
), bytes
);
3795 if (tevent_req_nomem(subreq
, req
)) {
3796 return tevent_req_post(req
, ev
);
3798 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3802 static void cli_getatr_done(struct tevent_req
*subreq
)
3804 struct tevent_req
*req
= tevent_req_callback_data(
3805 subreq
, struct tevent_req
);
3806 struct cli_getatr_state
*state
= tevent_req_data(
3807 req
, struct cli_getatr_state
);
3809 uint16_t *vwv
= NULL
;
3812 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3814 TALLOC_FREE(subreq
);
3815 if (tevent_req_nterror(req
, status
)) {
3819 state
->attr
= SVAL(vwv
+0,0);
3820 state
->size
= (off_t
)IVAL(vwv
+3,0);
3821 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3823 tevent_req_done(req
);
3826 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3831 struct cli_getatr_state
*state
= tevent_req_data(
3832 req
, struct cli_getatr_state
);
3835 if (tevent_req_is_nterror(req
, &status
)) {
3839 *attr
= state
->attr
;
3842 *size
= state
->size
;
3845 *write_time
= state
->write_time
;
3847 return NT_STATUS_OK
;
3850 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3856 TALLOC_CTX
*frame
= NULL
;
3857 struct tevent_context
*ev
= NULL
;
3858 struct tevent_req
*req
= NULL
;
3859 NTSTATUS status
= NT_STATUS_OK
;
3861 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3862 return cli_smb2_getatr(cli
,
3869 frame
= talloc_stackframe();
3871 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3873 * Can't use sync call while an async call is in flight
3875 status
= NT_STATUS_INVALID_PARAMETER
;
3879 ev
= samba_tevent_context_init(frame
);
3881 status
= NT_STATUS_NO_MEMORY
;
3885 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3887 status
= NT_STATUS_NO_MEMORY
;
3891 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3895 status
= cli_getatr_recv(req
,
3905 /****************************************************************************
3906 Do a SMBsetattrE call.
3907 ****************************************************************************/
3909 static void cli_setattrE_done(struct tevent_req
*subreq
);
3911 struct cli_setattrE_state
{
3915 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3916 struct tevent_context
*ev
,
3917 struct cli_state
*cli
,
3923 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3924 struct cli_setattrE_state
*state
= NULL
;
3925 uint8_t additional_flags
= 0;
3927 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3932 SSVAL(state
->vwv
+0, 0, fnum
);
3933 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3934 smb1cli_conn_server_time_zone(cli
->conn
));
3935 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3936 smb1cli_conn_server_time_zone(cli
->conn
));
3937 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3938 smb1cli_conn_server_time_zone(cli
->conn
));
3940 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
, 0,
3941 7, state
->vwv
, 0, NULL
);
3942 if (tevent_req_nomem(subreq
, req
)) {
3943 return tevent_req_post(req
, ev
);
3945 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3949 static void cli_setattrE_done(struct tevent_req
*subreq
)
3951 struct tevent_req
*req
= tevent_req_callback_data(
3952 subreq
, struct tevent_req
);
3955 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3956 TALLOC_FREE(subreq
);
3957 if (tevent_req_nterror(req
, status
)) {
3960 tevent_req_done(req
);
3963 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3965 return tevent_req_simple_recv_ntstatus(req
);
3968 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3974 TALLOC_CTX
*frame
= NULL
;
3975 struct tevent_context
*ev
= NULL
;
3976 struct tevent_req
*req
= NULL
;
3977 NTSTATUS status
= NT_STATUS_OK
;
3979 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3980 return cli_smb2_setattrE(cli
,
3987 frame
= talloc_stackframe();
3989 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3991 * Can't use sync call while an async call is in flight
3993 status
= NT_STATUS_INVALID_PARAMETER
;
3997 ev
= samba_tevent_context_init(frame
);
3999 status
= NT_STATUS_NO_MEMORY
;
4003 req
= cli_setattrE_send(frame
, ev
,
4011 status
= NT_STATUS_NO_MEMORY
;
4015 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4019 status
= cli_setattrE_recv(req
);
4026 /****************************************************************************
4027 Do a SMBsetatr call.
4028 ****************************************************************************/
4030 static void cli_setatr_done(struct tevent_req
*subreq
);
4032 struct cli_setatr_state
{
4036 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
4037 struct tevent_context
*ev
,
4038 struct cli_state
*cli
,
4043 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4044 struct cli_setatr_state
*state
= NULL
;
4045 uint8_t additional_flags
= 0;
4046 uint16_t additional_flags2
= 0;
4047 uint8_t *bytes
= NULL
;
4049 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
4054 SSVAL(state
->vwv
+0, 0, attr
);
4055 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
4057 bytes
= talloc_array(state
, uint8_t, 1);
4058 if (tevent_req_nomem(bytes
, req
)) {
4059 return tevent_req_post(req
, ev
);
4062 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4063 strlen(fname
)+1, NULL
);
4064 if (tevent_req_nomem(bytes
, req
)) {
4065 return tevent_req_post(req
, ev
);
4067 bytes
= talloc_realloc(state
, bytes
, uint8_t,
4068 talloc_get_size(bytes
)+1);
4069 if (tevent_req_nomem(bytes
, req
)) {
4070 return tevent_req_post(req
, ev
);
4073 bytes
[talloc_get_size(bytes
)-1] = 4;
4074 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
4076 if (tevent_req_nomem(bytes
, req
)) {
4077 return tevent_req_post(req
, ev
);
4080 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
4081 additional_flags2
= FLAGS2_REPARSE_PATH
;
4084 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
4086 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
4087 if (tevent_req_nomem(subreq
, req
)) {
4088 return tevent_req_post(req
, ev
);
4090 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
4094 static void cli_setatr_done(struct tevent_req
*subreq
)
4096 struct tevent_req
*req
= tevent_req_callback_data(
4097 subreq
, struct tevent_req
);
4100 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4101 TALLOC_FREE(subreq
);
4102 if (tevent_req_nterror(req
, status
)) {
4105 tevent_req_done(req
);
4108 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
4110 return tevent_req_simple_recv_ntstatus(req
);
4113 NTSTATUS
cli_setatr(struct cli_state
*cli
,
4118 TALLOC_CTX
*frame
= NULL
;
4119 struct tevent_context
*ev
= NULL
;
4120 struct tevent_req
*req
= NULL
;
4121 NTSTATUS status
= NT_STATUS_OK
;
4123 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4124 return cli_smb2_setatr(cli
,
4130 frame
= talloc_stackframe();
4132 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4134 * Can't use sync call while an async call is in flight
4136 status
= NT_STATUS_INVALID_PARAMETER
;
4140 ev
= samba_tevent_context_init(frame
);
4142 status
= NT_STATUS_NO_MEMORY
;
4146 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
4148 status
= NT_STATUS_NO_MEMORY
;
4152 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4156 status
= cli_setatr_recv(req
);
4163 /****************************************************************************
4164 Check for existence of a dir.
4165 ****************************************************************************/
4167 static void cli_chkpath_done(struct tevent_req
*subreq
);
4169 struct cli_chkpath_state
{
4173 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
4174 struct tevent_context
*ev
,
4175 struct cli_state
*cli
,
4178 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4179 struct cli_chkpath_state
*state
= NULL
;
4180 uint8_t additional_flags
= 0;
4181 uint16_t additional_flags2
= 0;
4182 uint8_t *bytes
= NULL
;
4184 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
4189 bytes
= talloc_array(state
, uint8_t, 1);
4190 if (tevent_req_nomem(bytes
, req
)) {
4191 return tevent_req_post(req
, ev
);
4194 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4195 strlen(fname
)+1, NULL
);
4197 if (tevent_req_nomem(bytes
, req
)) {
4198 return tevent_req_post(req
, ev
);
4201 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
)) {
4202 additional_flags2
= FLAGS2_REPARSE_PATH
;
4205 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
4207 0, NULL
, talloc_get_size(bytes
), bytes
);
4208 if (tevent_req_nomem(subreq
, req
)) {
4209 return tevent_req_post(req
, ev
);
4211 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
4215 static void cli_chkpath_done(struct tevent_req
*subreq
)
4217 struct tevent_req
*req
= tevent_req_callback_data(
4218 subreq
, struct tevent_req
);
4221 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4222 TALLOC_FREE(subreq
);
4223 if (tevent_req_nterror(req
, status
)) {
4226 tevent_req_done(req
);
4229 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
4231 return tevent_req_simple_recv_ntstatus(req
);
4234 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
4236 TALLOC_CTX
*frame
= NULL
;
4237 struct tevent_context
*ev
= NULL
;
4238 struct tevent_req
*req
= NULL
;
4240 NTSTATUS status
= NT_STATUS_OK
;
4242 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4243 return cli_smb2_chkpath(cli
, path
);
4246 frame
= talloc_stackframe();
4248 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4250 * Can't use sync call while an async call is in flight
4252 status
= NT_STATUS_INVALID_PARAMETER
;
4256 path2
= talloc_strdup(frame
, path
);
4258 status
= NT_STATUS_NO_MEMORY
;
4261 trim_char(path2
,'\0','\\');
4263 path2
= talloc_strdup(frame
, "\\");
4265 status
= NT_STATUS_NO_MEMORY
;
4270 ev
= samba_tevent_context_init(frame
);
4272 status
= NT_STATUS_NO_MEMORY
;
4276 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4278 status
= NT_STATUS_NO_MEMORY
;
4282 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4286 status
= cli_chkpath_recv(req
);
4293 /****************************************************************************
4295 ****************************************************************************/
4297 static void cli_dskattr_done(struct tevent_req
*subreq
);
4299 struct cli_dskattr_state
{
4305 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4306 struct tevent_context
*ev
,
4307 struct cli_state
*cli
)
4309 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4310 struct cli_dskattr_state
*state
= NULL
;
4311 uint8_t additional_flags
= 0;
4313 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4318 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
, 0,
4320 if (tevent_req_nomem(subreq
, req
)) {
4321 return tevent_req_post(req
, ev
);
4323 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4327 static void cli_dskattr_done(struct tevent_req
*subreq
)
4329 struct tevent_req
*req
= tevent_req_callback_data(
4330 subreq
, struct tevent_req
);
4331 struct cli_dskattr_state
*state
= tevent_req_data(
4332 req
, struct cli_dskattr_state
);
4334 uint16_t *vwv
= NULL
;
4337 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4339 TALLOC_FREE(subreq
);
4340 if (tevent_req_nterror(req
, status
)) {
4343 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4344 state
->total
= SVAL(vwv
+0, 0);
4345 state
->avail
= SVAL(vwv
+3, 0);
4346 tevent_req_done(req
);
4349 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4351 struct cli_dskattr_state
*state
= tevent_req_data(
4352 req
, struct cli_dskattr_state
);
4355 if (tevent_req_is_nterror(req
, &status
)) {
4358 *bsize
= state
->bsize
;
4359 *total
= state
->total
;
4360 *avail
= state
->avail
;
4361 return NT_STATUS_OK
;
4364 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4366 TALLOC_CTX
*frame
= NULL
;
4367 struct tevent_context
*ev
= NULL
;
4368 struct tevent_req
*req
= NULL
;
4369 NTSTATUS status
= NT_STATUS_OK
;
4371 frame
= talloc_stackframe();
4373 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4375 * Can't use sync call while an async call is in flight
4377 status
= NT_STATUS_INVALID_PARAMETER
;
4381 ev
= samba_tevent_context_init(frame
);
4383 status
= NT_STATUS_NO_MEMORY
;
4387 req
= cli_dskattr_send(frame
, ev
, cli
);
4389 status
= NT_STATUS_NO_MEMORY
;
4393 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4397 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4404 NTSTATUS
cli_disk_size(struct cli_state
*cli
, const char *path
, uint64_t *bsize
,
4405 uint64_t *total
, uint64_t *avail
)
4407 uint64_t sectors_per_block
;
4408 uint64_t bytes_per_sector
;
4409 int old_bsize
, old_total
, old_avail
;
4412 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4413 return cli_smb2_dskattr(cli
, path
, bsize
, total
, avail
);
4417 * Try the trans2 disk full size info call first.
4418 * We already use this in SMBC_fstatvfs_ctx().
4419 * Ignore 'actual_available_units' as we only
4420 * care about the quota for the caller.
4423 status
= cli_get_fs_full_size_info(cli
,
4430 /* Try and cope will all varients of "we don't do this call"
4431 and fall back to cli_dskattr. */
4433 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
4434 NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
) ||
4435 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
4436 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
4437 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
4438 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
4439 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
4440 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
4441 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
4442 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
4446 if (!NT_STATUS_IS_OK(status
)) {
4451 *bsize
= sectors_per_block
*
4455 return NT_STATUS_OK
;
4459 /* Old SMB1 core protocol fallback. */
4460 status
= cli_dskattr(cli
, &old_bsize
, &old_total
, &old_avail
);
4461 if (!NT_STATUS_IS_OK(status
)) {
4465 *bsize
= (uint64_t)old_bsize
;
4468 *total
= (uint64_t)old_total
;
4471 *avail
= (uint64_t)old_avail
;
4473 return NT_STATUS_OK
;
4476 /****************************************************************************
4477 Create and open a temporary file.
4478 ****************************************************************************/
4480 static void cli_ctemp_done(struct tevent_req
*subreq
);
4482 struct ctemp_state
{
4488 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4489 struct tevent_context
*ev
,
4490 struct cli_state
*cli
,
4493 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4494 struct ctemp_state
*state
= NULL
;
4495 uint8_t additional_flags
= 0;
4496 uint16_t additional_flags2
= 0;
4497 uint8_t *bytes
= NULL
;
4499 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4504 SSVAL(state
->vwv
,0,0);
4505 SIVALS(state
->vwv
+1,0,-1);
4507 bytes
= talloc_array(state
, uint8_t, 1);
4508 if (tevent_req_nomem(bytes
, req
)) {
4509 return tevent_req_post(req
, ev
);
4512 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4513 strlen(path
)+1, NULL
);
4514 if (tevent_req_nomem(bytes
, req
)) {
4515 return tevent_req_post(req
, ev
);
4518 if (clistr_is_previous_version_path(path
, NULL
, NULL
, NULL
)) {
4519 additional_flags2
= FLAGS2_REPARSE_PATH
;
4522 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4524 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4525 if (tevent_req_nomem(subreq
, req
)) {
4526 return tevent_req_post(req
, ev
);
4528 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4532 static void cli_ctemp_done(struct tevent_req
*subreq
)
4534 struct tevent_req
*req
= tevent_req_callback_data(
4535 subreq
, struct tevent_req
);
4536 struct ctemp_state
*state
= tevent_req_data(
4537 req
, struct ctemp_state
);
4541 uint32_t num_bytes
= 0;
4542 uint8_t *bytes
= NULL
;
4544 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4545 &num_bytes
, &bytes
);
4546 TALLOC_FREE(subreq
);
4547 if (tevent_req_nterror(req
, status
)) {
4551 state
->fnum
= SVAL(vwv
+0, 0);
4553 /* From W2K3, the result is just the ASCII name */
4554 if (num_bytes
< 2) {
4555 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4559 if (pull_string_talloc(state
,
4566 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4569 tevent_req_done(req
);
4572 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4577 struct ctemp_state
*state
= tevent_req_data(req
,
4578 struct ctemp_state
);
4581 if (tevent_req_is_nterror(req
, &status
)) {
4584 *pfnum
= state
->fnum
;
4585 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4587 return NT_STATUS_NO_MEMORY
;
4589 return NT_STATUS_OK
;
4592 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4598 TALLOC_CTX
*frame
= talloc_stackframe();
4599 struct tevent_context
*ev
;
4600 struct tevent_req
*req
;
4601 NTSTATUS status
= NT_STATUS_OK
;
4603 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4605 * Can't use sync call while an async call is in flight
4607 status
= NT_STATUS_INVALID_PARAMETER
;
4611 ev
= samba_tevent_context_init(frame
);
4613 status
= NT_STATUS_NO_MEMORY
;
4617 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4619 status
= NT_STATUS_NO_MEMORY
;
4623 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4627 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4635 send a raw ioctl - used by the torture code
4637 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4642 SSVAL(vwv
+0, 0, fnum
);
4643 SSVAL(vwv
+1, 0, code
>>16);
4644 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4646 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4647 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4648 if (!NT_STATUS_IS_OK(status
)) {
4651 *blob
= data_blob_null
;
4652 return NT_STATUS_OK
;
4655 /*********************************************************
4656 Set an extended attribute utility fn.
4657 *********************************************************/
4659 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4660 uint8_t *param
, unsigned int param_len
,
4661 const char *ea_name
,
4662 const char *ea_val
, size_t ea_len
)
4665 unsigned int data_len
= 0;
4666 uint8_t *data
= NULL
;
4668 size_t ea_namelen
= strlen(ea_name
);
4671 SSVAL(setup
, 0, setup_val
);
4673 if (ea_namelen
== 0 && ea_len
== 0) {
4675 data
= talloc_array(talloc_tos(),
4679 return NT_STATUS_NO_MEMORY
;
4682 SIVAL(p
,0,data_len
);
4684 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4685 data
= talloc_array(talloc_tos(),
4689 return NT_STATUS_NO_MEMORY
;
4692 SIVAL(p
,0,data_len
);
4694 SCVAL(p
, 0, 0); /* EA flags. */
4695 SCVAL(p
, 1, ea_namelen
);
4696 SSVAL(p
, 2, ea_len
);
4697 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4698 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4702 * FIXME - if we want to do previous version path
4703 * processing on an EA set call we need to turn this
4704 * into calls to cli_trans_send()/cli_trans_recv()
4705 * with a temporary event context, as cli_trans_send()
4706 * have access to the additional_flags2 needed to
4707 * send @GMT- paths. JRA.
4710 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4712 param
, param_len
, 2,
4715 NULL
, 0, NULL
, /* rsetup */
4716 NULL
, 0, NULL
, /* rparam */
4717 NULL
, 0, NULL
); /* rdata */
4722 /*********************************************************
4723 Set an extended attribute on a pathname.
4724 *********************************************************/
4726 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4727 const char *ea_name
, const char *ea_val
,
4730 unsigned int param_len
= 0;
4733 TALLOC_CTX
*frame
= NULL
;
4735 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4736 return cli_smb2_set_ea_path(cli
,
4743 frame
= talloc_stackframe();
4745 param
= talloc_array(frame
, uint8_t, 6);
4747 status
= NT_STATUS_NO_MEMORY
;
4750 SSVAL(param
,0,SMB_INFO_SET_EA
);
4754 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4755 path
, strlen(path
)+1,
4757 param_len
= talloc_get_size(param
);
4759 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4760 ea_name
, ea_val
, ea_len
);
4768 /*********************************************************
4769 Set an extended attribute on an fnum.
4770 *********************************************************/
4772 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4773 const char *ea_name
, const char *ea_val
,
4778 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4779 return cli_smb2_set_ea_fnum(cli
,
4786 memset(param
, 0, 6);
4787 SSVAL(param
,0,fnum
);
4788 SSVAL(param
,2,SMB_INFO_SET_EA
);
4790 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4791 ea_name
, ea_val
, ea_len
);
4794 /*********************************************************
4795 Get an extended attribute list utility fn.
4796 *********************************************************/
4798 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4800 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4802 struct ea_struct
*ea_list
= NULL
;
4807 if (rdata_len
< 4) {
4811 ea_size
= (size_t)IVAL(rdata
,0);
4812 if (ea_size
> rdata_len
) {
4817 /* No EA's present. */
4826 /* Validate the EA list and count it. */
4827 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4828 unsigned int ea_namelen
= CVAL(p
,1);
4829 unsigned int ea_valuelen
= SVAL(p
,2);
4830 if (ea_namelen
== 0) {
4833 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4836 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4837 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4846 *pnum_eas
= num_eas
;
4848 /* Caller only wants number of EA's. */
4852 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4857 ea_size
= (size_t)IVAL(rdata
,0);
4860 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4861 struct ea_struct
*ea
= &ea_list
[num_eas
];
4862 fstring unix_ea_name
;
4863 unsigned int ea_namelen
= CVAL(p
,1);
4864 unsigned int ea_valuelen
= SVAL(p
,2);
4866 ea
->flags
= CVAL(p
,0);
4867 unix_ea_name
[0] = '\0';
4868 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4869 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4873 /* Ensure the value is null terminated (in case it's a string). */
4874 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4875 if (!ea
->value
.data
) {
4879 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4881 ea
->value
.data
[ea_valuelen
] = 0;
4883 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4886 *pea_list
= ea_list
;
4890 TALLOC_FREE(ea_list
);
4894 /*********************************************************
4895 Get an extended attribute list from a pathname.
4896 *********************************************************/
4898 struct cli_get_ea_list_path_state
{
4903 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4905 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4906 struct tevent_context
*ev
,
4907 struct cli_state
*cli
,
4910 struct tevent_req
*req
, *subreq
;
4911 struct cli_get_ea_list_path_state
*state
;
4913 req
= tevent_req_create(mem_ctx
, &state
,
4914 struct cli_get_ea_list_path_state
);
4918 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4919 SMB_INFO_QUERY_ALL_EAS
, 4,
4921 if (tevent_req_nomem(subreq
, req
)) {
4922 return tevent_req_post(req
, ev
);
4924 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4928 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4930 struct tevent_req
*req
= tevent_req_callback_data(
4931 subreq
, struct tevent_req
);
4932 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4933 req
, struct cli_get_ea_list_path_state
);
4936 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4938 TALLOC_FREE(subreq
);
4939 if (tevent_req_nterror(req
, status
)) {
4942 tevent_req_done(req
);
4945 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4946 size_t *pnum_eas
, struct ea_struct
**peas
)
4948 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4949 req
, struct cli_get_ea_list_path_state
);
4952 if (tevent_req_is_nterror(req
, &status
)) {
4955 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4957 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4959 return NT_STATUS_OK
;
4962 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4965 struct ea_struct
**pea_list
)
4967 TALLOC_CTX
*frame
= NULL
;
4968 struct tevent_context
*ev
= NULL
;
4969 struct tevent_req
*req
= NULL
;
4970 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4972 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4973 return cli_smb2_get_ea_list_path(cli
,
4980 frame
= talloc_stackframe();
4982 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4984 * Can't use sync call while an async call is in flight
4986 status
= NT_STATUS_INVALID_PARAMETER
;
4989 ev
= samba_tevent_context_init(frame
);
4993 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4997 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5000 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
5006 /****************************************************************************
5007 Convert open "flags" arg to uint32_t on wire.
5008 ****************************************************************************/
5010 static uint32_t open_flags_to_wire(int flags
)
5012 int open_mode
= flags
& O_ACCMODE
;
5015 switch (open_mode
) {
5017 ret
|= SMB_O_WRONLY
;
5024 ret
|= SMB_O_RDONLY
;
5028 if (flags
& O_CREAT
) {
5031 if (flags
& O_EXCL
) {
5034 if (flags
& O_TRUNC
) {
5038 if (flags
& O_SYNC
) {
5042 if (flags
& O_APPEND
) {
5043 ret
|= SMB_O_APPEND
;
5045 #if defined(O_DIRECT)
5046 if (flags
& O_DIRECT
) {
5047 ret
|= SMB_O_DIRECT
;
5050 #if defined(O_DIRECTORY)
5051 if (flags
& O_DIRECTORY
) {
5052 ret
|= SMB_O_DIRECTORY
;
5058 /****************************************************************************
5059 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5060 ****************************************************************************/
5062 struct posix_open_state
{
5066 uint16_t fnum
; /* Out */
5069 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
5071 struct tevent_req
*req
= tevent_req_callback_data(
5072 subreq
, struct tevent_req
);
5073 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
5078 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5079 NULL
, 0, NULL
, &data
, 12, &num_data
);
5080 TALLOC_FREE(subreq
);
5081 if (tevent_req_nterror(req
, status
)) {
5084 state
->fnum
= SVAL(data
,2);
5085 tevent_req_done(req
);
5088 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
5089 struct tevent_context
*ev
,
5090 struct cli_state
*cli
,
5096 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5097 struct posix_open_state
*state
= NULL
;
5098 uint32_t wire_flags
= open_flags_to_wire(flags
);
5100 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
5105 /* Setup setup word. */
5106 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
5108 /* Setup param array. */
5109 state
->param
= talloc_array(state
, uint8_t, 6);
5110 if (tevent_req_nomem(state
->param
, req
)) {
5111 return tevent_req_post(req
, ev
);
5113 memset(state
->param
, '\0', 6);
5114 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
5116 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
5117 strlen(fname
)+1, NULL
);
5119 if (tevent_req_nomem(state
->param
, req
)) {
5120 return tevent_req_post(req
, ev
);
5123 /* Setup data words. */
5125 wire_flags
|= SMB_O_DIRECTORY
;
5128 SIVAL(state
->data
,0,0); /* No oplock. */
5129 SIVAL(state
->data
,4,wire_flags
);
5130 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
5131 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
5132 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
5134 subreq
= cli_trans_send(state
, /* mem ctx. */
5135 ev
, /* event ctx. */
5136 cli
, /* cli_state. */
5137 0, /* additional_flags2 */
5138 SMBtrans2
, /* cmd. */
5139 NULL
, /* pipe name. */
5143 &state
->setup
, /* setup. */
5144 1, /* num setup uint16_t words. */
5145 0, /* max returned setup. */
5146 state
->param
, /* param. */
5147 talloc_get_size(state
->param
),/* num param. */
5148 2, /* max returned param. */
5149 state
->data
, /* data. */
5151 12); /* max returned data. */
5153 if (tevent_req_nomem(subreq
, req
)) {
5154 return tevent_req_post(req
, ev
);
5156 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
5160 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
5161 struct tevent_context
*ev
,
5162 struct cli_state
*cli
,
5167 return cli_posix_open_internal_send(mem_ctx
, ev
,
5168 cli
, fname
, flags
, mode
, false);
5171 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
5173 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
5176 if (tevent_req_is_nterror(req
, &status
)) {
5179 *pfnum
= state
->fnum
;
5180 return NT_STATUS_OK
;
5183 /****************************************************************************
5184 Open - POSIX semantics. Doesn't request oplock.
5185 ****************************************************************************/
5187 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
5188 int flags
, mode_t mode
, uint16_t *pfnum
)
5191 TALLOC_CTX
*frame
= talloc_stackframe();
5192 struct tevent_context
*ev
= NULL
;
5193 struct tevent_req
*req
= NULL
;
5194 NTSTATUS status
= NT_STATUS_OK
;
5196 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5198 * Can't use sync call while an async call is in flight
5200 status
= NT_STATUS_INVALID_PARAMETER
;
5204 ev
= samba_tevent_context_init(frame
);
5206 status
= NT_STATUS_NO_MEMORY
;
5210 req
= cli_posix_open_send(frame
,
5217 status
= NT_STATUS_NO_MEMORY
;
5221 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5225 status
= cli_posix_open_recv(req
, pfnum
);
5232 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5233 struct tevent_context
*ev
,
5234 struct cli_state
*cli
,
5238 return cli_posix_open_internal_send(mem_ctx
, ev
,
5239 cli
, fname
, O_CREAT
, mode
, true);
5242 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5244 return tevent_req_simple_recv_ntstatus(req
);
5247 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5249 TALLOC_CTX
*frame
= talloc_stackframe();
5250 struct tevent_context
*ev
= NULL
;
5251 struct tevent_req
*req
= NULL
;
5252 NTSTATUS status
= NT_STATUS_OK
;
5254 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5256 * Can't use sync call while an async call is in flight
5258 status
= NT_STATUS_INVALID_PARAMETER
;
5262 ev
= samba_tevent_context_init(frame
);
5264 status
= NT_STATUS_NO_MEMORY
;
5268 req
= cli_posix_mkdir_send(frame
,
5274 status
= NT_STATUS_NO_MEMORY
;
5278 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5282 status
= cli_posix_mkdir_recv(req
);
5289 /****************************************************************************
5290 unlink or rmdir - POSIX semantics.
5291 ****************************************************************************/
5293 struct cli_posix_unlink_internal_state
{
5297 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5299 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5300 struct tevent_context
*ev
,
5301 struct cli_state
*cli
,
5305 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5306 struct cli_posix_unlink_internal_state
*state
= NULL
;
5308 req
= tevent_req_create(mem_ctx
, &state
,
5309 struct cli_posix_unlink_internal_state
);
5314 /* Setup data word. */
5315 SSVAL(state
->data
, 0, level
);
5317 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5318 SMB_POSIX_PATH_UNLINK
,
5320 state
->data
, sizeof(state
->data
));
5321 if (tevent_req_nomem(subreq
, req
)) {
5322 return tevent_req_post(req
, ev
);
5324 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5328 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5330 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5331 tevent_req_simple_finish_ntstatus(subreq
, status
);
5334 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5335 struct tevent_context
*ev
,
5336 struct cli_state
*cli
,
5339 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5340 SMB_POSIX_UNLINK_FILE_TARGET
);
5343 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5345 return tevent_req_simple_recv_ntstatus(req
);
5348 /****************************************************************************
5349 unlink - POSIX semantics.
5350 ****************************************************************************/
5352 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5354 TALLOC_CTX
*frame
= talloc_stackframe();
5355 struct tevent_context
*ev
= NULL
;
5356 struct tevent_req
*req
= NULL
;
5357 NTSTATUS status
= NT_STATUS_OK
;
5359 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5361 * Can't use sync call while an async call is in flight
5363 status
= NT_STATUS_INVALID_PARAMETER
;
5367 ev
= samba_tevent_context_init(frame
);
5369 status
= NT_STATUS_NO_MEMORY
;
5373 req
= cli_posix_unlink_send(frame
,
5378 status
= NT_STATUS_NO_MEMORY
;
5382 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5386 status
= cli_posix_unlink_recv(req
);
5393 /****************************************************************************
5394 rmdir - POSIX semantics.
5395 ****************************************************************************/
5397 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5398 struct tevent_context
*ev
,
5399 struct cli_state
*cli
,
5402 return cli_posix_unlink_internal_send(
5403 mem_ctx
, ev
, cli
, fname
,
5404 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5407 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5409 return tevent_req_simple_recv_ntstatus(req
);
5412 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5414 TALLOC_CTX
*frame
= talloc_stackframe();
5415 struct tevent_context
*ev
= NULL
;
5416 struct tevent_req
*req
= NULL
;
5417 NTSTATUS status
= NT_STATUS_OK
;
5419 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5421 * Can't use sync call while an async call is in flight
5423 status
= NT_STATUS_INVALID_PARAMETER
;
5427 ev
= samba_tevent_context_init(frame
);
5429 status
= NT_STATUS_NO_MEMORY
;
5433 req
= cli_posix_rmdir_send(frame
,
5438 status
= NT_STATUS_NO_MEMORY
;
5442 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5446 status
= cli_posix_rmdir_recv(req
, frame
);
5453 /****************************************************************************
5455 ****************************************************************************/
5457 struct cli_notify_state
{
5459 uint32_t num_changes
;
5460 struct notify_change
*changes
;
5463 static void cli_notify_done(struct tevent_req
*subreq
);
5465 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5466 struct tevent_context
*ev
,
5467 struct cli_state
*cli
, uint16_t fnum
,
5468 uint32_t buffer_size
,
5469 uint32_t completion_filter
, bool recursive
)
5471 struct tevent_req
*req
, *subreq
;
5472 struct cli_notify_state
*state
;
5473 unsigned old_timeout
;
5475 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5480 SIVAL(state
->setup
, 0, completion_filter
);
5481 SSVAL(state
->setup
, 4, fnum
);
5482 SSVAL(state
->setup
, 6, recursive
);
5485 * Notifies should not time out
5487 old_timeout
= cli_set_timeout(cli
, 0);
5489 subreq
= cli_trans_send(
5490 state
, /* mem ctx. */
5491 ev
, /* event ctx. */
5492 cli
, /* cli_state. */
5493 0, /* additional_flags2 */
5494 SMBnttrans
, /* cmd. */
5495 NULL
, /* pipe name. */
5497 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5499 (uint16_t *)state
->setup
, /* setup. */
5500 4, /* num setup uint16_t words. */
5501 0, /* max returned setup. */
5504 buffer_size
, /* max returned param. */
5507 0); /* max returned data. */
5509 cli_set_timeout(cli
, old_timeout
);
5511 if (tevent_req_nomem(subreq
, req
)) {
5512 return tevent_req_post(req
, ev
);
5514 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5518 static void cli_notify_done(struct tevent_req
*subreq
)
5520 struct tevent_req
*req
= tevent_req_callback_data(
5521 subreq
, struct tevent_req
);
5522 struct cli_notify_state
*state
= tevent_req_data(
5523 req
, struct cli_notify_state
);
5526 uint32_t i
, ofs
, num_params
;
5529 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5530 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5531 TALLOC_FREE(subreq
);
5532 if (tevent_req_nterror(req
, status
)) {
5533 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5537 state
->num_changes
= 0;
5540 while (num_params
- ofs
> 12) {
5541 uint32_t next
= IVAL(params
, ofs
);
5542 state
->num_changes
+= 1;
5544 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5550 state
->changes
= talloc_array(state
, struct notify_change
,
5551 state
->num_changes
);
5552 if (tevent_req_nomem(state
->changes
, req
)) {
5553 TALLOC_FREE(params
);
5559 for (i
=0; i
<state
->num_changes
; i
++) {
5560 uint32_t next
= IVAL(params
, ofs
);
5561 uint32_t len
= IVAL(params
, ofs
+8);
5565 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5566 TALLOC_FREE(params
);
5568 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5572 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5573 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5574 &name
, params
+ofs
+12, len
,
5575 STR_TERMINATE
|STR_UNICODE
);
5577 TALLOC_FREE(params
);
5578 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5581 state
->changes
[i
].name
= name
;
5585 TALLOC_FREE(params
);
5586 tevent_req_done(req
);
5589 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5590 uint32_t *pnum_changes
,
5591 struct notify_change
**pchanges
)
5593 struct cli_notify_state
*state
= tevent_req_data(
5594 req
, struct cli_notify_state
);
5597 if (tevent_req_is_nterror(req
, &status
)) {
5601 *pnum_changes
= state
->num_changes
;
5602 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5603 return NT_STATUS_OK
;
5606 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5607 uint32_t completion_filter
, bool recursive
,
5608 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5609 struct notify_change
**pchanges
)
5612 struct tevent_context
*ev
;
5613 struct tevent_req
*req
;
5614 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5616 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
5617 return cli_smb2_notify(cli
, fnum
, buffer_size
,
5618 completion_filter
, recursive
,
5619 mem_ctx
, pchanges
, pnum_changes
);
5622 frame
= talloc_stackframe();
5624 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5626 * Can't use sync call while an async call is in flight
5628 status
= NT_STATUS_INVALID_PARAMETER
;
5631 ev
= samba_tevent_context_init(frame
);
5635 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5636 completion_filter
, recursive
);
5640 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5643 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5649 struct cli_qpathinfo_state
{
5658 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5660 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5661 struct tevent_context
*ev
,
5662 struct cli_state
*cli
, const char *fname
,
5663 uint16_t level
, uint32_t min_rdata
,
5666 struct tevent_req
*req
, *subreq
;
5667 struct cli_qpathinfo_state
*state
;
5668 uint16_t additional_flags2
= 0;
5670 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5674 state
->min_rdata
= min_rdata
;
5675 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5677 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5678 if (tevent_req_nomem(state
->param
, req
)) {
5679 return tevent_req_post(req
, ev
);
5681 SSVAL(state
->param
, 0, level
);
5682 state
->param
= trans2_bytes_push_str(
5683 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5684 if (tevent_req_nomem(state
->param
, req
)) {
5685 return tevent_req_post(req
, ev
);
5688 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
) &&
5689 !INFO_LEVEL_IS_UNIX(level
)) {
5690 additional_flags2
= FLAGS2_REPARSE_PATH
;
5693 subreq
= cli_trans_send(
5694 state
, /* mem ctx. */
5695 ev
, /* event ctx. */
5696 cli
, /* cli_state. */
5697 additional_flags2
, /* additional_flags2 */
5698 SMBtrans2
, /* cmd. */
5699 NULL
, /* pipe name. */
5703 state
->setup
, /* setup. */
5704 1, /* num setup uint16_t words. */
5705 0, /* max returned setup. */
5706 state
->param
, /* param. */
5707 talloc_get_size(state
->param
), /* num param. */
5708 2, /* max returned param. */
5711 max_rdata
); /* max returned data. */
5713 if (tevent_req_nomem(subreq
, req
)) {
5714 return tevent_req_post(req
, ev
);
5716 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5720 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5722 struct tevent_req
*req
= tevent_req_callback_data(
5723 subreq
, struct tevent_req
);
5724 struct cli_qpathinfo_state
*state
= tevent_req_data(
5725 req
, struct cli_qpathinfo_state
);
5728 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5730 &state
->rdata
, state
->min_rdata
,
5732 if (tevent_req_nterror(req
, status
)) {
5735 tevent_req_done(req
);
5738 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5739 uint8_t **rdata
, uint32_t *num_rdata
)
5741 struct cli_qpathinfo_state
*state
= tevent_req_data(
5742 req
, struct cli_qpathinfo_state
);
5745 if (tevent_req_is_nterror(req
, &status
)) {
5748 if (rdata
!= NULL
) {
5749 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5751 TALLOC_FREE(state
->rdata
);
5753 if (num_rdata
!= NULL
) {
5754 *num_rdata
= state
->num_rdata
;
5756 return NT_STATUS_OK
;
5759 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5760 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5762 uint8_t **rdata
, uint32_t *num_rdata
)
5764 TALLOC_CTX
*frame
= talloc_stackframe();
5765 struct tevent_context
*ev
;
5766 struct tevent_req
*req
;
5767 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5769 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5771 * Can't use sync call while an async call is in flight
5773 status
= NT_STATUS_INVALID_PARAMETER
;
5776 ev
= samba_tevent_context_init(frame
);
5780 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5785 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5788 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5794 struct cli_qfileinfo_state
{
5798 uint16_t recv_flags2
;
5804 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5806 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5807 struct tevent_context
*ev
,
5808 struct cli_state
*cli
, uint16_t fnum
,
5809 uint16_t level
, uint32_t min_rdata
,
5812 struct tevent_req
*req
, *subreq
;
5813 struct cli_qfileinfo_state
*state
;
5815 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5819 state
->min_rdata
= min_rdata
;
5820 SSVAL(state
->param
, 0, fnum
);
5821 SSVAL(state
->param
, 2, level
);
5822 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5824 subreq
= cli_trans_send(
5825 state
, /* mem ctx. */
5826 ev
, /* event ctx. */
5827 cli
, /* cli_state. */
5828 0, /* additional_flags2 */
5829 SMBtrans2
, /* cmd. */
5830 NULL
, /* pipe name. */
5834 state
->setup
, /* setup. */
5835 1, /* num setup uint16_t words. */
5836 0, /* max returned setup. */
5837 state
->param
, /* param. */
5838 sizeof(state
->param
), /* num param. */
5839 2, /* max returned param. */
5842 max_rdata
); /* max returned data. */
5844 if (tevent_req_nomem(subreq
, req
)) {
5845 return tevent_req_post(req
, ev
);
5847 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5851 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5853 struct tevent_req
*req
= tevent_req_callback_data(
5854 subreq
, struct tevent_req
);
5855 struct cli_qfileinfo_state
*state
= tevent_req_data(
5856 req
, struct cli_qfileinfo_state
);
5859 status
= cli_trans_recv(subreq
, state
,
5860 &state
->recv_flags2
,
5863 &state
->rdata
, state
->min_rdata
,
5865 if (tevent_req_nterror(req
, status
)) {
5868 tevent_req_done(req
);
5871 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5872 uint16_t *recv_flags2
,
5873 uint8_t **rdata
, uint32_t *num_rdata
)
5875 struct cli_qfileinfo_state
*state
= tevent_req_data(
5876 req
, struct cli_qfileinfo_state
);
5879 if (tevent_req_is_nterror(req
, &status
)) {
5883 if (recv_flags2
!= NULL
) {
5884 *recv_flags2
= state
->recv_flags2
;
5886 if (rdata
!= NULL
) {
5887 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5889 TALLOC_FREE(state
->rdata
);
5891 if (num_rdata
!= NULL
) {
5892 *num_rdata
= state
->num_rdata
;
5894 return NT_STATUS_OK
;
5897 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5898 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5899 uint32_t max_rdata
, uint16_t *recv_flags2
,
5900 uint8_t **rdata
, uint32_t *num_rdata
)
5902 TALLOC_CTX
*frame
= talloc_stackframe();
5903 struct tevent_context
*ev
;
5904 struct tevent_req
*req
;
5905 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5907 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5909 * Can't use sync call while an async call is in flight
5911 status
= NT_STATUS_INVALID_PARAMETER
;
5914 ev
= samba_tevent_context_init(frame
);
5918 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5923 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5926 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5932 struct cli_flush_state
{
5936 static void cli_flush_done(struct tevent_req
*subreq
);
5938 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5939 struct tevent_context
*ev
,
5940 struct cli_state
*cli
,
5943 struct tevent_req
*req
, *subreq
;
5944 struct cli_flush_state
*state
;
5946 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5950 SSVAL(state
->vwv
+ 0, 0, fnum
);
5952 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 0, 1, state
->vwv
,
5954 if (tevent_req_nomem(subreq
, req
)) {
5955 return tevent_req_post(req
, ev
);
5957 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5961 static void cli_flush_done(struct tevent_req
*subreq
)
5963 struct tevent_req
*req
= tevent_req_callback_data(
5964 subreq
, struct tevent_req
);
5967 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5968 TALLOC_FREE(subreq
);
5969 if (tevent_req_nterror(req
, status
)) {
5972 tevent_req_done(req
);
5975 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5977 return tevent_req_simple_recv_ntstatus(req
);
5980 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5982 TALLOC_CTX
*frame
= talloc_stackframe();
5983 struct tevent_context
*ev
;
5984 struct tevent_req
*req
;
5985 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5987 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5989 * Can't use sync call while an async call is in flight
5991 status
= NT_STATUS_INVALID_PARAMETER
;
5994 ev
= samba_tevent_context_init(frame
);
5998 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
6002 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
6005 status
= cli_flush_recv(req
);
6011 struct cli_shadow_copy_data_state
{
6018 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
6020 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
6021 struct tevent_context
*ev
,
6022 struct cli_state
*cli
,
6026 struct tevent_req
*req
, *subreq
;
6027 struct cli_shadow_copy_data_state
*state
;
6030 req
= tevent_req_create(mem_ctx
, &state
,
6031 struct cli_shadow_copy_data_state
);
6035 state
->get_names
= get_names
;
6036 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
6038 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
6039 SSVAL(state
->setup
+ 2, 0, fnum
);
6040 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
6041 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
6043 subreq
= cli_trans_send(
6044 state
, ev
, cli
, 0, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
6045 state
->setup
, ARRAY_SIZE(state
->setup
),
6046 ARRAY_SIZE(state
->setup
),
6049 if (tevent_req_nomem(subreq
, req
)) {
6050 return tevent_req_post(req
, ev
);
6052 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
6056 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
6058 struct tevent_req
*req
= tevent_req_callback_data(
6059 subreq
, struct tevent_req
);
6060 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
6061 req
, struct cli_shadow_copy_data_state
);
6064 status
= cli_trans_recv(subreq
, state
, NULL
,
6065 NULL
, 0, NULL
, /* setup */
6066 NULL
, 0, NULL
, /* param */
6067 &state
->data
, 12, &state
->num_data
);
6068 TALLOC_FREE(subreq
);
6069 if (tevent_req_nterror(req
, status
)) {
6072 tevent_req_done(req
);
6075 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
6076 char ***pnames
, int *pnum_names
)
6078 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
6079 req
, struct cli_shadow_copy_data_state
);
6080 char **names
= NULL
;
6081 uint32_t i
, num_names
;
6083 uint8_t *endp
= NULL
;
6086 if (tevent_req_is_nterror(req
, &status
)) {
6090 if (state
->num_data
< 16) {
6091 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6094 num_names
= IVAL(state
->data
, 4);
6095 dlength
= IVAL(state
->data
, 8);
6097 if (num_names
> 0x7FFFFFFF) {
6098 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6101 if (!state
->get_names
) {
6102 *pnum_names
= (int)num_names
;
6103 return NT_STATUS_OK
;
6106 if (dlength
+ 12 < 12) {
6107 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6109 if (dlength
+ 12 > state
->num_data
) {
6110 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6112 if (state
->num_data
+ (2 * sizeof(SHADOW_COPY_LABEL
)) <
6114 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6117 names
= talloc_array(mem_ctx
, char *, num_names
);
6118 if (names
== NULL
) {
6119 return NT_STATUS_NO_MEMORY
;
6122 endp
= state
->data
+ state
->num_data
;
6124 for (i
=0; i
<num_names
; i
++) {
6127 size_t converted_size
;
6129 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
6131 if (src
+ (2 * sizeof(SHADOW_COPY_LABEL
)) > endp
) {
6132 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6135 ret
= convert_string_talloc(
6136 names
, CH_UTF16LE
, CH_UNIX
,
6137 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
6138 &names
[i
], &converted_size
);
6141 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6144 *pnum_names
= (int)num_names
;
6146 return NT_STATUS_OK
;
6149 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
6150 uint16_t fnum
, bool get_names
,
6151 char ***pnames
, int *pnum_names
)
6153 TALLOC_CTX
*frame
= NULL
;
6154 struct tevent_context
*ev
;
6155 struct tevent_req
*req
;
6156 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
6158 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
6159 return cli_smb2_shadow_copy_data(mem_ctx
,
6167 frame
= talloc_stackframe();
6169 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
6171 * Can't use sync call while an async call is in flight
6173 status
= NT_STATUS_INVALID_PARAMETER
;
6176 ev
= samba_tevent_context_init(frame
);
6180 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
6184 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
6187 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);