2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
41 size_t *pconverted_size)
45 size_t converted_size;
51 buflen = talloc_get_size(buf);
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
77 TALLOC_FREE(converted);
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
155 memcpy(&buf[buflen], bytes, num_bytes);
159 struct cli_setpathinfo_state {
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
176 uint16_t additional_flags2 = 0;
178 req = tevent_req_create(mem_ctx, &state,
179 struct cli_setpathinfo_state);
184 /* Setup setup word. */
185 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
187 /* Setup param array. */
188 state->param = talloc_zero_array(state, uint8_t, 6);
189 if (tevent_req_nomem(state->param, req)) {
190 return tevent_req_post(req, ev);
192 SSVAL(state->param, 0, level);
194 state->param = trans2_bytes_push_str(
195 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
196 if (tevent_req_nomem(state->param, req)) {
197 return tevent_req_post(req, ev);
200 if (clistr_is_previous_version_path(path) &&
201 !INFO_LEVEL_IS_UNIX(level)) {
202 additional_flags2 = FLAGS2_REPARSE_PATH;
205 subreq = cli_trans_send(
206 state, /* mem ctx. */
208 cli, /* cli_state. */
209 additional_flags2, /* additional_flags2 */
210 SMBtrans2, /* cmd. */
211 NULL, /* pipe name. */
215 &state->setup, /* setup. */
216 1, /* num setup uint16_t words. */
217 0, /* max returned setup. */
218 state->param, /* param. */
219 talloc_get_size(state->param), /* num param. */
220 2, /* max returned param. */
222 data_len, /* num data. */
223 0); /* max returned data. */
225 if (tevent_req_nomem(subreq, req)) {
226 return tevent_req_post(req, ev);
228 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
232 static void cli_setpathinfo_done(struct tevent_req *subreq)
234 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
235 NULL, 0, NULL, NULL, 0, NULL);
236 tevent_req_simple_finish_ntstatus(subreq, status);
239 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
241 return tevent_req_simple_recv_ntstatus(req);
244 NTSTATUS cli_setpathinfo(struct cli_state *cli,
250 TALLOC_CTX *frame = talloc_stackframe();
251 struct tevent_context *ev;
252 struct tevent_req *req;
253 NTSTATUS status = NT_STATUS_NO_MEMORY;
255 if (smbXcli_conn_has_async_calls(cli->conn)) {
257 * Can't use sync call while an async call is in flight
259 status = NT_STATUS_INVALID_PARAMETER;
262 ev = samba_tevent_context_init(frame);
266 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
270 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
273 status = cli_setpathinfo_recv(req);
279 /****************************************************************************
280 Hard/Symlink a file (UNIX extensions).
281 Creates new name (sym)linked to oldname.
282 ****************************************************************************/
284 struct cli_posix_link_internal_state {
288 static void cli_posix_link_internal_done(struct tevent_req *subreq);
290 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
291 struct tevent_context *ev,
292 struct cli_state *cli,
297 struct tevent_req *req = NULL, *subreq = NULL;
298 struct cli_posix_link_internal_state *state = NULL;
300 req = tevent_req_create(mem_ctx, &state,
301 struct cli_posix_link_internal_state);
306 /* Setup data array. */
307 state->data = talloc_array(state, uint8_t, 0);
308 if (tevent_req_nomem(state->data, req)) {
309 return tevent_req_post(req, ev);
311 state->data = trans2_bytes_push_str(
312 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
314 subreq = cli_setpathinfo_send(
315 state, ev, cli, level, newname,
316 state->data, talloc_get_size(state->data));
317 if (tevent_req_nomem(subreq, req)) {
318 return tevent_req_post(req, ev);
320 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
324 static void cli_posix_link_internal_done(struct tevent_req *subreq)
326 NTSTATUS status = cli_setpathinfo_recv(subreq);
327 tevent_req_simple_finish_ntstatus(subreq, status);
330 /****************************************************************************
331 Symlink a file (UNIX extensions).
332 ****************************************************************************/
334 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
335 struct tevent_context *ev,
336 struct cli_state *cli,
340 return cli_posix_link_internal_send(
341 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
344 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
346 return tevent_req_simple_recv_ntstatus(req);
349 NTSTATUS cli_posix_symlink(struct cli_state *cli,
353 TALLOC_CTX *frame = talloc_stackframe();
354 struct tevent_context *ev = NULL;
355 struct tevent_req *req = NULL;
356 NTSTATUS status = NT_STATUS_OK;
358 if (smbXcli_conn_has_async_calls(cli->conn)) {
360 * Can't use sync call while an async call is in flight
362 status = NT_STATUS_INVALID_PARAMETER;
366 ev = samba_tevent_context_init(frame);
368 status = NT_STATUS_NO_MEMORY;
372 req = cli_posix_symlink_send(frame,
378 status = NT_STATUS_NO_MEMORY;
382 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
386 status = cli_posix_symlink_recv(req);
393 /****************************************************************************
394 Read a POSIX symlink.
395 ****************************************************************************/
397 struct readlink_state {
402 static void cli_posix_readlink_done(struct tevent_req *subreq);
404 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
405 struct tevent_context *ev,
406 struct cli_state *cli,
410 struct tevent_req *req = NULL, *subreq = NULL;
411 struct readlink_state *state = NULL;
412 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
414 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
420 * Len is in bytes, we need it in UCS2 units.
422 if ((2*len < len) || (maxbytelen < len)) {
423 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
424 return tevent_req_post(req, ev);
427 subreq = cli_qpathinfo_send(state, ev, cli, fname,
428 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
429 if (tevent_req_nomem(subreq, req)) {
430 return tevent_req_post(req, ev);
432 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
436 static void cli_posix_readlink_done(struct tevent_req *subreq)
438 struct tevent_req *req = tevent_req_callback_data(
439 subreq, struct tevent_req);
440 struct readlink_state *state = tevent_req_data(
441 req, struct readlink_state);
444 status = cli_qpathinfo_recv(subreq, state, &state->data,
447 if (tevent_req_nterror(req, status)) {
451 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
453 if (state->data[state->num_data-1] != '\0') {
454 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
457 tevent_req_done(req);
460 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
461 char *retpath, size_t len)
464 char *converted = NULL;
465 size_t converted_size = 0;
466 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
468 if (tevent_req_is_nterror(req, &status)) {
471 /* The returned data is a pushed string, not raw data. */
472 if (!convert_string_talloc(state,
473 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
479 return NT_STATUS_NO_MEMORY;
482 len = MIN(len,converted_size);
484 return NT_STATUS_DATA_ERROR;
486 memcpy(retpath, converted, len);
490 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
491 char *linkpath, size_t len)
493 TALLOC_CTX *frame = talloc_stackframe();
494 struct tevent_context *ev = NULL;
495 struct tevent_req *req = NULL;
496 NTSTATUS status = NT_STATUS_OK;
498 if (smbXcli_conn_has_async_calls(cli->conn)) {
500 * Can't use sync call while an async call is in flight
502 status = NT_STATUS_INVALID_PARAMETER;
506 ev = samba_tevent_context_init(frame);
508 status = NT_STATUS_NO_MEMORY;
512 req = cli_posix_readlink_send(frame,
518 status = NT_STATUS_NO_MEMORY;
522 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
526 status = cli_posix_readlink_recv(req, cli, linkpath, len);
533 /****************************************************************************
534 Hard link a file (UNIX extensions).
535 ****************************************************************************/
537 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
538 struct tevent_context *ev,
539 struct cli_state *cli,
543 return cli_posix_link_internal_send(
544 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
547 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
549 return tevent_req_simple_recv_ntstatus(req);
552 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
556 TALLOC_CTX *frame = talloc_stackframe();
557 struct tevent_context *ev = NULL;
558 struct tevent_req *req = NULL;
559 NTSTATUS status = NT_STATUS_OK;
561 if (smbXcli_conn_has_async_calls(cli->conn)) {
563 * Can't use sync call while an async call is in flight
565 status = NT_STATUS_INVALID_PARAMETER;
569 ev = samba_tevent_context_init(frame);
571 status = NT_STATUS_NO_MEMORY;
575 req = cli_posix_hardlink_send(frame,
581 status = NT_STATUS_NO_MEMORY;
585 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
589 status = cli_posix_hardlink_recv(req);
596 /****************************************************************************
597 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
598 ****************************************************************************/
600 struct getacl_state {
605 static void cli_posix_getacl_done(struct tevent_req *subreq);
607 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
608 struct tevent_context *ev,
609 struct cli_state *cli,
612 struct tevent_req *req = NULL, *subreq = NULL;
613 struct getacl_state *state = NULL;
615 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
619 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
621 if (tevent_req_nomem(subreq, req)) {
622 return tevent_req_post(req, ev);
624 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
628 static void cli_posix_getacl_done(struct tevent_req *subreq)
630 struct tevent_req *req = tevent_req_callback_data(
631 subreq, struct tevent_req);
632 struct getacl_state *state = tevent_req_data(
633 req, struct getacl_state);
636 status = cli_qpathinfo_recv(subreq, state, &state->data,
639 if (tevent_req_nterror(req, status)) {
642 tevent_req_done(req);
645 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
650 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
653 if (tevent_req_is_nterror(req, &status)) {
656 *prb_size = (size_t)state->num_data;
657 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
661 NTSTATUS cli_posix_getacl(struct cli_state *cli,
667 TALLOC_CTX *frame = talloc_stackframe();
668 struct tevent_context *ev = NULL;
669 struct tevent_req *req = NULL;
670 NTSTATUS status = NT_STATUS_OK;
672 if (smbXcli_conn_has_async_calls(cli->conn)) {
674 * Can't use sync call while an async call is in flight
676 status = NT_STATUS_INVALID_PARAMETER;
680 ev = samba_tevent_context_init(frame);
682 status = NT_STATUS_NO_MEMORY;
686 req = cli_posix_getacl_send(frame,
691 status = NT_STATUS_NO_MEMORY;
695 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
699 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
706 /****************************************************************************
707 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
708 ****************************************************************************/
710 struct setacl_state {
714 static void cli_posix_setacl_done(struct tevent_req *subreq);
716 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
717 struct tevent_context *ev,
718 struct cli_state *cli,
723 struct tevent_req *req = NULL, *subreq = NULL;
724 struct setacl_state *state = NULL;
726 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
730 state->data = talloc_memdup(state, data, num_data);
731 if (tevent_req_nomem(state->data, req)) {
732 return tevent_req_post(req, ev);
735 subreq = cli_setpathinfo_send(state,
742 if (tevent_req_nomem(subreq, req)) {
743 return tevent_req_post(req, ev);
745 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
749 static void cli_posix_setacl_done(struct tevent_req *subreq)
751 NTSTATUS status = cli_setpathinfo_recv(subreq);
752 tevent_req_simple_finish_ntstatus(subreq, status);
755 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
757 return tevent_req_simple_recv_ntstatus(req);
760 NTSTATUS cli_posix_setacl(struct cli_state *cli,
765 TALLOC_CTX *frame = talloc_stackframe();
766 struct tevent_context *ev = NULL;
767 struct tevent_req *req = NULL;
768 NTSTATUS status = NT_STATUS_OK;
770 if (smbXcli_conn_has_async_calls(cli->conn)) {
772 * Can't use sync call while an async call is in flight
774 status = NT_STATUS_INVALID_PARAMETER;
778 ev = samba_tevent_context_init(frame);
780 status = NT_STATUS_NO_MEMORY;
784 req = cli_posix_setacl_send(frame,
791 status = NT_STATUS_NO_MEMORY;
795 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
799 status = cli_posix_setacl_recv(req);
806 /****************************************************************************
807 Stat a file (UNIX extensions).
808 ****************************************************************************/
815 static void cli_posix_stat_done(struct tevent_req *subreq);
817 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
818 struct tevent_context *ev,
819 struct cli_state *cli,
822 struct tevent_req *req = NULL, *subreq = NULL;
823 struct stat_state *state = NULL;
825 req = tevent_req_create(mem_ctx, &state, struct stat_state);
829 subreq = cli_qpathinfo_send(state, ev, cli, fname,
830 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
831 if (tevent_req_nomem(subreq, req)) {
832 return tevent_req_post(req, ev);
834 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
838 static void cli_posix_stat_done(struct tevent_req *subreq)
840 struct tevent_req *req = tevent_req_callback_data(
841 subreq, struct tevent_req);
842 struct stat_state *state = tevent_req_data(req, struct stat_state);
845 status = cli_qpathinfo_recv(subreq, state, &state->data,
848 if (tevent_req_nterror(req, status)) {
851 tevent_req_done(req);
854 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
855 SMB_STRUCT_STAT *sbuf)
857 struct stat_state *state = tevent_req_data(req, struct stat_state);
860 if (tevent_req_is_nterror(req, &status)) {
864 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
865 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
866 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
867 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
869 /* assume 512 byte blocks */
870 sbuf->st_ex_blocks /= 512;
872 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
873 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
874 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
876 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
877 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
878 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
879 #if defined(HAVE_MAKEDEV)
881 uint32_t dev_major = IVAL(state->data,60);
882 uint32_t dev_minor = IVAL(state->data,68);
883 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
886 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
887 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
888 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
893 NTSTATUS cli_posix_stat(struct cli_state *cli,
895 SMB_STRUCT_STAT *sbuf)
897 TALLOC_CTX *frame = talloc_stackframe();
898 struct tevent_context *ev = NULL;
899 struct tevent_req *req = NULL;
900 NTSTATUS status = NT_STATUS_OK;
902 if (smbXcli_conn_has_async_calls(cli->conn)) {
904 * Can't use sync call while an async call is in flight
906 status = NT_STATUS_INVALID_PARAMETER;
910 ev = samba_tevent_context_init(frame);
912 status = NT_STATUS_NO_MEMORY;
916 req = cli_posix_stat_send(frame,
921 status = NT_STATUS_NO_MEMORY;
925 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
929 status = cli_posix_stat_recv(req, sbuf);
936 /****************************************************************************
937 Chmod or chown a file internal (UNIX extensions).
938 ****************************************************************************/
940 struct cli_posix_chown_chmod_internal_state {
944 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
946 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct cli_state *cli,
954 struct tevent_req *req = NULL, *subreq = NULL;
955 struct cli_posix_chown_chmod_internal_state *state = NULL;
957 req = tevent_req_create(mem_ctx, &state,
958 struct cli_posix_chown_chmod_internal_state);
963 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
964 memset(&state->data[40], '\0', 60);
965 SIVAL(state->data,40,uid);
966 SIVAL(state->data,48,gid);
967 SIVAL(state->data,84,mode);
969 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
970 fname, state->data, sizeof(state->data));
971 if (tevent_req_nomem(subreq, req)) {
972 return tevent_req_post(req, ev);
974 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
979 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
981 NTSTATUS status = cli_setpathinfo_recv(subreq);
982 tevent_req_simple_finish_ntstatus(subreq, status);
985 /****************************************************************************
986 chmod a file (UNIX extensions).
987 ****************************************************************************/
989 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
990 struct tevent_context *ev,
991 struct cli_state *cli,
995 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
997 unix_perms_to_wire(mode),
1002 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
1004 return tevent_req_simple_recv_ntstatus(req);
1007 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1009 TALLOC_CTX *frame = talloc_stackframe();
1010 struct tevent_context *ev = NULL;
1011 struct tevent_req *req = NULL;
1012 NTSTATUS status = NT_STATUS_OK;
1014 if (smbXcli_conn_has_async_calls(cli->conn)) {
1016 * Can't use sync call while an async call is in flight
1018 status = NT_STATUS_INVALID_PARAMETER;
1022 ev = samba_tevent_context_init(frame);
1024 status = NT_STATUS_NO_MEMORY;
1028 req = cli_posix_chmod_send(frame,
1034 status = NT_STATUS_NO_MEMORY;
1038 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1042 status = cli_posix_chmod_recv(req);
1049 /****************************************************************************
1050 chown a file (UNIX extensions).
1051 ****************************************************************************/
1053 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1054 struct tevent_context *ev,
1055 struct cli_state *cli,
1060 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1067 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1069 return tevent_req_simple_recv_ntstatus(req);
1072 NTSTATUS cli_posix_chown(struct cli_state *cli,
1077 TALLOC_CTX *frame = talloc_stackframe();
1078 struct tevent_context *ev = NULL;
1079 struct tevent_req *req = NULL;
1080 NTSTATUS status = NT_STATUS_OK;
1082 if (smbXcli_conn_has_async_calls(cli->conn)) {
1084 * Can't use sync call while an async call is in flight
1086 status = NT_STATUS_INVALID_PARAMETER;
1090 ev = samba_tevent_context_init(frame);
1092 status = NT_STATUS_NO_MEMORY;
1096 req = cli_posix_chown_send(frame,
1103 status = NT_STATUS_NO_MEMORY;
1107 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1111 status = cli_posix_chown_recv(req);
1118 /****************************************************************************
1120 ****************************************************************************/
1122 static void cli_rename_done(struct tevent_req *subreq);
1124 struct cli_rename_state {
1128 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1129 struct tevent_context *ev,
1130 struct cli_state *cli,
1131 const char *fname_src,
1132 const char *fname_dst)
1134 struct tevent_req *req = NULL, *subreq = NULL;
1135 struct cli_rename_state *state = NULL;
1136 uint8_t additional_flags = 0;
1137 uint16_t additional_flags2 = 0;
1138 uint8_t *bytes = NULL;
1140 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1145 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
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, smbXcli_conn_use_unicode(cli->conn), fname_src,
1153 strlen(fname_src)+1, NULL);
1154 if (tevent_req_nomem(bytes, req)) {
1155 return tevent_req_post(req, ev);
1158 if (clistr_is_previous_version_path(fname_src)) {
1159 additional_flags2 = FLAGS2_REPARSE_PATH;
1162 bytes = talloc_realloc(state, bytes, uint8_t,
1163 talloc_get_size(bytes)+1);
1164 if (tevent_req_nomem(bytes, req)) {
1165 return tevent_req_post(req, ev);
1168 bytes[talloc_get_size(bytes)-1] = 4;
1169 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1170 strlen(fname_dst)+1, NULL);
1171 if (tevent_req_nomem(bytes, req)) {
1172 return tevent_req_post(req, ev);
1175 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1177 1, state->vwv, talloc_get_size(bytes), bytes);
1178 if (tevent_req_nomem(subreq, req)) {
1179 return tevent_req_post(req, ev);
1181 tevent_req_set_callback(subreq, cli_rename_done, req);
1185 static void cli_rename_done(struct tevent_req *subreq)
1187 struct tevent_req *req = tevent_req_callback_data(
1188 subreq, struct tevent_req);
1191 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1192 TALLOC_FREE(subreq);
1193 if (tevent_req_nterror(req, status)) {
1196 tevent_req_done(req);
1199 NTSTATUS cli_rename_recv(struct tevent_req *req)
1201 return tevent_req_simple_recv_ntstatus(req);
1204 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1206 TALLOC_CTX *frame = NULL;
1207 struct tevent_context *ev;
1208 struct tevent_req *req;
1209 NTSTATUS status = NT_STATUS_OK;
1211 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1212 return cli_smb2_rename(cli,
1217 frame = talloc_stackframe();
1219 if (smbXcli_conn_has_async_calls(cli->conn)) {
1221 * Can't use sync call while an async call is in flight
1223 status = NT_STATUS_INVALID_PARAMETER;
1227 ev = samba_tevent_context_init(frame);
1229 status = NT_STATUS_NO_MEMORY;
1233 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1235 status = NT_STATUS_NO_MEMORY;
1239 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1243 status = cli_rename_recv(req);
1250 /****************************************************************************
1252 ****************************************************************************/
1254 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1256 struct cli_ntrename_internal_state {
1260 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1261 struct tevent_context *ev,
1262 struct cli_state *cli,
1263 const char *fname_src,
1264 const char *fname_dst,
1265 uint16_t rename_flag)
1267 struct tevent_req *req = NULL, *subreq = NULL;
1268 struct cli_ntrename_internal_state *state = NULL;
1269 uint8_t additional_flags = 0;
1270 uint16_t additional_flags2 = 0;
1271 uint8_t *bytes = NULL;
1273 req = tevent_req_create(mem_ctx, &state,
1274 struct cli_ntrename_internal_state);
1279 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1280 SSVAL(state->vwv+1, 0, rename_flag);
1282 bytes = talloc_array(state, uint8_t, 1);
1283 if (tevent_req_nomem(bytes, req)) {
1284 return tevent_req_post(req, ev);
1287 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1288 strlen(fname_src)+1, NULL);
1289 if (tevent_req_nomem(bytes, req)) {
1290 return tevent_req_post(req, ev);
1293 if (clistr_is_previous_version_path(fname_src)) {
1294 additional_flags2 = FLAGS2_REPARSE_PATH;
1297 bytes = talloc_realloc(state, bytes, uint8_t,
1298 talloc_get_size(bytes)+1);
1299 if (tevent_req_nomem(bytes, req)) {
1300 return tevent_req_post(req, ev);
1303 bytes[talloc_get_size(bytes)-1] = 4;
1304 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1305 strlen(fname_dst)+1, NULL);
1306 if (tevent_req_nomem(bytes, req)) {
1307 return tevent_req_post(req, ev);
1310 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1312 4, state->vwv, talloc_get_size(bytes), bytes);
1313 if (tevent_req_nomem(subreq, req)) {
1314 return tevent_req_post(req, ev);
1316 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1320 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1322 struct tevent_req *req = tevent_req_callback_data(
1323 subreq, struct tevent_req);
1326 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1327 TALLOC_FREE(subreq);
1328 if (tevent_req_nterror(req, status)) {
1331 tevent_req_done(req);
1334 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1336 return tevent_req_simple_recv_ntstatus(req);
1339 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1340 struct tevent_context *ev,
1341 struct cli_state *cli,
1342 const char *fname_src,
1343 const char *fname_dst)
1345 return cli_ntrename_internal_send(mem_ctx,
1350 RENAME_FLAG_RENAME);
1353 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1355 return cli_ntrename_internal_recv(req);
1358 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1360 TALLOC_CTX *frame = talloc_stackframe();
1361 struct tevent_context *ev;
1362 struct tevent_req *req;
1363 NTSTATUS status = NT_STATUS_OK;
1365 if (smbXcli_conn_has_async_calls(cli->conn)) {
1367 * Can't use sync call while an async call is in flight
1369 status = NT_STATUS_INVALID_PARAMETER;
1373 ev = samba_tevent_context_init(frame);
1375 status = NT_STATUS_NO_MEMORY;
1379 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1381 status = NT_STATUS_NO_MEMORY;
1385 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1389 status = cli_ntrename_recv(req);
1396 /****************************************************************************
1398 ****************************************************************************/
1400 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1401 struct tevent_context *ev,
1402 struct cli_state *cli,
1403 const char *fname_src,
1404 const char *fname_dst)
1406 return cli_ntrename_internal_send(mem_ctx,
1411 RENAME_FLAG_HARD_LINK);
1414 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1416 return cli_ntrename_internal_recv(req);
1419 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1421 TALLOC_CTX *frame = talloc_stackframe();
1422 struct tevent_context *ev;
1423 struct tevent_req *req;
1424 NTSTATUS status = NT_STATUS_OK;
1426 if (smbXcli_conn_has_async_calls(cli->conn)) {
1428 * Can't use sync call while an async call is in flight
1430 status = NT_STATUS_INVALID_PARAMETER;
1434 ev = samba_tevent_context_init(frame);
1436 status = NT_STATUS_NO_MEMORY;
1440 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1442 status = NT_STATUS_NO_MEMORY;
1446 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1450 status = cli_nt_hardlink_recv(req);
1457 /****************************************************************************
1459 ****************************************************************************/
1461 static void cli_unlink_done(struct tevent_req *subreq);
1463 struct cli_unlink_state {
1467 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1468 struct tevent_context *ev,
1469 struct cli_state *cli,
1471 uint16_t mayhave_attrs)
1473 struct tevent_req *req = NULL, *subreq = NULL;
1474 struct cli_unlink_state *state = NULL;
1475 uint8_t additional_flags = 0;
1476 uint16_t additional_flags2 = 0;
1477 uint8_t *bytes = NULL;
1479 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1484 SSVAL(state->vwv+0, 0, mayhave_attrs);
1486 bytes = talloc_array(state, uint8_t, 1);
1487 if (tevent_req_nomem(bytes, req)) {
1488 return tevent_req_post(req, ev);
1491 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1492 strlen(fname)+1, NULL);
1494 if (tevent_req_nomem(bytes, req)) {
1495 return tevent_req_post(req, ev);
1498 if (clistr_is_previous_version_path(fname)) {
1499 additional_flags2 = FLAGS2_REPARSE_PATH;
1502 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1504 1, state->vwv, talloc_get_size(bytes), bytes);
1505 if (tevent_req_nomem(subreq, req)) {
1506 return tevent_req_post(req, ev);
1508 tevent_req_set_callback(subreq, cli_unlink_done, req);
1512 static void cli_unlink_done(struct tevent_req *subreq)
1514 struct tevent_req *req = tevent_req_callback_data(
1515 subreq, struct tevent_req);
1518 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1519 TALLOC_FREE(subreq);
1520 if (tevent_req_nterror(req, status)) {
1523 tevent_req_done(req);
1526 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1528 return tevent_req_simple_recv_ntstatus(req);
1531 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1533 TALLOC_CTX *frame = NULL;
1534 struct tevent_context *ev;
1535 struct tevent_req *req;
1536 NTSTATUS status = NT_STATUS_OK;
1538 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1539 return cli_smb2_unlink(cli, fname);
1542 frame = talloc_stackframe();
1544 if (smbXcli_conn_has_async_calls(cli->conn)) {
1546 * Can't use sync call while an async call is in flight
1548 status = NT_STATUS_INVALID_PARAMETER;
1552 ev = samba_tevent_context_init(frame);
1554 status = NT_STATUS_NO_MEMORY;
1558 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1560 status = NT_STATUS_NO_MEMORY;
1564 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1568 status = cli_unlink_recv(req);
1575 /****************************************************************************
1577 ****************************************************************************/
1579 static void cli_mkdir_done(struct tevent_req *subreq);
1581 struct cli_mkdir_state {
1585 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1586 struct tevent_context *ev,
1587 struct cli_state *cli,
1590 struct tevent_req *req = NULL, *subreq = NULL;
1591 struct cli_mkdir_state *state = NULL;
1592 uint8_t additional_flags = 0;
1593 uint16_t additional_flags2 = 0;
1594 uint8_t *bytes = NULL;
1596 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1601 bytes = talloc_array(state, uint8_t, 1);
1602 if (tevent_req_nomem(bytes, req)) {
1603 return tevent_req_post(req, ev);
1606 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1607 strlen(dname)+1, NULL);
1609 if (tevent_req_nomem(bytes, req)) {
1610 return tevent_req_post(req, ev);
1613 if (clistr_is_previous_version_path(dname)) {
1614 additional_flags2 = FLAGS2_REPARSE_PATH;
1617 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1619 0, NULL, talloc_get_size(bytes), bytes);
1620 if (tevent_req_nomem(subreq, req)) {
1621 return tevent_req_post(req, ev);
1623 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1627 static void cli_mkdir_done(struct tevent_req *subreq)
1629 struct tevent_req *req = tevent_req_callback_data(
1630 subreq, struct tevent_req);
1633 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1634 TALLOC_FREE(subreq);
1635 if (tevent_req_nterror(req, status)) {
1638 tevent_req_done(req);
1641 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1643 return tevent_req_simple_recv_ntstatus(req);
1646 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1648 TALLOC_CTX *frame = NULL;
1649 struct tevent_context *ev;
1650 struct tevent_req *req;
1651 NTSTATUS status = NT_STATUS_OK;
1653 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1654 return cli_smb2_mkdir(cli, dname);
1657 frame = talloc_stackframe();
1659 if (smbXcli_conn_has_async_calls(cli->conn)) {
1661 * Can't use sync call while an async call is in flight
1663 status = NT_STATUS_INVALID_PARAMETER;
1667 ev = samba_tevent_context_init(frame);
1669 status = NT_STATUS_NO_MEMORY;
1673 req = cli_mkdir_send(frame, ev, cli, dname);
1675 status = NT_STATUS_NO_MEMORY;
1679 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1683 status = cli_mkdir_recv(req);
1690 /****************************************************************************
1692 ****************************************************************************/
1694 static void cli_rmdir_done(struct tevent_req *subreq);
1696 struct cli_rmdir_state {
1700 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1701 struct tevent_context *ev,
1702 struct cli_state *cli,
1705 struct tevent_req *req = NULL, *subreq = NULL;
1706 struct cli_rmdir_state *state = NULL;
1707 uint8_t additional_flags = 0;
1708 uint16_t additional_flags2 = 0;
1709 uint8_t *bytes = NULL;
1711 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1716 bytes = talloc_array(state, uint8_t, 1);
1717 if (tevent_req_nomem(bytes, req)) {
1718 return tevent_req_post(req, ev);
1721 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1722 strlen(dname)+1, NULL);
1724 if (tevent_req_nomem(bytes, req)) {
1725 return tevent_req_post(req, ev);
1728 if (clistr_is_previous_version_path(dname)) {
1729 additional_flags2 = FLAGS2_REPARSE_PATH;
1732 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1734 0, NULL, talloc_get_size(bytes), bytes);
1735 if (tevent_req_nomem(subreq, req)) {
1736 return tevent_req_post(req, ev);
1738 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1742 static void cli_rmdir_done(struct tevent_req *subreq)
1744 struct tevent_req *req = tevent_req_callback_data(
1745 subreq, struct tevent_req);
1748 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1749 TALLOC_FREE(subreq);
1750 if (tevent_req_nterror(req, status)) {
1753 tevent_req_done(req);
1756 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1758 return tevent_req_simple_recv_ntstatus(req);
1761 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1763 TALLOC_CTX *frame = NULL;
1764 struct tevent_context *ev;
1765 struct tevent_req *req;
1766 NTSTATUS status = NT_STATUS_OK;
1768 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1769 return cli_smb2_rmdir(cli, dname);
1772 frame = talloc_stackframe();
1774 if (smbXcli_conn_has_async_calls(cli->conn)) {
1776 * Can't use sync call while an async call is in flight
1778 status = NT_STATUS_INVALID_PARAMETER;
1782 ev = samba_tevent_context_init(frame);
1784 status = NT_STATUS_NO_MEMORY;
1788 req = cli_rmdir_send(frame, ev, cli, dname);
1790 status = NT_STATUS_NO_MEMORY;
1794 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1798 status = cli_rmdir_recv(req);
1805 /****************************************************************************
1806 Set or clear the delete on close flag.
1807 ****************************************************************************/
1815 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1817 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1818 NULL, 0, NULL, NULL, 0, NULL);
1819 tevent_req_simple_finish_ntstatus(subreq, status);
1822 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1823 struct tevent_context *ev,
1824 struct cli_state *cli,
1828 struct tevent_req *req = NULL, *subreq = NULL;
1829 struct doc_state *state = NULL;
1831 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1836 /* Setup setup word. */
1837 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1839 /* Setup param array. */
1840 SSVAL(state->param,0,fnum);
1841 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1843 /* Setup data array. */
1844 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1846 subreq = cli_trans_send(state, /* mem ctx. */
1847 ev, /* event ctx. */
1848 cli, /* cli_state. */
1849 0, /* additional_flags2 */
1850 SMBtrans2, /* cmd. */
1851 NULL, /* pipe name. */
1855 &state->setup, /* setup. */
1856 1, /* num setup uint16_t words. */
1857 0, /* max returned setup. */
1858 state->param, /* param. */
1860 2, /* max returned param. */
1861 state->data, /* data. */
1863 0); /* max returned data. */
1865 if (tevent_req_nomem(subreq, req)) {
1866 return tevent_req_post(req, ev);
1868 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1872 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1874 return tevent_req_simple_recv_ntstatus(req);
1877 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1879 TALLOC_CTX *frame = talloc_stackframe();
1880 struct tevent_context *ev = NULL;
1881 struct tevent_req *req = NULL;
1882 NTSTATUS status = NT_STATUS_OK;
1884 if (smbXcli_conn_has_async_calls(cli->conn)) {
1886 * Can't use sync call while an async call is in flight
1888 status = NT_STATUS_INVALID_PARAMETER;
1892 ev = samba_tevent_context_init(frame);
1894 status = NT_STATUS_NO_MEMORY;
1898 req = cli_nt_delete_on_close_send(frame,
1904 status = NT_STATUS_NO_MEMORY;
1908 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1912 status = cli_nt_delete_on_close_recv(req);
1919 struct cli_ntcreate1_state {
1922 struct smb_create_returns cr;
1923 struct tevent_req *subreq;
1926 static void cli_ntcreate1_done(struct tevent_req *subreq);
1927 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1929 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1930 struct tevent_context *ev,
1931 struct cli_state *cli,
1933 uint32_t CreatFlags,
1934 uint32_t DesiredAccess,
1935 uint32_t FileAttributes,
1936 uint32_t ShareAccess,
1937 uint32_t CreateDisposition,
1938 uint32_t CreateOptions,
1939 uint8_t SecurityFlags)
1941 struct tevent_req *req, *subreq;
1942 struct cli_ntcreate1_state *state;
1945 size_t converted_len;
1946 uint16_t additional_flags2 = 0;
1948 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1955 SCVAL(vwv+0, 0, 0xFF);
1960 if (cli->use_oplocks) {
1961 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1963 SIVAL(vwv+3, 1, CreatFlags);
1964 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1965 SIVAL(vwv+7, 1, DesiredAccess);
1966 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1967 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1968 SIVAL(vwv+13, 1, FileAttributes);
1969 SIVAL(vwv+15, 1, ShareAccess);
1970 SIVAL(vwv+17, 1, CreateDisposition);
1971 SIVAL(vwv+19, 1, CreateOptions |
1972 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1973 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1974 SCVAL(vwv+23, 1, SecurityFlags);
1976 bytes = talloc_array(state, uint8_t, 0);
1977 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1978 fname, strlen(fname)+1,
1981 if (clistr_is_previous_version_path(fname)) {
1982 additional_flags2 = FLAGS2_REPARSE_PATH;
1985 /* sigh. this copes with broken netapp filer behaviour */
1986 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1988 if (tevent_req_nomem(bytes, req)) {
1989 return tevent_req_post(req, ev);
1992 SSVAL(vwv+2, 1, converted_len);
1994 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
1995 additional_flags2, 24, vwv,
1996 talloc_get_size(bytes), bytes);
1997 if (tevent_req_nomem(subreq, req)) {
1998 return tevent_req_post(req, ev);
2000 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
2002 state->subreq = subreq;
2003 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
2008 static void cli_ntcreate1_done(struct tevent_req *subreq)
2010 struct tevent_req *req = tevent_req_callback_data(
2011 subreq, struct tevent_req);
2012 struct cli_ntcreate1_state *state = tevent_req_data(
2013 req, struct cli_ntcreate1_state);
2020 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2021 &num_bytes, &bytes);
2022 TALLOC_FREE(subreq);
2023 if (tevent_req_nterror(req, status)) {
2026 state->cr.oplock_level = CVAL(vwv+2, 0);
2027 state->fnum = SVAL(vwv+2, 1);
2028 state->cr.create_action = IVAL(vwv+3, 1);
2029 state->cr.creation_time = BVAL(vwv+5, 1);
2030 state->cr.last_access_time = BVAL(vwv+9, 1);
2031 state->cr.last_write_time = BVAL(vwv+13, 1);
2032 state->cr.change_time = BVAL(vwv+17, 1);
2033 state->cr.file_attributes = IVAL(vwv+21, 1);
2034 state->cr.allocation_size = BVAL(vwv+23, 1);
2035 state->cr.end_of_file = BVAL(vwv+27, 1);
2037 tevent_req_done(req);
2040 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2042 struct cli_ntcreate1_state *state = tevent_req_data(
2043 req, struct cli_ntcreate1_state);
2044 return tevent_req_cancel(state->subreq);
2047 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2049 struct smb_create_returns *cr)
2051 struct cli_ntcreate1_state *state = tevent_req_data(
2052 req, struct cli_ntcreate1_state);
2055 if (tevent_req_is_nterror(req, &status)) {
2058 *pfnum = state->fnum;
2062 return NT_STATUS_OK;
2065 struct cli_ntcreate_state {
2066 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2067 struct smb_create_returns *cr);
2068 struct smb_create_returns cr;
2070 struct tevent_req *subreq;
2073 static void cli_ntcreate_done(struct tevent_req *subreq);
2074 static bool cli_ntcreate_cancel(struct tevent_req *req);
2076 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2077 struct tevent_context *ev,
2078 struct cli_state *cli,
2080 uint32_t create_flags,
2081 uint32_t desired_access,
2082 uint32_t file_attributes,
2083 uint32_t share_access,
2084 uint32_t create_disposition,
2085 uint32_t create_options,
2086 uint8_t security_flags)
2088 struct tevent_req *req, *subreq;
2089 struct cli_ntcreate_state *state;
2091 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2096 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2097 state->recv = cli_smb2_create_fnum_recv;
2099 if (cli->use_oplocks) {
2100 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2103 subreq = cli_smb2_create_fnum_send(
2104 state, ev, cli, fname, create_flags, desired_access,
2105 file_attributes, share_access, create_disposition,
2108 state->recv = cli_ntcreate1_recv;
2109 subreq = cli_ntcreate1_send(
2110 state, ev, cli, fname, create_flags, desired_access,
2111 file_attributes, share_access, create_disposition,
2112 create_options, security_flags);
2114 if (tevent_req_nomem(subreq, req)) {
2115 return tevent_req_post(req, ev);
2117 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2119 state->subreq = subreq;
2120 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2125 static void cli_ntcreate_done(struct tevent_req *subreq)
2127 struct tevent_req *req = tevent_req_callback_data(
2128 subreq, struct tevent_req);
2129 struct cli_ntcreate_state *state = tevent_req_data(
2130 req, struct cli_ntcreate_state);
2133 status = state->recv(subreq, &state->fnum, &state->cr);
2134 TALLOC_FREE(subreq);
2135 if (tevent_req_nterror(req, status)) {
2138 tevent_req_done(req);
2141 static bool cli_ntcreate_cancel(struct tevent_req *req)
2143 struct cli_ntcreate_state *state = tevent_req_data(
2144 req, struct cli_ntcreate_state);
2145 return tevent_req_cancel(state->subreq);
2148 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2149 struct smb_create_returns *cr)
2151 struct cli_ntcreate_state *state = tevent_req_data(
2152 req, struct cli_ntcreate_state);
2155 if (tevent_req_is_nterror(req, &status)) {
2159 *fnum = state->fnum;
2164 return NT_STATUS_OK;
2167 NTSTATUS cli_ntcreate(struct cli_state *cli,
2169 uint32_t CreatFlags,
2170 uint32_t DesiredAccess,
2171 uint32_t FileAttributes,
2172 uint32_t ShareAccess,
2173 uint32_t CreateDisposition,
2174 uint32_t CreateOptions,
2175 uint8_t SecurityFlags,
2177 struct smb_create_returns *cr)
2179 TALLOC_CTX *frame = talloc_stackframe();
2180 struct tevent_context *ev;
2181 struct tevent_req *req;
2182 NTSTATUS status = NT_STATUS_NO_MEMORY;
2184 if (smbXcli_conn_has_async_calls(cli->conn)) {
2186 * Can't use sync call while an async call is in flight
2188 status = NT_STATUS_INVALID_PARAMETER;
2192 ev = samba_tevent_context_init(frame);
2197 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2198 DesiredAccess, FileAttributes, ShareAccess,
2199 CreateDisposition, CreateOptions,
2205 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2209 status = cli_ntcreate_recv(req, pfid, cr);
2215 struct cli_nttrans_create_state {
2217 struct smb_create_returns cr;
2220 static void cli_nttrans_create_done(struct tevent_req *subreq);
2222 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2223 struct tevent_context *ev,
2224 struct cli_state *cli,
2226 uint32_t CreatFlags,
2227 uint32_t DesiredAccess,
2228 uint32_t FileAttributes,
2229 uint32_t ShareAccess,
2230 uint32_t CreateDisposition,
2231 uint32_t CreateOptions,
2232 uint8_t SecurityFlags,
2233 struct security_descriptor *secdesc,
2234 struct ea_struct *eas,
2237 struct tevent_req *req, *subreq;
2238 struct cli_nttrans_create_state *state;
2240 uint8_t *secdesc_buf;
2243 size_t converted_len;
2244 uint16_t additional_flags2 = 0;
2246 req = tevent_req_create(mem_ctx,
2247 &state, struct cli_nttrans_create_state);
2252 if (secdesc != NULL) {
2253 status = marshall_sec_desc(talloc_tos(), secdesc,
2254 &secdesc_buf, &secdesc_len);
2255 if (tevent_req_nterror(req, status)) {
2256 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2257 nt_errstr(status)));
2258 return tevent_req_post(req, ev);
2269 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2270 return tevent_req_post(req, ev);
2273 param = talloc_array(state, uint8_t, 53);
2274 if (tevent_req_nomem(param, req)) {
2275 return tevent_req_post(req, ev);
2278 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2279 fname, strlen(fname),
2281 if (tevent_req_nomem(param, req)) {
2282 return tevent_req_post(req, ev);
2285 if (clistr_is_previous_version_path(fname)) {
2286 additional_flags2 = FLAGS2_REPARSE_PATH;
2289 SIVAL(param, 0, CreatFlags);
2290 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2291 SIVAL(param, 8, DesiredAccess);
2292 SIVAL(param, 12, 0x0); /* AllocationSize */
2293 SIVAL(param, 16, 0x0); /* AllocationSize */
2294 SIVAL(param, 20, FileAttributes);
2295 SIVAL(param, 24, ShareAccess);
2296 SIVAL(param, 28, CreateDisposition);
2297 SIVAL(param, 32, CreateOptions |
2298 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2299 SIVAL(param, 36, secdesc_len);
2300 SIVAL(param, 40, 0); /* EA length*/
2301 SIVAL(param, 44, converted_len);
2302 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2303 SCVAL(param, 52, SecurityFlags);
2305 subreq = cli_trans_send(state, ev, cli,
2306 additional_flags2, /* additional_flags2 */
2308 NULL, -1, /* name, fid */
2309 NT_TRANSACT_CREATE, 0,
2310 NULL, 0, 0, /* setup */
2311 param, talloc_get_size(param), 128, /* param */
2312 secdesc_buf, secdesc_len, 0); /* data */
2313 if (tevent_req_nomem(subreq, req)) {
2314 return tevent_req_post(req, ev);
2316 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2320 static void cli_nttrans_create_done(struct tevent_req *subreq)
2322 struct tevent_req *req = tevent_req_callback_data(
2323 subreq, struct tevent_req);
2324 struct cli_nttrans_create_state *state = tevent_req_data(
2325 req, struct cli_nttrans_create_state);
2330 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2331 NULL, 0, NULL, /* rsetup */
2332 ¶m, 69, &num_param,
2334 if (tevent_req_nterror(req, status)) {
2337 state->cr.oplock_level = CVAL(param, 0);
2338 state->fnum = SVAL(param, 2);
2339 state->cr.create_action = IVAL(param, 4);
2340 state->cr.creation_time = BVAL(param, 12);
2341 state->cr.last_access_time = BVAL(param, 20);
2342 state->cr.last_write_time = BVAL(param, 28);
2343 state->cr.change_time = BVAL(param, 36);
2344 state->cr.file_attributes = IVAL(param, 44);
2345 state->cr.allocation_size = BVAL(param, 48);
2346 state->cr.end_of_file = BVAL(param, 56);
2349 tevent_req_done(req);
2352 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2354 struct smb_create_returns *cr)
2356 struct cli_nttrans_create_state *state = tevent_req_data(
2357 req, struct cli_nttrans_create_state);
2360 if (tevent_req_is_nterror(req, &status)) {
2363 *fnum = state->fnum;
2367 return NT_STATUS_OK;
2370 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2372 uint32_t CreatFlags,
2373 uint32_t DesiredAccess,
2374 uint32_t FileAttributes,
2375 uint32_t ShareAccess,
2376 uint32_t CreateDisposition,
2377 uint32_t CreateOptions,
2378 uint8_t SecurityFlags,
2379 struct security_descriptor *secdesc,
2380 struct ea_struct *eas,
2383 struct smb_create_returns *cr)
2385 TALLOC_CTX *frame = talloc_stackframe();
2386 struct tevent_context *ev;
2387 struct tevent_req *req;
2388 NTSTATUS status = NT_STATUS_NO_MEMORY;
2390 if (smbXcli_conn_has_async_calls(cli->conn)) {
2392 * Can't use sync call while an async call is in flight
2394 status = NT_STATUS_INVALID_PARAMETER;
2397 ev = samba_tevent_context_init(frame);
2401 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2402 DesiredAccess, FileAttributes,
2403 ShareAccess, CreateDisposition,
2404 CreateOptions, SecurityFlags,
2405 secdesc, eas, num_eas);
2409 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2412 status = cli_nttrans_create_recv(req, pfid, cr);
2418 /****************************************************************************
2420 WARNING: if you open with O_WRONLY then getattrE won't work!
2421 ****************************************************************************/
2423 struct cli_openx_state {
2430 static void cli_openx_done(struct tevent_req *subreq);
2432 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2433 struct tevent_context *ev,
2434 struct cli_state *cli, const char *fname,
2435 int flags, int share_mode,
2436 struct tevent_req **psmbreq)
2438 struct tevent_req *req, *subreq;
2439 struct cli_openx_state *state;
2441 unsigned accessmode;
2442 uint8_t additional_flags;
2443 uint16_t additional_flags2 = 0;
2446 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2452 if (flags & O_CREAT) {
2455 if (!(flags & O_EXCL)) {
2456 if (flags & O_TRUNC)
2462 accessmode = (share_mode<<4);
2464 if ((flags & O_ACCMODE) == O_RDWR) {
2466 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2471 if ((flags & O_SYNC) == O_SYNC) {
2472 accessmode |= (1<<14);
2476 if (share_mode == DENY_FCB) {
2480 SCVAL(state->vwv + 0, 0, 0xFF);
2481 SCVAL(state->vwv + 0, 1, 0);
2482 SSVAL(state->vwv + 1, 0, 0);
2483 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2484 SSVAL(state->vwv + 3, 0, accessmode);
2485 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2486 SSVAL(state->vwv + 5, 0, 0);
2487 SIVAL(state->vwv + 6, 0, 0);
2488 SSVAL(state->vwv + 8, 0, openfn);
2489 SIVAL(state->vwv + 9, 0, 0);
2490 SIVAL(state->vwv + 11, 0, 0);
2491 SIVAL(state->vwv + 13, 0, 0);
2493 additional_flags = 0;
2495 if (cli->use_oplocks) {
2496 /* if using oplocks then ask for a batch oplock via
2497 core and extended methods */
2499 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2500 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2503 bytes = talloc_array(state, uint8_t, 0);
2504 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2505 strlen(fname)+1, NULL);
2507 if (tevent_req_nomem(bytes, req)) {
2508 return tevent_req_post(req, ev);
2511 if (clistr_is_previous_version_path(fname)) {
2512 additional_flags2 = FLAGS2_REPARSE_PATH;
2515 state->bytes.iov_base = (void *)bytes;
2516 state->bytes.iov_len = talloc_get_size(bytes);
2518 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2519 additional_flags2, 15, state->vwv, 1, &state->bytes);
2520 if (subreq == NULL) {
2524 tevent_req_set_callback(subreq, cli_openx_done, req);
2529 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2530 struct cli_state *cli, const char *fname,
2531 int flags, int share_mode)
2533 struct tevent_req *req, *subreq;
2536 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2542 status = smb1cli_req_chain_submit(&subreq, 1);
2543 if (tevent_req_nterror(req, status)) {
2544 return tevent_req_post(req, ev);
2549 static void cli_openx_done(struct tevent_req *subreq)
2551 struct tevent_req *req = tevent_req_callback_data(
2552 subreq, struct tevent_req);
2553 struct cli_openx_state *state = tevent_req_data(
2554 req, struct cli_openx_state);
2559 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2561 TALLOC_FREE(subreq);
2562 if (tevent_req_nterror(req, status)) {
2565 state->fnum = SVAL(vwv+2, 0);
2566 tevent_req_done(req);
2569 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2571 struct cli_openx_state *state = tevent_req_data(
2572 req, struct cli_openx_state);
2575 if (tevent_req_is_nterror(req, &status)) {
2578 *pfnum = state->fnum;
2579 return NT_STATUS_OK;
2582 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2583 int share_mode, uint16_t *pfnum)
2585 TALLOC_CTX *frame = talloc_stackframe();
2586 struct tevent_context *ev;
2587 struct tevent_req *req;
2588 NTSTATUS status = NT_STATUS_NO_MEMORY;
2590 if (smbXcli_conn_has_async_calls(cli->conn)) {
2592 * Can't use sync call while an async call is in flight
2594 status = NT_STATUS_INVALID_PARAMETER;
2598 ev = samba_tevent_context_init(frame);
2603 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2608 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2612 status = cli_openx_recv(req, pfnum);
2617 /****************************************************************************
2618 Synchronous wrapper function that does an NtCreateX open by preference
2619 and falls back to openX if this fails.
2620 ****************************************************************************/
2622 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2623 int share_mode_in, uint16_t *pfnum)
2626 unsigned int openfn = 0;
2627 unsigned int dos_deny = 0;
2628 uint32_t access_mask, share_mode, create_disposition, create_options;
2629 struct smb_create_returns cr;
2631 /* Do the initial mapping into OpenX parameters. */
2632 if (flags & O_CREAT) {
2635 if (!(flags & O_EXCL)) {
2636 if (flags & O_TRUNC)
2642 dos_deny = (share_mode_in<<4);
2644 if ((flags & O_ACCMODE) == O_RDWR) {
2646 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2651 if ((flags & O_SYNC) == O_SYNC) {
2652 dos_deny |= (1<<14);
2656 if (share_mode_in == DENY_FCB) {
2661 /* Hmmm. This is what I think the above code
2662 should look like if it's using the constants
2665 if (flags & O_CREAT) {
2666 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2668 if (!(flags & O_EXCL)) {
2669 if (flags & O_TRUNC)
2670 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2672 openfn |= OPENX_FILE_EXISTS_OPEN;
2675 dos_deny = SET_DENY_MODE(share_mode_in);
2677 if ((flags & O_ACCMODE) == O_RDWR) {
2678 dos_deny |= DOS_OPEN_RDWR;
2679 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2680 dos_deny |= DOS_OPEN_WRONLY;
2684 if ((flags & O_SYNC) == O_SYNC) {
2685 dos_deny |= FILE_SYNC_OPENMODE;
2689 if (share_mode_in == DENY_FCB) {
2694 if (!map_open_params_to_ntcreate(fname, dos_deny,
2695 openfn, &access_mask,
2696 &share_mode, &create_disposition,
2697 &create_options, NULL)) {
2701 status = cli_ntcreate(cli,
2713 /* Try and cope will all varients of "we don't do this call"
2714 and fall back to openX. */
2716 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2717 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2718 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2719 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2720 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2721 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2722 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2723 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2724 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2728 if (NT_STATUS_IS_OK(status) &&
2729 (create_options & FILE_NON_DIRECTORY_FILE) &&
2730 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2733 * Some (broken) servers return a valid handle
2734 * for directories even if FILE_NON_DIRECTORY_FILE
2735 * is set. Just close the handle and set the
2736 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2738 status = cli_close(cli, *pfnum);
2739 if (!NT_STATUS_IS_OK(status)) {
2742 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2743 /* Set this so libsmbclient can retrieve it. */
2744 cli->raw_status = status;
2751 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2754 /****************************************************************************
2756 ****************************************************************************/
2758 struct cli_close_state {
2762 static void cli_close_done(struct tevent_req *subreq);
2764 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2765 struct tevent_context *ev,
2766 struct cli_state *cli,
2768 struct tevent_req **psubreq)
2770 struct tevent_req *req, *subreq;
2771 struct cli_close_state *state;
2773 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2778 SSVAL(state->vwv+0, 0, fnum);
2779 SIVALS(state->vwv+1, 0, -1);
2781 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
2782 3, state->vwv, 0, NULL);
2783 if (subreq == NULL) {
2787 tevent_req_set_callback(subreq, cli_close_done, req);
2792 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2793 struct tevent_context *ev,
2794 struct cli_state *cli,
2797 struct tevent_req *req, *subreq;
2800 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2805 status = smb1cli_req_chain_submit(&subreq, 1);
2806 if (tevent_req_nterror(req, status)) {
2807 return tevent_req_post(req, ev);
2812 static void cli_close_done(struct tevent_req *subreq)
2814 struct tevent_req *req = tevent_req_callback_data(
2815 subreq, struct tevent_req);
2818 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2819 TALLOC_FREE(subreq);
2820 if (tevent_req_nterror(req, status)) {
2823 tevent_req_done(req);
2826 NTSTATUS cli_close_recv(struct tevent_req *req)
2828 return tevent_req_simple_recv_ntstatus(req);
2831 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2833 TALLOC_CTX *frame = NULL;
2834 struct tevent_context *ev;
2835 struct tevent_req *req;
2836 NTSTATUS status = NT_STATUS_OK;
2838 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2839 return cli_smb2_close_fnum(cli, fnum);
2842 frame = talloc_stackframe();
2844 if (smbXcli_conn_has_async_calls(cli->conn)) {
2846 * Can't use sync call while an async call is in flight
2848 status = NT_STATUS_INVALID_PARAMETER;
2852 ev = samba_tevent_context_init(frame);
2854 status = NT_STATUS_NO_MEMORY;
2858 req = cli_close_send(frame, ev, cli, fnum);
2860 status = NT_STATUS_NO_MEMORY;
2864 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2868 status = cli_close_recv(req);
2874 /****************************************************************************
2875 Truncate a file to a specified size
2876 ****************************************************************************/
2878 struct ftrunc_state {
2884 static void cli_ftruncate_done(struct tevent_req *subreq)
2886 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2887 NULL, 0, NULL, NULL, 0, NULL);
2888 tevent_req_simple_finish_ntstatus(subreq, status);
2891 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2892 struct tevent_context *ev,
2893 struct cli_state *cli,
2897 struct tevent_req *req = NULL, *subreq = NULL;
2898 struct ftrunc_state *state = NULL;
2900 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2905 /* Setup setup word. */
2906 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2908 /* Setup param array. */
2909 SSVAL(state->param,0,fnum);
2910 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2911 SSVAL(state->param,4,0);
2913 /* Setup data array. */
2914 SBVAL(state->data, 0, size);
2916 subreq = cli_trans_send(state, /* mem ctx. */
2917 ev, /* event ctx. */
2918 cli, /* cli_state. */
2919 0, /* additional_flags2 */
2920 SMBtrans2, /* cmd. */
2921 NULL, /* pipe name. */
2925 &state->setup, /* setup. */
2926 1, /* num setup uint16_t words. */
2927 0, /* max returned setup. */
2928 state->param, /* param. */
2930 2, /* max returned param. */
2931 state->data, /* data. */
2933 0); /* max returned data. */
2935 if (tevent_req_nomem(subreq, req)) {
2936 return tevent_req_post(req, ev);
2938 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2942 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2944 return tevent_req_simple_recv_ntstatus(req);
2947 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2949 TALLOC_CTX *frame = talloc_stackframe();
2950 struct tevent_context *ev = NULL;
2951 struct tevent_req *req = NULL;
2952 NTSTATUS status = NT_STATUS_OK;
2954 if (smbXcli_conn_has_async_calls(cli->conn)) {
2956 * Can't use sync call while an async call is in flight
2958 status = NT_STATUS_INVALID_PARAMETER;
2962 ev = samba_tevent_context_init(frame);
2964 status = NT_STATUS_NO_MEMORY;
2968 req = cli_ftruncate_send(frame,
2974 status = NT_STATUS_NO_MEMORY;
2978 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2982 status = cli_ftruncate_recv(req);
2989 /****************************************************************************
2990 send a lock with a specified locktype
2991 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2992 ****************************************************************************/
2994 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2995 uint32_t offset, uint32_t len,
2996 int timeout, unsigned char locktype)
3001 unsigned int set_timeout = 0;
3002 unsigned int saved_timeout = 0;
3004 SCVAL(vwv + 0, 0, 0xff);
3005 SCVAL(vwv + 0, 1, 0);
3006 SSVAL(vwv + 1, 0, 0);
3007 SSVAL(vwv + 2, 0, fnum);
3008 SCVAL(vwv + 3, 0, locktype);
3009 SCVAL(vwv + 3, 1, 0);
3010 SIVALS(vwv + 4, 0, timeout);
3011 SSVAL(vwv + 6, 0, 0);
3012 SSVAL(vwv + 7, 0, 1);
3014 SSVAL(bytes, 0, cli_getpid(cli));
3015 SIVAL(bytes, 2, offset);
3016 SIVAL(bytes, 6, len);
3019 if (timeout == -1) {
3020 set_timeout = 0x7FFFFFFF;
3022 set_timeout = timeout + 2*1000;
3024 saved_timeout = cli_set_timeout(cli, set_timeout);
3027 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3028 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3030 if (saved_timeout != 0) {
3031 cli_set_timeout(cli, saved_timeout);
3037 /****************************************************************************
3039 note that timeout is in units of 2 milliseconds
3040 ****************************************************************************/
3042 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3043 uint32_t offset, uint32_t len, int timeout,
3044 enum brl_type lock_type)
3048 status = cli_locktype(cli, fnum, offset, len, timeout,
3049 (lock_type == READ_LOCK? 1 : 0));
3053 /****************************************************************************
3055 ****************************************************************************/
3057 struct cli_unlock_state {
3062 static void cli_unlock_done(struct tevent_req *subreq);
3064 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3065 struct tevent_context *ev,
3066 struct cli_state *cli,
3072 struct tevent_req *req = NULL, *subreq = NULL;
3073 struct cli_unlock_state *state = NULL;
3074 uint8_t additional_flags = 0;
3076 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3081 SCVAL(state->vwv+0, 0, 0xFF);
3082 SSVAL(state->vwv+2, 0, fnum);
3083 SCVAL(state->vwv+3, 0, 0);
3084 SIVALS(state->vwv+4, 0, 0);
3085 SSVAL(state->vwv+6, 0, 1);
3086 SSVAL(state->vwv+7, 0, 0);
3088 SSVAL(state->data, 0, cli_getpid(cli));
3089 SIVAL(state->data, 2, offset);
3090 SIVAL(state->data, 6, len);
3092 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3093 8, state->vwv, 10, state->data);
3094 if (tevent_req_nomem(subreq, req)) {
3095 return tevent_req_post(req, ev);
3097 tevent_req_set_callback(subreq, cli_unlock_done, req);
3101 static void cli_unlock_done(struct tevent_req *subreq)
3103 struct tevent_req *req = tevent_req_callback_data(
3104 subreq, struct tevent_req);
3107 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3108 TALLOC_FREE(subreq);
3109 if (tevent_req_nterror(req, status)) {
3112 tevent_req_done(req);
3115 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3117 return tevent_req_simple_recv_ntstatus(req);
3120 NTSTATUS cli_unlock(struct cli_state *cli,
3125 TALLOC_CTX *frame = talloc_stackframe();
3126 struct tevent_context *ev;
3127 struct tevent_req *req;
3128 NTSTATUS status = NT_STATUS_OK;
3130 if (smbXcli_conn_has_async_calls(cli->conn)) {
3132 * Can't use sync call while an async call is in flight
3134 status = NT_STATUS_INVALID_PARAMETER;
3138 ev = samba_tevent_context_init(frame);
3140 status = NT_STATUS_NO_MEMORY;
3144 req = cli_unlock_send(frame, ev, cli,
3147 status = NT_STATUS_NO_MEMORY;
3151 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3155 status = cli_unlock_recv(req);
3162 /****************************************************************************
3163 Lock a file with 64 bit offsets.
3164 ****************************************************************************/
3166 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3167 uint64_t offset, uint64_t len, int timeout,
3168 enum brl_type lock_type)
3172 unsigned int set_timeout = 0;
3173 unsigned int saved_timeout = 0;
3177 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3178 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3181 ltype = (lock_type == READ_LOCK? 1 : 0);
3182 ltype |= LOCKING_ANDX_LARGE_FILES;
3184 SCVAL(vwv + 0, 0, 0xff);
3185 SCVAL(vwv + 0, 1, 0);
3186 SSVAL(vwv + 1, 0, 0);
3187 SSVAL(vwv + 2, 0, fnum);
3188 SCVAL(vwv + 3, 0, ltype);
3189 SCVAL(vwv + 3, 1, 0);
3190 SIVALS(vwv + 4, 0, timeout);
3191 SSVAL(vwv + 6, 0, 0);
3192 SSVAL(vwv + 7, 0, 1);
3194 SIVAL(bytes, 0, cli_getpid(cli));
3195 SOFF_T_R(bytes, 4, offset);
3196 SOFF_T_R(bytes, 12, len);
3199 if (timeout == -1) {
3200 set_timeout = 0x7FFFFFFF;
3202 set_timeout = timeout + 2*1000;
3204 saved_timeout = cli_set_timeout(cli, set_timeout);
3207 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3208 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3210 if (saved_timeout != 0) {
3211 cli_set_timeout(cli, saved_timeout);
3217 /****************************************************************************
3218 Unlock a file with 64 bit offsets.
3219 ****************************************************************************/
3221 struct cli_unlock64_state {
3226 static void cli_unlock64_done(struct tevent_req *subreq);
3228 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3229 struct tevent_context *ev,
3230 struct cli_state *cli,
3236 struct tevent_req *req = NULL, *subreq = NULL;
3237 struct cli_unlock64_state *state = NULL;
3238 uint8_t additional_flags = 0;
3240 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3245 SCVAL(state->vwv+0, 0, 0xff);
3246 SSVAL(state->vwv+2, 0, fnum);
3247 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3248 SIVALS(state->vwv+4, 0, 0);
3249 SSVAL(state->vwv+6, 0, 1);
3250 SSVAL(state->vwv+7, 0, 0);
3252 SIVAL(state->data, 0, cli_getpid(cli));
3253 SOFF_T_R(state->data, 4, offset);
3254 SOFF_T_R(state->data, 12, len);
3256 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3257 8, state->vwv, 20, state->data);
3258 if (tevent_req_nomem(subreq, req)) {
3259 return tevent_req_post(req, ev);
3261 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3265 static void cli_unlock64_done(struct tevent_req *subreq)
3267 struct tevent_req *req = tevent_req_callback_data(
3268 subreq, struct tevent_req);
3271 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3272 TALLOC_FREE(subreq);
3273 if (tevent_req_nterror(req, status)) {
3276 tevent_req_done(req);
3279 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3281 return tevent_req_simple_recv_ntstatus(req);
3284 NTSTATUS cli_unlock64(struct cli_state *cli,
3289 TALLOC_CTX *frame = talloc_stackframe();
3290 struct tevent_context *ev;
3291 struct tevent_req *req;
3292 NTSTATUS status = NT_STATUS_OK;
3294 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3295 return cli_unlock(cli, fnum, offset, len);
3298 if (smbXcli_conn_has_async_calls(cli->conn)) {
3300 * Can't use sync call while an async call is in flight
3302 status = NT_STATUS_INVALID_PARAMETER;
3306 ev = samba_tevent_context_init(frame);
3308 status = NT_STATUS_NO_MEMORY;
3312 req = cli_unlock64_send(frame, ev, cli,
3315 status = NT_STATUS_NO_MEMORY;
3319 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3323 status = cli_unlock64_recv(req);
3330 /****************************************************************************
3331 Get/unlock a POSIX lock on a file - internal function.
3332 ****************************************************************************/
3334 struct posix_lock_state {
3337 uint8_t data[POSIX_LOCK_DATA_SIZE];
3340 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3342 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3343 NULL, 0, NULL, NULL, 0, NULL);
3344 tevent_req_simple_finish_ntstatus(subreq, status);
3347 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3348 struct tevent_context *ev,
3349 struct cli_state *cli,
3354 enum brl_type lock_type)
3356 struct tevent_req *req = NULL, *subreq = NULL;
3357 struct posix_lock_state *state = NULL;
3359 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3364 /* Setup setup word. */
3365 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3367 /* Setup param array. */
3368 SSVAL(&state->param, 0, fnum);
3369 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3371 /* Setup data array. */
3372 switch (lock_type) {
3374 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3375 POSIX_LOCK_TYPE_READ);
3378 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3379 POSIX_LOCK_TYPE_WRITE);
3382 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3383 POSIX_LOCK_TYPE_UNLOCK);
3390 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3391 POSIX_LOCK_FLAG_WAIT);
3393 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3394 POSIX_LOCK_FLAG_NOWAIT);
3397 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3398 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3399 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3401 subreq = cli_trans_send(state, /* mem ctx. */
3402 ev, /* event ctx. */
3403 cli, /* cli_state. */
3404 0, /* additional_flags2 */
3405 SMBtrans2, /* cmd. */
3406 NULL, /* pipe name. */
3410 &state->setup, /* setup. */
3411 1, /* num setup uint16_t words. */
3412 0, /* max returned setup. */
3413 state->param, /* param. */
3415 2, /* max returned param. */
3416 state->data, /* data. */
3417 POSIX_LOCK_DATA_SIZE, /* num data. */
3418 0); /* max returned data. */
3420 if (tevent_req_nomem(subreq, req)) {
3421 return tevent_req_post(req, ev);
3423 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3427 /****************************************************************************
3429 ****************************************************************************/
3431 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3432 struct tevent_context *ev,
3433 struct cli_state *cli,
3438 enum brl_type lock_type)
3440 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3441 wait_lock, lock_type);
3444 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3446 return tevent_req_simple_recv_ntstatus(req);
3449 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3450 uint64_t offset, uint64_t len,
3451 bool wait_lock, enum brl_type lock_type)
3453 TALLOC_CTX *frame = talloc_stackframe();
3454 struct tevent_context *ev = NULL;
3455 struct tevent_req *req = NULL;
3456 NTSTATUS status = NT_STATUS_OK;
3458 if (smbXcli_conn_has_async_calls(cli->conn)) {
3460 * Can't use sync call while an async call is in flight
3462 status = NT_STATUS_INVALID_PARAMETER;
3466 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3467 status = NT_STATUS_INVALID_PARAMETER;
3471 ev = samba_tevent_context_init(frame);
3473 status = NT_STATUS_NO_MEMORY;
3477 req = cli_posix_lock_send(frame,
3486 status = NT_STATUS_NO_MEMORY;
3490 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3494 status = cli_posix_lock_recv(req);
3501 /****************************************************************************
3502 POSIX Unlock a file.
3503 ****************************************************************************/
3505 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3506 struct tevent_context *ev,
3507 struct cli_state *cli,
3512 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3513 false, UNLOCK_LOCK);
3516 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3518 return tevent_req_simple_recv_ntstatus(req);
3521 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3523 TALLOC_CTX *frame = talloc_stackframe();
3524 struct tevent_context *ev = NULL;
3525 struct tevent_req *req = NULL;
3526 NTSTATUS status = NT_STATUS_OK;
3528 if (smbXcli_conn_has_async_calls(cli->conn)) {
3530 * Can't use sync call while an async call is in flight
3532 status = NT_STATUS_INVALID_PARAMETER;
3536 ev = samba_tevent_context_init(frame);
3538 status = NT_STATUS_NO_MEMORY;
3542 req = cli_posix_unlock_send(frame,
3549 status = NT_STATUS_NO_MEMORY;
3553 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3557 status = cli_posix_unlock_recv(req);
3564 /****************************************************************************
3565 Do a SMBgetattrE call.
3566 ****************************************************************************/
3568 static void cli_getattrE_done(struct tevent_req *subreq);
3570 struct cli_getattrE_state {
3580 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3581 struct tevent_context *ev,
3582 struct cli_state *cli,
3585 struct tevent_req *req = NULL, *subreq = NULL;
3586 struct cli_getattrE_state *state = NULL;
3587 uint8_t additional_flags = 0;
3589 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3594 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3595 SSVAL(state->vwv+0,0,fnum);
3597 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3598 1, state->vwv, 0, NULL);
3599 if (tevent_req_nomem(subreq, req)) {
3600 return tevent_req_post(req, ev);
3602 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3606 static void cli_getattrE_done(struct tevent_req *subreq)
3608 struct tevent_req *req = tevent_req_callback_data(
3609 subreq, struct tevent_req);
3610 struct cli_getattrE_state *state = tevent_req_data(
3611 req, struct cli_getattrE_state);
3613 uint16_t *vwv = NULL;
3616 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3618 TALLOC_FREE(subreq);
3619 if (tevent_req_nterror(req, status)) {
3623 state->size = (off_t)IVAL(vwv+6,0);
3624 state->attr = SVAL(vwv+10,0);
3625 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3626 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3627 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3629 tevent_req_done(req);
3632 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3635 time_t *change_time,
3636 time_t *access_time,
3639 struct cli_getattrE_state *state = tevent_req_data(
3640 req, struct cli_getattrE_state);
3643 if (tevent_req_is_nterror(req, &status)) {
3647 *attr = state->attr;
3650 *size = state->size;
3653 *change_time = state->change_time;
3656 *access_time = state->access_time;
3659 *write_time = state->write_time;
3661 return NT_STATUS_OK;
3664 NTSTATUS cli_getattrE(struct cli_state *cli,
3668 time_t *change_time,
3669 time_t *access_time,
3672 TALLOC_CTX *frame = NULL;
3673 struct tevent_context *ev = NULL;
3674 struct tevent_req *req = NULL;
3675 NTSTATUS status = NT_STATUS_OK;
3677 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3678 return cli_smb2_getattrE(cli,
3687 frame = talloc_stackframe();
3689 if (smbXcli_conn_has_async_calls(cli->conn)) {
3691 * Can't use sync call while an async call is in flight
3693 status = NT_STATUS_INVALID_PARAMETER;
3697 ev = samba_tevent_context_init(frame);
3699 status = NT_STATUS_NO_MEMORY;
3703 req = cli_getattrE_send(frame, ev, cli, fnum);
3705 status = NT_STATUS_NO_MEMORY;
3709 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3713 status = cli_getattrE_recv(req,
3725 /****************************************************************************
3727 ****************************************************************************/
3729 static void cli_getatr_done(struct tevent_req *subreq);
3731 struct cli_getatr_state {
3738 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3739 struct tevent_context *ev,
3740 struct cli_state *cli,
3743 struct tevent_req *req = NULL, *subreq = NULL;
3744 struct cli_getatr_state *state = NULL;
3745 uint8_t additional_flags = 0;
3746 uint8_t *bytes = NULL;
3748 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3753 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3755 bytes = talloc_array(state, uint8_t, 1);
3756 if (tevent_req_nomem(bytes, req)) {
3757 return tevent_req_post(req, ev);
3760 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3761 strlen(fname)+1, NULL);
3763 if (tevent_req_nomem(bytes, req)) {
3764 return tevent_req_post(req, ev);
3767 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags, 0,
3768 0, NULL, talloc_get_size(bytes), bytes);
3769 if (tevent_req_nomem(subreq, req)) {
3770 return tevent_req_post(req, ev);
3772 tevent_req_set_callback(subreq, cli_getatr_done, req);
3776 static void cli_getatr_done(struct tevent_req *subreq)
3778 struct tevent_req *req = tevent_req_callback_data(
3779 subreq, struct tevent_req);
3780 struct cli_getatr_state *state = tevent_req_data(
3781 req, struct cli_getatr_state);
3783 uint16_t *vwv = NULL;
3786 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3788 TALLOC_FREE(subreq);
3789 if (tevent_req_nterror(req, status)) {
3793 state->attr = SVAL(vwv+0,0);
3794 state->size = (off_t)IVAL(vwv+3,0);
3795 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3797 tevent_req_done(req);
3800 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3805 struct cli_getatr_state *state = tevent_req_data(
3806 req, struct cli_getatr_state);
3809 if (tevent_req_is_nterror(req, &status)) {
3813 *attr = state->attr;
3816 *size = state->size;
3819 *write_time = state->write_time;
3821 return NT_STATUS_OK;
3824 NTSTATUS cli_getatr(struct cli_state *cli,
3830 TALLOC_CTX *frame = NULL;
3831 struct tevent_context *ev = NULL;
3832 struct tevent_req *req = NULL;
3833 NTSTATUS status = NT_STATUS_OK;
3835 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3836 return cli_smb2_getatr(cli,
3843 frame = talloc_stackframe();
3845 if (smbXcli_conn_has_async_calls(cli->conn)) {
3847 * Can't use sync call while an async call is in flight
3849 status = NT_STATUS_INVALID_PARAMETER;
3853 ev = samba_tevent_context_init(frame);
3855 status = NT_STATUS_NO_MEMORY;
3859 req = cli_getatr_send(frame, ev, cli, fname);
3861 status = NT_STATUS_NO_MEMORY;
3865 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3869 status = cli_getatr_recv(req,
3879 /****************************************************************************
3880 Do a SMBsetattrE call.
3881 ****************************************************************************/
3883 static void cli_setattrE_done(struct tevent_req *subreq);
3885 struct cli_setattrE_state {
3889 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3890 struct tevent_context *ev,
3891 struct cli_state *cli,
3897 struct tevent_req *req = NULL, *subreq = NULL;
3898 struct cli_setattrE_state *state = NULL;
3899 uint8_t additional_flags = 0;
3901 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3906 SSVAL(state->vwv+0, 0, fnum);
3907 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3908 smb1cli_conn_server_time_zone(cli->conn));
3909 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3910 smb1cli_conn_server_time_zone(cli->conn));
3911 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3912 smb1cli_conn_server_time_zone(cli->conn));
3914 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3915 7, state->vwv, 0, NULL);
3916 if (tevent_req_nomem(subreq, req)) {
3917 return tevent_req_post(req, ev);
3919 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3923 static void cli_setattrE_done(struct tevent_req *subreq)
3925 struct tevent_req *req = tevent_req_callback_data(
3926 subreq, struct tevent_req);
3929 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3930 TALLOC_FREE(subreq);
3931 if (tevent_req_nterror(req, status)) {
3934 tevent_req_done(req);
3937 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3939 return tevent_req_simple_recv_ntstatus(req);
3942 NTSTATUS cli_setattrE(struct cli_state *cli,
3948 TALLOC_CTX *frame = NULL;
3949 struct tevent_context *ev = NULL;
3950 struct tevent_req *req = NULL;
3951 NTSTATUS status = NT_STATUS_OK;
3953 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3954 return cli_smb2_setattrE(cli,
3961 frame = talloc_stackframe();
3963 if (smbXcli_conn_has_async_calls(cli->conn)) {
3965 * Can't use sync call while an async call is in flight
3967 status = NT_STATUS_INVALID_PARAMETER;
3971 ev = samba_tevent_context_init(frame);
3973 status = NT_STATUS_NO_MEMORY;
3977 req = cli_setattrE_send(frame, ev,
3985 status = NT_STATUS_NO_MEMORY;
3989 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3993 status = cli_setattrE_recv(req);
4000 /****************************************************************************
4001 Do a SMBsetatr call.
4002 ****************************************************************************/
4004 static void cli_setatr_done(struct tevent_req *subreq);
4006 struct cli_setatr_state {
4010 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4011 struct tevent_context *ev,
4012 struct cli_state *cli,
4017 struct tevent_req *req = NULL, *subreq = NULL;
4018 struct cli_setatr_state *state = NULL;
4019 uint8_t additional_flags = 0;
4020 uint8_t *bytes = NULL;
4022 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4027 SSVAL(state->vwv+0, 0, attr);
4028 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4030 bytes = talloc_array(state, uint8_t, 1);
4031 if (tevent_req_nomem(bytes, req)) {
4032 return tevent_req_post(req, ev);
4035 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4036 strlen(fname)+1, NULL);
4037 if (tevent_req_nomem(bytes, req)) {
4038 return tevent_req_post(req, ev);
4040 bytes = talloc_realloc(state, bytes, uint8_t,
4041 talloc_get_size(bytes)+1);
4042 if (tevent_req_nomem(bytes, req)) {
4043 return tevent_req_post(req, ev);
4046 bytes[talloc_get_size(bytes)-1] = 4;
4047 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4049 if (tevent_req_nomem(bytes, req)) {
4050 return tevent_req_post(req, ev);
4053 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags, 0,
4054 8, state->vwv, talloc_get_size(bytes), bytes);
4055 if (tevent_req_nomem(subreq, req)) {
4056 return tevent_req_post(req, ev);
4058 tevent_req_set_callback(subreq, cli_setatr_done, req);
4062 static void cli_setatr_done(struct tevent_req *subreq)
4064 struct tevent_req *req = tevent_req_callback_data(
4065 subreq, struct tevent_req);
4068 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4069 TALLOC_FREE(subreq);
4070 if (tevent_req_nterror(req, status)) {
4073 tevent_req_done(req);
4076 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4078 return tevent_req_simple_recv_ntstatus(req);
4081 NTSTATUS cli_setatr(struct cli_state *cli,
4086 TALLOC_CTX *frame = NULL;
4087 struct tevent_context *ev = NULL;
4088 struct tevent_req *req = NULL;
4089 NTSTATUS status = NT_STATUS_OK;
4091 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4092 return cli_smb2_setatr(cli,
4098 frame = talloc_stackframe();
4100 if (smbXcli_conn_has_async_calls(cli->conn)) {
4102 * Can't use sync call while an async call is in flight
4104 status = NT_STATUS_INVALID_PARAMETER;
4108 ev = samba_tevent_context_init(frame);
4110 status = NT_STATUS_NO_MEMORY;
4114 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4116 status = NT_STATUS_NO_MEMORY;
4120 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4124 status = cli_setatr_recv(req);
4131 /****************************************************************************
4132 Check for existance of a dir.
4133 ****************************************************************************/
4135 static void cli_chkpath_done(struct tevent_req *subreq);
4137 struct cli_chkpath_state {
4141 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4142 struct tevent_context *ev,
4143 struct cli_state *cli,
4146 struct tevent_req *req = NULL, *subreq = NULL;
4147 struct cli_chkpath_state *state = NULL;
4148 uint8_t additional_flags = 0;
4149 uint8_t *bytes = NULL;
4151 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4156 bytes = talloc_array(state, uint8_t, 1);
4157 if (tevent_req_nomem(bytes, req)) {
4158 return tevent_req_post(req, ev);
4161 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4162 strlen(fname)+1, NULL);
4164 if (tevent_req_nomem(bytes, req)) {
4165 return tevent_req_post(req, ev);
4168 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags, 0,
4169 0, NULL, talloc_get_size(bytes), bytes);
4170 if (tevent_req_nomem(subreq, req)) {
4171 return tevent_req_post(req, ev);
4173 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4177 static void cli_chkpath_done(struct tevent_req *subreq)
4179 struct tevent_req *req = tevent_req_callback_data(
4180 subreq, struct tevent_req);
4183 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4184 TALLOC_FREE(subreq);
4185 if (tevent_req_nterror(req, status)) {
4188 tevent_req_done(req);
4191 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4193 return tevent_req_simple_recv_ntstatus(req);
4196 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4198 TALLOC_CTX *frame = talloc_stackframe();
4199 struct tevent_context *ev = NULL;
4200 struct tevent_req *req = NULL;
4202 NTSTATUS status = NT_STATUS_OK;
4204 if (smbXcli_conn_has_async_calls(cli->conn)) {
4206 * Can't use sync call while an async call is in flight
4208 status = NT_STATUS_INVALID_PARAMETER;
4212 path2 = talloc_strdup(frame, path);
4214 status = NT_STATUS_NO_MEMORY;
4217 trim_char(path2,'\0','\\');
4219 path2 = talloc_strdup(frame, "\\");
4221 status = NT_STATUS_NO_MEMORY;
4226 ev = samba_tevent_context_init(frame);
4228 status = NT_STATUS_NO_MEMORY;
4232 req = cli_chkpath_send(frame, ev, cli, path2);
4234 status = NT_STATUS_NO_MEMORY;
4238 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4242 status = cli_chkpath_recv(req);
4249 /****************************************************************************
4251 ****************************************************************************/
4253 static void cli_dskattr_done(struct tevent_req *subreq);
4255 struct cli_dskattr_state {
4261 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4262 struct tevent_context *ev,
4263 struct cli_state *cli)
4265 struct tevent_req *req = NULL, *subreq = NULL;
4266 struct cli_dskattr_state *state = NULL;
4267 uint8_t additional_flags = 0;
4269 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4274 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4276 if (tevent_req_nomem(subreq, req)) {
4277 return tevent_req_post(req, ev);
4279 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4283 static void cli_dskattr_done(struct tevent_req *subreq)
4285 struct tevent_req *req = tevent_req_callback_data(
4286 subreq, struct tevent_req);
4287 struct cli_dskattr_state *state = tevent_req_data(
4288 req, struct cli_dskattr_state);
4290 uint16_t *vwv = NULL;
4293 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4295 TALLOC_FREE(subreq);
4296 if (tevent_req_nterror(req, status)) {
4299 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4300 state->total = SVAL(vwv+0, 0);
4301 state->avail = SVAL(vwv+3, 0);
4302 tevent_req_done(req);
4305 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4307 struct cli_dskattr_state *state = tevent_req_data(
4308 req, struct cli_dskattr_state);
4311 if (tevent_req_is_nterror(req, &status)) {
4314 *bsize = state->bsize;
4315 *total = state->total;
4316 *avail = state->avail;
4317 return NT_STATUS_OK;
4320 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4322 TALLOC_CTX *frame = NULL;
4323 struct tevent_context *ev = NULL;
4324 struct tevent_req *req = NULL;
4325 NTSTATUS status = NT_STATUS_OK;
4327 frame = talloc_stackframe();
4329 if (smbXcli_conn_has_async_calls(cli->conn)) {
4331 * Can't use sync call while an async call is in flight
4333 status = NT_STATUS_INVALID_PARAMETER;
4337 ev = samba_tevent_context_init(frame);
4339 status = NT_STATUS_NO_MEMORY;
4343 req = cli_dskattr_send(frame, ev, cli);
4345 status = NT_STATUS_NO_MEMORY;
4349 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4353 status = cli_dskattr_recv(req, bsize, total, avail);
4360 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4361 uint64_t *total, uint64_t *avail)
4363 uint64_t sectors_per_block;
4364 uint64_t bytes_per_sector;
4365 int old_bsize, old_total, old_avail;
4368 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4369 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4373 * Try the trans2 disk full size info call first.
4374 * We already use this in SMBC_fstatvfs_ctx().
4375 * Ignore 'actual_available_units' as we only
4376 * care about the quota for the caller.
4379 status = cli_get_fs_full_size_info(cli,
4386 /* Try and cope will all varients of "we don't do this call"
4387 and fall back to cli_dskattr. */
4389 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4390 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4391 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4392 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4393 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4394 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4395 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4396 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4397 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4398 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4402 if (!NT_STATUS_IS_OK(status)) {
4407 *bsize = sectors_per_block *
4411 return NT_STATUS_OK;
4415 /* Old SMB1 core protocol fallback. */
4416 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4417 if (!NT_STATUS_IS_OK(status)) {
4421 *bsize = (uint64_t)old_bsize;
4424 *total = (uint64_t)old_total;
4427 *avail = (uint64_t)old_avail;
4429 return NT_STATUS_OK;
4432 /****************************************************************************
4433 Create and open a temporary file.
4434 ****************************************************************************/
4436 static void cli_ctemp_done(struct tevent_req *subreq);
4438 struct ctemp_state {
4444 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4445 struct tevent_context *ev,
4446 struct cli_state *cli,
4449 struct tevent_req *req = NULL, *subreq = NULL;
4450 struct ctemp_state *state = NULL;
4451 uint8_t additional_flags = 0;
4452 uint8_t *bytes = NULL;
4454 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4459 SSVAL(state->vwv,0,0);
4460 SIVALS(state->vwv+1,0,-1);
4462 bytes = talloc_array(state, uint8_t, 1);
4463 if (tevent_req_nomem(bytes, req)) {
4464 return tevent_req_post(req, ev);
4467 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4468 strlen(path)+1, NULL);
4469 if (tevent_req_nomem(bytes, req)) {
4470 return tevent_req_post(req, ev);
4473 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags, 0,
4474 3, state->vwv, talloc_get_size(bytes), bytes);
4475 if (tevent_req_nomem(subreq, req)) {
4476 return tevent_req_post(req, ev);
4478 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4482 static void cli_ctemp_done(struct tevent_req *subreq)
4484 struct tevent_req *req = tevent_req_callback_data(
4485 subreq, struct tevent_req);
4486 struct ctemp_state *state = tevent_req_data(
4487 req, struct ctemp_state);
4491 uint32_t num_bytes = 0;
4492 uint8_t *bytes = NULL;
4494 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4495 &num_bytes, &bytes);
4496 TALLOC_FREE(subreq);
4497 if (tevent_req_nterror(req, status)) {
4501 state->fnum = SVAL(vwv+0, 0);
4503 /* From W2K3, the result is just the ASCII name */
4504 if (num_bytes < 2) {
4505 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4509 if (pull_string_talloc(state,
4516 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4519 tevent_req_done(req);
4522 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4527 struct ctemp_state *state = tevent_req_data(req,
4528 struct ctemp_state);
4531 if (tevent_req_is_nterror(req, &status)) {
4534 *pfnum = state->fnum;
4535 *outfile = talloc_strdup(ctx, state->ret_path);
4537 return NT_STATUS_NO_MEMORY;
4539 return NT_STATUS_OK;
4542 NTSTATUS cli_ctemp(struct cli_state *cli,
4548 TALLOC_CTX *frame = talloc_stackframe();
4549 struct tevent_context *ev;
4550 struct tevent_req *req;
4551 NTSTATUS status = NT_STATUS_OK;
4553 if (smbXcli_conn_has_async_calls(cli->conn)) {
4555 * Can't use sync call while an async call is in flight
4557 status = NT_STATUS_INVALID_PARAMETER;
4561 ev = samba_tevent_context_init(frame);
4563 status = NT_STATUS_NO_MEMORY;
4567 req = cli_ctemp_send(frame, ev, cli, path);
4569 status = NT_STATUS_NO_MEMORY;
4573 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4577 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4585 send a raw ioctl - used by the torture code
4587 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4592 SSVAL(vwv+0, 0, fnum);
4593 SSVAL(vwv+1, 0, code>>16);
4594 SSVAL(vwv+2, 0, (code&0xFFFF));
4596 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4597 NULL, 0, NULL, NULL, NULL, NULL);
4598 if (!NT_STATUS_IS_OK(status)) {
4601 *blob = data_blob_null;
4602 return NT_STATUS_OK;
4605 /*********************************************************
4606 Set an extended attribute utility fn.
4607 *********************************************************/
4609 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4610 uint8_t *param, unsigned int param_len,
4611 const char *ea_name,
4612 const char *ea_val, size_t ea_len)
4615 unsigned int data_len = 0;
4616 uint8_t *data = NULL;
4618 size_t ea_namelen = strlen(ea_name);
4621 SSVAL(setup, 0, setup_val);
4623 if (ea_namelen == 0 && ea_len == 0) {
4625 data = talloc_array(talloc_tos(),
4629 return NT_STATUS_NO_MEMORY;
4632 SIVAL(p,0,data_len);
4634 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4635 data = talloc_array(talloc_tos(),
4639 return NT_STATUS_NO_MEMORY;
4642 SIVAL(p,0,data_len);
4644 SCVAL(p, 0, 0); /* EA flags. */
4645 SCVAL(p, 1, ea_namelen);
4646 SSVAL(p, 2, ea_len);
4647 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4648 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4651 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4653 param, param_len, 2,
4656 NULL, 0, NULL, /* rsetup */
4657 NULL, 0, NULL, /* rparam */
4658 NULL, 0, NULL); /* rdata */
4663 /*********************************************************
4664 Set an extended attribute on a pathname.
4665 *********************************************************/
4667 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4668 const char *ea_name, const char *ea_val,
4671 unsigned int param_len = 0;
4674 TALLOC_CTX *frame = NULL;
4676 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4677 return cli_smb2_set_ea_path(cli,
4684 frame = talloc_stackframe();
4686 param = talloc_array(frame, uint8_t, 6);
4688 status = NT_STATUS_NO_MEMORY;
4691 SSVAL(param,0,SMB_INFO_SET_EA);
4695 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4696 path, strlen(path)+1,
4698 param_len = talloc_get_size(param);
4700 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4701 ea_name, ea_val, ea_len);
4709 /*********************************************************
4710 Set an extended attribute on an fnum.
4711 *********************************************************/
4713 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4714 const char *ea_name, const char *ea_val,
4719 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4720 return cli_smb2_set_ea_fnum(cli,
4727 memset(param, 0, 6);
4728 SSVAL(param,0,fnum);
4729 SSVAL(param,2,SMB_INFO_SET_EA);
4731 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4732 ea_name, ea_val, ea_len);
4735 /*********************************************************
4736 Get an extended attribute list utility fn.
4737 *********************************************************/
4739 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4741 size_t *pnum_eas, struct ea_struct **pea_list)
4743 struct ea_struct *ea_list = NULL;
4748 if (rdata_len < 4) {
4752 ea_size = (size_t)IVAL(rdata,0);
4753 if (ea_size > rdata_len) {
4758 /* No EA's present. */
4767 /* Validate the EA list and count it. */
4768 for (num_eas = 0; ea_size >= 4; num_eas++) {
4769 unsigned int ea_namelen = CVAL(p,1);
4770 unsigned int ea_valuelen = SVAL(p,2);
4771 if (ea_namelen == 0) {
4774 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4777 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4778 p += 4 + ea_namelen + 1 + ea_valuelen;
4787 *pnum_eas = num_eas;
4789 /* Caller only wants number of EA's. */
4793 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4798 ea_size = (size_t)IVAL(rdata,0);
4801 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4802 struct ea_struct *ea = &ea_list[num_eas];
4803 fstring unix_ea_name;
4804 unsigned int ea_namelen = CVAL(p,1);
4805 unsigned int ea_valuelen = SVAL(p,2);
4807 ea->flags = CVAL(p,0);
4808 unix_ea_name[0] = '\0';
4809 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4810 ea->name = talloc_strdup(ea_list, unix_ea_name);
4814 /* Ensure the value is null terminated (in case it's a string). */
4815 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4816 if (!ea->value.data) {
4820 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4822 ea->value.data[ea_valuelen] = 0;
4824 p += 4 + ea_namelen + 1 + ea_valuelen;
4827 *pea_list = ea_list;
4831 TALLOC_FREE(ea_list);
4835 /*********************************************************
4836 Get an extended attribute list from a pathname.
4837 *********************************************************/
4839 struct cli_get_ea_list_path_state {
4844 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4846 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4847 struct tevent_context *ev,
4848 struct cli_state *cli,
4851 struct tevent_req *req, *subreq;
4852 struct cli_get_ea_list_path_state *state;
4854 req = tevent_req_create(mem_ctx, &state,
4855 struct cli_get_ea_list_path_state);
4859 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4860 SMB_INFO_QUERY_ALL_EAS, 4,
4862 if (tevent_req_nomem(subreq, req)) {
4863 return tevent_req_post(req, ev);
4865 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4869 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4871 struct tevent_req *req = tevent_req_callback_data(
4872 subreq, struct tevent_req);
4873 struct cli_get_ea_list_path_state *state = tevent_req_data(
4874 req, struct cli_get_ea_list_path_state);
4877 status = cli_qpathinfo_recv(subreq, state, &state->data,
4879 TALLOC_FREE(subreq);
4880 if (tevent_req_nterror(req, status)) {
4883 tevent_req_done(req);
4886 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4887 size_t *pnum_eas, struct ea_struct **peas)
4889 struct cli_get_ea_list_path_state *state = tevent_req_data(
4890 req, struct cli_get_ea_list_path_state);
4893 if (tevent_req_is_nterror(req, &status)) {
4896 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4898 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4900 return NT_STATUS_OK;
4903 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4906 struct ea_struct **pea_list)
4908 TALLOC_CTX *frame = NULL;
4909 struct tevent_context *ev = NULL;
4910 struct tevent_req *req = NULL;
4911 NTSTATUS status = NT_STATUS_NO_MEMORY;
4913 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4914 return cli_smb2_get_ea_list_path(cli,
4921 frame = talloc_stackframe();
4923 if (smbXcli_conn_has_async_calls(cli->conn)) {
4925 * Can't use sync call while an async call is in flight
4927 status = NT_STATUS_INVALID_PARAMETER;
4930 ev = samba_tevent_context_init(frame);
4934 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4938 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4941 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4947 /****************************************************************************
4948 Convert open "flags" arg to uint32_t on wire.
4949 ****************************************************************************/
4951 static uint32_t open_flags_to_wire(int flags)
4953 int open_mode = flags & O_ACCMODE;
4956 switch (open_mode) {
4958 ret |= SMB_O_WRONLY;
4965 ret |= SMB_O_RDONLY;
4969 if (flags & O_CREAT) {
4972 if (flags & O_EXCL) {
4975 if (flags & O_TRUNC) {
4979 if (flags & O_SYNC) {
4983 if (flags & O_APPEND) {
4984 ret |= SMB_O_APPEND;
4986 #if defined(O_DIRECT)
4987 if (flags & O_DIRECT) {
4988 ret |= SMB_O_DIRECT;
4991 #if defined(O_DIRECTORY)
4992 if (flags & O_DIRECTORY) {
4993 ret |= SMB_O_DIRECTORY;
4999 /****************************************************************************
5000 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5001 ****************************************************************************/
5003 struct posix_open_state {
5007 uint16_t fnum; /* Out */
5010 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5012 struct tevent_req *req = tevent_req_callback_data(
5013 subreq, struct tevent_req);
5014 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5019 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5020 NULL, 0, NULL, &data, 12, &num_data);
5021 TALLOC_FREE(subreq);
5022 if (tevent_req_nterror(req, status)) {
5025 state->fnum = SVAL(data,2);
5026 tevent_req_done(req);
5029 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5030 struct tevent_context *ev,
5031 struct cli_state *cli,
5037 struct tevent_req *req = NULL, *subreq = NULL;
5038 struct posix_open_state *state = NULL;
5039 uint32_t wire_flags = open_flags_to_wire(flags);
5041 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5046 /* Setup setup word. */
5047 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5049 /* Setup param array. */
5050 state->param = talloc_array(state, uint8_t, 6);
5051 if (tevent_req_nomem(state->param, req)) {
5052 return tevent_req_post(req, ev);
5054 memset(state->param, '\0', 6);
5055 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5057 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5058 strlen(fname)+1, NULL);
5060 if (tevent_req_nomem(state->param, req)) {
5061 return tevent_req_post(req, ev);
5064 /* Setup data words. */
5066 wire_flags |= SMB_O_DIRECTORY;
5069 SIVAL(state->data,0,0); /* No oplock. */
5070 SIVAL(state->data,4,wire_flags);
5071 SIVAL(state->data,8,unix_perms_to_wire(mode));
5072 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5073 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5075 subreq = cli_trans_send(state, /* mem ctx. */
5076 ev, /* event ctx. */
5077 cli, /* cli_state. */
5078 0, /* additional_flags2 */
5079 SMBtrans2, /* cmd. */
5080 NULL, /* pipe name. */
5084 &state->setup, /* setup. */
5085 1, /* num setup uint16_t words. */
5086 0, /* max returned setup. */
5087 state->param, /* param. */
5088 talloc_get_size(state->param),/* num param. */
5089 2, /* max returned param. */
5090 state->data, /* data. */
5092 12); /* max returned data. */
5094 if (tevent_req_nomem(subreq, req)) {
5095 return tevent_req_post(req, ev);
5097 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5101 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5102 struct tevent_context *ev,
5103 struct cli_state *cli,
5108 return cli_posix_open_internal_send(mem_ctx, ev,
5109 cli, fname, flags, mode, false);
5112 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5114 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5117 if (tevent_req_is_nterror(req, &status)) {
5120 *pfnum = state->fnum;
5121 return NT_STATUS_OK;
5124 /****************************************************************************
5125 Open - POSIX semantics. Doesn't request oplock.
5126 ****************************************************************************/
5128 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5129 int flags, mode_t mode, uint16_t *pfnum)
5132 TALLOC_CTX *frame = talloc_stackframe();
5133 struct tevent_context *ev = NULL;
5134 struct tevent_req *req = NULL;
5135 NTSTATUS status = NT_STATUS_OK;
5137 if (smbXcli_conn_has_async_calls(cli->conn)) {
5139 * Can't use sync call while an async call is in flight
5141 status = NT_STATUS_INVALID_PARAMETER;
5145 ev = samba_tevent_context_init(frame);
5147 status = NT_STATUS_NO_MEMORY;
5151 req = cli_posix_open_send(frame,
5158 status = NT_STATUS_NO_MEMORY;
5162 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5166 status = cli_posix_open_recv(req, pfnum);
5173 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5174 struct tevent_context *ev,
5175 struct cli_state *cli,
5179 return cli_posix_open_internal_send(mem_ctx, ev,
5180 cli, fname, O_CREAT, mode, true);
5183 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5185 return tevent_req_simple_recv_ntstatus(req);
5188 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5190 TALLOC_CTX *frame = talloc_stackframe();
5191 struct tevent_context *ev = NULL;
5192 struct tevent_req *req = NULL;
5193 NTSTATUS status = NT_STATUS_OK;
5195 if (smbXcli_conn_has_async_calls(cli->conn)) {
5197 * Can't use sync call while an async call is in flight
5199 status = NT_STATUS_INVALID_PARAMETER;
5203 ev = samba_tevent_context_init(frame);
5205 status = NT_STATUS_NO_MEMORY;
5209 req = cli_posix_mkdir_send(frame,
5215 status = NT_STATUS_NO_MEMORY;
5219 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5223 status = cli_posix_mkdir_recv(req);
5230 /****************************************************************************
5231 unlink or rmdir - POSIX semantics.
5232 ****************************************************************************/
5234 struct cli_posix_unlink_internal_state {
5238 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5240 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5241 struct tevent_context *ev,
5242 struct cli_state *cli,
5246 struct tevent_req *req = NULL, *subreq = NULL;
5247 struct cli_posix_unlink_internal_state *state = NULL;
5249 req = tevent_req_create(mem_ctx, &state,
5250 struct cli_posix_unlink_internal_state);
5255 /* Setup data word. */
5256 SSVAL(state->data, 0, level);
5258 subreq = cli_setpathinfo_send(state, ev, cli,
5259 SMB_POSIX_PATH_UNLINK,
5261 state->data, sizeof(state->data));
5262 if (tevent_req_nomem(subreq, req)) {
5263 return tevent_req_post(req, ev);
5265 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5269 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5271 NTSTATUS status = cli_setpathinfo_recv(subreq);
5272 tevent_req_simple_finish_ntstatus(subreq, status);
5275 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5276 struct tevent_context *ev,
5277 struct cli_state *cli,
5280 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5281 SMB_POSIX_UNLINK_FILE_TARGET);
5284 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5286 return tevent_req_simple_recv_ntstatus(req);
5289 /****************************************************************************
5290 unlink - POSIX semantics.
5291 ****************************************************************************/
5293 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5295 TALLOC_CTX *frame = talloc_stackframe();
5296 struct tevent_context *ev = NULL;
5297 struct tevent_req *req = NULL;
5298 NTSTATUS status = NT_STATUS_OK;
5300 if (smbXcli_conn_has_async_calls(cli->conn)) {
5302 * Can't use sync call while an async call is in flight
5304 status = NT_STATUS_INVALID_PARAMETER;
5308 ev = samba_tevent_context_init(frame);
5310 status = NT_STATUS_NO_MEMORY;
5314 req = cli_posix_unlink_send(frame,
5319 status = NT_STATUS_NO_MEMORY;
5323 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5327 status = cli_posix_unlink_recv(req);
5334 /****************************************************************************
5335 rmdir - POSIX semantics.
5336 ****************************************************************************/
5338 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5339 struct tevent_context *ev,
5340 struct cli_state *cli,
5343 return cli_posix_unlink_internal_send(
5344 mem_ctx, ev, cli, fname,
5345 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5348 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5350 return tevent_req_simple_recv_ntstatus(req);
5353 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5355 TALLOC_CTX *frame = talloc_stackframe();
5356 struct tevent_context *ev = NULL;
5357 struct tevent_req *req = NULL;
5358 NTSTATUS status = NT_STATUS_OK;
5360 if (smbXcli_conn_has_async_calls(cli->conn)) {
5362 * Can't use sync call while an async call is in flight
5364 status = NT_STATUS_INVALID_PARAMETER;
5368 ev = samba_tevent_context_init(frame);
5370 status = NT_STATUS_NO_MEMORY;
5374 req = cli_posix_rmdir_send(frame,
5379 status = NT_STATUS_NO_MEMORY;
5383 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5387 status = cli_posix_rmdir_recv(req, frame);
5394 /****************************************************************************
5396 ****************************************************************************/
5398 struct cli_notify_state {
5400 uint32_t num_changes;
5401 struct notify_change *changes;
5404 static void cli_notify_done(struct tevent_req *subreq);
5406 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5407 struct tevent_context *ev,
5408 struct cli_state *cli, uint16_t fnum,
5409 uint32_t buffer_size,
5410 uint32_t completion_filter, bool recursive)
5412 struct tevent_req *req, *subreq;
5413 struct cli_notify_state *state;
5414 unsigned old_timeout;
5416 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5421 SIVAL(state->setup, 0, completion_filter);
5422 SSVAL(state->setup, 4, fnum);
5423 SSVAL(state->setup, 6, recursive);
5426 * Notifies should not time out
5428 old_timeout = cli_set_timeout(cli, 0);
5430 subreq = cli_trans_send(
5431 state, /* mem ctx. */
5432 ev, /* event ctx. */
5433 cli, /* cli_state. */
5434 0, /* additional_flags2 */
5435 SMBnttrans, /* cmd. */
5436 NULL, /* pipe name. */
5438 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5440 (uint16_t *)state->setup, /* setup. */
5441 4, /* num setup uint16_t words. */
5442 0, /* max returned setup. */
5445 buffer_size, /* max returned param. */
5448 0); /* max returned data. */
5450 cli_set_timeout(cli, old_timeout);
5452 if (tevent_req_nomem(subreq, req)) {
5453 return tevent_req_post(req, ev);
5455 tevent_req_set_callback(subreq, cli_notify_done, req);
5459 static void cli_notify_done(struct tevent_req *subreq)
5461 struct tevent_req *req = tevent_req_callback_data(
5462 subreq, struct tevent_req);
5463 struct cli_notify_state *state = tevent_req_data(
5464 req, struct cli_notify_state);
5467 uint32_t i, ofs, num_params;
5470 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5471 ¶ms, 0, &num_params, NULL, 0, NULL);
5472 TALLOC_FREE(subreq);
5473 if (tevent_req_nterror(req, status)) {
5474 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5478 state->num_changes = 0;
5481 while (num_params - ofs > 12) {
5482 uint32_t next = IVAL(params, ofs);
5483 state->num_changes += 1;
5485 if ((next == 0) || (ofs+next >= num_params)) {
5491 state->changes = talloc_array(state, struct notify_change,
5492 state->num_changes);
5493 if (tevent_req_nomem(state->changes, req)) {
5494 TALLOC_FREE(params);
5500 for (i=0; i<state->num_changes; i++) {
5501 uint32_t next = IVAL(params, ofs);
5502 uint32_t len = IVAL(params, ofs+8);
5506 if (trans_oob(num_params, ofs + 12, len)) {
5507 TALLOC_FREE(params);
5509 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5513 state->changes[i].action = IVAL(params, ofs+4);
5514 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5515 &name, params+ofs+12, len,
5516 STR_TERMINATE|STR_UNICODE);
5518 TALLOC_FREE(params);
5519 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5522 state->changes[i].name = name;
5526 TALLOC_FREE(params);
5527 tevent_req_done(req);
5530 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5531 uint32_t *pnum_changes,
5532 struct notify_change **pchanges)
5534 struct cli_notify_state *state = tevent_req_data(
5535 req, struct cli_notify_state);
5538 if (tevent_req_is_nterror(req, &status)) {
5542 *pnum_changes = state->num_changes;
5543 *pchanges = talloc_move(mem_ctx, &state->changes);
5544 return NT_STATUS_OK;
5547 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5548 uint32_t completion_filter, bool recursive,
5549 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5550 struct notify_change **pchanges)
5552 TALLOC_CTX *frame = talloc_stackframe();
5553 struct tevent_context *ev;
5554 struct tevent_req *req;
5555 NTSTATUS status = NT_STATUS_NO_MEMORY;
5557 if (smbXcli_conn_has_async_calls(cli->conn)) {
5559 * Can't use sync call while an async call is in flight
5561 status = NT_STATUS_INVALID_PARAMETER;
5564 ev = samba_tevent_context_init(frame);
5568 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5569 completion_filter, recursive);
5573 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5576 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5582 struct cli_qpathinfo_state {
5591 static void cli_qpathinfo_done(struct tevent_req *subreq);
5593 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5594 struct tevent_context *ev,
5595 struct cli_state *cli, const char *fname,
5596 uint16_t level, uint32_t min_rdata,
5599 struct tevent_req *req, *subreq;
5600 struct cli_qpathinfo_state *state;
5601 uint16_t additional_flags2 = 0;
5603 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5607 state->min_rdata = min_rdata;
5608 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5610 state->param = talloc_zero_array(state, uint8_t, 6);
5611 if (tevent_req_nomem(state->param, req)) {
5612 return tevent_req_post(req, ev);
5614 SSVAL(state->param, 0, level);
5615 state->param = trans2_bytes_push_str(
5616 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5617 if (tevent_req_nomem(state->param, req)) {
5618 return tevent_req_post(req, ev);
5621 if (clistr_is_previous_version_path(fname) &&
5622 !INFO_LEVEL_IS_UNIX(level)) {
5623 additional_flags2 = FLAGS2_REPARSE_PATH;
5626 subreq = cli_trans_send(
5627 state, /* mem ctx. */
5628 ev, /* event ctx. */
5629 cli, /* cli_state. */
5630 additional_flags2, /* additional_flags2 */
5631 SMBtrans2, /* cmd. */
5632 NULL, /* pipe name. */
5636 state->setup, /* setup. */
5637 1, /* num setup uint16_t words. */
5638 0, /* max returned setup. */
5639 state->param, /* param. */
5640 talloc_get_size(state->param), /* num param. */
5641 2, /* max returned param. */
5644 max_rdata); /* max returned data. */
5646 if (tevent_req_nomem(subreq, req)) {
5647 return tevent_req_post(req, ev);
5649 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5653 static void cli_qpathinfo_done(struct tevent_req *subreq)
5655 struct tevent_req *req = tevent_req_callback_data(
5656 subreq, struct tevent_req);
5657 struct cli_qpathinfo_state *state = tevent_req_data(
5658 req, struct cli_qpathinfo_state);
5661 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5663 &state->rdata, state->min_rdata,
5665 if (tevent_req_nterror(req, status)) {
5668 tevent_req_done(req);
5671 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5672 uint8_t **rdata, uint32_t *num_rdata)
5674 struct cli_qpathinfo_state *state = tevent_req_data(
5675 req, struct cli_qpathinfo_state);
5678 if (tevent_req_is_nterror(req, &status)) {
5681 if (rdata != NULL) {
5682 *rdata = talloc_move(mem_ctx, &state->rdata);
5684 TALLOC_FREE(state->rdata);
5686 if (num_rdata != NULL) {
5687 *num_rdata = state->num_rdata;
5689 return NT_STATUS_OK;
5692 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5693 const char *fname, uint16_t level, uint32_t min_rdata,
5695 uint8_t **rdata, uint32_t *num_rdata)
5697 TALLOC_CTX *frame = talloc_stackframe();
5698 struct tevent_context *ev;
5699 struct tevent_req *req;
5700 NTSTATUS status = NT_STATUS_NO_MEMORY;
5702 if (smbXcli_conn_has_async_calls(cli->conn)) {
5704 * Can't use sync call while an async call is in flight
5706 status = NT_STATUS_INVALID_PARAMETER;
5709 ev = samba_tevent_context_init(frame);
5713 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5718 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5721 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5727 struct cli_qfileinfo_state {
5731 uint16_t recv_flags2;
5737 static void cli_qfileinfo_done(struct tevent_req *subreq);
5739 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5740 struct tevent_context *ev,
5741 struct cli_state *cli, uint16_t fnum,
5742 uint16_t level, uint32_t min_rdata,
5745 struct tevent_req *req, *subreq;
5746 struct cli_qfileinfo_state *state;
5748 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5752 state->min_rdata = min_rdata;
5753 SSVAL(state->param, 0, fnum);
5754 SSVAL(state->param, 2, level);
5755 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5757 subreq = cli_trans_send(
5758 state, /* mem ctx. */
5759 ev, /* event ctx. */
5760 cli, /* cli_state. */
5761 0, /* additional_flags2 */
5762 SMBtrans2, /* cmd. */
5763 NULL, /* pipe name. */
5767 state->setup, /* setup. */
5768 1, /* num setup uint16_t words. */
5769 0, /* max returned setup. */
5770 state->param, /* param. */
5771 sizeof(state->param), /* num param. */
5772 2, /* max returned param. */
5775 max_rdata); /* max returned data. */
5777 if (tevent_req_nomem(subreq, req)) {
5778 return tevent_req_post(req, ev);
5780 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5784 static void cli_qfileinfo_done(struct tevent_req *subreq)
5786 struct tevent_req *req = tevent_req_callback_data(
5787 subreq, struct tevent_req);
5788 struct cli_qfileinfo_state *state = tevent_req_data(
5789 req, struct cli_qfileinfo_state);
5792 status = cli_trans_recv(subreq, state,
5793 &state->recv_flags2,
5796 &state->rdata, state->min_rdata,
5798 if (tevent_req_nterror(req, status)) {
5801 tevent_req_done(req);
5804 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5805 uint16_t *recv_flags2,
5806 uint8_t **rdata, uint32_t *num_rdata)
5808 struct cli_qfileinfo_state *state = tevent_req_data(
5809 req, struct cli_qfileinfo_state);
5812 if (tevent_req_is_nterror(req, &status)) {
5816 if (recv_flags2 != NULL) {
5817 *recv_flags2 = state->recv_flags2;
5819 if (rdata != NULL) {
5820 *rdata = talloc_move(mem_ctx, &state->rdata);
5822 TALLOC_FREE(state->rdata);
5824 if (num_rdata != NULL) {
5825 *num_rdata = state->num_rdata;
5827 return NT_STATUS_OK;
5830 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5831 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5832 uint32_t max_rdata, uint16_t *recv_flags2,
5833 uint8_t **rdata, uint32_t *num_rdata)
5835 TALLOC_CTX *frame = talloc_stackframe();
5836 struct tevent_context *ev;
5837 struct tevent_req *req;
5838 NTSTATUS status = NT_STATUS_NO_MEMORY;
5840 if (smbXcli_conn_has_async_calls(cli->conn)) {
5842 * Can't use sync call while an async call is in flight
5844 status = NT_STATUS_INVALID_PARAMETER;
5847 ev = samba_tevent_context_init(frame);
5851 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5856 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5859 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5865 struct cli_flush_state {
5869 static void cli_flush_done(struct tevent_req *subreq);
5871 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5872 struct tevent_context *ev,
5873 struct cli_state *cli,
5876 struct tevent_req *req, *subreq;
5877 struct cli_flush_state *state;
5879 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5883 SSVAL(state->vwv + 0, 0, fnum);
5885 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5887 if (tevent_req_nomem(subreq, req)) {
5888 return tevent_req_post(req, ev);
5890 tevent_req_set_callback(subreq, cli_flush_done, req);
5894 static void cli_flush_done(struct tevent_req *subreq)
5896 struct tevent_req *req = tevent_req_callback_data(
5897 subreq, struct tevent_req);
5900 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5901 TALLOC_FREE(subreq);
5902 if (tevent_req_nterror(req, status)) {
5905 tevent_req_done(req);
5908 NTSTATUS cli_flush_recv(struct tevent_req *req)
5910 return tevent_req_simple_recv_ntstatus(req);
5913 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5915 TALLOC_CTX *frame = talloc_stackframe();
5916 struct tevent_context *ev;
5917 struct tevent_req *req;
5918 NTSTATUS status = NT_STATUS_NO_MEMORY;
5920 if (smbXcli_conn_has_async_calls(cli->conn)) {
5922 * Can't use sync call while an async call is in flight
5924 status = NT_STATUS_INVALID_PARAMETER;
5927 ev = samba_tevent_context_init(frame);
5931 req = cli_flush_send(frame, ev, cli, fnum);
5935 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5938 status = cli_flush_recv(req);
5944 struct cli_shadow_copy_data_state {
5951 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5953 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5954 struct tevent_context *ev,
5955 struct cli_state *cli,
5959 struct tevent_req *req, *subreq;
5960 struct cli_shadow_copy_data_state *state;
5963 req = tevent_req_create(mem_ctx, &state,
5964 struct cli_shadow_copy_data_state);
5968 state->get_names = get_names;
5969 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5971 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5972 SSVAL(state->setup + 2, 0, fnum);
5973 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5974 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5976 subreq = cli_trans_send(
5977 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5978 state->setup, ARRAY_SIZE(state->setup), 0,
5981 if (tevent_req_nomem(subreq, req)) {
5982 return tevent_req_post(req, ev);
5984 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5988 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5990 struct tevent_req *req = tevent_req_callback_data(
5991 subreq, struct tevent_req);
5992 struct cli_shadow_copy_data_state *state = tevent_req_data(
5993 req, struct cli_shadow_copy_data_state);
5996 status = cli_trans_recv(subreq, state, NULL,
5997 NULL, 0, NULL, /* setup */
5998 NULL, 0, NULL, /* param */
5999 &state->data, 12, &state->num_data);
6000 TALLOC_FREE(subreq);
6001 if (tevent_req_nterror(req, status)) {
6004 tevent_req_done(req);
6007 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6008 char ***pnames, int *pnum_names)
6010 struct cli_shadow_copy_data_state *state = tevent_req_data(
6011 req, struct cli_shadow_copy_data_state);
6017 if (tevent_req_is_nterror(req, &status)) {
6020 num_names = IVAL(state->data, 4);
6021 dlength = IVAL(state->data, 8);
6023 if (!state->get_names) {
6024 *pnum_names = num_names;
6025 return NT_STATUS_OK;
6028 if (dlength+12 > state->num_data) {
6029 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6031 names = talloc_array(mem_ctx, char *, num_names);
6032 if (names == NULL) {
6033 return NT_STATUS_NO_MEMORY;
6036 for (i=0; i<num_names; i++) {
6039 size_t converted_size;
6041 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6042 ret = convert_string_talloc(
6043 names, CH_UTF16LE, CH_UNIX,
6044 src, 2 * sizeof(SHADOW_COPY_LABEL),
6045 &names[i], &converted_size);
6048 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6051 *pnum_names = num_names;
6053 return NT_STATUS_OK;
6056 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6057 uint16_t fnum, bool get_names,
6058 char ***pnames, int *pnum_names)
6060 TALLOC_CTX *frame = talloc_stackframe();
6061 struct tevent_context *ev;
6062 struct tevent_req *req;
6063 NTSTATUS status = NT_STATUS_NO_MEMORY;
6065 if (smbXcli_conn_has_async_calls(cli->conn)) {
6067 * Can't use sync call while an async call is in flight
6069 status = NT_STATUS_INVALID_PARAMETER;
6072 ev = samba_tevent_context_init(frame);
6076 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6080 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6083 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);