2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf
, bool ucs2
,
39 const char *str
, size_t str_len
,
41 size_t *pconverted_size
)
45 size_t converted_size
;
51 buflen
= talloc_get_size(buf
);
54 ((align_odd
&& (buflen
% 2 == 0)) ||
55 (!align_odd
&& (buflen
% 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf
= talloc_realloc(NULL
, buf
, uint8_t, buflen
+ 1);
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
68 ucs2
? CH_UTF16LE
: CH_DOS
,
69 str
, str_len
, &converted
,
74 buf
= talloc_realloc(NULL
, buf
, uint8_t,
75 buflen
+ converted_size
);
77 TALLOC_FREE(converted
);
81 memcpy(buf
+ buflen
, converted
, converted_size
);
83 TALLOC_FREE(converted
);
85 if (pconverted_size
) {
86 *pconverted_size
= converted_size
;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
98 const char *str
, size_t str_len
,
99 size_t *pconverted_size
)
101 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
102 true, pconverted_size
);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf
, uint8_t prefix
,
106 const uint8_t *bytes
, size_t num_bytes
)
113 buflen
= talloc_get_size(buf
);
115 buf
= talloc_realloc(NULL
, buf
, uint8_t,
116 buflen
+ 1 + num_bytes
);
120 buf
[buflen
] = prefix
;
121 memcpy(&buf
[buflen
+1], bytes
, num_bytes
);
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf
, bool ucs2
,
133 const char *str
, size_t str_len
,
134 size_t *pconverted_size
)
136 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
137 false, pconverted_size
);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf
,
141 const uint8_t *bytes
, size_t num_bytes
)
148 buflen
= talloc_get_size(buf
);
150 buf
= talloc_realloc(NULL
, buf
, uint8_t,
155 memcpy(&buf
[buflen
], bytes
, num_bytes
);
159 struct cli_setpathinfo_state
{
164 static void cli_setpathinfo_done(struct tevent_req
*subreq
);
166 struct tevent_req
*cli_setpathinfo_send(TALLOC_CTX
*mem_ctx
,
167 struct tevent_context
*ev
,
168 struct cli_state
*cli
,
174 struct tevent_req
*req
, *subreq
;
175 struct cli_setpathinfo_state
*state
;
177 req
= tevent_req_create(mem_ctx
, &state
,
178 struct cli_setpathinfo_state
);
183 /* Setup setup word. */
184 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
186 /* Setup param array. */
187 state
->param
= talloc_zero_array(state
, uint8_t, 6);
188 if (tevent_req_nomem(state
->param
, req
)) {
189 return tevent_req_post(req
, ev
);
191 SSVAL(state
->param
, 0, level
);
193 state
->param
= trans2_bytes_push_str(
194 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), path
, strlen(path
)+1, NULL
);
195 if (tevent_req_nomem(state
->param
, req
)) {
196 return tevent_req_post(req
, ev
);
199 subreq
= cli_trans_send(
200 state
, /* mem ctx. */
202 cli
, /* cli_state. */
203 SMBtrans2
, /* cmd. */
204 NULL
, /* pipe name. */
208 &state
->setup
, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state
->param
, /* param. */
212 talloc_get_size(state
->param
), /* num param. */
213 2, /* max returned param. */
215 data_len
, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq
, req
)) {
219 return tevent_req_post(req
, ev
);
221 tevent_req_set_callback(subreq
, cli_setpathinfo_done
, req
);
225 static void cli_setpathinfo_done(struct tevent_req
*subreq
)
227 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
228 NULL
, 0, NULL
, NULL
, 0, NULL
);
229 tevent_req_simple_finish_ntstatus(subreq
, status
);
232 NTSTATUS
cli_setpathinfo_recv(struct tevent_req
*req
)
234 return tevent_req_simple_recv_ntstatus(req
);
237 NTSTATUS
cli_setpathinfo(struct cli_state
*cli
,
243 TALLOC_CTX
*frame
= talloc_stackframe();
244 struct tevent_context
*ev
;
245 struct tevent_req
*req
;
246 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
248 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
250 * Can't use sync call while an async call is in flight
252 status
= NT_STATUS_INVALID_PARAMETER
;
255 ev
= samba_tevent_context_init(frame
);
259 req
= cli_setpathinfo_send(ev
, ev
, cli
, level
, path
, data
, data_len
);
263 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
266 status
= cli_setpathinfo_recv(req
);
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state
{
281 static void cli_posix_link_internal_done(struct tevent_req
*subreq
);
283 static struct tevent_req
*cli_posix_link_internal_send(TALLOC_CTX
*mem_ctx
,
284 struct tevent_context
*ev
,
285 struct cli_state
*cli
,
290 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
291 struct cli_posix_link_internal_state
*state
= NULL
;
293 req
= tevent_req_create(mem_ctx
, &state
,
294 struct cli_posix_link_internal_state
);
299 /* Setup data array. */
300 state
->data
= talloc_array(state
, uint8_t, 0);
301 if (tevent_req_nomem(state
->data
, req
)) {
302 return tevent_req_post(req
, ev
);
304 state
->data
= trans2_bytes_push_str(
305 state
->data
, smbXcli_conn_use_unicode(cli
->conn
), oldname
, strlen(oldname
)+1, NULL
);
307 subreq
= cli_setpathinfo_send(
308 state
, ev
, cli
, level
, newname
,
309 state
->data
, talloc_get_size(state
->data
));
310 if (tevent_req_nomem(subreq
, req
)) {
311 return tevent_req_post(req
, ev
);
313 tevent_req_set_callback(subreq
, cli_posix_link_internal_done
, req
);
317 static void cli_posix_link_internal_done(struct tevent_req
*subreq
)
319 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
320 tevent_req_simple_finish_ntstatus(subreq
, status
);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req
*cli_posix_symlink_send(TALLOC_CTX
*mem_ctx
,
328 struct tevent_context
*ev
,
329 struct cli_state
*cli
,
333 return cli_posix_link_internal_send(
334 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_LINK
, oldname
, newname
);
337 NTSTATUS
cli_posix_symlink_recv(struct tevent_req
*req
)
339 return tevent_req_simple_recv_ntstatus(req
);
342 NTSTATUS
cli_posix_symlink(struct cli_state
*cli
,
346 TALLOC_CTX
*frame
= talloc_stackframe();
347 struct tevent_context
*ev
= NULL
;
348 struct tevent_req
*req
= NULL
;
349 NTSTATUS status
= NT_STATUS_OK
;
351 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
353 * Can't use sync call while an async call is in flight
355 status
= NT_STATUS_INVALID_PARAMETER
;
359 ev
= samba_tevent_context_init(frame
);
361 status
= NT_STATUS_NO_MEMORY
;
365 req
= cli_posix_symlink_send(frame
,
371 status
= NT_STATUS_NO_MEMORY
;
375 if (!tevent_req_poll(req
, ev
)) {
376 status
= map_nt_error_from_unix(errno
);
380 status
= cli_posix_symlink_recv(req
);
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state
{
396 static void cli_posix_readlink_done(struct tevent_req
*subreq
);
398 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
399 struct tevent_context
*ev
,
400 struct cli_state
*cli
,
404 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
405 struct readlink_state
*state
= NULL
;
406 uint32_t maxbytelen
= (uint32_t)(smbXcli_conn_use_unicode(cli
->conn
) ? len
*3 : len
);
408 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len
< len
) || (maxbytelen
< len
)) {
417 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
418 return tevent_req_post(req
, ev
);
421 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
422 SMB_QUERY_FILE_UNIX_LINK
, 1, maxbytelen
);
423 if (tevent_req_nomem(subreq
, req
)) {
424 return tevent_req_post(req
, ev
);
426 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
430 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
432 struct tevent_req
*req
= tevent_req_callback_data(
433 subreq
, struct tevent_req
);
434 struct readlink_state
*state
= tevent_req_data(
435 req
, struct readlink_state
);
438 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
441 if (tevent_req_nterror(req
, status
)) {
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state
->data
[state
->num_data
-1] != '\0') {
448 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
451 tevent_req_done(req
);
454 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
455 char *retpath
, size_t len
)
458 char *converted
= NULL
;
459 size_t converted_size
= 0;
460 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
462 if (tevent_req_is_nterror(req
, &status
)) {
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state
,
467 smbXcli_conn_use_unicode(cli
->conn
) ? CH_UTF16LE
: CH_DOS
,
473 return NT_STATUS_NO_MEMORY
;
476 len
= MIN(len
,converted_size
);
478 return NT_STATUS_DATA_ERROR
;
480 memcpy(retpath
, converted
, len
);
484 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
485 char *linkpath
, size_t len
)
487 TALLOC_CTX
*frame
= talloc_stackframe();
488 struct tevent_context
*ev
= NULL
;
489 struct tevent_req
*req
= NULL
;
490 NTSTATUS status
= NT_STATUS_OK
;
492 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
494 * Can't use sync call while an async call is in flight
496 status
= NT_STATUS_INVALID_PARAMETER
;
500 ev
= samba_tevent_context_init(frame
);
502 status
= NT_STATUS_NO_MEMORY
;
506 req
= cli_posix_readlink_send(frame
,
512 status
= NT_STATUS_NO_MEMORY
;
516 if (!tevent_req_poll(req
, ev
)) {
517 status
= map_nt_error_from_unix(errno
);
521 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
533 struct tevent_context
*ev
,
534 struct cli_state
*cli
,
538 return cli_posix_link_internal_send(
539 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_HLINK
, oldname
, newname
);
542 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
544 return tevent_req_simple_recv_ntstatus(req
);
547 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
551 TALLOC_CTX
*frame
= talloc_stackframe();
552 struct tevent_context
*ev
= NULL
;
553 struct tevent_req
*req
= NULL
;
554 NTSTATUS status
= NT_STATUS_OK
;
556 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
558 * Can't use sync call while an async call is in flight
560 status
= NT_STATUS_INVALID_PARAMETER
;
564 ev
= samba_tevent_context_init(frame
);
566 status
= NT_STATUS_NO_MEMORY
;
570 req
= cli_posix_hardlink_send(frame
,
576 status
= NT_STATUS_NO_MEMORY
;
580 if (!tevent_req_poll(req
, ev
)) {
581 status
= map_nt_error_from_unix(errno
);
585 status
= cli_posix_hardlink_recv(req
);
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state
{
601 static void cli_posix_getfacl_done(struct tevent_req
*subreq
);
603 struct tevent_req
*cli_posix_getfacl_send(TALLOC_CTX
*mem_ctx
,
604 struct tevent_context
*ev
,
605 struct cli_state
*cli
,
608 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
609 struct getfacl_state
*state
= NULL
;
611 req
= tevent_req_create(mem_ctx
, &state
, struct getfacl_state
);
615 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_QUERY_POSIX_ACL
,
617 if (tevent_req_nomem(subreq
, req
)) {
618 return tevent_req_post(req
, ev
);
620 tevent_req_set_callback(subreq
, cli_posix_getfacl_done
, req
);
624 static void cli_posix_getfacl_done(struct tevent_req
*subreq
)
626 struct tevent_req
*req
= tevent_req_callback_data(
627 subreq
, struct tevent_req
);
628 struct getfacl_state
*state
= tevent_req_data(
629 req
, struct getfacl_state
);
632 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
635 if (tevent_req_nterror(req
, status
)) {
638 tevent_req_done(req
);
641 NTSTATUS
cli_posix_getfacl_recv(struct tevent_req
*req
,
646 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
649 if (tevent_req_is_nterror(req
, &status
)) {
652 *prb_size
= (size_t)state
->num_data
;
653 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
657 NTSTATUS
cli_posix_getfacl(struct cli_state
*cli
,
663 TALLOC_CTX
*frame
= talloc_stackframe();
664 struct tevent_context
*ev
= NULL
;
665 struct tevent_req
*req
= NULL
;
666 NTSTATUS status
= NT_STATUS_OK
;
668 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
670 * Can't use sync call while an async call is in flight
672 status
= NT_STATUS_INVALID_PARAMETER
;
676 ev
= samba_tevent_context_init(frame
);
678 status
= NT_STATUS_NO_MEMORY
;
682 req
= cli_posix_getfacl_send(frame
,
687 status
= NT_STATUS_NO_MEMORY
;
691 if (!tevent_req_poll(req
, ev
)) {
692 status
= map_nt_error_from_unix(errno
);
696 status
= cli_posix_getfacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
712 static void cli_posix_stat_done(struct tevent_req
*subreq
);
714 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
715 struct tevent_context
*ev
,
716 struct cli_state
*cli
,
719 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
720 struct stat_state
*state
= NULL
;
722 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
726 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
727 SMB_QUERY_FILE_UNIX_BASIC
, 100, 100);
728 if (tevent_req_nomem(subreq
, req
)) {
729 return tevent_req_post(req
, ev
);
731 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
735 static void cli_posix_stat_done(struct tevent_req
*subreq
)
737 struct tevent_req
*req
= tevent_req_callback_data(
738 subreq
, struct tevent_req
);
739 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
742 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
745 if (tevent_req_nterror(req
, status
)) {
748 tevent_req_done(req
);
751 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
752 SMB_STRUCT_STAT
*sbuf
)
754 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
757 if (tevent_req_is_nterror(req
, &status
)) {
761 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
762 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
766 /* assume 512 byte blocks */
767 sbuf
->st_ex_blocks
/= 512;
769 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
770 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
771 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
773 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
774 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
775 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major
= IVAL(state
->data
,60);
779 uint32_t dev_minor
= IVAL(state
->data
,68);
780 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
783 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
784 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
785 sbuf
->st_ex_nlink
= BIG_UINT(state
->data
,92); /* number of hard links */
790 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
792 SMB_STRUCT_STAT
*sbuf
)
794 TALLOC_CTX
*frame
= talloc_stackframe();
795 struct tevent_context
*ev
= NULL
;
796 struct tevent_req
*req
= NULL
;
797 NTSTATUS status
= NT_STATUS_OK
;
799 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
801 * Can't use sync call while an async call is in flight
803 status
= NT_STATUS_INVALID_PARAMETER
;
807 ev
= samba_tevent_context_init(frame
);
809 status
= NT_STATUS_NO_MEMORY
;
813 req
= cli_posix_stat_send(frame
,
818 status
= NT_STATUS_NO_MEMORY
;
822 if (!tevent_req_poll(req
, ev
)) {
823 status
= map_nt_error_from_unix(errno
);
827 status
= cli_posix_stat_recv(req
, sbuf
);
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state
{
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
);
844 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
845 struct tevent_context
*ev
,
846 struct cli_state
*cli
,
852 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
853 struct cli_posix_chown_chmod_internal_state
*state
= NULL
;
855 req
= tevent_req_create(mem_ctx
, &state
,
856 struct cli_posix_chown_chmod_internal_state
);
861 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state
->data
[40], '\0', 60);
863 SIVAL(state
->data
,40,uid
);
864 SIVAL(state
->data
,48,gid
);
865 SIVAL(state
->data
,84,mode
);
867 subreq
= cli_setpathinfo_send(state
, ev
, cli
, SMB_SET_FILE_UNIX_BASIC
,
868 fname
, state
->data
, sizeof(state
->data
));
869 if (tevent_req_nomem(subreq
, req
)) {
870 return tevent_req_post(req
, ev
);
872 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
,
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
879 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
880 tevent_req_simple_finish_ntstatus(subreq
, status
);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
888 struct tevent_context
*ev
,
889 struct cli_state
*cli
,
893 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
895 unix_perms_to_wire(mode
),
900 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
902 return tevent_req_simple_recv_ntstatus(req
);
905 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
907 TALLOC_CTX
*frame
= talloc_stackframe();
908 struct tevent_context
*ev
= NULL
;
909 struct tevent_req
*req
= NULL
;
910 NTSTATUS status
= NT_STATUS_OK
;
912 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
914 * Can't use sync call while an async call is in flight
916 status
= NT_STATUS_INVALID_PARAMETER
;
920 ev
= samba_tevent_context_init(frame
);
922 status
= NT_STATUS_NO_MEMORY
;
926 req
= cli_posix_chmod_send(frame
,
932 status
= NT_STATUS_NO_MEMORY
;
936 if (!tevent_req_poll(req
, ev
)) {
937 status
= map_nt_error_from_unix(errno
);
941 status
= cli_posix_chmod_recv(req
);
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
953 struct tevent_context
*ev
,
954 struct cli_state
*cli
,
959 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
966 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
968 return tevent_req_simple_recv_ntstatus(req
);
971 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
976 TALLOC_CTX
*frame
= talloc_stackframe();
977 struct tevent_context
*ev
= NULL
;
978 struct tevent_req
*req
= NULL
;
979 NTSTATUS status
= NT_STATUS_OK
;
981 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
983 * Can't use sync call while an async call is in flight
985 status
= NT_STATUS_INVALID_PARAMETER
;
989 ev
= samba_tevent_context_init(frame
);
991 status
= NT_STATUS_NO_MEMORY
;
995 req
= cli_posix_chown_send(frame
,
1002 status
= NT_STATUS_NO_MEMORY
;
1006 if (!tevent_req_poll(req
, ev
)) {
1007 status
= map_nt_error_from_unix(errno
);
1011 status
= cli_posix_chown_recv(req
);
1018 /****************************************************************************
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req
*subreq
);
1024 struct cli_rename_state
{
1028 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1029 struct tevent_context
*ev
,
1030 struct cli_state
*cli
,
1031 const char *fname_src
,
1032 const char *fname_dst
)
1034 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1035 struct cli_rename_state
*state
= NULL
;
1036 uint8_t additional_flags
= 0;
1037 uint8_t *bytes
= NULL
;
1039 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
1044 SSVAL(state
->vwv
+0, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1046 bytes
= talloc_array(state
, uint8_t, 1);
1047 if (tevent_req_nomem(bytes
, req
)) {
1048 return tevent_req_post(req
, ev
);
1051 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1052 strlen(fname_src
)+1, NULL
);
1053 if (tevent_req_nomem(bytes
, req
)) {
1054 return tevent_req_post(req
, ev
);
1057 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1058 talloc_get_size(bytes
)+1);
1059 if (tevent_req_nomem(bytes
, req
)) {
1060 return tevent_req_post(req
, ev
);
1063 bytes
[talloc_get_size(bytes
)-1] = 4;
1064 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1065 strlen(fname_dst
)+1, NULL
);
1066 if (tevent_req_nomem(bytes
, req
)) {
1067 return tevent_req_post(req
, ev
);
1070 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1071 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1072 if (tevent_req_nomem(subreq
, req
)) {
1073 return tevent_req_post(req
, ev
);
1075 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
1079 static void cli_rename_done(struct tevent_req
*subreq
)
1081 struct tevent_req
*req
= tevent_req_callback_data(
1082 subreq
, struct tevent_req
);
1085 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1086 TALLOC_FREE(subreq
);
1087 if (tevent_req_nterror(req
, status
)) {
1090 tevent_req_done(req
);
1093 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1095 return tevent_req_simple_recv_ntstatus(req
);
1098 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1100 TALLOC_CTX
*frame
= NULL
;
1101 struct tevent_context
*ev
;
1102 struct tevent_req
*req
;
1103 NTSTATUS status
= NT_STATUS_OK
;
1105 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1106 return cli_smb2_rename(cli
,
1111 frame
= talloc_stackframe();
1113 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1115 * Can't use sync call while an async call is in flight
1117 status
= NT_STATUS_INVALID_PARAMETER
;
1121 ev
= samba_tevent_context_init(frame
);
1123 status
= NT_STATUS_NO_MEMORY
;
1127 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1129 status
= NT_STATUS_NO_MEMORY
;
1133 if (!tevent_req_poll(req
, ev
)) {
1134 status
= map_nt_error_from_unix(errno
);
1138 status
= cli_rename_recv(req
);
1145 /****************************************************************************
1147 ****************************************************************************/
1149 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1151 struct cli_ntrename_internal_state
{
1155 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1156 struct tevent_context
*ev
,
1157 struct cli_state
*cli
,
1158 const char *fname_src
,
1159 const char *fname_dst
,
1160 uint16_t rename_flag
)
1162 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1163 struct cli_ntrename_internal_state
*state
= NULL
;
1164 uint8_t additional_flags
= 0;
1165 uint8_t *bytes
= NULL
;
1167 req
= tevent_req_create(mem_ctx
, &state
,
1168 struct cli_ntrename_internal_state
);
1173 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1174 SSVAL(state
->vwv
+1, 0, rename_flag
);
1176 bytes
= talloc_array(state
, uint8_t, 1);
1177 if (tevent_req_nomem(bytes
, req
)) {
1178 return tevent_req_post(req
, ev
);
1181 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_src
,
1182 strlen(fname_src
)+1, NULL
);
1183 if (tevent_req_nomem(bytes
, req
)) {
1184 return tevent_req_post(req
, ev
);
1187 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1188 talloc_get_size(bytes
)+1);
1189 if (tevent_req_nomem(bytes
, req
)) {
1190 return tevent_req_post(req
, ev
);
1193 bytes
[talloc_get_size(bytes
)-1] = 4;
1194 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname_dst
,
1195 strlen(fname_dst
)+1, NULL
);
1196 if (tevent_req_nomem(bytes
, req
)) {
1197 return tevent_req_post(req
, ev
);
1200 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1201 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1202 if (tevent_req_nomem(subreq
, req
)) {
1203 return tevent_req_post(req
, ev
);
1205 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1209 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1211 struct tevent_req
*req
= tevent_req_callback_data(
1212 subreq
, struct tevent_req
);
1215 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1216 TALLOC_FREE(subreq
);
1217 if (tevent_req_nterror(req
, status
)) {
1220 tevent_req_done(req
);
1223 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1225 return tevent_req_simple_recv_ntstatus(req
);
1228 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1229 struct tevent_context
*ev
,
1230 struct cli_state
*cli
,
1231 const char *fname_src
,
1232 const char *fname_dst
)
1234 return cli_ntrename_internal_send(mem_ctx
,
1239 RENAME_FLAG_RENAME
);
1242 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1244 return cli_ntrename_internal_recv(req
);
1247 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1249 TALLOC_CTX
*frame
= talloc_stackframe();
1250 struct tevent_context
*ev
;
1251 struct tevent_req
*req
;
1252 NTSTATUS status
= NT_STATUS_OK
;
1254 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1256 * Can't use sync call while an async call is in flight
1258 status
= NT_STATUS_INVALID_PARAMETER
;
1262 ev
= samba_tevent_context_init(frame
);
1264 status
= NT_STATUS_NO_MEMORY
;
1268 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1270 status
= NT_STATUS_NO_MEMORY
;
1274 if (!tevent_req_poll(req
, ev
)) {
1275 status
= map_nt_error_from_unix(errno
);
1279 status
= cli_ntrename_recv(req
);
1286 /****************************************************************************
1288 ****************************************************************************/
1290 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1291 struct tevent_context
*ev
,
1292 struct cli_state
*cli
,
1293 const char *fname_src
,
1294 const char *fname_dst
)
1296 return cli_ntrename_internal_send(mem_ctx
,
1301 RENAME_FLAG_HARD_LINK
);
1304 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1306 return cli_ntrename_internal_recv(req
);
1309 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1311 TALLOC_CTX
*frame
= talloc_stackframe();
1312 struct tevent_context
*ev
;
1313 struct tevent_req
*req
;
1314 NTSTATUS status
= NT_STATUS_OK
;
1316 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1318 * Can't use sync call while an async call is in flight
1320 status
= NT_STATUS_INVALID_PARAMETER
;
1324 ev
= samba_tevent_context_init(frame
);
1326 status
= NT_STATUS_NO_MEMORY
;
1330 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1332 status
= NT_STATUS_NO_MEMORY
;
1336 if (!tevent_req_poll(req
, ev
)) {
1337 status
= map_nt_error_from_unix(errno
);
1341 status
= cli_nt_hardlink_recv(req
);
1348 /****************************************************************************
1350 ****************************************************************************/
1352 static void cli_unlink_done(struct tevent_req
*subreq
);
1354 struct cli_unlink_state
{
1358 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1359 struct tevent_context
*ev
,
1360 struct cli_state
*cli
,
1362 uint16_t mayhave_attrs
)
1364 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1365 struct cli_unlink_state
*state
= NULL
;
1366 uint8_t additional_flags
= 0;
1367 uint8_t *bytes
= NULL
;
1369 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1374 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1376 bytes
= talloc_array(state
, uint8_t, 1);
1377 if (tevent_req_nomem(bytes
, req
)) {
1378 return tevent_req_post(req
, ev
);
1381 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
1382 strlen(fname
)+1, NULL
);
1384 if (tevent_req_nomem(bytes
, req
)) {
1385 return tevent_req_post(req
, ev
);
1388 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1389 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1390 if (tevent_req_nomem(subreq
, req
)) {
1391 return tevent_req_post(req
, ev
);
1393 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1397 static void cli_unlink_done(struct tevent_req
*subreq
)
1399 struct tevent_req
*req
= tevent_req_callback_data(
1400 subreq
, struct tevent_req
);
1403 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1404 TALLOC_FREE(subreq
);
1405 if (tevent_req_nterror(req
, status
)) {
1408 tevent_req_done(req
);
1411 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1413 return tevent_req_simple_recv_ntstatus(req
);
1416 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1418 TALLOC_CTX
*frame
= NULL
;
1419 struct tevent_context
*ev
;
1420 struct tevent_req
*req
;
1421 NTSTATUS status
= NT_STATUS_OK
;
1423 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1424 return cli_smb2_unlink(cli
, fname
);
1427 frame
= talloc_stackframe();
1429 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1431 * Can't use sync call while an async call is in flight
1433 status
= NT_STATUS_INVALID_PARAMETER
;
1437 ev
= samba_tevent_context_init(frame
);
1439 status
= NT_STATUS_NO_MEMORY
;
1443 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1445 status
= NT_STATUS_NO_MEMORY
;
1449 if (!tevent_req_poll(req
, ev
)) {
1450 status
= map_nt_error_from_unix(errno
);
1454 status
= cli_unlink_recv(req
);
1461 /****************************************************************************
1463 ****************************************************************************/
1465 static void cli_mkdir_done(struct tevent_req
*subreq
);
1467 struct cli_mkdir_state
{
1471 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1472 struct tevent_context
*ev
,
1473 struct cli_state
*cli
,
1476 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1477 struct cli_mkdir_state
*state
= NULL
;
1478 uint8_t additional_flags
= 0;
1479 uint8_t *bytes
= NULL
;
1481 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1486 bytes
= talloc_array(state
, uint8_t, 1);
1487 if (tevent_req_nomem(bytes
, req
)) {
1488 return tevent_req_post(req
, ev
);
1491 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1492 strlen(dname
)+1, NULL
);
1494 if (tevent_req_nomem(bytes
, req
)) {
1495 return tevent_req_post(req
, ev
);
1498 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1499 0, NULL
, talloc_get_size(bytes
), bytes
);
1500 if (tevent_req_nomem(subreq
, req
)) {
1501 return tevent_req_post(req
, ev
);
1503 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1507 static void cli_mkdir_done(struct tevent_req
*subreq
)
1509 struct tevent_req
*req
= tevent_req_callback_data(
1510 subreq
, struct tevent_req
);
1513 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1514 TALLOC_FREE(subreq
);
1515 if (tevent_req_nterror(req
, status
)) {
1518 tevent_req_done(req
);
1521 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1523 return tevent_req_simple_recv_ntstatus(req
);
1526 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1528 TALLOC_CTX
*frame
= NULL
;
1529 struct tevent_context
*ev
;
1530 struct tevent_req
*req
;
1531 NTSTATUS status
= NT_STATUS_OK
;
1533 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1534 return cli_smb2_mkdir(cli
, dname
);
1537 frame
= talloc_stackframe();
1539 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1541 * Can't use sync call while an async call is in flight
1543 status
= NT_STATUS_INVALID_PARAMETER
;
1547 ev
= samba_tevent_context_init(frame
);
1549 status
= NT_STATUS_NO_MEMORY
;
1553 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1555 status
= NT_STATUS_NO_MEMORY
;
1559 if (!tevent_req_poll(req
, ev
)) {
1560 status
= map_nt_error_from_unix(errno
);
1564 status
= cli_mkdir_recv(req
);
1571 /****************************************************************************
1573 ****************************************************************************/
1575 static void cli_rmdir_done(struct tevent_req
*subreq
);
1577 struct cli_rmdir_state
{
1581 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1582 struct tevent_context
*ev
,
1583 struct cli_state
*cli
,
1586 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1587 struct cli_rmdir_state
*state
= NULL
;
1588 uint8_t additional_flags
= 0;
1589 uint8_t *bytes
= NULL
;
1591 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1596 bytes
= talloc_array(state
, uint8_t, 1);
1597 if (tevent_req_nomem(bytes
, req
)) {
1598 return tevent_req_post(req
, ev
);
1601 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), dname
,
1602 strlen(dname
)+1, NULL
);
1604 if (tevent_req_nomem(bytes
, req
)) {
1605 return tevent_req_post(req
, ev
);
1608 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1609 0, NULL
, talloc_get_size(bytes
), bytes
);
1610 if (tevent_req_nomem(subreq
, req
)) {
1611 return tevent_req_post(req
, ev
);
1613 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1617 static void cli_rmdir_done(struct tevent_req
*subreq
)
1619 struct tevent_req
*req
= tevent_req_callback_data(
1620 subreq
, struct tevent_req
);
1623 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1624 TALLOC_FREE(subreq
);
1625 if (tevent_req_nterror(req
, status
)) {
1628 tevent_req_done(req
);
1631 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1633 return tevent_req_simple_recv_ntstatus(req
);
1636 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1638 TALLOC_CTX
*frame
= NULL
;
1639 struct tevent_context
*ev
;
1640 struct tevent_req
*req
;
1641 NTSTATUS status
= NT_STATUS_OK
;
1643 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
1644 return cli_smb2_rmdir(cli
, dname
);
1647 frame
= talloc_stackframe();
1649 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1651 * Can't use sync call while an async call is in flight
1653 status
= NT_STATUS_INVALID_PARAMETER
;
1657 ev
= samba_tevent_context_init(frame
);
1659 status
= NT_STATUS_NO_MEMORY
;
1663 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1665 status
= NT_STATUS_NO_MEMORY
;
1669 if (!tevent_req_poll(req
, ev
)) {
1670 status
= map_nt_error_from_unix(errno
);
1674 status
= cli_rmdir_recv(req
);
1681 /****************************************************************************
1682 Set or clear the delete on close flag.
1683 ****************************************************************************/
1691 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1693 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1694 NULL
, 0, NULL
, NULL
, 0, NULL
);
1695 tevent_req_simple_finish_ntstatus(subreq
, status
);
1698 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1699 struct tevent_context
*ev
,
1700 struct cli_state
*cli
,
1704 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1705 struct doc_state
*state
= NULL
;
1707 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1712 /* Setup setup word. */
1713 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1715 /* Setup param array. */
1716 SSVAL(state
->param
,0,fnum
);
1717 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1719 /* Setup data array. */
1720 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1722 subreq
= cli_trans_send(state
, /* mem ctx. */
1723 ev
, /* event ctx. */
1724 cli
, /* cli_state. */
1725 SMBtrans2
, /* cmd. */
1726 NULL
, /* pipe name. */
1730 &state
->setup
, /* setup. */
1731 1, /* num setup uint16_t words. */
1732 0, /* max returned setup. */
1733 state
->param
, /* param. */
1735 2, /* max returned param. */
1736 state
->data
, /* data. */
1738 0); /* max returned data. */
1740 if (tevent_req_nomem(subreq
, req
)) {
1741 return tevent_req_post(req
, ev
);
1743 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1747 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1749 return tevent_req_simple_recv_ntstatus(req
);
1752 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1754 TALLOC_CTX
*frame
= talloc_stackframe();
1755 struct tevent_context
*ev
= NULL
;
1756 struct tevent_req
*req
= NULL
;
1757 NTSTATUS status
= NT_STATUS_OK
;
1759 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1761 * Can't use sync call while an async call is in flight
1763 status
= NT_STATUS_INVALID_PARAMETER
;
1767 ev
= samba_tevent_context_init(frame
);
1769 status
= NT_STATUS_NO_MEMORY
;
1773 req
= cli_nt_delete_on_close_send(frame
,
1779 status
= NT_STATUS_NO_MEMORY
;
1783 if (!tevent_req_poll(req
, ev
)) {
1784 status
= map_nt_error_from_unix(errno
);
1788 status
= cli_nt_delete_on_close_recv(req
);
1795 struct cli_ntcreate_state
{
1800 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1802 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1803 struct tevent_context
*ev
,
1804 struct cli_state
*cli
,
1806 uint32_t CreatFlags
,
1807 uint32_t DesiredAccess
,
1808 uint32_t FileAttributes
,
1809 uint32_t ShareAccess
,
1810 uint32_t CreateDisposition
,
1811 uint32_t CreateOptions
,
1812 uint8_t SecurityFlags
)
1814 struct tevent_req
*req
, *subreq
;
1815 struct cli_ntcreate_state
*state
;
1818 size_t converted_len
;
1820 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1827 SCVAL(vwv
+0, 0, 0xFF);
1832 if (cli
->use_oplocks
) {
1833 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1835 SIVAL(vwv
+3, 1, CreatFlags
);
1836 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1837 SIVAL(vwv
+7, 1, DesiredAccess
);
1838 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1839 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv
+13, 1, FileAttributes
);
1841 SIVAL(vwv
+15, 1, ShareAccess
);
1842 SIVAL(vwv
+17, 1, CreateDisposition
);
1843 SIVAL(vwv
+19, 1, CreateOptions
|
1844 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1845 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1846 SCVAL(vwv
+23, 1, SecurityFlags
);
1848 bytes
= talloc_array(state
, uint8_t, 0);
1849 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
),
1850 fname
, strlen(fname
)+1,
1853 /* sigh. this copes with broken netapp filer behaviour */
1854 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "", 1, NULL
);
1856 if (tevent_req_nomem(bytes
, req
)) {
1857 return tevent_req_post(req
, ev
);
1860 SSVAL(vwv
+2, 1, converted_len
);
1862 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1863 talloc_get_size(bytes
), bytes
);
1864 if (tevent_req_nomem(subreq
, req
)) {
1865 return tevent_req_post(req
, ev
);
1867 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1871 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1873 struct tevent_req
*req
= tevent_req_callback_data(
1874 subreq
, struct tevent_req
);
1875 struct cli_ntcreate_state
*state
= tevent_req_data(
1876 req
, struct cli_ntcreate_state
);
1883 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
,
1884 &num_bytes
, &bytes
);
1885 TALLOC_FREE(subreq
);
1886 if (tevent_req_nterror(req
, status
)) {
1889 state
->fnum
= SVAL(vwv
+2, 1);
1890 tevent_req_done(req
);
1893 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1895 struct cli_ntcreate_state
*state
= tevent_req_data(
1896 req
, struct cli_ntcreate_state
);
1899 if (tevent_req_is_nterror(req
, &status
)) {
1902 *pfnum
= state
->fnum
;
1903 return NT_STATUS_OK
;
1906 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1908 uint32_t CreatFlags
,
1909 uint32_t DesiredAccess
,
1910 uint32_t FileAttributes
,
1911 uint32_t ShareAccess
,
1912 uint32_t CreateDisposition
,
1913 uint32_t CreateOptions
,
1914 uint8_t SecurityFlags
,
1917 TALLOC_CTX
*frame
= talloc_stackframe();
1918 struct tevent_context
*ev
;
1919 struct tevent_req
*req
;
1920 NTSTATUS status
= NT_STATUS_OK
;
1922 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1924 * Can't use sync call while an async call is in flight
1926 status
= NT_STATUS_INVALID_PARAMETER
;
1930 ev
= samba_tevent_context_init(frame
);
1932 status
= NT_STATUS_NO_MEMORY
;
1936 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1937 DesiredAccess
, FileAttributes
, ShareAccess
,
1938 CreateDisposition
, CreateOptions
,
1941 status
= NT_STATUS_NO_MEMORY
;
1945 if (!tevent_req_poll(req
, ev
)) {
1946 status
= map_nt_error_from_unix(errno
);
1950 status
= cli_ntcreate_recv(req
, pfid
);
1956 struct cli_nttrans_create_state
{
1960 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1962 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1963 struct tevent_context
*ev
,
1964 struct cli_state
*cli
,
1966 uint32_t CreatFlags
,
1967 uint32_t DesiredAccess
,
1968 uint32_t FileAttributes
,
1969 uint32_t ShareAccess
,
1970 uint32_t CreateDisposition
,
1971 uint32_t CreateOptions
,
1972 uint8_t SecurityFlags
,
1973 struct security_descriptor
*secdesc
,
1974 struct ea_struct
*eas
,
1977 struct tevent_req
*req
, *subreq
;
1978 struct cli_nttrans_create_state
*state
;
1980 uint8_t *secdesc_buf
;
1983 size_t converted_len
;
1985 req
= tevent_req_create(mem_ctx
,
1986 &state
, struct cli_nttrans_create_state
);
1991 if (secdesc
!= NULL
) {
1992 status
= marshall_sec_desc(talloc_tos(), secdesc
,
1993 &secdesc_buf
, &secdesc_len
);
1994 if (tevent_req_nterror(req
, status
)) {
1995 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1996 nt_errstr(status
)));
1997 return tevent_req_post(req
, ev
);
2008 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
2009 return tevent_req_post(req
, ev
);
2012 param
= talloc_array(state
, uint8_t, 53);
2013 if (tevent_req_nomem(param
, req
)) {
2014 return tevent_req_post(req
, ev
);
2017 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
2018 fname
, strlen(fname
),
2020 if (tevent_req_nomem(param
, req
)) {
2021 return tevent_req_post(req
, ev
);
2024 SIVAL(param
, 0, CreatFlags
);
2025 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2026 SIVAL(param
, 8, DesiredAccess
);
2027 SIVAL(param
, 12, 0x0); /* AllocationSize */
2028 SIVAL(param
, 16, 0x0); /* AllocationSize */
2029 SIVAL(param
, 20, FileAttributes
);
2030 SIVAL(param
, 24, ShareAccess
);
2031 SIVAL(param
, 28, CreateDisposition
);
2032 SIVAL(param
, 32, CreateOptions
|
2033 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2034 SIVAL(param
, 36, secdesc_len
);
2035 SIVAL(param
, 40, 0); /* EA length*/
2036 SIVAL(param
, 44, converted_len
);
2037 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2038 SCVAL(param
, 52, SecurityFlags
);
2040 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2041 NULL
, -1, /* name, fid */
2042 NT_TRANSACT_CREATE
, 0,
2043 NULL
, 0, 0, /* setup */
2044 param
, talloc_get_size(param
), 128, /* param */
2045 secdesc_buf
, secdesc_len
, 0); /* data */
2046 if (tevent_req_nomem(subreq
, req
)) {
2047 return tevent_req_post(req
, ev
);
2049 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2053 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2055 struct tevent_req
*req
= tevent_req_callback_data(
2056 subreq
, struct tevent_req
);
2057 struct cli_nttrans_create_state
*state
= tevent_req_data(
2058 req
, struct cli_nttrans_create_state
);
2063 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2064 NULL
, 0, NULL
, /* rsetup */
2065 ¶m
, 69, &num_param
,
2067 if (tevent_req_nterror(req
, status
)) {
2070 state
->fnum
= SVAL(param
, 2);
2072 tevent_req_done(req
);
2075 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
, uint16_t *fnum
)
2077 struct cli_nttrans_create_state
*state
= tevent_req_data(
2078 req
, struct cli_nttrans_create_state
);
2081 if (tevent_req_is_nterror(req
, &status
)) {
2084 *fnum
= state
->fnum
;
2085 return NT_STATUS_OK
;
2088 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2090 uint32_t CreatFlags
,
2091 uint32_t DesiredAccess
,
2092 uint32_t FileAttributes
,
2093 uint32_t ShareAccess
,
2094 uint32_t CreateDisposition
,
2095 uint32_t CreateOptions
,
2096 uint8_t SecurityFlags
,
2097 struct security_descriptor
*secdesc
,
2098 struct ea_struct
*eas
,
2102 TALLOC_CTX
*frame
= talloc_stackframe();
2103 struct tevent_context
*ev
;
2104 struct tevent_req
*req
;
2105 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2107 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2109 * Can't use sync call while an async call is in flight
2111 status
= NT_STATUS_INVALID_PARAMETER
;
2114 ev
= samba_tevent_context_init(frame
);
2118 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2119 DesiredAccess
, FileAttributes
,
2120 ShareAccess
, CreateDisposition
,
2121 CreateOptions
, SecurityFlags
,
2122 secdesc
, eas
, num_eas
);
2126 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2129 status
= cli_nttrans_create_recv(req
, pfid
);
2135 /****************************************************************************
2137 WARNING: if you open with O_WRONLY then getattrE won't work!
2138 ****************************************************************************/
2140 struct cli_openx_state
{
2147 static void cli_openx_done(struct tevent_req
*subreq
);
2149 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2150 struct tevent_context
*ev
,
2151 struct cli_state
*cli
, const char *fname
,
2152 int flags
, int share_mode
,
2153 struct tevent_req
**psmbreq
)
2155 struct tevent_req
*req
, *subreq
;
2156 struct cli_openx_state
*state
;
2158 unsigned accessmode
;
2159 uint8_t additional_flags
;
2162 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2168 if (flags
& O_CREAT
) {
2171 if (!(flags
& O_EXCL
)) {
2172 if (flags
& O_TRUNC
)
2178 accessmode
= (share_mode
<<4);
2180 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2182 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2187 if ((flags
& O_SYNC
) == O_SYNC
) {
2188 accessmode
|= (1<<14);
2192 if (share_mode
== DENY_FCB
) {
2196 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2197 SCVAL(state
->vwv
+ 0, 1, 0);
2198 SSVAL(state
->vwv
+ 1, 0, 0);
2199 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2200 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2201 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2202 SSVAL(state
->vwv
+ 5, 0, 0);
2203 SIVAL(state
->vwv
+ 6, 0, 0);
2204 SSVAL(state
->vwv
+ 8, 0, openfn
);
2205 SIVAL(state
->vwv
+ 9, 0, 0);
2206 SIVAL(state
->vwv
+ 11, 0, 0);
2207 SIVAL(state
->vwv
+ 13, 0, 0);
2209 additional_flags
= 0;
2211 if (cli
->use_oplocks
) {
2212 /* if using oplocks then ask for a batch oplock via
2213 core and extended methods */
2215 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2216 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2219 bytes
= talloc_array(state
, uint8_t, 0);
2220 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
2221 strlen(fname
)+1, NULL
);
2223 if (tevent_req_nomem(bytes
, req
)) {
2224 return tevent_req_post(req
, ev
);
2227 state
->bytes
.iov_base
= (void *)bytes
;
2228 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2230 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2231 15, state
->vwv
, 1, &state
->bytes
);
2232 if (subreq
== NULL
) {
2236 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2241 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
2242 struct cli_state
*cli
, const char *fname
,
2243 int flags
, int share_mode
)
2245 struct tevent_req
*req
, *subreq
;
2248 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2254 status
= smb1cli_req_chain_submit(&subreq
, 1);
2255 if (tevent_req_nterror(req
, status
)) {
2256 return tevent_req_post(req
, ev
);
2261 static void cli_openx_done(struct tevent_req
*subreq
)
2263 struct tevent_req
*req
= tevent_req_callback_data(
2264 subreq
, struct tevent_req
);
2265 struct cli_openx_state
*state
= tevent_req_data(
2266 req
, struct cli_openx_state
);
2271 status
= cli_smb_recv(subreq
, state
, NULL
, 3, &wct
, &vwv
, NULL
,
2273 TALLOC_FREE(subreq
);
2274 if (tevent_req_nterror(req
, status
)) {
2277 state
->fnum
= SVAL(vwv
+2, 0);
2278 tevent_req_done(req
);
2281 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2283 struct cli_openx_state
*state
= tevent_req_data(
2284 req
, struct cli_openx_state
);
2287 if (tevent_req_is_nterror(req
, &status
)) {
2290 *pfnum
= state
->fnum
;
2291 return NT_STATUS_OK
;
2294 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2295 int share_mode
, uint16_t *pfnum
)
2297 TALLOC_CTX
*frame
= talloc_stackframe();
2298 struct tevent_context
*ev
;
2299 struct tevent_req
*req
;
2300 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2302 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2304 * Can't use sync call while an async call is in flight
2306 status
= NT_STATUS_INVALID_PARAMETER
;
2310 ev
= samba_tevent_context_init(frame
);
2315 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2320 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2324 status
= cli_openx_recv(req
, pfnum
);
2329 /****************************************************************************
2330 Synchronous wrapper function that does an NtCreateX open by preference
2331 and falls back to openX if this fails.
2332 ****************************************************************************/
2334 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2335 int share_mode_in
, uint16_t *pfnum
)
2338 unsigned int openfn
= 0;
2339 unsigned int dos_deny
= 0;
2340 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2342 /* Do the initial mapping into OpenX parameters. */
2343 if (flags
& O_CREAT
) {
2346 if (!(flags
& O_EXCL
)) {
2347 if (flags
& O_TRUNC
)
2353 dos_deny
= (share_mode_in
<<4);
2355 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2357 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2362 if ((flags
& O_SYNC
) == O_SYNC
) {
2363 dos_deny
|= (1<<14);
2367 if (share_mode_in
== DENY_FCB
) {
2372 /* Hmmm. This is what I think the above code
2373 should look like if it's using the constants
2376 if (flags
& O_CREAT
) {
2377 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2379 if (!(flags
& O_EXCL
)) {
2380 if (flags
& O_TRUNC
)
2381 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2383 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2386 dos_deny
= SET_DENY_MODE(share_mode_in
);
2388 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2389 dos_deny
|= DOS_OPEN_RDWR
;
2390 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2391 dos_deny
|= DOS_OPEN_WRONLY
;
2395 if ((flags
& O_SYNC
) == O_SYNC
) {
2396 dos_deny
|= FILE_SYNC_OPENMODE
;
2400 if (share_mode_in
== DENY_FCB
) {
2405 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2406 openfn
, &access_mask
,
2407 &share_mode
, &create_disposition
,
2408 &create_options
, NULL
)) {
2412 status
= cli_ntcreate(cli
,
2423 /* Try and cope will all varients of "we don't do this call"
2424 and fall back to openX. */
2426 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2427 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2428 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2429 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2430 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2431 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2432 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2433 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2434 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2442 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2445 /****************************************************************************
2447 ****************************************************************************/
2449 struct cli_close_state
{
2453 static void cli_close_done(struct tevent_req
*subreq
);
2455 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2456 struct tevent_context
*ev
,
2457 struct cli_state
*cli
,
2459 struct tevent_req
**psubreq
)
2461 struct tevent_req
*req
, *subreq
;
2462 struct cli_close_state
*state
;
2464 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2469 SSVAL(state
->vwv
+0, 0, fnum
);
2470 SIVALS(state
->vwv
+1, 0, -1);
2472 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2474 if (subreq
== NULL
) {
2478 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2483 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2484 struct tevent_context
*ev
,
2485 struct cli_state
*cli
,
2488 struct tevent_req
*req
, *subreq
;
2491 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2496 status
= smb1cli_req_chain_submit(&subreq
, 1);
2497 if (tevent_req_nterror(req
, status
)) {
2498 return tevent_req_post(req
, ev
);
2503 static void cli_close_done(struct tevent_req
*subreq
)
2505 struct tevent_req
*req
= tevent_req_callback_data(
2506 subreq
, struct tevent_req
);
2509 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2510 TALLOC_FREE(subreq
);
2511 if (tevent_req_nterror(req
, status
)) {
2514 tevent_req_done(req
);
2517 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2519 return tevent_req_simple_recv_ntstatus(req
);
2522 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2524 TALLOC_CTX
*frame
= talloc_stackframe();
2525 struct tevent_context
*ev
;
2526 struct tevent_req
*req
;
2527 NTSTATUS status
= NT_STATUS_OK
;
2529 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2531 * Can't use sync call while an async call is in flight
2533 status
= NT_STATUS_INVALID_PARAMETER
;
2537 ev
= samba_tevent_context_init(frame
);
2539 status
= NT_STATUS_NO_MEMORY
;
2543 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2545 status
= NT_STATUS_NO_MEMORY
;
2549 if (!tevent_req_poll(req
, ev
)) {
2550 status
= map_nt_error_from_unix(errno
);
2554 status
= cli_close_recv(req
);
2560 /****************************************************************************
2561 Truncate a file to a specified size
2562 ****************************************************************************/
2564 struct ftrunc_state
{
2570 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2572 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2573 NULL
, 0, NULL
, NULL
, 0, NULL
);
2574 tevent_req_simple_finish_ntstatus(subreq
, status
);
2577 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2578 struct tevent_context
*ev
,
2579 struct cli_state
*cli
,
2583 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2584 struct ftrunc_state
*state
= NULL
;
2586 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2591 /* Setup setup word. */
2592 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2594 /* Setup param array. */
2595 SSVAL(state
->param
,0,fnum
);
2596 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2597 SSVAL(state
->param
,4,0);
2599 /* Setup data array. */
2600 SBVAL(state
->data
, 0, size
);
2602 subreq
= cli_trans_send(state
, /* mem ctx. */
2603 ev
, /* event ctx. */
2604 cli
, /* cli_state. */
2605 SMBtrans2
, /* cmd. */
2606 NULL
, /* pipe name. */
2610 &state
->setup
, /* setup. */
2611 1, /* num setup uint16_t words. */
2612 0, /* max returned setup. */
2613 state
->param
, /* param. */
2615 2, /* max returned param. */
2616 state
->data
, /* data. */
2618 0); /* max returned data. */
2620 if (tevent_req_nomem(subreq
, req
)) {
2621 return tevent_req_post(req
, ev
);
2623 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2627 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2629 return tevent_req_simple_recv_ntstatus(req
);
2632 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2634 TALLOC_CTX
*frame
= talloc_stackframe();
2635 struct tevent_context
*ev
= NULL
;
2636 struct tevent_req
*req
= NULL
;
2637 NTSTATUS status
= NT_STATUS_OK
;
2639 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2641 * Can't use sync call while an async call is in flight
2643 status
= NT_STATUS_INVALID_PARAMETER
;
2647 ev
= samba_tevent_context_init(frame
);
2649 status
= NT_STATUS_NO_MEMORY
;
2653 req
= cli_ftruncate_send(frame
,
2659 status
= NT_STATUS_NO_MEMORY
;
2663 if (!tevent_req_poll(req
, ev
)) {
2664 status
= map_nt_error_from_unix(errno
);
2668 status
= cli_ftruncate_recv(req
);
2675 /****************************************************************************
2676 send a lock with a specified locktype
2677 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2678 ****************************************************************************/
2680 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2681 uint32_t offset
, uint32_t len
,
2682 int timeout
, unsigned char locktype
)
2687 unsigned int set_timeout
= 0;
2688 unsigned int saved_timeout
= 0;
2690 SCVAL(vwv
+ 0, 0, 0xff);
2691 SCVAL(vwv
+ 0, 1, 0);
2692 SSVAL(vwv
+ 1, 0, 0);
2693 SSVAL(vwv
+ 2, 0, fnum
);
2694 SCVAL(vwv
+ 3, 0, locktype
);
2695 SCVAL(vwv
+ 3, 1, 0);
2696 SIVALS(vwv
+ 4, 0, timeout
);
2697 SSVAL(vwv
+ 6, 0, 0);
2698 SSVAL(vwv
+ 7, 0, 1);
2700 SSVAL(bytes
, 0, cli_getpid(cli
));
2701 SIVAL(bytes
, 2, offset
);
2702 SIVAL(bytes
, 6, len
);
2705 if (timeout
== -1) {
2706 set_timeout
= 0x7FFFFFFF;
2708 set_timeout
= timeout
+ 2*1000;
2710 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2713 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2714 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2716 if (saved_timeout
!= 0) {
2717 cli_set_timeout(cli
, saved_timeout
);
2723 /****************************************************************************
2725 note that timeout is in units of 2 milliseconds
2726 ****************************************************************************/
2728 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2729 uint32_t offset
, uint32_t len
, int timeout
,
2730 enum brl_type lock_type
)
2734 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2735 (lock_type
== READ_LOCK
? 1 : 0));
2739 /****************************************************************************
2741 ****************************************************************************/
2743 struct cli_unlock_state
{
2748 static void cli_unlock_done(struct tevent_req
*subreq
);
2750 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2751 struct tevent_context
*ev
,
2752 struct cli_state
*cli
,
2758 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2759 struct cli_unlock_state
*state
= NULL
;
2760 uint8_t additional_flags
= 0;
2762 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2767 SCVAL(state
->vwv
+0, 0, 0xFF);
2768 SSVAL(state
->vwv
+2, 0, fnum
);
2769 SCVAL(state
->vwv
+3, 0, 0);
2770 SIVALS(state
->vwv
+4, 0, 0);
2771 SSVAL(state
->vwv
+6, 0, 1);
2772 SSVAL(state
->vwv
+7, 0, 0);
2774 SSVAL(state
->data
, 0, cli_getpid(cli
));
2775 SIVAL(state
->data
, 2, offset
);
2776 SIVAL(state
->data
, 6, len
);
2778 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2779 8, state
->vwv
, 10, state
->data
);
2780 if (tevent_req_nomem(subreq
, req
)) {
2781 return tevent_req_post(req
, ev
);
2783 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2787 static void cli_unlock_done(struct tevent_req
*subreq
)
2789 struct tevent_req
*req
= tevent_req_callback_data(
2790 subreq
, struct tevent_req
);
2793 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2794 TALLOC_FREE(subreq
);
2795 if (tevent_req_nterror(req
, status
)) {
2798 tevent_req_done(req
);
2801 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2803 return tevent_req_simple_recv_ntstatus(req
);
2806 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2811 TALLOC_CTX
*frame
= talloc_stackframe();
2812 struct tevent_context
*ev
;
2813 struct tevent_req
*req
;
2814 NTSTATUS status
= NT_STATUS_OK
;
2816 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2818 * Can't use sync call while an async call is in flight
2820 status
= NT_STATUS_INVALID_PARAMETER
;
2824 ev
= samba_tevent_context_init(frame
);
2826 status
= NT_STATUS_NO_MEMORY
;
2830 req
= cli_unlock_send(frame
, ev
, cli
,
2833 status
= NT_STATUS_NO_MEMORY
;
2837 if (!tevent_req_poll(req
, ev
)) {
2838 status
= map_nt_error_from_unix(errno
);
2842 status
= cli_unlock_recv(req
);
2849 /****************************************************************************
2850 Lock a file with 64 bit offsets.
2851 ****************************************************************************/
2853 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2854 uint64_t offset
, uint64_t len
, int timeout
,
2855 enum brl_type lock_type
)
2859 unsigned int set_timeout
= 0;
2860 unsigned int saved_timeout
= 0;
2864 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2865 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2868 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2869 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2871 SCVAL(vwv
+ 0, 0, 0xff);
2872 SCVAL(vwv
+ 0, 1, 0);
2873 SSVAL(vwv
+ 1, 0, 0);
2874 SSVAL(vwv
+ 2, 0, fnum
);
2875 SCVAL(vwv
+ 3, 0, ltype
);
2876 SCVAL(vwv
+ 3, 1, 0);
2877 SIVALS(vwv
+ 4, 0, timeout
);
2878 SSVAL(vwv
+ 6, 0, 0);
2879 SSVAL(vwv
+ 7, 0, 1);
2881 SIVAL(bytes
, 0, cli_getpid(cli
));
2882 SOFF_T_R(bytes
, 4, offset
);
2883 SOFF_T_R(bytes
, 12, len
);
2886 if (timeout
== -1) {
2887 set_timeout
= 0x7FFFFFFF;
2889 set_timeout
= timeout
+ 2*1000;
2891 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2894 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2895 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2897 if (saved_timeout
!= 0) {
2898 cli_set_timeout(cli
, saved_timeout
);
2904 /****************************************************************************
2905 Unlock a file with 64 bit offsets.
2906 ****************************************************************************/
2908 struct cli_unlock64_state
{
2913 static void cli_unlock64_done(struct tevent_req
*subreq
);
2915 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2916 struct tevent_context
*ev
,
2917 struct cli_state
*cli
,
2923 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2924 struct cli_unlock64_state
*state
= NULL
;
2925 uint8_t additional_flags
= 0;
2927 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2932 SCVAL(state
->vwv
+0, 0, 0xff);
2933 SSVAL(state
->vwv
+2, 0, fnum
);
2934 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2935 SIVALS(state
->vwv
+4, 0, 0);
2936 SSVAL(state
->vwv
+6, 0, 1);
2937 SSVAL(state
->vwv
+7, 0, 0);
2939 SIVAL(state
->data
, 0, cli_getpid(cli
));
2940 SOFF_T_R(state
->data
, 4, offset
);
2941 SOFF_T_R(state
->data
, 12, len
);
2943 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2944 8, state
->vwv
, 20, state
->data
);
2945 if (tevent_req_nomem(subreq
, req
)) {
2946 return tevent_req_post(req
, ev
);
2948 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2952 static void cli_unlock64_done(struct tevent_req
*subreq
)
2954 struct tevent_req
*req
= tevent_req_callback_data(
2955 subreq
, struct tevent_req
);
2958 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2959 TALLOC_FREE(subreq
);
2960 if (tevent_req_nterror(req
, status
)) {
2963 tevent_req_done(req
);
2966 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2968 return tevent_req_simple_recv_ntstatus(req
);
2971 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2976 TALLOC_CTX
*frame
= talloc_stackframe();
2977 struct tevent_context
*ev
;
2978 struct tevent_req
*req
;
2979 NTSTATUS status
= NT_STATUS_OK
;
2981 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2982 return cli_unlock(cli
, fnum
, offset
, len
);
2985 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2987 * Can't use sync call while an async call is in flight
2989 status
= NT_STATUS_INVALID_PARAMETER
;
2993 ev
= samba_tevent_context_init(frame
);
2995 status
= NT_STATUS_NO_MEMORY
;
2999 req
= cli_unlock64_send(frame
, ev
, cli
,
3002 status
= NT_STATUS_NO_MEMORY
;
3006 if (!tevent_req_poll(req
, ev
)) {
3007 status
= map_nt_error_from_unix(errno
);
3011 status
= cli_unlock64_recv(req
);
3018 /****************************************************************************
3019 Get/unlock a POSIX lock on a file - internal function.
3020 ****************************************************************************/
3022 struct posix_lock_state
{
3025 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3028 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3030 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3031 NULL
, 0, NULL
, NULL
, 0, NULL
);
3032 tevent_req_simple_finish_ntstatus(subreq
, status
);
3035 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3036 struct tevent_context
*ev
,
3037 struct cli_state
*cli
,
3042 enum brl_type lock_type
)
3044 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3045 struct posix_lock_state
*state
= NULL
;
3047 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3052 /* Setup setup word. */
3053 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3055 /* Setup param array. */
3056 SSVAL(&state
->param
, 0, fnum
);
3057 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3059 /* Setup data array. */
3060 switch (lock_type
) {
3062 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3063 POSIX_LOCK_TYPE_READ
);
3066 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3067 POSIX_LOCK_TYPE_WRITE
);
3070 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3071 POSIX_LOCK_TYPE_UNLOCK
);
3078 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3079 POSIX_LOCK_FLAG_WAIT
);
3081 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3082 POSIX_LOCK_FLAG_NOWAIT
);
3085 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3086 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3087 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3089 subreq
= cli_trans_send(state
, /* mem ctx. */
3090 ev
, /* event ctx. */
3091 cli
, /* cli_state. */
3092 SMBtrans2
, /* cmd. */
3093 NULL
, /* pipe name. */
3097 &state
->setup
, /* setup. */
3098 1, /* num setup uint16_t words. */
3099 0, /* max returned setup. */
3100 state
->param
, /* param. */
3102 2, /* max returned param. */
3103 state
->data
, /* data. */
3104 POSIX_LOCK_DATA_SIZE
, /* num data. */
3105 0); /* max returned data. */
3107 if (tevent_req_nomem(subreq
, req
)) {
3108 return tevent_req_post(req
, ev
);
3110 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3114 /****************************************************************************
3116 ****************************************************************************/
3118 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3119 struct tevent_context
*ev
,
3120 struct cli_state
*cli
,
3125 enum brl_type lock_type
)
3127 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3128 wait_lock
, lock_type
);
3131 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3133 return tevent_req_simple_recv_ntstatus(req
);
3136 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3137 uint64_t offset
, uint64_t len
,
3138 bool wait_lock
, enum brl_type lock_type
)
3140 TALLOC_CTX
*frame
= talloc_stackframe();
3141 struct tevent_context
*ev
= NULL
;
3142 struct tevent_req
*req
= NULL
;
3143 NTSTATUS status
= NT_STATUS_OK
;
3145 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3147 * Can't use sync call while an async call is in flight
3149 status
= NT_STATUS_INVALID_PARAMETER
;
3153 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3154 status
= NT_STATUS_INVALID_PARAMETER
;
3158 ev
= samba_tevent_context_init(frame
);
3160 status
= NT_STATUS_NO_MEMORY
;
3164 req
= cli_posix_lock_send(frame
,
3173 status
= NT_STATUS_NO_MEMORY
;
3177 if (!tevent_req_poll(req
, ev
)) {
3178 status
= map_nt_error_from_unix(errno
);
3182 status
= cli_posix_lock_recv(req
);
3189 /****************************************************************************
3190 POSIX Unlock a file.
3191 ****************************************************************************/
3193 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3194 struct tevent_context
*ev
,
3195 struct cli_state
*cli
,
3200 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3201 false, UNLOCK_LOCK
);
3204 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3206 return tevent_req_simple_recv_ntstatus(req
);
3209 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3211 TALLOC_CTX
*frame
= talloc_stackframe();
3212 struct tevent_context
*ev
= NULL
;
3213 struct tevent_req
*req
= NULL
;
3214 NTSTATUS status
= NT_STATUS_OK
;
3216 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3218 * Can't use sync call while an async call is in flight
3220 status
= NT_STATUS_INVALID_PARAMETER
;
3224 ev
= samba_tevent_context_init(frame
);
3226 status
= NT_STATUS_NO_MEMORY
;
3230 req
= cli_posix_unlock_send(frame
,
3237 status
= NT_STATUS_NO_MEMORY
;
3241 if (!tevent_req_poll(req
, ev
)) {
3242 status
= map_nt_error_from_unix(errno
);
3246 status
= cli_posix_unlock_recv(req
);
3253 /****************************************************************************
3254 Do a SMBgetattrE call.
3255 ****************************************************************************/
3257 static void cli_getattrE_done(struct tevent_req
*subreq
);
3259 struct cli_getattrE_state
{
3269 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3270 struct tevent_context
*ev
,
3271 struct cli_state
*cli
,
3274 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3275 struct cli_getattrE_state
*state
= NULL
;
3276 uint8_t additional_flags
= 0;
3278 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3283 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3284 SSVAL(state
->vwv
+0,0,fnum
);
3286 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3287 1, state
->vwv
, 0, NULL
);
3288 if (tevent_req_nomem(subreq
, req
)) {
3289 return tevent_req_post(req
, ev
);
3291 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3295 static void cli_getattrE_done(struct tevent_req
*subreq
)
3297 struct tevent_req
*req
= tevent_req_callback_data(
3298 subreq
, struct tevent_req
);
3299 struct cli_getattrE_state
*state
= tevent_req_data(
3300 req
, struct cli_getattrE_state
);
3302 uint16_t *vwv
= NULL
;
3305 status
= cli_smb_recv(subreq
, state
, NULL
, 11, &wct
, &vwv
,
3307 TALLOC_FREE(subreq
);
3308 if (tevent_req_nterror(req
, status
)) {
3312 state
->size
= (off_t
)IVAL(vwv
+6,0);
3313 state
->attr
= SVAL(vwv
+10,0);
3314 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3315 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3316 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3318 tevent_req_done(req
);
3321 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3324 time_t *change_time
,
3325 time_t *access_time
,
3328 struct cli_getattrE_state
*state
= tevent_req_data(
3329 req
, struct cli_getattrE_state
);
3332 if (tevent_req_is_nterror(req
, &status
)) {
3336 *attr
= state
->attr
;
3339 *size
= state
->size
;
3342 *change_time
= state
->change_time
;
3345 *access_time
= state
->access_time
;
3348 *write_time
= state
->write_time
;
3350 return NT_STATUS_OK
;
3353 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3357 time_t *change_time
,
3358 time_t *access_time
,
3361 TALLOC_CTX
*frame
= talloc_stackframe();
3362 struct tevent_context
*ev
= NULL
;
3363 struct tevent_req
*req
= NULL
;
3364 NTSTATUS status
= NT_STATUS_OK
;
3366 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3368 * Can't use sync call while an async call is in flight
3370 status
= NT_STATUS_INVALID_PARAMETER
;
3374 ev
= samba_tevent_context_init(frame
);
3376 status
= NT_STATUS_NO_MEMORY
;
3380 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3382 status
= NT_STATUS_NO_MEMORY
;
3386 if (!tevent_req_poll(req
, ev
)) {
3387 status
= map_nt_error_from_unix(errno
);
3391 status
= cli_getattrE_recv(req
,
3403 /****************************************************************************
3405 ****************************************************************************/
3407 static void cli_getatr_done(struct tevent_req
*subreq
);
3409 struct cli_getatr_state
{
3416 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3417 struct tevent_context
*ev
,
3418 struct cli_state
*cli
,
3421 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3422 struct cli_getatr_state
*state
= NULL
;
3423 uint8_t additional_flags
= 0;
3424 uint8_t *bytes
= NULL
;
3426 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3431 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3433 bytes
= talloc_array(state
, uint8_t, 1);
3434 if (tevent_req_nomem(bytes
, req
)) {
3435 return tevent_req_post(req
, ev
);
3438 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3439 strlen(fname
)+1, NULL
);
3441 if (tevent_req_nomem(bytes
, req
)) {
3442 return tevent_req_post(req
, ev
);
3445 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3446 0, NULL
, talloc_get_size(bytes
), bytes
);
3447 if (tevent_req_nomem(subreq
, req
)) {
3448 return tevent_req_post(req
, ev
);
3450 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3454 static void cli_getatr_done(struct tevent_req
*subreq
)
3456 struct tevent_req
*req
= tevent_req_callback_data(
3457 subreq
, struct tevent_req
);
3458 struct cli_getatr_state
*state
= tevent_req_data(
3459 req
, struct cli_getatr_state
);
3461 uint16_t *vwv
= NULL
;
3464 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3466 TALLOC_FREE(subreq
);
3467 if (tevent_req_nterror(req
, status
)) {
3471 state
->attr
= SVAL(vwv
+0,0);
3472 state
->size
= (off_t
)IVAL(vwv
+3,0);
3473 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3475 tevent_req_done(req
);
3478 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3483 struct cli_getatr_state
*state
= tevent_req_data(
3484 req
, struct cli_getatr_state
);
3487 if (tevent_req_is_nterror(req
, &status
)) {
3491 *attr
= state
->attr
;
3494 *size
= state
->size
;
3497 *write_time
= state
->write_time
;
3499 return NT_STATUS_OK
;
3502 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3508 TALLOC_CTX
*frame
= talloc_stackframe();
3509 struct tevent_context
*ev
= NULL
;
3510 struct tevent_req
*req
= NULL
;
3511 NTSTATUS status
= NT_STATUS_OK
;
3513 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3515 * Can't use sync call while an async call is in flight
3517 status
= NT_STATUS_INVALID_PARAMETER
;
3521 ev
= samba_tevent_context_init(frame
);
3523 status
= NT_STATUS_NO_MEMORY
;
3527 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3529 status
= NT_STATUS_NO_MEMORY
;
3533 if (!tevent_req_poll(req
, ev
)) {
3534 status
= map_nt_error_from_unix(errno
);
3538 status
= cli_getatr_recv(req
,
3548 /****************************************************************************
3549 Do a SMBsetattrE call.
3550 ****************************************************************************/
3552 static void cli_setattrE_done(struct tevent_req
*subreq
);
3554 struct cli_setattrE_state
{
3558 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3559 struct tevent_context
*ev
,
3560 struct cli_state
*cli
,
3566 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3567 struct cli_setattrE_state
*state
= NULL
;
3568 uint8_t additional_flags
= 0;
3570 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3575 SSVAL(state
->vwv
+0, 0, fnum
);
3576 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3577 smb1cli_conn_server_time_zone(cli
->conn
));
3578 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3579 smb1cli_conn_server_time_zone(cli
->conn
));
3580 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3581 smb1cli_conn_server_time_zone(cli
->conn
));
3583 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3584 7, state
->vwv
, 0, NULL
);
3585 if (tevent_req_nomem(subreq
, req
)) {
3586 return tevent_req_post(req
, ev
);
3588 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3592 static void cli_setattrE_done(struct tevent_req
*subreq
)
3594 struct tevent_req
*req
= tevent_req_callback_data(
3595 subreq
, struct tevent_req
);
3598 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3599 TALLOC_FREE(subreq
);
3600 if (tevent_req_nterror(req
, status
)) {
3603 tevent_req_done(req
);
3606 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3608 return tevent_req_simple_recv_ntstatus(req
);
3611 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3617 TALLOC_CTX
*frame
= talloc_stackframe();
3618 struct tevent_context
*ev
= NULL
;
3619 struct tevent_req
*req
= NULL
;
3620 NTSTATUS status
= NT_STATUS_OK
;
3622 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3624 * Can't use sync call while an async call is in flight
3626 status
= NT_STATUS_INVALID_PARAMETER
;
3630 ev
= samba_tevent_context_init(frame
);
3632 status
= NT_STATUS_NO_MEMORY
;
3636 req
= cli_setattrE_send(frame
, ev
,
3644 status
= NT_STATUS_NO_MEMORY
;
3648 if (!tevent_req_poll(req
, ev
)) {
3649 status
= map_nt_error_from_unix(errno
);
3653 status
= cli_setattrE_recv(req
);
3660 /****************************************************************************
3661 Do a SMBsetatr call.
3662 ****************************************************************************/
3664 static void cli_setatr_done(struct tevent_req
*subreq
);
3666 struct cli_setatr_state
{
3670 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3671 struct tevent_context
*ev
,
3672 struct cli_state
*cli
,
3677 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3678 struct cli_setatr_state
*state
= NULL
;
3679 uint8_t additional_flags
= 0;
3680 uint8_t *bytes
= NULL
;
3682 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3687 SSVAL(state
->vwv
+0, 0, attr
);
3688 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3690 bytes
= talloc_array(state
, uint8_t, 1);
3691 if (tevent_req_nomem(bytes
, req
)) {
3692 return tevent_req_post(req
, ev
);
3695 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3696 strlen(fname
)+1, NULL
);
3697 if (tevent_req_nomem(bytes
, req
)) {
3698 return tevent_req_post(req
, ev
);
3700 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3701 talloc_get_size(bytes
)+1);
3702 if (tevent_req_nomem(bytes
, req
)) {
3703 return tevent_req_post(req
, ev
);
3706 bytes
[talloc_get_size(bytes
)-1] = 4;
3707 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), "",
3709 if (tevent_req_nomem(bytes
, req
)) {
3710 return tevent_req_post(req
, ev
);
3713 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3714 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3715 if (tevent_req_nomem(subreq
, req
)) {
3716 return tevent_req_post(req
, ev
);
3718 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3722 static void cli_setatr_done(struct tevent_req
*subreq
)
3724 struct tevent_req
*req
= tevent_req_callback_data(
3725 subreq
, struct tevent_req
);
3728 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3729 TALLOC_FREE(subreq
);
3730 if (tevent_req_nterror(req
, status
)) {
3733 tevent_req_done(req
);
3736 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3738 return tevent_req_simple_recv_ntstatus(req
);
3741 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3746 TALLOC_CTX
*frame
= talloc_stackframe();
3747 struct tevent_context
*ev
= NULL
;
3748 struct tevent_req
*req
= NULL
;
3749 NTSTATUS status
= NT_STATUS_OK
;
3751 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3753 * Can't use sync call while an async call is in flight
3755 status
= NT_STATUS_INVALID_PARAMETER
;
3759 ev
= samba_tevent_context_init(frame
);
3761 status
= NT_STATUS_NO_MEMORY
;
3765 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3767 status
= NT_STATUS_NO_MEMORY
;
3771 if (!tevent_req_poll(req
, ev
)) {
3772 status
= map_nt_error_from_unix(errno
);
3776 status
= cli_setatr_recv(req
);
3783 /****************************************************************************
3784 Check for existance of a dir.
3785 ****************************************************************************/
3787 static void cli_chkpath_done(struct tevent_req
*subreq
);
3789 struct cli_chkpath_state
{
3793 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3794 struct tevent_context
*ev
,
3795 struct cli_state
*cli
,
3798 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3799 struct cli_chkpath_state
*state
= NULL
;
3800 uint8_t additional_flags
= 0;
3801 uint8_t *bytes
= NULL
;
3803 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3808 bytes
= talloc_array(state
, uint8_t, 1);
3809 if (tevent_req_nomem(bytes
, req
)) {
3810 return tevent_req_post(req
, ev
);
3813 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
3814 strlen(fname
)+1, NULL
);
3816 if (tevent_req_nomem(bytes
, req
)) {
3817 return tevent_req_post(req
, ev
);
3820 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3821 0, NULL
, talloc_get_size(bytes
), bytes
);
3822 if (tevent_req_nomem(subreq
, req
)) {
3823 return tevent_req_post(req
, ev
);
3825 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3829 static void cli_chkpath_done(struct tevent_req
*subreq
)
3831 struct tevent_req
*req
= tevent_req_callback_data(
3832 subreq
, struct tevent_req
);
3835 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3836 TALLOC_FREE(subreq
);
3837 if (tevent_req_nterror(req
, status
)) {
3840 tevent_req_done(req
);
3843 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3845 return tevent_req_simple_recv_ntstatus(req
);
3848 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3850 TALLOC_CTX
*frame
= talloc_stackframe();
3851 struct tevent_context
*ev
= NULL
;
3852 struct tevent_req
*req
= NULL
;
3854 NTSTATUS status
= NT_STATUS_OK
;
3856 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3858 * Can't use sync call while an async call is in flight
3860 status
= NT_STATUS_INVALID_PARAMETER
;
3864 path2
= talloc_strdup(frame
, path
);
3866 status
= NT_STATUS_NO_MEMORY
;
3869 trim_char(path2
,'\0','\\');
3871 path2
= talloc_strdup(frame
, "\\");
3873 status
= NT_STATUS_NO_MEMORY
;
3878 ev
= samba_tevent_context_init(frame
);
3880 status
= NT_STATUS_NO_MEMORY
;
3884 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3886 status
= NT_STATUS_NO_MEMORY
;
3890 if (!tevent_req_poll(req
, ev
)) {
3891 status
= map_nt_error_from_unix(errno
);
3895 status
= cli_chkpath_recv(req
);
3902 /****************************************************************************
3904 ****************************************************************************/
3906 static void cli_dskattr_done(struct tevent_req
*subreq
);
3908 struct cli_dskattr_state
{
3914 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3915 struct tevent_context
*ev
,
3916 struct cli_state
*cli
)
3918 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3919 struct cli_dskattr_state
*state
= NULL
;
3920 uint8_t additional_flags
= 0;
3922 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3927 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3929 if (tevent_req_nomem(subreq
, req
)) {
3930 return tevent_req_post(req
, ev
);
3932 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3936 static void cli_dskattr_done(struct tevent_req
*subreq
)
3938 struct tevent_req
*req
= tevent_req_callback_data(
3939 subreq
, struct tevent_req
);
3940 struct cli_dskattr_state
*state
= tevent_req_data(
3941 req
, struct cli_dskattr_state
);
3943 uint16_t *vwv
= NULL
;
3946 status
= cli_smb_recv(subreq
, state
, NULL
, 4, &wct
, &vwv
, NULL
,
3948 TALLOC_FREE(subreq
);
3949 if (tevent_req_nterror(req
, status
)) {
3952 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3953 state
->total
= SVAL(vwv
+0, 0);
3954 state
->avail
= SVAL(vwv
+3, 0);
3955 tevent_req_done(req
);
3958 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3960 struct cli_dskattr_state
*state
= tevent_req_data(
3961 req
, struct cli_dskattr_state
);
3964 if (tevent_req_is_nterror(req
, &status
)) {
3967 *bsize
= state
->bsize
;
3968 *total
= state
->total
;
3969 *avail
= state
->avail
;
3970 return NT_STATUS_OK
;
3973 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3975 TALLOC_CTX
*frame
= talloc_stackframe();
3976 struct tevent_context
*ev
= NULL
;
3977 struct tevent_req
*req
= NULL
;
3978 NTSTATUS status
= NT_STATUS_OK
;
3980 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3982 * Can't use sync call while an async call is in flight
3984 status
= NT_STATUS_INVALID_PARAMETER
;
3988 ev
= samba_tevent_context_init(frame
);
3990 status
= NT_STATUS_NO_MEMORY
;
3994 req
= cli_dskattr_send(frame
, ev
, cli
);
3996 status
= NT_STATUS_NO_MEMORY
;
4000 if (!tevent_req_poll(req
, ev
)) {
4001 status
= map_nt_error_from_unix(errno
);
4005 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4012 /****************************************************************************
4013 Create and open a temporary file.
4014 ****************************************************************************/
4016 static void cli_ctemp_done(struct tevent_req
*subreq
);
4018 struct ctemp_state
{
4024 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4025 struct tevent_context
*ev
,
4026 struct cli_state
*cli
,
4029 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4030 struct ctemp_state
*state
= NULL
;
4031 uint8_t additional_flags
= 0;
4032 uint8_t *bytes
= NULL
;
4034 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4039 SSVAL(state
->vwv
,0,0);
4040 SIVALS(state
->vwv
+1,0,-1);
4042 bytes
= talloc_array(state
, uint8_t, 1);
4043 if (tevent_req_nomem(bytes
, req
)) {
4044 return tevent_req_post(req
, ev
);
4047 bytes
= smb_bytes_push_str(bytes
, smbXcli_conn_use_unicode(cli
->conn
), path
,
4048 strlen(path
)+1, NULL
);
4049 if (tevent_req_nomem(bytes
, req
)) {
4050 return tevent_req_post(req
, ev
);
4053 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4054 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4055 if (tevent_req_nomem(subreq
, req
)) {
4056 return tevent_req_post(req
, ev
);
4058 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4062 static void cli_ctemp_done(struct tevent_req
*subreq
)
4064 struct tevent_req
*req
= tevent_req_callback_data(
4065 subreq
, struct tevent_req
);
4066 struct ctemp_state
*state
= tevent_req_data(
4067 req
, struct ctemp_state
);
4071 uint32_t num_bytes
= 0;
4072 uint8_t *bytes
= NULL
;
4074 status
= cli_smb_recv(subreq
, state
, NULL
, 1, &wcnt
, &vwv
,
4075 &num_bytes
, &bytes
);
4076 TALLOC_FREE(subreq
);
4077 if (tevent_req_nterror(req
, status
)) {
4081 state
->fnum
= SVAL(vwv
+0, 0);
4083 /* From W2K3, the result is just the ASCII name */
4084 if (num_bytes
< 2) {
4085 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4089 if (pull_string_talloc(state
,
4096 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4099 tevent_req_done(req
);
4102 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4107 struct ctemp_state
*state
= tevent_req_data(req
,
4108 struct ctemp_state
);
4111 if (tevent_req_is_nterror(req
, &status
)) {
4114 *pfnum
= state
->fnum
;
4115 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4117 return NT_STATUS_NO_MEMORY
;
4119 return NT_STATUS_OK
;
4122 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4128 TALLOC_CTX
*frame
= talloc_stackframe();
4129 struct tevent_context
*ev
;
4130 struct tevent_req
*req
;
4131 NTSTATUS status
= NT_STATUS_OK
;
4133 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4135 * Can't use sync call while an async call is in flight
4137 status
= NT_STATUS_INVALID_PARAMETER
;
4141 ev
= samba_tevent_context_init(frame
);
4143 status
= NT_STATUS_NO_MEMORY
;
4147 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4149 status
= NT_STATUS_NO_MEMORY
;
4153 if (!tevent_req_poll(req
, ev
)) {
4154 status
= map_nt_error_from_unix(errno
);
4158 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4166 send a raw ioctl - used by the torture code
4168 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4173 SSVAL(vwv
+0, 0, fnum
);
4174 SSVAL(vwv
+1, 0, code
>>16);
4175 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4177 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4178 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4179 if (!NT_STATUS_IS_OK(status
)) {
4182 *blob
= data_blob_null
;
4183 return NT_STATUS_OK
;
4186 /*********************************************************
4187 Set an extended attribute utility fn.
4188 *********************************************************/
4190 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4191 uint8_t *param
, unsigned int param_len
,
4192 const char *ea_name
,
4193 const char *ea_val
, size_t ea_len
)
4196 unsigned int data_len
= 0;
4197 uint8_t *data
= NULL
;
4199 size_t ea_namelen
= strlen(ea_name
);
4202 SSVAL(setup
, 0, setup_val
);
4204 if (ea_namelen
== 0 && ea_len
== 0) {
4206 data
= talloc_array(talloc_tos(),
4210 return NT_STATUS_NO_MEMORY
;
4213 SIVAL(p
,0,data_len
);
4215 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4216 data
= talloc_array(talloc_tos(),
4220 return NT_STATUS_NO_MEMORY
;
4223 SIVAL(p
,0,data_len
);
4225 SCVAL(p
, 0, 0); /* EA flags. */
4226 SCVAL(p
, 1, ea_namelen
);
4227 SSVAL(p
, 2, ea_len
);
4228 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4229 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4232 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4234 param
, param_len
, 2,
4235 data
, data_len
, CLI_BUFFER_SIZE
,
4237 NULL
, 0, NULL
, /* rsetup */
4238 NULL
, 0, NULL
, /* rparam */
4239 NULL
, 0, NULL
); /* rdata */
4244 /*********************************************************
4245 Set an extended attribute on a pathname.
4246 *********************************************************/
4248 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4249 const char *ea_name
, const char *ea_val
,
4252 unsigned int param_len
= 0;
4255 TALLOC_CTX
*frame
= talloc_stackframe();
4257 param
= talloc_array(talloc_tos(), uint8_t, 6);
4259 return NT_STATUS_NO_MEMORY
;
4261 SSVAL(param
,0,SMB_INFO_SET_EA
);
4265 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
4266 path
, strlen(path
)+1,
4268 param_len
= talloc_get_size(param
);
4270 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4271 ea_name
, ea_val
, ea_len
);
4276 /*********************************************************
4277 Set an extended attribute on an fnum.
4278 *********************************************************/
4280 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4281 const char *ea_name
, const char *ea_val
,
4286 memset(param
, 0, 6);
4287 SSVAL(param
,0,fnum
);
4288 SSVAL(param
,2,SMB_INFO_SET_EA
);
4290 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4291 ea_name
, ea_val
, ea_len
);
4294 /*********************************************************
4295 Get an extended attribute list utility fn.
4296 *********************************************************/
4298 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4300 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4302 struct ea_struct
*ea_list
= NULL
;
4307 if (rdata_len
< 4) {
4311 ea_size
= (size_t)IVAL(rdata
,0);
4312 if (ea_size
> rdata_len
) {
4317 /* No EA's present. */
4326 /* Validate the EA list and count it. */
4327 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4328 unsigned int ea_namelen
= CVAL(p
,1);
4329 unsigned int ea_valuelen
= SVAL(p
,2);
4330 if (ea_namelen
== 0) {
4333 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4336 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4337 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4346 *pnum_eas
= num_eas
;
4348 /* Caller only wants number of EA's. */
4352 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4357 ea_size
= (size_t)IVAL(rdata
,0);
4360 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4361 struct ea_struct
*ea
= &ea_list
[num_eas
];
4362 fstring unix_ea_name
;
4363 unsigned int ea_namelen
= CVAL(p
,1);
4364 unsigned int ea_valuelen
= SVAL(p
,2);
4366 ea
->flags
= CVAL(p
,0);
4367 unix_ea_name
[0] = '\0';
4368 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4369 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4373 /* Ensure the value is null terminated (in case it's a string). */
4374 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4375 if (!ea
->value
.data
) {
4379 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4381 ea
->value
.data
[ea_valuelen
] = 0;
4383 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4386 *pea_list
= ea_list
;
4390 TALLOC_FREE(ea_list
);
4394 /*********************************************************
4395 Get an extended attribute list from a pathname.
4396 *********************************************************/
4398 struct cli_get_ea_list_path_state
{
4403 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4405 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4406 struct tevent_context
*ev
,
4407 struct cli_state
*cli
,
4410 struct tevent_req
*req
, *subreq
;
4411 struct cli_get_ea_list_path_state
*state
;
4413 req
= tevent_req_create(mem_ctx
, &state
,
4414 struct cli_get_ea_list_path_state
);
4418 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4419 SMB_INFO_QUERY_ALL_EAS
, 4,
4421 if (tevent_req_nomem(subreq
, req
)) {
4422 return tevent_req_post(req
, ev
);
4424 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4428 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4430 struct tevent_req
*req
= tevent_req_callback_data(
4431 subreq
, struct tevent_req
);
4432 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4433 req
, struct cli_get_ea_list_path_state
);
4436 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4438 TALLOC_FREE(subreq
);
4439 if (tevent_req_nterror(req
, status
)) {
4442 tevent_req_done(req
);
4445 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4446 size_t *pnum_eas
, struct ea_struct
**peas
)
4448 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4449 req
, struct cli_get_ea_list_path_state
);
4452 if (tevent_req_is_nterror(req
, &status
)) {
4455 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4457 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4459 return NT_STATUS_OK
;
4462 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4465 struct ea_struct
**pea_list
)
4467 TALLOC_CTX
*frame
= talloc_stackframe();
4468 struct tevent_context
*ev
= NULL
;
4469 struct tevent_req
*req
= NULL
;
4470 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4472 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4474 * Can't use sync call while an async call is in flight
4476 status
= NT_STATUS_INVALID_PARAMETER
;
4479 ev
= samba_tevent_context_init(frame
);
4483 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4487 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4490 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4496 /****************************************************************************
4497 Convert open "flags" arg to uint32_t on wire.
4498 ****************************************************************************/
4500 static uint32_t open_flags_to_wire(int flags
)
4502 int open_mode
= flags
& O_ACCMODE
;
4505 switch (open_mode
) {
4507 ret
|= SMB_O_WRONLY
;
4514 ret
|= SMB_O_RDONLY
;
4518 if (flags
& O_CREAT
) {
4521 if (flags
& O_EXCL
) {
4524 if (flags
& O_TRUNC
) {
4528 if (flags
& O_SYNC
) {
4532 if (flags
& O_APPEND
) {
4533 ret
|= SMB_O_APPEND
;
4535 #if defined(O_DIRECT)
4536 if (flags
& O_DIRECT
) {
4537 ret
|= SMB_O_DIRECT
;
4540 #if defined(O_DIRECTORY)
4541 if (flags
& O_DIRECTORY
) {
4542 ret
|= SMB_O_DIRECTORY
;
4548 /****************************************************************************
4549 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4550 ****************************************************************************/
4552 struct posix_open_state
{
4556 uint16_t fnum
; /* Out */
4559 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4561 struct tevent_req
*req
= tevent_req_callback_data(
4562 subreq
, struct tevent_req
);
4563 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4568 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4569 NULL
, 0, NULL
, &data
, 12, &num_data
);
4570 TALLOC_FREE(subreq
);
4571 if (tevent_req_nterror(req
, status
)) {
4574 state
->fnum
= SVAL(data
,2);
4575 tevent_req_done(req
);
4578 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4579 struct tevent_context
*ev
,
4580 struct cli_state
*cli
,
4586 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4587 struct posix_open_state
*state
= NULL
;
4588 uint32_t wire_flags
= open_flags_to_wire(flags
);
4590 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4595 /* Setup setup word. */
4596 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4598 /* Setup param array. */
4599 state
->param
= talloc_array(state
, uint8_t, 6);
4600 if (tevent_req_nomem(state
->param
, req
)) {
4601 return tevent_req_post(req
, ev
);
4603 memset(state
->param
, '\0', 6);
4604 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4606 state
->param
= trans2_bytes_push_str(state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
,
4607 strlen(fname
)+1, NULL
);
4609 if (tevent_req_nomem(state
->param
, req
)) {
4610 return tevent_req_post(req
, ev
);
4613 /* Setup data words. */
4615 wire_flags
|= SMB_O_DIRECTORY
;
4618 SIVAL(state
->data
,0,0); /* No oplock. */
4619 SIVAL(state
->data
,4,wire_flags
);
4620 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4621 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4622 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4624 subreq
= cli_trans_send(state
, /* mem ctx. */
4625 ev
, /* event ctx. */
4626 cli
, /* cli_state. */
4627 SMBtrans2
, /* cmd. */
4628 NULL
, /* pipe name. */
4632 &state
->setup
, /* setup. */
4633 1, /* num setup uint16_t words. */
4634 0, /* max returned setup. */
4635 state
->param
, /* param. */
4636 talloc_get_size(state
->param
),/* num param. */
4637 2, /* max returned param. */
4638 state
->data
, /* data. */
4640 12); /* max returned data. */
4642 if (tevent_req_nomem(subreq
, req
)) {
4643 return tevent_req_post(req
, ev
);
4645 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4649 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4650 struct tevent_context
*ev
,
4651 struct cli_state
*cli
,
4656 return cli_posix_open_internal_send(mem_ctx
, ev
,
4657 cli
, fname
, flags
, mode
, false);
4660 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4662 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4665 if (tevent_req_is_nterror(req
, &status
)) {
4668 *pfnum
= state
->fnum
;
4669 return NT_STATUS_OK
;
4672 /****************************************************************************
4673 Open - POSIX semantics. Doesn't request oplock.
4674 ****************************************************************************/
4676 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4677 int flags
, mode_t mode
, uint16_t *pfnum
)
4680 TALLOC_CTX
*frame
= talloc_stackframe();
4681 struct tevent_context
*ev
= NULL
;
4682 struct tevent_req
*req
= NULL
;
4683 NTSTATUS status
= NT_STATUS_OK
;
4685 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4687 * Can't use sync call while an async call is in flight
4689 status
= NT_STATUS_INVALID_PARAMETER
;
4693 ev
= samba_tevent_context_init(frame
);
4695 status
= NT_STATUS_NO_MEMORY
;
4699 req
= cli_posix_open_send(frame
,
4706 status
= NT_STATUS_NO_MEMORY
;
4710 if (!tevent_req_poll(req
, ev
)) {
4711 status
= map_nt_error_from_unix(errno
);
4715 status
= cli_posix_open_recv(req
, pfnum
);
4722 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4723 struct tevent_context
*ev
,
4724 struct cli_state
*cli
,
4728 return cli_posix_open_internal_send(mem_ctx
, ev
,
4729 cli
, fname
, O_CREAT
, mode
, true);
4732 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4734 return tevent_req_simple_recv_ntstatus(req
);
4737 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4739 TALLOC_CTX
*frame
= talloc_stackframe();
4740 struct tevent_context
*ev
= NULL
;
4741 struct tevent_req
*req
= NULL
;
4742 NTSTATUS status
= NT_STATUS_OK
;
4744 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4746 * Can't use sync call while an async call is in flight
4748 status
= NT_STATUS_INVALID_PARAMETER
;
4752 ev
= samba_tevent_context_init(frame
);
4754 status
= NT_STATUS_NO_MEMORY
;
4758 req
= cli_posix_mkdir_send(frame
,
4764 status
= NT_STATUS_NO_MEMORY
;
4768 if (!tevent_req_poll(req
, ev
)) {
4769 status
= map_nt_error_from_unix(errno
);
4773 status
= cli_posix_mkdir_recv(req
);
4780 /****************************************************************************
4781 unlink or rmdir - POSIX semantics.
4782 ****************************************************************************/
4784 struct cli_posix_unlink_internal_state
{
4788 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4790 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4791 struct tevent_context
*ev
,
4792 struct cli_state
*cli
,
4796 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4797 struct cli_posix_unlink_internal_state
*state
= NULL
;
4799 req
= tevent_req_create(mem_ctx
, &state
,
4800 struct cli_posix_unlink_internal_state
);
4805 /* Setup data word. */
4806 SSVAL(state
->data
, 0, level
);
4808 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4809 SMB_POSIX_PATH_UNLINK
,
4811 state
->data
, sizeof(state
->data
));
4812 if (tevent_req_nomem(subreq
, req
)) {
4813 return tevent_req_post(req
, ev
);
4815 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4819 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4821 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4822 tevent_req_simple_finish_ntstatus(subreq
, status
);
4825 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4826 struct tevent_context
*ev
,
4827 struct cli_state
*cli
,
4830 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4831 SMB_POSIX_UNLINK_FILE_TARGET
);
4834 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4836 return tevent_req_simple_recv_ntstatus(req
);
4839 /****************************************************************************
4840 unlink - POSIX semantics.
4841 ****************************************************************************/
4843 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4845 TALLOC_CTX
*frame
= talloc_stackframe();
4846 struct tevent_context
*ev
= NULL
;
4847 struct tevent_req
*req
= NULL
;
4848 NTSTATUS status
= NT_STATUS_OK
;
4850 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4852 * Can't use sync call while an async call is in flight
4854 status
= NT_STATUS_INVALID_PARAMETER
;
4858 ev
= samba_tevent_context_init(frame
);
4860 status
= NT_STATUS_NO_MEMORY
;
4864 req
= cli_posix_unlink_send(frame
,
4869 status
= NT_STATUS_NO_MEMORY
;
4873 if (!tevent_req_poll(req
, ev
)) {
4874 status
= map_nt_error_from_unix(errno
);
4878 status
= cli_posix_unlink_recv(req
);
4885 /****************************************************************************
4886 rmdir - POSIX semantics.
4887 ****************************************************************************/
4889 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4890 struct tevent_context
*ev
,
4891 struct cli_state
*cli
,
4894 return cli_posix_unlink_internal_send(
4895 mem_ctx
, ev
, cli
, fname
,
4896 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4899 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4901 return tevent_req_simple_recv_ntstatus(req
);
4904 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4906 TALLOC_CTX
*frame
= talloc_stackframe();
4907 struct tevent_context
*ev
= NULL
;
4908 struct tevent_req
*req
= NULL
;
4909 NTSTATUS status
= NT_STATUS_OK
;
4911 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4913 * Can't use sync call while an async call is in flight
4915 status
= NT_STATUS_INVALID_PARAMETER
;
4919 ev
= samba_tevent_context_init(frame
);
4921 status
= NT_STATUS_NO_MEMORY
;
4925 req
= cli_posix_rmdir_send(frame
,
4930 status
= NT_STATUS_NO_MEMORY
;
4934 if (!tevent_req_poll(req
, ev
)) {
4935 status
= map_nt_error_from_unix(errno
);
4939 status
= cli_posix_rmdir_recv(req
, frame
);
4946 /****************************************************************************
4948 ****************************************************************************/
4950 struct cli_notify_state
{
4952 uint32_t num_changes
;
4953 struct notify_change
*changes
;
4956 static void cli_notify_done(struct tevent_req
*subreq
);
4958 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4959 struct tevent_context
*ev
,
4960 struct cli_state
*cli
, uint16_t fnum
,
4961 uint32_t buffer_size
,
4962 uint32_t completion_filter
, bool recursive
)
4964 struct tevent_req
*req
, *subreq
;
4965 struct cli_notify_state
*state
;
4966 unsigned old_timeout
;
4968 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
4973 SIVAL(state
->setup
, 0, completion_filter
);
4974 SSVAL(state
->setup
, 4, fnum
);
4975 SSVAL(state
->setup
, 6, recursive
);
4978 * Notifies should not time out
4980 old_timeout
= cli_set_timeout(cli
, 0);
4982 subreq
= cli_trans_send(
4983 state
, /* mem ctx. */
4984 ev
, /* event ctx. */
4985 cli
, /* cli_state. */
4986 SMBnttrans
, /* cmd. */
4987 NULL
, /* pipe name. */
4989 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
4991 (uint16_t *)state
->setup
, /* setup. */
4992 4, /* num setup uint16_t words. */
4993 0, /* max returned setup. */
4996 buffer_size
, /* max returned param. */
4999 0); /* max returned data. */
5001 cli_set_timeout(cli
, old_timeout
);
5003 if (tevent_req_nomem(subreq
, req
)) {
5004 return tevent_req_post(req
, ev
);
5006 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5010 static void cli_notify_done(struct tevent_req
*subreq
)
5012 struct tevent_req
*req
= tevent_req_callback_data(
5013 subreq
, struct tevent_req
);
5014 struct cli_notify_state
*state
= tevent_req_data(
5015 req
, struct cli_notify_state
);
5018 uint32_t i
, ofs
, num_params
;
5021 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5022 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5023 TALLOC_FREE(subreq
);
5024 if (tevent_req_nterror(req
, status
)) {
5025 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5029 state
->num_changes
= 0;
5032 while (num_params
- ofs
> 12) {
5033 uint32_t next
= IVAL(params
, ofs
);
5034 state
->num_changes
+= 1;
5036 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5042 state
->changes
= talloc_array(state
, struct notify_change
,
5043 state
->num_changes
);
5044 if (tevent_req_nomem(state
->changes
, req
)) {
5045 TALLOC_FREE(params
);
5051 for (i
=0; i
<state
->num_changes
; i
++) {
5052 uint32_t next
= IVAL(params
, ofs
);
5053 uint32_t len
= IVAL(params
, ofs
+8);
5057 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5058 TALLOC_FREE(params
);
5060 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5064 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5065 ret
= clistr_pull_talloc(state
->changes
, (char *)params
, flags2
,
5066 &name
, params
+ofs
+12, len
,
5067 STR_TERMINATE
|STR_UNICODE
);
5069 TALLOC_FREE(params
);
5070 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5073 state
->changes
[i
].name
= name
;
5077 TALLOC_FREE(params
);
5078 tevent_req_done(req
);
5081 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5082 uint32_t *pnum_changes
,
5083 struct notify_change
**pchanges
)
5085 struct cli_notify_state
*state
= tevent_req_data(
5086 req
, struct cli_notify_state
);
5089 if (tevent_req_is_nterror(req
, &status
)) {
5093 *pnum_changes
= state
->num_changes
;
5094 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5095 return NT_STATUS_OK
;
5098 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5099 uint32_t completion_filter
, bool recursive
,
5100 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5101 struct notify_change
**pchanges
)
5103 TALLOC_CTX
*frame
= talloc_stackframe();
5104 struct tevent_context
*ev
;
5105 struct tevent_req
*req
;
5106 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5108 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5110 * Can't use sync call while an async call is in flight
5112 status
= NT_STATUS_INVALID_PARAMETER
;
5115 ev
= samba_tevent_context_init(frame
);
5119 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5120 completion_filter
, recursive
);
5124 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5127 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5133 struct cli_qpathinfo_state
{
5142 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5144 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5145 struct tevent_context
*ev
,
5146 struct cli_state
*cli
, const char *fname
,
5147 uint16_t level
, uint32_t min_rdata
,
5150 struct tevent_req
*req
, *subreq
;
5151 struct cli_qpathinfo_state
*state
;
5153 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5157 state
->min_rdata
= min_rdata
;
5158 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5160 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5161 if (tevent_req_nomem(state
->param
, req
)) {
5162 return tevent_req_post(req
, ev
);
5164 SSVAL(state
->param
, 0, level
);
5165 state
->param
= trans2_bytes_push_str(
5166 state
->param
, smbXcli_conn_use_unicode(cli
->conn
), fname
, strlen(fname
)+1, NULL
);
5167 if (tevent_req_nomem(state
->param
, req
)) {
5168 return tevent_req_post(req
, ev
);
5171 subreq
= cli_trans_send(
5172 state
, /* mem ctx. */
5173 ev
, /* event ctx. */
5174 cli
, /* cli_state. */
5175 SMBtrans2
, /* cmd. */
5176 NULL
, /* pipe name. */
5180 state
->setup
, /* setup. */
5181 1, /* num setup uint16_t words. */
5182 0, /* max returned setup. */
5183 state
->param
, /* param. */
5184 talloc_get_size(state
->param
), /* num param. */
5185 2, /* max returned param. */
5188 max_rdata
); /* max returned data. */
5190 if (tevent_req_nomem(subreq
, req
)) {
5191 return tevent_req_post(req
, ev
);
5193 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5197 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5199 struct tevent_req
*req
= tevent_req_callback_data(
5200 subreq
, struct tevent_req
);
5201 struct cli_qpathinfo_state
*state
= tevent_req_data(
5202 req
, struct cli_qpathinfo_state
);
5205 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5207 &state
->rdata
, state
->min_rdata
,
5209 if (tevent_req_nterror(req
, status
)) {
5212 tevent_req_done(req
);
5215 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5216 uint8_t **rdata
, uint32_t *num_rdata
)
5218 struct cli_qpathinfo_state
*state
= tevent_req_data(
5219 req
, struct cli_qpathinfo_state
);
5222 if (tevent_req_is_nterror(req
, &status
)) {
5225 if (rdata
!= NULL
) {
5226 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5228 TALLOC_FREE(state
->rdata
);
5230 if (num_rdata
!= NULL
) {
5231 *num_rdata
= state
->num_rdata
;
5233 return NT_STATUS_OK
;
5236 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5237 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5239 uint8_t **rdata
, uint32_t *num_rdata
)
5241 TALLOC_CTX
*frame
= talloc_stackframe();
5242 struct tevent_context
*ev
;
5243 struct tevent_req
*req
;
5244 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5246 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5248 * Can't use sync call while an async call is in flight
5250 status
= NT_STATUS_INVALID_PARAMETER
;
5253 ev
= samba_tevent_context_init(frame
);
5257 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5262 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5265 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5271 struct cli_qfileinfo_state
{
5275 uint16_t recv_flags2
;
5281 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5283 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5284 struct tevent_context
*ev
,
5285 struct cli_state
*cli
, uint16_t fnum
,
5286 uint16_t level
, uint32_t min_rdata
,
5289 struct tevent_req
*req
, *subreq
;
5290 struct cli_qfileinfo_state
*state
;
5292 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5296 state
->min_rdata
= min_rdata
;
5297 SSVAL(state
->param
, 0, fnum
);
5298 SSVAL(state
->param
, 2, level
);
5299 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5301 subreq
= cli_trans_send(
5302 state
, /* mem ctx. */
5303 ev
, /* event ctx. */
5304 cli
, /* cli_state. */
5305 SMBtrans2
, /* cmd. */
5306 NULL
, /* pipe name. */
5310 state
->setup
, /* setup. */
5311 1, /* num setup uint16_t words. */
5312 0, /* max returned setup. */
5313 state
->param
, /* param. */
5314 sizeof(state
->param
), /* num param. */
5315 2, /* max returned param. */
5318 max_rdata
); /* max returned data. */
5320 if (tevent_req_nomem(subreq
, req
)) {
5321 return tevent_req_post(req
, ev
);
5323 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5327 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5329 struct tevent_req
*req
= tevent_req_callback_data(
5330 subreq
, struct tevent_req
);
5331 struct cli_qfileinfo_state
*state
= tevent_req_data(
5332 req
, struct cli_qfileinfo_state
);
5335 status
= cli_trans_recv(subreq
, state
,
5336 &state
->recv_flags2
,
5339 &state
->rdata
, state
->min_rdata
,
5341 if (tevent_req_nterror(req
, status
)) {
5344 tevent_req_done(req
);
5347 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5348 uint16_t *recv_flags2
,
5349 uint8_t **rdata
, uint32_t *num_rdata
)
5351 struct cli_qfileinfo_state
*state
= tevent_req_data(
5352 req
, struct cli_qfileinfo_state
);
5355 if (tevent_req_is_nterror(req
, &status
)) {
5359 if (recv_flags2
!= NULL
) {
5360 *recv_flags2
= state
->recv_flags2
;
5362 if (rdata
!= NULL
) {
5363 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5365 TALLOC_FREE(state
->rdata
);
5367 if (num_rdata
!= NULL
) {
5368 *num_rdata
= state
->num_rdata
;
5370 return NT_STATUS_OK
;
5373 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5374 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5375 uint32_t max_rdata
, uint16_t *recv_flags2
,
5376 uint8_t **rdata
, uint32_t *num_rdata
)
5378 TALLOC_CTX
*frame
= talloc_stackframe();
5379 struct tevent_context
*ev
;
5380 struct tevent_req
*req
;
5381 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5383 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5385 * Can't use sync call while an async call is in flight
5387 status
= NT_STATUS_INVALID_PARAMETER
;
5390 ev
= samba_tevent_context_init(frame
);
5394 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5399 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5402 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5408 struct cli_flush_state
{
5412 static void cli_flush_done(struct tevent_req
*subreq
);
5414 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5415 struct tevent_context
*ev
,
5416 struct cli_state
*cli
,
5419 struct tevent_req
*req
, *subreq
;
5420 struct cli_flush_state
*state
;
5422 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5426 SSVAL(state
->vwv
+ 0, 0, fnum
);
5428 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5430 if (tevent_req_nomem(subreq
, req
)) {
5431 return tevent_req_post(req
, ev
);
5433 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5437 static void cli_flush_done(struct tevent_req
*subreq
)
5439 struct tevent_req
*req
= tevent_req_callback_data(
5440 subreq
, struct tevent_req
);
5443 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5444 TALLOC_FREE(subreq
);
5445 if (tevent_req_nterror(req
, status
)) {
5448 tevent_req_done(req
);
5451 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5453 return tevent_req_simple_recv_ntstatus(req
);
5456 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5458 TALLOC_CTX
*frame
= talloc_stackframe();
5459 struct tevent_context
*ev
;
5460 struct tevent_req
*req
;
5461 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5463 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5465 * Can't use sync call while an async call is in flight
5467 status
= NT_STATUS_INVALID_PARAMETER
;
5470 ev
= samba_tevent_context_init(frame
);
5474 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5478 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5481 status
= cli_flush_recv(req
);
5487 struct cli_shadow_copy_data_state
{
5494 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5496 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5497 struct tevent_context
*ev
,
5498 struct cli_state
*cli
,
5502 struct tevent_req
*req
, *subreq
;
5503 struct cli_shadow_copy_data_state
*state
;
5506 req
= tevent_req_create(mem_ctx
, &state
,
5507 struct cli_shadow_copy_data_state
);
5511 state
->get_names
= get_names
;
5512 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5514 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5515 SSVAL(state
->setup
+ 2, 0, fnum
);
5516 SCVAL(state
->setup
+ 3, 0, 1); /* isFsctl */
5517 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5519 subreq
= cli_trans_send(
5520 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5521 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5524 if (tevent_req_nomem(subreq
, req
)) {
5525 return tevent_req_post(req
, ev
);
5527 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5531 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5533 struct tevent_req
*req
= tevent_req_callback_data(
5534 subreq
, struct tevent_req
);
5535 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5536 req
, struct cli_shadow_copy_data_state
);
5539 status
= cli_trans_recv(subreq
, state
, NULL
,
5540 NULL
, 0, NULL
, /* setup */
5541 NULL
, 0, NULL
, /* param */
5542 &state
->data
, 12, &state
->num_data
);
5543 TALLOC_FREE(subreq
);
5544 if (tevent_req_nterror(req
, status
)) {
5547 tevent_req_done(req
);
5550 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5551 char ***pnames
, int *pnum_names
)
5553 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5554 req
, struct cli_shadow_copy_data_state
);
5560 if (tevent_req_is_nterror(req
, &status
)) {
5563 num_names
= IVAL(state
->data
, 4);
5564 dlength
= IVAL(state
->data
, 8);
5566 if (!state
->get_names
) {
5567 *pnum_names
= num_names
;
5568 return NT_STATUS_OK
;
5571 if (dlength
+12 > state
->num_data
) {
5572 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5574 names
= talloc_array(mem_ctx
, char *, num_names
);
5575 if (names
== NULL
) {
5576 return NT_STATUS_NO_MEMORY
;
5579 for (i
=0; i
<num_names
; i
++) {
5582 size_t converted_size
;
5584 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5585 ret
= convert_string_talloc(
5586 names
, CH_UTF16LE
, CH_UNIX
,
5587 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5588 &names
[i
], &converted_size
);
5591 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5594 *pnum_names
= num_names
;
5596 return NT_STATUS_OK
;
5599 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5600 uint16_t fnum
, bool get_names
,
5601 char ***pnames
, int *pnum_names
)
5603 TALLOC_CTX
*frame
= talloc_stackframe();
5604 struct tevent_context
*ev
;
5605 struct tevent_req
*req
;
5606 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5608 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5610 * Can't use sync call while an async call is in flight
5612 status
= NT_STATUS_INVALID_PARAMETER
;
5615 ev
= samba_tevent_context_init(frame
);
5619 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5623 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5626 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);