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 uint16_t additional_flags2 = 0;
3747 uint8_t *bytes = NULL;
3749 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3754 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3756 bytes = talloc_array(state, uint8_t, 1);
3757 if (tevent_req_nomem(bytes, req)) {
3758 return tevent_req_post(req, ev);
3761 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3762 strlen(fname)+1, NULL);
3764 if (tevent_req_nomem(bytes, req)) {
3765 return tevent_req_post(req, ev);
3768 if (clistr_is_previous_version_path(fname)) {
3769 additional_flags2 = FLAGS2_REPARSE_PATH;
3772 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3774 0, NULL, talloc_get_size(bytes), bytes);
3775 if (tevent_req_nomem(subreq, req)) {
3776 return tevent_req_post(req, ev);
3778 tevent_req_set_callback(subreq, cli_getatr_done, req);
3782 static void cli_getatr_done(struct tevent_req *subreq)
3784 struct tevent_req *req = tevent_req_callback_data(
3785 subreq, struct tevent_req);
3786 struct cli_getatr_state *state = tevent_req_data(
3787 req, struct cli_getatr_state);
3789 uint16_t *vwv = NULL;
3792 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3794 TALLOC_FREE(subreq);
3795 if (tevent_req_nterror(req, status)) {
3799 state->attr = SVAL(vwv+0,0);
3800 state->size = (off_t)IVAL(vwv+3,0);
3801 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3803 tevent_req_done(req);
3806 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3811 struct cli_getatr_state *state = tevent_req_data(
3812 req, struct cli_getatr_state);
3815 if (tevent_req_is_nterror(req, &status)) {
3819 *attr = state->attr;
3822 *size = state->size;
3825 *write_time = state->write_time;
3827 return NT_STATUS_OK;
3830 NTSTATUS cli_getatr(struct cli_state *cli,
3836 TALLOC_CTX *frame = NULL;
3837 struct tevent_context *ev = NULL;
3838 struct tevent_req *req = NULL;
3839 NTSTATUS status = NT_STATUS_OK;
3841 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3842 return cli_smb2_getatr(cli,
3849 frame = talloc_stackframe();
3851 if (smbXcli_conn_has_async_calls(cli->conn)) {
3853 * Can't use sync call while an async call is in flight
3855 status = NT_STATUS_INVALID_PARAMETER;
3859 ev = samba_tevent_context_init(frame);
3861 status = NT_STATUS_NO_MEMORY;
3865 req = cli_getatr_send(frame, ev, cli, fname);
3867 status = NT_STATUS_NO_MEMORY;
3871 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3875 status = cli_getatr_recv(req,
3885 /****************************************************************************
3886 Do a SMBsetattrE call.
3887 ****************************************************************************/
3889 static void cli_setattrE_done(struct tevent_req *subreq);
3891 struct cli_setattrE_state {
3895 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3896 struct tevent_context *ev,
3897 struct cli_state *cli,
3903 struct tevent_req *req = NULL, *subreq = NULL;
3904 struct cli_setattrE_state *state = NULL;
3905 uint8_t additional_flags = 0;
3907 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3912 SSVAL(state->vwv+0, 0, fnum);
3913 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3914 smb1cli_conn_server_time_zone(cli->conn));
3915 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3916 smb1cli_conn_server_time_zone(cli->conn));
3917 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3918 smb1cli_conn_server_time_zone(cli->conn));
3920 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3921 7, state->vwv, 0, NULL);
3922 if (tevent_req_nomem(subreq, req)) {
3923 return tevent_req_post(req, ev);
3925 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3929 static void cli_setattrE_done(struct tevent_req *subreq)
3931 struct tevent_req *req = tevent_req_callback_data(
3932 subreq, struct tevent_req);
3935 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3936 TALLOC_FREE(subreq);
3937 if (tevent_req_nterror(req, status)) {
3940 tevent_req_done(req);
3943 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3945 return tevent_req_simple_recv_ntstatus(req);
3948 NTSTATUS cli_setattrE(struct cli_state *cli,
3954 TALLOC_CTX *frame = NULL;
3955 struct tevent_context *ev = NULL;
3956 struct tevent_req *req = NULL;
3957 NTSTATUS status = NT_STATUS_OK;
3959 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3960 return cli_smb2_setattrE(cli,
3967 frame = talloc_stackframe();
3969 if (smbXcli_conn_has_async_calls(cli->conn)) {
3971 * Can't use sync call while an async call is in flight
3973 status = NT_STATUS_INVALID_PARAMETER;
3977 ev = samba_tevent_context_init(frame);
3979 status = NT_STATUS_NO_MEMORY;
3983 req = cli_setattrE_send(frame, ev,
3991 status = NT_STATUS_NO_MEMORY;
3995 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3999 status = cli_setattrE_recv(req);
4006 /****************************************************************************
4007 Do a SMBsetatr call.
4008 ****************************************************************************/
4010 static void cli_setatr_done(struct tevent_req *subreq);
4012 struct cli_setatr_state {
4016 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4017 struct tevent_context *ev,
4018 struct cli_state *cli,
4023 struct tevent_req *req = NULL, *subreq = NULL;
4024 struct cli_setatr_state *state = NULL;
4025 uint8_t additional_flags = 0;
4026 uint16_t additional_flags2 = 0;
4027 uint8_t *bytes = NULL;
4029 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4034 SSVAL(state->vwv+0, 0, attr);
4035 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4037 bytes = talloc_array(state, uint8_t, 1);
4038 if (tevent_req_nomem(bytes, req)) {
4039 return tevent_req_post(req, ev);
4042 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4043 strlen(fname)+1, NULL);
4044 if (tevent_req_nomem(bytes, req)) {
4045 return tevent_req_post(req, ev);
4047 bytes = talloc_realloc(state, bytes, uint8_t,
4048 talloc_get_size(bytes)+1);
4049 if (tevent_req_nomem(bytes, req)) {
4050 return tevent_req_post(req, ev);
4053 bytes[talloc_get_size(bytes)-1] = 4;
4054 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4056 if (tevent_req_nomem(bytes, req)) {
4057 return tevent_req_post(req, ev);
4060 if (clistr_is_previous_version_path(fname)) {
4061 additional_flags2 = FLAGS2_REPARSE_PATH;
4064 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
4066 8, state->vwv, talloc_get_size(bytes), bytes);
4067 if (tevent_req_nomem(subreq, req)) {
4068 return tevent_req_post(req, ev);
4070 tevent_req_set_callback(subreq, cli_setatr_done, req);
4074 static void cli_setatr_done(struct tevent_req *subreq)
4076 struct tevent_req *req = tevent_req_callback_data(
4077 subreq, struct tevent_req);
4080 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4081 TALLOC_FREE(subreq);
4082 if (tevent_req_nterror(req, status)) {
4085 tevent_req_done(req);
4088 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4090 return tevent_req_simple_recv_ntstatus(req);
4093 NTSTATUS cli_setatr(struct cli_state *cli,
4098 TALLOC_CTX *frame = NULL;
4099 struct tevent_context *ev = NULL;
4100 struct tevent_req *req = NULL;
4101 NTSTATUS status = NT_STATUS_OK;
4103 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4104 return cli_smb2_setatr(cli,
4110 frame = talloc_stackframe();
4112 if (smbXcli_conn_has_async_calls(cli->conn)) {
4114 * Can't use sync call while an async call is in flight
4116 status = NT_STATUS_INVALID_PARAMETER;
4120 ev = samba_tevent_context_init(frame);
4122 status = NT_STATUS_NO_MEMORY;
4126 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4128 status = NT_STATUS_NO_MEMORY;
4132 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4136 status = cli_setatr_recv(req);
4143 /****************************************************************************
4144 Check for existance of a dir.
4145 ****************************************************************************/
4147 static void cli_chkpath_done(struct tevent_req *subreq);
4149 struct cli_chkpath_state {
4153 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4154 struct tevent_context *ev,
4155 struct cli_state *cli,
4158 struct tevent_req *req = NULL, *subreq = NULL;
4159 struct cli_chkpath_state *state = NULL;
4160 uint8_t additional_flags = 0;
4161 uint16_t additional_flags2 = 0;
4162 uint8_t *bytes = NULL;
4164 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4169 bytes = talloc_array(state, uint8_t, 1);
4170 if (tevent_req_nomem(bytes, req)) {
4171 return tevent_req_post(req, ev);
4174 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4175 strlen(fname)+1, NULL);
4177 if (tevent_req_nomem(bytes, req)) {
4178 return tevent_req_post(req, ev);
4181 if (clistr_is_previous_version_path(fname)) {
4182 additional_flags2 = FLAGS2_REPARSE_PATH;
4185 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4187 0, NULL, talloc_get_size(bytes), bytes);
4188 if (tevent_req_nomem(subreq, req)) {
4189 return tevent_req_post(req, ev);
4191 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4195 static void cli_chkpath_done(struct tevent_req *subreq)
4197 struct tevent_req *req = tevent_req_callback_data(
4198 subreq, struct tevent_req);
4201 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4202 TALLOC_FREE(subreq);
4203 if (tevent_req_nterror(req, status)) {
4206 tevent_req_done(req);
4209 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4211 return tevent_req_simple_recv_ntstatus(req);
4214 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4216 TALLOC_CTX *frame = talloc_stackframe();
4217 struct tevent_context *ev = NULL;
4218 struct tevent_req *req = NULL;
4220 NTSTATUS status = NT_STATUS_OK;
4222 if (smbXcli_conn_has_async_calls(cli->conn)) {
4224 * Can't use sync call while an async call is in flight
4226 status = NT_STATUS_INVALID_PARAMETER;
4230 path2 = talloc_strdup(frame, path);
4232 status = NT_STATUS_NO_MEMORY;
4235 trim_char(path2,'\0','\\');
4237 path2 = talloc_strdup(frame, "\\");
4239 status = NT_STATUS_NO_MEMORY;
4244 ev = samba_tevent_context_init(frame);
4246 status = NT_STATUS_NO_MEMORY;
4250 req = cli_chkpath_send(frame, ev, cli, path2);
4252 status = NT_STATUS_NO_MEMORY;
4256 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4260 status = cli_chkpath_recv(req);
4267 /****************************************************************************
4269 ****************************************************************************/
4271 static void cli_dskattr_done(struct tevent_req *subreq);
4273 struct cli_dskattr_state {
4279 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4280 struct tevent_context *ev,
4281 struct cli_state *cli)
4283 struct tevent_req *req = NULL, *subreq = NULL;
4284 struct cli_dskattr_state *state = NULL;
4285 uint8_t additional_flags = 0;
4287 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4292 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4294 if (tevent_req_nomem(subreq, req)) {
4295 return tevent_req_post(req, ev);
4297 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4301 static void cli_dskattr_done(struct tevent_req *subreq)
4303 struct tevent_req *req = tevent_req_callback_data(
4304 subreq, struct tevent_req);
4305 struct cli_dskattr_state *state = tevent_req_data(
4306 req, struct cli_dskattr_state);
4308 uint16_t *vwv = NULL;
4311 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4313 TALLOC_FREE(subreq);
4314 if (tevent_req_nterror(req, status)) {
4317 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4318 state->total = SVAL(vwv+0, 0);
4319 state->avail = SVAL(vwv+3, 0);
4320 tevent_req_done(req);
4323 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4325 struct cli_dskattr_state *state = tevent_req_data(
4326 req, struct cli_dskattr_state);
4329 if (tevent_req_is_nterror(req, &status)) {
4332 *bsize = state->bsize;
4333 *total = state->total;
4334 *avail = state->avail;
4335 return NT_STATUS_OK;
4338 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4340 TALLOC_CTX *frame = NULL;
4341 struct tevent_context *ev = NULL;
4342 struct tevent_req *req = NULL;
4343 NTSTATUS status = NT_STATUS_OK;
4345 frame = talloc_stackframe();
4347 if (smbXcli_conn_has_async_calls(cli->conn)) {
4349 * Can't use sync call while an async call is in flight
4351 status = NT_STATUS_INVALID_PARAMETER;
4355 ev = samba_tevent_context_init(frame);
4357 status = NT_STATUS_NO_MEMORY;
4361 req = cli_dskattr_send(frame, ev, cli);
4363 status = NT_STATUS_NO_MEMORY;
4367 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4371 status = cli_dskattr_recv(req, bsize, total, avail);
4378 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4379 uint64_t *total, uint64_t *avail)
4381 uint64_t sectors_per_block;
4382 uint64_t bytes_per_sector;
4383 int old_bsize, old_total, old_avail;
4386 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4387 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4391 * Try the trans2 disk full size info call first.
4392 * We already use this in SMBC_fstatvfs_ctx().
4393 * Ignore 'actual_available_units' as we only
4394 * care about the quota for the caller.
4397 status = cli_get_fs_full_size_info(cli,
4404 /* Try and cope will all varients of "we don't do this call"
4405 and fall back to cli_dskattr. */
4407 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4408 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4409 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4410 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4411 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4412 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4413 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4414 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4415 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4416 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4420 if (!NT_STATUS_IS_OK(status)) {
4425 *bsize = sectors_per_block *
4429 return NT_STATUS_OK;
4433 /* Old SMB1 core protocol fallback. */
4434 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4435 if (!NT_STATUS_IS_OK(status)) {
4439 *bsize = (uint64_t)old_bsize;
4442 *total = (uint64_t)old_total;
4445 *avail = (uint64_t)old_avail;
4447 return NT_STATUS_OK;
4450 /****************************************************************************
4451 Create and open a temporary file.
4452 ****************************************************************************/
4454 static void cli_ctemp_done(struct tevent_req *subreq);
4456 struct ctemp_state {
4462 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4463 struct tevent_context *ev,
4464 struct cli_state *cli,
4467 struct tevent_req *req = NULL, *subreq = NULL;
4468 struct ctemp_state *state = NULL;
4469 uint8_t additional_flags = 0;
4470 uint16_t additional_flags2 = 0;
4471 uint8_t *bytes = NULL;
4473 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4478 SSVAL(state->vwv,0,0);
4479 SIVALS(state->vwv+1,0,-1);
4481 bytes = talloc_array(state, uint8_t, 1);
4482 if (tevent_req_nomem(bytes, req)) {
4483 return tevent_req_post(req, ev);
4486 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4487 strlen(path)+1, NULL);
4488 if (tevent_req_nomem(bytes, req)) {
4489 return tevent_req_post(req, ev);
4492 if (clistr_is_previous_version_path(path)) {
4493 additional_flags2 = FLAGS2_REPARSE_PATH;
4496 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4498 3, state->vwv, talloc_get_size(bytes), bytes);
4499 if (tevent_req_nomem(subreq, req)) {
4500 return tevent_req_post(req, ev);
4502 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4506 static void cli_ctemp_done(struct tevent_req *subreq)
4508 struct tevent_req *req = tevent_req_callback_data(
4509 subreq, struct tevent_req);
4510 struct ctemp_state *state = tevent_req_data(
4511 req, struct ctemp_state);
4515 uint32_t num_bytes = 0;
4516 uint8_t *bytes = NULL;
4518 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4519 &num_bytes, &bytes);
4520 TALLOC_FREE(subreq);
4521 if (tevent_req_nterror(req, status)) {
4525 state->fnum = SVAL(vwv+0, 0);
4527 /* From W2K3, the result is just the ASCII name */
4528 if (num_bytes < 2) {
4529 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4533 if (pull_string_talloc(state,
4540 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4543 tevent_req_done(req);
4546 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4551 struct ctemp_state *state = tevent_req_data(req,
4552 struct ctemp_state);
4555 if (tevent_req_is_nterror(req, &status)) {
4558 *pfnum = state->fnum;
4559 *outfile = talloc_strdup(ctx, state->ret_path);
4561 return NT_STATUS_NO_MEMORY;
4563 return NT_STATUS_OK;
4566 NTSTATUS cli_ctemp(struct cli_state *cli,
4572 TALLOC_CTX *frame = talloc_stackframe();
4573 struct tevent_context *ev;
4574 struct tevent_req *req;
4575 NTSTATUS status = NT_STATUS_OK;
4577 if (smbXcli_conn_has_async_calls(cli->conn)) {
4579 * Can't use sync call while an async call is in flight
4581 status = NT_STATUS_INVALID_PARAMETER;
4585 ev = samba_tevent_context_init(frame);
4587 status = NT_STATUS_NO_MEMORY;
4591 req = cli_ctemp_send(frame, ev, cli, path);
4593 status = NT_STATUS_NO_MEMORY;
4597 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4601 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4609 send a raw ioctl - used by the torture code
4611 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4616 SSVAL(vwv+0, 0, fnum);
4617 SSVAL(vwv+1, 0, code>>16);
4618 SSVAL(vwv+2, 0, (code&0xFFFF));
4620 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4621 NULL, 0, NULL, NULL, NULL, NULL);
4622 if (!NT_STATUS_IS_OK(status)) {
4625 *blob = data_blob_null;
4626 return NT_STATUS_OK;
4629 /*********************************************************
4630 Set an extended attribute utility fn.
4631 *********************************************************/
4633 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4634 uint8_t *param, unsigned int param_len,
4635 const char *ea_name,
4636 const char *ea_val, size_t ea_len)
4639 unsigned int data_len = 0;
4640 uint8_t *data = NULL;
4642 size_t ea_namelen = strlen(ea_name);
4645 SSVAL(setup, 0, setup_val);
4647 if (ea_namelen == 0 && ea_len == 0) {
4649 data = talloc_array(talloc_tos(),
4653 return NT_STATUS_NO_MEMORY;
4656 SIVAL(p,0,data_len);
4658 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4659 data = talloc_array(talloc_tos(),
4663 return NT_STATUS_NO_MEMORY;
4666 SIVAL(p,0,data_len);
4668 SCVAL(p, 0, 0); /* EA flags. */
4669 SCVAL(p, 1, ea_namelen);
4670 SSVAL(p, 2, ea_len);
4671 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4672 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4676 * FIXME - if we want to do previous version path
4677 * processing on an EA set call we need to turn this
4678 * into calls to cli_trans_send()/cli_trans_recv()
4679 * with a temporary event context, as cli_trans_send()
4680 * have access to the additional_flags2 needed to
4681 * send @GMT- paths. JRA.
4684 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4686 param, param_len, 2,
4689 NULL, 0, NULL, /* rsetup */
4690 NULL, 0, NULL, /* rparam */
4691 NULL, 0, NULL); /* rdata */
4696 /*********************************************************
4697 Set an extended attribute on a pathname.
4698 *********************************************************/
4700 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4701 const char *ea_name, const char *ea_val,
4704 unsigned int param_len = 0;
4707 TALLOC_CTX *frame = NULL;
4709 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4710 return cli_smb2_set_ea_path(cli,
4717 frame = talloc_stackframe();
4719 param = talloc_array(frame, uint8_t, 6);
4721 status = NT_STATUS_NO_MEMORY;
4724 SSVAL(param,0,SMB_INFO_SET_EA);
4728 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4729 path, strlen(path)+1,
4731 param_len = talloc_get_size(param);
4733 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4734 ea_name, ea_val, ea_len);
4742 /*********************************************************
4743 Set an extended attribute on an fnum.
4744 *********************************************************/
4746 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4747 const char *ea_name, const char *ea_val,
4752 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4753 return cli_smb2_set_ea_fnum(cli,
4760 memset(param, 0, 6);
4761 SSVAL(param,0,fnum);
4762 SSVAL(param,2,SMB_INFO_SET_EA);
4764 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4765 ea_name, ea_val, ea_len);
4768 /*********************************************************
4769 Get an extended attribute list utility fn.
4770 *********************************************************/
4772 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4774 size_t *pnum_eas, struct ea_struct **pea_list)
4776 struct ea_struct *ea_list = NULL;
4781 if (rdata_len < 4) {
4785 ea_size = (size_t)IVAL(rdata,0);
4786 if (ea_size > rdata_len) {
4791 /* No EA's present. */
4800 /* Validate the EA list and count it. */
4801 for (num_eas = 0; ea_size >= 4; num_eas++) {
4802 unsigned int ea_namelen = CVAL(p,1);
4803 unsigned int ea_valuelen = SVAL(p,2);
4804 if (ea_namelen == 0) {
4807 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4810 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4811 p += 4 + ea_namelen + 1 + ea_valuelen;
4820 *pnum_eas = num_eas;
4822 /* Caller only wants number of EA's. */
4826 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4831 ea_size = (size_t)IVAL(rdata,0);
4834 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4835 struct ea_struct *ea = &ea_list[num_eas];
4836 fstring unix_ea_name;
4837 unsigned int ea_namelen = CVAL(p,1);
4838 unsigned int ea_valuelen = SVAL(p,2);
4840 ea->flags = CVAL(p,0);
4841 unix_ea_name[0] = '\0';
4842 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4843 ea->name = talloc_strdup(ea_list, unix_ea_name);
4847 /* Ensure the value is null terminated (in case it's a string). */
4848 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4849 if (!ea->value.data) {
4853 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4855 ea->value.data[ea_valuelen] = 0;
4857 p += 4 + ea_namelen + 1 + ea_valuelen;
4860 *pea_list = ea_list;
4864 TALLOC_FREE(ea_list);
4868 /*********************************************************
4869 Get an extended attribute list from a pathname.
4870 *********************************************************/
4872 struct cli_get_ea_list_path_state {
4877 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4879 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4880 struct tevent_context *ev,
4881 struct cli_state *cli,
4884 struct tevent_req *req, *subreq;
4885 struct cli_get_ea_list_path_state *state;
4887 req = tevent_req_create(mem_ctx, &state,
4888 struct cli_get_ea_list_path_state);
4892 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4893 SMB_INFO_QUERY_ALL_EAS, 4,
4895 if (tevent_req_nomem(subreq, req)) {
4896 return tevent_req_post(req, ev);
4898 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4902 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4904 struct tevent_req *req = tevent_req_callback_data(
4905 subreq, struct tevent_req);
4906 struct cli_get_ea_list_path_state *state = tevent_req_data(
4907 req, struct cli_get_ea_list_path_state);
4910 status = cli_qpathinfo_recv(subreq, state, &state->data,
4912 TALLOC_FREE(subreq);
4913 if (tevent_req_nterror(req, status)) {
4916 tevent_req_done(req);
4919 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4920 size_t *pnum_eas, struct ea_struct **peas)
4922 struct cli_get_ea_list_path_state *state = tevent_req_data(
4923 req, struct cli_get_ea_list_path_state);
4926 if (tevent_req_is_nterror(req, &status)) {
4929 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4931 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4933 return NT_STATUS_OK;
4936 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4939 struct ea_struct **pea_list)
4941 TALLOC_CTX *frame = NULL;
4942 struct tevent_context *ev = NULL;
4943 struct tevent_req *req = NULL;
4944 NTSTATUS status = NT_STATUS_NO_MEMORY;
4946 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4947 return cli_smb2_get_ea_list_path(cli,
4954 frame = talloc_stackframe();
4956 if (smbXcli_conn_has_async_calls(cli->conn)) {
4958 * Can't use sync call while an async call is in flight
4960 status = NT_STATUS_INVALID_PARAMETER;
4963 ev = samba_tevent_context_init(frame);
4967 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4971 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4974 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4980 /****************************************************************************
4981 Convert open "flags" arg to uint32_t on wire.
4982 ****************************************************************************/
4984 static uint32_t open_flags_to_wire(int flags)
4986 int open_mode = flags & O_ACCMODE;
4989 switch (open_mode) {
4991 ret |= SMB_O_WRONLY;
4998 ret |= SMB_O_RDONLY;
5002 if (flags & O_CREAT) {
5005 if (flags & O_EXCL) {
5008 if (flags & O_TRUNC) {
5012 if (flags & O_SYNC) {
5016 if (flags & O_APPEND) {
5017 ret |= SMB_O_APPEND;
5019 #if defined(O_DIRECT)
5020 if (flags & O_DIRECT) {
5021 ret |= SMB_O_DIRECT;
5024 #if defined(O_DIRECTORY)
5025 if (flags & O_DIRECTORY) {
5026 ret |= SMB_O_DIRECTORY;
5032 /****************************************************************************
5033 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5034 ****************************************************************************/
5036 struct posix_open_state {
5040 uint16_t fnum; /* Out */
5043 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5045 struct tevent_req *req = tevent_req_callback_data(
5046 subreq, struct tevent_req);
5047 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5052 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5053 NULL, 0, NULL, &data, 12, &num_data);
5054 TALLOC_FREE(subreq);
5055 if (tevent_req_nterror(req, status)) {
5058 state->fnum = SVAL(data,2);
5059 tevent_req_done(req);
5062 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5063 struct tevent_context *ev,
5064 struct cli_state *cli,
5070 struct tevent_req *req = NULL, *subreq = NULL;
5071 struct posix_open_state *state = NULL;
5072 uint32_t wire_flags = open_flags_to_wire(flags);
5074 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5079 /* Setup setup word. */
5080 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5082 /* Setup param array. */
5083 state->param = talloc_array(state, uint8_t, 6);
5084 if (tevent_req_nomem(state->param, req)) {
5085 return tevent_req_post(req, ev);
5087 memset(state->param, '\0', 6);
5088 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5090 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5091 strlen(fname)+1, NULL);
5093 if (tevent_req_nomem(state->param, req)) {
5094 return tevent_req_post(req, ev);
5097 /* Setup data words. */
5099 wire_flags |= SMB_O_DIRECTORY;
5102 SIVAL(state->data,0,0); /* No oplock. */
5103 SIVAL(state->data,4,wire_flags);
5104 SIVAL(state->data,8,unix_perms_to_wire(mode));
5105 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5106 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5108 subreq = cli_trans_send(state, /* mem ctx. */
5109 ev, /* event ctx. */
5110 cli, /* cli_state. */
5111 0, /* additional_flags2 */
5112 SMBtrans2, /* cmd. */
5113 NULL, /* pipe name. */
5117 &state->setup, /* setup. */
5118 1, /* num setup uint16_t words. */
5119 0, /* max returned setup. */
5120 state->param, /* param. */
5121 talloc_get_size(state->param),/* num param. */
5122 2, /* max returned param. */
5123 state->data, /* data. */
5125 12); /* max returned data. */
5127 if (tevent_req_nomem(subreq, req)) {
5128 return tevent_req_post(req, ev);
5130 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5134 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5135 struct tevent_context *ev,
5136 struct cli_state *cli,
5141 return cli_posix_open_internal_send(mem_ctx, ev,
5142 cli, fname, flags, mode, false);
5145 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5147 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5150 if (tevent_req_is_nterror(req, &status)) {
5153 *pfnum = state->fnum;
5154 return NT_STATUS_OK;
5157 /****************************************************************************
5158 Open - POSIX semantics. Doesn't request oplock.
5159 ****************************************************************************/
5161 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5162 int flags, mode_t mode, uint16_t *pfnum)
5165 TALLOC_CTX *frame = talloc_stackframe();
5166 struct tevent_context *ev = NULL;
5167 struct tevent_req *req = NULL;
5168 NTSTATUS status = NT_STATUS_OK;
5170 if (smbXcli_conn_has_async_calls(cli->conn)) {
5172 * Can't use sync call while an async call is in flight
5174 status = NT_STATUS_INVALID_PARAMETER;
5178 ev = samba_tevent_context_init(frame);
5180 status = NT_STATUS_NO_MEMORY;
5184 req = cli_posix_open_send(frame,
5191 status = NT_STATUS_NO_MEMORY;
5195 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5199 status = cli_posix_open_recv(req, pfnum);
5206 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5207 struct tevent_context *ev,
5208 struct cli_state *cli,
5212 return cli_posix_open_internal_send(mem_ctx, ev,
5213 cli, fname, O_CREAT, mode, true);
5216 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5218 return tevent_req_simple_recv_ntstatus(req);
5221 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5223 TALLOC_CTX *frame = talloc_stackframe();
5224 struct tevent_context *ev = NULL;
5225 struct tevent_req *req = NULL;
5226 NTSTATUS status = NT_STATUS_OK;
5228 if (smbXcli_conn_has_async_calls(cli->conn)) {
5230 * Can't use sync call while an async call is in flight
5232 status = NT_STATUS_INVALID_PARAMETER;
5236 ev = samba_tevent_context_init(frame);
5238 status = NT_STATUS_NO_MEMORY;
5242 req = cli_posix_mkdir_send(frame,
5248 status = NT_STATUS_NO_MEMORY;
5252 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5256 status = cli_posix_mkdir_recv(req);
5263 /****************************************************************************
5264 unlink or rmdir - POSIX semantics.
5265 ****************************************************************************/
5267 struct cli_posix_unlink_internal_state {
5271 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5273 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5274 struct tevent_context *ev,
5275 struct cli_state *cli,
5279 struct tevent_req *req = NULL, *subreq = NULL;
5280 struct cli_posix_unlink_internal_state *state = NULL;
5282 req = tevent_req_create(mem_ctx, &state,
5283 struct cli_posix_unlink_internal_state);
5288 /* Setup data word. */
5289 SSVAL(state->data, 0, level);
5291 subreq = cli_setpathinfo_send(state, ev, cli,
5292 SMB_POSIX_PATH_UNLINK,
5294 state->data, sizeof(state->data));
5295 if (tevent_req_nomem(subreq, req)) {
5296 return tevent_req_post(req, ev);
5298 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5302 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5304 NTSTATUS status = cli_setpathinfo_recv(subreq);
5305 tevent_req_simple_finish_ntstatus(subreq, status);
5308 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5309 struct tevent_context *ev,
5310 struct cli_state *cli,
5313 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5314 SMB_POSIX_UNLINK_FILE_TARGET);
5317 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5319 return tevent_req_simple_recv_ntstatus(req);
5322 /****************************************************************************
5323 unlink - POSIX semantics.
5324 ****************************************************************************/
5326 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5328 TALLOC_CTX *frame = talloc_stackframe();
5329 struct tevent_context *ev = NULL;
5330 struct tevent_req *req = NULL;
5331 NTSTATUS status = NT_STATUS_OK;
5333 if (smbXcli_conn_has_async_calls(cli->conn)) {
5335 * Can't use sync call while an async call is in flight
5337 status = NT_STATUS_INVALID_PARAMETER;
5341 ev = samba_tevent_context_init(frame);
5343 status = NT_STATUS_NO_MEMORY;
5347 req = cli_posix_unlink_send(frame,
5352 status = NT_STATUS_NO_MEMORY;
5356 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5360 status = cli_posix_unlink_recv(req);
5367 /****************************************************************************
5368 rmdir - POSIX semantics.
5369 ****************************************************************************/
5371 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5372 struct tevent_context *ev,
5373 struct cli_state *cli,
5376 return cli_posix_unlink_internal_send(
5377 mem_ctx, ev, cli, fname,
5378 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5381 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5383 return tevent_req_simple_recv_ntstatus(req);
5386 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5388 TALLOC_CTX *frame = talloc_stackframe();
5389 struct tevent_context *ev = NULL;
5390 struct tevent_req *req = NULL;
5391 NTSTATUS status = NT_STATUS_OK;
5393 if (smbXcli_conn_has_async_calls(cli->conn)) {
5395 * Can't use sync call while an async call is in flight
5397 status = NT_STATUS_INVALID_PARAMETER;
5401 ev = samba_tevent_context_init(frame);
5403 status = NT_STATUS_NO_MEMORY;
5407 req = cli_posix_rmdir_send(frame,
5412 status = NT_STATUS_NO_MEMORY;
5416 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5420 status = cli_posix_rmdir_recv(req, frame);
5427 /****************************************************************************
5429 ****************************************************************************/
5431 struct cli_notify_state {
5433 uint32_t num_changes;
5434 struct notify_change *changes;
5437 static void cli_notify_done(struct tevent_req *subreq);
5439 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5440 struct tevent_context *ev,
5441 struct cli_state *cli, uint16_t fnum,
5442 uint32_t buffer_size,
5443 uint32_t completion_filter, bool recursive)
5445 struct tevent_req *req, *subreq;
5446 struct cli_notify_state *state;
5447 unsigned old_timeout;
5449 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5454 SIVAL(state->setup, 0, completion_filter);
5455 SSVAL(state->setup, 4, fnum);
5456 SSVAL(state->setup, 6, recursive);
5459 * Notifies should not time out
5461 old_timeout = cli_set_timeout(cli, 0);
5463 subreq = cli_trans_send(
5464 state, /* mem ctx. */
5465 ev, /* event ctx. */
5466 cli, /* cli_state. */
5467 0, /* additional_flags2 */
5468 SMBnttrans, /* cmd. */
5469 NULL, /* pipe name. */
5471 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5473 (uint16_t *)state->setup, /* setup. */
5474 4, /* num setup uint16_t words. */
5475 0, /* max returned setup. */
5478 buffer_size, /* max returned param. */
5481 0); /* max returned data. */
5483 cli_set_timeout(cli, old_timeout);
5485 if (tevent_req_nomem(subreq, req)) {
5486 return tevent_req_post(req, ev);
5488 tevent_req_set_callback(subreq, cli_notify_done, req);
5492 static void cli_notify_done(struct tevent_req *subreq)
5494 struct tevent_req *req = tevent_req_callback_data(
5495 subreq, struct tevent_req);
5496 struct cli_notify_state *state = tevent_req_data(
5497 req, struct cli_notify_state);
5500 uint32_t i, ofs, num_params;
5503 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5504 ¶ms, 0, &num_params, NULL, 0, NULL);
5505 TALLOC_FREE(subreq);
5506 if (tevent_req_nterror(req, status)) {
5507 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5511 state->num_changes = 0;
5514 while (num_params - ofs > 12) {
5515 uint32_t next = IVAL(params, ofs);
5516 state->num_changes += 1;
5518 if ((next == 0) || (ofs+next >= num_params)) {
5524 state->changes = talloc_array(state, struct notify_change,
5525 state->num_changes);
5526 if (tevent_req_nomem(state->changes, req)) {
5527 TALLOC_FREE(params);
5533 for (i=0; i<state->num_changes; i++) {
5534 uint32_t next = IVAL(params, ofs);
5535 uint32_t len = IVAL(params, ofs+8);
5539 if (trans_oob(num_params, ofs + 12, len)) {
5540 TALLOC_FREE(params);
5542 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5546 state->changes[i].action = IVAL(params, ofs+4);
5547 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5548 &name, params+ofs+12, len,
5549 STR_TERMINATE|STR_UNICODE);
5551 TALLOC_FREE(params);
5552 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5555 state->changes[i].name = name;
5559 TALLOC_FREE(params);
5560 tevent_req_done(req);
5563 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5564 uint32_t *pnum_changes,
5565 struct notify_change **pchanges)
5567 struct cli_notify_state *state = tevent_req_data(
5568 req, struct cli_notify_state);
5571 if (tevent_req_is_nterror(req, &status)) {
5575 *pnum_changes = state->num_changes;
5576 *pchanges = talloc_move(mem_ctx, &state->changes);
5577 return NT_STATUS_OK;
5580 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5581 uint32_t completion_filter, bool recursive,
5582 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5583 struct notify_change **pchanges)
5585 TALLOC_CTX *frame = talloc_stackframe();
5586 struct tevent_context *ev;
5587 struct tevent_req *req;
5588 NTSTATUS status = NT_STATUS_NO_MEMORY;
5590 if (smbXcli_conn_has_async_calls(cli->conn)) {
5592 * Can't use sync call while an async call is in flight
5594 status = NT_STATUS_INVALID_PARAMETER;
5597 ev = samba_tevent_context_init(frame);
5601 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5602 completion_filter, recursive);
5606 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5609 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5615 struct cli_qpathinfo_state {
5624 static void cli_qpathinfo_done(struct tevent_req *subreq);
5626 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5627 struct tevent_context *ev,
5628 struct cli_state *cli, const char *fname,
5629 uint16_t level, uint32_t min_rdata,
5632 struct tevent_req *req, *subreq;
5633 struct cli_qpathinfo_state *state;
5634 uint16_t additional_flags2 = 0;
5636 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5640 state->min_rdata = min_rdata;
5641 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5643 state->param = talloc_zero_array(state, uint8_t, 6);
5644 if (tevent_req_nomem(state->param, req)) {
5645 return tevent_req_post(req, ev);
5647 SSVAL(state->param, 0, level);
5648 state->param = trans2_bytes_push_str(
5649 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5650 if (tevent_req_nomem(state->param, req)) {
5651 return tevent_req_post(req, ev);
5654 if (clistr_is_previous_version_path(fname) &&
5655 !INFO_LEVEL_IS_UNIX(level)) {
5656 additional_flags2 = FLAGS2_REPARSE_PATH;
5659 subreq = cli_trans_send(
5660 state, /* mem ctx. */
5661 ev, /* event ctx. */
5662 cli, /* cli_state. */
5663 additional_flags2, /* additional_flags2 */
5664 SMBtrans2, /* cmd. */
5665 NULL, /* pipe name. */
5669 state->setup, /* setup. */
5670 1, /* num setup uint16_t words. */
5671 0, /* max returned setup. */
5672 state->param, /* param. */
5673 talloc_get_size(state->param), /* num param. */
5674 2, /* max returned param. */
5677 max_rdata); /* max returned data. */
5679 if (tevent_req_nomem(subreq, req)) {
5680 return tevent_req_post(req, ev);
5682 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5686 static void cli_qpathinfo_done(struct tevent_req *subreq)
5688 struct tevent_req *req = tevent_req_callback_data(
5689 subreq, struct tevent_req);
5690 struct cli_qpathinfo_state *state = tevent_req_data(
5691 req, struct cli_qpathinfo_state);
5694 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5696 &state->rdata, state->min_rdata,
5698 if (tevent_req_nterror(req, status)) {
5701 tevent_req_done(req);
5704 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5705 uint8_t **rdata, uint32_t *num_rdata)
5707 struct cli_qpathinfo_state *state = tevent_req_data(
5708 req, struct cli_qpathinfo_state);
5711 if (tevent_req_is_nterror(req, &status)) {
5714 if (rdata != NULL) {
5715 *rdata = talloc_move(mem_ctx, &state->rdata);
5717 TALLOC_FREE(state->rdata);
5719 if (num_rdata != NULL) {
5720 *num_rdata = state->num_rdata;
5722 return NT_STATUS_OK;
5725 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5726 const char *fname, uint16_t level, uint32_t min_rdata,
5728 uint8_t **rdata, uint32_t *num_rdata)
5730 TALLOC_CTX *frame = talloc_stackframe();
5731 struct tevent_context *ev;
5732 struct tevent_req *req;
5733 NTSTATUS status = NT_STATUS_NO_MEMORY;
5735 if (smbXcli_conn_has_async_calls(cli->conn)) {
5737 * Can't use sync call while an async call is in flight
5739 status = NT_STATUS_INVALID_PARAMETER;
5742 ev = samba_tevent_context_init(frame);
5746 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5751 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5754 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5760 struct cli_qfileinfo_state {
5764 uint16_t recv_flags2;
5770 static void cli_qfileinfo_done(struct tevent_req *subreq);
5772 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5773 struct tevent_context *ev,
5774 struct cli_state *cli, uint16_t fnum,
5775 uint16_t level, uint32_t min_rdata,
5778 struct tevent_req *req, *subreq;
5779 struct cli_qfileinfo_state *state;
5781 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5785 state->min_rdata = min_rdata;
5786 SSVAL(state->param, 0, fnum);
5787 SSVAL(state->param, 2, level);
5788 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5790 subreq = cli_trans_send(
5791 state, /* mem ctx. */
5792 ev, /* event ctx. */
5793 cli, /* cli_state. */
5794 0, /* additional_flags2 */
5795 SMBtrans2, /* cmd. */
5796 NULL, /* pipe name. */
5800 state->setup, /* setup. */
5801 1, /* num setup uint16_t words. */
5802 0, /* max returned setup. */
5803 state->param, /* param. */
5804 sizeof(state->param), /* num param. */
5805 2, /* max returned param. */
5808 max_rdata); /* max returned data. */
5810 if (tevent_req_nomem(subreq, req)) {
5811 return tevent_req_post(req, ev);
5813 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5817 static void cli_qfileinfo_done(struct tevent_req *subreq)
5819 struct tevent_req *req = tevent_req_callback_data(
5820 subreq, struct tevent_req);
5821 struct cli_qfileinfo_state *state = tevent_req_data(
5822 req, struct cli_qfileinfo_state);
5825 status = cli_trans_recv(subreq, state,
5826 &state->recv_flags2,
5829 &state->rdata, state->min_rdata,
5831 if (tevent_req_nterror(req, status)) {
5834 tevent_req_done(req);
5837 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5838 uint16_t *recv_flags2,
5839 uint8_t **rdata, uint32_t *num_rdata)
5841 struct cli_qfileinfo_state *state = tevent_req_data(
5842 req, struct cli_qfileinfo_state);
5845 if (tevent_req_is_nterror(req, &status)) {
5849 if (recv_flags2 != NULL) {
5850 *recv_flags2 = state->recv_flags2;
5852 if (rdata != NULL) {
5853 *rdata = talloc_move(mem_ctx, &state->rdata);
5855 TALLOC_FREE(state->rdata);
5857 if (num_rdata != NULL) {
5858 *num_rdata = state->num_rdata;
5860 return NT_STATUS_OK;
5863 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5864 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5865 uint32_t max_rdata, uint16_t *recv_flags2,
5866 uint8_t **rdata, uint32_t *num_rdata)
5868 TALLOC_CTX *frame = talloc_stackframe();
5869 struct tevent_context *ev;
5870 struct tevent_req *req;
5871 NTSTATUS status = NT_STATUS_NO_MEMORY;
5873 if (smbXcli_conn_has_async_calls(cli->conn)) {
5875 * Can't use sync call while an async call is in flight
5877 status = NT_STATUS_INVALID_PARAMETER;
5880 ev = samba_tevent_context_init(frame);
5884 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5889 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5892 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5898 struct cli_flush_state {
5902 static void cli_flush_done(struct tevent_req *subreq);
5904 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5905 struct tevent_context *ev,
5906 struct cli_state *cli,
5909 struct tevent_req *req, *subreq;
5910 struct cli_flush_state *state;
5912 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5916 SSVAL(state->vwv + 0, 0, fnum);
5918 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5920 if (tevent_req_nomem(subreq, req)) {
5921 return tevent_req_post(req, ev);
5923 tevent_req_set_callback(subreq, cli_flush_done, req);
5927 static void cli_flush_done(struct tevent_req *subreq)
5929 struct tevent_req *req = tevent_req_callback_data(
5930 subreq, struct tevent_req);
5933 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5934 TALLOC_FREE(subreq);
5935 if (tevent_req_nterror(req, status)) {
5938 tevent_req_done(req);
5941 NTSTATUS cli_flush_recv(struct tevent_req *req)
5943 return tevent_req_simple_recv_ntstatus(req);
5946 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5948 TALLOC_CTX *frame = talloc_stackframe();
5949 struct tevent_context *ev;
5950 struct tevent_req *req;
5951 NTSTATUS status = NT_STATUS_NO_MEMORY;
5953 if (smbXcli_conn_has_async_calls(cli->conn)) {
5955 * Can't use sync call while an async call is in flight
5957 status = NT_STATUS_INVALID_PARAMETER;
5960 ev = samba_tevent_context_init(frame);
5964 req = cli_flush_send(frame, ev, cli, fnum);
5968 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5971 status = cli_flush_recv(req);
5977 struct cli_shadow_copy_data_state {
5984 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5986 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5987 struct tevent_context *ev,
5988 struct cli_state *cli,
5992 struct tevent_req *req, *subreq;
5993 struct cli_shadow_copy_data_state *state;
5996 req = tevent_req_create(mem_ctx, &state,
5997 struct cli_shadow_copy_data_state);
6001 state->get_names = get_names;
6002 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
6004 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
6005 SSVAL(state->setup + 2, 0, fnum);
6006 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
6007 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
6009 subreq = cli_trans_send(
6010 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
6011 state->setup, ARRAY_SIZE(state->setup), 0,
6014 if (tevent_req_nomem(subreq, req)) {
6015 return tevent_req_post(req, ev);
6017 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
6021 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
6023 struct tevent_req *req = tevent_req_callback_data(
6024 subreq, struct tevent_req);
6025 struct cli_shadow_copy_data_state *state = tevent_req_data(
6026 req, struct cli_shadow_copy_data_state);
6029 status = cli_trans_recv(subreq, state, NULL,
6030 NULL, 0, NULL, /* setup */
6031 NULL, 0, NULL, /* param */
6032 &state->data, 12, &state->num_data);
6033 TALLOC_FREE(subreq);
6034 if (tevent_req_nterror(req, status)) {
6037 tevent_req_done(req);
6040 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6041 char ***pnames, int *pnum_names)
6043 struct cli_shadow_copy_data_state *state = tevent_req_data(
6044 req, struct cli_shadow_copy_data_state);
6050 if (tevent_req_is_nterror(req, &status)) {
6053 num_names = IVAL(state->data, 4);
6054 dlength = IVAL(state->data, 8);
6056 if (!state->get_names) {
6057 *pnum_names = num_names;
6058 return NT_STATUS_OK;
6061 if (dlength+12 > state->num_data) {
6062 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6064 names = talloc_array(mem_ctx, char *, num_names);
6065 if (names == NULL) {
6066 return NT_STATUS_NO_MEMORY;
6069 for (i=0; i<num_names; i++) {
6072 size_t converted_size;
6074 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6075 ret = convert_string_talloc(
6076 names, CH_UTF16LE, CH_UNIX,
6077 src, 2 * sizeof(SHADOW_COPY_LABEL),
6078 &names[i], &converted_size);
6081 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6084 *pnum_names = num_names;
6086 return NT_STATUS_OK;
6089 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6090 uint16_t fnum, bool get_names,
6091 char ***pnames, int *pnum_names)
6093 TALLOC_CTX *frame = talloc_stackframe();
6094 struct tevent_context *ev;
6095 struct tevent_req *req;
6096 NTSTATUS status = NT_STATUS_NO_MEMORY;
6098 if (smbXcli_conn_has_async_calls(cli->conn)) {
6100 * Can't use sync call while an async call is in flight
6102 status = NT_STATUS_INVALID_PARAMETER;
6105 ev = samba_tevent_context_init(frame);
6109 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6113 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6116 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);