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 "../lib/util/tevent_ntstatus.h"
24 #include "async_smb.h"
25 #include "libsmb/clirap.h"
29 /***********************************************************
30 Common function for pushing stings, used by smb_bytes_push_str()
31 and trans_bytes_push_str(). Only difference is the align_odd
33 ***********************************************************/
35 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
36 const char *str, size_t str_len,
38 size_t *pconverted_size)
42 size_t converted_size;
48 buflen = talloc_get_size(buf);
50 if (align_odd && ucs2 && (buflen % 2 == 0)) {
52 * We're pushing into an SMB buffer, align odd
54 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
62 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
63 ucs2 ? CH_UTF16LE : CH_DOS,
64 str, str_len, &converted,
69 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
70 buflen + converted_size);
72 TALLOC_FREE(converted);
76 memcpy(buf + buflen, converted, converted_size);
78 TALLOC_FREE(converted);
80 if (pconverted_size) {
81 *pconverted_size = converted_size;
87 /***********************************************************
88 Push a string into an SMB buffer, with odd byte alignment
89 if it's a UCS2 string.
90 ***********************************************************/
92 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
93 const char *str, size_t str_len,
94 size_t *pconverted_size)
96 return internal_bytes_push_str(buf, ucs2, str, str_len,
97 true, pconverted_size);
100 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
101 const uint8_t *bytes, size_t num_bytes)
108 buflen = talloc_get_size(buf);
110 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
111 buflen + 1 + num_bytes);
115 buf[buflen] = prefix;
116 memcpy(&buf[buflen+1], bytes, num_bytes);
120 /***********************************************************
121 Same as smb_bytes_push_str(), but without the odd byte
122 align for ucs2 (we're pushing into a param or data block).
123 static for now, although this will probably change when
124 other modules use async trans calls.
125 ***********************************************************/
127 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
128 const char *str, size_t str_len,
129 size_t *pconverted_size)
131 return internal_bytes_push_str(buf, ucs2, str, str_len,
132 false, pconverted_size);
135 struct cli_setpathinfo_state {
140 static void cli_setpathinfo_done(struct tevent_req *subreq);
142 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
143 struct tevent_context *ev,
144 struct cli_state *cli,
150 struct tevent_req *req, *subreq;
151 struct cli_setpathinfo_state *state;
153 req = tevent_req_create(mem_ctx, &state,
154 struct cli_setpathinfo_state);
159 /* Setup setup word. */
160 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
162 /* Setup param array. */
163 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
164 if (tevent_req_nomem(state->param, req)) {
165 return tevent_req_post(req, ev);
167 SSVAL(state->param, 0, level);
169 state->param = trans2_bytes_push_str(
170 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
171 if (tevent_req_nomem(state->param, req)) {
172 return tevent_req_post(req, ev);
175 subreq = cli_trans_send(
176 state, /* mem ctx. */
178 cli, /* cli_state. */
179 SMBtrans2, /* cmd. */
180 NULL, /* pipe name. */
184 &state->setup, /* setup. */
185 1, /* num setup uint16_t words. */
186 0, /* max returned setup. */
187 state->param, /* param. */
188 talloc_get_size(state->param), /* num param. */
189 2, /* max returned param. */
191 data_len, /* num data. */
192 0); /* max returned data. */
194 if (tevent_req_nomem(subreq, req)) {
195 return tevent_req_post(req, ev);
197 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
201 static void cli_setpathinfo_done(struct tevent_req *subreq)
203 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
204 NULL, 0, NULL, NULL, 0, NULL);
205 tevent_req_simple_finish_ntstatus(subreq, status);
208 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
210 return tevent_req_simple_recv_ntstatus(req);
213 NTSTATUS cli_setpathinfo(struct cli_state *cli,
219 TALLOC_CTX *frame = talloc_stackframe();
220 struct tevent_context *ev;
221 struct tevent_req *req;
222 NTSTATUS status = NT_STATUS_NO_MEMORY;
224 if (cli_has_async_calls(cli)) {
226 * Can't use sync call while an async call is in flight
228 status = NT_STATUS_INVALID_PARAMETER;
231 ev = tevent_context_init(frame);
235 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
239 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
242 status = cli_setpathinfo_recv(req);
248 /****************************************************************************
249 Hard/Symlink a file (UNIX extensions).
250 Creates new name (sym)linked to oldname.
251 ****************************************************************************/
253 struct cli_posix_link_internal_state {
257 static void cli_posix_link_internal_done(struct tevent_req *subreq);
259 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
260 struct event_context *ev,
261 struct cli_state *cli,
266 struct tevent_req *req = NULL, *subreq = NULL;
267 struct cli_posix_link_internal_state *state = NULL;
269 req = tevent_req_create(mem_ctx, &state,
270 struct cli_posix_link_internal_state);
275 /* Setup data array. */
276 state->data = talloc_array(state, uint8_t, 0);
277 if (tevent_req_nomem(state->data, req)) {
278 return tevent_req_post(req, ev);
280 state->data = trans2_bytes_push_str(
281 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
283 subreq = cli_setpathinfo_send(
284 state, ev, cli, level, newname,
285 state->data, talloc_get_size(state->data));
286 if (tevent_req_nomem(subreq, req)) {
287 return tevent_req_post(req, ev);
289 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
293 static void cli_posix_link_internal_done(struct tevent_req *subreq)
295 NTSTATUS status = cli_setpathinfo_recv(subreq);
296 tevent_req_simple_finish_ntstatus(subreq, status);
299 /****************************************************************************
300 Symlink a file (UNIX extensions).
301 ****************************************************************************/
303 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
304 struct event_context *ev,
305 struct cli_state *cli,
309 return cli_posix_link_internal_send(
310 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
313 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
315 return tevent_req_simple_recv_ntstatus(req);
318 NTSTATUS cli_posix_symlink(struct cli_state *cli,
322 TALLOC_CTX *frame = talloc_stackframe();
323 struct event_context *ev = NULL;
324 struct tevent_req *req = NULL;
325 NTSTATUS status = NT_STATUS_OK;
327 if (cli_has_async_calls(cli)) {
329 * Can't use sync call while an async call is in flight
331 status = NT_STATUS_INVALID_PARAMETER;
335 ev = event_context_init(frame);
337 status = NT_STATUS_NO_MEMORY;
341 req = cli_posix_symlink_send(frame,
347 status = NT_STATUS_NO_MEMORY;
351 if (!tevent_req_poll(req, ev)) {
352 status = map_nt_error_from_unix(errno);
356 status = cli_posix_symlink_recv(req);
360 if (!NT_STATUS_IS_OK(status)) {
361 cli_set_error(cli, status);
366 /****************************************************************************
367 Read a POSIX symlink.
368 ****************************************************************************/
370 struct readlink_state {
375 static void cli_posix_readlink_done(struct tevent_req *subreq);
377 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
378 struct event_context *ev,
379 struct cli_state *cli,
383 struct tevent_req *req = NULL, *subreq = NULL;
384 struct readlink_state *state = NULL;
385 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
387 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
393 * Len is in bytes, we need it in UCS2 units.
395 if ((2*len < len) || (maxbytelen < len)) {
396 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
397 return tevent_req_post(req, ev);
400 subreq = cli_qpathinfo_send(state, ev, cli, fname,
401 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
402 if (tevent_req_nomem(subreq, req)) {
403 return tevent_req_post(req, ev);
405 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
409 static void cli_posix_readlink_done(struct tevent_req *subreq)
411 struct tevent_req *req = tevent_req_callback_data(
412 subreq, struct tevent_req);
413 struct readlink_state *state = tevent_req_data(
414 req, struct readlink_state);
417 status = cli_qpathinfo_recv(subreq, state, &state->data,
420 if (!NT_STATUS_IS_OK(status)) {
421 tevent_req_nterror(req, status);
425 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
427 if (state->data[state->num_data-1] != '\0') {
428 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
431 tevent_req_done(req);
434 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
435 char *retpath, size_t len)
438 char *converted = NULL;
439 size_t converted_size = 0;
440 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
442 if (tevent_req_is_nterror(req, &status)) {
445 /* The returned data is a pushed string, not raw data. */
446 if (!convert_string_talloc(state,
447 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
453 return NT_STATUS_NO_MEMORY;
456 len = MIN(len,converted_size);
458 return NT_STATUS_DATA_ERROR;
460 memcpy(retpath, converted, len);
464 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
465 char *linkpath, size_t len)
467 TALLOC_CTX *frame = talloc_stackframe();
468 struct event_context *ev = NULL;
469 struct tevent_req *req = NULL;
470 NTSTATUS status = NT_STATUS_OK;
472 if (cli_has_async_calls(cli)) {
474 * Can't use sync call while an async call is in flight
476 status = NT_STATUS_INVALID_PARAMETER;
480 ev = event_context_init(frame);
482 status = NT_STATUS_NO_MEMORY;
486 req = cli_posix_readlink_send(frame,
492 status = NT_STATUS_NO_MEMORY;
496 if (!tevent_req_poll(req, ev)) {
497 status = map_nt_error_from_unix(errno);
501 status = cli_posix_readlink_recv(req, cli, linkpath, len);
505 if (!NT_STATUS_IS_OK(status)) {
506 cli_set_error(cli, status);
511 /****************************************************************************
512 Hard link a file (UNIX extensions).
513 ****************************************************************************/
515 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
516 struct event_context *ev,
517 struct cli_state *cli,
521 return cli_posix_link_internal_send(
522 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
525 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
527 return tevent_req_simple_recv_ntstatus(req);
530 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
534 TALLOC_CTX *frame = talloc_stackframe();
535 struct event_context *ev = NULL;
536 struct tevent_req *req = NULL;
537 NTSTATUS status = NT_STATUS_OK;
539 if (cli_has_async_calls(cli)) {
541 * Can't use sync call while an async call is in flight
543 status = NT_STATUS_INVALID_PARAMETER;
547 ev = event_context_init(frame);
549 status = NT_STATUS_NO_MEMORY;
553 req = cli_posix_hardlink_send(frame,
559 status = NT_STATUS_NO_MEMORY;
563 if (!tevent_req_poll(req, ev)) {
564 status = map_nt_error_from_unix(errno);
568 status = cli_posix_hardlink_recv(req);
572 if (!NT_STATUS_IS_OK(status)) {
573 cli_set_error(cli, status);
578 /****************************************************************************
579 Do a POSIX getfacl (UNIX extensions).
580 ****************************************************************************/
582 struct getfacl_state {
587 static void cli_posix_getfacl_done(struct tevent_req *subreq);
589 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
590 struct event_context *ev,
591 struct cli_state *cli,
594 struct tevent_req *req = NULL, *subreq = NULL;
595 struct getfacl_state *state = NULL;
597 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
601 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
603 if (tevent_req_nomem(subreq, req)) {
604 return tevent_req_post(req, ev);
606 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
610 static void cli_posix_getfacl_done(struct tevent_req *subreq)
612 struct tevent_req *req = tevent_req_callback_data(
613 subreq, struct tevent_req);
614 struct getfacl_state *state = tevent_req_data(
615 req, struct getfacl_state);
618 status = cli_qpathinfo_recv(subreq, state, &state->data,
621 if (!NT_STATUS_IS_OK(status)) {
622 tevent_req_nterror(req, status);
625 tevent_req_done(req);
628 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
633 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
636 if (tevent_req_is_nterror(req, &status)) {
639 *prb_size = (size_t)state->num_data;
640 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
644 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
650 TALLOC_CTX *frame = talloc_stackframe();
651 struct event_context *ev = NULL;
652 struct tevent_req *req = NULL;
653 NTSTATUS status = NT_STATUS_OK;
655 if (cli_has_async_calls(cli)) {
657 * Can't use sync call while an async call is in flight
659 status = NT_STATUS_INVALID_PARAMETER;
663 ev = event_context_init(frame);
665 status = NT_STATUS_NO_MEMORY;
669 req = cli_posix_getfacl_send(frame,
674 status = NT_STATUS_NO_MEMORY;
678 if (!tevent_req_poll(req, ev)) {
679 status = map_nt_error_from_unix(errno);
683 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
687 if (!NT_STATUS_IS_OK(status)) {
688 cli_set_error(cli, status);
693 /****************************************************************************
694 Stat a file (UNIX extensions).
695 ****************************************************************************/
702 static void cli_posix_stat_done(struct tevent_req *subreq);
704 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
705 struct event_context *ev,
706 struct cli_state *cli,
709 struct tevent_req *req = NULL, *subreq = NULL;
710 struct stat_state *state = NULL;
712 req = tevent_req_create(mem_ctx, &state, struct stat_state);
716 subreq = cli_qpathinfo_send(state, ev, cli, fname,
717 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
718 if (tevent_req_nomem(subreq, req)) {
719 return tevent_req_post(req, ev);
721 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
725 static void cli_posix_stat_done(struct tevent_req *subreq)
727 struct tevent_req *req = tevent_req_callback_data(
728 subreq, struct tevent_req);
729 struct stat_state *state = tevent_req_data(req, struct stat_state);
732 status = cli_qpathinfo_recv(subreq, state, &state->data,
735 if (!NT_STATUS_IS_OK(status)) {
736 tevent_req_nterror(req, status);
739 tevent_req_done(req);
742 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
743 SMB_STRUCT_STAT *sbuf)
745 struct stat_state *state = tevent_req_data(req, struct stat_state);
748 if (tevent_req_is_nterror(req, &status)) {
752 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
753 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
754 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
755 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
757 /* assume 512 byte blocks */
758 sbuf->st_ex_blocks /= 512;
760 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
761 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
762 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
764 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
765 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
766 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
767 #if defined(HAVE_MAKEDEV)
769 uint32_t dev_major = IVAL(state->data,60);
770 uint32_t dev_minor = IVAL(state->data,68);
771 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
774 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
775 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
776 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
781 NTSTATUS cli_posix_stat(struct cli_state *cli,
783 SMB_STRUCT_STAT *sbuf)
785 TALLOC_CTX *frame = talloc_stackframe();
786 struct event_context *ev = NULL;
787 struct tevent_req *req = NULL;
788 NTSTATUS status = NT_STATUS_OK;
790 if (cli_has_async_calls(cli)) {
792 * Can't use sync call while an async call is in flight
794 status = NT_STATUS_INVALID_PARAMETER;
798 ev = event_context_init(frame);
800 status = NT_STATUS_NO_MEMORY;
804 req = cli_posix_stat_send(frame,
809 status = NT_STATUS_NO_MEMORY;
813 if (!tevent_req_poll(req, ev)) {
814 status = map_nt_error_from_unix(errno);
818 status = cli_posix_stat_recv(req, sbuf);
822 if (!NT_STATUS_IS_OK(status)) {
823 cli_set_error(cli, status);
828 /****************************************************************************
829 Chmod or chown a file internal (UNIX extensions).
830 ****************************************************************************/
832 struct cli_posix_chown_chmod_internal_state {
836 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
838 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
839 struct event_context *ev,
840 struct cli_state *cli,
846 struct tevent_req *req = NULL, *subreq = NULL;
847 struct cli_posix_chown_chmod_internal_state *state = NULL;
849 req = tevent_req_create(mem_ctx, &state,
850 struct cli_posix_chown_chmod_internal_state);
855 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
856 memset(&state->data[40], '\0', 60);
857 SIVAL(state->data,40,uid);
858 SIVAL(state->data,48,gid);
859 SIVAL(state->data,84,mode);
861 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
862 fname, state->data, sizeof(state->data));
863 if (tevent_req_nomem(subreq, req)) {
864 return tevent_req_post(req, ev);
866 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
871 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
873 NTSTATUS status = cli_setpathinfo_recv(subreq);
874 tevent_req_simple_finish_ntstatus(subreq, status);
877 /****************************************************************************
878 chmod a file (UNIX extensions).
879 ****************************************************************************/
881 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
882 struct event_context *ev,
883 struct cli_state *cli,
887 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
889 unix_perms_to_wire(mode),
894 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
896 return tevent_req_simple_recv_ntstatus(req);
899 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
901 TALLOC_CTX *frame = talloc_stackframe();
902 struct event_context *ev = NULL;
903 struct tevent_req *req = NULL;
904 NTSTATUS status = NT_STATUS_OK;
906 if (cli_has_async_calls(cli)) {
908 * Can't use sync call while an async call is in flight
910 status = NT_STATUS_INVALID_PARAMETER;
914 ev = event_context_init(frame);
916 status = NT_STATUS_NO_MEMORY;
920 req = cli_posix_chmod_send(frame,
926 status = NT_STATUS_NO_MEMORY;
930 if (!tevent_req_poll(req, ev)) {
931 status = map_nt_error_from_unix(errno);
935 status = cli_posix_chmod_recv(req);
939 if (!NT_STATUS_IS_OK(status)) {
940 cli_set_error(cli, status);
945 /****************************************************************************
946 chown a file (UNIX extensions).
947 ****************************************************************************/
949 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
950 struct event_context *ev,
951 struct cli_state *cli,
956 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
963 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
965 return tevent_req_simple_recv_ntstatus(req);
968 NTSTATUS cli_posix_chown(struct cli_state *cli,
973 TALLOC_CTX *frame = talloc_stackframe();
974 struct event_context *ev = NULL;
975 struct tevent_req *req = NULL;
976 NTSTATUS status = NT_STATUS_OK;
978 if (cli_has_async_calls(cli)) {
980 * Can't use sync call while an async call is in flight
982 status = NT_STATUS_INVALID_PARAMETER;
986 ev = event_context_init(frame);
988 status = NT_STATUS_NO_MEMORY;
992 req = cli_posix_chown_send(frame,
999 status = NT_STATUS_NO_MEMORY;
1003 if (!tevent_req_poll(req, ev)) {
1004 status = map_nt_error_from_unix(errno);
1008 status = cli_posix_chown_recv(req);
1012 if (!NT_STATUS_IS_OK(status)) {
1013 cli_set_error(cli, status);
1018 /****************************************************************************
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req *subreq);
1024 struct cli_rename_state {
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029 struct event_context *ev,
1030 struct cli_state *cli,
1031 const char *fname_src,
1032 const char *fname_dst)
1034 struct tevent_req *req = NULL, *subreq = NULL;
1035 struct cli_rename_state *state = NULL;
1036 uint8_t additional_flags = 0;
1037 uint8_t *bytes = NULL;
1039 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1044 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1046 bytes = talloc_array(state, uint8_t, 1);
1047 if (tevent_req_nomem(bytes, req)) {
1048 return tevent_req_post(req, ev);
1051 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1052 strlen(fname_src)+1, NULL);
1053 if (tevent_req_nomem(bytes, req)) {
1054 return tevent_req_post(req, ev);
1057 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1058 talloc_get_size(bytes)+1);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 bytes[talloc_get_size(bytes)-1] = 4;
1064 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1065 strlen(fname_dst)+1, NULL);
1066 if (tevent_req_nomem(bytes, req)) {
1067 return tevent_req_post(req, ev);
1070 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071 1, state->vwv, talloc_get_size(bytes), bytes);
1072 if (tevent_req_nomem(subreq, req)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, cli_rename_done, req);
1079 static void cli_rename_done(struct tevent_req *subreq)
1081 struct tevent_req *req = tevent_req_callback_data(
1082 subreq, struct tevent_req);
1085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086 TALLOC_FREE(subreq);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 tevent_req_nterror(req, status);
1091 tevent_req_done(req);
1094 NTSTATUS cli_rename_recv(struct tevent_req *req)
1096 return tevent_req_simple_recv_ntstatus(req);
1099 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1101 TALLOC_CTX *frame = talloc_stackframe();
1102 struct event_context *ev;
1103 struct tevent_req *req;
1104 NTSTATUS status = NT_STATUS_OK;
1106 if (cli_has_async_calls(cli)) {
1108 * Can't use sync call while an async call is in flight
1110 status = NT_STATUS_INVALID_PARAMETER;
1114 ev = event_context_init(frame);
1116 status = NT_STATUS_NO_MEMORY;
1120 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1122 status = NT_STATUS_NO_MEMORY;
1126 if (!tevent_req_poll(req, ev)) {
1127 status = map_nt_error_from_unix(errno);
1131 status = cli_rename_recv(req);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 cli_set_error(cli, status);
1141 /****************************************************************************
1143 ****************************************************************************/
1145 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1147 struct cli_ntrename_internal_state {
1151 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1152 struct event_context *ev,
1153 struct cli_state *cli,
1154 const char *fname_src,
1155 const char *fname_dst,
1156 uint16_t rename_flag)
1158 struct tevent_req *req = NULL, *subreq = NULL;
1159 struct cli_ntrename_internal_state *state = NULL;
1160 uint8_t additional_flags = 0;
1161 uint8_t *bytes = NULL;
1163 req = tevent_req_create(mem_ctx, &state,
1164 struct cli_ntrename_internal_state);
1169 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1170 SSVAL(state->vwv+1, 0, rename_flag);
1172 bytes = talloc_array(state, uint8_t, 1);
1173 if (tevent_req_nomem(bytes, req)) {
1174 return tevent_req_post(req, ev);
1177 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1178 strlen(fname_src)+1, NULL);
1179 if (tevent_req_nomem(bytes, req)) {
1180 return tevent_req_post(req, ev);
1183 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1184 talloc_get_size(bytes)+1);
1185 if (tevent_req_nomem(bytes, req)) {
1186 return tevent_req_post(req, ev);
1189 bytes[talloc_get_size(bytes)-1] = 4;
1190 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1191 strlen(fname_dst)+1, NULL);
1192 if (tevent_req_nomem(bytes, req)) {
1193 return tevent_req_post(req, ev);
1196 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1197 4, state->vwv, talloc_get_size(bytes), bytes);
1198 if (tevent_req_nomem(subreq, req)) {
1199 return tevent_req_post(req, ev);
1201 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1205 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1207 struct tevent_req *req = tevent_req_callback_data(
1208 subreq, struct tevent_req);
1211 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1212 TALLOC_FREE(subreq);
1213 if (!NT_STATUS_IS_OK(status)) {
1214 tevent_req_nterror(req, status);
1217 tevent_req_done(req);
1220 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1222 return tevent_req_simple_recv_ntstatus(req);
1225 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1226 struct event_context *ev,
1227 struct cli_state *cli,
1228 const char *fname_src,
1229 const char *fname_dst)
1231 return cli_ntrename_internal_send(mem_ctx,
1236 RENAME_FLAG_RENAME);
1239 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1241 return cli_ntrename_internal_recv(req);
1244 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1246 TALLOC_CTX *frame = talloc_stackframe();
1247 struct event_context *ev;
1248 struct tevent_req *req;
1249 NTSTATUS status = NT_STATUS_OK;
1251 if (cli_has_async_calls(cli)) {
1253 * Can't use sync call while an async call is in flight
1255 status = NT_STATUS_INVALID_PARAMETER;
1259 ev = event_context_init(frame);
1261 status = NT_STATUS_NO_MEMORY;
1265 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1267 status = NT_STATUS_NO_MEMORY;
1271 if (!tevent_req_poll(req, ev)) {
1272 status = map_nt_error_from_unix(errno);
1276 status = cli_ntrename_recv(req);
1280 if (!NT_STATUS_IS_OK(status)) {
1281 cli_set_error(cli, status);
1286 /****************************************************************************
1288 ****************************************************************************/
1290 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1291 struct event_context *ev,
1292 struct cli_state *cli,
1293 const char *fname_src,
1294 const char *fname_dst)
1296 return cli_ntrename_internal_send(mem_ctx,
1301 RENAME_FLAG_HARD_LINK);
1304 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1306 return cli_ntrename_internal_recv(req);
1309 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1311 TALLOC_CTX *frame = talloc_stackframe();
1312 struct event_context *ev;
1313 struct tevent_req *req;
1314 NTSTATUS status = NT_STATUS_OK;
1316 if (cli_has_async_calls(cli)) {
1318 * Can't use sync call while an async call is in flight
1320 status = NT_STATUS_INVALID_PARAMETER;
1324 ev = event_context_init(frame);
1326 status = NT_STATUS_NO_MEMORY;
1330 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1332 status = NT_STATUS_NO_MEMORY;
1336 if (!tevent_req_poll(req, ev)) {
1337 status = map_nt_error_from_unix(errno);
1341 status = cli_nt_hardlink_recv(req);
1345 if (!NT_STATUS_IS_OK(status)) {
1346 cli_set_error(cli, status);
1351 /****************************************************************************
1353 ****************************************************************************/
1355 static void cli_unlink_done(struct tevent_req *subreq);
1357 struct cli_unlink_state {
1361 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1362 struct event_context *ev,
1363 struct cli_state *cli,
1365 uint16_t mayhave_attrs)
1367 struct tevent_req *req = NULL, *subreq = NULL;
1368 struct cli_unlink_state *state = NULL;
1369 uint8_t additional_flags = 0;
1370 uint8_t *bytes = NULL;
1372 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1377 SSVAL(state->vwv+0, 0, mayhave_attrs);
1379 bytes = talloc_array(state, uint8_t, 1);
1380 if (tevent_req_nomem(bytes, req)) {
1381 return tevent_req_post(req, ev);
1384 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1385 strlen(fname)+1, NULL);
1387 if (tevent_req_nomem(bytes, req)) {
1388 return tevent_req_post(req, ev);
1391 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1392 1, state->vwv, talloc_get_size(bytes), bytes);
1393 if (tevent_req_nomem(subreq, req)) {
1394 return tevent_req_post(req, ev);
1396 tevent_req_set_callback(subreq, cli_unlink_done, req);
1400 static void cli_unlink_done(struct tevent_req *subreq)
1402 struct tevent_req *req = tevent_req_callback_data(
1403 subreq, struct tevent_req);
1406 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1407 TALLOC_FREE(subreq);
1408 if (!NT_STATUS_IS_OK(status)) {
1409 tevent_req_nterror(req, status);
1412 tevent_req_done(req);
1415 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1417 return tevent_req_simple_recv_ntstatus(req);
1420 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1422 TALLOC_CTX *frame = talloc_stackframe();
1423 struct event_context *ev;
1424 struct tevent_req *req;
1425 NTSTATUS status = NT_STATUS_OK;
1427 if (cli_has_async_calls(cli)) {
1429 * Can't use sync call while an async call is in flight
1431 status = NT_STATUS_INVALID_PARAMETER;
1435 ev = event_context_init(frame);
1437 status = NT_STATUS_NO_MEMORY;
1441 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1443 status = NT_STATUS_NO_MEMORY;
1447 if (!tevent_req_poll(req, ev)) {
1448 status = map_nt_error_from_unix(errno);
1452 status = cli_unlink_recv(req);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 cli_set_error(cli, status);
1462 /****************************************************************************
1464 ****************************************************************************/
1466 static void cli_mkdir_done(struct tevent_req *subreq);
1468 struct cli_mkdir_state {
1472 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1473 struct event_context *ev,
1474 struct cli_state *cli,
1477 struct tevent_req *req = NULL, *subreq = NULL;
1478 struct cli_mkdir_state *state = NULL;
1479 uint8_t additional_flags = 0;
1480 uint8_t *bytes = NULL;
1482 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1487 bytes = talloc_array(state, uint8_t, 1);
1488 if (tevent_req_nomem(bytes, req)) {
1489 return tevent_req_post(req, ev);
1492 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1493 strlen(dname)+1, NULL);
1495 if (tevent_req_nomem(bytes, req)) {
1496 return tevent_req_post(req, ev);
1499 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1500 0, NULL, talloc_get_size(bytes), bytes);
1501 if (tevent_req_nomem(subreq, req)) {
1502 return tevent_req_post(req, ev);
1504 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1508 static void cli_mkdir_done(struct tevent_req *subreq)
1510 struct tevent_req *req = tevent_req_callback_data(
1511 subreq, struct tevent_req);
1514 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1515 TALLOC_FREE(subreq);
1516 if (!NT_STATUS_IS_OK(status)) {
1517 tevent_req_nterror(req, status);
1520 tevent_req_done(req);
1523 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1525 return tevent_req_simple_recv_ntstatus(req);
1528 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1530 TALLOC_CTX *frame = talloc_stackframe();
1531 struct event_context *ev;
1532 struct tevent_req *req;
1533 NTSTATUS status = NT_STATUS_OK;
1535 if (cli_has_async_calls(cli)) {
1537 * Can't use sync call while an async call is in flight
1539 status = NT_STATUS_INVALID_PARAMETER;
1543 ev = event_context_init(frame);
1545 status = NT_STATUS_NO_MEMORY;
1549 req = cli_mkdir_send(frame, ev, cli, dname);
1551 status = NT_STATUS_NO_MEMORY;
1555 if (!tevent_req_poll(req, ev)) {
1556 status = map_nt_error_from_unix(errno);
1560 status = cli_mkdir_recv(req);
1564 if (!NT_STATUS_IS_OK(status)) {
1565 cli_set_error(cli, status);
1570 /****************************************************************************
1572 ****************************************************************************/
1574 static void cli_rmdir_done(struct tevent_req *subreq);
1576 struct cli_rmdir_state {
1580 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1581 struct event_context *ev,
1582 struct cli_state *cli,
1585 struct tevent_req *req = NULL, *subreq = NULL;
1586 struct cli_rmdir_state *state = NULL;
1587 uint8_t additional_flags = 0;
1588 uint8_t *bytes = NULL;
1590 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1595 bytes = talloc_array(state, uint8_t, 1);
1596 if (tevent_req_nomem(bytes, req)) {
1597 return tevent_req_post(req, ev);
1600 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1601 strlen(dname)+1, NULL);
1603 if (tevent_req_nomem(bytes, req)) {
1604 return tevent_req_post(req, ev);
1607 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1608 0, NULL, talloc_get_size(bytes), bytes);
1609 if (tevent_req_nomem(subreq, req)) {
1610 return tevent_req_post(req, ev);
1612 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1616 static void cli_rmdir_done(struct tevent_req *subreq)
1618 struct tevent_req *req = tevent_req_callback_data(
1619 subreq, struct tevent_req);
1622 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1623 TALLOC_FREE(subreq);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 tevent_req_nterror(req, status);
1628 tevent_req_done(req);
1631 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1633 return tevent_req_simple_recv_ntstatus(req);
1636 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1638 TALLOC_CTX *frame = talloc_stackframe();
1639 struct event_context *ev;
1640 struct tevent_req *req;
1641 NTSTATUS status = NT_STATUS_OK;
1643 if (cli_has_async_calls(cli)) {
1645 * Can't use sync call while an async call is in flight
1647 status = NT_STATUS_INVALID_PARAMETER;
1651 ev = event_context_init(frame);
1653 status = NT_STATUS_NO_MEMORY;
1657 req = cli_rmdir_send(frame, ev, cli, dname);
1659 status = NT_STATUS_NO_MEMORY;
1663 if (!tevent_req_poll(req, ev)) {
1664 status = map_nt_error_from_unix(errno);
1668 status = cli_rmdir_recv(req);
1672 if (!NT_STATUS_IS_OK(status)) {
1673 cli_set_error(cli, status);
1678 /****************************************************************************
1679 Set or clear the delete on close flag.
1680 ****************************************************************************/
1688 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1690 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1691 NULL, 0, NULL, NULL, 0, NULL);
1692 tevent_req_simple_finish_ntstatus(subreq, status);
1695 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1696 struct event_context *ev,
1697 struct cli_state *cli,
1701 struct tevent_req *req = NULL, *subreq = NULL;
1702 struct doc_state *state = NULL;
1704 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1709 /* Setup setup word. */
1710 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1712 /* Setup param array. */
1713 SSVAL(state->param,0,fnum);
1714 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1716 /* Setup data array. */
1717 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1719 subreq = cli_trans_send(state, /* mem ctx. */
1720 ev, /* event ctx. */
1721 cli, /* cli_state. */
1722 SMBtrans2, /* cmd. */
1723 NULL, /* pipe name. */
1727 &state->setup, /* setup. */
1728 1, /* num setup uint16_t words. */
1729 0, /* max returned setup. */
1730 state->param, /* param. */
1732 2, /* max returned param. */
1733 state->data, /* data. */
1735 0); /* max returned data. */
1737 if (tevent_req_nomem(subreq, req)) {
1738 return tevent_req_post(req, ev);
1740 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1744 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1746 return tevent_req_simple_recv_ntstatus(req);
1749 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1751 TALLOC_CTX *frame = talloc_stackframe();
1752 struct event_context *ev = NULL;
1753 struct tevent_req *req = NULL;
1754 NTSTATUS status = NT_STATUS_OK;
1756 if (cli_has_async_calls(cli)) {
1758 * Can't use sync call while an async call is in flight
1760 status = NT_STATUS_INVALID_PARAMETER;
1764 ev = event_context_init(frame);
1766 status = NT_STATUS_NO_MEMORY;
1770 req = cli_nt_delete_on_close_send(frame,
1776 status = NT_STATUS_NO_MEMORY;
1780 if (!tevent_req_poll(req, ev)) {
1781 status = map_nt_error_from_unix(errno);
1785 status = cli_nt_delete_on_close_recv(req);
1789 if (!NT_STATUS_IS_OK(status)) {
1790 cli_set_error(cli, status);
1795 struct cli_ntcreate_state {
1800 static void cli_ntcreate_done(struct tevent_req *subreq);
1802 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1803 struct event_context *ev,
1804 struct cli_state *cli,
1806 uint32_t CreatFlags,
1807 uint32_t DesiredAccess,
1808 uint32_t FileAttributes,
1809 uint32_t ShareAccess,
1810 uint32_t CreateDisposition,
1811 uint32_t CreateOptions,
1812 uint8_t SecurityFlags)
1814 struct tevent_req *req, *subreq;
1815 struct cli_ntcreate_state *state;
1818 size_t converted_len;
1820 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1827 SCVAL(vwv+0, 0, 0xFF);
1832 if (cli->use_oplocks) {
1833 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1835 SIVAL(vwv+3, 1, CreatFlags);
1836 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1837 SIVAL(vwv+7, 1, DesiredAccess);
1838 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1839 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv+13, 1, FileAttributes);
1841 SIVAL(vwv+15, 1, ShareAccess);
1842 SIVAL(vwv+17, 1, CreateDisposition);
1843 SIVAL(vwv+19, 1, CreateOptions);
1844 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1845 SCVAL(vwv+23, 1, SecurityFlags);
1847 bytes = talloc_array(state, uint8_t, 0);
1848 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1849 fname, strlen(fname)+1,
1852 /* sigh. this copes with broken netapp filer behaviour */
1853 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1855 if (tevent_req_nomem(bytes, req)) {
1856 return tevent_req_post(req, ev);
1859 SSVAL(vwv+2, 1, converted_len);
1861 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1862 talloc_get_size(bytes), bytes);
1863 if (tevent_req_nomem(subreq, req)) {
1864 return tevent_req_post(req, ev);
1866 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1870 static void cli_ntcreate_done(struct tevent_req *subreq)
1872 struct tevent_req *req = tevent_req_callback_data(
1873 subreq, struct tevent_req);
1874 struct cli_ntcreate_state *state = tevent_req_data(
1875 req, struct cli_ntcreate_state);
1883 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1884 &num_bytes, &bytes);
1885 TALLOC_FREE(subreq);
1886 if (!NT_STATUS_IS_OK(status)) {
1887 tevent_req_nterror(req, status);
1890 state->fnum = SVAL(vwv+2, 1);
1891 tevent_req_done(req);
1894 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1896 struct cli_ntcreate_state *state = tevent_req_data(
1897 req, struct cli_ntcreate_state);
1900 if (tevent_req_is_nterror(req, &status)) {
1903 *pfnum = state->fnum;
1904 return NT_STATUS_OK;
1907 NTSTATUS cli_ntcreate(struct cli_state *cli,
1909 uint32_t CreatFlags,
1910 uint32_t DesiredAccess,
1911 uint32_t FileAttributes,
1912 uint32_t ShareAccess,
1913 uint32_t CreateDisposition,
1914 uint32_t CreateOptions,
1915 uint8_t SecurityFlags,
1918 TALLOC_CTX *frame = talloc_stackframe();
1919 struct event_context *ev;
1920 struct tevent_req *req;
1921 NTSTATUS status = NT_STATUS_OK;
1923 if (cli_has_async_calls(cli)) {
1925 * Can't use sync call while an async call is in flight
1927 status = NT_STATUS_INVALID_PARAMETER;
1931 ev = event_context_init(frame);
1933 status = NT_STATUS_NO_MEMORY;
1937 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1938 DesiredAccess, FileAttributes, ShareAccess,
1939 CreateDisposition, CreateOptions,
1942 status = NT_STATUS_NO_MEMORY;
1946 if (!tevent_req_poll(req, ev)) {
1947 status = map_nt_error_from_unix(errno);
1951 status = cli_ntcreate_recv(req, pfid);
1954 if (!NT_STATUS_IS_OK(status)) {
1955 cli_set_error(cli, status);
1960 /****************************************************************************
1962 WARNING: if you open with O_WRONLY then getattrE won't work!
1963 ****************************************************************************/
1965 struct cli_open_state {
1971 static void cli_open_done(struct tevent_req *subreq);
1973 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1974 struct event_context *ev,
1975 struct cli_state *cli, const char *fname,
1976 int flags, int share_mode,
1977 struct tevent_req **psmbreq)
1979 struct tevent_req *req, *subreq;
1980 struct cli_open_state *state;
1982 unsigned accessmode;
1983 uint8_t additional_flags;
1986 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1992 if (flags & O_CREAT) {
1995 if (!(flags & O_EXCL)) {
1996 if (flags & O_TRUNC)
2002 accessmode = (share_mode<<4);
2004 if ((flags & O_ACCMODE) == O_RDWR) {
2006 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2011 if ((flags & O_SYNC) == O_SYNC) {
2012 accessmode |= (1<<14);
2016 if (share_mode == DENY_FCB) {
2020 SCVAL(state->vwv + 0, 0, 0xFF);
2021 SCVAL(state->vwv + 0, 1, 0);
2022 SSVAL(state->vwv + 1, 0, 0);
2023 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2024 SSVAL(state->vwv + 3, 0, accessmode);
2025 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2026 SSVAL(state->vwv + 5, 0, 0);
2027 SIVAL(state->vwv + 6, 0, 0);
2028 SSVAL(state->vwv + 8, 0, openfn);
2029 SIVAL(state->vwv + 9, 0, 0);
2030 SIVAL(state->vwv + 11, 0, 0);
2031 SIVAL(state->vwv + 13, 0, 0);
2033 additional_flags = 0;
2035 if (cli->use_oplocks) {
2036 /* if using oplocks then ask for a batch oplock via
2037 core and extended methods */
2039 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2040 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2043 bytes = talloc_array(state, uint8_t, 0);
2044 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2045 strlen(fname)+1, NULL);
2047 if (tevent_req_nomem(bytes, req)) {
2048 return tevent_req_post(req, ev);
2051 state->bytes.iov_base = (void *)bytes;
2052 state->bytes.iov_len = talloc_get_size(bytes);
2054 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2055 15, state->vwv, 1, &state->bytes);
2056 if (subreq == NULL) {
2060 tevent_req_set_callback(subreq, cli_open_done, req);
2065 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2066 struct cli_state *cli, const char *fname,
2067 int flags, int share_mode)
2069 struct tevent_req *req, *subreq;
2072 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2078 status = cli_smb_req_send(subreq);
2079 if (!NT_STATUS_IS_OK(status)) {
2080 tevent_req_nterror(req, status);
2081 return tevent_req_post(req, ev);
2086 static void cli_open_done(struct tevent_req *subreq)
2088 struct tevent_req *req = tevent_req_callback_data(
2089 subreq, struct tevent_req);
2090 struct cli_open_state *state = tevent_req_data(
2091 req, struct cli_open_state);
2097 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2099 TALLOC_FREE(subreq);
2100 if (!NT_STATUS_IS_OK(status)) {
2101 tevent_req_nterror(req, status);
2104 state->fnum = SVAL(vwv+2, 0);
2105 tevent_req_done(req);
2108 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2110 struct cli_open_state *state = tevent_req_data(
2111 req, struct cli_open_state);
2114 if (tevent_req_is_nterror(req, &status)) {
2117 *pfnum = state->fnum;
2118 return NT_STATUS_OK;
2121 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2122 int share_mode, uint16_t *pfnum)
2124 TALLOC_CTX *frame = talloc_stackframe();
2125 struct event_context *ev;
2126 struct tevent_req *req;
2127 NTSTATUS status = NT_STATUS_OK;
2129 if (cli_has_async_calls(cli)) {
2131 * Can't use sync call while an async call is in flight
2133 status = NT_STATUS_INVALID_PARAMETER;
2137 ev = event_context_init(frame);
2139 status = NT_STATUS_NO_MEMORY;
2143 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2145 status = NT_STATUS_NO_MEMORY;
2149 if (!tevent_req_poll(req, ev)) {
2150 status = map_nt_error_from_unix(errno);
2154 status = cli_open_recv(req, pfnum);
2157 if (!NT_STATUS_IS_OK(status)) {
2158 cli_set_error(cli, status);
2163 /****************************************************************************
2165 ****************************************************************************/
2167 struct cli_close_state {
2171 static void cli_close_done(struct tevent_req *subreq);
2173 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2174 struct event_context *ev,
2175 struct cli_state *cli,
2177 struct tevent_req **psubreq)
2179 struct tevent_req *req, *subreq;
2180 struct cli_close_state *state;
2182 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2187 SSVAL(state->vwv+0, 0, fnum);
2188 SIVALS(state->vwv+1, 0, -1);
2190 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2192 if (subreq == NULL) {
2196 tevent_req_set_callback(subreq, cli_close_done, req);
2201 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2202 struct event_context *ev,
2203 struct cli_state *cli,
2206 struct tevent_req *req, *subreq;
2209 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2214 status = cli_smb_req_send(subreq);
2215 if (!NT_STATUS_IS_OK(status)) {
2216 tevent_req_nterror(req, status);
2217 return tevent_req_post(req, ev);
2222 static void cli_close_done(struct tevent_req *subreq)
2224 struct tevent_req *req = tevent_req_callback_data(
2225 subreq, struct tevent_req);
2228 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2229 TALLOC_FREE(subreq);
2230 if (!NT_STATUS_IS_OK(status)) {
2231 tevent_req_nterror(req, status);
2234 tevent_req_done(req);
2237 NTSTATUS cli_close_recv(struct tevent_req *req)
2239 return tevent_req_simple_recv_ntstatus(req);
2242 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2244 TALLOC_CTX *frame = talloc_stackframe();
2245 struct event_context *ev;
2246 struct tevent_req *req;
2247 NTSTATUS status = NT_STATUS_OK;
2249 if (cli_has_async_calls(cli)) {
2251 * Can't use sync call while an async call is in flight
2253 status = NT_STATUS_INVALID_PARAMETER;
2257 ev = event_context_init(frame);
2259 status = NT_STATUS_NO_MEMORY;
2263 req = cli_close_send(frame, ev, cli, fnum);
2265 status = NT_STATUS_NO_MEMORY;
2269 if (!tevent_req_poll(req, ev)) {
2270 status = map_nt_error_from_unix(errno);
2274 status = cli_close_recv(req);
2277 if (!NT_STATUS_IS_OK(status)) {
2278 cli_set_error(cli, status);
2283 /****************************************************************************
2284 Truncate a file to a specified size
2285 ****************************************************************************/
2287 struct ftrunc_state {
2293 static void cli_ftruncate_done(struct tevent_req *subreq)
2295 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2296 NULL, 0, NULL, NULL, 0, NULL);
2297 tevent_req_simple_finish_ntstatus(subreq, status);
2300 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2301 struct event_context *ev,
2302 struct cli_state *cli,
2306 struct tevent_req *req = NULL, *subreq = NULL;
2307 struct ftrunc_state *state = NULL;
2309 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2314 /* Setup setup word. */
2315 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2317 /* Setup param array. */
2318 SSVAL(state->param,0,fnum);
2319 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2320 SSVAL(state->param,4,0);
2322 /* Setup data array. */
2323 SBVAL(state->data, 0, size);
2325 subreq = cli_trans_send(state, /* mem ctx. */
2326 ev, /* event ctx. */
2327 cli, /* cli_state. */
2328 SMBtrans2, /* cmd. */
2329 NULL, /* pipe name. */
2333 &state->setup, /* setup. */
2334 1, /* num setup uint16_t words. */
2335 0, /* max returned setup. */
2336 state->param, /* param. */
2338 2, /* max returned param. */
2339 state->data, /* data. */
2341 0); /* max returned data. */
2343 if (tevent_req_nomem(subreq, req)) {
2344 return tevent_req_post(req, ev);
2346 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2350 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2352 return tevent_req_simple_recv_ntstatus(req);
2355 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2357 TALLOC_CTX *frame = talloc_stackframe();
2358 struct event_context *ev = NULL;
2359 struct tevent_req *req = NULL;
2360 NTSTATUS status = NT_STATUS_OK;
2362 if (cli_has_async_calls(cli)) {
2364 * Can't use sync call while an async call is in flight
2366 status = NT_STATUS_INVALID_PARAMETER;
2370 ev = event_context_init(frame);
2372 status = NT_STATUS_NO_MEMORY;
2376 req = cli_ftruncate_send(frame,
2382 status = NT_STATUS_NO_MEMORY;
2386 if (!tevent_req_poll(req, ev)) {
2387 status = map_nt_error_from_unix(errno);
2391 status = cli_ftruncate_recv(req);
2395 if (!NT_STATUS_IS_OK(status)) {
2396 cli_set_error(cli, status);
2401 /****************************************************************************
2402 send a lock with a specified locktype
2403 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2404 ****************************************************************************/
2406 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2407 uint32_t offset, uint32_t len,
2408 int timeout, unsigned char locktype)
2415 SCVAL(vwv + 0, 0, 0xff);
2416 SCVAL(vwv + 0, 1, 0);
2417 SSVAL(vwv + 1, 0, 0);
2418 SSVAL(vwv + 2, 0, fnum);
2419 SCVAL(vwv + 3, 0, locktype);
2420 SCVAL(vwv + 3, 1, 0);
2421 SIVALS(vwv + 4, 0, timeout);
2422 SSVAL(vwv + 6, 0, 0);
2423 SSVAL(vwv + 7, 0, 1);
2425 SSVAL(bytes, 0, cli->pid);
2426 SIVAL(bytes, 2, offset);
2427 SIVAL(bytes, 6, len);
2429 saved_timeout = cli->timeout;
2432 cli->timeout = (timeout == -1)
2433 ? 0x7FFFFFFF : (timeout + 2*1000);
2436 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2437 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2439 cli->timeout = saved_timeout;
2444 /****************************************************************************
2446 note that timeout is in units of 2 milliseconds
2447 ****************************************************************************/
2449 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2450 uint32_t offset, uint32_t len, int timeout,
2451 enum brl_type lock_type)
2455 status = cli_locktype(cli, fnum, offset, len, timeout,
2456 (lock_type == READ_LOCK? 1 : 0));
2457 cli_set_error(cli, status);
2458 return NT_STATUS_IS_OK(status);
2461 /****************************************************************************
2463 ****************************************************************************/
2465 struct cli_unlock_state {
2470 static void cli_unlock_done(struct tevent_req *subreq);
2472 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2473 struct event_context *ev,
2474 struct cli_state *cli,
2480 struct tevent_req *req = NULL, *subreq = NULL;
2481 struct cli_unlock_state *state = NULL;
2482 uint8_t additional_flags = 0;
2484 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2489 SCVAL(state->vwv+0, 0, 0xFF);
2490 SSVAL(state->vwv+2, 0, fnum);
2491 SCVAL(state->vwv+3, 0, 0);
2492 SIVALS(state->vwv+4, 0, 0);
2493 SSVAL(state->vwv+6, 0, 1);
2494 SSVAL(state->vwv+7, 0, 0);
2496 SSVAL(state->data, 0, cli->pid);
2497 SIVAL(state->data, 2, offset);
2498 SIVAL(state->data, 6, len);
2500 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2501 8, state->vwv, 10, state->data);
2502 if (tevent_req_nomem(subreq, req)) {
2503 return tevent_req_post(req, ev);
2505 tevent_req_set_callback(subreq, cli_unlock_done, req);
2509 static void cli_unlock_done(struct tevent_req *subreq)
2511 struct tevent_req *req = tevent_req_callback_data(
2512 subreq, struct tevent_req);
2515 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2516 TALLOC_FREE(subreq);
2517 if (!NT_STATUS_IS_OK(status)) {
2518 tevent_req_nterror(req, status);
2521 tevent_req_done(req);
2524 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2526 return tevent_req_simple_recv_ntstatus(req);
2529 NTSTATUS cli_unlock(struct cli_state *cli,
2534 TALLOC_CTX *frame = talloc_stackframe();
2535 struct event_context *ev;
2536 struct tevent_req *req;
2537 NTSTATUS status = NT_STATUS_OK;
2539 if (cli_has_async_calls(cli)) {
2541 * Can't use sync call while an async call is in flight
2543 status = NT_STATUS_INVALID_PARAMETER;
2547 ev = event_context_init(frame);
2549 status = NT_STATUS_NO_MEMORY;
2553 req = cli_unlock_send(frame, ev, cli,
2556 status = NT_STATUS_NO_MEMORY;
2560 if (!tevent_req_poll(req, ev)) {
2561 status = map_nt_error_from_unix(errno);
2565 status = cli_unlock_recv(req);
2569 if (!NT_STATUS_IS_OK(status)) {
2570 cli_set_error(cli, status);
2575 /****************************************************************************
2576 Lock a file with 64 bit offsets.
2577 ****************************************************************************/
2579 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2580 uint64_t offset, uint64_t len, int timeout,
2581 enum brl_type lock_type)
2585 int saved_timeout = cli->timeout;
2589 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2590 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2593 ltype = (lock_type == READ_LOCK? 1 : 0);
2594 ltype |= LOCKING_ANDX_LARGE_FILES;
2596 SCVAL(vwv + 0, 0, 0xff);
2597 SCVAL(vwv + 0, 1, 0);
2598 SSVAL(vwv + 1, 0, 0);
2599 SSVAL(vwv + 2, 0, fnum);
2600 SCVAL(vwv + 3, 0, ltype);
2601 SCVAL(vwv + 3, 1, 0);
2602 SIVALS(vwv + 4, 0, timeout);
2603 SSVAL(vwv + 6, 0, 0);
2604 SSVAL(vwv + 7, 0, 1);
2606 SIVAL(bytes, 0, cli->pid);
2607 SOFF_T_R(bytes, 4, offset);
2608 SOFF_T_R(bytes, 12, len);
2610 saved_timeout = cli->timeout;
2613 cli->timeout = (timeout == -1)
2614 ? 0x7FFFFFFF : (timeout + 2*1000);
2617 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2618 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2620 cli->timeout = saved_timeout;
2622 cli_set_error(cli, status);
2623 return NT_STATUS_IS_OK(status);
2626 /****************************************************************************
2627 Unlock a file with 64 bit offsets.
2628 ****************************************************************************/
2630 struct cli_unlock64_state {
2635 static void cli_unlock64_done(struct tevent_req *subreq);
2637 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2638 struct event_context *ev,
2639 struct cli_state *cli,
2645 struct tevent_req *req = NULL, *subreq = NULL;
2646 struct cli_unlock64_state *state = NULL;
2647 uint8_t additional_flags = 0;
2649 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2654 SCVAL(state->vwv+0, 0, 0xff);
2655 SSVAL(state->vwv+2, 0, fnum);
2656 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2657 SIVALS(state->vwv+4, 0, 0);
2658 SSVAL(state->vwv+6, 0, 1);
2659 SSVAL(state->vwv+7, 0, 0);
2661 SIVAL(state->data, 0, cli->pid);
2662 SOFF_T_R(state->data, 4, offset);
2663 SOFF_T_R(state->data, 12, len);
2665 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2666 8, state->vwv, 20, state->data);
2667 if (tevent_req_nomem(subreq, req)) {
2668 return tevent_req_post(req, ev);
2670 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2674 static void cli_unlock64_done(struct tevent_req *subreq)
2676 struct tevent_req *req = tevent_req_callback_data(
2677 subreq, struct tevent_req);
2680 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2681 TALLOC_FREE(subreq);
2682 if (!NT_STATUS_IS_OK(status)) {
2683 tevent_req_nterror(req, status);
2686 tevent_req_done(req);
2689 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2691 return tevent_req_simple_recv_ntstatus(req);
2694 NTSTATUS cli_unlock64(struct cli_state *cli,
2699 TALLOC_CTX *frame = talloc_stackframe();
2700 struct event_context *ev;
2701 struct tevent_req *req;
2702 NTSTATUS status = NT_STATUS_OK;
2704 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2705 return cli_unlock(cli, fnum, offset, len);
2708 if (cli_has_async_calls(cli)) {
2710 * Can't use sync call while an async call is in flight
2712 status = NT_STATUS_INVALID_PARAMETER;
2716 ev = event_context_init(frame);
2718 status = NT_STATUS_NO_MEMORY;
2722 req = cli_unlock64_send(frame, ev, cli,
2725 status = NT_STATUS_NO_MEMORY;
2729 if (!tevent_req_poll(req, ev)) {
2730 status = map_nt_error_from_unix(errno);
2734 status = cli_unlock64_recv(req);
2738 if (!NT_STATUS_IS_OK(status)) {
2739 cli_set_error(cli, status);
2744 /****************************************************************************
2745 Get/unlock a POSIX lock on a file - internal function.
2746 ****************************************************************************/
2748 struct posix_lock_state {
2751 uint8_t data[POSIX_LOCK_DATA_SIZE];
2754 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2756 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2757 NULL, 0, NULL, NULL, 0, NULL);
2758 tevent_req_simple_finish_ntstatus(subreq, status);
2761 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2762 struct event_context *ev,
2763 struct cli_state *cli,
2768 enum brl_type lock_type)
2770 struct tevent_req *req = NULL, *subreq = NULL;
2771 struct posix_lock_state *state = NULL;
2773 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2778 /* Setup setup word. */
2779 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2781 /* Setup param array. */
2782 SSVAL(&state->param, 0, fnum);
2783 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2785 /* Setup data array. */
2786 switch (lock_type) {
2788 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2789 POSIX_LOCK_TYPE_READ);
2792 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2793 POSIX_LOCK_TYPE_WRITE);
2796 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2797 POSIX_LOCK_TYPE_UNLOCK);
2804 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2805 POSIX_LOCK_FLAG_WAIT);
2807 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2808 POSIX_LOCK_FLAG_NOWAIT);
2811 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2812 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2813 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2815 subreq = cli_trans_send(state, /* mem ctx. */
2816 ev, /* event ctx. */
2817 cli, /* cli_state. */
2818 SMBtrans2, /* cmd. */
2819 NULL, /* pipe name. */
2823 &state->setup, /* setup. */
2824 1, /* num setup uint16_t words. */
2825 0, /* max returned setup. */
2826 state->param, /* param. */
2828 2, /* max returned param. */
2829 state->data, /* data. */
2830 POSIX_LOCK_DATA_SIZE, /* num data. */
2831 0); /* max returned data. */
2833 if (tevent_req_nomem(subreq, req)) {
2834 return tevent_req_post(req, ev);
2836 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2840 /****************************************************************************
2842 ****************************************************************************/
2844 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2845 struct event_context *ev,
2846 struct cli_state *cli,
2851 enum brl_type lock_type)
2853 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2854 wait_lock, lock_type);
2857 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2859 return tevent_req_simple_recv_ntstatus(req);
2862 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2863 uint64_t offset, uint64_t len,
2864 bool wait_lock, enum brl_type lock_type)
2866 TALLOC_CTX *frame = talloc_stackframe();
2867 struct event_context *ev = NULL;
2868 struct tevent_req *req = NULL;
2869 NTSTATUS status = NT_STATUS_OK;
2871 if (cli_has_async_calls(cli)) {
2873 * Can't use sync call while an async call is in flight
2875 status = NT_STATUS_INVALID_PARAMETER;
2879 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2880 status = NT_STATUS_INVALID_PARAMETER;
2884 ev = event_context_init(frame);
2886 status = NT_STATUS_NO_MEMORY;
2890 req = cli_posix_lock_send(frame,
2899 status = NT_STATUS_NO_MEMORY;
2903 if (!tevent_req_poll(req, ev)) {
2904 status = map_nt_error_from_unix(errno);
2908 status = cli_posix_lock_recv(req);
2912 if (!NT_STATUS_IS_OK(status)) {
2913 cli_set_error(cli, status);
2918 /****************************************************************************
2919 POSIX Unlock a file.
2920 ****************************************************************************/
2922 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
2923 struct event_context *ev,
2924 struct cli_state *cli,
2929 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2930 false, UNLOCK_LOCK);
2933 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
2935 return tevent_req_simple_recv_ntstatus(req);
2938 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2940 TALLOC_CTX *frame = talloc_stackframe();
2941 struct event_context *ev = NULL;
2942 struct tevent_req *req = NULL;
2943 NTSTATUS status = NT_STATUS_OK;
2945 if (cli_has_async_calls(cli)) {
2947 * Can't use sync call while an async call is in flight
2949 status = NT_STATUS_INVALID_PARAMETER;
2953 ev = event_context_init(frame);
2955 status = NT_STATUS_NO_MEMORY;
2959 req = cli_posix_unlock_send(frame,
2966 status = NT_STATUS_NO_MEMORY;
2970 if (!tevent_req_poll(req, ev)) {
2971 status = map_nt_error_from_unix(errno);
2975 status = cli_posix_unlock_recv(req);
2979 if (!NT_STATUS_IS_OK(status)) {
2980 cli_set_error(cli, status);
2985 /****************************************************************************
2986 Do a SMBgetattrE call.
2987 ****************************************************************************/
2989 static void cli_getattrE_done(struct tevent_req *subreq);
2991 struct cli_getattrE_state {
3001 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3002 struct event_context *ev,
3003 struct cli_state *cli,
3006 struct tevent_req *req = NULL, *subreq = NULL;
3007 struct cli_getattrE_state *state = NULL;
3008 uint8_t additional_flags = 0;
3010 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3015 state->zone_offset = cli->serverzone;
3016 SSVAL(state->vwv+0,0,fnum);
3018 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3019 1, state->vwv, 0, NULL);
3020 if (tevent_req_nomem(subreq, req)) {
3021 return tevent_req_post(req, ev);
3023 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3027 static void cli_getattrE_done(struct tevent_req *subreq)
3029 struct tevent_req *req = tevent_req_callback_data(
3030 subreq, struct tevent_req);
3031 struct cli_getattrE_state *state = tevent_req_data(
3032 req, struct cli_getattrE_state);
3034 uint16_t *vwv = NULL;
3038 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3040 TALLOC_FREE(subreq);
3041 if (!NT_STATUS_IS_OK(status)) {
3042 tevent_req_nterror(req, status);
3046 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3047 state->attr = SVAL(vwv+10,0);
3048 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3049 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3050 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3052 tevent_req_done(req);
3055 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3058 time_t *change_time,
3059 time_t *access_time,
3062 struct cli_getattrE_state *state = tevent_req_data(
3063 req, struct cli_getattrE_state);
3066 if (tevent_req_is_nterror(req, &status)) {
3070 *attr = state->attr;
3073 *size = state->size;
3076 *change_time = state->change_time;
3079 *access_time = state->access_time;
3082 *write_time = state->write_time;
3084 return NT_STATUS_OK;
3087 NTSTATUS cli_getattrE(struct cli_state *cli,
3091 time_t *change_time,
3092 time_t *access_time,
3095 TALLOC_CTX *frame = talloc_stackframe();
3096 struct event_context *ev = NULL;
3097 struct tevent_req *req = NULL;
3098 NTSTATUS status = NT_STATUS_OK;
3100 if (cli_has_async_calls(cli)) {
3102 * Can't use sync call while an async call is in flight
3104 status = NT_STATUS_INVALID_PARAMETER;
3108 ev = event_context_init(frame);
3110 status = NT_STATUS_NO_MEMORY;
3114 req = cli_getattrE_send(frame, ev, cli, fnum);
3116 status = NT_STATUS_NO_MEMORY;
3120 if (!tevent_req_poll(req, ev)) {
3121 status = map_nt_error_from_unix(errno);
3125 status = cli_getattrE_recv(req,
3134 if (!NT_STATUS_IS_OK(status)) {
3135 cli_set_error(cli, status);
3140 /****************************************************************************
3142 ****************************************************************************/
3144 static void cli_getatr_done(struct tevent_req *subreq);
3146 struct cli_getatr_state {
3153 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3154 struct event_context *ev,
3155 struct cli_state *cli,
3158 struct tevent_req *req = NULL, *subreq = NULL;
3159 struct cli_getatr_state *state = NULL;
3160 uint8_t additional_flags = 0;
3161 uint8_t *bytes = NULL;
3163 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3168 state->zone_offset = cli->serverzone;
3170 bytes = talloc_array(state, uint8_t, 1);
3171 if (tevent_req_nomem(bytes, req)) {
3172 return tevent_req_post(req, ev);
3175 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3176 strlen(fname)+1, NULL);
3178 if (tevent_req_nomem(bytes, req)) {
3179 return tevent_req_post(req, ev);
3182 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3183 0, NULL, talloc_get_size(bytes), bytes);
3184 if (tevent_req_nomem(subreq, req)) {
3185 return tevent_req_post(req, ev);
3187 tevent_req_set_callback(subreq, cli_getatr_done, req);
3191 static void cli_getatr_done(struct tevent_req *subreq)
3193 struct tevent_req *req = tevent_req_callback_data(
3194 subreq, struct tevent_req);
3195 struct cli_getatr_state *state = tevent_req_data(
3196 req, struct cli_getatr_state);
3198 uint16_t *vwv = NULL;
3202 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3204 TALLOC_FREE(subreq);
3205 if (!NT_STATUS_IS_OK(status)) {
3206 tevent_req_nterror(req, status);
3210 state->attr = SVAL(vwv+0,0);
3211 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3212 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3214 tevent_req_done(req);
3217 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3222 struct cli_getatr_state *state = tevent_req_data(
3223 req, struct cli_getatr_state);
3226 if (tevent_req_is_nterror(req, &status)) {
3230 *attr = state->attr;
3233 *size = state->size;
3236 *write_time = state->write_time;
3238 return NT_STATUS_OK;
3241 NTSTATUS cli_getatr(struct cli_state *cli,
3247 TALLOC_CTX *frame = talloc_stackframe();
3248 struct event_context *ev = NULL;
3249 struct tevent_req *req = NULL;
3250 NTSTATUS status = NT_STATUS_OK;
3252 if (cli_has_async_calls(cli)) {
3254 * Can't use sync call while an async call is in flight
3256 status = NT_STATUS_INVALID_PARAMETER;
3260 ev = event_context_init(frame);
3262 status = NT_STATUS_NO_MEMORY;
3266 req = cli_getatr_send(frame, ev, cli, fname);
3268 status = NT_STATUS_NO_MEMORY;
3272 if (!tevent_req_poll(req, ev)) {
3273 status = map_nt_error_from_unix(errno);
3277 status = cli_getatr_recv(req,
3284 if (!NT_STATUS_IS_OK(status)) {
3285 cli_set_error(cli, status);
3290 /****************************************************************************
3291 Do a SMBsetattrE call.
3292 ****************************************************************************/
3294 static void cli_setattrE_done(struct tevent_req *subreq);
3296 struct cli_setattrE_state {
3300 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3301 struct event_context *ev,
3302 struct cli_state *cli,
3308 struct tevent_req *req = NULL, *subreq = NULL;
3309 struct cli_setattrE_state *state = NULL;
3310 uint8_t additional_flags = 0;
3312 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3317 SSVAL(state->vwv+0, 0, fnum);
3318 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3320 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3322 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3325 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3326 7, state->vwv, 0, NULL);
3327 if (tevent_req_nomem(subreq, req)) {
3328 return tevent_req_post(req, ev);
3330 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3334 static void cli_setattrE_done(struct tevent_req *subreq)
3336 struct tevent_req *req = tevent_req_callback_data(
3337 subreq, struct tevent_req);
3340 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3341 TALLOC_FREE(subreq);
3342 if (!NT_STATUS_IS_OK(status)) {
3343 tevent_req_nterror(req, status);
3346 tevent_req_done(req);
3349 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3351 return tevent_req_simple_recv_ntstatus(req);
3354 NTSTATUS cli_setattrE(struct cli_state *cli,
3360 TALLOC_CTX *frame = talloc_stackframe();
3361 struct event_context *ev = NULL;
3362 struct tevent_req *req = NULL;
3363 NTSTATUS status = NT_STATUS_OK;
3365 if (cli_has_async_calls(cli)) {
3367 * Can't use sync call while an async call is in flight
3369 status = NT_STATUS_INVALID_PARAMETER;
3373 ev = event_context_init(frame);
3375 status = NT_STATUS_NO_MEMORY;
3379 req = cli_setattrE_send(frame, ev,
3387 status = NT_STATUS_NO_MEMORY;
3391 if (!tevent_req_poll(req, ev)) {
3392 status = map_nt_error_from_unix(errno);
3396 status = cli_setattrE_recv(req);
3400 if (!NT_STATUS_IS_OK(status)) {
3401 cli_set_error(cli, status);
3406 /****************************************************************************
3407 Do a SMBsetatr call.
3408 ****************************************************************************/
3410 static void cli_setatr_done(struct tevent_req *subreq);
3412 struct cli_setatr_state {
3416 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3417 struct event_context *ev,
3418 struct cli_state *cli,
3423 struct tevent_req *req = NULL, *subreq = NULL;
3424 struct cli_setatr_state *state = NULL;
3425 uint8_t additional_flags = 0;
3426 uint8_t *bytes = NULL;
3428 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3433 SSVAL(state->vwv+0, 0, attr);
3434 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3436 bytes = talloc_array(state, uint8_t, 1);
3437 if (tevent_req_nomem(bytes, req)) {
3438 return tevent_req_post(req, ev);
3441 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3442 strlen(fname)+1, NULL);
3443 if (tevent_req_nomem(bytes, req)) {
3444 return tevent_req_post(req, ev);
3446 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3447 talloc_get_size(bytes)+1);
3448 if (tevent_req_nomem(bytes, req)) {
3449 return tevent_req_post(req, ev);
3452 bytes[talloc_get_size(bytes)-1] = 4;
3453 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3455 if (tevent_req_nomem(bytes, req)) {
3456 return tevent_req_post(req, ev);
3459 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3460 8, state->vwv, talloc_get_size(bytes), bytes);
3461 if (tevent_req_nomem(subreq, req)) {
3462 return tevent_req_post(req, ev);
3464 tevent_req_set_callback(subreq, cli_setatr_done, req);
3468 static void cli_setatr_done(struct tevent_req *subreq)
3470 struct tevent_req *req = tevent_req_callback_data(
3471 subreq, struct tevent_req);
3474 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3475 TALLOC_FREE(subreq);
3476 if (!NT_STATUS_IS_OK(status)) {
3477 tevent_req_nterror(req, status);
3480 tevent_req_done(req);
3483 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3485 return tevent_req_simple_recv_ntstatus(req);
3488 NTSTATUS cli_setatr(struct cli_state *cli,
3493 TALLOC_CTX *frame = talloc_stackframe();
3494 struct event_context *ev = NULL;
3495 struct tevent_req *req = NULL;
3496 NTSTATUS status = NT_STATUS_OK;
3498 if (cli_has_async_calls(cli)) {
3500 * Can't use sync call while an async call is in flight
3502 status = NT_STATUS_INVALID_PARAMETER;
3506 ev = event_context_init(frame);
3508 status = NT_STATUS_NO_MEMORY;
3512 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3514 status = NT_STATUS_NO_MEMORY;
3518 if (!tevent_req_poll(req, ev)) {
3519 status = map_nt_error_from_unix(errno);
3523 status = cli_setatr_recv(req);
3527 if (!NT_STATUS_IS_OK(status)) {
3528 cli_set_error(cli, status);
3533 /****************************************************************************
3534 Check for existance of a dir.
3535 ****************************************************************************/
3537 static void cli_chkpath_done(struct tevent_req *subreq);
3539 struct cli_chkpath_state {
3543 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3544 struct event_context *ev,
3545 struct cli_state *cli,
3548 struct tevent_req *req = NULL, *subreq = NULL;
3549 struct cli_chkpath_state *state = NULL;
3550 uint8_t additional_flags = 0;
3551 uint8_t *bytes = NULL;
3553 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3558 bytes = talloc_array(state, uint8_t, 1);
3559 if (tevent_req_nomem(bytes, req)) {
3560 return tevent_req_post(req, ev);
3563 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3564 strlen(fname)+1, NULL);
3566 if (tevent_req_nomem(bytes, req)) {
3567 return tevent_req_post(req, ev);
3570 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3571 0, NULL, talloc_get_size(bytes), bytes);
3572 if (tevent_req_nomem(subreq, req)) {
3573 return tevent_req_post(req, ev);
3575 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3579 static void cli_chkpath_done(struct tevent_req *subreq)
3581 struct tevent_req *req = tevent_req_callback_data(
3582 subreq, struct tevent_req);
3585 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3586 TALLOC_FREE(subreq);
3587 if (!NT_STATUS_IS_OK(status)) {
3588 tevent_req_nterror(req, status);
3591 tevent_req_done(req);
3594 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3596 return tevent_req_simple_recv_ntstatus(req);
3599 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3601 TALLOC_CTX *frame = talloc_stackframe();
3602 struct event_context *ev = NULL;
3603 struct tevent_req *req = NULL;
3605 NTSTATUS status = NT_STATUS_OK;
3607 if (cli_has_async_calls(cli)) {
3609 * Can't use sync call while an async call is in flight
3611 status = NT_STATUS_INVALID_PARAMETER;
3615 path2 = talloc_strdup(frame, path);
3617 status = NT_STATUS_NO_MEMORY;
3620 trim_char(path2,'\0','\\');
3622 path2 = talloc_strdup(frame, "\\");
3624 status = NT_STATUS_NO_MEMORY;
3629 ev = event_context_init(frame);
3631 status = NT_STATUS_NO_MEMORY;
3635 req = cli_chkpath_send(frame, ev, cli, path2);
3637 status = NT_STATUS_NO_MEMORY;
3641 if (!tevent_req_poll(req, ev)) {
3642 status = map_nt_error_from_unix(errno);
3646 status = cli_chkpath_recv(req);
3650 if (!NT_STATUS_IS_OK(status)) {
3651 cli_set_error(cli, status);
3656 /****************************************************************************
3658 ****************************************************************************/
3660 static void cli_dskattr_done(struct tevent_req *subreq);
3662 struct cli_dskattr_state {
3668 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3669 struct event_context *ev,
3670 struct cli_state *cli)
3672 struct tevent_req *req = NULL, *subreq = NULL;
3673 struct cli_dskattr_state *state = NULL;
3674 uint8_t additional_flags = 0;
3676 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3681 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3683 if (tevent_req_nomem(subreq, req)) {
3684 return tevent_req_post(req, ev);
3686 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3690 static void cli_dskattr_done(struct tevent_req *subreq)
3692 struct tevent_req *req = tevent_req_callback_data(
3693 subreq, struct tevent_req);
3694 struct cli_dskattr_state *state = tevent_req_data(
3695 req, struct cli_dskattr_state);
3697 uint16_t *vwv = NULL;
3701 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3703 TALLOC_FREE(subreq);
3704 if (!NT_STATUS_IS_OK(status)) {
3705 tevent_req_nterror(req, status);
3708 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3709 state->total = SVAL(vwv+0, 0);
3710 state->avail = SVAL(vwv+3, 0);
3711 tevent_req_done(req);
3714 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3716 struct cli_dskattr_state *state = tevent_req_data(
3717 req, struct cli_dskattr_state);
3720 if (tevent_req_is_nterror(req, &status)) {
3723 *bsize = state->bsize;
3724 *total = state->total;
3725 *avail = state->avail;
3726 return NT_STATUS_OK;
3729 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3731 TALLOC_CTX *frame = talloc_stackframe();
3732 struct event_context *ev = NULL;
3733 struct tevent_req *req = NULL;
3734 NTSTATUS status = NT_STATUS_OK;
3736 if (cli_has_async_calls(cli)) {
3738 * Can't use sync call while an async call is in flight
3740 status = NT_STATUS_INVALID_PARAMETER;
3744 ev = event_context_init(frame);
3746 status = NT_STATUS_NO_MEMORY;
3750 req = cli_dskattr_send(frame, ev, cli);
3752 status = NT_STATUS_NO_MEMORY;
3756 if (!tevent_req_poll(req, ev)) {
3757 status = map_nt_error_from_unix(errno);
3761 status = cli_dskattr_recv(req, bsize, total, avail);
3765 if (!NT_STATUS_IS_OK(status)) {
3766 cli_set_error(cli, status);
3771 /****************************************************************************
3772 Create and open a temporary file.
3773 ****************************************************************************/
3775 static void cli_ctemp_done(struct tevent_req *subreq);
3777 struct ctemp_state {
3783 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3784 struct event_context *ev,
3785 struct cli_state *cli,
3788 struct tevent_req *req = NULL, *subreq = NULL;
3789 struct ctemp_state *state = NULL;
3790 uint8_t additional_flags = 0;
3791 uint8_t *bytes = NULL;
3793 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3798 SSVAL(state->vwv,0,0);
3799 SIVALS(state->vwv+1,0,-1);
3801 bytes = talloc_array(state, uint8_t, 1);
3802 if (tevent_req_nomem(bytes, req)) {
3803 return tevent_req_post(req, ev);
3806 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3807 strlen(path)+1, NULL);
3808 if (tevent_req_nomem(bytes, req)) {
3809 return tevent_req_post(req, ev);
3812 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3813 3, state->vwv, talloc_get_size(bytes), bytes);
3814 if (tevent_req_nomem(subreq, req)) {
3815 return tevent_req_post(req, ev);
3817 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3821 static void cli_ctemp_done(struct tevent_req *subreq)
3823 struct tevent_req *req = tevent_req_callback_data(
3824 subreq, struct tevent_req);
3825 struct ctemp_state *state = tevent_req_data(
3826 req, struct ctemp_state);
3830 uint32_t num_bytes = 0;
3831 uint8_t *bytes = NULL;
3834 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3835 &num_bytes, &bytes);
3836 TALLOC_FREE(subreq);
3837 if (!NT_STATUS_IS_OK(status)) {
3838 tevent_req_nterror(req, status);
3842 state->fnum = SVAL(vwv+0, 0);
3844 /* From W2K3, the result is just the ASCII name */
3845 if (num_bytes < 2) {
3846 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3850 if (pull_string_talloc(state,
3857 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3860 tevent_req_done(req);
3863 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3868 struct ctemp_state *state = tevent_req_data(req,
3869 struct ctemp_state);
3872 if (tevent_req_is_nterror(req, &status)) {
3875 *pfnum = state->fnum;
3876 *outfile = talloc_strdup(ctx, state->ret_path);
3878 return NT_STATUS_NO_MEMORY;
3880 return NT_STATUS_OK;
3883 NTSTATUS cli_ctemp(struct cli_state *cli,
3889 TALLOC_CTX *frame = talloc_stackframe();
3890 struct event_context *ev;
3891 struct tevent_req *req;
3892 NTSTATUS status = NT_STATUS_OK;
3894 if (cli_has_async_calls(cli)) {
3896 * Can't use sync call while an async call is in flight
3898 status = NT_STATUS_INVALID_PARAMETER;
3902 ev = event_context_init(frame);
3904 status = NT_STATUS_NO_MEMORY;
3908 req = cli_ctemp_send(frame, ev, cli, path);
3910 status = NT_STATUS_NO_MEMORY;
3914 if (!tevent_req_poll(req, ev)) {
3915 status = map_nt_error_from_unix(errno);
3919 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3923 if (!NT_STATUS_IS_OK(status)) {
3924 cli_set_error(cli, status);
3930 send a raw ioctl - used by the torture code
3932 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3937 SSVAL(vwv+0, 0, fnum);
3938 SSVAL(vwv+1, 0, code>>16);
3939 SSVAL(vwv+2, 0, (code&0xFFFF));
3941 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
3942 NULL, 0, NULL, NULL, NULL, NULL);
3943 if (!NT_STATUS_IS_OK(status)) {
3946 *blob = data_blob_null;
3947 return NT_STATUS_OK;
3950 /*********************************************************
3951 Set an extended attribute utility fn.
3952 *********************************************************/
3954 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
3955 uint8_t *param, unsigned int param_len,
3956 const char *ea_name,
3957 const char *ea_val, size_t ea_len)
3960 unsigned int data_len = 0;
3961 uint8_t *data = NULL;
3963 size_t ea_namelen = strlen(ea_name);
3966 SSVAL(setup, 0, setup_val);
3968 if (ea_namelen == 0 && ea_len == 0) {
3970 data = (uint8_t *)SMB_MALLOC(data_len);
3972 return NT_STATUS_NO_MEMORY;
3975 SIVAL(p,0,data_len);
3977 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3978 data = (uint8_t *)SMB_MALLOC(data_len);
3980 return NT_STATUS_NO_MEMORY;
3983 SIVAL(p,0,data_len);
3985 SCVAL(p, 0, 0); /* EA flags. */
3986 SCVAL(p, 1, ea_namelen);
3987 SSVAL(p, 2, ea_len);
3988 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3989 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3992 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
3994 param, param_len, 2,
3995 data, data_len, cli->max_xmit,
3997 NULL, 0, NULL, /* rsetup */
3998 NULL, 0, NULL, /* rparam */
3999 NULL, 0, NULL); /* rdata */
4004 /*********************************************************
4005 Set an extended attribute on a pathname.
4006 *********************************************************/
4008 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4009 const char *ea_name, const char *ea_val,
4012 unsigned int param_len = 0;
4014 size_t srclen = 2*(strlen(path)+1);
4018 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4020 return NT_STATUS_NO_MEMORY;
4022 memset(param, '\0', 6);
4023 SSVAL(param,0,SMB_INFO_SET_EA);
4024 p = (char *)(¶m[6]);
4026 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4027 param_len = PTR_DIFF(p, param);
4029 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4030 ea_name, ea_val, ea_len);
4035 /*********************************************************
4036 Set an extended attribute on an fnum.
4037 *********************************************************/
4039 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4040 const char *ea_name, const char *ea_val,
4045 memset(param, 0, 6);
4046 SSVAL(param,0,fnum);
4047 SSVAL(param,2,SMB_INFO_SET_EA);
4049 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4050 ea_name, ea_val, ea_len);
4053 /*********************************************************
4054 Get an extended attribute list utility fn.
4055 *********************************************************/
4057 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4059 size_t *pnum_eas, struct ea_struct **pea_list)
4061 struct ea_struct *ea_list = NULL;
4066 if (rdata_len < 4) {
4070 ea_size = (size_t)IVAL(rdata,0);
4071 if (ea_size > rdata_len) {
4076 /* No EA's present. */
4085 /* Validate the EA list and count it. */
4086 for (num_eas = 0; ea_size >= 4; num_eas++) {
4087 unsigned int ea_namelen = CVAL(p,1);
4088 unsigned int ea_valuelen = SVAL(p,2);
4089 if (ea_namelen == 0) {
4092 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4095 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4096 p += 4 + ea_namelen + 1 + ea_valuelen;
4105 *pnum_eas = num_eas;
4107 /* Caller only wants number of EA's. */
4111 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4116 ea_size = (size_t)IVAL(rdata,0);
4119 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4120 struct ea_struct *ea = &ea_list[num_eas];
4121 fstring unix_ea_name;
4122 unsigned int ea_namelen = CVAL(p,1);
4123 unsigned int ea_valuelen = SVAL(p,2);
4125 ea->flags = CVAL(p,0);
4126 unix_ea_name[0] = '\0';
4127 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4128 ea->name = talloc_strdup(ea_list, unix_ea_name);
4132 /* Ensure the value is null terminated (in case it's a string). */
4133 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4134 if (!ea->value.data) {
4138 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4140 ea->value.data[ea_valuelen] = 0;
4142 p += 4 + ea_namelen + 1 + ea_valuelen;
4145 *pea_list = ea_list;
4149 TALLOC_FREE(ea_list);
4153 /*********************************************************
4154 Get an extended attribute list from a pathname.
4155 *********************************************************/
4157 struct cli_get_ea_list_path_state {
4162 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4164 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4165 struct tevent_context *ev,
4166 struct cli_state *cli,
4169 struct tevent_req *req, *subreq;
4170 struct cli_get_ea_list_path_state *state;
4172 req = tevent_req_create(mem_ctx, &state,
4173 struct cli_get_ea_list_path_state);
4177 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4178 SMB_INFO_QUERY_ALL_EAS, 4,
4180 if (tevent_req_nomem(subreq, req)) {
4181 return tevent_req_post(req, ev);
4183 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4187 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4189 struct tevent_req *req = tevent_req_callback_data(
4190 subreq, struct tevent_req);
4191 struct cli_get_ea_list_path_state *state = tevent_req_data(
4192 req, struct cli_get_ea_list_path_state);
4195 status = cli_qpathinfo_recv(subreq, state, &state->data,
4197 TALLOC_FREE(subreq);
4198 if (!NT_STATUS_IS_OK(status)) {
4199 tevent_req_nterror(req, status);
4202 tevent_req_done(req);
4205 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4206 size_t *pnum_eas, struct ea_struct **peas)
4208 struct cli_get_ea_list_path_state *state = tevent_req_data(
4209 req, struct cli_get_ea_list_path_state);
4212 if (tevent_req_is_nterror(req, &status)) {
4215 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4217 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4219 return NT_STATUS_OK;
4222 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4225 struct ea_struct **pea_list)
4227 TALLOC_CTX *frame = talloc_stackframe();
4228 struct event_context *ev = NULL;
4229 struct tevent_req *req = NULL;
4230 NTSTATUS status = NT_STATUS_NO_MEMORY;
4232 if (cli_has_async_calls(cli)) {
4234 * Can't use sync call while an async call is in flight
4236 status = NT_STATUS_INVALID_PARAMETER;
4239 ev = event_context_init(frame);
4243 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4247 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4250 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4253 if (!NT_STATUS_IS_OK(status)) {
4254 cli_set_error(cli, status);
4259 /****************************************************************************
4260 Convert open "flags" arg to uint32_t on wire.
4261 ****************************************************************************/
4263 static uint32_t open_flags_to_wire(int flags)
4265 int open_mode = flags & O_ACCMODE;
4268 switch (open_mode) {
4270 ret |= SMB_O_WRONLY;
4277 ret |= SMB_O_RDONLY;
4281 if (flags & O_CREAT) {
4284 if (flags & O_EXCL) {
4287 if (flags & O_TRUNC) {
4291 if (flags & O_SYNC) {
4295 if (flags & O_APPEND) {
4296 ret |= SMB_O_APPEND;
4298 #if defined(O_DIRECT)
4299 if (flags & O_DIRECT) {
4300 ret |= SMB_O_DIRECT;
4303 #if defined(O_DIRECTORY)
4304 if (flags & O_DIRECTORY) {
4305 ret |= SMB_O_DIRECTORY;
4311 /****************************************************************************
4312 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4313 ****************************************************************************/
4315 struct posix_open_state {
4319 uint16_t fnum; /* Out */
4322 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4324 struct tevent_req *req = tevent_req_callback_data(
4325 subreq, struct tevent_req);
4326 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4331 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4332 NULL, 0, NULL, &data, 12, &num_data);
4333 TALLOC_FREE(subreq);
4334 if (!NT_STATUS_IS_OK(status)) {
4335 tevent_req_nterror(req, status);
4338 state->fnum = SVAL(data,2);
4339 tevent_req_done(req);
4342 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4343 struct event_context *ev,
4344 struct cli_state *cli,
4350 struct tevent_req *req = NULL, *subreq = NULL;
4351 struct posix_open_state *state = NULL;
4352 uint32_t wire_flags = open_flags_to_wire(flags);
4354 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4359 /* Setup setup word. */
4360 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4362 /* Setup param array. */
4363 state->param = talloc_array(state, uint8_t, 6);
4364 if (tevent_req_nomem(state->param, req)) {
4365 return tevent_req_post(req, ev);
4367 memset(state->param, '\0', 6);
4368 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4370 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4371 strlen(fname)+1, NULL);
4373 if (tevent_req_nomem(state->param, req)) {
4374 return tevent_req_post(req, ev);
4377 /* Setup data words. */
4379 wire_flags |= SMB_O_DIRECTORY;
4382 SIVAL(state->data,0,0); /* No oplock. */
4383 SIVAL(state->data,4,wire_flags);
4384 SIVAL(state->data,8,unix_perms_to_wire(mode));
4385 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4386 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4388 subreq = cli_trans_send(state, /* mem ctx. */
4389 ev, /* event ctx. */
4390 cli, /* cli_state. */
4391 SMBtrans2, /* cmd. */
4392 NULL, /* pipe name. */
4396 &state->setup, /* setup. */
4397 1, /* num setup uint16_t words. */
4398 0, /* max returned setup. */
4399 state->param, /* param. */
4400 talloc_get_size(state->param),/* num param. */
4401 2, /* max returned param. */
4402 state->data, /* data. */
4404 12); /* max returned data. */
4406 if (tevent_req_nomem(subreq, req)) {
4407 return tevent_req_post(req, ev);
4409 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4413 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4414 struct event_context *ev,
4415 struct cli_state *cli,
4420 return cli_posix_open_internal_send(mem_ctx, ev,
4421 cli, fname, flags, mode, false);
4424 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4426 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4429 if (tevent_req_is_nterror(req, &status)) {
4432 *pfnum = state->fnum;
4433 return NT_STATUS_OK;
4436 /****************************************************************************
4437 Open - POSIX semantics. Doesn't request oplock.
4438 ****************************************************************************/
4440 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4441 int flags, mode_t mode, uint16_t *pfnum)
4444 TALLOC_CTX *frame = talloc_stackframe();
4445 struct event_context *ev = NULL;
4446 struct tevent_req *req = NULL;
4447 NTSTATUS status = NT_STATUS_OK;
4449 if (cli_has_async_calls(cli)) {
4451 * Can't use sync call while an async call is in flight
4453 status = NT_STATUS_INVALID_PARAMETER;
4457 ev = event_context_init(frame);
4459 status = NT_STATUS_NO_MEMORY;
4463 req = cli_posix_open_send(frame,
4470 status = NT_STATUS_NO_MEMORY;
4474 if (!tevent_req_poll(req, ev)) {
4475 status = map_nt_error_from_unix(errno);
4479 status = cli_posix_open_recv(req, pfnum);
4483 if (!NT_STATUS_IS_OK(status)) {
4484 cli_set_error(cli, status);
4489 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4490 struct event_context *ev,
4491 struct cli_state *cli,
4495 return cli_posix_open_internal_send(mem_ctx, ev,
4496 cli, fname, O_CREAT, mode, true);
4499 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4501 return tevent_req_simple_recv_ntstatus(req);
4504 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4506 TALLOC_CTX *frame = talloc_stackframe();
4507 struct event_context *ev = NULL;
4508 struct tevent_req *req = NULL;
4509 NTSTATUS status = NT_STATUS_OK;
4511 if (cli_has_async_calls(cli)) {
4513 * Can't use sync call while an async call is in flight
4515 status = NT_STATUS_INVALID_PARAMETER;
4519 ev = event_context_init(frame);
4521 status = NT_STATUS_NO_MEMORY;
4525 req = cli_posix_mkdir_send(frame,
4531 status = NT_STATUS_NO_MEMORY;
4535 if (!tevent_req_poll(req, ev)) {
4536 status = map_nt_error_from_unix(errno);
4540 status = cli_posix_mkdir_recv(req);
4544 if (!NT_STATUS_IS_OK(status)) {
4545 cli_set_error(cli, status);
4550 /****************************************************************************
4551 unlink or rmdir - POSIX semantics.
4552 ****************************************************************************/
4554 struct cli_posix_unlink_internal_state {
4558 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4560 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4561 struct event_context *ev,
4562 struct cli_state *cli,
4566 struct tevent_req *req = NULL, *subreq = NULL;
4567 struct cli_posix_unlink_internal_state *state = NULL;
4569 req = tevent_req_create(mem_ctx, &state,
4570 struct cli_posix_unlink_internal_state);
4575 /* Setup data word. */
4576 SSVAL(state->data, 0, level);
4578 subreq = cli_setpathinfo_send(state, ev, cli,
4579 SMB_POSIX_PATH_UNLINK,
4581 state->data, sizeof(state->data));
4582 if (tevent_req_nomem(subreq, req)) {
4583 return tevent_req_post(req, ev);
4585 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4589 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4591 NTSTATUS status = cli_setpathinfo_recv(subreq);
4592 tevent_req_simple_finish_ntstatus(subreq, status);
4595 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4596 struct event_context *ev,
4597 struct cli_state *cli,
4600 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4601 SMB_POSIX_UNLINK_FILE_TARGET);
4604 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4606 return tevent_req_simple_recv_ntstatus(req);
4609 /****************************************************************************
4610 unlink - POSIX semantics.
4611 ****************************************************************************/
4613 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4615 TALLOC_CTX *frame = talloc_stackframe();
4616 struct event_context *ev = NULL;
4617 struct tevent_req *req = NULL;
4618 NTSTATUS status = NT_STATUS_OK;
4620 if (cli_has_async_calls(cli)) {
4622 * Can't use sync call while an async call is in flight
4624 status = NT_STATUS_INVALID_PARAMETER;
4628 ev = event_context_init(frame);
4630 status = NT_STATUS_NO_MEMORY;
4634 req = cli_posix_unlink_send(frame,
4639 status = NT_STATUS_NO_MEMORY;
4643 if (!tevent_req_poll(req, ev)) {
4644 status = map_nt_error_from_unix(errno);
4648 status = cli_posix_unlink_recv(req);
4652 if (!NT_STATUS_IS_OK(status)) {
4653 cli_set_error(cli, status);
4658 /****************************************************************************
4659 rmdir - POSIX semantics.
4660 ****************************************************************************/
4662 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4663 struct event_context *ev,
4664 struct cli_state *cli,
4667 return cli_posix_unlink_internal_send(
4668 mem_ctx, ev, cli, fname,
4669 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4672 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4674 return tevent_req_simple_recv_ntstatus(req);
4677 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4679 TALLOC_CTX *frame = talloc_stackframe();
4680 struct event_context *ev = NULL;
4681 struct tevent_req *req = NULL;
4682 NTSTATUS status = NT_STATUS_OK;
4684 if (cli_has_async_calls(cli)) {
4686 * Can't use sync call while an async call is in flight
4688 status = NT_STATUS_INVALID_PARAMETER;
4692 ev = event_context_init(frame);
4694 status = NT_STATUS_NO_MEMORY;
4698 req = cli_posix_rmdir_send(frame,
4703 status = NT_STATUS_NO_MEMORY;
4707 if (!tevent_req_poll(req, ev)) {
4708 status = map_nt_error_from_unix(errno);
4712 status = cli_posix_rmdir_recv(req, frame);
4716 if (!NT_STATUS_IS_OK(status)) {
4717 cli_set_error(cli, status);
4722 /****************************************************************************
4724 ****************************************************************************/
4726 struct cli_notify_state {
4728 uint32_t num_changes;
4729 struct notify_change *changes;
4732 static void cli_notify_done(struct tevent_req *subreq);
4734 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4735 struct tevent_context *ev,
4736 struct cli_state *cli, uint16_t fnum,
4737 uint32_t buffer_size,
4738 uint32_t completion_filter, bool recursive)
4740 struct tevent_req *req, *subreq;
4741 struct cli_notify_state *state;
4743 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4748 SIVAL(state->setup, 0, completion_filter);
4749 SSVAL(state->setup, 4, fnum);
4750 SSVAL(state->setup, 6, recursive);
4752 subreq = cli_trans_send(
4753 state, /* mem ctx. */
4754 ev, /* event ctx. */
4755 cli, /* cli_state. */
4756 SMBnttrans, /* cmd. */
4757 NULL, /* pipe name. */
4759 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4761 (uint16_t *)state->setup, /* setup. */
4762 4, /* num setup uint16_t words. */
4763 0, /* max returned setup. */
4766 buffer_size, /* max returned param. */
4769 0); /* max returned data. */
4771 if (tevent_req_nomem(subreq, req)) {
4772 return tevent_req_post(req, ev);
4774 tevent_req_set_callback(subreq, cli_notify_done, req);
4778 static void cli_notify_done(struct tevent_req *subreq)
4780 struct tevent_req *req = tevent_req_callback_data(
4781 subreq, struct tevent_req);
4782 struct cli_notify_state *state = tevent_req_data(
4783 req, struct cli_notify_state);
4786 uint32_t i, ofs, num_params;
4789 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4790 ¶ms, 0, &num_params, NULL, 0, NULL);
4791 TALLOC_FREE(subreq);
4792 if (!NT_STATUS_IS_OK(status)) {
4793 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4794 tevent_req_nterror(req, status);
4798 state->num_changes = 0;
4801 while (num_params - ofs > 12) {
4802 uint32_t len = IVAL(params, ofs);
4803 state->num_changes += 1;
4805 if ((len == 0) || (ofs+len >= num_params)) {
4811 state->changes = talloc_array(state, struct notify_change,
4812 state->num_changes);
4813 if (tevent_req_nomem(state->changes, req)) {
4814 TALLOC_FREE(params);
4820 for (i=0; i<state->num_changes; i++) {
4821 uint32_t next = IVAL(params, ofs);
4822 uint32_t len = IVAL(params, ofs+8);
4826 if ((next != 0) && (len+12 != next)) {
4827 TALLOC_FREE(params);
4829 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4833 state->changes[i].action = IVAL(params, ofs+4);
4834 ret = clistr_pull_talloc(params, (char *)params, flags2,
4835 &name, params+ofs+12, len,
4836 STR_TERMINATE|STR_UNICODE);
4838 TALLOC_FREE(params);
4839 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4842 state->changes[i].name = name;
4846 TALLOC_FREE(params);
4847 tevent_req_done(req);
4850 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4851 uint32_t *pnum_changes,
4852 struct notify_change **pchanges)
4854 struct cli_notify_state *state = tevent_req_data(
4855 req, struct cli_notify_state);
4858 if (tevent_req_is_nterror(req, &status)) {
4862 *pnum_changes = state->num_changes;
4863 *pchanges = talloc_move(mem_ctx, &state->changes);
4864 return NT_STATUS_OK;
4867 struct cli_qpathinfo_state {
4876 static void cli_qpathinfo_done(struct tevent_req *subreq);
4878 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4879 struct tevent_context *ev,
4880 struct cli_state *cli, const char *fname,
4881 uint16_t level, uint32_t min_rdata,
4884 struct tevent_req *req, *subreq;
4885 struct cli_qpathinfo_state *state;
4887 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4891 state->min_rdata = min_rdata;
4892 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4894 state->param = talloc_zero_array(state, uint8_t, 6);
4895 if (tevent_req_nomem(state->param, req)) {
4896 return tevent_req_post(req, ev);
4898 SSVAL(state->param, 0, level);
4899 state->param = trans2_bytes_push_str(
4900 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4901 if (tevent_req_nomem(state->param, req)) {
4902 return tevent_req_post(req, ev);
4905 subreq = cli_trans_send(
4906 state, /* mem ctx. */
4907 ev, /* event ctx. */
4908 cli, /* cli_state. */
4909 SMBtrans2, /* cmd. */
4910 NULL, /* pipe name. */
4914 state->setup, /* setup. */
4915 1, /* num setup uint16_t words. */
4916 0, /* max returned setup. */
4917 state->param, /* param. */
4918 talloc_get_size(state->param), /* num param. */
4919 2, /* max returned param. */
4922 max_rdata); /* max returned data. */
4924 if (tevent_req_nomem(subreq, req)) {
4925 return tevent_req_post(req, ev);
4927 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4931 static void cli_qpathinfo_done(struct tevent_req *subreq)
4933 struct tevent_req *req = tevent_req_callback_data(
4934 subreq, struct tevent_req);
4935 struct cli_qpathinfo_state *state = tevent_req_data(
4936 req, struct cli_qpathinfo_state);
4939 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4941 &state->rdata, state->min_rdata,
4943 if (!NT_STATUS_IS_OK(status)) {
4944 tevent_req_nterror(req, status);
4947 tevent_req_done(req);
4950 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4951 uint8_t **rdata, uint32_t *num_rdata)
4953 struct cli_qpathinfo_state *state = tevent_req_data(
4954 req, struct cli_qpathinfo_state);
4957 if (tevent_req_is_nterror(req, &status)) {
4960 if (rdata != NULL) {
4961 *rdata = talloc_move(mem_ctx, &state->rdata);
4963 TALLOC_FREE(state->rdata);
4965 if (num_rdata != NULL) {
4966 *num_rdata = state->num_rdata;
4968 return NT_STATUS_OK;
4971 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
4972 const char *fname, uint16_t level, uint32_t min_rdata,
4974 uint8_t **rdata, uint32_t *num_rdata)
4976 TALLOC_CTX *frame = talloc_stackframe();
4977 struct event_context *ev;
4978 struct tevent_req *req;
4979 NTSTATUS status = NT_STATUS_NO_MEMORY;
4981 if (cli_has_async_calls(cli)) {
4983 * Can't use sync call while an async call is in flight
4985 status = NT_STATUS_INVALID_PARAMETER;
4988 ev = event_context_init(frame);
4992 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
4997 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5000 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5003 if (!NT_STATUS_IS_OK(status)) {
5004 cli_set_error(cli, status);
5009 struct cli_qfileinfo_state {
5018 static void cli_qfileinfo_done(struct tevent_req *subreq);
5020 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5021 struct tevent_context *ev,
5022 struct cli_state *cli, uint16_t fnum,
5023 uint16_t level, uint32_t min_rdata,
5026 struct tevent_req *req, *subreq;
5027 struct cli_qfileinfo_state *state;
5029 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5033 state->min_rdata = min_rdata;
5034 SSVAL(state->param, 0, fnum);
5035 SSVAL(state->param, 2, level);
5036 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5038 subreq = cli_trans_send(
5039 state, /* mem ctx. */
5040 ev, /* event ctx. */
5041 cli, /* cli_state. */
5042 SMBtrans2, /* cmd. */
5043 NULL, /* pipe name. */
5047 state->setup, /* setup. */
5048 1, /* num setup uint16_t words. */
5049 0, /* max returned setup. */
5050 state->param, /* param. */
5051 sizeof(state->param), /* num param. */
5052 2, /* max returned param. */
5055 max_rdata); /* max returned data. */
5057 if (tevent_req_nomem(subreq, req)) {
5058 return tevent_req_post(req, ev);
5060 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5064 static void cli_qfileinfo_done(struct tevent_req *subreq)
5066 struct tevent_req *req = tevent_req_callback_data(
5067 subreq, struct tevent_req);
5068 struct cli_qfileinfo_state *state = tevent_req_data(
5069 req, struct cli_qfileinfo_state);
5072 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5074 &state->rdata, state->min_rdata,
5076 if (!NT_STATUS_IS_OK(status)) {
5077 tevent_req_nterror(req, status);
5080 tevent_req_done(req);
5083 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5084 uint8_t **rdata, uint32_t *num_rdata)
5086 struct cli_qfileinfo_state *state = tevent_req_data(
5087 req, struct cli_qfileinfo_state);
5090 if (tevent_req_is_nterror(req, &status)) {
5093 if (rdata != NULL) {
5094 *rdata = talloc_move(mem_ctx, &state->rdata);
5096 TALLOC_FREE(state->rdata);
5098 if (num_rdata != NULL) {
5099 *num_rdata = state->num_rdata;
5101 return NT_STATUS_OK;
5104 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5105 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5107 uint8_t **rdata, uint32_t *num_rdata)
5109 TALLOC_CTX *frame = talloc_stackframe();
5110 struct event_context *ev;
5111 struct tevent_req *req;
5112 NTSTATUS status = NT_STATUS_NO_MEMORY;
5114 if (cli_has_async_calls(cli)) {
5116 * Can't use sync call while an async call is in flight
5118 status = NT_STATUS_INVALID_PARAMETER;
5121 ev = event_context_init(frame);
5125 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5130 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5133 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5136 if (!NT_STATUS_IS_OK(status)) {
5137 cli_set_error(cli, status);
5142 struct cli_flush_state {
5146 static void cli_flush_done(struct tevent_req *subreq);
5148 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5149 struct event_context *ev,
5150 struct cli_state *cli,
5153 struct tevent_req *req, *subreq;
5154 struct cli_flush_state *state;
5156 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5160 SSVAL(state->vwv + 0, 0, fnum);
5162 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5164 if (tevent_req_nomem(subreq, req)) {
5165 return tevent_req_post(req, ev);
5167 tevent_req_set_callback(subreq, cli_flush_done, req);
5171 static void cli_flush_done(struct tevent_req *subreq)
5173 struct tevent_req *req = tevent_req_callback_data(
5174 subreq, struct tevent_req);
5177 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5178 TALLOC_FREE(subreq);
5179 if (!NT_STATUS_IS_OK(status)) {
5180 tevent_req_nterror(req, status);
5183 tevent_req_done(req);
5186 NTSTATUS cli_flush_recv(struct tevent_req *req)
5188 return tevent_req_simple_recv_ntstatus(req);
5191 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5193 TALLOC_CTX *frame = talloc_stackframe();
5194 struct event_context *ev;
5195 struct tevent_req *req;
5196 NTSTATUS status = NT_STATUS_NO_MEMORY;
5198 if (cli_has_async_calls(cli)) {
5200 * Can't use sync call while an async call is in flight
5202 status = NT_STATUS_INVALID_PARAMETER;
5205 ev = event_context_init(frame);
5209 req = cli_flush_send(frame, ev, cli, fnum);
5213 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5216 status = cli_flush_recv(req);
5219 if (!NT_STATUS_IS_OK(status)) {
5220 cli_set_error(cli, status);
5225 struct cli_shadow_copy_data_state {
5232 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5234 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5235 struct tevent_context *ev,
5236 struct cli_state *cli,
5240 struct tevent_req *req, *subreq;
5241 struct cli_shadow_copy_data_state *state;
5244 req = tevent_req_create(mem_ctx, &state,
5245 struct cli_shadow_copy_data_state);
5249 state->get_names = get_names;
5250 ret_size = get_names ? cli->max_xmit : 16;
5252 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5253 SSVAL(state->setup + 2, 0, fnum);
5254 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5255 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5257 subreq = cli_trans_send(
5258 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5259 state->setup, ARRAY_SIZE(state->setup), 0,
5262 if (tevent_req_nomem(subreq, req)) {
5263 return tevent_req_post(req, ev);
5265 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5269 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5271 struct tevent_req *req = tevent_req_callback_data(
5272 subreq, struct tevent_req);
5273 struct cli_shadow_copy_data_state *state = tevent_req_data(
5274 req, struct cli_shadow_copy_data_state);
5277 status = cli_trans_recv(subreq, state, NULL,
5278 NULL, 0, NULL, /* setup */
5279 NULL, 0, NULL, /* param */
5280 &state->data, 12, &state->num_data);
5281 TALLOC_FREE(subreq);
5282 if (!NT_STATUS_IS_OK(status)) {
5283 tevent_req_nterror(req, status);
5286 tevent_req_done(req);
5289 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5290 char ***pnames, int *pnum_names)
5292 struct cli_shadow_copy_data_state *state = tevent_req_data(
5293 req, struct cli_shadow_copy_data_state);
5299 if (tevent_req_is_nterror(req, &status)) {
5302 num_names = IVAL(state->data, 4);
5303 dlength = IVAL(state->data, 8);
5305 if (!state->get_names) {
5306 *pnum_names = num_names;
5307 return NT_STATUS_OK;
5310 if (dlength+12 > state->num_data) {
5311 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5313 names = talloc_array(mem_ctx, char *, num_names);
5314 if (names == NULL) {
5315 return NT_STATUS_NO_MEMORY;
5318 for (i=0; i<num_names; i++) {
5321 size_t converted_size;
5323 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5324 ret = convert_string_talloc(
5325 names, CH_UTF16LE, CH_UNIX,
5326 src, 2 * sizeof(SHADOW_COPY_LABEL),
5327 &names[i], &converted_size);
5330 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5333 *pnum_names = num_names;
5335 return NT_STATUS_OK;
5338 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5339 uint16_t fnum, bool get_names,
5340 char ***pnames, int *pnum_names)
5342 TALLOC_CTX *frame = talloc_stackframe();
5343 struct event_context *ev;
5344 struct tevent_req *req;
5345 NTSTATUS status = NT_STATUS_NO_MEMORY;
5347 if (cli_has_async_calls(cli)) {
5349 * Can't use sync call while an async call is in flight
5351 status = NT_STATUS_INVALID_PARAMETER;
5354 ev = event_context_init(frame);
5358 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5362 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5365 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5368 if (!NT_STATUS_IS_OK(status)) {
5369 cli_set_error(cli, status);