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_ntcreate_state
{
1798 struct smb_create_returns cr
;
1801 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1803 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1804 struct tevent_context
*ev
,
1805 struct cli_state
*cli
,
1807 uint32_t CreatFlags
,
1808 uint32_t DesiredAccess
,
1809 uint32_t FileAttributes
,
1810 uint32_t ShareAccess
,
1811 uint32_t CreateDisposition
,
1812 uint32_t CreateOptions
,
1813 uint8_t SecurityFlags
)
1815 struct tevent_req
*req
, *subreq
;
1816 struct cli_ntcreate_state
*state
;
1819 size_t converted_len
;
1821 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1828 SCVAL(vwv
+0, 0, 0xFF);
1833 if (cli
->use_oplocks
) {
1834 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1836 SIVAL(vwv
+3, 1, CreatFlags
);
1837 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1838 SIVAL(vwv
+7, 1, DesiredAccess
);
1839 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1841 SIVAL(vwv
+13, 1, FileAttributes
);
1842 SIVAL(vwv
+15, 1, ShareAccess
);
1843 SIVAL(vwv
+17, 1, CreateDisposition
);
1844 SIVAL(vwv
+19, 1, CreateOptions
|
1845 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1846 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1847 SCVAL(vwv
+23, 1, SecurityFlags
);
1849 bytes
= talloc_array(state
, uint8_t, 0);
1850 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1851 fname
, strlen(fname
)+1,
1854 /* sigh. this copes with broken netapp filer behaviour */
1855 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1857 if (tevent_req_nomem(bytes
, req
)) {
1858 return tevent_req_post(req
, ev
);
1861 SSVAL(vwv
+2, 1, converted_len
);
1863 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1864 talloc_get_size(bytes
), bytes
);
1865 if (tevent_req_nomem(subreq
, req
)) {
1866 return tevent_req_post(req
, ev
);
1868 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1872 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1874 struct tevent_req
*req
= tevent_req_callback_data(
1875 subreq
, struct tevent_req
);
1876 struct cli_ntcreate_state
*state
= tevent_req_data(
1877 req
, struct cli_ntcreate_state
);
1884 status
= cli_smb_recv(subreq
, state
, NULL
, 34, &wct
, &vwv
,
1885 &num_bytes
, &bytes
);
1886 TALLOC_FREE(subreq
);
1887 if (tevent_req_nterror(req
, status
)) {
1890 state
->cr
.oplock_level
= CVAL(vwv
+2, 0);
1891 state
->fnum
= SVAL(vwv
+2, 1);
1892 state
->cr
.create_action
= IVAL(vwv
+3, 1);
1893 state
->cr
.creation_time
= BVAL(vwv
+5, 1);
1894 state
->cr
.last_access_time
= BVAL(vwv
+9, 1);
1895 state
->cr
.last_write_time
= BVAL(vwv
+13, 1);
1896 state
->cr
.change_time
= BVAL(vwv
+17, 1);
1897 state
->cr
.file_attributes
= IVAL(vwv
+21, 1);
1898 state
->cr
.allocation_size
= BVAL(vwv
+23, 1);
1899 state
->cr
.end_of_file
= BVAL(vwv
+27, 1);
1901 tevent_req_done(req
);
1904 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
,
1906 struct smb_create_returns
*cr
)
1908 struct cli_ntcreate_state
*state
= tevent_req_data(
1909 req
, struct cli_ntcreate_state
);
1912 if (tevent_req_is_nterror(req
, &status
)) {
1915 *pfnum
= state
->fnum
;
1919 return NT_STATUS_OK
;
1922 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1924 uint32_t CreatFlags
,
1925 uint32_t DesiredAccess
,
1926 uint32_t FileAttributes
,
1927 uint32_t ShareAccess
,
1928 uint32_t CreateDisposition
,
1929 uint32_t CreateOptions
,
1930 uint8_t SecurityFlags
,
1932 struct smb_create_returns
*cr
)
1934 TALLOC_CTX
*frame
= NULL
;
1935 struct tevent_context
*ev
;
1936 struct tevent_req
*req
;
1937 NTSTATUS status
= NT_STATUS_OK
;
1939 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1940 return cli_smb2_create_fnum(cli
,
1952 frame
= talloc_stackframe();
1954 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1956 * Can't use sync call while an async call is in flight
1958 status
= NT_STATUS_INVALID_PARAMETER
;
1962 ev
= samba_tevent_context_init(frame
);
1964 status
= NT_STATUS_NO_MEMORY
;
1968 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1969 DesiredAccess
, FileAttributes
, ShareAccess
,
1970 CreateDisposition
, CreateOptions
,
1973 status
= NT_STATUS_NO_MEMORY
;
1977 if (!tevent_req_poll(req
, ev
)) {
1978 status
= map_nt_error_from_unix(errno
);
1982 status
= cli_ntcreate_recv(req
, pfid
, cr
);
1988 struct cli_nttrans_create_state
{
1990 struct smb_create_returns cr
;
1993 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1995 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1996 struct tevent_context
*ev
,
1997 struct cli_state
*cli
,
1999 uint32_t CreatFlags
,
2000 uint32_t DesiredAccess
,
2001 uint32_t FileAttributes
,
2002 uint32_t ShareAccess
,
2003 uint32_t CreateDisposition
,
2004 uint32_t CreateOptions
,
2005 uint8_t SecurityFlags
,
2006 struct security_descriptor
*secdesc
,
2007 struct ea_struct
*eas
,
2010 struct tevent_req
*req
, *subreq
;
2011 struct cli_nttrans_create_state
*state
;
2013 uint8_t *secdesc_buf
;
2016 size_t converted_len
;
2018 req
= tevent_req_create(mem_ctx
,
2019 &state
, struct cli_nttrans_create_state
);
2024 if (secdesc
!= NULL
) {
2025 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2026 &secdesc_buf
, &secdesc_len
);
2027 if (tevent_req_nterror(req
, status
)) {
2028 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2029 nt_errstr(status
)));
2030 return tevent_req_post(req
, ev
);
2041 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2042 return tevent_req_post(req
, ev
);
2045 param
= talloc_array(state
, uint8_t, 53);
2046 if (tevent_req_nomem(param
, req
)) {
2047 return tevent_req_post(req
, ev
);
2050 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2051 fname
, strlen(fname
),
2053 if (tevent_req_nomem(param
, req
)) {
2054 return tevent_req_post(req
, ev
);
2057 SIVAL(param
, 0, CreatFlags
);
2058 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2059 SIVAL(param
, 8, DesiredAccess
);
2060 SIVAL(param
, 12, 0x0); /* AllocationSize */
2061 SIVAL(param
, 16, 0x0); /* AllocationSize */
2062 SIVAL(param
, 20, FileAttributes
);
2063 SIVAL(param
, 24, ShareAccess
);
2064 SIVAL(param
, 28, CreateDisposition
);
2065 SIVAL(param
, 32, CreateOptions
|
2066 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2067 SIVAL(param
, 36, secdesc_len
);
2068 SIVAL(param
, 40, 0); /* EA length*/
2069 SIVAL(param
, 44, converted_len
);
2070 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2071 SCVAL(param
, 52, SecurityFlags
);
2073 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2074 NULL
, -1, /* name, fid */
2075 NT_TRANSACT_CREATE
, 0,
2076 NULL
, 0, 0, /* setup */
2077 param
, talloc_get_size(param
), 128, /* param */
2078 secdesc_buf
, secdesc_len
, 0); /* data */
2079 if (tevent_req_nomem(subreq
, req
)) {
2080 return tevent_req_post(req
, ev
);
2082 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2086 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2088 struct tevent_req
*req
= tevent_req_callback_data(
2089 subreq
, struct tevent_req
);
2090 struct cli_nttrans_create_state
*state
= tevent_req_data(
2091 req
, struct cli_nttrans_create_state
);
2096 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2097 NULL
, 0, NULL
, /* rsetup */
2098 ¶m
, 69, &num_param
,
2100 if (tevent_req_nterror(req
, status
)) {
2103 state
->cr
.oplock_level
= CVAL(param
, 0);
2104 state
->fnum
= SVAL(param
, 2);
2105 state
->cr
.create_action
= IVAL(param
, 4);
2106 state
->cr
.creation_time
= BVAL(param
, 12);
2107 state
->cr
.last_access_time
= BVAL(param
, 20);
2108 state
->cr
.last_write_time
= BVAL(param
, 28);
2109 state
->cr
.change_time
= BVAL(param
, 36);
2110 state
->cr
.file_attributes
= IVAL(param
, 44);
2111 state
->cr
.allocation_size
= BVAL(param
, 48);
2112 state
->cr
.end_of_file
= BVAL(param
, 56);
2115 tevent_req_done(req
);
2118 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
,
2120 struct smb_create_returns
*cr
)
2122 struct cli_nttrans_create_state
*state
= tevent_req_data(
2123 req
, struct cli_nttrans_create_state
);
2126 if (tevent_req_is_nterror(req
, &status
)) {
2129 *fnum
= state
->fnum
;
2133 return NT_STATUS_OK
;
2136 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2138 uint32_t CreatFlags
,
2139 uint32_t DesiredAccess
,
2140 uint32_t FileAttributes
,
2141 uint32_t ShareAccess
,
2142 uint32_t CreateDisposition
,
2143 uint32_t CreateOptions
,
2144 uint8_t SecurityFlags
,
2145 struct security_descriptor
*secdesc
,
2146 struct ea_struct
*eas
,
2149 struct smb_create_returns
*cr
)
2151 TALLOC_CTX
*frame
= talloc_stackframe();
2152 struct tevent_context
*ev
;
2153 struct tevent_req
*req
;
2154 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2156 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2158 * Can't use sync call while an async call is in flight
2160 status
= NT_STATUS_INVALID_PARAMETER
;
2163 ev
= samba_tevent_context_init(frame
);
2167 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2168 DesiredAccess
, FileAttributes
,
2169 ShareAccess
, CreateDisposition
,
2170 CreateOptions
, SecurityFlags
,
2171 secdesc
, eas
, num_eas
);
2175 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2178 status
= cli_nttrans_create_recv(req
, pfid
, cr
);
2184 /****************************************************************************
2186 WARNING: if you open with O_WRONLY then getattrE won't work!
2187 ****************************************************************************/
2189 struct cli_openx_state
{
2196 static void cli_openx_done(struct tevent_req
*subreq
);
2198 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2199 struct tevent_context
*ev
,
2200 struct cli_state
*cli
, const char *fname
,
2201 int flags
, int share_mode
,
2202 struct tevent_req
**psmbreq
)
2204 struct tevent_req
*req
, *subreq
;
2205 struct cli_openx_state
*state
;
2207 unsigned accessmode
;
2208 uint8_t additional_flags
;
2211 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2217 if (flags
& O_CREAT
) {
2220 if (!(flags
& O_EXCL
)) {
2221 if (flags
& O_TRUNC
)
2227 accessmode
= (share_mode
<<4);
2229 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2231 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2236 if ((flags
& O_SYNC
) == O_SYNC
) {
2237 accessmode
|= (1<<14);
2241 if (share_mode
== DENY_FCB
) {
2245 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2246 SCVAL(state
->vwv
+ 0, 1, 0);
2247 SSVAL(state
->vwv
+ 1, 0, 0);
2248 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2249 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2250 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2251 SSVAL(state
->vwv
+ 5, 0, 0);
2252 SIVAL(state
->vwv
+ 6, 0, 0);
2253 SSVAL(state
->vwv
+ 8, 0, openfn
);
2254 SIVAL(state
->vwv
+ 9, 0, 0);
2255 SIVAL(state
->vwv
+ 11, 0, 0);
2256 SIVAL(state
->vwv
+ 13, 0, 0);
2258 additional_flags
= 0;
2260 if (cli
->use_oplocks
) {
2261 /* if using oplocks then ask for a batch oplock via
2262 core and extended methods */
2264 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2265 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2268 bytes
= talloc_array(state
, uint8_t, 0);
2269 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2270 strlen(fname
)+1, NULL
);
2272 if (tevent_req_nomem(bytes
, req
)) {
2273 return tevent_req_post(req
, ev
);
2276 state
->bytes
.iov_base
= (void *)bytes
;
2277 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2279 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2280 15, state
->vwv
, 1, &state
->bytes
);
2281 if (subreq
== NULL
) {
2285 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2290 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2291 struct cli_state
*cli
, const char *fname
,
2292 int flags
, int share_mode
)
2294 struct tevent_req
*req
, *subreq
;
2297 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2303 status
= smb1cli_req_chain_submit(&subreq
, 1);
2304 if (tevent_req_nterror(req
, status
)) {
2305 return tevent_req_post(req
, ev
);
2310 static void cli_openx_done(struct tevent_req
*subreq
)
2312 struct tevent_req
*req
= tevent_req_callback_data(
2313 subreq
, struct tevent_req
);
2314 struct cli_openx_state
*state
= tevent_req_data(
2315 req
, struct cli_openx_state
);
2320 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2322 TALLOC_FREE(subreq
);
2323 if (tevent_req_nterror(req
, status
)) {
2326 state
->fnum
= SVAL(vwv
+2, 0);
2327 tevent_req_done(req
);
2330 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2332 struct cli_openx_state
*state
= tevent_req_data(
2333 req
, struct cli_openx_state
);
2336 if (tevent_req_is_nterror(req
, &status
)) {
2339 *pfnum
= state
->fnum
;
2340 return NT_STATUS_OK
;
2343 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2344 int share_mode
, uint16_t *pfnum
)
2346 TALLOC_CTX
*frame
= talloc_stackframe();
2347 struct tevent_context
*ev
;
2348 struct tevent_req
*req
;
2349 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2351 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2353 * Can't use sync call while an async call is in flight
2355 status
= NT_STATUS_INVALID_PARAMETER
;
2359 ev
= samba_tevent_context_init(frame
);
2364 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2369 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2373 status
= cli_openx_recv(req
, pfnum
);
2378 /****************************************************************************
2379 Synchronous wrapper function that does an NtCreateX open by preference
2380 and falls back to openX if this fails.
2381 ****************************************************************************/
2383 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2384 int share_mode_in
, uint16_t *pfnum
)
2387 unsigned int openfn
= 0;
2388 unsigned int dos_deny
= 0;
2389 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2390 struct smb_create_returns cr
;
2392 /* Do the initial mapping into OpenX parameters. */
2393 if (flags
& O_CREAT
) {
2396 if (!(flags
& O_EXCL
)) {
2397 if (flags
& O_TRUNC
)
2403 dos_deny
= (share_mode_in
<<4);
2405 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2407 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2412 if ((flags
& O_SYNC
) == O_SYNC
) {
2413 dos_deny
|= (1<<14);
2417 if (share_mode_in
== DENY_FCB
) {
2422 /* Hmmm. This is what I think the above code
2423 should look like if it's using the constants
2426 if (flags
& O_CREAT
) {
2427 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2429 if (!(flags
& O_EXCL
)) {
2430 if (flags
& O_TRUNC
)
2431 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2433 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2436 dos_deny
= SET_DENY_MODE(share_mode_in
);
2438 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2439 dos_deny
|= DOS_OPEN_RDWR
;
2440 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2441 dos_deny
|= DOS_OPEN_WRONLY
;
2445 if ((flags
& O_SYNC
) == O_SYNC
) {
2446 dos_deny
|= FILE_SYNC_OPENMODE
;
2450 if (share_mode_in
== DENY_FCB
) {
2455 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2456 openfn
, &access_mask
,
2457 &share_mode
, &create_disposition
,
2458 &create_options
, NULL
)) {
2462 status
= cli_ntcreate(cli
,
2474 /* Try and cope will all varients of "we don't do this call"
2475 and fall back to openX. */
2477 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2478 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2479 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2480 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2481 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2482 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2483 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2484 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2485 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2489 if (NT_STATUS_IS_OK(status
) &&
2490 (create_options
& FILE_NON_DIRECTORY_FILE
) &&
2491 (cr
.file_attributes
& FILE_ATTRIBUTE_DIRECTORY
))
2494 * Some (broken) servers return a valid handle
2495 * for directories even if FILE_NON_DIRECTORY_FILE
2496 * is set. Just close the handle and set the
2497 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2499 status
= cli_close(cli
, *pfnum
);
2500 if (!NT_STATUS_IS_OK(status
)) {
2503 status
= NT_STATUS_FILE_IS_A_DIRECTORY
;
2504 /* Set this so libsmbclient can retrieve it. */
2505 cli
->raw_status
= status
;
2512 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2515 /****************************************************************************
2517 ****************************************************************************/
2519 struct cli_close_state
{
2523 static void cli_close_done(struct tevent_req
*subreq
);
2525 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2526 struct tevent_context
*ev
,
2527 struct cli_state
*cli
,
2529 struct tevent_req
**psubreq
)
2531 struct tevent_req
*req
, *subreq
;
2532 struct cli_close_state
*state
;
2534 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2539 SSVAL(state
->vwv
+0, 0, fnum
);
2540 SIVALS(state
->vwv
+1, 0, -1);
2542 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2544 if (subreq
== NULL
) {
2548 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2553 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2554 struct tevent_context
*ev
,
2555 struct cli_state
*cli
,
2558 struct tevent_req
*req
, *subreq
;
2561 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2566 status
= smb1cli_req_chain_submit(&subreq
, 1);
2567 if (tevent_req_nterror(req
, status
)) {
2568 return tevent_req_post(req
, ev
);
2573 static void cli_close_done(struct tevent_req
*subreq
)
2575 struct tevent_req
*req
= tevent_req_callback_data(
2576 subreq
, struct tevent_req
);
2579 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2580 TALLOC_FREE(subreq
);
2581 if (tevent_req_nterror(req
, status
)) {
2584 tevent_req_done(req
);
2587 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2589 return tevent_req_simple_recv_ntstatus(req
);
2592 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2594 TALLOC_CTX
*frame
= NULL
;
2595 struct tevent_context
*ev
;
2596 struct tevent_req
*req
;
2597 NTSTATUS status
= NT_STATUS_OK
;
2599 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2600 return cli_smb2_close_fnum(cli
, fnum
);
2603 frame
= talloc_stackframe();
2605 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2607 * Can't use sync call while an async call is in flight
2609 status
= NT_STATUS_INVALID_PARAMETER
;
2613 ev
= samba_tevent_context_init(frame
);
2615 status
= NT_STATUS_NO_MEMORY
;
2619 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2621 status
= NT_STATUS_NO_MEMORY
;
2625 if (!tevent_req_poll(req
, ev
)) {
2626 status
= map_nt_error_from_unix(errno
);
2630 status
= cli_close_recv(req
);
2636 /****************************************************************************
2637 Truncate a file to a specified size
2638 ****************************************************************************/
2640 struct ftrunc_state
{
2646 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2648 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2649 NULL
, 0, NULL
, NULL
, 0, NULL
);
2650 tevent_req_simple_finish_ntstatus(subreq
, status
);
2653 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2654 struct tevent_context
*ev
,
2655 struct cli_state
*cli
,
2659 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2660 struct ftrunc_state
*state
= NULL
;
2662 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2667 /* Setup setup word. */
2668 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2670 /* Setup param array. */
2671 SSVAL(state
->param
,0,fnum
);
2672 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2673 SSVAL(state
->param
,4,0);
2675 /* Setup data array. */
2676 SBVAL(state
->data
, 0, size
);
2678 subreq
= cli_trans_send(state
, /* mem ctx. */
2679 ev
, /* event ctx. */
2680 cli
, /* cli_state. */
2681 SMBtrans2
, /* cmd. */
2682 NULL
, /* pipe name. */
2686 &state
->setup
, /* setup. */
2687 1, /* num setup uint16_t words. */
2688 0, /* max returned setup. */
2689 state
->param
, /* param. */
2691 2, /* max returned param. */
2692 state
->data
, /* data. */
2694 0); /* max returned data. */
2696 if (tevent_req_nomem(subreq
, req
)) {
2697 return tevent_req_post(req
, ev
);
2699 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2703 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2705 return tevent_req_simple_recv_ntstatus(req
);
2708 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2710 TALLOC_CTX
*frame
= talloc_stackframe();
2711 struct tevent_context
*ev
= NULL
;
2712 struct tevent_req
*req
= NULL
;
2713 NTSTATUS status
= NT_STATUS_OK
;
2715 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2717 * Can't use sync call while an async call is in flight
2719 status
= NT_STATUS_INVALID_PARAMETER
;
2723 ev
= samba_tevent_context_init(frame
);
2725 status
= NT_STATUS_NO_MEMORY
;
2729 req
= cli_ftruncate_send(frame
,
2735 status
= NT_STATUS_NO_MEMORY
;
2739 if (!tevent_req_poll(req
, ev
)) {
2740 status
= map_nt_error_from_unix(errno
);
2744 status
= cli_ftruncate_recv(req
);
2751 /****************************************************************************
2752 send a lock with a specified locktype
2753 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2754 ****************************************************************************/
2756 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2757 uint32_t offset
, uint32_t len
,
2758 int timeout
, unsigned char locktype
)
2763 unsigned int set_timeout
= 0;
2764 unsigned int saved_timeout
= 0;
2766 SCVAL(vwv
+ 0, 0, 0xff);
2767 SCVAL(vwv
+ 0, 1, 0);
2768 SSVAL(vwv
+ 1, 0, 0);
2769 SSVAL(vwv
+ 2, 0, fnum
);
2770 SCVAL(vwv
+ 3, 0, locktype
);
2771 SCVAL(vwv
+ 3, 1, 0);
2772 SIVALS(vwv
+ 4, 0, timeout
);
2773 SSVAL(vwv
+ 6, 0, 0);
2774 SSVAL(vwv
+ 7, 0, 1);
2776 SSVAL(bytes
, 0, cli_getpid(cli
));
2777 SIVAL(bytes
, 2, offset
);
2778 SIVAL(bytes
, 6, len
);
2781 if (timeout
== -1) {
2782 set_timeout
= 0x7FFFFFFF;
2784 set_timeout
= timeout
+ 2*1000;
2786 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2789 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2790 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2792 if (saved_timeout
!= 0) {
2793 cli_set_timeout(cli
, saved_timeout
);
2799 /****************************************************************************
2801 note that timeout is in units of 2 milliseconds
2802 ****************************************************************************/
2804 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2805 uint32_t offset
, uint32_t len
, int timeout
,
2806 enum brl_type lock_type
)
2810 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2811 (lock_type
== READ_LOCK
? 1 : 0));
2815 /****************************************************************************
2817 ****************************************************************************/
2819 struct cli_unlock_state
{
2824 static void cli_unlock_done(struct tevent_req
*subreq
);
2826 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2827 struct tevent_context
*ev
,
2828 struct cli_state
*cli
,
2834 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2835 struct cli_unlock_state
*state
= NULL
;
2836 uint8_t additional_flags
= 0;
2838 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2843 SCVAL(state
->vwv
+0, 0, 0xFF);
2844 SSVAL(state
->vwv
+2, 0, fnum
);
2845 SCVAL(state
->vwv
+3, 0, 0);
2846 SIVALS(state
->vwv
+4, 0, 0);
2847 SSVAL(state
->vwv
+6, 0, 1);
2848 SSVAL(state
->vwv
+7, 0, 0);
2850 SSVAL(state
->data
, 0, cli_getpid(cli
));
2851 SIVAL(state
->data
, 2, offset
);
2852 SIVAL(state
->data
, 6, len
);
2854 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2855 8, state
->vwv
, 10, state
->data
);
2856 if (tevent_req_nomem(subreq
, req
)) {
2857 return tevent_req_post(req
, ev
);
2859 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2863 static void cli_unlock_done(struct tevent_req
*subreq
)
2865 struct tevent_req
*req
= tevent_req_callback_data(
2866 subreq
, struct tevent_req
);
2869 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2870 TALLOC_FREE(subreq
);
2871 if (tevent_req_nterror(req
, status
)) {
2874 tevent_req_done(req
);
2877 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2879 return tevent_req_simple_recv_ntstatus(req
);
2882 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2887 TALLOC_CTX
*frame
= talloc_stackframe();
2888 struct tevent_context
*ev
;
2889 struct tevent_req
*req
;
2890 NTSTATUS status
= NT_STATUS_OK
;
2892 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2894 * Can't use sync call while an async call is in flight
2896 status
= NT_STATUS_INVALID_PARAMETER
;
2900 ev
= samba_tevent_context_init(frame
);
2902 status
= NT_STATUS_NO_MEMORY
;
2906 req
= cli_unlock_send(frame
, ev
, cli
,
2909 status
= NT_STATUS_NO_MEMORY
;
2913 if (!tevent_req_poll(req
, ev
)) {
2914 status
= map_nt_error_from_unix(errno
);
2918 status
= cli_unlock_recv(req
);
2925 /****************************************************************************
2926 Lock a file with 64 bit offsets.
2927 ****************************************************************************/
2929 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2930 uint64_t offset
, uint64_t len
, int timeout
,
2931 enum brl_type lock_type
)
2935 unsigned int set_timeout
= 0;
2936 unsigned int saved_timeout
= 0;
2940 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2941 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2944 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2945 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2947 SCVAL(vwv
+ 0, 0, 0xff);
2948 SCVAL(vwv
+ 0, 1, 0);
2949 SSVAL(vwv
+ 1, 0, 0);
2950 SSVAL(vwv
+ 2, 0, fnum
);
2951 SCVAL(vwv
+ 3, 0, ltype
);
2952 SCVAL(vwv
+ 3, 1, 0);
2953 SIVALS(vwv
+ 4, 0, timeout
);
2954 SSVAL(vwv
+ 6, 0, 0);
2955 SSVAL(vwv
+ 7, 0, 1);
2957 SIVAL(bytes
, 0, cli_getpid(cli
));
2958 SOFF_T_R(bytes
, 4, offset
);
2959 SOFF_T_R(bytes
, 12, len
);
2962 if (timeout
== -1) {
2963 set_timeout
= 0x7FFFFFFF;
2965 set_timeout
= timeout
+ 2*1000;
2967 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2970 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2971 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2973 if (saved_timeout
!= 0) {
2974 cli_set_timeout(cli
, saved_timeout
);
2980 /****************************************************************************
2981 Unlock a file with 64 bit offsets.
2982 ****************************************************************************/
2984 struct cli_unlock64_state
{
2989 static void cli_unlock64_done(struct tevent_req
*subreq
);
2991 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2992 struct tevent_context
*ev
,
2993 struct cli_state
*cli
,
2999 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3000 struct cli_unlock64_state
*state
= NULL
;
3001 uint8_t additional_flags
= 0;
3003 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
3008 SCVAL(state
->vwv
+0, 0, 0xff);
3009 SSVAL(state
->vwv
+2, 0, fnum
);
3010 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
3011 SIVALS(state
->vwv
+4, 0, 0);
3012 SSVAL(state
->vwv
+6, 0, 1);
3013 SSVAL(state
->vwv
+7, 0, 0);
3015 SIVAL(state
->data
, 0, cli_getpid(cli
));
3016 SOFF_T_R(state
->data
, 4, offset
);
3017 SOFF_T_R(state
->data
, 12, len
);
3019 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
3020 8, state
->vwv
, 20, state
->data
);
3021 if (tevent_req_nomem(subreq
, req
)) {
3022 return tevent_req_post(req
, ev
);
3024 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
3028 static void cli_unlock64_done(struct tevent_req
*subreq
)
3030 struct tevent_req
*req
= tevent_req_callback_data(
3031 subreq
, struct tevent_req
);
3034 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3035 TALLOC_FREE(subreq
);
3036 if (tevent_req_nterror(req
, status
)) {
3039 tevent_req_done(req
);
3042 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
3044 return tevent_req_simple_recv_ntstatus(req
);
3047 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
3052 TALLOC_CTX
*frame
= talloc_stackframe();
3053 struct tevent_context
*ev
;
3054 struct tevent_req
*req
;
3055 NTSTATUS status
= NT_STATUS_OK
;
3057 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3058 return cli_unlock(cli
, fnum
, offset
, len
);
3061 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3063 * Can't use sync call while an async call is in flight
3065 status
= NT_STATUS_INVALID_PARAMETER
;
3069 ev
= samba_tevent_context_init(frame
);
3071 status
= NT_STATUS_NO_MEMORY
;
3075 req
= cli_unlock64_send(frame
, ev
, cli
,
3078 status
= NT_STATUS_NO_MEMORY
;
3082 if (!tevent_req_poll(req
, ev
)) {
3083 status
= map_nt_error_from_unix(errno
);
3087 status
= cli_unlock64_recv(req
);
3094 /****************************************************************************
3095 Get/unlock a POSIX lock on a file - internal function.
3096 ****************************************************************************/
3098 struct posix_lock_state
{
3101 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3104 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3106 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3107 NULL
, 0, NULL
, NULL
, 0, NULL
);
3108 tevent_req_simple_finish_ntstatus(subreq
, status
);
3111 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3112 struct tevent_context
*ev
,
3113 struct cli_state
*cli
,
3118 enum brl_type lock_type
)
3120 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3121 struct posix_lock_state
*state
= NULL
;
3123 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3128 /* Setup setup word. */
3129 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3131 /* Setup param array. */
3132 SSVAL(&state
->param
, 0, fnum
);
3133 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3135 /* Setup data array. */
3136 switch (lock_type
) {
3138 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3139 POSIX_LOCK_TYPE_READ
);
3142 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3143 POSIX_LOCK_TYPE_WRITE
);
3146 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3147 POSIX_LOCK_TYPE_UNLOCK
);
3154 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3155 POSIX_LOCK_FLAG_WAIT
);
3157 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3158 POSIX_LOCK_FLAG_NOWAIT
);
3161 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3162 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3163 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3165 subreq
= cli_trans_send(state
, /* mem ctx. */
3166 ev
, /* event ctx. */
3167 cli
, /* cli_state. */
3168 SMBtrans2
, /* cmd. */
3169 NULL
, /* pipe name. */
3173 &state
->setup
, /* setup. */
3174 1, /* num setup uint16_t words. */
3175 0, /* max returned setup. */
3176 state
->param
, /* param. */
3178 2, /* max returned param. */
3179 state
->data
, /* data. */
3180 POSIX_LOCK_DATA_SIZE
, /* num data. */
3181 0); /* max returned data. */
3183 if (tevent_req_nomem(subreq
, req
)) {
3184 return tevent_req_post(req
, ev
);
3186 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3190 /****************************************************************************
3192 ****************************************************************************/
3194 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3195 struct tevent_context
*ev
,
3196 struct cli_state
*cli
,
3201 enum brl_type lock_type
)
3203 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3204 wait_lock
, lock_type
);
3207 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3209 return tevent_req_simple_recv_ntstatus(req
);
3212 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3213 uint64_t offset
, uint64_t len
,
3214 bool wait_lock
, enum brl_type lock_type
)
3216 TALLOC_CTX
*frame
= talloc_stackframe();
3217 struct tevent_context
*ev
= NULL
;
3218 struct tevent_req
*req
= NULL
;
3219 NTSTATUS status
= NT_STATUS_OK
;
3221 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3223 * Can't use sync call while an async call is in flight
3225 status
= NT_STATUS_INVALID_PARAMETER
;
3229 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3230 status
= NT_STATUS_INVALID_PARAMETER
;
3234 ev
= samba_tevent_context_init(frame
);
3236 status
= NT_STATUS_NO_MEMORY
;
3240 req
= cli_posix_lock_send(frame
,
3249 status
= NT_STATUS_NO_MEMORY
;
3253 if (!tevent_req_poll(req
, ev
)) {
3254 status
= map_nt_error_from_unix(errno
);
3258 status
= cli_posix_lock_recv(req
);
3265 /****************************************************************************
3266 POSIX Unlock a file.
3267 ****************************************************************************/
3269 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3270 struct tevent_context
*ev
,
3271 struct cli_state
*cli
,
3276 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3277 false, UNLOCK_LOCK
);
3280 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3282 return tevent_req_simple_recv_ntstatus(req
);
3285 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3287 TALLOC_CTX
*frame
= talloc_stackframe();
3288 struct tevent_context
*ev
= NULL
;
3289 struct tevent_req
*req
= NULL
;
3290 NTSTATUS status
= NT_STATUS_OK
;
3292 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3294 * Can't use sync call while an async call is in flight
3296 status
= NT_STATUS_INVALID_PARAMETER
;
3300 ev
= samba_tevent_context_init(frame
);
3302 status
= NT_STATUS_NO_MEMORY
;
3306 req
= cli_posix_unlock_send(frame
,
3313 status
= NT_STATUS_NO_MEMORY
;
3317 if (!tevent_req_poll(req
, ev
)) {
3318 status
= map_nt_error_from_unix(errno
);
3322 status
= cli_posix_unlock_recv(req
);
3329 /****************************************************************************
3330 Do a SMBgetattrE call.
3331 ****************************************************************************/
3333 static void cli_getattrE_done(struct tevent_req
*subreq
);
3335 struct cli_getattrE_state
{
3345 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3346 struct tevent_context
*ev
,
3347 struct cli_state
*cli
,
3350 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3351 struct cli_getattrE_state
*state
= NULL
;
3352 uint8_t additional_flags
= 0;
3354 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3359 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3360 SSVAL(state
->vwv
+0,0,fnum
);
3362 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3363 1, state
->vwv
, 0, NULL
);
3364 if (tevent_req_nomem(subreq
, req
)) {
3365 return tevent_req_post(req
, ev
);
3367 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3371 static void cli_getattrE_done(struct tevent_req
*subreq
)
3373 struct tevent_req
*req
= tevent_req_callback_data(
3374 subreq
, struct tevent_req
);
3375 struct cli_getattrE_state
*state
= tevent_req_data(
3376 req
, struct cli_getattrE_state
);
3378 uint16_t *vwv
= NULL
;
3381 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3383 TALLOC_FREE(subreq
);
3384 if (tevent_req_nterror(req
, status
)) {
3388 state
->size
= (off_t
)IVAL(vwv
+6,0);
3389 state
->attr
= SVAL(vwv
+10,0);
3390 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3391 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3392 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3394 tevent_req_done(req
);
3397 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3400 time_t *change_time
,
3401 time_t *access_time
,
3404 struct cli_getattrE_state
*state
= tevent_req_data(
3405 req
, struct cli_getattrE_state
);
3408 if (tevent_req_is_nterror(req
, &status
)) {
3412 *attr
= state
->attr
;
3415 *size
= state
->size
;
3418 *change_time
= state
->change_time
;
3421 *access_time
= state
->access_time
;
3424 *write_time
= state
->write_time
;
3426 return NT_STATUS_OK
;
3429 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3433 time_t *change_time
,
3434 time_t *access_time
,
3437 TALLOC_CTX
*frame
= NULL
;
3438 struct tevent_context
*ev
= NULL
;
3439 struct tevent_req
*req
= NULL
;
3440 NTSTATUS status
= NT_STATUS_OK
;
3442 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3443 return cli_smb2_getattrE(cli
,
3452 frame
= talloc_stackframe();
3454 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3456 * Can't use sync call while an async call is in flight
3458 status
= NT_STATUS_INVALID_PARAMETER
;
3462 ev
= samba_tevent_context_init(frame
);
3464 status
= NT_STATUS_NO_MEMORY
;
3468 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3470 status
= NT_STATUS_NO_MEMORY
;
3474 if (!tevent_req_poll(req
, ev
)) {
3475 status
= map_nt_error_from_unix(errno
);
3479 status
= cli_getattrE_recv(req
,
3491 /****************************************************************************
3493 ****************************************************************************/
3495 static void cli_getatr_done(struct tevent_req
*subreq
);
3497 struct cli_getatr_state
{
3504 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3505 struct tevent_context
*ev
,
3506 struct cli_state
*cli
,
3509 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3510 struct cli_getatr_state
*state
= NULL
;
3511 uint8_t additional_flags
= 0;
3512 uint8_t *bytes
= NULL
;
3514 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3519 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3521 bytes
= talloc_array(state
, uint8_t, 1);
3522 if (tevent_req_nomem(bytes
, req
)) {
3523 return tevent_req_post(req
, ev
);
3526 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3527 strlen(fname
)+1, NULL
);
3529 if (tevent_req_nomem(bytes
, req
)) {
3530 return tevent_req_post(req
, ev
);
3533 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3534 0, NULL
, talloc_get_size(bytes
), bytes
);
3535 if (tevent_req_nomem(subreq
, req
)) {
3536 return tevent_req_post(req
, ev
);
3538 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3542 static void cli_getatr_done(struct tevent_req
*subreq
)
3544 struct tevent_req
*req
= tevent_req_callback_data(
3545 subreq
, struct tevent_req
);
3546 struct cli_getatr_state
*state
= tevent_req_data(
3547 req
, struct cli_getatr_state
);
3549 uint16_t *vwv
= NULL
;
3552 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3554 TALLOC_FREE(subreq
);
3555 if (tevent_req_nterror(req
, status
)) {
3559 state
->attr
= SVAL(vwv
+0,0);
3560 state
->size
= (off_t
)IVAL(vwv
+3,0);
3561 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3563 tevent_req_done(req
);
3566 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3571 struct cli_getatr_state
*state
= tevent_req_data(
3572 req
, struct cli_getatr_state
);
3575 if (tevent_req_is_nterror(req
, &status
)) {
3579 *attr
= state
->attr
;
3582 *size
= state
->size
;
3585 *write_time
= state
->write_time
;
3587 return NT_STATUS_OK
;
3590 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3596 TALLOC_CTX
*frame
= NULL
;
3597 struct tevent_context
*ev
= NULL
;
3598 struct tevent_req
*req
= NULL
;
3599 NTSTATUS status
= NT_STATUS_OK
;
3601 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3602 return cli_smb2_getatr(cli
,
3609 frame
= talloc_stackframe();
3611 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3613 * Can't use sync call while an async call is in flight
3615 status
= NT_STATUS_INVALID_PARAMETER
;
3619 ev
= samba_tevent_context_init(frame
);
3621 status
= NT_STATUS_NO_MEMORY
;
3625 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3627 status
= NT_STATUS_NO_MEMORY
;
3631 if (!tevent_req_poll(req
, ev
)) {
3632 status
= map_nt_error_from_unix(errno
);
3636 status
= cli_getatr_recv(req
,
3646 /****************************************************************************
3647 Do a SMBsetattrE call.
3648 ****************************************************************************/
3650 static void cli_setattrE_done(struct tevent_req
*subreq
);
3652 struct cli_setattrE_state
{
3656 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3657 struct tevent_context
*ev
,
3658 struct cli_state
*cli
,
3664 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3665 struct cli_setattrE_state
*state
= NULL
;
3666 uint8_t additional_flags
= 0;
3668 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3673 SSVAL(state
->vwv
+0, 0, fnum
);
3674 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3675 smb1cli_conn_server_time_zone(cli
->conn
));
3676 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3677 smb1cli_conn_server_time_zone(cli
->conn
));
3678 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3679 smb1cli_conn_server_time_zone(cli
->conn
));
3681 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3682 7, state
->vwv
, 0, NULL
);
3683 if (tevent_req_nomem(subreq
, req
)) {
3684 return tevent_req_post(req
, ev
);
3686 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3690 static void cli_setattrE_done(struct tevent_req
*subreq
)
3692 struct tevent_req
*req
= tevent_req_callback_data(
3693 subreq
, struct tevent_req
);
3696 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3697 TALLOC_FREE(subreq
);
3698 if (tevent_req_nterror(req
, status
)) {
3701 tevent_req_done(req
);
3704 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3706 return tevent_req_simple_recv_ntstatus(req
);
3709 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3715 TALLOC_CTX
*frame
= NULL
;
3716 struct tevent_context
*ev
= NULL
;
3717 struct tevent_req
*req
= NULL
;
3718 NTSTATUS status
= NT_STATUS_OK
;
3720 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3721 return cli_smb2_setattrE(cli
,
3728 frame
= talloc_stackframe();
3730 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3732 * Can't use sync call while an async call is in flight
3734 status
= NT_STATUS_INVALID_PARAMETER
;
3738 ev
= samba_tevent_context_init(frame
);
3740 status
= NT_STATUS_NO_MEMORY
;
3744 req
= cli_setattrE_send(frame
, ev
,
3752 status
= NT_STATUS_NO_MEMORY
;
3756 if (!tevent_req_poll(req
, ev
)) {
3757 status
= map_nt_error_from_unix(errno
);
3761 status
= cli_setattrE_recv(req
);
3768 /****************************************************************************
3769 Do a SMBsetatr call.
3770 ****************************************************************************/
3772 static void cli_setatr_done(struct tevent_req
*subreq
);
3774 struct cli_setatr_state
{
3778 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3779 struct tevent_context
*ev
,
3780 struct cli_state
*cli
,
3785 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3786 struct cli_setatr_state
*state
= NULL
;
3787 uint8_t additional_flags
= 0;
3788 uint8_t *bytes
= NULL
;
3790 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3795 SSVAL(state
->vwv
+0, 0, attr
);
3796 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3798 bytes
= talloc_array(state
, uint8_t, 1);
3799 if (tevent_req_nomem(bytes
, req
)) {
3800 return tevent_req_post(req
, ev
);
3803 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3804 strlen(fname
)+1, NULL
);
3805 if (tevent_req_nomem(bytes
, req
)) {
3806 return tevent_req_post(req
, ev
);
3808 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3809 talloc_get_size(bytes
)+1);
3810 if (tevent_req_nomem(bytes
, req
)) {
3811 return tevent_req_post(req
, ev
);
3814 bytes
[talloc_get_size(bytes
)-1] = 4;
3815 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3817 if (tevent_req_nomem(bytes
, req
)) {
3818 return tevent_req_post(req
, ev
);
3821 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3822 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3823 if (tevent_req_nomem(subreq
, req
)) {
3824 return tevent_req_post(req
, ev
);
3826 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3830 static void cli_setatr_done(struct tevent_req
*subreq
)
3832 struct tevent_req
*req
= tevent_req_callback_data(
3833 subreq
, struct tevent_req
);
3836 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3837 TALLOC_FREE(subreq
);
3838 if (tevent_req_nterror(req
, status
)) {
3841 tevent_req_done(req
);
3844 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3846 return tevent_req_simple_recv_ntstatus(req
);
3849 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3854 TALLOC_CTX
*frame
= NULL
;
3855 struct tevent_context
*ev
= NULL
;
3856 struct tevent_req
*req
= NULL
;
3857 NTSTATUS status
= NT_STATUS_OK
;
3859 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3860 return cli_smb2_setatr(cli
,
3866 frame
= talloc_stackframe();
3868 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3870 * Can't use sync call while an async call is in flight
3872 status
= NT_STATUS_INVALID_PARAMETER
;
3876 ev
= samba_tevent_context_init(frame
);
3878 status
= NT_STATUS_NO_MEMORY
;
3882 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3884 status
= NT_STATUS_NO_MEMORY
;
3888 if (!tevent_req_poll(req
, ev
)) {
3889 status
= map_nt_error_from_unix(errno
);
3893 status
= cli_setatr_recv(req
);
3900 /****************************************************************************
3901 Check for existance of a dir.
3902 ****************************************************************************/
3904 static void cli_chkpath_done(struct tevent_req
*subreq
);
3906 struct cli_chkpath_state
{
3910 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3911 struct tevent_context
*ev
,
3912 struct cli_state
*cli
,
3915 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3916 struct cli_chkpath_state
*state
= NULL
;
3917 uint8_t additional_flags
= 0;
3918 uint8_t *bytes
= NULL
;
3920 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3925 bytes
= talloc_array(state
, uint8_t, 1);
3926 if (tevent_req_nomem(bytes
, req
)) {
3927 return tevent_req_post(req
, ev
);
3930 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3931 strlen(fname
)+1, NULL
);
3933 if (tevent_req_nomem(bytes
, req
)) {
3934 return tevent_req_post(req
, ev
);
3937 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3938 0, NULL
, talloc_get_size(bytes
), bytes
);
3939 if (tevent_req_nomem(subreq
, req
)) {
3940 return tevent_req_post(req
, ev
);
3942 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3946 static void cli_chkpath_done(struct tevent_req
*subreq
)
3948 struct tevent_req
*req
= tevent_req_callback_data(
3949 subreq
, struct tevent_req
);
3952 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3953 TALLOC_FREE(subreq
);
3954 if (tevent_req_nterror(req
, status
)) {
3957 tevent_req_done(req
);
3960 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3962 return tevent_req_simple_recv_ntstatus(req
);
3965 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3967 TALLOC_CTX
*frame
= talloc_stackframe();
3968 struct tevent_context
*ev
= NULL
;
3969 struct tevent_req
*req
= NULL
;
3971 NTSTATUS status
= NT_STATUS_OK
;
3973 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3975 * Can't use sync call while an async call is in flight
3977 status
= NT_STATUS_INVALID_PARAMETER
;
3981 path2
= talloc_strdup(frame
, path
);
3983 status
= NT_STATUS_NO_MEMORY
;
3986 trim_char(path2
,'\0','\\');
3988 path2
= talloc_strdup(frame
, "\\");
3990 status
= NT_STATUS_NO_MEMORY
;
3995 ev
= samba_tevent_context_init(frame
);
3997 status
= NT_STATUS_NO_MEMORY
;
4001 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
4003 status
= NT_STATUS_NO_MEMORY
;
4007 if (!tevent_req_poll(req
, ev
)) {
4008 status
= map_nt_error_from_unix(errno
);
4012 status
= cli_chkpath_recv(req
);
4019 /****************************************************************************
4021 ****************************************************************************/
4023 static void cli_dskattr_done(struct tevent_req
*subreq
);
4025 struct cli_dskattr_state
{
4031 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
4032 struct tevent_context
*ev
,
4033 struct cli_state
*cli
)
4035 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4036 struct cli_dskattr_state
*state
= NULL
;
4037 uint8_t additional_flags
= 0;
4039 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
4044 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
4046 if (tevent_req_nomem(subreq
, req
)) {
4047 return tevent_req_post(req
, ev
);
4049 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
4053 static void cli_dskattr_done(struct tevent_req
*subreq
)
4055 struct tevent_req
*req
= tevent_req_callback_data(
4056 subreq
, struct tevent_req
);
4057 struct cli_dskattr_state
*state
= tevent_req_data(
4058 req
, struct cli_dskattr_state
);
4060 uint16_t *vwv
= NULL
;
4063 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
4065 TALLOC_FREE(subreq
);
4066 if (tevent_req_nterror(req
, status
)) {
4069 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
4070 state
->total
= SVAL(vwv
+0, 0);
4071 state
->avail
= SVAL(vwv
+3, 0);
4072 tevent_req_done(req
);
4075 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
4077 struct cli_dskattr_state
*state
= tevent_req_data(
4078 req
, struct cli_dskattr_state
);
4081 if (tevent_req_is_nterror(req
, &status
)) {
4084 *bsize
= state
->bsize
;
4085 *total
= state
->total
;
4086 *avail
= state
->avail
;
4087 return NT_STATUS_OK
;
4090 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4092 TALLOC_CTX
*frame
= NULL
;
4093 struct tevent_context
*ev
= NULL
;
4094 struct tevent_req
*req
= NULL
;
4095 NTSTATUS status
= NT_STATUS_OK
;
4097 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4098 return cli_smb2_dskattr(cli
, bsize
, total
, avail
);
4101 frame
= talloc_stackframe();
4103 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4105 * Can't use sync call while an async call is in flight
4107 status
= NT_STATUS_INVALID_PARAMETER
;
4111 ev
= samba_tevent_context_init(frame
);
4113 status
= NT_STATUS_NO_MEMORY
;
4117 req
= cli_dskattr_send(frame
, ev
, cli
);
4119 status
= NT_STATUS_NO_MEMORY
;
4123 if (!tevent_req_poll(req
, ev
)) {
4124 status
= map_nt_error_from_unix(errno
);
4128 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4135 /****************************************************************************
4136 Create and open a temporary file.
4137 ****************************************************************************/
4139 static void cli_ctemp_done(struct tevent_req
*subreq
);
4141 struct ctemp_state
{
4147 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4148 struct tevent_context
*ev
,
4149 struct cli_state
*cli
,
4152 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4153 struct ctemp_state
*state
= NULL
;
4154 uint8_t additional_flags
= 0;
4155 uint8_t *bytes
= NULL
;
4157 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4162 SSVAL(state
->vwv
,0,0);
4163 SIVALS(state
->vwv
+1,0,-1);
4165 bytes
= talloc_array(state
, uint8_t, 1);
4166 if (tevent_req_nomem(bytes
, req
)) {
4167 return tevent_req_post(req
, ev
);
4170 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4171 strlen(path
)+1, NULL
);
4172 if (tevent_req_nomem(bytes
, req
)) {
4173 return tevent_req_post(req
, ev
);
4176 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4177 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4178 if (tevent_req_nomem(subreq
, req
)) {
4179 return tevent_req_post(req
, ev
);
4181 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4185 static void cli_ctemp_done(struct tevent_req
*subreq
)
4187 struct tevent_req
*req
= tevent_req_callback_data(
4188 subreq
, struct tevent_req
);
4189 struct ctemp_state
*state
= tevent_req_data(
4190 req
, struct ctemp_state
);
4194 uint32_t num_bytes
= 0;
4195 uint8_t *bytes
= NULL
;
4197 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4198 &num_bytes
, &bytes
);
4199 TALLOC_FREE(subreq
);
4200 if (tevent_req_nterror(req
, status
)) {
4204 state
->fnum
= SVAL(vwv
+0, 0);
4206 /* From W2K3, the result is just the ASCII name */
4207 if (num_bytes
< 2) {
4208 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4212 if (pull_string_talloc(state
,
4219 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4222 tevent_req_done(req
);
4225 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4230 struct ctemp_state
*state
= tevent_req_data(req
,
4231 struct ctemp_state
);
4234 if (tevent_req_is_nterror(req
, &status
)) {
4237 *pfnum
= state
->fnum
;
4238 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4240 return NT_STATUS_NO_MEMORY
;
4242 return NT_STATUS_OK
;
4245 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4251 TALLOC_CTX
*frame
= talloc_stackframe();
4252 struct tevent_context
*ev
;
4253 struct tevent_req
*req
;
4254 NTSTATUS status
= NT_STATUS_OK
;
4256 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4258 * Can't use sync call while an async call is in flight
4260 status
= NT_STATUS_INVALID_PARAMETER
;
4264 ev
= samba_tevent_context_init(frame
);
4266 status
= NT_STATUS_NO_MEMORY
;
4270 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4272 status
= NT_STATUS_NO_MEMORY
;
4276 if (!tevent_req_poll(req
, ev
)) {
4277 status
= map_nt_error_from_unix(errno
);
4281 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4289 send a raw ioctl - used by the torture code
4291 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4296 SSVAL(vwv
+0, 0, fnum
);
4297 SSVAL(vwv
+1, 0, code
>>16);
4298 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4300 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4301 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4302 if (!NT_STATUS_IS_OK(status
)) {
4305 *blob
= data_blob_null
;
4306 return NT_STATUS_OK
;
4309 /*********************************************************
4310 Set an extended attribute utility fn.
4311 *********************************************************/
4313 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4314 uint8_t *param
, unsigned int param_len
,
4315 const char *ea_name
,
4316 const char *ea_val
, size_t ea_len
)
4319 unsigned int data_len
= 0;
4320 uint8_t *data
= NULL
;
4322 size_t ea_namelen
= strlen(ea_name
);
4325 SSVAL(setup
, 0, setup_val
);
4327 if (ea_namelen
== 0 && ea_len
== 0) {
4329 data
= talloc_array(talloc_tos(),
4333 return NT_STATUS_NO_MEMORY
;
4336 SIVAL(p
,0,data_len
);
4338 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4339 data
= talloc_array(talloc_tos(),
4343 return NT_STATUS_NO_MEMORY
;
4346 SIVAL(p
,0,data_len
);
4348 SCVAL(p
, 0, 0); /* EA flags. */
4349 SCVAL(p
, 1, ea_namelen
);
4350 SSVAL(p
, 2, ea_len
);
4351 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4352 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4355 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4357 param
, param_len
, 2,
4358 data
, data_len
, CLI_BUFFER_SIZE
,
4360 NULL
, 0, NULL
, /* rsetup */
4361 NULL
, 0, NULL
, /* rparam */
4362 NULL
, 0, NULL
); /* rdata */
4367 /*********************************************************
4368 Set an extended attribute on a pathname.
4369 *********************************************************/
4371 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4372 const char *ea_name
, const char *ea_val
,
4375 unsigned int param_len
= 0;
4378 TALLOC_CTX
*frame
= NULL
;
4380 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4381 return cli_smb2_set_ea_path(cli
,
4388 frame
= talloc_stackframe();
4390 param
= talloc_array(frame
, uint8_t, 6);
4392 status
= NT_STATUS_NO_MEMORY
;
4395 SSVAL(param
,0,SMB_INFO_SET_EA
);
4399 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4400 path
, strlen(path
)+1,
4402 param_len
= talloc_get_size(param
);
4404 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4405 ea_name
, ea_val
, ea_len
);
4413 /*********************************************************
4414 Set an extended attribute on an fnum.
4415 *********************************************************/
4417 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4418 const char *ea_name
, const char *ea_val
,
4423 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4424 return cli_smb2_set_ea_fnum(cli
,
4431 memset(param
, 0, 6);
4432 SSVAL(param
,0,fnum
);
4433 SSVAL(param
,2,SMB_INFO_SET_EA
);
4435 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4436 ea_name
, ea_val
, ea_len
);
4439 /*********************************************************
4440 Get an extended attribute list utility fn.
4441 *********************************************************/
4443 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4445 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4447 struct ea_struct
*ea_list
= NULL
;
4452 if (rdata_len
< 4) {
4456 ea_size
= (size_t)IVAL(rdata
,0);
4457 if (ea_size
> rdata_len
) {
4462 /* No EA's present. */
4471 /* Validate the EA list and count it. */
4472 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4473 unsigned int ea_namelen
= CVAL(p
,1);
4474 unsigned int ea_valuelen
= SVAL(p
,2);
4475 if (ea_namelen
== 0) {
4478 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4481 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4482 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4491 *pnum_eas
= num_eas
;
4493 /* Caller only wants number of EA's. */
4497 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4502 ea_size
= (size_t)IVAL(rdata
,0);
4505 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4506 struct ea_struct
*ea
= &ea_list
[num_eas
];
4507 fstring unix_ea_name
;
4508 unsigned int ea_namelen
= CVAL(p
,1);
4509 unsigned int ea_valuelen
= SVAL(p
,2);
4511 ea
->flags
= CVAL(p
,0);
4512 unix_ea_name
[0] = '\0';
4513 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4514 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4518 /* Ensure the value is null terminated (in case it's a string). */
4519 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4520 if (!ea
->value
.data
) {
4524 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4526 ea
->value
.data
[ea_valuelen
] = 0;
4528 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4531 *pea_list
= ea_list
;
4535 TALLOC_FREE(ea_list
);
4539 /*********************************************************
4540 Get an extended attribute list from a pathname.
4541 *********************************************************/
4543 struct cli_get_ea_list_path_state
{
4548 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4550 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4551 struct tevent_context
*ev
,
4552 struct cli_state
*cli
,
4555 struct tevent_req
*req
, *subreq
;
4556 struct cli_get_ea_list_path_state
*state
;
4558 req
= tevent_req_create(mem_ctx
, &state
,
4559 struct cli_get_ea_list_path_state
);
4563 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4564 SMB_INFO_QUERY_ALL_EAS
, 4,
4566 if (tevent_req_nomem(subreq
, req
)) {
4567 return tevent_req_post(req
, ev
);
4569 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4573 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4575 struct tevent_req
*req
= tevent_req_callback_data(
4576 subreq
, struct tevent_req
);
4577 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4578 req
, struct cli_get_ea_list_path_state
);
4581 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4583 TALLOC_FREE(subreq
);
4584 if (tevent_req_nterror(req
, status
)) {
4587 tevent_req_done(req
);
4590 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4591 size_t *pnum_eas
, struct ea_struct
**peas
)
4593 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4594 req
, struct cli_get_ea_list_path_state
);
4597 if (tevent_req_is_nterror(req
, &status
)) {
4600 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4602 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4604 return NT_STATUS_OK
;
4607 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4610 struct ea_struct
**pea_list
)
4612 TALLOC_CTX
*frame
= NULL
;
4613 struct tevent_context
*ev
= NULL
;
4614 struct tevent_req
*req
= NULL
;
4615 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4617 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
4618 return cli_smb2_get_ea_list_path(cli
,
4625 frame
= talloc_stackframe();
4627 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4629 * Can't use sync call while an async call is in flight
4631 status
= NT_STATUS_INVALID_PARAMETER
;
4634 ev
= samba_tevent_context_init(frame
);
4638 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4642 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4645 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4651 /****************************************************************************
4652 Convert open "flags" arg to uint32_t on wire.
4653 ****************************************************************************/
4655 static uint32_t open_flags_to_wire(int flags
)
4657 int open_mode
= flags
& O_ACCMODE
;
4660 switch (open_mode
) {
4662 ret
|= SMB_O_WRONLY
;
4669 ret
|= SMB_O_RDONLY
;
4673 if (flags
& O_CREAT
) {
4676 if (flags
& O_EXCL
) {
4679 if (flags
& O_TRUNC
) {
4683 if (flags
& O_SYNC
) {
4687 if (flags
& O_APPEND
) {
4688 ret
|= SMB_O_APPEND
;
4690 #if defined(O_DIRECT)
4691 if (flags
& O_DIRECT
) {
4692 ret
|= SMB_O_DIRECT
;
4695 #if defined(O_DIRECTORY)
4696 if (flags
& O_DIRECTORY
) {
4697 ret
|= SMB_O_DIRECTORY
;
4703 /****************************************************************************
4704 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4705 ****************************************************************************/
4707 struct posix_open_state
{
4711 uint16_t fnum
; /* Out */
4714 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4716 struct tevent_req
*req
= tevent_req_callback_data(
4717 subreq
, struct tevent_req
);
4718 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4723 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4724 NULL
, 0, NULL
, &data
, 12, &num_data
);
4725 TALLOC_FREE(subreq
);
4726 if (tevent_req_nterror(req
, status
)) {
4729 state
->fnum
= SVAL(data
,2);
4730 tevent_req_done(req
);
4733 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4734 struct tevent_context
*ev
,
4735 struct cli_state
*cli
,
4741 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4742 struct posix_open_state
*state
= NULL
;
4743 uint32_t wire_flags
= open_flags_to_wire(flags
);
4745 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4750 /* Setup setup word. */
4751 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4753 /* Setup param array. */
4754 state
->param
= talloc_array(state
, uint8_t, 6);
4755 if (tevent_req_nomem(state
->param
, req
)) {
4756 return tevent_req_post(req
, ev
);
4758 memset(state
->param
, '\0', 6);
4759 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4761 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4762 strlen(fname
)+1, NULL
);
4764 if (tevent_req_nomem(state
->param
, req
)) {
4765 return tevent_req_post(req
, ev
);
4768 /* Setup data words. */
4770 wire_flags
|= SMB_O_DIRECTORY
;
4773 SIVAL(state
->data
,0,0); /* No oplock. */
4774 SIVAL(state
->data
,4,wire_flags
);
4775 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4776 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4777 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4779 subreq
= cli_trans_send(state
, /* mem ctx. */
4780 ev
, /* event ctx. */
4781 cli
, /* cli_state. */
4782 SMBtrans2
, /* cmd. */
4783 NULL
, /* pipe name. */
4787 &state
->setup
, /* setup. */
4788 1, /* num setup uint16_t words. */
4789 0, /* max returned setup. */
4790 state
->param
, /* param. */
4791 talloc_get_size(state
->param
),/* num param. */
4792 2, /* max returned param. */
4793 state
->data
, /* data. */
4795 12); /* max returned data. */
4797 if (tevent_req_nomem(subreq
, req
)) {
4798 return tevent_req_post(req
, ev
);
4800 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4804 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4805 struct tevent_context
*ev
,
4806 struct cli_state
*cli
,
4811 return cli_posix_open_internal_send(mem_ctx
, ev
,
4812 cli
, fname
, flags
, mode
, false);
4815 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4817 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4820 if (tevent_req_is_nterror(req
, &status
)) {
4823 *pfnum
= state
->fnum
;
4824 return NT_STATUS_OK
;
4827 /****************************************************************************
4828 Open - POSIX semantics. Doesn't request oplock.
4829 ****************************************************************************/
4831 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4832 int flags
, mode_t mode
, uint16_t *pfnum
)
4835 TALLOC_CTX
*frame
= talloc_stackframe();
4836 struct tevent_context
*ev
= NULL
;
4837 struct tevent_req
*req
= NULL
;
4838 NTSTATUS status
= NT_STATUS_OK
;
4840 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4842 * Can't use sync call while an async call is in flight
4844 status
= NT_STATUS_INVALID_PARAMETER
;
4848 ev
= samba_tevent_context_init(frame
);
4850 status
= NT_STATUS_NO_MEMORY
;
4854 req
= cli_posix_open_send(frame
,
4861 status
= NT_STATUS_NO_MEMORY
;
4865 if (!tevent_req_poll(req
, ev
)) {
4866 status
= map_nt_error_from_unix(errno
);
4870 status
= cli_posix_open_recv(req
, pfnum
);
4877 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4878 struct tevent_context
*ev
,
4879 struct cli_state
*cli
,
4883 return cli_posix_open_internal_send(mem_ctx
, ev
,
4884 cli
, fname
, O_CREAT
, mode
, true);
4887 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4889 return tevent_req_simple_recv_ntstatus(req
);
4892 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4894 TALLOC_CTX
*frame
= talloc_stackframe();
4895 struct tevent_context
*ev
= NULL
;
4896 struct tevent_req
*req
= NULL
;
4897 NTSTATUS status
= NT_STATUS_OK
;
4899 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4901 * Can't use sync call while an async call is in flight
4903 status
= NT_STATUS_INVALID_PARAMETER
;
4907 ev
= samba_tevent_context_init(frame
);
4909 status
= NT_STATUS_NO_MEMORY
;
4913 req
= cli_posix_mkdir_send(frame
,
4919 status
= NT_STATUS_NO_MEMORY
;
4923 if (!tevent_req_poll(req
, ev
)) {
4924 status
= map_nt_error_from_unix(errno
);
4928 status
= cli_posix_mkdir_recv(req
);
4935 /****************************************************************************
4936 unlink or rmdir - POSIX semantics.
4937 ****************************************************************************/
4939 struct cli_posix_unlink_internal_state
{
4943 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4945 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4946 struct tevent_context
*ev
,
4947 struct cli_state
*cli
,
4951 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4952 struct cli_posix_unlink_internal_state
*state
= NULL
;
4954 req
= tevent_req_create(mem_ctx
, &state
,
4955 struct cli_posix_unlink_internal_state
);
4960 /* Setup data word. */
4961 SSVAL(state
->data
, 0, level
);
4963 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4964 SMB_POSIX_PATH_UNLINK
,
4966 state
->data
, sizeof(state
->data
));
4967 if (tevent_req_nomem(subreq
, req
)) {
4968 return tevent_req_post(req
, ev
);
4970 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4974 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4976 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4977 tevent_req_simple_finish_ntstatus(subreq
, status
);
4980 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4981 struct tevent_context
*ev
,
4982 struct cli_state
*cli
,
4985 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4986 SMB_POSIX_UNLINK_FILE_TARGET
);
4989 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4991 return tevent_req_simple_recv_ntstatus(req
);
4994 /****************************************************************************
4995 unlink - POSIX semantics.
4996 ****************************************************************************/
4998 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
5000 TALLOC_CTX
*frame
= talloc_stackframe();
5001 struct tevent_context
*ev
= NULL
;
5002 struct tevent_req
*req
= NULL
;
5003 NTSTATUS status
= NT_STATUS_OK
;
5005 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5007 * Can't use sync call while an async call is in flight
5009 status
= NT_STATUS_INVALID_PARAMETER
;
5013 ev
= samba_tevent_context_init(frame
);
5015 status
= NT_STATUS_NO_MEMORY
;
5019 req
= cli_posix_unlink_send(frame
,
5024 status
= NT_STATUS_NO_MEMORY
;
5028 if (!tevent_req_poll(req
, ev
)) {
5029 status
= map_nt_error_from_unix(errno
);
5033 status
= cli_posix_unlink_recv(req
);
5040 /****************************************************************************
5041 rmdir - POSIX semantics.
5042 ****************************************************************************/
5044 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
5045 struct tevent_context
*ev
,
5046 struct cli_state
*cli
,
5049 return cli_posix_unlink_internal_send(
5050 mem_ctx
, ev
, cli
, fname
,
5051 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
5054 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
5056 return tevent_req_simple_recv_ntstatus(req
);
5059 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
5061 TALLOC_CTX
*frame
= talloc_stackframe();
5062 struct tevent_context
*ev
= NULL
;
5063 struct tevent_req
*req
= NULL
;
5064 NTSTATUS status
= NT_STATUS_OK
;
5066 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5068 * Can't use sync call while an async call is in flight
5070 status
= NT_STATUS_INVALID_PARAMETER
;
5074 ev
= samba_tevent_context_init(frame
);
5076 status
= NT_STATUS_NO_MEMORY
;
5080 req
= cli_posix_rmdir_send(frame
,
5085 status
= NT_STATUS_NO_MEMORY
;
5089 if (!tevent_req_poll(req
, ev
)) {
5090 status
= map_nt_error_from_unix(errno
);
5094 status
= cli_posix_rmdir_recv(req
, frame
);
5101 /****************************************************************************
5103 ****************************************************************************/
5105 struct cli_notify_state
{
5107 uint32_t num_changes
;
5108 struct notify_change
*changes
;
5111 static void cli_notify_done(struct tevent_req
*subreq
);
5113 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5114 struct tevent_context
*ev
,
5115 struct cli_state
*cli
, uint16_t fnum
,
5116 uint32_t buffer_size
,
5117 uint32_t completion_filter
, bool recursive
)
5119 struct tevent_req
*req
, *subreq
;
5120 struct cli_notify_state
*state
;
5121 unsigned old_timeout
;
5123 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5128 SIVAL(state
->setup
, 0, completion_filter
);
5129 SSVAL(state
->setup
, 4, fnum
);
5130 SSVAL(state
->setup
, 6, recursive
);
5133 * Notifies should not time out
5135 old_timeout
= cli_set_timeout(cli
, 0);
5137 subreq
= cli_trans_send(
5138 state
, /* mem ctx. */
5139 ev
, /* event ctx. */
5140 cli
, /* cli_state. */
5141 SMBnttrans
, /* cmd. */
5142 NULL
, /* pipe name. */
5144 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5146 (uint16_t *)state
->setup
, /* setup. */
5147 4, /* num setup uint16_t words. */
5148 0, /* max returned setup. */
5151 buffer_size
, /* max returned param. */
5154 0); /* max returned data. */
5156 cli_set_timeout(cli
, old_timeout
);
5158 if (tevent_req_nomem(subreq
, req
)) {
5159 return tevent_req_post(req
, ev
);
5161 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5165 static void cli_notify_done(struct tevent_req
*subreq
)
5167 struct tevent_req
*req
= tevent_req_callback_data(
5168 subreq
, struct tevent_req
);
5169 struct cli_notify_state
*state
= tevent_req_data(
5170 req
, struct cli_notify_state
);
5173 uint32_t i
, ofs
, num_params
;
5176 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5177 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5178 TALLOC_FREE(subreq
);
5179 if (tevent_req_nterror(req
, status
)) {
5180 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5184 state
->num_changes
= 0;
5187 while (num_params
- ofs
> 12) {
5188 uint32_t next
= IVAL(params
, ofs
);
5189 state
->num_changes
+= 1;
5191 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5197 state
->changes
= talloc_array(state
, struct notify_change
,
5198 state
->num_changes
);
5199 if (tevent_req_nomem(state
->changes
, req
)) {
5200 TALLOC_FREE(params
);
5206 for (i
=0; i
<state
->num_changes
; i
++) {
5207 uint32_t next
= IVAL(params
, ofs
);
5208 uint32_t len
= IVAL(params
, ofs
+8);
5212 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5213 TALLOC_FREE(params
);
5215 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5219 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5220 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5221 &name
, params
+ofs
+12, len
,
5222 STR_TERMINATE
|STR_UNICODE
);
5224 TALLOC_FREE(params
);
5225 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5228 state
->changes
[i
].name
= name
;
5232 TALLOC_FREE(params
);
5233 tevent_req_done(req
);
5236 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5237 uint32_t *pnum_changes
,
5238 struct notify_change
**pchanges
)
5240 struct cli_notify_state
*state
= tevent_req_data(
5241 req
, struct cli_notify_state
);
5244 if (tevent_req_is_nterror(req
, &status
)) {
5248 *pnum_changes
= state
->num_changes
;
5249 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5250 return NT_STATUS_OK
;
5253 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5254 uint32_t completion_filter
, bool recursive
,
5255 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5256 struct notify_change
**pchanges
)
5258 TALLOC_CTX
*frame
= talloc_stackframe();
5259 struct tevent_context
*ev
;
5260 struct tevent_req
*req
;
5261 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5263 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5265 * Can't use sync call while an async call is in flight
5267 status
= NT_STATUS_INVALID_PARAMETER
;
5270 ev
= samba_tevent_context_init(frame
);
5274 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5275 completion_filter
, recursive
);
5279 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5282 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5288 struct cli_qpathinfo_state
{
5297 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5299 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5300 struct tevent_context
*ev
,
5301 struct cli_state
*cli
, const char *fname
,
5302 uint16_t level
, uint32_t min_rdata
,
5305 struct tevent_req
*req
, *subreq
;
5306 struct cli_qpathinfo_state
*state
;
5308 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5312 state
->min_rdata
= min_rdata
;
5313 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5315 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5316 if (tevent_req_nomem(state
->param
, req
)) {
5317 return tevent_req_post(req
, ev
);
5319 SSVAL(state
->param
, 0, level
);
5320 state
->param
= trans2_bytes_push_str(
5321 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5322 if (tevent_req_nomem(state
->param
, req
)) {
5323 return tevent_req_post(req
, ev
);
5326 subreq
= cli_trans_send(
5327 state
, /* mem ctx. */
5328 ev
, /* event ctx. */
5329 cli
, /* cli_state. */
5330 SMBtrans2
, /* cmd. */
5331 NULL
, /* pipe name. */
5335 state
->setup
, /* setup. */
5336 1, /* num setup uint16_t words. */
5337 0, /* max returned setup. */
5338 state
->param
, /* param. */
5339 talloc_get_size(state
->param
), /* num param. */
5340 2, /* max returned param. */
5343 max_rdata
); /* max returned data. */
5345 if (tevent_req_nomem(subreq
, req
)) {
5346 return tevent_req_post(req
, ev
);
5348 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5352 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5354 struct tevent_req
*req
= tevent_req_callback_data(
5355 subreq
, struct tevent_req
);
5356 struct cli_qpathinfo_state
*state
= tevent_req_data(
5357 req
, struct cli_qpathinfo_state
);
5360 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5362 &state
->rdata
, state
->min_rdata
,
5364 if (tevent_req_nterror(req
, status
)) {
5367 tevent_req_done(req
);
5370 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5371 uint8_t **rdata
, uint32_t *num_rdata
)
5373 struct cli_qpathinfo_state
*state
= tevent_req_data(
5374 req
, struct cli_qpathinfo_state
);
5377 if (tevent_req_is_nterror(req
, &status
)) {
5380 if (rdata
!= NULL
) {
5381 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5383 TALLOC_FREE(state
->rdata
);
5385 if (num_rdata
!= NULL
) {
5386 *num_rdata
= state
->num_rdata
;
5388 return NT_STATUS_OK
;
5391 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5392 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5394 uint8_t **rdata
, uint32_t *num_rdata
)
5396 TALLOC_CTX
*frame
= talloc_stackframe();
5397 struct tevent_context
*ev
;
5398 struct tevent_req
*req
;
5399 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5401 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5403 * Can't use sync call while an async call is in flight
5405 status
= NT_STATUS_INVALID_PARAMETER
;
5408 ev
= samba_tevent_context_init(frame
);
5412 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5417 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5420 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5426 struct cli_qfileinfo_state
{
5430 uint16_t recv_flags2
;
5436 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5438 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5439 struct tevent_context
*ev
,
5440 struct cli_state
*cli
, uint16_t fnum
,
5441 uint16_t level
, uint32_t min_rdata
,
5444 struct tevent_req
*req
, *subreq
;
5445 struct cli_qfileinfo_state
*state
;
5447 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5451 state
->min_rdata
= min_rdata
;
5452 SSVAL(state
->param
, 0, fnum
);
5453 SSVAL(state
->param
, 2, level
);
5454 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5456 subreq
= cli_trans_send(
5457 state
, /* mem ctx. */
5458 ev
, /* event ctx. */
5459 cli
, /* cli_state. */
5460 SMBtrans2
, /* cmd. */
5461 NULL
, /* pipe name. */
5465 state
->setup
, /* setup. */
5466 1, /* num setup uint16_t words. */
5467 0, /* max returned setup. */
5468 state
->param
, /* param. */
5469 sizeof(state
->param
), /* num param. */
5470 2, /* max returned param. */
5473 max_rdata
); /* max returned data. */
5475 if (tevent_req_nomem(subreq
, req
)) {
5476 return tevent_req_post(req
, ev
);
5478 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5482 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5484 struct tevent_req
*req
= tevent_req_callback_data(
5485 subreq
, struct tevent_req
);
5486 struct cli_qfileinfo_state
*state
= tevent_req_data(
5487 req
, struct cli_qfileinfo_state
);
5490 status
= cli_trans_recv(subreq
, state
,
5491 &state
->recv_flags2
,
5494 &state
->rdata
, state
->min_rdata
,
5496 if (tevent_req_nterror(req
, status
)) {
5499 tevent_req_done(req
);
5502 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5503 uint16_t *recv_flags2
,
5504 uint8_t **rdata
, uint32_t *num_rdata
)
5506 struct cli_qfileinfo_state
*state
= tevent_req_data(
5507 req
, struct cli_qfileinfo_state
);
5510 if (tevent_req_is_nterror(req
, &status
)) {
5514 if (recv_flags2
!= NULL
) {
5515 *recv_flags2
= state
->recv_flags2
;
5517 if (rdata
!= NULL
) {
5518 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5520 TALLOC_FREE(state
->rdata
);
5522 if (num_rdata
!= NULL
) {
5523 *num_rdata
= state
->num_rdata
;
5525 return NT_STATUS_OK
;
5528 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5529 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5530 uint32_t max_rdata
, uint16_t *recv_flags2
,
5531 uint8_t **rdata
, uint32_t *num_rdata
)
5533 TALLOC_CTX
*frame
= talloc_stackframe();
5534 struct tevent_context
*ev
;
5535 struct tevent_req
*req
;
5536 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5538 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5540 * Can't use sync call while an async call is in flight
5542 status
= NT_STATUS_INVALID_PARAMETER
;
5545 ev
= samba_tevent_context_init(frame
);
5549 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5554 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5557 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5563 struct cli_flush_state
{
5567 static void cli_flush_done(struct tevent_req
*subreq
);
5569 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5570 struct tevent_context
*ev
,
5571 struct cli_state
*cli
,
5574 struct tevent_req
*req
, *subreq
;
5575 struct cli_flush_state
*state
;
5577 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5581 SSVAL(state
->vwv
+ 0, 0, fnum
);
5583 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5585 if (tevent_req_nomem(subreq
, req
)) {
5586 return tevent_req_post(req
, ev
);
5588 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5592 static void cli_flush_done(struct tevent_req
*subreq
)
5594 struct tevent_req
*req
= tevent_req_callback_data(
5595 subreq
, struct tevent_req
);
5598 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5599 TALLOC_FREE(subreq
);
5600 if (tevent_req_nterror(req
, status
)) {
5603 tevent_req_done(req
);
5606 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5608 return tevent_req_simple_recv_ntstatus(req
);
5611 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5613 TALLOC_CTX
*frame
= talloc_stackframe();
5614 struct tevent_context
*ev
;
5615 struct tevent_req
*req
;
5616 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5618 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5620 * Can't use sync call while an async call is in flight
5622 status
= NT_STATUS_INVALID_PARAMETER
;
5625 ev
= samba_tevent_context_init(frame
);
5629 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5633 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5636 status
= cli_flush_recv(req
);
5642 struct cli_shadow_copy_data_state
{
5649 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5651 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5652 struct tevent_context
*ev
,
5653 struct cli_state
*cli
,
5657 struct tevent_req
*req
, *subreq
;
5658 struct cli_shadow_copy_data_state
*state
;
5661 req
= tevent_req_create(mem_ctx
, &state
,
5662 struct cli_shadow_copy_data_state
);
5666 state
->get_names
= get_names
;
5667 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5669 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5670 SSVAL(state
->setup
+ 2, 0, fnum
);
5671 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5672 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5674 subreq
= cli_trans_send(
5675 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5676 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5679 if (tevent_req_nomem(subreq
, req
)) {
5680 return tevent_req_post(req
, ev
);
5682 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5686 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5688 struct tevent_req
*req
= tevent_req_callback_data(
5689 subreq
, struct tevent_req
);
5690 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5691 req
, struct cli_shadow_copy_data_state
);
5694 status
= cli_trans_recv(subreq
, state
, NULL
,
5695 NULL
, 0, NULL
, /* setup */
5696 NULL
, 0, NULL
, /* param */
5697 &state
->data
, 12, &state
->num_data
);
5698 TALLOC_FREE(subreq
);
5699 if (tevent_req_nterror(req
, status
)) {
5702 tevent_req_done(req
);
5705 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5706 char ***pnames
, int *pnum_names
)
5708 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5709 req
, struct cli_shadow_copy_data_state
);
5715 if (tevent_req_is_nterror(req
, &status
)) {
5718 num_names
= IVAL(state
->data
, 4);
5719 dlength
= IVAL(state
->data
, 8);
5721 if (!state
->get_names
) {
5722 *pnum_names
= num_names
;
5723 return NT_STATUS_OK
;
5726 if (dlength
+12 > state
->num_data
) {
5727 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5729 names
= talloc_array(mem_ctx
, char *, num_names
);
5730 if (names
== NULL
) {
5731 return NT_STATUS_NO_MEMORY
;
5734 for (i
=0; i
<num_names
; i
++) {
5737 size_t converted_size
;
5739 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5740 ret
= convert_string_talloc(
5741 names
, CH_UTF16LE
, CH_UNIX
,
5742 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5743 &names
[i
], &converted_size
);
5746 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5749 *pnum_names
= num_names
;
5751 return NT_STATUS_OK
;
5754 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5755 uint16_t fnum
, bool get_names
,
5756 char ***pnames
, int *pnum_names
)
5758 TALLOC_CTX
*frame
= talloc_stackframe();
5759 struct tevent_context
*ev
;
5760 struct tevent_req
*req
;
5761 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5763 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5765 * Can't use sync call while an async call is in flight
5767 status
= NT_STATUS_INVALID_PARAMETER
;
5770 ev
= samba_tevent_context_init(frame
);
5774 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5778 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5781 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);