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
{
1800 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1802 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1803 struct tevent_context
*ev
,
1804 struct cli_state
*cli
,
1806 uint32_t CreatFlags
,
1807 uint32_t DesiredAccess
,
1808 uint32_t FileAttributes
,
1809 uint32_t ShareAccess
,
1810 uint32_t CreateDisposition
,
1811 uint32_t CreateOptions
,
1812 uint8_t SecurityFlags
)
1814 struct tevent_req
*req
, *subreq
;
1815 struct cli_ntcreate_state
*state
;
1818 size_t converted_len
;
1820 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1827 SCVAL(vwv
+0, 0, 0xFF);
1832 if (cli
->use_oplocks
) {
1833 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1835 SIVAL(vwv
+3, 1, CreatFlags
);
1836 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1837 SIVAL(vwv
+7, 1, DesiredAccess
);
1838 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1839 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv
+13, 1, FileAttributes
);
1841 SIVAL(vwv
+15, 1, ShareAccess
);
1842 SIVAL(vwv
+17, 1, CreateDisposition
);
1843 SIVAL(vwv
+19, 1, CreateOptions
|
1844 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1845 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1846 SCVAL(vwv
+23, 1, SecurityFlags
);
1848 bytes
= talloc_array(state
, uint8_t, 0);
1849 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1850 fname
, strlen(fname
)+1,
1853 /* sigh. this copes with broken netapp filer behaviour */
1854 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1856 if (tevent_req_nomem(bytes
, req
)) {
1857 return tevent_req_post(req
, ev
);
1860 SSVAL(vwv
+2, 1, converted_len
);
1862 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1863 talloc_get_size(bytes
), bytes
);
1864 if (tevent_req_nomem(subreq
, req
)) {
1865 return tevent_req_post(req
, ev
);
1867 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1871 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1873 struct tevent_req
*req
= tevent_req_callback_data(
1874 subreq
, struct tevent_req
);
1875 struct cli_ntcreate_state
*state
= tevent_req_data(
1876 req
, struct cli_ntcreate_state
);
1883 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
,
1884 &num_bytes
, &bytes
);
1885 TALLOC_FREE(subreq
);
1886 if (tevent_req_nterror(req
, status
)) {
1889 state
->fnum
= SVAL(vwv
+2, 1);
1890 tevent_req_done(req
);
1893 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1895 struct cli_ntcreate_state
*state
= tevent_req_data(
1896 req
, struct cli_ntcreate_state
);
1899 if (tevent_req_is_nterror(req
, &status
)) {
1902 *pfnum
= state
->fnum
;
1903 return NT_STATUS_OK
;
1906 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1908 uint32_t CreatFlags
,
1909 uint32_t DesiredAccess
,
1910 uint32_t FileAttributes
,
1911 uint32_t ShareAccess
,
1912 uint32_t CreateDisposition
,
1913 uint32_t CreateOptions
,
1914 uint8_t SecurityFlags
,
1917 TALLOC_CTX
*frame
= NULL
;
1918 struct tevent_context
*ev
;
1919 struct tevent_req
*req
;
1920 NTSTATUS status
= NT_STATUS_OK
;
1922 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1923 return cli_smb2_create_fnum(cli
,
1935 frame
= talloc_stackframe();
1937 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1939 * Can't use sync call while an async call is in flight
1941 status
= NT_STATUS_INVALID_PARAMETER
;
1945 ev
= samba_tevent_context_init(frame
);
1947 status
= NT_STATUS_NO_MEMORY
;
1951 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1952 DesiredAccess
, FileAttributes
, ShareAccess
,
1953 CreateDisposition
, CreateOptions
,
1956 status
= NT_STATUS_NO_MEMORY
;
1960 if (!tevent_req_poll(req
, ev
)) {
1961 status
= map_nt_error_from_unix(errno
);
1965 status
= cli_ntcreate_recv(req
, pfid
);
1971 struct cli_nttrans_create_state
{
1975 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1977 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1978 struct tevent_context
*ev
,
1979 struct cli_state
*cli
,
1981 uint32_t CreatFlags
,
1982 uint32_t DesiredAccess
,
1983 uint32_t FileAttributes
,
1984 uint32_t ShareAccess
,
1985 uint32_t CreateDisposition
,
1986 uint32_t CreateOptions
,
1987 uint8_t SecurityFlags
,
1988 struct security_descriptor
*secdesc
,
1989 struct ea_struct
*eas
,
1992 struct tevent_req
*req
, *subreq
;
1993 struct cli_nttrans_create_state
*state
;
1995 uint8_t *secdesc_buf
;
1998 size_t converted_len
;
2000 req
= tevent_req_create(mem_ctx
,
2001 &state
, struct cli_nttrans_create_state
);
2006 if (secdesc
!= NULL
) {
2007 status
= marshall_sec_desc(talloc_tos(), secdesc
,
2008 &secdesc_buf
, &secdesc_len
);
2009 if (tevent_req_nterror(req
, status
)) {
2010 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2011 nt_errstr(status
)));
2012 return tevent_req_post(req
, ev
);
2023 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2024 return tevent_req_post(req
, ev
);
2027 param
= talloc_array(state
, uint8_t, 53);
2028 if (tevent_req_nomem(param
, req
)) {
2029 return tevent_req_post(req
, ev
);
2032 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2033 fname
, strlen(fname
),
2035 if (tevent_req_nomem(param
, req
)) {
2036 return tevent_req_post(req
, ev
);
2039 SIVAL(param
, 0, CreatFlags
);
2040 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2041 SIVAL(param
, 8, DesiredAccess
);
2042 SIVAL(param
, 12, 0x0); /* AllocationSize */
2043 SIVAL(param
, 16, 0x0); /* AllocationSize */
2044 SIVAL(param
, 20, FileAttributes
);
2045 SIVAL(param
, 24, ShareAccess
);
2046 SIVAL(param
, 28, CreateDisposition
);
2047 SIVAL(param
, 32, CreateOptions
|
2048 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2049 SIVAL(param
, 36, secdesc_len
);
2050 SIVAL(param
, 40, 0); /* EA length*/
2051 SIVAL(param
, 44, converted_len
);
2052 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2053 SCVAL(param
, 52, SecurityFlags
);
2055 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2056 NULL
, -1, /* name, fid */
2057 NT_TRANSACT_CREATE
, 0,
2058 NULL
, 0, 0, /* setup */
2059 param
, talloc_get_size(param
), 128, /* param */
2060 secdesc_buf
, secdesc_len
, 0); /* data */
2061 if (tevent_req_nomem(subreq
, req
)) {
2062 return tevent_req_post(req
, ev
);
2064 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2068 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2070 struct tevent_req
*req
= tevent_req_callback_data(
2071 subreq
, struct tevent_req
);
2072 struct cli_nttrans_create_state
*state
= tevent_req_data(
2073 req
, struct cli_nttrans_create_state
);
2078 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2079 NULL
, 0, NULL
, /* rsetup */
2080 ¶m
, 69, &num_param
,
2082 if (tevent_req_nterror(req
, status
)) {
2085 state
->fnum
= SVAL(param
, 2);
2087 tevent_req_done(req
);
2090 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
, uint16_t *fnum
)
2092 struct cli_nttrans_create_state
*state
= tevent_req_data(
2093 req
, struct cli_nttrans_create_state
);
2096 if (tevent_req_is_nterror(req
, &status
)) {
2099 *fnum
= state
->fnum
;
2100 return NT_STATUS_OK
;
2103 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2105 uint32_t CreatFlags
,
2106 uint32_t DesiredAccess
,
2107 uint32_t FileAttributes
,
2108 uint32_t ShareAccess
,
2109 uint32_t CreateDisposition
,
2110 uint32_t CreateOptions
,
2111 uint8_t SecurityFlags
,
2112 struct security_descriptor
*secdesc
,
2113 struct ea_struct
*eas
,
2117 TALLOC_CTX
*frame
= talloc_stackframe();
2118 struct tevent_context
*ev
;
2119 struct tevent_req
*req
;
2120 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2122 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2124 * Can't use sync call while an async call is in flight
2126 status
= NT_STATUS_INVALID_PARAMETER
;
2129 ev
= samba_tevent_context_init(frame
);
2133 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2134 DesiredAccess
, FileAttributes
,
2135 ShareAccess
, CreateDisposition
,
2136 CreateOptions
, SecurityFlags
,
2137 secdesc
, eas
, num_eas
);
2141 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2144 status
= cli_nttrans_create_recv(req
, pfid
);
2150 /****************************************************************************
2152 WARNING: if you open with O_WRONLY then getattrE won't work!
2153 ****************************************************************************/
2155 struct cli_openx_state
{
2162 static void cli_openx_done(struct tevent_req
*subreq
);
2164 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2165 struct tevent_context
*ev
,
2166 struct cli_state
*cli
, const char *fname
,
2167 int flags
, int share_mode
,
2168 struct tevent_req
**psmbreq
)
2170 struct tevent_req
*req
, *subreq
;
2171 struct cli_openx_state
*state
;
2173 unsigned accessmode
;
2174 uint8_t additional_flags
;
2177 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2183 if (flags
& O_CREAT
) {
2186 if (!(flags
& O_EXCL
)) {
2187 if (flags
& O_TRUNC
)
2193 accessmode
= (share_mode
<<4);
2195 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2197 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2202 if ((flags
& O_SYNC
) == O_SYNC
) {
2203 accessmode
|= (1<<14);
2207 if (share_mode
== DENY_FCB
) {
2211 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2212 SCVAL(state
->vwv
+ 0, 1, 0);
2213 SSVAL(state
->vwv
+ 1, 0, 0);
2214 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2215 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2216 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2217 SSVAL(state
->vwv
+ 5, 0, 0);
2218 SIVAL(state
->vwv
+ 6, 0, 0);
2219 SSVAL(state
->vwv
+ 8, 0, openfn
);
2220 SIVAL(state
->vwv
+ 9, 0, 0);
2221 SIVAL(state
->vwv
+ 11, 0, 0);
2222 SIVAL(state
->vwv
+ 13, 0, 0);
2224 additional_flags
= 0;
2226 if (cli
->use_oplocks
) {
2227 /* if using oplocks then ask for a batch oplock via
2228 core and extended methods */
2230 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2231 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2234 bytes
= talloc_array(state
, uint8_t, 0);
2235 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2236 strlen(fname
)+1, NULL
);
2238 if (tevent_req_nomem(bytes
, req
)) {
2239 return tevent_req_post(req
, ev
);
2242 state
->bytes
.iov_base
= (void *)bytes
;
2243 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2245 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2246 15, state
->vwv
, 1, &state
->bytes
);
2247 if (subreq
== NULL
) {
2251 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2256 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2257 struct cli_state
*cli
, const char *fname
,
2258 int flags
, int share_mode
)
2260 struct tevent_req
*req
, *subreq
;
2263 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2269 status
= smb1cli_req_chain_submit(&subreq
, 1);
2270 if (tevent_req_nterror(req
, status
)) {
2271 return tevent_req_post(req
, ev
);
2276 static void cli_openx_done(struct tevent_req
*subreq
)
2278 struct tevent_req
*req
= tevent_req_callback_data(
2279 subreq
, struct tevent_req
);
2280 struct cli_openx_state
*state
= tevent_req_data(
2281 req
, struct cli_openx_state
);
2286 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2288 TALLOC_FREE(subreq
);
2289 if (tevent_req_nterror(req
, status
)) {
2292 state
->fnum
= SVAL(vwv
+2, 0);
2293 tevent_req_done(req
);
2296 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2298 struct cli_openx_state
*state
= tevent_req_data(
2299 req
, struct cli_openx_state
);
2302 if (tevent_req_is_nterror(req
, &status
)) {
2305 *pfnum
= state
->fnum
;
2306 return NT_STATUS_OK
;
2309 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2310 int share_mode
, uint16_t *pfnum
)
2312 TALLOC_CTX
*frame
= talloc_stackframe();
2313 struct tevent_context
*ev
;
2314 struct tevent_req
*req
;
2315 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2317 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2319 * Can't use sync call while an async call is in flight
2321 status
= NT_STATUS_INVALID_PARAMETER
;
2325 ev
= samba_tevent_context_init(frame
);
2330 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2335 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2339 status
= cli_openx_recv(req
, pfnum
);
2344 /****************************************************************************
2345 Synchronous wrapper function that does an NtCreateX open by preference
2346 and falls back to openX if this fails.
2347 ****************************************************************************/
2349 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2350 int share_mode_in
, uint16_t *pfnum
)
2353 unsigned int openfn
= 0;
2354 unsigned int dos_deny
= 0;
2355 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2357 /* Do the initial mapping into OpenX parameters. */
2358 if (flags
& O_CREAT
) {
2361 if (!(flags
& O_EXCL
)) {
2362 if (flags
& O_TRUNC
)
2368 dos_deny
= (share_mode_in
<<4);
2370 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2372 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2377 if ((flags
& O_SYNC
) == O_SYNC
) {
2378 dos_deny
|= (1<<14);
2382 if (share_mode_in
== DENY_FCB
) {
2387 /* Hmmm. This is what I think the above code
2388 should look like if it's using the constants
2391 if (flags
& O_CREAT
) {
2392 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2394 if (!(flags
& O_EXCL
)) {
2395 if (flags
& O_TRUNC
)
2396 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2398 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2401 dos_deny
= SET_DENY_MODE(share_mode_in
);
2403 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2404 dos_deny
|= DOS_OPEN_RDWR
;
2405 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2406 dos_deny
|= DOS_OPEN_WRONLY
;
2410 if ((flags
& O_SYNC
) == O_SYNC
) {
2411 dos_deny
|= FILE_SYNC_OPENMODE
;
2415 if (share_mode_in
== DENY_FCB
) {
2420 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2421 openfn
, &access_mask
,
2422 &share_mode
, &create_disposition
,
2423 &create_options
, NULL
)) {
2427 status
= cli_ntcreate(cli
,
2438 /* Try and cope will all varients of "we don't do this call"
2439 and fall back to openX. */
2441 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2442 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2443 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2444 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2445 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2446 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2447 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2448 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2449 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2457 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2460 /****************************************************************************
2462 ****************************************************************************/
2464 struct cli_close_state
{
2468 static void cli_close_done(struct tevent_req
*subreq
);
2470 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2471 struct tevent_context
*ev
,
2472 struct cli_state
*cli
,
2474 struct tevent_req
**psubreq
)
2476 struct tevent_req
*req
, *subreq
;
2477 struct cli_close_state
*state
;
2479 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2484 SSVAL(state
->vwv
+0, 0, fnum
);
2485 SIVALS(state
->vwv
+1, 0, -1);
2487 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2489 if (subreq
== NULL
) {
2493 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2498 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2499 struct tevent_context
*ev
,
2500 struct cli_state
*cli
,
2503 struct tevent_req
*req
, *subreq
;
2506 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2511 status
= smb1cli_req_chain_submit(&subreq
, 1);
2512 if (tevent_req_nterror(req
, status
)) {
2513 return tevent_req_post(req
, ev
);
2518 static void cli_close_done(struct tevent_req
*subreq
)
2520 struct tevent_req
*req
= tevent_req_callback_data(
2521 subreq
, struct tevent_req
);
2524 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2525 TALLOC_FREE(subreq
);
2526 if (tevent_req_nterror(req
, status
)) {
2529 tevent_req_done(req
);
2532 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2534 return tevent_req_simple_recv_ntstatus(req
);
2537 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2539 TALLOC_CTX
*frame
= NULL
;
2540 struct tevent_context
*ev
;
2541 struct tevent_req
*req
;
2542 NTSTATUS status
= NT_STATUS_OK
;
2544 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
2545 return cli_smb2_close_fnum(cli
, fnum
);
2548 frame
= talloc_stackframe();
2550 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2552 * Can't use sync call while an async call is in flight
2554 status
= NT_STATUS_INVALID_PARAMETER
;
2558 ev
= samba_tevent_context_init(frame
);
2560 status
= NT_STATUS_NO_MEMORY
;
2564 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2566 status
= NT_STATUS_NO_MEMORY
;
2570 if (!tevent_req_poll(req
, ev
)) {
2571 status
= map_nt_error_from_unix(errno
);
2575 status
= cli_close_recv(req
);
2581 /****************************************************************************
2582 Truncate a file to a specified size
2583 ****************************************************************************/
2585 struct ftrunc_state
{
2591 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2593 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2594 NULL
, 0, NULL
, NULL
, 0, NULL
);
2595 tevent_req_simple_finish_ntstatus(subreq
, status
);
2598 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2599 struct tevent_context
*ev
,
2600 struct cli_state
*cli
,
2604 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2605 struct ftrunc_state
*state
= NULL
;
2607 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2612 /* Setup setup word. */
2613 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2615 /* Setup param array. */
2616 SSVAL(state
->param
,0,fnum
);
2617 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2618 SSVAL(state
->param
,4,0);
2620 /* Setup data array. */
2621 SBVAL(state
->data
, 0, size
);
2623 subreq
= cli_trans_send(state
, /* mem ctx. */
2624 ev
, /* event ctx. */
2625 cli
, /* cli_state. */
2626 SMBtrans2
, /* cmd. */
2627 NULL
, /* pipe name. */
2631 &state
->setup
, /* setup. */
2632 1, /* num setup uint16_t words. */
2633 0, /* max returned setup. */
2634 state
->param
, /* param. */
2636 2, /* max returned param. */
2637 state
->data
, /* data. */
2639 0); /* max returned data. */
2641 if (tevent_req_nomem(subreq
, req
)) {
2642 return tevent_req_post(req
, ev
);
2644 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2648 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2650 return tevent_req_simple_recv_ntstatus(req
);
2653 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2655 TALLOC_CTX
*frame
= talloc_stackframe();
2656 struct tevent_context
*ev
= NULL
;
2657 struct tevent_req
*req
= NULL
;
2658 NTSTATUS status
= NT_STATUS_OK
;
2660 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2662 * Can't use sync call while an async call is in flight
2664 status
= NT_STATUS_INVALID_PARAMETER
;
2668 ev
= samba_tevent_context_init(frame
);
2670 status
= NT_STATUS_NO_MEMORY
;
2674 req
= cli_ftruncate_send(frame
,
2680 status
= NT_STATUS_NO_MEMORY
;
2684 if (!tevent_req_poll(req
, ev
)) {
2685 status
= map_nt_error_from_unix(errno
);
2689 status
= cli_ftruncate_recv(req
);
2696 /****************************************************************************
2697 send a lock with a specified locktype
2698 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2699 ****************************************************************************/
2701 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2702 uint32_t offset
, uint32_t len
,
2703 int timeout
, unsigned char locktype
)
2708 unsigned int set_timeout
= 0;
2709 unsigned int saved_timeout
= 0;
2711 SCVAL(vwv
+ 0, 0, 0xff);
2712 SCVAL(vwv
+ 0, 1, 0);
2713 SSVAL(vwv
+ 1, 0, 0);
2714 SSVAL(vwv
+ 2, 0, fnum
);
2715 SCVAL(vwv
+ 3, 0, locktype
);
2716 SCVAL(vwv
+ 3, 1, 0);
2717 SIVALS(vwv
+ 4, 0, timeout
);
2718 SSVAL(vwv
+ 6, 0, 0);
2719 SSVAL(vwv
+ 7, 0, 1);
2721 SSVAL(bytes
, 0, cli_getpid(cli
));
2722 SIVAL(bytes
, 2, offset
);
2723 SIVAL(bytes
, 6, len
);
2726 if (timeout
== -1) {
2727 set_timeout
= 0x7FFFFFFF;
2729 set_timeout
= timeout
+ 2*1000;
2731 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2734 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2735 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2737 if (saved_timeout
!= 0) {
2738 cli_set_timeout(cli
, saved_timeout
);
2744 /****************************************************************************
2746 note that timeout is in units of 2 milliseconds
2747 ****************************************************************************/
2749 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2750 uint32_t offset
, uint32_t len
, int timeout
,
2751 enum brl_type lock_type
)
2755 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2756 (lock_type
== READ_LOCK
? 1 : 0));
2760 /****************************************************************************
2762 ****************************************************************************/
2764 struct cli_unlock_state
{
2769 static void cli_unlock_done(struct tevent_req
*subreq
);
2771 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2772 struct tevent_context
*ev
,
2773 struct cli_state
*cli
,
2779 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2780 struct cli_unlock_state
*state
= NULL
;
2781 uint8_t additional_flags
= 0;
2783 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2788 SCVAL(state
->vwv
+0, 0, 0xFF);
2789 SSVAL(state
->vwv
+2, 0, fnum
);
2790 SCVAL(state
->vwv
+3, 0, 0);
2791 SIVALS(state
->vwv
+4, 0, 0);
2792 SSVAL(state
->vwv
+6, 0, 1);
2793 SSVAL(state
->vwv
+7, 0, 0);
2795 SSVAL(state
->data
, 0, cli_getpid(cli
));
2796 SIVAL(state
->data
, 2, offset
);
2797 SIVAL(state
->data
, 6, len
);
2799 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2800 8, state
->vwv
, 10, state
->data
);
2801 if (tevent_req_nomem(subreq
, req
)) {
2802 return tevent_req_post(req
, ev
);
2804 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2808 static void cli_unlock_done(struct tevent_req
*subreq
)
2810 struct tevent_req
*req
= tevent_req_callback_data(
2811 subreq
, struct tevent_req
);
2814 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2815 TALLOC_FREE(subreq
);
2816 if (tevent_req_nterror(req
, status
)) {
2819 tevent_req_done(req
);
2822 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2824 return tevent_req_simple_recv_ntstatus(req
);
2827 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2832 TALLOC_CTX
*frame
= talloc_stackframe();
2833 struct tevent_context
*ev
;
2834 struct tevent_req
*req
;
2835 NTSTATUS status
= NT_STATUS_OK
;
2837 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2839 * Can't use sync call while an async call is in flight
2841 status
= NT_STATUS_INVALID_PARAMETER
;
2845 ev
= samba_tevent_context_init(frame
);
2847 status
= NT_STATUS_NO_MEMORY
;
2851 req
= cli_unlock_send(frame
, ev
, cli
,
2854 status
= NT_STATUS_NO_MEMORY
;
2858 if (!tevent_req_poll(req
, ev
)) {
2859 status
= map_nt_error_from_unix(errno
);
2863 status
= cli_unlock_recv(req
);
2870 /****************************************************************************
2871 Lock a file with 64 bit offsets.
2872 ****************************************************************************/
2874 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2875 uint64_t offset
, uint64_t len
, int timeout
,
2876 enum brl_type lock_type
)
2880 unsigned int set_timeout
= 0;
2881 unsigned int saved_timeout
= 0;
2885 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2886 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2889 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2890 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2892 SCVAL(vwv
+ 0, 0, 0xff);
2893 SCVAL(vwv
+ 0, 1, 0);
2894 SSVAL(vwv
+ 1, 0, 0);
2895 SSVAL(vwv
+ 2, 0, fnum
);
2896 SCVAL(vwv
+ 3, 0, ltype
);
2897 SCVAL(vwv
+ 3, 1, 0);
2898 SIVALS(vwv
+ 4, 0, timeout
);
2899 SSVAL(vwv
+ 6, 0, 0);
2900 SSVAL(vwv
+ 7, 0, 1);
2902 SIVAL(bytes
, 0, cli_getpid(cli
));
2903 SOFF_T_R(bytes
, 4, offset
);
2904 SOFF_T_R(bytes
, 12, len
);
2907 if (timeout
== -1) {
2908 set_timeout
= 0x7FFFFFFF;
2910 set_timeout
= timeout
+ 2*1000;
2912 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2915 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2916 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2918 if (saved_timeout
!= 0) {
2919 cli_set_timeout(cli
, saved_timeout
);
2925 /****************************************************************************
2926 Unlock a file with 64 bit offsets.
2927 ****************************************************************************/
2929 struct cli_unlock64_state
{
2934 static void cli_unlock64_done(struct tevent_req
*subreq
);
2936 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2937 struct tevent_context
*ev
,
2938 struct cli_state
*cli
,
2944 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2945 struct cli_unlock64_state
*state
= NULL
;
2946 uint8_t additional_flags
= 0;
2948 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2953 SCVAL(state
->vwv
+0, 0, 0xff);
2954 SSVAL(state
->vwv
+2, 0, fnum
);
2955 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2956 SIVALS(state
->vwv
+4, 0, 0);
2957 SSVAL(state
->vwv
+6, 0, 1);
2958 SSVAL(state
->vwv
+7, 0, 0);
2960 SIVAL(state
->data
, 0, cli_getpid(cli
));
2961 SOFF_T_R(state
->data
, 4, offset
);
2962 SOFF_T_R(state
->data
, 12, len
);
2964 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2965 8, state
->vwv
, 20, state
->data
);
2966 if (tevent_req_nomem(subreq
, req
)) {
2967 return tevent_req_post(req
, ev
);
2969 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2973 static void cli_unlock64_done(struct tevent_req
*subreq
)
2975 struct tevent_req
*req
= tevent_req_callback_data(
2976 subreq
, struct tevent_req
);
2979 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2980 TALLOC_FREE(subreq
);
2981 if (tevent_req_nterror(req
, status
)) {
2984 tevent_req_done(req
);
2987 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2989 return tevent_req_simple_recv_ntstatus(req
);
2992 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2997 TALLOC_CTX
*frame
= talloc_stackframe();
2998 struct tevent_context
*ev
;
2999 struct tevent_req
*req
;
3000 NTSTATUS status
= NT_STATUS_OK
;
3002 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
3003 return cli_unlock(cli
, fnum
, offset
, len
);
3006 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3008 * Can't use sync call while an async call is in flight
3010 status
= NT_STATUS_INVALID_PARAMETER
;
3014 ev
= samba_tevent_context_init(frame
);
3016 status
= NT_STATUS_NO_MEMORY
;
3020 req
= cli_unlock64_send(frame
, ev
, cli
,
3023 status
= NT_STATUS_NO_MEMORY
;
3027 if (!tevent_req_poll(req
, ev
)) {
3028 status
= map_nt_error_from_unix(errno
);
3032 status
= cli_unlock64_recv(req
);
3039 /****************************************************************************
3040 Get/unlock a POSIX lock on a file - internal function.
3041 ****************************************************************************/
3043 struct posix_lock_state
{
3046 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3049 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3051 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3052 NULL
, 0, NULL
, NULL
, 0, NULL
);
3053 tevent_req_simple_finish_ntstatus(subreq
, status
);
3056 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3057 struct tevent_context
*ev
,
3058 struct cli_state
*cli
,
3063 enum brl_type lock_type
)
3065 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3066 struct posix_lock_state
*state
= NULL
;
3068 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3073 /* Setup setup word. */
3074 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3076 /* Setup param array. */
3077 SSVAL(&state
->param
, 0, fnum
);
3078 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3080 /* Setup data array. */
3081 switch (lock_type
) {
3083 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3084 POSIX_LOCK_TYPE_READ
);
3087 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3088 POSIX_LOCK_TYPE_WRITE
);
3091 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3092 POSIX_LOCK_TYPE_UNLOCK
);
3099 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3100 POSIX_LOCK_FLAG_WAIT
);
3102 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3103 POSIX_LOCK_FLAG_NOWAIT
);
3106 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3107 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3108 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3110 subreq
= cli_trans_send(state
, /* mem ctx. */
3111 ev
, /* event ctx. */
3112 cli
, /* cli_state. */
3113 SMBtrans2
, /* cmd. */
3114 NULL
, /* pipe name. */
3118 &state
->setup
, /* setup. */
3119 1, /* num setup uint16_t words. */
3120 0, /* max returned setup. */
3121 state
->param
, /* param. */
3123 2, /* max returned param. */
3124 state
->data
, /* data. */
3125 POSIX_LOCK_DATA_SIZE
, /* num data. */
3126 0); /* max returned data. */
3128 if (tevent_req_nomem(subreq
, req
)) {
3129 return tevent_req_post(req
, ev
);
3131 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3135 /****************************************************************************
3137 ****************************************************************************/
3139 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3140 struct tevent_context
*ev
,
3141 struct cli_state
*cli
,
3146 enum brl_type lock_type
)
3148 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3149 wait_lock
, lock_type
);
3152 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3154 return tevent_req_simple_recv_ntstatus(req
);
3157 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3158 uint64_t offset
, uint64_t len
,
3159 bool wait_lock
, enum brl_type lock_type
)
3161 TALLOC_CTX
*frame
= talloc_stackframe();
3162 struct tevent_context
*ev
= NULL
;
3163 struct tevent_req
*req
= NULL
;
3164 NTSTATUS status
= NT_STATUS_OK
;
3166 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3168 * Can't use sync call while an async call is in flight
3170 status
= NT_STATUS_INVALID_PARAMETER
;
3174 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3175 status
= NT_STATUS_INVALID_PARAMETER
;
3179 ev
= samba_tevent_context_init(frame
);
3181 status
= NT_STATUS_NO_MEMORY
;
3185 req
= cli_posix_lock_send(frame
,
3194 status
= NT_STATUS_NO_MEMORY
;
3198 if (!tevent_req_poll(req
, ev
)) {
3199 status
= map_nt_error_from_unix(errno
);
3203 status
= cli_posix_lock_recv(req
);
3210 /****************************************************************************
3211 POSIX Unlock a file.
3212 ****************************************************************************/
3214 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3215 struct tevent_context
*ev
,
3216 struct cli_state
*cli
,
3221 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3222 false, UNLOCK_LOCK
);
3225 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3227 return tevent_req_simple_recv_ntstatus(req
);
3230 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3232 TALLOC_CTX
*frame
= talloc_stackframe();
3233 struct tevent_context
*ev
= NULL
;
3234 struct tevent_req
*req
= NULL
;
3235 NTSTATUS status
= NT_STATUS_OK
;
3237 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3239 * Can't use sync call while an async call is in flight
3241 status
= NT_STATUS_INVALID_PARAMETER
;
3245 ev
= samba_tevent_context_init(frame
);
3247 status
= NT_STATUS_NO_MEMORY
;
3251 req
= cli_posix_unlock_send(frame
,
3258 status
= NT_STATUS_NO_MEMORY
;
3262 if (!tevent_req_poll(req
, ev
)) {
3263 status
= map_nt_error_from_unix(errno
);
3267 status
= cli_posix_unlock_recv(req
);
3274 /****************************************************************************
3275 Do a SMBgetattrE call.
3276 ****************************************************************************/
3278 static void cli_getattrE_done(struct tevent_req
*subreq
);
3280 struct cli_getattrE_state
{
3290 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3291 struct tevent_context
*ev
,
3292 struct cli_state
*cli
,
3295 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3296 struct cli_getattrE_state
*state
= NULL
;
3297 uint8_t additional_flags
= 0;
3299 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3304 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3305 SSVAL(state
->vwv
+0,0,fnum
);
3307 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3308 1, state
->vwv
, 0, NULL
);
3309 if (tevent_req_nomem(subreq
, req
)) {
3310 return tevent_req_post(req
, ev
);
3312 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3316 static void cli_getattrE_done(struct tevent_req
*subreq
)
3318 struct tevent_req
*req
= tevent_req_callback_data(
3319 subreq
, struct tevent_req
);
3320 struct cli_getattrE_state
*state
= tevent_req_data(
3321 req
, struct cli_getattrE_state
);
3323 uint16_t *vwv
= NULL
;
3326 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3328 TALLOC_FREE(subreq
);
3329 if (tevent_req_nterror(req
, status
)) {
3333 state
->size
= (off_t
)IVAL(vwv
+6,0);
3334 state
->attr
= SVAL(vwv
+10,0);
3335 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3336 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3337 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3339 tevent_req_done(req
);
3342 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3345 time_t *change_time
,
3346 time_t *access_time
,
3349 struct cli_getattrE_state
*state
= tevent_req_data(
3350 req
, struct cli_getattrE_state
);
3353 if (tevent_req_is_nterror(req
, &status
)) {
3357 *attr
= state
->attr
;
3360 *size
= state
->size
;
3363 *change_time
= state
->change_time
;
3366 *access_time
= state
->access_time
;
3369 *write_time
= state
->write_time
;
3371 return NT_STATUS_OK
;
3374 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3378 time_t *change_time
,
3379 time_t *access_time
,
3382 TALLOC_CTX
*frame
= NULL
;
3383 struct tevent_context
*ev
= NULL
;
3384 struct tevent_req
*req
= NULL
;
3385 NTSTATUS status
= NT_STATUS_OK
;
3387 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
3388 return cli_smb2_getattrE(cli
,
3397 frame
= talloc_stackframe();
3399 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3401 * Can't use sync call while an async call is in flight
3403 status
= NT_STATUS_INVALID_PARAMETER
;
3407 ev
= samba_tevent_context_init(frame
);
3409 status
= NT_STATUS_NO_MEMORY
;
3413 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3415 status
= NT_STATUS_NO_MEMORY
;
3419 if (!tevent_req_poll(req
, ev
)) {
3420 status
= map_nt_error_from_unix(errno
);
3424 status
= cli_getattrE_recv(req
,
3436 /****************************************************************************
3438 ****************************************************************************/
3440 static void cli_getatr_done(struct tevent_req
*subreq
);
3442 struct cli_getatr_state
{
3449 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3450 struct tevent_context
*ev
,
3451 struct cli_state
*cli
,
3454 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3455 struct cli_getatr_state
*state
= NULL
;
3456 uint8_t additional_flags
= 0;
3457 uint8_t *bytes
= NULL
;
3459 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3464 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3466 bytes
= talloc_array(state
, uint8_t, 1);
3467 if (tevent_req_nomem(bytes
, req
)) {
3468 return tevent_req_post(req
, ev
);
3471 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3472 strlen(fname
)+1, NULL
);
3474 if (tevent_req_nomem(bytes
, req
)) {
3475 return tevent_req_post(req
, ev
);
3478 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3479 0, NULL
, talloc_get_size(bytes
), bytes
);
3480 if (tevent_req_nomem(subreq
, req
)) {
3481 return tevent_req_post(req
, ev
);
3483 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3487 static void cli_getatr_done(struct tevent_req
*subreq
)
3489 struct tevent_req
*req
= tevent_req_callback_data(
3490 subreq
, struct tevent_req
);
3491 struct cli_getatr_state
*state
= tevent_req_data(
3492 req
, struct cli_getatr_state
);
3494 uint16_t *vwv
= NULL
;
3497 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3499 TALLOC_FREE(subreq
);
3500 if (tevent_req_nterror(req
, status
)) {
3504 state
->attr
= SVAL(vwv
+0,0);
3505 state
->size
= (off_t
)IVAL(vwv
+3,0);
3506 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3508 tevent_req_done(req
);
3511 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3516 struct cli_getatr_state
*state
= tevent_req_data(
3517 req
, struct cli_getatr_state
);
3520 if (tevent_req_is_nterror(req
, &status
)) {
3524 *attr
= state
->attr
;
3527 *size
= state
->size
;
3530 *write_time
= state
->write_time
;
3532 return NT_STATUS_OK
;
3535 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3541 TALLOC_CTX
*frame
= talloc_stackframe();
3542 struct tevent_context
*ev
= NULL
;
3543 struct tevent_req
*req
= NULL
;
3544 NTSTATUS status
= NT_STATUS_OK
;
3546 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3548 * Can't use sync call while an async call is in flight
3550 status
= NT_STATUS_INVALID_PARAMETER
;
3554 ev
= samba_tevent_context_init(frame
);
3556 status
= NT_STATUS_NO_MEMORY
;
3560 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3562 status
= NT_STATUS_NO_MEMORY
;
3566 if (!tevent_req_poll(req
, ev
)) {
3567 status
= map_nt_error_from_unix(errno
);
3571 status
= cli_getatr_recv(req
,
3581 /****************************************************************************
3582 Do a SMBsetattrE call.
3583 ****************************************************************************/
3585 static void cli_setattrE_done(struct tevent_req
*subreq
);
3587 struct cli_setattrE_state
{
3591 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3592 struct tevent_context
*ev
,
3593 struct cli_state
*cli
,
3599 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3600 struct cli_setattrE_state
*state
= NULL
;
3601 uint8_t additional_flags
= 0;
3603 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3608 SSVAL(state
->vwv
+0, 0, fnum
);
3609 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3610 smb1cli_conn_server_time_zone(cli
->conn
));
3611 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3612 smb1cli_conn_server_time_zone(cli
->conn
));
3613 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3614 smb1cli_conn_server_time_zone(cli
->conn
));
3616 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3617 7, state
->vwv
, 0, NULL
);
3618 if (tevent_req_nomem(subreq
, req
)) {
3619 return tevent_req_post(req
, ev
);
3621 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3625 static void cli_setattrE_done(struct tevent_req
*subreq
)
3627 struct tevent_req
*req
= tevent_req_callback_data(
3628 subreq
, struct tevent_req
);
3631 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3632 TALLOC_FREE(subreq
);
3633 if (tevent_req_nterror(req
, status
)) {
3636 tevent_req_done(req
);
3639 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3641 return tevent_req_simple_recv_ntstatus(req
);
3644 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3650 TALLOC_CTX
*frame
= talloc_stackframe();
3651 struct tevent_context
*ev
= NULL
;
3652 struct tevent_req
*req
= NULL
;
3653 NTSTATUS status
= NT_STATUS_OK
;
3655 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3657 * Can't use sync call while an async call is in flight
3659 status
= NT_STATUS_INVALID_PARAMETER
;
3663 ev
= samba_tevent_context_init(frame
);
3665 status
= NT_STATUS_NO_MEMORY
;
3669 req
= cli_setattrE_send(frame
, ev
,
3677 status
= NT_STATUS_NO_MEMORY
;
3681 if (!tevent_req_poll(req
, ev
)) {
3682 status
= map_nt_error_from_unix(errno
);
3686 status
= cli_setattrE_recv(req
);
3693 /****************************************************************************
3694 Do a SMBsetatr call.
3695 ****************************************************************************/
3697 static void cli_setatr_done(struct tevent_req
*subreq
);
3699 struct cli_setatr_state
{
3703 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3704 struct tevent_context
*ev
,
3705 struct cli_state
*cli
,
3710 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3711 struct cli_setatr_state
*state
= NULL
;
3712 uint8_t additional_flags
= 0;
3713 uint8_t *bytes
= NULL
;
3715 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3720 SSVAL(state
->vwv
+0, 0, attr
);
3721 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3723 bytes
= talloc_array(state
, uint8_t, 1);
3724 if (tevent_req_nomem(bytes
, req
)) {
3725 return tevent_req_post(req
, ev
);
3728 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3729 strlen(fname
)+1, NULL
);
3730 if (tevent_req_nomem(bytes
, req
)) {
3731 return tevent_req_post(req
, ev
);
3733 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3734 talloc_get_size(bytes
)+1);
3735 if (tevent_req_nomem(bytes
, req
)) {
3736 return tevent_req_post(req
, ev
);
3739 bytes
[talloc_get_size(bytes
)-1] = 4;
3740 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3742 if (tevent_req_nomem(bytes
, req
)) {
3743 return tevent_req_post(req
, ev
);
3746 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3747 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3748 if (tevent_req_nomem(subreq
, req
)) {
3749 return tevent_req_post(req
, ev
);
3751 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3755 static void cli_setatr_done(struct tevent_req
*subreq
)
3757 struct tevent_req
*req
= tevent_req_callback_data(
3758 subreq
, struct tevent_req
);
3761 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3762 TALLOC_FREE(subreq
);
3763 if (tevent_req_nterror(req
, status
)) {
3766 tevent_req_done(req
);
3769 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3771 return tevent_req_simple_recv_ntstatus(req
);
3774 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3779 TALLOC_CTX
*frame
= talloc_stackframe();
3780 struct tevent_context
*ev
= NULL
;
3781 struct tevent_req
*req
= NULL
;
3782 NTSTATUS status
= NT_STATUS_OK
;
3784 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3786 * Can't use sync call while an async call is in flight
3788 status
= NT_STATUS_INVALID_PARAMETER
;
3792 ev
= samba_tevent_context_init(frame
);
3794 status
= NT_STATUS_NO_MEMORY
;
3798 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3800 status
= NT_STATUS_NO_MEMORY
;
3804 if (!tevent_req_poll(req
, ev
)) {
3805 status
= map_nt_error_from_unix(errno
);
3809 status
= cli_setatr_recv(req
);
3816 /****************************************************************************
3817 Check for existance of a dir.
3818 ****************************************************************************/
3820 static void cli_chkpath_done(struct tevent_req
*subreq
);
3822 struct cli_chkpath_state
{
3826 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3827 struct tevent_context
*ev
,
3828 struct cli_state
*cli
,
3831 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3832 struct cli_chkpath_state
*state
= NULL
;
3833 uint8_t additional_flags
= 0;
3834 uint8_t *bytes
= NULL
;
3836 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3841 bytes
= talloc_array(state
, uint8_t, 1);
3842 if (tevent_req_nomem(bytes
, req
)) {
3843 return tevent_req_post(req
, ev
);
3846 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3847 strlen(fname
)+1, NULL
);
3849 if (tevent_req_nomem(bytes
, req
)) {
3850 return tevent_req_post(req
, ev
);
3853 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3854 0, NULL
, talloc_get_size(bytes
), bytes
);
3855 if (tevent_req_nomem(subreq
, req
)) {
3856 return tevent_req_post(req
, ev
);
3858 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3862 static void cli_chkpath_done(struct tevent_req
*subreq
)
3864 struct tevent_req
*req
= tevent_req_callback_data(
3865 subreq
, struct tevent_req
);
3868 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3869 TALLOC_FREE(subreq
);
3870 if (tevent_req_nterror(req
, status
)) {
3873 tevent_req_done(req
);
3876 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3878 return tevent_req_simple_recv_ntstatus(req
);
3881 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3883 TALLOC_CTX
*frame
= talloc_stackframe();
3884 struct tevent_context
*ev
= NULL
;
3885 struct tevent_req
*req
= NULL
;
3887 NTSTATUS status
= NT_STATUS_OK
;
3889 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3891 * Can't use sync call while an async call is in flight
3893 status
= NT_STATUS_INVALID_PARAMETER
;
3897 path2
= talloc_strdup(frame
, path
);
3899 status
= NT_STATUS_NO_MEMORY
;
3902 trim_char(path2
,'\0','\\');
3904 path2
= talloc_strdup(frame
, "\\");
3906 status
= NT_STATUS_NO_MEMORY
;
3911 ev
= samba_tevent_context_init(frame
);
3913 status
= NT_STATUS_NO_MEMORY
;
3917 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3919 status
= NT_STATUS_NO_MEMORY
;
3923 if (!tevent_req_poll(req
, ev
)) {
3924 status
= map_nt_error_from_unix(errno
);
3928 status
= cli_chkpath_recv(req
);
3935 /****************************************************************************
3937 ****************************************************************************/
3939 static void cli_dskattr_done(struct tevent_req
*subreq
);
3941 struct cli_dskattr_state
{
3947 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3948 struct tevent_context
*ev
,
3949 struct cli_state
*cli
)
3951 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3952 struct cli_dskattr_state
*state
= NULL
;
3953 uint8_t additional_flags
= 0;
3955 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3960 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3962 if (tevent_req_nomem(subreq
, req
)) {
3963 return tevent_req_post(req
, ev
);
3965 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3969 static void cli_dskattr_done(struct tevent_req
*subreq
)
3971 struct tevent_req
*req
= tevent_req_callback_data(
3972 subreq
, struct tevent_req
);
3973 struct cli_dskattr_state
*state
= tevent_req_data(
3974 req
, struct cli_dskattr_state
);
3976 uint16_t *vwv
= NULL
;
3979 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3981 TALLOC_FREE(subreq
);
3982 if (tevent_req_nterror(req
, status
)) {
3985 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3986 state
->total
= SVAL(vwv
+0, 0);
3987 state
->avail
= SVAL(vwv
+3, 0);
3988 tevent_req_done(req
);
3991 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3993 struct cli_dskattr_state
*state
= tevent_req_data(
3994 req
, struct cli_dskattr_state
);
3997 if (tevent_req_is_nterror(req
, &status
)) {
4000 *bsize
= state
->bsize
;
4001 *total
= state
->total
;
4002 *avail
= state
->avail
;
4003 return NT_STATUS_OK
;
4006 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
4008 TALLOC_CTX
*frame
= talloc_stackframe();
4009 struct tevent_context
*ev
= NULL
;
4010 struct tevent_req
*req
= NULL
;
4011 NTSTATUS status
= NT_STATUS_OK
;
4013 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4015 * Can't use sync call while an async call is in flight
4017 status
= NT_STATUS_INVALID_PARAMETER
;
4021 ev
= samba_tevent_context_init(frame
);
4023 status
= NT_STATUS_NO_MEMORY
;
4027 req
= cli_dskattr_send(frame
, ev
, cli
);
4029 status
= NT_STATUS_NO_MEMORY
;
4033 if (!tevent_req_poll(req
, ev
)) {
4034 status
= map_nt_error_from_unix(errno
);
4038 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4045 /****************************************************************************
4046 Create and open a temporary file.
4047 ****************************************************************************/
4049 static void cli_ctemp_done(struct tevent_req
*subreq
);
4051 struct ctemp_state
{
4057 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4058 struct tevent_context
*ev
,
4059 struct cli_state
*cli
,
4062 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4063 struct ctemp_state
*state
= NULL
;
4064 uint8_t additional_flags
= 0;
4065 uint8_t *bytes
= NULL
;
4067 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4072 SSVAL(state
->vwv
,0,0);
4073 SIVALS(state
->vwv
+1,0,-1);
4075 bytes
= talloc_array(state
, uint8_t, 1);
4076 if (tevent_req_nomem(bytes
, req
)) {
4077 return tevent_req_post(req
, ev
);
4080 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4081 strlen(path
)+1, NULL
);
4082 if (tevent_req_nomem(bytes
, req
)) {
4083 return tevent_req_post(req
, ev
);
4086 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4087 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4088 if (tevent_req_nomem(subreq
, req
)) {
4089 return tevent_req_post(req
, ev
);
4091 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4095 static void cli_ctemp_done(struct tevent_req
*subreq
)
4097 struct tevent_req
*req
= tevent_req_callback_data(
4098 subreq
, struct tevent_req
);
4099 struct ctemp_state
*state
= tevent_req_data(
4100 req
, struct ctemp_state
);
4104 uint32_t num_bytes
= 0;
4105 uint8_t *bytes
= NULL
;
4107 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4108 &num_bytes
, &bytes
);
4109 TALLOC_FREE(subreq
);
4110 if (tevent_req_nterror(req
, status
)) {
4114 state
->fnum
= SVAL(vwv
+0, 0);
4116 /* From W2K3, the result is just the ASCII name */
4117 if (num_bytes
< 2) {
4118 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4122 if (pull_string_talloc(state
,
4129 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4132 tevent_req_done(req
);
4135 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4140 struct ctemp_state
*state
= tevent_req_data(req
,
4141 struct ctemp_state
);
4144 if (tevent_req_is_nterror(req
, &status
)) {
4147 *pfnum
= state
->fnum
;
4148 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4150 return NT_STATUS_NO_MEMORY
;
4152 return NT_STATUS_OK
;
4155 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4161 TALLOC_CTX
*frame
= talloc_stackframe();
4162 struct tevent_context
*ev
;
4163 struct tevent_req
*req
;
4164 NTSTATUS status
= NT_STATUS_OK
;
4166 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4168 * Can't use sync call while an async call is in flight
4170 status
= NT_STATUS_INVALID_PARAMETER
;
4174 ev
= samba_tevent_context_init(frame
);
4176 status
= NT_STATUS_NO_MEMORY
;
4180 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4182 status
= NT_STATUS_NO_MEMORY
;
4186 if (!tevent_req_poll(req
, ev
)) {
4187 status
= map_nt_error_from_unix(errno
);
4191 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4199 send a raw ioctl - used by the torture code
4201 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4206 SSVAL(vwv
+0, 0, fnum
);
4207 SSVAL(vwv
+1, 0, code
>>16);
4208 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4210 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4211 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4212 if (!NT_STATUS_IS_OK(status
)) {
4215 *blob
= data_blob_null
;
4216 return NT_STATUS_OK
;
4219 /*********************************************************
4220 Set an extended attribute utility fn.
4221 *********************************************************/
4223 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4224 uint8_t *param
, unsigned int param_len
,
4225 const char *ea_name
,
4226 const char *ea_val
, size_t ea_len
)
4229 unsigned int data_len
= 0;
4230 uint8_t *data
= NULL
;
4232 size_t ea_namelen
= strlen(ea_name
);
4235 SSVAL(setup
, 0, setup_val
);
4237 if (ea_namelen
== 0 && ea_len
== 0) {
4239 data
= talloc_array(talloc_tos(),
4243 return NT_STATUS_NO_MEMORY
;
4246 SIVAL(p
,0,data_len
);
4248 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4249 data
= talloc_array(talloc_tos(),
4253 return NT_STATUS_NO_MEMORY
;
4256 SIVAL(p
,0,data_len
);
4258 SCVAL(p
, 0, 0); /* EA flags. */
4259 SCVAL(p
, 1, ea_namelen
);
4260 SSVAL(p
, 2, ea_len
);
4261 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4262 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4265 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4267 param
, param_len
, 2,
4268 data
, data_len
, CLI_BUFFER_SIZE
,
4270 NULL
, 0, NULL
, /* rsetup */
4271 NULL
, 0, NULL
, /* rparam */
4272 NULL
, 0, NULL
); /* rdata */
4277 /*********************************************************
4278 Set an extended attribute on a pathname.
4279 *********************************************************/
4281 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4282 const char *ea_name
, const char *ea_val
,
4285 unsigned int param_len
= 0;
4288 TALLOC_CTX
*frame
= talloc_stackframe();
4290 param
= talloc_array(talloc_tos(), uint8_t, 6);
4292 return NT_STATUS_NO_MEMORY
;
4294 SSVAL(param
,0,SMB_INFO_SET_EA
);
4298 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4299 path
, strlen(path
)+1,
4301 param_len
= talloc_get_size(param
);
4303 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4304 ea_name
, ea_val
, ea_len
);
4309 /*********************************************************
4310 Set an extended attribute on an fnum.
4311 *********************************************************/
4313 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4314 const char *ea_name
, const char *ea_val
,
4319 memset(param
, 0, 6);
4320 SSVAL(param
,0,fnum
);
4321 SSVAL(param
,2,SMB_INFO_SET_EA
);
4323 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4324 ea_name
, ea_val
, ea_len
);
4327 /*********************************************************
4328 Get an extended attribute list utility fn.
4329 *********************************************************/
4331 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4333 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4335 struct ea_struct
*ea_list
= NULL
;
4340 if (rdata_len
< 4) {
4344 ea_size
= (size_t)IVAL(rdata
,0);
4345 if (ea_size
> rdata_len
) {
4350 /* No EA's present. */
4359 /* Validate the EA list and count it. */
4360 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4361 unsigned int ea_namelen
= CVAL(p
,1);
4362 unsigned int ea_valuelen
= SVAL(p
,2);
4363 if (ea_namelen
== 0) {
4366 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4369 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4370 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4379 *pnum_eas
= num_eas
;
4381 /* Caller only wants number of EA's. */
4385 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4390 ea_size
= (size_t)IVAL(rdata
,0);
4393 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4394 struct ea_struct
*ea
= &ea_list
[num_eas
];
4395 fstring unix_ea_name
;
4396 unsigned int ea_namelen
= CVAL(p
,1);
4397 unsigned int ea_valuelen
= SVAL(p
,2);
4399 ea
->flags
= CVAL(p
,0);
4400 unix_ea_name
[0] = '\0';
4401 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4402 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4406 /* Ensure the value is null terminated (in case it's a string). */
4407 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4408 if (!ea
->value
.data
) {
4412 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4414 ea
->value
.data
[ea_valuelen
] = 0;
4416 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4419 *pea_list
= ea_list
;
4423 TALLOC_FREE(ea_list
);
4427 /*********************************************************
4428 Get an extended attribute list from a pathname.
4429 *********************************************************/
4431 struct cli_get_ea_list_path_state
{
4436 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4438 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4439 struct tevent_context
*ev
,
4440 struct cli_state
*cli
,
4443 struct tevent_req
*req
, *subreq
;
4444 struct cli_get_ea_list_path_state
*state
;
4446 req
= tevent_req_create(mem_ctx
, &state
,
4447 struct cli_get_ea_list_path_state
);
4451 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4452 SMB_INFO_QUERY_ALL_EAS
, 4,
4454 if (tevent_req_nomem(subreq
, req
)) {
4455 return tevent_req_post(req
, ev
);
4457 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4461 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4463 struct tevent_req
*req
= tevent_req_callback_data(
4464 subreq
, struct tevent_req
);
4465 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4466 req
, struct cli_get_ea_list_path_state
);
4469 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4471 TALLOC_FREE(subreq
);
4472 if (tevent_req_nterror(req
, status
)) {
4475 tevent_req_done(req
);
4478 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4479 size_t *pnum_eas
, struct ea_struct
**peas
)
4481 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4482 req
, struct cli_get_ea_list_path_state
);
4485 if (tevent_req_is_nterror(req
, &status
)) {
4488 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4490 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4492 return NT_STATUS_OK
;
4495 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4498 struct ea_struct
**pea_list
)
4500 TALLOC_CTX
*frame
= talloc_stackframe();
4501 struct tevent_context
*ev
= NULL
;
4502 struct tevent_req
*req
= NULL
;
4503 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4505 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4507 * Can't use sync call while an async call is in flight
4509 status
= NT_STATUS_INVALID_PARAMETER
;
4512 ev
= samba_tevent_context_init(frame
);
4516 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4520 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4523 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4529 /****************************************************************************
4530 Convert open "flags" arg to uint32_t on wire.
4531 ****************************************************************************/
4533 static uint32_t open_flags_to_wire(int flags
)
4535 int open_mode
= flags
& O_ACCMODE
;
4538 switch (open_mode
) {
4540 ret
|= SMB_O_WRONLY
;
4547 ret
|= SMB_O_RDONLY
;
4551 if (flags
& O_CREAT
) {
4554 if (flags
& O_EXCL
) {
4557 if (flags
& O_TRUNC
) {
4561 if (flags
& O_SYNC
) {
4565 if (flags
& O_APPEND
) {
4566 ret
|= SMB_O_APPEND
;
4568 #if defined(O_DIRECT)
4569 if (flags
& O_DIRECT
) {
4570 ret
|= SMB_O_DIRECT
;
4573 #if defined(O_DIRECTORY)
4574 if (flags
& O_DIRECTORY
) {
4575 ret
|= SMB_O_DIRECTORY
;
4581 /****************************************************************************
4582 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4583 ****************************************************************************/
4585 struct posix_open_state
{
4589 uint16_t fnum
; /* Out */
4592 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4594 struct tevent_req
*req
= tevent_req_callback_data(
4595 subreq
, struct tevent_req
);
4596 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4601 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4602 NULL
, 0, NULL
, &data
, 12, &num_data
);
4603 TALLOC_FREE(subreq
);
4604 if (tevent_req_nterror(req
, status
)) {
4607 state
->fnum
= SVAL(data
,2);
4608 tevent_req_done(req
);
4611 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4612 struct tevent_context
*ev
,
4613 struct cli_state
*cli
,
4619 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4620 struct posix_open_state
*state
= NULL
;
4621 uint32_t wire_flags
= open_flags_to_wire(flags
);
4623 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4628 /* Setup setup word. */
4629 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4631 /* Setup param array. */
4632 state
->param
= talloc_array(state
, uint8_t, 6);
4633 if (tevent_req_nomem(state
->param
, req
)) {
4634 return tevent_req_post(req
, ev
);
4636 memset(state
->param
, '\0', 6);
4637 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4639 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4640 strlen(fname
)+1, NULL
);
4642 if (tevent_req_nomem(state
->param
, req
)) {
4643 return tevent_req_post(req
, ev
);
4646 /* Setup data words. */
4648 wire_flags
|= SMB_O_DIRECTORY
;
4651 SIVAL(state
->data
,0,0); /* No oplock. */
4652 SIVAL(state
->data
,4,wire_flags
);
4653 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4654 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4655 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4657 subreq
= cli_trans_send(state
, /* mem ctx. */
4658 ev
, /* event ctx. */
4659 cli
, /* cli_state. */
4660 SMBtrans2
, /* cmd. */
4661 NULL
, /* pipe name. */
4665 &state
->setup
, /* setup. */
4666 1, /* num setup uint16_t words. */
4667 0, /* max returned setup. */
4668 state
->param
, /* param. */
4669 talloc_get_size(state
->param
),/* num param. */
4670 2, /* max returned param. */
4671 state
->data
, /* data. */
4673 12); /* max returned data. */
4675 if (tevent_req_nomem(subreq
, req
)) {
4676 return tevent_req_post(req
, ev
);
4678 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4682 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4683 struct tevent_context
*ev
,
4684 struct cli_state
*cli
,
4689 return cli_posix_open_internal_send(mem_ctx
, ev
,
4690 cli
, fname
, flags
, mode
, false);
4693 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4695 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4698 if (tevent_req_is_nterror(req
, &status
)) {
4701 *pfnum
= state
->fnum
;
4702 return NT_STATUS_OK
;
4705 /****************************************************************************
4706 Open - POSIX semantics. Doesn't request oplock.
4707 ****************************************************************************/
4709 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4710 int flags
, mode_t mode
, uint16_t *pfnum
)
4713 TALLOC_CTX
*frame
= talloc_stackframe();
4714 struct tevent_context
*ev
= NULL
;
4715 struct tevent_req
*req
= NULL
;
4716 NTSTATUS status
= NT_STATUS_OK
;
4718 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4720 * Can't use sync call while an async call is in flight
4722 status
= NT_STATUS_INVALID_PARAMETER
;
4726 ev
= samba_tevent_context_init(frame
);
4728 status
= NT_STATUS_NO_MEMORY
;
4732 req
= cli_posix_open_send(frame
,
4739 status
= NT_STATUS_NO_MEMORY
;
4743 if (!tevent_req_poll(req
, ev
)) {
4744 status
= map_nt_error_from_unix(errno
);
4748 status
= cli_posix_open_recv(req
, pfnum
);
4755 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4756 struct tevent_context
*ev
,
4757 struct cli_state
*cli
,
4761 return cli_posix_open_internal_send(mem_ctx
, ev
,
4762 cli
, fname
, O_CREAT
, mode
, true);
4765 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4767 return tevent_req_simple_recv_ntstatus(req
);
4770 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4772 TALLOC_CTX
*frame
= talloc_stackframe();
4773 struct tevent_context
*ev
= NULL
;
4774 struct tevent_req
*req
= NULL
;
4775 NTSTATUS status
= NT_STATUS_OK
;
4777 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4779 * Can't use sync call while an async call is in flight
4781 status
= NT_STATUS_INVALID_PARAMETER
;
4785 ev
= samba_tevent_context_init(frame
);
4787 status
= NT_STATUS_NO_MEMORY
;
4791 req
= cli_posix_mkdir_send(frame
,
4797 status
= NT_STATUS_NO_MEMORY
;
4801 if (!tevent_req_poll(req
, ev
)) {
4802 status
= map_nt_error_from_unix(errno
);
4806 status
= cli_posix_mkdir_recv(req
);
4813 /****************************************************************************
4814 unlink or rmdir - POSIX semantics.
4815 ****************************************************************************/
4817 struct cli_posix_unlink_internal_state
{
4821 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4823 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4824 struct tevent_context
*ev
,
4825 struct cli_state
*cli
,
4829 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4830 struct cli_posix_unlink_internal_state
*state
= NULL
;
4832 req
= tevent_req_create(mem_ctx
, &state
,
4833 struct cli_posix_unlink_internal_state
);
4838 /* Setup data word. */
4839 SSVAL(state
->data
, 0, level
);
4841 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4842 SMB_POSIX_PATH_UNLINK
,
4844 state
->data
, sizeof(state
->data
));
4845 if (tevent_req_nomem(subreq
, req
)) {
4846 return tevent_req_post(req
, ev
);
4848 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4852 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4854 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4855 tevent_req_simple_finish_ntstatus(subreq
, status
);
4858 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4859 struct tevent_context
*ev
,
4860 struct cli_state
*cli
,
4863 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4864 SMB_POSIX_UNLINK_FILE_TARGET
);
4867 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4869 return tevent_req_simple_recv_ntstatus(req
);
4872 /****************************************************************************
4873 unlink - POSIX semantics.
4874 ****************************************************************************/
4876 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4878 TALLOC_CTX
*frame
= talloc_stackframe();
4879 struct tevent_context
*ev
= NULL
;
4880 struct tevent_req
*req
= NULL
;
4881 NTSTATUS status
= NT_STATUS_OK
;
4883 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4885 * Can't use sync call while an async call is in flight
4887 status
= NT_STATUS_INVALID_PARAMETER
;
4891 ev
= samba_tevent_context_init(frame
);
4893 status
= NT_STATUS_NO_MEMORY
;
4897 req
= cli_posix_unlink_send(frame
,
4902 status
= NT_STATUS_NO_MEMORY
;
4906 if (!tevent_req_poll(req
, ev
)) {
4907 status
= map_nt_error_from_unix(errno
);
4911 status
= cli_posix_unlink_recv(req
);
4918 /****************************************************************************
4919 rmdir - POSIX semantics.
4920 ****************************************************************************/
4922 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4923 struct tevent_context
*ev
,
4924 struct cli_state
*cli
,
4927 return cli_posix_unlink_internal_send(
4928 mem_ctx
, ev
, cli
, fname
,
4929 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4932 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4934 return tevent_req_simple_recv_ntstatus(req
);
4937 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4939 TALLOC_CTX
*frame
= talloc_stackframe();
4940 struct tevent_context
*ev
= NULL
;
4941 struct tevent_req
*req
= NULL
;
4942 NTSTATUS status
= NT_STATUS_OK
;
4944 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4946 * Can't use sync call while an async call is in flight
4948 status
= NT_STATUS_INVALID_PARAMETER
;
4952 ev
= samba_tevent_context_init(frame
);
4954 status
= NT_STATUS_NO_MEMORY
;
4958 req
= cli_posix_rmdir_send(frame
,
4963 status
= NT_STATUS_NO_MEMORY
;
4967 if (!tevent_req_poll(req
, ev
)) {
4968 status
= map_nt_error_from_unix(errno
);
4972 status
= cli_posix_rmdir_recv(req
, frame
);
4979 /****************************************************************************
4981 ****************************************************************************/
4983 struct cli_notify_state
{
4985 uint32_t num_changes
;
4986 struct notify_change
*changes
;
4989 static void cli_notify_done(struct tevent_req
*subreq
);
4991 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4992 struct tevent_context
*ev
,
4993 struct cli_state
*cli
, uint16_t fnum
,
4994 uint32_t buffer_size
,
4995 uint32_t completion_filter
, bool recursive
)
4997 struct tevent_req
*req
, *subreq
;
4998 struct cli_notify_state
*state
;
4999 unsigned old_timeout
;
5001 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5006 SIVAL(state
->setup
, 0, completion_filter
);
5007 SSVAL(state
->setup
, 4, fnum
);
5008 SSVAL(state
->setup
, 6, recursive
);
5011 * Notifies should not time out
5013 old_timeout
= cli_set_timeout(cli
, 0);
5015 subreq
= cli_trans_send(
5016 state
, /* mem ctx. */
5017 ev
, /* event ctx. */
5018 cli
, /* cli_state. */
5019 SMBnttrans
, /* cmd. */
5020 NULL
, /* pipe name. */
5022 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5024 (uint16_t *)state
->setup
, /* setup. */
5025 4, /* num setup uint16_t words. */
5026 0, /* max returned setup. */
5029 buffer_size
, /* max returned param. */
5032 0); /* max returned data. */
5034 cli_set_timeout(cli
, old_timeout
);
5036 if (tevent_req_nomem(subreq
, req
)) {
5037 return tevent_req_post(req
, ev
);
5039 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5043 static void cli_notify_done(struct tevent_req
*subreq
)
5045 struct tevent_req
*req
= tevent_req_callback_data(
5046 subreq
, struct tevent_req
);
5047 struct cli_notify_state
*state
= tevent_req_data(
5048 req
, struct cli_notify_state
);
5051 uint32_t i
, ofs
, num_params
;
5054 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5055 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5056 TALLOC_FREE(subreq
);
5057 if (tevent_req_nterror(req
, status
)) {
5058 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5062 state
->num_changes
= 0;
5065 while (num_params
- ofs
> 12) {
5066 uint32_t next
= IVAL(params
, ofs
);
5067 state
->num_changes
+= 1;
5069 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5075 state
->changes
= talloc_array(state
, struct notify_change
,
5076 state
->num_changes
);
5077 if (tevent_req_nomem(state
->changes
, req
)) {
5078 TALLOC_FREE(params
);
5084 for (i
=0; i
<state
->num_changes
; i
++) {
5085 uint32_t next
= IVAL(params
, ofs
);
5086 uint32_t len
= IVAL(params
, ofs
+8);
5090 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5091 TALLOC_FREE(params
);
5093 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5097 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5098 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5099 &name
, params
+ofs
+12, len
,
5100 STR_TERMINATE
|STR_UNICODE
);
5102 TALLOC_FREE(params
);
5103 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5106 state
->changes
[i
].name
= name
;
5110 TALLOC_FREE(params
);
5111 tevent_req_done(req
);
5114 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5115 uint32_t *pnum_changes
,
5116 struct notify_change
**pchanges
)
5118 struct cli_notify_state
*state
= tevent_req_data(
5119 req
, struct cli_notify_state
);
5122 if (tevent_req_is_nterror(req
, &status
)) {
5126 *pnum_changes
= state
->num_changes
;
5127 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5128 return NT_STATUS_OK
;
5131 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5132 uint32_t completion_filter
, bool recursive
,
5133 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5134 struct notify_change
**pchanges
)
5136 TALLOC_CTX
*frame
= talloc_stackframe();
5137 struct tevent_context
*ev
;
5138 struct tevent_req
*req
;
5139 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5141 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5143 * Can't use sync call while an async call is in flight
5145 status
= NT_STATUS_INVALID_PARAMETER
;
5148 ev
= samba_tevent_context_init(frame
);
5152 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5153 completion_filter
, recursive
);
5157 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5160 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5166 struct cli_qpathinfo_state
{
5175 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5177 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5178 struct tevent_context
*ev
,
5179 struct cli_state
*cli
, const char *fname
,
5180 uint16_t level
, uint32_t min_rdata
,
5183 struct tevent_req
*req
, *subreq
;
5184 struct cli_qpathinfo_state
*state
;
5186 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5190 state
->min_rdata
= min_rdata
;
5191 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5193 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5194 if (tevent_req_nomem(state
->param
, req
)) {
5195 return tevent_req_post(req
, ev
);
5197 SSVAL(state
->param
, 0, level
);
5198 state
->param
= trans2_bytes_push_str(
5199 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5200 if (tevent_req_nomem(state
->param
, req
)) {
5201 return tevent_req_post(req
, ev
);
5204 subreq
= cli_trans_send(
5205 state
, /* mem ctx. */
5206 ev
, /* event ctx. */
5207 cli
, /* cli_state. */
5208 SMBtrans2
, /* cmd. */
5209 NULL
, /* pipe name. */
5213 state
->setup
, /* setup. */
5214 1, /* num setup uint16_t words. */
5215 0, /* max returned setup. */
5216 state
->param
, /* param. */
5217 talloc_get_size(state
->param
), /* num param. */
5218 2, /* max returned param. */
5221 max_rdata
); /* max returned data. */
5223 if (tevent_req_nomem(subreq
, req
)) {
5224 return tevent_req_post(req
, ev
);
5226 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5230 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5232 struct tevent_req
*req
= tevent_req_callback_data(
5233 subreq
, struct tevent_req
);
5234 struct cli_qpathinfo_state
*state
= tevent_req_data(
5235 req
, struct cli_qpathinfo_state
);
5238 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5240 &state
->rdata
, state
->min_rdata
,
5242 if (tevent_req_nterror(req
, status
)) {
5245 tevent_req_done(req
);
5248 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5249 uint8_t **rdata
, uint32_t *num_rdata
)
5251 struct cli_qpathinfo_state
*state
= tevent_req_data(
5252 req
, struct cli_qpathinfo_state
);
5255 if (tevent_req_is_nterror(req
, &status
)) {
5258 if (rdata
!= NULL
) {
5259 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5261 TALLOC_FREE(state
->rdata
);
5263 if (num_rdata
!= NULL
) {
5264 *num_rdata
= state
->num_rdata
;
5266 return NT_STATUS_OK
;
5269 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5270 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5272 uint8_t **rdata
, uint32_t *num_rdata
)
5274 TALLOC_CTX
*frame
= talloc_stackframe();
5275 struct tevent_context
*ev
;
5276 struct tevent_req
*req
;
5277 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5279 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5281 * Can't use sync call while an async call is in flight
5283 status
= NT_STATUS_INVALID_PARAMETER
;
5286 ev
= samba_tevent_context_init(frame
);
5290 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5295 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5298 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5304 struct cli_qfileinfo_state
{
5308 uint16_t recv_flags2
;
5314 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5316 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5317 struct tevent_context
*ev
,
5318 struct cli_state
*cli
, uint16_t fnum
,
5319 uint16_t level
, uint32_t min_rdata
,
5322 struct tevent_req
*req
, *subreq
;
5323 struct cli_qfileinfo_state
*state
;
5325 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5329 state
->min_rdata
= min_rdata
;
5330 SSVAL(state
->param
, 0, fnum
);
5331 SSVAL(state
->param
, 2, level
);
5332 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5334 subreq
= cli_trans_send(
5335 state
, /* mem ctx. */
5336 ev
, /* event ctx. */
5337 cli
, /* cli_state. */
5338 SMBtrans2
, /* cmd. */
5339 NULL
, /* pipe name. */
5343 state
->setup
, /* setup. */
5344 1, /* num setup uint16_t words. */
5345 0, /* max returned setup. */
5346 state
->param
, /* param. */
5347 sizeof(state
->param
), /* num param. */
5348 2, /* max returned param. */
5351 max_rdata
); /* max returned data. */
5353 if (tevent_req_nomem(subreq
, req
)) {
5354 return tevent_req_post(req
, ev
);
5356 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5360 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5362 struct tevent_req
*req
= tevent_req_callback_data(
5363 subreq
, struct tevent_req
);
5364 struct cli_qfileinfo_state
*state
= tevent_req_data(
5365 req
, struct cli_qfileinfo_state
);
5368 status
= cli_trans_recv(subreq
, state
,
5369 &state
->recv_flags2
,
5372 &state
->rdata
, state
->min_rdata
,
5374 if (tevent_req_nterror(req
, status
)) {
5377 tevent_req_done(req
);
5380 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5381 uint16_t *recv_flags2
,
5382 uint8_t **rdata
, uint32_t *num_rdata
)
5384 struct cli_qfileinfo_state
*state
= tevent_req_data(
5385 req
, struct cli_qfileinfo_state
);
5388 if (tevent_req_is_nterror(req
, &status
)) {
5392 if (recv_flags2
!= NULL
) {
5393 *recv_flags2
= state
->recv_flags2
;
5395 if (rdata
!= NULL
) {
5396 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5398 TALLOC_FREE(state
->rdata
);
5400 if (num_rdata
!= NULL
) {
5401 *num_rdata
= state
->num_rdata
;
5403 return NT_STATUS_OK
;
5406 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5407 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5408 uint32_t max_rdata
, uint16_t *recv_flags2
,
5409 uint8_t **rdata
, uint32_t *num_rdata
)
5411 TALLOC_CTX
*frame
= talloc_stackframe();
5412 struct tevent_context
*ev
;
5413 struct tevent_req
*req
;
5414 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5416 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5418 * Can't use sync call while an async call is in flight
5420 status
= NT_STATUS_INVALID_PARAMETER
;
5423 ev
= samba_tevent_context_init(frame
);
5427 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5432 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5435 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5441 struct cli_flush_state
{
5445 static void cli_flush_done(struct tevent_req
*subreq
);
5447 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5448 struct tevent_context
*ev
,
5449 struct cli_state
*cli
,
5452 struct tevent_req
*req
, *subreq
;
5453 struct cli_flush_state
*state
;
5455 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5459 SSVAL(state
->vwv
+ 0, 0, fnum
);
5461 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5463 if (tevent_req_nomem(subreq
, req
)) {
5464 return tevent_req_post(req
, ev
);
5466 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5470 static void cli_flush_done(struct tevent_req
*subreq
)
5472 struct tevent_req
*req
= tevent_req_callback_data(
5473 subreq
, struct tevent_req
);
5476 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5477 TALLOC_FREE(subreq
);
5478 if (tevent_req_nterror(req
, status
)) {
5481 tevent_req_done(req
);
5484 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5486 return tevent_req_simple_recv_ntstatus(req
);
5489 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5491 TALLOC_CTX
*frame
= talloc_stackframe();
5492 struct tevent_context
*ev
;
5493 struct tevent_req
*req
;
5494 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5496 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5498 * Can't use sync call while an async call is in flight
5500 status
= NT_STATUS_INVALID_PARAMETER
;
5503 ev
= samba_tevent_context_init(frame
);
5507 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5511 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5514 status
= cli_flush_recv(req
);
5520 struct cli_shadow_copy_data_state
{
5527 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5529 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5530 struct tevent_context
*ev
,
5531 struct cli_state
*cli
,
5535 struct tevent_req
*req
, *subreq
;
5536 struct cli_shadow_copy_data_state
*state
;
5539 req
= tevent_req_create(mem_ctx
, &state
,
5540 struct cli_shadow_copy_data_state
);
5544 state
->get_names
= get_names
;
5545 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5547 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5548 SSVAL(state
->setup
+ 2, 0, fnum
);
5549 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5550 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5552 subreq
= cli_trans_send(
5553 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5554 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5557 if (tevent_req_nomem(subreq
, req
)) {
5558 return tevent_req_post(req
, ev
);
5560 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5564 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5566 struct tevent_req
*req
= tevent_req_callback_data(
5567 subreq
, struct tevent_req
);
5568 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5569 req
, struct cli_shadow_copy_data_state
);
5572 status
= cli_trans_recv(subreq
, state
, NULL
,
5573 NULL
, 0, NULL
, /* setup */
5574 NULL
, 0, NULL
, /* param */
5575 &state
->data
, 12, &state
->num_data
);
5576 TALLOC_FREE(subreq
);
5577 if (tevent_req_nterror(req
, status
)) {
5580 tevent_req_done(req
);
5583 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5584 char ***pnames
, int *pnum_names
)
5586 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5587 req
, struct cli_shadow_copy_data_state
);
5593 if (tevent_req_is_nterror(req
, &status
)) {
5596 num_names
= IVAL(state
->data
, 4);
5597 dlength
= IVAL(state
->data
, 8);
5599 if (!state
->get_names
) {
5600 *pnum_names
= num_names
;
5601 return NT_STATUS_OK
;
5604 if (dlength
+12 > state
->num_data
) {
5605 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5607 names
= talloc_array(mem_ctx
, char *, num_names
);
5608 if (names
== NULL
) {
5609 return NT_STATUS_NO_MEMORY
;
5612 for (i
=0; i
<num_names
; i
++) {
5615 size_t converted_size
;
5617 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5618 ret
= convert_string_talloc(
5619 names
, CH_UTF16LE
, CH_UNIX
,
5620 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5621 &names
[i
], &converted_size
);
5624 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5627 *pnum_names
= num_names
;
5629 return NT_STATUS_OK
;
5632 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5633 uint16_t fnum
, bool get_names
,
5634 char ***pnames
, int *pnum_names
)
5636 TALLOC_CTX
*frame
= talloc_stackframe();
5637 struct tevent_context
*ev
;
5638 struct tevent_req
*req
;
5639 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5641 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5643 * Can't use sync call while an async call is in flight
5645 status
= NT_STATUS_INVALID_PARAMETER
;
5648 ev
= samba_tevent_context_init(frame
);
5652 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5656 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5659 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);