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 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf
, bool ucs2
,
39 const char *str
, size_t str_len
,
41 size_t *pconverted_size
)
45 size_t converted_size
;
51 buflen
= talloc_get_size(buf
);
54 ((align_odd
&& (buflen
% 2 == 0)) ||
55 (!align_odd
&& (buflen
% 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf
= talloc_realloc(NULL
, buf
, uint8_t, buflen
+ 1);
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
68 ucs2
? CH_UTF16LE
: CH_DOS
,
69 str
, str_len
, &converted
,
74 buf
= talloc_realloc(NULL
, buf
, uint8_t,
75 buflen
+ converted_size
);
77 TALLOC_FREE(converted
);
81 memcpy(buf
+ buflen
, converted
, converted_size
);
83 TALLOC_FREE(converted
);
85 if (pconverted_size
) {
86 *pconverted_size
= converted_size
;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
98 const char *str
, size_t str_len
,
99 size_t *pconverted_size
)
101 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
102 true, pconverted_size
);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf
, uint8_t prefix
,
106 const uint8_t *bytes
, size_t num_bytes
)
113 buflen
= talloc_get_size(buf
);
115 buf
= talloc_realloc(NULL
, buf
, uint8_t,
116 buflen
+ 1 + num_bytes
);
120 buf
[buflen
] = prefix
;
121 memcpy(&buf
[buflen
+1], bytes
, num_bytes
);
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf
, bool ucs2
,
133 const char *str
, size_t str_len
,
134 size_t *pconverted_size
)
136 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
137 false, pconverted_size
);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf
,
141 const uint8_t *bytes
, size_t num_bytes
)
148 buflen
= talloc_get_size(buf
);
150 buf
= talloc_realloc(NULL
, buf
, uint8_t,
155 memcpy(&buf
[buflen
], bytes
, num_bytes
);
159 struct cli_setpathinfo_state
{
164 static void cli_setpathinfo_done(struct tevent_req
*subreq
);
166 struct tevent_req
*cli_setpathinfo_send(TALLOC_CTX
*mem_ctx
,
167 struct tevent_context
*ev
,
168 struct cli_state
*cli
,
174 struct tevent_req
*req
, *subreq
;
175 struct cli_setpathinfo_state
*state
;
177 req
= tevent_req_create(mem_ctx
, &state
,
178 struct cli_setpathinfo_state
);
183 /* Setup setup word. */
184 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
186 /* Setup param array. */
187 state
->param
= talloc_zero_array(state
, uint8_t, 6);
188 if (tevent_req_nomem(state
->param
, req
)) {
189 return tevent_req_post(req
, ev
);
191 SSVAL(state
->param
, 0, level
);
193 state
->param
= trans2_bytes_push_str(
194 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), path
, strlen(path
)+1, NULL
);
195 if (tevent_req_nomem(state
->param
, req
)) {
196 return tevent_req_post(req
, ev
);
199 subreq
= cli_trans_send(
200 state
, /* mem ctx. */
202 cli
, /* cli_state. */
203 SMBtrans2
, /* cmd. */
204 NULL
, /* pipe name. */
208 &state
->setup
, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state
->param
, /* param. */
212 talloc_get_size(state
->param
), /* num param. */
213 2, /* max returned param. */
215 data_len
, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq
, req
)) {
219 return tevent_req_post(req
, ev
);
221 tevent_req_set_callback(subreq
, cli_setpathinfo_done
, req
);
225 static void cli_setpathinfo_done(struct tevent_req
*subreq
)
227 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
228 NULL
, 0, NULL
, NULL
, 0, NULL
);
229 tevent_req_simple_finish_ntstatus(subreq
, status
);
232 NTSTATUS
cli_setpathinfo_recv(struct tevent_req
*req
)
234 return tevent_req_simple_recv_ntstatus(req
);
237 NTSTATUS
cli_setpathinfo(struct cli_state
*cli
,
243 TALLOC_CTX
*frame
= talloc_stackframe();
244 struct tevent_context
*ev
;
245 struct tevent_req
*req
;
246 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
248 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
250 * Can't use sync call while an async call is in flight
252 status
= NT_STATUS_INVALID_PARAMETER
;
255 ev
= samba_tevent_context_init(frame
);
259 req
= cli_setpathinfo_send(ev
, ev
, cli
, level
, path
, data
, data_len
);
263 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
266 status
= cli_setpathinfo_recv(req
);
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state
{
281 static void cli_posix_link_internal_done(struct tevent_req
*subreq
);
283 static struct tevent_req
*cli_posix_link_internal_send(TALLOC_CTX
*mem_ctx
,
284 struct tevent_context
*ev
,
285 struct cli_state
*cli
,
290 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
291 struct cli_posix_link_internal_state
*state
= NULL
;
293 req
= tevent_req_create(mem_ctx
, &state
,
294 struct cli_posix_link_internal_state
);
299 /* Setup data array. */
300 state
->data
= talloc_array(state
, uint8_t, 0);
301 if (tevent_req_nomem(state
->data
, req
)) {
302 return tevent_req_post(req
, ev
);
304 state
->data
= trans2_bytes_push_str(
305 state
->data
, smbXcli_conn_use_unicode(cli
->conn
), oldname
, strlen(oldname
)+1, NULL
);
307 subreq
= cli_setpathinfo_send(
308 state
, ev
, cli
, level
, newname
,
309 state
->data
, talloc_get_size(state
->data
));
310 if (tevent_req_nomem(subreq
, req
)) {
311 return tevent_req_post(req
, ev
);
313 tevent_req_set_callback(subreq
, cli_posix_link_internal_done
, req
);
317 static void cli_posix_link_internal_done(struct tevent_req
*subreq
)
319 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
320 tevent_req_simple_finish_ntstatus(subreq
, status
);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req
*cli_posix_symlink_send(TALLOC_CTX
*mem_ctx
,
328 struct tevent_context
*ev
,
329 struct cli_state
*cli
,
333 return cli_posix_link_internal_send(
334 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_LINK
, oldname
, newname
);
337 NTSTATUS
cli_posix_symlink_recv(struct tevent_req
*req
)
339 return tevent_req_simple_recv_ntstatus(req
);
342 NTSTATUS
cli_posix_symlink(struct cli_state
*cli
,
346 TALLOC_CTX
*frame
= talloc_stackframe();
347 struct tevent_context
*ev
= NULL
;
348 struct tevent_req
*req
= NULL
;
349 NTSTATUS status
= NT_STATUS_OK
;
351 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
353 * Can't use sync call while an async call is in flight
355 status
= NT_STATUS_INVALID_PARAMETER
;
359 ev
= samba_tevent_context_init(frame
);
361 status
= NT_STATUS_NO_MEMORY
;
365 req
= cli_posix_symlink_send(frame
,
371 status
= NT_STATUS_NO_MEMORY
;
375 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
379 status
= cli_posix_symlink_recv(req
);
386 /****************************************************************************
387 Read a POSIX symlink.
388 ****************************************************************************/
390 struct readlink_state
{
395 static void cli_posix_readlink_done(struct tevent_req
*subreq
);
397 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
398 struct tevent_context
*ev
,
399 struct cli_state
*cli
,
403 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
404 struct readlink_state
*state
= NULL
;
405 uint32_t maxbytelen
= (uint32_t)(smbXcli_conn_use_unicode(cli
->conn
) ? len
*3 : len
);
407 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
413 * Len is in bytes, we need it in UCS2 units.
415 if ((2*len
< len
) || (maxbytelen
< len
)) {
416 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
417 return tevent_req_post(req
, ev
);
420 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
421 SMB_QUERY_FILE_UNIX_LINK
, 1, maxbytelen
);
422 if (tevent_req_nomem(subreq
, req
)) {
423 return tevent_req_post(req
, ev
);
425 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
429 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
431 struct tevent_req
*req
= tevent_req_callback_data(
432 subreq
, struct tevent_req
);
433 struct readlink_state
*state
= tevent_req_data(
434 req
, struct readlink_state
);
437 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
440 if (tevent_req_nterror(req
, status
)) {
444 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446 if (state
->data
[state
->num_data
-1] != '\0') {
447 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
450 tevent_req_done(req
);
453 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
454 char *retpath
, size_t len
)
457 char *converted
= NULL
;
458 size_t converted_size
= 0;
459 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
461 if (tevent_req_is_nterror(req
, &status
)) {
464 /* The returned data is a pushed string, not raw data. */
465 if (!convert_string_talloc(state
,
466 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
472 return NT_STATUS_NO_MEMORY
;
475 len
= MIN(len
,converted_size
);
477 return NT_STATUS_DATA_ERROR
;
479 memcpy(retpath
, converted
, len
);
483 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
484 char *linkpath
, size_t len
)
486 TALLOC_CTX
*frame
= talloc_stackframe();
487 struct tevent_context
*ev
= NULL
;
488 struct tevent_req
*req
= NULL
;
489 NTSTATUS status
= NT_STATUS_OK
;
491 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
493 * Can't use sync call while an async call is in flight
495 status
= NT_STATUS_INVALID_PARAMETER
;
499 ev
= samba_tevent_context_init(frame
);
501 status
= NT_STATUS_NO_MEMORY
;
505 req
= cli_posix_readlink_send(frame
,
511 status
= NT_STATUS_NO_MEMORY
;
515 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
519 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
526 /****************************************************************************
527 Hard link a file (UNIX extensions).
528 ****************************************************************************/
530 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
531 struct tevent_context
*ev
,
532 struct cli_state
*cli
,
536 return cli_posix_link_internal_send(
537 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_HLINK
, oldname
, newname
);
540 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
542 return tevent_req_simple_recv_ntstatus(req
);
545 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
549 TALLOC_CTX
*frame
= talloc_stackframe();
550 struct tevent_context
*ev
= NULL
;
551 struct tevent_req
*req
= NULL
;
552 NTSTATUS status
= NT_STATUS_OK
;
554 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
556 * Can't use sync call while an async call is in flight
558 status
= NT_STATUS_INVALID_PARAMETER
;
562 ev
= samba_tevent_context_init(frame
);
564 status
= NT_STATUS_NO_MEMORY
;
568 req
= cli_posix_hardlink_send(frame
,
574 status
= NT_STATUS_NO_MEMORY
;
578 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
582 status
= cli_posix_hardlink_recv(req
);
589 /****************************************************************************
590 Do a POSIX getfacl (UNIX extensions).
591 ****************************************************************************/
593 struct getfacl_state
{
598 static void cli_posix_getfacl_done(struct tevent_req
*subreq
);
600 struct tevent_req
*cli_posix_getfacl_send(TALLOC_CTX
*mem_ctx
,
601 struct tevent_context
*ev
,
602 struct cli_state
*cli
,
605 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
606 struct getfacl_state
*state
= NULL
;
608 req
= tevent_req_create(mem_ctx
, &state
, struct getfacl_state
);
612 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_QUERY_POSIX_ACL
,
614 if (tevent_req_nomem(subreq
, req
)) {
615 return tevent_req_post(req
, ev
);
617 tevent_req_set_callback(subreq
, cli_posix_getfacl_done
, req
);
621 static void cli_posix_getfacl_done(struct tevent_req
*subreq
)
623 struct tevent_req
*req
= tevent_req_callback_data(
624 subreq
, struct tevent_req
);
625 struct getfacl_state
*state
= tevent_req_data(
626 req
, struct getfacl_state
);
629 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
632 if (tevent_req_nterror(req
, status
)) {
635 tevent_req_done(req
);
638 NTSTATUS
cli_posix_getfacl_recv(struct tevent_req
*req
,
643 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
646 if (tevent_req_is_nterror(req
, &status
)) {
649 *prb_size
= (size_t)state
->num_data
;
650 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
654 NTSTATUS
cli_posix_getfacl(struct cli_state
*cli
,
660 TALLOC_CTX
*frame
= talloc_stackframe();
661 struct tevent_context
*ev
= NULL
;
662 struct tevent_req
*req
= NULL
;
663 NTSTATUS status
= NT_STATUS_OK
;
665 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
667 * Can't use sync call while an async call is in flight
669 status
= NT_STATUS_INVALID_PARAMETER
;
673 ev
= samba_tevent_context_init(frame
);
675 status
= NT_STATUS_NO_MEMORY
;
679 req
= cli_posix_getfacl_send(frame
,
684 status
= NT_STATUS_NO_MEMORY
;
688 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
692 status
= cli_posix_getfacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
699 /****************************************************************************
700 Stat a file (UNIX extensions).
701 ****************************************************************************/
708 static void cli_posix_stat_done(struct tevent_req
*subreq
);
710 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
711 struct tevent_context
*ev
,
712 struct cli_state
*cli
,
715 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
716 struct stat_state
*state
= NULL
;
718 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
722 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
723 SMB_QUERY_FILE_UNIX_BASIC
, 100, 100);
724 if (tevent_req_nomem(subreq
, req
)) {
725 return tevent_req_post(req
, ev
);
727 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
731 static void cli_posix_stat_done(struct tevent_req
*subreq
)
733 struct tevent_req
*req
= tevent_req_callback_data(
734 subreq
, struct tevent_req
);
735 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
738 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
741 if (tevent_req_nterror(req
, status
)) {
744 tevent_req_done(req
);
747 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
748 SMB_STRUCT_STAT
*sbuf
)
750 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
753 if (tevent_req_is_nterror(req
, &status
)) {
757 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
758 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
759 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
760 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
762 /* assume 512 byte blocks */
763 sbuf
->st_ex_blocks
/= 512;
765 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
766 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
767 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
769 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
770 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
771 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
772 #if defined(HAVE_MAKEDEV)
774 uint32_t dev_major
= IVAL(state
->data
,60);
775 uint32_t dev_minor
= IVAL(state
->data
,68);
776 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
779 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
780 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
781 sbuf
->st_ex_nlink
= BIG_UINT(state
->data
,92); /* number of hard links */
786 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
788 SMB_STRUCT_STAT
*sbuf
)
790 TALLOC_CTX
*frame
= talloc_stackframe();
791 struct tevent_context
*ev
= NULL
;
792 struct tevent_req
*req
= NULL
;
793 NTSTATUS status
= NT_STATUS_OK
;
795 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
797 * Can't use sync call while an async call is in flight
799 status
= NT_STATUS_INVALID_PARAMETER
;
803 ev
= samba_tevent_context_init(frame
);
805 status
= NT_STATUS_NO_MEMORY
;
809 req
= cli_posix_stat_send(frame
,
814 status
= NT_STATUS_NO_MEMORY
;
818 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
822 status
= cli_posix_stat_recv(req
, sbuf
);
829 /****************************************************************************
830 Chmod or chown a file internal (UNIX extensions).
831 ****************************************************************************/
833 struct cli_posix_chown_chmod_internal_state
{
837 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
);
839 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
840 struct tevent_context
*ev
,
841 struct cli_state
*cli
,
847 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
848 struct cli_posix_chown_chmod_internal_state
*state
= NULL
;
850 req
= tevent_req_create(mem_ctx
, &state
,
851 struct cli_posix_chown_chmod_internal_state
);
856 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
857 memset(&state
->data
[40], '\0', 60);
858 SIVAL(state
->data
,40,uid
);
859 SIVAL(state
->data
,48,gid
);
860 SIVAL(state
->data
,84,mode
);
862 subreq
= cli_setpathinfo_send(state
, ev
, cli
, SMB_SET_FILE_UNIX_BASIC
,
863 fname
, state
->data
, sizeof(state
->data
));
864 if (tevent_req_nomem(subreq
, req
)) {
865 return tevent_req_post(req
, ev
);
867 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
,
872 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
874 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
875 tevent_req_simple_finish_ntstatus(subreq
, status
);
878 /****************************************************************************
879 chmod a file (UNIX extensions).
880 ****************************************************************************/
882 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
883 struct tevent_context
*ev
,
884 struct cli_state
*cli
,
888 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
890 unix_perms_to_wire(mode
),
895 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
897 return tevent_req_simple_recv_ntstatus(req
);
900 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
902 TALLOC_CTX
*frame
= talloc_stackframe();
903 struct tevent_context
*ev
= NULL
;
904 struct tevent_req
*req
= NULL
;
905 NTSTATUS status
= NT_STATUS_OK
;
907 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
909 * Can't use sync call while an async call is in flight
911 status
= NT_STATUS_INVALID_PARAMETER
;
915 ev
= samba_tevent_context_init(frame
);
917 status
= NT_STATUS_NO_MEMORY
;
921 req
= cli_posix_chmod_send(frame
,
927 status
= NT_STATUS_NO_MEMORY
;
931 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
935 status
= cli_posix_chmod_recv(req
);
942 /****************************************************************************
943 chown a file (UNIX extensions).
944 ****************************************************************************/
946 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
947 struct tevent_context
*ev
,
948 struct cli_state
*cli
,
953 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
960 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
962 return tevent_req_simple_recv_ntstatus(req
);
965 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
970 TALLOC_CTX
*frame
= talloc_stackframe();
971 struct tevent_context
*ev
= NULL
;
972 struct tevent_req
*req
= NULL
;
973 NTSTATUS status
= NT_STATUS_OK
;
975 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
977 * Can't use sync call while an async call is in flight
979 status
= NT_STATUS_INVALID_PARAMETER
;
983 ev
= samba_tevent_context_init(frame
);
985 status
= NT_STATUS_NO_MEMORY
;
989 req
= cli_posix_chown_send(frame
,
996 status
= NT_STATUS_NO_MEMORY
;
1000 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1004 status
= cli_posix_chown_recv(req
);
1011 /****************************************************************************
1013 ****************************************************************************/
1015 static void cli_rename_done(struct tevent_req
*subreq
);
1017 struct cli_rename_state
{
1021 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1022 struct tevent_context
*ev
,
1023 struct cli_state
*cli
,
1024 const char *fname_src
,
1025 const char *fname_dst
)
1027 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1028 struct cli_rename_state
*state
= NULL
;
1029 uint8_t additional_flags
= 0;
1030 uint8_t *bytes
= NULL
;
1032 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
1037 SSVAL(state
->vwv
+0, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1039 bytes
= talloc_array(state
, uint8_t, 1);
1040 if (tevent_req_nomem(bytes
, req
)) {
1041 return tevent_req_post(req
, ev
);
1044 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1045 strlen(fname_src
)+1, NULL
);
1046 if (tevent_req_nomem(bytes
, req
)) {
1047 return tevent_req_post(req
, ev
);
1050 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1051 talloc_get_size(bytes
)+1);
1052 if (tevent_req_nomem(bytes
, req
)) {
1053 return tevent_req_post(req
, ev
);
1056 bytes
[talloc_get_size(bytes
)-1] = 4;
1057 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1058 strlen(fname_dst
)+1, NULL
);
1059 if (tevent_req_nomem(bytes
, req
)) {
1060 return tevent_req_post(req
, ev
);
1063 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1064 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1065 if (tevent_req_nomem(subreq
, req
)) {
1066 return tevent_req_post(req
, ev
);
1068 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
1072 static void cli_rename_done(struct tevent_req
*subreq
)
1074 struct tevent_req
*req
= tevent_req_callback_data(
1075 subreq
, struct tevent_req
);
1078 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1079 TALLOC_FREE(subreq
);
1080 if (tevent_req_nterror(req
, status
)) {
1083 tevent_req_done(req
);
1086 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1088 return tevent_req_simple_recv_ntstatus(req
);
1091 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1093 TALLOC_CTX
*frame
= NULL
;
1094 struct tevent_context
*ev
;
1095 struct tevent_req
*req
;
1096 NTSTATUS status
= NT_STATUS_OK
;
1098 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1099 return cli_smb2_rename(cli
,
1104 frame
= talloc_stackframe();
1106 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1108 * Can't use sync call while an async call is in flight
1110 status
= NT_STATUS_INVALID_PARAMETER
;
1114 ev
= samba_tevent_context_init(frame
);
1116 status
= NT_STATUS_NO_MEMORY
;
1120 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1122 status
= NT_STATUS_NO_MEMORY
;
1126 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1130 status
= cli_rename_recv(req
);
1137 /****************************************************************************
1139 ****************************************************************************/
1141 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1143 struct cli_ntrename_internal_state
{
1147 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1148 struct tevent_context
*ev
,
1149 struct cli_state
*cli
,
1150 const char *fname_src
,
1151 const char *fname_dst
,
1152 uint16_t rename_flag
)
1154 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1155 struct cli_ntrename_internal_state
*state
= NULL
;
1156 uint8_t additional_flags
= 0;
1157 uint8_t *bytes
= NULL
;
1159 req
= tevent_req_create(mem_ctx
, &state
,
1160 struct cli_ntrename_internal_state
);
1165 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1166 SSVAL(state
->vwv
+1, 0, rename_flag
);
1168 bytes
= talloc_array(state
, uint8_t, 1);
1169 if (tevent_req_nomem(bytes
, req
)) {
1170 return tevent_req_post(req
, ev
);
1173 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1174 strlen(fname_src
)+1, NULL
);
1175 if (tevent_req_nomem(bytes
, req
)) {
1176 return tevent_req_post(req
, ev
);
1179 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1180 talloc_get_size(bytes
)+1);
1181 if (tevent_req_nomem(bytes
, req
)) {
1182 return tevent_req_post(req
, ev
);
1185 bytes
[talloc_get_size(bytes
)-1] = 4;
1186 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1187 strlen(fname_dst
)+1, NULL
);
1188 if (tevent_req_nomem(bytes
, req
)) {
1189 return tevent_req_post(req
, ev
);
1192 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1193 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1194 if (tevent_req_nomem(subreq
, req
)) {
1195 return tevent_req_post(req
, ev
);
1197 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1201 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1203 struct tevent_req
*req
= tevent_req_callback_data(
1204 subreq
, struct tevent_req
);
1207 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1208 TALLOC_FREE(subreq
);
1209 if (tevent_req_nterror(req
, status
)) {
1212 tevent_req_done(req
);
1215 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1217 return tevent_req_simple_recv_ntstatus(req
);
1220 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1221 struct tevent_context
*ev
,
1222 struct cli_state
*cli
,
1223 const char *fname_src
,
1224 const char *fname_dst
)
1226 return cli_ntrename_internal_send(mem_ctx
,
1231 RENAME_FLAG_RENAME
);
1234 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1236 return cli_ntrename_internal_recv(req
);
1239 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1241 TALLOC_CTX
*frame
= talloc_stackframe();
1242 struct tevent_context
*ev
;
1243 struct tevent_req
*req
;
1244 NTSTATUS status
= NT_STATUS_OK
;
1246 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1248 * Can't use sync call while an async call is in flight
1250 status
= NT_STATUS_INVALID_PARAMETER
;
1254 ev
= samba_tevent_context_init(frame
);
1256 status
= NT_STATUS_NO_MEMORY
;
1260 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1262 status
= NT_STATUS_NO_MEMORY
;
1266 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1270 status
= cli_ntrename_recv(req
);
1277 /****************************************************************************
1279 ****************************************************************************/
1281 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1282 struct tevent_context
*ev
,
1283 struct cli_state
*cli
,
1284 const char *fname_src
,
1285 const char *fname_dst
)
1287 return cli_ntrename_internal_send(mem_ctx
,
1292 RENAME_FLAG_HARD_LINK
);
1295 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1297 return cli_ntrename_internal_recv(req
);
1300 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1302 TALLOC_CTX
*frame
= talloc_stackframe();
1303 struct tevent_context
*ev
;
1304 struct tevent_req
*req
;
1305 NTSTATUS status
= NT_STATUS_OK
;
1307 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1309 * Can't use sync call while an async call is in flight
1311 status
= NT_STATUS_INVALID_PARAMETER
;
1315 ev
= samba_tevent_context_init(frame
);
1317 status
= NT_STATUS_NO_MEMORY
;
1321 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1323 status
= NT_STATUS_NO_MEMORY
;
1327 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1331 status
= cli_nt_hardlink_recv(req
);
1338 /****************************************************************************
1340 ****************************************************************************/
1342 static void cli_unlink_done(struct tevent_req
*subreq
);
1344 struct cli_unlink_state
{
1348 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1349 struct tevent_context
*ev
,
1350 struct cli_state
*cli
,
1352 uint16_t mayhave_attrs
)
1354 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1355 struct cli_unlink_state
*state
= NULL
;
1356 uint8_t additional_flags
= 0;
1357 uint8_t *bytes
= NULL
;
1359 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1364 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1366 bytes
= talloc_array(state
, uint8_t, 1);
1367 if (tevent_req_nomem(bytes
, req
)) {
1368 return tevent_req_post(req
, ev
);
1371 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
1372 strlen(fname
)+1, NULL
);
1374 if (tevent_req_nomem(bytes
, req
)) {
1375 return tevent_req_post(req
, ev
);
1378 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1379 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1380 if (tevent_req_nomem(subreq
, req
)) {
1381 return tevent_req_post(req
, ev
);
1383 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1387 static void cli_unlink_done(struct tevent_req
*subreq
)
1389 struct tevent_req
*req
= tevent_req_callback_data(
1390 subreq
, struct tevent_req
);
1393 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1394 TALLOC_FREE(subreq
);
1395 if (tevent_req_nterror(req
, status
)) {
1398 tevent_req_done(req
);
1401 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1403 return tevent_req_simple_recv_ntstatus(req
);
1406 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1408 TALLOC_CTX
*frame
= NULL
;
1409 struct tevent_context
*ev
;
1410 struct tevent_req
*req
;
1411 NTSTATUS status
= NT_STATUS_OK
;
1413 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1414 return cli_smb2_unlink(cli
, fname
);
1417 frame
= talloc_stackframe();
1419 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1421 * Can't use sync call while an async call is in flight
1423 status
= NT_STATUS_INVALID_PARAMETER
;
1427 ev
= samba_tevent_context_init(frame
);
1429 status
= NT_STATUS_NO_MEMORY
;
1433 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1435 status
= NT_STATUS_NO_MEMORY
;
1439 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1443 status
= cli_unlink_recv(req
);
1450 /****************************************************************************
1452 ****************************************************************************/
1454 static void cli_mkdir_done(struct tevent_req
*subreq
);
1456 struct cli_mkdir_state
{
1460 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1461 struct tevent_context
*ev
,
1462 struct cli_state
*cli
,
1465 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1466 struct cli_mkdir_state
*state
= NULL
;
1467 uint8_t additional_flags
= 0;
1468 uint8_t *bytes
= NULL
;
1470 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1475 bytes
= talloc_array(state
, uint8_t, 1);
1476 if (tevent_req_nomem(bytes
, req
)) {
1477 return tevent_req_post(req
, ev
);
1480 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1481 strlen(dname
)+1, NULL
);
1483 if (tevent_req_nomem(bytes
, req
)) {
1484 return tevent_req_post(req
, ev
);
1487 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1488 0, NULL
, talloc_get_size(bytes
), bytes
);
1489 if (tevent_req_nomem(subreq
, req
)) {
1490 return tevent_req_post(req
, ev
);
1492 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1496 static void cli_mkdir_done(struct tevent_req
*subreq
)
1498 struct tevent_req
*req
= tevent_req_callback_data(
1499 subreq
, struct tevent_req
);
1502 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1503 TALLOC_FREE(subreq
);
1504 if (tevent_req_nterror(req
, status
)) {
1507 tevent_req_done(req
);
1510 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1512 return tevent_req_simple_recv_ntstatus(req
);
1515 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1517 TALLOC_CTX
*frame
= NULL
;
1518 struct tevent_context
*ev
;
1519 struct tevent_req
*req
;
1520 NTSTATUS status
= NT_STATUS_OK
;
1522 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1523 return cli_smb2_mkdir(cli
, dname
);
1526 frame
= talloc_stackframe();
1528 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1530 * Can't use sync call while an async call is in flight
1532 status
= NT_STATUS_INVALID_PARAMETER
;
1536 ev
= samba_tevent_context_init(frame
);
1538 status
= NT_STATUS_NO_MEMORY
;
1542 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1544 status
= NT_STATUS_NO_MEMORY
;
1548 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1552 status
= cli_mkdir_recv(req
);
1559 /****************************************************************************
1561 ****************************************************************************/
1563 static void cli_rmdir_done(struct tevent_req
*subreq
);
1565 struct cli_rmdir_state
{
1569 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1570 struct tevent_context
*ev
,
1571 struct cli_state
*cli
,
1574 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1575 struct cli_rmdir_state
*state
= NULL
;
1576 uint8_t additional_flags
= 0;
1577 uint8_t *bytes
= NULL
;
1579 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1584 bytes
= talloc_array(state
, uint8_t, 1);
1585 if (tevent_req_nomem(bytes
, req
)) {
1586 return tevent_req_post(req
, ev
);
1589 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1590 strlen(dname
)+1, NULL
);
1592 if (tevent_req_nomem(bytes
, req
)) {
1593 return tevent_req_post(req
, ev
);
1596 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1597 0, NULL
, talloc_get_size(bytes
), bytes
);
1598 if (tevent_req_nomem(subreq
, req
)) {
1599 return tevent_req_post(req
, ev
);
1601 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1605 static void cli_rmdir_done(struct tevent_req
*subreq
)
1607 struct tevent_req
*req
= tevent_req_callback_data(
1608 subreq
, struct tevent_req
);
1611 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1612 TALLOC_FREE(subreq
);
1613 if (tevent_req_nterror(req
, status
)) {
1616 tevent_req_done(req
);
1619 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1621 return tevent_req_simple_recv_ntstatus(req
);
1624 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1626 TALLOC_CTX
*frame
= NULL
;
1627 struct tevent_context
*ev
;
1628 struct tevent_req
*req
;
1629 NTSTATUS status
= NT_STATUS_OK
;
1631 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1632 return cli_smb2_rmdir(cli
, dname
);
1635 frame
= talloc_stackframe();
1637 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1639 * Can't use sync call while an async call is in flight
1641 status
= NT_STATUS_INVALID_PARAMETER
;
1645 ev
= samba_tevent_context_init(frame
);
1647 status
= NT_STATUS_NO_MEMORY
;
1651 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1653 status
= NT_STATUS_NO_MEMORY
;
1657 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1661 status
= cli_rmdir_recv(req
);
1668 /****************************************************************************
1669 Set or clear the delete on close flag.
1670 ****************************************************************************/
1678 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1680 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1681 NULL
, 0, NULL
, NULL
, 0, NULL
);
1682 tevent_req_simple_finish_ntstatus(subreq
, status
);
1685 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1686 struct tevent_context
*ev
,
1687 struct cli_state
*cli
,
1691 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1692 struct doc_state
*state
= NULL
;
1694 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1699 /* Setup setup word. */
1700 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1702 /* Setup param array. */
1703 SSVAL(state
->param
,0,fnum
);
1704 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1706 /* Setup data array. */
1707 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1709 subreq
= cli_trans_send(state
, /* mem ctx. */
1710 ev
, /* event ctx. */
1711 cli
, /* cli_state. */
1712 SMBtrans2
, /* cmd. */
1713 NULL
, /* pipe name. */
1717 &state
->setup
, /* setup. */
1718 1, /* num setup uint16_t words. */
1719 0, /* max returned setup. */
1720 state
->param
, /* param. */
1722 2, /* max returned param. */
1723 state
->data
, /* data. */
1725 0); /* max returned data. */
1727 if (tevent_req_nomem(subreq
, req
)) {
1728 return tevent_req_post(req
, ev
);
1730 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1734 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1736 return tevent_req_simple_recv_ntstatus(req
);
1739 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1741 TALLOC_CTX
*frame
= talloc_stackframe();
1742 struct tevent_context
*ev
= NULL
;
1743 struct tevent_req
*req
= NULL
;
1744 NTSTATUS status
= NT_STATUS_OK
;
1746 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1748 * Can't use sync call while an async call is in flight
1750 status
= NT_STATUS_INVALID_PARAMETER
;
1754 ev
= samba_tevent_context_init(frame
);
1756 status
= NT_STATUS_NO_MEMORY
;
1760 req
= cli_nt_delete_on_close_send(frame
,
1766 status
= NT_STATUS_NO_MEMORY
;
1770 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
1774 status
= cli_nt_delete_on_close_recv(req
);
1781 struct cli_ntcreate1_state
{
1784 struct smb_create_returns cr
;
1785 struct tevent_req
*subreq
;
1788 static void cli_ntcreate1_done(struct tevent_req
*subreq
);
1789 static bool cli_ntcreate1_cancel(struct tevent_req
*req
);
1791 static struct tevent_req
*cli_ntcreate1_send(TALLOC_CTX
*mem_ctx
,
1792 struct tevent_context
*ev
,
1793 struct cli_state
*cli
,
1795 uint32_t CreatFlags
,
1796 uint32_t DesiredAccess
,
1797 uint32_t FileAttributes
,
1798 uint32_t ShareAccess
,
1799 uint32_t CreateDisposition
,
1800 uint32_t CreateOptions
,
1801 uint8_t SecurityFlags
)
1803 struct tevent_req
*req
, *subreq
;
1804 struct cli_ntcreate1_state
*state
;
1807 size_t converted_len
;
1809 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate1_state
);
1816 SCVAL(vwv
+0, 0, 0xFF);
1821 if (cli
->use_oplocks
) {
1822 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1824 SIVAL(vwv
+3, 1, CreatFlags
);
1825 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1826 SIVAL(vwv
+7, 1, DesiredAccess
);
1827 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1828 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1829 SIVAL(vwv
+13, 1, FileAttributes
);
1830 SIVAL(vwv
+15, 1, ShareAccess
);
1831 SIVAL(vwv
+17, 1, CreateDisposition
);
1832 SIVAL(vwv
+19, 1, CreateOptions
|
1833 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1834 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1835 SCVAL(vwv
+23, 1, SecurityFlags
);
1837 bytes
= talloc_array(state
, uint8_t, 0);
1838 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1839 fname
, strlen(fname
)+1,
1842 /* sigh. this copes with broken netapp filer behaviour */
1843 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1845 if (tevent_req_nomem(bytes
, req
)) {
1846 return tevent_req_post(req
, ev
);
1849 SSVAL(vwv
+2, 1, converted_len
);
1851 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1852 talloc_get_size(bytes
), bytes
);
1853 if (tevent_req_nomem(subreq
, req
)) {
1854 return tevent_req_post(req
, ev
);
1856 tevent_req_set_callback(subreq
, cli_ntcreate1_done
, req
);
1858 state
->subreq
= subreq
;
1859 tevent_req_set_cancel_fn(req
, cli_ntcreate1_cancel
);
1864 static void cli_ntcreate1_done(struct tevent_req
*subreq
)
1866 struct tevent_req
*req
= tevent_req_callback_data(
1867 subreq
, struct tevent_req
);
1868 struct cli_ntcreate1_state
*state
= tevent_req_data(
1869 req
, struct cli_ntcreate1_state
);
1876 status
= cli_smb_recv(subreq
, state
, NULL
, 34, &wct
, &vwv
,
1877 &num_bytes
, &bytes
);
1878 TALLOC_FREE(subreq
);
1879 if (tevent_req_nterror(req
, status
)) {
1882 state
->cr
.oplock_level
= CVAL(vwv
+2, 0);
1883 state
->fnum
= SVAL(vwv
+2, 1);
1884 state
->cr
.create_action
= IVAL(vwv
+3, 1);
1885 state
->cr
.creation_time
= BVAL(vwv
+5, 1);
1886 state
->cr
.last_access_time
= BVAL(vwv
+9, 1);
1887 state
->cr
.last_write_time
= BVAL(vwv
+13, 1);
1888 state
->cr
.change_time
= BVAL(vwv
+17, 1);
1889 state
->cr
.file_attributes
= IVAL(vwv
+21, 1);
1890 state
->cr
.allocation_size
= BVAL(vwv
+23, 1);
1891 state
->cr
.end_of_file
= BVAL(vwv
+27, 1);
1893 tevent_req_done(req
);
1896 static bool cli_ntcreate1_cancel(struct tevent_req
*req
)
1898 struct cli_ntcreate1_state
*state
= tevent_req_data(
1899 req
, struct cli_ntcreate1_state
);
1900 return tevent_req_cancel(state
->subreq
);
1903 static NTSTATUS
cli_ntcreate1_recv(struct tevent_req
*req
,
1905 struct smb_create_returns
*cr
)
1907 struct cli_ntcreate1_state
*state
= tevent_req_data(
1908 req
, struct cli_ntcreate1_state
);
1911 if (tevent_req_is_nterror(req
, &status
)) {
1914 *pfnum
= state
->fnum
;
1918 return NT_STATUS_OK
;
1921 struct cli_ntcreate_state
{
1922 NTSTATUS (*recv
)(struct tevent_req
*req
, uint16_t *fnum
,
1923 struct smb_create_returns
*cr
);
1924 struct smb_create_returns cr
;
1926 struct tevent_req
*subreq
;
1929 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1930 static bool cli_ntcreate_cancel(struct tevent_req
*req
);
1932 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1933 struct tevent_context
*ev
,
1934 struct cli_state
*cli
,
1936 uint32_t create_flags
,
1937 uint32_t desired_access
,
1938 uint32_t file_attributes
,
1939 uint32_t share_access
,
1940 uint32_t create_disposition
,
1941 uint32_t create_options
,
1942 uint8_t security_flags
)
1944 struct tevent_req
*req
, *subreq
;
1945 struct cli_ntcreate_state
*state
;
1947 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1952 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1953 state
->recv
= cli_smb2_create_fnum_recv
;
1955 if (cli
->use_oplocks
) {
1956 create_flags
|= REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
;
1959 subreq
= cli_smb2_create_fnum_send(
1960 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1961 file_attributes
, share_access
, create_disposition
,
1964 state
->recv
= cli_ntcreate1_recv
;
1965 subreq
= cli_ntcreate1_send(
1966 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1967 file_attributes
, share_access
, create_disposition
,
1968 create_options
, security_flags
);
1970 if (tevent_req_nomem(subreq
, req
)) {
1971 return tevent_req_post(req
, ev
);
1973 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1975 state
->subreq
= subreq
;
1976 tevent_req_set_cancel_fn(req
, cli_ntcreate_cancel
);
1981 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1983 struct tevent_req
*req
= tevent_req_callback_data(
1984 subreq
, struct tevent_req
);
1985 struct cli_ntcreate_state
*state
= tevent_req_data(
1986 req
, struct cli_ntcreate_state
);
1989 status
= state
->recv(subreq
, &state
->fnum
, &state
->cr
);
1990 TALLOC_FREE(subreq
);
1991 if (tevent_req_nterror(req
, status
)) {
1994 tevent_req_done(req
);
1997 static bool cli_ntcreate_cancel(struct tevent_req
*req
)
1999 struct cli_ntcreate_state
*state
= tevent_req_data(
2000 req
, struct cli_ntcreate_state
);
2001 return tevent_req_cancel(state
->subreq
);
2004 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *fnum
,
2005 struct smb_create_returns
*cr
)
2007 struct cli_ntcreate_state
*state
= tevent_req_data(
2008 req
, struct cli_ntcreate_state
);
2011 if (tevent_req_is_nterror(req
, &status
)) {
2015 *fnum
= state
->fnum
;
2020 return NT_STATUS_OK
;
2023 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2025 uint32_t CreatFlags
,
2026 uint32_t DesiredAccess
,
2027 uint32_t FileAttributes
,
2028 uint32_t ShareAccess
,
2029 uint32_t CreateDisposition
,
2030 uint32_t CreateOptions
,
2031 uint8_t SecurityFlags
,
2033 struct smb_create_returns
*cr
)
2035 TALLOC_CTX
*frame
= talloc_stackframe();
2036 struct tevent_context
*ev
;
2037 struct tevent_req
*req
;
2038 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2040 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2042 * Can't use sync call while an async call is in flight
2044 status
= NT_STATUS_INVALID_PARAMETER
;
2048 ev
= samba_tevent_context_init(frame
);
2053 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2054 DesiredAccess
, FileAttributes
, ShareAccess
,
2055 CreateDisposition
, CreateOptions
,
2061 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2065 status
= cli_ntcreate_recv(req
, pfid
, cr
);
2071 struct cli_nttrans_create_state
{
2073 struct smb_create_returns cr
;
2076 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
2078 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
2079 struct tevent_context
*ev
,
2080 struct cli_state
*cli
,
2082 uint32_t CreatFlags
,
2083 uint32_t DesiredAccess
,
2084 uint32_t FileAttributes
,
2085 uint32_t ShareAccess
,
2086 uint32_t CreateDisposition
,
2087 uint32_t CreateOptions
,
2088 uint8_t SecurityFlags
,
2089 struct security_descriptor
*secdesc
,
2090 struct ea_struct
*eas
,
2093 struct tevent_req
*req
, *subreq
;
2094 struct cli_nttrans_create_state
*state
;
2096 uint8_t *secdesc_buf
;
2099 size_t converted_len
;
2101 req
= tevent_req_create(mem_ctx
,
2102 &state
, struct cli_nttrans_create_state
);
2107 if (secdesc
!= NULL
) {
2108 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2109 &secdesc_buf
, &secdesc_len
);
2110 if (tevent_req_nterror(req
, status
)) {
2111 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2112 nt_errstr(status
)));
2113 return tevent_req_post(req
, ev
);
2124 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2125 return tevent_req_post(req
, ev
);
2128 param
= talloc_array(state
, uint8_t, 53);
2129 if (tevent_req_nomem(param
, req
)) {
2130 return tevent_req_post(req
, ev
);
2133 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2134 fname
, strlen(fname
),
2136 if (tevent_req_nomem(param
, req
)) {
2137 return tevent_req_post(req
, ev
);
2140 SIVAL(param
, 0, CreatFlags
);
2141 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2142 SIVAL(param
, 8, DesiredAccess
);
2143 SIVAL(param
, 12, 0x0); /* AllocationSize */
2144 SIVAL(param
, 16, 0x0); /* AllocationSize */
2145 SIVAL(param
, 20, FileAttributes
);
2146 SIVAL(param
, 24, ShareAccess
);
2147 SIVAL(param
, 28, CreateDisposition
);
2148 SIVAL(param
, 32, CreateOptions
|
2149 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2150 SIVAL(param
, 36, secdesc_len
);
2151 SIVAL(param
, 40, 0); /* EA length*/
2152 SIVAL(param
, 44, converted_len
);
2153 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2154 SCVAL(param
, 52, SecurityFlags
);
2156 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2157 NULL
, -1, /* name, fid */
2158 NT_TRANSACT_CREATE
, 0,
2159 NULL
, 0, 0, /* setup */
2160 param
, talloc_get_size(param
), 128, /* param */
2161 secdesc_buf
, secdesc_len
, 0); /* data */
2162 if (tevent_req_nomem(subreq
, req
)) {
2163 return tevent_req_post(req
, ev
);
2165 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2169 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2171 struct tevent_req
*req
= tevent_req_callback_data(
2172 subreq
, struct tevent_req
);
2173 struct cli_nttrans_create_state
*state
= tevent_req_data(
2174 req
, struct cli_nttrans_create_state
);
2179 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2180 NULL
, 0, NULL
, /* rsetup */
2181 ¶m
, 69, &num_param
,
2183 if (tevent_req_nterror(req
, status
)) {
2186 state
->cr
.oplock_level
= CVAL(param
, 0);
2187 state
->fnum
= SVAL(param
, 2);
2188 state
->cr
.create_action
= IVAL(param
, 4);
2189 state
->cr
.creation_time
= BVAL(param
, 12);
2190 state
->cr
.last_access_time
= BVAL(param
, 20);
2191 state
->cr
.last_write_time
= BVAL(param
, 28);
2192 state
->cr
.change_time
= BVAL(param
, 36);
2193 state
->cr
.file_attributes
= IVAL(param
, 44);
2194 state
->cr
.allocation_size
= BVAL(param
, 48);
2195 state
->cr
.end_of_file
= BVAL(param
, 56);
2198 tevent_req_done(req
);
2201 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2203 struct smb_create_returns
*cr
)
2205 struct cli_nttrans_create_state
*state
= tevent_req_data(
2206 req
, struct cli_nttrans_create_state
);
2209 if (tevent_req_is_nterror(req
, &status
)) {
2212 *fnum
= state
->fnum
;
2216 return NT_STATUS_OK
;
2219 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2221 uint32_t CreatFlags
,
2222 uint32_t DesiredAccess
,
2223 uint32_t FileAttributes
,
2224 uint32_t ShareAccess
,
2225 uint32_t CreateDisposition
,
2226 uint32_t CreateOptions
,
2227 uint8_t SecurityFlags
,
2228 struct security_descriptor
*secdesc
,
2229 struct ea_struct
*eas
,
2232 struct smb_create_returns
*cr
)
2234 TALLOC_CTX
*frame
= talloc_stackframe();
2235 struct tevent_context
*ev
;
2236 struct tevent_req
*req
;
2237 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2239 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2241 * Can't use sync call while an async call is in flight
2243 status
= NT_STATUS_INVALID_PARAMETER
;
2246 ev
= samba_tevent_context_init(frame
);
2250 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2251 DesiredAccess
, FileAttributes
,
2252 ShareAccess
, CreateDisposition
,
2253 CreateOptions
, SecurityFlags
,
2254 secdesc
, eas
, num_eas
);
2258 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2261 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2267 /****************************************************************************
2269 WARNING: if you open with O_WRONLY then getattrE won't work!
2270 ****************************************************************************/
2272 struct cli_openx_state
{
2279 static void cli_openx_done(struct tevent_req
*subreq
);
2281 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2282 struct tevent_context
*ev
,
2283 struct cli_state
*cli
, const char *fname
,
2284 int flags
, int share_mode
,
2285 struct tevent_req
**psmbreq
)
2287 struct tevent_req
*req
, *subreq
;
2288 struct cli_openx_state
*state
;
2290 unsigned accessmode
;
2291 uint8_t additional_flags
;
2294 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2300 if (flags
& O_CREAT
) {
2303 if (!(flags
& O_EXCL
)) {
2304 if (flags
& O_TRUNC
)
2310 accessmode
= (share_mode
<<4);
2312 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2314 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2319 if ((flags
& O_SYNC
) == O_SYNC
) {
2320 accessmode
|= (1<<14);
2324 if (share_mode
== DENY_FCB
) {
2328 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2329 SCVAL(state
->vwv
+ 0, 1, 0);
2330 SSVAL(state
->vwv
+ 1, 0, 0);
2331 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2332 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2333 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2334 SSVAL(state
->vwv
+ 5, 0, 0);
2335 SIVAL(state
->vwv
+ 6, 0, 0);
2336 SSVAL(state
->vwv
+ 8, 0, openfn
);
2337 SIVAL(state
->vwv
+ 9, 0, 0);
2338 SIVAL(state
->vwv
+ 11, 0, 0);
2339 SIVAL(state
->vwv
+ 13, 0, 0);
2341 additional_flags
= 0;
2343 if (cli
->use_oplocks
) {
2344 /* if using oplocks then ask for a batch oplock via
2345 core and extended methods */
2347 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2348 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2351 bytes
= talloc_array(state
, uint8_t, 0);
2352 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2353 strlen(fname
)+1, NULL
);
2355 if (tevent_req_nomem(bytes
, req
)) {
2356 return tevent_req_post(req
, ev
);
2359 state
->bytes
.iov_base
= (void *)bytes
;
2360 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2362 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2363 15, state
->vwv
, 1, &state
->bytes
);
2364 if (subreq
== NULL
) {
2368 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2373 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2374 struct cli_state
*cli
, const char *fname
,
2375 int flags
, int share_mode
)
2377 struct tevent_req
*req
, *subreq
;
2380 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2386 status
= smb1cli_req_chain_submit(&subreq
, 1);
2387 if (tevent_req_nterror(req
, status
)) {
2388 return tevent_req_post(req
, ev
);
2393 static void cli_openx_done(struct tevent_req
*subreq
)
2395 struct tevent_req
*req
= tevent_req_callback_data(
2396 subreq
, struct tevent_req
);
2397 struct cli_openx_state
*state
= tevent_req_data(
2398 req
, struct cli_openx_state
);
2403 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2405 TALLOC_FREE(subreq
);
2406 if (tevent_req_nterror(req
, status
)) {
2409 state
->fnum
= SVAL(vwv
+2, 0);
2410 tevent_req_done(req
);
2413 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2415 struct cli_openx_state
*state
= tevent_req_data(
2416 req
, struct cli_openx_state
);
2419 if (tevent_req_is_nterror(req
, &status
)) {
2422 *pfnum
= state
->fnum
;
2423 return NT_STATUS_OK
;
2426 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2427 int share_mode
, uint16_t *pfnum
)
2429 TALLOC_CTX
*frame
= talloc_stackframe();
2430 struct tevent_context
*ev
;
2431 struct tevent_req
*req
;
2432 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2434 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2436 * Can't use sync call while an async call is in flight
2438 status
= NT_STATUS_INVALID_PARAMETER
;
2442 ev
= samba_tevent_context_init(frame
);
2447 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2452 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2456 status
= cli_openx_recv(req
, pfnum
);
2461 /****************************************************************************
2462 Synchronous wrapper function that does an NtCreateX open by preference
2463 and falls back to openX if this fails.
2464 ****************************************************************************/
2466 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2467 int share_mode_in
, uint16_t *pfnum
)
2470 unsigned int openfn
= 0;
2471 unsigned int dos_deny
= 0;
2472 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2473 struct smb_create_returns cr
;
2475 /* Do the initial mapping into OpenX parameters. */
2476 if (flags
& O_CREAT
) {
2479 if (!(flags
& O_EXCL
)) {
2480 if (flags
& O_TRUNC
)
2486 dos_deny
= (share_mode_in
<<4);
2488 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2490 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2495 if ((flags
& O_SYNC
) == O_SYNC
) {
2496 dos_deny
|= (1<<14);
2500 if (share_mode_in
== DENY_FCB
) {
2505 /* Hmmm. This is what I think the above code
2506 should look like if it's using the constants
2509 if (flags
& O_CREAT
) {
2510 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2512 if (!(flags
& O_EXCL
)) {
2513 if (flags
& O_TRUNC
)
2514 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2516 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2519 dos_deny
= SET_DENY_MODE(share_mode_in
);
2521 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2522 dos_deny
|= DOS_OPEN_RDWR
;
2523 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2524 dos_deny
|= DOS_OPEN_WRONLY
;
2528 if ((flags
& O_SYNC
) == O_SYNC
) {
2529 dos_deny
|= FILE_SYNC_OPENMODE
;
2533 if (share_mode_in
== DENY_FCB
) {
2538 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2539 openfn
, &access_mask
,
2540 &share_mode
, &create_disposition
,
2541 &create_options
, NULL
)) {
2545 status
= cli_ntcreate(cli
,
2557 /* Try and cope will all varients of "we don't do this call"
2558 and fall back to openX. */
2560 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2561 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2562 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2563 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2564 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2565 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2566 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2567 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2568 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2572 if (NT_STATUS_IS_OK(status
) &&
2573 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2574 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2577 * Some (broken) servers return a valid handle
2578 * for directories even if FILE_NON_DIRECTORY_FILE
2579 * is set. Just close the handle and set the
2580 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2582 status
= cli_close(cli
, *pfnum
);
2583 if (!NT_STATUS_IS_OK(status
)) {
2586 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2587 /* Set this so libsmbclient can retrieve it. */
2588 cli
->raw_status
= status
;
2595 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2598 /****************************************************************************
2600 ****************************************************************************/
2602 struct cli_close_state
{
2606 static void cli_close_done(struct tevent_req
*subreq
);
2608 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2609 struct tevent_context
*ev
,
2610 struct cli_state
*cli
,
2612 struct tevent_req
**psubreq
)
2614 struct tevent_req
*req
, *subreq
;
2615 struct cli_close_state
*state
;
2617 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2622 SSVAL(state
->vwv
+0, 0, fnum
);
2623 SIVALS(state
->vwv
+1, 0, -1);
2625 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2627 if (subreq
== NULL
) {
2631 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2636 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2637 struct tevent_context
*ev
,
2638 struct cli_state
*cli
,
2641 struct tevent_req
*req
, *subreq
;
2644 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2649 status
= smb1cli_req_chain_submit(&subreq
, 1);
2650 if (tevent_req_nterror(req
, status
)) {
2651 return tevent_req_post(req
, ev
);
2656 static void cli_close_done(struct tevent_req
*subreq
)
2658 struct tevent_req
*req
= tevent_req_callback_data(
2659 subreq
, struct tevent_req
);
2662 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2663 TALLOC_FREE(subreq
);
2664 if (tevent_req_nterror(req
, status
)) {
2667 tevent_req_done(req
);
2670 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2672 return tevent_req_simple_recv_ntstatus(req
);
2675 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2677 TALLOC_CTX
*frame
= NULL
;
2678 struct tevent_context
*ev
;
2679 struct tevent_req
*req
;
2680 NTSTATUS status
= NT_STATUS_OK
;
2682 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2683 return cli_smb2_close_fnum(cli
, fnum
);
2686 frame
= talloc_stackframe();
2688 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2690 * Can't use sync call while an async call is in flight
2692 status
= NT_STATUS_INVALID_PARAMETER
;
2696 ev
= samba_tevent_context_init(frame
);
2698 status
= NT_STATUS_NO_MEMORY
;
2702 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2704 status
= NT_STATUS_NO_MEMORY
;
2708 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2712 status
= cli_close_recv(req
);
2718 /****************************************************************************
2719 Truncate a file to a specified size
2720 ****************************************************************************/
2722 struct ftrunc_state
{
2728 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2730 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2731 NULL
, 0, NULL
, NULL
, 0, NULL
);
2732 tevent_req_simple_finish_ntstatus(subreq
, status
);
2735 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2736 struct tevent_context
*ev
,
2737 struct cli_state
*cli
,
2741 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2742 struct ftrunc_state
*state
= NULL
;
2744 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2749 /* Setup setup word. */
2750 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2752 /* Setup param array. */
2753 SSVAL(state
->param
,0,fnum
);
2754 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2755 SSVAL(state
->param
,4,0);
2757 /* Setup data array. */
2758 SBVAL(state
->data
, 0, size
);
2760 subreq
= cli_trans_send(state
, /* mem ctx. */
2761 ev
, /* event ctx. */
2762 cli
, /* cli_state. */
2763 SMBtrans2
, /* cmd. */
2764 NULL
, /* pipe name. */
2768 &state
->setup
, /* setup. */
2769 1, /* num setup uint16_t words. */
2770 0, /* max returned setup. */
2771 state
->param
, /* param. */
2773 2, /* max returned param. */
2774 state
->data
, /* data. */
2776 0); /* max returned data. */
2778 if (tevent_req_nomem(subreq
, req
)) {
2779 return tevent_req_post(req
, ev
);
2781 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2785 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2787 return tevent_req_simple_recv_ntstatus(req
);
2790 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2792 TALLOC_CTX
*frame
= talloc_stackframe();
2793 struct tevent_context
*ev
= NULL
;
2794 struct tevent_req
*req
= NULL
;
2795 NTSTATUS status
= NT_STATUS_OK
;
2797 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2799 * Can't use sync call while an async call is in flight
2801 status
= NT_STATUS_INVALID_PARAMETER
;
2805 ev
= samba_tevent_context_init(frame
);
2807 status
= NT_STATUS_NO_MEMORY
;
2811 req
= cli_ftruncate_send(frame
,
2817 status
= NT_STATUS_NO_MEMORY
;
2821 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2825 status
= cli_ftruncate_recv(req
);
2832 /****************************************************************************
2833 send a lock with a specified locktype
2834 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2835 ****************************************************************************/
2837 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2838 uint32_t offset
, uint32_t len
,
2839 int timeout
, unsigned char locktype
)
2844 unsigned int set_timeout
= 0;
2845 unsigned int saved_timeout
= 0;
2847 SCVAL(vwv
+ 0, 0, 0xff);
2848 SCVAL(vwv
+ 0, 1, 0);
2849 SSVAL(vwv
+ 1, 0, 0);
2850 SSVAL(vwv
+ 2, 0, fnum
);
2851 SCVAL(vwv
+ 3, 0, locktype
);
2852 SCVAL(vwv
+ 3, 1, 0);
2853 SIVALS(vwv
+ 4, 0, timeout
);
2854 SSVAL(vwv
+ 6, 0, 0);
2855 SSVAL(vwv
+ 7, 0, 1);
2857 SSVAL(bytes
, 0, cli_getpid(cli
));
2858 SIVAL(bytes
, 2, offset
);
2859 SIVAL(bytes
, 6, len
);
2862 if (timeout
== -1) {
2863 set_timeout
= 0x7FFFFFFF;
2865 set_timeout
= timeout
+ 2*1000;
2867 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2870 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2871 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2873 if (saved_timeout
!= 0) {
2874 cli_set_timeout(cli
, saved_timeout
);
2880 /****************************************************************************
2882 note that timeout is in units of 2 milliseconds
2883 ****************************************************************************/
2885 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2886 uint32_t offset
, uint32_t len
, int timeout
,
2887 enum brl_type lock_type
)
2891 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2892 (lock_type
== READ_LOCK
? 1 : 0));
2896 /****************************************************************************
2898 ****************************************************************************/
2900 struct cli_unlock_state
{
2905 static void cli_unlock_done(struct tevent_req
*subreq
);
2907 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2908 struct tevent_context
*ev
,
2909 struct cli_state
*cli
,
2915 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2916 struct cli_unlock_state
*state
= NULL
;
2917 uint8_t additional_flags
= 0;
2919 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2924 SCVAL(state
->vwv
+0, 0, 0xFF);
2925 SSVAL(state
->vwv
+2, 0, fnum
);
2926 SCVAL(state
->vwv
+3, 0, 0);
2927 SIVALS(state
->vwv
+4, 0, 0);
2928 SSVAL(state
->vwv
+6, 0, 1);
2929 SSVAL(state
->vwv
+7, 0, 0);
2931 SSVAL(state
->data
, 0, cli_getpid(cli
));
2932 SIVAL(state
->data
, 2, offset
);
2933 SIVAL(state
->data
, 6, len
);
2935 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2936 8, state
->vwv
, 10, state
->data
);
2937 if (tevent_req_nomem(subreq
, req
)) {
2938 return tevent_req_post(req
, ev
);
2940 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2944 static void cli_unlock_done(struct tevent_req
*subreq
)
2946 struct tevent_req
*req
= tevent_req_callback_data(
2947 subreq
, struct tevent_req
);
2950 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2951 TALLOC_FREE(subreq
);
2952 if (tevent_req_nterror(req
, status
)) {
2955 tevent_req_done(req
);
2958 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2960 return tevent_req_simple_recv_ntstatus(req
);
2963 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2968 TALLOC_CTX
*frame
= talloc_stackframe();
2969 struct tevent_context
*ev
;
2970 struct tevent_req
*req
;
2971 NTSTATUS status
= NT_STATUS_OK
;
2973 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2975 * Can't use sync call while an async call is in flight
2977 status
= NT_STATUS_INVALID_PARAMETER
;
2981 ev
= samba_tevent_context_init(frame
);
2983 status
= NT_STATUS_NO_MEMORY
;
2987 req
= cli_unlock_send(frame
, ev
, cli
,
2990 status
= NT_STATUS_NO_MEMORY
;
2994 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2998 status
= cli_unlock_recv(req
);
3005 /****************************************************************************
3006 Lock a file with 64 bit offsets.
3007 ****************************************************************************/
3009 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
3010 uint64_t offset
, uint64_t len
, int timeout
,
3011 enum brl_type lock_type
)
3015 unsigned int set_timeout
= 0;
3016 unsigned int saved_timeout
= 0;
3020 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3021 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
3024 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
3025 ltype
|= LOCKING_ANDX_LARGE_FILES
;
3027 SCVAL(vwv
+ 0, 0, 0xff);
3028 SCVAL(vwv
+ 0, 1, 0);
3029 SSVAL(vwv
+ 1, 0, 0);
3030 SSVAL(vwv
+ 2, 0, fnum
);
3031 SCVAL(vwv
+ 3, 0, ltype
);
3032 SCVAL(vwv
+ 3, 1, 0);
3033 SIVALS(vwv
+ 4, 0, timeout
);
3034 SSVAL(vwv
+ 6, 0, 0);
3035 SSVAL(vwv
+ 7, 0, 1);
3037 SIVAL(bytes
, 0, cli_getpid(cli
));
3038 SOFF_T_R(bytes
, 4, offset
);
3039 SOFF_T_R(bytes
, 12, len
);
3042 if (timeout
== -1) {
3043 set_timeout
= 0x7FFFFFFF;
3045 set_timeout
= timeout
+ 2*1000;
3047 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3050 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3051 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3053 if (saved_timeout
!= 0) {
3054 cli_set_timeout(cli
, saved_timeout
);
3060 /****************************************************************************
3061 Unlock a file with 64 bit offsets.
3062 ****************************************************************************/
3064 struct cli_unlock64_state
{
3069 static void cli_unlock64_done(struct tevent_req
*subreq
);
3071 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
3072 struct tevent_context
*ev
,
3073 struct cli_state
*cli
,
3079 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3080 struct cli_unlock64_state
*state
= NULL
;
3081 uint8_t additional_flags
= 0;
3083 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3088 SCVAL(state
->vwv
+0, 0, 0xff);
3089 SSVAL(state
->vwv
+2, 0, fnum
);
3090 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3091 SIVALS(state
->vwv
+4, 0, 0);
3092 SSVAL(state
->vwv
+6, 0, 1);
3093 SSVAL(state
->vwv
+7, 0, 0);
3095 SIVAL(state
->data
, 0, cli_getpid(cli
));
3096 SOFF_T_R(state
->data
, 4, offset
);
3097 SOFF_T_R(state
->data
, 12, len
);
3099 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
3100 8, state
->vwv
, 20, state
->data
);
3101 if (tevent_req_nomem(subreq
, req
)) {
3102 return tevent_req_post(req
, ev
);
3104 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3108 static void cli_unlock64_done(struct tevent_req
*subreq
)
3110 struct tevent_req
*req
= tevent_req_callback_data(
3111 subreq
, struct tevent_req
);
3114 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3115 TALLOC_FREE(subreq
);
3116 if (tevent_req_nterror(req
, status
)) {
3119 tevent_req_done(req
);
3122 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3124 return tevent_req_simple_recv_ntstatus(req
);
3127 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3132 TALLOC_CTX
*frame
= talloc_stackframe();
3133 struct tevent_context
*ev
;
3134 struct tevent_req
*req
;
3135 NTSTATUS status
= NT_STATUS_OK
;
3137 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3138 return cli_unlock(cli
, fnum
, offset
, len
);
3141 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3143 * Can't use sync call while an async call is in flight
3145 status
= NT_STATUS_INVALID_PARAMETER
;
3149 ev
= samba_tevent_context_init(frame
);
3151 status
= NT_STATUS_NO_MEMORY
;
3155 req
= cli_unlock64_send(frame
, ev
, cli
,
3158 status
= NT_STATUS_NO_MEMORY
;
3162 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3166 status
= cli_unlock64_recv(req
);
3173 /****************************************************************************
3174 Get/unlock a POSIX lock on a file - internal function.
3175 ****************************************************************************/
3177 struct posix_lock_state
{
3180 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3183 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3185 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3186 NULL
, 0, NULL
, NULL
, 0, NULL
);
3187 tevent_req_simple_finish_ntstatus(subreq
, status
);
3190 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3191 struct tevent_context
*ev
,
3192 struct cli_state
*cli
,
3197 enum brl_type lock_type
)
3199 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3200 struct posix_lock_state
*state
= NULL
;
3202 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3207 /* Setup setup word. */
3208 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3210 /* Setup param array. */
3211 SSVAL(&state
->param
, 0, fnum
);
3212 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3214 /* Setup data array. */
3215 switch (lock_type
) {
3217 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3218 POSIX_LOCK_TYPE_READ
);
3221 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3222 POSIX_LOCK_TYPE_WRITE
);
3225 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3226 POSIX_LOCK_TYPE_UNLOCK
);
3233 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3234 POSIX_LOCK_FLAG_WAIT
);
3236 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3237 POSIX_LOCK_FLAG_NOWAIT
);
3240 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3241 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3242 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3244 subreq
= cli_trans_send(state
, /* mem ctx. */
3245 ev
, /* event ctx. */
3246 cli
, /* cli_state. */
3247 SMBtrans2
, /* cmd. */
3248 NULL
, /* pipe name. */
3252 &state
->setup
, /* setup. */
3253 1, /* num setup uint16_t words. */
3254 0, /* max returned setup. */
3255 state
->param
, /* param. */
3257 2, /* max returned param. */
3258 state
->data
, /* data. */
3259 POSIX_LOCK_DATA_SIZE
, /* num data. */
3260 0); /* max returned data. */
3262 if (tevent_req_nomem(subreq
, req
)) {
3263 return tevent_req_post(req
, ev
);
3265 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3269 /****************************************************************************
3271 ****************************************************************************/
3273 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3274 struct tevent_context
*ev
,
3275 struct cli_state
*cli
,
3280 enum brl_type lock_type
)
3282 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3283 wait_lock
, lock_type
);
3286 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3288 return tevent_req_simple_recv_ntstatus(req
);
3291 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3292 uint64_t offset
, uint64_t len
,
3293 bool wait_lock
, enum brl_type lock_type
)
3295 TALLOC_CTX
*frame
= talloc_stackframe();
3296 struct tevent_context
*ev
= NULL
;
3297 struct tevent_req
*req
= NULL
;
3298 NTSTATUS status
= NT_STATUS_OK
;
3300 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3302 * Can't use sync call while an async call is in flight
3304 status
= NT_STATUS_INVALID_PARAMETER
;
3308 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3309 status
= NT_STATUS_INVALID_PARAMETER
;
3313 ev
= samba_tevent_context_init(frame
);
3315 status
= NT_STATUS_NO_MEMORY
;
3319 req
= cli_posix_lock_send(frame
,
3328 status
= NT_STATUS_NO_MEMORY
;
3332 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3336 status
= cli_posix_lock_recv(req
);
3343 /****************************************************************************
3344 POSIX Unlock a file.
3345 ****************************************************************************/
3347 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3348 struct tevent_context
*ev
,
3349 struct cli_state
*cli
,
3354 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3355 false, UNLOCK_LOCK
);
3358 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3360 return tevent_req_simple_recv_ntstatus(req
);
3363 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3365 TALLOC_CTX
*frame
= talloc_stackframe();
3366 struct tevent_context
*ev
= NULL
;
3367 struct tevent_req
*req
= NULL
;
3368 NTSTATUS status
= NT_STATUS_OK
;
3370 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3372 * Can't use sync call while an async call is in flight
3374 status
= NT_STATUS_INVALID_PARAMETER
;
3378 ev
= samba_tevent_context_init(frame
);
3380 status
= NT_STATUS_NO_MEMORY
;
3384 req
= cli_posix_unlock_send(frame
,
3391 status
= NT_STATUS_NO_MEMORY
;
3395 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3399 status
= cli_posix_unlock_recv(req
);
3406 /****************************************************************************
3407 Do a SMBgetattrE call.
3408 ****************************************************************************/
3410 static void cli_getattrE_done(struct tevent_req
*subreq
);
3412 struct cli_getattrE_state
{
3422 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3423 struct tevent_context
*ev
,
3424 struct cli_state
*cli
,
3427 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3428 struct cli_getattrE_state
*state
= NULL
;
3429 uint8_t additional_flags
= 0;
3431 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3436 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3437 SSVAL(state
->vwv
+0,0,fnum
);
3439 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3440 1, state
->vwv
, 0, NULL
);
3441 if (tevent_req_nomem(subreq
, req
)) {
3442 return tevent_req_post(req
, ev
);
3444 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3448 static void cli_getattrE_done(struct tevent_req
*subreq
)
3450 struct tevent_req
*req
= tevent_req_callback_data(
3451 subreq
, struct tevent_req
);
3452 struct cli_getattrE_state
*state
= tevent_req_data(
3453 req
, struct cli_getattrE_state
);
3455 uint16_t *vwv
= NULL
;
3458 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3460 TALLOC_FREE(subreq
);
3461 if (tevent_req_nterror(req
, status
)) {
3465 state
->size
= (off_t
)IVAL(vwv
+6,0);
3466 state
->attr
= SVAL(vwv
+10,0);
3467 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3468 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3469 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3471 tevent_req_done(req
);
3474 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3477 time_t *change_time
,
3478 time_t *access_time
,
3481 struct cli_getattrE_state
*state
= tevent_req_data(
3482 req
, struct cli_getattrE_state
);
3485 if (tevent_req_is_nterror(req
, &status
)) {
3489 *attr
= state
->attr
;
3492 *size
= state
->size
;
3495 *change_time
= state
->change_time
;
3498 *access_time
= state
->access_time
;
3501 *write_time
= state
->write_time
;
3503 return NT_STATUS_OK
;
3506 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3510 time_t *change_time
,
3511 time_t *access_time
,
3514 TALLOC_CTX
*frame
= NULL
;
3515 struct tevent_context
*ev
= NULL
;
3516 struct tevent_req
*req
= NULL
;
3517 NTSTATUS status
= NT_STATUS_OK
;
3519 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3520 return cli_smb2_getattrE(cli
,
3529 frame
= talloc_stackframe();
3531 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3533 * Can't use sync call while an async call is in flight
3535 status
= NT_STATUS_INVALID_PARAMETER
;
3539 ev
= samba_tevent_context_init(frame
);
3541 status
= NT_STATUS_NO_MEMORY
;
3545 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3547 status
= NT_STATUS_NO_MEMORY
;
3551 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3555 status
= cli_getattrE_recv(req
,
3567 /****************************************************************************
3569 ****************************************************************************/
3571 static void cli_getatr_done(struct tevent_req
*subreq
);
3573 struct cli_getatr_state
{
3580 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3581 struct tevent_context
*ev
,
3582 struct cli_state
*cli
,
3585 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3586 struct cli_getatr_state
*state
= NULL
;
3587 uint8_t additional_flags
= 0;
3588 uint8_t *bytes
= NULL
;
3590 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3595 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3597 bytes
= talloc_array(state
, uint8_t, 1);
3598 if (tevent_req_nomem(bytes
, req
)) {
3599 return tevent_req_post(req
, ev
);
3602 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3603 strlen(fname
)+1, NULL
);
3605 if (tevent_req_nomem(bytes
, req
)) {
3606 return tevent_req_post(req
, ev
);
3609 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3610 0, NULL
, talloc_get_size(bytes
), bytes
);
3611 if (tevent_req_nomem(subreq
, req
)) {
3612 return tevent_req_post(req
, ev
);
3614 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3618 static void cli_getatr_done(struct tevent_req
*subreq
)
3620 struct tevent_req
*req
= tevent_req_callback_data(
3621 subreq
, struct tevent_req
);
3622 struct cli_getatr_state
*state
= tevent_req_data(
3623 req
, struct cli_getatr_state
);
3625 uint16_t *vwv
= NULL
;
3628 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3630 TALLOC_FREE(subreq
);
3631 if (tevent_req_nterror(req
, status
)) {
3635 state
->attr
= SVAL(vwv
+0,0);
3636 state
->size
= (off_t
)IVAL(vwv
+3,0);
3637 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3639 tevent_req_done(req
);
3642 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3647 struct cli_getatr_state
*state
= tevent_req_data(
3648 req
, struct cli_getatr_state
);
3651 if (tevent_req_is_nterror(req
, &status
)) {
3655 *attr
= state
->attr
;
3658 *size
= state
->size
;
3661 *write_time
= state
->write_time
;
3663 return NT_STATUS_OK
;
3666 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3672 TALLOC_CTX
*frame
= NULL
;
3673 struct tevent_context
*ev
= NULL
;
3674 struct tevent_req
*req
= NULL
;
3675 NTSTATUS status
= NT_STATUS_OK
;
3677 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3678 return cli_smb2_getatr(cli
,
3685 frame
= talloc_stackframe();
3687 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3689 * Can't use sync call while an async call is in flight
3691 status
= NT_STATUS_INVALID_PARAMETER
;
3695 ev
= samba_tevent_context_init(frame
);
3697 status
= NT_STATUS_NO_MEMORY
;
3701 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3703 status
= NT_STATUS_NO_MEMORY
;
3707 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3711 status
= cli_getatr_recv(req
,
3721 /****************************************************************************
3722 Do a SMBsetattrE call.
3723 ****************************************************************************/
3725 static void cli_setattrE_done(struct tevent_req
*subreq
);
3727 struct cli_setattrE_state
{
3731 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3732 struct tevent_context
*ev
,
3733 struct cli_state
*cli
,
3739 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3740 struct cli_setattrE_state
*state
= NULL
;
3741 uint8_t additional_flags
= 0;
3743 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3748 SSVAL(state
->vwv
+0, 0, fnum
);
3749 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3750 smb1cli_conn_server_time_zone(cli
->conn
));
3751 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3752 smb1cli_conn_server_time_zone(cli
->conn
));
3753 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3754 smb1cli_conn_server_time_zone(cli
->conn
));
3756 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3757 7, state
->vwv
, 0, NULL
);
3758 if (tevent_req_nomem(subreq
, req
)) {
3759 return tevent_req_post(req
, ev
);
3761 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3765 static void cli_setattrE_done(struct tevent_req
*subreq
)
3767 struct tevent_req
*req
= tevent_req_callback_data(
3768 subreq
, struct tevent_req
);
3771 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3772 TALLOC_FREE(subreq
);
3773 if (tevent_req_nterror(req
, status
)) {
3776 tevent_req_done(req
);
3779 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3781 return tevent_req_simple_recv_ntstatus(req
);
3784 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3790 TALLOC_CTX
*frame
= NULL
;
3791 struct tevent_context
*ev
= NULL
;
3792 struct tevent_req
*req
= NULL
;
3793 NTSTATUS status
= NT_STATUS_OK
;
3795 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3796 return cli_smb2_setattrE(cli
,
3803 frame
= talloc_stackframe();
3805 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3807 * Can't use sync call while an async call is in flight
3809 status
= NT_STATUS_INVALID_PARAMETER
;
3813 ev
= samba_tevent_context_init(frame
);
3815 status
= NT_STATUS_NO_MEMORY
;
3819 req
= cli_setattrE_send(frame
, ev
,
3827 status
= NT_STATUS_NO_MEMORY
;
3831 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3835 status
= cli_setattrE_recv(req
);
3842 /****************************************************************************
3843 Do a SMBsetatr call.
3844 ****************************************************************************/
3846 static void cli_setatr_done(struct tevent_req
*subreq
);
3848 struct cli_setatr_state
{
3852 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3853 struct tevent_context
*ev
,
3854 struct cli_state
*cli
,
3859 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3860 struct cli_setatr_state
*state
= NULL
;
3861 uint8_t additional_flags
= 0;
3862 uint8_t *bytes
= NULL
;
3864 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3869 SSVAL(state
->vwv
+0, 0, attr
);
3870 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3872 bytes
= talloc_array(state
, uint8_t, 1);
3873 if (tevent_req_nomem(bytes
, req
)) {
3874 return tevent_req_post(req
, ev
);
3877 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3878 strlen(fname
)+1, NULL
);
3879 if (tevent_req_nomem(bytes
, req
)) {
3880 return tevent_req_post(req
, ev
);
3882 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3883 talloc_get_size(bytes
)+1);
3884 if (tevent_req_nomem(bytes
, req
)) {
3885 return tevent_req_post(req
, ev
);
3888 bytes
[talloc_get_size(bytes
)-1] = 4;
3889 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3891 if (tevent_req_nomem(bytes
, req
)) {
3892 return tevent_req_post(req
, ev
);
3895 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3896 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3897 if (tevent_req_nomem(subreq
, req
)) {
3898 return tevent_req_post(req
, ev
);
3900 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3904 static void cli_setatr_done(struct tevent_req
*subreq
)
3906 struct tevent_req
*req
= tevent_req_callback_data(
3907 subreq
, struct tevent_req
);
3910 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3911 TALLOC_FREE(subreq
);
3912 if (tevent_req_nterror(req
, status
)) {
3915 tevent_req_done(req
);
3918 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3920 return tevent_req_simple_recv_ntstatus(req
);
3923 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3928 TALLOC_CTX
*frame
= NULL
;
3929 struct tevent_context
*ev
= NULL
;
3930 struct tevent_req
*req
= NULL
;
3931 NTSTATUS status
= NT_STATUS_OK
;
3933 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3934 return cli_smb2_setatr(cli
,
3940 frame
= talloc_stackframe();
3942 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3944 * Can't use sync call while an async call is in flight
3946 status
= NT_STATUS_INVALID_PARAMETER
;
3950 ev
= samba_tevent_context_init(frame
);
3952 status
= NT_STATUS_NO_MEMORY
;
3956 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3958 status
= NT_STATUS_NO_MEMORY
;
3962 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
3966 status
= cli_setatr_recv(req
);
3973 /****************************************************************************
3974 Check for existance of a dir.
3975 ****************************************************************************/
3977 static void cli_chkpath_done(struct tevent_req
*subreq
);
3979 struct cli_chkpath_state
{
3983 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3984 struct tevent_context
*ev
,
3985 struct cli_state
*cli
,
3988 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3989 struct cli_chkpath_state
*state
= NULL
;
3990 uint8_t additional_flags
= 0;
3991 uint8_t *bytes
= NULL
;
3993 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3998 bytes
= talloc_array(state
, uint8_t, 1);
3999 if (tevent_req_nomem(bytes
, req
)) {
4000 return tevent_req_post(req
, ev
);
4003 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4004 strlen(fname
)+1, NULL
);
4006 if (tevent_req_nomem(bytes
, req
)) {
4007 return tevent_req_post(req
, ev
);
4010 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
4011 0, NULL
, talloc_get_size(bytes
), bytes
);
4012 if (tevent_req_nomem(subreq
, req
)) {
4013 return tevent_req_post(req
, ev
);
4015 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
4019 static void cli_chkpath_done(struct tevent_req
*subreq
)
4021 struct tevent_req
*req
= tevent_req_callback_data(
4022 subreq
, struct tevent_req
);
4025 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4026 TALLOC_FREE(subreq
);
4027 if (tevent_req_nterror(req
, status
)) {
4030 tevent_req_done(req
);
4033 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
4035 return tevent_req_simple_recv_ntstatus(req
);
4038 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
4040 TALLOC_CTX
*frame
= talloc_stackframe();
4041 struct tevent_context
*ev
= NULL
;
4042 struct tevent_req
*req
= NULL
;
4044 NTSTATUS status
= NT_STATUS_OK
;
4046 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4048 * Can't use sync call while an async call is in flight
4050 status
= NT_STATUS_INVALID_PARAMETER
;
4054 path2
= talloc_strdup(frame
, path
);
4056 status
= NT_STATUS_NO_MEMORY
;
4059 trim_char(path2
,'\0','\\');
4061 path2
= talloc_strdup(frame
, "\\");
4063 status
= NT_STATUS_NO_MEMORY
;
4068 ev
= samba_tevent_context_init(frame
);
4070 status
= NT_STATUS_NO_MEMORY
;
4074 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4076 status
= NT_STATUS_NO_MEMORY
;
4080 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4084 status
= cli_chkpath_recv(req
);
4091 /****************************************************************************
4093 ****************************************************************************/
4095 static void cli_dskattr_done(struct tevent_req
*subreq
);
4097 struct cli_dskattr_state
{
4103 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4104 struct tevent_context
*ev
,
4105 struct cli_state
*cli
)
4107 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4108 struct cli_dskattr_state
*state
= NULL
;
4109 uint8_t additional_flags
= 0;
4111 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4116 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
4118 if (tevent_req_nomem(subreq
, req
)) {
4119 return tevent_req_post(req
, ev
);
4121 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4125 static void cli_dskattr_done(struct tevent_req
*subreq
)
4127 struct tevent_req
*req
= tevent_req_callback_data(
4128 subreq
, struct tevent_req
);
4129 struct cli_dskattr_state
*state
= tevent_req_data(
4130 req
, struct cli_dskattr_state
);
4132 uint16_t *vwv
= NULL
;
4135 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4137 TALLOC_FREE(subreq
);
4138 if (tevent_req_nterror(req
, status
)) {
4141 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4142 state
->total
= SVAL(vwv
+0, 0);
4143 state
->avail
= SVAL(vwv
+3, 0);
4144 tevent_req_done(req
);
4147 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4149 struct cli_dskattr_state
*state
= tevent_req_data(
4150 req
, struct cli_dskattr_state
);
4153 if (tevent_req_is_nterror(req
, &status
)) {
4156 *bsize
= state
->bsize
;
4157 *total
= state
->total
;
4158 *avail
= state
->avail
;
4159 return NT_STATUS_OK
;
4162 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4164 TALLOC_CTX
*frame
= NULL
;
4165 struct tevent_context
*ev
= NULL
;
4166 struct tevent_req
*req
= NULL
;
4167 NTSTATUS status
= NT_STATUS_OK
;
4169 frame
= talloc_stackframe();
4171 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4173 * Can't use sync call while an async call is in flight
4175 status
= NT_STATUS_INVALID_PARAMETER
;
4179 ev
= samba_tevent_context_init(frame
);
4181 status
= NT_STATUS_NO_MEMORY
;
4185 req
= cli_dskattr_send(frame
, ev
, cli
);
4187 status
= NT_STATUS_NO_MEMORY
;
4191 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4195 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4202 NTSTATUS
cli_disk_size(struct cli_state
*cli
, uint64_t *bsize
, uint64_t *total
, uint64_t *avail
)
4204 uint64_t sectors_per_block
;
4205 uint64_t bytes_per_sector
;
4206 int old_bsize
, old_total
, old_avail
;
4209 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4210 return cli_smb2_dskattr(cli
, bsize
, total
, avail
);
4214 * Try the trans2 disk full size info call first.
4215 * We already use this in SMBC_fstatvfs_ctx().
4216 * Ignore 'actual_available_units' as we only
4217 * care about the quota for the caller.
4220 status
= cli_get_fs_full_size_info(cli
,
4227 /* Try and cope will all varients of "we don't do this call"
4228 and fall back to cli_dskattr. */
4230 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
4231 NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
) ||
4232 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
4233 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
4234 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
4235 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
4236 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
4237 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
4238 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
4239 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
4243 if (!NT_STATUS_IS_OK(status
)) {
4248 *bsize
= sectors_per_block
*
4252 return NT_STATUS_OK
;
4256 /* Old SMB1 core protocol fallback. */
4257 status
= cli_dskattr(cli
, &old_bsize
, &old_total
, &old_avail
);
4258 if (!NT_STATUS_IS_OK(status
)) {
4262 *bsize
= (uint64_t)old_bsize
;
4265 *total
= (uint64_t)old_total
;
4268 *avail
= (uint64_t)old_avail
;
4270 return NT_STATUS_OK
;
4273 /****************************************************************************
4274 Create and open a temporary file.
4275 ****************************************************************************/
4277 static void cli_ctemp_done(struct tevent_req
*subreq
);
4279 struct ctemp_state
{
4285 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4286 struct tevent_context
*ev
,
4287 struct cli_state
*cli
,
4290 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4291 struct ctemp_state
*state
= NULL
;
4292 uint8_t additional_flags
= 0;
4293 uint8_t *bytes
= NULL
;
4295 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4300 SSVAL(state
->vwv
,0,0);
4301 SIVALS(state
->vwv
+1,0,-1);
4303 bytes
= talloc_array(state
, uint8_t, 1);
4304 if (tevent_req_nomem(bytes
, req
)) {
4305 return tevent_req_post(req
, ev
);
4308 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4309 strlen(path
)+1, NULL
);
4310 if (tevent_req_nomem(bytes
, req
)) {
4311 return tevent_req_post(req
, ev
);
4314 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4315 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4316 if (tevent_req_nomem(subreq
, req
)) {
4317 return tevent_req_post(req
, ev
);
4319 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4323 static void cli_ctemp_done(struct tevent_req
*subreq
)
4325 struct tevent_req
*req
= tevent_req_callback_data(
4326 subreq
, struct tevent_req
);
4327 struct ctemp_state
*state
= tevent_req_data(
4328 req
, struct ctemp_state
);
4332 uint32_t num_bytes
= 0;
4333 uint8_t *bytes
= NULL
;
4335 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4336 &num_bytes
, &bytes
);
4337 TALLOC_FREE(subreq
);
4338 if (tevent_req_nterror(req
, status
)) {
4342 state
->fnum
= SVAL(vwv
+0, 0);
4344 /* From W2K3, the result is just the ASCII name */
4345 if (num_bytes
< 2) {
4346 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4350 if (pull_string_talloc(state
,
4357 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4360 tevent_req_done(req
);
4363 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4368 struct ctemp_state
*state
= tevent_req_data(req
,
4369 struct ctemp_state
);
4372 if (tevent_req_is_nterror(req
, &status
)) {
4375 *pfnum
= state
->fnum
;
4376 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4378 return NT_STATUS_NO_MEMORY
;
4380 return NT_STATUS_OK
;
4383 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4389 TALLOC_CTX
*frame
= talloc_stackframe();
4390 struct tevent_context
*ev
;
4391 struct tevent_req
*req
;
4392 NTSTATUS status
= NT_STATUS_OK
;
4394 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4396 * Can't use sync call while an async call is in flight
4398 status
= NT_STATUS_INVALID_PARAMETER
;
4402 ev
= samba_tevent_context_init(frame
);
4404 status
= NT_STATUS_NO_MEMORY
;
4408 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4410 status
= NT_STATUS_NO_MEMORY
;
4414 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4418 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4426 send a raw ioctl - used by the torture code
4428 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4433 SSVAL(vwv
+0, 0, fnum
);
4434 SSVAL(vwv
+1, 0, code
>>16);
4435 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4437 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4438 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4439 if (!NT_STATUS_IS_OK(status
)) {
4442 *blob
= data_blob_null
;
4443 return NT_STATUS_OK
;
4446 /*********************************************************
4447 Set an extended attribute utility fn.
4448 *********************************************************/
4450 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4451 uint8_t *param
, unsigned int param_len
,
4452 const char *ea_name
,
4453 const char *ea_val
, size_t ea_len
)
4456 unsigned int data_len
= 0;
4457 uint8_t *data
= NULL
;
4459 size_t ea_namelen
= strlen(ea_name
);
4462 SSVAL(setup
, 0, setup_val
);
4464 if (ea_namelen
== 0 && ea_len
== 0) {
4466 data
= talloc_array(talloc_tos(),
4470 return NT_STATUS_NO_MEMORY
;
4473 SIVAL(p
,0,data_len
);
4475 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4476 data
= talloc_array(talloc_tos(),
4480 return NT_STATUS_NO_MEMORY
;
4483 SIVAL(p
,0,data_len
);
4485 SCVAL(p
, 0, 0); /* EA flags. */
4486 SCVAL(p
, 1, ea_namelen
);
4487 SSVAL(p
, 2, ea_len
);
4488 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4489 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4492 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4494 param
, param_len
, 2,
4495 data
, data_len
, CLI_BUFFER_SIZE
,
4497 NULL
, 0, NULL
, /* rsetup */
4498 NULL
, 0, NULL
, /* rparam */
4499 NULL
, 0, NULL
); /* rdata */
4504 /*********************************************************
4505 Set an extended attribute on a pathname.
4506 *********************************************************/
4508 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4509 const char *ea_name
, const char *ea_val
,
4512 unsigned int param_len
= 0;
4515 TALLOC_CTX
*frame
= NULL
;
4517 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4518 return cli_smb2_set_ea_path(cli
,
4525 frame
= talloc_stackframe();
4527 param
= talloc_array(frame
, uint8_t, 6);
4529 status
= NT_STATUS_NO_MEMORY
;
4532 SSVAL(param
,0,SMB_INFO_SET_EA
);
4536 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4537 path
, strlen(path
)+1,
4539 param_len
= talloc_get_size(param
);
4541 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4542 ea_name
, ea_val
, ea_len
);
4550 /*********************************************************
4551 Set an extended attribute on an fnum.
4552 *********************************************************/
4554 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4555 const char *ea_name
, const char *ea_val
,
4560 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4561 return cli_smb2_set_ea_fnum(cli
,
4568 memset(param
, 0, 6);
4569 SSVAL(param
,0,fnum
);
4570 SSVAL(param
,2,SMB_INFO_SET_EA
);
4572 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4573 ea_name
, ea_val
, ea_len
);
4576 /*********************************************************
4577 Get an extended attribute list utility fn.
4578 *********************************************************/
4580 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4582 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4584 struct ea_struct
*ea_list
= NULL
;
4589 if (rdata_len
< 4) {
4593 ea_size
= (size_t)IVAL(rdata
,0);
4594 if (ea_size
> rdata_len
) {
4599 /* No EA's present. */
4608 /* Validate the EA list and count it. */
4609 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4610 unsigned int ea_namelen
= CVAL(p
,1);
4611 unsigned int ea_valuelen
= SVAL(p
,2);
4612 if (ea_namelen
== 0) {
4615 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4618 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4619 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4628 *pnum_eas
= num_eas
;
4630 /* Caller only wants number of EA's. */
4634 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4639 ea_size
= (size_t)IVAL(rdata
,0);
4642 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4643 struct ea_struct
*ea
= &ea_list
[num_eas
];
4644 fstring unix_ea_name
;
4645 unsigned int ea_namelen
= CVAL(p
,1);
4646 unsigned int ea_valuelen
= SVAL(p
,2);
4648 ea
->flags
= CVAL(p
,0);
4649 unix_ea_name
[0] = '\0';
4650 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4651 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4655 /* Ensure the value is null terminated (in case it's a string). */
4656 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4657 if (!ea
->value
.data
) {
4661 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4663 ea
->value
.data
[ea_valuelen
] = 0;
4665 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4668 *pea_list
= ea_list
;
4672 TALLOC_FREE(ea_list
);
4676 /*********************************************************
4677 Get an extended attribute list from a pathname.
4678 *********************************************************/
4680 struct cli_get_ea_list_path_state
{
4685 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4687 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4688 struct tevent_context
*ev
,
4689 struct cli_state
*cli
,
4692 struct tevent_req
*req
, *subreq
;
4693 struct cli_get_ea_list_path_state
*state
;
4695 req
= tevent_req_create(mem_ctx
, &state
,
4696 struct cli_get_ea_list_path_state
);
4700 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4701 SMB_INFO_QUERY_ALL_EAS
, 4,
4703 if (tevent_req_nomem(subreq
, req
)) {
4704 return tevent_req_post(req
, ev
);
4706 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4710 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4712 struct tevent_req
*req
= tevent_req_callback_data(
4713 subreq
, struct tevent_req
);
4714 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4715 req
, struct cli_get_ea_list_path_state
);
4718 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4720 TALLOC_FREE(subreq
);
4721 if (tevent_req_nterror(req
, status
)) {
4724 tevent_req_done(req
);
4727 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4728 size_t *pnum_eas
, struct ea_struct
**peas
)
4730 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4731 req
, struct cli_get_ea_list_path_state
);
4734 if (tevent_req_is_nterror(req
, &status
)) {
4737 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4739 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4741 return NT_STATUS_OK
;
4744 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4747 struct ea_struct
**pea_list
)
4749 TALLOC_CTX
*frame
= NULL
;
4750 struct tevent_context
*ev
= NULL
;
4751 struct tevent_req
*req
= NULL
;
4752 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4754 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4755 return cli_smb2_get_ea_list_path(cli
,
4762 frame
= talloc_stackframe();
4764 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4766 * Can't use sync call while an async call is in flight
4768 status
= NT_STATUS_INVALID_PARAMETER
;
4771 ev
= samba_tevent_context_init(frame
);
4775 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4779 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4782 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4788 /****************************************************************************
4789 Convert open "flags" arg to uint32_t on wire.
4790 ****************************************************************************/
4792 static uint32_t open_flags_to_wire(int flags
)
4794 int open_mode
= flags
& O_ACCMODE
;
4797 switch (open_mode
) {
4799 ret
|= SMB_O_WRONLY
;
4806 ret
|= SMB_O_RDONLY
;
4810 if (flags
& O_CREAT
) {
4813 if (flags
& O_EXCL
) {
4816 if (flags
& O_TRUNC
) {
4820 if (flags
& O_SYNC
) {
4824 if (flags
& O_APPEND
) {
4825 ret
|= SMB_O_APPEND
;
4827 #if defined(O_DIRECT)
4828 if (flags
& O_DIRECT
) {
4829 ret
|= SMB_O_DIRECT
;
4832 #if defined(O_DIRECTORY)
4833 if (flags
& O_DIRECTORY
) {
4834 ret
|= SMB_O_DIRECTORY
;
4840 /****************************************************************************
4841 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4842 ****************************************************************************/
4844 struct posix_open_state
{
4848 uint16_t fnum
; /* Out */
4851 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4853 struct tevent_req
*req
= tevent_req_callback_data(
4854 subreq
, struct tevent_req
);
4855 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4860 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4861 NULL
, 0, NULL
, &data
, 12, &num_data
);
4862 TALLOC_FREE(subreq
);
4863 if (tevent_req_nterror(req
, status
)) {
4866 state
->fnum
= SVAL(data
,2);
4867 tevent_req_done(req
);
4870 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4871 struct tevent_context
*ev
,
4872 struct cli_state
*cli
,
4878 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4879 struct posix_open_state
*state
= NULL
;
4880 uint32_t wire_flags
= open_flags_to_wire(flags
);
4882 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4887 /* Setup setup word. */
4888 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4890 /* Setup param array. */
4891 state
->param
= talloc_array(state
, uint8_t, 6);
4892 if (tevent_req_nomem(state
->param
, req
)) {
4893 return tevent_req_post(req
, ev
);
4895 memset(state
->param
, '\0', 6);
4896 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4898 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4899 strlen(fname
)+1, NULL
);
4901 if (tevent_req_nomem(state
->param
, req
)) {
4902 return tevent_req_post(req
, ev
);
4905 /* Setup data words. */
4907 wire_flags
|= SMB_O_DIRECTORY
;
4910 SIVAL(state
->data
,0,0); /* No oplock. */
4911 SIVAL(state
->data
,4,wire_flags
);
4912 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4913 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4914 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4916 subreq
= cli_trans_send(state
, /* mem ctx. */
4917 ev
, /* event ctx. */
4918 cli
, /* cli_state. */
4919 SMBtrans2
, /* cmd. */
4920 NULL
, /* pipe name. */
4924 &state
->setup
, /* setup. */
4925 1, /* num setup uint16_t words. */
4926 0, /* max returned setup. */
4927 state
->param
, /* param. */
4928 talloc_get_size(state
->param
),/* num param. */
4929 2, /* max returned param. */
4930 state
->data
, /* data. */
4932 12); /* max returned data. */
4934 if (tevent_req_nomem(subreq
, req
)) {
4935 return tevent_req_post(req
, ev
);
4937 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4941 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4942 struct tevent_context
*ev
,
4943 struct cli_state
*cli
,
4948 return cli_posix_open_internal_send(mem_ctx
, ev
,
4949 cli
, fname
, flags
, mode
, false);
4952 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4954 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4957 if (tevent_req_is_nterror(req
, &status
)) {
4960 *pfnum
= state
->fnum
;
4961 return NT_STATUS_OK
;
4964 /****************************************************************************
4965 Open - POSIX semantics. Doesn't request oplock.
4966 ****************************************************************************/
4968 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4969 int flags
, mode_t mode
, uint16_t *pfnum
)
4972 TALLOC_CTX
*frame
= talloc_stackframe();
4973 struct tevent_context
*ev
= NULL
;
4974 struct tevent_req
*req
= NULL
;
4975 NTSTATUS status
= NT_STATUS_OK
;
4977 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4979 * Can't use sync call while an async call is in flight
4981 status
= NT_STATUS_INVALID_PARAMETER
;
4985 ev
= samba_tevent_context_init(frame
);
4987 status
= NT_STATUS_NO_MEMORY
;
4991 req
= cli_posix_open_send(frame
,
4998 status
= NT_STATUS_NO_MEMORY
;
5002 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5006 status
= cli_posix_open_recv(req
, pfnum
);
5013 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5014 struct tevent_context
*ev
,
5015 struct cli_state
*cli
,
5019 return cli_posix_open_internal_send(mem_ctx
, ev
,
5020 cli
, fname
, O_CREAT
, mode
, true);
5023 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5025 return tevent_req_simple_recv_ntstatus(req
);
5028 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5030 TALLOC_CTX
*frame
= talloc_stackframe();
5031 struct tevent_context
*ev
= NULL
;
5032 struct tevent_req
*req
= NULL
;
5033 NTSTATUS status
= NT_STATUS_OK
;
5035 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5037 * Can't use sync call while an async call is in flight
5039 status
= NT_STATUS_INVALID_PARAMETER
;
5043 ev
= samba_tevent_context_init(frame
);
5045 status
= NT_STATUS_NO_MEMORY
;
5049 req
= cli_posix_mkdir_send(frame
,
5055 status
= NT_STATUS_NO_MEMORY
;
5059 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5063 status
= cli_posix_mkdir_recv(req
);
5070 /****************************************************************************
5071 unlink or rmdir - POSIX semantics.
5072 ****************************************************************************/
5074 struct cli_posix_unlink_internal_state
{
5078 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5080 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5081 struct tevent_context
*ev
,
5082 struct cli_state
*cli
,
5086 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5087 struct cli_posix_unlink_internal_state
*state
= NULL
;
5089 req
= tevent_req_create(mem_ctx
, &state
,
5090 struct cli_posix_unlink_internal_state
);
5095 /* Setup data word. */
5096 SSVAL(state
->data
, 0, level
);
5098 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5099 SMB_POSIX_PATH_UNLINK
,
5101 state
->data
, sizeof(state
->data
));
5102 if (tevent_req_nomem(subreq
, req
)) {
5103 return tevent_req_post(req
, ev
);
5105 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5109 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5111 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5112 tevent_req_simple_finish_ntstatus(subreq
, status
);
5115 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5116 struct tevent_context
*ev
,
5117 struct cli_state
*cli
,
5120 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5121 SMB_POSIX_UNLINK_FILE_TARGET
);
5124 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5126 return tevent_req_simple_recv_ntstatus(req
);
5129 /****************************************************************************
5130 unlink - POSIX semantics.
5131 ****************************************************************************/
5133 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5135 TALLOC_CTX
*frame
= talloc_stackframe();
5136 struct tevent_context
*ev
= NULL
;
5137 struct tevent_req
*req
= NULL
;
5138 NTSTATUS status
= NT_STATUS_OK
;
5140 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5142 * Can't use sync call while an async call is in flight
5144 status
= NT_STATUS_INVALID_PARAMETER
;
5148 ev
= samba_tevent_context_init(frame
);
5150 status
= NT_STATUS_NO_MEMORY
;
5154 req
= cli_posix_unlink_send(frame
,
5159 status
= NT_STATUS_NO_MEMORY
;
5163 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5167 status
= cli_posix_unlink_recv(req
);
5174 /****************************************************************************
5175 rmdir - POSIX semantics.
5176 ****************************************************************************/
5178 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5179 struct tevent_context
*ev
,
5180 struct cli_state
*cli
,
5183 return cli_posix_unlink_internal_send(
5184 mem_ctx
, ev
, cli
, fname
,
5185 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5188 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5190 return tevent_req_simple_recv_ntstatus(req
);
5193 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5195 TALLOC_CTX
*frame
= talloc_stackframe();
5196 struct tevent_context
*ev
= NULL
;
5197 struct tevent_req
*req
= NULL
;
5198 NTSTATUS status
= NT_STATUS_OK
;
5200 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5202 * Can't use sync call while an async call is in flight
5204 status
= NT_STATUS_INVALID_PARAMETER
;
5208 ev
= samba_tevent_context_init(frame
);
5210 status
= NT_STATUS_NO_MEMORY
;
5214 req
= cli_posix_rmdir_send(frame
,
5219 status
= NT_STATUS_NO_MEMORY
;
5223 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5227 status
= cli_posix_rmdir_recv(req
, frame
);
5234 /****************************************************************************
5236 ****************************************************************************/
5238 struct cli_notify_state
{
5240 uint32_t num_changes
;
5241 struct notify_change
*changes
;
5244 static void cli_notify_done(struct tevent_req
*subreq
);
5246 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5247 struct tevent_context
*ev
,
5248 struct cli_state
*cli
, uint16_t fnum
,
5249 uint32_t buffer_size
,
5250 uint32_t completion_filter
, bool recursive
)
5252 struct tevent_req
*req
, *subreq
;
5253 struct cli_notify_state
*state
;
5254 unsigned old_timeout
;
5256 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5261 SIVAL(state
->setup
, 0, completion_filter
);
5262 SSVAL(state
->setup
, 4, fnum
);
5263 SSVAL(state
->setup
, 6, recursive
);
5266 * Notifies should not time out
5268 old_timeout
= cli_set_timeout(cli
, 0);
5270 subreq
= cli_trans_send(
5271 state
, /* mem ctx. */
5272 ev
, /* event ctx. */
5273 cli
, /* cli_state. */
5274 SMBnttrans
, /* cmd. */
5275 NULL
, /* pipe name. */
5277 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5279 (uint16_t *)state
->setup
, /* setup. */
5280 4, /* num setup uint16_t words. */
5281 0, /* max returned setup. */
5284 buffer_size
, /* max returned param. */
5287 0); /* max returned data. */
5289 cli_set_timeout(cli
, old_timeout
);
5291 if (tevent_req_nomem(subreq
, req
)) {
5292 return tevent_req_post(req
, ev
);
5294 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5298 static void cli_notify_done(struct tevent_req
*subreq
)
5300 struct tevent_req
*req
= tevent_req_callback_data(
5301 subreq
, struct tevent_req
);
5302 struct cli_notify_state
*state
= tevent_req_data(
5303 req
, struct cli_notify_state
);
5306 uint32_t i
, ofs
, num_params
;
5309 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5310 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5311 TALLOC_FREE(subreq
);
5312 if (tevent_req_nterror(req
, status
)) {
5313 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5317 state
->num_changes
= 0;
5320 while (num_params
- ofs
> 12) {
5321 uint32_t next
= IVAL(params
, ofs
);
5322 state
->num_changes
+= 1;
5324 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5330 state
->changes
= talloc_array(state
, struct notify_change
,
5331 state
->num_changes
);
5332 if (tevent_req_nomem(state
->changes
, req
)) {
5333 TALLOC_FREE(params
);
5339 for (i
=0; i
<state
->num_changes
; i
++) {
5340 uint32_t next
= IVAL(params
, ofs
);
5341 uint32_t len
= IVAL(params
, ofs
+8);
5345 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5346 TALLOC_FREE(params
);
5348 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5352 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5353 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5354 &name
, params
+ofs
+12, len
,
5355 STR_TERMINATE
|STR_UNICODE
);
5357 TALLOC_FREE(params
);
5358 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5361 state
->changes
[i
].name
= name
;
5365 TALLOC_FREE(params
);
5366 tevent_req_done(req
);
5369 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5370 uint32_t *pnum_changes
,
5371 struct notify_change
**pchanges
)
5373 struct cli_notify_state
*state
= tevent_req_data(
5374 req
, struct cli_notify_state
);
5377 if (tevent_req_is_nterror(req
, &status
)) {
5381 *pnum_changes
= state
->num_changes
;
5382 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5383 return NT_STATUS_OK
;
5386 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5387 uint32_t completion_filter
, bool recursive
,
5388 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5389 struct notify_change
**pchanges
)
5391 TALLOC_CTX
*frame
= talloc_stackframe();
5392 struct tevent_context
*ev
;
5393 struct tevent_req
*req
;
5394 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5396 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5398 * Can't use sync call while an async call is in flight
5400 status
= NT_STATUS_INVALID_PARAMETER
;
5403 ev
= samba_tevent_context_init(frame
);
5407 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5408 completion_filter
, recursive
);
5412 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5415 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5421 struct cli_qpathinfo_state
{
5430 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5432 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5433 struct tevent_context
*ev
,
5434 struct cli_state
*cli
, const char *fname
,
5435 uint16_t level
, uint32_t min_rdata
,
5438 struct tevent_req
*req
, *subreq
;
5439 struct cli_qpathinfo_state
*state
;
5441 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5445 state
->min_rdata
= min_rdata
;
5446 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5448 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5449 if (tevent_req_nomem(state
->param
, req
)) {
5450 return tevent_req_post(req
, ev
);
5452 SSVAL(state
->param
, 0, level
);
5453 state
->param
= trans2_bytes_push_str(
5454 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5455 if (tevent_req_nomem(state
->param
, req
)) {
5456 return tevent_req_post(req
, ev
);
5459 subreq
= cli_trans_send(
5460 state
, /* mem ctx. */
5461 ev
, /* event ctx. */
5462 cli
, /* cli_state. */
5463 SMBtrans2
, /* cmd. */
5464 NULL
, /* pipe name. */
5468 state
->setup
, /* setup. */
5469 1, /* num setup uint16_t words. */
5470 0, /* max returned setup. */
5471 state
->param
, /* param. */
5472 talloc_get_size(state
->param
), /* num param. */
5473 2, /* max returned param. */
5476 max_rdata
); /* max returned data. */
5478 if (tevent_req_nomem(subreq
, req
)) {
5479 return tevent_req_post(req
, ev
);
5481 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5485 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5487 struct tevent_req
*req
= tevent_req_callback_data(
5488 subreq
, struct tevent_req
);
5489 struct cli_qpathinfo_state
*state
= tevent_req_data(
5490 req
, struct cli_qpathinfo_state
);
5493 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5495 &state
->rdata
, state
->min_rdata
,
5497 if (tevent_req_nterror(req
, status
)) {
5500 tevent_req_done(req
);
5503 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5504 uint8_t **rdata
, uint32_t *num_rdata
)
5506 struct cli_qpathinfo_state
*state
= tevent_req_data(
5507 req
, struct cli_qpathinfo_state
);
5510 if (tevent_req_is_nterror(req
, &status
)) {
5513 if (rdata
!= NULL
) {
5514 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5516 TALLOC_FREE(state
->rdata
);
5518 if (num_rdata
!= NULL
) {
5519 *num_rdata
= state
->num_rdata
;
5521 return NT_STATUS_OK
;
5524 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5525 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5527 uint8_t **rdata
, uint32_t *num_rdata
)
5529 TALLOC_CTX
*frame
= talloc_stackframe();
5530 struct tevent_context
*ev
;
5531 struct tevent_req
*req
;
5532 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5534 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5536 * Can't use sync call while an async call is in flight
5538 status
= NT_STATUS_INVALID_PARAMETER
;
5541 ev
= samba_tevent_context_init(frame
);
5545 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5550 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5553 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5559 struct cli_qfileinfo_state
{
5563 uint16_t recv_flags2
;
5569 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5571 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5572 struct tevent_context
*ev
,
5573 struct cli_state
*cli
, uint16_t fnum
,
5574 uint16_t level
, uint32_t min_rdata
,
5577 struct tevent_req
*req
, *subreq
;
5578 struct cli_qfileinfo_state
*state
;
5580 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5584 state
->min_rdata
= min_rdata
;
5585 SSVAL(state
->param
, 0, fnum
);
5586 SSVAL(state
->param
, 2, level
);
5587 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5589 subreq
= cli_trans_send(
5590 state
, /* mem ctx. */
5591 ev
, /* event ctx. */
5592 cli
, /* cli_state. */
5593 SMBtrans2
, /* cmd. */
5594 NULL
, /* pipe name. */
5598 state
->setup
, /* setup. */
5599 1, /* num setup uint16_t words. */
5600 0, /* max returned setup. */
5601 state
->param
, /* param. */
5602 sizeof(state
->param
), /* num param. */
5603 2, /* max returned param. */
5606 max_rdata
); /* max returned data. */
5608 if (tevent_req_nomem(subreq
, req
)) {
5609 return tevent_req_post(req
, ev
);
5611 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5615 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5617 struct tevent_req
*req
= tevent_req_callback_data(
5618 subreq
, struct tevent_req
);
5619 struct cli_qfileinfo_state
*state
= tevent_req_data(
5620 req
, struct cli_qfileinfo_state
);
5623 status
= cli_trans_recv(subreq
, state
,
5624 &state
->recv_flags2
,
5627 &state
->rdata
, state
->min_rdata
,
5629 if (tevent_req_nterror(req
, status
)) {
5632 tevent_req_done(req
);
5635 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5636 uint16_t *recv_flags2
,
5637 uint8_t **rdata
, uint32_t *num_rdata
)
5639 struct cli_qfileinfo_state
*state
= tevent_req_data(
5640 req
, struct cli_qfileinfo_state
);
5643 if (tevent_req_is_nterror(req
, &status
)) {
5647 if (recv_flags2
!= NULL
) {
5648 *recv_flags2
= state
->recv_flags2
;
5650 if (rdata
!= NULL
) {
5651 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5653 TALLOC_FREE(state
->rdata
);
5655 if (num_rdata
!= NULL
) {
5656 *num_rdata
= state
->num_rdata
;
5658 return NT_STATUS_OK
;
5661 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5662 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5663 uint32_t max_rdata
, uint16_t *recv_flags2
,
5664 uint8_t **rdata
, uint32_t *num_rdata
)
5666 TALLOC_CTX
*frame
= talloc_stackframe();
5667 struct tevent_context
*ev
;
5668 struct tevent_req
*req
;
5669 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5671 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5673 * Can't use sync call while an async call is in flight
5675 status
= NT_STATUS_INVALID_PARAMETER
;
5678 ev
= samba_tevent_context_init(frame
);
5682 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5687 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5690 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5696 struct cli_flush_state
{
5700 static void cli_flush_done(struct tevent_req
*subreq
);
5702 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5703 struct tevent_context
*ev
,
5704 struct cli_state
*cli
,
5707 struct tevent_req
*req
, *subreq
;
5708 struct cli_flush_state
*state
;
5710 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5714 SSVAL(state
->vwv
+ 0, 0, fnum
);
5716 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5718 if (tevent_req_nomem(subreq
, req
)) {
5719 return tevent_req_post(req
, ev
);
5721 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5725 static void cli_flush_done(struct tevent_req
*subreq
)
5727 struct tevent_req
*req
= tevent_req_callback_data(
5728 subreq
, struct tevent_req
);
5731 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5732 TALLOC_FREE(subreq
);
5733 if (tevent_req_nterror(req
, status
)) {
5736 tevent_req_done(req
);
5739 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5741 return tevent_req_simple_recv_ntstatus(req
);
5744 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5746 TALLOC_CTX
*frame
= talloc_stackframe();
5747 struct tevent_context
*ev
;
5748 struct tevent_req
*req
;
5749 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5751 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5753 * Can't use sync call while an async call is in flight
5755 status
= NT_STATUS_INVALID_PARAMETER
;
5758 ev
= samba_tevent_context_init(frame
);
5762 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5766 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5769 status
= cli_flush_recv(req
);
5775 struct cli_shadow_copy_data_state
{
5782 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5784 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5785 struct tevent_context
*ev
,
5786 struct cli_state
*cli
,
5790 struct tevent_req
*req
, *subreq
;
5791 struct cli_shadow_copy_data_state
*state
;
5794 req
= tevent_req_create(mem_ctx
, &state
,
5795 struct cli_shadow_copy_data_state
);
5799 state
->get_names
= get_names
;
5800 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5802 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5803 SSVAL(state
->setup
+ 2, 0, fnum
);
5804 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5805 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5807 subreq
= cli_trans_send(
5808 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5809 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5812 if (tevent_req_nomem(subreq
, req
)) {
5813 return tevent_req_post(req
, ev
);
5815 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5819 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5821 struct tevent_req
*req
= tevent_req_callback_data(
5822 subreq
, struct tevent_req
);
5823 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5824 req
, struct cli_shadow_copy_data_state
);
5827 status
= cli_trans_recv(subreq
, state
, NULL
,
5828 NULL
, 0, NULL
, /* setup */
5829 NULL
, 0, NULL
, /* param */
5830 &state
->data
, 12, &state
->num_data
);
5831 TALLOC_FREE(subreq
);
5832 if (tevent_req_nterror(req
, status
)) {
5835 tevent_req_done(req
);
5838 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5839 char ***pnames
, int *pnum_names
)
5841 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5842 req
, struct cli_shadow_copy_data_state
);
5848 if (tevent_req_is_nterror(req
, &status
)) {
5851 num_names
= IVAL(state
->data
, 4);
5852 dlength
= IVAL(state
->data
, 8);
5854 if (!state
->get_names
) {
5855 *pnum_names
= num_names
;
5856 return NT_STATUS_OK
;
5859 if (dlength
+12 > state
->num_data
) {
5860 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5862 names
= talloc_array(mem_ctx
, char *, num_names
);
5863 if (names
== NULL
) {
5864 return NT_STATUS_NO_MEMORY
;
5867 for (i
=0; i
<num_names
; i
++) {
5870 size_t converted_size
;
5872 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5873 ret
= convert_string_talloc(
5874 names
, CH_UTF16LE
, CH_UNIX
,
5875 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5876 &names
[i
], &converted_size
);
5879 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5882 *pnum_names
= num_names
;
5884 return NT_STATUS_OK
;
5887 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5888 uint16_t fnum
, bool get_names
,
5889 char ***pnames
, int *pnum_names
)
5891 TALLOC_CTX
*frame
= talloc_stackframe();
5892 struct tevent_context
*ev
;
5893 struct tevent_req
*req
;
5894 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5896 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5898 * Can't use sync call while an async call is in flight
5900 status
= NT_STATUS_INVALID_PARAMETER
;
5903 ev
= samba_tevent_context_init(frame
);
5907 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5911 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5914 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);