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
;
1799 struct tevent_req
*subreq
;
1802 static void cli_ntcreate1_done(struct tevent_req
*subreq
);
1803 static bool cli_ntcreate1_cancel(struct tevent_req
*req
);
1805 static struct tevent_req
*cli_ntcreate1_send(TALLOC_CTX
*mem_ctx
,
1806 struct tevent_context
*ev
,
1807 struct cli_state
*cli
,
1809 uint32_t CreatFlags
,
1810 uint32_t DesiredAccess
,
1811 uint32_t FileAttributes
,
1812 uint32_t ShareAccess
,
1813 uint32_t CreateDisposition
,
1814 uint32_t CreateOptions
,
1815 uint8_t SecurityFlags
)
1817 struct tevent_req
*req
, *subreq
;
1818 struct cli_ntcreate1_state
*state
;
1821 size_t converted_len
;
1823 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate1_state
);
1830 SCVAL(vwv
+0, 0, 0xFF);
1835 if (cli
->use_oplocks
) {
1836 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1838 SIVAL(vwv
+3, 1, CreatFlags
);
1839 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1840 SIVAL(vwv
+7, 1, DesiredAccess
);
1841 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1842 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1843 SIVAL(vwv
+13, 1, FileAttributes
);
1844 SIVAL(vwv
+15, 1, ShareAccess
);
1845 SIVAL(vwv
+17, 1, CreateDisposition
);
1846 SIVAL(vwv
+19, 1, CreateOptions
|
1847 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1848 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1849 SCVAL(vwv
+23, 1, SecurityFlags
);
1851 bytes
= talloc_array(state
, uint8_t, 0);
1852 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1853 fname
, strlen(fname
)+1,
1856 /* sigh. this copes with broken netapp filer behaviour */
1857 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1859 if (tevent_req_nomem(bytes
, req
)) {
1860 return tevent_req_post(req
, ev
);
1863 SSVAL(vwv
+2, 1, converted_len
);
1865 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1866 talloc_get_size(bytes
), bytes
);
1867 if (tevent_req_nomem(subreq
, req
)) {
1868 return tevent_req_post(req
, ev
);
1870 tevent_req_set_callback(subreq
, cli_ntcreate1_done
, req
);
1872 state
->subreq
= subreq
;
1873 tevent_req_set_cancel_fn(req
, cli_ntcreate1_cancel
);
1878 static void cli_ntcreate1_done(struct tevent_req
*subreq
)
1880 struct tevent_req
*req
= tevent_req_callback_data(
1881 subreq
, struct tevent_req
);
1882 struct cli_ntcreate1_state
*state
= tevent_req_data(
1883 req
, struct cli_ntcreate1_state
);
1890 status
= cli_smb_recv(subreq
, state
, NULL
, 34, &wct
, &vwv
,
1891 &num_bytes
, &bytes
);
1892 TALLOC_FREE(subreq
);
1893 if (tevent_req_nterror(req
, status
)) {
1896 state
->cr
.oplock_level
= CVAL(vwv
+2, 0);
1897 state
->fnum
= SVAL(vwv
+2, 1);
1898 state
->cr
.create_action
= IVAL(vwv
+3, 1);
1899 state
->cr
.creation_time
= BVAL(vwv
+5, 1);
1900 state
->cr
.last_access_time
= BVAL(vwv
+9, 1);
1901 state
->cr
.last_write_time
= BVAL(vwv
+13, 1);
1902 state
->cr
.change_time
= BVAL(vwv
+17, 1);
1903 state
->cr
.file_attributes
= IVAL(vwv
+21, 1);
1904 state
->cr
.allocation_size
= BVAL(vwv
+23, 1);
1905 state
->cr
.end_of_file
= BVAL(vwv
+27, 1);
1907 tevent_req_done(req
);
1910 static bool cli_ntcreate1_cancel(struct tevent_req
*req
)
1912 struct cli_ntcreate1_state
*state
= tevent_req_data(
1913 req
, struct cli_ntcreate1_state
);
1914 return tevent_req_cancel(state
->subreq
);
1917 static NTSTATUS
cli_ntcreate1_recv(struct tevent_req
*req
,
1919 struct smb_create_returns
*cr
)
1921 struct cli_ntcreate1_state
*state
= tevent_req_data(
1922 req
, struct cli_ntcreate1_state
);
1925 if (tevent_req_is_nterror(req
, &status
)) {
1928 *pfnum
= state
->fnum
;
1932 return NT_STATUS_OK
;
1935 struct cli_ntcreate_state
{
1936 NTSTATUS (*recv
)(struct tevent_req
*req
, uint16_t *fnum
,
1937 struct smb_create_returns
*cr
);
1938 struct smb_create_returns cr
;
1940 struct tevent_req
*subreq
;
1943 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1944 static bool cli_ntcreate_cancel(struct tevent_req
*req
);
1946 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1947 struct tevent_context
*ev
,
1948 struct cli_state
*cli
,
1950 uint32_t create_flags
,
1951 uint32_t desired_access
,
1952 uint32_t file_attributes
,
1953 uint32_t share_access
,
1954 uint32_t create_disposition
,
1955 uint32_t create_options
,
1956 uint8_t security_flags
)
1958 struct tevent_req
*req
, *subreq
;
1959 struct cli_ntcreate_state
*state
;
1961 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1966 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1967 state
->recv
= cli_smb2_create_fnum_recv
;
1969 if (cli
->use_oplocks
) {
1970 create_flags
|= REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
;
1973 subreq
= cli_smb2_create_fnum_send(
1974 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1975 file_attributes
, share_access
, create_disposition
,
1978 state
->recv
= cli_ntcreate1_recv
;
1979 subreq
= cli_ntcreate1_send(
1980 state
, ev
, cli
, fname
, create_flags
, desired_access
,
1981 file_attributes
, share_access
, create_disposition
,
1982 create_options
, security_flags
);
1984 if (tevent_req_nomem(subreq
, req
)) {
1985 return tevent_req_post(req
, ev
);
1987 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1989 state
->subreq
= subreq
;
1990 tevent_req_set_cancel_fn(req
, cli_ntcreate_cancel
);
1995 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1997 struct tevent_req
*req
= tevent_req_callback_data(
1998 subreq
, struct tevent_req
);
1999 struct cli_ntcreate_state
*state
= tevent_req_data(
2000 req
, struct cli_ntcreate_state
);
2003 status
= state
->recv(subreq
, &state
->fnum
, &state
->cr
);
2004 TALLOC_FREE(subreq
);
2005 if (tevent_req_nterror(req
, status
)) {
2008 tevent_req_done(req
);
2011 static bool cli_ntcreate_cancel(struct tevent_req
*req
)
2013 struct cli_ntcreate_state
*state
= tevent_req_data(
2014 req
, struct cli_ntcreate_state
);
2015 return tevent_req_cancel(state
->subreq
);
2018 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *fnum
,
2019 struct smb_create_returns
*cr
)
2021 struct cli_ntcreate_state
*state
= tevent_req_data(
2022 req
, struct cli_ntcreate_state
);
2025 if (tevent_req_is_nterror(req
, &status
)) {
2029 *fnum
= state
->fnum
;
2034 return NT_STATUS_OK
;
2037 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2039 uint32_t CreatFlags
,
2040 uint32_t DesiredAccess
,
2041 uint32_t FileAttributes
,
2042 uint32_t ShareAccess
,
2043 uint32_t CreateDisposition
,
2044 uint32_t CreateOptions
,
2045 uint8_t SecurityFlags
,
2047 struct smb_create_returns
*cr
)
2049 TALLOC_CTX
*frame
= talloc_stackframe();
2050 struct tevent_context
*ev
;
2051 struct tevent_req
*req
;
2052 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2054 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2056 * Can't use sync call while an async call is in flight
2058 status
= NT_STATUS_INVALID_PARAMETER
;
2062 ev
= samba_tevent_context_init(frame
);
2067 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2068 DesiredAccess
, FileAttributes
, ShareAccess
,
2069 CreateDisposition
, CreateOptions
,
2075 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2079 status
= cli_ntcreate_recv(req
, pfid
, cr
);
2085 struct cli_nttrans_create_state
{
2087 struct smb_create_returns cr
;
2090 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
2092 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
2093 struct tevent_context
*ev
,
2094 struct cli_state
*cli
,
2096 uint32_t CreatFlags
,
2097 uint32_t DesiredAccess
,
2098 uint32_t FileAttributes
,
2099 uint32_t ShareAccess
,
2100 uint32_t CreateDisposition
,
2101 uint32_t CreateOptions
,
2102 uint8_t SecurityFlags
,
2103 struct security_descriptor
*secdesc
,
2104 struct ea_struct
*eas
,
2107 struct tevent_req
*req
, *subreq
;
2108 struct cli_nttrans_create_state
*state
;
2110 uint8_t *secdesc_buf
;
2113 size_t converted_len
;
2115 req
= tevent_req_create(mem_ctx
,
2116 &state
, struct cli_nttrans_create_state
);
2121 if (secdesc
!= NULL
) {
2122 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2123 &secdesc_buf
, &secdesc_len
);
2124 if (tevent_req_nterror(req
, status
)) {
2125 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2126 nt_errstr(status
)));
2127 return tevent_req_post(req
, ev
);
2138 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2139 return tevent_req_post(req
, ev
);
2142 param
= talloc_array(state
, uint8_t, 53);
2143 if (tevent_req_nomem(param
, req
)) {
2144 return tevent_req_post(req
, ev
);
2147 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2148 fname
, strlen(fname
),
2150 if (tevent_req_nomem(param
, req
)) {
2151 return tevent_req_post(req
, ev
);
2154 SIVAL(param
, 0, CreatFlags
);
2155 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2156 SIVAL(param
, 8, DesiredAccess
);
2157 SIVAL(param
, 12, 0x0); /* AllocationSize */
2158 SIVAL(param
, 16, 0x0); /* AllocationSize */
2159 SIVAL(param
, 20, FileAttributes
);
2160 SIVAL(param
, 24, ShareAccess
);
2161 SIVAL(param
, 28, CreateDisposition
);
2162 SIVAL(param
, 32, CreateOptions
|
2163 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2164 SIVAL(param
, 36, secdesc_len
);
2165 SIVAL(param
, 40, 0); /* EA length*/
2166 SIVAL(param
, 44, converted_len
);
2167 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2168 SCVAL(param
, 52, SecurityFlags
);
2170 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2171 NULL
, -1, /* name, fid */
2172 NT_TRANSACT_CREATE
, 0,
2173 NULL
, 0, 0, /* setup */
2174 param
, talloc_get_size(param
), 128, /* param */
2175 secdesc_buf
, secdesc_len
, 0); /* data */
2176 if (tevent_req_nomem(subreq
, req
)) {
2177 return tevent_req_post(req
, ev
);
2179 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2183 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2185 struct tevent_req
*req
= tevent_req_callback_data(
2186 subreq
, struct tevent_req
);
2187 struct cli_nttrans_create_state
*state
= tevent_req_data(
2188 req
, struct cli_nttrans_create_state
);
2193 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2194 NULL
, 0, NULL
, /* rsetup */
2195 ¶m
, 69, &num_param
,
2197 if (tevent_req_nterror(req
, status
)) {
2200 state
->cr
.oplock_level
= CVAL(param
, 0);
2201 state
->fnum
= SVAL(param
, 2);
2202 state
->cr
.create_action
= IVAL(param
, 4);
2203 state
->cr
.creation_time
= BVAL(param
, 12);
2204 state
->cr
.last_access_time
= BVAL(param
, 20);
2205 state
->cr
.last_write_time
= BVAL(param
, 28);
2206 state
->cr
.change_time
= BVAL(param
, 36);
2207 state
->cr
.file_attributes
= IVAL(param
, 44);
2208 state
->cr
.allocation_size
= BVAL(param
, 48);
2209 state
->cr
.end_of_file
= BVAL(param
, 56);
2212 tevent_req_done(req
);
2215 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2217 struct smb_create_returns
*cr
)
2219 struct cli_nttrans_create_state
*state
= tevent_req_data(
2220 req
, struct cli_nttrans_create_state
);
2223 if (tevent_req_is_nterror(req
, &status
)) {
2226 *fnum
= state
->fnum
;
2230 return NT_STATUS_OK
;
2233 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2235 uint32_t CreatFlags
,
2236 uint32_t DesiredAccess
,
2237 uint32_t FileAttributes
,
2238 uint32_t ShareAccess
,
2239 uint32_t CreateDisposition
,
2240 uint32_t CreateOptions
,
2241 uint8_t SecurityFlags
,
2242 struct security_descriptor
*secdesc
,
2243 struct ea_struct
*eas
,
2246 struct smb_create_returns
*cr
)
2248 TALLOC_CTX
*frame
= talloc_stackframe();
2249 struct tevent_context
*ev
;
2250 struct tevent_req
*req
;
2251 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2253 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2255 * Can't use sync call while an async call is in flight
2257 status
= NT_STATUS_INVALID_PARAMETER
;
2260 ev
= samba_tevent_context_init(frame
);
2264 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2265 DesiredAccess
, FileAttributes
,
2266 ShareAccess
, CreateDisposition
,
2267 CreateOptions
, SecurityFlags
,
2268 secdesc
, eas
, num_eas
);
2272 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2275 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2281 /****************************************************************************
2283 WARNING: if you open with O_WRONLY then getattrE won't work!
2284 ****************************************************************************/
2286 struct cli_openx_state
{
2293 static void cli_openx_done(struct tevent_req
*subreq
);
2295 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2296 struct tevent_context
*ev
,
2297 struct cli_state
*cli
, const char *fname
,
2298 int flags
, int share_mode
,
2299 struct tevent_req
**psmbreq
)
2301 struct tevent_req
*req
, *subreq
;
2302 struct cli_openx_state
*state
;
2304 unsigned accessmode
;
2305 uint8_t additional_flags
;
2308 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2314 if (flags
& O_CREAT
) {
2317 if (!(flags
& O_EXCL
)) {
2318 if (flags
& O_TRUNC
)
2324 accessmode
= (share_mode
<<4);
2326 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2328 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2333 if ((flags
& O_SYNC
) == O_SYNC
) {
2334 accessmode
|= (1<<14);
2338 if (share_mode
== DENY_FCB
) {
2342 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2343 SCVAL(state
->vwv
+ 0, 1, 0);
2344 SSVAL(state
->vwv
+ 1, 0, 0);
2345 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2346 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2347 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2348 SSVAL(state
->vwv
+ 5, 0, 0);
2349 SIVAL(state
->vwv
+ 6, 0, 0);
2350 SSVAL(state
->vwv
+ 8, 0, openfn
);
2351 SIVAL(state
->vwv
+ 9, 0, 0);
2352 SIVAL(state
->vwv
+ 11, 0, 0);
2353 SIVAL(state
->vwv
+ 13, 0, 0);
2355 additional_flags
= 0;
2357 if (cli
->use_oplocks
) {
2358 /* if using oplocks then ask for a batch oplock via
2359 core and extended methods */
2361 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2362 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2365 bytes
= talloc_array(state
, uint8_t, 0);
2366 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2367 strlen(fname
)+1, NULL
);
2369 if (tevent_req_nomem(bytes
, req
)) {
2370 return tevent_req_post(req
, ev
);
2373 state
->bytes
.iov_base
= (void *)bytes
;
2374 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2376 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2377 15, state
->vwv
, 1, &state
->bytes
);
2378 if (subreq
== NULL
) {
2382 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2387 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2388 struct cli_state
*cli
, const char *fname
,
2389 int flags
, int share_mode
)
2391 struct tevent_req
*req
, *subreq
;
2394 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2400 status
= smb1cli_req_chain_submit(&subreq
, 1);
2401 if (tevent_req_nterror(req
, status
)) {
2402 return tevent_req_post(req
, ev
);
2407 static void cli_openx_done(struct tevent_req
*subreq
)
2409 struct tevent_req
*req
= tevent_req_callback_data(
2410 subreq
, struct tevent_req
);
2411 struct cli_openx_state
*state
= tevent_req_data(
2412 req
, struct cli_openx_state
);
2417 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2419 TALLOC_FREE(subreq
);
2420 if (tevent_req_nterror(req
, status
)) {
2423 state
->fnum
= SVAL(vwv
+2, 0);
2424 tevent_req_done(req
);
2427 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2429 struct cli_openx_state
*state
= tevent_req_data(
2430 req
, struct cli_openx_state
);
2433 if (tevent_req_is_nterror(req
, &status
)) {
2436 *pfnum
= state
->fnum
;
2437 return NT_STATUS_OK
;
2440 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2441 int share_mode
, uint16_t *pfnum
)
2443 TALLOC_CTX
*frame
= talloc_stackframe();
2444 struct tevent_context
*ev
;
2445 struct tevent_req
*req
;
2446 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2448 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2450 * Can't use sync call while an async call is in flight
2452 status
= NT_STATUS_INVALID_PARAMETER
;
2456 ev
= samba_tevent_context_init(frame
);
2461 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2466 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2470 status
= cli_openx_recv(req
, pfnum
);
2475 /****************************************************************************
2476 Synchronous wrapper function that does an NtCreateX open by preference
2477 and falls back to openX if this fails.
2478 ****************************************************************************/
2480 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2481 int share_mode_in
, uint16_t *pfnum
)
2484 unsigned int openfn
= 0;
2485 unsigned int dos_deny
= 0;
2486 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2487 struct smb_create_returns cr
;
2489 /* Do the initial mapping into OpenX parameters. */
2490 if (flags
& O_CREAT
) {
2493 if (!(flags
& O_EXCL
)) {
2494 if (flags
& O_TRUNC
)
2500 dos_deny
= (share_mode_in
<<4);
2502 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2504 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2509 if ((flags
& O_SYNC
) == O_SYNC
) {
2510 dos_deny
|= (1<<14);
2514 if (share_mode_in
== DENY_FCB
) {
2519 /* Hmmm. This is what I think the above code
2520 should look like if it's using the constants
2523 if (flags
& O_CREAT
) {
2524 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2526 if (!(flags
& O_EXCL
)) {
2527 if (flags
& O_TRUNC
)
2528 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2530 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2533 dos_deny
= SET_DENY_MODE(share_mode_in
);
2535 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2536 dos_deny
|= DOS_OPEN_RDWR
;
2537 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2538 dos_deny
|= DOS_OPEN_WRONLY
;
2542 if ((flags
& O_SYNC
) == O_SYNC
) {
2543 dos_deny
|= FILE_SYNC_OPENMODE
;
2547 if (share_mode_in
== DENY_FCB
) {
2552 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2553 openfn
, &access_mask
,
2554 &share_mode
, &create_disposition
,
2555 &create_options
, NULL
)) {
2559 status
= cli_ntcreate(cli
,
2571 /* Try and cope will all varients of "we don't do this call"
2572 and fall back to openX. */
2574 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2575 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2576 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2577 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2578 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2579 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2580 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2581 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2582 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2586 if (NT_STATUS_IS_OK(status
) &&
2587 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2588 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2591 * Some (broken) servers return a valid handle
2592 * for directories even if FILE_NON_DIRECTORY_FILE
2593 * is set. Just close the handle and set the
2594 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2596 status
= cli_close(cli
, *pfnum
);
2597 if (!NT_STATUS_IS_OK(status
)) {
2600 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2601 /* Set this so libsmbclient can retrieve it. */
2602 cli
->raw_status
= status
;
2609 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2612 /****************************************************************************
2614 ****************************************************************************/
2616 struct cli_close_state
{
2620 static void cli_close_done(struct tevent_req
*subreq
);
2622 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2623 struct tevent_context
*ev
,
2624 struct cli_state
*cli
,
2626 struct tevent_req
**psubreq
)
2628 struct tevent_req
*req
, *subreq
;
2629 struct cli_close_state
*state
;
2631 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2636 SSVAL(state
->vwv
+0, 0, fnum
);
2637 SIVALS(state
->vwv
+1, 0, -1);
2639 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2641 if (subreq
== NULL
) {
2645 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2650 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2651 struct tevent_context
*ev
,
2652 struct cli_state
*cli
,
2655 struct tevent_req
*req
, *subreq
;
2658 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2663 status
= smb1cli_req_chain_submit(&subreq
, 1);
2664 if (tevent_req_nterror(req
, status
)) {
2665 return tevent_req_post(req
, ev
);
2670 static void cli_close_done(struct tevent_req
*subreq
)
2672 struct tevent_req
*req
= tevent_req_callback_data(
2673 subreq
, struct tevent_req
);
2676 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2677 TALLOC_FREE(subreq
);
2678 if (tevent_req_nterror(req
, status
)) {
2681 tevent_req_done(req
);
2684 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2686 return tevent_req_simple_recv_ntstatus(req
);
2689 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2691 TALLOC_CTX
*frame
= NULL
;
2692 struct tevent_context
*ev
;
2693 struct tevent_req
*req
;
2694 NTSTATUS status
= NT_STATUS_OK
;
2696 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2697 return cli_smb2_close_fnum(cli
, fnum
);
2700 frame
= talloc_stackframe();
2702 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2704 * Can't use sync call while an async call is in flight
2706 status
= NT_STATUS_INVALID_PARAMETER
;
2710 ev
= samba_tevent_context_init(frame
);
2712 status
= NT_STATUS_NO_MEMORY
;
2716 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2718 status
= NT_STATUS_NO_MEMORY
;
2722 if (!tevent_req_poll(req
, ev
)) {
2723 status
= map_nt_error_from_unix(errno
);
2727 status
= cli_close_recv(req
);
2733 /****************************************************************************
2734 Truncate a file to a specified size
2735 ****************************************************************************/
2737 struct ftrunc_state
{
2743 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2745 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2746 NULL
, 0, NULL
, NULL
, 0, NULL
);
2747 tevent_req_simple_finish_ntstatus(subreq
, status
);
2750 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2751 struct tevent_context
*ev
,
2752 struct cli_state
*cli
,
2756 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2757 struct ftrunc_state
*state
= NULL
;
2759 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2764 /* Setup setup word. */
2765 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2767 /* Setup param array. */
2768 SSVAL(state
->param
,0,fnum
);
2769 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2770 SSVAL(state
->param
,4,0);
2772 /* Setup data array. */
2773 SBVAL(state
->data
, 0, size
);
2775 subreq
= cli_trans_send(state
, /* mem ctx. */
2776 ev
, /* event ctx. */
2777 cli
, /* cli_state. */
2778 SMBtrans2
, /* cmd. */
2779 NULL
, /* pipe name. */
2783 &state
->setup
, /* setup. */
2784 1, /* num setup uint16_t words. */
2785 0, /* max returned setup. */
2786 state
->param
, /* param. */
2788 2, /* max returned param. */
2789 state
->data
, /* data. */
2791 0); /* max returned data. */
2793 if (tevent_req_nomem(subreq
, req
)) {
2794 return tevent_req_post(req
, ev
);
2796 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2800 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2802 return tevent_req_simple_recv_ntstatus(req
);
2805 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2807 TALLOC_CTX
*frame
= talloc_stackframe();
2808 struct tevent_context
*ev
= NULL
;
2809 struct tevent_req
*req
= NULL
;
2810 NTSTATUS status
= NT_STATUS_OK
;
2812 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2814 * Can't use sync call while an async call is in flight
2816 status
= NT_STATUS_INVALID_PARAMETER
;
2820 ev
= samba_tevent_context_init(frame
);
2822 status
= NT_STATUS_NO_MEMORY
;
2826 req
= cli_ftruncate_send(frame
,
2832 status
= NT_STATUS_NO_MEMORY
;
2836 if (!tevent_req_poll(req
, ev
)) {
2837 status
= map_nt_error_from_unix(errno
);
2841 status
= cli_ftruncate_recv(req
);
2848 /****************************************************************************
2849 send a lock with a specified locktype
2850 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2851 ****************************************************************************/
2853 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2854 uint32_t offset
, uint32_t len
,
2855 int timeout
, unsigned char locktype
)
2860 unsigned int set_timeout
= 0;
2861 unsigned int saved_timeout
= 0;
2863 SCVAL(vwv
+ 0, 0, 0xff);
2864 SCVAL(vwv
+ 0, 1, 0);
2865 SSVAL(vwv
+ 1, 0, 0);
2866 SSVAL(vwv
+ 2, 0, fnum
);
2867 SCVAL(vwv
+ 3, 0, locktype
);
2868 SCVAL(vwv
+ 3, 1, 0);
2869 SIVALS(vwv
+ 4, 0, timeout
);
2870 SSVAL(vwv
+ 6, 0, 0);
2871 SSVAL(vwv
+ 7, 0, 1);
2873 SSVAL(bytes
, 0, cli_getpid(cli
));
2874 SIVAL(bytes
, 2, offset
);
2875 SIVAL(bytes
, 6, len
);
2878 if (timeout
== -1) {
2879 set_timeout
= 0x7FFFFFFF;
2881 set_timeout
= timeout
+ 2*1000;
2883 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2886 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2887 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2889 if (saved_timeout
!= 0) {
2890 cli_set_timeout(cli
, saved_timeout
);
2896 /****************************************************************************
2898 note that timeout is in units of 2 milliseconds
2899 ****************************************************************************/
2901 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2902 uint32_t offset
, uint32_t len
, int timeout
,
2903 enum brl_type lock_type
)
2907 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2908 (lock_type
== READ_LOCK
? 1 : 0));
2912 /****************************************************************************
2914 ****************************************************************************/
2916 struct cli_unlock_state
{
2921 static void cli_unlock_done(struct tevent_req
*subreq
);
2923 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2924 struct tevent_context
*ev
,
2925 struct cli_state
*cli
,
2931 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2932 struct cli_unlock_state
*state
= NULL
;
2933 uint8_t additional_flags
= 0;
2935 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2940 SCVAL(state
->vwv
+0, 0, 0xFF);
2941 SSVAL(state
->vwv
+2, 0, fnum
);
2942 SCVAL(state
->vwv
+3, 0, 0);
2943 SIVALS(state
->vwv
+4, 0, 0);
2944 SSVAL(state
->vwv
+6, 0, 1);
2945 SSVAL(state
->vwv
+7, 0, 0);
2947 SSVAL(state
->data
, 0, cli_getpid(cli
));
2948 SIVAL(state
->data
, 2, offset
);
2949 SIVAL(state
->data
, 6, len
);
2951 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2952 8, state
->vwv
, 10, state
->data
);
2953 if (tevent_req_nomem(subreq
, req
)) {
2954 return tevent_req_post(req
, ev
);
2956 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2960 static void cli_unlock_done(struct tevent_req
*subreq
)
2962 struct tevent_req
*req
= tevent_req_callback_data(
2963 subreq
, struct tevent_req
);
2966 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2967 TALLOC_FREE(subreq
);
2968 if (tevent_req_nterror(req
, status
)) {
2971 tevent_req_done(req
);
2974 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2976 return tevent_req_simple_recv_ntstatus(req
);
2979 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2984 TALLOC_CTX
*frame
= talloc_stackframe();
2985 struct tevent_context
*ev
;
2986 struct tevent_req
*req
;
2987 NTSTATUS status
= NT_STATUS_OK
;
2989 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2991 * Can't use sync call while an async call is in flight
2993 status
= NT_STATUS_INVALID_PARAMETER
;
2997 ev
= samba_tevent_context_init(frame
);
2999 status
= NT_STATUS_NO_MEMORY
;
3003 req
= cli_unlock_send(frame
, ev
, cli
,
3006 status
= NT_STATUS_NO_MEMORY
;
3010 if (!tevent_req_poll(req
, ev
)) {
3011 status
= map_nt_error_from_unix(errno
);
3015 status
= cli_unlock_recv(req
);
3022 /****************************************************************************
3023 Lock a file with 64 bit offsets.
3024 ****************************************************************************/
3026 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
3027 uint64_t offset
, uint64_t len
, int timeout
,
3028 enum brl_type lock_type
)
3032 unsigned int set_timeout
= 0;
3033 unsigned int saved_timeout
= 0;
3037 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3038 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
3041 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
3042 ltype
|= LOCKING_ANDX_LARGE_FILES
;
3044 SCVAL(vwv
+ 0, 0, 0xff);
3045 SCVAL(vwv
+ 0, 1, 0);
3046 SSVAL(vwv
+ 1, 0, 0);
3047 SSVAL(vwv
+ 2, 0, fnum
);
3048 SCVAL(vwv
+ 3, 0, ltype
);
3049 SCVAL(vwv
+ 3, 1, 0);
3050 SIVALS(vwv
+ 4, 0, timeout
);
3051 SSVAL(vwv
+ 6, 0, 0);
3052 SSVAL(vwv
+ 7, 0, 1);
3054 SIVAL(bytes
, 0, cli_getpid(cli
));
3055 SOFF_T_R(bytes
, 4, offset
);
3056 SOFF_T_R(bytes
, 12, len
);
3059 if (timeout
== -1) {
3060 set_timeout
= 0x7FFFFFFF;
3062 set_timeout
= timeout
+ 2*1000;
3064 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
3067 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
3068 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3070 if (saved_timeout
!= 0) {
3071 cli_set_timeout(cli
, saved_timeout
);
3077 /****************************************************************************
3078 Unlock a file with 64 bit offsets.
3079 ****************************************************************************/
3081 struct cli_unlock64_state
{
3086 static void cli_unlock64_done(struct tevent_req
*subreq
);
3088 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
3089 struct tevent_context
*ev
,
3090 struct cli_state
*cli
,
3096 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3097 struct cli_unlock64_state
*state
= NULL
;
3098 uint8_t additional_flags
= 0;
3100 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3105 SCVAL(state
->vwv
+0, 0, 0xff);
3106 SSVAL(state
->vwv
+2, 0, fnum
);
3107 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3108 SIVALS(state
->vwv
+4, 0, 0);
3109 SSVAL(state
->vwv
+6, 0, 1);
3110 SSVAL(state
->vwv
+7, 0, 0);
3112 SIVAL(state
->data
, 0, cli_getpid(cli
));
3113 SOFF_T_R(state
->data
, 4, offset
);
3114 SOFF_T_R(state
->data
, 12, len
);
3116 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
3117 8, state
->vwv
, 20, state
->data
);
3118 if (tevent_req_nomem(subreq
, req
)) {
3119 return tevent_req_post(req
, ev
);
3121 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3125 static void cli_unlock64_done(struct tevent_req
*subreq
)
3127 struct tevent_req
*req
= tevent_req_callback_data(
3128 subreq
, struct tevent_req
);
3131 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3132 TALLOC_FREE(subreq
);
3133 if (tevent_req_nterror(req
, status
)) {
3136 tevent_req_done(req
);
3139 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3141 return tevent_req_simple_recv_ntstatus(req
);
3144 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3149 TALLOC_CTX
*frame
= talloc_stackframe();
3150 struct tevent_context
*ev
;
3151 struct tevent_req
*req
;
3152 NTSTATUS status
= NT_STATUS_OK
;
3154 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3155 return cli_unlock(cli
, fnum
, offset
, len
);
3158 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3160 * Can't use sync call while an async call is in flight
3162 status
= NT_STATUS_INVALID_PARAMETER
;
3166 ev
= samba_tevent_context_init(frame
);
3168 status
= NT_STATUS_NO_MEMORY
;
3172 req
= cli_unlock64_send(frame
, ev
, cli
,
3175 status
= NT_STATUS_NO_MEMORY
;
3179 if (!tevent_req_poll(req
, ev
)) {
3180 status
= map_nt_error_from_unix(errno
);
3184 status
= cli_unlock64_recv(req
);
3191 /****************************************************************************
3192 Get/unlock a POSIX lock on a file - internal function.
3193 ****************************************************************************/
3195 struct posix_lock_state
{
3198 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3201 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3203 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3204 NULL
, 0, NULL
, NULL
, 0, NULL
);
3205 tevent_req_simple_finish_ntstatus(subreq
, status
);
3208 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3209 struct tevent_context
*ev
,
3210 struct cli_state
*cli
,
3215 enum brl_type lock_type
)
3217 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3218 struct posix_lock_state
*state
= NULL
;
3220 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3225 /* Setup setup word. */
3226 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3228 /* Setup param array. */
3229 SSVAL(&state
->param
, 0, fnum
);
3230 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3232 /* Setup data array. */
3233 switch (lock_type
) {
3235 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3236 POSIX_LOCK_TYPE_READ
);
3239 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3240 POSIX_LOCK_TYPE_WRITE
);
3243 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3244 POSIX_LOCK_TYPE_UNLOCK
);
3251 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3252 POSIX_LOCK_FLAG_WAIT
);
3254 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3255 POSIX_LOCK_FLAG_NOWAIT
);
3258 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3259 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3260 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3262 subreq
= cli_trans_send(state
, /* mem ctx. */
3263 ev
, /* event ctx. */
3264 cli
, /* cli_state. */
3265 SMBtrans2
, /* cmd. */
3266 NULL
, /* pipe name. */
3270 &state
->setup
, /* setup. */
3271 1, /* num setup uint16_t words. */
3272 0, /* max returned setup. */
3273 state
->param
, /* param. */
3275 2, /* max returned param. */
3276 state
->data
, /* data. */
3277 POSIX_LOCK_DATA_SIZE
, /* num data. */
3278 0); /* max returned data. */
3280 if (tevent_req_nomem(subreq
, req
)) {
3281 return tevent_req_post(req
, ev
);
3283 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3287 /****************************************************************************
3289 ****************************************************************************/
3291 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3292 struct tevent_context
*ev
,
3293 struct cli_state
*cli
,
3298 enum brl_type lock_type
)
3300 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3301 wait_lock
, lock_type
);
3304 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3306 return tevent_req_simple_recv_ntstatus(req
);
3309 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3310 uint64_t offset
, uint64_t len
,
3311 bool wait_lock
, enum brl_type lock_type
)
3313 TALLOC_CTX
*frame
= talloc_stackframe();
3314 struct tevent_context
*ev
= NULL
;
3315 struct tevent_req
*req
= NULL
;
3316 NTSTATUS status
= NT_STATUS_OK
;
3318 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3320 * Can't use sync call while an async call is in flight
3322 status
= NT_STATUS_INVALID_PARAMETER
;
3326 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3327 status
= NT_STATUS_INVALID_PARAMETER
;
3331 ev
= samba_tevent_context_init(frame
);
3333 status
= NT_STATUS_NO_MEMORY
;
3337 req
= cli_posix_lock_send(frame
,
3346 status
= NT_STATUS_NO_MEMORY
;
3350 if (!tevent_req_poll(req
, ev
)) {
3351 status
= map_nt_error_from_unix(errno
);
3355 status
= cli_posix_lock_recv(req
);
3362 /****************************************************************************
3363 POSIX Unlock a file.
3364 ****************************************************************************/
3366 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3367 struct tevent_context
*ev
,
3368 struct cli_state
*cli
,
3373 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3374 false, UNLOCK_LOCK
);
3377 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3379 return tevent_req_simple_recv_ntstatus(req
);
3382 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3384 TALLOC_CTX
*frame
= talloc_stackframe();
3385 struct tevent_context
*ev
= NULL
;
3386 struct tevent_req
*req
= NULL
;
3387 NTSTATUS status
= NT_STATUS_OK
;
3389 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3391 * Can't use sync call while an async call is in flight
3393 status
= NT_STATUS_INVALID_PARAMETER
;
3397 ev
= samba_tevent_context_init(frame
);
3399 status
= NT_STATUS_NO_MEMORY
;
3403 req
= cli_posix_unlock_send(frame
,
3410 status
= NT_STATUS_NO_MEMORY
;
3414 if (!tevent_req_poll(req
, ev
)) {
3415 status
= map_nt_error_from_unix(errno
);
3419 status
= cli_posix_unlock_recv(req
);
3426 /****************************************************************************
3427 Do a SMBgetattrE call.
3428 ****************************************************************************/
3430 static void cli_getattrE_done(struct tevent_req
*subreq
);
3432 struct cli_getattrE_state
{
3442 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3443 struct tevent_context
*ev
,
3444 struct cli_state
*cli
,
3447 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3448 struct cli_getattrE_state
*state
= NULL
;
3449 uint8_t additional_flags
= 0;
3451 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3456 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3457 SSVAL(state
->vwv
+0,0,fnum
);
3459 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3460 1, state
->vwv
, 0, NULL
);
3461 if (tevent_req_nomem(subreq
, req
)) {
3462 return tevent_req_post(req
, ev
);
3464 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3468 static void cli_getattrE_done(struct tevent_req
*subreq
)
3470 struct tevent_req
*req
= tevent_req_callback_data(
3471 subreq
, struct tevent_req
);
3472 struct cli_getattrE_state
*state
= tevent_req_data(
3473 req
, struct cli_getattrE_state
);
3475 uint16_t *vwv
= NULL
;
3478 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3480 TALLOC_FREE(subreq
);
3481 if (tevent_req_nterror(req
, status
)) {
3485 state
->size
= (off_t
)IVAL(vwv
+6,0);
3486 state
->attr
= SVAL(vwv
+10,0);
3487 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3488 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3489 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3491 tevent_req_done(req
);
3494 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3497 time_t *change_time
,
3498 time_t *access_time
,
3501 struct cli_getattrE_state
*state
= tevent_req_data(
3502 req
, struct cli_getattrE_state
);
3505 if (tevent_req_is_nterror(req
, &status
)) {
3509 *attr
= state
->attr
;
3512 *size
= state
->size
;
3515 *change_time
= state
->change_time
;
3518 *access_time
= state
->access_time
;
3521 *write_time
= state
->write_time
;
3523 return NT_STATUS_OK
;
3526 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3530 time_t *change_time
,
3531 time_t *access_time
,
3534 TALLOC_CTX
*frame
= NULL
;
3535 struct tevent_context
*ev
= NULL
;
3536 struct tevent_req
*req
= NULL
;
3537 NTSTATUS status
= NT_STATUS_OK
;
3539 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3540 return cli_smb2_getattrE(cli
,
3549 frame
= talloc_stackframe();
3551 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3553 * Can't use sync call while an async call is in flight
3555 status
= NT_STATUS_INVALID_PARAMETER
;
3559 ev
= samba_tevent_context_init(frame
);
3561 status
= NT_STATUS_NO_MEMORY
;
3565 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3567 status
= NT_STATUS_NO_MEMORY
;
3571 if (!tevent_req_poll(req
, ev
)) {
3572 status
= map_nt_error_from_unix(errno
);
3576 status
= cli_getattrE_recv(req
,
3588 /****************************************************************************
3590 ****************************************************************************/
3592 static void cli_getatr_done(struct tevent_req
*subreq
);
3594 struct cli_getatr_state
{
3601 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3602 struct tevent_context
*ev
,
3603 struct cli_state
*cli
,
3606 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3607 struct cli_getatr_state
*state
= NULL
;
3608 uint8_t additional_flags
= 0;
3609 uint8_t *bytes
= NULL
;
3611 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3616 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3618 bytes
= talloc_array(state
, uint8_t, 1);
3619 if (tevent_req_nomem(bytes
, req
)) {
3620 return tevent_req_post(req
, ev
);
3623 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3624 strlen(fname
)+1, NULL
);
3626 if (tevent_req_nomem(bytes
, req
)) {
3627 return tevent_req_post(req
, ev
);
3630 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3631 0, NULL
, talloc_get_size(bytes
), bytes
);
3632 if (tevent_req_nomem(subreq
, req
)) {
3633 return tevent_req_post(req
, ev
);
3635 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3639 static void cli_getatr_done(struct tevent_req
*subreq
)
3641 struct tevent_req
*req
= tevent_req_callback_data(
3642 subreq
, struct tevent_req
);
3643 struct cli_getatr_state
*state
= tevent_req_data(
3644 req
, struct cli_getatr_state
);
3646 uint16_t *vwv
= NULL
;
3649 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3651 TALLOC_FREE(subreq
);
3652 if (tevent_req_nterror(req
, status
)) {
3656 state
->attr
= SVAL(vwv
+0,0);
3657 state
->size
= (off_t
)IVAL(vwv
+3,0);
3658 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3660 tevent_req_done(req
);
3663 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3668 struct cli_getatr_state
*state
= tevent_req_data(
3669 req
, struct cli_getatr_state
);
3672 if (tevent_req_is_nterror(req
, &status
)) {
3676 *attr
= state
->attr
;
3679 *size
= state
->size
;
3682 *write_time
= state
->write_time
;
3684 return NT_STATUS_OK
;
3687 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3693 TALLOC_CTX
*frame
= NULL
;
3694 struct tevent_context
*ev
= NULL
;
3695 struct tevent_req
*req
= NULL
;
3696 NTSTATUS status
= NT_STATUS_OK
;
3698 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3699 return cli_smb2_getatr(cli
,
3706 frame
= talloc_stackframe();
3708 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3710 * Can't use sync call while an async call is in flight
3712 status
= NT_STATUS_INVALID_PARAMETER
;
3716 ev
= samba_tevent_context_init(frame
);
3718 status
= NT_STATUS_NO_MEMORY
;
3722 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3724 status
= NT_STATUS_NO_MEMORY
;
3728 if (!tevent_req_poll(req
, ev
)) {
3729 status
= map_nt_error_from_unix(errno
);
3733 status
= cli_getatr_recv(req
,
3743 /****************************************************************************
3744 Do a SMBsetattrE call.
3745 ****************************************************************************/
3747 static void cli_setattrE_done(struct tevent_req
*subreq
);
3749 struct cli_setattrE_state
{
3753 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3754 struct tevent_context
*ev
,
3755 struct cli_state
*cli
,
3761 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3762 struct cli_setattrE_state
*state
= NULL
;
3763 uint8_t additional_flags
= 0;
3765 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3770 SSVAL(state
->vwv
+0, 0, fnum
);
3771 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3772 smb1cli_conn_server_time_zone(cli
->conn
));
3773 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3774 smb1cli_conn_server_time_zone(cli
->conn
));
3775 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3776 smb1cli_conn_server_time_zone(cli
->conn
));
3778 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3779 7, state
->vwv
, 0, NULL
);
3780 if (tevent_req_nomem(subreq
, req
)) {
3781 return tevent_req_post(req
, ev
);
3783 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3787 static void cli_setattrE_done(struct tevent_req
*subreq
)
3789 struct tevent_req
*req
= tevent_req_callback_data(
3790 subreq
, struct tevent_req
);
3793 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3794 TALLOC_FREE(subreq
);
3795 if (tevent_req_nterror(req
, status
)) {
3798 tevent_req_done(req
);
3801 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3803 return tevent_req_simple_recv_ntstatus(req
);
3806 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3812 TALLOC_CTX
*frame
= NULL
;
3813 struct tevent_context
*ev
= NULL
;
3814 struct tevent_req
*req
= NULL
;
3815 NTSTATUS status
= NT_STATUS_OK
;
3817 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3818 return cli_smb2_setattrE(cli
,
3825 frame
= talloc_stackframe();
3827 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3829 * Can't use sync call while an async call is in flight
3831 status
= NT_STATUS_INVALID_PARAMETER
;
3835 ev
= samba_tevent_context_init(frame
);
3837 status
= NT_STATUS_NO_MEMORY
;
3841 req
= cli_setattrE_send(frame
, ev
,
3849 status
= NT_STATUS_NO_MEMORY
;
3853 if (!tevent_req_poll(req
, ev
)) {
3854 status
= map_nt_error_from_unix(errno
);
3858 status
= cli_setattrE_recv(req
);
3865 /****************************************************************************
3866 Do a SMBsetatr call.
3867 ****************************************************************************/
3869 static void cli_setatr_done(struct tevent_req
*subreq
);
3871 struct cli_setatr_state
{
3875 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3876 struct tevent_context
*ev
,
3877 struct cli_state
*cli
,
3882 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3883 struct cli_setatr_state
*state
= NULL
;
3884 uint8_t additional_flags
= 0;
3885 uint8_t *bytes
= NULL
;
3887 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3892 SSVAL(state
->vwv
+0, 0, attr
);
3893 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3895 bytes
= talloc_array(state
, uint8_t, 1);
3896 if (tevent_req_nomem(bytes
, req
)) {
3897 return tevent_req_post(req
, ev
);
3900 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3901 strlen(fname
)+1, NULL
);
3902 if (tevent_req_nomem(bytes
, req
)) {
3903 return tevent_req_post(req
, ev
);
3905 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3906 talloc_get_size(bytes
)+1);
3907 if (tevent_req_nomem(bytes
, req
)) {
3908 return tevent_req_post(req
, ev
);
3911 bytes
[talloc_get_size(bytes
)-1] = 4;
3912 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3914 if (tevent_req_nomem(bytes
, req
)) {
3915 return tevent_req_post(req
, ev
);
3918 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3919 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3920 if (tevent_req_nomem(subreq
, req
)) {
3921 return tevent_req_post(req
, ev
);
3923 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3927 static void cli_setatr_done(struct tevent_req
*subreq
)
3929 struct tevent_req
*req
= tevent_req_callback_data(
3930 subreq
, struct tevent_req
);
3933 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3934 TALLOC_FREE(subreq
);
3935 if (tevent_req_nterror(req
, status
)) {
3938 tevent_req_done(req
);
3941 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3943 return tevent_req_simple_recv_ntstatus(req
);
3946 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3951 TALLOC_CTX
*frame
= NULL
;
3952 struct tevent_context
*ev
= NULL
;
3953 struct tevent_req
*req
= NULL
;
3954 NTSTATUS status
= NT_STATUS_OK
;
3956 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3957 return cli_smb2_setatr(cli
,
3963 frame
= talloc_stackframe();
3965 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3967 * Can't use sync call while an async call is in flight
3969 status
= NT_STATUS_INVALID_PARAMETER
;
3973 ev
= samba_tevent_context_init(frame
);
3975 status
= NT_STATUS_NO_MEMORY
;
3979 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3981 status
= NT_STATUS_NO_MEMORY
;
3985 if (!tevent_req_poll(req
, ev
)) {
3986 status
= map_nt_error_from_unix(errno
);
3990 status
= cli_setatr_recv(req
);
3997 /****************************************************************************
3998 Check for existance of a dir.
3999 ****************************************************************************/
4001 static void cli_chkpath_done(struct tevent_req
*subreq
);
4003 struct cli_chkpath_state
{
4007 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
4008 struct tevent_context
*ev
,
4009 struct cli_state
*cli
,
4012 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4013 struct cli_chkpath_state
*state
= NULL
;
4014 uint8_t additional_flags
= 0;
4015 uint8_t *bytes
= NULL
;
4017 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
4022 bytes
= talloc_array(state
, uint8_t, 1);
4023 if (tevent_req_nomem(bytes
, req
)) {
4024 return tevent_req_post(req
, ev
);
4027 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4028 strlen(fname
)+1, NULL
);
4030 if (tevent_req_nomem(bytes
, req
)) {
4031 return tevent_req_post(req
, ev
);
4034 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
4035 0, NULL
, talloc_get_size(bytes
), bytes
);
4036 if (tevent_req_nomem(subreq
, req
)) {
4037 return tevent_req_post(req
, ev
);
4039 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
4043 static void cli_chkpath_done(struct tevent_req
*subreq
)
4045 struct tevent_req
*req
= tevent_req_callback_data(
4046 subreq
, struct tevent_req
);
4049 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4050 TALLOC_FREE(subreq
);
4051 if (tevent_req_nterror(req
, status
)) {
4054 tevent_req_done(req
);
4057 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
4059 return tevent_req_simple_recv_ntstatus(req
);
4062 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
4064 TALLOC_CTX
*frame
= talloc_stackframe();
4065 struct tevent_context
*ev
= NULL
;
4066 struct tevent_req
*req
= NULL
;
4068 NTSTATUS status
= NT_STATUS_OK
;
4070 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4072 * Can't use sync call while an async call is in flight
4074 status
= NT_STATUS_INVALID_PARAMETER
;
4078 path2
= talloc_strdup(frame
, path
);
4080 status
= NT_STATUS_NO_MEMORY
;
4083 trim_char(path2
,'\0','\\');
4085 path2
= talloc_strdup(frame
, "\\");
4087 status
= NT_STATUS_NO_MEMORY
;
4092 ev
= samba_tevent_context_init(frame
);
4094 status
= NT_STATUS_NO_MEMORY
;
4098 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4100 status
= NT_STATUS_NO_MEMORY
;
4104 if (!tevent_req_poll(req
, ev
)) {
4105 status
= map_nt_error_from_unix(errno
);
4109 status
= cli_chkpath_recv(req
);
4116 /****************************************************************************
4118 ****************************************************************************/
4120 static void cli_dskattr_done(struct tevent_req
*subreq
);
4122 struct cli_dskattr_state
{
4128 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4129 struct tevent_context
*ev
,
4130 struct cli_state
*cli
)
4132 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4133 struct cli_dskattr_state
*state
= NULL
;
4134 uint8_t additional_flags
= 0;
4136 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4141 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
4143 if (tevent_req_nomem(subreq
, req
)) {
4144 return tevent_req_post(req
, ev
);
4146 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4150 static void cli_dskattr_done(struct tevent_req
*subreq
)
4152 struct tevent_req
*req
= tevent_req_callback_data(
4153 subreq
, struct tevent_req
);
4154 struct cli_dskattr_state
*state
= tevent_req_data(
4155 req
, struct cli_dskattr_state
);
4157 uint16_t *vwv
= NULL
;
4160 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4162 TALLOC_FREE(subreq
);
4163 if (tevent_req_nterror(req
, status
)) {
4166 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4167 state
->total
= SVAL(vwv
+0, 0);
4168 state
->avail
= SVAL(vwv
+3, 0);
4169 tevent_req_done(req
);
4172 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4174 struct cli_dskattr_state
*state
= tevent_req_data(
4175 req
, struct cli_dskattr_state
);
4178 if (tevent_req_is_nterror(req
, &status
)) {
4181 *bsize
= state
->bsize
;
4182 *total
= state
->total
;
4183 *avail
= state
->avail
;
4184 return NT_STATUS_OK
;
4187 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4189 TALLOC_CTX
*frame
= NULL
;
4190 struct tevent_context
*ev
= NULL
;
4191 struct tevent_req
*req
= NULL
;
4192 NTSTATUS status
= NT_STATUS_OK
;
4194 frame
= talloc_stackframe();
4196 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4198 * Can't use sync call while an async call is in flight
4200 status
= NT_STATUS_INVALID_PARAMETER
;
4204 ev
= samba_tevent_context_init(frame
);
4206 status
= NT_STATUS_NO_MEMORY
;
4210 req
= cli_dskattr_send(frame
, ev
, cli
);
4212 status
= NT_STATUS_NO_MEMORY
;
4216 if (!tevent_req_poll(req
, ev
)) {
4217 status
= map_nt_error_from_unix(errno
);
4221 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4228 NTSTATUS
cli_disk_size(struct cli_state
*cli
, uint64_t *bsize
, uint64_t *total
, uint64_t *avail
)
4230 uint64_t sectors_per_block
;
4231 uint64_t bytes_per_sector
;
4232 int old_bsize
, old_total
, old_avail
;
4235 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4236 return cli_smb2_dskattr(cli
, bsize
, total
, avail
);
4240 * Try the trans2 disk full size info call first.
4241 * We already use this in SMBC_fstatvfs_ctx().
4242 * Ignore 'actual_available_units' as we only
4243 * care about the quota for the caller.
4246 status
= cli_get_fs_full_size_info(cli
,
4253 /* Try and cope will all varients of "we don't do this call"
4254 and fall back to cli_dskattr. */
4256 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
4257 NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
) ||
4258 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
4259 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
4260 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
4261 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
4262 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
4263 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
4264 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
4265 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
4269 if (!NT_STATUS_IS_OK(status
)) {
4274 *bsize
= sectors_per_block
*
4278 return NT_STATUS_OK
;
4282 /* Old SMB1 core protocol fallback. */
4283 status
= cli_dskattr(cli
, &old_bsize
, &old_total
, &old_avail
);
4284 if (!NT_STATUS_IS_OK(status
)) {
4288 *bsize
= (uint64_t)old_bsize
;
4291 *total
= (uint64_t)old_total
;
4294 *avail
= (uint64_t)old_avail
;
4296 return NT_STATUS_OK
;
4299 /****************************************************************************
4300 Create and open a temporary file.
4301 ****************************************************************************/
4303 static void cli_ctemp_done(struct tevent_req
*subreq
);
4305 struct ctemp_state
{
4311 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4312 struct tevent_context
*ev
,
4313 struct cli_state
*cli
,
4316 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4317 struct ctemp_state
*state
= NULL
;
4318 uint8_t additional_flags
= 0;
4319 uint8_t *bytes
= NULL
;
4321 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4326 SSVAL(state
->vwv
,0,0);
4327 SIVALS(state
->vwv
+1,0,-1);
4329 bytes
= talloc_array(state
, uint8_t, 1);
4330 if (tevent_req_nomem(bytes
, req
)) {
4331 return tevent_req_post(req
, ev
);
4334 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4335 strlen(path
)+1, NULL
);
4336 if (tevent_req_nomem(bytes
, req
)) {
4337 return tevent_req_post(req
, ev
);
4340 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4341 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4342 if (tevent_req_nomem(subreq
, req
)) {
4343 return tevent_req_post(req
, ev
);
4345 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4349 static void cli_ctemp_done(struct tevent_req
*subreq
)
4351 struct tevent_req
*req
= tevent_req_callback_data(
4352 subreq
, struct tevent_req
);
4353 struct ctemp_state
*state
= tevent_req_data(
4354 req
, struct ctemp_state
);
4358 uint32_t num_bytes
= 0;
4359 uint8_t *bytes
= NULL
;
4361 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4362 &num_bytes
, &bytes
);
4363 TALLOC_FREE(subreq
);
4364 if (tevent_req_nterror(req
, status
)) {
4368 state
->fnum
= SVAL(vwv
+0, 0);
4370 /* From W2K3, the result is just the ASCII name */
4371 if (num_bytes
< 2) {
4372 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4376 if (pull_string_talloc(state
,
4383 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4386 tevent_req_done(req
);
4389 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4394 struct ctemp_state
*state
= tevent_req_data(req
,
4395 struct ctemp_state
);
4398 if (tevent_req_is_nterror(req
, &status
)) {
4401 *pfnum
= state
->fnum
;
4402 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4404 return NT_STATUS_NO_MEMORY
;
4406 return NT_STATUS_OK
;
4409 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4415 TALLOC_CTX
*frame
= talloc_stackframe();
4416 struct tevent_context
*ev
;
4417 struct tevent_req
*req
;
4418 NTSTATUS status
= NT_STATUS_OK
;
4420 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4422 * Can't use sync call while an async call is in flight
4424 status
= NT_STATUS_INVALID_PARAMETER
;
4428 ev
= samba_tevent_context_init(frame
);
4430 status
= NT_STATUS_NO_MEMORY
;
4434 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4436 status
= NT_STATUS_NO_MEMORY
;
4440 if (!tevent_req_poll(req
, ev
)) {
4441 status
= map_nt_error_from_unix(errno
);
4445 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4453 send a raw ioctl - used by the torture code
4455 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4460 SSVAL(vwv
+0, 0, fnum
);
4461 SSVAL(vwv
+1, 0, code
>>16);
4462 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4464 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4465 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4466 if (!NT_STATUS_IS_OK(status
)) {
4469 *blob
= data_blob_null
;
4470 return NT_STATUS_OK
;
4473 /*********************************************************
4474 Set an extended attribute utility fn.
4475 *********************************************************/
4477 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4478 uint8_t *param
, unsigned int param_len
,
4479 const char *ea_name
,
4480 const char *ea_val
, size_t ea_len
)
4483 unsigned int data_len
= 0;
4484 uint8_t *data
= NULL
;
4486 size_t ea_namelen
= strlen(ea_name
);
4489 SSVAL(setup
, 0, setup_val
);
4491 if (ea_namelen
== 0 && ea_len
== 0) {
4493 data
= talloc_array(talloc_tos(),
4497 return NT_STATUS_NO_MEMORY
;
4500 SIVAL(p
,0,data_len
);
4502 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4503 data
= talloc_array(talloc_tos(),
4507 return NT_STATUS_NO_MEMORY
;
4510 SIVAL(p
,0,data_len
);
4512 SCVAL(p
, 0, 0); /* EA flags. */
4513 SCVAL(p
, 1, ea_namelen
);
4514 SSVAL(p
, 2, ea_len
);
4515 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4516 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4519 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4521 param
, param_len
, 2,
4522 data
, data_len
, CLI_BUFFER_SIZE
,
4524 NULL
, 0, NULL
, /* rsetup */
4525 NULL
, 0, NULL
, /* rparam */
4526 NULL
, 0, NULL
); /* rdata */
4531 /*********************************************************
4532 Set an extended attribute on a pathname.
4533 *********************************************************/
4535 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4536 const char *ea_name
, const char *ea_val
,
4539 unsigned int param_len
= 0;
4542 TALLOC_CTX
*frame
= NULL
;
4544 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4545 return cli_smb2_set_ea_path(cli
,
4552 frame
= talloc_stackframe();
4554 param
= talloc_array(frame
, uint8_t, 6);
4556 status
= NT_STATUS_NO_MEMORY
;
4559 SSVAL(param
,0,SMB_INFO_SET_EA
);
4563 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4564 path
, strlen(path
)+1,
4566 param_len
= talloc_get_size(param
);
4568 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4569 ea_name
, ea_val
, ea_len
);
4577 /*********************************************************
4578 Set an extended attribute on an fnum.
4579 *********************************************************/
4581 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4582 const char *ea_name
, const char *ea_val
,
4587 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4588 return cli_smb2_set_ea_fnum(cli
,
4595 memset(param
, 0, 6);
4596 SSVAL(param
,0,fnum
);
4597 SSVAL(param
,2,SMB_INFO_SET_EA
);
4599 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4600 ea_name
, ea_val
, ea_len
);
4603 /*********************************************************
4604 Get an extended attribute list utility fn.
4605 *********************************************************/
4607 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4609 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4611 struct ea_struct
*ea_list
= NULL
;
4616 if (rdata_len
< 4) {
4620 ea_size
= (size_t)IVAL(rdata
,0);
4621 if (ea_size
> rdata_len
) {
4626 /* No EA's present. */
4635 /* Validate the EA list and count it. */
4636 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4637 unsigned int ea_namelen
= CVAL(p
,1);
4638 unsigned int ea_valuelen
= SVAL(p
,2);
4639 if (ea_namelen
== 0) {
4642 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4645 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4646 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4655 *pnum_eas
= num_eas
;
4657 /* Caller only wants number of EA's. */
4661 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4666 ea_size
= (size_t)IVAL(rdata
,0);
4669 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4670 struct ea_struct
*ea
= &ea_list
[num_eas
];
4671 fstring unix_ea_name
;
4672 unsigned int ea_namelen
= CVAL(p
,1);
4673 unsigned int ea_valuelen
= SVAL(p
,2);
4675 ea
->flags
= CVAL(p
,0);
4676 unix_ea_name
[0] = '\0';
4677 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4678 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4682 /* Ensure the value is null terminated (in case it's a string). */
4683 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4684 if (!ea
->value
.data
) {
4688 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4690 ea
->value
.data
[ea_valuelen
] = 0;
4692 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4695 *pea_list
= ea_list
;
4699 TALLOC_FREE(ea_list
);
4703 /*********************************************************
4704 Get an extended attribute list from a pathname.
4705 *********************************************************/
4707 struct cli_get_ea_list_path_state
{
4712 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4714 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4715 struct tevent_context
*ev
,
4716 struct cli_state
*cli
,
4719 struct tevent_req
*req
, *subreq
;
4720 struct cli_get_ea_list_path_state
*state
;
4722 req
= tevent_req_create(mem_ctx
, &state
,
4723 struct cli_get_ea_list_path_state
);
4727 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4728 SMB_INFO_QUERY_ALL_EAS
, 4,
4730 if (tevent_req_nomem(subreq
, req
)) {
4731 return tevent_req_post(req
, ev
);
4733 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4737 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4739 struct tevent_req
*req
= tevent_req_callback_data(
4740 subreq
, struct tevent_req
);
4741 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4742 req
, struct cli_get_ea_list_path_state
);
4745 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4747 TALLOC_FREE(subreq
);
4748 if (tevent_req_nterror(req
, status
)) {
4751 tevent_req_done(req
);
4754 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4755 size_t *pnum_eas
, struct ea_struct
**peas
)
4757 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4758 req
, struct cli_get_ea_list_path_state
);
4761 if (tevent_req_is_nterror(req
, &status
)) {
4764 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4766 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4768 return NT_STATUS_OK
;
4771 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4774 struct ea_struct
**pea_list
)
4776 TALLOC_CTX
*frame
= NULL
;
4777 struct tevent_context
*ev
= NULL
;
4778 struct tevent_req
*req
= NULL
;
4779 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4781 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4782 return cli_smb2_get_ea_list_path(cli
,
4789 frame
= talloc_stackframe();
4791 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4793 * Can't use sync call while an async call is in flight
4795 status
= NT_STATUS_INVALID_PARAMETER
;
4798 ev
= samba_tevent_context_init(frame
);
4802 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4806 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4809 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4815 /****************************************************************************
4816 Convert open "flags" arg to uint32_t on wire.
4817 ****************************************************************************/
4819 static uint32_t open_flags_to_wire(int flags
)
4821 int open_mode
= flags
& O_ACCMODE
;
4824 switch (open_mode
) {
4826 ret
|= SMB_O_WRONLY
;
4833 ret
|= SMB_O_RDONLY
;
4837 if (flags
& O_CREAT
) {
4840 if (flags
& O_EXCL
) {
4843 if (flags
& O_TRUNC
) {
4847 if (flags
& O_SYNC
) {
4851 if (flags
& O_APPEND
) {
4852 ret
|= SMB_O_APPEND
;
4854 #if defined(O_DIRECT)
4855 if (flags
& O_DIRECT
) {
4856 ret
|= SMB_O_DIRECT
;
4859 #if defined(O_DIRECTORY)
4860 if (flags
& O_DIRECTORY
) {
4861 ret
|= SMB_O_DIRECTORY
;
4867 /****************************************************************************
4868 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4869 ****************************************************************************/
4871 struct posix_open_state
{
4875 uint16_t fnum
; /* Out */
4878 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4880 struct tevent_req
*req
= tevent_req_callback_data(
4881 subreq
, struct tevent_req
);
4882 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4887 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4888 NULL
, 0, NULL
, &data
, 12, &num_data
);
4889 TALLOC_FREE(subreq
);
4890 if (tevent_req_nterror(req
, status
)) {
4893 state
->fnum
= SVAL(data
,2);
4894 tevent_req_done(req
);
4897 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4898 struct tevent_context
*ev
,
4899 struct cli_state
*cli
,
4905 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4906 struct posix_open_state
*state
= NULL
;
4907 uint32_t wire_flags
= open_flags_to_wire(flags
);
4909 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4914 /* Setup setup word. */
4915 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4917 /* Setup param array. */
4918 state
->param
= talloc_array(state
, uint8_t, 6);
4919 if (tevent_req_nomem(state
->param
, req
)) {
4920 return tevent_req_post(req
, ev
);
4922 memset(state
->param
, '\0', 6);
4923 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4925 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4926 strlen(fname
)+1, NULL
);
4928 if (tevent_req_nomem(state
->param
, req
)) {
4929 return tevent_req_post(req
, ev
);
4932 /* Setup data words. */
4934 wire_flags
|= SMB_O_DIRECTORY
;
4937 SIVAL(state
->data
,0,0); /* No oplock. */
4938 SIVAL(state
->data
,4,wire_flags
);
4939 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4940 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4941 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4943 subreq
= cli_trans_send(state
, /* mem ctx. */
4944 ev
, /* event ctx. */
4945 cli
, /* cli_state. */
4946 SMBtrans2
, /* cmd. */
4947 NULL
, /* pipe name. */
4951 &state
->setup
, /* setup. */
4952 1, /* num setup uint16_t words. */
4953 0, /* max returned setup. */
4954 state
->param
, /* param. */
4955 talloc_get_size(state
->param
),/* num param. */
4956 2, /* max returned param. */
4957 state
->data
, /* data. */
4959 12); /* max returned data. */
4961 if (tevent_req_nomem(subreq
, req
)) {
4962 return tevent_req_post(req
, ev
);
4964 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4968 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4969 struct tevent_context
*ev
,
4970 struct cli_state
*cli
,
4975 return cli_posix_open_internal_send(mem_ctx
, ev
,
4976 cli
, fname
, flags
, mode
, false);
4979 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4981 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4984 if (tevent_req_is_nterror(req
, &status
)) {
4987 *pfnum
= state
->fnum
;
4988 return NT_STATUS_OK
;
4991 /****************************************************************************
4992 Open - POSIX semantics. Doesn't request oplock.
4993 ****************************************************************************/
4995 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4996 int flags
, mode_t mode
, uint16_t *pfnum
)
4999 TALLOC_CTX
*frame
= talloc_stackframe();
5000 struct tevent_context
*ev
= NULL
;
5001 struct tevent_req
*req
= NULL
;
5002 NTSTATUS status
= NT_STATUS_OK
;
5004 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5006 * Can't use sync call while an async call is in flight
5008 status
= NT_STATUS_INVALID_PARAMETER
;
5012 ev
= samba_tevent_context_init(frame
);
5014 status
= NT_STATUS_NO_MEMORY
;
5018 req
= cli_posix_open_send(frame
,
5025 status
= NT_STATUS_NO_MEMORY
;
5029 if (!tevent_req_poll(req
, ev
)) {
5030 status
= map_nt_error_from_unix(errno
);
5034 status
= cli_posix_open_recv(req
, pfnum
);
5041 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
5042 struct tevent_context
*ev
,
5043 struct cli_state
*cli
,
5047 return cli_posix_open_internal_send(mem_ctx
, ev
,
5048 cli
, fname
, O_CREAT
, mode
, true);
5051 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
5053 return tevent_req_simple_recv_ntstatus(req
);
5056 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
5058 TALLOC_CTX
*frame
= talloc_stackframe();
5059 struct tevent_context
*ev
= NULL
;
5060 struct tevent_req
*req
= NULL
;
5061 NTSTATUS status
= NT_STATUS_OK
;
5063 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5065 * Can't use sync call while an async call is in flight
5067 status
= NT_STATUS_INVALID_PARAMETER
;
5071 ev
= samba_tevent_context_init(frame
);
5073 status
= NT_STATUS_NO_MEMORY
;
5077 req
= cli_posix_mkdir_send(frame
,
5083 status
= NT_STATUS_NO_MEMORY
;
5087 if (!tevent_req_poll(req
, ev
)) {
5088 status
= map_nt_error_from_unix(errno
);
5092 status
= cli_posix_mkdir_recv(req
);
5099 /****************************************************************************
5100 unlink or rmdir - POSIX semantics.
5101 ****************************************************************************/
5103 struct cli_posix_unlink_internal_state
{
5107 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
5109 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
5110 struct tevent_context
*ev
,
5111 struct cli_state
*cli
,
5115 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
5116 struct cli_posix_unlink_internal_state
*state
= NULL
;
5118 req
= tevent_req_create(mem_ctx
, &state
,
5119 struct cli_posix_unlink_internal_state
);
5124 /* Setup data word. */
5125 SSVAL(state
->data
, 0, level
);
5127 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
5128 SMB_POSIX_PATH_UNLINK
,
5130 state
->data
, sizeof(state
->data
));
5131 if (tevent_req_nomem(subreq
, req
)) {
5132 return tevent_req_post(req
, ev
);
5134 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
5138 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
5140 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
5141 tevent_req_simple_finish_ntstatus(subreq
, status
);
5144 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
5145 struct tevent_context
*ev
,
5146 struct cli_state
*cli
,
5149 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
5150 SMB_POSIX_UNLINK_FILE_TARGET
);
5153 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
5155 return tevent_req_simple_recv_ntstatus(req
);
5158 /****************************************************************************
5159 unlink - POSIX semantics.
5160 ****************************************************************************/
5162 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5164 TALLOC_CTX
*frame
= talloc_stackframe();
5165 struct tevent_context
*ev
= NULL
;
5166 struct tevent_req
*req
= NULL
;
5167 NTSTATUS status
= NT_STATUS_OK
;
5169 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5171 * Can't use sync call while an async call is in flight
5173 status
= NT_STATUS_INVALID_PARAMETER
;
5177 ev
= samba_tevent_context_init(frame
);
5179 status
= NT_STATUS_NO_MEMORY
;
5183 req
= cli_posix_unlink_send(frame
,
5188 status
= NT_STATUS_NO_MEMORY
;
5192 if (!tevent_req_poll(req
, ev
)) {
5193 status
= map_nt_error_from_unix(errno
);
5197 status
= cli_posix_unlink_recv(req
);
5204 /****************************************************************************
5205 rmdir - POSIX semantics.
5206 ****************************************************************************/
5208 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5209 struct tevent_context
*ev
,
5210 struct cli_state
*cli
,
5213 return cli_posix_unlink_internal_send(
5214 mem_ctx
, ev
, cli
, fname
,
5215 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5218 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5220 return tevent_req_simple_recv_ntstatus(req
);
5223 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5225 TALLOC_CTX
*frame
= talloc_stackframe();
5226 struct tevent_context
*ev
= NULL
;
5227 struct tevent_req
*req
= NULL
;
5228 NTSTATUS status
= NT_STATUS_OK
;
5230 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5232 * Can't use sync call while an async call is in flight
5234 status
= NT_STATUS_INVALID_PARAMETER
;
5238 ev
= samba_tevent_context_init(frame
);
5240 status
= NT_STATUS_NO_MEMORY
;
5244 req
= cli_posix_rmdir_send(frame
,
5249 status
= NT_STATUS_NO_MEMORY
;
5253 if (!tevent_req_poll(req
, ev
)) {
5254 status
= map_nt_error_from_unix(errno
);
5258 status
= cli_posix_rmdir_recv(req
, frame
);
5265 /****************************************************************************
5267 ****************************************************************************/
5269 struct cli_notify_state
{
5271 uint32_t num_changes
;
5272 struct notify_change
*changes
;
5275 static void cli_notify_done(struct tevent_req
*subreq
);
5277 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5278 struct tevent_context
*ev
,
5279 struct cli_state
*cli
, uint16_t fnum
,
5280 uint32_t buffer_size
,
5281 uint32_t completion_filter
, bool recursive
)
5283 struct tevent_req
*req
, *subreq
;
5284 struct cli_notify_state
*state
;
5285 unsigned old_timeout
;
5287 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5292 SIVAL(state
->setup
, 0, completion_filter
);
5293 SSVAL(state
->setup
, 4, fnum
);
5294 SSVAL(state
->setup
, 6, recursive
);
5297 * Notifies should not time out
5299 old_timeout
= cli_set_timeout(cli
, 0);
5301 subreq
= cli_trans_send(
5302 state
, /* mem ctx. */
5303 ev
, /* event ctx. */
5304 cli
, /* cli_state. */
5305 SMBnttrans
, /* cmd. */
5306 NULL
, /* pipe name. */
5308 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5310 (uint16_t *)state
->setup
, /* setup. */
5311 4, /* num setup uint16_t words. */
5312 0, /* max returned setup. */
5315 buffer_size
, /* max returned param. */
5318 0); /* max returned data. */
5320 cli_set_timeout(cli
, old_timeout
);
5322 if (tevent_req_nomem(subreq
, req
)) {
5323 return tevent_req_post(req
, ev
);
5325 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5329 static void cli_notify_done(struct tevent_req
*subreq
)
5331 struct tevent_req
*req
= tevent_req_callback_data(
5332 subreq
, struct tevent_req
);
5333 struct cli_notify_state
*state
= tevent_req_data(
5334 req
, struct cli_notify_state
);
5337 uint32_t i
, ofs
, num_params
;
5340 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5341 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5342 TALLOC_FREE(subreq
);
5343 if (tevent_req_nterror(req
, status
)) {
5344 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5348 state
->num_changes
= 0;
5351 while (num_params
- ofs
> 12) {
5352 uint32_t next
= IVAL(params
, ofs
);
5353 state
->num_changes
+= 1;
5355 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5361 state
->changes
= talloc_array(state
, struct notify_change
,
5362 state
->num_changes
);
5363 if (tevent_req_nomem(state
->changes
, req
)) {
5364 TALLOC_FREE(params
);
5370 for (i
=0; i
<state
->num_changes
; i
++) {
5371 uint32_t next
= IVAL(params
, ofs
);
5372 uint32_t len
= IVAL(params
, ofs
+8);
5376 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5377 TALLOC_FREE(params
);
5379 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5383 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5384 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5385 &name
, params
+ofs
+12, len
,
5386 STR_TERMINATE
|STR_UNICODE
);
5388 TALLOC_FREE(params
);
5389 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5392 state
->changes
[i
].name
= name
;
5396 TALLOC_FREE(params
);
5397 tevent_req_done(req
);
5400 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5401 uint32_t *pnum_changes
,
5402 struct notify_change
**pchanges
)
5404 struct cli_notify_state
*state
= tevent_req_data(
5405 req
, struct cli_notify_state
);
5408 if (tevent_req_is_nterror(req
, &status
)) {
5412 *pnum_changes
= state
->num_changes
;
5413 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5414 return NT_STATUS_OK
;
5417 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5418 uint32_t completion_filter
, bool recursive
,
5419 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5420 struct notify_change
**pchanges
)
5422 TALLOC_CTX
*frame
= talloc_stackframe();
5423 struct tevent_context
*ev
;
5424 struct tevent_req
*req
;
5425 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5427 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5429 * Can't use sync call while an async call is in flight
5431 status
= NT_STATUS_INVALID_PARAMETER
;
5434 ev
= samba_tevent_context_init(frame
);
5438 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5439 completion_filter
, recursive
);
5443 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5446 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5452 struct cli_qpathinfo_state
{
5461 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5463 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5464 struct tevent_context
*ev
,
5465 struct cli_state
*cli
, const char *fname
,
5466 uint16_t level
, uint32_t min_rdata
,
5469 struct tevent_req
*req
, *subreq
;
5470 struct cli_qpathinfo_state
*state
;
5472 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5476 state
->min_rdata
= min_rdata
;
5477 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5479 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5480 if (tevent_req_nomem(state
->param
, req
)) {
5481 return tevent_req_post(req
, ev
);
5483 SSVAL(state
->param
, 0, level
);
5484 state
->param
= trans2_bytes_push_str(
5485 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5486 if (tevent_req_nomem(state
->param
, req
)) {
5487 return tevent_req_post(req
, ev
);
5490 subreq
= cli_trans_send(
5491 state
, /* mem ctx. */
5492 ev
, /* event ctx. */
5493 cli
, /* cli_state. */
5494 SMBtrans2
, /* cmd. */
5495 NULL
, /* pipe name. */
5499 state
->setup
, /* setup. */
5500 1, /* num setup uint16_t words. */
5501 0, /* max returned setup. */
5502 state
->param
, /* param. */
5503 talloc_get_size(state
->param
), /* num param. */
5504 2, /* max returned param. */
5507 max_rdata
); /* max returned data. */
5509 if (tevent_req_nomem(subreq
, req
)) {
5510 return tevent_req_post(req
, ev
);
5512 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5516 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5518 struct tevent_req
*req
= tevent_req_callback_data(
5519 subreq
, struct tevent_req
);
5520 struct cli_qpathinfo_state
*state
= tevent_req_data(
5521 req
, struct cli_qpathinfo_state
);
5524 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5526 &state
->rdata
, state
->min_rdata
,
5528 if (tevent_req_nterror(req
, status
)) {
5531 tevent_req_done(req
);
5534 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5535 uint8_t **rdata
, uint32_t *num_rdata
)
5537 struct cli_qpathinfo_state
*state
= tevent_req_data(
5538 req
, struct cli_qpathinfo_state
);
5541 if (tevent_req_is_nterror(req
, &status
)) {
5544 if (rdata
!= NULL
) {
5545 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5547 TALLOC_FREE(state
->rdata
);
5549 if (num_rdata
!= NULL
) {
5550 *num_rdata
= state
->num_rdata
;
5552 return NT_STATUS_OK
;
5555 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5556 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5558 uint8_t **rdata
, uint32_t *num_rdata
)
5560 TALLOC_CTX
*frame
= talloc_stackframe();
5561 struct tevent_context
*ev
;
5562 struct tevent_req
*req
;
5563 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5565 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5567 * Can't use sync call while an async call is in flight
5569 status
= NT_STATUS_INVALID_PARAMETER
;
5572 ev
= samba_tevent_context_init(frame
);
5576 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5581 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5584 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5590 struct cli_qfileinfo_state
{
5594 uint16_t recv_flags2
;
5600 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5602 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5603 struct tevent_context
*ev
,
5604 struct cli_state
*cli
, uint16_t fnum
,
5605 uint16_t level
, uint32_t min_rdata
,
5608 struct tevent_req
*req
, *subreq
;
5609 struct cli_qfileinfo_state
*state
;
5611 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5615 state
->min_rdata
= min_rdata
;
5616 SSVAL(state
->param
, 0, fnum
);
5617 SSVAL(state
->param
, 2, level
);
5618 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5620 subreq
= cli_trans_send(
5621 state
, /* mem ctx. */
5622 ev
, /* event ctx. */
5623 cli
, /* cli_state. */
5624 SMBtrans2
, /* cmd. */
5625 NULL
, /* pipe name. */
5629 state
->setup
, /* setup. */
5630 1, /* num setup uint16_t words. */
5631 0, /* max returned setup. */
5632 state
->param
, /* param. */
5633 sizeof(state
->param
), /* num param. */
5634 2, /* max returned param. */
5637 max_rdata
); /* max returned data. */
5639 if (tevent_req_nomem(subreq
, req
)) {
5640 return tevent_req_post(req
, ev
);
5642 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5646 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5648 struct tevent_req
*req
= tevent_req_callback_data(
5649 subreq
, struct tevent_req
);
5650 struct cli_qfileinfo_state
*state
= tevent_req_data(
5651 req
, struct cli_qfileinfo_state
);
5654 status
= cli_trans_recv(subreq
, state
,
5655 &state
->recv_flags2
,
5658 &state
->rdata
, state
->min_rdata
,
5660 if (tevent_req_nterror(req
, status
)) {
5663 tevent_req_done(req
);
5666 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5667 uint16_t *recv_flags2
,
5668 uint8_t **rdata
, uint32_t *num_rdata
)
5670 struct cli_qfileinfo_state
*state
= tevent_req_data(
5671 req
, struct cli_qfileinfo_state
);
5674 if (tevent_req_is_nterror(req
, &status
)) {
5678 if (recv_flags2
!= NULL
) {
5679 *recv_flags2
= state
->recv_flags2
;
5681 if (rdata
!= NULL
) {
5682 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5684 TALLOC_FREE(state
->rdata
);
5686 if (num_rdata
!= NULL
) {
5687 *num_rdata
= state
->num_rdata
;
5689 return NT_STATUS_OK
;
5692 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5693 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5694 uint32_t max_rdata
, uint16_t *recv_flags2
,
5695 uint8_t **rdata
, uint32_t *num_rdata
)
5697 TALLOC_CTX
*frame
= talloc_stackframe();
5698 struct tevent_context
*ev
;
5699 struct tevent_req
*req
;
5700 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5702 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5704 * Can't use sync call while an async call is in flight
5706 status
= NT_STATUS_INVALID_PARAMETER
;
5709 ev
= samba_tevent_context_init(frame
);
5713 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5718 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5721 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5727 struct cli_flush_state
{
5731 static void cli_flush_done(struct tevent_req
*subreq
);
5733 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5734 struct tevent_context
*ev
,
5735 struct cli_state
*cli
,
5738 struct tevent_req
*req
, *subreq
;
5739 struct cli_flush_state
*state
;
5741 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5745 SSVAL(state
->vwv
+ 0, 0, fnum
);
5747 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5749 if (tevent_req_nomem(subreq
, req
)) {
5750 return tevent_req_post(req
, ev
);
5752 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5756 static void cli_flush_done(struct tevent_req
*subreq
)
5758 struct tevent_req
*req
= tevent_req_callback_data(
5759 subreq
, struct tevent_req
);
5762 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5763 TALLOC_FREE(subreq
);
5764 if (tevent_req_nterror(req
, status
)) {
5767 tevent_req_done(req
);
5770 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5772 return tevent_req_simple_recv_ntstatus(req
);
5775 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5777 TALLOC_CTX
*frame
= talloc_stackframe();
5778 struct tevent_context
*ev
;
5779 struct tevent_req
*req
;
5780 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5782 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5784 * Can't use sync call while an async call is in flight
5786 status
= NT_STATUS_INVALID_PARAMETER
;
5789 ev
= samba_tevent_context_init(frame
);
5793 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5797 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5800 status
= cli_flush_recv(req
);
5806 struct cli_shadow_copy_data_state
{
5813 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5815 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5816 struct tevent_context
*ev
,
5817 struct cli_state
*cli
,
5821 struct tevent_req
*req
, *subreq
;
5822 struct cli_shadow_copy_data_state
*state
;
5825 req
= tevent_req_create(mem_ctx
, &state
,
5826 struct cli_shadow_copy_data_state
);
5830 state
->get_names
= get_names
;
5831 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5833 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5834 SSVAL(state
->setup
+ 2, 0, fnum
);
5835 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5836 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5838 subreq
= cli_trans_send(
5839 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5840 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5843 if (tevent_req_nomem(subreq
, req
)) {
5844 return tevent_req_post(req
, ev
);
5846 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5850 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5852 struct tevent_req
*req
= tevent_req_callback_data(
5853 subreq
, struct tevent_req
);
5854 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5855 req
, struct cli_shadow_copy_data_state
);
5858 status
= cli_trans_recv(subreq
, state
, NULL
,
5859 NULL
, 0, NULL
, /* setup */
5860 NULL
, 0, NULL
, /* param */
5861 &state
->data
, 12, &state
->num_data
);
5862 TALLOC_FREE(subreq
);
5863 if (tevent_req_nterror(req
, status
)) {
5866 tevent_req_done(req
);
5869 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5870 char ***pnames
, int *pnum_names
)
5872 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5873 req
, struct cli_shadow_copy_data_state
);
5879 if (tevent_req_is_nterror(req
, &status
)) {
5882 num_names
= IVAL(state
->data
, 4);
5883 dlength
= IVAL(state
->data
, 8);
5885 if (!state
->get_names
) {
5886 *pnum_names
= num_names
;
5887 return NT_STATUS_OK
;
5890 if (dlength
+12 > state
->num_data
) {
5891 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5893 names
= talloc_array(mem_ctx
, char *, num_names
);
5894 if (names
== NULL
) {
5895 return NT_STATUS_NO_MEMORY
;
5898 for (i
=0; i
<num_names
; i
++) {
5901 size_t converted_size
;
5903 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5904 ret
= convert_string_talloc(
5905 names
, CH_UTF16LE
, CH_UNIX
,
5906 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5907 &names
[i
], &converted_size
);
5910 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5913 *pnum_names
= num_names
;
5915 return NT_STATUS_OK
;
5918 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5919 uint16_t fnum
, bool get_names
,
5920 char ***pnames
, int *pnum_names
)
5922 TALLOC_CTX
*frame
= talloc_stackframe();
5923 struct tevent_context
*ev
;
5924 struct tevent_req
*req
;
5925 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5927 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5929 * Can't use sync call while an async call is in flight
5931 status
= NT_STATUS_INVALID_PARAMETER
;
5934 ev
= samba_tevent_context_init(frame
);
5938 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5942 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5945 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);