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
= talloc_stackframe();
4237 struct tevent_context
*ev
= NULL
;
4238 struct tevent_req
*req
= NULL
;
4240 NTSTATUS status
= NT_STATUS_OK
;
4242 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4244 * Can't use sync call while an async call is in flight
4246 status
= NT_STATUS_INVALID_PARAMETER
;
4250 path2
= talloc_strdup(frame
, path
);
4252 status
= NT_STATUS_NO_MEMORY
;
4255 trim_char(path2
,'\0','\\');
4257 path2
= talloc_strdup(frame
, "\\");
4259 status
= NT_STATUS_NO_MEMORY
;
4264 ev
= samba_tevent_context_init(frame
);
4266 status
= NT_STATUS_NO_MEMORY
;
4270 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4272 status
= NT_STATUS_NO_MEMORY
;
4276 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4280 status
= cli_chkpath_recv(req
);
4287 /****************************************************************************
4289 ****************************************************************************/
4291 static void cli_dskattr_done(struct tevent_req
*subreq
);
4293 struct cli_dskattr_state
{
4299 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4300 struct tevent_context
*ev
,
4301 struct cli_state
*cli
)
4303 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4304 struct cli_dskattr_state
*state
= NULL
;
4305 uint8_t additional_flags
= 0;
4307 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4312 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
, 0,
4314 if (tevent_req_nomem(subreq
, req
)) {
4315 return tevent_req_post(req
, ev
);
4317 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4321 static void cli_dskattr_done(struct tevent_req
*subreq
)
4323 struct tevent_req
*req
= tevent_req_callback_data(
4324 subreq
, struct tevent_req
);
4325 struct cli_dskattr_state
*state
= tevent_req_data(
4326 req
, struct cli_dskattr_state
);
4328 uint16_t *vwv
= NULL
;
4331 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4333 TALLOC_FREE(subreq
);
4334 if (tevent_req_nterror(req
, status
)) {
4337 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4338 state
->total
= SVAL(vwv
+0, 0);
4339 state
->avail
= SVAL(vwv
+3, 0);
4340 tevent_req_done(req
);
4343 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4345 struct cli_dskattr_state
*state
= tevent_req_data(
4346 req
, struct cli_dskattr_state
);
4349 if (tevent_req_is_nterror(req
, &status
)) {
4352 *bsize
= state
->bsize
;
4353 *total
= state
->total
;
4354 *avail
= state
->avail
;
4355 return NT_STATUS_OK
;
4358 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4360 TALLOC_CTX
*frame
= NULL
;
4361 struct tevent_context
*ev
= NULL
;
4362 struct tevent_req
*req
= NULL
;
4363 NTSTATUS status
= NT_STATUS_OK
;
4365 frame
= talloc_stackframe();
4367 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4369 * Can't use sync call while an async call is in flight
4371 status
= NT_STATUS_INVALID_PARAMETER
;
4375 ev
= samba_tevent_context_init(frame
);
4377 status
= NT_STATUS_NO_MEMORY
;
4381 req
= cli_dskattr_send(frame
, ev
, cli
);
4383 status
= NT_STATUS_NO_MEMORY
;
4387 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4391 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4398 NTSTATUS
cli_disk_size(struct cli_state
*cli
, const char *path
, uint64_t *bsize
,
4399 uint64_t *total
, uint64_t *avail
)
4401 uint64_t sectors_per_block
;
4402 uint64_t bytes_per_sector
;
4403 int old_bsize
, old_total
, old_avail
;
4406 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4407 return cli_smb2_dskattr(cli
, path
, bsize
, total
, avail
);
4411 * Try the trans2 disk full size info call first.
4412 * We already use this in SMBC_fstatvfs_ctx().
4413 * Ignore 'actual_available_units' as we only
4414 * care about the quota for the caller.
4417 status
= cli_get_fs_full_size_info(cli
,
4424 /* Try and cope will all varients of "we don't do this call"
4425 and fall back to cli_dskattr. */
4427 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
4428 NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
) ||
4429 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
4430 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
4431 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
4432 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
4433 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
4434 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
4435 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
4436 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
4440 if (!NT_STATUS_IS_OK(status
)) {
4445 *bsize
= sectors_per_block
*
4449 return NT_STATUS_OK
;
4453 /* Old SMB1 core protocol fallback. */
4454 status
= cli_dskattr(cli
, &old_bsize
, &old_total
, &old_avail
);
4455 if (!NT_STATUS_IS_OK(status
)) {
4459 *bsize
= (uint64_t)old_bsize
;
4462 *total
= (uint64_t)old_total
;
4465 *avail
= (uint64_t)old_avail
;
4467 return NT_STATUS_OK
;
4470 /****************************************************************************
4471 Create and open a temporary file.
4472 ****************************************************************************/
4474 static void cli_ctemp_done(struct tevent_req
*subreq
);
4476 struct ctemp_state
{
4482 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4483 struct tevent_context
*ev
,
4484 struct cli_state
*cli
,
4487 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4488 struct ctemp_state
*state
= NULL
;
4489 uint8_t additional_flags
= 0;
4490 uint16_t additional_flags2
= 0;
4491 uint8_t *bytes
= NULL
;
4493 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4498 SSVAL(state
->vwv
,0,0);
4499 SIVALS(state
->vwv
+1,0,-1);
4501 bytes
= talloc_array(state
, uint8_t, 1);
4502 if (tevent_req_nomem(bytes
, req
)) {
4503 return tevent_req_post(req
, ev
);
4506 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4507 strlen(path
)+1, NULL
);
4508 if (tevent_req_nomem(bytes
, req
)) {
4509 return tevent_req_post(req
, ev
);
4512 if (clistr_is_previous_version_path(path
, NULL
, NULL
, NULL
)) {
4513 additional_flags2
= FLAGS2_REPARSE_PATH
;
4516 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4518 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4519 if (tevent_req_nomem(subreq
, req
)) {
4520 return tevent_req_post(req
, ev
);
4522 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4526 static void cli_ctemp_done(struct tevent_req
*subreq
)
4528 struct tevent_req
*req
= tevent_req_callback_data(
4529 subreq
, struct tevent_req
);
4530 struct ctemp_state
*state
= tevent_req_data(
4531 req
, struct ctemp_state
);
4535 uint32_t num_bytes
= 0;
4536 uint8_t *bytes
= NULL
;
4538 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4539 &num_bytes
, &bytes
);
4540 TALLOC_FREE(subreq
);
4541 if (tevent_req_nterror(req
, status
)) {
4545 state
->fnum
= SVAL(vwv
+0, 0);
4547 /* From W2K3, the result is just the ASCII name */
4548 if (num_bytes
< 2) {
4549 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4553 if (pull_string_talloc(state
,
4560 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4563 tevent_req_done(req
);
4566 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4571 struct ctemp_state
*state
= tevent_req_data(req
,
4572 struct ctemp_state
);
4575 if (tevent_req_is_nterror(req
, &status
)) {
4578 *pfnum
= state
->fnum
;
4579 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4581 return NT_STATUS_NO_MEMORY
;
4583 return NT_STATUS_OK
;
4586 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4592 TALLOC_CTX
*frame
= talloc_stackframe();
4593 struct tevent_context
*ev
;
4594 struct tevent_req
*req
;
4595 NTSTATUS status
= NT_STATUS_OK
;
4597 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4599 * Can't use sync call while an async call is in flight
4601 status
= NT_STATUS_INVALID_PARAMETER
;
4605 ev
= samba_tevent_context_init(frame
);
4607 status
= NT_STATUS_NO_MEMORY
;
4611 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4613 status
= NT_STATUS_NO_MEMORY
;
4617 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4621 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4629 send a raw ioctl - used by the torture code
4631 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4636 SSVAL(vwv
+0, 0, fnum
);
4637 SSVAL(vwv
+1, 0, code
>>16);
4638 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4640 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4641 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4642 if (!NT_STATUS_IS_OK(status
)) {
4645 *blob
= data_blob_null
;
4646 return NT_STATUS_OK
;
4649 /*********************************************************
4650 Set an extended attribute utility fn.
4651 *********************************************************/
4653 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4654 uint8_t *param
, unsigned int param_len
,
4655 const char *ea_name
,
4656 const char *ea_val
, size_t ea_len
)
4659 unsigned int data_len
= 0;
4660 uint8_t *data
= NULL
;
4662 size_t ea_namelen
= strlen(ea_name
);
4665 SSVAL(setup
, 0, setup_val
);
4667 if (ea_namelen
== 0 && ea_len
== 0) {
4669 data
= talloc_array(talloc_tos(),
4673 return NT_STATUS_NO_MEMORY
;
4676 SIVAL(p
,0,data_len
);
4678 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4679 data
= talloc_array(talloc_tos(),
4683 return NT_STATUS_NO_MEMORY
;
4686 SIVAL(p
,0,data_len
);
4688 SCVAL(p
, 0, 0); /* EA flags. */
4689 SCVAL(p
, 1, ea_namelen
);
4690 SSVAL(p
, 2, ea_len
);
4691 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4692 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4696 * FIXME - if we want to do previous version path
4697 * processing on an EA set call we need to turn this
4698 * into calls to cli_trans_send()/cli_trans_recv()
4699 * with a temporary event context, as cli_trans_send()
4700 * have access to the additional_flags2 needed to
4701 * send @GMT- paths. JRA.
4704 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4706 param
, param_len
, 2,
4709 NULL
, 0, NULL
, /* rsetup */
4710 NULL
, 0, NULL
, /* rparam */
4711 NULL
, 0, NULL
); /* rdata */
4716 /*********************************************************
4717 Set an extended attribute on a pathname.
4718 *********************************************************/
4720 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4721 const char *ea_name
, const char *ea_val
,
4724 unsigned int param_len
= 0;
4727 TALLOC_CTX
*frame
= NULL
;
4729 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4730 return cli_smb2_set_ea_path(cli
,
4737 frame
= talloc_stackframe();
4739 param
= talloc_array(frame
, uint8_t, 6);
4741 status
= NT_STATUS_NO_MEMORY
;
4744 SSVAL(param
,0,SMB_INFO_SET_EA
);
4748 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4749 path
, strlen(path
)+1,
4751 param_len
= talloc_get_size(param
);
4753 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4754 ea_name
, ea_val
, ea_len
);
4762 /*********************************************************
4763 Set an extended attribute on an fnum.
4764 *********************************************************/
4766 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4767 const char *ea_name
, const char *ea_val
,
4772 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4773 return cli_smb2_set_ea_fnum(cli
,
4780 memset(param
, 0, 6);
4781 SSVAL(param
,0,fnum
);
4782 SSVAL(param
,2,SMB_INFO_SET_EA
);
4784 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4785 ea_name
, ea_val
, ea_len
);
4788 /*********************************************************
4789 Get an extended attribute list utility fn.
4790 *********************************************************/
4792 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4794 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4796 struct ea_struct
*ea_list
= NULL
;
4801 if (rdata_len
< 4) {
4805 ea_size
= (size_t)IVAL(rdata
,0);
4806 if (ea_size
> rdata_len
) {
4811 /* No EA's present. */
4820 /* Validate the EA list and count it. */
4821 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4822 unsigned int ea_namelen
= CVAL(p
,1);
4823 unsigned int ea_valuelen
= SVAL(p
,2);
4824 if (ea_namelen
== 0) {
4827 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4830 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4831 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4840 *pnum_eas
= num_eas
;
4842 /* Caller only wants number of EA's. */
4846 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4851 ea_size
= (size_t)IVAL(rdata
,0);
4854 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4855 struct ea_struct
*ea
= &ea_list
[num_eas
];
4856 fstring unix_ea_name
;
4857 unsigned int ea_namelen
= CVAL(p
,1);
4858 unsigned int ea_valuelen
= SVAL(p
,2);
4860 ea
->flags
= CVAL(p
,0);
4861 unix_ea_name
[0] = '\0';
4862 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4863 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4867 /* Ensure the value is null terminated (in case it's a string). */
4868 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4869 if (!ea
->value
.data
) {
4873 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4875 ea
->value
.data
[ea_valuelen
] = 0;
4877 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4880 *pea_list
= ea_list
;
4884 TALLOC_FREE(ea_list
);
4888 /*********************************************************
4889 Get an extended attribute list from a pathname.
4890 *********************************************************/
4892 struct cli_get_ea_list_path_state
{
4897 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4899 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4900 struct tevent_context
*ev
,
4901 struct cli_state
*cli
,
4904 struct tevent_req
*req
, *subreq
;
4905 struct cli_get_ea_list_path_state
*state
;
4907 req
= tevent_req_create(mem_ctx
, &state
,
4908 struct cli_get_ea_list_path_state
);
4912 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4913 SMB_INFO_QUERY_ALL_EAS
, 4,
4915 if (tevent_req_nomem(subreq
, req
)) {
4916 return tevent_req_post(req
, ev
);
4918 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4922 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4924 struct tevent_req
*req
= tevent_req_callback_data(
4925 subreq
, struct tevent_req
);
4926 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4927 req
, struct cli_get_ea_list_path_state
);
4930 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4932 TALLOC_FREE(subreq
);
4933 if (tevent_req_nterror(req
, status
)) {
4936 tevent_req_done(req
);
4939 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4940 size_t *pnum_eas
, struct ea_struct
**peas
)
4942 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4943 req
, struct cli_get_ea_list_path_state
);
4946 if (tevent_req_is_nterror(req
, &status
)) {
4949 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4951 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4953 return NT_STATUS_OK
;
4956 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4959 struct ea_struct
**pea_list
)
4961 TALLOC_CTX
*frame
= NULL
;
4962 struct tevent_context
*ev
= NULL
;
4963 struct tevent_req
*req
= NULL
;
4964 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4966 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4967 return cli_smb2_get_ea_list_path(cli
,
4974 frame
= talloc_stackframe();
4976 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4978 * Can't use sync call while an async call is in flight
4980 status
= NT_STATUS_INVALID_PARAMETER
;
4983 ev
= samba_tevent_context_init(frame
);
4987 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4991 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4994 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
5000 /****************************************************************************
5001 Convert open "flags" arg to uint32_t on wire.
5002 ****************************************************************************/
5004 static uint32_t open_flags_to_wire(int flags
)
5006 int open_mode
= flags
& O_ACCMODE
;
5009 switch (open_mode
) {
5011 ret
|= SMB_O_WRONLY
;
5018 ret
|= SMB_O_RDONLY
;
5022 if (flags
& O_CREAT
) {
5025 if (flags
& O_EXCL
) {
5028 if (flags
& O_TRUNC
) {
5032 if (flags
& O_SYNC
) {
5036 if (flags
& O_APPEND
) {
5037 ret
|= SMB_O_APPEND
;
5039 #if defined(O_DIRECT)
5040 if (flags
& O_DIRECT
) {
5041 ret
|= SMB_O_DIRECT
;
5044 #if defined(O_DIRECTORY)
5045 if (flags
& O_DIRECTORY
) {
5046 ret
|= SMB_O_DIRECTORY
;
5052 /****************************************************************************
5053 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5054 ****************************************************************************/
5056 struct posix_open_state
{
5060 uint16_t fnum
; /* Out */
5063 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
5065 struct tevent_req
*req
= tevent_req_callback_data(
5066 subreq
, struct tevent_req
);
5067 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
5072 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5073 NULL
, 0, NULL
, &data
, 12, &num_data
);
5074 TALLOC_FREE(subreq
);
5075 if (tevent_req_nterror(req
, status
)) {
5078 state
->fnum
= SVAL(data
,2);
5079 tevent_req_done(req
);
5082 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
5083 struct tevent_context
*ev
,
5084 struct cli_state
*cli
,
5090 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5091 struct posix_open_state
*state
= NULL
;
5092 uint32_t wire_flags
= open_flags_to_wire(flags
);
5094 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
5099 /* Setup setup word. */
5100 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
5102 /* Setup param array. */
5103 state
->param
= talloc_array(state
, uint8_t, 6);
5104 if (tevent_req_nomem(state
->param
, req
)) {
5105 return tevent_req_post(req
, ev
);
5107 memset(state
->param
, '\0', 6);
5108 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
5110 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
5111 strlen(fname
)+1, NULL
);
5113 if (tevent_req_nomem(state
->param
, req
)) {
5114 return tevent_req_post(req
, ev
);
5117 /* Setup data words. */
5119 wire_flags
|= SMB_O_DIRECTORY
;
5122 SIVAL(state
->data
,0,0); /* No oplock. */
5123 SIVAL(state
->data
,4,wire_flags
);
5124 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
5125 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
5126 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
5128 subreq
= cli_trans_send(state
, /* mem ctx. */
5129 ev
, /* event ctx. */
5130 cli
, /* cli_state. */
5131 0, /* additional_flags2 */
5132 SMBtrans2
, /* cmd. */
5133 NULL
, /* pipe name. */
5137 &state
->setup
, /* setup. */
5138 1, /* num setup uint16_t words. */
5139 0, /* max returned setup. */
5140 state
->param
, /* param. */
5141 talloc_get_size(state
->param
),/* num param. */
5142 2, /* max returned param. */
5143 state
->data
, /* data. */
5145 12); /* max returned data. */
5147 if (tevent_req_nomem(subreq
, req
)) {
5148 return tevent_req_post(req
, ev
);
5150 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
5154 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
5155 struct tevent_context
*ev
,
5156 struct cli_state
*cli
,
5161 return cli_posix_open_internal_send(mem_ctx
, ev
,
5162 cli
, fname
, flags
, mode
, false);
5165 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
5167 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
5170 if (tevent_req_is_nterror(req
, &status
)) {
5173 *pfnum
= state
->fnum
;
5174 return NT_STATUS_OK
;
5177 /****************************************************************************
5178 Open - POSIX semantics. Doesn't request oplock.
5179 ****************************************************************************/
5181 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
5182 int flags
, mode_t mode
, uint16_t *pfnum
)
5185 TALLOC_CTX
*frame
= talloc_stackframe();
5186 struct tevent_context
*ev
= NULL
;
5187 struct tevent_req
*req
= NULL
;
5188 NTSTATUS status
= NT_STATUS_OK
;
5190 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5192 * Can't use sync call while an async call is in flight
5194 status
= NT_STATUS_INVALID_PARAMETER
;
5198 ev
= samba_tevent_context_init(frame
);
5200 status
= NT_STATUS_NO_MEMORY
;
5204 req
= cli_posix_open_send(frame
,
5211 status
= NT_STATUS_NO_MEMORY
;
5215 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5219 status
= cli_posix_open_recv(req
, pfnum
);
5226 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5227 struct tevent_context
*ev
,
5228 struct cli_state
*cli
,
5232 return cli_posix_open_internal_send(mem_ctx
, ev
,
5233 cli
, fname
, O_CREAT
, mode
, true);
5236 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5238 return tevent_req_simple_recv_ntstatus(req
);
5241 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5243 TALLOC_CTX
*frame
= talloc_stackframe();
5244 struct tevent_context
*ev
= NULL
;
5245 struct tevent_req
*req
= NULL
;
5246 NTSTATUS status
= NT_STATUS_OK
;
5248 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5250 * Can't use sync call while an async call is in flight
5252 status
= NT_STATUS_INVALID_PARAMETER
;
5256 ev
= samba_tevent_context_init(frame
);
5258 status
= NT_STATUS_NO_MEMORY
;
5262 req
= cli_posix_mkdir_send(frame
,
5268 status
= NT_STATUS_NO_MEMORY
;
5272 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5276 status
= cli_posix_mkdir_recv(req
);
5283 /****************************************************************************
5284 unlink or rmdir - POSIX semantics.
5285 ****************************************************************************/
5287 struct cli_posix_unlink_internal_state
{
5291 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5293 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5294 struct tevent_context
*ev
,
5295 struct cli_state
*cli
,
5299 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5300 struct cli_posix_unlink_internal_state
*state
= NULL
;
5302 req
= tevent_req_create(mem_ctx
, &state
,
5303 struct cli_posix_unlink_internal_state
);
5308 /* Setup data word. */
5309 SSVAL(state
->data
, 0, level
);
5311 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5312 SMB_POSIX_PATH_UNLINK
,
5314 state
->data
, sizeof(state
->data
));
5315 if (tevent_req_nomem(subreq
, req
)) {
5316 return tevent_req_post(req
, ev
);
5318 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5322 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5324 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5325 tevent_req_simple_finish_ntstatus(subreq
, status
);
5328 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5329 struct tevent_context
*ev
,
5330 struct cli_state
*cli
,
5333 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5334 SMB_POSIX_UNLINK_FILE_TARGET
);
5337 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5339 return tevent_req_simple_recv_ntstatus(req
);
5342 /****************************************************************************
5343 unlink - POSIX semantics.
5344 ****************************************************************************/
5346 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5348 TALLOC_CTX
*frame
= talloc_stackframe();
5349 struct tevent_context
*ev
= NULL
;
5350 struct tevent_req
*req
= NULL
;
5351 NTSTATUS status
= NT_STATUS_OK
;
5353 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5355 * Can't use sync call while an async call is in flight
5357 status
= NT_STATUS_INVALID_PARAMETER
;
5361 ev
= samba_tevent_context_init(frame
);
5363 status
= NT_STATUS_NO_MEMORY
;
5367 req
= cli_posix_unlink_send(frame
,
5372 status
= NT_STATUS_NO_MEMORY
;
5376 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5380 status
= cli_posix_unlink_recv(req
);
5387 /****************************************************************************
5388 rmdir - POSIX semantics.
5389 ****************************************************************************/
5391 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5392 struct tevent_context
*ev
,
5393 struct cli_state
*cli
,
5396 return cli_posix_unlink_internal_send(
5397 mem_ctx
, ev
, cli
, fname
,
5398 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5401 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5403 return tevent_req_simple_recv_ntstatus(req
);
5406 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5408 TALLOC_CTX
*frame
= talloc_stackframe();
5409 struct tevent_context
*ev
= NULL
;
5410 struct tevent_req
*req
= NULL
;
5411 NTSTATUS status
= NT_STATUS_OK
;
5413 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5415 * Can't use sync call while an async call is in flight
5417 status
= NT_STATUS_INVALID_PARAMETER
;
5421 ev
= samba_tevent_context_init(frame
);
5423 status
= NT_STATUS_NO_MEMORY
;
5427 req
= cli_posix_rmdir_send(frame
,
5432 status
= NT_STATUS_NO_MEMORY
;
5436 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5440 status
= cli_posix_rmdir_recv(req
, frame
);
5447 /****************************************************************************
5449 ****************************************************************************/
5451 struct cli_notify_state
{
5453 uint32_t num_changes
;
5454 struct notify_change
*changes
;
5457 static void cli_notify_done(struct tevent_req
*subreq
);
5459 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5460 struct tevent_context
*ev
,
5461 struct cli_state
*cli
, uint16_t fnum
,
5462 uint32_t buffer_size
,
5463 uint32_t completion_filter
, bool recursive
)
5465 struct tevent_req
*req
, *subreq
;
5466 struct cli_notify_state
*state
;
5467 unsigned old_timeout
;
5469 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5474 SIVAL(state
->setup
, 0, completion_filter
);
5475 SSVAL(state
->setup
, 4, fnum
);
5476 SSVAL(state
->setup
, 6, recursive
);
5479 * Notifies should not time out
5481 old_timeout
= cli_set_timeout(cli
, 0);
5483 subreq
= cli_trans_send(
5484 state
, /* mem ctx. */
5485 ev
, /* event ctx. */
5486 cli
, /* cli_state. */
5487 0, /* additional_flags2 */
5488 SMBnttrans
, /* cmd. */
5489 NULL
, /* pipe name. */
5491 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5493 (uint16_t *)state
->setup
, /* setup. */
5494 4, /* num setup uint16_t words. */
5495 0, /* max returned setup. */
5498 buffer_size
, /* max returned param. */
5501 0); /* max returned data. */
5503 cli_set_timeout(cli
, old_timeout
);
5505 if (tevent_req_nomem(subreq
, req
)) {
5506 return tevent_req_post(req
, ev
);
5508 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5512 static void cli_notify_done(struct tevent_req
*subreq
)
5514 struct tevent_req
*req
= tevent_req_callback_data(
5515 subreq
, struct tevent_req
);
5516 struct cli_notify_state
*state
= tevent_req_data(
5517 req
, struct cli_notify_state
);
5520 uint32_t i
, ofs
, num_params
;
5523 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5524 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5525 TALLOC_FREE(subreq
);
5526 if (tevent_req_nterror(req
, status
)) {
5527 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5531 state
->num_changes
= 0;
5534 while (num_params
- ofs
> 12) {
5535 uint32_t next
= IVAL(params
, ofs
);
5536 state
->num_changes
+= 1;
5538 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5544 state
->changes
= talloc_array(state
, struct notify_change
,
5545 state
->num_changes
);
5546 if (tevent_req_nomem(state
->changes
, req
)) {
5547 TALLOC_FREE(params
);
5553 for (i
=0; i
<state
->num_changes
; i
++) {
5554 uint32_t next
= IVAL(params
, ofs
);
5555 uint32_t len
= IVAL(params
, ofs
+8);
5559 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5560 TALLOC_FREE(params
);
5562 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5566 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5567 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5568 &name
, params
+ofs
+12, len
,
5569 STR_TERMINATE
|STR_UNICODE
);
5571 TALLOC_FREE(params
);
5572 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5575 state
->changes
[i
].name
= name
;
5579 TALLOC_FREE(params
);
5580 tevent_req_done(req
);
5583 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5584 uint32_t *pnum_changes
,
5585 struct notify_change
**pchanges
)
5587 struct cli_notify_state
*state
= tevent_req_data(
5588 req
, struct cli_notify_state
);
5591 if (tevent_req_is_nterror(req
, &status
)) {
5595 *pnum_changes
= state
->num_changes
;
5596 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5597 return NT_STATUS_OK
;
5600 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5601 uint32_t completion_filter
, bool recursive
,
5602 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5603 struct notify_change
**pchanges
)
5606 struct tevent_context
*ev
;
5607 struct tevent_req
*req
;
5608 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5610 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
5611 return cli_smb2_notify(cli
, fnum
, buffer_size
,
5612 completion_filter
, recursive
,
5613 mem_ctx
, pchanges
, pnum_changes
);
5616 frame
= talloc_stackframe();
5618 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5620 * Can't use sync call while an async call is in flight
5622 status
= NT_STATUS_INVALID_PARAMETER
;
5625 ev
= samba_tevent_context_init(frame
);
5629 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5630 completion_filter
, recursive
);
5634 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5637 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5643 struct cli_qpathinfo_state
{
5652 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5654 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5655 struct tevent_context
*ev
,
5656 struct cli_state
*cli
, const char *fname
,
5657 uint16_t level
, uint32_t min_rdata
,
5660 struct tevent_req
*req
, *subreq
;
5661 struct cli_qpathinfo_state
*state
;
5662 uint16_t additional_flags2
= 0;
5664 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5668 state
->min_rdata
= min_rdata
;
5669 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5671 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5672 if (tevent_req_nomem(state
->param
, req
)) {
5673 return tevent_req_post(req
, ev
);
5675 SSVAL(state
->param
, 0, level
);
5676 state
->param
= trans2_bytes_push_str(
5677 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5678 if (tevent_req_nomem(state
->param
, req
)) {
5679 return tevent_req_post(req
, ev
);
5682 if (clistr_is_previous_version_path(fname
, NULL
, NULL
, NULL
) &&
5683 !INFO_LEVEL_IS_UNIX(level
)) {
5684 additional_flags2
= FLAGS2_REPARSE_PATH
;
5687 subreq
= cli_trans_send(
5688 state
, /* mem ctx. */
5689 ev
, /* event ctx. */
5690 cli
, /* cli_state. */
5691 additional_flags2
, /* additional_flags2 */
5692 SMBtrans2
, /* cmd. */
5693 NULL
, /* pipe name. */
5697 state
->setup
, /* setup. */
5698 1, /* num setup uint16_t words. */
5699 0, /* max returned setup. */
5700 state
->param
, /* param. */
5701 talloc_get_size(state
->param
), /* num param. */
5702 2, /* max returned param. */
5705 max_rdata
); /* max returned data. */
5707 if (tevent_req_nomem(subreq
, req
)) {
5708 return tevent_req_post(req
, ev
);
5710 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5714 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5716 struct tevent_req
*req
= tevent_req_callback_data(
5717 subreq
, struct tevent_req
);
5718 struct cli_qpathinfo_state
*state
= tevent_req_data(
5719 req
, struct cli_qpathinfo_state
);
5722 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5724 &state
->rdata
, state
->min_rdata
,
5726 if (tevent_req_nterror(req
, status
)) {
5729 tevent_req_done(req
);
5732 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5733 uint8_t **rdata
, uint32_t *num_rdata
)
5735 struct cli_qpathinfo_state
*state
= tevent_req_data(
5736 req
, struct cli_qpathinfo_state
);
5739 if (tevent_req_is_nterror(req
, &status
)) {
5742 if (rdata
!= NULL
) {
5743 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5745 TALLOC_FREE(state
->rdata
);
5747 if (num_rdata
!= NULL
) {
5748 *num_rdata
= state
->num_rdata
;
5750 return NT_STATUS_OK
;
5753 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5754 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5756 uint8_t **rdata
, uint32_t *num_rdata
)
5758 TALLOC_CTX
*frame
= talloc_stackframe();
5759 struct tevent_context
*ev
;
5760 struct tevent_req
*req
;
5761 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5763 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5765 * Can't use sync call while an async call is in flight
5767 status
= NT_STATUS_INVALID_PARAMETER
;
5770 ev
= samba_tevent_context_init(frame
);
5774 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5779 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5782 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5788 struct cli_qfileinfo_state
{
5792 uint16_t recv_flags2
;
5798 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5800 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5801 struct tevent_context
*ev
,
5802 struct cli_state
*cli
, uint16_t fnum
,
5803 uint16_t level
, uint32_t min_rdata
,
5806 struct tevent_req
*req
, *subreq
;
5807 struct cli_qfileinfo_state
*state
;
5809 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5813 state
->min_rdata
= min_rdata
;
5814 SSVAL(state
->param
, 0, fnum
);
5815 SSVAL(state
->param
, 2, level
);
5816 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5818 subreq
= cli_trans_send(
5819 state
, /* mem ctx. */
5820 ev
, /* event ctx. */
5821 cli
, /* cli_state. */
5822 0, /* additional_flags2 */
5823 SMBtrans2
, /* cmd. */
5824 NULL
, /* pipe name. */
5828 state
->setup
, /* setup. */
5829 1, /* num setup uint16_t words. */
5830 0, /* max returned setup. */
5831 state
->param
, /* param. */
5832 sizeof(state
->param
), /* num param. */
5833 2, /* max returned param. */
5836 max_rdata
); /* max returned data. */
5838 if (tevent_req_nomem(subreq
, req
)) {
5839 return tevent_req_post(req
, ev
);
5841 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5845 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5847 struct tevent_req
*req
= tevent_req_callback_data(
5848 subreq
, struct tevent_req
);
5849 struct cli_qfileinfo_state
*state
= tevent_req_data(
5850 req
, struct cli_qfileinfo_state
);
5853 status
= cli_trans_recv(subreq
, state
,
5854 &state
->recv_flags2
,
5857 &state
->rdata
, state
->min_rdata
,
5859 if (tevent_req_nterror(req
, status
)) {
5862 tevent_req_done(req
);
5865 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5866 uint16_t *recv_flags2
,
5867 uint8_t **rdata
, uint32_t *num_rdata
)
5869 struct cli_qfileinfo_state
*state
= tevent_req_data(
5870 req
, struct cli_qfileinfo_state
);
5873 if (tevent_req_is_nterror(req
, &status
)) {
5877 if (recv_flags2
!= NULL
) {
5878 *recv_flags2
= state
->recv_flags2
;
5880 if (rdata
!= NULL
) {
5881 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5883 TALLOC_FREE(state
->rdata
);
5885 if (num_rdata
!= NULL
) {
5886 *num_rdata
= state
->num_rdata
;
5888 return NT_STATUS_OK
;
5891 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5892 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5893 uint32_t max_rdata
, uint16_t *recv_flags2
,
5894 uint8_t **rdata
, uint32_t *num_rdata
)
5896 TALLOC_CTX
*frame
= talloc_stackframe();
5897 struct tevent_context
*ev
;
5898 struct tevent_req
*req
;
5899 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5901 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5903 * Can't use sync call while an async call is in flight
5905 status
= NT_STATUS_INVALID_PARAMETER
;
5908 ev
= samba_tevent_context_init(frame
);
5912 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5917 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5920 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5926 struct cli_flush_state
{
5930 static void cli_flush_done(struct tevent_req
*subreq
);
5932 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5933 struct tevent_context
*ev
,
5934 struct cli_state
*cli
,
5937 struct tevent_req
*req
, *subreq
;
5938 struct cli_flush_state
*state
;
5940 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5944 SSVAL(state
->vwv
+ 0, 0, fnum
);
5946 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 0, 1, state
->vwv
,
5948 if (tevent_req_nomem(subreq
, req
)) {
5949 return tevent_req_post(req
, ev
);
5951 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5955 static void cli_flush_done(struct tevent_req
*subreq
)
5957 struct tevent_req
*req
= tevent_req_callback_data(
5958 subreq
, struct tevent_req
);
5961 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5962 TALLOC_FREE(subreq
);
5963 if (tevent_req_nterror(req
, status
)) {
5966 tevent_req_done(req
);
5969 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5971 return tevent_req_simple_recv_ntstatus(req
);
5974 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5976 TALLOC_CTX
*frame
= talloc_stackframe();
5977 struct tevent_context
*ev
;
5978 struct tevent_req
*req
;
5979 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5981 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5983 * Can't use sync call while an async call is in flight
5985 status
= NT_STATUS_INVALID_PARAMETER
;
5988 ev
= samba_tevent_context_init(frame
);
5992 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5996 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5999 status
= cli_flush_recv(req
);
6005 struct cli_shadow_copy_data_state
{
6012 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
6014 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
6015 struct tevent_context
*ev
,
6016 struct cli_state
*cli
,
6020 struct tevent_req
*req
, *subreq
;
6021 struct cli_shadow_copy_data_state
*state
;
6024 req
= tevent_req_create(mem_ctx
, &state
,
6025 struct cli_shadow_copy_data_state
);
6029 state
->get_names
= get_names
;
6030 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
6032 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
6033 SSVAL(state
->setup
+ 2, 0, fnum
);
6034 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
6035 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
6037 subreq
= cli_trans_send(
6038 state
, ev
, cli
, 0, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
6039 state
->setup
, ARRAY_SIZE(state
->setup
),
6040 ARRAY_SIZE(state
->setup
),
6043 if (tevent_req_nomem(subreq
, req
)) {
6044 return tevent_req_post(req
, ev
);
6046 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
6050 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
6052 struct tevent_req
*req
= tevent_req_callback_data(
6053 subreq
, struct tevent_req
);
6054 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
6055 req
, struct cli_shadow_copy_data_state
);
6058 status
= cli_trans_recv(subreq
, state
, NULL
,
6059 NULL
, 0, NULL
, /* setup */
6060 NULL
, 0, NULL
, /* param */
6061 &state
->data
, 12, &state
->num_data
);
6062 TALLOC_FREE(subreq
);
6063 if (tevent_req_nterror(req
, status
)) {
6066 tevent_req_done(req
);
6069 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
6070 char ***pnames
, int *pnum_names
)
6072 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
6073 req
, struct cli_shadow_copy_data_state
);
6074 char **names
= NULL
;
6075 uint32_t i
, num_names
;
6077 uint8_t *endp
= NULL
;
6080 if (tevent_req_is_nterror(req
, &status
)) {
6084 if (state
->num_data
< 16) {
6085 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6088 num_names
= IVAL(state
->data
, 4);
6089 dlength
= IVAL(state
->data
, 8);
6091 if (num_names
> 0x7FFFFFFF) {
6092 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6095 if (!state
->get_names
) {
6096 *pnum_names
= (int)num_names
;
6097 return NT_STATUS_OK
;
6100 if (dlength
+ 12 < 12) {
6101 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6103 if (dlength
+ 12 > state
->num_data
) {
6104 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6106 if (state
->num_data
+ (2 * sizeof(SHADOW_COPY_LABEL
)) <
6108 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6111 names
= talloc_array(mem_ctx
, char *, num_names
);
6112 if (names
== NULL
) {
6113 return NT_STATUS_NO_MEMORY
;
6116 endp
= state
->data
+ state
->num_data
;
6118 for (i
=0; i
<num_names
; i
++) {
6121 size_t converted_size
;
6123 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
6125 if (src
+ (2 * sizeof(SHADOW_COPY_LABEL
)) > endp
) {
6126 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6129 ret
= convert_string_talloc(
6130 names
, CH_UTF16LE
, CH_UNIX
,
6131 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
6132 &names
[i
], &converted_size
);
6135 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
6138 *pnum_names
= (int)num_names
;
6140 return NT_STATUS_OK
;
6143 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
6144 uint16_t fnum
, bool get_names
,
6145 char ***pnames
, int *pnum_names
)
6147 TALLOC_CTX
*frame
= NULL
;
6148 struct tevent_context
*ev
;
6149 struct tevent_req
*req
;
6150 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
6152 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
6153 return cli_smb2_shadow_copy_data(mem_ctx
,
6161 frame
= talloc_stackframe();
6163 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
6165 * Can't use sync call while an async call is in flight
6167 status
= NT_STATUS_INVALID_PARAMETER
;
6170 ev
= samba_tevent_context_init(frame
);
6174 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
6178 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
6181 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);