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
= talloc_stackframe();
1419 struct tevent_context
*ev
;
1420 struct tevent_req
*req
;
1421 NTSTATUS status
= NT_STATUS_OK
;
1423 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1425 * Can't use sync call while an async call is in flight
1427 status
= NT_STATUS_INVALID_PARAMETER
;
1431 ev
= samba_tevent_context_init(frame
);
1433 status
= NT_STATUS_NO_MEMORY
;
1437 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1439 status
= NT_STATUS_NO_MEMORY
;
1443 if (!tevent_req_poll(req
, ev
)) {
1444 status
= map_nt_error_from_unix(errno
);
1448 status
= cli_unlink_recv(req
);
1455 /****************************************************************************
1457 ****************************************************************************/
1459 static void cli_mkdir_done(struct tevent_req
*subreq
);
1461 struct cli_mkdir_state
{
1465 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1466 struct tevent_context
*ev
,
1467 struct cli_state
*cli
,
1470 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1471 struct cli_mkdir_state
*state
= NULL
;
1472 uint8_t additional_flags
= 0;
1473 uint8_t *bytes
= NULL
;
1475 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1480 bytes
= talloc_array(state
, uint8_t, 1);
1481 if (tevent_req_nomem(bytes
, req
)) {
1482 return tevent_req_post(req
, ev
);
1485 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1486 strlen(dname
)+1, NULL
);
1488 if (tevent_req_nomem(bytes
, req
)) {
1489 return tevent_req_post(req
, ev
);
1492 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1493 0, NULL
, talloc_get_size(bytes
), bytes
);
1494 if (tevent_req_nomem(subreq
, req
)) {
1495 return tevent_req_post(req
, ev
);
1497 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1501 static void cli_mkdir_done(struct tevent_req
*subreq
)
1503 struct tevent_req
*req
= tevent_req_callback_data(
1504 subreq
, struct tevent_req
);
1507 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1508 TALLOC_FREE(subreq
);
1509 if (tevent_req_nterror(req
, status
)) {
1512 tevent_req_done(req
);
1515 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1517 return tevent_req_simple_recv_ntstatus(req
);
1520 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1522 TALLOC_CTX
*frame
= talloc_stackframe();
1523 struct tevent_context
*ev
;
1524 struct tevent_req
*req
;
1525 NTSTATUS status
= NT_STATUS_OK
;
1527 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1529 * Can't use sync call while an async call is in flight
1531 status
= NT_STATUS_INVALID_PARAMETER
;
1535 ev
= samba_tevent_context_init(frame
);
1537 status
= NT_STATUS_NO_MEMORY
;
1541 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1543 status
= NT_STATUS_NO_MEMORY
;
1547 if (!tevent_req_poll(req
, ev
)) {
1548 status
= map_nt_error_from_unix(errno
);
1552 status
= cli_mkdir_recv(req
);
1559 /****************************************************************************
1561 ****************************************************************************/
1563 static void cli_rmdir_done(struct tevent_req
*subreq
);
1565 struct cli_rmdir_state
{
1569 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1570 struct tevent_context
*ev
,
1571 struct cli_state
*cli
,
1574 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1575 struct cli_rmdir_state
*state
= NULL
;
1576 uint8_t additional_flags
= 0;
1577 uint8_t *bytes
= NULL
;
1579 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1584 bytes
= talloc_array(state
, uint8_t, 1);
1585 if (tevent_req_nomem(bytes
, req
)) {
1586 return tevent_req_post(req
, ev
);
1589 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1590 strlen(dname
)+1, NULL
);
1592 if (tevent_req_nomem(bytes
, req
)) {
1593 return tevent_req_post(req
, ev
);
1596 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1597 0, NULL
, talloc_get_size(bytes
), bytes
);
1598 if (tevent_req_nomem(subreq
, req
)) {
1599 return tevent_req_post(req
, ev
);
1601 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1605 static void cli_rmdir_done(struct tevent_req
*subreq
)
1607 struct tevent_req
*req
= tevent_req_callback_data(
1608 subreq
, struct tevent_req
);
1611 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1612 TALLOC_FREE(subreq
);
1613 if (tevent_req_nterror(req
, status
)) {
1616 tevent_req_done(req
);
1619 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1621 return tevent_req_simple_recv_ntstatus(req
);
1624 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1626 TALLOC_CTX
*frame
= talloc_stackframe();
1627 struct tevent_context
*ev
;
1628 struct tevent_req
*req
;
1629 NTSTATUS status
= NT_STATUS_OK
;
1631 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1633 * Can't use sync call while an async call is in flight
1635 status
= NT_STATUS_INVALID_PARAMETER
;
1639 ev
= samba_tevent_context_init(frame
);
1641 status
= NT_STATUS_NO_MEMORY
;
1645 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1647 status
= NT_STATUS_NO_MEMORY
;
1651 if (!tevent_req_poll(req
, ev
)) {
1652 status
= map_nt_error_from_unix(errno
);
1656 status
= cli_rmdir_recv(req
);
1663 /****************************************************************************
1664 Set or clear the delete on close flag.
1665 ****************************************************************************/
1673 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1675 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1676 NULL
, 0, NULL
, NULL
, 0, NULL
);
1677 tevent_req_simple_finish_ntstatus(subreq
, status
);
1680 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1681 struct tevent_context
*ev
,
1682 struct cli_state
*cli
,
1686 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1687 struct doc_state
*state
= NULL
;
1689 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1694 /* Setup setup word. */
1695 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1697 /* Setup param array. */
1698 SSVAL(state
->param
,0,fnum
);
1699 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1701 /* Setup data array. */
1702 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1704 subreq
= cli_trans_send(state
, /* mem ctx. */
1705 ev
, /* event ctx. */
1706 cli
, /* cli_state. */
1707 SMBtrans2
, /* cmd. */
1708 NULL
, /* pipe name. */
1712 &state
->setup
, /* setup. */
1713 1, /* num setup uint16_t words. */
1714 0, /* max returned setup. */
1715 state
->param
, /* param. */
1717 2, /* max returned param. */
1718 state
->data
, /* data. */
1720 0); /* max returned data. */
1722 if (tevent_req_nomem(subreq
, req
)) {
1723 return tevent_req_post(req
, ev
);
1725 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1729 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1731 return tevent_req_simple_recv_ntstatus(req
);
1734 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1736 TALLOC_CTX
*frame
= talloc_stackframe();
1737 struct tevent_context
*ev
= NULL
;
1738 struct tevent_req
*req
= NULL
;
1739 NTSTATUS status
= NT_STATUS_OK
;
1741 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1743 * Can't use sync call while an async call is in flight
1745 status
= NT_STATUS_INVALID_PARAMETER
;
1749 ev
= samba_tevent_context_init(frame
);
1751 status
= NT_STATUS_NO_MEMORY
;
1755 req
= cli_nt_delete_on_close_send(frame
,
1761 status
= NT_STATUS_NO_MEMORY
;
1765 if (!tevent_req_poll(req
, ev
)) {
1766 status
= map_nt_error_from_unix(errno
);
1770 status
= cli_nt_delete_on_close_recv(req
);
1777 struct cli_ntcreate_state
{
1782 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1784 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1785 struct tevent_context
*ev
,
1786 struct cli_state
*cli
,
1788 uint32_t CreatFlags
,
1789 uint32_t DesiredAccess
,
1790 uint32_t FileAttributes
,
1791 uint32_t ShareAccess
,
1792 uint32_t CreateDisposition
,
1793 uint32_t CreateOptions
,
1794 uint8_t SecurityFlags
)
1796 struct tevent_req
*req
, *subreq
;
1797 struct cli_ntcreate_state
*state
;
1800 size_t converted_len
;
1802 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1809 SCVAL(vwv
+0, 0, 0xFF);
1814 if (cli
->use_oplocks
) {
1815 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1817 SIVAL(vwv
+3, 1, CreatFlags
);
1818 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1819 SIVAL(vwv
+7, 1, DesiredAccess
);
1820 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1821 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1822 SIVAL(vwv
+13, 1, FileAttributes
);
1823 SIVAL(vwv
+15, 1, ShareAccess
);
1824 SIVAL(vwv
+17, 1, CreateDisposition
);
1825 SIVAL(vwv
+19, 1, CreateOptions
|
1826 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1827 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1828 SCVAL(vwv
+23, 1, SecurityFlags
);
1830 bytes
= talloc_array(state
, uint8_t, 0);
1831 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1832 fname
, strlen(fname
)+1,
1835 /* sigh. this copes with broken netapp filer behaviour */
1836 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1838 if (tevent_req_nomem(bytes
, req
)) {
1839 return tevent_req_post(req
, ev
);
1842 SSVAL(vwv
+2, 1, converted_len
);
1844 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1845 talloc_get_size(bytes
), bytes
);
1846 if (tevent_req_nomem(subreq
, req
)) {
1847 return tevent_req_post(req
, ev
);
1849 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1853 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1855 struct tevent_req
*req
= tevent_req_callback_data(
1856 subreq
, struct tevent_req
);
1857 struct cli_ntcreate_state
*state
= tevent_req_data(
1858 req
, struct cli_ntcreate_state
);
1865 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
,
1866 &num_bytes
, &bytes
);
1867 TALLOC_FREE(subreq
);
1868 if (tevent_req_nterror(req
, status
)) {
1871 state
->fnum
= SVAL(vwv
+2, 1);
1872 tevent_req_done(req
);
1875 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1877 struct cli_ntcreate_state
*state
= tevent_req_data(
1878 req
, struct cli_ntcreate_state
);
1881 if (tevent_req_is_nterror(req
, &status
)) {
1884 *pfnum
= state
->fnum
;
1885 return NT_STATUS_OK
;
1888 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1890 uint32_t CreatFlags
,
1891 uint32_t DesiredAccess
,
1892 uint32_t FileAttributes
,
1893 uint32_t ShareAccess
,
1894 uint32_t CreateDisposition
,
1895 uint32_t CreateOptions
,
1896 uint8_t SecurityFlags
,
1899 TALLOC_CTX
*frame
= talloc_stackframe();
1900 struct tevent_context
*ev
;
1901 struct tevent_req
*req
;
1902 NTSTATUS status
= NT_STATUS_OK
;
1904 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1906 * Can't use sync call while an async call is in flight
1908 status
= NT_STATUS_INVALID_PARAMETER
;
1912 ev
= samba_tevent_context_init(frame
);
1914 status
= NT_STATUS_NO_MEMORY
;
1918 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1919 DesiredAccess
, FileAttributes
, ShareAccess
,
1920 CreateDisposition
, CreateOptions
,
1923 status
= NT_STATUS_NO_MEMORY
;
1927 if (!tevent_req_poll(req
, ev
)) {
1928 status
= map_nt_error_from_unix(errno
);
1932 status
= cli_ntcreate_recv(req
, pfid
);
1938 struct cli_nttrans_create_state
{
1942 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1944 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1945 struct tevent_context
*ev
,
1946 struct cli_state
*cli
,
1948 uint32_t CreatFlags
,
1949 uint32_t DesiredAccess
,
1950 uint32_t FileAttributes
,
1951 uint32_t ShareAccess
,
1952 uint32_t CreateDisposition
,
1953 uint32_t CreateOptions
,
1954 uint8_t SecurityFlags
,
1955 struct security_descriptor
*secdesc
,
1956 struct ea_struct
*eas
,
1959 struct tevent_req
*req
, *subreq
;
1960 struct cli_nttrans_create_state
*state
;
1962 uint8_t *secdesc_buf
;
1965 size_t converted_len
;
1967 req
= tevent_req_create(mem_ctx
,
1968 &state
, struct cli_nttrans_create_state
);
1973 if (secdesc
!= NULL
) {
1974 status
= marshall_sec_desc(talloc_tos(), secdesc
,
1975 &secdesc_buf
, &secdesc_len
);
1976 if (tevent_req_nterror(req
, status
)) {
1977 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1978 nt_errstr(status
)));
1979 return tevent_req_post(req
, ev
);
1990 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
1991 return tevent_req_post(req
, ev
);
1994 param
= talloc_array(state
, uint8_t, 53);
1995 if (tevent_req_nomem(param
, req
)) {
1996 return tevent_req_post(req
, ev
);
1999 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2000 fname
, strlen(fname
),
2002 if (tevent_req_nomem(param
, req
)) {
2003 return tevent_req_post(req
, ev
);
2006 SIVAL(param
, 0, CreatFlags
);
2007 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2008 SIVAL(param
, 8, DesiredAccess
);
2009 SIVAL(param
, 12, 0x0); /* AllocationSize */
2010 SIVAL(param
, 16, 0x0); /* AllocationSize */
2011 SIVAL(param
, 20, FileAttributes
);
2012 SIVAL(param
, 24, ShareAccess
);
2013 SIVAL(param
, 28, CreateDisposition
);
2014 SIVAL(param
, 32, CreateOptions
|
2015 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2016 SIVAL(param
, 36, secdesc_len
);
2017 SIVAL(param
, 40, 0); /* EA length*/
2018 SIVAL(param
, 44, converted_len
);
2019 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2020 SCVAL(param
, 52, SecurityFlags
);
2022 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2023 NULL
, -1, /* name, fid */
2024 NT_TRANSACT_CREATE
, 0,
2025 NULL
, 0, 0, /* setup */
2026 param
, talloc_get_size(param
), 128, /* param */
2027 secdesc_buf
, secdesc_len
, 0); /* data */
2028 if (tevent_req_nomem(subreq
, req
)) {
2029 return tevent_req_post(req
, ev
);
2031 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2035 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2037 struct tevent_req
*req
= tevent_req_callback_data(
2038 subreq
, struct tevent_req
);
2039 struct cli_nttrans_create_state
*state
= tevent_req_data(
2040 req
, struct cli_nttrans_create_state
);
2045 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2046 NULL
, 0, NULL
, /* rsetup */
2047 ¶m
, 69, &num_param
,
2049 if (tevent_req_nterror(req
, status
)) {
2052 state
->fnum
= SVAL(param
, 2);
2054 tevent_req_done(req
);
2057 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
, uint16_t *fnum
)
2059 struct cli_nttrans_create_state
*state
= tevent_req_data(
2060 req
, struct cli_nttrans_create_state
);
2063 if (tevent_req_is_nterror(req
, &status
)) {
2066 *fnum
= state
->fnum
;
2067 return NT_STATUS_OK
;
2070 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2072 uint32_t CreatFlags
,
2073 uint32_t DesiredAccess
,
2074 uint32_t FileAttributes
,
2075 uint32_t ShareAccess
,
2076 uint32_t CreateDisposition
,
2077 uint32_t CreateOptions
,
2078 uint8_t SecurityFlags
,
2079 struct security_descriptor
*secdesc
,
2080 struct ea_struct
*eas
,
2084 TALLOC_CTX
*frame
= talloc_stackframe();
2085 struct tevent_context
*ev
;
2086 struct tevent_req
*req
;
2087 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2089 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2091 * Can't use sync call while an async call is in flight
2093 status
= NT_STATUS_INVALID_PARAMETER
;
2096 ev
= samba_tevent_context_init(frame
);
2100 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2101 DesiredAccess
, FileAttributes
,
2102 ShareAccess
, CreateDisposition
,
2103 CreateOptions
, SecurityFlags
,
2104 secdesc
, eas
, num_eas
);
2108 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2111 status
= cli_nttrans_create_recv(req
, pfid
);
2117 /****************************************************************************
2119 WARNING: if you open with O_WRONLY then getattrE won't work!
2120 ****************************************************************************/
2122 struct cli_openx_state
{
2129 static void cli_openx_done(struct tevent_req
*subreq
);
2131 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2132 struct tevent_context
*ev
,
2133 struct cli_state
*cli
, const char *fname
,
2134 int flags
, int share_mode
,
2135 struct tevent_req
**psmbreq
)
2137 struct tevent_req
*req
, *subreq
;
2138 struct cli_openx_state
*state
;
2140 unsigned accessmode
;
2141 uint8_t additional_flags
;
2144 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2150 if (flags
& O_CREAT
) {
2153 if (!(flags
& O_EXCL
)) {
2154 if (flags
& O_TRUNC
)
2160 accessmode
= (share_mode
<<4);
2162 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2164 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2169 if ((flags
& O_SYNC
) == O_SYNC
) {
2170 accessmode
|= (1<<14);
2174 if (share_mode
== DENY_FCB
) {
2178 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2179 SCVAL(state
->vwv
+ 0, 1, 0);
2180 SSVAL(state
->vwv
+ 1, 0, 0);
2181 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2182 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2183 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2184 SSVAL(state
->vwv
+ 5, 0, 0);
2185 SIVAL(state
->vwv
+ 6, 0, 0);
2186 SSVAL(state
->vwv
+ 8, 0, openfn
);
2187 SIVAL(state
->vwv
+ 9, 0, 0);
2188 SIVAL(state
->vwv
+ 11, 0, 0);
2189 SIVAL(state
->vwv
+ 13, 0, 0);
2191 additional_flags
= 0;
2193 if (cli
->use_oplocks
) {
2194 /* if using oplocks then ask for a batch oplock via
2195 core and extended methods */
2197 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2198 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2201 bytes
= talloc_array(state
, uint8_t, 0);
2202 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2203 strlen(fname
)+1, NULL
);
2205 if (tevent_req_nomem(bytes
, req
)) {
2206 return tevent_req_post(req
, ev
);
2209 state
->bytes
.iov_base
= (void *)bytes
;
2210 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2212 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2213 15, state
->vwv
, 1, &state
->bytes
);
2214 if (subreq
== NULL
) {
2218 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2223 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2224 struct cli_state
*cli
, const char *fname
,
2225 int flags
, int share_mode
)
2227 struct tevent_req
*req
, *subreq
;
2230 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2236 status
= smb1cli_req_chain_submit(&subreq
, 1);
2237 if (tevent_req_nterror(req
, status
)) {
2238 return tevent_req_post(req
, ev
);
2243 static void cli_openx_done(struct tevent_req
*subreq
)
2245 struct tevent_req
*req
= tevent_req_callback_data(
2246 subreq
, struct tevent_req
);
2247 struct cli_openx_state
*state
= tevent_req_data(
2248 req
, struct cli_openx_state
);
2253 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2255 TALLOC_FREE(subreq
);
2256 if (tevent_req_nterror(req
, status
)) {
2259 state
->fnum
= SVAL(vwv
+2, 0);
2260 tevent_req_done(req
);
2263 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2265 struct cli_openx_state
*state
= tevent_req_data(
2266 req
, struct cli_openx_state
);
2269 if (tevent_req_is_nterror(req
, &status
)) {
2272 *pfnum
= state
->fnum
;
2273 return NT_STATUS_OK
;
2276 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2277 int share_mode
, uint16_t *pfnum
)
2279 TALLOC_CTX
*frame
= talloc_stackframe();
2280 struct tevent_context
*ev
;
2281 struct tevent_req
*req
;
2282 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2284 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2286 * Can't use sync call while an async call is in flight
2288 status
= NT_STATUS_INVALID_PARAMETER
;
2292 ev
= samba_tevent_context_init(frame
);
2297 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2302 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2306 status
= cli_openx_recv(req
, pfnum
);
2311 /****************************************************************************
2312 Synchronous wrapper function that does an NtCreateX open by preference
2313 and falls back to openX if this fails.
2314 ****************************************************************************/
2316 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2317 int share_mode_in
, uint16_t *pfnum
)
2320 unsigned int openfn
= 0;
2321 unsigned int dos_deny
= 0;
2322 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2324 /* Do the initial mapping into OpenX parameters. */
2325 if (flags
& O_CREAT
) {
2328 if (!(flags
& O_EXCL
)) {
2329 if (flags
& O_TRUNC
)
2335 dos_deny
= (share_mode_in
<<4);
2337 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2339 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2344 if ((flags
& O_SYNC
) == O_SYNC
) {
2345 dos_deny
|= (1<<14);
2349 if (share_mode_in
== DENY_FCB
) {
2354 /* Hmmm. This is what I think the above code
2355 should look like if it's using the constants
2358 if (flags
& O_CREAT
) {
2359 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2361 if (!(flags
& O_EXCL
)) {
2362 if (flags
& O_TRUNC
)
2363 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2365 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2368 dos_deny
= SET_DENY_MODE(share_mode_in
);
2370 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2371 dos_deny
|= DOS_OPEN_RDWR
;
2372 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2373 dos_deny
|= DOS_OPEN_WRONLY
;
2377 if ((flags
& O_SYNC
) == O_SYNC
) {
2378 dos_deny
|= FILE_SYNC_OPENMODE
;
2382 if (share_mode_in
== DENY_FCB
) {
2387 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2388 openfn
, &access_mask
,
2389 &share_mode
, &create_disposition
,
2390 &create_options
, NULL
)) {
2394 status
= cli_ntcreate(cli
,
2405 /* Try and cope will all varients of "we don't do this call"
2406 and fall back to openX. */
2408 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2409 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2410 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2411 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2412 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2413 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2414 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2415 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2416 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2424 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2427 /****************************************************************************
2429 ****************************************************************************/
2431 struct cli_close_state
{
2435 static void cli_close_done(struct tevent_req
*subreq
);
2437 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2438 struct tevent_context
*ev
,
2439 struct cli_state
*cli
,
2441 struct tevent_req
**psubreq
)
2443 struct tevent_req
*req
, *subreq
;
2444 struct cli_close_state
*state
;
2446 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2451 SSVAL(state
->vwv
+0, 0, fnum
);
2452 SIVALS(state
->vwv
+1, 0, -1);
2454 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2456 if (subreq
== NULL
) {
2460 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2465 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2466 struct tevent_context
*ev
,
2467 struct cli_state
*cli
,
2470 struct tevent_req
*req
, *subreq
;
2473 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2478 status
= smb1cli_req_chain_submit(&subreq
, 1);
2479 if (tevent_req_nterror(req
, status
)) {
2480 return tevent_req_post(req
, ev
);
2485 static void cli_close_done(struct tevent_req
*subreq
)
2487 struct tevent_req
*req
= tevent_req_callback_data(
2488 subreq
, struct tevent_req
);
2491 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2492 TALLOC_FREE(subreq
);
2493 if (tevent_req_nterror(req
, status
)) {
2496 tevent_req_done(req
);
2499 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2501 return tevent_req_simple_recv_ntstatus(req
);
2504 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2506 TALLOC_CTX
*frame
= talloc_stackframe();
2507 struct tevent_context
*ev
;
2508 struct tevent_req
*req
;
2509 NTSTATUS status
= NT_STATUS_OK
;
2511 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2513 * Can't use sync call while an async call is in flight
2515 status
= NT_STATUS_INVALID_PARAMETER
;
2519 ev
= samba_tevent_context_init(frame
);
2521 status
= NT_STATUS_NO_MEMORY
;
2525 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2527 status
= NT_STATUS_NO_MEMORY
;
2531 if (!tevent_req_poll(req
, ev
)) {
2532 status
= map_nt_error_from_unix(errno
);
2536 status
= cli_close_recv(req
);
2542 /****************************************************************************
2543 Truncate a file to a specified size
2544 ****************************************************************************/
2546 struct ftrunc_state
{
2552 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2554 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2555 NULL
, 0, NULL
, NULL
, 0, NULL
);
2556 tevent_req_simple_finish_ntstatus(subreq
, status
);
2559 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2560 struct tevent_context
*ev
,
2561 struct cli_state
*cli
,
2565 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2566 struct ftrunc_state
*state
= NULL
;
2568 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2573 /* Setup setup word. */
2574 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2576 /* Setup param array. */
2577 SSVAL(state
->param
,0,fnum
);
2578 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2579 SSVAL(state
->param
,4,0);
2581 /* Setup data array. */
2582 SBVAL(state
->data
, 0, size
);
2584 subreq
= cli_trans_send(state
, /* mem ctx. */
2585 ev
, /* event ctx. */
2586 cli
, /* cli_state. */
2587 SMBtrans2
, /* cmd. */
2588 NULL
, /* pipe name. */
2592 &state
->setup
, /* setup. */
2593 1, /* num setup uint16_t words. */
2594 0, /* max returned setup. */
2595 state
->param
, /* param. */
2597 2, /* max returned param. */
2598 state
->data
, /* data. */
2600 0); /* max returned data. */
2602 if (tevent_req_nomem(subreq
, req
)) {
2603 return tevent_req_post(req
, ev
);
2605 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2609 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2611 return tevent_req_simple_recv_ntstatus(req
);
2614 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2616 TALLOC_CTX
*frame
= talloc_stackframe();
2617 struct tevent_context
*ev
= NULL
;
2618 struct tevent_req
*req
= NULL
;
2619 NTSTATUS status
= NT_STATUS_OK
;
2621 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2623 * Can't use sync call while an async call is in flight
2625 status
= NT_STATUS_INVALID_PARAMETER
;
2629 ev
= samba_tevent_context_init(frame
);
2631 status
= NT_STATUS_NO_MEMORY
;
2635 req
= cli_ftruncate_send(frame
,
2641 status
= NT_STATUS_NO_MEMORY
;
2645 if (!tevent_req_poll(req
, ev
)) {
2646 status
= map_nt_error_from_unix(errno
);
2650 status
= cli_ftruncate_recv(req
);
2657 /****************************************************************************
2658 send a lock with a specified locktype
2659 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2660 ****************************************************************************/
2662 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2663 uint32_t offset
, uint32_t len
,
2664 int timeout
, unsigned char locktype
)
2669 unsigned int set_timeout
= 0;
2670 unsigned int saved_timeout
= 0;
2672 SCVAL(vwv
+ 0, 0, 0xff);
2673 SCVAL(vwv
+ 0, 1, 0);
2674 SSVAL(vwv
+ 1, 0, 0);
2675 SSVAL(vwv
+ 2, 0, fnum
);
2676 SCVAL(vwv
+ 3, 0, locktype
);
2677 SCVAL(vwv
+ 3, 1, 0);
2678 SIVALS(vwv
+ 4, 0, timeout
);
2679 SSVAL(vwv
+ 6, 0, 0);
2680 SSVAL(vwv
+ 7, 0, 1);
2682 SSVAL(bytes
, 0, cli_getpid(cli
));
2683 SIVAL(bytes
, 2, offset
);
2684 SIVAL(bytes
, 6, len
);
2687 if (timeout
== -1) {
2688 set_timeout
= 0x7FFFFFFF;
2690 set_timeout
= timeout
+ 2*1000;
2692 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2695 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2696 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2698 if (saved_timeout
!= 0) {
2699 cli_set_timeout(cli
, saved_timeout
);
2705 /****************************************************************************
2707 note that timeout is in units of 2 milliseconds
2708 ****************************************************************************/
2710 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2711 uint32_t offset
, uint32_t len
, int timeout
,
2712 enum brl_type lock_type
)
2716 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2717 (lock_type
== READ_LOCK
? 1 : 0));
2721 /****************************************************************************
2723 ****************************************************************************/
2725 struct cli_unlock_state
{
2730 static void cli_unlock_done(struct tevent_req
*subreq
);
2732 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2733 struct tevent_context
*ev
,
2734 struct cli_state
*cli
,
2740 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2741 struct cli_unlock_state
*state
= NULL
;
2742 uint8_t additional_flags
= 0;
2744 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2749 SCVAL(state
->vwv
+0, 0, 0xFF);
2750 SSVAL(state
->vwv
+2, 0, fnum
);
2751 SCVAL(state
->vwv
+3, 0, 0);
2752 SIVALS(state
->vwv
+4, 0, 0);
2753 SSVAL(state
->vwv
+6, 0, 1);
2754 SSVAL(state
->vwv
+7, 0, 0);
2756 SSVAL(state
->data
, 0, cli_getpid(cli
));
2757 SIVAL(state
->data
, 2, offset
);
2758 SIVAL(state
->data
, 6, len
);
2760 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2761 8, state
->vwv
, 10, state
->data
);
2762 if (tevent_req_nomem(subreq
, req
)) {
2763 return tevent_req_post(req
, ev
);
2765 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2769 static void cli_unlock_done(struct tevent_req
*subreq
)
2771 struct tevent_req
*req
= tevent_req_callback_data(
2772 subreq
, struct tevent_req
);
2775 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2776 TALLOC_FREE(subreq
);
2777 if (tevent_req_nterror(req
, status
)) {
2780 tevent_req_done(req
);
2783 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2785 return tevent_req_simple_recv_ntstatus(req
);
2788 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2793 TALLOC_CTX
*frame
= talloc_stackframe();
2794 struct tevent_context
*ev
;
2795 struct tevent_req
*req
;
2796 NTSTATUS status
= NT_STATUS_OK
;
2798 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2800 * Can't use sync call while an async call is in flight
2802 status
= NT_STATUS_INVALID_PARAMETER
;
2806 ev
= samba_tevent_context_init(frame
);
2808 status
= NT_STATUS_NO_MEMORY
;
2812 req
= cli_unlock_send(frame
, ev
, cli
,
2815 status
= NT_STATUS_NO_MEMORY
;
2819 if (!tevent_req_poll(req
, ev
)) {
2820 status
= map_nt_error_from_unix(errno
);
2824 status
= cli_unlock_recv(req
);
2831 /****************************************************************************
2832 Lock a file with 64 bit offsets.
2833 ****************************************************************************/
2835 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2836 uint64_t offset
, uint64_t len
, int timeout
,
2837 enum brl_type lock_type
)
2841 unsigned int set_timeout
= 0;
2842 unsigned int saved_timeout
= 0;
2846 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2847 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2850 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2851 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2853 SCVAL(vwv
+ 0, 0, 0xff);
2854 SCVAL(vwv
+ 0, 1, 0);
2855 SSVAL(vwv
+ 1, 0, 0);
2856 SSVAL(vwv
+ 2, 0, fnum
);
2857 SCVAL(vwv
+ 3, 0, ltype
);
2858 SCVAL(vwv
+ 3, 1, 0);
2859 SIVALS(vwv
+ 4, 0, timeout
);
2860 SSVAL(vwv
+ 6, 0, 0);
2861 SSVAL(vwv
+ 7, 0, 1);
2863 SIVAL(bytes
, 0, cli_getpid(cli
));
2864 SOFF_T_R(bytes
, 4, offset
);
2865 SOFF_T_R(bytes
, 12, len
);
2868 if (timeout
== -1) {
2869 set_timeout
= 0x7FFFFFFF;
2871 set_timeout
= timeout
+ 2*1000;
2873 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2876 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2877 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2879 if (saved_timeout
!= 0) {
2880 cli_set_timeout(cli
, saved_timeout
);
2886 /****************************************************************************
2887 Unlock a file with 64 bit offsets.
2888 ****************************************************************************/
2890 struct cli_unlock64_state
{
2895 static void cli_unlock64_done(struct tevent_req
*subreq
);
2897 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2898 struct tevent_context
*ev
,
2899 struct cli_state
*cli
,
2905 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2906 struct cli_unlock64_state
*state
= NULL
;
2907 uint8_t additional_flags
= 0;
2909 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2914 SCVAL(state
->vwv
+0, 0, 0xff);
2915 SSVAL(state
->vwv
+2, 0, fnum
);
2916 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2917 SIVALS(state
->vwv
+4, 0, 0);
2918 SSVAL(state
->vwv
+6, 0, 1);
2919 SSVAL(state
->vwv
+7, 0, 0);
2921 SIVAL(state
->data
, 0, cli_getpid(cli
));
2922 SOFF_T_R(state
->data
, 4, offset
);
2923 SOFF_T_R(state
->data
, 12, len
);
2925 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2926 8, state
->vwv
, 20, state
->data
);
2927 if (tevent_req_nomem(subreq
, req
)) {
2928 return tevent_req_post(req
, ev
);
2930 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2934 static void cli_unlock64_done(struct tevent_req
*subreq
)
2936 struct tevent_req
*req
= tevent_req_callback_data(
2937 subreq
, struct tevent_req
);
2940 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2941 TALLOC_FREE(subreq
);
2942 if (tevent_req_nterror(req
, status
)) {
2945 tevent_req_done(req
);
2948 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2950 return tevent_req_simple_recv_ntstatus(req
);
2953 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2958 TALLOC_CTX
*frame
= talloc_stackframe();
2959 struct tevent_context
*ev
;
2960 struct tevent_req
*req
;
2961 NTSTATUS status
= NT_STATUS_OK
;
2963 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2964 return cli_unlock(cli
, fnum
, offset
, len
);
2967 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2969 * Can't use sync call while an async call is in flight
2971 status
= NT_STATUS_INVALID_PARAMETER
;
2975 ev
= samba_tevent_context_init(frame
);
2977 status
= NT_STATUS_NO_MEMORY
;
2981 req
= cli_unlock64_send(frame
, ev
, cli
,
2984 status
= NT_STATUS_NO_MEMORY
;
2988 if (!tevent_req_poll(req
, ev
)) {
2989 status
= map_nt_error_from_unix(errno
);
2993 status
= cli_unlock64_recv(req
);
3000 /****************************************************************************
3001 Get/unlock a POSIX lock on a file - internal function.
3002 ****************************************************************************/
3004 struct posix_lock_state
{
3007 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3010 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3012 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3013 NULL
, 0, NULL
, NULL
, 0, NULL
);
3014 tevent_req_simple_finish_ntstatus(subreq
, status
);
3017 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3018 struct tevent_context
*ev
,
3019 struct cli_state
*cli
,
3024 enum brl_type lock_type
)
3026 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3027 struct posix_lock_state
*state
= NULL
;
3029 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3034 /* Setup setup word. */
3035 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3037 /* Setup param array. */
3038 SSVAL(&state
->param
, 0, fnum
);
3039 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3041 /* Setup data array. */
3042 switch (lock_type
) {
3044 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3045 POSIX_LOCK_TYPE_READ
);
3048 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3049 POSIX_LOCK_TYPE_WRITE
);
3052 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3053 POSIX_LOCK_TYPE_UNLOCK
);
3060 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3061 POSIX_LOCK_FLAG_WAIT
);
3063 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3064 POSIX_LOCK_FLAG_NOWAIT
);
3067 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3068 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3069 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3071 subreq
= cli_trans_send(state
, /* mem ctx. */
3072 ev
, /* event ctx. */
3073 cli
, /* cli_state. */
3074 SMBtrans2
, /* cmd. */
3075 NULL
, /* pipe name. */
3079 &state
->setup
, /* setup. */
3080 1, /* num setup uint16_t words. */
3081 0, /* max returned setup. */
3082 state
->param
, /* param. */
3084 2, /* max returned param. */
3085 state
->data
, /* data. */
3086 POSIX_LOCK_DATA_SIZE
, /* num data. */
3087 0); /* max returned data. */
3089 if (tevent_req_nomem(subreq
, req
)) {
3090 return tevent_req_post(req
, ev
);
3092 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3096 /****************************************************************************
3098 ****************************************************************************/
3100 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3101 struct tevent_context
*ev
,
3102 struct cli_state
*cli
,
3107 enum brl_type lock_type
)
3109 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3110 wait_lock
, lock_type
);
3113 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3115 return tevent_req_simple_recv_ntstatus(req
);
3118 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3119 uint64_t offset
, uint64_t len
,
3120 bool wait_lock
, enum brl_type lock_type
)
3122 TALLOC_CTX
*frame
= talloc_stackframe();
3123 struct tevent_context
*ev
= NULL
;
3124 struct tevent_req
*req
= NULL
;
3125 NTSTATUS status
= NT_STATUS_OK
;
3127 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3129 * Can't use sync call while an async call is in flight
3131 status
= NT_STATUS_INVALID_PARAMETER
;
3135 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3136 status
= NT_STATUS_INVALID_PARAMETER
;
3140 ev
= samba_tevent_context_init(frame
);
3142 status
= NT_STATUS_NO_MEMORY
;
3146 req
= cli_posix_lock_send(frame
,
3155 status
= NT_STATUS_NO_MEMORY
;
3159 if (!tevent_req_poll(req
, ev
)) {
3160 status
= map_nt_error_from_unix(errno
);
3164 status
= cli_posix_lock_recv(req
);
3171 /****************************************************************************
3172 POSIX Unlock a file.
3173 ****************************************************************************/
3175 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3176 struct tevent_context
*ev
,
3177 struct cli_state
*cli
,
3182 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3183 false, UNLOCK_LOCK
);
3186 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3188 return tevent_req_simple_recv_ntstatus(req
);
3191 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3193 TALLOC_CTX
*frame
= talloc_stackframe();
3194 struct tevent_context
*ev
= NULL
;
3195 struct tevent_req
*req
= NULL
;
3196 NTSTATUS status
= NT_STATUS_OK
;
3198 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3200 * Can't use sync call while an async call is in flight
3202 status
= NT_STATUS_INVALID_PARAMETER
;
3206 ev
= samba_tevent_context_init(frame
);
3208 status
= NT_STATUS_NO_MEMORY
;
3212 req
= cli_posix_unlock_send(frame
,
3219 status
= NT_STATUS_NO_MEMORY
;
3223 if (!tevent_req_poll(req
, ev
)) {
3224 status
= map_nt_error_from_unix(errno
);
3228 status
= cli_posix_unlock_recv(req
);
3235 /****************************************************************************
3236 Do a SMBgetattrE call.
3237 ****************************************************************************/
3239 static void cli_getattrE_done(struct tevent_req
*subreq
);
3241 struct cli_getattrE_state
{
3251 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3252 struct tevent_context
*ev
,
3253 struct cli_state
*cli
,
3256 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3257 struct cli_getattrE_state
*state
= NULL
;
3258 uint8_t additional_flags
= 0;
3260 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3265 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3266 SSVAL(state
->vwv
+0,0,fnum
);
3268 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3269 1, state
->vwv
, 0, NULL
);
3270 if (tevent_req_nomem(subreq
, req
)) {
3271 return tevent_req_post(req
, ev
);
3273 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3277 static void cli_getattrE_done(struct tevent_req
*subreq
)
3279 struct tevent_req
*req
= tevent_req_callback_data(
3280 subreq
, struct tevent_req
);
3281 struct cli_getattrE_state
*state
= tevent_req_data(
3282 req
, struct cli_getattrE_state
);
3284 uint16_t *vwv
= NULL
;
3287 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3289 TALLOC_FREE(subreq
);
3290 if (tevent_req_nterror(req
, status
)) {
3294 state
->size
= (off_t
)IVAL(vwv
+6,0);
3295 state
->attr
= SVAL(vwv
+10,0);
3296 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3297 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3298 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3300 tevent_req_done(req
);
3303 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3306 time_t *change_time
,
3307 time_t *access_time
,
3310 struct cli_getattrE_state
*state
= tevent_req_data(
3311 req
, struct cli_getattrE_state
);
3314 if (tevent_req_is_nterror(req
, &status
)) {
3318 *attr
= state
->attr
;
3321 *size
= state
->size
;
3324 *change_time
= state
->change_time
;
3327 *access_time
= state
->access_time
;
3330 *write_time
= state
->write_time
;
3332 return NT_STATUS_OK
;
3335 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3339 time_t *change_time
,
3340 time_t *access_time
,
3343 TALLOC_CTX
*frame
= talloc_stackframe();
3344 struct tevent_context
*ev
= NULL
;
3345 struct tevent_req
*req
= NULL
;
3346 NTSTATUS status
= NT_STATUS_OK
;
3348 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3350 * Can't use sync call while an async call is in flight
3352 status
= NT_STATUS_INVALID_PARAMETER
;
3356 ev
= samba_tevent_context_init(frame
);
3358 status
= NT_STATUS_NO_MEMORY
;
3362 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3364 status
= NT_STATUS_NO_MEMORY
;
3368 if (!tevent_req_poll(req
, ev
)) {
3369 status
= map_nt_error_from_unix(errno
);
3373 status
= cli_getattrE_recv(req
,
3385 /****************************************************************************
3387 ****************************************************************************/
3389 static void cli_getatr_done(struct tevent_req
*subreq
);
3391 struct cli_getatr_state
{
3398 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3399 struct tevent_context
*ev
,
3400 struct cli_state
*cli
,
3403 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3404 struct cli_getatr_state
*state
= NULL
;
3405 uint8_t additional_flags
= 0;
3406 uint8_t *bytes
= NULL
;
3408 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3413 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3415 bytes
= talloc_array(state
, uint8_t, 1);
3416 if (tevent_req_nomem(bytes
, req
)) {
3417 return tevent_req_post(req
, ev
);
3420 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3421 strlen(fname
)+1, NULL
);
3423 if (tevent_req_nomem(bytes
, req
)) {
3424 return tevent_req_post(req
, ev
);
3427 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3428 0, NULL
, talloc_get_size(bytes
), bytes
);
3429 if (tevent_req_nomem(subreq
, req
)) {
3430 return tevent_req_post(req
, ev
);
3432 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3436 static void cli_getatr_done(struct tevent_req
*subreq
)
3438 struct tevent_req
*req
= tevent_req_callback_data(
3439 subreq
, struct tevent_req
);
3440 struct cli_getatr_state
*state
= tevent_req_data(
3441 req
, struct cli_getatr_state
);
3443 uint16_t *vwv
= NULL
;
3446 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3448 TALLOC_FREE(subreq
);
3449 if (tevent_req_nterror(req
, status
)) {
3453 state
->attr
= SVAL(vwv
+0,0);
3454 state
->size
= (off_t
)IVAL(vwv
+3,0);
3455 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3457 tevent_req_done(req
);
3460 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3465 struct cli_getatr_state
*state
= tevent_req_data(
3466 req
, struct cli_getatr_state
);
3469 if (tevent_req_is_nterror(req
, &status
)) {
3473 *attr
= state
->attr
;
3476 *size
= state
->size
;
3479 *write_time
= state
->write_time
;
3481 return NT_STATUS_OK
;
3484 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3490 TALLOC_CTX
*frame
= talloc_stackframe();
3491 struct tevent_context
*ev
= NULL
;
3492 struct tevent_req
*req
= NULL
;
3493 NTSTATUS status
= NT_STATUS_OK
;
3495 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3497 * Can't use sync call while an async call is in flight
3499 status
= NT_STATUS_INVALID_PARAMETER
;
3503 ev
= samba_tevent_context_init(frame
);
3505 status
= NT_STATUS_NO_MEMORY
;
3509 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3511 status
= NT_STATUS_NO_MEMORY
;
3515 if (!tevent_req_poll(req
, ev
)) {
3516 status
= map_nt_error_from_unix(errno
);
3520 status
= cli_getatr_recv(req
,
3530 /****************************************************************************
3531 Do a SMBsetattrE call.
3532 ****************************************************************************/
3534 static void cli_setattrE_done(struct tevent_req
*subreq
);
3536 struct cli_setattrE_state
{
3540 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3541 struct tevent_context
*ev
,
3542 struct cli_state
*cli
,
3548 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3549 struct cli_setattrE_state
*state
= NULL
;
3550 uint8_t additional_flags
= 0;
3552 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3557 SSVAL(state
->vwv
+0, 0, fnum
);
3558 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3559 smb1cli_conn_server_time_zone(cli
->conn
));
3560 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3561 smb1cli_conn_server_time_zone(cli
->conn
));
3562 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3563 smb1cli_conn_server_time_zone(cli
->conn
));
3565 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3566 7, state
->vwv
, 0, NULL
);
3567 if (tevent_req_nomem(subreq
, req
)) {
3568 return tevent_req_post(req
, ev
);
3570 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3574 static void cli_setattrE_done(struct tevent_req
*subreq
)
3576 struct tevent_req
*req
= tevent_req_callback_data(
3577 subreq
, struct tevent_req
);
3580 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3581 TALLOC_FREE(subreq
);
3582 if (tevent_req_nterror(req
, status
)) {
3585 tevent_req_done(req
);
3588 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3590 return tevent_req_simple_recv_ntstatus(req
);
3593 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3599 TALLOC_CTX
*frame
= talloc_stackframe();
3600 struct tevent_context
*ev
= NULL
;
3601 struct tevent_req
*req
= NULL
;
3602 NTSTATUS status
= NT_STATUS_OK
;
3604 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3606 * Can't use sync call while an async call is in flight
3608 status
= NT_STATUS_INVALID_PARAMETER
;
3612 ev
= samba_tevent_context_init(frame
);
3614 status
= NT_STATUS_NO_MEMORY
;
3618 req
= cli_setattrE_send(frame
, ev
,
3626 status
= NT_STATUS_NO_MEMORY
;
3630 if (!tevent_req_poll(req
, ev
)) {
3631 status
= map_nt_error_from_unix(errno
);
3635 status
= cli_setattrE_recv(req
);
3642 /****************************************************************************
3643 Do a SMBsetatr call.
3644 ****************************************************************************/
3646 static void cli_setatr_done(struct tevent_req
*subreq
);
3648 struct cli_setatr_state
{
3652 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3653 struct tevent_context
*ev
,
3654 struct cli_state
*cli
,
3659 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3660 struct cli_setatr_state
*state
= NULL
;
3661 uint8_t additional_flags
= 0;
3662 uint8_t *bytes
= NULL
;
3664 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3669 SSVAL(state
->vwv
+0, 0, attr
);
3670 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3672 bytes
= talloc_array(state
, uint8_t, 1);
3673 if (tevent_req_nomem(bytes
, req
)) {
3674 return tevent_req_post(req
, ev
);
3677 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3678 strlen(fname
)+1, NULL
);
3679 if (tevent_req_nomem(bytes
, req
)) {
3680 return tevent_req_post(req
, ev
);
3682 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3683 talloc_get_size(bytes
)+1);
3684 if (tevent_req_nomem(bytes
, req
)) {
3685 return tevent_req_post(req
, ev
);
3688 bytes
[talloc_get_size(bytes
)-1] = 4;
3689 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3691 if (tevent_req_nomem(bytes
, req
)) {
3692 return tevent_req_post(req
, ev
);
3695 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3696 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3697 if (tevent_req_nomem(subreq
, req
)) {
3698 return tevent_req_post(req
, ev
);
3700 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3704 static void cli_setatr_done(struct tevent_req
*subreq
)
3706 struct tevent_req
*req
= tevent_req_callback_data(
3707 subreq
, struct tevent_req
);
3710 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3711 TALLOC_FREE(subreq
);
3712 if (tevent_req_nterror(req
, status
)) {
3715 tevent_req_done(req
);
3718 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3720 return tevent_req_simple_recv_ntstatus(req
);
3723 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3728 TALLOC_CTX
*frame
= talloc_stackframe();
3729 struct tevent_context
*ev
= NULL
;
3730 struct tevent_req
*req
= NULL
;
3731 NTSTATUS status
= NT_STATUS_OK
;
3733 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3735 * Can't use sync call while an async call is in flight
3737 status
= NT_STATUS_INVALID_PARAMETER
;
3741 ev
= samba_tevent_context_init(frame
);
3743 status
= NT_STATUS_NO_MEMORY
;
3747 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3749 status
= NT_STATUS_NO_MEMORY
;
3753 if (!tevent_req_poll(req
, ev
)) {
3754 status
= map_nt_error_from_unix(errno
);
3758 status
= cli_setatr_recv(req
);
3765 /****************************************************************************
3766 Check for existance of a dir.
3767 ****************************************************************************/
3769 static void cli_chkpath_done(struct tevent_req
*subreq
);
3771 struct cli_chkpath_state
{
3775 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3776 struct tevent_context
*ev
,
3777 struct cli_state
*cli
,
3780 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3781 struct cli_chkpath_state
*state
= NULL
;
3782 uint8_t additional_flags
= 0;
3783 uint8_t *bytes
= NULL
;
3785 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3790 bytes
= talloc_array(state
, uint8_t, 1);
3791 if (tevent_req_nomem(bytes
, req
)) {
3792 return tevent_req_post(req
, ev
);
3795 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3796 strlen(fname
)+1, NULL
);
3798 if (tevent_req_nomem(bytes
, req
)) {
3799 return tevent_req_post(req
, ev
);
3802 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3803 0, NULL
, talloc_get_size(bytes
), bytes
);
3804 if (tevent_req_nomem(subreq
, req
)) {
3805 return tevent_req_post(req
, ev
);
3807 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3811 static void cli_chkpath_done(struct tevent_req
*subreq
)
3813 struct tevent_req
*req
= tevent_req_callback_data(
3814 subreq
, struct tevent_req
);
3817 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3818 TALLOC_FREE(subreq
);
3819 if (tevent_req_nterror(req
, status
)) {
3822 tevent_req_done(req
);
3825 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3827 return tevent_req_simple_recv_ntstatus(req
);
3830 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3832 TALLOC_CTX
*frame
= talloc_stackframe();
3833 struct tevent_context
*ev
= NULL
;
3834 struct tevent_req
*req
= NULL
;
3836 NTSTATUS status
= NT_STATUS_OK
;
3838 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3840 * Can't use sync call while an async call is in flight
3842 status
= NT_STATUS_INVALID_PARAMETER
;
3846 path2
= talloc_strdup(frame
, path
);
3848 status
= NT_STATUS_NO_MEMORY
;
3851 trim_char(path2
,'\0','\\');
3853 path2
= talloc_strdup(frame
, "\\");
3855 status
= NT_STATUS_NO_MEMORY
;
3860 ev
= samba_tevent_context_init(frame
);
3862 status
= NT_STATUS_NO_MEMORY
;
3866 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3868 status
= NT_STATUS_NO_MEMORY
;
3872 if (!tevent_req_poll(req
, ev
)) {
3873 status
= map_nt_error_from_unix(errno
);
3877 status
= cli_chkpath_recv(req
);
3884 /****************************************************************************
3886 ****************************************************************************/
3888 static void cli_dskattr_done(struct tevent_req
*subreq
);
3890 struct cli_dskattr_state
{
3896 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3897 struct tevent_context
*ev
,
3898 struct cli_state
*cli
)
3900 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3901 struct cli_dskattr_state
*state
= NULL
;
3902 uint8_t additional_flags
= 0;
3904 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3909 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3911 if (tevent_req_nomem(subreq
, req
)) {
3912 return tevent_req_post(req
, ev
);
3914 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3918 static void cli_dskattr_done(struct tevent_req
*subreq
)
3920 struct tevent_req
*req
= tevent_req_callback_data(
3921 subreq
, struct tevent_req
);
3922 struct cli_dskattr_state
*state
= tevent_req_data(
3923 req
, struct cli_dskattr_state
);
3925 uint16_t *vwv
= NULL
;
3928 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3930 TALLOC_FREE(subreq
);
3931 if (tevent_req_nterror(req
, status
)) {
3934 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3935 state
->total
= SVAL(vwv
+0, 0);
3936 state
->avail
= SVAL(vwv
+3, 0);
3937 tevent_req_done(req
);
3940 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3942 struct cli_dskattr_state
*state
= tevent_req_data(
3943 req
, struct cli_dskattr_state
);
3946 if (tevent_req_is_nterror(req
, &status
)) {
3949 *bsize
= state
->bsize
;
3950 *total
= state
->total
;
3951 *avail
= state
->avail
;
3952 return NT_STATUS_OK
;
3955 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3957 TALLOC_CTX
*frame
= talloc_stackframe();
3958 struct tevent_context
*ev
= NULL
;
3959 struct tevent_req
*req
= NULL
;
3960 NTSTATUS status
= NT_STATUS_OK
;
3962 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3964 * Can't use sync call while an async call is in flight
3966 status
= NT_STATUS_INVALID_PARAMETER
;
3970 ev
= samba_tevent_context_init(frame
);
3972 status
= NT_STATUS_NO_MEMORY
;
3976 req
= cli_dskattr_send(frame
, ev
, cli
);
3978 status
= NT_STATUS_NO_MEMORY
;
3982 if (!tevent_req_poll(req
, ev
)) {
3983 status
= map_nt_error_from_unix(errno
);
3987 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
3994 /****************************************************************************
3995 Create and open a temporary file.
3996 ****************************************************************************/
3998 static void cli_ctemp_done(struct tevent_req
*subreq
);
4000 struct ctemp_state
{
4006 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4007 struct tevent_context
*ev
,
4008 struct cli_state
*cli
,
4011 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4012 struct ctemp_state
*state
= NULL
;
4013 uint8_t additional_flags
= 0;
4014 uint8_t *bytes
= NULL
;
4016 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4021 SSVAL(state
->vwv
,0,0);
4022 SIVALS(state
->vwv
+1,0,-1);
4024 bytes
= talloc_array(state
, uint8_t, 1);
4025 if (tevent_req_nomem(bytes
, req
)) {
4026 return tevent_req_post(req
, ev
);
4029 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4030 strlen(path
)+1, NULL
);
4031 if (tevent_req_nomem(bytes
, req
)) {
4032 return tevent_req_post(req
, ev
);
4035 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4036 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4037 if (tevent_req_nomem(subreq
, req
)) {
4038 return tevent_req_post(req
, ev
);
4040 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4044 static void cli_ctemp_done(struct tevent_req
*subreq
)
4046 struct tevent_req
*req
= tevent_req_callback_data(
4047 subreq
, struct tevent_req
);
4048 struct ctemp_state
*state
= tevent_req_data(
4049 req
, struct ctemp_state
);
4053 uint32_t num_bytes
= 0;
4054 uint8_t *bytes
= NULL
;
4056 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4057 &num_bytes
, &bytes
);
4058 TALLOC_FREE(subreq
);
4059 if (tevent_req_nterror(req
, status
)) {
4063 state
->fnum
= SVAL(vwv
+0, 0);
4065 /* From W2K3, the result is just the ASCII name */
4066 if (num_bytes
< 2) {
4067 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4071 if (pull_string_talloc(state
,
4078 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4081 tevent_req_done(req
);
4084 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4089 struct ctemp_state
*state
= tevent_req_data(req
,
4090 struct ctemp_state
);
4093 if (tevent_req_is_nterror(req
, &status
)) {
4096 *pfnum
= state
->fnum
;
4097 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4099 return NT_STATUS_NO_MEMORY
;
4101 return NT_STATUS_OK
;
4104 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4110 TALLOC_CTX
*frame
= talloc_stackframe();
4111 struct tevent_context
*ev
;
4112 struct tevent_req
*req
;
4113 NTSTATUS status
= NT_STATUS_OK
;
4115 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4117 * Can't use sync call while an async call is in flight
4119 status
= NT_STATUS_INVALID_PARAMETER
;
4123 ev
= samba_tevent_context_init(frame
);
4125 status
= NT_STATUS_NO_MEMORY
;
4129 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4131 status
= NT_STATUS_NO_MEMORY
;
4135 if (!tevent_req_poll(req
, ev
)) {
4136 status
= map_nt_error_from_unix(errno
);
4140 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4148 send a raw ioctl - used by the torture code
4150 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4155 SSVAL(vwv
+0, 0, fnum
);
4156 SSVAL(vwv
+1, 0, code
>>16);
4157 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4159 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4160 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4161 if (!NT_STATUS_IS_OK(status
)) {
4164 *blob
= data_blob_null
;
4165 return NT_STATUS_OK
;
4168 /*********************************************************
4169 Set an extended attribute utility fn.
4170 *********************************************************/
4172 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4173 uint8_t *param
, unsigned int param_len
,
4174 const char *ea_name
,
4175 const char *ea_val
, size_t ea_len
)
4178 unsigned int data_len
= 0;
4179 uint8_t *data
= NULL
;
4181 size_t ea_namelen
= strlen(ea_name
);
4184 SSVAL(setup
, 0, setup_val
);
4186 if (ea_namelen
== 0 && ea_len
== 0) {
4188 data
= talloc_array(talloc_tos(),
4192 return NT_STATUS_NO_MEMORY
;
4195 SIVAL(p
,0,data_len
);
4197 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4198 data
= talloc_array(talloc_tos(),
4202 return NT_STATUS_NO_MEMORY
;
4205 SIVAL(p
,0,data_len
);
4207 SCVAL(p
, 0, 0); /* EA flags. */
4208 SCVAL(p
, 1, ea_namelen
);
4209 SSVAL(p
, 2, ea_len
);
4210 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4211 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4214 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4216 param
, param_len
, 2,
4217 data
, data_len
, CLI_BUFFER_SIZE
,
4219 NULL
, 0, NULL
, /* rsetup */
4220 NULL
, 0, NULL
, /* rparam */
4221 NULL
, 0, NULL
); /* rdata */
4226 /*********************************************************
4227 Set an extended attribute on a pathname.
4228 *********************************************************/
4230 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4231 const char *ea_name
, const char *ea_val
,
4234 unsigned int param_len
= 0;
4237 TALLOC_CTX
*frame
= talloc_stackframe();
4239 param
= talloc_array(talloc_tos(), uint8_t, 6);
4241 return NT_STATUS_NO_MEMORY
;
4243 SSVAL(param
,0,SMB_INFO_SET_EA
);
4247 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4248 path
, strlen(path
)+1,
4250 param_len
= talloc_get_size(param
);
4252 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4253 ea_name
, ea_val
, ea_len
);
4258 /*********************************************************
4259 Set an extended attribute on an fnum.
4260 *********************************************************/
4262 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4263 const char *ea_name
, const char *ea_val
,
4268 memset(param
, 0, 6);
4269 SSVAL(param
,0,fnum
);
4270 SSVAL(param
,2,SMB_INFO_SET_EA
);
4272 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4273 ea_name
, ea_val
, ea_len
);
4276 /*********************************************************
4277 Get an extended attribute list utility fn.
4278 *********************************************************/
4280 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4282 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4284 struct ea_struct
*ea_list
= NULL
;
4289 if (rdata_len
< 4) {
4293 ea_size
= (size_t)IVAL(rdata
,0);
4294 if (ea_size
> rdata_len
) {
4299 /* No EA's present. */
4308 /* Validate the EA list and count it. */
4309 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4310 unsigned int ea_namelen
= CVAL(p
,1);
4311 unsigned int ea_valuelen
= SVAL(p
,2);
4312 if (ea_namelen
== 0) {
4315 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4318 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4319 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4328 *pnum_eas
= num_eas
;
4330 /* Caller only wants number of EA's. */
4334 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4339 ea_size
= (size_t)IVAL(rdata
,0);
4342 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4343 struct ea_struct
*ea
= &ea_list
[num_eas
];
4344 fstring unix_ea_name
;
4345 unsigned int ea_namelen
= CVAL(p
,1);
4346 unsigned int ea_valuelen
= SVAL(p
,2);
4348 ea
->flags
= CVAL(p
,0);
4349 unix_ea_name
[0] = '\0';
4350 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4351 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4355 /* Ensure the value is null terminated (in case it's a string). */
4356 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4357 if (!ea
->value
.data
) {
4361 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4363 ea
->value
.data
[ea_valuelen
] = 0;
4365 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4368 *pea_list
= ea_list
;
4372 TALLOC_FREE(ea_list
);
4376 /*********************************************************
4377 Get an extended attribute list from a pathname.
4378 *********************************************************/
4380 struct cli_get_ea_list_path_state
{
4385 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4387 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4388 struct tevent_context
*ev
,
4389 struct cli_state
*cli
,
4392 struct tevent_req
*req
, *subreq
;
4393 struct cli_get_ea_list_path_state
*state
;
4395 req
= tevent_req_create(mem_ctx
, &state
,
4396 struct cli_get_ea_list_path_state
);
4400 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4401 SMB_INFO_QUERY_ALL_EAS
, 4,
4403 if (tevent_req_nomem(subreq
, req
)) {
4404 return tevent_req_post(req
, ev
);
4406 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4410 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4412 struct tevent_req
*req
= tevent_req_callback_data(
4413 subreq
, struct tevent_req
);
4414 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4415 req
, struct cli_get_ea_list_path_state
);
4418 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4420 TALLOC_FREE(subreq
);
4421 if (tevent_req_nterror(req
, status
)) {
4424 tevent_req_done(req
);
4427 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4428 size_t *pnum_eas
, struct ea_struct
**peas
)
4430 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4431 req
, struct cli_get_ea_list_path_state
);
4434 if (tevent_req_is_nterror(req
, &status
)) {
4437 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4439 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4441 return NT_STATUS_OK
;
4444 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4447 struct ea_struct
**pea_list
)
4449 TALLOC_CTX
*frame
= talloc_stackframe();
4450 struct tevent_context
*ev
= NULL
;
4451 struct tevent_req
*req
= NULL
;
4452 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4454 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4456 * Can't use sync call while an async call is in flight
4458 status
= NT_STATUS_INVALID_PARAMETER
;
4461 ev
= samba_tevent_context_init(frame
);
4465 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4469 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4472 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4478 /****************************************************************************
4479 Convert open "flags" arg to uint32_t on wire.
4480 ****************************************************************************/
4482 static uint32_t open_flags_to_wire(int flags
)
4484 int open_mode
= flags
& O_ACCMODE
;
4487 switch (open_mode
) {
4489 ret
|= SMB_O_WRONLY
;
4496 ret
|= SMB_O_RDONLY
;
4500 if (flags
& O_CREAT
) {
4503 if (flags
& O_EXCL
) {
4506 if (flags
& O_TRUNC
) {
4510 if (flags
& O_SYNC
) {
4514 if (flags
& O_APPEND
) {
4515 ret
|= SMB_O_APPEND
;
4517 #if defined(O_DIRECT)
4518 if (flags
& O_DIRECT
) {
4519 ret
|= SMB_O_DIRECT
;
4522 #if defined(O_DIRECTORY)
4523 if (flags
& O_DIRECTORY
) {
4524 ret
|= SMB_O_DIRECTORY
;
4530 /****************************************************************************
4531 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4532 ****************************************************************************/
4534 struct posix_open_state
{
4538 uint16_t fnum
; /* Out */
4541 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4543 struct tevent_req
*req
= tevent_req_callback_data(
4544 subreq
, struct tevent_req
);
4545 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4550 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4551 NULL
, 0, NULL
, &data
, 12, &num_data
);
4552 TALLOC_FREE(subreq
);
4553 if (tevent_req_nterror(req
, status
)) {
4556 state
->fnum
= SVAL(data
,2);
4557 tevent_req_done(req
);
4560 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4561 struct tevent_context
*ev
,
4562 struct cli_state
*cli
,
4568 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4569 struct posix_open_state
*state
= NULL
;
4570 uint32_t wire_flags
= open_flags_to_wire(flags
);
4572 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4577 /* Setup setup word. */
4578 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4580 /* Setup param array. */
4581 state
->param
= talloc_array(state
, uint8_t, 6);
4582 if (tevent_req_nomem(state
->param
, req
)) {
4583 return tevent_req_post(req
, ev
);
4585 memset(state
->param
, '\0', 6);
4586 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4588 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4589 strlen(fname
)+1, NULL
);
4591 if (tevent_req_nomem(state
->param
, req
)) {
4592 return tevent_req_post(req
, ev
);
4595 /* Setup data words. */
4597 wire_flags
|= SMB_O_DIRECTORY
;
4600 SIVAL(state
->data
,0,0); /* No oplock. */
4601 SIVAL(state
->data
,4,wire_flags
);
4602 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4603 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4604 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4606 subreq
= cli_trans_send(state
, /* mem ctx. */
4607 ev
, /* event ctx. */
4608 cli
, /* cli_state. */
4609 SMBtrans2
, /* cmd. */
4610 NULL
, /* pipe name. */
4614 &state
->setup
, /* setup. */
4615 1, /* num setup uint16_t words. */
4616 0, /* max returned setup. */
4617 state
->param
, /* param. */
4618 talloc_get_size(state
->param
),/* num param. */
4619 2, /* max returned param. */
4620 state
->data
, /* data. */
4622 12); /* max returned data. */
4624 if (tevent_req_nomem(subreq
, req
)) {
4625 return tevent_req_post(req
, ev
);
4627 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4631 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4632 struct tevent_context
*ev
,
4633 struct cli_state
*cli
,
4638 return cli_posix_open_internal_send(mem_ctx
, ev
,
4639 cli
, fname
, flags
, mode
, false);
4642 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4644 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4647 if (tevent_req_is_nterror(req
, &status
)) {
4650 *pfnum
= state
->fnum
;
4651 return NT_STATUS_OK
;
4654 /****************************************************************************
4655 Open - POSIX semantics. Doesn't request oplock.
4656 ****************************************************************************/
4658 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4659 int flags
, mode_t mode
, uint16_t *pfnum
)
4662 TALLOC_CTX
*frame
= talloc_stackframe();
4663 struct tevent_context
*ev
= NULL
;
4664 struct tevent_req
*req
= NULL
;
4665 NTSTATUS status
= NT_STATUS_OK
;
4667 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4669 * Can't use sync call while an async call is in flight
4671 status
= NT_STATUS_INVALID_PARAMETER
;
4675 ev
= samba_tevent_context_init(frame
);
4677 status
= NT_STATUS_NO_MEMORY
;
4681 req
= cli_posix_open_send(frame
,
4688 status
= NT_STATUS_NO_MEMORY
;
4692 if (!tevent_req_poll(req
, ev
)) {
4693 status
= map_nt_error_from_unix(errno
);
4697 status
= cli_posix_open_recv(req
, pfnum
);
4704 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4705 struct tevent_context
*ev
,
4706 struct cli_state
*cli
,
4710 return cli_posix_open_internal_send(mem_ctx
, ev
,
4711 cli
, fname
, O_CREAT
, mode
, true);
4714 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4716 return tevent_req_simple_recv_ntstatus(req
);
4719 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4721 TALLOC_CTX
*frame
= talloc_stackframe();
4722 struct tevent_context
*ev
= NULL
;
4723 struct tevent_req
*req
= NULL
;
4724 NTSTATUS status
= NT_STATUS_OK
;
4726 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4728 * Can't use sync call while an async call is in flight
4730 status
= NT_STATUS_INVALID_PARAMETER
;
4734 ev
= samba_tevent_context_init(frame
);
4736 status
= NT_STATUS_NO_MEMORY
;
4740 req
= cli_posix_mkdir_send(frame
,
4746 status
= NT_STATUS_NO_MEMORY
;
4750 if (!tevent_req_poll(req
, ev
)) {
4751 status
= map_nt_error_from_unix(errno
);
4755 status
= cli_posix_mkdir_recv(req
);
4762 /****************************************************************************
4763 unlink or rmdir - POSIX semantics.
4764 ****************************************************************************/
4766 struct cli_posix_unlink_internal_state
{
4770 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4772 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4773 struct tevent_context
*ev
,
4774 struct cli_state
*cli
,
4778 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4779 struct cli_posix_unlink_internal_state
*state
= NULL
;
4781 req
= tevent_req_create(mem_ctx
, &state
,
4782 struct cli_posix_unlink_internal_state
);
4787 /* Setup data word. */
4788 SSVAL(state
->data
, 0, level
);
4790 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4791 SMB_POSIX_PATH_UNLINK
,
4793 state
->data
, sizeof(state
->data
));
4794 if (tevent_req_nomem(subreq
, req
)) {
4795 return tevent_req_post(req
, ev
);
4797 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4801 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4803 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4804 tevent_req_simple_finish_ntstatus(subreq
, status
);
4807 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4808 struct tevent_context
*ev
,
4809 struct cli_state
*cli
,
4812 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4813 SMB_POSIX_UNLINK_FILE_TARGET
);
4816 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4818 return tevent_req_simple_recv_ntstatus(req
);
4821 /****************************************************************************
4822 unlink - POSIX semantics.
4823 ****************************************************************************/
4825 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4827 TALLOC_CTX
*frame
= talloc_stackframe();
4828 struct tevent_context
*ev
= NULL
;
4829 struct tevent_req
*req
= NULL
;
4830 NTSTATUS status
= NT_STATUS_OK
;
4832 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4834 * Can't use sync call while an async call is in flight
4836 status
= NT_STATUS_INVALID_PARAMETER
;
4840 ev
= samba_tevent_context_init(frame
);
4842 status
= NT_STATUS_NO_MEMORY
;
4846 req
= cli_posix_unlink_send(frame
,
4851 status
= NT_STATUS_NO_MEMORY
;
4855 if (!tevent_req_poll(req
, ev
)) {
4856 status
= map_nt_error_from_unix(errno
);
4860 status
= cli_posix_unlink_recv(req
);
4867 /****************************************************************************
4868 rmdir - POSIX semantics.
4869 ****************************************************************************/
4871 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4872 struct tevent_context
*ev
,
4873 struct cli_state
*cli
,
4876 return cli_posix_unlink_internal_send(
4877 mem_ctx
, ev
, cli
, fname
,
4878 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4881 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4883 return tevent_req_simple_recv_ntstatus(req
);
4886 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4888 TALLOC_CTX
*frame
= talloc_stackframe();
4889 struct tevent_context
*ev
= NULL
;
4890 struct tevent_req
*req
= NULL
;
4891 NTSTATUS status
= NT_STATUS_OK
;
4893 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4895 * Can't use sync call while an async call is in flight
4897 status
= NT_STATUS_INVALID_PARAMETER
;
4901 ev
= samba_tevent_context_init(frame
);
4903 status
= NT_STATUS_NO_MEMORY
;
4907 req
= cli_posix_rmdir_send(frame
,
4912 status
= NT_STATUS_NO_MEMORY
;
4916 if (!tevent_req_poll(req
, ev
)) {
4917 status
= map_nt_error_from_unix(errno
);
4921 status
= cli_posix_rmdir_recv(req
, frame
);
4928 /****************************************************************************
4930 ****************************************************************************/
4932 struct cli_notify_state
{
4934 uint32_t num_changes
;
4935 struct notify_change
*changes
;
4938 static void cli_notify_done(struct tevent_req
*subreq
);
4940 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4941 struct tevent_context
*ev
,
4942 struct cli_state
*cli
, uint16_t fnum
,
4943 uint32_t buffer_size
,
4944 uint32_t completion_filter
, bool recursive
)
4946 struct tevent_req
*req
, *subreq
;
4947 struct cli_notify_state
*state
;
4948 unsigned old_timeout
;
4950 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
4955 SIVAL(state
->setup
, 0, completion_filter
);
4956 SSVAL(state
->setup
, 4, fnum
);
4957 SSVAL(state
->setup
, 6, recursive
);
4960 * Notifies should not time out
4962 old_timeout
= cli_set_timeout(cli
, 0);
4964 subreq
= cli_trans_send(
4965 state
, /* mem ctx. */
4966 ev
, /* event ctx. */
4967 cli
, /* cli_state. */
4968 SMBnttrans
, /* cmd. */
4969 NULL
, /* pipe name. */
4971 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
4973 (uint16_t *)state
->setup
, /* setup. */
4974 4, /* num setup uint16_t words. */
4975 0, /* max returned setup. */
4978 buffer_size
, /* max returned param. */
4981 0); /* max returned data. */
4983 cli_set_timeout(cli
, old_timeout
);
4985 if (tevent_req_nomem(subreq
, req
)) {
4986 return tevent_req_post(req
, ev
);
4988 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
4992 static void cli_notify_done(struct tevent_req
*subreq
)
4994 struct tevent_req
*req
= tevent_req_callback_data(
4995 subreq
, struct tevent_req
);
4996 struct cli_notify_state
*state
= tevent_req_data(
4997 req
, struct cli_notify_state
);
5000 uint32_t i
, ofs
, num_params
;
5003 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5004 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5005 TALLOC_FREE(subreq
);
5006 if (tevent_req_nterror(req
, status
)) {
5007 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5011 state
->num_changes
= 0;
5014 while (num_params
- ofs
> 12) {
5015 uint32_t next
= IVAL(params
, ofs
);
5016 state
->num_changes
+= 1;
5018 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5024 state
->changes
= talloc_array(state
, struct notify_change
,
5025 state
->num_changes
);
5026 if (tevent_req_nomem(state
->changes
, req
)) {
5027 TALLOC_FREE(params
);
5033 for (i
=0; i
<state
->num_changes
; i
++) {
5034 uint32_t next
= IVAL(params
, ofs
);
5035 uint32_t len
= IVAL(params
, ofs
+8);
5039 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5040 TALLOC_FREE(params
);
5042 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5046 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5047 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5048 &name
, params
+ofs
+12, len
,
5049 STR_TERMINATE
|STR_UNICODE
);
5051 TALLOC_FREE(params
);
5052 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5055 state
->changes
[i
].name
= name
;
5059 TALLOC_FREE(params
);
5060 tevent_req_done(req
);
5063 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5064 uint32_t *pnum_changes
,
5065 struct notify_change
**pchanges
)
5067 struct cli_notify_state
*state
= tevent_req_data(
5068 req
, struct cli_notify_state
);
5071 if (tevent_req_is_nterror(req
, &status
)) {
5075 *pnum_changes
= state
->num_changes
;
5076 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5077 return NT_STATUS_OK
;
5080 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5081 uint32_t completion_filter
, bool recursive
,
5082 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5083 struct notify_change
**pchanges
)
5085 TALLOC_CTX
*frame
= talloc_stackframe();
5086 struct tevent_context
*ev
;
5087 struct tevent_req
*req
;
5088 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5090 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5092 * Can't use sync call while an async call is in flight
5094 status
= NT_STATUS_INVALID_PARAMETER
;
5097 ev
= samba_tevent_context_init(frame
);
5101 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5102 completion_filter
, recursive
);
5106 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5109 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5115 struct cli_qpathinfo_state
{
5124 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5126 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5127 struct tevent_context
*ev
,
5128 struct cli_state
*cli
, const char *fname
,
5129 uint16_t level
, uint32_t min_rdata
,
5132 struct tevent_req
*req
, *subreq
;
5133 struct cli_qpathinfo_state
*state
;
5135 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5139 state
->min_rdata
= min_rdata
;
5140 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5142 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5143 if (tevent_req_nomem(state
->param
, req
)) {
5144 return tevent_req_post(req
, ev
);
5146 SSVAL(state
->param
, 0, level
);
5147 state
->param
= trans2_bytes_push_str(
5148 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5149 if (tevent_req_nomem(state
->param
, req
)) {
5150 return tevent_req_post(req
, ev
);
5153 subreq
= cli_trans_send(
5154 state
, /* mem ctx. */
5155 ev
, /* event ctx. */
5156 cli
, /* cli_state. */
5157 SMBtrans2
, /* cmd. */
5158 NULL
, /* pipe name. */
5162 state
->setup
, /* setup. */
5163 1, /* num setup uint16_t words. */
5164 0, /* max returned setup. */
5165 state
->param
, /* param. */
5166 talloc_get_size(state
->param
), /* num param. */
5167 2, /* max returned param. */
5170 max_rdata
); /* max returned data. */
5172 if (tevent_req_nomem(subreq
, req
)) {
5173 return tevent_req_post(req
, ev
);
5175 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5179 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5181 struct tevent_req
*req
= tevent_req_callback_data(
5182 subreq
, struct tevent_req
);
5183 struct cli_qpathinfo_state
*state
= tevent_req_data(
5184 req
, struct cli_qpathinfo_state
);
5187 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5189 &state
->rdata
, state
->min_rdata
,
5191 if (tevent_req_nterror(req
, status
)) {
5194 tevent_req_done(req
);
5197 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5198 uint8_t **rdata
, uint32_t *num_rdata
)
5200 struct cli_qpathinfo_state
*state
= tevent_req_data(
5201 req
, struct cli_qpathinfo_state
);
5204 if (tevent_req_is_nterror(req
, &status
)) {
5207 if (rdata
!= NULL
) {
5208 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5210 TALLOC_FREE(state
->rdata
);
5212 if (num_rdata
!= NULL
) {
5213 *num_rdata
= state
->num_rdata
;
5215 return NT_STATUS_OK
;
5218 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5219 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5221 uint8_t **rdata
, uint32_t *num_rdata
)
5223 TALLOC_CTX
*frame
= talloc_stackframe();
5224 struct tevent_context
*ev
;
5225 struct tevent_req
*req
;
5226 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5228 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5230 * Can't use sync call while an async call is in flight
5232 status
= NT_STATUS_INVALID_PARAMETER
;
5235 ev
= samba_tevent_context_init(frame
);
5239 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5244 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5247 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5253 struct cli_qfileinfo_state
{
5257 uint16_t recv_flags2
;
5263 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5265 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5266 struct tevent_context
*ev
,
5267 struct cli_state
*cli
, uint16_t fnum
,
5268 uint16_t level
, uint32_t min_rdata
,
5271 struct tevent_req
*req
, *subreq
;
5272 struct cli_qfileinfo_state
*state
;
5274 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5278 state
->min_rdata
= min_rdata
;
5279 SSVAL(state
->param
, 0, fnum
);
5280 SSVAL(state
->param
, 2, level
);
5281 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5283 subreq
= cli_trans_send(
5284 state
, /* mem ctx. */
5285 ev
, /* event ctx. */
5286 cli
, /* cli_state. */
5287 SMBtrans2
, /* cmd. */
5288 NULL
, /* pipe name. */
5292 state
->setup
, /* setup. */
5293 1, /* num setup uint16_t words. */
5294 0, /* max returned setup. */
5295 state
->param
, /* param. */
5296 sizeof(state
->param
), /* num param. */
5297 2, /* max returned param. */
5300 max_rdata
); /* max returned data. */
5302 if (tevent_req_nomem(subreq
, req
)) {
5303 return tevent_req_post(req
, ev
);
5305 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5309 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5311 struct tevent_req
*req
= tevent_req_callback_data(
5312 subreq
, struct tevent_req
);
5313 struct cli_qfileinfo_state
*state
= tevent_req_data(
5314 req
, struct cli_qfileinfo_state
);
5317 status
= cli_trans_recv(subreq
, state
,
5318 &state
->recv_flags2
,
5321 &state
->rdata
, state
->min_rdata
,
5323 if (tevent_req_nterror(req
, status
)) {
5326 tevent_req_done(req
);
5329 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5330 uint16_t *recv_flags2
,
5331 uint8_t **rdata
, uint32_t *num_rdata
)
5333 struct cli_qfileinfo_state
*state
= tevent_req_data(
5334 req
, struct cli_qfileinfo_state
);
5337 if (tevent_req_is_nterror(req
, &status
)) {
5341 if (recv_flags2
!= NULL
) {
5342 *recv_flags2
= state
->recv_flags2
;
5344 if (rdata
!= NULL
) {
5345 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5347 TALLOC_FREE(state
->rdata
);
5349 if (num_rdata
!= NULL
) {
5350 *num_rdata
= state
->num_rdata
;
5352 return NT_STATUS_OK
;
5355 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5356 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5357 uint32_t max_rdata
, uint16_t *recv_flags2
,
5358 uint8_t **rdata
, uint32_t *num_rdata
)
5360 TALLOC_CTX
*frame
= talloc_stackframe();
5361 struct tevent_context
*ev
;
5362 struct tevent_req
*req
;
5363 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5365 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5367 * Can't use sync call while an async call is in flight
5369 status
= NT_STATUS_INVALID_PARAMETER
;
5372 ev
= samba_tevent_context_init(frame
);
5376 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5381 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5384 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5390 struct cli_flush_state
{
5394 static void cli_flush_done(struct tevent_req
*subreq
);
5396 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5397 struct tevent_context
*ev
,
5398 struct cli_state
*cli
,
5401 struct tevent_req
*req
, *subreq
;
5402 struct cli_flush_state
*state
;
5404 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5408 SSVAL(state
->vwv
+ 0, 0, fnum
);
5410 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5412 if (tevent_req_nomem(subreq
, req
)) {
5413 return tevent_req_post(req
, ev
);
5415 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5419 static void cli_flush_done(struct tevent_req
*subreq
)
5421 struct tevent_req
*req
= tevent_req_callback_data(
5422 subreq
, struct tevent_req
);
5425 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5426 TALLOC_FREE(subreq
);
5427 if (tevent_req_nterror(req
, status
)) {
5430 tevent_req_done(req
);
5433 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5435 return tevent_req_simple_recv_ntstatus(req
);
5438 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5440 TALLOC_CTX
*frame
= talloc_stackframe();
5441 struct tevent_context
*ev
;
5442 struct tevent_req
*req
;
5443 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5445 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5447 * Can't use sync call while an async call is in flight
5449 status
= NT_STATUS_INVALID_PARAMETER
;
5452 ev
= samba_tevent_context_init(frame
);
5456 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5460 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5463 status
= cli_flush_recv(req
);
5469 struct cli_shadow_copy_data_state
{
5476 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5478 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5479 struct tevent_context
*ev
,
5480 struct cli_state
*cli
,
5484 struct tevent_req
*req
, *subreq
;
5485 struct cli_shadow_copy_data_state
*state
;
5488 req
= tevent_req_create(mem_ctx
, &state
,
5489 struct cli_shadow_copy_data_state
);
5493 state
->get_names
= get_names
;
5494 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5496 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5497 SSVAL(state
->setup
+ 2, 0, fnum
);
5498 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5499 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5501 subreq
= cli_trans_send(
5502 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5503 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5506 if (tevent_req_nomem(subreq
, req
)) {
5507 return tevent_req_post(req
, ev
);
5509 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5513 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5515 struct tevent_req
*req
= tevent_req_callback_data(
5516 subreq
, struct tevent_req
);
5517 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5518 req
, struct cli_shadow_copy_data_state
);
5521 status
= cli_trans_recv(subreq
, state
, NULL
,
5522 NULL
, 0, NULL
, /* setup */
5523 NULL
, 0, NULL
, /* param */
5524 &state
->data
, 12, &state
->num_data
);
5525 TALLOC_FREE(subreq
);
5526 if (tevent_req_nterror(req
, status
)) {
5529 tevent_req_done(req
);
5532 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5533 char ***pnames
, int *pnum_names
)
5535 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5536 req
, struct cli_shadow_copy_data_state
);
5542 if (tevent_req_is_nterror(req
, &status
)) {
5545 num_names
= IVAL(state
->data
, 4);
5546 dlength
= IVAL(state
->data
, 8);
5548 if (!state
->get_names
) {
5549 *pnum_names
= num_names
;
5550 return NT_STATUS_OK
;
5553 if (dlength
+12 > state
->num_data
) {
5554 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5556 names
= talloc_array(mem_ctx
, char *, num_names
);
5557 if (names
== NULL
) {
5558 return NT_STATUS_NO_MEMORY
;
5561 for (i
=0; i
<num_names
; i
++) {
5564 size_t converted_size
;
5566 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5567 ret
= convert_string_talloc(
5568 names
, CH_UTF16LE
, CH_UNIX
,
5569 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5570 &names
[i
], &converted_size
);
5573 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5576 *pnum_names
= num_names
;
5578 return NT_STATUS_OK
;
5581 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5582 uint16_t fnum
, bool get_names
,
5583 char ***pnames
, int *pnum_names
)
5585 TALLOC_CTX
*frame
= talloc_stackframe();
5586 struct tevent_context
*ev
;
5587 struct tevent_req
*req
;
5588 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5590 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5592 * Can't use sync call while an async call is in flight
5594 status
= NT_STATUS_INVALID_PARAMETER
;
5597 ev
= samba_tevent_context_init(frame
);
5601 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5605 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5608 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);