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(req
, ev
)) {
376 status
= map_nt_error_from_unix(errno
);
380 status
= cli_posix_symlink_recv(req
);
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state
{
396 static void cli_posix_readlink_done(struct tevent_req
*subreq
);
398 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
399 struct tevent_context
*ev
,
400 struct cli_state
*cli
,
404 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
405 struct readlink_state
*state
= NULL
;
406 uint32_t maxbytelen
= (uint32_t)(smbXcli_conn_use_unicode(cli
->conn
) ? len
*3 : len
);
408 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len
< len
) || (maxbytelen
< len
)) {
417 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
418 return tevent_req_post(req
, ev
);
421 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
422 SMB_QUERY_FILE_UNIX_LINK
, 1, maxbytelen
);
423 if (tevent_req_nomem(subreq
, req
)) {
424 return tevent_req_post(req
, ev
);
426 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
430 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
432 struct tevent_req
*req
= tevent_req_callback_data(
433 subreq
, struct tevent_req
);
434 struct readlink_state
*state
= tevent_req_data(
435 req
, struct readlink_state
);
438 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
441 if (tevent_req_nterror(req
, status
)) {
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state
->data
[state
->num_data
-1] != '\0') {
448 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
451 tevent_req_done(req
);
454 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
455 char *retpath
, size_t len
)
458 char *converted
= NULL
;
459 size_t converted_size
= 0;
460 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
462 if (tevent_req_is_nterror(req
, &status
)) {
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state
,
467 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
473 return NT_STATUS_NO_MEMORY
;
476 len
= MIN(len
,converted_size
);
478 return NT_STATUS_DATA_ERROR
;
480 memcpy(retpath
, converted
, len
);
484 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
485 char *linkpath
, size_t len
)
487 TALLOC_CTX
*frame
= talloc_stackframe();
488 struct tevent_context
*ev
= NULL
;
489 struct tevent_req
*req
= NULL
;
490 NTSTATUS status
= NT_STATUS_OK
;
492 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
494 * Can't use sync call while an async call is in flight
496 status
= NT_STATUS_INVALID_PARAMETER
;
500 ev
= samba_tevent_context_init(frame
);
502 status
= NT_STATUS_NO_MEMORY
;
506 req
= cli_posix_readlink_send(frame
,
512 status
= NT_STATUS_NO_MEMORY
;
516 if (!tevent_req_poll(req
, ev
)) {
517 status
= map_nt_error_from_unix(errno
);
521 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
533 struct tevent_context
*ev
,
534 struct cli_state
*cli
,
538 return cli_posix_link_internal_send(
539 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_HLINK
, oldname
, newname
);
542 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
544 return tevent_req_simple_recv_ntstatus(req
);
547 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
551 TALLOC_CTX
*frame
= talloc_stackframe();
552 struct tevent_context
*ev
= NULL
;
553 struct tevent_req
*req
= NULL
;
554 NTSTATUS status
= NT_STATUS_OK
;
556 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
558 * Can't use sync call while an async call is in flight
560 status
= NT_STATUS_INVALID_PARAMETER
;
564 ev
= samba_tevent_context_init(frame
);
566 status
= NT_STATUS_NO_MEMORY
;
570 req
= cli_posix_hardlink_send(frame
,
576 status
= NT_STATUS_NO_MEMORY
;
580 if (!tevent_req_poll(req
, ev
)) {
581 status
= map_nt_error_from_unix(errno
);
585 status
= cli_posix_hardlink_recv(req
);
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state
{
601 static void cli_posix_getfacl_done(struct tevent_req
*subreq
);
603 struct tevent_req
*cli_posix_getfacl_send(TALLOC_CTX
*mem_ctx
,
604 struct tevent_context
*ev
,
605 struct cli_state
*cli
,
608 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
609 struct getfacl_state
*state
= NULL
;
611 req
= tevent_req_create(mem_ctx
, &state
, struct getfacl_state
);
615 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_QUERY_POSIX_ACL
,
617 if (tevent_req_nomem(subreq
, req
)) {
618 return tevent_req_post(req
, ev
);
620 tevent_req_set_callback(subreq
, cli_posix_getfacl_done
, req
);
624 static void cli_posix_getfacl_done(struct tevent_req
*subreq
)
626 struct tevent_req
*req
= tevent_req_callback_data(
627 subreq
, struct tevent_req
);
628 struct getfacl_state
*state
= tevent_req_data(
629 req
, struct getfacl_state
);
632 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
635 if (tevent_req_nterror(req
, status
)) {
638 tevent_req_done(req
);
641 NTSTATUS
cli_posix_getfacl_recv(struct tevent_req
*req
,
646 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
649 if (tevent_req_is_nterror(req
, &status
)) {
652 *prb_size
= (size_t)state
->num_data
;
653 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
657 NTSTATUS
cli_posix_getfacl(struct cli_state
*cli
,
663 TALLOC_CTX
*frame
= talloc_stackframe();
664 struct tevent_context
*ev
= NULL
;
665 struct tevent_req
*req
= NULL
;
666 NTSTATUS status
= NT_STATUS_OK
;
668 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
670 * Can't use sync call while an async call is in flight
672 status
= NT_STATUS_INVALID_PARAMETER
;
676 ev
= samba_tevent_context_init(frame
);
678 status
= NT_STATUS_NO_MEMORY
;
682 req
= cli_posix_getfacl_send(frame
,
687 status
= NT_STATUS_NO_MEMORY
;
691 if (!tevent_req_poll(req
, ev
)) {
692 status
= map_nt_error_from_unix(errno
);
696 status
= cli_posix_getfacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
712 static void cli_posix_stat_done(struct tevent_req
*subreq
);
714 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
715 struct tevent_context
*ev
,
716 struct cli_state
*cli
,
719 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
720 struct stat_state
*state
= NULL
;
722 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
726 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
727 SMB_QUERY_FILE_UNIX_BASIC
, 100, 100);
728 if (tevent_req_nomem(subreq
, req
)) {
729 return tevent_req_post(req
, ev
);
731 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
735 static void cli_posix_stat_done(struct tevent_req
*subreq
)
737 struct tevent_req
*req
= tevent_req_callback_data(
738 subreq
, struct tevent_req
);
739 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
742 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
745 if (tevent_req_nterror(req
, status
)) {
748 tevent_req_done(req
);
751 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
752 SMB_STRUCT_STAT
*sbuf
)
754 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
757 if (tevent_req_is_nterror(req
, &status
)) {
761 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
762 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
766 /* assume 512 byte blocks */
767 sbuf
->st_ex_blocks
/= 512;
769 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
770 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
771 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
773 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
774 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
775 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major
= IVAL(state
->data
,60);
779 uint32_t dev_minor
= IVAL(state
->data
,68);
780 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
783 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
784 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
785 sbuf
->st_ex_nlink
= BIG_UINT(state
->data
,92); /* number of hard links */
790 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
792 SMB_STRUCT_STAT
*sbuf
)
794 TALLOC_CTX
*frame
= talloc_stackframe();
795 struct tevent_context
*ev
= NULL
;
796 struct tevent_req
*req
= NULL
;
797 NTSTATUS status
= NT_STATUS_OK
;
799 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
801 * Can't use sync call while an async call is in flight
803 status
= NT_STATUS_INVALID_PARAMETER
;
807 ev
= samba_tevent_context_init(frame
);
809 status
= NT_STATUS_NO_MEMORY
;
813 req
= cli_posix_stat_send(frame
,
818 status
= NT_STATUS_NO_MEMORY
;
822 if (!tevent_req_poll(req
, ev
)) {
823 status
= map_nt_error_from_unix(errno
);
827 status
= cli_posix_stat_recv(req
, sbuf
);
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state
{
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
);
844 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
845 struct tevent_context
*ev
,
846 struct cli_state
*cli
,
852 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
853 struct cli_posix_chown_chmod_internal_state
*state
= NULL
;
855 req
= tevent_req_create(mem_ctx
, &state
,
856 struct cli_posix_chown_chmod_internal_state
);
861 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state
->data
[40], '\0', 60);
863 SIVAL(state
->data
,40,uid
);
864 SIVAL(state
->data
,48,gid
);
865 SIVAL(state
->data
,84,mode
);
867 subreq
= cli_setpathinfo_send(state
, ev
, cli
, SMB_SET_FILE_UNIX_BASIC
,
868 fname
, state
->data
, sizeof(state
->data
));
869 if (tevent_req_nomem(subreq
, req
)) {
870 return tevent_req_post(req
, ev
);
872 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
,
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
879 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
880 tevent_req_simple_finish_ntstatus(subreq
, status
);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
888 struct tevent_context
*ev
,
889 struct cli_state
*cli
,
893 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
895 unix_perms_to_wire(mode
),
900 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
902 return tevent_req_simple_recv_ntstatus(req
);
905 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
907 TALLOC_CTX
*frame
= talloc_stackframe();
908 struct tevent_context
*ev
= NULL
;
909 struct tevent_req
*req
= NULL
;
910 NTSTATUS status
= NT_STATUS_OK
;
912 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
914 * Can't use sync call while an async call is in flight
916 status
= NT_STATUS_INVALID_PARAMETER
;
920 ev
= samba_tevent_context_init(frame
);
922 status
= NT_STATUS_NO_MEMORY
;
926 req
= cli_posix_chmod_send(frame
,
932 status
= NT_STATUS_NO_MEMORY
;
936 if (!tevent_req_poll(req
, ev
)) {
937 status
= map_nt_error_from_unix(errno
);
941 status
= cli_posix_chmod_recv(req
);
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
953 struct tevent_context
*ev
,
954 struct cli_state
*cli
,
959 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
966 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
968 return tevent_req_simple_recv_ntstatus(req
);
971 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
976 TALLOC_CTX
*frame
= talloc_stackframe();
977 struct tevent_context
*ev
= NULL
;
978 struct tevent_req
*req
= NULL
;
979 NTSTATUS status
= NT_STATUS_OK
;
981 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
983 * Can't use sync call while an async call is in flight
985 status
= NT_STATUS_INVALID_PARAMETER
;
989 ev
= samba_tevent_context_init(frame
);
991 status
= NT_STATUS_NO_MEMORY
;
995 req
= cli_posix_chown_send(frame
,
1002 status
= NT_STATUS_NO_MEMORY
;
1006 if (!tevent_req_poll(req
, ev
)) {
1007 status
= map_nt_error_from_unix(errno
);
1011 status
= cli_posix_chown_recv(req
);
1018 /****************************************************************************
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req
*subreq
);
1024 struct cli_rename_state
{
1028 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1029 struct tevent_context
*ev
,
1030 struct cli_state
*cli
,
1031 const char *fname_src
,
1032 const char *fname_dst
)
1034 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1035 struct cli_rename_state
*state
= NULL
;
1036 uint8_t additional_flags
= 0;
1037 uint8_t *bytes
= NULL
;
1039 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
1044 SSVAL(state
->vwv
+0, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1046 bytes
= talloc_array(state
, uint8_t, 1);
1047 if (tevent_req_nomem(bytes
, req
)) {
1048 return tevent_req_post(req
, ev
);
1051 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1052 strlen(fname_src
)+1, NULL
);
1053 if (tevent_req_nomem(bytes
, req
)) {
1054 return tevent_req_post(req
, ev
);
1057 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1058 talloc_get_size(bytes
)+1);
1059 if (tevent_req_nomem(bytes
, req
)) {
1060 return tevent_req_post(req
, ev
);
1063 bytes
[talloc_get_size(bytes
)-1] = 4;
1064 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1065 strlen(fname_dst
)+1, NULL
);
1066 if (tevent_req_nomem(bytes
, req
)) {
1067 return tevent_req_post(req
, ev
);
1070 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1071 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1072 if (tevent_req_nomem(subreq
, req
)) {
1073 return tevent_req_post(req
, ev
);
1075 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
1079 static void cli_rename_done(struct tevent_req
*subreq
)
1081 struct tevent_req
*req
= tevent_req_callback_data(
1082 subreq
, struct tevent_req
);
1085 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1086 TALLOC_FREE(subreq
);
1087 if (tevent_req_nterror(req
, status
)) {
1090 tevent_req_done(req
);
1093 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1095 return tevent_req_simple_recv_ntstatus(req
);
1098 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1100 TALLOC_CTX
*frame
= NULL
;
1101 struct tevent_context
*ev
;
1102 struct tevent_req
*req
;
1103 NTSTATUS status
= NT_STATUS_OK
;
1105 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1106 return cli_smb2_rename(cli
,
1111 frame
= talloc_stackframe();
1113 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1115 * Can't use sync call while an async call is in flight
1117 status
= NT_STATUS_INVALID_PARAMETER
;
1121 ev
= samba_tevent_context_init(frame
);
1123 status
= NT_STATUS_NO_MEMORY
;
1127 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1129 status
= NT_STATUS_NO_MEMORY
;
1133 if (!tevent_req_poll(req
, ev
)) {
1134 status
= map_nt_error_from_unix(errno
);
1138 status
= cli_rename_recv(req
);
1145 /****************************************************************************
1147 ****************************************************************************/
1149 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1151 struct cli_ntrename_internal_state
{
1155 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1156 struct tevent_context
*ev
,
1157 struct cli_state
*cli
,
1158 const char *fname_src
,
1159 const char *fname_dst
,
1160 uint16_t rename_flag
)
1162 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1163 struct cli_ntrename_internal_state
*state
= NULL
;
1164 uint8_t additional_flags
= 0;
1165 uint8_t *bytes
= NULL
;
1167 req
= tevent_req_create(mem_ctx
, &state
,
1168 struct cli_ntrename_internal_state
);
1173 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1174 SSVAL(state
->vwv
+1, 0, rename_flag
);
1176 bytes
= talloc_array(state
, uint8_t, 1);
1177 if (tevent_req_nomem(bytes
, req
)) {
1178 return tevent_req_post(req
, ev
);
1181 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1182 strlen(fname_src
)+1, NULL
);
1183 if (tevent_req_nomem(bytes
, req
)) {
1184 return tevent_req_post(req
, ev
);
1187 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1188 talloc_get_size(bytes
)+1);
1189 if (tevent_req_nomem(bytes
, req
)) {
1190 return tevent_req_post(req
, ev
);
1193 bytes
[talloc_get_size(bytes
)-1] = 4;
1194 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1195 strlen(fname_dst
)+1, NULL
);
1196 if (tevent_req_nomem(bytes
, req
)) {
1197 return tevent_req_post(req
, ev
);
1200 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1201 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1202 if (tevent_req_nomem(subreq
, req
)) {
1203 return tevent_req_post(req
, ev
);
1205 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1209 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1211 struct tevent_req
*req
= tevent_req_callback_data(
1212 subreq
, struct tevent_req
);
1215 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1216 TALLOC_FREE(subreq
);
1217 if (tevent_req_nterror(req
, status
)) {
1220 tevent_req_done(req
);
1223 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1225 return tevent_req_simple_recv_ntstatus(req
);
1228 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1229 struct tevent_context
*ev
,
1230 struct cli_state
*cli
,
1231 const char *fname_src
,
1232 const char *fname_dst
)
1234 return cli_ntrename_internal_send(mem_ctx
,
1239 RENAME_FLAG_RENAME
);
1242 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1244 return cli_ntrename_internal_recv(req
);
1247 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1249 TALLOC_CTX
*frame
= talloc_stackframe();
1250 struct tevent_context
*ev
;
1251 struct tevent_req
*req
;
1252 NTSTATUS status
= NT_STATUS_OK
;
1254 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1256 * Can't use sync call while an async call is in flight
1258 status
= NT_STATUS_INVALID_PARAMETER
;
1262 ev
= samba_tevent_context_init(frame
);
1264 status
= NT_STATUS_NO_MEMORY
;
1268 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1270 status
= NT_STATUS_NO_MEMORY
;
1274 if (!tevent_req_poll(req
, ev
)) {
1275 status
= map_nt_error_from_unix(errno
);
1279 status
= cli_ntrename_recv(req
);
1286 /****************************************************************************
1288 ****************************************************************************/
1290 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1291 struct tevent_context
*ev
,
1292 struct cli_state
*cli
,
1293 const char *fname_src
,
1294 const char *fname_dst
)
1296 return cli_ntrename_internal_send(mem_ctx
,
1301 RENAME_FLAG_HARD_LINK
);
1304 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1306 return cli_ntrename_internal_recv(req
);
1309 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1311 TALLOC_CTX
*frame
= talloc_stackframe();
1312 struct tevent_context
*ev
;
1313 struct tevent_req
*req
;
1314 NTSTATUS status
= NT_STATUS_OK
;
1316 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1318 * Can't use sync call while an async call is in flight
1320 status
= NT_STATUS_INVALID_PARAMETER
;
1324 ev
= samba_tevent_context_init(frame
);
1326 status
= NT_STATUS_NO_MEMORY
;
1330 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1332 status
= NT_STATUS_NO_MEMORY
;
1336 if (!tevent_req_poll(req
, ev
)) {
1337 status
= map_nt_error_from_unix(errno
);
1341 status
= cli_nt_hardlink_recv(req
);
1348 /****************************************************************************
1350 ****************************************************************************/
1352 static void cli_unlink_done(struct tevent_req
*subreq
);
1354 struct cli_unlink_state
{
1358 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1359 struct tevent_context
*ev
,
1360 struct cli_state
*cli
,
1362 uint16_t mayhave_attrs
)
1364 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1365 struct cli_unlink_state
*state
= NULL
;
1366 uint8_t additional_flags
= 0;
1367 uint8_t *bytes
= NULL
;
1369 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1374 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1376 bytes
= talloc_array(state
, uint8_t, 1);
1377 if (tevent_req_nomem(bytes
, req
)) {
1378 return tevent_req_post(req
, ev
);
1381 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
1382 strlen(fname
)+1, NULL
);
1384 if (tevent_req_nomem(bytes
, req
)) {
1385 return tevent_req_post(req
, ev
);
1388 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1389 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1390 if (tevent_req_nomem(subreq
, req
)) {
1391 return tevent_req_post(req
, ev
);
1393 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1397 static void cli_unlink_done(struct tevent_req
*subreq
)
1399 struct tevent_req
*req
= tevent_req_callback_data(
1400 subreq
, struct tevent_req
);
1403 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1404 TALLOC_FREE(subreq
);
1405 if (tevent_req_nterror(req
, status
)) {
1408 tevent_req_done(req
);
1411 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1413 return tevent_req_simple_recv_ntstatus(req
);
1416 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1418 TALLOC_CTX
*frame
= NULL
;
1419 struct tevent_context
*ev
;
1420 struct tevent_req
*req
;
1421 NTSTATUS status
= NT_STATUS_OK
;
1423 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1424 return cli_smb2_unlink(cli
, fname
);
1427 frame
= talloc_stackframe();
1429 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1431 * Can't use sync call while an async call is in flight
1433 status
= NT_STATUS_INVALID_PARAMETER
;
1437 ev
= samba_tevent_context_init(frame
);
1439 status
= NT_STATUS_NO_MEMORY
;
1443 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1445 status
= NT_STATUS_NO_MEMORY
;
1449 if (!tevent_req_poll(req
, ev
)) {
1450 status
= map_nt_error_from_unix(errno
);
1454 status
= cli_unlink_recv(req
);
1461 /****************************************************************************
1463 ****************************************************************************/
1465 static void cli_mkdir_done(struct tevent_req
*subreq
);
1467 struct cli_mkdir_state
{
1471 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1472 struct tevent_context
*ev
,
1473 struct cli_state
*cli
,
1476 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1477 struct cli_mkdir_state
*state
= NULL
;
1478 uint8_t additional_flags
= 0;
1479 uint8_t *bytes
= NULL
;
1481 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1486 bytes
= talloc_array(state
, uint8_t, 1);
1487 if (tevent_req_nomem(bytes
, req
)) {
1488 return tevent_req_post(req
, ev
);
1491 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1492 strlen(dname
)+1, NULL
);
1494 if (tevent_req_nomem(bytes
, req
)) {
1495 return tevent_req_post(req
, ev
);
1498 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1499 0, NULL
, talloc_get_size(bytes
), bytes
);
1500 if (tevent_req_nomem(subreq
, req
)) {
1501 return tevent_req_post(req
, ev
);
1503 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1507 static void cli_mkdir_done(struct tevent_req
*subreq
)
1509 struct tevent_req
*req
= tevent_req_callback_data(
1510 subreq
, struct tevent_req
);
1513 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1514 TALLOC_FREE(subreq
);
1515 if (tevent_req_nterror(req
, status
)) {
1518 tevent_req_done(req
);
1521 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1523 return tevent_req_simple_recv_ntstatus(req
);
1526 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1528 TALLOC_CTX
*frame
= NULL
;
1529 struct tevent_context
*ev
;
1530 struct tevent_req
*req
;
1531 NTSTATUS status
= NT_STATUS_OK
;
1533 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1534 return cli_smb2_mkdir(cli
, dname
);
1537 frame
= talloc_stackframe();
1539 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1541 * Can't use sync call while an async call is in flight
1543 status
= NT_STATUS_INVALID_PARAMETER
;
1547 ev
= samba_tevent_context_init(frame
);
1549 status
= NT_STATUS_NO_MEMORY
;
1553 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1555 status
= NT_STATUS_NO_MEMORY
;
1559 if (!tevent_req_poll(req
, ev
)) {
1560 status
= map_nt_error_from_unix(errno
);
1564 status
= cli_mkdir_recv(req
);
1571 /****************************************************************************
1573 ****************************************************************************/
1575 static void cli_rmdir_done(struct tevent_req
*subreq
);
1577 struct cli_rmdir_state
{
1581 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1582 struct tevent_context
*ev
,
1583 struct cli_state
*cli
,
1586 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1587 struct cli_rmdir_state
*state
= NULL
;
1588 uint8_t additional_flags
= 0;
1589 uint8_t *bytes
= NULL
;
1591 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1596 bytes
= talloc_array(state
, uint8_t, 1);
1597 if (tevent_req_nomem(bytes
, req
)) {
1598 return tevent_req_post(req
, ev
);
1601 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1602 strlen(dname
)+1, NULL
);
1604 if (tevent_req_nomem(bytes
, req
)) {
1605 return tevent_req_post(req
, ev
);
1608 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1609 0, NULL
, talloc_get_size(bytes
), bytes
);
1610 if (tevent_req_nomem(subreq
, req
)) {
1611 return tevent_req_post(req
, ev
);
1613 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1617 static void cli_rmdir_done(struct tevent_req
*subreq
)
1619 struct tevent_req
*req
= tevent_req_callback_data(
1620 subreq
, struct tevent_req
);
1623 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1624 TALLOC_FREE(subreq
);
1625 if (tevent_req_nterror(req
, status
)) {
1628 tevent_req_done(req
);
1631 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1633 return tevent_req_simple_recv_ntstatus(req
);
1636 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1638 TALLOC_CTX
*frame
= NULL
;
1639 struct tevent_context
*ev
;
1640 struct tevent_req
*req
;
1641 NTSTATUS status
= NT_STATUS_OK
;
1643 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1644 return cli_smb2_rmdir(cli
, dname
);
1647 frame
= talloc_stackframe();
1649 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1651 * Can't use sync call while an async call is in flight
1653 status
= NT_STATUS_INVALID_PARAMETER
;
1657 ev
= samba_tevent_context_init(frame
);
1659 status
= NT_STATUS_NO_MEMORY
;
1663 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1665 status
= NT_STATUS_NO_MEMORY
;
1669 if (!tevent_req_poll(req
, ev
)) {
1670 status
= map_nt_error_from_unix(errno
);
1674 status
= cli_rmdir_recv(req
);
1681 /****************************************************************************
1682 Set or clear the delete on close flag.
1683 ****************************************************************************/
1691 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1693 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1694 NULL
, 0, NULL
, NULL
, 0, NULL
);
1695 tevent_req_simple_finish_ntstatus(subreq
, status
);
1698 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1699 struct tevent_context
*ev
,
1700 struct cli_state
*cli
,
1704 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1705 struct doc_state
*state
= NULL
;
1707 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1712 /* Setup setup word. */
1713 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1715 /* Setup param array. */
1716 SSVAL(state
->param
,0,fnum
);
1717 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1719 /* Setup data array. */
1720 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1722 subreq
= cli_trans_send(state
, /* mem ctx. */
1723 ev
, /* event ctx. */
1724 cli
, /* cli_state. */
1725 SMBtrans2
, /* cmd. */
1726 NULL
, /* pipe name. */
1730 &state
->setup
, /* setup. */
1731 1, /* num setup uint16_t words. */
1732 0, /* max returned setup. */
1733 state
->param
, /* param. */
1735 2, /* max returned param. */
1736 state
->data
, /* data. */
1738 0); /* max returned data. */
1740 if (tevent_req_nomem(subreq
, req
)) {
1741 return tevent_req_post(req
, ev
);
1743 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1747 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1749 return tevent_req_simple_recv_ntstatus(req
);
1752 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1754 TALLOC_CTX
*frame
= talloc_stackframe();
1755 struct tevent_context
*ev
= NULL
;
1756 struct tevent_req
*req
= NULL
;
1757 NTSTATUS status
= NT_STATUS_OK
;
1759 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1761 * Can't use sync call while an async call is in flight
1763 status
= NT_STATUS_INVALID_PARAMETER
;
1767 ev
= samba_tevent_context_init(frame
);
1769 status
= NT_STATUS_NO_MEMORY
;
1773 req
= cli_nt_delete_on_close_send(frame
,
1779 status
= NT_STATUS_NO_MEMORY
;
1783 if (!tevent_req_poll(req
, ev
)) {
1784 status
= map_nt_error_from_unix(errno
);
1788 status
= cli_nt_delete_on_close_recv(req
);
1795 struct cli_ntcreate1_state
{
1798 struct smb_create_returns cr
;
1801 static void cli_ntcreate1_done(struct tevent_req
*subreq
);
1803 static struct tevent_req
*cli_ntcreate1_send(TALLOC_CTX
*mem_ctx
,
1804 struct tevent_context
*ev
,
1805 struct cli_state
*cli
,
1807 uint32_t CreatFlags
,
1808 uint32_t DesiredAccess
,
1809 uint32_t FileAttributes
,
1810 uint32_t ShareAccess
,
1811 uint32_t CreateDisposition
,
1812 uint32_t CreateOptions
,
1813 uint8_t SecurityFlags
)
1815 struct tevent_req
*req
, *subreq
;
1816 struct cli_ntcreate1_state
*state
;
1819 size_t converted_len
;
1821 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate1_state
);
1828 SCVAL(vwv
+0, 0, 0xFF);
1833 if (cli
->use_oplocks
) {
1834 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1836 SIVAL(vwv
+3, 1, CreatFlags
);
1837 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1838 SIVAL(vwv
+7, 1, DesiredAccess
);
1839 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1841 SIVAL(vwv
+13, 1, FileAttributes
);
1842 SIVAL(vwv
+15, 1, ShareAccess
);
1843 SIVAL(vwv
+17, 1, CreateDisposition
);
1844 SIVAL(vwv
+19, 1, CreateOptions
|
1845 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1846 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1847 SCVAL(vwv
+23, 1, SecurityFlags
);
1849 bytes
= talloc_array(state
, uint8_t, 0);
1850 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1851 fname
, strlen(fname
)+1,
1854 /* sigh. this copes with broken netapp filer behaviour */
1855 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1857 if (tevent_req_nomem(bytes
, req
)) {
1858 return tevent_req_post(req
, ev
);
1861 SSVAL(vwv
+2, 1, converted_len
);
1863 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1864 talloc_get_size(bytes
), bytes
);
1865 if (tevent_req_nomem(subreq
, req
)) {
1866 return tevent_req_post(req
, ev
);
1868 tevent_req_set_callback(subreq
, cli_ntcreate1_done
, req
);
1872 static void cli_ntcreate1_done(struct tevent_req
*subreq
)
1874 struct tevent_req
*req
= tevent_req_callback_data(
1875 subreq
, struct tevent_req
);
1876 struct cli_ntcreate1_state
*state
= tevent_req_data(
1877 req
, struct cli_ntcreate1_state
);
1884 status
= cli_smb_recv(subreq
, state
, NULL
, 34, &wct
, &vwv
,
1885 &num_bytes
, &bytes
);
1886 TALLOC_FREE(subreq
);
1887 if (tevent_req_nterror(req
, status
)) {
1890 state
->cr
.oplock_level
= CVAL(vwv
+2, 0);
1891 state
->fnum
= SVAL(vwv
+2, 1);
1892 state
->cr
.create_action
= IVAL(vwv
+3, 1);
1893 state
->cr
.creation_time
= BVAL(vwv
+5, 1);
1894 state
->cr
.last_access_time
= BVAL(vwv
+9, 1);
1895 state
->cr
.last_write_time
= BVAL(vwv
+13, 1);
1896 state
->cr
.change_time
= BVAL(vwv
+17, 1);
1897 state
->cr
.file_attributes
= IVAL(vwv
+21, 1);
1898 state
->cr
.allocation_size
= BVAL(vwv
+23, 1);
1899 state
->cr
.end_of_file
= BVAL(vwv
+27, 1);
1901 tevent_req_done(req
);
1904 static NTSTATUS
cli_ntcreate1_recv(struct tevent_req
*req
,
1906 struct smb_create_returns
*cr
)
1908 struct cli_ntcreate1_state
*state
= tevent_req_data(
1909 req
, struct cli_ntcreate1_state
);
1912 if (tevent_req_is_nterror(req
, &status
)) {
1915 *pfnum
= state
->fnum
;
1919 return NT_STATUS_OK
;
1922 struct cli_ntcreate_state
{
1923 NTSTATUS (*recv
)(struct tevent_req
*req
, uint16_t *fnum
,
1924 struct smb_create_returns
*cr
);
1925 struct smb_create_returns cr
;
1929 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1931 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1932 struct tevent_context
*ev
,
1933 struct cli_state
*cli
,
1935 uint32_t create_flags
,
1936 uint32_t desired_access
,
1937 uint32_t file_attributes
,
1938 uint32_t share_access
,
1939 uint32_t create_disposition
,
1940 uint32_t create_options
,
1941 uint8_t security_flags
)
1943 struct tevent_req
*req
, *subreq
;
1944 struct cli_ntcreate_state
*state
;
1946 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1951 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1952 state
->recv
= cli_smb2_create_fnum_recv
;
1954 if (cli
->use_oplocks
) {
1955 create_flags
|= REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
;
1958 subreq
= cli_smb2_create_fnum_send(
1959 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1960 file_attributes
, share_access
, create_disposition
,
1963 state
->recv
= cli_ntcreate1_recv
;
1964 subreq
= cli_ntcreate1_send(
1965 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1966 file_attributes
, share_access
, create_disposition
,
1967 create_options
, security_flags
);
1969 if (tevent_req_nomem(subreq
, req
)) {
1970 return tevent_req_post(req
, ev
);
1972 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1976 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1978 struct tevent_req
*req
= tevent_req_callback_data(
1979 subreq
, struct tevent_req
);
1980 struct cli_ntcreate_state
*state
= tevent_req_data(
1981 req
, struct cli_ntcreate_state
);
1984 status
= state
->recv(subreq
, &state
->fnum
, &state
->cr
);
1985 TALLOC_FREE(subreq
);
1986 if (tevent_req_nterror(req
, status
)) {
1989 tevent_req_done(req
);
1992 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *fnum
,
1993 struct smb_create_returns
*cr
)
1995 struct cli_ntcreate_state
*state
= tevent_req_data(
1996 req
, struct cli_ntcreate_state
);
1999 if (tevent_req_is_nterror(req
, &status
)) {
2003 *fnum
= state
->fnum
;
2008 return NT_STATUS_OK
;
2011 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2013 uint32_t CreatFlags
,
2014 uint32_t DesiredAccess
,
2015 uint32_t FileAttributes
,
2016 uint32_t ShareAccess
,
2017 uint32_t CreateDisposition
,
2018 uint32_t CreateOptions
,
2019 uint8_t SecurityFlags
,
2021 struct smb_create_returns
*cr
)
2023 TALLOC_CTX
*frame
= talloc_stackframe();
2024 struct tevent_context
*ev
;
2025 struct tevent_req
*req
;
2026 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2028 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2030 * Can't use sync call while an async call is in flight
2032 status
= NT_STATUS_INVALID_PARAMETER
;
2036 ev
= samba_tevent_context_init(frame
);
2041 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2042 DesiredAccess
, FileAttributes
, ShareAccess
,
2043 CreateDisposition
, CreateOptions
,
2049 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2053 status
= cli_ntcreate_recv(req
, pfid
, cr
);
2059 struct cli_nttrans_create_state
{
2061 struct smb_create_returns cr
;
2064 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
2066 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
2067 struct tevent_context
*ev
,
2068 struct cli_state
*cli
,
2070 uint32_t CreatFlags
,
2071 uint32_t DesiredAccess
,
2072 uint32_t FileAttributes
,
2073 uint32_t ShareAccess
,
2074 uint32_t CreateDisposition
,
2075 uint32_t CreateOptions
,
2076 uint8_t SecurityFlags
,
2077 struct security_descriptor
*secdesc
,
2078 struct ea_struct
*eas
,
2081 struct tevent_req
*req
, *subreq
;
2082 struct cli_nttrans_create_state
*state
;
2084 uint8_t *secdesc_buf
;
2087 size_t converted_len
;
2089 req
= tevent_req_create(mem_ctx
,
2090 &state
, struct cli_nttrans_create_state
);
2095 if (secdesc
!= NULL
) {
2096 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2097 &secdesc_buf
, &secdesc_len
);
2098 if (tevent_req_nterror(req
, status
)) {
2099 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2100 nt_errstr(status
)));
2101 return tevent_req_post(req
, ev
);
2112 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2113 return tevent_req_post(req
, ev
);
2116 param
= talloc_array(state
, uint8_t, 53);
2117 if (tevent_req_nomem(param
, req
)) {
2118 return tevent_req_post(req
, ev
);
2121 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2122 fname
, strlen(fname
),
2124 if (tevent_req_nomem(param
, req
)) {
2125 return tevent_req_post(req
, ev
);
2128 SIVAL(param
, 0, CreatFlags
);
2129 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2130 SIVAL(param
, 8, DesiredAccess
);
2131 SIVAL(param
, 12, 0x0); /* AllocationSize */
2132 SIVAL(param
, 16, 0x0); /* AllocationSize */
2133 SIVAL(param
, 20, FileAttributes
);
2134 SIVAL(param
, 24, ShareAccess
);
2135 SIVAL(param
, 28, CreateDisposition
);
2136 SIVAL(param
, 32, CreateOptions
|
2137 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2138 SIVAL(param
, 36, secdesc_len
);
2139 SIVAL(param
, 40, 0); /* EA length*/
2140 SIVAL(param
, 44, converted_len
);
2141 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2142 SCVAL(param
, 52, SecurityFlags
);
2144 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2145 NULL
, -1, /* name, fid */
2146 NT_TRANSACT_CREATE
, 0,
2147 NULL
, 0, 0, /* setup */
2148 param
, talloc_get_size(param
), 128, /* param */
2149 secdesc_buf
, secdesc_len
, 0); /* data */
2150 if (tevent_req_nomem(subreq
, req
)) {
2151 return tevent_req_post(req
, ev
);
2153 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2157 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2159 struct tevent_req
*req
= tevent_req_callback_data(
2160 subreq
, struct tevent_req
);
2161 struct cli_nttrans_create_state
*state
= tevent_req_data(
2162 req
, struct cli_nttrans_create_state
);
2167 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2168 NULL
, 0, NULL
, /* rsetup */
2169 ¶m
, 69, &num_param
,
2171 if (tevent_req_nterror(req
, status
)) {
2174 state
->cr
.oplock_level
= CVAL(param
, 0);
2175 state
->fnum
= SVAL(param
, 2);
2176 state
->cr
.create_action
= IVAL(param
, 4);
2177 state
->cr
.creation_time
= BVAL(param
, 12);
2178 state
->cr
.last_access_time
= BVAL(param
, 20);
2179 state
->cr
.last_write_time
= BVAL(param
, 28);
2180 state
->cr
.change_time
= BVAL(param
, 36);
2181 state
->cr
.file_attributes
= IVAL(param
, 44);
2182 state
->cr
.allocation_size
= BVAL(param
, 48);
2183 state
->cr
.end_of_file
= BVAL(param
, 56);
2186 tevent_req_done(req
);
2189 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2191 struct smb_create_returns
*cr
)
2193 struct cli_nttrans_create_state
*state
= tevent_req_data(
2194 req
, struct cli_nttrans_create_state
);
2197 if (tevent_req_is_nterror(req
, &status
)) {
2200 *fnum
= state
->fnum
;
2204 return NT_STATUS_OK
;
2207 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2209 uint32_t CreatFlags
,
2210 uint32_t DesiredAccess
,
2211 uint32_t FileAttributes
,
2212 uint32_t ShareAccess
,
2213 uint32_t CreateDisposition
,
2214 uint32_t CreateOptions
,
2215 uint8_t SecurityFlags
,
2216 struct security_descriptor
*secdesc
,
2217 struct ea_struct
*eas
,
2220 struct smb_create_returns
*cr
)
2222 TALLOC_CTX
*frame
= talloc_stackframe();
2223 struct tevent_context
*ev
;
2224 struct tevent_req
*req
;
2225 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2227 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2229 * Can't use sync call while an async call is in flight
2231 status
= NT_STATUS_INVALID_PARAMETER
;
2234 ev
= samba_tevent_context_init(frame
);
2238 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2239 DesiredAccess
, FileAttributes
,
2240 ShareAccess
, CreateDisposition
,
2241 CreateOptions
, SecurityFlags
,
2242 secdesc
, eas
, num_eas
);
2246 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2249 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2255 /****************************************************************************
2257 WARNING: if you open with O_WRONLY then getattrE won't work!
2258 ****************************************************************************/
2260 struct cli_openx_state
{
2267 static void cli_openx_done(struct tevent_req
*subreq
);
2269 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2270 struct tevent_context
*ev
,
2271 struct cli_state
*cli
, const char *fname
,
2272 int flags
, int share_mode
,
2273 struct tevent_req
**psmbreq
)
2275 struct tevent_req
*req
, *subreq
;
2276 struct cli_openx_state
*state
;
2278 unsigned accessmode
;
2279 uint8_t additional_flags
;
2282 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2288 if (flags
& O_CREAT
) {
2291 if (!(flags
& O_EXCL
)) {
2292 if (flags
& O_TRUNC
)
2298 accessmode
= (share_mode
<<4);
2300 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2302 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2307 if ((flags
& O_SYNC
) == O_SYNC
) {
2308 accessmode
|= (1<<14);
2312 if (share_mode
== DENY_FCB
) {
2316 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2317 SCVAL(state
->vwv
+ 0, 1, 0);
2318 SSVAL(state
->vwv
+ 1, 0, 0);
2319 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2320 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2321 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2322 SSVAL(state
->vwv
+ 5, 0, 0);
2323 SIVAL(state
->vwv
+ 6, 0, 0);
2324 SSVAL(state
->vwv
+ 8, 0, openfn
);
2325 SIVAL(state
->vwv
+ 9, 0, 0);
2326 SIVAL(state
->vwv
+ 11, 0, 0);
2327 SIVAL(state
->vwv
+ 13, 0, 0);
2329 additional_flags
= 0;
2331 if (cli
->use_oplocks
) {
2332 /* if using oplocks then ask for a batch oplock via
2333 core and extended methods */
2335 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2336 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2339 bytes
= talloc_array(state
, uint8_t, 0);
2340 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2341 strlen(fname
)+1, NULL
);
2343 if (tevent_req_nomem(bytes
, req
)) {
2344 return tevent_req_post(req
, ev
);
2347 state
->bytes
.iov_base
= (void *)bytes
;
2348 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2350 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2351 15, state
->vwv
, 1, &state
->bytes
);
2352 if (subreq
== NULL
) {
2356 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2361 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2362 struct cli_state
*cli
, const char *fname
,
2363 int flags
, int share_mode
)
2365 struct tevent_req
*req
, *subreq
;
2368 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2374 status
= smb1cli_req_chain_submit(&subreq
, 1);
2375 if (tevent_req_nterror(req
, status
)) {
2376 return tevent_req_post(req
, ev
);
2381 static void cli_openx_done(struct tevent_req
*subreq
)
2383 struct tevent_req
*req
= tevent_req_callback_data(
2384 subreq
, struct tevent_req
);
2385 struct cli_openx_state
*state
= tevent_req_data(
2386 req
, struct cli_openx_state
);
2391 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2393 TALLOC_FREE(subreq
);
2394 if (tevent_req_nterror(req
, status
)) {
2397 state
->fnum
= SVAL(vwv
+2, 0);
2398 tevent_req_done(req
);
2401 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2403 struct cli_openx_state
*state
= tevent_req_data(
2404 req
, struct cli_openx_state
);
2407 if (tevent_req_is_nterror(req
, &status
)) {
2410 *pfnum
= state
->fnum
;
2411 return NT_STATUS_OK
;
2414 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2415 int share_mode
, uint16_t *pfnum
)
2417 TALLOC_CTX
*frame
= talloc_stackframe();
2418 struct tevent_context
*ev
;
2419 struct tevent_req
*req
;
2420 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2422 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2424 * Can't use sync call while an async call is in flight
2426 status
= NT_STATUS_INVALID_PARAMETER
;
2430 ev
= samba_tevent_context_init(frame
);
2435 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2440 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2444 status
= cli_openx_recv(req
, pfnum
);
2449 /****************************************************************************
2450 Synchronous wrapper function that does an NtCreateX open by preference
2451 and falls back to openX if this fails.
2452 ****************************************************************************/
2454 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2455 int share_mode_in
, uint16_t *pfnum
)
2458 unsigned int openfn
= 0;
2459 unsigned int dos_deny
= 0;
2460 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2461 struct smb_create_returns cr
;
2463 /* Do the initial mapping into OpenX parameters. */
2464 if (flags
& O_CREAT
) {
2467 if (!(flags
& O_EXCL
)) {
2468 if (flags
& O_TRUNC
)
2474 dos_deny
= (share_mode_in
<<4);
2476 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2478 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2483 if ((flags
& O_SYNC
) == O_SYNC
) {
2484 dos_deny
|= (1<<14);
2488 if (share_mode_in
== DENY_FCB
) {
2493 /* Hmmm. This is what I think the above code
2494 should look like if it's using the constants
2497 if (flags
& O_CREAT
) {
2498 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2500 if (!(flags
& O_EXCL
)) {
2501 if (flags
& O_TRUNC
)
2502 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2504 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2507 dos_deny
= SET_DENY_MODE(share_mode_in
);
2509 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2510 dos_deny
|= DOS_OPEN_RDWR
;
2511 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2512 dos_deny
|= DOS_OPEN_WRONLY
;
2516 if ((flags
& O_SYNC
) == O_SYNC
) {
2517 dos_deny
|= FILE_SYNC_OPENMODE
;
2521 if (share_mode_in
== DENY_FCB
) {
2526 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2527 openfn
, &access_mask
,
2528 &share_mode
, &create_disposition
,
2529 &create_options
, NULL
)) {
2533 status
= cli_ntcreate(cli
,
2545 /* Try and cope will all varients of "we don't do this call"
2546 and fall back to openX. */
2548 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2549 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2550 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2551 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2552 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2553 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2554 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2555 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2556 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2560 if (NT_STATUS_IS_OK(status
) &&
2561 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2562 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2565 * Some (broken) servers return a valid handle
2566 * for directories even if FILE_NON_DIRECTORY_FILE
2567 * is set. Just close the handle and set the
2568 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2570 status
= cli_close(cli
, *pfnum
);
2571 if (!NT_STATUS_IS_OK(status
)) {
2574 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2575 /* Set this so libsmbclient can retrieve it. */
2576 cli
->raw_status
= status
;
2583 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2586 /****************************************************************************
2588 ****************************************************************************/
2590 struct cli_close_state
{
2594 static void cli_close_done(struct tevent_req
*subreq
);
2596 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2597 struct tevent_context
*ev
,
2598 struct cli_state
*cli
,
2600 struct tevent_req
**psubreq
)
2602 struct tevent_req
*req
, *subreq
;
2603 struct cli_close_state
*state
;
2605 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2610 SSVAL(state
->vwv
+0, 0, fnum
);
2611 SIVALS(state
->vwv
+1, 0, -1);
2613 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2615 if (subreq
== NULL
) {
2619 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2624 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2625 struct tevent_context
*ev
,
2626 struct cli_state
*cli
,
2629 struct tevent_req
*req
, *subreq
;
2632 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2637 status
= smb1cli_req_chain_submit(&subreq
, 1);
2638 if (tevent_req_nterror(req
, status
)) {
2639 return tevent_req_post(req
, ev
);
2644 static void cli_close_done(struct tevent_req
*subreq
)
2646 struct tevent_req
*req
= tevent_req_callback_data(
2647 subreq
, struct tevent_req
);
2650 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2651 TALLOC_FREE(subreq
);
2652 if (tevent_req_nterror(req
, status
)) {
2655 tevent_req_done(req
);
2658 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2660 return tevent_req_simple_recv_ntstatus(req
);
2663 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2665 TALLOC_CTX
*frame
= NULL
;
2666 struct tevent_context
*ev
;
2667 struct tevent_req
*req
;
2668 NTSTATUS status
= NT_STATUS_OK
;
2670 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2671 return cli_smb2_close_fnum(cli
, fnum
);
2674 frame
= talloc_stackframe();
2676 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2678 * Can't use sync call while an async call is in flight
2680 status
= NT_STATUS_INVALID_PARAMETER
;
2684 ev
= samba_tevent_context_init(frame
);
2686 status
= NT_STATUS_NO_MEMORY
;
2690 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2692 status
= NT_STATUS_NO_MEMORY
;
2696 if (!tevent_req_poll(req
, ev
)) {
2697 status
= map_nt_error_from_unix(errno
);
2701 status
= cli_close_recv(req
);
2707 /****************************************************************************
2708 Truncate a file to a specified size
2709 ****************************************************************************/
2711 struct ftrunc_state
{
2717 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2719 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2720 NULL
, 0, NULL
, NULL
, 0, NULL
);
2721 tevent_req_simple_finish_ntstatus(subreq
, status
);
2724 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2725 struct tevent_context
*ev
,
2726 struct cli_state
*cli
,
2730 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2731 struct ftrunc_state
*state
= NULL
;
2733 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2738 /* Setup setup word. */
2739 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2741 /* Setup param array. */
2742 SSVAL(state
->param
,0,fnum
);
2743 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2744 SSVAL(state
->param
,4,0);
2746 /* Setup data array. */
2747 SBVAL(state
->data
, 0, size
);
2749 subreq
= cli_trans_send(state
, /* mem ctx. */
2750 ev
, /* event ctx. */
2751 cli
, /* cli_state. */
2752 SMBtrans2
, /* cmd. */
2753 NULL
, /* pipe name. */
2757 &state
->setup
, /* setup. */
2758 1, /* num setup uint16_t words. */
2759 0, /* max returned setup. */
2760 state
->param
, /* param. */
2762 2, /* max returned param. */
2763 state
->data
, /* data. */
2765 0); /* max returned data. */
2767 if (tevent_req_nomem(subreq
, req
)) {
2768 return tevent_req_post(req
, ev
);
2770 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2774 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2776 return tevent_req_simple_recv_ntstatus(req
);
2779 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2781 TALLOC_CTX
*frame
= talloc_stackframe();
2782 struct tevent_context
*ev
= NULL
;
2783 struct tevent_req
*req
= NULL
;
2784 NTSTATUS status
= NT_STATUS_OK
;
2786 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2788 * Can't use sync call while an async call is in flight
2790 status
= NT_STATUS_INVALID_PARAMETER
;
2794 ev
= samba_tevent_context_init(frame
);
2796 status
= NT_STATUS_NO_MEMORY
;
2800 req
= cli_ftruncate_send(frame
,
2806 status
= NT_STATUS_NO_MEMORY
;
2810 if (!tevent_req_poll(req
, ev
)) {
2811 status
= map_nt_error_from_unix(errno
);
2815 status
= cli_ftruncate_recv(req
);
2822 /****************************************************************************
2823 send a lock with a specified locktype
2824 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2825 ****************************************************************************/
2827 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2828 uint32_t offset
, uint32_t len
,
2829 int timeout
, unsigned char locktype
)
2834 unsigned int set_timeout
= 0;
2835 unsigned int saved_timeout
= 0;
2837 SCVAL(vwv
+ 0, 0, 0xff);
2838 SCVAL(vwv
+ 0, 1, 0);
2839 SSVAL(vwv
+ 1, 0, 0);
2840 SSVAL(vwv
+ 2, 0, fnum
);
2841 SCVAL(vwv
+ 3, 0, locktype
);
2842 SCVAL(vwv
+ 3, 1, 0);
2843 SIVALS(vwv
+ 4, 0, timeout
);
2844 SSVAL(vwv
+ 6, 0, 0);
2845 SSVAL(vwv
+ 7, 0, 1);
2847 SSVAL(bytes
, 0, cli_getpid(cli
));
2848 SIVAL(bytes
, 2, offset
);
2849 SIVAL(bytes
, 6, len
);
2852 if (timeout
== -1) {
2853 set_timeout
= 0x7FFFFFFF;
2855 set_timeout
= timeout
+ 2*1000;
2857 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2860 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2861 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2863 if (saved_timeout
!= 0) {
2864 cli_set_timeout(cli
, saved_timeout
);
2870 /****************************************************************************
2872 note that timeout is in units of 2 milliseconds
2873 ****************************************************************************/
2875 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2876 uint32_t offset
, uint32_t len
, int timeout
,
2877 enum brl_type lock_type
)
2881 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2882 (lock_type
== READ_LOCK
? 1 : 0));
2886 /****************************************************************************
2888 ****************************************************************************/
2890 struct cli_unlock_state
{
2895 static void cli_unlock_done(struct tevent_req
*subreq
);
2897 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2898 struct tevent_context
*ev
,
2899 struct cli_state
*cli
,
2905 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2906 struct cli_unlock_state
*state
= NULL
;
2907 uint8_t additional_flags
= 0;
2909 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2914 SCVAL(state
->vwv
+0, 0, 0xFF);
2915 SSVAL(state
->vwv
+2, 0, fnum
);
2916 SCVAL(state
->vwv
+3, 0, 0);
2917 SIVALS(state
->vwv
+4, 0, 0);
2918 SSVAL(state
->vwv
+6, 0, 1);
2919 SSVAL(state
->vwv
+7, 0, 0);
2921 SSVAL(state
->data
, 0, cli_getpid(cli
));
2922 SIVAL(state
->data
, 2, offset
);
2923 SIVAL(state
->data
, 6, len
);
2925 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2926 8, state
->vwv
, 10, state
->data
);
2927 if (tevent_req_nomem(subreq
, req
)) {
2928 return tevent_req_post(req
, ev
);
2930 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2934 static void cli_unlock_done(struct tevent_req
*subreq
)
2936 struct tevent_req
*req
= tevent_req_callback_data(
2937 subreq
, struct tevent_req
);
2940 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2941 TALLOC_FREE(subreq
);
2942 if (tevent_req_nterror(req
, status
)) {
2945 tevent_req_done(req
);
2948 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2950 return tevent_req_simple_recv_ntstatus(req
);
2953 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2958 TALLOC_CTX
*frame
= talloc_stackframe();
2959 struct tevent_context
*ev
;
2960 struct tevent_req
*req
;
2961 NTSTATUS status
= NT_STATUS_OK
;
2963 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2965 * Can't use sync call while an async call is in flight
2967 status
= NT_STATUS_INVALID_PARAMETER
;
2971 ev
= samba_tevent_context_init(frame
);
2973 status
= NT_STATUS_NO_MEMORY
;
2977 req
= cli_unlock_send(frame
, ev
, cli
,
2980 status
= NT_STATUS_NO_MEMORY
;
2984 if (!tevent_req_poll(req
, ev
)) {
2985 status
= map_nt_error_from_unix(errno
);
2989 status
= cli_unlock_recv(req
);
2996 /****************************************************************************
2997 Lock a file with 64 bit offsets.
2998 ****************************************************************************/
3000 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
3001 uint64_t offset
, uint64_t len
, int timeout
,
3002 enum brl_type lock_type
)
3006 unsigned int set_timeout
= 0;
3007 unsigned int saved_timeout
= 0;
3011 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3012 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
3015 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
3016 ltype
|= LOCKING_ANDX_LARGE_FILES
;
3018 SCVAL(vwv
+ 0, 0, 0xff);
3019 SCVAL(vwv
+ 0, 1, 0);
3020 SSVAL(vwv
+ 1, 0, 0);
3021 SSVAL(vwv
+ 2, 0, fnum
);
3022 SCVAL(vwv
+ 3, 0, ltype
);
3023 SCVAL(vwv
+ 3, 1, 0);
3024 SIVALS(vwv
+ 4, 0, timeout
);
3025 SSVAL(vwv
+ 6, 0, 0);
3026 SSVAL(vwv
+ 7, 0, 1);
3028 SIVAL(bytes
, 0, cli_getpid(cli
));
3029 SOFF_T_R(bytes
, 4, offset
);
3030 SOFF_T_R(bytes
, 12, len
);
3033 if (timeout
== -1) {
3034 set_timeout
= 0x7FFFFFFF;
3036 set_timeout
= timeout
+ 2*1000;
3038 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3041 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3042 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3044 if (saved_timeout
!= 0) {
3045 cli_set_timeout(cli
, saved_timeout
);
3051 /****************************************************************************
3052 Unlock a file with 64 bit offsets.
3053 ****************************************************************************/
3055 struct cli_unlock64_state
{
3060 static void cli_unlock64_done(struct tevent_req
*subreq
);
3062 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
3063 struct tevent_context
*ev
,
3064 struct cli_state
*cli
,
3070 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3071 struct cli_unlock64_state
*state
= NULL
;
3072 uint8_t additional_flags
= 0;
3074 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3079 SCVAL(state
->vwv
+0, 0, 0xff);
3080 SSVAL(state
->vwv
+2, 0, fnum
);
3081 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3082 SIVALS(state
->vwv
+4, 0, 0);
3083 SSVAL(state
->vwv
+6, 0, 1);
3084 SSVAL(state
->vwv
+7, 0, 0);
3086 SIVAL(state
->data
, 0, cli_getpid(cli
));
3087 SOFF_T_R(state
->data
, 4, offset
);
3088 SOFF_T_R(state
->data
, 12, len
);
3090 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
3091 8, state
->vwv
, 20, state
->data
);
3092 if (tevent_req_nomem(subreq
, req
)) {
3093 return tevent_req_post(req
, ev
);
3095 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3099 static void cli_unlock64_done(struct tevent_req
*subreq
)
3101 struct tevent_req
*req
= tevent_req_callback_data(
3102 subreq
, struct tevent_req
);
3105 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3106 TALLOC_FREE(subreq
);
3107 if (tevent_req_nterror(req
, status
)) {
3110 tevent_req_done(req
);
3113 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3115 return tevent_req_simple_recv_ntstatus(req
);
3118 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3123 TALLOC_CTX
*frame
= talloc_stackframe();
3124 struct tevent_context
*ev
;
3125 struct tevent_req
*req
;
3126 NTSTATUS status
= NT_STATUS_OK
;
3128 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3129 return cli_unlock(cli
, fnum
, offset
, len
);
3132 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3134 * Can't use sync call while an async call is in flight
3136 status
= NT_STATUS_INVALID_PARAMETER
;
3140 ev
= samba_tevent_context_init(frame
);
3142 status
= NT_STATUS_NO_MEMORY
;
3146 req
= cli_unlock64_send(frame
, ev
, cli
,
3149 status
= NT_STATUS_NO_MEMORY
;
3153 if (!tevent_req_poll(req
, ev
)) {
3154 status
= map_nt_error_from_unix(errno
);
3158 status
= cli_unlock64_recv(req
);
3165 /****************************************************************************
3166 Get/unlock a POSIX lock on a file - internal function.
3167 ****************************************************************************/
3169 struct posix_lock_state
{
3172 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3175 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3177 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3178 NULL
, 0, NULL
, NULL
, 0, NULL
);
3179 tevent_req_simple_finish_ntstatus(subreq
, status
);
3182 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3183 struct tevent_context
*ev
,
3184 struct cli_state
*cli
,
3189 enum brl_type lock_type
)
3191 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3192 struct posix_lock_state
*state
= NULL
;
3194 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3199 /* Setup setup word. */
3200 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3202 /* Setup param array. */
3203 SSVAL(&state
->param
, 0, fnum
);
3204 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3206 /* Setup data array. */
3207 switch (lock_type
) {
3209 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3210 POSIX_LOCK_TYPE_READ
);
3213 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3214 POSIX_LOCK_TYPE_WRITE
);
3217 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3218 POSIX_LOCK_TYPE_UNLOCK
);
3225 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3226 POSIX_LOCK_FLAG_WAIT
);
3228 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3229 POSIX_LOCK_FLAG_NOWAIT
);
3232 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3233 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3234 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3236 subreq
= cli_trans_send(state
, /* mem ctx. */
3237 ev
, /* event ctx. */
3238 cli
, /* cli_state. */
3239 SMBtrans2
, /* cmd. */
3240 NULL
, /* pipe name. */
3244 &state
->setup
, /* setup. */
3245 1, /* num setup uint16_t words. */
3246 0, /* max returned setup. */
3247 state
->param
, /* param. */
3249 2, /* max returned param. */
3250 state
->data
, /* data. */
3251 POSIX_LOCK_DATA_SIZE
, /* num data. */
3252 0); /* max returned data. */
3254 if (tevent_req_nomem(subreq
, req
)) {
3255 return tevent_req_post(req
, ev
);
3257 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3261 /****************************************************************************
3263 ****************************************************************************/
3265 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3266 struct tevent_context
*ev
,
3267 struct cli_state
*cli
,
3272 enum brl_type lock_type
)
3274 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3275 wait_lock
, lock_type
);
3278 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3280 return tevent_req_simple_recv_ntstatus(req
);
3283 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3284 uint64_t offset
, uint64_t len
,
3285 bool wait_lock
, enum brl_type lock_type
)
3287 TALLOC_CTX
*frame
= talloc_stackframe();
3288 struct tevent_context
*ev
= NULL
;
3289 struct tevent_req
*req
= NULL
;
3290 NTSTATUS status
= NT_STATUS_OK
;
3292 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3294 * Can't use sync call while an async call is in flight
3296 status
= NT_STATUS_INVALID_PARAMETER
;
3300 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3301 status
= NT_STATUS_INVALID_PARAMETER
;
3305 ev
= samba_tevent_context_init(frame
);
3307 status
= NT_STATUS_NO_MEMORY
;
3311 req
= cli_posix_lock_send(frame
,
3320 status
= NT_STATUS_NO_MEMORY
;
3324 if (!tevent_req_poll(req
, ev
)) {
3325 status
= map_nt_error_from_unix(errno
);
3329 status
= cli_posix_lock_recv(req
);
3336 /****************************************************************************
3337 POSIX Unlock a file.
3338 ****************************************************************************/
3340 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3341 struct tevent_context
*ev
,
3342 struct cli_state
*cli
,
3347 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3348 false, UNLOCK_LOCK
);
3351 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3353 return tevent_req_simple_recv_ntstatus(req
);
3356 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3358 TALLOC_CTX
*frame
= talloc_stackframe();
3359 struct tevent_context
*ev
= NULL
;
3360 struct tevent_req
*req
= NULL
;
3361 NTSTATUS status
= NT_STATUS_OK
;
3363 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3365 * Can't use sync call while an async call is in flight
3367 status
= NT_STATUS_INVALID_PARAMETER
;
3371 ev
= samba_tevent_context_init(frame
);
3373 status
= NT_STATUS_NO_MEMORY
;
3377 req
= cli_posix_unlock_send(frame
,
3384 status
= NT_STATUS_NO_MEMORY
;
3388 if (!tevent_req_poll(req
, ev
)) {
3389 status
= map_nt_error_from_unix(errno
);
3393 status
= cli_posix_unlock_recv(req
);
3400 /****************************************************************************
3401 Do a SMBgetattrE call.
3402 ****************************************************************************/
3404 static void cli_getattrE_done(struct tevent_req
*subreq
);
3406 struct cli_getattrE_state
{
3416 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3417 struct tevent_context
*ev
,
3418 struct cli_state
*cli
,
3421 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3422 struct cli_getattrE_state
*state
= NULL
;
3423 uint8_t additional_flags
= 0;
3425 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3430 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3431 SSVAL(state
->vwv
+0,0,fnum
);
3433 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3434 1, state
->vwv
, 0, NULL
);
3435 if (tevent_req_nomem(subreq
, req
)) {
3436 return tevent_req_post(req
, ev
);
3438 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3442 static void cli_getattrE_done(struct tevent_req
*subreq
)
3444 struct tevent_req
*req
= tevent_req_callback_data(
3445 subreq
, struct tevent_req
);
3446 struct cli_getattrE_state
*state
= tevent_req_data(
3447 req
, struct cli_getattrE_state
);
3449 uint16_t *vwv
= NULL
;
3452 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3454 TALLOC_FREE(subreq
);
3455 if (tevent_req_nterror(req
, status
)) {
3459 state
->size
= (off_t
)IVAL(vwv
+6,0);
3460 state
->attr
= SVAL(vwv
+10,0);
3461 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3462 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3463 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3465 tevent_req_done(req
);
3468 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3471 time_t *change_time
,
3472 time_t *access_time
,
3475 struct cli_getattrE_state
*state
= tevent_req_data(
3476 req
, struct cli_getattrE_state
);
3479 if (tevent_req_is_nterror(req
, &status
)) {
3483 *attr
= state
->attr
;
3486 *size
= state
->size
;
3489 *change_time
= state
->change_time
;
3492 *access_time
= state
->access_time
;
3495 *write_time
= state
->write_time
;
3497 return NT_STATUS_OK
;
3500 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3504 time_t *change_time
,
3505 time_t *access_time
,
3508 TALLOC_CTX
*frame
= NULL
;
3509 struct tevent_context
*ev
= NULL
;
3510 struct tevent_req
*req
= NULL
;
3511 NTSTATUS status
= NT_STATUS_OK
;
3513 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3514 return cli_smb2_getattrE(cli
,
3523 frame
= talloc_stackframe();
3525 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3527 * Can't use sync call while an async call is in flight
3529 status
= NT_STATUS_INVALID_PARAMETER
;
3533 ev
= samba_tevent_context_init(frame
);
3535 status
= NT_STATUS_NO_MEMORY
;
3539 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3541 status
= NT_STATUS_NO_MEMORY
;
3545 if (!tevent_req_poll(req
, ev
)) {
3546 status
= map_nt_error_from_unix(errno
);
3550 status
= cli_getattrE_recv(req
,
3562 /****************************************************************************
3564 ****************************************************************************/
3566 static void cli_getatr_done(struct tevent_req
*subreq
);
3568 struct cli_getatr_state
{
3575 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3576 struct tevent_context
*ev
,
3577 struct cli_state
*cli
,
3580 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3581 struct cli_getatr_state
*state
= NULL
;
3582 uint8_t additional_flags
= 0;
3583 uint8_t *bytes
= NULL
;
3585 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3590 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3592 bytes
= talloc_array(state
, uint8_t, 1);
3593 if (tevent_req_nomem(bytes
, req
)) {
3594 return tevent_req_post(req
, ev
);
3597 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3598 strlen(fname
)+1, NULL
);
3600 if (tevent_req_nomem(bytes
, req
)) {
3601 return tevent_req_post(req
, ev
);
3604 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3605 0, NULL
, talloc_get_size(bytes
), bytes
);
3606 if (tevent_req_nomem(subreq
, req
)) {
3607 return tevent_req_post(req
, ev
);
3609 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3613 static void cli_getatr_done(struct tevent_req
*subreq
)
3615 struct tevent_req
*req
= tevent_req_callback_data(
3616 subreq
, struct tevent_req
);
3617 struct cli_getatr_state
*state
= tevent_req_data(
3618 req
, struct cli_getatr_state
);
3620 uint16_t *vwv
= NULL
;
3623 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3625 TALLOC_FREE(subreq
);
3626 if (tevent_req_nterror(req
, status
)) {
3630 state
->attr
= SVAL(vwv
+0,0);
3631 state
->size
= (off_t
)IVAL(vwv
+3,0);
3632 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3634 tevent_req_done(req
);
3637 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3642 struct cli_getatr_state
*state
= tevent_req_data(
3643 req
, struct cli_getatr_state
);
3646 if (tevent_req_is_nterror(req
, &status
)) {
3650 *attr
= state
->attr
;
3653 *size
= state
->size
;
3656 *write_time
= state
->write_time
;
3658 return NT_STATUS_OK
;
3661 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3667 TALLOC_CTX
*frame
= NULL
;
3668 struct tevent_context
*ev
= NULL
;
3669 struct tevent_req
*req
= NULL
;
3670 NTSTATUS status
= NT_STATUS_OK
;
3672 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3673 return cli_smb2_getatr(cli
,
3680 frame
= talloc_stackframe();
3682 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3684 * Can't use sync call while an async call is in flight
3686 status
= NT_STATUS_INVALID_PARAMETER
;
3690 ev
= samba_tevent_context_init(frame
);
3692 status
= NT_STATUS_NO_MEMORY
;
3696 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3698 status
= NT_STATUS_NO_MEMORY
;
3702 if (!tevent_req_poll(req
, ev
)) {
3703 status
= map_nt_error_from_unix(errno
);
3707 status
= cli_getatr_recv(req
,
3717 /****************************************************************************
3718 Do a SMBsetattrE call.
3719 ****************************************************************************/
3721 static void cli_setattrE_done(struct tevent_req
*subreq
);
3723 struct cli_setattrE_state
{
3727 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3728 struct tevent_context
*ev
,
3729 struct cli_state
*cli
,
3735 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3736 struct cli_setattrE_state
*state
= NULL
;
3737 uint8_t additional_flags
= 0;
3739 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3744 SSVAL(state
->vwv
+0, 0, fnum
);
3745 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3746 smb1cli_conn_server_time_zone(cli
->conn
));
3747 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3748 smb1cli_conn_server_time_zone(cli
->conn
));
3749 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3750 smb1cli_conn_server_time_zone(cli
->conn
));
3752 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3753 7, state
->vwv
, 0, NULL
);
3754 if (tevent_req_nomem(subreq
, req
)) {
3755 return tevent_req_post(req
, ev
);
3757 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3761 static void cli_setattrE_done(struct tevent_req
*subreq
)
3763 struct tevent_req
*req
= tevent_req_callback_data(
3764 subreq
, struct tevent_req
);
3767 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3768 TALLOC_FREE(subreq
);
3769 if (tevent_req_nterror(req
, status
)) {
3772 tevent_req_done(req
);
3775 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3777 return tevent_req_simple_recv_ntstatus(req
);
3780 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3786 TALLOC_CTX
*frame
= NULL
;
3787 struct tevent_context
*ev
= NULL
;
3788 struct tevent_req
*req
= NULL
;
3789 NTSTATUS status
= NT_STATUS_OK
;
3791 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3792 return cli_smb2_setattrE(cli
,
3799 frame
= talloc_stackframe();
3801 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3803 * Can't use sync call while an async call is in flight
3805 status
= NT_STATUS_INVALID_PARAMETER
;
3809 ev
= samba_tevent_context_init(frame
);
3811 status
= NT_STATUS_NO_MEMORY
;
3815 req
= cli_setattrE_send(frame
, ev
,
3823 status
= NT_STATUS_NO_MEMORY
;
3827 if (!tevent_req_poll(req
, ev
)) {
3828 status
= map_nt_error_from_unix(errno
);
3832 status
= cli_setattrE_recv(req
);
3839 /****************************************************************************
3840 Do a SMBsetatr call.
3841 ****************************************************************************/
3843 static void cli_setatr_done(struct tevent_req
*subreq
);
3845 struct cli_setatr_state
{
3849 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3850 struct tevent_context
*ev
,
3851 struct cli_state
*cli
,
3856 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3857 struct cli_setatr_state
*state
= NULL
;
3858 uint8_t additional_flags
= 0;
3859 uint8_t *bytes
= NULL
;
3861 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3866 SSVAL(state
->vwv
+0, 0, attr
);
3867 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3869 bytes
= talloc_array(state
, uint8_t, 1);
3870 if (tevent_req_nomem(bytes
, req
)) {
3871 return tevent_req_post(req
, ev
);
3874 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3875 strlen(fname
)+1, NULL
);
3876 if (tevent_req_nomem(bytes
, req
)) {
3877 return tevent_req_post(req
, ev
);
3879 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3880 talloc_get_size(bytes
)+1);
3881 if (tevent_req_nomem(bytes
, req
)) {
3882 return tevent_req_post(req
, ev
);
3885 bytes
[talloc_get_size(bytes
)-1] = 4;
3886 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3888 if (tevent_req_nomem(bytes
, req
)) {
3889 return tevent_req_post(req
, ev
);
3892 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3893 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3894 if (tevent_req_nomem(subreq
, req
)) {
3895 return tevent_req_post(req
, ev
);
3897 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3901 static void cli_setatr_done(struct tevent_req
*subreq
)
3903 struct tevent_req
*req
= tevent_req_callback_data(
3904 subreq
, struct tevent_req
);
3907 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3908 TALLOC_FREE(subreq
);
3909 if (tevent_req_nterror(req
, status
)) {
3912 tevent_req_done(req
);
3915 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3917 return tevent_req_simple_recv_ntstatus(req
);
3920 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3925 TALLOC_CTX
*frame
= NULL
;
3926 struct tevent_context
*ev
= NULL
;
3927 struct tevent_req
*req
= NULL
;
3928 NTSTATUS status
= NT_STATUS_OK
;
3930 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3931 return cli_smb2_setatr(cli
,
3937 frame
= talloc_stackframe();
3939 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3941 * Can't use sync call while an async call is in flight
3943 status
= NT_STATUS_INVALID_PARAMETER
;
3947 ev
= samba_tevent_context_init(frame
);
3949 status
= NT_STATUS_NO_MEMORY
;
3953 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3955 status
= NT_STATUS_NO_MEMORY
;
3959 if (!tevent_req_poll(req
, ev
)) {
3960 status
= map_nt_error_from_unix(errno
);
3964 status
= cli_setatr_recv(req
);
3971 /****************************************************************************
3972 Check for existance of a dir.
3973 ****************************************************************************/
3975 static void cli_chkpath_done(struct tevent_req
*subreq
);
3977 struct cli_chkpath_state
{
3981 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3982 struct tevent_context
*ev
,
3983 struct cli_state
*cli
,
3986 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3987 struct cli_chkpath_state
*state
= NULL
;
3988 uint8_t additional_flags
= 0;
3989 uint8_t *bytes
= NULL
;
3991 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3996 bytes
= talloc_array(state
, uint8_t, 1);
3997 if (tevent_req_nomem(bytes
, req
)) {
3998 return tevent_req_post(req
, ev
);
4001 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4002 strlen(fname
)+1, NULL
);
4004 if (tevent_req_nomem(bytes
, req
)) {
4005 return tevent_req_post(req
, ev
);
4008 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
4009 0, NULL
, talloc_get_size(bytes
), bytes
);
4010 if (tevent_req_nomem(subreq
, req
)) {
4011 return tevent_req_post(req
, ev
);
4013 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
4017 static void cli_chkpath_done(struct tevent_req
*subreq
)
4019 struct tevent_req
*req
= tevent_req_callback_data(
4020 subreq
, struct tevent_req
);
4023 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4024 TALLOC_FREE(subreq
);
4025 if (tevent_req_nterror(req
, status
)) {
4028 tevent_req_done(req
);
4031 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
4033 return tevent_req_simple_recv_ntstatus(req
);
4036 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
4038 TALLOC_CTX
*frame
= talloc_stackframe();
4039 struct tevent_context
*ev
= NULL
;
4040 struct tevent_req
*req
= NULL
;
4042 NTSTATUS status
= NT_STATUS_OK
;
4044 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4046 * Can't use sync call while an async call is in flight
4048 status
= NT_STATUS_INVALID_PARAMETER
;
4052 path2
= talloc_strdup(frame
, path
);
4054 status
= NT_STATUS_NO_MEMORY
;
4057 trim_char(path2
,'\0','\\');
4059 path2
= talloc_strdup(frame
, "\\");
4061 status
= NT_STATUS_NO_MEMORY
;
4066 ev
= samba_tevent_context_init(frame
);
4068 status
= NT_STATUS_NO_MEMORY
;
4072 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4074 status
= NT_STATUS_NO_MEMORY
;
4078 if (!tevent_req_poll(req
, ev
)) {
4079 status
= map_nt_error_from_unix(errno
);
4083 status
= cli_chkpath_recv(req
);
4090 /****************************************************************************
4092 ****************************************************************************/
4094 static void cli_dskattr_done(struct tevent_req
*subreq
);
4096 struct cli_dskattr_state
{
4102 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4103 struct tevent_context
*ev
,
4104 struct cli_state
*cli
)
4106 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4107 struct cli_dskattr_state
*state
= NULL
;
4108 uint8_t additional_flags
= 0;
4110 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4115 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
4117 if (tevent_req_nomem(subreq
, req
)) {
4118 return tevent_req_post(req
, ev
);
4120 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4124 static void cli_dskattr_done(struct tevent_req
*subreq
)
4126 struct tevent_req
*req
= tevent_req_callback_data(
4127 subreq
, struct tevent_req
);
4128 struct cli_dskattr_state
*state
= tevent_req_data(
4129 req
, struct cli_dskattr_state
);
4131 uint16_t *vwv
= NULL
;
4134 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4136 TALLOC_FREE(subreq
);
4137 if (tevent_req_nterror(req
, status
)) {
4140 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4141 state
->total
= SVAL(vwv
+0, 0);
4142 state
->avail
= SVAL(vwv
+3, 0);
4143 tevent_req_done(req
);
4146 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4148 struct cli_dskattr_state
*state
= tevent_req_data(
4149 req
, struct cli_dskattr_state
);
4152 if (tevent_req_is_nterror(req
, &status
)) {
4155 *bsize
= state
->bsize
;
4156 *total
= state
->total
;
4157 *avail
= state
->avail
;
4158 return NT_STATUS_OK
;
4161 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4163 TALLOC_CTX
*frame
= NULL
;
4164 struct tevent_context
*ev
= NULL
;
4165 struct tevent_req
*req
= NULL
;
4166 NTSTATUS status
= NT_STATUS_OK
;
4168 frame
= talloc_stackframe();
4170 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4172 * Can't use sync call while an async call is in flight
4174 status
= NT_STATUS_INVALID_PARAMETER
;
4178 ev
= samba_tevent_context_init(frame
);
4180 status
= NT_STATUS_NO_MEMORY
;
4184 req
= cli_dskattr_send(frame
, ev
, cli
);
4186 status
= NT_STATUS_NO_MEMORY
;
4190 if (!tevent_req_poll(req
, ev
)) {
4191 status
= map_nt_error_from_unix(errno
);
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(req
, ev
)) {
4415 status
= map_nt_error_from_unix(errno
);
4419 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4427 send a raw ioctl - used by the torture code
4429 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4434 SSVAL(vwv
+0, 0, fnum
);
4435 SSVAL(vwv
+1, 0, code
>>16);
4436 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4438 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4439 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4440 if (!NT_STATUS_IS_OK(status
)) {
4443 *blob
= data_blob_null
;
4444 return NT_STATUS_OK
;
4447 /*********************************************************
4448 Set an extended attribute utility fn.
4449 *********************************************************/
4451 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4452 uint8_t *param
, unsigned int param_len
,
4453 const char *ea_name
,
4454 const char *ea_val
, size_t ea_len
)
4457 unsigned int data_len
= 0;
4458 uint8_t *data
= NULL
;
4460 size_t ea_namelen
= strlen(ea_name
);
4463 SSVAL(setup
, 0, setup_val
);
4465 if (ea_namelen
== 0 && ea_len
== 0) {
4467 data
= talloc_array(talloc_tos(),
4471 return NT_STATUS_NO_MEMORY
;
4474 SIVAL(p
,0,data_len
);
4476 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4477 data
= talloc_array(talloc_tos(),
4481 return NT_STATUS_NO_MEMORY
;
4484 SIVAL(p
,0,data_len
);
4486 SCVAL(p
, 0, 0); /* EA flags. */
4487 SCVAL(p
, 1, ea_namelen
);
4488 SSVAL(p
, 2, ea_len
);
4489 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4490 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4493 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4495 param
, param_len
, 2,
4496 data
, data_len
, CLI_BUFFER_SIZE
,
4498 NULL
, 0, NULL
, /* rsetup */
4499 NULL
, 0, NULL
, /* rparam */
4500 NULL
, 0, NULL
); /* rdata */
4505 /*********************************************************
4506 Set an extended attribute on a pathname.
4507 *********************************************************/
4509 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4510 const char *ea_name
, const char *ea_val
,
4513 unsigned int param_len
= 0;
4516 TALLOC_CTX
*frame
= NULL
;
4518 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4519 return cli_smb2_set_ea_path(cli
,
4526 frame
= talloc_stackframe();
4528 param
= talloc_array(frame
, uint8_t, 6);
4530 status
= NT_STATUS_NO_MEMORY
;
4533 SSVAL(param
,0,SMB_INFO_SET_EA
);
4537 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4538 path
, strlen(path
)+1,
4540 param_len
= talloc_get_size(param
);
4542 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4543 ea_name
, ea_val
, ea_len
);
4551 /*********************************************************
4552 Set an extended attribute on an fnum.
4553 *********************************************************/
4555 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4556 const char *ea_name
, const char *ea_val
,
4561 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4562 return cli_smb2_set_ea_fnum(cli
,
4569 memset(param
, 0, 6);
4570 SSVAL(param
,0,fnum
);
4571 SSVAL(param
,2,SMB_INFO_SET_EA
);
4573 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4574 ea_name
, ea_val
, ea_len
);
4577 /*********************************************************
4578 Get an extended attribute list utility fn.
4579 *********************************************************/
4581 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4583 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4585 struct ea_struct
*ea_list
= NULL
;
4590 if (rdata_len
< 4) {
4594 ea_size
= (size_t)IVAL(rdata
,0);
4595 if (ea_size
> rdata_len
) {
4600 /* No EA's present. */
4609 /* Validate the EA list and count it. */
4610 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4611 unsigned int ea_namelen
= CVAL(p
,1);
4612 unsigned int ea_valuelen
= SVAL(p
,2);
4613 if (ea_namelen
== 0) {
4616 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4619 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4620 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4629 *pnum_eas
= num_eas
;
4631 /* Caller only wants number of EA's. */
4635 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4640 ea_size
= (size_t)IVAL(rdata
,0);
4643 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4644 struct ea_struct
*ea
= &ea_list
[num_eas
];
4645 fstring unix_ea_name
;
4646 unsigned int ea_namelen
= CVAL(p
,1);
4647 unsigned int ea_valuelen
= SVAL(p
,2);
4649 ea
->flags
= CVAL(p
,0);
4650 unix_ea_name
[0] = '\0';
4651 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4652 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4656 /* Ensure the value is null terminated (in case it's a string). */
4657 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4658 if (!ea
->value
.data
) {
4662 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4664 ea
->value
.data
[ea_valuelen
] = 0;
4666 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4669 *pea_list
= ea_list
;
4673 TALLOC_FREE(ea_list
);
4677 /*********************************************************
4678 Get an extended attribute list from a pathname.
4679 *********************************************************/
4681 struct cli_get_ea_list_path_state
{
4686 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4688 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4689 struct tevent_context
*ev
,
4690 struct cli_state
*cli
,
4693 struct tevent_req
*req
, *subreq
;
4694 struct cli_get_ea_list_path_state
*state
;
4696 req
= tevent_req_create(mem_ctx
, &state
,
4697 struct cli_get_ea_list_path_state
);
4701 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4702 SMB_INFO_QUERY_ALL_EAS
, 4,
4704 if (tevent_req_nomem(subreq
, req
)) {
4705 return tevent_req_post(req
, ev
);
4707 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4711 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4713 struct tevent_req
*req
= tevent_req_callback_data(
4714 subreq
, struct tevent_req
);
4715 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4716 req
, struct cli_get_ea_list_path_state
);
4719 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4721 TALLOC_FREE(subreq
);
4722 if (tevent_req_nterror(req
, status
)) {
4725 tevent_req_done(req
);
4728 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4729 size_t *pnum_eas
, struct ea_struct
**peas
)
4731 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4732 req
, struct cli_get_ea_list_path_state
);
4735 if (tevent_req_is_nterror(req
, &status
)) {
4738 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4740 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4742 return NT_STATUS_OK
;
4745 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4748 struct ea_struct
**pea_list
)
4750 TALLOC_CTX
*frame
= NULL
;
4751 struct tevent_context
*ev
= NULL
;
4752 struct tevent_req
*req
= NULL
;
4753 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4755 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4756 return cli_smb2_get_ea_list_path(cli
,
4763 frame
= talloc_stackframe();
4765 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4767 * Can't use sync call while an async call is in flight
4769 status
= NT_STATUS_INVALID_PARAMETER
;
4772 ev
= samba_tevent_context_init(frame
);
4776 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4780 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4783 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4789 /****************************************************************************
4790 Convert open "flags" arg to uint32_t on wire.
4791 ****************************************************************************/
4793 static uint32_t open_flags_to_wire(int flags
)
4795 int open_mode
= flags
& O_ACCMODE
;
4798 switch (open_mode
) {
4800 ret
|= SMB_O_WRONLY
;
4807 ret
|= SMB_O_RDONLY
;
4811 if (flags
& O_CREAT
) {
4814 if (flags
& O_EXCL
) {
4817 if (flags
& O_TRUNC
) {
4821 if (flags
& O_SYNC
) {
4825 if (flags
& O_APPEND
) {
4826 ret
|= SMB_O_APPEND
;
4828 #if defined(O_DIRECT)
4829 if (flags
& O_DIRECT
) {
4830 ret
|= SMB_O_DIRECT
;
4833 #if defined(O_DIRECTORY)
4834 if (flags
& O_DIRECTORY
) {
4835 ret
|= SMB_O_DIRECTORY
;
4841 /****************************************************************************
4842 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4843 ****************************************************************************/
4845 struct posix_open_state
{
4849 uint16_t fnum
; /* Out */
4852 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4854 struct tevent_req
*req
= tevent_req_callback_data(
4855 subreq
, struct tevent_req
);
4856 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4861 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4862 NULL
, 0, NULL
, &data
, 12, &num_data
);
4863 TALLOC_FREE(subreq
);
4864 if (tevent_req_nterror(req
, status
)) {
4867 state
->fnum
= SVAL(data
,2);
4868 tevent_req_done(req
);
4871 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4872 struct tevent_context
*ev
,
4873 struct cli_state
*cli
,
4879 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4880 struct posix_open_state
*state
= NULL
;
4881 uint32_t wire_flags
= open_flags_to_wire(flags
);
4883 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4888 /* Setup setup word. */
4889 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4891 /* Setup param array. */
4892 state
->param
= talloc_array(state
, uint8_t, 6);
4893 if (tevent_req_nomem(state
->param
, req
)) {
4894 return tevent_req_post(req
, ev
);
4896 memset(state
->param
, '\0', 6);
4897 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4899 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4900 strlen(fname
)+1, NULL
);
4902 if (tevent_req_nomem(state
->param
, req
)) {
4903 return tevent_req_post(req
, ev
);
4906 /* Setup data words. */
4908 wire_flags
|= SMB_O_DIRECTORY
;
4911 SIVAL(state
->data
,0,0); /* No oplock. */
4912 SIVAL(state
->data
,4,wire_flags
);
4913 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4914 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4915 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4917 subreq
= cli_trans_send(state
, /* mem ctx. */
4918 ev
, /* event ctx. */
4919 cli
, /* cli_state. */
4920 SMBtrans2
, /* cmd. */
4921 NULL
, /* pipe name. */
4925 &state
->setup
, /* setup. */
4926 1, /* num setup uint16_t words. */
4927 0, /* max returned setup. */
4928 state
->param
, /* param. */
4929 talloc_get_size(state
->param
),/* num param. */
4930 2, /* max returned param. */
4931 state
->data
, /* data. */
4933 12); /* max returned data. */
4935 if (tevent_req_nomem(subreq
, req
)) {
4936 return tevent_req_post(req
, ev
);
4938 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4942 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4943 struct tevent_context
*ev
,
4944 struct cli_state
*cli
,
4949 return cli_posix_open_internal_send(mem_ctx
, ev
,
4950 cli
, fname
, flags
, mode
, false);
4953 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4955 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4958 if (tevent_req_is_nterror(req
, &status
)) {
4961 *pfnum
= state
->fnum
;
4962 return NT_STATUS_OK
;
4965 /****************************************************************************
4966 Open - POSIX semantics. Doesn't request oplock.
4967 ****************************************************************************/
4969 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4970 int flags
, mode_t mode
, uint16_t *pfnum
)
4973 TALLOC_CTX
*frame
= talloc_stackframe();
4974 struct tevent_context
*ev
= NULL
;
4975 struct tevent_req
*req
= NULL
;
4976 NTSTATUS status
= NT_STATUS_OK
;
4978 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4980 * Can't use sync call while an async call is in flight
4982 status
= NT_STATUS_INVALID_PARAMETER
;
4986 ev
= samba_tevent_context_init(frame
);
4988 status
= NT_STATUS_NO_MEMORY
;
4992 req
= cli_posix_open_send(frame
,
4999 status
= NT_STATUS_NO_MEMORY
;
5003 if (!tevent_req_poll(req
, ev
)) {
5004 status
= map_nt_error_from_unix(errno
);
5008 status
= cli_posix_open_recv(req
, pfnum
);
5015 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5016 struct tevent_context
*ev
,
5017 struct cli_state
*cli
,
5021 return cli_posix_open_internal_send(mem_ctx
, ev
,
5022 cli
, fname
, O_CREAT
, mode
, true);
5025 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5027 return tevent_req_simple_recv_ntstatus(req
);
5030 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5032 TALLOC_CTX
*frame
= talloc_stackframe();
5033 struct tevent_context
*ev
= NULL
;
5034 struct tevent_req
*req
= NULL
;
5035 NTSTATUS status
= NT_STATUS_OK
;
5037 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5039 * Can't use sync call while an async call is in flight
5041 status
= NT_STATUS_INVALID_PARAMETER
;
5045 ev
= samba_tevent_context_init(frame
);
5047 status
= NT_STATUS_NO_MEMORY
;
5051 req
= cli_posix_mkdir_send(frame
,
5057 status
= NT_STATUS_NO_MEMORY
;
5061 if (!tevent_req_poll(req
, ev
)) {
5062 status
= map_nt_error_from_unix(errno
);
5066 status
= cli_posix_mkdir_recv(req
);
5073 /****************************************************************************
5074 unlink or rmdir - POSIX semantics.
5075 ****************************************************************************/
5077 struct cli_posix_unlink_internal_state
{
5081 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5083 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5084 struct tevent_context
*ev
,
5085 struct cli_state
*cli
,
5089 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5090 struct cli_posix_unlink_internal_state
*state
= NULL
;
5092 req
= tevent_req_create(mem_ctx
, &state
,
5093 struct cli_posix_unlink_internal_state
);
5098 /* Setup data word. */
5099 SSVAL(state
->data
, 0, level
);
5101 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5102 SMB_POSIX_PATH_UNLINK
,
5104 state
->data
, sizeof(state
->data
));
5105 if (tevent_req_nomem(subreq
, req
)) {
5106 return tevent_req_post(req
, ev
);
5108 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5112 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5114 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5115 tevent_req_simple_finish_ntstatus(subreq
, status
);
5118 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5119 struct tevent_context
*ev
,
5120 struct cli_state
*cli
,
5123 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5124 SMB_POSIX_UNLINK_FILE_TARGET
);
5127 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5129 return tevent_req_simple_recv_ntstatus(req
);
5132 /****************************************************************************
5133 unlink - POSIX semantics.
5134 ****************************************************************************/
5136 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5138 TALLOC_CTX
*frame
= talloc_stackframe();
5139 struct tevent_context
*ev
= NULL
;
5140 struct tevent_req
*req
= NULL
;
5141 NTSTATUS status
= NT_STATUS_OK
;
5143 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5145 * Can't use sync call while an async call is in flight
5147 status
= NT_STATUS_INVALID_PARAMETER
;
5151 ev
= samba_tevent_context_init(frame
);
5153 status
= NT_STATUS_NO_MEMORY
;
5157 req
= cli_posix_unlink_send(frame
,
5162 status
= NT_STATUS_NO_MEMORY
;
5166 if (!tevent_req_poll(req
, ev
)) {
5167 status
= map_nt_error_from_unix(errno
);
5171 status
= cli_posix_unlink_recv(req
);
5178 /****************************************************************************
5179 rmdir - POSIX semantics.
5180 ****************************************************************************/
5182 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5183 struct tevent_context
*ev
,
5184 struct cli_state
*cli
,
5187 return cli_posix_unlink_internal_send(
5188 mem_ctx
, ev
, cli
, fname
,
5189 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5192 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5194 return tevent_req_simple_recv_ntstatus(req
);
5197 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5199 TALLOC_CTX
*frame
= talloc_stackframe();
5200 struct tevent_context
*ev
= NULL
;
5201 struct tevent_req
*req
= NULL
;
5202 NTSTATUS status
= NT_STATUS_OK
;
5204 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5206 * Can't use sync call while an async call is in flight
5208 status
= NT_STATUS_INVALID_PARAMETER
;
5212 ev
= samba_tevent_context_init(frame
);
5214 status
= NT_STATUS_NO_MEMORY
;
5218 req
= cli_posix_rmdir_send(frame
,
5223 status
= NT_STATUS_NO_MEMORY
;
5227 if (!tevent_req_poll(req
, ev
)) {
5228 status
= map_nt_error_from_unix(errno
);
5232 status
= cli_posix_rmdir_recv(req
, frame
);
5239 /****************************************************************************
5241 ****************************************************************************/
5243 struct cli_notify_state
{
5245 uint32_t num_changes
;
5246 struct notify_change
*changes
;
5249 static void cli_notify_done(struct tevent_req
*subreq
);
5251 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5252 struct tevent_context
*ev
,
5253 struct cli_state
*cli
, uint16_t fnum
,
5254 uint32_t buffer_size
,
5255 uint32_t completion_filter
, bool recursive
)
5257 struct tevent_req
*req
, *subreq
;
5258 struct cli_notify_state
*state
;
5259 unsigned old_timeout
;
5261 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5266 SIVAL(state
->setup
, 0, completion_filter
);
5267 SSVAL(state
->setup
, 4, fnum
);
5268 SSVAL(state
->setup
, 6, recursive
);
5271 * Notifies should not time out
5273 old_timeout
= cli_set_timeout(cli
, 0);
5275 subreq
= cli_trans_send(
5276 state
, /* mem ctx. */
5277 ev
, /* event ctx. */
5278 cli
, /* cli_state. */
5279 SMBnttrans
, /* cmd. */
5280 NULL
, /* pipe name. */
5282 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5284 (uint16_t *)state
->setup
, /* setup. */
5285 4, /* num setup uint16_t words. */
5286 0, /* max returned setup. */
5289 buffer_size
, /* max returned param. */
5292 0); /* max returned data. */
5294 cli_set_timeout(cli
, old_timeout
);
5296 if (tevent_req_nomem(subreq
, req
)) {
5297 return tevent_req_post(req
, ev
);
5299 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5303 static void cli_notify_done(struct tevent_req
*subreq
)
5305 struct tevent_req
*req
= tevent_req_callback_data(
5306 subreq
, struct tevent_req
);
5307 struct cli_notify_state
*state
= tevent_req_data(
5308 req
, struct cli_notify_state
);
5311 uint32_t i
, ofs
, num_params
;
5314 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5315 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5316 TALLOC_FREE(subreq
);
5317 if (tevent_req_nterror(req
, status
)) {
5318 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5322 state
->num_changes
= 0;
5325 while (num_params
- ofs
> 12) {
5326 uint32_t next
= IVAL(params
, ofs
);
5327 state
->num_changes
+= 1;
5329 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5335 state
->changes
= talloc_array(state
, struct notify_change
,
5336 state
->num_changes
);
5337 if (tevent_req_nomem(state
->changes
, req
)) {
5338 TALLOC_FREE(params
);
5344 for (i
=0; i
<state
->num_changes
; i
++) {
5345 uint32_t next
= IVAL(params
, ofs
);
5346 uint32_t len
= IVAL(params
, ofs
+8);
5350 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5351 TALLOC_FREE(params
);
5353 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5357 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5358 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5359 &name
, params
+ofs
+12, len
,
5360 STR_TERMINATE
|STR_UNICODE
);
5362 TALLOC_FREE(params
);
5363 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5366 state
->changes
[i
].name
= name
;
5370 TALLOC_FREE(params
);
5371 tevent_req_done(req
);
5374 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5375 uint32_t *pnum_changes
,
5376 struct notify_change
**pchanges
)
5378 struct cli_notify_state
*state
= tevent_req_data(
5379 req
, struct cli_notify_state
);
5382 if (tevent_req_is_nterror(req
, &status
)) {
5386 *pnum_changes
= state
->num_changes
;
5387 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5388 return NT_STATUS_OK
;
5391 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5392 uint32_t completion_filter
, bool recursive
,
5393 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5394 struct notify_change
**pchanges
)
5396 TALLOC_CTX
*frame
= talloc_stackframe();
5397 struct tevent_context
*ev
;
5398 struct tevent_req
*req
;
5399 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5401 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5403 * Can't use sync call while an async call is in flight
5405 status
= NT_STATUS_INVALID_PARAMETER
;
5408 ev
= samba_tevent_context_init(frame
);
5412 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5413 completion_filter
, recursive
);
5417 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5420 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5426 struct cli_qpathinfo_state
{
5435 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5437 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5438 struct tevent_context
*ev
,
5439 struct cli_state
*cli
, const char *fname
,
5440 uint16_t level
, uint32_t min_rdata
,
5443 struct tevent_req
*req
, *subreq
;
5444 struct cli_qpathinfo_state
*state
;
5446 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5450 state
->min_rdata
= min_rdata
;
5451 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5453 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5454 if (tevent_req_nomem(state
->param
, req
)) {
5455 return tevent_req_post(req
, ev
);
5457 SSVAL(state
->param
, 0, level
);
5458 state
->param
= trans2_bytes_push_str(
5459 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5460 if (tevent_req_nomem(state
->param
, req
)) {
5461 return tevent_req_post(req
, ev
);
5464 subreq
= cli_trans_send(
5465 state
, /* mem ctx. */
5466 ev
, /* event ctx. */
5467 cli
, /* cli_state. */
5468 SMBtrans2
, /* cmd. */
5469 NULL
, /* pipe name. */
5473 state
->setup
, /* setup. */
5474 1, /* num setup uint16_t words. */
5475 0, /* max returned setup. */
5476 state
->param
, /* param. */
5477 talloc_get_size(state
->param
), /* num param. */
5478 2, /* max returned param. */
5481 max_rdata
); /* max returned data. */
5483 if (tevent_req_nomem(subreq
, req
)) {
5484 return tevent_req_post(req
, ev
);
5486 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5490 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5492 struct tevent_req
*req
= tevent_req_callback_data(
5493 subreq
, struct tevent_req
);
5494 struct cli_qpathinfo_state
*state
= tevent_req_data(
5495 req
, struct cli_qpathinfo_state
);
5498 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5500 &state
->rdata
, state
->min_rdata
,
5502 if (tevent_req_nterror(req
, status
)) {
5505 tevent_req_done(req
);
5508 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5509 uint8_t **rdata
, uint32_t *num_rdata
)
5511 struct cli_qpathinfo_state
*state
= tevent_req_data(
5512 req
, struct cli_qpathinfo_state
);
5515 if (tevent_req_is_nterror(req
, &status
)) {
5518 if (rdata
!= NULL
) {
5519 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5521 TALLOC_FREE(state
->rdata
);
5523 if (num_rdata
!= NULL
) {
5524 *num_rdata
= state
->num_rdata
;
5526 return NT_STATUS_OK
;
5529 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5530 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5532 uint8_t **rdata
, uint32_t *num_rdata
)
5534 TALLOC_CTX
*frame
= talloc_stackframe();
5535 struct tevent_context
*ev
;
5536 struct tevent_req
*req
;
5537 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5539 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5541 * Can't use sync call while an async call is in flight
5543 status
= NT_STATUS_INVALID_PARAMETER
;
5546 ev
= samba_tevent_context_init(frame
);
5550 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5555 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5558 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5564 struct cli_qfileinfo_state
{
5568 uint16_t recv_flags2
;
5574 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5576 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5577 struct tevent_context
*ev
,
5578 struct cli_state
*cli
, uint16_t fnum
,
5579 uint16_t level
, uint32_t min_rdata
,
5582 struct tevent_req
*req
, *subreq
;
5583 struct cli_qfileinfo_state
*state
;
5585 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5589 state
->min_rdata
= min_rdata
;
5590 SSVAL(state
->param
, 0, fnum
);
5591 SSVAL(state
->param
, 2, level
);
5592 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5594 subreq
= cli_trans_send(
5595 state
, /* mem ctx. */
5596 ev
, /* event ctx. */
5597 cli
, /* cli_state. */
5598 SMBtrans2
, /* cmd. */
5599 NULL
, /* pipe name. */
5603 state
->setup
, /* setup. */
5604 1, /* num setup uint16_t words. */
5605 0, /* max returned setup. */
5606 state
->param
, /* param. */
5607 sizeof(state
->param
), /* num param. */
5608 2, /* max returned param. */
5611 max_rdata
); /* max returned data. */
5613 if (tevent_req_nomem(subreq
, req
)) {
5614 return tevent_req_post(req
, ev
);
5616 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5620 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5622 struct tevent_req
*req
= tevent_req_callback_data(
5623 subreq
, struct tevent_req
);
5624 struct cli_qfileinfo_state
*state
= tevent_req_data(
5625 req
, struct cli_qfileinfo_state
);
5628 status
= cli_trans_recv(subreq
, state
,
5629 &state
->recv_flags2
,
5632 &state
->rdata
, state
->min_rdata
,
5634 if (tevent_req_nterror(req
, status
)) {
5637 tevent_req_done(req
);
5640 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5641 uint16_t *recv_flags2
,
5642 uint8_t **rdata
, uint32_t *num_rdata
)
5644 struct cli_qfileinfo_state
*state
= tevent_req_data(
5645 req
, struct cli_qfileinfo_state
);
5648 if (tevent_req_is_nterror(req
, &status
)) {
5652 if (recv_flags2
!= NULL
) {
5653 *recv_flags2
= state
->recv_flags2
;
5655 if (rdata
!= NULL
) {
5656 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5658 TALLOC_FREE(state
->rdata
);
5660 if (num_rdata
!= NULL
) {
5661 *num_rdata
= state
->num_rdata
;
5663 return NT_STATUS_OK
;
5666 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5667 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5668 uint32_t max_rdata
, uint16_t *recv_flags2
,
5669 uint8_t **rdata
, uint32_t *num_rdata
)
5671 TALLOC_CTX
*frame
= talloc_stackframe();
5672 struct tevent_context
*ev
;
5673 struct tevent_req
*req
;
5674 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5676 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5678 * Can't use sync call while an async call is in flight
5680 status
= NT_STATUS_INVALID_PARAMETER
;
5683 ev
= samba_tevent_context_init(frame
);
5687 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5692 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5695 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5701 struct cli_flush_state
{
5705 static void cli_flush_done(struct tevent_req
*subreq
);
5707 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5708 struct tevent_context
*ev
,
5709 struct cli_state
*cli
,
5712 struct tevent_req
*req
, *subreq
;
5713 struct cli_flush_state
*state
;
5715 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5719 SSVAL(state
->vwv
+ 0, 0, fnum
);
5721 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5723 if (tevent_req_nomem(subreq
, req
)) {
5724 return tevent_req_post(req
, ev
);
5726 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5730 static void cli_flush_done(struct tevent_req
*subreq
)
5732 struct tevent_req
*req
= tevent_req_callback_data(
5733 subreq
, struct tevent_req
);
5736 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5737 TALLOC_FREE(subreq
);
5738 if (tevent_req_nterror(req
, status
)) {
5741 tevent_req_done(req
);
5744 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5746 return tevent_req_simple_recv_ntstatus(req
);
5749 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5751 TALLOC_CTX
*frame
= talloc_stackframe();
5752 struct tevent_context
*ev
;
5753 struct tevent_req
*req
;
5754 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5756 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5758 * Can't use sync call while an async call is in flight
5760 status
= NT_STATUS_INVALID_PARAMETER
;
5763 ev
= samba_tevent_context_init(frame
);
5767 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5771 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5774 status
= cli_flush_recv(req
);
5780 struct cli_shadow_copy_data_state
{
5787 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5789 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5790 struct tevent_context
*ev
,
5791 struct cli_state
*cli
,
5795 struct tevent_req
*req
, *subreq
;
5796 struct cli_shadow_copy_data_state
*state
;
5799 req
= tevent_req_create(mem_ctx
, &state
,
5800 struct cli_shadow_copy_data_state
);
5804 state
->get_names
= get_names
;
5805 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5807 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5808 SSVAL(state
->setup
+ 2, 0, fnum
);
5809 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5810 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5812 subreq
= cli_trans_send(
5813 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5814 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5817 if (tevent_req_nomem(subreq
, req
)) {
5818 return tevent_req_post(req
, ev
);
5820 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5824 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5826 struct tevent_req
*req
= tevent_req_callback_data(
5827 subreq
, struct tevent_req
);
5828 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5829 req
, struct cli_shadow_copy_data_state
);
5832 status
= cli_trans_recv(subreq
, state
, NULL
,
5833 NULL
, 0, NULL
, /* setup */
5834 NULL
, 0, NULL
, /* param */
5835 &state
->data
, 12, &state
->num_data
);
5836 TALLOC_FREE(subreq
);
5837 if (tevent_req_nterror(req
, status
)) {
5840 tevent_req_done(req
);
5843 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5844 char ***pnames
, int *pnum_names
)
5846 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5847 req
, struct cli_shadow_copy_data_state
);
5853 if (tevent_req_is_nterror(req
, &status
)) {
5856 num_names
= IVAL(state
->data
, 4);
5857 dlength
= IVAL(state
->data
, 8);
5859 if (!state
->get_names
) {
5860 *pnum_names
= num_names
;
5861 return NT_STATUS_OK
;
5864 if (dlength
+12 > state
->num_data
) {
5865 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5867 names
= talloc_array(mem_ctx
, char *, num_names
);
5868 if (names
== NULL
) {
5869 return NT_STATUS_NO_MEMORY
;
5872 for (i
=0; i
<num_names
; i
++) {
5875 size_t converted_size
;
5877 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5878 ret
= convert_string_talloc(
5879 names
, CH_UTF16LE
, CH_UNIX
,
5880 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5881 &names
[i
], &converted_size
);
5884 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5887 *pnum_names
= num_names
;
5889 return NT_STATUS_OK
;
5892 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5893 uint16_t fnum
, bool get_names
,
5894 char ***pnames
, int *pnum_names
)
5896 TALLOC_CTX
*frame
= talloc_stackframe();
5897 struct tevent_context
*ev
;
5898 struct tevent_req
*req
;
5899 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5901 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5903 * Can't use sync call while an async call is in flight
5905 status
= NT_STATUS_INVALID_PARAMETER
;
5908 ev
= samba_tevent_context_init(frame
);
5912 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5916 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5919 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);