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
;
1953 subreq
= cli_smb2_create_fnum_send(
1954 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1955 file_attributes
, share_access
, create_disposition
,
1958 state
->recv
= cli_ntcreate1_recv
;
1959 subreq
= cli_ntcreate1_send(
1960 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1961 file_attributes
, share_access
, create_disposition
,
1962 create_options
, security_flags
);
1964 if (tevent_req_nomem(subreq
, req
)) {
1965 return tevent_req_post(req
, ev
);
1967 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1971 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1973 struct tevent_req
*req
= tevent_req_callback_data(
1974 subreq
, struct tevent_req
);
1975 struct cli_ntcreate_state
*state
= tevent_req_data(
1976 req
, struct cli_ntcreate_state
);
1979 status
= state
->recv(subreq
, &state
->fnum
, &state
->cr
);
1980 TALLOC_FREE(subreq
);
1981 if (tevent_req_nterror(req
, status
)) {
1984 tevent_req_done(req
);
1987 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *fnum
,
1988 struct smb_create_returns
*cr
)
1990 struct cli_ntcreate_state
*state
= tevent_req_data(
1991 req
, struct cli_ntcreate_state
);
1994 if (tevent_req_is_nterror(req
, &status
)) {
1998 *fnum
= state
->fnum
;
2003 return NT_STATUS_OK
;
2006 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2008 uint32_t CreatFlags
,
2009 uint32_t DesiredAccess
,
2010 uint32_t FileAttributes
,
2011 uint32_t ShareAccess
,
2012 uint32_t CreateDisposition
,
2013 uint32_t CreateOptions
,
2014 uint8_t SecurityFlags
,
2016 struct smb_create_returns
*cr
)
2018 TALLOC_CTX
*frame
= NULL
;
2019 struct tevent_context
*ev
;
2020 struct tevent_req
*req
;
2021 NTSTATUS status
= NT_STATUS_OK
;
2023 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2024 return cli_smb2_create_fnum(cli
,
2036 frame
= talloc_stackframe();
2038 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2040 * Can't use sync call while an async call is in flight
2042 status
= NT_STATUS_INVALID_PARAMETER
;
2046 ev
= samba_tevent_context_init(frame
);
2048 status
= NT_STATUS_NO_MEMORY
;
2052 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2053 DesiredAccess
, FileAttributes
, ShareAccess
,
2054 CreateDisposition
, CreateOptions
,
2057 status
= NT_STATUS_NO_MEMORY
;
2061 if (!tevent_req_poll(req
, ev
)) {
2062 status
= map_nt_error_from_unix(errno
);
2066 status
= cli_ntcreate_recv(req
, pfid
, cr
);
2072 struct cli_nttrans_create_state
{
2074 struct smb_create_returns cr
;
2077 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
2079 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
2080 struct tevent_context
*ev
,
2081 struct cli_state
*cli
,
2083 uint32_t CreatFlags
,
2084 uint32_t DesiredAccess
,
2085 uint32_t FileAttributes
,
2086 uint32_t ShareAccess
,
2087 uint32_t CreateDisposition
,
2088 uint32_t CreateOptions
,
2089 uint8_t SecurityFlags
,
2090 struct security_descriptor
*secdesc
,
2091 struct ea_struct
*eas
,
2094 struct tevent_req
*req
, *subreq
;
2095 struct cli_nttrans_create_state
*state
;
2097 uint8_t *secdesc_buf
;
2100 size_t converted_len
;
2102 req
= tevent_req_create(mem_ctx
,
2103 &state
, struct cli_nttrans_create_state
);
2108 if (secdesc
!= NULL
) {
2109 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2110 &secdesc_buf
, &secdesc_len
);
2111 if (tevent_req_nterror(req
, status
)) {
2112 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2113 nt_errstr(status
)));
2114 return tevent_req_post(req
, ev
);
2125 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2126 return tevent_req_post(req
, ev
);
2129 param
= talloc_array(state
, uint8_t, 53);
2130 if (tevent_req_nomem(param
, req
)) {
2131 return tevent_req_post(req
, ev
);
2134 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2135 fname
, strlen(fname
),
2137 if (tevent_req_nomem(param
, req
)) {
2138 return tevent_req_post(req
, ev
);
2141 SIVAL(param
, 0, CreatFlags
);
2142 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2143 SIVAL(param
, 8, DesiredAccess
);
2144 SIVAL(param
, 12, 0x0); /* AllocationSize */
2145 SIVAL(param
, 16, 0x0); /* AllocationSize */
2146 SIVAL(param
, 20, FileAttributes
);
2147 SIVAL(param
, 24, ShareAccess
);
2148 SIVAL(param
, 28, CreateDisposition
);
2149 SIVAL(param
, 32, CreateOptions
|
2150 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2151 SIVAL(param
, 36, secdesc_len
);
2152 SIVAL(param
, 40, 0); /* EA length*/
2153 SIVAL(param
, 44, converted_len
);
2154 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2155 SCVAL(param
, 52, SecurityFlags
);
2157 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2158 NULL
, -1, /* name, fid */
2159 NT_TRANSACT_CREATE
, 0,
2160 NULL
, 0, 0, /* setup */
2161 param
, talloc_get_size(param
), 128, /* param */
2162 secdesc_buf
, secdesc_len
, 0); /* data */
2163 if (tevent_req_nomem(subreq
, req
)) {
2164 return tevent_req_post(req
, ev
);
2166 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2170 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2172 struct tevent_req
*req
= tevent_req_callback_data(
2173 subreq
, struct tevent_req
);
2174 struct cli_nttrans_create_state
*state
= tevent_req_data(
2175 req
, struct cli_nttrans_create_state
);
2180 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2181 NULL
, 0, NULL
, /* rsetup */
2182 ¶m
, 69, &num_param
,
2184 if (tevent_req_nterror(req
, status
)) {
2187 state
->cr
.oplock_level
= CVAL(param
, 0);
2188 state
->fnum
= SVAL(param
, 2);
2189 state
->cr
.create_action
= IVAL(param
, 4);
2190 state
->cr
.creation_time
= BVAL(param
, 12);
2191 state
->cr
.last_access_time
= BVAL(param
, 20);
2192 state
->cr
.last_write_time
= BVAL(param
, 28);
2193 state
->cr
.change_time
= BVAL(param
, 36);
2194 state
->cr
.file_attributes
= IVAL(param
, 44);
2195 state
->cr
.allocation_size
= BVAL(param
, 48);
2196 state
->cr
.end_of_file
= BVAL(param
, 56);
2199 tevent_req_done(req
);
2202 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2204 struct smb_create_returns
*cr
)
2206 struct cli_nttrans_create_state
*state
= tevent_req_data(
2207 req
, struct cli_nttrans_create_state
);
2210 if (tevent_req_is_nterror(req
, &status
)) {
2213 *fnum
= state
->fnum
;
2217 return NT_STATUS_OK
;
2220 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2222 uint32_t CreatFlags
,
2223 uint32_t DesiredAccess
,
2224 uint32_t FileAttributes
,
2225 uint32_t ShareAccess
,
2226 uint32_t CreateDisposition
,
2227 uint32_t CreateOptions
,
2228 uint8_t SecurityFlags
,
2229 struct security_descriptor
*secdesc
,
2230 struct ea_struct
*eas
,
2233 struct smb_create_returns
*cr
)
2235 TALLOC_CTX
*frame
= talloc_stackframe();
2236 struct tevent_context
*ev
;
2237 struct tevent_req
*req
;
2238 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2240 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2242 * Can't use sync call while an async call is in flight
2244 status
= NT_STATUS_INVALID_PARAMETER
;
2247 ev
= samba_tevent_context_init(frame
);
2251 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2252 DesiredAccess
, FileAttributes
,
2253 ShareAccess
, CreateDisposition
,
2254 CreateOptions
, SecurityFlags
,
2255 secdesc
, eas
, num_eas
);
2259 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2262 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2268 /****************************************************************************
2270 WARNING: if you open with O_WRONLY then getattrE won't work!
2271 ****************************************************************************/
2273 struct cli_openx_state
{
2280 static void cli_openx_done(struct tevent_req
*subreq
);
2282 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2283 struct tevent_context
*ev
,
2284 struct cli_state
*cli
, const char *fname
,
2285 int flags
, int share_mode
,
2286 struct tevent_req
**psmbreq
)
2288 struct tevent_req
*req
, *subreq
;
2289 struct cli_openx_state
*state
;
2291 unsigned accessmode
;
2292 uint8_t additional_flags
;
2295 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2301 if (flags
& O_CREAT
) {
2304 if (!(flags
& O_EXCL
)) {
2305 if (flags
& O_TRUNC
)
2311 accessmode
= (share_mode
<<4);
2313 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2315 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2320 if ((flags
& O_SYNC
) == O_SYNC
) {
2321 accessmode
|= (1<<14);
2325 if (share_mode
== DENY_FCB
) {
2329 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2330 SCVAL(state
->vwv
+ 0, 1, 0);
2331 SSVAL(state
->vwv
+ 1, 0, 0);
2332 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2333 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2334 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2335 SSVAL(state
->vwv
+ 5, 0, 0);
2336 SIVAL(state
->vwv
+ 6, 0, 0);
2337 SSVAL(state
->vwv
+ 8, 0, openfn
);
2338 SIVAL(state
->vwv
+ 9, 0, 0);
2339 SIVAL(state
->vwv
+ 11, 0, 0);
2340 SIVAL(state
->vwv
+ 13, 0, 0);
2342 additional_flags
= 0;
2344 if (cli
->use_oplocks
) {
2345 /* if using oplocks then ask for a batch oplock via
2346 core and extended methods */
2348 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2349 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2352 bytes
= talloc_array(state
, uint8_t, 0);
2353 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2354 strlen(fname
)+1, NULL
);
2356 if (tevent_req_nomem(bytes
, req
)) {
2357 return tevent_req_post(req
, ev
);
2360 state
->bytes
.iov_base
= (void *)bytes
;
2361 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2363 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2364 15, state
->vwv
, 1, &state
->bytes
);
2365 if (subreq
== NULL
) {
2369 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2374 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2375 struct cli_state
*cli
, const char *fname
,
2376 int flags
, int share_mode
)
2378 struct tevent_req
*req
, *subreq
;
2381 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2387 status
= smb1cli_req_chain_submit(&subreq
, 1);
2388 if (tevent_req_nterror(req
, status
)) {
2389 return tevent_req_post(req
, ev
);
2394 static void cli_openx_done(struct tevent_req
*subreq
)
2396 struct tevent_req
*req
= tevent_req_callback_data(
2397 subreq
, struct tevent_req
);
2398 struct cli_openx_state
*state
= tevent_req_data(
2399 req
, struct cli_openx_state
);
2404 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2406 TALLOC_FREE(subreq
);
2407 if (tevent_req_nterror(req
, status
)) {
2410 state
->fnum
= SVAL(vwv
+2, 0);
2411 tevent_req_done(req
);
2414 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2416 struct cli_openx_state
*state
= tevent_req_data(
2417 req
, struct cli_openx_state
);
2420 if (tevent_req_is_nterror(req
, &status
)) {
2423 *pfnum
= state
->fnum
;
2424 return NT_STATUS_OK
;
2427 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2428 int share_mode
, uint16_t *pfnum
)
2430 TALLOC_CTX
*frame
= talloc_stackframe();
2431 struct tevent_context
*ev
;
2432 struct tevent_req
*req
;
2433 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2435 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2437 * Can't use sync call while an async call is in flight
2439 status
= NT_STATUS_INVALID_PARAMETER
;
2443 ev
= samba_tevent_context_init(frame
);
2448 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2453 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2457 status
= cli_openx_recv(req
, pfnum
);
2462 /****************************************************************************
2463 Synchronous wrapper function that does an NtCreateX open by preference
2464 and falls back to openX if this fails.
2465 ****************************************************************************/
2467 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2468 int share_mode_in
, uint16_t *pfnum
)
2471 unsigned int openfn
= 0;
2472 unsigned int dos_deny
= 0;
2473 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2474 struct smb_create_returns cr
;
2476 /* Do the initial mapping into OpenX parameters. */
2477 if (flags
& O_CREAT
) {
2480 if (!(flags
& O_EXCL
)) {
2481 if (flags
& O_TRUNC
)
2487 dos_deny
= (share_mode_in
<<4);
2489 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2491 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2496 if ((flags
& O_SYNC
) == O_SYNC
) {
2497 dos_deny
|= (1<<14);
2501 if (share_mode_in
== DENY_FCB
) {
2506 /* Hmmm. This is what I think the above code
2507 should look like if it's using the constants
2510 if (flags
& O_CREAT
) {
2511 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2513 if (!(flags
& O_EXCL
)) {
2514 if (flags
& O_TRUNC
)
2515 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2517 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2520 dos_deny
= SET_DENY_MODE(share_mode_in
);
2522 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2523 dos_deny
|= DOS_OPEN_RDWR
;
2524 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2525 dos_deny
|= DOS_OPEN_WRONLY
;
2529 if ((flags
& O_SYNC
) == O_SYNC
) {
2530 dos_deny
|= FILE_SYNC_OPENMODE
;
2534 if (share_mode_in
== DENY_FCB
) {
2539 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2540 openfn
, &access_mask
,
2541 &share_mode
, &create_disposition
,
2542 &create_options
, NULL
)) {
2546 status
= cli_ntcreate(cli
,
2558 /* Try and cope will all varients of "we don't do this call"
2559 and fall back to openX. */
2561 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2562 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2563 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2564 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2565 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2566 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2567 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2568 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2569 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2573 if (NT_STATUS_IS_OK(status
) &&
2574 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2575 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2578 * Some (broken) servers return a valid handle
2579 * for directories even if FILE_NON_DIRECTORY_FILE
2580 * is set. Just close the handle and set the
2581 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2583 status
= cli_close(cli
, *pfnum
);
2584 if (!NT_STATUS_IS_OK(status
)) {
2587 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2588 /* Set this so libsmbclient can retrieve it. */
2589 cli
->raw_status
= status
;
2596 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2599 /****************************************************************************
2601 ****************************************************************************/
2603 struct cli_close_state
{
2607 static void cli_close_done(struct tevent_req
*subreq
);
2609 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2610 struct tevent_context
*ev
,
2611 struct cli_state
*cli
,
2613 struct tevent_req
**psubreq
)
2615 struct tevent_req
*req
, *subreq
;
2616 struct cli_close_state
*state
;
2618 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2623 SSVAL(state
->vwv
+0, 0, fnum
);
2624 SIVALS(state
->vwv
+1, 0, -1);
2626 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2628 if (subreq
== NULL
) {
2632 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2637 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2638 struct tevent_context
*ev
,
2639 struct cli_state
*cli
,
2642 struct tevent_req
*req
, *subreq
;
2645 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2650 status
= smb1cli_req_chain_submit(&subreq
, 1);
2651 if (tevent_req_nterror(req
, status
)) {
2652 return tevent_req_post(req
, ev
);
2657 static void cli_close_done(struct tevent_req
*subreq
)
2659 struct tevent_req
*req
= tevent_req_callback_data(
2660 subreq
, struct tevent_req
);
2663 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2664 TALLOC_FREE(subreq
);
2665 if (tevent_req_nterror(req
, status
)) {
2668 tevent_req_done(req
);
2671 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2673 return tevent_req_simple_recv_ntstatus(req
);
2676 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2678 TALLOC_CTX
*frame
= NULL
;
2679 struct tevent_context
*ev
;
2680 struct tevent_req
*req
;
2681 NTSTATUS status
= NT_STATUS_OK
;
2683 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2684 return cli_smb2_close_fnum(cli
, fnum
);
2687 frame
= talloc_stackframe();
2689 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2691 * Can't use sync call while an async call is in flight
2693 status
= NT_STATUS_INVALID_PARAMETER
;
2697 ev
= samba_tevent_context_init(frame
);
2699 status
= NT_STATUS_NO_MEMORY
;
2703 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2705 status
= NT_STATUS_NO_MEMORY
;
2709 if (!tevent_req_poll(req
, ev
)) {
2710 status
= map_nt_error_from_unix(errno
);
2714 status
= cli_close_recv(req
);
2720 /****************************************************************************
2721 Truncate a file to a specified size
2722 ****************************************************************************/
2724 struct ftrunc_state
{
2730 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2732 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2733 NULL
, 0, NULL
, NULL
, 0, NULL
);
2734 tevent_req_simple_finish_ntstatus(subreq
, status
);
2737 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2738 struct tevent_context
*ev
,
2739 struct cli_state
*cli
,
2743 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2744 struct ftrunc_state
*state
= NULL
;
2746 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2751 /* Setup setup word. */
2752 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2754 /* Setup param array. */
2755 SSVAL(state
->param
,0,fnum
);
2756 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2757 SSVAL(state
->param
,4,0);
2759 /* Setup data array. */
2760 SBVAL(state
->data
, 0, size
);
2762 subreq
= cli_trans_send(state
, /* mem ctx. */
2763 ev
, /* event ctx. */
2764 cli
, /* cli_state. */
2765 SMBtrans2
, /* cmd. */
2766 NULL
, /* pipe name. */
2770 &state
->setup
, /* setup. */
2771 1, /* num setup uint16_t words. */
2772 0, /* max returned setup. */
2773 state
->param
, /* param. */
2775 2, /* max returned param. */
2776 state
->data
, /* data. */
2778 0); /* max returned data. */
2780 if (tevent_req_nomem(subreq
, req
)) {
2781 return tevent_req_post(req
, ev
);
2783 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2787 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2789 return tevent_req_simple_recv_ntstatus(req
);
2792 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2794 TALLOC_CTX
*frame
= talloc_stackframe();
2795 struct tevent_context
*ev
= NULL
;
2796 struct tevent_req
*req
= NULL
;
2797 NTSTATUS status
= NT_STATUS_OK
;
2799 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2801 * Can't use sync call while an async call is in flight
2803 status
= NT_STATUS_INVALID_PARAMETER
;
2807 ev
= samba_tevent_context_init(frame
);
2809 status
= NT_STATUS_NO_MEMORY
;
2813 req
= cli_ftruncate_send(frame
,
2819 status
= NT_STATUS_NO_MEMORY
;
2823 if (!tevent_req_poll(req
, ev
)) {
2824 status
= map_nt_error_from_unix(errno
);
2828 status
= cli_ftruncate_recv(req
);
2835 /****************************************************************************
2836 send a lock with a specified locktype
2837 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2838 ****************************************************************************/
2840 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2841 uint32_t offset
, uint32_t len
,
2842 int timeout
, unsigned char locktype
)
2847 unsigned int set_timeout
= 0;
2848 unsigned int saved_timeout
= 0;
2850 SCVAL(vwv
+ 0, 0, 0xff);
2851 SCVAL(vwv
+ 0, 1, 0);
2852 SSVAL(vwv
+ 1, 0, 0);
2853 SSVAL(vwv
+ 2, 0, fnum
);
2854 SCVAL(vwv
+ 3, 0, locktype
);
2855 SCVAL(vwv
+ 3, 1, 0);
2856 SIVALS(vwv
+ 4, 0, timeout
);
2857 SSVAL(vwv
+ 6, 0, 0);
2858 SSVAL(vwv
+ 7, 0, 1);
2860 SSVAL(bytes
, 0, cli_getpid(cli
));
2861 SIVAL(bytes
, 2, offset
);
2862 SIVAL(bytes
, 6, len
);
2865 if (timeout
== -1) {
2866 set_timeout
= 0x7FFFFFFF;
2868 set_timeout
= timeout
+ 2*1000;
2870 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2873 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2874 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2876 if (saved_timeout
!= 0) {
2877 cli_set_timeout(cli
, saved_timeout
);
2883 /****************************************************************************
2885 note that timeout is in units of 2 milliseconds
2886 ****************************************************************************/
2888 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2889 uint32_t offset
, uint32_t len
, int timeout
,
2890 enum brl_type lock_type
)
2894 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2895 (lock_type
== READ_LOCK
? 1 : 0));
2899 /****************************************************************************
2901 ****************************************************************************/
2903 struct cli_unlock_state
{
2908 static void cli_unlock_done(struct tevent_req
*subreq
);
2910 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2911 struct tevent_context
*ev
,
2912 struct cli_state
*cli
,
2918 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2919 struct cli_unlock_state
*state
= NULL
;
2920 uint8_t additional_flags
= 0;
2922 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2927 SCVAL(state
->vwv
+0, 0, 0xFF);
2928 SSVAL(state
->vwv
+2, 0, fnum
);
2929 SCVAL(state
->vwv
+3, 0, 0);
2930 SIVALS(state
->vwv
+4, 0, 0);
2931 SSVAL(state
->vwv
+6, 0, 1);
2932 SSVAL(state
->vwv
+7, 0, 0);
2934 SSVAL(state
->data
, 0, cli_getpid(cli
));
2935 SIVAL(state
->data
, 2, offset
);
2936 SIVAL(state
->data
, 6, len
);
2938 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2939 8, state
->vwv
, 10, state
->data
);
2940 if (tevent_req_nomem(subreq
, req
)) {
2941 return tevent_req_post(req
, ev
);
2943 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2947 static void cli_unlock_done(struct tevent_req
*subreq
)
2949 struct tevent_req
*req
= tevent_req_callback_data(
2950 subreq
, struct tevent_req
);
2953 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2954 TALLOC_FREE(subreq
);
2955 if (tevent_req_nterror(req
, status
)) {
2958 tevent_req_done(req
);
2961 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2963 return tevent_req_simple_recv_ntstatus(req
);
2966 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2971 TALLOC_CTX
*frame
= talloc_stackframe();
2972 struct tevent_context
*ev
;
2973 struct tevent_req
*req
;
2974 NTSTATUS status
= NT_STATUS_OK
;
2976 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2978 * Can't use sync call while an async call is in flight
2980 status
= NT_STATUS_INVALID_PARAMETER
;
2984 ev
= samba_tevent_context_init(frame
);
2986 status
= NT_STATUS_NO_MEMORY
;
2990 req
= cli_unlock_send(frame
, ev
, cli
,
2993 status
= NT_STATUS_NO_MEMORY
;
2997 if (!tevent_req_poll(req
, ev
)) {
2998 status
= map_nt_error_from_unix(errno
);
3002 status
= cli_unlock_recv(req
);
3009 /****************************************************************************
3010 Lock a file with 64 bit offsets.
3011 ****************************************************************************/
3013 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
3014 uint64_t offset
, uint64_t len
, int timeout
,
3015 enum brl_type lock_type
)
3019 unsigned int set_timeout
= 0;
3020 unsigned int saved_timeout
= 0;
3024 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3025 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
3028 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
3029 ltype
|= LOCKING_ANDX_LARGE_FILES
;
3031 SCVAL(vwv
+ 0, 0, 0xff);
3032 SCVAL(vwv
+ 0, 1, 0);
3033 SSVAL(vwv
+ 1, 0, 0);
3034 SSVAL(vwv
+ 2, 0, fnum
);
3035 SCVAL(vwv
+ 3, 0, ltype
);
3036 SCVAL(vwv
+ 3, 1, 0);
3037 SIVALS(vwv
+ 4, 0, timeout
);
3038 SSVAL(vwv
+ 6, 0, 0);
3039 SSVAL(vwv
+ 7, 0, 1);
3041 SIVAL(bytes
, 0, cli_getpid(cli
));
3042 SOFF_T_R(bytes
, 4, offset
);
3043 SOFF_T_R(bytes
, 12, len
);
3046 if (timeout
== -1) {
3047 set_timeout
= 0x7FFFFFFF;
3049 set_timeout
= timeout
+ 2*1000;
3051 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3054 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3055 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3057 if (saved_timeout
!= 0) {
3058 cli_set_timeout(cli
, saved_timeout
);
3064 /****************************************************************************
3065 Unlock a file with 64 bit offsets.
3066 ****************************************************************************/
3068 struct cli_unlock64_state
{
3073 static void cli_unlock64_done(struct tevent_req
*subreq
);
3075 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
3076 struct tevent_context
*ev
,
3077 struct cli_state
*cli
,
3083 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3084 struct cli_unlock64_state
*state
= NULL
;
3085 uint8_t additional_flags
= 0;
3087 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3092 SCVAL(state
->vwv
+0, 0, 0xff);
3093 SSVAL(state
->vwv
+2, 0, fnum
);
3094 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3095 SIVALS(state
->vwv
+4, 0, 0);
3096 SSVAL(state
->vwv
+6, 0, 1);
3097 SSVAL(state
->vwv
+7, 0, 0);
3099 SIVAL(state
->data
, 0, cli_getpid(cli
));
3100 SOFF_T_R(state
->data
, 4, offset
);
3101 SOFF_T_R(state
->data
, 12, len
);
3103 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
3104 8, state
->vwv
, 20, state
->data
);
3105 if (tevent_req_nomem(subreq
, req
)) {
3106 return tevent_req_post(req
, ev
);
3108 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3112 static void cli_unlock64_done(struct tevent_req
*subreq
)
3114 struct tevent_req
*req
= tevent_req_callback_data(
3115 subreq
, struct tevent_req
);
3118 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3119 TALLOC_FREE(subreq
);
3120 if (tevent_req_nterror(req
, status
)) {
3123 tevent_req_done(req
);
3126 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3128 return tevent_req_simple_recv_ntstatus(req
);
3131 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3136 TALLOC_CTX
*frame
= talloc_stackframe();
3137 struct tevent_context
*ev
;
3138 struct tevent_req
*req
;
3139 NTSTATUS status
= NT_STATUS_OK
;
3141 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3142 return cli_unlock(cli
, fnum
, offset
, len
);
3145 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3147 * Can't use sync call while an async call is in flight
3149 status
= NT_STATUS_INVALID_PARAMETER
;
3153 ev
= samba_tevent_context_init(frame
);
3155 status
= NT_STATUS_NO_MEMORY
;
3159 req
= cli_unlock64_send(frame
, ev
, cli
,
3162 status
= NT_STATUS_NO_MEMORY
;
3166 if (!tevent_req_poll(req
, ev
)) {
3167 status
= map_nt_error_from_unix(errno
);
3171 status
= cli_unlock64_recv(req
);
3178 /****************************************************************************
3179 Get/unlock a POSIX lock on a file - internal function.
3180 ****************************************************************************/
3182 struct posix_lock_state
{
3185 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3188 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3190 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3191 NULL
, 0, NULL
, NULL
, 0, NULL
);
3192 tevent_req_simple_finish_ntstatus(subreq
, status
);
3195 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3196 struct tevent_context
*ev
,
3197 struct cli_state
*cli
,
3202 enum brl_type lock_type
)
3204 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3205 struct posix_lock_state
*state
= NULL
;
3207 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3212 /* Setup setup word. */
3213 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3215 /* Setup param array. */
3216 SSVAL(&state
->param
, 0, fnum
);
3217 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3219 /* Setup data array. */
3220 switch (lock_type
) {
3222 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3223 POSIX_LOCK_TYPE_READ
);
3226 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3227 POSIX_LOCK_TYPE_WRITE
);
3230 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3231 POSIX_LOCK_TYPE_UNLOCK
);
3238 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3239 POSIX_LOCK_FLAG_WAIT
);
3241 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3242 POSIX_LOCK_FLAG_NOWAIT
);
3245 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3246 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3247 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3249 subreq
= cli_trans_send(state
, /* mem ctx. */
3250 ev
, /* event ctx. */
3251 cli
, /* cli_state. */
3252 SMBtrans2
, /* cmd. */
3253 NULL
, /* pipe name. */
3257 &state
->setup
, /* setup. */
3258 1, /* num setup uint16_t words. */
3259 0, /* max returned setup. */
3260 state
->param
, /* param. */
3262 2, /* max returned param. */
3263 state
->data
, /* data. */
3264 POSIX_LOCK_DATA_SIZE
, /* num data. */
3265 0); /* max returned data. */
3267 if (tevent_req_nomem(subreq
, req
)) {
3268 return tevent_req_post(req
, ev
);
3270 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3274 /****************************************************************************
3276 ****************************************************************************/
3278 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3279 struct tevent_context
*ev
,
3280 struct cli_state
*cli
,
3285 enum brl_type lock_type
)
3287 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3288 wait_lock
, lock_type
);
3291 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3293 return tevent_req_simple_recv_ntstatus(req
);
3296 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3297 uint64_t offset
, uint64_t len
,
3298 bool wait_lock
, enum brl_type lock_type
)
3300 TALLOC_CTX
*frame
= talloc_stackframe();
3301 struct tevent_context
*ev
= NULL
;
3302 struct tevent_req
*req
= NULL
;
3303 NTSTATUS status
= NT_STATUS_OK
;
3305 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3307 * Can't use sync call while an async call is in flight
3309 status
= NT_STATUS_INVALID_PARAMETER
;
3313 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3314 status
= NT_STATUS_INVALID_PARAMETER
;
3318 ev
= samba_tevent_context_init(frame
);
3320 status
= NT_STATUS_NO_MEMORY
;
3324 req
= cli_posix_lock_send(frame
,
3333 status
= NT_STATUS_NO_MEMORY
;
3337 if (!tevent_req_poll(req
, ev
)) {
3338 status
= map_nt_error_from_unix(errno
);
3342 status
= cli_posix_lock_recv(req
);
3349 /****************************************************************************
3350 POSIX Unlock a file.
3351 ****************************************************************************/
3353 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3354 struct tevent_context
*ev
,
3355 struct cli_state
*cli
,
3360 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3361 false, UNLOCK_LOCK
);
3364 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3366 return tevent_req_simple_recv_ntstatus(req
);
3369 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3371 TALLOC_CTX
*frame
= talloc_stackframe();
3372 struct tevent_context
*ev
= NULL
;
3373 struct tevent_req
*req
= NULL
;
3374 NTSTATUS status
= NT_STATUS_OK
;
3376 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3378 * Can't use sync call while an async call is in flight
3380 status
= NT_STATUS_INVALID_PARAMETER
;
3384 ev
= samba_tevent_context_init(frame
);
3386 status
= NT_STATUS_NO_MEMORY
;
3390 req
= cli_posix_unlock_send(frame
,
3397 status
= NT_STATUS_NO_MEMORY
;
3401 if (!tevent_req_poll(req
, ev
)) {
3402 status
= map_nt_error_from_unix(errno
);
3406 status
= cli_posix_unlock_recv(req
);
3413 /****************************************************************************
3414 Do a SMBgetattrE call.
3415 ****************************************************************************/
3417 static void cli_getattrE_done(struct tevent_req
*subreq
);
3419 struct cli_getattrE_state
{
3429 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3430 struct tevent_context
*ev
,
3431 struct cli_state
*cli
,
3434 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3435 struct cli_getattrE_state
*state
= NULL
;
3436 uint8_t additional_flags
= 0;
3438 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3443 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3444 SSVAL(state
->vwv
+0,0,fnum
);
3446 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3447 1, state
->vwv
, 0, NULL
);
3448 if (tevent_req_nomem(subreq
, req
)) {
3449 return tevent_req_post(req
, ev
);
3451 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3455 static void cli_getattrE_done(struct tevent_req
*subreq
)
3457 struct tevent_req
*req
= tevent_req_callback_data(
3458 subreq
, struct tevent_req
);
3459 struct cli_getattrE_state
*state
= tevent_req_data(
3460 req
, struct cli_getattrE_state
);
3462 uint16_t *vwv
= NULL
;
3465 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3467 TALLOC_FREE(subreq
);
3468 if (tevent_req_nterror(req
, status
)) {
3472 state
->size
= (off_t
)IVAL(vwv
+6,0);
3473 state
->attr
= SVAL(vwv
+10,0);
3474 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3475 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3476 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3478 tevent_req_done(req
);
3481 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3484 time_t *change_time
,
3485 time_t *access_time
,
3488 struct cli_getattrE_state
*state
= tevent_req_data(
3489 req
, struct cli_getattrE_state
);
3492 if (tevent_req_is_nterror(req
, &status
)) {
3496 *attr
= state
->attr
;
3499 *size
= state
->size
;
3502 *change_time
= state
->change_time
;
3505 *access_time
= state
->access_time
;
3508 *write_time
= state
->write_time
;
3510 return NT_STATUS_OK
;
3513 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3517 time_t *change_time
,
3518 time_t *access_time
,
3521 TALLOC_CTX
*frame
= NULL
;
3522 struct tevent_context
*ev
= NULL
;
3523 struct tevent_req
*req
= NULL
;
3524 NTSTATUS status
= NT_STATUS_OK
;
3526 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3527 return cli_smb2_getattrE(cli
,
3536 frame
= talloc_stackframe();
3538 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3540 * Can't use sync call while an async call is in flight
3542 status
= NT_STATUS_INVALID_PARAMETER
;
3546 ev
= samba_tevent_context_init(frame
);
3548 status
= NT_STATUS_NO_MEMORY
;
3552 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3554 status
= NT_STATUS_NO_MEMORY
;
3558 if (!tevent_req_poll(req
, ev
)) {
3559 status
= map_nt_error_from_unix(errno
);
3563 status
= cli_getattrE_recv(req
,
3575 /****************************************************************************
3577 ****************************************************************************/
3579 static void cli_getatr_done(struct tevent_req
*subreq
);
3581 struct cli_getatr_state
{
3588 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3589 struct tevent_context
*ev
,
3590 struct cli_state
*cli
,
3593 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3594 struct cli_getatr_state
*state
= NULL
;
3595 uint8_t additional_flags
= 0;
3596 uint8_t *bytes
= NULL
;
3598 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3603 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3605 bytes
= talloc_array(state
, uint8_t, 1);
3606 if (tevent_req_nomem(bytes
, req
)) {
3607 return tevent_req_post(req
, ev
);
3610 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3611 strlen(fname
)+1, NULL
);
3613 if (tevent_req_nomem(bytes
, req
)) {
3614 return tevent_req_post(req
, ev
);
3617 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3618 0, NULL
, talloc_get_size(bytes
), bytes
);
3619 if (tevent_req_nomem(subreq
, req
)) {
3620 return tevent_req_post(req
, ev
);
3622 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3626 static void cli_getatr_done(struct tevent_req
*subreq
)
3628 struct tevent_req
*req
= tevent_req_callback_data(
3629 subreq
, struct tevent_req
);
3630 struct cli_getatr_state
*state
= tevent_req_data(
3631 req
, struct cli_getatr_state
);
3633 uint16_t *vwv
= NULL
;
3636 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3638 TALLOC_FREE(subreq
);
3639 if (tevent_req_nterror(req
, status
)) {
3643 state
->attr
= SVAL(vwv
+0,0);
3644 state
->size
= (off_t
)IVAL(vwv
+3,0);
3645 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3647 tevent_req_done(req
);
3650 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3655 struct cli_getatr_state
*state
= tevent_req_data(
3656 req
, struct cli_getatr_state
);
3659 if (tevent_req_is_nterror(req
, &status
)) {
3663 *attr
= state
->attr
;
3666 *size
= state
->size
;
3669 *write_time
= state
->write_time
;
3671 return NT_STATUS_OK
;
3674 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3680 TALLOC_CTX
*frame
= NULL
;
3681 struct tevent_context
*ev
= NULL
;
3682 struct tevent_req
*req
= NULL
;
3683 NTSTATUS status
= NT_STATUS_OK
;
3685 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3686 return cli_smb2_getatr(cli
,
3693 frame
= talloc_stackframe();
3695 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3697 * Can't use sync call while an async call is in flight
3699 status
= NT_STATUS_INVALID_PARAMETER
;
3703 ev
= samba_tevent_context_init(frame
);
3705 status
= NT_STATUS_NO_MEMORY
;
3709 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3711 status
= NT_STATUS_NO_MEMORY
;
3715 if (!tevent_req_poll(req
, ev
)) {
3716 status
= map_nt_error_from_unix(errno
);
3720 status
= cli_getatr_recv(req
,
3730 /****************************************************************************
3731 Do a SMBsetattrE call.
3732 ****************************************************************************/
3734 static void cli_setattrE_done(struct tevent_req
*subreq
);
3736 struct cli_setattrE_state
{
3740 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3741 struct tevent_context
*ev
,
3742 struct cli_state
*cli
,
3748 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3749 struct cli_setattrE_state
*state
= NULL
;
3750 uint8_t additional_flags
= 0;
3752 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3757 SSVAL(state
->vwv
+0, 0, fnum
);
3758 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3759 smb1cli_conn_server_time_zone(cli
->conn
));
3760 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3761 smb1cli_conn_server_time_zone(cli
->conn
));
3762 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3763 smb1cli_conn_server_time_zone(cli
->conn
));
3765 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3766 7, state
->vwv
, 0, NULL
);
3767 if (tevent_req_nomem(subreq
, req
)) {
3768 return tevent_req_post(req
, ev
);
3770 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3774 static void cli_setattrE_done(struct tevent_req
*subreq
)
3776 struct tevent_req
*req
= tevent_req_callback_data(
3777 subreq
, struct tevent_req
);
3780 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3781 TALLOC_FREE(subreq
);
3782 if (tevent_req_nterror(req
, status
)) {
3785 tevent_req_done(req
);
3788 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3790 return tevent_req_simple_recv_ntstatus(req
);
3793 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3799 TALLOC_CTX
*frame
= NULL
;
3800 struct tevent_context
*ev
= NULL
;
3801 struct tevent_req
*req
= NULL
;
3802 NTSTATUS status
= NT_STATUS_OK
;
3804 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3805 return cli_smb2_setattrE(cli
,
3812 frame
= talloc_stackframe();
3814 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3816 * Can't use sync call while an async call is in flight
3818 status
= NT_STATUS_INVALID_PARAMETER
;
3822 ev
= samba_tevent_context_init(frame
);
3824 status
= NT_STATUS_NO_MEMORY
;
3828 req
= cli_setattrE_send(frame
, ev
,
3836 status
= NT_STATUS_NO_MEMORY
;
3840 if (!tevent_req_poll(req
, ev
)) {
3841 status
= map_nt_error_from_unix(errno
);
3845 status
= cli_setattrE_recv(req
);
3852 /****************************************************************************
3853 Do a SMBsetatr call.
3854 ****************************************************************************/
3856 static void cli_setatr_done(struct tevent_req
*subreq
);
3858 struct cli_setatr_state
{
3862 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3863 struct tevent_context
*ev
,
3864 struct cli_state
*cli
,
3869 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3870 struct cli_setatr_state
*state
= NULL
;
3871 uint8_t additional_flags
= 0;
3872 uint8_t *bytes
= NULL
;
3874 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3879 SSVAL(state
->vwv
+0, 0, attr
);
3880 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3882 bytes
= talloc_array(state
, uint8_t, 1);
3883 if (tevent_req_nomem(bytes
, req
)) {
3884 return tevent_req_post(req
, ev
);
3887 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3888 strlen(fname
)+1, NULL
);
3889 if (tevent_req_nomem(bytes
, req
)) {
3890 return tevent_req_post(req
, ev
);
3892 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3893 talloc_get_size(bytes
)+1);
3894 if (tevent_req_nomem(bytes
, req
)) {
3895 return tevent_req_post(req
, ev
);
3898 bytes
[talloc_get_size(bytes
)-1] = 4;
3899 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3901 if (tevent_req_nomem(bytes
, req
)) {
3902 return tevent_req_post(req
, ev
);
3905 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3906 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3907 if (tevent_req_nomem(subreq
, req
)) {
3908 return tevent_req_post(req
, ev
);
3910 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3914 static void cli_setatr_done(struct tevent_req
*subreq
)
3916 struct tevent_req
*req
= tevent_req_callback_data(
3917 subreq
, struct tevent_req
);
3920 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3921 TALLOC_FREE(subreq
);
3922 if (tevent_req_nterror(req
, status
)) {
3925 tevent_req_done(req
);
3928 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3930 return tevent_req_simple_recv_ntstatus(req
);
3933 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3938 TALLOC_CTX
*frame
= NULL
;
3939 struct tevent_context
*ev
= NULL
;
3940 struct tevent_req
*req
= NULL
;
3941 NTSTATUS status
= NT_STATUS_OK
;
3943 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3944 return cli_smb2_setatr(cli
,
3950 frame
= talloc_stackframe();
3952 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3954 * Can't use sync call while an async call is in flight
3956 status
= NT_STATUS_INVALID_PARAMETER
;
3960 ev
= samba_tevent_context_init(frame
);
3962 status
= NT_STATUS_NO_MEMORY
;
3966 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3968 status
= NT_STATUS_NO_MEMORY
;
3972 if (!tevent_req_poll(req
, ev
)) {
3973 status
= map_nt_error_from_unix(errno
);
3977 status
= cli_setatr_recv(req
);
3984 /****************************************************************************
3985 Check for existance of a dir.
3986 ****************************************************************************/
3988 static void cli_chkpath_done(struct tevent_req
*subreq
);
3990 struct cli_chkpath_state
{
3994 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3995 struct tevent_context
*ev
,
3996 struct cli_state
*cli
,
3999 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4000 struct cli_chkpath_state
*state
= NULL
;
4001 uint8_t additional_flags
= 0;
4002 uint8_t *bytes
= NULL
;
4004 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
4009 bytes
= talloc_array(state
, uint8_t, 1);
4010 if (tevent_req_nomem(bytes
, req
)) {
4011 return tevent_req_post(req
, ev
);
4014 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4015 strlen(fname
)+1, NULL
);
4017 if (tevent_req_nomem(bytes
, req
)) {
4018 return tevent_req_post(req
, ev
);
4021 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
4022 0, NULL
, talloc_get_size(bytes
), bytes
);
4023 if (tevent_req_nomem(subreq
, req
)) {
4024 return tevent_req_post(req
, ev
);
4026 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
4030 static void cli_chkpath_done(struct tevent_req
*subreq
)
4032 struct tevent_req
*req
= tevent_req_callback_data(
4033 subreq
, struct tevent_req
);
4036 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4037 TALLOC_FREE(subreq
);
4038 if (tevent_req_nterror(req
, status
)) {
4041 tevent_req_done(req
);
4044 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
4046 return tevent_req_simple_recv_ntstatus(req
);
4049 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
4051 TALLOC_CTX
*frame
= talloc_stackframe();
4052 struct tevent_context
*ev
= NULL
;
4053 struct tevent_req
*req
= NULL
;
4055 NTSTATUS status
= NT_STATUS_OK
;
4057 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4059 * Can't use sync call while an async call is in flight
4061 status
= NT_STATUS_INVALID_PARAMETER
;
4065 path2
= talloc_strdup(frame
, path
);
4067 status
= NT_STATUS_NO_MEMORY
;
4070 trim_char(path2
,'\0','\\');
4072 path2
= talloc_strdup(frame
, "\\");
4074 status
= NT_STATUS_NO_MEMORY
;
4079 ev
= samba_tevent_context_init(frame
);
4081 status
= NT_STATUS_NO_MEMORY
;
4085 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4087 status
= NT_STATUS_NO_MEMORY
;
4091 if (!tevent_req_poll(req
, ev
)) {
4092 status
= map_nt_error_from_unix(errno
);
4096 status
= cli_chkpath_recv(req
);
4103 /****************************************************************************
4105 ****************************************************************************/
4107 static void cli_dskattr_done(struct tevent_req
*subreq
);
4109 struct cli_dskattr_state
{
4115 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4116 struct tevent_context
*ev
,
4117 struct cli_state
*cli
)
4119 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4120 struct cli_dskattr_state
*state
= NULL
;
4121 uint8_t additional_flags
= 0;
4123 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4128 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
4130 if (tevent_req_nomem(subreq
, req
)) {
4131 return tevent_req_post(req
, ev
);
4133 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4137 static void cli_dskattr_done(struct tevent_req
*subreq
)
4139 struct tevent_req
*req
= tevent_req_callback_data(
4140 subreq
, struct tevent_req
);
4141 struct cli_dskattr_state
*state
= tevent_req_data(
4142 req
, struct cli_dskattr_state
);
4144 uint16_t *vwv
= NULL
;
4147 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4149 TALLOC_FREE(subreq
);
4150 if (tevent_req_nterror(req
, status
)) {
4153 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4154 state
->total
= SVAL(vwv
+0, 0);
4155 state
->avail
= SVAL(vwv
+3, 0);
4156 tevent_req_done(req
);
4159 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4161 struct cli_dskattr_state
*state
= tevent_req_data(
4162 req
, struct cli_dskattr_state
);
4165 if (tevent_req_is_nterror(req
, &status
)) {
4168 *bsize
= state
->bsize
;
4169 *total
= state
->total
;
4170 *avail
= state
->avail
;
4171 return NT_STATUS_OK
;
4174 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4176 TALLOC_CTX
*frame
= NULL
;
4177 struct tevent_context
*ev
= NULL
;
4178 struct tevent_req
*req
= NULL
;
4179 NTSTATUS status
= NT_STATUS_OK
;
4181 frame
= talloc_stackframe();
4183 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4185 * Can't use sync call while an async call is in flight
4187 status
= NT_STATUS_INVALID_PARAMETER
;
4191 ev
= samba_tevent_context_init(frame
);
4193 status
= NT_STATUS_NO_MEMORY
;
4197 req
= cli_dskattr_send(frame
, ev
, cli
);
4199 status
= NT_STATUS_NO_MEMORY
;
4203 if (!tevent_req_poll(req
, ev
)) {
4204 status
= map_nt_error_from_unix(errno
);
4208 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4215 NTSTATUS
cli_disk_size(struct cli_state
*cli
, uint64_t *bsize
, uint64_t *total
, uint64_t *avail
)
4217 uint64_t sectors_per_block
;
4218 uint64_t bytes_per_sector
;
4219 int old_bsize
, old_total
, old_avail
;
4222 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4223 return cli_smb2_dskattr(cli
, bsize
, total
, avail
);
4227 * Try the trans2 disk full size info call first.
4228 * We already use this in SMBC_fstatvfs_ctx().
4229 * Ignore 'actual_available_units' as we only
4230 * care about the quota for the caller.
4233 status
= cli_get_fs_full_size_info(cli
,
4240 /* Try and cope will all varients of "we don't do this call"
4241 and fall back to cli_dskattr. */
4243 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
4244 NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
) ||
4245 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
4246 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
4247 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
4248 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
4249 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
4250 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
4251 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
4252 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
4256 if (!NT_STATUS_IS_OK(status
)) {
4261 *bsize
= sectors_per_block
*
4265 return NT_STATUS_OK
;
4269 /* Old SMB1 core protocol fallback. */
4270 status
= cli_dskattr(cli
, &old_bsize
, &old_total
, &old_avail
);
4271 if (!NT_STATUS_IS_OK(status
)) {
4275 *bsize
= (uint64_t)old_bsize
;
4278 *total
= (uint64_t)old_total
;
4281 *avail
= (uint64_t)old_avail
;
4283 return NT_STATUS_OK
;
4286 /****************************************************************************
4287 Create and open a temporary file.
4288 ****************************************************************************/
4290 static void cli_ctemp_done(struct tevent_req
*subreq
);
4292 struct ctemp_state
{
4298 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4299 struct tevent_context
*ev
,
4300 struct cli_state
*cli
,
4303 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4304 struct ctemp_state
*state
= NULL
;
4305 uint8_t additional_flags
= 0;
4306 uint8_t *bytes
= NULL
;
4308 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4313 SSVAL(state
->vwv
,0,0);
4314 SIVALS(state
->vwv
+1,0,-1);
4316 bytes
= talloc_array(state
, uint8_t, 1);
4317 if (tevent_req_nomem(bytes
, req
)) {
4318 return tevent_req_post(req
, ev
);
4321 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4322 strlen(path
)+1, NULL
);
4323 if (tevent_req_nomem(bytes
, req
)) {
4324 return tevent_req_post(req
, ev
);
4327 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4328 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4329 if (tevent_req_nomem(subreq
, req
)) {
4330 return tevent_req_post(req
, ev
);
4332 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4336 static void cli_ctemp_done(struct tevent_req
*subreq
)
4338 struct tevent_req
*req
= tevent_req_callback_data(
4339 subreq
, struct tevent_req
);
4340 struct ctemp_state
*state
= tevent_req_data(
4341 req
, struct ctemp_state
);
4345 uint32_t num_bytes
= 0;
4346 uint8_t *bytes
= NULL
;
4348 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4349 &num_bytes
, &bytes
);
4350 TALLOC_FREE(subreq
);
4351 if (tevent_req_nterror(req
, status
)) {
4355 state
->fnum
= SVAL(vwv
+0, 0);
4357 /* From W2K3, the result is just the ASCII name */
4358 if (num_bytes
< 2) {
4359 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4363 if (pull_string_talloc(state
,
4370 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4373 tevent_req_done(req
);
4376 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4381 struct ctemp_state
*state
= tevent_req_data(req
,
4382 struct ctemp_state
);
4385 if (tevent_req_is_nterror(req
, &status
)) {
4388 *pfnum
= state
->fnum
;
4389 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4391 return NT_STATUS_NO_MEMORY
;
4393 return NT_STATUS_OK
;
4396 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4402 TALLOC_CTX
*frame
= talloc_stackframe();
4403 struct tevent_context
*ev
;
4404 struct tevent_req
*req
;
4405 NTSTATUS status
= NT_STATUS_OK
;
4407 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4409 * Can't use sync call while an async call is in flight
4411 status
= NT_STATUS_INVALID_PARAMETER
;
4415 ev
= samba_tevent_context_init(frame
);
4417 status
= NT_STATUS_NO_MEMORY
;
4421 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4423 status
= NT_STATUS_NO_MEMORY
;
4427 if (!tevent_req_poll(req
, ev
)) {
4428 status
= map_nt_error_from_unix(errno
);
4432 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4440 send a raw ioctl - used by the torture code
4442 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4447 SSVAL(vwv
+0, 0, fnum
);
4448 SSVAL(vwv
+1, 0, code
>>16);
4449 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4451 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4452 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4453 if (!NT_STATUS_IS_OK(status
)) {
4456 *blob
= data_blob_null
;
4457 return NT_STATUS_OK
;
4460 /*********************************************************
4461 Set an extended attribute utility fn.
4462 *********************************************************/
4464 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4465 uint8_t *param
, unsigned int param_len
,
4466 const char *ea_name
,
4467 const char *ea_val
, size_t ea_len
)
4470 unsigned int data_len
= 0;
4471 uint8_t *data
= NULL
;
4473 size_t ea_namelen
= strlen(ea_name
);
4476 SSVAL(setup
, 0, setup_val
);
4478 if (ea_namelen
== 0 && ea_len
== 0) {
4480 data
= talloc_array(talloc_tos(),
4484 return NT_STATUS_NO_MEMORY
;
4487 SIVAL(p
,0,data_len
);
4489 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4490 data
= talloc_array(talloc_tos(),
4494 return NT_STATUS_NO_MEMORY
;
4497 SIVAL(p
,0,data_len
);
4499 SCVAL(p
, 0, 0); /* EA flags. */
4500 SCVAL(p
, 1, ea_namelen
);
4501 SSVAL(p
, 2, ea_len
);
4502 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4503 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4506 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4508 param
, param_len
, 2,
4509 data
, data_len
, CLI_BUFFER_SIZE
,
4511 NULL
, 0, NULL
, /* rsetup */
4512 NULL
, 0, NULL
, /* rparam */
4513 NULL
, 0, NULL
); /* rdata */
4518 /*********************************************************
4519 Set an extended attribute on a pathname.
4520 *********************************************************/
4522 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4523 const char *ea_name
, const char *ea_val
,
4526 unsigned int param_len
= 0;
4529 TALLOC_CTX
*frame
= NULL
;
4531 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4532 return cli_smb2_set_ea_path(cli
,
4539 frame
= talloc_stackframe();
4541 param
= talloc_array(frame
, uint8_t, 6);
4543 status
= NT_STATUS_NO_MEMORY
;
4546 SSVAL(param
,0,SMB_INFO_SET_EA
);
4550 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4551 path
, strlen(path
)+1,
4553 param_len
= talloc_get_size(param
);
4555 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4556 ea_name
, ea_val
, ea_len
);
4564 /*********************************************************
4565 Set an extended attribute on an fnum.
4566 *********************************************************/
4568 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4569 const char *ea_name
, const char *ea_val
,
4574 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4575 return cli_smb2_set_ea_fnum(cli
,
4582 memset(param
, 0, 6);
4583 SSVAL(param
,0,fnum
);
4584 SSVAL(param
,2,SMB_INFO_SET_EA
);
4586 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4587 ea_name
, ea_val
, ea_len
);
4590 /*********************************************************
4591 Get an extended attribute list utility fn.
4592 *********************************************************/
4594 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4596 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4598 struct ea_struct
*ea_list
= NULL
;
4603 if (rdata_len
< 4) {
4607 ea_size
= (size_t)IVAL(rdata
,0);
4608 if (ea_size
> rdata_len
) {
4613 /* No EA's present. */
4622 /* Validate the EA list and count it. */
4623 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4624 unsigned int ea_namelen
= CVAL(p
,1);
4625 unsigned int ea_valuelen
= SVAL(p
,2);
4626 if (ea_namelen
== 0) {
4629 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4632 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4633 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4642 *pnum_eas
= num_eas
;
4644 /* Caller only wants number of EA's. */
4648 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4653 ea_size
= (size_t)IVAL(rdata
,0);
4656 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4657 struct ea_struct
*ea
= &ea_list
[num_eas
];
4658 fstring unix_ea_name
;
4659 unsigned int ea_namelen
= CVAL(p
,1);
4660 unsigned int ea_valuelen
= SVAL(p
,2);
4662 ea
->flags
= CVAL(p
,0);
4663 unix_ea_name
[0] = '\0';
4664 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4665 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4669 /* Ensure the value is null terminated (in case it's a string). */
4670 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4671 if (!ea
->value
.data
) {
4675 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4677 ea
->value
.data
[ea_valuelen
] = 0;
4679 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4682 *pea_list
= ea_list
;
4686 TALLOC_FREE(ea_list
);
4690 /*********************************************************
4691 Get an extended attribute list from a pathname.
4692 *********************************************************/
4694 struct cli_get_ea_list_path_state
{
4699 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4701 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4702 struct tevent_context
*ev
,
4703 struct cli_state
*cli
,
4706 struct tevent_req
*req
, *subreq
;
4707 struct cli_get_ea_list_path_state
*state
;
4709 req
= tevent_req_create(mem_ctx
, &state
,
4710 struct cli_get_ea_list_path_state
);
4714 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4715 SMB_INFO_QUERY_ALL_EAS
, 4,
4717 if (tevent_req_nomem(subreq
, req
)) {
4718 return tevent_req_post(req
, ev
);
4720 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4724 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4726 struct tevent_req
*req
= tevent_req_callback_data(
4727 subreq
, struct tevent_req
);
4728 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4729 req
, struct cli_get_ea_list_path_state
);
4732 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4734 TALLOC_FREE(subreq
);
4735 if (tevent_req_nterror(req
, status
)) {
4738 tevent_req_done(req
);
4741 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4742 size_t *pnum_eas
, struct ea_struct
**peas
)
4744 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4745 req
, struct cli_get_ea_list_path_state
);
4748 if (tevent_req_is_nterror(req
, &status
)) {
4751 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4753 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4755 return NT_STATUS_OK
;
4758 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4761 struct ea_struct
**pea_list
)
4763 TALLOC_CTX
*frame
= NULL
;
4764 struct tevent_context
*ev
= NULL
;
4765 struct tevent_req
*req
= NULL
;
4766 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4768 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4769 return cli_smb2_get_ea_list_path(cli
,
4776 frame
= talloc_stackframe();
4778 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4780 * Can't use sync call while an async call is in flight
4782 status
= NT_STATUS_INVALID_PARAMETER
;
4785 ev
= samba_tevent_context_init(frame
);
4789 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4793 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4796 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4802 /****************************************************************************
4803 Convert open "flags" arg to uint32_t on wire.
4804 ****************************************************************************/
4806 static uint32_t open_flags_to_wire(int flags
)
4808 int open_mode
= flags
& O_ACCMODE
;
4811 switch (open_mode
) {
4813 ret
|= SMB_O_WRONLY
;
4820 ret
|= SMB_O_RDONLY
;
4824 if (flags
& O_CREAT
) {
4827 if (flags
& O_EXCL
) {
4830 if (flags
& O_TRUNC
) {
4834 if (flags
& O_SYNC
) {
4838 if (flags
& O_APPEND
) {
4839 ret
|= SMB_O_APPEND
;
4841 #if defined(O_DIRECT)
4842 if (flags
& O_DIRECT
) {
4843 ret
|= SMB_O_DIRECT
;
4846 #if defined(O_DIRECTORY)
4847 if (flags
& O_DIRECTORY
) {
4848 ret
|= SMB_O_DIRECTORY
;
4854 /****************************************************************************
4855 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4856 ****************************************************************************/
4858 struct posix_open_state
{
4862 uint16_t fnum
; /* Out */
4865 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4867 struct tevent_req
*req
= tevent_req_callback_data(
4868 subreq
, struct tevent_req
);
4869 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4874 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4875 NULL
, 0, NULL
, &data
, 12, &num_data
);
4876 TALLOC_FREE(subreq
);
4877 if (tevent_req_nterror(req
, status
)) {
4880 state
->fnum
= SVAL(data
,2);
4881 tevent_req_done(req
);
4884 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4885 struct tevent_context
*ev
,
4886 struct cli_state
*cli
,
4892 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4893 struct posix_open_state
*state
= NULL
;
4894 uint32_t wire_flags
= open_flags_to_wire(flags
);
4896 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4901 /* Setup setup word. */
4902 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4904 /* Setup param array. */
4905 state
->param
= talloc_array(state
, uint8_t, 6);
4906 if (tevent_req_nomem(state
->param
, req
)) {
4907 return tevent_req_post(req
, ev
);
4909 memset(state
->param
, '\0', 6);
4910 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4912 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4913 strlen(fname
)+1, NULL
);
4915 if (tevent_req_nomem(state
->param
, req
)) {
4916 return tevent_req_post(req
, ev
);
4919 /* Setup data words. */
4921 wire_flags
|= SMB_O_DIRECTORY
;
4924 SIVAL(state
->data
,0,0); /* No oplock. */
4925 SIVAL(state
->data
,4,wire_flags
);
4926 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4927 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4928 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4930 subreq
= cli_trans_send(state
, /* mem ctx. */
4931 ev
, /* event ctx. */
4932 cli
, /* cli_state. */
4933 SMBtrans2
, /* cmd. */
4934 NULL
, /* pipe name. */
4938 &state
->setup
, /* setup. */
4939 1, /* num setup uint16_t words. */
4940 0, /* max returned setup. */
4941 state
->param
, /* param. */
4942 talloc_get_size(state
->param
),/* num param. */
4943 2, /* max returned param. */
4944 state
->data
, /* data. */
4946 12); /* max returned data. */
4948 if (tevent_req_nomem(subreq
, req
)) {
4949 return tevent_req_post(req
, ev
);
4951 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4955 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4956 struct tevent_context
*ev
,
4957 struct cli_state
*cli
,
4962 return cli_posix_open_internal_send(mem_ctx
, ev
,
4963 cli
, fname
, flags
, mode
, false);
4966 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4968 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4971 if (tevent_req_is_nterror(req
, &status
)) {
4974 *pfnum
= state
->fnum
;
4975 return NT_STATUS_OK
;
4978 /****************************************************************************
4979 Open - POSIX semantics. Doesn't request oplock.
4980 ****************************************************************************/
4982 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4983 int flags
, mode_t mode
, uint16_t *pfnum
)
4986 TALLOC_CTX
*frame
= talloc_stackframe();
4987 struct tevent_context
*ev
= NULL
;
4988 struct tevent_req
*req
= NULL
;
4989 NTSTATUS status
= NT_STATUS_OK
;
4991 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4993 * Can't use sync call while an async call is in flight
4995 status
= NT_STATUS_INVALID_PARAMETER
;
4999 ev
= samba_tevent_context_init(frame
);
5001 status
= NT_STATUS_NO_MEMORY
;
5005 req
= cli_posix_open_send(frame
,
5012 status
= NT_STATUS_NO_MEMORY
;
5016 if (!tevent_req_poll(req
, ev
)) {
5017 status
= map_nt_error_from_unix(errno
);
5021 status
= cli_posix_open_recv(req
, pfnum
);
5028 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5029 struct tevent_context
*ev
,
5030 struct cli_state
*cli
,
5034 return cli_posix_open_internal_send(mem_ctx
, ev
,
5035 cli
, fname
, O_CREAT
, mode
, true);
5038 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5040 return tevent_req_simple_recv_ntstatus(req
);
5043 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5045 TALLOC_CTX
*frame
= talloc_stackframe();
5046 struct tevent_context
*ev
= NULL
;
5047 struct tevent_req
*req
= NULL
;
5048 NTSTATUS status
= NT_STATUS_OK
;
5050 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5052 * Can't use sync call while an async call is in flight
5054 status
= NT_STATUS_INVALID_PARAMETER
;
5058 ev
= samba_tevent_context_init(frame
);
5060 status
= NT_STATUS_NO_MEMORY
;
5064 req
= cli_posix_mkdir_send(frame
,
5070 status
= NT_STATUS_NO_MEMORY
;
5074 if (!tevent_req_poll(req
, ev
)) {
5075 status
= map_nt_error_from_unix(errno
);
5079 status
= cli_posix_mkdir_recv(req
);
5086 /****************************************************************************
5087 unlink or rmdir - POSIX semantics.
5088 ****************************************************************************/
5090 struct cli_posix_unlink_internal_state
{
5094 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5096 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5097 struct tevent_context
*ev
,
5098 struct cli_state
*cli
,
5102 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5103 struct cli_posix_unlink_internal_state
*state
= NULL
;
5105 req
= tevent_req_create(mem_ctx
, &state
,
5106 struct cli_posix_unlink_internal_state
);
5111 /* Setup data word. */
5112 SSVAL(state
->data
, 0, level
);
5114 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5115 SMB_POSIX_PATH_UNLINK
,
5117 state
->data
, sizeof(state
->data
));
5118 if (tevent_req_nomem(subreq
, req
)) {
5119 return tevent_req_post(req
, ev
);
5121 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5125 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5127 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5128 tevent_req_simple_finish_ntstatus(subreq
, status
);
5131 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5132 struct tevent_context
*ev
,
5133 struct cli_state
*cli
,
5136 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5137 SMB_POSIX_UNLINK_FILE_TARGET
);
5140 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5142 return tevent_req_simple_recv_ntstatus(req
);
5145 /****************************************************************************
5146 unlink - POSIX semantics.
5147 ****************************************************************************/
5149 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5151 TALLOC_CTX
*frame
= talloc_stackframe();
5152 struct tevent_context
*ev
= NULL
;
5153 struct tevent_req
*req
= NULL
;
5154 NTSTATUS status
= NT_STATUS_OK
;
5156 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5158 * Can't use sync call while an async call is in flight
5160 status
= NT_STATUS_INVALID_PARAMETER
;
5164 ev
= samba_tevent_context_init(frame
);
5166 status
= NT_STATUS_NO_MEMORY
;
5170 req
= cli_posix_unlink_send(frame
,
5175 status
= NT_STATUS_NO_MEMORY
;
5179 if (!tevent_req_poll(req
, ev
)) {
5180 status
= map_nt_error_from_unix(errno
);
5184 status
= cli_posix_unlink_recv(req
);
5191 /****************************************************************************
5192 rmdir - POSIX semantics.
5193 ****************************************************************************/
5195 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5196 struct tevent_context
*ev
,
5197 struct cli_state
*cli
,
5200 return cli_posix_unlink_internal_send(
5201 mem_ctx
, ev
, cli
, fname
,
5202 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5205 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5207 return tevent_req_simple_recv_ntstatus(req
);
5210 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5212 TALLOC_CTX
*frame
= talloc_stackframe();
5213 struct tevent_context
*ev
= NULL
;
5214 struct tevent_req
*req
= NULL
;
5215 NTSTATUS status
= NT_STATUS_OK
;
5217 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5219 * Can't use sync call while an async call is in flight
5221 status
= NT_STATUS_INVALID_PARAMETER
;
5225 ev
= samba_tevent_context_init(frame
);
5227 status
= NT_STATUS_NO_MEMORY
;
5231 req
= cli_posix_rmdir_send(frame
,
5236 status
= NT_STATUS_NO_MEMORY
;
5240 if (!tevent_req_poll(req
, ev
)) {
5241 status
= map_nt_error_from_unix(errno
);
5245 status
= cli_posix_rmdir_recv(req
, frame
);
5252 /****************************************************************************
5254 ****************************************************************************/
5256 struct cli_notify_state
{
5258 uint32_t num_changes
;
5259 struct notify_change
*changes
;
5262 static void cli_notify_done(struct tevent_req
*subreq
);
5264 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5265 struct tevent_context
*ev
,
5266 struct cli_state
*cli
, uint16_t fnum
,
5267 uint32_t buffer_size
,
5268 uint32_t completion_filter
, bool recursive
)
5270 struct tevent_req
*req
, *subreq
;
5271 struct cli_notify_state
*state
;
5272 unsigned old_timeout
;
5274 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5279 SIVAL(state
->setup
, 0, completion_filter
);
5280 SSVAL(state
->setup
, 4, fnum
);
5281 SSVAL(state
->setup
, 6, recursive
);
5284 * Notifies should not time out
5286 old_timeout
= cli_set_timeout(cli
, 0);
5288 subreq
= cli_trans_send(
5289 state
, /* mem ctx. */
5290 ev
, /* event ctx. */
5291 cli
, /* cli_state. */
5292 SMBnttrans
, /* cmd. */
5293 NULL
, /* pipe name. */
5295 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5297 (uint16_t *)state
->setup
, /* setup. */
5298 4, /* num setup uint16_t words. */
5299 0, /* max returned setup. */
5302 buffer_size
, /* max returned param. */
5305 0); /* max returned data. */
5307 cli_set_timeout(cli
, old_timeout
);
5309 if (tevent_req_nomem(subreq
, req
)) {
5310 return tevent_req_post(req
, ev
);
5312 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5316 static void cli_notify_done(struct tevent_req
*subreq
)
5318 struct tevent_req
*req
= tevent_req_callback_data(
5319 subreq
, struct tevent_req
);
5320 struct cli_notify_state
*state
= tevent_req_data(
5321 req
, struct cli_notify_state
);
5324 uint32_t i
, ofs
, num_params
;
5327 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5328 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5329 TALLOC_FREE(subreq
);
5330 if (tevent_req_nterror(req
, status
)) {
5331 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5335 state
->num_changes
= 0;
5338 while (num_params
- ofs
> 12) {
5339 uint32_t next
= IVAL(params
, ofs
);
5340 state
->num_changes
+= 1;
5342 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5348 state
->changes
= talloc_array(state
, struct notify_change
,
5349 state
->num_changes
);
5350 if (tevent_req_nomem(state
->changes
, req
)) {
5351 TALLOC_FREE(params
);
5357 for (i
=0; i
<state
->num_changes
; i
++) {
5358 uint32_t next
= IVAL(params
, ofs
);
5359 uint32_t len
= IVAL(params
, ofs
+8);
5363 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5364 TALLOC_FREE(params
);
5366 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5370 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5371 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5372 &name
, params
+ofs
+12, len
,
5373 STR_TERMINATE
|STR_UNICODE
);
5375 TALLOC_FREE(params
);
5376 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5379 state
->changes
[i
].name
= name
;
5383 TALLOC_FREE(params
);
5384 tevent_req_done(req
);
5387 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5388 uint32_t *pnum_changes
,
5389 struct notify_change
**pchanges
)
5391 struct cli_notify_state
*state
= tevent_req_data(
5392 req
, struct cli_notify_state
);
5395 if (tevent_req_is_nterror(req
, &status
)) {
5399 *pnum_changes
= state
->num_changes
;
5400 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5401 return NT_STATUS_OK
;
5404 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5405 uint32_t completion_filter
, bool recursive
,
5406 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5407 struct notify_change
**pchanges
)
5409 TALLOC_CTX
*frame
= talloc_stackframe();
5410 struct tevent_context
*ev
;
5411 struct tevent_req
*req
;
5412 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5414 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5416 * Can't use sync call while an async call is in flight
5418 status
= NT_STATUS_INVALID_PARAMETER
;
5421 ev
= samba_tevent_context_init(frame
);
5425 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5426 completion_filter
, recursive
);
5430 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5433 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5439 struct cli_qpathinfo_state
{
5448 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5450 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5451 struct tevent_context
*ev
,
5452 struct cli_state
*cli
, const char *fname
,
5453 uint16_t level
, uint32_t min_rdata
,
5456 struct tevent_req
*req
, *subreq
;
5457 struct cli_qpathinfo_state
*state
;
5459 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5463 state
->min_rdata
= min_rdata
;
5464 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5466 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5467 if (tevent_req_nomem(state
->param
, req
)) {
5468 return tevent_req_post(req
, ev
);
5470 SSVAL(state
->param
, 0, level
);
5471 state
->param
= trans2_bytes_push_str(
5472 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5473 if (tevent_req_nomem(state
->param
, req
)) {
5474 return tevent_req_post(req
, ev
);
5477 subreq
= cli_trans_send(
5478 state
, /* mem ctx. */
5479 ev
, /* event ctx. */
5480 cli
, /* cli_state. */
5481 SMBtrans2
, /* cmd. */
5482 NULL
, /* pipe name. */
5486 state
->setup
, /* setup. */
5487 1, /* num setup uint16_t words. */
5488 0, /* max returned setup. */
5489 state
->param
, /* param. */
5490 talloc_get_size(state
->param
), /* num param. */
5491 2, /* max returned param. */
5494 max_rdata
); /* max returned data. */
5496 if (tevent_req_nomem(subreq
, req
)) {
5497 return tevent_req_post(req
, ev
);
5499 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5503 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5505 struct tevent_req
*req
= tevent_req_callback_data(
5506 subreq
, struct tevent_req
);
5507 struct cli_qpathinfo_state
*state
= tevent_req_data(
5508 req
, struct cli_qpathinfo_state
);
5511 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5513 &state
->rdata
, state
->min_rdata
,
5515 if (tevent_req_nterror(req
, status
)) {
5518 tevent_req_done(req
);
5521 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5522 uint8_t **rdata
, uint32_t *num_rdata
)
5524 struct cli_qpathinfo_state
*state
= tevent_req_data(
5525 req
, struct cli_qpathinfo_state
);
5528 if (tevent_req_is_nterror(req
, &status
)) {
5531 if (rdata
!= NULL
) {
5532 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5534 TALLOC_FREE(state
->rdata
);
5536 if (num_rdata
!= NULL
) {
5537 *num_rdata
= state
->num_rdata
;
5539 return NT_STATUS_OK
;
5542 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5543 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5545 uint8_t **rdata
, uint32_t *num_rdata
)
5547 TALLOC_CTX
*frame
= talloc_stackframe();
5548 struct tevent_context
*ev
;
5549 struct tevent_req
*req
;
5550 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5552 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5554 * Can't use sync call while an async call is in flight
5556 status
= NT_STATUS_INVALID_PARAMETER
;
5559 ev
= samba_tevent_context_init(frame
);
5563 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5568 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5571 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5577 struct cli_qfileinfo_state
{
5581 uint16_t recv_flags2
;
5587 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5589 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5590 struct tevent_context
*ev
,
5591 struct cli_state
*cli
, uint16_t fnum
,
5592 uint16_t level
, uint32_t min_rdata
,
5595 struct tevent_req
*req
, *subreq
;
5596 struct cli_qfileinfo_state
*state
;
5598 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5602 state
->min_rdata
= min_rdata
;
5603 SSVAL(state
->param
, 0, fnum
);
5604 SSVAL(state
->param
, 2, level
);
5605 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5607 subreq
= cli_trans_send(
5608 state
, /* mem ctx. */
5609 ev
, /* event ctx. */
5610 cli
, /* cli_state. */
5611 SMBtrans2
, /* cmd. */
5612 NULL
, /* pipe name. */
5616 state
->setup
, /* setup. */
5617 1, /* num setup uint16_t words. */
5618 0, /* max returned setup. */
5619 state
->param
, /* param. */
5620 sizeof(state
->param
), /* num param. */
5621 2, /* max returned param. */
5624 max_rdata
); /* max returned data. */
5626 if (tevent_req_nomem(subreq
, req
)) {
5627 return tevent_req_post(req
, ev
);
5629 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5633 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5635 struct tevent_req
*req
= tevent_req_callback_data(
5636 subreq
, struct tevent_req
);
5637 struct cli_qfileinfo_state
*state
= tevent_req_data(
5638 req
, struct cli_qfileinfo_state
);
5641 status
= cli_trans_recv(subreq
, state
,
5642 &state
->recv_flags2
,
5645 &state
->rdata
, state
->min_rdata
,
5647 if (tevent_req_nterror(req
, status
)) {
5650 tevent_req_done(req
);
5653 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5654 uint16_t *recv_flags2
,
5655 uint8_t **rdata
, uint32_t *num_rdata
)
5657 struct cli_qfileinfo_state
*state
= tevent_req_data(
5658 req
, struct cli_qfileinfo_state
);
5661 if (tevent_req_is_nterror(req
, &status
)) {
5665 if (recv_flags2
!= NULL
) {
5666 *recv_flags2
= state
->recv_flags2
;
5668 if (rdata
!= NULL
) {
5669 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5671 TALLOC_FREE(state
->rdata
);
5673 if (num_rdata
!= NULL
) {
5674 *num_rdata
= state
->num_rdata
;
5676 return NT_STATUS_OK
;
5679 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5680 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5681 uint32_t max_rdata
, uint16_t *recv_flags2
,
5682 uint8_t **rdata
, uint32_t *num_rdata
)
5684 TALLOC_CTX
*frame
= talloc_stackframe();
5685 struct tevent_context
*ev
;
5686 struct tevent_req
*req
;
5687 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5689 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5691 * Can't use sync call while an async call is in flight
5693 status
= NT_STATUS_INVALID_PARAMETER
;
5696 ev
= samba_tevent_context_init(frame
);
5700 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5705 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5708 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5714 struct cli_flush_state
{
5718 static void cli_flush_done(struct tevent_req
*subreq
);
5720 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5721 struct tevent_context
*ev
,
5722 struct cli_state
*cli
,
5725 struct tevent_req
*req
, *subreq
;
5726 struct cli_flush_state
*state
;
5728 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5732 SSVAL(state
->vwv
+ 0, 0, fnum
);
5734 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5736 if (tevent_req_nomem(subreq
, req
)) {
5737 return tevent_req_post(req
, ev
);
5739 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5743 static void cli_flush_done(struct tevent_req
*subreq
)
5745 struct tevent_req
*req
= tevent_req_callback_data(
5746 subreq
, struct tevent_req
);
5749 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5750 TALLOC_FREE(subreq
);
5751 if (tevent_req_nterror(req
, status
)) {
5754 tevent_req_done(req
);
5757 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5759 return tevent_req_simple_recv_ntstatus(req
);
5762 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5764 TALLOC_CTX
*frame
= talloc_stackframe();
5765 struct tevent_context
*ev
;
5766 struct tevent_req
*req
;
5767 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5769 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5771 * Can't use sync call while an async call is in flight
5773 status
= NT_STATUS_INVALID_PARAMETER
;
5776 ev
= samba_tevent_context_init(frame
);
5780 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5784 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5787 status
= cli_flush_recv(req
);
5793 struct cli_shadow_copy_data_state
{
5800 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5802 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5803 struct tevent_context
*ev
,
5804 struct cli_state
*cli
,
5808 struct tevent_req
*req
, *subreq
;
5809 struct cli_shadow_copy_data_state
*state
;
5812 req
= tevent_req_create(mem_ctx
, &state
,
5813 struct cli_shadow_copy_data_state
);
5817 state
->get_names
= get_names
;
5818 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5820 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5821 SSVAL(state
->setup
+ 2, 0, fnum
);
5822 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5823 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5825 subreq
= cli_trans_send(
5826 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5827 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5830 if (tevent_req_nomem(subreq
, req
)) {
5831 return tevent_req_post(req
, ev
);
5833 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5837 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5839 struct tevent_req
*req
= tevent_req_callback_data(
5840 subreq
, struct tevent_req
);
5841 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5842 req
, struct cli_shadow_copy_data_state
);
5845 status
= cli_trans_recv(subreq
, state
, NULL
,
5846 NULL
, 0, NULL
, /* setup */
5847 NULL
, 0, NULL
, /* param */
5848 &state
->data
, 12, &state
->num_data
);
5849 TALLOC_FREE(subreq
);
5850 if (tevent_req_nterror(req
, status
)) {
5853 tevent_req_done(req
);
5856 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5857 char ***pnames
, int *pnum_names
)
5859 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5860 req
, struct cli_shadow_copy_data_state
);
5866 if (tevent_req_is_nterror(req
, &status
)) {
5869 num_names
= IVAL(state
->data
, 4);
5870 dlength
= IVAL(state
->data
, 8);
5872 if (!state
->get_names
) {
5873 *pnum_names
= num_names
;
5874 return NT_STATUS_OK
;
5877 if (dlength
+12 > state
->num_data
) {
5878 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5880 names
= talloc_array(mem_ctx
, char *, num_names
);
5881 if (names
== NULL
) {
5882 return NT_STATUS_NO_MEMORY
;
5885 for (i
=0; i
<num_names
; i
++) {
5888 size_t converted_size
;
5890 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5891 ret
= convert_string_talloc(
5892 names
, CH_UTF16LE
, CH_UNIX
,
5893 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5894 &names
[i
], &converted_size
);
5897 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5900 *pnum_names
= num_names
;
5902 return NT_STATUS_OK
;
5905 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5906 uint16_t fnum
, bool get_names
,
5907 char ***pnames
, int *pnum_names
)
5909 TALLOC_CTX
*frame
= talloc_stackframe();
5910 struct tevent_context
*ev
;
5911 struct tevent_req
*req
;
5912 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5914 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5916 * Can't use sync call while an async call is in flight
5918 status
= NT_STATUS_INVALID_PARAMETER
;
5921 ev
= samba_tevent_context_init(frame
);
5925 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5929 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5932 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);