2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
41 size_t *pconverted_size)
45 size_t converted_size;
51 buflen = talloc_get_size(buf);
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
77 TALLOC_FREE(converted);
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
155 memcpy(&buf[buflen], bytes, num_bytes);
159 struct cli_setpathinfo_state {
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
186 /* Setup param array. */
187 state->param = talloc_zero_array(state, uint8_t, 6);
188 if (tevent_req_nomem(state->param, req)) {
189 return tevent_req_post(req, ev);
191 SSVAL(state->param, 0, level);
193 state->param = trans2_bytes_push_str(
194 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195 if (tevent_req_nomem(state->param, req)) {
196 return tevent_req_post(req, ev);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
202 cli, /* cli_state. */
203 0, /* additional_flags2 */
204 SMBtrans2, /* cmd. */
205 NULL, /* pipe name. */
209 &state->setup, /* setup. */
210 1, /* num setup uint16_t words. */
211 0, /* max returned setup. */
212 state->param, /* param. */
213 talloc_get_size(state->param), /* num param. */
214 2, /* max returned param. */
216 data_len, /* num data. */
217 0); /* max returned data. */
219 if (tevent_req_nomem(subreq, req)) {
220 return tevent_req_post(req, ev);
222 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
226 static void cli_setpathinfo_done(struct tevent_req *subreq)
228 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
229 NULL, 0, NULL, NULL, 0, NULL);
230 tevent_req_simple_finish_ntstatus(subreq, status);
233 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
235 return tevent_req_simple_recv_ntstatus(req);
238 NTSTATUS cli_setpathinfo(struct cli_state *cli,
244 TALLOC_CTX *frame = talloc_stackframe();
245 struct tevent_context *ev;
246 struct tevent_req *req;
247 NTSTATUS status = NT_STATUS_NO_MEMORY;
249 if (smbXcli_conn_has_async_calls(cli->conn)) {
251 * Can't use sync call while an async call is in flight
253 status = NT_STATUS_INVALID_PARAMETER;
256 ev = samba_tevent_context_init(frame);
260 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
264 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
267 status = cli_setpathinfo_recv(req);
273 /****************************************************************************
274 Hard/Symlink a file (UNIX extensions).
275 Creates new name (sym)linked to oldname.
276 ****************************************************************************/
278 struct cli_posix_link_internal_state {
282 static void cli_posix_link_internal_done(struct tevent_req *subreq);
284 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
285 struct tevent_context *ev,
286 struct cli_state *cli,
291 struct tevent_req *req = NULL, *subreq = NULL;
292 struct cli_posix_link_internal_state *state = NULL;
294 req = tevent_req_create(mem_ctx, &state,
295 struct cli_posix_link_internal_state);
300 /* Setup data array. */
301 state->data = talloc_array(state, uint8_t, 0);
302 if (tevent_req_nomem(state->data, req)) {
303 return tevent_req_post(req, ev);
305 state->data = trans2_bytes_push_str(
306 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
308 subreq = cli_setpathinfo_send(
309 state, ev, cli, level, newname,
310 state->data, talloc_get_size(state->data));
311 if (tevent_req_nomem(subreq, req)) {
312 return tevent_req_post(req, ev);
314 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
318 static void cli_posix_link_internal_done(struct tevent_req *subreq)
320 NTSTATUS status = cli_setpathinfo_recv(subreq);
321 tevent_req_simple_finish_ntstatus(subreq, status);
324 /****************************************************************************
325 Symlink a file (UNIX extensions).
326 ****************************************************************************/
328 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
329 struct tevent_context *ev,
330 struct cli_state *cli,
334 return cli_posix_link_internal_send(
335 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
338 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
340 return tevent_req_simple_recv_ntstatus(req);
343 NTSTATUS cli_posix_symlink(struct cli_state *cli,
347 TALLOC_CTX *frame = talloc_stackframe();
348 struct tevent_context *ev = NULL;
349 struct tevent_req *req = NULL;
350 NTSTATUS status = NT_STATUS_OK;
352 if (smbXcli_conn_has_async_calls(cli->conn)) {
354 * Can't use sync call while an async call is in flight
356 status = NT_STATUS_INVALID_PARAMETER;
360 ev = samba_tevent_context_init(frame);
362 status = NT_STATUS_NO_MEMORY;
366 req = cli_posix_symlink_send(frame,
372 status = NT_STATUS_NO_MEMORY;
376 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
380 status = cli_posix_symlink_recv(req);
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state {
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399 struct tevent_context *ev,
400 struct cli_state *cli,
404 struct tevent_req *req = NULL, *subreq = NULL;
405 struct readlink_state *state = NULL;
406 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
408 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len < len) || (maxbytelen < len)) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return tevent_req_post(req, ev);
421 subreq = cli_qpathinfo_send(state, ev, cli, fname,
422 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423 if (tevent_req_nomem(subreq, req)) {
424 return tevent_req_post(req, ev);
426 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct readlink_state *state = tevent_req_data(
435 req, struct readlink_state);
438 status = cli_qpathinfo_recv(subreq, state, &state->data,
441 if (tevent_req_nterror(req, status)) {
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state->data[state->num_data-1] != '\0') {
448 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
451 tevent_req_done(req);
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455 char *retpath, size_t len)
458 char *converted = NULL;
459 size_t converted_size = 0;
460 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
462 if (tevent_req_is_nterror(req, &status)) {
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state,
467 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
473 return NT_STATUS_NO_MEMORY;
476 len = MIN(len,converted_size);
478 return NT_STATUS_DATA_ERROR;
480 memcpy(retpath, converted, len);
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485 char *linkpath, size_t len)
487 TALLOC_CTX *frame = talloc_stackframe();
488 struct tevent_context *ev = NULL;
489 struct tevent_req *req = NULL;
490 NTSTATUS status = NT_STATUS_OK;
492 if (smbXcli_conn_has_async_calls(cli->conn)) {
494 * Can't use sync call while an async call is in flight
496 status = NT_STATUS_INVALID_PARAMETER;
500 ev = samba_tevent_context_init(frame);
502 status = NT_STATUS_NO_MEMORY;
506 req = cli_posix_readlink_send(frame,
512 status = NT_STATUS_NO_MEMORY;
516 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
520 status = cli_posix_readlink_recv(req, cli, linkpath, len);
527 /****************************************************************************
528 Hard link a file (UNIX extensions).
529 ****************************************************************************/
531 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
532 struct tevent_context *ev,
533 struct cli_state *cli,
537 return cli_posix_link_internal_send(
538 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
541 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
543 return tevent_req_simple_recv_ntstatus(req);
546 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
550 TALLOC_CTX *frame = talloc_stackframe();
551 struct tevent_context *ev = NULL;
552 struct tevent_req *req = NULL;
553 NTSTATUS status = NT_STATUS_OK;
555 if (smbXcli_conn_has_async_calls(cli->conn)) {
557 * Can't use sync call while an async call is in flight
559 status = NT_STATUS_INVALID_PARAMETER;
563 ev = samba_tevent_context_init(frame);
565 status = NT_STATUS_NO_MEMORY;
569 req = cli_posix_hardlink_send(frame,
575 status = NT_STATUS_NO_MEMORY;
579 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
583 status = cli_posix_hardlink_recv(req);
590 /****************************************************************************
591 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
592 ****************************************************************************/
594 struct getacl_state {
599 static void cli_posix_getacl_done(struct tevent_req *subreq);
601 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
602 struct tevent_context *ev,
603 struct cli_state *cli,
606 struct tevent_req *req = NULL, *subreq = NULL;
607 struct getacl_state *state = NULL;
609 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
613 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
615 if (tevent_req_nomem(subreq, req)) {
616 return tevent_req_post(req, ev);
618 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
622 static void cli_posix_getacl_done(struct tevent_req *subreq)
624 struct tevent_req *req = tevent_req_callback_data(
625 subreq, struct tevent_req);
626 struct getacl_state *state = tevent_req_data(
627 req, struct getacl_state);
630 status = cli_qpathinfo_recv(subreq, state, &state->data,
633 if (tevent_req_nterror(req, status)) {
636 tevent_req_done(req);
639 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
644 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
647 if (tevent_req_is_nterror(req, &status)) {
650 *prb_size = (size_t)state->num_data;
651 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
655 NTSTATUS cli_posix_getacl(struct cli_state *cli,
661 TALLOC_CTX *frame = talloc_stackframe();
662 struct tevent_context *ev = NULL;
663 struct tevent_req *req = NULL;
664 NTSTATUS status = NT_STATUS_OK;
666 if (smbXcli_conn_has_async_calls(cli->conn)) {
668 * Can't use sync call while an async call is in flight
670 status = NT_STATUS_INVALID_PARAMETER;
674 ev = samba_tevent_context_init(frame);
676 status = NT_STATUS_NO_MEMORY;
680 req = cli_posix_getacl_send(frame,
685 status = NT_STATUS_NO_MEMORY;
689 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
693 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
700 /****************************************************************************
701 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
702 ****************************************************************************/
704 struct setacl_state {
708 static void cli_posix_setacl_done(struct tevent_req *subreq);
710 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
711 struct tevent_context *ev,
712 struct cli_state *cli,
717 struct tevent_req *req = NULL, *subreq = NULL;
718 struct setacl_state *state = NULL;
720 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
724 state->data = talloc_memdup(state, data, num_data);
725 if (tevent_req_nomem(state->data, req)) {
726 return tevent_req_post(req, ev);
729 subreq = cli_setpathinfo_send(state,
736 if (tevent_req_nomem(subreq, req)) {
737 return tevent_req_post(req, ev);
739 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
743 static void cli_posix_setacl_done(struct tevent_req *subreq)
745 NTSTATUS status = cli_setpathinfo_recv(subreq);
746 tevent_req_simple_finish_ntstatus(subreq, status);
749 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
751 return tevent_req_simple_recv_ntstatus(req);
754 NTSTATUS cli_posix_setacl(struct cli_state *cli,
759 TALLOC_CTX *frame = talloc_stackframe();
760 struct tevent_context *ev = NULL;
761 struct tevent_req *req = NULL;
762 NTSTATUS status = NT_STATUS_OK;
764 if (smbXcli_conn_has_async_calls(cli->conn)) {
766 * Can't use sync call while an async call is in flight
768 status = NT_STATUS_INVALID_PARAMETER;
772 ev = samba_tevent_context_init(frame);
774 status = NT_STATUS_NO_MEMORY;
778 req = cli_posix_setacl_send(frame,
785 status = NT_STATUS_NO_MEMORY;
789 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
793 status = cli_posix_setacl_recv(req);
800 /****************************************************************************
801 Stat a file (UNIX extensions).
802 ****************************************************************************/
809 static void cli_posix_stat_done(struct tevent_req *subreq);
811 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
812 struct tevent_context *ev,
813 struct cli_state *cli,
816 struct tevent_req *req = NULL, *subreq = NULL;
817 struct stat_state *state = NULL;
819 req = tevent_req_create(mem_ctx, &state, struct stat_state);
823 subreq = cli_qpathinfo_send(state, ev, cli, fname,
824 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
825 if (tevent_req_nomem(subreq, req)) {
826 return tevent_req_post(req, ev);
828 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
832 static void cli_posix_stat_done(struct tevent_req *subreq)
834 struct tevent_req *req = tevent_req_callback_data(
835 subreq, struct tevent_req);
836 struct stat_state *state = tevent_req_data(req, struct stat_state);
839 status = cli_qpathinfo_recv(subreq, state, &state->data,
842 if (tevent_req_nterror(req, status)) {
845 tevent_req_done(req);
848 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
849 SMB_STRUCT_STAT *sbuf)
851 struct stat_state *state = tevent_req_data(req, struct stat_state);
854 if (tevent_req_is_nterror(req, &status)) {
858 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
859 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
860 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
861 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
863 /* assume 512 byte blocks */
864 sbuf->st_ex_blocks /= 512;
866 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
867 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
868 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
870 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
871 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
872 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
873 #if defined(HAVE_MAKEDEV)
875 uint32_t dev_major = IVAL(state->data,60);
876 uint32_t dev_minor = IVAL(state->data,68);
877 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
880 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
881 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
882 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
887 NTSTATUS cli_posix_stat(struct cli_state *cli,
889 SMB_STRUCT_STAT *sbuf)
891 TALLOC_CTX *frame = talloc_stackframe();
892 struct tevent_context *ev = NULL;
893 struct tevent_req *req = NULL;
894 NTSTATUS status = NT_STATUS_OK;
896 if (smbXcli_conn_has_async_calls(cli->conn)) {
898 * Can't use sync call while an async call is in flight
900 status = NT_STATUS_INVALID_PARAMETER;
904 ev = samba_tevent_context_init(frame);
906 status = NT_STATUS_NO_MEMORY;
910 req = cli_posix_stat_send(frame,
915 status = NT_STATUS_NO_MEMORY;
919 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
923 status = cli_posix_stat_recv(req, sbuf);
930 /****************************************************************************
931 Chmod or chown a file internal (UNIX extensions).
932 ****************************************************************************/
934 struct cli_posix_chown_chmod_internal_state {
938 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
940 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
941 struct tevent_context *ev,
942 struct cli_state *cli,
948 struct tevent_req *req = NULL, *subreq = NULL;
949 struct cli_posix_chown_chmod_internal_state *state = NULL;
951 req = tevent_req_create(mem_ctx, &state,
952 struct cli_posix_chown_chmod_internal_state);
957 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
958 memset(&state->data[40], '\0', 60);
959 SIVAL(state->data,40,uid);
960 SIVAL(state->data,48,gid);
961 SIVAL(state->data,84,mode);
963 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
964 fname, state->data, sizeof(state->data));
965 if (tevent_req_nomem(subreq, req)) {
966 return tevent_req_post(req, ev);
968 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
973 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
975 NTSTATUS status = cli_setpathinfo_recv(subreq);
976 tevent_req_simple_finish_ntstatus(subreq, status);
979 /****************************************************************************
980 chmod a file (UNIX extensions).
981 ****************************************************************************/
983 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
984 struct tevent_context *ev,
985 struct cli_state *cli,
989 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
991 unix_perms_to_wire(mode),
996 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
998 return tevent_req_simple_recv_ntstatus(req);
1001 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1003 TALLOC_CTX *frame = talloc_stackframe();
1004 struct tevent_context *ev = NULL;
1005 struct tevent_req *req = NULL;
1006 NTSTATUS status = NT_STATUS_OK;
1008 if (smbXcli_conn_has_async_calls(cli->conn)) {
1010 * Can't use sync call while an async call is in flight
1012 status = NT_STATUS_INVALID_PARAMETER;
1016 ev = samba_tevent_context_init(frame);
1018 status = NT_STATUS_NO_MEMORY;
1022 req = cli_posix_chmod_send(frame,
1028 status = NT_STATUS_NO_MEMORY;
1032 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1036 status = cli_posix_chmod_recv(req);
1043 /****************************************************************************
1044 chown a file (UNIX extensions).
1045 ****************************************************************************/
1047 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1048 struct tevent_context *ev,
1049 struct cli_state *cli,
1054 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1061 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1063 return tevent_req_simple_recv_ntstatus(req);
1066 NTSTATUS cli_posix_chown(struct cli_state *cli,
1071 TALLOC_CTX *frame = talloc_stackframe();
1072 struct tevent_context *ev = NULL;
1073 struct tevent_req *req = NULL;
1074 NTSTATUS status = NT_STATUS_OK;
1076 if (smbXcli_conn_has_async_calls(cli->conn)) {
1078 * Can't use sync call while an async call is in flight
1080 status = NT_STATUS_INVALID_PARAMETER;
1084 ev = samba_tevent_context_init(frame);
1086 status = NT_STATUS_NO_MEMORY;
1090 req = cli_posix_chown_send(frame,
1097 status = NT_STATUS_NO_MEMORY;
1101 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1105 status = cli_posix_chown_recv(req);
1112 /****************************************************************************
1114 ****************************************************************************/
1116 static void cli_rename_done(struct tevent_req *subreq);
1118 struct cli_rename_state {
1122 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1123 struct tevent_context *ev,
1124 struct cli_state *cli,
1125 const char *fname_src,
1126 const char *fname_dst)
1128 struct tevent_req *req = NULL, *subreq = NULL;
1129 struct cli_rename_state *state = NULL;
1130 uint8_t additional_flags = 0;
1131 uint8_t *bytes = NULL;
1133 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1138 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1140 bytes = talloc_array(state, uint8_t, 1);
1141 if (tevent_req_nomem(bytes, req)) {
1142 return tevent_req_post(req, ev);
1145 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1146 strlen(fname_src)+1, NULL);
1147 if (tevent_req_nomem(bytes, req)) {
1148 return tevent_req_post(req, ev);
1151 bytes = talloc_realloc(state, bytes, uint8_t,
1152 talloc_get_size(bytes)+1);
1153 if (tevent_req_nomem(bytes, req)) {
1154 return tevent_req_post(req, ev);
1157 bytes[talloc_get_size(bytes)-1] = 4;
1158 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1159 strlen(fname_dst)+1, NULL);
1160 if (tevent_req_nomem(bytes, req)) {
1161 return tevent_req_post(req, ev);
1164 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags, 0,
1165 1, state->vwv, talloc_get_size(bytes), bytes);
1166 if (tevent_req_nomem(subreq, req)) {
1167 return tevent_req_post(req, ev);
1169 tevent_req_set_callback(subreq, cli_rename_done, req);
1173 static void cli_rename_done(struct tevent_req *subreq)
1175 struct tevent_req *req = tevent_req_callback_data(
1176 subreq, struct tevent_req);
1179 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1180 TALLOC_FREE(subreq);
1181 if (tevent_req_nterror(req, status)) {
1184 tevent_req_done(req);
1187 NTSTATUS cli_rename_recv(struct tevent_req *req)
1189 return tevent_req_simple_recv_ntstatus(req);
1192 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1194 TALLOC_CTX *frame = NULL;
1195 struct tevent_context *ev;
1196 struct tevent_req *req;
1197 NTSTATUS status = NT_STATUS_OK;
1199 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1200 return cli_smb2_rename(cli,
1205 frame = talloc_stackframe();
1207 if (smbXcli_conn_has_async_calls(cli->conn)) {
1209 * Can't use sync call while an async call is in flight
1211 status = NT_STATUS_INVALID_PARAMETER;
1215 ev = samba_tevent_context_init(frame);
1217 status = NT_STATUS_NO_MEMORY;
1221 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1223 status = NT_STATUS_NO_MEMORY;
1227 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1231 status = cli_rename_recv(req);
1238 /****************************************************************************
1240 ****************************************************************************/
1242 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1244 struct cli_ntrename_internal_state {
1248 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1249 struct tevent_context *ev,
1250 struct cli_state *cli,
1251 const char *fname_src,
1252 const char *fname_dst,
1253 uint16_t rename_flag)
1255 struct tevent_req *req = NULL, *subreq = NULL;
1256 struct cli_ntrename_internal_state *state = NULL;
1257 uint8_t additional_flags = 0;
1258 uint8_t *bytes = NULL;
1260 req = tevent_req_create(mem_ctx, &state,
1261 struct cli_ntrename_internal_state);
1266 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1267 SSVAL(state->vwv+1, 0, rename_flag);
1269 bytes = talloc_array(state, uint8_t, 1);
1270 if (tevent_req_nomem(bytes, req)) {
1271 return tevent_req_post(req, ev);
1274 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1275 strlen(fname_src)+1, NULL);
1276 if (tevent_req_nomem(bytes, req)) {
1277 return tevent_req_post(req, ev);
1280 bytes = talloc_realloc(state, bytes, uint8_t,
1281 talloc_get_size(bytes)+1);
1282 if (tevent_req_nomem(bytes, req)) {
1283 return tevent_req_post(req, ev);
1286 bytes[talloc_get_size(bytes)-1] = 4;
1287 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1288 strlen(fname_dst)+1, NULL);
1289 if (tevent_req_nomem(bytes, req)) {
1290 return tevent_req_post(req, ev);
1293 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags, 0,
1294 4, state->vwv, talloc_get_size(bytes), bytes);
1295 if (tevent_req_nomem(subreq, req)) {
1296 return tevent_req_post(req, ev);
1298 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1302 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1304 struct tevent_req *req = tevent_req_callback_data(
1305 subreq, struct tevent_req);
1308 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1309 TALLOC_FREE(subreq);
1310 if (tevent_req_nterror(req, status)) {
1313 tevent_req_done(req);
1316 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1318 return tevent_req_simple_recv_ntstatus(req);
1321 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1322 struct tevent_context *ev,
1323 struct cli_state *cli,
1324 const char *fname_src,
1325 const char *fname_dst)
1327 return cli_ntrename_internal_send(mem_ctx,
1332 RENAME_FLAG_RENAME);
1335 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1337 return cli_ntrename_internal_recv(req);
1340 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1342 TALLOC_CTX *frame = talloc_stackframe();
1343 struct tevent_context *ev;
1344 struct tevent_req *req;
1345 NTSTATUS status = NT_STATUS_OK;
1347 if (smbXcli_conn_has_async_calls(cli->conn)) {
1349 * Can't use sync call while an async call is in flight
1351 status = NT_STATUS_INVALID_PARAMETER;
1355 ev = samba_tevent_context_init(frame);
1357 status = NT_STATUS_NO_MEMORY;
1361 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1363 status = NT_STATUS_NO_MEMORY;
1367 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1371 status = cli_ntrename_recv(req);
1378 /****************************************************************************
1380 ****************************************************************************/
1382 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1383 struct tevent_context *ev,
1384 struct cli_state *cli,
1385 const char *fname_src,
1386 const char *fname_dst)
1388 return cli_ntrename_internal_send(mem_ctx,
1393 RENAME_FLAG_HARD_LINK);
1396 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1398 return cli_ntrename_internal_recv(req);
1401 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1403 TALLOC_CTX *frame = talloc_stackframe();
1404 struct tevent_context *ev;
1405 struct tevent_req *req;
1406 NTSTATUS status = NT_STATUS_OK;
1408 if (smbXcli_conn_has_async_calls(cli->conn)) {
1410 * Can't use sync call while an async call is in flight
1412 status = NT_STATUS_INVALID_PARAMETER;
1416 ev = samba_tevent_context_init(frame);
1418 status = NT_STATUS_NO_MEMORY;
1422 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1424 status = NT_STATUS_NO_MEMORY;
1428 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1432 status = cli_nt_hardlink_recv(req);
1439 /****************************************************************************
1441 ****************************************************************************/
1443 static void cli_unlink_done(struct tevent_req *subreq);
1445 struct cli_unlink_state {
1449 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1450 struct tevent_context *ev,
1451 struct cli_state *cli,
1453 uint16_t mayhave_attrs)
1455 struct tevent_req *req = NULL, *subreq = NULL;
1456 struct cli_unlink_state *state = NULL;
1457 uint8_t additional_flags = 0;
1458 uint8_t *bytes = NULL;
1460 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1465 SSVAL(state->vwv+0, 0, mayhave_attrs);
1467 bytes = talloc_array(state, uint8_t, 1);
1468 if (tevent_req_nomem(bytes, req)) {
1469 return tevent_req_post(req, ev);
1472 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1473 strlen(fname)+1, NULL);
1475 if (tevent_req_nomem(bytes, req)) {
1476 return tevent_req_post(req, ev);
1479 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags, 0,
1480 1, state->vwv, talloc_get_size(bytes), bytes);
1481 if (tevent_req_nomem(subreq, req)) {
1482 return tevent_req_post(req, ev);
1484 tevent_req_set_callback(subreq, cli_unlink_done, req);
1488 static void cli_unlink_done(struct tevent_req *subreq)
1490 struct tevent_req *req = tevent_req_callback_data(
1491 subreq, struct tevent_req);
1494 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1495 TALLOC_FREE(subreq);
1496 if (tevent_req_nterror(req, status)) {
1499 tevent_req_done(req);
1502 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1504 return tevent_req_simple_recv_ntstatus(req);
1507 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1509 TALLOC_CTX *frame = NULL;
1510 struct tevent_context *ev;
1511 struct tevent_req *req;
1512 NTSTATUS status = NT_STATUS_OK;
1514 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1515 return cli_smb2_unlink(cli, fname);
1518 frame = talloc_stackframe();
1520 if (smbXcli_conn_has_async_calls(cli->conn)) {
1522 * Can't use sync call while an async call is in flight
1524 status = NT_STATUS_INVALID_PARAMETER;
1528 ev = samba_tevent_context_init(frame);
1530 status = NT_STATUS_NO_MEMORY;
1534 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1536 status = NT_STATUS_NO_MEMORY;
1540 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1544 status = cli_unlink_recv(req);
1551 /****************************************************************************
1553 ****************************************************************************/
1555 static void cli_mkdir_done(struct tevent_req *subreq);
1557 struct cli_mkdir_state {
1561 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1562 struct tevent_context *ev,
1563 struct cli_state *cli,
1566 struct tevent_req *req = NULL, *subreq = NULL;
1567 struct cli_mkdir_state *state = NULL;
1568 uint8_t additional_flags = 0;
1569 uint8_t *bytes = NULL;
1571 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1576 bytes = talloc_array(state, uint8_t, 1);
1577 if (tevent_req_nomem(bytes, req)) {
1578 return tevent_req_post(req, ev);
1581 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1582 strlen(dname)+1, NULL);
1584 if (tevent_req_nomem(bytes, req)) {
1585 return tevent_req_post(req, ev);
1588 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags, 0,
1589 0, NULL, talloc_get_size(bytes), bytes);
1590 if (tevent_req_nomem(subreq, req)) {
1591 return tevent_req_post(req, ev);
1593 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1597 static void cli_mkdir_done(struct tevent_req *subreq)
1599 struct tevent_req *req = tevent_req_callback_data(
1600 subreq, struct tevent_req);
1603 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1604 TALLOC_FREE(subreq);
1605 if (tevent_req_nterror(req, status)) {
1608 tevent_req_done(req);
1611 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1613 return tevent_req_simple_recv_ntstatus(req);
1616 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1618 TALLOC_CTX *frame = NULL;
1619 struct tevent_context *ev;
1620 struct tevent_req *req;
1621 NTSTATUS status = NT_STATUS_OK;
1623 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1624 return cli_smb2_mkdir(cli, dname);
1627 frame = talloc_stackframe();
1629 if (smbXcli_conn_has_async_calls(cli->conn)) {
1631 * Can't use sync call while an async call is in flight
1633 status = NT_STATUS_INVALID_PARAMETER;
1637 ev = samba_tevent_context_init(frame);
1639 status = NT_STATUS_NO_MEMORY;
1643 req = cli_mkdir_send(frame, ev, cli, dname);
1645 status = NT_STATUS_NO_MEMORY;
1649 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1653 status = cli_mkdir_recv(req);
1660 /****************************************************************************
1662 ****************************************************************************/
1664 static void cli_rmdir_done(struct tevent_req *subreq);
1666 struct cli_rmdir_state {
1670 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1671 struct tevent_context *ev,
1672 struct cli_state *cli,
1675 struct tevent_req *req = NULL, *subreq = NULL;
1676 struct cli_rmdir_state *state = NULL;
1677 uint8_t additional_flags = 0;
1678 uint8_t *bytes = NULL;
1680 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1685 bytes = talloc_array(state, uint8_t, 1);
1686 if (tevent_req_nomem(bytes, req)) {
1687 return tevent_req_post(req, ev);
1690 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1691 strlen(dname)+1, NULL);
1693 if (tevent_req_nomem(bytes, req)) {
1694 return tevent_req_post(req, ev);
1697 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags, 0,
1698 0, NULL, talloc_get_size(bytes), bytes);
1699 if (tevent_req_nomem(subreq, req)) {
1700 return tevent_req_post(req, ev);
1702 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1706 static void cli_rmdir_done(struct tevent_req *subreq)
1708 struct tevent_req *req = tevent_req_callback_data(
1709 subreq, struct tevent_req);
1712 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1713 TALLOC_FREE(subreq);
1714 if (tevent_req_nterror(req, status)) {
1717 tevent_req_done(req);
1720 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1722 return tevent_req_simple_recv_ntstatus(req);
1725 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1727 TALLOC_CTX *frame = NULL;
1728 struct tevent_context *ev;
1729 struct tevent_req *req;
1730 NTSTATUS status = NT_STATUS_OK;
1732 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1733 return cli_smb2_rmdir(cli, dname);
1736 frame = talloc_stackframe();
1738 if (smbXcli_conn_has_async_calls(cli->conn)) {
1740 * Can't use sync call while an async call is in flight
1742 status = NT_STATUS_INVALID_PARAMETER;
1746 ev = samba_tevent_context_init(frame);
1748 status = NT_STATUS_NO_MEMORY;
1752 req = cli_rmdir_send(frame, ev, cli, dname);
1754 status = NT_STATUS_NO_MEMORY;
1758 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1762 status = cli_rmdir_recv(req);
1769 /****************************************************************************
1770 Set or clear the delete on close flag.
1771 ****************************************************************************/
1779 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1781 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1782 NULL, 0, NULL, NULL, 0, NULL);
1783 tevent_req_simple_finish_ntstatus(subreq, status);
1786 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1787 struct tevent_context *ev,
1788 struct cli_state *cli,
1792 struct tevent_req *req = NULL, *subreq = NULL;
1793 struct doc_state *state = NULL;
1795 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1800 /* Setup setup word. */
1801 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1803 /* Setup param array. */
1804 SSVAL(state->param,0,fnum);
1805 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1807 /* Setup data array. */
1808 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1810 subreq = cli_trans_send(state, /* mem ctx. */
1811 ev, /* event ctx. */
1812 cli, /* cli_state. */
1813 0, /* additional_flags2 */
1814 SMBtrans2, /* cmd. */
1815 NULL, /* pipe name. */
1819 &state->setup, /* setup. */
1820 1, /* num setup uint16_t words. */
1821 0, /* max returned setup. */
1822 state->param, /* param. */
1824 2, /* max returned param. */
1825 state->data, /* data. */
1827 0); /* max returned data. */
1829 if (tevent_req_nomem(subreq, req)) {
1830 return tevent_req_post(req, ev);
1832 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1836 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1838 return tevent_req_simple_recv_ntstatus(req);
1841 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1843 TALLOC_CTX *frame = talloc_stackframe();
1844 struct tevent_context *ev = NULL;
1845 struct tevent_req *req = NULL;
1846 NTSTATUS status = NT_STATUS_OK;
1848 if (smbXcli_conn_has_async_calls(cli->conn)) {
1850 * Can't use sync call while an async call is in flight
1852 status = NT_STATUS_INVALID_PARAMETER;
1856 ev = samba_tevent_context_init(frame);
1858 status = NT_STATUS_NO_MEMORY;
1862 req = cli_nt_delete_on_close_send(frame,
1868 status = NT_STATUS_NO_MEMORY;
1872 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1876 status = cli_nt_delete_on_close_recv(req);
1883 struct cli_ntcreate1_state {
1886 struct smb_create_returns cr;
1887 struct tevent_req *subreq;
1890 static void cli_ntcreate1_done(struct tevent_req *subreq);
1891 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1893 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1894 struct tevent_context *ev,
1895 struct cli_state *cli,
1897 uint32_t CreatFlags,
1898 uint32_t DesiredAccess,
1899 uint32_t FileAttributes,
1900 uint32_t ShareAccess,
1901 uint32_t CreateDisposition,
1902 uint32_t CreateOptions,
1903 uint8_t SecurityFlags)
1905 struct tevent_req *req, *subreq;
1906 struct cli_ntcreate1_state *state;
1909 size_t converted_len;
1911 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1918 SCVAL(vwv+0, 0, 0xFF);
1923 if (cli->use_oplocks) {
1924 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1926 SIVAL(vwv+3, 1, CreatFlags);
1927 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1928 SIVAL(vwv+7, 1, DesiredAccess);
1929 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1930 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1931 SIVAL(vwv+13, 1, FileAttributes);
1932 SIVAL(vwv+15, 1, ShareAccess);
1933 SIVAL(vwv+17, 1, CreateDisposition);
1934 SIVAL(vwv+19, 1, CreateOptions |
1935 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1936 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1937 SCVAL(vwv+23, 1, SecurityFlags);
1939 bytes = talloc_array(state, uint8_t, 0);
1940 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1941 fname, strlen(fname)+1,
1944 /* sigh. this copes with broken netapp filer behaviour */
1945 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1947 if (tevent_req_nomem(bytes, req)) {
1948 return tevent_req_post(req, ev);
1951 SSVAL(vwv+2, 1, converted_len);
1953 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 0, 24, vwv,
1954 talloc_get_size(bytes), bytes);
1955 if (tevent_req_nomem(subreq, req)) {
1956 return tevent_req_post(req, ev);
1958 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1960 state->subreq = subreq;
1961 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
1966 static void cli_ntcreate1_done(struct tevent_req *subreq)
1968 struct tevent_req *req = tevent_req_callback_data(
1969 subreq, struct tevent_req);
1970 struct cli_ntcreate1_state *state = tevent_req_data(
1971 req, struct cli_ntcreate1_state);
1978 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1979 &num_bytes, &bytes);
1980 TALLOC_FREE(subreq);
1981 if (tevent_req_nterror(req, status)) {
1984 state->cr.oplock_level = CVAL(vwv+2, 0);
1985 state->fnum = SVAL(vwv+2, 1);
1986 state->cr.create_action = IVAL(vwv+3, 1);
1987 state->cr.creation_time = BVAL(vwv+5, 1);
1988 state->cr.last_access_time = BVAL(vwv+9, 1);
1989 state->cr.last_write_time = BVAL(vwv+13, 1);
1990 state->cr.change_time = BVAL(vwv+17, 1);
1991 state->cr.file_attributes = IVAL(vwv+21, 1);
1992 state->cr.allocation_size = BVAL(vwv+23, 1);
1993 state->cr.end_of_file = BVAL(vwv+27, 1);
1995 tevent_req_done(req);
1998 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2000 struct cli_ntcreate1_state *state = tevent_req_data(
2001 req, struct cli_ntcreate1_state);
2002 return tevent_req_cancel(state->subreq);
2005 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2007 struct smb_create_returns *cr)
2009 struct cli_ntcreate1_state *state = tevent_req_data(
2010 req, struct cli_ntcreate1_state);
2013 if (tevent_req_is_nterror(req, &status)) {
2016 *pfnum = state->fnum;
2020 return NT_STATUS_OK;
2023 struct cli_ntcreate_state {
2024 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2025 struct smb_create_returns *cr);
2026 struct smb_create_returns cr;
2028 struct tevent_req *subreq;
2031 static void cli_ntcreate_done(struct tevent_req *subreq);
2032 static bool cli_ntcreate_cancel(struct tevent_req *req);
2034 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2035 struct tevent_context *ev,
2036 struct cli_state *cli,
2038 uint32_t create_flags,
2039 uint32_t desired_access,
2040 uint32_t file_attributes,
2041 uint32_t share_access,
2042 uint32_t create_disposition,
2043 uint32_t create_options,
2044 uint8_t security_flags)
2046 struct tevent_req *req, *subreq;
2047 struct cli_ntcreate_state *state;
2049 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2054 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2055 state->recv = cli_smb2_create_fnum_recv;
2057 if (cli->use_oplocks) {
2058 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2061 subreq = cli_smb2_create_fnum_send(
2062 state, ev, cli, fname, create_flags, desired_access,
2063 file_attributes, share_access, create_disposition,
2066 state->recv = cli_ntcreate1_recv;
2067 subreq = cli_ntcreate1_send(
2068 state, ev, cli, fname, create_flags, desired_access,
2069 file_attributes, share_access, create_disposition,
2070 create_options, security_flags);
2072 if (tevent_req_nomem(subreq, req)) {
2073 return tevent_req_post(req, ev);
2075 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2077 state->subreq = subreq;
2078 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2083 static void cli_ntcreate_done(struct tevent_req *subreq)
2085 struct tevent_req *req = tevent_req_callback_data(
2086 subreq, struct tevent_req);
2087 struct cli_ntcreate_state *state = tevent_req_data(
2088 req, struct cli_ntcreate_state);
2091 status = state->recv(subreq, &state->fnum, &state->cr);
2092 TALLOC_FREE(subreq);
2093 if (tevent_req_nterror(req, status)) {
2096 tevent_req_done(req);
2099 static bool cli_ntcreate_cancel(struct tevent_req *req)
2101 struct cli_ntcreate_state *state = tevent_req_data(
2102 req, struct cli_ntcreate_state);
2103 return tevent_req_cancel(state->subreq);
2106 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2107 struct smb_create_returns *cr)
2109 struct cli_ntcreate_state *state = tevent_req_data(
2110 req, struct cli_ntcreate_state);
2113 if (tevent_req_is_nterror(req, &status)) {
2117 *fnum = state->fnum;
2122 return NT_STATUS_OK;
2125 NTSTATUS cli_ntcreate(struct cli_state *cli,
2127 uint32_t CreatFlags,
2128 uint32_t DesiredAccess,
2129 uint32_t FileAttributes,
2130 uint32_t ShareAccess,
2131 uint32_t CreateDisposition,
2132 uint32_t CreateOptions,
2133 uint8_t SecurityFlags,
2135 struct smb_create_returns *cr)
2137 TALLOC_CTX *frame = talloc_stackframe();
2138 struct tevent_context *ev;
2139 struct tevent_req *req;
2140 NTSTATUS status = NT_STATUS_NO_MEMORY;
2142 if (smbXcli_conn_has_async_calls(cli->conn)) {
2144 * Can't use sync call while an async call is in flight
2146 status = NT_STATUS_INVALID_PARAMETER;
2150 ev = samba_tevent_context_init(frame);
2155 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2156 DesiredAccess, FileAttributes, ShareAccess,
2157 CreateDisposition, CreateOptions,
2163 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2167 status = cli_ntcreate_recv(req, pfid, cr);
2173 struct cli_nttrans_create_state {
2175 struct smb_create_returns cr;
2178 static void cli_nttrans_create_done(struct tevent_req *subreq);
2180 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2181 struct tevent_context *ev,
2182 struct cli_state *cli,
2184 uint32_t CreatFlags,
2185 uint32_t DesiredAccess,
2186 uint32_t FileAttributes,
2187 uint32_t ShareAccess,
2188 uint32_t CreateDisposition,
2189 uint32_t CreateOptions,
2190 uint8_t SecurityFlags,
2191 struct security_descriptor *secdesc,
2192 struct ea_struct *eas,
2195 struct tevent_req *req, *subreq;
2196 struct cli_nttrans_create_state *state;
2198 uint8_t *secdesc_buf;
2201 size_t converted_len;
2203 req = tevent_req_create(mem_ctx,
2204 &state, struct cli_nttrans_create_state);
2209 if (secdesc != NULL) {
2210 status = marshall_sec_desc(talloc_tos(), secdesc,
2211 &secdesc_buf, &secdesc_len);
2212 if (tevent_req_nterror(req, status)) {
2213 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2214 nt_errstr(status)));
2215 return tevent_req_post(req, ev);
2226 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2227 return tevent_req_post(req, ev);
2230 param = talloc_array(state, uint8_t, 53);
2231 if (tevent_req_nomem(param, req)) {
2232 return tevent_req_post(req, ev);
2235 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2236 fname, strlen(fname),
2238 if (tevent_req_nomem(param, req)) {
2239 return tevent_req_post(req, ev);
2242 SIVAL(param, 0, CreatFlags);
2243 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2244 SIVAL(param, 8, DesiredAccess);
2245 SIVAL(param, 12, 0x0); /* AllocationSize */
2246 SIVAL(param, 16, 0x0); /* AllocationSize */
2247 SIVAL(param, 20, FileAttributes);
2248 SIVAL(param, 24, ShareAccess);
2249 SIVAL(param, 28, CreateDisposition);
2250 SIVAL(param, 32, CreateOptions |
2251 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2252 SIVAL(param, 36, secdesc_len);
2253 SIVAL(param, 40, 0); /* EA length*/
2254 SIVAL(param, 44, converted_len);
2255 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2256 SCVAL(param, 52, SecurityFlags);
2258 subreq = cli_trans_send(state, ev, cli,
2259 0, /* additional_flags2 */
2261 NULL, -1, /* name, fid */
2262 NT_TRANSACT_CREATE, 0,
2263 NULL, 0, 0, /* setup */
2264 param, talloc_get_size(param), 128, /* param */
2265 secdesc_buf, secdesc_len, 0); /* data */
2266 if (tevent_req_nomem(subreq, req)) {
2267 return tevent_req_post(req, ev);
2269 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2273 static void cli_nttrans_create_done(struct tevent_req *subreq)
2275 struct tevent_req *req = tevent_req_callback_data(
2276 subreq, struct tevent_req);
2277 struct cli_nttrans_create_state *state = tevent_req_data(
2278 req, struct cli_nttrans_create_state);
2283 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2284 NULL, 0, NULL, /* rsetup */
2285 ¶m, 69, &num_param,
2287 if (tevent_req_nterror(req, status)) {
2290 state->cr.oplock_level = CVAL(param, 0);
2291 state->fnum = SVAL(param, 2);
2292 state->cr.create_action = IVAL(param, 4);
2293 state->cr.creation_time = BVAL(param, 12);
2294 state->cr.last_access_time = BVAL(param, 20);
2295 state->cr.last_write_time = BVAL(param, 28);
2296 state->cr.change_time = BVAL(param, 36);
2297 state->cr.file_attributes = IVAL(param, 44);
2298 state->cr.allocation_size = BVAL(param, 48);
2299 state->cr.end_of_file = BVAL(param, 56);
2302 tevent_req_done(req);
2305 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2307 struct smb_create_returns *cr)
2309 struct cli_nttrans_create_state *state = tevent_req_data(
2310 req, struct cli_nttrans_create_state);
2313 if (tevent_req_is_nterror(req, &status)) {
2316 *fnum = state->fnum;
2320 return NT_STATUS_OK;
2323 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2325 uint32_t CreatFlags,
2326 uint32_t DesiredAccess,
2327 uint32_t FileAttributes,
2328 uint32_t ShareAccess,
2329 uint32_t CreateDisposition,
2330 uint32_t CreateOptions,
2331 uint8_t SecurityFlags,
2332 struct security_descriptor *secdesc,
2333 struct ea_struct *eas,
2336 struct smb_create_returns *cr)
2338 TALLOC_CTX *frame = talloc_stackframe();
2339 struct tevent_context *ev;
2340 struct tevent_req *req;
2341 NTSTATUS status = NT_STATUS_NO_MEMORY;
2343 if (smbXcli_conn_has_async_calls(cli->conn)) {
2345 * Can't use sync call while an async call is in flight
2347 status = NT_STATUS_INVALID_PARAMETER;
2350 ev = samba_tevent_context_init(frame);
2354 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2355 DesiredAccess, FileAttributes,
2356 ShareAccess, CreateDisposition,
2357 CreateOptions, SecurityFlags,
2358 secdesc, eas, num_eas);
2362 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2365 status = cli_nttrans_create_recv(req, pfid, cr);
2371 /****************************************************************************
2373 WARNING: if you open with O_WRONLY then getattrE won't work!
2374 ****************************************************************************/
2376 struct cli_openx_state {
2383 static void cli_openx_done(struct tevent_req *subreq);
2385 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2386 struct tevent_context *ev,
2387 struct cli_state *cli, const char *fname,
2388 int flags, int share_mode,
2389 struct tevent_req **psmbreq)
2391 struct tevent_req *req, *subreq;
2392 struct cli_openx_state *state;
2394 unsigned accessmode;
2395 uint8_t additional_flags;
2398 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2404 if (flags & O_CREAT) {
2407 if (!(flags & O_EXCL)) {
2408 if (flags & O_TRUNC)
2414 accessmode = (share_mode<<4);
2416 if ((flags & O_ACCMODE) == O_RDWR) {
2418 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2423 if ((flags & O_SYNC) == O_SYNC) {
2424 accessmode |= (1<<14);
2428 if (share_mode == DENY_FCB) {
2432 SCVAL(state->vwv + 0, 0, 0xFF);
2433 SCVAL(state->vwv + 0, 1, 0);
2434 SSVAL(state->vwv + 1, 0, 0);
2435 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2436 SSVAL(state->vwv + 3, 0, accessmode);
2437 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2438 SSVAL(state->vwv + 5, 0, 0);
2439 SIVAL(state->vwv + 6, 0, 0);
2440 SSVAL(state->vwv + 8, 0, openfn);
2441 SIVAL(state->vwv + 9, 0, 0);
2442 SIVAL(state->vwv + 11, 0, 0);
2443 SIVAL(state->vwv + 13, 0, 0);
2445 additional_flags = 0;
2447 if (cli->use_oplocks) {
2448 /* if using oplocks then ask for a batch oplock via
2449 core and extended methods */
2451 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2452 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2455 bytes = talloc_array(state, uint8_t, 0);
2456 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2457 strlen(fname)+1, NULL);
2459 if (tevent_req_nomem(bytes, req)) {
2460 return tevent_req_post(req, ev);
2463 state->bytes.iov_base = (void *)bytes;
2464 state->bytes.iov_len = talloc_get_size(bytes);
2466 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2467 15, state->vwv, 1, &state->bytes);
2468 if (subreq == NULL) {
2472 tevent_req_set_callback(subreq, cli_openx_done, req);
2477 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2478 struct cli_state *cli, const char *fname,
2479 int flags, int share_mode)
2481 struct tevent_req *req, *subreq;
2484 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2490 status = smb1cli_req_chain_submit(&subreq, 1);
2491 if (tevent_req_nterror(req, status)) {
2492 return tevent_req_post(req, ev);
2497 static void cli_openx_done(struct tevent_req *subreq)
2499 struct tevent_req *req = tevent_req_callback_data(
2500 subreq, struct tevent_req);
2501 struct cli_openx_state *state = tevent_req_data(
2502 req, struct cli_openx_state);
2507 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2509 TALLOC_FREE(subreq);
2510 if (tevent_req_nterror(req, status)) {
2513 state->fnum = SVAL(vwv+2, 0);
2514 tevent_req_done(req);
2517 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2519 struct cli_openx_state *state = tevent_req_data(
2520 req, struct cli_openx_state);
2523 if (tevent_req_is_nterror(req, &status)) {
2526 *pfnum = state->fnum;
2527 return NT_STATUS_OK;
2530 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2531 int share_mode, uint16_t *pfnum)
2533 TALLOC_CTX *frame = talloc_stackframe();
2534 struct tevent_context *ev;
2535 struct tevent_req *req;
2536 NTSTATUS status = NT_STATUS_NO_MEMORY;
2538 if (smbXcli_conn_has_async_calls(cli->conn)) {
2540 * Can't use sync call while an async call is in flight
2542 status = NT_STATUS_INVALID_PARAMETER;
2546 ev = samba_tevent_context_init(frame);
2551 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2556 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2560 status = cli_openx_recv(req, pfnum);
2565 /****************************************************************************
2566 Synchronous wrapper function that does an NtCreateX open by preference
2567 and falls back to openX if this fails.
2568 ****************************************************************************/
2570 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2571 int share_mode_in, uint16_t *pfnum)
2574 unsigned int openfn = 0;
2575 unsigned int dos_deny = 0;
2576 uint32_t access_mask, share_mode, create_disposition, create_options;
2577 struct smb_create_returns cr;
2579 /* Do the initial mapping into OpenX parameters. */
2580 if (flags & O_CREAT) {
2583 if (!(flags & O_EXCL)) {
2584 if (flags & O_TRUNC)
2590 dos_deny = (share_mode_in<<4);
2592 if ((flags & O_ACCMODE) == O_RDWR) {
2594 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2599 if ((flags & O_SYNC) == O_SYNC) {
2600 dos_deny |= (1<<14);
2604 if (share_mode_in == DENY_FCB) {
2609 /* Hmmm. This is what I think the above code
2610 should look like if it's using the constants
2613 if (flags & O_CREAT) {
2614 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2616 if (!(flags & O_EXCL)) {
2617 if (flags & O_TRUNC)
2618 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2620 openfn |= OPENX_FILE_EXISTS_OPEN;
2623 dos_deny = SET_DENY_MODE(share_mode_in);
2625 if ((flags & O_ACCMODE) == O_RDWR) {
2626 dos_deny |= DOS_OPEN_RDWR;
2627 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2628 dos_deny |= DOS_OPEN_WRONLY;
2632 if ((flags & O_SYNC) == O_SYNC) {
2633 dos_deny |= FILE_SYNC_OPENMODE;
2637 if (share_mode_in == DENY_FCB) {
2642 if (!map_open_params_to_ntcreate(fname, dos_deny,
2643 openfn, &access_mask,
2644 &share_mode, &create_disposition,
2645 &create_options, NULL)) {
2649 status = cli_ntcreate(cli,
2661 /* Try and cope will all varients of "we don't do this call"
2662 and fall back to openX. */
2664 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2665 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2666 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2667 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2668 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2669 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2670 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2671 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2672 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2676 if (NT_STATUS_IS_OK(status) &&
2677 (create_options & FILE_NON_DIRECTORY_FILE) &&
2678 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2681 * Some (broken) servers return a valid handle
2682 * for directories even if FILE_NON_DIRECTORY_FILE
2683 * is set. Just close the handle and set the
2684 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2686 status = cli_close(cli, *pfnum);
2687 if (!NT_STATUS_IS_OK(status)) {
2690 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2691 /* Set this so libsmbclient can retrieve it. */
2692 cli->raw_status = status;
2699 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2702 /****************************************************************************
2704 ****************************************************************************/
2706 struct cli_close_state {
2710 static void cli_close_done(struct tevent_req *subreq);
2712 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2713 struct tevent_context *ev,
2714 struct cli_state *cli,
2716 struct tevent_req **psubreq)
2718 struct tevent_req *req, *subreq;
2719 struct cli_close_state *state;
2721 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2726 SSVAL(state->vwv+0, 0, fnum);
2727 SIVALS(state->vwv+1, 0, -1);
2729 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2731 if (subreq == NULL) {
2735 tevent_req_set_callback(subreq, cli_close_done, req);
2740 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2741 struct tevent_context *ev,
2742 struct cli_state *cli,
2745 struct tevent_req *req, *subreq;
2748 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2753 status = smb1cli_req_chain_submit(&subreq, 1);
2754 if (tevent_req_nterror(req, status)) {
2755 return tevent_req_post(req, ev);
2760 static void cli_close_done(struct tevent_req *subreq)
2762 struct tevent_req *req = tevent_req_callback_data(
2763 subreq, struct tevent_req);
2766 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2767 TALLOC_FREE(subreq);
2768 if (tevent_req_nterror(req, status)) {
2771 tevent_req_done(req);
2774 NTSTATUS cli_close_recv(struct tevent_req *req)
2776 return tevent_req_simple_recv_ntstatus(req);
2779 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2781 TALLOC_CTX *frame = NULL;
2782 struct tevent_context *ev;
2783 struct tevent_req *req;
2784 NTSTATUS status = NT_STATUS_OK;
2786 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2787 return cli_smb2_close_fnum(cli, fnum);
2790 frame = talloc_stackframe();
2792 if (smbXcli_conn_has_async_calls(cli->conn)) {
2794 * Can't use sync call while an async call is in flight
2796 status = NT_STATUS_INVALID_PARAMETER;
2800 ev = samba_tevent_context_init(frame);
2802 status = NT_STATUS_NO_MEMORY;
2806 req = cli_close_send(frame, ev, cli, fnum);
2808 status = NT_STATUS_NO_MEMORY;
2812 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2816 status = cli_close_recv(req);
2822 /****************************************************************************
2823 Truncate a file to a specified size
2824 ****************************************************************************/
2826 struct ftrunc_state {
2832 static void cli_ftruncate_done(struct tevent_req *subreq)
2834 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2835 NULL, 0, NULL, NULL, 0, NULL);
2836 tevent_req_simple_finish_ntstatus(subreq, status);
2839 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2840 struct tevent_context *ev,
2841 struct cli_state *cli,
2845 struct tevent_req *req = NULL, *subreq = NULL;
2846 struct ftrunc_state *state = NULL;
2848 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2853 /* Setup setup word. */
2854 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2856 /* Setup param array. */
2857 SSVAL(state->param,0,fnum);
2858 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2859 SSVAL(state->param,4,0);
2861 /* Setup data array. */
2862 SBVAL(state->data, 0, size);
2864 subreq = cli_trans_send(state, /* mem ctx. */
2865 ev, /* event ctx. */
2866 cli, /* cli_state. */
2867 0, /* additional_flags2 */
2868 SMBtrans2, /* cmd. */
2869 NULL, /* pipe name. */
2873 &state->setup, /* setup. */
2874 1, /* num setup uint16_t words. */
2875 0, /* max returned setup. */
2876 state->param, /* param. */
2878 2, /* max returned param. */
2879 state->data, /* data. */
2881 0); /* max returned data. */
2883 if (tevent_req_nomem(subreq, req)) {
2884 return tevent_req_post(req, ev);
2886 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2890 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2892 return tevent_req_simple_recv_ntstatus(req);
2895 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2897 TALLOC_CTX *frame = talloc_stackframe();
2898 struct tevent_context *ev = NULL;
2899 struct tevent_req *req = NULL;
2900 NTSTATUS status = NT_STATUS_OK;
2902 if (smbXcli_conn_has_async_calls(cli->conn)) {
2904 * Can't use sync call while an async call is in flight
2906 status = NT_STATUS_INVALID_PARAMETER;
2910 ev = samba_tevent_context_init(frame);
2912 status = NT_STATUS_NO_MEMORY;
2916 req = cli_ftruncate_send(frame,
2922 status = NT_STATUS_NO_MEMORY;
2926 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2930 status = cli_ftruncate_recv(req);
2937 /****************************************************************************
2938 send a lock with a specified locktype
2939 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2940 ****************************************************************************/
2942 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2943 uint32_t offset, uint32_t len,
2944 int timeout, unsigned char locktype)
2949 unsigned int set_timeout = 0;
2950 unsigned int saved_timeout = 0;
2952 SCVAL(vwv + 0, 0, 0xff);
2953 SCVAL(vwv + 0, 1, 0);
2954 SSVAL(vwv + 1, 0, 0);
2955 SSVAL(vwv + 2, 0, fnum);
2956 SCVAL(vwv + 3, 0, locktype);
2957 SCVAL(vwv + 3, 1, 0);
2958 SIVALS(vwv + 4, 0, timeout);
2959 SSVAL(vwv + 6, 0, 0);
2960 SSVAL(vwv + 7, 0, 1);
2962 SSVAL(bytes, 0, cli_getpid(cli));
2963 SIVAL(bytes, 2, offset);
2964 SIVAL(bytes, 6, len);
2967 if (timeout == -1) {
2968 set_timeout = 0x7FFFFFFF;
2970 set_timeout = timeout + 2*1000;
2972 saved_timeout = cli_set_timeout(cli, set_timeout);
2975 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2976 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2978 if (saved_timeout != 0) {
2979 cli_set_timeout(cli, saved_timeout);
2985 /****************************************************************************
2987 note that timeout is in units of 2 milliseconds
2988 ****************************************************************************/
2990 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2991 uint32_t offset, uint32_t len, int timeout,
2992 enum brl_type lock_type)
2996 status = cli_locktype(cli, fnum, offset, len, timeout,
2997 (lock_type == READ_LOCK? 1 : 0));
3001 /****************************************************************************
3003 ****************************************************************************/
3005 struct cli_unlock_state {
3010 static void cli_unlock_done(struct tevent_req *subreq);
3012 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3013 struct tevent_context *ev,
3014 struct cli_state *cli,
3020 struct tevent_req *req = NULL, *subreq = NULL;
3021 struct cli_unlock_state *state = NULL;
3022 uint8_t additional_flags = 0;
3024 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3029 SCVAL(state->vwv+0, 0, 0xFF);
3030 SSVAL(state->vwv+2, 0, fnum);
3031 SCVAL(state->vwv+3, 0, 0);
3032 SIVALS(state->vwv+4, 0, 0);
3033 SSVAL(state->vwv+6, 0, 1);
3034 SSVAL(state->vwv+7, 0, 0);
3036 SSVAL(state->data, 0, cli_getpid(cli));
3037 SIVAL(state->data, 2, offset);
3038 SIVAL(state->data, 6, len);
3040 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3041 8, state->vwv, 10, state->data);
3042 if (tevent_req_nomem(subreq, req)) {
3043 return tevent_req_post(req, ev);
3045 tevent_req_set_callback(subreq, cli_unlock_done, req);
3049 static void cli_unlock_done(struct tevent_req *subreq)
3051 struct tevent_req *req = tevent_req_callback_data(
3052 subreq, struct tevent_req);
3055 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3056 TALLOC_FREE(subreq);
3057 if (tevent_req_nterror(req, status)) {
3060 tevent_req_done(req);
3063 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3065 return tevent_req_simple_recv_ntstatus(req);
3068 NTSTATUS cli_unlock(struct cli_state *cli,
3073 TALLOC_CTX *frame = talloc_stackframe();
3074 struct tevent_context *ev;
3075 struct tevent_req *req;
3076 NTSTATUS status = NT_STATUS_OK;
3078 if (smbXcli_conn_has_async_calls(cli->conn)) {
3080 * Can't use sync call while an async call is in flight
3082 status = NT_STATUS_INVALID_PARAMETER;
3086 ev = samba_tevent_context_init(frame);
3088 status = NT_STATUS_NO_MEMORY;
3092 req = cli_unlock_send(frame, ev, cli,
3095 status = NT_STATUS_NO_MEMORY;
3099 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3103 status = cli_unlock_recv(req);
3110 /****************************************************************************
3111 Lock a file with 64 bit offsets.
3112 ****************************************************************************/
3114 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3115 uint64_t offset, uint64_t len, int timeout,
3116 enum brl_type lock_type)
3120 unsigned int set_timeout = 0;
3121 unsigned int saved_timeout = 0;
3125 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3126 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3129 ltype = (lock_type == READ_LOCK? 1 : 0);
3130 ltype |= LOCKING_ANDX_LARGE_FILES;
3132 SCVAL(vwv + 0, 0, 0xff);
3133 SCVAL(vwv + 0, 1, 0);
3134 SSVAL(vwv + 1, 0, 0);
3135 SSVAL(vwv + 2, 0, fnum);
3136 SCVAL(vwv + 3, 0, ltype);
3137 SCVAL(vwv + 3, 1, 0);
3138 SIVALS(vwv + 4, 0, timeout);
3139 SSVAL(vwv + 6, 0, 0);
3140 SSVAL(vwv + 7, 0, 1);
3142 SIVAL(bytes, 0, cli_getpid(cli));
3143 SOFF_T_R(bytes, 4, offset);
3144 SOFF_T_R(bytes, 12, len);
3147 if (timeout == -1) {
3148 set_timeout = 0x7FFFFFFF;
3150 set_timeout = timeout + 2*1000;
3152 saved_timeout = cli_set_timeout(cli, set_timeout);
3155 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3156 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3158 if (saved_timeout != 0) {
3159 cli_set_timeout(cli, saved_timeout);
3165 /****************************************************************************
3166 Unlock a file with 64 bit offsets.
3167 ****************************************************************************/
3169 struct cli_unlock64_state {
3174 static void cli_unlock64_done(struct tevent_req *subreq);
3176 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3177 struct tevent_context *ev,
3178 struct cli_state *cli,
3184 struct tevent_req *req = NULL, *subreq = NULL;
3185 struct cli_unlock64_state *state = NULL;
3186 uint8_t additional_flags = 0;
3188 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3193 SCVAL(state->vwv+0, 0, 0xff);
3194 SSVAL(state->vwv+2, 0, fnum);
3195 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3196 SIVALS(state->vwv+4, 0, 0);
3197 SSVAL(state->vwv+6, 0, 1);
3198 SSVAL(state->vwv+7, 0, 0);
3200 SIVAL(state->data, 0, cli_getpid(cli));
3201 SOFF_T_R(state->data, 4, offset);
3202 SOFF_T_R(state->data, 12, len);
3204 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3205 8, state->vwv, 20, state->data);
3206 if (tevent_req_nomem(subreq, req)) {
3207 return tevent_req_post(req, ev);
3209 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3213 static void cli_unlock64_done(struct tevent_req *subreq)
3215 struct tevent_req *req = tevent_req_callback_data(
3216 subreq, struct tevent_req);
3219 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3220 TALLOC_FREE(subreq);
3221 if (tevent_req_nterror(req, status)) {
3224 tevent_req_done(req);
3227 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3229 return tevent_req_simple_recv_ntstatus(req);
3232 NTSTATUS cli_unlock64(struct cli_state *cli,
3237 TALLOC_CTX *frame = talloc_stackframe();
3238 struct tevent_context *ev;
3239 struct tevent_req *req;
3240 NTSTATUS status = NT_STATUS_OK;
3242 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3243 return cli_unlock(cli, fnum, offset, len);
3246 if (smbXcli_conn_has_async_calls(cli->conn)) {
3248 * Can't use sync call while an async call is in flight
3250 status = NT_STATUS_INVALID_PARAMETER;
3254 ev = samba_tevent_context_init(frame);
3256 status = NT_STATUS_NO_MEMORY;
3260 req = cli_unlock64_send(frame, ev, cli,
3263 status = NT_STATUS_NO_MEMORY;
3267 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3271 status = cli_unlock64_recv(req);
3278 /****************************************************************************
3279 Get/unlock a POSIX lock on a file - internal function.
3280 ****************************************************************************/
3282 struct posix_lock_state {
3285 uint8_t data[POSIX_LOCK_DATA_SIZE];
3288 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3290 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3291 NULL, 0, NULL, NULL, 0, NULL);
3292 tevent_req_simple_finish_ntstatus(subreq, status);
3295 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3296 struct tevent_context *ev,
3297 struct cli_state *cli,
3302 enum brl_type lock_type)
3304 struct tevent_req *req = NULL, *subreq = NULL;
3305 struct posix_lock_state *state = NULL;
3307 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3312 /* Setup setup word. */
3313 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3315 /* Setup param array. */
3316 SSVAL(&state->param, 0, fnum);
3317 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3319 /* Setup data array. */
3320 switch (lock_type) {
3322 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3323 POSIX_LOCK_TYPE_READ);
3326 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3327 POSIX_LOCK_TYPE_WRITE);
3330 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3331 POSIX_LOCK_TYPE_UNLOCK);
3338 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3339 POSIX_LOCK_FLAG_WAIT);
3341 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3342 POSIX_LOCK_FLAG_NOWAIT);
3345 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3346 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3347 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3349 subreq = cli_trans_send(state, /* mem ctx. */
3350 ev, /* event ctx. */
3351 cli, /* cli_state. */
3352 0, /* additional_flags2 */
3353 SMBtrans2, /* cmd. */
3354 NULL, /* pipe name. */
3358 &state->setup, /* setup. */
3359 1, /* num setup uint16_t words. */
3360 0, /* max returned setup. */
3361 state->param, /* param. */
3363 2, /* max returned param. */
3364 state->data, /* data. */
3365 POSIX_LOCK_DATA_SIZE, /* num data. */
3366 0); /* max returned data. */
3368 if (tevent_req_nomem(subreq, req)) {
3369 return tevent_req_post(req, ev);
3371 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3375 /****************************************************************************
3377 ****************************************************************************/
3379 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3380 struct tevent_context *ev,
3381 struct cli_state *cli,
3386 enum brl_type lock_type)
3388 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3389 wait_lock, lock_type);
3392 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3394 return tevent_req_simple_recv_ntstatus(req);
3397 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3398 uint64_t offset, uint64_t len,
3399 bool wait_lock, enum brl_type lock_type)
3401 TALLOC_CTX *frame = talloc_stackframe();
3402 struct tevent_context *ev = NULL;
3403 struct tevent_req *req = NULL;
3404 NTSTATUS status = NT_STATUS_OK;
3406 if (smbXcli_conn_has_async_calls(cli->conn)) {
3408 * Can't use sync call while an async call is in flight
3410 status = NT_STATUS_INVALID_PARAMETER;
3414 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3415 status = NT_STATUS_INVALID_PARAMETER;
3419 ev = samba_tevent_context_init(frame);
3421 status = NT_STATUS_NO_MEMORY;
3425 req = cli_posix_lock_send(frame,
3434 status = NT_STATUS_NO_MEMORY;
3438 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3442 status = cli_posix_lock_recv(req);
3449 /****************************************************************************
3450 POSIX Unlock a file.
3451 ****************************************************************************/
3453 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3454 struct tevent_context *ev,
3455 struct cli_state *cli,
3460 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3461 false, UNLOCK_LOCK);
3464 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3466 return tevent_req_simple_recv_ntstatus(req);
3469 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3471 TALLOC_CTX *frame = talloc_stackframe();
3472 struct tevent_context *ev = NULL;
3473 struct tevent_req *req = NULL;
3474 NTSTATUS status = NT_STATUS_OK;
3476 if (smbXcli_conn_has_async_calls(cli->conn)) {
3478 * Can't use sync call while an async call is in flight
3480 status = NT_STATUS_INVALID_PARAMETER;
3484 ev = samba_tevent_context_init(frame);
3486 status = NT_STATUS_NO_MEMORY;
3490 req = cli_posix_unlock_send(frame,
3497 status = NT_STATUS_NO_MEMORY;
3501 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3505 status = cli_posix_unlock_recv(req);
3512 /****************************************************************************
3513 Do a SMBgetattrE call.
3514 ****************************************************************************/
3516 static void cli_getattrE_done(struct tevent_req *subreq);
3518 struct cli_getattrE_state {
3528 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3529 struct tevent_context *ev,
3530 struct cli_state *cli,
3533 struct tevent_req *req = NULL, *subreq = NULL;
3534 struct cli_getattrE_state *state = NULL;
3535 uint8_t additional_flags = 0;
3537 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3542 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3543 SSVAL(state->vwv+0,0,fnum);
3545 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3546 1, state->vwv, 0, NULL);
3547 if (tevent_req_nomem(subreq, req)) {
3548 return tevent_req_post(req, ev);
3550 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3554 static void cli_getattrE_done(struct tevent_req *subreq)
3556 struct tevent_req *req = tevent_req_callback_data(
3557 subreq, struct tevent_req);
3558 struct cli_getattrE_state *state = tevent_req_data(
3559 req, struct cli_getattrE_state);
3561 uint16_t *vwv = NULL;
3564 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3566 TALLOC_FREE(subreq);
3567 if (tevent_req_nterror(req, status)) {
3571 state->size = (off_t)IVAL(vwv+6,0);
3572 state->attr = SVAL(vwv+10,0);
3573 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3574 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3575 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3577 tevent_req_done(req);
3580 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3583 time_t *change_time,
3584 time_t *access_time,
3587 struct cli_getattrE_state *state = tevent_req_data(
3588 req, struct cli_getattrE_state);
3591 if (tevent_req_is_nterror(req, &status)) {
3595 *attr = state->attr;
3598 *size = state->size;
3601 *change_time = state->change_time;
3604 *access_time = state->access_time;
3607 *write_time = state->write_time;
3609 return NT_STATUS_OK;
3612 NTSTATUS cli_getattrE(struct cli_state *cli,
3616 time_t *change_time,
3617 time_t *access_time,
3620 TALLOC_CTX *frame = NULL;
3621 struct tevent_context *ev = NULL;
3622 struct tevent_req *req = NULL;
3623 NTSTATUS status = NT_STATUS_OK;
3625 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3626 return cli_smb2_getattrE(cli,
3635 frame = talloc_stackframe();
3637 if (smbXcli_conn_has_async_calls(cli->conn)) {
3639 * Can't use sync call while an async call is in flight
3641 status = NT_STATUS_INVALID_PARAMETER;
3645 ev = samba_tevent_context_init(frame);
3647 status = NT_STATUS_NO_MEMORY;
3651 req = cli_getattrE_send(frame, ev, cli, fnum);
3653 status = NT_STATUS_NO_MEMORY;
3657 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3661 status = cli_getattrE_recv(req,
3673 /****************************************************************************
3675 ****************************************************************************/
3677 static void cli_getatr_done(struct tevent_req *subreq);
3679 struct cli_getatr_state {
3686 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3687 struct tevent_context *ev,
3688 struct cli_state *cli,
3691 struct tevent_req *req = NULL, *subreq = NULL;
3692 struct cli_getatr_state *state = NULL;
3693 uint8_t additional_flags = 0;
3694 uint8_t *bytes = NULL;
3696 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3701 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3703 bytes = talloc_array(state, uint8_t, 1);
3704 if (tevent_req_nomem(bytes, req)) {
3705 return tevent_req_post(req, ev);
3708 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3709 strlen(fname)+1, NULL);
3711 if (tevent_req_nomem(bytes, req)) {
3712 return tevent_req_post(req, ev);
3715 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags, 0,
3716 0, NULL, talloc_get_size(bytes), bytes);
3717 if (tevent_req_nomem(subreq, req)) {
3718 return tevent_req_post(req, ev);
3720 tevent_req_set_callback(subreq, cli_getatr_done, req);
3724 static void cli_getatr_done(struct tevent_req *subreq)
3726 struct tevent_req *req = tevent_req_callback_data(
3727 subreq, struct tevent_req);
3728 struct cli_getatr_state *state = tevent_req_data(
3729 req, struct cli_getatr_state);
3731 uint16_t *vwv = NULL;
3734 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3736 TALLOC_FREE(subreq);
3737 if (tevent_req_nterror(req, status)) {
3741 state->attr = SVAL(vwv+0,0);
3742 state->size = (off_t)IVAL(vwv+3,0);
3743 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3745 tevent_req_done(req);
3748 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3753 struct cli_getatr_state *state = tevent_req_data(
3754 req, struct cli_getatr_state);
3757 if (tevent_req_is_nterror(req, &status)) {
3761 *attr = state->attr;
3764 *size = state->size;
3767 *write_time = state->write_time;
3769 return NT_STATUS_OK;
3772 NTSTATUS cli_getatr(struct cli_state *cli,
3778 TALLOC_CTX *frame = NULL;
3779 struct tevent_context *ev = NULL;
3780 struct tevent_req *req = NULL;
3781 NTSTATUS status = NT_STATUS_OK;
3783 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3784 return cli_smb2_getatr(cli,
3791 frame = talloc_stackframe();
3793 if (smbXcli_conn_has_async_calls(cli->conn)) {
3795 * Can't use sync call while an async call is in flight
3797 status = NT_STATUS_INVALID_PARAMETER;
3801 ev = samba_tevent_context_init(frame);
3803 status = NT_STATUS_NO_MEMORY;
3807 req = cli_getatr_send(frame, ev, cli, fname);
3809 status = NT_STATUS_NO_MEMORY;
3813 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3817 status = cli_getatr_recv(req,
3827 /****************************************************************************
3828 Do a SMBsetattrE call.
3829 ****************************************************************************/
3831 static void cli_setattrE_done(struct tevent_req *subreq);
3833 struct cli_setattrE_state {
3837 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3838 struct tevent_context *ev,
3839 struct cli_state *cli,
3845 struct tevent_req *req = NULL, *subreq = NULL;
3846 struct cli_setattrE_state *state = NULL;
3847 uint8_t additional_flags = 0;
3849 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3854 SSVAL(state->vwv+0, 0, fnum);
3855 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3856 smb1cli_conn_server_time_zone(cli->conn));
3857 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3858 smb1cli_conn_server_time_zone(cli->conn));
3859 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3860 smb1cli_conn_server_time_zone(cli->conn));
3862 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3863 7, state->vwv, 0, NULL);
3864 if (tevent_req_nomem(subreq, req)) {
3865 return tevent_req_post(req, ev);
3867 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3871 static void cli_setattrE_done(struct tevent_req *subreq)
3873 struct tevent_req *req = tevent_req_callback_data(
3874 subreq, struct tevent_req);
3877 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3878 TALLOC_FREE(subreq);
3879 if (tevent_req_nterror(req, status)) {
3882 tevent_req_done(req);
3885 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3887 return tevent_req_simple_recv_ntstatus(req);
3890 NTSTATUS cli_setattrE(struct cli_state *cli,
3896 TALLOC_CTX *frame = NULL;
3897 struct tevent_context *ev = NULL;
3898 struct tevent_req *req = NULL;
3899 NTSTATUS status = NT_STATUS_OK;
3901 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3902 return cli_smb2_setattrE(cli,
3909 frame = talloc_stackframe();
3911 if (smbXcli_conn_has_async_calls(cli->conn)) {
3913 * Can't use sync call while an async call is in flight
3915 status = NT_STATUS_INVALID_PARAMETER;
3919 ev = samba_tevent_context_init(frame);
3921 status = NT_STATUS_NO_MEMORY;
3925 req = cli_setattrE_send(frame, ev,
3933 status = NT_STATUS_NO_MEMORY;
3937 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3941 status = cli_setattrE_recv(req);
3948 /****************************************************************************
3949 Do a SMBsetatr call.
3950 ****************************************************************************/
3952 static void cli_setatr_done(struct tevent_req *subreq);
3954 struct cli_setatr_state {
3958 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3959 struct tevent_context *ev,
3960 struct cli_state *cli,
3965 struct tevent_req *req = NULL, *subreq = NULL;
3966 struct cli_setatr_state *state = NULL;
3967 uint8_t additional_flags = 0;
3968 uint8_t *bytes = NULL;
3970 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3975 SSVAL(state->vwv+0, 0, attr);
3976 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3978 bytes = talloc_array(state, uint8_t, 1);
3979 if (tevent_req_nomem(bytes, req)) {
3980 return tevent_req_post(req, ev);
3983 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3984 strlen(fname)+1, NULL);
3985 if (tevent_req_nomem(bytes, req)) {
3986 return tevent_req_post(req, ev);
3988 bytes = talloc_realloc(state, bytes, uint8_t,
3989 talloc_get_size(bytes)+1);
3990 if (tevent_req_nomem(bytes, req)) {
3991 return tevent_req_post(req, ev);
3994 bytes[talloc_get_size(bytes)-1] = 4;
3995 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3997 if (tevent_req_nomem(bytes, req)) {
3998 return tevent_req_post(req, ev);
4001 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags, 0,
4002 8, state->vwv, talloc_get_size(bytes), bytes);
4003 if (tevent_req_nomem(subreq, req)) {
4004 return tevent_req_post(req, ev);
4006 tevent_req_set_callback(subreq, cli_setatr_done, req);
4010 static void cli_setatr_done(struct tevent_req *subreq)
4012 struct tevent_req *req = tevent_req_callback_data(
4013 subreq, struct tevent_req);
4016 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4017 TALLOC_FREE(subreq);
4018 if (tevent_req_nterror(req, status)) {
4021 tevent_req_done(req);
4024 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4026 return tevent_req_simple_recv_ntstatus(req);
4029 NTSTATUS cli_setatr(struct cli_state *cli,
4034 TALLOC_CTX *frame = NULL;
4035 struct tevent_context *ev = NULL;
4036 struct tevent_req *req = NULL;
4037 NTSTATUS status = NT_STATUS_OK;
4039 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4040 return cli_smb2_setatr(cli,
4046 frame = talloc_stackframe();
4048 if (smbXcli_conn_has_async_calls(cli->conn)) {
4050 * Can't use sync call while an async call is in flight
4052 status = NT_STATUS_INVALID_PARAMETER;
4056 ev = samba_tevent_context_init(frame);
4058 status = NT_STATUS_NO_MEMORY;
4062 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4064 status = NT_STATUS_NO_MEMORY;
4068 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4072 status = cli_setatr_recv(req);
4079 /****************************************************************************
4080 Check for existance of a dir.
4081 ****************************************************************************/
4083 static void cli_chkpath_done(struct tevent_req *subreq);
4085 struct cli_chkpath_state {
4089 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4090 struct tevent_context *ev,
4091 struct cli_state *cli,
4094 struct tevent_req *req = NULL, *subreq = NULL;
4095 struct cli_chkpath_state *state = NULL;
4096 uint8_t additional_flags = 0;
4097 uint8_t *bytes = NULL;
4099 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4104 bytes = talloc_array(state, uint8_t, 1);
4105 if (tevent_req_nomem(bytes, req)) {
4106 return tevent_req_post(req, ev);
4109 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4110 strlen(fname)+1, NULL);
4112 if (tevent_req_nomem(bytes, req)) {
4113 return tevent_req_post(req, ev);
4116 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags, 0,
4117 0, NULL, talloc_get_size(bytes), bytes);
4118 if (tevent_req_nomem(subreq, req)) {
4119 return tevent_req_post(req, ev);
4121 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4125 static void cli_chkpath_done(struct tevent_req *subreq)
4127 struct tevent_req *req = tevent_req_callback_data(
4128 subreq, struct tevent_req);
4131 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4132 TALLOC_FREE(subreq);
4133 if (tevent_req_nterror(req, status)) {
4136 tevent_req_done(req);
4139 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4141 return tevent_req_simple_recv_ntstatus(req);
4144 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4146 TALLOC_CTX *frame = talloc_stackframe();
4147 struct tevent_context *ev = NULL;
4148 struct tevent_req *req = NULL;
4150 NTSTATUS status = NT_STATUS_OK;
4152 if (smbXcli_conn_has_async_calls(cli->conn)) {
4154 * Can't use sync call while an async call is in flight
4156 status = NT_STATUS_INVALID_PARAMETER;
4160 path2 = talloc_strdup(frame, path);
4162 status = NT_STATUS_NO_MEMORY;
4165 trim_char(path2,'\0','\\');
4167 path2 = talloc_strdup(frame, "\\");
4169 status = NT_STATUS_NO_MEMORY;
4174 ev = samba_tevent_context_init(frame);
4176 status = NT_STATUS_NO_MEMORY;
4180 req = cli_chkpath_send(frame, ev, cli, path2);
4182 status = NT_STATUS_NO_MEMORY;
4186 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4190 status = cli_chkpath_recv(req);
4197 /****************************************************************************
4199 ****************************************************************************/
4201 static void cli_dskattr_done(struct tevent_req *subreq);
4203 struct cli_dskattr_state {
4209 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4210 struct tevent_context *ev,
4211 struct cli_state *cli)
4213 struct tevent_req *req = NULL, *subreq = NULL;
4214 struct cli_dskattr_state *state = NULL;
4215 uint8_t additional_flags = 0;
4217 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4222 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4224 if (tevent_req_nomem(subreq, req)) {
4225 return tevent_req_post(req, ev);
4227 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4231 static void cli_dskattr_done(struct tevent_req *subreq)
4233 struct tevent_req *req = tevent_req_callback_data(
4234 subreq, struct tevent_req);
4235 struct cli_dskattr_state *state = tevent_req_data(
4236 req, struct cli_dskattr_state);
4238 uint16_t *vwv = NULL;
4241 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4243 TALLOC_FREE(subreq);
4244 if (tevent_req_nterror(req, status)) {
4247 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4248 state->total = SVAL(vwv+0, 0);
4249 state->avail = SVAL(vwv+3, 0);
4250 tevent_req_done(req);
4253 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4255 struct cli_dskattr_state *state = tevent_req_data(
4256 req, struct cli_dskattr_state);
4259 if (tevent_req_is_nterror(req, &status)) {
4262 *bsize = state->bsize;
4263 *total = state->total;
4264 *avail = state->avail;
4265 return NT_STATUS_OK;
4268 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4270 TALLOC_CTX *frame = NULL;
4271 struct tevent_context *ev = NULL;
4272 struct tevent_req *req = NULL;
4273 NTSTATUS status = NT_STATUS_OK;
4275 frame = talloc_stackframe();
4277 if (smbXcli_conn_has_async_calls(cli->conn)) {
4279 * Can't use sync call while an async call is in flight
4281 status = NT_STATUS_INVALID_PARAMETER;
4285 ev = samba_tevent_context_init(frame);
4287 status = NT_STATUS_NO_MEMORY;
4291 req = cli_dskattr_send(frame, ev, cli);
4293 status = NT_STATUS_NO_MEMORY;
4297 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4301 status = cli_dskattr_recv(req, bsize, total, avail);
4308 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4309 uint64_t *total, uint64_t *avail)
4311 uint64_t sectors_per_block;
4312 uint64_t bytes_per_sector;
4313 int old_bsize, old_total, old_avail;
4316 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4317 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4321 * Try the trans2 disk full size info call first.
4322 * We already use this in SMBC_fstatvfs_ctx().
4323 * Ignore 'actual_available_units' as we only
4324 * care about the quota for the caller.
4327 status = cli_get_fs_full_size_info(cli,
4334 /* Try and cope will all varients of "we don't do this call"
4335 and fall back to cli_dskattr. */
4337 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4338 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4339 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4340 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4341 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4342 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4343 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4344 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4345 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4346 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4350 if (!NT_STATUS_IS_OK(status)) {
4355 *bsize = sectors_per_block *
4359 return NT_STATUS_OK;
4363 /* Old SMB1 core protocol fallback. */
4364 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4365 if (!NT_STATUS_IS_OK(status)) {
4369 *bsize = (uint64_t)old_bsize;
4372 *total = (uint64_t)old_total;
4375 *avail = (uint64_t)old_avail;
4377 return NT_STATUS_OK;
4380 /****************************************************************************
4381 Create and open a temporary file.
4382 ****************************************************************************/
4384 static void cli_ctemp_done(struct tevent_req *subreq);
4386 struct ctemp_state {
4392 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4393 struct tevent_context *ev,
4394 struct cli_state *cli,
4397 struct tevent_req *req = NULL, *subreq = NULL;
4398 struct ctemp_state *state = NULL;
4399 uint8_t additional_flags = 0;
4400 uint8_t *bytes = NULL;
4402 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4407 SSVAL(state->vwv,0,0);
4408 SIVALS(state->vwv+1,0,-1);
4410 bytes = talloc_array(state, uint8_t, 1);
4411 if (tevent_req_nomem(bytes, req)) {
4412 return tevent_req_post(req, ev);
4415 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4416 strlen(path)+1, NULL);
4417 if (tevent_req_nomem(bytes, req)) {
4418 return tevent_req_post(req, ev);
4421 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags, 0,
4422 3, state->vwv, talloc_get_size(bytes), bytes);
4423 if (tevent_req_nomem(subreq, req)) {
4424 return tevent_req_post(req, ev);
4426 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4430 static void cli_ctemp_done(struct tevent_req *subreq)
4432 struct tevent_req *req = tevent_req_callback_data(
4433 subreq, struct tevent_req);
4434 struct ctemp_state *state = tevent_req_data(
4435 req, struct ctemp_state);
4439 uint32_t num_bytes = 0;
4440 uint8_t *bytes = NULL;
4442 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4443 &num_bytes, &bytes);
4444 TALLOC_FREE(subreq);
4445 if (tevent_req_nterror(req, status)) {
4449 state->fnum = SVAL(vwv+0, 0);
4451 /* From W2K3, the result is just the ASCII name */
4452 if (num_bytes < 2) {
4453 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4457 if (pull_string_talloc(state,
4464 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4467 tevent_req_done(req);
4470 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4475 struct ctemp_state *state = tevent_req_data(req,
4476 struct ctemp_state);
4479 if (tevent_req_is_nterror(req, &status)) {
4482 *pfnum = state->fnum;
4483 *outfile = talloc_strdup(ctx, state->ret_path);
4485 return NT_STATUS_NO_MEMORY;
4487 return NT_STATUS_OK;
4490 NTSTATUS cli_ctemp(struct cli_state *cli,
4496 TALLOC_CTX *frame = talloc_stackframe();
4497 struct tevent_context *ev;
4498 struct tevent_req *req;
4499 NTSTATUS status = NT_STATUS_OK;
4501 if (smbXcli_conn_has_async_calls(cli->conn)) {
4503 * Can't use sync call while an async call is in flight
4505 status = NT_STATUS_INVALID_PARAMETER;
4509 ev = samba_tevent_context_init(frame);
4511 status = NT_STATUS_NO_MEMORY;
4515 req = cli_ctemp_send(frame, ev, cli, path);
4517 status = NT_STATUS_NO_MEMORY;
4521 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4525 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4533 send a raw ioctl - used by the torture code
4535 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4540 SSVAL(vwv+0, 0, fnum);
4541 SSVAL(vwv+1, 0, code>>16);
4542 SSVAL(vwv+2, 0, (code&0xFFFF));
4544 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4545 NULL, 0, NULL, NULL, NULL, NULL);
4546 if (!NT_STATUS_IS_OK(status)) {
4549 *blob = data_blob_null;
4550 return NT_STATUS_OK;
4553 /*********************************************************
4554 Set an extended attribute utility fn.
4555 *********************************************************/
4557 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4558 uint8_t *param, unsigned int param_len,
4559 const char *ea_name,
4560 const char *ea_val, size_t ea_len)
4563 unsigned int data_len = 0;
4564 uint8_t *data = NULL;
4566 size_t ea_namelen = strlen(ea_name);
4569 SSVAL(setup, 0, setup_val);
4571 if (ea_namelen == 0 && ea_len == 0) {
4573 data = talloc_array(talloc_tos(),
4577 return NT_STATUS_NO_MEMORY;
4580 SIVAL(p,0,data_len);
4582 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4583 data = talloc_array(talloc_tos(),
4587 return NT_STATUS_NO_MEMORY;
4590 SIVAL(p,0,data_len);
4592 SCVAL(p, 0, 0); /* EA flags. */
4593 SCVAL(p, 1, ea_namelen);
4594 SSVAL(p, 2, ea_len);
4595 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4596 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4599 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4601 param, param_len, 2,
4604 NULL, 0, NULL, /* rsetup */
4605 NULL, 0, NULL, /* rparam */
4606 NULL, 0, NULL); /* rdata */
4611 /*********************************************************
4612 Set an extended attribute on a pathname.
4613 *********************************************************/
4615 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4616 const char *ea_name, const char *ea_val,
4619 unsigned int param_len = 0;
4622 TALLOC_CTX *frame = NULL;
4624 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4625 return cli_smb2_set_ea_path(cli,
4632 frame = talloc_stackframe();
4634 param = talloc_array(frame, uint8_t, 6);
4636 status = NT_STATUS_NO_MEMORY;
4639 SSVAL(param,0,SMB_INFO_SET_EA);
4643 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4644 path, strlen(path)+1,
4646 param_len = talloc_get_size(param);
4648 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4649 ea_name, ea_val, ea_len);
4657 /*********************************************************
4658 Set an extended attribute on an fnum.
4659 *********************************************************/
4661 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4662 const char *ea_name, const char *ea_val,
4667 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4668 return cli_smb2_set_ea_fnum(cli,
4675 memset(param, 0, 6);
4676 SSVAL(param,0,fnum);
4677 SSVAL(param,2,SMB_INFO_SET_EA);
4679 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4680 ea_name, ea_val, ea_len);
4683 /*********************************************************
4684 Get an extended attribute list utility fn.
4685 *********************************************************/
4687 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4689 size_t *pnum_eas, struct ea_struct **pea_list)
4691 struct ea_struct *ea_list = NULL;
4696 if (rdata_len < 4) {
4700 ea_size = (size_t)IVAL(rdata,0);
4701 if (ea_size > rdata_len) {
4706 /* No EA's present. */
4715 /* Validate the EA list and count it. */
4716 for (num_eas = 0; ea_size >= 4; num_eas++) {
4717 unsigned int ea_namelen = CVAL(p,1);
4718 unsigned int ea_valuelen = SVAL(p,2);
4719 if (ea_namelen == 0) {
4722 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4725 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4726 p += 4 + ea_namelen + 1 + ea_valuelen;
4735 *pnum_eas = num_eas;
4737 /* Caller only wants number of EA's. */
4741 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4746 ea_size = (size_t)IVAL(rdata,0);
4749 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4750 struct ea_struct *ea = &ea_list[num_eas];
4751 fstring unix_ea_name;
4752 unsigned int ea_namelen = CVAL(p,1);
4753 unsigned int ea_valuelen = SVAL(p,2);
4755 ea->flags = CVAL(p,0);
4756 unix_ea_name[0] = '\0';
4757 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4758 ea->name = talloc_strdup(ea_list, unix_ea_name);
4762 /* Ensure the value is null terminated (in case it's a string). */
4763 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4764 if (!ea->value.data) {
4768 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4770 ea->value.data[ea_valuelen] = 0;
4772 p += 4 + ea_namelen + 1 + ea_valuelen;
4775 *pea_list = ea_list;
4779 TALLOC_FREE(ea_list);
4783 /*********************************************************
4784 Get an extended attribute list from a pathname.
4785 *********************************************************/
4787 struct cli_get_ea_list_path_state {
4792 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4794 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4795 struct tevent_context *ev,
4796 struct cli_state *cli,
4799 struct tevent_req *req, *subreq;
4800 struct cli_get_ea_list_path_state *state;
4802 req = tevent_req_create(mem_ctx, &state,
4803 struct cli_get_ea_list_path_state);
4807 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4808 SMB_INFO_QUERY_ALL_EAS, 4,
4810 if (tevent_req_nomem(subreq, req)) {
4811 return tevent_req_post(req, ev);
4813 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4817 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4819 struct tevent_req *req = tevent_req_callback_data(
4820 subreq, struct tevent_req);
4821 struct cli_get_ea_list_path_state *state = tevent_req_data(
4822 req, struct cli_get_ea_list_path_state);
4825 status = cli_qpathinfo_recv(subreq, state, &state->data,
4827 TALLOC_FREE(subreq);
4828 if (tevent_req_nterror(req, status)) {
4831 tevent_req_done(req);
4834 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4835 size_t *pnum_eas, struct ea_struct **peas)
4837 struct cli_get_ea_list_path_state *state = tevent_req_data(
4838 req, struct cli_get_ea_list_path_state);
4841 if (tevent_req_is_nterror(req, &status)) {
4844 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4846 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4848 return NT_STATUS_OK;
4851 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4854 struct ea_struct **pea_list)
4856 TALLOC_CTX *frame = NULL;
4857 struct tevent_context *ev = NULL;
4858 struct tevent_req *req = NULL;
4859 NTSTATUS status = NT_STATUS_NO_MEMORY;
4861 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4862 return cli_smb2_get_ea_list_path(cli,
4869 frame = talloc_stackframe();
4871 if (smbXcli_conn_has_async_calls(cli->conn)) {
4873 * Can't use sync call while an async call is in flight
4875 status = NT_STATUS_INVALID_PARAMETER;
4878 ev = samba_tevent_context_init(frame);
4882 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4886 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4889 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4895 /****************************************************************************
4896 Convert open "flags" arg to uint32_t on wire.
4897 ****************************************************************************/
4899 static uint32_t open_flags_to_wire(int flags)
4901 int open_mode = flags & O_ACCMODE;
4904 switch (open_mode) {
4906 ret |= SMB_O_WRONLY;
4913 ret |= SMB_O_RDONLY;
4917 if (flags & O_CREAT) {
4920 if (flags & O_EXCL) {
4923 if (flags & O_TRUNC) {
4927 if (flags & O_SYNC) {
4931 if (flags & O_APPEND) {
4932 ret |= SMB_O_APPEND;
4934 #if defined(O_DIRECT)
4935 if (flags & O_DIRECT) {
4936 ret |= SMB_O_DIRECT;
4939 #if defined(O_DIRECTORY)
4940 if (flags & O_DIRECTORY) {
4941 ret |= SMB_O_DIRECTORY;
4947 /****************************************************************************
4948 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4949 ****************************************************************************/
4951 struct posix_open_state {
4955 uint16_t fnum; /* Out */
4958 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4960 struct tevent_req *req = tevent_req_callback_data(
4961 subreq, struct tevent_req);
4962 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4967 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4968 NULL, 0, NULL, &data, 12, &num_data);
4969 TALLOC_FREE(subreq);
4970 if (tevent_req_nterror(req, status)) {
4973 state->fnum = SVAL(data,2);
4974 tevent_req_done(req);
4977 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4978 struct tevent_context *ev,
4979 struct cli_state *cli,
4985 struct tevent_req *req = NULL, *subreq = NULL;
4986 struct posix_open_state *state = NULL;
4987 uint32_t wire_flags = open_flags_to_wire(flags);
4989 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4994 /* Setup setup word. */
4995 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4997 /* Setup param array. */
4998 state->param = talloc_array(state, uint8_t, 6);
4999 if (tevent_req_nomem(state->param, req)) {
5000 return tevent_req_post(req, ev);
5002 memset(state->param, '\0', 6);
5003 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5005 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5006 strlen(fname)+1, NULL);
5008 if (tevent_req_nomem(state->param, req)) {
5009 return tevent_req_post(req, ev);
5012 /* Setup data words. */
5014 wire_flags |= SMB_O_DIRECTORY;
5017 SIVAL(state->data,0,0); /* No oplock. */
5018 SIVAL(state->data,4,wire_flags);
5019 SIVAL(state->data,8,unix_perms_to_wire(mode));
5020 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5021 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5023 subreq = cli_trans_send(state, /* mem ctx. */
5024 ev, /* event ctx. */
5025 cli, /* cli_state. */
5026 0, /* additional_flags2 */
5027 SMBtrans2, /* cmd. */
5028 NULL, /* pipe name. */
5032 &state->setup, /* setup. */
5033 1, /* num setup uint16_t words. */
5034 0, /* max returned setup. */
5035 state->param, /* param. */
5036 talloc_get_size(state->param),/* num param. */
5037 2, /* max returned param. */
5038 state->data, /* data. */
5040 12); /* max returned data. */
5042 if (tevent_req_nomem(subreq, req)) {
5043 return tevent_req_post(req, ev);
5045 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5049 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5050 struct tevent_context *ev,
5051 struct cli_state *cli,
5056 return cli_posix_open_internal_send(mem_ctx, ev,
5057 cli, fname, flags, mode, false);
5060 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5062 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5065 if (tevent_req_is_nterror(req, &status)) {
5068 *pfnum = state->fnum;
5069 return NT_STATUS_OK;
5072 /****************************************************************************
5073 Open - POSIX semantics. Doesn't request oplock.
5074 ****************************************************************************/
5076 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5077 int flags, mode_t mode, uint16_t *pfnum)
5080 TALLOC_CTX *frame = talloc_stackframe();
5081 struct tevent_context *ev = NULL;
5082 struct tevent_req *req = NULL;
5083 NTSTATUS status = NT_STATUS_OK;
5085 if (smbXcli_conn_has_async_calls(cli->conn)) {
5087 * Can't use sync call while an async call is in flight
5089 status = NT_STATUS_INVALID_PARAMETER;
5093 ev = samba_tevent_context_init(frame);
5095 status = NT_STATUS_NO_MEMORY;
5099 req = cli_posix_open_send(frame,
5106 status = NT_STATUS_NO_MEMORY;
5110 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5114 status = cli_posix_open_recv(req, pfnum);
5121 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5122 struct tevent_context *ev,
5123 struct cli_state *cli,
5127 return cli_posix_open_internal_send(mem_ctx, ev,
5128 cli, fname, O_CREAT, mode, true);
5131 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5133 return tevent_req_simple_recv_ntstatus(req);
5136 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5138 TALLOC_CTX *frame = talloc_stackframe();
5139 struct tevent_context *ev = NULL;
5140 struct tevent_req *req = NULL;
5141 NTSTATUS status = NT_STATUS_OK;
5143 if (smbXcli_conn_has_async_calls(cli->conn)) {
5145 * Can't use sync call while an async call is in flight
5147 status = NT_STATUS_INVALID_PARAMETER;
5151 ev = samba_tevent_context_init(frame);
5153 status = NT_STATUS_NO_MEMORY;
5157 req = cli_posix_mkdir_send(frame,
5163 status = NT_STATUS_NO_MEMORY;
5167 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5171 status = cli_posix_mkdir_recv(req);
5178 /****************************************************************************
5179 unlink or rmdir - POSIX semantics.
5180 ****************************************************************************/
5182 struct cli_posix_unlink_internal_state {
5186 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5188 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5189 struct tevent_context *ev,
5190 struct cli_state *cli,
5194 struct tevent_req *req = NULL, *subreq = NULL;
5195 struct cli_posix_unlink_internal_state *state = NULL;
5197 req = tevent_req_create(mem_ctx, &state,
5198 struct cli_posix_unlink_internal_state);
5203 /* Setup data word. */
5204 SSVAL(state->data, 0, level);
5206 subreq = cli_setpathinfo_send(state, ev, cli,
5207 SMB_POSIX_PATH_UNLINK,
5209 state->data, sizeof(state->data));
5210 if (tevent_req_nomem(subreq, req)) {
5211 return tevent_req_post(req, ev);
5213 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5217 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5219 NTSTATUS status = cli_setpathinfo_recv(subreq);
5220 tevent_req_simple_finish_ntstatus(subreq, status);
5223 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5224 struct tevent_context *ev,
5225 struct cli_state *cli,
5228 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5229 SMB_POSIX_UNLINK_FILE_TARGET);
5232 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5234 return tevent_req_simple_recv_ntstatus(req);
5237 /****************************************************************************
5238 unlink - POSIX semantics.
5239 ****************************************************************************/
5241 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5243 TALLOC_CTX *frame = talloc_stackframe();
5244 struct tevent_context *ev = NULL;
5245 struct tevent_req *req = NULL;
5246 NTSTATUS status = NT_STATUS_OK;
5248 if (smbXcli_conn_has_async_calls(cli->conn)) {
5250 * Can't use sync call while an async call is in flight
5252 status = NT_STATUS_INVALID_PARAMETER;
5256 ev = samba_tevent_context_init(frame);
5258 status = NT_STATUS_NO_MEMORY;
5262 req = cli_posix_unlink_send(frame,
5267 status = NT_STATUS_NO_MEMORY;
5271 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5275 status = cli_posix_unlink_recv(req);
5282 /****************************************************************************
5283 rmdir - POSIX semantics.
5284 ****************************************************************************/
5286 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5287 struct tevent_context *ev,
5288 struct cli_state *cli,
5291 return cli_posix_unlink_internal_send(
5292 mem_ctx, ev, cli, fname,
5293 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5296 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5298 return tevent_req_simple_recv_ntstatus(req);
5301 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5303 TALLOC_CTX *frame = talloc_stackframe();
5304 struct tevent_context *ev = NULL;
5305 struct tevent_req *req = NULL;
5306 NTSTATUS status = NT_STATUS_OK;
5308 if (smbXcli_conn_has_async_calls(cli->conn)) {
5310 * Can't use sync call while an async call is in flight
5312 status = NT_STATUS_INVALID_PARAMETER;
5316 ev = samba_tevent_context_init(frame);
5318 status = NT_STATUS_NO_MEMORY;
5322 req = cli_posix_rmdir_send(frame,
5327 status = NT_STATUS_NO_MEMORY;
5331 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5335 status = cli_posix_rmdir_recv(req, frame);
5342 /****************************************************************************
5344 ****************************************************************************/
5346 struct cli_notify_state {
5348 uint32_t num_changes;
5349 struct notify_change *changes;
5352 static void cli_notify_done(struct tevent_req *subreq);
5354 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5355 struct tevent_context *ev,
5356 struct cli_state *cli, uint16_t fnum,
5357 uint32_t buffer_size,
5358 uint32_t completion_filter, bool recursive)
5360 struct tevent_req *req, *subreq;
5361 struct cli_notify_state *state;
5362 unsigned old_timeout;
5364 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5369 SIVAL(state->setup, 0, completion_filter);
5370 SSVAL(state->setup, 4, fnum);
5371 SSVAL(state->setup, 6, recursive);
5374 * Notifies should not time out
5376 old_timeout = cli_set_timeout(cli, 0);
5378 subreq = cli_trans_send(
5379 state, /* mem ctx. */
5380 ev, /* event ctx. */
5381 cli, /* cli_state. */
5382 0, /* additional_flags2 */
5383 SMBnttrans, /* cmd. */
5384 NULL, /* pipe name. */
5386 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5388 (uint16_t *)state->setup, /* setup. */
5389 4, /* num setup uint16_t words. */
5390 0, /* max returned setup. */
5393 buffer_size, /* max returned param. */
5396 0); /* max returned data. */
5398 cli_set_timeout(cli, old_timeout);
5400 if (tevent_req_nomem(subreq, req)) {
5401 return tevent_req_post(req, ev);
5403 tevent_req_set_callback(subreq, cli_notify_done, req);
5407 static void cli_notify_done(struct tevent_req *subreq)
5409 struct tevent_req *req = tevent_req_callback_data(
5410 subreq, struct tevent_req);
5411 struct cli_notify_state *state = tevent_req_data(
5412 req, struct cli_notify_state);
5415 uint32_t i, ofs, num_params;
5418 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5419 ¶ms, 0, &num_params, NULL, 0, NULL);
5420 TALLOC_FREE(subreq);
5421 if (tevent_req_nterror(req, status)) {
5422 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5426 state->num_changes = 0;
5429 while (num_params - ofs > 12) {
5430 uint32_t next = IVAL(params, ofs);
5431 state->num_changes += 1;
5433 if ((next == 0) || (ofs+next >= num_params)) {
5439 state->changes = talloc_array(state, struct notify_change,
5440 state->num_changes);
5441 if (tevent_req_nomem(state->changes, req)) {
5442 TALLOC_FREE(params);
5448 for (i=0; i<state->num_changes; i++) {
5449 uint32_t next = IVAL(params, ofs);
5450 uint32_t len = IVAL(params, ofs+8);
5454 if (trans_oob(num_params, ofs + 12, len)) {
5455 TALLOC_FREE(params);
5457 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5461 state->changes[i].action = IVAL(params, ofs+4);
5462 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5463 &name, params+ofs+12, len,
5464 STR_TERMINATE|STR_UNICODE);
5466 TALLOC_FREE(params);
5467 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5470 state->changes[i].name = name;
5474 TALLOC_FREE(params);
5475 tevent_req_done(req);
5478 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5479 uint32_t *pnum_changes,
5480 struct notify_change **pchanges)
5482 struct cli_notify_state *state = tevent_req_data(
5483 req, struct cli_notify_state);
5486 if (tevent_req_is_nterror(req, &status)) {
5490 *pnum_changes = state->num_changes;
5491 *pchanges = talloc_move(mem_ctx, &state->changes);
5492 return NT_STATUS_OK;
5495 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5496 uint32_t completion_filter, bool recursive,
5497 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5498 struct notify_change **pchanges)
5500 TALLOC_CTX *frame = talloc_stackframe();
5501 struct tevent_context *ev;
5502 struct tevent_req *req;
5503 NTSTATUS status = NT_STATUS_NO_MEMORY;
5505 if (smbXcli_conn_has_async_calls(cli->conn)) {
5507 * Can't use sync call while an async call is in flight
5509 status = NT_STATUS_INVALID_PARAMETER;
5512 ev = samba_tevent_context_init(frame);
5516 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5517 completion_filter, recursive);
5521 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5524 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5530 struct cli_qpathinfo_state {
5539 static void cli_qpathinfo_done(struct tevent_req *subreq);
5541 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5542 struct tevent_context *ev,
5543 struct cli_state *cli, const char *fname,
5544 uint16_t level, uint32_t min_rdata,
5547 struct tevent_req *req, *subreq;
5548 struct cli_qpathinfo_state *state;
5550 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5554 state->min_rdata = min_rdata;
5555 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5557 state->param = talloc_zero_array(state, uint8_t, 6);
5558 if (tevent_req_nomem(state->param, req)) {
5559 return tevent_req_post(req, ev);
5561 SSVAL(state->param, 0, level);
5562 state->param = trans2_bytes_push_str(
5563 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5564 if (tevent_req_nomem(state->param, req)) {
5565 return tevent_req_post(req, ev);
5568 subreq = cli_trans_send(
5569 state, /* mem ctx. */
5570 ev, /* event ctx. */
5571 cli, /* cli_state. */
5572 0, /* additional_flags2 */
5573 SMBtrans2, /* cmd. */
5574 NULL, /* pipe name. */
5578 state->setup, /* setup. */
5579 1, /* num setup uint16_t words. */
5580 0, /* max returned setup. */
5581 state->param, /* param. */
5582 talloc_get_size(state->param), /* num param. */
5583 2, /* max returned param. */
5586 max_rdata); /* max returned data. */
5588 if (tevent_req_nomem(subreq, req)) {
5589 return tevent_req_post(req, ev);
5591 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5595 static void cli_qpathinfo_done(struct tevent_req *subreq)
5597 struct tevent_req *req = tevent_req_callback_data(
5598 subreq, struct tevent_req);
5599 struct cli_qpathinfo_state *state = tevent_req_data(
5600 req, struct cli_qpathinfo_state);
5603 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5605 &state->rdata, state->min_rdata,
5607 if (tevent_req_nterror(req, status)) {
5610 tevent_req_done(req);
5613 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5614 uint8_t **rdata, uint32_t *num_rdata)
5616 struct cli_qpathinfo_state *state = tevent_req_data(
5617 req, struct cli_qpathinfo_state);
5620 if (tevent_req_is_nterror(req, &status)) {
5623 if (rdata != NULL) {
5624 *rdata = talloc_move(mem_ctx, &state->rdata);
5626 TALLOC_FREE(state->rdata);
5628 if (num_rdata != NULL) {
5629 *num_rdata = state->num_rdata;
5631 return NT_STATUS_OK;
5634 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5635 const char *fname, uint16_t level, uint32_t min_rdata,
5637 uint8_t **rdata, uint32_t *num_rdata)
5639 TALLOC_CTX *frame = talloc_stackframe();
5640 struct tevent_context *ev;
5641 struct tevent_req *req;
5642 NTSTATUS status = NT_STATUS_NO_MEMORY;
5644 if (smbXcli_conn_has_async_calls(cli->conn)) {
5646 * Can't use sync call while an async call is in flight
5648 status = NT_STATUS_INVALID_PARAMETER;
5651 ev = samba_tevent_context_init(frame);
5655 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5660 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5663 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5669 struct cli_qfileinfo_state {
5673 uint16_t recv_flags2;
5679 static void cli_qfileinfo_done(struct tevent_req *subreq);
5681 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5682 struct tevent_context *ev,
5683 struct cli_state *cli, uint16_t fnum,
5684 uint16_t level, uint32_t min_rdata,
5687 struct tevent_req *req, *subreq;
5688 struct cli_qfileinfo_state *state;
5690 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5694 state->min_rdata = min_rdata;
5695 SSVAL(state->param, 0, fnum);
5696 SSVAL(state->param, 2, level);
5697 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5699 subreq = cli_trans_send(
5700 state, /* mem ctx. */
5701 ev, /* event ctx. */
5702 cli, /* cli_state. */
5703 0, /* additional_flags2 */
5704 SMBtrans2, /* cmd. */
5705 NULL, /* pipe name. */
5709 state->setup, /* setup. */
5710 1, /* num setup uint16_t words. */
5711 0, /* max returned setup. */
5712 state->param, /* param. */
5713 sizeof(state->param), /* num param. */
5714 2, /* max returned param. */
5717 max_rdata); /* max returned data. */
5719 if (tevent_req_nomem(subreq, req)) {
5720 return tevent_req_post(req, ev);
5722 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5726 static void cli_qfileinfo_done(struct tevent_req *subreq)
5728 struct tevent_req *req = tevent_req_callback_data(
5729 subreq, struct tevent_req);
5730 struct cli_qfileinfo_state *state = tevent_req_data(
5731 req, struct cli_qfileinfo_state);
5734 status = cli_trans_recv(subreq, state,
5735 &state->recv_flags2,
5738 &state->rdata, state->min_rdata,
5740 if (tevent_req_nterror(req, status)) {
5743 tevent_req_done(req);
5746 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5747 uint16_t *recv_flags2,
5748 uint8_t **rdata, uint32_t *num_rdata)
5750 struct cli_qfileinfo_state *state = tevent_req_data(
5751 req, struct cli_qfileinfo_state);
5754 if (tevent_req_is_nterror(req, &status)) {
5758 if (recv_flags2 != NULL) {
5759 *recv_flags2 = state->recv_flags2;
5761 if (rdata != NULL) {
5762 *rdata = talloc_move(mem_ctx, &state->rdata);
5764 TALLOC_FREE(state->rdata);
5766 if (num_rdata != NULL) {
5767 *num_rdata = state->num_rdata;
5769 return NT_STATUS_OK;
5772 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5773 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5774 uint32_t max_rdata, uint16_t *recv_flags2,
5775 uint8_t **rdata, uint32_t *num_rdata)
5777 TALLOC_CTX *frame = talloc_stackframe();
5778 struct tevent_context *ev;
5779 struct tevent_req *req;
5780 NTSTATUS status = NT_STATUS_NO_MEMORY;
5782 if (smbXcli_conn_has_async_calls(cli->conn)) {
5784 * Can't use sync call while an async call is in flight
5786 status = NT_STATUS_INVALID_PARAMETER;
5789 ev = samba_tevent_context_init(frame);
5793 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5798 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5801 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5807 struct cli_flush_state {
5811 static void cli_flush_done(struct tevent_req *subreq);
5813 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5814 struct tevent_context *ev,
5815 struct cli_state *cli,
5818 struct tevent_req *req, *subreq;
5819 struct cli_flush_state *state;
5821 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5825 SSVAL(state->vwv + 0, 0, fnum);
5827 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5829 if (tevent_req_nomem(subreq, req)) {
5830 return tevent_req_post(req, ev);
5832 tevent_req_set_callback(subreq, cli_flush_done, req);
5836 static void cli_flush_done(struct tevent_req *subreq)
5838 struct tevent_req *req = tevent_req_callback_data(
5839 subreq, struct tevent_req);
5842 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5843 TALLOC_FREE(subreq);
5844 if (tevent_req_nterror(req, status)) {
5847 tevent_req_done(req);
5850 NTSTATUS cli_flush_recv(struct tevent_req *req)
5852 return tevent_req_simple_recv_ntstatus(req);
5855 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5857 TALLOC_CTX *frame = talloc_stackframe();
5858 struct tevent_context *ev;
5859 struct tevent_req *req;
5860 NTSTATUS status = NT_STATUS_NO_MEMORY;
5862 if (smbXcli_conn_has_async_calls(cli->conn)) {
5864 * Can't use sync call while an async call is in flight
5866 status = NT_STATUS_INVALID_PARAMETER;
5869 ev = samba_tevent_context_init(frame);
5873 req = cli_flush_send(frame, ev, cli, fnum);
5877 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5880 status = cli_flush_recv(req);
5886 struct cli_shadow_copy_data_state {
5893 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5895 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5896 struct tevent_context *ev,
5897 struct cli_state *cli,
5901 struct tevent_req *req, *subreq;
5902 struct cli_shadow_copy_data_state *state;
5905 req = tevent_req_create(mem_ctx, &state,
5906 struct cli_shadow_copy_data_state);
5910 state->get_names = get_names;
5911 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5913 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5914 SSVAL(state->setup + 2, 0, fnum);
5915 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5916 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5918 subreq = cli_trans_send(
5919 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5920 state->setup, ARRAY_SIZE(state->setup), 0,
5923 if (tevent_req_nomem(subreq, req)) {
5924 return tevent_req_post(req, ev);
5926 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5930 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5932 struct tevent_req *req = tevent_req_callback_data(
5933 subreq, struct tevent_req);
5934 struct cli_shadow_copy_data_state *state = tevent_req_data(
5935 req, struct cli_shadow_copy_data_state);
5938 status = cli_trans_recv(subreq, state, NULL,
5939 NULL, 0, NULL, /* setup */
5940 NULL, 0, NULL, /* param */
5941 &state->data, 12, &state->num_data);
5942 TALLOC_FREE(subreq);
5943 if (tevent_req_nterror(req, status)) {
5946 tevent_req_done(req);
5949 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5950 char ***pnames, int *pnum_names)
5952 struct cli_shadow_copy_data_state *state = tevent_req_data(
5953 req, struct cli_shadow_copy_data_state);
5959 if (tevent_req_is_nterror(req, &status)) {
5962 num_names = IVAL(state->data, 4);
5963 dlength = IVAL(state->data, 8);
5965 if (!state->get_names) {
5966 *pnum_names = num_names;
5967 return NT_STATUS_OK;
5970 if (dlength+12 > state->num_data) {
5971 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5973 names = talloc_array(mem_ctx, char *, num_names);
5974 if (names == NULL) {
5975 return NT_STATUS_NO_MEMORY;
5978 for (i=0; i<num_names; i++) {
5981 size_t converted_size;
5983 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5984 ret = convert_string_talloc(
5985 names, CH_UTF16LE, CH_UNIX,
5986 src, 2 * sizeof(SHADOW_COPY_LABEL),
5987 &names[i], &converted_size);
5990 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5993 *pnum_names = num_names;
5995 return NT_STATUS_OK;
5998 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5999 uint16_t fnum, bool get_names,
6000 char ***pnames, int *pnum_names)
6002 TALLOC_CTX *frame = talloc_stackframe();
6003 struct tevent_context *ev;
6004 struct tevent_req *req;
6005 NTSTATUS status = NT_STATUS_NO_MEMORY;
6007 if (smbXcli_conn_has_async_calls(cli->conn)) {
6009 * Can't use sync call while an async call is in flight
6011 status = NT_STATUS_INVALID_PARAMETER;
6014 ev = samba_tevent_context_init(frame);
6018 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6022 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6025 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);