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"
30 /***********************************************************
31 Common function for pushing stings, used by smb_bytes_push_str()
32 and trans_bytes_push_str(). Only difference is the align_odd
34 ***********************************************************/
36 static uint8_t *internal_bytes_push_str(uint8_t *buf
, bool ucs2
,
37 const char *str
, size_t str_len
,
39 size_t *pconverted_size
)
43 size_t converted_size
;
49 buflen
= talloc_get_size(buf
);
52 ((align_odd
&& (buflen
% 2 == 0)) ||
53 (!align_odd
&& (buflen
% 2 == 1)))) {
55 * We're pushing into an SMB buffer, align odd
57 buf
= talloc_realloc(NULL
, buf
, uint8_t, buflen
+ 1);
65 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
66 ucs2
? CH_UTF16LE
: CH_DOS
,
67 str
, str_len
, &converted
,
72 buf
= talloc_realloc(NULL
, buf
, uint8_t,
73 buflen
+ converted_size
);
75 TALLOC_FREE(converted
);
79 memcpy(buf
+ buflen
, converted
, converted_size
);
81 TALLOC_FREE(converted
);
83 if (pconverted_size
) {
84 *pconverted_size
= converted_size
;
90 /***********************************************************
91 Push a string into an SMB buffer, with odd byte alignment
92 if it's a UCS2 string.
93 ***********************************************************/
95 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
96 const char *str
, size_t str_len
,
97 size_t *pconverted_size
)
99 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
100 true, pconverted_size
);
103 uint8_t *smb_bytes_push_bytes(uint8_t *buf
, uint8_t prefix
,
104 const uint8_t *bytes
, size_t num_bytes
)
111 buflen
= talloc_get_size(buf
);
113 buf
= talloc_realloc(NULL
, buf
, uint8_t,
114 buflen
+ 1 + num_bytes
);
118 buf
[buflen
] = prefix
;
119 memcpy(&buf
[buflen
+1], bytes
, num_bytes
);
123 /***********************************************************
124 Same as smb_bytes_push_str(), but without the odd byte
125 align for ucs2 (we're pushing into a param or data block).
126 static for now, although this will probably change when
127 other modules use async trans calls.
128 ***********************************************************/
130 static uint8_t *trans2_bytes_push_str(uint8_t *buf
, bool ucs2
,
131 const char *str
, size_t str_len
,
132 size_t *pconverted_size
)
134 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
135 false, pconverted_size
);
138 struct cli_setpathinfo_state
{
143 static void cli_setpathinfo_done(struct tevent_req
*subreq
);
145 struct tevent_req
*cli_setpathinfo_send(TALLOC_CTX
*mem_ctx
,
146 struct tevent_context
*ev
,
147 struct cli_state
*cli
,
153 struct tevent_req
*req
, *subreq
;
154 struct cli_setpathinfo_state
*state
;
156 req
= tevent_req_create(mem_ctx
, &state
,
157 struct cli_setpathinfo_state
);
162 /* Setup setup word. */
163 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
165 /* Setup param array. */
166 state
->param
= talloc_zero_array(state
, uint8_t, 6);
167 if (tevent_req_nomem(state
->param
, req
)) {
168 return tevent_req_post(req
, ev
);
170 SSVAL(state
->param
, 0, level
);
172 state
->param
= trans2_bytes_push_str(
173 state
->param
, cli_ucs2(cli
), path
, strlen(path
)+1, NULL
);
174 if (tevent_req_nomem(state
->param
, req
)) {
175 return tevent_req_post(req
, ev
);
178 subreq
= cli_trans_send(
179 state
, /* mem ctx. */
181 cli
, /* cli_state. */
182 SMBtrans2
, /* cmd. */
183 NULL
, /* pipe name. */
187 &state
->setup
, /* setup. */
188 1, /* num setup uint16_t words. */
189 0, /* max returned setup. */
190 state
->param
, /* param. */
191 talloc_get_size(state
->param
), /* num param. */
192 2, /* max returned param. */
194 data_len
, /* num data. */
195 0); /* max returned data. */
197 if (tevent_req_nomem(subreq
, req
)) {
198 return tevent_req_post(req
, ev
);
200 tevent_req_set_callback(subreq
, cli_setpathinfo_done
, req
);
204 static void cli_setpathinfo_done(struct tevent_req
*subreq
)
206 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
207 NULL
, 0, NULL
, NULL
, 0, NULL
);
208 tevent_req_simple_finish_ntstatus(subreq
, status
);
211 NTSTATUS
cli_setpathinfo_recv(struct tevent_req
*req
)
213 return tevent_req_simple_recv_ntstatus(req
);
216 NTSTATUS
cli_setpathinfo(struct cli_state
*cli
,
222 TALLOC_CTX
*frame
= talloc_stackframe();
223 struct tevent_context
*ev
;
224 struct tevent_req
*req
;
225 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
227 if (cli_has_async_calls(cli
)) {
229 * Can't use sync call while an async call is in flight
231 status
= NT_STATUS_INVALID_PARAMETER
;
234 ev
= tevent_context_init(frame
);
238 req
= cli_setpathinfo_send(ev
, ev
, cli
, level
, path
, data
, data_len
);
242 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
245 status
= cli_setpathinfo_recv(req
);
251 /****************************************************************************
252 Hard/Symlink a file (UNIX extensions).
253 Creates new name (sym)linked to oldname.
254 ****************************************************************************/
256 struct cli_posix_link_internal_state
{
260 static void cli_posix_link_internal_done(struct tevent_req
*subreq
);
262 static struct tevent_req
*cli_posix_link_internal_send(TALLOC_CTX
*mem_ctx
,
263 struct event_context
*ev
,
264 struct cli_state
*cli
,
269 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
270 struct cli_posix_link_internal_state
*state
= NULL
;
272 req
= tevent_req_create(mem_ctx
, &state
,
273 struct cli_posix_link_internal_state
);
278 /* Setup data array. */
279 state
->data
= talloc_array(state
, uint8_t, 0);
280 if (tevent_req_nomem(state
->data
, req
)) {
281 return tevent_req_post(req
, ev
);
283 state
->data
= trans2_bytes_push_str(
284 state
->data
, cli_ucs2(cli
), oldname
, strlen(oldname
)+1, NULL
);
286 subreq
= cli_setpathinfo_send(
287 state
, ev
, cli
, level
, newname
,
288 state
->data
, talloc_get_size(state
->data
));
289 if (tevent_req_nomem(subreq
, req
)) {
290 return tevent_req_post(req
, ev
);
292 tevent_req_set_callback(subreq
, cli_posix_link_internal_done
, req
);
296 static void cli_posix_link_internal_done(struct tevent_req
*subreq
)
298 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
299 tevent_req_simple_finish_ntstatus(subreq
, status
);
302 /****************************************************************************
303 Symlink a file (UNIX extensions).
304 ****************************************************************************/
306 struct tevent_req
*cli_posix_symlink_send(TALLOC_CTX
*mem_ctx
,
307 struct event_context
*ev
,
308 struct cli_state
*cli
,
312 return cli_posix_link_internal_send(
313 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_LINK
, oldname
, newname
);
316 NTSTATUS
cli_posix_symlink_recv(struct tevent_req
*req
)
318 return tevent_req_simple_recv_ntstatus(req
);
321 NTSTATUS
cli_posix_symlink(struct cli_state
*cli
,
325 TALLOC_CTX
*frame
= talloc_stackframe();
326 struct event_context
*ev
= NULL
;
327 struct tevent_req
*req
= NULL
;
328 NTSTATUS status
= NT_STATUS_OK
;
330 if (cli_has_async_calls(cli
)) {
332 * Can't use sync call while an async call is in flight
334 status
= NT_STATUS_INVALID_PARAMETER
;
338 ev
= event_context_init(frame
);
340 status
= NT_STATUS_NO_MEMORY
;
344 req
= cli_posix_symlink_send(frame
,
350 status
= NT_STATUS_NO_MEMORY
;
354 if (!tevent_req_poll(req
, ev
)) {
355 status
= map_nt_error_from_unix(errno
);
359 status
= cli_posix_symlink_recv(req
);
366 /****************************************************************************
367 Read a POSIX symlink.
368 ****************************************************************************/
370 struct readlink_state
{
375 static void cli_posix_readlink_done(struct tevent_req
*subreq
);
377 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
378 struct event_context
*ev
,
379 struct cli_state
*cli
,
383 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
384 struct readlink_state
*state
= NULL
;
385 uint32_t maxbytelen
= (uint32_t)(cli_ucs2(cli
) ? len
*3 : len
);
387 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
393 * Len is in bytes, we need it in UCS2 units.
395 if ((2*len
< len
) || (maxbytelen
< len
)) {
396 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
397 return tevent_req_post(req
, ev
);
400 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
401 SMB_QUERY_FILE_UNIX_LINK
, 1, maxbytelen
);
402 if (tevent_req_nomem(subreq
, req
)) {
403 return tevent_req_post(req
, ev
);
405 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
409 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
411 struct tevent_req
*req
= tevent_req_callback_data(
412 subreq
, struct tevent_req
);
413 struct readlink_state
*state
= tevent_req_data(
414 req
, struct readlink_state
);
417 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
420 if (tevent_req_nterror(req
, status
)) {
424 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
426 if (state
->data
[state
->num_data
-1] != '\0') {
427 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
430 tevent_req_done(req
);
433 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
434 char *retpath
, size_t len
)
437 char *converted
= NULL
;
438 size_t converted_size
= 0;
439 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
441 if (tevent_req_is_nterror(req
, &status
)) {
444 /* The returned data is a pushed string, not raw data. */
445 if (!convert_string_talloc(state
,
446 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
452 return NT_STATUS_NO_MEMORY
;
455 len
= MIN(len
,converted_size
);
457 return NT_STATUS_DATA_ERROR
;
459 memcpy(retpath
, converted
, len
);
463 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
464 char *linkpath
, size_t len
)
466 TALLOC_CTX
*frame
= talloc_stackframe();
467 struct event_context
*ev
= NULL
;
468 struct tevent_req
*req
= NULL
;
469 NTSTATUS status
= NT_STATUS_OK
;
471 if (cli_has_async_calls(cli
)) {
473 * Can't use sync call while an async call is in flight
475 status
= NT_STATUS_INVALID_PARAMETER
;
479 ev
= event_context_init(frame
);
481 status
= NT_STATUS_NO_MEMORY
;
485 req
= cli_posix_readlink_send(frame
,
491 status
= NT_STATUS_NO_MEMORY
;
495 if (!tevent_req_poll(req
, ev
)) {
496 status
= map_nt_error_from_unix(errno
);
500 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
507 /****************************************************************************
508 Hard link a file (UNIX extensions).
509 ****************************************************************************/
511 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
512 struct event_context
*ev
,
513 struct cli_state
*cli
,
517 return cli_posix_link_internal_send(
518 mem_ctx
, ev
, cli
, SMB_SET_FILE_UNIX_HLINK
, oldname
, newname
);
521 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
523 return tevent_req_simple_recv_ntstatus(req
);
526 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
530 TALLOC_CTX
*frame
= talloc_stackframe();
531 struct event_context
*ev
= NULL
;
532 struct tevent_req
*req
= NULL
;
533 NTSTATUS status
= NT_STATUS_OK
;
535 if (cli_has_async_calls(cli
)) {
537 * Can't use sync call while an async call is in flight
539 status
= NT_STATUS_INVALID_PARAMETER
;
543 ev
= event_context_init(frame
);
545 status
= NT_STATUS_NO_MEMORY
;
549 req
= cli_posix_hardlink_send(frame
,
555 status
= NT_STATUS_NO_MEMORY
;
559 if (!tevent_req_poll(req
, ev
)) {
560 status
= map_nt_error_from_unix(errno
);
564 status
= cli_posix_hardlink_recv(req
);
571 /****************************************************************************
572 Do a POSIX getfacl (UNIX extensions).
573 ****************************************************************************/
575 struct getfacl_state
{
580 static void cli_posix_getfacl_done(struct tevent_req
*subreq
);
582 struct tevent_req
*cli_posix_getfacl_send(TALLOC_CTX
*mem_ctx
,
583 struct event_context
*ev
,
584 struct cli_state
*cli
,
587 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
588 struct getfacl_state
*state
= NULL
;
590 req
= tevent_req_create(mem_ctx
, &state
, struct getfacl_state
);
594 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
, SMB_QUERY_POSIX_ACL
,
596 if (tevent_req_nomem(subreq
, req
)) {
597 return tevent_req_post(req
, ev
);
599 tevent_req_set_callback(subreq
, cli_posix_getfacl_done
, req
);
603 static void cli_posix_getfacl_done(struct tevent_req
*subreq
)
605 struct tevent_req
*req
= tevent_req_callback_data(
606 subreq
, struct tevent_req
);
607 struct getfacl_state
*state
= tevent_req_data(
608 req
, struct getfacl_state
);
611 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
614 if (tevent_req_nterror(req
, status
)) {
617 tevent_req_done(req
);
620 NTSTATUS
cli_posix_getfacl_recv(struct tevent_req
*req
,
625 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
628 if (tevent_req_is_nterror(req
, &status
)) {
631 *prb_size
= (size_t)state
->num_data
;
632 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
636 NTSTATUS
cli_posix_getfacl(struct cli_state
*cli
,
642 TALLOC_CTX
*frame
= talloc_stackframe();
643 struct event_context
*ev
= NULL
;
644 struct tevent_req
*req
= NULL
;
645 NTSTATUS status
= NT_STATUS_OK
;
647 if (cli_has_async_calls(cli
)) {
649 * Can't use sync call while an async call is in flight
651 status
= NT_STATUS_INVALID_PARAMETER
;
655 ev
= event_context_init(frame
);
657 status
= NT_STATUS_NO_MEMORY
;
661 req
= cli_posix_getfacl_send(frame
,
666 status
= NT_STATUS_NO_MEMORY
;
670 if (!tevent_req_poll(req
, ev
)) {
671 status
= map_nt_error_from_unix(errno
);
675 status
= cli_posix_getfacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
682 /****************************************************************************
683 Stat a file (UNIX extensions).
684 ****************************************************************************/
691 static void cli_posix_stat_done(struct tevent_req
*subreq
);
693 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
694 struct event_context
*ev
,
695 struct cli_state
*cli
,
698 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
699 struct stat_state
*state
= NULL
;
701 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
705 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
706 SMB_QUERY_FILE_UNIX_BASIC
, 100, 100);
707 if (tevent_req_nomem(subreq
, req
)) {
708 return tevent_req_post(req
, ev
);
710 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
714 static void cli_posix_stat_done(struct tevent_req
*subreq
)
716 struct tevent_req
*req
= tevent_req_callback_data(
717 subreq
, struct tevent_req
);
718 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
721 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
724 if (tevent_req_nterror(req
, status
)) {
727 tevent_req_done(req
);
730 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
731 SMB_STRUCT_STAT
*sbuf
)
733 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
736 if (tevent_req_is_nterror(req
, &status
)) {
740 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
741 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
742 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
743 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
745 /* assume 512 byte blocks */
746 sbuf
->st_ex_blocks
/= 512;
748 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
749 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
750 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
752 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
753 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
754 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
755 #if defined(HAVE_MAKEDEV)
757 uint32_t dev_major
= IVAL(state
->data
,60);
758 uint32_t dev_minor
= IVAL(state
->data
,68);
759 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
762 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
763 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
764 sbuf
->st_ex_nlink
= BIG_UINT(state
->data
,92); /* number of hard links */
769 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
771 SMB_STRUCT_STAT
*sbuf
)
773 TALLOC_CTX
*frame
= talloc_stackframe();
774 struct event_context
*ev
= NULL
;
775 struct tevent_req
*req
= NULL
;
776 NTSTATUS status
= NT_STATUS_OK
;
778 if (cli_has_async_calls(cli
)) {
780 * Can't use sync call while an async call is in flight
782 status
= NT_STATUS_INVALID_PARAMETER
;
786 ev
= event_context_init(frame
);
788 status
= NT_STATUS_NO_MEMORY
;
792 req
= cli_posix_stat_send(frame
,
797 status
= NT_STATUS_NO_MEMORY
;
801 if (!tevent_req_poll(req
, ev
)) {
802 status
= map_nt_error_from_unix(errno
);
806 status
= cli_posix_stat_recv(req
, sbuf
);
813 /****************************************************************************
814 Chmod or chown a file internal (UNIX extensions).
815 ****************************************************************************/
817 struct cli_posix_chown_chmod_internal_state
{
821 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
);
823 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
824 struct event_context
*ev
,
825 struct cli_state
*cli
,
831 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
832 struct cli_posix_chown_chmod_internal_state
*state
= NULL
;
834 req
= tevent_req_create(mem_ctx
, &state
,
835 struct cli_posix_chown_chmod_internal_state
);
840 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
841 memset(&state
->data
[40], '\0', 60);
842 SIVAL(state
->data
,40,uid
);
843 SIVAL(state
->data
,48,gid
);
844 SIVAL(state
->data
,84,mode
);
846 subreq
= cli_setpathinfo_send(state
, ev
, cli
, SMB_SET_FILE_UNIX_BASIC
,
847 fname
, state
->data
, sizeof(state
->data
));
848 if (tevent_req_nomem(subreq
, req
)) {
849 return tevent_req_post(req
, ev
);
851 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
,
856 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
858 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
859 tevent_req_simple_finish_ntstatus(subreq
, status
);
862 /****************************************************************************
863 chmod a file (UNIX extensions).
864 ****************************************************************************/
866 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
867 struct event_context
*ev
,
868 struct cli_state
*cli
,
872 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
874 unix_perms_to_wire(mode
),
879 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
881 return tevent_req_simple_recv_ntstatus(req
);
884 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
886 TALLOC_CTX
*frame
= talloc_stackframe();
887 struct event_context
*ev
= NULL
;
888 struct tevent_req
*req
= NULL
;
889 NTSTATUS status
= NT_STATUS_OK
;
891 if (cli_has_async_calls(cli
)) {
893 * Can't use sync call while an async call is in flight
895 status
= NT_STATUS_INVALID_PARAMETER
;
899 ev
= event_context_init(frame
);
901 status
= NT_STATUS_NO_MEMORY
;
905 req
= cli_posix_chmod_send(frame
,
911 status
= NT_STATUS_NO_MEMORY
;
915 if (!tevent_req_poll(req
, ev
)) {
916 status
= map_nt_error_from_unix(errno
);
920 status
= cli_posix_chmod_recv(req
);
927 /****************************************************************************
928 chown a file (UNIX extensions).
929 ****************************************************************************/
931 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
932 struct event_context
*ev
,
933 struct cli_state
*cli
,
938 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
945 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
947 return tevent_req_simple_recv_ntstatus(req
);
950 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
955 TALLOC_CTX
*frame
= talloc_stackframe();
956 struct event_context
*ev
= NULL
;
957 struct tevent_req
*req
= NULL
;
958 NTSTATUS status
= NT_STATUS_OK
;
960 if (cli_has_async_calls(cli
)) {
962 * Can't use sync call while an async call is in flight
964 status
= NT_STATUS_INVALID_PARAMETER
;
968 ev
= event_context_init(frame
);
970 status
= NT_STATUS_NO_MEMORY
;
974 req
= cli_posix_chown_send(frame
,
981 status
= NT_STATUS_NO_MEMORY
;
985 if (!tevent_req_poll(req
, ev
)) {
986 status
= map_nt_error_from_unix(errno
);
990 status
= cli_posix_chown_recv(req
);
997 /****************************************************************************
999 ****************************************************************************/
1001 static void cli_rename_done(struct tevent_req
*subreq
);
1003 struct cli_rename_state
{
1007 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1008 struct event_context
*ev
,
1009 struct cli_state
*cli
,
1010 const char *fname_src
,
1011 const char *fname_dst
)
1013 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1014 struct cli_rename_state
*state
= NULL
;
1015 uint8_t additional_flags
= 0;
1016 uint8_t *bytes
= NULL
;
1018 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
1023 SSVAL(state
->vwv
+0, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1025 bytes
= talloc_array(state
, uint8_t, 1);
1026 if (tevent_req_nomem(bytes
, req
)) {
1027 return tevent_req_post(req
, ev
);
1030 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
1031 strlen(fname_src
)+1, NULL
);
1032 if (tevent_req_nomem(bytes
, req
)) {
1033 return tevent_req_post(req
, ev
);
1036 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1037 talloc_get_size(bytes
)+1);
1038 if (tevent_req_nomem(bytes
, req
)) {
1039 return tevent_req_post(req
, ev
);
1042 bytes
[talloc_get_size(bytes
)-1] = 4;
1043 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
1044 strlen(fname_dst
)+1, NULL
);
1045 if (tevent_req_nomem(bytes
, req
)) {
1046 return tevent_req_post(req
, ev
);
1049 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1050 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1051 if (tevent_req_nomem(subreq
, req
)) {
1052 return tevent_req_post(req
, ev
);
1054 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
1058 static void cli_rename_done(struct tevent_req
*subreq
)
1060 struct tevent_req
*req
= tevent_req_callback_data(
1061 subreq
, struct tevent_req
);
1064 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1065 TALLOC_FREE(subreq
);
1066 if (tevent_req_nterror(req
, status
)) {
1069 tevent_req_done(req
);
1072 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1074 return tevent_req_simple_recv_ntstatus(req
);
1077 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1079 TALLOC_CTX
*frame
= talloc_stackframe();
1080 struct event_context
*ev
;
1081 struct tevent_req
*req
;
1082 NTSTATUS status
= NT_STATUS_OK
;
1084 if (cli_has_async_calls(cli
)) {
1086 * Can't use sync call while an async call is in flight
1088 status
= NT_STATUS_INVALID_PARAMETER
;
1092 ev
= event_context_init(frame
);
1094 status
= NT_STATUS_NO_MEMORY
;
1098 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1100 status
= NT_STATUS_NO_MEMORY
;
1104 if (!tevent_req_poll(req
, ev
)) {
1105 status
= map_nt_error_from_unix(errno
);
1109 status
= cli_rename_recv(req
);
1116 /****************************************************************************
1118 ****************************************************************************/
1120 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1122 struct cli_ntrename_internal_state
{
1126 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1127 struct event_context
*ev
,
1128 struct cli_state
*cli
,
1129 const char *fname_src
,
1130 const char *fname_dst
,
1131 uint16_t rename_flag
)
1133 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1134 struct cli_ntrename_internal_state
*state
= NULL
;
1135 uint8_t additional_flags
= 0;
1136 uint8_t *bytes
= NULL
;
1138 req
= tevent_req_create(mem_ctx
, &state
,
1139 struct cli_ntrename_internal_state
);
1144 SSVAL(state
->vwv
+0, 0 ,FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
);
1145 SSVAL(state
->vwv
+1, 0, rename_flag
);
1147 bytes
= talloc_array(state
, uint8_t, 1);
1148 if (tevent_req_nomem(bytes
, req
)) {
1149 return tevent_req_post(req
, ev
);
1152 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
1153 strlen(fname_src
)+1, NULL
);
1154 if (tevent_req_nomem(bytes
, req
)) {
1155 return tevent_req_post(req
, ev
);
1158 bytes
= talloc_realloc(state
, bytes
, uint8_t,
1159 talloc_get_size(bytes
)+1);
1160 if (tevent_req_nomem(bytes
, req
)) {
1161 return tevent_req_post(req
, ev
);
1164 bytes
[talloc_get_size(bytes
)-1] = 4;
1165 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
1166 strlen(fname_dst
)+1, NULL
);
1167 if (tevent_req_nomem(bytes
, req
)) {
1168 return tevent_req_post(req
, ev
);
1171 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1172 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1173 if (tevent_req_nomem(subreq
, req
)) {
1174 return tevent_req_post(req
, ev
);
1176 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1180 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1182 struct tevent_req
*req
= tevent_req_callback_data(
1183 subreq
, struct tevent_req
);
1186 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1187 TALLOC_FREE(subreq
);
1188 if (tevent_req_nterror(req
, status
)) {
1191 tevent_req_done(req
);
1194 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1196 return tevent_req_simple_recv_ntstatus(req
);
1199 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1200 struct event_context
*ev
,
1201 struct cli_state
*cli
,
1202 const char *fname_src
,
1203 const char *fname_dst
)
1205 return cli_ntrename_internal_send(mem_ctx
,
1210 RENAME_FLAG_RENAME
);
1213 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1215 return cli_ntrename_internal_recv(req
);
1218 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1220 TALLOC_CTX
*frame
= talloc_stackframe();
1221 struct event_context
*ev
;
1222 struct tevent_req
*req
;
1223 NTSTATUS status
= NT_STATUS_OK
;
1225 if (cli_has_async_calls(cli
)) {
1227 * Can't use sync call while an async call is in flight
1229 status
= NT_STATUS_INVALID_PARAMETER
;
1233 ev
= event_context_init(frame
);
1235 status
= NT_STATUS_NO_MEMORY
;
1239 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1241 status
= NT_STATUS_NO_MEMORY
;
1245 if (!tevent_req_poll(req
, ev
)) {
1246 status
= map_nt_error_from_unix(errno
);
1250 status
= cli_ntrename_recv(req
);
1257 /****************************************************************************
1259 ****************************************************************************/
1261 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1262 struct event_context
*ev
,
1263 struct cli_state
*cli
,
1264 const char *fname_src
,
1265 const char *fname_dst
)
1267 return cli_ntrename_internal_send(mem_ctx
,
1272 RENAME_FLAG_HARD_LINK
);
1275 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1277 return cli_ntrename_internal_recv(req
);
1280 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1282 TALLOC_CTX
*frame
= talloc_stackframe();
1283 struct event_context
*ev
;
1284 struct tevent_req
*req
;
1285 NTSTATUS status
= NT_STATUS_OK
;
1287 if (cli_has_async_calls(cli
)) {
1289 * Can't use sync call while an async call is in flight
1291 status
= NT_STATUS_INVALID_PARAMETER
;
1295 ev
= event_context_init(frame
);
1297 status
= NT_STATUS_NO_MEMORY
;
1301 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1303 status
= NT_STATUS_NO_MEMORY
;
1307 if (!tevent_req_poll(req
, ev
)) {
1308 status
= map_nt_error_from_unix(errno
);
1312 status
= cli_nt_hardlink_recv(req
);
1319 /****************************************************************************
1321 ****************************************************************************/
1323 static void cli_unlink_done(struct tevent_req
*subreq
);
1325 struct cli_unlink_state
{
1329 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1330 struct event_context
*ev
,
1331 struct cli_state
*cli
,
1333 uint16_t mayhave_attrs
)
1335 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1336 struct cli_unlink_state
*state
= NULL
;
1337 uint8_t additional_flags
= 0;
1338 uint8_t *bytes
= NULL
;
1340 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1345 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1347 bytes
= talloc_array(state
, uint8_t, 1);
1348 if (tevent_req_nomem(bytes
, req
)) {
1349 return tevent_req_post(req
, ev
);
1352 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1353 strlen(fname
)+1, NULL
);
1355 if (tevent_req_nomem(bytes
, req
)) {
1356 return tevent_req_post(req
, ev
);
1359 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1360 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1361 if (tevent_req_nomem(subreq
, req
)) {
1362 return tevent_req_post(req
, ev
);
1364 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1368 static void cli_unlink_done(struct tevent_req
*subreq
)
1370 struct tevent_req
*req
= tevent_req_callback_data(
1371 subreq
, struct tevent_req
);
1374 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1375 TALLOC_FREE(subreq
);
1376 if (tevent_req_nterror(req
, status
)) {
1379 tevent_req_done(req
);
1382 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1384 return tevent_req_simple_recv_ntstatus(req
);
1387 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1389 TALLOC_CTX
*frame
= talloc_stackframe();
1390 struct event_context
*ev
;
1391 struct tevent_req
*req
;
1392 NTSTATUS status
= NT_STATUS_OK
;
1394 if (cli_has_async_calls(cli
)) {
1396 * Can't use sync call while an async call is in flight
1398 status
= NT_STATUS_INVALID_PARAMETER
;
1402 ev
= event_context_init(frame
);
1404 status
= NT_STATUS_NO_MEMORY
;
1408 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1410 status
= NT_STATUS_NO_MEMORY
;
1414 if (!tevent_req_poll(req
, ev
)) {
1415 status
= map_nt_error_from_unix(errno
);
1419 status
= cli_unlink_recv(req
);
1426 /****************************************************************************
1428 ****************************************************************************/
1430 static void cli_mkdir_done(struct tevent_req
*subreq
);
1432 struct cli_mkdir_state
{
1436 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1437 struct event_context
*ev
,
1438 struct cli_state
*cli
,
1441 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1442 struct cli_mkdir_state
*state
= NULL
;
1443 uint8_t additional_flags
= 0;
1444 uint8_t *bytes
= NULL
;
1446 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1451 bytes
= talloc_array(state
, uint8_t, 1);
1452 if (tevent_req_nomem(bytes
, req
)) {
1453 return tevent_req_post(req
, ev
);
1456 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1457 strlen(dname
)+1, NULL
);
1459 if (tevent_req_nomem(bytes
, req
)) {
1460 return tevent_req_post(req
, ev
);
1463 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1464 0, NULL
, talloc_get_size(bytes
), bytes
);
1465 if (tevent_req_nomem(subreq
, req
)) {
1466 return tevent_req_post(req
, ev
);
1468 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1472 static void cli_mkdir_done(struct tevent_req
*subreq
)
1474 struct tevent_req
*req
= tevent_req_callback_data(
1475 subreq
, struct tevent_req
);
1478 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1479 TALLOC_FREE(subreq
);
1480 if (tevent_req_nterror(req
, status
)) {
1483 tevent_req_done(req
);
1486 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1488 return tevent_req_simple_recv_ntstatus(req
);
1491 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1493 TALLOC_CTX
*frame
= talloc_stackframe();
1494 struct event_context
*ev
;
1495 struct tevent_req
*req
;
1496 NTSTATUS status
= NT_STATUS_OK
;
1498 if (cli_has_async_calls(cli
)) {
1500 * Can't use sync call while an async call is in flight
1502 status
= NT_STATUS_INVALID_PARAMETER
;
1506 ev
= event_context_init(frame
);
1508 status
= NT_STATUS_NO_MEMORY
;
1512 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1514 status
= NT_STATUS_NO_MEMORY
;
1518 if (!tevent_req_poll(req
, ev
)) {
1519 status
= map_nt_error_from_unix(errno
);
1523 status
= cli_mkdir_recv(req
);
1530 /****************************************************************************
1532 ****************************************************************************/
1534 static void cli_rmdir_done(struct tevent_req
*subreq
);
1536 struct cli_rmdir_state
{
1540 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1541 struct event_context
*ev
,
1542 struct cli_state
*cli
,
1545 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1546 struct cli_rmdir_state
*state
= NULL
;
1547 uint8_t additional_flags
= 0;
1548 uint8_t *bytes
= NULL
;
1550 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1555 bytes
= talloc_array(state
, uint8_t, 1);
1556 if (tevent_req_nomem(bytes
, req
)) {
1557 return tevent_req_post(req
, ev
);
1560 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1561 strlen(dname
)+1, NULL
);
1563 if (tevent_req_nomem(bytes
, req
)) {
1564 return tevent_req_post(req
, ev
);
1567 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1568 0, NULL
, talloc_get_size(bytes
), bytes
);
1569 if (tevent_req_nomem(subreq
, req
)) {
1570 return tevent_req_post(req
, ev
);
1572 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1576 static void cli_rmdir_done(struct tevent_req
*subreq
)
1578 struct tevent_req
*req
= tevent_req_callback_data(
1579 subreq
, struct tevent_req
);
1582 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1583 TALLOC_FREE(subreq
);
1584 if (tevent_req_nterror(req
, status
)) {
1587 tevent_req_done(req
);
1590 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1592 return tevent_req_simple_recv_ntstatus(req
);
1595 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1597 TALLOC_CTX
*frame
= talloc_stackframe();
1598 struct event_context
*ev
;
1599 struct tevent_req
*req
;
1600 NTSTATUS status
= NT_STATUS_OK
;
1602 if (cli_has_async_calls(cli
)) {
1604 * Can't use sync call while an async call is in flight
1606 status
= NT_STATUS_INVALID_PARAMETER
;
1610 ev
= event_context_init(frame
);
1612 status
= NT_STATUS_NO_MEMORY
;
1616 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1618 status
= NT_STATUS_NO_MEMORY
;
1622 if (!tevent_req_poll(req
, ev
)) {
1623 status
= map_nt_error_from_unix(errno
);
1627 status
= cli_rmdir_recv(req
);
1634 /****************************************************************************
1635 Set or clear the delete on close flag.
1636 ****************************************************************************/
1644 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1646 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
1647 NULL
, 0, NULL
, NULL
, 0, NULL
);
1648 tevent_req_simple_finish_ntstatus(subreq
, status
);
1651 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1652 struct event_context
*ev
,
1653 struct cli_state
*cli
,
1657 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1658 struct doc_state
*state
= NULL
;
1660 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1665 /* Setup setup word. */
1666 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1668 /* Setup param array. */
1669 SSVAL(state
->param
,0,fnum
);
1670 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1672 /* Setup data array. */
1673 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1675 subreq
= cli_trans_send(state
, /* mem ctx. */
1676 ev
, /* event ctx. */
1677 cli
, /* cli_state. */
1678 SMBtrans2
, /* cmd. */
1679 NULL
, /* pipe name. */
1683 &state
->setup
, /* setup. */
1684 1, /* num setup uint16_t words. */
1685 0, /* max returned setup. */
1686 state
->param
, /* param. */
1688 2, /* max returned param. */
1689 state
->data
, /* data. */
1691 0); /* max returned data. */
1693 if (tevent_req_nomem(subreq
, req
)) {
1694 return tevent_req_post(req
, ev
);
1696 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1700 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1702 return tevent_req_simple_recv_ntstatus(req
);
1705 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1707 TALLOC_CTX
*frame
= talloc_stackframe();
1708 struct event_context
*ev
= NULL
;
1709 struct tevent_req
*req
= NULL
;
1710 NTSTATUS status
= NT_STATUS_OK
;
1712 if (cli_has_async_calls(cli
)) {
1714 * Can't use sync call while an async call is in flight
1716 status
= NT_STATUS_INVALID_PARAMETER
;
1720 ev
= event_context_init(frame
);
1722 status
= NT_STATUS_NO_MEMORY
;
1726 req
= cli_nt_delete_on_close_send(frame
,
1732 status
= NT_STATUS_NO_MEMORY
;
1736 if (!tevent_req_poll(req
, ev
)) {
1737 status
= map_nt_error_from_unix(errno
);
1741 status
= cli_nt_delete_on_close_recv(req
);
1748 struct cli_ntcreate_state
{
1753 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1755 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1756 struct event_context
*ev
,
1757 struct cli_state
*cli
,
1759 uint32_t CreatFlags
,
1760 uint32_t DesiredAccess
,
1761 uint32_t FileAttributes
,
1762 uint32_t ShareAccess
,
1763 uint32_t CreateDisposition
,
1764 uint32_t CreateOptions
,
1765 uint8_t SecurityFlags
)
1767 struct tevent_req
*req
, *subreq
;
1768 struct cli_ntcreate_state
*state
;
1771 size_t converted_len
;
1773 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1780 SCVAL(vwv
+0, 0, 0xFF);
1785 if (cli
->use_oplocks
) {
1786 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1788 SIVAL(vwv
+3, 1, CreatFlags
);
1789 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1790 SIVAL(vwv
+7, 1, DesiredAccess
);
1791 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1792 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1793 SIVAL(vwv
+13, 1, FileAttributes
);
1794 SIVAL(vwv
+15, 1, ShareAccess
);
1795 SIVAL(vwv
+17, 1, CreateDisposition
);
1796 SIVAL(vwv
+19, 1, CreateOptions
);
1797 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1798 SCVAL(vwv
+23, 1, SecurityFlags
);
1800 bytes
= talloc_array(state
, uint8_t, 0);
1801 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
),
1802 fname
, strlen(fname
)+1,
1805 /* sigh. this copes with broken netapp filer behaviour */
1806 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, NULL
);
1808 if (tevent_req_nomem(bytes
, req
)) {
1809 return tevent_req_post(req
, ev
);
1812 SSVAL(vwv
+2, 1, converted_len
);
1814 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1815 talloc_get_size(bytes
), bytes
);
1816 if (tevent_req_nomem(subreq
, req
)) {
1817 return tevent_req_post(req
, ev
);
1819 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1823 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1825 struct tevent_req
*req
= tevent_req_callback_data(
1826 subreq
, struct tevent_req
);
1827 struct cli_ntcreate_state
*state
= tevent_req_data(
1828 req
, struct cli_ntcreate_state
);
1836 status
= cli_smb_recv(subreq
, state
, &inbuf
, 3, &wct
, &vwv
,
1837 &num_bytes
, &bytes
);
1838 TALLOC_FREE(subreq
);
1839 if (tevent_req_nterror(req
, status
)) {
1842 state
->fnum
= SVAL(vwv
+2, 1);
1843 tevent_req_done(req
);
1846 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1848 struct cli_ntcreate_state
*state
= tevent_req_data(
1849 req
, struct cli_ntcreate_state
);
1852 if (tevent_req_is_nterror(req
, &status
)) {
1855 *pfnum
= state
->fnum
;
1856 return NT_STATUS_OK
;
1859 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1861 uint32_t CreatFlags
,
1862 uint32_t DesiredAccess
,
1863 uint32_t FileAttributes
,
1864 uint32_t ShareAccess
,
1865 uint32_t CreateDisposition
,
1866 uint32_t CreateOptions
,
1867 uint8_t SecurityFlags
,
1870 TALLOC_CTX
*frame
= talloc_stackframe();
1871 struct event_context
*ev
;
1872 struct tevent_req
*req
;
1873 NTSTATUS status
= NT_STATUS_OK
;
1875 if (cli_has_async_calls(cli
)) {
1877 * Can't use sync call while an async call is in flight
1879 status
= NT_STATUS_INVALID_PARAMETER
;
1883 ev
= event_context_init(frame
);
1885 status
= NT_STATUS_NO_MEMORY
;
1889 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1890 DesiredAccess
, FileAttributes
, ShareAccess
,
1891 CreateDisposition
, CreateOptions
,
1894 status
= NT_STATUS_NO_MEMORY
;
1898 if (!tevent_req_poll(req
, ev
)) {
1899 status
= map_nt_error_from_unix(errno
);
1903 status
= cli_ntcreate_recv(req
, pfid
);
1909 /****************************************************************************
1911 WARNING: if you open with O_WRONLY then getattrE won't work!
1912 ****************************************************************************/
1914 struct cli_open_state
{
1920 static void cli_open_done(struct tevent_req
*subreq
);
1922 struct tevent_req
*cli_open_create(TALLOC_CTX
*mem_ctx
,
1923 struct event_context
*ev
,
1924 struct cli_state
*cli
, const char *fname
,
1925 int flags
, int share_mode
,
1926 struct tevent_req
**psmbreq
)
1928 struct tevent_req
*req
, *subreq
;
1929 struct cli_open_state
*state
;
1931 unsigned accessmode
;
1932 uint8_t additional_flags
;
1935 req
= tevent_req_create(mem_ctx
, &state
, struct cli_open_state
);
1941 if (flags
& O_CREAT
) {
1944 if (!(flags
& O_EXCL
)) {
1945 if (flags
& O_TRUNC
)
1951 accessmode
= (share_mode
<<4);
1953 if ((flags
& O_ACCMODE
) == O_RDWR
) {
1955 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
1960 if ((flags
& O_SYNC
) == O_SYNC
) {
1961 accessmode
|= (1<<14);
1965 if (share_mode
== DENY_FCB
) {
1969 SCVAL(state
->vwv
+ 0, 0, 0xFF);
1970 SCVAL(state
->vwv
+ 0, 1, 0);
1971 SSVAL(state
->vwv
+ 1, 0, 0);
1972 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
1973 SSVAL(state
->vwv
+ 3, 0, accessmode
);
1974 SSVAL(state
->vwv
+ 4, 0, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
1975 SSVAL(state
->vwv
+ 5, 0, 0);
1976 SIVAL(state
->vwv
+ 6, 0, 0);
1977 SSVAL(state
->vwv
+ 8, 0, openfn
);
1978 SIVAL(state
->vwv
+ 9, 0, 0);
1979 SIVAL(state
->vwv
+ 11, 0, 0);
1980 SIVAL(state
->vwv
+ 13, 0, 0);
1982 additional_flags
= 0;
1984 if (cli
->use_oplocks
) {
1985 /* if using oplocks then ask for a batch oplock via
1986 core and extended methods */
1988 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
1989 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
1992 bytes
= talloc_array(state
, uint8_t, 0);
1993 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1994 strlen(fname
)+1, NULL
);
1996 if (tevent_req_nomem(bytes
, req
)) {
1997 return tevent_req_post(req
, ev
);
2000 state
->bytes
.iov_base
= (void *)bytes
;
2001 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2003 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2004 15, state
->vwv
, 1, &state
->bytes
);
2005 if (subreq
== NULL
) {
2009 tevent_req_set_callback(subreq
, cli_open_done
, req
);
2014 struct tevent_req
*cli_open_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
2015 struct cli_state
*cli
, const char *fname
,
2016 int flags
, int share_mode
)
2018 struct tevent_req
*req
, *subreq
;
2021 req
= cli_open_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2027 status
= cli_smb_req_send(subreq
);
2028 if (tevent_req_nterror(req
, status
)) {
2029 return tevent_req_post(req
, ev
);
2034 static void cli_open_done(struct tevent_req
*subreq
)
2036 struct tevent_req
*req
= tevent_req_callback_data(
2037 subreq
, struct tevent_req
);
2038 struct cli_open_state
*state
= tevent_req_data(
2039 req
, struct cli_open_state
);
2045 status
= cli_smb_recv(subreq
, state
, &inbuf
, 3, &wct
, &vwv
, NULL
,
2047 TALLOC_FREE(subreq
);
2048 if (tevent_req_nterror(req
, status
)) {
2051 state
->fnum
= SVAL(vwv
+2, 0);
2052 tevent_req_done(req
);
2055 NTSTATUS
cli_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2057 struct cli_open_state
*state
= tevent_req_data(
2058 req
, struct cli_open_state
);
2061 if (tevent_req_is_nterror(req
, &status
)) {
2064 *pfnum
= state
->fnum
;
2065 return NT_STATUS_OK
;
2068 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2069 int share_mode
, uint16_t *pfnum
)
2071 TALLOC_CTX
*frame
= talloc_stackframe();
2072 struct event_context
*ev
;
2073 struct tevent_req
*req
;
2074 NTSTATUS status
= NT_STATUS_OK
;
2076 if (cli_has_async_calls(cli
)) {
2078 * Can't use sync call while an async call is in flight
2080 status
= NT_STATUS_INVALID_PARAMETER
;
2084 ev
= event_context_init(frame
);
2086 status
= NT_STATUS_NO_MEMORY
;
2090 req
= cli_open_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2092 status
= NT_STATUS_NO_MEMORY
;
2096 if (!tevent_req_poll(req
, ev
)) {
2097 status
= map_nt_error_from_unix(errno
);
2101 status
= cli_open_recv(req
, pfnum
);
2107 /****************************************************************************
2109 ****************************************************************************/
2111 struct cli_close_state
{
2115 static void cli_close_done(struct tevent_req
*subreq
);
2117 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2118 struct event_context
*ev
,
2119 struct cli_state
*cli
,
2121 struct tevent_req
**psubreq
)
2123 struct tevent_req
*req
, *subreq
;
2124 struct cli_close_state
*state
;
2126 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2131 SSVAL(state
->vwv
+0, 0, fnum
);
2132 SIVALS(state
->vwv
+1, 0, -1);
2134 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2136 if (subreq
== NULL
) {
2140 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2145 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2146 struct event_context
*ev
,
2147 struct cli_state
*cli
,
2150 struct tevent_req
*req
, *subreq
;
2153 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2158 status
= cli_smb_req_send(subreq
);
2159 if (tevent_req_nterror(req
, status
)) {
2160 return tevent_req_post(req
, ev
);
2165 static void cli_close_done(struct tevent_req
*subreq
)
2167 struct tevent_req
*req
= tevent_req_callback_data(
2168 subreq
, struct tevent_req
);
2171 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2172 TALLOC_FREE(subreq
);
2173 if (tevent_req_nterror(req
, status
)) {
2176 tevent_req_done(req
);
2179 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2181 return tevent_req_simple_recv_ntstatus(req
);
2184 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2186 TALLOC_CTX
*frame
= talloc_stackframe();
2187 struct event_context
*ev
;
2188 struct tevent_req
*req
;
2189 NTSTATUS status
= NT_STATUS_OK
;
2191 if (cli_has_async_calls(cli
)) {
2193 * Can't use sync call while an async call is in flight
2195 status
= NT_STATUS_INVALID_PARAMETER
;
2199 ev
= event_context_init(frame
);
2201 status
= NT_STATUS_NO_MEMORY
;
2205 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2207 status
= NT_STATUS_NO_MEMORY
;
2211 if (!tevent_req_poll(req
, ev
)) {
2212 status
= map_nt_error_from_unix(errno
);
2216 status
= cli_close_recv(req
);
2222 /****************************************************************************
2223 Truncate a file to a specified size
2224 ****************************************************************************/
2226 struct ftrunc_state
{
2232 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2234 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2235 NULL
, 0, NULL
, NULL
, 0, NULL
);
2236 tevent_req_simple_finish_ntstatus(subreq
, status
);
2239 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2240 struct event_context
*ev
,
2241 struct cli_state
*cli
,
2245 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2246 struct ftrunc_state
*state
= NULL
;
2248 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2253 /* Setup setup word. */
2254 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2256 /* Setup param array. */
2257 SSVAL(state
->param
,0,fnum
);
2258 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2259 SSVAL(state
->param
,4,0);
2261 /* Setup data array. */
2262 SBVAL(state
->data
, 0, size
);
2264 subreq
= cli_trans_send(state
, /* mem ctx. */
2265 ev
, /* event ctx. */
2266 cli
, /* cli_state. */
2267 SMBtrans2
, /* cmd. */
2268 NULL
, /* pipe name. */
2272 &state
->setup
, /* setup. */
2273 1, /* num setup uint16_t words. */
2274 0, /* max returned setup. */
2275 state
->param
, /* param. */
2277 2, /* max returned param. */
2278 state
->data
, /* data. */
2280 0); /* max returned data. */
2282 if (tevent_req_nomem(subreq
, req
)) {
2283 return tevent_req_post(req
, ev
);
2285 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2289 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2291 return tevent_req_simple_recv_ntstatus(req
);
2294 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2296 TALLOC_CTX
*frame
= talloc_stackframe();
2297 struct event_context
*ev
= NULL
;
2298 struct tevent_req
*req
= NULL
;
2299 NTSTATUS status
= NT_STATUS_OK
;
2301 if (cli_has_async_calls(cli
)) {
2303 * Can't use sync call while an async call is in flight
2305 status
= NT_STATUS_INVALID_PARAMETER
;
2309 ev
= event_context_init(frame
);
2311 status
= NT_STATUS_NO_MEMORY
;
2315 req
= cli_ftruncate_send(frame
,
2321 status
= NT_STATUS_NO_MEMORY
;
2325 if (!tevent_req_poll(req
, ev
)) {
2326 status
= map_nt_error_from_unix(errno
);
2330 status
= cli_ftruncate_recv(req
);
2337 /****************************************************************************
2338 send a lock with a specified locktype
2339 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2340 ****************************************************************************/
2342 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2343 uint32_t offset
, uint32_t len
,
2344 int timeout
, unsigned char locktype
)
2351 SCVAL(vwv
+ 0, 0, 0xff);
2352 SCVAL(vwv
+ 0, 1, 0);
2353 SSVAL(vwv
+ 1, 0, 0);
2354 SSVAL(vwv
+ 2, 0, fnum
);
2355 SCVAL(vwv
+ 3, 0, locktype
);
2356 SCVAL(vwv
+ 3, 1, 0);
2357 SIVALS(vwv
+ 4, 0, timeout
);
2358 SSVAL(vwv
+ 6, 0, 0);
2359 SSVAL(vwv
+ 7, 0, 1);
2361 SSVAL(bytes
, 0, cli
->pid
);
2362 SIVAL(bytes
, 2, offset
);
2363 SIVAL(bytes
, 6, len
);
2365 saved_timeout
= cli
->timeout
;
2368 cli
->timeout
= (timeout
== -1)
2369 ? 0x7FFFFFFF : (timeout
+ 2*1000);
2372 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2373 10, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2375 cli
->timeout
= saved_timeout
;
2380 /****************************************************************************
2382 note that timeout is in units of 2 milliseconds
2383 ****************************************************************************/
2385 bool cli_lock(struct cli_state
*cli
, uint16_t fnum
,
2386 uint32_t offset
, uint32_t len
, int timeout
,
2387 enum brl_type lock_type
)
2391 status
= cli_locktype(cli
, fnum
, offset
, len
, timeout
,
2392 (lock_type
== READ_LOCK
? 1 : 0));
2393 return NT_STATUS_IS_OK(status
);
2396 /****************************************************************************
2398 ****************************************************************************/
2400 struct cli_unlock_state
{
2405 static void cli_unlock_done(struct tevent_req
*subreq
);
2407 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2408 struct event_context
*ev
,
2409 struct cli_state
*cli
,
2415 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2416 struct cli_unlock_state
*state
= NULL
;
2417 uint8_t additional_flags
= 0;
2419 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2424 SCVAL(state
->vwv
+0, 0, 0xFF);
2425 SSVAL(state
->vwv
+2, 0, fnum
);
2426 SCVAL(state
->vwv
+3, 0, 0);
2427 SIVALS(state
->vwv
+4, 0, 0);
2428 SSVAL(state
->vwv
+6, 0, 1);
2429 SSVAL(state
->vwv
+7, 0, 0);
2431 SSVAL(state
->data
, 0, cli
->pid
);
2432 SIVAL(state
->data
, 2, offset
);
2433 SIVAL(state
->data
, 6, len
);
2435 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2436 8, state
->vwv
, 10, state
->data
);
2437 if (tevent_req_nomem(subreq
, req
)) {
2438 return tevent_req_post(req
, ev
);
2440 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2444 static void cli_unlock_done(struct tevent_req
*subreq
)
2446 struct tevent_req
*req
= tevent_req_callback_data(
2447 subreq
, struct tevent_req
);
2450 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2451 TALLOC_FREE(subreq
);
2452 if (tevent_req_nterror(req
, status
)) {
2455 tevent_req_done(req
);
2458 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2460 return tevent_req_simple_recv_ntstatus(req
);
2463 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2468 TALLOC_CTX
*frame
= talloc_stackframe();
2469 struct event_context
*ev
;
2470 struct tevent_req
*req
;
2471 NTSTATUS status
= NT_STATUS_OK
;
2473 if (cli_has_async_calls(cli
)) {
2475 * Can't use sync call while an async call is in flight
2477 status
= NT_STATUS_INVALID_PARAMETER
;
2481 ev
= event_context_init(frame
);
2483 status
= NT_STATUS_NO_MEMORY
;
2487 req
= cli_unlock_send(frame
, ev
, cli
,
2490 status
= NT_STATUS_NO_MEMORY
;
2494 if (!tevent_req_poll(req
, ev
)) {
2495 status
= map_nt_error_from_unix(errno
);
2499 status
= cli_unlock_recv(req
);
2506 /****************************************************************************
2507 Lock a file with 64 bit offsets.
2508 ****************************************************************************/
2510 bool cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2511 uint64_t offset
, uint64_t len
, int timeout
,
2512 enum brl_type lock_type
)
2516 int saved_timeout
= cli
->timeout
;
2520 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
2521 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2524 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2525 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2527 SCVAL(vwv
+ 0, 0, 0xff);
2528 SCVAL(vwv
+ 0, 1, 0);
2529 SSVAL(vwv
+ 1, 0, 0);
2530 SSVAL(vwv
+ 2, 0, fnum
);
2531 SCVAL(vwv
+ 3, 0, ltype
);
2532 SCVAL(vwv
+ 3, 1, 0);
2533 SIVALS(vwv
+ 4, 0, timeout
);
2534 SSVAL(vwv
+ 6, 0, 0);
2535 SSVAL(vwv
+ 7, 0, 1);
2537 SIVAL(bytes
, 0, cli
->pid
);
2538 SOFF_T_R(bytes
, 4, offset
);
2539 SOFF_T_R(bytes
, 12, len
);
2541 saved_timeout
= cli
->timeout
;
2544 cli
->timeout
= (timeout
== -1)
2545 ? 0x7FFFFFFF : (timeout
+ 2*1000);
2548 status
= cli_smb(talloc_tos(), cli
, SMBlockingX
, 0, 8, vwv
,
2549 20, bytes
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2551 cli
->timeout
= saved_timeout
;
2553 return NT_STATUS_IS_OK(status
);
2556 /****************************************************************************
2557 Unlock a file with 64 bit offsets.
2558 ****************************************************************************/
2560 struct cli_unlock64_state
{
2565 static void cli_unlock64_done(struct tevent_req
*subreq
);
2567 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2568 struct event_context
*ev
,
2569 struct cli_state
*cli
,
2575 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2576 struct cli_unlock64_state
*state
= NULL
;
2577 uint8_t additional_flags
= 0;
2579 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2584 SCVAL(state
->vwv
+0, 0, 0xff);
2585 SSVAL(state
->vwv
+2, 0, fnum
);
2586 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2587 SIVALS(state
->vwv
+4, 0, 0);
2588 SSVAL(state
->vwv
+6, 0, 1);
2589 SSVAL(state
->vwv
+7, 0, 0);
2591 SIVAL(state
->data
, 0, cli
->pid
);
2592 SOFF_T_R(state
->data
, 4, offset
);
2593 SOFF_T_R(state
->data
, 12, len
);
2595 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2596 8, state
->vwv
, 20, state
->data
);
2597 if (tevent_req_nomem(subreq
, req
)) {
2598 return tevent_req_post(req
, ev
);
2600 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2604 static void cli_unlock64_done(struct tevent_req
*subreq
)
2606 struct tevent_req
*req
= tevent_req_callback_data(
2607 subreq
, struct tevent_req
);
2610 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
2611 TALLOC_FREE(subreq
);
2612 if (tevent_req_nterror(req
, status
)) {
2615 tevent_req_done(req
);
2618 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2620 return tevent_req_simple_recv_ntstatus(req
);
2623 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2628 TALLOC_CTX
*frame
= talloc_stackframe();
2629 struct event_context
*ev
;
2630 struct tevent_req
*req
;
2631 NTSTATUS status
= NT_STATUS_OK
;
2633 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
2634 return cli_unlock(cli
, fnum
, offset
, len
);
2637 if (cli_has_async_calls(cli
)) {
2639 * Can't use sync call while an async call is in flight
2641 status
= NT_STATUS_INVALID_PARAMETER
;
2645 ev
= event_context_init(frame
);
2647 status
= NT_STATUS_NO_MEMORY
;
2651 req
= cli_unlock64_send(frame
, ev
, cli
,
2654 status
= NT_STATUS_NO_MEMORY
;
2658 if (!tevent_req_poll(req
, ev
)) {
2659 status
= map_nt_error_from_unix(errno
);
2663 status
= cli_unlock64_recv(req
);
2670 /****************************************************************************
2671 Get/unlock a POSIX lock on a file - internal function.
2672 ****************************************************************************/
2674 struct posix_lock_state
{
2677 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
2680 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
2682 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
2683 NULL
, 0, NULL
, NULL
, 0, NULL
);
2684 tevent_req_simple_finish_ntstatus(subreq
, status
);
2687 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
2688 struct event_context
*ev
,
2689 struct cli_state
*cli
,
2694 enum brl_type lock_type
)
2696 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2697 struct posix_lock_state
*state
= NULL
;
2699 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
2704 /* Setup setup word. */
2705 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2707 /* Setup param array. */
2708 SSVAL(&state
->param
, 0, fnum
);
2709 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
2711 /* Setup data array. */
2712 switch (lock_type
) {
2714 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
2715 POSIX_LOCK_TYPE_READ
);
2718 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
2719 POSIX_LOCK_TYPE_WRITE
);
2722 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
2723 POSIX_LOCK_TYPE_UNLOCK
);
2730 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
2731 POSIX_LOCK_FLAG_WAIT
);
2733 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
2734 POSIX_LOCK_FLAG_NOWAIT
);
2737 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
2738 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
2739 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
2741 subreq
= cli_trans_send(state
, /* mem ctx. */
2742 ev
, /* event ctx. */
2743 cli
, /* cli_state. */
2744 SMBtrans2
, /* cmd. */
2745 NULL
, /* pipe name. */
2749 &state
->setup
, /* setup. */
2750 1, /* num setup uint16_t words. */
2751 0, /* max returned setup. */
2752 state
->param
, /* param. */
2754 2, /* max returned param. */
2755 state
->data
, /* data. */
2756 POSIX_LOCK_DATA_SIZE
, /* num data. */
2757 0); /* max returned data. */
2759 if (tevent_req_nomem(subreq
, req
)) {
2760 return tevent_req_post(req
, ev
);
2762 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
2766 /****************************************************************************
2768 ****************************************************************************/
2770 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
2771 struct event_context
*ev
,
2772 struct cli_state
*cli
,
2777 enum brl_type lock_type
)
2779 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
2780 wait_lock
, lock_type
);
2783 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
2785 return tevent_req_simple_recv_ntstatus(req
);
2788 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
2789 uint64_t offset
, uint64_t len
,
2790 bool wait_lock
, enum brl_type lock_type
)
2792 TALLOC_CTX
*frame
= talloc_stackframe();
2793 struct event_context
*ev
= NULL
;
2794 struct tevent_req
*req
= NULL
;
2795 NTSTATUS status
= NT_STATUS_OK
;
2797 if (cli_has_async_calls(cli
)) {
2799 * Can't use sync call while an async call is in flight
2801 status
= NT_STATUS_INVALID_PARAMETER
;
2805 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
2806 status
= NT_STATUS_INVALID_PARAMETER
;
2810 ev
= event_context_init(frame
);
2812 status
= NT_STATUS_NO_MEMORY
;
2816 req
= cli_posix_lock_send(frame
,
2825 status
= NT_STATUS_NO_MEMORY
;
2829 if (!tevent_req_poll(req
, ev
)) {
2830 status
= map_nt_error_from_unix(errno
);
2834 status
= cli_posix_lock_recv(req
);
2841 /****************************************************************************
2842 POSIX Unlock a file.
2843 ****************************************************************************/
2845 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
2846 struct event_context
*ev
,
2847 struct cli_state
*cli
,
2852 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
2853 false, UNLOCK_LOCK
);
2856 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
2858 return tevent_req_simple_recv_ntstatus(req
);
2861 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
2863 TALLOC_CTX
*frame
= talloc_stackframe();
2864 struct event_context
*ev
= NULL
;
2865 struct tevent_req
*req
= NULL
;
2866 NTSTATUS status
= NT_STATUS_OK
;
2868 if (cli_has_async_calls(cli
)) {
2870 * Can't use sync call while an async call is in flight
2872 status
= NT_STATUS_INVALID_PARAMETER
;
2876 ev
= event_context_init(frame
);
2878 status
= NT_STATUS_NO_MEMORY
;
2882 req
= cli_posix_unlock_send(frame
,
2889 status
= NT_STATUS_NO_MEMORY
;
2893 if (!tevent_req_poll(req
, ev
)) {
2894 status
= map_nt_error_from_unix(errno
);
2898 status
= cli_posix_unlock_recv(req
);
2905 /****************************************************************************
2906 Do a SMBgetattrE call.
2907 ****************************************************************************/
2909 static void cli_getattrE_done(struct tevent_req
*subreq
);
2911 struct cli_getattrE_state
{
2921 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
2922 struct event_context
*ev
,
2923 struct cli_state
*cli
,
2926 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2927 struct cli_getattrE_state
*state
= NULL
;
2928 uint8_t additional_flags
= 0;
2930 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
2935 state
->zone_offset
= cli
->serverzone
;
2936 SSVAL(state
->vwv
+0,0,fnum
);
2938 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
2939 1, state
->vwv
, 0, NULL
);
2940 if (tevent_req_nomem(subreq
, req
)) {
2941 return tevent_req_post(req
, ev
);
2943 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
2947 static void cli_getattrE_done(struct tevent_req
*subreq
)
2949 struct tevent_req
*req
= tevent_req_callback_data(
2950 subreq
, struct tevent_req
);
2951 struct cli_getattrE_state
*state
= tevent_req_data(
2952 req
, struct cli_getattrE_state
);
2954 uint16_t *vwv
= NULL
;
2958 status
= cli_smb_recv(subreq
, state
, &inbuf
, 11, &wct
, &vwv
,
2960 TALLOC_FREE(subreq
);
2961 if (tevent_req_nterror(req
, status
)) {
2965 state
->size
= (SMB_OFF_T
)IVAL(vwv
+6,0);
2966 state
->attr
= SVAL(vwv
+10,0);
2967 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
2968 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
2969 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
2971 tevent_req_done(req
);
2974 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
2977 time_t *change_time
,
2978 time_t *access_time
,
2981 struct cli_getattrE_state
*state
= tevent_req_data(
2982 req
, struct cli_getattrE_state
);
2985 if (tevent_req_is_nterror(req
, &status
)) {
2989 *attr
= state
->attr
;
2992 *size
= state
->size
;
2995 *change_time
= state
->change_time
;
2998 *access_time
= state
->access_time
;
3001 *write_time
= state
->write_time
;
3003 return NT_STATUS_OK
;
3006 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3010 time_t *change_time
,
3011 time_t *access_time
,
3014 TALLOC_CTX
*frame
= talloc_stackframe();
3015 struct event_context
*ev
= NULL
;
3016 struct tevent_req
*req
= NULL
;
3017 NTSTATUS status
= NT_STATUS_OK
;
3019 if (cli_has_async_calls(cli
)) {
3021 * Can't use sync call while an async call is in flight
3023 status
= NT_STATUS_INVALID_PARAMETER
;
3027 ev
= event_context_init(frame
);
3029 status
= NT_STATUS_NO_MEMORY
;
3033 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3035 status
= NT_STATUS_NO_MEMORY
;
3039 if (!tevent_req_poll(req
, ev
)) {
3040 status
= map_nt_error_from_unix(errno
);
3044 status
= cli_getattrE_recv(req
,
3056 /****************************************************************************
3058 ****************************************************************************/
3060 static void cli_getatr_done(struct tevent_req
*subreq
);
3062 struct cli_getatr_state
{
3069 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3070 struct event_context
*ev
,
3071 struct cli_state
*cli
,
3074 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3075 struct cli_getatr_state
*state
= NULL
;
3076 uint8_t additional_flags
= 0;
3077 uint8_t *bytes
= NULL
;
3079 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3084 state
->zone_offset
= cli
->serverzone
;
3086 bytes
= talloc_array(state
, uint8_t, 1);
3087 if (tevent_req_nomem(bytes
, req
)) {
3088 return tevent_req_post(req
, ev
);
3091 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3092 strlen(fname
)+1, NULL
);
3094 if (tevent_req_nomem(bytes
, req
)) {
3095 return tevent_req_post(req
, ev
);
3098 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3099 0, NULL
, talloc_get_size(bytes
), bytes
);
3100 if (tevent_req_nomem(subreq
, req
)) {
3101 return tevent_req_post(req
, ev
);
3103 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3107 static void cli_getatr_done(struct tevent_req
*subreq
)
3109 struct tevent_req
*req
= tevent_req_callback_data(
3110 subreq
, struct tevent_req
);
3111 struct cli_getatr_state
*state
= tevent_req_data(
3112 req
, struct cli_getatr_state
);
3114 uint16_t *vwv
= NULL
;
3118 status
= cli_smb_recv(subreq
, state
, &inbuf
, 4, &wct
, &vwv
, NULL
,
3120 TALLOC_FREE(subreq
);
3121 if (tevent_req_nterror(req
, status
)) {
3125 state
->attr
= SVAL(vwv
+0,0);
3126 state
->size
= (SMB_OFF_T
)IVAL(vwv
+3,0);
3127 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3129 tevent_req_done(req
);
3132 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3137 struct cli_getatr_state
*state
= tevent_req_data(
3138 req
, struct cli_getatr_state
);
3141 if (tevent_req_is_nterror(req
, &status
)) {
3145 *attr
= state
->attr
;
3148 *size
= state
->size
;
3151 *write_time
= state
->write_time
;
3153 return NT_STATUS_OK
;
3156 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3162 TALLOC_CTX
*frame
= talloc_stackframe();
3163 struct event_context
*ev
= NULL
;
3164 struct tevent_req
*req
= NULL
;
3165 NTSTATUS status
= NT_STATUS_OK
;
3167 if (cli_has_async_calls(cli
)) {
3169 * Can't use sync call while an async call is in flight
3171 status
= NT_STATUS_INVALID_PARAMETER
;
3175 ev
= event_context_init(frame
);
3177 status
= NT_STATUS_NO_MEMORY
;
3181 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3183 status
= NT_STATUS_NO_MEMORY
;
3187 if (!tevent_req_poll(req
, ev
)) {
3188 status
= map_nt_error_from_unix(errno
);
3192 status
= cli_getatr_recv(req
,
3202 /****************************************************************************
3203 Do a SMBsetattrE call.
3204 ****************************************************************************/
3206 static void cli_setattrE_done(struct tevent_req
*subreq
);
3208 struct cli_setattrE_state
{
3212 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3213 struct event_context
*ev
,
3214 struct cli_state
*cli
,
3220 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3221 struct cli_setattrE_state
*state
= NULL
;
3222 uint8_t additional_flags
= 0;
3224 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3229 SSVAL(state
->vwv
+0, 0, fnum
);
3230 push_dos_date2((uint8_t *)&state
->vwv
[1], 0, change_time
,
3232 push_dos_date2((uint8_t *)&state
->vwv
[3], 0, access_time
,
3234 push_dos_date2((uint8_t *)&state
->vwv
[5], 0, write_time
,
3237 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3238 7, state
->vwv
, 0, NULL
);
3239 if (tevent_req_nomem(subreq
, req
)) {
3240 return tevent_req_post(req
, ev
);
3242 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3246 static void cli_setattrE_done(struct tevent_req
*subreq
)
3248 struct tevent_req
*req
= tevent_req_callback_data(
3249 subreq
, struct tevent_req
);
3252 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3253 TALLOC_FREE(subreq
);
3254 if (tevent_req_nterror(req
, status
)) {
3257 tevent_req_done(req
);
3260 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3262 return tevent_req_simple_recv_ntstatus(req
);
3265 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3271 TALLOC_CTX
*frame
= talloc_stackframe();
3272 struct event_context
*ev
= NULL
;
3273 struct tevent_req
*req
= NULL
;
3274 NTSTATUS status
= NT_STATUS_OK
;
3276 if (cli_has_async_calls(cli
)) {
3278 * Can't use sync call while an async call is in flight
3280 status
= NT_STATUS_INVALID_PARAMETER
;
3284 ev
= event_context_init(frame
);
3286 status
= NT_STATUS_NO_MEMORY
;
3290 req
= cli_setattrE_send(frame
, ev
,
3298 status
= NT_STATUS_NO_MEMORY
;
3302 if (!tevent_req_poll(req
, ev
)) {
3303 status
= map_nt_error_from_unix(errno
);
3307 status
= cli_setattrE_recv(req
);
3314 /****************************************************************************
3315 Do a SMBsetatr call.
3316 ****************************************************************************/
3318 static void cli_setatr_done(struct tevent_req
*subreq
);
3320 struct cli_setatr_state
{
3324 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3325 struct event_context
*ev
,
3326 struct cli_state
*cli
,
3331 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3332 struct cli_setatr_state
*state
= NULL
;
3333 uint8_t additional_flags
= 0;
3334 uint8_t *bytes
= NULL
;
3336 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3341 SSVAL(state
->vwv
+0, 0, attr
);
3342 push_dos_date3((uint8_t *)&state
->vwv
[1], 0, mtime
, cli
->serverzone
);
3344 bytes
= talloc_array(state
, uint8_t, 1);
3345 if (tevent_req_nomem(bytes
, req
)) {
3346 return tevent_req_post(req
, ev
);
3349 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3350 strlen(fname
)+1, NULL
);
3351 if (tevent_req_nomem(bytes
, req
)) {
3352 return tevent_req_post(req
, ev
);
3354 bytes
= talloc_realloc(state
, bytes
, uint8_t,
3355 talloc_get_size(bytes
)+1);
3356 if (tevent_req_nomem(bytes
, req
)) {
3357 return tevent_req_post(req
, ev
);
3360 bytes
[talloc_get_size(bytes
)-1] = 4;
3361 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "",
3363 if (tevent_req_nomem(bytes
, req
)) {
3364 return tevent_req_post(req
, ev
);
3367 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3368 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3369 if (tevent_req_nomem(subreq
, req
)) {
3370 return tevent_req_post(req
, ev
);
3372 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3376 static void cli_setatr_done(struct tevent_req
*subreq
)
3378 struct tevent_req
*req
= tevent_req_callback_data(
3379 subreq
, struct tevent_req
);
3382 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3383 TALLOC_FREE(subreq
);
3384 if (tevent_req_nterror(req
, status
)) {
3387 tevent_req_done(req
);
3390 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3392 return tevent_req_simple_recv_ntstatus(req
);
3395 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3400 TALLOC_CTX
*frame
= talloc_stackframe();
3401 struct event_context
*ev
= NULL
;
3402 struct tevent_req
*req
= NULL
;
3403 NTSTATUS status
= NT_STATUS_OK
;
3405 if (cli_has_async_calls(cli
)) {
3407 * Can't use sync call while an async call is in flight
3409 status
= NT_STATUS_INVALID_PARAMETER
;
3413 ev
= event_context_init(frame
);
3415 status
= NT_STATUS_NO_MEMORY
;
3419 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3421 status
= NT_STATUS_NO_MEMORY
;
3425 if (!tevent_req_poll(req
, ev
)) {
3426 status
= map_nt_error_from_unix(errno
);
3430 status
= cli_setatr_recv(req
);
3437 /****************************************************************************
3438 Check for existance of a dir.
3439 ****************************************************************************/
3441 static void cli_chkpath_done(struct tevent_req
*subreq
);
3443 struct cli_chkpath_state
{
3447 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3448 struct event_context
*ev
,
3449 struct cli_state
*cli
,
3452 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3453 struct cli_chkpath_state
*state
= NULL
;
3454 uint8_t additional_flags
= 0;
3455 uint8_t *bytes
= NULL
;
3457 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3462 bytes
= talloc_array(state
, uint8_t, 1);
3463 if (tevent_req_nomem(bytes
, req
)) {
3464 return tevent_req_post(req
, ev
);
3467 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3468 strlen(fname
)+1, NULL
);
3470 if (tevent_req_nomem(bytes
, req
)) {
3471 return tevent_req_post(req
, ev
);
3474 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3475 0, NULL
, talloc_get_size(bytes
), bytes
);
3476 if (tevent_req_nomem(subreq
, req
)) {
3477 return tevent_req_post(req
, ev
);
3479 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3483 static void cli_chkpath_done(struct tevent_req
*subreq
)
3485 struct tevent_req
*req
= tevent_req_callback_data(
3486 subreq
, struct tevent_req
);
3489 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3490 TALLOC_FREE(subreq
);
3491 if (tevent_req_nterror(req
, status
)) {
3494 tevent_req_done(req
);
3497 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3499 return tevent_req_simple_recv_ntstatus(req
);
3502 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3504 TALLOC_CTX
*frame
= talloc_stackframe();
3505 struct event_context
*ev
= NULL
;
3506 struct tevent_req
*req
= NULL
;
3508 NTSTATUS status
= NT_STATUS_OK
;
3510 if (cli_has_async_calls(cli
)) {
3512 * Can't use sync call while an async call is in flight
3514 status
= NT_STATUS_INVALID_PARAMETER
;
3518 path2
= talloc_strdup(frame
, path
);
3520 status
= NT_STATUS_NO_MEMORY
;
3523 trim_char(path2
,'\0','\\');
3525 path2
= talloc_strdup(frame
, "\\");
3527 status
= NT_STATUS_NO_MEMORY
;
3532 ev
= event_context_init(frame
);
3534 status
= NT_STATUS_NO_MEMORY
;
3538 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3540 status
= NT_STATUS_NO_MEMORY
;
3544 if (!tevent_req_poll(req
, ev
)) {
3545 status
= map_nt_error_from_unix(errno
);
3549 status
= cli_chkpath_recv(req
);
3556 /****************************************************************************
3558 ****************************************************************************/
3560 static void cli_dskattr_done(struct tevent_req
*subreq
);
3562 struct cli_dskattr_state
{
3568 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3569 struct event_context
*ev
,
3570 struct cli_state
*cli
)
3572 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3573 struct cli_dskattr_state
*state
= NULL
;
3574 uint8_t additional_flags
= 0;
3576 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3581 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3583 if (tevent_req_nomem(subreq
, req
)) {
3584 return tevent_req_post(req
, ev
);
3586 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3590 static void cli_dskattr_done(struct tevent_req
*subreq
)
3592 struct tevent_req
*req
= tevent_req_callback_data(
3593 subreq
, struct tevent_req
);
3594 struct cli_dskattr_state
*state
= tevent_req_data(
3595 req
, struct cli_dskattr_state
);
3597 uint16_t *vwv
= NULL
;
3601 status
= cli_smb_recv(subreq
, state
, &inbuf
, 4, &wct
, &vwv
, NULL
,
3603 TALLOC_FREE(subreq
);
3604 if (tevent_req_nterror(req
, status
)) {
3607 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3608 state
->total
= SVAL(vwv
+0, 0);
3609 state
->avail
= SVAL(vwv
+3, 0);
3610 tevent_req_done(req
);
3613 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3615 struct cli_dskattr_state
*state
= tevent_req_data(
3616 req
, struct cli_dskattr_state
);
3619 if (tevent_req_is_nterror(req
, &status
)) {
3622 *bsize
= state
->bsize
;
3623 *total
= state
->total
;
3624 *avail
= state
->avail
;
3625 return NT_STATUS_OK
;
3628 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3630 TALLOC_CTX
*frame
= talloc_stackframe();
3631 struct event_context
*ev
= NULL
;
3632 struct tevent_req
*req
= NULL
;
3633 NTSTATUS status
= NT_STATUS_OK
;
3635 if (cli_has_async_calls(cli
)) {
3637 * Can't use sync call while an async call is in flight
3639 status
= NT_STATUS_INVALID_PARAMETER
;
3643 ev
= event_context_init(frame
);
3645 status
= NT_STATUS_NO_MEMORY
;
3649 req
= cli_dskattr_send(frame
, ev
, cli
);
3651 status
= NT_STATUS_NO_MEMORY
;
3655 if (!tevent_req_poll(req
, ev
)) {
3656 status
= map_nt_error_from_unix(errno
);
3660 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
3667 /****************************************************************************
3668 Create and open a temporary file.
3669 ****************************************************************************/
3671 static void cli_ctemp_done(struct tevent_req
*subreq
);
3673 struct ctemp_state
{
3679 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
3680 struct event_context
*ev
,
3681 struct cli_state
*cli
,
3684 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3685 struct ctemp_state
*state
= NULL
;
3686 uint8_t additional_flags
= 0;
3687 uint8_t *bytes
= NULL
;
3689 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
3694 SSVAL(state
->vwv
,0,0);
3695 SIVALS(state
->vwv
+1,0,-1);
3697 bytes
= talloc_array(state
, uint8_t, 1);
3698 if (tevent_req_nomem(bytes
, req
)) {
3699 return tevent_req_post(req
, ev
);
3702 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), path
,
3703 strlen(path
)+1, NULL
);
3704 if (tevent_req_nomem(bytes
, req
)) {
3705 return tevent_req_post(req
, ev
);
3708 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
3709 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
3710 if (tevent_req_nomem(subreq
, req
)) {
3711 return tevent_req_post(req
, ev
);
3713 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
3717 static void cli_ctemp_done(struct tevent_req
*subreq
)
3719 struct tevent_req
*req
= tevent_req_callback_data(
3720 subreq
, struct tevent_req
);
3721 struct ctemp_state
*state
= tevent_req_data(
3722 req
, struct ctemp_state
);
3726 uint32_t num_bytes
= 0;
3727 uint8_t *bytes
= NULL
;
3730 status
= cli_smb_recv(subreq
, state
, &inbuf
, 1, &wcnt
, &vwv
,
3731 &num_bytes
, &bytes
);
3732 TALLOC_FREE(subreq
);
3733 if (tevent_req_nterror(req
, status
)) {
3737 state
->fnum
= SVAL(vwv
+0, 0);
3739 /* From W2K3, the result is just the ASCII name */
3740 if (num_bytes
< 2) {
3741 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
3745 if (pull_string_talloc(state
,
3752 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
3755 tevent_req_done(req
);
3758 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
3763 struct ctemp_state
*state
= tevent_req_data(req
,
3764 struct ctemp_state
);
3767 if (tevent_req_is_nterror(req
, &status
)) {
3770 *pfnum
= state
->fnum
;
3771 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
3773 return NT_STATUS_NO_MEMORY
;
3775 return NT_STATUS_OK
;
3778 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
3784 TALLOC_CTX
*frame
= talloc_stackframe();
3785 struct event_context
*ev
;
3786 struct tevent_req
*req
;
3787 NTSTATUS status
= NT_STATUS_OK
;
3789 if (cli_has_async_calls(cli
)) {
3791 * Can't use sync call while an async call is in flight
3793 status
= NT_STATUS_INVALID_PARAMETER
;
3797 ev
= event_context_init(frame
);
3799 status
= NT_STATUS_NO_MEMORY
;
3803 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
3805 status
= NT_STATUS_NO_MEMORY
;
3809 if (!tevent_req_poll(req
, ev
)) {
3810 status
= map_nt_error_from_unix(errno
);
3814 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
3822 send a raw ioctl - used by the torture code
3824 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
3829 SSVAL(vwv
+0, 0, fnum
);
3830 SSVAL(vwv
+1, 0, code
>>16);
3831 SSVAL(vwv
+2, 0, (code
&0xFFFF));
3833 status
= cli_smb(talloc_tos(), cli
, SMBioctl
, 0, 3, vwv
, 0, NULL
,
3834 NULL
, 0, NULL
, NULL
, NULL
, NULL
);
3835 if (!NT_STATUS_IS_OK(status
)) {
3838 *blob
= data_blob_null
;
3839 return NT_STATUS_OK
;
3842 /*********************************************************
3843 Set an extended attribute utility fn.
3844 *********************************************************/
3846 static NTSTATUS
cli_set_ea(struct cli_state
*cli
, uint16_t setup_val
,
3847 uint8_t *param
, unsigned int param_len
,
3848 const char *ea_name
,
3849 const char *ea_val
, size_t ea_len
)
3852 unsigned int data_len
= 0;
3853 uint8_t *data
= NULL
;
3855 size_t ea_namelen
= strlen(ea_name
);
3858 SSVAL(setup
, 0, setup_val
);
3860 if (ea_namelen
== 0 && ea_len
== 0) {
3862 data
= (uint8_t *)SMB_MALLOC(data_len
);
3864 return NT_STATUS_NO_MEMORY
;
3867 SIVAL(p
,0,data_len
);
3869 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
3870 data
= (uint8_t *)SMB_MALLOC(data_len
);
3872 return NT_STATUS_NO_MEMORY
;
3875 SIVAL(p
,0,data_len
);
3877 SCVAL(p
, 0, 0); /* EA flags. */
3878 SCVAL(p
, 1, ea_namelen
);
3879 SSVAL(p
, 2, ea_len
);
3880 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
3881 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
3884 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, -1, 0, 0,
3886 param
, param_len
, 2,
3887 data
, data_len
, cli
->max_xmit
,
3889 NULL
, 0, NULL
, /* rsetup */
3890 NULL
, 0, NULL
, /* rparam */
3891 NULL
, 0, NULL
); /* rdata */
3896 /*********************************************************
3897 Set an extended attribute on a pathname.
3898 *********************************************************/
3900 NTSTATUS
cli_set_ea_path(struct cli_state
*cli
, const char *path
,
3901 const char *ea_name
, const char *ea_val
,
3904 unsigned int param_len
= 0;
3906 size_t srclen
= 2*(strlen(path
)+1);
3910 param
= SMB_MALLOC_ARRAY(uint8_t, 6+srclen
+2);
3912 return NT_STATUS_NO_MEMORY
;
3914 memset(param
, '\0', 6);
3915 SSVAL(param
,0,SMB_INFO_SET_EA
);
3916 p
= (char *)(¶m
[6]);
3918 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
3919 param_len
= PTR_DIFF(p
, param
);
3921 status
= cli_set_ea(cli
, TRANSACT2_SETPATHINFO
, param
, param_len
,
3922 ea_name
, ea_val
, ea_len
);
3927 /*********************************************************
3928 Set an extended attribute on an fnum.
3929 *********************************************************/
3931 NTSTATUS
cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
,
3932 const char *ea_name
, const char *ea_val
,
3937 memset(param
, 0, 6);
3938 SSVAL(param
,0,fnum
);
3939 SSVAL(param
,2,SMB_INFO_SET_EA
);
3941 return cli_set_ea(cli
, TRANSACT2_SETFILEINFO
, param
, 6,
3942 ea_name
, ea_val
, ea_len
);
3945 /*********************************************************
3946 Get an extended attribute list utility fn.
3947 *********************************************************/
3949 static bool parse_ea_blob(TALLOC_CTX
*ctx
, const uint8_t *rdata
,
3951 size_t *pnum_eas
, struct ea_struct
**pea_list
)
3953 struct ea_struct
*ea_list
= NULL
;
3958 if (rdata_len
< 4) {
3962 ea_size
= (size_t)IVAL(rdata
,0);
3963 if (ea_size
> rdata_len
) {
3968 /* No EA's present. */
3977 /* Validate the EA list and count it. */
3978 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
3979 unsigned int ea_namelen
= CVAL(p
,1);
3980 unsigned int ea_valuelen
= SVAL(p
,2);
3981 if (ea_namelen
== 0) {
3984 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
3987 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
3988 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
3997 *pnum_eas
= num_eas
;
3999 /* Caller only wants number of EA's. */
4003 ea_list
= talloc_array(ctx
, struct ea_struct
, num_eas
);
4008 ea_size
= (size_t)IVAL(rdata
,0);
4011 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4012 struct ea_struct
*ea
= &ea_list
[num_eas
];
4013 fstring unix_ea_name
;
4014 unsigned int ea_namelen
= CVAL(p
,1);
4015 unsigned int ea_valuelen
= SVAL(p
,2);
4017 ea
->flags
= CVAL(p
,0);
4018 unix_ea_name
[0] = '\0';
4019 pull_ascii(unix_ea_name
, p
+ 4, sizeof(unix_ea_name
), rdata_len
- PTR_DIFF(p
+4, rdata
), STR_TERMINATE
);
4020 ea
->name
= talloc_strdup(ea_list
, unix_ea_name
);
4024 /* Ensure the value is null terminated (in case it's a string). */
4025 ea
->value
= data_blob_talloc(ea_list
, NULL
, ea_valuelen
+ 1);
4026 if (!ea
->value
.data
) {
4030 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4032 ea
->value
.data
[ea_valuelen
] = 0;
4034 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4037 *pea_list
= ea_list
;
4041 TALLOC_FREE(ea_list
);
4045 /*********************************************************
4046 Get an extended attribute list from a pathname.
4047 *********************************************************/
4049 struct cli_get_ea_list_path_state
{
4054 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
);
4056 struct tevent_req
*cli_get_ea_list_path_send(TALLOC_CTX
*mem_ctx
,
4057 struct tevent_context
*ev
,
4058 struct cli_state
*cli
,
4061 struct tevent_req
*req
, *subreq
;
4062 struct cli_get_ea_list_path_state
*state
;
4064 req
= tevent_req_create(mem_ctx
, &state
,
4065 struct cli_get_ea_list_path_state
);
4069 subreq
= cli_qpathinfo_send(state
, ev
, cli
, fname
,
4070 SMB_INFO_QUERY_ALL_EAS
, 4,
4072 if (tevent_req_nomem(subreq
, req
)) {
4073 return tevent_req_post(req
, ev
);
4075 tevent_req_set_callback(subreq
, cli_get_ea_list_path_done
, req
);
4079 static void cli_get_ea_list_path_done(struct tevent_req
*subreq
)
4081 struct tevent_req
*req
= tevent_req_callback_data(
4082 subreq
, struct tevent_req
);
4083 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4084 req
, struct cli_get_ea_list_path_state
);
4087 status
= cli_qpathinfo_recv(subreq
, state
, &state
->data
,
4089 TALLOC_FREE(subreq
);
4090 if (tevent_req_nterror(req
, status
)) {
4093 tevent_req_done(req
);
4096 NTSTATUS
cli_get_ea_list_path_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4097 size_t *pnum_eas
, struct ea_struct
**peas
)
4099 struct cli_get_ea_list_path_state
*state
= tevent_req_data(
4100 req
, struct cli_get_ea_list_path_state
);
4103 if (tevent_req_is_nterror(req
, &status
)) {
4106 if (!parse_ea_blob(mem_ctx
, state
->data
, state
->num_data
,
4108 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4110 return NT_STATUS_OK
;
4113 NTSTATUS
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4116 struct ea_struct
**pea_list
)
4118 TALLOC_CTX
*frame
= talloc_stackframe();
4119 struct event_context
*ev
= NULL
;
4120 struct tevent_req
*req
= NULL
;
4121 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4123 if (cli_has_async_calls(cli
)) {
4125 * Can't use sync call while an async call is in flight
4127 status
= NT_STATUS_INVALID_PARAMETER
;
4130 ev
= event_context_init(frame
);
4134 req
= cli_get_ea_list_path_send(frame
, ev
, cli
, path
);
4138 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4141 status
= cli_get_ea_list_path_recv(req
, ctx
, pnum_eas
, pea_list
);
4147 /****************************************************************************
4148 Convert open "flags" arg to uint32_t on wire.
4149 ****************************************************************************/
4151 static uint32_t open_flags_to_wire(int flags
)
4153 int open_mode
= flags
& O_ACCMODE
;
4156 switch (open_mode
) {
4158 ret
|= SMB_O_WRONLY
;
4165 ret
|= SMB_O_RDONLY
;
4169 if (flags
& O_CREAT
) {
4172 if (flags
& O_EXCL
) {
4175 if (flags
& O_TRUNC
) {
4179 if (flags
& O_SYNC
) {
4183 if (flags
& O_APPEND
) {
4184 ret
|= SMB_O_APPEND
;
4186 #if defined(O_DIRECT)
4187 if (flags
& O_DIRECT
) {
4188 ret
|= SMB_O_DIRECT
;
4191 #if defined(O_DIRECTORY)
4192 if (flags
& O_DIRECTORY
) {
4193 ret
|= SMB_O_DIRECTORY
;
4199 /****************************************************************************
4200 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4201 ****************************************************************************/
4203 struct posix_open_state
{
4207 uint16_t fnum
; /* Out */
4210 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4212 struct tevent_req
*req
= tevent_req_callback_data(
4213 subreq
, struct tevent_req
);
4214 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4219 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4220 NULL
, 0, NULL
, &data
, 12, &num_data
);
4221 TALLOC_FREE(subreq
);
4222 if (tevent_req_nterror(req
, status
)) {
4225 state
->fnum
= SVAL(data
,2);
4226 tevent_req_done(req
);
4229 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4230 struct event_context
*ev
,
4231 struct cli_state
*cli
,
4237 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4238 struct posix_open_state
*state
= NULL
;
4239 uint32_t wire_flags
= open_flags_to_wire(flags
);
4241 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4246 /* Setup setup word. */
4247 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4249 /* Setup param array. */
4250 state
->param
= talloc_array(state
, uint8_t, 6);
4251 if (tevent_req_nomem(state
->param
, req
)) {
4252 return tevent_req_post(req
, ev
);
4254 memset(state
->param
, '\0', 6);
4255 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4257 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
4258 strlen(fname
)+1, NULL
);
4260 if (tevent_req_nomem(state
->param
, req
)) {
4261 return tevent_req_post(req
, ev
);
4264 /* Setup data words. */
4266 wire_flags
|= SMB_O_DIRECTORY
;
4269 SIVAL(state
->data
,0,0); /* No oplock. */
4270 SIVAL(state
->data
,4,wire_flags
);
4271 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4272 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4273 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4275 subreq
= cli_trans_send(state
, /* mem ctx. */
4276 ev
, /* event ctx. */
4277 cli
, /* cli_state. */
4278 SMBtrans2
, /* cmd. */
4279 NULL
, /* pipe name. */
4283 &state
->setup
, /* setup. */
4284 1, /* num setup uint16_t words. */
4285 0, /* max returned setup. */
4286 state
->param
, /* param. */
4287 talloc_get_size(state
->param
),/* num param. */
4288 2, /* max returned param. */
4289 state
->data
, /* data. */
4291 12); /* max returned data. */
4293 if (tevent_req_nomem(subreq
, req
)) {
4294 return tevent_req_post(req
, ev
);
4296 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4300 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4301 struct event_context
*ev
,
4302 struct cli_state
*cli
,
4307 return cli_posix_open_internal_send(mem_ctx
, ev
,
4308 cli
, fname
, flags
, mode
, false);
4311 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4313 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4316 if (tevent_req_is_nterror(req
, &status
)) {
4319 *pfnum
= state
->fnum
;
4320 return NT_STATUS_OK
;
4323 /****************************************************************************
4324 Open - POSIX semantics. Doesn't request oplock.
4325 ****************************************************************************/
4327 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4328 int flags
, mode_t mode
, uint16_t *pfnum
)
4331 TALLOC_CTX
*frame
= talloc_stackframe();
4332 struct event_context
*ev
= NULL
;
4333 struct tevent_req
*req
= NULL
;
4334 NTSTATUS status
= NT_STATUS_OK
;
4336 if (cli_has_async_calls(cli
)) {
4338 * Can't use sync call while an async call is in flight
4340 status
= NT_STATUS_INVALID_PARAMETER
;
4344 ev
= event_context_init(frame
);
4346 status
= NT_STATUS_NO_MEMORY
;
4350 req
= cli_posix_open_send(frame
,
4357 status
= NT_STATUS_NO_MEMORY
;
4361 if (!tevent_req_poll(req
, ev
)) {
4362 status
= map_nt_error_from_unix(errno
);
4366 status
= cli_posix_open_recv(req
, pfnum
);
4373 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4374 struct event_context
*ev
,
4375 struct cli_state
*cli
,
4379 return cli_posix_open_internal_send(mem_ctx
, ev
,
4380 cli
, fname
, O_CREAT
, mode
, true);
4383 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4385 return tevent_req_simple_recv_ntstatus(req
);
4388 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4390 TALLOC_CTX
*frame
= talloc_stackframe();
4391 struct event_context
*ev
= NULL
;
4392 struct tevent_req
*req
= NULL
;
4393 NTSTATUS status
= NT_STATUS_OK
;
4395 if (cli_has_async_calls(cli
)) {
4397 * Can't use sync call while an async call is in flight
4399 status
= NT_STATUS_INVALID_PARAMETER
;
4403 ev
= event_context_init(frame
);
4405 status
= NT_STATUS_NO_MEMORY
;
4409 req
= cli_posix_mkdir_send(frame
,
4415 status
= NT_STATUS_NO_MEMORY
;
4419 if (!tevent_req_poll(req
, ev
)) {
4420 status
= map_nt_error_from_unix(errno
);
4424 status
= cli_posix_mkdir_recv(req
);
4431 /****************************************************************************
4432 unlink or rmdir - POSIX semantics.
4433 ****************************************************************************/
4435 struct cli_posix_unlink_internal_state
{
4439 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
);
4441 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4442 struct event_context
*ev
,
4443 struct cli_state
*cli
,
4447 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4448 struct cli_posix_unlink_internal_state
*state
= NULL
;
4450 req
= tevent_req_create(mem_ctx
, &state
,
4451 struct cli_posix_unlink_internal_state
);
4456 /* Setup data word. */
4457 SSVAL(state
->data
, 0, level
);
4459 subreq
= cli_setpathinfo_send(state
, ev
, cli
,
4460 SMB_POSIX_PATH_UNLINK
,
4462 state
->data
, sizeof(state
->data
));
4463 if (tevent_req_nomem(subreq
, req
)) {
4464 return tevent_req_post(req
, ev
);
4466 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4470 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4472 NTSTATUS status
= cli_setpathinfo_recv(subreq
);
4473 tevent_req_simple_finish_ntstatus(subreq
, status
);
4476 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4477 struct event_context
*ev
,
4478 struct cli_state
*cli
,
4481 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
,
4482 SMB_POSIX_UNLINK_FILE_TARGET
);
4485 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4487 return tevent_req_simple_recv_ntstatus(req
);
4490 /****************************************************************************
4491 unlink - POSIX semantics.
4492 ****************************************************************************/
4494 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4496 TALLOC_CTX
*frame
= talloc_stackframe();
4497 struct event_context
*ev
= NULL
;
4498 struct tevent_req
*req
= NULL
;
4499 NTSTATUS status
= NT_STATUS_OK
;
4501 if (cli_has_async_calls(cli
)) {
4503 * Can't use sync call while an async call is in flight
4505 status
= NT_STATUS_INVALID_PARAMETER
;
4509 ev
= event_context_init(frame
);
4511 status
= NT_STATUS_NO_MEMORY
;
4515 req
= cli_posix_unlink_send(frame
,
4520 status
= NT_STATUS_NO_MEMORY
;
4524 if (!tevent_req_poll(req
, ev
)) {
4525 status
= map_nt_error_from_unix(errno
);
4529 status
= cli_posix_unlink_recv(req
);
4536 /****************************************************************************
4537 rmdir - POSIX semantics.
4538 ****************************************************************************/
4540 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4541 struct event_context
*ev
,
4542 struct cli_state
*cli
,
4545 return cli_posix_unlink_internal_send(
4546 mem_ctx
, ev
, cli
, fname
,
4547 SMB_POSIX_UNLINK_DIRECTORY_TARGET
);
4550 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4552 return tevent_req_simple_recv_ntstatus(req
);
4555 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4557 TALLOC_CTX
*frame
= talloc_stackframe();
4558 struct event_context
*ev
= NULL
;
4559 struct tevent_req
*req
= NULL
;
4560 NTSTATUS status
= NT_STATUS_OK
;
4562 if (cli_has_async_calls(cli
)) {
4564 * Can't use sync call while an async call is in flight
4566 status
= NT_STATUS_INVALID_PARAMETER
;
4570 ev
= event_context_init(frame
);
4572 status
= NT_STATUS_NO_MEMORY
;
4576 req
= cli_posix_rmdir_send(frame
,
4581 status
= NT_STATUS_NO_MEMORY
;
4585 if (!tevent_req_poll(req
, ev
)) {
4586 status
= map_nt_error_from_unix(errno
);
4590 status
= cli_posix_rmdir_recv(req
, frame
);
4597 /****************************************************************************
4599 ****************************************************************************/
4601 struct cli_notify_state
{
4603 uint32_t num_changes
;
4604 struct notify_change
*changes
;
4607 static void cli_notify_done(struct tevent_req
*subreq
);
4609 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
4610 struct tevent_context
*ev
,
4611 struct cli_state
*cli
, uint16_t fnum
,
4612 uint32_t buffer_size
,
4613 uint32_t completion_filter
, bool recursive
)
4615 struct tevent_req
*req
, *subreq
;
4616 struct cli_notify_state
*state
;
4618 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
4623 SIVAL(state
->setup
, 0, completion_filter
);
4624 SSVAL(state
->setup
, 4, fnum
);
4625 SSVAL(state
->setup
, 6, recursive
);
4627 subreq
= cli_trans_send(
4628 state
, /* mem ctx. */
4629 ev
, /* event ctx. */
4630 cli
, /* cli_state. */
4631 SMBnttrans
, /* cmd. */
4632 NULL
, /* pipe name. */
4634 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
4636 (uint16_t *)state
->setup
, /* setup. */
4637 4, /* num setup uint16_t words. */
4638 0, /* max returned setup. */
4641 buffer_size
, /* max returned param. */
4644 0); /* max returned data. */
4646 if (tevent_req_nomem(subreq
, req
)) {
4647 return tevent_req_post(req
, ev
);
4649 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
4653 static void cli_notify_done(struct tevent_req
*subreq
)
4655 struct tevent_req
*req
= tevent_req_callback_data(
4656 subreq
, struct tevent_req
);
4657 struct cli_notify_state
*state
= tevent_req_data(
4658 req
, struct cli_notify_state
);
4661 uint32_t i
, ofs
, num_params
;
4664 status
= cli_trans_recv(subreq
, talloc_tos(), &flags2
, NULL
, 0, NULL
,
4665 ¶ms
, 0, &num_params
, NULL
, 0, NULL
);
4666 TALLOC_FREE(subreq
);
4667 if (tevent_req_nterror(req
, status
)) {
4668 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
4672 state
->num_changes
= 0;
4675 while (num_params
- ofs
> 12) {
4676 uint32_t len
= IVAL(params
, ofs
);
4677 state
->num_changes
+= 1;
4679 if ((len
== 0) || (ofs
+len
>= num_params
)) {
4685 state
->changes
= talloc_array(state
, struct notify_change
,
4686 state
->num_changes
);
4687 if (tevent_req_nomem(state
->changes
, req
)) {
4688 TALLOC_FREE(params
);
4694 for (i
=0; i
<state
->num_changes
; i
++) {
4695 uint32_t next
= IVAL(params
, ofs
);
4696 uint32_t len
= IVAL(params
, ofs
+8);
4700 if ((next
!= 0) && (len
+12 != next
)) {
4701 TALLOC_FREE(params
);
4703 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4707 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
4708 ret
= clistr_pull_talloc(params
, (char *)params
, flags2
,
4709 &name
, params
+ofs
+12, len
,
4710 STR_TERMINATE
|STR_UNICODE
);
4712 TALLOC_FREE(params
);
4713 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
4716 state
->changes
[i
].name
= name
;
4720 TALLOC_FREE(params
);
4721 tevent_req_done(req
);
4724 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4725 uint32_t *pnum_changes
,
4726 struct notify_change
**pchanges
)
4728 struct cli_notify_state
*state
= tevent_req_data(
4729 req
, struct cli_notify_state
);
4732 if (tevent_req_is_nterror(req
, &status
)) {
4736 *pnum_changes
= state
->num_changes
;
4737 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
4738 return NT_STATUS_OK
;
4741 struct cli_qpathinfo_state
{
4750 static void cli_qpathinfo_done(struct tevent_req
*subreq
);
4752 struct tevent_req
*cli_qpathinfo_send(TALLOC_CTX
*mem_ctx
,
4753 struct tevent_context
*ev
,
4754 struct cli_state
*cli
, const char *fname
,
4755 uint16_t level
, uint32_t min_rdata
,
4758 struct tevent_req
*req
, *subreq
;
4759 struct cli_qpathinfo_state
*state
;
4761 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qpathinfo_state
);
4765 state
->min_rdata
= min_rdata
;
4766 SSVAL(state
->setup
, 0, TRANSACT2_QPATHINFO
);
4768 state
->param
= talloc_zero_array(state
, uint8_t, 6);
4769 if (tevent_req_nomem(state
->param
, req
)) {
4770 return tevent_req_post(req
, ev
);
4772 SSVAL(state
->param
, 0, level
);
4773 state
->param
= trans2_bytes_push_str(
4774 state
->param
, cli_ucs2(cli
), fname
, strlen(fname
)+1, NULL
);
4775 if (tevent_req_nomem(state
->param
, req
)) {
4776 return tevent_req_post(req
, ev
);
4779 subreq
= cli_trans_send(
4780 state
, /* mem ctx. */
4781 ev
, /* event ctx. */
4782 cli
, /* cli_state. */
4783 SMBtrans2
, /* cmd. */
4784 NULL
, /* pipe name. */
4788 state
->setup
, /* setup. */
4789 1, /* num setup uint16_t words. */
4790 0, /* max returned setup. */
4791 state
->param
, /* param. */
4792 talloc_get_size(state
->param
), /* num param. */
4793 2, /* max returned param. */
4796 max_rdata
); /* max returned data. */
4798 if (tevent_req_nomem(subreq
, req
)) {
4799 return tevent_req_post(req
, ev
);
4801 tevent_req_set_callback(subreq
, cli_qpathinfo_done
, req
);
4805 static void cli_qpathinfo_done(struct tevent_req
*subreq
)
4807 struct tevent_req
*req
= tevent_req_callback_data(
4808 subreq
, struct tevent_req
);
4809 struct cli_qpathinfo_state
*state
= tevent_req_data(
4810 req
, struct cli_qpathinfo_state
);
4813 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4815 &state
->rdata
, state
->min_rdata
,
4817 if (tevent_req_nterror(req
, status
)) {
4820 tevent_req_done(req
);
4823 NTSTATUS
cli_qpathinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4824 uint8_t **rdata
, uint32_t *num_rdata
)
4826 struct cli_qpathinfo_state
*state
= tevent_req_data(
4827 req
, struct cli_qpathinfo_state
);
4830 if (tevent_req_is_nterror(req
, &status
)) {
4833 if (rdata
!= NULL
) {
4834 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
4836 TALLOC_FREE(state
->rdata
);
4838 if (num_rdata
!= NULL
) {
4839 *num_rdata
= state
->num_rdata
;
4841 return NT_STATUS_OK
;
4844 NTSTATUS
cli_qpathinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
4845 const char *fname
, uint16_t level
, uint32_t min_rdata
,
4847 uint8_t **rdata
, uint32_t *num_rdata
)
4849 TALLOC_CTX
*frame
= talloc_stackframe();
4850 struct event_context
*ev
;
4851 struct tevent_req
*req
;
4852 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4854 if (cli_has_async_calls(cli
)) {
4856 * Can't use sync call while an async call is in flight
4858 status
= NT_STATUS_INVALID_PARAMETER
;
4861 ev
= event_context_init(frame
);
4865 req
= cli_qpathinfo_send(frame
, ev
, cli
, fname
, level
, min_rdata
,
4870 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
4873 status
= cli_qpathinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
4879 struct cli_qfileinfo_state
{
4888 static void cli_qfileinfo_done(struct tevent_req
*subreq
);
4890 struct tevent_req
*cli_qfileinfo_send(TALLOC_CTX
*mem_ctx
,
4891 struct tevent_context
*ev
,
4892 struct cli_state
*cli
, uint16_t fnum
,
4893 uint16_t level
, uint32_t min_rdata
,
4896 struct tevent_req
*req
, *subreq
;
4897 struct cli_qfileinfo_state
*state
;
4899 req
= tevent_req_create(mem_ctx
, &state
, struct cli_qfileinfo_state
);
4903 state
->min_rdata
= min_rdata
;
4904 SSVAL(state
->param
, 0, fnum
);
4905 SSVAL(state
->param
, 2, level
);
4906 SSVAL(state
->setup
, 0, TRANSACT2_QFILEINFO
);
4908 subreq
= cli_trans_send(
4909 state
, /* mem ctx. */
4910 ev
, /* event ctx. */
4911 cli
, /* cli_state. */
4912 SMBtrans2
, /* cmd. */
4913 NULL
, /* pipe name. */
4917 state
->setup
, /* setup. */
4918 1, /* num setup uint16_t words. */
4919 0, /* max returned setup. */
4920 state
->param
, /* param. */
4921 sizeof(state
->param
), /* num param. */
4922 2, /* max returned param. */
4925 max_rdata
); /* max returned data. */
4927 if (tevent_req_nomem(subreq
, req
)) {
4928 return tevent_req_post(req
, ev
);
4930 tevent_req_set_callback(subreq
, cli_qfileinfo_done
, req
);
4934 static void cli_qfileinfo_done(struct tevent_req
*subreq
)
4936 struct tevent_req
*req
= tevent_req_callback_data(
4937 subreq
, struct tevent_req
);
4938 struct cli_qfileinfo_state
*state
= tevent_req_data(
4939 req
, struct cli_qfileinfo_state
);
4942 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
4944 &state
->rdata
, state
->min_rdata
,
4946 if (tevent_req_nterror(req
, status
)) {
4949 tevent_req_done(req
);
4952 NTSTATUS
cli_qfileinfo_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4953 uint8_t **rdata
, uint32_t *num_rdata
)
4955 struct cli_qfileinfo_state
*state
= tevent_req_data(
4956 req
, struct cli_qfileinfo_state
);
4959 if (tevent_req_is_nterror(req
, &status
)) {
4962 if (rdata
!= NULL
) {
4963 *rdata
= talloc_move(mem_ctx
, &state
->rdata
);
4965 TALLOC_FREE(state
->rdata
);
4967 if (num_rdata
!= NULL
) {
4968 *num_rdata
= state
->num_rdata
;
4970 return NT_STATUS_OK
;
4973 NTSTATUS
cli_qfileinfo(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
4974 uint16_t fnum
, uint16_t level
, uint32_t min_rdata
,
4976 uint8_t **rdata
, uint32_t *num_rdata
)
4978 TALLOC_CTX
*frame
= talloc_stackframe();
4979 struct event_context
*ev
;
4980 struct tevent_req
*req
;
4981 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4983 if (cli_has_async_calls(cli
)) {
4985 * Can't use sync call while an async call is in flight
4987 status
= NT_STATUS_INVALID_PARAMETER
;
4990 ev
= event_context_init(frame
);
4994 req
= cli_qfileinfo_send(frame
, ev
, cli
, fnum
, level
, min_rdata
,
4999 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5002 status
= cli_qfileinfo_recv(req
, mem_ctx
, rdata
, num_rdata
);
5008 struct cli_flush_state
{
5012 static void cli_flush_done(struct tevent_req
*subreq
);
5014 struct tevent_req
*cli_flush_send(TALLOC_CTX
*mem_ctx
,
5015 struct event_context
*ev
,
5016 struct cli_state
*cli
,
5019 struct tevent_req
*req
, *subreq
;
5020 struct cli_flush_state
*state
;
5022 req
= tevent_req_create(mem_ctx
, &state
, struct cli_flush_state
);
5026 SSVAL(state
->vwv
+ 0, 0, fnum
);
5028 subreq
= cli_smb_send(state
, ev
, cli
, SMBflush
, 0, 1, state
->vwv
,
5030 if (tevent_req_nomem(subreq
, req
)) {
5031 return tevent_req_post(req
, ev
);
5033 tevent_req_set_callback(subreq
, cli_flush_done
, req
);
5037 static void cli_flush_done(struct tevent_req
*subreq
)
5039 struct tevent_req
*req
= tevent_req_callback_data(
5040 subreq
, struct tevent_req
);
5043 status
= cli_smb_recv(subreq
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
5044 TALLOC_FREE(subreq
);
5045 if (tevent_req_nterror(req
, status
)) {
5048 tevent_req_done(req
);
5051 NTSTATUS
cli_flush_recv(struct tevent_req
*req
)
5053 return tevent_req_simple_recv_ntstatus(req
);
5056 NTSTATUS
cli_flush(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
, uint16_t fnum
)
5058 TALLOC_CTX
*frame
= talloc_stackframe();
5059 struct event_context
*ev
;
5060 struct tevent_req
*req
;
5061 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5063 if (cli_has_async_calls(cli
)) {
5065 * Can't use sync call while an async call is in flight
5067 status
= NT_STATUS_INVALID_PARAMETER
;
5070 ev
= event_context_init(frame
);
5074 req
= cli_flush_send(frame
, ev
, cli
, fnum
);
5078 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5081 status
= cli_flush_recv(req
);
5087 struct cli_shadow_copy_data_state
{
5094 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
);
5096 struct tevent_req
*cli_shadow_copy_data_send(TALLOC_CTX
*mem_ctx
,
5097 struct tevent_context
*ev
,
5098 struct cli_state
*cli
,
5102 struct tevent_req
*req
, *subreq
;
5103 struct cli_shadow_copy_data_state
*state
;
5106 req
= tevent_req_create(mem_ctx
, &state
,
5107 struct cli_shadow_copy_data_state
);
5111 state
->get_names
= get_names
;
5112 ret_size
= get_names
? cli
->max_xmit
: 16;
5114 SIVAL(state
->setup
+ 0, 0, FSCTL_GET_SHADOW_COPY_DATA
);
5115 SSVAL(state
->setup
+ 2, 0, fnum
);
5116 SCVAL(state
->setup
+ 3, 0, 0); /* isFsctl */
5117 SCVAL(state
->setup
+ 3, 1, 0); /* compfilter, isFlags (WSSP) */
5119 subreq
= cli_trans_send(
5120 state
, ev
, cli
, SMBnttrans
, NULL
, 0, NT_TRANSACT_IOCTL
, 0,
5121 state
->setup
, ARRAY_SIZE(state
->setup
), 0,
5124 if (tevent_req_nomem(subreq
, req
)) {
5125 return tevent_req_post(req
, ev
);
5127 tevent_req_set_callback(subreq
, cli_shadow_copy_data_done
, req
);
5131 static void cli_shadow_copy_data_done(struct tevent_req
*subreq
)
5133 struct tevent_req
*req
= tevent_req_callback_data(
5134 subreq
, struct tevent_req
);
5135 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5136 req
, struct cli_shadow_copy_data_state
);
5139 status
= cli_trans_recv(subreq
, state
, NULL
,
5140 NULL
, 0, NULL
, /* setup */
5141 NULL
, 0, NULL
, /* param */
5142 &state
->data
, 12, &state
->num_data
);
5143 TALLOC_FREE(subreq
);
5144 if (tevent_req_nterror(req
, status
)) {
5147 tevent_req_done(req
);
5150 NTSTATUS
cli_shadow_copy_data_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5151 char ***pnames
, int *pnum_names
)
5153 struct cli_shadow_copy_data_state
*state
= tevent_req_data(
5154 req
, struct cli_shadow_copy_data_state
);
5160 if (tevent_req_is_nterror(req
, &status
)) {
5163 num_names
= IVAL(state
->data
, 4);
5164 dlength
= IVAL(state
->data
, 8);
5166 if (!state
->get_names
) {
5167 *pnum_names
= num_names
;
5168 return NT_STATUS_OK
;
5171 if (dlength
+12 > state
->num_data
) {
5172 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5174 names
= talloc_array(mem_ctx
, char *, num_names
);
5175 if (names
== NULL
) {
5176 return NT_STATUS_NO_MEMORY
;
5179 for (i
=0; i
<num_names
; i
++) {
5182 size_t converted_size
;
5184 src
= state
->data
+ 12 + i
* 2 * sizeof(SHADOW_COPY_LABEL
);
5185 ret
= convert_string_talloc(
5186 names
, CH_UTF16LE
, CH_UNIX
,
5187 src
, 2 * sizeof(SHADOW_COPY_LABEL
),
5188 &names
[i
], &converted_size
);
5191 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5194 *pnum_names
= num_names
;
5196 return NT_STATUS_OK
;
5199 NTSTATUS
cli_shadow_copy_data(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
5200 uint16_t fnum
, bool get_names
,
5201 char ***pnames
, int *pnum_names
)
5203 TALLOC_CTX
*frame
= talloc_stackframe();
5204 struct event_context
*ev
;
5205 struct tevent_req
*req
;
5206 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5208 if (cli_has_async_calls(cli
)) {
5210 * Can't use sync call while an async call is in flight
5212 status
= NT_STATUS_INVALID_PARAMETER
;
5215 ev
= event_context_init(frame
);
5219 req
= cli_shadow_copy_data_send(frame
, ev
, cli
, fnum
, get_names
);
5223 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
5226 status
= cli_shadow_copy_data_recv(req
, mem_ctx
, pnames
, pnum_names
);