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
, cli_ucs2(cli
), 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
= 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 event_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
, cli_ucs2(cli
), 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 event_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 event_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
= event_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 event_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)(cli_ucs2(cli
) ? 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 cli_ucs2(cli
) ? 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 event_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
= event_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 event_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 event_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
= event_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 event_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 event_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
= event_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 event_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 event_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
= event_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 event_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 event_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 event_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
= event_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 event_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 event_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
= event_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 event_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
, cli_ucs2(cli
), 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
, cli_ucs2(cli
), 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
= talloc_stackframe();
1101 struct event_context
*ev
;
1102 struct tevent_req
*req
;
1103 NTSTATUS status
= NT_STATUS_OK
;
1105 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1107 * Can't use sync call while an async call is in flight
1109 status
= NT_STATUS_INVALID_PARAMETER
;
1113 ev
= event_context_init(frame
);
1115 status
= NT_STATUS_NO_MEMORY
;
1119 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1121 status
= NT_STATUS_NO_MEMORY
;
1125 if (!tevent_req_poll(req
, ev
)) {
1126 status
= map_nt_error_from_unix(errno
);
1130 status
= cli_rename_recv(req
);
1137 /****************************************************************************
1139 ****************************************************************************/
1141 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1143 struct cli_ntrename_internal_state
{
1147 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1148 struct event_context
*ev
,
1149 struct cli_state
*cli
,
1150 const char *fname_src
,
1151 const char *fname_dst
,
1152 uint16_t rename_flag
)
1154 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1155 struct cli_ntrename_internal_state
*state
= NULL
;
1156 uint8_t additional_flags
= 0;
1157 uint8_t *bytes
= NULL
;
1159 req
= tevent_req_create(mem_ctx
, &state
,
1160 struct cli_ntrename_internal_state
);
1165 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1166 SSVAL(state
->vwv
+1, 0, rename_flag
);
1168 bytes
= talloc_array(state
, uint8_t, 1);
1169 if (tevent_req_nomem(bytes
, req
)) {
1170 return tevent_req_post(req
, ev
);
1173 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
1174 strlen(fname_src
)+1, NULL
);
1175 if (tevent_req_nomem(bytes
, req
)) {
1176 return tevent_req_post(req
, ev
);
1179 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1180 talloc_get_size(bytes
)+1);
1181 if (tevent_req_nomem(bytes
, req
)) {
1182 return tevent_req_post(req
, ev
);
1185 bytes
[talloc_get_size(bytes
)-1] = 4;
1186 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
1187 strlen(fname_dst
)+1, NULL
);
1188 if (tevent_req_nomem(bytes
, req
)) {
1189 return tevent_req_post(req
, ev
);
1192 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1193 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1194 if (tevent_req_nomem(subreq
, req
)) {
1195 return tevent_req_post(req
, ev
);
1197 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1201 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1203 struct tevent_req
*req
= tevent_req_callback_data(
1204 subreq
, struct tevent_req
);
1207 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1208 TALLOC_FREE(subreq
);
1209 if (tevent_req_nterror(req
, status
)) {
1212 tevent_req_done(req
);
1215 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1217 return tevent_req_simple_recv_ntstatus(req
);
1220 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1221 struct event_context
*ev
,
1222 struct cli_state
*cli
,
1223 const char *fname_src
,
1224 const char *fname_dst
)
1226 return cli_ntrename_internal_send(mem_ctx
,
1231 RENAME_FLAG_RENAME
);
1234 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1236 return cli_ntrename_internal_recv(req
);
1239 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1241 TALLOC_CTX
*frame
= talloc_stackframe();
1242 struct event_context
*ev
;
1243 struct tevent_req
*req
;
1244 NTSTATUS status
= NT_STATUS_OK
;
1246 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1248 * Can't use sync call while an async call is in flight
1250 status
= NT_STATUS_INVALID_PARAMETER
;
1254 ev
= event_context_init(frame
);
1256 status
= NT_STATUS_NO_MEMORY
;
1260 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1262 status
= NT_STATUS_NO_MEMORY
;
1266 if (!tevent_req_poll(req
, ev
)) {
1267 status
= map_nt_error_from_unix(errno
);
1271 status
= cli_ntrename_recv(req
);
1278 /****************************************************************************
1280 ****************************************************************************/
1282 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1283 struct event_context
*ev
,
1284 struct cli_state
*cli
,
1285 const char *fname_src
,
1286 const char *fname_dst
)
1288 return cli_ntrename_internal_send(mem_ctx
,
1293 RENAME_FLAG_HARD_LINK
);
1296 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1298 return cli_ntrename_internal_recv(req
);
1301 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1303 TALLOC_CTX
*frame
= talloc_stackframe();
1304 struct event_context
*ev
;
1305 struct tevent_req
*req
;
1306 NTSTATUS status
= NT_STATUS_OK
;
1308 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1310 * Can't use sync call while an async call is in flight
1312 status
= NT_STATUS_INVALID_PARAMETER
;
1316 ev
= event_context_init(frame
);
1318 status
= NT_STATUS_NO_MEMORY
;
1322 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1324 status
= NT_STATUS_NO_MEMORY
;
1328 if (!tevent_req_poll(req
, ev
)) {
1329 status
= map_nt_error_from_unix(errno
);
1333 status
= cli_nt_hardlink_recv(req
);
1340 /****************************************************************************
1342 ****************************************************************************/
1344 static void cli_unlink_done(struct tevent_req
*subreq
);
1346 struct cli_unlink_state
{
1350 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1351 struct event_context
*ev
,
1352 struct cli_state
*cli
,
1354 uint16_t mayhave_attrs
)
1356 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1357 struct cli_unlink_state
*state
= NULL
;
1358 uint8_t additional_flags
= 0;
1359 uint8_t *bytes
= NULL
;
1361 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1366 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1368 bytes
= talloc_array(state
, uint8_t, 1);
1369 if (tevent_req_nomem(bytes
, req
)) {
1370 return tevent_req_post(req
, ev
);
1373 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1374 strlen(fname
)+1, NULL
);
1376 if (tevent_req_nomem(bytes
, req
)) {
1377 return tevent_req_post(req
, ev
);
1380 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1381 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1382 if (tevent_req_nomem(subreq
, req
)) {
1383 return tevent_req_post(req
, ev
);
1385 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1389 static void cli_unlink_done(struct tevent_req
*subreq
)
1391 struct tevent_req
*req
= tevent_req_callback_data(
1392 subreq
, struct tevent_req
);
1395 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1396 TALLOC_FREE(subreq
);
1397 if (tevent_req_nterror(req
, status
)) {
1400 tevent_req_done(req
);
1403 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1405 return tevent_req_simple_recv_ntstatus(req
);
1408 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1410 TALLOC_CTX
*frame
= talloc_stackframe();
1411 struct event_context
*ev
;
1412 struct tevent_req
*req
;
1413 NTSTATUS status
= NT_STATUS_OK
;
1415 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1417 * Can't use sync call while an async call is in flight
1419 status
= NT_STATUS_INVALID_PARAMETER
;
1423 ev
= event_context_init(frame
);
1425 status
= NT_STATUS_NO_MEMORY
;
1429 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1431 status
= NT_STATUS_NO_MEMORY
;
1435 if (!tevent_req_poll(req
, ev
)) {
1436 status
= map_nt_error_from_unix(errno
);
1440 status
= cli_unlink_recv(req
);
1447 /****************************************************************************
1449 ****************************************************************************/
1451 static void cli_mkdir_done(struct tevent_req
*subreq
);
1453 struct cli_mkdir_state
{
1457 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1458 struct event_context
*ev
,
1459 struct cli_state
*cli
,
1462 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1463 struct cli_mkdir_state
*state
= NULL
;
1464 uint8_t additional_flags
= 0;
1465 uint8_t *bytes
= NULL
;
1467 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1472 bytes
= talloc_array(state
, uint8_t, 1);
1473 if (tevent_req_nomem(bytes
, req
)) {
1474 return tevent_req_post(req
, ev
);
1477 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1478 strlen(dname
)+1, NULL
);
1480 if (tevent_req_nomem(bytes
, req
)) {
1481 return tevent_req_post(req
, ev
);
1484 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1485 0, NULL
, talloc_get_size(bytes
), bytes
);
1486 if (tevent_req_nomem(subreq
, req
)) {
1487 return tevent_req_post(req
, ev
);
1489 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1493 static void cli_mkdir_done(struct tevent_req
*subreq
)
1495 struct tevent_req
*req
= tevent_req_callback_data(
1496 subreq
, struct tevent_req
);
1499 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1500 TALLOC_FREE(subreq
);
1501 if (tevent_req_nterror(req
, status
)) {
1504 tevent_req_done(req
);
1507 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1509 return tevent_req_simple_recv_ntstatus(req
);
1512 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1514 TALLOC_CTX
*frame
= talloc_stackframe();
1515 struct event_context
*ev
;
1516 struct tevent_req
*req
;
1517 NTSTATUS status
= NT_STATUS_OK
;
1519 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1521 * Can't use sync call while an async call is in flight
1523 status
= NT_STATUS_INVALID_PARAMETER
;
1527 ev
= event_context_init(frame
);
1529 status
= NT_STATUS_NO_MEMORY
;
1533 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1535 status
= NT_STATUS_NO_MEMORY
;
1539 if (!tevent_req_poll(req
, ev
)) {
1540 status
= map_nt_error_from_unix(errno
);
1544 status
= cli_mkdir_recv(req
);
1551 /****************************************************************************
1553 ****************************************************************************/
1555 static void cli_rmdir_done(struct tevent_req
*subreq
);
1557 struct cli_rmdir_state
{
1561 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1562 struct event_context
*ev
,
1563 struct cli_state
*cli
,
1566 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1567 struct cli_rmdir_state
*state
= NULL
;
1568 uint8_t additional_flags
= 0;
1569 uint8_t *bytes
= NULL
;
1571 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1576 bytes
= talloc_array(state
, uint8_t, 1);
1577 if (tevent_req_nomem(bytes
, req
)) {
1578 return tevent_req_post(req
, ev
);
1581 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1582 strlen(dname
)+1, NULL
);
1584 if (tevent_req_nomem(bytes
, req
)) {
1585 return tevent_req_post(req
, ev
);
1588 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1589 0, NULL
, talloc_get_size(bytes
), bytes
);
1590 if (tevent_req_nomem(subreq
, req
)) {
1591 return tevent_req_post(req
, ev
);
1593 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1597 static void cli_rmdir_done(struct tevent_req
*subreq
)
1599 struct tevent_req
*req
= tevent_req_callback_data(
1600 subreq
, struct tevent_req
);
1603 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1604 TALLOC_FREE(subreq
);
1605 if (tevent_req_nterror(req
, status
)) {
1608 tevent_req_done(req
);
1611 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1613 return tevent_req_simple_recv_ntstatus(req
);
1616 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1618 TALLOC_CTX
*frame
= talloc_stackframe();
1619 struct event_context
*ev
;
1620 struct tevent_req
*req
;
1621 NTSTATUS status
= NT_STATUS_OK
;
1623 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1625 * Can't use sync call while an async call is in flight
1627 status
= NT_STATUS_INVALID_PARAMETER
;
1631 ev
= event_context_init(frame
);
1633 status
= NT_STATUS_NO_MEMORY
;
1637 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1639 status
= NT_STATUS_NO_MEMORY
;
1643 if (!tevent_req_poll(req
, ev
)) {
1644 status
= map_nt_error_from_unix(errno
);
1648 status
= cli_rmdir_recv(req
);
1655 /****************************************************************************
1656 Set or clear the delete on close flag.
1657 ****************************************************************************/
1665 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1667 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1668 NULL
, 0, NULL
, NULL
, 0, NULL
);
1669 tevent_req_simple_finish_ntstatus(subreq
, status
);
1672 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1673 struct event_context
*ev
,
1674 struct cli_state
*cli
,
1678 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1679 struct doc_state
*state
= NULL
;
1681 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1686 /* Setup setup word. */
1687 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1689 /* Setup param array. */
1690 SSVAL(state
->param
,0,fnum
);
1691 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1693 /* Setup data array. */
1694 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1696 subreq
= cli_trans_send(state
, /* mem ctx. */
1697 ev
, /* event ctx. */
1698 cli
, /* cli_state. */
1699 SMBtrans2
, /* cmd. */
1700 NULL
, /* pipe name. */
1704 &state
->setup
, /* setup. */
1705 1, /* num setup uint16_t words. */
1706 0, /* max returned setup. */
1707 state
->param
, /* param. */
1709 2, /* max returned param. */
1710 state
->data
, /* data. */
1712 0); /* max returned data. */
1714 if (tevent_req_nomem(subreq
, req
)) {
1715 return tevent_req_post(req
, ev
);
1717 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1721 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1723 return tevent_req_simple_recv_ntstatus(req
);
1726 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1728 TALLOC_CTX
*frame
= talloc_stackframe();
1729 struct event_context
*ev
= NULL
;
1730 struct tevent_req
*req
= NULL
;
1731 NTSTATUS status
= NT_STATUS_OK
;
1733 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1735 * Can't use sync call while an async call is in flight
1737 status
= NT_STATUS_INVALID_PARAMETER
;
1741 ev
= event_context_init(frame
);
1743 status
= NT_STATUS_NO_MEMORY
;
1747 req
= cli_nt_delete_on_close_send(frame
,
1753 status
= NT_STATUS_NO_MEMORY
;
1757 if (!tevent_req_poll(req
, ev
)) {
1758 status
= map_nt_error_from_unix(errno
);
1762 status
= cli_nt_delete_on_close_recv(req
);
1769 struct cli_ntcreate_state
{
1774 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1776 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1777 struct event_context
*ev
,
1778 struct cli_state
*cli
,
1780 uint32_t CreatFlags
,
1781 uint32_t DesiredAccess
,
1782 uint32_t FileAttributes
,
1783 uint32_t ShareAccess
,
1784 uint32_t CreateDisposition
,
1785 uint32_t CreateOptions
,
1786 uint8_t SecurityFlags
)
1788 struct tevent_req
*req
, *subreq
;
1789 struct cli_ntcreate_state
*state
;
1792 size_t converted_len
;
1794 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1801 SCVAL(vwv
+0, 0, 0xFF);
1806 if (cli
->use_oplocks
) {
1807 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1809 SIVAL(vwv
+3, 1, CreatFlags
);
1810 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1811 SIVAL(vwv
+7, 1, DesiredAccess
);
1812 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1813 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1814 SIVAL(vwv
+13, 1, FileAttributes
);
1815 SIVAL(vwv
+15, 1, ShareAccess
);
1816 SIVAL(vwv
+17, 1, CreateDisposition
);
1817 SIVAL(vwv
+19, 1, CreateOptions
|
1818 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
1819 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1820 SCVAL(vwv
+23, 1, SecurityFlags
);
1822 bytes
= talloc_array(state
, uint8_t, 0);
1823 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
),
1824 fname
, strlen(fname
)+1,
1827 /* sigh. this copes with broken netapp filer behaviour */
1828 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, NULL
);
1830 if (tevent_req_nomem(bytes
, req
)) {
1831 return tevent_req_post(req
, ev
);
1834 SSVAL(vwv
+2, 1, converted_len
);
1836 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1837 talloc_get_size(bytes
), bytes
);
1838 if (tevent_req_nomem(subreq
, req
)) {
1839 return tevent_req_post(req
, ev
);
1841 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1845 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1847 struct tevent_req
*req
= tevent_req_callback_data(
1848 subreq
, struct tevent_req
);
1849 struct cli_ntcreate_state
*state
= tevent_req_data(
1850 req
, struct cli_ntcreate_state
);
1858 status
= cli_smb_recv(subreq
, state
, &inbuf
, 3, &wct
, &vwv
,
1859 &num_bytes
, &bytes
);
1860 TALLOC_FREE(subreq
);
1861 if (tevent_req_nterror(req
, status
)) {
1864 state
->fnum
= SVAL(vwv
+2, 1);
1865 tevent_req_done(req
);
1868 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1870 struct cli_ntcreate_state
*state
= tevent_req_data(
1871 req
, struct cli_ntcreate_state
);
1874 if (tevent_req_is_nterror(req
, &status
)) {
1877 *pfnum
= state
->fnum
;
1878 return NT_STATUS_OK
;
1881 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1883 uint32_t CreatFlags
,
1884 uint32_t DesiredAccess
,
1885 uint32_t FileAttributes
,
1886 uint32_t ShareAccess
,
1887 uint32_t CreateDisposition
,
1888 uint32_t CreateOptions
,
1889 uint8_t SecurityFlags
,
1892 TALLOC_CTX
*frame
= talloc_stackframe();
1893 struct event_context
*ev
;
1894 struct tevent_req
*req
;
1895 NTSTATUS status
= NT_STATUS_OK
;
1897 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1899 * Can't use sync call while an async call is in flight
1901 status
= NT_STATUS_INVALID_PARAMETER
;
1905 ev
= event_context_init(frame
);
1907 status
= NT_STATUS_NO_MEMORY
;
1911 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1912 DesiredAccess
, FileAttributes
, ShareAccess
,
1913 CreateDisposition
, CreateOptions
,
1916 status
= NT_STATUS_NO_MEMORY
;
1920 if (!tevent_req_poll(req
, ev
)) {
1921 status
= map_nt_error_from_unix(errno
);
1925 status
= cli_ntcreate_recv(req
, pfid
);
1931 struct cli_nttrans_create_state
{
1935 static void cli_nttrans_create_done(struct tevent_req
*subreq
);
1937 struct tevent_req
*cli_nttrans_create_send(TALLOC_CTX
*mem_ctx
,
1938 struct event_context
*ev
,
1939 struct cli_state
*cli
,
1941 uint32_t CreatFlags
,
1942 uint32_t DesiredAccess
,
1943 uint32_t FileAttributes
,
1944 uint32_t ShareAccess
,
1945 uint32_t CreateDisposition
,
1946 uint32_t CreateOptions
,
1947 uint8_t SecurityFlags
,
1948 struct security_descriptor
*secdesc
,
1949 struct ea_struct
*eas
,
1952 struct tevent_req
*req
, *subreq
;
1953 struct cli_nttrans_create_state
*state
;
1955 uint8_t *secdesc_buf
;
1958 size_t converted_len
;
1960 req
= tevent_req_create(mem_ctx
,
1961 &state
, struct cli_nttrans_create_state
);
1966 if (secdesc
!= NULL
) {
1967 status
= marshall_sec_desc(talloc_tos(), secdesc
,
1968 &secdesc_buf
, &secdesc_len
);
1969 if (tevent_req_nterror(req
, status
)) {
1970 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1971 nt_errstr(status
)));
1972 return tevent_req_post(req
, ev
);
1983 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
1984 return tevent_req_post(req
, ev
);
1987 param
= talloc_array(state
, uint8_t, 53);
1988 if (tevent_req_nomem(param
, req
)) {
1989 return tevent_req_post(req
, ev
);
1992 param
= trans2_bytes_push_str(param
, cli_ucs2(cli
),
1993 fname
, strlen(fname
),
1995 if (tevent_req_nomem(param
, req
)) {
1996 return tevent_req_post(req
, ev
);
1999 SIVAL(param
, 0, CreatFlags
);
2000 SIVAL(param
, 4, 0x0); /* RootDirectoryFid */
2001 SIVAL(param
, 8, DesiredAccess
);
2002 SIVAL(param
, 12, 0x0); /* AllocationSize */
2003 SIVAL(param
, 16, 0x0); /* AllocationSize */
2004 SIVAL(param
, 20, FileAttributes
);
2005 SIVAL(param
, 24, ShareAccess
);
2006 SIVAL(param
, 28, CreateDisposition
);
2007 SIVAL(param
, 32, CreateOptions
|
2008 (cli
->backup_intent
? FILE_OPEN_FOR_BACKUP_INTENT
: 0));
2009 SIVAL(param
, 36, secdesc_len
);
2010 SIVAL(param
, 40, 0); /* EA length*/
2011 SIVAL(param
, 44, converted_len
);
2012 SIVAL(param
, 48, 0x02); /* ImpersonationLevel */
2013 SCVAL(param
, 52, SecurityFlags
);
2015 subreq
= cli_trans_send(state
, ev
, cli
, SMBnttrans
,
2016 NULL
, -1, /* name, fid */
2017 NT_TRANSACT_CREATE
, 0,
2018 NULL
, 0, 0, /* setup */
2019 param
, talloc_get_size(param
), 128, /* param */
2020 secdesc_buf
, secdesc_len
, 0); /* data */
2021 if (tevent_req_nomem(subreq
, req
)) {
2022 return tevent_req_post(req
, ev
);
2024 tevent_req_set_callback(subreq
, cli_nttrans_create_done
, req
);
2028 static void cli_nttrans_create_done(struct tevent_req
*subreq
)
2030 struct tevent_req
*req
= tevent_req_callback_data(
2031 subreq
, struct tevent_req
);
2032 struct cli_nttrans_create_state
*state
= tevent_req_data(
2033 req
, struct cli_nttrans_create_state
);
2038 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
,
2039 NULL
, 0, NULL
, /* rsetup */
2040 ¶m
, 69, &num_param
,
2042 if (tevent_req_nterror(req
, status
)) {
2045 state
->fnum
= SVAL(param
, 2);
2047 tevent_req_done(req
);
2050 NTSTATUS
cli_nttrans_create_recv(struct tevent_req
*req
, uint16_t *fnum
)
2052 struct cli_nttrans_create_state
*state
= tevent_req_data(
2053 req
, struct cli_nttrans_create_state
);
2056 if (tevent_req_is_nterror(req
, &status
)) {
2059 *fnum
= state
->fnum
;
2060 return NT_STATUS_OK
;
2063 NTSTATUS
cli_nttrans_create(struct cli_state
*cli
,
2065 uint32_t CreatFlags
,
2066 uint32_t DesiredAccess
,
2067 uint32_t FileAttributes
,
2068 uint32_t ShareAccess
,
2069 uint32_t CreateDisposition
,
2070 uint32_t CreateOptions
,
2071 uint8_t SecurityFlags
,
2072 struct security_descriptor
*secdesc
,
2073 struct ea_struct
*eas
,
2077 TALLOC_CTX
*frame
= talloc_stackframe();
2078 struct event_context
*ev
;
2079 struct tevent_req
*req
;
2080 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
2082 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2084 * Can't use sync call while an async call is in flight
2086 status
= NT_STATUS_INVALID_PARAMETER
;
2089 ev
= event_context_init(frame
);
2093 req
= cli_nttrans_create_send(frame
, ev
, cli
, fname
, CreatFlags
,
2094 DesiredAccess
, FileAttributes
,
2095 ShareAccess
, CreateDisposition
,
2096 CreateOptions
, SecurityFlags
,
2097 secdesc
, eas
, num_eas
);
2101 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
2104 status
= cli_nttrans_create_recv(req
, pfid
);
2110 /****************************************************************************
2112 WARNING: if you open with O_WRONLY then getattrE won't work!
2113 ****************************************************************************/
2115 struct cli_openx_state
{
2122 static void cli_openx_done(struct tevent_req
*subreq
);
2124 struct tevent_req
*cli_openx_create(TALLOC_CTX
*mem_ctx
,
2125 struct event_context
*ev
,
2126 struct cli_state
*cli
, const char *fname
,
2127 int flags
, int share_mode
,
2128 struct tevent_req
**psmbreq
)
2130 struct tevent_req
*req
, *subreq
;
2131 struct cli_openx_state
*state
;
2133 unsigned accessmode
;
2134 uint8_t additional_flags
;
2137 req
= tevent_req_create(mem_ctx
, &state
, struct cli_openx_state
);
2143 if (flags
& O_CREAT
) {
2146 if (!(flags
& O_EXCL
)) {
2147 if (flags
& O_TRUNC
)
2153 accessmode
= (share_mode
<<4);
2155 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2157 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2162 if ((flags
& O_SYNC
) == O_SYNC
) {
2163 accessmode
|= (1<<14);
2167 if (share_mode
== DENY_FCB
) {
2171 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2172 SCVAL(state
->vwv
+ 0, 1, 0);
2173 SSVAL(state
->vwv
+ 1, 0, 0);
2174 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2175 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2176 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2177 SSVAL(state
->vwv
+ 5, 0, 0);
2178 SIVAL(state
->vwv
+ 6, 0, 0);
2179 SSVAL(state
->vwv
+ 8, 0, openfn
);
2180 SIVAL(state
->vwv
+ 9, 0, 0);
2181 SIVAL(state
->vwv
+ 11, 0, 0);
2182 SIVAL(state
->vwv
+ 13, 0, 0);
2184 additional_flags
= 0;
2186 if (cli
->use_oplocks
) {
2187 /* if using oplocks then ask for a batch oplock via
2188 core and extended methods */
2190 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2191 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2194 bytes
= talloc_array(state
, uint8_t, 0);
2195 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
2196 strlen(fname
)+1, NULL
);
2198 if (tevent_req_nomem(bytes
, req
)) {
2199 return tevent_req_post(req
, ev
);
2202 state
->bytes
.iov_base
= (void *)bytes
;
2203 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2205 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2206 15, state
->vwv
, 1, &state
->bytes
);
2207 if (subreq
== NULL
) {
2211 tevent_req_set_callback(subreq
, cli_openx_done
, req
);
2216 struct tevent_req
*cli_openx_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
2217 struct cli_state
*cli
, const char *fname
,
2218 int flags
, int share_mode
)
2220 struct tevent_req
*req
, *subreq
;
2223 req
= cli_openx_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2229 status
= cli_smb_req_send(subreq
);
2230 if (tevent_req_nterror(req
, status
)) {
2231 return tevent_req_post(req
, ev
);
2236 static void cli_openx_done(struct tevent_req
*subreq
)
2238 struct tevent_req
*req
= tevent_req_callback_data(
2239 subreq
, struct tevent_req
);
2240 struct cli_openx_state
*state
= tevent_req_data(
2241 req
, struct cli_openx_state
);
2247 status
= cli_smb_recv(subreq
, state
, &inbuf
, 3, &wct
, &vwv
, NULL
,
2249 TALLOC_FREE(subreq
);
2250 if (tevent_req_nterror(req
, status
)) {
2253 state
->fnum
= SVAL(vwv
+2, 0);
2254 tevent_req_done(req
);
2257 NTSTATUS
cli_openx_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2259 struct cli_openx_state
*state
= tevent_req_data(
2260 req
, struct cli_openx_state
);
2263 if (tevent_req_is_nterror(req
, &status
)) {
2266 *pfnum
= state
->fnum
;
2267 return NT_STATUS_OK
;
2270 NTSTATUS
cli_openx(struct cli_state
*cli
, const char *fname
, int flags
,
2271 int share_mode
, uint16_t *pfnum
)
2273 TALLOC_CTX
*frame
= talloc_stackframe();
2274 struct event_context
*ev
;
2275 struct tevent_req
*req
;
2276 NTSTATUS status
= NT_STATUS_OK
;
2278 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2280 * Can't use sync call while an async call is in flight
2282 status
= NT_STATUS_INVALID_PARAMETER
;
2286 ev
= event_context_init(frame
);
2288 status
= NT_STATUS_NO_MEMORY
;
2292 req
= cli_openx_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2294 status
= NT_STATUS_NO_MEMORY
;
2298 if (!tevent_req_poll(req
, ev
)) {
2299 status
= map_nt_error_from_unix(errno
);
2303 status
= cli_openx_recv(req
, pfnum
);
2308 /****************************************************************************
2309 Synchronous wrapper function that does an NtCreateX open by preference
2310 and falls back to openX if this fails.
2311 ****************************************************************************/
2313 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2314 int share_mode_in
, uint16_t *pfnum
)
2317 unsigned int openfn
= 0;
2318 unsigned int dos_deny
= 0;
2319 uint32_t access_mask
, share_mode
, create_disposition
, create_options
;
2321 /* Do the initial mapping into OpenX parameters. */
2322 if (flags
& O_CREAT
) {
2325 if (!(flags
& O_EXCL
)) {
2326 if (flags
& O_TRUNC
)
2332 dos_deny
= (share_mode_in
<<4);
2334 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2336 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2341 if ((flags
& O_SYNC
) == O_SYNC
) {
2342 dos_deny
|= (1<<14);
2346 if (share_mode_in
== DENY_FCB
) {
2351 /* Hmmm. This is what I think the above code
2352 should look like if it's using the constants
2355 if (flags
& O_CREAT
) {
2356 openfn
|= OPENX_FILE_CREATE_IF_NOT_EXIST
;
2358 if (!(flags
& O_EXCL
)) {
2359 if (flags
& O_TRUNC
)
2360 openfn
|= OPENX_FILE_EXISTS_TRUNCATE
;
2362 openfn
|= OPENX_FILE_EXISTS_OPEN
;
2365 dos_deny
= SET_DENY_MODE(share_mode_in
);
2367 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2368 dos_deny
|= DOS_OPEN_RDWR
;
2369 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2370 dos_deny
|= DOS_OPEN_WRONLY
;
2374 if ((flags
& O_SYNC
) == O_SYNC
) {
2375 dos_deny
|= FILE_SYNC_OPENMODE
;
2379 if (share_mode_in
== DENY_FCB
) {
2384 if (!map_open_params_to_ntcreate(fname
, dos_deny
,
2385 openfn
, &access_mask
,
2386 &share_mode
, &create_disposition
,
2387 &create_options
, NULL
)) {
2391 status
= cli_ntcreate(cli
,
2402 /* Try and cope will all varients of "we don't do this call"
2403 and fall back to openX. */
2405 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_IMPLEMENTED
) ||
2406 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_INFO_CLASS
) ||
2407 NT_STATUS_EQUAL(status
,NT_STATUS_PROCEDURE_NOT_FOUND
) ||
2408 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_LEVEL
) ||
2409 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_PARAMETER
) ||
2410 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_REQUEST
) ||
2411 NT_STATUS_EQUAL(status
,NT_STATUS_INVALID_DEVICE_STATE
) ||
2412 NT_STATUS_EQUAL(status
,NT_STATUS_CTL_FILE_NOT_SUPPORTED
) ||
2413 NT_STATUS_EQUAL(status
,NT_STATUS_UNSUCCESSFUL
)) {
2421 return cli_openx(cli
, fname
, flags
, share_mode_in
, pfnum
);
2424 /****************************************************************************
2426 ****************************************************************************/
2428 struct cli_close_state
{
2432 static void cli_close_done(struct tevent_req
*subreq
);
2434 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2435 struct event_context
*ev
,
2436 struct cli_state
*cli
,
2438 struct tevent_req
**psubreq
)
2440 struct tevent_req
*req
, *subreq
;
2441 struct cli_close_state
*state
;
2443 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2448 SSVAL(state
->vwv
+0, 0, fnum
);
2449 SIVALS(state
->vwv
+1, 0, -1);
2451 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2453 if (subreq
== NULL
) {
2457 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2462 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2463 struct event_context
*ev
,
2464 struct cli_state
*cli
,
2467 struct tevent_req
*req
, *subreq
;
2470 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2475 status
= cli_smb_req_send(subreq
);
2476 if (tevent_req_nterror(req
, status
)) {
2477 return tevent_req_post(req
, ev
);
2482 static void cli_close_done(struct tevent_req
*subreq
)
2484 struct tevent_req
*req
= tevent_req_callback_data(
2485 subreq
, struct tevent_req
);
2488 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2489 TALLOC_FREE(subreq
);
2490 if (tevent_req_nterror(req
, status
)) {
2493 tevent_req_done(req
);
2496 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2498 return tevent_req_simple_recv_ntstatus(req
);
2501 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2503 TALLOC_CTX
*frame
= talloc_stackframe();
2504 struct event_context
*ev
;
2505 struct tevent_req
*req
;
2506 NTSTATUS status
= NT_STATUS_OK
;
2508 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2510 * Can't use sync call while an async call is in flight
2512 status
= NT_STATUS_INVALID_PARAMETER
;
2516 ev
= event_context_init(frame
);
2518 status
= NT_STATUS_NO_MEMORY
;
2522 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2524 status
= NT_STATUS_NO_MEMORY
;
2528 if (!tevent_req_poll(req
, ev
)) {
2529 status
= map_nt_error_from_unix(errno
);
2533 status
= cli_close_recv(req
);
2539 /****************************************************************************
2540 Truncate a file to a specified size
2541 ****************************************************************************/
2543 struct ftrunc_state
{
2549 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2551 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2552 NULL
, 0, NULL
, NULL
, 0, NULL
);
2553 tevent_req_simple_finish_ntstatus(subreq
, status
);
2556 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2557 struct event_context
*ev
,
2558 struct cli_state
*cli
,
2562 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2563 struct ftrunc_state
*state
= NULL
;
2565 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2570 /* Setup setup word. */
2571 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2573 /* Setup param array. */
2574 SSVAL(state
->param
,0,fnum
);
2575 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2576 SSVAL(state
->param
,4,0);
2578 /* Setup data array. */
2579 SBVAL(state
->data
, 0, size
);
2581 subreq
= cli_trans_send(state
, /* mem ctx. */
2582 ev
, /* event ctx. */
2583 cli
, /* cli_state. */
2584 SMBtrans2
, /* cmd. */
2585 NULL
, /* pipe name. */
2589 &state
->setup
, /* setup. */
2590 1, /* num setup uint16_t words. */
2591 0, /* max returned setup. */
2592 state
->param
, /* param. */
2594 2, /* max returned param. */
2595 state
->data
, /* data. */
2597 0); /* max returned data. */
2599 if (tevent_req_nomem(subreq
, req
)) {
2600 return tevent_req_post(req
, ev
);
2602 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2606 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2608 return tevent_req_simple_recv_ntstatus(req
);
2611 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2613 TALLOC_CTX
*frame
= talloc_stackframe();
2614 struct event_context
*ev
= NULL
;
2615 struct tevent_req
*req
= NULL
;
2616 NTSTATUS status
= NT_STATUS_OK
;
2618 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2620 * Can't use sync call while an async call is in flight
2622 status
= NT_STATUS_INVALID_PARAMETER
;
2626 ev
= event_context_init(frame
);
2628 status
= NT_STATUS_NO_MEMORY
;
2632 req
= cli_ftruncate_send(frame
,
2638 status
= NT_STATUS_NO_MEMORY
;
2642 if (!tevent_req_poll(req
, ev
)) {
2643 status
= map_nt_error_from_unix(errno
);
2647 status
= cli_ftruncate_recv(req
);
2654 /****************************************************************************
2655 send a lock with a specified locktype
2656 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2657 ****************************************************************************/
2659 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2660 uint32_t offset
, uint32_t len
,
2661 int timeout
, unsigned char locktype
)
2666 unsigned int set_timeout
= 0;
2667 unsigned int saved_timeout
= 0;
2669 SCVAL(vwv
+ 0, 0, 0xff);
2670 SCVAL(vwv
+ 0, 1, 0);
2671 SSVAL(vwv
+ 1, 0, 0);
2672 SSVAL(vwv
+ 2, 0, fnum
);
2673 SCVAL(vwv
+ 3, 0, locktype
);
2674 SCVAL(vwv
+ 3, 1, 0);
2675 SIVALS(vwv
+ 4, 0, timeout
);
2676 SSVAL(vwv
+ 6, 0, 0);
2677 SSVAL(vwv
+ 7, 0, 1);
2679 SSVAL(bytes
, 0, cli_getpid(cli
));
2680 SIVAL(bytes
, 2, offset
);
2681 SIVAL(bytes
, 6, len
);
2684 if (timeout
== -1) {
2685 set_timeout
= 0x7FFFFFFF;
2687 set_timeout
= timeout
+ 2*1000;
2689 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2692 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2693 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2695 if (saved_timeout
!= 0) {
2696 cli_set_timeout(cli
, saved_timeout
);
2702 /****************************************************************************
2704 note that timeout is in units of 2 milliseconds
2705 ****************************************************************************/
2707 NTSTATUS
cli_lock32(struct cli_state
*cli
, uint16_t fnum
,
2708 uint32_t offset
, uint32_t len
, int timeout
,
2709 enum brl_type lock_type
)
2713 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2714 (lock_type
== READ_LOCK
? 1 : 0));
2718 /****************************************************************************
2720 ****************************************************************************/
2722 struct cli_unlock_state
{
2727 static void cli_unlock_done(struct tevent_req
*subreq
);
2729 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2730 struct event_context
*ev
,
2731 struct cli_state
*cli
,
2737 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2738 struct cli_unlock_state
*state
= NULL
;
2739 uint8_t additional_flags
= 0;
2741 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2746 SCVAL(state
->vwv
+0, 0, 0xFF);
2747 SSVAL(state
->vwv
+2, 0, fnum
);
2748 SCVAL(state
->vwv
+3, 0, 0);
2749 SIVALS(state
->vwv
+4, 0, 0);
2750 SSVAL(state
->vwv
+6, 0, 1);
2751 SSVAL(state
->vwv
+7, 0, 0);
2753 SSVAL(state
->data
, 0, cli_getpid(cli
));
2754 SIVAL(state
->data
, 2, offset
);
2755 SIVAL(state
->data
, 6, len
);
2757 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2758 8, state
->vwv
, 10, state
->data
);
2759 if (tevent_req_nomem(subreq
, req
)) {
2760 return tevent_req_post(req
, ev
);
2762 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2766 static void cli_unlock_done(struct tevent_req
*subreq
)
2768 struct tevent_req
*req
= tevent_req_callback_data(
2769 subreq
, struct tevent_req
);
2772 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2773 TALLOC_FREE(subreq
);
2774 if (tevent_req_nterror(req
, status
)) {
2777 tevent_req_done(req
);
2780 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2782 return tevent_req_simple_recv_ntstatus(req
);
2785 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2790 TALLOC_CTX
*frame
= talloc_stackframe();
2791 struct event_context
*ev
;
2792 struct tevent_req
*req
;
2793 NTSTATUS status
= NT_STATUS_OK
;
2795 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2797 * Can't use sync call while an async call is in flight
2799 status
= NT_STATUS_INVALID_PARAMETER
;
2803 ev
= event_context_init(frame
);
2805 status
= NT_STATUS_NO_MEMORY
;
2809 req
= cli_unlock_send(frame
, ev
, cli
,
2812 status
= NT_STATUS_NO_MEMORY
;
2816 if (!tevent_req_poll(req
, ev
)) {
2817 status
= map_nt_error_from_unix(errno
);
2821 status
= cli_unlock_recv(req
);
2828 /****************************************************************************
2829 Lock a file with 64 bit offsets.
2830 ****************************************************************************/
2832 NTSTATUS
cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2833 uint64_t offset
, uint64_t len
, int timeout
,
2834 enum brl_type lock_type
)
2838 unsigned int set_timeout
= 0;
2839 unsigned int saved_timeout
= 0;
2843 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2844 return cli_lock32(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2847 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2848 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2850 SCVAL(vwv
+ 0, 0, 0xff);
2851 SCVAL(vwv
+ 0, 1, 0);
2852 SSVAL(vwv
+ 1, 0, 0);
2853 SSVAL(vwv
+ 2, 0, fnum
);
2854 SCVAL(vwv
+ 3, 0, ltype
);
2855 SCVAL(vwv
+ 3, 1, 0);
2856 SIVALS(vwv
+ 4, 0, timeout
);
2857 SSVAL(vwv
+ 6, 0, 0);
2858 SSVAL(vwv
+ 7, 0, 1);
2860 SIVAL(bytes
, 0, cli_getpid(cli
));
2861 SOFF_T_R(bytes
, 4, offset
);
2862 SOFF_T_R(bytes
, 12, len
);
2865 if (timeout
== -1) {
2866 set_timeout
= 0x7FFFFFFF;
2868 set_timeout
= timeout
+ 2*1000;
2870 saved_timeout
= cli_set_timeout(cli
, set_timeout
);
2873 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2874 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2876 if (saved_timeout
!= 0) {
2877 cli_set_timeout(cli
, saved_timeout
);
2883 /****************************************************************************
2884 Unlock a file with 64 bit offsets.
2885 ****************************************************************************/
2887 struct cli_unlock64_state
{
2892 static void cli_unlock64_done(struct tevent_req
*subreq
);
2894 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2895 struct event_context
*ev
,
2896 struct cli_state
*cli
,
2902 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2903 struct cli_unlock64_state
*state
= NULL
;
2904 uint8_t additional_flags
= 0;
2906 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2911 SCVAL(state
->vwv
+0, 0, 0xff);
2912 SSVAL(state
->vwv
+2, 0, fnum
);
2913 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2914 SIVALS(state
->vwv
+4, 0, 0);
2915 SSVAL(state
->vwv
+6, 0, 1);
2916 SSVAL(state
->vwv
+7, 0, 0);
2918 SIVAL(state
->data
, 0, cli_getpid(cli
));
2919 SOFF_T_R(state
->data
, 4, offset
);
2920 SOFF_T_R(state
->data
, 12, len
);
2922 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2923 8, state
->vwv
, 20, state
->data
);
2924 if (tevent_req_nomem(subreq
, req
)) {
2925 return tevent_req_post(req
, ev
);
2927 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2931 static void cli_unlock64_done(struct tevent_req
*subreq
)
2933 struct tevent_req
*req
= tevent_req_callback_data(
2934 subreq
, struct tevent_req
);
2937 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2938 TALLOC_FREE(subreq
);
2939 if (tevent_req_nterror(req
, status
)) {
2942 tevent_req_done(req
);
2945 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2947 return tevent_req_simple_recv_ntstatus(req
);
2950 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2955 TALLOC_CTX
*frame
= talloc_stackframe();
2956 struct event_context
*ev
;
2957 struct tevent_req
*req
;
2958 NTSTATUS status
= NT_STATUS_OK
;
2960 if (! (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
)) {
2961 return cli_unlock(cli
, fnum
, offset
, len
);
2964 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
2966 * Can't use sync call while an async call is in flight
2968 status
= NT_STATUS_INVALID_PARAMETER
;
2972 ev
= event_context_init(frame
);
2974 status
= NT_STATUS_NO_MEMORY
;
2978 req
= cli_unlock64_send(frame
, ev
, cli
,
2981 status
= NT_STATUS_NO_MEMORY
;
2985 if (!tevent_req_poll(req
, ev
)) {
2986 status
= map_nt_error_from_unix(errno
);
2990 status
= cli_unlock64_recv(req
);
2997 /****************************************************************************
2998 Get/unlock a POSIX lock on a file - internal function.
2999 ****************************************************************************/
3001 struct posix_lock_state
{
3004 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3007 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3009 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
3010 NULL
, 0, NULL
, NULL
, 0, NULL
);
3011 tevent_req_simple_finish_ntstatus(subreq
, status
);
3014 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3015 struct event_context
*ev
,
3016 struct cli_state
*cli
,
3021 enum brl_type lock_type
)
3023 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3024 struct posix_lock_state
*state
= NULL
;
3026 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3031 /* Setup setup word. */
3032 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3034 /* Setup param array. */
3035 SSVAL(&state
->param
, 0, fnum
);
3036 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3038 /* Setup data array. */
3039 switch (lock_type
) {
3041 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3042 POSIX_LOCK_TYPE_READ
);
3045 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3046 POSIX_LOCK_TYPE_WRITE
);
3049 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3050 POSIX_LOCK_TYPE_UNLOCK
);
3057 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3058 POSIX_LOCK_FLAG_WAIT
);
3060 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3061 POSIX_LOCK_FLAG_NOWAIT
);
3064 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli_getpid(cli
));
3065 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3066 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3068 subreq
= cli_trans_send(state
, /* mem ctx. */
3069 ev
, /* event ctx. */
3070 cli
, /* cli_state. */
3071 SMBtrans2
, /* cmd. */
3072 NULL
, /* pipe name. */
3076 &state
->setup
, /* setup. */
3077 1, /* num setup uint16_t words. */
3078 0, /* max returned setup. */
3079 state
->param
, /* param. */
3081 2, /* max returned param. */
3082 state
->data
, /* data. */
3083 POSIX_LOCK_DATA_SIZE
, /* num data. */
3084 0); /* max returned data. */
3086 if (tevent_req_nomem(subreq
, req
)) {
3087 return tevent_req_post(req
, ev
);
3089 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3093 /****************************************************************************
3095 ****************************************************************************/
3097 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3098 struct event_context
*ev
,
3099 struct cli_state
*cli
,
3104 enum brl_type lock_type
)
3106 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3107 wait_lock
, lock_type
);
3110 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3112 return tevent_req_simple_recv_ntstatus(req
);
3115 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3116 uint64_t offset
, uint64_t len
,
3117 bool wait_lock
, enum brl_type lock_type
)
3119 TALLOC_CTX
*frame
= talloc_stackframe();
3120 struct event_context
*ev
= NULL
;
3121 struct tevent_req
*req
= NULL
;
3122 NTSTATUS status
= NT_STATUS_OK
;
3124 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3126 * Can't use sync call while an async call is in flight
3128 status
= NT_STATUS_INVALID_PARAMETER
;
3132 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3133 status
= NT_STATUS_INVALID_PARAMETER
;
3137 ev
= event_context_init(frame
);
3139 status
= NT_STATUS_NO_MEMORY
;
3143 req
= cli_posix_lock_send(frame
,
3152 status
= NT_STATUS_NO_MEMORY
;
3156 if (!tevent_req_poll(req
, ev
)) {
3157 status
= map_nt_error_from_unix(errno
);
3161 status
= cli_posix_lock_recv(req
);
3168 /****************************************************************************
3169 POSIX Unlock a file.
3170 ****************************************************************************/
3172 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3173 struct event_context
*ev
,
3174 struct cli_state
*cli
,
3179 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3180 false, UNLOCK_LOCK
);
3183 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3185 return tevent_req_simple_recv_ntstatus(req
);
3188 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3190 TALLOC_CTX
*frame
= talloc_stackframe();
3191 struct event_context
*ev
= NULL
;
3192 struct tevent_req
*req
= NULL
;
3193 NTSTATUS status
= NT_STATUS_OK
;
3195 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3197 * Can't use sync call while an async call is in flight
3199 status
= NT_STATUS_INVALID_PARAMETER
;
3203 ev
= event_context_init(frame
);
3205 status
= NT_STATUS_NO_MEMORY
;
3209 req
= cli_posix_unlock_send(frame
,
3216 status
= NT_STATUS_NO_MEMORY
;
3220 if (!tevent_req_poll(req
, ev
)) {
3221 status
= map_nt_error_from_unix(errno
);
3225 status
= cli_posix_unlock_recv(req
);
3232 /****************************************************************************
3233 Do a SMBgetattrE call.
3234 ****************************************************************************/
3236 static void cli_getattrE_done(struct tevent_req
*subreq
);
3238 struct cli_getattrE_state
{
3248 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3249 struct event_context
*ev
,
3250 struct cli_state
*cli
,
3253 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3254 struct cli_getattrE_state
*state
= NULL
;
3255 uint8_t additional_flags
= 0;
3257 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3262 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3263 SSVAL(state
->vwv
+0,0,fnum
);
3265 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3266 1, state
->vwv
, 0, NULL
);
3267 if (tevent_req_nomem(subreq
, req
)) {
3268 return tevent_req_post(req
, ev
);
3270 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3274 static void cli_getattrE_done(struct tevent_req
*subreq
)
3276 struct tevent_req
*req
= tevent_req_callback_data(
3277 subreq
, struct tevent_req
);
3278 struct cli_getattrE_state
*state
= tevent_req_data(
3279 req
, struct cli_getattrE_state
);
3281 uint16_t *vwv
= NULL
;
3285 status
= cli_smb_recv(subreq
, state
, &inbuf
, 11, &wct
, &vwv
,
3287 TALLOC_FREE(subreq
);
3288 if (tevent_req_nterror(req
, status
)) {
3292 state
->size
= (off_t
)IVAL(vwv
+6,0);
3293 state
->attr
= SVAL(vwv
+10,0);
3294 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3295 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3296 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3298 tevent_req_done(req
);
3301 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3304 time_t *change_time
,
3305 time_t *access_time
,
3308 struct cli_getattrE_state
*state
= tevent_req_data(
3309 req
, struct cli_getattrE_state
);
3312 if (tevent_req_is_nterror(req
, &status
)) {
3316 *attr
= state
->attr
;
3319 *size
= state
->size
;
3322 *change_time
= state
->change_time
;
3325 *access_time
= state
->access_time
;
3328 *write_time
= state
->write_time
;
3330 return NT_STATUS_OK
;
3333 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3337 time_t *change_time
,
3338 time_t *access_time
,
3341 TALLOC_CTX
*frame
= talloc_stackframe();
3342 struct event_context
*ev
= NULL
;
3343 struct tevent_req
*req
= NULL
;
3344 NTSTATUS status
= NT_STATUS_OK
;
3346 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3348 * Can't use sync call while an async call is in flight
3350 status
= NT_STATUS_INVALID_PARAMETER
;
3354 ev
= event_context_init(frame
);
3356 status
= NT_STATUS_NO_MEMORY
;
3360 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3362 status
= NT_STATUS_NO_MEMORY
;
3366 if (!tevent_req_poll(req
, ev
)) {
3367 status
= map_nt_error_from_unix(errno
);
3371 status
= cli_getattrE_recv(req
,
3383 /****************************************************************************
3385 ****************************************************************************/
3387 static void cli_getatr_done(struct tevent_req
*subreq
);
3389 struct cli_getatr_state
{
3396 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3397 struct event_context
*ev
,
3398 struct cli_state
*cli
,
3401 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3402 struct cli_getatr_state
*state
= NULL
;
3403 uint8_t additional_flags
= 0;
3404 uint8_t *bytes
= NULL
;
3406 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3411 state
->zone_offset
= smb1cli_conn_server_time_zone(cli
->conn
);
3413 bytes
= talloc_array(state
, uint8_t, 1);
3414 if (tevent_req_nomem(bytes
, req
)) {
3415 return tevent_req_post(req
, ev
);
3418 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3419 strlen(fname
)+1, NULL
);
3421 if (tevent_req_nomem(bytes
, req
)) {
3422 return tevent_req_post(req
, ev
);
3425 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3426 0, NULL
, talloc_get_size(bytes
), bytes
);
3427 if (tevent_req_nomem(subreq
, req
)) {
3428 return tevent_req_post(req
, ev
);
3430 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3434 static void cli_getatr_done(struct tevent_req
*subreq
)
3436 struct tevent_req
*req
= tevent_req_callback_data(
3437 subreq
, struct tevent_req
);
3438 struct cli_getatr_state
*state
= tevent_req_data(
3439 req
, struct cli_getatr_state
);
3441 uint16_t *vwv
= NULL
;
3445 status
= cli_smb_recv(subreq
, state
, &inbuf
, 4, &wct
, &vwv
, NULL
,
3447 TALLOC_FREE(subreq
);
3448 if (tevent_req_nterror(req
, status
)) {
3452 state
->attr
= SVAL(vwv
+0,0);
3453 state
->size
= (off_t
)IVAL(vwv
+3,0);
3454 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3456 tevent_req_done(req
);
3459 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3464 struct cli_getatr_state
*state
= tevent_req_data(
3465 req
, struct cli_getatr_state
);
3468 if (tevent_req_is_nterror(req
, &status
)) {
3472 *attr
= state
->attr
;
3475 *size
= state
->size
;
3478 *write_time
= state
->write_time
;
3480 return NT_STATUS_OK
;
3483 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3489 TALLOC_CTX
*frame
= talloc_stackframe();
3490 struct event_context
*ev
= NULL
;
3491 struct tevent_req
*req
= NULL
;
3492 NTSTATUS status
= NT_STATUS_OK
;
3494 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3496 * Can't use sync call while an async call is in flight
3498 status
= NT_STATUS_INVALID_PARAMETER
;
3502 ev
= event_context_init(frame
);
3504 status
= NT_STATUS_NO_MEMORY
;
3508 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3510 status
= NT_STATUS_NO_MEMORY
;
3514 if (!tevent_req_poll(req
, ev
)) {
3515 status
= map_nt_error_from_unix(errno
);
3519 status
= cli_getatr_recv(req
,
3529 /****************************************************************************
3530 Do a SMBsetattrE call.
3531 ****************************************************************************/
3533 static void cli_setattrE_done(struct tevent_req
*subreq
);
3535 struct cli_setattrE_state
{
3539 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3540 struct event_context
*ev
,
3541 struct cli_state
*cli
,
3547 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3548 struct cli_setattrE_state
*state
= NULL
;
3549 uint8_t additional_flags
= 0;
3551 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3556 SSVAL(state
->vwv
+0, 0, fnum
);
3557 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3558 smb1cli_conn_server_time_zone(cli
->conn
));
3559 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3560 smb1cli_conn_server_time_zone(cli
->conn
));
3561 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3562 smb1cli_conn_server_time_zone(cli
->conn
));
3564 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3565 7, state
->vwv
, 0, NULL
);
3566 if (tevent_req_nomem(subreq
, req
)) {
3567 return tevent_req_post(req
, ev
);
3569 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3573 static void cli_setattrE_done(struct tevent_req
*subreq
)
3575 struct tevent_req
*req
= tevent_req_callback_data(
3576 subreq
, struct tevent_req
);
3579 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3580 TALLOC_FREE(subreq
);
3581 if (tevent_req_nterror(req
, status
)) {
3584 tevent_req_done(req
);
3587 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3589 return tevent_req_simple_recv_ntstatus(req
);
3592 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3598 TALLOC_CTX
*frame
= talloc_stackframe();
3599 struct event_context
*ev
= NULL
;
3600 struct tevent_req
*req
= NULL
;
3601 NTSTATUS status
= NT_STATUS_OK
;
3603 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3605 * Can't use sync call while an async call is in flight
3607 status
= NT_STATUS_INVALID_PARAMETER
;
3611 ev
= event_context_init(frame
);
3613 status
= NT_STATUS_NO_MEMORY
;
3617 req
= cli_setattrE_send(frame
, ev
,
3625 status
= NT_STATUS_NO_MEMORY
;
3629 if (!tevent_req_poll(req
, ev
)) {
3630 status
= map_nt_error_from_unix(errno
);
3634 status
= cli_setattrE_recv(req
);
3641 /****************************************************************************
3642 Do a SMBsetatr call.
3643 ****************************************************************************/
3645 static void cli_setatr_done(struct tevent_req
*subreq
);
3647 struct cli_setatr_state
{
3651 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3652 struct event_context
*ev
,
3653 struct cli_state
*cli
,
3658 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3659 struct cli_setatr_state
*state
= NULL
;
3660 uint8_t additional_flags
= 0;
3661 uint8_t *bytes
= NULL
;
3663 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3668 SSVAL(state
->vwv
+0, 0, attr
);
3669 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, smb1cli_conn_server_time_zone(cli
->conn
));
3671 bytes
= talloc_array(state
, uint8_t, 1);
3672 if (tevent_req_nomem(bytes
, req
)) {
3673 return tevent_req_post(req
, ev
);
3676 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3677 strlen(fname
)+1, NULL
);
3678 if (tevent_req_nomem(bytes
, req
)) {
3679 return tevent_req_post(req
, ev
);
3681 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3682 talloc_get_size(bytes
)+1);
3683 if (tevent_req_nomem(bytes
, req
)) {
3684 return tevent_req_post(req
, ev
);
3687 bytes
[talloc_get_size(bytes
)-1] = 4;
3688 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "",
3690 if (tevent_req_nomem(bytes
, req
)) {
3691 return tevent_req_post(req
, ev
);
3694 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3695 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3696 if (tevent_req_nomem(subreq
, req
)) {
3697 return tevent_req_post(req
, ev
);
3699 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3703 static void cli_setatr_done(struct tevent_req
*subreq
)
3705 struct tevent_req
*req
= tevent_req_callback_data(
3706 subreq
, struct tevent_req
);
3709 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3710 TALLOC_FREE(subreq
);
3711 if (tevent_req_nterror(req
, status
)) {
3714 tevent_req_done(req
);
3717 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3719 return tevent_req_simple_recv_ntstatus(req
);
3722 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3727 TALLOC_CTX
*frame
= talloc_stackframe();
3728 struct event_context
*ev
= NULL
;
3729 struct tevent_req
*req
= NULL
;
3730 NTSTATUS status
= NT_STATUS_OK
;
3732 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3734 * Can't use sync call while an async call is in flight
3736 status
= NT_STATUS_INVALID_PARAMETER
;
3740 ev
= event_context_init(frame
);
3742 status
= NT_STATUS_NO_MEMORY
;
3746 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3748 status
= NT_STATUS_NO_MEMORY
;
3752 if (!tevent_req_poll(req
, ev
)) {
3753 status
= map_nt_error_from_unix(errno
);
3757 status
= cli_setatr_recv(req
);
3764 /****************************************************************************
3765 Check for existance of a dir.
3766 ****************************************************************************/
3768 static void cli_chkpath_done(struct tevent_req
*subreq
);
3770 struct cli_chkpath_state
{
3774 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3775 struct event_context
*ev
,
3776 struct cli_state
*cli
,
3779 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3780 struct cli_chkpath_state
*state
= NULL
;
3781 uint8_t additional_flags
= 0;
3782 uint8_t *bytes
= NULL
;
3784 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3789 bytes
= talloc_array(state
, uint8_t, 1);
3790 if (tevent_req_nomem(bytes
, req
)) {
3791 return tevent_req_post(req
, ev
);
3794 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3795 strlen(fname
)+1, NULL
);
3797 if (tevent_req_nomem(bytes
, req
)) {
3798 return tevent_req_post(req
, ev
);
3801 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3802 0, NULL
, talloc_get_size(bytes
), bytes
);
3803 if (tevent_req_nomem(subreq
, req
)) {
3804 return tevent_req_post(req
, ev
);
3806 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3810 static void cli_chkpath_done(struct tevent_req
*subreq
)
3812 struct tevent_req
*req
= tevent_req_callback_data(
3813 subreq
, struct tevent_req
);
3816 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3817 TALLOC_FREE(subreq
);
3818 if (tevent_req_nterror(req
, status
)) {
3821 tevent_req_done(req
);
3824 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3826 return tevent_req_simple_recv_ntstatus(req
);
3829 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3831 TALLOC_CTX
*frame
= talloc_stackframe();
3832 struct event_context
*ev
= NULL
;
3833 struct tevent_req
*req
= NULL
;
3835 NTSTATUS status
= NT_STATUS_OK
;
3837 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3839 * Can't use sync call while an async call is in flight
3841 status
= NT_STATUS_INVALID_PARAMETER
;
3845 path2
= talloc_strdup(frame
, path
);
3847 status
= NT_STATUS_NO_MEMORY
;
3850 trim_char(path2
,'\0','\\');
3852 path2
= talloc_strdup(frame
, "\\");
3854 status
= NT_STATUS_NO_MEMORY
;
3859 ev
= event_context_init(frame
);
3861 status
= NT_STATUS_NO_MEMORY
;
3865 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3867 status
= NT_STATUS_NO_MEMORY
;
3871 if (!tevent_req_poll(req
, ev
)) {
3872 status
= map_nt_error_from_unix(errno
);
3876 status
= cli_chkpath_recv(req
);
3883 /****************************************************************************
3885 ****************************************************************************/
3887 static void cli_dskattr_done(struct tevent_req
*subreq
);
3889 struct cli_dskattr_state
{
3895 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3896 struct event_context
*ev
,
3897 struct cli_state
*cli
)
3899 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3900 struct cli_dskattr_state
*state
= NULL
;
3901 uint8_t additional_flags
= 0;
3903 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3908 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3910 if (tevent_req_nomem(subreq
, req
)) {
3911 return tevent_req_post(req
, ev
);
3913 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3917 static void cli_dskattr_done(struct tevent_req
*subreq
)
3919 struct tevent_req
*req
= tevent_req_callback_data(
3920 subreq
, struct tevent_req
);
3921 struct cli_dskattr_state
*state
= tevent_req_data(
3922 req
, struct cli_dskattr_state
);
3924 uint16_t *vwv
= NULL
;
3928 status
= cli_smb_recv(subreq
, state
, &inbuf
, 4, &wct
, &vwv
, NULL
,
3930 TALLOC_FREE(subreq
);
3931 if (tevent_req_nterror(req
, status
)) {
3934 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3935 state
->total
= SVAL(vwv
+0, 0);
3936 state
->avail
= SVAL(vwv
+3, 0);
3937 tevent_req_done(req
);
3940 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3942 struct cli_dskattr_state
*state
= tevent_req_data(
3943 req
, struct cli_dskattr_state
);
3946 if (tevent_req_is_nterror(req
, &status
)) {
3949 *bsize
= state
->bsize
;
3950 *total
= state
->total
;
3951 *avail
= state
->avail
;
3952 return NT_STATUS_OK
;
3955 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3957 TALLOC_CTX
*frame
= talloc_stackframe();
3958 struct event_context
*ev
= NULL
;
3959 struct tevent_req
*req
= NULL
;
3960 NTSTATUS status
= NT_STATUS_OK
;
3962 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
3964 * Can't use sync call while an async call is in flight
3966 status
= NT_STATUS_INVALID_PARAMETER
;
3970 ev
= event_context_init(frame
);
3972 status
= NT_STATUS_NO_MEMORY
;
3976 req
= cli_dskattr_send(frame
, ev
, cli
);
3978 status
= NT_STATUS_NO_MEMORY
;
3982 if (!tevent_req_poll(req
, ev
)) {
3983 status
= map_nt_error_from_unix(errno
);
3987 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
3994 /****************************************************************************
3995 Create and open a temporary file.
3996 ****************************************************************************/
3998 static void cli_ctemp_done(struct tevent_req
*subreq
);
4000 struct ctemp_state
{
4006 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4007 struct event_context
*ev
,
4008 struct cli_state
*cli
,
4011 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4012 struct ctemp_state
*state
= NULL
;
4013 uint8_t additional_flags
= 0;
4014 uint8_t *bytes
= NULL
;
4016 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4021 SSVAL(state
->vwv
,0,0);
4022 SIVALS(state
->vwv
+1,0,-1);
4024 bytes
= talloc_array(state
, uint8_t, 1);
4025 if (tevent_req_nomem(bytes
, req
)) {
4026 return tevent_req_post(req
, ev
);
4029 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), path
,
4030 strlen(path
)+1, NULL
);
4031 if (tevent_req_nomem(bytes
, req
)) {
4032 return tevent_req_post(req
, ev
);
4035 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4036 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4037 if (tevent_req_nomem(subreq
, req
)) {
4038 return tevent_req_post(req
, ev
);
4040 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4044 static void cli_ctemp_done(struct tevent_req
*subreq
)
4046 struct tevent_req
*req
= tevent_req_callback_data(
4047 subreq
, struct tevent_req
);
4048 struct ctemp_state
*state
= tevent_req_data(
4049 req
, struct ctemp_state
);
4053 uint32_t num_bytes
= 0;
4054 uint8_t *bytes
= NULL
;
4057 status
= cli_smb_recv(subreq
, state
, &inbuf
, 1, &wcnt
, &vwv
,
4058 &num_bytes
, &bytes
);
4059 TALLOC_FREE(subreq
);
4060 if (tevent_req_nterror(req
, status
)) {
4064 state
->fnum
= SVAL(vwv
+0, 0);
4066 /* From W2K3, the result is just the ASCII name */
4067 if (num_bytes
< 2) {
4068 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4072 if (pull_string_talloc(state
,
4079 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4082 tevent_req_done(req
);
4085 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4090 struct ctemp_state
*state
= tevent_req_data(req
,
4091 struct ctemp_state
);
4094 if (tevent_req_is_nterror(req
, &status
)) {
4097 *pfnum
= state
->fnum
;
4098 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4100 return NT_STATUS_NO_MEMORY
;
4102 return NT_STATUS_OK
;
4105 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4111 TALLOC_CTX
*frame
= talloc_stackframe();
4112 struct event_context
*ev
;
4113 struct tevent_req
*req
;
4114 NTSTATUS status
= NT_STATUS_OK
;
4116 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4118 * Can't use sync call while an async call is in flight
4120 status
= NT_STATUS_INVALID_PARAMETER
;
4124 ev
= event_context_init(frame
);
4126 status
= NT_STATUS_NO_MEMORY
;
4130 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4132 status
= NT_STATUS_NO_MEMORY
;
4136 if (!tevent_req_poll(req
, ev
)) {
4137 status
= map_nt_error_from_unix(errno
);
4141 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4149 send a raw ioctl - used by the torture code
4151 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4156 SSVAL(vwv
+0, 0, fnum
);
4157 SSVAL(vwv
+1, 0, code
>>16);
4158 SSVAL(vwv
+2, 0, (code
&0xFFFF));
4160 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
4161 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
4162 if (!NT_STATUS_IS_OK(status
)) {
4165 *blob
= data_blob_null
;
4166 return NT_STATUS_OK
;
4169 /*********************************************************
4170 Set an extended attribute utility fn.
4171 *********************************************************/
4173 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
4174 uint8_t *param
, unsigned int param_len
,
4175 const char *ea_name
,
4176 const char *ea_val
, size_t ea_len
)
4179 unsigned int data_len
= 0;
4180 uint8_t *data
= NULL
;
4182 size_t ea_namelen
= strlen(ea_name
);
4185 SSVAL(setup
, 0, setup_val
);
4187 if (ea_namelen
== 0 && ea_len
== 0) {
4189 data
= talloc_array(talloc_tos(),
4193 return NT_STATUS_NO_MEMORY
;
4196 SIVAL(p
,0,data_len
);
4198 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4199 data
= talloc_array(talloc_tos(),
4203 return NT_STATUS_NO_MEMORY
;
4206 SIVAL(p
,0,data_len
);
4208 SCVAL(p
, 0, 0); /* EA flags. */
4209 SCVAL(p
, 1, ea_namelen
);
4210 SSVAL(p
, 2, ea_len
);
4211 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4212 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4215 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
4217 param
, param_len
, 2,
4218 data
, data_len
, CLI_BUFFER_SIZE
,
4220 NULL
, 0, NULL
, /* rsetup */
4221 NULL
, 0, NULL
, /* rparam */
4222 NULL
, 0, NULL
); /* rdata */
4227 /*********************************************************
4228 Set an extended attribute on a pathname.
4229 *********************************************************/
4231 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
4232 const char *ea_name
, const char *ea_val
,
4235 unsigned int param_len
= 0;
4238 TALLOC_CTX
*frame
= talloc_stackframe();
4240 param
= talloc_array(talloc_tos(), uint8_t, 6);
4242 return NT_STATUS_NO_MEMORY
;
4244 SSVAL(param
,0,SMB_INFO_SET_EA
);
4248 param
= trans2_bytes_push_str(param
, cli_ucs2(cli
),
4249 path
, strlen(path
)+1,
4251 param_len
= talloc_get_size(param
);
4253 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
4254 ea_name
, ea_val
, ea_len
);
4259 /*********************************************************
4260 Set an extended attribute on an fnum.
4261 *********************************************************/
4263 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
4264 const char *ea_name
, const char *ea_val
,
4269 memset(param
, 0, 6);
4270 SSVAL(param
,0,fnum
);
4271 SSVAL(param
,2,SMB_INFO_SET_EA
);
4273 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
4274 ea_name
, ea_val
, ea_len
);
4277 /*********************************************************
4278 Get an extended attribute list utility fn.
4279 *********************************************************/
4281 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
4283 size_t *pnum_eas
, struct ea_struct
**pea_list
)
4285 struct ea_struct
*ea_list
= NULL
;
4290 if (rdata_len
< 4) {
4294 ea_size
= (size_t)IVAL(rdata
,0);
4295 if (ea_size
> rdata_len
) {
4300 /* No EA's present. */
4309 /* Validate the EA list and count it. */
4310 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4311 unsigned int ea_namelen
= CVAL(p
,1);
4312 unsigned int ea_valuelen
= SVAL(p
,2);
4313 if (ea_namelen
== 0) {
4316 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4319 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4320 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4329 *pnum_eas
= num_eas
;
4331 /* Caller only wants number of EA's. */
4335 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4340 ea_size
= (size_t)IVAL(rdata
,0);
4343 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4344 struct ea_struct
*ea
= &ea_list
[num_eas
];
4345 fstring unix_ea_name
;
4346 unsigned int ea_namelen
= CVAL(p
,1);
4347 unsigned int ea_valuelen
= SVAL(p
,2);
4349 ea
->flags
= CVAL(p
,0);
4350 unix_ea_name
[0] = '\0';
4351 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4352 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4356 /* Ensure the value is null terminated (in case it's a string). */
4357 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4358 if (!ea
->value
.data
) {
4362 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4364 ea
->value
.data
[ea_valuelen
] = 0;
4366 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4369 *pea_list
= ea_list
;
4373 TALLOC_FREE(ea_list
);
4377 /*********************************************************
4378 Get an extended attribute list from a pathname.
4379 *********************************************************/
4381 struct cli_get_ea_list_path_state
{
4386 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4388 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4389 struct tevent_context
*ev
,
4390 struct cli_state
*cli
,
4393 struct tevent_req
*req
, *subreq
;
4394 struct cli_get_ea_list_path_state
*state
;
4396 req
= tevent_req_create(mem_ctx
, &state
,
4397 struct cli_get_ea_list_path_state
);
4401 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4402 SMB_INFO_QUERY_ALL_EAS
, 4,
4404 if (tevent_req_nomem(subreq
, req
)) {
4405 return tevent_req_post(req
, ev
);
4407 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4411 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4413 struct tevent_req
*req
= tevent_req_callback_data(
4414 subreq
, struct tevent_req
);
4415 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4416 req
, struct cli_get_ea_list_path_state
);
4419 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4421 TALLOC_FREE(subreq
);
4422 if (tevent_req_nterror(req
, status
)) {
4425 tevent_req_done(req
);
4428 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4429 size_t *pnum_eas
, struct ea_struct
**peas
)
4431 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4432 req
, struct cli_get_ea_list_path_state
);
4435 if (tevent_req_is_nterror(req
, &status
)) {
4438 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4440 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4442 return NT_STATUS_OK
;
4445 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4448 struct ea_struct
**pea_list
)
4450 TALLOC_CTX
*frame
= talloc_stackframe();
4451 struct event_context
*ev
= NULL
;
4452 struct tevent_req
*req
= NULL
;
4453 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4455 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4457 * Can't use sync call while an async call is in flight
4459 status
= NT_STATUS_INVALID_PARAMETER
;
4462 ev
= event_context_init(frame
);
4466 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4470 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4473 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4479 /****************************************************************************
4480 Convert open "flags" arg to uint32_t on wire.
4481 ****************************************************************************/
4483 static uint32_t open_flags_to_wire(int flags
)
4485 int open_mode
= flags
& O_ACCMODE
;
4488 switch (open_mode
) {
4490 ret
|= SMB_O_WRONLY
;
4497 ret
|= SMB_O_RDONLY
;
4501 if (flags
& O_CREAT
) {
4504 if (flags
& O_EXCL
) {
4507 if (flags
& O_TRUNC
) {
4511 if (flags
& O_SYNC
) {
4515 if (flags
& O_APPEND
) {
4516 ret
|= SMB_O_APPEND
;
4518 #if defined(O_DIRECT)
4519 if (flags
& O_DIRECT
) {
4520 ret
|= SMB_O_DIRECT
;
4523 #if defined(O_DIRECTORY)
4524 if (flags
& O_DIRECTORY
) {
4525 ret
|= SMB_O_DIRECTORY
;
4531 /****************************************************************************
4532 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4533 ****************************************************************************/
4535 struct posix_open_state
{
4539 uint16_t fnum
; /* Out */
4542 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4544 struct tevent_req
*req
= tevent_req_callback_data(
4545 subreq
, struct tevent_req
);
4546 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4551 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4552 NULL
, 0, NULL
, &data
, 12, &num_data
);
4553 TALLOC_FREE(subreq
);
4554 if (tevent_req_nterror(req
, status
)) {
4557 state
->fnum
= SVAL(data
,2);
4558 tevent_req_done(req
);
4561 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4562 struct event_context
*ev
,
4563 struct cli_state
*cli
,
4569 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4570 struct posix_open_state
*state
= NULL
;
4571 uint32_t wire_flags
= open_flags_to_wire(flags
);
4573 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4578 /* Setup setup word. */
4579 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4581 /* Setup param array. */
4582 state
->param
= talloc_array(state
, uint8_t, 6);
4583 if (tevent_req_nomem(state
->param
, req
)) {
4584 return tevent_req_post(req
, ev
);
4586 memset(state
->param
, '\0', 6);
4587 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4589 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
4590 strlen(fname
)+1, NULL
);
4592 if (tevent_req_nomem(state
->param
, req
)) {
4593 return tevent_req_post(req
, ev
);
4596 /* Setup data words. */
4598 wire_flags
|= SMB_O_DIRECTORY
;
4601 SIVAL(state
->data
,0,0); /* No oplock. */
4602 SIVAL(state
->data
,4,wire_flags
);
4603 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4604 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4605 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4607 subreq
= cli_trans_send(state
, /* mem ctx. */
4608 ev
, /* event ctx. */
4609 cli
, /* cli_state. */
4610 SMBtrans2
, /* cmd. */
4611 NULL
, /* pipe name. */
4615 &state
->setup
, /* setup. */
4616 1, /* num setup uint16_t words. */
4617 0, /* max returned setup. */
4618 state
->param
, /* param. */
4619 talloc_get_size(state
->param
),/* num param. */
4620 2, /* max returned param. */
4621 state
->data
, /* data. */
4623 12); /* max returned data. */
4625 if (tevent_req_nomem(subreq
, req
)) {
4626 return tevent_req_post(req
, ev
);
4628 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4632 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4633 struct event_context
*ev
,
4634 struct cli_state
*cli
,
4639 return cli_posix_open_internal_send(mem_ctx
, ev
,
4640 cli
, fname
, flags
, mode
, false);
4643 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4645 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4648 if (tevent_req_is_nterror(req
, &status
)) {
4651 *pfnum
= state
->fnum
;
4652 return NT_STATUS_OK
;
4655 /****************************************************************************
4656 Open - POSIX semantics. Doesn't request oplock.
4657 ****************************************************************************/
4659 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4660 int flags
, mode_t mode
, uint16_t *pfnum
)
4663 TALLOC_CTX
*frame
= talloc_stackframe();
4664 struct event_context
*ev
= NULL
;
4665 struct tevent_req
*req
= NULL
;
4666 NTSTATUS status
= NT_STATUS_OK
;
4668 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4670 * Can't use sync call while an async call is in flight
4672 status
= NT_STATUS_INVALID_PARAMETER
;
4676 ev
= event_context_init(frame
);
4678 status
= NT_STATUS_NO_MEMORY
;
4682 req
= cli_posix_open_send(frame
,
4689 status
= NT_STATUS_NO_MEMORY
;
4693 if (!tevent_req_poll(req
, ev
)) {
4694 status
= map_nt_error_from_unix(errno
);
4698 status
= cli_posix_open_recv(req
, pfnum
);
4705 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4706 struct event_context
*ev
,
4707 struct cli_state
*cli
,
4711 return cli_posix_open_internal_send(mem_ctx
, ev
,
4712 cli
, fname
, O_CREAT
, mode
, true);
4715 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4717 return tevent_req_simple_recv_ntstatus(req
);
4720 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4722 TALLOC_CTX
*frame
= talloc_stackframe();
4723 struct event_context
*ev
= NULL
;
4724 struct tevent_req
*req
= NULL
;
4725 NTSTATUS status
= NT_STATUS_OK
;
4727 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4729 * Can't use sync call while an async call is in flight
4731 status
= NT_STATUS_INVALID_PARAMETER
;
4735 ev
= event_context_init(frame
);
4737 status
= NT_STATUS_NO_MEMORY
;
4741 req
= cli_posix_mkdir_send(frame
,
4747 status
= NT_STATUS_NO_MEMORY
;
4751 if (!tevent_req_poll(req
, ev
)) {
4752 status
= map_nt_error_from_unix(errno
);
4756 status
= cli_posix_mkdir_recv(req
);
4763 /****************************************************************************
4764 unlink or rmdir - POSIX semantics.
4765 ****************************************************************************/
4767 struct cli_posix_unlink_internal_state
{
4771 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4773 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4774 struct event_context
*ev
,
4775 struct cli_state
*cli
,
4779 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4780 struct cli_posix_unlink_internal_state
*state
= NULL
;
4782 req
= tevent_req_create(mem_ctx
, &state
,
4783 struct cli_posix_unlink_internal_state
);
4788 /* Setup data word. */
4789 SSVAL(state
->data
, 0, level
);
4791 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4792 SMB_POSIX_PATH_UNLINK
,
4794 state
->data
, sizeof(state
->data
));
4795 if (tevent_req_nomem(subreq
, req
)) {
4796 return tevent_req_post(req
, ev
);
4798 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4802 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4804 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4805 tevent_req_simple_finish_ntstatus(subreq
, status
);
4808 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4809 struct event_context
*ev
,
4810 struct cli_state
*cli
,
4813 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4814 SMB_POSIX_UNLINK_FILE_TARGET
);
4817 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4819 return tevent_req_simple_recv_ntstatus(req
);
4822 /****************************************************************************
4823 unlink - POSIX semantics.
4824 ****************************************************************************/
4826 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4828 TALLOC_CTX
*frame
= talloc_stackframe();
4829 struct event_context
*ev
= NULL
;
4830 struct tevent_req
*req
= NULL
;
4831 NTSTATUS status
= NT_STATUS_OK
;
4833 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4835 * Can't use sync call while an async call is in flight
4837 status
= NT_STATUS_INVALID_PARAMETER
;
4841 ev
= event_context_init(frame
);
4843 status
= NT_STATUS_NO_MEMORY
;
4847 req
= cli_posix_unlink_send(frame
,
4852 status
= NT_STATUS_NO_MEMORY
;
4856 if (!tevent_req_poll(req
, ev
)) {
4857 status
= map_nt_error_from_unix(errno
);
4861 status
= cli_posix_unlink_recv(req
);
4868 /****************************************************************************
4869 rmdir - POSIX semantics.
4870 ****************************************************************************/
4872 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4873 struct event_context
*ev
,
4874 struct cli_state
*cli
,
4877 return cli_posix_unlink_internal_send(
4878 mem_ctx
, ev
, cli
, fname
,
4879 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4882 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4884 return tevent_req_simple_recv_ntstatus(req
);
4887 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4889 TALLOC_CTX
*frame
= talloc_stackframe();
4890 struct event_context
*ev
= NULL
;
4891 struct tevent_req
*req
= NULL
;
4892 NTSTATUS status
= NT_STATUS_OK
;
4894 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
4896 * Can't use sync call while an async call is in flight
4898 status
= NT_STATUS_INVALID_PARAMETER
;
4902 ev
= event_context_init(frame
);
4904 status
= NT_STATUS_NO_MEMORY
;
4908 req
= cli_posix_rmdir_send(frame
,
4913 status
= NT_STATUS_NO_MEMORY
;
4917 if (!tevent_req_poll(req
, ev
)) {
4918 status
= map_nt_error_from_unix(errno
);
4922 status
= cli_posix_rmdir_recv(req
, frame
);
4929 /****************************************************************************
4931 ****************************************************************************/
4933 struct cli_notify_state
{
4935 uint32_t num_changes
;
4936 struct notify_change
*changes
;
4939 static void cli_notify_done(struct tevent_req
*subreq
);
4941 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4942 struct tevent_context
*ev
,
4943 struct cli_state
*cli
, uint16_t fnum
,
4944 uint32_t buffer_size
,
4945 uint32_t completion_filter
, bool recursive
)
4947 struct tevent_req
*req
, *subreq
;
4948 struct cli_notify_state
*state
;
4949 unsigned old_timeout
;
4951 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
4956 SIVAL(state
->setup
, 0, completion_filter
);
4957 SSVAL(state
->setup
, 4, fnum
);
4958 SSVAL(state
->setup
, 6, recursive
);
4961 * Notifies should not time out
4963 old_timeout
= cli_set_timeout(cli
, 0);
4965 subreq
= cli_trans_send(
4966 state
, /* mem ctx. */
4967 ev
, /* event ctx. */
4968 cli
, /* cli_state. */
4969 SMBnttrans
, /* cmd. */
4970 NULL
, /* pipe name. */
4972 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
4974 (uint16_t *)state
->setup
, /* setup. */
4975 4, /* num setup uint16_t words. */
4976 0, /* max returned setup. */
4979 buffer_size
, /* max returned param. */
4982 0); /* max returned data. */
4984 cli_set_timeout(cli
, old_timeout
);
4986 if (tevent_req_nomem(subreq
, req
)) {
4987 return tevent_req_post(req
, ev
);
4989 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
4993 static void cli_notify_done(struct tevent_req
*subreq
)
4995 struct tevent_req
*req
= tevent_req_callback_data(
4996 subreq
, struct tevent_req
);
4997 struct cli_notify_state
*state
= tevent_req_data(
4998 req
, struct cli_notify_state
);
5001 uint32_t i
, ofs
, num_params
;
5004 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
5005 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
5006 TALLOC_FREE(subreq
);
5007 if (tevent_req_nterror(req
, status
)) {
5008 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5012 state
->num_changes
= 0;
5015 while (num_params
- ofs
> 12) {
5016 uint32_t next
= IVAL(params
, ofs
);
5017 state
->num_changes
+= 1;
5019 if ((next
== 0) || (ofs
+next
>= num_params
)) {
5025 state
->changes
= talloc_array(state
, struct notify_change
,
5026 state
->num_changes
);
5027 if (tevent_req_nomem(state
->changes
, req
)) {
5028 TALLOC_FREE(params
);
5034 for (i
=0; i
<state
->num_changes
; i
++) {
5035 uint32_t next
= IVAL(params
, ofs
);
5036 uint32_t len
= IVAL(params
, ofs
+8);
5040 if (trans_oob(num_params
, ofs
+ 12, len
)) {
5041 TALLOC_FREE(params
);
5043 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5047 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5048 ret
= clistr_pull_talloc(params
, (char *)params
, flags2
,
5049 &name
, params
+ofs
+12, len
,
5050 STR_TERMINATE
|STR_UNICODE
);
5052 TALLOC_FREE(params
);
5053 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5056 state
->changes
[i
].name
= name
;
5060 TALLOC_FREE(params
);
5061 tevent_req_done(req
);
5064 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5065 uint32_t *pnum_changes
,
5066 struct notify_change
**pchanges
)
5068 struct cli_notify_state
*state
= tevent_req_data(
5069 req
, struct cli_notify_state
);
5072 if (tevent_req_is_nterror(req
, &status
)) {
5076 *pnum_changes
= state
->num_changes
;
5077 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5078 return NT_STATUS_OK
;
5081 NTSTATUS
cli_notify(struct cli_state
*cli
, uint16_t fnum
, uint32_t buffer_size
,
5082 uint32_t completion_filter
, bool recursive
,
5083 TALLOC_CTX
*mem_ctx
, uint32_t *pnum_changes
,
5084 struct notify_change
**pchanges
)
5086 TALLOC_CTX
*frame
= talloc_stackframe();
5087 struct tevent_context
*ev
;
5088 struct tevent_req
*req
;
5089 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5091 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5093 * Can't use sync call while an async call is in flight
5095 status
= NT_STATUS_INVALID_PARAMETER
;
5098 ev
= tevent_context_init(frame
);
5102 req
= cli_notify_send(ev
, ev
, cli
, fnum
, buffer_size
,
5103 completion_filter
, recursive
);
5107 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5110 status
= cli_notify_recv(req
, mem_ctx
, pnum_changes
, pchanges
);
5116 struct cli_qpathinfo_state
{
5125 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
5127 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
5128 struct tevent_context
*ev
,
5129 struct cli_state
*cli
, const char *fname
,
5130 uint16_t level
, uint32_t min_rdata
,
5133 struct tevent_req
*req
, *subreq
;
5134 struct cli_qpathinfo_state
*state
;
5136 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
5140 state
->min_rdata
= min_rdata
;
5141 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
5143 state
->param
= talloc_zero_array(state
, uint8_t, 6);
5144 if (tevent_req_nomem(state
->param
, req
)) {
5145 return tevent_req_post(req
, ev
);
5147 SSVAL(state
->param
, 0, level
);
5148 state
->param
= trans2_bytes_push_str(
5149 state
->param
, cli_ucs2(cli
), fname
, strlen(fname
)+1, NULL
);
5150 if (tevent_req_nomem(state
->param
, req
)) {
5151 return tevent_req_post(req
, ev
);
5154 subreq
= cli_trans_send(
5155 state
, /* mem ctx. */
5156 ev
, /* event ctx. */
5157 cli
, /* cli_state. */
5158 SMBtrans2
, /* cmd. */
5159 NULL
, /* pipe name. */
5163 state
->setup
, /* setup. */
5164 1, /* num setup uint16_t words. */
5165 0, /* max returned setup. */
5166 state
->param
, /* param. */
5167 talloc_get_size(state
->param
), /* num param. */
5168 2, /* max returned param. */
5171 max_rdata
); /* max returned data. */
5173 if (tevent_req_nomem(subreq
, req
)) {
5174 return tevent_req_post(req
, ev
);
5176 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
5180 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
5182 struct tevent_req
*req
= tevent_req_callback_data(
5183 subreq
, struct tevent_req
);
5184 struct cli_qpathinfo_state
*state
= tevent_req_data(
5185 req
, struct cli_qpathinfo_state
);
5188 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
5190 &state
->rdata
, state
->min_rdata
,
5192 if (tevent_req_nterror(req
, status
)) {
5195 tevent_req_done(req
);
5198 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5199 uint8_t **rdata
, uint32_t *num_rdata
)
5201 struct cli_qpathinfo_state
*state
= tevent_req_data(
5202 req
, struct cli_qpathinfo_state
);
5205 if (tevent_req_is_nterror(req
, &status
)) {
5208 if (rdata
!= NULL
) {
5209 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5211 TALLOC_FREE(state
->rdata
);
5213 if (num_rdata
!= NULL
) {
5214 *num_rdata
= state
->num_rdata
;
5216 return NT_STATUS_OK
;
5219 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5220 const char *fname
, uint16_t level
, uint32_t min_rdata
,
5222 uint8_t **rdata
, uint32_t *num_rdata
)
5224 TALLOC_CTX
*frame
= talloc_stackframe();
5225 struct event_context
*ev
;
5226 struct tevent_req
*req
;
5227 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5229 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5231 * Can't use sync call while an async call is in flight
5233 status
= NT_STATUS_INVALID_PARAMETER
;
5236 ev
= event_context_init(frame
);
5240 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
5245 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5248 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5254 struct cli_qfileinfo_state
{
5258 uint16_t recv_flags2
;
5264 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
5266 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
5267 struct tevent_context
*ev
,
5268 struct cli_state
*cli
, uint16_t fnum
,
5269 uint16_t level
, uint32_t min_rdata
,
5272 struct tevent_req
*req
, *subreq
;
5273 struct cli_qfileinfo_state
*state
;
5275 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
5279 state
->min_rdata
= min_rdata
;
5280 SSVAL(state
->param
, 0, fnum
);
5281 SSVAL(state
->param
, 2, level
);
5282 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
5284 subreq
= cli_trans_send(
5285 state
, /* mem ctx. */
5286 ev
, /* event ctx. */
5287 cli
, /* cli_state. */
5288 SMBtrans2
, /* cmd. */
5289 NULL
, /* pipe name. */
5293 state
->setup
, /* setup. */
5294 1, /* num setup uint16_t words. */
5295 0, /* max returned setup. */
5296 state
->param
, /* param. */
5297 sizeof(state
->param
), /* num param. */
5298 2, /* max returned param. */
5301 max_rdata
); /* max returned data. */
5303 if (tevent_req_nomem(subreq
, req
)) {
5304 return tevent_req_post(req
, ev
);
5306 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
5310 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
5312 struct tevent_req
*req
= tevent_req_callback_data(
5313 subreq
, struct tevent_req
);
5314 struct cli_qfileinfo_state
*state
= tevent_req_data(
5315 req
, struct cli_qfileinfo_state
);
5318 status
= cli_trans_recv(subreq
, state
,
5319 &state
->recv_flags2
,
5322 &state
->rdata
, state
->min_rdata
,
5324 if (tevent_req_nterror(req
, status
)) {
5327 tevent_req_done(req
);
5330 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5331 uint16_t *recv_flags2
,
5332 uint8_t **rdata
, uint32_t *num_rdata
)
5334 struct cli_qfileinfo_state
*state
= tevent_req_data(
5335 req
, struct cli_qfileinfo_state
);
5338 if (tevent_req_is_nterror(req
, &status
)) {
5342 if (recv_flags2
!= NULL
) {
5343 *recv_flags2
= state
->recv_flags2
;
5345 if (rdata
!= NULL
) {
5346 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
5348 TALLOC_FREE(state
->rdata
);
5350 if (num_rdata
!= NULL
) {
5351 *num_rdata
= state
->num_rdata
;
5353 return NT_STATUS_OK
;
5356 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5357 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
5358 uint32_t max_rdata
, uint16_t *recv_flags2
,
5359 uint8_t **rdata
, uint32_t *num_rdata
)
5361 TALLOC_CTX
*frame
= talloc_stackframe();
5362 struct event_context
*ev
;
5363 struct tevent_req
*req
;
5364 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5366 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5368 * Can't use sync call while an async call is in flight
5370 status
= NT_STATUS_INVALID_PARAMETER
;
5373 ev
= event_context_init(frame
);
5377 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
5382 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5385 status
= cli_qfileinfo_recv(req
, mem_ctx
, recv_flags2
, rdata
, num_rdata
);
5391 struct cli_flush_state
{
5395 static void cli_flush_done(struct tevent_req
*subreq
);
5397 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5398 struct event_context
*ev
,
5399 struct cli_state
*cli
,
5402 struct tevent_req
*req
, *subreq
;
5403 struct cli_flush_state
*state
;
5405 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5409 SSVAL(state
->vwv
+ 0, 0, fnum
);
5411 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5413 if (tevent_req_nomem(subreq
, req
)) {
5414 return tevent_req_post(req
, ev
);
5416 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5420 static void cli_flush_done(struct tevent_req
*subreq
)
5422 struct tevent_req
*req
= tevent_req_callback_data(
5423 subreq
, struct tevent_req
);
5426 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5427 TALLOC_FREE(subreq
);
5428 if (tevent_req_nterror(req
, status
)) {
5431 tevent_req_done(req
);
5434 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5436 return tevent_req_simple_recv_ntstatus(req
);
5439 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5441 TALLOC_CTX
*frame
= talloc_stackframe();
5442 struct event_context
*ev
;
5443 struct tevent_req
*req
;
5444 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5446 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5448 * Can't use sync call while an async call is in flight
5450 status
= NT_STATUS_INVALID_PARAMETER
;
5453 ev
= event_context_init(frame
);
5457 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5461 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5464 status
= cli_flush_recv(req
);
5470 struct cli_shadow_copy_data_state
{
5477 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5479 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5480 struct tevent_context
*ev
,
5481 struct cli_state
*cli
,
5485 struct tevent_req
*req
, *subreq
;
5486 struct cli_shadow_copy_data_state
*state
;
5489 req
= tevent_req_create(mem_ctx
, &state
,
5490 struct cli_shadow_copy_data_state
);
5494 state
->get_names
= get_names
;
5495 ret_size
= get_names
? CLI_BUFFER_SIZE
: 16;
5497 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5498 SSVAL(state
->setup
+ 2, 0, fnum
);
5499 SCVAL(state
->setup
+ 3, 0, 0); /* isFsctl */
5500 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5502 subreq
= cli_trans_send(
5503 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5504 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5507 if (tevent_req_nomem(subreq
, req
)) {
5508 return tevent_req_post(req
, ev
);
5510 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5514 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5516 struct tevent_req
*req
= tevent_req_callback_data(
5517 subreq
, struct tevent_req
);
5518 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5519 req
, struct cli_shadow_copy_data_state
);
5522 status
= cli_trans_recv(subreq
, state
, NULL
,
5523 NULL
, 0, NULL
, /* setup */
5524 NULL
, 0, NULL
, /* param */
5525 &state
->data
, 12, &state
->num_data
);
5526 TALLOC_FREE(subreq
);
5527 if (tevent_req_nterror(req
, status
)) {
5530 tevent_req_done(req
);
5533 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5534 char ***pnames
, int *pnum_names
)
5536 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5537 req
, struct cli_shadow_copy_data_state
);
5543 if (tevent_req_is_nterror(req
, &status
)) {
5546 num_names
= IVAL(state
->data
, 4);
5547 dlength
= IVAL(state
->data
, 8);
5549 if (!state
->get_names
) {
5550 *pnum_names
= num_names
;
5551 return NT_STATUS_OK
;
5554 if (dlength
+12 > state
->num_data
) {
5555 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5557 names
= talloc_array(mem_ctx
, char *, num_names
);
5558 if (names
== NULL
) {
5559 return NT_STATUS_NO_MEMORY
;
5562 for (i
=0; i
<num_names
; i
++) {
5565 size_t converted_size
;
5567 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5568 ret
= convert_string_talloc(
5569 names
, CH_UTF16LE
, CH_UNIX
,
5570 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5571 &names
[i
], &converted_size
);
5574 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5577 *pnum_names
= num_names
;
5579 return NT_STATUS_OK
;
5582 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5583 uint16_t fnum
, bool get_names
,
5584 char ***pnames
, int *pnum_names
)
5586 TALLOC_CTX
*frame
= talloc_stackframe();
5587 struct event_context
*ev
;
5588 struct tevent_req
*req
;
5589 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5591 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
5593 * Can't use sync call while an async call is in flight
5595 status
= NT_STATUS_INVALID_PARAMETER
;
5598 ev
= event_context_init(frame
);
5602 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5606 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5609 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);