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
= talloc_stackframe();
1529 struct tevent_context
*ev
;
1530 struct tevent_req
*req
;
1531 NTSTATUS status
= NT_STATUS_OK
;
1533 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1535 * Can't use sync call while an async call is in flight
1537 status
= NT_STATUS_INVALID_PARAMETER
;
1541 ev
= samba_tevent_context_init(frame
);
1543 status
= NT_STATUS_NO_MEMORY
;
1547 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1549 status
= NT_STATUS_NO_MEMORY
;
1553 if (!tevent_req_poll(req
, ev
)) {
1554 status
= map_nt_error_from_unix(errno
);
1558 status
= cli_mkdir_recv(req
);
1565 /****************************************************************************
1567 ****************************************************************************/
1569 static void cli_rmdir_done(struct tevent_req
*subreq
);
1571 struct cli_rmdir_state
{
1575 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1576 struct tevent_context
*ev
,
1577 struct cli_state
*cli
,
1580 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1581 struct cli_rmdir_state
*state
= NULL
;
1582 uint8_t additional_flags
= 0;
1583 uint8_t *bytes
= NULL
;
1585 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1590 bytes
= talloc_array(state
, uint8_t, 1);
1591 if (tevent_req_nomem(bytes
, req
)) {
1592 return tevent_req_post(req
, ev
);
1595 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1596 strlen(dname
)+1, NULL
);
1598 if (tevent_req_nomem(bytes
, req
)) {
1599 return tevent_req_post(req
, ev
);
1602 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1603 0, NULL
, talloc_get_size(bytes
), bytes
);
1604 if (tevent_req_nomem(subreq
, req
)) {
1605 return tevent_req_post(req
, ev
);
1607 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1611 static void cli_rmdir_done(struct tevent_req
*subreq
)
1613 struct tevent_req
*req
= tevent_req_callback_data(
1614 subreq
, struct tevent_req
);
1617 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1618 TALLOC_FREE(subreq
);
1619 if (tevent_req_nterror(req
, status
)) {
1622 tevent_req_done(req
);
1625 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1627 return tevent_req_simple_recv_ntstatus(req
);
1630 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1632 TALLOC_CTX
*frame
= talloc_stackframe();
1633 struct tevent_context
*ev
;
1634 struct tevent_req
*req
;
1635 NTSTATUS status
= NT_STATUS_OK
;
1637 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1639 * Can't use sync call while an async call is in flight
1641 status
= NT_STATUS_INVALID_PARAMETER
;
1645 ev
= samba_tevent_context_init(frame
);
1647 status
= NT_STATUS_NO_MEMORY
;
1651 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1653 status
= NT_STATUS_NO_MEMORY
;
1657 if (!tevent_req_poll(req
, ev
)) {
1658 status
= map_nt_error_from_unix(errno
);
1662 status
= cli_rmdir_recv(req
);
1669 /****************************************************************************
1670 Set or clear the delete on close flag.
1671 ****************************************************************************/
1679 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1681 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1682 NULL
, 0, NULL
, NULL
, 0, NULL
);
1683 tevent_req_simple_finish_ntstatus(subreq
, status
);
1686 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1687 struct tevent_context
*ev
,
1688 struct cli_state
*cli
,
1692 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1693 struct doc_state
*state
= NULL
;
1695 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1700 /* Setup setup word. */
1701 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1703 /* Setup param array. */
1704 SSVAL(state
->param
,0,fnum
);
1705 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1707 /* Setup data array. */
1708 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1710 subreq
= cli_trans_send(state
, /* mem ctx. */
1711 ev
, /* event ctx. */
1712 cli
, /* cli_state. */
1713 SMBtrans2
, /* cmd. */
1714 NULL
, /* pipe name. */
1718 &state
->setup
, /* setup. */
1719 1, /* num setup uint16_t words. */
1720 0, /* max returned setup. */
1721 state
->param
, /* param. */
1723 2, /* max returned param. */
1724 state
->data
, /* data. */
1726 0); /* max returned data. */
1728 if (tevent_req_nomem(subreq
, req
)) {
1729 return tevent_req_post(req
, ev
);
1731 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1735 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1737 return tevent_req_simple_recv_ntstatus(req
);
1740 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1742 TALLOC_CTX
*frame
= talloc_stackframe();
1743 struct tevent_context
*ev
= NULL
;
1744 struct tevent_req
*req
= NULL
;
1745 NTSTATUS status
= NT_STATUS_OK
;
1747 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1749 * Can't use sync call while an async call is in flight
1751 status
= NT_STATUS_INVALID_PARAMETER
;
1755 ev
= samba_tevent_context_init(frame
);
1757 status
= NT_STATUS_NO_MEMORY
;
1761 req
= cli_nt_delete_on_close_send(frame
,
1767 status
= NT_STATUS_NO_MEMORY
;
1771 if (!tevent_req_poll(req
, ev
)) {
1772 status
= map_nt_error_from_unix(errno
);
1776 status
= cli_nt_delete_on_close_recv(req
);
1783 struct cli_ntcreate_state
{
1788 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1790 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1791 struct tevent_context
*ev
,
1792 struct cli_state
*cli
,
1794 uint32_t CreatFlags
,
1795 uint32_t DesiredAccess
,
1796 uint32_t FileAttributes
,
1797 uint32_t ShareAccess
,
1798 uint32_t CreateDisposition
,
1799 uint32_t CreateOptions
,
1800 uint8_t SecurityFlags
)
1802 struct tevent_req
*req
, *subreq
;
1803 struct cli_ntcreate_state
*state
;
1806 size_t converted_len
;
1808 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1815 SCVAL(vwv
+0, 0, 0xFF);
1820 if (cli
->use_oplocks
) {
1821 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1823 SIVAL(vwv
+3, 1, CreatFlags
);
1824 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1825 SIVAL(vwv
+7, 1, DesiredAccess
);
1826 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1827 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1828 SIVAL(vwv
+13, 1, FileAttributes
);
1829 SIVAL(vwv
+15, 1, ShareAccess
);
1830 SIVAL(vwv
+17, 1, CreateDisposition
);
1831 SIVAL(vwv
+19, 1, CreateOptions
|
1832 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1833 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1834 SCVAL(vwv
+23, 1, SecurityFlags
);
1836 bytes
= talloc_array(state
, uint8_t, 0);
1837 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1838 fname
, strlen(fname
)+1,
1841 /* sigh. this copes with broken netapp filer behaviour */
1842 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1844 if (tevent_req_nomem(bytes
, req
)) {
1845 return tevent_req_post(req
, ev
);
1848 SSVAL(vwv
+2, 1, converted_len
);
1850 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1851 talloc_get_size(bytes
), bytes
);
1852 if (tevent_req_nomem(subreq
, req
)) {
1853 return tevent_req_post(req
, ev
);
1855 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1859 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1861 struct tevent_req
*req
= tevent_req_callback_data(
1862 subreq
, struct tevent_req
);
1863 struct cli_ntcreate_state
*state
= tevent_req_data(
1864 req
, struct cli_ntcreate_state
);
1871 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
,
1872 &num_bytes
, &bytes
);
1873 TALLOC_FREE(subreq
);
1874 if (tevent_req_nterror(req
, status
)) {
1877 state
->fnum
= SVAL(vwv
+2, 1);
1878 tevent_req_done(req
);
1881 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1883 struct cli_ntcreate_state
*state
= tevent_req_data(
1884 req
, struct cli_ntcreate_state
);
1887 if (tevent_req_is_nterror(req
, &status
)) {
1890 *pfnum
= state
->fnum
;
1891 return NT_STATUS_OK
;
1894 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1896 uint32_t CreatFlags
,
1897 uint32_t DesiredAccess
,
1898 uint32_t FileAttributes
,
1899 uint32_t ShareAccess
,
1900 uint32_t CreateDisposition
,
1901 uint32_t CreateOptions
,
1902 uint8_t SecurityFlags
,
1905 TALLOC_CTX
*frame
= talloc_stackframe();
1906 struct tevent_context
*ev
;
1907 struct tevent_req
*req
;
1908 NTSTATUS status
= NT_STATUS_OK
;
1910 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1912 * Can't use sync call while an async call is in flight
1914 status
= NT_STATUS_INVALID_PARAMETER
;
1918 ev
= samba_tevent_context_init(frame
);
1920 status
= NT_STATUS_NO_MEMORY
;
1924 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1925 DesiredAccess
, FileAttributes
, ShareAccess
,
1926 CreateDisposition
, CreateOptions
,
1929 status
= NT_STATUS_NO_MEMORY
;
1933 if (!tevent_req_poll(req
, ev
)) {
1934 status
= map_nt_error_from_unix(errno
);
1938 status
= cli_ntcreate_recv(req
, pfid
);
1944 struct cli_nttrans_create_state
{
1948 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1950 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1951 struct tevent_context
*ev
,
1952 struct cli_state
*cli
,
1954 uint32_t CreatFlags
,
1955 uint32_t DesiredAccess
,
1956 uint32_t FileAttributes
,
1957 uint32_t ShareAccess
,
1958 uint32_t CreateDisposition
,
1959 uint32_t CreateOptions
,
1960 uint8_t SecurityFlags
,
1961 struct security_descriptor
*secdesc
,
1962 struct ea_struct
*eas
,
1965 struct tevent_req
*req
, *subreq
;
1966 struct cli_nttrans_create_state
*state
;
1968 uint8_t *secdesc_buf
;
1971 size_t converted_len
;
1973 req
= tevent_req_create(mem_ctx
,
1974 &state
, struct cli_nttrans_create_state
);
1979 if (secdesc
!= NULL
) {
1980 status
= marshall_sec_desc(talloc_tos(), secdesc
,
1981 &secdesc_buf
, &secdesc_len
);
1982 if (tevent_req_nterror(req
, status
)) {
1983 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1984 nt_errstr(status
)));
1985 return tevent_req_post(req
, ev
);
1996 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
1997 return tevent_req_post(req
, ev
);
2000 param
= talloc_array(state
, uint8_t, 53);
2001 if (tevent_req_nomem(param
, req
)) {
2002 return tevent_req_post(req
, ev
);
2005 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2006 fname
, strlen(fname
),
2008 if (tevent_req_nomem(param
, req
)) {
2009 return tevent_req_post(req
, ev
);
2012 SIVAL(param
, 0, CreatFlags
);
2013 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2014 SIVAL(param
, 8, DesiredAccess
);
2015 SIVAL(param
, 12, 0x0); /* AllocationSize */
2016 SIVAL(param
, 16, 0x0); /* AllocationSize */
2017 SIVAL(param
, 20, FileAttributes
);
2018 SIVAL(param
, 24, ShareAccess
);
2019 SIVAL(param
, 28, CreateDisposition
);
2020 SIVAL(param
, 32, CreateOptions
|
2021 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2022 SIVAL(param
, 36, secdesc_len
);
2023 SIVAL(param
, 40, 0); /* EA length*/
2024 SIVAL(param
, 44, converted_len
);
2025 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2026 SCVAL(param
, 52, SecurityFlags
);
2028 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2029 NULL
, -1, /* name, fid */
2030 NT_TRANSACT_CREATE
, 0,
2031 NULL
, 0, 0, /* setup */
2032 param
, talloc_get_size(param
), 128, /* param */
2033 secdesc_buf
, secdesc_len
, 0); /* data */
2034 if (tevent_req_nomem(subreq
, req
)) {
2035 return tevent_req_post(req
, ev
);
2037 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2041 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2043 struct tevent_req
*req
= tevent_req_callback_data(
2044 subreq
, struct tevent_req
);
2045 struct cli_nttrans_create_state
*state
= tevent_req_data(
2046 req
, struct cli_nttrans_create_state
);
2051 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2052 NULL
, 0, NULL
, /* rsetup */
2053 ¶m
, 69, &num_param
,
2055 if (tevent_req_nterror(req
, status
)) {
2058 state
->fnum
= SVAL(param
, 2);
2060 tevent_req_done(req
);
2063 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
, uint16_t *fnum
)
2065 struct cli_nttrans_create_state
*state
= tevent_req_data(
2066 req
, struct cli_nttrans_create_state
);
2069 if (tevent_req_is_nterror(req
, &status
)) {
2072 *fnum
= state
->fnum
;
2073 return NT_STATUS_OK
;
2076 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2078 uint32_t CreatFlags
,
2079 uint32_t DesiredAccess
,
2080 uint32_t FileAttributes
,
2081 uint32_t ShareAccess
,
2082 uint32_t CreateDisposition
,
2083 uint32_t CreateOptions
,
2084 uint8_t SecurityFlags
,
2085 struct security_descriptor
*secdesc
,
2086 struct ea_struct
*eas
,
2090 TALLOC_CTX
*frame
= talloc_stackframe();
2091 struct tevent_context
*ev
;
2092 struct tevent_req
*req
;
2093 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2095 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2097 * Can't use sync call while an async call is in flight
2099 status
= NT_STATUS_INVALID_PARAMETER
;
2102 ev
= samba_tevent_context_init(frame
);
2106 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2107 DesiredAccess
, FileAttributes
,
2108 ShareAccess
, CreateDisposition
,
2109 CreateOptions
, SecurityFlags
,
2110 secdesc
, eas
, num_eas
);
2114 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2117 status
= cli_nttrans_create_recv(req
, pfid
);
2123 /****************************************************************************
2125 WARNING: if you open with O_WRONLY then getattrE won't work!
2126 ****************************************************************************/
2128 struct cli_openx_state
{
2135 static void cli_openx_done(struct tevent_req
*subreq
);
2137 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2138 struct tevent_context
*ev
,
2139 struct cli_state
*cli
, const char *fname
,
2140 int flags
, int share_mode
,
2141 struct tevent_req
**psmbreq
)
2143 struct tevent_req
*req
, *subreq
;
2144 struct cli_openx_state
*state
;
2146 unsigned accessmode
;
2147 uint8_t additional_flags
;
2150 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2156 if (flags
& O_CREAT
) {
2159 if (!(flags
& O_EXCL
)) {
2160 if (flags
& O_TRUNC
)
2166 accessmode
= (share_mode
<<4);
2168 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2170 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2175 if ((flags
& O_SYNC
) == O_SYNC
) {
2176 accessmode
|= (1<<14);
2180 if (share_mode
== DENY_FCB
) {
2184 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2185 SCVAL(state
->vwv
+ 0, 1, 0);
2186 SSVAL(state
->vwv
+ 1, 0, 0);
2187 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2188 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2189 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2190 SSVAL(state
->vwv
+ 5, 0, 0);
2191 SIVAL(state
->vwv
+ 6, 0, 0);
2192 SSVAL(state
->vwv
+ 8, 0, openfn
);
2193 SIVAL(state
->vwv
+ 9, 0, 0);
2194 SIVAL(state
->vwv
+ 11, 0, 0);
2195 SIVAL(state
->vwv
+ 13, 0, 0);
2197 additional_flags
= 0;
2199 if (cli
->use_oplocks
) {
2200 /* if using oplocks then ask for a batch oplock via
2201 core and extended methods */
2203 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2204 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2207 bytes
= talloc_array(state
, uint8_t, 0);
2208 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2209 strlen(fname
)+1, NULL
);
2211 if (tevent_req_nomem(bytes
, req
)) {
2212 return tevent_req_post(req
, ev
);
2215 state
->bytes
.iov_base
= (void *)bytes
;
2216 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2218 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2219 15, state
->vwv
, 1, &state
->bytes
);
2220 if (subreq
== NULL
) {
2224 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2229 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2230 struct cli_state
*cli
, const char *fname
,
2231 int flags
, int share_mode
)
2233 struct tevent_req
*req
, *subreq
;
2236 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2242 status
= smb1cli_req_chain_submit(&subreq
, 1);
2243 if (tevent_req_nterror(req
, status
)) {
2244 return tevent_req_post(req
, ev
);
2249 static void cli_openx_done(struct tevent_req
*subreq
)
2251 struct tevent_req
*req
= tevent_req_callback_data(
2252 subreq
, struct tevent_req
);
2253 struct cli_openx_state
*state
= tevent_req_data(
2254 req
, struct cli_openx_state
);
2259 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2261 TALLOC_FREE(subreq
);
2262 if (tevent_req_nterror(req
, status
)) {
2265 state
->fnum
= SVAL(vwv
+2, 0);
2266 tevent_req_done(req
);
2269 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2271 struct cli_openx_state
*state
= tevent_req_data(
2272 req
, struct cli_openx_state
);
2275 if (tevent_req_is_nterror(req
, &status
)) {
2278 *pfnum
= state
->fnum
;
2279 return NT_STATUS_OK
;
2282 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2283 int share_mode
, uint16_t *pfnum
)
2285 TALLOC_CTX
*frame
= talloc_stackframe();
2286 struct tevent_context
*ev
;
2287 struct tevent_req
*req
;
2288 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2290 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2292 * Can't use sync call while an async call is in flight
2294 status
= NT_STATUS_INVALID_PARAMETER
;
2298 ev
= samba_tevent_context_init(frame
);
2303 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2308 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2312 status
= cli_openx_recv(req
, pfnum
);
2317 /****************************************************************************
2318 Synchronous wrapper function that does an NtCreateX open by preference
2319 and falls back to openX if this fails.
2320 ****************************************************************************/
2322 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2323 int share_mode_in
, uint16_t *pfnum
)
2326 unsigned int openfn
= 0;
2327 unsigned int dos_deny
= 0;
2328 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2330 /* Do the initial mapping into OpenX parameters. */
2331 if (flags
& O_CREAT
) {
2334 if (!(flags
& O_EXCL
)) {
2335 if (flags
& O_TRUNC
)
2341 dos_deny
= (share_mode_in
<<4);
2343 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2345 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2350 if ((flags
& O_SYNC
) == O_SYNC
) {
2351 dos_deny
|= (1<<14);
2355 if (share_mode_in
== DENY_FCB
) {
2360 /* Hmmm. This is what I think the above code
2361 should look like if it's using the constants
2364 if (flags
& O_CREAT
) {
2365 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2367 if (!(flags
& O_EXCL
)) {
2368 if (flags
& O_TRUNC
)
2369 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2371 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2374 dos_deny
= SET_DENY_MODE(share_mode_in
);
2376 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2377 dos_deny
|= DOS_OPEN_RDWR
;
2378 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2379 dos_deny
|= DOS_OPEN_WRONLY
;
2383 if ((flags
& O_SYNC
) == O_SYNC
) {
2384 dos_deny
|= FILE_SYNC_OPENMODE
;
2388 if (share_mode_in
== DENY_FCB
) {
2393 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2394 openfn
, &access_mask
,
2395 &share_mode
, &create_disposition
,
2396 &create_options
, NULL
)) {
2400 status
= cli_ntcreate(cli
,
2411 /* Try and cope will all varients of "we don't do this call"
2412 and fall back to openX. */
2414 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2415 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2416 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2417 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2418 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2419 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2420 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2421 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2422 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2430 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2433 /****************************************************************************
2435 ****************************************************************************/
2437 struct cli_close_state
{
2441 static void cli_close_done(struct tevent_req
*subreq
);
2443 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2444 struct tevent_context
*ev
,
2445 struct cli_state
*cli
,
2447 struct tevent_req
**psubreq
)
2449 struct tevent_req
*req
, *subreq
;
2450 struct cli_close_state
*state
;
2452 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2457 SSVAL(state
->vwv
+0, 0, fnum
);
2458 SIVALS(state
->vwv
+1, 0, -1);
2460 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2462 if (subreq
== NULL
) {
2466 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2471 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2472 struct tevent_context
*ev
,
2473 struct cli_state
*cli
,
2476 struct tevent_req
*req
, *subreq
;
2479 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2484 status
= smb1cli_req_chain_submit(&subreq
, 1);
2485 if (tevent_req_nterror(req
, status
)) {
2486 return tevent_req_post(req
, ev
);
2491 static void cli_close_done(struct tevent_req
*subreq
)
2493 struct tevent_req
*req
= tevent_req_callback_data(
2494 subreq
, struct tevent_req
);
2497 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2498 TALLOC_FREE(subreq
);
2499 if (tevent_req_nterror(req
, status
)) {
2502 tevent_req_done(req
);
2505 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2507 return tevent_req_simple_recv_ntstatus(req
);
2510 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2512 TALLOC_CTX
*frame
= talloc_stackframe();
2513 struct tevent_context
*ev
;
2514 struct tevent_req
*req
;
2515 NTSTATUS status
= NT_STATUS_OK
;
2517 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2519 * Can't use sync call while an async call is in flight
2521 status
= NT_STATUS_INVALID_PARAMETER
;
2525 ev
= samba_tevent_context_init(frame
);
2527 status
= NT_STATUS_NO_MEMORY
;
2531 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2533 status
= NT_STATUS_NO_MEMORY
;
2537 if (!tevent_req_poll(req
, ev
)) {
2538 status
= map_nt_error_from_unix(errno
);
2542 status
= cli_close_recv(req
);
2548 /****************************************************************************
2549 Truncate a file to a specified size
2550 ****************************************************************************/
2552 struct ftrunc_state
{
2558 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2560 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2561 NULL
, 0, NULL
, NULL
, 0, NULL
);
2562 tevent_req_simple_finish_ntstatus(subreq
, status
);
2565 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2566 struct tevent_context
*ev
,
2567 struct cli_state
*cli
,
2571 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2572 struct ftrunc_state
*state
= NULL
;
2574 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2579 /* Setup setup word. */
2580 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2582 /* Setup param array. */
2583 SSVAL(state
->param
,0,fnum
);
2584 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2585 SSVAL(state
->param
,4,0);
2587 /* Setup data array. */
2588 SBVAL(state
->data
, 0, size
);
2590 subreq
= cli_trans_send(state
, /* mem ctx. */
2591 ev
, /* event ctx. */
2592 cli
, /* cli_state. */
2593 SMBtrans2
, /* cmd. */
2594 NULL
, /* pipe name. */
2598 &state
->setup
, /* setup. */
2599 1, /* num setup uint16_t words. */
2600 0, /* max returned setup. */
2601 state
->param
, /* param. */
2603 2, /* max returned param. */
2604 state
->data
, /* data. */
2606 0); /* max returned data. */
2608 if (tevent_req_nomem(subreq
, req
)) {
2609 return tevent_req_post(req
, ev
);
2611 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2615 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2617 return tevent_req_simple_recv_ntstatus(req
);
2620 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2622 TALLOC_CTX
*frame
= talloc_stackframe();
2623 struct tevent_context
*ev
= NULL
;
2624 struct tevent_req
*req
= NULL
;
2625 NTSTATUS status
= NT_STATUS_OK
;
2627 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2629 * Can't use sync call while an async call is in flight
2631 status
= NT_STATUS_INVALID_PARAMETER
;
2635 ev
= samba_tevent_context_init(frame
);
2637 status
= NT_STATUS_NO_MEMORY
;
2641 req
= cli_ftruncate_send(frame
,
2647 status
= NT_STATUS_NO_MEMORY
;
2651 if (!tevent_req_poll(req
, ev
)) {
2652 status
= map_nt_error_from_unix(errno
);
2656 status
= cli_ftruncate_recv(req
);
2663 /****************************************************************************
2664 send a lock with a specified locktype
2665 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2666 ****************************************************************************/
2668 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2669 uint32_t offset
, uint32_t len
,
2670 int timeout
, unsigned char locktype
)
2675 unsigned int set_timeout
= 0;
2676 unsigned int saved_timeout
= 0;
2678 SCVAL(vwv
+ 0, 0, 0xff);
2679 SCVAL(vwv
+ 0, 1, 0);
2680 SSVAL(vwv
+ 1, 0, 0);
2681 SSVAL(vwv
+ 2, 0, fnum
);
2682 SCVAL(vwv
+ 3, 0, locktype
);
2683 SCVAL(vwv
+ 3, 1, 0);
2684 SIVALS(vwv
+ 4, 0, timeout
);
2685 SSVAL(vwv
+ 6, 0, 0);
2686 SSVAL(vwv
+ 7, 0, 1);
2688 SSVAL(bytes
, 0, cli_getpid(cli
));
2689 SIVAL(bytes
, 2, offset
);
2690 SIVAL(bytes
, 6, len
);
2693 if (timeout
== -1) {
2694 set_timeout
= 0x7FFFFFFF;
2696 set_timeout
= timeout
+ 2*1000;
2698 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2701 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2702 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2704 if (saved_timeout
!= 0) {
2705 cli_set_timeout(cli
, saved_timeout
);
2711 /****************************************************************************
2713 note that timeout is in units of 2 milliseconds
2714 ****************************************************************************/
2716 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2717 uint32_t offset
, uint32_t len
, int timeout
,
2718 enum brl_type lock_type
)
2722 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2723 (lock_type
== READ_LOCK
? 1 : 0));
2727 /****************************************************************************
2729 ****************************************************************************/
2731 struct cli_unlock_state
{
2736 static void cli_unlock_done(struct tevent_req
*subreq
);
2738 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2739 struct tevent_context
*ev
,
2740 struct cli_state
*cli
,
2746 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2747 struct cli_unlock_state
*state
= NULL
;
2748 uint8_t additional_flags
= 0;
2750 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2755 SCVAL(state
->vwv
+0, 0, 0xFF);
2756 SSVAL(state
->vwv
+2, 0, fnum
);
2757 SCVAL(state
->vwv
+3, 0, 0);
2758 SIVALS(state
->vwv
+4, 0, 0);
2759 SSVAL(state
->vwv
+6, 0, 1);
2760 SSVAL(state
->vwv
+7, 0, 0);
2762 SSVAL(state
->data
, 0, cli_getpid(cli
));
2763 SIVAL(state
->data
, 2, offset
);
2764 SIVAL(state
->data
, 6, len
);
2766 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2767 8, state
->vwv
, 10, state
->data
);
2768 if (tevent_req_nomem(subreq
, req
)) {
2769 return tevent_req_post(req
, ev
);
2771 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2775 static void cli_unlock_done(struct tevent_req
*subreq
)
2777 struct tevent_req
*req
= tevent_req_callback_data(
2778 subreq
, struct tevent_req
);
2781 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2782 TALLOC_FREE(subreq
);
2783 if (tevent_req_nterror(req
, status
)) {
2786 tevent_req_done(req
);
2789 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2791 return tevent_req_simple_recv_ntstatus(req
);
2794 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2799 TALLOC_CTX
*frame
= talloc_stackframe();
2800 struct tevent_context
*ev
;
2801 struct tevent_req
*req
;
2802 NTSTATUS status
= NT_STATUS_OK
;
2804 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2806 * Can't use sync call while an async call is in flight
2808 status
= NT_STATUS_INVALID_PARAMETER
;
2812 ev
= samba_tevent_context_init(frame
);
2814 status
= NT_STATUS_NO_MEMORY
;
2818 req
= cli_unlock_send(frame
, ev
, cli
,
2821 status
= NT_STATUS_NO_MEMORY
;
2825 if (!tevent_req_poll(req
, ev
)) {
2826 status
= map_nt_error_from_unix(errno
);
2830 status
= cli_unlock_recv(req
);
2837 /****************************************************************************
2838 Lock a file with 64 bit offsets.
2839 ****************************************************************************/
2841 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2842 uint64_t offset
, uint64_t len
, int timeout
,
2843 enum brl_type lock_type
)
2847 unsigned int set_timeout
= 0;
2848 unsigned int saved_timeout
= 0;
2852 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2853 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2856 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2857 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2859 SCVAL(vwv
+ 0, 0, 0xff);
2860 SCVAL(vwv
+ 0, 1, 0);
2861 SSVAL(vwv
+ 1, 0, 0);
2862 SSVAL(vwv
+ 2, 0, fnum
);
2863 SCVAL(vwv
+ 3, 0, ltype
);
2864 SCVAL(vwv
+ 3, 1, 0);
2865 SIVALS(vwv
+ 4, 0, timeout
);
2866 SSVAL(vwv
+ 6, 0, 0);
2867 SSVAL(vwv
+ 7, 0, 1);
2869 SIVAL(bytes
, 0, cli_getpid(cli
));
2870 SOFF_T_R(bytes
, 4, offset
);
2871 SOFF_T_R(bytes
, 12, len
);
2874 if (timeout
== -1) {
2875 set_timeout
= 0x7FFFFFFF;
2877 set_timeout
= timeout
+ 2*1000;
2879 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2882 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2883 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2885 if (saved_timeout
!= 0) {
2886 cli_set_timeout(cli
, saved_timeout
);
2892 /****************************************************************************
2893 Unlock a file with 64 bit offsets.
2894 ****************************************************************************/
2896 struct cli_unlock64_state
{
2901 static void cli_unlock64_done(struct tevent_req
*subreq
);
2903 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2904 struct tevent_context
*ev
,
2905 struct cli_state
*cli
,
2911 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2912 struct cli_unlock64_state
*state
= NULL
;
2913 uint8_t additional_flags
= 0;
2915 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2920 SCVAL(state
->vwv
+0, 0, 0xff);
2921 SSVAL(state
->vwv
+2, 0, fnum
);
2922 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2923 SIVALS(state
->vwv
+4, 0, 0);
2924 SSVAL(state
->vwv
+6, 0, 1);
2925 SSVAL(state
->vwv
+7, 0, 0);
2927 SIVAL(state
->data
, 0, cli_getpid(cli
));
2928 SOFF_T_R(state
->data
, 4, offset
);
2929 SOFF_T_R(state
->data
, 12, len
);
2931 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2932 8, state
->vwv
, 20, state
->data
);
2933 if (tevent_req_nomem(subreq
, req
)) {
2934 return tevent_req_post(req
, ev
);
2936 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2940 static void cli_unlock64_done(struct tevent_req
*subreq
)
2942 struct tevent_req
*req
= tevent_req_callback_data(
2943 subreq
, struct tevent_req
);
2946 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2947 TALLOC_FREE(subreq
);
2948 if (tevent_req_nterror(req
, status
)) {
2951 tevent_req_done(req
);
2954 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2956 return tevent_req_simple_recv_ntstatus(req
);
2959 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2964 TALLOC_CTX
*frame
= talloc_stackframe();
2965 struct tevent_context
*ev
;
2966 struct tevent_req
*req
;
2967 NTSTATUS status
= NT_STATUS_OK
;
2969 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2970 return cli_unlock(cli
, fnum
, offset
, len
);
2973 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2975 * Can't use sync call while an async call is in flight
2977 status
= NT_STATUS_INVALID_PARAMETER
;
2981 ev
= samba_tevent_context_init(frame
);
2983 status
= NT_STATUS_NO_MEMORY
;
2987 req
= cli_unlock64_send(frame
, ev
, cli
,
2990 status
= NT_STATUS_NO_MEMORY
;
2994 if (!tevent_req_poll(req
, ev
)) {
2995 status
= map_nt_error_from_unix(errno
);
2999 status
= cli_unlock64_recv(req
);
3006 /****************************************************************************
3007 Get/unlock a POSIX lock on a file - internal function.
3008 ****************************************************************************/
3010 struct posix_lock_state
{
3013 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3016 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3018 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3019 NULL
, 0, NULL
, NULL
, 0, NULL
);
3020 tevent_req_simple_finish_ntstatus(subreq
, status
);
3023 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3024 struct tevent_context
*ev
,
3025 struct cli_state
*cli
,
3030 enum brl_type lock_type
)
3032 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3033 struct posix_lock_state
*state
= NULL
;
3035 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3040 /* Setup setup word. */
3041 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3043 /* Setup param array. */
3044 SSVAL(&state
->param
, 0, fnum
);
3045 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3047 /* Setup data array. */
3048 switch (lock_type
) {
3050 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3051 POSIX_LOCK_TYPE_READ
);
3054 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3055 POSIX_LOCK_TYPE_WRITE
);
3058 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3059 POSIX_LOCK_TYPE_UNLOCK
);
3066 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3067 POSIX_LOCK_FLAG_WAIT
);
3069 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3070 POSIX_LOCK_FLAG_NOWAIT
);
3073 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3074 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3075 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3077 subreq
= cli_trans_send(state
, /* mem ctx. */
3078 ev
, /* event ctx. */
3079 cli
, /* cli_state. */
3080 SMBtrans2
, /* cmd. */
3081 NULL
, /* pipe name. */
3085 &state
->setup
, /* setup. */
3086 1, /* num setup uint16_t words. */
3087 0, /* max returned setup. */
3088 state
->param
, /* param. */
3090 2, /* max returned param. */
3091 state
->data
, /* data. */
3092 POSIX_LOCK_DATA_SIZE
, /* num data. */
3093 0); /* max returned data. */
3095 if (tevent_req_nomem(subreq
, req
)) {
3096 return tevent_req_post(req
, ev
);
3098 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3102 /****************************************************************************
3104 ****************************************************************************/
3106 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3107 struct tevent_context
*ev
,
3108 struct cli_state
*cli
,
3113 enum brl_type lock_type
)
3115 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3116 wait_lock
, lock_type
);
3119 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3121 return tevent_req_simple_recv_ntstatus(req
);
3124 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3125 uint64_t offset
, uint64_t len
,
3126 bool wait_lock
, enum brl_type lock_type
)
3128 TALLOC_CTX
*frame
= talloc_stackframe();
3129 struct tevent_context
*ev
= NULL
;
3130 struct tevent_req
*req
= NULL
;
3131 NTSTATUS status
= NT_STATUS_OK
;
3133 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3135 * Can't use sync call while an async call is in flight
3137 status
= NT_STATUS_INVALID_PARAMETER
;
3141 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3142 status
= NT_STATUS_INVALID_PARAMETER
;
3146 ev
= samba_tevent_context_init(frame
);
3148 status
= NT_STATUS_NO_MEMORY
;
3152 req
= cli_posix_lock_send(frame
,
3161 status
= NT_STATUS_NO_MEMORY
;
3165 if (!tevent_req_poll(req
, ev
)) {
3166 status
= map_nt_error_from_unix(errno
);
3170 status
= cli_posix_lock_recv(req
);
3177 /****************************************************************************
3178 POSIX Unlock a file.
3179 ****************************************************************************/
3181 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3182 struct tevent_context
*ev
,
3183 struct cli_state
*cli
,
3188 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3189 false, UNLOCK_LOCK
);
3192 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3194 return tevent_req_simple_recv_ntstatus(req
);
3197 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3199 TALLOC_CTX
*frame
= talloc_stackframe();
3200 struct tevent_context
*ev
= NULL
;
3201 struct tevent_req
*req
= NULL
;
3202 NTSTATUS status
= NT_STATUS_OK
;
3204 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3206 * Can't use sync call while an async call is in flight
3208 status
= NT_STATUS_INVALID_PARAMETER
;
3212 ev
= samba_tevent_context_init(frame
);
3214 status
= NT_STATUS_NO_MEMORY
;
3218 req
= cli_posix_unlock_send(frame
,
3225 status
= NT_STATUS_NO_MEMORY
;
3229 if (!tevent_req_poll(req
, ev
)) {
3230 status
= map_nt_error_from_unix(errno
);
3234 status
= cli_posix_unlock_recv(req
);
3241 /****************************************************************************
3242 Do a SMBgetattrE call.
3243 ****************************************************************************/
3245 static void cli_getattrE_done(struct tevent_req
*subreq
);
3247 struct cli_getattrE_state
{
3257 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3258 struct tevent_context
*ev
,
3259 struct cli_state
*cli
,
3262 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3263 struct cli_getattrE_state
*state
= NULL
;
3264 uint8_t additional_flags
= 0;
3266 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3271 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3272 SSVAL(state
->vwv
+0,0,fnum
);
3274 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3275 1, state
->vwv
, 0, NULL
);
3276 if (tevent_req_nomem(subreq
, req
)) {
3277 return tevent_req_post(req
, ev
);
3279 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3283 static void cli_getattrE_done(struct tevent_req
*subreq
)
3285 struct tevent_req
*req
= tevent_req_callback_data(
3286 subreq
, struct tevent_req
);
3287 struct cli_getattrE_state
*state
= tevent_req_data(
3288 req
, struct cli_getattrE_state
);
3290 uint16_t *vwv
= NULL
;
3293 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3295 TALLOC_FREE(subreq
);
3296 if (tevent_req_nterror(req
, status
)) {
3300 state
->size
= (off_t
)IVAL(vwv
+6,0);
3301 state
->attr
= SVAL(vwv
+10,0);
3302 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3303 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3304 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3306 tevent_req_done(req
);
3309 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3312 time_t *change_time
,
3313 time_t *access_time
,
3316 struct cli_getattrE_state
*state
= tevent_req_data(
3317 req
, struct cli_getattrE_state
);
3320 if (tevent_req_is_nterror(req
, &status
)) {
3324 *attr
= state
->attr
;
3327 *size
= state
->size
;
3330 *change_time
= state
->change_time
;
3333 *access_time
= state
->access_time
;
3336 *write_time
= state
->write_time
;
3338 return NT_STATUS_OK
;
3341 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3345 time_t *change_time
,
3346 time_t *access_time
,
3349 TALLOC_CTX
*frame
= talloc_stackframe();
3350 struct tevent_context
*ev
= NULL
;
3351 struct tevent_req
*req
= NULL
;
3352 NTSTATUS status
= NT_STATUS_OK
;
3354 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3356 * Can't use sync call while an async call is in flight
3358 status
= NT_STATUS_INVALID_PARAMETER
;
3362 ev
= samba_tevent_context_init(frame
);
3364 status
= NT_STATUS_NO_MEMORY
;
3368 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3370 status
= NT_STATUS_NO_MEMORY
;
3374 if (!tevent_req_poll(req
, ev
)) {
3375 status
= map_nt_error_from_unix(errno
);
3379 status
= cli_getattrE_recv(req
,
3391 /****************************************************************************
3393 ****************************************************************************/
3395 static void cli_getatr_done(struct tevent_req
*subreq
);
3397 struct cli_getatr_state
{
3404 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3405 struct tevent_context
*ev
,
3406 struct cli_state
*cli
,
3409 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3410 struct cli_getatr_state
*state
= NULL
;
3411 uint8_t additional_flags
= 0;
3412 uint8_t *bytes
= NULL
;
3414 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3419 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3421 bytes
= talloc_array(state
, uint8_t, 1);
3422 if (tevent_req_nomem(bytes
, req
)) {
3423 return tevent_req_post(req
, ev
);
3426 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3427 strlen(fname
)+1, NULL
);
3429 if (tevent_req_nomem(bytes
, req
)) {
3430 return tevent_req_post(req
, ev
);
3433 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3434 0, NULL
, talloc_get_size(bytes
), bytes
);
3435 if (tevent_req_nomem(subreq
, req
)) {
3436 return tevent_req_post(req
, ev
);
3438 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3442 static void cli_getatr_done(struct tevent_req
*subreq
)
3444 struct tevent_req
*req
= tevent_req_callback_data(
3445 subreq
, struct tevent_req
);
3446 struct cli_getatr_state
*state
= tevent_req_data(
3447 req
, struct cli_getatr_state
);
3449 uint16_t *vwv
= NULL
;
3452 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3454 TALLOC_FREE(subreq
);
3455 if (tevent_req_nterror(req
, status
)) {
3459 state
->attr
= SVAL(vwv
+0,0);
3460 state
->size
= (off_t
)IVAL(vwv
+3,0);
3461 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3463 tevent_req_done(req
);
3466 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3471 struct cli_getatr_state
*state
= tevent_req_data(
3472 req
, struct cli_getatr_state
);
3475 if (tevent_req_is_nterror(req
, &status
)) {
3479 *attr
= state
->attr
;
3482 *size
= state
->size
;
3485 *write_time
= state
->write_time
;
3487 return NT_STATUS_OK
;
3490 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3496 TALLOC_CTX
*frame
= talloc_stackframe();
3497 struct tevent_context
*ev
= NULL
;
3498 struct tevent_req
*req
= NULL
;
3499 NTSTATUS status
= NT_STATUS_OK
;
3501 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3503 * Can't use sync call while an async call is in flight
3505 status
= NT_STATUS_INVALID_PARAMETER
;
3509 ev
= samba_tevent_context_init(frame
);
3511 status
= NT_STATUS_NO_MEMORY
;
3515 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3517 status
= NT_STATUS_NO_MEMORY
;
3521 if (!tevent_req_poll(req
, ev
)) {
3522 status
= map_nt_error_from_unix(errno
);
3526 status
= cli_getatr_recv(req
,
3536 /****************************************************************************
3537 Do a SMBsetattrE call.
3538 ****************************************************************************/
3540 static void cli_setattrE_done(struct tevent_req
*subreq
);
3542 struct cli_setattrE_state
{
3546 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3547 struct tevent_context
*ev
,
3548 struct cli_state
*cli
,
3554 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3555 struct cli_setattrE_state
*state
= NULL
;
3556 uint8_t additional_flags
= 0;
3558 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3563 SSVAL(state
->vwv
+0, 0, fnum
);
3564 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3565 smb1cli_conn_server_time_zone(cli
->conn
));
3566 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3567 smb1cli_conn_server_time_zone(cli
->conn
));
3568 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3569 smb1cli_conn_server_time_zone(cli
->conn
));
3571 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3572 7, state
->vwv
, 0, NULL
);
3573 if (tevent_req_nomem(subreq
, req
)) {
3574 return tevent_req_post(req
, ev
);
3576 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3580 static void cli_setattrE_done(struct tevent_req
*subreq
)
3582 struct tevent_req
*req
= tevent_req_callback_data(
3583 subreq
, struct tevent_req
);
3586 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3587 TALLOC_FREE(subreq
);
3588 if (tevent_req_nterror(req
, status
)) {
3591 tevent_req_done(req
);
3594 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3596 return tevent_req_simple_recv_ntstatus(req
);
3599 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3605 TALLOC_CTX
*frame
= talloc_stackframe();
3606 struct tevent_context
*ev
= NULL
;
3607 struct tevent_req
*req
= NULL
;
3608 NTSTATUS status
= NT_STATUS_OK
;
3610 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3612 * Can't use sync call while an async call is in flight
3614 status
= NT_STATUS_INVALID_PARAMETER
;
3618 ev
= samba_tevent_context_init(frame
);
3620 status
= NT_STATUS_NO_MEMORY
;
3624 req
= cli_setattrE_send(frame
, ev
,
3632 status
= NT_STATUS_NO_MEMORY
;
3636 if (!tevent_req_poll(req
, ev
)) {
3637 status
= map_nt_error_from_unix(errno
);
3641 status
= cli_setattrE_recv(req
);
3648 /****************************************************************************
3649 Do a SMBsetatr call.
3650 ****************************************************************************/
3652 static void cli_setatr_done(struct tevent_req
*subreq
);
3654 struct cli_setatr_state
{
3658 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3659 struct tevent_context
*ev
,
3660 struct cli_state
*cli
,
3665 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3666 struct cli_setatr_state
*state
= NULL
;
3667 uint8_t additional_flags
= 0;
3668 uint8_t *bytes
= NULL
;
3670 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3675 SSVAL(state
->vwv
+0, 0, attr
);
3676 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3678 bytes
= talloc_array(state
, uint8_t, 1);
3679 if (tevent_req_nomem(bytes
, req
)) {
3680 return tevent_req_post(req
, ev
);
3683 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3684 strlen(fname
)+1, NULL
);
3685 if (tevent_req_nomem(bytes
, req
)) {
3686 return tevent_req_post(req
, ev
);
3688 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3689 talloc_get_size(bytes
)+1);
3690 if (tevent_req_nomem(bytes
, req
)) {
3691 return tevent_req_post(req
, ev
);
3694 bytes
[talloc_get_size(bytes
)-1] = 4;
3695 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3697 if (tevent_req_nomem(bytes
, req
)) {
3698 return tevent_req_post(req
, ev
);
3701 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3702 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3703 if (tevent_req_nomem(subreq
, req
)) {
3704 return tevent_req_post(req
, ev
);
3706 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3710 static void cli_setatr_done(struct tevent_req
*subreq
)
3712 struct tevent_req
*req
= tevent_req_callback_data(
3713 subreq
, struct tevent_req
);
3716 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3717 TALLOC_FREE(subreq
);
3718 if (tevent_req_nterror(req
, status
)) {
3721 tevent_req_done(req
);
3724 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3726 return tevent_req_simple_recv_ntstatus(req
);
3729 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3734 TALLOC_CTX
*frame
= talloc_stackframe();
3735 struct tevent_context
*ev
= NULL
;
3736 struct tevent_req
*req
= NULL
;
3737 NTSTATUS status
= NT_STATUS_OK
;
3739 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3741 * Can't use sync call while an async call is in flight
3743 status
= NT_STATUS_INVALID_PARAMETER
;
3747 ev
= samba_tevent_context_init(frame
);
3749 status
= NT_STATUS_NO_MEMORY
;
3753 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3755 status
= NT_STATUS_NO_MEMORY
;
3759 if (!tevent_req_poll(req
, ev
)) {
3760 status
= map_nt_error_from_unix(errno
);
3764 status
= cli_setatr_recv(req
);
3771 /****************************************************************************
3772 Check for existance of a dir.
3773 ****************************************************************************/
3775 static void cli_chkpath_done(struct tevent_req
*subreq
);
3777 struct cli_chkpath_state
{
3781 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3782 struct tevent_context
*ev
,
3783 struct cli_state
*cli
,
3786 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3787 struct cli_chkpath_state
*state
= NULL
;
3788 uint8_t additional_flags
= 0;
3789 uint8_t *bytes
= NULL
;
3791 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3796 bytes
= talloc_array(state
, uint8_t, 1);
3797 if (tevent_req_nomem(bytes
, req
)) {
3798 return tevent_req_post(req
, ev
);
3801 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3802 strlen(fname
)+1, NULL
);
3804 if (tevent_req_nomem(bytes
, req
)) {
3805 return tevent_req_post(req
, ev
);
3808 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3809 0, NULL
, talloc_get_size(bytes
), bytes
);
3810 if (tevent_req_nomem(subreq
, req
)) {
3811 return tevent_req_post(req
, ev
);
3813 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3817 static void cli_chkpath_done(struct tevent_req
*subreq
)
3819 struct tevent_req
*req
= tevent_req_callback_data(
3820 subreq
, struct tevent_req
);
3823 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3824 TALLOC_FREE(subreq
);
3825 if (tevent_req_nterror(req
, status
)) {
3828 tevent_req_done(req
);
3831 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3833 return tevent_req_simple_recv_ntstatus(req
);
3836 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3838 TALLOC_CTX
*frame
= talloc_stackframe();
3839 struct tevent_context
*ev
= NULL
;
3840 struct tevent_req
*req
= NULL
;
3842 NTSTATUS status
= NT_STATUS_OK
;
3844 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3846 * Can't use sync call while an async call is in flight
3848 status
= NT_STATUS_INVALID_PARAMETER
;
3852 path2
= talloc_strdup(frame
, path
);
3854 status
= NT_STATUS_NO_MEMORY
;
3857 trim_char(path2
,'\0','\\');
3859 path2
= talloc_strdup(frame
, "\\");
3861 status
= NT_STATUS_NO_MEMORY
;
3866 ev
= samba_tevent_context_init(frame
);
3868 status
= NT_STATUS_NO_MEMORY
;
3872 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3874 status
= NT_STATUS_NO_MEMORY
;
3878 if (!tevent_req_poll(req
, ev
)) {
3879 status
= map_nt_error_from_unix(errno
);
3883 status
= cli_chkpath_recv(req
);
3890 /****************************************************************************
3892 ****************************************************************************/
3894 static void cli_dskattr_done(struct tevent_req
*subreq
);
3896 struct cli_dskattr_state
{
3902 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3903 struct tevent_context
*ev
,
3904 struct cli_state
*cli
)
3906 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3907 struct cli_dskattr_state
*state
= NULL
;
3908 uint8_t additional_flags
= 0;
3910 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3915 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3917 if (tevent_req_nomem(subreq
, req
)) {
3918 return tevent_req_post(req
, ev
);
3920 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3924 static void cli_dskattr_done(struct tevent_req
*subreq
)
3926 struct tevent_req
*req
= tevent_req_callback_data(
3927 subreq
, struct tevent_req
);
3928 struct cli_dskattr_state
*state
= tevent_req_data(
3929 req
, struct cli_dskattr_state
);
3931 uint16_t *vwv
= NULL
;
3934 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3936 TALLOC_FREE(subreq
);
3937 if (tevent_req_nterror(req
, status
)) {
3940 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3941 state
->total
= SVAL(vwv
+0, 0);
3942 state
->avail
= SVAL(vwv
+3, 0);
3943 tevent_req_done(req
);
3946 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3948 struct cli_dskattr_state
*state
= tevent_req_data(
3949 req
, struct cli_dskattr_state
);
3952 if (tevent_req_is_nterror(req
, &status
)) {
3955 *bsize
= state
->bsize
;
3956 *total
= state
->total
;
3957 *avail
= state
->avail
;
3958 return NT_STATUS_OK
;
3961 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3963 TALLOC_CTX
*frame
= talloc_stackframe();
3964 struct tevent_context
*ev
= NULL
;
3965 struct tevent_req
*req
= NULL
;
3966 NTSTATUS status
= NT_STATUS_OK
;
3968 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3970 * Can't use sync call while an async call is in flight
3972 status
= NT_STATUS_INVALID_PARAMETER
;
3976 ev
= samba_tevent_context_init(frame
);
3978 status
= NT_STATUS_NO_MEMORY
;
3982 req
= cli_dskattr_send(frame
, ev
, cli
);
3984 status
= NT_STATUS_NO_MEMORY
;
3988 if (!tevent_req_poll(req
, ev
)) {
3989 status
= map_nt_error_from_unix(errno
);
3993 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4000 /****************************************************************************
4001 Create and open a temporary file.
4002 ****************************************************************************/
4004 static void cli_ctemp_done(struct tevent_req
*subreq
);
4006 struct ctemp_state
{
4012 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4013 struct tevent_context
*ev
,
4014 struct cli_state
*cli
,
4017 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4018 struct ctemp_state
*state
= NULL
;
4019 uint8_t additional_flags
= 0;
4020 uint8_t *bytes
= NULL
;
4022 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4027 SSVAL(state
->vwv
,0,0);
4028 SIVALS(state
->vwv
+1,0,-1);
4030 bytes
= talloc_array(state
, uint8_t, 1);
4031 if (tevent_req_nomem(bytes
, req
)) {
4032 return tevent_req_post(req
, ev
);
4035 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4036 strlen(path
)+1, NULL
);
4037 if (tevent_req_nomem(bytes
, req
)) {
4038 return tevent_req_post(req
, ev
);
4041 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4042 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4043 if (tevent_req_nomem(subreq
, req
)) {
4044 return tevent_req_post(req
, ev
);
4046 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4050 static void cli_ctemp_done(struct tevent_req
*subreq
)
4052 struct tevent_req
*req
= tevent_req_callback_data(
4053 subreq
, struct tevent_req
);
4054 struct ctemp_state
*state
= tevent_req_data(
4055 req
, struct ctemp_state
);
4059 uint32_t num_bytes
= 0;
4060 uint8_t *bytes
= NULL
;
4062 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4063 &num_bytes
, &bytes
);
4064 TALLOC_FREE(subreq
);
4065 if (tevent_req_nterror(req
, status
)) {
4069 state
->fnum
= SVAL(vwv
+0, 0);
4071 /* From W2K3, the result is just the ASCII name */
4072 if (num_bytes
< 2) {
4073 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4077 if (pull_string_talloc(state
,
4084 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4087 tevent_req_done(req
);
4090 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4095 struct ctemp_state
*state
= tevent_req_data(req
,
4096 struct ctemp_state
);
4099 if (tevent_req_is_nterror(req
, &status
)) {
4102 *pfnum
= state
->fnum
;
4103 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4105 return NT_STATUS_NO_MEMORY
;
4107 return NT_STATUS_OK
;
4110 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4116 TALLOC_CTX
*frame
= talloc_stackframe();
4117 struct tevent_context
*ev
;
4118 struct tevent_req
*req
;
4119 NTSTATUS status
= NT_STATUS_OK
;
4121 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4123 * Can't use sync call while an async call is in flight
4125 status
= NT_STATUS_INVALID_PARAMETER
;
4129 ev
= samba_tevent_context_init(frame
);
4131 status
= NT_STATUS_NO_MEMORY
;
4135 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4137 status
= NT_STATUS_NO_MEMORY
;
4141 if (!tevent_req_poll(req
, ev
)) {
4142 status
= map_nt_error_from_unix(errno
);
4146 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4154 send a raw ioctl - used by the torture code
4156 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4161 SSVAL(vwv
+0, 0, fnum
);
4162 SSVAL(vwv
+1, 0, code
>>16);
4163 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4165 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4166 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4167 if (!NT_STATUS_IS_OK(status
)) {
4170 *blob
= data_blob_null
;
4171 return NT_STATUS_OK
;
4174 /*********************************************************
4175 Set an extended attribute utility fn.
4176 *********************************************************/
4178 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4179 uint8_t *param
, unsigned int param_len
,
4180 const char *ea_name
,
4181 const char *ea_val
, size_t ea_len
)
4184 unsigned int data_len
= 0;
4185 uint8_t *data
= NULL
;
4187 size_t ea_namelen
= strlen(ea_name
);
4190 SSVAL(setup
, 0, setup_val
);
4192 if (ea_namelen
== 0 && ea_len
== 0) {
4194 data
= talloc_array(talloc_tos(),
4198 return NT_STATUS_NO_MEMORY
;
4201 SIVAL(p
,0,data_len
);
4203 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4204 data
= talloc_array(talloc_tos(),
4208 return NT_STATUS_NO_MEMORY
;
4211 SIVAL(p
,0,data_len
);
4213 SCVAL(p
, 0, 0); /* EA flags. */
4214 SCVAL(p
, 1, ea_namelen
);
4215 SSVAL(p
, 2, ea_len
);
4216 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4217 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4220 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4222 param
, param_len
, 2,
4223 data
, data_len
, CLI_BUFFER_SIZE
,
4225 NULL
, 0, NULL
, /* rsetup */
4226 NULL
, 0, NULL
, /* rparam */
4227 NULL
, 0, NULL
); /* rdata */
4232 /*********************************************************
4233 Set an extended attribute on a pathname.
4234 *********************************************************/
4236 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4237 const char *ea_name
, const char *ea_val
,
4240 unsigned int param_len
= 0;
4243 TALLOC_CTX
*frame
= talloc_stackframe();
4245 param
= talloc_array(talloc_tos(), uint8_t, 6);
4247 return NT_STATUS_NO_MEMORY
;
4249 SSVAL(param
,0,SMB_INFO_SET_EA
);
4253 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4254 path
, strlen(path
)+1,
4256 param_len
= talloc_get_size(param
);
4258 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4259 ea_name
, ea_val
, ea_len
);
4264 /*********************************************************
4265 Set an extended attribute on an fnum.
4266 *********************************************************/
4268 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4269 const char *ea_name
, const char *ea_val
,
4274 memset(param
, 0, 6);
4275 SSVAL(param
,0,fnum
);
4276 SSVAL(param
,2,SMB_INFO_SET_EA
);
4278 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4279 ea_name
, ea_val
, ea_len
);
4282 /*********************************************************
4283 Get an extended attribute list utility fn.
4284 *********************************************************/
4286 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4288 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4290 struct ea_struct
*ea_list
= NULL
;
4295 if (rdata_len
< 4) {
4299 ea_size
= (size_t)IVAL(rdata
,0);
4300 if (ea_size
> rdata_len
) {
4305 /* No EA's present. */
4314 /* Validate the EA list and count it. */
4315 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4316 unsigned int ea_namelen
= CVAL(p
,1);
4317 unsigned int ea_valuelen
= SVAL(p
,2);
4318 if (ea_namelen
== 0) {
4321 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4324 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4325 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4334 *pnum_eas
= num_eas
;
4336 /* Caller only wants number of EA's. */
4340 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4345 ea_size
= (size_t)IVAL(rdata
,0);
4348 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4349 struct ea_struct
*ea
= &ea_list
[num_eas
];
4350 fstring unix_ea_name
;
4351 unsigned int ea_namelen
= CVAL(p
,1);
4352 unsigned int ea_valuelen
= SVAL(p
,2);
4354 ea
->flags
= CVAL(p
,0);
4355 unix_ea_name
[0] = '\0';
4356 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4357 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4361 /* Ensure the value is null terminated (in case it's a string). */
4362 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4363 if (!ea
->value
.data
) {
4367 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4369 ea
->value
.data
[ea_valuelen
] = 0;
4371 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4374 *pea_list
= ea_list
;
4378 TALLOC_FREE(ea_list
);
4382 /*********************************************************
4383 Get an extended attribute list from a pathname.
4384 *********************************************************/
4386 struct cli_get_ea_list_path_state
{
4391 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4393 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4394 struct tevent_context
*ev
,
4395 struct cli_state
*cli
,
4398 struct tevent_req
*req
, *subreq
;
4399 struct cli_get_ea_list_path_state
*state
;
4401 req
= tevent_req_create(mem_ctx
, &state
,
4402 struct cli_get_ea_list_path_state
);
4406 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4407 SMB_INFO_QUERY_ALL_EAS
, 4,
4409 if (tevent_req_nomem(subreq
, req
)) {
4410 return tevent_req_post(req
, ev
);
4412 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4416 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4418 struct tevent_req
*req
= tevent_req_callback_data(
4419 subreq
, struct tevent_req
);
4420 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4421 req
, struct cli_get_ea_list_path_state
);
4424 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4426 TALLOC_FREE(subreq
);
4427 if (tevent_req_nterror(req
, status
)) {
4430 tevent_req_done(req
);
4433 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4434 size_t *pnum_eas
, struct ea_struct
**peas
)
4436 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4437 req
, struct cli_get_ea_list_path_state
);
4440 if (tevent_req_is_nterror(req
, &status
)) {
4443 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4445 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4447 return NT_STATUS_OK
;
4450 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4453 struct ea_struct
**pea_list
)
4455 TALLOC_CTX
*frame
= talloc_stackframe();
4456 struct tevent_context
*ev
= NULL
;
4457 struct tevent_req
*req
= NULL
;
4458 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4460 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4462 * Can't use sync call while an async call is in flight
4464 status
= NT_STATUS_INVALID_PARAMETER
;
4467 ev
= samba_tevent_context_init(frame
);
4471 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4475 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4478 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4484 /****************************************************************************
4485 Convert open "flags" arg to uint32_t on wire.
4486 ****************************************************************************/
4488 static uint32_t open_flags_to_wire(int flags
)
4490 int open_mode
= flags
& O_ACCMODE
;
4493 switch (open_mode
) {
4495 ret
|= SMB_O_WRONLY
;
4502 ret
|= SMB_O_RDONLY
;
4506 if (flags
& O_CREAT
) {
4509 if (flags
& O_EXCL
) {
4512 if (flags
& O_TRUNC
) {
4516 if (flags
& O_SYNC
) {
4520 if (flags
& O_APPEND
) {
4521 ret
|= SMB_O_APPEND
;
4523 #if defined(O_DIRECT)
4524 if (flags
& O_DIRECT
) {
4525 ret
|= SMB_O_DIRECT
;
4528 #if defined(O_DIRECTORY)
4529 if (flags
& O_DIRECTORY
) {
4530 ret
|= SMB_O_DIRECTORY
;
4536 /****************************************************************************
4537 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4538 ****************************************************************************/
4540 struct posix_open_state
{
4544 uint16_t fnum
; /* Out */
4547 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4549 struct tevent_req
*req
= tevent_req_callback_data(
4550 subreq
, struct tevent_req
);
4551 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4556 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4557 NULL
, 0, NULL
, &data
, 12, &num_data
);
4558 TALLOC_FREE(subreq
);
4559 if (tevent_req_nterror(req
, status
)) {
4562 state
->fnum
= SVAL(data
,2);
4563 tevent_req_done(req
);
4566 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4567 struct tevent_context
*ev
,
4568 struct cli_state
*cli
,
4574 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4575 struct posix_open_state
*state
= NULL
;
4576 uint32_t wire_flags
= open_flags_to_wire(flags
);
4578 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4583 /* Setup setup word. */
4584 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4586 /* Setup param array. */
4587 state
->param
= talloc_array(state
, uint8_t, 6);
4588 if (tevent_req_nomem(state
->param
, req
)) {
4589 return tevent_req_post(req
, ev
);
4591 memset(state
->param
, '\0', 6);
4592 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4594 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4595 strlen(fname
)+1, NULL
);
4597 if (tevent_req_nomem(state
->param
, req
)) {
4598 return tevent_req_post(req
, ev
);
4601 /* Setup data words. */
4603 wire_flags
|= SMB_O_DIRECTORY
;
4606 SIVAL(state
->data
,0,0); /* No oplock. */
4607 SIVAL(state
->data
,4,wire_flags
);
4608 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4609 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4610 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4612 subreq
= cli_trans_send(state
, /* mem ctx. */
4613 ev
, /* event ctx. */
4614 cli
, /* cli_state. */
4615 SMBtrans2
, /* cmd. */
4616 NULL
, /* pipe name. */
4620 &state
->setup
, /* setup. */
4621 1, /* num setup uint16_t words. */
4622 0, /* max returned setup. */
4623 state
->param
, /* param. */
4624 talloc_get_size(state
->param
),/* num param. */
4625 2, /* max returned param. */
4626 state
->data
, /* data. */
4628 12); /* max returned data. */
4630 if (tevent_req_nomem(subreq
, req
)) {
4631 return tevent_req_post(req
, ev
);
4633 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4637 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4638 struct tevent_context
*ev
,
4639 struct cli_state
*cli
,
4644 return cli_posix_open_internal_send(mem_ctx
, ev
,
4645 cli
, fname
, flags
, mode
, false);
4648 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4650 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4653 if (tevent_req_is_nterror(req
, &status
)) {
4656 *pfnum
= state
->fnum
;
4657 return NT_STATUS_OK
;
4660 /****************************************************************************
4661 Open - POSIX semantics. Doesn't request oplock.
4662 ****************************************************************************/
4664 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4665 int flags
, mode_t mode
, uint16_t *pfnum
)
4668 TALLOC_CTX
*frame
= talloc_stackframe();
4669 struct tevent_context
*ev
= NULL
;
4670 struct tevent_req
*req
= NULL
;
4671 NTSTATUS status
= NT_STATUS_OK
;
4673 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4675 * Can't use sync call while an async call is in flight
4677 status
= NT_STATUS_INVALID_PARAMETER
;
4681 ev
= samba_tevent_context_init(frame
);
4683 status
= NT_STATUS_NO_MEMORY
;
4687 req
= cli_posix_open_send(frame
,
4694 status
= NT_STATUS_NO_MEMORY
;
4698 if (!tevent_req_poll(req
, ev
)) {
4699 status
= map_nt_error_from_unix(errno
);
4703 status
= cli_posix_open_recv(req
, pfnum
);
4710 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4711 struct tevent_context
*ev
,
4712 struct cli_state
*cli
,
4716 return cli_posix_open_internal_send(mem_ctx
, ev
,
4717 cli
, fname
, O_CREAT
, mode
, true);
4720 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4722 return tevent_req_simple_recv_ntstatus(req
);
4725 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4727 TALLOC_CTX
*frame
= talloc_stackframe();
4728 struct tevent_context
*ev
= NULL
;
4729 struct tevent_req
*req
= NULL
;
4730 NTSTATUS status
= NT_STATUS_OK
;
4732 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4734 * Can't use sync call while an async call is in flight
4736 status
= NT_STATUS_INVALID_PARAMETER
;
4740 ev
= samba_tevent_context_init(frame
);
4742 status
= NT_STATUS_NO_MEMORY
;
4746 req
= cli_posix_mkdir_send(frame
,
4752 status
= NT_STATUS_NO_MEMORY
;
4756 if (!tevent_req_poll(req
, ev
)) {
4757 status
= map_nt_error_from_unix(errno
);
4761 status
= cli_posix_mkdir_recv(req
);
4768 /****************************************************************************
4769 unlink or rmdir - POSIX semantics.
4770 ****************************************************************************/
4772 struct cli_posix_unlink_internal_state
{
4776 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4778 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4779 struct tevent_context
*ev
,
4780 struct cli_state
*cli
,
4784 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4785 struct cli_posix_unlink_internal_state
*state
= NULL
;
4787 req
= tevent_req_create(mem_ctx
, &state
,
4788 struct cli_posix_unlink_internal_state
);
4793 /* Setup data word. */
4794 SSVAL(state
->data
, 0, level
);
4796 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4797 SMB_POSIX_PATH_UNLINK
,
4799 state
->data
, sizeof(state
->data
));
4800 if (tevent_req_nomem(subreq
, req
)) {
4801 return tevent_req_post(req
, ev
);
4803 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4807 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4809 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4810 tevent_req_simple_finish_ntstatus(subreq
, status
);
4813 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4814 struct tevent_context
*ev
,
4815 struct cli_state
*cli
,
4818 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4819 SMB_POSIX_UNLINK_FILE_TARGET
);
4822 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4824 return tevent_req_simple_recv_ntstatus(req
);
4827 /****************************************************************************
4828 unlink - POSIX semantics.
4829 ****************************************************************************/
4831 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4833 TALLOC_CTX
*frame
= talloc_stackframe();
4834 struct tevent_context
*ev
= NULL
;
4835 struct tevent_req
*req
= NULL
;
4836 NTSTATUS status
= NT_STATUS_OK
;
4838 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4840 * Can't use sync call while an async call is in flight
4842 status
= NT_STATUS_INVALID_PARAMETER
;
4846 ev
= samba_tevent_context_init(frame
);
4848 status
= NT_STATUS_NO_MEMORY
;
4852 req
= cli_posix_unlink_send(frame
,
4857 status
= NT_STATUS_NO_MEMORY
;
4861 if (!tevent_req_poll(req
, ev
)) {
4862 status
= map_nt_error_from_unix(errno
);
4866 status
= cli_posix_unlink_recv(req
);
4873 /****************************************************************************
4874 rmdir - POSIX semantics.
4875 ****************************************************************************/
4877 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4878 struct tevent_context
*ev
,
4879 struct cli_state
*cli
,
4882 return cli_posix_unlink_internal_send(
4883 mem_ctx
, ev
, cli
, fname
,
4884 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4887 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4889 return tevent_req_simple_recv_ntstatus(req
);
4892 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4894 TALLOC_CTX
*frame
= talloc_stackframe();
4895 struct tevent_context
*ev
= NULL
;
4896 struct tevent_req
*req
= NULL
;
4897 NTSTATUS status
= NT_STATUS_OK
;
4899 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4901 * Can't use sync call while an async call is in flight
4903 status
= NT_STATUS_INVALID_PARAMETER
;
4907 ev
= samba_tevent_context_init(frame
);
4909 status
= NT_STATUS_NO_MEMORY
;
4913 req
= cli_posix_rmdir_send(frame
,
4918 status
= NT_STATUS_NO_MEMORY
;
4922 if (!tevent_req_poll(req
, ev
)) {
4923 status
= map_nt_error_from_unix(errno
);
4927 status
= cli_posix_rmdir_recv(req
, frame
);
4934 /****************************************************************************
4936 ****************************************************************************/
4938 struct cli_notify_state
{
4940 uint32_t num_changes
;
4941 struct notify_change
*changes
;
4944 static void cli_notify_done(struct tevent_req
*subreq
);
4946 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4947 struct tevent_context
*ev
,
4948 struct cli_state
*cli
, uint16_t fnum
,
4949 uint32_t buffer_size
,
4950 uint32_t completion_filter
, bool recursive
)
4952 struct tevent_req
*req
, *subreq
;
4953 struct cli_notify_state
*state
;
4954 unsigned old_timeout
;
4956 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
4961 SIVAL(state
->setup
, 0, completion_filter
);
4962 SSVAL(state
->setup
, 4, fnum
);
4963 SSVAL(state
->setup
, 6, recursive
);
4966 * Notifies should not time out
4968 old_timeout
= cli_set_timeout(cli
, 0);
4970 subreq
= cli_trans_send(
4971 state
, /* mem ctx. */
4972 ev
, /* event ctx. */
4973 cli
, /* cli_state. */
4974 SMBnttrans
, /* cmd. */
4975 NULL
, /* pipe name. */
4977 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
4979 (uint16_t *)state
->setup
, /* setup. */
4980 4, /* num setup uint16_t words. */
4981 0, /* max returned setup. */
4984 buffer_size
, /* max returned param. */
4987 0); /* max returned data. */
4989 cli_set_timeout(cli
, old_timeout
);
4991 if (tevent_req_nomem(subreq
, req
)) {
4992 return tevent_req_post(req
, ev
);
4994 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
4998 static void cli_notify_done(struct tevent_req
*subreq
)
5000 struct tevent_req
*req
= tevent_req_callback_data(
5001 subreq
, struct tevent_req
);
5002 struct cli_notify_state
*state
= tevent_req_data(
5003 req
, struct cli_notify_state
);
5006 uint32_t i
, ofs
, num_params
;
5009 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5010 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5011 TALLOC_FREE(subreq
);
5012 if (tevent_req_nterror(req
, status
)) {
5013 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5017 state
->num_changes
= 0;
5020 while (num_params
- ofs
> 12) {
5021 uint32_t next
= IVAL(params
, ofs
);
5022 state
->num_changes
+= 1;
5024 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5030 state
->changes
= talloc_array(state
, struct notify_change
,
5031 state
->num_changes
);
5032 if (tevent_req_nomem(state
->changes
, req
)) {
5033 TALLOC_FREE(params
);
5039 for (i
=0; i
<state
->num_changes
; i
++) {
5040 uint32_t next
= IVAL(params
, ofs
);
5041 uint32_t len
= IVAL(params
, ofs
+8);
5045 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5046 TALLOC_FREE(params
);
5048 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5052 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5053 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5054 &name
, params
+ofs
+12, len
,
5055 STR_TERMINATE
|STR_UNICODE
);
5057 TALLOC_FREE(params
);
5058 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5061 state
->changes
[i
].name
= name
;
5065 TALLOC_FREE(params
);
5066 tevent_req_done(req
);
5069 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5070 uint32_t *pnum_changes
,
5071 struct notify_change
**pchanges
)
5073 struct cli_notify_state
*state
= tevent_req_data(
5074 req
, struct cli_notify_state
);
5077 if (tevent_req_is_nterror(req
, &status
)) {
5081 *pnum_changes
= state
->num_changes
;
5082 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5083 return NT_STATUS_OK
;
5086 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5087 uint32_t completion_filter
, bool recursive
,
5088 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5089 struct notify_change
**pchanges
)
5091 TALLOC_CTX
*frame
= talloc_stackframe();
5092 struct tevent_context
*ev
;
5093 struct tevent_req
*req
;
5094 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5096 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5098 * Can't use sync call while an async call is in flight
5100 status
= NT_STATUS_INVALID_PARAMETER
;
5103 ev
= samba_tevent_context_init(frame
);
5107 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5108 completion_filter
, recursive
);
5112 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5115 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5121 struct cli_qpathinfo_state
{
5130 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5132 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5133 struct tevent_context
*ev
,
5134 struct cli_state
*cli
, const char *fname
,
5135 uint16_t level
, uint32_t min_rdata
,
5138 struct tevent_req
*req
, *subreq
;
5139 struct cli_qpathinfo_state
*state
;
5141 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5145 state
->min_rdata
= min_rdata
;
5146 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5148 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5149 if (tevent_req_nomem(state
->param
, req
)) {
5150 return tevent_req_post(req
, ev
);
5152 SSVAL(state
->param
, 0, level
);
5153 state
->param
= trans2_bytes_push_str(
5154 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5155 if (tevent_req_nomem(state
->param
, req
)) {
5156 return tevent_req_post(req
, ev
);
5159 subreq
= cli_trans_send(
5160 state
, /* mem ctx. */
5161 ev
, /* event ctx. */
5162 cli
, /* cli_state. */
5163 SMBtrans2
, /* cmd. */
5164 NULL
, /* pipe name. */
5168 state
->setup
, /* setup. */
5169 1, /* num setup uint16_t words. */
5170 0, /* max returned setup. */
5171 state
->param
, /* param. */
5172 talloc_get_size(state
->param
), /* num param. */
5173 2, /* max returned param. */
5176 max_rdata
); /* max returned data. */
5178 if (tevent_req_nomem(subreq
, req
)) {
5179 return tevent_req_post(req
, ev
);
5181 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5185 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5187 struct tevent_req
*req
= tevent_req_callback_data(
5188 subreq
, struct tevent_req
);
5189 struct cli_qpathinfo_state
*state
= tevent_req_data(
5190 req
, struct cli_qpathinfo_state
);
5193 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5195 &state
->rdata
, state
->min_rdata
,
5197 if (tevent_req_nterror(req
, status
)) {
5200 tevent_req_done(req
);
5203 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5204 uint8_t **rdata
, uint32_t *num_rdata
)
5206 struct cli_qpathinfo_state
*state
= tevent_req_data(
5207 req
, struct cli_qpathinfo_state
);
5210 if (tevent_req_is_nterror(req
, &status
)) {
5213 if (rdata
!= NULL
) {
5214 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5216 TALLOC_FREE(state
->rdata
);
5218 if (num_rdata
!= NULL
) {
5219 *num_rdata
= state
->num_rdata
;
5221 return NT_STATUS_OK
;
5224 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5225 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5227 uint8_t **rdata
, uint32_t *num_rdata
)
5229 TALLOC_CTX
*frame
= talloc_stackframe();
5230 struct tevent_context
*ev
;
5231 struct tevent_req
*req
;
5232 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5234 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5236 * Can't use sync call while an async call is in flight
5238 status
= NT_STATUS_INVALID_PARAMETER
;
5241 ev
= samba_tevent_context_init(frame
);
5245 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5250 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5253 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5259 struct cli_qfileinfo_state
{
5263 uint16_t recv_flags2
;
5269 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5271 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5272 struct tevent_context
*ev
,
5273 struct cli_state
*cli
, uint16_t fnum
,
5274 uint16_t level
, uint32_t min_rdata
,
5277 struct tevent_req
*req
, *subreq
;
5278 struct cli_qfileinfo_state
*state
;
5280 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5284 state
->min_rdata
= min_rdata
;
5285 SSVAL(state
->param
, 0, fnum
);
5286 SSVAL(state
->param
, 2, level
);
5287 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5289 subreq
= cli_trans_send(
5290 state
, /* mem ctx. */
5291 ev
, /* event ctx. */
5292 cli
, /* cli_state. */
5293 SMBtrans2
, /* cmd. */
5294 NULL
, /* pipe name. */
5298 state
->setup
, /* setup. */
5299 1, /* num setup uint16_t words. */
5300 0, /* max returned setup. */
5301 state
->param
, /* param. */
5302 sizeof(state
->param
), /* num param. */
5303 2, /* max returned param. */
5306 max_rdata
); /* max returned data. */
5308 if (tevent_req_nomem(subreq
, req
)) {
5309 return tevent_req_post(req
, ev
);
5311 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5315 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5317 struct tevent_req
*req
= tevent_req_callback_data(
5318 subreq
, struct tevent_req
);
5319 struct cli_qfileinfo_state
*state
= tevent_req_data(
5320 req
, struct cli_qfileinfo_state
);
5323 status
= cli_trans_recv(subreq
, state
,
5324 &state
->recv_flags2
,
5327 &state
->rdata
, state
->min_rdata
,
5329 if (tevent_req_nterror(req
, status
)) {
5332 tevent_req_done(req
);
5335 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5336 uint16_t *recv_flags2
,
5337 uint8_t **rdata
, uint32_t *num_rdata
)
5339 struct cli_qfileinfo_state
*state
= tevent_req_data(
5340 req
, struct cli_qfileinfo_state
);
5343 if (tevent_req_is_nterror(req
, &status
)) {
5347 if (recv_flags2
!= NULL
) {
5348 *recv_flags2
= state
->recv_flags2
;
5350 if (rdata
!= NULL
) {
5351 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5353 TALLOC_FREE(state
->rdata
);
5355 if (num_rdata
!= NULL
) {
5356 *num_rdata
= state
->num_rdata
;
5358 return NT_STATUS_OK
;
5361 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5362 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5363 uint32_t max_rdata
, uint16_t *recv_flags2
,
5364 uint8_t **rdata
, uint32_t *num_rdata
)
5366 TALLOC_CTX
*frame
= talloc_stackframe();
5367 struct tevent_context
*ev
;
5368 struct tevent_req
*req
;
5369 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5371 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5373 * Can't use sync call while an async call is in flight
5375 status
= NT_STATUS_INVALID_PARAMETER
;
5378 ev
= samba_tevent_context_init(frame
);
5382 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5387 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5390 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5396 struct cli_flush_state
{
5400 static void cli_flush_done(struct tevent_req
*subreq
);
5402 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5403 struct tevent_context
*ev
,
5404 struct cli_state
*cli
,
5407 struct tevent_req
*req
, *subreq
;
5408 struct cli_flush_state
*state
;
5410 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5414 SSVAL(state
->vwv
+ 0, 0, fnum
);
5416 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5418 if (tevent_req_nomem(subreq
, req
)) {
5419 return tevent_req_post(req
, ev
);
5421 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5425 static void cli_flush_done(struct tevent_req
*subreq
)
5427 struct tevent_req
*req
= tevent_req_callback_data(
5428 subreq
, struct tevent_req
);
5431 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5432 TALLOC_FREE(subreq
);
5433 if (tevent_req_nterror(req
, status
)) {
5436 tevent_req_done(req
);
5439 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5441 return tevent_req_simple_recv_ntstatus(req
);
5444 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5446 TALLOC_CTX
*frame
= talloc_stackframe();
5447 struct tevent_context
*ev
;
5448 struct tevent_req
*req
;
5449 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5451 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5453 * Can't use sync call while an async call is in flight
5455 status
= NT_STATUS_INVALID_PARAMETER
;
5458 ev
= samba_tevent_context_init(frame
);
5462 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5466 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5469 status
= cli_flush_recv(req
);
5475 struct cli_shadow_copy_data_state
{
5482 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5484 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5485 struct tevent_context
*ev
,
5486 struct cli_state
*cli
,
5490 struct tevent_req
*req
, *subreq
;
5491 struct cli_shadow_copy_data_state
*state
;
5494 req
= tevent_req_create(mem_ctx
, &state
,
5495 struct cli_shadow_copy_data_state
);
5499 state
->get_names
= get_names
;
5500 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5502 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5503 SSVAL(state
->setup
+ 2, 0, fnum
);
5504 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5505 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5507 subreq
= cli_trans_send(
5508 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5509 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5512 if (tevent_req_nomem(subreq
, req
)) {
5513 return tevent_req_post(req
, ev
);
5515 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5519 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5521 struct tevent_req
*req
= tevent_req_callback_data(
5522 subreq
, struct tevent_req
);
5523 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5524 req
, struct cli_shadow_copy_data_state
);
5527 status
= cli_trans_recv(subreq
, state
, NULL
,
5528 NULL
, 0, NULL
, /* setup */
5529 NULL
, 0, NULL
, /* param */
5530 &state
->data
, 12, &state
->num_data
);
5531 TALLOC_FREE(subreq
);
5532 if (tevent_req_nterror(req
, status
)) {
5535 tevent_req_done(req
);
5538 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5539 char ***pnames
, int *pnum_names
)
5541 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5542 req
, struct cli_shadow_copy_data_state
);
5548 if (tevent_req_is_nterror(req
, &status
)) {
5551 num_names
= IVAL(state
->data
, 4);
5552 dlength
= IVAL(state
->data
, 8);
5554 if (!state
->get_names
) {
5555 *pnum_names
= num_names
;
5556 return NT_STATUS_OK
;
5559 if (dlength
+12 > state
->num_data
) {
5560 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5562 names
= talloc_array(mem_ctx
, char *, num_names
);
5563 if (names
== NULL
) {
5564 return NT_STATUS_NO_MEMORY
;
5567 for (i
=0; i
<num_names
; i
++) {
5570 size_t converted_size
;
5572 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5573 ret
= convert_string_talloc(
5574 names
, CH_UTF16LE
, CH_UNIX
,
5575 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5576 &names
[i
], &converted_size
);
5579 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5582 *pnum_names
= num_names
;
5584 return NT_STATUS_OK
;
5587 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5588 uint16_t fnum
, bool get_names
,
5589 char ***pnames
, int *pnum_names
)
5591 TALLOC_CTX
*frame
= talloc_stackframe();
5592 struct tevent_context
*ev
;
5593 struct tevent_req
*req
;
5594 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5596 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5598 * Can't use sync call while an async call is in flight
5600 status
= NT_STATUS_INVALID_PARAMETER
;
5603 ev
= samba_tevent_context_init(frame
);
5607 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5611 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5614 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);