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;
1947 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1954 SCVAL(vwv+0, 0, 0xFF);
1959 if (cli->use_oplocks) {
1960 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1962 SIVAL(vwv+3, 1, CreatFlags);
1963 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1964 SIVAL(vwv+7, 1, DesiredAccess);
1965 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1966 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1967 SIVAL(vwv+13, 1, FileAttributes);
1968 SIVAL(vwv+15, 1, ShareAccess);
1969 SIVAL(vwv+17, 1, CreateDisposition);
1970 SIVAL(vwv+19, 1, CreateOptions |
1971 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1972 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1973 SCVAL(vwv+23, 1, SecurityFlags);
1975 bytes = talloc_array(state, uint8_t, 0);
1976 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1977 fname, strlen(fname)+1,
1980 /* sigh. this copes with broken netapp filer behaviour */
1981 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1983 if (tevent_req_nomem(bytes, req)) {
1984 return tevent_req_post(req, ev);
1987 SSVAL(vwv+2, 1, converted_len);
1989 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 0, 24, vwv,
1990 talloc_get_size(bytes), bytes);
1991 if (tevent_req_nomem(subreq, req)) {
1992 return tevent_req_post(req, ev);
1994 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1996 state->subreq = subreq;
1997 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
2002 static void cli_ntcreate1_done(struct tevent_req *subreq)
2004 struct tevent_req *req = tevent_req_callback_data(
2005 subreq, struct tevent_req);
2006 struct cli_ntcreate1_state *state = tevent_req_data(
2007 req, struct cli_ntcreate1_state);
2014 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2015 &num_bytes, &bytes);
2016 TALLOC_FREE(subreq);
2017 if (tevent_req_nterror(req, status)) {
2020 state->cr.oplock_level = CVAL(vwv+2, 0);
2021 state->fnum = SVAL(vwv+2, 1);
2022 state->cr.create_action = IVAL(vwv+3, 1);
2023 state->cr.creation_time = BVAL(vwv+5, 1);
2024 state->cr.last_access_time = BVAL(vwv+9, 1);
2025 state->cr.last_write_time = BVAL(vwv+13, 1);
2026 state->cr.change_time = BVAL(vwv+17, 1);
2027 state->cr.file_attributes = IVAL(vwv+21, 1);
2028 state->cr.allocation_size = BVAL(vwv+23, 1);
2029 state->cr.end_of_file = BVAL(vwv+27, 1);
2031 tevent_req_done(req);
2034 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2036 struct cli_ntcreate1_state *state = tevent_req_data(
2037 req, struct cli_ntcreate1_state);
2038 return tevent_req_cancel(state->subreq);
2041 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2043 struct smb_create_returns *cr)
2045 struct cli_ntcreate1_state *state = tevent_req_data(
2046 req, struct cli_ntcreate1_state);
2049 if (tevent_req_is_nterror(req, &status)) {
2052 *pfnum = state->fnum;
2056 return NT_STATUS_OK;
2059 struct cli_ntcreate_state {
2060 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2061 struct smb_create_returns *cr);
2062 struct smb_create_returns cr;
2064 struct tevent_req *subreq;
2067 static void cli_ntcreate_done(struct tevent_req *subreq);
2068 static bool cli_ntcreate_cancel(struct tevent_req *req);
2070 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2071 struct tevent_context *ev,
2072 struct cli_state *cli,
2074 uint32_t create_flags,
2075 uint32_t desired_access,
2076 uint32_t file_attributes,
2077 uint32_t share_access,
2078 uint32_t create_disposition,
2079 uint32_t create_options,
2080 uint8_t security_flags)
2082 struct tevent_req *req, *subreq;
2083 struct cli_ntcreate_state *state;
2085 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2090 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2091 state->recv = cli_smb2_create_fnum_recv;
2093 if (cli->use_oplocks) {
2094 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2097 subreq = cli_smb2_create_fnum_send(
2098 state, ev, cli, fname, create_flags, desired_access,
2099 file_attributes, share_access, create_disposition,
2102 state->recv = cli_ntcreate1_recv;
2103 subreq = cli_ntcreate1_send(
2104 state, ev, cli, fname, create_flags, desired_access,
2105 file_attributes, share_access, create_disposition,
2106 create_options, security_flags);
2108 if (tevent_req_nomem(subreq, req)) {
2109 return tevent_req_post(req, ev);
2111 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2113 state->subreq = subreq;
2114 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2119 static void cli_ntcreate_done(struct tevent_req *subreq)
2121 struct tevent_req *req = tevent_req_callback_data(
2122 subreq, struct tevent_req);
2123 struct cli_ntcreate_state *state = tevent_req_data(
2124 req, struct cli_ntcreate_state);
2127 status = state->recv(subreq, &state->fnum, &state->cr);
2128 TALLOC_FREE(subreq);
2129 if (tevent_req_nterror(req, status)) {
2132 tevent_req_done(req);
2135 static bool cli_ntcreate_cancel(struct tevent_req *req)
2137 struct cli_ntcreate_state *state = tevent_req_data(
2138 req, struct cli_ntcreate_state);
2139 return tevent_req_cancel(state->subreq);
2142 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2143 struct smb_create_returns *cr)
2145 struct cli_ntcreate_state *state = tevent_req_data(
2146 req, struct cli_ntcreate_state);
2149 if (tevent_req_is_nterror(req, &status)) {
2153 *fnum = state->fnum;
2158 return NT_STATUS_OK;
2161 NTSTATUS cli_ntcreate(struct cli_state *cli,
2163 uint32_t CreatFlags,
2164 uint32_t DesiredAccess,
2165 uint32_t FileAttributes,
2166 uint32_t ShareAccess,
2167 uint32_t CreateDisposition,
2168 uint32_t CreateOptions,
2169 uint8_t SecurityFlags,
2171 struct smb_create_returns *cr)
2173 TALLOC_CTX *frame = talloc_stackframe();
2174 struct tevent_context *ev;
2175 struct tevent_req *req;
2176 NTSTATUS status = NT_STATUS_NO_MEMORY;
2178 if (smbXcli_conn_has_async_calls(cli->conn)) {
2180 * Can't use sync call while an async call is in flight
2182 status = NT_STATUS_INVALID_PARAMETER;
2186 ev = samba_tevent_context_init(frame);
2191 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2192 DesiredAccess, FileAttributes, ShareAccess,
2193 CreateDisposition, CreateOptions,
2199 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2203 status = cli_ntcreate_recv(req, pfid, cr);
2209 struct cli_nttrans_create_state {
2211 struct smb_create_returns cr;
2214 static void cli_nttrans_create_done(struct tevent_req *subreq);
2216 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2217 struct tevent_context *ev,
2218 struct cli_state *cli,
2220 uint32_t CreatFlags,
2221 uint32_t DesiredAccess,
2222 uint32_t FileAttributes,
2223 uint32_t ShareAccess,
2224 uint32_t CreateDisposition,
2225 uint32_t CreateOptions,
2226 uint8_t SecurityFlags,
2227 struct security_descriptor *secdesc,
2228 struct ea_struct *eas,
2231 struct tevent_req *req, *subreq;
2232 struct cli_nttrans_create_state *state;
2234 uint8_t *secdesc_buf;
2237 size_t converted_len;
2239 req = tevent_req_create(mem_ctx,
2240 &state, struct cli_nttrans_create_state);
2245 if (secdesc != NULL) {
2246 status = marshall_sec_desc(talloc_tos(), secdesc,
2247 &secdesc_buf, &secdesc_len);
2248 if (tevent_req_nterror(req, status)) {
2249 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2250 nt_errstr(status)));
2251 return tevent_req_post(req, ev);
2262 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2263 return tevent_req_post(req, ev);
2266 param = talloc_array(state, uint8_t, 53);
2267 if (tevent_req_nomem(param, req)) {
2268 return tevent_req_post(req, ev);
2271 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2272 fname, strlen(fname),
2274 if (tevent_req_nomem(param, req)) {
2275 return tevent_req_post(req, ev);
2278 SIVAL(param, 0, CreatFlags);
2279 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2280 SIVAL(param, 8, DesiredAccess);
2281 SIVAL(param, 12, 0x0); /* AllocationSize */
2282 SIVAL(param, 16, 0x0); /* AllocationSize */
2283 SIVAL(param, 20, FileAttributes);
2284 SIVAL(param, 24, ShareAccess);
2285 SIVAL(param, 28, CreateDisposition);
2286 SIVAL(param, 32, CreateOptions |
2287 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2288 SIVAL(param, 36, secdesc_len);
2289 SIVAL(param, 40, 0); /* EA length*/
2290 SIVAL(param, 44, converted_len);
2291 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2292 SCVAL(param, 52, SecurityFlags);
2294 subreq = cli_trans_send(state, ev, cli,
2295 0, /* additional_flags2 */
2297 NULL, -1, /* name, fid */
2298 NT_TRANSACT_CREATE, 0,
2299 NULL, 0, 0, /* setup */
2300 param, talloc_get_size(param), 128, /* param */
2301 secdesc_buf, secdesc_len, 0); /* data */
2302 if (tevent_req_nomem(subreq, req)) {
2303 return tevent_req_post(req, ev);
2305 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2309 static void cli_nttrans_create_done(struct tevent_req *subreq)
2311 struct tevent_req *req = tevent_req_callback_data(
2312 subreq, struct tevent_req);
2313 struct cli_nttrans_create_state *state = tevent_req_data(
2314 req, struct cli_nttrans_create_state);
2319 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2320 NULL, 0, NULL, /* rsetup */
2321 ¶m, 69, &num_param,
2323 if (tevent_req_nterror(req, status)) {
2326 state->cr.oplock_level = CVAL(param, 0);
2327 state->fnum = SVAL(param, 2);
2328 state->cr.create_action = IVAL(param, 4);
2329 state->cr.creation_time = BVAL(param, 12);
2330 state->cr.last_access_time = BVAL(param, 20);
2331 state->cr.last_write_time = BVAL(param, 28);
2332 state->cr.change_time = BVAL(param, 36);
2333 state->cr.file_attributes = IVAL(param, 44);
2334 state->cr.allocation_size = BVAL(param, 48);
2335 state->cr.end_of_file = BVAL(param, 56);
2338 tevent_req_done(req);
2341 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2343 struct smb_create_returns *cr)
2345 struct cli_nttrans_create_state *state = tevent_req_data(
2346 req, struct cli_nttrans_create_state);
2349 if (tevent_req_is_nterror(req, &status)) {
2352 *fnum = state->fnum;
2356 return NT_STATUS_OK;
2359 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2361 uint32_t CreatFlags,
2362 uint32_t DesiredAccess,
2363 uint32_t FileAttributes,
2364 uint32_t ShareAccess,
2365 uint32_t CreateDisposition,
2366 uint32_t CreateOptions,
2367 uint8_t SecurityFlags,
2368 struct security_descriptor *secdesc,
2369 struct ea_struct *eas,
2372 struct smb_create_returns *cr)
2374 TALLOC_CTX *frame = talloc_stackframe();
2375 struct tevent_context *ev;
2376 struct tevent_req *req;
2377 NTSTATUS status = NT_STATUS_NO_MEMORY;
2379 if (smbXcli_conn_has_async_calls(cli->conn)) {
2381 * Can't use sync call while an async call is in flight
2383 status = NT_STATUS_INVALID_PARAMETER;
2386 ev = samba_tevent_context_init(frame);
2390 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2391 DesiredAccess, FileAttributes,
2392 ShareAccess, CreateDisposition,
2393 CreateOptions, SecurityFlags,
2394 secdesc, eas, num_eas);
2398 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2401 status = cli_nttrans_create_recv(req, pfid, cr);
2407 /****************************************************************************
2409 WARNING: if you open with O_WRONLY then getattrE won't work!
2410 ****************************************************************************/
2412 struct cli_openx_state {
2419 static void cli_openx_done(struct tevent_req *subreq);
2421 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2422 struct tevent_context *ev,
2423 struct cli_state *cli, const char *fname,
2424 int flags, int share_mode,
2425 struct tevent_req **psmbreq)
2427 struct tevent_req *req, *subreq;
2428 struct cli_openx_state *state;
2430 unsigned accessmode;
2431 uint8_t additional_flags;
2434 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2440 if (flags & O_CREAT) {
2443 if (!(flags & O_EXCL)) {
2444 if (flags & O_TRUNC)
2450 accessmode = (share_mode<<4);
2452 if ((flags & O_ACCMODE) == O_RDWR) {
2454 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2459 if ((flags & O_SYNC) == O_SYNC) {
2460 accessmode |= (1<<14);
2464 if (share_mode == DENY_FCB) {
2468 SCVAL(state->vwv + 0, 0, 0xFF);
2469 SCVAL(state->vwv + 0, 1, 0);
2470 SSVAL(state->vwv + 1, 0, 0);
2471 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2472 SSVAL(state->vwv + 3, 0, accessmode);
2473 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2474 SSVAL(state->vwv + 5, 0, 0);
2475 SIVAL(state->vwv + 6, 0, 0);
2476 SSVAL(state->vwv + 8, 0, openfn);
2477 SIVAL(state->vwv + 9, 0, 0);
2478 SIVAL(state->vwv + 11, 0, 0);
2479 SIVAL(state->vwv + 13, 0, 0);
2481 additional_flags = 0;
2483 if (cli->use_oplocks) {
2484 /* if using oplocks then ask for a batch oplock via
2485 core and extended methods */
2487 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2488 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2491 bytes = talloc_array(state, uint8_t, 0);
2492 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2493 strlen(fname)+1, NULL);
2495 if (tevent_req_nomem(bytes, req)) {
2496 return tevent_req_post(req, ev);
2499 state->bytes.iov_base = (void *)bytes;
2500 state->bytes.iov_len = talloc_get_size(bytes);
2502 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2503 0, 15, state->vwv, 1, &state->bytes);
2504 if (subreq == NULL) {
2508 tevent_req_set_callback(subreq, cli_openx_done, req);
2513 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2514 struct cli_state *cli, const char *fname,
2515 int flags, int share_mode)
2517 struct tevent_req *req, *subreq;
2520 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2526 status = smb1cli_req_chain_submit(&subreq, 1);
2527 if (tevent_req_nterror(req, status)) {
2528 return tevent_req_post(req, ev);
2533 static void cli_openx_done(struct tevent_req *subreq)
2535 struct tevent_req *req = tevent_req_callback_data(
2536 subreq, struct tevent_req);
2537 struct cli_openx_state *state = tevent_req_data(
2538 req, struct cli_openx_state);
2543 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2545 TALLOC_FREE(subreq);
2546 if (tevent_req_nterror(req, status)) {
2549 state->fnum = SVAL(vwv+2, 0);
2550 tevent_req_done(req);
2553 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2555 struct cli_openx_state *state = tevent_req_data(
2556 req, struct cli_openx_state);
2559 if (tevent_req_is_nterror(req, &status)) {
2562 *pfnum = state->fnum;
2563 return NT_STATUS_OK;
2566 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2567 int share_mode, uint16_t *pfnum)
2569 TALLOC_CTX *frame = talloc_stackframe();
2570 struct tevent_context *ev;
2571 struct tevent_req *req;
2572 NTSTATUS status = NT_STATUS_NO_MEMORY;
2574 if (smbXcli_conn_has_async_calls(cli->conn)) {
2576 * Can't use sync call while an async call is in flight
2578 status = NT_STATUS_INVALID_PARAMETER;
2582 ev = samba_tevent_context_init(frame);
2587 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2592 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2596 status = cli_openx_recv(req, pfnum);
2601 /****************************************************************************
2602 Synchronous wrapper function that does an NtCreateX open by preference
2603 and falls back to openX if this fails.
2604 ****************************************************************************/
2606 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2607 int share_mode_in, uint16_t *pfnum)
2610 unsigned int openfn = 0;
2611 unsigned int dos_deny = 0;
2612 uint32_t access_mask, share_mode, create_disposition, create_options;
2613 struct smb_create_returns cr;
2615 /* Do the initial mapping into OpenX parameters. */
2616 if (flags & O_CREAT) {
2619 if (!(flags & O_EXCL)) {
2620 if (flags & O_TRUNC)
2626 dos_deny = (share_mode_in<<4);
2628 if ((flags & O_ACCMODE) == O_RDWR) {
2630 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2635 if ((flags & O_SYNC) == O_SYNC) {
2636 dos_deny |= (1<<14);
2640 if (share_mode_in == DENY_FCB) {
2645 /* Hmmm. This is what I think the above code
2646 should look like if it's using the constants
2649 if (flags & O_CREAT) {
2650 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2652 if (!(flags & O_EXCL)) {
2653 if (flags & O_TRUNC)
2654 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2656 openfn |= OPENX_FILE_EXISTS_OPEN;
2659 dos_deny = SET_DENY_MODE(share_mode_in);
2661 if ((flags & O_ACCMODE) == O_RDWR) {
2662 dos_deny |= DOS_OPEN_RDWR;
2663 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2664 dos_deny |= DOS_OPEN_WRONLY;
2668 if ((flags & O_SYNC) == O_SYNC) {
2669 dos_deny |= FILE_SYNC_OPENMODE;
2673 if (share_mode_in == DENY_FCB) {
2678 if (!map_open_params_to_ntcreate(fname, dos_deny,
2679 openfn, &access_mask,
2680 &share_mode, &create_disposition,
2681 &create_options, NULL)) {
2685 status = cli_ntcreate(cli,
2697 /* Try and cope will all varients of "we don't do this call"
2698 and fall back to openX. */
2700 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2701 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2702 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2703 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2704 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2705 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2706 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2707 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2708 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2712 if (NT_STATUS_IS_OK(status) &&
2713 (create_options & FILE_NON_DIRECTORY_FILE) &&
2714 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2717 * Some (broken) servers return a valid handle
2718 * for directories even if FILE_NON_DIRECTORY_FILE
2719 * is set. Just close the handle and set the
2720 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2722 status = cli_close(cli, *pfnum);
2723 if (!NT_STATUS_IS_OK(status)) {
2726 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2727 /* Set this so libsmbclient can retrieve it. */
2728 cli->raw_status = status;
2735 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2738 /****************************************************************************
2740 ****************************************************************************/
2742 struct cli_close_state {
2746 static void cli_close_done(struct tevent_req *subreq);
2748 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2749 struct tevent_context *ev,
2750 struct cli_state *cli,
2752 struct tevent_req **psubreq)
2754 struct tevent_req *req, *subreq;
2755 struct cli_close_state *state;
2757 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2762 SSVAL(state->vwv+0, 0, fnum);
2763 SIVALS(state->vwv+1, 0, -1);
2765 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
2766 3, state->vwv, 0, NULL);
2767 if (subreq == NULL) {
2771 tevent_req_set_callback(subreq, cli_close_done, req);
2776 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2777 struct tevent_context *ev,
2778 struct cli_state *cli,
2781 struct tevent_req *req, *subreq;
2784 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2789 status = smb1cli_req_chain_submit(&subreq, 1);
2790 if (tevent_req_nterror(req, status)) {
2791 return tevent_req_post(req, ev);
2796 static void cli_close_done(struct tevent_req *subreq)
2798 struct tevent_req *req = tevent_req_callback_data(
2799 subreq, struct tevent_req);
2802 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2803 TALLOC_FREE(subreq);
2804 if (tevent_req_nterror(req, status)) {
2807 tevent_req_done(req);
2810 NTSTATUS cli_close_recv(struct tevent_req *req)
2812 return tevent_req_simple_recv_ntstatus(req);
2815 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2817 TALLOC_CTX *frame = NULL;
2818 struct tevent_context *ev;
2819 struct tevent_req *req;
2820 NTSTATUS status = NT_STATUS_OK;
2822 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2823 return cli_smb2_close_fnum(cli, fnum);
2826 frame = talloc_stackframe();
2828 if (smbXcli_conn_has_async_calls(cli->conn)) {
2830 * Can't use sync call while an async call is in flight
2832 status = NT_STATUS_INVALID_PARAMETER;
2836 ev = samba_tevent_context_init(frame);
2838 status = NT_STATUS_NO_MEMORY;
2842 req = cli_close_send(frame, ev, cli, fnum);
2844 status = NT_STATUS_NO_MEMORY;
2848 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2852 status = cli_close_recv(req);
2858 /****************************************************************************
2859 Truncate a file to a specified size
2860 ****************************************************************************/
2862 struct ftrunc_state {
2868 static void cli_ftruncate_done(struct tevent_req *subreq)
2870 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2871 NULL, 0, NULL, NULL, 0, NULL);
2872 tevent_req_simple_finish_ntstatus(subreq, status);
2875 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2876 struct tevent_context *ev,
2877 struct cli_state *cli,
2881 struct tevent_req *req = NULL, *subreq = NULL;
2882 struct ftrunc_state *state = NULL;
2884 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2889 /* Setup setup word. */
2890 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2892 /* Setup param array. */
2893 SSVAL(state->param,0,fnum);
2894 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2895 SSVAL(state->param,4,0);
2897 /* Setup data array. */
2898 SBVAL(state->data, 0, size);
2900 subreq = cli_trans_send(state, /* mem ctx. */
2901 ev, /* event ctx. */
2902 cli, /* cli_state. */
2903 0, /* additional_flags2 */
2904 SMBtrans2, /* cmd. */
2905 NULL, /* pipe name. */
2909 &state->setup, /* setup. */
2910 1, /* num setup uint16_t words. */
2911 0, /* max returned setup. */
2912 state->param, /* param. */
2914 2, /* max returned param. */
2915 state->data, /* data. */
2917 0); /* max returned data. */
2919 if (tevent_req_nomem(subreq, req)) {
2920 return tevent_req_post(req, ev);
2922 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2926 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2928 return tevent_req_simple_recv_ntstatus(req);
2931 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2933 TALLOC_CTX *frame = talloc_stackframe();
2934 struct tevent_context *ev = NULL;
2935 struct tevent_req *req = NULL;
2936 NTSTATUS status = NT_STATUS_OK;
2938 if (smbXcli_conn_has_async_calls(cli->conn)) {
2940 * Can't use sync call while an async call is in flight
2942 status = NT_STATUS_INVALID_PARAMETER;
2946 ev = samba_tevent_context_init(frame);
2948 status = NT_STATUS_NO_MEMORY;
2952 req = cli_ftruncate_send(frame,
2958 status = NT_STATUS_NO_MEMORY;
2962 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2966 status = cli_ftruncate_recv(req);
2973 /****************************************************************************
2974 send a lock with a specified locktype
2975 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2976 ****************************************************************************/
2978 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2979 uint32_t offset, uint32_t len,
2980 int timeout, unsigned char locktype)
2985 unsigned int set_timeout = 0;
2986 unsigned int saved_timeout = 0;
2988 SCVAL(vwv + 0, 0, 0xff);
2989 SCVAL(vwv + 0, 1, 0);
2990 SSVAL(vwv + 1, 0, 0);
2991 SSVAL(vwv + 2, 0, fnum);
2992 SCVAL(vwv + 3, 0, locktype);
2993 SCVAL(vwv + 3, 1, 0);
2994 SIVALS(vwv + 4, 0, timeout);
2995 SSVAL(vwv + 6, 0, 0);
2996 SSVAL(vwv + 7, 0, 1);
2998 SSVAL(bytes, 0, cli_getpid(cli));
2999 SIVAL(bytes, 2, offset);
3000 SIVAL(bytes, 6, len);
3003 if (timeout == -1) {
3004 set_timeout = 0x7FFFFFFF;
3006 set_timeout = timeout + 2*1000;
3008 saved_timeout = cli_set_timeout(cli, set_timeout);
3011 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3012 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3014 if (saved_timeout != 0) {
3015 cli_set_timeout(cli, saved_timeout);
3021 /****************************************************************************
3023 note that timeout is in units of 2 milliseconds
3024 ****************************************************************************/
3026 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3027 uint32_t offset, uint32_t len, int timeout,
3028 enum brl_type lock_type)
3032 status = cli_locktype(cli, fnum, offset, len, timeout,
3033 (lock_type == READ_LOCK? 1 : 0));
3037 /****************************************************************************
3039 ****************************************************************************/
3041 struct cli_unlock_state {
3046 static void cli_unlock_done(struct tevent_req *subreq);
3048 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3049 struct tevent_context *ev,
3050 struct cli_state *cli,
3056 struct tevent_req *req = NULL, *subreq = NULL;
3057 struct cli_unlock_state *state = NULL;
3058 uint8_t additional_flags = 0;
3060 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3065 SCVAL(state->vwv+0, 0, 0xFF);
3066 SSVAL(state->vwv+2, 0, fnum);
3067 SCVAL(state->vwv+3, 0, 0);
3068 SIVALS(state->vwv+4, 0, 0);
3069 SSVAL(state->vwv+6, 0, 1);
3070 SSVAL(state->vwv+7, 0, 0);
3072 SSVAL(state->data, 0, cli_getpid(cli));
3073 SIVAL(state->data, 2, offset);
3074 SIVAL(state->data, 6, len);
3076 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3077 8, state->vwv, 10, state->data);
3078 if (tevent_req_nomem(subreq, req)) {
3079 return tevent_req_post(req, ev);
3081 tevent_req_set_callback(subreq, cli_unlock_done, req);
3085 static void cli_unlock_done(struct tevent_req *subreq)
3087 struct tevent_req *req = tevent_req_callback_data(
3088 subreq, struct tevent_req);
3091 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3092 TALLOC_FREE(subreq);
3093 if (tevent_req_nterror(req, status)) {
3096 tevent_req_done(req);
3099 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3101 return tevent_req_simple_recv_ntstatus(req);
3104 NTSTATUS cli_unlock(struct cli_state *cli,
3109 TALLOC_CTX *frame = talloc_stackframe();
3110 struct tevent_context *ev;
3111 struct tevent_req *req;
3112 NTSTATUS status = NT_STATUS_OK;
3114 if (smbXcli_conn_has_async_calls(cli->conn)) {
3116 * Can't use sync call while an async call is in flight
3118 status = NT_STATUS_INVALID_PARAMETER;
3122 ev = samba_tevent_context_init(frame);
3124 status = NT_STATUS_NO_MEMORY;
3128 req = cli_unlock_send(frame, ev, cli,
3131 status = NT_STATUS_NO_MEMORY;
3135 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3139 status = cli_unlock_recv(req);
3146 /****************************************************************************
3147 Lock a file with 64 bit offsets.
3148 ****************************************************************************/
3150 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3151 uint64_t offset, uint64_t len, int timeout,
3152 enum brl_type lock_type)
3156 unsigned int set_timeout = 0;
3157 unsigned int saved_timeout = 0;
3161 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3162 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3165 ltype = (lock_type == READ_LOCK? 1 : 0);
3166 ltype |= LOCKING_ANDX_LARGE_FILES;
3168 SCVAL(vwv + 0, 0, 0xff);
3169 SCVAL(vwv + 0, 1, 0);
3170 SSVAL(vwv + 1, 0, 0);
3171 SSVAL(vwv + 2, 0, fnum);
3172 SCVAL(vwv + 3, 0, ltype);
3173 SCVAL(vwv + 3, 1, 0);
3174 SIVALS(vwv + 4, 0, timeout);
3175 SSVAL(vwv + 6, 0, 0);
3176 SSVAL(vwv + 7, 0, 1);
3178 SIVAL(bytes, 0, cli_getpid(cli));
3179 SOFF_T_R(bytes, 4, offset);
3180 SOFF_T_R(bytes, 12, len);
3183 if (timeout == -1) {
3184 set_timeout = 0x7FFFFFFF;
3186 set_timeout = timeout + 2*1000;
3188 saved_timeout = cli_set_timeout(cli, set_timeout);
3191 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3192 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3194 if (saved_timeout != 0) {
3195 cli_set_timeout(cli, saved_timeout);
3201 /****************************************************************************
3202 Unlock a file with 64 bit offsets.
3203 ****************************************************************************/
3205 struct cli_unlock64_state {
3210 static void cli_unlock64_done(struct tevent_req *subreq);
3212 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3213 struct tevent_context *ev,
3214 struct cli_state *cli,
3220 struct tevent_req *req = NULL, *subreq = NULL;
3221 struct cli_unlock64_state *state = NULL;
3222 uint8_t additional_flags = 0;
3224 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3229 SCVAL(state->vwv+0, 0, 0xff);
3230 SSVAL(state->vwv+2, 0, fnum);
3231 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3232 SIVALS(state->vwv+4, 0, 0);
3233 SSVAL(state->vwv+6, 0, 1);
3234 SSVAL(state->vwv+7, 0, 0);
3236 SIVAL(state->data, 0, cli_getpid(cli));
3237 SOFF_T_R(state->data, 4, offset);
3238 SOFF_T_R(state->data, 12, len);
3240 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3241 8, state->vwv, 20, state->data);
3242 if (tevent_req_nomem(subreq, req)) {
3243 return tevent_req_post(req, ev);
3245 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3249 static void cli_unlock64_done(struct tevent_req *subreq)
3251 struct tevent_req *req = tevent_req_callback_data(
3252 subreq, struct tevent_req);
3255 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3256 TALLOC_FREE(subreq);
3257 if (tevent_req_nterror(req, status)) {
3260 tevent_req_done(req);
3263 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3265 return tevent_req_simple_recv_ntstatus(req);
3268 NTSTATUS cli_unlock64(struct cli_state *cli,
3273 TALLOC_CTX *frame = talloc_stackframe();
3274 struct tevent_context *ev;
3275 struct tevent_req *req;
3276 NTSTATUS status = NT_STATUS_OK;
3278 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3279 return cli_unlock(cli, fnum, offset, len);
3282 if (smbXcli_conn_has_async_calls(cli->conn)) {
3284 * Can't use sync call while an async call is in flight
3286 status = NT_STATUS_INVALID_PARAMETER;
3290 ev = samba_tevent_context_init(frame);
3292 status = NT_STATUS_NO_MEMORY;
3296 req = cli_unlock64_send(frame, ev, cli,
3299 status = NT_STATUS_NO_MEMORY;
3303 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3307 status = cli_unlock64_recv(req);
3314 /****************************************************************************
3315 Get/unlock a POSIX lock on a file - internal function.
3316 ****************************************************************************/
3318 struct posix_lock_state {
3321 uint8_t data[POSIX_LOCK_DATA_SIZE];
3324 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3326 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3327 NULL, 0, NULL, NULL, 0, NULL);
3328 tevent_req_simple_finish_ntstatus(subreq, status);
3331 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3332 struct tevent_context *ev,
3333 struct cli_state *cli,
3338 enum brl_type lock_type)
3340 struct tevent_req *req = NULL, *subreq = NULL;
3341 struct posix_lock_state *state = NULL;
3343 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3348 /* Setup setup word. */
3349 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3351 /* Setup param array. */
3352 SSVAL(&state->param, 0, fnum);
3353 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3355 /* Setup data array. */
3356 switch (lock_type) {
3358 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3359 POSIX_LOCK_TYPE_READ);
3362 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3363 POSIX_LOCK_TYPE_WRITE);
3366 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3367 POSIX_LOCK_TYPE_UNLOCK);
3374 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3375 POSIX_LOCK_FLAG_WAIT);
3377 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3378 POSIX_LOCK_FLAG_NOWAIT);
3381 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3382 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3383 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3385 subreq = cli_trans_send(state, /* mem ctx. */
3386 ev, /* event ctx. */
3387 cli, /* cli_state. */
3388 0, /* additional_flags2 */
3389 SMBtrans2, /* cmd. */
3390 NULL, /* pipe name. */
3394 &state->setup, /* setup. */
3395 1, /* num setup uint16_t words. */
3396 0, /* max returned setup. */
3397 state->param, /* param. */
3399 2, /* max returned param. */
3400 state->data, /* data. */
3401 POSIX_LOCK_DATA_SIZE, /* num data. */
3402 0); /* max returned data. */
3404 if (tevent_req_nomem(subreq, req)) {
3405 return tevent_req_post(req, ev);
3407 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3411 /****************************************************************************
3413 ****************************************************************************/
3415 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3416 struct tevent_context *ev,
3417 struct cli_state *cli,
3422 enum brl_type lock_type)
3424 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3425 wait_lock, lock_type);
3428 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3430 return tevent_req_simple_recv_ntstatus(req);
3433 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3434 uint64_t offset, uint64_t len,
3435 bool wait_lock, enum brl_type lock_type)
3437 TALLOC_CTX *frame = talloc_stackframe();
3438 struct tevent_context *ev = NULL;
3439 struct tevent_req *req = NULL;
3440 NTSTATUS status = NT_STATUS_OK;
3442 if (smbXcli_conn_has_async_calls(cli->conn)) {
3444 * Can't use sync call while an async call is in flight
3446 status = NT_STATUS_INVALID_PARAMETER;
3450 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3451 status = NT_STATUS_INVALID_PARAMETER;
3455 ev = samba_tevent_context_init(frame);
3457 status = NT_STATUS_NO_MEMORY;
3461 req = cli_posix_lock_send(frame,
3470 status = NT_STATUS_NO_MEMORY;
3474 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3478 status = cli_posix_lock_recv(req);
3485 /****************************************************************************
3486 POSIX Unlock a file.
3487 ****************************************************************************/
3489 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3490 struct tevent_context *ev,
3491 struct cli_state *cli,
3496 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3497 false, UNLOCK_LOCK);
3500 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3502 return tevent_req_simple_recv_ntstatus(req);
3505 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3507 TALLOC_CTX *frame = talloc_stackframe();
3508 struct tevent_context *ev = NULL;
3509 struct tevent_req *req = NULL;
3510 NTSTATUS status = NT_STATUS_OK;
3512 if (smbXcli_conn_has_async_calls(cli->conn)) {
3514 * Can't use sync call while an async call is in flight
3516 status = NT_STATUS_INVALID_PARAMETER;
3520 ev = samba_tevent_context_init(frame);
3522 status = NT_STATUS_NO_MEMORY;
3526 req = cli_posix_unlock_send(frame,
3533 status = NT_STATUS_NO_MEMORY;
3537 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3541 status = cli_posix_unlock_recv(req);
3548 /****************************************************************************
3549 Do a SMBgetattrE call.
3550 ****************************************************************************/
3552 static void cli_getattrE_done(struct tevent_req *subreq);
3554 struct cli_getattrE_state {
3564 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3565 struct tevent_context *ev,
3566 struct cli_state *cli,
3569 struct tevent_req *req = NULL, *subreq = NULL;
3570 struct cli_getattrE_state *state = NULL;
3571 uint8_t additional_flags = 0;
3573 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3578 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3579 SSVAL(state->vwv+0,0,fnum);
3581 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3582 1, state->vwv, 0, NULL);
3583 if (tevent_req_nomem(subreq, req)) {
3584 return tevent_req_post(req, ev);
3586 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3590 static void cli_getattrE_done(struct tevent_req *subreq)
3592 struct tevent_req *req = tevent_req_callback_data(
3593 subreq, struct tevent_req);
3594 struct cli_getattrE_state *state = tevent_req_data(
3595 req, struct cli_getattrE_state);
3597 uint16_t *vwv = NULL;
3600 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3602 TALLOC_FREE(subreq);
3603 if (tevent_req_nterror(req, status)) {
3607 state->size = (off_t)IVAL(vwv+6,0);
3608 state->attr = SVAL(vwv+10,0);
3609 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3610 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3611 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3613 tevent_req_done(req);
3616 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3619 time_t *change_time,
3620 time_t *access_time,
3623 struct cli_getattrE_state *state = tevent_req_data(
3624 req, struct cli_getattrE_state);
3627 if (tevent_req_is_nterror(req, &status)) {
3631 *attr = state->attr;
3634 *size = state->size;
3637 *change_time = state->change_time;
3640 *access_time = state->access_time;
3643 *write_time = state->write_time;
3645 return NT_STATUS_OK;
3648 NTSTATUS cli_getattrE(struct cli_state *cli,
3652 time_t *change_time,
3653 time_t *access_time,
3656 TALLOC_CTX *frame = NULL;
3657 struct tevent_context *ev = NULL;
3658 struct tevent_req *req = NULL;
3659 NTSTATUS status = NT_STATUS_OK;
3661 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3662 return cli_smb2_getattrE(cli,
3671 frame = talloc_stackframe();
3673 if (smbXcli_conn_has_async_calls(cli->conn)) {
3675 * Can't use sync call while an async call is in flight
3677 status = NT_STATUS_INVALID_PARAMETER;
3681 ev = samba_tevent_context_init(frame);
3683 status = NT_STATUS_NO_MEMORY;
3687 req = cli_getattrE_send(frame, ev, cli, fnum);
3689 status = NT_STATUS_NO_MEMORY;
3693 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3697 status = cli_getattrE_recv(req,
3709 /****************************************************************************
3711 ****************************************************************************/
3713 static void cli_getatr_done(struct tevent_req *subreq);
3715 struct cli_getatr_state {
3722 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3723 struct tevent_context *ev,
3724 struct cli_state *cli,
3727 struct tevent_req *req = NULL, *subreq = NULL;
3728 struct cli_getatr_state *state = NULL;
3729 uint8_t additional_flags = 0;
3730 uint8_t *bytes = NULL;
3732 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3737 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3739 bytes = talloc_array(state, uint8_t, 1);
3740 if (tevent_req_nomem(bytes, req)) {
3741 return tevent_req_post(req, ev);
3744 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3745 strlen(fname)+1, NULL);
3747 if (tevent_req_nomem(bytes, req)) {
3748 return tevent_req_post(req, ev);
3751 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags, 0,
3752 0, NULL, talloc_get_size(bytes), bytes);
3753 if (tevent_req_nomem(subreq, req)) {
3754 return tevent_req_post(req, ev);
3756 tevent_req_set_callback(subreq, cli_getatr_done, req);
3760 static void cli_getatr_done(struct tevent_req *subreq)
3762 struct tevent_req *req = tevent_req_callback_data(
3763 subreq, struct tevent_req);
3764 struct cli_getatr_state *state = tevent_req_data(
3765 req, struct cli_getatr_state);
3767 uint16_t *vwv = NULL;
3770 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3772 TALLOC_FREE(subreq);
3773 if (tevent_req_nterror(req, status)) {
3777 state->attr = SVAL(vwv+0,0);
3778 state->size = (off_t)IVAL(vwv+3,0);
3779 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3781 tevent_req_done(req);
3784 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3789 struct cli_getatr_state *state = tevent_req_data(
3790 req, struct cli_getatr_state);
3793 if (tevent_req_is_nterror(req, &status)) {
3797 *attr = state->attr;
3800 *size = state->size;
3803 *write_time = state->write_time;
3805 return NT_STATUS_OK;
3808 NTSTATUS cli_getatr(struct cli_state *cli,
3814 TALLOC_CTX *frame = NULL;
3815 struct tevent_context *ev = NULL;
3816 struct tevent_req *req = NULL;
3817 NTSTATUS status = NT_STATUS_OK;
3819 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3820 return cli_smb2_getatr(cli,
3827 frame = talloc_stackframe();
3829 if (smbXcli_conn_has_async_calls(cli->conn)) {
3831 * Can't use sync call while an async call is in flight
3833 status = NT_STATUS_INVALID_PARAMETER;
3837 ev = samba_tevent_context_init(frame);
3839 status = NT_STATUS_NO_MEMORY;
3843 req = cli_getatr_send(frame, ev, cli, fname);
3845 status = NT_STATUS_NO_MEMORY;
3849 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3853 status = cli_getatr_recv(req,
3863 /****************************************************************************
3864 Do a SMBsetattrE call.
3865 ****************************************************************************/
3867 static void cli_setattrE_done(struct tevent_req *subreq);
3869 struct cli_setattrE_state {
3873 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3874 struct tevent_context *ev,
3875 struct cli_state *cli,
3881 struct tevent_req *req = NULL, *subreq = NULL;
3882 struct cli_setattrE_state *state = NULL;
3883 uint8_t additional_flags = 0;
3885 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3890 SSVAL(state->vwv+0, 0, fnum);
3891 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3892 smb1cli_conn_server_time_zone(cli->conn));
3893 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3894 smb1cli_conn_server_time_zone(cli->conn));
3895 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3896 smb1cli_conn_server_time_zone(cli->conn));
3898 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3899 7, state->vwv, 0, NULL);
3900 if (tevent_req_nomem(subreq, req)) {
3901 return tevent_req_post(req, ev);
3903 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3907 static void cli_setattrE_done(struct tevent_req *subreq)
3909 struct tevent_req *req = tevent_req_callback_data(
3910 subreq, struct tevent_req);
3913 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3914 TALLOC_FREE(subreq);
3915 if (tevent_req_nterror(req, status)) {
3918 tevent_req_done(req);
3921 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3923 return tevent_req_simple_recv_ntstatus(req);
3926 NTSTATUS cli_setattrE(struct cli_state *cli,
3932 TALLOC_CTX *frame = NULL;
3933 struct tevent_context *ev = NULL;
3934 struct tevent_req *req = NULL;
3935 NTSTATUS status = NT_STATUS_OK;
3937 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3938 return cli_smb2_setattrE(cli,
3945 frame = talloc_stackframe();
3947 if (smbXcli_conn_has_async_calls(cli->conn)) {
3949 * Can't use sync call while an async call is in flight
3951 status = NT_STATUS_INVALID_PARAMETER;
3955 ev = samba_tevent_context_init(frame);
3957 status = NT_STATUS_NO_MEMORY;
3961 req = cli_setattrE_send(frame, ev,
3969 status = NT_STATUS_NO_MEMORY;
3973 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3977 status = cli_setattrE_recv(req);
3984 /****************************************************************************
3985 Do a SMBsetatr call.
3986 ****************************************************************************/
3988 static void cli_setatr_done(struct tevent_req *subreq);
3990 struct cli_setatr_state {
3994 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3995 struct tevent_context *ev,
3996 struct cli_state *cli,
4001 struct tevent_req *req = NULL, *subreq = NULL;
4002 struct cli_setatr_state *state = NULL;
4003 uint8_t additional_flags = 0;
4004 uint8_t *bytes = NULL;
4006 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4011 SSVAL(state->vwv+0, 0, attr);
4012 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4014 bytes = talloc_array(state, uint8_t, 1);
4015 if (tevent_req_nomem(bytes, req)) {
4016 return tevent_req_post(req, ev);
4019 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4020 strlen(fname)+1, NULL);
4021 if (tevent_req_nomem(bytes, req)) {
4022 return tevent_req_post(req, ev);
4024 bytes = talloc_realloc(state, bytes, uint8_t,
4025 talloc_get_size(bytes)+1);
4026 if (tevent_req_nomem(bytes, req)) {
4027 return tevent_req_post(req, ev);
4030 bytes[talloc_get_size(bytes)-1] = 4;
4031 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4033 if (tevent_req_nomem(bytes, req)) {
4034 return tevent_req_post(req, ev);
4037 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags, 0,
4038 8, state->vwv, talloc_get_size(bytes), bytes);
4039 if (tevent_req_nomem(subreq, req)) {
4040 return tevent_req_post(req, ev);
4042 tevent_req_set_callback(subreq, cli_setatr_done, req);
4046 static void cli_setatr_done(struct tevent_req *subreq)
4048 struct tevent_req *req = tevent_req_callback_data(
4049 subreq, struct tevent_req);
4052 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4053 TALLOC_FREE(subreq);
4054 if (tevent_req_nterror(req, status)) {
4057 tevent_req_done(req);
4060 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4062 return tevent_req_simple_recv_ntstatus(req);
4065 NTSTATUS cli_setatr(struct cli_state *cli,
4070 TALLOC_CTX *frame = NULL;
4071 struct tevent_context *ev = NULL;
4072 struct tevent_req *req = NULL;
4073 NTSTATUS status = NT_STATUS_OK;
4075 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4076 return cli_smb2_setatr(cli,
4082 frame = talloc_stackframe();
4084 if (smbXcli_conn_has_async_calls(cli->conn)) {
4086 * Can't use sync call while an async call is in flight
4088 status = NT_STATUS_INVALID_PARAMETER;
4092 ev = samba_tevent_context_init(frame);
4094 status = NT_STATUS_NO_MEMORY;
4098 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4100 status = NT_STATUS_NO_MEMORY;
4104 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4108 status = cli_setatr_recv(req);
4115 /****************************************************************************
4116 Check for existance of a dir.
4117 ****************************************************************************/
4119 static void cli_chkpath_done(struct tevent_req *subreq);
4121 struct cli_chkpath_state {
4125 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4126 struct tevent_context *ev,
4127 struct cli_state *cli,
4130 struct tevent_req *req = NULL, *subreq = NULL;
4131 struct cli_chkpath_state *state = NULL;
4132 uint8_t additional_flags = 0;
4133 uint8_t *bytes = NULL;
4135 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4140 bytes = talloc_array(state, uint8_t, 1);
4141 if (tevent_req_nomem(bytes, req)) {
4142 return tevent_req_post(req, ev);
4145 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4146 strlen(fname)+1, NULL);
4148 if (tevent_req_nomem(bytes, req)) {
4149 return tevent_req_post(req, ev);
4152 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags, 0,
4153 0, NULL, talloc_get_size(bytes), bytes);
4154 if (tevent_req_nomem(subreq, req)) {
4155 return tevent_req_post(req, ev);
4157 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4161 static void cli_chkpath_done(struct tevent_req *subreq)
4163 struct tevent_req *req = tevent_req_callback_data(
4164 subreq, struct tevent_req);
4167 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4168 TALLOC_FREE(subreq);
4169 if (tevent_req_nterror(req, status)) {
4172 tevent_req_done(req);
4175 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4177 return tevent_req_simple_recv_ntstatus(req);
4180 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4182 TALLOC_CTX *frame = talloc_stackframe();
4183 struct tevent_context *ev = NULL;
4184 struct tevent_req *req = NULL;
4186 NTSTATUS status = NT_STATUS_OK;
4188 if (smbXcli_conn_has_async_calls(cli->conn)) {
4190 * Can't use sync call while an async call is in flight
4192 status = NT_STATUS_INVALID_PARAMETER;
4196 path2 = talloc_strdup(frame, path);
4198 status = NT_STATUS_NO_MEMORY;
4201 trim_char(path2,'\0','\\');
4203 path2 = talloc_strdup(frame, "\\");
4205 status = NT_STATUS_NO_MEMORY;
4210 ev = samba_tevent_context_init(frame);
4212 status = NT_STATUS_NO_MEMORY;
4216 req = cli_chkpath_send(frame, ev, cli, path2);
4218 status = NT_STATUS_NO_MEMORY;
4222 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4226 status = cli_chkpath_recv(req);
4233 /****************************************************************************
4235 ****************************************************************************/
4237 static void cli_dskattr_done(struct tevent_req *subreq);
4239 struct cli_dskattr_state {
4245 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4246 struct tevent_context *ev,
4247 struct cli_state *cli)
4249 struct tevent_req *req = NULL, *subreq = NULL;
4250 struct cli_dskattr_state *state = NULL;
4251 uint8_t additional_flags = 0;
4253 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4258 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4260 if (tevent_req_nomem(subreq, req)) {
4261 return tevent_req_post(req, ev);
4263 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4267 static void cli_dskattr_done(struct tevent_req *subreq)
4269 struct tevent_req *req = tevent_req_callback_data(
4270 subreq, struct tevent_req);
4271 struct cli_dskattr_state *state = tevent_req_data(
4272 req, struct cli_dskattr_state);
4274 uint16_t *vwv = NULL;
4277 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4279 TALLOC_FREE(subreq);
4280 if (tevent_req_nterror(req, status)) {
4283 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4284 state->total = SVAL(vwv+0, 0);
4285 state->avail = SVAL(vwv+3, 0);
4286 tevent_req_done(req);
4289 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4291 struct cli_dskattr_state *state = tevent_req_data(
4292 req, struct cli_dskattr_state);
4295 if (tevent_req_is_nterror(req, &status)) {
4298 *bsize = state->bsize;
4299 *total = state->total;
4300 *avail = state->avail;
4301 return NT_STATUS_OK;
4304 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4306 TALLOC_CTX *frame = NULL;
4307 struct tevent_context *ev = NULL;
4308 struct tevent_req *req = NULL;
4309 NTSTATUS status = NT_STATUS_OK;
4311 frame = talloc_stackframe();
4313 if (smbXcli_conn_has_async_calls(cli->conn)) {
4315 * Can't use sync call while an async call is in flight
4317 status = NT_STATUS_INVALID_PARAMETER;
4321 ev = samba_tevent_context_init(frame);
4323 status = NT_STATUS_NO_MEMORY;
4327 req = cli_dskattr_send(frame, ev, cli);
4329 status = NT_STATUS_NO_MEMORY;
4333 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4337 status = cli_dskattr_recv(req, bsize, total, avail);
4344 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4345 uint64_t *total, uint64_t *avail)
4347 uint64_t sectors_per_block;
4348 uint64_t bytes_per_sector;
4349 int old_bsize, old_total, old_avail;
4352 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4353 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4357 * Try the trans2 disk full size info call first.
4358 * We already use this in SMBC_fstatvfs_ctx().
4359 * Ignore 'actual_available_units' as we only
4360 * care about the quota for the caller.
4363 status = cli_get_fs_full_size_info(cli,
4370 /* Try and cope will all varients of "we don't do this call"
4371 and fall back to cli_dskattr. */
4373 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4374 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4375 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4376 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4377 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4378 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4379 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4380 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4381 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4382 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4386 if (!NT_STATUS_IS_OK(status)) {
4391 *bsize = sectors_per_block *
4395 return NT_STATUS_OK;
4399 /* Old SMB1 core protocol fallback. */
4400 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4401 if (!NT_STATUS_IS_OK(status)) {
4405 *bsize = (uint64_t)old_bsize;
4408 *total = (uint64_t)old_total;
4411 *avail = (uint64_t)old_avail;
4413 return NT_STATUS_OK;
4416 /****************************************************************************
4417 Create and open a temporary file.
4418 ****************************************************************************/
4420 static void cli_ctemp_done(struct tevent_req *subreq);
4422 struct ctemp_state {
4428 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4429 struct tevent_context *ev,
4430 struct cli_state *cli,
4433 struct tevent_req *req = NULL, *subreq = NULL;
4434 struct ctemp_state *state = NULL;
4435 uint8_t additional_flags = 0;
4436 uint8_t *bytes = NULL;
4438 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4443 SSVAL(state->vwv,0,0);
4444 SIVALS(state->vwv+1,0,-1);
4446 bytes = talloc_array(state, uint8_t, 1);
4447 if (tevent_req_nomem(bytes, req)) {
4448 return tevent_req_post(req, ev);
4451 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4452 strlen(path)+1, NULL);
4453 if (tevent_req_nomem(bytes, req)) {
4454 return tevent_req_post(req, ev);
4457 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags, 0,
4458 3, state->vwv, talloc_get_size(bytes), bytes);
4459 if (tevent_req_nomem(subreq, req)) {
4460 return tevent_req_post(req, ev);
4462 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4466 static void cli_ctemp_done(struct tevent_req *subreq)
4468 struct tevent_req *req = tevent_req_callback_data(
4469 subreq, struct tevent_req);
4470 struct ctemp_state *state = tevent_req_data(
4471 req, struct ctemp_state);
4475 uint32_t num_bytes = 0;
4476 uint8_t *bytes = NULL;
4478 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4479 &num_bytes, &bytes);
4480 TALLOC_FREE(subreq);
4481 if (tevent_req_nterror(req, status)) {
4485 state->fnum = SVAL(vwv+0, 0);
4487 /* From W2K3, the result is just the ASCII name */
4488 if (num_bytes < 2) {
4489 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4493 if (pull_string_talloc(state,
4500 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4503 tevent_req_done(req);
4506 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4511 struct ctemp_state *state = tevent_req_data(req,
4512 struct ctemp_state);
4515 if (tevent_req_is_nterror(req, &status)) {
4518 *pfnum = state->fnum;
4519 *outfile = talloc_strdup(ctx, state->ret_path);
4521 return NT_STATUS_NO_MEMORY;
4523 return NT_STATUS_OK;
4526 NTSTATUS cli_ctemp(struct cli_state *cli,
4532 TALLOC_CTX *frame = talloc_stackframe();
4533 struct tevent_context *ev;
4534 struct tevent_req *req;
4535 NTSTATUS status = NT_STATUS_OK;
4537 if (smbXcli_conn_has_async_calls(cli->conn)) {
4539 * Can't use sync call while an async call is in flight
4541 status = NT_STATUS_INVALID_PARAMETER;
4545 ev = samba_tevent_context_init(frame);
4547 status = NT_STATUS_NO_MEMORY;
4551 req = cli_ctemp_send(frame, ev, cli, path);
4553 status = NT_STATUS_NO_MEMORY;
4557 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4561 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4569 send a raw ioctl - used by the torture code
4571 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4576 SSVAL(vwv+0, 0, fnum);
4577 SSVAL(vwv+1, 0, code>>16);
4578 SSVAL(vwv+2, 0, (code&0xFFFF));
4580 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4581 NULL, 0, NULL, NULL, NULL, NULL);
4582 if (!NT_STATUS_IS_OK(status)) {
4585 *blob = data_blob_null;
4586 return NT_STATUS_OK;
4589 /*********************************************************
4590 Set an extended attribute utility fn.
4591 *********************************************************/
4593 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4594 uint8_t *param, unsigned int param_len,
4595 const char *ea_name,
4596 const char *ea_val, size_t ea_len)
4599 unsigned int data_len = 0;
4600 uint8_t *data = NULL;
4602 size_t ea_namelen = strlen(ea_name);
4605 SSVAL(setup, 0, setup_val);
4607 if (ea_namelen == 0 && ea_len == 0) {
4609 data = talloc_array(talloc_tos(),
4613 return NT_STATUS_NO_MEMORY;
4616 SIVAL(p,0,data_len);
4618 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4619 data = talloc_array(talloc_tos(),
4623 return NT_STATUS_NO_MEMORY;
4626 SIVAL(p,0,data_len);
4628 SCVAL(p, 0, 0); /* EA flags. */
4629 SCVAL(p, 1, ea_namelen);
4630 SSVAL(p, 2, ea_len);
4631 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4632 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4635 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4637 param, param_len, 2,
4640 NULL, 0, NULL, /* rsetup */
4641 NULL, 0, NULL, /* rparam */
4642 NULL, 0, NULL); /* rdata */
4647 /*********************************************************
4648 Set an extended attribute on a pathname.
4649 *********************************************************/
4651 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4652 const char *ea_name, const char *ea_val,
4655 unsigned int param_len = 0;
4658 TALLOC_CTX *frame = NULL;
4660 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4661 return cli_smb2_set_ea_path(cli,
4668 frame = talloc_stackframe();
4670 param = talloc_array(frame, uint8_t, 6);
4672 status = NT_STATUS_NO_MEMORY;
4675 SSVAL(param,0,SMB_INFO_SET_EA);
4679 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4680 path, strlen(path)+1,
4682 param_len = talloc_get_size(param);
4684 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4685 ea_name, ea_val, ea_len);
4693 /*********************************************************
4694 Set an extended attribute on an fnum.
4695 *********************************************************/
4697 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4698 const char *ea_name, const char *ea_val,
4703 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4704 return cli_smb2_set_ea_fnum(cli,
4711 memset(param, 0, 6);
4712 SSVAL(param,0,fnum);
4713 SSVAL(param,2,SMB_INFO_SET_EA);
4715 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4716 ea_name, ea_val, ea_len);
4719 /*********************************************************
4720 Get an extended attribute list utility fn.
4721 *********************************************************/
4723 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4725 size_t *pnum_eas, struct ea_struct **pea_list)
4727 struct ea_struct *ea_list = NULL;
4732 if (rdata_len < 4) {
4736 ea_size = (size_t)IVAL(rdata,0);
4737 if (ea_size > rdata_len) {
4742 /* No EA's present. */
4751 /* Validate the EA list and count it. */
4752 for (num_eas = 0; ea_size >= 4; num_eas++) {
4753 unsigned int ea_namelen = CVAL(p,1);
4754 unsigned int ea_valuelen = SVAL(p,2);
4755 if (ea_namelen == 0) {
4758 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4761 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4762 p += 4 + ea_namelen + 1 + ea_valuelen;
4771 *pnum_eas = num_eas;
4773 /* Caller only wants number of EA's. */
4777 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4782 ea_size = (size_t)IVAL(rdata,0);
4785 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4786 struct ea_struct *ea = &ea_list[num_eas];
4787 fstring unix_ea_name;
4788 unsigned int ea_namelen = CVAL(p,1);
4789 unsigned int ea_valuelen = SVAL(p,2);
4791 ea->flags = CVAL(p,0);
4792 unix_ea_name[0] = '\0';
4793 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4794 ea->name = talloc_strdup(ea_list, unix_ea_name);
4798 /* Ensure the value is null terminated (in case it's a string). */
4799 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4800 if (!ea->value.data) {
4804 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4806 ea->value.data[ea_valuelen] = 0;
4808 p += 4 + ea_namelen + 1 + ea_valuelen;
4811 *pea_list = ea_list;
4815 TALLOC_FREE(ea_list);
4819 /*********************************************************
4820 Get an extended attribute list from a pathname.
4821 *********************************************************/
4823 struct cli_get_ea_list_path_state {
4828 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4830 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4831 struct tevent_context *ev,
4832 struct cli_state *cli,
4835 struct tevent_req *req, *subreq;
4836 struct cli_get_ea_list_path_state *state;
4838 req = tevent_req_create(mem_ctx, &state,
4839 struct cli_get_ea_list_path_state);
4843 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4844 SMB_INFO_QUERY_ALL_EAS, 4,
4846 if (tevent_req_nomem(subreq, req)) {
4847 return tevent_req_post(req, ev);
4849 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4853 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4855 struct tevent_req *req = tevent_req_callback_data(
4856 subreq, struct tevent_req);
4857 struct cli_get_ea_list_path_state *state = tevent_req_data(
4858 req, struct cli_get_ea_list_path_state);
4861 status = cli_qpathinfo_recv(subreq, state, &state->data,
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4867 tevent_req_done(req);
4870 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4871 size_t *pnum_eas, struct ea_struct **peas)
4873 struct cli_get_ea_list_path_state *state = tevent_req_data(
4874 req, struct cli_get_ea_list_path_state);
4877 if (tevent_req_is_nterror(req, &status)) {
4880 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4882 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4884 return NT_STATUS_OK;
4887 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4890 struct ea_struct **pea_list)
4892 TALLOC_CTX *frame = NULL;
4893 struct tevent_context *ev = NULL;
4894 struct tevent_req *req = NULL;
4895 NTSTATUS status = NT_STATUS_NO_MEMORY;
4897 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4898 return cli_smb2_get_ea_list_path(cli,
4905 frame = talloc_stackframe();
4907 if (smbXcli_conn_has_async_calls(cli->conn)) {
4909 * Can't use sync call while an async call is in flight
4911 status = NT_STATUS_INVALID_PARAMETER;
4914 ev = samba_tevent_context_init(frame);
4918 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4922 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4925 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4931 /****************************************************************************
4932 Convert open "flags" arg to uint32_t on wire.
4933 ****************************************************************************/
4935 static uint32_t open_flags_to_wire(int flags)
4937 int open_mode = flags & O_ACCMODE;
4940 switch (open_mode) {
4942 ret |= SMB_O_WRONLY;
4949 ret |= SMB_O_RDONLY;
4953 if (flags & O_CREAT) {
4956 if (flags & O_EXCL) {
4959 if (flags & O_TRUNC) {
4963 if (flags & O_SYNC) {
4967 if (flags & O_APPEND) {
4968 ret |= SMB_O_APPEND;
4970 #if defined(O_DIRECT)
4971 if (flags & O_DIRECT) {
4972 ret |= SMB_O_DIRECT;
4975 #if defined(O_DIRECTORY)
4976 if (flags & O_DIRECTORY) {
4977 ret |= SMB_O_DIRECTORY;
4983 /****************************************************************************
4984 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4985 ****************************************************************************/
4987 struct posix_open_state {
4991 uint16_t fnum; /* Out */
4994 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4996 struct tevent_req *req = tevent_req_callback_data(
4997 subreq, struct tevent_req);
4998 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5003 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5004 NULL, 0, NULL, &data, 12, &num_data);
5005 TALLOC_FREE(subreq);
5006 if (tevent_req_nterror(req, status)) {
5009 state->fnum = SVAL(data,2);
5010 tevent_req_done(req);
5013 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5014 struct tevent_context *ev,
5015 struct cli_state *cli,
5021 struct tevent_req *req = NULL, *subreq = NULL;
5022 struct posix_open_state *state = NULL;
5023 uint32_t wire_flags = open_flags_to_wire(flags);
5025 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5030 /* Setup setup word. */
5031 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5033 /* Setup param array. */
5034 state->param = talloc_array(state, uint8_t, 6);
5035 if (tevent_req_nomem(state->param, req)) {
5036 return tevent_req_post(req, ev);
5038 memset(state->param, '\0', 6);
5039 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5041 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5042 strlen(fname)+1, NULL);
5044 if (tevent_req_nomem(state->param, req)) {
5045 return tevent_req_post(req, ev);
5048 /* Setup data words. */
5050 wire_flags |= SMB_O_DIRECTORY;
5053 SIVAL(state->data,0,0); /* No oplock. */
5054 SIVAL(state->data,4,wire_flags);
5055 SIVAL(state->data,8,unix_perms_to_wire(mode));
5056 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5057 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5059 subreq = cli_trans_send(state, /* mem ctx. */
5060 ev, /* event ctx. */
5061 cli, /* cli_state. */
5062 0, /* additional_flags2 */
5063 SMBtrans2, /* cmd. */
5064 NULL, /* pipe name. */
5068 &state->setup, /* setup. */
5069 1, /* num setup uint16_t words. */
5070 0, /* max returned setup. */
5071 state->param, /* param. */
5072 talloc_get_size(state->param),/* num param. */
5073 2, /* max returned param. */
5074 state->data, /* data. */
5076 12); /* max returned data. */
5078 if (tevent_req_nomem(subreq, req)) {
5079 return tevent_req_post(req, ev);
5081 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5085 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5086 struct tevent_context *ev,
5087 struct cli_state *cli,
5092 return cli_posix_open_internal_send(mem_ctx, ev,
5093 cli, fname, flags, mode, false);
5096 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5098 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5101 if (tevent_req_is_nterror(req, &status)) {
5104 *pfnum = state->fnum;
5105 return NT_STATUS_OK;
5108 /****************************************************************************
5109 Open - POSIX semantics. Doesn't request oplock.
5110 ****************************************************************************/
5112 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5113 int flags, mode_t mode, uint16_t *pfnum)
5116 TALLOC_CTX *frame = talloc_stackframe();
5117 struct tevent_context *ev = NULL;
5118 struct tevent_req *req = NULL;
5119 NTSTATUS status = NT_STATUS_OK;
5121 if (smbXcli_conn_has_async_calls(cli->conn)) {
5123 * Can't use sync call while an async call is in flight
5125 status = NT_STATUS_INVALID_PARAMETER;
5129 ev = samba_tevent_context_init(frame);
5131 status = NT_STATUS_NO_MEMORY;
5135 req = cli_posix_open_send(frame,
5142 status = NT_STATUS_NO_MEMORY;
5146 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5150 status = cli_posix_open_recv(req, pfnum);
5157 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5158 struct tevent_context *ev,
5159 struct cli_state *cli,
5163 return cli_posix_open_internal_send(mem_ctx, ev,
5164 cli, fname, O_CREAT, mode, true);
5167 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5169 return tevent_req_simple_recv_ntstatus(req);
5172 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5174 TALLOC_CTX *frame = talloc_stackframe();
5175 struct tevent_context *ev = NULL;
5176 struct tevent_req *req = NULL;
5177 NTSTATUS status = NT_STATUS_OK;
5179 if (smbXcli_conn_has_async_calls(cli->conn)) {
5181 * Can't use sync call while an async call is in flight
5183 status = NT_STATUS_INVALID_PARAMETER;
5187 ev = samba_tevent_context_init(frame);
5189 status = NT_STATUS_NO_MEMORY;
5193 req = cli_posix_mkdir_send(frame,
5199 status = NT_STATUS_NO_MEMORY;
5203 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5207 status = cli_posix_mkdir_recv(req);
5214 /****************************************************************************
5215 unlink or rmdir - POSIX semantics.
5216 ****************************************************************************/
5218 struct cli_posix_unlink_internal_state {
5222 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5224 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5225 struct tevent_context *ev,
5226 struct cli_state *cli,
5230 struct tevent_req *req = NULL, *subreq = NULL;
5231 struct cli_posix_unlink_internal_state *state = NULL;
5233 req = tevent_req_create(mem_ctx, &state,
5234 struct cli_posix_unlink_internal_state);
5239 /* Setup data word. */
5240 SSVAL(state->data, 0, level);
5242 subreq = cli_setpathinfo_send(state, ev, cli,
5243 SMB_POSIX_PATH_UNLINK,
5245 state->data, sizeof(state->data));
5246 if (tevent_req_nomem(subreq, req)) {
5247 return tevent_req_post(req, ev);
5249 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5253 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5255 NTSTATUS status = cli_setpathinfo_recv(subreq);
5256 tevent_req_simple_finish_ntstatus(subreq, status);
5259 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5260 struct tevent_context *ev,
5261 struct cli_state *cli,
5264 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5265 SMB_POSIX_UNLINK_FILE_TARGET);
5268 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5270 return tevent_req_simple_recv_ntstatus(req);
5273 /****************************************************************************
5274 unlink - POSIX semantics.
5275 ****************************************************************************/
5277 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5279 TALLOC_CTX *frame = talloc_stackframe();
5280 struct tevent_context *ev = NULL;
5281 struct tevent_req *req = NULL;
5282 NTSTATUS status = NT_STATUS_OK;
5284 if (smbXcli_conn_has_async_calls(cli->conn)) {
5286 * Can't use sync call while an async call is in flight
5288 status = NT_STATUS_INVALID_PARAMETER;
5292 ev = samba_tevent_context_init(frame);
5294 status = NT_STATUS_NO_MEMORY;
5298 req = cli_posix_unlink_send(frame,
5303 status = NT_STATUS_NO_MEMORY;
5307 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5311 status = cli_posix_unlink_recv(req);
5318 /****************************************************************************
5319 rmdir - POSIX semantics.
5320 ****************************************************************************/
5322 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5323 struct tevent_context *ev,
5324 struct cli_state *cli,
5327 return cli_posix_unlink_internal_send(
5328 mem_ctx, ev, cli, fname,
5329 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5332 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5334 return tevent_req_simple_recv_ntstatus(req);
5337 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5339 TALLOC_CTX *frame = talloc_stackframe();
5340 struct tevent_context *ev = NULL;
5341 struct tevent_req *req = NULL;
5342 NTSTATUS status = NT_STATUS_OK;
5344 if (smbXcli_conn_has_async_calls(cli->conn)) {
5346 * Can't use sync call while an async call is in flight
5348 status = NT_STATUS_INVALID_PARAMETER;
5352 ev = samba_tevent_context_init(frame);
5354 status = NT_STATUS_NO_MEMORY;
5358 req = cli_posix_rmdir_send(frame,
5363 status = NT_STATUS_NO_MEMORY;
5367 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5371 status = cli_posix_rmdir_recv(req, frame);
5378 /****************************************************************************
5380 ****************************************************************************/
5382 struct cli_notify_state {
5384 uint32_t num_changes;
5385 struct notify_change *changes;
5388 static void cli_notify_done(struct tevent_req *subreq);
5390 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5391 struct tevent_context *ev,
5392 struct cli_state *cli, uint16_t fnum,
5393 uint32_t buffer_size,
5394 uint32_t completion_filter, bool recursive)
5396 struct tevent_req *req, *subreq;
5397 struct cli_notify_state *state;
5398 unsigned old_timeout;
5400 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5405 SIVAL(state->setup, 0, completion_filter);
5406 SSVAL(state->setup, 4, fnum);
5407 SSVAL(state->setup, 6, recursive);
5410 * Notifies should not time out
5412 old_timeout = cli_set_timeout(cli, 0);
5414 subreq = cli_trans_send(
5415 state, /* mem ctx. */
5416 ev, /* event ctx. */
5417 cli, /* cli_state. */
5418 0, /* additional_flags2 */
5419 SMBnttrans, /* cmd. */
5420 NULL, /* pipe name. */
5422 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5424 (uint16_t *)state->setup, /* setup. */
5425 4, /* num setup uint16_t words. */
5426 0, /* max returned setup. */
5429 buffer_size, /* max returned param. */
5432 0); /* max returned data. */
5434 cli_set_timeout(cli, old_timeout);
5436 if (tevent_req_nomem(subreq, req)) {
5437 return tevent_req_post(req, ev);
5439 tevent_req_set_callback(subreq, cli_notify_done, req);
5443 static void cli_notify_done(struct tevent_req *subreq)
5445 struct tevent_req *req = tevent_req_callback_data(
5446 subreq, struct tevent_req);
5447 struct cli_notify_state *state = tevent_req_data(
5448 req, struct cli_notify_state);
5451 uint32_t i, ofs, num_params;
5454 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5455 ¶ms, 0, &num_params, NULL, 0, NULL);
5456 TALLOC_FREE(subreq);
5457 if (tevent_req_nterror(req, status)) {
5458 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5462 state->num_changes = 0;
5465 while (num_params - ofs > 12) {
5466 uint32_t next = IVAL(params, ofs);
5467 state->num_changes += 1;
5469 if ((next == 0) || (ofs+next >= num_params)) {
5475 state->changes = talloc_array(state, struct notify_change,
5476 state->num_changes);
5477 if (tevent_req_nomem(state->changes, req)) {
5478 TALLOC_FREE(params);
5484 for (i=0; i<state->num_changes; i++) {
5485 uint32_t next = IVAL(params, ofs);
5486 uint32_t len = IVAL(params, ofs+8);
5490 if (trans_oob(num_params, ofs + 12, len)) {
5491 TALLOC_FREE(params);
5493 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5497 state->changes[i].action = IVAL(params, ofs+4);
5498 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5499 &name, params+ofs+12, len,
5500 STR_TERMINATE|STR_UNICODE);
5502 TALLOC_FREE(params);
5503 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5506 state->changes[i].name = name;
5510 TALLOC_FREE(params);
5511 tevent_req_done(req);
5514 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5515 uint32_t *pnum_changes,
5516 struct notify_change **pchanges)
5518 struct cli_notify_state *state = tevent_req_data(
5519 req, struct cli_notify_state);
5522 if (tevent_req_is_nterror(req, &status)) {
5526 *pnum_changes = state->num_changes;
5527 *pchanges = talloc_move(mem_ctx, &state->changes);
5528 return NT_STATUS_OK;
5531 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5532 uint32_t completion_filter, bool recursive,
5533 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5534 struct notify_change **pchanges)
5536 TALLOC_CTX *frame = talloc_stackframe();
5537 struct tevent_context *ev;
5538 struct tevent_req *req;
5539 NTSTATUS status = NT_STATUS_NO_MEMORY;
5541 if (smbXcli_conn_has_async_calls(cli->conn)) {
5543 * Can't use sync call while an async call is in flight
5545 status = NT_STATUS_INVALID_PARAMETER;
5548 ev = samba_tevent_context_init(frame);
5552 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5553 completion_filter, recursive);
5557 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5560 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5566 struct cli_qpathinfo_state {
5575 static void cli_qpathinfo_done(struct tevent_req *subreq);
5577 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5578 struct tevent_context *ev,
5579 struct cli_state *cli, const char *fname,
5580 uint16_t level, uint32_t min_rdata,
5583 struct tevent_req *req, *subreq;
5584 struct cli_qpathinfo_state *state;
5585 uint16_t additional_flags2 = 0;
5587 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5591 state->min_rdata = min_rdata;
5592 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5594 state->param = talloc_zero_array(state, uint8_t, 6);
5595 if (tevent_req_nomem(state->param, req)) {
5596 return tevent_req_post(req, ev);
5598 SSVAL(state->param, 0, level);
5599 state->param = trans2_bytes_push_str(
5600 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5601 if (tevent_req_nomem(state->param, req)) {
5602 return tevent_req_post(req, ev);
5605 if (clistr_is_previous_version_path(fname) &&
5606 !INFO_LEVEL_IS_UNIX(level)) {
5607 additional_flags2 = FLAGS2_REPARSE_PATH;
5610 subreq = cli_trans_send(
5611 state, /* mem ctx. */
5612 ev, /* event ctx. */
5613 cli, /* cli_state. */
5614 additional_flags2, /* additional_flags2 */
5615 SMBtrans2, /* cmd. */
5616 NULL, /* pipe name. */
5620 state->setup, /* setup. */
5621 1, /* num setup uint16_t words. */
5622 0, /* max returned setup. */
5623 state->param, /* param. */
5624 talloc_get_size(state->param), /* num param. */
5625 2, /* max returned param. */
5628 max_rdata); /* max returned data. */
5630 if (tevent_req_nomem(subreq, req)) {
5631 return tevent_req_post(req, ev);
5633 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5637 static void cli_qpathinfo_done(struct tevent_req *subreq)
5639 struct tevent_req *req = tevent_req_callback_data(
5640 subreq, struct tevent_req);
5641 struct cli_qpathinfo_state *state = tevent_req_data(
5642 req, struct cli_qpathinfo_state);
5645 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5647 &state->rdata, state->min_rdata,
5649 if (tevent_req_nterror(req, status)) {
5652 tevent_req_done(req);
5655 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5656 uint8_t **rdata, uint32_t *num_rdata)
5658 struct cli_qpathinfo_state *state = tevent_req_data(
5659 req, struct cli_qpathinfo_state);
5662 if (tevent_req_is_nterror(req, &status)) {
5665 if (rdata != NULL) {
5666 *rdata = talloc_move(mem_ctx, &state->rdata);
5668 TALLOC_FREE(state->rdata);
5670 if (num_rdata != NULL) {
5671 *num_rdata = state->num_rdata;
5673 return NT_STATUS_OK;
5676 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5677 const char *fname, uint16_t level, uint32_t min_rdata,
5679 uint8_t **rdata, uint32_t *num_rdata)
5681 TALLOC_CTX *frame = talloc_stackframe();
5682 struct tevent_context *ev;
5683 struct tevent_req *req;
5684 NTSTATUS status = NT_STATUS_NO_MEMORY;
5686 if (smbXcli_conn_has_async_calls(cli->conn)) {
5688 * Can't use sync call while an async call is in flight
5690 status = NT_STATUS_INVALID_PARAMETER;
5693 ev = samba_tevent_context_init(frame);
5697 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5702 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5705 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5711 struct cli_qfileinfo_state {
5715 uint16_t recv_flags2;
5721 static void cli_qfileinfo_done(struct tevent_req *subreq);
5723 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5724 struct tevent_context *ev,
5725 struct cli_state *cli, uint16_t fnum,
5726 uint16_t level, uint32_t min_rdata,
5729 struct tevent_req *req, *subreq;
5730 struct cli_qfileinfo_state *state;
5732 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5736 state->min_rdata = min_rdata;
5737 SSVAL(state->param, 0, fnum);
5738 SSVAL(state->param, 2, level);
5739 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5741 subreq = cli_trans_send(
5742 state, /* mem ctx. */
5743 ev, /* event ctx. */
5744 cli, /* cli_state. */
5745 0, /* additional_flags2 */
5746 SMBtrans2, /* cmd. */
5747 NULL, /* pipe name. */
5751 state->setup, /* setup. */
5752 1, /* num setup uint16_t words. */
5753 0, /* max returned setup. */
5754 state->param, /* param. */
5755 sizeof(state->param), /* num param. */
5756 2, /* max returned param. */
5759 max_rdata); /* max returned data. */
5761 if (tevent_req_nomem(subreq, req)) {
5762 return tevent_req_post(req, ev);
5764 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5768 static void cli_qfileinfo_done(struct tevent_req *subreq)
5770 struct tevent_req *req = tevent_req_callback_data(
5771 subreq, struct tevent_req);
5772 struct cli_qfileinfo_state *state = tevent_req_data(
5773 req, struct cli_qfileinfo_state);
5776 status = cli_trans_recv(subreq, state,
5777 &state->recv_flags2,
5780 &state->rdata, state->min_rdata,
5782 if (tevent_req_nterror(req, status)) {
5785 tevent_req_done(req);
5788 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5789 uint16_t *recv_flags2,
5790 uint8_t **rdata, uint32_t *num_rdata)
5792 struct cli_qfileinfo_state *state = tevent_req_data(
5793 req, struct cli_qfileinfo_state);
5796 if (tevent_req_is_nterror(req, &status)) {
5800 if (recv_flags2 != NULL) {
5801 *recv_flags2 = state->recv_flags2;
5803 if (rdata != NULL) {
5804 *rdata = talloc_move(mem_ctx, &state->rdata);
5806 TALLOC_FREE(state->rdata);
5808 if (num_rdata != NULL) {
5809 *num_rdata = state->num_rdata;
5811 return NT_STATUS_OK;
5814 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5815 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5816 uint32_t max_rdata, uint16_t *recv_flags2,
5817 uint8_t **rdata, uint32_t *num_rdata)
5819 TALLOC_CTX *frame = talloc_stackframe();
5820 struct tevent_context *ev;
5821 struct tevent_req *req;
5822 NTSTATUS status = NT_STATUS_NO_MEMORY;
5824 if (smbXcli_conn_has_async_calls(cli->conn)) {
5826 * Can't use sync call while an async call is in flight
5828 status = NT_STATUS_INVALID_PARAMETER;
5831 ev = samba_tevent_context_init(frame);
5835 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5840 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5843 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5849 struct cli_flush_state {
5853 static void cli_flush_done(struct tevent_req *subreq);
5855 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5856 struct tevent_context *ev,
5857 struct cli_state *cli,
5860 struct tevent_req *req, *subreq;
5861 struct cli_flush_state *state;
5863 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5867 SSVAL(state->vwv + 0, 0, fnum);
5869 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5871 if (tevent_req_nomem(subreq, req)) {
5872 return tevent_req_post(req, ev);
5874 tevent_req_set_callback(subreq, cli_flush_done, req);
5878 static void cli_flush_done(struct tevent_req *subreq)
5880 struct tevent_req *req = tevent_req_callback_data(
5881 subreq, struct tevent_req);
5884 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5885 TALLOC_FREE(subreq);
5886 if (tevent_req_nterror(req, status)) {
5889 tevent_req_done(req);
5892 NTSTATUS cli_flush_recv(struct tevent_req *req)
5894 return tevent_req_simple_recv_ntstatus(req);
5897 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5899 TALLOC_CTX *frame = talloc_stackframe();
5900 struct tevent_context *ev;
5901 struct tevent_req *req;
5902 NTSTATUS status = NT_STATUS_NO_MEMORY;
5904 if (smbXcli_conn_has_async_calls(cli->conn)) {
5906 * Can't use sync call while an async call is in flight
5908 status = NT_STATUS_INVALID_PARAMETER;
5911 ev = samba_tevent_context_init(frame);
5915 req = cli_flush_send(frame, ev, cli, fnum);
5919 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5922 status = cli_flush_recv(req);
5928 struct cli_shadow_copy_data_state {
5935 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5937 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5938 struct tevent_context *ev,
5939 struct cli_state *cli,
5943 struct tevent_req *req, *subreq;
5944 struct cli_shadow_copy_data_state *state;
5947 req = tevent_req_create(mem_ctx, &state,
5948 struct cli_shadow_copy_data_state);
5952 state->get_names = get_names;
5953 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5955 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5956 SSVAL(state->setup + 2, 0, fnum);
5957 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5958 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5960 subreq = cli_trans_send(
5961 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5962 state->setup, ARRAY_SIZE(state->setup), 0,
5965 if (tevent_req_nomem(subreq, req)) {
5966 return tevent_req_post(req, ev);
5968 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5972 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5974 struct tevent_req *req = tevent_req_callback_data(
5975 subreq, struct tevent_req);
5976 struct cli_shadow_copy_data_state *state = tevent_req_data(
5977 req, struct cli_shadow_copy_data_state);
5980 status = cli_trans_recv(subreq, state, NULL,
5981 NULL, 0, NULL, /* setup */
5982 NULL, 0, NULL, /* param */
5983 &state->data, 12, &state->num_data);
5984 TALLOC_FREE(subreq);
5985 if (tevent_req_nterror(req, status)) {
5988 tevent_req_done(req);
5991 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5992 char ***pnames, int *pnum_names)
5994 struct cli_shadow_copy_data_state *state = tevent_req_data(
5995 req, struct cli_shadow_copy_data_state);
6001 if (tevent_req_is_nterror(req, &status)) {
6004 num_names = IVAL(state->data, 4);
6005 dlength = IVAL(state->data, 8);
6007 if (!state->get_names) {
6008 *pnum_names = num_names;
6009 return NT_STATUS_OK;
6012 if (dlength+12 > state->num_data) {
6013 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6015 names = talloc_array(mem_ctx, char *, num_names);
6016 if (names == NULL) {
6017 return NT_STATUS_NO_MEMORY;
6020 for (i=0; i<num_names; i++) {
6023 size_t converted_size;
6025 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6026 ret = convert_string_talloc(
6027 names, CH_UTF16LE, CH_UNIX,
6028 src, 2 * sizeof(SHADOW_COPY_LABEL),
6029 &names[i], &converted_size);
6032 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6035 *pnum_names = num_names;
6037 return NT_STATUS_OK;
6040 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6041 uint16_t fnum, bool get_names,
6042 char ***pnames, int *pnum_names)
6044 TALLOC_CTX *frame = talloc_stackframe();
6045 struct tevent_context *ev;
6046 struct tevent_req *req;
6047 NTSTATUS status = NT_STATUS_NO_MEMORY;
6049 if (smbXcli_conn_has_async_calls(cli->conn)) {
6051 * Can't use sync call while an async call is in flight
6053 status = NT_STATUS_INVALID_PARAMETER;
6056 ev = samba_tevent_context_init(frame);
6060 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6064 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6067 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);