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 Map standard UNIX permissions onto wire representations.
580 ****************************************************************************/
582 uint32_t unix_perms_to_wire(mode_t perms)
584 unsigned int ret = 0;
586 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
587 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
588 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
589 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
590 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
591 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
592 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
593 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
594 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
596 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
599 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
602 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
607 /****************************************************************************
608 Map wire permissions to standard UNIX.
609 ****************************************************************************/
611 mode_t wire_perms_to_unix(uint32_t perms)
613 mode_t ret = (mode_t)0;
615 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
616 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
617 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
618 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
619 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
620 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
621 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
622 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
623 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
625 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
628 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
631 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
636 /****************************************************************************
637 Return the file type from the wire filetype for UNIX extensions.
638 ****************************************************************************/
640 static mode_t unix_filetype_from_wire(uint32_t wire_type)
648 case UNIX_TYPE_SYMLINK:
652 case UNIX_TYPE_CHARDEV:
656 case UNIX_TYPE_BLKDEV:
664 case UNIX_TYPE_SOCKET:
672 /****************************************************************************
673 Do a POSIX getfacl (UNIX extensions).
674 ****************************************************************************/
676 struct getfacl_state {
681 static void cli_posix_getfacl_done(struct tevent_req *subreq);
683 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
684 struct event_context *ev,
685 struct cli_state *cli,
688 struct tevent_req *req = NULL, *subreq = NULL;
689 struct getfacl_state *state = NULL;
691 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
695 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
697 if (tevent_req_nomem(subreq, req)) {
698 return tevent_req_post(req, ev);
700 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
704 static void cli_posix_getfacl_done(struct tevent_req *subreq)
706 struct tevent_req *req = tevent_req_callback_data(
707 subreq, struct tevent_req);
708 struct getfacl_state *state = tevent_req_data(
709 req, struct getfacl_state);
712 status = cli_qpathinfo_recv(subreq, state, &state->data,
715 if (!NT_STATUS_IS_OK(status)) {
716 tevent_req_nterror(req, status);
719 tevent_req_done(req);
722 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
727 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
730 if (tevent_req_is_nterror(req, &status)) {
733 *prb_size = (size_t)state->num_data;
734 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
738 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
744 TALLOC_CTX *frame = talloc_stackframe();
745 struct event_context *ev = NULL;
746 struct tevent_req *req = NULL;
747 NTSTATUS status = NT_STATUS_OK;
749 if (cli_has_async_calls(cli)) {
751 * Can't use sync call while an async call is in flight
753 status = NT_STATUS_INVALID_PARAMETER;
757 ev = event_context_init(frame);
759 status = NT_STATUS_NO_MEMORY;
763 req = cli_posix_getfacl_send(frame,
768 status = NT_STATUS_NO_MEMORY;
772 if (!tevent_req_poll(req, ev)) {
773 status = map_nt_error_from_unix(errno);
777 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
781 if (!NT_STATUS_IS_OK(status)) {
782 cli_set_error(cli, status);
787 /****************************************************************************
788 Stat a file (UNIX extensions).
789 ****************************************************************************/
796 static void cli_posix_stat_done(struct tevent_req *subreq);
798 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
799 struct event_context *ev,
800 struct cli_state *cli,
803 struct tevent_req *req = NULL, *subreq = NULL;
804 struct stat_state *state = NULL;
806 req = tevent_req_create(mem_ctx, &state, struct stat_state);
810 subreq = cli_qpathinfo_send(state, ev, cli, fname,
811 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
812 if (tevent_req_nomem(subreq, req)) {
813 return tevent_req_post(req, ev);
815 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
819 static void cli_posix_stat_done(struct tevent_req *subreq)
821 struct tevent_req *req = tevent_req_callback_data(
822 subreq, struct tevent_req);
823 struct stat_state *state = tevent_req_data(req, struct stat_state);
826 status = cli_qpathinfo_recv(subreq, state, &state->data,
829 if (!NT_STATUS_IS_OK(status)) {
830 tevent_req_nterror(req, status);
833 tevent_req_done(req);
836 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
837 SMB_STRUCT_STAT *sbuf)
839 struct stat_state *state = tevent_req_data(req, struct stat_state);
842 if (tevent_req_is_nterror(req, &status)) {
846 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
847 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
848 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
849 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
851 /* assume 512 byte blocks */
852 sbuf->st_ex_blocks /= 512;
854 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
855 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
856 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
858 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
859 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
860 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
861 #if defined(HAVE_MAKEDEV)
863 uint32_t dev_major = IVAL(state->data,60);
864 uint32_t dev_minor = IVAL(state->data,68);
865 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
868 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
869 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
870 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
875 NTSTATUS cli_posix_stat(struct cli_state *cli,
877 SMB_STRUCT_STAT *sbuf)
879 TALLOC_CTX *frame = talloc_stackframe();
880 struct event_context *ev = NULL;
881 struct tevent_req *req = NULL;
882 NTSTATUS status = NT_STATUS_OK;
884 if (cli_has_async_calls(cli)) {
886 * Can't use sync call while an async call is in flight
888 status = NT_STATUS_INVALID_PARAMETER;
892 ev = event_context_init(frame);
894 status = NT_STATUS_NO_MEMORY;
898 req = cli_posix_stat_send(frame,
903 status = NT_STATUS_NO_MEMORY;
907 if (!tevent_req_poll(req, ev)) {
908 status = map_nt_error_from_unix(errno);
912 status = cli_posix_stat_recv(req, sbuf);
916 if (!NT_STATUS_IS_OK(status)) {
917 cli_set_error(cli, status);
922 /****************************************************************************
923 Chmod or chown a file internal (UNIX extensions).
924 ****************************************************************************/
926 struct cli_posix_chown_chmod_internal_state {
930 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
932 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
933 struct event_context *ev,
934 struct cli_state *cli,
940 struct tevent_req *req = NULL, *subreq = NULL;
941 struct cli_posix_chown_chmod_internal_state *state = NULL;
943 req = tevent_req_create(mem_ctx, &state,
944 struct cli_posix_chown_chmod_internal_state);
949 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
950 memset(&state->data[40], '\0', 60);
951 SIVAL(state->data,40,uid);
952 SIVAL(state->data,48,gid);
953 SIVAL(state->data,84,mode);
955 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
956 fname, state->data, sizeof(state->data));
957 if (tevent_req_nomem(subreq, req)) {
958 return tevent_req_post(req, ev);
960 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
965 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
967 NTSTATUS status = cli_setpathinfo_recv(subreq);
968 tevent_req_simple_finish_ntstatus(subreq, status);
971 /****************************************************************************
972 chmod a file (UNIX extensions).
973 ****************************************************************************/
975 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
976 struct event_context *ev,
977 struct cli_state *cli,
981 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
983 unix_perms_to_wire(mode),
988 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
990 return tevent_req_simple_recv_ntstatus(req);
993 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
995 TALLOC_CTX *frame = talloc_stackframe();
996 struct event_context *ev = NULL;
997 struct tevent_req *req = NULL;
998 NTSTATUS status = NT_STATUS_OK;
1000 if (cli_has_async_calls(cli)) {
1002 * Can't use sync call while an async call is in flight
1004 status = NT_STATUS_INVALID_PARAMETER;
1008 ev = event_context_init(frame);
1010 status = NT_STATUS_NO_MEMORY;
1014 req = cli_posix_chmod_send(frame,
1020 status = NT_STATUS_NO_MEMORY;
1024 if (!tevent_req_poll(req, ev)) {
1025 status = map_nt_error_from_unix(errno);
1029 status = cli_posix_chmod_recv(req);
1033 if (!NT_STATUS_IS_OK(status)) {
1034 cli_set_error(cli, status);
1039 /****************************************************************************
1040 chown a file (UNIX extensions).
1041 ****************************************************************************/
1043 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1044 struct event_context *ev,
1045 struct cli_state *cli,
1050 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1057 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1059 return tevent_req_simple_recv_ntstatus(req);
1062 NTSTATUS cli_posix_chown(struct cli_state *cli,
1067 TALLOC_CTX *frame = talloc_stackframe();
1068 struct event_context *ev = NULL;
1069 struct tevent_req *req = NULL;
1070 NTSTATUS status = NT_STATUS_OK;
1072 if (cli_has_async_calls(cli)) {
1074 * Can't use sync call while an async call is in flight
1076 status = NT_STATUS_INVALID_PARAMETER;
1080 ev = event_context_init(frame);
1082 status = NT_STATUS_NO_MEMORY;
1086 req = cli_posix_chown_send(frame,
1093 status = NT_STATUS_NO_MEMORY;
1097 if (!tevent_req_poll(req, ev)) {
1098 status = map_nt_error_from_unix(errno);
1102 status = cli_posix_chown_recv(req);
1106 if (!NT_STATUS_IS_OK(status)) {
1107 cli_set_error(cli, status);
1112 /****************************************************************************
1114 ****************************************************************************/
1116 static void cli_rename_done(struct tevent_req *subreq);
1118 struct cli_rename_state {
1122 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1123 struct event_context *ev,
1124 struct cli_state *cli,
1125 const char *fname_src,
1126 const char *fname_dst)
1128 struct tevent_req *req = NULL, *subreq = NULL;
1129 struct cli_rename_state *state = NULL;
1130 uint8_t additional_flags = 0;
1131 uint8_t *bytes = NULL;
1133 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1138 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1140 bytes = talloc_array(state, uint8_t, 1);
1141 if (tevent_req_nomem(bytes, req)) {
1142 return tevent_req_post(req, ev);
1145 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1146 strlen(fname_src)+1, NULL);
1147 if (tevent_req_nomem(bytes, req)) {
1148 return tevent_req_post(req, ev);
1151 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1152 talloc_get_size(bytes)+1);
1153 if (tevent_req_nomem(bytes, req)) {
1154 return tevent_req_post(req, ev);
1157 bytes[talloc_get_size(bytes)-1] = 4;
1158 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1159 strlen(fname_dst)+1, NULL);
1160 if (tevent_req_nomem(bytes, req)) {
1161 return tevent_req_post(req, ev);
1164 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1165 1, state->vwv, talloc_get_size(bytes), bytes);
1166 if (tevent_req_nomem(subreq, req)) {
1167 return tevent_req_post(req, ev);
1169 tevent_req_set_callback(subreq, cli_rename_done, req);
1173 static void cli_rename_done(struct tevent_req *subreq)
1175 struct tevent_req *req = tevent_req_callback_data(
1176 subreq, struct tevent_req);
1179 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1180 TALLOC_FREE(subreq);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 tevent_req_nterror(req, status);
1185 tevent_req_done(req);
1188 NTSTATUS cli_rename_recv(struct tevent_req *req)
1190 return tevent_req_simple_recv_ntstatus(req);
1193 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1195 TALLOC_CTX *frame = talloc_stackframe();
1196 struct event_context *ev;
1197 struct tevent_req *req;
1198 NTSTATUS status = NT_STATUS_OK;
1200 if (cli_has_async_calls(cli)) {
1202 * Can't use sync call while an async call is in flight
1204 status = NT_STATUS_INVALID_PARAMETER;
1208 ev = event_context_init(frame);
1210 status = NT_STATUS_NO_MEMORY;
1214 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1216 status = NT_STATUS_NO_MEMORY;
1220 if (!tevent_req_poll(req, ev)) {
1221 status = map_nt_error_from_unix(errno);
1225 status = cli_rename_recv(req);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 cli_set_error(cli, status);
1235 /****************************************************************************
1237 ****************************************************************************/
1239 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1241 struct cli_ntrename_internal_state {
1245 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1246 struct event_context *ev,
1247 struct cli_state *cli,
1248 const char *fname_src,
1249 const char *fname_dst,
1250 uint16_t rename_flag)
1252 struct tevent_req *req = NULL, *subreq = NULL;
1253 struct cli_ntrename_internal_state *state = NULL;
1254 uint8_t additional_flags = 0;
1255 uint8_t *bytes = NULL;
1257 req = tevent_req_create(mem_ctx, &state,
1258 struct cli_ntrename_internal_state);
1263 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1264 SSVAL(state->vwv+1, 0, rename_flag);
1266 bytes = talloc_array(state, uint8_t, 1);
1267 if (tevent_req_nomem(bytes, req)) {
1268 return tevent_req_post(req, ev);
1271 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1272 strlen(fname_src)+1, NULL);
1273 if (tevent_req_nomem(bytes, req)) {
1274 return tevent_req_post(req, ev);
1277 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1278 talloc_get_size(bytes)+1);
1279 if (tevent_req_nomem(bytes, req)) {
1280 return tevent_req_post(req, ev);
1283 bytes[talloc_get_size(bytes)-1] = 4;
1284 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1285 strlen(fname_dst)+1, NULL);
1286 if (tevent_req_nomem(bytes, req)) {
1287 return tevent_req_post(req, ev);
1290 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1291 4, state->vwv, talloc_get_size(bytes), bytes);
1292 if (tevent_req_nomem(subreq, req)) {
1293 return tevent_req_post(req, ev);
1295 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1299 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1301 struct tevent_req *req = tevent_req_callback_data(
1302 subreq, struct tevent_req);
1305 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1306 TALLOC_FREE(subreq);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 tevent_req_nterror(req, status);
1311 tevent_req_done(req);
1314 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1316 return tevent_req_simple_recv_ntstatus(req);
1319 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1320 struct event_context *ev,
1321 struct cli_state *cli,
1322 const char *fname_src,
1323 const char *fname_dst)
1325 return cli_ntrename_internal_send(mem_ctx,
1330 RENAME_FLAG_RENAME);
1333 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1335 return cli_ntrename_internal_recv(req);
1338 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1340 TALLOC_CTX *frame = talloc_stackframe();
1341 struct event_context *ev;
1342 struct tevent_req *req;
1343 NTSTATUS status = NT_STATUS_OK;
1345 if (cli_has_async_calls(cli)) {
1347 * Can't use sync call while an async call is in flight
1349 status = NT_STATUS_INVALID_PARAMETER;
1353 ev = event_context_init(frame);
1355 status = NT_STATUS_NO_MEMORY;
1359 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1361 status = NT_STATUS_NO_MEMORY;
1365 if (!tevent_req_poll(req, ev)) {
1366 status = map_nt_error_from_unix(errno);
1370 status = cli_ntrename_recv(req);
1374 if (!NT_STATUS_IS_OK(status)) {
1375 cli_set_error(cli, status);
1380 /****************************************************************************
1382 ****************************************************************************/
1384 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1385 struct event_context *ev,
1386 struct cli_state *cli,
1387 const char *fname_src,
1388 const char *fname_dst)
1390 return cli_ntrename_internal_send(mem_ctx,
1395 RENAME_FLAG_HARD_LINK);
1398 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1400 return cli_ntrename_internal_recv(req);
1403 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1405 TALLOC_CTX *frame = talloc_stackframe();
1406 struct event_context *ev;
1407 struct tevent_req *req;
1408 NTSTATUS status = NT_STATUS_OK;
1410 if (cli_has_async_calls(cli)) {
1412 * Can't use sync call while an async call is in flight
1414 status = NT_STATUS_INVALID_PARAMETER;
1418 ev = event_context_init(frame);
1420 status = NT_STATUS_NO_MEMORY;
1424 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1426 status = NT_STATUS_NO_MEMORY;
1430 if (!tevent_req_poll(req, ev)) {
1431 status = map_nt_error_from_unix(errno);
1435 status = cli_nt_hardlink_recv(req);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 cli_set_error(cli, status);
1445 /****************************************************************************
1447 ****************************************************************************/
1449 static void cli_unlink_done(struct tevent_req *subreq);
1451 struct cli_unlink_state {
1455 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1456 struct event_context *ev,
1457 struct cli_state *cli,
1459 uint16_t mayhave_attrs)
1461 struct tevent_req *req = NULL, *subreq = NULL;
1462 struct cli_unlink_state *state = NULL;
1463 uint8_t additional_flags = 0;
1464 uint8_t *bytes = NULL;
1466 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1471 SSVAL(state->vwv+0, 0, mayhave_attrs);
1473 bytes = talloc_array(state, uint8_t, 1);
1474 if (tevent_req_nomem(bytes, req)) {
1475 return tevent_req_post(req, ev);
1478 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1479 strlen(fname)+1, NULL);
1481 if (tevent_req_nomem(bytes, req)) {
1482 return tevent_req_post(req, ev);
1485 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1486 1, state->vwv, talloc_get_size(bytes), bytes);
1487 if (tevent_req_nomem(subreq, req)) {
1488 return tevent_req_post(req, ev);
1490 tevent_req_set_callback(subreq, cli_unlink_done, req);
1494 static void cli_unlink_done(struct tevent_req *subreq)
1496 struct tevent_req *req = tevent_req_callback_data(
1497 subreq, struct tevent_req);
1500 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1501 TALLOC_FREE(subreq);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 tevent_req_nterror(req, status);
1506 tevent_req_done(req);
1509 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1511 return tevent_req_simple_recv_ntstatus(req);
1514 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1516 TALLOC_CTX *frame = talloc_stackframe();
1517 struct event_context *ev;
1518 struct tevent_req *req;
1519 NTSTATUS status = NT_STATUS_OK;
1521 if (cli_has_async_calls(cli)) {
1523 * Can't use sync call while an async call is in flight
1525 status = NT_STATUS_INVALID_PARAMETER;
1529 ev = event_context_init(frame);
1531 status = NT_STATUS_NO_MEMORY;
1535 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1537 status = NT_STATUS_NO_MEMORY;
1541 if (!tevent_req_poll(req, ev)) {
1542 status = map_nt_error_from_unix(errno);
1546 status = cli_unlink_recv(req);
1550 if (!NT_STATUS_IS_OK(status)) {
1551 cli_set_error(cli, status);
1556 /****************************************************************************
1558 ****************************************************************************/
1560 static void cli_mkdir_done(struct tevent_req *subreq);
1562 struct cli_mkdir_state {
1566 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1567 struct event_context *ev,
1568 struct cli_state *cli,
1571 struct tevent_req *req = NULL, *subreq = NULL;
1572 struct cli_mkdir_state *state = NULL;
1573 uint8_t additional_flags = 0;
1574 uint8_t *bytes = NULL;
1576 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1581 bytes = talloc_array(state, uint8_t, 1);
1582 if (tevent_req_nomem(bytes, req)) {
1583 return tevent_req_post(req, ev);
1586 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1587 strlen(dname)+1, NULL);
1589 if (tevent_req_nomem(bytes, req)) {
1590 return tevent_req_post(req, ev);
1593 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1594 0, NULL, talloc_get_size(bytes), bytes);
1595 if (tevent_req_nomem(subreq, req)) {
1596 return tevent_req_post(req, ev);
1598 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1602 static void cli_mkdir_done(struct tevent_req *subreq)
1604 struct tevent_req *req = tevent_req_callback_data(
1605 subreq, struct tevent_req);
1608 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1609 TALLOC_FREE(subreq);
1610 if (!NT_STATUS_IS_OK(status)) {
1611 tevent_req_nterror(req, status);
1614 tevent_req_done(req);
1617 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1619 return tevent_req_simple_recv_ntstatus(req);
1622 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1624 TALLOC_CTX *frame = talloc_stackframe();
1625 struct event_context *ev;
1626 struct tevent_req *req;
1627 NTSTATUS status = NT_STATUS_OK;
1629 if (cli_has_async_calls(cli)) {
1631 * Can't use sync call while an async call is in flight
1633 status = NT_STATUS_INVALID_PARAMETER;
1637 ev = event_context_init(frame);
1639 status = NT_STATUS_NO_MEMORY;
1643 req = cli_mkdir_send(frame, ev, cli, dname);
1645 status = NT_STATUS_NO_MEMORY;
1649 if (!tevent_req_poll(req, ev)) {
1650 status = map_nt_error_from_unix(errno);
1654 status = cli_mkdir_recv(req);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 cli_set_error(cli, status);
1664 /****************************************************************************
1666 ****************************************************************************/
1668 static void cli_rmdir_done(struct tevent_req *subreq);
1670 struct cli_rmdir_state {
1674 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1675 struct event_context *ev,
1676 struct cli_state *cli,
1679 struct tevent_req *req = NULL, *subreq = NULL;
1680 struct cli_rmdir_state *state = NULL;
1681 uint8_t additional_flags = 0;
1682 uint8_t *bytes = NULL;
1684 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1689 bytes = talloc_array(state, uint8_t, 1);
1690 if (tevent_req_nomem(bytes, req)) {
1691 return tevent_req_post(req, ev);
1694 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1695 strlen(dname)+1, NULL);
1697 if (tevent_req_nomem(bytes, req)) {
1698 return tevent_req_post(req, ev);
1701 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1702 0, NULL, talloc_get_size(bytes), bytes);
1703 if (tevent_req_nomem(subreq, req)) {
1704 return tevent_req_post(req, ev);
1706 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1710 static void cli_rmdir_done(struct tevent_req *subreq)
1712 struct tevent_req *req = tevent_req_callback_data(
1713 subreq, struct tevent_req);
1716 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1717 TALLOC_FREE(subreq);
1718 if (!NT_STATUS_IS_OK(status)) {
1719 tevent_req_nterror(req, status);
1722 tevent_req_done(req);
1725 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1727 return tevent_req_simple_recv_ntstatus(req);
1730 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1732 TALLOC_CTX *frame = talloc_stackframe();
1733 struct event_context *ev;
1734 struct tevent_req *req;
1735 NTSTATUS status = NT_STATUS_OK;
1737 if (cli_has_async_calls(cli)) {
1739 * Can't use sync call while an async call is in flight
1741 status = NT_STATUS_INVALID_PARAMETER;
1745 ev = event_context_init(frame);
1747 status = NT_STATUS_NO_MEMORY;
1751 req = cli_rmdir_send(frame, ev, cli, dname);
1753 status = NT_STATUS_NO_MEMORY;
1757 if (!tevent_req_poll(req, ev)) {
1758 status = map_nt_error_from_unix(errno);
1762 status = cli_rmdir_recv(req);
1766 if (!NT_STATUS_IS_OK(status)) {
1767 cli_set_error(cli, status);
1772 /****************************************************************************
1773 Set or clear the delete on close flag.
1774 ****************************************************************************/
1782 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1784 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1785 NULL, 0, NULL, NULL, 0, NULL);
1786 tevent_req_simple_finish_ntstatus(subreq, status);
1789 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1790 struct event_context *ev,
1791 struct cli_state *cli,
1795 struct tevent_req *req = NULL, *subreq = NULL;
1796 struct doc_state *state = NULL;
1798 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1803 /* Setup setup word. */
1804 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1806 /* Setup param array. */
1807 SSVAL(state->param,0,fnum);
1808 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1810 /* Setup data array. */
1811 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1813 subreq = cli_trans_send(state, /* mem ctx. */
1814 ev, /* event ctx. */
1815 cli, /* cli_state. */
1816 SMBtrans2, /* cmd. */
1817 NULL, /* pipe name. */
1821 &state->setup, /* setup. */
1822 1, /* num setup uint16_t words. */
1823 0, /* max returned setup. */
1824 state->param, /* param. */
1826 2, /* max returned param. */
1827 state->data, /* data. */
1829 0); /* max returned data. */
1831 if (tevent_req_nomem(subreq, req)) {
1832 return tevent_req_post(req, ev);
1834 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1838 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1840 return tevent_req_simple_recv_ntstatus(req);
1843 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1845 TALLOC_CTX *frame = talloc_stackframe();
1846 struct event_context *ev = NULL;
1847 struct tevent_req *req = NULL;
1848 NTSTATUS status = NT_STATUS_OK;
1850 if (cli_has_async_calls(cli)) {
1852 * Can't use sync call while an async call is in flight
1854 status = NT_STATUS_INVALID_PARAMETER;
1858 ev = event_context_init(frame);
1860 status = NT_STATUS_NO_MEMORY;
1864 req = cli_nt_delete_on_close_send(frame,
1870 status = NT_STATUS_NO_MEMORY;
1874 if (!tevent_req_poll(req, ev)) {
1875 status = map_nt_error_from_unix(errno);
1879 status = cli_nt_delete_on_close_recv(req);
1883 if (!NT_STATUS_IS_OK(status)) {
1884 cli_set_error(cli, status);
1889 struct cli_ntcreate_state {
1894 static void cli_ntcreate_done(struct tevent_req *subreq);
1896 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1897 struct event_context *ev,
1898 struct cli_state *cli,
1900 uint32_t CreatFlags,
1901 uint32_t DesiredAccess,
1902 uint32_t FileAttributes,
1903 uint32_t ShareAccess,
1904 uint32_t CreateDisposition,
1905 uint32_t CreateOptions,
1906 uint8_t SecurityFlags)
1908 struct tevent_req *req, *subreq;
1909 struct cli_ntcreate_state *state;
1912 size_t converted_len;
1914 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1921 SCVAL(vwv+0, 0, 0xFF);
1926 if (cli->use_oplocks) {
1927 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1929 SIVAL(vwv+3, 1, CreatFlags);
1930 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1931 SIVAL(vwv+7, 1, DesiredAccess);
1932 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1933 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1934 SIVAL(vwv+13, 1, FileAttributes);
1935 SIVAL(vwv+15, 1, ShareAccess);
1936 SIVAL(vwv+17, 1, CreateDisposition);
1937 SIVAL(vwv+19, 1, CreateOptions);
1938 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1939 SCVAL(vwv+23, 1, SecurityFlags);
1941 bytes = talloc_array(state, uint8_t, 0);
1942 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1943 fname, strlen(fname)+1,
1946 /* sigh. this copes with broken netapp filer behaviour */
1947 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1949 if (tevent_req_nomem(bytes, req)) {
1950 return tevent_req_post(req, ev);
1953 SSVAL(vwv+2, 1, converted_len);
1955 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1956 talloc_get_size(bytes), bytes);
1957 if (tevent_req_nomem(subreq, req)) {
1958 return tevent_req_post(req, ev);
1960 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1964 static void cli_ntcreate_done(struct tevent_req *subreq)
1966 struct tevent_req *req = tevent_req_callback_data(
1967 subreq, struct tevent_req);
1968 struct cli_ntcreate_state *state = tevent_req_data(
1969 req, struct cli_ntcreate_state);
1977 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1978 &num_bytes, &bytes);
1979 TALLOC_FREE(subreq);
1980 if (!NT_STATUS_IS_OK(status)) {
1981 tevent_req_nterror(req, status);
1984 state->fnum = SVAL(vwv+2, 1);
1985 tevent_req_done(req);
1988 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1990 struct cli_ntcreate_state *state = tevent_req_data(
1991 req, struct cli_ntcreate_state);
1994 if (tevent_req_is_nterror(req, &status)) {
1997 *pfnum = state->fnum;
1998 return NT_STATUS_OK;
2001 NTSTATUS cli_ntcreate(struct cli_state *cli,
2003 uint32_t CreatFlags,
2004 uint32_t DesiredAccess,
2005 uint32_t FileAttributes,
2006 uint32_t ShareAccess,
2007 uint32_t CreateDisposition,
2008 uint32_t CreateOptions,
2009 uint8_t SecurityFlags,
2012 TALLOC_CTX *frame = talloc_stackframe();
2013 struct event_context *ev;
2014 struct tevent_req *req;
2015 NTSTATUS status = NT_STATUS_OK;
2017 if (cli_has_async_calls(cli)) {
2019 * Can't use sync call while an async call is in flight
2021 status = NT_STATUS_INVALID_PARAMETER;
2025 ev = event_context_init(frame);
2027 status = NT_STATUS_NO_MEMORY;
2031 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2032 DesiredAccess, FileAttributes, ShareAccess,
2033 CreateDisposition, CreateOptions,
2036 status = NT_STATUS_NO_MEMORY;
2040 if (!tevent_req_poll(req, ev)) {
2041 status = map_nt_error_from_unix(errno);
2045 status = cli_ntcreate_recv(req, pfid);
2048 if (!NT_STATUS_IS_OK(status)) {
2049 cli_set_error(cli, status);
2054 /****************************************************************************
2056 WARNING: if you open with O_WRONLY then getattrE won't work!
2057 ****************************************************************************/
2059 struct cli_open_state {
2065 static void cli_open_done(struct tevent_req *subreq);
2067 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2068 struct event_context *ev,
2069 struct cli_state *cli, const char *fname,
2070 int flags, int share_mode,
2071 struct tevent_req **psmbreq)
2073 struct tevent_req *req, *subreq;
2074 struct cli_open_state *state;
2076 unsigned accessmode;
2077 uint8_t additional_flags;
2080 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2086 if (flags & O_CREAT) {
2089 if (!(flags & O_EXCL)) {
2090 if (flags & O_TRUNC)
2096 accessmode = (share_mode<<4);
2098 if ((flags & O_ACCMODE) == O_RDWR) {
2100 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2105 if ((flags & O_SYNC) == O_SYNC) {
2106 accessmode |= (1<<14);
2110 if (share_mode == DENY_FCB) {
2114 SCVAL(state->vwv + 0, 0, 0xFF);
2115 SCVAL(state->vwv + 0, 1, 0);
2116 SSVAL(state->vwv + 1, 0, 0);
2117 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2118 SSVAL(state->vwv + 3, 0, accessmode);
2119 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2120 SSVAL(state->vwv + 5, 0, 0);
2121 SIVAL(state->vwv + 6, 0, 0);
2122 SSVAL(state->vwv + 8, 0, openfn);
2123 SIVAL(state->vwv + 9, 0, 0);
2124 SIVAL(state->vwv + 11, 0, 0);
2125 SIVAL(state->vwv + 13, 0, 0);
2127 additional_flags = 0;
2129 if (cli->use_oplocks) {
2130 /* if using oplocks then ask for a batch oplock via
2131 core and extended methods */
2133 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2134 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2137 bytes = talloc_array(state, uint8_t, 0);
2138 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2139 strlen(fname)+1, NULL);
2141 if (tevent_req_nomem(bytes, req)) {
2142 return tevent_req_post(req, ev);
2145 state->bytes.iov_base = (void *)bytes;
2146 state->bytes.iov_len = talloc_get_size(bytes);
2148 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2149 15, state->vwv, 1, &state->bytes);
2150 if (subreq == NULL) {
2154 tevent_req_set_callback(subreq, cli_open_done, req);
2159 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2160 struct cli_state *cli, const char *fname,
2161 int flags, int share_mode)
2163 struct tevent_req *req, *subreq;
2166 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2172 status = cli_smb_req_send(subreq);
2173 if (!NT_STATUS_IS_OK(status)) {
2174 tevent_req_nterror(req, status);
2175 return tevent_req_post(req, ev);
2180 static void cli_open_done(struct tevent_req *subreq)
2182 struct tevent_req *req = tevent_req_callback_data(
2183 subreq, struct tevent_req);
2184 struct cli_open_state *state = tevent_req_data(
2185 req, struct cli_open_state);
2191 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2193 TALLOC_FREE(subreq);
2194 if (!NT_STATUS_IS_OK(status)) {
2195 tevent_req_nterror(req, status);
2198 state->fnum = SVAL(vwv+2, 0);
2199 tevent_req_done(req);
2202 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2204 struct cli_open_state *state = tevent_req_data(
2205 req, struct cli_open_state);
2208 if (tevent_req_is_nterror(req, &status)) {
2211 *pfnum = state->fnum;
2212 return NT_STATUS_OK;
2215 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2216 int share_mode, uint16_t *pfnum)
2218 TALLOC_CTX *frame = talloc_stackframe();
2219 struct event_context *ev;
2220 struct tevent_req *req;
2221 NTSTATUS status = NT_STATUS_OK;
2223 if (cli_has_async_calls(cli)) {
2225 * Can't use sync call while an async call is in flight
2227 status = NT_STATUS_INVALID_PARAMETER;
2231 ev = event_context_init(frame);
2233 status = NT_STATUS_NO_MEMORY;
2237 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2239 status = NT_STATUS_NO_MEMORY;
2243 if (!tevent_req_poll(req, ev)) {
2244 status = map_nt_error_from_unix(errno);
2248 status = cli_open_recv(req, pfnum);
2251 if (!NT_STATUS_IS_OK(status)) {
2252 cli_set_error(cli, status);
2257 /****************************************************************************
2259 ****************************************************************************/
2261 struct cli_close_state {
2265 static void cli_close_done(struct tevent_req *subreq);
2267 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2268 struct event_context *ev,
2269 struct cli_state *cli,
2271 struct tevent_req **psubreq)
2273 struct tevent_req *req, *subreq;
2274 struct cli_close_state *state;
2276 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2281 SSVAL(state->vwv+0, 0, fnum);
2282 SIVALS(state->vwv+1, 0, -1);
2284 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2286 if (subreq == NULL) {
2290 tevent_req_set_callback(subreq, cli_close_done, req);
2295 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2296 struct event_context *ev,
2297 struct cli_state *cli,
2300 struct tevent_req *req, *subreq;
2303 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2308 status = cli_smb_req_send(subreq);
2309 if (!NT_STATUS_IS_OK(status)) {
2310 tevent_req_nterror(req, status);
2311 return tevent_req_post(req, ev);
2316 static void cli_close_done(struct tevent_req *subreq)
2318 struct tevent_req *req = tevent_req_callback_data(
2319 subreq, struct tevent_req);
2322 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2323 TALLOC_FREE(subreq);
2324 if (!NT_STATUS_IS_OK(status)) {
2325 tevent_req_nterror(req, status);
2328 tevent_req_done(req);
2331 NTSTATUS cli_close_recv(struct tevent_req *req)
2333 return tevent_req_simple_recv_ntstatus(req);
2336 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2338 TALLOC_CTX *frame = talloc_stackframe();
2339 struct event_context *ev;
2340 struct tevent_req *req;
2341 NTSTATUS status = NT_STATUS_OK;
2343 if (cli_has_async_calls(cli)) {
2345 * Can't use sync call while an async call is in flight
2347 status = NT_STATUS_INVALID_PARAMETER;
2351 ev = event_context_init(frame);
2353 status = NT_STATUS_NO_MEMORY;
2357 req = cli_close_send(frame, ev, cli, fnum);
2359 status = NT_STATUS_NO_MEMORY;
2363 if (!tevent_req_poll(req, ev)) {
2364 status = map_nt_error_from_unix(errno);
2368 status = cli_close_recv(req);
2371 if (!NT_STATUS_IS_OK(status)) {
2372 cli_set_error(cli, status);
2377 /****************************************************************************
2378 Truncate a file to a specified size
2379 ****************************************************************************/
2381 struct ftrunc_state {
2387 static void cli_ftruncate_done(struct tevent_req *subreq)
2389 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2390 NULL, 0, NULL, NULL, 0, NULL);
2391 tevent_req_simple_finish_ntstatus(subreq, status);
2394 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2395 struct event_context *ev,
2396 struct cli_state *cli,
2400 struct tevent_req *req = NULL, *subreq = NULL;
2401 struct ftrunc_state *state = NULL;
2403 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2408 /* Setup setup word. */
2409 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2411 /* Setup param array. */
2412 SSVAL(state->param,0,fnum);
2413 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2414 SSVAL(state->param,4,0);
2416 /* Setup data array. */
2417 SBVAL(state->data, 0, size);
2419 subreq = cli_trans_send(state, /* mem ctx. */
2420 ev, /* event ctx. */
2421 cli, /* cli_state. */
2422 SMBtrans2, /* cmd. */
2423 NULL, /* pipe name. */
2427 &state->setup, /* setup. */
2428 1, /* num setup uint16_t words. */
2429 0, /* max returned setup. */
2430 state->param, /* param. */
2432 2, /* max returned param. */
2433 state->data, /* data. */
2435 0); /* max returned data. */
2437 if (tevent_req_nomem(subreq, req)) {
2438 return tevent_req_post(req, ev);
2440 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2444 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2446 return tevent_req_simple_recv_ntstatus(req);
2449 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2451 TALLOC_CTX *frame = talloc_stackframe();
2452 struct event_context *ev = NULL;
2453 struct tevent_req *req = NULL;
2454 NTSTATUS status = NT_STATUS_OK;
2456 if (cli_has_async_calls(cli)) {
2458 * Can't use sync call while an async call is in flight
2460 status = NT_STATUS_INVALID_PARAMETER;
2464 ev = event_context_init(frame);
2466 status = NT_STATUS_NO_MEMORY;
2470 req = cli_ftruncate_send(frame,
2476 status = NT_STATUS_NO_MEMORY;
2480 if (!tevent_req_poll(req, ev)) {
2481 status = map_nt_error_from_unix(errno);
2485 status = cli_ftruncate_recv(req);
2489 if (!NT_STATUS_IS_OK(status)) {
2490 cli_set_error(cli, status);
2495 /****************************************************************************
2496 send a lock with a specified locktype
2497 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2498 ****************************************************************************/
2500 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2501 uint32_t offset, uint32_t len,
2502 int timeout, unsigned char locktype)
2509 SCVAL(vwv + 0, 0, 0xff);
2510 SCVAL(vwv + 0, 1, 0);
2511 SSVAL(vwv + 1, 0, 0);
2512 SSVAL(vwv + 2, 0, fnum);
2513 SCVAL(vwv + 3, 0, locktype);
2514 SCVAL(vwv + 3, 1, 0);
2515 SIVALS(vwv + 4, 0, timeout);
2516 SSVAL(vwv + 6, 0, 0);
2517 SSVAL(vwv + 7, 0, 1);
2519 SSVAL(bytes, 0, cli->pid);
2520 SIVAL(bytes, 2, offset);
2521 SIVAL(bytes, 6, len);
2523 saved_timeout = cli->timeout;
2526 cli->timeout = (timeout == -1)
2527 ? 0x7FFFFFFF : (timeout + 2*1000);
2530 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2531 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2533 cli->timeout = saved_timeout;
2538 /****************************************************************************
2540 note that timeout is in units of 2 milliseconds
2541 ****************************************************************************/
2543 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2544 uint32_t offset, uint32_t len, int timeout,
2545 enum brl_type lock_type)
2549 status = cli_locktype(cli, fnum, offset, len, timeout,
2550 (lock_type == READ_LOCK? 1 : 0));
2551 cli_set_error(cli, status);
2552 return NT_STATUS_IS_OK(status);
2555 /****************************************************************************
2557 ****************************************************************************/
2559 struct cli_unlock_state {
2564 static void cli_unlock_done(struct tevent_req *subreq);
2566 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2567 struct event_context *ev,
2568 struct cli_state *cli,
2574 struct tevent_req *req = NULL, *subreq = NULL;
2575 struct cli_unlock_state *state = NULL;
2576 uint8_t additional_flags = 0;
2578 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2583 SCVAL(state->vwv+0, 0, 0xFF);
2584 SSVAL(state->vwv+2, 0, fnum);
2585 SCVAL(state->vwv+3, 0, 0);
2586 SIVALS(state->vwv+4, 0, 0);
2587 SSVAL(state->vwv+6, 0, 1);
2588 SSVAL(state->vwv+7, 0, 0);
2590 SSVAL(state->data, 0, cli->pid);
2591 SIVAL(state->data, 2, offset);
2592 SIVAL(state->data, 6, len);
2594 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2595 8, state->vwv, 10, state->data);
2596 if (tevent_req_nomem(subreq, req)) {
2597 return tevent_req_post(req, ev);
2599 tevent_req_set_callback(subreq, cli_unlock_done, req);
2603 static void cli_unlock_done(struct tevent_req *subreq)
2605 struct tevent_req *req = tevent_req_callback_data(
2606 subreq, struct tevent_req);
2609 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2610 TALLOC_FREE(subreq);
2611 if (!NT_STATUS_IS_OK(status)) {
2612 tevent_req_nterror(req, status);
2615 tevent_req_done(req);
2618 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2620 return tevent_req_simple_recv_ntstatus(req);
2623 NTSTATUS cli_unlock(struct cli_state *cli,
2628 TALLOC_CTX *frame = talloc_stackframe();
2629 struct event_context *ev;
2630 struct tevent_req *req;
2631 NTSTATUS status = NT_STATUS_OK;
2633 if (cli_has_async_calls(cli)) {
2635 * Can't use sync call while an async call is in flight
2637 status = NT_STATUS_INVALID_PARAMETER;
2641 ev = event_context_init(frame);
2643 status = NT_STATUS_NO_MEMORY;
2647 req = cli_unlock_send(frame, ev, cli,
2650 status = NT_STATUS_NO_MEMORY;
2654 if (!tevent_req_poll(req, ev)) {
2655 status = map_nt_error_from_unix(errno);
2659 status = cli_unlock_recv(req);
2663 if (!NT_STATUS_IS_OK(status)) {
2664 cli_set_error(cli, status);
2669 /****************************************************************************
2670 Lock a file with 64 bit offsets.
2671 ****************************************************************************/
2673 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2674 uint64_t offset, uint64_t len, int timeout,
2675 enum brl_type lock_type)
2679 int saved_timeout = cli->timeout;
2683 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2684 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2687 ltype = (lock_type == READ_LOCK? 1 : 0);
2688 ltype |= LOCKING_ANDX_LARGE_FILES;
2690 SCVAL(vwv + 0, 0, 0xff);
2691 SCVAL(vwv + 0, 1, 0);
2692 SSVAL(vwv + 1, 0, 0);
2693 SSVAL(vwv + 2, 0, fnum);
2694 SCVAL(vwv + 3, 0, ltype);
2695 SCVAL(vwv + 3, 1, 0);
2696 SIVALS(vwv + 4, 0, timeout);
2697 SSVAL(vwv + 6, 0, 0);
2698 SSVAL(vwv + 7, 0, 1);
2700 SIVAL(bytes, 0, cli->pid);
2701 SOFF_T_R(bytes, 4, offset);
2702 SOFF_T_R(bytes, 12, len);
2704 saved_timeout = cli->timeout;
2707 cli->timeout = (timeout == -1)
2708 ? 0x7FFFFFFF : (timeout + 2*1000);
2711 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2712 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2714 cli->timeout = saved_timeout;
2716 cli_set_error(cli, status);
2717 return NT_STATUS_IS_OK(status);
2720 /****************************************************************************
2721 Unlock a file with 64 bit offsets.
2722 ****************************************************************************/
2724 struct cli_unlock64_state {
2729 static void cli_unlock64_done(struct tevent_req *subreq);
2731 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2732 struct event_context *ev,
2733 struct cli_state *cli,
2739 struct tevent_req *req = NULL, *subreq = NULL;
2740 struct cli_unlock64_state *state = NULL;
2741 uint8_t additional_flags = 0;
2743 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2748 SCVAL(state->vwv+0, 0, 0xff);
2749 SSVAL(state->vwv+2, 0, fnum);
2750 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2751 SIVALS(state->vwv+4, 0, 0);
2752 SSVAL(state->vwv+6, 0, 1);
2753 SSVAL(state->vwv+7, 0, 0);
2755 SIVAL(state->data, 0, cli->pid);
2756 SOFF_T_R(state->data, 4, offset);
2757 SOFF_T_R(state->data, 12, len);
2759 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2760 8, state->vwv, 20, state->data);
2761 if (tevent_req_nomem(subreq, req)) {
2762 return tevent_req_post(req, ev);
2764 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2768 static void cli_unlock64_done(struct tevent_req *subreq)
2770 struct tevent_req *req = tevent_req_callback_data(
2771 subreq, struct tevent_req);
2774 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2775 TALLOC_FREE(subreq);
2776 if (!NT_STATUS_IS_OK(status)) {
2777 tevent_req_nterror(req, status);
2780 tevent_req_done(req);
2783 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2785 return tevent_req_simple_recv_ntstatus(req);
2788 NTSTATUS cli_unlock64(struct cli_state *cli,
2793 TALLOC_CTX *frame = talloc_stackframe();
2794 struct event_context *ev;
2795 struct tevent_req *req;
2796 NTSTATUS status = NT_STATUS_OK;
2798 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2799 return cli_unlock(cli, fnum, offset, len);
2802 if (cli_has_async_calls(cli)) {
2804 * Can't use sync call while an async call is in flight
2806 status = NT_STATUS_INVALID_PARAMETER;
2810 ev = event_context_init(frame);
2812 status = NT_STATUS_NO_MEMORY;
2816 req = cli_unlock64_send(frame, ev, cli,
2819 status = NT_STATUS_NO_MEMORY;
2823 if (!tevent_req_poll(req, ev)) {
2824 status = map_nt_error_from_unix(errno);
2828 status = cli_unlock64_recv(req);
2832 if (!NT_STATUS_IS_OK(status)) {
2833 cli_set_error(cli, status);
2838 /****************************************************************************
2839 Get/unlock a POSIX lock on a file - internal function.
2840 ****************************************************************************/
2842 struct posix_lock_state {
2845 uint8_t data[POSIX_LOCK_DATA_SIZE];
2848 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2850 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2851 NULL, 0, NULL, NULL, 0, NULL);
2852 tevent_req_simple_finish_ntstatus(subreq, status);
2855 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2856 struct event_context *ev,
2857 struct cli_state *cli,
2862 enum brl_type lock_type)
2864 struct tevent_req *req = NULL, *subreq = NULL;
2865 struct posix_lock_state *state = NULL;
2867 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2872 /* Setup setup word. */
2873 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2875 /* Setup param array. */
2876 SSVAL(&state->param, 0, fnum);
2877 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2879 /* Setup data array. */
2880 switch (lock_type) {
2882 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2883 POSIX_LOCK_TYPE_READ);
2886 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2887 POSIX_LOCK_TYPE_WRITE);
2890 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2891 POSIX_LOCK_TYPE_UNLOCK);
2898 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2899 POSIX_LOCK_FLAG_WAIT);
2901 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2902 POSIX_LOCK_FLAG_NOWAIT);
2905 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2906 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2907 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2909 subreq = cli_trans_send(state, /* mem ctx. */
2910 ev, /* event ctx. */
2911 cli, /* cli_state. */
2912 SMBtrans2, /* cmd. */
2913 NULL, /* pipe name. */
2917 &state->setup, /* setup. */
2918 1, /* num setup uint16_t words. */
2919 0, /* max returned setup. */
2920 state->param, /* param. */
2922 2, /* max returned param. */
2923 state->data, /* data. */
2924 POSIX_LOCK_DATA_SIZE, /* num data. */
2925 0); /* max returned data. */
2927 if (tevent_req_nomem(subreq, req)) {
2928 return tevent_req_post(req, ev);
2930 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2934 /****************************************************************************
2936 ****************************************************************************/
2938 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2939 struct event_context *ev,
2940 struct cli_state *cli,
2945 enum brl_type lock_type)
2947 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2948 wait_lock, lock_type);
2951 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2953 return tevent_req_simple_recv_ntstatus(req);
2956 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2957 uint64_t offset, uint64_t len,
2958 bool wait_lock, enum brl_type lock_type)
2960 TALLOC_CTX *frame = talloc_stackframe();
2961 struct event_context *ev = NULL;
2962 struct tevent_req *req = NULL;
2963 NTSTATUS status = NT_STATUS_OK;
2965 if (cli_has_async_calls(cli)) {
2967 * Can't use sync call while an async call is in flight
2969 status = NT_STATUS_INVALID_PARAMETER;
2973 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2974 status = NT_STATUS_INVALID_PARAMETER;
2978 ev = event_context_init(frame);
2980 status = NT_STATUS_NO_MEMORY;
2984 req = cli_posix_lock_send(frame,
2993 status = NT_STATUS_NO_MEMORY;
2997 if (!tevent_req_poll(req, ev)) {
2998 status = map_nt_error_from_unix(errno);
3002 status = cli_posix_lock_recv(req);
3006 if (!NT_STATUS_IS_OK(status)) {
3007 cli_set_error(cli, status);
3012 /****************************************************************************
3013 POSIX Unlock a file.
3014 ****************************************************************************/
3016 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3017 struct event_context *ev,
3018 struct cli_state *cli,
3023 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3024 false, UNLOCK_LOCK);
3027 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3029 return tevent_req_simple_recv_ntstatus(req);
3032 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3034 TALLOC_CTX *frame = talloc_stackframe();
3035 struct event_context *ev = NULL;
3036 struct tevent_req *req = NULL;
3037 NTSTATUS status = NT_STATUS_OK;
3039 if (cli_has_async_calls(cli)) {
3041 * Can't use sync call while an async call is in flight
3043 status = NT_STATUS_INVALID_PARAMETER;
3047 ev = event_context_init(frame);
3049 status = NT_STATUS_NO_MEMORY;
3053 req = cli_posix_unlock_send(frame,
3060 status = NT_STATUS_NO_MEMORY;
3064 if (!tevent_req_poll(req, ev)) {
3065 status = map_nt_error_from_unix(errno);
3069 status = cli_posix_unlock_recv(req);
3073 if (!NT_STATUS_IS_OK(status)) {
3074 cli_set_error(cli, status);
3079 /****************************************************************************
3080 Do a SMBgetattrE call.
3081 ****************************************************************************/
3083 static void cli_getattrE_done(struct tevent_req *subreq);
3085 struct cli_getattrE_state {
3095 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3096 struct event_context *ev,
3097 struct cli_state *cli,
3100 struct tevent_req *req = NULL, *subreq = NULL;
3101 struct cli_getattrE_state *state = NULL;
3102 uint8_t additional_flags = 0;
3104 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3109 state->zone_offset = cli->serverzone;
3110 SSVAL(state->vwv+0,0,fnum);
3112 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3113 1, state->vwv, 0, NULL);
3114 if (tevent_req_nomem(subreq, req)) {
3115 return tevent_req_post(req, ev);
3117 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3121 static void cli_getattrE_done(struct tevent_req *subreq)
3123 struct tevent_req *req = tevent_req_callback_data(
3124 subreq, struct tevent_req);
3125 struct cli_getattrE_state *state = tevent_req_data(
3126 req, struct cli_getattrE_state);
3128 uint16_t *vwv = NULL;
3132 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3134 TALLOC_FREE(subreq);
3135 if (!NT_STATUS_IS_OK(status)) {
3136 tevent_req_nterror(req, status);
3140 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3141 state->attr = SVAL(vwv+10,0);
3142 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3143 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3144 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3146 tevent_req_done(req);
3149 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3152 time_t *change_time,
3153 time_t *access_time,
3156 struct cli_getattrE_state *state = tevent_req_data(
3157 req, struct cli_getattrE_state);
3160 if (tevent_req_is_nterror(req, &status)) {
3164 *attr = state->attr;
3167 *size = state->size;
3170 *change_time = state->change_time;
3173 *access_time = state->access_time;
3176 *write_time = state->write_time;
3178 return NT_STATUS_OK;
3181 NTSTATUS cli_getattrE(struct cli_state *cli,
3185 time_t *change_time,
3186 time_t *access_time,
3189 TALLOC_CTX *frame = talloc_stackframe();
3190 struct event_context *ev = NULL;
3191 struct tevent_req *req = NULL;
3192 NTSTATUS status = NT_STATUS_OK;
3194 if (cli_has_async_calls(cli)) {
3196 * Can't use sync call while an async call is in flight
3198 status = NT_STATUS_INVALID_PARAMETER;
3202 ev = event_context_init(frame);
3204 status = NT_STATUS_NO_MEMORY;
3208 req = cli_getattrE_send(frame, ev, cli, fnum);
3210 status = NT_STATUS_NO_MEMORY;
3214 if (!tevent_req_poll(req, ev)) {
3215 status = map_nt_error_from_unix(errno);
3219 status = cli_getattrE_recv(req,
3228 if (!NT_STATUS_IS_OK(status)) {
3229 cli_set_error(cli, status);
3234 /****************************************************************************
3236 ****************************************************************************/
3238 static void cli_getatr_done(struct tevent_req *subreq);
3240 struct cli_getatr_state {
3247 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3248 struct event_context *ev,
3249 struct cli_state *cli,
3252 struct tevent_req *req = NULL, *subreq = NULL;
3253 struct cli_getatr_state *state = NULL;
3254 uint8_t additional_flags = 0;
3255 uint8_t *bytes = NULL;
3257 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3262 state->zone_offset = cli->serverzone;
3264 bytes = talloc_array(state, uint8_t, 1);
3265 if (tevent_req_nomem(bytes, req)) {
3266 return tevent_req_post(req, ev);
3269 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3270 strlen(fname)+1, NULL);
3272 if (tevent_req_nomem(bytes, req)) {
3273 return tevent_req_post(req, ev);
3276 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3277 0, NULL, talloc_get_size(bytes), bytes);
3278 if (tevent_req_nomem(subreq, req)) {
3279 return tevent_req_post(req, ev);
3281 tevent_req_set_callback(subreq, cli_getatr_done, req);
3285 static void cli_getatr_done(struct tevent_req *subreq)
3287 struct tevent_req *req = tevent_req_callback_data(
3288 subreq, struct tevent_req);
3289 struct cli_getatr_state *state = tevent_req_data(
3290 req, struct cli_getatr_state);
3292 uint16_t *vwv = NULL;
3296 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3298 TALLOC_FREE(subreq);
3299 if (!NT_STATUS_IS_OK(status)) {
3300 tevent_req_nterror(req, status);
3304 state->attr = SVAL(vwv+0,0);
3305 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3306 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3308 tevent_req_done(req);
3311 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3316 struct cli_getatr_state *state = tevent_req_data(
3317 req, struct cli_getatr_state);
3320 if (tevent_req_is_nterror(req, &status)) {
3324 *attr = state->attr;
3327 *size = state->size;
3330 *write_time = state->write_time;
3332 return NT_STATUS_OK;
3335 NTSTATUS cli_getatr(struct cli_state *cli,
3341 TALLOC_CTX *frame = talloc_stackframe();
3342 struct event_context *ev = NULL;
3343 struct tevent_req *req = NULL;
3344 NTSTATUS status = NT_STATUS_OK;
3346 if (cli_has_async_calls(cli)) {
3348 * Can't use sync call while an async call is in flight
3350 status = NT_STATUS_INVALID_PARAMETER;
3354 ev = event_context_init(frame);
3356 status = NT_STATUS_NO_MEMORY;
3360 req = cli_getatr_send(frame, ev, cli, fname);
3362 status = NT_STATUS_NO_MEMORY;
3366 if (!tevent_req_poll(req, ev)) {
3367 status = map_nt_error_from_unix(errno);
3371 status = cli_getatr_recv(req,
3378 if (!NT_STATUS_IS_OK(status)) {
3379 cli_set_error(cli, status);
3384 /****************************************************************************
3385 Do a SMBsetattrE call.
3386 ****************************************************************************/
3388 static void cli_setattrE_done(struct tevent_req *subreq);
3390 struct cli_setattrE_state {
3394 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3395 struct event_context *ev,
3396 struct cli_state *cli,
3402 struct tevent_req *req = NULL, *subreq = NULL;
3403 struct cli_setattrE_state *state = NULL;
3404 uint8_t additional_flags = 0;
3406 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3411 SSVAL(state->vwv+0, 0, fnum);
3412 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3414 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3416 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3419 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3420 7, state->vwv, 0, NULL);
3421 if (tevent_req_nomem(subreq, req)) {
3422 return tevent_req_post(req, ev);
3424 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3428 static void cli_setattrE_done(struct tevent_req *subreq)
3430 struct tevent_req *req = tevent_req_callback_data(
3431 subreq, struct tevent_req);
3434 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3435 TALLOC_FREE(subreq);
3436 if (!NT_STATUS_IS_OK(status)) {
3437 tevent_req_nterror(req, status);
3440 tevent_req_done(req);
3443 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3445 return tevent_req_simple_recv_ntstatus(req);
3448 NTSTATUS cli_setattrE(struct cli_state *cli,
3454 TALLOC_CTX *frame = talloc_stackframe();
3455 struct event_context *ev = NULL;
3456 struct tevent_req *req = NULL;
3457 NTSTATUS status = NT_STATUS_OK;
3459 if (cli_has_async_calls(cli)) {
3461 * Can't use sync call while an async call is in flight
3463 status = NT_STATUS_INVALID_PARAMETER;
3467 ev = event_context_init(frame);
3469 status = NT_STATUS_NO_MEMORY;
3473 req = cli_setattrE_send(frame, ev,
3481 status = NT_STATUS_NO_MEMORY;
3485 if (!tevent_req_poll(req, ev)) {
3486 status = map_nt_error_from_unix(errno);
3490 status = cli_setattrE_recv(req);
3494 if (!NT_STATUS_IS_OK(status)) {
3495 cli_set_error(cli, status);
3500 /****************************************************************************
3501 Do a SMBsetatr call.
3502 ****************************************************************************/
3504 static void cli_setatr_done(struct tevent_req *subreq);
3506 struct cli_setatr_state {
3510 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3511 struct event_context *ev,
3512 struct cli_state *cli,
3517 struct tevent_req *req = NULL, *subreq = NULL;
3518 struct cli_setatr_state *state = NULL;
3519 uint8_t additional_flags = 0;
3520 uint8_t *bytes = NULL;
3522 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3527 SSVAL(state->vwv+0, 0, attr);
3528 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3530 bytes = talloc_array(state, uint8_t, 1);
3531 if (tevent_req_nomem(bytes, req)) {
3532 return tevent_req_post(req, ev);
3535 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3536 strlen(fname)+1, NULL);
3537 if (tevent_req_nomem(bytes, req)) {
3538 return tevent_req_post(req, ev);
3540 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3541 talloc_get_size(bytes)+1);
3542 if (tevent_req_nomem(bytes, req)) {
3543 return tevent_req_post(req, ev);
3546 bytes[talloc_get_size(bytes)-1] = 4;
3547 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3549 if (tevent_req_nomem(bytes, req)) {
3550 return tevent_req_post(req, ev);
3553 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3554 8, state->vwv, talloc_get_size(bytes), bytes);
3555 if (tevent_req_nomem(subreq, req)) {
3556 return tevent_req_post(req, ev);
3558 tevent_req_set_callback(subreq, cli_setatr_done, req);
3562 static void cli_setatr_done(struct tevent_req *subreq)
3564 struct tevent_req *req = tevent_req_callback_data(
3565 subreq, struct tevent_req);
3568 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3569 TALLOC_FREE(subreq);
3570 if (!NT_STATUS_IS_OK(status)) {
3571 tevent_req_nterror(req, status);
3574 tevent_req_done(req);
3577 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3579 return tevent_req_simple_recv_ntstatus(req);
3582 NTSTATUS cli_setatr(struct cli_state *cli,
3587 TALLOC_CTX *frame = talloc_stackframe();
3588 struct event_context *ev = NULL;
3589 struct tevent_req *req = NULL;
3590 NTSTATUS status = NT_STATUS_OK;
3592 if (cli_has_async_calls(cli)) {
3594 * Can't use sync call while an async call is in flight
3596 status = NT_STATUS_INVALID_PARAMETER;
3600 ev = event_context_init(frame);
3602 status = NT_STATUS_NO_MEMORY;
3606 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3608 status = NT_STATUS_NO_MEMORY;
3612 if (!tevent_req_poll(req, ev)) {
3613 status = map_nt_error_from_unix(errno);
3617 status = cli_setatr_recv(req);
3621 if (!NT_STATUS_IS_OK(status)) {
3622 cli_set_error(cli, status);
3627 /****************************************************************************
3628 Check for existance of a dir.
3629 ****************************************************************************/
3631 static void cli_chkpath_done(struct tevent_req *subreq);
3633 struct cli_chkpath_state {
3637 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3638 struct event_context *ev,
3639 struct cli_state *cli,
3642 struct tevent_req *req = NULL, *subreq = NULL;
3643 struct cli_chkpath_state *state = NULL;
3644 uint8_t additional_flags = 0;
3645 uint8_t *bytes = NULL;
3647 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3652 bytes = talloc_array(state, uint8_t, 1);
3653 if (tevent_req_nomem(bytes, req)) {
3654 return tevent_req_post(req, ev);
3657 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3658 strlen(fname)+1, NULL);
3660 if (tevent_req_nomem(bytes, req)) {
3661 return tevent_req_post(req, ev);
3664 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3665 0, NULL, talloc_get_size(bytes), bytes);
3666 if (tevent_req_nomem(subreq, req)) {
3667 return tevent_req_post(req, ev);
3669 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3673 static void cli_chkpath_done(struct tevent_req *subreq)
3675 struct tevent_req *req = tevent_req_callback_data(
3676 subreq, struct tevent_req);
3679 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3680 TALLOC_FREE(subreq);
3681 if (!NT_STATUS_IS_OK(status)) {
3682 tevent_req_nterror(req, status);
3685 tevent_req_done(req);
3688 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3690 return tevent_req_simple_recv_ntstatus(req);
3693 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3695 TALLOC_CTX *frame = talloc_stackframe();
3696 struct event_context *ev = NULL;
3697 struct tevent_req *req = NULL;
3699 NTSTATUS status = NT_STATUS_OK;
3701 if (cli_has_async_calls(cli)) {
3703 * Can't use sync call while an async call is in flight
3705 status = NT_STATUS_INVALID_PARAMETER;
3709 path2 = talloc_strdup(frame, path);
3711 status = NT_STATUS_NO_MEMORY;
3714 trim_char(path2,'\0','\\');
3716 path2 = talloc_strdup(frame, "\\");
3718 status = NT_STATUS_NO_MEMORY;
3723 ev = event_context_init(frame);
3725 status = NT_STATUS_NO_MEMORY;
3729 req = cli_chkpath_send(frame, ev, cli, path2);
3731 status = NT_STATUS_NO_MEMORY;
3735 if (!tevent_req_poll(req, ev)) {
3736 status = map_nt_error_from_unix(errno);
3740 status = cli_chkpath_recv(req);
3744 if (!NT_STATUS_IS_OK(status)) {
3745 cli_set_error(cli, status);
3750 /****************************************************************************
3752 ****************************************************************************/
3754 static void cli_dskattr_done(struct tevent_req *subreq);
3756 struct cli_dskattr_state {
3762 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3763 struct event_context *ev,
3764 struct cli_state *cli)
3766 struct tevent_req *req = NULL, *subreq = NULL;
3767 struct cli_dskattr_state *state = NULL;
3768 uint8_t additional_flags = 0;
3770 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3775 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3777 if (tevent_req_nomem(subreq, req)) {
3778 return tevent_req_post(req, ev);
3780 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3784 static void cli_dskattr_done(struct tevent_req *subreq)
3786 struct tevent_req *req = tevent_req_callback_data(
3787 subreq, struct tevent_req);
3788 struct cli_dskattr_state *state = tevent_req_data(
3789 req, struct cli_dskattr_state);
3791 uint16_t *vwv = NULL;
3795 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3797 TALLOC_FREE(subreq);
3798 if (!NT_STATUS_IS_OK(status)) {
3799 tevent_req_nterror(req, status);
3802 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3803 state->total = SVAL(vwv+0, 0);
3804 state->avail = SVAL(vwv+3, 0);
3805 tevent_req_done(req);
3808 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3810 struct cli_dskattr_state *state = tevent_req_data(
3811 req, struct cli_dskattr_state);
3814 if (tevent_req_is_nterror(req, &status)) {
3817 *bsize = state->bsize;
3818 *total = state->total;
3819 *avail = state->avail;
3820 return NT_STATUS_OK;
3823 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3825 TALLOC_CTX *frame = talloc_stackframe();
3826 struct event_context *ev = NULL;
3827 struct tevent_req *req = NULL;
3828 NTSTATUS status = NT_STATUS_OK;
3830 if (cli_has_async_calls(cli)) {
3832 * Can't use sync call while an async call is in flight
3834 status = NT_STATUS_INVALID_PARAMETER;
3838 ev = event_context_init(frame);
3840 status = NT_STATUS_NO_MEMORY;
3844 req = cli_dskattr_send(frame, ev, cli);
3846 status = NT_STATUS_NO_MEMORY;
3850 if (!tevent_req_poll(req, ev)) {
3851 status = map_nt_error_from_unix(errno);
3855 status = cli_dskattr_recv(req, bsize, total, avail);
3859 if (!NT_STATUS_IS_OK(status)) {
3860 cli_set_error(cli, status);
3865 /****************************************************************************
3866 Create and open a temporary file.
3867 ****************************************************************************/
3869 static void cli_ctemp_done(struct tevent_req *subreq);
3871 struct ctemp_state {
3877 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3878 struct event_context *ev,
3879 struct cli_state *cli,
3882 struct tevent_req *req = NULL, *subreq = NULL;
3883 struct ctemp_state *state = NULL;
3884 uint8_t additional_flags = 0;
3885 uint8_t *bytes = NULL;
3887 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3892 SSVAL(state->vwv,0,0);
3893 SIVALS(state->vwv+1,0,-1);
3895 bytes = talloc_array(state, uint8_t, 1);
3896 if (tevent_req_nomem(bytes, req)) {
3897 return tevent_req_post(req, ev);
3900 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3901 strlen(path)+1, NULL);
3902 if (tevent_req_nomem(bytes, req)) {
3903 return tevent_req_post(req, ev);
3906 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3907 3, state->vwv, talloc_get_size(bytes), bytes);
3908 if (tevent_req_nomem(subreq, req)) {
3909 return tevent_req_post(req, ev);
3911 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3915 static void cli_ctemp_done(struct tevent_req *subreq)
3917 struct tevent_req *req = tevent_req_callback_data(
3918 subreq, struct tevent_req);
3919 struct ctemp_state *state = tevent_req_data(
3920 req, struct ctemp_state);
3924 uint32_t num_bytes = 0;
3925 uint8_t *bytes = NULL;
3928 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3929 &num_bytes, &bytes);
3930 TALLOC_FREE(subreq);
3931 if (!NT_STATUS_IS_OK(status)) {
3932 tevent_req_nterror(req, status);
3936 state->fnum = SVAL(vwv+0, 0);
3938 /* From W2K3, the result is just the ASCII name */
3939 if (num_bytes < 2) {
3940 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3944 if (pull_string_talloc(state,
3951 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3954 tevent_req_done(req);
3957 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3962 struct ctemp_state *state = tevent_req_data(req,
3963 struct ctemp_state);
3966 if (tevent_req_is_nterror(req, &status)) {
3969 *pfnum = state->fnum;
3970 *outfile = talloc_strdup(ctx, state->ret_path);
3972 return NT_STATUS_NO_MEMORY;
3974 return NT_STATUS_OK;
3977 NTSTATUS cli_ctemp(struct cli_state *cli,
3983 TALLOC_CTX *frame = talloc_stackframe();
3984 struct event_context *ev;
3985 struct tevent_req *req;
3986 NTSTATUS status = NT_STATUS_OK;
3988 if (cli_has_async_calls(cli)) {
3990 * Can't use sync call while an async call is in flight
3992 status = NT_STATUS_INVALID_PARAMETER;
3996 ev = event_context_init(frame);
3998 status = NT_STATUS_NO_MEMORY;
4002 req = cli_ctemp_send(frame, ev, cli, path);
4004 status = NT_STATUS_NO_MEMORY;
4008 if (!tevent_req_poll(req, ev)) {
4009 status = map_nt_error_from_unix(errno);
4013 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4017 if (!NT_STATUS_IS_OK(status)) {
4018 cli_set_error(cli, status);
4024 send a raw ioctl - used by the torture code
4026 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4031 SSVAL(vwv+0, 0, fnum);
4032 SSVAL(vwv+1, 0, code>>16);
4033 SSVAL(vwv+2, 0, (code&0xFFFF));
4035 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4036 NULL, 0, NULL, NULL, NULL, NULL);
4037 if (!NT_STATUS_IS_OK(status)) {
4040 *blob = data_blob_null;
4041 return NT_STATUS_OK;
4044 /*********************************************************
4045 Set an extended attribute utility fn.
4046 *********************************************************/
4048 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4049 uint8_t *param, unsigned int param_len,
4050 const char *ea_name,
4051 const char *ea_val, size_t ea_len)
4054 unsigned int data_len = 0;
4055 uint8_t *data = NULL;
4057 size_t ea_namelen = strlen(ea_name);
4060 SSVAL(setup, 0, setup_val);
4062 if (ea_namelen == 0 && ea_len == 0) {
4064 data = (uint8_t *)SMB_MALLOC(data_len);
4066 return NT_STATUS_NO_MEMORY;
4069 SIVAL(p,0,data_len);
4071 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4072 data = (uint8_t *)SMB_MALLOC(data_len);
4074 return NT_STATUS_NO_MEMORY;
4077 SIVAL(p,0,data_len);
4079 SCVAL(p, 0, 0); /* EA flags. */
4080 SCVAL(p, 1, ea_namelen);
4081 SSVAL(p, 2, ea_len);
4082 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4083 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4086 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4088 param, param_len, 2,
4089 data, data_len, cli->max_xmit,
4091 NULL, 0, NULL, /* rsetup */
4092 NULL, 0, NULL, /* rparam */
4093 NULL, 0, NULL); /* rdata */
4098 /*********************************************************
4099 Set an extended attribute on a pathname.
4100 *********************************************************/
4102 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4103 const char *ea_name, const char *ea_val,
4106 unsigned int param_len = 0;
4108 size_t srclen = 2*(strlen(path)+1);
4112 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4114 return NT_STATUS_NO_MEMORY;
4116 memset(param, '\0', 6);
4117 SSVAL(param,0,SMB_INFO_SET_EA);
4118 p = (char *)(¶m[6]);
4120 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4121 param_len = PTR_DIFF(p, param);
4123 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4124 ea_name, ea_val, ea_len);
4129 /*********************************************************
4130 Set an extended attribute on an fnum.
4131 *********************************************************/
4133 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4134 const char *ea_name, const char *ea_val,
4139 memset(param, 0, 6);
4140 SSVAL(param,0,fnum);
4141 SSVAL(param,2,SMB_INFO_SET_EA);
4143 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4144 ea_name, ea_val, ea_len);
4147 /*********************************************************
4148 Get an extended attribute list utility fn.
4149 *********************************************************/
4151 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4153 size_t *pnum_eas, struct ea_struct **pea_list)
4155 struct ea_struct *ea_list = NULL;
4160 if (rdata_len < 4) {
4164 ea_size = (size_t)IVAL(rdata,0);
4165 if (ea_size > rdata_len) {
4170 /* No EA's present. */
4179 /* Validate the EA list and count it. */
4180 for (num_eas = 0; ea_size >= 4; num_eas++) {
4181 unsigned int ea_namelen = CVAL(p,1);
4182 unsigned int ea_valuelen = SVAL(p,2);
4183 if (ea_namelen == 0) {
4186 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4189 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4190 p += 4 + ea_namelen + 1 + ea_valuelen;
4199 *pnum_eas = num_eas;
4201 /* Caller only wants number of EA's. */
4205 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4210 ea_size = (size_t)IVAL(rdata,0);
4213 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4214 struct ea_struct *ea = &ea_list[num_eas];
4215 fstring unix_ea_name;
4216 unsigned int ea_namelen = CVAL(p,1);
4217 unsigned int ea_valuelen = SVAL(p,2);
4219 ea->flags = CVAL(p,0);
4220 unix_ea_name[0] = '\0';
4221 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4222 ea->name = talloc_strdup(ea_list, unix_ea_name);
4226 /* Ensure the value is null terminated (in case it's a string). */
4227 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4228 if (!ea->value.data) {
4232 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4234 ea->value.data[ea_valuelen] = 0;
4236 p += 4 + ea_namelen + 1 + ea_valuelen;
4239 *pea_list = ea_list;
4243 TALLOC_FREE(ea_list);
4247 /*********************************************************
4248 Get an extended attribute list from a pathname.
4249 *********************************************************/
4251 struct cli_get_ea_list_path_state {
4256 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4258 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4259 struct tevent_context *ev,
4260 struct cli_state *cli,
4263 struct tevent_req *req, *subreq;
4264 struct cli_get_ea_list_path_state *state;
4266 req = tevent_req_create(mem_ctx, &state,
4267 struct cli_get_ea_list_path_state);
4271 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4272 SMB_INFO_QUERY_ALL_EAS, 4,
4274 if (tevent_req_nomem(subreq, req)) {
4275 return tevent_req_post(req, ev);
4277 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4281 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4283 struct tevent_req *req = tevent_req_callback_data(
4284 subreq, struct tevent_req);
4285 struct cli_get_ea_list_path_state *state = tevent_req_data(
4286 req, struct cli_get_ea_list_path_state);
4289 status = cli_qpathinfo_recv(subreq, state, &state->data,
4291 TALLOC_FREE(subreq);
4292 if (!NT_STATUS_IS_OK(status)) {
4293 tevent_req_nterror(req, status);
4296 tevent_req_done(req);
4299 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4300 size_t *pnum_eas, struct ea_struct **peas)
4302 struct cli_get_ea_list_path_state *state = tevent_req_data(
4303 req, struct cli_get_ea_list_path_state);
4306 if (tevent_req_is_nterror(req, &status)) {
4309 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4311 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4313 return NT_STATUS_OK;
4316 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4319 struct ea_struct **pea_list)
4321 TALLOC_CTX *frame = talloc_stackframe();
4322 struct event_context *ev = NULL;
4323 struct tevent_req *req = NULL;
4324 NTSTATUS status = NT_STATUS_NO_MEMORY;
4326 if (cli_has_async_calls(cli)) {
4328 * Can't use sync call while an async call is in flight
4330 status = NT_STATUS_INVALID_PARAMETER;
4333 ev = event_context_init(frame);
4337 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4341 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4344 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4347 if (!NT_STATUS_IS_OK(status)) {
4348 cli_set_error(cli, status);
4353 /****************************************************************************
4354 Convert open "flags" arg to uint32_t on wire.
4355 ****************************************************************************/
4357 static uint32_t open_flags_to_wire(int flags)
4359 int open_mode = flags & O_ACCMODE;
4362 switch (open_mode) {
4364 ret |= SMB_O_WRONLY;
4371 ret |= SMB_O_RDONLY;
4375 if (flags & O_CREAT) {
4378 if (flags & O_EXCL) {
4381 if (flags & O_TRUNC) {
4385 if (flags & O_SYNC) {
4389 if (flags & O_APPEND) {
4390 ret |= SMB_O_APPEND;
4392 #if defined(O_DIRECT)
4393 if (flags & O_DIRECT) {
4394 ret |= SMB_O_DIRECT;
4397 #if defined(O_DIRECTORY)
4398 if (flags & O_DIRECTORY) {
4399 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4400 ret |= SMB_O_DIRECTORY;
4406 /****************************************************************************
4407 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4408 ****************************************************************************/
4410 struct posix_open_state {
4414 uint16_t fnum; /* Out */
4417 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4419 struct tevent_req *req = tevent_req_callback_data(
4420 subreq, struct tevent_req);
4421 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4426 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4427 NULL, 0, NULL, &data, 12, &num_data);
4428 TALLOC_FREE(subreq);
4429 if (!NT_STATUS_IS_OK(status)) {
4430 tevent_req_nterror(req, status);
4433 state->fnum = SVAL(data,2);
4434 tevent_req_done(req);
4437 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4438 struct event_context *ev,
4439 struct cli_state *cli,
4445 struct tevent_req *req = NULL, *subreq = NULL;
4446 struct posix_open_state *state = NULL;
4447 uint32_t wire_flags = open_flags_to_wire(flags);
4449 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4454 /* Setup setup word. */
4455 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4457 /* Setup param array. */
4458 state->param = talloc_array(state, uint8_t, 6);
4459 if (tevent_req_nomem(state->param, req)) {
4460 return tevent_req_post(req, ev);
4462 memset(state->param, '\0', 6);
4463 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4465 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4466 strlen(fname)+1, NULL);
4468 if (tevent_req_nomem(state->param, req)) {
4469 return tevent_req_post(req, ev);
4472 /* Setup data words. */
4474 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4475 wire_flags |= SMB_O_DIRECTORY;
4478 SIVAL(state->data,0,0); /* No oplock. */
4479 SIVAL(state->data,4,wire_flags);
4480 SIVAL(state->data,8,unix_perms_to_wire(mode));
4481 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4482 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4484 subreq = cli_trans_send(state, /* mem ctx. */
4485 ev, /* event ctx. */
4486 cli, /* cli_state. */
4487 SMBtrans2, /* cmd. */
4488 NULL, /* pipe name. */
4492 &state->setup, /* setup. */
4493 1, /* num setup uint16_t words. */
4494 0, /* max returned setup. */
4495 state->param, /* param. */
4496 talloc_get_size(state->param),/* num param. */
4497 2, /* max returned param. */
4498 state->data, /* data. */
4500 12); /* max returned data. */
4502 if (tevent_req_nomem(subreq, req)) {
4503 return tevent_req_post(req, ev);
4505 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4509 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4510 struct event_context *ev,
4511 struct cli_state *cli,
4516 return cli_posix_open_internal_send(mem_ctx, ev,
4517 cli, fname, flags, mode, false);
4520 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4522 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4525 if (tevent_req_is_nterror(req, &status)) {
4528 *pfnum = state->fnum;
4529 return NT_STATUS_OK;
4532 /****************************************************************************
4533 Open - POSIX semantics. Doesn't request oplock.
4534 ****************************************************************************/
4536 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4537 int flags, mode_t mode, uint16_t *pfnum)
4540 TALLOC_CTX *frame = talloc_stackframe();
4541 struct event_context *ev = NULL;
4542 struct tevent_req *req = NULL;
4543 NTSTATUS status = NT_STATUS_OK;
4545 if (cli_has_async_calls(cli)) {
4547 * Can't use sync call while an async call is in flight
4549 status = NT_STATUS_INVALID_PARAMETER;
4553 ev = event_context_init(frame);
4555 status = NT_STATUS_NO_MEMORY;
4559 req = cli_posix_open_send(frame,
4566 status = NT_STATUS_NO_MEMORY;
4570 if (!tevent_req_poll(req, ev)) {
4571 status = map_nt_error_from_unix(errno);
4575 status = cli_posix_open_recv(req, pfnum);
4579 if (!NT_STATUS_IS_OK(status)) {
4580 cli_set_error(cli, status);
4585 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4586 struct event_context *ev,
4587 struct cli_state *cli,
4591 return cli_posix_open_internal_send(mem_ctx, ev,
4592 cli, fname, O_CREAT, mode, true);
4595 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4597 return tevent_req_simple_recv_ntstatus(req);
4600 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4602 TALLOC_CTX *frame = talloc_stackframe();
4603 struct event_context *ev = NULL;
4604 struct tevent_req *req = NULL;
4605 NTSTATUS status = NT_STATUS_OK;
4607 if (cli_has_async_calls(cli)) {
4609 * Can't use sync call while an async call is in flight
4611 status = NT_STATUS_INVALID_PARAMETER;
4615 ev = event_context_init(frame);
4617 status = NT_STATUS_NO_MEMORY;
4621 req = cli_posix_mkdir_send(frame,
4627 status = NT_STATUS_NO_MEMORY;
4631 if (!tevent_req_poll(req, ev)) {
4632 status = map_nt_error_from_unix(errno);
4636 status = cli_posix_mkdir_recv(req);
4640 if (!NT_STATUS_IS_OK(status)) {
4641 cli_set_error(cli, status);
4646 /****************************************************************************
4647 unlink or rmdir - POSIX semantics.
4648 ****************************************************************************/
4650 struct cli_posix_unlink_internal_state {
4654 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4656 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4657 struct event_context *ev,
4658 struct cli_state *cli,
4662 struct tevent_req *req = NULL, *subreq = NULL;
4663 struct cli_posix_unlink_internal_state *state = NULL;
4665 req = tevent_req_create(mem_ctx, &state,
4666 struct cli_posix_unlink_internal_state);
4671 /* Setup data word. */
4672 SSVAL(state->data, 0, level);
4674 subreq = cli_setpathinfo_send(state, ev, cli,
4675 SMB_POSIX_PATH_UNLINK,
4677 state->data, sizeof(state->data));
4678 if (tevent_req_nomem(subreq, req)) {
4679 return tevent_req_post(req, ev);
4681 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4685 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4687 NTSTATUS status = cli_setpathinfo_recv(subreq);
4688 tevent_req_simple_finish_ntstatus(subreq, status);
4691 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4692 struct event_context *ev,
4693 struct cli_state *cli,
4696 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4697 SMB_POSIX_UNLINK_FILE_TARGET);
4700 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4702 return tevent_req_simple_recv_ntstatus(req);
4705 /****************************************************************************
4706 unlink - POSIX semantics.
4707 ****************************************************************************/
4709 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4711 TALLOC_CTX *frame = talloc_stackframe();
4712 struct event_context *ev = NULL;
4713 struct tevent_req *req = NULL;
4714 NTSTATUS status = NT_STATUS_OK;
4716 if (cli_has_async_calls(cli)) {
4718 * Can't use sync call while an async call is in flight
4720 status = NT_STATUS_INVALID_PARAMETER;
4724 ev = event_context_init(frame);
4726 status = NT_STATUS_NO_MEMORY;
4730 req = cli_posix_unlink_send(frame,
4735 status = NT_STATUS_NO_MEMORY;
4739 if (!tevent_req_poll(req, ev)) {
4740 status = map_nt_error_from_unix(errno);
4744 status = cli_posix_unlink_recv(req);
4748 if (!NT_STATUS_IS_OK(status)) {
4749 cli_set_error(cli, status);
4754 /****************************************************************************
4755 rmdir - POSIX semantics.
4756 ****************************************************************************/
4758 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4759 struct event_context *ev,
4760 struct cli_state *cli,
4763 return cli_posix_unlink_internal_send(
4764 mem_ctx, ev, cli, fname,
4765 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4768 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4770 return tevent_req_simple_recv_ntstatus(req);
4773 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4775 TALLOC_CTX *frame = talloc_stackframe();
4776 struct event_context *ev = NULL;
4777 struct tevent_req *req = NULL;
4778 NTSTATUS status = NT_STATUS_OK;
4780 if (cli_has_async_calls(cli)) {
4782 * Can't use sync call while an async call is in flight
4784 status = NT_STATUS_INVALID_PARAMETER;
4788 ev = event_context_init(frame);
4790 status = NT_STATUS_NO_MEMORY;
4794 req = cli_posix_rmdir_send(frame,
4799 status = NT_STATUS_NO_MEMORY;
4803 if (!tevent_req_poll(req, ev)) {
4804 status = map_nt_error_from_unix(errno);
4808 status = cli_posix_rmdir_recv(req, frame);
4812 if (!NT_STATUS_IS_OK(status)) {
4813 cli_set_error(cli, status);
4818 /****************************************************************************
4820 ****************************************************************************/
4822 struct cli_notify_state {
4824 uint32_t num_changes;
4825 struct notify_change *changes;
4828 static void cli_notify_done(struct tevent_req *subreq);
4830 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4831 struct tevent_context *ev,
4832 struct cli_state *cli, uint16_t fnum,
4833 uint32_t buffer_size,
4834 uint32_t completion_filter, bool recursive)
4836 struct tevent_req *req, *subreq;
4837 struct cli_notify_state *state;
4839 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4844 SIVAL(state->setup, 0, completion_filter);
4845 SSVAL(state->setup, 4, fnum);
4846 SSVAL(state->setup, 6, recursive);
4848 subreq = cli_trans_send(
4849 state, /* mem ctx. */
4850 ev, /* event ctx. */
4851 cli, /* cli_state. */
4852 SMBnttrans, /* cmd. */
4853 NULL, /* pipe name. */
4855 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4857 (uint16_t *)state->setup, /* setup. */
4858 4, /* num setup uint16_t words. */
4859 0, /* max returned setup. */
4862 buffer_size, /* max returned param. */
4865 0); /* max returned data. */
4867 if (tevent_req_nomem(subreq, req)) {
4868 return tevent_req_post(req, ev);
4870 tevent_req_set_callback(subreq, cli_notify_done, req);
4874 static void cli_notify_done(struct tevent_req *subreq)
4876 struct tevent_req *req = tevent_req_callback_data(
4877 subreq, struct tevent_req);
4878 struct cli_notify_state *state = tevent_req_data(
4879 req, struct cli_notify_state);
4882 uint32_t i, ofs, num_params;
4885 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4886 ¶ms, 0, &num_params, NULL, 0, NULL);
4887 TALLOC_FREE(subreq);
4888 if (!NT_STATUS_IS_OK(status)) {
4889 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4890 tevent_req_nterror(req, status);
4894 state->num_changes = 0;
4897 while (num_params - ofs > 12) {
4898 uint32_t len = IVAL(params, ofs);
4899 state->num_changes += 1;
4901 if ((len == 0) || (ofs+len >= num_params)) {
4907 state->changes = talloc_array(state, struct notify_change,
4908 state->num_changes);
4909 if (tevent_req_nomem(state->changes, req)) {
4910 TALLOC_FREE(params);
4916 for (i=0; i<state->num_changes; i++) {
4917 uint32_t next = IVAL(params, ofs);
4918 uint32_t len = IVAL(params, ofs+8);
4922 if ((next != 0) && (len+12 != next)) {
4923 TALLOC_FREE(params);
4925 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4929 state->changes[i].action = IVAL(params, ofs+4);
4930 ret = clistr_pull_talloc(params, (char *)params, flags2,
4931 &name, params+ofs+12, len,
4932 STR_TERMINATE|STR_UNICODE);
4934 TALLOC_FREE(params);
4935 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4938 state->changes[i].name = name;
4942 TALLOC_FREE(params);
4943 tevent_req_done(req);
4946 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4947 uint32_t *pnum_changes,
4948 struct notify_change **pchanges)
4950 struct cli_notify_state *state = tevent_req_data(
4951 req, struct cli_notify_state);
4954 if (tevent_req_is_nterror(req, &status)) {
4958 *pnum_changes = state->num_changes;
4959 *pchanges = talloc_move(mem_ctx, &state->changes);
4960 return NT_STATUS_OK;
4963 struct cli_qpathinfo_state {
4972 static void cli_qpathinfo_done(struct tevent_req *subreq);
4974 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4975 struct tevent_context *ev,
4976 struct cli_state *cli, const char *fname,
4977 uint16_t level, uint32_t min_rdata,
4980 struct tevent_req *req, *subreq;
4981 struct cli_qpathinfo_state *state;
4983 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4987 state->min_rdata = min_rdata;
4988 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4990 state->param = talloc_zero_array(state, uint8_t, 6);
4991 if (tevent_req_nomem(state->param, req)) {
4992 return tevent_req_post(req, ev);
4994 SSVAL(state->param, 0, level);
4995 state->param = trans2_bytes_push_str(
4996 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4997 if (tevent_req_nomem(state->param, req)) {
4998 return tevent_req_post(req, ev);
5001 subreq = cli_trans_send(
5002 state, /* mem ctx. */
5003 ev, /* event ctx. */
5004 cli, /* cli_state. */
5005 SMBtrans2, /* cmd. */
5006 NULL, /* pipe name. */
5010 state->setup, /* setup. */
5011 1, /* num setup uint16_t words. */
5012 0, /* max returned setup. */
5013 state->param, /* param. */
5014 talloc_get_size(state->param), /* num param. */
5015 2, /* max returned param. */
5018 max_rdata); /* max returned data. */
5020 if (tevent_req_nomem(subreq, req)) {
5021 return tevent_req_post(req, ev);
5023 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5027 static void cli_qpathinfo_done(struct tevent_req *subreq)
5029 struct tevent_req *req = tevent_req_callback_data(
5030 subreq, struct tevent_req);
5031 struct cli_qpathinfo_state *state = tevent_req_data(
5032 req, struct cli_qpathinfo_state);
5035 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5037 &state->rdata, state->min_rdata,
5039 if (!NT_STATUS_IS_OK(status)) {
5040 tevent_req_nterror(req, status);
5043 tevent_req_done(req);
5046 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5047 uint8_t **rdata, uint32_t *num_rdata)
5049 struct cli_qpathinfo_state *state = tevent_req_data(
5050 req, struct cli_qpathinfo_state);
5053 if (tevent_req_is_nterror(req, &status)) {
5056 if (rdata != NULL) {
5057 *rdata = talloc_move(mem_ctx, &state->rdata);
5059 TALLOC_FREE(state->rdata);
5061 if (num_rdata != NULL) {
5062 *num_rdata = state->num_rdata;
5064 return NT_STATUS_OK;
5067 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5068 const char *fname, uint16_t level, uint32_t min_rdata,
5070 uint8_t **rdata, uint32_t *num_rdata)
5072 TALLOC_CTX *frame = talloc_stackframe();
5073 struct event_context *ev;
5074 struct tevent_req *req;
5075 NTSTATUS status = NT_STATUS_NO_MEMORY;
5077 if (cli_has_async_calls(cli)) {
5079 * Can't use sync call while an async call is in flight
5081 status = NT_STATUS_INVALID_PARAMETER;
5084 ev = event_context_init(frame);
5088 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5093 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5096 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5099 if (!NT_STATUS_IS_OK(status)) {
5100 cli_set_error(cli, status);
5105 struct cli_qfileinfo_state {
5114 static void cli_qfileinfo_done(struct tevent_req *subreq);
5116 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5117 struct tevent_context *ev,
5118 struct cli_state *cli, uint16_t fnum,
5119 uint16_t level, uint32_t min_rdata,
5122 struct tevent_req *req, *subreq;
5123 struct cli_qfileinfo_state *state;
5125 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5129 state->min_rdata = min_rdata;
5130 SSVAL(state->param, 0, fnum);
5131 SSVAL(state->param, 2, level);
5132 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5134 subreq = cli_trans_send(
5135 state, /* mem ctx. */
5136 ev, /* event ctx. */
5137 cli, /* cli_state. */
5138 SMBtrans2, /* cmd. */
5139 NULL, /* pipe name. */
5143 state->setup, /* setup. */
5144 1, /* num setup uint16_t words. */
5145 0, /* max returned setup. */
5146 state->param, /* param. */
5147 sizeof(state->param), /* num param. */
5148 2, /* max returned param. */
5151 max_rdata); /* max returned data. */
5153 if (tevent_req_nomem(subreq, req)) {
5154 return tevent_req_post(req, ev);
5156 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5160 static void cli_qfileinfo_done(struct tevent_req *subreq)
5162 struct tevent_req *req = tevent_req_callback_data(
5163 subreq, struct tevent_req);
5164 struct cli_qfileinfo_state *state = tevent_req_data(
5165 req, struct cli_qfileinfo_state);
5168 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5170 &state->rdata, state->min_rdata,
5172 if (!NT_STATUS_IS_OK(status)) {
5173 tevent_req_nterror(req, status);
5176 tevent_req_done(req);
5179 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5180 uint8_t **rdata, uint32_t *num_rdata)
5182 struct cli_qfileinfo_state *state = tevent_req_data(
5183 req, struct cli_qfileinfo_state);
5186 if (tevent_req_is_nterror(req, &status)) {
5189 if (rdata != NULL) {
5190 *rdata = talloc_move(mem_ctx, &state->rdata);
5192 TALLOC_FREE(state->rdata);
5194 if (num_rdata != NULL) {
5195 *num_rdata = state->num_rdata;
5197 return NT_STATUS_OK;
5200 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5201 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5203 uint8_t **rdata, uint32_t *num_rdata)
5205 TALLOC_CTX *frame = talloc_stackframe();
5206 struct event_context *ev;
5207 struct tevent_req *req;
5208 NTSTATUS status = NT_STATUS_NO_MEMORY;
5210 if (cli_has_async_calls(cli)) {
5212 * Can't use sync call while an async call is in flight
5214 status = NT_STATUS_INVALID_PARAMETER;
5217 ev = event_context_init(frame);
5221 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5226 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5229 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5232 if (!NT_STATUS_IS_OK(status)) {
5233 cli_set_error(cli, status);
5238 struct cli_flush_state {
5242 static void cli_flush_done(struct tevent_req *subreq);
5244 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5245 struct event_context *ev,
5246 struct cli_state *cli,
5249 struct tevent_req *req, *subreq;
5250 struct cli_flush_state *state;
5252 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5256 SSVAL(state->vwv + 0, 0, fnum);
5258 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5260 if (tevent_req_nomem(subreq, req)) {
5261 return tevent_req_post(req, ev);
5263 tevent_req_set_callback(subreq, cli_flush_done, req);
5267 static void cli_flush_done(struct tevent_req *subreq)
5269 struct tevent_req *req = tevent_req_callback_data(
5270 subreq, struct tevent_req);
5273 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5274 TALLOC_FREE(subreq);
5275 if (!NT_STATUS_IS_OK(status)) {
5276 tevent_req_nterror(req, status);
5279 tevent_req_done(req);
5282 NTSTATUS cli_flush_recv(struct tevent_req *req)
5284 return tevent_req_simple_recv_ntstatus(req);
5287 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5289 TALLOC_CTX *frame = talloc_stackframe();
5290 struct event_context *ev;
5291 struct tevent_req *req;
5292 NTSTATUS status = NT_STATUS_NO_MEMORY;
5294 if (cli_has_async_calls(cli)) {
5296 * Can't use sync call while an async call is in flight
5298 status = NT_STATUS_INVALID_PARAMETER;
5301 ev = event_context_init(frame);
5305 req = cli_flush_send(frame, ev, cli, fnum);
5309 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5312 status = cli_flush_recv(req);
5315 if (!NT_STATUS_IS_OK(status)) {
5316 cli_set_error(cli, status);
5321 struct cli_shadow_copy_data_state {
5328 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5330 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5331 struct tevent_context *ev,
5332 struct cli_state *cli,
5336 struct tevent_req *req, *subreq;
5337 struct cli_shadow_copy_data_state *state;
5340 req = tevent_req_create(mem_ctx, &state,
5341 struct cli_shadow_copy_data_state);
5345 state->get_names = get_names;
5346 ret_size = get_names ? cli->max_xmit : 16;
5348 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5349 SSVAL(state->setup + 2, 0, fnum);
5350 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5351 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5353 subreq = cli_trans_send(
5354 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5355 state->setup, ARRAY_SIZE(state->setup), 0,
5358 if (tevent_req_nomem(subreq, req)) {
5359 return tevent_req_post(req, ev);
5361 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5365 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5367 struct tevent_req *req = tevent_req_callback_data(
5368 subreq, struct tevent_req);
5369 struct cli_shadow_copy_data_state *state = tevent_req_data(
5370 req, struct cli_shadow_copy_data_state);
5373 status = cli_trans_recv(subreq, state, NULL,
5374 NULL, 0, NULL, /* setup */
5375 NULL, 0, NULL, /* param */
5376 &state->data, 12, &state->num_data);
5377 TALLOC_FREE(subreq);
5378 if (!NT_STATUS_IS_OK(status)) {
5379 tevent_req_nterror(req, status);
5382 tevent_req_done(req);
5385 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5386 char ***pnames, int *pnum_names)
5388 struct cli_shadow_copy_data_state *state = tevent_req_data(
5389 req, struct cli_shadow_copy_data_state);
5395 if (tevent_req_is_nterror(req, &status)) {
5398 num_names = IVAL(state->data, 4);
5399 dlength = IVAL(state->data, 8);
5401 if (!state->get_names) {
5402 *pnum_names = num_names;
5403 return NT_STATUS_OK;
5406 if (dlength+12 > state->num_data) {
5407 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5409 names = talloc_array(mem_ctx, char *, num_names);
5410 if (names == NULL) {
5411 return NT_STATUS_NO_MEMORY;
5414 for (i=0; i<num_names; i++) {
5417 size_t converted_size;
5419 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5420 ret = convert_string_talloc(
5421 names, CH_UTF16LE, CH_UNIX,
5422 src, 2 * sizeof(SHADOW_COPY_LABEL),
5423 &names[i], &converted_size);
5426 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5429 *pnum_names = num_names;
5431 return NT_STATUS_OK;
5434 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5435 uint16_t fnum, bool get_names,
5436 char ***pnames, int *pnum_names)
5438 TALLOC_CTX *frame = talloc_stackframe();
5439 struct event_context *ev;
5440 struct tevent_req *req;
5441 NTSTATUS status = NT_STATUS_NO_MEMORY;
5443 if (cli_has_async_calls(cli)) {
5445 * Can't use sync call while an async call is in flight
5447 status = NT_STATUS_INVALID_PARAMETER;
5450 ev = event_context_init(frame);
5454 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5458 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5461 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5464 if (!NT_STATUS_IS_OK(status)) {
5465 cli_set_error(cli, status);